* pt.c (unify_pack_expansion): Handle deduction from init-list.
[official-gcc/constexpr.git] / gcc / cp / pt.c
blobd8147365fed2c09a7bba1b564a3f04fd96d56a0f
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "intl.h"
36 #include "pointer-set.h"
37 #include "flags.h"
38 #include "c-common.h"
39 #include "cp-tree.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
42 #include "decl.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "rtl.h"
47 #include "timevar.h"
48 #include "tree-iterator.h"
49 #include "vecprim.h"
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY (()) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
66 int processing_template_parmlist;
67 static int template_header_count;
69 static GTY(()) tree saved_trees;
70 static VEC(int,heap) *inline_parm_levels;
72 static GTY(()) struct tinst_level *current_tinst_level;
74 static GTY(()) tree saved_access_scope;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
81 /* A map from local variable declarations in the body of the template
82 presently being instantiated to the corresponding instantiated
83 local variables. */
84 static htab_t local_specializations;
86 typedef struct GTY(()) spec_entry
88 tree tmpl;
89 tree args;
90 tree spec;
91 } spec_entry;
93 static GTY ((param_is (spec_entry)))
94 htab_t decl_specializations;
96 static GTY ((param_is (spec_entry)))
97 htab_t type_specializations;
99 /* Contains canonical template parameter types. The vector is indexed by
100 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
101 TREE_LIST, whose TREE_VALUEs contain the canonical template
102 parameters of various types and levels. */
103 static GTY(()) VEC(tree,gc) *canonical_template_parms;
105 #define UNIFY_ALLOW_NONE 0
106 #define UNIFY_ALLOW_MORE_CV_QUAL 1
107 #define UNIFY_ALLOW_LESS_CV_QUAL 2
108 #define UNIFY_ALLOW_DERIVED 4
109 #define UNIFY_ALLOW_INTEGER 8
110 #define UNIFY_ALLOW_OUTER_LEVEL 16
111 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
112 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
114 static void push_access_scope (tree);
115 static void pop_access_scope (tree);
116 static bool resolve_overloaded_unification (tree, tree, tree, tree,
117 unification_kind_t, int);
118 static int try_one_overload (tree, tree, tree, tree, tree,
119 unification_kind_t, int, bool);
120 static int unify (tree, tree, tree, tree, int);
121 static void add_pending_template (tree);
122 static int push_tinst_level (tree);
123 static void pop_tinst_level (void);
124 static tree reopen_tinst_level (struct tinst_level *);
125 static tree tsubst_initializer_list (tree, tree);
126 static tree get_class_bindings (tree, tree, tree);
127 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
128 bool, bool);
129 static void tsubst_enum (tree, tree, tree);
130 static tree add_to_template_args (tree, tree);
131 static tree add_outermost_template_args (tree, tree);
132 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
133 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
134 tree);
135 static int type_unification_real (tree, tree, tree, const tree *,
136 unsigned int, int, unification_kind_t, int);
137 static void note_template_header (int);
138 static tree convert_nontype_argument_function (tree, tree);
139 static tree convert_nontype_argument (tree, tree);
140 static tree convert_template_argument (tree, tree, tree,
141 tsubst_flags_t, int, tree);
142 static int for_each_template_parm (tree, tree_fn_t, void*,
143 struct pointer_set_t*, bool);
144 static tree expand_template_argument_pack (tree);
145 static tree build_template_parm_index (int, int, int, tree, tree);
146 static bool inline_needs_template_parms (tree);
147 static void push_inline_template_parms_recursive (tree, int);
148 static tree retrieve_local_specialization (tree);
149 static void register_local_specialization (tree, tree);
150 static hashval_t hash_specialization (const void *p);
151 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
152 static int mark_template_parm (tree, void *);
153 static int template_parm_this_level_p (tree, void *);
154 static tree tsubst_friend_function (tree, tree);
155 static tree tsubst_friend_class (tree, tree);
156 static int can_complete_type_without_circularity (tree);
157 static tree get_bindings (tree, tree, tree, bool);
158 static int template_decl_level (tree);
159 static int check_cv_quals_for_unify (int, tree, tree);
160 static void template_parm_level_and_index (tree, int*, int*);
161 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
162 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
165 static void regenerate_decl_from_template (tree, tree);
166 static tree most_specialized_class (tree, tree);
167 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
168 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
169 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
170 static bool check_specialization_scope (void);
171 static tree process_partial_specialization (tree);
172 static void set_current_access_from_decl (tree);
173 static tree get_template_base (tree, tree, tree, tree);
174 static tree try_class_unification (tree, tree, tree, tree);
175 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
176 tree, tree);
177 static bool template_template_parm_bindings_ok_p (tree, tree);
178 static int template_args_equal (tree, tree);
179 static void tsubst_default_arguments (tree);
180 static tree for_each_template_parm_r (tree *, int *, void *);
181 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
182 static void copy_default_args_to_explicit_spec (tree);
183 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
184 static int eq_local_specializations (const void *, const void *);
185 static bool dependent_template_arg_p (tree);
186 static bool any_template_arguments_need_structural_equality_p (tree);
187 static bool dependent_type_p_r (tree);
188 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
189 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_decl (tree, tree, tsubst_flags_t);
192 static void perform_typedefs_access_check (tree tmpl, tree targs);
193 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
194 location_t);
195 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
196 static tree listify (tree);
197 static tree listify_autos (tree, tree);
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);
321 return tinfo;
324 /* Returns the template nesting level of the indicated class TYPE.
326 For example, in:
327 template <class T>
328 struct A
330 template <class U>
331 struct B {};
334 A<T>::B<U> has depth two, while A<T> has depth one.
335 Both A<T>::B<int> and A<int>::B<U> have depth one, if
336 they are instantiations, not specializations.
338 This function is guaranteed to return 0 if passed NULL_TREE so
339 that, for example, `template_class_depth (current_class_type)' is
340 always safe. */
343 template_class_depth (tree type)
345 int depth;
347 for (depth = 0;
348 type && TREE_CODE (type) != NAMESPACE_DECL;
349 type = (TREE_CODE (type) == FUNCTION_DECL)
350 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
352 tree tinfo = get_template_info (type);
354 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
355 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
356 ++depth;
359 return depth;
362 /* Subroutine of maybe_begin_member_template_processing.
363 Returns true if processing DECL needs us to push template parms. */
365 static bool
366 inline_needs_template_parms (tree decl)
368 if (! DECL_TEMPLATE_INFO (decl))
369 return false;
371 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
372 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
375 /* Subroutine of maybe_begin_member_template_processing.
376 Push the template parms in PARMS, starting from LEVELS steps into the
377 chain, and ending at the beginning, since template parms are listed
378 innermost first. */
380 static void
381 push_inline_template_parms_recursive (tree parmlist, int levels)
383 tree parms = TREE_VALUE (parmlist);
384 int i;
386 if (levels > 1)
387 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
389 ++processing_template_decl;
390 current_template_parms
391 = tree_cons (size_int (processing_template_decl),
392 parms, current_template_parms);
393 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
395 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
396 NULL);
397 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
399 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
401 if (parm == error_mark_node)
402 continue;
404 gcc_assert (DECL_P (parm));
406 switch (TREE_CODE (parm))
408 case TYPE_DECL:
409 case TEMPLATE_DECL:
410 pushdecl (parm);
411 break;
413 case PARM_DECL:
415 /* Make a CONST_DECL as is done in process_template_parm.
416 It is ugly that we recreate this here; the original
417 version built in process_template_parm is no longer
418 available. */
419 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
420 CONST_DECL, DECL_NAME (parm),
421 TREE_TYPE (parm));
422 DECL_ARTIFICIAL (decl) = 1;
423 TREE_CONSTANT (decl) = 1;
424 TREE_READONLY (decl) = 1;
425 DECL_INITIAL (decl) = DECL_INITIAL (parm);
426 SET_DECL_TEMPLATE_PARM_P (decl);
427 pushdecl (decl);
429 break;
431 default:
432 gcc_unreachable ();
437 /* Restore the template parameter context for a member template or
438 a friend template defined in a class definition. */
440 void
441 maybe_begin_member_template_processing (tree decl)
443 tree parms;
444 int levels = 0;
446 if (inline_needs_template_parms (decl))
448 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
449 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
451 if (DECL_TEMPLATE_SPECIALIZATION (decl))
453 --levels;
454 parms = TREE_CHAIN (parms);
457 push_inline_template_parms_recursive (parms, levels);
460 /* Remember how many levels of template parameters we pushed so that
461 we can pop them later. */
462 VEC_safe_push (int, heap, inline_parm_levels, levels);
465 /* Undo the effects of maybe_begin_member_template_processing. */
467 void
468 maybe_end_member_template_processing (void)
470 int i;
471 int last;
473 if (VEC_length (int, inline_parm_levels) == 0)
474 return;
476 last = VEC_pop (int, inline_parm_levels);
477 for (i = 0; i < last; ++i)
479 --processing_template_decl;
480 current_template_parms = TREE_CHAIN (current_template_parms);
481 poplevel (0, 0, 0);
485 /* Return a new template argument vector which contains all of ARGS,
486 but has as its innermost set of arguments the EXTRA_ARGS. */
488 static tree
489 add_to_template_args (tree args, tree extra_args)
491 tree new_args;
492 int extra_depth;
493 int i;
494 int j;
496 extra_depth = TMPL_ARGS_DEPTH (extra_args);
497 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
499 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
500 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
502 for (j = 1; j <= extra_depth; ++j, ++i)
503 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
505 return new_args;
508 /* Like add_to_template_args, but only the outermost ARGS are added to
509 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
510 (EXTRA_ARGS) levels are added. This function is used to combine
511 the template arguments from a partial instantiation with the
512 template arguments used to attain the full instantiation from the
513 partial instantiation. */
515 static tree
516 add_outermost_template_args (tree args, tree extra_args)
518 tree new_args;
520 /* If there are more levels of EXTRA_ARGS than there are ARGS,
521 something very fishy is going on. */
522 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
524 /* If *all* the new arguments will be the EXTRA_ARGS, just return
525 them. */
526 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
527 return extra_args;
529 /* For the moment, we make ARGS look like it contains fewer levels. */
530 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
532 new_args = add_to_template_args (args, extra_args);
534 /* Now, we restore ARGS to its full dimensions. */
535 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
537 return new_args;
540 /* Return the N levels of innermost template arguments from the ARGS. */
542 tree
543 get_innermost_template_args (tree args, int n)
545 tree new_args;
546 int extra_levels;
547 int i;
549 gcc_assert (n >= 0);
551 /* If N is 1, just return the innermost set of template arguments. */
552 if (n == 1)
553 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
555 /* If we're not removing anything, just return the arguments we were
556 given. */
557 extra_levels = TMPL_ARGS_DEPTH (args) - n;
558 gcc_assert (extra_levels >= 0);
559 if (extra_levels == 0)
560 return args;
562 /* Make a new set of arguments, not containing the outer arguments. */
563 new_args = make_tree_vec (n);
564 for (i = 1; i <= n; ++i)
565 SET_TMPL_ARGS_LEVEL (new_args, i,
566 TMPL_ARGS_LEVEL (args, i + extra_levels));
568 return new_args;
571 /* The inverse of get_innermost_template_args: Return all but the innermost
572 EXTRA_LEVELS levels of template arguments from the ARGS. */
574 static tree
575 strip_innermost_template_args (tree args, int extra_levels)
577 tree new_args;
578 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
579 int i;
581 gcc_assert (n >= 0);
583 /* If N is 1, just return the outermost set of template arguments. */
584 if (n == 1)
585 return TMPL_ARGS_LEVEL (args, 1);
587 /* If we're not removing anything, just return the arguments we were
588 given. */
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
593 /* Make a new set of arguments, not containing the inner arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i));
599 return new_args;
602 /* We've got a template header coming up; push to a new level for storing
603 the parms. */
605 void
606 begin_template_parm_list (void)
608 /* We use a non-tag-transparent scope here, which causes pushtag to
609 put tags in this scope, rather than in the enclosing class or
610 namespace scope. This is the right thing, since we want
611 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
612 global template class, push_template_decl handles putting the
613 TEMPLATE_DECL into top-level scope. For a nested template class,
614 e.g.:
616 template <class T> struct S1 {
617 template <class T> struct S2 {};
620 pushtag contains special code to call pushdecl_with_scope on the
621 TEMPLATE_DECL for S2. */
622 begin_scope (sk_template_parms, NULL);
623 ++processing_template_decl;
624 ++processing_template_parmlist;
625 note_template_header (0);
628 /* This routine is called when a specialization is declared. If it is
629 invalid to declare a specialization here, an error is reported and
630 false is returned, otherwise this routine will return true. */
632 static bool
633 check_specialization_scope (void)
635 tree scope = current_scope ();
637 /* [temp.expl.spec]
639 An explicit specialization shall be declared in the namespace of
640 which the template is a member, or, for member templates, in the
641 namespace of which the enclosing class or enclosing class
642 template is a member. An explicit specialization of a member
643 function, member class or static data member of a class template
644 shall be declared in the namespace of which the class template
645 is a member. */
646 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
648 error ("explicit specialization in non-namespace scope %qD", scope);
649 return false;
652 /* [temp.expl.spec]
654 In an explicit specialization declaration for a member of a class
655 template or a member template that appears in namespace scope,
656 the member template and some of its enclosing class templates may
657 remain unspecialized, except that the declaration shall not
658 explicitly specialize a class member template if its enclosing
659 class templates are not explicitly specialized as well. */
660 if (current_template_parms)
662 error ("enclosing class templates are not explicitly specialized");
663 return false;
666 return true;
669 /* We've just seen template <>. */
671 bool
672 begin_specialization (void)
674 begin_scope (sk_template_spec, NULL);
675 note_template_header (1);
676 return check_specialization_scope ();
679 /* Called at then end of processing a declaration preceded by
680 template<>. */
682 void
683 end_specialization (void)
685 finish_scope ();
686 reset_specialization ();
689 /* Any template <>'s that we have seen thus far are not referring to a
690 function specialization. */
692 void
693 reset_specialization (void)
695 processing_specialization = 0;
696 template_header_count = 0;
699 /* We've just seen a template header. If SPECIALIZATION is nonzero,
700 it was of the form template <>. */
702 static void
703 note_template_header (int specialization)
705 processing_specialization = specialization;
706 template_header_count++;
709 /* We're beginning an explicit instantiation. */
711 void
712 begin_explicit_instantiation (void)
714 gcc_assert (!processing_explicit_instantiation);
715 processing_explicit_instantiation = true;
719 void
720 end_explicit_instantiation (void)
722 gcc_assert (processing_explicit_instantiation);
723 processing_explicit_instantiation = false;
726 /* An explicit specialization or partial specialization TMPL is being
727 declared. Check that the namespace in which the specialization is
728 occurring is permissible. Returns false iff it is invalid to
729 specialize TMPL in the current namespace. */
731 static bool
732 check_specialization_namespace (tree tmpl)
734 tree tpl_ns = decl_namespace_context (tmpl);
736 /* [tmpl.expl.spec]
738 An explicit specialization shall be declared in the namespace of
739 which the template is a member, or, for member templates, in the
740 namespace of which the enclosing class or enclosing class
741 template is a member. An explicit specialization of a member
742 function, member class or static data member of a class template
743 shall be declared in the namespace of which the class template is
744 a member. */
745 if (current_scope() != DECL_CONTEXT (tmpl)
746 && !at_namespace_scope_p ())
748 error ("specialization of %qD must appear at namespace scope", tmpl);
749 return false;
751 if (is_associated_namespace (current_namespace, tpl_ns))
752 /* Same or super-using namespace. */
753 return true;
754 else
756 permerror (input_location, "specialization of %qD in different namespace", tmpl);
757 permerror (input_location, " from definition of %q+#D", tmpl);
758 return false;
762 /* SPEC is an explicit instantiation. Check that it is valid to
763 perform this explicit instantiation in the current namespace. */
765 static void
766 check_explicit_instantiation_namespace (tree spec)
768 tree ns;
770 /* DR 275: An explicit instantiation shall appear in an enclosing
771 namespace of its template. */
772 ns = decl_namespace_context (spec);
773 if (!is_ancestor (current_namespace, ns))
774 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
775 "(which does not enclose namespace %qD)",
776 spec, current_namespace, ns);
779 /* The TYPE is being declared. If it is a template type, that means it
780 is a partial specialization. Do appropriate error-checking. */
782 tree
783 maybe_process_partial_specialization (tree type)
785 tree context;
787 if (type == error_mark_node)
788 return error_mark_node;
790 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
792 error ("name of class shadows template template parameter %qD",
793 TYPE_NAME (type));
794 return error_mark_node;
797 context = TYPE_CONTEXT (type);
799 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
801 /* This is for ordinary explicit specialization and partial
802 specialization of a template class such as:
804 template <> class C<int>;
808 template <class T> class C<T*>;
810 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
812 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
813 && !COMPLETE_TYPE_P (type))
815 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
816 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
817 if (processing_template_decl)
819 if (push_template_decl (TYPE_MAIN_DECL (type))
820 == error_mark_node)
821 return error_mark_node;
824 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
825 error ("specialization of %qT after instantiation", type);
827 else if (CLASS_TYPE_P (type)
828 && !CLASSTYPE_USE_TEMPLATE (type)
829 && CLASSTYPE_TEMPLATE_INFO (type)
830 && context && CLASS_TYPE_P (context)
831 && CLASSTYPE_TEMPLATE_INFO (context))
833 /* This is for an explicit specialization of member class
834 template according to [temp.expl.spec/18]:
836 template <> template <class U> class C<int>::D;
838 The context `C<int>' must be an implicit instantiation.
839 Otherwise this is just a member class template declared
840 earlier like:
842 template <> class C<int> { template <class U> class D; };
843 template <> template <class U> class C<int>::D;
845 In the first case, `C<int>::D' is a specialization of `C<T>::D'
846 while in the second case, `C<int>::D' is a primary template
847 and `C<T>::D' may not exist. */
849 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
850 && !COMPLETE_TYPE_P (type))
852 tree t;
853 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
855 if (current_namespace
856 != decl_namespace_context (tmpl))
858 permerror (input_location, "specializing %q#T in different namespace", type);
859 permerror (input_location, " from definition of %q+#D", tmpl);
862 /* Check for invalid specialization after instantiation:
864 template <> template <> class C<int>::D<int>;
865 template <> template <class U> class C<int>::D; */
867 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
868 t; t = TREE_CHAIN (t))
870 tree inst = TREE_VALUE (t);
871 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
873 /* We already have a full specialization of this partial
874 instantiation. Reassign it to the new member
875 specialization template. */
876 spec_entry elt;
877 spec_entry **slot;
879 elt.tmpl = most_general_template (tmpl);
880 elt.args = CLASSTYPE_TI_ARGS (inst);
881 elt.spec = inst;
883 htab_remove_elt (type_specializations, &elt);
885 elt.tmpl = tmpl;
886 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
888 slot = (spec_entry **)
889 htab_find_slot (type_specializations, &elt, INSERT);
890 *slot = GGC_NEW (spec_entry);
891 **slot = elt;
893 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
894 /* But if we've had an implicit instantiation, that's a
895 problem ([temp.expl.spec]/6). */
896 error ("specialization %qT after instantiation %qT",
897 type, inst);
900 /* Mark TYPE as a specialization. And as a result, we only
901 have one level of template argument for the innermost
902 class template. */
903 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
904 CLASSTYPE_TI_ARGS (type)
905 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
908 else if (processing_specialization)
910 error ("explicit specialization of non-template %qT", type);
911 return error_mark_node;
914 return type;
917 /* Returns nonzero if we can optimize the retrieval of specializations
918 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
919 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
921 static inline bool
922 optimize_specialization_lookup_p (tree tmpl)
924 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
925 && DECL_CLASS_SCOPE_P (tmpl)
926 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
927 parameter. */
928 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
929 /* The optimized lookup depends on the fact that the
930 template arguments for the member function template apply
931 purely to the containing class, which is not true if the
932 containing class is an explicit or partial
933 specialization. */
934 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
935 && !DECL_MEMBER_TEMPLATE_P (tmpl)
936 && !DECL_CONV_FN_P (tmpl)
937 /* It is possible to have a template that is not a member
938 template and is not a member of a template class:
940 template <typename T>
941 struct S { friend A::f(); };
943 Here, the friend function is a template, but the context does
944 not have template information. The optimized lookup relies
945 on having ARGS be the template arguments for both the class
946 and the function template. */
947 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
950 /* Retrieve the specialization (in the sense of [temp.spec] - a
951 specialization is either an instantiation or an explicit
952 specialization) of TMPL for the given template ARGS. If there is
953 no such specialization, return NULL_TREE. The ARGS are a vector of
954 arguments, or a vector of vectors of arguments, in the case of
955 templates with more than one level of parameters.
957 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
958 then we search for a partial specialization matching ARGS. This
959 parameter is ignored if TMPL is not a class template. */
961 static tree
962 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
964 if (args == error_mark_node)
965 return NULL_TREE;
967 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
969 /* There should be as many levels of arguments as there are
970 levels of parameters. */
971 gcc_assert (TMPL_ARGS_DEPTH (args)
972 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
974 if (optimize_specialization_lookup_p (tmpl))
976 tree class_template;
977 tree class_specialization;
978 VEC(tree,gc) *methods;
979 tree fns;
980 int idx;
982 /* The template arguments actually apply to the containing
983 class. Find the class specialization with those
984 arguments. */
985 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
986 class_specialization
987 = retrieve_specialization (class_template, args, 0);
988 if (!class_specialization)
989 return NULL_TREE;
990 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
991 for the specialization. */
992 idx = class_method_index_for_fn (class_specialization, tmpl);
993 if (idx == -1)
994 return NULL_TREE;
995 /* Iterate through the methods with the indicated name, looking
996 for the one that has an instance of TMPL. */
997 methods = CLASSTYPE_METHOD_VEC (class_specialization);
998 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1000 tree fn = OVL_CURRENT (fns);
1001 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1002 /* using-declarations can add base methods to the method vec,
1003 and we don't want those here. */
1004 && DECL_CONTEXT (fn) == class_specialization)
1005 return fn;
1007 return NULL_TREE;
1009 else
1011 spec_entry *found;
1012 spec_entry elt;
1013 htab_t specializations;
1015 elt.tmpl = tmpl;
1016 elt.args = args;
1017 elt.spec = NULL_TREE;
1019 if (DECL_CLASS_TEMPLATE_P (tmpl))
1020 specializations = type_specializations;
1021 else
1022 specializations = decl_specializations;
1024 if (hash == 0)
1025 hash = hash_specialization (&elt);
1026 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1027 if (found)
1028 return found->spec;
1031 return NULL_TREE;
1034 /* Like retrieve_specialization, but for local declarations. */
1036 static tree
1037 retrieve_local_specialization (tree tmpl)
1039 tree spec;
1041 if (local_specializations == NULL)
1042 return NULL_TREE;
1044 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1045 htab_hash_pointer (tmpl));
1046 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1049 /* Returns nonzero iff DECL is a specialization of TMPL. */
1052 is_specialization_of (tree decl, tree tmpl)
1054 tree t;
1056 if (TREE_CODE (decl) == FUNCTION_DECL)
1058 for (t = decl;
1059 t != NULL_TREE;
1060 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1061 if (t == tmpl)
1062 return 1;
1064 else
1066 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1068 for (t = TREE_TYPE (decl);
1069 t != NULL_TREE;
1070 t = CLASSTYPE_USE_TEMPLATE (t)
1071 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1072 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1073 return 1;
1076 return 0;
1079 /* Returns nonzero iff DECL is a specialization of friend declaration
1080 FRIEND_DECL according to [temp.friend]. */
1082 bool
1083 is_specialization_of_friend (tree decl, tree friend_decl)
1085 bool need_template = true;
1086 int template_depth;
1088 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1089 || TREE_CODE (decl) == TYPE_DECL);
1091 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1092 of a template class, we want to check if DECL is a specialization
1093 if this. */
1094 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1095 && DECL_TEMPLATE_INFO (friend_decl)
1096 && !DECL_USE_TEMPLATE (friend_decl))
1098 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1099 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1100 need_template = false;
1102 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1103 && !PRIMARY_TEMPLATE_P (friend_decl))
1104 need_template = false;
1106 /* There is nothing to do if this is not a template friend. */
1107 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1108 return false;
1110 if (is_specialization_of (decl, friend_decl))
1111 return true;
1113 /* [temp.friend/6]
1114 A member of a class template may be declared to be a friend of a
1115 non-template class. In this case, the corresponding member of
1116 every specialization of the class template is a friend of the
1117 class granting friendship.
1119 For example, given a template friend declaration
1121 template <class T> friend void A<T>::f();
1123 the member function below is considered a friend
1125 template <> struct A<int> {
1126 void f();
1129 For this type of template friend, TEMPLATE_DEPTH below will be
1130 nonzero. To determine if DECL is a friend of FRIEND, we first
1131 check if the enclosing class is a specialization of another. */
1133 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1134 if (template_depth
1135 && DECL_CLASS_SCOPE_P (decl)
1136 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1137 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1139 /* Next, we check the members themselves. In order to handle
1140 a few tricky cases, such as when FRIEND_DECL's are
1142 template <class T> friend void A<T>::g(T t);
1143 template <class T> template <T t> friend void A<T>::h();
1145 and DECL's are
1147 void A<int>::g(int);
1148 template <int> void A<int>::h();
1150 we need to figure out ARGS, the template arguments from
1151 the context of DECL. This is required for template substitution
1152 of `T' in the function parameter of `g' and template parameter
1153 of `h' in the above examples. Here ARGS corresponds to `int'. */
1155 tree context = DECL_CONTEXT (decl);
1156 tree args = NULL_TREE;
1157 int current_depth = 0;
1159 while (current_depth < template_depth)
1161 if (CLASSTYPE_TEMPLATE_INFO (context))
1163 if (current_depth == 0)
1164 args = TYPE_TI_ARGS (context);
1165 else
1166 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1167 current_depth++;
1169 context = TYPE_CONTEXT (context);
1172 if (TREE_CODE (decl) == FUNCTION_DECL)
1174 bool is_template;
1175 tree friend_type;
1176 tree decl_type;
1177 tree friend_args_type;
1178 tree decl_args_type;
1180 /* Make sure that both DECL and FRIEND_DECL are templates or
1181 non-templates. */
1182 is_template = DECL_TEMPLATE_INFO (decl)
1183 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1184 if (need_template ^ is_template)
1185 return false;
1186 else if (is_template)
1188 /* If both are templates, check template parameter list. */
1189 tree friend_parms
1190 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1191 args, tf_none);
1192 if (!comp_template_parms
1193 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1194 friend_parms))
1195 return false;
1197 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1199 else
1200 decl_type = TREE_TYPE (decl);
1202 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1203 tf_none, NULL_TREE);
1204 if (friend_type == error_mark_node)
1205 return false;
1207 /* Check if return types match. */
1208 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1209 return false;
1211 /* Check if function parameter types match, ignoring the
1212 `this' parameter. */
1213 friend_args_type = TYPE_ARG_TYPES (friend_type);
1214 decl_args_type = TYPE_ARG_TYPES (decl_type);
1215 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1216 friend_args_type = TREE_CHAIN (friend_args_type);
1217 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1218 decl_args_type = TREE_CHAIN (decl_args_type);
1220 return compparms (decl_args_type, friend_args_type);
1222 else
1224 /* DECL is a TYPE_DECL */
1225 bool is_template;
1226 tree decl_type = TREE_TYPE (decl);
1228 /* Make sure that both DECL and FRIEND_DECL are templates or
1229 non-templates. */
1230 is_template
1231 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1232 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1234 if (need_template ^ is_template)
1235 return false;
1236 else if (is_template)
1238 tree friend_parms;
1239 /* If both are templates, check the name of the two
1240 TEMPLATE_DECL's first because is_friend didn't. */
1241 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1242 != DECL_NAME (friend_decl))
1243 return false;
1245 /* Now check template parameter list. */
1246 friend_parms
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1248 args, tf_none);
1249 return comp_template_parms
1250 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1251 friend_parms);
1253 else
1254 return (DECL_NAME (decl)
1255 == DECL_NAME (friend_decl));
1258 return false;
1261 /* Register the specialization SPEC as a specialization of TMPL with
1262 the indicated ARGS. IS_FRIEND indicates whether the specialization
1263 is actually just a friend declaration. Returns SPEC, or an
1264 equivalent prior declaration, if available. */
1266 static tree
1267 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1268 hashval_t hash)
1270 tree fn;
1271 spec_entry **slot = NULL;
1272 spec_entry elt;
1274 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1276 if (TREE_CODE (spec) == FUNCTION_DECL
1277 && uses_template_parms (DECL_TI_ARGS (spec)))
1278 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1279 register it; we want the corresponding TEMPLATE_DECL instead.
1280 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1281 the more obvious `uses_template_parms (spec)' to avoid problems
1282 with default function arguments. In particular, given
1283 something like this:
1285 template <class T> void f(T t1, T t = T())
1287 the default argument expression is not substituted for in an
1288 instantiation unless and until it is actually needed. */
1289 return spec;
1291 if (optimize_specialization_lookup_p (tmpl))
1292 /* We don't put these specializations in the hash table, but we might
1293 want to give an error about a mismatch. */
1294 fn = retrieve_specialization (tmpl, args, 0);
1295 else
1297 elt.tmpl = tmpl;
1298 elt.args = args;
1299 elt.spec = spec;
1301 if (hash == 0)
1302 hash = hash_specialization (&elt);
1304 slot = (spec_entry **)
1305 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1306 if (*slot)
1307 fn = (*slot)->spec;
1308 else
1309 fn = NULL_TREE;
1312 /* We can sometimes try to re-register a specialization that we've
1313 already got. In particular, regenerate_decl_from_template calls
1314 duplicate_decls which will update the specialization list. But,
1315 we'll still get called again here anyhow. It's more convenient
1316 to simply allow this than to try to prevent it. */
1317 if (fn == spec)
1318 return spec;
1319 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1321 if (DECL_TEMPLATE_INSTANTIATION (fn))
1323 if (DECL_ODR_USED (fn)
1324 || DECL_EXPLICIT_INSTANTIATION (fn))
1326 error ("specialization of %qD after instantiation",
1327 fn);
1328 return error_mark_node;
1330 else
1332 tree clone;
1333 /* This situation should occur only if the first
1334 specialization is an implicit instantiation, the
1335 second is an explicit specialization, and the
1336 implicit instantiation has not yet been used. That
1337 situation can occur if we have implicitly
1338 instantiated a member function and then specialized
1339 it later.
1341 We can also wind up here if a friend declaration that
1342 looked like an instantiation turns out to be a
1343 specialization:
1345 template <class T> void foo(T);
1346 class S { friend void foo<>(int) };
1347 template <> void foo(int);
1349 We transform the existing DECL in place so that any
1350 pointers to it become pointers to the updated
1351 declaration.
1353 If there was a definition for the template, but not
1354 for the specialization, we want this to look as if
1355 there were no definition, and vice versa. */
1356 DECL_INITIAL (fn) = NULL_TREE;
1357 duplicate_decls (spec, fn, is_friend);
1358 /* The call to duplicate_decls will have applied
1359 [temp.expl.spec]:
1361 An explicit specialization of a function template
1362 is inline only if it is explicitly declared to be,
1363 and independently of whether its function template
1366 to the primary function; now copy the inline bits to
1367 the various clones. */
1368 FOR_EACH_CLONE (clone, fn)
1370 DECL_DECLARED_INLINE_P (clone)
1371 = DECL_DECLARED_INLINE_P (fn);
1372 DECL_SOURCE_LOCATION (clone)
1373 = DECL_SOURCE_LOCATION (fn);
1375 check_specialization_namespace (fn);
1377 return fn;
1380 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1382 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1383 /* Dup decl failed, but this is a new definition. Set the
1384 line number so any errors match this new
1385 definition. */
1386 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1388 return fn;
1391 else if (fn)
1392 return duplicate_decls (spec, fn, is_friend);
1394 /* A specialization must be declared in the same namespace as the
1395 template it is specializing. */
1396 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1397 && !check_specialization_namespace (tmpl))
1398 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1400 if (!optimize_specialization_lookup_p (tmpl))
1402 gcc_assert (tmpl && args && spec);
1403 *slot = GGC_NEW (spec_entry);
1404 **slot = elt;
1405 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1406 && PRIMARY_TEMPLATE_P (tmpl)
1407 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1408 /* TMPL is a forward declaration of a template function; keep a list
1409 of all specializations in case we need to reassign them to a friend
1410 template later in tsubst_friend_function. */
1411 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1412 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1415 return spec;
1418 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1419 TMPL and ARGS members, ignores SPEC. */
1421 static int
1422 eq_specializations (const void *p1, const void *p2)
1424 const spec_entry *e1 = (const spec_entry *)p1;
1425 const spec_entry *e2 = (const spec_entry *)p2;
1427 return (e1->tmpl == e2->tmpl
1428 && comp_template_args (e1->args, e2->args));
1431 /* Returns a hash for a template TMPL and template arguments ARGS. */
1433 static hashval_t
1434 hash_tmpl_and_args (tree tmpl, tree args)
1436 hashval_t val = DECL_UID (tmpl);
1437 return iterative_hash_template_arg (args, val);
1440 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1441 ignoring SPEC. */
1443 static hashval_t
1444 hash_specialization (const void *p)
1446 const spec_entry *e = (const spec_entry *)p;
1447 return hash_tmpl_and_args (e->tmpl, e->args);
1450 /* Recursively calculate a hash value for a template argument ARG, for use
1451 in the hash tables of template specializations. */
1453 static hashval_t
1454 iterative_hash_template_arg (tree arg, hashval_t val)
1456 unsigned HOST_WIDE_INT i;
1457 enum tree_code code;
1458 char tclass;
1460 if (arg == NULL_TREE)
1461 return iterative_hash_object (arg, val);
1463 if (!TYPE_P (arg))
1464 STRIP_NOPS (arg);
1466 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1467 /* We can get one of these when re-hashing a previous entry in the middle
1468 of substituting into a pack expansion. Just look through it. */
1469 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1471 code = TREE_CODE (arg);
1472 tclass = TREE_CODE_CLASS (code);
1474 val = iterative_hash_object (code, val);
1476 switch (code)
1478 case ERROR_MARK:
1479 return val;
1481 case IDENTIFIER_NODE:
1482 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1484 case TREE_VEC:
1486 int i, len = TREE_VEC_LENGTH (arg);
1487 for (i = 0; i < len; ++i)
1488 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1489 return val;
1492 case TYPE_PACK_EXPANSION:
1493 case EXPR_PACK_EXPANSION:
1494 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1496 case TYPE_ARGUMENT_PACK:
1497 case NONTYPE_ARGUMENT_PACK:
1498 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1500 case TREE_LIST:
1501 for (; arg; arg = TREE_CHAIN (arg))
1502 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1503 return val;
1505 case OVERLOAD:
1506 for (; arg; arg = OVL_CHAIN (arg))
1507 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1508 return val;
1510 case CONSTRUCTOR:
1512 tree field, value;
1513 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1515 val = iterative_hash_template_arg (field, val);
1516 val = iterative_hash_template_arg (value, val);
1518 return val;
1521 case PARM_DECL:
1522 if (!DECL_ARTIFICIAL (arg))
1523 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1524 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1526 case TARGET_EXPR:
1527 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1529 case PTRMEM_CST:
1530 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1531 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1533 case TEMPLATE_PARM_INDEX:
1534 val = iterative_hash_template_arg
1535 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1536 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1537 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1539 case TRAIT_EXPR:
1540 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1541 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1542 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1544 case BASELINK:
1545 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1546 val);
1547 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1548 val);
1550 case MODOP_EXPR:
1551 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1552 code = TREE_CODE (TREE_OPERAND (arg, 1));
1553 val = iterative_hash_object (code, val);
1554 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1556 case ARRAY_TYPE:
1557 /* layout_type sets structural equality for arrays of
1558 incomplete type, so we can't rely on the canonical type
1559 for hashing. */
1560 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1561 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1563 default:
1564 switch (tclass)
1566 case tcc_type:
1567 if (TYPE_CANONICAL (arg))
1568 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1569 val);
1570 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1571 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1572 /* Otherwise just compare the types during lookup. */
1573 return val;
1575 case tcc_declaration:
1576 case tcc_constant:
1577 return iterative_hash_expr (arg, val);
1579 default:
1580 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1582 unsigned n = TREE_OPERAND_LENGTH (arg);
1583 for (i = 0; i < n; ++i)
1584 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1585 return val;
1589 gcc_unreachable ();
1590 return 0;
1593 /* Unregister the specialization SPEC as a specialization of TMPL.
1594 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1595 if the SPEC was listed as a specialization of TMPL.
1597 Note that SPEC has been ggc_freed, so we can't look inside it. */
1599 bool
1600 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1602 spec_entry **slot;
1603 spec_entry elt;
1605 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1606 elt.args = TI_ARGS (tinfo);
1607 elt.spec = NULL_TREE;
1609 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1610 if (*slot)
1612 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1613 gcc_assert (new_spec != NULL_TREE);
1614 (*slot)->spec = new_spec;
1615 return 1;
1618 return 0;
1621 /* Compare an entry in the local specializations hash table P1 (which
1622 is really a pointer to a TREE_LIST) with P2 (which is really a
1623 DECL). */
1625 static int
1626 eq_local_specializations (const void *p1, const void *p2)
1628 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1631 /* Hash P1, an entry in the local specializations table. */
1633 static hashval_t
1634 hash_local_specialization (const void* p1)
1636 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1639 /* Like register_specialization, but for local declarations. We are
1640 registering SPEC, an instantiation of TMPL. */
1642 static void
1643 register_local_specialization (tree spec, tree tmpl)
1645 void **slot;
1647 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1648 htab_hash_pointer (tmpl), INSERT);
1649 *slot = build_tree_list (spec, tmpl);
1652 /* TYPE is a class type. Returns true if TYPE is an explicitly
1653 specialized class. */
1655 bool
1656 explicit_class_specialization_p (tree type)
1658 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1659 return false;
1660 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1663 /* Print the list of functions at FNS, going through all the overloads
1664 for each element of the list. Alternatively, FNS can not be a
1665 TREE_LIST, in which case it will be printed together with all the
1666 overloads.
1668 MORE and *STR should respectively be FALSE and NULL when the function
1669 is called from the outside. They are used internally on recursive
1670 calls. print_candidates manages the two parameters and leaves NULL
1671 in *STR when it ends. */
1673 static void
1674 print_candidates_1 (tree fns, bool more, const char **str)
1676 tree fn, fn2;
1677 char *spaces = NULL;
1679 for (fn = fns; fn; fn = OVL_NEXT (fn))
1680 if (TREE_CODE (fn) == TREE_LIST)
1682 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1683 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1684 print_candidates_1 (TREE_VALUE (fn2),
1685 TREE_CHAIN (fn2) || more, str);
1687 else
1689 if (!*str)
1691 /* Pick the prefix string. */
1692 if (!more && !OVL_NEXT (fns))
1694 error ("candidate is: %+#D", OVL_CURRENT (fn));
1695 continue;
1698 *str = _("candidates are:");
1699 spaces = get_spaces (*str);
1701 error ("%s %+#D", *str, OVL_CURRENT (fn));
1702 *str = spaces ? spaces : *str;
1705 if (!more)
1707 free (spaces);
1708 *str = NULL;
1712 /* Print the list of candidate FNS in an error message. */
1714 void
1715 print_candidates (tree fns)
1717 const char *str = NULL;
1718 print_candidates_1 (fns, false, &str);
1719 gcc_assert (str == NULL);
1722 /* Returns the template (one of the functions given by TEMPLATE_ID)
1723 which can be specialized to match the indicated DECL with the
1724 explicit template args given in TEMPLATE_ID. The DECL may be
1725 NULL_TREE if none is available. In that case, the functions in
1726 TEMPLATE_ID are non-members.
1728 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1729 specialization of a member template.
1731 The TEMPLATE_COUNT is the number of references to qualifying
1732 template classes that appeared in the name of the function. See
1733 check_explicit_specialization for a more accurate description.
1735 TSK indicates what kind of template declaration (if any) is being
1736 declared. TSK_TEMPLATE indicates that the declaration given by
1737 DECL, though a FUNCTION_DECL, has template parameters, and is
1738 therefore a template function.
1740 The template args (those explicitly specified and those deduced)
1741 are output in a newly created vector *TARGS_OUT.
1743 If it is impossible to determine the result, an error message is
1744 issued. The error_mark_node is returned to indicate failure. */
1746 static tree
1747 determine_specialization (tree template_id,
1748 tree decl,
1749 tree* targs_out,
1750 int need_member_template,
1751 int template_count,
1752 tmpl_spec_kind tsk)
1754 tree fns;
1755 tree targs;
1756 tree explicit_targs;
1757 tree candidates = NULL_TREE;
1758 /* A TREE_LIST of templates of which DECL may be a specialization.
1759 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1760 corresponding TREE_PURPOSE is the set of template arguments that,
1761 when used to instantiate the template, would produce a function
1762 with the signature of DECL. */
1763 tree templates = NULL_TREE;
1764 int header_count;
1765 struct cp_binding_level *b;
1767 *targs_out = NULL_TREE;
1769 if (template_id == error_mark_node || decl == error_mark_node)
1770 return error_mark_node;
1772 fns = TREE_OPERAND (template_id, 0);
1773 explicit_targs = TREE_OPERAND (template_id, 1);
1775 if (fns == error_mark_node)
1776 return error_mark_node;
1778 /* Check for baselinks. */
1779 if (BASELINK_P (fns))
1780 fns = BASELINK_FUNCTIONS (fns);
1782 if (!is_overloaded_fn (fns))
1784 error ("%qD is not a function template", fns);
1785 return error_mark_node;
1788 /* Count the number of template headers specified for this
1789 specialization. */
1790 header_count = 0;
1791 for (b = current_binding_level;
1792 b->kind == sk_template_parms;
1793 b = b->level_chain)
1794 ++header_count;
1796 for (; fns; fns = OVL_NEXT (fns))
1798 tree fn = OVL_CURRENT (fns);
1800 if (TREE_CODE (fn) == TEMPLATE_DECL)
1802 tree decl_arg_types;
1803 tree fn_arg_types;
1805 /* In case of explicit specialization, we need to check if
1806 the number of template headers appearing in the specialization
1807 is correct. This is usually done in check_explicit_specialization,
1808 but the check done there cannot be exhaustive when specializing
1809 member functions. Consider the following code:
1811 template <> void A<int>::f(int);
1812 template <> template <> void A<int>::f(int);
1814 Assuming that A<int> is not itself an explicit specialization
1815 already, the first line specializes "f" which is a non-template
1816 member function, whilst the second line specializes "f" which
1817 is a template member function. So both lines are syntactically
1818 correct, and check_explicit_specialization does not reject
1819 them.
1821 Here, we can do better, as we are matching the specialization
1822 against the declarations. We count the number of template
1823 headers, and we check if they match TEMPLATE_COUNT + 1
1824 (TEMPLATE_COUNT is the number of qualifying template classes,
1825 plus there must be another header for the member template
1826 itself).
1828 Notice that if header_count is zero, this is not a
1829 specialization but rather a template instantiation, so there
1830 is no check we can perform here. */
1831 if (header_count && header_count != template_count + 1)
1832 continue;
1834 /* Check that the number of template arguments at the
1835 innermost level for DECL is the same as for FN. */
1836 if (current_binding_level->kind == sk_template_parms
1837 && !current_binding_level->explicit_spec_p
1838 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1839 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1840 (current_template_parms))))
1841 continue;
1843 /* DECL might be a specialization of FN. */
1844 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1845 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1847 /* For a non-static member function, we need to make sure
1848 that the const qualification is the same. Since
1849 get_bindings does not try to merge the "this" parameter,
1850 we must do the comparison explicitly. */
1851 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1852 && !same_type_p (TREE_VALUE (fn_arg_types),
1853 TREE_VALUE (decl_arg_types)))
1854 continue;
1856 /* Skip the "this" parameter and, for constructors of
1857 classes with virtual bases, the VTT parameter. A
1858 full specialization of a constructor will have a VTT
1859 parameter, but a template never will. */
1860 decl_arg_types
1861 = skip_artificial_parms_for (decl, decl_arg_types);
1862 fn_arg_types
1863 = skip_artificial_parms_for (fn, fn_arg_types);
1865 /* Check that the number of function parameters matches.
1866 For example,
1867 template <class T> void f(int i = 0);
1868 template <> void f<int>();
1869 The specialization f<int> is invalid but is not caught
1870 by get_bindings below. */
1871 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1872 continue;
1874 /* Function templates cannot be specializations; there are
1875 no partial specializations of functions. Therefore, if
1876 the type of DECL does not match FN, there is no
1877 match. */
1878 if (tsk == tsk_template)
1880 if (compparms (fn_arg_types, decl_arg_types))
1881 candidates = tree_cons (NULL_TREE, fn, candidates);
1882 continue;
1885 /* See whether this function might be a specialization of this
1886 template. */
1887 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1889 if (!targs)
1890 /* We cannot deduce template arguments that when used to
1891 specialize TMPL will produce DECL. */
1892 continue;
1894 /* Save this template, and the arguments deduced. */
1895 templates = tree_cons (targs, fn, templates);
1897 else if (need_member_template)
1898 /* FN is an ordinary member function, and we need a
1899 specialization of a member template. */
1901 else if (TREE_CODE (fn) != FUNCTION_DECL)
1902 /* We can get IDENTIFIER_NODEs here in certain erroneous
1903 cases. */
1905 else if (!DECL_FUNCTION_MEMBER_P (fn))
1906 /* This is just an ordinary non-member function. Nothing can
1907 be a specialization of that. */
1909 else if (DECL_ARTIFICIAL (fn))
1910 /* Cannot specialize functions that are created implicitly. */
1912 else
1914 tree decl_arg_types;
1916 /* This is an ordinary member function. However, since
1917 we're here, we can assume it's enclosing class is a
1918 template class. For example,
1920 template <typename T> struct S { void f(); };
1921 template <> void S<int>::f() {}
1923 Here, S<int>::f is a non-template, but S<int> is a
1924 template class. If FN has the same type as DECL, we
1925 might be in business. */
1927 if (!DECL_TEMPLATE_INFO (fn))
1928 /* Its enclosing class is an explicit specialization
1929 of a template class. This is not a candidate. */
1930 continue;
1932 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1933 TREE_TYPE (TREE_TYPE (fn))))
1934 /* The return types differ. */
1935 continue;
1937 /* Adjust the type of DECL in case FN is a static member. */
1938 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1939 if (DECL_STATIC_FUNCTION_P (fn)
1940 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1941 decl_arg_types = TREE_CHAIN (decl_arg_types);
1943 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1944 decl_arg_types))
1945 /* They match! */
1946 candidates = tree_cons (NULL_TREE, fn, candidates);
1950 if (templates && TREE_CHAIN (templates))
1952 /* We have:
1954 [temp.expl.spec]
1956 It is possible for a specialization with a given function
1957 signature to be instantiated from more than one function
1958 template. In such cases, explicit specification of the
1959 template arguments must be used to uniquely identify the
1960 function template specialization being specialized.
1962 Note that here, there's no suggestion that we're supposed to
1963 determine which of the candidate templates is most
1964 specialized. However, we, also have:
1966 [temp.func.order]
1968 Partial ordering of overloaded function template
1969 declarations is used in the following contexts to select
1970 the function template to which a function template
1971 specialization refers:
1973 -- when an explicit specialization refers to a function
1974 template.
1976 So, we do use the partial ordering rules, at least for now.
1977 This extension can only serve to make invalid programs valid,
1978 so it's safe. And, there is strong anecdotal evidence that
1979 the committee intended the partial ordering rules to apply;
1980 the EDG front end has that behavior, and John Spicer claims
1981 that the committee simply forgot to delete the wording in
1982 [temp.expl.spec]. */
1983 tree tmpl = most_specialized_instantiation (templates);
1984 if (tmpl != error_mark_node)
1986 templates = tmpl;
1987 TREE_CHAIN (templates) = NULL_TREE;
1991 if (templates == NULL_TREE && candidates == NULL_TREE)
1993 error ("template-id %qD for %q+D does not match any template "
1994 "declaration", template_id, decl);
1995 if (header_count && header_count != template_count + 1)
1996 inform (input_location, "saw %d %<template<>%>, need %d for "
1997 "specializing a member function template",
1998 header_count, template_count + 1);
1999 return error_mark_node;
2001 else if ((templates && TREE_CHAIN (templates))
2002 || (candidates && TREE_CHAIN (candidates))
2003 || (templates && candidates))
2005 error ("ambiguous template specialization %qD for %q+D",
2006 template_id, decl);
2007 candidates = chainon (candidates, templates);
2008 print_candidates (candidates);
2009 return error_mark_node;
2012 /* We have one, and exactly one, match. */
2013 if (candidates)
2015 tree fn = TREE_VALUE (candidates);
2016 *targs_out = copy_node (DECL_TI_ARGS (fn));
2017 /* DECL is a re-declaration or partial instantiation of a template
2018 function. */
2019 if (TREE_CODE (fn) == TEMPLATE_DECL)
2020 return fn;
2021 /* It was a specialization of an ordinary member function in a
2022 template class. */
2023 return DECL_TI_TEMPLATE (fn);
2026 /* It was a specialization of a template. */
2027 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2028 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2030 *targs_out = copy_node (targs);
2031 SET_TMPL_ARGS_LEVEL (*targs_out,
2032 TMPL_ARGS_DEPTH (*targs_out),
2033 TREE_PURPOSE (templates));
2035 else
2036 *targs_out = TREE_PURPOSE (templates);
2037 return TREE_VALUE (templates);
2040 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2041 but with the default argument values filled in from those in the
2042 TMPL_TYPES. */
2044 static tree
2045 copy_default_args_to_explicit_spec_1 (tree spec_types,
2046 tree tmpl_types)
2048 tree new_spec_types;
2050 if (!spec_types)
2051 return NULL_TREE;
2053 if (spec_types == void_list_node)
2054 return void_list_node;
2056 /* Substitute into the rest of the list. */
2057 new_spec_types =
2058 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2059 TREE_CHAIN (tmpl_types));
2061 /* Add the default argument for this parameter. */
2062 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2063 TREE_VALUE (spec_types),
2064 new_spec_types);
2067 /* DECL is an explicit specialization. Replicate default arguments
2068 from the template it specializes. (That way, code like:
2070 template <class T> void f(T = 3);
2071 template <> void f(double);
2072 void g () { f (); }
2074 works, as required.) An alternative approach would be to look up
2075 the correct default arguments at the call-site, but this approach
2076 is consistent with how implicit instantiations are handled. */
2078 static void
2079 copy_default_args_to_explicit_spec (tree decl)
2081 tree tmpl;
2082 tree spec_types;
2083 tree tmpl_types;
2084 tree new_spec_types;
2085 tree old_type;
2086 tree new_type;
2087 tree t;
2088 tree object_type = NULL_TREE;
2089 tree in_charge = NULL_TREE;
2090 tree vtt = NULL_TREE;
2092 /* See if there's anything we need to do. */
2093 tmpl = DECL_TI_TEMPLATE (decl);
2094 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2095 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2096 if (TREE_PURPOSE (t))
2097 break;
2098 if (!t)
2099 return;
2101 old_type = TREE_TYPE (decl);
2102 spec_types = TYPE_ARG_TYPES (old_type);
2104 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2106 /* Remove the this pointer, but remember the object's type for
2107 CV quals. */
2108 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2109 spec_types = TREE_CHAIN (spec_types);
2110 tmpl_types = TREE_CHAIN (tmpl_types);
2112 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2114 /* DECL may contain more parameters than TMPL due to the extra
2115 in-charge parameter in constructors and destructors. */
2116 in_charge = spec_types;
2117 spec_types = TREE_CHAIN (spec_types);
2119 if (DECL_HAS_VTT_PARM_P (decl))
2121 vtt = spec_types;
2122 spec_types = TREE_CHAIN (spec_types);
2126 /* Compute the merged default arguments. */
2127 new_spec_types =
2128 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2130 /* Compute the new FUNCTION_TYPE. */
2131 if (object_type)
2133 if (vtt)
2134 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2135 TREE_VALUE (vtt),
2136 new_spec_types);
2138 if (in_charge)
2139 /* Put the in-charge parameter back. */
2140 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2141 TREE_VALUE (in_charge),
2142 new_spec_types);
2144 new_type = build_method_type_directly (object_type,
2145 TREE_TYPE (old_type),
2146 new_spec_types);
2148 else
2149 new_type = build_function_type (TREE_TYPE (old_type),
2150 new_spec_types);
2151 new_type = cp_build_type_attribute_variant (new_type,
2152 TYPE_ATTRIBUTES (old_type));
2153 new_type = build_exception_variant (new_type,
2154 TYPE_RAISES_EXCEPTIONS (old_type));
2155 TREE_TYPE (decl) = new_type;
2158 /* Check to see if the function just declared, as indicated in
2159 DECLARATOR, and in DECL, is a specialization of a function
2160 template. We may also discover that the declaration is an explicit
2161 instantiation at this point.
2163 Returns DECL, or an equivalent declaration that should be used
2164 instead if all goes well. Issues an error message if something is
2165 amiss. Returns error_mark_node if the error is not easily
2166 recoverable.
2168 FLAGS is a bitmask consisting of the following flags:
2170 2: The function has a definition.
2171 4: The function is a friend.
2173 The TEMPLATE_COUNT is the number of references to qualifying
2174 template classes that appeared in the name of the function. For
2175 example, in
2177 template <class T> struct S { void f(); };
2178 void S<int>::f();
2180 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2181 classes are not counted in the TEMPLATE_COUNT, so that in
2183 template <class T> struct S {};
2184 template <> struct S<int> { void f(); }
2185 template <> void S<int>::f();
2187 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2188 invalid; there should be no template <>.)
2190 If the function is a specialization, it is marked as such via
2191 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2192 is set up correctly, and it is added to the list of specializations
2193 for that template. */
2195 tree
2196 check_explicit_specialization (tree declarator,
2197 tree decl,
2198 int template_count,
2199 int flags)
2201 int have_def = flags & 2;
2202 int is_friend = flags & 4;
2203 int specialization = 0;
2204 int explicit_instantiation = 0;
2205 int member_specialization = 0;
2206 tree ctype = DECL_CLASS_CONTEXT (decl);
2207 tree dname = DECL_NAME (decl);
2208 tmpl_spec_kind tsk;
2210 if (is_friend)
2212 if (!processing_specialization)
2213 tsk = tsk_none;
2214 else
2215 tsk = tsk_excessive_parms;
2217 else
2218 tsk = current_tmpl_spec_kind (template_count);
2220 switch (tsk)
2222 case tsk_none:
2223 if (processing_specialization)
2225 specialization = 1;
2226 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2228 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2230 if (is_friend)
2231 /* This could be something like:
2233 template <class T> void f(T);
2234 class S { friend void f<>(int); } */
2235 specialization = 1;
2236 else
2238 /* This case handles bogus declarations like template <>
2239 template <class T> void f<int>(); */
2241 error ("template-id %qD in declaration of primary template",
2242 declarator);
2243 return decl;
2246 break;
2248 case tsk_invalid_member_spec:
2249 /* The error has already been reported in
2250 check_specialization_scope. */
2251 return error_mark_node;
2253 case tsk_invalid_expl_inst:
2254 error ("template parameter list used in explicit instantiation");
2256 /* Fall through. */
2258 case tsk_expl_inst:
2259 if (have_def)
2260 error ("definition provided for explicit instantiation");
2262 explicit_instantiation = 1;
2263 break;
2265 case tsk_excessive_parms:
2266 case tsk_insufficient_parms:
2267 if (tsk == tsk_excessive_parms)
2268 error ("too many template parameter lists in declaration of %qD",
2269 decl);
2270 else if (template_header_count)
2271 error("too few template parameter lists in declaration of %qD", decl);
2272 else
2273 error("explicit specialization of %qD must be introduced by "
2274 "%<template <>%>", decl);
2276 /* Fall through. */
2277 case tsk_expl_spec:
2278 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2279 if (ctype)
2280 member_specialization = 1;
2281 else
2282 specialization = 1;
2283 break;
2285 case tsk_template:
2286 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2288 /* This case handles bogus declarations like template <>
2289 template <class T> void f<int>(); */
2291 if (uses_template_parms (declarator))
2292 error ("function template partial specialization %qD "
2293 "is not allowed", declarator);
2294 else
2295 error ("template-id %qD in declaration of primary template",
2296 declarator);
2297 return decl;
2300 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2301 /* This is a specialization of a member template, without
2302 specialization the containing class. Something like:
2304 template <class T> struct S {
2305 template <class U> void f (U);
2307 template <> template <class U> void S<int>::f(U) {}
2309 That's a specialization -- but of the entire template. */
2310 specialization = 1;
2311 break;
2313 default:
2314 gcc_unreachable ();
2317 if (specialization || member_specialization)
2319 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2320 for (; t; t = TREE_CHAIN (t))
2321 if (TREE_PURPOSE (t))
2323 permerror (input_location,
2324 "default argument specified in explicit specialization");
2325 break;
2329 if (specialization || member_specialization || explicit_instantiation)
2331 tree tmpl = NULL_TREE;
2332 tree targs = NULL_TREE;
2334 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2335 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2337 tree fns;
2339 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2340 if (ctype)
2341 fns = dname;
2342 else
2344 /* If there is no class context, the explicit instantiation
2345 must be at namespace scope. */
2346 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2348 /* Find the namespace binding, using the declaration
2349 context. */
2350 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2351 false, true);
2352 if (fns == error_mark_node || !is_overloaded_fn (fns))
2354 error ("%qD is not a template function", dname);
2355 fns = error_mark_node;
2357 else
2359 tree fn = OVL_CURRENT (fns);
2360 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2361 CP_DECL_CONTEXT (fn)))
2362 error ("%qD is not declared in %qD",
2363 decl, current_namespace);
2367 declarator = lookup_template_function (fns, NULL_TREE);
2370 if (declarator == error_mark_node)
2371 return error_mark_node;
2373 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2375 if (!explicit_instantiation)
2376 /* A specialization in class scope. This is invalid,
2377 but the error will already have been flagged by
2378 check_specialization_scope. */
2379 return error_mark_node;
2380 else
2382 /* It's not valid to write an explicit instantiation in
2383 class scope, e.g.:
2385 class C { template void f(); }
2387 This case is caught by the parser. However, on
2388 something like:
2390 template class C { void f(); };
2392 (which is invalid) we can get here. The error will be
2393 issued later. */
2397 return decl;
2399 else if (ctype != NULL_TREE
2400 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2401 IDENTIFIER_NODE))
2403 /* Find the list of functions in ctype that have the same
2404 name as the declared function. */
2405 tree name = TREE_OPERAND (declarator, 0);
2406 tree fns = NULL_TREE;
2407 int idx;
2409 if (constructor_name_p (name, ctype))
2411 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2413 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2414 : !CLASSTYPE_DESTRUCTORS (ctype))
2416 /* From [temp.expl.spec]:
2418 If such an explicit specialization for the member
2419 of a class template names an implicitly-declared
2420 special member function (clause _special_), the
2421 program is ill-formed.
2423 Similar language is found in [temp.explicit]. */
2424 error ("specialization of implicitly-declared special member function");
2425 return error_mark_node;
2428 name = is_constructor ? ctor_identifier : dtor_identifier;
2431 if (!DECL_CONV_FN_P (decl))
2433 idx = lookup_fnfields_1 (ctype, name);
2434 if (idx >= 0)
2435 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2437 else
2439 VEC(tree,gc) *methods;
2440 tree ovl;
2442 /* For a type-conversion operator, we cannot do a
2443 name-based lookup. We might be looking for `operator
2444 int' which will be a specialization of `operator T'.
2445 So, we find *all* the conversion operators, and then
2446 select from them. */
2447 fns = NULL_TREE;
2449 methods = CLASSTYPE_METHOD_VEC (ctype);
2450 if (methods)
2451 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2452 VEC_iterate (tree, methods, idx, ovl);
2453 ++idx)
2455 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2456 /* There are no more conversion functions. */
2457 break;
2459 /* Glue all these conversion functions together
2460 with those we already have. */
2461 for (; ovl; ovl = OVL_NEXT (ovl))
2462 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2466 if (fns == NULL_TREE)
2468 error ("no member function %qD declared in %qT", name, ctype);
2469 return error_mark_node;
2471 else
2472 TREE_OPERAND (declarator, 0) = fns;
2475 /* Figure out what exactly is being specialized at this point.
2476 Note that for an explicit instantiation, even one for a
2477 member function, we cannot tell apriori whether the
2478 instantiation is for a member template, or just a member
2479 function of a template class. Even if a member template is
2480 being instantiated, the member template arguments may be
2481 elided if they can be deduced from the rest of the
2482 declaration. */
2483 tmpl = determine_specialization (declarator, decl,
2484 &targs,
2485 member_specialization,
2486 template_count,
2487 tsk);
2489 if (!tmpl || tmpl == error_mark_node)
2490 /* We couldn't figure out what this declaration was
2491 specializing. */
2492 return error_mark_node;
2493 else
2495 tree gen_tmpl = most_general_template (tmpl);
2497 if (explicit_instantiation)
2499 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2500 is done by do_decl_instantiation later. */
2502 int arg_depth = TMPL_ARGS_DEPTH (targs);
2503 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2505 if (arg_depth > parm_depth)
2507 /* If TMPL is not the most general template (for
2508 example, if TMPL is a friend template that is
2509 injected into namespace scope), then there will
2510 be too many levels of TARGS. Remove some of them
2511 here. */
2512 int i;
2513 tree new_targs;
2515 new_targs = make_tree_vec (parm_depth);
2516 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2517 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2518 = TREE_VEC_ELT (targs, i);
2519 targs = new_targs;
2522 return instantiate_template (tmpl, targs, tf_error);
2525 /* If we thought that the DECL was a member function, but it
2526 turns out to be specializing a static member function,
2527 make DECL a static member function as well. */
2528 if (DECL_STATIC_FUNCTION_P (tmpl)
2529 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2530 revert_static_member_fn (decl);
2532 /* If this is a specialization of a member template of a
2533 template class, we want to return the TEMPLATE_DECL, not
2534 the specialization of it. */
2535 if (tsk == tsk_template)
2537 tree result = DECL_TEMPLATE_RESULT (tmpl);
2538 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2539 DECL_INITIAL (result) = NULL_TREE;
2540 if (have_def)
2542 tree parm;
2543 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2544 DECL_SOURCE_LOCATION (result)
2545 = DECL_SOURCE_LOCATION (decl);
2546 /* We want to use the argument list specified in the
2547 definition, not in the original declaration. */
2548 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2549 for (parm = DECL_ARGUMENTS (result); parm;
2550 parm = TREE_CHAIN (parm))
2551 DECL_CONTEXT (parm) = result;
2553 return register_specialization (tmpl, gen_tmpl, targs,
2554 is_friend, 0);
2557 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2558 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2560 /* Inherit default function arguments from the template
2561 DECL is specializing. */
2562 copy_default_args_to_explicit_spec (decl);
2564 /* This specialization has the same protection as the
2565 template it specializes. */
2566 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2567 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2569 /* 7.1.1-1 [dcl.stc]
2571 A storage-class-specifier shall not be specified in an
2572 explicit specialization...
2574 The parser rejects these, so unless action is taken here,
2575 explicit function specializations will always appear with
2576 global linkage.
2578 The action recommended by the C++ CWG in response to C++
2579 defect report 605 is to make the storage class and linkage
2580 of the explicit specialization match the templated function:
2582 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2584 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2586 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2587 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2589 /* This specialization has the same linkage and visibility as
2590 the function template it specializes. */
2591 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2592 if (! TREE_PUBLIC (decl))
2594 DECL_INTERFACE_KNOWN (decl) = 1;
2595 DECL_NOT_REALLY_EXTERN (decl) = 1;
2597 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2598 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2600 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2601 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2605 /* If DECL is a friend declaration, declared using an
2606 unqualified name, the namespace associated with DECL may
2607 have been set incorrectly. For example, in:
2609 template <typename T> void f(T);
2610 namespace N {
2611 struct S { friend void f<int>(int); }
2614 we will have set the DECL_CONTEXT for the friend
2615 declaration to N, rather than to the global namespace. */
2616 if (DECL_NAMESPACE_SCOPE_P (decl))
2617 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2619 if (is_friend && !have_def)
2620 /* This is not really a declaration of a specialization.
2621 It's just the name of an instantiation. But, it's not
2622 a request for an instantiation, either. */
2623 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2624 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2625 /* This is indeed a specialization. In case of constructors
2626 and destructors, we need in-charge and not-in-charge
2627 versions in V3 ABI. */
2628 clone_function_decl (decl, /*update_method_vec_p=*/0);
2630 /* Register this specialization so that we can find it
2631 again. */
2632 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2636 return decl;
2639 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2640 parameters. These are represented in the same format used for
2641 DECL_TEMPLATE_PARMS. */
2644 comp_template_parms (const_tree parms1, const_tree parms2)
2646 const_tree p1;
2647 const_tree p2;
2649 if (parms1 == parms2)
2650 return 1;
2652 for (p1 = parms1, p2 = parms2;
2653 p1 != NULL_TREE && p2 != NULL_TREE;
2654 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2656 tree t1 = TREE_VALUE (p1);
2657 tree t2 = TREE_VALUE (p2);
2658 int i;
2660 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2661 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2663 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2664 return 0;
2666 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2668 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2669 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2671 /* If either of the template parameters are invalid, assume
2672 they match for the sake of error recovery. */
2673 if (parm1 == error_mark_node || parm2 == error_mark_node)
2674 return 1;
2676 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2677 return 0;
2679 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2680 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2681 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2682 continue;
2683 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2684 return 0;
2688 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2689 /* One set of parameters has more parameters lists than the
2690 other. */
2691 return 0;
2693 return 1;
2696 /* Determine whether PARM is a parameter pack. */
2698 bool
2699 template_parameter_pack_p (const_tree parm)
2701 /* Determine if we have a non-type template parameter pack. */
2702 if (TREE_CODE (parm) == PARM_DECL)
2703 return (DECL_TEMPLATE_PARM_P (parm)
2704 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2706 /* If this is a list of template parameters, we could get a
2707 TYPE_DECL or a TEMPLATE_DECL. */
2708 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2709 parm = TREE_TYPE (parm);
2711 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2712 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2713 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2716 /* Determine if T is a function parameter pack. */
2718 bool
2719 function_parameter_pack_p (const_tree t)
2721 if (t && TREE_CODE (t) == PARM_DECL)
2722 return FUNCTION_PARAMETER_PACK_P (t);
2723 return false;
2726 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2727 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2729 tree
2730 get_function_template_decl (const_tree primary_func_tmpl_inst)
2732 if (! primary_func_tmpl_inst
2733 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2734 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2735 return NULL;
2737 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2740 /* Return true iff the function parameter PARAM_DECL was expanded
2741 from the function parameter pack PACK. */
2743 bool
2744 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2746 if (DECL_ARTIFICIAL (param_decl)
2747 || !function_parameter_pack_p (pack))
2748 return false;
2750 /* The parameter pack and its pack arguments have the same
2751 DECL_PARM_INDEX. */
2752 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2755 /* Determine whether ARGS describes a variadic template args list,
2756 i.e., one that is terminated by a template argument pack. */
2758 static bool
2759 template_args_variadic_p (tree args)
2761 int nargs;
2762 tree last_parm;
2764 if (args == NULL_TREE)
2765 return false;
2767 args = INNERMOST_TEMPLATE_ARGS (args);
2768 nargs = TREE_VEC_LENGTH (args);
2770 if (nargs == 0)
2771 return false;
2773 last_parm = TREE_VEC_ELT (args, nargs - 1);
2775 return ARGUMENT_PACK_P (last_parm);
2778 /* Generate a new name for the parameter pack name NAME (an
2779 IDENTIFIER_NODE) that incorporates its */
2781 static tree
2782 make_ith_pack_parameter_name (tree name, int i)
2784 /* Munge the name to include the parameter index. */
2785 #define NUMBUF_LEN 128
2786 char numbuf[NUMBUF_LEN];
2787 char* newname;
2788 int newname_len;
2790 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2791 newname_len = IDENTIFIER_LENGTH (name)
2792 + strlen (numbuf) + 2;
2793 newname = (char*)alloca (newname_len);
2794 snprintf (newname, newname_len,
2795 "%s#%i", IDENTIFIER_POINTER (name), i);
2796 return get_identifier (newname);
2799 /* Return true if T is a primary function
2800 or class template instantiation. */
2802 bool
2803 primary_template_instantiation_p (const_tree t)
2805 if (!t)
2806 return false;
2808 if (TREE_CODE (t) == FUNCTION_DECL)
2809 return DECL_LANG_SPECIFIC (t)
2810 && DECL_TEMPLATE_INSTANTIATION (t)
2811 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2812 else if (CLASS_TYPE_P (t))
2813 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2814 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2815 return false;
2818 /* Return true if PARM is a template template parameter. */
2820 bool
2821 template_template_parameter_p (const_tree parm)
2823 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2826 /* Return the template parameters of T if T is a
2827 primary template instantiation, NULL otherwise. */
2829 tree
2830 get_primary_template_innermost_parameters (const_tree t)
2832 tree parms = NULL, template_info = NULL;
2834 if ((template_info = get_template_info (t))
2835 && primary_template_instantiation_p (t))
2836 parms = INNERMOST_TEMPLATE_PARMS
2837 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2839 return parms;
2842 /* Returns the template arguments of T if T is a template instantiation,
2843 NULL otherwise. */
2845 tree
2846 get_template_innermost_arguments (const_tree t)
2848 tree args = NULL, template_info = NULL;
2850 if ((template_info = get_template_info (t))
2851 && TI_ARGS (template_info))
2852 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2854 return args;
2857 /* Return the argument pack elements of T if T is a template argument pack,
2858 NULL otherwise. */
2860 tree
2861 get_template_argument_pack_elems (const_tree t)
2863 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2864 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2865 return NULL;
2867 return ARGUMENT_PACK_ARGS (t);
2870 /* Structure used to track the progress of find_parameter_packs_r. */
2871 struct find_parameter_pack_data
2873 /* TREE_LIST that will contain all of the parameter packs found by
2874 the traversal. */
2875 tree* parameter_packs;
2877 /* Set of AST nodes that have been visited by the traversal. */
2878 struct pointer_set_t *visited;
2881 /* Identifies all of the argument packs that occur in a template
2882 argument and appends them to the TREE_LIST inside DATA, which is a
2883 find_parameter_pack_data structure. This is a subroutine of
2884 make_pack_expansion and uses_parameter_packs. */
2885 static tree
2886 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2888 tree t = *tp;
2889 struct find_parameter_pack_data* ppd =
2890 (struct find_parameter_pack_data*)data;
2891 bool parameter_pack_p = false;
2893 /* Identify whether this is a parameter pack or not. */
2894 switch (TREE_CODE (t))
2896 case TEMPLATE_PARM_INDEX:
2897 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2898 parameter_pack_p = true;
2899 break;
2901 case TEMPLATE_TYPE_PARM:
2902 case TEMPLATE_TEMPLATE_PARM:
2903 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2904 parameter_pack_p = true;
2905 break;
2907 case PARM_DECL:
2908 if (FUNCTION_PARAMETER_PACK_P (t))
2910 /* We don't want to walk into the type of a PARM_DECL,
2911 because we don't want to see the type parameter pack. */
2912 *walk_subtrees = 0;
2913 parameter_pack_p = true;
2915 break;
2917 default:
2918 /* Not a parameter pack. */
2919 break;
2922 if (parameter_pack_p)
2924 /* Add this parameter pack to the list. */
2925 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2928 if (TYPE_P (t))
2929 cp_walk_tree (&TYPE_CONTEXT (t),
2930 &find_parameter_packs_r, ppd, ppd->visited);
2932 /* This switch statement will return immediately if we don't find a
2933 parameter pack. */
2934 switch (TREE_CODE (t))
2936 case TEMPLATE_PARM_INDEX:
2937 return NULL_TREE;
2939 case BOUND_TEMPLATE_TEMPLATE_PARM:
2940 /* Check the template itself. */
2941 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2942 &find_parameter_packs_r, ppd, ppd->visited);
2943 /* Check the template arguments. */
2944 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2945 ppd->visited);
2946 *walk_subtrees = 0;
2947 return NULL_TREE;
2949 case TEMPLATE_TYPE_PARM:
2950 case TEMPLATE_TEMPLATE_PARM:
2951 return NULL_TREE;
2953 case PARM_DECL:
2954 return NULL_TREE;
2956 case RECORD_TYPE:
2957 if (TYPE_PTRMEMFUNC_P (t))
2958 return NULL_TREE;
2959 /* Fall through. */
2961 case UNION_TYPE:
2962 case ENUMERAL_TYPE:
2963 if (TYPE_TEMPLATE_INFO (t))
2964 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
2965 &find_parameter_packs_r, ppd, ppd->visited);
2967 *walk_subtrees = 0;
2968 return NULL_TREE;
2970 case TEMPLATE_DECL:
2971 cp_walk_tree (&TREE_TYPE (t),
2972 &find_parameter_packs_r, ppd, ppd->visited);
2973 return NULL_TREE;
2975 case TYPENAME_TYPE:
2976 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2977 ppd, ppd->visited);
2978 *walk_subtrees = 0;
2979 return NULL_TREE;
2981 case TYPE_PACK_EXPANSION:
2982 case EXPR_PACK_EXPANSION:
2983 *walk_subtrees = 0;
2984 return NULL_TREE;
2986 case INTEGER_TYPE:
2987 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2988 ppd, ppd->visited);
2989 *walk_subtrees = 0;
2990 return NULL_TREE;
2992 case IDENTIFIER_NODE:
2993 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2994 ppd->visited);
2995 *walk_subtrees = 0;
2996 return NULL_TREE;
2998 default:
2999 return NULL_TREE;
3002 return NULL_TREE;
3005 /* Determines if the expression or type T uses any parameter packs. */
3006 bool
3007 uses_parameter_packs (tree t)
3009 tree parameter_packs = NULL_TREE;
3010 struct find_parameter_pack_data ppd;
3011 ppd.parameter_packs = &parameter_packs;
3012 ppd.visited = pointer_set_create ();
3013 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3014 pointer_set_destroy (ppd.visited);
3015 return parameter_packs != NULL_TREE;
3018 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3019 representation a base-class initializer into a parameter pack
3020 expansion. If all goes well, the resulting node will be an
3021 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3022 respectively. */
3023 tree
3024 make_pack_expansion (tree arg)
3026 tree result;
3027 tree parameter_packs = NULL_TREE;
3028 bool for_types = false;
3029 struct find_parameter_pack_data ppd;
3031 if (!arg || arg == error_mark_node)
3032 return arg;
3034 if (TREE_CODE (arg) == TREE_LIST)
3036 /* The only time we will see a TREE_LIST here is for a base
3037 class initializer. In this case, the TREE_PURPOSE will be a
3038 _TYPE node (representing the base class expansion we're
3039 initializing) and the TREE_VALUE will be a TREE_LIST
3040 containing the initialization arguments.
3042 The resulting expansion looks somewhat different from most
3043 expansions. Rather than returning just one _EXPANSION, we
3044 return a TREE_LIST whose TREE_PURPOSE is a
3045 TYPE_PACK_EXPANSION containing the bases that will be
3046 initialized. The TREE_VALUE will be identical to the
3047 original TREE_VALUE, which is a list of arguments that will
3048 be passed to each base. We do not introduce any new pack
3049 expansion nodes into the TREE_VALUE (although it is possible
3050 that some already exist), because the TREE_PURPOSE and
3051 TREE_VALUE all need to be expanded together with the same
3052 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3053 resulting TREE_PURPOSE will mention the parameter packs in
3054 both the bases and the arguments to the bases. */
3055 tree purpose;
3056 tree value;
3057 tree parameter_packs = NULL_TREE;
3059 /* Determine which parameter packs will be used by the base
3060 class expansion. */
3061 ppd.visited = pointer_set_create ();
3062 ppd.parameter_packs = &parameter_packs;
3063 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3064 &ppd, ppd.visited);
3066 if (parameter_packs == NULL_TREE)
3068 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3069 pointer_set_destroy (ppd.visited);
3070 return error_mark_node;
3073 if (TREE_VALUE (arg) != void_type_node)
3075 /* Collect the sets of parameter packs used in each of the
3076 initialization arguments. */
3077 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3079 /* Determine which parameter packs will be expanded in this
3080 argument. */
3081 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3082 &ppd, ppd.visited);
3086 pointer_set_destroy (ppd.visited);
3088 /* Create the pack expansion type for the base type. */
3089 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3090 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3091 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3093 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3094 they will rarely be compared to anything. */
3095 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3097 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3100 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3101 for_types = true;
3103 /* Build the PACK_EXPANSION_* node. */
3104 result = for_types
3105 ? cxx_make_type (TYPE_PACK_EXPANSION)
3106 : make_node (EXPR_PACK_EXPANSION);
3107 SET_PACK_EXPANSION_PATTERN (result, arg);
3108 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3110 /* Propagate type and const-expression information. */
3111 TREE_TYPE (result) = TREE_TYPE (arg);
3112 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3114 else
3115 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3116 they will rarely be compared to anything. */
3117 SET_TYPE_STRUCTURAL_EQUALITY (result);
3119 /* Determine which parameter packs will be expanded. */
3120 ppd.parameter_packs = &parameter_packs;
3121 ppd.visited = pointer_set_create ();
3122 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3123 pointer_set_destroy (ppd.visited);
3125 /* Make sure we found some parameter packs. */
3126 if (parameter_packs == NULL_TREE)
3128 if (TYPE_P (arg))
3129 error ("expansion pattern %<%T%> contains no argument packs", arg);
3130 else
3131 error ("expansion pattern %<%E%> contains no argument packs", arg);
3132 return error_mark_node;
3134 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3136 return result;
3139 /* Checks T for any "bare" parameter packs, which have not yet been
3140 expanded, and issues an error if any are found. This operation can
3141 only be done on full expressions or types (e.g., an expression
3142 statement, "if" condition, etc.), because we could have expressions like:
3144 foo(f(g(h(args)))...)
3146 where "args" is a parameter pack. check_for_bare_parameter_packs
3147 should not be called for the subexpressions args, h(args),
3148 g(h(args)), or f(g(h(args))), because we would produce erroneous
3149 error messages.
3151 Returns TRUE and emits an error if there were bare parameter packs,
3152 returns FALSE otherwise. */
3153 bool
3154 check_for_bare_parameter_packs (tree t)
3156 tree parameter_packs = NULL_TREE;
3157 struct find_parameter_pack_data ppd;
3159 if (!processing_template_decl || !t || t == error_mark_node)
3160 return false;
3162 if (TREE_CODE (t) == TYPE_DECL)
3163 t = TREE_TYPE (t);
3165 ppd.parameter_packs = &parameter_packs;
3166 ppd.visited = pointer_set_create ();
3167 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3168 pointer_set_destroy (ppd.visited);
3170 if (parameter_packs)
3172 error ("parameter packs not expanded with %<...%>:");
3173 while (parameter_packs)
3175 tree pack = TREE_VALUE (parameter_packs);
3176 tree name = NULL_TREE;
3178 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3179 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3180 name = TYPE_NAME (pack);
3181 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3182 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3183 else
3184 name = DECL_NAME (pack);
3186 if (name)
3187 inform (input_location, " %qD", name);
3188 else
3189 inform (input_location, " <anonymous>");
3191 parameter_packs = TREE_CHAIN (parameter_packs);
3194 return true;
3197 return false;
3200 /* Expand any parameter packs that occur in the template arguments in
3201 ARGS. */
3202 tree
3203 expand_template_argument_pack (tree args)
3205 tree result_args = NULL_TREE;
3206 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3207 int num_result_args = -1;
3209 /* First, determine if we need to expand anything, and the number of
3210 slots we'll need. */
3211 for (in_arg = 0; in_arg < nargs; ++in_arg)
3213 tree arg = TREE_VEC_ELT (args, in_arg);
3214 if (arg == NULL_TREE)
3215 return args;
3216 if (ARGUMENT_PACK_P (arg))
3218 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3219 if (num_result_args < 0)
3220 num_result_args = in_arg + num_packed;
3221 else
3222 num_result_args += num_packed;
3224 else
3226 if (num_result_args >= 0)
3227 num_result_args++;
3231 /* If no expansion is necessary, we're done. */
3232 if (num_result_args < 0)
3233 return args;
3235 /* Expand arguments. */
3236 result_args = make_tree_vec (num_result_args);
3237 for (in_arg = 0; in_arg < nargs; ++in_arg)
3239 tree arg = TREE_VEC_ELT (args, in_arg);
3240 if (ARGUMENT_PACK_P (arg))
3242 tree packed = ARGUMENT_PACK_ARGS (arg);
3243 int i, num_packed = TREE_VEC_LENGTH (packed);
3244 for (i = 0; i < num_packed; ++i, ++out_arg)
3245 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3247 else
3249 TREE_VEC_ELT (result_args, out_arg) = arg;
3250 ++out_arg;
3254 return result_args;
3257 /* Checks if DECL shadows a template parameter.
3259 [temp.local]: A template-parameter shall not be redeclared within its
3260 scope (including nested scopes).
3262 Emits an error and returns TRUE if the DECL shadows a parameter,
3263 returns FALSE otherwise. */
3265 bool
3266 check_template_shadow (tree decl)
3268 tree olddecl;
3270 /* If we're not in a template, we can't possibly shadow a template
3271 parameter. */
3272 if (!current_template_parms)
3273 return true;
3275 /* Figure out what we're shadowing. */
3276 if (TREE_CODE (decl) == OVERLOAD)
3277 decl = OVL_CURRENT (decl);
3278 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3280 /* If there's no previous binding for this name, we're not shadowing
3281 anything, let alone a template parameter. */
3282 if (!olddecl)
3283 return true;
3285 /* If we're not shadowing a template parameter, we're done. Note
3286 that OLDDECL might be an OVERLOAD (or perhaps even an
3287 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3288 node. */
3289 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3290 return true;
3292 /* We check for decl != olddecl to avoid bogus errors for using a
3293 name inside a class. We check TPFI to avoid duplicate errors for
3294 inline member templates. */
3295 if (decl == olddecl
3296 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3297 return true;
3299 error ("declaration of %q+#D", decl);
3300 error (" shadows template parm %q+#D", olddecl);
3301 return false;
3304 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3305 ORIG_LEVEL, DECL, and TYPE. */
3307 static tree
3308 build_template_parm_index (int index,
3309 int level,
3310 int orig_level,
3311 tree decl,
3312 tree type)
3314 tree t = make_node (TEMPLATE_PARM_INDEX);
3315 TEMPLATE_PARM_IDX (t) = index;
3316 TEMPLATE_PARM_LEVEL (t) = level;
3317 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3318 TEMPLATE_PARM_DECL (t) = decl;
3319 TREE_TYPE (t) = type;
3320 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3321 TREE_READONLY (t) = TREE_READONLY (decl);
3323 return t;
3326 /* Find the canonical type parameter for the given template type
3327 parameter. Returns the canonical type parameter, which may be TYPE
3328 if no such parameter existed. */
3329 static tree
3330 canonical_type_parameter (tree type)
3332 tree list;
3333 int idx = TEMPLATE_TYPE_IDX (type);
3334 if (!canonical_template_parms)
3335 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3337 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3338 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3340 list = VEC_index (tree, canonical_template_parms, idx);
3341 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3342 list = TREE_CHAIN (list);
3344 if (list)
3345 return TREE_VALUE (list);
3346 else
3348 VEC_replace(tree, canonical_template_parms, idx,
3349 tree_cons (NULL_TREE, type,
3350 VEC_index (tree, canonical_template_parms, idx)));
3351 return type;
3355 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3356 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3357 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3358 new one is created. */
3360 static tree
3361 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3362 tsubst_flags_t complain)
3364 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3365 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3366 != TEMPLATE_PARM_LEVEL (index) - levels)
3367 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3369 tree orig_decl = TEMPLATE_PARM_DECL (index);
3370 tree decl, t;
3372 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3373 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3374 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3375 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3376 DECL_ARTIFICIAL (decl) = 1;
3377 SET_DECL_TEMPLATE_PARM_P (decl);
3379 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3380 TEMPLATE_PARM_LEVEL (index) - levels,
3381 TEMPLATE_PARM_ORIG_LEVEL (index),
3382 decl, type);
3383 TEMPLATE_PARM_DESCENDANTS (index) = t;
3384 TEMPLATE_PARM_PARAMETER_PACK (t)
3385 = TEMPLATE_PARM_PARAMETER_PACK (index);
3387 /* Template template parameters need this. */
3388 if (TREE_CODE (decl) == TEMPLATE_DECL)
3389 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3390 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3391 args, complain);
3394 return TEMPLATE_PARM_DESCENDANTS (index);
3397 /* Process information from new template parameter PARM and append it to the
3398 LIST being built. This new parameter is a non-type parameter iff
3399 IS_NON_TYPE is true. This new parameter is a parameter
3400 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3401 PARM_LOC. */
3403 tree
3404 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3405 bool is_parameter_pack)
3407 tree decl = 0;
3408 tree defval;
3409 tree err_parm_list;
3410 int idx = 0;
3412 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3413 defval = TREE_PURPOSE (parm);
3415 if (list)
3417 tree p = tree_last (list);
3419 if (p && TREE_VALUE (p) != error_mark_node)
3421 p = TREE_VALUE (p);
3422 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3423 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3424 else
3425 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3428 ++idx;
3430 else
3431 idx = 0;
3433 if (is_non_type)
3435 parm = TREE_VALUE (parm);
3437 SET_DECL_TEMPLATE_PARM_P (parm);
3439 if (TREE_TYPE (parm) == error_mark_node)
3441 err_parm_list = build_tree_list (defval, parm);
3442 TREE_VALUE (err_parm_list) = error_mark_node;
3443 return chainon (list, err_parm_list);
3445 else
3447 /* [temp.param]
3449 The top-level cv-qualifiers on the template-parameter are
3450 ignored when determining its type. */
3451 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3452 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3454 err_parm_list = build_tree_list (defval, parm);
3455 TREE_VALUE (err_parm_list) = error_mark_node;
3456 return chainon (list, err_parm_list);
3459 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3461 /* This template parameter is not a parameter pack, but it
3462 should be. Complain about "bare" parameter packs. */
3463 check_for_bare_parameter_packs (TREE_TYPE (parm));
3465 /* Recover by calling this a parameter pack. */
3466 is_parameter_pack = true;
3470 /* A template parameter is not modifiable. */
3471 TREE_CONSTANT (parm) = 1;
3472 TREE_READONLY (parm) = 1;
3473 decl = build_decl (parm_loc,
3474 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3475 TREE_CONSTANT (decl) = 1;
3476 TREE_READONLY (decl) = 1;
3477 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3478 = build_template_parm_index (idx, processing_template_decl,
3479 processing_template_decl,
3480 decl, TREE_TYPE (parm));
3482 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3483 = is_parameter_pack;
3485 else
3487 tree t;
3488 parm = TREE_VALUE (TREE_VALUE (parm));
3490 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3492 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3493 /* This is for distinguishing between real templates and template
3494 template parameters */
3495 TREE_TYPE (parm) = t;
3496 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3497 decl = parm;
3499 else
3501 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3502 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3503 decl = build_decl (parm_loc,
3504 TYPE_DECL, parm, t);
3507 TYPE_NAME (t) = decl;
3508 TYPE_STUB_DECL (t) = decl;
3509 parm = decl;
3510 TEMPLATE_TYPE_PARM_INDEX (t)
3511 = build_template_parm_index (idx, processing_template_decl,
3512 processing_template_decl,
3513 decl, TREE_TYPE (parm));
3514 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3515 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3517 DECL_ARTIFICIAL (decl) = 1;
3518 SET_DECL_TEMPLATE_PARM_P (decl);
3519 pushdecl (decl);
3520 parm = build_tree_list (defval, parm);
3521 return chainon (list, parm);
3524 /* The end of a template parameter list has been reached. Process the
3525 tree list into a parameter vector, converting each parameter into a more
3526 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3527 as PARM_DECLs. */
3529 tree
3530 end_template_parm_list (tree parms)
3532 int nparms;
3533 tree parm, next;
3534 tree saved_parmlist = make_tree_vec (list_length (parms));
3536 current_template_parms
3537 = tree_cons (size_int (processing_template_decl),
3538 saved_parmlist, current_template_parms);
3540 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3542 next = TREE_CHAIN (parm);
3543 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3544 TREE_CHAIN (parm) = NULL_TREE;
3547 --processing_template_parmlist;
3549 return saved_parmlist;
3552 /* end_template_decl is called after a template declaration is seen. */
3554 void
3555 end_template_decl (void)
3557 reset_specialization ();
3559 if (! processing_template_decl)
3560 return;
3562 /* This matches the pushlevel in begin_template_parm_list. */
3563 finish_scope ();
3565 --processing_template_decl;
3566 current_template_parms = TREE_CHAIN (current_template_parms);
3569 /* Within the declaration of a template, return all levels of template
3570 parameters that apply. The template parameters are represented as
3571 a TREE_VEC, in the form documented in cp-tree.h for template
3572 arguments. */
3574 static tree
3575 current_template_args (void)
3577 tree header;
3578 tree args = NULL_TREE;
3579 int length = TMPL_PARMS_DEPTH (current_template_parms);
3580 int l = length;
3582 /* If there is only one level of template parameters, we do not
3583 create a TREE_VEC of TREE_VECs. Instead, we return a single
3584 TREE_VEC containing the arguments. */
3585 if (length > 1)
3586 args = make_tree_vec (length);
3588 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3590 tree a = copy_node (TREE_VALUE (header));
3591 int i;
3593 TREE_TYPE (a) = NULL_TREE;
3594 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3596 tree t = TREE_VEC_ELT (a, i);
3598 /* T will be a list if we are called from within a
3599 begin/end_template_parm_list pair, but a vector directly
3600 if within a begin/end_member_template_processing pair. */
3601 if (TREE_CODE (t) == TREE_LIST)
3603 t = TREE_VALUE (t);
3605 if (!error_operand_p (t))
3607 if (TREE_CODE (t) == TYPE_DECL
3608 || TREE_CODE (t) == TEMPLATE_DECL)
3610 t = TREE_TYPE (t);
3612 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3614 /* Turn this argument into a TYPE_ARGUMENT_PACK
3615 with a single element, which expands T. */
3616 tree vec = make_tree_vec (1);
3617 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3619 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3620 SET_ARGUMENT_PACK_ARGS (t, vec);
3623 else
3625 t = DECL_INITIAL (t);
3627 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3629 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3630 with a single element, which expands T. */
3631 tree vec = make_tree_vec (1);
3632 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3633 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3635 t = make_node (NONTYPE_ARGUMENT_PACK);
3636 SET_ARGUMENT_PACK_ARGS (t, vec);
3637 TREE_TYPE (t) = type;
3640 TREE_VEC_ELT (a, i) = t;
3645 if (length > 1)
3646 TREE_VEC_ELT (args, --l) = a;
3647 else
3648 args = a;
3651 return args;
3654 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3655 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3656 a member template. Used by push_template_decl below. */
3658 static tree
3659 build_template_decl (tree decl, tree parms, bool member_template_p)
3661 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3662 DECL_TEMPLATE_PARMS (tmpl) = parms;
3663 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3664 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3666 return tmpl;
3669 struct template_parm_data
3671 /* The level of the template parameters we are currently
3672 processing. */
3673 int level;
3675 /* The index of the specialization argument we are currently
3676 processing. */
3677 int current_arg;
3679 /* An array whose size is the number of template parameters. The
3680 elements are nonzero if the parameter has been used in any one
3681 of the arguments processed so far. */
3682 int* parms;
3684 /* An array whose size is the number of template arguments. The
3685 elements are nonzero if the argument makes use of template
3686 parameters of this level. */
3687 int* arg_uses_template_parms;
3690 /* Subroutine of push_template_decl used to see if each template
3691 parameter in a partial specialization is used in the explicit
3692 argument list. If T is of the LEVEL given in DATA (which is
3693 treated as a template_parm_data*), then DATA->PARMS is marked
3694 appropriately. */
3696 static int
3697 mark_template_parm (tree t, void* data)
3699 int level;
3700 int idx;
3701 struct template_parm_data* tpd = (struct template_parm_data*) data;
3703 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3705 level = TEMPLATE_PARM_LEVEL (t);
3706 idx = TEMPLATE_PARM_IDX (t);
3708 else
3710 level = TEMPLATE_TYPE_LEVEL (t);
3711 idx = TEMPLATE_TYPE_IDX (t);
3714 if (level == tpd->level)
3716 tpd->parms[idx] = 1;
3717 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3720 /* Return zero so that for_each_template_parm will continue the
3721 traversal of the tree; we want to mark *every* template parm. */
3722 return 0;
3725 /* Process the partial specialization DECL. */
3727 static tree
3728 process_partial_specialization (tree decl)
3730 tree type = TREE_TYPE (decl);
3731 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3732 tree specargs = CLASSTYPE_TI_ARGS (type);
3733 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3734 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3735 tree inner_parms;
3736 int nargs = TREE_VEC_LENGTH (inner_args);
3737 int ntparms;
3738 int i;
3739 int did_error_intro = 0;
3740 struct template_parm_data tpd;
3741 struct template_parm_data tpd2;
3743 gcc_assert (current_template_parms);
3745 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3746 ntparms = TREE_VEC_LENGTH (inner_parms);
3748 /* We check that each of the template parameters given in the
3749 partial specialization is used in the argument list to the
3750 specialization. For example:
3752 template <class T> struct S;
3753 template <class T> struct S<T*>;
3755 The second declaration is OK because `T*' uses the template
3756 parameter T, whereas
3758 template <class T> struct S<int>;
3760 is no good. Even trickier is:
3762 template <class T>
3763 struct S1
3765 template <class U>
3766 struct S2;
3767 template <class U>
3768 struct S2<T>;
3771 The S2<T> declaration is actually invalid; it is a
3772 full-specialization. Of course,
3774 template <class U>
3775 struct S2<T (*)(U)>;
3777 or some such would have been OK. */
3778 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3779 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3780 memset (tpd.parms, 0, sizeof (int) * ntparms);
3782 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3783 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3784 for (i = 0; i < nargs; ++i)
3786 tpd.current_arg = i;
3787 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3788 &mark_template_parm,
3789 &tpd,
3790 NULL,
3791 /*include_nondeduced_p=*/false);
3793 for (i = 0; i < ntparms; ++i)
3794 if (tpd.parms[i] == 0)
3796 /* One of the template parms was not used in the
3797 specialization. */
3798 if (!did_error_intro)
3800 error ("template parameters not used in partial specialization:");
3801 did_error_intro = 1;
3804 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3807 /* [temp.class.spec]
3809 The argument list of the specialization shall not be identical to
3810 the implicit argument list of the primary template. */
3811 if (comp_template_args
3812 (inner_args,
3813 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3814 (maintmpl)))))
3815 error ("partial specialization %qT does not specialize any template arguments", type);
3817 /* [temp.class.spec]
3819 A partially specialized non-type argument expression shall not
3820 involve template parameters of the partial specialization except
3821 when the argument expression is a simple identifier.
3823 The type of a template parameter corresponding to a specialized
3824 non-type argument shall not be dependent on a parameter of the
3825 specialization.
3827 Also, we verify that pack expansions only occur at the
3828 end of the argument list. */
3829 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3830 tpd2.parms = 0;
3831 for (i = 0; i < nargs; ++i)
3833 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3834 tree arg = TREE_VEC_ELT (inner_args, i);
3835 tree packed_args = NULL_TREE;
3836 int j, len = 1;
3838 if (ARGUMENT_PACK_P (arg))
3840 /* Extract the arguments from the argument pack. We'll be
3841 iterating over these in the following loop. */
3842 packed_args = ARGUMENT_PACK_ARGS (arg);
3843 len = TREE_VEC_LENGTH (packed_args);
3846 for (j = 0; j < len; j++)
3848 if (packed_args)
3849 /* Get the Jth argument in the parameter pack. */
3850 arg = TREE_VEC_ELT (packed_args, j);
3852 if (PACK_EXPANSION_P (arg))
3854 /* Pack expansions must come at the end of the
3855 argument list. */
3856 if ((packed_args && j < len - 1)
3857 || (!packed_args && i < nargs - 1))
3859 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3860 error ("parameter pack argument %qE must be at the "
3861 "end of the template argument list", arg);
3862 else
3863 error ("parameter pack argument %qT must be at the "
3864 "end of the template argument list", arg);
3868 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3869 /* We only care about the pattern. */
3870 arg = PACK_EXPANSION_PATTERN (arg);
3872 if (/* These first two lines are the `non-type' bit. */
3873 !TYPE_P (arg)
3874 && TREE_CODE (arg) != TEMPLATE_DECL
3875 /* This next line is the `argument expression is not just a
3876 simple identifier' condition and also the `specialized
3877 non-type argument' bit. */
3878 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3880 if ((!packed_args && tpd.arg_uses_template_parms[i])
3881 || (packed_args && uses_template_parms (arg)))
3882 error ("template argument %qE involves template parameter(s)",
3883 arg);
3884 else
3886 /* Look at the corresponding template parameter,
3887 marking which template parameters its type depends
3888 upon. */
3889 tree type = TREE_TYPE (parm);
3891 if (!tpd2.parms)
3893 /* We haven't yet initialized TPD2. Do so now. */
3894 tpd2.arg_uses_template_parms
3895 = (int *) alloca (sizeof (int) * nargs);
3896 /* The number of parameters here is the number in the
3897 main template, which, as checked in the assertion
3898 above, is NARGS. */
3899 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3900 tpd2.level =
3901 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3904 /* Mark the template parameters. But this time, we're
3905 looking for the template parameters of the main
3906 template, not in the specialization. */
3907 tpd2.current_arg = i;
3908 tpd2.arg_uses_template_parms[i] = 0;
3909 memset (tpd2.parms, 0, sizeof (int) * nargs);
3910 for_each_template_parm (type,
3911 &mark_template_parm,
3912 &tpd2,
3913 NULL,
3914 /*include_nondeduced_p=*/false);
3916 if (tpd2.arg_uses_template_parms [i])
3918 /* The type depended on some template parameters.
3919 If they are fully specialized in the
3920 specialization, that's OK. */
3921 int j;
3922 for (j = 0; j < nargs; ++j)
3923 if (tpd2.parms[j] != 0
3924 && tpd.arg_uses_template_parms [j])
3926 error ("type %qT of template argument %qE depends "
3927 "on template parameter(s)",
3928 type,
3929 arg);
3930 break;
3938 /* We should only get here once. */
3939 gcc_assert (!COMPLETE_TYPE_P (type));
3941 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3942 = tree_cons (specargs, inner_parms,
3943 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3944 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3945 return decl;
3948 /* Check that a template declaration's use of default arguments and
3949 parameter packs is not invalid. Here, PARMS are the template
3950 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3951 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3952 specialization.
3955 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3956 declaration (but not a definition); 1 indicates a declaration, 2
3957 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3958 emitted for extraneous default arguments.
3960 Returns TRUE if there were no errors found, FALSE otherwise. */
3962 bool
3963 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3964 int is_partial, int is_friend_decl)
3966 const char *msg;
3967 int last_level_to_check;
3968 tree parm_level;
3969 bool no_errors = true;
3971 /* [temp.param]
3973 A default template-argument shall not be specified in a
3974 function template declaration or a function template definition, nor
3975 in the template-parameter-list of the definition of a member of a
3976 class template. */
3978 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3979 /* You can't have a function template declaration in a local
3980 scope, nor you can you define a member of a class template in a
3981 local scope. */
3982 return true;
3984 if (current_class_type
3985 && !TYPE_BEING_DEFINED (current_class_type)
3986 && DECL_LANG_SPECIFIC (decl)
3987 && DECL_DECLARES_FUNCTION_P (decl)
3988 /* If this is either a friend defined in the scope of the class
3989 or a member function. */
3990 && (DECL_FUNCTION_MEMBER_P (decl)
3991 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3992 : DECL_FRIEND_CONTEXT (decl)
3993 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3994 : false)
3995 /* And, if it was a member function, it really was defined in
3996 the scope of the class. */
3997 && (!DECL_FUNCTION_MEMBER_P (decl)
3998 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3999 /* We already checked these parameters when the template was
4000 declared, so there's no need to do it again now. This function
4001 was defined in class scope, but we're processing it's body now
4002 that the class is complete. */
4003 return true;
4005 /* Core issue 226 (C++0x only): the following only applies to class
4006 templates. */
4007 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4009 /* [temp.param]
4011 If a template-parameter has a default template-argument, all
4012 subsequent template-parameters shall have a default
4013 template-argument supplied. */
4014 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4016 tree inner_parms = TREE_VALUE (parm_level);
4017 int ntparms = TREE_VEC_LENGTH (inner_parms);
4018 int seen_def_arg_p = 0;
4019 int i;
4021 for (i = 0; i < ntparms; ++i)
4023 tree parm = TREE_VEC_ELT (inner_parms, i);
4025 if (parm == error_mark_node)
4026 continue;
4028 if (TREE_PURPOSE (parm))
4029 seen_def_arg_p = 1;
4030 else if (seen_def_arg_p
4031 && !template_parameter_pack_p (TREE_VALUE (parm)))
4033 error ("no default argument for %qD", TREE_VALUE (parm));
4034 /* For better subsequent error-recovery, we indicate that
4035 there should have been a default argument. */
4036 TREE_PURPOSE (parm) = error_mark_node;
4037 no_errors = false;
4039 else if (is_primary
4040 && !is_partial
4041 && !is_friend_decl
4042 /* Don't complain about an enclosing partial
4043 specialization. */
4044 && parm_level == parms
4045 && TREE_CODE (decl) == TYPE_DECL
4046 && i < ntparms - 1
4047 && template_parameter_pack_p (TREE_VALUE (parm)))
4049 /* A primary class template can only have one
4050 parameter pack, at the end of the template
4051 parameter list. */
4053 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4054 error ("parameter pack %qE must be at the end of the"
4055 " template parameter list", TREE_VALUE (parm));
4056 else
4057 error ("parameter pack %qT must be at the end of the"
4058 " template parameter list",
4059 TREE_TYPE (TREE_VALUE (parm)));
4061 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4062 = error_mark_node;
4063 no_errors = false;
4069 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4070 || is_partial
4071 || !is_primary
4072 || is_friend_decl)
4073 /* For an ordinary class template, default template arguments are
4074 allowed at the innermost level, e.g.:
4075 template <class T = int>
4076 struct S {};
4077 but, in a partial specialization, they're not allowed even
4078 there, as we have in [temp.class.spec]:
4080 The template parameter list of a specialization shall not
4081 contain default template argument values.
4083 So, for a partial specialization, or for a function template
4084 (in C++98/C++03), we look at all of them. */
4086 else
4087 /* But, for a primary class template that is not a partial
4088 specialization we look at all template parameters except the
4089 innermost ones. */
4090 parms = TREE_CHAIN (parms);
4092 /* Figure out what error message to issue. */
4093 if (is_friend_decl == 2)
4094 msg = "default template arguments may not be used in function template friend re-declaration";
4095 else if (is_friend_decl)
4096 msg = "default template arguments may not be used in function template friend declarations";
4097 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4098 msg = ("default template arguments may not be used in function templates "
4099 "without -std=c++0x or -std=gnu++0x");
4100 else if (is_partial)
4101 msg = "default template arguments may not be used in partial specializations";
4102 else
4103 msg = "default argument for template parameter for class enclosing %qD";
4105 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4106 /* If we're inside a class definition, there's no need to
4107 examine the parameters to the class itself. On the one
4108 hand, they will be checked when the class is defined, and,
4109 on the other, default arguments are valid in things like:
4110 template <class T = double>
4111 struct S { template <class U> void f(U); };
4112 Here the default argument for `S' has no bearing on the
4113 declaration of `f'. */
4114 last_level_to_check = template_class_depth (current_class_type) + 1;
4115 else
4116 /* Check everything. */
4117 last_level_to_check = 0;
4119 for (parm_level = parms;
4120 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4121 parm_level = TREE_CHAIN (parm_level))
4123 tree inner_parms = TREE_VALUE (parm_level);
4124 int i;
4125 int ntparms;
4127 ntparms = TREE_VEC_LENGTH (inner_parms);
4128 for (i = 0; i < ntparms; ++i)
4130 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4131 continue;
4133 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4135 if (msg)
4137 no_errors = false;
4138 if (is_friend_decl == 2)
4139 return no_errors;
4141 error (msg, decl);
4142 msg = 0;
4145 /* Clear out the default argument so that we are not
4146 confused later. */
4147 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4151 /* At this point, if we're still interested in issuing messages,
4152 they must apply to classes surrounding the object declared. */
4153 if (msg)
4154 msg = "default argument for template parameter for class enclosing %qD";
4157 return no_errors;
4160 /* Worker for push_template_decl_real, called via
4161 for_each_template_parm. DATA is really an int, indicating the
4162 level of the parameters we are interested in. If T is a template
4163 parameter of that level, return nonzero. */
4165 static int
4166 template_parm_this_level_p (tree t, void* data)
4168 int this_level = *(int *)data;
4169 int level;
4171 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4172 level = TEMPLATE_PARM_LEVEL (t);
4173 else
4174 level = TEMPLATE_TYPE_LEVEL (t);
4175 return level == this_level;
4178 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4179 parameters given by current_template_args, or reuses a
4180 previously existing one, if appropriate. Returns the DECL, or an
4181 equivalent one, if it is replaced via a call to duplicate_decls.
4183 If IS_FRIEND is true, DECL is a friend declaration. */
4185 tree
4186 push_template_decl_real (tree decl, bool is_friend)
4188 tree tmpl;
4189 tree args;
4190 tree info;
4191 tree ctx;
4192 int primary;
4193 int is_partial;
4194 int new_template_p = 0;
4195 /* True if the template is a member template, in the sense of
4196 [temp.mem]. */
4197 bool member_template_p = false;
4199 if (decl == error_mark_node || !current_template_parms)
4200 return error_mark_node;
4202 /* See if this is a partial specialization. */
4203 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4204 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4205 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4207 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4208 is_friend = true;
4210 if (is_friend)
4211 /* For a friend, we want the context of the friend function, not
4212 the type of which it is a friend. */
4213 ctx = DECL_CONTEXT (decl);
4214 else if (CP_DECL_CONTEXT (decl)
4215 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4216 /* In the case of a virtual function, we want the class in which
4217 it is defined. */
4218 ctx = CP_DECL_CONTEXT (decl);
4219 else
4220 /* Otherwise, if we're currently defining some class, the DECL
4221 is assumed to be a member of the class. */
4222 ctx = current_scope ();
4224 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4225 ctx = NULL_TREE;
4227 if (!DECL_CONTEXT (decl))
4228 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4230 /* See if this is a primary template. */
4231 if (is_friend && ctx)
4232 /* A friend template that specifies a class context, i.e.
4233 template <typename T> friend void A<T>::f();
4234 is not primary. */
4235 primary = 0;
4236 else
4237 primary = template_parm_scope_p ();
4239 if (primary)
4241 if (DECL_CLASS_SCOPE_P (decl))
4242 member_template_p = true;
4243 if (TREE_CODE (decl) == TYPE_DECL
4244 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4246 error ("template class without a name");
4247 return error_mark_node;
4249 else if (TREE_CODE (decl) == FUNCTION_DECL)
4251 if (DECL_DESTRUCTOR_P (decl))
4253 /* [temp.mem]
4255 A destructor shall not be a member template. */
4256 error ("destructor %qD declared as member template", decl);
4257 return error_mark_node;
4259 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4260 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4261 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4262 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4263 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4264 == void_list_node)))
4266 /* [basic.stc.dynamic.allocation]
4268 An allocation function can be a function
4269 template. ... Template allocation functions shall
4270 have two or more parameters. */
4271 error ("invalid template declaration of %qD", decl);
4272 return error_mark_node;
4275 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4276 && CLASS_TYPE_P (TREE_TYPE (decl)))
4277 /* OK */;
4278 else
4280 error ("template declaration of %q#D", decl);
4281 return error_mark_node;
4285 /* Check to see that the rules regarding the use of default
4286 arguments are not being violated. */
4287 check_default_tmpl_args (decl, current_template_parms,
4288 primary, is_partial, /*is_friend_decl=*/0);
4290 /* Ensure that there are no parameter packs in the type of this
4291 declaration that have not been expanded. */
4292 if (TREE_CODE (decl) == FUNCTION_DECL)
4294 /* Check each of the arguments individually to see if there are
4295 any bare parameter packs. */
4296 tree type = TREE_TYPE (decl);
4297 tree arg = DECL_ARGUMENTS (decl);
4298 tree argtype = TYPE_ARG_TYPES (type);
4300 while (arg && argtype)
4302 if (!FUNCTION_PARAMETER_PACK_P (arg)
4303 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4305 /* This is a PARM_DECL that contains unexpanded parameter
4306 packs. We have already complained about this in the
4307 check_for_bare_parameter_packs call, so just replace
4308 these types with ERROR_MARK_NODE. */
4309 TREE_TYPE (arg) = error_mark_node;
4310 TREE_VALUE (argtype) = error_mark_node;
4313 arg = TREE_CHAIN (arg);
4314 argtype = TREE_CHAIN (argtype);
4317 /* Check for bare parameter packs in the return type and the
4318 exception specifiers. */
4319 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4320 /* Errors were already issued, set return type to int
4321 as the frontend doesn't expect error_mark_node as
4322 the return type. */
4323 TREE_TYPE (type) = integer_type_node;
4324 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4325 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4327 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4329 TREE_TYPE (decl) = error_mark_node;
4330 return error_mark_node;
4333 if (is_partial)
4334 return process_partial_specialization (decl);
4336 args = current_template_args ();
4338 if (!ctx
4339 || TREE_CODE (ctx) == FUNCTION_DECL
4340 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4341 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4343 if (DECL_LANG_SPECIFIC (decl)
4344 && DECL_TEMPLATE_INFO (decl)
4345 && DECL_TI_TEMPLATE (decl))
4346 tmpl = DECL_TI_TEMPLATE (decl);
4347 /* If DECL is a TYPE_DECL for a class-template, then there won't
4348 be DECL_LANG_SPECIFIC. The information equivalent to
4349 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4350 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4351 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4352 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4354 /* Since a template declaration already existed for this
4355 class-type, we must be redeclaring it here. Make sure
4356 that the redeclaration is valid. */
4357 redeclare_class_template (TREE_TYPE (decl),
4358 current_template_parms);
4359 /* We don't need to create a new TEMPLATE_DECL; just use the
4360 one we already had. */
4361 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4363 else
4365 tmpl = build_template_decl (decl, current_template_parms,
4366 member_template_p);
4367 new_template_p = 1;
4369 if (DECL_LANG_SPECIFIC (decl)
4370 && DECL_TEMPLATE_SPECIALIZATION (decl))
4372 /* A specialization of a member template of a template
4373 class. */
4374 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4375 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4376 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4380 else
4382 tree a, t, current, parms;
4383 int i;
4384 tree tinfo = get_template_info (decl);
4386 if (!tinfo)
4388 error ("template definition of non-template %q#D", decl);
4389 return error_mark_node;
4392 tmpl = TI_TEMPLATE (tinfo);
4394 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4395 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4396 && DECL_TEMPLATE_SPECIALIZATION (decl)
4397 && DECL_MEMBER_TEMPLATE_P (tmpl))
4399 tree new_tmpl;
4401 /* The declaration is a specialization of a member
4402 template, declared outside the class. Therefore, the
4403 innermost template arguments will be NULL, so we
4404 replace them with the arguments determined by the
4405 earlier call to check_explicit_specialization. */
4406 args = DECL_TI_ARGS (decl);
4408 new_tmpl
4409 = build_template_decl (decl, current_template_parms,
4410 member_template_p);
4411 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4412 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4413 DECL_TI_TEMPLATE (decl) = new_tmpl;
4414 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4415 DECL_TEMPLATE_INFO (new_tmpl)
4416 = build_template_info (tmpl, args);
4418 register_specialization (new_tmpl,
4419 most_general_template (tmpl),
4420 args,
4421 is_friend, 0);
4422 return decl;
4425 /* Make sure the template headers we got make sense. */
4427 parms = DECL_TEMPLATE_PARMS (tmpl);
4428 i = TMPL_PARMS_DEPTH (parms);
4429 if (TMPL_ARGS_DEPTH (args) != i)
4431 error ("expected %d levels of template parms for %q#D, got %d",
4432 i, decl, TMPL_ARGS_DEPTH (args));
4434 else
4435 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4437 a = TMPL_ARGS_LEVEL (args, i);
4438 t = INNERMOST_TEMPLATE_PARMS (parms);
4440 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4442 if (current == decl)
4443 error ("got %d template parameters for %q#D",
4444 TREE_VEC_LENGTH (a), decl);
4445 else
4446 error ("got %d template parameters for %q#T",
4447 TREE_VEC_LENGTH (a), current);
4448 error (" but %d required", TREE_VEC_LENGTH (t));
4449 return error_mark_node;
4452 if (current == decl)
4453 current = ctx;
4454 else
4455 current = (TYPE_P (current)
4456 ? TYPE_CONTEXT (current)
4457 : DECL_CONTEXT (current));
4460 /* Check that the parms are used in the appropriate qualifying scopes
4461 in the declarator. */
4462 if (!comp_template_args
4463 (TI_ARGS (tinfo),
4464 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4466 error ("\
4467 template arguments to %qD do not match original template %qD",
4468 decl, DECL_TEMPLATE_RESULT (tmpl));
4469 if (!uses_template_parms (TI_ARGS (tinfo)))
4470 inform (input_location, "use template<> for an explicit specialization");
4471 /* Avoid crash in import_export_decl. */
4472 DECL_INTERFACE_KNOWN (decl) = 1;
4473 return error_mark_node;
4477 DECL_TEMPLATE_RESULT (tmpl) = decl;
4478 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4480 /* Push template declarations for global functions and types. Note
4481 that we do not try to push a global template friend declared in a
4482 template class; such a thing may well depend on the template
4483 parameters of the class. */
4484 if (new_template_p && !ctx
4485 && !(is_friend && template_class_depth (current_class_type) > 0))
4487 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4488 if (tmpl == error_mark_node)
4489 return error_mark_node;
4491 /* Hide template friend classes that haven't been declared yet. */
4492 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4494 DECL_ANTICIPATED (tmpl) = 1;
4495 DECL_FRIEND_P (tmpl) = 1;
4499 if (primary)
4501 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4502 int i;
4504 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4505 if (DECL_CONV_FN_P (tmpl))
4507 int depth = TMPL_PARMS_DEPTH (parms);
4509 /* It is a conversion operator. See if the type converted to
4510 depends on innermost template operands. */
4512 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4513 depth))
4514 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4517 /* Give template template parms a DECL_CONTEXT of the template
4518 for which they are a parameter. */
4519 parms = INNERMOST_TEMPLATE_PARMS (parms);
4520 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4522 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4523 if (TREE_CODE (parm) == TEMPLATE_DECL)
4524 DECL_CONTEXT (parm) = tmpl;
4526 if (TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM)
4527 DECL_CONTEXT (TYPE_NAME (TREE_TYPE (parm))) = tmpl;
4531 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4532 back to its most general template. If TMPL is a specialization,
4533 ARGS may only have the innermost set of arguments. Add the missing
4534 argument levels if necessary. */
4535 if (DECL_TEMPLATE_INFO (tmpl))
4536 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4538 info = build_template_info (tmpl, args);
4540 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4541 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4542 else if (DECL_LANG_SPECIFIC (decl))
4543 DECL_TEMPLATE_INFO (decl) = info;
4545 return DECL_TEMPLATE_RESULT (tmpl);
4548 tree
4549 push_template_decl (tree decl)
4551 return push_template_decl_real (decl, false);
4554 /* Called when a class template TYPE is redeclared with the indicated
4555 template PARMS, e.g.:
4557 template <class T> struct S;
4558 template <class T> struct S {}; */
4560 bool
4561 redeclare_class_template (tree type, tree parms)
4563 tree tmpl;
4564 tree tmpl_parms;
4565 int i;
4567 if (!TYPE_TEMPLATE_INFO (type))
4569 error ("%qT is not a template type", type);
4570 return false;
4573 tmpl = TYPE_TI_TEMPLATE (type);
4574 if (!PRIMARY_TEMPLATE_P (tmpl))
4575 /* The type is nested in some template class. Nothing to worry
4576 about here; there are no new template parameters for the nested
4577 type. */
4578 return true;
4580 if (!parms)
4582 error ("template specifiers not specified in declaration of %qD",
4583 tmpl);
4584 return false;
4587 parms = INNERMOST_TEMPLATE_PARMS (parms);
4588 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4590 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4592 error ("redeclared with %d template parameter(s)",
4593 TREE_VEC_LENGTH (parms));
4594 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4595 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4596 return false;
4599 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4601 tree tmpl_parm;
4602 tree parm;
4603 tree tmpl_default;
4604 tree parm_default;
4606 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4607 || TREE_VEC_ELT (parms, i) == error_mark_node)
4608 continue;
4610 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4611 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4612 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4613 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4615 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4616 TEMPLATE_DECL. */
4617 if (tmpl_parm != error_mark_node
4618 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4619 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4620 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4621 || (TREE_CODE (tmpl_parm) != PARM_DECL
4622 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4623 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4624 || (TREE_CODE (tmpl_parm) == PARM_DECL
4625 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4626 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4628 error ("template parameter %q+#D", tmpl_parm);
4629 error ("redeclared here as %q#D", parm);
4630 return false;
4633 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4635 /* We have in [temp.param]:
4637 A template-parameter may not be given default arguments
4638 by two different declarations in the same scope. */
4639 error_at (input_location, "redefinition of default argument for %q#D", parm);
4640 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4641 "original definition appeared here");
4642 return false;
4645 if (parm_default != NULL_TREE)
4646 /* Update the previous template parameters (which are the ones
4647 that will really count) with the new default value. */
4648 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4649 else if (tmpl_default != NULL_TREE)
4650 /* Update the new parameters, too; they'll be used as the
4651 parameters for any members. */
4652 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4655 return true;
4658 /* Simplify EXPR if it is a non-dependent expression. Returns the
4659 (possibly simplified) expression. */
4661 tree
4662 fold_non_dependent_expr (tree expr)
4664 if (expr == NULL_TREE)
4665 return NULL_TREE;
4667 /* If we're in a template, but EXPR isn't value dependent, simplify
4668 it. We're supposed to treat:
4670 template <typename T> void f(T[1 + 1]);
4671 template <typename T> void f(T[2]);
4673 as two declarations of the same function, for example. */
4674 if (processing_template_decl
4675 && !type_dependent_expression_p (expr)
4676 && !value_dependent_expression_p (expr))
4678 HOST_WIDE_INT saved_processing_template_decl;
4680 saved_processing_template_decl = processing_template_decl;
4681 processing_template_decl = 0;
4682 expr = tsubst_copy_and_build (expr,
4683 /*args=*/NULL_TREE,
4684 tf_error,
4685 /*in_decl=*/NULL_TREE,
4686 /*function_p=*/false,
4687 /*integral_constant_expression_p=*/true);
4688 processing_template_decl = saved_processing_template_decl;
4690 return expr;
4693 /* EXPR is an expression which is used in a constant-expression context.
4694 For instance, it could be a VAR_DECL with a constant initializer.
4695 Extract the innermost constant expression.
4697 This is basically a more powerful version of
4698 integral_constant_value, which can be used also in templates where
4699 initializers can maintain a syntactic rather than semantic form
4700 (even if they are non-dependent, for access-checking purposes). */
4702 static tree
4703 fold_decl_constant_value (tree expr)
4705 tree const_expr = expr;
4708 expr = fold_non_dependent_expr (const_expr);
4709 const_expr = integral_constant_value (expr);
4711 while (expr != const_expr);
4713 return expr;
4716 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4717 must be a function or a pointer-to-function type, as specified
4718 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4719 and check that the resulting function has external linkage. */
4721 static tree
4722 convert_nontype_argument_function (tree type, tree expr)
4724 tree fns = expr;
4725 tree fn, fn_no_ptr;
4727 fn = instantiate_type (type, fns, tf_none);
4728 if (fn == error_mark_node)
4729 return error_mark_node;
4731 fn_no_ptr = fn;
4732 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4733 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4734 if (TREE_CODE (fn_no_ptr) == BASELINK)
4735 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4737 /* [temp.arg.nontype]/1
4739 A template-argument for a non-type, non-template template-parameter
4740 shall be one of:
4741 [...]
4742 -- the address of an object or function with external linkage. */
4743 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4745 error ("%qE is not a valid template argument for type %qT "
4746 "because function %qD has not external linkage",
4747 expr, type, fn_no_ptr);
4748 return NULL_TREE;
4751 return fn;
4754 /* Subroutine of convert_nontype_argument.
4755 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4756 Emit an error otherwise. */
4758 static bool
4759 check_valid_ptrmem_cst_expr (tree type, tree expr)
4761 STRIP_NOPS (expr);
4762 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4763 return true;
4764 error ("%qE is not a valid template argument for type %qT",
4765 expr, type);
4766 error ("it must be a pointer-to-member of the form `&X::Y'");
4767 return false;
4770 /* Attempt to convert the non-type template parameter EXPR to the
4771 indicated TYPE. If the conversion is successful, return the
4772 converted value. If the conversion is unsuccessful, return
4773 NULL_TREE if we issued an error message, or error_mark_node if we
4774 did not. We issue error messages for out-and-out bad template
4775 parameters, but not simply because the conversion failed, since we
4776 might be just trying to do argument deduction. Both TYPE and EXPR
4777 must be non-dependent.
4779 The conversion follows the special rules described in
4780 [temp.arg.nontype], and it is much more strict than an implicit
4781 conversion.
4783 This function is called twice for each template argument (see
4784 lookup_template_class for a more accurate description of this
4785 problem). This means that we need to handle expressions which
4786 are not valid in a C++ source, but can be created from the
4787 first call (for instance, casts to perform conversions). These
4788 hacks can go away after we fix the double coercion problem. */
4790 static tree
4791 convert_nontype_argument (tree type, tree expr)
4793 tree expr_type;
4795 /* Detect immediately string literals as invalid non-type argument.
4796 This special-case is not needed for correctness (we would easily
4797 catch this later), but only to provide better diagnostic for this
4798 common user mistake. As suggested by DR 100, we do not mention
4799 linkage issues in the diagnostic as this is not the point. */
4800 if (TREE_CODE (expr) == STRING_CST)
4802 error ("%qE is not a valid template argument for type %qT "
4803 "because string literals can never be used in this context",
4804 expr, type);
4805 return NULL_TREE;
4808 /* If we are in a template, EXPR may be non-dependent, but still
4809 have a syntactic, rather than semantic, form. For example, EXPR
4810 might be a SCOPE_REF, rather than the VAR_DECL to which the
4811 SCOPE_REF refers. Preserving the qualifying scope is necessary
4812 so that access checking can be performed when the template is
4813 instantiated -- but here we need the resolved form so that we can
4814 convert the argument. */
4815 expr = fold_non_dependent_expr (expr);
4816 if (error_operand_p (expr))
4817 return error_mark_node;
4818 expr_type = TREE_TYPE (expr);
4820 /* HACK: Due to double coercion, we can get a
4821 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4822 which is the tree that we built on the first call (see
4823 below when coercing to reference to object or to reference to
4824 function). We just strip everything and get to the arg.
4825 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4826 for examples. */
4827 if (TREE_CODE (expr) == NOP_EXPR)
4829 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4831 /* ??? Maybe we could use convert_from_reference here, but we
4832 would need to relax its constraints because the NOP_EXPR
4833 could actually change the type to something more cv-qualified,
4834 and this is not folded by convert_from_reference. */
4835 tree addr = TREE_OPERAND (expr, 0);
4836 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4837 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4838 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4839 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4840 (TREE_TYPE (expr_type),
4841 TREE_TYPE (TREE_TYPE (addr))));
4843 expr = TREE_OPERAND (addr, 0);
4844 expr_type = TREE_TYPE (expr);
4847 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4848 parameter is a pointer to object, through decay and
4849 qualification conversion. Let's strip everything. */
4850 else if (TYPE_PTROBV_P (type))
4852 STRIP_NOPS (expr);
4853 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4854 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4855 /* Skip the ADDR_EXPR only if it is part of the decay for
4856 an array. Otherwise, it is part of the original argument
4857 in the source code. */
4858 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4859 expr = TREE_OPERAND (expr, 0);
4860 expr_type = TREE_TYPE (expr);
4864 /* [temp.arg.nontype]/5, bullet 1
4866 For a non-type template-parameter of integral or enumeration type,
4867 integral promotions (_conv.prom_) and integral conversions
4868 (_conv.integral_) are applied. */
4869 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4871 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4872 return error_mark_node;
4874 expr = fold_decl_constant_value (expr);
4875 /* Notice that there are constant expressions like '4 % 0' which
4876 do not fold into integer constants. */
4877 if (TREE_CODE (expr) != INTEGER_CST)
4879 error ("%qE is not a valid template argument for type %qT "
4880 "because it is a non-constant expression", expr, type);
4881 return NULL_TREE;
4884 /* At this point, an implicit conversion does what we want,
4885 because we already know that the expression is of integral
4886 type. */
4887 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4888 if (expr == error_mark_node)
4889 return error_mark_node;
4891 /* Conversion was allowed: fold it to a bare integer constant. */
4892 expr = fold (expr);
4894 /* [temp.arg.nontype]/5, bullet 2
4896 For a non-type template-parameter of type pointer to object,
4897 qualification conversions (_conv.qual_) and the array-to-pointer
4898 conversion (_conv.array_) are applied. */
4899 else if (TYPE_PTROBV_P (type))
4901 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4903 A template-argument for a non-type, non-template template-parameter
4904 shall be one of: [...]
4906 -- the name of a non-type template-parameter;
4907 -- the address of an object or function with external linkage, [...]
4908 expressed as "& id-expression" where the & is optional if the name
4909 refers to a function or array, or if the corresponding
4910 template-parameter is a reference.
4912 Here, we do not care about functions, as they are invalid anyway
4913 for a parameter of type pointer-to-object. */
4915 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4916 /* Non-type template parameters are OK. */
4918 else if (TREE_CODE (expr) != ADDR_EXPR
4919 && TREE_CODE (expr_type) != ARRAY_TYPE)
4921 if (TREE_CODE (expr) == VAR_DECL)
4923 error ("%qD is not a valid template argument "
4924 "because %qD is a variable, not the address of "
4925 "a variable",
4926 expr, expr);
4927 return NULL_TREE;
4929 /* Other values, like integer constants, might be valid
4930 non-type arguments of some other type. */
4931 return error_mark_node;
4933 else
4935 tree decl;
4937 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4938 ? TREE_OPERAND (expr, 0) : expr);
4939 if (TREE_CODE (decl) != VAR_DECL)
4941 error ("%qE is not a valid template argument of type %qT "
4942 "because %qE is not a variable",
4943 expr, type, decl);
4944 return NULL_TREE;
4946 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4948 error ("%qE is not a valid template argument of type %qT "
4949 "because %qD does not have external linkage",
4950 expr, type, decl);
4951 return NULL_TREE;
4955 expr = decay_conversion (expr);
4956 if (expr == error_mark_node)
4957 return error_mark_node;
4959 expr = perform_qualification_conversions (type, expr);
4960 if (expr == error_mark_node)
4961 return error_mark_node;
4963 /* [temp.arg.nontype]/5, bullet 3
4965 For a non-type template-parameter of type reference to object, no
4966 conversions apply. The type referred to by the reference may be more
4967 cv-qualified than the (otherwise identical) type of the
4968 template-argument. The template-parameter is bound directly to the
4969 template-argument, which must be an lvalue. */
4970 else if (TYPE_REF_OBJ_P (type))
4972 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4973 expr_type))
4974 return error_mark_node;
4976 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4978 error ("%qE is not a valid template argument for type %qT "
4979 "because of conflicts in cv-qualification", expr, type);
4980 return NULL_TREE;
4983 if (!real_lvalue_p (expr))
4985 error ("%qE is not a valid template argument for type %qT "
4986 "because it is not an lvalue", expr, type);
4987 return NULL_TREE;
4990 /* [temp.arg.nontype]/1
4992 A template-argument for a non-type, non-template template-parameter
4993 shall be one of: [...]
4995 -- the address of an object or function with external linkage. */
4996 if (TREE_CODE (expr) == INDIRECT_REF
4997 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
4999 expr = TREE_OPERAND (expr, 0);
5000 if (DECL_P (expr))
5002 error ("%q#D is not a valid template argument for type %qT "
5003 "because a reference variable does not have a constant "
5004 "address", expr, type);
5005 return NULL_TREE;
5009 if (!DECL_P (expr))
5011 error ("%qE is not a valid template argument for type %qT "
5012 "because it is not an object with external linkage",
5013 expr, type);
5014 return NULL_TREE;
5017 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5019 error ("%qE is not a valid template argument for type %qT "
5020 "because object %qD has not external linkage",
5021 expr, type, expr);
5022 return NULL_TREE;
5025 expr = build_nop (type, build_address (expr));
5027 /* [temp.arg.nontype]/5, bullet 4
5029 For a non-type template-parameter of type pointer to function, only
5030 the function-to-pointer conversion (_conv.func_) is applied. If the
5031 template-argument represents a set of overloaded functions (or a
5032 pointer to such), the matching function is selected from the set
5033 (_over.over_). */
5034 else if (TYPE_PTRFN_P (type))
5036 /* If the argument is a template-id, we might not have enough
5037 context information to decay the pointer. */
5038 if (!type_unknown_p (expr_type))
5040 expr = decay_conversion (expr);
5041 if (expr == error_mark_node)
5042 return error_mark_node;
5045 expr = convert_nontype_argument_function (type, expr);
5046 if (!expr || expr == error_mark_node)
5047 return expr;
5049 if (TREE_CODE (expr) != ADDR_EXPR)
5051 error ("%qE is not a valid template argument for type %qT", expr, type);
5052 error ("it must be the address of a function with external linkage");
5053 return NULL_TREE;
5056 /* [temp.arg.nontype]/5, bullet 5
5058 For a non-type template-parameter of type reference to function, no
5059 conversions apply. If the template-argument represents a set of
5060 overloaded functions, the matching function is selected from the set
5061 (_over.over_). */
5062 else if (TYPE_REFFN_P (type))
5064 if (TREE_CODE (expr) == ADDR_EXPR)
5066 error ("%qE is not a valid template argument for type %qT "
5067 "because it is a pointer", expr, type);
5068 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5069 return NULL_TREE;
5072 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5073 if (!expr || expr == error_mark_node)
5074 return expr;
5076 expr = build_nop (type, build_address (expr));
5078 /* [temp.arg.nontype]/5, bullet 6
5080 For a non-type template-parameter of type pointer to member function,
5081 no conversions apply. If the template-argument represents a set of
5082 overloaded member functions, the matching member function is selected
5083 from the set (_over.over_). */
5084 else if (TYPE_PTRMEMFUNC_P (type))
5086 expr = instantiate_type (type, expr, tf_none);
5087 if (expr == error_mark_node)
5088 return error_mark_node;
5090 /* [temp.arg.nontype] bullet 1 says the pointer to member
5091 expression must be a pointer-to-member constant. */
5092 if (!check_valid_ptrmem_cst_expr (type, expr))
5093 return error_mark_node;
5095 /* There is no way to disable standard conversions in
5096 resolve_address_of_overloaded_function (called by
5097 instantiate_type). It is possible that the call succeeded by
5098 converting &B::I to &D::I (where B is a base of D), so we need
5099 to reject this conversion here.
5101 Actually, even if there was a way to disable standard conversions,
5102 it would still be better to reject them here so that we can
5103 provide a superior diagnostic. */
5104 if (!same_type_p (TREE_TYPE (expr), type))
5106 /* Make sure we are just one standard conversion off. */
5107 gcc_assert (can_convert (type, TREE_TYPE (expr)));
5108 error ("%qE is not a valid template argument for type %qT "
5109 "because it is of type %qT", expr, type,
5110 TREE_TYPE (expr));
5111 inform (input_location, "standard conversions are not allowed in this context");
5112 return NULL_TREE;
5115 /* [temp.arg.nontype]/5, bullet 7
5117 For a non-type template-parameter of type pointer to data member,
5118 qualification conversions (_conv.qual_) are applied. */
5119 else if (TYPE_PTRMEM_P (type))
5121 /* [temp.arg.nontype] bullet 1 says the pointer to member
5122 expression must be a pointer-to-member constant. */
5123 if (!check_valid_ptrmem_cst_expr (type, expr))
5124 return error_mark_node;
5126 expr = perform_qualification_conversions (type, expr);
5127 if (expr == error_mark_node)
5128 return expr;
5130 /* A template non-type parameter must be one of the above. */
5131 else
5132 gcc_unreachable ();
5134 /* Sanity check: did we actually convert the argument to the
5135 right type? */
5136 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5137 return expr;
5140 /* Subroutine of coerce_template_template_parms, which returns 1 if
5141 PARM_PARM and ARG_PARM match using the rule for the template
5142 parameters of template template parameters. Both PARM and ARG are
5143 template parameters; the rest of the arguments are the same as for
5144 coerce_template_template_parms.
5146 static int
5147 coerce_template_template_parm (tree parm,
5148 tree arg,
5149 tsubst_flags_t complain,
5150 tree in_decl,
5151 tree outer_args)
5153 if (arg == NULL_TREE || arg == error_mark_node
5154 || parm == NULL_TREE || parm == error_mark_node)
5155 return 0;
5157 if (TREE_CODE (arg) != TREE_CODE (parm))
5158 return 0;
5160 switch (TREE_CODE (parm))
5162 case TEMPLATE_DECL:
5163 /* We encounter instantiations of templates like
5164 template <template <template <class> class> class TT>
5165 class C; */
5167 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5168 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5170 if (!coerce_template_template_parms
5171 (parmparm, argparm, complain, in_decl, outer_args))
5172 return 0;
5174 /* Fall through. */
5176 case TYPE_DECL:
5177 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5178 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5179 /* Argument is a parameter pack but parameter is not. */
5180 return 0;
5181 break;
5183 case PARM_DECL:
5184 /* The tsubst call is used to handle cases such as
5186 template <int> class C {};
5187 template <class T, template <T> class TT> class D {};
5188 D<int, C> d;
5190 i.e. the parameter list of TT depends on earlier parameters. */
5191 if (!uses_template_parms (TREE_TYPE (arg))
5192 && !same_type_p
5193 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5194 TREE_TYPE (arg)))
5195 return 0;
5197 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5198 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5199 /* Argument is a parameter pack but parameter is not. */
5200 return 0;
5202 break;
5204 default:
5205 gcc_unreachable ();
5208 return 1;
5212 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5213 template template parameters. Both PARM_PARMS and ARG_PARMS are
5214 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5215 or PARM_DECL.
5217 Consider the example:
5218 template <class T> class A;
5219 template<template <class U> class TT> class B;
5221 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5222 the parameters to A, and OUTER_ARGS contains A. */
5224 static int
5225 coerce_template_template_parms (tree parm_parms,
5226 tree arg_parms,
5227 tsubst_flags_t complain,
5228 tree in_decl,
5229 tree outer_args)
5231 int nparms, nargs, i;
5232 tree parm, arg;
5233 int variadic_p = 0;
5235 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5236 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5238 nparms = TREE_VEC_LENGTH (parm_parms);
5239 nargs = TREE_VEC_LENGTH (arg_parms);
5241 /* Determine whether we have a parameter pack at the end of the
5242 template template parameter's template parameter list. */
5243 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5245 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5247 if (parm == error_mark_node)
5248 return 0;
5250 switch (TREE_CODE (parm))
5252 case TEMPLATE_DECL:
5253 case TYPE_DECL:
5254 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5255 variadic_p = 1;
5256 break;
5258 case PARM_DECL:
5259 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5260 variadic_p = 1;
5261 break;
5263 default:
5264 gcc_unreachable ();
5268 if (nargs != nparms
5269 && !(variadic_p && nargs >= nparms - 1))
5270 return 0;
5272 /* Check all of the template parameters except the parameter pack at
5273 the end (if any). */
5274 for (i = 0; i < nparms - variadic_p; ++i)
5276 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5277 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5278 continue;
5280 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5281 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5283 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5284 outer_args))
5285 return 0;
5289 if (variadic_p)
5291 /* Check each of the template parameters in the template
5292 argument against the template parameter pack at the end of
5293 the template template parameter. */
5294 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5295 return 0;
5297 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5299 for (; i < nargs; ++i)
5301 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5302 continue;
5304 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5306 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5307 outer_args))
5308 return 0;
5312 return 1;
5315 /* Verifies that the deduced template arguments (in TARGS) for the
5316 template template parameters (in TPARMS) represent valid bindings,
5317 by comparing the template parameter list of each template argument
5318 to the template parameter list of its corresponding template
5319 template parameter, in accordance with DR150. This
5320 routine can only be called after all template arguments have been
5321 deduced. It will return TRUE if all of the template template
5322 parameter bindings are okay, FALSE otherwise. */
5323 bool
5324 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5326 int i, ntparms = TREE_VEC_LENGTH (tparms);
5327 bool ret = true;
5329 /* We're dealing with template parms in this process. */
5330 ++processing_template_decl;
5332 targs = INNERMOST_TEMPLATE_ARGS (targs);
5334 for (i = 0; i < ntparms; ++i)
5336 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5337 tree targ = TREE_VEC_ELT (targs, i);
5339 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5341 tree packed_args = NULL_TREE;
5342 int idx, len = 1;
5344 if (ARGUMENT_PACK_P (targ))
5346 /* Look inside the argument pack. */
5347 packed_args = ARGUMENT_PACK_ARGS (targ);
5348 len = TREE_VEC_LENGTH (packed_args);
5351 for (idx = 0; idx < len; ++idx)
5353 tree targ_parms = NULL_TREE;
5355 if (packed_args)
5356 /* Extract the next argument from the argument
5357 pack. */
5358 targ = TREE_VEC_ELT (packed_args, idx);
5360 if (PACK_EXPANSION_P (targ))
5361 /* Look at the pattern of the pack expansion. */
5362 targ = PACK_EXPANSION_PATTERN (targ);
5364 /* Extract the template parameters from the template
5365 argument. */
5366 if (TREE_CODE (targ) == TEMPLATE_DECL)
5367 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5368 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5369 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5371 /* Verify that we can coerce the template template
5372 parameters from the template argument to the template
5373 parameter. This requires an exact match. */
5374 if (targ_parms
5375 && !coerce_template_template_parms
5376 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5377 targ_parms,
5378 tf_none,
5379 tparm,
5380 targs))
5382 ret = false;
5383 goto out;
5389 out:
5391 --processing_template_decl;
5392 return ret;
5395 /* Convert the indicated template ARG as necessary to match the
5396 indicated template PARM. Returns the converted ARG, or
5397 error_mark_node if the conversion was unsuccessful. Error and
5398 warning messages are issued under control of COMPLAIN. This
5399 conversion is for the Ith parameter in the parameter list. ARGS is
5400 the full set of template arguments deduced so far. */
5402 static tree
5403 convert_template_argument (tree parm,
5404 tree arg,
5405 tree args,
5406 tsubst_flags_t complain,
5407 int i,
5408 tree in_decl)
5410 tree orig_arg;
5411 tree val;
5412 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5414 if (TREE_CODE (arg) == TREE_LIST
5415 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5417 /* The template argument was the name of some
5418 member function. That's usually
5419 invalid, but static members are OK. In any
5420 case, grab the underlying fields/functions
5421 and issue an error later if required. */
5422 orig_arg = TREE_VALUE (arg);
5423 TREE_TYPE (arg) = unknown_type_node;
5426 orig_arg = arg;
5428 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5429 requires_type = (TREE_CODE (parm) == TYPE_DECL
5430 || requires_tmpl_type);
5432 /* When determining whether an argument pack expansion is a template,
5433 look at the pattern. */
5434 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5435 arg = PACK_EXPANSION_PATTERN (arg);
5437 /* Deal with an injected-class-name used as a template template arg. */
5438 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5440 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5441 if (TREE_CODE (t) == TEMPLATE_DECL)
5443 if (complain & tf_warning_or_error)
5444 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5445 " used as template template argument", TYPE_NAME (arg));
5446 else if (flag_pedantic_errors)
5447 t = arg;
5449 arg = t;
5453 is_tmpl_type =
5454 ((TREE_CODE (arg) == TEMPLATE_DECL
5455 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5456 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5457 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5459 if (is_tmpl_type
5460 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5461 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5462 arg = TYPE_STUB_DECL (arg);
5464 is_type = TYPE_P (arg) || is_tmpl_type;
5466 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5467 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5469 permerror (input_location, "to refer to a type member of a template parameter, "
5470 "use %<typename %E%>", orig_arg);
5472 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5473 TREE_OPERAND (arg, 1),
5474 typename_type,
5475 complain & tf_error);
5476 arg = orig_arg;
5477 is_type = 1;
5479 if (is_type != requires_type)
5481 if (in_decl)
5483 if (complain & tf_error)
5485 error ("type/value mismatch at argument %d in template "
5486 "parameter list for %qD",
5487 i + 1, in_decl);
5488 if (is_type)
5489 error (" expected a constant of type %qT, got %qT",
5490 TREE_TYPE (parm),
5491 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5492 else if (requires_tmpl_type)
5493 error (" expected a class template, got %qE", orig_arg);
5494 else
5495 error (" expected a type, got %qE", orig_arg);
5498 return error_mark_node;
5500 if (is_tmpl_type ^ requires_tmpl_type)
5502 if (in_decl && (complain & tf_error))
5504 error ("type/value mismatch at argument %d in template "
5505 "parameter list for %qD",
5506 i + 1, in_decl);
5507 if (is_tmpl_type)
5508 error (" expected a type, got %qT", DECL_NAME (arg));
5509 else
5510 error (" expected a class template, got %qT", orig_arg);
5512 return error_mark_node;
5515 if (is_type)
5517 if (requires_tmpl_type)
5519 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5520 /* The number of argument required is not known yet.
5521 Just accept it for now. */
5522 val = TREE_TYPE (arg);
5523 else
5525 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5526 tree argparm;
5528 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5530 if (coerce_template_template_parms (parmparm, argparm,
5531 complain, in_decl,
5532 args))
5534 val = arg;
5536 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5537 TEMPLATE_DECL. */
5538 if (val != error_mark_node)
5540 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5541 val = TREE_TYPE (val);
5542 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5543 val = make_pack_expansion (val);
5546 else
5548 if (in_decl && (complain & tf_error))
5550 error ("type/value mismatch at argument %d in "
5551 "template parameter list for %qD",
5552 i + 1, in_decl);
5553 error (" expected a template of type %qD, got %qT",
5554 parm, orig_arg);
5557 val = error_mark_node;
5561 else
5562 val = orig_arg;
5563 /* We only form one instance of each template specialization.
5564 Therefore, if we use a non-canonical variant (i.e., a
5565 typedef), any future messages referring to the type will use
5566 the typedef, which is confusing if those future uses do not
5567 themselves also use the typedef. */
5568 if (TYPE_P (val))
5569 val = strip_typedefs (val);
5571 else
5573 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5575 if (invalid_nontype_parm_type_p (t, complain))
5576 return error_mark_node;
5578 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5580 if (same_type_p (t, TREE_TYPE (orig_arg)))
5581 val = orig_arg;
5582 else
5584 /* Not sure if this is reachable, but it doesn't hurt
5585 to be robust. */
5586 error ("type mismatch in nontype parameter pack");
5587 val = error_mark_node;
5590 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5591 /* We used to call digest_init here. However, digest_init
5592 will report errors, which we don't want when complain
5593 is zero. More importantly, digest_init will try too
5594 hard to convert things: for example, `0' should not be
5595 converted to pointer type at this point according to
5596 the standard. Accepting this is not merely an
5597 extension, since deciding whether or not these
5598 conversions can occur is part of determining which
5599 function template to call, or whether a given explicit
5600 argument specification is valid. */
5601 val = convert_nontype_argument (t, orig_arg);
5602 else
5603 val = orig_arg;
5605 if (val == NULL_TREE)
5606 val = error_mark_node;
5607 else if (val == error_mark_node && (complain & tf_error))
5608 error ("could not convert template argument %qE to %qT", orig_arg, t);
5610 if (TREE_CODE (val) == SCOPE_REF)
5612 /* Strip typedefs from the SCOPE_REF. */
5613 tree type = strip_typedefs (TREE_TYPE (val));
5614 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5615 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5616 QUALIFIED_NAME_IS_TEMPLATE (val));
5620 return val;
5623 /* Coerces the remaining template arguments in INNER_ARGS (from
5624 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5625 Returns the coerced argument pack. PARM_IDX is the position of this
5626 parameter in the template parameter list. ARGS is the original
5627 template argument list. */
5628 static tree
5629 coerce_template_parameter_pack (tree parms,
5630 int parm_idx,
5631 tree args,
5632 tree inner_args,
5633 int arg_idx,
5634 tree new_args,
5635 int* lost,
5636 tree in_decl,
5637 tsubst_flags_t complain)
5639 tree parm = TREE_VEC_ELT (parms, parm_idx);
5640 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5641 tree packed_args;
5642 tree argument_pack;
5643 tree packed_types = NULL_TREE;
5645 if (arg_idx > nargs)
5646 arg_idx = nargs;
5648 packed_args = make_tree_vec (nargs - arg_idx);
5650 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5651 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5653 /* When the template parameter is a non-type template
5654 parameter pack whose type uses parameter packs, we need
5655 to look at each of the template arguments
5656 separately. Build a vector of the types for these
5657 non-type template parameters in PACKED_TYPES. */
5658 tree expansion
5659 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5660 packed_types = tsubst_pack_expansion (expansion, args,
5661 complain, in_decl);
5663 if (packed_types == error_mark_node)
5664 return error_mark_node;
5666 /* Check that we have the right number of arguments. */
5667 if (arg_idx < nargs
5668 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5669 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5671 int needed_parms
5672 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5673 error ("wrong number of template arguments (%d, should be %d)",
5674 nargs, needed_parms);
5675 return error_mark_node;
5678 /* If we aren't able to check the actual arguments now
5679 (because they haven't been expanded yet), we can at least
5680 verify that all of the types used for the non-type
5681 template parameter pack are, in fact, valid for non-type
5682 template parameters. */
5683 if (arg_idx < nargs
5684 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5686 int j, len = TREE_VEC_LENGTH (packed_types);
5687 for (j = 0; j < len; ++j)
5689 tree t = TREE_VEC_ELT (packed_types, j);
5690 if (invalid_nontype_parm_type_p (t, complain))
5691 return error_mark_node;
5696 /* Convert the remaining arguments, which will be a part of the
5697 parameter pack "parm". */
5698 for (; arg_idx < nargs; ++arg_idx)
5700 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5701 tree actual_parm = TREE_VALUE (parm);
5703 if (packed_types && !PACK_EXPANSION_P (arg))
5705 /* When we have a vector of types (corresponding to the
5706 non-type template parameter pack that uses parameter
5707 packs in its type, as mention above), and the
5708 argument is not an expansion (which expands to a
5709 currently unknown number of arguments), clone the
5710 parm and give it the next type in PACKED_TYPES. */
5711 actual_parm = copy_node (actual_parm);
5712 TREE_TYPE (actual_parm) =
5713 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5716 if (arg != error_mark_node)
5717 arg = convert_template_argument (actual_parm,
5718 arg, new_args, complain, parm_idx,
5719 in_decl);
5720 if (arg == error_mark_node)
5721 (*lost)++;
5722 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5725 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5726 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5727 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5728 else
5730 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5731 TREE_TYPE (argument_pack)
5732 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5733 TREE_CONSTANT (argument_pack) = 1;
5736 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5737 return argument_pack;
5740 /* Convert all template arguments to their appropriate types, and
5741 return a vector containing the innermost resulting template
5742 arguments. If any error occurs, return error_mark_node. Error and
5743 warning messages are issued under control of COMPLAIN.
5745 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5746 for arguments not specified in ARGS. Otherwise, if
5747 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5748 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5749 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5750 ARGS. */
5752 static tree
5753 coerce_template_parms (tree parms,
5754 tree args,
5755 tree in_decl,
5756 tsubst_flags_t complain,
5757 bool require_all_args,
5758 bool use_default_args)
5760 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5761 tree inner_args;
5762 tree new_args;
5763 tree new_inner_args;
5764 int saved_unevaluated_operand;
5765 int saved_inhibit_evaluation_warnings;
5767 /* When used as a boolean value, indicates whether this is a
5768 variadic template parameter list. Since it's an int, we can also
5769 subtract it from nparms to get the number of non-variadic
5770 parameters. */
5771 int variadic_p = 0;
5773 nparms = TREE_VEC_LENGTH (parms);
5775 /* Determine if there are any parameter packs. */
5776 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5778 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5779 if (template_parameter_pack_p (tparm))
5780 ++variadic_p;
5783 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5784 /* If there are 0 or 1 parameter packs, we need to expand any argument
5785 packs so that we can deduce a parameter pack from some non-packed args
5786 followed by an argument pack, as in variadic85.C. If there are more
5787 than that, we need to leave argument packs intact so the arguments are
5788 assigned to the right parameter packs. This should only happen when
5789 dealing with a nested class inside a partial specialization of a class
5790 template, as in variadic92.C. */
5791 if (variadic_p <= 1)
5792 inner_args = expand_template_argument_pack (inner_args);
5794 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5795 if ((nargs > nparms && !variadic_p)
5796 || (nargs < nparms - variadic_p
5797 && require_all_args
5798 && (!use_default_args
5799 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5800 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5802 if (complain & tf_error)
5804 const char *or_more = "";
5805 if (variadic_p)
5807 or_more = " or more";
5808 --nparms;
5811 error ("wrong number of template arguments (%d, should be %d%s)",
5812 nargs, nparms, or_more);
5814 if (in_decl)
5815 error ("provided for %q+D", in_decl);
5818 return error_mark_node;
5821 /* We need to evaluate the template arguments, even though this
5822 template-id may be nested within a "sizeof". */
5823 saved_unevaluated_operand = cp_unevaluated_operand;
5824 cp_unevaluated_operand = 0;
5825 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5826 c_inhibit_evaluation_warnings = 0;
5827 new_inner_args = make_tree_vec (nparms);
5828 new_args = add_outermost_template_args (args, new_inner_args);
5829 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5831 tree arg;
5832 tree parm;
5834 /* Get the Ith template parameter. */
5835 parm = TREE_VEC_ELT (parms, parm_idx);
5837 if (parm == error_mark_node)
5839 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5840 continue;
5843 /* Calculate the next argument. */
5844 if (arg_idx < nargs)
5845 arg = TREE_VEC_ELT (inner_args, arg_idx);
5846 else
5847 arg = NULL_TREE;
5849 if (template_parameter_pack_p (TREE_VALUE (parm))
5850 && !(arg && ARGUMENT_PACK_P (arg)))
5852 /* All remaining arguments will be placed in the
5853 template parameter pack PARM. */
5854 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5855 inner_args, arg_idx,
5856 new_args, &lost,
5857 in_decl, complain);
5859 /* Store this argument. */
5860 if (arg == error_mark_node)
5861 lost++;
5862 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5864 /* We are done with all of the arguments. */
5865 arg_idx = nargs;
5867 continue;
5869 else if (arg)
5871 if (PACK_EXPANSION_P (arg))
5873 if (complain & tf_error)
5875 /* FIXME this restriction was removed by N2555; see
5876 bug 35722. */
5877 /* If ARG is a pack expansion, but PARM is not a
5878 template parameter pack (if it were, we would have
5879 handled it above), we're trying to expand into a
5880 fixed-length argument list. */
5881 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5882 sorry ("cannot expand %<%E%> into a fixed-length "
5883 "argument list", arg);
5884 else
5885 sorry ("cannot expand %<%T%> into a fixed-length "
5886 "argument list", arg);
5888 return error_mark_node;
5891 else if (require_all_args)
5892 /* There must be a default arg in this case. */
5893 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5894 complain, in_decl);
5895 else
5896 break;
5898 if (arg == error_mark_node)
5900 if (complain & tf_error)
5901 error ("template argument %d is invalid", arg_idx + 1);
5903 else if (!arg)
5904 /* This only occurs if there was an error in the template
5905 parameter list itself (which we would already have
5906 reported) that we are trying to recover from, e.g., a class
5907 template with a parameter list such as
5908 template<typename..., typename>. */
5909 return error_mark_node;
5910 else
5911 arg = convert_template_argument (TREE_VALUE (parm),
5912 arg, new_args, complain,
5913 parm_idx, in_decl);
5915 if (arg == error_mark_node)
5916 lost++;
5917 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5919 cp_unevaluated_operand = saved_unevaluated_operand;
5920 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
5922 if (lost)
5923 return error_mark_node;
5925 return new_inner_args;
5928 /* Returns 1 if template args OT and NT are equivalent. */
5930 static int
5931 template_args_equal (tree ot, tree nt)
5933 if (nt == ot)
5934 return 1;
5936 if (TREE_CODE (nt) == TREE_VEC)
5937 /* For member templates */
5938 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5939 else if (PACK_EXPANSION_P (ot))
5940 return PACK_EXPANSION_P (nt)
5941 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5942 PACK_EXPANSION_PATTERN (nt));
5943 else if (ARGUMENT_PACK_P (ot))
5945 int i, len;
5946 tree opack, npack;
5948 if (!ARGUMENT_PACK_P (nt))
5949 return 0;
5951 opack = ARGUMENT_PACK_ARGS (ot);
5952 npack = ARGUMENT_PACK_ARGS (nt);
5953 len = TREE_VEC_LENGTH (opack);
5954 if (TREE_VEC_LENGTH (npack) != len)
5955 return 0;
5956 for (i = 0; i < len; ++i)
5957 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5958 TREE_VEC_ELT (npack, i)))
5959 return 0;
5960 return 1;
5962 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
5964 /* We get here probably because we are in the middle of substituting
5965 into the pattern of a pack expansion. In that case the
5966 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
5967 interested in. So we want to use the initial pack argument for
5968 the comparison. */
5969 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
5970 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
5971 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
5972 return template_args_equal (ot, nt);
5974 else if (TYPE_P (nt))
5975 return TYPE_P (ot) && same_type_p (ot, nt);
5976 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5977 return 0;
5978 else
5979 return cp_tree_equal (ot, nt);
5982 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5983 of template arguments. Returns 0 otherwise. */
5986 comp_template_args (tree oldargs, tree newargs)
5988 int i;
5990 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5991 return 0;
5993 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5995 tree nt = TREE_VEC_ELT (newargs, i);
5996 tree ot = TREE_VEC_ELT (oldargs, i);
5998 if (! template_args_equal (ot, nt))
5999 return 0;
6001 return 1;
6004 static void
6005 add_pending_template (tree d)
6007 tree ti = (TYPE_P (d)
6008 ? CLASSTYPE_TEMPLATE_INFO (d)
6009 : DECL_TEMPLATE_INFO (d));
6010 struct pending_template *pt;
6011 int level;
6013 if (TI_PENDING_TEMPLATE_FLAG (ti))
6014 return;
6016 /* We are called both from instantiate_decl, where we've already had a
6017 tinst_level pushed, and instantiate_template, where we haven't.
6018 Compensate. */
6019 level = !current_tinst_level || current_tinst_level->decl != d;
6021 if (level)
6022 push_tinst_level (d);
6024 pt = GGC_NEW (struct pending_template);
6025 pt->next = NULL;
6026 pt->tinst = current_tinst_level;
6027 if (last_pending_template)
6028 last_pending_template->next = pt;
6029 else
6030 pending_templates = pt;
6032 last_pending_template = pt;
6034 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6036 if (level)
6037 pop_tinst_level ();
6041 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6042 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6043 documentation for TEMPLATE_ID_EXPR. */
6045 tree
6046 lookup_template_function (tree fns, tree arglist)
6048 tree type;
6050 if (fns == error_mark_node || arglist == error_mark_node)
6051 return error_mark_node;
6053 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6054 gcc_assert (fns && (is_overloaded_fn (fns)
6055 || TREE_CODE (fns) == IDENTIFIER_NODE));
6057 if (BASELINK_P (fns))
6059 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6060 unknown_type_node,
6061 BASELINK_FUNCTIONS (fns),
6062 arglist);
6063 return fns;
6066 type = TREE_TYPE (fns);
6067 if (TREE_CODE (fns) == OVERLOAD || !type)
6068 type = unknown_type_node;
6070 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6073 /* Within the scope of a template class S<T>, the name S gets bound
6074 (in build_self_reference) to a TYPE_DECL for the class, not a
6075 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6076 or one of its enclosing classes, and that type is a template,
6077 return the associated TEMPLATE_DECL. Otherwise, the original
6078 DECL is returned.
6080 Also handle the case when DECL is a TREE_LIST of ambiguous
6081 injected-class-names from different bases. */
6083 tree
6084 maybe_get_template_decl_from_type_decl (tree decl)
6086 if (decl == NULL_TREE)
6087 return decl;
6089 /* DR 176: A lookup that finds an injected-class-name (10.2
6090 [class.member.lookup]) can result in an ambiguity in certain cases
6091 (for example, if it is found in more than one base class). If all of
6092 the injected-class-names that are found refer to specializations of
6093 the same class template, and if the name is followed by a
6094 template-argument-list, the reference refers to the class template
6095 itself and not a specialization thereof, and is not ambiguous. */
6096 if (TREE_CODE (decl) == TREE_LIST)
6098 tree t, tmpl = NULL_TREE;
6099 for (t = decl; t; t = TREE_CHAIN (t))
6101 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6102 if (!tmpl)
6103 tmpl = elt;
6104 else if (tmpl != elt)
6105 break;
6107 if (tmpl && t == NULL_TREE)
6108 return tmpl;
6109 else
6110 return decl;
6113 return (decl != NULL_TREE
6114 && DECL_SELF_REFERENCE_P (decl)
6115 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6116 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6119 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6120 parameters, find the desired type.
6122 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6124 IN_DECL, if non-NULL, is the template declaration we are trying to
6125 instantiate.
6127 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6128 the class we are looking up.
6130 Issue error and warning messages under control of COMPLAIN.
6132 If the template class is really a local class in a template
6133 function, then the FUNCTION_CONTEXT is the function in which it is
6134 being instantiated.
6136 ??? Note that this function is currently called *twice* for each
6137 template-id: the first time from the parser, while creating the
6138 incomplete type (finish_template_type), and the second type during the
6139 real instantiation (instantiate_template_class). This is surely something
6140 that we want to avoid. It also causes some problems with argument
6141 coercion (see convert_nontype_argument for more information on this). */
6143 tree
6144 lookup_template_class (tree d1,
6145 tree arglist,
6146 tree in_decl,
6147 tree context,
6148 int entering_scope,
6149 tsubst_flags_t complain)
6151 tree templ = NULL_TREE, parmlist;
6152 tree t;
6153 spec_entry **slot;
6154 spec_entry *entry;
6155 spec_entry elt;
6156 hashval_t hash;
6158 timevar_push (TV_NAME_LOOKUP);
6160 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6162 tree value = innermost_non_namespace_value (d1);
6163 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6164 templ = value;
6165 else
6167 if (context)
6168 push_decl_namespace (context);
6169 templ = lookup_name (d1);
6170 templ = maybe_get_template_decl_from_type_decl (templ);
6171 if (context)
6172 pop_decl_namespace ();
6174 if (templ)
6175 context = DECL_CONTEXT (templ);
6177 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6179 tree type = TREE_TYPE (d1);
6181 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6182 an implicit typename for the second A. Deal with it. */
6183 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6184 type = TREE_TYPE (type);
6186 if (CLASSTYPE_TEMPLATE_INFO (type))
6188 templ = CLASSTYPE_TI_TEMPLATE (type);
6189 d1 = DECL_NAME (templ);
6192 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6193 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6195 templ = TYPE_TI_TEMPLATE (d1);
6196 d1 = DECL_NAME (templ);
6198 else if (TREE_CODE (d1) == TEMPLATE_DECL
6199 && DECL_TEMPLATE_RESULT (d1)
6200 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6202 templ = d1;
6203 d1 = DECL_NAME (templ);
6204 context = DECL_CONTEXT (templ);
6207 /* Issue an error message if we didn't find a template. */
6208 if (! templ)
6210 if (complain & tf_error)
6211 error ("%qT is not a template", d1);
6212 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6215 if (TREE_CODE (templ) != TEMPLATE_DECL
6216 /* Make sure it's a user visible template, if it was named by
6217 the user. */
6218 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6219 && !PRIMARY_TEMPLATE_P (templ)))
6221 if (complain & tf_error)
6223 error ("non-template type %qT used as a template", d1);
6224 if (in_decl)
6225 error ("for template declaration %q+D", in_decl);
6227 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6230 complain &= ~tf_user;
6232 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6234 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6235 template arguments */
6237 tree parm;
6238 tree arglist2;
6239 tree outer;
6241 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6243 /* Consider an example where a template template parameter declared as
6245 template <class T, class U = std::allocator<T> > class TT
6247 The template parameter level of T and U are one level larger than
6248 of TT. To proper process the default argument of U, say when an
6249 instantiation `TT<int>' is seen, we need to build the full
6250 arguments containing {int} as the innermost level. Outer levels,
6251 available when not appearing as default template argument, can be
6252 obtained from the arguments of the enclosing template.
6254 Suppose that TT is later substituted with std::vector. The above
6255 instantiation is `TT<int, std::allocator<T> >' with TT at
6256 level 1, and T at level 2, while the template arguments at level 1
6257 becomes {std::vector} and the inner level 2 is {int}. */
6259 outer = DECL_CONTEXT (templ);
6260 if (outer)
6261 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6262 else if (current_template_parms)
6263 /* This is an argument of the current template, so we haven't set
6264 DECL_CONTEXT yet. */
6265 outer = current_template_args ();
6267 if (outer)
6268 arglist = add_to_template_args (outer, arglist);
6270 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6271 complain,
6272 /*require_all_args=*/true,
6273 /*use_default_args=*/true);
6274 if (arglist2 == error_mark_node
6275 || (!uses_template_parms (arglist2)
6276 && check_instantiated_args (templ, arglist2, complain)))
6277 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6279 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6280 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6282 else
6284 tree template_type = TREE_TYPE (templ);
6285 tree gen_tmpl;
6286 tree type_decl;
6287 tree found = NULL_TREE;
6288 int arg_depth;
6289 int parm_depth;
6290 int is_partial_instantiation;
6292 gen_tmpl = most_general_template (templ);
6293 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6294 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6295 arg_depth = TMPL_ARGS_DEPTH (arglist);
6297 if (arg_depth == 1 && parm_depth > 1)
6299 /* We've been given an incomplete set of template arguments.
6300 For example, given:
6302 template <class T> struct S1 {
6303 template <class U> struct S2 {};
6304 template <class U> struct S2<U*> {};
6307 we will be called with an ARGLIST of `U*', but the
6308 TEMPLATE will be `template <class T> template
6309 <class U> struct S1<T>::S2'. We must fill in the missing
6310 arguments. */
6311 arglist
6312 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6313 arglist);
6314 arg_depth = TMPL_ARGS_DEPTH (arglist);
6317 /* Now we should have enough arguments. */
6318 gcc_assert (parm_depth == arg_depth);
6320 /* From here on, we're only interested in the most general
6321 template. */
6323 /* Calculate the BOUND_ARGS. These will be the args that are
6324 actually tsubst'd into the definition to create the
6325 instantiation. */
6326 if (parm_depth > 1)
6328 /* We have multiple levels of arguments to coerce, at once. */
6329 int i;
6330 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6332 tree bound_args = make_tree_vec (parm_depth);
6334 for (i = saved_depth,
6335 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6336 i > 0 && t != NULL_TREE;
6337 --i, t = TREE_CHAIN (t))
6339 tree a = coerce_template_parms (TREE_VALUE (t),
6340 arglist, gen_tmpl,
6341 complain,
6342 /*require_all_args=*/true,
6343 /*use_default_args=*/true);
6345 /* Don't process further if one of the levels fails. */
6346 if (a == error_mark_node)
6348 /* Restore the ARGLIST to its full size. */
6349 TREE_VEC_LENGTH (arglist) = saved_depth;
6350 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6353 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6355 /* We temporarily reduce the length of the ARGLIST so
6356 that coerce_template_parms will see only the arguments
6357 corresponding to the template parameters it is
6358 examining. */
6359 TREE_VEC_LENGTH (arglist)--;
6362 /* Restore the ARGLIST to its full size. */
6363 TREE_VEC_LENGTH (arglist) = saved_depth;
6365 arglist = bound_args;
6367 else
6368 arglist
6369 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6370 INNERMOST_TEMPLATE_ARGS (arglist),
6371 gen_tmpl,
6372 complain,
6373 /*require_all_args=*/true,
6374 /*use_default_args=*/true);
6376 if (arglist == error_mark_node)
6377 /* We were unable to bind the arguments. */
6378 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6380 /* In the scope of a template class, explicit references to the
6381 template class refer to the type of the template, not any
6382 instantiation of it. For example, in:
6384 template <class T> class C { void f(C<T>); }
6386 the `C<T>' is just the same as `C'. Outside of the
6387 class, however, such a reference is an instantiation. */
6388 if ((entering_scope
6389 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6390 || currently_open_class (template_type))
6391 /* comp_template_args is expensive, check it last. */
6392 && comp_template_args (TYPE_TI_ARGS (template_type),
6393 arglist))
6394 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6396 /* If we already have this specialization, return it. */
6397 elt.tmpl = gen_tmpl;
6398 elt.args = arglist;
6399 hash = hash_specialization (&elt);
6400 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6401 &elt, hash);
6403 if (entry)
6404 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6406 /* This type is a "partial instantiation" if any of the template
6407 arguments still involve template parameters. Note that we set
6408 IS_PARTIAL_INSTANTIATION for partial specializations as
6409 well. */
6410 is_partial_instantiation = uses_template_parms (arglist);
6412 /* If the deduced arguments are invalid, then the binding
6413 failed. */
6414 if (!is_partial_instantiation
6415 && check_instantiated_args (gen_tmpl,
6416 INNERMOST_TEMPLATE_ARGS (arglist),
6417 complain))
6418 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6420 if (!is_partial_instantiation
6421 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6422 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6423 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6425 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6426 DECL_NAME (gen_tmpl),
6427 /*tag_scope=*/ts_global);
6428 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6431 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6432 complain, in_decl);
6433 if (!context)
6434 context = global_namespace;
6436 /* Create the type. */
6437 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6439 if (!is_partial_instantiation)
6441 set_current_access_from_decl (TYPE_NAME (template_type));
6442 t = start_enum (TYPE_IDENTIFIER (template_type),
6443 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6444 arglist, complain, in_decl),
6445 SCOPED_ENUM_P (template_type));
6447 else
6449 /* We don't want to call start_enum for this type, since
6450 the values for the enumeration constants may involve
6451 template parameters. And, no one should be interested
6452 in the enumeration constants for such a type. */
6453 t = cxx_make_type (ENUMERAL_TYPE);
6454 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6457 else
6459 t = make_class_type (TREE_CODE (template_type));
6460 CLASSTYPE_DECLARED_CLASS (t)
6461 = CLASSTYPE_DECLARED_CLASS (template_type);
6462 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6463 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6465 /* A local class. Make sure the decl gets registered properly. */
6466 if (context == current_function_decl)
6467 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6469 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6470 /* This instantiation is another name for the primary
6471 template type. Set the TYPE_CANONICAL field
6472 appropriately. */
6473 TYPE_CANONICAL (t) = template_type;
6474 else if (any_template_arguments_need_structural_equality_p (arglist))
6475 /* Some of the template arguments require structural
6476 equality testing, so this template class requires
6477 structural equality testing. */
6478 SET_TYPE_STRUCTURAL_EQUALITY (t);
6481 /* If we called start_enum or pushtag above, this information
6482 will already be set up. */
6483 if (!TYPE_NAME (t))
6485 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6487 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6488 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6489 DECL_SOURCE_LOCATION (type_decl)
6490 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6492 else
6493 type_decl = TYPE_NAME (t);
6495 TREE_PRIVATE (type_decl)
6496 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6497 TREE_PROTECTED (type_decl)
6498 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6499 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6501 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6502 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6505 /* Set up the template information. We have to figure out which
6506 template is the immediate parent if this is a full
6507 instantiation. */
6508 if (parm_depth == 1 || is_partial_instantiation
6509 || !PRIMARY_TEMPLATE_P (gen_tmpl))
6510 /* This case is easy; there are no member templates involved. */
6511 found = gen_tmpl;
6512 else
6514 /* This is a full instantiation of a member template. Find
6515 the partial instantiation of which this is an instance. */
6517 /* Temporarily reduce by one the number of levels in the ARGLIST
6518 so as to avoid comparing the last set of arguments. */
6519 TREE_VEC_LENGTH (arglist)--;
6520 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6521 TREE_VEC_LENGTH (arglist)++;
6522 found = CLASSTYPE_TI_TEMPLATE (found);
6525 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6527 elt.spec = t;
6528 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6529 &elt, hash, INSERT);
6530 *slot = GGC_NEW (spec_entry);
6531 **slot = elt;
6533 /* Note this use of the partial instantiation so we can check it
6534 later in maybe_process_partial_specialization. */
6535 DECL_TEMPLATE_INSTANTIATIONS (templ)
6536 = tree_cons (arglist, t,
6537 DECL_TEMPLATE_INSTANTIATIONS (templ));
6539 if (TREE_CODE (t) == ENUMERAL_TYPE
6540 && !is_partial_instantiation)
6541 /* Now that the type has been registered on the instantiations
6542 list, we set up the enumerators. Because the enumeration
6543 constants may involve the enumeration type itself, we make
6544 sure to register the type first, and then create the
6545 constants. That way, doing tsubst_expr for the enumeration
6546 constants won't result in recursive calls here; we'll find
6547 the instantiation and exit above. */
6548 tsubst_enum (template_type, t, arglist);
6550 if (is_partial_instantiation)
6551 /* If the type makes use of template parameters, the
6552 code that generates debugging information will crash. */
6553 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6555 /* Possibly limit visibility based on template args. */
6556 TREE_PUBLIC (type_decl) = 1;
6557 determine_visibility (type_decl);
6559 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6561 timevar_pop (TV_NAME_LOOKUP);
6564 struct pair_fn_data
6566 tree_fn_t fn;
6567 void *data;
6568 /* True when we should also visit template parameters that occur in
6569 non-deduced contexts. */
6570 bool include_nondeduced_p;
6571 struct pointer_set_t *visited;
6574 /* Called from for_each_template_parm via walk_tree. */
6576 static tree
6577 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6579 tree t = *tp;
6580 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6581 tree_fn_t fn = pfd->fn;
6582 void *data = pfd->data;
6584 if (TYPE_P (t)
6585 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6586 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6587 pfd->include_nondeduced_p))
6588 return error_mark_node;
6590 switch (TREE_CODE (t))
6592 case RECORD_TYPE:
6593 if (TYPE_PTRMEMFUNC_P (t))
6594 break;
6595 /* Fall through. */
6597 case UNION_TYPE:
6598 case ENUMERAL_TYPE:
6599 if (!TYPE_TEMPLATE_INFO (t))
6600 *walk_subtrees = 0;
6601 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6602 fn, data, pfd->visited,
6603 pfd->include_nondeduced_p))
6604 return error_mark_node;
6605 break;
6607 case INTEGER_TYPE:
6608 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6609 fn, data, pfd->visited,
6610 pfd->include_nondeduced_p)
6611 || for_each_template_parm (TYPE_MAX_VALUE (t),
6612 fn, data, pfd->visited,
6613 pfd->include_nondeduced_p))
6614 return error_mark_node;
6615 break;
6617 case METHOD_TYPE:
6618 /* Since we're not going to walk subtrees, we have to do this
6619 explicitly here. */
6620 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6621 pfd->visited, pfd->include_nondeduced_p))
6622 return error_mark_node;
6623 /* Fall through. */
6625 case FUNCTION_TYPE:
6626 /* Check the return type. */
6627 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6628 pfd->include_nondeduced_p))
6629 return error_mark_node;
6631 /* Check the parameter types. Since default arguments are not
6632 instantiated until they are needed, the TYPE_ARG_TYPES may
6633 contain expressions that involve template parameters. But,
6634 no-one should be looking at them yet. And, once they're
6635 instantiated, they don't contain template parameters, so
6636 there's no point in looking at them then, either. */
6638 tree parm;
6640 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6641 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6642 pfd->visited, pfd->include_nondeduced_p))
6643 return error_mark_node;
6645 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6646 want walk_tree walking into them itself. */
6647 *walk_subtrees = 0;
6649 break;
6651 case TYPEOF_TYPE:
6652 if (pfd->include_nondeduced_p
6653 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6654 pfd->visited,
6655 pfd->include_nondeduced_p))
6656 return error_mark_node;
6657 break;
6659 case FUNCTION_DECL:
6660 case VAR_DECL:
6661 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6662 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6663 pfd->visited, pfd->include_nondeduced_p))
6664 return error_mark_node;
6665 /* Fall through. */
6667 case PARM_DECL:
6668 case CONST_DECL:
6669 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6670 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6671 pfd->visited, pfd->include_nondeduced_p))
6672 return error_mark_node;
6673 if (DECL_CONTEXT (t)
6674 && pfd->include_nondeduced_p
6675 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6676 pfd->visited, pfd->include_nondeduced_p))
6677 return error_mark_node;
6678 break;
6680 case BOUND_TEMPLATE_TEMPLATE_PARM:
6681 /* Record template parameters such as `T' inside `TT<T>'. */
6682 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6683 pfd->include_nondeduced_p))
6684 return error_mark_node;
6685 /* Fall through. */
6687 case TEMPLATE_TEMPLATE_PARM:
6688 case TEMPLATE_TYPE_PARM:
6689 case TEMPLATE_PARM_INDEX:
6690 if (fn && (*fn)(t, data))
6691 return error_mark_node;
6692 else if (!fn)
6693 return error_mark_node;
6694 break;
6696 case TEMPLATE_DECL:
6697 /* A template template parameter is encountered. */
6698 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6699 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6700 pfd->include_nondeduced_p))
6701 return error_mark_node;
6703 /* Already substituted template template parameter */
6704 *walk_subtrees = 0;
6705 break;
6707 case TYPENAME_TYPE:
6708 if (!fn
6709 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6710 data, pfd->visited,
6711 pfd->include_nondeduced_p))
6712 return error_mark_node;
6713 break;
6715 case CONSTRUCTOR:
6716 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6717 && pfd->include_nondeduced_p
6718 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6719 (TREE_TYPE (t)), fn, data,
6720 pfd->visited, pfd->include_nondeduced_p))
6721 return error_mark_node;
6722 break;
6724 case INDIRECT_REF:
6725 case COMPONENT_REF:
6726 /* If there's no type, then this thing must be some expression
6727 involving template parameters. */
6728 if (!fn && !TREE_TYPE (t))
6729 return error_mark_node;
6730 break;
6732 case MODOP_EXPR:
6733 case CAST_EXPR:
6734 case REINTERPRET_CAST_EXPR:
6735 case CONST_CAST_EXPR:
6736 case STATIC_CAST_EXPR:
6737 case DYNAMIC_CAST_EXPR:
6738 case ARROW_EXPR:
6739 case DOTSTAR_EXPR:
6740 case TYPEID_EXPR:
6741 case PSEUDO_DTOR_EXPR:
6742 if (!fn)
6743 return error_mark_node;
6744 break;
6746 default:
6747 break;
6750 /* We didn't find any template parameters we liked. */
6751 return NULL_TREE;
6754 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6755 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6756 call FN with the parameter and the DATA.
6757 If FN returns nonzero, the iteration is terminated, and
6758 for_each_template_parm returns 1. Otherwise, the iteration
6759 continues. If FN never returns a nonzero value, the value
6760 returned by for_each_template_parm is 0. If FN is NULL, it is
6761 considered to be the function which always returns 1.
6763 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6764 parameters that occur in non-deduced contexts. When false, only
6765 visits those template parameters that can be deduced. */
6767 static int
6768 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6769 struct pointer_set_t *visited,
6770 bool include_nondeduced_p)
6772 struct pair_fn_data pfd;
6773 int result;
6775 /* Set up. */
6776 pfd.fn = fn;
6777 pfd.data = data;
6778 pfd.include_nondeduced_p = include_nondeduced_p;
6780 /* Walk the tree. (Conceptually, we would like to walk without
6781 duplicates, but for_each_template_parm_r recursively calls
6782 for_each_template_parm, so we would need to reorganize a fair
6783 bit to use walk_tree_without_duplicates, so we keep our own
6784 visited list.) */
6785 if (visited)
6786 pfd.visited = visited;
6787 else
6788 pfd.visited = pointer_set_create ();
6789 result = cp_walk_tree (&t,
6790 for_each_template_parm_r,
6791 &pfd,
6792 pfd.visited) != NULL_TREE;
6794 /* Clean up. */
6795 if (!visited)
6797 pointer_set_destroy (pfd.visited);
6798 pfd.visited = 0;
6801 return result;
6804 /* Returns true if T depends on any template parameter. */
6807 uses_template_parms (tree t)
6809 bool dependent_p;
6810 int saved_processing_template_decl;
6812 saved_processing_template_decl = processing_template_decl;
6813 if (!saved_processing_template_decl)
6814 processing_template_decl = 1;
6815 if (TYPE_P (t))
6816 dependent_p = dependent_type_p (t);
6817 else if (TREE_CODE (t) == TREE_VEC)
6818 dependent_p = any_dependent_template_arguments_p (t);
6819 else if (TREE_CODE (t) == TREE_LIST)
6820 dependent_p = (uses_template_parms (TREE_VALUE (t))
6821 || uses_template_parms (TREE_CHAIN (t)));
6822 else if (TREE_CODE (t) == TYPE_DECL)
6823 dependent_p = dependent_type_p (TREE_TYPE (t));
6824 else if (DECL_P (t)
6825 || EXPR_P (t)
6826 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6827 || TREE_CODE (t) == OVERLOAD
6828 || TREE_CODE (t) == BASELINK
6829 || TREE_CODE (t) == IDENTIFIER_NODE
6830 || TREE_CODE (t) == TRAIT_EXPR
6831 || TREE_CODE (t) == CONSTRUCTOR
6832 || CONSTANT_CLASS_P (t))
6833 dependent_p = (type_dependent_expression_p (t)
6834 || value_dependent_expression_p (t));
6835 else
6837 gcc_assert (t == error_mark_node);
6838 dependent_p = false;
6841 processing_template_decl = saved_processing_template_decl;
6843 return dependent_p;
6846 /* Returns true if T depends on any template parameter with level LEVEL. */
6849 uses_template_parms_level (tree t, int level)
6851 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6852 /*include_nondeduced_p=*/true);
6855 static int tinst_depth;
6856 extern int max_tinst_depth;
6857 #ifdef GATHER_STATISTICS
6858 int depth_reached;
6859 #endif
6860 static int tinst_level_tick;
6861 static int last_template_error_tick;
6863 /* We're starting to instantiate D; record the template instantiation context
6864 for diagnostics and to restore it later. */
6866 static int
6867 push_tinst_level (tree d)
6869 struct tinst_level *new_level;
6871 if (tinst_depth >= max_tinst_depth)
6873 /* If the instantiation in question still has unbound template parms,
6874 we don't really care if we can't instantiate it, so just return.
6875 This happens with base instantiation for implicit `typename'. */
6876 if (uses_template_parms (d))
6877 return 0;
6879 last_template_error_tick = tinst_level_tick;
6880 error ("template instantiation depth exceeds maximum of %d (use "
6881 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6882 max_tinst_depth, d);
6884 print_instantiation_context ();
6886 return 0;
6889 new_level = GGC_NEW (struct tinst_level);
6890 new_level->decl = d;
6891 new_level->locus = input_location;
6892 new_level->in_system_header_p = in_system_header;
6893 new_level->next = current_tinst_level;
6894 current_tinst_level = new_level;
6896 ++tinst_depth;
6897 #ifdef GATHER_STATISTICS
6898 if (tinst_depth > depth_reached)
6899 depth_reached = tinst_depth;
6900 #endif
6902 ++tinst_level_tick;
6903 return 1;
6906 /* We're done instantiating this template; return to the instantiation
6907 context. */
6909 static void
6910 pop_tinst_level (void)
6912 /* Restore the filename and line number stashed away when we started
6913 this instantiation. */
6914 input_location = current_tinst_level->locus;
6915 current_tinst_level = current_tinst_level->next;
6916 --tinst_depth;
6917 ++tinst_level_tick;
6920 /* We're instantiating a deferred template; restore the template
6921 instantiation context in which the instantiation was requested, which
6922 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6924 static tree
6925 reopen_tinst_level (struct tinst_level *level)
6927 struct tinst_level *t;
6929 tinst_depth = 0;
6930 for (t = level; t; t = t->next)
6931 ++tinst_depth;
6933 current_tinst_level = level;
6934 pop_tinst_level ();
6935 return level->decl;
6938 /* Returns the TINST_LEVEL which gives the original instantiation
6939 context. */
6941 struct tinst_level *
6942 outermost_tinst_level (void)
6944 struct tinst_level *level = current_tinst_level;
6945 if (level)
6946 while (level->next)
6947 level = level->next;
6948 return level;
6951 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6953 bool
6954 parameter_of_template_p (tree parm, tree templ)
6956 tree parms;
6957 int i;
6959 if (!parm || !templ)
6960 return false;
6962 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6963 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6965 parms = DECL_TEMPLATE_PARMS (templ);
6966 parms = INNERMOST_TEMPLATE_PARMS (parms);
6968 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6969 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6970 return true;
6972 return false;
6975 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6976 vector of template arguments, as for tsubst.
6978 Returns an appropriate tsubst'd friend declaration. */
6980 static tree
6981 tsubst_friend_function (tree decl, tree args)
6983 tree new_friend;
6985 if (TREE_CODE (decl) == FUNCTION_DECL
6986 && DECL_TEMPLATE_INSTANTIATION (decl)
6987 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6988 /* This was a friend declared with an explicit template
6989 argument list, e.g.:
6991 friend void f<>(T);
6993 to indicate that f was a template instantiation, not a new
6994 function declaration. Now, we have to figure out what
6995 instantiation of what template. */
6997 tree template_id, arglist, fns;
6998 tree new_args;
6999 tree tmpl;
7000 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7002 /* Friend functions are looked up in the containing namespace scope.
7003 We must enter that scope, to avoid finding member functions of the
7004 current class with same name. */
7005 push_nested_namespace (ns);
7006 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7007 tf_warning_or_error, NULL_TREE,
7008 /*integral_constant_expression_p=*/false);
7009 pop_nested_namespace (ns);
7010 arglist = tsubst (DECL_TI_ARGS (decl), args,
7011 tf_warning_or_error, NULL_TREE);
7012 template_id = lookup_template_function (fns, arglist);
7014 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7015 tmpl = determine_specialization (template_id, new_friend,
7016 &new_args,
7017 /*need_member_template=*/0,
7018 TREE_VEC_LENGTH (args),
7019 tsk_none);
7020 return instantiate_template (tmpl, new_args, tf_error);
7023 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7025 /* The NEW_FRIEND will look like an instantiation, to the
7026 compiler, but is not an instantiation from the point of view of
7027 the language. For example, we might have had:
7029 template <class T> struct S {
7030 template <class U> friend void f(T, U);
7033 Then, in S<int>, template <class U> void f(int, U) is not an
7034 instantiation of anything. */
7035 if (new_friend == error_mark_node)
7036 return error_mark_node;
7038 DECL_USE_TEMPLATE (new_friend) = 0;
7039 if (TREE_CODE (decl) == TEMPLATE_DECL)
7041 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7042 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7043 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7046 /* The mangled name for the NEW_FRIEND is incorrect. The function
7047 is not a template instantiation and should not be mangled like
7048 one. Therefore, we forget the mangling here; we'll recompute it
7049 later if we need it. */
7050 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7052 SET_DECL_RTL (new_friend, NULL_RTX);
7053 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7056 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7058 tree old_decl;
7059 tree new_friend_template_info;
7060 tree new_friend_result_template_info;
7061 tree ns;
7062 int new_friend_is_defn;
7064 /* We must save some information from NEW_FRIEND before calling
7065 duplicate decls since that function will free NEW_FRIEND if
7066 possible. */
7067 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7068 new_friend_is_defn =
7069 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7070 (template_for_substitution (new_friend)))
7071 != NULL_TREE);
7072 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7074 /* This declaration is a `primary' template. */
7075 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7077 new_friend_result_template_info
7078 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7080 else
7081 new_friend_result_template_info = NULL_TREE;
7083 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7084 if (new_friend_is_defn)
7085 DECL_INITIAL (new_friend) = error_mark_node;
7087 /* Inside pushdecl_namespace_level, we will push into the
7088 current namespace. However, the friend function should go
7089 into the namespace of the template. */
7090 ns = decl_namespace_context (new_friend);
7091 push_nested_namespace (ns);
7092 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7093 pop_nested_namespace (ns);
7095 if (old_decl == error_mark_node)
7096 return error_mark_node;
7098 if (old_decl != new_friend)
7100 /* This new friend declaration matched an existing
7101 declaration. For example, given:
7103 template <class T> void f(T);
7104 template <class U> class C {
7105 template <class T> friend void f(T) {}
7108 the friend declaration actually provides the definition
7109 of `f', once C has been instantiated for some type. So,
7110 old_decl will be the out-of-class template declaration,
7111 while new_friend is the in-class definition.
7113 But, if `f' was called before this point, the
7114 instantiation of `f' will have DECL_TI_ARGS corresponding
7115 to `T' but not to `U', references to which might appear
7116 in the definition of `f'. Previously, the most general
7117 template for an instantiation of `f' was the out-of-class
7118 version; now it is the in-class version. Therefore, we
7119 run through all specialization of `f', adding to their
7120 DECL_TI_ARGS appropriately. In particular, they need a
7121 new set of outer arguments, corresponding to the
7122 arguments for this class instantiation.
7124 The same situation can arise with something like this:
7126 friend void f(int);
7127 template <class T> class C {
7128 friend void f(T) {}
7131 when `C<int>' is instantiated. Now, `f(int)' is defined
7132 in the class. */
7134 if (!new_friend_is_defn)
7135 /* On the other hand, if the in-class declaration does
7136 *not* provide a definition, then we don't want to alter
7137 existing definitions. We can just leave everything
7138 alone. */
7140 else
7142 tree new_template = TI_TEMPLATE (new_friend_template_info);
7143 tree new_args = TI_ARGS (new_friend_template_info);
7145 /* Overwrite whatever template info was there before, if
7146 any, with the new template information pertaining to
7147 the declaration. */
7148 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7150 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7151 /* We should have called reregister_specialization in
7152 duplicate_decls. */
7153 gcc_assert (retrieve_specialization (new_template,
7154 new_args, 0)
7155 == old_decl);
7156 else
7158 tree t;
7160 /* Indicate that the old function template is a partial
7161 instantiation. */
7162 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7163 = new_friend_result_template_info;
7165 gcc_assert (new_template
7166 == most_general_template (new_template));
7167 gcc_assert (new_template != old_decl);
7169 /* Reassign any specializations already in the hash table
7170 to the new more general template, and add the
7171 additional template args. */
7172 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7173 t != NULL_TREE;
7174 t = TREE_CHAIN (t))
7176 tree spec = TREE_VALUE (t);
7177 spec_entry elt;
7179 elt.tmpl = old_decl;
7180 elt.args = DECL_TI_ARGS (spec);
7181 elt.spec = NULL_TREE;
7183 htab_remove_elt (decl_specializations, &elt);
7185 DECL_TI_ARGS (spec)
7186 = add_outermost_template_args (new_args,
7187 DECL_TI_ARGS (spec));
7189 register_specialization
7190 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7193 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7197 /* The information from NEW_FRIEND has been merged into OLD_DECL
7198 by duplicate_decls. */
7199 new_friend = old_decl;
7202 else
7204 tree context = DECL_CONTEXT (new_friend);
7205 bool dependent_p;
7207 /* In the code
7208 template <class T> class C {
7209 template <class U> friend void C1<U>::f (); // case 1
7210 friend void C2<T>::f (); // case 2
7212 we only need to make sure CONTEXT is a complete type for
7213 case 2. To distinguish between the two cases, we note that
7214 CONTEXT of case 1 remains dependent type after tsubst while
7215 this isn't true for case 2. */
7216 ++processing_template_decl;
7217 dependent_p = dependent_type_p (context);
7218 --processing_template_decl;
7220 if (!dependent_p
7221 && !complete_type_or_else (context, NULL_TREE))
7222 return error_mark_node;
7224 if (COMPLETE_TYPE_P (context))
7226 /* Check to see that the declaration is really present, and,
7227 possibly obtain an improved declaration. */
7228 tree fn = check_classfn (context,
7229 new_friend, NULL_TREE);
7231 if (fn)
7232 new_friend = fn;
7236 return new_friend;
7239 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7240 template arguments, as for tsubst.
7242 Returns an appropriate tsubst'd friend type or error_mark_node on
7243 failure. */
7245 static tree
7246 tsubst_friend_class (tree friend_tmpl, tree args)
7248 tree friend_type;
7249 tree tmpl;
7250 tree context;
7252 context = DECL_CONTEXT (friend_tmpl);
7254 if (context)
7256 if (TREE_CODE (context) == NAMESPACE_DECL)
7257 push_nested_namespace (context);
7258 else
7259 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7262 /* Look for a class template declaration. We look for hidden names
7263 because two friend declarations of the same template are the
7264 same. For example, in:
7266 struct A {
7267 template <typename> friend class F;
7269 template <typename> struct B {
7270 template <typename> friend class F;
7273 both F templates are the same. */
7274 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7275 /*block_p=*/true, 0,
7276 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7278 /* But, if we don't find one, it might be because we're in a
7279 situation like this:
7281 template <class T>
7282 struct S {
7283 template <class U>
7284 friend struct S;
7287 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7288 for `S<int>', not the TEMPLATE_DECL. */
7289 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7291 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7292 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7295 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7297 /* The friend template has already been declared. Just
7298 check to see that the declarations match, and install any new
7299 default parameters. We must tsubst the default parameters,
7300 of course. We only need the innermost template parameters
7301 because that is all that redeclare_class_template will look
7302 at. */
7303 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7304 > TMPL_ARGS_DEPTH (args))
7306 tree parms;
7307 location_t saved_input_location;
7308 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7309 args, tf_warning_or_error);
7311 saved_input_location = input_location;
7312 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7313 redeclare_class_template (TREE_TYPE (tmpl), parms);
7314 input_location = saved_input_location;
7318 friend_type = TREE_TYPE (tmpl);
7320 else
7322 /* The friend template has not already been declared. In this
7323 case, the instantiation of the template class will cause the
7324 injection of this template into the global scope. */
7325 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7326 if (tmpl == error_mark_node)
7327 return error_mark_node;
7329 /* The new TMPL is not an instantiation of anything, so we
7330 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7331 the new type because that is supposed to be the corresponding
7332 template decl, i.e., TMPL. */
7333 DECL_USE_TEMPLATE (tmpl) = 0;
7334 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7335 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7336 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7337 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7339 /* Inject this template into the global scope. */
7340 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7343 if (context)
7345 if (TREE_CODE (context) == NAMESPACE_DECL)
7346 pop_nested_namespace (context);
7347 else
7348 pop_nested_class ();
7351 return friend_type;
7354 /* Returns zero if TYPE cannot be completed later due to circularity.
7355 Otherwise returns one. */
7357 static int
7358 can_complete_type_without_circularity (tree type)
7360 if (type == NULL_TREE || type == error_mark_node)
7361 return 0;
7362 else if (COMPLETE_TYPE_P (type))
7363 return 1;
7364 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7365 return can_complete_type_without_circularity (TREE_TYPE (type));
7366 else if (CLASS_TYPE_P (type)
7367 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7368 return 0;
7369 else
7370 return 1;
7373 /* Apply any attributes which had to be deferred until instantiation
7374 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7375 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7377 static void
7378 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7379 tree args, tsubst_flags_t complain, tree in_decl)
7381 tree last_dep = NULL_TREE;
7382 tree t;
7383 tree *p;
7385 for (t = attributes; t; t = TREE_CHAIN (t))
7386 if (ATTR_IS_DEPENDENT (t))
7388 last_dep = t;
7389 attributes = copy_list (attributes);
7390 break;
7393 if (DECL_P (*decl_p))
7395 if (TREE_TYPE (*decl_p) == error_mark_node)
7396 return;
7397 p = &DECL_ATTRIBUTES (*decl_p);
7399 else
7400 p = &TYPE_ATTRIBUTES (*decl_p);
7402 if (last_dep)
7404 tree late_attrs = NULL_TREE;
7405 tree *q = &late_attrs;
7407 for (*p = attributes; *p; )
7409 t = *p;
7410 if (ATTR_IS_DEPENDENT (t))
7412 *p = TREE_CHAIN (t);
7413 TREE_CHAIN (t) = NULL_TREE;
7414 /* If the first attribute argument is an identifier, don't
7415 pass it through tsubst. Attributes like mode, format,
7416 cleanup and several target specific attributes expect it
7417 unmodified. */
7418 if (TREE_VALUE (t)
7419 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7420 && TREE_VALUE (TREE_VALUE (t))
7421 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7422 == IDENTIFIER_NODE))
7424 tree chain
7425 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7426 in_decl,
7427 /*integral_constant_expression_p=*/false);
7428 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7429 TREE_VALUE (t)
7430 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7431 chain);
7433 else
7434 TREE_VALUE (t)
7435 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7436 /*integral_constant_expression_p=*/false);
7437 *q = t;
7438 q = &TREE_CHAIN (t);
7440 else
7441 p = &TREE_CHAIN (t);
7444 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7448 /* Perform (or defer) access check for typedefs that were referenced
7449 from within the template TMPL code.
7450 This is a subroutine of instantiate_template and instantiate_class_template.
7451 TMPL is the template to consider and TARGS is the list of arguments of
7452 that template. */
7454 static void
7455 perform_typedefs_access_check (tree tmpl, tree targs)
7457 location_t saved_location;
7458 int i;
7459 qualified_typedef_usage_t *iter;
7461 if (!tmpl
7462 || (!CLASS_TYPE_P (tmpl)
7463 && TREE_CODE (tmpl) != FUNCTION_DECL))
7464 return;
7466 saved_location = input_location;
7467 for (i = 0;
7468 VEC_iterate (qualified_typedef_usage_t,
7469 get_types_needing_access_check (tmpl),
7470 i, iter);
7471 ++i)
7473 tree type_decl = iter->typedef_decl;
7474 tree type_scope = iter->context;
7476 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7477 continue;
7479 if (uses_template_parms (type_decl))
7480 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7481 if (uses_template_parms (type_scope))
7482 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7484 /* Make access check error messages point to the location
7485 of the use of the typedef. */
7486 input_location = iter->locus;
7487 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7488 type_decl, type_decl);
7490 input_location = saved_location;
7493 tree
7494 instantiate_class_template (tree type)
7496 tree templ, args, pattern, t, member;
7497 tree typedecl;
7498 tree pbinfo;
7499 tree base_list;
7500 unsigned int saved_maximum_field_alignment;
7502 if (type == error_mark_node)
7503 return error_mark_node;
7505 if (TYPE_BEING_DEFINED (type)
7506 || COMPLETE_TYPE_P (type)
7507 || uses_template_parms (type))
7508 return type;
7510 /* Figure out which template is being instantiated. */
7511 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7512 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7514 /* Determine what specialization of the original template to
7515 instantiate. */
7516 t = most_specialized_class (type, templ);
7517 if (t == error_mark_node)
7519 TYPE_BEING_DEFINED (type) = 1;
7520 return error_mark_node;
7522 else if (t)
7524 /* This TYPE is actually an instantiation of a partial
7525 specialization. We replace the innermost set of ARGS with
7526 the arguments appropriate for substitution. For example,
7527 given:
7529 template <class T> struct S {};
7530 template <class T> struct S<T*> {};
7532 and supposing that we are instantiating S<int*>, ARGS will
7533 presently be {int*} -- but we need {int}. */
7534 pattern = TREE_TYPE (t);
7535 args = TREE_PURPOSE (t);
7537 else
7539 pattern = TREE_TYPE (templ);
7540 args = CLASSTYPE_TI_ARGS (type);
7543 /* If the template we're instantiating is incomplete, then clearly
7544 there's nothing we can do. */
7545 if (!COMPLETE_TYPE_P (pattern))
7546 return type;
7548 /* If we've recursively instantiated too many templates, stop. */
7549 if (! push_tinst_level (type))
7550 return type;
7552 /* Now we're really doing the instantiation. Mark the type as in
7553 the process of being defined. */
7554 TYPE_BEING_DEFINED (type) = 1;
7556 /* We may be in the middle of deferred access check. Disable
7557 it now. */
7558 push_deferring_access_checks (dk_no_deferred);
7560 push_to_top_level ();
7561 /* Use #pragma pack from the template context. */
7562 saved_maximum_field_alignment = maximum_field_alignment;
7563 maximum_field_alignment = TYPE_PRECISION (pattern);
7565 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7567 /* Set the input location to the most specialized template definition.
7568 This is needed if tsubsting causes an error. */
7569 typedecl = TYPE_MAIN_DECL (pattern);
7570 input_location = DECL_SOURCE_LOCATION (typedecl);
7572 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7573 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7574 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7575 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7576 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7577 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7578 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7579 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7580 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7581 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7582 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7583 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7584 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7585 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7586 if (ANON_AGGR_TYPE_P (pattern))
7587 SET_ANON_AGGR_TYPE_P (type);
7588 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7590 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7591 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7594 pbinfo = TYPE_BINFO (pattern);
7596 /* We should never instantiate a nested class before its enclosing
7597 class; we need to look up the nested class by name before we can
7598 instantiate it, and that lookup should instantiate the enclosing
7599 class. */
7600 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7601 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7602 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7604 base_list = NULL_TREE;
7605 if (BINFO_N_BASE_BINFOS (pbinfo))
7607 tree pbase_binfo;
7608 tree context = TYPE_CONTEXT (type);
7609 tree pushed_scope;
7610 int i;
7612 /* We must enter the scope containing the type, as that is where
7613 the accessibility of types named in dependent bases are
7614 looked up from. */
7615 pushed_scope = push_scope (context ? context : global_namespace);
7617 /* Substitute into each of the bases to determine the actual
7618 basetypes. */
7619 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7621 tree base;
7622 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7623 tree expanded_bases = NULL_TREE;
7624 int idx, len = 1;
7626 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7628 expanded_bases =
7629 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7630 args, tf_error, NULL_TREE);
7631 if (expanded_bases == error_mark_node)
7632 continue;
7634 len = TREE_VEC_LENGTH (expanded_bases);
7637 for (idx = 0; idx < len; idx++)
7639 if (expanded_bases)
7640 /* Extract the already-expanded base class. */
7641 base = TREE_VEC_ELT (expanded_bases, idx);
7642 else
7643 /* Substitute to figure out the base class. */
7644 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7645 NULL_TREE);
7647 if (base == error_mark_node)
7648 continue;
7650 base_list = tree_cons (access, base, base_list);
7651 if (BINFO_VIRTUAL_P (pbase_binfo))
7652 TREE_TYPE (base_list) = integer_type_node;
7656 /* The list is now in reverse order; correct that. */
7657 base_list = nreverse (base_list);
7659 if (pushed_scope)
7660 pop_scope (pushed_scope);
7662 /* Now call xref_basetypes to set up all the base-class
7663 information. */
7664 xref_basetypes (type, base_list);
7666 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7667 (int) ATTR_FLAG_TYPE_IN_PLACE,
7668 args, tf_error, NULL_TREE);
7670 /* Now that our base classes are set up, enter the scope of the
7671 class, so that name lookups into base classes, etc. will work
7672 correctly. This is precisely analogous to what we do in
7673 begin_class_definition when defining an ordinary non-template
7674 class, except we also need to push the enclosing classes. */
7675 push_nested_class (type);
7677 /* Now members are processed in the order of declaration. */
7678 for (member = CLASSTYPE_DECL_LIST (pattern);
7679 member; member = TREE_CHAIN (member))
7681 tree t = TREE_VALUE (member);
7683 if (TREE_PURPOSE (member))
7685 if (TYPE_P (t))
7687 /* Build new CLASSTYPE_NESTED_UTDS. */
7689 tree newtag;
7690 bool class_template_p;
7692 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7693 && TYPE_LANG_SPECIFIC (t)
7694 && CLASSTYPE_IS_TEMPLATE (t));
7695 /* If the member is a class template, then -- even after
7696 substitution -- there may be dependent types in the
7697 template argument list for the class. We increment
7698 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7699 that function will assume that no types are dependent
7700 when outside of a template. */
7701 if (class_template_p)
7702 ++processing_template_decl;
7703 newtag = tsubst (t, args, tf_error, NULL_TREE);
7704 if (class_template_p)
7705 --processing_template_decl;
7706 if (newtag == error_mark_node)
7707 continue;
7709 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7711 tree name = TYPE_IDENTIFIER (t);
7713 if (class_template_p)
7714 /* Unfortunately, lookup_template_class sets
7715 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7716 instantiation (i.e., for the type of a member
7717 template class nested within a template class.)
7718 This behavior is required for
7719 maybe_process_partial_specialization to work
7720 correctly, but is not accurate in this case;
7721 the TAG is not an instantiation of anything.
7722 (The corresponding TEMPLATE_DECL is an
7723 instantiation, but the TYPE is not.) */
7724 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7726 /* Now, we call pushtag to put this NEWTAG into the scope of
7727 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7728 pushtag calling push_template_decl. We don't have to do
7729 this for enums because it will already have been done in
7730 tsubst_enum. */
7731 if (name)
7732 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7733 pushtag (name, newtag, /*tag_scope=*/ts_current);
7736 else if (TREE_CODE (t) == FUNCTION_DECL
7737 || DECL_FUNCTION_TEMPLATE_P (t))
7739 /* Build new TYPE_METHODS. */
7740 tree r;
7742 if (TREE_CODE (t) == TEMPLATE_DECL)
7743 ++processing_template_decl;
7744 r = tsubst (t, args, tf_error, NULL_TREE);
7745 if (TREE_CODE (t) == TEMPLATE_DECL)
7746 --processing_template_decl;
7747 set_current_access_from_decl (r);
7748 finish_member_declaration (r);
7750 else
7752 /* Build new TYPE_FIELDS. */
7753 if (TREE_CODE (t) == STATIC_ASSERT)
7755 tree condition =
7756 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7757 tf_warning_or_error, NULL_TREE,
7758 /*integral_constant_expression_p=*/true);
7759 finish_static_assert (condition,
7760 STATIC_ASSERT_MESSAGE (t),
7761 STATIC_ASSERT_SOURCE_LOCATION (t),
7762 /*member_p=*/true);
7764 else if (TREE_CODE (t) != CONST_DECL)
7766 tree r;
7768 /* The file and line for this declaration, to
7769 assist in error message reporting. Since we
7770 called push_tinst_level above, we don't need to
7771 restore these. */
7772 input_location = DECL_SOURCE_LOCATION (t);
7774 if (TREE_CODE (t) == TEMPLATE_DECL)
7775 ++processing_template_decl;
7776 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7777 if (TREE_CODE (t) == TEMPLATE_DECL)
7778 --processing_template_decl;
7779 if (TREE_CODE (r) == VAR_DECL)
7781 /* In [temp.inst]:
7783 [t]he initialization (and any associated
7784 side-effects) of a static data member does
7785 not occur unless the static data member is
7786 itself used in a way that requires the
7787 definition of the static data member to
7788 exist.
7790 Therefore, we do not substitute into the
7791 initialized for the static data member here. */
7792 finish_static_data_member_decl
7794 /*init=*/NULL_TREE,
7795 /*init_const_expr_p=*/false,
7796 /*asmspec_tree=*/NULL_TREE,
7797 /*flags=*/0);
7798 if (DECL_INITIALIZED_IN_CLASS_P (r))
7799 check_static_variable_definition (r, TREE_TYPE (r));
7801 else if (TREE_CODE (r) == FIELD_DECL)
7803 /* Determine whether R has a valid type and can be
7804 completed later. If R is invalid, then it is
7805 replaced by error_mark_node so that it will not be
7806 added to TYPE_FIELDS. */
7807 tree rtype = TREE_TYPE (r);
7808 if (can_complete_type_without_circularity (rtype))
7809 complete_type (rtype);
7811 if (!COMPLETE_TYPE_P (rtype))
7813 cxx_incomplete_type_error (r, rtype);
7814 r = error_mark_node;
7818 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7819 such a thing will already have been added to the field
7820 list by tsubst_enum in finish_member_declaration in the
7821 CLASSTYPE_NESTED_UTDS case above. */
7822 if (!(TREE_CODE (r) == TYPE_DECL
7823 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7824 && DECL_ARTIFICIAL (r)))
7826 set_current_access_from_decl (r);
7827 finish_member_declaration (r);
7832 else
7834 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7836 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7838 tree friend_type = t;
7839 bool adjust_processing_template_decl = false;
7841 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7843 /* template <class T> friend class C; */
7844 friend_type = tsubst_friend_class (friend_type, args);
7845 adjust_processing_template_decl = true;
7847 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7849 /* template <class T> friend class C::D; */
7850 friend_type = tsubst (friend_type, args,
7851 tf_warning_or_error, NULL_TREE);
7852 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7853 friend_type = TREE_TYPE (friend_type);
7854 adjust_processing_template_decl = true;
7856 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7858 /* This could be either
7860 friend class T::C;
7862 when dependent_type_p is false or
7864 template <class U> friend class T::C;
7866 otherwise. */
7867 friend_type = tsubst (friend_type, args,
7868 tf_warning_or_error, NULL_TREE);
7869 /* Bump processing_template_decl for correct
7870 dependent_type_p calculation. */
7871 ++processing_template_decl;
7872 if (dependent_type_p (friend_type))
7873 adjust_processing_template_decl = true;
7874 --processing_template_decl;
7876 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7877 && hidden_name_p (TYPE_NAME (friend_type)))
7879 /* friend class C;
7881 where C hasn't been declared yet. Let's lookup name
7882 from namespace scope directly, bypassing any name that
7883 come from dependent base class. */
7884 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7886 /* The call to xref_tag_from_type does injection for friend
7887 classes. */
7888 push_nested_namespace (ns);
7889 friend_type =
7890 xref_tag_from_type (friend_type, NULL_TREE,
7891 /*tag_scope=*/ts_current);
7892 pop_nested_namespace (ns);
7894 else if (uses_template_parms (friend_type))
7895 /* friend class C<T>; */
7896 friend_type = tsubst (friend_type, args,
7897 tf_warning_or_error, NULL_TREE);
7898 /* Otherwise it's
7900 friend class C;
7902 where C is already declared or
7904 friend class C<int>;
7906 We don't have to do anything in these cases. */
7908 if (adjust_processing_template_decl)
7909 /* Trick make_friend_class into realizing that the friend
7910 we're adding is a template, not an ordinary class. It's
7911 important that we use make_friend_class since it will
7912 perform some error-checking and output cross-reference
7913 information. */
7914 ++processing_template_decl;
7916 if (friend_type != error_mark_node)
7917 make_friend_class (type, friend_type, /*complain=*/false);
7919 if (adjust_processing_template_decl)
7920 --processing_template_decl;
7922 else
7924 /* Build new DECL_FRIENDLIST. */
7925 tree r;
7927 /* The file and line for this declaration, to
7928 assist in error message reporting. Since we
7929 called push_tinst_level above, we don't need to
7930 restore these. */
7931 input_location = DECL_SOURCE_LOCATION (t);
7933 if (TREE_CODE (t) == TEMPLATE_DECL)
7935 ++processing_template_decl;
7936 push_deferring_access_checks (dk_no_check);
7939 r = tsubst_friend_function (t, args);
7940 add_friend (type, r, /*complain=*/false);
7941 if (TREE_CODE (t) == TEMPLATE_DECL)
7943 pop_deferring_access_checks ();
7944 --processing_template_decl;
7950 /* Set the file and line number information to whatever is given for
7951 the class itself. This puts error messages involving generated
7952 implicit functions at a predictable point, and the same point
7953 that would be used for non-template classes. */
7954 input_location = DECL_SOURCE_LOCATION (typedecl);
7956 unreverse_member_declarations (type);
7957 finish_struct_1 (type);
7958 TYPE_BEING_DEFINED (type) = 0;
7960 /* Now that the class is complete, instantiate default arguments for
7961 any member functions. We don't do this earlier because the
7962 default arguments may reference members of the class. */
7963 if (!PRIMARY_TEMPLATE_P (templ))
7964 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7965 if (TREE_CODE (t) == FUNCTION_DECL
7966 /* Implicitly generated member functions will not have template
7967 information; they are not instantiations, but instead are
7968 created "fresh" for each instantiation. */
7969 && DECL_TEMPLATE_INFO (t))
7970 tsubst_default_arguments (t);
7972 /* Some typedefs referenced from within the template code need to be access
7973 checked at template instantiation time, i.e now. These types were
7974 added to the template at parsing time. Let's get those and perform
7975 the access checks then. */
7976 perform_typedefs_access_check (pattern, args);
7977 perform_deferred_access_checks ();
7978 pop_nested_class ();
7979 maximum_field_alignment = saved_maximum_field_alignment;
7980 pop_from_top_level ();
7981 pop_deferring_access_checks ();
7982 pop_tinst_level ();
7984 /* The vtable for a template class can be emitted in any translation
7985 unit in which the class is instantiated. When there is no key
7986 method, however, finish_struct_1 will already have added TYPE to
7987 the keyed_classes list. */
7988 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7989 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7991 return type;
7994 static tree
7995 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7997 tree r;
7999 if (!t)
8000 r = t;
8001 else if (TYPE_P (t))
8002 r = tsubst (t, args, complain, in_decl);
8003 else
8005 r = tsubst_expr (t, args, complain, in_decl,
8006 /*integral_constant_expression_p=*/true);
8007 r = fold_non_dependent_expr (r);
8009 return r;
8012 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8013 NONTYPE_ARGUMENT_PACK. */
8015 static tree
8016 make_fnparm_pack (tree spec_parm)
8018 /* Collect all of the extra "packed" parameters into an
8019 argument pack. */
8020 tree parmvec;
8021 tree parmtypevec;
8022 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8023 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8024 int i, len = list_length (spec_parm);
8026 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8027 parmvec = make_tree_vec (len);
8028 parmtypevec = make_tree_vec (len);
8029 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
8031 TREE_VEC_ELT (parmvec, i) = spec_parm;
8032 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8035 /* Build the argument packs. */
8036 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8037 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8038 TREE_TYPE (argpack) = argtypepack;
8040 return argpack;
8043 /* Substitute ARGS into T, which is an pack expansion
8044 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8045 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8046 (if only a partial substitution could be performed) or
8047 ERROR_MARK_NODE if there was an error. */
8048 tree
8049 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8050 tree in_decl)
8052 tree pattern;
8053 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8054 int i, len = -1;
8055 tree result;
8056 int incomplete = 0;
8057 bool very_local_specializations = false;
8059 gcc_assert (PACK_EXPANSION_P (t));
8060 pattern = PACK_EXPANSION_PATTERN (t);
8062 /* Determine the argument packs that will instantiate the parameter
8063 packs used in the expansion expression. While we're at it,
8064 compute the number of arguments to be expanded and make sure it
8065 is consistent. */
8066 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8067 pack = TREE_CHAIN (pack))
8069 tree parm_pack = TREE_VALUE (pack);
8070 tree arg_pack = NULL_TREE;
8071 tree orig_arg = NULL_TREE;
8073 if (TREE_CODE (parm_pack) == PARM_DECL)
8075 arg_pack = retrieve_local_specialization (parm_pack);
8076 if (arg_pack == NULL_TREE)
8078 /* This can happen for a parameter name used later in a function
8079 declaration (such as in a late-specified return type). Just
8080 make a dummy decl, since it's only used for its type. */
8081 gcc_assert (cp_unevaluated_operand != 0);
8082 arg_pack = tsubst_decl (parm_pack, args, complain);
8083 arg_pack = make_fnparm_pack (arg_pack);
8086 else
8088 int level, idx, levels;
8089 template_parm_level_and_index (parm_pack, &level, &idx);
8091 levels = TMPL_ARGS_DEPTH (args);
8092 if (level <= levels)
8093 arg_pack = TMPL_ARG (args, level, idx);
8096 orig_arg = arg_pack;
8097 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8098 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8100 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8101 /* This can only happen if we forget to expand an argument
8102 pack somewhere else. Just return an error, silently. */
8104 result = make_tree_vec (1);
8105 TREE_VEC_ELT (result, 0) = error_mark_node;
8106 return result;
8109 if (arg_pack
8110 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8111 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8113 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8114 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8115 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8116 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8117 /* The argument pack that the parameter maps to is just an
8118 expansion of the parameter itself, such as one would
8119 find in the implicit typedef of a class inside the
8120 class itself. Consider this parameter "unsubstituted",
8121 so that we will maintain the outer pack expansion. */
8122 arg_pack = NULL_TREE;
8125 if (arg_pack)
8127 int my_len =
8128 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8130 /* It's all-or-nothing with incomplete argument packs. */
8131 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8132 return error_mark_node;
8134 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8135 incomplete = 1;
8137 if (len < 0)
8138 len = my_len;
8139 else if (len != my_len)
8141 if (incomplete)
8142 /* We got explicit args for some packs but not others;
8143 do nothing now and try again after deduction. */
8144 return t;
8145 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8146 error ("mismatched argument pack lengths while expanding "
8147 "%<%T%>",
8148 pattern);
8149 else
8150 error ("mismatched argument pack lengths while expanding "
8151 "%<%E%>",
8152 pattern);
8153 return error_mark_node;
8156 /* Keep track of the parameter packs and their corresponding
8157 argument packs. */
8158 packs = tree_cons (parm_pack, arg_pack, packs);
8159 TREE_TYPE (packs) = orig_arg;
8161 else
8162 /* We can't substitute for this parameter pack. */
8163 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8164 TREE_VALUE (pack),
8165 unsubstituted_packs);
8168 /* We cannot expand this expansion expression, because we don't have
8169 all of the argument packs we need. Substitute into the pattern
8170 and return a PACK_EXPANSION_*. The caller will need to deal with
8171 that. */
8172 if (unsubstituted_packs)
8174 tree new_pat;
8175 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8176 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8177 /*integral_constant_expression_p=*/false);
8178 else
8179 new_pat = tsubst (pattern, args, complain, in_decl);
8180 return make_pack_expansion (new_pat);
8183 /* We could not find any argument packs that work. */
8184 if (len < 0)
8185 return error_mark_node;
8187 if (!local_specializations)
8189 /* We're in a late-specified return type, so we don't have a local
8190 specializations table. Create one for doing this expansion. */
8191 very_local_specializations = true;
8192 local_specializations = htab_create (37,
8193 hash_local_specialization,
8194 eq_local_specializations,
8195 NULL);
8198 /* For each argument in each argument pack, substitute into the
8199 pattern. */
8200 result = make_tree_vec (len + incomplete);
8201 for (i = 0; i < len + incomplete; ++i)
8203 /* For parameter pack, change the substitution of the parameter
8204 pack to the ith argument in its argument pack, then expand
8205 the pattern. */
8206 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8208 tree parm = TREE_PURPOSE (pack);
8210 if (TREE_CODE (parm) == PARM_DECL)
8212 /* Select the Ith argument from the pack. */
8213 tree arg = make_node (ARGUMENT_PACK_SELECT);
8214 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8215 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8216 mark_used (parm);
8217 register_local_specialization (arg, parm);
8219 else
8221 tree value = parm;
8222 int idx, level;
8223 template_parm_level_and_index (parm, &level, &idx);
8225 if (i < len)
8227 /* Select the Ith argument from the pack. */
8228 value = make_node (ARGUMENT_PACK_SELECT);
8229 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8230 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8233 /* Update the corresponding argument. */
8234 TMPL_ARG (args, level, idx) = value;
8238 /* Substitute into the PATTERN with the altered arguments. */
8239 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8240 TREE_VEC_ELT (result, i) =
8241 tsubst_expr (pattern, args, complain, in_decl,
8242 /*integral_constant_expression_p=*/false);
8243 else
8244 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8246 if (i == len)
8247 /* When we have incomplete argument packs, the last "expanded"
8248 result is itself a pack expansion, which allows us
8249 to deduce more arguments. */
8250 TREE_VEC_ELT (result, i) =
8251 make_pack_expansion (TREE_VEC_ELT (result, i));
8253 if (TREE_VEC_ELT (result, i) == error_mark_node)
8255 result = error_mark_node;
8256 break;
8260 /* Update ARGS to restore the substitution from parameter packs to
8261 their argument packs. */
8262 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8264 tree parm = TREE_PURPOSE (pack);
8266 if (TREE_CODE (parm) == PARM_DECL)
8267 register_local_specialization (TREE_TYPE (pack), parm);
8268 else
8270 int idx, level;
8271 template_parm_level_and_index (parm, &level, &idx);
8273 /* Update the corresponding argument. */
8274 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8275 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8276 TREE_TYPE (pack);
8277 else
8278 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8282 if (very_local_specializations)
8284 htab_delete (local_specializations);
8285 local_specializations = NULL;
8288 return result;
8291 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8292 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8293 parameter packs; all parms generated from a function parameter pack will
8294 have the same DECL_PARM_INDEX. */
8296 tree
8297 get_pattern_parm (tree parm, tree tmpl)
8299 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8300 tree patparm;
8302 if (DECL_ARTIFICIAL (parm))
8304 for (patparm = DECL_ARGUMENTS (pattern);
8305 patparm; patparm = TREE_CHAIN (patparm))
8306 if (DECL_ARTIFICIAL (patparm)
8307 && DECL_NAME (parm) == DECL_NAME (patparm))
8308 break;
8310 else
8312 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8313 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8314 gcc_assert (DECL_PARM_INDEX (patparm)
8315 == DECL_PARM_INDEX (parm));
8318 return patparm;
8321 /* Substitute ARGS into the vector or list of template arguments T. */
8323 static tree
8324 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8326 tree orig_t = t;
8327 int len = TREE_VEC_LENGTH (t);
8328 int need_new = 0, i, expanded_len_adjust = 0, out;
8329 tree *elts = (tree *) alloca (len * sizeof (tree));
8331 for (i = 0; i < len; i++)
8333 tree orig_arg = TREE_VEC_ELT (t, i);
8334 tree new_arg;
8336 if (TREE_CODE (orig_arg) == TREE_VEC)
8337 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8338 else if (PACK_EXPANSION_P (orig_arg))
8340 /* Substitute into an expansion expression. */
8341 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8343 if (TREE_CODE (new_arg) == TREE_VEC)
8344 /* Add to the expanded length adjustment the number of
8345 expanded arguments. We subtract one from this
8346 measurement, because the argument pack expression
8347 itself is already counted as 1 in
8348 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8349 the argument pack is empty. */
8350 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8352 else if (ARGUMENT_PACK_P (orig_arg))
8354 /* Substitute into each of the arguments. */
8355 new_arg = TYPE_P (orig_arg)
8356 ? cxx_make_type (TREE_CODE (orig_arg))
8357 : make_node (TREE_CODE (orig_arg));
8359 SET_ARGUMENT_PACK_ARGS (
8360 new_arg,
8361 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8362 args, complain, in_decl));
8364 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8365 new_arg = error_mark_node;
8367 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8368 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8369 complain, in_decl);
8370 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8372 if (TREE_TYPE (new_arg) == error_mark_node)
8373 new_arg = error_mark_node;
8376 else
8377 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8379 if (new_arg == error_mark_node)
8380 return error_mark_node;
8382 elts[i] = new_arg;
8383 if (new_arg != orig_arg)
8384 need_new = 1;
8387 if (!need_new)
8388 return t;
8390 /* Make space for the expanded arguments coming from template
8391 argument packs. */
8392 t = make_tree_vec (len + expanded_len_adjust);
8393 for (i = 0, out = 0; i < len; i++)
8395 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8396 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8397 && TREE_CODE (elts[i]) == TREE_VEC)
8399 int idx;
8401 /* Now expand the template argument pack "in place". */
8402 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8403 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8405 else
8407 TREE_VEC_ELT (t, out) = elts[i];
8408 out++;
8412 return t;
8415 /* Return the result of substituting ARGS into the template parameters
8416 given by PARMS. If there are m levels of ARGS and m + n levels of
8417 PARMS, then the result will contain n levels of PARMS. For
8418 example, if PARMS is `template <class T> template <class U>
8419 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8420 result will be `template <int*, double, class V>'. */
8422 static tree
8423 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8425 tree r = NULL_TREE;
8426 tree* new_parms;
8428 /* When substituting into a template, we must set
8429 PROCESSING_TEMPLATE_DECL as the template parameters may be
8430 dependent if they are based on one-another, and the dependency
8431 predicates are short-circuit outside of templates. */
8432 ++processing_template_decl;
8434 for (new_parms = &r;
8435 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8436 new_parms = &(TREE_CHAIN (*new_parms)),
8437 parms = TREE_CHAIN (parms))
8439 tree new_vec =
8440 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8441 int i;
8443 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8445 tree tuple;
8446 tree default_value;
8447 tree parm_decl;
8449 if (parms == error_mark_node)
8450 continue;
8452 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8454 if (tuple == error_mark_node)
8455 continue;
8457 default_value = TREE_PURPOSE (tuple);
8458 parm_decl = TREE_VALUE (tuple);
8460 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8461 if (TREE_CODE (parm_decl) == PARM_DECL
8462 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8463 parm_decl = error_mark_node;
8464 default_value = tsubst_template_arg (default_value, args,
8465 complain, NULL_TREE);
8467 tuple = build_tree_list (default_value, parm_decl);
8468 TREE_VEC_ELT (new_vec, i) = tuple;
8471 *new_parms =
8472 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8473 - TMPL_ARGS_DEPTH (args)),
8474 new_vec, NULL_TREE);
8477 --processing_template_decl;
8479 return r;
8482 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8483 type T. If T is not an aggregate or enumeration type, it is
8484 handled as if by tsubst. IN_DECL is as for tsubst. If
8485 ENTERING_SCOPE is nonzero, T is the context for a template which
8486 we are presently tsubst'ing. Return the substituted value. */
8488 static tree
8489 tsubst_aggr_type (tree t,
8490 tree args,
8491 tsubst_flags_t complain,
8492 tree in_decl,
8493 int entering_scope)
8495 if (t == NULL_TREE)
8496 return NULL_TREE;
8498 switch (TREE_CODE (t))
8500 case RECORD_TYPE:
8501 if (TYPE_PTRMEMFUNC_P (t))
8502 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8504 /* Else fall through. */
8505 case ENUMERAL_TYPE:
8506 case UNION_TYPE:
8507 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8509 tree argvec;
8510 tree context;
8511 tree r;
8512 int saved_unevaluated_operand;
8513 int saved_inhibit_evaluation_warnings;
8515 /* In "sizeof(X<I>)" we need to evaluate "I". */
8516 saved_unevaluated_operand = cp_unevaluated_operand;
8517 cp_unevaluated_operand = 0;
8518 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8519 c_inhibit_evaluation_warnings = 0;
8521 /* First, determine the context for the type we are looking
8522 up. */
8523 context = TYPE_CONTEXT (t);
8524 if (context)
8526 context = tsubst_aggr_type (context, args, complain,
8527 in_decl, /*entering_scope=*/1);
8528 /* If context is a nested class inside a class template,
8529 it may still need to be instantiated (c++/33959). */
8530 if (TYPE_P (context))
8531 context = complete_type (context);
8534 /* Then, figure out what arguments are appropriate for the
8535 type we are trying to find. For example, given:
8537 template <class T> struct S;
8538 template <class T, class U> void f(T, U) { S<U> su; }
8540 and supposing that we are instantiating f<int, double>,
8541 then our ARGS will be {int, double}, but, when looking up
8542 S we only want {double}. */
8543 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8544 complain, in_decl);
8545 if (argvec == error_mark_node)
8546 r = error_mark_node;
8547 else
8549 r = lookup_template_class (t, argvec, in_decl, context,
8550 entering_scope, complain);
8551 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8554 cp_unevaluated_operand = saved_unevaluated_operand;
8555 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8557 return r;
8559 else
8560 /* This is not a template type, so there's nothing to do. */
8561 return t;
8563 default:
8564 return tsubst (t, args, complain, in_decl);
8568 /* Substitute into the default argument ARG (a default argument for
8569 FN), which has the indicated TYPE. */
8571 tree
8572 tsubst_default_argument (tree fn, tree type, tree arg)
8574 tree saved_class_ptr = NULL_TREE;
8575 tree saved_class_ref = NULL_TREE;
8577 /* This default argument came from a template. Instantiate the
8578 default argument here, not in tsubst. In the case of
8579 something like:
8581 template <class T>
8582 struct S {
8583 static T t();
8584 void f(T = t());
8587 we must be careful to do name lookup in the scope of S<T>,
8588 rather than in the current class. */
8589 push_access_scope (fn);
8590 /* The "this" pointer is not valid in a default argument. */
8591 if (cfun)
8593 saved_class_ptr = current_class_ptr;
8594 cp_function_chain->x_current_class_ptr = NULL_TREE;
8595 saved_class_ref = current_class_ref;
8596 cp_function_chain->x_current_class_ref = NULL_TREE;
8599 push_deferring_access_checks(dk_no_deferred);
8600 /* The default argument expression may cause implicitly defined
8601 member functions to be synthesized, which will result in garbage
8602 collection. We must treat this situation as if we were within
8603 the body of function so as to avoid collecting live data on the
8604 stack. */
8605 ++function_depth;
8606 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8607 tf_warning_or_error, NULL_TREE,
8608 /*integral_constant_expression_p=*/false);
8609 --function_depth;
8610 pop_deferring_access_checks();
8612 /* Restore the "this" pointer. */
8613 if (cfun)
8615 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8616 cp_function_chain->x_current_class_ref = saved_class_ref;
8619 /* Make sure the default argument is reasonable. */
8620 arg = check_default_argument (type, arg);
8622 pop_access_scope (fn);
8624 return arg;
8627 /* Substitute into all the default arguments for FN. */
8629 static void
8630 tsubst_default_arguments (tree fn)
8632 tree arg;
8633 tree tmpl_args;
8635 tmpl_args = DECL_TI_ARGS (fn);
8637 /* If this function is not yet instantiated, we certainly don't need
8638 its default arguments. */
8639 if (uses_template_parms (tmpl_args))
8640 return;
8642 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8643 arg;
8644 arg = TREE_CHAIN (arg))
8645 if (TREE_PURPOSE (arg))
8646 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8647 TREE_VALUE (arg),
8648 TREE_PURPOSE (arg));
8651 /* Substitute the ARGS into the T, which is a _DECL. Return the
8652 result of the substitution. Issue error and warning messages under
8653 control of COMPLAIN. */
8655 static tree
8656 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8658 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8659 location_t saved_loc;
8660 tree r = NULL_TREE;
8661 tree in_decl = t;
8662 hashval_t hash = 0;
8664 /* Set the filename and linenumber to improve error-reporting. */
8665 saved_loc = input_location;
8666 input_location = DECL_SOURCE_LOCATION (t);
8668 switch (TREE_CODE (t))
8670 case TEMPLATE_DECL:
8672 /* We can get here when processing a member function template,
8673 member class template, or template template parameter. */
8674 tree decl = DECL_TEMPLATE_RESULT (t);
8675 tree spec;
8676 tree tmpl_args;
8677 tree full_args;
8679 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8681 /* Template template parameter is treated here. */
8682 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8683 if (new_type == error_mark_node)
8684 RETURN (error_mark_node);
8686 r = copy_decl (t);
8687 TREE_CHAIN (r) = NULL_TREE;
8688 TREE_TYPE (r) = new_type;
8689 DECL_TEMPLATE_RESULT (r)
8690 = build_decl (DECL_SOURCE_LOCATION (decl),
8691 TYPE_DECL, DECL_NAME (decl), new_type);
8692 DECL_TEMPLATE_PARMS (r)
8693 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8694 complain);
8695 TYPE_NAME (new_type) = r;
8696 break;
8699 /* We might already have an instance of this template.
8700 The ARGS are for the surrounding class type, so the
8701 full args contain the tsubst'd args for the context,
8702 plus the innermost args from the template decl. */
8703 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8704 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8705 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8706 /* Because this is a template, the arguments will still be
8707 dependent, even after substitution. If
8708 PROCESSING_TEMPLATE_DECL is not set, the dependency
8709 predicates will short-circuit. */
8710 ++processing_template_decl;
8711 full_args = tsubst_template_args (tmpl_args, args,
8712 complain, in_decl);
8713 --processing_template_decl;
8714 if (full_args == error_mark_node)
8715 RETURN (error_mark_node);
8717 /* If this is a default template template argument,
8718 tsubst might not have changed anything. */
8719 if (full_args == tmpl_args)
8720 RETURN (t);
8722 hash = hash_tmpl_and_args (t, full_args);
8723 spec = retrieve_specialization (t, full_args, hash);
8724 if (spec != NULL_TREE)
8726 r = spec;
8727 break;
8730 /* Make a new template decl. It will be similar to the
8731 original, but will record the current template arguments.
8732 We also create a new function declaration, which is just
8733 like the old one, but points to this new template, rather
8734 than the old one. */
8735 r = copy_decl (t);
8736 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8737 TREE_CHAIN (r) = NULL_TREE;
8739 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
8741 if (TREE_CODE (decl) == TYPE_DECL)
8743 tree new_type;
8744 ++processing_template_decl;
8745 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8746 --processing_template_decl;
8747 if (new_type == error_mark_node)
8748 RETURN (error_mark_node);
8750 TREE_TYPE (r) = new_type;
8751 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8752 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8753 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8754 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8756 else
8758 tree new_decl;
8759 ++processing_template_decl;
8760 new_decl = tsubst (decl, args, complain, in_decl);
8761 --processing_template_decl;
8762 if (new_decl == error_mark_node)
8763 RETURN (error_mark_node);
8765 DECL_TEMPLATE_RESULT (r) = new_decl;
8766 DECL_TI_TEMPLATE (new_decl) = r;
8767 TREE_TYPE (r) = TREE_TYPE (new_decl);
8768 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8769 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8772 SET_DECL_IMPLICIT_INSTANTIATION (r);
8773 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8774 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8776 /* The template parameters for this new template are all the
8777 template parameters for the old template, except the
8778 outermost level of parameters. */
8779 DECL_TEMPLATE_PARMS (r)
8780 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8781 complain);
8783 if (PRIMARY_TEMPLATE_P (t))
8784 DECL_PRIMARY_TEMPLATE (r) = r;
8786 if (TREE_CODE (decl) != TYPE_DECL)
8787 /* Record this non-type partial instantiation. */
8788 register_specialization (r, t,
8789 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8790 false, hash);
8792 break;
8794 case FUNCTION_DECL:
8796 tree ctx;
8797 tree argvec = NULL_TREE;
8798 tree *friends;
8799 tree gen_tmpl;
8800 tree type;
8801 int member;
8802 int args_depth;
8803 int parms_depth;
8805 /* Nobody should be tsubst'ing into non-template functions. */
8806 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8808 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8810 tree spec;
8811 bool dependent_p;
8813 /* If T is not dependent, just return it. We have to
8814 increment PROCESSING_TEMPLATE_DECL because
8815 value_dependent_expression_p assumes that nothing is
8816 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8817 ++processing_template_decl;
8818 dependent_p = value_dependent_expression_p (t);
8819 --processing_template_decl;
8820 if (!dependent_p)
8821 RETURN (t);
8823 /* Calculate the most general template of which R is a
8824 specialization, and the complete set of arguments used to
8825 specialize R. */
8826 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8827 argvec = tsubst_template_args (DECL_TI_ARGS
8828 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8829 args, complain, in_decl);
8831 /* Check to see if we already have this specialization. */
8832 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8833 spec = retrieve_specialization (gen_tmpl, argvec, hash);
8835 if (spec)
8837 r = spec;
8838 break;
8841 /* We can see more levels of arguments than parameters if
8842 there was a specialization of a member template, like
8843 this:
8845 template <class T> struct S { template <class U> void f(); }
8846 template <> template <class U> void S<int>::f(U);
8848 Here, we'll be substituting into the specialization,
8849 because that's where we can find the code we actually
8850 want to generate, but we'll have enough arguments for
8851 the most general template.
8853 We also deal with the peculiar case:
8855 template <class T> struct S {
8856 template <class U> friend void f();
8858 template <class U> void f() {}
8859 template S<int>;
8860 template void f<double>();
8862 Here, the ARGS for the instantiation of will be {int,
8863 double}. But, we only need as many ARGS as there are
8864 levels of template parameters in CODE_PATTERN. We are
8865 careful not to get fooled into reducing the ARGS in
8866 situations like:
8868 template <class T> struct S { template <class U> void f(U); }
8869 template <class T> template <> void S<T>::f(int) {}
8871 which we can spot because the pattern will be a
8872 specialization in this case. */
8873 args_depth = TMPL_ARGS_DEPTH (args);
8874 parms_depth =
8875 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8876 if (args_depth > parms_depth
8877 && !DECL_TEMPLATE_SPECIALIZATION (t))
8878 args = get_innermost_template_args (args, parms_depth);
8880 else
8882 /* This special case arises when we have something like this:
8884 template <class T> struct S {
8885 friend void f<int>(int, double);
8888 Here, the DECL_TI_TEMPLATE for the friend declaration
8889 will be an IDENTIFIER_NODE. We are being called from
8890 tsubst_friend_function, and we want only to create a
8891 new decl (R) with appropriate types so that we can call
8892 determine_specialization. */
8893 gen_tmpl = NULL_TREE;
8896 if (DECL_CLASS_SCOPE_P (t))
8898 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8899 member = 2;
8900 else
8901 member = 1;
8902 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8903 complain, t, /*entering_scope=*/1);
8905 else
8907 member = 0;
8908 ctx = DECL_CONTEXT (t);
8910 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8911 if (type == error_mark_node)
8912 RETURN (error_mark_node);
8914 /* We do NOT check for matching decls pushed separately at this
8915 point, as they may not represent instantiations of this
8916 template, and in any case are considered separate under the
8917 discrete model. */
8918 r = copy_decl (t);
8919 DECL_USE_TEMPLATE (r) = 0;
8920 TREE_TYPE (r) = type;
8921 /* Clear out the mangled name and RTL for the instantiation. */
8922 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8923 SET_DECL_RTL (r, NULL_RTX);
8924 /* Leave DECL_INITIAL set on deleted instantiations. */
8925 if (!DECL_DELETED_FN (r))
8926 DECL_INITIAL (r) = NULL_TREE;
8927 DECL_CONTEXT (r) = ctx;
8929 if (member && DECL_CONV_FN_P (r))
8930 /* Type-conversion operator. Reconstruct the name, in
8931 case it's the name of one of the template's parameters. */
8932 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8934 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8935 complain, t);
8936 DECL_RESULT (r) = NULL_TREE;
8938 TREE_STATIC (r) = 0;
8939 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8940 DECL_EXTERNAL (r) = 1;
8941 /* If this is an instantiation of a function with internal
8942 linkage, we already know what object file linkage will be
8943 assigned to the instantiation. */
8944 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8945 DECL_DEFER_OUTPUT (r) = 0;
8946 TREE_CHAIN (r) = NULL_TREE;
8947 DECL_PENDING_INLINE_INFO (r) = 0;
8948 DECL_PENDING_INLINE_P (r) = 0;
8949 DECL_SAVED_TREE (r) = NULL_TREE;
8950 DECL_STRUCT_FUNCTION (r) = NULL;
8951 TREE_USED (r) = 0;
8952 /* We'll re-clone as appropriate in instantiate_template. */
8953 DECL_CLONED_FUNCTION (r) = NULL_TREE;
8955 /* If we aren't complaining now, return on error before we register
8956 the specialization so that we'll complain eventually. */
8957 if ((complain & tf_error) == 0
8958 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
8959 && !grok_op_properties (r, /*complain=*/false))
8960 RETURN (error_mark_node);
8962 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8963 this in the special friend case mentioned above where
8964 GEN_TMPL is NULL. */
8965 if (gen_tmpl)
8967 DECL_TEMPLATE_INFO (r)
8968 = build_template_info (gen_tmpl, argvec);
8969 SET_DECL_IMPLICIT_INSTANTIATION (r);
8970 register_specialization (r, gen_tmpl, argvec, false, hash);
8972 /* We're not supposed to instantiate default arguments
8973 until they are called, for a template. But, for a
8974 declaration like:
8976 template <class T> void f ()
8977 { extern void g(int i = T()); }
8979 we should do the substitution when the template is
8980 instantiated. We handle the member function case in
8981 instantiate_class_template since the default arguments
8982 might refer to other members of the class. */
8983 if (!member
8984 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8985 && !uses_template_parms (argvec))
8986 tsubst_default_arguments (r);
8988 else
8989 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8991 /* Copy the list of befriending classes. */
8992 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8993 *friends;
8994 friends = &TREE_CHAIN (*friends))
8996 *friends = copy_node (*friends);
8997 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8998 args, complain,
8999 in_decl);
9002 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9004 maybe_retrofit_in_chrg (r);
9005 if (DECL_CONSTRUCTOR_P (r))
9006 grok_ctor_properties (ctx, r);
9007 /* If this is an instantiation of a member template, clone it.
9008 If it isn't, that'll be handled by
9009 clone_constructors_and_destructors. */
9010 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9011 clone_function_decl (r, /*update_method_vec_p=*/0);
9013 else if ((complain & tf_error) != 0
9014 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9015 && !grok_op_properties (r, /*complain=*/true))
9016 RETURN (error_mark_node);
9018 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9019 SET_DECL_FRIEND_CONTEXT (r,
9020 tsubst (DECL_FRIEND_CONTEXT (t),
9021 args, complain, in_decl));
9023 /* Possibly limit visibility based on template args. */
9024 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9025 if (DECL_VISIBILITY_SPECIFIED (t))
9027 DECL_VISIBILITY_SPECIFIED (r) = 0;
9028 DECL_ATTRIBUTES (r)
9029 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9031 determine_visibility (r);
9032 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9033 && !processing_template_decl)
9034 defaulted_late_check (r);
9036 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9037 args, complain, in_decl);
9039 break;
9041 case PARM_DECL:
9043 tree type = NULL_TREE;
9044 int i, len = 1;
9045 tree expanded_types = NULL_TREE;
9046 tree prev_r = NULL_TREE;
9047 tree first_r = NULL_TREE;
9049 if (FUNCTION_PARAMETER_PACK_P (t))
9051 /* If there is a local specialization that isn't a
9052 parameter pack, it means that we're doing a "simple"
9053 substitution from inside tsubst_pack_expansion. Just
9054 return the local specialization (which will be a single
9055 parm). */
9056 tree spec = retrieve_local_specialization (t);
9057 if (spec
9058 && TREE_CODE (spec) == PARM_DECL
9059 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9060 RETURN (spec);
9062 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9063 the parameters in this function parameter pack. */
9064 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9065 complain, in_decl);
9066 if (TREE_CODE (expanded_types) == TREE_VEC)
9068 len = TREE_VEC_LENGTH (expanded_types);
9070 /* Zero-length parameter packs are boring. Just substitute
9071 into the chain. */
9072 if (len == 0)
9073 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9074 TREE_CHAIN (t)));
9076 else
9078 /* All we did was update the type. Make a note of that. */
9079 type = expanded_types;
9080 expanded_types = NULL_TREE;
9084 /* Loop through all of the parameter's we'll build. When T is
9085 a function parameter pack, LEN is the number of expanded
9086 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9087 r = NULL_TREE;
9088 for (i = 0; i < len; ++i)
9090 prev_r = r;
9091 r = copy_node (t);
9092 if (DECL_TEMPLATE_PARM_P (t))
9093 SET_DECL_TEMPLATE_PARM_P (r);
9095 /* An argument of a function parameter pack is not a parameter
9096 pack. */
9097 FUNCTION_PARAMETER_PACK_P (r) = false;
9099 if (expanded_types)
9100 /* We're on the Ith parameter of the function parameter
9101 pack. */
9103 /* Get the Ith type. */
9104 type = TREE_VEC_ELT (expanded_types, i);
9106 if (DECL_NAME (r))
9107 /* Rename the parameter to include the index. */
9108 DECL_NAME (r) =
9109 make_ith_pack_parameter_name (DECL_NAME (r), i);
9111 else if (!type)
9112 /* We're dealing with a normal parameter. */
9113 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9115 type = type_decays_to (type);
9116 TREE_TYPE (r) = type;
9117 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9119 if (DECL_INITIAL (r))
9121 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9122 DECL_INITIAL (r) = TREE_TYPE (r);
9123 else
9124 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9125 complain, in_decl);
9128 DECL_CONTEXT (r) = NULL_TREE;
9130 if (!DECL_TEMPLATE_PARM_P (r))
9131 DECL_ARG_TYPE (r) = type_passed_as (type);
9133 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9134 args, complain, in_decl);
9136 /* Keep track of the first new parameter we
9137 generate. That's what will be returned to the
9138 caller. */
9139 if (!first_r)
9140 first_r = r;
9142 /* Build a proper chain of parameters when substituting
9143 into a function parameter pack. */
9144 if (prev_r)
9145 TREE_CHAIN (prev_r) = r;
9148 if (TREE_CHAIN (t))
9149 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
9150 complain, TREE_CHAIN (t));
9152 /* FIRST_R contains the start of the chain we've built. */
9153 r = first_r;
9155 break;
9157 case FIELD_DECL:
9159 tree type;
9161 r = copy_decl (t);
9162 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9163 if (type == error_mark_node)
9164 RETURN (error_mark_node);
9165 TREE_TYPE (r) = type;
9166 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9168 /* DECL_INITIAL gives the number of bits in a bit-field. */
9169 DECL_INITIAL (r)
9170 = tsubst_expr (DECL_INITIAL (t), args,
9171 complain, in_decl,
9172 /*integral_constant_expression_p=*/true);
9173 /* We don't have to set DECL_CONTEXT here; it is set by
9174 finish_member_declaration. */
9175 TREE_CHAIN (r) = NULL_TREE;
9176 if (VOID_TYPE_P (type))
9177 error ("instantiation of %q+D as type %qT", r, type);
9179 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9180 args, complain, in_decl);
9182 break;
9184 case USING_DECL:
9185 /* We reach here only for member using decls. */
9186 if (DECL_DEPENDENT_P (t))
9188 r = do_class_using_decl
9189 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9190 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9191 if (!r)
9192 r = error_mark_node;
9193 else
9195 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9196 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9199 else
9201 r = copy_node (t);
9202 TREE_CHAIN (r) = NULL_TREE;
9204 break;
9206 case TYPE_DECL:
9207 case VAR_DECL:
9209 tree argvec = NULL_TREE;
9210 tree gen_tmpl = NULL_TREE;
9211 tree spec;
9212 tree tmpl = NULL_TREE;
9213 tree ctx;
9214 tree type = NULL_TREE;
9215 bool local_p;
9217 if (TREE_CODE (t) == TYPE_DECL
9218 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9220 /* If this is the canonical decl, we don't have to
9221 mess with instantiations, and often we can't (for
9222 typename, template type parms and such). Note that
9223 TYPE_NAME is not correct for the above test if
9224 we've copied the type for a typedef. */
9225 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9226 if (type == error_mark_node)
9227 RETURN (error_mark_node);
9228 r = TYPE_NAME (type);
9229 break;
9232 /* Check to see if we already have the specialization we
9233 need. */
9234 spec = NULL_TREE;
9235 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9237 /* T is a static data member or namespace-scope entity.
9238 We have to substitute into namespace-scope variables
9239 (even though such entities are never templates) because
9240 of cases like:
9242 template <class T> void f() { extern T t; }
9244 where the entity referenced is not known until
9245 instantiation time. */
9246 local_p = false;
9247 ctx = DECL_CONTEXT (t);
9248 if (DECL_CLASS_SCOPE_P (t))
9250 ctx = tsubst_aggr_type (ctx, args,
9251 complain,
9252 in_decl, /*entering_scope=*/1);
9253 /* If CTX is unchanged, then T is in fact the
9254 specialization we want. That situation occurs when
9255 referencing a static data member within in its own
9256 class. We can use pointer equality, rather than
9257 same_type_p, because DECL_CONTEXT is always
9258 canonical. */
9259 if (ctx == DECL_CONTEXT (t))
9260 spec = t;
9263 if (!spec)
9265 tmpl = DECL_TI_TEMPLATE (t);
9266 gen_tmpl = most_general_template (tmpl);
9267 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9268 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9269 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9272 else
9274 /* A local variable. */
9275 local_p = true;
9276 /* Subsequent calls to pushdecl will fill this in. */
9277 ctx = NULL_TREE;
9278 spec = retrieve_local_specialization (t);
9280 /* If we already have the specialization we need, there is
9281 nothing more to do. */
9282 if (spec)
9284 r = spec;
9285 break;
9288 /* Create a new node for the specialization we need. */
9289 r = copy_decl (t);
9290 if (type == NULL_TREE)
9292 if (is_typedef_decl (t))
9293 type = DECL_ORIGINAL_TYPE (t);
9294 else
9295 type = TREE_TYPE (t);
9296 type = tsubst (type, args, complain, in_decl);
9298 if (TREE_CODE (r) == VAR_DECL)
9300 /* Even if the original location is out of scope, the
9301 newly substituted one is not. */
9302 DECL_DEAD_FOR_LOCAL (r) = 0;
9303 DECL_INITIALIZED_P (r) = 0;
9304 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9305 if (type == error_mark_node)
9306 RETURN (error_mark_node);
9307 if (TREE_CODE (type) == FUNCTION_TYPE)
9309 /* It may seem that this case cannot occur, since:
9311 typedef void f();
9312 void g() { f x; }
9314 declares a function, not a variable. However:
9316 typedef void f();
9317 template <typename T> void g() { T t; }
9318 template void g<f>();
9320 is an attempt to declare a variable with function
9321 type. */
9322 error ("variable %qD has function type",
9323 /* R is not yet sufficiently initialized, so we
9324 just use its name. */
9325 DECL_NAME (r));
9326 RETURN (error_mark_node);
9328 type = complete_type (type);
9329 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9330 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9331 type = check_var_type (DECL_NAME (r), type);
9333 if (DECL_HAS_VALUE_EXPR_P (t))
9335 tree ve = DECL_VALUE_EXPR (t);
9336 ve = tsubst_expr (ve, args, complain, in_decl,
9337 /*constant_expression_p=*/false);
9338 SET_DECL_VALUE_EXPR (r, ve);
9341 else if (DECL_SELF_REFERENCE_P (t))
9342 SET_DECL_SELF_REFERENCE_P (r);
9343 TREE_TYPE (r) = type;
9344 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9345 DECL_CONTEXT (r) = ctx;
9346 /* Clear out the mangled name and RTL for the instantiation. */
9347 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9348 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9349 SET_DECL_RTL (r, NULL_RTX);
9350 /* The initializer must not be expanded until it is required;
9351 see [temp.inst]. */
9352 DECL_INITIAL (r) = NULL_TREE;
9353 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9354 SET_DECL_RTL (r, NULL_RTX);
9355 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9356 if (TREE_CODE (r) == VAR_DECL)
9358 /* Possibly limit visibility based on template args. */
9359 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9360 if (DECL_VISIBILITY_SPECIFIED (t))
9362 DECL_VISIBILITY_SPECIFIED (r) = 0;
9363 DECL_ATTRIBUTES (r)
9364 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9366 determine_visibility (r);
9369 if (!local_p)
9371 /* A static data member declaration is always marked
9372 external when it is declared in-class, even if an
9373 initializer is present. We mimic the non-template
9374 processing here. */
9375 DECL_EXTERNAL (r) = 1;
9377 register_specialization (r, gen_tmpl, argvec, false, hash);
9378 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9379 SET_DECL_IMPLICIT_INSTANTIATION (r);
9381 else if (cp_unevaluated_operand)
9383 /* We're substituting this var in a decltype outside of its
9384 scope, such as for a lambda return type. Don't add it to
9385 local_specializations, do perform auto deduction. */
9386 tree auto_node = type_uses_auto (type);
9387 tree init
9388 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9389 /*constant_expression_p=*/false);
9391 if (auto_node && init && describable_type (init))
9393 type = do_auto_deduction (type, init, auto_node);
9394 TREE_TYPE (r) = type;
9397 else
9398 register_local_specialization (r, t);
9400 TREE_CHAIN (r) = NULL_TREE;
9402 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9403 /*flags=*/0,
9404 args, complain, in_decl);
9406 /* Preserve a typedef that names a type. */
9407 if (is_typedef_decl (r))
9409 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9410 set_underlying_type (r);
9413 layout_decl (r, 0);
9415 break;
9417 default:
9418 gcc_unreachable ();
9420 #undef RETURN
9422 out:
9423 /* Restore the file and line information. */
9424 input_location = saved_loc;
9426 return r;
9429 /* Substitute into the ARG_TYPES of a function type. */
9431 static tree
9432 tsubst_arg_types (tree arg_types,
9433 tree args,
9434 tsubst_flags_t complain,
9435 tree in_decl)
9437 tree remaining_arg_types;
9438 tree type = NULL_TREE;
9439 int i = 1;
9440 tree expanded_args = NULL_TREE;
9441 tree default_arg;
9443 if (!arg_types || arg_types == void_list_node)
9444 return arg_types;
9446 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9447 args, complain, in_decl);
9448 if (remaining_arg_types == error_mark_node)
9449 return error_mark_node;
9451 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9453 /* For a pack expansion, perform substitution on the
9454 entire expression. Later on, we'll handle the arguments
9455 one-by-one. */
9456 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9457 args, complain, in_decl);
9459 if (TREE_CODE (expanded_args) == TREE_VEC)
9460 /* So that we'll spin through the parameters, one by one. */
9461 i = TREE_VEC_LENGTH (expanded_args);
9462 else
9464 /* We only partially substituted into the parameter
9465 pack. Our type is TYPE_PACK_EXPANSION. */
9466 type = expanded_args;
9467 expanded_args = NULL_TREE;
9471 while (i > 0) {
9472 --i;
9474 if (expanded_args)
9475 type = TREE_VEC_ELT (expanded_args, i);
9476 else if (!type)
9477 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9479 if (type == error_mark_node)
9480 return error_mark_node;
9481 if (VOID_TYPE_P (type))
9483 if (complain & tf_error)
9485 error ("invalid parameter type %qT", type);
9486 if (in_decl)
9487 error ("in declaration %q+D", in_decl);
9489 return error_mark_node;
9492 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9493 top-level qualifiers as required. */
9494 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9496 /* We do not substitute into default arguments here. The standard
9497 mandates that they be instantiated only when needed, which is
9498 done in build_over_call. */
9499 default_arg = TREE_PURPOSE (arg_types);
9501 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9503 /* We've instantiated a template before its default arguments
9504 have been parsed. This can happen for a nested template
9505 class, and is not an error unless we require the default
9506 argument in a call of this function. */
9507 remaining_arg_types =
9508 tree_cons (default_arg, type, remaining_arg_types);
9509 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9510 remaining_arg_types);
9512 else
9513 remaining_arg_types =
9514 hash_tree_cons (default_arg, type, remaining_arg_types);
9517 return remaining_arg_types;
9520 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9521 *not* handle the exception-specification for FNTYPE, because the
9522 initial substitution of explicitly provided template parameters
9523 during argument deduction forbids substitution into the
9524 exception-specification:
9526 [temp.deduct]
9528 All references in the function type of the function template to the
9529 corresponding template parameters are replaced by the specified tem-
9530 plate argument values. If a substitution in a template parameter or
9531 in the function type of the function template results in an invalid
9532 type, type deduction fails. [Note: The equivalent substitution in
9533 exception specifications is done only when the function is instanti-
9534 ated, at which point a program is ill-formed if the substitution
9535 results in an invalid type.] */
9537 static tree
9538 tsubst_function_type (tree t,
9539 tree args,
9540 tsubst_flags_t complain,
9541 tree in_decl)
9543 tree return_type;
9544 tree arg_types;
9545 tree fntype;
9547 /* The TYPE_CONTEXT is not used for function/method types. */
9548 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9550 /* Substitute the return type. */
9551 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9552 if (return_type == error_mark_node)
9553 return error_mark_node;
9554 /* The standard does not presently indicate that creation of a
9555 function type with an invalid return type is a deduction failure.
9556 However, that is clearly analogous to creating an array of "void"
9557 or a reference to a reference. This is core issue #486. */
9558 if (TREE_CODE (return_type) == ARRAY_TYPE
9559 || TREE_CODE (return_type) == FUNCTION_TYPE)
9561 if (complain & tf_error)
9563 if (TREE_CODE (return_type) == ARRAY_TYPE)
9564 error ("function returning an array");
9565 else
9566 error ("function returning a function");
9568 return error_mark_node;
9571 /* Substitute the argument types. */
9572 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9573 complain, in_decl);
9574 if (arg_types == error_mark_node)
9575 return error_mark_node;
9577 /* Construct a new type node and return it. */
9578 if (TREE_CODE (t) == FUNCTION_TYPE)
9579 fntype = build_function_type (return_type, arg_types);
9580 else
9582 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9583 if (! MAYBE_CLASS_TYPE_P (r))
9585 /* [temp.deduct]
9587 Type deduction may fail for any of the following
9588 reasons:
9590 -- Attempting to create "pointer to member of T" when T
9591 is not a class type. */
9592 if (complain & tf_error)
9593 error ("creating pointer to member function of non-class type %qT",
9595 return error_mark_node;
9598 fntype = build_method_type_directly (r, return_type,
9599 TREE_CHAIN (arg_types));
9601 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9602 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9604 return fntype;
9607 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9608 ARGS into that specification, and return the substituted
9609 specification. If there is no specification, return NULL_TREE. */
9611 static tree
9612 tsubst_exception_specification (tree fntype,
9613 tree args,
9614 tsubst_flags_t complain,
9615 tree in_decl)
9617 tree specs;
9618 tree new_specs;
9620 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9621 new_specs = NULL_TREE;
9622 if (specs)
9624 if (! TREE_VALUE (specs))
9625 new_specs = specs;
9626 else
9627 while (specs)
9629 tree spec;
9630 int i, len = 1;
9631 tree expanded_specs = NULL_TREE;
9633 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9635 /* Expand the pack expansion type. */
9636 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9637 args, complain,
9638 in_decl);
9640 if (expanded_specs == error_mark_node)
9641 return error_mark_node;
9642 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9643 len = TREE_VEC_LENGTH (expanded_specs);
9644 else
9646 /* We're substituting into a member template, so
9647 we got a TYPE_PACK_EXPANSION back. Add that
9648 expansion and move on. */
9649 gcc_assert (TREE_CODE (expanded_specs)
9650 == TYPE_PACK_EXPANSION);
9651 new_specs = add_exception_specifier (new_specs,
9652 expanded_specs,
9653 complain);
9654 specs = TREE_CHAIN (specs);
9655 continue;
9659 for (i = 0; i < len; ++i)
9661 if (expanded_specs)
9662 spec = TREE_VEC_ELT (expanded_specs, i);
9663 else
9664 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9665 if (spec == error_mark_node)
9666 return spec;
9667 new_specs = add_exception_specifier (new_specs, spec,
9668 complain);
9671 specs = TREE_CHAIN (specs);
9674 return new_specs;
9677 /* Take the tree structure T and replace template parameters used
9678 therein with the argument vector ARGS. IN_DECL is an associated
9679 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9680 Issue error and warning messages under control of COMPLAIN. Note
9681 that we must be relatively non-tolerant of extensions here, in
9682 order to preserve conformance; if we allow substitutions that
9683 should not be allowed, we may allow argument deductions that should
9684 not succeed, and therefore report ambiguous overload situations
9685 where there are none. In theory, we could allow the substitution,
9686 but indicate that it should have failed, and allow our caller to
9687 make sure that the right thing happens, but we don't try to do this
9688 yet.
9690 This function is used for dealing with types, decls and the like;
9691 for expressions, use tsubst_expr or tsubst_copy. */
9693 tree
9694 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9696 tree type, r;
9698 if (t == NULL_TREE || t == error_mark_node
9699 || t == integer_type_node
9700 || t == void_type_node
9701 || t == char_type_node
9702 || t == unknown_type_node
9703 || TREE_CODE (t) == NAMESPACE_DECL)
9704 return t;
9706 if (DECL_P (t))
9707 return tsubst_decl (t, args, complain);
9709 if (args == NULL_TREE)
9710 return t;
9712 if (TREE_CODE (t) == IDENTIFIER_NODE)
9713 type = IDENTIFIER_TYPE_VALUE (t);
9714 else
9715 type = TREE_TYPE (t);
9717 gcc_assert (type != unknown_type_node);
9719 /* Reuse typedefs. We need to do this to handle dependent attributes,
9720 such as attribute aligned. */
9721 if (TYPE_P (t)
9722 && TYPE_NAME (t)
9723 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9725 tree decl = TYPE_NAME (t);
9727 if (DECL_CLASS_SCOPE_P (decl)
9728 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9729 && uses_template_parms (DECL_CONTEXT (decl)))
9731 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9732 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9733 r = retrieve_specialization (tmpl, gen_args, 0);
9735 else if (DECL_FUNCTION_SCOPE_P (decl)
9736 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9737 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9738 r = retrieve_local_specialization (decl);
9739 else
9740 /* The typedef is from a non-template context. */
9741 return t;
9743 if (r)
9745 r = TREE_TYPE (r);
9746 r = cp_build_qualified_type_real
9747 (r, cp_type_quals (t) | cp_type_quals (r),
9748 complain | tf_ignore_bad_quals);
9749 return r;
9751 /* Else we must be instantiating the typedef, so fall through. */
9754 if (type
9755 && TREE_CODE (t) != TYPENAME_TYPE
9756 && TREE_CODE (t) != IDENTIFIER_NODE
9757 && TREE_CODE (t) != FUNCTION_TYPE
9758 && TREE_CODE (t) != METHOD_TYPE)
9759 type = tsubst (type, args, complain, in_decl);
9760 if (type == error_mark_node)
9761 return error_mark_node;
9763 switch (TREE_CODE (t))
9765 case RECORD_TYPE:
9766 case UNION_TYPE:
9767 case ENUMERAL_TYPE:
9768 return tsubst_aggr_type (t, args, complain, in_decl,
9769 /*entering_scope=*/0);
9771 case ERROR_MARK:
9772 case IDENTIFIER_NODE:
9773 case VOID_TYPE:
9774 case REAL_TYPE:
9775 case COMPLEX_TYPE:
9776 case VECTOR_TYPE:
9777 case BOOLEAN_TYPE:
9778 case INTEGER_CST:
9779 case REAL_CST:
9780 case STRING_CST:
9781 return t;
9783 case INTEGER_TYPE:
9784 if (t == integer_type_node)
9785 return t;
9787 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9788 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9789 return t;
9792 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9794 max = tsubst_expr (omax, args, complain, in_decl,
9795 /*integral_constant_expression_p=*/false);
9797 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9798 needed. */
9799 if (TREE_CODE (max) == NOP_EXPR
9800 && TREE_SIDE_EFFECTS (omax)
9801 && !TREE_TYPE (max))
9802 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9804 max = fold_decl_constant_value (max);
9806 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9807 with TREE_SIDE_EFFECTS that indicates this is not an integral
9808 constant expression. */
9809 if (processing_template_decl
9810 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9812 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9813 TREE_SIDE_EFFECTS (max) = 1;
9816 if (TREE_CODE (max) != INTEGER_CST
9817 && !at_function_scope_p ()
9818 && !TREE_SIDE_EFFECTS (max)
9819 && !value_dependent_expression_p (max))
9821 if (complain & tf_error)
9822 error ("array bound is not an integer constant");
9823 return error_mark_node;
9826 /* [temp.deduct]
9828 Type deduction may fail for any of the following
9829 reasons:
9831 Attempting to create an array with a size that is
9832 zero or negative. */
9833 if (integer_zerop (max) && !(complain & tf_error))
9834 /* We must fail if performing argument deduction (as
9835 indicated by the state of complain), so that
9836 another substitution can be found. */
9837 return error_mark_node;
9838 else if (TREE_CODE (max) == INTEGER_CST
9839 && INT_CST_LT (max, integer_zero_node))
9841 if (complain & tf_error)
9842 error ("creating array with negative size (%qE)", max);
9844 return error_mark_node;
9847 return compute_array_index_type (NULL_TREE, max);
9850 case TEMPLATE_TYPE_PARM:
9851 case TEMPLATE_TEMPLATE_PARM:
9852 case BOUND_TEMPLATE_TEMPLATE_PARM:
9853 case TEMPLATE_PARM_INDEX:
9855 int idx;
9856 int level;
9857 int levels;
9858 tree arg = NULL_TREE;
9860 r = NULL_TREE;
9862 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9863 template_parm_level_and_index (t, &level, &idx);
9865 levels = TMPL_ARGS_DEPTH (args);
9866 if (level <= levels)
9868 arg = TMPL_ARG (args, level, idx);
9870 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9871 /* See through ARGUMENT_PACK_SELECT arguments. */
9872 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9875 if (arg == error_mark_node)
9876 return error_mark_node;
9877 else if (arg != NULL_TREE)
9879 if (ARGUMENT_PACK_P (arg))
9880 /* If ARG is an argument pack, we don't actually want to
9881 perform a substitution here, because substitutions
9882 for argument packs are only done
9883 element-by-element. We can get to this point when
9884 substituting the type of a non-type template
9885 parameter pack, when that type actually contains
9886 template parameter packs from an outer template, e.g.,
9888 template<typename... Types> struct A {
9889 template<Types... Values> struct B { };
9890 }; */
9891 return t;
9893 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9895 int quals;
9896 gcc_assert (TYPE_P (arg));
9898 /* cv-quals from the template are discarded when
9899 substituting in a function or reference type. */
9900 if (TREE_CODE (arg) == FUNCTION_TYPE
9901 || TREE_CODE (arg) == METHOD_TYPE
9902 || TREE_CODE (arg) == REFERENCE_TYPE)
9903 quals = cp_type_quals (arg);
9904 else
9905 quals = cp_type_quals (arg) | cp_type_quals (t);
9907 return cp_build_qualified_type_real
9908 (arg, quals, complain | tf_ignore_bad_quals);
9910 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9912 /* We are processing a type constructed from a
9913 template template parameter. */
9914 tree argvec = tsubst (TYPE_TI_ARGS (t),
9915 args, complain, in_decl);
9916 if (argvec == error_mark_node)
9917 return error_mark_node;
9919 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9920 are resolving nested-types in the signature of a
9921 member function templates. Otherwise ARG is a
9922 TEMPLATE_DECL and is the real template to be
9923 instantiated. */
9924 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9925 arg = TYPE_NAME (arg);
9927 r = lookup_template_class (arg,
9928 argvec, in_decl,
9929 DECL_CONTEXT (arg),
9930 /*entering_scope=*/0,
9931 complain);
9932 return cp_build_qualified_type_real
9933 (r, TYPE_QUALS (t), complain);
9935 else
9936 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9937 return arg;
9940 if (level == 1)
9941 /* This can happen during the attempted tsubst'ing in
9942 unify. This means that we don't yet have any information
9943 about the template parameter in question. */
9944 return t;
9946 /* If we get here, we must have been looking at a parm for a
9947 more deeply nested template. Make a new version of this
9948 template parameter, but with a lower level. */
9949 switch (TREE_CODE (t))
9951 case TEMPLATE_TYPE_PARM:
9952 case TEMPLATE_TEMPLATE_PARM:
9953 case BOUND_TEMPLATE_TEMPLATE_PARM:
9954 if (cp_type_quals (t))
9956 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9957 r = cp_build_qualified_type_real
9958 (r, cp_type_quals (t),
9959 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9960 ? tf_ignore_bad_quals : 0));
9962 else
9964 r = copy_type (t);
9965 TEMPLATE_TYPE_PARM_INDEX (r)
9966 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9967 r, levels, args, complain);
9968 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9969 TYPE_MAIN_VARIANT (r) = r;
9970 TYPE_POINTER_TO (r) = NULL_TREE;
9971 TYPE_REFERENCE_TO (r) = NULL_TREE;
9973 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9974 /* We have reduced the level of the template
9975 template parameter, but not the levels of its
9976 template parameters, so canonical_type_parameter
9977 will not be able to find the canonical template
9978 template parameter for this level. Thus, we
9979 require structural equality checking to compare
9980 TEMPLATE_TEMPLATE_PARMs. */
9981 SET_TYPE_STRUCTURAL_EQUALITY (r);
9982 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9983 SET_TYPE_STRUCTURAL_EQUALITY (r);
9984 else
9985 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9987 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9989 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9990 complain, in_decl);
9991 if (argvec == error_mark_node)
9992 return error_mark_node;
9994 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9995 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
9998 break;
10000 case TEMPLATE_PARM_INDEX:
10001 r = reduce_template_parm_level (t, type, levels, args, complain);
10002 break;
10004 default:
10005 gcc_unreachable ();
10008 return r;
10011 case TREE_LIST:
10013 tree purpose, value, chain;
10015 if (t == void_list_node)
10016 return t;
10018 purpose = TREE_PURPOSE (t);
10019 if (purpose)
10021 purpose = tsubst (purpose, args, complain, in_decl);
10022 if (purpose == error_mark_node)
10023 return error_mark_node;
10025 value = TREE_VALUE (t);
10026 if (value)
10028 value = tsubst (value, args, complain, in_decl);
10029 if (value == error_mark_node)
10030 return error_mark_node;
10032 chain = TREE_CHAIN (t);
10033 if (chain && chain != void_type_node)
10035 chain = tsubst (chain, args, complain, in_decl);
10036 if (chain == error_mark_node)
10037 return error_mark_node;
10039 if (purpose == TREE_PURPOSE (t)
10040 && value == TREE_VALUE (t)
10041 && chain == TREE_CHAIN (t))
10042 return t;
10043 return hash_tree_cons (purpose, value, chain);
10046 case TREE_BINFO:
10047 /* We should never be tsubsting a binfo. */
10048 gcc_unreachable ();
10050 case TREE_VEC:
10051 /* A vector of template arguments. */
10052 gcc_assert (!type);
10053 return tsubst_template_args (t, args, complain, in_decl);
10055 case POINTER_TYPE:
10056 case REFERENCE_TYPE:
10058 enum tree_code code;
10060 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10061 return t;
10063 code = TREE_CODE (t);
10066 /* [temp.deduct]
10068 Type deduction may fail for any of the following
10069 reasons:
10071 -- Attempting to create a pointer to reference type.
10072 -- Attempting to create a reference to a reference type or
10073 a reference to void.
10075 Core issue 106 says that creating a reference to a reference
10076 during instantiation is no longer a cause for failure. We
10077 only enforce this check in strict C++98 mode. */
10078 if ((TREE_CODE (type) == REFERENCE_TYPE
10079 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10080 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10082 static location_t last_loc;
10084 /* We keep track of the last time we issued this error
10085 message to avoid spewing a ton of messages during a
10086 single bad template instantiation. */
10087 if (complain & tf_error
10088 && last_loc != input_location)
10090 if (TREE_CODE (type) == VOID_TYPE)
10091 error ("forming reference to void");
10092 else if (code == POINTER_TYPE)
10093 error ("forming pointer to reference type %qT", type);
10094 else
10095 error ("forming reference to reference type %qT", type);
10096 last_loc = input_location;
10099 return error_mark_node;
10101 else if (code == POINTER_TYPE)
10103 r = build_pointer_type (type);
10104 if (TREE_CODE (type) == METHOD_TYPE)
10105 r = build_ptrmemfunc_type (r);
10107 else if (TREE_CODE (type) == REFERENCE_TYPE)
10108 /* In C++0x, during template argument substitution, when there is an
10109 attempt to create a reference to a reference type, reference
10110 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10112 "If a template-argument for a template-parameter T names a type
10113 that is a reference to a type A, an attempt to create the type
10114 'lvalue reference to cv T' creates the type 'lvalue reference to
10115 A,' while an attempt to create the type type rvalue reference to
10116 cv T' creates the type T"
10118 r = cp_build_reference_type
10119 (TREE_TYPE (type),
10120 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10121 else
10122 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10123 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
10125 if (r != error_mark_node)
10126 /* Will this ever be needed for TYPE_..._TO values? */
10127 layout_type (r);
10129 return r;
10131 case OFFSET_TYPE:
10133 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10134 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10136 /* [temp.deduct]
10138 Type deduction may fail for any of the following
10139 reasons:
10141 -- Attempting to create "pointer to member of T" when T
10142 is not a class type. */
10143 if (complain & tf_error)
10144 error ("creating pointer to member of non-class type %qT", r);
10145 return error_mark_node;
10147 if (TREE_CODE (type) == REFERENCE_TYPE)
10149 if (complain & tf_error)
10150 error ("creating pointer to member reference type %qT", type);
10151 return error_mark_node;
10153 if (TREE_CODE (type) == VOID_TYPE)
10155 if (complain & tf_error)
10156 error ("creating pointer to member of type void");
10157 return error_mark_node;
10159 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10160 if (TREE_CODE (type) == FUNCTION_TYPE)
10162 /* The type of the implicit object parameter gets its
10163 cv-qualifiers from the FUNCTION_TYPE. */
10164 tree memptr;
10165 tree method_type = build_memfn_type (type, r, cp_type_quals (type));
10166 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10167 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10168 complain);
10170 else
10171 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10172 TYPE_QUALS (t),
10173 complain);
10175 case FUNCTION_TYPE:
10176 case METHOD_TYPE:
10178 tree fntype;
10179 tree specs;
10180 fntype = tsubst_function_type (t, args, complain, in_decl);
10181 if (fntype == error_mark_node)
10182 return error_mark_node;
10184 /* Substitute the exception specification. */
10185 specs = tsubst_exception_specification (t, args, complain,
10186 in_decl);
10187 if (specs == error_mark_node)
10188 return error_mark_node;
10189 if (specs)
10190 fntype = build_exception_variant (fntype, specs);
10191 return fntype;
10193 case ARRAY_TYPE:
10195 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10196 if (domain == error_mark_node)
10197 return error_mark_node;
10199 /* As an optimization, we avoid regenerating the array type if
10200 it will obviously be the same as T. */
10201 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10202 return t;
10204 /* These checks should match the ones in grokdeclarator.
10206 [temp.deduct]
10208 The deduction may fail for any of the following reasons:
10210 -- Attempting to create an array with an element type that
10211 is void, a function type, or a reference type, or [DR337]
10212 an abstract class type. */
10213 if (TREE_CODE (type) == VOID_TYPE
10214 || TREE_CODE (type) == FUNCTION_TYPE
10215 || TREE_CODE (type) == REFERENCE_TYPE)
10217 if (complain & tf_error)
10218 error ("creating array of %qT", type);
10219 return error_mark_node;
10221 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10223 if (complain & tf_error)
10224 error ("creating array of %qT, which is an abstract class type",
10225 type);
10226 return error_mark_node;
10229 r = build_cplus_array_type (type, domain);
10231 if (TYPE_USER_ALIGN (t))
10233 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10234 TYPE_USER_ALIGN (r) = 1;
10237 return r;
10240 case PLUS_EXPR:
10241 case MINUS_EXPR:
10243 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10244 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10246 if (e1 == error_mark_node || e2 == error_mark_node)
10247 return error_mark_node;
10249 return fold_build2_loc (input_location,
10250 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10253 case NEGATE_EXPR:
10254 case NOP_EXPR:
10256 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10257 if (e == error_mark_node)
10258 return error_mark_node;
10260 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10263 case TYPENAME_TYPE:
10265 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10266 in_decl, /*entering_scope=*/1);
10267 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10268 complain, in_decl);
10270 if (ctx == error_mark_node || f == error_mark_node)
10271 return error_mark_node;
10273 if (!MAYBE_CLASS_TYPE_P (ctx))
10275 if (complain & tf_error)
10276 error ("%qT is not a class, struct, or union type", ctx);
10277 return error_mark_node;
10279 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10281 /* Normally, make_typename_type does not require that the CTX
10282 have complete type in order to allow things like:
10284 template <class T> struct S { typename S<T>::X Y; };
10286 But, such constructs have already been resolved by this
10287 point, so here CTX really should have complete type, unless
10288 it's a partial instantiation. */
10289 if (!(complain & tf_no_class_instantiations))
10290 ctx = complete_type (ctx);
10291 if (!COMPLETE_TYPE_P (ctx))
10293 if (complain & tf_error)
10294 cxx_incomplete_type_error (NULL_TREE, ctx);
10295 return error_mark_node;
10299 f = make_typename_type (ctx, f, typename_type,
10300 (complain & tf_error) | tf_keep_type_decl);
10301 if (f == error_mark_node)
10302 return f;
10303 if (TREE_CODE (f) == TYPE_DECL)
10305 complain |= tf_ignore_bad_quals;
10306 f = TREE_TYPE (f);
10309 if (TREE_CODE (f) != TYPENAME_TYPE)
10311 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10312 error ("%qT resolves to %qT, which is not an enumeration type",
10313 t, f);
10314 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10315 error ("%qT resolves to %qT, which is is not a class type",
10316 t, f);
10319 return cp_build_qualified_type_real
10320 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10323 case UNBOUND_CLASS_TEMPLATE:
10325 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10326 in_decl, /*entering_scope=*/1);
10327 tree name = TYPE_IDENTIFIER (t);
10328 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10330 if (ctx == error_mark_node || name == error_mark_node)
10331 return error_mark_node;
10333 if (parm_list)
10334 parm_list = tsubst_template_parms (parm_list, args, complain);
10335 return make_unbound_class_template (ctx, name, parm_list, complain);
10338 case INDIRECT_REF:
10339 case ADDR_EXPR:
10340 case CALL_EXPR:
10341 gcc_unreachable ();
10343 case ARRAY_REF:
10345 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10346 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10347 /*integral_constant_expression_p=*/false);
10348 if (e1 == error_mark_node || e2 == error_mark_node)
10349 return error_mark_node;
10351 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10354 case SCOPE_REF:
10356 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10357 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10358 if (e1 == error_mark_node || e2 == error_mark_node)
10359 return error_mark_node;
10361 return build_qualified_name (/*type=*/NULL_TREE,
10362 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10365 case TYPEOF_TYPE:
10367 tree type;
10369 ++cp_unevaluated_operand;
10370 ++c_inhibit_evaluation_warnings;
10372 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10373 complain, in_decl,
10374 /*integral_constant_expression_p=*/false);
10376 --cp_unevaluated_operand;
10377 --c_inhibit_evaluation_warnings;
10379 type = finish_typeof (type);
10380 return cp_build_qualified_type_real (type,
10381 cp_type_quals (t)
10382 | cp_type_quals (type),
10383 complain);
10386 case DECLTYPE_TYPE:
10388 tree type;
10390 ++cp_unevaluated_operand;
10391 ++c_inhibit_evaluation_warnings;
10393 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10394 complain, in_decl,
10395 /*integral_constant_expression_p=*/false);
10397 --cp_unevaluated_operand;
10398 --c_inhibit_evaluation_warnings;
10400 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10401 type = lambda_capture_field_type (type);
10402 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10403 type = lambda_return_type (type);
10404 else
10405 type = finish_decltype_type
10406 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10407 return cp_build_qualified_type_real (type,
10408 cp_type_quals (t)
10409 | cp_type_quals (type),
10410 complain);
10413 case TYPE_ARGUMENT_PACK:
10414 case NONTYPE_ARGUMENT_PACK:
10416 tree r = TYPE_P (t)
10417 ? cxx_make_type (TREE_CODE (t))
10418 : make_node (TREE_CODE (t));
10419 tree packed_out =
10420 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10421 args,
10422 complain,
10423 in_decl);
10424 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10426 /* For template nontype argument packs, also substitute into
10427 the type. */
10428 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10429 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10431 return r;
10433 break;
10435 default:
10436 sorry ("use of %qs in template",
10437 tree_code_name [(int) TREE_CODE (t)]);
10438 return error_mark_node;
10442 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10443 type of the expression on the left-hand side of the "." or "->"
10444 operator. */
10446 static tree
10447 tsubst_baselink (tree baselink, tree object_type,
10448 tree args, tsubst_flags_t complain, tree in_decl)
10450 tree name;
10451 tree qualifying_scope;
10452 tree fns;
10453 tree optype;
10454 tree template_args = 0;
10455 bool template_id_p = false;
10457 /* A baselink indicates a function from a base class. Both the
10458 BASELINK_ACCESS_BINFO and the base class referenced may
10459 indicate bases of the template class, rather than the
10460 instantiated class. In addition, lookups that were not
10461 ambiguous before may be ambiguous now. Therefore, we perform
10462 the lookup again. */
10463 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10464 qualifying_scope = tsubst (qualifying_scope, args,
10465 complain, in_decl);
10466 fns = BASELINK_FUNCTIONS (baselink);
10467 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10468 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10470 template_id_p = true;
10471 template_args = TREE_OPERAND (fns, 1);
10472 fns = TREE_OPERAND (fns, 0);
10473 if (template_args)
10474 template_args = tsubst_template_args (template_args, args,
10475 complain, in_decl);
10477 name = DECL_NAME (get_first_fn (fns));
10478 if (IDENTIFIER_TYPENAME_P (name))
10479 name = mangle_conv_op_name_for_type (optype);
10480 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10482 /* If lookup found a single function, mark it as used at this
10483 point. (If it lookup found multiple functions the one selected
10484 later by overload resolution will be marked as used at that
10485 point.) */
10486 if (BASELINK_P (baselink))
10487 fns = BASELINK_FUNCTIONS (baselink);
10488 if (!template_id_p && !really_overloaded_fn (fns))
10489 mark_used (OVL_CURRENT (fns));
10491 /* Add back the template arguments, if present. */
10492 if (BASELINK_P (baselink) && template_id_p)
10493 BASELINK_FUNCTIONS (baselink)
10494 = build_nt (TEMPLATE_ID_EXPR,
10495 BASELINK_FUNCTIONS (baselink),
10496 template_args);
10497 /* Update the conversion operator type. */
10498 BASELINK_OPTYPE (baselink) = optype;
10500 if (!object_type)
10501 object_type = current_class_type;
10502 return adjust_result_of_qualified_name_lookup (baselink,
10503 qualifying_scope,
10504 object_type);
10507 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10508 true if the qualified-id will be a postfix-expression in-and-of
10509 itself; false if more of the postfix-expression follows the
10510 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10511 of "&". */
10513 static tree
10514 tsubst_qualified_id (tree qualified_id, tree args,
10515 tsubst_flags_t complain, tree in_decl,
10516 bool done, bool address_p)
10518 tree expr;
10519 tree scope;
10520 tree name;
10521 bool is_template;
10522 tree template_args;
10524 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10526 /* Figure out what name to look up. */
10527 name = TREE_OPERAND (qualified_id, 1);
10528 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10530 is_template = true;
10531 template_args = TREE_OPERAND (name, 1);
10532 if (template_args)
10533 template_args = tsubst_template_args (template_args, args,
10534 complain, in_decl);
10535 name = TREE_OPERAND (name, 0);
10537 else
10539 is_template = false;
10540 template_args = NULL_TREE;
10543 /* Substitute into the qualifying scope. When there are no ARGS, we
10544 are just trying to simplify a non-dependent expression. In that
10545 case the qualifying scope may be dependent, and, in any case,
10546 substituting will not help. */
10547 scope = TREE_OPERAND (qualified_id, 0);
10548 if (args)
10550 scope = tsubst (scope, args, complain, in_decl);
10551 expr = tsubst_copy (name, args, complain, in_decl);
10553 else
10554 expr = name;
10556 if (dependent_type_p (scope))
10558 tree type = NULL_TREE;
10559 if (DECL_P (expr) && !dependent_scope_p (scope))
10560 type = TREE_TYPE (expr);
10561 return build_qualified_name (type, scope, expr,
10562 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10565 if (!BASELINK_P (name) && !DECL_P (expr))
10567 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10569 /* A BIT_NOT_EXPR is used to represent a destructor. */
10570 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10572 error ("qualifying type %qT does not match destructor name ~%qT",
10573 scope, TREE_OPERAND (expr, 0));
10574 expr = error_mark_node;
10576 else
10577 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10578 /*is_type_p=*/0, false);
10580 else
10581 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10582 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10583 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10585 if (complain & tf_error)
10587 error ("dependent-name %qE is parsed as a non-type, but "
10588 "instantiation yields a type", qualified_id);
10589 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10591 return error_mark_node;
10595 if (DECL_P (expr))
10597 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10598 scope);
10599 /* Remember that there was a reference to this entity. */
10600 mark_used (expr);
10603 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10605 if (complain & tf_error)
10606 qualified_name_lookup_error (scope,
10607 TREE_OPERAND (qualified_id, 1),
10608 expr, input_location);
10609 return error_mark_node;
10612 if (is_template)
10613 expr = lookup_template_function (expr, template_args);
10615 if (expr == error_mark_node && complain & tf_error)
10616 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10617 expr, input_location);
10618 else if (TYPE_P (scope))
10620 expr = (adjust_result_of_qualified_name_lookup
10621 (expr, scope, current_class_type));
10622 expr = (finish_qualified_id_expr
10623 (scope, expr, done, address_p,
10624 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10625 /*template_arg_p=*/false));
10628 /* Expressions do not generally have reference type. */
10629 if (TREE_CODE (expr) != SCOPE_REF
10630 /* However, if we're about to form a pointer-to-member, we just
10631 want the referenced member referenced. */
10632 && TREE_CODE (expr) != OFFSET_REF)
10633 expr = convert_from_reference (expr);
10635 return expr;
10638 /* Like tsubst, but deals with expressions. This function just replaces
10639 template parms; to finish processing the resultant expression, use
10640 tsubst_expr. */
10642 static tree
10643 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10645 enum tree_code code;
10646 tree r;
10648 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10649 return t;
10651 code = TREE_CODE (t);
10653 switch (code)
10655 case PARM_DECL:
10656 r = retrieve_local_specialization (t);
10658 if (r == NULL)
10660 tree c;
10661 /* This can happen for a parameter name used later in a function
10662 declaration (such as in a late-specified return type). Just
10663 make a dummy decl, since it's only used for its type. */
10664 gcc_assert (cp_unevaluated_operand != 0);
10665 /* We copy T because want to tsubst the PARM_DECL only,
10666 not the following PARM_DECLs that are chained to T. */
10667 c = copy_node (t);
10668 r = tsubst_decl (c, args, complain);
10669 /* Give it the template pattern as its context; its true context
10670 hasn't been instantiated yet and this is good enough for
10671 mangling. */
10672 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10675 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10676 r = ARGUMENT_PACK_SELECT_ARG (r);
10677 mark_used (r);
10678 return r;
10680 case CONST_DECL:
10682 tree enum_type;
10683 tree v;
10685 if (DECL_TEMPLATE_PARM_P (t))
10686 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10687 /* There is no need to substitute into namespace-scope
10688 enumerators. */
10689 if (DECL_NAMESPACE_SCOPE_P (t))
10690 return t;
10691 /* If ARGS is NULL, then T is known to be non-dependent. */
10692 if (args == NULL_TREE)
10693 return integral_constant_value (t);
10695 /* Unfortunately, we cannot just call lookup_name here.
10696 Consider:
10698 template <int I> int f() {
10699 enum E { a = I };
10700 struct S { void g() { E e = a; } };
10703 When we instantiate f<7>::S::g(), say, lookup_name is not
10704 clever enough to find f<7>::a. */
10705 enum_type
10706 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10707 /*entering_scope=*/0);
10709 for (v = TYPE_VALUES (enum_type);
10710 v != NULL_TREE;
10711 v = TREE_CHAIN (v))
10712 if (TREE_PURPOSE (v) == DECL_NAME (t))
10713 return TREE_VALUE (v);
10715 /* We didn't find the name. That should never happen; if
10716 name-lookup found it during preliminary parsing, we
10717 should find it again here during instantiation. */
10718 gcc_unreachable ();
10720 return t;
10722 case FIELD_DECL:
10723 if (DECL_CONTEXT (t))
10725 tree ctx;
10727 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10728 /*entering_scope=*/1);
10729 if (ctx != DECL_CONTEXT (t))
10731 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10732 if (!r)
10734 if (complain & tf_error)
10735 error ("using invalid field %qD", t);
10736 return error_mark_node;
10738 return r;
10742 return t;
10744 case VAR_DECL:
10745 case FUNCTION_DECL:
10746 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10747 || local_variable_p (t))
10748 t = tsubst (t, args, complain, in_decl);
10749 mark_used (t);
10750 return t;
10752 case BASELINK:
10753 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10755 case TEMPLATE_DECL:
10756 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10757 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10758 args, complain, in_decl);
10759 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10760 return tsubst (t, args, complain, in_decl);
10761 else if (DECL_CLASS_SCOPE_P (t)
10762 && uses_template_parms (DECL_CONTEXT (t)))
10764 /* Template template argument like the following example need
10765 special treatment:
10767 template <template <class> class TT> struct C {};
10768 template <class T> struct D {
10769 template <class U> struct E {};
10770 C<E> c; // #1
10772 D<int> d; // #2
10774 We are processing the template argument `E' in #1 for
10775 the template instantiation #2. Originally, `E' is a
10776 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10777 have to substitute this with one having context `D<int>'. */
10779 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10780 return lookup_field (context, DECL_NAME(t), 0, false);
10782 else
10783 /* Ordinary template template argument. */
10784 return t;
10786 case CAST_EXPR:
10787 case REINTERPRET_CAST_EXPR:
10788 case CONST_CAST_EXPR:
10789 case STATIC_CAST_EXPR:
10790 case DYNAMIC_CAST_EXPR:
10791 case NOP_EXPR:
10792 return build1
10793 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10794 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10796 case SIZEOF_EXPR:
10797 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10799 /* We only want to compute the number of arguments. */
10800 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10801 complain, in_decl);
10802 int len = 0;
10804 if (TREE_CODE (expanded) == TREE_VEC)
10805 len = TREE_VEC_LENGTH (expanded);
10807 if (expanded == error_mark_node)
10808 return error_mark_node;
10809 else if (PACK_EXPANSION_P (expanded)
10810 || (TREE_CODE (expanded) == TREE_VEC
10811 && len > 0
10812 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10814 if (TREE_CODE (expanded) == TREE_VEC)
10815 expanded = TREE_VEC_ELT (expanded, len - 1);
10817 if (TYPE_P (expanded))
10818 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10819 complain & tf_error);
10820 else
10821 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10822 complain & tf_error);
10824 else
10825 return build_int_cst (size_type_node, len);
10827 /* Fall through */
10829 case INDIRECT_REF:
10830 case NEGATE_EXPR:
10831 case TRUTH_NOT_EXPR:
10832 case BIT_NOT_EXPR:
10833 case ADDR_EXPR:
10834 case UNARY_PLUS_EXPR: /* Unary + */
10835 case ALIGNOF_EXPR:
10836 case ARROW_EXPR:
10837 case THROW_EXPR:
10838 case TYPEID_EXPR:
10839 case REALPART_EXPR:
10840 case IMAGPART_EXPR:
10841 return build1
10842 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10843 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10845 case COMPONENT_REF:
10847 tree object;
10848 tree name;
10850 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10851 name = TREE_OPERAND (t, 1);
10852 if (TREE_CODE (name) == BIT_NOT_EXPR)
10854 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10855 complain, in_decl);
10856 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10858 else if (TREE_CODE (name) == SCOPE_REF
10859 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10861 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10862 complain, in_decl);
10863 name = TREE_OPERAND (name, 1);
10864 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10865 complain, in_decl);
10866 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10867 name = build_qualified_name (/*type=*/NULL_TREE,
10868 base, name,
10869 /*template_p=*/false);
10871 else if (TREE_CODE (name) == BASELINK)
10872 name = tsubst_baselink (name,
10873 non_reference (TREE_TYPE (object)),
10874 args, complain,
10875 in_decl);
10876 else
10877 name = tsubst_copy (name, args, complain, in_decl);
10878 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10881 case PLUS_EXPR:
10882 case MINUS_EXPR:
10883 case MULT_EXPR:
10884 case TRUNC_DIV_EXPR:
10885 case CEIL_DIV_EXPR:
10886 case FLOOR_DIV_EXPR:
10887 case ROUND_DIV_EXPR:
10888 case EXACT_DIV_EXPR:
10889 case BIT_AND_EXPR:
10890 case BIT_IOR_EXPR:
10891 case BIT_XOR_EXPR:
10892 case TRUNC_MOD_EXPR:
10893 case FLOOR_MOD_EXPR:
10894 case TRUTH_ANDIF_EXPR:
10895 case TRUTH_ORIF_EXPR:
10896 case TRUTH_AND_EXPR:
10897 case TRUTH_OR_EXPR:
10898 case RSHIFT_EXPR:
10899 case LSHIFT_EXPR:
10900 case RROTATE_EXPR:
10901 case LROTATE_EXPR:
10902 case EQ_EXPR:
10903 case NE_EXPR:
10904 case MAX_EXPR:
10905 case MIN_EXPR:
10906 case LE_EXPR:
10907 case GE_EXPR:
10908 case LT_EXPR:
10909 case GT_EXPR:
10910 case COMPOUND_EXPR:
10911 case DOTSTAR_EXPR:
10912 case MEMBER_REF:
10913 case PREDECREMENT_EXPR:
10914 case PREINCREMENT_EXPR:
10915 case POSTDECREMENT_EXPR:
10916 case POSTINCREMENT_EXPR:
10917 return build_nt
10918 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10919 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10921 case SCOPE_REF:
10922 return build_qualified_name (/*type=*/NULL_TREE,
10923 tsubst_copy (TREE_OPERAND (t, 0),
10924 args, complain, in_decl),
10925 tsubst_copy (TREE_OPERAND (t, 1),
10926 args, complain, in_decl),
10927 QUALIFIED_NAME_IS_TEMPLATE (t));
10929 case ARRAY_REF:
10930 return build_nt
10931 (ARRAY_REF,
10932 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10933 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10934 NULL_TREE, NULL_TREE);
10936 case CALL_EXPR:
10938 int n = VL_EXP_OPERAND_LENGTH (t);
10939 tree result = build_vl_exp (CALL_EXPR, n);
10940 int i;
10941 for (i = 0; i < n; i++)
10942 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10943 complain, in_decl);
10944 return result;
10947 case COND_EXPR:
10948 case MODOP_EXPR:
10949 case PSEUDO_DTOR_EXPR:
10951 r = build_nt
10952 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10953 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10954 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10955 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10956 return r;
10959 case NEW_EXPR:
10961 r = build_nt
10962 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10963 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10964 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10965 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10966 return r;
10969 case DELETE_EXPR:
10971 r = build_nt
10972 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10973 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10974 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10975 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10976 return r;
10979 case TEMPLATE_ID_EXPR:
10981 /* Substituted template arguments */
10982 tree fn = TREE_OPERAND (t, 0);
10983 tree targs = TREE_OPERAND (t, 1);
10985 fn = tsubst_copy (fn, args, complain, in_decl);
10986 if (targs)
10987 targs = tsubst_template_args (targs, args, complain, in_decl);
10989 return lookup_template_function (fn, targs);
10992 case TREE_LIST:
10994 tree purpose, value, chain;
10996 if (t == void_list_node)
10997 return t;
10999 purpose = TREE_PURPOSE (t);
11000 if (purpose)
11001 purpose = tsubst_copy (purpose, args, complain, in_decl);
11002 value = TREE_VALUE (t);
11003 if (value)
11004 value = tsubst_copy (value, args, complain, in_decl);
11005 chain = TREE_CHAIN (t);
11006 if (chain && chain != void_type_node)
11007 chain = tsubst_copy (chain, args, complain, in_decl);
11008 if (purpose == TREE_PURPOSE (t)
11009 && value == TREE_VALUE (t)
11010 && chain == TREE_CHAIN (t))
11011 return t;
11012 return tree_cons (purpose, value, chain);
11015 case RECORD_TYPE:
11016 case UNION_TYPE:
11017 case ENUMERAL_TYPE:
11018 case INTEGER_TYPE:
11019 case TEMPLATE_TYPE_PARM:
11020 case TEMPLATE_TEMPLATE_PARM:
11021 case BOUND_TEMPLATE_TEMPLATE_PARM:
11022 case TEMPLATE_PARM_INDEX:
11023 case POINTER_TYPE:
11024 case REFERENCE_TYPE:
11025 case OFFSET_TYPE:
11026 case FUNCTION_TYPE:
11027 case METHOD_TYPE:
11028 case ARRAY_TYPE:
11029 case TYPENAME_TYPE:
11030 case UNBOUND_CLASS_TEMPLATE:
11031 case TYPEOF_TYPE:
11032 case DECLTYPE_TYPE:
11033 case TYPE_DECL:
11034 return tsubst (t, args, complain, in_decl);
11036 case IDENTIFIER_NODE:
11037 if (IDENTIFIER_TYPENAME_P (t))
11039 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11040 return mangle_conv_op_name_for_type (new_type);
11042 else
11043 return t;
11045 case CONSTRUCTOR:
11046 /* This is handled by tsubst_copy_and_build. */
11047 gcc_unreachable ();
11049 case VA_ARG_EXPR:
11050 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11051 in_decl),
11052 tsubst (TREE_TYPE (t), args, complain, in_decl));
11054 case CLEANUP_POINT_EXPR:
11055 /* We shouldn't have built any of these during initial template
11056 generation. Instead, they should be built during instantiation
11057 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11058 gcc_unreachable ();
11060 case OFFSET_REF:
11061 mark_used (TREE_OPERAND (t, 1));
11062 return t;
11064 case EXPR_PACK_EXPANSION:
11065 error ("invalid use of pack expansion expression");
11066 return error_mark_node;
11068 case NONTYPE_ARGUMENT_PACK:
11069 error ("use %<...%> to expand argument pack");
11070 return error_mark_node;
11072 default:
11073 return t;
11077 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11079 static tree
11080 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11081 tree in_decl)
11083 tree new_clauses = NULL, nc, oc;
11085 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11087 nc = copy_node (oc);
11088 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11089 new_clauses = nc;
11091 switch (OMP_CLAUSE_CODE (nc))
11093 case OMP_CLAUSE_LASTPRIVATE:
11094 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11096 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11097 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11098 in_decl, /*integral_constant_expression_p=*/false);
11099 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11100 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11102 /* FALLTHRU */
11103 case OMP_CLAUSE_PRIVATE:
11104 case OMP_CLAUSE_SHARED:
11105 case OMP_CLAUSE_FIRSTPRIVATE:
11106 case OMP_CLAUSE_REDUCTION:
11107 case OMP_CLAUSE_COPYIN:
11108 case OMP_CLAUSE_COPYPRIVATE:
11109 case OMP_CLAUSE_IF:
11110 case OMP_CLAUSE_NUM_THREADS:
11111 case OMP_CLAUSE_SCHEDULE:
11112 case OMP_CLAUSE_COLLAPSE:
11113 OMP_CLAUSE_OPERAND (nc, 0)
11114 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11115 in_decl, /*integral_constant_expression_p=*/false);
11116 break;
11117 case OMP_CLAUSE_NOWAIT:
11118 case OMP_CLAUSE_ORDERED:
11119 case OMP_CLAUSE_DEFAULT:
11120 case OMP_CLAUSE_UNTIED:
11121 break;
11122 default:
11123 gcc_unreachable ();
11127 return finish_omp_clauses (nreverse (new_clauses));
11130 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11132 static tree
11133 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11134 tree in_decl)
11136 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11138 tree purpose, value, chain;
11140 if (t == NULL)
11141 return t;
11143 if (TREE_CODE (t) != TREE_LIST)
11144 return tsubst_copy_and_build (t, args, complain, in_decl,
11145 /*function_p=*/false,
11146 /*integral_constant_expression_p=*/false);
11148 if (t == void_list_node)
11149 return t;
11151 purpose = TREE_PURPOSE (t);
11152 if (purpose)
11153 purpose = RECUR (purpose);
11154 value = TREE_VALUE (t);
11155 if (value && TREE_CODE (value) != LABEL_DECL)
11156 value = RECUR (value);
11157 chain = TREE_CHAIN (t);
11158 if (chain && chain != void_type_node)
11159 chain = RECUR (chain);
11160 return tree_cons (purpose, value, chain);
11161 #undef RECUR
11164 /* Substitute one OMP_FOR iterator. */
11166 static void
11167 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11168 tree condv, tree incrv, tree *clauses,
11169 tree args, tsubst_flags_t complain, tree in_decl,
11170 bool integral_constant_expression_p)
11172 #define RECUR(NODE) \
11173 tsubst_expr ((NODE), args, complain, in_decl, \
11174 integral_constant_expression_p)
11175 tree decl, init, cond, incr, auto_node;
11177 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11178 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11179 decl = RECUR (TREE_OPERAND (init, 0));
11180 init = TREE_OPERAND (init, 1);
11181 auto_node = type_uses_auto (TREE_TYPE (decl));
11182 if (auto_node && init)
11184 tree init_expr = init;
11185 if (TREE_CODE (init_expr) == DECL_EXPR)
11186 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11187 init_expr = RECUR (init_expr);
11188 TREE_TYPE (decl)
11189 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11191 gcc_assert (!type_dependent_expression_p (decl));
11193 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11195 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11196 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11197 if (TREE_CODE (incr) == MODIFY_EXPR)
11198 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11199 RECUR (TREE_OPERAND (incr, 1)),
11200 complain);
11201 else
11202 incr = RECUR (incr);
11203 TREE_VEC_ELT (declv, i) = decl;
11204 TREE_VEC_ELT (initv, i) = init;
11205 TREE_VEC_ELT (condv, i) = cond;
11206 TREE_VEC_ELT (incrv, i) = incr;
11207 return;
11210 if (init && TREE_CODE (init) != DECL_EXPR)
11212 tree c;
11213 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11215 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11216 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11217 && OMP_CLAUSE_DECL (c) == decl)
11218 break;
11219 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11220 && OMP_CLAUSE_DECL (c) == decl)
11221 error ("iteration variable %qD should not be firstprivate", decl);
11222 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11223 && OMP_CLAUSE_DECL (c) == decl)
11224 error ("iteration variable %qD should not be reduction", decl);
11226 if (c == NULL)
11228 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11229 OMP_CLAUSE_DECL (c) = decl;
11230 c = finish_omp_clauses (c);
11231 if (c)
11233 OMP_CLAUSE_CHAIN (c) = *clauses;
11234 *clauses = c;
11238 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11239 if (COMPARISON_CLASS_P (cond))
11240 cond = build2 (TREE_CODE (cond), boolean_type_node,
11241 RECUR (TREE_OPERAND (cond, 0)),
11242 RECUR (TREE_OPERAND (cond, 1)));
11243 else
11244 cond = RECUR (cond);
11245 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11246 switch (TREE_CODE (incr))
11248 case PREINCREMENT_EXPR:
11249 case PREDECREMENT_EXPR:
11250 case POSTINCREMENT_EXPR:
11251 case POSTDECREMENT_EXPR:
11252 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11253 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11254 break;
11255 case MODIFY_EXPR:
11256 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11257 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11259 tree rhs = TREE_OPERAND (incr, 1);
11260 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11261 RECUR (TREE_OPERAND (incr, 0)),
11262 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11263 RECUR (TREE_OPERAND (rhs, 0)),
11264 RECUR (TREE_OPERAND (rhs, 1))));
11266 else
11267 incr = RECUR (incr);
11268 break;
11269 case MODOP_EXPR:
11270 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11271 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11273 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11274 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11275 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11276 TREE_TYPE (decl), lhs,
11277 RECUR (TREE_OPERAND (incr, 2))));
11279 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11280 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11281 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11283 tree rhs = TREE_OPERAND (incr, 2);
11284 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11285 RECUR (TREE_OPERAND (incr, 0)),
11286 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11287 RECUR (TREE_OPERAND (rhs, 0)),
11288 RECUR (TREE_OPERAND (rhs, 1))));
11290 else
11291 incr = RECUR (incr);
11292 break;
11293 default:
11294 incr = RECUR (incr);
11295 break;
11298 TREE_VEC_ELT (declv, i) = decl;
11299 TREE_VEC_ELT (initv, i) = init;
11300 TREE_VEC_ELT (condv, i) = cond;
11301 TREE_VEC_ELT (incrv, i) = incr;
11302 #undef RECUR
11305 /* Like tsubst_copy for expressions, etc. but also does semantic
11306 processing. */
11308 static tree
11309 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11310 bool integral_constant_expression_p)
11312 #define RECUR(NODE) \
11313 tsubst_expr ((NODE), args, complain, in_decl, \
11314 integral_constant_expression_p)
11316 tree stmt, tmp;
11318 if (t == NULL_TREE || t == error_mark_node)
11319 return t;
11321 if (EXPR_HAS_LOCATION (t))
11322 input_location = EXPR_LOCATION (t);
11323 if (STATEMENT_CODE_P (TREE_CODE (t)))
11324 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11326 switch (TREE_CODE (t))
11328 case STATEMENT_LIST:
11330 tree_stmt_iterator i;
11331 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11332 RECUR (tsi_stmt (i));
11333 break;
11336 case CTOR_INITIALIZER:
11337 finish_mem_initializers (tsubst_initializer_list
11338 (TREE_OPERAND (t, 0), args));
11339 break;
11341 case RETURN_EXPR:
11342 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11343 break;
11345 case EXPR_STMT:
11346 tmp = RECUR (EXPR_STMT_EXPR (t));
11347 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11348 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11349 else
11350 finish_expr_stmt (tmp);
11351 break;
11353 case USING_STMT:
11354 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11355 break;
11357 case DECL_EXPR:
11359 tree decl;
11360 tree init;
11362 decl = DECL_EXPR_DECL (t);
11363 if (TREE_CODE (decl) == LABEL_DECL)
11364 finish_label_decl (DECL_NAME (decl));
11365 else if (TREE_CODE (decl) == USING_DECL)
11367 tree scope = USING_DECL_SCOPE (decl);
11368 tree name = DECL_NAME (decl);
11369 tree decl;
11371 scope = RECUR (scope);
11372 decl = lookup_qualified_name (scope, name,
11373 /*is_type_p=*/false,
11374 /*complain=*/false);
11375 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11376 qualified_name_lookup_error (scope, name, decl, input_location);
11377 else
11378 do_local_using_decl (decl, scope, name);
11380 else
11382 init = DECL_INITIAL (decl);
11383 decl = tsubst (decl, args, complain, in_decl);
11384 if (decl != error_mark_node)
11386 /* By marking the declaration as instantiated, we avoid
11387 trying to instantiate it. Since instantiate_decl can't
11388 handle local variables, and since we've already done
11389 all that needs to be done, that's the right thing to
11390 do. */
11391 if (TREE_CODE (decl) == VAR_DECL)
11392 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11393 if (TREE_CODE (decl) == VAR_DECL
11394 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11395 /* Anonymous aggregates are a special case. */
11396 finish_anon_union (decl);
11397 else
11399 maybe_push_decl (decl);
11400 if (TREE_CODE (decl) == VAR_DECL
11401 && DECL_PRETTY_FUNCTION_P (decl))
11403 /* For __PRETTY_FUNCTION__ we have to adjust the
11404 initializer. */
11405 const char *const name
11406 = cxx_printable_name (current_function_decl, 2);
11407 init = cp_fname_init (name, &TREE_TYPE (decl));
11409 else
11411 tree t = RECUR (init);
11413 if (init && !t)
11414 /* If we had an initializer but it
11415 instantiated to nothing,
11416 value-initialize the object. This will
11417 only occur when the initializer was a
11418 pack expansion where the parameter packs
11419 used in that expansion were of length
11420 zero. */
11421 init = build_value_init (TREE_TYPE (decl));
11422 else
11423 init = t;
11426 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11431 /* A DECL_EXPR can also be used as an expression, in the condition
11432 clause of an if/for/while construct. */
11433 return decl;
11436 case FOR_STMT:
11437 stmt = begin_for_stmt ();
11438 RECUR (FOR_INIT_STMT (t));
11439 finish_for_init_stmt (stmt);
11440 tmp = RECUR (FOR_COND (t));
11441 finish_for_cond (tmp, stmt);
11442 tmp = RECUR (FOR_EXPR (t));
11443 finish_for_expr (tmp, stmt);
11444 RECUR (FOR_BODY (t));
11445 finish_for_stmt (stmt);
11446 break;
11448 case WHILE_STMT:
11449 stmt = begin_while_stmt ();
11450 tmp = RECUR (WHILE_COND (t));
11451 finish_while_stmt_cond (tmp, stmt);
11452 RECUR (WHILE_BODY (t));
11453 finish_while_stmt (stmt);
11454 break;
11456 case DO_STMT:
11457 stmt = begin_do_stmt ();
11458 RECUR (DO_BODY (t));
11459 finish_do_body (stmt);
11460 tmp = RECUR (DO_COND (t));
11461 finish_do_stmt (tmp, stmt);
11462 break;
11464 case IF_STMT:
11465 stmt = begin_if_stmt ();
11466 tmp = RECUR (IF_COND (t));
11467 finish_if_stmt_cond (tmp, stmt);
11468 RECUR (THEN_CLAUSE (t));
11469 finish_then_clause (stmt);
11471 if (ELSE_CLAUSE (t))
11473 begin_else_clause (stmt);
11474 RECUR (ELSE_CLAUSE (t));
11475 finish_else_clause (stmt);
11478 finish_if_stmt (stmt);
11479 break;
11481 case BIND_EXPR:
11482 if (BIND_EXPR_BODY_BLOCK (t))
11483 stmt = begin_function_body ();
11484 else
11485 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11486 ? BCS_TRY_BLOCK : 0);
11488 RECUR (BIND_EXPR_BODY (t));
11490 if (BIND_EXPR_BODY_BLOCK (t))
11491 finish_function_body (stmt);
11492 else
11493 finish_compound_stmt (stmt);
11494 break;
11496 case BREAK_STMT:
11497 finish_break_stmt ();
11498 break;
11500 case CONTINUE_STMT:
11501 finish_continue_stmt ();
11502 break;
11504 case SWITCH_STMT:
11505 stmt = begin_switch_stmt ();
11506 tmp = RECUR (SWITCH_STMT_COND (t));
11507 finish_switch_cond (tmp, stmt);
11508 RECUR (SWITCH_STMT_BODY (t));
11509 finish_switch_stmt (stmt);
11510 break;
11512 case CASE_LABEL_EXPR:
11513 finish_case_label (EXPR_LOCATION (t),
11514 RECUR (CASE_LOW (t)),
11515 RECUR (CASE_HIGH (t)));
11516 break;
11518 case LABEL_EXPR:
11520 tree decl = LABEL_EXPR_LABEL (t);
11521 tree label;
11523 label = finish_label_stmt (DECL_NAME (decl));
11524 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11525 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11527 break;
11529 case GOTO_EXPR:
11530 tmp = GOTO_DESTINATION (t);
11531 if (TREE_CODE (tmp) != LABEL_DECL)
11532 /* Computed goto's must be tsubst'd into. On the other hand,
11533 non-computed gotos must not be; the identifier in question
11534 will have no binding. */
11535 tmp = RECUR (tmp);
11536 else
11537 tmp = DECL_NAME (tmp);
11538 finish_goto_stmt (tmp);
11539 break;
11541 case ASM_EXPR:
11542 tmp = finish_asm_stmt
11543 (ASM_VOLATILE_P (t),
11544 RECUR (ASM_STRING (t)),
11545 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11546 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11547 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11548 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11550 tree asm_expr = tmp;
11551 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11552 asm_expr = TREE_OPERAND (asm_expr, 0);
11553 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11555 break;
11557 case TRY_BLOCK:
11558 if (CLEANUP_P (t))
11560 stmt = begin_try_block ();
11561 RECUR (TRY_STMTS (t));
11562 finish_cleanup_try_block (stmt);
11563 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11565 else
11567 tree compound_stmt = NULL_TREE;
11569 if (FN_TRY_BLOCK_P (t))
11570 stmt = begin_function_try_block (&compound_stmt);
11571 else
11572 stmt = begin_try_block ();
11574 RECUR (TRY_STMTS (t));
11576 if (FN_TRY_BLOCK_P (t))
11577 finish_function_try_block (stmt);
11578 else
11579 finish_try_block (stmt);
11581 RECUR (TRY_HANDLERS (t));
11582 if (FN_TRY_BLOCK_P (t))
11583 finish_function_handler_sequence (stmt, compound_stmt);
11584 else
11585 finish_handler_sequence (stmt);
11587 break;
11589 case HANDLER:
11591 tree decl = HANDLER_PARMS (t);
11593 if (decl)
11595 decl = tsubst (decl, args, complain, in_decl);
11596 /* Prevent instantiate_decl from trying to instantiate
11597 this variable. We've already done all that needs to be
11598 done. */
11599 if (decl != error_mark_node)
11600 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11602 stmt = begin_handler ();
11603 finish_handler_parms (decl, stmt);
11604 RECUR (HANDLER_BODY (t));
11605 finish_handler (stmt);
11607 break;
11609 case TAG_DEFN:
11610 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11611 break;
11613 case STATIC_ASSERT:
11615 tree condition =
11616 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11617 args,
11618 complain, in_decl,
11619 /*integral_constant_expression_p=*/true);
11620 finish_static_assert (condition,
11621 STATIC_ASSERT_MESSAGE (t),
11622 STATIC_ASSERT_SOURCE_LOCATION (t),
11623 /*member_p=*/false);
11625 break;
11627 case OMP_PARALLEL:
11628 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11629 args, complain, in_decl);
11630 stmt = begin_omp_parallel ();
11631 RECUR (OMP_PARALLEL_BODY (t));
11632 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11633 = OMP_PARALLEL_COMBINED (t);
11634 break;
11636 case OMP_TASK:
11637 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11638 args, complain, in_decl);
11639 stmt = begin_omp_task ();
11640 RECUR (OMP_TASK_BODY (t));
11641 finish_omp_task (tmp, stmt);
11642 break;
11644 case OMP_FOR:
11646 tree clauses, body, pre_body;
11647 tree declv, initv, condv, incrv;
11648 int i;
11650 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11651 args, complain, in_decl);
11652 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11653 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11654 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11655 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11657 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11658 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11659 &clauses, args, complain, in_decl,
11660 integral_constant_expression_p);
11662 stmt = begin_omp_structured_block ();
11664 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11665 if (TREE_VEC_ELT (initv, i) == NULL
11666 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11667 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11668 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11670 tree init = RECUR (TREE_VEC_ELT (initv, i));
11671 gcc_assert (init == TREE_VEC_ELT (declv, i));
11672 TREE_VEC_ELT (initv, i) = NULL_TREE;
11674 else
11676 tree decl_expr = TREE_VEC_ELT (initv, i);
11677 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11678 gcc_assert (init != NULL);
11679 TREE_VEC_ELT (initv, i) = RECUR (init);
11680 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11681 RECUR (decl_expr);
11682 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11685 pre_body = push_stmt_list ();
11686 RECUR (OMP_FOR_PRE_BODY (t));
11687 pre_body = pop_stmt_list (pre_body);
11689 body = push_stmt_list ();
11690 RECUR (OMP_FOR_BODY (t));
11691 body = pop_stmt_list (body);
11693 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11694 body, pre_body, clauses);
11696 add_stmt (finish_omp_structured_block (stmt));
11698 break;
11700 case OMP_SECTIONS:
11701 case OMP_SINGLE:
11702 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11703 stmt = push_stmt_list ();
11704 RECUR (OMP_BODY (t));
11705 stmt = pop_stmt_list (stmt);
11707 t = copy_node (t);
11708 OMP_BODY (t) = stmt;
11709 OMP_CLAUSES (t) = tmp;
11710 add_stmt (t);
11711 break;
11713 case OMP_SECTION:
11714 case OMP_CRITICAL:
11715 case OMP_MASTER:
11716 case OMP_ORDERED:
11717 stmt = push_stmt_list ();
11718 RECUR (OMP_BODY (t));
11719 stmt = pop_stmt_list (stmt);
11721 t = copy_node (t);
11722 OMP_BODY (t) = stmt;
11723 add_stmt (t);
11724 break;
11726 case OMP_ATOMIC:
11727 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11729 tree op1 = TREE_OPERAND (t, 1);
11730 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11731 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11732 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11734 break;
11736 case EXPR_PACK_EXPANSION:
11737 error ("invalid use of pack expansion expression");
11738 return error_mark_node;
11740 case NONTYPE_ARGUMENT_PACK:
11741 error ("use %<...%> to expand argument pack");
11742 return error_mark_node;
11744 default:
11745 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11747 return tsubst_copy_and_build (t, args, complain, in_decl,
11748 /*function_p=*/false,
11749 integral_constant_expression_p);
11752 return NULL_TREE;
11753 #undef RECUR
11756 /* T is a postfix-expression that is not being used in a function
11757 call. Return the substituted version of T. */
11759 static tree
11760 tsubst_non_call_postfix_expression (tree t, tree args,
11761 tsubst_flags_t complain,
11762 tree in_decl)
11764 if (TREE_CODE (t) == SCOPE_REF)
11765 t = tsubst_qualified_id (t, args, complain, in_decl,
11766 /*done=*/false, /*address_p=*/false);
11767 else
11768 t = tsubst_copy_and_build (t, args, complain, in_decl,
11769 /*function_p=*/false,
11770 /*integral_constant_expression_p=*/false);
11772 return t;
11775 /* Like tsubst but deals with expressions and performs semantic
11776 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11778 tree
11779 tsubst_copy_and_build (tree t,
11780 tree args,
11781 tsubst_flags_t complain,
11782 tree in_decl,
11783 bool function_p,
11784 bool integral_constant_expression_p)
11786 #define RECUR(NODE) \
11787 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11788 /*function_p=*/false, \
11789 integral_constant_expression_p)
11791 tree op1;
11793 if (t == NULL_TREE || t == error_mark_node)
11794 return t;
11796 switch (TREE_CODE (t))
11798 case USING_DECL:
11799 t = DECL_NAME (t);
11800 /* Fall through. */
11801 case IDENTIFIER_NODE:
11803 tree decl;
11804 cp_id_kind idk;
11805 bool non_integral_constant_expression_p;
11806 const char *error_msg;
11808 if (IDENTIFIER_TYPENAME_P (t))
11810 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11811 t = mangle_conv_op_name_for_type (new_type);
11814 /* Look up the name. */
11815 decl = lookup_name (t);
11817 /* By convention, expressions use ERROR_MARK_NODE to indicate
11818 failure, not NULL_TREE. */
11819 if (decl == NULL_TREE)
11820 decl = error_mark_node;
11822 decl = finish_id_expression (t, decl, NULL_TREE,
11823 &idk,
11824 integral_constant_expression_p,
11825 /*allow_non_integral_constant_expression_p=*/false,
11826 &non_integral_constant_expression_p,
11827 /*template_p=*/false,
11828 /*done=*/true,
11829 /*address_p=*/false,
11830 /*template_arg_p=*/false,
11831 &error_msg,
11832 input_location);
11833 if (error_msg)
11834 error (error_msg);
11835 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11836 decl = unqualified_name_lookup_error (decl);
11837 return decl;
11840 case TEMPLATE_ID_EXPR:
11842 tree object;
11843 tree templ = RECUR (TREE_OPERAND (t, 0));
11844 tree targs = TREE_OPERAND (t, 1);
11846 if (targs)
11847 targs = tsubst_template_args (targs, args, complain, in_decl);
11849 if (TREE_CODE (templ) == COMPONENT_REF)
11851 object = TREE_OPERAND (templ, 0);
11852 templ = TREE_OPERAND (templ, 1);
11854 else
11855 object = NULL_TREE;
11856 templ = lookup_template_function (templ, targs);
11858 if (object)
11859 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11860 object, templ, NULL_TREE);
11861 else
11862 return baselink_for_fns (templ);
11865 case INDIRECT_REF:
11867 tree r = RECUR (TREE_OPERAND (t, 0));
11869 if (REFERENCE_REF_P (t))
11871 /* A type conversion to reference type will be enclosed in
11872 such an indirect ref, but the substitution of the cast
11873 will have also added such an indirect ref. */
11874 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11875 r = convert_from_reference (r);
11877 else
11878 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
11879 return r;
11882 case NOP_EXPR:
11883 return build_nop
11884 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11885 RECUR (TREE_OPERAND (t, 0)));
11887 case CAST_EXPR:
11888 case REINTERPRET_CAST_EXPR:
11889 case CONST_CAST_EXPR:
11890 case DYNAMIC_CAST_EXPR:
11891 case STATIC_CAST_EXPR:
11893 tree type;
11894 tree op;
11896 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11897 if (integral_constant_expression_p
11898 && !cast_valid_in_integral_constant_expression_p (type))
11900 if (complain & tf_error)
11901 error ("a cast to a type other than an integral or "
11902 "enumeration type cannot appear in a constant-expression");
11903 return error_mark_node;
11906 op = RECUR (TREE_OPERAND (t, 0));
11908 switch (TREE_CODE (t))
11910 case CAST_EXPR:
11911 return build_functional_cast (type, op, complain);
11912 case REINTERPRET_CAST_EXPR:
11913 return build_reinterpret_cast (type, op, complain);
11914 case CONST_CAST_EXPR:
11915 return build_const_cast (type, op, complain);
11916 case DYNAMIC_CAST_EXPR:
11917 return build_dynamic_cast (type, op, complain);
11918 case STATIC_CAST_EXPR:
11919 return build_static_cast (type, op, complain);
11920 default:
11921 gcc_unreachable ();
11925 case POSTDECREMENT_EXPR:
11926 case POSTINCREMENT_EXPR:
11927 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11928 args, complain, in_decl);
11929 return build_x_unary_op (TREE_CODE (t), op1, complain);
11931 case PREDECREMENT_EXPR:
11932 case PREINCREMENT_EXPR:
11933 case NEGATE_EXPR:
11934 case BIT_NOT_EXPR:
11935 case ABS_EXPR:
11936 case TRUTH_NOT_EXPR:
11937 case UNARY_PLUS_EXPR: /* Unary + */
11938 case REALPART_EXPR:
11939 case IMAGPART_EXPR:
11940 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11941 complain);
11943 case ADDR_EXPR:
11944 op1 = TREE_OPERAND (t, 0);
11945 if (TREE_CODE (op1) == SCOPE_REF)
11946 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11947 /*done=*/true, /*address_p=*/true);
11948 else
11949 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11950 in_decl);
11951 if (TREE_CODE (op1) == LABEL_DECL)
11952 return finish_label_address_expr (DECL_NAME (op1),
11953 EXPR_LOCATION (op1));
11954 return build_x_unary_op (ADDR_EXPR, op1, complain);
11956 case PLUS_EXPR:
11957 case MINUS_EXPR:
11958 case MULT_EXPR:
11959 case TRUNC_DIV_EXPR:
11960 case CEIL_DIV_EXPR:
11961 case FLOOR_DIV_EXPR:
11962 case ROUND_DIV_EXPR:
11963 case EXACT_DIV_EXPR:
11964 case BIT_AND_EXPR:
11965 case BIT_IOR_EXPR:
11966 case BIT_XOR_EXPR:
11967 case TRUNC_MOD_EXPR:
11968 case FLOOR_MOD_EXPR:
11969 case TRUTH_ANDIF_EXPR:
11970 case TRUTH_ORIF_EXPR:
11971 case TRUTH_AND_EXPR:
11972 case TRUTH_OR_EXPR:
11973 case RSHIFT_EXPR:
11974 case LSHIFT_EXPR:
11975 case RROTATE_EXPR:
11976 case LROTATE_EXPR:
11977 case EQ_EXPR:
11978 case NE_EXPR:
11979 case MAX_EXPR:
11980 case MIN_EXPR:
11981 case LE_EXPR:
11982 case GE_EXPR:
11983 case LT_EXPR:
11984 case GT_EXPR:
11985 case MEMBER_REF:
11986 case DOTSTAR_EXPR:
11987 return build_x_binary_op
11988 (TREE_CODE (t),
11989 RECUR (TREE_OPERAND (t, 0)),
11990 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11991 ? ERROR_MARK
11992 : TREE_CODE (TREE_OPERAND (t, 0))),
11993 RECUR (TREE_OPERAND (t, 1)),
11994 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11995 ? ERROR_MARK
11996 : TREE_CODE (TREE_OPERAND (t, 1))),
11997 /*overloaded_p=*/NULL,
11998 complain);
12000 case SCOPE_REF:
12001 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12002 /*address_p=*/false);
12003 case ARRAY_REF:
12004 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12005 args, complain, in_decl);
12006 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12008 case SIZEOF_EXPR:
12009 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12010 return tsubst_copy (t, args, complain, in_decl);
12011 /* Fall through */
12013 case ALIGNOF_EXPR:
12014 op1 = TREE_OPERAND (t, 0);
12015 if (!args)
12017 /* When there are no ARGS, we are trying to evaluate a
12018 non-dependent expression from the parser. Trying to do
12019 the substitutions may not work. */
12020 if (!TYPE_P (op1))
12021 op1 = TREE_TYPE (op1);
12023 else
12025 ++cp_unevaluated_operand;
12026 ++c_inhibit_evaluation_warnings;
12027 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12028 /*function_p=*/false,
12029 /*integral_constant_expression_p=*/false);
12030 --cp_unevaluated_operand;
12031 --c_inhibit_evaluation_warnings;
12033 if (TYPE_P (op1))
12034 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12035 complain & tf_error);
12036 else
12037 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12038 complain & tf_error);
12040 case MODOP_EXPR:
12042 tree r = build_x_modify_expr
12043 (RECUR (TREE_OPERAND (t, 0)),
12044 TREE_CODE (TREE_OPERAND (t, 1)),
12045 RECUR (TREE_OPERAND (t, 2)),
12046 complain);
12047 /* TREE_NO_WARNING must be set if either the expression was
12048 parenthesized or it uses an operator such as >>= rather
12049 than plain assignment. In the former case, it was already
12050 set and must be copied. In the latter case,
12051 build_x_modify_expr sets it and it must not be reset
12052 here. */
12053 if (TREE_NO_WARNING (t))
12054 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12055 return r;
12058 case ARROW_EXPR:
12059 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12060 args, complain, in_decl);
12061 /* Remember that there was a reference to this entity. */
12062 if (DECL_P (op1))
12063 mark_used (op1);
12064 return build_x_arrow (op1);
12066 case NEW_EXPR:
12068 tree placement = RECUR (TREE_OPERAND (t, 0));
12069 tree init = RECUR (TREE_OPERAND (t, 3));
12070 VEC(tree,gc) *placement_vec;
12071 VEC(tree,gc) *init_vec;
12072 tree ret;
12074 if (placement == NULL_TREE)
12075 placement_vec = NULL;
12076 else
12078 placement_vec = make_tree_vector ();
12079 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12080 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12083 /* If there was an initializer in the original tree, but it
12084 instantiated to an empty list, then we should pass a
12085 non-NULL empty vector to tell build_new that it was an
12086 empty initializer() rather than no initializer. This can
12087 only happen when the initializer is a pack expansion whose
12088 parameter packs are of length zero. */
12089 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12090 init_vec = NULL;
12091 else
12093 init_vec = make_tree_vector ();
12094 if (init == void_zero_node)
12095 gcc_assert (init_vec != NULL);
12096 else
12098 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12099 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12103 ret = build_new (&placement_vec,
12104 RECUR (TREE_OPERAND (t, 1)),
12105 RECUR (TREE_OPERAND (t, 2)),
12106 &init_vec,
12107 NEW_EXPR_USE_GLOBAL (t),
12108 complain);
12110 if (placement_vec != NULL)
12111 release_tree_vector (placement_vec);
12112 if (init_vec != NULL)
12113 release_tree_vector (init_vec);
12115 return ret;
12118 case DELETE_EXPR:
12119 return delete_sanity
12120 (RECUR (TREE_OPERAND (t, 0)),
12121 RECUR (TREE_OPERAND (t, 1)),
12122 DELETE_EXPR_USE_VEC (t),
12123 DELETE_EXPR_USE_GLOBAL (t));
12125 case COMPOUND_EXPR:
12126 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12127 RECUR (TREE_OPERAND (t, 1)),
12128 complain);
12130 case CALL_EXPR:
12132 tree function;
12133 VEC(tree,gc) *call_args;
12134 unsigned int nargs, i;
12135 bool qualified_p;
12136 bool koenig_p;
12137 tree ret;
12139 function = CALL_EXPR_FN (t);
12140 /* When we parsed the expression, we determined whether or
12141 not Koenig lookup should be performed. */
12142 koenig_p = KOENIG_LOOKUP_P (t);
12143 if (TREE_CODE (function) == SCOPE_REF)
12145 qualified_p = true;
12146 function = tsubst_qualified_id (function, args, complain, in_decl,
12147 /*done=*/false,
12148 /*address_p=*/false);
12150 else
12152 if (TREE_CODE (function) == COMPONENT_REF)
12154 tree op = TREE_OPERAND (function, 1);
12156 qualified_p = (TREE_CODE (op) == SCOPE_REF
12157 || (BASELINK_P (op)
12158 && BASELINK_QUALIFIED_P (op)));
12160 else
12161 qualified_p = false;
12163 function = tsubst_copy_and_build (function, args, complain,
12164 in_decl,
12165 !qualified_p,
12166 integral_constant_expression_p);
12168 if (BASELINK_P (function))
12169 qualified_p = true;
12172 nargs = call_expr_nargs (t);
12173 call_args = make_tree_vector ();
12174 for (i = 0; i < nargs; ++i)
12176 tree arg = CALL_EXPR_ARG (t, i);
12178 if (!PACK_EXPANSION_P (arg))
12179 VEC_safe_push (tree, gc, call_args,
12180 RECUR (CALL_EXPR_ARG (t, i)));
12181 else
12183 /* Expand the pack expansion and push each entry onto
12184 CALL_ARGS. */
12185 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12186 if (TREE_CODE (arg) == TREE_VEC)
12188 unsigned int len, j;
12190 len = TREE_VEC_LENGTH (arg);
12191 for (j = 0; j < len; ++j)
12193 tree value = TREE_VEC_ELT (arg, j);
12194 if (value != NULL_TREE)
12195 value = convert_from_reference (value);
12196 VEC_safe_push (tree, gc, call_args, value);
12199 else
12201 /* A partial substitution. Add one entry. */
12202 VEC_safe_push (tree, gc, call_args, arg);
12207 /* We do not perform argument-dependent lookup if normal
12208 lookup finds a non-function, in accordance with the
12209 expected resolution of DR 218. */
12210 if (koenig_p
12211 && ((is_overloaded_fn (function)
12212 /* If lookup found a member function, the Koenig lookup is
12213 not appropriate, even if an unqualified-name was used
12214 to denote the function. */
12215 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12216 || TREE_CODE (function) == IDENTIFIER_NODE)
12217 /* Only do this when substitution turns a dependent call
12218 into a non-dependent call. */
12219 && type_dependent_expression_p_push (t)
12220 && !any_type_dependent_arguments_p (call_args))
12221 function = perform_koenig_lookup (function, call_args);
12223 if (TREE_CODE (function) == IDENTIFIER_NODE)
12225 unqualified_name_lookup_error (function);
12226 release_tree_vector (call_args);
12227 return error_mark_node;
12230 /* Remember that there was a reference to this entity. */
12231 if (DECL_P (function))
12232 mark_used (function);
12234 if (TREE_CODE (function) == OFFSET_REF)
12235 ret = build_offset_ref_call_from_tree (function, &call_args);
12236 else if (TREE_CODE (function) == COMPONENT_REF)
12238 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12239 ret = finish_call_expr (function, &call_args,
12240 /*disallow_virtual=*/false,
12241 /*koenig_p=*/false,
12242 complain);
12243 else
12244 ret = (build_new_method_call
12245 (TREE_OPERAND (function, 0),
12246 TREE_OPERAND (function, 1),
12247 &call_args, NULL_TREE,
12248 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12249 /*fn_p=*/NULL,
12250 complain));
12252 else
12253 ret = finish_call_expr (function, &call_args,
12254 /*disallow_virtual=*/qualified_p,
12255 koenig_p,
12256 complain);
12258 release_tree_vector (call_args);
12260 return ret;
12263 case COND_EXPR:
12264 return build_x_conditional_expr
12265 (RECUR (TREE_OPERAND (t, 0)),
12266 RECUR (TREE_OPERAND (t, 1)),
12267 RECUR (TREE_OPERAND (t, 2)),
12268 complain);
12270 case PSEUDO_DTOR_EXPR:
12271 return finish_pseudo_destructor_expr
12272 (RECUR (TREE_OPERAND (t, 0)),
12273 RECUR (TREE_OPERAND (t, 1)),
12274 RECUR (TREE_OPERAND (t, 2)));
12276 case TREE_LIST:
12278 tree purpose, value, chain;
12280 if (t == void_list_node)
12281 return t;
12283 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12284 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12286 /* We have pack expansions, so expand those and
12287 create a new list out of it. */
12288 tree purposevec = NULL_TREE;
12289 tree valuevec = NULL_TREE;
12290 tree chain;
12291 int i, len = -1;
12293 /* Expand the argument expressions. */
12294 if (TREE_PURPOSE (t))
12295 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12296 complain, in_decl);
12297 if (TREE_VALUE (t))
12298 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12299 complain, in_decl);
12301 /* Build the rest of the list. */
12302 chain = TREE_CHAIN (t);
12303 if (chain && chain != void_type_node)
12304 chain = RECUR (chain);
12306 /* Determine the number of arguments. */
12307 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12309 len = TREE_VEC_LENGTH (purposevec);
12310 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12312 else if (TREE_CODE (valuevec) == TREE_VEC)
12313 len = TREE_VEC_LENGTH (valuevec);
12314 else
12316 /* Since we only performed a partial substitution into
12317 the argument pack, we only return a single list
12318 node. */
12319 if (purposevec == TREE_PURPOSE (t)
12320 && valuevec == TREE_VALUE (t)
12321 && chain == TREE_CHAIN (t))
12322 return t;
12324 return tree_cons (purposevec, valuevec, chain);
12327 /* Convert the argument vectors into a TREE_LIST */
12328 i = len;
12329 while (i > 0)
12331 /* Grab the Ith values. */
12332 i--;
12333 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12334 : NULL_TREE;
12335 value
12336 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12337 : NULL_TREE;
12339 /* Build the list (backwards). */
12340 chain = tree_cons (purpose, value, chain);
12343 return chain;
12346 purpose = TREE_PURPOSE (t);
12347 if (purpose)
12348 purpose = RECUR (purpose);
12349 value = TREE_VALUE (t);
12350 if (value)
12351 value = RECUR (value);
12352 chain = TREE_CHAIN (t);
12353 if (chain && chain != void_type_node)
12354 chain = RECUR (chain);
12355 if (purpose == TREE_PURPOSE (t)
12356 && value == TREE_VALUE (t)
12357 && chain == TREE_CHAIN (t))
12358 return t;
12359 return tree_cons (purpose, value, chain);
12362 case COMPONENT_REF:
12364 tree object;
12365 tree object_type;
12366 tree member;
12368 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12369 args, complain, in_decl);
12370 /* Remember that there was a reference to this entity. */
12371 if (DECL_P (object))
12372 mark_used (object);
12373 object_type = TREE_TYPE (object);
12375 member = TREE_OPERAND (t, 1);
12376 if (BASELINK_P (member))
12377 member = tsubst_baselink (member,
12378 non_reference (TREE_TYPE (object)),
12379 args, complain, in_decl);
12380 else
12381 member = tsubst_copy (member, args, complain, in_decl);
12382 if (member == error_mark_node)
12383 return error_mark_node;
12385 if (object_type && !CLASS_TYPE_P (object_type))
12387 if (SCALAR_TYPE_P (object_type))
12389 tree s = NULL_TREE;
12390 tree dtor = member;
12392 if (TREE_CODE (dtor) == SCOPE_REF)
12394 s = TREE_OPERAND (dtor, 0);
12395 dtor = TREE_OPERAND (dtor, 1);
12397 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12399 dtor = TREE_OPERAND (dtor, 0);
12400 if (TYPE_P (dtor))
12401 return finish_pseudo_destructor_expr (object, s, dtor);
12405 else if (TREE_CODE (member) == SCOPE_REF
12406 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12408 tree tmpl;
12409 tree args;
12411 /* Lookup the template functions now that we know what the
12412 scope is. */
12413 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12414 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12415 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12416 /*is_type_p=*/false,
12417 /*complain=*/false);
12418 if (BASELINK_P (member))
12420 BASELINK_FUNCTIONS (member)
12421 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12422 args);
12423 member = (adjust_result_of_qualified_name_lookup
12424 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12425 object_type));
12427 else
12429 qualified_name_lookup_error (object_type, tmpl, member,
12430 input_location);
12431 return error_mark_node;
12434 else if (TREE_CODE (member) == SCOPE_REF
12435 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12436 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12438 if (complain & tf_error)
12440 if (TYPE_P (TREE_OPERAND (member, 0)))
12441 error ("%qT is not a class or namespace",
12442 TREE_OPERAND (member, 0));
12443 else
12444 error ("%qD is not a class or namespace",
12445 TREE_OPERAND (member, 0));
12447 return error_mark_node;
12449 else if (TREE_CODE (member) == FIELD_DECL)
12450 return finish_non_static_data_member (member, object, NULL_TREE);
12452 return finish_class_member_access_expr (object, member,
12453 /*template_p=*/false,
12454 complain);
12457 case THROW_EXPR:
12458 return build_throw
12459 (RECUR (TREE_OPERAND (t, 0)));
12461 case CONSTRUCTOR:
12463 VEC(constructor_elt,gc) *n;
12464 constructor_elt *ce;
12465 unsigned HOST_WIDE_INT idx;
12466 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12467 bool process_index_p;
12468 int newlen;
12469 bool need_copy_p = false;
12470 tree r;
12472 if (type == error_mark_node)
12473 return error_mark_node;
12475 /* digest_init will do the wrong thing if we let it. */
12476 if (type && TYPE_PTRMEMFUNC_P (type))
12477 return t;
12479 /* We do not want to process the index of aggregate
12480 initializers as they are identifier nodes which will be
12481 looked up by digest_init. */
12482 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12484 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12485 newlen = VEC_length (constructor_elt, n);
12486 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12488 if (ce->index && process_index_p)
12489 ce->index = RECUR (ce->index);
12491 if (PACK_EXPANSION_P (ce->value))
12493 /* Substitute into the pack expansion. */
12494 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12495 in_decl);
12497 if (ce->value == error_mark_node)
12499 else if (TREE_VEC_LENGTH (ce->value) == 1)
12500 /* Just move the argument into place. */
12501 ce->value = TREE_VEC_ELT (ce->value, 0);
12502 else
12504 /* Update the length of the final CONSTRUCTOR
12505 arguments vector, and note that we will need to
12506 copy.*/
12507 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12508 need_copy_p = true;
12511 else
12512 ce->value = RECUR (ce->value);
12515 if (need_copy_p)
12517 VEC(constructor_elt,gc) *old_n = n;
12519 n = VEC_alloc (constructor_elt, gc, newlen);
12520 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12521 idx++)
12523 if (TREE_CODE (ce->value) == TREE_VEC)
12525 int i, len = TREE_VEC_LENGTH (ce->value);
12526 for (i = 0; i < len; ++i)
12527 CONSTRUCTOR_APPEND_ELT (n, 0,
12528 TREE_VEC_ELT (ce->value, i));
12530 else
12531 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12535 r = build_constructor (init_list_type_node, n);
12536 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12538 if (TREE_HAS_CONSTRUCTOR (t))
12539 return finish_compound_literal (type, r);
12541 return r;
12544 case TYPEID_EXPR:
12546 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12547 if (TYPE_P (operand_0))
12548 return get_typeid (operand_0);
12549 return build_typeid (operand_0);
12552 case VAR_DECL:
12553 if (!args)
12554 return t;
12555 /* Fall through */
12557 case PARM_DECL:
12559 tree r = tsubst_copy (t, args, complain, in_decl);
12561 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12562 /* If the original type was a reference, we'll be wrapped in
12563 the appropriate INDIRECT_REF. */
12564 r = convert_from_reference (r);
12565 return r;
12568 case VA_ARG_EXPR:
12569 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12570 tsubst_copy (TREE_TYPE (t), args, complain,
12571 in_decl));
12573 case OFFSETOF_EXPR:
12574 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12576 case TRAIT_EXPR:
12578 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12579 complain, in_decl);
12581 tree type2 = TRAIT_EXPR_TYPE2 (t);
12582 if (type2)
12583 type2 = tsubst_copy (type2, args, complain, in_decl);
12585 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12588 case STMT_EXPR:
12590 tree old_stmt_expr = cur_stmt_expr;
12591 tree stmt_expr = begin_stmt_expr ();
12593 cur_stmt_expr = stmt_expr;
12594 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12595 integral_constant_expression_p);
12596 stmt_expr = finish_stmt_expr (stmt_expr, false);
12597 cur_stmt_expr = old_stmt_expr;
12599 /* If the resulting list of expression statement is empty,
12600 fold it further into void_zero_node. */
12601 if (empty_expr_stmt_p (stmt_expr))
12602 stmt_expr = void_zero_node;
12604 return stmt_expr;
12607 case CONST_DECL:
12608 t = tsubst_copy (t, args, complain, in_decl);
12609 /* As in finish_id_expression, we resolve enumeration constants
12610 to their underlying values. */
12611 if (TREE_CODE (t) == CONST_DECL)
12613 used_types_insert (TREE_TYPE (t));
12614 return DECL_INITIAL (t);
12616 return t;
12618 case LAMBDA_EXPR:
12620 tree r = build_lambda_expr ();
12622 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12623 TREE_TYPE (r) = type;
12624 CLASSTYPE_LAMBDA_EXPR (type) = r;
12626 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12627 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12628 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12629 LAMBDA_EXPR_DISCRIMINATOR (r)
12630 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12631 LAMBDA_EXPR_CAPTURE_LIST (r)
12632 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12633 LAMBDA_EXPR_THIS_CAPTURE (r)
12634 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12635 LAMBDA_EXPR_EXTRA_SCOPE (r)
12636 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12638 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12639 determine_visibility (TYPE_NAME (type));
12640 /* Now that we know visibility, instantiate the type so we have a
12641 declaration of the op() for later calls to lambda_function. */
12642 complete_type (type);
12644 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12645 if (type)
12646 apply_lambda_return_type (r, type);
12648 return build_lambda_object (r);
12651 default:
12652 /* Handle Objective-C++ constructs, if appropriate. */
12654 tree subst
12655 = objcp_tsubst_copy_and_build (t, args, complain,
12656 in_decl, /*function_p=*/false);
12657 if (subst)
12658 return subst;
12660 return tsubst_copy (t, args, complain, in_decl);
12663 #undef RECUR
12666 /* Verify that the instantiated ARGS are valid. For type arguments,
12667 make sure that the type's linkage is ok. For non-type arguments,
12668 make sure they are constants if they are integral or enumerations.
12669 Emit an error under control of COMPLAIN, and return TRUE on error. */
12671 static bool
12672 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12674 if (ARGUMENT_PACK_P (t))
12676 tree vec = ARGUMENT_PACK_ARGS (t);
12677 int len = TREE_VEC_LENGTH (vec);
12678 bool result = false;
12679 int i;
12681 for (i = 0; i < len; ++i)
12682 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12683 result = true;
12684 return result;
12686 else if (TYPE_P (t))
12688 /* [basic.link]: A name with no linkage (notably, the name
12689 of a class or enumeration declared in a local scope)
12690 shall not be used to declare an entity with linkage.
12691 This implies that names with no linkage cannot be used as
12692 template arguments
12694 DR 757 relaxes this restriction for C++0x. */
12695 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
12696 : no_linkage_check (t, /*relaxed_p=*/false));
12698 if (nt)
12700 /* DR 488 makes use of a type with no linkage cause
12701 type deduction to fail. */
12702 if (complain & tf_error)
12704 if (TYPE_ANONYMOUS_P (nt))
12705 error ("%qT is/uses anonymous type", t);
12706 else
12707 error ("template argument for %qD uses local type %qT",
12708 tmpl, t);
12710 return true;
12712 /* In order to avoid all sorts of complications, we do not
12713 allow variably-modified types as template arguments. */
12714 else if (variably_modified_type_p (t, NULL_TREE))
12716 if (complain & tf_error)
12717 error ("%qT is a variably modified type", t);
12718 return true;
12721 /* A non-type argument of integral or enumerated type must be a
12722 constant. */
12723 else if (TREE_TYPE (t)
12724 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12725 && !TREE_CONSTANT (t))
12727 if (complain & tf_error)
12728 error ("integral expression %qE is not constant", t);
12729 return true;
12731 return false;
12734 static bool
12735 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12737 int ix, len = DECL_NTPARMS (tmpl);
12738 bool result = false;
12740 for (ix = 0; ix != len; ix++)
12742 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12743 result = true;
12745 if (result && (complain & tf_error))
12746 error (" trying to instantiate %qD", tmpl);
12747 return result;
12750 /* Instantiate the indicated variable or function template TMPL with
12751 the template arguments in TARG_PTR. */
12753 tree
12754 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12756 tree targ_ptr = orig_args;
12757 tree fndecl;
12758 tree gen_tmpl;
12759 tree spec;
12760 HOST_WIDE_INT saved_processing_template_decl;
12762 if (tmpl == error_mark_node)
12763 return error_mark_node;
12765 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12767 /* If this function is a clone, handle it specially. */
12768 if (DECL_CLONED_FUNCTION_P (tmpl))
12770 tree spec;
12771 tree clone;
12773 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12774 DECL_CLONED_FUNCTION. */
12775 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12776 targ_ptr, complain);
12777 if (spec == error_mark_node)
12778 return error_mark_node;
12780 /* Look for the clone. */
12781 FOR_EACH_CLONE (clone, spec)
12782 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12783 return clone;
12784 /* We should always have found the clone by now. */
12785 gcc_unreachable ();
12786 return NULL_TREE;
12789 /* Check to see if we already have this specialization. */
12790 gen_tmpl = most_general_template (tmpl);
12791 if (tmpl != gen_tmpl)
12792 /* The TMPL is a partial instantiation. To get a full set of
12793 arguments we must add the arguments used to perform the
12794 partial instantiation. */
12795 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12796 targ_ptr);
12798 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12799 but it doesn't seem to be on the hot path. */
12800 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12802 gcc_assert (tmpl == gen_tmpl
12803 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12804 == spec)
12805 || fndecl == NULL_TREE);
12807 if (spec != NULL_TREE)
12808 return spec;
12810 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12811 complain))
12812 return error_mark_node;
12814 /* We are building a FUNCTION_DECL, during which the access of its
12815 parameters and return types have to be checked. However this
12816 FUNCTION_DECL which is the desired context for access checking
12817 is not built yet. We solve this chicken-and-egg problem by
12818 deferring all checks until we have the FUNCTION_DECL. */
12819 push_deferring_access_checks (dk_deferred);
12821 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12822 (because, for example, we have encountered a non-dependent
12823 function call in the body of a template function and must now
12824 determine which of several overloaded functions will be called),
12825 within the instantiation itself we are not processing a
12826 template. */
12827 saved_processing_template_decl = processing_template_decl;
12828 processing_template_decl = 0;
12829 /* Substitute template parameters to obtain the specialization. */
12830 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12831 targ_ptr, complain, gen_tmpl);
12832 processing_template_decl = saved_processing_template_decl;
12833 if (fndecl == error_mark_node)
12834 return error_mark_node;
12836 /* Now we know the specialization, compute access previously
12837 deferred. */
12838 push_access_scope (fndecl);
12840 /* Some typedefs referenced from within the template code need to be access
12841 checked at template instantiation time, i.e now. These types were
12842 added to the template at parsing time. Let's get those and perfom
12843 the acces checks then. */
12844 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
12845 perform_deferred_access_checks ();
12846 pop_access_scope (fndecl);
12847 pop_deferring_access_checks ();
12849 /* The DECL_TI_TEMPLATE should always be the immediate parent
12850 template, not the most general template. */
12851 DECL_TI_TEMPLATE (fndecl) = tmpl;
12853 /* If we've just instantiated the main entry point for a function,
12854 instantiate all the alternate entry points as well. We do this
12855 by cloning the instantiation of the main entry point, not by
12856 instantiating the template clones. */
12857 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12858 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12860 return fndecl;
12863 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12864 NARGS elements of the arguments that are being used when calling
12865 it. TARGS is a vector into which the deduced template arguments
12866 are placed.
12868 Return zero for success, 2 for an incomplete match that doesn't resolve
12869 all the types, and 1 for complete failure. An error message will be
12870 printed only for an incomplete match.
12872 If FN is a conversion operator, or we are trying to produce a specific
12873 specialization, RETURN_TYPE is the return type desired.
12875 The EXPLICIT_TARGS are explicit template arguments provided via a
12876 template-id.
12878 The parameter STRICT is one of:
12880 DEDUCE_CALL:
12881 We are deducing arguments for a function call, as in
12882 [temp.deduct.call].
12884 DEDUCE_CONV:
12885 We are deducing arguments for a conversion function, as in
12886 [temp.deduct.conv].
12888 DEDUCE_EXACT:
12889 We are deducing arguments when doing an explicit instantiation
12890 as in [temp.explicit], when determining an explicit specialization
12891 as in [temp.expl.spec], or when taking the address of a function
12892 template, as in [temp.deduct.funcaddr]. */
12895 fn_type_unification (tree fn,
12896 tree explicit_targs,
12897 tree targs,
12898 const tree *args,
12899 unsigned int nargs,
12900 tree return_type,
12901 unification_kind_t strict,
12902 int flags)
12904 tree parms;
12905 tree fntype;
12906 int result;
12907 bool incomplete_argument_packs_p = false;
12909 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12911 fntype = TREE_TYPE (fn);
12912 if (explicit_targs)
12914 /* [temp.deduct]
12916 The specified template arguments must match the template
12917 parameters in kind (i.e., type, nontype, template), and there
12918 must not be more arguments than there are parameters;
12919 otherwise type deduction fails.
12921 Nontype arguments must match the types of the corresponding
12922 nontype template parameters, or must be convertible to the
12923 types of the corresponding nontype parameters as specified in
12924 _temp.arg.nontype_, otherwise type deduction fails.
12926 All references in the function type of the function template
12927 to the corresponding template parameters are replaced by the
12928 specified template argument values. If a substitution in a
12929 template parameter or in the function type of the function
12930 template results in an invalid type, type deduction fails. */
12931 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12932 int i, len = TREE_VEC_LENGTH (tparms);
12933 tree converted_args;
12934 bool incomplete = false;
12936 if (explicit_targs == error_mark_node)
12937 return 1;
12939 converted_args
12940 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12941 /*require_all_args=*/false,
12942 /*use_default_args=*/false));
12943 if (converted_args == error_mark_node)
12944 return 1;
12946 /* Substitute the explicit args into the function type. This is
12947 necessary so that, for instance, explicitly declared function
12948 arguments can match null pointed constants. If we were given
12949 an incomplete set of explicit args, we must not do semantic
12950 processing during substitution as we could create partial
12951 instantiations. */
12952 for (i = 0; i < len; i++)
12954 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12955 bool parameter_pack = false;
12957 /* Dig out the actual parm. */
12958 if (TREE_CODE (parm) == TYPE_DECL
12959 || TREE_CODE (parm) == TEMPLATE_DECL)
12961 parm = TREE_TYPE (parm);
12962 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12964 else if (TREE_CODE (parm) == PARM_DECL)
12966 parm = DECL_INITIAL (parm);
12967 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12970 if (parameter_pack)
12972 int level, idx;
12973 tree targ;
12974 template_parm_level_and_index (parm, &level, &idx);
12976 /* Mark the argument pack as "incomplete". We could
12977 still deduce more arguments during unification. */
12978 targ = TMPL_ARG (converted_args, level, idx);
12979 if (targ)
12981 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12982 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12983 = ARGUMENT_PACK_ARGS (targ);
12986 /* We have some incomplete argument packs. */
12987 incomplete_argument_packs_p = true;
12991 if (incomplete_argument_packs_p)
12992 /* Any substitution is guaranteed to be incomplete if there
12993 are incomplete argument packs, because we can still deduce
12994 more arguments. */
12995 incomplete = 1;
12996 else
12997 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12999 processing_template_decl += incomplete;
13000 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13001 processing_template_decl -= incomplete;
13003 if (fntype == error_mark_node)
13004 return 1;
13006 /* Place the explicitly specified arguments in TARGS. */
13007 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13008 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13011 /* Never do unification on the 'this' parameter. */
13012 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13014 if (return_type)
13016 tree *new_args;
13018 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13019 new_args = XALLOCAVEC (tree, nargs + 1);
13020 new_args[0] = return_type;
13021 memcpy (new_args + 1, args, nargs * sizeof (tree));
13022 args = new_args;
13023 ++nargs;
13026 /* We allow incomplete unification without an error message here
13027 because the standard doesn't seem to explicitly prohibit it. Our
13028 callers must be ready to deal with unification failures in any
13029 event. */
13030 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13031 targs, parms, args, nargs, /*subr=*/0,
13032 strict, flags);
13034 if (result == 0 && incomplete_argument_packs_p)
13036 int i, len = NUM_TMPL_ARGS (targs);
13038 /* Clear the "incomplete" flags on all argument packs. */
13039 for (i = 0; i < len; i++)
13041 tree arg = TREE_VEC_ELT (targs, i);
13042 if (ARGUMENT_PACK_P (arg))
13044 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13045 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13050 /* Now that we have bindings for all of the template arguments,
13051 ensure that the arguments deduced for the template template
13052 parameters have compatible template parameter lists. We cannot
13053 check this property before we have deduced all template
13054 arguments, because the template parameter types of a template
13055 template parameter might depend on prior template parameters
13056 deduced after the template template parameter. The following
13057 ill-formed example illustrates this issue:
13059 template<typename T, template<T> class C> void f(C<5>, T);
13061 template<int N> struct X {};
13063 void g() {
13064 f(X<5>(), 5l); // error: template argument deduction fails
13067 The template parameter list of 'C' depends on the template type
13068 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13069 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13070 time that we deduce 'C'. */
13071 if (result == 0
13072 && !template_template_parm_bindings_ok_p
13073 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13074 return 1;
13076 if (result == 0)
13077 /* All is well so far. Now, check:
13079 [temp.deduct]
13081 When all template arguments have been deduced, all uses of
13082 template parameters in nondeduced contexts are replaced with
13083 the corresponding deduced argument values. If the
13084 substitution results in an invalid type, as described above,
13085 type deduction fails. */
13087 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13088 if (substed == error_mark_node)
13089 return 1;
13091 /* If we're looking for an exact match, check that what we got
13092 is indeed an exact match. It might not be if some template
13093 parameters are used in non-deduced contexts. */
13094 if (strict == DEDUCE_EXACT)
13096 unsigned int i;
13098 tree sarg
13099 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13100 if (return_type)
13101 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13102 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13103 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13104 return 1;
13108 return result;
13111 /* Adjust types before performing type deduction, as described in
13112 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13113 sections are symmetric. PARM is the type of a function parameter
13114 or the return type of the conversion function. ARG is the type of
13115 the argument passed to the call, or the type of the value
13116 initialized with the result of the conversion function.
13117 ARG_EXPR is the original argument expression, which may be null. */
13119 static int
13120 maybe_adjust_types_for_deduction (unification_kind_t strict,
13121 tree* parm,
13122 tree* arg,
13123 tree arg_expr)
13125 int result = 0;
13127 switch (strict)
13129 case DEDUCE_CALL:
13130 break;
13132 case DEDUCE_CONV:
13134 /* Swap PARM and ARG throughout the remainder of this
13135 function; the handling is precisely symmetric since PARM
13136 will initialize ARG rather than vice versa. */
13137 tree* temp = parm;
13138 parm = arg;
13139 arg = temp;
13140 break;
13143 case DEDUCE_EXACT:
13144 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13145 too, but here handle it by stripping the reference from PARM
13146 rather than by adding it to ARG. */
13147 if (TREE_CODE (*parm) == REFERENCE_TYPE
13148 && TYPE_REF_IS_RVALUE (*parm)
13149 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13150 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13151 && TREE_CODE (*arg) == REFERENCE_TYPE
13152 && !TYPE_REF_IS_RVALUE (*arg))
13153 *parm = TREE_TYPE (*parm);
13154 /* Nothing else to do in this case. */
13155 return 0;
13157 default:
13158 gcc_unreachable ();
13161 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13163 /* [temp.deduct.call]
13165 If P is not a reference type:
13167 --If A is an array type, the pointer type produced by the
13168 array-to-pointer standard conversion (_conv.array_) is
13169 used in place of A for type deduction; otherwise,
13171 --If A is a function type, the pointer type produced by
13172 the function-to-pointer standard conversion
13173 (_conv.func_) is used in place of A for type deduction;
13174 otherwise,
13176 --If A is a cv-qualified type, the top level
13177 cv-qualifiers of A's type are ignored for type
13178 deduction. */
13179 if (TREE_CODE (*arg) == ARRAY_TYPE)
13180 *arg = build_pointer_type (TREE_TYPE (*arg));
13181 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13182 *arg = build_pointer_type (*arg);
13183 else
13184 *arg = TYPE_MAIN_VARIANT (*arg);
13187 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13188 of the form T&&, where T is a template parameter, and the argument
13189 is an lvalue, T is deduced as A& */
13190 if (TREE_CODE (*parm) == REFERENCE_TYPE
13191 && TYPE_REF_IS_RVALUE (*parm)
13192 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13193 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13194 && arg_expr && real_lvalue_p (arg_expr))
13195 *arg = build_reference_type (*arg);
13197 /* [temp.deduct.call]
13199 If P is a cv-qualified type, the top level cv-qualifiers
13200 of P's type are ignored for type deduction. If P is a
13201 reference type, the type referred to by P is used for
13202 type deduction. */
13203 *parm = TYPE_MAIN_VARIANT (*parm);
13204 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13206 *parm = TREE_TYPE (*parm);
13207 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13210 /* DR 322. For conversion deduction, remove a reference type on parm
13211 too (which has been swapped into ARG). */
13212 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13213 *arg = TREE_TYPE (*arg);
13215 return result;
13218 /* Most parms like fn_type_unification.
13220 If SUBR is 1, we're being called recursively (to unify the
13221 arguments of a function or method parameter of a function
13222 template). */
13224 static int
13225 type_unification_real (tree tparms,
13226 tree targs,
13227 tree xparms,
13228 const tree *xargs,
13229 unsigned int xnargs,
13230 int subr,
13231 unification_kind_t strict,
13232 int flags)
13234 tree parm, arg, arg_expr;
13235 int i;
13236 int ntparms = TREE_VEC_LENGTH (tparms);
13237 int sub_strict;
13238 int saw_undeduced = 0;
13239 tree parms;
13240 const tree *args;
13241 unsigned int nargs;
13242 unsigned int ia;
13244 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13245 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13246 gcc_assert (ntparms > 0);
13248 switch (strict)
13250 case DEDUCE_CALL:
13251 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13252 | UNIFY_ALLOW_DERIVED);
13253 break;
13255 case DEDUCE_CONV:
13256 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13257 break;
13259 case DEDUCE_EXACT:
13260 sub_strict = UNIFY_ALLOW_NONE;
13261 break;
13263 default:
13264 gcc_unreachable ();
13267 again:
13268 parms = xparms;
13269 args = xargs;
13270 nargs = xnargs;
13272 ia = 0;
13273 while (parms && parms != void_list_node
13274 && ia < nargs)
13276 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13277 break;
13279 parm = TREE_VALUE (parms);
13280 parms = TREE_CHAIN (parms);
13281 arg = args[ia];
13282 ++ia;
13283 arg_expr = NULL;
13285 if (arg == error_mark_node)
13286 return 1;
13287 if (arg == unknown_type_node)
13288 /* We can't deduce anything from this, but we might get all the
13289 template args from other function args. */
13290 continue;
13292 /* Conversions will be performed on a function argument that
13293 corresponds with a function parameter that contains only
13294 non-deducible template parameters and explicitly specified
13295 template parameters. */
13296 if (!uses_template_parms (parm))
13298 tree type;
13300 if (!TYPE_P (arg))
13301 type = TREE_TYPE (arg);
13302 else
13303 type = arg;
13305 if (same_type_p (parm, type))
13306 continue;
13307 if (strict != DEDUCE_EXACT
13308 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13309 flags))
13310 continue;
13312 return 1;
13315 if (!TYPE_P (arg))
13317 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13318 if (type_unknown_p (arg))
13320 /* [temp.deduct.type]
13322 A template-argument can be deduced from a pointer to
13323 function or pointer to member function argument if
13324 the set of overloaded functions does not contain
13325 function templates and at most one of a set of
13326 overloaded functions provides a unique match. */
13327 if (resolve_overloaded_unification
13328 (tparms, targs, parm, arg, strict, sub_strict))
13329 continue;
13331 return 1;
13333 arg_expr = arg;
13334 arg = unlowered_expr_type (arg);
13335 if (arg == error_mark_node)
13336 return 1;
13340 int arg_strict = sub_strict;
13342 if (!subr)
13343 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13344 arg_expr);
13346 if (arg == init_list_type_node && arg_expr)
13347 arg = arg_expr;
13348 if (unify (tparms, targs, parm, arg, arg_strict))
13349 return 1;
13354 if (parms
13355 && parms != void_list_node
13356 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13358 /* Unify the remaining arguments with the pack expansion type. */
13359 tree argvec;
13360 tree parmvec = make_tree_vec (1);
13362 /* Allocate a TREE_VEC and copy in all of the arguments */
13363 argvec = make_tree_vec (nargs - ia);
13364 for (i = 0; ia < nargs; ++ia, ++i)
13365 TREE_VEC_ELT (argvec, i) = args[ia];
13367 /* Copy the parameter into parmvec. */
13368 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13369 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13370 /*call_args_p=*/true, /*subr=*/subr))
13371 return 1;
13373 /* Advance to the end of the list of parameters. */
13374 parms = TREE_CHAIN (parms);
13377 /* Fail if we've reached the end of the parm list, and more args
13378 are present, and the parm list isn't variadic. */
13379 if (ia < nargs && parms == void_list_node)
13380 return 1;
13381 /* Fail if parms are left and they don't have default values. */
13382 if (parms && parms != void_list_node
13383 && TREE_PURPOSE (parms) == NULL_TREE)
13384 return 1;
13386 if (!subr)
13387 for (i = 0; i < ntparms; i++)
13388 if (!TREE_VEC_ELT (targs, i))
13390 tree tparm;
13392 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13393 continue;
13395 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13397 /* If this is an undeduced nontype parameter that depends on
13398 a type parameter, try another pass; its type may have been
13399 deduced from a later argument than the one from which
13400 this parameter can be deduced. */
13401 if (TREE_CODE (tparm) == PARM_DECL
13402 && uses_template_parms (TREE_TYPE (tparm))
13403 && !saw_undeduced++)
13404 goto again;
13406 /* Core issue #226 (C++0x) [temp.deduct]:
13408 If a template argument has not been deduced, its
13409 default template argument, if any, is used.
13411 When we are in C++98 mode, TREE_PURPOSE will either
13412 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13413 to explicitly check cxx_dialect here. */
13414 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13416 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13417 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13418 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13419 arg = convert_template_argument (parm, arg, targs, tf_none,
13420 i, NULL_TREE);
13421 if (arg == error_mark_node)
13422 return 1;
13423 else
13425 TREE_VEC_ELT (targs, i) = arg;
13426 continue;
13430 /* If the type parameter is a parameter pack, then it will
13431 be deduced to an empty parameter pack. */
13432 if (template_parameter_pack_p (tparm))
13434 tree arg;
13436 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13438 arg = make_node (NONTYPE_ARGUMENT_PACK);
13439 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13440 TREE_CONSTANT (arg) = 1;
13442 else
13443 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13445 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13447 TREE_VEC_ELT (targs, i) = arg;
13448 continue;
13451 return 2;
13454 return 0;
13457 /* Subroutine of type_unification_real. Args are like the variables
13458 at the call site. ARG is an overloaded function (or template-id);
13459 we try deducing template args from each of the overloads, and if
13460 only one succeeds, we go with that. Modifies TARGS and returns
13461 true on success. */
13463 static bool
13464 resolve_overloaded_unification (tree tparms,
13465 tree targs,
13466 tree parm,
13467 tree arg,
13468 unification_kind_t strict,
13469 int sub_strict)
13471 tree tempargs = copy_node (targs);
13472 int good = 0;
13473 tree goodfn = NULL_TREE;
13474 bool addr_p;
13476 if (TREE_CODE (arg) == ADDR_EXPR)
13478 arg = TREE_OPERAND (arg, 0);
13479 addr_p = true;
13481 else
13482 addr_p = false;
13484 if (TREE_CODE (arg) == COMPONENT_REF)
13485 /* Handle `&x' where `x' is some static or non-static member
13486 function name. */
13487 arg = TREE_OPERAND (arg, 1);
13489 if (TREE_CODE (arg) == OFFSET_REF)
13490 arg = TREE_OPERAND (arg, 1);
13492 /* Strip baselink information. */
13493 if (BASELINK_P (arg))
13494 arg = BASELINK_FUNCTIONS (arg);
13496 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13498 /* If we got some explicit template args, we need to plug them into
13499 the affected templates before we try to unify, in case the
13500 explicit args will completely resolve the templates in question. */
13502 tree expl_subargs = TREE_OPERAND (arg, 1);
13503 arg = TREE_OPERAND (arg, 0);
13505 for (; arg; arg = OVL_NEXT (arg))
13507 tree fn = OVL_CURRENT (arg);
13508 tree subargs, elem;
13510 if (TREE_CODE (fn) != TEMPLATE_DECL)
13511 continue;
13513 ++processing_template_decl;
13514 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13515 expl_subargs, /*check_ret=*/false);
13516 if (subargs)
13518 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13519 if (try_one_overload (tparms, targs, tempargs, parm,
13520 elem, strict, sub_strict, addr_p)
13521 && (!goodfn || !decls_match (goodfn, elem)))
13523 goodfn = elem;
13524 ++good;
13527 --processing_template_decl;
13530 else if (TREE_CODE (arg) != OVERLOAD
13531 && TREE_CODE (arg) != FUNCTION_DECL)
13532 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13533 -- but the deduction does not succeed because the expression is
13534 not just the function on its own. */
13535 return false;
13536 else
13537 for (; arg; arg = OVL_NEXT (arg))
13538 if (try_one_overload (tparms, targs, tempargs, parm,
13539 TREE_TYPE (OVL_CURRENT (arg)),
13540 strict, sub_strict, addr_p)
13541 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13543 goodfn = OVL_CURRENT (arg);
13544 ++good;
13547 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13548 to function or pointer to member function argument if the set of
13549 overloaded functions does not contain function templates and at most
13550 one of a set of overloaded functions provides a unique match.
13552 So if we found multiple possibilities, we return success but don't
13553 deduce anything. */
13555 if (good == 1)
13557 int i = TREE_VEC_LENGTH (targs);
13558 for (; i--; )
13559 if (TREE_VEC_ELT (tempargs, i))
13560 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13562 if (good)
13563 return true;
13565 return false;
13568 /* Core DR 115: In contexts where deduction is done and fails, or in
13569 contexts where deduction is not done, if a template argument list is
13570 specified and it, along with any default template arguments, identifies
13571 a single function template specialization, then the template-id is an
13572 lvalue for the function template specialization. */
13574 tree
13575 resolve_nondeduced_context (tree orig_expr)
13577 tree expr, offset, baselink;
13578 bool addr;
13580 if (!type_unknown_p (orig_expr))
13581 return orig_expr;
13583 expr = orig_expr;
13584 addr = false;
13585 offset = NULL_TREE;
13586 baselink = NULL_TREE;
13588 if (TREE_CODE (expr) == ADDR_EXPR)
13590 expr = TREE_OPERAND (expr, 0);
13591 addr = true;
13593 if (TREE_CODE (expr) == OFFSET_REF)
13595 offset = expr;
13596 expr = TREE_OPERAND (expr, 1);
13598 if (TREE_CODE (expr) == BASELINK)
13600 baselink = expr;
13601 expr = BASELINK_FUNCTIONS (expr);
13604 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13606 int good = 0;
13607 tree goodfn = NULL_TREE;
13609 /* If we got some explicit template args, we need to plug them into
13610 the affected templates before we try to unify, in case the
13611 explicit args will completely resolve the templates in question. */
13613 tree expl_subargs = TREE_OPERAND (expr, 1);
13614 tree arg = TREE_OPERAND (expr, 0);
13615 tree badfn = NULL_TREE;
13616 tree badargs = NULL_TREE;
13618 for (; arg; arg = OVL_NEXT (arg))
13620 tree fn = OVL_CURRENT (arg);
13621 tree subargs, elem;
13623 if (TREE_CODE (fn) != TEMPLATE_DECL)
13624 continue;
13626 ++processing_template_decl;
13627 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13628 expl_subargs, /*check_ret=*/false);
13629 if (subargs && !any_dependent_template_arguments_p (subargs))
13631 elem = instantiate_template (fn, subargs, tf_none);
13632 if (elem == error_mark_node)
13634 badfn = fn;
13635 badargs = subargs;
13637 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
13639 goodfn = elem;
13640 ++good;
13643 --processing_template_decl;
13645 if (good == 1)
13647 expr = goodfn;
13648 if (baselink)
13649 expr = build_baselink (BASELINK_BINFO (baselink),
13650 BASELINK_ACCESS_BINFO (baselink),
13651 expr, BASELINK_OPTYPE (baselink));
13652 if (offset)
13653 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
13654 TREE_OPERAND (offset, 0), expr);
13655 if (addr)
13656 expr = build_address (expr);
13657 return expr;
13659 else if (good == 0 && badargs)
13660 /* There were no good options and at least one bad one, so let the
13661 user know what the problem is. */
13662 instantiate_template (badfn, badargs, tf_warning_or_error);
13664 return orig_expr;
13667 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13668 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13669 different overloads deduce different arguments for a given parm.
13670 ADDR_P is true if the expression for which deduction is being
13671 performed was of the form "& fn" rather than simply "fn".
13673 Returns 1 on success. */
13675 static int
13676 try_one_overload (tree tparms,
13677 tree orig_targs,
13678 tree targs,
13679 tree parm,
13680 tree arg,
13681 unification_kind_t strict,
13682 int sub_strict,
13683 bool addr_p)
13685 int nargs;
13686 tree tempargs;
13687 int i;
13689 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13690 to function or pointer to member function argument if the set of
13691 overloaded functions does not contain function templates and at most
13692 one of a set of overloaded functions provides a unique match.
13694 So if this is a template, just return success. */
13696 if (uses_template_parms (arg))
13697 return 1;
13699 if (TREE_CODE (arg) == METHOD_TYPE)
13700 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13701 else if (addr_p)
13702 arg = build_pointer_type (arg);
13704 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13706 /* We don't copy orig_targs for this because if we have already deduced
13707 some template args from previous args, unify would complain when we
13708 try to deduce a template parameter for the same argument, even though
13709 there isn't really a conflict. */
13710 nargs = TREE_VEC_LENGTH (targs);
13711 tempargs = make_tree_vec (nargs);
13713 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13714 return 0;
13716 /* First make sure we didn't deduce anything that conflicts with
13717 explicitly specified args. */
13718 for (i = nargs; i--; )
13720 tree elt = TREE_VEC_ELT (tempargs, i);
13721 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13723 if (!elt)
13724 /*NOP*/;
13725 else if (uses_template_parms (elt))
13726 /* Since we're unifying against ourselves, we will fill in
13727 template args used in the function parm list with our own
13728 template parms. Discard them. */
13729 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13730 else if (oldelt && !template_args_equal (oldelt, elt))
13731 return 0;
13734 for (i = nargs; i--; )
13736 tree elt = TREE_VEC_ELT (tempargs, i);
13738 if (elt)
13739 TREE_VEC_ELT (targs, i) = elt;
13742 return 1;
13745 /* PARM is a template class (perhaps with unbound template
13746 parameters). ARG is a fully instantiated type. If ARG can be
13747 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13748 TARGS are as for unify. */
13750 static tree
13751 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13753 tree copy_of_targs;
13755 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13756 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13757 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13758 return NULL_TREE;
13760 /* We need to make a new template argument vector for the call to
13761 unify. If we used TARGS, we'd clutter it up with the result of
13762 the attempted unification, even if this class didn't work out.
13763 We also don't want to commit ourselves to all the unifications
13764 we've already done, since unification is supposed to be done on
13765 an argument-by-argument basis. In other words, consider the
13766 following pathological case:
13768 template <int I, int J, int K>
13769 struct S {};
13771 template <int I, int J>
13772 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13774 template <int I, int J, int K>
13775 void f(S<I, J, K>, S<I, I, I>);
13777 void g() {
13778 S<0, 0, 0> s0;
13779 S<0, 1, 2> s2;
13781 f(s0, s2);
13784 Now, by the time we consider the unification involving `s2', we
13785 already know that we must have `f<0, 0, 0>'. But, even though
13786 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13787 because there are two ways to unify base classes of S<0, 1, 2>
13788 with S<I, I, I>. If we kept the already deduced knowledge, we
13789 would reject the possibility I=1. */
13790 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13792 /* If unification failed, we're done. */
13793 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13794 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
13795 return NULL_TREE;
13797 return arg;
13800 /* Given a template type PARM and a class type ARG, find the unique
13801 base type in ARG that is an instance of PARM. We do not examine
13802 ARG itself; only its base-classes. If there is not exactly one
13803 appropriate base class, return NULL_TREE. PARM may be the type of
13804 a partial specialization, as well as a plain template type. Used
13805 by unify. */
13807 static tree
13808 get_template_base (tree tparms, tree targs, tree parm, tree arg)
13810 tree rval = NULL_TREE;
13811 tree binfo;
13813 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
13815 binfo = TYPE_BINFO (complete_type (arg));
13816 if (!binfo)
13817 /* The type could not be completed. */
13818 return NULL_TREE;
13820 /* Walk in inheritance graph order. The search order is not
13821 important, and this avoids multiple walks of virtual bases. */
13822 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
13824 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
13826 if (r)
13828 /* If there is more than one satisfactory baseclass, then:
13830 [temp.deduct.call]
13832 If they yield more than one possible deduced A, the type
13833 deduction fails.
13835 applies. */
13836 if (rval && !same_type_p (r, rval))
13837 return NULL_TREE;
13839 rval = r;
13843 return rval;
13846 /* Returns the level of DECL, which declares a template parameter. */
13848 static int
13849 template_decl_level (tree decl)
13851 switch (TREE_CODE (decl))
13853 case TYPE_DECL:
13854 case TEMPLATE_DECL:
13855 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13857 case PARM_DECL:
13858 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13860 default:
13861 gcc_unreachable ();
13863 return 0;
13866 /* Decide whether ARG can be unified with PARM, considering only the
13867 cv-qualifiers of each type, given STRICT as documented for unify.
13868 Returns nonzero iff the unification is OK on that basis. */
13870 static int
13871 check_cv_quals_for_unify (int strict, tree arg, tree parm)
13873 int arg_quals = cp_type_quals (arg);
13874 int parm_quals = cp_type_quals (parm);
13876 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13877 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13879 /* Although a CVR qualifier is ignored when being applied to a
13880 substituted template parameter ([8.3.2]/1 for example), that
13881 does not apply during deduction [14.8.2.4]/1, (even though
13882 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13883 this). Except when we're allowing additional CV qualifiers
13884 at the outer level [14.8.2.1]/3,1st bullet. */
13885 if ((TREE_CODE (arg) == REFERENCE_TYPE
13886 || TREE_CODE (arg) == FUNCTION_TYPE
13887 || TREE_CODE (arg) == METHOD_TYPE)
13888 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13889 return 0;
13891 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13892 && (parm_quals & TYPE_QUAL_RESTRICT))
13893 return 0;
13896 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13897 && (arg_quals & parm_quals) != parm_quals)
13898 return 0;
13900 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13901 && (parm_quals & arg_quals) != arg_quals)
13902 return 0;
13904 return 1;
13907 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13908 void
13909 template_parm_level_and_index (tree parm, int* level, int* index)
13911 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13912 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13913 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13915 *index = TEMPLATE_TYPE_IDX (parm);
13916 *level = TEMPLATE_TYPE_LEVEL (parm);
13918 else
13920 *index = TEMPLATE_PARM_IDX (parm);
13921 *level = TEMPLATE_PARM_LEVEL (parm);
13925 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13926 expansion at the end of PACKED_PARMS. Returns 0 if the type
13927 deduction succeeds, 1 otherwise. STRICT is the same as in
13928 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13929 call argument list. We'll need to adjust the arguments to make them
13930 types. SUBR tells us if this is from a recursive call to
13931 type_unification_real. */
13933 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13934 tree packed_args, int strict, bool call_args_p,
13935 bool subr)
13937 tree parm
13938 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13939 tree pattern = PACK_EXPANSION_PATTERN (parm);
13940 tree pack, packs = NULL_TREE;
13941 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13942 int len = TREE_VEC_LENGTH (packed_args);
13944 /* Determine the parameter packs we will be deducing from the
13945 pattern, and record their current deductions. */
13946 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13947 pack; pack = TREE_CHAIN (pack))
13949 tree parm_pack = TREE_VALUE (pack);
13950 int idx, level;
13952 /* Determine the index and level of this parameter pack. */
13953 template_parm_level_and_index (parm_pack, &level, &idx);
13955 /* Keep track of the parameter packs and their corresponding
13956 argument packs. */
13957 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13958 TREE_TYPE (packs) = make_tree_vec (len - start);
13961 /* Loop through all of the arguments that have not yet been
13962 unified and unify each with the pattern. */
13963 for (i = start; i < len; i++)
13965 tree parm = pattern;
13967 /* For each parameter pack, clear out the deduced value so that
13968 we can deduce it again. */
13969 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13971 int idx, level;
13972 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13974 TMPL_ARG (targs, level, idx) = NULL_TREE;
13977 /* Unify the pattern with the current argument. */
13979 tree arg = TREE_VEC_ELT (packed_args, i);
13980 tree arg_expr = NULL_TREE;
13981 int arg_strict = strict;
13982 bool skip_arg_p = false;
13984 if (call_args_p)
13986 int sub_strict;
13988 /* This mirrors what we do in type_unification_real. */
13989 switch (strict)
13991 case DEDUCE_CALL:
13992 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13993 | UNIFY_ALLOW_MORE_CV_QUAL
13994 | UNIFY_ALLOW_DERIVED);
13995 break;
13997 case DEDUCE_CONV:
13998 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13999 break;
14001 case DEDUCE_EXACT:
14002 sub_strict = UNIFY_ALLOW_NONE;
14003 break;
14005 default:
14006 gcc_unreachable ();
14009 if (!TYPE_P (arg))
14011 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14012 if (type_unknown_p (arg))
14014 /* [temp.deduct.type] A template-argument can be
14015 deduced from a pointer to function or pointer
14016 to member function argument if the set of
14017 overloaded functions does not contain function
14018 templates and at most one of a set of
14019 overloaded functions provides a unique
14020 match. */
14022 if (resolve_overloaded_unification
14023 (tparms, targs, parm, arg,
14024 (unification_kind_t) strict,
14025 sub_strict)
14026 != 0)
14027 return 1;
14028 skip_arg_p = true;
14031 if (!skip_arg_p)
14033 arg_expr = arg;
14034 arg = unlowered_expr_type (arg);
14035 if (arg == error_mark_node)
14036 return 1;
14040 arg_strict = sub_strict;
14042 if (!subr)
14043 arg_strict |=
14044 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14045 &parm, &arg, arg_expr);
14048 if (!skip_arg_p)
14050 /* For deduction from an init-list we need the actual list. */
14051 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14052 arg = arg_expr;
14053 if (unify (tparms, targs, parm, arg, arg_strict))
14054 return 1;
14058 /* For each parameter pack, collect the deduced value. */
14059 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14061 int idx, level;
14062 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14064 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14065 TMPL_ARG (targs, level, idx);
14069 /* Verify that the results of unification with the parameter packs
14070 produce results consistent with what we've seen before, and make
14071 the deduced argument packs available. */
14072 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14074 tree old_pack = TREE_VALUE (pack);
14075 tree new_args = TREE_TYPE (pack);
14076 int i, len = TREE_VEC_LENGTH (new_args);
14077 bool nondeduced_p = false;
14079 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14080 actually deduce anything. */
14081 for (i = 0; i < len && !nondeduced_p; ++i)
14082 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14083 nondeduced_p = true;
14084 if (nondeduced_p)
14085 continue;
14087 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14089 /* Prepend the explicit arguments onto NEW_ARGS. */
14090 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14091 tree old_args = new_args;
14092 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14093 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14095 /* Copy the explicit arguments. */
14096 new_args = make_tree_vec (len);
14097 for (i = 0; i < explicit_len; i++)
14098 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14100 /* Copy the deduced arguments. */
14101 for (; i < len; i++)
14102 TREE_VEC_ELT (new_args, i) =
14103 TREE_VEC_ELT (old_args, i - explicit_len);
14106 if (!old_pack)
14108 tree result;
14109 int idx, level;
14111 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14113 /* Build the deduced *_ARGUMENT_PACK. */
14114 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14116 result = make_node (NONTYPE_ARGUMENT_PACK);
14117 TREE_TYPE (result) =
14118 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14119 TREE_CONSTANT (result) = 1;
14121 else
14122 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14124 SET_ARGUMENT_PACK_ARGS (result, new_args);
14126 /* Note the deduced argument packs for this parameter
14127 pack. */
14128 TMPL_ARG (targs, level, idx) = result;
14130 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14131 && (ARGUMENT_PACK_ARGS (old_pack)
14132 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14134 /* We only had the explicitly-provided arguments before, but
14135 now we have a complete set of arguments. */
14136 int idx, level;
14137 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14138 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14140 /* Keep the original deduced argument pack. */
14141 TMPL_ARG (targs, level, idx) = old_pack;
14143 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14144 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14145 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14147 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14148 new_args))
14149 /* Inconsistent unification of this parameter pack. */
14150 return 1;
14151 else
14153 int idx, level;
14155 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14157 /* Keep the original deduced argument pack. */
14158 TMPL_ARG (targs, level, idx) = old_pack;
14162 return 0;
14165 /* Deduce the value of template parameters. TPARMS is the (innermost)
14166 set of template parameters to a template. TARGS is the bindings
14167 for those template parameters, as determined thus far; TARGS may
14168 include template arguments for outer levels of template parameters
14169 as well. PARM is a parameter to a template function, or a
14170 subcomponent of that parameter; ARG is the corresponding argument.
14171 This function attempts to match PARM with ARG in a manner
14172 consistent with the existing assignments in TARGS. If more values
14173 are deduced, then TARGS is updated.
14175 Returns 0 if the type deduction succeeds, 1 otherwise. The
14176 parameter STRICT is a bitwise or of the following flags:
14178 UNIFY_ALLOW_NONE:
14179 Require an exact match between PARM and ARG.
14180 UNIFY_ALLOW_MORE_CV_QUAL:
14181 Allow the deduced ARG to be more cv-qualified (by qualification
14182 conversion) than ARG.
14183 UNIFY_ALLOW_LESS_CV_QUAL:
14184 Allow the deduced ARG to be less cv-qualified than ARG.
14185 UNIFY_ALLOW_DERIVED:
14186 Allow the deduced ARG to be a template base class of ARG,
14187 or a pointer to a template base class of the type pointed to by
14188 ARG.
14189 UNIFY_ALLOW_INTEGER:
14190 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14191 case for more information.
14192 UNIFY_ALLOW_OUTER_LEVEL:
14193 This is the outermost level of a deduction. Used to determine validity
14194 of qualification conversions. A valid qualification conversion must
14195 have const qualified pointers leading up to the inner type which
14196 requires additional CV quals, except at the outer level, where const
14197 is not required [conv.qual]. It would be normal to set this flag in
14198 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14199 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14200 This is the outermost level of a deduction, and PARM can be more CV
14201 qualified at this point.
14202 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14203 This is the outermost level of a deduction, and PARM can be less CV
14204 qualified at this point. */
14206 static int
14207 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14209 int idx;
14210 tree targ;
14211 tree tparm;
14212 int strict_in = strict;
14214 /* I don't think this will do the right thing with respect to types.
14215 But the only case I've seen it in so far has been array bounds, where
14216 signedness is the only information lost, and I think that will be
14217 okay. */
14218 while (TREE_CODE (parm) == NOP_EXPR)
14219 parm = TREE_OPERAND (parm, 0);
14221 if (arg == error_mark_node)
14222 return 1;
14223 if (arg == unknown_type_node
14224 || arg == init_list_type_node)
14225 /* We can't deduce anything from this, but we might get all the
14226 template args from other function args. */
14227 return 0;
14229 /* If PARM uses template parameters, then we can't bail out here,
14230 even if ARG == PARM, since we won't record unifications for the
14231 template parameters. We might need them if we're trying to
14232 figure out which of two things is more specialized. */
14233 if (arg == parm && !uses_template_parms (parm))
14234 return 0;
14236 /* Handle init lists early, so the rest of the function can assume
14237 we're dealing with a type. */
14238 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14240 tree elt, elttype;
14241 unsigned i;
14242 tree orig_parm = parm;
14244 /* Replace T with std::initializer_list<T> for deduction. */
14245 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14246 && flag_deduce_init_list)
14247 parm = listify (parm);
14249 if (!is_std_init_list (parm))
14250 /* We can only deduce from an initializer list argument if the
14251 parameter is std::initializer_list; otherwise this is a
14252 non-deduced context. */
14253 return 0;
14255 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14257 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14259 int elt_strict = strict;
14260 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14262 tree type = TREE_TYPE (elt);
14263 /* It should only be possible to get here for a call. */
14264 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14265 elt_strict |= maybe_adjust_types_for_deduction
14266 (DEDUCE_CALL, &elttype, &type, elt);
14267 elt = type;
14270 if (unify (tparms, targs, elttype, elt, elt_strict))
14271 return 1;
14274 /* If the std::initializer_list<T> deduction worked, replace the
14275 deduced A with std::initializer_list<A>. */
14276 if (orig_parm != parm)
14278 idx = TEMPLATE_TYPE_IDX (orig_parm);
14279 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14280 targ = listify (targ);
14281 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14283 return 0;
14286 /* Immediately reject some pairs that won't unify because of
14287 cv-qualification mismatches. */
14288 if (TREE_CODE (arg) == TREE_CODE (parm)
14289 && TYPE_P (arg)
14290 /* It is the elements of the array which hold the cv quals of an array
14291 type, and the elements might be template type parms. We'll check
14292 when we recurse. */
14293 && TREE_CODE (arg) != ARRAY_TYPE
14294 /* We check the cv-qualifiers when unifying with template type
14295 parameters below. We want to allow ARG `const T' to unify with
14296 PARM `T' for example, when computing which of two templates
14297 is more specialized, for example. */
14298 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14299 && !check_cv_quals_for_unify (strict_in, arg, parm))
14300 return 1;
14302 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14303 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14304 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14305 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14306 strict &= ~UNIFY_ALLOW_DERIVED;
14307 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14308 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14310 switch (TREE_CODE (parm))
14312 case TYPENAME_TYPE:
14313 case SCOPE_REF:
14314 case UNBOUND_CLASS_TEMPLATE:
14315 /* In a type which contains a nested-name-specifier, template
14316 argument values cannot be deduced for template parameters used
14317 within the nested-name-specifier. */
14318 return 0;
14320 case TEMPLATE_TYPE_PARM:
14321 case TEMPLATE_TEMPLATE_PARM:
14322 case BOUND_TEMPLATE_TEMPLATE_PARM:
14323 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14324 if (tparm == error_mark_node)
14325 return 1;
14327 if (TEMPLATE_TYPE_LEVEL (parm)
14328 != template_decl_level (tparm))
14329 /* The PARM is not one we're trying to unify. Just check
14330 to see if it matches ARG. */
14331 return (TREE_CODE (arg) == TREE_CODE (parm)
14332 && same_type_p (parm, arg)) ? 0 : 1;
14333 idx = TEMPLATE_TYPE_IDX (parm);
14334 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14335 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14337 /* Check for mixed types and values. */
14338 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14339 && TREE_CODE (tparm) != TYPE_DECL)
14340 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14341 && TREE_CODE (tparm) != TEMPLATE_DECL))
14342 return 1;
14344 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14346 /* ARG must be constructed from a template class or a template
14347 template parameter. */
14348 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14349 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14350 return 1;
14353 tree parmvec = TYPE_TI_ARGS (parm);
14354 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14355 tree parm_parms
14356 = DECL_INNERMOST_TEMPLATE_PARMS
14357 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14358 int i, len;
14359 int parm_variadic_p = 0;
14361 /* The resolution to DR150 makes clear that default
14362 arguments for an N-argument may not be used to bind T
14363 to a template template parameter with fewer than N
14364 parameters. It is not safe to permit the binding of
14365 default arguments as an extension, as that may change
14366 the meaning of a conforming program. Consider:
14368 struct Dense { static const unsigned int dim = 1; };
14370 template <template <typename> class View,
14371 typename Block>
14372 void operator+(float, View<Block> const&);
14374 template <typename Block,
14375 unsigned int Dim = Block::dim>
14376 struct Lvalue_proxy { operator float() const; };
14378 void
14379 test_1d (void) {
14380 Lvalue_proxy<Dense> p;
14381 float b;
14382 b + p;
14385 Here, if Lvalue_proxy is permitted to bind to View, then
14386 the global operator+ will be used; if they are not, the
14387 Lvalue_proxy will be converted to float. */
14388 if (coerce_template_parms (parm_parms,
14389 argvec,
14390 TYPE_TI_TEMPLATE (parm),
14391 tf_none,
14392 /*require_all_args=*/true,
14393 /*use_default_args=*/false)
14394 == error_mark_node)
14395 return 1;
14397 /* Deduce arguments T, i from TT<T> or TT<i>.
14398 We check each element of PARMVEC and ARGVEC individually
14399 rather than the whole TREE_VEC since they can have
14400 different number of elements. */
14402 parmvec = expand_template_argument_pack (parmvec);
14403 argvec = expand_template_argument_pack (argvec);
14405 len = TREE_VEC_LENGTH (parmvec);
14407 /* Check if the parameters end in a pack, making them
14408 variadic. */
14409 if (len > 0
14410 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14411 parm_variadic_p = 1;
14413 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14414 return 1;
14416 for (i = 0; i < len - parm_variadic_p; ++i)
14418 if (unify (tparms, targs,
14419 TREE_VEC_ELT (parmvec, i),
14420 TREE_VEC_ELT (argvec, i),
14421 UNIFY_ALLOW_NONE))
14422 return 1;
14425 if (parm_variadic_p
14426 && unify_pack_expansion (tparms, targs,
14427 parmvec, argvec,
14428 UNIFY_ALLOW_NONE,
14429 /*call_args_p=*/false,
14430 /*subr=*/false))
14431 return 1;
14433 arg = TYPE_TI_TEMPLATE (arg);
14435 /* Fall through to deduce template name. */
14438 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14439 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14441 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14443 /* Simple cases: Value already set, does match or doesn't. */
14444 if (targ != NULL_TREE && template_args_equal (targ, arg))
14445 return 0;
14446 else if (targ)
14447 return 1;
14449 else
14451 /* If PARM is `const T' and ARG is only `int', we don't have
14452 a match unless we are allowing additional qualification.
14453 If ARG is `const int' and PARM is just `T' that's OK;
14454 that binds `const int' to `T'. */
14455 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14456 arg, parm))
14457 return 1;
14459 /* Consider the case where ARG is `const volatile int' and
14460 PARM is `const T'. Then, T should be `volatile int'. */
14461 arg = cp_build_qualified_type_real
14462 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14463 if (arg == error_mark_node)
14464 return 1;
14466 /* Simple cases: Value already set, does match or doesn't. */
14467 if (targ != NULL_TREE && same_type_p (targ, arg))
14468 return 0;
14469 else if (targ)
14470 return 1;
14472 /* Make sure that ARG is not a variable-sized array. (Note
14473 that were talking about variable-sized arrays (like
14474 `int[n]'), rather than arrays of unknown size (like
14475 `int[]').) We'll get very confused by such a type since
14476 the bound of the array will not be computable in an
14477 instantiation. Besides, such types are not allowed in
14478 ISO C++, so we can do as we please here. */
14479 if (variably_modified_type_p (arg, NULL_TREE))
14480 return 1;
14482 /* Strip typedefs as in convert_template_argument. */
14483 arg = strip_typedefs (arg);
14486 /* If ARG is a parameter pack or an expansion, we cannot unify
14487 against it unless PARM is also a parameter pack. */
14488 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14489 && !template_parameter_pack_p (parm))
14490 return 1;
14492 /* If the argument deduction results is a METHOD_TYPE,
14493 then there is a problem.
14494 METHOD_TYPE doesn't map to any real C++ type the result of
14495 the deduction can not be of that type. */
14496 if (TREE_CODE (arg) == METHOD_TYPE)
14497 return 1;
14499 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14500 return 0;
14502 case TEMPLATE_PARM_INDEX:
14503 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14504 if (tparm == error_mark_node)
14505 return 1;
14507 if (TEMPLATE_PARM_LEVEL (parm)
14508 != template_decl_level (tparm))
14509 /* The PARM is not one we're trying to unify. Just check
14510 to see if it matches ARG. */
14511 return !(TREE_CODE (arg) == TREE_CODE (parm)
14512 && cp_tree_equal (parm, arg));
14514 idx = TEMPLATE_PARM_IDX (parm);
14515 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14517 if (targ)
14518 return !cp_tree_equal (targ, arg);
14520 /* [temp.deduct.type] If, in the declaration of a function template
14521 with a non-type template-parameter, the non-type
14522 template-parameter is used in an expression in the function
14523 parameter-list and, if the corresponding template-argument is
14524 deduced, the template-argument type shall match the type of the
14525 template-parameter exactly, except that a template-argument
14526 deduced from an array bound may be of any integral type.
14527 The non-type parameter might use already deduced type parameters. */
14528 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14529 if (!TREE_TYPE (arg))
14530 /* Template-parameter dependent expression. Just accept it for now.
14531 It will later be processed in convert_template_argument. */
14533 else if (same_type_p (TREE_TYPE (arg), tparm))
14534 /* OK */;
14535 else if ((strict & UNIFY_ALLOW_INTEGER)
14536 && (TREE_CODE (tparm) == INTEGER_TYPE
14537 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14538 /* Convert the ARG to the type of PARM; the deduced non-type
14539 template argument must exactly match the types of the
14540 corresponding parameter. */
14541 arg = fold (build_nop (tparm, arg));
14542 else if (uses_template_parms (tparm))
14543 /* We haven't deduced the type of this parameter yet. Try again
14544 later. */
14545 return 0;
14546 else
14547 return 1;
14549 /* If ARG is a parameter pack or an expansion, we cannot unify
14550 against it unless PARM is also a parameter pack. */
14551 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14552 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14553 return 1;
14555 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14556 return 0;
14558 case PTRMEM_CST:
14560 /* A pointer-to-member constant can be unified only with
14561 another constant. */
14562 if (TREE_CODE (arg) != PTRMEM_CST)
14563 return 1;
14565 /* Just unify the class member. It would be useless (and possibly
14566 wrong, depending on the strict flags) to unify also
14567 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14568 arg refer to the same variable, even if through different
14569 classes. For instance:
14571 struct A { int x; };
14572 struct B : A { };
14574 Unification of &A::x and &B::x must succeed. */
14575 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14576 PTRMEM_CST_MEMBER (arg), strict);
14579 case POINTER_TYPE:
14581 if (TREE_CODE (arg) != POINTER_TYPE)
14582 return 1;
14584 /* [temp.deduct.call]
14586 A can be another pointer or pointer to member type that can
14587 be converted to the deduced A via a qualification
14588 conversion (_conv.qual_).
14590 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14591 This will allow for additional cv-qualification of the
14592 pointed-to types if appropriate. */
14594 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14595 /* The derived-to-base conversion only persists through one
14596 level of pointers. */
14597 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14599 return unify (tparms, targs, TREE_TYPE (parm),
14600 TREE_TYPE (arg), strict);
14603 case REFERENCE_TYPE:
14604 if (TREE_CODE (arg) != REFERENCE_TYPE)
14605 return 1;
14606 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14607 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14609 case ARRAY_TYPE:
14610 if (TREE_CODE (arg) != ARRAY_TYPE)
14611 return 1;
14612 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14613 != (TYPE_DOMAIN (arg) == NULL_TREE))
14614 return 1;
14615 if (TYPE_DOMAIN (parm) != NULL_TREE)
14617 tree parm_max;
14618 tree arg_max;
14619 bool parm_cst;
14620 bool arg_cst;
14622 /* Our representation of array types uses "N - 1" as the
14623 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14624 not an integer constant. We cannot unify arbitrarily
14625 complex expressions, so we eliminate the MINUS_EXPRs
14626 here. */
14627 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14628 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14629 if (!parm_cst)
14631 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14632 parm_max = TREE_OPERAND (parm_max, 0);
14634 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14635 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14636 if (!arg_cst)
14638 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14639 trying to unify the type of a variable with the type
14640 of a template parameter. For example:
14642 template <unsigned int N>
14643 void f (char (&) [N]);
14644 int g();
14645 void h(int i) {
14646 char a[g(i)];
14647 f(a);
14650 Here, the type of the ARG will be "int [g(i)]", and
14651 may be a SAVE_EXPR, etc. */
14652 if (TREE_CODE (arg_max) != MINUS_EXPR)
14653 return 1;
14654 arg_max = TREE_OPERAND (arg_max, 0);
14657 /* If only one of the bounds used a MINUS_EXPR, compensate
14658 by adding one to the other bound. */
14659 if (parm_cst && !arg_cst)
14660 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14661 integer_type_node,
14662 parm_max,
14663 integer_one_node);
14664 else if (arg_cst && !parm_cst)
14665 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14666 integer_type_node,
14667 arg_max,
14668 integer_one_node);
14670 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14671 return 1;
14673 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14674 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14676 case REAL_TYPE:
14677 case COMPLEX_TYPE:
14678 case VECTOR_TYPE:
14679 case INTEGER_TYPE:
14680 case BOOLEAN_TYPE:
14681 case ENUMERAL_TYPE:
14682 case VOID_TYPE:
14683 if (TREE_CODE (arg) != TREE_CODE (parm))
14684 return 1;
14686 /* We have already checked cv-qualification at the top of the
14687 function. */
14688 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14689 return 1;
14691 /* As far as unification is concerned, this wins. Later checks
14692 will invalidate it if necessary. */
14693 return 0;
14695 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14696 /* Type INTEGER_CST can come from ordinary constant template args. */
14697 case INTEGER_CST:
14698 while (TREE_CODE (arg) == NOP_EXPR)
14699 arg = TREE_OPERAND (arg, 0);
14701 if (TREE_CODE (arg) != INTEGER_CST)
14702 return 1;
14703 return !tree_int_cst_equal (parm, arg);
14705 case TREE_VEC:
14707 int i;
14708 if (TREE_CODE (arg) != TREE_VEC)
14709 return 1;
14710 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14711 return 1;
14712 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14713 if (unify (tparms, targs,
14714 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14715 UNIFY_ALLOW_NONE))
14716 return 1;
14717 return 0;
14720 case RECORD_TYPE:
14721 case UNION_TYPE:
14722 if (TREE_CODE (arg) != TREE_CODE (parm))
14723 return 1;
14725 if (TYPE_PTRMEMFUNC_P (parm))
14727 if (!TYPE_PTRMEMFUNC_P (arg))
14728 return 1;
14730 return unify (tparms, targs,
14731 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14732 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14733 strict);
14736 if (CLASSTYPE_TEMPLATE_INFO (parm))
14738 tree t = NULL_TREE;
14740 if (strict_in & UNIFY_ALLOW_DERIVED)
14742 /* First, we try to unify the PARM and ARG directly. */
14743 t = try_class_unification (tparms, targs,
14744 parm, arg);
14746 if (!t)
14748 /* Fallback to the special case allowed in
14749 [temp.deduct.call]:
14751 If P is a class, and P has the form
14752 template-id, then A can be a derived class of
14753 the deduced A. Likewise, if P is a pointer to
14754 a class of the form template-id, A can be a
14755 pointer to a derived class pointed to by the
14756 deduced A. */
14757 t = get_template_base (tparms, targs, parm, arg);
14759 if (!t)
14760 return 1;
14763 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14764 && (CLASSTYPE_TI_TEMPLATE (parm)
14765 == CLASSTYPE_TI_TEMPLATE (arg)))
14766 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14767 Then, we should unify `int' and `U'. */
14768 t = arg;
14769 else
14770 /* There's no chance of unification succeeding. */
14771 return 1;
14773 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14774 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14776 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14777 return 1;
14778 return 0;
14780 case METHOD_TYPE:
14781 case FUNCTION_TYPE:
14783 unsigned int nargs;
14784 tree *args;
14785 tree a;
14786 unsigned int i;
14788 if (TREE_CODE (arg) != TREE_CODE (parm))
14789 return 1;
14791 /* CV qualifications for methods can never be deduced, they must
14792 match exactly. We need to check them explicitly here,
14793 because type_unification_real treats them as any other
14794 cv-qualified parameter. */
14795 if (TREE_CODE (parm) == METHOD_TYPE
14796 && (!check_cv_quals_for_unify
14797 (UNIFY_ALLOW_NONE,
14798 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14799 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14800 return 1;
14802 if (unify (tparms, targs, TREE_TYPE (parm),
14803 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14804 return 1;
14806 nargs = list_length (TYPE_ARG_TYPES (arg));
14807 args = XALLOCAVEC (tree, nargs);
14808 for (a = TYPE_ARG_TYPES (arg), i = 0;
14809 a != NULL_TREE && a != void_list_node;
14810 a = TREE_CHAIN (a), ++i)
14811 args[i] = TREE_VALUE (a);
14812 nargs = i;
14814 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14815 args, nargs, 1, DEDUCE_EXACT,
14816 LOOKUP_NORMAL);
14819 case OFFSET_TYPE:
14820 /* Unify a pointer to member with a pointer to member function, which
14821 deduces the type of the member as a function type. */
14822 if (TYPE_PTRMEMFUNC_P (arg))
14824 tree method_type;
14825 tree fntype;
14826 cp_cv_quals cv_quals;
14828 /* Check top-level cv qualifiers */
14829 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14830 return 1;
14832 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14833 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14834 return 1;
14836 /* Determine the type of the function we are unifying against. */
14837 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14838 fntype =
14839 build_function_type (TREE_TYPE (method_type),
14840 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14842 /* Extract the cv-qualifiers of the member function from the
14843 implicit object parameter and place them on the function
14844 type to be restored later. */
14845 cv_quals =
14846 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14847 fntype = build_qualified_type (fntype, cv_quals);
14848 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14851 if (TREE_CODE (arg) != OFFSET_TYPE)
14852 return 1;
14853 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14854 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
14855 return 1;
14856 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14857 strict);
14859 case CONST_DECL:
14860 if (DECL_TEMPLATE_PARM_P (parm))
14861 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
14862 if (arg != integral_constant_value (parm))
14863 return 1;
14864 return 0;
14866 case FIELD_DECL:
14867 case TEMPLATE_DECL:
14868 /* Matched cases are handled by the ARG == PARM test above. */
14869 return 1;
14871 case TYPE_ARGUMENT_PACK:
14872 case NONTYPE_ARGUMENT_PACK:
14874 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14875 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14876 int i, len = TREE_VEC_LENGTH (packed_parms);
14877 int argslen = TREE_VEC_LENGTH (packed_args);
14878 int parm_variadic_p = 0;
14880 for (i = 0; i < len; ++i)
14882 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14884 if (i == len - 1)
14885 /* We can unify against something with a trailing
14886 parameter pack. */
14887 parm_variadic_p = 1;
14888 else
14889 /* Since there is something following the pack
14890 expansion, we cannot unify this template argument
14891 list. */
14892 return 0;
14897 /* If we don't have enough arguments to satisfy the parameters
14898 (not counting the pack expression at the end), or we have
14899 too many arguments for a parameter list that doesn't end in
14900 a pack expression, we can't unify. */
14901 if (argslen < (len - parm_variadic_p)
14902 || (argslen > len && !parm_variadic_p))
14903 return 1;
14905 /* Unify all of the parameters that precede the (optional)
14906 pack expression. */
14907 for (i = 0; i < len - parm_variadic_p; ++i)
14909 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14910 TREE_VEC_ELT (packed_args, i), strict))
14911 return 1;
14914 if (parm_variadic_p)
14915 return unify_pack_expansion (tparms, targs,
14916 packed_parms, packed_args,
14917 strict, /*call_args_p=*/false,
14918 /*subr=*/false);
14919 return 0;
14922 break;
14924 case TYPEOF_TYPE:
14925 case DECLTYPE_TYPE:
14926 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14927 nodes. */
14928 return 0;
14930 case ERROR_MARK:
14931 /* Unification fails if we hit an error node. */
14932 return 1;
14934 default:
14935 gcc_assert (EXPR_P (parm));
14937 /* We must be looking at an expression. This can happen with
14938 something like:
14940 template <int I>
14941 void foo(S<I>, S<I + 2>);
14943 This is a "nondeduced context":
14945 [deduct.type]
14947 The nondeduced contexts are:
14949 --A type that is a template-id in which one or more of
14950 the template-arguments is an expression that references
14951 a template-parameter.
14953 In these cases, we assume deduction succeeded, but don't
14954 actually infer any unifications. */
14956 if (!uses_template_parms (parm)
14957 && !template_args_equal (parm, arg))
14958 return 1;
14959 else
14960 return 0;
14964 /* Note that DECL can be defined in this translation unit, if
14965 required. */
14967 static void
14968 mark_definable (tree decl)
14970 tree clone;
14971 DECL_NOT_REALLY_EXTERN (decl) = 1;
14972 FOR_EACH_CLONE (clone, decl)
14973 DECL_NOT_REALLY_EXTERN (clone) = 1;
14976 /* Called if RESULT is explicitly instantiated, or is a member of an
14977 explicitly instantiated class. */
14979 void
14980 mark_decl_instantiated (tree result, int extern_p)
14982 SET_DECL_EXPLICIT_INSTANTIATION (result);
14984 /* If this entity has already been written out, it's too late to
14985 make any modifications. */
14986 if (TREE_ASM_WRITTEN (result))
14987 return;
14989 if (TREE_CODE (result) != FUNCTION_DECL)
14990 /* The TREE_PUBLIC flag for function declarations will have been
14991 set correctly by tsubst. */
14992 TREE_PUBLIC (result) = 1;
14994 /* This might have been set by an earlier implicit instantiation. */
14995 DECL_COMDAT (result) = 0;
14997 if (extern_p)
14998 DECL_NOT_REALLY_EXTERN (result) = 0;
14999 else
15001 mark_definable (result);
15002 /* Always make artificials weak. */
15003 if (DECL_ARTIFICIAL (result) && flag_weak)
15004 comdat_linkage (result);
15005 /* For WIN32 we also want to put explicit instantiations in
15006 linkonce sections. */
15007 else if (TREE_PUBLIC (result))
15008 maybe_make_one_only (result);
15011 /* If EXTERN_P, then this function will not be emitted -- unless
15012 followed by an explicit instantiation, at which point its linkage
15013 will be adjusted. If !EXTERN_P, then this function will be
15014 emitted here. In neither circumstance do we want
15015 import_export_decl to adjust the linkage. */
15016 DECL_INTERFACE_KNOWN (result) = 1;
15019 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15020 important template arguments. If any are missing, we check whether
15021 they're important by using error_mark_node for substituting into any
15022 args that were used for partial ordering (the ones between ARGS and END)
15023 and seeing if it bubbles up. */
15025 static bool
15026 check_undeduced_parms (tree targs, tree args, tree end)
15028 bool found = false;
15029 int i;
15030 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15031 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15033 found = true;
15034 TREE_VEC_ELT (targs, i) = error_mark_node;
15036 if (found)
15038 for (; args != end; args = TREE_CHAIN (args))
15040 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15041 if (substed == error_mark_node)
15042 return true;
15045 return false;
15048 /* Given two function templates PAT1 and PAT2, return:
15050 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15051 -1 if PAT2 is more specialized than PAT1.
15052 0 if neither is more specialized.
15054 LEN indicates the number of parameters we should consider
15055 (defaulted parameters should not be considered).
15057 The 1998 std underspecified function template partial ordering, and
15058 DR214 addresses the issue. We take pairs of arguments, one from
15059 each of the templates, and deduce them against each other. One of
15060 the templates will be more specialized if all the *other*
15061 template's arguments deduce against its arguments and at least one
15062 of its arguments *does* *not* deduce against the other template's
15063 corresponding argument. Deduction is done as for class templates.
15064 The arguments used in deduction have reference and top level cv
15065 qualifiers removed. Iff both arguments were originally reference
15066 types *and* deduction succeeds in both directions, the template
15067 with the more cv-qualified argument wins for that pairing (if
15068 neither is more cv-qualified, they both are equal). Unlike regular
15069 deduction, after all the arguments have been deduced in this way,
15070 we do *not* verify the deduced template argument values can be
15071 substituted into non-deduced contexts.
15073 The logic can be a bit confusing here, because we look at deduce1 and
15074 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15075 can find template arguments for pat1 to make arg1 look like arg2, that
15076 means that arg2 is at least as specialized as arg1. */
15079 more_specialized_fn (tree pat1, tree pat2, int len)
15081 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15082 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15083 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15084 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15085 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15086 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15087 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15088 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15089 tree origs1, origs2;
15090 bool lose1 = false;
15091 bool lose2 = false;
15093 /* Remove the this parameter from non-static member functions. If
15094 one is a non-static member function and the other is not a static
15095 member function, remove the first parameter from that function
15096 also. This situation occurs for operator functions where we
15097 locate both a member function (with this pointer) and non-member
15098 operator (with explicit first operand). */
15099 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15101 len--; /* LEN is the number of significant arguments for DECL1 */
15102 args1 = TREE_CHAIN (args1);
15103 if (!DECL_STATIC_FUNCTION_P (decl2))
15104 args2 = TREE_CHAIN (args2);
15106 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15108 args2 = TREE_CHAIN (args2);
15109 if (!DECL_STATIC_FUNCTION_P (decl1))
15111 len--;
15112 args1 = TREE_CHAIN (args1);
15116 /* If only one is a conversion operator, they are unordered. */
15117 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15118 return 0;
15120 /* Consider the return type for a conversion function */
15121 if (DECL_CONV_FN_P (decl1))
15123 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15124 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15125 len++;
15128 processing_template_decl++;
15130 origs1 = args1;
15131 origs2 = args2;
15133 while (len--
15134 /* Stop when an ellipsis is seen. */
15135 && args1 != NULL_TREE && args2 != NULL_TREE)
15137 tree arg1 = TREE_VALUE (args1);
15138 tree arg2 = TREE_VALUE (args2);
15139 int deduce1, deduce2;
15140 int quals1 = -1;
15141 int quals2 = -1;
15143 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15144 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15146 /* When both arguments are pack expansions, we need only
15147 unify the patterns themselves. */
15148 arg1 = PACK_EXPANSION_PATTERN (arg1);
15149 arg2 = PACK_EXPANSION_PATTERN (arg2);
15151 /* This is the last comparison we need to do. */
15152 len = 0;
15155 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15157 arg1 = TREE_TYPE (arg1);
15158 quals1 = cp_type_quals (arg1);
15161 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15163 arg2 = TREE_TYPE (arg2);
15164 quals2 = cp_type_quals (arg2);
15167 if ((quals1 < 0) != (quals2 < 0))
15169 /* Only of the args is a reference, see if we should apply
15170 array/function pointer decay to it. This is not part of
15171 DR214, but is, IMHO, consistent with the deduction rules
15172 for the function call itself, and with our earlier
15173 implementation of the underspecified partial ordering
15174 rules. (nathan). */
15175 if (quals1 >= 0)
15177 switch (TREE_CODE (arg1))
15179 case ARRAY_TYPE:
15180 arg1 = TREE_TYPE (arg1);
15181 /* FALLTHROUGH. */
15182 case FUNCTION_TYPE:
15183 arg1 = build_pointer_type (arg1);
15184 break;
15186 default:
15187 break;
15190 else
15192 switch (TREE_CODE (arg2))
15194 case ARRAY_TYPE:
15195 arg2 = TREE_TYPE (arg2);
15196 /* FALLTHROUGH. */
15197 case FUNCTION_TYPE:
15198 arg2 = build_pointer_type (arg2);
15199 break;
15201 default:
15202 break;
15207 arg1 = TYPE_MAIN_VARIANT (arg1);
15208 arg2 = TYPE_MAIN_VARIANT (arg2);
15210 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15212 int i, len2 = list_length (args2);
15213 tree parmvec = make_tree_vec (1);
15214 tree argvec = make_tree_vec (len2);
15215 tree ta = args2;
15217 /* Setup the parameter vector, which contains only ARG1. */
15218 TREE_VEC_ELT (parmvec, 0) = arg1;
15220 /* Setup the argument vector, which contains the remaining
15221 arguments. */
15222 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15223 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15225 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15226 argvec, UNIFY_ALLOW_NONE,
15227 /*call_args_p=*/false,
15228 /*subr=*/0);
15230 /* We cannot deduce in the other direction, because ARG1 is
15231 a pack expansion but ARG2 is not. */
15232 deduce2 = 0;
15234 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15236 int i, len1 = list_length (args1);
15237 tree parmvec = make_tree_vec (1);
15238 tree argvec = make_tree_vec (len1);
15239 tree ta = args1;
15241 /* Setup the parameter vector, which contains only ARG1. */
15242 TREE_VEC_ELT (parmvec, 0) = arg2;
15244 /* Setup the argument vector, which contains the remaining
15245 arguments. */
15246 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15247 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15249 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15250 argvec, UNIFY_ALLOW_NONE,
15251 /*call_args_p=*/false,
15252 /*subr=*/0);
15254 /* We cannot deduce in the other direction, because ARG2 is
15255 a pack expansion but ARG1 is not.*/
15256 deduce1 = 0;
15259 else
15261 /* The normal case, where neither argument is a pack
15262 expansion. */
15263 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15264 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15267 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15268 arg2, then arg2 is not as specialized as arg1. */
15269 if (!deduce1)
15270 lose2 = true;
15271 if (!deduce2)
15272 lose1 = true;
15274 /* "If, for a given type, deduction succeeds in both directions
15275 (i.e., the types are identical after the transformations above)
15276 and if the type from the argument template is more cv-qualified
15277 than the type from the parameter template (as described above)
15278 that type is considered to be more specialized than the other. If
15279 neither type is more cv-qualified than the other then neither type
15280 is more specialized than the other."
15282 We check same_type_p explicitly because deduction can also succeed
15283 in both directions when there is a nondeduced context. */
15284 if (deduce1 && deduce2
15285 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0
15286 && same_type_p (arg1, arg2))
15288 if ((quals1 & quals2) == quals2)
15289 lose2 = true;
15290 if ((quals1 & quals2) == quals1)
15291 lose1 = true;
15294 if (lose1 && lose2)
15295 /* We've failed to deduce something in either direction.
15296 These must be unordered. */
15297 break;
15299 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15300 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15301 /* We have already processed all of the arguments in our
15302 handing of the pack expansion type. */
15303 len = 0;
15305 args1 = TREE_CHAIN (args1);
15306 args2 = TREE_CHAIN (args2);
15309 /* "In most cases, all template parameters must have values in order for
15310 deduction to succeed, but for partial ordering purposes a template
15311 parameter may remain without a value provided it is not used in the
15312 types being used for partial ordering."
15314 Thus, if we are missing any of the targs1 we need to substitute into
15315 origs1, then pat2 is not as specialized as pat1. This can happen when
15316 there is a nondeduced context. */
15317 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15318 lose2 = true;
15319 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15320 lose1 = true;
15322 processing_template_decl--;
15324 /* All things being equal, if the next argument is a pack expansion
15325 for one function but not for the other, prefer the
15326 non-variadic function. FIXME this is bogus; see c++/41958. */
15327 if (lose1 == lose2
15328 && args1 && TREE_VALUE (args1)
15329 && args2 && TREE_VALUE (args2))
15331 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15332 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15335 if (lose1 == lose2)
15336 return 0;
15337 else if (!lose1)
15338 return 1;
15339 else
15340 return -1;
15343 /* Determine which of two partial specializations is more specialized.
15345 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15346 to the first partial specialization. The TREE_VALUE is the
15347 innermost set of template parameters for the partial
15348 specialization. PAT2 is similar, but for the second template.
15350 Return 1 if the first partial specialization is more specialized;
15351 -1 if the second is more specialized; 0 if neither is more
15352 specialized.
15354 See [temp.class.order] for information about determining which of
15355 two templates is more specialized. */
15357 static int
15358 more_specialized_class (tree pat1, tree pat2)
15360 tree targs;
15361 tree tmpl1, tmpl2;
15362 int winner = 0;
15363 bool any_deductions = false;
15365 tmpl1 = TREE_TYPE (pat1);
15366 tmpl2 = TREE_TYPE (pat2);
15368 /* Just like what happens for functions, if we are ordering between
15369 different class template specializations, we may encounter dependent
15370 types in the arguments, and we need our dependency check functions
15371 to behave correctly. */
15372 ++processing_template_decl;
15373 targs = get_class_bindings (TREE_VALUE (pat1),
15374 CLASSTYPE_TI_ARGS (tmpl1),
15375 CLASSTYPE_TI_ARGS (tmpl2));
15376 if (targs)
15378 --winner;
15379 any_deductions = true;
15382 targs = get_class_bindings (TREE_VALUE (pat2),
15383 CLASSTYPE_TI_ARGS (tmpl2),
15384 CLASSTYPE_TI_ARGS (tmpl1));
15385 if (targs)
15387 ++winner;
15388 any_deductions = true;
15390 --processing_template_decl;
15392 /* In the case of a tie where at least one of the class templates
15393 has a parameter pack at the end, the template with the most
15394 non-packed parameters wins. */
15395 if (winner == 0
15396 && any_deductions
15397 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15398 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15400 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15401 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15402 int len1 = TREE_VEC_LENGTH (args1);
15403 int len2 = TREE_VEC_LENGTH (args2);
15405 /* We don't count the pack expansion at the end. */
15406 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15407 --len1;
15408 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15409 --len2;
15411 if (len1 > len2)
15412 return 1;
15413 else if (len1 < len2)
15414 return -1;
15417 return winner;
15420 /* Return the template arguments that will produce the function signature
15421 DECL from the function template FN, with the explicit template
15422 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15423 also match. Return NULL_TREE if no satisfactory arguments could be
15424 found. */
15426 static tree
15427 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15429 int ntparms = DECL_NTPARMS (fn);
15430 tree targs = make_tree_vec (ntparms);
15431 tree decl_type;
15432 tree decl_arg_types;
15433 tree *args;
15434 unsigned int nargs, ix;
15435 tree arg;
15437 /* Substitute the explicit template arguments into the type of DECL.
15438 The call to fn_type_unification will handle substitution into the
15439 FN. */
15440 decl_type = TREE_TYPE (decl);
15441 if (explicit_args && uses_template_parms (decl_type))
15443 tree tmpl;
15444 tree converted_args;
15446 if (DECL_TEMPLATE_INFO (decl))
15447 tmpl = DECL_TI_TEMPLATE (decl);
15448 else
15449 /* We can get here for some invalid specializations. */
15450 return NULL_TREE;
15452 converted_args
15453 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15454 explicit_args, NULL_TREE,
15455 tf_none,
15456 /*require_all_args=*/false,
15457 /*use_default_args=*/false);
15458 if (converted_args == error_mark_node)
15459 return NULL_TREE;
15461 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15462 if (decl_type == error_mark_node)
15463 return NULL_TREE;
15466 /* Never do unification on the 'this' parameter. */
15467 decl_arg_types = skip_artificial_parms_for (decl,
15468 TYPE_ARG_TYPES (decl_type));
15470 nargs = list_length (decl_arg_types);
15471 args = XALLOCAVEC (tree, nargs);
15472 for (arg = decl_arg_types, ix = 0;
15473 arg != NULL_TREE && arg != void_list_node;
15474 arg = TREE_CHAIN (arg), ++ix)
15475 args[ix] = TREE_VALUE (arg);
15477 if (fn_type_unification (fn, explicit_args, targs,
15478 args, ix,
15479 (check_rettype || DECL_CONV_FN_P (fn)
15480 ? TREE_TYPE (decl_type) : NULL_TREE),
15481 DEDUCE_EXACT, LOOKUP_NORMAL))
15482 return NULL_TREE;
15484 return targs;
15487 /* Return the innermost template arguments that, when applied to a
15488 template specialization whose innermost template parameters are
15489 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15490 ARGS.
15492 For example, suppose we have:
15494 template <class T, class U> struct S {};
15495 template <class T> struct S<T*, int> {};
15497 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15498 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15499 int}. The resulting vector will be {double}, indicating that `T'
15500 is bound to `double'. */
15502 static tree
15503 get_class_bindings (tree tparms, tree spec_args, tree args)
15505 int i, ntparms = TREE_VEC_LENGTH (tparms);
15506 tree deduced_args;
15507 tree innermost_deduced_args;
15509 innermost_deduced_args = make_tree_vec (ntparms);
15510 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15512 deduced_args = copy_node (args);
15513 SET_TMPL_ARGS_LEVEL (deduced_args,
15514 TMPL_ARGS_DEPTH (deduced_args),
15515 innermost_deduced_args);
15517 else
15518 deduced_args = innermost_deduced_args;
15520 if (unify (tparms, deduced_args,
15521 INNERMOST_TEMPLATE_ARGS (spec_args),
15522 INNERMOST_TEMPLATE_ARGS (args),
15523 UNIFY_ALLOW_NONE))
15524 return NULL_TREE;
15526 for (i = 0; i < ntparms; ++i)
15527 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15528 return NULL_TREE;
15530 /* Verify that nondeduced template arguments agree with the type
15531 obtained from argument deduction.
15533 For example:
15535 struct A { typedef int X; };
15536 template <class T, class U> struct C {};
15537 template <class T> struct C<T, typename T::X> {};
15539 Then with the instantiation `C<A, int>', we can deduce that
15540 `T' is `A' but unify () does not check whether `typename T::X'
15541 is `int'. */
15542 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15543 if (spec_args == error_mark_node
15544 /* We only need to check the innermost arguments; the other
15545 arguments will always agree. */
15546 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15547 INNERMOST_TEMPLATE_ARGS (args)))
15548 return NULL_TREE;
15550 /* Now that we have bindings for all of the template arguments,
15551 ensure that the arguments deduced for the template template
15552 parameters have compatible template parameter lists. See the use
15553 of template_template_parm_bindings_ok_p in fn_type_unification
15554 for more information. */
15555 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15556 return NULL_TREE;
15558 return deduced_args;
15561 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15562 Return the TREE_LIST node with the most specialized template, if
15563 any. If there is no most specialized template, the error_mark_node
15564 is returned.
15566 Note that this function does not look at, or modify, the
15567 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15568 returned is one of the elements of INSTANTIATIONS, callers may
15569 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15570 and retrieve it from the value returned. */
15572 tree
15573 most_specialized_instantiation (tree templates)
15575 tree fn, champ;
15577 ++processing_template_decl;
15579 champ = templates;
15580 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15582 int fate = 0;
15584 if (get_bindings (TREE_VALUE (champ),
15585 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15586 NULL_TREE, /*check_ret=*/false))
15587 fate--;
15589 if (get_bindings (TREE_VALUE (fn),
15590 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15591 NULL_TREE, /*check_ret=*/false))
15592 fate++;
15594 if (fate == -1)
15595 champ = fn;
15596 else if (!fate)
15598 /* Equally specialized, move to next function. If there
15599 is no next function, nothing's most specialized. */
15600 fn = TREE_CHAIN (fn);
15601 champ = fn;
15602 if (!fn)
15603 break;
15607 if (champ)
15608 /* Now verify that champ is better than everything earlier in the
15609 instantiation list. */
15610 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15611 if (get_bindings (TREE_VALUE (champ),
15612 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15613 NULL_TREE, /*check_ret=*/false)
15614 || !get_bindings (TREE_VALUE (fn),
15615 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15616 NULL_TREE, /*check_ret=*/false))
15618 champ = NULL_TREE;
15619 break;
15622 processing_template_decl--;
15624 if (!champ)
15625 return error_mark_node;
15627 return champ;
15630 /* If DECL is a specialization of some template, return the most
15631 general such template. Otherwise, returns NULL_TREE.
15633 For example, given:
15635 template <class T> struct S { template <class U> void f(U); };
15637 if TMPL is `template <class U> void S<int>::f(U)' this will return
15638 the full template. This function will not trace past partial
15639 specializations, however. For example, given in addition:
15641 template <class T> struct S<T*> { template <class U> void f(U); };
15643 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15644 `template <class T> template <class U> S<T*>::f(U)'. */
15646 tree
15647 most_general_template (tree decl)
15649 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15650 an immediate specialization. */
15651 if (TREE_CODE (decl) == FUNCTION_DECL)
15653 if (DECL_TEMPLATE_INFO (decl)) {
15654 decl = DECL_TI_TEMPLATE (decl);
15656 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15657 template friend. */
15658 if (TREE_CODE (decl) != TEMPLATE_DECL)
15659 return NULL_TREE;
15660 } else
15661 return NULL_TREE;
15664 /* Look for more and more general templates. */
15665 while (DECL_TEMPLATE_INFO (decl))
15667 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15668 (See cp-tree.h for details.) */
15669 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15670 break;
15672 if (CLASS_TYPE_P (TREE_TYPE (decl))
15673 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15674 break;
15676 /* Stop if we run into an explicitly specialized class template. */
15677 if (!DECL_NAMESPACE_SCOPE_P (decl)
15678 && DECL_CONTEXT (decl)
15679 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15680 break;
15682 decl = DECL_TI_TEMPLATE (decl);
15685 return decl;
15688 /* Return the most specialized of the class template partial
15689 specializations of TMPL which can produce TYPE, a specialization of
15690 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15691 a _TYPE node corresponding to the partial specialization, while the
15692 TREE_PURPOSE is the set of template arguments that must be
15693 substituted into the TREE_TYPE in order to generate TYPE.
15695 If the choice of partial specialization is ambiguous, a diagnostic
15696 is issued, and the error_mark_node is returned. If there are no
15697 partial specializations of TMPL matching TYPE, then NULL_TREE is
15698 returned. */
15700 static tree
15701 most_specialized_class (tree type, tree tmpl)
15703 tree list = NULL_TREE;
15704 tree t;
15705 tree champ;
15706 int fate;
15707 bool ambiguous_p;
15708 tree args;
15709 tree outer_args = NULL_TREE;
15711 tmpl = most_general_template (tmpl);
15712 args = CLASSTYPE_TI_ARGS (type);
15714 /* For determining which partial specialization to use, only the
15715 innermost args are interesting. */
15716 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15718 outer_args = strip_innermost_template_args (args, 1);
15719 args = INNERMOST_TEMPLATE_ARGS (args);
15722 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15724 tree partial_spec_args;
15725 tree spec_args;
15726 tree parms = TREE_VALUE (t);
15728 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15729 if (outer_args)
15731 int i;
15733 ++processing_template_decl;
15735 /* Discard the outer levels of args, and then substitute in the
15736 template args from the enclosing class. */
15737 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15738 partial_spec_args = tsubst_template_args
15739 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15741 /* PARMS already refers to just the innermost parms, but the
15742 template parms in partial_spec_args had their levels lowered
15743 by tsubst, so we need to do the same for the parm list. We
15744 can't just tsubst the TREE_VEC itself, as tsubst wants to
15745 treat a TREE_VEC as an argument vector. */
15746 parms = copy_node (parms);
15747 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15748 TREE_VEC_ELT (parms, i) =
15749 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15751 --processing_template_decl;
15753 spec_args = get_class_bindings (parms,
15754 partial_spec_args,
15755 args);
15756 if (spec_args)
15758 if (outer_args)
15759 spec_args = add_to_template_args (outer_args, spec_args);
15760 list = tree_cons (spec_args, TREE_VALUE (t), list);
15761 TREE_TYPE (list) = TREE_TYPE (t);
15765 if (! list)
15766 return NULL_TREE;
15768 ambiguous_p = false;
15769 t = list;
15770 champ = t;
15771 t = TREE_CHAIN (t);
15772 for (; t; t = TREE_CHAIN (t))
15774 fate = more_specialized_class (champ, t);
15775 if (fate == 1)
15777 else
15779 if (fate == 0)
15781 t = TREE_CHAIN (t);
15782 if (! t)
15784 ambiguous_p = true;
15785 break;
15788 champ = t;
15792 if (!ambiguous_p)
15793 for (t = list; t && t != champ; t = TREE_CHAIN (t))
15795 fate = more_specialized_class (champ, t);
15796 if (fate != 1)
15798 ambiguous_p = true;
15799 break;
15803 if (ambiguous_p)
15805 const char *str;
15806 char *spaces = NULL;
15807 error ("ambiguous class template instantiation for %q#T", type);
15808 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
15809 for (t = list; t; t = TREE_CHAIN (t))
15811 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
15812 spaces = spaces ? spaces : get_spaces (str);
15814 free (spaces);
15815 return error_mark_node;
15818 return champ;
15821 /* Explicitly instantiate DECL. */
15823 void
15824 do_decl_instantiation (tree decl, tree storage)
15826 tree result = NULL_TREE;
15827 int extern_p = 0;
15829 if (!decl || decl == error_mark_node)
15830 /* An error occurred, for which grokdeclarator has already issued
15831 an appropriate message. */
15832 return;
15833 else if (! DECL_LANG_SPECIFIC (decl))
15835 error ("explicit instantiation of non-template %q#D", decl);
15836 return;
15838 else if (TREE_CODE (decl) == VAR_DECL)
15840 /* There is an asymmetry here in the way VAR_DECLs and
15841 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15842 the latter, the DECL we get back will be marked as a
15843 template instantiation, and the appropriate
15844 DECL_TEMPLATE_INFO will be set up. This does not happen for
15845 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15846 should handle VAR_DECLs as it currently handles
15847 FUNCTION_DECLs. */
15848 if (!DECL_CLASS_SCOPE_P (decl))
15850 error ("%qD is not a static data member of a class template", decl);
15851 return;
15853 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
15854 if (!result || TREE_CODE (result) != VAR_DECL)
15856 error ("no matching template for %qD found", decl);
15857 return;
15859 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15861 error ("type %qT for explicit instantiation %qD does not match "
15862 "declared type %qT", TREE_TYPE (result), decl,
15863 TREE_TYPE (decl));
15864 return;
15867 else if (TREE_CODE (decl) != FUNCTION_DECL)
15869 error ("explicit instantiation of %q#D", decl);
15870 return;
15872 else
15873 result = decl;
15875 /* Check for various error cases. Note that if the explicit
15876 instantiation is valid the RESULT will currently be marked as an
15877 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15878 until we get here. */
15880 if (DECL_TEMPLATE_SPECIALIZATION (result))
15882 /* DR 259 [temp.spec].
15884 Both an explicit instantiation and a declaration of an explicit
15885 specialization shall not appear in a program unless the explicit
15886 instantiation follows a declaration of the explicit specialization.
15888 For a given set of template parameters, if an explicit
15889 instantiation of a template appears after a declaration of an
15890 explicit specialization for that template, the explicit
15891 instantiation has no effect. */
15892 return;
15894 else if (DECL_EXPLICIT_INSTANTIATION (result))
15896 /* [temp.spec]
15898 No program shall explicitly instantiate any template more
15899 than once.
15901 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15902 the first instantiation was `extern' and the second is not,
15903 and EXTERN_P for the opposite case. */
15904 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
15905 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
15906 /* If an "extern" explicit instantiation follows an ordinary
15907 explicit instantiation, the template is instantiated. */
15908 if (extern_p)
15909 return;
15911 else if (!DECL_IMPLICIT_INSTANTIATION (result))
15913 error ("no matching template for %qD found", result);
15914 return;
15916 else if (!DECL_TEMPLATE_INFO (result))
15918 permerror (input_location, "explicit instantiation of non-template %q#D", result);
15919 return;
15922 if (storage == NULL_TREE)
15924 else if (storage == ridpointers[(int) RID_EXTERN])
15926 if (!in_system_header && (cxx_dialect == cxx98))
15927 pedwarn (input_location, OPT_pedantic,
15928 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15929 "instantiations");
15930 extern_p = 1;
15932 else
15933 error ("storage class %qD applied to template instantiation", storage);
15935 check_explicit_instantiation_namespace (result);
15936 mark_decl_instantiated (result, extern_p);
15937 if (! extern_p)
15938 instantiate_decl (result, /*defer_ok=*/1,
15939 /*expl_inst_class_mem_p=*/false);
15942 static void
15943 mark_class_instantiated (tree t, int extern_p)
15945 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
15946 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15947 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
15948 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15949 if (! extern_p)
15951 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15952 rest_of_type_compilation (t, 1);
15956 /* Called from do_type_instantiation through binding_table_foreach to
15957 do recursive instantiation for the type bound in ENTRY. */
15958 static void
15959 bt_instantiate_type_proc (binding_entry entry, void *data)
15961 tree storage = *(tree *) data;
15963 if (MAYBE_CLASS_TYPE_P (entry->type)
15964 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15965 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15968 /* Called from do_type_instantiation to instantiate a member
15969 (a member function or a static member variable) of an
15970 explicitly instantiated class template. */
15971 static void
15972 instantiate_class_member (tree decl, int extern_p)
15974 mark_decl_instantiated (decl, extern_p);
15975 if (! extern_p)
15976 instantiate_decl (decl, /*defer_ok=*/1,
15977 /*expl_inst_class_mem_p=*/true);
15980 /* Perform an explicit instantiation of template class T. STORAGE, if
15981 non-null, is the RID for extern, inline or static. COMPLAIN is
15982 nonzero if this is called from the parser, zero if called recursively,
15983 since the standard is unclear (as detailed below). */
15985 void
15986 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15988 int extern_p = 0;
15989 int nomem_p = 0;
15990 int static_p = 0;
15991 int previous_instantiation_extern_p = 0;
15993 if (TREE_CODE (t) == TYPE_DECL)
15994 t = TREE_TYPE (t);
15996 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15998 error ("explicit instantiation of non-template type %qT", t);
15999 return;
16002 complete_type (t);
16004 if (!COMPLETE_TYPE_P (t))
16006 if (complain & tf_error)
16007 error ("explicit instantiation of %q#T before definition of template",
16009 return;
16012 if (storage != NULL_TREE)
16014 if (!in_system_header)
16016 if (storage == ridpointers[(int) RID_EXTERN])
16018 if (cxx_dialect == cxx98)
16019 pedwarn (input_location, OPT_pedantic,
16020 "ISO C++ 1998 forbids the use of %<extern%> on "
16021 "explicit instantiations");
16023 else
16024 pedwarn (input_location, OPT_pedantic,
16025 "ISO C++ forbids the use of %qE"
16026 " on explicit instantiations", storage);
16029 if (storage == ridpointers[(int) RID_INLINE])
16030 nomem_p = 1;
16031 else if (storage == ridpointers[(int) RID_EXTERN])
16032 extern_p = 1;
16033 else if (storage == ridpointers[(int) RID_STATIC])
16034 static_p = 1;
16035 else
16037 error ("storage class %qD applied to template instantiation",
16038 storage);
16039 extern_p = 0;
16043 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16045 /* DR 259 [temp.spec].
16047 Both an explicit instantiation and a declaration of an explicit
16048 specialization shall not appear in a program unless the explicit
16049 instantiation follows a declaration of the explicit specialization.
16051 For a given set of template parameters, if an explicit
16052 instantiation of a template appears after a declaration of an
16053 explicit specialization for that template, the explicit
16054 instantiation has no effect. */
16055 return;
16057 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16059 /* [temp.spec]
16061 No program shall explicitly instantiate any template more
16062 than once.
16064 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16065 instantiation was `extern'. If EXTERN_P then the second is.
16066 These cases are OK. */
16067 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16069 if (!previous_instantiation_extern_p && !extern_p
16070 && (complain & tf_error))
16071 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16073 /* If we've already instantiated the template, just return now. */
16074 if (!CLASSTYPE_INTERFACE_ONLY (t))
16075 return;
16078 check_explicit_instantiation_namespace (TYPE_NAME (t));
16079 mark_class_instantiated (t, extern_p);
16081 if (nomem_p)
16082 return;
16085 tree tmp;
16087 /* In contrast to implicit instantiation, where only the
16088 declarations, and not the definitions, of members are
16089 instantiated, we have here:
16091 [temp.explicit]
16093 The explicit instantiation of a class template specialization
16094 implies the instantiation of all of its members not
16095 previously explicitly specialized in the translation unit
16096 containing the explicit instantiation.
16098 Of course, we can't instantiate member template classes, since
16099 we don't have any arguments for them. Note that the standard
16100 is unclear on whether the instantiation of the members are
16101 *explicit* instantiations or not. However, the most natural
16102 interpretation is that it should be an explicit instantiation. */
16104 if (! static_p)
16105 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
16106 if (TREE_CODE (tmp) == FUNCTION_DECL
16107 && DECL_TEMPLATE_INSTANTIATION (tmp))
16108 instantiate_class_member (tmp, extern_p);
16110 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
16111 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16112 instantiate_class_member (tmp, extern_p);
16114 if (CLASSTYPE_NESTED_UTDS (t))
16115 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16116 bt_instantiate_type_proc, &storage);
16120 /* Given a function DECL, which is a specialization of TMPL, modify
16121 DECL to be a re-instantiation of TMPL with the same template
16122 arguments. TMPL should be the template into which tsubst'ing
16123 should occur for DECL, not the most general template.
16125 One reason for doing this is a scenario like this:
16127 template <class T>
16128 void f(const T&, int i);
16130 void g() { f(3, 7); }
16132 template <class T>
16133 void f(const T& t, const int i) { }
16135 Note that when the template is first instantiated, with
16136 instantiate_template, the resulting DECL will have no name for the
16137 first parameter, and the wrong type for the second. So, when we go
16138 to instantiate the DECL, we regenerate it. */
16140 static void
16141 regenerate_decl_from_template (tree decl, tree tmpl)
16143 /* The arguments used to instantiate DECL, from the most general
16144 template. */
16145 tree args;
16146 tree code_pattern;
16148 args = DECL_TI_ARGS (decl);
16149 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16151 /* Make sure that we can see identifiers, and compute access
16152 correctly. */
16153 push_access_scope (decl);
16155 if (TREE_CODE (decl) == FUNCTION_DECL)
16157 tree decl_parm;
16158 tree pattern_parm;
16159 tree specs;
16160 int args_depth;
16161 int parms_depth;
16163 args_depth = TMPL_ARGS_DEPTH (args);
16164 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16165 if (args_depth > parms_depth)
16166 args = get_innermost_template_args (args, parms_depth);
16168 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16169 args, tf_error, NULL_TREE);
16170 if (specs)
16171 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16172 specs);
16174 /* Merge parameter declarations. */
16175 decl_parm = skip_artificial_parms_for (decl,
16176 DECL_ARGUMENTS (decl));
16177 pattern_parm
16178 = skip_artificial_parms_for (code_pattern,
16179 DECL_ARGUMENTS (code_pattern));
16180 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16182 tree parm_type;
16183 tree attributes;
16185 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16186 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16187 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16188 NULL_TREE);
16189 parm_type = type_decays_to (parm_type);
16190 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16191 TREE_TYPE (decl_parm) = parm_type;
16192 attributes = DECL_ATTRIBUTES (pattern_parm);
16193 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16195 DECL_ATTRIBUTES (decl_parm) = attributes;
16196 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16198 decl_parm = TREE_CHAIN (decl_parm);
16199 pattern_parm = TREE_CHAIN (pattern_parm);
16201 /* Merge any parameters that match with the function parameter
16202 pack. */
16203 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16205 int i, len;
16206 tree expanded_types;
16207 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16208 the parameters in this function parameter pack. */
16209 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16210 args, tf_error, NULL_TREE);
16211 len = TREE_VEC_LENGTH (expanded_types);
16212 for (i = 0; i < len; i++)
16214 tree parm_type;
16215 tree attributes;
16217 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16218 /* Rename the parameter to include the index. */
16219 DECL_NAME (decl_parm) =
16220 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16221 parm_type = TREE_VEC_ELT (expanded_types, i);
16222 parm_type = type_decays_to (parm_type);
16223 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16224 TREE_TYPE (decl_parm) = parm_type;
16225 attributes = DECL_ATTRIBUTES (pattern_parm);
16226 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16228 DECL_ATTRIBUTES (decl_parm) = attributes;
16229 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16231 decl_parm = TREE_CHAIN (decl_parm);
16234 /* Merge additional specifiers from the CODE_PATTERN. */
16235 if (DECL_DECLARED_INLINE_P (code_pattern)
16236 && !DECL_DECLARED_INLINE_P (decl))
16237 DECL_DECLARED_INLINE_P (decl) = 1;
16239 else if (TREE_CODE (decl) == VAR_DECL)
16240 DECL_INITIAL (decl) =
16241 tsubst_expr (DECL_INITIAL (code_pattern), args,
16242 tf_error, DECL_TI_TEMPLATE (decl),
16243 /*integral_constant_expression_p=*/false);
16244 else
16245 gcc_unreachable ();
16247 pop_access_scope (decl);
16250 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16251 substituted to get DECL. */
16253 tree
16254 template_for_substitution (tree decl)
16256 tree tmpl = DECL_TI_TEMPLATE (decl);
16258 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16259 for the instantiation. This is not always the most general
16260 template. Consider, for example:
16262 template <class T>
16263 struct S { template <class U> void f();
16264 template <> void f<int>(); };
16266 and an instantiation of S<double>::f<int>. We want TD to be the
16267 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16268 while (/* An instantiation cannot have a definition, so we need a
16269 more general template. */
16270 DECL_TEMPLATE_INSTANTIATION (tmpl)
16271 /* We must also deal with friend templates. Given:
16273 template <class T> struct S {
16274 template <class U> friend void f() {};
16277 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16278 so far as the language is concerned, but that's still
16279 where we get the pattern for the instantiation from. On
16280 other hand, if the definition comes outside the class, say:
16282 template <class T> struct S {
16283 template <class U> friend void f();
16285 template <class U> friend void f() {}
16287 we don't need to look any further. That's what the check for
16288 DECL_INITIAL is for. */
16289 || (TREE_CODE (decl) == FUNCTION_DECL
16290 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16291 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16293 /* The present template, TD, should not be a definition. If it
16294 were a definition, we should be using it! Note that we
16295 cannot restructure the loop to just keep going until we find
16296 a template with a definition, since that might go too far if
16297 a specialization was declared, but not defined. */
16298 gcc_assert (TREE_CODE (decl) != VAR_DECL
16299 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16301 /* Fetch the more general template. */
16302 tmpl = DECL_TI_TEMPLATE (tmpl);
16305 return tmpl;
16308 /* Returns true if we need to instantiate this template instance even if we
16309 know we aren't going to emit it.. */
16311 bool
16312 always_instantiate_p (tree decl)
16314 /* We always instantiate inline functions so that we can inline them. An
16315 explicit instantiation declaration prohibits implicit instantiation of
16316 non-inline functions. With high levels of optimization, we would
16317 normally inline non-inline functions -- but we're not allowed to do
16318 that for "extern template" functions. Therefore, we check
16319 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16320 return ((TREE_CODE (decl) == FUNCTION_DECL
16321 && DECL_DECLARED_INLINE_P (decl))
16322 /* And we need to instantiate static data members so that
16323 their initializers are available in integral constant
16324 expressions. */
16325 || (TREE_CODE (decl) == VAR_DECL
16326 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16329 /* Produce the definition of D, a _DECL generated from a template. If
16330 DEFER_OK is nonzero, then we don't have to actually do the
16331 instantiation now; we just have to do it sometime. Normally it is
16332 an error if this is an explicit instantiation but D is undefined.
16333 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16334 explicitly instantiated class template. */
16336 tree
16337 instantiate_decl (tree d, int defer_ok,
16338 bool expl_inst_class_mem_p)
16340 tree tmpl = DECL_TI_TEMPLATE (d);
16341 tree gen_args;
16342 tree args;
16343 tree td;
16344 tree code_pattern;
16345 tree spec;
16346 tree gen_tmpl;
16347 bool pattern_defined;
16348 int need_push;
16349 location_t saved_loc = input_location;
16350 bool external_p;
16352 /* This function should only be used to instantiate templates for
16353 functions and static member variables. */
16354 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16355 || TREE_CODE (d) == VAR_DECL);
16357 /* Variables are never deferred; if instantiation is required, they
16358 are instantiated right away. That allows for better code in the
16359 case that an expression refers to the value of the variable --
16360 if the variable has a constant value the referring expression can
16361 take advantage of that fact. */
16362 if (TREE_CODE (d) == VAR_DECL)
16363 defer_ok = 0;
16365 /* Don't instantiate cloned functions. Instead, instantiate the
16366 functions they cloned. */
16367 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16368 d = DECL_CLONED_FUNCTION (d);
16370 if (DECL_TEMPLATE_INSTANTIATED (d)
16371 || DECL_TEMPLATE_SPECIALIZATION (d))
16372 /* D has already been instantiated or explicitly specialized, so
16373 there's nothing for us to do here.
16375 It might seem reasonable to check whether or not D is an explicit
16376 instantiation, and, if so, stop here. But when an explicit
16377 instantiation is deferred until the end of the compilation,
16378 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16379 the instantiation. */
16380 return d;
16382 /* Check to see whether we know that this template will be
16383 instantiated in some other file, as with "extern template"
16384 extension. */
16385 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16387 /* In general, we do not instantiate such templates. */
16388 if (external_p && !always_instantiate_p (d))
16389 return d;
16391 gen_tmpl = most_general_template (tmpl);
16392 gen_args = DECL_TI_ARGS (d);
16394 if (tmpl != gen_tmpl)
16395 /* We should already have the extra args. */
16396 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16397 == TMPL_ARGS_DEPTH (gen_args));
16398 /* And what's in the hash table should match D. */
16399 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16400 || spec == NULL_TREE);
16402 /* This needs to happen before any tsubsting. */
16403 if (! push_tinst_level (d))
16404 return d;
16406 timevar_push (TV_PARSE);
16408 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16409 for the instantiation. */
16410 td = template_for_substitution (d);
16411 code_pattern = DECL_TEMPLATE_RESULT (td);
16413 /* We should never be trying to instantiate a member of a class
16414 template or partial specialization. */
16415 gcc_assert (d != code_pattern);
16417 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16418 || DECL_TEMPLATE_SPECIALIZATION (td))
16419 /* In the case of a friend template whose definition is provided
16420 outside the class, we may have too many arguments. Drop the
16421 ones we don't need. The same is true for specializations. */
16422 args = get_innermost_template_args
16423 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16424 else
16425 args = gen_args;
16427 if (TREE_CODE (d) == FUNCTION_DECL)
16428 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16429 else
16430 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16432 /* We may be in the middle of deferred access check. Disable it now. */
16433 push_deferring_access_checks (dk_no_deferred);
16435 /* Unless an explicit instantiation directive has already determined
16436 the linkage of D, remember that a definition is available for
16437 this entity. */
16438 if (pattern_defined
16439 && !DECL_INTERFACE_KNOWN (d)
16440 && !DECL_NOT_REALLY_EXTERN (d))
16441 mark_definable (d);
16443 input_location = DECL_SOURCE_LOCATION (d);
16445 /* If D is a member of an explicitly instantiated class template,
16446 and no definition is available, treat it like an implicit
16447 instantiation. */
16448 if (!pattern_defined && expl_inst_class_mem_p
16449 && DECL_EXPLICIT_INSTANTIATION (d))
16451 DECL_NOT_REALLY_EXTERN (d) = 0;
16452 DECL_INTERFACE_KNOWN (d) = 0;
16453 SET_DECL_IMPLICIT_INSTANTIATION (d);
16456 /* Recheck the substitutions to obtain any warning messages
16457 about ignoring cv qualifiers. Don't do this for artificial decls,
16458 as it breaks the context-sensitive substitution for lambda op(). */
16459 if (!defer_ok && !DECL_ARTIFICIAL (d))
16461 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16462 tree type = TREE_TYPE (gen);
16464 /* Make sure that we can see identifiers, and compute access
16465 correctly. D is already the target FUNCTION_DECL with the
16466 right context. */
16467 push_access_scope (d);
16469 if (TREE_CODE (gen) == FUNCTION_DECL)
16471 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16472 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16474 /* Don't simply tsubst the function type, as that will give
16475 duplicate warnings about poor parameter qualifications.
16476 The function arguments are the same as the decl_arguments
16477 without the top level cv qualifiers. */
16478 type = TREE_TYPE (type);
16480 tsubst (type, gen_args, tf_warning_or_error, d);
16482 pop_access_scope (d);
16485 /* Defer all other templates, unless we have been explicitly
16486 forbidden from doing so. */
16487 if (/* If there is no definition, we cannot instantiate the
16488 template. */
16489 ! pattern_defined
16490 /* If it's OK to postpone instantiation, do so. */
16491 || defer_ok
16492 /* If this is a static data member that will be defined
16493 elsewhere, we don't want to instantiate the entire data
16494 member, but we do want to instantiate the initializer so that
16495 we can substitute that elsewhere. */
16496 || (external_p && TREE_CODE (d) == VAR_DECL))
16498 /* The definition of the static data member is now required so
16499 we must substitute the initializer. */
16500 if (TREE_CODE (d) == VAR_DECL
16501 && !DECL_INITIAL (d)
16502 && DECL_INITIAL (code_pattern))
16504 tree ns;
16505 tree init;
16507 ns = decl_namespace_context (d);
16508 push_nested_namespace (ns);
16509 push_nested_class (DECL_CONTEXT (d));
16510 init = tsubst_expr (DECL_INITIAL (code_pattern),
16511 args,
16512 tf_warning_or_error, NULL_TREE,
16513 /*integral_constant_expression_p=*/false);
16514 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16515 /*asmspec_tree=*/NULL_TREE,
16516 LOOKUP_ONLYCONVERTING);
16517 pop_nested_class ();
16518 pop_nested_namespace (ns);
16521 /* We restore the source position here because it's used by
16522 add_pending_template. */
16523 input_location = saved_loc;
16525 if (at_eof && !pattern_defined
16526 && DECL_EXPLICIT_INSTANTIATION (d)
16527 && DECL_NOT_REALLY_EXTERN (d))
16528 /* [temp.explicit]
16530 The definition of a non-exported function template, a
16531 non-exported member function template, or a non-exported
16532 member function or static data member of a class template
16533 shall be present in every translation unit in which it is
16534 explicitly instantiated. */
16535 permerror (input_location, "explicit instantiation of %qD "
16536 "but no definition available", d);
16538 /* ??? Historically, we have instantiated inline functions, even
16539 when marked as "extern template". */
16540 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16541 add_pending_template (d);
16542 goto out;
16544 /* Tell the repository that D is available in this translation unit
16545 -- and see if it is supposed to be instantiated here. */
16546 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16548 /* In a PCH file, despite the fact that the repository hasn't
16549 requested instantiation in the PCH it is still possible that
16550 an instantiation will be required in a file that includes the
16551 PCH. */
16552 if (pch_file)
16553 add_pending_template (d);
16554 /* Instantiate inline functions so that the inliner can do its
16555 job, even though we'll not be emitting a copy of this
16556 function. */
16557 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16558 goto out;
16561 need_push = !cfun || !global_bindings_p ();
16562 if (need_push)
16563 push_to_top_level ();
16565 /* Mark D as instantiated so that recursive calls to
16566 instantiate_decl do not try to instantiate it again. */
16567 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16569 /* Regenerate the declaration in case the template has been modified
16570 by a subsequent redeclaration. */
16571 regenerate_decl_from_template (d, td);
16573 /* We already set the file and line above. Reset them now in case
16574 they changed as a result of calling regenerate_decl_from_template. */
16575 input_location = DECL_SOURCE_LOCATION (d);
16577 if (TREE_CODE (d) == VAR_DECL)
16579 tree init;
16581 /* Clear out DECL_RTL; whatever was there before may not be right
16582 since we've reset the type of the declaration. */
16583 SET_DECL_RTL (d, NULL_RTX);
16584 DECL_IN_AGGR_P (d) = 0;
16586 /* The initializer is placed in DECL_INITIAL by
16587 regenerate_decl_from_template. Pull it out so that
16588 cp_finish_decl can process it. */
16589 init = DECL_INITIAL (d);
16590 DECL_INITIAL (d) = NULL_TREE;
16591 DECL_INITIALIZED_P (d) = 0;
16593 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16594 initializer. That function will defer actual emission until
16595 we have a chance to determine linkage. */
16596 DECL_EXTERNAL (d) = 0;
16598 /* Enter the scope of D so that access-checking works correctly. */
16599 push_nested_class (DECL_CONTEXT (d));
16600 cp_finish_decl (d, init, false, NULL_TREE, 0);
16601 pop_nested_class ();
16603 else if (TREE_CODE (d) == FUNCTION_DECL)
16605 htab_t saved_local_specializations;
16606 tree subst_decl;
16607 tree tmpl_parm;
16608 tree spec_parm;
16610 /* Save away the current list, in case we are instantiating one
16611 template from within the body of another. */
16612 saved_local_specializations = local_specializations;
16614 /* Set up the list of local specializations. */
16615 local_specializations = htab_create (37,
16616 hash_local_specialization,
16617 eq_local_specializations,
16618 NULL);
16620 /* Set up context. */
16621 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16623 /* Create substitution entries for the parameters. */
16624 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16625 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16626 spec_parm = DECL_ARGUMENTS (d);
16627 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16629 register_local_specialization (spec_parm, tmpl_parm);
16630 spec_parm = skip_artificial_parms_for (d, spec_parm);
16631 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16633 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16635 register_local_specialization (spec_parm, tmpl_parm);
16636 tmpl_parm = TREE_CHAIN (tmpl_parm);
16637 spec_parm = TREE_CHAIN (spec_parm);
16639 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16641 /* Register the (value) argument pack as a specialization of
16642 TMPL_PARM, then move on. */
16643 tree argpack = make_fnparm_pack (spec_parm);
16644 register_local_specialization (argpack, tmpl_parm);
16645 tmpl_parm = TREE_CHAIN (tmpl_parm);
16646 spec_parm = NULL_TREE;
16648 gcc_assert (!spec_parm);
16650 /* Substitute into the body of the function. */
16651 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16652 tf_warning_or_error, tmpl,
16653 /*integral_constant_expression_p=*/false);
16655 /* Set the current input_location to the end of the function
16656 so that finish_function knows where we are. */
16657 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16659 /* We don't need the local specializations any more. */
16660 htab_delete (local_specializations);
16661 local_specializations = saved_local_specializations;
16663 /* Finish the function. */
16664 d = finish_function (0);
16665 expand_or_defer_fn (d);
16668 /* We're not deferring instantiation any more. */
16669 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16671 if (need_push)
16672 pop_from_top_level ();
16674 out:
16675 input_location = saved_loc;
16676 pop_deferring_access_checks ();
16677 pop_tinst_level ();
16679 timevar_pop (TV_PARSE);
16681 return d;
16684 /* Run through the list of templates that we wish we could
16685 instantiate, and instantiate any we can. RETRIES is the
16686 number of times we retry pending template instantiation. */
16688 void
16689 instantiate_pending_templates (int retries)
16691 int reconsider;
16692 location_t saved_loc = input_location;
16694 /* Instantiating templates may trigger vtable generation. This in turn
16695 may require further template instantiations. We place a limit here
16696 to avoid infinite loop. */
16697 if (pending_templates && retries >= max_tinst_depth)
16699 tree decl = pending_templates->tinst->decl;
16701 error ("template instantiation depth exceeds maximum of %d"
16702 " instantiating %q+D, possibly from virtual table generation"
16703 " (use -ftemplate-depth-NN to increase the maximum)",
16704 max_tinst_depth, decl);
16705 if (TREE_CODE (decl) == FUNCTION_DECL)
16706 /* Pretend that we defined it. */
16707 DECL_INITIAL (decl) = error_mark_node;
16708 return;
16713 struct pending_template **t = &pending_templates;
16714 struct pending_template *last = NULL;
16715 reconsider = 0;
16716 while (*t)
16718 tree instantiation = reopen_tinst_level ((*t)->tinst);
16719 bool complete = false;
16721 if (TYPE_P (instantiation))
16723 tree fn;
16725 if (!COMPLETE_TYPE_P (instantiation))
16727 instantiate_class_template (instantiation);
16728 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16729 for (fn = TYPE_METHODS (instantiation);
16731 fn = TREE_CHAIN (fn))
16732 if (! DECL_ARTIFICIAL (fn))
16733 instantiate_decl (fn,
16734 /*defer_ok=*/0,
16735 /*expl_inst_class_mem_p=*/false);
16736 if (COMPLETE_TYPE_P (instantiation))
16737 reconsider = 1;
16740 complete = COMPLETE_TYPE_P (instantiation);
16742 else
16744 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16745 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16747 instantiation
16748 = instantiate_decl (instantiation,
16749 /*defer_ok=*/0,
16750 /*expl_inst_class_mem_p=*/false);
16751 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16752 reconsider = 1;
16755 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16756 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16759 if (complete)
16760 /* If INSTANTIATION has been instantiated, then we don't
16761 need to consider it again in the future. */
16762 *t = (*t)->next;
16763 else
16765 last = *t;
16766 t = &(*t)->next;
16768 tinst_depth = 0;
16769 current_tinst_level = NULL;
16771 last_pending_template = last;
16773 while (reconsider);
16775 input_location = saved_loc;
16778 /* Substitute ARGVEC into T, which is a list of initializers for
16779 either base class or a non-static data member. The TREE_PURPOSEs
16780 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16781 instantiate_decl. */
16783 static tree
16784 tsubst_initializer_list (tree t, tree argvec)
16786 tree inits = NULL_TREE;
16788 for (; t; t = TREE_CHAIN (t))
16790 tree decl;
16791 tree init;
16792 tree expanded_bases = NULL_TREE;
16793 tree expanded_arguments = NULL_TREE;
16794 int i, len = 1;
16796 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
16798 tree expr;
16799 tree arg;
16801 /* Expand the base class expansion type into separate base
16802 classes. */
16803 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
16804 tf_warning_or_error,
16805 NULL_TREE);
16806 if (expanded_bases == error_mark_node)
16807 continue;
16809 /* We'll be building separate TREE_LISTs of arguments for
16810 each base. */
16811 len = TREE_VEC_LENGTH (expanded_bases);
16812 expanded_arguments = make_tree_vec (len);
16813 for (i = 0; i < len; i++)
16814 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
16816 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16817 expand each argument in the TREE_VALUE of t. */
16818 expr = make_node (EXPR_PACK_EXPANSION);
16819 PACK_EXPANSION_PARAMETER_PACKS (expr) =
16820 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
16822 if (TREE_VALUE (t) == void_type_node)
16823 /* VOID_TYPE_NODE is used to indicate
16824 value-initialization. */
16826 for (i = 0; i < len; i++)
16827 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
16829 else
16831 /* Substitute parameter packs into each argument in the
16832 TREE_LIST. */
16833 in_base_initializer = 1;
16834 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
16836 tree expanded_exprs;
16838 /* Expand the argument. */
16839 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
16840 expanded_exprs
16841 = tsubst_pack_expansion (expr, argvec,
16842 tf_warning_or_error,
16843 NULL_TREE);
16844 if (expanded_exprs == error_mark_node)
16845 continue;
16847 /* Prepend each of the expanded expressions to the
16848 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16849 for (i = 0; i < len; i++)
16851 TREE_VEC_ELT (expanded_arguments, i) =
16852 tree_cons (NULL_TREE,
16853 TREE_VEC_ELT (expanded_exprs, i),
16854 TREE_VEC_ELT (expanded_arguments, i));
16857 in_base_initializer = 0;
16859 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16860 since we built them backwards. */
16861 for (i = 0; i < len; i++)
16863 TREE_VEC_ELT (expanded_arguments, i) =
16864 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16869 for (i = 0; i < len; ++i)
16871 if (expanded_bases)
16873 decl = TREE_VEC_ELT (expanded_bases, i);
16874 decl = expand_member_init (decl);
16875 init = TREE_VEC_ELT (expanded_arguments, i);
16877 else
16879 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16880 tf_warning_or_error, NULL_TREE);
16882 decl = expand_member_init (decl);
16883 if (decl && !DECL_P (decl))
16884 in_base_initializer = 1;
16886 init = tsubst_expr (TREE_VALUE (t), argvec,
16887 tf_warning_or_error, NULL_TREE,
16888 /*integral_constant_expression_p=*/false);
16889 in_base_initializer = 0;
16892 if (decl)
16894 init = build_tree_list (decl, init);
16895 TREE_CHAIN (init) = inits;
16896 inits = init;
16900 return inits;
16903 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16905 static void
16906 set_current_access_from_decl (tree decl)
16908 if (TREE_PRIVATE (decl))
16909 current_access_specifier = access_private_node;
16910 else if (TREE_PROTECTED (decl))
16911 current_access_specifier = access_protected_node;
16912 else
16913 current_access_specifier = access_public_node;
16916 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16917 is the instantiation (which should have been created with
16918 start_enum) and ARGS are the template arguments to use. */
16920 static void
16921 tsubst_enum (tree tag, tree newtag, tree args)
16923 tree e;
16925 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16927 tree value;
16928 tree decl;
16930 decl = TREE_VALUE (e);
16931 /* Note that in a template enum, the TREE_VALUE is the
16932 CONST_DECL, not the corresponding INTEGER_CST. */
16933 value = tsubst_expr (DECL_INITIAL (decl),
16934 args, tf_warning_or_error, NULL_TREE,
16935 /*integral_constant_expression_p=*/true);
16937 /* Give this enumeration constant the correct access. */
16938 set_current_access_from_decl (decl);
16940 /* Actually build the enumerator itself. */
16941 build_enumerator (DECL_NAME (decl), value, newtag);
16944 finish_enum (newtag);
16945 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16946 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
16949 /* DECL is a FUNCTION_DECL that is a template specialization. Return
16950 its type -- but without substituting the innermost set of template
16951 arguments. So, innermost set of template parameters will appear in
16952 the type. */
16954 tree
16955 get_mostly_instantiated_function_type (tree decl)
16957 tree fn_type;
16958 tree tmpl;
16959 tree targs;
16960 tree tparms;
16961 int parm_depth;
16963 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16964 targs = DECL_TI_ARGS (decl);
16965 tparms = DECL_TEMPLATE_PARMS (tmpl);
16966 parm_depth = TMPL_PARMS_DEPTH (tparms);
16968 /* There should be as many levels of arguments as there are levels
16969 of parameters. */
16970 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
16972 fn_type = TREE_TYPE (tmpl);
16974 if (parm_depth == 1)
16975 /* No substitution is necessary. */
16977 else
16979 int i, save_access_control;
16980 tree partial_args;
16982 /* Replace the innermost level of the TARGS with NULL_TREEs to
16983 let tsubst know not to substitute for those parameters. */
16984 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16985 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16986 SET_TMPL_ARGS_LEVEL (partial_args, i,
16987 TMPL_ARGS_LEVEL (targs, i));
16988 SET_TMPL_ARGS_LEVEL (partial_args,
16989 TMPL_ARGS_DEPTH (targs),
16990 make_tree_vec (DECL_NTPARMS (tmpl)));
16992 /* Disable access control as this function is used only during
16993 name-mangling. */
16994 save_access_control = flag_access_control;
16995 flag_access_control = 0;
16997 ++processing_template_decl;
16998 /* Now, do the (partial) substitution to figure out the
16999 appropriate function type. */
17000 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17001 --processing_template_decl;
17003 /* Substitute into the template parameters to obtain the real
17004 innermost set of parameters. This step is important if the
17005 innermost set of template parameters contains value
17006 parameters whose types depend on outer template parameters. */
17007 TREE_VEC_LENGTH (partial_args)--;
17008 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17010 flag_access_control = save_access_control;
17013 return fn_type;
17016 /* Return truthvalue if we're processing a template different from
17017 the last one involved in diagnostics. */
17019 problematic_instantiation_changed (void)
17021 return last_template_error_tick != tinst_level_tick;
17024 /* Remember current template involved in diagnostics. */
17025 void
17026 record_last_problematic_instantiation (void)
17028 last_template_error_tick = tinst_level_tick;
17031 struct tinst_level *
17032 current_instantiation (void)
17034 return current_tinst_level;
17037 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17038 type. Return zero for ok, nonzero for disallowed. Issue error and
17039 warning messages under control of COMPLAIN. */
17041 static int
17042 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17044 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17045 return 0;
17046 else if (POINTER_TYPE_P (type))
17047 return 0;
17048 else if (TYPE_PTR_TO_MEMBER_P (type))
17049 return 0;
17050 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17051 return 0;
17052 else if (TREE_CODE (type) == TYPENAME_TYPE)
17053 return 0;
17055 if (complain & tf_error)
17056 error ("%q#T is not a valid type for a template constant parameter", type);
17057 return 1;
17060 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17061 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17063 static bool
17064 dependent_type_p_r (tree type)
17066 tree scope;
17068 /* [temp.dep.type]
17070 A type is dependent if it is:
17072 -- a template parameter. Template template parameters are types
17073 for us (since TYPE_P holds true for them) so we handle
17074 them here. */
17075 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17076 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17077 return true;
17078 /* -- a qualified-id with a nested-name-specifier which contains a
17079 class-name that names a dependent type or whose unqualified-id
17080 names a dependent type. */
17081 if (TREE_CODE (type) == TYPENAME_TYPE)
17082 return true;
17083 /* -- a cv-qualified type where the cv-unqualified type is
17084 dependent. */
17085 type = TYPE_MAIN_VARIANT (type);
17086 /* -- a compound type constructed from any dependent type. */
17087 if (TYPE_PTR_TO_MEMBER_P (type))
17088 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17089 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17090 (type)));
17091 else if (TREE_CODE (type) == POINTER_TYPE
17092 || TREE_CODE (type) == REFERENCE_TYPE)
17093 return dependent_type_p (TREE_TYPE (type));
17094 else if (TREE_CODE (type) == FUNCTION_TYPE
17095 || TREE_CODE (type) == METHOD_TYPE)
17097 tree arg_type;
17099 if (dependent_type_p (TREE_TYPE (type)))
17100 return true;
17101 for (arg_type = TYPE_ARG_TYPES (type);
17102 arg_type;
17103 arg_type = TREE_CHAIN (arg_type))
17104 if (dependent_type_p (TREE_VALUE (arg_type)))
17105 return true;
17106 return false;
17108 /* -- an array type constructed from any dependent type or whose
17109 size is specified by a constant expression that is
17110 value-dependent. */
17111 if (TREE_CODE (type) == ARRAY_TYPE)
17113 if (TYPE_DOMAIN (type)
17114 && dependent_type_p (TYPE_DOMAIN (type)))
17115 return true;
17116 return dependent_type_p (TREE_TYPE (type));
17118 else if (TREE_CODE (type) == INTEGER_TYPE
17119 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
17121 /* If this is the TYPE_DOMAIN of an array type, consider it
17122 dependent. We already checked for value-dependence in
17123 compute_array_index_type. */
17124 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
17127 /* -- a template-id in which either the template name is a template
17128 parameter ... */
17129 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17130 return true;
17131 /* ... or any of the template arguments is a dependent type or
17132 an expression that is type-dependent or value-dependent. */
17133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17134 && (any_dependent_template_arguments_p
17135 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17136 return true;
17138 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17139 argument of the `typeof' expression is not type-dependent, then
17140 it should already been have resolved. */
17141 if (TREE_CODE (type) == TYPEOF_TYPE
17142 || TREE_CODE (type) == DECLTYPE_TYPE)
17143 return true;
17145 /* A template argument pack is dependent if any of its packed
17146 arguments are. */
17147 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17149 tree args = ARGUMENT_PACK_ARGS (type);
17150 int i, len = TREE_VEC_LENGTH (args);
17151 for (i = 0; i < len; ++i)
17152 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17153 return true;
17156 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17157 be template parameters. */
17158 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17159 return true;
17161 /* The standard does not specifically mention types that are local
17162 to template functions or local classes, but they should be
17163 considered dependent too. For example:
17165 template <int I> void f() {
17166 enum E { a = I };
17167 S<sizeof (E)> s;
17170 The size of `E' cannot be known until the value of `I' has been
17171 determined. Therefore, `E' must be considered dependent. */
17172 scope = TYPE_CONTEXT (type);
17173 if (scope && TYPE_P (scope))
17174 return dependent_type_p (scope);
17175 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17176 return type_dependent_expression_p (scope);
17178 /* Other types are non-dependent. */
17179 return false;
17182 /* Returns TRUE if TYPE is dependent, in the sense of
17183 [temp.dep.type]. */
17185 bool
17186 dependent_type_p (tree type)
17188 /* If there are no template parameters in scope, then there can't be
17189 any dependent types. */
17190 if (!processing_template_decl)
17192 /* If we are not processing a template, then nobody should be
17193 providing us with a dependent type. */
17194 gcc_assert (type);
17195 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17196 return false;
17199 /* If the type is NULL, we have not computed a type for the entity
17200 in question; in that case, the type is dependent. */
17201 if (!type)
17202 return true;
17204 /* Erroneous types can be considered non-dependent. */
17205 if (type == error_mark_node)
17206 return false;
17208 /* If we have not already computed the appropriate value for TYPE,
17209 do so now. */
17210 if (!TYPE_DEPENDENT_P_VALID (type))
17212 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17213 TYPE_DEPENDENT_P_VALID (type) = 1;
17216 return TYPE_DEPENDENT_P (type);
17219 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17220 lookup. In other words, a dependent type that is not the current
17221 instantiation. */
17223 bool
17224 dependent_scope_p (tree scope)
17226 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17227 && !currently_open_class (scope));
17230 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17232 static bool
17233 dependent_scope_ref_p (tree expression, bool criterion (tree))
17235 tree scope;
17236 tree name;
17238 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
17240 if (!TYPE_P (TREE_OPERAND (expression, 0)))
17241 return true;
17243 scope = TREE_OPERAND (expression, 0);
17244 name = TREE_OPERAND (expression, 1);
17246 /* [temp.dep.expr]
17248 An id-expression is type-dependent if it contains a
17249 nested-name-specifier that contains a class-name that names a
17250 dependent type. */
17251 /* The suggested resolution to Core Issue 224 implies that if the
17252 qualifying type is the current class, then we must peek
17253 inside it. */
17254 if (DECL_P (name)
17255 && currently_open_class (scope)
17256 && !criterion (name))
17257 return false;
17258 if (dependent_type_p (scope))
17259 return true;
17261 return false;
17264 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17265 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17266 expression. */
17268 bool
17269 value_dependent_expression_p (tree expression)
17271 if (!processing_template_decl)
17272 return false;
17274 /* A name declared with a dependent type. */
17275 if (DECL_P (expression) && type_dependent_expression_p (expression))
17276 return true;
17278 switch (TREE_CODE (expression))
17280 case IDENTIFIER_NODE:
17281 /* A name that has not been looked up -- must be dependent. */
17282 return true;
17284 case TEMPLATE_PARM_INDEX:
17285 /* A non-type template parm. */
17286 return true;
17288 case CONST_DECL:
17289 /* A non-type template parm. */
17290 if (DECL_TEMPLATE_PARM_P (expression))
17291 return true;
17292 return value_dependent_expression_p (DECL_INITIAL (expression));
17294 case VAR_DECL:
17295 /* A constant with integral or enumeration type and is initialized
17296 with an expression that is value-dependent. */
17297 if (DECL_INITIAL (expression)
17298 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17299 && value_dependent_expression_p (DECL_INITIAL (expression)))
17300 return true;
17301 return false;
17303 case DYNAMIC_CAST_EXPR:
17304 case STATIC_CAST_EXPR:
17305 case CONST_CAST_EXPR:
17306 case REINTERPRET_CAST_EXPR:
17307 case CAST_EXPR:
17308 /* These expressions are value-dependent if the type to which
17309 the cast occurs is dependent or the expression being casted
17310 is value-dependent. */
17312 tree type = TREE_TYPE (expression);
17314 if (dependent_type_p (type))
17315 return true;
17317 /* A functional cast has a list of operands. */
17318 expression = TREE_OPERAND (expression, 0);
17319 if (!expression)
17321 /* If there are no operands, it must be an expression such
17322 as "int()". This should not happen for aggregate types
17323 because it would form non-constant expressions. */
17324 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17326 return false;
17329 if (TREE_CODE (expression) == TREE_LIST)
17330 return any_value_dependent_elements_p (expression);
17332 return value_dependent_expression_p (expression);
17335 case SIZEOF_EXPR:
17336 case ALIGNOF_EXPR:
17337 /* A `sizeof' expression is value-dependent if the operand is
17338 type-dependent or is a pack expansion. */
17339 expression = TREE_OPERAND (expression, 0);
17340 if (PACK_EXPANSION_P (expression))
17341 return true;
17342 else if (TYPE_P (expression))
17343 return dependent_type_p (expression);
17344 return type_dependent_expression_p (expression);
17346 case SCOPE_REF:
17347 return dependent_scope_ref_p (expression, value_dependent_expression_p);
17349 case COMPONENT_REF:
17350 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17351 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17353 case CALL_EXPR:
17354 /* A CALL_EXPR may appear in a constant expression if it is a
17355 call to a builtin function, e.g., __builtin_constant_p. All
17356 such calls are value-dependent. */
17357 return true;
17359 case NONTYPE_ARGUMENT_PACK:
17360 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17361 is value-dependent. */
17363 tree values = ARGUMENT_PACK_ARGS (expression);
17364 int i, len = TREE_VEC_LENGTH (values);
17366 for (i = 0; i < len; ++i)
17367 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17368 return true;
17370 return false;
17373 case TRAIT_EXPR:
17375 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17376 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17377 || (type2 ? dependent_type_p (type2) : false));
17380 case MODOP_EXPR:
17381 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17382 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17384 default:
17385 /* A constant expression is value-dependent if any subexpression is
17386 value-dependent. */
17387 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17389 case tcc_reference:
17390 case tcc_unary:
17391 return (value_dependent_expression_p
17392 (TREE_OPERAND (expression, 0)));
17394 case tcc_comparison:
17395 case tcc_binary:
17396 return ((value_dependent_expression_p
17397 (TREE_OPERAND (expression, 0)))
17398 || (value_dependent_expression_p
17399 (TREE_OPERAND (expression, 1))));
17401 case tcc_expression:
17402 case tcc_vl_exp:
17404 int i;
17405 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17406 /* In some cases, some of the operands may be missing.
17407 (For example, in the case of PREDECREMENT_EXPR, the
17408 amount to increment by may be missing.) That doesn't
17409 make the expression dependent. */
17410 if (TREE_OPERAND (expression, i)
17411 && (value_dependent_expression_p
17412 (TREE_OPERAND (expression, i))))
17413 return true;
17414 return false;
17417 default:
17418 break;
17422 /* The expression is not value-dependent. */
17423 return false;
17426 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17427 [temp.dep.expr]. */
17429 bool
17430 type_dependent_expression_p (tree expression)
17432 if (!processing_template_decl)
17433 return false;
17435 if (expression == error_mark_node)
17436 return false;
17438 /* An unresolved name is always dependent. */
17439 if (TREE_CODE (expression) == IDENTIFIER_NODE
17440 || TREE_CODE (expression) == USING_DECL)
17441 return true;
17443 /* Some expression forms are never type-dependent. */
17444 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17445 || TREE_CODE (expression) == SIZEOF_EXPR
17446 || TREE_CODE (expression) == ALIGNOF_EXPR
17447 || TREE_CODE (expression) == TRAIT_EXPR
17448 || TREE_CODE (expression) == TYPEID_EXPR
17449 || TREE_CODE (expression) == DELETE_EXPR
17450 || TREE_CODE (expression) == VEC_DELETE_EXPR
17451 || TREE_CODE (expression) == THROW_EXPR)
17452 return false;
17454 /* The types of these expressions depends only on the type to which
17455 the cast occurs. */
17456 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17457 || TREE_CODE (expression) == STATIC_CAST_EXPR
17458 || TREE_CODE (expression) == CONST_CAST_EXPR
17459 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17460 || TREE_CODE (expression) == CAST_EXPR)
17461 return dependent_type_p (TREE_TYPE (expression));
17463 /* The types of these expressions depends only on the type created
17464 by the expression. */
17465 if (TREE_CODE (expression) == NEW_EXPR
17466 || TREE_CODE (expression) == VEC_NEW_EXPR)
17468 /* For NEW_EXPR tree nodes created inside a template, either
17469 the object type itself or a TREE_LIST may appear as the
17470 operand 1. */
17471 tree type = TREE_OPERAND (expression, 1);
17472 if (TREE_CODE (type) == TREE_LIST)
17473 /* This is an array type. We need to check array dimensions
17474 as well. */
17475 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17476 || value_dependent_expression_p
17477 (TREE_OPERAND (TREE_VALUE (type), 1));
17478 else
17479 return dependent_type_p (type);
17482 if (TREE_CODE (expression) == SCOPE_REF
17483 && dependent_scope_ref_p (expression,
17484 type_dependent_expression_p))
17485 return true;
17487 if (TREE_CODE (expression) == FUNCTION_DECL
17488 && DECL_LANG_SPECIFIC (expression)
17489 && DECL_TEMPLATE_INFO (expression)
17490 && (any_dependent_template_arguments_p
17491 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17492 return true;
17494 if (TREE_CODE (expression) == TEMPLATE_DECL
17495 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17496 return false;
17498 if (TREE_CODE (expression) == STMT_EXPR)
17499 expression = stmt_expr_value_expr (expression);
17501 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17503 tree elt;
17504 unsigned i;
17506 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17508 if (type_dependent_expression_p (elt))
17509 return true;
17511 return false;
17514 if (TREE_TYPE (expression) == unknown_type_node)
17516 if (TREE_CODE (expression) == ADDR_EXPR)
17517 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17518 if (TREE_CODE (expression) == COMPONENT_REF
17519 || TREE_CODE (expression) == OFFSET_REF)
17521 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17522 return true;
17523 expression = TREE_OPERAND (expression, 1);
17524 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17525 return false;
17527 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17528 if (TREE_CODE (expression) == SCOPE_REF)
17529 return false;
17531 if (TREE_CODE (expression) == BASELINK)
17532 expression = BASELINK_FUNCTIONS (expression);
17534 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17536 if (any_dependent_template_arguments_p
17537 (TREE_OPERAND (expression, 1)))
17538 return true;
17539 expression = TREE_OPERAND (expression, 0);
17541 gcc_assert (TREE_CODE (expression) == OVERLOAD
17542 || TREE_CODE (expression) == FUNCTION_DECL);
17544 while (expression)
17546 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17547 return true;
17548 expression = OVL_NEXT (expression);
17550 return false;
17553 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17555 return (dependent_type_p (TREE_TYPE (expression)));
17558 /* Like type_dependent_expression_p, but it also works while not processing
17559 a template definition, i.e. during substitution or mangling. */
17561 bool
17562 type_dependent_expression_p_push (tree expr)
17564 bool b;
17565 ++processing_template_decl;
17566 b = type_dependent_expression_p (expr);
17567 --processing_template_decl;
17568 return b;
17571 /* Returns TRUE if ARGS contains a type-dependent expression. */
17573 bool
17574 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17576 unsigned int i;
17577 tree arg;
17579 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17581 if (type_dependent_expression_p (arg))
17582 return true;
17584 return false;
17587 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17588 expressions) contains any value-dependent expressions. */
17590 bool
17591 any_value_dependent_elements_p (const_tree list)
17593 for (; list; list = TREE_CHAIN (list))
17594 if (value_dependent_expression_p (TREE_VALUE (list)))
17595 return true;
17597 return false;
17600 /* Returns TRUE if the ARG (a template argument) is dependent. */
17602 bool
17603 dependent_template_arg_p (tree arg)
17605 if (!processing_template_decl)
17606 return false;
17608 if (TREE_CODE (arg) == TEMPLATE_DECL
17609 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17610 return dependent_template_p (arg);
17611 else if (ARGUMENT_PACK_P (arg))
17613 tree args = ARGUMENT_PACK_ARGS (arg);
17614 int i, len = TREE_VEC_LENGTH (args);
17615 for (i = 0; i < len; ++i)
17617 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17618 return true;
17621 return false;
17623 else if (TYPE_P (arg))
17624 return dependent_type_p (arg);
17625 else
17626 return (type_dependent_expression_p (arg)
17627 || value_dependent_expression_p (arg));
17630 /* Returns true if ARGS (a collection of template arguments) contains
17631 any types that require structural equality testing. */
17633 bool
17634 any_template_arguments_need_structural_equality_p (tree args)
17636 int i;
17637 int j;
17639 if (!args)
17640 return false;
17641 if (args == error_mark_node)
17642 return true;
17644 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17646 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17647 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17649 tree arg = TREE_VEC_ELT (level, j);
17650 tree packed_args = NULL_TREE;
17651 int k, len = 1;
17653 if (ARGUMENT_PACK_P (arg))
17655 /* Look inside the argument pack. */
17656 packed_args = ARGUMENT_PACK_ARGS (arg);
17657 len = TREE_VEC_LENGTH (packed_args);
17660 for (k = 0; k < len; ++k)
17662 if (packed_args)
17663 arg = TREE_VEC_ELT (packed_args, k);
17665 if (error_operand_p (arg))
17666 return true;
17667 else if (TREE_CODE (arg) == TEMPLATE_DECL
17668 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17669 continue;
17670 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17671 return true;
17672 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17673 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17674 return true;
17679 return false;
17682 /* Returns true if ARGS (a collection of template arguments) contains
17683 any dependent arguments. */
17685 bool
17686 any_dependent_template_arguments_p (const_tree args)
17688 int i;
17689 int j;
17691 if (!args)
17692 return false;
17693 if (args == error_mark_node)
17694 return true;
17696 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17698 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17699 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17700 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17701 return true;
17704 return false;
17707 /* Returns TRUE if the template TMPL is dependent. */
17709 bool
17710 dependent_template_p (tree tmpl)
17712 if (TREE_CODE (tmpl) == OVERLOAD)
17714 while (tmpl)
17716 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17717 return true;
17718 tmpl = OVL_CHAIN (tmpl);
17720 return false;
17723 /* Template template parameters are dependent. */
17724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17725 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17726 return true;
17727 /* So are names that have not been looked up. */
17728 if (TREE_CODE (tmpl) == SCOPE_REF
17729 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17730 return true;
17731 /* So are member templates of dependent classes. */
17732 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17733 return dependent_type_p (DECL_CONTEXT (tmpl));
17734 return false;
17737 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17739 bool
17740 dependent_template_id_p (tree tmpl, tree args)
17742 return (dependent_template_p (tmpl)
17743 || any_dependent_template_arguments_p (args));
17746 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17747 is dependent. */
17749 bool
17750 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17752 int i;
17754 if (!processing_template_decl)
17755 return false;
17757 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17759 tree decl = TREE_VEC_ELT (declv, i);
17760 tree init = TREE_VEC_ELT (initv, i);
17761 tree cond = TREE_VEC_ELT (condv, i);
17762 tree incr = TREE_VEC_ELT (incrv, i);
17764 if (type_dependent_expression_p (decl))
17765 return true;
17767 if (init && type_dependent_expression_p (init))
17768 return true;
17770 if (type_dependent_expression_p (cond))
17771 return true;
17773 if (COMPARISON_CLASS_P (cond)
17774 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17775 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17776 return true;
17778 if (TREE_CODE (incr) == MODOP_EXPR)
17780 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17781 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17782 return true;
17784 else if (type_dependent_expression_p (incr))
17785 return true;
17786 else if (TREE_CODE (incr) == MODIFY_EXPR)
17788 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
17789 return true;
17790 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
17792 tree t = TREE_OPERAND (incr, 1);
17793 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
17794 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
17795 return true;
17800 return false;
17803 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
17804 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17805 no such TYPE can be found. Note that this function peers inside
17806 uninstantiated templates and therefore should be used only in
17807 extremely limited situations. ONLY_CURRENT_P restricts this
17808 peering to the currently open classes hierarchy (which is required
17809 when comparing types). */
17811 tree
17812 resolve_typename_type (tree type, bool only_current_p)
17814 tree scope;
17815 tree name;
17816 tree decl;
17817 int quals;
17818 tree pushed_scope;
17819 tree result;
17821 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
17823 scope = TYPE_CONTEXT (type);
17824 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17825 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17826 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17827 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17828 identifier of the TYPENAME_TYPE anymore.
17829 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17830 TYPENAME_TYPE instead, we avoid messing up with a possible
17831 typedef variant case. */
17832 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
17834 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17835 it first before we can figure out what NAME refers to. */
17836 if (TREE_CODE (scope) == TYPENAME_TYPE)
17837 scope = resolve_typename_type (scope, only_current_p);
17838 /* If we don't know what SCOPE refers to, then we cannot resolve the
17839 TYPENAME_TYPE. */
17840 if (TREE_CODE (scope) == TYPENAME_TYPE)
17841 return type;
17842 /* If the SCOPE is a template type parameter, we have no way of
17843 resolving the name. */
17844 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
17845 return type;
17846 /* If the SCOPE is not the current instantiation, there's no reason
17847 to look inside it. */
17848 if (only_current_p && !currently_open_class (scope))
17849 return type;
17850 /* If this is a typedef, we don't want to look inside (c++/11987). */
17851 if (typedef_variant_p (type))
17852 return type;
17853 /* If SCOPE isn't the template itself, it will not have a valid
17854 TYPE_FIELDS list. */
17855 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
17856 /* scope is either the template itself or a compatible instantiation
17857 like X<T>, so look up the name in the original template. */
17858 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
17859 else
17860 /* scope is a partial instantiation, so we can't do the lookup or we
17861 will lose the template arguments. */
17862 return type;
17863 /* Enter the SCOPE so that name lookup will be resolved as if we
17864 were in the class definition. In particular, SCOPE will no
17865 longer be considered a dependent type. */
17866 pushed_scope = push_scope (scope);
17867 /* Look up the declaration. */
17868 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
17870 result = NULL_TREE;
17872 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17873 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17874 if (!decl)
17875 /*nop*/;
17876 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17877 && TREE_CODE (decl) == TYPE_DECL)
17879 result = TREE_TYPE (decl);
17880 if (result == error_mark_node)
17881 result = NULL_TREE;
17883 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17884 && DECL_CLASS_TEMPLATE_P (decl))
17886 tree tmpl;
17887 tree args;
17888 /* Obtain the template and the arguments. */
17889 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17890 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17891 /* Instantiate the template. */
17892 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17893 /*entering_scope=*/0,
17894 tf_error | tf_user);
17895 if (result == error_mark_node)
17896 result = NULL_TREE;
17899 /* Leave the SCOPE. */
17900 if (pushed_scope)
17901 pop_scope (pushed_scope);
17903 /* If we failed to resolve it, return the original typename. */
17904 if (!result)
17905 return type;
17907 /* If lookup found a typename type, resolve that too. */
17908 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17910 /* Ill-formed programs can cause infinite recursion here, so we
17911 must catch that. */
17912 TYPENAME_IS_RESOLVING_P (type) = 1;
17913 result = resolve_typename_type (result, only_current_p);
17914 TYPENAME_IS_RESOLVING_P (type) = 0;
17917 /* Qualify the resulting type. */
17918 quals = cp_type_quals (type);
17919 if (quals)
17920 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17922 return result;
17925 /* EXPR is an expression which is not type-dependent. Return a proxy
17926 for EXPR that can be used to compute the types of larger
17927 expressions containing EXPR. */
17929 tree
17930 build_non_dependent_expr (tree expr)
17932 tree inner_expr;
17934 /* Preserve null pointer constants so that the type of things like
17935 "p == 0" where "p" is a pointer can be determined. */
17936 if (null_ptr_cst_p (expr))
17937 return expr;
17938 /* Preserve OVERLOADs; the functions must be available to resolve
17939 types. */
17940 inner_expr = expr;
17941 if (TREE_CODE (inner_expr) == STMT_EXPR)
17942 inner_expr = stmt_expr_value_expr (inner_expr);
17943 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17944 inner_expr = TREE_OPERAND (inner_expr, 0);
17945 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17946 inner_expr = TREE_OPERAND (inner_expr, 1);
17947 if (is_overloaded_fn (inner_expr)
17948 || TREE_CODE (inner_expr) == OFFSET_REF)
17949 return expr;
17950 /* There is no need to return a proxy for a variable. */
17951 if (TREE_CODE (expr) == VAR_DECL)
17952 return expr;
17953 /* Preserve string constants; conversions from string constants to
17954 "char *" are allowed, even though normally a "const char *"
17955 cannot be used to initialize a "char *". */
17956 if (TREE_CODE (expr) == STRING_CST)
17957 return expr;
17958 /* Preserve arithmetic constants, as an optimization -- there is no
17959 reason to create a new node. */
17960 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17961 return expr;
17962 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17963 There is at least one place where we want to know that a
17964 particular expression is a throw-expression: when checking a ?:
17965 expression, there are special rules if the second or third
17966 argument is a throw-expression. */
17967 if (TREE_CODE (expr) == THROW_EXPR)
17968 return expr;
17970 if (TREE_CODE (expr) == COND_EXPR)
17971 return build3 (COND_EXPR,
17972 TREE_TYPE (expr),
17973 TREE_OPERAND (expr, 0),
17974 (TREE_OPERAND (expr, 1)
17975 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17976 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17977 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
17978 if (TREE_CODE (expr) == COMPOUND_EXPR
17979 && !COMPOUND_EXPR_OVERLOADED (expr))
17980 return build2 (COMPOUND_EXPR,
17981 TREE_TYPE (expr),
17982 TREE_OPERAND (expr, 0),
17983 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
17985 /* If the type is unknown, it can't really be non-dependent */
17986 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
17988 /* Otherwise, build a NON_DEPENDENT_EXPR.
17990 REFERENCE_TYPEs are not stripped for expressions in templates
17991 because doing so would play havoc with mangling. Consider, for
17992 example:
17994 template <typename T> void f<T& g>() { g(); }
17996 In the body of "f", the expression for "g" will have
17997 REFERENCE_TYPE, even though the standard says that it should
17998 not. The reason is that we must preserve the syntactic form of
17999 the expression so that mangling (say) "f<g>" inside the body of
18000 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18001 stripped here. */
18002 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
18005 /* ARGS is a vector of expressions as arguments to a function call.
18006 Replace the arguments with equivalent non-dependent expressions.
18007 This modifies ARGS in place. */
18009 void
18010 make_args_non_dependent (VEC(tree,gc) *args)
18012 unsigned int ix;
18013 tree arg;
18015 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
18017 tree newarg = build_non_dependent_expr (arg);
18018 if (newarg != arg)
18019 VEC_replace (tree, args, ix, newarg);
18023 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18024 with a level one deeper than the actual template parms. */
18026 tree
18027 make_auto (void)
18029 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18030 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18031 TYPE_DECL, get_identifier ("auto"), au);
18032 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18033 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18034 (0, processing_template_decl + 1, processing_template_decl + 1,
18035 TYPE_NAME (au), NULL_TREE);
18036 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18037 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18038 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18040 return au;
18043 /* Given type ARG, return std::initializer_list<ARG>. */
18045 static tree
18046 listify (tree arg)
18048 tree std_init_list = namespace_binding
18049 (get_identifier ("initializer_list"), std_node);
18050 tree argvec;
18051 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18053 error ("deducing from brace-enclosed initializer list requires "
18054 "#include <initializer_list>");
18055 return error_mark_node;
18057 argvec = make_tree_vec (1);
18058 TREE_VEC_ELT (argvec, 0) = arg;
18059 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18060 NULL_TREE, 0, tf_warning_or_error);
18063 /* Replace auto in TYPE with std::initializer_list<auto>. */
18065 static tree
18066 listify_autos (tree type, tree auto_node)
18068 tree init_auto = listify (auto_node);
18069 tree argvec = make_tree_vec (1);
18070 TREE_VEC_ELT (argvec, 0) = init_auto;
18071 if (processing_template_decl)
18072 argvec = add_to_template_args (current_template_args (), argvec);
18073 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18076 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18077 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18079 tree
18080 do_auto_deduction (tree type, tree init, tree auto_node)
18082 tree parms, tparms, targs;
18083 tree args[1];
18084 int val;
18086 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18087 with either a new invented type template parameter U or, if the
18088 initializer is a braced-init-list (8.5.4), with
18089 std::initializer_list<U>. */
18090 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18091 type = listify_autos (type, auto_node);
18093 parms = build_tree_list (NULL_TREE, type);
18094 args[0] = init;
18095 tparms = make_tree_vec (1);
18096 targs = make_tree_vec (1);
18097 TREE_VEC_ELT (tparms, 0)
18098 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18099 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18100 DEDUCE_CALL, LOOKUP_NORMAL);
18101 if (val > 0)
18103 error ("unable to deduce %qT from %qE", type, init);
18104 return error_mark_node;
18107 /* If the list of declarators contains more than one declarator, the type
18108 of each declared variable is determined as described above. If the
18109 type deduced for the template parameter U is not the same in each
18110 deduction, the program is ill-formed. */
18111 if (TREE_TYPE (auto_node)
18112 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18114 error ("inconsistent deduction for %qT: %qT and then %qT",
18115 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18116 return error_mark_node;
18118 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18120 if (processing_template_decl)
18121 targs = add_to_template_args (current_template_args (), targs);
18122 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18125 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18126 result. */
18128 tree
18129 splice_late_return_type (tree type, tree late_return_type)
18131 tree argvec;
18133 if (late_return_type == NULL_TREE)
18134 return type;
18135 argvec = make_tree_vec (1);
18136 TREE_VEC_ELT (argvec, 0) = late_return_type;
18137 if (processing_template_decl)
18138 argvec = add_to_template_args (current_template_args (), argvec);
18139 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18142 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18144 bool
18145 is_auto (const_tree type)
18147 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18148 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18149 return true;
18150 else
18151 return false;
18154 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18155 appear as a type-specifier for the declaration in question, we don't
18156 have to look through the whole type. */
18158 tree
18159 type_uses_auto (tree type)
18161 enum tree_code code;
18162 if (is_auto (type))
18163 return type;
18165 code = TREE_CODE (type);
18167 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18168 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18169 || code == METHOD_TYPE || code == ARRAY_TYPE)
18170 return type_uses_auto (TREE_TYPE (type));
18172 if (TYPE_PTRMEMFUNC_P (type))
18173 return type_uses_auto (TREE_TYPE (TREE_TYPE
18174 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18176 return NULL_TREE;
18179 /* For a given template T, return the vector of typedefs referenced
18180 in T for which access check is needed at T instantiation time.
18181 T is either a FUNCTION_DECL or a RECORD_TYPE.
18182 Those typedefs were added to T by the function
18183 append_type_to_template_for_access_check. */
18185 VEC(qualified_typedef_usage_t,gc)*
18186 get_types_needing_access_check (tree t)
18188 tree ti;
18189 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18191 if (!t || t == error_mark_node)
18192 return NULL;
18194 if (!(ti = get_template_info (t)))
18195 return NULL;
18197 if (CLASS_TYPE_P (t)
18198 || TREE_CODE (t) == FUNCTION_DECL)
18200 if (!TI_TEMPLATE (ti))
18201 return NULL;
18203 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18206 return result;
18209 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18210 tied to T. That list of typedefs will be access checked at
18211 T instantiation time.
18212 T is either a FUNCTION_DECL or a RECORD_TYPE.
18213 TYPE_DECL is a TYPE_DECL node representing a typedef.
18214 SCOPE is the scope through which TYPE_DECL is accessed.
18215 LOCATION is the location of the usage point of TYPE_DECL.
18217 This function is a subroutine of
18218 append_type_to_template_for_access_check. */
18220 static void
18221 append_type_to_template_for_access_check_1 (tree t,
18222 tree type_decl,
18223 tree scope,
18224 location_t location)
18226 qualified_typedef_usage_t typedef_usage;
18227 tree ti;
18229 if (!t || t == error_mark_node)
18230 return;
18232 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18233 || CLASS_TYPE_P (t))
18234 && type_decl
18235 && TREE_CODE (type_decl) == TYPE_DECL
18236 && scope);
18238 if (!(ti = get_template_info (t)))
18239 return;
18241 gcc_assert (TI_TEMPLATE (ti));
18243 typedef_usage.typedef_decl = type_decl;
18244 typedef_usage.context = scope;
18245 typedef_usage.locus = location;
18247 VEC_safe_push (qualified_typedef_usage_t, gc,
18248 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18249 &typedef_usage);
18252 /* Append TYPE_DECL to the template TEMPL.
18253 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18254 At TEMPL instanciation time, TYPE_DECL will be checked to see
18255 if it can be accessed through SCOPE.
18256 LOCATION is the location of the usage point of TYPE_DECL.
18258 e.g. consider the following code snippet:
18260 class C
18262 typedef int myint;
18265 template<class U> struct S
18267 C::myint mi; // <-- usage point of the typedef C::myint
18270 S<char> s;
18272 At S<char> instantiation time, we need to check the access of C::myint
18273 In other words, we need to check the access of the myint typedef through
18274 the C scope. For that purpose, this function will add the myint typedef
18275 and the scope C through which its being accessed to a list of typedefs
18276 tied to the template S. That list will be walked at template instantiation
18277 time and access check performed on each typedefs it contains.
18278 Note that this particular code snippet should yield an error because
18279 myint is private to C. */
18281 void
18282 append_type_to_template_for_access_check (tree templ,
18283 tree type_decl,
18284 tree scope,
18285 location_t location)
18287 qualified_typedef_usage_t *iter;
18288 int i;
18290 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18292 /* Make sure we don't append the type to the template twice. */
18293 for (i = 0;
18294 VEC_iterate (qualified_typedef_usage_t,
18295 get_types_needing_access_check (templ),
18296 i, iter);
18297 ++i)
18298 if (iter->typedef_decl == type_decl && scope == iter->context)
18299 return;
18301 append_type_to_template_for_access_check_1 (templ, type_decl,
18302 scope, location);
18305 /* Set up the hash tables for template instantiations. */
18307 void
18308 init_template_processing (void)
18310 decl_specializations = htab_create_ggc (37,
18311 hash_specialization,
18312 eq_specializations,
18313 ggc_free);
18314 type_specializations = htab_create_ggc (37,
18315 hash_specialization,
18316 eq_specializations,
18317 ggc_free);
18320 #include "gt-cp-pt.h"