cp/ChangeLog
[official-gcc.git] / gcc / cp / pt.c
blob481306f5f7fd80e3ce41c890cd56437e1019dab4
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, 2010, 2011
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 "tree.h"
34 #include "intl.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
42 #include "decl.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "timevar.h"
46 #include "tree-iterator.h"
47 #include "vecprim.h"
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template {
57 struct pending_template *next;
58 struct tinst_level *tinst;
61 static GTY(()) struct pending_template *pending_templates;
62 static GTY(()) struct pending_template *last_pending_template;
64 int processing_template_parmlist;
65 static int template_header_count;
67 static GTY(()) tree saved_trees;
68 static VEC(int,heap) *inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) tree saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
81 local variables. */
82 static htab_t local_specializations;
84 typedef struct GTY(()) spec_entry
86 tree tmpl;
87 tree args;
88 tree spec;
89 } spec_entry;
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree,gc) *canonical_template_parms;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 static void push_access_scope (tree);
113 static void pop_access_scope (tree);
114 static bool resolve_overloaded_unification (tree, tree, tree, tree,
115 unification_kind_t, int);
116 static int try_one_overload (tree, tree, tree, tree, tree,
117 unification_kind_t, int, bool);
118 static int unify (tree, tree, tree, tree, int);
119 static void add_pending_template (tree);
120 static tree reopen_tinst_level (struct tinst_level *);
121 static tree tsubst_initializer_list (tree, tree);
122 static tree get_class_bindings (tree, tree, tree);
123 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
124 bool, bool);
125 static void tsubst_enum (tree, tree, tree);
126 static tree add_to_template_args (tree, tree);
127 static tree add_outermost_template_args (tree, tree);
128 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
129 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
130 tree);
131 static int type_unification_real (tree, tree, tree, const tree *,
132 unsigned int, int, unification_kind_t, int);
133 static void note_template_header (int);
134 static tree convert_nontype_argument_function (tree, tree);
135 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
136 static tree convert_template_argument (tree, tree, tree,
137 tsubst_flags_t, int, tree);
138 static int for_each_template_parm (tree, tree_fn_t, void*,
139 struct pointer_set_t*, bool);
140 static tree expand_template_argument_pack (tree);
141 static tree build_template_parm_index (int, int, int, int, tree, tree);
142 static bool inline_needs_template_parms (tree);
143 static void push_inline_template_parms_recursive (tree, int);
144 static tree retrieve_local_specialization (tree);
145 static void register_local_specialization (tree, tree);
146 static hashval_t hash_specialization (const void *p);
147 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
148 static int mark_template_parm (tree, void *);
149 static int template_parm_this_level_p (tree, void *);
150 static tree tsubst_friend_function (tree, tree);
151 static tree tsubst_friend_class (tree, tree);
152 static int can_complete_type_without_circularity (tree);
153 static tree get_bindings (tree, tree, tree, bool);
154 static int template_decl_level (tree);
155 static int check_cv_quals_for_unify (int, tree, tree);
156 static void template_parm_level_and_index (tree, int*, int*);
157 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
158 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
159 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
160 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
161 static void regenerate_decl_from_template (tree, tree);
162 static tree most_specialized_class (tree, tree, tsubst_flags_t);
163 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
164 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
165 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
166 static bool check_specialization_scope (void);
167 static tree process_partial_specialization (tree);
168 static void set_current_access_from_decl (tree);
169 static tree get_template_base (tree, tree, tree, tree);
170 static tree try_class_unification (tree, tree, tree, tree);
171 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
172 tree, tree);
173 static bool template_template_parm_bindings_ok_p (tree, tree);
174 static int template_args_equal (tree, tree);
175 static void tsubst_default_arguments (tree);
176 static tree for_each_template_parm_r (tree *, int *, void *);
177 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
178 static void copy_default_args_to_explicit_spec (tree);
179 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
180 static int eq_local_specializations (const void *, const void *);
181 static bool dependent_template_arg_p (tree);
182 static bool any_template_arguments_need_structural_equality_p (tree);
183 static bool dependent_type_p_r (tree);
184 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
185 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
186 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_decl (tree, tree, tsubst_flags_t);
188 static void perform_typedefs_access_check (tree tmpl, tree targs);
189 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
190 location_t);
191 static tree listify (tree);
192 static tree listify_autos (tree, tree);
193 static tree template_parm_to_arg (tree t);
194 static tree current_template_args (void);
195 static tree fixup_template_type_parm_type (tree, int);
196 static tree fixup_template_parm_index (tree, tree, int);
197 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
199 /* Make the current scope suitable for access checking when we are
200 processing T. T can be FUNCTION_DECL for instantiated function
201 template, or VAR_DECL for static member variable (need by
202 instantiate_decl). */
204 static void
205 push_access_scope (tree t)
207 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
208 || TREE_CODE (t) == VAR_DECL);
210 if (DECL_FRIEND_CONTEXT (t))
211 push_nested_class (DECL_FRIEND_CONTEXT (t));
212 else if (DECL_CLASS_SCOPE_P (t))
213 push_nested_class (DECL_CONTEXT (t));
214 else
215 push_to_top_level ();
217 if (TREE_CODE (t) == FUNCTION_DECL)
219 saved_access_scope = tree_cons
220 (NULL_TREE, current_function_decl, saved_access_scope);
221 current_function_decl = t;
225 /* Restore the scope set up by push_access_scope. T is the node we
226 are processing. */
228 static void
229 pop_access_scope (tree t)
231 if (TREE_CODE (t) == FUNCTION_DECL)
233 current_function_decl = TREE_VALUE (saved_access_scope);
234 saved_access_scope = TREE_CHAIN (saved_access_scope);
237 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
238 pop_nested_class ();
239 else
240 pop_from_top_level ();
243 /* Do any processing required when DECL (a member template
244 declaration) is finished. Returns the TEMPLATE_DECL corresponding
245 to DECL, unless it is a specialization, in which case the DECL
246 itself is returned. */
248 tree
249 finish_member_template_decl (tree decl)
251 if (decl == error_mark_node)
252 return error_mark_node;
254 gcc_assert (DECL_P (decl));
256 if (TREE_CODE (decl) == TYPE_DECL)
258 tree type;
260 type = TREE_TYPE (decl);
261 if (type == error_mark_node)
262 return error_mark_node;
263 if (MAYBE_CLASS_TYPE_P (type)
264 && CLASSTYPE_TEMPLATE_INFO (type)
265 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
267 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
268 check_member_template (tmpl);
269 return tmpl;
271 return NULL_TREE;
273 else if (TREE_CODE (decl) == FIELD_DECL)
274 error ("data member %qD cannot be a member template", decl);
275 else if (DECL_TEMPLATE_INFO (decl))
277 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
279 check_member_template (DECL_TI_TEMPLATE (decl));
280 return DECL_TI_TEMPLATE (decl);
282 else
283 return decl;
285 else
286 error ("invalid member template declaration %qD", decl);
288 return error_mark_node;
291 /* Create a template info node. */
293 tree
294 build_template_info (tree template_decl, tree template_args)
296 tree result = make_node (TEMPLATE_INFO);
297 TI_TEMPLATE (result) = template_decl;
298 TI_ARGS (result) = template_args;
299 return result;
302 /* Return the template info node corresponding to T, whatever T is. */
304 tree
305 get_template_info (const_tree t)
307 tree tinfo = NULL_TREE;
309 if (!t || t == error_mark_node)
310 return NULL;
312 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
313 tinfo = DECL_TEMPLATE_INFO (t);
315 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
316 t = TREE_TYPE (t);
318 if (TAGGED_TYPE_P (t))
319 tinfo = TYPE_TEMPLATE_INFO (t);
320 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
321 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
323 return tinfo;
326 /* Returns the template nesting level of the indicated class TYPE.
328 For example, in:
329 template <class T>
330 struct A
332 template <class U>
333 struct B {};
336 A<T>::B<U> has depth two, while A<T> has depth one.
337 Both A<T>::B<int> and A<int>::B<U> have depth one, if
338 they are instantiations, not specializations.
340 This function is guaranteed to return 0 if passed NULL_TREE so
341 that, for example, `template_class_depth (current_class_type)' is
342 always safe. */
345 template_class_depth (tree type)
347 int depth;
349 for (depth = 0;
350 type && TREE_CODE (type) != NAMESPACE_DECL;
351 type = (TREE_CODE (type) == FUNCTION_DECL)
352 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
354 tree tinfo = get_template_info (type);
356 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
357 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
358 ++depth;
361 return depth;
364 /* Subroutine of maybe_begin_member_template_processing.
365 Returns true if processing DECL needs us to push template parms. */
367 static bool
368 inline_needs_template_parms (tree decl)
370 if (! DECL_TEMPLATE_INFO (decl))
371 return false;
373 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
374 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
377 /* Subroutine of maybe_begin_member_template_processing.
378 Push the template parms in PARMS, starting from LEVELS steps into the
379 chain, and ending at the beginning, since template parms are listed
380 innermost first. */
382 static void
383 push_inline_template_parms_recursive (tree parmlist, int levels)
385 tree parms = TREE_VALUE (parmlist);
386 int i;
388 if (levels > 1)
389 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
391 ++processing_template_decl;
392 current_template_parms
393 = tree_cons (size_int (processing_template_decl),
394 parms, current_template_parms);
395 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
397 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
398 NULL);
399 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
401 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
403 if (parm == error_mark_node)
404 continue;
406 gcc_assert (DECL_P (parm));
408 switch (TREE_CODE (parm))
410 case TYPE_DECL:
411 case TEMPLATE_DECL:
412 pushdecl (parm);
413 break;
415 case PARM_DECL:
417 /* Make a CONST_DECL as is done in process_template_parm.
418 It is ugly that we recreate this here; the original
419 version built in process_template_parm is no longer
420 available. */
421 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
422 CONST_DECL, DECL_NAME (parm),
423 TREE_TYPE (parm));
424 DECL_ARTIFICIAL (decl) = 1;
425 TREE_CONSTANT (decl) = 1;
426 TREE_READONLY (decl) = 1;
427 DECL_INITIAL (decl) = DECL_INITIAL (parm);
428 SET_DECL_TEMPLATE_PARM_P (decl);
429 pushdecl (decl);
431 break;
433 default:
434 gcc_unreachable ();
439 /* Restore the template parameter context for a member template or
440 a friend template defined in a class definition. */
442 void
443 maybe_begin_member_template_processing (tree decl)
445 tree parms;
446 int levels = 0;
448 if (inline_needs_template_parms (decl))
450 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
451 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
453 if (DECL_TEMPLATE_SPECIALIZATION (decl))
455 --levels;
456 parms = TREE_CHAIN (parms);
459 push_inline_template_parms_recursive (parms, levels);
462 /* Remember how many levels of template parameters we pushed so that
463 we can pop them later. */
464 VEC_safe_push (int, heap, inline_parm_levels, levels);
467 /* Undo the effects of maybe_begin_member_template_processing. */
469 void
470 maybe_end_member_template_processing (void)
472 int i;
473 int last;
475 if (VEC_length (int, inline_parm_levels) == 0)
476 return;
478 last = VEC_pop (int, inline_parm_levels);
479 for (i = 0; i < last; ++i)
481 --processing_template_decl;
482 current_template_parms = TREE_CHAIN (current_template_parms);
483 poplevel (0, 0, 0);
487 /* Return a new template argument vector which contains all of ARGS,
488 but has as its innermost set of arguments the EXTRA_ARGS. */
490 static tree
491 add_to_template_args (tree args, tree extra_args)
493 tree new_args;
494 int extra_depth;
495 int i;
496 int j;
498 if (args == NULL_TREE || extra_args == error_mark_node)
499 return extra_args;
501 extra_depth = TMPL_ARGS_DEPTH (extra_args);
502 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
504 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
505 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
507 for (j = 1; j <= extra_depth; ++j, ++i)
508 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
510 return new_args;
513 /* Like add_to_template_args, but only the outermost ARGS are added to
514 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
515 (EXTRA_ARGS) levels are added. This function is used to combine
516 the template arguments from a partial instantiation with the
517 template arguments used to attain the full instantiation from the
518 partial instantiation. */
520 static tree
521 add_outermost_template_args (tree args, tree extra_args)
523 tree new_args;
525 /* If there are more levels of EXTRA_ARGS than there are ARGS,
526 something very fishy is going on. */
527 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
529 /* If *all* the new arguments will be the EXTRA_ARGS, just return
530 them. */
531 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
532 return extra_args;
534 /* For the moment, we make ARGS look like it contains fewer levels. */
535 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
537 new_args = add_to_template_args (args, extra_args);
539 /* Now, we restore ARGS to its full dimensions. */
540 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
542 return new_args;
545 /* Return the N levels of innermost template arguments from the ARGS. */
547 tree
548 get_innermost_template_args (tree args, int n)
550 tree new_args;
551 int extra_levels;
552 int i;
554 gcc_assert (n >= 0);
556 /* If N is 1, just return the innermost set of template arguments. */
557 if (n == 1)
558 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
560 /* If we're not removing anything, just return the arguments we were
561 given. */
562 extra_levels = TMPL_ARGS_DEPTH (args) - n;
563 gcc_assert (extra_levels >= 0);
564 if (extra_levels == 0)
565 return args;
567 /* Make a new set of arguments, not containing the outer arguments. */
568 new_args = make_tree_vec (n);
569 for (i = 1; i <= n; ++i)
570 SET_TMPL_ARGS_LEVEL (new_args, i,
571 TMPL_ARGS_LEVEL (args, i + extra_levels));
573 return new_args;
576 /* The inverse of get_innermost_template_args: Return all but the innermost
577 EXTRA_LEVELS levels of template arguments from the ARGS. */
579 static tree
580 strip_innermost_template_args (tree args, int extra_levels)
582 tree new_args;
583 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
584 int i;
586 gcc_assert (n >= 0);
588 /* If N is 1, just return the outermost set of template arguments. */
589 if (n == 1)
590 return TMPL_ARGS_LEVEL (args, 1);
592 /* If we're not removing anything, just return the arguments we were
593 given. */
594 gcc_assert (extra_levels >= 0);
595 if (extra_levels == 0)
596 return args;
598 /* Make a new set of arguments, not containing the inner arguments. */
599 new_args = make_tree_vec (n);
600 for (i = 1; i <= n; ++i)
601 SET_TMPL_ARGS_LEVEL (new_args, i,
602 TMPL_ARGS_LEVEL (args, i));
604 return new_args;
607 /* We've got a template header coming up; push to a new level for storing
608 the parms. */
610 void
611 begin_template_parm_list (void)
613 /* We use a non-tag-transparent scope here, which causes pushtag to
614 put tags in this scope, rather than in the enclosing class or
615 namespace scope. This is the right thing, since we want
616 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
617 global template class, push_template_decl handles putting the
618 TEMPLATE_DECL into top-level scope. For a nested template class,
619 e.g.:
621 template <class T> struct S1 {
622 template <class T> struct S2 {};
625 pushtag contains special code to call pushdecl_with_scope on the
626 TEMPLATE_DECL for S2. */
627 begin_scope (sk_template_parms, NULL);
628 ++processing_template_decl;
629 ++processing_template_parmlist;
630 note_template_header (0);
633 /* This routine is called when a specialization is declared. If it is
634 invalid to declare a specialization here, an error is reported and
635 false is returned, otherwise this routine will return true. */
637 static bool
638 check_specialization_scope (void)
640 tree scope = current_scope ();
642 /* [temp.expl.spec]
644 An explicit specialization shall be declared in the namespace of
645 which the template is a member, or, for member templates, in the
646 namespace of which the enclosing class or enclosing class
647 template is a member. An explicit specialization of a member
648 function, member class or static data member of a class template
649 shall be declared in the namespace of which the class template
650 is a member. */
651 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
653 error ("explicit specialization in non-namespace scope %qD", scope);
654 return false;
657 /* [temp.expl.spec]
659 In an explicit specialization declaration for a member of a class
660 template or a member template that appears in namespace scope,
661 the member template and some of its enclosing class templates may
662 remain unspecialized, except that the declaration shall not
663 explicitly specialize a class member template if its enclosing
664 class templates are not explicitly specialized as well. */
665 if (current_template_parms)
667 error ("enclosing class templates are not explicitly specialized");
668 return false;
671 return true;
674 /* We've just seen template <>. */
676 bool
677 begin_specialization (void)
679 begin_scope (sk_template_spec, NULL);
680 note_template_header (1);
681 return check_specialization_scope ();
684 /* Called at then end of processing a declaration preceded by
685 template<>. */
687 void
688 end_specialization (void)
690 finish_scope ();
691 reset_specialization ();
694 /* Any template <>'s that we have seen thus far are not referring to a
695 function specialization. */
697 void
698 reset_specialization (void)
700 processing_specialization = 0;
701 template_header_count = 0;
704 /* We've just seen a template header. If SPECIALIZATION is nonzero,
705 it was of the form template <>. */
707 static void
708 note_template_header (int specialization)
710 processing_specialization = specialization;
711 template_header_count++;
714 /* We're beginning an explicit instantiation. */
716 void
717 begin_explicit_instantiation (void)
719 gcc_assert (!processing_explicit_instantiation);
720 processing_explicit_instantiation = true;
724 void
725 end_explicit_instantiation (void)
727 gcc_assert (processing_explicit_instantiation);
728 processing_explicit_instantiation = false;
731 /* An explicit specialization or partial specialization TMPL is being
732 declared. Check that the namespace in which the specialization is
733 occurring is permissible. Returns false iff it is invalid to
734 specialize TMPL in the current namespace. */
736 static bool
737 check_specialization_namespace (tree tmpl)
739 tree tpl_ns = decl_namespace_context (tmpl);
741 /* [tmpl.expl.spec]
743 An explicit specialization shall be declared in the namespace of
744 which the template is a member, or, for member templates, in the
745 namespace of which the enclosing class or enclosing class
746 template is a member. An explicit specialization of a member
747 function, member class or static data member of a class template
748 shall be declared in the namespace of which the class template is
749 a member. */
750 if (current_scope() != DECL_CONTEXT (tmpl)
751 && !at_namespace_scope_p ())
753 error ("specialization of %qD must appear at namespace scope", tmpl);
754 return false;
756 if (is_associated_namespace (current_namespace, tpl_ns))
757 /* Same or super-using namespace. */
758 return true;
759 else
761 permerror (input_location, "specialization of %qD in different namespace", tmpl);
762 permerror (input_location, " from definition of %q+#D", tmpl);
763 return false;
767 /* SPEC is an explicit instantiation. Check that it is valid to
768 perform this explicit instantiation in the current namespace. */
770 static void
771 check_explicit_instantiation_namespace (tree spec)
773 tree ns;
775 /* DR 275: An explicit instantiation shall appear in an enclosing
776 namespace of its template. */
777 ns = decl_namespace_context (spec);
778 if (!is_ancestor (current_namespace, ns))
779 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
780 "(which does not enclose namespace %qD)",
781 spec, current_namespace, ns);
784 /* The TYPE is being declared. If it is a template type, that means it
785 is a partial specialization. Do appropriate error-checking. */
787 tree
788 maybe_process_partial_specialization (tree type)
790 tree context;
792 if (type == error_mark_node)
793 return error_mark_node;
795 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
797 error ("name of class shadows template template parameter %qD",
798 TYPE_NAME (type));
799 return error_mark_node;
802 context = TYPE_CONTEXT (type);
804 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
806 /* This is for ordinary explicit specialization and partial
807 specialization of a template class such as:
809 template <> class C<int>;
813 template <class T> class C<T*>;
815 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
817 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
818 && !COMPLETE_TYPE_P (type))
820 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
821 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
822 if (processing_template_decl)
824 if (push_template_decl (TYPE_MAIN_DECL (type))
825 == error_mark_node)
826 return error_mark_node;
829 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
830 error ("specialization of %qT after instantiation", type);
832 else if (CLASS_TYPE_P (type)
833 && !CLASSTYPE_USE_TEMPLATE (type)
834 && CLASSTYPE_TEMPLATE_INFO (type)
835 && context && CLASS_TYPE_P (context)
836 && CLASSTYPE_TEMPLATE_INFO (context))
838 /* This is for an explicit specialization of member class
839 template according to [temp.expl.spec/18]:
841 template <> template <class U> class C<int>::D;
843 The context `C<int>' must be an implicit instantiation.
844 Otherwise this is just a member class template declared
845 earlier like:
847 template <> class C<int> { template <class U> class D; };
848 template <> template <class U> class C<int>::D;
850 In the first case, `C<int>::D' is a specialization of `C<T>::D'
851 while in the second case, `C<int>::D' is a primary template
852 and `C<T>::D' may not exist. */
854 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
855 && !COMPLETE_TYPE_P (type))
857 tree t;
858 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
860 if (current_namespace
861 != decl_namespace_context (tmpl))
863 permerror (input_location, "specializing %q#T in different namespace", type);
864 permerror (input_location, " from definition of %q+#D", tmpl);
867 /* Check for invalid specialization after instantiation:
869 template <> template <> class C<int>::D<int>;
870 template <> template <class U> class C<int>::D; */
872 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
873 t; t = TREE_CHAIN (t))
875 tree inst = TREE_VALUE (t);
876 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
878 /* We already have a full specialization of this partial
879 instantiation. Reassign it to the new member
880 specialization template. */
881 spec_entry elt;
882 spec_entry **slot;
884 elt.tmpl = most_general_template (tmpl);
885 elt.args = CLASSTYPE_TI_ARGS (inst);
886 elt.spec = inst;
888 htab_remove_elt (type_specializations, &elt);
890 elt.tmpl = tmpl;
891 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
893 slot = (spec_entry **)
894 htab_find_slot (type_specializations, &elt, INSERT);
895 *slot = ggc_alloc_spec_entry ();
896 **slot = elt;
898 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
899 /* But if we've had an implicit instantiation, that's a
900 problem ([temp.expl.spec]/6). */
901 error ("specialization %qT after instantiation %qT",
902 type, inst);
905 /* Mark TYPE as a specialization. And as a result, we only
906 have one level of template argument for the innermost
907 class template. */
908 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
909 CLASSTYPE_TI_ARGS (type)
910 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
913 else if (processing_specialization)
915 /* Someday C++0x may allow for enum template specialization. */
916 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
917 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
918 pedwarn (input_location, OPT_pedantic, "template specialization "
919 "of %qD not allowed by ISO C++", type);
920 else
922 error ("explicit specialization of non-template %qT", type);
923 return error_mark_node;
927 return type;
930 /* Returns nonzero if we can optimize the retrieval of specializations
931 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
932 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
934 static inline bool
935 optimize_specialization_lookup_p (tree tmpl)
937 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
938 && DECL_CLASS_SCOPE_P (tmpl)
939 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
940 parameter. */
941 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
942 /* The optimized lookup depends on the fact that the
943 template arguments for the member function template apply
944 purely to the containing class, which is not true if the
945 containing class is an explicit or partial
946 specialization. */
947 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
948 && !DECL_MEMBER_TEMPLATE_P (tmpl)
949 && !DECL_CONV_FN_P (tmpl)
950 /* It is possible to have a template that is not a member
951 template and is not a member of a template class:
953 template <typename T>
954 struct S { friend A::f(); };
956 Here, the friend function is a template, but the context does
957 not have template information. The optimized lookup relies
958 on having ARGS be the template arguments for both the class
959 and the function template. */
960 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
963 /* Retrieve the specialization (in the sense of [temp.spec] - a
964 specialization is either an instantiation or an explicit
965 specialization) of TMPL for the given template ARGS. If there is
966 no such specialization, return NULL_TREE. The ARGS are a vector of
967 arguments, or a vector of vectors of arguments, in the case of
968 templates with more than one level of parameters.
970 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
971 then we search for a partial specialization matching ARGS. This
972 parameter is ignored if TMPL is not a class template. */
974 static tree
975 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
977 if (args == error_mark_node)
978 return NULL_TREE;
980 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
982 /* There should be as many levels of arguments as there are
983 levels of parameters. */
984 gcc_assert (TMPL_ARGS_DEPTH (args)
985 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
987 if (optimize_specialization_lookup_p (tmpl))
989 tree class_template;
990 tree class_specialization;
991 VEC(tree,gc) *methods;
992 tree fns;
993 int idx;
995 /* The template arguments actually apply to the containing
996 class. Find the class specialization with those
997 arguments. */
998 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
999 class_specialization
1000 = retrieve_specialization (class_template, args, 0);
1001 if (!class_specialization)
1002 return NULL_TREE;
1003 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1004 for the specialization. */
1005 idx = class_method_index_for_fn (class_specialization, tmpl);
1006 if (idx == -1)
1007 return NULL_TREE;
1008 /* Iterate through the methods with the indicated name, looking
1009 for the one that has an instance of TMPL. */
1010 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1011 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1013 tree fn = OVL_CURRENT (fns);
1014 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1015 /* using-declarations can add base methods to the method vec,
1016 and we don't want those here. */
1017 && DECL_CONTEXT (fn) == class_specialization)
1018 return fn;
1020 return NULL_TREE;
1022 else
1024 spec_entry *found;
1025 spec_entry elt;
1026 htab_t specializations;
1028 elt.tmpl = tmpl;
1029 elt.args = args;
1030 elt.spec = NULL_TREE;
1032 if (DECL_CLASS_TEMPLATE_P (tmpl))
1033 specializations = type_specializations;
1034 else
1035 specializations = decl_specializations;
1037 if (hash == 0)
1038 hash = hash_specialization (&elt);
1039 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1040 if (found)
1041 return found->spec;
1044 return NULL_TREE;
1047 /* Like retrieve_specialization, but for local declarations. */
1049 static tree
1050 retrieve_local_specialization (tree tmpl)
1052 tree spec;
1054 if (local_specializations == NULL)
1055 return NULL_TREE;
1057 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1058 htab_hash_pointer (tmpl));
1059 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1062 /* Returns nonzero iff DECL is a specialization of TMPL. */
1065 is_specialization_of (tree decl, tree tmpl)
1067 tree t;
1069 if (TREE_CODE (decl) == FUNCTION_DECL)
1071 for (t = decl;
1072 t != NULL_TREE;
1073 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1074 if (t == tmpl)
1075 return 1;
1077 else
1079 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1081 for (t = TREE_TYPE (decl);
1082 t != NULL_TREE;
1083 t = CLASSTYPE_USE_TEMPLATE (t)
1084 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1085 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1086 return 1;
1089 return 0;
1092 /* Returns nonzero iff DECL is a specialization of friend declaration
1093 FRIEND_DECL according to [temp.friend]. */
1095 bool
1096 is_specialization_of_friend (tree decl, tree friend_decl)
1098 bool need_template = true;
1099 int template_depth;
1101 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1102 || TREE_CODE (decl) == TYPE_DECL);
1104 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1105 of a template class, we want to check if DECL is a specialization
1106 if this. */
1107 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1108 && DECL_TEMPLATE_INFO (friend_decl)
1109 && !DECL_USE_TEMPLATE (friend_decl))
1111 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1112 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1113 need_template = false;
1115 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1116 && !PRIMARY_TEMPLATE_P (friend_decl))
1117 need_template = false;
1119 /* There is nothing to do if this is not a template friend. */
1120 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1121 return false;
1123 if (is_specialization_of (decl, friend_decl))
1124 return true;
1126 /* [temp.friend/6]
1127 A member of a class template may be declared to be a friend of a
1128 non-template class. In this case, the corresponding member of
1129 every specialization of the class template is a friend of the
1130 class granting friendship.
1132 For example, given a template friend declaration
1134 template <class T> friend void A<T>::f();
1136 the member function below is considered a friend
1138 template <> struct A<int> {
1139 void f();
1142 For this type of template friend, TEMPLATE_DEPTH below will be
1143 nonzero. To determine if DECL is a friend of FRIEND, we first
1144 check if the enclosing class is a specialization of another. */
1146 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1147 if (template_depth
1148 && DECL_CLASS_SCOPE_P (decl)
1149 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1150 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1152 /* Next, we check the members themselves. In order to handle
1153 a few tricky cases, such as when FRIEND_DECL's are
1155 template <class T> friend void A<T>::g(T t);
1156 template <class T> template <T t> friend void A<T>::h();
1158 and DECL's are
1160 void A<int>::g(int);
1161 template <int> void A<int>::h();
1163 we need to figure out ARGS, the template arguments from
1164 the context of DECL. This is required for template substitution
1165 of `T' in the function parameter of `g' and template parameter
1166 of `h' in the above examples. Here ARGS corresponds to `int'. */
1168 tree context = DECL_CONTEXT (decl);
1169 tree args = NULL_TREE;
1170 int current_depth = 0;
1172 while (current_depth < template_depth)
1174 if (CLASSTYPE_TEMPLATE_INFO (context))
1176 if (current_depth == 0)
1177 args = TYPE_TI_ARGS (context);
1178 else
1179 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1180 current_depth++;
1182 context = TYPE_CONTEXT (context);
1185 if (TREE_CODE (decl) == FUNCTION_DECL)
1187 bool is_template;
1188 tree friend_type;
1189 tree decl_type;
1190 tree friend_args_type;
1191 tree decl_args_type;
1193 /* Make sure that both DECL and FRIEND_DECL are templates or
1194 non-templates. */
1195 is_template = DECL_TEMPLATE_INFO (decl)
1196 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1197 if (need_template ^ is_template)
1198 return false;
1199 else if (is_template)
1201 /* If both are templates, check template parameter list. */
1202 tree friend_parms
1203 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1204 args, tf_none);
1205 if (!comp_template_parms
1206 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1207 friend_parms))
1208 return false;
1210 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1212 else
1213 decl_type = TREE_TYPE (decl);
1215 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1216 tf_none, NULL_TREE);
1217 if (friend_type == error_mark_node)
1218 return false;
1220 /* Check if return types match. */
1221 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1222 return false;
1224 /* Check if function parameter types match, ignoring the
1225 `this' parameter. */
1226 friend_args_type = TYPE_ARG_TYPES (friend_type);
1227 decl_args_type = TYPE_ARG_TYPES (decl_type);
1228 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1229 friend_args_type = TREE_CHAIN (friend_args_type);
1230 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1231 decl_args_type = TREE_CHAIN (decl_args_type);
1233 return compparms (decl_args_type, friend_args_type);
1235 else
1237 /* DECL is a TYPE_DECL */
1238 bool is_template;
1239 tree decl_type = TREE_TYPE (decl);
1241 /* Make sure that both DECL and FRIEND_DECL are templates or
1242 non-templates. */
1243 is_template
1244 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1245 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1247 if (need_template ^ is_template)
1248 return false;
1249 else if (is_template)
1251 tree friend_parms;
1252 /* If both are templates, check the name of the two
1253 TEMPLATE_DECL's first because is_friend didn't. */
1254 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1255 != DECL_NAME (friend_decl))
1256 return false;
1258 /* Now check template parameter list. */
1259 friend_parms
1260 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1261 args, tf_none);
1262 return comp_template_parms
1263 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1264 friend_parms);
1266 else
1267 return (DECL_NAME (decl)
1268 == DECL_NAME (friend_decl));
1271 return false;
1274 /* Register the specialization SPEC as a specialization of TMPL with
1275 the indicated ARGS. IS_FRIEND indicates whether the specialization
1276 is actually just a friend declaration. Returns SPEC, or an
1277 equivalent prior declaration, if available. */
1279 static tree
1280 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1281 hashval_t hash)
1283 tree fn;
1284 spec_entry **slot = NULL;
1285 spec_entry elt;
1287 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1289 if (TREE_CODE (spec) == FUNCTION_DECL
1290 && uses_template_parms (DECL_TI_ARGS (spec)))
1291 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1292 register it; we want the corresponding TEMPLATE_DECL instead.
1293 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1294 the more obvious `uses_template_parms (spec)' to avoid problems
1295 with default function arguments. In particular, given
1296 something like this:
1298 template <class T> void f(T t1, T t = T())
1300 the default argument expression is not substituted for in an
1301 instantiation unless and until it is actually needed. */
1302 return spec;
1304 if (optimize_specialization_lookup_p (tmpl))
1305 /* We don't put these specializations in the hash table, but we might
1306 want to give an error about a mismatch. */
1307 fn = retrieve_specialization (tmpl, args, 0);
1308 else
1310 elt.tmpl = tmpl;
1311 elt.args = args;
1312 elt.spec = spec;
1314 if (hash == 0)
1315 hash = hash_specialization (&elt);
1317 slot = (spec_entry **)
1318 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1319 if (*slot)
1320 fn = (*slot)->spec;
1321 else
1322 fn = NULL_TREE;
1325 /* We can sometimes try to re-register a specialization that we've
1326 already got. In particular, regenerate_decl_from_template calls
1327 duplicate_decls which will update the specialization list. But,
1328 we'll still get called again here anyhow. It's more convenient
1329 to simply allow this than to try to prevent it. */
1330 if (fn == spec)
1331 return spec;
1332 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1334 if (DECL_TEMPLATE_INSTANTIATION (fn))
1336 if (DECL_ODR_USED (fn)
1337 || DECL_EXPLICIT_INSTANTIATION (fn))
1339 error ("specialization of %qD after instantiation",
1340 fn);
1341 return error_mark_node;
1343 else
1345 tree clone;
1346 /* This situation should occur only if the first
1347 specialization is an implicit instantiation, the
1348 second is an explicit specialization, and the
1349 implicit instantiation has not yet been used. That
1350 situation can occur if we have implicitly
1351 instantiated a member function and then specialized
1352 it later.
1354 We can also wind up here if a friend declaration that
1355 looked like an instantiation turns out to be a
1356 specialization:
1358 template <class T> void foo(T);
1359 class S { friend void foo<>(int) };
1360 template <> void foo(int);
1362 We transform the existing DECL in place so that any
1363 pointers to it become pointers to the updated
1364 declaration.
1366 If there was a definition for the template, but not
1367 for the specialization, we want this to look as if
1368 there were no definition, and vice versa. */
1369 DECL_INITIAL (fn) = NULL_TREE;
1370 duplicate_decls (spec, fn, is_friend);
1371 /* The call to duplicate_decls will have applied
1372 [temp.expl.spec]:
1374 An explicit specialization of a function template
1375 is inline only if it is explicitly declared to be,
1376 and independently of whether its function template
1379 to the primary function; now copy the inline bits to
1380 the various clones. */
1381 FOR_EACH_CLONE (clone, fn)
1383 DECL_DECLARED_INLINE_P (clone)
1384 = DECL_DECLARED_INLINE_P (fn);
1385 DECL_SOURCE_LOCATION (clone)
1386 = DECL_SOURCE_LOCATION (fn);
1388 check_specialization_namespace (fn);
1390 return fn;
1393 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1395 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1396 /* Dup decl failed, but this is a new definition. Set the
1397 line number so any errors match this new
1398 definition. */
1399 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1401 return fn;
1404 else if (fn)
1405 return duplicate_decls (spec, fn, is_friend);
1407 /* A specialization must be declared in the same namespace as the
1408 template it is specializing. */
1409 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1410 && !check_specialization_namespace (tmpl))
1411 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1413 if (!optimize_specialization_lookup_p (tmpl))
1415 gcc_assert (tmpl && args && spec);
1416 *slot = ggc_alloc_spec_entry ();
1417 **slot = elt;
1418 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1419 && PRIMARY_TEMPLATE_P (tmpl)
1420 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1421 /* TMPL is a forward declaration of a template function; keep a list
1422 of all specializations in case we need to reassign them to a friend
1423 template later in tsubst_friend_function. */
1424 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1425 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1428 return spec;
1431 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1432 TMPL and ARGS members, ignores SPEC. */
1434 static int
1435 eq_specializations (const void *p1, const void *p2)
1437 const spec_entry *e1 = (const spec_entry *)p1;
1438 const spec_entry *e2 = (const spec_entry *)p2;
1440 return (e1->tmpl == e2->tmpl
1441 && comp_template_args (e1->args, e2->args));
1444 /* Returns a hash for a template TMPL and template arguments ARGS. */
1446 static hashval_t
1447 hash_tmpl_and_args (tree tmpl, tree args)
1449 hashval_t val = DECL_UID (tmpl);
1450 return iterative_hash_template_arg (args, val);
1453 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1454 ignoring SPEC. */
1456 static hashval_t
1457 hash_specialization (const void *p)
1459 const spec_entry *e = (const spec_entry *)p;
1460 return hash_tmpl_and_args (e->tmpl, e->args);
1463 /* Recursively calculate a hash value for a template argument ARG, for use
1464 in the hash tables of template specializations. */
1466 hashval_t
1467 iterative_hash_template_arg (tree arg, hashval_t val)
1469 unsigned HOST_WIDE_INT i;
1470 enum tree_code code;
1471 char tclass;
1473 if (arg == NULL_TREE)
1474 return iterative_hash_object (arg, val);
1476 if (!TYPE_P (arg))
1477 STRIP_NOPS (arg);
1479 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1480 /* We can get one of these when re-hashing a previous entry in the middle
1481 of substituting into a pack expansion. Just look through it. */
1482 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1484 code = TREE_CODE (arg);
1485 tclass = TREE_CODE_CLASS (code);
1487 val = iterative_hash_object (code, val);
1489 switch (code)
1491 case ERROR_MARK:
1492 return val;
1494 case IDENTIFIER_NODE:
1495 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1497 case TREE_VEC:
1499 int i, len = TREE_VEC_LENGTH (arg);
1500 for (i = 0; i < len; ++i)
1501 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1502 return val;
1505 case TYPE_PACK_EXPANSION:
1506 case EXPR_PACK_EXPANSION:
1507 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1509 case TYPE_ARGUMENT_PACK:
1510 case NONTYPE_ARGUMENT_PACK:
1511 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1513 case TREE_LIST:
1514 for (; arg; arg = TREE_CHAIN (arg))
1515 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1516 return val;
1518 case OVERLOAD:
1519 for (; arg; arg = OVL_NEXT (arg))
1520 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1521 return val;
1523 case CONSTRUCTOR:
1525 tree field, value;
1526 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1528 val = iterative_hash_template_arg (field, val);
1529 val = iterative_hash_template_arg (value, val);
1531 return val;
1534 case PARM_DECL:
1535 if (!DECL_ARTIFICIAL (arg))
1537 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1538 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1540 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1542 case TARGET_EXPR:
1543 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1545 case PTRMEM_CST:
1546 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1547 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1549 case TEMPLATE_PARM_INDEX:
1550 val = iterative_hash_template_arg
1551 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1552 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1553 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1555 case TRAIT_EXPR:
1556 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1557 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1558 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1560 case BASELINK:
1561 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1562 val);
1563 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1564 val);
1566 case MODOP_EXPR:
1567 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1568 code = TREE_CODE (TREE_OPERAND (arg, 1));
1569 val = iterative_hash_object (code, val);
1570 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1572 case LAMBDA_EXPR:
1573 /* A lambda can't appear in a template arg, but don't crash on
1574 erroneous input. */
1575 gcc_assert (seen_error ());
1576 return val;
1578 case CAST_EXPR:
1579 case STATIC_CAST_EXPR:
1580 case REINTERPRET_CAST_EXPR:
1581 case CONST_CAST_EXPR:
1582 case DYNAMIC_CAST_EXPR:
1583 case NEW_EXPR:
1584 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1585 /* Now hash operands as usual. */
1586 break;
1588 default:
1589 break;
1592 switch (tclass)
1594 case tcc_type:
1595 if (TYPE_CANONICAL (arg))
1596 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1597 val);
1598 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1599 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1600 /* Otherwise just compare the types during lookup. */
1601 return val;
1603 case tcc_declaration:
1604 case tcc_constant:
1605 return iterative_hash_expr (arg, val);
1607 default:
1608 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1610 unsigned n = TREE_OPERAND_LENGTH (arg);
1611 for (i = 0; i < n; ++i)
1612 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1613 return val;
1616 gcc_unreachable ();
1617 return 0;
1620 /* Unregister the specialization SPEC as a specialization of TMPL.
1621 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1622 if the SPEC was listed as a specialization of TMPL.
1624 Note that SPEC has been ggc_freed, so we can't look inside it. */
1626 bool
1627 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1629 spec_entry **slot;
1630 spec_entry elt;
1632 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1633 elt.args = TI_ARGS (tinfo);
1634 elt.spec = NULL_TREE;
1636 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1637 if (*slot)
1639 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1640 gcc_assert (new_spec != NULL_TREE);
1641 (*slot)->spec = new_spec;
1642 return 1;
1645 return 0;
1648 /* Compare an entry in the local specializations hash table P1 (which
1649 is really a pointer to a TREE_LIST) with P2 (which is really a
1650 DECL). */
1652 static int
1653 eq_local_specializations (const void *p1, const void *p2)
1655 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1658 /* Hash P1, an entry in the local specializations table. */
1660 static hashval_t
1661 hash_local_specialization (const void* p1)
1663 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1666 /* Like register_specialization, but for local declarations. We are
1667 registering SPEC, an instantiation of TMPL. */
1669 static void
1670 register_local_specialization (tree spec, tree tmpl)
1672 void **slot;
1674 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1675 htab_hash_pointer (tmpl), INSERT);
1676 *slot = build_tree_list (spec, tmpl);
1679 /* TYPE is a class type. Returns true if TYPE is an explicitly
1680 specialized class. */
1682 bool
1683 explicit_class_specialization_p (tree type)
1685 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1686 return false;
1687 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1690 /* Print the list of functions at FNS, going through all the overloads
1691 for each element of the list. Alternatively, FNS can not be a
1692 TREE_LIST, in which case it will be printed together with all the
1693 overloads.
1695 MORE and *STR should respectively be FALSE and NULL when the function
1696 is called from the outside. They are used internally on recursive
1697 calls. print_candidates manages the two parameters and leaves NULL
1698 in *STR when it ends. */
1700 static void
1701 print_candidates_1 (tree fns, bool more, const char **str)
1703 tree fn, fn2;
1704 char *spaces = NULL;
1706 for (fn = fns; fn; fn = OVL_NEXT (fn))
1707 if (TREE_CODE (fn) == TREE_LIST)
1709 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1710 print_candidates_1 (TREE_VALUE (fn2),
1711 TREE_CHAIN (fn2) || more, str);
1713 else
1715 if (!*str)
1717 /* Pick the prefix string. */
1718 if (!more && !OVL_NEXT (fns))
1720 error ("candidate is: %+#D", OVL_CURRENT (fn));
1721 continue;
1724 *str = _("candidates are:");
1725 spaces = get_spaces (*str);
1727 error ("%s %+#D", *str, OVL_CURRENT (fn));
1728 *str = spaces ? spaces : *str;
1731 if (!more)
1733 free (spaces);
1734 *str = NULL;
1738 /* Print the list of candidate FNS in an error message. FNS can also
1739 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1741 void
1742 print_candidates (tree fns)
1744 const char *str = NULL;
1745 print_candidates_1 (fns, false, &str);
1746 gcc_assert (str == NULL);
1749 /* Returns the template (one of the functions given by TEMPLATE_ID)
1750 which can be specialized to match the indicated DECL with the
1751 explicit template args given in TEMPLATE_ID. The DECL may be
1752 NULL_TREE if none is available. In that case, the functions in
1753 TEMPLATE_ID are non-members.
1755 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1756 specialization of a member template.
1758 The TEMPLATE_COUNT is the number of references to qualifying
1759 template classes that appeared in the name of the function. See
1760 check_explicit_specialization for a more accurate description.
1762 TSK indicates what kind of template declaration (if any) is being
1763 declared. TSK_TEMPLATE indicates that the declaration given by
1764 DECL, though a FUNCTION_DECL, has template parameters, and is
1765 therefore a template function.
1767 The template args (those explicitly specified and those deduced)
1768 are output in a newly created vector *TARGS_OUT.
1770 If it is impossible to determine the result, an error message is
1771 issued. The error_mark_node is returned to indicate failure. */
1773 static tree
1774 determine_specialization (tree template_id,
1775 tree decl,
1776 tree* targs_out,
1777 int need_member_template,
1778 int template_count,
1779 tmpl_spec_kind tsk)
1781 tree fns;
1782 tree targs;
1783 tree explicit_targs;
1784 tree candidates = NULL_TREE;
1785 /* A TREE_LIST of templates of which DECL may be a specialization.
1786 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1787 corresponding TREE_PURPOSE is the set of template arguments that,
1788 when used to instantiate the template, would produce a function
1789 with the signature of DECL. */
1790 tree templates = NULL_TREE;
1791 int header_count;
1792 struct cp_binding_level *b;
1794 *targs_out = NULL_TREE;
1796 if (template_id == error_mark_node || decl == error_mark_node)
1797 return error_mark_node;
1799 fns = TREE_OPERAND (template_id, 0);
1800 explicit_targs = TREE_OPERAND (template_id, 1);
1802 if (fns == error_mark_node)
1803 return error_mark_node;
1805 /* Check for baselinks. */
1806 if (BASELINK_P (fns))
1807 fns = BASELINK_FUNCTIONS (fns);
1809 if (!is_overloaded_fn (fns))
1811 error ("%qD is not a function template", fns);
1812 return error_mark_node;
1815 /* Count the number of template headers specified for this
1816 specialization. */
1817 header_count = 0;
1818 for (b = current_binding_level;
1819 b->kind == sk_template_parms;
1820 b = b->level_chain)
1821 ++header_count;
1823 for (; fns; fns = OVL_NEXT (fns))
1825 tree fn = OVL_CURRENT (fns);
1827 if (TREE_CODE (fn) == TEMPLATE_DECL)
1829 tree decl_arg_types;
1830 tree fn_arg_types;
1832 /* In case of explicit specialization, we need to check if
1833 the number of template headers appearing in the specialization
1834 is correct. This is usually done in check_explicit_specialization,
1835 but the check done there cannot be exhaustive when specializing
1836 member functions. Consider the following code:
1838 template <> void A<int>::f(int);
1839 template <> template <> void A<int>::f(int);
1841 Assuming that A<int> is not itself an explicit specialization
1842 already, the first line specializes "f" which is a non-template
1843 member function, whilst the second line specializes "f" which
1844 is a template member function. So both lines are syntactically
1845 correct, and check_explicit_specialization does not reject
1846 them.
1848 Here, we can do better, as we are matching the specialization
1849 against the declarations. We count the number of template
1850 headers, and we check if they match TEMPLATE_COUNT + 1
1851 (TEMPLATE_COUNT is the number of qualifying template classes,
1852 plus there must be another header for the member template
1853 itself).
1855 Notice that if header_count is zero, this is not a
1856 specialization but rather a template instantiation, so there
1857 is no check we can perform here. */
1858 if (header_count && header_count != template_count + 1)
1859 continue;
1861 /* Check that the number of template arguments at the
1862 innermost level for DECL is the same as for FN. */
1863 if (current_binding_level->kind == sk_template_parms
1864 && !current_binding_level->explicit_spec_p
1865 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1866 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1867 (current_template_parms))))
1868 continue;
1870 /* DECL might be a specialization of FN. */
1871 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1872 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1874 /* For a non-static member function, we need to make sure
1875 that the const qualification is the same. Since
1876 get_bindings does not try to merge the "this" parameter,
1877 we must do the comparison explicitly. */
1878 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1879 && !same_type_p (TREE_VALUE (fn_arg_types),
1880 TREE_VALUE (decl_arg_types)))
1881 continue;
1883 /* Skip the "this" parameter and, for constructors of
1884 classes with virtual bases, the VTT parameter. A
1885 full specialization of a constructor will have a VTT
1886 parameter, but a template never will. */
1887 decl_arg_types
1888 = skip_artificial_parms_for (decl, decl_arg_types);
1889 fn_arg_types
1890 = skip_artificial_parms_for (fn, fn_arg_types);
1892 /* Check that the number of function parameters matches.
1893 For example,
1894 template <class T> void f(int i = 0);
1895 template <> void f<int>();
1896 The specialization f<int> is invalid but is not caught
1897 by get_bindings below. */
1898 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1899 continue;
1901 /* Function templates cannot be specializations; there are
1902 no partial specializations of functions. Therefore, if
1903 the type of DECL does not match FN, there is no
1904 match. */
1905 if (tsk == tsk_template)
1907 if (compparms (fn_arg_types, decl_arg_types))
1908 candidates = tree_cons (NULL_TREE, fn, candidates);
1909 continue;
1912 /* See whether this function might be a specialization of this
1913 template. */
1914 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1916 if (!targs)
1917 /* We cannot deduce template arguments that when used to
1918 specialize TMPL will produce DECL. */
1919 continue;
1921 /* Save this template, and the arguments deduced. */
1922 templates = tree_cons (targs, fn, templates);
1924 else if (need_member_template)
1925 /* FN is an ordinary member function, and we need a
1926 specialization of a member template. */
1928 else if (TREE_CODE (fn) != FUNCTION_DECL)
1929 /* We can get IDENTIFIER_NODEs here in certain erroneous
1930 cases. */
1932 else if (!DECL_FUNCTION_MEMBER_P (fn))
1933 /* This is just an ordinary non-member function. Nothing can
1934 be a specialization of that. */
1936 else if (DECL_ARTIFICIAL (fn))
1937 /* Cannot specialize functions that are created implicitly. */
1939 else
1941 tree decl_arg_types;
1943 /* This is an ordinary member function. However, since
1944 we're here, we can assume it's enclosing class is a
1945 template class. For example,
1947 template <typename T> struct S { void f(); };
1948 template <> void S<int>::f() {}
1950 Here, S<int>::f is a non-template, but S<int> is a
1951 template class. If FN has the same type as DECL, we
1952 might be in business. */
1954 if (!DECL_TEMPLATE_INFO (fn))
1955 /* Its enclosing class is an explicit specialization
1956 of a template class. This is not a candidate. */
1957 continue;
1959 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1960 TREE_TYPE (TREE_TYPE (fn))))
1961 /* The return types differ. */
1962 continue;
1964 /* Adjust the type of DECL in case FN is a static member. */
1965 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1966 if (DECL_STATIC_FUNCTION_P (fn)
1967 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1968 decl_arg_types = TREE_CHAIN (decl_arg_types);
1970 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1971 decl_arg_types))
1972 /* They match! */
1973 candidates = tree_cons (NULL_TREE, fn, candidates);
1977 if (templates && TREE_CHAIN (templates))
1979 /* We have:
1981 [temp.expl.spec]
1983 It is possible for a specialization with a given function
1984 signature to be instantiated from more than one function
1985 template. In such cases, explicit specification of the
1986 template arguments must be used to uniquely identify the
1987 function template specialization being specialized.
1989 Note that here, there's no suggestion that we're supposed to
1990 determine which of the candidate templates is most
1991 specialized. However, we, also have:
1993 [temp.func.order]
1995 Partial ordering of overloaded function template
1996 declarations is used in the following contexts to select
1997 the function template to which a function template
1998 specialization refers:
2000 -- when an explicit specialization refers to a function
2001 template.
2003 So, we do use the partial ordering rules, at least for now.
2004 This extension can only serve to make invalid programs valid,
2005 so it's safe. And, there is strong anecdotal evidence that
2006 the committee intended the partial ordering rules to apply;
2007 the EDG front end has that behavior, and John Spicer claims
2008 that the committee simply forgot to delete the wording in
2009 [temp.expl.spec]. */
2010 tree tmpl = most_specialized_instantiation (templates);
2011 if (tmpl != error_mark_node)
2013 templates = tmpl;
2014 TREE_CHAIN (templates) = NULL_TREE;
2018 if (templates == NULL_TREE && candidates == NULL_TREE)
2020 error ("template-id %qD for %q+D does not match any template "
2021 "declaration", template_id, decl);
2022 if (header_count && header_count != template_count + 1)
2023 inform (input_location, "saw %d %<template<>%>, need %d for "
2024 "specializing a member function template",
2025 header_count, template_count + 1);
2026 return error_mark_node;
2028 else if ((templates && TREE_CHAIN (templates))
2029 || (candidates && TREE_CHAIN (candidates))
2030 || (templates && candidates))
2032 error ("ambiguous template specialization %qD for %q+D",
2033 template_id, decl);
2034 candidates = chainon (candidates, templates);
2035 print_candidates (candidates);
2036 return error_mark_node;
2039 /* We have one, and exactly one, match. */
2040 if (candidates)
2042 tree fn = TREE_VALUE (candidates);
2043 *targs_out = copy_node (DECL_TI_ARGS (fn));
2044 /* DECL is a re-declaration or partial instantiation of a template
2045 function. */
2046 if (TREE_CODE (fn) == TEMPLATE_DECL)
2047 return fn;
2048 /* It was a specialization of an ordinary member function in a
2049 template class. */
2050 return DECL_TI_TEMPLATE (fn);
2053 /* It was a specialization of a template. */
2054 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2055 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2057 *targs_out = copy_node (targs);
2058 SET_TMPL_ARGS_LEVEL (*targs_out,
2059 TMPL_ARGS_DEPTH (*targs_out),
2060 TREE_PURPOSE (templates));
2062 else
2063 *targs_out = TREE_PURPOSE (templates);
2064 return TREE_VALUE (templates);
2067 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2068 but with the default argument values filled in from those in the
2069 TMPL_TYPES. */
2071 static tree
2072 copy_default_args_to_explicit_spec_1 (tree spec_types,
2073 tree tmpl_types)
2075 tree new_spec_types;
2077 if (!spec_types)
2078 return NULL_TREE;
2080 if (spec_types == void_list_node)
2081 return void_list_node;
2083 /* Substitute into the rest of the list. */
2084 new_spec_types =
2085 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2086 TREE_CHAIN (tmpl_types));
2088 /* Add the default argument for this parameter. */
2089 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2090 TREE_VALUE (spec_types),
2091 new_spec_types);
2094 /* DECL is an explicit specialization. Replicate default arguments
2095 from the template it specializes. (That way, code like:
2097 template <class T> void f(T = 3);
2098 template <> void f(double);
2099 void g () { f (); }
2101 works, as required.) An alternative approach would be to look up
2102 the correct default arguments at the call-site, but this approach
2103 is consistent with how implicit instantiations are handled. */
2105 static void
2106 copy_default_args_to_explicit_spec (tree decl)
2108 tree tmpl;
2109 tree spec_types;
2110 tree tmpl_types;
2111 tree new_spec_types;
2112 tree old_type;
2113 tree new_type;
2114 tree t;
2115 tree object_type = NULL_TREE;
2116 tree in_charge = NULL_TREE;
2117 tree vtt = NULL_TREE;
2119 /* See if there's anything we need to do. */
2120 tmpl = DECL_TI_TEMPLATE (decl);
2121 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2122 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2123 if (TREE_PURPOSE (t))
2124 break;
2125 if (!t)
2126 return;
2128 old_type = TREE_TYPE (decl);
2129 spec_types = TYPE_ARG_TYPES (old_type);
2131 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2133 /* Remove the this pointer, but remember the object's type for
2134 CV quals. */
2135 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2136 spec_types = TREE_CHAIN (spec_types);
2137 tmpl_types = TREE_CHAIN (tmpl_types);
2139 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2141 /* DECL may contain more parameters than TMPL due to the extra
2142 in-charge parameter in constructors and destructors. */
2143 in_charge = spec_types;
2144 spec_types = TREE_CHAIN (spec_types);
2146 if (DECL_HAS_VTT_PARM_P (decl))
2148 vtt = spec_types;
2149 spec_types = TREE_CHAIN (spec_types);
2153 /* Compute the merged default arguments. */
2154 new_spec_types =
2155 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2157 /* Compute the new FUNCTION_TYPE. */
2158 if (object_type)
2160 if (vtt)
2161 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2162 TREE_VALUE (vtt),
2163 new_spec_types);
2165 if (in_charge)
2166 /* Put the in-charge parameter back. */
2167 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2168 TREE_VALUE (in_charge),
2169 new_spec_types);
2171 new_type = build_method_type_directly (object_type,
2172 TREE_TYPE (old_type),
2173 new_spec_types);
2175 else
2176 new_type = build_function_type (TREE_TYPE (old_type),
2177 new_spec_types);
2178 new_type = cp_build_type_attribute_variant (new_type,
2179 TYPE_ATTRIBUTES (old_type));
2180 new_type = build_exception_variant (new_type,
2181 TYPE_RAISES_EXCEPTIONS (old_type));
2182 TREE_TYPE (decl) = new_type;
2185 /* Check to see if the function just declared, as indicated in
2186 DECLARATOR, and in DECL, is a specialization of a function
2187 template. We may also discover that the declaration is an explicit
2188 instantiation at this point.
2190 Returns DECL, or an equivalent declaration that should be used
2191 instead if all goes well. Issues an error message if something is
2192 amiss. Returns error_mark_node if the error is not easily
2193 recoverable.
2195 FLAGS is a bitmask consisting of the following flags:
2197 2: The function has a definition.
2198 4: The function is a friend.
2200 The TEMPLATE_COUNT is the number of references to qualifying
2201 template classes that appeared in the name of the function. For
2202 example, in
2204 template <class T> struct S { void f(); };
2205 void S<int>::f();
2207 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2208 classes are not counted in the TEMPLATE_COUNT, so that in
2210 template <class T> struct S {};
2211 template <> struct S<int> { void f(); }
2212 template <> void S<int>::f();
2214 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2215 invalid; there should be no template <>.)
2217 If the function is a specialization, it is marked as such via
2218 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2219 is set up correctly, and it is added to the list of specializations
2220 for that template. */
2222 tree
2223 check_explicit_specialization (tree declarator,
2224 tree decl,
2225 int template_count,
2226 int flags)
2228 int have_def = flags & 2;
2229 int is_friend = flags & 4;
2230 int specialization = 0;
2231 int explicit_instantiation = 0;
2232 int member_specialization = 0;
2233 tree ctype = DECL_CLASS_CONTEXT (decl);
2234 tree dname = DECL_NAME (decl);
2235 tmpl_spec_kind tsk;
2237 if (is_friend)
2239 if (!processing_specialization)
2240 tsk = tsk_none;
2241 else
2242 tsk = tsk_excessive_parms;
2244 else
2245 tsk = current_tmpl_spec_kind (template_count);
2247 switch (tsk)
2249 case tsk_none:
2250 if (processing_specialization)
2252 specialization = 1;
2253 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2255 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2257 if (is_friend)
2258 /* This could be something like:
2260 template <class T> void f(T);
2261 class S { friend void f<>(int); } */
2262 specialization = 1;
2263 else
2265 /* This case handles bogus declarations like template <>
2266 template <class T> void f<int>(); */
2268 error ("template-id %qD in declaration of primary template",
2269 declarator);
2270 return decl;
2273 break;
2275 case tsk_invalid_member_spec:
2276 /* The error has already been reported in
2277 check_specialization_scope. */
2278 return error_mark_node;
2280 case tsk_invalid_expl_inst:
2281 error ("template parameter list used in explicit instantiation");
2283 /* Fall through. */
2285 case tsk_expl_inst:
2286 if (have_def)
2287 error ("definition provided for explicit instantiation");
2289 explicit_instantiation = 1;
2290 break;
2292 case tsk_excessive_parms:
2293 case tsk_insufficient_parms:
2294 if (tsk == tsk_excessive_parms)
2295 error ("too many template parameter lists in declaration of %qD",
2296 decl);
2297 else if (template_header_count)
2298 error("too few template parameter lists in declaration of %qD", decl);
2299 else
2300 error("explicit specialization of %qD must be introduced by "
2301 "%<template <>%>", decl);
2303 /* Fall through. */
2304 case tsk_expl_spec:
2305 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2306 if (ctype)
2307 member_specialization = 1;
2308 else
2309 specialization = 1;
2310 break;
2312 case tsk_template:
2313 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2315 /* This case handles bogus declarations like template <>
2316 template <class T> void f<int>(); */
2318 if (uses_template_parms (declarator))
2319 error ("function template partial specialization %qD "
2320 "is not allowed", declarator);
2321 else
2322 error ("template-id %qD in declaration of primary template",
2323 declarator);
2324 return decl;
2327 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2328 /* This is a specialization of a member template, without
2329 specialization the containing class. Something like:
2331 template <class T> struct S {
2332 template <class U> void f (U);
2334 template <> template <class U> void S<int>::f(U) {}
2336 That's a specialization -- but of the entire template. */
2337 specialization = 1;
2338 break;
2340 default:
2341 gcc_unreachable ();
2344 if (specialization || member_specialization)
2346 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2347 for (; t; t = TREE_CHAIN (t))
2348 if (TREE_PURPOSE (t))
2350 permerror (input_location,
2351 "default argument specified in explicit specialization");
2352 break;
2356 if (specialization || member_specialization || explicit_instantiation)
2358 tree tmpl = NULL_TREE;
2359 tree targs = NULL_TREE;
2361 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2362 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2364 tree fns;
2366 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2367 if (ctype)
2368 fns = dname;
2369 else
2371 /* If there is no class context, the explicit instantiation
2372 must be at namespace scope. */
2373 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2375 /* Find the namespace binding, using the declaration
2376 context. */
2377 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2378 false, true);
2379 if (fns == error_mark_node || !is_overloaded_fn (fns))
2381 error ("%qD is not a template function", dname);
2382 fns = error_mark_node;
2384 else
2386 tree fn = OVL_CURRENT (fns);
2387 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2388 CP_DECL_CONTEXT (fn)))
2389 error ("%qD is not declared in %qD",
2390 decl, current_namespace);
2394 declarator = lookup_template_function (fns, NULL_TREE);
2397 if (declarator == error_mark_node)
2398 return error_mark_node;
2400 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2402 if (!explicit_instantiation)
2403 /* A specialization in class scope. This is invalid,
2404 but the error will already have been flagged by
2405 check_specialization_scope. */
2406 return error_mark_node;
2407 else
2409 /* It's not valid to write an explicit instantiation in
2410 class scope, e.g.:
2412 class C { template void f(); }
2414 This case is caught by the parser. However, on
2415 something like:
2417 template class C { void f(); };
2419 (which is invalid) we can get here. The error will be
2420 issued later. */
2424 return decl;
2426 else if (ctype != NULL_TREE
2427 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2428 IDENTIFIER_NODE))
2430 /* Find the list of functions in ctype that have the same
2431 name as the declared function. */
2432 tree name = TREE_OPERAND (declarator, 0);
2433 tree fns = NULL_TREE;
2434 int idx;
2436 if (constructor_name_p (name, ctype))
2438 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2440 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2441 : !CLASSTYPE_DESTRUCTORS (ctype))
2443 /* From [temp.expl.spec]:
2445 If such an explicit specialization for the member
2446 of a class template names an implicitly-declared
2447 special member function (clause _special_), the
2448 program is ill-formed.
2450 Similar language is found in [temp.explicit]. */
2451 error ("specialization of implicitly-declared special member function");
2452 return error_mark_node;
2455 name = is_constructor ? ctor_identifier : dtor_identifier;
2458 if (!DECL_CONV_FN_P (decl))
2460 idx = lookup_fnfields_1 (ctype, name);
2461 if (idx >= 0)
2462 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2464 else
2466 VEC(tree,gc) *methods;
2467 tree ovl;
2469 /* For a type-conversion operator, we cannot do a
2470 name-based lookup. We might be looking for `operator
2471 int' which will be a specialization of `operator T'.
2472 So, we find *all* the conversion operators, and then
2473 select from them. */
2474 fns = NULL_TREE;
2476 methods = CLASSTYPE_METHOD_VEC (ctype);
2477 if (methods)
2478 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2479 VEC_iterate (tree, methods, idx, ovl);
2480 ++idx)
2482 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2483 /* There are no more conversion functions. */
2484 break;
2486 /* Glue all these conversion functions together
2487 with those we already have. */
2488 for (; ovl; ovl = OVL_NEXT (ovl))
2489 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2493 if (fns == NULL_TREE)
2495 error ("no member function %qD declared in %qT", name, ctype);
2496 return error_mark_node;
2498 else
2499 TREE_OPERAND (declarator, 0) = fns;
2502 /* Figure out what exactly is being specialized at this point.
2503 Note that for an explicit instantiation, even one for a
2504 member function, we cannot tell apriori whether the
2505 instantiation is for a member template, or just a member
2506 function of a template class. Even if a member template is
2507 being instantiated, the member template arguments may be
2508 elided if they can be deduced from the rest of the
2509 declaration. */
2510 tmpl = determine_specialization (declarator, decl,
2511 &targs,
2512 member_specialization,
2513 template_count,
2514 tsk);
2516 if (!tmpl || tmpl == error_mark_node)
2517 /* We couldn't figure out what this declaration was
2518 specializing. */
2519 return error_mark_node;
2520 else
2522 tree gen_tmpl = most_general_template (tmpl);
2524 if (explicit_instantiation)
2526 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2527 is done by do_decl_instantiation later. */
2529 int arg_depth = TMPL_ARGS_DEPTH (targs);
2530 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2532 if (arg_depth > parm_depth)
2534 /* If TMPL is not the most general template (for
2535 example, if TMPL is a friend template that is
2536 injected into namespace scope), then there will
2537 be too many levels of TARGS. Remove some of them
2538 here. */
2539 int i;
2540 tree new_targs;
2542 new_targs = make_tree_vec (parm_depth);
2543 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2544 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2545 = TREE_VEC_ELT (targs, i);
2546 targs = new_targs;
2549 return instantiate_template (tmpl, targs, tf_error);
2552 /* If we thought that the DECL was a member function, but it
2553 turns out to be specializing a static member function,
2554 make DECL a static member function as well. */
2555 if (DECL_STATIC_FUNCTION_P (tmpl)
2556 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2557 revert_static_member_fn (decl);
2559 /* If this is a specialization of a member template of a
2560 template class, we want to return the TEMPLATE_DECL, not
2561 the specialization of it. */
2562 if (tsk == tsk_template)
2564 tree result = DECL_TEMPLATE_RESULT (tmpl);
2565 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2566 DECL_INITIAL (result) = NULL_TREE;
2567 if (have_def)
2569 tree parm;
2570 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2571 DECL_SOURCE_LOCATION (result)
2572 = DECL_SOURCE_LOCATION (decl);
2573 /* We want to use the argument list specified in the
2574 definition, not in the original declaration. */
2575 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2576 for (parm = DECL_ARGUMENTS (result); parm;
2577 parm = DECL_CHAIN (parm))
2578 DECL_CONTEXT (parm) = result;
2580 return register_specialization (tmpl, gen_tmpl, targs,
2581 is_friend, 0);
2584 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2585 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2587 /* Inherit default function arguments from the template
2588 DECL is specializing. */
2589 copy_default_args_to_explicit_spec (decl);
2591 /* This specialization has the same protection as the
2592 template it specializes. */
2593 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2594 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2596 /* 7.1.1-1 [dcl.stc]
2598 A storage-class-specifier shall not be specified in an
2599 explicit specialization...
2601 The parser rejects these, so unless action is taken here,
2602 explicit function specializations will always appear with
2603 global linkage.
2605 The action recommended by the C++ CWG in response to C++
2606 defect report 605 is to make the storage class and linkage
2607 of the explicit specialization match the templated function:
2609 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2611 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2613 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2614 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2616 /* This specialization has the same linkage and visibility as
2617 the function template it specializes. */
2618 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2619 if (! TREE_PUBLIC (decl))
2621 DECL_INTERFACE_KNOWN (decl) = 1;
2622 DECL_NOT_REALLY_EXTERN (decl) = 1;
2624 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2625 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2627 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2628 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2632 /* If DECL is a friend declaration, declared using an
2633 unqualified name, the namespace associated with DECL may
2634 have been set incorrectly. For example, in:
2636 template <typename T> void f(T);
2637 namespace N {
2638 struct S { friend void f<int>(int); }
2641 we will have set the DECL_CONTEXT for the friend
2642 declaration to N, rather than to the global namespace. */
2643 if (DECL_NAMESPACE_SCOPE_P (decl))
2644 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2646 if (is_friend && !have_def)
2647 /* This is not really a declaration of a specialization.
2648 It's just the name of an instantiation. But, it's not
2649 a request for an instantiation, either. */
2650 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2651 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2652 /* This is indeed a specialization. In case of constructors
2653 and destructors, we need in-charge and not-in-charge
2654 versions in V3 ABI. */
2655 clone_function_decl (decl, /*update_method_vec_p=*/0);
2657 /* Register this specialization so that we can find it
2658 again. */
2659 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2663 return decl;
2666 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2667 parameters. These are represented in the same format used for
2668 DECL_TEMPLATE_PARMS. */
2671 comp_template_parms (const_tree parms1, const_tree parms2)
2673 const_tree p1;
2674 const_tree p2;
2676 if (parms1 == parms2)
2677 return 1;
2679 for (p1 = parms1, p2 = parms2;
2680 p1 != NULL_TREE && p2 != NULL_TREE;
2681 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2683 tree t1 = TREE_VALUE (p1);
2684 tree t2 = TREE_VALUE (p2);
2685 int i;
2687 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2688 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2690 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2691 return 0;
2693 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2695 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2696 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2698 /* If either of the template parameters are invalid, assume
2699 they match for the sake of error recovery. */
2700 if (parm1 == error_mark_node || parm2 == error_mark_node)
2701 return 1;
2703 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2704 return 0;
2706 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2707 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2708 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2709 continue;
2710 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2711 return 0;
2715 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2716 /* One set of parameters has more parameters lists than the
2717 other. */
2718 return 0;
2720 return 1;
2723 /* Determine whether PARM is a parameter pack. */
2725 bool
2726 template_parameter_pack_p (const_tree parm)
2728 /* Determine if we have a non-type template parameter pack. */
2729 if (TREE_CODE (parm) == PARM_DECL)
2730 return (DECL_TEMPLATE_PARM_P (parm)
2731 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2733 /* If this is a list of template parameters, we could get a
2734 TYPE_DECL or a TEMPLATE_DECL. */
2735 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2736 parm = TREE_TYPE (parm);
2738 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2739 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2740 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2743 /* Determine if T is a function parameter pack. */
2745 bool
2746 function_parameter_pack_p (const_tree t)
2748 if (t && TREE_CODE (t) == PARM_DECL)
2749 return FUNCTION_PARAMETER_PACK_P (t);
2750 return false;
2753 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2754 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2756 tree
2757 get_function_template_decl (const_tree primary_func_tmpl_inst)
2759 if (! primary_func_tmpl_inst
2760 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2761 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2762 return NULL;
2764 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2767 /* Return true iff the function parameter PARAM_DECL was expanded
2768 from the function parameter pack PACK. */
2770 bool
2771 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2773 if (DECL_ARTIFICIAL (param_decl)
2774 || !function_parameter_pack_p (pack))
2775 return false;
2777 /* The parameter pack and its pack arguments have the same
2778 DECL_PARM_INDEX. */
2779 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2782 /* Determine whether ARGS describes a variadic template args list,
2783 i.e., one that is terminated by a template argument pack. */
2785 static bool
2786 template_args_variadic_p (tree args)
2788 int nargs;
2789 tree last_parm;
2791 if (args == NULL_TREE)
2792 return false;
2794 args = INNERMOST_TEMPLATE_ARGS (args);
2795 nargs = TREE_VEC_LENGTH (args);
2797 if (nargs == 0)
2798 return false;
2800 last_parm = TREE_VEC_ELT (args, nargs - 1);
2802 return ARGUMENT_PACK_P (last_parm);
2805 /* Generate a new name for the parameter pack name NAME (an
2806 IDENTIFIER_NODE) that incorporates its */
2808 static tree
2809 make_ith_pack_parameter_name (tree name, int i)
2811 /* Munge the name to include the parameter index. */
2812 #define NUMBUF_LEN 128
2813 char numbuf[NUMBUF_LEN];
2814 char* newname;
2815 int newname_len;
2817 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2818 newname_len = IDENTIFIER_LENGTH (name)
2819 + strlen (numbuf) + 2;
2820 newname = (char*)alloca (newname_len);
2821 snprintf (newname, newname_len,
2822 "%s#%i", IDENTIFIER_POINTER (name), i);
2823 return get_identifier (newname);
2826 /* Return true if T is a primary function
2827 or class template instantiation. */
2829 bool
2830 primary_template_instantiation_p (const_tree t)
2832 if (!t)
2833 return false;
2835 if (TREE_CODE (t) == FUNCTION_DECL)
2836 return DECL_LANG_SPECIFIC (t)
2837 && DECL_TEMPLATE_INSTANTIATION (t)
2838 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2839 else if (CLASS_TYPE_P (t))
2840 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2841 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2842 return false;
2845 /* Return true if PARM is a template template parameter. */
2847 bool
2848 template_template_parameter_p (const_tree parm)
2850 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2853 /* Return the template parameters of T if T is a
2854 primary template instantiation, NULL otherwise. */
2856 tree
2857 get_primary_template_innermost_parameters (const_tree t)
2859 tree parms = NULL, template_info = NULL;
2861 if ((template_info = get_template_info (t))
2862 && primary_template_instantiation_p (t))
2863 parms = INNERMOST_TEMPLATE_PARMS
2864 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2866 return parms;
2869 /* Return the template parameters of the LEVELth level from the full list
2870 of template parameters PARMS. */
2872 tree
2873 get_template_parms_at_level (tree parms, int level)
2875 tree p;
2876 if (!parms
2877 || TREE_CODE (parms) != TREE_LIST
2878 || level > TMPL_PARMS_DEPTH (parms))
2879 return NULL_TREE;
2881 for (p = parms; p; p = TREE_CHAIN (p))
2882 if (TMPL_PARMS_DEPTH (p) == level)
2883 return p;
2885 return NULL_TREE;
2888 /* Returns the template arguments of T if T is a template instantiation,
2889 NULL otherwise. */
2891 tree
2892 get_template_innermost_arguments (const_tree t)
2894 tree args = NULL, template_info = NULL;
2896 if ((template_info = get_template_info (t))
2897 && TI_ARGS (template_info))
2898 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2900 return args;
2903 /* Return the argument pack elements of T if T is a template argument pack,
2904 NULL otherwise. */
2906 tree
2907 get_template_argument_pack_elems (const_tree t)
2909 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2910 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2911 return NULL;
2913 return ARGUMENT_PACK_ARGS (t);
2916 /* Structure used to track the progress of find_parameter_packs_r. */
2917 struct find_parameter_pack_data
2919 /* TREE_LIST that will contain all of the parameter packs found by
2920 the traversal. */
2921 tree* parameter_packs;
2923 /* Set of AST nodes that have been visited by the traversal. */
2924 struct pointer_set_t *visited;
2927 /* Identifies all of the argument packs that occur in a template
2928 argument and appends them to the TREE_LIST inside DATA, which is a
2929 find_parameter_pack_data structure. This is a subroutine of
2930 make_pack_expansion and uses_parameter_packs. */
2931 static tree
2932 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2934 tree t = *tp;
2935 struct find_parameter_pack_data* ppd =
2936 (struct find_parameter_pack_data*)data;
2937 bool parameter_pack_p = false;
2939 /* Identify whether this is a parameter pack or not. */
2940 switch (TREE_CODE (t))
2942 case TEMPLATE_PARM_INDEX:
2943 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2944 parameter_pack_p = true;
2945 break;
2947 case TEMPLATE_TYPE_PARM:
2948 case TEMPLATE_TEMPLATE_PARM:
2949 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2950 parameter_pack_p = true;
2951 break;
2953 case PARM_DECL:
2954 if (FUNCTION_PARAMETER_PACK_P (t))
2956 /* We don't want to walk into the type of a PARM_DECL,
2957 because we don't want to see the type parameter pack. */
2958 *walk_subtrees = 0;
2959 parameter_pack_p = true;
2961 break;
2963 default:
2964 /* Not a parameter pack. */
2965 break;
2968 if (parameter_pack_p)
2970 /* Add this parameter pack to the list. */
2971 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2974 if (TYPE_P (t))
2975 cp_walk_tree (&TYPE_CONTEXT (t),
2976 &find_parameter_packs_r, ppd, ppd->visited);
2978 /* This switch statement will return immediately if we don't find a
2979 parameter pack. */
2980 switch (TREE_CODE (t))
2982 case TEMPLATE_PARM_INDEX:
2983 return NULL_TREE;
2985 case BOUND_TEMPLATE_TEMPLATE_PARM:
2986 /* Check the template itself. */
2987 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2988 &find_parameter_packs_r, ppd, ppd->visited);
2989 /* Check the template arguments. */
2990 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2991 ppd->visited);
2992 *walk_subtrees = 0;
2993 return NULL_TREE;
2995 case TEMPLATE_TYPE_PARM:
2996 case TEMPLATE_TEMPLATE_PARM:
2997 return NULL_TREE;
2999 case PARM_DECL:
3000 return NULL_TREE;
3002 case RECORD_TYPE:
3003 if (TYPE_PTRMEMFUNC_P (t))
3004 return NULL_TREE;
3005 /* Fall through. */
3007 case UNION_TYPE:
3008 case ENUMERAL_TYPE:
3009 if (TYPE_TEMPLATE_INFO (t))
3010 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3011 &find_parameter_packs_r, ppd, ppd->visited);
3013 *walk_subtrees = 0;
3014 return NULL_TREE;
3016 case TEMPLATE_DECL:
3017 cp_walk_tree (&TREE_TYPE (t),
3018 &find_parameter_packs_r, ppd, ppd->visited);
3019 return NULL_TREE;
3021 case TYPENAME_TYPE:
3022 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3023 ppd, ppd->visited);
3024 *walk_subtrees = 0;
3025 return NULL_TREE;
3027 case TYPE_PACK_EXPANSION:
3028 case EXPR_PACK_EXPANSION:
3029 *walk_subtrees = 0;
3030 return NULL_TREE;
3032 case INTEGER_TYPE:
3033 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3034 ppd, ppd->visited);
3035 *walk_subtrees = 0;
3036 return NULL_TREE;
3038 case IDENTIFIER_NODE:
3039 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3040 ppd->visited);
3041 *walk_subtrees = 0;
3042 return NULL_TREE;
3044 default:
3045 return NULL_TREE;
3048 return NULL_TREE;
3051 /* Determines if the expression or type T uses any parameter packs. */
3052 bool
3053 uses_parameter_packs (tree t)
3055 tree parameter_packs = NULL_TREE;
3056 struct find_parameter_pack_data ppd;
3057 ppd.parameter_packs = &parameter_packs;
3058 ppd.visited = pointer_set_create ();
3059 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3060 pointer_set_destroy (ppd.visited);
3061 return parameter_packs != NULL_TREE;
3064 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3065 representation a base-class initializer into a parameter pack
3066 expansion. If all goes well, the resulting node will be an
3067 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3068 respectively. */
3069 tree
3070 make_pack_expansion (tree arg)
3072 tree result;
3073 tree parameter_packs = NULL_TREE;
3074 bool for_types = false;
3075 struct find_parameter_pack_data ppd;
3077 if (!arg || arg == error_mark_node)
3078 return arg;
3080 if (TREE_CODE (arg) == TREE_LIST)
3082 /* The only time we will see a TREE_LIST here is for a base
3083 class initializer. In this case, the TREE_PURPOSE will be a
3084 _TYPE node (representing the base class expansion we're
3085 initializing) and the TREE_VALUE will be a TREE_LIST
3086 containing the initialization arguments.
3088 The resulting expansion looks somewhat different from most
3089 expansions. Rather than returning just one _EXPANSION, we
3090 return a TREE_LIST whose TREE_PURPOSE is a
3091 TYPE_PACK_EXPANSION containing the bases that will be
3092 initialized. The TREE_VALUE will be identical to the
3093 original TREE_VALUE, which is a list of arguments that will
3094 be passed to each base. We do not introduce any new pack
3095 expansion nodes into the TREE_VALUE (although it is possible
3096 that some already exist), because the TREE_PURPOSE and
3097 TREE_VALUE all need to be expanded together with the same
3098 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3099 resulting TREE_PURPOSE will mention the parameter packs in
3100 both the bases and the arguments to the bases. */
3101 tree purpose;
3102 tree value;
3103 tree parameter_packs = NULL_TREE;
3105 /* Determine which parameter packs will be used by the base
3106 class expansion. */
3107 ppd.visited = pointer_set_create ();
3108 ppd.parameter_packs = &parameter_packs;
3109 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3110 &ppd, ppd.visited);
3112 if (parameter_packs == NULL_TREE)
3114 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3115 pointer_set_destroy (ppd.visited);
3116 return error_mark_node;
3119 if (TREE_VALUE (arg) != void_type_node)
3121 /* Collect the sets of parameter packs used in each of the
3122 initialization arguments. */
3123 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3125 /* Determine which parameter packs will be expanded in this
3126 argument. */
3127 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3128 &ppd, ppd.visited);
3132 pointer_set_destroy (ppd.visited);
3134 /* Create the pack expansion type for the base type. */
3135 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3136 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3137 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3139 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3140 they will rarely be compared to anything. */
3141 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3143 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3146 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3147 for_types = true;
3149 /* Build the PACK_EXPANSION_* node. */
3150 result = for_types
3151 ? cxx_make_type (TYPE_PACK_EXPANSION)
3152 : make_node (EXPR_PACK_EXPANSION);
3153 SET_PACK_EXPANSION_PATTERN (result, arg);
3154 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3156 /* Propagate type and const-expression information. */
3157 TREE_TYPE (result) = TREE_TYPE (arg);
3158 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3160 else
3161 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3162 they will rarely be compared to anything. */
3163 SET_TYPE_STRUCTURAL_EQUALITY (result);
3165 /* Determine which parameter packs will be expanded. */
3166 ppd.parameter_packs = &parameter_packs;
3167 ppd.visited = pointer_set_create ();
3168 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3169 pointer_set_destroy (ppd.visited);
3171 /* Make sure we found some parameter packs. */
3172 if (parameter_packs == NULL_TREE)
3174 if (TYPE_P (arg))
3175 error ("expansion pattern %<%T%> contains no argument packs", arg);
3176 else
3177 error ("expansion pattern %<%E%> contains no argument packs", arg);
3178 return error_mark_node;
3180 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3182 return result;
3185 /* Checks T for any "bare" parameter packs, which have not yet been
3186 expanded, and issues an error if any are found. This operation can
3187 only be done on full expressions or types (e.g., an expression
3188 statement, "if" condition, etc.), because we could have expressions like:
3190 foo(f(g(h(args)))...)
3192 where "args" is a parameter pack. check_for_bare_parameter_packs
3193 should not be called for the subexpressions args, h(args),
3194 g(h(args)), or f(g(h(args))), because we would produce erroneous
3195 error messages.
3197 Returns TRUE and emits an error if there were bare parameter packs,
3198 returns FALSE otherwise. */
3199 bool
3200 check_for_bare_parameter_packs (tree t)
3202 tree parameter_packs = NULL_TREE;
3203 struct find_parameter_pack_data ppd;
3205 if (!processing_template_decl || !t || t == error_mark_node)
3206 return false;
3208 if (TREE_CODE (t) == TYPE_DECL)
3209 t = TREE_TYPE (t);
3211 ppd.parameter_packs = &parameter_packs;
3212 ppd.visited = pointer_set_create ();
3213 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3214 pointer_set_destroy (ppd.visited);
3216 if (parameter_packs)
3218 error ("parameter packs not expanded with %<...%>:");
3219 while (parameter_packs)
3221 tree pack = TREE_VALUE (parameter_packs);
3222 tree name = NULL_TREE;
3224 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3225 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3226 name = TYPE_NAME (pack);
3227 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3228 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3229 else
3230 name = DECL_NAME (pack);
3232 if (name)
3233 inform (input_location, " %qD", name);
3234 else
3235 inform (input_location, " <anonymous>");
3237 parameter_packs = TREE_CHAIN (parameter_packs);
3240 return true;
3243 return false;
3246 /* Expand any parameter packs that occur in the template arguments in
3247 ARGS. */
3248 tree
3249 expand_template_argument_pack (tree args)
3251 tree result_args = NULL_TREE;
3252 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3253 int num_result_args = -1;
3254 int non_default_args_count = -1;
3256 /* First, determine if we need to expand anything, and the number of
3257 slots we'll need. */
3258 for (in_arg = 0; in_arg < nargs; ++in_arg)
3260 tree arg = TREE_VEC_ELT (args, in_arg);
3261 if (arg == NULL_TREE)
3262 return args;
3263 if (ARGUMENT_PACK_P (arg))
3265 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3266 if (num_result_args < 0)
3267 num_result_args = in_arg + num_packed;
3268 else
3269 num_result_args += num_packed;
3271 else
3273 if (num_result_args >= 0)
3274 num_result_args++;
3278 /* If no expansion is necessary, we're done. */
3279 if (num_result_args < 0)
3280 return args;
3282 /* Expand arguments. */
3283 result_args = make_tree_vec (num_result_args);
3284 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3285 non_default_args_count =
3286 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3287 for (in_arg = 0; in_arg < nargs; ++in_arg)
3289 tree arg = TREE_VEC_ELT (args, in_arg);
3290 if (ARGUMENT_PACK_P (arg))
3292 tree packed = ARGUMENT_PACK_ARGS (arg);
3293 int i, num_packed = TREE_VEC_LENGTH (packed);
3294 for (i = 0; i < num_packed; ++i, ++out_arg)
3295 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3296 if (non_default_args_count > 0)
3297 non_default_args_count += num_packed;
3299 else
3301 TREE_VEC_ELT (result_args, out_arg) = arg;
3302 ++out_arg;
3305 if (non_default_args_count >= 0)
3306 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3307 return result_args;
3310 /* Checks if DECL shadows a template parameter.
3312 [temp.local]: A template-parameter shall not be redeclared within its
3313 scope (including nested scopes).
3315 Emits an error and returns TRUE if the DECL shadows a parameter,
3316 returns FALSE otherwise. */
3318 bool
3319 check_template_shadow (tree decl)
3321 tree olddecl;
3323 /* If we're not in a template, we can't possibly shadow a template
3324 parameter. */
3325 if (!current_template_parms)
3326 return true;
3328 /* Figure out what we're shadowing. */
3329 if (TREE_CODE (decl) == OVERLOAD)
3330 decl = OVL_CURRENT (decl);
3331 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3333 /* If there's no previous binding for this name, we're not shadowing
3334 anything, let alone a template parameter. */
3335 if (!olddecl)
3336 return true;
3338 /* If we're not shadowing a template parameter, we're done. Note
3339 that OLDDECL might be an OVERLOAD (or perhaps even an
3340 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3341 node. */
3342 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3343 return true;
3345 /* We check for decl != olddecl to avoid bogus errors for using a
3346 name inside a class. We check TPFI to avoid duplicate errors for
3347 inline member templates. */
3348 if (decl == olddecl
3349 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3350 return true;
3352 error ("declaration of %q+#D", decl);
3353 error (" shadows template parm %q+#D", olddecl);
3354 return false;
3357 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3358 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3359 template parameters. */
3361 static tree
3362 build_template_parm_index (int index,
3363 int level,
3364 int orig_level,
3365 int num_siblings,
3366 tree decl,
3367 tree type)
3369 tree t = make_node (TEMPLATE_PARM_INDEX);
3370 TEMPLATE_PARM_IDX (t) = index;
3371 TEMPLATE_PARM_LEVEL (t) = level;
3372 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3373 TEMPLATE_PARM_NUM_SIBLINGS (t) = num_siblings;
3374 TEMPLATE_PARM_DECL (t) = decl;
3375 TREE_TYPE (t) = type;
3376 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3377 TREE_READONLY (t) = TREE_READONLY (decl);
3379 return t;
3382 /* Find the canonical type parameter for the given template type
3383 parameter. Returns the canonical type parameter, which may be TYPE
3384 if no such parameter existed. */
3386 static tree
3387 canonical_type_parameter (tree type)
3389 tree list;
3390 int idx = TEMPLATE_TYPE_IDX (type);
3391 if (!canonical_template_parms)
3392 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3394 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3395 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3397 list = VEC_index (tree, canonical_template_parms, idx);
3398 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3399 list = TREE_CHAIN (list);
3401 if (list)
3402 return TREE_VALUE (list);
3403 else
3405 VEC_replace(tree, canonical_template_parms, idx,
3406 tree_cons (NULL_TREE, type,
3407 VEC_index (tree, canonical_template_parms, idx)));
3408 return type;
3412 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3413 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3414 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3415 new one is created. */
3417 static tree
3418 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3419 tsubst_flags_t complain)
3421 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3422 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3423 != TEMPLATE_PARM_LEVEL (index) - levels)
3424 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3426 tree orig_decl = TEMPLATE_PARM_DECL (index);
3427 tree decl, t;
3429 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3430 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3431 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3432 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3433 DECL_ARTIFICIAL (decl) = 1;
3434 SET_DECL_TEMPLATE_PARM_P (decl);
3436 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3437 TEMPLATE_PARM_LEVEL (index) - levels,
3438 TEMPLATE_PARM_ORIG_LEVEL (index),
3439 TEMPLATE_PARM_NUM_SIBLINGS (index),
3440 decl, type);
3441 TEMPLATE_PARM_DESCENDANTS (index) = t;
3442 TEMPLATE_PARM_PARAMETER_PACK (t)
3443 = TEMPLATE_PARM_PARAMETER_PACK (index);
3445 /* Template template parameters need this. */
3446 if (TREE_CODE (decl) == TEMPLATE_DECL)
3447 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3448 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3449 args, complain);
3452 return TEMPLATE_PARM_DESCENDANTS (index);
3455 /* Process information from new template parameter PARM and append it
3456 to the LIST being built. This new parameter is a non-type
3457 parameter iff IS_NON_TYPE is true. This new parameter is a
3458 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3459 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3460 parameter list PARM belongs to. This is used used to create a
3461 proper canonical type for the type of PARM that is to be created,
3462 iff PARM is a type. If the size is not known, this parameter shall
3463 be set to 0. */
3465 tree
3466 process_template_parm (tree list, location_t parm_loc, tree parm,
3467 bool is_non_type, bool is_parameter_pack,
3468 unsigned num_template_parms)
3470 tree decl = 0;
3471 tree defval;
3472 tree err_parm_list;
3473 int idx = 0;
3475 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3476 defval = TREE_PURPOSE (parm);
3478 if (list)
3480 tree p = tree_last (list);
3482 if (p && TREE_VALUE (p) != error_mark_node)
3484 p = TREE_VALUE (p);
3485 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3486 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3487 else
3488 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3491 ++idx;
3493 else
3494 idx = 0;
3496 if (is_non_type)
3498 parm = TREE_VALUE (parm);
3500 SET_DECL_TEMPLATE_PARM_P (parm);
3502 if (TREE_TYPE (parm) == error_mark_node)
3504 err_parm_list = build_tree_list (defval, parm);
3505 TREE_VALUE (err_parm_list) = error_mark_node;
3506 return chainon (list, err_parm_list);
3508 else
3510 /* [temp.param]
3512 The top-level cv-qualifiers on the template-parameter are
3513 ignored when determining its type. */
3514 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3515 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3517 err_parm_list = build_tree_list (defval, parm);
3518 TREE_VALUE (err_parm_list) = error_mark_node;
3519 return chainon (list, err_parm_list);
3522 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3524 /* This template parameter is not a parameter pack, but it
3525 should be. Complain about "bare" parameter packs. */
3526 check_for_bare_parameter_packs (TREE_TYPE (parm));
3528 /* Recover by calling this a parameter pack. */
3529 is_parameter_pack = true;
3533 /* A template parameter is not modifiable. */
3534 TREE_CONSTANT (parm) = 1;
3535 TREE_READONLY (parm) = 1;
3536 decl = build_decl (parm_loc,
3537 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3538 TREE_CONSTANT (decl) = 1;
3539 TREE_READONLY (decl) = 1;
3540 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3541 = build_template_parm_index (idx, processing_template_decl,
3542 processing_template_decl,
3543 num_template_parms,
3544 decl, TREE_TYPE (parm));
3546 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3547 = is_parameter_pack;
3549 else
3551 tree t;
3552 parm = TREE_VALUE (TREE_VALUE (parm));
3554 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3556 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3557 /* This is for distinguishing between real templates and template
3558 template parameters */
3559 TREE_TYPE (parm) = t;
3560 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3561 decl = parm;
3563 else
3565 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3566 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3567 decl = build_decl (parm_loc,
3568 TYPE_DECL, parm, t);
3571 TYPE_NAME (t) = decl;
3572 TYPE_STUB_DECL (t) = decl;
3573 parm = decl;
3574 TEMPLATE_TYPE_PARM_INDEX (t)
3575 = build_template_parm_index (idx, processing_template_decl,
3576 processing_template_decl,
3577 num_template_parms,
3578 decl, TREE_TYPE (parm));
3579 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3580 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3582 DECL_ARTIFICIAL (decl) = 1;
3583 SET_DECL_TEMPLATE_PARM_P (decl);
3584 pushdecl (decl);
3585 parm = build_tree_list (defval, parm);
3586 return chainon (list, parm);
3589 /* The end of a template parameter list has been reached. Process the
3590 tree list into a parameter vector, converting each parameter into a more
3591 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3592 as PARM_DECLs. */
3594 tree
3595 end_template_parm_list (tree parms)
3597 int nparms;
3598 tree parm, next;
3599 tree saved_parmlist = make_tree_vec (list_length (parms));
3601 current_template_parms
3602 = tree_cons (size_int (processing_template_decl),
3603 saved_parmlist, current_template_parms);
3605 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3607 next = TREE_CHAIN (parm);
3608 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3609 TREE_CHAIN (parm) = NULL_TREE;
3612 --processing_template_parmlist;
3614 return saved_parmlist;
3617 /* Create a new type almost identical to TYPE but which has the
3618 following differences:
3620 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3621 template sibling parameters of T.
3623 2/ T has a new canonical type that matches the new number
3624 of sibling parms.
3626 3/ From now on, T is going to be what lookups referring to the
3627 name of TYPE will return. No lookup should return TYPE anymore.
3629 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3631 This is a subroutine of fixup_template_parms. */
3633 static tree
3634 fixup_template_type_parm_type (tree type, int num_parms)
3636 tree orig_idx = TEMPLATE_TYPE_PARM_INDEX (type), idx;
3637 tree t;
3638 /* This is the decl which name is inserted into the symbol table for
3639 the template parm type. So whenever we lookup the type name, this
3640 is the DECL we get. */
3641 tree decl;
3643 /* Do not fix up the type twice. */
3644 if (orig_idx && TEMPLATE_PARM_NUM_SIBLINGS (orig_idx) != 0)
3645 return type;
3647 t = copy_type (type);
3648 decl = TYPE_NAME (t);
3650 TYPE_MAIN_VARIANT (t) = t;
3651 TYPE_NEXT_VARIANT (t)= NULL_TREE;
3652 TYPE_POINTER_TO (t) = 0;
3653 TYPE_REFERENCE_TO (t) = 0;
3655 idx = build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx),
3656 TEMPLATE_PARM_LEVEL (orig_idx),
3657 TEMPLATE_PARM_ORIG_LEVEL (orig_idx),
3658 num_parms,
3659 decl, t);
3660 TEMPLATE_PARM_DESCENDANTS (idx) = TEMPLATE_PARM_DESCENDANTS (orig_idx);
3661 TEMPLATE_PARM_PARAMETER_PACK (idx) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx);
3662 TEMPLATE_TYPE_PARM_INDEX (t) = idx;
3664 TYPE_STUB_DECL (t) = decl;
3665 TEMPLATE_TYPE_DECL (t) = decl;
3666 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
3667 TREE_TYPE (DECL_TEMPLATE_RESULT (decl)) = t;
3669 /* Update the type associated to the type name stored in the symbol
3670 table. Now, whenever the type name is looked up, the resulting
3671 type is properly fixed up. */
3672 TREE_TYPE (decl) = t;
3674 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3676 return t;
3679 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3680 identical to I, but that is fixed up as to:
3682 1/ carry the number of sibling parms (NUM_PARMS) of the template
3683 parm represented by I.
3685 2/ replace all references to template parm types declared before I
3686 (in the same template parm list as I) by references to template
3687 parm types contained in ARGS. ARGS should contain the list of
3688 template parms that have been fixed up so far, in a form suitable
3689 to be passed to tsubst.
3691 This is a subroutine of fixup_template_parms. */
3693 static tree
3694 fixup_template_parm_index (tree i, tree args, int num_parms)
3696 tree index, decl, type;
3698 if (i == NULL_TREE
3699 || TREE_CODE (i) != TEMPLATE_PARM_INDEX
3700 /* Do not fix up the index twice. */
3701 || (TEMPLATE_PARM_NUM_SIBLINGS (i) != 0))
3702 return i;
3704 decl = TEMPLATE_PARM_DECL (i);
3705 type = TREE_TYPE (decl);
3707 index = build_template_parm_index (TEMPLATE_PARM_IDX (i),
3708 TEMPLATE_PARM_LEVEL (i),
3709 TEMPLATE_PARM_ORIG_LEVEL (i),
3710 num_parms,
3711 decl, type);
3713 TEMPLATE_PARM_DESCENDANTS (index) = TEMPLATE_PARM_DESCENDANTS (i);
3714 TEMPLATE_PARM_PARAMETER_PACK (index) = TEMPLATE_PARM_PARAMETER_PACK (i);
3716 type = tsubst (type, args, tf_none, NULL_TREE);
3718 TREE_TYPE (decl) = type;
3719 TREE_TYPE (index) = type;
3721 return index;
3725 This is a subroutine of fixup_template_parms.
3727 It computes the canonical type of the type of the template
3728 parameter PARM_DESC and update all references to that type so that
3729 they use the newly computed canonical type. No access check is
3730 performed during the fixup. PARM_DESC is a TREE_LIST which
3731 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3732 default argument of the template parm if any. IDX is the index of
3733 the template parameter, starting at 0. NUM_PARMS is the number of
3734 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3735 TREE_VEC containing the full set of template parameters in a form
3736 suitable to be passed to substs functions as their ARGS
3737 argument. This is what current_template_args returns for a given
3738 template. The innermost vector of args in ARGLIST is the set of
3739 template parms that have been fixed up so far. This function adds
3740 the fixed up parameter into that vector. */
3742 static void
3743 fixup_template_parm (tree parm_desc,
3744 int idx,
3745 int num_parms,
3746 tree arglist)
3748 tree parm = TREE_VALUE (parm_desc);
3749 tree fixedup_args = INNERMOST_TEMPLATE_ARGS (arglist);
3751 push_deferring_access_checks (dk_no_check);
3753 if (TREE_CODE (parm) == TYPE_DECL)
3755 /* PARM is a template type parameter. Fix up its type, add
3756 the fixed-up template parm to the vector of fixed-up
3757 template parms so far, and substitute the fixed-up
3758 template parms into the default argument of this
3759 parameter. */
3760 tree t =
3761 fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3762 TREE_TYPE (parm) = t;
3764 TREE_VEC_ELT (fixedup_args, idx) = template_parm_to_arg (parm_desc);
3766 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3768 /* PARM is a template template parameter. This is going to
3769 be interesting. */
3770 tree tparms, targs, innermost_args, t;
3771 int j;
3773 /* First, fix up the parms of the template template parm
3774 because the parms are involved in defining the new canonical
3775 type of the template template parm. */
3777 /* So we need to substitute the template parm types that have
3778 been fixed up so far into the template parms of this template
3779 template parm. E.g, consider this:
3781 template<class T, template<T u> class TT> class S;
3783 In this case we want to substitute T into the
3784 template parameters of TT.
3786 So let's walk the template parms of PARM here, and
3787 tsubst ARGLIST into into each of the template
3788 parms. */
3790 /* For this substitution we need to build the full set of
3791 template parameters and use that as arguments for the
3792 tsubsting function. */
3793 tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
3795 /* This will contain the innermost parms of PARM into which
3796 we have substituted so far. */
3797 innermost_args = make_tree_vec (TREE_VEC_LENGTH (tparms));
3798 targs = add_to_template_args (arglist, innermost_args);
3799 for (j = 0; j < TREE_VEC_LENGTH (tparms); ++j)
3801 tree parameter;
3803 parameter = TREE_VEC_ELT (tparms, j);
3805 /* INNERMOST_ARGS needs to have at least the same number
3806 of elements as the index PARAMETER, ortherwise
3807 tsubsting into PARAMETER will result in partially
3808 instantiating it, reducing its tempate parm
3809 level. Let's tactically fill INNERMOST_ARGS for that
3810 purpose. */
3811 TREE_VEC_ELT (innermost_args, j) =
3812 template_parm_to_arg (parameter);
3814 fixup_template_parm (parameter, j,
3815 TREE_VEC_LENGTH (tparms),
3816 targs);
3819 /* Now fix up the type of the template template parm. */
3821 t = fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3822 TREE_TYPE (parm) = t;
3824 TREE_VEC_ELT (fixedup_args, idx) =
3825 template_parm_to_arg (parm_desc);
3827 else if (TREE_CODE (parm) == PARM_DECL)
3829 /* PARM is a non-type template parameter. We need to:
3831 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3832 proper number of sibling parameters.
3834 * Make lookups of the template parameter return a reference
3835 to the fixed-up index. No lookup should return references
3836 to the former index anymore.
3838 * Substitute the template parms that got fixed up so far
3840 * into the type of PARM. */
3842 tree index = DECL_INITIAL (parm);
3844 /* PUSHED_DECL is the decl added to the symbol table with
3845 the name of the parameter. E,g:
3847 template<class T, T u> //#0
3848 auto my_function(T t) -> decltype(u); //#1
3850 Here, when looking up u at //#1, we get the decl of u
3851 resulting from the declaration in #0. This is what
3852 PUSHED_DECL is. We need to replace the reference to the
3853 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3854 fixed-up TEMPLATE_PARM_INDEX. */
3855 tree pushed_decl = TEMPLATE_PARM_DECL (index);
3857 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3858 fixup the type of PUSHED_DECL as well and luckily
3859 fixup_template_parm_index does it for us too. */
3860 tree fixed_up_index =
3861 fixup_template_parm_index (index, arglist, num_parms);
3863 DECL_INITIAL (pushed_decl) = DECL_INITIAL (parm) = fixed_up_index;
3865 /* Add this fixed up PARM to the template parms we've fixed
3866 up so far and use that to substitute the fixed-up
3867 template parms into the type of PARM. */
3868 TREE_VEC_ELT (fixedup_args, idx) =
3869 template_parm_to_arg (parm_desc);
3870 TREE_TYPE (parm) = tsubst (TREE_TYPE (parm), arglist,
3871 tf_none, NULL_TREE);
3874 TREE_PURPOSE (parm_desc) =
3875 tsubst_template_arg (TREE_PURPOSE (parm_desc),
3876 arglist, tf_none, parm);
3878 pop_deferring_access_checks ();
3881 /* Walk the current template parms and properly compute the canonical
3882 types of the dependent types created during
3883 cp_parser_template_parameter_list. */
3885 void
3886 fixup_template_parms (void)
3888 tree arglist;
3889 tree parameter_vec;
3890 tree fixedup_args;
3891 int i, num_parms;
3893 parameter_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3894 if (parameter_vec == NULL_TREE)
3895 return;
3897 num_parms = TREE_VEC_LENGTH (parameter_vec);
3899 /* This vector contains the current innermost template parms that
3900 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3901 to be passed to tsubst* functions as their ARGS argument. */
3902 fixedup_args = make_tree_vec (num_parms);
3904 /* This vector contains the full set of template parms in a form
3905 suitable to be passed to substs functions as their ARGS
3906 argument. */
3907 arglist = current_template_args ();
3908 arglist = add_outermost_template_args (arglist, fixedup_args);
3910 /* Let's do the proper fixup now. */
3911 for (i = 0; i < num_parms; ++i)
3912 fixup_template_parm (TREE_VEC_ELT (parameter_vec, i),
3913 i, num_parms, arglist);
3916 /* end_template_decl is called after a template declaration is seen. */
3918 void
3919 end_template_decl (void)
3921 reset_specialization ();
3923 if (! processing_template_decl)
3924 return;
3926 /* This matches the pushlevel in begin_template_parm_list. */
3927 finish_scope ();
3929 --processing_template_decl;
3930 current_template_parms = TREE_CHAIN (current_template_parms);
3933 /* Takes a TREE_LIST representing a template parameter and convert it
3934 into an argument suitable to be passed to the type substitution
3935 functions. Note that If the TREE_LIST contains an error_mark
3936 node, the returned argument is error_mark_node. */
3938 static tree
3939 template_parm_to_arg (tree t)
3942 if (t == NULL_TREE
3943 || TREE_CODE (t) != TREE_LIST)
3944 return t;
3946 if (error_operand_p (TREE_VALUE (t)))
3947 return error_mark_node;
3949 t = TREE_VALUE (t);
3951 if (TREE_CODE (t) == TYPE_DECL
3952 || TREE_CODE (t) == TEMPLATE_DECL)
3954 t = TREE_TYPE (t);
3956 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3958 /* Turn this argument into a TYPE_ARGUMENT_PACK
3959 with a single element, which expands T. */
3960 tree vec = make_tree_vec (1);
3961 #ifdef ENABLE_CHECKING
3962 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3963 (vec, TREE_VEC_LENGTH (vec));
3964 #endif
3965 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3967 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3968 SET_ARGUMENT_PACK_ARGS (t, vec);
3971 else
3973 t = DECL_INITIAL (t);
3975 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3977 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3978 with a single element, which expands T. */
3979 tree vec = make_tree_vec (1);
3980 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3981 #ifdef ENABLE_CHECKING
3982 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3983 (vec, TREE_VEC_LENGTH (vec));
3984 #endif
3985 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3987 t = make_node (NONTYPE_ARGUMENT_PACK);
3988 SET_ARGUMENT_PACK_ARGS (t, vec);
3989 TREE_TYPE (t) = type;
3992 return t;
3995 /* Within the declaration of a template, return all levels of template
3996 parameters that apply. The template parameters are represented as
3997 a TREE_VEC, in the form documented in cp-tree.h for template
3998 arguments. */
4000 static tree
4001 current_template_args (void)
4003 tree header;
4004 tree args = NULL_TREE;
4005 int length = TMPL_PARMS_DEPTH (current_template_parms);
4006 int l = length;
4008 /* If there is only one level of template parameters, we do not
4009 create a TREE_VEC of TREE_VECs. Instead, we return a single
4010 TREE_VEC containing the arguments. */
4011 if (length > 1)
4012 args = make_tree_vec (length);
4014 for (header = current_template_parms; header; header = TREE_CHAIN (header))
4016 tree a = copy_node (TREE_VALUE (header));
4017 int i;
4019 TREE_TYPE (a) = NULL_TREE;
4020 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4021 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4023 #ifdef ENABLE_CHECKING
4024 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4025 #endif
4027 if (length > 1)
4028 TREE_VEC_ELT (args, --l) = a;
4029 else
4030 args = a;
4033 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
4034 /* This can happen for template parms of a template template
4035 parameter, e.g:
4037 template<template<class T, class U> class TT> struct S;
4039 Consider the level of the parms of TT; T and U both have
4040 level 2; TT has no template parm of level 1. So in this case
4041 the first element of full_template_args is NULL_TREE. If we
4042 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4043 of 2. This will make tsubst wrongly consider that T and U
4044 have level 1. Instead, let's create a dummy vector as the
4045 first element of full_template_args so that TMPL_ARG_DEPTH
4046 returns the correct depth for args. */
4047 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4048 return args;
4051 /* Update the declared TYPE by doing any lookups which were thought to be
4052 dependent, but are not now that we know the SCOPE of the declarator. */
4054 tree
4055 maybe_update_decl_type (tree orig_type, tree scope)
4057 tree type = orig_type;
4059 if (type == NULL_TREE)
4060 return type;
4062 if (TREE_CODE (orig_type) == TYPE_DECL)
4063 type = TREE_TYPE (type);
4065 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4066 && dependent_type_p (type)
4067 /* Don't bother building up the args in this case. */
4068 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4070 /* tsubst in the args corresponding to the template parameters,
4071 including auto if present. Most things will be unchanged, but
4072 make_typename_type and tsubst_qualified_id will resolve
4073 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4074 tree args = current_template_args ();
4075 tree auto_node = type_uses_auto (type);
4076 tree pushed;
4077 if (auto_node)
4079 tree auto_vec = make_tree_vec (1);
4080 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4081 args = add_to_template_args (args, auto_vec);
4083 pushed = push_scope (scope);
4084 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4085 if (pushed)
4086 pop_scope (scope);
4089 if (type == error_mark_node)
4090 return orig_type;
4092 if (TREE_CODE (orig_type) == TYPE_DECL)
4094 if (same_type_p (type, TREE_TYPE (orig_type)))
4095 type = orig_type;
4096 else
4097 type = TYPE_NAME (type);
4099 return type;
4102 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4103 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4104 a member template. Used by push_template_decl below. */
4106 static tree
4107 build_template_decl (tree decl, tree parms, bool member_template_p)
4109 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4110 DECL_TEMPLATE_PARMS (tmpl) = parms;
4111 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4112 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4114 return tmpl;
4117 struct template_parm_data
4119 /* The level of the template parameters we are currently
4120 processing. */
4121 int level;
4123 /* The index of the specialization argument we are currently
4124 processing. */
4125 int current_arg;
4127 /* An array whose size is the number of template parameters. The
4128 elements are nonzero if the parameter has been used in any one
4129 of the arguments processed so far. */
4130 int* parms;
4132 /* An array whose size is the number of template arguments. The
4133 elements are nonzero if the argument makes use of template
4134 parameters of this level. */
4135 int* arg_uses_template_parms;
4138 /* Subroutine of push_template_decl used to see if each template
4139 parameter in a partial specialization is used in the explicit
4140 argument list. If T is of the LEVEL given in DATA (which is
4141 treated as a template_parm_data*), then DATA->PARMS is marked
4142 appropriately. */
4144 static int
4145 mark_template_parm (tree t, void* data)
4147 int level;
4148 int idx;
4149 struct template_parm_data* tpd = (struct template_parm_data*) data;
4151 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4153 level = TEMPLATE_PARM_LEVEL (t);
4154 idx = TEMPLATE_PARM_IDX (t);
4156 else
4158 level = TEMPLATE_TYPE_LEVEL (t);
4159 idx = TEMPLATE_TYPE_IDX (t);
4162 if (level == tpd->level)
4164 tpd->parms[idx] = 1;
4165 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4168 /* Return zero so that for_each_template_parm will continue the
4169 traversal of the tree; we want to mark *every* template parm. */
4170 return 0;
4173 /* Process the partial specialization DECL. */
4175 static tree
4176 process_partial_specialization (tree decl)
4178 tree type = TREE_TYPE (decl);
4179 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4180 tree specargs = CLASSTYPE_TI_ARGS (type);
4181 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4182 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4183 tree inner_parms;
4184 tree inst;
4185 int nargs = TREE_VEC_LENGTH (inner_args);
4186 int ntparms;
4187 int i;
4188 bool did_error_intro = false;
4189 struct template_parm_data tpd;
4190 struct template_parm_data tpd2;
4192 gcc_assert (current_template_parms);
4194 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4195 ntparms = TREE_VEC_LENGTH (inner_parms);
4197 /* We check that each of the template parameters given in the
4198 partial specialization is used in the argument list to the
4199 specialization. For example:
4201 template <class T> struct S;
4202 template <class T> struct S<T*>;
4204 The second declaration is OK because `T*' uses the template
4205 parameter T, whereas
4207 template <class T> struct S<int>;
4209 is no good. Even trickier is:
4211 template <class T>
4212 struct S1
4214 template <class U>
4215 struct S2;
4216 template <class U>
4217 struct S2<T>;
4220 The S2<T> declaration is actually invalid; it is a
4221 full-specialization. Of course,
4223 template <class U>
4224 struct S2<T (*)(U)>;
4226 or some such would have been OK. */
4227 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4228 tpd.parms = XALLOCAVEC (int, ntparms);
4229 memset (tpd.parms, 0, sizeof (int) * ntparms);
4231 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4232 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4233 for (i = 0; i < nargs; ++i)
4235 tpd.current_arg = i;
4236 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4237 &mark_template_parm,
4238 &tpd,
4239 NULL,
4240 /*include_nondeduced_p=*/false);
4242 for (i = 0; i < ntparms; ++i)
4243 if (tpd.parms[i] == 0)
4245 /* One of the template parms was not used in the
4246 specialization. */
4247 if (!did_error_intro)
4249 error ("template parameters not used in partial specialization:");
4250 did_error_intro = true;
4253 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4256 if (did_error_intro)
4257 return error_mark_node;
4259 /* [temp.class.spec]
4261 The argument list of the specialization shall not be identical to
4262 the implicit argument list of the primary template. */
4263 if (comp_template_args
4264 (inner_args,
4265 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4266 (maintmpl)))))
4267 error ("partial specialization %qT does not specialize any template arguments", type);
4269 /* [temp.class.spec]
4271 A partially specialized non-type argument expression shall not
4272 involve template parameters of the partial specialization except
4273 when the argument expression is a simple identifier.
4275 The type of a template parameter corresponding to a specialized
4276 non-type argument shall not be dependent on a parameter of the
4277 specialization.
4279 Also, we verify that pack expansions only occur at the
4280 end of the argument list. */
4281 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4282 tpd2.parms = 0;
4283 for (i = 0; i < nargs; ++i)
4285 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4286 tree arg = TREE_VEC_ELT (inner_args, i);
4287 tree packed_args = NULL_TREE;
4288 int j, len = 1;
4290 if (ARGUMENT_PACK_P (arg))
4292 /* Extract the arguments from the argument pack. We'll be
4293 iterating over these in the following loop. */
4294 packed_args = ARGUMENT_PACK_ARGS (arg);
4295 len = TREE_VEC_LENGTH (packed_args);
4298 for (j = 0; j < len; j++)
4300 if (packed_args)
4301 /* Get the Jth argument in the parameter pack. */
4302 arg = TREE_VEC_ELT (packed_args, j);
4304 if (PACK_EXPANSION_P (arg))
4306 /* Pack expansions must come at the end of the
4307 argument list. */
4308 if ((packed_args && j < len - 1)
4309 || (!packed_args && i < nargs - 1))
4311 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4312 error ("parameter pack argument %qE must be at the "
4313 "end of the template argument list", arg);
4314 else
4315 error ("parameter pack argument %qT must be at the "
4316 "end of the template argument list", arg);
4320 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4321 /* We only care about the pattern. */
4322 arg = PACK_EXPANSION_PATTERN (arg);
4324 if (/* These first two lines are the `non-type' bit. */
4325 !TYPE_P (arg)
4326 && TREE_CODE (arg) != TEMPLATE_DECL
4327 /* This next line is the `argument expression is not just a
4328 simple identifier' condition and also the `specialized
4329 non-type argument' bit. */
4330 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4332 if ((!packed_args && tpd.arg_uses_template_parms[i])
4333 || (packed_args && uses_template_parms (arg)))
4334 error ("template argument %qE involves template parameter(s)",
4335 arg);
4336 else
4338 /* Look at the corresponding template parameter,
4339 marking which template parameters its type depends
4340 upon. */
4341 tree type = TREE_TYPE (parm);
4343 if (!tpd2.parms)
4345 /* We haven't yet initialized TPD2. Do so now. */
4346 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4347 /* The number of parameters here is the number in the
4348 main template, which, as checked in the assertion
4349 above, is NARGS. */
4350 tpd2.parms = XALLOCAVEC (int, nargs);
4351 tpd2.level =
4352 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4355 /* Mark the template parameters. But this time, we're
4356 looking for the template parameters of the main
4357 template, not in the specialization. */
4358 tpd2.current_arg = i;
4359 tpd2.arg_uses_template_parms[i] = 0;
4360 memset (tpd2.parms, 0, sizeof (int) * nargs);
4361 for_each_template_parm (type,
4362 &mark_template_parm,
4363 &tpd2,
4364 NULL,
4365 /*include_nondeduced_p=*/false);
4367 if (tpd2.arg_uses_template_parms [i])
4369 /* The type depended on some template parameters.
4370 If they are fully specialized in the
4371 specialization, that's OK. */
4372 int j;
4373 int count = 0;
4374 for (j = 0; j < nargs; ++j)
4375 if (tpd2.parms[j] != 0
4376 && tpd.arg_uses_template_parms [j])
4377 ++count;
4378 if (count != 0)
4379 error_n (input_location, count,
4380 "type %qT of template argument %qE depends "
4381 "on a template parameter",
4382 "type %qT of template argument %qE depends "
4383 "on template parameters",
4384 type,
4385 arg);
4392 /* We should only get here once. */
4393 gcc_assert (!COMPLETE_TYPE_P (type));
4395 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4396 = tree_cons (specargs, inner_parms,
4397 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4398 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4400 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4401 inst = TREE_CHAIN (inst))
4403 tree inst_type = TREE_VALUE (inst);
4404 if (COMPLETE_TYPE_P (inst_type)
4405 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4407 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4408 if (spec && TREE_TYPE (spec) == type)
4409 permerror (input_location,
4410 "partial specialization of %qT after instantiation "
4411 "of %qT", type, inst_type);
4415 return decl;
4418 /* Check that a template declaration's use of default arguments and
4419 parameter packs is not invalid. Here, PARMS are the template
4420 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4421 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4422 specialization.
4425 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4426 declaration (but not a definition); 1 indicates a declaration, 2
4427 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4428 emitted for extraneous default arguments.
4430 Returns TRUE if there were no errors found, FALSE otherwise. */
4432 bool
4433 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4434 int is_partial, int is_friend_decl)
4436 const char *msg;
4437 int last_level_to_check;
4438 tree parm_level;
4439 bool no_errors = true;
4441 /* [temp.param]
4443 A default template-argument shall not be specified in a
4444 function template declaration or a function template definition, nor
4445 in the template-parameter-list of the definition of a member of a
4446 class template. */
4448 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4449 /* You can't have a function template declaration in a local
4450 scope, nor you can you define a member of a class template in a
4451 local scope. */
4452 return true;
4454 if (current_class_type
4455 && !TYPE_BEING_DEFINED (current_class_type)
4456 && DECL_LANG_SPECIFIC (decl)
4457 && DECL_DECLARES_FUNCTION_P (decl)
4458 /* If this is either a friend defined in the scope of the class
4459 or a member function. */
4460 && (DECL_FUNCTION_MEMBER_P (decl)
4461 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4462 : DECL_FRIEND_CONTEXT (decl)
4463 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4464 : false)
4465 /* And, if it was a member function, it really was defined in
4466 the scope of the class. */
4467 && (!DECL_FUNCTION_MEMBER_P (decl)
4468 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4469 /* We already checked these parameters when the template was
4470 declared, so there's no need to do it again now. This function
4471 was defined in class scope, but we're processing it's body now
4472 that the class is complete. */
4473 return true;
4475 /* Core issue 226 (C++0x only): the following only applies to class
4476 templates. */
4477 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4479 /* [temp.param]
4481 If a template-parameter has a default template-argument, all
4482 subsequent template-parameters shall have a default
4483 template-argument supplied. */
4484 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4486 tree inner_parms = TREE_VALUE (parm_level);
4487 int ntparms = TREE_VEC_LENGTH (inner_parms);
4488 int seen_def_arg_p = 0;
4489 int i;
4491 for (i = 0; i < ntparms; ++i)
4493 tree parm = TREE_VEC_ELT (inner_parms, i);
4495 if (parm == error_mark_node)
4496 continue;
4498 if (TREE_PURPOSE (parm))
4499 seen_def_arg_p = 1;
4500 else if (seen_def_arg_p
4501 && !template_parameter_pack_p (TREE_VALUE (parm)))
4503 error ("no default argument for %qD", TREE_VALUE (parm));
4504 /* For better subsequent error-recovery, we indicate that
4505 there should have been a default argument. */
4506 TREE_PURPOSE (parm) = error_mark_node;
4507 no_errors = false;
4509 else if (is_primary
4510 && !is_partial
4511 && !is_friend_decl
4512 /* Don't complain about an enclosing partial
4513 specialization. */
4514 && parm_level == parms
4515 && TREE_CODE (decl) == TYPE_DECL
4516 && i < ntparms - 1
4517 && template_parameter_pack_p (TREE_VALUE (parm)))
4519 /* A primary class template can only have one
4520 parameter pack, at the end of the template
4521 parameter list. */
4523 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4524 error ("parameter pack %qE must be at the end of the"
4525 " template parameter list", TREE_VALUE (parm));
4526 else
4527 error ("parameter pack %qT must be at the end of the"
4528 " template parameter list",
4529 TREE_TYPE (TREE_VALUE (parm)));
4531 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4532 = error_mark_node;
4533 no_errors = false;
4539 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4540 || is_partial
4541 || !is_primary
4542 || is_friend_decl)
4543 /* For an ordinary class template, default template arguments are
4544 allowed at the innermost level, e.g.:
4545 template <class T = int>
4546 struct S {};
4547 but, in a partial specialization, they're not allowed even
4548 there, as we have in [temp.class.spec]:
4550 The template parameter list of a specialization shall not
4551 contain default template argument values.
4553 So, for a partial specialization, or for a function template
4554 (in C++98/C++03), we look at all of them. */
4556 else
4557 /* But, for a primary class template that is not a partial
4558 specialization we look at all template parameters except the
4559 innermost ones. */
4560 parms = TREE_CHAIN (parms);
4562 /* Figure out what error message to issue. */
4563 if (is_friend_decl == 2)
4564 msg = G_("default template arguments may not be used in function template "
4565 "friend re-declaration");
4566 else if (is_friend_decl)
4567 msg = G_("default template arguments may not be used in function template "
4568 "friend declarations");
4569 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4570 msg = G_("default template arguments may not be used in function templates "
4571 "without -std=c++0x or -std=gnu++0x");
4572 else if (is_partial)
4573 msg = G_("default template arguments may not be used in "
4574 "partial specializations");
4575 else
4576 msg = G_("default argument for template parameter for class enclosing %qD");
4578 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4579 /* If we're inside a class definition, there's no need to
4580 examine the parameters to the class itself. On the one
4581 hand, they will be checked when the class is defined, and,
4582 on the other, default arguments are valid in things like:
4583 template <class T = double>
4584 struct S { template <class U> void f(U); };
4585 Here the default argument for `S' has no bearing on the
4586 declaration of `f'. */
4587 last_level_to_check = template_class_depth (current_class_type) + 1;
4588 else
4589 /* Check everything. */
4590 last_level_to_check = 0;
4592 for (parm_level = parms;
4593 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4594 parm_level = TREE_CHAIN (parm_level))
4596 tree inner_parms = TREE_VALUE (parm_level);
4597 int i;
4598 int ntparms;
4600 ntparms = TREE_VEC_LENGTH (inner_parms);
4601 for (i = 0; i < ntparms; ++i)
4603 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4604 continue;
4606 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4608 if (msg)
4610 no_errors = false;
4611 if (is_friend_decl == 2)
4612 return no_errors;
4614 error (msg, decl);
4615 msg = 0;
4618 /* Clear out the default argument so that we are not
4619 confused later. */
4620 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4624 /* At this point, if we're still interested in issuing messages,
4625 they must apply to classes surrounding the object declared. */
4626 if (msg)
4627 msg = G_("default argument for template parameter for class "
4628 "enclosing %qD");
4631 return no_errors;
4634 /* Worker for push_template_decl_real, called via
4635 for_each_template_parm. DATA is really an int, indicating the
4636 level of the parameters we are interested in. If T is a template
4637 parameter of that level, return nonzero. */
4639 static int
4640 template_parm_this_level_p (tree t, void* data)
4642 int this_level = *(int *)data;
4643 int level;
4645 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4646 level = TEMPLATE_PARM_LEVEL (t);
4647 else
4648 level = TEMPLATE_TYPE_LEVEL (t);
4649 return level == this_level;
4652 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4653 parameters given by current_template_args, or reuses a
4654 previously existing one, if appropriate. Returns the DECL, or an
4655 equivalent one, if it is replaced via a call to duplicate_decls.
4657 If IS_FRIEND is true, DECL is a friend declaration. */
4659 tree
4660 push_template_decl_real (tree decl, bool is_friend)
4662 tree tmpl;
4663 tree args;
4664 tree info;
4665 tree ctx;
4666 int primary;
4667 int is_partial;
4668 int new_template_p = 0;
4669 /* True if the template is a member template, in the sense of
4670 [temp.mem]. */
4671 bool member_template_p = false;
4673 if (decl == error_mark_node || !current_template_parms)
4674 return error_mark_node;
4676 /* See if this is a partial specialization. */
4677 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4678 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4679 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4681 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4682 is_friend = true;
4684 if (is_friend)
4685 /* For a friend, we want the context of the friend function, not
4686 the type of which it is a friend. */
4687 ctx = CP_DECL_CONTEXT (decl);
4688 else if (CP_DECL_CONTEXT (decl)
4689 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4690 /* In the case of a virtual function, we want the class in which
4691 it is defined. */
4692 ctx = CP_DECL_CONTEXT (decl);
4693 else
4694 /* Otherwise, if we're currently defining some class, the DECL
4695 is assumed to be a member of the class. */
4696 ctx = current_scope ();
4698 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4699 ctx = NULL_TREE;
4701 if (!DECL_CONTEXT (decl))
4702 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4704 /* See if this is a primary template. */
4705 if (is_friend && ctx)
4706 /* A friend template that specifies a class context, i.e.
4707 template <typename T> friend void A<T>::f();
4708 is not primary. */
4709 primary = 0;
4710 else
4711 primary = template_parm_scope_p ();
4713 if (primary)
4715 if (DECL_CLASS_SCOPE_P (decl))
4716 member_template_p = true;
4717 if (TREE_CODE (decl) == TYPE_DECL
4718 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4720 error ("template class without a name");
4721 return error_mark_node;
4723 else if (TREE_CODE (decl) == FUNCTION_DECL)
4725 if (DECL_DESTRUCTOR_P (decl))
4727 /* [temp.mem]
4729 A destructor shall not be a member template. */
4730 error ("destructor %qD declared as member template", decl);
4731 return error_mark_node;
4733 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4734 && (!prototype_p (TREE_TYPE (decl))
4735 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4736 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4737 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4738 == void_list_node)))
4740 /* [basic.stc.dynamic.allocation]
4742 An allocation function can be a function
4743 template. ... Template allocation functions shall
4744 have two or more parameters. */
4745 error ("invalid template declaration of %qD", decl);
4746 return error_mark_node;
4749 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4750 && CLASS_TYPE_P (TREE_TYPE (decl)))
4751 /* OK */;
4752 else
4754 error ("template declaration of %q#D", decl);
4755 return error_mark_node;
4759 /* Check to see that the rules regarding the use of default
4760 arguments are not being violated. */
4761 check_default_tmpl_args (decl, current_template_parms,
4762 primary, is_partial, /*is_friend_decl=*/0);
4764 /* Ensure that there are no parameter packs in the type of this
4765 declaration that have not been expanded. */
4766 if (TREE_CODE (decl) == FUNCTION_DECL)
4768 /* Check each of the arguments individually to see if there are
4769 any bare parameter packs. */
4770 tree type = TREE_TYPE (decl);
4771 tree arg = DECL_ARGUMENTS (decl);
4772 tree argtype = TYPE_ARG_TYPES (type);
4774 while (arg && argtype)
4776 if (!FUNCTION_PARAMETER_PACK_P (arg)
4777 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4779 /* This is a PARM_DECL that contains unexpanded parameter
4780 packs. We have already complained about this in the
4781 check_for_bare_parameter_packs call, so just replace
4782 these types with ERROR_MARK_NODE. */
4783 TREE_TYPE (arg) = error_mark_node;
4784 TREE_VALUE (argtype) = error_mark_node;
4787 arg = DECL_CHAIN (arg);
4788 argtype = TREE_CHAIN (argtype);
4791 /* Check for bare parameter packs in the return type and the
4792 exception specifiers. */
4793 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4794 /* Errors were already issued, set return type to int
4795 as the frontend doesn't expect error_mark_node as
4796 the return type. */
4797 TREE_TYPE (type) = integer_type_node;
4798 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4799 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4801 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4803 TREE_TYPE (decl) = error_mark_node;
4804 return error_mark_node;
4807 if (is_partial)
4808 return process_partial_specialization (decl);
4810 args = current_template_args ();
4812 if (!ctx
4813 || TREE_CODE (ctx) == FUNCTION_DECL
4814 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4815 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4817 if (DECL_LANG_SPECIFIC (decl)
4818 && DECL_TEMPLATE_INFO (decl)
4819 && DECL_TI_TEMPLATE (decl))
4820 tmpl = DECL_TI_TEMPLATE (decl);
4821 /* If DECL is a TYPE_DECL for a class-template, then there won't
4822 be DECL_LANG_SPECIFIC. The information equivalent to
4823 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4824 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4825 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4826 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4828 /* Since a template declaration already existed for this
4829 class-type, we must be redeclaring it here. Make sure
4830 that the redeclaration is valid. */
4831 redeclare_class_template (TREE_TYPE (decl),
4832 current_template_parms);
4833 /* We don't need to create a new TEMPLATE_DECL; just use the
4834 one we already had. */
4835 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4837 else
4839 tmpl = build_template_decl (decl, current_template_parms,
4840 member_template_p);
4841 new_template_p = 1;
4843 if (DECL_LANG_SPECIFIC (decl)
4844 && DECL_TEMPLATE_SPECIALIZATION (decl))
4846 /* A specialization of a member template of a template
4847 class. */
4848 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4849 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4850 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4854 else
4856 tree a, t, current, parms;
4857 int i;
4858 tree tinfo = get_template_info (decl);
4860 if (!tinfo)
4862 error ("template definition of non-template %q#D", decl);
4863 return error_mark_node;
4866 tmpl = TI_TEMPLATE (tinfo);
4868 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4869 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4870 && DECL_TEMPLATE_SPECIALIZATION (decl)
4871 && DECL_MEMBER_TEMPLATE_P (tmpl))
4873 tree new_tmpl;
4875 /* The declaration is a specialization of a member
4876 template, declared outside the class. Therefore, the
4877 innermost template arguments will be NULL, so we
4878 replace them with the arguments determined by the
4879 earlier call to check_explicit_specialization. */
4880 args = DECL_TI_ARGS (decl);
4882 new_tmpl
4883 = build_template_decl (decl, current_template_parms,
4884 member_template_p);
4885 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4886 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4887 DECL_TI_TEMPLATE (decl) = new_tmpl;
4888 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4889 DECL_TEMPLATE_INFO (new_tmpl)
4890 = build_template_info (tmpl, args);
4892 register_specialization (new_tmpl,
4893 most_general_template (tmpl),
4894 args,
4895 is_friend, 0);
4896 return decl;
4899 /* Make sure the template headers we got make sense. */
4901 parms = DECL_TEMPLATE_PARMS (tmpl);
4902 i = TMPL_PARMS_DEPTH (parms);
4903 if (TMPL_ARGS_DEPTH (args) != i)
4905 error ("expected %d levels of template parms for %q#D, got %d",
4906 i, decl, TMPL_ARGS_DEPTH (args));
4908 else
4909 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4911 a = TMPL_ARGS_LEVEL (args, i);
4912 t = INNERMOST_TEMPLATE_PARMS (parms);
4914 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4916 if (current == decl)
4917 error ("got %d template parameters for %q#D",
4918 TREE_VEC_LENGTH (a), decl);
4919 else
4920 error ("got %d template parameters for %q#T",
4921 TREE_VEC_LENGTH (a), current);
4922 error (" but %d required", TREE_VEC_LENGTH (t));
4923 return error_mark_node;
4926 if (current == decl)
4927 current = ctx;
4928 else if (current == NULL_TREE)
4929 /* Can happen in erroneous input. */
4930 break;
4931 else
4932 current = (TYPE_P (current)
4933 ? TYPE_CONTEXT (current)
4934 : DECL_CONTEXT (current));
4937 /* Check that the parms are used in the appropriate qualifying scopes
4938 in the declarator. */
4939 if (!comp_template_args
4940 (TI_ARGS (tinfo),
4941 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4943 error ("\
4944 template arguments to %qD do not match original template %qD",
4945 decl, DECL_TEMPLATE_RESULT (tmpl));
4946 if (!uses_template_parms (TI_ARGS (tinfo)))
4947 inform (input_location, "use template<> for an explicit specialization");
4948 /* Avoid crash in import_export_decl. */
4949 DECL_INTERFACE_KNOWN (decl) = 1;
4950 return error_mark_node;
4954 DECL_TEMPLATE_RESULT (tmpl) = decl;
4955 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4957 /* Push template declarations for global functions and types. Note
4958 that we do not try to push a global template friend declared in a
4959 template class; such a thing may well depend on the template
4960 parameters of the class. */
4961 if (new_template_p && !ctx
4962 && !(is_friend && template_class_depth (current_class_type) > 0))
4964 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4965 if (tmpl == error_mark_node)
4966 return error_mark_node;
4968 /* Hide template friend classes that haven't been declared yet. */
4969 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4971 DECL_ANTICIPATED (tmpl) = 1;
4972 DECL_FRIEND_P (tmpl) = 1;
4976 if (primary)
4978 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4979 int i;
4981 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4982 if (DECL_CONV_FN_P (tmpl))
4984 int depth = TMPL_PARMS_DEPTH (parms);
4986 /* It is a conversion operator. See if the type converted to
4987 depends on innermost template operands. */
4989 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4990 depth))
4991 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4994 /* Give template template parms a DECL_CONTEXT of the template
4995 for which they are a parameter. */
4996 parms = INNERMOST_TEMPLATE_PARMS (parms);
4997 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4999 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5000 if (TREE_CODE (parm) == TEMPLATE_DECL)
5001 DECL_CONTEXT (parm) = tmpl;
5005 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5006 back to its most general template. If TMPL is a specialization,
5007 ARGS may only have the innermost set of arguments. Add the missing
5008 argument levels if necessary. */
5009 if (DECL_TEMPLATE_INFO (tmpl))
5010 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5012 info = build_template_info (tmpl, args);
5014 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5015 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5016 else if (DECL_LANG_SPECIFIC (decl))
5017 DECL_TEMPLATE_INFO (decl) = info;
5019 return DECL_TEMPLATE_RESULT (tmpl);
5022 tree
5023 push_template_decl (tree decl)
5025 return push_template_decl_real (decl, false);
5028 /* Called when a class template TYPE is redeclared with the indicated
5029 template PARMS, e.g.:
5031 template <class T> struct S;
5032 template <class T> struct S {}; */
5034 bool
5035 redeclare_class_template (tree type, tree parms)
5037 tree tmpl;
5038 tree tmpl_parms;
5039 int i;
5041 if (!TYPE_TEMPLATE_INFO (type))
5043 error ("%qT is not a template type", type);
5044 return false;
5047 tmpl = TYPE_TI_TEMPLATE (type);
5048 if (!PRIMARY_TEMPLATE_P (tmpl))
5049 /* The type is nested in some template class. Nothing to worry
5050 about here; there are no new template parameters for the nested
5051 type. */
5052 return true;
5054 if (!parms)
5056 error ("template specifiers not specified in declaration of %qD",
5057 tmpl);
5058 return false;
5061 parms = INNERMOST_TEMPLATE_PARMS (parms);
5062 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5064 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5066 error_n (input_location, TREE_VEC_LENGTH (parms),
5067 "redeclared with %d template parameter",
5068 "redeclared with %d template parameters",
5069 TREE_VEC_LENGTH (parms));
5070 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5071 "previous declaration %q+D used %d template parameter",
5072 "previous declaration %q+D used %d template parameters",
5073 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5074 return false;
5077 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5079 tree tmpl_parm;
5080 tree parm;
5081 tree tmpl_default;
5082 tree parm_default;
5084 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5085 || TREE_VEC_ELT (parms, i) == error_mark_node)
5086 continue;
5088 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5089 if (tmpl_parm == error_mark_node)
5090 return false;
5092 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5093 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5094 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5096 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5097 TEMPLATE_DECL. */
5098 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5099 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5100 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5101 || (TREE_CODE (tmpl_parm) != PARM_DECL
5102 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5103 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5104 || (TREE_CODE (tmpl_parm) == PARM_DECL
5105 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5106 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5108 error ("template parameter %q+#D", tmpl_parm);
5109 error ("redeclared here as %q#D", parm);
5110 return false;
5113 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5115 /* We have in [temp.param]:
5117 A template-parameter may not be given default arguments
5118 by two different declarations in the same scope. */
5119 error_at (input_location, "redefinition of default argument for %q#D", parm);
5120 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5121 "original definition appeared here");
5122 return false;
5125 if (parm_default != NULL_TREE)
5126 /* Update the previous template parameters (which are the ones
5127 that will really count) with the new default value. */
5128 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5129 else if (tmpl_default != NULL_TREE)
5130 /* Update the new parameters, too; they'll be used as the
5131 parameters for any members. */
5132 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5135 return true;
5138 /* Simplify EXPR if it is a non-dependent expression. Returns the
5139 (possibly simplified) expression. */
5141 static tree
5142 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5144 if (expr == NULL_TREE)
5145 return NULL_TREE;
5147 /* If we're in a template, but EXPR isn't value dependent, simplify
5148 it. We're supposed to treat:
5150 template <typename T> void f(T[1 + 1]);
5151 template <typename T> void f(T[2]);
5153 as two declarations of the same function, for example. */
5154 if (processing_template_decl
5155 && !type_dependent_expression_p (expr)
5156 && potential_constant_expression (expr)
5157 && !value_dependent_expression_p (expr))
5159 HOST_WIDE_INT saved_processing_template_decl;
5161 saved_processing_template_decl = processing_template_decl;
5162 processing_template_decl = 0;
5163 expr = tsubst_copy_and_build (expr,
5164 /*args=*/NULL_TREE,
5165 complain,
5166 /*in_decl=*/NULL_TREE,
5167 /*function_p=*/false,
5168 /*integral_constant_expression_p=*/true);
5169 processing_template_decl = saved_processing_template_decl;
5171 return expr;
5174 tree
5175 fold_non_dependent_expr (tree expr)
5177 return fold_non_dependent_expr_sfinae (expr, tf_error);
5180 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5181 must be a function or a pointer-to-function type, as specified
5182 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5183 and check that the resulting function has external linkage. */
5185 static tree
5186 convert_nontype_argument_function (tree type, tree expr)
5188 tree fns = expr;
5189 tree fn, fn_no_ptr;
5191 fn = instantiate_type (type, fns, tf_none);
5192 if (fn == error_mark_node)
5193 return error_mark_node;
5195 fn_no_ptr = fn;
5196 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5197 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5198 if (TREE_CODE (fn_no_ptr) == BASELINK)
5199 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5201 /* [temp.arg.nontype]/1
5203 A template-argument for a non-type, non-template template-parameter
5204 shall be one of:
5205 [...]
5206 -- the address of an object or function with external linkage. */
5207 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
5209 error ("%qE is not a valid template argument for type %qT "
5210 "because function %qD has not external linkage",
5211 expr, type, fn_no_ptr);
5212 return NULL_TREE;
5215 return fn;
5218 /* Subroutine of convert_nontype_argument.
5219 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5220 Emit an error otherwise. */
5222 static bool
5223 check_valid_ptrmem_cst_expr (tree type, tree expr,
5224 tsubst_flags_t complain)
5226 STRIP_NOPS (expr);
5227 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5228 return true;
5229 if (complain & tf_error)
5231 error ("%qE is not a valid template argument for type %qT",
5232 expr, type);
5233 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5235 return false;
5238 /* Returns TRUE iff the address of OP is value-dependent.
5240 14.6.2.4 [temp.dep.temp]:
5241 A non-integral non-type template-argument is dependent if its type is
5242 dependent or it has either of the following forms
5243 qualified-id
5244 & qualified-id
5245 and contains a nested-name-specifier which specifies a class-name that
5246 names a dependent type.
5248 We generalize this to just say that the address of a member of a
5249 dependent class is value-dependent; the above doesn't cover the
5250 address of a static data member named with an unqualified-id. */
5252 static bool
5253 has_value_dependent_address (tree op)
5255 /* We could use get_inner_reference here, but there's no need;
5256 this is only relevant for template non-type arguments, which
5257 can only be expressed as &id-expression. */
5258 if (DECL_P (op))
5260 tree ctx = CP_DECL_CONTEXT (op);
5261 if (TYPE_P (ctx) && dependent_type_p (ctx))
5262 return true;
5265 return false;
5268 /* Attempt to convert the non-type template parameter EXPR to the
5269 indicated TYPE. If the conversion is successful, return the
5270 converted value. If the conversion is unsuccessful, return
5271 NULL_TREE if we issued an error message, or error_mark_node if we
5272 did not. We issue error messages for out-and-out bad template
5273 parameters, but not simply because the conversion failed, since we
5274 might be just trying to do argument deduction. Both TYPE and EXPR
5275 must be non-dependent.
5277 The conversion follows the special rules described in
5278 [temp.arg.nontype], and it is much more strict than an implicit
5279 conversion.
5281 This function is called twice for each template argument (see
5282 lookup_template_class for a more accurate description of this
5283 problem). This means that we need to handle expressions which
5284 are not valid in a C++ source, but can be created from the
5285 first call (for instance, casts to perform conversions). These
5286 hacks can go away after we fix the double coercion problem. */
5288 static tree
5289 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5291 tree expr_type;
5293 /* Detect immediately string literals as invalid non-type argument.
5294 This special-case is not needed for correctness (we would easily
5295 catch this later), but only to provide better diagnostic for this
5296 common user mistake. As suggested by DR 100, we do not mention
5297 linkage issues in the diagnostic as this is not the point. */
5298 /* FIXME we're making this OK. */
5299 if (TREE_CODE (expr) == STRING_CST)
5301 if (complain & tf_error)
5302 error ("%qE is not a valid template argument for type %qT "
5303 "because string literals can never be used in this context",
5304 expr, type);
5305 return NULL_TREE;
5308 /* Add the ADDR_EXPR now for the benefit of
5309 value_dependent_expression_p. */
5310 if (TYPE_PTROBV_P (type)
5311 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5312 expr = decay_conversion (expr);
5314 /* If we are in a template, EXPR may be non-dependent, but still
5315 have a syntactic, rather than semantic, form. For example, EXPR
5316 might be a SCOPE_REF, rather than the VAR_DECL to which the
5317 SCOPE_REF refers. Preserving the qualifying scope is necessary
5318 so that access checking can be performed when the template is
5319 instantiated -- but here we need the resolved form so that we can
5320 convert the argument. */
5321 if (TYPE_REF_OBJ_P (type)
5322 && has_value_dependent_address (expr))
5323 /* If we want the address and it's value-dependent, don't fold. */;
5324 else if (!type_unknown_p (expr))
5325 expr = fold_non_dependent_expr_sfinae (expr, complain);
5326 if (error_operand_p (expr))
5327 return error_mark_node;
5328 expr_type = TREE_TYPE (expr);
5329 if (TREE_CODE (type) == REFERENCE_TYPE)
5330 expr = mark_lvalue_use (expr);
5331 else
5332 expr = mark_rvalue_use (expr);
5334 /* HACK: Due to double coercion, we can get a
5335 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5336 which is the tree that we built on the first call (see
5337 below when coercing to reference to object or to reference to
5338 function). We just strip everything and get to the arg.
5339 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5340 for examples. */
5341 if (TREE_CODE (expr) == NOP_EXPR)
5343 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5345 /* ??? Maybe we could use convert_from_reference here, but we
5346 would need to relax its constraints because the NOP_EXPR
5347 could actually change the type to something more cv-qualified,
5348 and this is not folded by convert_from_reference. */
5349 tree addr = TREE_OPERAND (expr, 0);
5350 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
5351 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5352 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5353 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5354 (TREE_TYPE (expr_type),
5355 TREE_TYPE (TREE_TYPE (addr))));
5357 expr = TREE_OPERAND (addr, 0);
5358 expr_type = TREE_TYPE (expr);
5361 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5362 parameter is a pointer to object, through decay and
5363 qualification conversion. Let's strip everything. */
5364 else if (TYPE_PTROBV_P (type))
5366 STRIP_NOPS (expr);
5367 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5368 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5369 /* Skip the ADDR_EXPR only if it is part of the decay for
5370 an array. Otherwise, it is part of the original argument
5371 in the source code. */
5372 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5373 expr = TREE_OPERAND (expr, 0);
5374 expr_type = TREE_TYPE (expr);
5378 /* [temp.arg.nontype]/5, bullet 1
5380 For a non-type template-parameter of integral or enumeration type,
5381 integral promotions (_conv.prom_) and integral conversions
5382 (_conv.integral_) are applied. */
5383 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5385 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5386 t = maybe_constant_value (t);
5387 if (t != error_mark_node)
5388 expr = t;
5390 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5391 return error_mark_node;
5393 /* Notice that there are constant expressions like '4 % 0' which
5394 do not fold into integer constants. */
5395 if (TREE_CODE (expr) != INTEGER_CST)
5397 if (complain & tf_error)
5399 int errs = errorcount, warns = warningcount;
5400 expr = cxx_constant_value (expr);
5401 if (errorcount > errs || warningcount > warns)
5402 inform (EXPR_LOC_OR_HERE (expr),
5403 "in template argument for type %qT ", type);
5404 if (expr == error_mark_node)
5405 return NULL_TREE;
5406 /* else cxx_constant_value complained but gave us
5407 a real constant, so go ahead. */
5408 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5410 else
5411 return NULL_TREE;
5414 /* [temp.arg.nontype]/5, bullet 2
5416 For a non-type template-parameter of type pointer to object,
5417 qualification conversions (_conv.qual_) and the array-to-pointer
5418 conversion (_conv.array_) are applied. */
5419 else if (TYPE_PTROBV_P (type))
5421 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5423 A template-argument for a non-type, non-template template-parameter
5424 shall be one of: [...]
5426 -- the name of a non-type template-parameter;
5427 -- the address of an object or function with external linkage, [...]
5428 expressed as "& id-expression" where the & is optional if the name
5429 refers to a function or array, or if the corresponding
5430 template-parameter is a reference.
5432 Here, we do not care about functions, as they are invalid anyway
5433 for a parameter of type pointer-to-object. */
5435 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5436 /* Non-type template parameters are OK. */
5438 else if (TREE_CODE (expr) != ADDR_EXPR
5439 && TREE_CODE (expr_type) != ARRAY_TYPE)
5441 if (TREE_CODE (expr) == VAR_DECL)
5443 error ("%qD is not a valid template argument "
5444 "because %qD is a variable, not the address of "
5445 "a variable",
5446 expr, expr);
5447 return NULL_TREE;
5449 /* Other values, like integer constants, might be valid
5450 non-type arguments of some other type. */
5451 return error_mark_node;
5453 else
5455 tree decl;
5457 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5458 ? TREE_OPERAND (expr, 0) : expr);
5459 if (TREE_CODE (decl) != VAR_DECL)
5461 error ("%qE is not a valid template argument of type %qT "
5462 "because %qE is not a variable",
5463 expr, type, decl);
5464 return NULL_TREE;
5466 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5468 error ("%qE is not a valid template argument of type %qT "
5469 "because %qD does not have external linkage",
5470 expr, type, decl);
5471 return NULL_TREE;
5475 expr = decay_conversion (expr);
5476 if (expr == error_mark_node)
5477 return error_mark_node;
5479 expr = perform_qualification_conversions (type, expr);
5480 if (expr == error_mark_node)
5481 return error_mark_node;
5483 /* [temp.arg.nontype]/5, bullet 3
5485 For a non-type template-parameter of type reference to object, no
5486 conversions apply. The type referred to by the reference may be more
5487 cv-qualified than the (otherwise identical) type of the
5488 template-argument. The template-parameter is bound directly to the
5489 template-argument, which must be an lvalue. */
5490 else if (TYPE_REF_OBJ_P (type))
5492 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5493 expr_type))
5494 return error_mark_node;
5496 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5498 error ("%qE is not a valid template argument for type %qT "
5499 "because of conflicts in cv-qualification", expr, type);
5500 return NULL_TREE;
5503 if (!real_lvalue_p (expr))
5505 error ("%qE is not a valid template argument for type %qT "
5506 "because it is not an lvalue", expr, type);
5507 return NULL_TREE;
5510 /* [temp.arg.nontype]/1
5512 A template-argument for a non-type, non-template template-parameter
5513 shall be one of: [...]
5515 -- the address of an object or function with external linkage. */
5516 if (TREE_CODE (expr) == INDIRECT_REF
5517 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5519 expr = TREE_OPERAND (expr, 0);
5520 if (DECL_P (expr))
5522 error ("%q#D is not a valid template argument for type %qT "
5523 "because a reference variable does not have a constant "
5524 "address", expr, type);
5525 return NULL_TREE;
5529 if (!DECL_P (expr))
5531 error ("%qE is not a valid template argument for type %qT "
5532 "because it is not an object with external linkage",
5533 expr, type);
5534 return NULL_TREE;
5537 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5539 error ("%qE is not a valid template argument for type %qT "
5540 "because object %qD has not external linkage",
5541 expr, type, expr);
5542 return NULL_TREE;
5545 expr = build_nop (type, build_address (expr));
5547 /* [temp.arg.nontype]/5, bullet 4
5549 For a non-type template-parameter of type pointer to function, only
5550 the function-to-pointer conversion (_conv.func_) is applied. If the
5551 template-argument represents a set of overloaded functions (or a
5552 pointer to such), the matching function is selected from the set
5553 (_over.over_). */
5554 else if (TYPE_PTRFN_P (type))
5556 /* If the argument is a template-id, we might not have enough
5557 context information to decay the pointer. */
5558 if (!type_unknown_p (expr_type))
5560 expr = decay_conversion (expr);
5561 if (expr == error_mark_node)
5562 return error_mark_node;
5565 expr = convert_nontype_argument_function (type, expr);
5566 if (!expr || expr == error_mark_node)
5567 return expr;
5569 if (TREE_CODE (expr) != ADDR_EXPR)
5571 error ("%qE is not a valid template argument for type %qT", expr, type);
5572 error ("it must be the address of a function with external linkage");
5573 return NULL_TREE;
5576 /* [temp.arg.nontype]/5, bullet 5
5578 For a non-type template-parameter of type reference to function, no
5579 conversions apply. If the template-argument represents a set of
5580 overloaded functions, the matching function is selected from the set
5581 (_over.over_). */
5582 else if (TYPE_REFFN_P (type))
5584 if (TREE_CODE (expr) == ADDR_EXPR)
5586 error ("%qE is not a valid template argument for type %qT "
5587 "because it is a pointer", expr, type);
5588 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5589 return NULL_TREE;
5592 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5593 if (!expr || expr == error_mark_node)
5594 return expr;
5596 expr = build_nop (type, build_address (expr));
5598 /* [temp.arg.nontype]/5, bullet 6
5600 For a non-type template-parameter of type pointer to member function,
5601 no conversions apply. If the template-argument represents a set of
5602 overloaded member functions, the matching member function is selected
5603 from the set (_over.over_). */
5604 else if (TYPE_PTRMEMFUNC_P (type))
5606 expr = instantiate_type (type, expr, tf_none);
5607 if (expr == error_mark_node)
5608 return error_mark_node;
5610 /* [temp.arg.nontype] bullet 1 says the pointer to member
5611 expression must be a pointer-to-member constant. */
5612 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5613 return error_mark_node;
5615 /* There is no way to disable standard conversions in
5616 resolve_address_of_overloaded_function (called by
5617 instantiate_type). It is possible that the call succeeded by
5618 converting &B::I to &D::I (where B is a base of D), so we need
5619 to reject this conversion here.
5621 Actually, even if there was a way to disable standard conversions,
5622 it would still be better to reject them here so that we can
5623 provide a superior diagnostic. */
5624 if (!same_type_p (TREE_TYPE (expr), type))
5626 error ("%qE is not a valid template argument for type %qT "
5627 "because it is of type %qT", expr, type,
5628 TREE_TYPE (expr));
5629 /* If we are just one standard conversion off, explain. */
5630 if (can_convert (type, TREE_TYPE (expr)))
5631 inform (input_location,
5632 "standard conversions are not allowed in this context");
5633 return NULL_TREE;
5636 /* [temp.arg.nontype]/5, bullet 7
5638 For a non-type template-parameter of type pointer to data member,
5639 qualification conversions (_conv.qual_) are applied. */
5640 else if (TYPE_PTRMEM_P (type))
5642 /* [temp.arg.nontype] bullet 1 says the pointer to member
5643 expression must be a pointer-to-member constant. */
5644 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5645 return error_mark_node;
5647 expr = perform_qualification_conversions (type, expr);
5648 if (expr == error_mark_node)
5649 return expr;
5651 /* A template non-type parameter must be one of the above. */
5652 else
5653 gcc_unreachable ();
5655 /* Sanity check: did we actually convert the argument to the
5656 right type? */
5657 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5658 (type, TREE_TYPE (expr)));
5659 return expr;
5662 /* Subroutine of coerce_template_template_parms, which returns 1 if
5663 PARM_PARM and ARG_PARM match using the rule for the template
5664 parameters of template template parameters. Both PARM and ARG are
5665 template parameters; the rest of the arguments are the same as for
5666 coerce_template_template_parms.
5668 static int
5669 coerce_template_template_parm (tree parm,
5670 tree arg,
5671 tsubst_flags_t complain,
5672 tree in_decl,
5673 tree outer_args)
5675 if (arg == NULL_TREE || arg == error_mark_node
5676 || parm == NULL_TREE || parm == error_mark_node)
5677 return 0;
5679 if (TREE_CODE (arg) != TREE_CODE (parm))
5680 return 0;
5682 switch (TREE_CODE (parm))
5684 case TEMPLATE_DECL:
5685 /* We encounter instantiations of templates like
5686 template <template <template <class> class> class TT>
5687 class C; */
5689 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5690 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5692 if (!coerce_template_template_parms
5693 (parmparm, argparm, complain, in_decl, outer_args))
5694 return 0;
5696 /* Fall through. */
5698 case TYPE_DECL:
5699 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5700 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5701 /* Argument is a parameter pack but parameter is not. */
5702 return 0;
5703 break;
5705 case PARM_DECL:
5706 /* The tsubst call is used to handle cases such as
5708 template <int> class C {};
5709 template <class T, template <T> class TT> class D {};
5710 D<int, C> d;
5712 i.e. the parameter list of TT depends on earlier parameters. */
5713 if (!uses_template_parms (TREE_TYPE (arg))
5714 && !same_type_p
5715 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5716 TREE_TYPE (arg)))
5717 return 0;
5719 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5720 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5721 /* Argument is a parameter pack but parameter is not. */
5722 return 0;
5724 break;
5726 default:
5727 gcc_unreachable ();
5730 return 1;
5734 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5735 template template parameters. Both PARM_PARMS and ARG_PARMS are
5736 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5737 or PARM_DECL.
5739 Consider the example:
5740 template <class T> class A;
5741 template<template <class U> class TT> class B;
5743 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5744 the parameters to A, and OUTER_ARGS contains A. */
5746 static int
5747 coerce_template_template_parms (tree parm_parms,
5748 tree arg_parms,
5749 tsubst_flags_t complain,
5750 tree in_decl,
5751 tree outer_args)
5753 int nparms, nargs, i;
5754 tree parm, arg;
5755 int variadic_p = 0;
5757 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5758 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5760 nparms = TREE_VEC_LENGTH (parm_parms);
5761 nargs = TREE_VEC_LENGTH (arg_parms);
5763 /* Determine whether we have a parameter pack at the end of the
5764 template template parameter's template parameter list. */
5765 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5767 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5769 if (parm == error_mark_node)
5770 return 0;
5772 switch (TREE_CODE (parm))
5774 case TEMPLATE_DECL:
5775 case TYPE_DECL:
5776 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5777 variadic_p = 1;
5778 break;
5780 case PARM_DECL:
5781 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5782 variadic_p = 1;
5783 break;
5785 default:
5786 gcc_unreachable ();
5790 if (nargs != nparms
5791 && !(variadic_p && nargs >= nparms - 1))
5792 return 0;
5794 /* Check all of the template parameters except the parameter pack at
5795 the end (if any). */
5796 for (i = 0; i < nparms - variadic_p; ++i)
5798 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5799 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5800 continue;
5802 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5803 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5805 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5806 outer_args))
5807 return 0;
5811 if (variadic_p)
5813 /* Check each of the template parameters in the template
5814 argument against the template parameter pack at the end of
5815 the template template parameter. */
5816 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5817 return 0;
5819 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5821 for (; i < nargs; ++i)
5823 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5824 continue;
5826 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5828 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5829 outer_args))
5830 return 0;
5834 return 1;
5837 /* Verifies that the deduced template arguments (in TARGS) for the
5838 template template parameters (in TPARMS) represent valid bindings,
5839 by comparing the template parameter list of each template argument
5840 to the template parameter list of its corresponding template
5841 template parameter, in accordance with DR150. This
5842 routine can only be called after all template arguments have been
5843 deduced. It will return TRUE if all of the template template
5844 parameter bindings are okay, FALSE otherwise. */
5845 bool
5846 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5848 int i, ntparms = TREE_VEC_LENGTH (tparms);
5849 bool ret = true;
5851 /* We're dealing with template parms in this process. */
5852 ++processing_template_decl;
5854 targs = INNERMOST_TEMPLATE_ARGS (targs);
5856 for (i = 0; i < ntparms; ++i)
5858 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5859 tree targ = TREE_VEC_ELT (targs, i);
5861 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5863 tree packed_args = NULL_TREE;
5864 int idx, len = 1;
5866 if (ARGUMENT_PACK_P (targ))
5868 /* Look inside the argument pack. */
5869 packed_args = ARGUMENT_PACK_ARGS (targ);
5870 len = TREE_VEC_LENGTH (packed_args);
5873 for (idx = 0; idx < len; ++idx)
5875 tree targ_parms = NULL_TREE;
5877 if (packed_args)
5878 /* Extract the next argument from the argument
5879 pack. */
5880 targ = TREE_VEC_ELT (packed_args, idx);
5882 if (PACK_EXPANSION_P (targ))
5883 /* Look at the pattern of the pack expansion. */
5884 targ = PACK_EXPANSION_PATTERN (targ);
5886 /* Extract the template parameters from the template
5887 argument. */
5888 if (TREE_CODE (targ) == TEMPLATE_DECL)
5889 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5890 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5891 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5893 /* Verify that we can coerce the template template
5894 parameters from the template argument to the template
5895 parameter. This requires an exact match. */
5896 if (targ_parms
5897 && !coerce_template_template_parms
5898 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5899 targ_parms,
5900 tf_none,
5901 tparm,
5902 targs))
5904 ret = false;
5905 goto out;
5911 out:
5913 --processing_template_decl;
5914 return ret;
5917 /* Convert the indicated template ARG as necessary to match the
5918 indicated template PARM. Returns the converted ARG, or
5919 error_mark_node if the conversion was unsuccessful. Error and
5920 warning messages are issued under control of COMPLAIN. This
5921 conversion is for the Ith parameter in the parameter list. ARGS is
5922 the full set of template arguments deduced so far. */
5924 static tree
5925 convert_template_argument (tree parm,
5926 tree arg,
5927 tree args,
5928 tsubst_flags_t complain,
5929 int i,
5930 tree in_decl)
5932 tree orig_arg;
5933 tree val;
5934 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5936 if (TREE_CODE (arg) == TREE_LIST
5937 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5939 /* The template argument was the name of some
5940 member function. That's usually
5941 invalid, but static members are OK. In any
5942 case, grab the underlying fields/functions
5943 and issue an error later if required. */
5944 orig_arg = TREE_VALUE (arg);
5945 TREE_TYPE (arg) = unknown_type_node;
5948 orig_arg = arg;
5950 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5951 requires_type = (TREE_CODE (parm) == TYPE_DECL
5952 || requires_tmpl_type);
5954 /* When determining whether an argument pack expansion is a template,
5955 look at the pattern. */
5956 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5957 arg = PACK_EXPANSION_PATTERN (arg);
5959 /* Deal with an injected-class-name used as a template template arg. */
5960 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5962 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5963 if (TREE_CODE (t) == TEMPLATE_DECL)
5965 if (complain & tf_warning_or_error)
5966 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5967 " used as template template argument", TYPE_NAME (arg));
5968 else if (flag_pedantic_errors)
5969 t = arg;
5971 arg = t;
5975 is_tmpl_type =
5976 ((TREE_CODE (arg) == TEMPLATE_DECL
5977 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5978 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5979 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5981 if (is_tmpl_type
5982 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5983 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5984 arg = TYPE_STUB_DECL (arg);
5986 is_type = TYPE_P (arg) || is_tmpl_type;
5988 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5989 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5991 permerror (input_location, "to refer to a type member of a template parameter, "
5992 "use %<typename %E%>", orig_arg);
5994 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5995 TREE_OPERAND (arg, 1),
5996 typename_type,
5997 complain & tf_error);
5998 arg = orig_arg;
5999 is_type = 1;
6001 if (is_type != requires_type)
6003 if (in_decl)
6005 if (complain & tf_error)
6007 error ("type/value mismatch at argument %d in template "
6008 "parameter list for %qD",
6009 i + 1, in_decl);
6010 if (is_type)
6011 error (" expected a constant of type %qT, got %qT",
6012 TREE_TYPE (parm),
6013 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6014 else if (requires_tmpl_type)
6015 error (" expected a class template, got %qE", orig_arg);
6016 else
6017 error (" expected a type, got %qE", orig_arg);
6020 return error_mark_node;
6022 if (is_tmpl_type ^ requires_tmpl_type)
6024 if (in_decl && (complain & tf_error))
6026 error ("type/value mismatch at argument %d in template "
6027 "parameter list for %qD",
6028 i + 1, in_decl);
6029 if (is_tmpl_type)
6030 error (" expected a type, got %qT", DECL_NAME (arg));
6031 else
6032 error (" expected a class template, got %qT", orig_arg);
6034 return error_mark_node;
6037 if (is_type)
6039 if (requires_tmpl_type)
6041 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6042 /* The number of argument required is not known yet.
6043 Just accept it for now. */
6044 val = TREE_TYPE (arg);
6045 else
6047 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6048 tree argparm;
6050 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6052 if (coerce_template_template_parms (parmparm, argparm,
6053 complain, in_decl,
6054 args))
6056 val = arg;
6058 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6059 TEMPLATE_DECL. */
6060 if (val != error_mark_node)
6062 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6063 val = TREE_TYPE (val);
6064 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6065 val = make_pack_expansion (val);
6068 else
6070 if (in_decl && (complain & tf_error))
6072 error ("type/value mismatch at argument %d in "
6073 "template parameter list for %qD",
6074 i + 1, in_decl);
6075 error (" expected a template of type %qD, got %qT",
6076 parm, orig_arg);
6079 val = error_mark_node;
6083 else
6084 val = orig_arg;
6085 /* We only form one instance of each template specialization.
6086 Therefore, if we use a non-canonical variant (i.e., a
6087 typedef), any future messages referring to the type will use
6088 the typedef, which is confusing if those future uses do not
6089 themselves also use the typedef. */
6090 if (TYPE_P (val))
6091 val = strip_typedefs (val);
6093 else
6095 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6097 if (invalid_nontype_parm_type_p (t, complain))
6098 return error_mark_node;
6100 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6102 if (same_type_p (t, TREE_TYPE (orig_arg)))
6103 val = orig_arg;
6104 else
6106 /* Not sure if this is reachable, but it doesn't hurt
6107 to be robust. */
6108 error ("type mismatch in nontype parameter pack");
6109 val = error_mark_node;
6112 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6113 /* We used to call digest_init here. However, digest_init
6114 will report errors, which we don't want when complain
6115 is zero. More importantly, digest_init will try too
6116 hard to convert things: for example, `0' should not be
6117 converted to pointer type at this point according to
6118 the standard. Accepting this is not merely an
6119 extension, since deciding whether or not these
6120 conversions can occur is part of determining which
6121 function template to call, or whether a given explicit
6122 argument specification is valid. */
6123 val = convert_nontype_argument (t, orig_arg, complain);
6124 else
6125 val = orig_arg;
6127 if (val == NULL_TREE)
6128 val = error_mark_node;
6129 else if (val == error_mark_node && (complain & tf_error))
6130 error ("could not convert template argument %qE to %qT", orig_arg, t);
6132 if (TREE_CODE (val) == SCOPE_REF)
6134 /* Strip typedefs from the SCOPE_REF. */
6135 tree type = strip_typedefs (TREE_TYPE (val));
6136 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
6137 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6138 QUALIFIED_NAME_IS_TEMPLATE (val));
6142 return val;
6145 /* Coerces the remaining template arguments in INNER_ARGS (from
6146 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6147 Returns the coerced argument pack. PARM_IDX is the position of this
6148 parameter in the template parameter list. ARGS is the original
6149 template argument list. */
6150 static tree
6151 coerce_template_parameter_pack (tree parms,
6152 int parm_idx,
6153 tree args,
6154 tree inner_args,
6155 int arg_idx,
6156 tree new_args,
6157 int* lost,
6158 tree in_decl,
6159 tsubst_flags_t complain)
6161 tree parm = TREE_VEC_ELT (parms, parm_idx);
6162 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6163 tree packed_args;
6164 tree argument_pack;
6165 tree packed_types = NULL_TREE;
6167 if (arg_idx > nargs)
6168 arg_idx = nargs;
6170 packed_args = make_tree_vec (nargs - arg_idx);
6172 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6173 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6175 /* When the template parameter is a non-type template
6176 parameter pack whose type uses parameter packs, we need
6177 to look at each of the template arguments
6178 separately. Build a vector of the types for these
6179 non-type template parameters in PACKED_TYPES. */
6180 tree expansion
6181 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6182 packed_types = tsubst_pack_expansion (expansion, args,
6183 complain, in_decl);
6185 if (packed_types == error_mark_node)
6186 return error_mark_node;
6188 /* Check that we have the right number of arguments. */
6189 if (arg_idx < nargs
6190 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6191 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6193 int needed_parms
6194 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6195 error ("wrong number of template arguments (%d, should be %d)",
6196 nargs, needed_parms);
6197 return error_mark_node;
6200 /* If we aren't able to check the actual arguments now
6201 (because they haven't been expanded yet), we can at least
6202 verify that all of the types used for the non-type
6203 template parameter pack are, in fact, valid for non-type
6204 template parameters. */
6205 if (arg_idx < nargs
6206 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6208 int j, len = TREE_VEC_LENGTH (packed_types);
6209 for (j = 0; j < len; ++j)
6211 tree t = TREE_VEC_ELT (packed_types, j);
6212 if (invalid_nontype_parm_type_p (t, complain))
6213 return error_mark_node;
6218 /* Convert the remaining arguments, which will be a part of the
6219 parameter pack "parm". */
6220 for (; arg_idx < nargs; ++arg_idx)
6222 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6223 tree actual_parm = TREE_VALUE (parm);
6225 if (packed_types && !PACK_EXPANSION_P (arg))
6227 /* When we have a vector of types (corresponding to the
6228 non-type template parameter pack that uses parameter
6229 packs in its type, as mention above), and the
6230 argument is not an expansion (which expands to a
6231 currently unknown number of arguments), clone the
6232 parm and give it the next type in PACKED_TYPES. */
6233 actual_parm = copy_node (actual_parm);
6234 TREE_TYPE (actual_parm) =
6235 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6238 if (arg != error_mark_node)
6239 arg = convert_template_argument (actual_parm,
6240 arg, new_args, complain, parm_idx,
6241 in_decl);
6242 if (arg == error_mark_node)
6243 (*lost)++;
6244 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6247 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6248 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6249 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6250 else
6252 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6253 TREE_TYPE (argument_pack)
6254 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6255 TREE_CONSTANT (argument_pack) = 1;
6258 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6259 #ifdef ENABLE_CHECKING
6260 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6261 TREE_VEC_LENGTH (packed_args));
6262 #endif
6263 return argument_pack;
6266 /* Convert all template arguments to their appropriate types, and
6267 return a vector containing the innermost resulting template
6268 arguments. If any error occurs, return error_mark_node. Error and
6269 warning messages are issued under control of COMPLAIN.
6271 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6272 for arguments not specified in ARGS. Otherwise, if
6273 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6274 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6275 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6276 ARGS. */
6278 static tree
6279 coerce_template_parms (tree parms,
6280 tree args,
6281 tree in_decl,
6282 tsubst_flags_t complain,
6283 bool require_all_args,
6284 bool use_default_args)
6286 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6287 tree inner_args;
6288 tree new_args;
6289 tree new_inner_args;
6290 int saved_unevaluated_operand;
6291 int saved_inhibit_evaluation_warnings;
6293 /* When used as a boolean value, indicates whether this is a
6294 variadic template parameter list. Since it's an int, we can also
6295 subtract it from nparms to get the number of non-variadic
6296 parameters. */
6297 int variadic_p = 0;
6299 if (args == error_mark_node)
6300 return error_mark_node;
6302 nparms = TREE_VEC_LENGTH (parms);
6304 /* Determine if there are any parameter packs. */
6305 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6307 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6308 if (template_parameter_pack_p (tparm))
6309 ++variadic_p;
6312 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6313 /* If there are 0 or 1 parameter packs, we need to expand any argument
6314 packs so that we can deduce a parameter pack from some non-packed args
6315 followed by an argument pack, as in variadic85.C. If there are more
6316 than that, we need to leave argument packs intact so the arguments are
6317 assigned to the right parameter packs. This should only happen when
6318 dealing with a nested class inside a partial specialization of a class
6319 template, as in variadic92.C. */
6320 if (variadic_p <= 1)
6321 inner_args = expand_template_argument_pack (inner_args);
6323 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6324 if ((nargs > nparms && !variadic_p)
6325 || (nargs < nparms - variadic_p
6326 && require_all_args
6327 && (!use_default_args
6328 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6329 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6331 if (complain & tf_error)
6333 if (variadic_p)
6335 --nparms;
6336 error ("wrong number of template arguments "
6337 "(%d, should be %d or more)", nargs, nparms);
6339 else
6340 error ("wrong number of template arguments "
6341 "(%d, should be %d)", nargs, nparms);
6343 if (in_decl)
6344 error ("provided for %q+D", in_decl);
6347 return error_mark_node;
6350 /* We need to evaluate the template arguments, even though this
6351 template-id may be nested within a "sizeof". */
6352 saved_unevaluated_operand = cp_unevaluated_operand;
6353 cp_unevaluated_operand = 0;
6354 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6355 c_inhibit_evaluation_warnings = 0;
6356 new_inner_args = make_tree_vec (nparms);
6357 new_args = add_outermost_template_args (args, new_inner_args);
6358 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6360 tree arg;
6361 tree parm;
6363 /* Get the Ith template parameter. */
6364 parm = TREE_VEC_ELT (parms, parm_idx);
6366 if (parm == error_mark_node)
6368 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6369 continue;
6372 /* Calculate the next argument. */
6373 if (arg_idx < nargs)
6374 arg = TREE_VEC_ELT (inner_args, arg_idx);
6375 else
6376 arg = NULL_TREE;
6378 if (template_parameter_pack_p (TREE_VALUE (parm))
6379 && !(arg && ARGUMENT_PACK_P (arg)))
6381 /* All remaining arguments will be placed in the
6382 template parameter pack PARM. */
6383 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6384 inner_args, arg_idx,
6385 new_args, &lost,
6386 in_decl, complain);
6388 /* Store this argument. */
6389 if (arg == error_mark_node)
6390 lost++;
6391 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6393 /* We are done with all of the arguments. */
6394 arg_idx = nargs;
6396 continue;
6398 else if (arg)
6400 if (PACK_EXPANSION_P (arg))
6402 if (complain & tf_error)
6404 /* FIXME this restriction was removed by N2555; see
6405 bug 35722. */
6406 /* If ARG is a pack expansion, but PARM is not a
6407 template parameter pack (if it were, we would have
6408 handled it above), we're trying to expand into a
6409 fixed-length argument list. */
6410 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6411 sorry ("cannot expand %<%E%> into a fixed-length "
6412 "argument list", arg);
6413 else
6414 sorry ("cannot expand %<%T%> into a fixed-length "
6415 "argument list", arg);
6417 ++lost;
6420 else if (require_all_args)
6422 /* There must be a default arg in this case. */
6423 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6424 complain, in_decl);
6425 /* The position of the first default template argument,
6426 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6427 Record that. */
6428 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6429 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6431 else
6432 break;
6434 if (arg == error_mark_node)
6436 if (complain & tf_error)
6437 error ("template argument %d is invalid", arg_idx + 1);
6439 else if (!arg)
6440 /* This only occurs if there was an error in the template
6441 parameter list itself (which we would already have
6442 reported) that we are trying to recover from, e.g., a class
6443 template with a parameter list such as
6444 template<typename..., typename>. */
6445 ++lost;
6446 else
6447 arg = convert_template_argument (TREE_VALUE (parm),
6448 arg, new_args, complain,
6449 parm_idx, in_decl);
6451 if (arg == error_mark_node)
6452 lost++;
6453 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6455 cp_unevaluated_operand = saved_unevaluated_operand;
6456 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6458 if (lost)
6459 return error_mark_node;
6461 #ifdef ENABLE_CHECKING
6462 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6463 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6464 TREE_VEC_LENGTH (new_inner_args));
6465 #endif
6467 return new_inner_args;
6470 /* Returns 1 if template args OT and NT are equivalent. */
6472 static int
6473 template_args_equal (tree ot, tree nt)
6475 if (nt == ot)
6476 return 1;
6478 if (TREE_CODE (nt) == TREE_VEC)
6479 /* For member templates */
6480 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6481 else if (PACK_EXPANSION_P (ot))
6482 return PACK_EXPANSION_P (nt)
6483 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6484 PACK_EXPANSION_PATTERN (nt));
6485 else if (ARGUMENT_PACK_P (ot))
6487 int i, len;
6488 tree opack, npack;
6490 if (!ARGUMENT_PACK_P (nt))
6491 return 0;
6493 opack = ARGUMENT_PACK_ARGS (ot);
6494 npack = ARGUMENT_PACK_ARGS (nt);
6495 len = TREE_VEC_LENGTH (opack);
6496 if (TREE_VEC_LENGTH (npack) != len)
6497 return 0;
6498 for (i = 0; i < len; ++i)
6499 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6500 TREE_VEC_ELT (npack, i)))
6501 return 0;
6502 return 1;
6504 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6506 /* We get here probably because we are in the middle of substituting
6507 into the pattern of a pack expansion. In that case the
6508 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6509 interested in. So we want to use the initial pack argument for
6510 the comparison. */
6511 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6512 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6513 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6514 return template_args_equal (ot, nt);
6516 else if (TYPE_P (nt))
6517 return TYPE_P (ot) && same_type_p (ot, nt);
6518 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6519 return 0;
6520 else
6521 return cp_tree_equal (ot, nt);
6524 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6525 of template arguments. Returns 0 otherwise. */
6528 comp_template_args (tree oldargs, tree newargs)
6530 int i;
6532 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6533 return 0;
6535 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6537 tree nt = TREE_VEC_ELT (newargs, i);
6538 tree ot = TREE_VEC_ELT (oldargs, i);
6540 if (! template_args_equal (ot, nt))
6541 return 0;
6543 return 1;
6546 static void
6547 add_pending_template (tree d)
6549 tree ti = (TYPE_P (d)
6550 ? CLASSTYPE_TEMPLATE_INFO (d)
6551 : DECL_TEMPLATE_INFO (d));
6552 struct pending_template *pt;
6553 int level;
6555 if (TI_PENDING_TEMPLATE_FLAG (ti))
6556 return;
6558 /* We are called both from instantiate_decl, where we've already had a
6559 tinst_level pushed, and instantiate_template, where we haven't.
6560 Compensate. */
6561 level = !current_tinst_level || current_tinst_level->decl != d;
6563 if (level)
6564 push_tinst_level (d);
6566 pt = ggc_alloc_pending_template ();
6567 pt->next = NULL;
6568 pt->tinst = current_tinst_level;
6569 if (last_pending_template)
6570 last_pending_template->next = pt;
6571 else
6572 pending_templates = pt;
6574 last_pending_template = pt;
6576 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6578 if (level)
6579 pop_tinst_level ();
6583 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6584 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6585 documentation for TEMPLATE_ID_EXPR. */
6587 tree
6588 lookup_template_function (tree fns, tree arglist)
6590 tree type;
6592 if (fns == error_mark_node || arglist == error_mark_node)
6593 return error_mark_node;
6595 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6596 gcc_assert (fns && (is_overloaded_fn (fns)
6597 || TREE_CODE (fns) == IDENTIFIER_NODE));
6599 if (BASELINK_P (fns))
6601 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6602 unknown_type_node,
6603 BASELINK_FUNCTIONS (fns),
6604 arglist);
6605 return fns;
6608 type = TREE_TYPE (fns);
6609 if (TREE_CODE (fns) == OVERLOAD || !type)
6610 type = unknown_type_node;
6612 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6615 /* Within the scope of a template class S<T>, the name S gets bound
6616 (in build_self_reference) to a TYPE_DECL for the class, not a
6617 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6618 or one of its enclosing classes, and that type is a template,
6619 return the associated TEMPLATE_DECL. Otherwise, the original
6620 DECL is returned.
6622 Also handle the case when DECL is a TREE_LIST of ambiguous
6623 injected-class-names from different bases. */
6625 tree
6626 maybe_get_template_decl_from_type_decl (tree decl)
6628 if (decl == NULL_TREE)
6629 return decl;
6631 /* DR 176: A lookup that finds an injected-class-name (10.2
6632 [class.member.lookup]) can result in an ambiguity in certain cases
6633 (for example, if it is found in more than one base class). If all of
6634 the injected-class-names that are found refer to specializations of
6635 the same class template, and if the name is followed by a
6636 template-argument-list, the reference refers to the class template
6637 itself and not a specialization thereof, and is not ambiguous. */
6638 if (TREE_CODE (decl) == TREE_LIST)
6640 tree t, tmpl = NULL_TREE;
6641 for (t = decl; t; t = TREE_CHAIN (t))
6643 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6644 if (!tmpl)
6645 tmpl = elt;
6646 else if (tmpl != elt)
6647 break;
6649 if (tmpl && t == NULL_TREE)
6650 return tmpl;
6651 else
6652 return decl;
6655 return (decl != NULL_TREE
6656 && DECL_SELF_REFERENCE_P (decl)
6657 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6658 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6661 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6662 parameters, find the desired type.
6664 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6666 IN_DECL, if non-NULL, is the template declaration we are trying to
6667 instantiate.
6669 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6670 the class we are looking up.
6672 Issue error and warning messages under control of COMPLAIN.
6674 If the template class is really a local class in a template
6675 function, then the FUNCTION_CONTEXT is the function in which it is
6676 being instantiated.
6678 ??? Note that this function is currently called *twice* for each
6679 template-id: the first time from the parser, while creating the
6680 incomplete type (finish_template_type), and the second type during the
6681 real instantiation (instantiate_template_class). This is surely something
6682 that we want to avoid. It also causes some problems with argument
6683 coercion (see convert_nontype_argument for more information on this). */
6685 tree
6686 lookup_template_class (tree d1,
6687 tree arglist,
6688 tree in_decl,
6689 tree context,
6690 int entering_scope,
6691 tsubst_flags_t complain)
6693 tree templ = NULL_TREE, parmlist;
6694 tree t;
6695 spec_entry **slot;
6696 spec_entry *entry;
6697 spec_entry elt;
6698 hashval_t hash;
6700 timevar_push (TV_NAME_LOOKUP);
6702 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6704 tree value = innermost_non_namespace_value (d1);
6705 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6706 templ = value;
6707 else
6709 if (context)
6710 push_decl_namespace (context);
6711 templ = lookup_name (d1);
6712 templ = maybe_get_template_decl_from_type_decl (templ);
6713 if (context)
6714 pop_decl_namespace ();
6716 if (templ)
6717 context = DECL_CONTEXT (templ);
6719 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6721 tree type = TREE_TYPE (d1);
6723 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6724 an implicit typename for the second A. Deal with it. */
6725 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6726 type = TREE_TYPE (type);
6728 if (CLASSTYPE_TEMPLATE_INFO (type))
6730 templ = CLASSTYPE_TI_TEMPLATE (type);
6731 d1 = DECL_NAME (templ);
6734 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6735 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6737 templ = TYPE_TI_TEMPLATE (d1);
6738 d1 = DECL_NAME (templ);
6740 else if (TREE_CODE (d1) == TEMPLATE_DECL
6741 && DECL_TEMPLATE_RESULT (d1)
6742 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6744 templ = d1;
6745 d1 = DECL_NAME (templ);
6746 context = DECL_CONTEXT (templ);
6749 /* Issue an error message if we didn't find a template. */
6750 if (! templ)
6752 if (complain & tf_error)
6753 error ("%qT is not a template", d1);
6754 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6757 if (TREE_CODE (templ) != TEMPLATE_DECL
6758 /* Make sure it's a user visible template, if it was named by
6759 the user. */
6760 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6761 && !PRIMARY_TEMPLATE_P (templ)))
6763 if (complain & tf_error)
6765 error ("non-template type %qT used as a template", d1);
6766 if (in_decl)
6767 error ("for template declaration %q+D", in_decl);
6769 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6772 complain &= ~tf_user;
6774 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6776 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6777 template arguments */
6779 tree parm;
6780 tree arglist2;
6781 tree outer;
6783 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6785 /* Consider an example where a template template parameter declared as
6787 template <class T, class U = std::allocator<T> > class TT
6789 The template parameter level of T and U are one level larger than
6790 of TT. To proper process the default argument of U, say when an
6791 instantiation `TT<int>' is seen, we need to build the full
6792 arguments containing {int} as the innermost level. Outer levels,
6793 available when not appearing as default template argument, can be
6794 obtained from the arguments of the enclosing template.
6796 Suppose that TT is later substituted with std::vector. The above
6797 instantiation is `TT<int, std::allocator<T> >' with TT at
6798 level 1, and T at level 2, while the template arguments at level 1
6799 becomes {std::vector} and the inner level 2 is {int}. */
6801 outer = DECL_CONTEXT (templ);
6802 if (outer)
6803 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6804 else if (current_template_parms)
6805 /* This is an argument of the current template, so we haven't set
6806 DECL_CONTEXT yet. */
6807 outer = current_template_args ();
6809 if (outer)
6810 arglist = add_to_template_args (outer, arglist);
6812 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6813 complain,
6814 /*require_all_args=*/true,
6815 /*use_default_args=*/true);
6816 if (arglist2 == error_mark_node
6817 || (!uses_template_parms (arglist2)
6818 && check_instantiated_args (templ, arglist2, complain)))
6819 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6821 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6822 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6824 else
6826 tree template_type = TREE_TYPE (templ);
6827 tree gen_tmpl;
6828 tree type_decl;
6829 tree found = NULL_TREE;
6830 int arg_depth;
6831 int parm_depth;
6832 int is_dependent_type;
6833 int use_partial_inst_tmpl = false;
6835 gen_tmpl = most_general_template (templ);
6836 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6837 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6838 arg_depth = TMPL_ARGS_DEPTH (arglist);
6840 if (arg_depth == 1 && parm_depth > 1)
6842 /* We've been given an incomplete set of template arguments.
6843 For example, given:
6845 template <class T> struct S1 {
6846 template <class U> struct S2 {};
6847 template <class U> struct S2<U*> {};
6850 we will be called with an ARGLIST of `U*', but the
6851 TEMPLATE will be `template <class T> template
6852 <class U> struct S1<T>::S2'. We must fill in the missing
6853 arguments. */
6854 arglist
6855 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6856 arglist);
6857 arg_depth = TMPL_ARGS_DEPTH (arglist);
6860 /* Now we should have enough arguments. */
6861 gcc_assert (parm_depth == arg_depth);
6863 /* From here on, we're only interested in the most general
6864 template. */
6866 /* Calculate the BOUND_ARGS. These will be the args that are
6867 actually tsubst'd into the definition to create the
6868 instantiation. */
6869 if (parm_depth > 1)
6871 /* We have multiple levels of arguments to coerce, at once. */
6872 int i;
6873 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6875 tree bound_args = make_tree_vec (parm_depth);
6877 for (i = saved_depth,
6878 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6879 i > 0 && t != NULL_TREE;
6880 --i, t = TREE_CHAIN (t))
6882 tree a;
6883 if (i == saved_depth)
6884 a = coerce_template_parms (TREE_VALUE (t),
6885 arglist, gen_tmpl,
6886 complain,
6887 /*require_all_args=*/true,
6888 /*use_default_args=*/true);
6889 else
6890 /* Outer levels should have already been coerced. */
6891 a = TMPL_ARGS_LEVEL (arglist, i);
6893 /* Don't process further if one of the levels fails. */
6894 if (a == error_mark_node)
6896 /* Restore the ARGLIST to its full size. */
6897 TREE_VEC_LENGTH (arglist) = saved_depth;
6898 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6901 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6903 /* We temporarily reduce the length of the ARGLIST so
6904 that coerce_template_parms will see only the arguments
6905 corresponding to the template parameters it is
6906 examining. */
6907 TREE_VEC_LENGTH (arglist)--;
6910 /* Restore the ARGLIST to its full size. */
6911 TREE_VEC_LENGTH (arglist) = saved_depth;
6913 arglist = bound_args;
6915 else
6916 arglist
6917 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6918 INNERMOST_TEMPLATE_ARGS (arglist),
6919 gen_tmpl,
6920 complain,
6921 /*require_all_args=*/true,
6922 /*use_default_args=*/true);
6924 if (arglist == error_mark_node)
6925 /* We were unable to bind the arguments. */
6926 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6928 /* In the scope of a template class, explicit references to the
6929 template class refer to the type of the template, not any
6930 instantiation of it. For example, in:
6932 template <class T> class C { void f(C<T>); }
6934 the `C<T>' is just the same as `C'. Outside of the
6935 class, however, such a reference is an instantiation. */
6936 if ((entering_scope
6937 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6938 || currently_open_class (template_type))
6939 /* comp_template_args is expensive, check it last. */
6940 && comp_template_args (TYPE_TI_ARGS (template_type),
6941 arglist))
6942 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6944 /* If we already have this specialization, return it. */
6945 elt.tmpl = gen_tmpl;
6946 elt.args = arglist;
6947 hash = hash_specialization (&elt);
6948 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6949 &elt, hash);
6951 if (entry)
6952 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6954 is_dependent_type = uses_template_parms (arglist);
6956 /* If the deduced arguments are invalid, then the binding
6957 failed. */
6958 if (!is_dependent_type
6959 && check_instantiated_args (gen_tmpl,
6960 INNERMOST_TEMPLATE_ARGS (arglist),
6961 complain))
6962 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6964 if (!is_dependent_type
6965 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6966 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6967 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6969 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6970 DECL_NAME (gen_tmpl),
6971 /*tag_scope=*/ts_global);
6972 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6975 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6976 complain, in_decl);
6977 if (!context)
6978 context = global_namespace;
6980 /* Create the type. */
6981 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6983 if (!is_dependent_type)
6985 set_current_access_from_decl (TYPE_NAME (template_type));
6986 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
6987 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6988 arglist, complain, in_decl),
6989 SCOPED_ENUM_P (template_type), NULL);
6991 else
6993 /* We don't want to call start_enum for this type, since
6994 the values for the enumeration constants may involve
6995 template parameters. And, no one should be interested
6996 in the enumeration constants for such a type. */
6997 t = cxx_make_type (ENUMERAL_TYPE);
6998 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7000 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7001 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7002 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7004 else
7006 t = make_class_type (TREE_CODE (template_type));
7007 CLASSTYPE_DECLARED_CLASS (t)
7008 = CLASSTYPE_DECLARED_CLASS (template_type);
7009 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7010 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7012 /* A local class. Make sure the decl gets registered properly. */
7013 if (context == current_function_decl)
7014 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7016 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7017 /* This instantiation is another name for the primary
7018 template type. Set the TYPE_CANONICAL field
7019 appropriately. */
7020 TYPE_CANONICAL (t) = template_type;
7021 else if (any_template_arguments_need_structural_equality_p (arglist))
7022 /* Some of the template arguments require structural
7023 equality testing, so this template class requires
7024 structural equality testing. */
7025 SET_TYPE_STRUCTURAL_EQUALITY (t);
7028 /* If we called start_enum or pushtag above, this information
7029 will already be set up. */
7030 if (!TYPE_NAME (t))
7032 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7034 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7035 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7036 DECL_SOURCE_LOCATION (type_decl)
7037 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7039 else
7040 type_decl = TYPE_NAME (t);
7042 TREE_PRIVATE (type_decl)
7043 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
7044 TREE_PROTECTED (type_decl)
7045 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
7046 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7048 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7049 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7052 /* Let's consider the explicit specialization of a member
7053 of a class template specialization that is implicitely instantiated,
7054 e.g.:
7055 template<class T>
7056 struct S
7058 template<class U> struct M {}; //#0
7061 template<>
7062 template<>
7063 struct S<int>::M<char> //#1
7065 int i;
7067 [temp.expl.spec]/4 says this is valid.
7069 In this case, when we write:
7070 S<int>::M<char> m;
7072 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7073 the one of #0.
7075 When we encounter #1, we want to store the partial instantiation
7076 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7078 For all cases other than this "explicit specialization of member of a
7079 class template", we just want to store the most general template into
7080 the CLASSTYPE_TI_TEMPLATE of M.
7082 This case of "explicit specialization of member of a class template"
7083 only happens when:
7084 1/ the enclosing class is an instantiation of, and therefore not
7085 the same as, the context of the most general template, and
7086 2/ we aren't looking at the partial instantiation itself, i.e.
7087 the innermost arguments are not the same as the innermost parms of
7088 the most general template.
7090 So it's only when 1/ and 2/ happens that we want to use the partial
7091 instantiation of the member template in lieu of its most general
7092 template. */
7094 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7095 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7096 /* the enclosing class must be an instantiation... */
7097 && CLASS_TYPE_P (context)
7098 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7100 tree partial_inst_args;
7101 TREE_VEC_LENGTH (arglist)--;
7102 ++processing_template_decl;
7103 partial_inst_args =
7104 tsubst (INNERMOST_TEMPLATE_ARGS
7105 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7106 arglist, complain, NULL_TREE);
7107 --processing_template_decl;
7108 TREE_VEC_LENGTH (arglist)++;
7109 use_partial_inst_tmpl =
7110 /*...and we must not be looking at the partial instantiation
7111 itself. */
7112 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7113 partial_inst_args);
7116 if (!use_partial_inst_tmpl)
7117 /* This case is easy; there are no member templates involved. */
7118 found = gen_tmpl;
7119 else
7121 /* This is a full instantiation of a member template. Find
7122 the partial instantiation of which this is an instance. */
7124 /* Temporarily reduce by one the number of levels in the ARGLIST
7125 so as to avoid comparing the last set of arguments. */
7126 TREE_VEC_LENGTH (arglist)--;
7127 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7128 TREE_VEC_LENGTH (arglist)++;
7129 found = CLASSTYPE_TI_TEMPLATE (found);
7132 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7134 elt.spec = t;
7135 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
7136 &elt, hash, INSERT);
7137 *slot = ggc_alloc_spec_entry ();
7138 **slot = elt;
7140 /* Note this use of the partial instantiation so we can check it
7141 later in maybe_process_partial_specialization. */
7142 DECL_TEMPLATE_INSTANTIATIONS (templ)
7143 = tree_cons (arglist, t,
7144 DECL_TEMPLATE_INSTANTIATIONS (templ));
7146 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
7147 /* Now that the type has been registered on the instantiations
7148 list, we set up the enumerators. Because the enumeration
7149 constants may involve the enumeration type itself, we make
7150 sure to register the type first, and then create the
7151 constants. That way, doing tsubst_expr for the enumeration
7152 constants won't result in recursive calls here; we'll find
7153 the instantiation and exit above. */
7154 tsubst_enum (template_type, t, arglist);
7156 if (is_dependent_type)
7157 /* If the type makes use of template parameters, the
7158 code that generates debugging information will crash. */
7159 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7161 /* Possibly limit visibility based on template args. */
7162 TREE_PUBLIC (type_decl) = 1;
7163 determine_visibility (type_decl);
7165 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
7167 timevar_pop (TV_NAME_LOOKUP);
7170 struct pair_fn_data
7172 tree_fn_t fn;
7173 void *data;
7174 /* True when we should also visit template parameters that occur in
7175 non-deduced contexts. */
7176 bool include_nondeduced_p;
7177 struct pointer_set_t *visited;
7180 /* Called from for_each_template_parm via walk_tree. */
7182 static tree
7183 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7185 tree t = *tp;
7186 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7187 tree_fn_t fn = pfd->fn;
7188 void *data = pfd->data;
7190 if (TYPE_P (t)
7191 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7192 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7193 pfd->include_nondeduced_p))
7194 return error_mark_node;
7196 switch (TREE_CODE (t))
7198 case RECORD_TYPE:
7199 if (TYPE_PTRMEMFUNC_P (t))
7200 break;
7201 /* Fall through. */
7203 case UNION_TYPE:
7204 case ENUMERAL_TYPE:
7205 if (!TYPE_TEMPLATE_INFO (t))
7206 *walk_subtrees = 0;
7207 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7208 fn, data, pfd->visited,
7209 pfd->include_nondeduced_p))
7210 return error_mark_node;
7211 break;
7213 case INTEGER_TYPE:
7214 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7215 fn, data, pfd->visited,
7216 pfd->include_nondeduced_p)
7217 || for_each_template_parm (TYPE_MAX_VALUE (t),
7218 fn, data, pfd->visited,
7219 pfd->include_nondeduced_p))
7220 return error_mark_node;
7221 break;
7223 case METHOD_TYPE:
7224 /* Since we're not going to walk subtrees, we have to do this
7225 explicitly here. */
7226 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7227 pfd->visited, pfd->include_nondeduced_p))
7228 return error_mark_node;
7229 /* Fall through. */
7231 case FUNCTION_TYPE:
7232 /* Check the return type. */
7233 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7234 pfd->include_nondeduced_p))
7235 return error_mark_node;
7237 /* Check the parameter types. Since default arguments are not
7238 instantiated until they are needed, the TYPE_ARG_TYPES may
7239 contain expressions that involve template parameters. But,
7240 no-one should be looking at them yet. And, once they're
7241 instantiated, they don't contain template parameters, so
7242 there's no point in looking at them then, either. */
7244 tree parm;
7246 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7247 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7248 pfd->visited, pfd->include_nondeduced_p))
7249 return error_mark_node;
7251 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7252 want walk_tree walking into them itself. */
7253 *walk_subtrees = 0;
7255 break;
7257 case TYPEOF_TYPE:
7258 case UNDERLYING_TYPE:
7259 if (pfd->include_nondeduced_p
7260 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7261 pfd->visited,
7262 pfd->include_nondeduced_p))
7263 return error_mark_node;
7264 break;
7266 case FUNCTION_DECL:
7267 case VAR_DECL:
7268 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7269 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7270 pfd->visited, pfd->include_nondeduced_p))
7271 return error_mark_node;
7272 /* Fall through. */
7274 case PARM_DECL:
7275 case CONST_DECL:
7276 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7277 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7278 pfd->visited, pfd->include_nondeduced_p))
7279 return error_mark_node;
7280 if (DECL_CONTEXT (t)
7281 && pfd->include_nondeduced_p
7282 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7283 pfd->visited, pfd->include_nondeduced_p))
7284 return error_mark_node;
7285 break;
7287 case BOUND_TEMPLATE_TEMPLATE_PARM:
7288 /* Record template parameters such as `T' inside `TT<T>'. */
7289 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7290 pfd->include_nondeduced_p))
7291 return error_mark_node;
7292 /* Fall through. */
7294 case TEMPLATE_TEMPLATE_PARM:
7295 case TEMPLATE_TYPE_PARM:
7296 case TEMPLATE_PARM_INDEX:
7297 if (fn && (*fn)(t, data))
7298 return error_mark_node;
7299 else if (!fn)
7300 return error_mark_node;
7301 break;
7303 case TEMPLATE_DECL:
7304 /* A template template parameter is encountered. */
7305 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7306 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7307 pfd->include_nondeduced_p))
7308 return error_mark_node;
7310 /* Already substituted template template parameter */
7311 *walk_subtrees = 0;
7312 break;
7314 case TYPENAME_TYPE:
7315 if (!fn
7316 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7317 data, pfd->visited,
7318 pfd->include_nondeduced_p))
7319 return error_mark_node;
7320 break;
7322 case CONSTRUCTOR:
7323 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7324 && pfd->include_nondeduced_p
7325 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7326 (TREE_TYPE (t)), fn, data,
7327 pfd->visited, pfd->include_nondeduced_p))
7328 return error_mark_node;
7329 break;
7331 case INDIRECT_REF:
7332 case COMPONENT_REF:
7333 /* If there's no type, then this thing must be some expression
7334 involving template parameters. */
7335 if (!fn && !TREE_TYPE (t))
7336 return error_mark_node;
7337 break;
7339 case MODOP_EXPR:
7340 case CAST_EXPR:
7341 case REINTERPRET_CAST_EXPR:
7342 case CONST_CAST_EXPR:
7343 case STATIC_CAST_EXPR:
7344 case DYNAMIC_CAST_EXPR:
7345 case ARROW_EXPR:
7346 case DOTSTAR_EXPR:
7347 case TYPEID_EXPR:
7348 case PSEUDO_DTOR_EXPR:
7349 if (!fn)
7350 return error_mark_node;
7351 break;
7353 default:
7354 break;
7357 /* We didn't find any template parameters we liked. */
7358 return NULL_TREE;
7361 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7362 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7363 call FN with the parameter and the DATA.
7364 If FN returns nonzero, the iteration is terminated, and
7365 for_each_template_parm returns 1. Otherwise, the iteration
7366 continues. If FN never returns a nonzero value, the value
7367 returned by for_each_template_parm is 0. If FN is NULL, it is
7368 considered to be the function which always returns 1.
7370 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7371 parameters that occur in non-deduced contexts. When false, only
7372 visits those template parameters that can be deduced. */
7374 static int
7375 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7376 struct pointer_set_t *visited,
7377 bool include_nondeduced_p)
7379 struct pair_fn_data pfd;
7380 int result;
7382 /* Set up. */
7383 pfd.fn = fn;
7384 pfd.data = data;
7385 pfd.include_nondeduced_p = include_nondeduced_p;
7387 /* Walk the tree. (Conceptually, we would like to walk without
7388 duplicates, but for_each_template_parm_r recursively calls
7389 for_each_template_parm, so we would need to reorganize a fair
7390 bit to use walk_tree_without_duplicates, so we keep our own
7391 visited list.) */
7392 if (visited)
7393 pfd.visited = visited;
7394 else
7395 pfd.visited = pointer_set_create ();
7396 result = cp_walk_tree (&t,
7397 for_each_template_parm_r,
7398 &pfd,
7399 pfd.visited) != NULL_TREE;
7401 /* Clean up. */
7402 if (!visited)
7404 pointer_set_destroy (pfd.visited);
7405 pfd.visited = 0;
7408 return result;
7411 /* Returns true if T depends on any template parameter. */
7414 uses_template_parms (tree t)
7416 bool dependent_p;
7417 int saved_processing_template_decl;
7419 saved_processing_template_decl = processing_template_decl;
7420 if (!saved_processing_template_decl)
7421 processing_template_decl = 1;
7422 if (TYPE_P (t))
7423 dependent_p = dependent_type_p (t);
7424 else if (TREE_CODE (t) == TREE_VEC)
7425 dependent_p = any_dependent_template_arguments_p (t);
7426 else if (TREE_CODE (t) == TREE_LIST)
7427 dependent_p = (uses_template_parms (TREE_VALUE (t))
7428 || uses_template_parms (TREE_CHAIN (t)));
7429 else if (TREE_CODE (t) == TYPE_DECL)
7430 dependent_p = dependent_type_p (TREE_TYPE (t));
7431 else if (DECL_P (t)
7432 || EXPR_P (t)
7433 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7434 || TREE_CODE (t) == OVERLOAD
7435 || TREE_CODE (t) == BASELINK
7436 || TREE_CODE (t) == IDENTIFIER_NODE
7437 || TREE_CODE (t) == TRAIT_EXPR
7438 || TREE_CODE (t) == CONSTRUCTOR
7439 || CONSTANT_CLASS_P (t))
7440 dependent_p = (type_dependent_expression_p (t)
7441 || value_dependent_expression_p (t));
7442 else
7444 gcc_assert (t == error_mark_node);
7445 dependent_p = false;
7448 processing_template_decl = saved_processing_template_decl;
7450 return dependent_p;
7453 /* Returns true if T depends on any template parameter with level LEVEL. */
7456 uses_template_parms_level (tree t, int level)
7458 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7459 /*include_nondeduced_p=*/true);
7462 static int tinst_depth;
7463 extern int max_tinst_depth;
7464 #ifdef GATHER_STATISTICS
7465 int depth_reached;
7466 #endif
7467 static int tinst_level_tick;
7468 static int last_template_error_tick;
7470 /* We're starting to instantiate D; record the template instantiation context
7471 for diagnostics and to restore it later. */
7474 push_tinst_level (tree d)
7476 struct tinst_level *new_level;
7478 if (tinst_depth >= max_tinst_depth)
7480 /* If the instantiation in question still has unbound template parms,
7481 we don't really care if we can't instantiate it, so just return.
7482 This happens with base instantiation for implicit `typename'. */
7483 if (uses_template_parms (d))
7484 return 0;
7486 last_template_error_tick = tinst_level_tick;
7487 error ("template instantiation depth exceeds maximum of %d (use "
7488 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7489 max_tinst_depth, d);
7491 print_instantiation_context ();
7493 return 0;
7496 new_level = ggc_alloc_tinst_level ();
7497 new_level->decl = d;
7498 new_level->locus = input_location;
7499 new_level->in_system_header_p = in_system_header;
7500 new_level->next = current_tinst_level;
7501 current_tinst_level = new_level;
7503 ++tinst_depth;
7504 #ifdef GATHER_STATISTICS
7505 if (tinst_depth > depth_reached)
7506 depth_reached = tinst_depth;
7507 #endif
7509 ++tinst_level_tick;
7510 return 1;
7513 /* We're done instantiating this template; return to the instantiation
7514 context. */
7516 void
7517 pop_tinst_level (void)
7519 /* Restore the filename and line number stashed away when we started
7520 this instantiation. */
7521 input_location = current_tinst_level->locus;
7522 current_tinst_level = current_tinst_level->next;
7523 --tinst_depth;
7524 ++tinst_level_tick;
7527 /* We're instantiating a deferred template; restore the template
7528 instantiation context in which the instantiation was requested, which
7529 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7531 static tree
7532 reopen_tinst_level (struct tinst_level *level)
7534 struct tinst_level *t;
7536 tinst_depth = 0;
7537 for (t = level; t; t = t->next)
7538 ++tinst_depth;
7540 current_tinst_level = level;
7541 pop_tinst_level ();
7542 return level->decl;
7545 /* Returns the TINST_LEVEL which gives the original instantiation
7546 context. */
7548 struct tinst_level *
7549 outermost_tinst_level (void)
7551 struct tinst_level *level = current_tinst_level;
7552 if (level)
7553 while (level->next)
7554 level = level->next;
7555 return level;
7558 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7560 bool
7561 parameter_of_template_p (tree parm, tree templ)
7563 tree parms;
7564 int i;
7566 if (!parm || !templ)
7567 return false;
7569 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7570 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7572 parms = DECL_TEMPLATE_PARMS (templ);
7573 parms = INNERMOST_TEMPLATE_PARMS (parms);
7575 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7576 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7577 return true;
7579 return false;
7582 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7583 vector of template arguments, as for tsubst.
7585 Returns an appropriate tsubst'd friend declaration. */
7587 static tree
7588 tsubst_friend_function (tree decl, tree args)
7590 tree new_friend;
7592 if (TREE_CODE (decl) == FUNCTION_DECL
7593 && DECL_TEMPLATE_INSTANTIATION (decl)
7594 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7595 /* This was a friend declared with an explicit template
7596 argument list, e.g.:
7598 friend void f<>(T);
7600 to indicate that f was a template instantiation, not a new
7601 function declaration. Now, we have to figure out what
7602 instantiation of what template. */
7604 tree template_id, arglist, fns;
7605 tree new_args;
7606 tree tmpl;
7607 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7609 /* Friend functions are looked up in the containing namespace scope.
7610 We must enter that scope, to avoid finding member functions of the
7611 current class with same name. */
7612 push_nested_namespace (ns);
7613 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7614 tf_warning_or_error, NULL_TREE,
7615 /*integral_constant_expression_p=*/false);
7616 pop_nested_namespace (ns);
7617 arglist = tsubst (DECL_TI_ARGS (decl), args,
7618 tf_warning_or_error, NULL_TREE);
7619 template_id = lookup_template_function (fns, arglist);
7621 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7622 tmpl = determine_specialization (template_id, new_friend,
7623 &new_args,
7624 /*need_member_template=*/0,
7625 TREE_VEC_LENGTH (args),
7626 tsk_none);
7627 return instantiate_template (tmpl, new_args, tf_error);
7630 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7632 /* The NEW_FRIEND will look like an instantiation, to the
7633 compiler, but is not an instantiation from the point of view of
7634 the language. For example, we might have had:
7636 template <class T> struct S {
7637 template <class U> friend void f(T, U);
7640 Then, in S<int>, template <class U> void f(int, U) is not an
7641 instantiation of anything. */
7642 if (new_friend == error_mark_node)
7643 return error_mark_node;
7645 DECL_USE_TEMPLATE (new_friend) = 0;
7646 if (TREE_CODE (decl) == TEMPLATE_DECL)
7648 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7649 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7650 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7653 /* The mangled name for the NEW_FRIEND is incorrect. The function
7654 is not a template instantiation and should not be mangled like
7655 one. Therefore, we forget the mangling here; we'll recompute it
7656 later if we need it. */
7657 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7659 SET_DECL_RTL (new_friend, NULL);
7660 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7663 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7665 tree old_decl;
7666 tree new_friend_template_info;
7667 tree new_friend_result_template_info;
7668 tree ns;
7669 int new_friend_is_defn;
7671 /* We must save some information from NEW_FRIEND before calling
7672 duplicate decls since that function will free NEW_FRIEND if
7673 possible. */
7674 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7675 new_friend_is_defn =
7676 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7677 (template_for_substitution (new_friend)))
7678 != NULL_TREE);
7679 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7681 /* This declaration is a `primary' template. */
7682 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7684 new_friend_result_template_info
7685 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7687 else
7688 new_friend_result_template_info = NULL_TREE;
7690 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7691 if (new_friend_is_defn)
7692 DECL_INITIAL (new_friend) = error_mark_node;
7694 /* Inside pushdecl_namespace_level, we will push into the
7695 current namespace. However, the friend function should go
7696 into the namespace of the template. */
7697 ns = decl_namespace_context (new_friend);
7698 push_nested_namespace (ns);
7699 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7700 pop_nested_namespace (ns);
7702 if (old_decl == error_mark_node)
7703 return error_mark_node;
7705 if (old_decl != new_friend)
7707 /* This new friend declaration matched an existing
7708 declaration. For example, given:
7710 template <class T> void f(T);
7711 template <class U> class C {
7712 template <class T> friend void f(T) {}
7715 the friend declaration actually provides the definition
7716 of `f', once C has been instantiated for some type. So,
7717 old_decl will be the out-of-class template declaration,
7718 while new_friend is the in-class definition.
7720 But, if `f' was called before this point, the
7721 instantiation of `f' will have DECL_TI_ARGS corresponding
7722 to `T' but not to `U', references to which might appear
7723 in the definition of `f'. Previously, the most general
7724 template for an instantiation of `f' was the out-of-class
7725 version; now it is the in-class version. Therefore, we
7726 run through all specialization of `f', adding to their
7727 DECL_TI_ARGS appropriately. In particular, they need a
7728 new set of outer arguments, corresponding to the
7729 arguments for this class instantiation.
7731 The same situation can arise with something like this:
7733 friend void f(int);
7734 template <class T> class C {
7735 friend void f(T) {}
7738 when `C<int>' is instantiated. Now, `f(int)' is defined
7739 in the class. */
7741 if (!new_friend_is_defn)
7742 /* On the other hand, if the in-class declaration does
7743 *not* provide a definition, then we don't want to alter
7744 existing definitions. We can just leave everything
7745 alone. */
7747 else
7749 tree new_template = TI_TEMPLATE (new_friend_template_info);
7750 tree new_args = TI_ARGS (new_friend_template_info);
7752 /* Overwrite whatever template info was there before, if
7753 any, with the new template information pertaining to
7754 the declaration. */
7755 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7757 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7759 /* We should have called reregister_specialization in
7760 duplicate_decls. */
7761 gcc_assert (retrieve_specialization (new_template,
7762 new_args, 0)
7763 == old_decl);
7765 /* Instantiate it if the global has already been used. */
7766 if (DECL_ODR_USED (old_decl))
7767 instantiate_decl (old_decl, /*defer_ok=*/true,
7768 /*expl_inst_class_mem_p=*/false);
7770 else
7772 tree t;
7774 /* Indicate that the old function template is a partial
7775 instantiation. */
7776 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7777 = new_friend_result_template_info;
7779 gcc_assert (new_template
7780 == most_general_template (new_template));
7781 gcc_assert (new_template != old_decl);
7783 /* Reassign any specializations already in the hash table
7784 to the new more general template, and add the
7785 additional template args. */
7786 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7787 t != NULL_TREE;
7788 t = TREE_CHAIN (t))
7790 tree spec = TREE_VALUE (t);
7791 spec_entry elt;
7793 elt.tmpl = old_decl;
7794 elt.args = DECL_TI_ARGS (spec);
7795 elt.spec = NULL_TREE;
7797 htab_remove_elt (decl_specializations, &elt);
7799 DECL_TI_ARGS (spec)
7800 = add_outermost_template_args (new_args,
7801 DECL_TI_ARGS (spec));
7803 register_specialization
7804 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7807 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7811 /* The information from NEW_FRIEND has been merged into OLD_DECL
7812 by duplicate_decls. */
7813 new_friend = old_decl;
7816 else
7818 tree context = DECL_CONTEXT (new_friend);
7819 bool dependent_p;
7821 /* In the code
7822 template <class T> class C {
7823 template <class U> friend void C1<U>::f (); // case 1
7824 friend void C2<T>::f (); // case 2
7826 we only need to make sure CONTEXT is a complete type for
7827 case 2. To distinguish between the two cases, we note that
7828 CONTEXT of case 1 remains dependent type after tsubst while
7829 this isn't true for case 2. */
7830 ++processing_template_decl;
7831 dependent_p = dependent_type_p (context);
7832 --processing_template_decl;
7834 if (!dependent_p
7835 && !complete_type_or_else (context, NULL_TREE))
7836 return error_mark_node;
7838 if (COMPLETE_TYPE_P (context))
7840 /* Check to see that the declaration is really present, and,
7841 possibly obtain an improved declaration. */
7842 tree fn = check_classfn (context,
7843 new_friend, NULL_TREE);
7845 if (fn)
7846 new_friend = fn;
7850 return new_friend;
7853 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7854 template arguments, as for tsubst.
7856 Returns an appropriate tsubst'd friend type or error_mark_node on
7857 failure. */
7859 static tree
7860 tsubst_friend_class (tree friend_tmpl, tree args)
7862 tree friend_type;
7863 tree tmpl;
7864 tree context;
7866 context = CP_DECL_CONTEXT (friend_tmpl);
7868 if (context != global_namespace)
7870 if (TREE_CODE (context) == NAMESPACE_DECL)
7871 push_nested_namespace (context);
7872 else
7873 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7876 /* Look for a class template declaration. We look for hidden names
7877 because two friend declarations of the same template are the
7878 same. For example, in:
7880 struct A {
7881 template <typename> friend class F;
7883 template <typename> struct B {
7884 template <typename> friend class F;
7887 both F templates are the same. */
7888 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7889 /*block_p=*/true, 0,
7890 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7892 /* But, if we don't find one, it might be because we're in a
7893 situation like this:
7895 template <class T>
7896 struct S {
7897 template <class U>
7898 friend struct S;
7901 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7902 for `S<int>', not the TEMPLATE_DECL. */
7903 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7905 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7906 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7909 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7911 /* The friend template has already been declared. Just
7912 check to see that the declarations match, and install any new
7913 default parameters. We must tsubst the default parameters,
7914 of course. We only need the innermost template parameters
7915 because that is all that redeclare_class_template will look
7916 at. */
7917 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7918 > TMPL_ARGS_DEPTH (args))
7920 tree parms;
7921 location_t saved_input_location;
7922 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7923 args, tf_warning_or_error);
7925 saved_input_location = input_location;
7926 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7927 redeclare_class_template (TREE_TYPE (tmpl), parms);
7928 input_location = saved_input_location;
7932 friend_type = TREE_TYPE (tmpl);
7934 else
7936 /* The friend template has not already been declared. In this
7937 case, the instantiation of the template class will cause the
7938 injection of this template into the global scope. */
7939 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7940 if (tmpl == error_mark_node)
7941 return error_mark_node;
7943 /* The new TMPL is not an instantiation of anything, so we
7944 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7945 the new type because that is supposed to be the corresponding
7946 template decl, i.e., TMPL. */
7947 DECL_USE_TEMPLATE (tmpl) = 0;
7948 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7949 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7950 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7951 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7953 /* Inject this template into the global scope. */
7954 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7957 if (context != global_namespace)
7959 if (TREE_CODE (context) == NAMESPACE_DECL)
7960 pop_nested_namespace (context);
7961 else
7962 pop_nested_class ();
7965 return friend_type;
7968 /* Returns zero if TYPE cannot be completed later due to circularity.
7969 Otherwise returns one. */
7971 static int
7972 can_complete_type_without_circularity (tree type)
7974 if (type == NULL_TREE || type == error_mark_node)
7975 return 0;
7976 else if (COMPLETE_TYPE_P (type))
7977 return 1;
7978 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7979 return can_complete_type_without_circularity (TREE_TYPE (type));
7980 else if (CLASS_TYPE_P (type)
7981 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7982 return 0;
7983 else
7984 return 1;
7987 /* Apply any attributes which had to be deferred until instantiation
7988 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7989 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7991 static void
7992 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7993 tree args, tsubst_flags_t complain, tree in_decl)
7995 tree last_dep = NULL_TREE;
7996 tree t;
7997 tree *p;
7999 for (t = attributes; t; t = TREE_CHAIN (t))
8000 if (ATTR_IS_DEPENDENT (t))
8002 last_dep = t;
8003 attributes = copy_list (attributes);
8004 break;
8007 if (DECL_P (*decl_p))
8009 if (TREE_TYPE (*decl_p) == error_mark_node)
8010 return;
8011 p = &DECL_ATTRIBUTES (*decl_p);
8013 else
8014 p = &TYPE_ATTRIBUTES (*decl_p);
8016 if (last_dep)
8018 tree late_attrs = NULL_TREE;
8019 tree *q = &late_attrs;
8021 for (*p = attributes; *p; )
8023 t = *p;
8024 if (ATTR_IS_DEPENDENT (t))
8026 *p = TREE_CHAIN (t);
8027 TREE_CHAIN (t) = NULL_TREE;
8028 /* If the first attribute argument is an identifier, don't
8029 pass it through tsubst. Attributes like mode, format,
8030 cleanup and several target specific attributes expect it
8031 unmodified. */
8032 if (TREE_VALUE (t)
8033 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8034 && TREE_VALUE (TREE_VALUE (t))
8035 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8036 == IDENTIFIER_NODE))
8038 tree chain
8039 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8040 in_decl,
8041 /*integral_constant_expression_p=*/false);
8042 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8043 TREE_VALUE (t)
8044 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8045 chain);
8047 else
8048 TREE_VALUE (t)
8049 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8050 /*integral_constant_expression_p=*/false);
8051 *q = t;
8052 q = &TREE_CHAIN (t);
8054 else
8055 p = &TREE_CHAIN (t);
8058 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8062 /* Perform (or defer) access check for typedefs that were referenced
8063 from within the template TMPL code.
8064 This is a subroutine of instantiate_template and instantiate_class_template.
8065 TMPL is the template to consider and TARGS is the list of arguments of
8066 that template. */
8068 static void
8069 perform_typedefs_access_check (tree tmpl, tree targs)
8071 location_t saved_location;
8072 int i;
8073 qualified_typedef_usage_t *iter;
8075 if (!tmpl
8076 || (!CLASS_TYPE_P (tmpl)
8077 && TREE_CODE (tmpl) != FUNCTION_DECL))
8078 return;
8080 saved_location = input_location;
8081 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
8082 get_types_needing_access_check (tmpl),
8083 i, iter)
8085 tree type_decl = iter->typedef_decl;
8086 tree type_scope = iter->context;
8088 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8089 continue;
8091 if (uses_template_parms (type_decl))
8092 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8093 if (uses_template_parms (type_scope))
8094 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8096 /* Make access check error messages point to the location
8097 of the use of the typedef. */
8098 input_location = iter->locus;
8099 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8100 type_decl, type_decl);
8102 input_location = saved_location;
8105 tree
8106 instantiate_class_template (tree type)
8108 tree templ, args, pattern, t, member;
8109 tree typedecl;
8110 tree pbinfo;
8111 tree base_list;
8112 unsigned int saved_maximum_field_alignment;
8114 if (type == error_mark_node)
8115 return error_mark_node;
8117 if (COMPLETE_OR_OPEN_TYPE_P (type)
8118 || uses_template_parms (type))
8119 return type;
8121 /* Figure out which template is being instantiated. */
8122 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8123 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8125 /* Determine what specialization of the original template to
8126 instantiate. */
8127 t = most_specialized_class (type, templ, tf_warning_or_error);
8128 if (t == error_mark_node)
8130 TYPE_BEING_DEFINED (type) = 1;
8131 return error_mark_node;
8133 else if (t)
8135 /* This TYPE is actually an instantiation of a partial
8136 specialization. We replace the innermost set of ARGS with
8137 the arguments appropriate for substitution. For example,
8138 given:
8140 template <class T> struct S {};
8141 template <class T> struct S<T*> {};
8143 and supposing that we are instantiating S<int*>, ARGS will
8144 presently be {int*} -- but we need {int}. */
8145 pattern = TREE_TYPE (t);
8146 args = TREE_PURPOSE (t);
8148 else
8150 pattern = TREE_TYPE (templ);
8151 args = CLASSTYPE_TI_ARGS (type);
8154 /* If the template we're instantiating is incomplete, then clearly
8155 there's nothing we can do. */
8156 if (!COMPLETE_TYPE_P (pattern))
8157 return type;
8159 /* If we've recursively instantiated too many templates, stop. */
8160 if (! push_tinst_level (type))
8161 return type;
8163 /* Now we're really doing the instantiation. Mark the type as in
8164 the process of being defined. */
8165 TYPE_BEING_DEFINED (type) = 1;
8167 /* We may be in the middle of deferred access check. Disable
8168 it now. */
8169 push_deferring_access_checks (dk_no_deferred);
8171 push_to_top_level ();
8172 /* Use #pragma pack from the template context. */
8173 saved_maximum_field_alignment = maximum_field_alignment;
8174 maximum_field_alignment = TYPE_PRECISION (pattern);
8176 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8178 /* Set the input location to the most specialized template definition.
8179 This is needed if tsubsting causes an error. */
8180 typedecl = TYPE_MAIN_DECL (pattern);
8181 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8182 DECL_SOURCE_LOCATION (typedecl);
8184 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
8185 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
8186 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
8187 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
8188 TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern);
8189 TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern);
8190 TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern);
8191 TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern);
8192 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
8193 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
8194 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8195 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8196 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8197 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8198 if (ANON_AGGR_TYPE_P (pattern))
8199 SET_ANON_AGGR_TYPE_P (type);
8200 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8202 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8203 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8206 pbinfo = TYPE_BINFO (pattern);
8208 /* We should never instantiate a nested class before its enclosing
8209 class; we need to look up the nested class by name before we can
8210 instantiate it, and that lookup should instantiate the enclosing
8211 class. */
8212 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8213 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8215 base_list = NULL_TREE;
8216 if (BINFO_N_BASE_BINFOS (pbinfo))
8218 tree pbase_binfo;
8219 tree pushed_scope;
8220 int i;
8222 /* We must enter the scope containing the type, as that is where
8223 the accessibility of types named in dependent bases are
8224 looked up from. */
8225 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8227 /* Substitute into each of the bases to determine the actual
8228 basetypes. */
8229 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8231 tree base;
8232 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8233 tree expanded_bases = NULL_TREE;
8234 int idx, len = 1;
8236 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8238 expanded_bases =
8239 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8240 args, tf_error, NULL_TREE);
8241 if (expanded_bases == error_mark_node)
8242 continue;
8244 len = TREE_VEC_LENGTH (expanded_bases);
8247 for (idx = 0; idx < len; idx++)
8249 if (expanded_bases)
8250 /* Extract the already-expanded base class. */
8251 base = TREE_VEC_ELT (expanded_bases, idx);
8252 else
8253 /* Substitute to figure out the base class. */
8254 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8255 NULL_TREE);
8257 if (base == error_mark_node)
8258 continue;
8260 base_list = tree_cons (access, base, base_list);
8261 if (BINFO_VIRTUAL_P (pbase_binfo))
8262 TREE_TYPE (base_list) = integer_type_node;
8266 /* The list is now in reverse order; correct that. */
8267 base_list = nreverse (base_list);
8269 if (pushed_scope)
8270 pop_scope (pushed_scope);
8272 /* Now call xref_basetypes to set up all the base-class
8273 information. */
8274 xref_basetypes (type, base_list);
8276 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8277 (int) ATTR_FLAG_TYPE_IN_PLACE,
8278 args, tf_error, NULL_TREE);
8279 fixup_attribute_variants (type);
8281 /* Now that our base classes are set up, enter the scope of the
8282 class, so that name lookups into base classes, etc. will work
8283 correctly. This is precisely analogous to what we do in
8284 begin_class_definition when defining an ordinary non-template
8285 class, except we also need to push the enclosing classes. */
8286 push_nested_class (type);
8288 /* Now members are processed in the order of declaration. */
8289 for (member = CLASSTYPE_DECL_LIST (pattern);
8290 member; member = TREE_CHAIN (member))
8292 tree t = TREE_VALUE (member);
8294 if (TREE_PURPOSE (member))
8296 if (TYPE_P (t))
8298 /* Build new CLASSTYPE_NESTED_UTDS. */
8300 tree newtag;
8301 bool class_template_p;
8303 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8304 && TYPE_LANG_SPECIFIC (t)
8305 && CLASSTYPE_IS_TEMPLATE (t));
8306 /* If the member is a class template, then -- even after
8307 substitution -- there may be dependent types in the
8308 template argument list for the class. We increment
8309 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8310 that function will assume that no types are dependent
8311 when outside of a template. */
8312 if (class_template_p)
8313 ++processing_template_decl;
8314 newtag = tsubst (t, args, tf_error, NULL_TREE);
8315 if (class_template_p)
8316 --processing_template_decl;
8317 if (newtag == error_mark_node)
8318 continue;
8320 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8322 tree name = TYPE_IDENTIFIER (t);
8324 if (class_template_p)
8325 /* Unfortunately, lookup_template_class sets
8326 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8327 instantiation (i.e., for the type of a member
8328 template class nested within a template class.)
8329 This behavior is required for
8330 maybe_process_partial_specialization to work
8331 correctly, but is not accurate in this case;
8332 the TAG is not an instantiation of anything.
8333 (The corresponding TEMPLATE_DECL is an
8334 instantiation, but the TYPE is not.) */
8335 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8337 /* Now, we call pushtag to put this NEWTAG into the scope of
8338 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8339 pushtag calling push_template_decl. We don't have to do
8340 this for enums because it will already have been done in
8341 tsubst_enum. */
8342 if (name)
8343 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8344 pushtag (name, newtag, /*tag_scope=*/ts_current);
8347 else if (TREE_CODE (t) == FUNCTION_DECL
8348 || DECL_FUNCTION_TEMPLATE_P (t))
8350 /* Build new TYPE_METHODS. */
8351 tree r;
8353 if (TREE_CODE (t) == TEMPLATE_DECL)
8354 ++processing_template_decl;
8355 r = tsubst (t, args, tf_error, NULL_TREE);
8356 if (TREE_CODE (t) == TEMPLATE_DECL)
8357 --processing_template_decl;
8358 set_current_access_from_decl (r);
8359 finish_member_declaration (r);
8361 else
8363 /* Build new TYPE_FIELDS. */
8364 if (TREE_CODE (t) == STATIC_ASSERT)
8366 tree condition =
8367 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8368 tf_warning_or_error, NULL_TREE,
8369 /*integral_constant_expression_p=*/true);
8370 finish_static_assert (condition,
8371 STATIC_ASSERT_MESSAGE (t),
8372 STATIC_ASSERT_SOURCE_LOCATION (t),
8373 /*member_p=*/true);
8375 else if (TREE_CODE (t) != CONST_DECL)
8377 tree r;
8379 /* The file and line for this declaration, to
8380 assist in error message reporting. Since we
8381 called push_tinst_level above, we don't need to
8382 restore these. */
8383 input_location = DECL_SOURCE_LOCATION (t);
8385 if (TREE_CODE (t) == TEMPLATE_DECL)
8386 ++processing_template_decl;
8387 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8388 if (TREE_CODE (t) == TEMPLATE_DECL)
8389 --processing_template_decl;
8390 if (TREE_CODE (r) == VAR_DECL)
8392 /* In [temp.inst]:
8394 [t]he initialization (and any associated
8395 side-effects) of a static data member does
8396 not occur unless the static data member is
8397 itself used in a way that requires the
8398 definition of the static data member to
8399 exist.
8401 Therefore, we do not substitute into the
8402 initialized for the static data member here. */
8403 finish_static_data_member_decl
8405 /*init=*/NULL_TREE,
8406 /*init_const_expr_p=*/false,
8407 /*asmspec_tree=*/NULL_TREE,
8408 /*flags=*/0);
8409 if (DECL_INITIALIZED_IN_CLASS_P (r))
8410 check_static_variable_definition (r, TREE_TYPE (r));
8412 else if (TREE_CODE (r) == FIELD_DECL)
8414 /* Determine whether R has a valid type and can be
8415 completed later. If R is invalid, then it is
8416 replaced by error_mark_node so that it will not be
8417 added to TYPE_FIELDS. */
8418 tree rtype = TREE_TYPE (r);
8419 if (can_complete_type_without_circularity (rtype))
8420 complete_type (rtype);
8422 if (!COMPLETE_TYPE_P (rtype))
8424 cxx_incomplete_type_error (r, rtype);
8425 r = error_mark_node;
8429 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8430 such a thing will already have been added to the field
8431 list by tsubst_enum in finish_member_declaration in the
8432 CLASSTYPE_NESTED_UTDS case above. */
8433 if (!(TREE_CODE (r) == TYPE_DECL
8434 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8435 && DECL_ARTIFICIAL (r)))
8437 set_current_access_from_decl (r);
8438 finish_member_declaration (r);
8443 else
8445 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8447 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8449 tree friend_type = t;
8450 bool adjust_processing_template_decl = false;
8452 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8454 /* template <class T> friend class C; */
8455 friend_type = tsubst_friend_class (friend_type, args);
8456 adjust_processing_template_decl = true;
8458 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8460 /* template <class T> friend class C::D; */
8461 friend_type = tsubst (friend_type, args,
8462 tf_warning_or_error, NULL_TREE);
8463 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8464 friend_type = TREE_TYPE (friend_type);
8465 adjust_processing_template_decl = true;
8467 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
8469 /* This could be either
8471 friend class T::C;
8473 when dependent_type_p is false or
8475 template <class U> friend class T::C;
8477 otherwise. */
8478 friend_type = tsubst (friend_type, args,
8479 tf_warning_or_error, NULL_TREE);
8480 /* Bump processing_template_decl for correct
8481 dependent_type_p calculation. */
8482 ++processing_template_decl;
8483 if (dependent_type_p (friend_type))
8484 adjust_processing_template_decl = true;
8485 --processing_template_decl;
8487 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8488 && hidden_name_p (TYPE_NAME (friend_type)))
8490 /* friend class C;
8492 where C hasn't been declared yet. Let's lookup name
8493 from namespace scope directly, bypassing any name that
8494 come from dependent base class. */
8495 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8497 /* The call to xref_tag_from_type does injection for friend
8498 classes. */
8499 push_nested_namespace (ns);
8500 friend_type =
8501 xref_tag_from_type (friend_type, NULL_TREE,
8502 /*tag_scope=*/ts_current);
8503 pop_nested_namespace (ns);
8505 else if (uses_template_parms (friend_type))
8506 /* friend class C<T>; */
8507 friend_type = tsubst (friend_type, args,
8508 tf_warning_or_error, NULL_TREE);
8509 /* Otherwise it's
8511 friend class C;
8513 where C is already declared or
8515 friend class C<int>;
8517 We don't have to do anything in these cases. */
8519 if (adjust_processing_template_decl)
8520 /* Trick make_friend_class into realizing that the friend
8521 we're adding is a template, not an ordinary class. It's
8522 important that we use make_friend_class since it will
8523 perform some error-checking and output cross-reference
8524 information. */
8525 ++processing_template_decl;
8527 if (friend_type != error_mark_node)
8528 make_friend_class (type, friend_type, /*complain=*/false);
8530 if (adjust_processing_template_decl)
8531 --processing_template_decl;
8533 else
8535 /* Build new DECL_FRIENDLIST. */
8536 tree r;
8538 /* The file and line for this declaration, to
8539 assist in error message reporting. Since we
8540 called push_tinst_level above, we don't need to
8541 restore these. */
8542 input_location = DECL_SOURCE_LOCATION (t);
8544 if (TREE_CODE (t) == TEMPLATE_DECL)
8546 ++processing_template_decl;
8547 push_deferring_access_checks (dk_no_check);
8550 r = tsubst_friend_function (t, args);
8551 add_friend (type, r, /*complain=*/false);
8552 if (TREE_CODE (t) == TEMPLATE_DECL)
8554 pop_deferring_access_checks ();
8555 --processing_template_decl;
8561 /* Set the file and line number information to whatever is given for
8562 the class itself. This puts error messages involving generated
8563 implicit functions at a predictable point, and the same point
8564 that would be used for non-template classes. */
8565 input_location = DECL_SOURCE_LOCATION (typedecl);
8567 unreverse_member_declarations (type);
8568 finish_struct_1 (type);
8569 TYPE_BEING_DEFINED (type) = 0;
8571 /* We don't instantiate default arguments for member functions. 14.7.1:
8573 The implicit instantiation of a class template specialization causes
8574 the implicit instantiation of the declarations, but not of the
8575 definitions or default arguments, of the class member functions,
8576 member classes, static data members and member templates.... */
8578 /* Some typedefs referenced from within the template code need to be access
8579 checked at template instantiation time, i.e now. These types were
8580 added to the template at parsing time. Let's get those and perform
8581 the access checks then. */
8582 perform_typedefs_access_check (pattern, args);
8583 perform_deferred_access_checks ();
8584 pop_nested_class ();
8585 maximum_field_alignment = saved_maximum_field_alignment;
8586 pop_from_top_level ();
8587 pop_deferring_access_checks ();
8588 pop_tinst_level ();
8590 /* The vtable for a template class can be emitted in any translation
8591 unit in which the class is instantiated. When there is no key
8592 method, however, finish_struct_1 will already have added TYPE to
8593 the keyed_classes list. */
8594 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8595 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8597 return type;
8600 static tree
8601 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8603 tree r;
8605 if (!t)
8606 r = t;
8607 else if (TYPE_P (t))
8608 r = tsubst (t, args, complain, in_decl);
8609 else
8611 if (!(complain & tf_warning))
8612 ++c_inhibit_evaluation_warnings;
8613 r = tsubst_expr (t, args, complain, in_decl,
8614 /*integral_constant_expression_p=*/true);
8615 if (!(complain & tf_warning))
8616 --c_inhibit_evaluation_warnings;
8618 return r;
8621 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8622 NONTYPE_ARGUMENT_PACK. */
8624 static tree
8625 make_fnparm_pack (tree spec_parm)
8627 /* Collect all of the extra "packed" parameters into an
8628 argument pack. */
8629 tree parmvec;
8630 tree parmtypevec;
8631 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8632 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8633 int i, len = list_length (spec_parm);
8635 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8636 parmvec = make_tree_vec (len);
8637 parmtypevec = make_tree_vec (len);
8638 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8640 TREE_VEC_ELT (parmvec, i) = spec_parm;
8641 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8644 /* Build the argument packs. */
8645 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8646 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8647 TREE_TYPE (argpack) = argtypepack;
8649 return argpack;
8652 /* Substitute ARGS into T, which is an pack expansion
8653 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8654 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8655 (if only a partial substitution could be performed) or
8656 ERROR_MARK_NODE if there was an error. */
8657 tree
8658 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8659 tree in_decl)
8661 tree pattern;
8662 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8663 int i, len = -1;
8664 tree result;
8665 int incomplete = 0;
8666 htab_t saved_local_specializations = NULL;
8668 gcc_assert (PACK_EXPANSION_P (t));
8669 pattern = PACK_EXPANSION_PATTERN (t);
8671 /* Determine the argument packs that will instantiate the parameter
8672 packs used in the expansion expression. While we're at it,
8673 compute the number of arguments to be expanded and make sure it
8674 is consistent. */
8675 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8676 pack = TREE_CHAIN (pack))
8678 tree parm_pack = TREE_VALUE (pack);
8679 tree arg_pack = NULL_TREE;
8680 tree orig_arg = NULL_TREE;
8682 if (TREE_CODE (parm_pack) == PARM_DECL)
8684 if (!cp_unevaluated_operand)
8685 arg_pack = retrieve_local_specialization (parm_pack);
8686 else
8688 /* We can't rely on local_specializations for a parameter
8689 name used later in a function declaration (such as in a
8690 late-specified return type). Even if it exists, it might
8691 have the wrong value for a recursive call. Just make a
8692 dummy decl, since it's only used for its type. */
8693 arg_pack = tsubst_decl (parm_pack, args, complain);
8694 arg_pack = make_fnparm_pack (arg_pack);
8697 else
8699 int level, idx, levels;
8700 template_parm_level_and_index (parm_pack, &level, &idx);
8702 levels = TMPL_ARGS_DEPTH (args);
8703 if (level <= levels)
8704 arg_pack = TMPL_ARG (args, level, idx);
8707 orig_arg = arg_pack;
8708 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8709 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8711 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8712 /* This can only happen if we forget to expand an argument
8713 pack somewhere else. Just return an error, silently. */
8715 result = make_tree_vec (1);
8716 TREE_VEC_ELT (result, 0) = error_mark_node;
8717 return result;
8720 /* For clarity in the comments below let's use the
8721 representation 'argument_pack<elements>' to denote an
8722 argument pack and its elements.
8724 In the 'if' block below, we want to detect cases where
8725 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
8726 check if ARG_PACK is an argument pack which sole element is
8727 the expansion of PARM_PACK. That argument pack is typically
8728 created by template_parm_to_arg when passed a parameter
8729 pack. */
8730 if (arg_pack
8731 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8732 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8734 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8735 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8736 /* So we have an argument_pack<P...>. We want to test if P
8737 is actually PARM_PACK. We will not use cp_tree_equal to
8738 test P and PARM_PACK because during type fixup (by
8739 fixup_template_parm) P can be a pre-fixup version of a
8740 type and PARM_PACK be its post-fixup version.
8741 cp_tree_equal would consider them as different even
8742 though we would want to consider them compatible for our
8743 precise purpose here.
8745 Thus we are going to consider that P and PARM_PACK are
8746 compatible if they have the same DECL. */
8747 if ((/* If ARG_PACK is a type parameter pack named by the
8748 same DECL as parm_pack ... */
8749 (TYPE_P (pattern)
8750 && TYPE_P (parm_pack)
8751 && TYPE_NAME (pattern) == TYPE_NAME (parm_pack))
8752 /* ... or if ARG_PACK is a non-type parameter
8753 named by the same DECL as parm_pack ... */
8754 || (TREE_CODE (pattern) == TEMPLATE_PARM_INDEX
8755 && TREE_CODE (parm_pack) == PARM_DECL
8756 && TEMPLATE_PARM_DECL (pattern)
8757 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack))))
8758 && template_parameter_pack_p (pattern))
8759 /* ... then the argument pack that the parameter maps to
8760 is just an expansion of the parameter itself, such as
8761 one would find in the implicit typedef of a class
8762 inside the class itself. Consider this parameter
8763 "unsubstituted", so that we will maintain the outer
8764 pack expansion. */
8765 arg_pack = NULL_TREE;
8768 if (arg_pack)
8770 int my_len =
8771 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8773 /* It's all-or-nothing with incomplete argument packs. */
8774 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8775 return error_mark_node;
8777 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8778 incomplete = 1;
8780 if (len < 0)
8781 len = my_len;
8782 else if (len != my_len)
8784 if (incomplete)
8785 /* We got explicit args for some packs but not others;
8786 do nothing now and try again after deduction. */
8787 return t;
8788 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8789 error ("mismatched argument pack lengths while expanding "
8790 "%<%T%>",
8791 pattern);
8792 else
8793 error ("mismatched argument pack lengths while expanding "
8794 "%<%E%>",
8795 pattern);
8796 return error_mark_node;
8799 /* Keep track of the parameter packs and their corresponding
8800 argument packs. */
8801 packs = tree_cons (parm_pack, arg_pack, packs);
8802 TREE_TYPE (packs) = orig_arg;
8804 else
8805 /* We can't substitute for this parameter pack. */
8806 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8807 TREE_VALUE (pack),
8808 unsubstituted_packs);
8811 /* We cannot expand this expansion expression, because we don't have
8812 all of the argument packs we need. Substitute into the pattern
8813 and return a PACK_EXPANSION_*. The caller will need to deal with
8814 that. */
8815 if (unsubstituted_packs)
8817 tree new_pat;
8818 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8819 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8820 /*integral_constant_expression_p=*/false);
8821 else
8822 new_pat = tsubst (pattern, args, complain, in_decl);
8823 return make_pack_expansion (new_pat);
8826 /* We could not find any argument packs that work. */
8827 if (len < 0)
8828 return error_mark_node;
8830 if (cp_unevaluated_operand)
8832 /* We're in a late-specified return type, so create our own local
8833 specializations table; the current table is either NULL or (in the
8834 case of recursive unification) might have bindings that we don't
8835 want to use or alter. */
8836 saved_local_specializations = local_specializations;
8837 local_specializations = htab_create (37,
8838 hash_local_specialization,
8839 eq_local_specializations,
8840 NULL);
8843 /* For each argument in each argument pack, substitute into the
8844 pattern. */
8845 result = make_tree_vec (len + incomplete);
8846 for (i = 0; i < len + incomplete; ++i)
8848 /* For parameter pack, change the substitution of the parameter
8849 pack to the ith argument in its argument pack, then expand
8850 the pattern. */
8851 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8853 tree parm = TREE_PURPOSE (pack);
8855 if (TREE_CODE (parm) == PARM_DECL)
8857 /* Select the Ith argument from the pack. */
8858 tree arg = make_node (ARGUMENT_PACK_SELECT);
8859 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8860 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8861 mark_used (parm);
8862 register_local_specialization (arg, parm);
8864 else
8866 tree value = parm;
8867 int idx, level;
8868 template_parm_level_and_index (parm, &level, &idx);
8870 if (i < len)
8872 /* Select the Ith argument from the pack. */
8873 value = make_node (ARGUMENT_PACK_SELECT);
8874 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8875 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8878 /* Update the corresponding argument. */
8879 TMPL_ARG (args, level, idx) = value;
8883 /* Substitute into the PATTERN with the altered arguments. */
8884 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8885 TREE_VEC_ELT (result, i) =
8886 tsubst_expr (pattern, args, complain, in_decl,
8887 /*integral_constant_expression_p=*/false);
8888 else
8889 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8891 if (i == len)
8892 /* When we have incomplete argument packs, the last "expanded"
8893 result is itself a pack expansion, which allows us
8894 to deduce more arguments. */
8895 TREE_VEC_ELT (result, i) =
8896 make_pack_expansion (TREE_VEC_ELT (result, i));
8898 if (TREE_VEC_ELT (result, i) == error_mark_node)
8900 result = error_mark_node;
8901 break;
8905 /* Update ARGS to restore the substitution from parameter packs to
8906 their argument packs. */
8907 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8909 tree parm = TREE_PURPOSE (pack);
8911 if (TREE_CODE (parm) == PARM_DECL)
8912 register_local_specialization (TREE_TYPE (pack), parm);
8913 else
8915 int idx, level;
8916 template_parm_level_and_index (parm, &level, &idx);
8918 /* Update the corresponding argument. */
8919 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8920 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8921 TREE_TYPE (pack);
8922 else
8923 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8927 if (saved_local_specializations)
8929 htab_delete (local_specializations);
8930 local_specializations = saved_local_specializations;
8933 return result;
8936 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8937 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8938 parameter packs; all parms generated from a function parameter pack will
8939 have the same DECL_PARM_INDEX. */
8941 tree
8942 get_pattern_parm (tree parm, tree tmpl)
8944 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8945 tree patparm;
8947 if (DECL_ARTIFICIAL (parm))
8949 for (patparm = DECL_ARGUMENTS (pattern);
8950 patparm; patparm = DECL_CHAIN (patparm))
8951 if (DECL_ARTIFICIAL (patparm)
8952 && DECL_NAME (parm) == DECL_NAME (patparm))
8953 break;
8955 else
8957 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8958 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8959 gcc_assert (DECL_PARM_INDEX (patparm)
8960 == DECL_PARM_INDEX (parm));
8963 return patparm;
8966 /* Substitute ARGS into the vector or list of template arguments T. */
8968 static tree
8969 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8971 tree orig_t = t;
8972 int len, need_new = 0, i, expanded_len_adjust = 0, out;
8973 tree *elts;
8975 if (t == error_mark_node)
8976 return error_mark_node;
8978 len = TREE_VEC_LENGTH (t);
8979 elts = XALLOCAVEC (tree, len);
8981 for (i = 0; i < len; i++)
8983 tree orig_arg = TREE_VEC_ELT (t, i);
8984 tree new_arg;
8986 if (TREE_CODE (orig_arg) == TREE_VEC)
8987 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8988 else if (PACK_EXPANSION_P (orig_arg))
8990 /* Substitute into an expansion expression. */
8991 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8993 if (TREE_CODE (new_arg) == TREE_VEC)
8994 /* Add to the expanded length adjustment the number of
8995 expanded arguments. We subtract one from this
8996 measurement, because the argument pack expression
8997 itself is already counted as 1 in
8998 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8999 the argument pack is empty. */
9000 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9002 else if (ARGUMENT_PACK_P (orig_arg))
9004 /* Substitute into each of the arguments. */
9005 new_arg = TYPE_P (orig_arg)
9006 ? cxx_make_type (TREE_CODE (orig_arg))
9007 : make_node (TREE_CODE (orig_arg));
9009 SET_ARGUMENT_PACK_ARGS (
9010 new_arg,
9011 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9012 args, complain, in_decl));
9014 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9015 new_arg = error_mark_node;
9017 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9018 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9019 complain, in_decl);
9020 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9022 if (TREE_TYPE (new_arg) == error_mark_node)
9023 new_arg = error_mark_node;
9026 else
9027 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9029 if (new_arg == error_mark_node)
9030 return error_mark_node;
9032 elts[i] = new_arg;
9033 if (new_arg != orig_arg)
9034 need_new = 1;
9037 if (!need_new)
9038 return t;
9040 /* Make space for the expanded arguments coming from template
9041 argument packs. */
9042 t = make_tree_vec (len + expanded_len_adjust);
9043 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9044 arguments for a member template.
9045 In that case each TREE_VEC in ORIG_T represents a level of template
9046 arguments, and ORIG_T won't carry any non defaulted argument count.
9047 It will rather be the nested TREE_VECs that will carry one.
9048 In other words, ORIG_T carries a non defaulted argument count only
9049 if it doesn't contain any nested TREE_VEC. */
9050 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9052 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9053 count += expanded_len_adjust;
9054 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9056 for (i = 0, out = 0; i < len; i++)
9058 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9059 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9060 && TREE_CODE (elts[i]) == TREE_VEC)
9062 int idx;
9064 /* Now expand the template argument pack "in place". */
9065 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9066 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9068 else
9070 TREE_VEC_ELT (t, out) = elts[i];
9071 out++;
9075 return t;
9078 /* Return the result of substituting ARGS into the template parameters
9079 given by PARMS. If there are m levels of ARGS and m + n levels of
9080 PARMS, then the result will contain n levels of PARMS. For
9081 example, if PARMS is `template <class T> template <class U>
9082 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9083 result will be `template <int*, double, class V>'. */
9085 static tree
9086 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9088 tree r = NULL_TREE;
9089 tree* new_parms;
9091 /* When substituting into a template, we must set
9092 PROCESSING_TEMPLATE_DECL as the template parameters may be
9093 dependent if they are based on one-another, and the dependency
9094 predicates are short-circuit outside of templates. */
9095 ++processing_template_decl;
9097 for (new_parms = &r;
9098 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9099 new_parms = &(TREE_CHAIN (*new_parms)),
9100 parms = TREE_CHAIN (parms))
9102 tree new_vec =
9103 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9104 int i;
9106 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9108 tree tuple;
9110 if (parms == error_mark_node)
9111 continue;
9113 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9115 if (tuple == error_mark_node)
9116 continue;
9118 TREE_VEC_ELT (new_vec, i) =
9119 tsubst_template_parm (tuple, args, complain);
9122 *new_parms =
9123 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9124 - TMPL_ARGS_DEPTH (args)),
9125 new_vec, NULL_TREE);
9128 --processing_template_decl;
9130 return r;
9133 /* Return the result of substituting ARGS into one template parameter
9134 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9135 parameter and which TREE_PURPOSE is the default argument of the
9136 template parameter. */
9138 static tree
9139 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9141 tree default_value, parm_decl;
9143 if (args == NULL_TREE
9144 || t == NULL_TREE
9145 || t == error_mark_node)
9146 return t;
9148 gcc_assert (TREE_CODE (t) == TREE_LIST);
9150 default_value = TREE_PURPOSE (t);
9151 parm_decl = TREE_VALUE (t);
9153 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9154 if (TREE_CODE (parm_decl) == PARM_DECL
9155 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9156 parm_decl = error_mark_node;
9157 default_value = tsubst_template_arg (default_value, args,
9158 complain, NULL_TREE);
9160 return build_tree_list (default_value, parm_decl);
9163 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9164 type T. If T is not an aggregate or enumeration type, it is
9165 handled as if by tsubst. IN_DECL is as for tsubst. If
9166 ENTERING_SCOPE is nonzero, T is the context for a template which
9167 we are presently tsubst'ing. Return the substituted value. */
9169 static tree
9170 tsubst_aggr_type (tree t,
9171 tree args,
9172 tsubst_flags_t complain,
9173 tree in_decl,
9174 int entering_scope)
9176 if (t == NULL_TREE)
9177 return NULL_TREE;
9179 switch (TREE_CODE (t))
9181 case RECORD_TYPE:
9182 if (TYPE_PTRMEMFUNC_P (t))
9183 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9185 /* Else fall through. */
9186 case ENUMERAL_TYPE:
9187 case UNION_TYPE:
9188 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9190 tree argvec;
9191 tree context;
9192 tree r;
9193 int saved_unevaluated_operand;
9194 int saved_inhibit_evaluation_warnings;
9196 /* In "sizeof(X<I>)" we need to evaluate "I". */
9197 saved_unevaluated_operand = cp_unevaluated_operand;
9198 cp_unevaluated_operand = 0;
9199 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9200 c_inhibit_evaluation_warnings = 0;
9202 /* First, determine the context for the type we are looking
9203 up. */
9204 context = TYPE_CONTEXT (t);
9205 if (context)
9207 context = tsubst_aggr_type (context, args, complain,
9208 in_decl, /*entering_scope=*/1);
9209 /* If context is a nested class inside a class template,
9210 it may still need to be instantiated (c++/33959). */
9211 if (TYPE_P (context))
9212 context = complete_type (context);
9215 /* Then, figure out what arguments are appropriate for the
9216 type we are trying to find. For example, given:
9218 template <class T> struct S;
9219 template <class T, class U> void f(T, U) { S<U> su; }
9221 and supposing that we are instantiating f<int, double>,
9222 then our ARGS will be {int, double}, but, when looking up
9223 S we only want {double}. */
9224 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9225 complain, in_decl);
9226 if (argvec == error_mark_node)
9227 r = error_mark_node;
9228 else
9230 r = lookup_template_class (t, argvec, in_decl, context,
9231 entering_scope, complain);
9232 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9235 cp_unevaluated_operand = saved_unevaluated_operand;
9236 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9238 return r;
9240 else
9241 /* This is not a template type, so there's nothing to do. */
9242 return t;
9244 default:
9245 return tsubst (t, args, complain, in_decl);
9249 /* Substitute into the default argument ARG (a default argument for
9250 FN), which has the indicated TYPE. */
9252 tree
9253 tsubst_default_argument (tree fn, tree type, tree arg)
9255 tree saved_class_ptr = NULL_TREE;
9256 tree saved_class_ref = NULL_TREE;
9258 /* This can happen in invalid code. */
9259 if (TREE_CODE (arg) == DEFAULT_ARG)
9260 return arg;
9262 /* This default argument came from a template. Instantiate the
9263 default argument here, not in tsubst. In the case of
9264 something like:
9266 template <class T>
9267 struct S {
9268 static T t();
9269 void f(T = t());
9272 we must be careful to do name lookup in the scope of S<T>,
9273 rather than in the current class. */
9274 push_access_scope (fn);
9275 /* The "this" pointer is not valid in a default argument. */
9276 if (cfun)
9278 saved_class_ptr = current_class_ptr;
9279 cp_function_chain->x_current_class_ptr = NULL_TREE;
9280 saved_class_ref = current_class_ref;
9281 cp_function_chain->x_current_class_ref = NULL_TREE;
9284 push_deferring_access_checks(dk_no_deferred);
9285 /* The default argument expression may cause implicitly defined
9286 member functions to be synthesized, which will result in garbage
9287 collection. We must treat this situation as if we were within
9288 the body of function so as to avoid collecting live data on the
9289 stack. */
9290 ++function_depth;
9291 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9292 tf_warning_or_error, NULL_TREE,
9293 /*integral_constant_expression_p=*/false);
9294 --function_depth;
9295 pop_deferring_access_checks();
9297 /* Restore the "this" pointer. */
9298 if (cfun)
9300 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9301 cp_function_chain->x_current_class_ref = saved_class_ref;
9304 /* Make sure the default argument is reasonable. */
9305 arg = check_default_argument (type, arg);
9307 pop_access_scope (fn);
9309 return arg;
9312 /* Substitute into all the default arguments for FN. */
9314 static void
9315 tsubst_default_arguments (tree fn)
9317 tree arg;
9318 tree tmpl_args;
9320 tmpl_args = DECL_TI_ARGS (fn);
9322 /* If this function is not yet instantiated, we certainly don't need
9323 its default arguments. */
9324 if (uses_template_parms (tmpl_args))
9325 return;
9326 /* Don't do this again for clones. */
9327 if (DECL_CLONED_FUNCTION_P (fn))
9328 return;
9330 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9331 arg;
9332 arg = TREE_CHAIN (arg))
9333 if (TREE_PURPOSE (arg))
9334 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9335 TREE_VALUE (arg),
9336 TREE_PURPOSE (arg));
9339 /* Substitute the ARGS into the T, which is a _DECL. Return the
9340 result of the substitution. Issue error and warning messages under
9341 control of COMPLAIN. */
9343 static tree
9344 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9346 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9347 location_t saved_loc;
9348 tree r = NULL_TREE;
9349 tree in_decl = t;
9350 hashval_t hash = 0;
9352 /* Set the filename and linenumber to improve error-reporting. */
9353 saved_loc = input_location;
9354 input_location = DECL_SOURCE_LOCATION (t);
9356 switch (TREE_CODE (t))
9358 case TEMPLATE_DECL:
9360 /* We can get here when processing a member function template,
9361 member class template, or template template parameter. */
9362 tree decl = DECL_TEMPLATE_RESULT (t);
9363 tree spec;
9364 tree tmpl_args;
9365 tree full_args;
9367 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9369 /* Template template parameter is treated here. */
9370 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9371 if (new_type == error_mark_node)
9372 RETURN (error_mark_node);
9374 r = copy_decl (t);
9375 DECL_CHAIN (r) = NULL_TREE;
9376 TREE_TYPE (r) = new_type;
9377 DECL_TEMPLATE_RESULT (r)
9378 = build_decl (DECL_SOURCE_LOCATION (decl),
9379 TYPE_DECL, DECL_NAME (decl), new_type);
9380 DECL_TEMPLATE_PARMS (r)
9381 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9382 complain);
9383 TYPE_NAME (new_type) = r;
9384 break;
9387 /* We might already have an instance of this template.
9388 The ARGS are for the surrounding class type, so the
9389 full args contain the tsubst'd args for the context,
9390 plus the innermost args from the template decl. */
9391 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9392 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9393 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9394 /* Because this is a template, the arguments will still be
9395 dependent, even after substitution. If
9396 PROCESSING_TEMPLATE_DECL is not set, the dependency
9397 predicates will short-circuit. */
9398 ++processing_template_decl;
9399 full_args = tsubst_template_args (tmpl_args, args,
9400 complain, in_decl);
9401 --processing_template_decl;
9402 if (full_args == error_mark_node)
9403 RETURN (error_mark_node);
9405 /* If this is a default template template argument,
9406 tsubst might not have changed anything. */
9407 if (full_args == tmpl_args)
9408 RETURN (t);
9410 hash = hash_tmpl_and_args (t, full_args);
9411 spec = retrieve_specialization (t, full_args, hash);
9412 if (spec != NULL_TREE)
9414 r = spec;
9415 break;
9418 /* Make a new template decl. It will be similar to the
9419 original, but will record the current template arguments.
9420 We also create a new function declaration, which is just
9421 like the old one, but points to this new template, rather
9422 than the old one. */
9423 r = copy_decl (t);
9424 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9425 DECL_CHAIN (r) = NULL_TREE;
9427 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9429 if (TREE_CODE (decl) == TYPE_DECL)
9431 tree new_type;
9432 ++processing_template_decl;
9433 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9434 --processing_template_decl;
9435 if (new_type == error_mark_node)
9436 RETURN (error_mark_node);
9438 TREE_TYPE (r) = new_type;
9439 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9440 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9441 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9442 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9444 else
9446 tree new_decl;
9447 ++processing_template_decl;
9448 new_decl = tsubst (decl, args, complain, in_decl);
9449 --processing_template_decl;
9450 if (new_decl == error_mark_node)
9451 RETURN (error_mark_node);
9453 DECL_TEMPLATE_RESULT (r) = new_decl;
9454 DECL_TI_TEMPLATE (new_decl) = r;
9455 TREE_TYPE (r) = TREE_TYPE (new_decl);
9456 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9457 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9460 SET_DECL_IMPLICIT_INSTANTIATION (r);
9461 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9462 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9464 /* The template parameters for this new template are all the
9465 template parameters for the old template, except the
9466 outermost level of parameters. */
9467 DECL_TEMPLATE_PARMS (r)
9468 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9469 complain);
9471 if (PRIMARY_TEMPLATE_P (t))
9472 DECL_PRIMARY_TEMPLATE (r) = r;
9474 if (TREE_CODE (decl) != TYPE_DECL)
9475 /* Record this non-type partial instantiation. */
9476 register_specialization (r, t,
9477 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9478 false, hash);
9480 break;
9482 case FUNCTION_DECL:
9484 tree ctx;
9485 tree argvec = NULL_TREE;
9486 tree *friends;
9487 tree gen_tmpl;
9488 tree type;
9489 int member;
9490 int args_depth;
9491 int parms_depth;
9493 /* Nobody should be tsubst'ing into non-template functions. */
9494 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9496 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9498 tree spec;
9499 bool dependent_p;
9501 /* If T is not dependent, just return it. We have to
9502 increment PROCESSING_TEMPLATE_DECL because
9503 value_dependent_expression_p assumes that nothing is
9504 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9505 ++processing_template_decl;
9506 dependent_p = value_dependent_expression_p (t);
9507 --processing_template_decl;
9508 if (!dependent_p)
9509 RETURN (t);
9511 /* Calculate the most general template of which R is a
9512 specialization, and the complete set of arguments used to
9513 specialize R. */
9514 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9515 argvec = tsubst_template_args (DECL_TI_ARGS
9516 (DECL_TEMPLATE_RESULT
9517 (DECL_TI_TEMPLATE (t))),
9518 args, complain, in_decl);
9520 /* Check to see if we already have this specialization. */
9521 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9522 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9524 if (spec)
9526 r = spec;
9527 break;
9530 /* We can see more levels of arguments than parameters if
9531 there was a specialization of a member template, like
9532 this:
9534 template <class T> struct S { template <class U> void f(); }
9535 template <> template <class U> void S<int>::f(U);
9537 Here, we'll be substituting into the specialization,
9538 because that's where we can find the code we actually
9539 want to generate, but we'll have enough arguments for
9540 the most general template.
9542 We also deal with the peculiar case:
9544 template <class T> struct S {
9545 template <class U> friend void f();
9547 template <class U> void f() {}
9548 template S<int>;
9549 template void f<double>();
9551 Here, the ARGS for the instantiation of will be {int,
9552 double}. But, we only need as many ARGS as there are
9553 levels of template parameters in CODE_PATTERN. We are
9554 careful not to get fooled into reducing the ARGS in
9555 situations like:
9557 template <class T> struct S { template <class U> void f(U); }
9558 template <class T> template <> void S<T>::f(int) {}
9560 which we can spot because the pattern will be a
9561 specialization in this case. */
9562 args_depth = TMPL_ARGS_DEPTH (args);
9563 parms_depth =
9564 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9565 if (args_depth > parms_depth
9566 && !DECL_TEMPLATE_SPECIALIZATION (t))
9567 args = get_innermost_template_args (args, parms_depth);
9569 else
9571 /* This special case arises when we have something like this:
9573 template <class T> struct S {
9574 friend void f<int>(int, double);
9577 Here, the DECL_TI_TEMPLATE for the friend declaration
9578 will be an IDENTIFIER_NODE. We are being called from
9579 tsubst_friend_function, and we want only to create a
9580 new decl (R) with appropriate types so that we can call
9581 determine_specialization. */
9582 gen_tmpl = NULL_TREE;
9585 if (DECL_CLASS_SCOPE_P (t))
9587 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9588 member = 2;
9589 else
9590 member = 1;
9591 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9592 complain, t, /*entering_scope=*/1);
9594 else
9596 member = 0;
9597 ctx = DECL_CONTEXT (t);
9599 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9600 if (type == error_mark_node)
9601 RETURN (error_mark_node);
9603 /* We do NOT check for matching decls pushed separately at this
9604 point, as they may not represent instantiations of this
9605 template, and in any case are considered separate under the
9606 discrete model. */
9607 r = copy_decl (t);
9608 DECL_USE_TEMPLATE (r) = 0;
9609 TREE_TYPE (r) = type;
9610 /* Clear out the mangled name and RTL for the instantiation. */
9611 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9612 SET_DECL_RTL (r, NULL);
9613 /* Leave DECL_INITIAL set on deleted instantiations. */
9614 if (!DECL_DELETED_FN (r))
9615 DECL_INITIAL (r) = NULL_TREE;
9616 DECL_CONTEXT (r) = ctx;
9618 if (member && DECL_CONV_FN_P (r))
9619 /* Type-conversion operator. Reconstruct the name, in
9620 case it's the name of one of the template's parameters. */
9621 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9623 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9624 complain, t);
9625 DECL_RESULT (r) = NULL_TREE;
9627 TREE_STATIC (r) = 0;
9628 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9629 DECL_EXTERNAL (r) = 1;
9630 /* If this is an instantiation of a function with internal
9631 linkage, we already know what object file linkage will be
9632 assigned to the instantiation. */
9633 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9634 DECL_DEFER_OUTPUT (r) = 0;
9635 DECL_CHAIN (r) = NULL_TREE;
9636 DECL_PENDING_INLINE_INFO (r) = 0;
9637 DECL_PENDING_INLINE_P (r) = 0;
9638 DECL_SAVED_TREE (r) = NULL_TREE;
9639 DECL_STRUCT_FUNCTION (r) = NULL;
9640 TREE_USED (r) = 0;
9641 /* We'll re-clone as appropriate in instantiate_template. */
9642 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9644 /* If we aren't complaining now, return on error before we register
9645 the specialization so that we'll complain eventually. */
9646 if ((complain & tf_error) == 0
9647 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9648 && !grok_op_properties (r, /*complain=*/false))
9649 RETURN (error_mark_node);
9651 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9652 this in the special friend case mentioned above where
9653 GEN_TMPL is NULL. */
9654 if (gen_tmpl)
9656 DECL_TEMPLATE_INFO (r)
9657 = build_template_info (gen_tmpl, argvec);
9658 SET_DECL_IMPLICIT_INSTANTIATION (r);
9659 register_specialization (r, gen_tmpl, argvec, false, hash);
9661 /* We're not supposed to instantiate default arguments
9662 until they are called, for a template. But, for a
9663 declaration like:
9665 template <class T> void f ()
9666 { extern void g(int i = T()); }
9668 we should do the substitution when the template is
9669 instantiated. We handle the member function case in
9670 instantiate_class_template since the default arguments
9671 might refer to other members of the class. */
9672 if (!member
9673 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9674 && !uses_template_parms (argvec))
9675 tsubst_default_arguments (r);
9677 else
9678 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9680 /* Copy the list of befriending classes. */
9681 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9682 *friends;
9683 friends = &TREE_CHAIN (*friends))
9685 *friends = copy_node (*friends);
9686 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9687 args, complain,
9688 in_decl);
9691 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9693 maybe_retrofit_in_chrg (r);
9694 if (DECL_CONSTRUCTOR_P (r))
9695 grok_ctor_properties (ctx, r);
9696 /* If this is an instantiation of a member template, clone it.
9697 If it isn't, that'll be handled by
9698 clone_constructors_and_destructors. */
9699 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9700 clone_function_decl (r, /*update_method_vec_p=*/0);
9702 else if ((complain & tf_error) != 0
9703 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9704 && !grok_op_properties (r, /*complain=*/true))
9705 RETURN (error_mark_node);
9707 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9708 SET_DECL_FRIEND_CONTEXT (r,
9709 tsubst (DECL_FRIEND_CONTEXT (t),
9710 args, complain, in_decl));
9712 /* Possibly limit visibility based on template args. */
9713 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9714 if (DECL_VISIBILITY_SPECIFIED (t))
9716 DECL_VISIBILITY_SPECIFIED (r) = 0;
9717 DECL_ATTRIBUTES (r)
9718 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9720 determine_visibility (r);
9721 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9722 && !processing_template_decl)
9723 defaulted_late_check (r);
9725 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9726 args, complain, in_decl);
9728 break;
9730 case PARM_DECL:
9732 tree type = NULL_TREE;
9733 int i, len = 1;
9734 tree expanded_types = NULL_TREE;
9735 tree prev_r = NULL_TREE;
9736 tree first_r = NULL_TREE;
9738 if (FUNCTION_PARAMETER_PACK_P (t))
9740 /* If there is a local specialization that isn't a
9741 parameter pack, it means that we're doing a "simple"
9742 substitution from inside tsubst_pack_expansion. Just
9743 return the local specialization (which will be a single
9744 parm). */
9745 tree spec = retrieve_local_specialization (t);
9746 if (spec
9747 && TREE_CODE (spec) == PARM_DECL
9748 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9749 RETURN (spec);
9751 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9752 the parameters in this function parameter pack. */
9753 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9754 complain, in_decl);
9755 if (TREE_CODE (expanded_types) == TREE_VEC)
9757 len = TREE_VEC_LENGTH (expanded_types);
9759 /* Zero-length parameter packs are boring. Just substitute
9760 into the chain. */
9761 if (len == 0)
9762 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9763 TREE_CHAIN (t)));
9765 else
9767 /* All we did was update the type. Make a note of that. */
9768 type = expanded_types;
9769 expanded_types = NULL_TREE;
9773 /* Loop through all of the parameter's we'll build. When T is
9774 a function parameter pack, LEN is the number of expanded
9775 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9776 r = NULL_TREE;
9777 for (i = 0; i < len; ++i)
9779 prev_r = r;
9780 r = copy_node (t);
9781 if (DECL_TEMPLATE_PARM_P (t))
9782 SET_DECL_TEMPLATE_PARM_P (r);
9784 /* An argument of a function parameter pack is not a parameter
9785 pack. */
9786 FUNCTION_PARAMETER_PACK_P (r) = false;
9788 if (expanded_types)
9789 /* We're on the Ith parameter of the function parameter
9790 pack. */
9792 /* Get the Ith type. */
9793 type = TREE_VEC_ELT (expanded_types, i);
9795 if (DECL_NAME (r))
9796 /* Rename the parameter to include the index. */
9797 DECL_NAME (r) =
9798 make_ith_pack_parameter_name (DECL_NAME (r), i);
9800 else if (!type)
9801 /* We're dealing with a normal parameter. */
9802 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9804 type = type_decays_to (type);
9805 TREE_TYPE (r) = type;
9806 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9808 if (DECL_INITIAL (r))
9810 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9811 DECL_INITIAL (r) = TREE_TYPE (r);
9812 else
9813 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9814 complain, in_decl);
9817 DECL_CONTEXT (r) = NULL_TREE;
9819 if (!DECL_TEMPLATE_PARM_P (r))
9820 DECL_ARG_TYPE (r) = type_passed_as (type);
9822 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9823 args, complain, in_decl);
9825 /* Keep track of the first new parameter we
9826 generate. That's what will be returned to the
9827 caller. */
9828 if (!first_r)
9829 first_r = r;
9831 /* Build a proper chain of parameters when substituting
9832 into a function parameter pack. */
9833 if (prev_r)
9834 DECL_CHAIN (prev_r) = r;
9837 if (DECL_CHAIN (t))
9838 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
9839 complain, DECL_CHAIN (t));
9841 /* FIRST_R contains the start of the chain we've built. */
9842 r = first_r;
9844 break;
9846 case FIELD_DECL:
9848 tree type;
9850 r = copy_decl (t);
9851 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9852 if (type == error_mark_node)
9853 RETURN (error_mark_node);
9854 TREE_TYPE (r) = type;
9855 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9857 /* DECL_INITIAL gives the number of bits in a bit-field. */
9858 DECL_INITIAL (r)
9859 = tsubst_expr (DECL_INITIAL (t), args,
9860 complain, in_decl,
9861 /*integral_constant_expression_p=*/true);
9862 /* We don't have to set DECL_CONTEXT here; it is set by
9863 finish_member_declaration. */
9864 DECL_CHAIN (r) = NULL_TREE;
9865 if (VOID_TYPE_P (type))
9866 error ("instantiation of %q+D as type %qT", r, type);
9868 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9869 args, complain, in_decl);
9871 break;
9873 case USING_DECL:
9874 /* We reach here only for member using decls. */
9875 if (DECL_DEPENDENT_P (t))
9877 r = do_class_using_decl
9878 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9879 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9880 if (!r)
9881 r = error_mark_node;
9882 else
9884 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9885 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9888 else
9890 r = copy_node (t);
9891 DECL_CHAIN (r) = NULL_TREE;
9893 break;
9895 case TYPE_DECL:
9896 case VAR_DECL:
9898 tree argvec = NULL_TREE;
9899 tree gen_tmpl = NULL_TREE;
9900 tree spec;
9901 tree tmpl = NULL_TREE;
9902 tree ctx;
9903 tree type = NULL_TREE;
9904 bool local_p;
9906 if (TREE_CODE (t) == TYPE_DECL
9907 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9909 /* If this is the canonical decl, we don't have to
9910 mess with instantiations, and often we can't (for
9911 typename, template type parms and such). Note that
9912 TYPE_NAME is not correct for the above test if
9913 we've copied the type for a typedef. */
9914 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9915 if (type == error_mark_node)
9916 RETURN (error_mark_node);
9917 r = TYPE_NAME (type);
9918 break;
9921 /* Check to see if we already have the specialization we
9922 need. */
9923 spec = NULL_TREE;
9924 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9926 /* T is a static data member or namespace-scope entity.
9927 We have to substitute into namespace-scope variables
9928 (even though such entities are never templates) because
9929 of cases like:
9931 template <class T> void f() { extern T t; }
9933 where the entity referenced is not known until
9934 instantiation time. */
9935 local_p = false;
9936 ctx = DECL_CONTEXT (t);
9937 if (DECL_CLASS_SCOPE_P (t))
9939 ctx = tsubst_aggr_type (ctx, args,
9940 complain,
9941 in_decl, /*entering_scope=*/1);
9942 /* If CTX is unchanged, then T is in fact the
9943 specialization we want. That situation occurs when
9944 referencing a static data member within in its own
9945 class. We can use pointer equality, rather than
9946 same_type_p, because DECL_CONTEXT is always
9947 canonical. */
9948 if (ctx == DECL_CONTEXT (t))
9949 spec = t;
9952 if (!spec)
9954 tmpl = DECL_TI_TEMPLATE (t);
9955 gen_tmpl = most_general_template (tmpl);
9956 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9957 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9958 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9961 else
9963 /* A local variable. */
9964 local_p = true;
9965 /* Subsequent calls to pushdecl will fill this in. */
9966 ctx = NULL_TREE;
9967 spec = retrieve_local_specialization (t);
9969 /* If we already have the specialization we need, there is
9970 nothing more to do. */
9971 if (spec)
9973 r = spec;
9974 break;
9977 /* Create a new node for the specialization we need. */
9978 r = copy_decl (t);
9979 if (type == NULL_TREE)
9981 if (is_typedef_decl (t))
9982 type = DECL_ORIGINAL_TYPE (t);
9983 else
9984 type = TREE_TYPE (t);
9985 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
9986 type = strip_array_domain (type);
9987 type = tsubst (type, args, complain, in_decl);
9989 if (TREE_CODE (r) == VAR_DECL)
9991 /* Even if the original location is out of scope, the
9992 newly substituted one is not. */
9993 DECL_DEAD_FOR_LOCAL (r) = 0;
9994 DECL_INITIALIZED_P (r) = 0;
9995 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9996 if (type == error_mark_node)
9997 RETURN (error_mark_node);
9998 if (TREE_CODE (type) == FUNCTION_TYPE)
10000 /* It may seem that this case cannot occur, since:
10002 typedef void f();
10003 void g() { f x; }
10005 declares a function, not a variable. However:
10007 typedef void f();
10008 template <typename T> void g() { T t; }
10009 template void g<f>();
10011 is an attempt to declare a variable with function
10012 type. */
10013 error ("variable %qD has function type",
10014 /* R is not yet sufficiently initialized, so we
10015 just use its name. */
10016 DECL_NAME (r));
10017 RETURN (error_mark_node);
10019 type = complete_type (type);
10020 /* Wait until cp_finish_decl to set this again, to handle
10021 circular dependency (template/instantiate6.C). */
10022 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10023 type = check_var_type (DECL_NAME (r), type);
10025 if (DECL_HAS_VALUE_EXPR_P (t))
10027 tree ve = DECL_VALUE_EXPR (t);
10028 ve = tsubst_expr (ve, args, complain, in_decl,
10029 /*constant_expression_p=*/false);
10030 SET_DECL_VALUE_EXPR (r, ve);
10033 else if (DECL_SELF_REFERENCE_P (t))
10034 SET_DECL_SELF_REFERENCE_P (r);
10035 TREE_TYPE (r) = type;
10036 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10037 DECL_CONTEXT (r) = ctx;
10038 /* Clear out the mangled name and RTL for the instantiation. */
10039 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10040 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10041 SET_DECL_RTL (r, NULL);
10042 /* The initializer must not be expanded until it is required;
10043 see [temp.inst]. */
10044 DECL_INITIAL (r) = NULL_TREE;
10045 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10046 SET_DECL_RTL (r, NULL);
10047 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10048 if (TREE_CODE (r) == VAR_DECL)
10050 /* Possibly limit visibility based on template args. */
10051 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10052 if (DECL_VISIBILITY_SPECIFIED (t))
10054 DECL_VISIBILITY_SPECIFIED (r) = 0;
10055 DECL_ATTRIBUTES (r)
10056 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10058 determine_visibility (r);
10061 if (!local_p)
10063 /* A static data member declaration is always marked
10064 external when it is declared in-class, even if an
10065 initializer is present. We mimic the non-template
10066 processing here. */
10067 DECL_EXTERNAL (r) = 1;
10069 register_specialization (r, gen_tmpl, argvec, false, hash);
10070 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10071 SET_DECL_IMPLICIT_INSTANTIATION (r);
10073 else if (cp_unevaluated_operand)
10075 /* We're substituting this var in a decltype outside of its
10076 scope, such as for a lambda return type. Don't add it to
10077 local_specializations, do perform auto deduction. */
10078 tree auto_node = type_uses_auto (type);
10079 tree init
10080 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10081 /*constant_expression_p=*/false);
10083 if (auto_node && init)
10085 init = resolve_nondeduced_context (init);
10086 if (describable_type (init))
10088 type = do_auto_deduction (type, init, auto_node);
10089 TREE_TYPE (r) = type;
10093 else
10094 register_local_specialization (r, t);
10096 DECL_CHAIN (r) = NULL_TREE;
10098 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10099 /*flags=*/0,
10100 args, complain, in_decl);
10102 /* Preserve a typedef that names a type. */
10103 if (is_typedef_decl (r))
10105 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10106 set_underlying_type (r);
10109 layout_decl (r, 0);
10111 break;
10113 default:
10114 gcc_unreachable ();
10116 #undef RETURN
10118 out:
10119 /* Restore the file and line information. */
10120 input_location = saved_loc;
10122 return r;
10125 /* Substitute into the ARG_TYPES of a function type. */
10127 static tree
10128 tsubst_arg_types (tree arg_types,
10129 tree args,
10130 tsubst_flags_t complain,
10131 tree in_decl)
10133 tree remaining_arg_types;
10134 tree type = NULL_TREE;
10135 int i = 1;
10136 tree expanded_args = NULL_TREE;
10137 tree default_arg;
10139 if (!arg_types || arg_types == void_list_node)
10140 return arg_types;
10142 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10143 args, complain, in_decl);
10144 if (remaining_arg_types == error_mark_node)
10145 return error_mark_node;
10147 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10149 /* For a pack expansion, perform substitution on the
10150 entire expression. Later on, we'll handle the arguments
10151 one-by-one. */
10152 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10153 args, complain, in_decl);
10155 if (TREE_CODE (expanded_args) == TREE_VEC)
10156 /* So that we'll spin through the parameters, one by one. */
10157 i = TREE_VEC_LENGTH (expanded_args);
10158 else
10160 /* We only partially substituted into the parameter
10161 pack. Our type is TYPE_PACK_EXPANSION. */
10162 type = expanded_args;
10163 expanded_args = NULL_TREE;
10167 while (i > 0) {
10168 --i;
10170 if (expanded_args)
10171 type = TREE_VEC_ELT (expanded_args, i);
10172 else if (!type)
10173 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10175 if (type == error_mark_node)
10176 return error_mark_node;
10177 if (VOID_TYPE_P (type))
10179 if (complain & tf_error)
10181 error ("invalid parameter type %qT", type);
10182 if (in_decl)
10183 error ("in declaration %q+D", in_decl);
10185 return error_mark_node;
10188 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10189 top-level qualifiers as required. */
10190 type = TYPE_MAIN_VARIANT (type_decays_to (type));
10192 /* We do not substitute into default arguments here. The standard
10193 mandates that they be instantiated only when needed, which is
10194 done in build_over_call. */
10195 default_arg = TREE_PURPOSE (arg_types);
10197 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10199 /* We've instantiated a template before its default arguments
10200 have been parsed. This can happen for a nested template
10201 class, and is not an error unless we require the default
10202 argument in a call of this function. */
10203 remaining_arg_types =
10204 tree_cons (default_arg, type, remaining_arg_types);
10205 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
10206 remaining_arg_types);
10208 else
10209 remaining_arg_types =
10210 hash_tree_cons (default_arg, type, remaining_arg_types);
10213 return remaining_arg_types;
10216 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10217 *not* handle the exception-specification for FNTYPE, because the
10218 initial substitution of explicitly provided template parameters
10219 during argument deduction forbids substitution into the
10220 exception-specification:
10222 [temp.deduct]
10224 All references in the function type of the function template to the
10225 corresponding template parameters are replaced by the specified tem-
10226 plate argument values. If a substitution in a template parameter or
10227 in the function type of the function template results in an invalid
10228 type, type deduction fails. [Note: The equivalent substitution in
10229 exception specifications is done only when the function is instanti-
10230 ated, at which point a program is ill-formed if the substitution
10231 results in an invalid type.] */
10233 static tree
10234 tsubst_function_type (tree t,
10235 tree args,
10236 tsubst_flags_t complain,
10237 tree in_decl)
10239 tree return_type;
10240 tree arg_types;
10241 tree fntype;
10243 /* The TYPE_CONTEXT is not used for function/method types. */
10244 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10246 /* Substitute the return type. */
10247 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10248 if (return_type == error_mark_node)
10249 return error_mark_node;
10250 /* The standard does not presently indicate that creation of a
10251 function type with an invalid return type is a deduction failure.
10252 However, that is clearly analogous to creating an array of "void"
10253 or a reference to a reference. This is core issue #486. */
10254 if (TREE_CODE (return_type) == ARRAY_TYPE
10255 || TREE_CODE (return_type) == FUNCTION_TYPE)
10257 if (complain & tf_error)
10259 if (TREE_CODE (return_type) == ARRAY_TYPE)
10260 error ("function returning an array");
10261 else
10262 error ("function returning a function");
10264 return error_mark_node;
10267 /* Substitute the argument types. */
10268 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
10269 complain, in_decl);
10270 if (arg_types == error_mark_node)
10271 return error_mark_node;
10273 /* Construct a new type node and return it. */
10274 if (TREE_CODE (t) == FUNCTION_TYPE)
10276 fntype = build_function_type (return_type, arg_types);
10277 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10279 else
10281 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10282 if (! MAYBE_CLASS_TYPE_P (r))
10284 /* [temp.deduct]
10286 Type deduction may fail for any of the following
10287 reasons:
10289 -- Attempting to create "pointer to member of T" when T
10290 is not a class type. */
10291 if (complain & tf_error)
10292 error ("creating pointer to member function of non-class type %qT",
10294 return error_mark_node;
10297 fntype = build_method_type_directly (r, return_type,
10298 TREE_CHAIN (arg_types));
10300 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10302 return fntype;
10305 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10306 ARGS into that specification, and return the substituted
10307 specification. If there is no specification, return NULL_TREE. */
10309 static tree
10310 tsubst_exception_specification (tree fntype,
10311 tree args,
10312 tsubst_flags_t complain,
10313 tree in_decl)
10315 tree specs;
10316 tree new_specs;
10318 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10319 new_specs = NULL_TREE;
10320 if (specs && TREE_PURPOSE (specs))
10322 /* A noexcept-specifier. */
10323 new_specs = tsubst_copy_and_build
10324 (TREE_PURPOSE (specs), args, complain, in_decl, /*function_p=*/false,
10325 /*integral_constant_expression_p=*/true);
10326 new_specs = build_noexcept_spec (new_specs, complain);
10328 else if (specs)
10330 if (! TREE_VALUE (specs))
10331 new_specs = specs;
10332 else
10333 while (specs)
10335 tree spec;
10336 int i, len = 1;
10337 tree expanded_specs = NULL_TREE;
10339 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10341 /* Expand the pack expansion type. */
10342 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10343 args, complain,
10344 in_decl);
10346 if (expanded_specs == error_mark_node)
10347 return error_mark_node;
10348 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10349 len = TREE_VEC_LENGTH (expanded_specs);
10350 else
10352 /* We're substituting into a member template, so
10353 we got a TYPE_PACK_EXPANSION back. Add that
10354 expansion and move on. */
10355 gcc_assert (TREE_CODE (expanded_specs)
10356 == TYPE_PACK_EXPANSION);
10357 new_specs = add_exception_specifier (new_specs,
10358 expanded_specs,
10359 complain);
10360 specs = TREE_CHAIN (specs);
10361 continue;
10365 for (i = 0; i < len; ++i)
10367 if (expanded_specs)
10368 spec = TREE_VEC_ELT (expanded_specs, i);
10369 else
10370 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10371 if (spec == error_mark_node)
10372 return spec;
10373 new_specs = add_exception_specifier (new_specs, spec,
10374 complain);
10377 specs = TREE_CHAIN (specs);
10380 return new_specs;
10383 /* Take the tree structure T and replace template parameters used
10384 therein with the argument vector ARGS. IN_DECL is an associated
10385 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10386 Issue error and warning messages under control of COMPLAIN. Note
10387 that we must be relatively non-tolerant of extensions here, in
10388 order to preserve conformance; if we allow substitutions that
10389 should not be allowed, we may allow argument deductions that should
10390 not succeed, and therefore report ambiguous overload situations
10391 where there are none. In theory, we could allow the substitution,
10392 but indicate that it should have failed, and allow our caller to
10393 make sure that the right thing happens, but we don't try to do this
10394 yet.
10396 This function is used for dealing with types, decls and the like;
10397 for expressions, use tsubst_expr or tsubst_copy. */
10399 tree
10400 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10402 enum tree_code code;
10403 tree type, r;
10405 if (t == NULL_TREE || t == error_mark_node
10406 || t == integer_type_node
10407 || t == void_type_node
10408 || t == char_type_node
10409 || t == unknown_type_node
10410 || TREE_CODE (t) == NAMESPACE_DECL
10411 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10412 return t;
10414 if (DECL_P (t))
10415 return tsubst_decl (t, args, complain);
10417 if (args == NULL_TREE)
10418 return t;
10420 code = TREE_CODE (t);
10422 if (code == IDENTIFIER_NODE)
10423 type = IDENTIFIER_TYPE_VALUE (t);
10424 else
10425 type = TREE_TYPE (t);
10427 gcc_assert (type != unknown_type_node);
10429 /* Reuse typedefs. We need to do this to handle dependent attributes,
10430 such as attribute aligned. */
10431 if (TYPE_P (t)
10432 && typedef_variant_p (t))
10434 tree decl = TYPE_NAME (t);
10436 if (DECL_CLASS_SCOPE_P (decl)
10437 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10438 && uses_template_parms (DECL_CONTEXT (decl)))
10440 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10441 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10442 r = retrieve_specialization (tmpl, gen_args, 0);
10444 else if (DECL_FUNCTION_SCOPE_P (decl)
10445 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10446 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10447 r = retrieve_local_specialization (decl);
10448 else
10449 /* The typedef is from a non-template context. */
10450 return t;
10452 if (r)
10454 r = TREE_TYPE (r);
10455 r = cp_build_qualified_type_real
10456 (r, cp_type_quals (t) | cp_type_quals (r),
10457 complain | tf_ignore_bad_quals);
10458 return r;
10460 /* Else we must be instantiating the typedef, so fall through. */
10463 if (type
10464 && code != TYPENAME_TYPE
10465 && code != TEMPLATE_TYPE_PARM
10466 && code != IDENTIFIER_NODE
10467 && code != FUNCTION_TYPE
10468 && code != METHOD_TYPE)
10469 type = tsubst (type, args, complain, in_decl);
10470 if (type == error_mark_node)
10471 return error_mark_node;
10473 switch (code)
10475 case RECORD_TYPE:
10476 case UNION_TYPE:
10477 case ENUMERAL_TYPE:
10478 return tsubst_aggr_type (t, args, complain, in_decl,
10479 /*entering_scope=*/0);
10481 case ERROR_MARK:
10482 case IDENTIFIER_NODE:
10483 case VOID_TYPE:
10484 case REAL_TYPE:
10485 case COMPLEX_TYPE:
10486 case VECTOR_TYPE:
10487 case BOOLEAN_TYPE:
10488 case NULLPTR_TYPE:
10489 case LANG_TYPE:
10490 return t;
10492 case INTEGER_TYPE:
10493 if (t == integer_type_node)
10494 return t;
10496 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10497 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10498 return t;
10501 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10503 max = tsubst_expr (omax, args, complain, in_decl,
10504 /*integral_constant_expression_p=*/false);
10506 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10507 needed. */
10508 if (TREE_CODE (max) == NOP_EXPR
10509 && TREE_SIDE_EFFECTS (omax)
10510 && !TREE_TYPE (max))
10511 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10513 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10514 with TREE_SIDE_EFFECTS that indicates this is not an integral
10515 constant expression. */
10516 if (processing_template_decl
10517 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10519 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10520 TREE_SIDE_EFFECTS (max) = 1;
10523 return compute_array_index_type (NULL_TREE, max, complain);
10526 case TEMPLATE_TYPE_PARM:
10527 case TEMPLATE_TEMPLATE_PARM:
10528 case BOUND_TEMPLATE_TEMPLATE_PARM:
10529 case TEMPLATE_PARM_INDEX:
10531 int idx;
10532 int level;
10533 int levels;
10534 tree arg = NULL_TREE;
10536 r = NULL_TREE;
10538 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10539 template_parm_level_and_index (t, &level, &idx);
10541 levels = TMPL_ARGS_DEPTH (args);
10542 if (level <= levels)
10544 arg = TMPL_ARG (args, level, idx);
10546 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10547 /* See through ARGUMENT_PACK_SELECT arguments. */
10548 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10551 if (arg == error_mark_node)
10552 return error_mark_node;
10553 else if (arg != NULL_TREE)
10555 if (ARGUMENT_PACK_P (arg))
10556 /* If ARG is an argument pack, we don't actually want to
10557 perform a substitution here, because substitutions
10558 for argument packs are only done
10559 element-by-element. We can get to this point when
10560 substituting the type of a non-type template
10561 parameter pack, when that type actually contains
10562 template parameter packs from an outer template, e.g.,
10564 template<typename... Types> struct A {
10565 template<Types... Values> struct B { };
10566 }; */
10567 return t;
10569 if (code == TEMPLATE_TYPE_PARM)
10571 int quals;
10572 gcc_assert (TYPE_P (arg));
10574 quals = cp_type_quals (arg) | cp_type_quals (t);
10576 return cp_build_qualified_type_real
10577 (arg, quals, complain | tf_ignore_bad_quals);
10579 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10581 /* We are processing a type constructed from a
10582 template template parameter. */
10583 tree argvec = tsubst (TYPE_TI_ARGS (t),
10584 args, complain, in_decl);
10585 if (argvec == error_mark_node)
10586 return error_mark_node;
10588 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10589 are resolving nested-types in the signature of a
10590 member function templates. Otherwise ARG is a
10591 TEMPLATE_DECL and is the real template to be
10592 instantiated. */
10593 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10594 arg = TYPE_NAME (arg);
10596 r = lookup_template_class (arg,
10597 argvec, in_decl,
10598 DECL_CONTEXT (arg),
10599 /*entering_scope=*/0,
10600 complain);
10601 return cp_build_qualified_type_real
10602 (r, cp_type_quals (t), complain);
10604 else
10605 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10606 return unshare_expr (arg);
10609 if (level == 1)
10610 /* This can happen during the attempted tsubst'ing in
10611 unify. This means that we don't yet have any information
10612 about the template parameter in question. */
10613 return t;
10615 /* If we get here, we must have been looking at a parm for a
10616 more deeply nested template. Make a new version of this
10617 template parameter, but with a lower level. */
10618 switch (code)
10620 case TEMPLATE_TYPE_PARM:
10621 case TEMPLATE_TEMPLATE_PARM:
10622 case BOUND_TEMPLATE_TEMPLATE_PARM:
10623 if (cp_type_quals (t))
10625 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10626 r = cp_build_qualified_type_real
10627 (r, cp_type_quals (t),
10628 complain | (code == TEMPLATE_TYPE_PARM
10629 ? tf_ignore_bad_quals : 0));
10631 else
10633 r = copy_type (t);
10634 TEMPLATE_TYPE_PARM_INDEX (r)
10635 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10636 r, levels, args, complain);
10637 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10638 TYPE_MAIN_VARIANT (r) = r;
10639 TYPE_POINTER_TO (r) = NULL_TREE;
10640 TYPE_REFERENCE_TO (r) = NULL_TREE;
10642 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10643 /* We have reduced the level of the template
10644 template parameter, but not the levels of its
10645 template parameters, so canonical_type_parameter
10646 will not be able to find the canonical template
10647 template parameter for this level. Thus, we
10648 require structural equality checking to compare
10649 TEMPLATE_TEMPLATE_PARMs. */
10650 SET_TYPE_STRUCTURAL_EQUALITY (r);
10651 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10652 SET_TYPE_STRUCTURAL_EQUALITY (r);
10653 else
10654 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10656 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10658 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10659 complain, in_decl);
10660 if (argvec == error_mark_node)
10661 return error_mark_node;
10663 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10664 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10667 break;
10669 case TEMPLATE_PARM_INDEX:
10670 r = reduce_template_parm_level (t, type, levels, args, complain);
10671 break;
10673 default:
10674 gcc_unreachable ();
10677 return r;
10680 case TREE_LIST:
10682 tree purpose, value, chain;
10684 if (t == void_list_node)
10685 return t;
10687 purpose = TREE_PURPOSE (t);
10688 if (purpose)
10690 purpose = tsubst (purpose, args, complain, in_decl);
10691 if (purpose == error_mark_node)
10692 return error_mark_node;
10694 value = TREE_VALUE (t);
10695 if (value)
10697 value = tsubst (value, args, complain, in_decl);
10698 if (value == error_mark_node)
10699 return error_mark_node;
10701 chain = TREE_CHAIN (t);
10702 if (chain && chain != void_type_node)
10704 chain = tsubst (chain, args, complain, in_decl);
10705 if (chain == error_mark_node)
10706 return error_mark_node;
10708 if (purpose == TREE_PURPOSE (t)
10709 && value == TREE_VALUE (t)
10710 && chain == TREE_CHAIN (t))
10711 return t;
10712 return hash_tree_cons (purpose, value, chain);
10715 case TREE_BINFO:
10716 /* We should never be tsubsting a binfo. */
10717 gcc_unreachable ();
10719 case TREE_VEC:
10720 /* A vector of template arguments. */
10721 gcc_assert (!type);
10722 return tsubst_template_args (t, args, complain, in_decl);
10724 case POINTER_TYPE:
10725 case REFERENCE_TYPE:
10727 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10728 return t;
10730 /* [temp.deduct]
10732 Type deduction may fail for any of the following
10733 reasons:
10735 -- Attempting to create a pointer to reference type.
10736 -- Attempting to create a reference to a reference type or
10737 a reference to void.
10739 Core issue 106 says that creating a reference to a reference
10740 during instantiation is no longer a cause for failure. We
10741 only enforce this check in strict C++98 mode. */
10742 if ((TREE_CODE (type) == REFERENCE_TYPE
10743 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10744 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10746 static location_t last_loc;
10748 /* We keep track of the last time we issued this error
10749 message to avoid spewing a ton of messages during a
10750 single bad template instantiation. */
10751 if (complain & tf_error
10752 && last_loc != input_location)
10754 if (TREE_CODE (type) == VOID_TYPE)
10755 error ("forming reference to void");
10756 else if (code == POINTER_TYPE)
10757 error ("forming pointer to reference type %qT", type);
10758 else
10759 error ("forming reference to reference type %qT", type);
10760 last_loc = input_location;
10763 return error_mark_node;
10765 else if (code == POINTER_TYPE)
10767 r = build_pointer_type (type);
10768 if (TREE_CODE (type) == METHOD_TYPE)
10769 r = build_ptrmemfunc_type (r);
10771 else if (TREE_CODE (type) == REFERENCE_TYPE)
10772 /* In C++0x, during template argument substitution, when there is an
10773 attempt to create a reference to a reference type, reference
10774 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10776 "If a template-argument for a template-parameter T names a type
10777 that is a reference to a type A, an attempt to create the type
10778 'lvalue reference to cv T' creates the type 'lvalue reference to
10779 A,' while an attempt to create the type type rvalue reference to
10780 cv T' creates the type T"
10782 r = cp_build_reference_type
10783 (TREE_TYPE (type),
10784 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10785 else
10786 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10787 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10789 if (r != error_mark_node)
10790 /* Will this ever be needed for TYPE_..._TO values? */
10791 layout_type (r);
10793 return r;
10795 case OFFSET_TYPE:
10797 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10798 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10800 /* [temp.deduct]
10802 Type deduction may fail for any of the following
10803 reasons:
10805 -- Attempting to create "pointer to member of T" when T
10806 is not a class type. */
10807 if (complain & tf_error)
10808 error ("creating pointer to member of non-class type %qT", r);
10809 return error_mark_node;
10811 if (TREE_CODE (type) == REFERENCE_TYPE)
10813 if (complain & tf_error)
10814 error ("creating pointer to member reference type %qT", type);
10815 return error_mark_node;
10817 if (TREE_CODE (type) == VOID_TYPE)
10819 if (complain & tf_error)
10820 error ("creating pointer to member of type void");
10821 return error_mark_node;
10823 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10824 if (TREE_CODE (type) == FUNCTION_TYPE)
10826 /* The type of the implicit object parameter gets its
10827 cv-qualifiers from the FUNCTION_TYPE. */
10828 tree memptr;
10829 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
10830 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10831 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10832 complain);
10834 else
10835 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10836 cp_type_quals (t),
10837 complain);
10839 case FUNCTION_TYPE:
10840 case METHOD_TYPE:
10842 tree fntype;
10843 tree specs;
10844 fntype = tsubst_function_type (t, args, complain, in_decl);
10845 if (fntype == error_mark_node)
10846 return error_mark_node;
10848 /* Substitute the exception specification. */
10849 specs = tsubst_exception_specification (t, args, complain,
10850 in_decl);
10851 if (specs == error_mark_node)
10852 return error_mark_node;
10853 if (specs)
10854 fntype = build_exception_variant (fntype, specs);
10855 return fntype;
10857 case ARRAY_TYPE:
10859 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10860 if (domain == error_mark_node)
10861 return error_mark_node;
10863 /* As an optimization, we avoid regenerating the array type if
10864 it will obviously be the same as T. */
10865 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10866 return t;
10868 /* These checks should match the ones in grokdeclarator.
10870 [temp.deduct]
10872 The deduction may fail for any of the following reasons:
10874 -- Attempting to create an array with an element type that
10875 is void, a function type, or a reference type, or [DR337]
10876 an abstract class type. */
10877 if (TREE_CODE (type) == VOID_TYPE
10878 || TREE_CODE (type) == FUNCTION_TYPE
10879 || TREE_CODE (type) == REFERENCE_TYPE)
10881 if (complain & tf_error)
10882 error ("creating array of %qT", type);
10883 return error_mark_node;
10885 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10887 if (complain & tf_error)
10888 error ("creating array of %qT, which is an abstract class type",
10889 type);
10890 return error_mark_node;
10893 r = build_cplus_array_type (type, domain);
10895 if (TYPE_USER_ALIGN (t))
10897 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10898 TYPE_USER_ALIGN (r) = 1;
10901 return r;
10904 case TYPENAME_TYPE:
10906 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10907 in_decl, /*entering_scope=*/1);
10908 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10909 complain, in_decl);
10911 if (ctx == error_mark_node || f == error_mark_node)
10912 return error_mark_node;
10914 if (!MAYBE_CLASS_TYPE_P (ctx))
10916 if (complain & tf_error)
10917 error ("%qT is not a class, struct, or union type", ctx);
10918 return error_mark_node;
10920 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10922 /* Normally, make_typename_type does not require that the CTX
10923 have complete type in order to allow things like:
10925 template <class T> struct S { typename S<T>::X Y; };
10927 But, such constructs have already been resolved by this
10928 point, so here CTX really should have complete type, unless
10929 it's a partial instantiation. */
10930 ctx = complete_type (ctx);
10931 if (!COMPLETE_TYPE_P (ctx))
10933 if (complain & tf_error)
10934 cxx_incomplete_type_error (NULL_TREE, ctx);
10935 return error_mark_node;
10939 f = make_typename_type (ctx, f, typename_type,
10940 (complain & tf_error) | tf_keep_type_decl);
10941 if (f == error_mark_node)
10942 return f;
10943 if (TREE_CODE (f) == TYPE_DECL)
10945 complain |= tf_ignore_bad_quals;
10946 f = TREE_TYPE (f);
10949 if (TREE_CODE (f) != TYPENAME_TYPE)
10951 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10953 if (complain & tf_error)
10954 error ("%qT resolves to %qT, which is not an enumeration type",
10955 t, f);
10956 else
10957 return error_mark_node;
10959 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10961 if (complain & tf_error)
10962 error ("%qT resolves to %qT, which is is not a class type",
10963 t, f);
10964 else
10965 return error_mark_node;
10969 return cp_build_qualified_type_real
10970 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10973 case UNBOUND_CLASS_TEMPLATE:
10975 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10976 in_decl, /*entering_scope=*/1);
10977 tree name = TYPE_IDENTIFIER (t);
10978 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10980 if (ctx == error_mark_node || name == error_mark_node)
10981 return error_mark_node;
10983 if (parm_list)
10984 parm_list = tsubst_template_parms (parm_list, args, complain);
10985 return make_unbound_class_template (ctx, name, parm_list, complain);
10988 case TYPEOF_TYPE:
10990 tree type;
10992 ++cp_unevaluated_operand;
10993 ++c_inhibit_evaluation_warnings;
10995 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10996 complain, in_decl,
10997 /*integral_constant_expression_p=*/false);
10999 --cp_unevaluated_operand;
11000 --c_inhibit_evaluation_warnings;
11002 type = finish_typeof (type);
11003 return cp_build_qualified_type_real (type,
11004 cp_type_quals (t)
11005 | cp_type_quals (type),
11006 complain);
11009 case DECLTYPE_TYPE:
11011 tree type;
11013 ++cp_unevaluated_operand;
11014 ++c_inhibit_evaluation_warnings;
11016 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11017 complain, in_decl,
11018 /*integral_constant_expression_p=*/false);
11020 --cp_unevaluated_operand;
11021 --c_inhibit_evaluation_warnings;
11023 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11024 type = lambda_capture_field_type (type);
11025 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
11026 type = lambda_return_type (type);
11027 else
11028 type = finish_decltype_type
11029 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11030 return cp_build_qualified_type_real (type,
11031 cp_type_quals (t)
11032 | cp_type_quals (type),
11033 complain);
11036 case UNDERLYING_TYPE:
11038 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11039 complain, in_decl);
11040 return finish_underlying_type (type);
11043 case TYPE_ARGUMENT_PACK:
11044 case NONTYPE_ARGUMENT_PACK:
11046 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11047 tree packed_out =
11048 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11049 args,
11050 complain,
11051 in_decl);
11052 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11054 /* For template nontype argument packs, also substitute into
11055 the type. */
11056 if (code == NONTYPE_ARGUMENT_PACK)
11057 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11059 return r;
11061 break;
11063 case INTEGER_CST:
11064 case REAL_CST:
11065 case STRING_CST:
11066 case PLUS_EXPR:
11067 case MINUS_EXPR:
11068 case NEGATE_EXPR:
11069 case NOP_EXPR:
11070 case INDIRECT_REF:
11071 case ADDR_EXPR:
11072 case CALL_EXPR:
11073 case ARRAY_REF:
11074 case SCOPE_REF:
11075 /* We should use one of the expression tsubsts for these codes. */
11076 gcc_unreachable ();
11078 default:
11079 sorry ("use of %qs in template", tree_code_name [(int) code]);
11080 return error_mark_node;
11084 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11085 type of the expression on the left-hand side of the "." or "->"
11086 operator. */
11088 static tree
11089 tsubst_baselink (tree baselink, tree object_type,
11090 tree args, tsubst_flags_t complain, tree in_decl)
11092 tree name;
11093 tree qualifying_scope;
11094 tree fns;
11095 tree optype;
11096 tree template_args = 0;
11097 bool template_id_p = false;
11099 /* A baselink indicates a function from a base class. Both the
11100 BASELINK_ACCESS_BINFO and the base class referenced may
11101 indicate bases of the template class, rather than the
11102 instantiated class. In addition, lookups that were not
11103 ambiguous before may be ambiguous now. Therefore, we perform
11104 the lookup again. */
11105 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11106 qualifying_scope = tsubst (qualifying_scope, args,
11107 complain, in_decl);
11108 fns = BASELINK_FUNCTIONS (baselink);
11109 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11110 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11112 template_id_p = true;
11113 template_args = TREE_OPERAND (fns, 1);
11114 fns = TREE_OPERAND (fns, 0);
11115 if (template_args)
11116 template_args = tsubst_template_args (template_args, args,
11117 complain, in_decl);
11119 name = DECL_NAME (get_first_fn (fns));
11120 if (IDENTIFIER_TYPENAME_P (name))
11121 name = mangle_conv_op_name_for_type (optype);
11122 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11123 if (!baselink)
11124 return error_mark_node;
11126 /* If lookup found a single function, mark it as used at this
11127 point. (If it lookup found multiple functions the one selected
11128 later by overload resolution will be marked as used at that
11129 point.) */
11130 if (BASELINK_P (baselink))
11131 fns = BASELINK_FUNCTIONS (baselink);
11132 if (!template_id_p && !really_overloaded_fn (fns))
11133 mark_used (OVL_CURRENT (fns));
11135 /* Add back the template arguments, if present. */
11136 if (BASELINK_P (baselink) && template_id_p)
11137 BASELINK_FUNCTIONS (baselink)
11138 = build_nt (TEMPLATE_ID_EXPR,
11139 BASELINK_FUNCTIONS (baselink),
11140 template_args);
11141 /* Update the conversion operator type. */
11142 BASELINK_OPTYPE (baselink) = optype;
11144 if (!object_type)
11145 object_type = current_class_type;
11146 return adjust_result_of_qualified_name_lookup (baselink,
11147 qualifying_scope,
11148 object_type);
11151 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11152 true if the qualified-id will be a postfix-expression in-and-of
11153 itself; false if more of the postfix-expression follows the
11154 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11155 of "&". */
11157 static tree
11158 tsubst_qualified_id (tree qualified_id, tree args,
11159 tsubst_flags_t complain, tree in_decl,
11160 bool done, bool address_p)
11162 tree expr;
11163 tree scope;
11164 tree name;
11165 bool is_template;
11166 tree template_args;
11168 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11170 /* Figure out what name to look up. */
11171 name = TREE_OPERAND (qualified_id, 1);
11172 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11174 is_template = true;
11175 template_args = TREE_OPERAND (name, 1);
11176 if (template_args)
11177 template_args = tsubst_template_args (template_args, args,
11178 complain, in_decl);
11179 name = TREE_OPERAND (name, 0);
11181 else
11183 is_template = false;
11184 template_args = NULL_TREE;
11187 /* Substitute into the qualifying scope. When there are no ARGS, we
11188 are just trying to simplify a non-dependent expression. In that
11189 case the qualifying scope may be dependent, and, in any case,
11190 substituting will not help. */
11191 scope = TREE_OPERAND (qualified_id, 0);
11192 if (args)
11194 scope = tsubst (scope, args, complain, in_decl);
11195 expr = tsubst_copy (name, args, complain, in_decl);
11197 else
11198 expr = name;
11200 if (dependent_scope_p (scope))
11201 return build_qualified_name (NULL_TREE, scope, expr,
11202 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11204 if (!BASELINK_P (name) && !DECL_P (expr))
11206 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11208 /* A BIT_NOT_EXPR is used to represent a destructor. */
11209 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11211 error ("qualifying type %qT does not match destructor name ~%qT",
11212 scope, TREE_OPERAND (expr, 0));
11213 expr = error_mark_node;
11215 else
11216 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11217 /*is_type_p=*/0, false);
11219 else
11220 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11221 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11222 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11224 if (complain & tf_error)
11226 error ("dependent-name %qE is parsed as a non-type, but "
11227 "instantiation yields a type", qualified_id);
11228 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11230 return error_mark_node;
11234 if (DECL_P (expr))
11236 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11237 scope);
11238 /* Remember that there was a reference to this entity. */
11239 mark_used (expr);
11242 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11244 if (complain & tf_error)
11245 qualified_name_lookup_error (scope,
11246 TREE_OPERAND (qualified_id, 1),
11247 expr, input_location);
11248 return error_mark_node;
11251 if (is_template)
11252 expr = lookup_template_function (expr, template_args);
11254 if (expr == error_mark_node && complain & tf_error)
11255 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11256 expr, input_location);
11257 else if (TYPE_P (scope))
11259 expr = (adjust_result_of_qualified_name_lookup
11260 (expr, scope, current_class_type));
11261 expr = (finish_qualified_id_expr
11262 (scope, expr, done, address_p,
11263 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11264 /*template_arg_p=*/false));
11267 /* Expressions do not generally have reference type. */
11268 if (TREE_CODE (expr) != SCOPE_REF
11269 /* However, if we're about to form a pointer-to-member, we just
11270 want the referenced member referenced. */
11271 && TREE_CODE (expr) != OFFSET_REF)
11272 expr = convert_from_reference (expr);
11274 return expr;
11277 /* Like tsubst, but deals with expressions. This function just replaces
11278 template parms; to finish processing the resultant expression, use
11279 tsubst_expr. */
11281 static tree
11282 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11284 enum tree_code code;
11285 tree r;
11287 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11288 return t;
11290 code = TREE_CODE (t);
11292 switch (code)
11294 case PARM_DECL:
11295 r = retrieve_local_specialization (t);
11297 if (r == NULL)
11299 tree c;
11300 /* This can happen for a parameter name used later in a function
11301 declaration (such as in a late-specified return type). Just
11302 make a dummy decl, since it's only used for its type. */
11303 gcc_assert (cp_unevaluated_operand != 0);
11304 /* We copy T because want to tsubst the PARM_DECL only,
11305 not the following PARM_DECLs that are chained to T. */
11306 c = copy_node (t);
11307 r = tsubst_decl (c, args, complain);
11308 /* Give it the template pattern as its context; its true context
11309 hasn't been instantiated yet and this is good enough for
11310 mangling. */
11311 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11314 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11315 r = ARGUMENT_PACK_SELECT_ARG (r);
11316 mark_used (r);
11317 return r;
11319 case CONST_DECL:
11321 tree enum_type;
11322 tree v;
11324 if (DECL_TEMPLATE_PARM_P (t))
11325 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11326 /* There is no need to substitute into namespace-scope
11327 enumerators. */
11328 if (DECL_NAMESPACE_SCOPE_P (t))
11329 return t;
11330 /* If ARGS is NULL, then T is known to be non-dependent. */
11331 if (args == NULL_TREE)
11332 return integral_constant_value (t);
11334 /* Unfortunately, we cannot just call lookup_name here.
11335 Consider:
11337 template <int I> int f() {
11338 enum E { a = I };
11339 struct S { void g() { E e = a; } };
11342 When we instantiate f<7>::S::g(), say, lookup_name is not
11343 clever enough to find f<7>::a. */
11344 enum_type
11345 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
11346 /*entering_scope=*/0);
11348 for (v = TYPE_VALUES (enum_type);
11349 v != NULL_TREE;
11350 v = TREE_CHAIN (v))
11351 if (TREE_PURPOSE (v) == DECL_NAME (t))
11352 return TREE_VALUE (v);
11354 /* We didn't find the name. That should never happen; if
11355 name-lookup found it during preliminary parsing, we
11356 should find it again here during instantiation. */
11357 gcc_unreachable ();
11359 return t;
11361 case FIELD_DECL:
11362 if (DECL_CONTEXT (t))
11364 tree ctx;
11366 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11367 /*entering_scope=*/1);
11368 if (ctx != DECL_CONTEXT (t))
11370 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11371 if (!r)
11373 if (complain & tf_error)
11374 error ("using invalid field %qD", t);
11375 return error_mark_node;
11377 return r;
11381 return t;
11383 case VAR_DECL:
11384 case FUNCTION_DECL:
11385 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11386 || local_variable_p (t))
11387 t = tsubst (t, args, complain, in_decl);
11388 mark_used (t);
11389 return t;
11391 case OVERLOAD:
11392 /* An OVERLOAD will always be a non-dependent overload set; an
11393 overload set from function scope will just be represented with an
11394 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11395 gcc_assert (!uses_template_parms (t));
11396 return t;
11398 case BASELINK:
11399 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11401 case TEMPLATE_DECL:
11402 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11403 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11404 args, complain, in_decl);
11405 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11406 return tsubst (t, args, complain, in_decl);
11407 else if (DECL_CLASS_SCOPE_P (t)
11408 && uses_template_parms (DECL_CONTEXT (t)))
11410 /* Template template argument like the following example need
11411 special treatment:
11413 template <template <class> class TT> struct C {};
11414 template <class T> struct D {
11415 template <class U> struct E {};
11416 C<E> c; // #1
11418 D<int> d; // #2
11420 We are processing the template argument `E' in #1 for
11421 the template instantiation #2. Originally, `E' is a
11422 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11423 have to substitute this with one having context `D<int>'. */
11425 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11426 return lookup_field (context, DECL_NAME(t), 0, false);
11428 else
11429 /* Ordinary template template argument. */
11430 return t;
11432 case CAST_EXPR:
11433 case REINTERPRET_CAST_EXPR:
11434 case CONST_CAST_EXPR:
11435 case STATIC_CAST_EXPR:
11436 case DYNAMIC_CAST_EXPR:
11437 case NOP_EXPR:
11438 return build1
11439 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11440 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11442 case SIZEOF_EXPR:
11443 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11446 tree expanded;
11447 int len = 0;
11449 ++cp_unevaluated_operand;
11450 ++c_inhibit_evaluation_warnings;
11451 /* We only want to compute the number of arguments. */
11452 expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11453 complain, in_decl);
11454 --cp_unevaluated_operand;
11455 --c_inhibit_evaluation_warnings;
11457 if (TREE_CODE (expanded) == TREE_VEC)
11458 len = TREE_VEC_LENGTH (expanded);
11460 if (expanded == error_mark_node)
11461 return error_mark_node;
11462 else if (PACK_EXPANSION_P (expanded)
11463 || (TREE_CODE (expanded) == TREE_VEC
11464 && len > 0
11465 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11467 if (TREE_CODE (expanded) == TREE_VEC)
11468 expanded = TREE_VEC_ELT (expanded, len - 1);
11470 if (TYPE_P (expanded))
11471 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11472 complain & tf_error);
11473 else
11474 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11475 complain & tf_error);
11477 else
11478 return build_int_cst (size_type_node, len);
11480 /* Fall through */
11482 case INDIRECT_REF:
11483 case NEGATE_EXPR:
11484 case TRUTH_NOT_EXPR:
11485 case BIT_NOT_EXPR:
11486 case ADDR_EXPR:
11487 case UNARY_PLUS_EXPR: /* Unary + */
11488 case ALIGNOF_EXPR:
11489 case AT_ENCODE_EXPR:
11490 case ARROW_EXPR:
11491 case THROW_EXPR:
11492 case TYPEID_EXPR:
11493 case REALPART_EXPR:
11494 case IMAGPART_EXPR:
11495 return build1
11496 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11497 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11499 case COMPONENT_REF:
11501 tree object;
11502 tree name;
11504 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11505 name = TREE_OPERAND (t, 1);
11506 if (TREE_CODE (name) == BIT_NOT_EXPR)
11508 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11509 complain, in_decl);
11510 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11512 else if (TREE_CODE (name) == SCOPE_REF
11513 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11515 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11516 complain, in_decl);
11517 name = TREE_OPERAND (name, 1);
11518 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11519 complain, in_decl);
11520 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11521 name = build_qualified_name (/*type=*/NULL_TREE,
11522 base, name,
11523 /*template_p=*/false);
11525 else if (TREE_CODE (name) == BASELINK)
11526 name = tsubst_baselink (name,
11527 non_reference (TREE_TYPE (object)),
11528 args, complain,
11529 in_decl);
11530 else
11531 name = tsubst_copy (name, args, complain, in_decl);
11532 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11535 case PLUS_EXPR:
11536 case MINUS_EXPR:
11537 case MULT_EXPR:
11538 case TRUNC_DIV_EXPR:
11539 case CEIL_DIV_EXPR:
11540 case FLOOR_DIV_EXPR:
11541 case ROUND_DIV_EXPR:
11542 case EXACT_DIV_EXPR:
11543 case BIT_AND_EXPR:
11544 case BIT_IOR_EXPR:
11545 case BIT_XOR_EXPR:
11546 case TRUNC_MOD_EXPR:
11547 case FLOOR_MOD_EXPR:
11548 case TRUTH_ANDIF_EXPR:
11549 case TRUTH_ORIF_EXPR:
11550 case TRUTH_AND_EXPR:
11551 case TRUTH_OR_EXPR:
11552 case RSHIFT_EXPR:
11553 case LSHIFT_EXPR:
11554 case RROTATE_EXPR:
11555 case LROTATE_EXPR:
11556 case EQ_EXPR:
11557 case NE_EXPR:
11558 case MAX_EXPR:
11559 case MIN_EXPR:
11560 case LE_EXPR:
11561 case GE_EXPR:
11562 case LT_EXPR:
11563 case GT_EXPR:
11564 case COMPOUND_EXPR:
11565 case DOTSTAR_EXPR:
11566 case MEMBER_REF:
11567 case PREDECREMENT_EXPR:
11568 case PREINCREMENT_EXPR:
11569 case POSTDECREMENT_EXPR:
11570 case POSTINCREMENT_EXPR:
11571 return build_nt
11572 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11573 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11575 case SCOPE_REF:
11576 return build_qualified_name (/*type=*/NULL_TREE,
11577 tsubst_copy (TREE_OPERAND (t, 0),
11578 args, complain, in_decl),
11579 tsubst_copy (TREE_OPERAND (t, 1),
11580 args, complain, in_decl),
11581 QUALIFIED_NAME_IS_TEMPLATE (t));
11583 case ARRAY_REF:
11584 return build_nt
11585 (ARRAY_REF,
11586 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11587 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11588 NULL_TREE, NULL_TREE);
11590 case CALL_EXPR:
11592 int n = VL_EXP_OPERAND_LENGTH (t);
11593 tree result = build_vl_exp (CALL_EXPR, n);
11594 int i;
11595 for (i = 0; i < n; i++)
11596 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11597 complain, in_decl);
11598 return result;
11601 case COND_EXPR:
11602 case MODOP_EXPR:
11603 case PSEUDO_DTOR_EXPR:
11605 r = build_nt
11606 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11607 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11608 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11609 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11610 return r;
11613 case NEW_EXPR:
11615 r = build_nt
11616 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11617 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11618 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11619 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11620 return r;
11623 case DELETE_EXPR:
11625 r = build_nt
11626 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11627 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11628 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11629 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11630 return r;
11633 case TEMPLATE_ID_EXPR:
11635 /* Substituted template arguments */
11636 tree fn = TREE_OPERAND (t, 0);
11637 tree targs = TREE_OPERAND (t, 1);
11639 fn = tsubst_copy (fn, args, complain, in_decl);
11640 if (targs)
11641 targs = tsubst_template_args (targs, args, complain, in_decl);
11643 return lookup_template_function (fn, targs);
11646 case TREE_LIST:
11648 tree purpose, value, chain;
11650 if (t == void_list_node)
11651 return t;
11653 purpose = TREE_PURPOSE (t);
11654 if (purpose)
11655 purpose = tsubst_copy (purpose, args, complain, in_decl);
11656 value = TREE_VALUE (t);
11657 if (value)
11658 value = tsubst_copy (value, args, complain, in_decl);
11659 chain = TREE_CHAIN (t);
11660 if (chain && chain != void_type_node)
11661 chain = tsubst_copy (chain, args, complain, in_decl);
11662 if (purpose == TREE_PURPOSE (t)
11663 && value == TREE_VALUE (t)
11664 && chain == TREE_CHAIN (t))
11665 return t;
11666 return tree_cons (purpose, value, chain);
11669 case RECORD_TYPE:
11670 case UNION_TYPE:
11671 case ENUMERAL_TYPE:
11672 case INTEGER_TYPE:
11673 case TEMPLATE_TYPE_PARM:
11674 case TEMPLATE_TEMPLATE_PARM:
11675 case BOUND_TEMPLATE_TEMPLATE_PARM:
11676 case TEMPLATE_PARM_INDEX:
11677 case POINTER_TYPE:
11678 case REFERENCE_TYPE:
11679 case OFFSET_TYPE:
11680 case FUNCTION_TYPE:
11681 case METHOD_TYPE:
11682 case ARRAY_TYPE:
11683 case TYPENAME_TYPE:
11684 case UNBOUND_CLASS_TEMPLATE:
11685 case TYPEOF_TYPE:
11686 case DECLTYPE_TYPE:
11687 case TYPE_DECL:
11688 return tsubst (t, args, complain, in_decl);
11690 case IDENTIFIER_NODE:
11691 if (IDENTIFIER_TYPENAME_P (t))
11693 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11694 return mangle_conv_op_name_for_type (new_type);
11696 else
11697 return t;
11699 case CONSTRUCTOR:
11700 /* This is handled by tsubst_copy_and_build. */
11701 gcc_unreachable ();
11703 case VA_ARG_EXPR:
11704 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11705 in_decl),
11706 tsubst (TREE_TYPE (t), args, complain, in_decl));
11708 case CLEANUP_POINT_EXPR:
11709 /* We shouldn't have built any of these during initial template
11710 generation. Instead, they should be built during instantiation
11711 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11712 gcc_unreachable ();
11714 case OFFSET_REF:
11715 r = build2
11716 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11717 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11718 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11719 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
11720 mark_used (TREE_OPERAND (r, 1));
11721 return r;
11723 case EXPR_PACK_EXPANSION:
11724 error ("invalid use of pack expansion expression");
11725 return error_mark_node;
11727 case NONTYPE_ARGUMENT_PACK:
11728 error ("use %<...%> to expand argument pack");
11729 return error_mark_node;
11731 case INTEGER_CST:
11732 case REAL_CST:
11733 case STRING_CST:
11734 case COMPLEX_CST:
11736 /* Instantiate any typedefs in the type. */
11737 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11738 r = fold_convert (type, t);
11739 gcc_assert (TREE_CODE (r) == code);
11740 return r;
11743 case PTRMEM_CST:
11744 /* These can sometimes show up in a partial instantiation, but never
11745 involve template parms. */
11746 gcc_assert (!uses_template_parms (t));
11747 return t;
11749 default:
11750 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
11751 gcc_checking_assert (false);
11752 return t;
11756 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11758 static tree
11759 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11760 tree in_decl)
11762 tree new_clauses = NULL, nc, oc;
11764 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11766 nc = copy_node (oc);
11767 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11768 new_clauses = nc;
11770 switch (OMP_CLAUSE_CODE (nc))
11772 case OMP_CLAUSE_LASTPRIVATE:
11773 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11775 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11776 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11777 in_decl, /*integral_constant_expression_p=*/false);
11778 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11779 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11781 /* FALLTHRU */
11782 case OMP_CLAUSE_PRIVATE:
11783 case OMP_CLAUSE_SHARED:
11784 case OMP_CLAUSE_FIRSTPRIVATE:
11785 case OMP_CLAUSE_REDUCTION:
11786 case OMP_CLAUSE_COPYIN:
11787 case OMP_CLAUSE_COPYPRIVATE:
11788 case OMP_CLAUSE_IF:
11789 case OMP_CLAUSE_NUM_THREADS:
11790 case OMP_CLAUSE_SCHEDULE:
11791 case OMP_CLAUSE_COLLAPSE:
11792 OMP_CLAUSE_OPERAND (nc, 0)
11793 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11794 in_decl, /*integral_constant_expression_p=*/false);
11795 break;
11796 case OMP_CLAUSE_NOWAIT:
11797 case OMP_CLAUSE_ORDERED:
11798 case OMP_CLAUSE_DEFAULT:
11799 case OMP_CLAUSE_UNTIED:
11800 break;
11801 default:
11802 gcc_unreachable ();
11806 return finish_omp_clauses (nreverse (new_clauses));
11809 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11811 static tree
11812 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11813 tree in_decl)
11815 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11817 tree purpose, value, chain;
11819 if (t == NULL)
11820 return t;
11822 if (TREE_CODE (t) != TREE_LIST)
11823 return tsubst_copy_and_build (t, args, complain, in_decl,
11824 /*function_p=*/false,
11825 /*integral_constant_expression_p=*/false);
11827 if (t == void_list_node)
11828 return t;
11830 purpose = TREE_PURPOSE (t);
11831 if (purpose)
11832 purpose = RECUR (purpose);
11833 value = TREE_VALUE (t);
11834 if (value && TREE_CODE (value) != LABEL_DECL)
11835 value = RECUR (value);
11836 chain = TREE_CHAIN (t);
11837 if (chain && chain != void_type_node)
11838 chain = RECUR (chain);
11839 return tree_cons (purpose, value, chain);
11840 #undef RECUR
11843 /* Substitute one OMP_FOR iterator. */
11845 static void
11846 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11847 tree condv, tree incrv, tree *clauses,
11848 tree args, tsubst_flags_t complain, tree in_decl,
11849 bool integral_constant_expression_p)
11851 #define RECUR(NODE) \
11852 tsubst_expr ((NODE), args, complain, in_decl, \
11853 integral_constant_expression_p)
11854 tree decl, init, cond, incr, auto_node;
11856 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11857 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11858 decl = RECUR (TREE_OPERAND (init, 0));
11859 init = TREE_OPERAND (init, 1);
11860 auto_node = type_uses_auto (TREE_TYPE (decl));
11861 if (auto_node && init)
11863 tree init_expr = init;
11864 if (TREE_CODE (init_expr) == DECL_EXPR)
11865 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11866 init_expr = RECUR (init_expr);
11867 TREE_TYPE (decl)
11868 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11870 gcc_assert (!type_dependent_expression_p (decl));
11872 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11874 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11875 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11876 if (TREE_CODE (incr) == MODIFY_EXPR)
11877 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11878 RECUR (TREE_OPERAND (incr, 1)),
11879 complain);
11880 else
11881 incr = RECUR (incr);
11882 TREE_VEC_ELT (declv, i) = decl;
11883 TREE_VEC_ELT (initv, i) = init;
11884 TREE_VEC_ELT (condv, i) = cond;
11885 TREE_VEC_ELT (incrv, i) = incr;
11886 return;
11889 if (init && TREE_CODE (init) != DECL_EXPR)
11891 tree c;
11892 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11894 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11895 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11896 && OMP_CLAUSE_DECL (c) == decl)
11897 break;
11898 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11899 && OMP_CLAUSE_DECL (c) == decl)
11900 error ("iteration variable %qD should not be firstprivate", decl);
11901 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11902 && OMP_CLAUSE_DECL (c) == decl)
11903 error ("iteration variable %qD should not be reduction", decl);
11905 if (c == NULL)
11907 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11908 OMP_CLAUSE_DECL (c) = decl;
11909 c = finish_omp_clauses (c);
11910 if (c)
11912 OMP_CLAUSE_CHAIN (c) = *clauses;
11913 *clauses = c;
11917 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11918 if (COMPARISON_CLASS_P (cond))
11919 cond = build2 (TREE_CODE (cond), boolean_type_node,
11920 RECUR (TREE_OPERAND (cond, 0)),
11921 RECUR (TREE_OPERAND (cond, 1)));
11922 else
11923 cond = RECUR (cond);
11924 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11925 switch (TREE_CODE (incr))
11927 case PREINCREMENT_EXPR:
11928 case PREDECREMENT_EXPR:
11929 case POSTINCREMENT_EXPR:
11930 case POSTDECREMENT_EXPR:
11931 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11932 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11933 break;
11934 case MODIFY_EXPR:
11935 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11936 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11938 tree rhs = TREE_OPERAND (incr, 1);
11939 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11940 RECUR (TREE_OPERAND (incr, 0)),
11941 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11942 RECUR (TREE_OPERAND (rhs, 0)),
11943 RECUR (TREE_OPERAND (rhs, 1))));
11945 else
11946 incr = RECUR (incr);
11947 break;
11948 case MODOP_EXPR:
11949 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11950 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11952 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11953 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11954 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11955 TREE_TYPE (decl), lhs,
11956 RECUR (TREE_OPERAND (incr, 2))));
11958 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11959 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11960 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11962 tree rhs = TREE_OPERAND (incr, 2);
11963 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11964 RECUR (TREE_OPERAND (incr, 0)),
11965 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11966 RECUR (TREE_OPERAND (rhs, 0)),
11967 RECUR (TREE_OPERAND (rhs, 1))));
11969 else
11970 incr = RECUR (incr);
11971 break;
11972 default:
11973 incr = RECUR (incr);
11974 break;
11977 TREE_VEC_ELT (declv, i) = decl;
11978 TREE_VEC_ELT (initv, i) = init;
11979 TREE_VEC_ELT (condv, i) = cond;
11980 TREE_VEC_ELT (incrv, i) = incr;
11981 #undef RECUR
11984 /* Like tsubst_copy for expressions, etc. but also does semantic
11985 processing. */
11987 static tree
11988 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11989 bool integral_constant_expression_p)
11991 #define RECUR(NODE) \
11992 tsubst_expr ((NODE), args, complain, in_decl, \
11993 integral_constant_expression_p)
11995 tree stmt, tmp;
11997 if (t == NULL_TREE || t == error_mark_node)
11998 return t;
12000 if (EXPR_HAS_LOCATION (t))
12001 input_location = EXPR_LOCATION (t);
12002 if (STATEMENT_CODE_P (TREE_CODE (t)))
12003 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12005 switch (TREE_CODE (t))
12007 case STATEMENT_LIST:
12009 tree_stmt_iterator i;
12010 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12011 RECUR (tsi_stmt (i));
12012 break;
12015 case CTOR_INITIALIZER:
12016 finish_mem_initializers (tsubst_initializer_list
12017 (TREE_OPERAND (t, 0), args));
12018 break;
12020 case RETURN_EXPR:
12021 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12022 break;
12024 case EXPR_STMT:
12025 tmp = RECUR (EXPR_STMT_EXPR (t));
12026 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12027 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12028 else
12029 finish_expr_stmt (tmp);
12030 break;
12032 case USING_STMT:
12033 do_using_directive (USING_STMT_NAMESPACE (t));
12034 break;
12036 case DECL_EXPR:
12038 tree decl, pattern_decl;
12039 tree init;
12041 pattern_decl = decl = DECL_EXPR_DECL (t);
12042 if (TREE_CODE (decl) == LABEL_DECL)
12043 finish_label_decl (DECL_NAME (decl));
12044 else if (TREE_CODE (decl) == USING_DECL)
12046 tree scope = USING_DECL_SCOPE (decl);
12047 tree name = DECL_NAME (decl);
12048 tree decl;
12050 scope = tsubst (scope, args, complain, in_decl);
12051 decl = lookup_qualified_name (scope, name,
12052 /*is_type_p=*/false,
12053 /*complain=*/false);
12054 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12055 qualified_name_lookup_error (scope, name, decl, input_location);
12056 else
12057 do_local_using_decl (decl, scope, name);
12059 else
12061 init = DECL_INITIAL (decl);
12062 decl = tsubst (decl, args, complain, in_decl);
12063 if (decl != error_mark_node)
12065 /* By marking the declaration as instantiated, we avoid
12066 trying to instantiate it. Since instantiate_decl can't
12067 handle local variables, and since we've already done
12068 all that needs to be done, that's the right thing to
12069 do. */
12070 if (TREE_CODE (decl) == VAR_DECL)
12071 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12072 if (TREE_CODE (decl) == VAR_DECL
12073 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12074 /* Anonymous aggregates are a special case. */
12075 finish_anon_union (decl);
12076 else
12078 int const_init = false;
12079 maybe_push_decl (decl);
12080 if (TREE_CODE (decl) == VAR_DECL
12081 && DECL_PRETTY_FUNCTION_P (decl))
12083 /* For __PRETTY_FUNCTION__ we have to adjust the
12084 initializer. */
12085 const char *const name
12086 = cxx_printable_name (current_function_decl, 2);
12087 init = cp_fname_init (name, &TREE_TYPE (decl));
12089 else
12091 tree t = RECUR (init);
12093 if (init && !t)
12095 /* If we had an initializer but it
12096 instantiated to nothing,
12097 value-initialize the object. This will
12098 only occur when the initializer was a
12099 pack expansion where the parameter packs
12100 used in that expansion were of length
12101 zero. */
12102 init = build_value_init (TREE_TYPE (decl),
12103 complain);
12104 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12105 init = get_target_expr_sfinae (init, complain);
12107 else
12108 init = t;
12111 if (TREE_CODE (decl) == VAR_DECL)
12112 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12113 (pattern_decl));
12114 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12119 /* A DECL_EXPR can also be used as an expression, in the condition
12120 clause of an if/for/while construct. */
12121 return decl;
12124 case FOR_STMT:
12125 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12126 RECUR (FOR_INIT_STMT (t));
12127 finish_for_init_stmt (stmt);
12128 tmp = RECUR (FOR_COND (t));
12129 finish_for_cond (tmp, stmt);
12130 tmp = RECUR (FOR_EXPR (t));
12131 finish_for_expr (tmp, stmt);
12132 RECUR (FOR_BODY (t));
12133 finish_for_stmt (stmt);
12134 break;
12136 case RANGE_FOR_STMT:
12138 tree decl, expr;
12139 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12140 decl = RANGE_FOR_DECL (t);
12141 decl = tsubst (decl, args, complain, in_decl);
12142 maybe_push_decl (decl);
12143 expr = RECUR (RANGE_FOR_EXPR (t));
12144 stmt = cp_convert_range_for (stmt, decl, expr);
12145 RECUR (RANGE_FOR_BODY (t));
12146 finish_for_stmt (stmt);
12148 break;
12150 case WHILE_STMT:
12151 stmt = begin_while_stmt ();
12152 tmp = RECUR (WHILE_COND (t));
12153 finish_while_stmt_cond (tmp, stmt);
12154 RECUR (WHILE_BODY (t));
12155 finish_while_stmt (stmt);
12156 break;
12158 case DO_STMT:
12159 stmt = begin_do_stmt ();
12160 RECUR (DO_BODY (t));
12161 finish_do_body (stmt);
12162 tmp = RECUR (DO_COND (t));
12163 finish_do_stmt (tmp, stmt);
12164 break;
12166 case IF_STMT:
12167 stmt = begin_if_stmt ();
12168 tmp = RECUR (IF_COND (t));
12169 finish_if_stmt_cond (tmp, stmt);
12170 RECUR (THEN_CLAUSE (t));
12171 finish_then_clause (stmt);
12173 if (ELSE_CLAUSE (t))
12175 begin_else_clause (stmt);
12176 RECUR (ELSE_CLAUSE (t));
12177 finish_else_clause (stmt);
12180 finish_if_stmt (stmt);
12181 break;
12183 case BIND_EXPR:
12184 if (BIND_EXPR_BODY_BLOCK (t))
12185 stmt = begin_function_body ();
12186 else
12187 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12188 ? BCS_TRY_BLOCK : 0);
12190 RECUR (BIND_EXPR_BODY (t));
12192 if (BIND_EXPR_BODY_BLOCK (t))
12193 finish_function_body (stmt);
12194 else
12195 finish_compound_stmt (stmt);
12196 break;
12198 case BREAK_STMT:
12199 finish_break_stmt ();
12200 break;
12202 case CONTINUE_STMT:
12203 finish_continue_stmt ();
12204 break;
12206 case SWITCH_STMT:
12207 stmt = begin_switch_stmt ();
12208 tmp = RECUR (SWITCH_STMT_COND (t));
12209 finish_switch_cond (tmp, stmt);
12210 RECUR (SWITCH_STMT_BODY (t));
12211 finish_switch_stmt (stmt);
12212 break;
12214 case CASE_LABEL_EXPR:
12215 finish_case_label (EXPR_LOCATION (t),
12216 RECUR (CASE_LOW (t)),
12217 RECUR (CASE_HIGH (t)));
12218 break;
12220 case LABEL_EXPR:
12222 tree decl = LABEL_EXPR_LABEL (t);
12223 tree label;
12225 label = finish_label_stmt (DECL_NAME (decl));
12226 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12227 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12229 break;
12231 case GOTO_EXPR:
12232 tmp = GOTO_DESTINATION (t);
12233 if (TREE_CODE (tmp) != LABEL_DECL)
12234 /* Computed goto's must be tsubst'd into. On the other hand,
12235 non-computed gotos must not be; the identifier in question
12236 will have no binding. */
12237 tmp = RECUR (tmp);
12238 else
12239 tmp = DECL_NAME (tmp);
12240 finish_goto_stmt (tmp);
12241 break;
12243 case ASM_EXPR:
12244 tmp = finish_asm_stmt
12245 (ASM_VOLATILE_P (t),
12246 RECUR (ASM_STRING (t)),
12247 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12248 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12249 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12250 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12252 tree asm_expr = tmp;
12253 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12254 asm_expr = TREE_OPERAND (asm_expr, 0);
12255 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12257 break;
12259 case TRY_BLOCK:
12260 if (CLEANUP_P (t))
12262 stmt = begin_try_block ();
12263 RECUR (TRY_STMTS (t));
12264 finish_cleanup_try_block (stmt);
12265 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12267 else
12269 tree compound_stmt = NULL_TREE;
12271 if (FN_TRY_BLOCK_P (t))
12272 stmt = begin_function_try_block (&compound_stmt);
12273 else
12274 stmt = begin_try_block ();
12276 RECUR (TRY_STMTS (t));
12278 if (FN_TRY_BLOCK_P (t))
12279 finish_function_try_block (stmt);
12280 else
12281 finish_try_block (stmt);
12283 RECUR (TRY_HANDLERS (t));
12284 if (FN_TRY_BLOCK_P (t))
12285 finish_function_handler_sequence (stmt, compound_stmt);
12286 else
12287 finish_handler_sequence (stmt);
12289 break;
12291 case HANDLER:
12293 tree decl = HANDLER_PARMS (t);
12295 if (decl)
12297 decl = tsubst (decl, args, complain, in_decl);
12298 /* Prevent instantiate_decl from trying to instantiate
12299 this variable. We've already done all that needs to be
12300 done. */
12301 if (decl != error_mark_node)
12302 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12304 stmt = begin_handler ();
12305 finish_handler_parms (decl, stmt);
12306 RECUR (HANDLER_BODY (t));
12307 finish_handler (stmt);
12309 break;
12311 case TAG_DEFN:
12312 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12313 break;
12315 case STATIC_ASSERT:
12317 tree condition =
12318 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12319 args,
12320 complain, in_decl,
12321 /*integral_constant_expression_p=*/true);
12322 finish_static_assert (condition,
12323 STATIC_ASSERT_MESSAGE (t),
12324 STATIC_ASSERT_SOURCE_LOCATION (t),
12325 /*member_p=*/false);
12327 break;
12329 case OMP_PARALLEL:
12330 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
12331 args, complain, in_decl);
12332 stmt = begin_omp_parallel ();
12333 RECUR (OMP_PARALLEL_BODY (t));
12334 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
12335 = OMP_PARALLEL_COMBINED (t);
12336 break;
12338 case OMP_TASK:
12339 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
12340 args, complain, in_decl);
12341 stmt = begin_omp_task ();
12342 RECUR (OMP_TASK_BODY (t));
12343 finish_omp_task (tmp, stmt);
12344 break;
12346 case OMP_FOR:
12348 tree clauses, body, pre_body;
12349 tree declv, initv, condv, incrv;
12350 int i;
12352 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
12353 args, complain, in_decl);
12354 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12355 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12356 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12357 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12359 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
12360 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
12361 &clauses, args, complain, in_decl,
12362 integral_constant_expression_p);
12364 stmt = begin_omp_structured_block ();
12366 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
12367 if (TREE_VEC_ELT (initv, i) == NULL
12368 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
12369 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
12370 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
12372 tree init = RECUR (TREE_VEC_ELT (initv, i));
12373 gcc_assert (init == TREE_VEC_ELT (declv, i));
12374 TREE_VEC_ELT (initv, i) = NULL_TREE;
12376 else
12378 tree decl_expr = TREE_VEC_ELT (initv, i);
12379 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
12380 gcc_assert (init != NULL);
12381 TREE_VEC_ELT (initv, i) = RECUR (init);
12382 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
12383 RECUR (decl_expr);
12384 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
12387 pre_body = push_stmt_list ();
12388 RECUR (OMP_FOR_PRE_BODY (t));
12389 pre_body = pop_stmt_list (pre_body);
12391 body = push_stmt_list ();
12392 RECUR (OMP_FOR_BODY (t));
12393 body = pop_stmt_list (body);
12395 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12396 body, pre_body, clauses);
12398 add_stmt (finish_omp_structured_block (stmt));
12400 break;
12402 case OMP_SECTIONS:
12403 case OMP_SINGLE:
12404 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12405 stmt = push_stmt_list ();
12406 RECUR (OMP_BODY (t));
12407 stmt = pop_stmt_list (stmt);
12409 t = copy_node (t);
12410 OMP_BODY (t) = stmt;
12411 OMP_CLAUSES (t) = tmp;
12412 add_stmt (t);
12413 break;
12415 case OMP_SECTION:
12416 case OMP_CRITICAL:
12417 case OMP_MASTER:
12418 case OMP_ORDERED:
12419 stmt = push_stmt_list ();
12420 RECUR (OMP_BODY (t));
12421 stmt = pop_stmt_list (stmt);
12423 t = copy_node (t);
12424 OMP_BODY (t) = stmt;
12425 add_stmt (t);
12426 break;
12428 case OMP_ATOMIC:
12429 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12431 tree op1 = TREE_OPERAND (t, 1);
12432 tree lhs = RECUR (TREE_OPERAND (op1, 0));
12433 tree rhs = RECUR (TREE_OPERAND (op1, 1));
12434 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
12436 break;
12438 case EXPR_PACK_EXPANSION:
12439 error ("invalid use of pack expansion expression");
12440 return error_mark_node;
12442 case NONTYPE_ARGUMENT_PACK:
12443 error ("use %<...%> to expand argument pack");
12444 return error_mark_node;
12446 default:
12447 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
12449 return tsubst_copy_and_build (t, args, complain, in_decl,
12450 /*function_p=*/false,
12451 integral_constant_expression_p);
12454 return NULL_TREE;
12455 #undef RECUR
12458 /* T is a postfix-expression that is not being used in a function
12459 call. Return the substituted version of T. */
12461 static tree
12462 tsubst_non_call_postfix_expression (tree t, tree args,
12463 tsubst_flags_t complain,
12464 tree in_decl)
12466 if (TREE_CODE (t) == SCOPE_REF)
12467 t = tsubst_qualified_id (t, args, complain, in_decl,
12468 /*done=*/false, /*address_p=*/false);
12469 else
12470 t = tsubst_copy_and_build (t, args, complain, in_decl,
12471 /*function_p=*/false,
12472 /*integral_constant_expression_p=*/false);
12474 return t;
12477 /* Like tsubst but deals with expressions and performs semantic
12478 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12480 tree
12481 tsubst_copy_and_build (tree t,
12482 tree args,
12483 tsubst_flags_t complain,
12484 tree in_decl,
12485 bool function_p,
12486 bool integral_constant_expression_p)
12488 #define RECUR(NODE) \
12489 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12490 /*function_p=*/false, \
12491 integral_constant_expression_p)
12493 tree op1;
12495 if (t == NULL_TREE || t == error_mark_node)
12496 return t;
12498 switch (TREE_CODE (t))
12500 case USING_DECL:
12501 t = DECL_NAME (t);
12502 /* Fall through. */
12503 case IDENTIFIER_NODE:
12505 tree decl;
12506 cp_id_kind idk;
12507 bool non_integral_constant_expression_p;
12508 const char *error_msg;
12510 if (IDENTIFIER_TYPENAME_P (t))
12512 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12513 t = mangle_conv_op_name_for_type (new_type);
12516 /* Look up the name. */
12517 decl = lookup_name (t);
12519 /* By convention, expressions use ERROR_MARK_NODE to indicate
12520 failure, not NULL_TREE. */
12521 if (decl == NULL_TREE)
12522 decl = error_mark_node;
12524 decl = finish_id_expression (t, decl, NULL_TREE,
12525 &idk,
12526 integral_constant_expression_p,
12527 /*allow_non_integral_constant_expression_p=*/false,
12528 &non_integral_constant_expression_p,
12529 /*template_p=*/false,
12530 /*done=*/true,
12531 /*address_p=*/false,
12532 /*template_arg_p=*/false,
12533 &error_msg,
12534 input_location);
12535 if (error_msg)
12536 error (error_msg);
12537 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12538 decl = unqualified_name_lookup_error (decl);
12539 return decl;
12542 case TEMPLATE_ID_EXPR:
12544 tree object;
12545 tree templ = RECUR (TREE_OPERAND (t, 0));
12546 tree targs = TREE_OPERAND (t, 1);
12548 if (targs)
12549 targs = tsubst_template_args (targs, args, complain, in_decl);
12551 if (TREE_CODE (templ) == COMPONENT_REF)
12553 object = TREE_OPERAND (templ, 0);
12554 templ = TREE_OPERAND (templ, 1);
12556 else
12557 object = NULL_TREE;
12558 templ = lookup_template_function (templ, targs);
12560 if (object)
12561 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12562 object, templ, NULL_TREE);
12563 else
12564 return baselink_for_fns (templ);
12567 case INDIRECT_REF:
12569 tree r = RECUR (TREE_OPERAND (t, 0));
12571 if (REFERENCE_REF_P (t))
12573 /* A type conversion to reference type will be enclosed in
12574 such an indirect ref, but the substitution of the cast
12575 will have also added such an indirect ref. */
12576 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12577 r = convert_from_reference (r);
12579 else
12580 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12581 return r;
12584 case NOP_EXPR:
12585 return build_nop
12586 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12587 RECUR (TREE_OPERAND (t, 0)));
12589 case CAST_EXPR:
12590 case REINTERPRET_CAST_EXPR:
12591 case CONST_CAST_EXPR:
12592 case DYNAMIC_CAST_EXPR:
12593 case STATIC_CAST_EXPR:
12595 tree type;
12596 tree op;
12598 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12599 if (integral_constant_expression_p
12600 && !cast_valid_in_integral_constant_expression_p (type))
12602 if (complain & tf_error)
12603 error ("a cast to a type other than an integral or "
12604 "enumeration type cannot appear in a constant-expression");
12605 return error_mark_node;
12608 op = RECUR (TREE_OPERAND (t, 0));
12610 switch (TREE_CODE (t))
12612 case CAST_EXPR:
12613 return build_functional_cast (type, op, complain);
12614 case REINTERPRET_CAST_EXPR:
12615 return build_reinterpret_cast (type, op, complain);
12616 case CONST_CAST_EXPR:
12617 return build_const_cast (type, op, complain);
12618 case DYNAMIC_CAST_EXPR:
12619 return build_dynamic_cast (type, op, complain);
12620 case STATIC_CAST_EXPR:
12621 return build_static_cast (type, op, complain);
12622 default:
12623 gcc_unreachable ();
12627 case POSTDECREMENT_EXPR:
12628 case POSTINCREMENT_EXPR:
12629 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12630 args, complain, in_decl);
12631 return build_x_unary_op (TREE_CODE (t), op1, complain);
12633 case PREDECREMENT_EXPR:
12634 case PREINCREMENT_EXPR:
12635 case NEGATE_EXPR:
12636 case BIT_NOT_EXPR:
12637 case ABS_EXPR:
12638 case TRUTH_NOT_EXPR:
12639 case UNARY_PLUS_EXPR: /* Unary + */
12640 case REALPART_EXPR:
12641 case IMAGPART_EXPR:
12642 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
12643 complain);
12645 case ADDR_EXPR:
12646 op1 = TREE_OPERAND (t, 0);
12647 if (TREE_CODE (op1) == LABEL_DECL)
12648 return finish_label_address_expr (DECL_NAME (op1),
12649 EXPR_LOCATION (op1));
12650 if (TREE_CODE (op1) == SCOPE_REF)
12651 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12652 /*done=*/true, /*address_p=*/true);
12653 else
12654 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12655 in_decl);
12656 return build_x_unary_op (ADDR_EXPR, op1, complain);
12658 case PLUS_EXPR:
12659 case MINUS_EXPR:
12660 case MULT_EXPR:
12661 case TRUNC_DIV_EXPR:
12662 case CEIL_DIV_EXPR:
12663 case FLOOR_DIV_EXPR:
12664 case ROUND_DIV_EXPR:
12665 case EXACT_DIV_EXPR:
12666 case BIT_AND_EXPR:
12667 case BIT_IOR_EXPR:
12668 case BIT_XOR_EXPR:
12669 case TRUNC_MOD_EXPR:
12670 case FLOOR_MOD_EXPR:
12671 case TRUTH_ANDIF_EXPR:
12672 case TRUTH_ORIF_EXPR:
12673 case TRUTH_AND_EXPR:
12674 case TRUTH_OR_EXPR:
12675 case RSHIFT_EXPR:
12676 case LSHIFT_EXPR:
12677 case RROTATE_EXPR:
12678 case LROTATE_EXPR:
12679 case EQ_EXPR:
12680 case NE_EXPR:
12681 case MAX_EXPR:
12682 case MIN_EXPR:
12683 case LE_EXPR:
12684 case GE_EXPR:
12685 case LT_EXPR:
12686 case GT_EXPR:
12687 case MEMBER_REF:
12688 case DOTSTAR_EXPR:
12689 return build_x_binary_op
12690 (TREE_CODE (t),
12691 RECUR (TREE_OPERAND (t, 0)),
12692 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
12693 ? ERROR_MARK
12694 : TREE_CODE (TREE_OPERAND (t, 0))),
12695 RECUR (TREE_OPERAND (t, 1)),
12696 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
12697 ? ERROR_MARK
12698 : TREE_CODE (TREE_OPERAND (t, 1))),
12699 /*overloaded_p=*/NULL,
12700 complain);
12702 case SCOPE_REF:
12703 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12704 /*address_p=*/false);
12705 case ARRAY_REF:
12706 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12707 args, complain, in_decl);
12708 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12710 case SIZEOF_EXPR:
12711 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12712 return tsubst_copy (t, args, complain, in_decl);
12713 /* Fall through */
12715 case ALIGNOF_EXPR:
12716 op1 = TREE_OPERAND (t, 0);
12717 if (!args)
12719 /* When there are no ARGS, we are trying to evaluate a
12720 non-dependent expression from the parser. Trying to do
12721 the substitutions may not work. */
12722 if (!TYPE_P (op1))
12723 op1 = TREE_TYPE (op1);
12725 else
12727 ++cp_unevaluated_operand;
12728 ++c_inhibit_evaluation_warnings;
12729 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12730 /*function_p=*/false,
12731 /*integral_constant_expression_p=*/false);
12732 --cp_unevaluated_operand;
12733 --c_inhibit_evaluation_warnings;
12735 if (TYPE_P (op1))
12736 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12737 complain & tf_error);
12738 else
12739 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12740 complain & tf_error);
12742 case AT_ENCODE_EXPR:
12744 op1 = TREE_OPERAND (t, 0);
12745 ++cp_unevaluated_operand;
12746 ++c_inhibit_evaluation_warnings;
12747 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12748 /*function_p=*/false,
12749 /*integral_constant_expression_p=*/false);
12750 --cp_unevaluated_operand;
12751 --c_inhibit_evaluation_warnings;
12752 return objc_build_encode_expr (op1);
12755 case NOEXCEPT_EXPR:
12756 op1 = TREE_OPERAND (t, 0);
12757 ++cp_unevaluated_operand;
12758 ++c_inhibit_evaluation_warnings;
12759 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12760 /*function_p=*/false,
12761 /*integral_constant_expression_p=*/false);
12762 --cp_unevaluated_operand;
12763 --c_inhibit_evaluation_warnings;
12764 return finish_noexcept_expr (op1, complain);
12766 case MODOP_EXPR:
12768 tree r = build_x_modify_expr
12769 (RECUR (TREE_OPERAND (t, 0)),
12770 TREE_CODE (TREE_OPERAND (t, 1)),
12771 RECUR (TREE_OPERAND (t, 2)),
12772 complain);
12773 /* TREE_NO_WARNING must be set if either the expression was
12774 parenthesized or it uses an operator such as >>= rather
12775 than plain assignment. In the former case, it was already
12776 set and must be copied. In the latter case,
12777 build_x_modify_expr sets it and it must not be reset
12778 here. */
12779 if (TREE_NO_WARNING (t))
12780 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12781 return r;
12784 case ARROW_EXPR:
12785 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12786 args, complain, in_decl);
12787 /* Remember that there was a reference to this entity. */
12788 if (DECL_P (op1))
12789 mark_used (op1);
12790 return build_x_arrow (op1);
12792 case NEW_EXPR:
12794 tree placement = RECUR (TREE_OPERAND (t, 0));
12795 tree init = RECUR (TREE_OPERAND (t, 3));
12796 VEC(tree,gc) *placement_vec;
12797 VEC(tree,gc) *init_vec;
12798 tree ret;
12800 if (placement == NULL_TREE)
12801 placement_vec = NULL;
12802 else
12804 placement_vec = make_tree_vector ();
12805 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12806 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12809 /* If there was an initializer in the original tree, but it
12810 instantiated to an empty list, then we should pass a
12811 non-NULL empty vector to tell build_new that it was an
12812 empty initializer() rather than no initializer. This can
12813 only happen when the initializer is a pack expansion whose
12814 parameter packs are of length zero. */
12815 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12816 init_vec = NULL;
12817 else
12819 init_vec = make_tree_vector ();
12820 if (init == void_zero_node)
12821 gcc_assert (init_vec != NULL);
12822 else
12824 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12825 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12829 ret = build_new (&placement_vec,
12830 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
12831 RECUR (TREE_OPERAND (t, 2)),
12832 &init_vec,
12833 NEW_EXPR_USE_GLOBAL (t),
12834 complain);
12836 if (placement_vec != NULL)
12837 release_tree_vector (placement_vec);
12838 if (init_vec != NULL)
12839 release_tree_vector (init_vec);
12841 return ret;
12844 case DELETE_EXPR:
12845 return delete_sanity
12846 (RECUR (TREE_OPERAND (t, 0)),
12847 RECUR (TREE_OPERAND (t, 1)),
12848 DELETE_EXPR_USE_VEC (t),
12849 DELETE_EXPR_USE_GLOBAL (t),
12850 complain);
12852 case COMPOUND_EXPR:
12853 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12854 RECUR (TREE_OPERAND (t, 1)),
12855 complain);
12857 case CALL_EXPR:
12859 tree function;
12860 VEC(tree,gc) *call_args;
12861 unsigned int nargs, i;
12862 bool qualified_p;
12863 bool koenig_p;
12864 tree ret;
12866 function = CALL_EXPR_FN (t);
12867 /* When we parsed the expression, we determined whether or
12868 not Koenig lookup should be performed. */
12869 koenig_p = KOENIG_LOOKUP_P (t);
12870 if (TREE_CODE (function) == SCOPE_REF)
12872 qualified_p = true;
12873 function = tsubst_qualified_id (function, args, complain, in_decl,
12874 /*done=*/false,
12875 /*address_p=*/false);
12877 else
12879 if (TREE_CODE (function) == COMPONENT_REF)
12881 tree op = TREE_OPERAND (function, 1);
12883 qualified_p = (TREE_CODE (op) == SCOPE_REF
12884 || (BASELINK_P (op)
12885 && BASELINK_QUALIFIED_P (op)));
12887 else
12888 qualified_p = false;
12890 function = tsubst_copy_and_build (function, args, complain,
12891 in_decl,
12892 !qualified_p,
12893 integral_constant_expression_p);
12895 if (BASELINK_P (function))
12896 qualified_p = true;
12899 nargs = call_expr_nargs (t);
12900 call_args = make_tree_vector ();
12901 for (i = 0; i < nargs; ++i)
12903 tree arg = CALL_EXPR_ARG (t, i);
12905 if (!PACK_EXPANSION_P (arg))
12906 VEC_safe_push (tree, gc, call_args,
12907 RECUR (CALL_EXPR_ARG (t, i)));
12908 else
12910 /* Expand the pack expansion and push each entry onto
12911 CALL_ARGS. */
12912 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12913 if (TREE_CODE (arg) == TREE_VEC)
12915 unsigned int len, j;
12917 len = TREE_VEC_LENGTH (arg);
12918 for (j = 0; j < len; ++j)
12920 tree value = TREE_VEC_ELT (arg, j);
12921 if (value != NULL_TREE)
12922 value = convert_from_reference (value);
12923 VEC_safe_push (tree, gc, call_args, value);
12926 else
12928 /* A partial substitution. Add one entry. */
12929 VEC_safe_push (tree, gc, call_args, arg);
12934 /* We do not perform argument-dependent lookup if normal
12935 lookup finds a non-function, in accordance with the
12936 expected resolution of DR 218. */
12937 if (koenig_p
12938 && ((is_overloaded_fn (function)
12939 /* If lookup found a member function, the Koenig lookup is
12940 not appropriate, even if an unqualified-name was used
12941 to denote the function. */
12942 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12943 || TREE_CODE (function) == IDENTIFIER_NODE)
12944 /* Only do this when substitution turns a dependent call
12945 into a non-dependent call. */
12946 && type_dependent_expression_p_push (t)
12947 && !any_type_dependent_arguments_p (call_args))
12948 function = perform_koenig_lookup (function, call_args, false);
12950 if (TREE_CODE (function) == IDENTIFIER_NODE
12951 && !processing_template_decl)
12953 unqualified_name_lookup_error (function);
12954 release_tree_vector (call_args);
12955 return error_mark_node;
12958 /* Remember that there was a reference to this entity. */
12959 if (DECL_P (function))
12960 mark_used (function);
12962 if (TREE_CODE (function) == OFFSET_REF)
12963 ret = build_offset_ref_call_from_tree (function, &call_args);
12964 else if (TREE_CODE (function) == COMPONENT_REF)
12966 tree instance = TREE_OPERAND (function, 0);
12967 tree fn = TREE_OPERAND (function, 1);
12969 if (processing_template_decl
12970 && (type_dependent_expression_p (instance)
12971 || (!BASELINK_P (fn)
12972 && TREE_CODE (fn) != FIELD_DECL)
12973 || type_dependent_expression_p (fn)
12974 || any_type_dependent_arguments_p (call_args)))
12975 ret = build_nt_call_vec (function, call_args);
12976 else if (!BASELINK_P (fn))
12977 ret = finish_call_expr (function, &call_args,
12978 /*disallow_virtual=*/false,
12979 /*koenig_p=*/false,
12980 complain);
12981 else
12982 ret = (build_new_method_call
12983 (instance, fn,
12984 &call_args, NULL_TREE,
12985 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12986 /*fn_p=*/NULL,
12987 complain));
12989 else
12990 ret = finish_call_expr (function, &call_args,
12991 /*disallow_virtual=*/qualified_p,
12992 koenig_p,
12993 complain);
12995 release_tree_vector (call_args);
12997 return ret;
13000 case COND_EXPR:
13001 return build_x_conditional_expr
13002 (RECUR (TREE_OPERAND (t, 0)),
13003 RECUR (TREE_OPERAND (t, 1)),
13004 RECUR (TREE_OPERAND (t, 2)),
13005 complain);
13007 case PSEUDO_DTOR_EXPR:
13008 return finish_pseudo_destructor_expr
13009 (RECUR (TREE_OPERAND (t, 0)),
13010 RECUR (TREE_OPERAND (t, 1)),
13011 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl));
13013 case TREE_LIST:
13015 tree purpose, value, chain;
13017 if (t == void_list_node)
13018 return t;
13020 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13021 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13023 /* We have pack expansions, so expand those and
13024 create a new list out of it. */
13025 tree purposevec = NULL_TREE;
13026 tree valuevec = NULL_TREE;
13027 tree chain;
13028 int i, len = -1;
13030 /* Expand the argument expressions. */
13031 if (TREE_PURPOSE (t))
13032 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13033 complain, in_decl);
13034 if (TREE_VALUE (t))
13035 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13036 complain, in_decl);
13038 /* Build the rest of the list. */
13039 chain = TREE_CHAIN (t);
13040 if (chain && chain != void_type_node)
13041 chain = RECUR (chain);
13043 /* Determine the number of arguments. */
13044 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13046 len = TREE_VEC_LENGTH (purposevec);
13047 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13049 else if (TREE_CODE (valuevec) == TREE_VEC)
13050 len = TREE_VEC_LENGTH (valuevec);
13051 else
13053 /* Since we only performed a partial substitution into
13054 the argument pack, we only return a single list
13055 node. */
13056 if (purposevec == TREE_PURPOSE (t)
13057 && valuevec == TREE_VALUE (t)
13058 && chain == TREE_CHAIN (t))
13059 return t;
13061 return tree_cons (purposevec, valuevec, chain);
13064 /* Convert the argument vectors into a TREE_LIST */
13065 i = len;
13066 while (i > 0)
13068 /* Grab the Ith values. */
13069 i--;
13070 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13071 : NULL_TREE;
13072 value
13073 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
13074 : NULL_TREE;
13076 /* Build the list (backwards). */
13077 chain = tree_cons (purpose, value, chain);
13080 return chain;
13083 purpose = TREE_PURPOSE (t);
13084 if (purpose)
13085 purpose = RECUR (purpose);
13086 value = TREE_VALUE (t);
13087 if (value)
13088 value = RECUR (value);
13089 chain = TREE_CHAIN (t);
13090 if (chain && chain != void_type_node)
13091 chain = RECUR (chain);
13092 if (purpose == TREE_PURPOSE (t)
13093 && value == TREE_VALUE (t)
13094 && chain == TREE_CHAIN (t))
13095 return t;
13096 return tree_cons (purpose, value, chain);
13099 case COMPONENT_REF:
13101 tree object;
13102 tree object_type;
13103 tree member;
13105 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13106 args, complain, in_decl);
13107 /* Remember that there was a reference to this entity. */
13108 if (DECL_P (object))
13109 mark_used (object);
13110 object_type = TREE_TYPE (object);
13112 member = TREE_OPERAND (t, 1);
13113 if (BASELINK_P (member))
13114 member = tsubst_baselink (member,
13115 non_reference (TREE_TYPE (object)),
13116 args, complain, in_decl);
13117 else
13118 member = tsubst_copy (member, args, complain, in_decl);
13119 if (member == error_mark_node)
13120 return error_mark_node;
13122 if (object_type && !CLASS_TYPE_P (object_type))
13124 if (SCALAR_TYPE_P (object_type))
13126 tree s = NULL_TREE;
13127 tree dtor = member;
13129 if (TREE_CODE (dtor) == SCOPE_REF)
13131 s = TREE_OPERAND (dtor, 0);
13132 dtor = TREE_OPERAND (dtor, 1);
13134 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
13136 dtor = TREE_OPERAND (dtor, 0);
13137 if (TYPE_P (dtor))
13138 return finish_pseudo_destructor_expr (object, s, dtor);
13142 else if (TREE_CODE (member) == SCOPE_REF
13143 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
13145 tree tmpl;
13146 tree args;
13148 /* Lookup the template functions now that we know what the
13149 scope is. */
13150 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
13151 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
13152 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
13153 /*is_type_p=*/false,
13154 /*complain=*/false);
13155 if (BASELINK_P (member))
13157 BASELINK_FUNCTIONS (member)
13158 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
13159 args);
13160 member = (adjust_result_of_qualified_name_lookup
13161 (member, BINFO_TYPE (BASELINK_BINFO (member)),
13162 object_type));
13164 else
13166 qualified_name_lookup_error (object_type, tmpl, member,
13167 input_location);
13168 return error_mark_node;
13171 else if (TREE_CODE (member) == SCOPE_REF
13172 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
13173 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
13175 if (complain & tf_error)
13177 if (TYPE_P (TREE_OPERAND (member, 0)))
13178 error ("%qT is not a class or namespace",
13179 TREE_OPERAND (member, 0));
13180 else
13181 error ("%qD is not a class or namespace",
13182 TREE_OPERAND (member, 0));
13184 return error_mark_node;
13186 else if (TREE_CODE (member) == FIELD_DECL)
13187 return finish_non_static_data_member (member, object, NULL_TREE);
13189 return finish_class_member_access_expr (object, member,
13190 /*template_p=*/false,
13191 complain);
13194 case THROW_EXPR:
13195 return build_throw
13196 (RECUR (TREE_OPERAND (t, 0)));
13198 case CONSTRUCTOR:
13200 VEC(constructor_elt,gc) *n;
13201 constructor_elt *ce;
13202 unsigned HOST_WIDE_INT idx;
13203 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13204 bool process_index_p;
13205 int newlen;
13206 bool need_copy_p = false;
13207 tree r;
13209 if (type == error_mark_node)
13210 return error_mark_node;
13212 /* digest_init will do the wrong thing if we let it. */
13213 if (type && TYPE_PTRMEMFUNC_P (type))
13214 return t;
13216 /* We do not want to process the index of aggregate
13217 initializers as they are identifier nodes which will be
13218 looked up by digest_init. */
13219 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
13221 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
13222 newlen = VEC_length (constructor_elt, n);
13223 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
13225 if (ce->index && process_index_p)
13226 ce->index = RECUR (ce->index);
13228 if (PACK_EXPANSION_P (ce->value))
13230 /* Substitute into the pack expansion. */
13231 ce->value = tsubst_pack_expansion (ce->value, args, complain,
13232 in_decl);
13234 if (ce->value == error_mark_node)
13236 else if (TREE_VEC_LENGTH (ce->value) == 1)
13237 /* Just move the argument into place. */
13238 ce->value = TREE_VEC_ELT (ce->value, 0);
13239 else
13241 /* Update the length of the final CONSTRUCTOR
13242 arguments vector, and note that we will need to
13243 copy.*/
13244 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
13245 need_copy_p = true;
13248 else
13249 ce->value = RECUR (ce->value);
13252 if (need_copy_p)
13254 VEC(constructor_elt,gc) *old_n = n;
13256 n = VEC_alloc (constructor_elt, gc, newlen);
13257 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
13259 if (TREE_CODE (ce->value) == TREE_VEC)
13261 int i, len = TREE_VEC_LENGTH (ce->value);
13262 for (i = 0; i < len; ++i)
13263 CONSTRUCTOR_APPEND_ELT (n, 0,
13264 TREE_VEC_ELT (ce->value, i));
13266 else
13267 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
13271 r = build_constructor (init_list_type_node, n);
13272 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
13274 if (TREE_HAS_CONSTRUCTOR (t))
13275 return finish_compound_literal (type, r, complain);
13277 TREE_TYPE (r) = type;
13278 return r;
13281 case TYPEID_EXPR:
13283 tree operand_0 = TREE_OPERAND (t, 0);
13284 if (TYPE_P (operand_0))
13286 operand_0 = tsubst (operand_0, args, complain, in_decl);
13287 return get_typeid (operand_0);
13289 else
13291 operand_0 = RECUR (operand_0);
13292 return build_typeid (operand_0);
13296 case VAR_DECL:
13297 if (!args)
13298 return t;
13299 /* Fall through */
13301 case PARM_DECL:
13303 tree r = tsubst_copy (t, args, complain, in_decl);
13305 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
13306 /* If the original type was a reference, we'll be wrapped in
13307 the appropriate INDIRECT_REF. */
13308 r = convert_from_reference (r);
13309 return r;
13312 case VA_ARG_EXPR:
13313 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
13314 tsubst (TREE_TYPE (t), args, complain, in_decl));
13316 case OFFSETOF_EXPR:
13317 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
13319 case TRAIT_EXPR:
13321 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
13322 complain, in_decl);
13324 tree type2 = TRAIT_EXPR_TYPE2 (t);
13325 if (type2)
13326 type2 = tsubst_copy (type2, args, complain, in_decl);
13328 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
13331 case STMT_EXPR:
13333 tree old_stmt_expr = cur_stmt_expr;
13334 tree stmt_expr = begin_stmt_expr ();
13336 cur_stmt_expr = stmt_expr;
13337 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
13338 integral_constant_expression_p);
13339 stmt_expr = finish_stmt_expr (stmt_expr, false);
13340 cur_stmt_expr = old_stmt_expr;
13342 /* If the resulting list of expression statement is empty,
13343 fold it further into void_zero_node. */
13344 if (empty_expr_stmt_p (stmt_expr))
13345 stmt_expr = void_zero_node;
13347 return stmt_expr;
13350 case CONST_DECL:
13351 t = tsubst_copy (t, args, complain, in_decl);
13352 /* As in finish_id_expression, we resolve enumeration constants
13353 to their underlying values. */
13354 if (TREE_CODE (t) == CONST_DECL)
13356 used_types_insert (TREE_TYPE (t));
13357 return DECL_INITIAL (t);
13359 return t;
13361 case LAMBDA_EXPR:
13363 tree r = build_lambda_expr ();
13365 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13366 TREE_TYPE (r) = type;
13367 CLASSTYPE_LAMBDA_EXPR (type) = r;
13369 LAMBDA_EXPR_LOCATION (r)
13370 = LAMBDA_EXPR_LOCATION (t);
13371 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
13372 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
13373 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
13374 LAMBDA_EXPR_DISCRIMINATOR (r)
13375 = (LAMBDA_EXPR_DISCRIMINATOR (t));
13376 LAMBDA_EXPR_CAPTURE_LIST (r)
13377 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
13378 LAMBDA_EXPR_THIS_CAPTURE (r)
13379 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
13380 LAMBDA_EXPR_EXTRA_SCOPE (r)
13381 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
13383 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13384 determine_visibility (TYPE_NAME (type));
13385 /* Now that we know visibility, instantiate the type so we have a
13386 declaration of the op() for later calls to lambda_function. */
13387 complete_type (type);
13389 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
13390 if (type)
13391 apply_lambda_return_type (r, type);
13393 return build_lambda_object (r);
13396 case TARGET_EXPR:
13397 /* We can get here for a constant initializer of non-dependent type.
13398 FIXME stop folding in cp_parser_initializer_clause. */
13399 gcc_assert (TREE_CONSTANT (t));
13401 tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t)));
13402 TREE_CONSTANT (r) = true;
13403 return r;
13406 default:
13407 /* Handle Objective-C++ constructs, if appropriate. */
13409 tree subst
13410 = objcp_tsubst_copy_and_build (t, args, complain,
13411 in_decl, /*function_p=*/false);
13412 if (subst)
13413 return subst;
13415 return tsubst_copy (t, args, complain, in_decl);
13418 #undef RECUR
13421 /* Verify that the instantiated ARGS are valid. For type arguments,
13422 make sure that the type's linkage is ok. For non-type arguments,
13423 make sure they are constants if they are integral or enumerations.
13424 Emit an error under control of COMPLAIN, and return TRUE on error. */
13426 static bool
13427 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
13429 if (ARGUMENT_PACK_P (t))
13431 tree vec = ARGUMENT_PACK_ARGS (t);
13432 int len = TREE_VEC_LENGTH (vec);
13433 bool result = false;
13434 int i;
13436 for (i = 0; i < len; ++i)
13437 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
13438 result = true;
13439 return result;
13441 else if (TYPE_P (t))
13443 /* [basic.link]: A name with no linkage (notably, the name
13444 of a class or enumeration declared in a local scope)
13445 shall not be used to declare an entity with linkage.
13446 This implies that names with no linkage cannot be used as
13447 template arguments
13449 DR 757 relaxes this restriction for C++0x. */
13450 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
13451 : no_linkage_check (t, /*relaxed_p=*/false));
13453 if (nt)
13455 /* DR 488 makes use of a type with no linkage cause
13456 type deduction to fail. */
13457 if (complain & tf_error)
13459 if (TYPE_ANONYMOUS_P (nt))
13460 error ("%qT is/uses anonymous type", t);
13461 else
13462 error ("template argument for %qD uses local type %qT",
13463 tmpl, t);
13465 return true;
13467 /* In order to avoid all sorts of complications, we do not
13468 allow variably-modified types as template arguments. */
13469 else if (variably_modified_type_p (t, NULL_TREE))
13471 if (complain & tf_error)
13472 error ("%qT is a variably modified type", t);
13473 return true;
13476 /* A non-type argument of integral or enumerated type must be a
13477 constant. */
13478 else if (TREE_TYPE (t)
13479 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
13480 && !TREE_CONSTANT (t))
13482 if (complain & tf_error)
13483 error ("integral expression %qE is not constant", t);
13484 return true;
13486 return false;
13489 static bool
13490 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
13492 int ix, len = DECL_NTPARMS (tmpl);
13493 bool result = false;
13495 for (ix = 0; ix != len; ix++)
13497 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
13498 result = true;
13500 if (result && (complain & tf_error))
13501 error (" trying to instantiate %qD", tmpl);
13502 return result;
13505 /* Instantiate the indicated variable or function template TMPL with
13506 the template arguments in TARG_PTR. */
13508 tree
13509 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
13511 tree targ_ptr = orig_args;
13512 tree fndecl;
13513 tree gen_tmpl;
13514 tree spec;
13515 HOST_WIDE_INT saved_processing_template_decl;
13517 if (tmpl == error_mark_node)
13518 return error_mark_node;
13520 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
13522 /* If this function is a clone, handle it specially. */
13523 if (DECL_CLONED_FUNCTION_P (tmpl))
13525 tree spec;
13526 tree clone;
13528 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13529 DECL_CLONED_FUNCTION. */
13530 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
13531 targ_ptr, complain);
13532 if (spec == error_mark_node)
13533 return error_mark_node;
13535 /* Look for the clone. */
13536 FOR_EACH_CLONE (clone, spec)
13537 if (DECL_NAME (clone) == DECL_NAME (tmpl))
13538 return clone;
13539 /* We should always have found the clone by now. */
13540 gcc_unreachable ();
13541 return NULL_TREE;
13544 /* Check to see if we already have this specialization. */
13545 gen_tmpl = most_general_template (tmpl);
13546 if (tmpl != gen_tmpl)
13547 /* The TMPL is a partial instantiation. To get a full set of
13548 arguments we must add the arguments used to perform the
13549 partial instantiation. */
13550 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
13551 targ_ptr);
13553 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13554 but it doesn't seem to be on the hot path. */
13555 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
13557 gcc_assert (tmpl == gen_tmpl
13558 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
13559 == spec)
13560 || fndecl == NULL_TREE);
13562 if (spec != NULL_TREE)
13563 return spec;
13565 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13566 complain))
13567 return error_mark_node;
13569 /* We are building a FUNCTION_DECL, during which the access of its
13570 parameters and return types have to be checked. However this
13571 FUNCTION_DECL which is the desired context for access checking
13572 is not built yet. We solve this chicken-and-egg problem by
13573 deferring all checks until we have the FUNCTION_DECL. */
13574 push_deferring_access_checks (dk_deferred);
13576 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13577 (because, for example, we have encountered a non-dependent
13578 function call in the body of a template function and must now
13579 determine which of several overloaded functions will be called),
13580 within the instantiation itself we are not processing a
13581 template. */
13582 saved_processing_template_decl = processing_template_decl;
13583 processing_template_decl = 0;
13584 /* Substitute template parameters to obtain the specialization. */
13585 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13586 targ_ptr, complain, gen_tmpl);
13587 processing_template_decl = saved_processing_template_decl;
13588 if (fndecl == error_mark_node)
13589 return error_mark_node;
13591 /* Now we know the specialization, compute access previously
13592 deferred. */
13593 push_access_scope (fndecl);
13595 /* Some typedefs referenced from within the template code need to be access
13596 checked at template instantiation time, i.e now. These types were
13597 added to the template at parsing time. Let's get those and perfom
13598 the acces checks then. */
13599 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13600 perform_deferred_access_checks ();
13601 pop_access_scope (fndecl);
13602 pop_deferring_access_checks ();
13604 /* The DECL_TI_TEMPLATE should always be the immediate parent
13605 template, not the most general template. */
13606 DECL_TI_TEMPLATE (fndecl) = tmpl;
13608 /* If we've just instantiated the main entry point for a function,
13609 instantiate all the alternate entry points as well. We do this
13610 by cloning the instantiation of the main entry point, not by
13611 instantiating the template clones. */
13612 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
13613 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13615 return fndecl;
13618 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13619 NARGS elements of the arguments that are being used when calling
13620 it. TARGS is a vector into which the deduced template arguments
13621 are placed.
13623 Return zero for success, 2 for an incomplete match that doesn't resolve
13624 all the types, and 1 for complete failure. An error message will be
13625 printed only for an incomplete match.
13627 If FN is a conversion operator, or we are trying to produce a specific
13628 specialization, RETURN_TYPE is the return type desired.
13630 The EXPLICIT_TARGS are explicit template arguments provided via a
13631 template-id.
13633 The parameter STRICT is one of:
13635 DEDUCE_CALL:
13636 We are deducing arguments for a function call, as in
13637 [temp.deduct.call].
13639 DEDUCE_CONV:
13640 We are deducing arguments for a conversion function, as in
13641 [temp.deduct.conv].
13643 DEDUCE_EXACT:
13644 We are deducing arguments when doing an explicit instantiation
13645 as in [temp.explicit], when determining an explicit specialization
13646 as in [temp.expl.spec], or when taking the address of a function
13647 template, as in [temp.deduct.funcaddr]. */
13650 fn_type_unification (tree fn,
13651 tree explicit_targs,
13652 tree targs,
13653 const tree *args,
13654 unsigned int nargs,
13655 tree return_type,
13656 unification_kind_t strict,
13657 int flags)
13659 tree parms;
13660 tree fntype;
13661 int result;
13662 bool incomplete_argument_packs_p = false;
13664 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13666 fntype = TREE_TYPE (fn);
13667 if (explicit_targs)
13669 /* [temp.deduct]
13671 The specified template arguments must match the template
13672 parameters in kind (i.e., type, nontype, template), and there
13673 must not be more arguments than there are parameters;
13674 otherwise type deduction fails.
13676 Nontype arguments must match the types of the corresponding
13677 nontype template parameters, or must be convertible to the
13678 types of the corresponding nontype parameters as specified in
13679 _temp.arg.nontype_, otherwise type deduction fails.
13681 All references in the function type of the function template
13682 to the corresponding template parameters are replaced by the
13683 specified template argument values. If a substitution in a
13684 template parameter or in the function type of the function
13685 template results in an invalid type, type deduction fails. */
13686 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13687 int i, len = TREE_VEC_LENGTH (tparms);
13688 tree converted_args;
13689 bool incomplete = false;
13691 if (explicit_targs == error_mark_node)
13692 return 1;
13694 converted_args
13695 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13696 /*require_all_args=*/false,
13697 /*use_default_args=*/false));
13698 if (converted_args == error_mark_node)
13699 return 1;
13701 /* Substitute the explicit args into the function type. This is
13702 necessary so that, for instance, explicitly declared function
13703 arguments can match null pointed constants. If we were given
13704 an incomplete set of explicit args, we must not do semantic
13705 processing during substitution as we could create partial
13706 instantiations. */
13707 for (i = 0; i < len; i++)
13709 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13710 bool parameter_pack = false;
13712 /* Dig out the actual parm. */
13713 if (TREE_CODE (parm) == TYPE_DECL
13714 || TREE_CODE (parm) == TEMPLATE_DECL)
13716 parm = TREE_TYPE (parm);
13717 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13719 else if (TREE_CODE (parm) == PARM_DECL)
13721 parm = DECL_INITIAL (parm);
13722 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13725 if (parameter_pack)
13727 int level, idx;
13728 tree targ;
13729 template_parm_level_and_index (parm, &level, &idx);
13731 /* Mark the argument pack as "incomplete". We could
13732 still deduce more arguments during unification.
13733 We remove this mark in type_unification_real. */
13734 targ = TMPL_ARG (converted_args, level, idx);
13735 if (targ)
13737 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13738 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13739 = ARGUMENT_PACK_ARGS (targ);
13742 /* We have some incomplete argument packs. */
13743 incomplete_argument_packs_p = true;
13747 if (incomplete_argument_packs_p)
13748 /* Any substitution is guaranteed to be incomplete if there
13749 are incomplete argument packs, because we can still deduce
13750 more arguments. */
13751 incomplete = 1;
13752 else
13753 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13755 processing_template_decl += incomplete;
13756 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13757 processing_template_decl -= incomplete;
13759 if (fntype == error_mark_node)
13760 return 1;
13762 /* Place the explicitly specified arguments in TARGS. */
13763 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13764 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13767 /* Never do unification on the 'this' parameter. */
13768 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13770 if (return_type)
13772 tree *new_args;
13774 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13775 new_args = XALLOCAVEC (tree, nargs + 1);
13776 new_args[0] = return_type;
13777 memcpy (new_args + 1, args, nargs * sizeof (tree));
13778 args = new_args;
13779 ++nargs;
13782 /* We allow incomplete unification without an error message here
13783 because the standard doesn't seem to explicitly prohibit it. Our
13784 callers must be ready to deal with unification failures in any
13785 event. */
13786 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13787 targs, parms, args, nargs, /*subr=*/0,
13788 strict, flags);
13790 /* Now that we have bindings for all of the template arguments,
13791 ensure that the arguments deduced for the template template
13792 parameters have compatible template parameter lists. We cannot
13793 check this property before we have deduced all template
13794 arguments, because the template parameter types of a template
13795 template parameter might depend on prior template parameters
13796 deduced after the template template parameter. The following
13797 ill-formed example illustrates this issue:
13799 template<typename T, template<T> class C> void f(C<5>, T);
13801 template<int N> struct X {};
13803 void g() {
13804 f(X<5>(), 5l); // error: template argument deduction fails
13807 The template parameter list of 'C' depends on the template type
13808 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13809 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13810 time that we deduce 'C'. */
13811 if (result == 0
13812 && !template_template_parm_bindings_ok_p
13813 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13814 return 1;
13816 if (result == 0)
13817 /* All is well so far. Now, check:
13819 [temp.deduct]
13821 When all template arguments have been deduced, all uses of
13822 template parameters in nondeduced contexts are replaced with
13823 the corresponding deduced argument values. If the
13824 substitution results in an invalid type, as described above,
13825 type deduction fails. */
13827 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13828 if (substed == error_mark_node)
13829 return 1;
13831 /* If we're looking for an exact match, check that what we got
13832 is indeed an exact match. It might not be if some template
13833 parameters are used in non-deduced contexts. */
13834 if (strict == DEDUCE_EXACT)
13836 unsigned int i;
13838 tree sarg
13839 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13840 if (return_type)
13841 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13842 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13843 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13844 return 1;
13848 return result;
13851 /* Adjust types before performing type deduction, as described in
13852 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13853 sections are symmetric. PARM is the type of a function parameter
13854 or the return type of the conversion function. ARG is the type of
13855 the argument passed to the call, or the type of the value
13856 initialized with the result of the conversion function.
13857 ARG_EXPR is the original argument expression, which may be null. */
13859 static int
13860 maybe_adjust_types_for_deduction (unification_kind_t strict,
13861 tree* parm,
13862 tree* arg,
13863 tree arg_expr)
13865 int result = 0;
13867 switch (strict)
13869 case DEDUCE_CALL:
13870 break;
13872 case DEDUCE_CONV:
13874 /* Swap PARM and ARG throughout the remainder of this
13875 function; the handling is precisely symmetric since PARM
13876 will initialize ARG rather than vice versa. */
13877 tree* temp = parm;
13878 parm = arg;
13879 arg = temp;
13880 break;
13883 case DEDUCE_EXACT:
13884 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13885 too, but here handle it by stripping the reference from PARM
13886 rather than by adding it to ARG. */
13887 if (TREE_CODE (*parm) == REFERENCE_TYPE
13888 && TYPE_REF_IS_RVALUE (*parm)
13889 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13890 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13891 && TREE_CODE (*arg) == REFERENCE_TYPE
13892 && !TYPE_REF_IS_RVALUE (*arg))
13893 *parm = TREE_TYPE (*parm);
13894 /* Nothing else to do in this case. */
13895 return 0;
13897 default:
13898 gcc_unreachable ();
13901 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13903 /* [temp.deduct.call]
13905 If P is not a reference type:
13907 --If A is an array type, the pointer type produced by the
13908 array-to-pointer standard conversion (_conv.array_) is
13909 used in place of A for type deduction; otherwise,
13911 --If A is a function type, the pointer type produced by
13912 the function-to-pointer standard conversion
13913 (_conv.func_) is used in place of A for type deduction;
13914 otherwise,
13916 --If A is a cv-qualified type, the top level
13917 cv-qualifiers of A's type are ignored for type
13918 deduction. */
13919 if (TREE_CODE (*arg) == ARRAY_TYPE)
13920 *arg = build_pointer_type (TREE_TYPE (*arg));
13921 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13922 *arg = build_pointer_type (*arg);
13923 else
13924 *arg = TYPE_MAIN_VARIANT (*arg);
13927 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13928 of the form T&&, where T is a template parameter, and the argument
13929 is an lvalue, T is deduced as A& */
13930 if (TREE_CODE (*parm) == REFERENCE_TYPE
13931 && TYPE_REF_IS_RVALUE (*parm)
13932 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13933 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13934 && (arg_expr ? real_lvalue_p (arg_expr)
13935 /* try_one_overload doesn't provide an arg_expr, but
13936 functions are always lvalues. */
13937 : TREE_CODE (*arg) == FUNCTION_TYPE))
13938 *arg = build_reference_type (*arg);
13940 /* [temp.deduct.call]
13942 If P is a cv-qualified type, the top level cv-qualifiers
13943 of P's type are ignored for type deduction. If P is a
13944 reference type, the type referred to by P is used for
13945 type deduction. */
13946 *parm = TYPE_MAIN_VARIANT (*parm);
13947 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13949 *parm = TREE_TYPE (*parm);
13950 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13953 /* DR 322. For conversion deduction, remove a reference type on parm
13954 too (which has been swapped into ARG). */
13955 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13956 *arg = TREE_TYPE (*arg);
13958 return result;
13961 /* Most parms like fn_type_unification.
13963 If SUBR is 1, we're being called recursively (to unify the
13964 arguments of a function or method parameter of a function
13965 template). */
13967 static int
13968 type_unification_real (tree tparms,
13969 tree targs,
13970 tree xparms,
13971 const tree *xargs,
13972 unsigned int xnargs,
13973 int subr,
13974 unification_kind_t strict,
13975 int flags)
13977 tree parm, arg, arg_expr;
13978 int i;
13979 int ntparms = TREE_VEC_LENGTH (tparms);
13980 int sub_strict;
13981 int saw_undeduced = 0;
13982 tree parms;
13983 const tree *args;
13984 unsigned int nargs;
13985 unsigned int ia;
13987 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13988 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13989 gcc_assert (ntparms > 0);
13991 /* Reset the number of non-defaulted template arguments contained
13992 in TARGS. */
13993 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
13995 switch (strict)
13997 case DEDUCE_CALL:
13998 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13999 | UNIFY_ALLOW_DERIVED);
14000 break;
14002 case DEDUCE_CONV:
14003 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14004 break;
14006 case DEDUCE_EXACT:
14007 sub_strict = UNIFY_ALLOW_NONE;
14008 break;
14010 default:
14011 gcc_unreachable ();
14014 again:
14015 parms = xparms;
14016 args = xargs;
14017 nargs = xnargs;
14019 ia = 0;
14020 while (parms && parms != void_list_node
14021 && ia < nargs)
14023 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
14024 break;
14026 parm = TREE_VALUE (parms);
14027 parms = TREE_CHAIN (parms);
14028 arg = args[ia];
14029 ++ia;
14030 arg_expr = NULL;
14032 if (arg == error_mark_node)
14033 return 1;
14034 if (arg == unknown_type_node)
14035 /* We can't deduce anything from this, but we might get all the
14036 template args from other function args. */
14037 continue;
14039 /* Conversions will be performed on a function argument that
14040 corresponds with a function parameter that contains only
14041 non-deducible template parameters and explicitly specified
14042 template parameters. */
14043 if (!uses_template_parms (parm))
14045 tree type;
14047 if (!TYPE_P (arg))
14048 type = TREE_TYPE (arg);
14049 else
14050 type = arg;
14052 if (same_type_p (parm, type))
14053 continue;
14054 if (strict != DEDUCE_EXACT
14055 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
14056 flags))
14057 continue;
14059 return 1;
14062 if (!TYPE_P (arg))
14064 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14065 if (type_unknown_p (arg))
14067 /* [temp.deduct.type]
14069 A template-argument can be deduced from a pointer to
14070 function or pointer to member function argument if
14071 the set of overloaded functions does not contain
14072 function templates and at most one of a set of
14073 overloaded functions provides a unique match. */
14074 if (resolve_overloaded_unification
14075 (tparms, targs, parm, arg, strict, sub_strict))
14076 continue;
14078 return 1;
14080 arg_expr = arg;
14081 arg = unlowered_expr_type (arg);
14082 if (arg == error_mark_node)
14083 return 1;
14087 int arg_strict = sub_strict;
14089 if (!subr)
14090 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
14091 arg_expr);
14093 if (arg == init_list_type_node && arg_expr)
14094 arg = arg_expr;
14095 if (unify (tparms, targs, parm, arg, arg_strict))
14096 return 1;
14101 if (parms
14102 && parms != void_list_node
14103 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
14105 /* Unify the remaining arguments with the pack expansion type. */
14106 tree argvec;
14107 tree parmvec = make_tree_vec (1);
14109 /* Allocate a TREE_VEC and copy in all of the arguments */
14110 argvec = make_tree_vec (nargs - ia);
14111 for (i = 0; ia < nargs; ++ia, ++i)
14112 TREE_VEC_ELT (argvec, i) = args[ia];
14114 /* Copy the parameter into parmvec. */
14115 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
14116 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
14117 /*call_args_p=*/true, /*subr=*/subr))
14118 return 1;
14120 /* Advance to the end of the list of parameters. */
14121 parms = TREE_CHAIN (parms);
14124 /* Fail if we've reached the end of the parm list, and more args
14125 are present, and the parm list isn't variadic. */
14126 if (ia < nargs && parms == void_list_node)
14127 return 1;
14128 /* Fail if parms are left and they don't have default values. */
14129 if (parms && parms != void_list_node
14130 && TREE_PURPOSE (parms) == NULL_TREE)
14131 return 1;
14133 if (!subr)
14135 /* Check to see if we need another pass before we start clearing
14136 ARGUMENT_PACK_INCOMPLETE_P. */
14137 for (i = 0; i < ntparms; i++)
14139 tree targ = TREE_VEC_ELT (targs, i);
14140 tree tparm = TREE_VEC_ELT (tparms, i);
14142 if (targ || tparm == error_mark_node)
14143 continue;
14144 tparm = TREE_VALUE (tparm);
14146 /* If this is an undeduced nontype parameter that depends on
14147 a type parameter, try another pass; its type may have been
14148 deduced from a later argument than the one from which
14149 this parameter can be deduced. */
14150 if (TREE_CODE (tparm) == PARM_DECL
14151 && uses_template_parms (TREE_TYPE (tparm))
14152 && !saw_undeduced++)
14153 goto again;
14156 for (i = 0; i < ntparms; i++)
14158 tree targ = TREE_VEC_ELT (targs, i);
14159 tree tparm = TREE_VEC_ELT (tparms, i);
14161 /* Clear the "incomplete" flags on all argument packs now so that
14162 substituting them into later default arguments works. */
14163 if (targ && ARGUMENT_PACK_P (targ))
14165 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
14166 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
14169 if (targ || tparm == error_mark_node)
14170 continue;
14171 tparm = TREE_VALUE (tparm);
14173 /* Core issue #226 (C++0x) [temp.deduct]:
14175 If a template argument has not been deduced, its
14176 default template argument, if any, is used.
14178 When we are in C++98 mode, TREE_PURPOSE will either
14179 be NULL_TREE or ERROR_MARK_NODE, so we do not need
14180 to explicitly check cxx_dialect here. */
14181 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
14183 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14184 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
14185 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
14186 arg = convert_template_argument (parm, arg, targs, tf_none,
14187 i, NULL_TREE);
14188 if (arg == error_mark_node)
14189 return 1;
14190 else
14192 TREE_VEC_ELT (targs, i) = arg;
14193 /* The position of the first default template argument,
14194 is also the number of non-defaulted arguments in TARGS.
14195 Record that. */
14196 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14197 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
14198 continue;
14202 /* If the type parameter is a parameter pack, then it will
14203 be deduced to an empty parameter pack. */
14204 if (template_parameter_pack_p (tparm))
14206 tree arg;
14208 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
14210 arg = make_node (NONTYPE_ARGUMENT_PACK);
14211 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
14212 TREE_CONSTANT (arg) = 1;
14214 else
14215 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
14217 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
14219 TREE_VEC_ELT (targs, i) = arg;
14220 continue;
14223 return 2;
14226 #ifdef ENABLE_CHECKING
14227 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14228 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
14229 #endif
14231 return 0;
14234 /* Subroutine of type_unification_real. Args are like the variables
14235 at the call site. ARG is an overloaded function (or template-id);
14236 we try deducing template args from each of the overloads, and if
14237 only one succeeds, we go with that. Modifies TARGS and returns
14238 true on success. */
14240 static bool
14241 resolve_overloaded_unification (tree tparms,
14242 tree targs,
14243 tree parm,
14244 tree arg,
14245 unification_kind_t strict,
14246 int sub_strict)
14248 tree tempargs = copy_node (targs);
14249 int good = 0;
14250 tree goodfn = NULL_TREE;
14251 bool addr_p;
14253 if (TREE_CODE (arg) == ADDR_EXPR)
14255 arg = TREE_OPERAND (arg, 0);
14256 addr_p = true;
14258 else
14259 addr_p = false;
14261 if (TREE_CODE (arg) == COMPONENT_REF)
14262 /* Handle `&x' where `x' is some static or non-static member
14263 function name. */
14264 arg = TREE_OPERAND (arg, 1);
14266 if (TREE_CODE (arg) == OFFSET_REF)
14267 arg = TREE_OPERAND (arg, 1);
14269 /* Strip baselink information. */
14270 if (BASELINK_P (arg))
14271 arg = BASELINK_FUNCTIONS (arg);
14273 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
14275 /* If we got some explicit template args, we need to plug them into
14276 the affected templates before we try to unify, in case the
14277 explicit args will completely resolve the templates in question. */
14279 tree expl_subargs = TREE_OPERAND (arg, 1);
14280 arg = TREE_OPERAND (arg, 0);
14282 for (; arg; arg = OVL_NEXT (arg))
14284 tree fn = OVL_CURRENT (arg);
14285 tree subargs, elem;
14287 if (TREE_CODE (fn) != TEMPLATE_DECL)
14288 continue;
14290 ++processing_template_decl;
14291 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14292 expl_subargs, /*check_ret=*/false);
14293 if (subargs)
14295 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
14296 if (try_one_overload (tparms, targs, tempargs, parm,
14297 elem, strict, sub_strict, addr_p)
14298 && (!goodfn || !decls_match (goodfn, elem)))
14300 goodfn = elem;
14301 ++good;
14304 --processing_template_decl;
14307 else if (TREE_CODE (arg) != OVERLOAD
14308 && TREE_CODE (arg) != FUNCTION_DECL)
14309 /* If ARG is, for example, "(0, &f)" then its type will be unknown
14310 -- but the deduction does not succeed because the expression is
14311 not just the function on its own. */
14312 return false;
14313 else
14314 for (; arg; arg = OVL_NEXT (arg))
14315 if (try_one_overload (tparms, targs, tempargs, parm,
14316 TREE_TYPE (OVL_CURRENT (arg)),
14317 strict, sub_strict, addr_p)
14318 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
14320 goodfn = OVL_CURRENT (arg);
14321 ++good;
14324 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14325 to function or pointer to member function argument if the set of
14326 overloaded functions does not contain function templates and at most
14327 one of a set of overloaded functions provides a unique match.
14329 So if we found multiple possibilities, we return success but don't
14330 deduce anything. */
14332 if (good == 1)
14334 int i = TREE_VEC_LENGTH (targs);
14335 for (; i--; )
14336 if (TREE_VEC_ELT (tempargs, i))
14337 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
14339 if (good)
14340 return true;
14342 return false;
14345 /* Core DR 115: In contexts where deduction is done and fails, or in
14346 contexts where deduction is not done, if a template argument list is
14347 specified and it, along with any default template arguments, identifies
14348 a single function template specialization, then the template-id is an
14349 lvalue for the function template specialization. */
14351 tree
14352 resolve_nondeduced_context (tree orig_expr)
14354 tree expr, offset, baselink;
14355 bool addr;
14357 if (!type_unknown_p (orig_expr))
14358 return orig_expr;
14360 expr = orig_expr;
14361 addr = false;
14362 offset = NULL_TREE;
14363 baselink = NULL_TREE;
14365 if (TREE_CODE (expr) == ADDR_EXPR)
14367 expr = TREE_OPERAND (expr, 0);
14368 addr = true;
14370 if (TREE_CODE (expr) == OFFSET_REF)
14372 offset = expr;
14373 expr = TREE_OPERAND (expr, 1);
14375 if (TREE_CODE (expr) == BASELINK)
14377 baselink = expr;
14378 expr = BASELINK_FUNCTIONS (expr);
14381 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
14383 int good = 0;
14384 tree goodfn = NULL_TREE;
14386 /* If we got some explicit template args, we need to plug them into
14387 the affected templates before we try to unify, in case the
14388 explicit args will completely resolve the templates in question. */
14390 tree expl_subargs = TREE_OPERAND (expr, 1);
14391 tree arg = TREE_OPERAND (expr, 0);
14392 tree badfn = NULL_TREE;
14393 tree badargs = NULL_TREE;
14395 for (; arg; arg = OVL_NEXT (arg))
14397 tree fn = OVL_CURRENT (arg);
14398 tree subargs, elem;
14400 if (TREE_CODE (fn) != TEMPLATE_DECL)
14401 continue;
14403 ++processing_template_decl;
14404 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14405 expl_subargs, /*check_ret=*/false);
14406 if (subargs && !any_dependent_template_arguments_p (subargs))
14408 elem = instantiate_template (fn, subargs, tf_none);
14409 if (elem == error_mark_node)
14411 badfn = fn;
14412 badargs = subargs;
14414 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
14416 goodfn = elem;
14417 ++good;
14420 --processing_template_decl;
14422 if (good == 1)
14424 expr = goodfn;
14425 if (baselink)
14426 expr = build_baselink (BASELINK_BINFO (baselink),
14427 BASELINK_ACCESS_BINFO (baselink),
14428 expr, BASELINK_OPTYPE (baselink));
14429 if (offset)
14431 tree base
14432 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
14433 expr = build_offset_ref (base, expr, addr);
14435 if (addr)
14436 expr = cp_build_addr_expr (expr, tf_warning_or_error);
14437 return expr;
14439 else if (good == 0 && badargs)
14440 /* There were no good options and at least one bad one, so let the
14441 user know what the problem is. */
14442 instantiate_template (badfn, badargs, tf_warning_or_error);
14444 return orig_expr;
14447 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14448 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14449 different overloads deduce different arguments for a given parm.
14450 ADDR_P is true if the expression for which deduction is being
14451 performed was of the form "& fn" rather than simply "fn".
14453 Returns 1 on success. */
14455 static int
14456 try_one_overload (tree tparms,
14457 tree orig_targs,
14458 tree targs,
14459 tree parm,
14460 tree arg,
14461 unification_kind_t strict,
14462 int sub_strict,
14463 bool addr_p)
14465 int nargs;
14466 tree tempargs;
14467 int i;
14469 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14470 to function or pointer to member function argument if the set of
14471 overloaded functions does not contain function templates and at most
14472 one of a set of overloaded functions provides a unique match.
14474 So if this is a template, just return success. */
14476 if (uses_template_parms (arg))
14477 return 1;
14479 if (TREE_CODE (arg) == METHOD_TYPE)
14480 arg = build_ptrmemfunc_type (build_pointer_type (arg));
14481 else if (addr_p)
14482 arg = build_pointer_type (arg);
14484 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
14486 /* We don't copy orig_targs for this because if we have already deduced
14487 some template args from previous args, unify would complain when we
14488 try to deduce a template parameter for the same argument, even though
14489 there isn't really a conflict. */
14490 nargs = TREE_VEC_LENGTH (targs);
14491 tempargs = make_tree_vec (nargs);
14493 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
14494 return 0;
14496 /* First make sure we didn't deduce anything that conflicts with
14497 explicitly specified args. */
14498 for (i = nargs; i--; )
14500 tree elt = TREE_VEC_ELT (tempargs, i);
14501 tree oldelt = TREE_VEC_ELT (orig_targs, i);
14503 if (!elt)
14504 /*NOP*/;
14505 else if (uses_template_parms (elt))
14506 /* Since we're unifying against ourselves, we will fill in
14507 template args used in the function parm list with our own
14508 template parms. Discard them. */
14509 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
14510 else if (oldelt && !template_args_equal (oldelt, elt))
14511 return 0;
14514 for (i = nargs; i--; )
14516 tree elt = TREE_VEC_ELT (tempargs, i);
14518 if (elt)
14519 TREE_VEC_ELT (targs, i) = elt;
14522 return 1;
14525 /* PARM is a template class (perhaps with unbound template
14526 parameters). ARG is a fully instantiated type. If ARG can be
14527 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14528 TARGS are as for unify. */
14530 static tree
14531 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
14533 tree copy_of_targs;
14535 if (!CLASSTYPE_TEMPLATE_INFO (arg)
14536 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
14537 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
14538 return NULL_TREE;
14540 /* We need to make a new template argument vector for the call to
14541 unify. If we used TARGS, we'd clutter it up with the result of
14542 the attempted unification, even if this class didn't work out.
14543 We also don't want to commit ourselves to all the unifications
14544 we've already done, since unification is supposed to be done on
14545 an argument-by-argument basis. In other words, consider the
14546 following pathological case:
14548 template <int I, int J, int K>
14549 struct S {};
14551 template <int I, int J>
14552 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14554 template <int I, int J, int K>
14555 void f(S<I, J, K>, S<I, I, I>);
14557 void g() {
14558 S<0, 0, 0> s0;
14559 S<0, 1, 2> s2;
14561 f(s0, s2);
14564 Now, by the time we consider the unification involving `s2', we
14565 already know that we must have `f<0, 0, 0>'. But, even though
14566 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14567 because there are two ways to unify base classes of S<0, 1, 2>
14568 with S<I, I, I>. If we kept the already deduced knowledge, we
14569 would reject the possibility I=1. */
14570 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
14572 /* If unification failed, we're done. */
14573 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14574 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14575 return NULL_TREE;
14577 return arg;
14580 /* Given a template type PARM and a class type ARG, find the unique
14581 base type in ARG that is an instance of PARM. We do not examine
14582 ARG itself; only its base-classes. If there is not exactly one
14583 appropriate base class, return NULL_TREE. PARM may be the type of
14584 a partial specialization, as well as a plain template type. Used
14585 by unify. */
14587 static tree
14588 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14590 tree rval = NULL_TREE;
14591 tree binfo;
14593 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14595 binfo = TYPE_BINFO (complete_type (arg));
14596 if (!binfo)
14597 /* The type could not be completed. */
14598 return NULL_TREE;
14600 /* Walk in inheritance graph order. The search order is not
14601 important, and this avoids multiple walks of virtual bases. */
14602 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14604 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14606 if (r)
14608 /* If there is more than one satisfactory baseclass, then:
14610 [temp.deduct.call]
14612 If they yield more than one possible deduced A, the type
14613 deduction fails.
14615 applies. */
14616 if (rval && !same_type_p (r, rval))
14617 return NULL_TREE;
14619 rval = r;
14623 return rval;
14626 /* Returns the level of DECL, which declares a template parameter. */
14628 static int
14629 template_decl_level (tree decl)
14631 switch (TREE_CODE (decl))
14633 case TYPE_DECL:
14634 case TEMPLATE_DECL:
14635 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14637 case PARM_DECL:
14638 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14640 default:
14641 gcc_unreachable ();
14643 return 0;
14646 /* Decide whether ARG can be unified with PARM, considering only the
14647 cv-qualifiers of each type, given STRICT as documented for unify.
14648 Returns nonzero iff the unification is OK on that basis. */
14650 static int
14651 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14653 int arg_quals = cp_type_quals (arg);
14654 int parm_quals = cp_type_quals (parm);
14656 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14657 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14659 /* Although a CVR qualifier is ignored when being applied to a
14660 substituted template parameter ([8.3.2]/1 for example), that
14661 does not allow us to unify "const T" with "int&" because both
14662 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14663 It is ok when we're allowing additional CV qualifiers
14664 at the outer level [14.8.2.1]/3,1st bullet. */
14665 if ((TREE_CODE (arg) == REFERENCE_TYPE
14666 || TREE_CODE (arg) == FUNCTION_TYPE
14667 || TREE_CODE (arg) == METHOD_TYPE)
14668 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14669 return 0;
14671 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14672 && (parm_quals & TYPE_QUAL_RESTRICT))
14673 return 0;
14676 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14677 && (arg_quals & parm_quals) != parm_quals)
14678 return 0;
14680 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14681 && (parm_quals & arg_quals) != arg_quals)
14682 return 0;
14684 return 1;
14687 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14688 void
14689 template_parm_level_and_index (tree parm, int* level, int* index)
14691 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14692 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14693 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14695 *index = TEMPLATE_TYPE_IDX (parm);
14696 *level = TEMPLATE_TYPE_LEVEL (parm);
14698 else
14700 *index = TEMPLATE_PARM_IDX (parm);
14701 *level = TEMPLATE_PARM_LEVEL (parm);
14705 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14706 expansion at the end of PACKED_PARMS. Returns 0 if the type
14707 deduction succeeds, 1 otherwise. STRICT is the same as in
14708 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14709 call argument list. We'll need to adjust the arguments to make them
14710 types. SUBR tells us if this is from a recursive call to
14711 type_unification_real. */
14713 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14714 tree packed_args, int strict, bool call_args_p,
14715 bool subr)
14717 tree parm
14718 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14719 tree pattern = PACK_EXPANSION_PATTERN (parm);
14720 tree pack, packs = NULL_TREE;
14721 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14722 int len = TREE_VEC_LENGTH (packed_args);
14724 /* Determine the parameter packs we will be deducing from the
14725 pattern, and record their current deductions. */
14726 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14727 pack; pack = TREE_CHAIN (pack))
14729 tree parm_pack = TREE_VALUE (pack);
14730 int idx, level;
14732 /* Determine the index and level of this parameter pack. */
14733 template_parm_level_and_index (parm_pack, &level, &idx);
14735 /* Keep track of the parameter packs and their corresponding
14736 argument packs. */
14737 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14738 TREE_TYPE (packs) = make_tree_vec (len - start);
14741 /* Loop through all of the arguments that have not yet been
14742 unified and unify each with the pattern. */
14743 for (i = start; i < len; i++)
14745 tree parm = pattern;
14747 /* For each parameter pack, clear out the deduced value so that
14748 we can deduce it again. */
14749 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14751 int idx, level;
14752 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14754 TMPL_ARG (targs, level, idx) = NULL_TREE;
14757 /* Unify the pattern with the current argument. */
14759 tree arg = TREE_VEC_ELT (packed_args, i);
14760 tree arg_expr = NULL_TREE;
14761 int arg_strict = strict;
14762 bool skip_arg_p = false;
14764 if (call_args_p)
14766 int sub_strict;
14768 /* This mirrors what we do in type_unification_real. */
14769 switch (strict)
14771 case DEDUCE_CALL:
14772 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
14773 | UNIFY_ALLOW_MORE_CV_QUAL
14774 | UNIFY_ALLOW_DERIVED);
14775 break;
14777 case DEDUCE_CONV:
14778 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14779 break;
14781 case DEDUCE_EXACT:
14782 sub_strict = UNIFY_ALLOW_NONE;
14783 break;
14785 default:
14786 gcc_unreachable ();
14789 if (!TYPE_P (arg))
14791 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14792 if (type_unknown_p (arg))
14794 /* [temp.deduct.type] A template-argument can be
14795 deduced from a pointer to function or pointer
14796 to member function argument if the set of
14797 overloaded functions does not contain function
14798 templates and at most one of a set of
14799 overloaded functions provides a unique
14800 match. */
14802 if (resolve_overloaded_unification
14803 (tparms, targs, parm, arg,
14804 (unification_kind_t) strict,
14805 sub_strict)
14806 != 0)
14807 return 1;
14808 skip_arg_p = true;
14811 if (!skip_arg_p)
14813 arg_expr = arg;
14814 arg = unlowered_expr_type (arg);
14815 if (arg == error_mark_node)
14816 return 1;
14820 arg_strict = sub_strict;
14822 if (!subr)
14823 arg_strict |=
14824 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14825 &parm, &arg, arg_expr);
14828 if (!skip_arg_p)
14830 /* For deduction from an init-list we need the actual list. */
14831 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14832 arg = arg_expr;
14833 if (unify (tparms, targs, parm, arg, arg_strict))
14834 return 1;
14838 /* For each parameter pack, collect the deduced value. */
14839 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14841 int idx, level;
14842 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14844 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14845 TMPL_ARG (targs, level, idx);
14849 /* Verify that the results of unification with the parameter packs
14850 produce results consistent with what we've seen before, and make
14851 the deduced argument packs available. */
14852 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14854 tree old_pack = TREE_VALUE (pack);
14855 tree new_args = TREE_TYPE (pack);
14856 int i, len = TREE_VEC_LENGTH (new_args);
14857 int idx, level;
14858 bool nondeduced_p = false;
14860 /* By default keep the original deduced argument pack.
14861 If necessary, more specific code is going to update the
14862 resulting deduced argument later down in this function. */
14863 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14864 TMPL_ARG (targs, level, idx) = old_pack;
14866 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14867 actually deduce anything. */
14868 for (i = 0; i < len && !nondeduced_p; ++i)
14869 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14870 nondeduced_p = true;
14871 if (nondeduced_p)
14872 continue;
14874 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14876 /* Prepend the explicit arguments onto NEW_ARGS. */
14877 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14878 tree old_args = new_args;
14879 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14880 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14882 /* Copy the explicit arguments. */
14883 new_args = make_tree_vec (len);
14884 for (i = 0; i < explicit_len; i++)
14885 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14887 /* Copy the deduced arguments. */
14888 for (; i < len; i++)
14889 TREE_VEC_ELT (new_args, i) =
14890 TREE_VEC_ELT (old_args, i - explicit_len);
14893 if (!old_pack)
14895 tree result;
14896 /* Build the deduced *_ARGUMENT_PACK. */
14897 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14899 result = make_node (NONTYPE_ARGUMENT_PACK);
14900 TREE_TYPE (result) =
14901 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14902 TREE_CONSTANT (result) = 1;
14904 else
14905 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14907 SET_ARGUMENT_PACK_ARGS (result, new_args);
14909 /* Note the deduced argument packs for this parameter
14910 pack. */
14911 TMPL_ARG (targs, level, idx) = result;
14913 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14914 && (ARGUMENT_PACK_ARGS (old_pack)
14915 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14917 /* We only had the explicitly-provided arguments before, but
14918 now we have a complete set of arguments. */
14919 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14921 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14922 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14923 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14925 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14926 new_args))
14927 /* Inconsistent unification of this parameter pack. */
14928 return 1;
14931 return 0;
14934 /* Deduce the value of template parameters. TPARMS is the (innermost)
14935 set of template parameters to a template. TARGS is the bindings
14936 for those template parameters, as determined thus far; TARGS may
14937 include template arguments for outer levels of template parameters
14938 as well. PARM is a parameter to a template function, or a
14939 subcomponent of that parameter; ARG is the corresponding argument.
14940 This function attempts to match PARM with ARG in a manner
14941 consistent with the existing assignments in TARGS. If more values
14942 are deduced, then TARGS is updated.
14944 Returns 0 if the type deduction succeeds, 1 otherwise. The
14945 parameter STRICT is a bitwise or of the following flags:
14947 UNIFY_ALLOW_NONE:
14948 Require an exact match between PARM and ARG.
14949 UNIFY_ALLOW_MORE_CV_QUAL:
14950 Allow the deduced ARG to be more cv-qualified (by qualification
14951 conversion) than ARG.
14952 UNIFY_ALLOW_LESS_CV_QUAL:
14953 Allow the deduced ARG to be less cv-qualified than ARG.
14954 UNIFY_ALLOW_DERIVED:
14955 Allow the deduced ARG to be a template base class of ARG,
14956 or a pointer to a template base class of the type pointed to by
14957 ARG.
14958 UNIFY_ALLOW_INTEGER:
14959 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14960 case for more information.
14961 UNIFY_ALLOW_OUTER_LEVEL:
14962 This is the outermost level of a deduction. Used to determine validity
14963 of qualification conversions. A valid qualification conversion must
14964 have const qualified pointers leading up to the inner type which
14965 requires additional CV quals, except at the outer level, where const
14966 is not required [conv.qual]. It would be normal to set this flag in
14967 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14968 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14969 This is the outermost level of a deduction, and PARM can be more CV
14970 qualified at this point.
14971 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14972 This is the outermost level of a deduction, and PARM can be less CV
14973 qualified at this point. */
14975 static int
14976 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14978 int idx;
14979 tree targ;
14980 tree tparm;
14981 int strict_in = strict;
14983 /* I don't think this will do the right thing with respect to types.
14984 But the only case I've seen it in so far has been array bounds, where
14985 signedness is the only information lost, and I think that will be
14986 okay. */
14987 while (TREE_CODE (parm) == NOP_EXPR)
14988 parm = TREE_OPERAND (parm, 0);
14990 if (arg == error_mark_node)
14991 return 1;
14992 if (arg == unknown_type_node
14993 || arg == init_list_type_node)
14994 /* We can't deduce anything from this, but we might get all the
14995 template args from other function args. */
14996 return 0;
14998 /* If PARM uses template parameters, then we can't bail out here,
14999 even if ARG == PARM, since we won't record unifications for the
15000 template parameters. We might need them if we're trying to
15001 figure out which of two things is more specialized. */
15002 if (arg == parm && !uses_template_parms (parm))
15003 return 0;
15005 /* Handle init lists early, so the rest of the function can assume
15006 we're dealing with a type. */
15007 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
15009 tree elt, elttype;
15010 unsigned i;
15011 tree orig_parm = parm;
15013 /* Replace T with std::initializer_list<T> for deduction. */
15014 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15015 && flag_deduce_init_list)
15016 parm = listify (parm);
15018 if (!is_std_init_list (parm))
15019 /* We can only deduce from an initializer list argument if the
15020 parameter is std::initializer_list; otherwise this is a
15021 non-deduced context. */
15022 return 0;
15024 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
15026 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
15028 int elt_strict = strict;
15030 if (elt == error_mark_node)
15031 return 1;
15033 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
15035 tree type = TREE_TYPE (elt);
15036 /* It should only be possible to get here for a call. */
15037 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
15038 elt_strict |= maybe_adjust_types_for_deduction
15039 (DEDUCE_CALL, &elttype, &type, elt);
15040 elt = type;
15043 if (unify (tparms, targs, elttype, elt, elt_strict))
15044 return 1;
15047 /* If the std::initializer_list<T> deduction worked, replace the
15048 deduced A with std::initializer_list<A>. */
15049 if (orig_parm != parm)
15051 idx = TEMPLATE_TYPE_IDX (orig_parm);
15052 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15053 targ = listify (targ);
15054 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
15056 return 0;
15059 /* Immediately reject some pairs that won't unify because of
15060 cv-qualification mismatches. */
15061 if (TREE_CODE (arg) == TREE_CODE (parm)
15062 && TYPE_P (arg)
15063 /* It is the elements of the array which hold the cv quals of an array
15064 type, and the elements might be template type parms. We'll check
15065 when we recurse. */
15066 && TREE_CODE (arg) != ARRAY_TYPE
15067 /* We check the cv-qualifiers when unifying with template type
15068 parameters below. We want to allow ARG `const T' to unify with
15069 PARM `T' for example, when computing which of two templates
15070 is more specialized, for example. */
15071 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
15072 && !check_cv_quals_for_unify (strict_in, arg, parm))
15073 return 1;
15075 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
15076 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
15077 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
15078 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
15079 strict &= ~UNIFY_ALLOW_DERIVED;
15080 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15081 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
15083 switch (TREE_CODE (parm))
15085 case TYPENAME_TYPE:
15086 case SCOPE_REF:
15087 case UNBOUND_CLASS_TEMPLATE:
15088 /* In a type which contains a nested-name-specifier, template
15089 argument values cannot be deduced for template parameters used
15090 within the nested-name-specifier. */
15091 return 0;
15093 case TEMPLATE_TYPE_PARM:
15094 case TEMPLATE_TEMPLATE_PARM:
15095 case BOUND_TEMPLATE_TEMPLATE_PARM:
15096 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15097 if (tparm == error_mark_node)
15098 return 1;
15100 if (TEMPLATE_TYPE_LEVEL (parm)
15101 != template_decl_level (tparm))
15102 /* The PARM is not one we're trying to unify. Just check
15103 to see if it matches ARG. */
15104 return (TREE_CODE (arg) == TREE_CODE (parm)
15105 && same_type_p (parm, arg)) ? 0 : 1;
15106 idx = TEMPLATE_TYPE_IDX (parm);
15107 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15108 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
15110 /* Check for mixed types and values. */
15111 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15112 && TREE_CODE (tparm) != TYPE_DECL)
15113 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15114 && TREE_CODE (tparm) != TEMPLATE_DECL))
15115 return 1;
15117 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15119 /* ARG must be constructed from a template class or a template
15120 template parameter. */
15121 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
15122 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
15123 return 1;
15126 tree parmvec = TYPE_TI_ARGS (parm);
15127 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
15128 tree full_argvec = add_to_template_args (targs, argvec);
15129 tree parm_parms
15130 = DECL_INNERMOST_TEMPLATE_PARMS
15131 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
15132 int i, len;
15133 int parm_variadic_p = 0;
15135 /* The resolution to DR150 makes clear that default
15136 arguments for an N-argument may not be used to bind T
15137 to a template template parameter with fewer than N
15138 parameters. It is not safe to permit the binding of
15139 default arguments as an extension, as that may change
15140 the meaning of a conforming program. Consider:
15142 struct Dense { static const unsigned int dim = 1; };
15144 template <template <typename> class View,
15145 typename Block>
15146 void operator+(float, View<Block> const&);
15148 template <typename Block,
15149 unsigned int Dim = Block::dim>
15150 struct Lvalue_proxy { operator float() const; };
15152 void
15153 test_1d (void) {
15154 Lvalue_proxy<Dense> p;
15155 float b;
15156 b + p;
15159 Here, if Lvalue_proxy is permitted to bind to View, then
15160 the global operator+ will be used; if they are not, the
15161 Lvalue_proxy will be converted to float. */
15162 if (coerce_template_parms (parm_parms,
15163 full_argvec,
15164 TYPE_TI_TEMPLATE (parm),
15165 tf_none,
15166 /*require_all_args=*/true,
15167 /*use_default_args=*/false)
15168 == error_mark_node)
15169 return 1;
15171 /* Deduce arguments T, i from TT<T> or TT<i>.
15172 We check each element of PARMVEC and ARGVEC individually
15173 rather than the whole TREE_VEC since they can have
15174 different number of elements. */
15176 parmvec = expand_template_argument_pack (parmvec);
15177 argvec = expand_template_argument_pack (argvec);
15179 len = TREE_VEC_LENGTH (parmvec);
15181 /* Check if the parameters end in a pack, making them
15182 variadic. */
15183 if (len > 0
15184 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
15185 parm_variadic_p = 1;
15187 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
15188 return 1;
15190 for (i = 0; i < len - parm_variadic_p; ++i)
15192 if (unify (tparms, targs,
15193 TREE_VEC_ELT (parmvec, i),
15194 TREE_VEC_ELT (argvec, i),
15195 UNIFY_ALLOW_NONE))
15196 return 1;
15199 if (parm_variadic_p
15200 && unify_pack_expansion (tparms, targs,
15201 parmvec, argvec,
15202 UNIFY_ALLOW_NONE,
15203 /*call_args_p=*/false,
15204 /*subr=*/false))
15205 return 1;
15207 arg = TYPE_TI_TEMPLATE (arg);
15209 /* Fall through to deduce template name. */
15212 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15213 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15215 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
15217 /* Simple cases: Value already set, does match or doesn't. */
15218 if (targ != NULL_TREE && template_args_equal (targ, arg))
15219 return 0;
15220 else if (targ)
15221 return 1;
15223 else
15225 /* If PARM is `const T' and ARG is only `int', we don't have
15226 a match unless we are allowing additional qualification.
15227 If ARG is `const int' and PARM is just `T' that's OK;
15228 that binds `const int' to `T'. */
15229 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
15230 arg, parm))
15231 return 1;
15233 /* Consider the case where ARG is `const volatile int' and
15234 PARM is `const T'. Then, T should be `volatile int'. */
15235 arg = cp_build_qualified_type_real
15236 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
15237 if (arg == error_mark_node)
15238 return 1;
15240 /* Simple cases: Value already set, does match or doesn't. */
15241 if (targ != NULL_TREE && same_type_p (targ, arg))
15242 return 0;
15243 else if (targ)
15244 return 1;
15246 /* Make sure that ARG is not a variable-sized array. (Note
15247 that were talking about variable-sized arrays (like
15248 `int[n]'), rather than arrays of unknown size (like
15249 `int[]').) We'll get very confused by such a type since
15250 the bound of the array will not be computable in an
15251 instantiation. Besides, such types are not allowed in
15252 ISO C++, so we can do as we please here. */
15253 if (variably_modified_type_p (arg, NULL_TREE))
15254 return 1;
15256 /* Strip typedefs as in convert_template_argument. */
15257 arg = strip_typedefs (arg);
15260 /* If ARG is a parameter pack or an expansion, we cannot unify
15261 against it unless PARM is also a parameter pack. */
15262 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15263 && !template_parameter_pack_p (parm))
15264 return 1;
15266 /* If the argument deduction results is a METHOD_TYPE,
15267 then there is a problem.
15268 METHOD_TYPE doesn't map to any real C++ type the result of
15269 the deduction can not be of that type. */
15270 if (TREE_CODE (arg) == METHOD_TYPE)
15271 return 1;
15273 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15274 return 0;
15276 case TEMPLATE_PARM_INDEX:
15277 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15278 if (tparm == error_mark_node)
15279 return 1;
15281 if (TEMPLATE_PARM_LEVEL (parm)
15282 != template_decl_level (tparm))
15283 /* The PARM is not one we're trying to unify. Just check
15284 to see if it matches ARG. */
15285 return !(TREE_CODE (arg) == TREE_CODE (parm)
15286 && cp_tree_equal (parm, arg));
15288 idx = TEMPLATE_PARM_IDX (parm);
15289 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15291 if (targ)
15292 return !cp_tree_equal (targ, arg);
15294 /* [temp.deduct.type] If, in the declaration of a function template
15295 with a non-type template-parameter, the non-type
15296 template-parameter is used in an expression in the function
15297 parameter-list and, if the corresponding template-argument is
15298 deduced, the template-argument type shall match the type of the
15299 template-parameter exactly, except that a template-argument
15300 deduced from an array bound may be of any integral type.
15301 The non-type parameter might use already deduced type parameters. */
15302 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
15303 if (!TREE_TYPE (arg))
15304 /* Template-parameter dependent expression. Just accept it for now.
15305 It will later be processed in convert_template_argument. */
15307 else if (same_type_p (TREE_TYPE (arg), tparm))
15308 /* OK */;
15309 else if ((strict & UNIFY_ALLOW_INTEGER)
15310 && (TREE_CODE (tparm) == INTEGER_TYPE
15311 || TREE_CODE (tparm) == BOOLEAN_TYPE))
15312 /* Convert the ARG to the type of PARM; the deduced non-type
15313 template argument must exactly match the types of the
15314 corresponding parameter. */
15315 arg = fold (build_nop (tparm, arg));
15316 else if (uses_template_parms (tparm))
15317 /* We haven't deduced the type of this parameter yet. Try again
15318 later. */
15319 return 0;
15320 else
15321 return 1;
15323 /* If ARG is a parameter pack or an expansion, we cannot unify
15324 against it unless PARM is also a parameter pack. */
15325 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15326 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
15327 return 1;
15329 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15330 return 0;
15332 case PTRMEM_CST:
15334 /* A pointer-to-member constant can be unified only with
15335 another constant. */
15336 if (TREE_CODE (arg) != PTRMEM_CST)
15337 return 1;
15339 /* Just unify the class member. It would be useless (and possibly
15340 wrong, depending on the strict flags) to unify also
15341 PTRMEM_CST_CLASS, because we want to be sure that both parm and
15342 arg refer to the same variable, even if through different
15343 classes. For instance:
15345 struct A { int x; };
15346 struct B : A { };
15348 Unification of &A::x and &B::x must succeed. */
15349 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
15350 PTRMEM_CST_MEMBER (arg), strict);
15353 case POINTER_TYPE:
15355 if (TREE_CODE (arg) != POINTER_TYPE)
15356 return 1;
15358 /* [temp.deduct.call]
15360 A can be another pointer or pointer to member type that can
15361 be converted to the deduced A via a qualification
15362 conversion (_conv.qual_).
15364 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
15365 This will allow for additional cv-qualification of the
15366 pointed-to types if appropriate. */
15368 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
15369 /* The derived-to-base conversion only persists through one
15370 level of pointers. */
15371 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
15373 return unify (tparms, targs, TREE_TYPE (parm),
15374 TREE_TYPE (arg), strict);
15377 case REFERENCE_TYPE:
15378 if (TREE_CODE (arg) != REFERENCE_TYPE)
15379 return 1;
15380 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15381 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15383 case ARRAY_TYPE:
15384 if (TREE_CODE (arg) != ARRAY_TYPE)
15385 return 1;
15386 if ((TYPE_DOMAIN (parm) == NULL_TREE)
15387 != (TYPE_DOMAIN (arg) == NULL_TREE))
15388 return 1;
15389 if (TYPE_DOMAIN (parm) != NULL_TREE)
15391 tree parm_max;
15392 tree arg_max;
15393 bool parm_cst;
15394 bool arg_cst;
15396 /* Our representation of array types uses "N - 1" as the
15397 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
15398 not an integer constant. We cannot unify arbitrarily
15399 complex expressions, so we eliminate the MINUS_EXPRs
15400 here. */
15401 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
15402 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
15403 if (!parm_cst)
15405 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
15406 parm_max = TREE_OPERAND (parm_max, 0);
15408 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
15409 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
15410 if (!arg_cst)
15412 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15413 trying to unify the type of a variable with the type
15414 of a template parameter. For example:
15416 template <unsigned int N>
15417 void f (char (&) [N]);
15418 int g();
15419 void h(int i) {
15420 char a[g(i)];
15421 f(a);
15424 Here, the type of the ARG will be "int [g(i)]", and
15425 may be a SAVE_EXPR, etc. */
15426 if (TREE_CODE (arg_max) != MINUS_EXPR)
15427 return 1;
15428 arg_max = TREE_OPERAND (arg_max, 0);
15431 /* If only one of the bounds used a MINUS_EXPR, compensate
15432 by adding one to the other bound. */
15433 if (parm_cst && !arg_cst)
15434 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
15435 integer_type_node,
15436 parm_max,
15437 integer_one_node);
15438 else if (arg_cst && !parm_cst)
15439 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
15440 integer_type_node,
15441 arg_max,
15442 integer_one_node);
15444 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
15445 return 1;
15447 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15448 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15450 case REAL_TYPE:
15451 case COMPLEX_TYPE:
15452 case VECTOR_TYPE:
15453 case INTEGER_TYPE:
15454 case BOOLEAN_TYPE:
15455 case ENUMERAL_TYPE:
15456 case VOID_TYPE:
15457 if (TREE_CODE (arg) != TREE_CODE (parm))
15458 return 1;
15460 /* We have already checked cv-qualification at the top of the
15461 function. */
15462 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
15463 return 1;
15465 /* As far as unification is concerned, this wins. Later checks
15466 will invalidate it if necessary. */
15467 return 0;
15469 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15470 /* Type INTEGER_CST can come from ordinary constant template args. */
15471 case INTEGER_CST:
15472 while (TREE_CODE (arg) == NOP_EXPR)
15473 arg = TREE_OPERAND (arg, 0);
15475 if (TREE_CODE (arg) != INTEGER_CST)
15476 return 1;
15477 return !tree_int_cst_equal (parm, arg);
15479 case TREE_VEC:
15481 int i;
15482 if (TREE_CODE (arg) != TREE_VEC)
15483 return 1;
15484 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
15485 return 1;
15486 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
15487 if (unify (tparms, targs,
15488 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
15489 UNIFY_ALLOW_NONE))
15490 return 1;
15491 return 0;
15494 case RECORD_TYPE:
15495 case UNION_TYPE:
15496 if (TREE_CODE (arg) != TREE_CODE (parm))
15497 return 1;
15499 if (TYPE_PTRMEMFUNC_P (parm))
15501 if (!TYPE_PTRMEMFUNC_P (arg))
15502 return 1;
15504 return unify (tparms, targs,
15505 TYPE_PTRMEMFUNC_FN_TYPE (parm),
15506 TYPE_PTRMEMFUNC_FN_TYPE (arg),
15507 strict);
15510 if (CLASSTYPE_TEMPLATE_INFO (parm))
15512 tree t = NULL_TREE;
15514 if (strict_in & UNIFY_ALLOW_DERIVED)
15516 /* First, we try to unify the PARM and ARG directly. */
15517 t = try_class_unification (tparms, targs,
15518 parm, arg);
15520 if (!t)
15522 /* Fallback to the special case allowed in
15523 [temp.deduct.call]:
15525 If P is a class, and P has the form
15526 template-id, then A can be a derived class of
15527 the deduced A. Likewise, if P is a pointer to
15528 a class of the form template-id, A can be a
15529 pointer to a derived class pointed to by the
15530 deduced A. */
15531 t = get_template_base (tparms, targs, parm, arg);
15533 if (!t)
15534 return 1;
15537 else if (CLASSTYPE_TEMPLATE_INFO (arg)
15538 && (CLASSTYPE_TI_TEMPLATE (parm)
15539 == CLASSTYPE_TI_TEMPLATE (arg)))
15540 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15541 Then, we should unify `int' and `U'. */
15542 t = arg;
15543 else
15544 /* There's no chance of unification succeeding. */
15545 return 1;
15547 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
15548 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
15550 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
15551 return 1;
15552 return 0;
15554 case METHOD_TYPE:
15555 case FUNCTION_TYPE:
15557 unsigned int nargs;
15558 tree *args;
15559 tree a;
15560 unsigned int i;
15562 if (TREE_CODE (arg) != TREE_CODE (parm))
15563 return 1;
15565 /* CV qualifications for methods can never be deduced, they must
15566 match exactly. We need to check them explicitly here,
15567 because type_unification_real treats them as any other
15568 cv-qualified parameter. */
15569 if (TREE_CODE (parm) == METHOD_TYPE
15570 && (!check_cv_quals_for_unify
15571 (UNIFY_ALLOW_NONE,
15572 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
15573 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
15574 return 1;
15576 if (unify (tparms, targs, TREE_TYPE (parm),
15577 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15578 return 1;
15580 nargs = list_length (TYPE_ARG_TYPES (arg));
15581 args = XALLOCAVEC (tree, nargs);
15582 for (a = TYPE_ARG_TYPES (arg), i = 0;
15583 a != NULL_TREE && a != void_list_node;
15584 a = TREE_CHAIN (a), ++i)
15585 args[i] = TREE_VALUE (a);
15586 nargs = i;
15588 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15589 args, nargs, 1, DEDUCE_EXACT,
15590 LOOKUP_NORMAL);
15593 case OFFSET_TYPE:
15594 /* Unify a pointer to member with a pointer to member function, which
15595 deduces the type of the member as a function type. */
15596 if (TYPE_PTRMEMFUNC_P (arg))
15598 tree method_type;
15599 tree fntype;
15601 /* Check top-level cv qualifiers */
15602 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15603 return 1;
15605 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15606 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15607 return 1;
15609 /* Determine the type of the function we are unifying against. */
15610 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15611 fntype =
15612 build_function_type (TREE_TYPE (method_type),
15613 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15615 /* Extract the cv-qualifiers of the member function from the
15616 implicit object parameter and place them on the function
15617 type to be restored later. */
15618 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
15619 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15622 if (TREE_CODE (arg) != OFFSET_TYPE)
15623 return 1;
15624 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15625 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15626 return 1;
15627 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15628 strict);
15630 case CONST_DECL:
15631 if (DECL_TEMPLATE_PARM_P (parm))
15632 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15633 if (arg != integral_constant_value (parm))
15634 return 1;
15635 return 0;
15637 case FIELD_DECL:
15638 case TEMPLATE_DECL:
15639 /* Matched cases are handled by the ARG == PARM test above. */
15640 return 1;
15642 case VAR_DECL:
15643 /* A non-type template parameter that is a variable should be a
15644 an integral constant, in which case, it whould have been
15645 folded into its (constant) value. So we should not be getting
15646 a variable here. */
15647 gcc_unreachable ();
15649 case TYPE_ARGUMENT_PACK:
15650 case NONTYPE_ARGUMENT_PACK:
15652 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15653 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15654 int i, len = TREE_VEC_LENGTH (packed_parms);
15655 int argslen = TREE_VEC_LENGTH (packed_args);
15656 int parm_variadic_p = 0;
15658 for (i = 0; i < len; ++i)
15660 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15662 if (i == len - 1)
15663 /* We can unify against something with a trailing
15664 parameter pack. */
15665 parm_variadic_p = 1;
15666 else
15667 /* Since there is something following the pack
15668 expansion, we cannot unify this template argument
15669 list. */
15670 return 0;
15675 /* If we don't have enough arguments to satisfy the parameters
15676 (not counting the pack expression at the end), or we have
15677 too many arguments for a parameter list that doesn't end in
15678 a pack expression, we can't unify. */
15679 if (argslen < (len - parm_variadic_p)
15680 || (argslen > len && !parm_variadic_p))
15681 return 1;
15683 /* Unify all of the parameters that precede the (optional)
15684 pack expression. */
15685 for (i = 0; i < len - parm_variadic_p; ++i)
15687 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15688 TREE_VEC_ELT (packed_args, i), strict))
15689 return 1;
15692 if (parm_variadic_p)
15693 return unify_pack_expansion (tparms, targs,
15694 packed_parms, packed_args,
15695 strict, /*call_args_p=*/false,
15696 /*subr=*/false);
15697 return 0;
15700 break;
15702 case TYPEOF_TYPE:
15703 case DECLTYPE_TYPE:
15704 case UNDERLYING_TYPE:
15705 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
15706 or UNDERLYING_TYPE nodes. */
15707 return 0;
15709 case ERROR_MARK:
15710 /* Unification fails if we hit an error node. */
15711 return 1;
15713 default:
15714 /* An unresolved overload is a nondeduced context. */
15715 if (type_unknown_p (parm))
15716 return 0;
15717 gcc_assert (EXPR_P (parm));
15719 /* We must be looking at an expression. This can happen with
15720 something like:
15722 template <int I>
15723 void foo(S<I>, S<I + 2>);
15725 This is a "nondeduced context":
15727 [deduct.type]
15729 The nondeduced contexts are:
15731 --A type that is a template-id in which one or more of
15732 the template-arguments is an expression that references
15733 a template-parameter.
15735 In these cases, we assume deduction succeeded, but don't
15736 actually infer any unifications. */
15738 if (!uses_template_parms (parm)
15739 && !template_args_equal (parm, arg))
15740 return 1;
15741 else
15742 return 0;
15746 /* Note that DECL can be defined in this translation unit, if
15747 required. */
15749 static void
15750 mark_definable (tree decl)
15752 tree clone;
15753 DECL_NOT_REALLY_EXTERN (decl) = 1;
15754 FOR_EACH_CLONE (clone, decl)
15755 DECL_NOT_REALLY_EXTERN (clone) = 1;
15758 /* Called if RESULT is explicitly instantiated, or is a member of an
15759 explicitly instantiated class. */
15761 void
15762 mark_decl_instantiated (tree result, int extern_p)
15764 SET_DECL_EXPLICIT_INSTANTIATION (result);
15766 /* If this entity has already been written out, it's too late to
15767 make any modifications. */
15768 if (TREE_ASM_WRITTEN (result))
15769 return;
15771 if (TREE_CODE (result) != FUNCTION_DECL)
15772 /* The TREE_PUBLIC flag for function declarations will have been
15773 set correctly by tsubst. */
15774 TREE_PUBLIC (result) = 1;
15776 /* This might have been set by an earlier implicit instantiation. */
15777 DECL_COMDAT (result) = 0;
15779 if (extern_p)
15780 DECL_NOT_REALLY_EXTERN (result) = 0;
15781 else
15783 mark_definable (result);
15784 /* Always make artificials weak. */
15785 if (DECL_ARTIFICIAL (result) && flag_weak)
15786 comdat_linkage (result);
15787 /* For WIN32 we also want to put explicit instantiations in
15788 linkonce sections. */
15789 else if (TREE_PUBLIC (result))
15790 maybe_make_one_only (result);
15793 /* If EXTERN_P, then this function will not be emitted -- unless
15794 followed by an explicit instantiation, at which point its linkage
15795 will be adjusted. If !EXTERN_P, then this function will be
15796 emitted here. In neither circumstance do we want
15797 import_export_decl to adjust the linkage. */
15798 DECL_INTERFACE_KNOWN (result) = 1;
15801 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15802 important template arguments. If any are missing, we check whether
15803 they're important by using error_mark_node for substituting into any
15804 args that were used for partial ordering (the ones between ARGS and END)
15805 and seeing if it bubbles up. */
15807 static bool
15808 check_undeduced_parms (tree targs, tree args, tree end)
15810 bool found = false;
15811 int i;
15812 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15813 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15815 found = true;
15816 TREE_VEC_ELT (targs, i) = error_mark_node;
15818 if (found)
15820 for (; args != end; args = TREE_CHAIN (args))
15822 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15823 if (substed == error_mark_node)
15824 return true;
15827 return false;
15830 /* Given two function templates PAT1 and PAT2, return:
15832 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15833 -1 if PAT2 is more specialized than PAT1.
15834 0 if neither is more specialized.
15836 LEN indicates the number of parameters we should consider
15837 (defaulted parameters should not be considered).
15839 The 1998 std underspecified function template partial ordering, and
15840 DR214 addresses the issue. We take pairs of arguments, one from
15841 each of the templates, and deduce them against each other. One of
15842 the templates will be more specialized if all the *other*
15843 template's arguments deduce against its arguments and at least one
15844 of its arguments *does* *not* deduce against the other template's
15845 corresponding argument. Deduction is done as for class templates.
15846 The arguments used in deduction have reference and top level cv
15847 qualifiers removed. Iff both arguments were originally reference
15848 types *and* deduction succeeds in both directions, the template
15849 with the more cv-qualified argument wins for that pairing (if
15850 neither is more cv-qualified, they both are equal). Unlike regular
15851 deduction, after all the arguments have been deduced in this way,
15852 we do *not* verify the deduced template argument values can be
15853 substituted into non-deduced contexts.
15855 The logic can be a bit confusing here, because we look at deduce1 and
15856 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15857 can find template arguments for pat1 to make arg1 look like arg2, that
15858 means that arg2 is at least as specialized as arg1. */
15861 more_specialized_fn (tree pat1, tree pat2, int len)
15863 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15864 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15865 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15866 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15867 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15868 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15869 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15870 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15871 tree origs1, origs2;
15872 bool lose1 = false;
15873 bool lose2 = false;
15875 /* Remove the this parameter from non-static member functions. If
15876 one is a non-static member function and the other is not a static
15877 member function, remove the first parameter from that function
15878 also. This situation occurs for operator functions where we
15879 locate both a member function (with this pointer) and non-member
15880 operator (with explicit first operand). */
15881 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15883 len--; /* LEN is the number of significant arguments for DECL1 */
15884 args1 = TREE_CHAIN (args1);
15885 if (!DECL_STATIC_FUNCTION_P (decl2))
15886 args2 = TREE_CHAIN (args2);
15888 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15890 args2 = TREE_CHAIN (args2);
15891 if (!DECL_STATIC_FUNCTION_P (decl1))
15893 len--;
15894 args1 = TREE_CHAIN (args1);
15898 /* If only one is a conversion operator, they are unordered. */
15899 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15900 return 0;
15902 /* Consider the return type for a conversion function */
15903 if (DECL_CONV_FN_P (decl1))
15905 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15906 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15907 len++;
15910 processing_template_decl++;
15912 origs1 = args1;
15913 origs2 = args2;
15915 while (len--
15916 /* Stop when an ellipsis is seen. */
15917 && args1 != NULL_TREE && args2 != NULL_TREE)
15919 tree arg1 = TREE_VALUE (args1);
15920 tree arg2 = TREE_VALUE (args2);
15921 int deduce1, deduce2;
15922 int quals1 = -1;
15923 int quals2 = -1;
15925 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15926 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15928 /* When both arguments are pack expansions, we need only
15929 unify the patterns themselves. */
15930 arg1 = PACK_EXPANSION_PATTERN (arg1);
15931 arg2 = PACK_EXPANSION_PATTERN (arg2);
15933 /* This is the last comparison we need to do. */
15934 len = 0;
15937 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15939 arg1 = TREE_TYPE (arg1);
15940 quals1 = cp_type_quals (arg1);
15943 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15945 arg2 = TREE_TYPE (arg2);
15946 quals2 = cp_type_quals (arg2);
15949 if ((quals1 < 0) != (quals2 < 0))
15951 /* Only of the args is a reference, see if we should apply
15952 array/function pointer decay to it. This is not part of
15953 DR214, but is, IMHO, consistent with the deduction rules
15954 for the function call itself, and with our earlier
15955 implementation of the underspecified partial ordering
15956 rules. (nathan). */
15957 if (quals1 >= 0)
15959 switch (TREE_CODE (arg1))
15961 case ARRAY_TYPE:
15962 arg1 = TREE_TYPE (arg1);
15963 /* FALLTHROUGH. */
15964 case FUNCTION_TYPE:
15965 arg1 = build_pointer_type (arg1);
15966 break;
15968 default:
15969 break;
15972 else
15974 switch (TREE_CODE (arg2))
15976 case ARRAY_TYPE:
15977 arg2 = TREE_TYPE (arg2);
15978 /* FALLTHROUGH. */
15979 case FUNCTION_TYPE:
15980 arg2 = build_pointer_type (arg2);
15981 break;
15983 default:
15984 break;
15989 arg1 = TYPE_MAIN_VARIANT (arg1);
15990 arg2 = TYPE_MAIN_VARIANT (arg2);
15992 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15994 int i, len2 = list_length (args2);
15995 tree parmvec = make_tree_vec (1);
15996 tree argvec = make_tree_vec (len2);
15997 tree ta = args2;
15999 /* Setup the parameter vector, which contains only ARG1. */
16000 TREE_VEC_ELT (parmvec, 0) = arg1;
16002 /* Setup the argument vector, which contains the remaining
16003 arguments. */
16004 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
16005 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16007 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
16008 argvec, UNIFY_ALLOW_NONE,
16009 /*call_args_p=*/false,
16010 /*subr=*/0);
16012 /* We cannot deduce in the other direction, because ARG1 is
16013 a pack expansion but ARG2 is not. */
16014 deduce2 = 0;
16016 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16018 int i, len1 = list_length (args1);
16019 tree parmvec = make_tree_vec (1);
16020 tree argvec = make_tree_vec (len1);
16021 tree ta = args1;
16023 /* Setup the parameter vector, which contains only ARG1. */
16024 TREE_VEC_ELT (parmvec, 0) = arg2;
16026 /* Setup the argument vector, which contains the remaining
16027 arguments. */
16028 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
16029 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16031 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
16032 argvec, UNIFY_ALLOW_NONE,
16033 /*call_args_p=*/false,
16034 /*subr=*/0);
16036 /* We cannot deduce in the other direction, because ARG2 is
16037 a pack expansion but ARG1 is not.*/
16038 deduce1 = 0;
16041 else
16043 /* The normal case, where neither argument is a pack
16044 expansion. */
16045 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
16046 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
16049 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16050 arg2, then arg2 is not as specialized as arg1. */
16051 if (!deduce1)
16052 lose2 = true;
16053 if (!deduce2)
16054 lose1 = true;
16056 /* "If, for a given type, deduction succeeds in both directions
16057 (i.e., the types are identical after the transformations above)
16058 and if the type from the argument template is more cv-qualified
16059 than the type from the parameter template (as described above)
16060 that type is considered to be more specialized than the other. If
16061 neither type is more cv-qualified than the other then neither type
16062 is more specialized than the other." */
16064 if (deduce1 && deduce2
16065 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
16067 if ((quals1 & quals2) == quals2)
16068 lose2 = true;
16069 if ((quals1 & quals2) == quals1)
16070 lose1 = true;
16073 if (lose1 && lose2)
16074 /* We've failed to deduce something in either direction.
16075 These must be unordered. */
16076 break;
16078 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16079 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16080 /* We have already processed all of the arguments in our
16081 handing of the pack expansion type. */
16082 len = 0;
16084 args1 = TREE_CHAIN (args1);
16085 args2 = TREE_CHAIN (args2);
16088 /* "In most cases, all template parameters must have values in order for
16089 deduction to succeed, but for partial ordering purposes a template
16090 parameter may remain without a value provided it is not used in the
16091 types being used for partial ordering."
16093 Thus, if we are missing any of the targs1 we need to substitute into
16094 origs1, then pat2 is not as specialized as pat1. This can happen when
16095 there is a nondeduced context. */
16096 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
16097 lose2 = true;
16098 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
16099 lose1 = true;
16101 processing_template_decl--;
16103 /* All things being equal, if the next argument is a pack expansion
16104 for one function but not for the other, prefer the
16105 non-variadic function. FIXME this is bogus; see c++/41958. */
16106 if (lose1 == lose2
16107 && args1 && TREE_VALUE (args1)
16108 && args2 && TREE_VALUE (args2))
16110 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
16111 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
16114 if (lose1 == lose2)
16115 return 0;
16116 else if (!lose1)
16117 return 1;
16118 else
16119 return -1;
16122 /* Determine which of two partial specializations is more specialized.
16124 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16125 to the first partial specialization. The TREE_VALUE is the
16126 innermost set of template parameters for the partial
16127 specialization. PAT2 is similar, but for the second template.
16129 Return 1 if the first partial specialization is more specialized;
16130 -1 if the second is more specialized; 0 if neither is more
16131 specialized.
16133 See [temp.class.order] for information about determining which of
16134 two templates is more specialized. */
16136 static int
16137 more_specialized_class (tree pat1, tree pat2)
16139 tree targs;
16140 tree tmpl1, tmpl2;
16141 int winner = 0;
16142 bool any_deductions = false;
16144 tmpl1 = TREE_TYPE (pat1);
16145 tmpl2 = TREE_TYPE (pat2);
16147 /* Just like what happens for functions, if we are ordering between
16148 different class template specializations, we may encounter dependent
16149 types in the arguments, and we need our dependency check functions
16150 to behave correctly. */
16151 ++processing_template_decl;
16152 targs = get_class_bindings (TREE_VALUE (pat1),
16153 CLASSTYPE_TI_ARGS (tmpl1),
16154 CLASSTYPE_TI_ARGS (tmpl2));
16155 if (targs)
16157 --winner;
16158 any_deductions = true;
16161 targs = get_class_bindings (TREE_VALUE (pat2),
16162 CLASSTYPE_TI_ARGS (tmpl2),
16163 CLASSTYPE_TI_ARGS (tmpl1));
16164 if (targs)
16166 ++winner;
16167 any_deductions = true;
16169 --processing_template_decl;
16171 /* In the case of a tie where at least one of the class templates
16172 has a parameter pack at the end, the template with the most
16173 non-packed parameters wins. */
16174 if (winner == 0
16175 && any_deductions
16176 && (template_args_variadic_p (TREE_PURPOSE (pat1))
16177 || template_args_variadic_p (TREE_PURPOSE (pat2))))
16179 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
16180 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
16181 int len1 = TREE_VEC_LENGTH (args1);
16182 int len2 = TREE_VEC_LENGTH (args2);
16184 /* We don't count the pack expansion at the end. */
16185 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
16186 --len1;
16187 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
16188 --len2;
16190 if (len1 > len2)
16191 return 1;
16192 else if (len1 < len2)
16193 return -1;
16196 return winner;
16199 /* Return the template arguments that will produce the function signature
16200 DECL from the function template FN, with the explicit template
16201 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
16202 also match. Return NULL_TREE if no satisfactory arguments could be
16203 found. */
16205 static tree
16206 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
16208 int ntparms = DECL_NTPARMS (fn);
16209 tree targs = make_tree_vec (ntparms);
16210 tree decl_type;
16211 tree decl_arg_types;
16212 tree *args;
16213 unsigned int nargs, ix;
16214 tree arg;
16216 /* Substitute the explicit template arguments into the type of DECL.
16217 The call to fn_type_unification will handle substitution into the
16218 FN. */
16219 decl_type = TREE_TYPE (decl);
16220 if (explicit_args && uses_template_parms (decl_type))
16222 tree tmpl;
16223 tree converted_args;
16225 if (DECL_TEMPLATE_INFO (decl))
16226 tmpl = DECL_TI_TEMPLATE (decl);
16227 else
16228 /* We can get here for some invalid specializations. */
16229 return NULL_TREE;
16231 converted_args
16232 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16233 explicit_args, NULL_TREE,
16234 tf_none,
16235 /*require_all_args=*/false,
16236 /*use_default_args=*/false);
16237 if (converted_args == error_mark_node)
16238 return NULL_TREE;
16240 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
16241 if (decl_type == error_mark_node)
16242 return NULL_TREE;
16245 /* Never do unification on the 'this' parameter. */
16246 decl_arg_types = skip_artificial_parms_for (decl,
16247 TYPE_ARG_TYPES (decl_type));
16249 nargs = list_length (decl_arg_types);
16250 args = XALLOCAVEC (tree, nargs);
16251 for (arg = decl_arg_types, ix = 0;
16252 arg != NULL_TREE && arg != void_list_node;
16253 arg = TREE_CHAIN (arg), ++ix)
16254 args[ix] = TREE_VALUE (arg);
16256 if (fn_type_unification (fn, explicit_args, targs,
16257 args, ix,
16258 (check_rettype || DECL_CONV_FN_P (fn)
16259 ? TREE_TYPE (decl_type) : NULL_TREE),
16260 DEDUCE_EXACT, LOOKUP_NORMAL))
16261 return NULL_TREE;
16263 return targs;
16266 /* Return the innermost template arguments that, when applied to a
16267 template specialization whose innermost template parameters are
16268 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
16269 ARGS.
16271 For example, suppose we have:
16273 template <class T, class U> struct S {};
16274 template <class T> struct S<T*, int> {};
16276 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
16277 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
16278 int}. The resulting vector will be {double}, indicating that `T'
16279 is bound to `double'. */
16281 static tree
16282 get_class_bindings (tree tparms, tree spec_args, tree args)
16284 int i, ntparms = TREE_VEC_LENGTH (tparms);
16285 tree deduced_args;
16286 tree innermost_deduced_args;
16288 innermost_deduced_args = make_tree_vec (ntparms);
16289 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16291 deduced_args = copy_node (args);
16292 SET_TMPL_ARGS_LEVEL (deduced_args,
16293 TMPL_ARGS_DEPTH (deduced_args),
16294 innermost_deduced_args);
16296 else
16297 deduced_args = innermost_deduced_args;
16299 if (unify (tparms, deduced_args,
16300 INNERMOST_TEMPLATE_ARGS (spec_args),
16301 INNERMOST_TEMPLATE_ARGS (args),
16302 UNIFY_ALLOW_NONE))
16303 return NULL_TREE;
16305 for (i = 0; i < ntparms; ++i)
16306 if (! TREE_VEC_ELT (innermost_deduced_args, i))
16307 return NULL_TREE;
16309 /* Verify that nondeduced template arguments agree with the type
16310 obtained from argument deduction.
16312 For example:
16314 struct A { typedef int X; };
16315 template <class T, class U> struct C {};
16316 template <class T> struct C<T, typename T::X> {};
16318 Then with the instantiation `C<A, int>', we can deduce that
16319 `T' is `A' but unify () does not check whether `typename T::X'
16320 is `int'. */
16321 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
16322 if (spec_args == error_mark_node
16323 /* We only need to check the innermost arguments; the other
16324 arguments will always agree. */
16325 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
16326 INNERMOST_TEMPLATE_ARGS (args)))
16327 return NULL_TREE;
16329 /* Now that we have bindings for all of the template arguments,
16330 ensure that the arguments deduced for the template template
16331 parameters have compatible template parameter lists. See the use
16332 of template_template_parm_bindings_ok_p in fn_type_unification
16333 for more information. */
16334 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
16335 return NULL_TREE;
16337 return deduced_args;
16340 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
16341 Return the TREE_LIST node with the most specialized template, if
16342 any. If there is no most specialized template, the error_mark_node
16343 is returned.
16345 Note that this function does not look at, or modify, the
16346 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
16347 returned is one of the elements of INSTANTIATIONS, callers may
16348 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
16349 and retrieve it from the value returned. */
16351 tree
16352 most_specialized_instantiation (tree templates)
16354 tree fn, champ;
16356 ++processing_template_decl;
16358 champ = templates;
16359 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
16361 int fate = 0;
16363 if (get_bindings (TREE_VALUE (champ),
16364 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
16365 NULL_TREE, /*check_ret=*/false))
16366 fate--;
16368 if (get_bindings (TREE_VALUE (fn),
16369 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
16370 NULL_TREE, /*check_ret=*/false))
16371 fate++;
16373 if (fate == -1)
16374 champ = fn;
16375 else if (!fate)
16377 /* Equally specialized, move to next function. If there
16378 is no next function, nothing's most specialized. */
16379 fn = TREE_CHAIN (fn);
16380 champ = fn;
16381 if (!fn)
16382 break;
16386 if (champ)
16387 /* Now verify that champ is better than everything earlier in the
16388 instantiation list. */
16389 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
16390 if (get_bindings (TREE_VALUE (champ),
16391 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
16392 NULL_TREE, /*check_ret=*/false)
16393 || !get_bindings (TREE_VALUE (fn),
16394 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
16395 NULL_TREE, /*check_ret=*/false))
16397 champ = NULL_TREE;
16398 break;
16401 processing_template_decl--;
16403 if (!champ)
16404 return error_mark_node;
16406 return champ;
16409 /* If DECL is a specialization of some template, return the most
16410 general such template. Otherwise, returns NULL_TREE.
16412 For example, given:
16414 template <class T> struct S { template <class U> void f(U); };
16416 if TMPL is `template <class U> void S<int>::f(U)' this will return
16417 the full template. This function will not trace past partial
16418 specializations, however. For example, given in addition:
16420 template <class T> struct S<T*> { template <class U> void f(U); };
16422 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16423 `template <class T> template <class U> S<T*>::f(U)'. */
16425 tree
16426 most_general_template (tree decl)
16428 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16429 an immediate specialization. */
16430 if (TREE_CODE (decl) == FUNCTION_DECL)
16432 if (DECL_TEMPLATE_INFO (decl)) {
16433 decl = DECL_TI_TEMPLATE (decl);
16435 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16436 template friend. */
16437 if (TREE_CODE (decl) != TEMPLATE_DECL)
16438 return NULL_TREE;
16439 } else
16440 return NULL_TREE;
16443 /* Look for more and more general templates. */
16444 while (DECL_TEMPLATE_INFO (decl))
16446 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16447 (See cp-tree.h for details.) */
16448 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
16449 break;
16451 if (CLASS_TYPE_P (TREE_TYPE (decl))
16452 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
16453 break;
16455 /* Stop if we run into an explicitly specialized class template. */
16456 if (!DECL_NAMESPACE_SCOPE_P (decl)
16457 && DECL_CONTEXT (decl)
16458 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
16459 break;
16461 decl = DECL_TI_TEMPLATE (decl);
16464 return decl;
16467 /* Return the most specialized of the class template partial
16468 specializations of TMPL which can produce TYPE, a specialization of
16469 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16470 a _TYPE node corresponding to the partial specialization, while the
16471 TREE_PURPOSE is the set of template arguments that must be
16472 substituted into the TREE_TYPE in order to generate TYPE.
16474 If the choice of partial specialization is ambiguous, a diagnostic
16475 is issued, and the error_mark_node is returned. If there are no
16476 partial specializations of TMPL matching TYPE, then NULL_TREE is
16477 returned. */
16479 static tree
16480 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
16482 tree list = NULL_TREE;
16483 tree t;
16484 tree champ;
16485 int fate;
16486 bool ambiguous_p;
16487 tree args;
16488 tree outer_args = NULL_TREE;
16490 tmpl = most_general_template (tmpl);
16491 args = CLASSTYPE_TI_ARGS (type);
16493 /* For determining which partial specialization to use, only the
16494 innermost args are interesting. */
16495 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16497 outer_args = strip_innermost_template_args (args, 1);
16498 args = INNERMOST_TEMPLATE_ARGS (args);
16501 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
16503 tree partial_spec_args;
16504 tree spec_args;
16505 tree parms = TREE_VALUE (t);
16507 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
16509 ++processing_template_decl;
16511 if (outer_args)
16513 int i;
16515 /* Discard the outer levels of args, and then substitute in the
16516 template args from the enclosing class. */
16517 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
16518 partial_spec_args = tsubst_template_args
16519 (partial_spec_args, outer_args, tf_none, NULL_TREE);
16521 /* PARMS already refers to just the innermost parms, but the
16522 template parms in partial_spec_args had their levels lowered
16523 by tsubst, so we need to do the same for the parm list. We
16524 can't just tsubst the TREE_VEC itself, as tsubst wants to
16525 treat a TREE_VEC as an argument vector. */
16526 parms = copy_node (parms);
16527 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
16528 TREE_VEC_ELT (parms, i) =
16529 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
16533 partial_spec_args =
16534 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16535 add_to_template_args (outer_args,
16536 partial_spec_args),
16537 tmpl, tf_none,
16538 /*require_all_args=*/true,
16539 /*use_default_args=*/true);
16541 --processing_template_decl;
16543 if (partial_spec_args == error_mark_node)
16544 return error_mark_node;
16546 spec_args = get_class_bindings (parms,
16547 partial_spec_args,
16548 args);
16549 if (spec_args)
16551 if (outer_args)
16552 spec_args = add_to_template_args (outer_args, spec_args);
16553 list = tree_cons (spec_args, TREE_VALUE (t), list);
16554 TREE_TYPE (list) = TREE_TYPE (t);
16558 if (! list)
16559 return NULL_TREE;
16561 ambiguous_p = false;
16562 t = list;
16563 champ = t;
16564 t = TREE_CHAIN (t);
16565 for (; t; t = TREE_CHAIN (t))
16567 fate = more_specialized_class (champ, t);
16568 if (fate == 1)
16570 else
16572 if (fate == 0)
16574 t = TREE_CHAIN (t);
16575 if (! t)
16577 ambiguous_p = true;
16578 break;
16581 champ = t;
16585 if (!ambiguous_p)
16586 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16588 fate = more_specialized_class (champ, t);
16589 if (fate != 1)
16591 ambiguous_p = true;
16592 break;
16596 if (ambiguous_p)
16598 const char *str;
16599 char *spaces = NULL;
16600 if (!(complain & tf_error))
16601 return error_mark_node;
16602 error ("ambiguous class template instantiation for %q#T", type);
16603 str = ngettext ("candidate is:", "candidates are:", list_length (list));
16604 for (t = list; t; t = TREE_CHAIN (t))
16606 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16607 spaces = spaces ? spaces : get_spaces (str);
16609 free (spaces);
16610 return error_mark_node;
16613 return champ;
16616 /* Explicitly instantiate DECL. */
16618 void
16619 do_decl_instantiation (tree decl, tree storage)
16621 tree result = NULL_TREE;
16622 int extern_p = 0;
16624 if (!decl || decl == error_mark_node)
16625 /* An error occurred, for which grokdeclarator has already issued
16626 an appropriate message. */
16627 return;
16628 else if (! DECL_LANG_SPECIFIC (decl))
16630 error ("explicit instantiation of non-template %q#D", decl);
16631 return;
16633 else if (TREE_CODE (decl) == VAR_DECL)
16635 /* There is an asymmetry here in the way VAR_DECLs and
16636 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16637 the latter, the DECL we get back will be marked as a
16638 template instantiation, and the appropriate
16639 DECL_TEMPLATE_INFO will be set up. This does not happen for
16640 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16641 should handle VAR_DECLs as it currently handles
16642 FUNCTION_DECLs. */
16643 if (!DECL_CLASS_SCOPE_P (decl))
16645 error ("%qD is not a static data member of a class template", decl);
16646 return;
16648 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16649 if (!result || TREE_CODE (result) != VAR_DECL)
16651 error ("no matching template for %qD found", decl);
16652 return;
16654 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16656 error ("type %qT for explicit instantiation %qD does not match "
16657 "declared type %qT", TREE_TYPE (result), decl,
16658 TREE_TYPE (decl));
16659 return;
16662 else if (TREE_CODE (decl) != FUNCTION_DECL)
16664 error ("explicit instantiation of %q#D", decl);
16665 return;
16667 else
16668 result = decl;
16670 /* Check for various error cases. Note that if the explicit
16671 instantiation is valid the RESULT will currently be marked as an
16672 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16673 until we get here. */
16675 if (DECL_TEMPLATE_SPECIALIZATION (result))
16677 /* DR 259 [temp.spec].
16679 Both an explicit instantiation and a declaration of an explicit
16680 specialization shall not appear in a program unless the explicit
16681 instantiation follows a declaration of the explicit specialization.
16683 For a given set of template parameters, if an explicit
16684 instantiation of a template appears after a declaration of an
16685 explicit specialization for that template, the explicit
16686 instantiation has no effect. */
16687 return;
16689 else if (DECL_EXPLICIT_INSTANTIATION (result))
16691 /* [temp.spec]
16693 No program shall explicitly instantiate any template more
16694 than once.
16696 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16697 the first instantiation was `extern' and the second is not,
16698 and EXTERN_P for the opposite case. */
16699 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16700 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16701 /* If an "extern" explicit instantiation follows an ordinary
16702 explicit instantiation, the template is instantiated. */
16703 if (extern_p)
16704 return;
16706 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16708 error ("no matching template for %qD found", result);
16709 return;
16711 else if (!DECL_TEMPLATE_INFO (result))
16713 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16714 return;
16717 if (storage == NULL_TREE)
16719 else if (storage == ridpointers[(int) RID_EXTERN])
16721 if (!in_system_header && (cxx_dialect == cxx98))
16722 pedwarn (input_location, OPT_pedantic,
16723 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16724 "instantiations");
16725 extern_p = 1;
16727 else
16728 error ("storage class %qD applied to template instantiation", storage);
16730 check_explicit_instantiation_namespace (result);
16731 mark_decl_instantiated (result, extern_p);
16732 if (! extern_p)
16733 instantiate_decl (result, /*defer_ok=*/1,
16734 /*expl_inst_class_mem_p=*/false);
16737 static void
16738 mark_class_instantiated (tree t, int extern_p)
16740 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16741 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16742 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
16743 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
16744 if (! extern_p)
16746 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
16747 rest_of_type_compilation (t, 1);
16751 /* Called from do_type_instantiation through binding_table_foreach to
16752 do recursive instantiation for the type bound in ENTRY. */
16753 static void
16754 bt_instantiate_type_proc (binding_entry entry, void *data)
16756 tree storage = *(tree *) data;
16758 if (MAYBE_CLASS_TYPE_P (entry->type)
16759 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
16760 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
16763 /* Called from do_type_instantiation to instantiate a member
16764 (a member function or a static member variable) of an
16765 explicitly instantiated class template. */
16766 static void
16767 instantiate_class_member (tree decl, int extern_p)
16769 mark_decl_instantiated (decl, extern_p);
16770 if (! extern_p)
16771 instantiate_decl (decl, /*defer_ok=*/1,
16772 /*expl_inst_class_mem_p=*/true);
16775 /* Perform an explicit instantiation of template class T. STORAGE, if
16776 non-null, is the RID for extern, inline or static. COMPLAIN is
16777 nonzero if this is called from the parser, zero if called recursively,
16778 since the standard is unclear (as detailed below). */
16780 void
16781 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
16783 int extern_p = 0;
16784 int nomem_p = 0;
16785 int static_p = 0;
16786 int previous_instantiation_extern_p = 0;
16788 if (TREE_CODE (t) == TYPE_DECL)
16789 t = TREE_TYPE (t);
16791 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
16793 error ("explicit instantiation of non-template type %qT", t);
16794 return;
16797 complete_type (t);
16799 if (!COMPLETE_TYPE_P (t))
16801 if (complain & tf_error)
16802 error ("explicit instantiation of %q#T before definition of template",
16804 return;
16807 if (storage != NULL_TREE)
16809 if (!in_system_header)
16811 if (storage == ridpointers[(int) RID_EXTERN])
16813 if (cxx_dialect == cxx98)
16814 pedwarn (input_location, OPT_pedantic,
16815 "ISO C++ 1998 forbids the use of %<extern%> on "
16816 "explicit instantiations");
16818 else
16819 pedwarn (input_location, OPT_pedantic,
16820 "ISO C++ forbids the use of %qE"
16821 " on explicit instantiations", storage);
16824 if (storage == ridpointers[(int) RID_INLINE])
16825 nomem_p = 1;
16826 else if (storage == ridpointers[(int) RID_EXTERN])
16827 extern_p = 1;
16828 else if (storage == ridpointers[(int) RID_STATIC])
16829 static_p = 1;
16830 else
16832 error ("storage class %qD applied to template instantiation",
16833 storage);
16834 extern_p = 0;
16838 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16840 /* DR 259 [temp.spec].
16842 Both an explicit instantiation and a declaration of an explicit
16843 specialization shall not appear in a program unless the explicit
16844 instantiation follows a declaration of the explicit specialization.
16846 For a given set of template parameters, if an explicit
16847 instantiation of a template appears after a declaration of an
16848 explicit specialization for that template, the explicit
16849 instantiation has no effect. */
16850 return;
16852 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16854 /* [temp.spec]
16856 No program shall explicitly instantiate any template more
16857 than once.
16859 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16860 instantiation was `extern'. If EXTERN_P then the second is.
16861 These cases are OK. */
16862 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16864 if (!previous_instantiation_extern_p && !extern_p
16865 && (complain & tf_error))
16866 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16868 /* If we've already instantiated the template, just return now. */
16869 if (!CLASSTYPE_INTERFACE_ONLY (t))
16870 return;
16873 check_explicit_instantiation_namespace (TYPE_NAME (t));
16874 mark_class_instantiated (t, extern_p);
16876 if (nomem_p)
16877 return;
16880 tree tmp;
16882 /* In contrast to implicit instantiation, where only the
16883 declarations, and not the definitions, of members are
16884 instantiated, we have here:
16886 [temp.explicit]
16888 The explicit instantiation of a class template specialization
16889 implies the instantiation of all of its members not
16890 previously explicitly specialized in the translation unit
16891 containing the explicit instantiation.
16893 Of course, we can't instantiate member template classes, since
16894 we don't have any arguments for them. Note that the standard
16895 is unclear on whether the instantiation of the members are
16896 *explicit* instantiations or not. However, the most natural
16897 interpretation is that it should be an explicit instantiation. */
16899 if (! static_p)
16900 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
16901 if (TREE_CODE (tmp) == FUNCTION_DECL
16902 && DECL_TEMPLATE_INSTANTIATION (tmp))
16903 instantiate_class_member (tmp, extern_p);
16905 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
16906 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16907 instantiate_class_member (tmp, extern_p);
16909 if (CLASSTYPE_NESTED_UTDS (t))
16910 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16911 bt_instantiate_type_proc, &storage);
16915 /* Given a function DECL, which is a specialization of TMPL, modify
16916 DECL to be a re-instantiation of TMPL with the same template
16917 arguments. TMPL should be the template into which tsubst'ing
16918 should occur for DECL, not the most general template.
16920 One reason for doing this is a scenario like this:
16922 template <class T>
16923 void f(const T&, int i);
16925 void g() { f(3, 7); }
16927 template <class T>
16928 void f(const T& t, const int i) { }
16930 Note that when the template is first instantiated, with
16931 instantiate_template, the resulting DECL will have no name for the
16932 first parameter, and the wrong type for the second. So, when we go
16933 to instantiate the DECL, we regenerate it. */
16935 static void
16936 regenerate_decl_from_template (tree decl, tree tmpl)
16938 /* The arguments used to instantiate DECL, from the most general
16939 template. */
16940 tree args;
16941 tree code_pattern;
16943 args = DECL_TI_ARGS (decl);
16944 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16946 /* Make sure that we can see identifiers, and compute access
16947 correctly. */
16948 push_access_scope (decl);
16950 if (TREE_CODE (decl) == FUNCTION_DECL)
16952 tree decl_parm;
16953 tree pattern_parm;
16954 tree specs;
16955 int args_depth;
16956 int parms_depth;
16958 args_depth = TMPL_ARGS_DEPTH (args);
16959 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16960 if (args_depth > parms_depth)
16961 args = get_innermost_template_args (args, parms_depth);
16963 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16964 args, tf_error, NULL_TREE);
16965 if (specs)
16966 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16967 specs);
16969 /* Merge parameter declarations. */
16970 decl_parm = skip_artificial_parms_for (decl,
16971 DECL_ARGUMENTS (decl));
16972 pattern_parm
16973 = skip_artificial_parms_for (code_pattern,
16974 DECL_ARGUMENTS (code_pattern));
16975 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16977 tree parm_type;
16978 tree attributes;
16980 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16981 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16982 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16983 NULL_TREE);
16984 parm_type = type_decays_to (parm_type);
16985 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16986 TREE_TYPE (decl_parm) = parm_type;
16987 attributes = DECL_ATTRIBUTES (pattern_parm);
16988 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16990 DECL_ATTRIBUTES (decl_parm) = attributes;
16991 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16993 decl_parm = DECL_CHAIN (decl_parm);
16994 pattern_parm = DECL_CHAIN (pattern_parm);
16996 /* Merge any parameters that match with the function parameter
16997 pack. */
16998 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
17000 int i, len;
17001 tree expanded_types;
17002 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17003 the parameters in this function parameter pack. */
17004 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
17005 args, tf_error, NULL_TREE);
17006 len = TREE_VEC_LENGTH (expanded_types);
17007 for (i = 0; i < len; i++)
17009 tree parm_type;
17010 tree attributes;
17012 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17013 /* Rename the parameter to include the index. */
17014 DECL_NAME (decl_parm) =
17015 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
17016 parm_type = TREE_VEC_ELT (expanded_types, i);
17017 parm_type = type_decays_to (parm_type);
17018 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17019 TREE_TYPE (decl_parm) = parm_type;
17020 attributes = DECL_ATTRIBUTES (pattern_parm);
17021 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17023 DECL_ATTRIBUTES (decl_parm) = attributes;
17024 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17026 decl_parm = DECL_CHAIN (decl_parm);
17029 /* Merge additional specifiers from the CODE_PATTERN. */
17030 if (DECL_DECLARED_INLINE_P (code_pattern)
17031 && !DECL_DECLARED_INLINE_P (decl))
17032 DECL_DECLARED_INLINE_P (decl) = 1;
17034 else if (TREE_CODE (decl) == VAR_DECL)
17036 DECL_INITIAL (decl) =
17037 tsubst_expr (DECL_INITIAL (code_pattern), args,
17038 tf_error, DECL_TI_TEMPLATE (decl),
17039 /*integral_constant_expression_p=*/false);
17040 if (VAR_HAD_UNKNOWN_BOUND (decl))
17041 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
17042 tf_error, DECL_TI_TEMPLATE (decl));
17044 else
17045 gcc_unreachable ();
17047 pop_access_scope (decl);
17050 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17051 substituted to get DECL. */
17053 tree
17054 template_for_substitution (tree decl)
17056 tree tmpl = DECL_TI_TEMPLATE (decl);
17058 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17059 for the instantiation. This is not always the most general
17060 template. Consider, for example:
17062 template <class T>
17063 struct S { template <class U> void f();
17064 template <> void f<int>(); };
17066 and an instantiation of S<double>::f<int>. We want TD to be the
17067 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17068 while (/* An instantiation cannot have a definition, so we need a
17069 more general template. */
17070 DECL_TEMPLATE_INSTANTIATION (tmpl)
17071 /* We must also deal with friend templates. Given:
17073 template <class T> struct S {
17074 template <class U> friend void f() {};
17077 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17078 so far as the language is concerned, but that's still
17079 where we get the pattern for the instantiation from. On
17080 other hand, if the definition comes outside the class, say:
17082 template <class T> struct S {
17083 template <class U> friend void f();
17085 template <class U> friend void f() {}
17087 we don't need to look any further. That's what the check for
17088 DECL_INITIAL is for. */
17089 || (TREE_CODE (decl) == FUNCTION_DECL
17090 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
17091 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
17093 /* The present template, TD, should not be a definition. If it
17094 were a definition, we should be using it! Note that we
17095 cannot restructure the loop to just keep going until we find
17096 a template with a definition, since that might go too far if
17097 a specialization was declared, but not defined. */
17098 gcc_assert (TREE_CODE (decl) != VAR_DECL
17099 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
17101 /* Fetch the more general template. */
17102 tmpl = DECL_TI_TEMPLATE (tmpl);
17105 return tmpl;
17108 /* Returns true if we need to instantiate this template instance even if we
17109 know we aren't going to emit it.. */
17111 bool
17112 always_instantiate_p (tree decl)
17114 /* We always instantiate inline functions so that we can inline them. An
17115 explicit instantiation declaration prohibits implicit instantiation of
17116 non-inline functions. With high levels of optimization, we would
17117 normally inline non-inline functions -- but we're not allowed to do
17118 that for "extern template" functions. Therefore, we check
17119 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17120 return ((TREE_CODE (decl) == FUNCTION_DECL
17121 && DECL_DECLARED_INLINE_P (decl))
17122 /* And we need to instantiate static data members so that
17123 their initializers are available in integral constant
17124 expressions. */
17125 || (TREE_CODE (decl) == VAR_DECL
17126 && decl_maybe_constant_var_p (decl)));
17129 /* Produce the definition of D, a _DECL generated from a template. If
17130 DEFER_OK is nonzero, then we don't have to actually do the
17131 instantiation now; we just have to do it sometime. Normally it is
17132 an error if this is an explicit instantiation but D is undefined.
17133 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
17134 explicitly instantiated class template. */
17136 tree
17137 instantiate_decl (tree d, int defer_ok,
17138 bool expl_inst_class_mem_p)
17140 tree tmpl = DECL_TI_TEMPLATE (d);
17141 tree gen_args;
17142 tree args;
17143 tree td;
17144 tree code_pattern;
17145 tree spec;
17146 tree gen_tmpl;
17147 bool pattern_defined;
17148 int need_push;
17149 location_t saved_loc = input_location;
17150 bool external_p;
17152 /* This function should only be used to instantiate templates for
17153 functions and static member variables. */
17154 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
17155 || TREE_CODE (d) == VAR_DECL);
17157 /* Variables are never deferred; if instantiation is required, they
17158 are instantiated right away. That allows for better code in the
17159 case that an expression refers to the value of the variable --
17160 if the variable has a constant value the referring expression can
17161 take advantage of that fact. */
17162 if (TREE_CODE (d) == VAR_DECL
17163 || DECL_DECLARED_CONSTEXPR_P (d))
17164 defer_ok = 0;
17166 /* Don't instantiate cloned functions. Instead, instantiate the
17167 functions they cloned. */
17168 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
17169 d = DECL_CLONED_FUNCTION (d);
17171 if (DECL_TEMPLATE_INSTANTIATED (d)
17172 || DECL_TEMPLATE_SPECIALIZATION (d))
17173 /* D has already been instantiated or explicitly specialized, so
17174 there's nothing for us to do here.
17176 It might seem reasonable to check whether or not D is an explicit
17177 instantiation, and, if so, stop here. But when an explicit
17178 instantiation is deferred until the end of the compilation,
17179 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
17180 the instantiation. */
17181 return d;
17183 /* Check to see whether we know that this template will be
17184 instantiated in some other file, as with "extern template"
17185 extension. */
17186 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
17188 /* In general, we do not instantiate such templates. */
17189 if (external_p && !always_instantiate_p (d))
17190 return d;
17192 gen_tmpl = most_general_template (tmpl);
17193 gen_args = DECL_TI_ARGS (d);
17195 if (tmpl != gen_tmpl)
17196 /* We should already have the extra args. */
17197 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
17198 == TMPL_ARGS_DEPTH (gen_args));
17199 /* And what's in the hash table should match D. */
17200 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
17201 || spec == NULL_TREE);
17203 /* This needs to happen before any tsubsting. */
17204 if (! push_tinst_level (d))
17205 return d;
17207 timevar_push (TV_PARSE);
17209 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
17210 for the instantiation. */
17211 td = template_for_substitution (d);
17212 code_pattern = DECL_TEMPLATE_RESULT (td);
17214 /* We should never be trying to instantiate a member of a class
17215 template or partial specialization. */
17216 gcc_assert (d != code_pattern);
17218 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
17219 || DECL_TEMPLATE_SPECIALIZATION (td))
17220 /* In the case of a friend template whose definition is provided
17221 outside the class, we may have too many arguments. Drop the
17222 ones we don't need. The same is true for specializations. */
17223 args = get_innermost_template_args
17224 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
17225 else
17226 args = gen_args;
17228 if (TREE_CODE (d) == FUNCTION_DECL)
17229 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
17230 else
17231 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
17233 /* We may be in the middle of deferred access check. Disable it now. */
17234 push_deferring_access_checks (dk_no_deferred);
17236 /* Unless an explicit instantiation directive has already determined
17237 the linkage of D, remember that a definition is available for
17238 this entity. */
17239 if (pattern_defined
17240 && !DECL_INTERFACE_KNOWN (d)
17241 && !DECL_NOT_REALLY_EXTERN (d))
17242 mark_definable (d);
17244 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
17245 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
17246 input_location = DECL_SOURCE_LOCATION (d);
17248 /* If D is a member of an explicitly instantiated class template,
17249 and no definition is available, treat it like an implicit
17250 instantiation. */
17251 if (!pattern_defined && expl_inst_class_mem_p
17252 && DECL_EXPLICIT_INSTANTIATION (d))
17254 /* Leave linkage flags alone on instantiations with anonymous
17255 visibility. */
17256 if (TREE_PUBLIC (d))
17258 DECL_NOT_REALLY_EXTERN (d) = 0;
17259 DECL_INTERFACE_KNOWN (d) = 0;
17261 SET_DECL_IMPLICIT_INSTANTIATION (d);
17264 /* Recheck the substitutions to obtain any warning messages
17265 about ignoring cv qualifiers. Don't do this for artificial decls,
17266 as it breaks the context-sensitive substitution for lambda op(). */
17267 if (!defer_ok && !DECL_ARTIFICIAL (d))
17269 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
17270 tree type = TREE_TYPE (gen);
17272 /* Make sure that we can see identifiers, and compute access
17273 correctly. D is already the target FUNCTION_DECL with the
17274 right context. */
17275 push_access_scope (d);
17277 if (TREE_CODE (gen) == FUNCTION_DECL)
17279 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
17280 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
17282 /* Don't simply tsubst the function type, as that will give
17283 duplicate warnings about poor parameter qualifications.
17284 The function arguments are the same as the decl_arguments
17285 without the top level cv qualifiers. */
17286 type = TREE_TYPE (type);
17288 tsubst (type, gen_args, tf_warning_or_error, d);
17290 pop_access_scope (d);
17293 /* Defer all other templates, unless we have been explicitly
17294 forbidden from doing so. */
17295 if (/* If there is no definition, we cannot instantiate the
17296 template. */
17297 ! pattern_defined
17298 /* If it's OK to postpone instantiation, do so. */
17299 || defer_ok
17300 /* If this is a static data member that will be defined
17301 elsewhere, we don't want to instantiate the entire data
17302 member, but we do want to instantiate the initializer so that
17303 we can substitute that elsewhere. */
17304 || (external_p && TREE_CODE (d) == VAR_DECL))
17306 /* The definition of the static data member is now required so
17307 we must substitute the initializer. */
17308 if (TREE_CODE (d) == VAR_DECL
17309 && !DECL_INITIAL (d)
17310 && DECL_INITIAL (code_pattern))
17312 tree ns;
17313 tree init;
17314 bool const_init = false;
17316 ns = decl_namespace_context (d);
17317 push_nested_namespace (ns);
17318 push_nested_class (DECL_CONTEXT (d));
17319 init = tsubst_expr (DECL_INITIAL (code_pattern),
17320 args,
17321 tf_warning_or_error, NULL_TREE,
17322 /*integral_constant_expression_p=*/false);
17323 /* Make sure the initializer is still constant, in case of
17324 circular dependency (template/instantiate6.C). */
17325 const_init
17326 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
17327 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
17328 /*asmspec_tree=*/NULL_TREE,
17329 LOOKUP_ONLYCONVERTING);
17330 pop_nested_class ();
17331 pop_nested_namespace (ns);
17334 /* We restore the source position here because it's used by
17335 add_pending_template. */
17336 input_location = saved_loc;
17338 if (at_eof && !pattern_defined
17339 && DECL_EXPLICIT_INSTANTIATION (d)
17340 && DECL_NOT_REALLY_EXTERN (d))
17341 /* [temp.explicit]
17343 The definition of a non-exported function template, a
17344 non-exported member function template, or a non-exported
17345 member function or static data member of a class template
17346 shall be present in every translation unit in which it is
17347 explicitly instantiated. */
17348 permerror (input_location, "explicit instantiation of %qD "
17349 "but no definition available", d);
17351 /* If we're in unevaluated context, we just wanted to get the
17352 constant value; this isn't an odr use, so don't queue
17353 a full instantiation. */
17354 if (cp_unevaluated_operand != 0)
17355 goto out;
17356 /* ??? Historically, we have instantiated inline functions, even
17357 when marked as "extern template". */
17358 if (!(external_p && TREE_CODE (d) == VAR_DECL))
17359 add_pending_template (d);
17360 goto out;
17362 /* Tell the repository that D is available in this translation unit
17363 -- and see if it is supposed to be instantiated here. */
17364 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
17366 /* In a PCH file, despite the fact that the repository hasn't
17367 requested instantiation in the PCH it is still possible that
17368 an instantiation will be required in a file that includes the
17369 PCH. */
17370 if (pch_file)
17371 add_pending_template (d);
17372 /* Instantiate inline functions so that the inliner can do its
17373 job, even though we'll not be emitting a copy of this
17374 function. */
17375 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
17376 goto out;
17379 need_push = !cfun || !global_bindings_p ();
17380 if (need_push)
17381 push_to_top_level ();
17383 /* Mark D as instantiated so that recursive calls to
17384 instantiate_decl do not try to instantiate it again. */
17385 DECL_TEMPLATE_INSTANTIATED (d) = 1;
17387 /* Regenerate the declaration in case the template has been modified
17388 by a subsequent redeclaration. */
17389 regenerate_decl_from_template (d, td);
17391 /* We already set the file and line above. Reset them now in case
17392 they changed as a result of calling regenerate_decl_from_template. */
17393 input_location = DECL_SOURCE_LOCATION (d);
17395 if (TREE_CODE (d) == VAR_DECL)
17397 tree init;
17398 bool const_init = false;
17400 /* Clear out DECL_RTL; whatever was there before may not be right
17401 since we've reset the type of the declaration. */
17402 SET_DECL_RTL (d, NULL);
17403 DECL_IN_AGGR_P (d) = 0;
17405 /* The initializer is placed in DECL_INITIAL by
17406 regenerate_decl_from_template so we don't need to
17407 push/pop_access_scope again here. Pull it out so that
17408 cp_finish_decl can process it. */
17409 init = DECL_INITIAL (d);
17410 DECL_INITIAL (d) = NULL_TREE;
17411 DECL_INITIALIZED_P (d) = 0;
17413 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
17414 initializer. That function will defer actual emission until
17415 we have a chance to determine linkage. */
17416 DECL_EXTERNAL (d) = 0;
17418 /* Enter the scope of D so that access-checking works correctly. */
17419 push_nested_class (DECL_CONTEXT (d));
17420 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
17421 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
17422 pop_nested_class ();
17424 else if (TREE_CODE (d) == FUNCTION_DECL)
17426 htab_t saved_local_specializations;
17427 tree subst_decl;
17428 tree tmpl_parm;
17429 tree spec_parm;
17431 /* Save away the current list, in case we are instantiating one
17432 template from within the body of another. */
17433 saved_local_specializations = local_specializations;
17435 /* Set up the list of local specializations. */
17436 local_specializations = htab_create (37,
17437 hash_local_specialization,
17438 eq_local_specializations,
17439 NULL);
17441 /* Set up context. */
17442 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
17444 /* Create substitution entries for the parameters. */
17445 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
17446 tmpl_parm = DECL_ARGUMENTS (subst_decl);
17447 spec_parm = DECL_ARGUMENTS (d);
17448 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
17450 register_local_specialization (spec_parm, tmpl_parm);
17451 spec_parm = skip_artificial_parms_for (d, spec_parm);
17452 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
17454 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17456 register_local_specialization (spec_parm, tmpl_parm);
17457 tmpl_parm = DECL_CHAIN (tmpl_parm);
17458 spec_parm = DECL_CHAIN (spec_parm);
17460 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17462 /* Register the (value) argument pack as a specialization of
17463 TMPL_PARM, then move on. */
17464 tree argpack = make_fnparm_pack (spec_parm);
17465 register_local_specialization (argpack, tmpl_parm);
17466 tmpl_parm = DECL_CHAIN (tmpl_parm);
17467 spec_parm = NULL_TREE;
17469 gcc_assert (!spec_parm);
17471 /* Substitute into the body of the function. */
17472 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
17473 tf_warning_or_error, tmpl,
17474 /*integral_constant_expression_p=*/false);
17476 /* Set the current input_location to the end of the function
17477 so that finish_function knows where we are. */
17478 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
17480 /* We don't need the local specializations any more. */
17481 htab_delete (local_specializations);
17482 local_specializations = saved_local_specializations;
17484 /* Finish the function. */
17485 d = finish_function (0);
17486 expand_or_defer_fn (d);
17489 /* We're not deferring instantiation any more. */
17490 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
17492 if (need_push)
17493 pop_from_top_level ();
17495 out:
17496 input_location = saved_loc;
17497 pop_deferring_access_checks ();
17498 pop_tinst_level ();
17500 timevar_pop (TV_PARSE);
17502 return d;
17505 /* Run through the list of templates that we wish we could
17506 instantiate, and instantiate any we can. RETRIES is the
17507 number of times we retry pending template instantiation. */
17509 void
17510 instantiate_pending_templates (int retries)
17512 int reconsider;
17513 location_t saved_loc = input_location;
17515 /* Instantiating templates may trigger vtable generation. This in turn
17516 may require further template instantiations. We place a limit here
17517 to avoid infinite loop. */
17518 if (pending_templates && retries >= max_tinst_depth)
17520 tree decl = pending_templates->tinst->decl;
17522 error ("template instantiation depth exceeds maximum of %d"
17523 " instantiating %q+D, possibly from virtual table generation"
17524 " (use -ftemplate-depth= to increase the maximum)",
17525 max_tinst_depth, decl);
17526 if (TREE_CODE (decl) == FUNCTION_DECL)
17527 /* Pretend that we defined it. */
17528 DECL_INITIAL (decl) = error_mark_node;
17529 return;
17534 struct pending_template **t = &pending_templates;
17535 struct pending_template *last = NULL;
17536 reconsider = 0;
17537 while (*t)
17539 tree instantiation = reopen_tinst_level ((*t)->tinst);
17540 bool complete = false;
17542 if (TYPE_P (instantiation))
17544 tree fn;
17546 if (!COMPLETE_TYPE_P (instantiation))
17548 instantiate_class_template (instantiation);
17549 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
17550 for (fn = TYPE_METHODS (instantiation);
17552 fn = TREE_CHAIN (fn))
17553 if (! DECL_ARTIFICIAL (fn))
17554 instantiate_decl (fn,
17555 /*defer_ok=*/0,
17556 /*expl_inst_class_mem_p=*/false);
17557 if (COMPLETE_TYPE_P (instantiation))
17558 reconsider = 1;
17561 complete = COMPLETE_TYPE_P (instantiation);
17563 else
17565 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
17566 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
17568 instantiation
17569 = instantiate_decl (instantiation,
17570 /*defer_ok=*/0,
17571 /*expl_inst_class_mem_p=*/false);
17572 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
17573 reconsider = 1;
17576 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
17577 || DECL_TEMPLATE_INSTANTIATED (instantiation));
17580 if (complete)
17581 /* If INSTANTIATION has been instantiated, then we don't
17582 need to consider it again in the future. */
17583 *t = (*t)->next;
17584 else
17586 last = *t;
17587 t = &(*t)->next;
17589 tinst_depth = 0;
17590 current_tinst_level = NULL;
17592 last_pending_template = last;
17594 while (reconsider);
17596 input_location = saved_loc;
17599 /* Substitute ARGVEC into T, which is a list of initializers for
17600 either base class or a non-static data member. The TREE_PURPOSEs
17601 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17602 instantiate_decl. */
17604 static tree
17605 tsubst_initializer_list (tree t, tree argvec)
17607 tree inits = NULL_TREE;
17609 for (; t; t = TREE_CHAIN (t))
17611 tree decl;
17612 tree init;
17613 tree expanded_bases = NULL_TREE;
17614 tree expanded_arguments = NULL_TREE;
17615 int i, len = 1;
17617 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17619 tree expr;
17620 tree arg;
17622 /* Expand the base class expansion type into separate base
17623 classes. */
17624 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17625 tf_warning_or_error,
17626 NULL_TREE);
17627 if (expanded_bases == error_mark_node)
17628 continue;
17630 /* We'll be building separate TREE_LISTs of arguments for
17631 each base. */
17632 len = TREE_VEC_LENGTH (expanded_bases);
17633 expanded_arguments = make_tree_vec (len);
17634 for (i = 0; i < len; i++)
17635 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17637 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17638 expand each argument in the TREE_VALUE of t. */
17639 expr = make_node (EXPR_PACK_EXPANSION);
17640 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17641 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17643 if (TREE_VALUE (t) == void_type_node)
17644 /* VOID_TYPE_NODE is used to indicate
17645 value-initialization. */
17647 for (i = 0; i < len; i++)
17648 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17650 else
17652 /* Substitute parameter packs into each argument in the
17653 TREE_LIST. */
17654 in_base_initializer = 1;
17655 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17657 tree expanded_exprs;
17659 /* Expand the argument. */
17660 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17661 expanded_exprs
17662 = tsubst_pack_expansion (expr, argvec,
17663 tf_warning_or_error,
17664 NULL_TREE);
17665 if (expanded_exprs == error_mark_node)
17666 continue;
17668 /* Prepend each of the expanded expressions to the
17669 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17670 for (i = 0; i < len; i++)
17672 TREE_VEC_ELT (expanded_arguments, i) =
17673 tree_cons (NULL_TREE,
17674 TREE_VEC_ELT (expanded_exprs, i),
17675 TREE_VEC_ELT (expanded_arguments, i));
17678 in_base_initializer = 0;
17680 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17681 since we built them backwards. */
17682 for (i = 0; i < len; i++)
17684 TREE_VEC_ELT (expanded_arguments, i) =
17685 nreverse (TREE_VEC_ELT (expanded_arguments, i));
17690 for (i = 0; i < len; ++i)
17692 if (expanded_bases)
17694 decl = TREE_VEC_ELT (expanded_bases, i);
17695 decl = expand_member_init (decl);
17696 init = TREE_VEC_ELT (expanded_arguments, i);
17698 else
17700 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
17701 tf_warning_or_error, NULL_TREE);
17703 decl = expand_member_init (decl);
17704 if (decl && !DECL_P (decl))
17705 in_base_initializer = 1;
17707 init = TREE_VALUE (t);
17708 if (init != void_type_node)
17709 init = tsubst_expr (init, argvec,
17710 tf_warning_or_error, NULL_TREE,
17711 /*integral_constant_expression_p=*/false);
17712 in_base_initializer = 0;
17715 if (decl)
17717 init = build_tree_list (decl, init);
17718 TREE_CHAIN (init) = inits;
17719 inits = init;
17723 return inits;
17726 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17728 static void
17729 set_current_access_from_decl (tree decl)
17731 if (TREE_PRIVATE (decl))
17732 current_access_specifier = access_private_node;
17733 else if (TREE_PROTECTED (decl))
17734 current_access_specifier = access_protected_node;
17735 else
17736 current_access_specifier = access_public_node;
17739 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17740 is the instantiation (which should have been created with
17741 start_enum) and ARGS are the template arguments to use. */
17743 static void
17744 tsubst_enum (tree tag, tree newtag, tree args)
17746 tree e;
17748 if (SCOPED_ENUM_P (newtag))
17749 begin_scope (sk_scoped_enum, newtag);
17751 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
17753 tree value;
17754 tree decl;
17756 decl = TREE_VALUE (e);
17757 /* Note that in a template enum, the TREE_VALUE is the
17758 CONST_DECL, not the corresponding INTEGER_CST. */
17759 value = tsubst_expr (DECL_INITIAL (decl),
17760 args, tf_warning_or_error, NULL_TREE,
17761 /*integral_constant_expression_p=*/true);
17763 /* Give this enumeration constant the correct access. */
17764 set_current_access_from_decl (decl);
17766 /* Actually build the enumerator itself. */
17767 build_enumerator
17768 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
17771 if (SCOPED_ENUM_P (newtag))
17772 finish_scope ();
17774 finish_enum_value_list (newtag);
17775 finish_enum (newtag);
17777 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
17778 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
17781 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17782 its type -- but without substituting the innermost set of template
17783 arguments. So, innermost set of template parameters will appear in
17784 the type. */
17786 tree
17787 get_mostly_instantiated_function_type (tree decl)
17789 tree fn_type;
17790 tree tmpl;
17791 tree targs;
17792 tree tparms;
17793 int parm_depth;
17795 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
17796 targs = DECL_TI_ARGS (decl);
17797 tparms = DECL_TEMPLATE_PARMS (tmpl);
17798 parm_depth = TMPL_PARMS_DEPTH (tparms);
17800 /* There should be as many levels of arguments as there are levels
17801 of parameters. */
17802 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
17804 fn_type = TREE_TYPE (tmpl);
17806 if (parm_depth == 1)
17807 /* No substitution is necessary. */
17809 else
17811 int i, save_access_control;
17812 tree partial_args;
17814 /* Replace the innermost level of the TARGS with NULL_TREEs to
17815 let tsubst know not to substitute for those parameters. */
17816 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
17817 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
17818 SET_TMPL_ARGS_LEVEL (partial_args, i,
17819 TMPL_ARGS_LEVEL (targs, i));
17820 SET_TMPL_ARGS_LEVEL (partial_args,
17821 TMPL_ARGS_DEPTH (targs),
17822 make_tree_vec (DECL_NTPARMS (tmpl)));
17824 /* Disable access control as this function is used only during
17825 name-mangling. */
17826 save_access_control = flag_access_control;
17827 flag_access_control = 0;
17829 ++processing_template_decl;
17830 /* Now, do the (partial) substitution to figure out the
17831 appropriate function type. */
17832 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17833 --processing_template_decl;
17835 /* Substitute into the template parameters to obtain the real
17836 innermost set of parameters. This step is important if the
17837 innermost set of template parameters contains value
17838 parameters whose types depend on outer template parameters. */
17839 TREE_VEC_LENGTH (partial_args)--;
17840 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17842 flag_access_control = save_access_control;
17845 return fn_type;
17848 /* Return truthvalue if we're processing a template different from
17849 the last one involved in diagnostics. */
17851 problematic_instantiation_changed (void)
17853 return last_template_error_tick != tinst_level_tick;
17856 /* Remember current template involved in diagnostics. */
17857 void
17858 record_last_problematic_instantiation (void)
17860 last_template_error_tick = tinst_level_tick;
17863 struct tinst_level *
17864 current_instantiation (void)
17866 return current_tinst_level;
17869 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17870 type. Return zero for ok, nonzero for disallowed. Issue error and
17871 warning messages under control of COMPLAIN. */
17873 static int
17874 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17876 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17877 return 0;
17878 else if (POINTER_TYPE_P (type))
17879 return 0;
17880 else if (TYPE_PTR_TO_MEMBER_P (type))
17881 return 0;
17882 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17883 return 0;
17884 else if (TREE_CODE (type) == TYPENAME_TYPE)
17885 return 0;
17887 if (complain & tf_error)
17888 error ("%q#T is not a valid type for a template constant parameter", type);
17889 return 1;
17892 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17893 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17895 static bool
17896 dependent_type_p_r (tree type)
17898 tree scope;
17900 /* [temp.dep.type]
17902 A type is dependent if it is:
17904 -- a template parameter. Template template parameters are types
17905 for us (since TYPE_P holds true for them) so we handle
17906 them here. */
17907 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17908 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17909 return true;
17910 /* -- a qualified-id with a nested-name-specifier which contains a
17911 class-name that names a dependent type or whose unqualified-id
17912 names a dependent type. */
17913 if (TREE_CODE (type) == TYPENAME_TYPE)
17914 return true;
17915 /* -- a cv-qualified type where the cv-unqualified type is
17916 dependent. */
17917 type = TYPE_MAIN_VARIANT (type);
17918 /* -- a compound type constructed from any dependent type. */
17919 if (TYPE_PTR_TO_MEMBER_P (type))
17920 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17921 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17922 (type)));
17923 else if (TREE_CODE (type) == POINTER_TYPE
17924 || TREE_CODE (type) == REFERENCE_TYPE)
17925 return dependent_type_p (TREE_TYPE (type));
17926 else if (TREE_CODE (type) == FUNCTION_TYPE
17927 || TREE_CODE (type) == METHOD_TYPE)
17929 tree arg_type;
17931 if (dependent_type_p (TREE_TYPE (type)))
17932 return true;
17933 for (arg_type = TYPE_ARG_TYPES (type);
17934 arg_type;
17935 arg_type = TREE_CHAIN (arg_type))
17936 if (dependent_type_p (TREE_VALUE (arg_type)))
17937 return true;
17938 return false;
17940 /* -- an array type constructed from any dependent type or whose
17941 size is specified by a constant expression that is
17942 value-dependent.
17944 We checked for type- and value-dependence of the bounds in
17945 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
17946 if (TREE_CODE (type) == ARRAY_TYPE)
17948 if (TYPE_DOMAIN (type)
17949 && dependent_type_p (TYPE_DOMAIN (type)))
17950 return true;
17951 return dependent_type_p (TREE_TYPE (type));
17954 /* -- a template-id in which either the template name is a template
17955 parameter ... */
17956 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17957 return true;
17958 /* ... or any of the template arguments is a dependent type or
17959 an expression that is type-dependent or value-dependent. */
17960 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17961 && (any_dependent_template_arguments_p
17962 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17963 return true;
17965 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
17966 dependent; if the argument of the `typeof' expression is not
17967 type-dependent, then it should already been have resolved. */
17968 if (TREE_CODE (type) == TYPEOF_TYPE
17969 || TREE_CODE (type) == DECLTYPE_TYPE
17970 || TREE_CODE (type) == UNDERLYING_TYPE)
17971 return true;
17973 /* A template argument pack is dependent if any of its packed
17974 arguments are. */
17975 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17977 tree args = ARGUMENT_PACK_ARGS (type);
17978 int i, len = TREE_VEC_LENGTH (args);
17979 for (i = 0; i < len; ++i)
17980 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17981 return true;
17984 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17985 be template parameters. */
17986 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17987 return true;
17989 /* The standard does not specifically mention types that are local
17990 to template functions or local classes, but they should be
17991 considered dependent too. For example:
17993 template <int I> void f() {
17994 enum E { a = I };
17995 S<sizeof (E)> s;
17998 The size of `E' cannot be known until the value of `I' has been
17999 determined. Therefore, `E' must be considered dependent. */
18000 scope = TYPE_CONTEXT (type);
18001 if (scope && TYPE_P (scope))
18002 return dependent_type_p (scope);
18003 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
18004 return type_dependent_expression_p (scope);
18006 /* Other types are non-dependent. */
18007 return false;
18010 /* Returns TRUE if TYPE is dependent, in the sense of
18011 [temp.dep.type]. Note that a NULL type is considered dependent. */
18013 bool
18014 dependent_type_p (tree type)
18016 /* If there are no template parameters in scope, then there can't be
18017 any dependent types. */
18018 if (!processing_template_decl)
18020 /* If we are not processing a template, then nobody should be
18021 providing us with a dependent type. */
18022 gcc_assert (type);
18023 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
18024 return false;
18027 /* If the type is NULL, we have not computed a type for the entity
18028 in question; in that case, the type is dependent. */
18029 if (!type)
18030 return true;
18032 /* Erroneous types can be considered non-dependent. */
18033 if (type == error_mark_node)
18034 return false;
18036 /* If we have not already computed the appropriate value for TYPE,
18037 do so now. */
18038 if (!TYPE_DEPENDENT_P_VALID (type))
18040 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
18041 TYPE_DEPENDENT_P_VALID (type) = 1;
18044 return TYPE_DEPENDENT_P (type);
18047 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
18048 lookup. In other words, a dependent type that is not the current
18049 instantiation. */
18051 bool
18052 dependent_scope_p (tree scope)
18054 return (scope && TYPE_P (scope) && dependent_type_p (scope)
18055 && !currently_open_class (scope));
18058 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
18059 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
18060 expression. */
18062 /* Note that this predicate is not appropriate for general expressions;
18063 only constant expressions (that satisfy potential_constant_expression)
18064 can be tested for value dependence.
18066 We should really also have a predicate for "instantiation-dependent".
18068 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
18069 (what about instantiation-dependent constant-expressions?)
18070 is_late_template_attribute: defer if instantiation-dependent.
18071 compute_array_index_type: proceed if constant and not t- or v-dependent
18072 if instantiation-dependent, need to remember full expression
18073 uses_template_parms: FIXME - need to audit callers
18074 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
18075 dependent_type_p [array_type]: dependent if index type is dependent
18076 (or non-constant?)
18077 static_assert - instantiation-dependent */
18079 bool
18080 value_dependent_expression_p (tree expression)
18082 if (!processing_template_decl)
18083 return false;
18085 /* A name declared with a dependent type. */
18086 if (DECL_P (expression) && type_dependent_expression_p (expression))
18087 return true;
18089 switch (TREE_CODE (expression))
18091 case IDENTIFIER_NODE:
18092 /* A name that has not been looked up -- must be dependent. */
18093 return true;
18095 case TEMPLATE_PARM_INDEX:
18096 /* A non-type template parm. */
18097 return true;
18099 case CONST_DECL:
18100 /* A non-type template parm. */
18101 if (DECL_TEMPLATE_PARM_P (expression))
18102 return true;
18103 return value_dependent_expression_p (DECL_INITIAL (expression));
18105 case VAR_DECL:
18106 /* A constant with literal type and is initialized
18107 with an expression that is value-dependent. */
18108 if (DECL_INITIAL (expression)
18109 && decl_constant_var_p (expression)
18110 && value_dependent_expression_p (DECL_INITIAL (expression)))
18111 return true;
18112 return false;
18114 case DYNAMIC_CAST_EXPR:
18115 case STATIC_CAST_EXPR:
18116 case CONST_CAST_EXPR:
18117 case REINTERPRET_CAST_EXPR:
18118 case CAST_EXPR:
18119 /* These expressions are value-dependent if the type to which
18120 the cast occurs is dependent or the expression being casted
18121 is value-dependent. */
18123 tree type = TREE_TYPE (expression);
18125 if (dependent_type_p (type))
18126 return true;
18128 /* A functional cast has a list of operands. */
18129 expression = TREE_OPERAND (expression, 0);
18130 if (!expression)
18132 /* If there are no operands, it must be an expression such
18133 as "int()". This should not happen for aggregate types
18134 because it would form non-constant expressions. */
18135 gcc_assert (cxx_dialect >= cxx0x
18136 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
18138 return false;
18141 if (TREE_CODE (expression) == TREE_LIST)
18142 return any_value_dependent_elements_p (expression);
18144 return value_dependent_expression_p (expression);
18147 case SIZEOF_EXPR:
18148 case ALIGNOF_EXPR:
18149 case TYPEID_EXPR:
18150 /* A `sizeof' expression is value-dependent if the operand is
18151 type-dependent or is a pack expansion. */
18152 expression = TREE_OPERAND (expression, 0);
18153 if (PACK_EXPANSION_P (expression))
18154 return true;
18155 else if (TYPE_P (expression))
18156 return dependent_type_p (expression);
18157 return type_dependent_expression_p (expression);
18159 case AT_ENCODE_EXPR:
18160 /* An 'encode' expression is value-dependent if the operand is
18161 type-dependent. */
18162 expression = TREE_OPERAND (expression, 0);
18163 return dependent_type_p (expression);
18165 case NOEXCEPT_EXPR:
18166 expression = TREE_OPERAND (expression, 0);
18167 return type_dependent_expression_p (expression);
18169 case SCOPE_REF:
18171 tree name = TREE_OPERAND (expression, 1);
18172 return value_dependent_expression_p (name);
18175 case COMPONENT_REF:
18176 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
18177 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
18179 case NONTYPE_ARGUMENT_PACK:
18180 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
18181 is value-dependent. */
18183 tree values = ARGUMENT_PACK_ARGS (expression);
18184 int i, len = TREE_VEC_LENGTH (values);
18186 for (i = 0; i < len; ++i)
18187 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
18188 return true;
18190 return false;
18193 case TRAIT_EXPR:
18195 tree type2 = TRAIT_EXPR_TYPE2 (expression);
18196 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
18197 || (type2 ? dependent_type_p (type2) : false));
18200 case MODOP_EXPR:
18201 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18202 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
18204 case ARRAY_REF:
18205 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18206 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
18208 case ADDR_EXPR:
18210 tree op = TREE_OPERAND (expression, 0);
18211 return (value_dependent_expression_p (op)
18212 || has_value_dependent_address (op));
18215 case CALL_EXPR:
18217 tree fn = get_callee_fndecl (expression);
18218 int i, nargs;
18219 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
18220 return true;
18221 nargs = call_expr_nargs (expression);
18222 for (i = 0; i < nargs; ++i)
18224 tree op = CALL_EXPR_ARG (expression, i);
18225 /* In a call to a constexpr member function, look through the
18226 implicit ADDR_EXPR on the object argument so that it doesn't
18227 cause the call to be considered value-dependent. We also
18228 look through it in potential_constant_expression. */
18229 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
18230 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
18231 && TREE_CODE (op) == ADDR_EXPR)
18232 op = TREE_OPERAND (op, 0);
18233 if (value_dependent_expression_p (op))
18234 return true;
18236 return false;
18239 case TEMPLATE_ID_EXPR:
18240 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
18241 type-dependent. */
18242 return type_dependent_expression_p (expression);
18244 default:
18245 /* A constant expression is value-dependent if any subexpression is
18246 value-dependent. */
18247 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
18249 case tcc_reference:
18250 case tcc_unary:
18251 return (value_dependent_expression_p
18252 (TREE_OPERAND (expression, 0)));
18254 case tcc_comparison:
18255 case tcc_binary:
18256 return ((value_dependent_expression_p
18257 (TREE_OPERAND (expression, 0)))
18258 || (value_dependent_expression_p
18259 (TREE_OPERAND (expression, 1))));
18261 case tcc_expression:
18262 case tcc_vl_exp:
18264 int i;
18265 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
18266 /* In some cases, some of the operands may be missing.
18267 (For example, in the case of PREDECREMENT_EXPR, the
18268 amount to increment by may be missing.) That doesn't
18269 make the expression dependent. */
18270 if (TREE_OPERAND (expression, i)
18271 && (value_dependent_expression_p
18272 (TREE_OPERAND (expression, i))))
18273 return true;
18274 return false;
18277 default:
18278 break;
18282 /* The expression is not value-dependent. */
18283 return false;
18286 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
18287 [temp.dep.expr]. Note that an expression with no type is
18288 considered dependent. Other parts of the compiler arrange for an
18289 expression with type-dependent subexpressions to have no type, so
18290 this function doesn't have to be fully recursive. */
18292 bool
18293 type_dependent_expression_p (tree expression)
18295 if (!processing_template_decl)
18296 return false;
18298 if (expression == error_mark_node)
18299 return false;
18301 /* An unresolved name is always dependent. */
18302 if (TREE_CODE (expression) == IDENTIFIER_NODE
18303 || TREE_CODE (expression) == USING_DECL)
18304 return true;
18306 /* Some expression forms are never type-dependent. */
18307 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
18308 || TREE_CODE (expression) == SIZEOF_EXPR
18309 || TREE_CODE (expression) == ALIGNOF_EXPR
18310 || TREE_CODE (expression) == AT_ENCODE_EXPR
18311 || TREE_CODE (expression) == NOEXCEPT_EXPR
18312 || TREE_CODE (expression) == TRAIT_EXPR
18313 || TREE_CODE (expression) == TYPEID_EXPR
18314 || TREE_CODE (expression) == DELETE_EXPR
18315 || TREE_CODE (expression) == VEC_DELETE_EXPR
18316 || TREE_CODE (expression) == THROW_EXPR)
18317 return false;
18319 /* The types of these expressions depends only on the type to which
18320 the cast occurs. */
18321 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
18322 || TREE_CODE (expression) == STATIC_CAST_EXPR
18323 || TREE_CODE (expression) == CONST_CAST_EXPR
18324 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
18325 || TREE_CODE (expression) == CAST_EXPR)
18326 return dependent_type_p (TREE_TYPE (expression));
18328 /* The types of these expressions depends only on the type created
18329 by the expression. */
18330 if (TREE_CODE (expression) == NEW_EXPR
18331 || TREE_CODE (expression) == VEC_NEW_EXPR)
18333 /* For NEW_EXPR tree nodes created inside a template, either
18334 the object type itself or a TREE_LIST may appear as the
18335 operand 1. */
18336 tree type = TREE_OPERAND (expression, 1);
18337 if (TREE_CODE (type) == TREE_LIST)
18338 /* This is an array type. We need to check array dimensions
18339 as well. */
18340 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
18341 || value_dependent_expression_p
18342 (TREE_OPERAND (TREE_VALUE (type), 1));
18343 else
18344 return dependent_type_p (type);
18347 if (TREE_CODE (expression) == SCOPE_REF)
18349 tree scope = TREE_OPERAND (expression, 0);
18350 tree name = TREE_OPERAND (expression, 1);
18352 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
18353 contains an identifier associated by name lookup with one or more
18354 declarations declared with a dependent type, or...a
18355 nested-name-specifier or qualified-id that names a member of an
18356 unknown specialization. */
18357 return (type_dependent_expression_p (name)
18358 || dependent_scope_p (scope));
18361 if (TREE_CODE (expression) == FUNCTION_DECL
18362 && DECL_LANG_SPECIFIC (expression)
18363 && DECL_TEMPLATE_INFO (expression)
18364 && (any_dependent_template_arguments_p
18365 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
18366 return true;
18368 if (TREE_CODE (expression) == TEMPLATE_DECL
18369 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
18370 return false;
18372 if (TREE_CODE (expression) == STMT_EXPR)
18373 expression = stmt_expr_value_expr (expression);
18375 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
18377 tree elt;
18378 unsigned i;
18380 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
18382 if (type_dependent_expression_p (elt))
18383 return true;
18385 return false;
18388 /* A static data member of the current instantiation with incomplete
18389 array type is type-dependent, as the definition and specializations
18390 can have different bounds. */
18391 if (TREE_CODE (expression) == VAR_DECL
18392 && DECL_CLASS_SCOPE_P (expression)
18393 && dependent_type_p (DECL_CONTEXT (expression))
18394 && VAR_HAD_UNKNOWN_BOUND (expression))
18395 return true;
18397 if (TREE_TYPE (expression) == unknown_type_node)
18399 if (TREE_CODE (expression) == ADDR_EXPR)
18400 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
18401 if (TREE_CODE (expression) == COMPONENT_REF
18402 || TREE_CODE (expression) == OFFSET_REF)
18404 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
18405 return true;
18406 expression = TREE_OPERAND (expression, 1);
18407 if (TREE_CODE (expression) == IDENTIFIER_NODE)
18408 return false;
18410 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
18411 if (TREE_CODE (expression) == SCOPE_REF)
18412 return false;
18414 if (TREE_CODE (expression) == BASELINK)
18415 expression = BASELINK_FUNCTIONS (expression);
18417 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
18419 if (any_dependent_template_arguments_p
18420 (TREE_OPERAND (expression, 1)))
18421 return true;
18422 expression = TREE_OPERAND (expression, 0);
18424 gcc_assert (TREE_CODE (expression) == OVERLOAD
18425 || TREE_CODE (expression) == FUNCTION_DECL);
18427 while (expression)
18429 if (type_dependent_expression_p (OVL_CURRENT (expression)))
18430 return true;
18431 expression = OVL_NEXT (expression);
18433 return false;
18436 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
18438 return (dependent_type_p (TREE_TYPE (expression)));
18441 /* Like type_dependent_expression_p, but it also works while not processing
18442 a template definition, i.e. during substitution or mangling. */
18444 bool
18445 type_dependent_expression_p_push (tree expr)
18447 bool b;
18448 ++processing_template_decl;
18449 b = type_dependent_expression_p (expr);
18450 --processing_template_decl;
18451 return b;
18454 /* Returns TRUE if ARGS contains a type-dependent expression. */
18456 bool
18457 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
18459 unsigned int i;
18460 tree arg;
18462 FOR_EACH_VEC_ELT (tree, args, i, arg)
18464 if (type_dependent_expression_p (arg))
18465 return true;
18467 return false;
18470 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18471 expressions) contains any type-dependent expressions. */
18473 bool
18474 any_type_dependent_elements_p (const_tree list)
18476 for (; list; list = TREE_CHAIN (list))
18477 if (value_dependent_expression_p (TREE_VALUE (list)))
18478 return true;
18480 return false;
18483 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18484 expressions) contains any value-dependent expressions. */
18486 bool
18487 any_value_dependent_elements_p (const_tree list)
18489 for (; list; list = TREE_CHAIN (list))
18490 if (value_dependent_expression_p (TREE_VALUE (list)))
18491 return true;
18493 return false;
18496 /* Returns TRUE if the ARG (a template argument) is dependent. */
18498 bool
18499 dependent_template_arg_p (tree arg)
18501 if (!processing_template_decl)
18502 return false;
18504 /* Assume a template argument that was wrongly written by the user
18505 is dependent. This is consistent with what
18506 any_dependent_template_arguments_p [that calls this function]
18507 does. */
18508 if (arg == error_mark_node)
18509 return true;
18511 if (TREE_CODE (arg) == TEMPLATE_DECL
18512 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18513 return dependent_template_p (arg);
18514 else if (ARGUMENT_PACK_P (arg))
18516 tree args = ARGUMENT_PACK_ARGS (arg);
18517 int i, len = TREE_VEC_LENGTH (args);
18518 for (i = 0; i < len; ++i)
18520 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18521 return true;
18524 return false;
18526 else if (TYPE_P (arg))
18527 return dependent_type_p (arg);
18528 else
18529 return (type_dependent_expression_p (arg)
18530 || value_dependent_expression_p (arg));
18533 /* Returns true if ARGS (a collection of template arguments) contains
18534 any types that require structural equality testing. */
18536 bool
18537 any_template_arguments_need_structural_equality_p (tree args)
18539 int i;
18540 int j;
18542 if (!args)
18543 return false;
18544 if (args == error_mark_node)
18545 return true;
18547 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18549 tree level = TMPL_ARGS_LEVEL (args, i + 1);
18550 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18552 tree arg = TREE_VEC_ELT (level, j);
18553 tree packed_args = NULL_TREE;
18554 int k, len = 1;
18556 if (ARGUMENT_PACK_P (arg))
18558 /* Look inside the argument pack. */
18559 packed_args = ARGUMENT_PACK_ARGS (arg);
18560 len = TREE_VEC_LENGTH (packed_args);
18563 for (k = 0; k < len; ++k)
18565 if (packed_args)
18566 arg = TREE_VEC_ELT (packed_args, k);
18568 if (error_operand_p (arg))
18569 return true;
18570 else if (TREE_CODE (arg) == TEMPLATE_DECL
18571 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18572 continue;
18573 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
18574 return true;
18575 else if (!TYPE_P (arg) && TREE_TYPE (arg)
18576 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
18577 return true;
18582 return false;
18585 /* Returns true if ARGS (a collection of template arguments) contains
18586 any dependent arguments. */
18588 bool
18589 any_dependent_template_arguments_p (const_tree args)
18591 int i;
18592 int j;
18594 if (!args)
18595 return false;
18596 if (args == error_mark_node)
18597 return true;
18599 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18601 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
18602 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18603 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
18604 return true;
18607 return false;
18610 /* Returns TRUE if the template TMPL is dependent. */
18612 bool
18613 dependent_template_p (tree tmpl)
18615 if (TREE_CODE (tmpl) == OVERLOAD)
18617 while (tmpl)
18619 if (dependent_template_p (OVL_CURRENT (tmpl)))
18620 return true;
18621 tmpl = OVL_NEXT (tmpl);
18623 return false;
18626 /* Template template parameters are dependent. */
18627 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
18628 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
18629 return true;
18630 /* So are names that have not been looked up. */
18631 if (TREE_CODE (tmpl) == SCOPE_REF
18632 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
18633 return true;
18634 /* So are member templates of dependent classes. */
18635 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
18636 return dependent_type_p (DECL_CONTEXT (tmpl));
18637 return false;
18640 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18642 bool
18643 dependent_template_id_p (tree tmpl, tree args)
18645 return (dependent_template_p (tmpl)
18646 || any_dependent_template_arguments_p (args));
18649 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18650 is dependent. */
18652 bool
18653 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
18655 int i;
18657 if (!processing_template_decl)
18658 return false;
18660 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
18662 tree decl = TREE_VEC_ELT (declv, i);
18663 tree init = TREE_VEC_ELT (initv, i);
18664 tree cond = TREE_VEC_ELT (condv, i);
18665 tree incr = TREE_VEC_ELT (incrv, i);
18667 if (type_dependent_expression_p (decl))
18668 return true;
18670 if (init && type_dependent_expression_p (init))
18671 return true;
18673 if (type_dependent_expression_p (cond))
18674 return true;
18676 if (COMPARISON_CLASS_P (cond)
18677 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
18678 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
18679 return true;
18681 if (TREE_CODE (incr) == MODOP_EXPR)
18683 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
18684 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
18685 return true;
18687 else if (type_dependent_expression_p (incr))
18688 return true;
18689 else if (TREE_CODE (incr) == MODIFY_EXPR)
18691 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
18692 return true;
18693 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
18695 tree t = TREE_OPERAND (incr, 1);
18696 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
18697 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
18698 return true;
18703 return false;
18706 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18707 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18708 no such TYPE can be found. Note that this function peers inside
18709 uninstantiated templates and therefore should be used only in
18710 extremely limited situations. ONLY_CURRENT_P restricts this
18711 peering to the currently open classes hierarchy (which is required
18712 when comparing types). */
18714 tree
18715 resolve_typename_type (tree type, bool only_current_p)
18717 tree scope;
18718 tree name;
18719 tree decl;
18720 int quals;
18721 tree pushed_scope;
18722 tree result;
18724 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
18726 scope = TYPE_CONTEXT (type);
18727 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18728 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18729 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18730 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18731 identifier of the TYPENAME_TYPE anymore.
18732 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18733 TYPENAME_TYPE instead, we avoid messing up with a possible
18734 typedef variant case. */
18735 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
18737 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18738 it first before we can figure out what NAME refers to. */
18739 if (TREE_CODE (scope) == TYPENAME_TYPE)
18740 scope = resolve_typename_type (scope, only_current_p);
18741 /* If we don't know what SCOPE refers to, then we cannot resolve the
18742 TYPENAME_TYPE. */
18743 if (TREE_CODE (scope) == TYPENAME_TYPE)
18744 return type;
18745 /* If the SCOPE is a template type parameter, we have no way of
18746 resolving the name. */
18747 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
18748 return type;
18749 /* If the SCOPE is not the current instantiation, there's no reason
18750 to look inside it. */
18751 if (only_current_p && !currently_open_class (scope))
18752 return type;
18753 /* If this is a typedef, we don't want to look inside (c++/11987). */
18754 if (typedef_variant_p (type))
18755 return type;
18756 /* If SCOPE isn't the template itself, it will not have a valid
18757 TYPE_FIELDS list. */
18758 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
18759 /* scope is either the template itself or a compatible instantiation
18760 like X<T>, so look up the name in the original template. */
18761 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
18762 else
18763 /* scope is a partial instantiation, so we can't do the lookup or we
18764 will lose the template arguments. */
18765 return type;
18766 /* Enter the SCOPE so that name lookup will be resolved as if we
18767 were in the class definition. In particular, SCOPE will no
18768 longer be considered a dependent type. */
18769 pushed_scope = push_scope (scope);
18770 /* Look up the declaration. */
18771 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
18773 result = NULL_TREE;
18775 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18776 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18777 if (!decl)
18778 /*nop*/;
18779 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
18780 && TREE_CODE (decl) == TYPE_DECL)
18782 result = TREE_TYPE (decl);
18783 if (result == error_mark_node)
18784 result = NULL_TREE;
18786 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
18787 && DECL_CLASS_TEMPLATE_P (decl))
18789 tree tmpl;
18790 tree args;
18791 /* Obtain the template and the arguments. */
18792 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
18793 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
18794 /* Instantiate the template. */
18795 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
18796 /*entering_scope=*/0,
18797 tf_error | tf_user);
18798 if (result == error_mark_node)
18799 result = NULL_TREE;
18802 /* Leave the SCOPE. */
18803 if (pushed_scope)
18804 pop_scope (pushed_scope);
18806 /* If we failed to resolve it, return the original typename. */
18807 if (!result)
18808 return type;
18810 /* If lookup found a typename type, resolve that too. */
18811 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
18813 /* Ill-formed programs can cause infinite recursion here, so we
18814 must catch that. */
18815 TYPENAME_IS_RESOLVING_P (type) = 1;
18816 result = resolve_typename_type (result, only_current_p);
18817 TYPENAME_IS_RESOLVING_P (type) = 0;
18820 /* Qualify the resulting type. */
18821 quals = cp_type_quals (type);
18822 if (quals)
18823 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
18825 return result;
18828 /* EXPR is an expression which is not type-dependent. Return a proxy
18829 for EXPR that can be used to compute the types of larger
18830 expressions containing EXPR. */
18832 tree
18833 build_non_dependent_expr (tree expr)
18835 tree inner_expr;
18837 /* Preserve null pointer constants so that the type of things like
18838 "p == 0" where "p" is a pointer can be determined. */
18839 if (null_ptr_cst_p (expr))
18840 return expr;
18841 /* Preserve OVERLOADs; the functions must be available to resolve
18842 types. */
18843 inner_expr = expr;
18844 if (TREE_CODE (inner_expr) == STMT_EXPR)
18845 inner_expr = stmt_expr_value_expr (inner_expr);
18846 if (TREE_CODE (inner_expr) == ADDR_EXPR)
18847 inner_expr = TREE_OPERAND (inner_expr, 0);
18848 if (TREE_CODE (inner_expr) == COMPONENT_REF)
18849 inner_expr = TREE_OPERAND (inner_expr, 1);
18850 if (is_overloaded_fn (inner_expr)
18851 || TREE_CODE (inner_expr) == OFFSET_REF)
18852 return expr;
18853 /* There is no need to return a proxy for a variable. */
18854 if (TREE_CODE (expr) == VAR_DECL)
18855 return expr;
18856 /* Preserve string constants; conversions from string constants to
18857 "char *" are allowed, even though normally a "const char *"
18858 cannot be used to initialize a "char *". */
18859 if (TREE_CODE (expr) == STRING_CST)
18860 return expr;
18861 /* Preserve arithmetic constants, as an optimization -- there is no
18862 reason to create a new node. */
18863 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
18864 return expr;
18865 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18866 There is at least one place where we want to know that a
18867 particular expression is a throw-expression: when checking a ?:
18868 expression, there are special rules if the second or third
18869 argument is a throw-expression. */
18870 if (TREE_CODE (expr) == THROW_EXPR)
18871 return expr;
18873 if (TREE_CODE (expr) == COND_EXPR)
18874 return build3 (COND_EXPR,
18875 TREE_TYPE (expr),
18876 TREE_OPERAND (expr, 0),
18877 (TREE_OPERAND (expr, 1)
18878 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
18879 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
18880 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
18881 if (TREE_CODE (expr) == COMPOUND_EXPR
18882 && !COMPOUND_EXPR_OVERLOADED (expr))
18883 return build2 (COMPOUND_EXPR,
18884 TREE_TYPE (expr),
18885 TREE_OPERAND (expr, 0),
18886 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
18888 /* Keep dereferences outside the NON_DEPENDENT_EXPR so lvalue_kind
18889 doesn't need to look inside. */
18890 if (TREE_CODE (expr) == INDIRECT_REF && REFERENCE_REF_P (expr))
18891 return convert_from_reference (build_non_dependent_expr
18892 (TREE_OPERAND (expr, 0)));
18894 /* If the type is unknown, it can't really be non-dependent */
18895 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
18897 /* Otherwise, build a NON_DEPENDENT_EXPR. */
18898 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
18901 /* ARGS is a vector of expressions as arguments to a function call.
18902 Replace the arguments with equivalent non-dependent expressions.
18903 This modifies ARGS in place. */
18905 void
18906 make_args_non_dependent (VEC(tree,gc) *args)
18908 unsigned int ix;
18909 tree arg;
18911 FOR_EACH_VEC_ELT (tree, args, ix, arg)
18913 tree newarg = build_non_dependent_expr (arg);
18914 if (newarg != arg)
18915 VEC_replace (tree, args, ix, newarg);
18919 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18920 with a level one deeper than the actual template parms. */
18922 tree
18923 make_auto (void)
18925 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18926 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18927 TYPE_DECL, get_identifier ("auto"), au);
18928 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18929 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18930 (0, processing_template_decl + 1, processing_template_decl + 1,
18931 0, TYPE_NAME (au), NULL_TREE);
18932 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18933 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18934 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18936 return au;
18939 /* Given type ARG, return std::initializer_list<ARG>. */
18941 static tree
18942 listify (tree arg)
18944 tree std_init_list = namespace_binding
18945 (get_identifier ("initializer_list"), std_node);
18946 tree argvec;
18947 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18949 error ("deducing from brace-enclosed initializer list requires "
18950 "#include <initializer_list>");
18951 return error_mark_node;
18953 argvec = make_tree_vec (1);
18954 TREE_VEC_ELT (argvec, 0) = arg;
18955 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18956 NULL_TREE, 0, tf_warning_or_error);
18959 /* Replace auto in TYPE with std::initializer_list<auto>. */
18961 static tree
18962 listify_autos (tree type, tree auto_node)
18964 tree init_auto = listify (auto_node);
18965 tree argvec = make_tree_vec (1);
18966 TREE_VEC_ELT (argvec, 0) = init_auto;
18967 if (processing_template_decl)
18968 argvec = add_to_template_args (current_template_args (), argvec);
18969 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18972 /* walk_tree helper for do_auto_deduction. */
18974 static tree
18975 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
18976 void *type)
18978 /* Is this a variable with the type we're looking for? */
18979 if (DECL_P (*tp)
18980 && TREE_TYPE (*tp) == type)
18981 return *tp;
18982 else
18983 return NULL_TREE;
18986 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18987 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18989 tree
18990 do_auto_deduction (tree type, tree init, tree auto_node)
18992 tree parms, tparms, targs;
18993 tree args[1];
18994 tree decl;
18995 int val;
18997 /* The name of the object being declared shall not appear in the
18998 initializer expression. */
18999 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
19000 if (decl)
19002 error ("variable %q#D with %<auto%> type used in its own "
19003 "initializer", decl);
19004 return error_mark_node;
19007 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
19008 with either a new invented type template parameter U or, if the
19009 initializer is a braced-init-list (8.5.4), with
19010 std::initializer_list<U>. */
19011 if (BRACE_ENCLOSED_INITIALIZER_P (init))
19012 type = listify_autos (type, auto_node);
19014 init = resolve_nondeduced_context (init);
19016 parms = build_tree_list (NULL_TREE, type);
19017 args[0] = init;
19018 tparms = make_tree_vec (1);
19019 targs = make_tree_vec (1);
19020 TREE_VEC_ELT (tparms, 0)
19021 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
19022 val = type_unification_real (tparms, targs, parms, args, 1, 0,
19023 DEDUCE_CALL, LOOKUP_NORMAL);
19024 if (val > 0)
19026 if (type && type != error_mark_node)
19027 /* If type is error_mark_node a diagnostic must have been
19028 emitted by now. Also, having a mention to '<type error>'
19029 in the diagnostic is not really useful to the user. */
19030 error ("unable to deduce %qT from %qE", type, init);
19031 return error_mark_node;
19034 /* If the list of declarators contains more than one declarator, the type
19035 of each declared variable is determined as described above. If the
19036 type deduced for the template parameter U is not the same in each
19037 deduction, the program is ill-formed. */
19038 if (TREE_TYPE (auto_node)
19039 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
19041 error ("inconsistent deduction for %qT: %qT and then %qT",
19042 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
19043 return error_mark_node;
19045 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
19047 if (processing_template_decl)
19048 targs = add_to_template_args (current_template_args (), targs);
19049 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
19052 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
19053 result. */
19055 tree
19056 splice_late_return_type (tree type, tree late_return_type)
19058 tree argvec;
19060 if (late_return_type == NULL_TREE)
19061 return type;
19062 argvec = make_tree_vec (1);
19063 TREE_VEC_ELT (argvec, 0) = late_return_type;
19064 if (processing_template_decl)
19065 argvec = add_to_template_args (current_template_args (), argvec);
19066 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
19069 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
19071 bool
19072 is_auto (const_tree type)
19074 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19075 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
19076 return true;
19077 else
19078 return false;
19081 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
19082 appear as a type-specifier for the declaration in question, we don't
19083 have to look through the whole type. */
19085 tree
19086 type_uses_auto (tree type)
19088 enum tree_code code;
19089 if (is_auto (type))
19090 return type;
19092 code = TREE_CODE (type);
19094 if (code == POINTER_TYPE || code == REFERENCE_TYPE
19095 || code == OFFSET_TYPE || code == FUNCTION_TYPE
19096 || code == METHOD_TYPE || code == ARRAY_TYPE)
19097 return type_uses_auto (TREE_TYPE (type));
19099 if (TYPE_PTRMEMFUNC_P (type))
19100 return type_uses_auto (TREE_TYPE (TREE_TYPE
19101 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
19103 return NULL_TREE;
19106 /* For a given template T, return the vector of typedefs referenced
19107 in T for which access check is needed at T instantiation time.
19108 T is either a FUNCTION_DECL or a RECORD_TYPE.
19109 Those typedefs were added to T by the function
19110 append_type_to_template_for_access_check. */
19112 VEC(qualified_typedef_usage_t,gc)*
19113 get_types_needing_access_check (tree t)
19115 tree ti;
19116 VEC(qualified_typedef_usage_t,gc) *result = NULL;
19118 if (!t || t == error_mark_node)
19119 return NULL;
19121 if (!(ti = get_template_info (t)))
19122 return NULL;
19124 if (CLASS_TYPE_P (t)
19125 || TREE_CODE (t) == FUNCTION_DECL)
19127 if (!TI_TEMPLATE (ti))
19128 return NULL;
19130 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
19133 return result;
19136 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
19137 tied to T. That list of typedefs will be access checked at
19138 T instantiation time.
19139 T is either a FUNCTION_DECL or a RECORD_TYPE.
19140 TYPE_DECL is a TYPE_DECL node representing a typedef.
19141 SCOPE is the scope through which TYPE_DECL is accessed.
19142 LOCATION is the location of the usage point of TYPE_DECL.
19144 This function is a subroutine of
19145 append_type_to_template_for_access_check. */
19147 static void
19148 append_type_to_template_for_access_check_1 (tree t,
19149 tree type_decl,
19150 tree scope,
19151 location_t location)
19153 qualified_typedef_usage_t typedef_usage;
19154 tree ti;
19156 if (!t || t == error_mark_node)
19157 return;
19159 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
19160 || CLASS_TYPE_P (t))
19161 && type_decl
19162 && TREE_CODE (type_decl) == TYPE_DECL
19163 && scope);
19165 if (!(ti = get_template_info (t)))
19166 return;
19168 gcc_assert (TI_TEMPLATE (ti));
19170 typedef_usage.typedef_decl = type_decl;
19171 typedef_usage.context = scope;
19172 typedef_usage.locus = location;
19174 VEC_safe_push (qualified_typedef_usage_t, gc,
19175 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
19176 &typedef_usage);
19179 /* Append TYPE_DECL to the template TEMPL.
19180 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
19181 At TEMPL instanciation time, TYPE_DECL will be checked to see
19182 if it can be accessed through SCOPE.
19183 LOCATION is the location of the usage point of TYPE_DECL.
19185 e.g. consider the following code snippet:
19187 class C
19189 typedef int myint;
19192 template<class U> struct S
19194 C::myint mi; // <-- usage point of the typedef C::myint
19197 S<char> s;
19199 At S<char> instantiation time, we need to check the access of C::myint
19200 In other words, we need to check the access of the myint typedef through
19201 the C scope. For that purpose, this function will add the myint typedef
19202 and the scope C through which its being accessed to a list of typedefs
19203 tied to the template S. That list will be walked at template instantiation
19204 time and access check performed on each typedefs it contains.
19205 Note that this particular code snippet should yield an error because
19206 myint is private to C. */
19208 void
19209 append_type_to_template_for_access_check (tree templ,
19210 tree type_decl,
19211 tree scope,
19212 location_t location)
19214 qualified_typedef_usage_t *iter;
19215 int i;
19217 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
19219 /* Make sure we don't append the type to the template twice. */
19220 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
19221 get_types_needing_access_check (templ),
19222 i, iter)
19223 if (iter->typedef_decl == type_decl && scope == iter->context)
19224 return;
19226 append_type_to_template_for_access_check_1 (templ, type_decl,
19227 scope, location);
19230 /* Set up the hash tables for template instantiations. */
19232 void
19233 init_template_processing (void)
19235 decl_specializations = htab_create_ggc (37,
19236 hash_specialization,
19237 eq_specializations,
19238 ggc_free);
19239 type_specializations = htab_create_ggc (37,
19240 hash_specialization,
19241 eq_specializations,
19242 ggc_free);
19245 /* Print stats about the template hash tables for -fstats. */
19247 void
19248 print_template_statistics (void)
19250 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
19251 "%f collisions\n", (long) htab_size (decl_specializations),
19252 (long) htab_elements (decl_specializations),
19253 htab_collisions (decl_specializations));
19254 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
19255 "%f collisions\n", (long) htab_size (type_specializations),
19256 (long) htab_elements (type_specializations),
19257 htab_collisions (type_specializations));
19260 #include "gt-cp-pt.h"