PR tree-optimization/43833
[official-gcc/alias-decl.git] / gcc / cp / pt.c
blob29489b6337c366be3049be5c4c3a0f77c1bbfda4
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 tree reopen_tinst_level (struct tinst_level *);
123 static tree tsubst_initializer_list (tree, tree);
124 static tree get_class_bindings (tree, tree, tree);
125 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
126 bool, bool);
127 static void tsubst_enum (tree, tree, tree);
128 static tree add_to_template_args (tree, tree);
129 static tree add_outermost_template_args (tree, tree);
130 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
131 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
132 tree);
133 static int type_unification_real (tree, tree, tree, const tree *,
134 unsigned int, int, unification_kind_t, int);
135 static void note_template_header (int);
136 static tree convert_nontype_argument_function (tree, tree);
137 static tree convert_nontype_argument (tree, tree);
138 static tree convert_template_argument (tree, tree, tree,
139 tsubst_flags_t, int, tree);
140 static int for_each_template_parm (tree, tree_fn_t, void*,
141 struct pointer_set_t*, bool);
142 static tree expand_template_argument_pack (tree);
143 static tree build_template_parm_index (int, int, int, tree, tree);
144 static bool inline_needs_template_parms (tree);
145 static void push_inline_template_parms_recursive (tree, int);
146 static tree retrieve_local_specialization (tree);
147 static void register_local_specialization (tree, tree);
148 static hashval_t hash_specialization (const void *p);
149 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
150 static int mark_template_parm (tree, void *);
151 static int template_parm_this_level_p (tree, void *);
152 static tree tsubst_friend_function (tree, tree);
153 static tree tsubst_friend_class (tree, tree);
154 static int can_complete_type_without_circularity (tree);
155 static tree get_bindings (tree, tree, tree, bool);
156 static int template_decl_level (tree);
157 static int check_cv_quals_for_unify (int, tree, tree);
158 static void template_parm_level_and_index (tree, int*, int*);
159 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
160 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
161 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
163 static void regenerate_decl_from_template (tree, tree);
164 static tree most_specialized_class (tree, tree);
165 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
166 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
167 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
168 static bool check_specialization_scope (void);
169 static tree process_partial_specialization (tree);
170 static void set_current_access_from_decl (tree);
171 static tree get_template_base (tree, tree, tree, tree);
172 static tree try_class_unification (tree, tree, tree, tree);
173 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
174 tree, tree);
175 static bool template_template_parm_bindings_ok_p (tree, tree);
176 static int template_args_equal (tree, tree);
177 static void tsubst_default_arguments (tree);
178 static tree for_each_template_parm_r (tree *, int *, void *);
179 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
180 static void copy_default_args_to_explicit_spec (tree);
181 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
182 static int eq_local_specializations (const void *, const void *);
183 static bool dependent_template_arg_p (tree);
184 static bool any_template_arguments_need_structural_equality_p (tree);
185 static bool dependent_type_p_r (tree);
186 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
187 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_decl (tree, tree, tsubst_flags_t);
190 static void perform_typedefs_access_check (tree tmpl, tree targs);
191 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
192 location_t);
193 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
194 static tree listify (tree);
195 static tree listify_autos (tree, tree);
197 /* Make the current scope suitable for access checking when we are
198 processing T. T can be FUNCTION_DECL for instantiated function
199 template, or VAR_DECL for static member variable (need by
200 instantiate_decl). */
202 static void
203 push_access_scope (tree t)
205 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
206 || TREE_CODE (t) == VAR_DECL);
208 if (DECL_FRIEND_CONTEXT (t))
209 push_nested_class (DECL_FRIEND_CONTEXT (t));
210 else if (DECL_CLASS_SCOPE_P (t))
211 push_nested_class (DECL_CONTEXT (t));
212 else
213 push_to_top_level ();
215 if (TREE_CODE (t) == FUNCTION_DECL)
217 saved_access_scope = tree_cons
218 (NULL_TREE, current_function_decl, saved_access_scope);
219 current_function_decl = t;
223 /* Restore the scope set up by push_access_scope. T is the node we
224 are processing. */
226 static void
227 pop_access_scope (tree t)
229 if (TREE_CODE (t) == FUNCTION_DECL)
231 current_function_decl = TREE_VALUE (saved_access_scope);
232 saved_access_scope = TREE_CHAIN (saved_access_scope);
235 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
236 pop_nested_class ();
237 else
238 pop_from_top_level ();
241 /* Do any processing required when DECL (a member template
242 declaration) is finished. Returns the TEMPLATE_DECL corresponding
243 to DECL, unless it is a specialization, in which case the DECL
244 itself is returned. */
246 tree
247 finish_member_template_decl (tree decl)
249 if (decl == error_mark_node)
250 return error_mark_node;
252 gcc_assert (DECL_P (decl));
254 if (TREE_CODE (decl) == TYPE_DECL)
256 tree type;
258 type = TREE_TYPE (decl);
259 if (type == error_mark_node)
260 return error_mark_node;
261 if (MAYBE_CLASS_TYPE_P (type)
262 && CLASSTYPE_TEMPLATE_INFO (type)
263 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
265 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
266 check_member_template (tmpl);
267 return tmpl;
269 return NULL_TREE;
271 else if (TREE_CODE (decl) == FIELD_DECL)
272 error ("data member %qD cannot be a member template", decl);
273 else if (DECL_TEMPLATE_INFO (decl))
275 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
277 check_member_template (DECL_TI_TEMPLATE (decl));
278 return DECL_TI_TEMPLATE (decl);
280 else
281 return decl;
283 else
284 error ("invalid member template declaration %qD", decl);
286 return error_mark_node;
289 /* Create a template info node. */
291 tree
292 build_template_info (tree template_decl, tree template_args)
294 tree result = make_node (TEMPLATE_INFO);
295 TI_TEMPLATE (result) = template_decl;
296 TI_ARGS (result) = template_args;
297 return result;
300 /* Return the template info node corresponding to T, whatever T is. */
302 tree
303 get_template_info (const_tree t)
305 tree tinfo = NULL_TREE;
307 if (!t || t == error_mark_node)
308 return NULL;
310 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
311 tinfo = DECL_TEMPLATE_INFO (t);
313 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
314 t = TREE_TYPE (t);
316 if (TAGGED_TYPE_P (t))
317 tinfo = TYPE_TEMPLATE_INFO (t);
318 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
319 tinfo = TEMPLATE_TEMPLATE_PARM_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 if (args == NULL_TREE)
497 return extra_args;
499 extra_depth = TMPL_ARGS_DEPTH (extra_args);
500 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
502 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
503 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
505 for (j = 1; j <= extra_depth; ++j, ++i)
506 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
508 return new_args;
511 /* Like add_to_template_args, but only the outermost ARGS are added to
512 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
513 (EXTRA_ARGS) levels are added. This function is used to combine
514 the template arguments from a partial instantiation with the
515 template arguments used to attain the full instantiation from the
516 partial instantiation. */
518 static tree
519 add_outermost_template_args (tree args, tree extra_args)
521 tree new_args;
523 /* If there are more levels of EXTRA_ARGS than there are ARGS,
524 something very fishy is going on. */
525 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
527 /* If *all* the new arguments will be the EXTRA_ARGS, just return
528 them. */
529 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
530 return extra_args;
532 /* For the moment, we make ARGS look like it contains fewer levels. */
533 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
535 new_args = add_to_template_args (args, extra_args);
537 /* Now, we restore ARGS to its full dimensions. */
538 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
540 return new_args;
543 /* Return the N levels of innermost template arguments from the ARGS. */
545 tree
546 get_innermost_template_args (tree args, int n)
548 tree new_args;
549 int extra_levels;
550 int i;
552 gcc_assert (n >= 0);
554 /* If N is 1, just return the innermost set of template arguments. */
555 if (n == 1)
556 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
558 /* If we're not removing anything, just return the arguments we were
559 given. */
560 extra_levels = TMPL_ARGS_DEPTH (args) - n;
561 gcc_assert (extra_levels >= 0);
562 if (extra_levels == 0)
563 return args;
565 /* Make a new set of arguments, not containing the outer arguments. */
566 new_args = make_tree_vec (n);
567 for (i = 1; i <= n; ++i)
568 SET_TMPL_ARGS_LEVEL (new_args, i,
569 TMPL_ARGS_LEVEL (args, i + extra_levels));
571 return new_args;
574 /* The inverse of get_innermost_template_args: Return all but the innermost
575 EXTRA_LEVELS levels of template arguments from the ARGS. */
577 static tree
578 strip_innermost_template_args (tree args, int extra_levels)
580 tree new_args;
581 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
582 int i;
584 gcc_assert (n >= 0);
586 /* If N is 1, just return the outermost set of template arguments. */
587 if (n == 1)
588 return TMPL_ARGS_LEVEL (args, 1);
590 /* If we're not removing anything, just return the arguments we were
591 given. */
592 gcc_assert (extra_levels >= 0);
593 if (extra_levels == 0)
594 return args;
596 /* Make a new set of arguments, not containing the inner arguments. */
597 new_args = make_tree_vec (n);
598 for (i = 1; i <= n; ++i)
599 SET_TMPL_ARGS_LEVEL (new_args, i,
600 TMPL_ARGS_LEVEL (args, i));
602 return new_args;
605 /* We've got a template header coming up; push to a new level for storing
606 the parms. */
608 void
609 begin_template_parm_list (void)
611 /* We use a non-tag-transparent scope here, which causes pushtag to
612 put tags in this scope, rather than in the enclosing class or
613 namespace scope. This is the right thing, since we want
614 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
615 global template class, push_template_decl handles putting the
616 TEMPLATE_DECL into top-level scope. For a nested template class,
617 e.g.:
619 template <class T> struct S1 {
620 template <class T> struct S2 {};
623 pushtag contains special code to call pushdecl_with_scope on the
624 TEMPLATE_DECL for S2. */
625 begin_scope (sk_template_parms, NULL);
626 ++processing_template_decl;
627 ++processing_template_parmlist;
628 note_template_header (0);
631 /* This routine is called when a specialization is declared. If it is
632 invalid to declare a specialization here, an error is reported and
633 false is returned, otherwise this routine will return true. */
635 static bool
636 check_specialization_scope (void)
638 tree scope = current_scope ();
640 /* [temp.expl.spec]
642 An explicit specialization shall be declared in the namespace of
643 which the template is a member, or, for member templates, in the
644 namespace of which the enclosing class or enclosing class
645 template is a member. An explicit specialization of a member
646 function, member class or static data member of a class template
647 shall be declared in the namespace of which the class template
648 is a member. */
649 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
651 error ("explicit specialization in non-namespace scope %qD", scope);
652 return false;
655 /* [temp.expl.spec]
657 In an explicit specialization declaration for a member of a class
658 template or a member template that appears in namespace scope,
659 the member template and some of its enclosing class templates may
660 remain unspecialized, except that the declaration shall not
661 explicitly specialize a class member template if its enclosing
662 class templates are not explicitly specialized as well. */
663 if (current_template_parms)
665 error ("enclosing class templates are not explicitly specialized");
666 return false;
669 return true;
672 /* We've just seen template <>. */
674 bool
675 begin_specialization (void)
677 begin_scope (sk_template_spec, NULL);
678 note_template_header (1);
679 return check_specialization_scope ();
682 /* Called at then end of processing a declaration preceded by
683 template<>. */
685 void
686 end_specialization (void)
688 finish_scope ();
689 reset_specialization ();
692 /* Any template <>'s that we have seen thus far are not referring to a
693 function specialization. */
695 void
696 reset_specialization (void)
698 processing_specialization = 0;
699 template_header_count = 0;
702 /* We've just seen a template header. If SPECIALIZATION is nonzero,
703 it was of the form template <>. */
705 static void
706 note_template_header (int specialization)
708 processing_specialization = specialization;
709 template_header_count++;
712 /* We're beginning an explicit instantiation. */
714 void
715 begin_explicit_instantiation (void)
717 gcc_assert (!processing_explicit_instantiation);
718 processing_explicit_instantiation = true;
722 void
723 end_explicit_instantiation (void)
725 gcc_assert (processing_explicit_instantiation);
726 processing_explicit_instantiation = false;
729 /* An explicit specialization or partial specialization TMPL is being
730 declared. Check that the namespace in which the specialization is
731 occurring is permissible. Returns false iff it is invalid to
732 specialize TMPL in the current namespace. */
734 static bool
735 check_specialization_namespace (tree tmpl)
737 tree tpl_ns = decl_namespace_context (tmpl);
739 /* [tmpl.expl.spec]
741 An explicit specialization shall be declared in the namespace of
742 which the template is a member, or, for member templates, in the
743 namespace of which the enclosing class or enclosing class
744 template is a member. An explicit specialization of a member
745 function, member class or static data member of a class template
746 shall be declared in the namespace of which the class template is
747 a member. */
748 if (current_scope() != DECL_CONTEXT (tmpl)
749 && !at_namespace_scope_p ())
751 error ("specialization of %qD must appear at namespace scope", tmpl);
752 return false;
754 if (is_associated_namespace (current_namespace, tpl_ns))
755 /* Same or super-using namespace. */
756 return true;
757 else
759 permerror (input_location, "specialization of %qD in different namespace", tmpl);
760 permerror (input_location, " from definition of %q+#D", tmpl);
761 return false;
765 /* SPEC is an explicit instantiation. Check that it is valid to
766 perform this explicit instantiation in the current namespace. */
768 static void
769 check_explicit_instantiation_namespace (tree spec)
771 tree ns;
773 /* DR 275: An explicit instantiation shall appear in an enclosing
774 namespace of its template. */
775 ns = decl_namespace_context (spec);
776 if (!is_ancestor (current_namespace, ns))
777 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
778 "(which does not enclose namespace %qD)",
779 spec, current_namespace, ns);
782 /* The TYPE is being declared. If it is a template type, that means it
783 is a partial specialization. Do appropriate error-checking. */
785 tree
786 maybe_process_partial_specialization (tree type)
788 tree context;
790 if (type == error_mark_node)
791 return error_mark_node;
793 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
795 error ("name of class shadows template template parameter %qD",
796 TYPE_NAME (type));
797 return error_mark_node;
800 context = TYPE_CONTEXT (type);
802 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
804 /* This is for ordinary explicit specialization and partial
805 specialization of a template class such as:
807 template <> class C<int>;
811 template <class T> class C<T*>;
813 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
815 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
816 && !COMPLETE_TYPE_P (type))
818 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
819 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
820 if (processing_template_decl)
822 if (push_template_decl (TYPE_MAIN_DECL (type))
823 == error_mark_node)
824 return error_mark_node;
827 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
828 error ("specialization of %qT after instantiation", type);
830 else if (CLASS_TYPE_P (type)
831 && !CLASSTYPE_USE_TEMPLATE (type)
832 && CLASSTYPE_TEMPLATE_INFO (type)
833 && context && CLASS_TYPE_P (context)
834 && CLASSTYPE_TEMPLATE_INFO (context))
836 /* This is for an explicit specialization of member class
837 template according to [temp.expl.spec/18]:
839 template <> template <class U> class C<int>::D;
841 The context `C<int>' must be an implicit instantiation.
842 Otherwise this is just a member class template declared
843 earlier like:
845 template <> class C<int> { template <class U> class D; };
846 template <> template <class U> class C<int>::D;
848 In the first case, `C<int>::D' is a specialization of `C<T>::D'
849 while in the second case, `C<int>::D' is a primary template
850 and `C<T>::D' may not exist. */
852 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
853 && !COMPLETE_TYPE_P (type))
855 tree t;
856 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
858 if (current_namespace
859 != decl_namespace_context (tmpl))
861 permerror (input_location, "specializing %q#T in different namespace", type);
862 permerror (input_location, " from definition of %q+#D", tmpl);
865 /* Check for invalid specialization after instantiation:
867 template <> template <> class C<int>::D<int>;
868 template <> template <class U> class C<int>::D; */
870 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
871 t; t = TREE_CHAIN (t))
873 tree inst = TREE_VALUE (t);
874 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
876 /* We already have a full specialization of this partial
877 instantiation. Reassign it to the new member
878 specialization template. */
879 spec_entry elt;
880 spec_entry **slot;
882 elt.tmpl = most_general_template (tmpl);
883 elt.args = CLASSTYPE_TI_ARGS (inst);
884 elt.spec = inst;
886 htab_remove_elt (type_specializations, &elt);
888 elt.tmpl = tmpl;
889 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
891 slot = (spec_entry **)
892 htab_find_slot (type_specializations, &elt, INSERT);
893 *slot = GGC_NEW (spec_entry);
894 **slot = elt;
896 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
897 /* But if we've had an implicit instantiation, that's a
898 problem ([temp.expl.spec]/6). */
899 error ("specialization %qT after instantiation %qT",
900 type, inst);
903 /* Mark TYPE as a specialization. And as a result, we only
904 have one level of template argument for the innermost
905 class template. */
906 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
907 CLASSTYPE_TI_ARGS (type)
908 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
911 else if (processing_specialization)
913 error ("explicit specialization of non-template %qT", type);
914 return error_mark_node;
917 return type;
920 /* Returns nonzero if we can optimize the retrieval of specializations
921 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
922 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
924 static inline bool
925 optimize_specialization_lookup_p (tree tmpl)
927 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
928 && DECL_CLASS_SCOPE_P (tmpl)
929 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
930 parameter. */
931 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
932 /* The optimized lookup depends on the fact that the
933 template arguments for the member function template apply
934 purely to the containing class, which is not true if the
935 containing class is an explicit or partial
936 specialization. */
937 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
938 && !DECL_MEMBER_TEMPLATE_P (tmpl)
939 && !DECL_CONV_FN_P (tmpl)
940 /* It is possible to have a template that is not a member
941 template and is not a member of a template class:
943 template <typename T>
944 struct S { friend A::f(); };
946 Here, the friend function is a template, but the context does
947 not have template information. The optimized lookup relies
948 on having ARGS be the template arguments for both the class
949 and the function template. */
950 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
953 /* Retrieve the specialization (in the sense of [temp.spec] - a
954 specialization is either an instantiation or an explicit
955 specialization) of TMPL for the given template ARGS. If there is
956 no such specialization, return NULL_TREE. The ARGS are a vector of
957 arguments, or a vector of vectors of arguments, in the case of
958 templates with more than one level of parameters.
960 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
961 then we search for a partial specialization matching ARGS. This
962 parameter is ignored if TMPL is not a class template. */
964 static tree
965 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
967 if (args == error_mark_node)
968 return NULL_TREE;
970 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
972 /* There should be as many levels of arguments as there are
973 levels of parameters. */
974 gcc_assert (TMPL_ARGS_DEPTH (args)
975 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
977 if (optimize_specialization_lookup_p (tmpl))
979 tree class_template;
980 tree class_specialization;
981 VEC(tree,gc) *methods;
982 tree fns;
983 int idx;
985 /* The template arguments actually apply to the containing
986 class. Find the class specialization with those
987 arguments. */
988 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
989 class_specialization
990 = retrieve_specialization (class_template, args, 0);
991 if (!class_specialization)
992 return NULL_TREE;
993 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
994 for the specialization. */
995 idx = class_method_index_for_fn (class_specialization, tmpl);
996 if (idx == -1)
997 return NULL_TREE;
998 /* Iterate through the methods with the indicated name, looking
999 for the one that has an instance of TMPL. */
1000 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1001 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1003 tree fn = OVL_CURRENT (fns);
1004 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1005 /* using-declarations can add base methods to the method vec,
1006 and we don't want those here. */
1007 && DECL_CONTEXT (fn) == class_specialization)
1008 return fn;
1010 return NULL_TREE;
1012 else
1014 spec_entry *found;
1015 spec_entry elt;
1016 htab_t specializations;
1018 elt.tmpl = tmpl;
1019 elt.args = args;
1020 elt.spec = NULL_TREE;
1022 if (DECL_CLASS_TEMPLATE_P (tmpl))
1023 specializations = type_specializations;
1024 else
1025 specializations = decl_specializations;
1027 if (hash == 0)
1028 hash = hash_specialization (&elt);
1029 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1030 if (found)
1031 return found->spec;
1034 return NULL_TREE;
1037 /* Like retrieve_specialization, but for local declarations. */
1039 static tree
1040 retrieve_local_specialization (tree tmpl)
1042 tree spec;
1044 if (local_specializations == NULL)
1045 return NULL_TREE;
1047 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1048 htab_hash_pointer (tmpl));
1049 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1052 /* Returns nonzero iff DECL is a specialization of TMPL. */
1055 is_specialization_of (tree decl, tree tmpl)
1057 tree t;
1059 if (TREE_CODE (decl) == FUNCTION_DECL)
1061 for (t = decl;
1062 t != NULL_TREE;
1063 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1064 if (t == tmpl)
1065 return 1;
1067 else
1069 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1071 for (t = TREE_TYPE (decl);
1072 t != NULL_TREE;
1073 t = CLASSTYPE_USE_TEMPLATE (t)
1074 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1075 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1076 return 1;
1079 return 0;
1082 /* Returns nonzero iff DECL is a specialization of friend declaration
1083 FRIEND_DECL according to [temp.friend]. */
1085 bool
1086 is_specialization_of_friend (tree decl, tree friend_decl)
1088 bool need_template = true;
1089 int template_depth;
1091 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1092 || TREE_CODE (decl) == TYPE_DECL);
1094 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1095 of a template class, we want to check if DECL is a specialization
1096 if this. */
1097 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1098 && DECL_TEMPLATE_INFO (friend_decl)
1099 && !DECL_USE_TEMPLATE (friend_decl))
1101 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1102 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1103 need_template = false;
1105 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1106 && !PRIMARY_TEMPLATE_P (friend_decl))
1107 need_template = false;
1109 /* There is nothing to do if this is not a template friend. */
1110 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1111 return false;
1113 if (is_specialization_of (decl, friend_decl))
1114 return true;
1116 /* [temp.friend/6]
1117 A member of a class template may be declared to be a friend of a
1118 non-template class. In this case, the corresponding member of
1119 every specialization of the class template is a friend of the
1120 class granting friendship.
1122 For example, given a template friend declaration
1124 template <class T> friend void A<T>::f();
1126 the member function below is considered a friend
1128 template <> struct A<int> {
1129 void f();
1132 For this type of template friend, TEMPLATE_DEPTH below will be
1133 nonzero. To determine if DECL is a friend of FRIEND, we first
1134 check if the enclosing class is a specialization of another. */
1136 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1137 if (template_depth
1138 && DECL_CLASS_SCOPE_P (decl)
1139 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1140 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1142 /* Next, we check the members themselves. In order to handle
1143 a few tricky cases, such as when FRIEND_DECL's are
1145 template <class T> friend void A<T>::g(T t);
1146 template <class T> template <T t> friend void A<T>::h();
1148 and DECL's are
1150 void A<int>::g(int);
1151 template <int> void A<int>::h();
1153 we need to figure out ARGS, the template arguments from
1154 the context of DECL. This is required for template substitution
1155 of `T' in the function parameter of `g' and template parameter
1156 of `h' in the above examples. Here ARGS corresponds to `int'. */
1158 tree context = DECL_CONTEXT (decl);
1159 tree args = NULL_TREE;
1160 int current_depth = 0;
1162 while (current_depth < template_depth)
1164 if (CLASSTYPE_TEMPLATE_INFO (context))
1166 if (current_depth == 0)
1167 args = TYPE_TI_ARGS (context);
1168 else
1169 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1170 current_depth++;
1172 context = TYPE_CONTEXT (context);
1175 if (TREE_CODE (decl) == FUNCTION_DECL)
1177 bool is_template;
1178 tree friend_type;
1179 tree decl_type;
1180 tree friend_args_type;
1181 tree decl_args_type;
1183 /* Make sure that both DECL and FRIEND_DECL are templates or
1184 non-templates. */
1185 is_template = DECL_TEMPLATE_INFO (decl)
1186 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1187 if (need_template ^ is_template)
1188 return false;
1189 else if (is_template)
1191 /* If both are templates, check template parameter list. */
1192 tree friend_parms
1193 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1194 args, tf_none);
1195 if (!comp_template_parms
1196 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1197 friend_parms))
1198 return false;
1200 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1202 else
1203 decl_type = TREE_TYPE (decl);
1205 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1206 tf_none, NULL_TREE);
1207 if (friend_type == error_mark_node)
1208 return false;
1210 /* Check if return types match. */
1211 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1212 return false;
1214 /* Check if function parameter types match, ignoring the
1215 `this' parameter. */
1216 friend_args_type = TYPE_ARG_TYPES (friend_type);
1217 decl_args_type = TYPE_ARG_TYPES (decl_type);
1218 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1219 friend_args_type = TREE_CHAIN (friend_args_type);
1220 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1221 decl_args_type = TREE_CHAIN (decl_args_type);
1223 return compparms (decl_args_type, friend_args_type);
1225 else
1227 /* DECL is a TYPE_DECL */
1228 bool is_template;
1229 tree decl_type = TREE_TYPE (decl);
1231 /* Make sure that both DECL and FRIEND_DECL are templates or
1232 non-templates. */
1233 is_template
1234 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1235 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1237 if (need_template ^ is_template)
1238 return false;
1239 else if (is_template)
1241 tree friend_parms;
1242 /* If both are templates, check the name of the two
1243 TEMPLATE_DECL's first because is_friend didn't. */
1244 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1245 != DECL_NAME (friend_decl))
1246 return false;
1248 /* Now check template parameter list. */
1249 friend_parms
1250 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1251 args, tf_none);
1252 return comp_template_parms
1253 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1254 friend_parms);
1256 else
1257 return (DECL_NAME (decl)
1258 == DECL_NAME (friend_decl));
1261 return false;
1264 /* Register the specialization SPEC as a specialization of TMPL with
1265 the indicated ARGS. IS_FRIEND indicates whether the specialization
1266 is actually just a friend declaration. Returns SPEC, or an
1267 equivalent prior declaration, if available. */
1269 static tree
1270 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1271 hashval_t hash)
1273 tree fn;
1274 spec_entry **slot = NULL;
1275 spec_entry elt;
1277 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1279 if (TREE_CODE (spec) == FUNCTION_DECL
1280 && uses_template_parms (DECL_TI_ARGS (spec)))
1281 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1282 register it; we want the corresponding TEMPLATE_DECL instead.
1283 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1284 the more obvious `uses_template_parms (spec)' to avoid problems
1285 with default function arguments. In particular, given
1286 something like this:
1288 template <class T> void f(T t1, T t = T())
1290 the default argument expression is not substituted for in an
1291 instantiation unless and until it is actually needed. */
1292 return spec;
1294 if (optimize_specialization_lookup_p (tmpl))
1295 /* We don't put these specializations in the hash table, but we might
1296 want to give an error about a mismatch. */
1297 fn = retrieve_specialization (tmpl, args, 0);
1298 else
1300 elt.tmpl = tmpl;
1301 elt.args = args;
1302 elt.spec = spec;
1304 if (hash == 0)
1305 hash = hash_specialization (&elt);
1307 slot = (spec_entry **)
1308 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1309 if (*slot)
1310 fn = (*slot)->spec;
1311 else
1312 fn = NULL_TREE;
1315 /* We can sometimes try to re-register a specialization that we've
1316 already got. In particular, regenerate_decl_from_template calls
1317 duplicate_decls which will update the specialization list. But,
1318 we'll still get called again here anyhow. It's more convenient
1319 to simply allow this than to try to prevent it. */
1320 if (fn == spec)
1321 return spec;
1322 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1324 if (DECL_TEMPLATE_INSTANTIATION (fn))
1326 if (DECL_ODR_USED (fn)
1327 || DECL_EXPLICIT_INSTANTIATION (fn))
1329 error ("specialization of %qD after instantiation",
1330 fn);
1331 return error_mark_node;
1333 else
1335 tree clone;
1336 /* This situation should occur only if the first
1337 specialization is an implicit instantiation, the
1338 second is an explicit specialization, and the
1339 implicit instantiation has not yet been used. That
1340 situation can occur if we have implicitly
1341 instantiated a member function and then specialized
1342 it later.
1344 We can also wind up here if a friend declaration that
1345 looked like an instantiation turns out to be a
1346 specialization:
1348 template <class T> void foo(T);
1349 class S { friend void foo<>(int) };
1350 template <> void foo(int);
1352 We transform the existing DECL in place so that any
1353 pointers to it become pointers to the updated
1354 declaration.
1356 If there was a definition for the template, but not
1357 for the specialization, we want this to look as if
1358 there were no definition, and vice versa. */
1359 DECL_INITIAL (fn) = NULL_TREE;
1360 duplicate_decls (spec, fn, is_friend);
1361 /* The call to duplicate_decls will have applied
1362 [temp.expl.spec]:
1364 An explicit specialization of a function template
1365 is inline only if it is explicitly declared to be,
1366 and independently of whether its function template
1369 to the primary function; now copy the inline bits to
1370 the various clones. */
1371 FOR_EACH_CLONE (clone, fn)
1373 DECL_DECLARED_INLINE_P (clone)
1374 = DECL_DECLARED_INLINE_P (fn);
1375 DECL_SOURCE_LOCATION (clone)
1376 = DECL_SOURCE_LOCATION (fn);
1378 check_specialization_namespace (fn);
1380 return fn;
1383 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1385 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1386 /* Dup decl failed, but this is a new definition. Set the
1387 line number so any errors match this new
1388 definition. */
1389 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1391 return fn;
1394 else if (fn)
1395 return duplicate_decls (spec, fn, is_friend);
1397 /* A specialization must be declared in the same namespace as the
1398 template it is specializing. */
1399 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1400 && !check_specialization_namespace (tmpl))
1401 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1403 if (!optimize_specialization_lookup_p (tmpl))
1405 gcc_assert (tmpl && args && spec);
1406 *slot = GGC_NEW (spec_entry);
1407 **slot = elt;
1408 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1409 && PRIMARY_TEMPLATE_P (tmpl)
1410 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1411 /* TMPL is a forward declaration of a template function; keep a list
1412 of all specializations in case we need to reassign them to a friend
1413 template later in tsubst_friend_function. */
1414 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1415 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1418 return spec;
1421 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1422 TMPL and ARGS members, ignores SPEC. */
1424 static int
1425 eq_specializations (const void *p1, const void *p2)
1427 const spec_entry *e1 = (const spec_entry *)p1;
1428 const spec_entry *e2 = (const spec_entry *)p2;
1430 return (e1->tmpl == e2->tmpl
1431 && comp_template_args (e1->args, e2->args));
1434 /* Returns a hash for a template TMPL and template arguments ARGS. */
1436 static hashval_t
1437 hash_tmpl_and_args (tree tmpl, tree args)
1439 hashval_t val = DECL_UID (tmpl);
1440 return iterative_hash_template_arg (args, val);
1443 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1444 ignoring SPEC. */
1446 static hashval_t
1447 hash_specialization (const void *p)
1449 const spec_entry *e = (const spec_entry *)p;
1450 return hash_tmpl_and_args (e->tmpl, e->args);
1453 /* Recursively calculate a hash value for a template argument ARG, for use
1454 in the hash tables of template specializations. */
1456 static hashval_t
1457 iterative_hash_template_arg (tree arg, hashval_t val)
1459 unsigned HOST_WIDE_INT i;
1460 enum tree_code code;
1461 char tclass;
1463 if (arg == NULL_TREE)
1464 return iterative_hash_object (arg, val);
1466 if (!TYPE_P (arg))
1467 STRIP_NOPS (arg);
1469 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1470 /* We can get one of these when re-hashing a previous entry in the middle
1471 of substituting into a pack expansion. Just look through it. */
1472 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1474 code = TREE_CODE (arg);
1475 tclass = TREE_CODE_CLASS (code);
1477 val = iterative_hash_object (code, val);
1479 switch (code)
1481 case ERROR_MARK:
1482 return val;
1484 case IDENTIFIER_NODE:
1485 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1487 case TREE_VEC:
1489 int i, len = TREE_VEC_LENGTH (arg);
1490 for (i = 0; i < len; ++i)
1491 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1492 return val;
1495 case TYPE_PACK_EXPANSION:
1496 case EXPR_PACK_EXPANSION:
1497 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1499 case TYPE_ARGUMENT_PACK:
1500 case NONTYPE_ARGUMENT_PACK:
1501 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1503 case TREE_LIST:
1504 for (; arg; arg = TREE_CHAIN (arg))
1505 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1506 return val;
1508 case OVERLOAD:
1509 for (; arg; arg = OVL_CHAIN (arg))
1510 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1511 return val;
1513 case CONSTRUCTOR:
1515 tree field, value;
1516 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1518 val = iterative_hash_template_arg (field, val);
1519 val = iterative_hash_template_arg (value, val);
1521 return val;
1524 case PARM_DECL:
1525 if (!DECL_ARTIFICIAL (arg))
1526 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1527 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1529 case TARGET_EXPR:
1530 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1532 case PTRMEM_CST:
1533 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1534 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1536 case TEMPLATE_PARM_INDEX:
1537 val = iterative_hash_template_arg
1538 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1539 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1540 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1542 case TRAIT_EXPR:
1543 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1544 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1545 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1547 case BASELINK:
1548 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1549 val);
1550 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1551 val);
1553 case MODOP_EXPR:
1554 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1555 code = TREE_CODE (TREE_OPERAND (arg, 1));
1556 val = iterative_hash_object (code, val);
1557 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1559 case ARRAY_TYPE:
1560 /* layout_type sets structural equality for arrays of
1561 incomplete type, so we can't rely on the canonical type
1562 for hashing. */
1563 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1564 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1566 case LAMBDA_EXPR:
1567 /* A lambda can't appear in a template arg, but don't crash on
1568 erroneous input. */
1569 gcc_assert (errorcount > 0);
1570 return val;
1572 default:
1573 switch (tclass)
1575 case tcc_type:
1576 if (TYPE_CANONICAL (arg))
1577 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1578 val);
1579 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1580 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1581 /* Otherwise just compare the types during lookup. */
1582 return val;
1584 case tcc_declaration:
1585 case tcc_constant:
1586 return iterative_hash_expr (arg, val);
1588 default:
1589 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1591 unsigned n = TREE_OPERAND_LENGTH (arg);
1592 for (i = 0; i < n; ++i)
1593 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1594 return val;
1598 gcc_unreachable ();
1599 return 0;
1602 /* Unregister the specialization SPEC as a specialization of TMPL.
1603 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1604 if the SPEC was listed as a specialization of TMPL.
1606 Note that SPEC has been ggc_freed, so we can't look inside it. */
1608 bool
1609 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1611 spec_entry **slot;
1612 spec_entry elt;
1614 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1615 elt.args = TI_ARGS (tinfo);
1616 elt.spec = NULL_TREE;
1618 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1619 if (*slot)
1621 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1622 gcc_assert (new_spec != NULL_TREE);
1623 (*slot)->spec = new_spec;
1624 return 1;
1627 return 0;
1630 /* Compare an entry in the local specializations hash table P1 (which
1631 is really a pointer to a TREE_LIST) with P2 (which is really a
1632 DECL). */
1634 static int
1635 eq_local_specializations (const void *p1, const void *p2)
1637 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1640 /* Hash P1, an entry in the local specializations table. */
1642 static hashval_t
1643 hash_local_specialization (const void* p1)
1645 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1648 /* Like register_specialization, but for local declarations. We are
1649 registering SPEC, an instantiation of TMPL. */
1651 static void
1652 register_local_specialization (tree spec, tree tmpl)
1654 void **slot;
1656 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1657 htab_hash_pointer (tmpl), INSERT);
1658 *slot = build_tree_list (spec, tmpl);
1661 /* TYPE is a class type. Returns true if TYPE is an explicitly
1662 specialized class. */
1664 bool
1665 explicit_class_specialization_p (tree type)
1667 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1668 return false;
1669 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1672 /* Print the list of functions at FNS, going through all the overloads
1673 for each element of the list. Alternatively, FNS can not be a
1674 TREE_LIST, in which case it will be printed together with all the
1675 overloads.
1677 MORE and *STR should respectively be FALSE and NULL when the function
1678 is called from the outside. They are used internally on recursive
1679 calls. print_candidates manages the two parameters and leaves NULL
1680 in *STR when it ends. */
1682 static void
1683 print_candidates_1 (tree fns, bool more, const char **str)
1685 tree fn, fn2;
1686 char *spaces = NULL;
1688 for (fn = fns; fn; fn = OVL_NEXT (fn))
1689 if (TREE_CODE (fn) == TREE_LIST)
1691 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1692 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1693 print_candidates_1 (TREE_VALUE (fn2),
1694 TREE_CHAIN (fn2) || more, str);
1696 else
1698 if (!*str)
1700 /* Pick the prefix string. */
1701 if (!more && !OVL_NEXT (fns))
1703 error ("candidate is: %+#D", OVL_CURRENT (fn));
1704 continue;
1707 *str = _("candidates are:");
1708 spaces = get_spaces (*str);
1710 error ("%s %+#D", *str, OVL_CURRENT (fn));
1711 *str = spaces ? spaces : *str;
1714 if (!more)
1716 free (spaces);
1717 *str = NULL;
1721 /* Print the list of candidate FNS in an error message. */
1723 void
1724 print_candidates (tree fns)
1726 const char *str = NULL;
1727 print_candidates_1 (fns, false, &str);
1728 gcc_assert (str == NULL);
1731 /* Returns the template (one of the functions given by TEMPLATE_ID)
1732 which can be specialized to match the indicated DECL with the
1733 explicit template args given in TEMPLATE_ID. The DECL may be
1734 NULL_TREE if none is available. In that case, the functions in
1735 TEMPLATE_ID are non-members.
1737 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1738 specialization of a member template.
1740 The TEMPLATE_COUNT is the number of references to qualifying
1741 template classes that appeared in the name of the function. See
1742 check_explicit_specialization for a more accurate description.
1744 TSK indicates what kind of template declaration (if any) is being
1745 declared. TSK_TEMPLATE indicates that the declaration given by
1746 DECL, though a FUNCTION_DECL, has template parameters, and is
1747 therefore a template function.
1749 The template args (those explicitly specified and those deduced)
1750 are output in a newly created vector *TARGS_OUT.
1752 If it is impossible to determine the result, an error message is
1753 issued. The error_mark_node is returned to indicate failure. */
1755 static tree
1756 determine_specialization (tree template_id,
1757 tree decl,
1758 tree* targs_out,
1759 int need_member_template,
1760 int template_count,
1761 tmpl_spec_kind tsk)
1763 tree fns;
1764 tree targs;
1765 tree explicit_targs;
1766 tree candidates = NULL_TREE;
1767 /* A TREE_LIST of templates of which DECL may be a specialization.
1768 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1769 corresponding TREE_PURPOSE is the set of template arguments that,
1770 when used to instantiate the template, would produce a function
1771 with the signature of DECL. */
1772 tree templates = NULL_TREE;
1773 int header_count;
1774 struct cp_binding_level *b;
1776 *targs_out = NULL_TREE;
1778 if (template_id == error_mark_node || decl == error_mark_node)
1779 return error_mark_node;
1781 fns = TREE_OPERAND (template_id, 0);
1782 explicit_targs = TREE_OPERAND (template_id, 1);
1784 if (fns == error_mark_node)
1785 return error_mark_node;
1787 /* Check for baselinks. */
1788 if (BASELINK_P (fns))
1789 fns = BASELINK_FUNCTIONS (fns);
1791 if (!is_overloaded_fn (fns))
1793 error ("%qD is not a function template", fns);
1794 return error_mark_node;
1797 /* Count the number of template headers specified for this
1798 specialization. */
1799 header_count = 0;
1800 for (b = current_binding_level;
1801 b->kind == sk_template_parms;
1802 b = b->level_chain)
1803 ++header_count;
1805 for (; fns; fns = OVL_NEXT (fns))
1807 tree fn = OVL_CURRENT (fns);
1809 if (TREE_CODE (fn) == TEMPLATE_DECL)
1811 tree decl_arg_types;
1812 tree fn_arg_types;
1814 /* In case of explicit specialization, we need to check if
1815 the number of template headers appearing in the specialization
1816 is correct. This is usually done in check_explicit_specialization,
1817 but the check done there cannot be exhaustive when specializing
1818 member functions. Consider the following code:
1820 template <> void A<int>::f(int);
1821 template <> template <> void A<int>::f(int);
1823 Assuming that A<int> is not itself an explicit specialization
1824 already, the first line specializes "f" which is a non-template
1825 member function, whilst the second line specializes "f" which
1826 is a template member function. So both lines are syntactically
1827 correct, and check_explicit_specialization does not reject
1828 them.
1830 Here, we can do better, as we are matching the specialization
1831 against the declarations. We count the number of template
1832 headers, and we check if they match TEMPLATE_COUNT + 1
1833 (TEMPLATE_COUNT is the number of qualifying template classes,
1834 plus there must be another header for the member template
1835 itself).
1837 Notice that if header_count is zero, this is not a
1838 specialization but rather a template instantiation, so there
1839 is no check we can perform here. */
1840 if (header_count && header_count != template_count + 1)
1841 continue;
1843 /* Check that the number of template arguments at the
1844 innermost level for DECL is the same as for FN. */
1845 if (current_binding_level->kind == sk_template_parms
1846 && !current_binding_level->explicit_spec_p
1847 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1848 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1849 (current_template_parms))))
1850 continue;
1852 /* DECL might be a specialization of FN. */
1853 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1854 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1856 /* For a non-static member function, we need to make sure
1857 that the const qualification is the same. Since
1858 get_bindings does not try to merge the "this" parameter,
1859 we must do the comparison explicitly. */
1860 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1861 && !same_type_p (TREE_VALUE (fn_arg_types),
1862 TREE_VALUE (decl_arg_types)))
1863 continue;
1865 /* Skip the "this" parameter and, for constructors of
1866 classes with virtual bases, the VTT parameter. A
1867 full specialization of a constructor will have a VTT
1868 parameter, but a template never will. */
1869 decl_arg_types
1870 = skip_artificial_parms_for (decl, decl_arg_types);
1871 fn_arg_types
1872 = skip_artificial_parms_for (fn, fn_arg_types);
1874 /* Check that the number of function parameters matches.
1875 For example,
1876 template <class T> void f(int i = 0);
1877 template <> void f<int>();
1878 The specialization f<int> is invalid but is not caught
1879 by get_bindings below. */
1880 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1881 continue;
1883 /* Function templates cannot be specializations; there are
1884 no partial specializations of functions. Therefore, if
1885 the type of DECL does not match FN, there is no
1886 match. */
1887 if (tsk == tsk_template)
1889 if (compparms (fn_arg_types, decl_arg_types))
1890 candidates = tree_cons (NULL_TREE, fn, candidates);
1891 continue;
1894 /* See whether this function might be a specialization of this
1895 template. */
1896 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1898 if (!targs)
1899 /* We cannot deduce template arguments that when used to
1900 specialize TMPL will produce DECL. */
1901 continue;
1903 /* Save this template, and the arguments deduced. */
1904 templates = tree_cons (targs, fn, templates);
1906 else if (need_member_template)
1907 /* FN is an ordinary member function, and we need a
1908 specialization of a member template. */
1910 else if (TREE_CODE (fn) != FUNCTION_DECL)
1911 /* We can get IDENTIFIER_NODEs here in certain erroneous
1912 cases. */
1914 else if (!DECL_FUNCTION_MEMBER_P (fn))
1915 /* This is just an ordinary non-member function. Nothing can
1916 be a specialization of that. */
1918 else if (DECL_ARTIFICIAL (fn))
1919 /* Cannot specialize functions that are created implicitly. */
1921 else
1923 tree decl_arg_types;
1925 /* This is an ordinary member function. However, since
1926 we're here, we can assume it's enclosing class is a
1927 template class. For example,
1929 template <typename T> struct S { void f(); };
1930 template <> void S<int>::f() {}
1932 Here, S<int>::f is a non-template, but S<int> is a
1933 template class. If FN has the same type as DECL, we
1934 might be in business. */
1936 if (!DECL_TEMPLATE_INFO (fn))
1937 /* Its enclosing class is an explicit specialization
1938 of a template class. This is not a candidate. */
1939 continue;
1941 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1942 TREE_TYPE (TREE_TYPE (fn))))
1943 /* The return types differ. */
1944 continue;
1946 /* Adjust the type of DECL in case FN is a static member. */
1947 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1948 if (DECL_STATIC_FUNCTION_P (fn)
1949 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1950 decl_arg_types = TREE_CHAIN (decl_arg_types);
1952 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1953 decl_arg_types))
1954 /* They match! */
1955 candidates = tree_cons (NULL_TREE, fn, candidates);
1959 if (templates && TREE_CHAIN (templates))
1961 /* We have:
1963 [temp.expl.spec]
1965 It is possible for a specialization with a given function
1966 signature to be instantiated from more than one function
1967 template. In such cases, explicit specification of the
1968 template arguments must be used to uniquely identify the
1969 function template specialization being specialized.
1971 Note that here, there's no suggestion that we're supposed to
1972 determine which of the candidate templates is most
1973 specialized. However, we, also have:
1975 [temp.func.order]
1977 Partial ordering of overloaded function template
1978 declarations is used in the following contexts to select
1979 the function template to which a function template
1980 specialization refers:
1982 -- when an explicit specialization refers to a function
1983 template.
1985 So, we do use the partial ordering rules, at least for now.
1986 This extension can only serve to make invalid programs valid,
1987 so it's safe. And, there is strong anecdotal evidence that
1988 the committee intended the partial ordering rules to apply;
1989 the EDG front end has that behavior, and John Spicer claims
1990 that the committee simply forgot to delete the wording in
1991 [temp.expl.spec]. */
1992 tree tmpl = most_specialized_instantiation (templates);
1993 if (tmpl != error_mark_node)
1995 templates = tmpl;
1996 TREE_CHAIN (templates) = NULL_TREE;
2000 if (templates == NULL_TREE && candidates == NULL_TREE)
2002 error ("template-id %qD for %q+D does not match any template "
2003 "declaration", template_id, decl);
2004 if (header_count && header_count != template_count + 1)
2005 inform (input_location, "saw %d %<template<>%>, need %d for "
2006 "specializing a member function template",
2007 header_count, template_count + 1);
2008 return error_mark_node;
2010 else if ((templates && TREE_CHAIN (templates))
2011 || (candidates && TREE_CHAIN (candidates))
2012 || (templates && candidates))
2014 error ("ambiguous template specialization %qD for %q+D",
2015 template_id, decl);
2016 candidates = chainon (candidates, templates);
2017 print_candidates (candidates);
2018 return error_mark_node;
2021 /* We have one, and exactly one, match. */
2022 if (candidates)
2024 tree fn = TREE_VALUE (candidates);
2025 *targs_out = copy_node (DECL_TI_ARGS (fn));
2026 /* DECL is a re-declaration or partial instantiation of a template
2027 function. */
2028 if (TREE_CODE (fn) == TEMPLATE_DECL)
2029 return fn;
2030 /* It was a specialization of an ordinary member function in a
2031 template class. */
2032 return DECL_TI_TEMPLATE (fn);
2035 /* It was a specialization of a template. */
2036 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2037 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2039 *targs_out = copy_node (targs);
2040 SET_TMPL_ARGS_LEVEL (*targs_out,
2041 TMPL_ARGS_DEPTH (*targs_out),
2042 TREE_PURPOSE (templates));
2044 else
2045 *targs_out = TREE_PURPOSE (templates);
2046 return TREE_VALUE (templates);
2049 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2050 but with the default argument values filled in from those in the
2051 TMPL_TYPES. */
2053 static tree
2054 copy_default_args_to_explicit_spec_1 (tree spec_types,
2055 tree tmpl_types)
2057 tree new_spec_types;
2059 if (!spec_types)
2060 return NULL_TREE;
2062 if (spec_types == void_list_node)
2063 return void_list_node;
2065 /* Substitute into the rest of the list. */
2066 new_spec_types =
2067 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2068 TREE_CHAIN (tmpl_types));
2070 /* Add the default argument for this parameter. */
2071 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2072 TREE_VALUE (spec_types),
2073 new_spec_types);
2076 /* DECL is an explicit specialization. Replicate default arguments
2077 from the template it specializes. (That way, code like:
2079 template <class T> void f(T = 3);
2080 template <> void f(double);
2081 void g () { f (); }
2083 works, as required.) An alternative approach would be to look up
2084 the correct default arguments at the call-site, but this approach
2085 is consistent with how implicit instantiations are handled. */
2087 static void
2088 copy_default_args_to_explicit_spec (tree decl)
2090 tree tmpl;
2091 tree spec_types;
2092 tree tmpl_types;
2093 tree new_spec_types;
2094 tree old_type;
2095 tree new_type;
2096 tree t;
2097 tree object_type = NULL_TREE;
2098 tree in_charge = NULL_TREE;
2099 tree vtt = NULL_TREE;
2101 /* See if there's anything we need to do. */
2102 tmpl = DECL_TI_TEMPLATE (decl);
2103 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2104 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2105 if (TREE_PURPOSE (t))
2106 break;
2107 if (!t)
2108 return;
2110 old_type = TREE_TYPE (decl);
2111 spec_types = TYPE_ARG_TYPES (old_type);
2113 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2115 /* Remove the this pointer, but remember the object's type for
2116 CV quals. */
2117 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2118 spec_types = TREE_CHAIN (spec_types);
2119 tmpl_types = TREE_CHAIN (tmpl_types);
2121 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2123 /* DECL may contain more parameters than TMPL due to the extra
2124 in-charge parameter in constructors and destructors. */
2125 in_charge = spec_types;
2126 spec_types = TREE_CHAIN (spec_types);
2128 if (DECL_HAS_VTT_PARM_P (decl))
2130 vtt = spec_types;
2131 spec_types = TREE_CHAIN (spec_types);
2135 /* Compute the merged default arguments. */
2136 new_spec_types =
2137 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2139 /* Compute the new FUNCTION_TYPE. */
2140 if (object_type)
2142 if (vtt)
2143 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2144 TREE_VALUE (vtt),
2145 new_spec_types);
2147 if (in_charge)
2148 /* Put the in-charge parameter back. */
2149 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2150 TREE_VALUE (in_charge),
2151 new_spec_types);
2153 new_type = build_method_type_directly (object_type,
2154 TREE_TYPE (old_type),
2155 new_spec_types);
2157 else
2158 new_type = build_function_type (TREE_TYPE (old_type),
2159 new_spec_types);
2160 new_type = cp_build_type_attribute_variant (new_type,
2161 TYPE_ATTRIBUTES (old_type));
2162 new_type = build_exception_variant (new_type,
2163 TYPE_RAISES_EXCEPTIONS (old_type));
2164 TREE_TYPE (decl) = new_type;
2167 /* Check to see if the function just declared, as indicated in
2168 DECLARATOR, and in DECL, is a specialization of a function
2169 template. We may also discover that the declaration is an explicit
2170 instantiation at this point.
2172 Returns DECL, or an equivalent declaration that should be used
2173 instead if all goes well. Issues an error message if something is
2174 amiss. Returns error_mark_node if the error is not easily
2175 recoverable.
2177 FLAGS is a bitmask consisting of the following flags:
2179 2: The function has a definition.
2180 4: The function is a friend.
2182 The TEMPLATE_COUNT is the number of references to qualifying
2183 template classes that appeared in the name of the function. For
2184 example, in
2186 template <class T> struct S { void f(); };
2187 void S<int>::f();
2189 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2190 classes are not counted in the TEMPLATE_COUNT, so that in
2192 template <class T> struct S {};
2193 template <> struct S<int> { void f(); }
2194 template <> void S<int>::f();
2196 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2197 invalid; there should be no template <>.)
2199 If the function is a specialization, it is marked as such via
2200 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2201 is set up correctly, and it is added to the list of specializations
2202 for that template. */
2204 tree
2205 check_explicit_specialization (tree declarator,
2206 tree decl,
2207 int template_count,
2208 int flags)
2210 int have_def = flags & 2;
2211 int is_friend = flags & 4;
2212 int specialization = 0;
2213 int explicit_instantiation = 0;
2214 int member_specialization = 0;
2215 tree ctype = DECL_CLASS_CONTEXT (decl);
2216 tree dname = DECL_NAME (decl);
2217 tmpl_spec_kind tsk;
2219 if (is_friend)
2221 if (!processing_specialization)
2222 tsk = tsk_none;
2223 else
2224 tsk = tsk_excessive_parms;
2226 else
2227 tsk = current_tmpl_spec_kind (template_count);
2229 switch (tsk)
2231 case tsk_none:
2232 if (processing_specialization)
2234 specialization = 1;
2235 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2237 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2239 if (is_friend)
2240 /* This could be something like:
2242 template <class T> void f(T);
2243 class S { friend void f<>(int); } */
2244 specialization = 1;
2245 else
2247 /* This case handles bogus declarations like template <>
2248 template <class T> void f<int>(); */
2250 error ("template-id %qD in declaration of primary template",
2251 declarator);
2252 return decl;
2255 break;
2257 case tsk_invalid_member_spec:
2258 /* The error has already been reported in
2259 check_specialization_scope. */
2260 return error_mark_node;
2262 case tsk_invalid_expl_inst:
2263 error ("template parameter list used in explicit instantiation");
2265 /* Fall through. */
2267 case tsk_expl_inst:
2268 if (have_def)
2269 error ("definition provided for explicit instantiation");
2271 explicit_instantiation = 1;
2272 break;
2274 case tsk_excessive_parms:
2275 case tsk_insufficient_parms:
2276 if (tsk == tsk_excessive_parms)
2277 error ("too many template parameter lists in declaration of %qD",
2278 decl);
2279 else if (template_header_count)
2280 error("too few template parameter lists in declaration of %qD", decl);
2281 else
2282 error("explicit specialization of %qD must be introduced by "
2283 "%<template <>%>", decl);
2285 /* Fall through. */
2286 case tsk_expl_spec:
2287 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2288 if (ctype)
2289 member_specialization = 1;
2290 else
2291 specialization = 1;
2292 break;
2294 case tsk_template:
2295 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2297 /* This case handles bogus declarations like template <>
2298 template <class T> void f<int>(); */
2300 if (uses_template_parms (declarator))
2301 error ("function template partial specialization %qD "
2302 "is not allowed", declarator);
2303 else
2304 error ("template-id %qD in declaration of primary template",
2305 declarator);
2306 return decl;
2309 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2310 /* This is a specialization of a member template, without
2311 specialization the containing class. Something like:
2313 template <class T> struct S {
2314 template <class U> void f (U);
2316 template <> template <class U> void S<int>::f(U) {}
2318 That's a specialization -- but of the entire template. */
2319 specialization = 1;
2320 break;
2322 default:
2323 gcc_unreachable ();
2326 if (specialization || member_specialization)
2328 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2329 for (; t; t = TREE_CHAIN (t))
2330 if (TREE_PURPOSE (t))
2332 permerror (input_location,
2333 "default argument specified in explicit specialization");
2334 break;
2338 if (specialization || member_specialization || explicit_instantiation)
2340 tree tmpl = NULL_TREE;
2341 tree targs = NULL_TREE;
2343 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2344 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2346 tree fns;
2348 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2349 if (ctype)
2350 fns = dname;
2351 else
2353 /* If there is no class context, the explicit instantiation
2354 must be at namespace scope. */
2355 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2357 /* Find the namespace binding, using the declaration
2358 context. */
2359 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2360 false, true);
2361 if (fns == error_mark_node || !is_overloaded_fn (fns))
2363 error ("%qD is not a template function", dname);
2364 fns = error_mark_node;
2366 else
2368 tree fn = OVL_CURRENT (fns);
2369 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2370 CP_DECL_CONTEXT (fn)))
2371 error ("%qD is not declared in %qD",
2372 decl, current_namespace);
2376 declarator = lookup_template_function (fns, NULL_TREE);
2379 if (declarator == error_mark_node)
2380 return error_mark_node;
2382 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2384 if (!explicit_instantiation)
2385 /* A specialization in class scope. This is invalid,
2386 but the error will already have been flagged by
2387 check_specialization_scope. */
2388 return error_mark_node;
2389 else
2391 /* It's not valid to write an explicit instantiation in
2392 class scope, e.g.:
2394 class C { template void f(); }
2396 This case is caught by the parser. However, on
2397 something like:
2399 template class C { void f(); };
2401 (which is invalid) we can get here. The error will be
2402 issued later. */
2406 return decl;
2408 else if (ctype != NULL_TREE
2409 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2410 IDENTIFIER_NODE))
2412 /* Find the list of functions in ctype that have the same
2413 name as the declared function. */
2414 tree name = TREE_OPERAND (declarator, 0);
2415 tree fns = NULL_TREE;
2416 int idx;
2418 if (constructor_name_p (name, ctype))
2420 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2422 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2423 : !CLASSTYPE_DESTRUCTORS (ctype))
2425 /* From [temp.expl.spec]:
2427 If such an explicit specialization for the member
2428 of a class template names an implicitly-declared
2429 special member function (clause _special_), the
2430 program is ill-formed.
2432 Similar language is found in [temp.explicit]. */
2433 error ("specialization of implicitly-declared special member function");
2434 return error_mark_node;
2437 name = is_constructor ? ctor_identifier : dtor_identifier;
2440 if (!DECL_CONV_FN_P (decl))
2442 idx = lookup_fnfields_1 (ctype, name);
2443 if (idx >= 0)
2444 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2446 else
2448 VEC(tree,gc) *methods;
2449 tree ovl;
2451 /* For a type-conversion operator, we cannot do a
2452 name-based lookup. We might be looking for `operator
2453 int' which will be a specialization of `operator T'.
2454 So, we find *all* the conversion operators, and then
2455 select from them. */
2456 fns = NULL_TREE;
2458 methods = CLASSTYPE_METHOD_VEC (ctype);
2459 if (methods)
2460 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2461 VEC_iterate (tree, methods, idx, ovl);
2462 ++idx)
2464 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2465 /* There are no more conversion functions. */
2466 break;
2468 /* Glue all these conversion functions together
2469 with those we already have. */
2470 for (; ovl; ovl = OVL_NEXT (ovl))
2471 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2475 if (fns == NULL_TREE)
2477 error ("no member function %qD declared in %qT", name, ctype);
2478 return error_mark_node;
2480 else
2481 TREE_OPERAND (declarator, 0) = fns;
2484 /* Figure out what exactly is being specialized at this point.
2485 Note that for an explicit instantiation, even one for a
2486 member function, we cannot tell apriori whether the
2487 instantiation is for a member template, or just a member
2488 function of a template class. Even if a member template is
2489 being instantiated, the member template arguments may be
2490 elided if they can be deduced from the rest of the
2491 declaration. */
2492 tmpl = determine_specialization (declarator, decl,
2493 &targs,
2494 member_specialization,
2495 template_count,
2496 tsk);
2498 if (!tmpl || tmpl == error_mark_node)
2499 /* We couldn't figure out what this declaration was
2500 specializing. */
2501 return error_mark_node;
2502 else
2504 tree gen_tmpl = most_general_template (tmpl);
2506 if (explicit_instantiation)
2508 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2509 is done by do_decl_instantiation later. */
2511 int arg_depth = TMPL_ARGS_DEPTH (targs);
2512 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2514 if (arg_depth > parm_depth)
2516 /* If TMPL is not the most general template (for
2517 example, if TMPL is a friend template that is
2518 injected into namespace scope), then there will
2519 be too many levels of TARGS. Remove some of them
2520 here. */
2521 int i;
2522 tree new_targs;
2524 new_targs = make_tree_vec (parm_depth);
2525 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2526 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2527 = TREE_VEC_ELT (targs, i);
2528 targs = new_targs;
2531 return instantiate_template (tmpl, targs, tf_error);
2534 /* If we thought that the DECL was a member function, but it
2535 turns out to be specializing a static member function,
2536 make DECL a static member function as well. */
2537 if (DECL_STATIC_FUNCTION_P (tmpl)
2538 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2539 revert_static_member_fn (decl);
2541 /* If this is a specialization of a member template of a
2542 template class, we want to return the TEMPLATE_DECL, not
2543 the specialization of it. */
2544 if (tsk == tsk_template)
2546 tree result = DECL_TEMPLATE_RESULT (tmpl);
2547 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2548 DECL_INITIAL (result) = NULL_TREE;
2549 if (have_def)
2551 tree parm;
2552 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2553 DECL_SOURCE_LOCATION (result)
2554 = DECL_SOURCE_LOCATION (decl);
2555 /* We want to use the argument list specified in the
2556 definition, not in the original declaration. */
2557 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2558 for (parm = DECL_ARGUMENTS (result); parm;
2559 parm = TREE_CHAIN (parm))
2560 DECL_CONTEXT (parm) = result;
2562 return register_specialization (tmpl, gen_tmpl, targs,
2563 is_friend, 0);
2566 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2567 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2569 /* Inherit default function arguments from the template
2570 DECL is specializing. */
2571 copy_default_args_to_explicit_spec (decl);
2573 /* This specialization has the same protection as the
2574 template it specializes. */
2575 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2576 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2578 /* 7.1.1-1 [dcl.stc]
2580 A storage-class-specifier shall not be specified in an
2581 explicit specialization...
2583 The parser rejects these, so unless action is taken here,
2584 explicit function specializations will always appear with
2585 global linkage.
2587 The action recommended by the C++ CWG in response to C++
2588 defect report 605 is to make the storage class and linkage
2589 of the explicit specialization match the templated function:
2591 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2593 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2595 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2596 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2598 /* This specialization has the same linkage and visibility as
2599 the function template it specializes. */
2600 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2601 if (! TREE_PUBLIC (decl))
2603 DECL_INTERFACE_KNOWN (decl) = 1;
2604 DECL_NOT_REALLY_EXTERN (decl) = 1;
2606 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2607 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2609 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2610 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2614 /* If DECL is a friend declaration, declared using an
2615 unqualified name, the namespace associated with DECL may
2616 have been set incorrectly. For example, in:
2618 template <typename T> void f(T);
2619 namespace N {
2620 struct S { friend void f<int>(int); }
2623 we will have set the DECL_CONTEXT for the friend
2624 declaration to N, rather than to the global namespace. */
2625 if (DECL_NAMESPACE_SCOPE_P (decl))
2626 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2628 if (is_friend && !have_def)
2629 /* This is not really a declaration of a specialization.
2630 It's just the name of an instantiation. But, it's not
2631 a request for an instantiation, either. */
2632 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2633 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2634 /* This is indeed a specialization. In case of constructors
2635 and destructors, we need in-charge and not-in-charge
2636 versions in V3 ABI. */
2637 clone_function_decl (decl, /*update_method_vec_p=*/0);
2639 /* Register this specialization so that we can find it
2640 again. */
2641 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2645 return decl;
2648 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2649 parameters. These are represented in the same format used for
2650 DECL_TEMPLATE_PARMS. */
2653 comp_template_parms (const_tree parms1, const_tree parms2)
2655 const_tree p1;
2656 const_tree p2;
2658 if (parms1 == parms2)
2659 return 1;
2661 for (p1 = parms1, p2 = parms2;
2662 p1 != NULL_TREE && p2 != NULL_TREE;
2663 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2665 tree t1 = TREE_VALUE (p1);
2666 tree t2 = TREE_VALUE (p2);
2667 int i;
2669 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2670 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2672 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2673 return 0;
2675 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2677 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2678 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2680 /* If either of the template parameters are invalid, assume
2681 they match for the sake of error recovery. */
2682 if (parm1 == error_mark_node || parm2 == error_mark_node)
2683 return 1;
2685 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2686 return 0;
2688 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2689 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2690 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2691 continue;
2692 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2693 return 0;
2697 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2698 /* One set of parameters has more parameters lists than the
2699 other. */
2700 return 0;
2702 return 1;
2705 /* Determine whether PARM is a parameter pack. */
2707 bool
2708 template_parameter_pack_p (const_tree parm)
2710 /* Determine if we have a non-type template parameter pack. */
2711 if (TREE_CODE (parm) == PARM_DECL)
2712 return (DECL_TEMPLATE_PARM_P (parm)
2713 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2715 /* If this is a list of template parameters, we could get a
2716 TYPE_DECL or a TEMPLATE_DECL. */
2717 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2718 parm = TREE_TYPE (parm);
2720 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2721 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2722 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2725 /* Determine if T is a function parameter pack. */
2727 bool
2728 function_parameter_pack_p (const_tree t)
2730 if (t && TREE_CODE (t) == PARM_DECL)
2731 return FUNCTION_PARAMETER_PACK_P (t);
2732 return false;
2735 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2736 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2738 tree
2739 get_function_template_decl (const_tree primary_func_tmpl_inst)
2741 if (! primary_func_tmpl_inst
2742 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2743 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2744 return NULL;
2746 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2749 /* Return true iff the function parameter PARAM_DECL was expanded
2750 from the function parameter pack PACK. */
2752 bool
2753 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2755 if (DECL_ARTIFICIAL (param_decl)
2756 || !function_parameter_pack_p (pack))
2757 return false;
2759 /* The parameter pack and its pack arguments have the same
2760 DECL_PARM_INDEX. */
2761 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2764 /* Determine whether ARGS describes a variadic template args list,
2765 i.e., one that is terminated by a template argument pack. */
2767 static bool
2768 template_args_variadic_p (tree args)
2770 int nargs;
2771 tree last_parm;
2773 if (args == NULL_TREE)
2774 return false;
2776 args = INNERMOST_TEMPLATE_ARGS (args);
2777 nargs = TREE_VEC_LENGTH (args);
2779 if (nargs == 0)
2780 return false;
2782 last_parm = TREE_VEC_ELT (args, nargs - 1);
2784 return ARGUMENT_PACK_P (last_parm);
2787 /* Generate a new name for the parameter pack name NAME (an
2788 IDENTIFIER_NODE) that incorporates its */
2790 static tree
2791 make_ith_pack_parameter_name (tree name, int i)
2793 /* Munge the name to include the parameter index. */
2794 #define NUMBUF_LEN 128
2795 char numbuf[NUMBUF_LEN];
2796 char* newname;
2797 int newname_len;
2799 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2800 newname_len = IDENTIFIER_LENGTH (name)
2801 + strlen (numbuf) + 2;
2802 newname = (char*)alloca (newname_len);
2803 snprintf (newname, newname_len,
2804 "%s#%i", IDENTIFIER_POINTER (name), i);
2805 return get_identifier (newname);
2808 /* Return true if T is a primary function
2809 or class template instantiation. */
2811 bool
2812 primary_template_instantiation_p (const_tree t)
2814 if (!t)
2815 return false;
2817 if (TREE_CODE (t) == FUNCTION_DECL)
2818 return DECL_LANG_SPECIFIC (t)
2819 && DECL_TEMPLATE_INSTANTIATION (t)
2820 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2821 else if (CLASS_TYPE_P (t))
2822 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2823 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2824 return false;
2827 /* Return true if PARM is a template template parameter. */
2829 bool
2830 template_template_parameter_p (const_tree parm)
2832 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2835 /* Return the template parameters of T if T is a
2836 primary template instantiation, NULL otherwise. */
2838 tree
2839 get_primary_template_innermost_parameters (const_tree t)
2841 tree parms = NULL, template_info = NULL;
2843 if ((template_info = get_template_info (t))
2844 && primary_template_instantiation_p (t))
2845 parms = INNERMOST_TEMPLATE_PARMS
2846 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2848 return parms;
2851 /* Return the template parameters of the LEVELth level from the full list
2852 of template parameters PARMS. */
2854 tree
2855 get_template_parms_at_level (tree parms, int level)
2857 tree p;
2858 if (!parms
2859 || TREE_CODE (parms) != TREE_LIST
2860 || level > TMPL_PARMS_DEPTH (parms))
2861 return NULL_TREE;
2863 for (p = parms; p; p = TREE_CHAIN (p))
2864 if (TMPL_PARMS_DEPTH (p) == level)
2865 return p;
2867 return NULL_TREE;
2870 /* Returns the template arguments of T if T is a template instantiation,
2871 NULL otherwise. */
2873 tree
2874 get_template_innermost_arguments (const_tree t)
2876 tree args = NULL, template_info = NULL;
2878 if ((template_info = get_template_info (t))
2879 && TI_ARGS (template_info))
2880 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2882 return args;
2885 /* Return the argument pack elements of T if T is a template argument pack,
2886 NULL otherwise. */
2888 tree
2889 get_template_argument_pack_elems (const_tree t)
2891 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2892 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2893 return NULL;
2895 return ARGUMENT_PACK_ARGS (t);
2898 /* Structure used to track the progress of find_parameter_packs_r. */
2899 struct find_parameter_pack_data
2901 /* TREE_LIST that will contain all of the parameter packs found by
2902 the traversal. */
2903 tree* parameter_packs;
2905 /* Set of AST nodes that have been visited by the traversal. */
2906 struct pointer_set_t *visited;
2909 /* Identifies all of the argument packs that occur in a template
2910 argument and appends them to the TREE_LIST inside DATA, which is a
2911 find_parameter_pack_data structure. This is a subroutine of
2912 make_pack_expansion and uses_parameter_packs. */
2913 static tree
2914 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2916 tree t = *tp;
2917 struct find_parameter_pack_data* ppd =
2918 (struct find_parameter_pack_data*)data;
2919 bool parameter_pack_p = false;
2921 /* Identify whether this is a parameter pack or not. */
2922 switch (TREE_CODE (t))
2924 case TEMPLATE_PARM_INDEX:
2925 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2926 parameter_pack_p = true;
2927 break;
2929 case TEMPLATE_TYPE_PARM:
2930 case TEMPLATE_TEMPLATE_PARM:
2931 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2932 parameter_pack_p = true;
2933 break;
2935 case PARM_DECL:
2936 if (FUNCTION_PARAMETER_PACK_P (t))
2938 /* We don't want to walk into the type of a PARM_DECL,
2939 because we don't want to see the type parameter pack. */
2940 *walk_subtrees = 0;
2941 parameter_pack_p = true;
2943 break;
2945 default:
2946 /* Not a parameter pack. */
2947 break;
2950 if (parameter_pack_p)
2952 /* Add this parameter pack to the list. */
2953 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2956 if (TYPE_P (t))
2957 cp_walk_tree (&TYPE_CONTEXT (t),
2958 &find_parameter_packs_r, ppd, ppd->visited);
2960 /* This switch statement will return immediately if we don't find a
2961 parameter pack. */
2962 switch (TREE_CODE (t))
2964 case TEMPLATE_PARM_INDEX:
2965 return NULL_TREE;
2967 case BOUND_TEMPLATE_TEMPLATE_PARM:
2968 /* Check the template itself. */
2969 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2970 &find_parameter_packs_r, ppd, ppd->visited);
2971 /* Check the template arguments. */
2972 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2973 ppd->visited);
2974 *walk_subtrees = 0;
2975 return NULL_TREE;
2977 case TEMPLATE_TYPE_PARM:
2978 case TEMPLATE_TEMPLATE_PARM:
2979 return NULL_TREE;
2981 case PARM_DECL:
2982 return NULL_TREE;
2984 case RECORD_TYPE:
2985 if (TYPE_PTRMEMFUNC_P (t))
2986 return NULL_TREE;
2987 /* Fall through. */
2989 case UNION_TYPE:
2990 case ENUMERAL_TYPE:
2991 if (TYPE_TEMPLATE_INFO (t))
2992 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
2993 &find_parameter_packs_r, ppd, ppd->visited);
2995 *walk_subtrees = 0;
2996 return NULL_TREE;
2998 case TEMPLATE_DECL:
2999 cp_walk_tree (&TREE_TYPE (t),
3000 &find_parameter_packs_r, ppd, ppd->visited);
3001 return NULL_TREE;
3003 case TYPENAME_TYPE:
3004 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3005 ppd, ppd->visited);
3006 *walk_subtrees = 0;
3007 return NULL_TREE;
3009 case TYPE_PACK_EXPANSION:
3010 case EXPR_PACK_EXPANSION:
3011 *walk_subtrees = 0;
3012 return NULL_TREE;
3014 case INTEGER_TYPE:
3015 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3016 ppd, ppd->visited);
3017 *walk_subtrees = 0;
3018 return NULL_TREE;
3020 case IDENTIFIER_NODE:
3021 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3022 ppd->visited);
3023 *walk_subtrees = 0;
3024 return NULL_TREE;
3026 default:
3027 return NULL_TREE;
3030 return NULL_TREE;
3033 /* Determines if the expression or type T uses any parameter packs. */
3034 bool
3035 uses_parameter_packs (tree t)
3037 tree parameter_packs = NULL_TREE;
3038 struct find_parameter_pack_data ppd;
3039 ppd.parameter_packs = &parameter_packs;
3040 ppd.visited = pointer_set_create ();
3041 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3042 pointer_set_destroy (ppd.visited);
3043 return parameter_packs != NULL_TREE;
3046 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3047 representation a base-class initializer into a parameter pack
3048 expansion. If all goes well, the resulting node will be an
3049 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3050 respectively. */
3051 tree
3052 make_pack_expansion (tree arg)
3054 tree result;
3055 tree parameter_packs = NULL_TREE;
3056 bool for_types = false;
3057 struct find_parameter_pack_data ppd;
3059 if (!arg || arg == error_mark_node)
3060 return arg;
3062 if (TREE_CODE (arg) == TREE_LIST)
3064 /* The only time we will see a TREE_LIST here is for a base
3065 class initializer. In this case, the TREE_PURPOSE will be a
3066 _TYPE node (representing the base class expansion we're
3067 initializing) and the TREE_VALUE will be a TREE_LIST
3068 containing the initialization arguments.
3070 The resulting expansion looks somewhat different from most
3071 expansions. Rather than returning just one _EXPANSION, we
3072 return a TREE_LIST whose TREE_PURPOSE is a
3073 TYPE_PACK_EXPANSION containing the bases that will be
3074 initialized. The TREE_VALUE will be identical to the
3075 original TREE_VALUE, which is a list of arguments that will
3076 be passed to each base. We do not introduce any new pack
3077 expansion nodes into the TREE_VALUE (although it is possible
3078 that some already exist), because the TREE_PURPOSE and
3079 TREE_VALUE all need to be expanded together with the same
3080 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3081 resulting TREE_PURPOSE will mention the parameter packs in
3082 both the bases and the arguments to the bases. */
3083 tree purpose;
3084 tree value;
3085 tree parameter_packs = NULL_TREE;
3087 /* Determine which parameter packs will be used by the base
3088 class expansion. */
3089 ppd.visited = pointer_set_create ();
3090 ppd.parameter_packs = &parameter_packs;
3091 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3092 &ppd, ppd.visited);
3094 if (parameter_packs == NULL_TREE)
3096 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3097 pointer_set_destroy (ppd.visited);
3098 return error_mark_node;
3101 if (TREE_VALUE (arg) != void_type_node)
3103 /* Collect the sets of parameter packs used in each of the
3104 initialization arguments. */
3105 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3107 /* Determine which parameter packs will be expanded in this
3108 argument. */
3109 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3110 &ppd, ppd.visited);
3114 pointer_set_destroy (ppd.visited);
3116 /* Create the pack expansion type for the base type. */
3117 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3118 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3119 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3121 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3122 they will rarely be compared to anything. */
3123 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3125 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3128 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3129 for_types = true;
3131 /* Build the PACK_EXPANSION_* node. */
3132 result = for_types
3133 ? cxx_make_type (TYPE_PACK_EXPANSION)
3134 : make_node (EXPR_PACK_EXPANSION);
3135 SET_PACK_EXPANSION_PATTERN (result, arg);
3136 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3138 /* Propagate type and const-expression information. */
3139 TREE_TYPE (result) = TREE_TYPE (arg);
3140 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3142 else
3143 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3144 they will rarely be compared to anything. */
3145 SET_TYPE_STRUCTURAL_EQUALITY (result);
3147 /* Determine which parameter packs will be expanded. */
3148 ppd.parameter_packs = &parameter_packs;
3149 ppd.visited = pointer_set_create ();
3150 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3151 pointer_set_destroy (ppd.visited);
3153 /* Make sure we found some parameter packs. */
3154 if (parameter_packs == NULL_TREE)
3156 if (TYPE_P (arg))
3157 error ("expansion pattern %<%T%> contains no argument packs", arg);
3158 else
3159 error ("expansion pattern %<%E%> contains no argument packs", arg);
3160 return error_mark_node;
3162 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3164 return result;
3167 /* Checks T for any "bare" parameter packs, which have not yet been
3168 expanded, and issues an error if any are found. This operation can
3169 only be done on full expressions or types (e.g., an expression
3170 statement, "if" condition, etc.), because we could have expressions like:
3172 foo(f(g(h(args)))...)
3174 where "args" is a parameter pack. check_for_bare_parameter_packs
3175 should not be called for the subexpressions args, h(args),
3176 g(h(args)), or f(g(h(args))), because we would produce erroneous
3177 error messages.
3179 Returns TRUE and emits an error if there were bare parameter packs,
3180 returns FALSE otherwise. */
3181 bool
3182 check_for_bare_parameter_packs (tree t)
3184 tree parameter_packs = NULL_TREE;
3185 struct find_parameter_pack_data ppd;
3187 if (!processing_template_decl || !t || t == error_mark_node)
3188 return false;
3190 if (TREE_CODE (t) == TYPE_DECL)
3191 t = TREE_TYPE (t);
3193 ppd.parameter_packs = &parameter_packs;
3194 ppd.visited = pointer_set_create ();
3195 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3196 pointer_set_destroy (ppd.visited);
3198 if (parameter_packs)
3200 error ("parameter packs not expanded with %<...%>:");
3201 while (parameter_packs)
3203 tree pack = TREE_VALUE (parameter_packs);
3204 tree name = NULL_TREE;
3206 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3207 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3208 name = TYPE_NAME (pack);
3209 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3210 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3211 else
3212 name = DECL_NAME (pack);
3214 if (name)
3215 inform (input_location, " %qD", name);
3216 else
3217 inform (input_location, " <anonymous>");
3219 parameter_packs = TREE_CHAIN (parameter_packs);
3222 return true;
3225 return false;
3228 /* Expand any parameter packs that occur in the template arguments in
3229 ARGS. */
3230 tree
3231 expand_template_argument_pack (tree args)
3233 tree result_args = NULL_TREE;
3234 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3235 int num_result_args = -1;
3236 int non_default_args_count = -1;
3238 /* First, determine if we need to expand anything, and the number of
3239 slots we'll need. */
3240 for (in_arg = 0; in_arg < nargs; ++in_arg)
3242 tree arg = TREE_VEC_ELT (args, in_arg);
3243 if (arg == NULL_TREE)
3244 return args;
3245 if (ARGUMENT_PACK_P (arg))
3247 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3248 if (num_result_args < 0)
3249 num_result_args = in_arg + num_packed;
3250 else
3251 num_result_args += num_packed;
3253 else
3255 if (num_result_args >= 0)
3256 num_result_args++;
3260 /* If no expansion is necessary, we're done. */
3261 if (num_result_args < 0)
3262 return args;
3264 /* Expand arguments. */
3265 result_args = make_tree_vec (num_result_args);
3266 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3267 non_default_args_count =
3268 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3269 for (in_arg = 0; in_arg < nargs; ++in_arg)
3271 tree arg = TREE_VEC_ELT (args, in_arg);
3272 if (ARGUMENT_PACK_P (arg))
3274 tree packed = ARGUMENT_PACK_ARGS (arg);
3275 int i, num_packed = TREE_VEC_LENGTH (packed);
3276 for (i = 0; i < num_packed; ++i, ++out_arg)
3277 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3278 if (non_default_args_count > 0)
3279 non_default_args_count += num_packed;
3281 else
3283 TREE_VEC_ELT (result_args, out_arg) = arg;
3284 ++out_arg;
3287 if (non_default_args_count >= 0)
3288 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3289 return result_args;
3292 /* Checks if DECL shadows a template parameter.
3294 [temp.local]: A template-parameter shall not be redeclared within its
3295 scope (including nested scopes).
3297 Emits an error and returns TRUE if the DECL shadows a parameter,
3298 returns FALSE otherwise. */
3300 bool
3301 check_template_shadow (tree decl)
3303 tree olddecl;
3305 /* If we're not in a template, we can't possibly shadow a template
3306 parameter. */
3307 if (!current_template_parms)
3308 return true;
3310 /* Figure out what we're shadowing. */
3311 if (TREE_CODE (decl) == OVERLOAD)
3312 decl = OVL_CURRENT (decl);
3313 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3315 /* If there's no previous binding for this name, we're not shadowing
3316 anything, let alone a template parameter. */
3317 if (!olddecl)
3318 return true;
3320 /* If we're not shadowing a template parameter, we're done. Note
3321 that OLDDECL might be an OVERLOAD (or perhaps even an
3322 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3323 node. */
3324 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3325 return true;
3327 /* We check for decl != olddecl to avoid bogus errors for using a
3328 name inside a class. We check TPFI to avoid duplicate errors for
3329 inline member templates. */
3330 if (decl == olddecl
3331 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3332 return true;
3334 error ("declaration of %q+#D", decl);
3335 error (" shadows template parm %q+#D", olddecl);
3336 return false;
3339 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3340 ORIG_LEVEL, DECL, and TYPE. */
3342 static tree
3343 build_template_parm_index (int index,
3344 int level,
3345 int orig_level,
3346 tree decl,
3347 tree type)
3349 tree t = make_node (TEMPLATE_PARM_INDEX);
3350 TEMPLATE_PARM_IDX (t) = index;
3351 TEMPLATE_PARM_LEVEL (t) = level;
3352 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3353 TEMPLATE_PARM_DECL (t) = decl;
3354 TREE_TYPE (t) = type;
3355 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3356 TREE_READONLY (t) = TREE_READONLY (decl);
3358 return t;
3361 /* Find the canonical type parameter for the given template type
3362 parameter. Returns the canonical type parameter, which may be TYPE
3363 if no such parameter existed. */
3364 static tree
3365 canonical_type_parameter (tree type)
3367 tree list;
3368 int idx = TEMPLATE_TYPE_IDX (type);
3369 if (!canonical_template_parms)
3370 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3372 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3373 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3375 list = VEC_index (tree, canonical_template_parms, idx);
3376 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3377 list = TREE_CHAIN (list);
3379 if (list)
3380 return TREE_VALUE (list);
3381 else
3383 VEC_replace(tree, canonical_template_parms, idx,
3384 tree_cons (NULL_TREE, type,
3385 VEC_index (tree, canonical_template_parms, idx)));
3386 return type;
3390 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3391 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3392 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3393 new one is created. */
3395 static tree
3396 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3397 tsubst_flags_t complain)
3399 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3400 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3401 != TEMPLATE_PARM_LEVEL (index) - levels)
3402 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3404 tree orig_decl = TEMPLATE_PARM_DECL (index);
3405 tree decl, t;
3407 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3408 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3409 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3410 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3411 DECL_ARTIFICIAL (decl) = 1;
3412 SET_DECL_TEMPLATE_PARM_P (decl);
3414 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3415 TEMPLATE_PARM_LEVEL (index) - levels,
3416 TEMPLATE_PARM_ORIG_LEVEL (index),
3417 decl, type);
3418 TEMPLATE_PARM_DESCENDANTS (index) = t;
3419 TEMPLATE_PARM_PARAMETER_PACK (t)
3420 = TEMPLATE_PARM_PARAMETER_PACK (index);
3422 /* Template template parameters need this. */
3423 if (TREE_CODE (decl) == TEMPLATE_DECL)
3424 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3425 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3426 args, complain);
3429 return TEMPLATE_PARM_DESCENDANTS (index);
3432 /* Process information from new template parameter PARM and append it to the
3433 LIST being built. This new parameter is a non-type parameter iff
3434 IS_NON_TYPE is true. This new parameter is a parameter
3435 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3436 PARM_LOC. */
3438 tree
3439 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3440 bool is_parameter_pack)
3442 tree decl = 0;
3443 tree defval;
3444 tree err_parm_list;
3445 int idx = 0;
3447 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3448 defval = TREE_PURPOSE (parm);
3450 if (list)
3452 tree p = tree_last (list);
3454 if (p && TREE_VALUE (p) != error_mark_node)
3456 p = TREE_VALUE (p);
3457 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3458 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3459 else
3460 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3463 ++idx;
3465 else
3466 idx = 0;
3468 if (is_non_type)
3470 parm = TREE_VALUE (parm);
3472 SET_DECL_TEMPLATE_PARM_P (parm);
3474 if (TREE_TYPE (parm) == error_mark_node)
3476 err_parm_list = build_tree_list (defval, parm);
3477 TREE_VALUE (err_parm_list) = error_mark_node;
3478 return chainon (list, err_parm_list);
3480 else
3482 /* [temp.param]
3484 The top-level cv-qualifiers on the template-parameter are
3485 ignored when determining its type. */
3486 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3487 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3489 err_parm_list = build_tree_list (defval, parm);
3490 TREE_VALUE (err_parm_list) = error_mark_node;
3491 return chainon (list, err_parm_list);
3494 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3496 /* This template parameter is not a parameter pack, but it
3497 should be. Complain about "bare" parameter packs. */
3498 check_for_bare_parameter_packs (TREE_TYPE (parm));
3500 /* Recover by calling this a parameter pack. */
3501 is_parameter_pack = true;
3505 /* A template parameter is not modifiable. */
3506 TREE_CONSTANT (parm) = 1;
3507 TREE_READONLY (parm) = 1;
3508 decl = build_decl (parm_loc,
3509 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3510 TREE_CONSTANT (decl) = 1;
3511 TREE_READONLY (decl) = 1;
3512 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3513 = build_template_parm_index (idx, processing_template_decl,
3514 processing_template_decl,
3515 decl, TREE_TYPE (parm));
3517 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3518 = is_parameter_pack;
3520 else
3522 tree t;
3523 parm = TREE_VALUE (TREE_VALUE (parm));
3525 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3527 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3528 /* This is for distinguishing between real templates and template
3529 template parameters */
3530 TREE_TYPE (parm) = t;
3531 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3532 decl = parm;
3534 else
3536 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3537 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3538 decl = build_decl (parm_loc,
3539 TYPE_DECL, parm, t);
3542 TYPE_NAME (t) = decl;
3543 TYPE_STUB_DECL (t) = decl;
3544 parm = decl;
3545 TEMPLATE_TYPE_PARM_INDEX (t)
3546 = build_template_parm_index (idx, processing_template_decl,
3547 processing_template_decl,
3548 decl, TREE_TYPE (parm));
3549 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3550 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3552 DECL_ARTIFICIAL (decl) = 1;
3553 SET_DECL_TEMPLATE_PARM_P (decl);
3554 pushdecl (decl);
3555 parm = build_tree_list (defval, parm);
3556 return chainon (list, parm);
3559 /* The end of a template parameter list has been reached. Process the
3560 tree list into a parameter vector, converting each parameter into a more
3561 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3562 as PARM_DECLs. */
3564 tree
3565 end_template_parm_list (tree parms)
3567 int nparms;
3568 tree parm, next;
3569 tree saved_parmlist = make_tree_vec (list_length (parms));
3571 current_template_parms
3572 = tree_cons (size_int (processing_template_decl),
3573 saved_parmlist, current_template_parms);
3575 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3577 next = TREE_CHAIN (parm);
3578 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3579 TREE_CHAIN (parm) = NULL_TREE;
3580 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3581 TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
3582 current_template_parms;
3585 --processing_template_parmlist;
3587 return saved_parmlist;
3590 /* end_template_decl is called after a template declaration is seen. */
3592 void
3593 end_template_decl (void)
3595 reset_specialization ();
3597 if (! processing_template_decl)
3598 return;
3600 /* This matches the pushlevel in begin_template_parm_list. */
3601 finish_scope ();
3603 --processing_template_decl;
3604 current_template_parms = TREE_CHAIN (current_template_parms);
3607 /* Within the declaration of a template, return all levels of template
3608 parameters that apply. The template parameters are represented as
3609 a TREE_VEC, in the form documented in cp-tree.h for template
3610 arguments. */
3612 static tree
3613 current_template_args (void)
3615 tree header;
3616 tree args = NULL_TREE;
3617 int length = TMPL_PARMS_DEPTH (current_template_parms);
3618 int l = length;
3620 /* If there is only one level of template parameters, we do not
3621 create a TREE_VEC of TREE_VECs. Instead, we return a single
3622 TREE_VEC containing the arguments. */
3623 if (length > 1)
3624 args = make_tree_vec (length);
3626 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3628 tree a = copy_node (TREE_VALUE (header));
3629 int i;
3631 TREE_TYPE (a) = NULL_TREE;
3632 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3634 tree t = TREE_VEC_ELT (a, i);
3636 /* T will be a list if we are called from within a
3637 begin/end_template_parm_list pair, but a vector directly
3638 if within a begin/end_member_template_processing pair. */
3639 if (TREE_CODE (t) == TREE_LIST)
3641 t = TREE_VALUE (t);
3643 if (!error_operand_p (t))
3645 if (TREE_CODE (t) == TYPE_DECL
3646 || TREE_CODE (t) == TEMPLATE_DECL)
3648 t = TREE_TYPE (t);
3650 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3652 /* Turn this argument into a TYPE_ARGUMENT_PACK
3653 with a single element, which expands T. */
3654 tree vec = make_tree_vec (1);
3655 #ifdef ENABLE_CHECKING
3656 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3657 (vec, TREE_VEC_LENGTH (vec));
3658 #endif
3659 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3661 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3662 SET_ARGUMENT_PACK_ARGS (t, vec);
3665 else
3667 t = DECL_INITIAL (t);
3669 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3671 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3672 with a single element, which expands T. */
3673 tree vec = make_tree_vec (1);
3674 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3675 #ifdef ENABLE_CHECKING
3676 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3677 (vec, TREE_VEC_LENGTH (vec));
3678 #endif
3679 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3681 t = make_node (NONTYPE_ARGUMENT_PACK);
3682 SET_ARGUMENT_PACK_ARGS (t, vec);
3683 TREE_TYPE (t) = type;
3686 TREE_VEC_ELT (a, i) = t;
3691 #ifdef ENABLE_CHECKING
3692 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3693 #endif
3695 if (length > 1)
3696 TREE_VEC_ELT (args, --l) = a;
3697 else
3698 args = a;
3701 return args;
3704 /* Update the declared TYPE by doing any lookups which were thought to be
3705 dependent, but are not now that we know the SCOPE of the declarator. */
3707 tree
3708 maybe_update_decl_type (tree orig_type, tree scope)
3710 tree type = orig_type;
3712 if (type == NULL_TREE)
3713 return type;
3715 if (TREE_CODE (orig_type) == TYPE_DECL)
3716 type = TREE_TYPE (type);
3718 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3719 && dependent_type_p (type)
3720 /* Don't bother building up the args in this case. */
3721 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3723 /* tsubst in the args corresponding to the template parameters,
3724 including auto if present. Most things will be unchanged, but
3725 make_typename_type and tsubst_qualified_id will resolve
3726 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3727 tree args = current_template_args ();
3728 tree auto_node = type_uses_auto (type);
3729 tree pushed;
3730 if (auto_node)
3732 tree auto_vec = make_tree_vec (1);
3733 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3734 args = add_to_template_args (args, auto_vec);
3736 pushed = push_scope (scope);
3737 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3738 if (pushed)
3739 pop_scope (scope);
3742 if (type == error_mark_node)
3743 return orig_type;
3745 if (TREE_CODE (orig_type) == TYPE_DECL)
3747 if (same_type_p (type, TREE_TYPE (orig_type)))
3748 type = orig_type;
3749 else
3750 type = TYPE_NAME (type);
3752 return type;
3755 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3756 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3757 a member template. Used by push_template_decl below. */
3759 static tree
3760 build_template_decl (tree decl, tree parms, bool member_template_p)
3762 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3763 DECL_TEMPLATE_PARMS (tmpl) = parms;
3764 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3765 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3767 return tmpl;
3770 struct template_parm_data
3772 /* The level of the template parameters we are currently
3773 processing. */
3774 int level;
3776 /* The index of the specialization argument we are currently
3777 processing. */
3778 int current_arg;
3780 /* An array whose size is the number of template parameters. The
3781 elements are nonzero if the parameter has been used in any one
3782 of the arguments processed so far. */
3783 int* parms;
3785 /* An array whose size is the number of template arguments. The
3786 elements are nonzero if the argument makes use of template
3787 parameters of this level. */
3788 int* arg_uses_template_parms;
3791 /* Subroutine of push_template_decl used to see if each template
3792 parameter in a partial specialization is used in the explicit
3793 argument list. If T is of the LEVEL given in DATA (which is
3794 treated as a template_parm_data*), then DATA->PARMS is marked
3795 appropriately. */
3797 static int
3798 mark_template_parm (tree t, void* data)
3800 int level;
3801 int idx;
3802 struct template_parm_data* tpd = (struct template_parm_data*) data;
3804 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3806 level = TEMPLATE_PARM_LEVEL (t);
3807 idx = TEMPLATE_PARM_IDX (t);
3809 else
3811 level = TEMPLATE_TYPE_LEVEL (t);
3812 idx = TEMPLATE_TYPE_IDX (t);
3815 if (level == tpd->level)
3817 tpd->parms[idx] = 1;
3818 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3821 /* Return zero so that for_each_template_parm will continue the
3822 traversal of the tree; we want to mark *every* template parm. */
3823 return 0;
3826 /* Process the partial specialization DECL. */
3828 static tree
3829 process_partial_specialization (tree decl)
3831 tree type = TREE_TYPE (decl);
3832 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3833 tree specargs = CLASSTYPE_TI_ARGS (type);
3834 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3835 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3836 tree inner_parms;
3837 int nargs = TREE_VEC_LENGTH (inner_args);
3838 int ntparms;
3839 int i;
3840 int did_error_intro = 0;
3841 struct template_parm_data tpd;
3842 struct template_parm_data tpd2;
3844 gcc_assert (current_template_parms);
3846 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3847 ntparms = TREE_VEC_LENGTH (inner_parms);
3849 /* We check that each of the template parameters given in the
3850 partial specialization is used in the argument list to the
3851 specialization. For example:
3853 template <class T> struct S;
3854 template <class T> struct S<T*>;
3856 The second declaration is OK because `T*' uses the template
3857 parameter T, whereas
3859 template <class T> struct S<int>;
3861 is no good. Even trickier is:
3863 template <class T>
3864 struct S1
3866 template <class U>
3867 struct S2;
3868 template <class U>
3869 struct S2<T>;
3872 The S2<T> declaration is actually invalid; it is a
3873 full-specialization. Of course,
3875 template <class U>
3876 struct S2<T (*)(U)>;
3878 or some such would have been OK. */
3879 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3880 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3881 memset (tpd.parms, 0, sizeof (int) * ntparms);
3883 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3884 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3885 for (i = 0; i < nargs; ++i)
3887 tpd.current_arg = i;
3888 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3889 &mark_template_parm,
3890 &tpd,
3891 NULL,
3892 /*include_nondeduced_p=*/false);
3894 for (i = 0; i < ntparms; ++i)
3895 if (tpd.parms[i] == 0)
3897 /* One of the template parms was not used in the
3898 specialization. */
3899 if (!did_error_intro)
3901 error ("template parameters not used in partial specialization:");
3902 did_error_intro = 1;
3905 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3908 /* [temp.class.spec]
3910 The argument list of the specialization shall not be identical to
3911 the implicit argument list of the primary template. */
3912 if (comp_template_args
3913 (inner_args,
3914 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3915 (maintmpl)))))
3916 error ("partial specialization %qT does not specialize any template arguments", type);
3918 /* [temp.class.spec]
3920 A partially specialized non-type argument expression shall not
3921 involve template parameters of the partial specialization except
3922 when the argument expression is a simple identifier.
3924 The type of a template parameter corresponding to a specialized
3925 non-type argument shall not be dependent on a parameter of the
3926 specialization.
3928 Also, we verify that pack expansions only occur at the
3929 end of the argument list. */
3930 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3931 tpd2.parms = 0;
3932 for (i = 0; i < nargs; ++i)
3934 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3935 tree arg = TREE_VEC_ELT (inner_args, i);
3936 tree packed_args = NULL_TREE;
3937 int j, len = 1;
3939 if (ARGUMENT_PACK_P (arg))
3941 /* Extract the arguments from the argument pack. We'll be
3942 iterating over these in the following loop. */
3943 packed_args = ARGUMENT_PACK_ARGS (arg);
3944 len = TREE_VEC_LENGTH (packed_args);
3947 for (j = 0; j < len; j++)
3949 if (packed_args)
3950 /* Get the Jth argument in the parameter pack. */
3951 arg = TREE_VEC_ELT (packed_args, j);
3953 if (PACK_EXPANSION_P (arg))
3955 /* Pack expansions must come at the end of the
3956 argument list. */
3957 if ((packed_args && j < len - 1)
3958 || (!packed_args && i < nargs - 1))
3960 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3961 error ("parameter pack argument %qE must be at the "
3962 "end of the template argument list", arg);
3963 else
3964 error ("parameter pack argument %qT must be at the "
3965 "end of the template argument list", arg);
3969 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3970 /* We only care about the pattern. */
3971 arg = PACK_EXPANSION_PATTERN (arg);
3973 if (/* These first two lines are the `non-type' bit. */
3974 !TYPE_P (arg)
3975 && TREE_CODE (arg) != TEMPLATE_DECL
3976 /* This next line is the `argument expression is not just a
3977 simple identifier' condition and also the `specialized
3978 non-type argument' bit. */
3979 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3981 if ((!packed_args && tpd.arg_uses_template_parms[i])
3982 || (packed_args && uses_template_parms (arg)))
3983 error ("template argument %qE involves template parameter(s)",
3984 arg);
3985 else
3987 /* Look at the corresponding template parameter,
3988 marking which template parameters its type depends
3989 upon. */
3990 tree type = TREE_TYPE (parm);
3992 if (!tpd2.parms)
3994 /* We haven't yet initialized TPD2. Do so now. */
3995 tpd2.arg_uses_template_parms
3996 = (int *) alloca (sizeof (int) * nargs);
3997 /* The number of parameters here is the number in the
3998 main template, which, as checked in the assertion
3999 above, is NARGS. */
4000 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
4001 tpd2.level =
4002 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4005 /* Mark the template parameters. But this time, we're
4006 looking for the template parameters of the main
4007 template, not in the specialization. */
4008 tpd2.current_arg = i;
4009 tpd2.arg_uses_template_parms[i] = 0;
4010 memset (tpd2.parms, 0, sizeof (int) * nargs);
4011 for_each_template_parm (type,
4012 &mark_template_parm,
4013 &tpd2,
4014 NULL,
4015 /*include_nondeduced_p=*/false);
4017 if (tpd2.arg_uses_template_parms [i])
4019 /* The type depended on some template parameters.
4020 If they are fully specialized in the
4021 specialization, that's OK. */
4022 int j;
4023 int count = 0;
4024 for (j = 0; j < nargs; ++j)
4025 if (tpd2.parms[j] != 0
4026 && tpd.arg_uses_template_parms [j])
4027 ++count;
4028 if (count != 0)
4029 error_n (input_location, count,
4030 "type %qT of template argument %qE depends "
4031 "on a template parameter",
4032 "type %qT of template argument %qE depends "
4033 "on template parameters",
4034 type,
4035 arg);
4042 /* We should only get here once. */
4043 gcc_assert (!COMPLETE_TYPE_P (type));
4045 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4046 = tree_cons (specargs, inner_parms,
4047 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4048 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4049 return decl;
4052 /* Check that a template declaration's use of default arguments and
4053 parameter packs is not invalid. Here, PARMS are the template
4054 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4055 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4056 specialization.
4059 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4060 declaration (but not a definition); 1 indicates a declaration, 2
4061 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4062 emitted for extraneous default arguments.
4064 Returns TRUE if there were no errors found, FALSE otherwise. */
4066 bool
4067 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4068 int is_partial, int is_friend_decl)
4070 const char *msg;
4071 int last_level_to_check;
4072 tree parm_level;
4073 bool no_errors = true;
4075 /* [temp.param]
4077 A default template-argument shall not be specified in a
4078 function template declaration or a function template definition, nor
4079 in the template-parameter-list of the definition of a member of a
4080 class template. */
4082 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4083 /* You can't have a function template declaration in a local
4084 scope, nor you can you define a member of a class template in a
4085 local scope. */
4086 return true;
4088 if (current_class_type
4089 && !TYPE_BEING_DEFINED (current_class_type)
4090 && DECL_LANG_SPECIFIC (decl)
4091 && DECL_DECLARES_FUNCTION_P (decl)
4092 /* If this is either a friend defined in the scope of the class
4093 or a member function. */
4094 && (DECL_FUNCTION_MEMBER_P (decl)
4095 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4096 : DECL_FRIEND_CONTEXT (decl)
4097 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4098 : false)
4099 /* And, if it was a member function, it really was defined in
4100 the scope of the class. */
4101 && (!DECL_FUNCTION_MEMBER_P (decl)
4102 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4103 /* We already checked these parameters when the template was
4104 declared, so there's no need to do it again now. This function
4105 was defined in class scope, but we're processing it's body now
4106 that the class is complete. */
4107 return true;
4109 /* Core issue 226 (C++0x only): the following only applies to class
4110 templates. */
4111 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4113 /* [temp.param]
4115 If a template-parameter has a default template-argument, all
4116 subsequent template-parameters shall have a default
4117 template-argument supplied. */
4118 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4120 tree inner_parms = TREE_VALUE (parm_level);
4121 int ntparms = TREE_VEC_LENGTH (inner_parms);
4122 int seen_def_arg_p = 0;
4123 int i;
4125 for (i = 0; i < ntparms; ++i)
4127 tree parm = TREE_VEC_ELT (inner_parms, i);
4129 if (parm == error_mark_node)
4130 continue;
4132 if (TREE_PURPOSE (parm))
4133 seen_def_arg_p = 1;
4134 else if (seen_def_arg_p
4135 && !template_parameter_pack_p (TREE_VALUE (parm)))
4137 error ("no default argument for %qD", TREE_VALUE (parm));
4138 /* For better subsequent error-recovery, we indicate that
4139 there should have been a default argument. */
4140 TREE_PURPOSE (parm) = error_mark_node;
4141 no_errors = false;
4143 else if (is_primary
4144 && !is_partial
4145 && !is_friend_decl
4146 /* Don't complain about an enclosing partial
4147 specialization. */
4148 && parm_level == parms
4149 && TREE_CODE (decl) == TYPE_DECL
4150 && i < ntparms - 1
4151 && template_parameter_pack_p (TREE_VALUE (parm)))
4153 /* A primary class template can only have one
4154 parameter pack, at the end of the template
4155 parameter list. */
4157 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4158 error ("parameter pack %qE must be at the end of the"
4159 " template parameter list", TREE_VALUE (parm));
4160 else
4161 error ("parameter pack %qT must be at the end of the"
4162 " template parameter list",
4163 TREE_TYPE (TREE_VALUE (parm)));
4165 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4166 = error_mark_node;
4167 no_errors = false;
4173 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4174 || is_partial
4175 || !is_primary
4176 || is_friend_decl)
4177 /* For an ordinary class template, default template arguments are
4178 allowed at the innermost level, e.g.:
4179 template <class T = int>
4180 struct S {};
4181 but, in a partial specialization, they're not allowed even
4182 there, as we have in [temp.class.spec]:
4184 The template parameter list of a specialization shall not
4185 contain default template argument values.
4187 So, for a partial specialization, or for a function template
4188 (in C++98/C++03), we look at all of them. */
4190 else
4191 /* But, for a primary class template that is not a partial
4192 specialization we look at all template parameters except the
4193 innermost ones. */
4194 parms = TREE_CHAIN (parms);
4196 /* Figure out what error message to issue. */
4197 if (is_friend_decl == 2)
4198 msg = G_("default template arguments may not be used in function template "
4199 "friend re-declaration");
4200 else if (is_friend_decl)
4201 msg = G_("default template arguments may not be used in function template "
4202 "friend declarations");
4203 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4204 msg = G_("default template arguments may not be used in function templates "
4205 "without -std=c++0x or -std=gnu++0x");
4206 else if (is_partial)
4207 msg = G_("default template arguments may not be used in "
4208 "partial specializations");
4209 else
4210 msg = G_("default argument for template parameter for class enclosing %qD");
4212 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4213 /* If we're inside a class definition, there's no need to
4214 examine the parameters to the class itself. On the one
4215 hand, they will be checked when the class is defined, and,
4216 on the other, default arguments are valid in things like:
4217 template <class T = double>
4218 struct S { template <class U> void f(U); };
4219 Here the default argument for `S' has no bearing on the
4220 declaration of `f'. */
4221 last_level_to_check = template_class_depth (current_class_type) + 1;
4222 else
4223 /* Check everything. */
4224 last_level_to_check = 0;
4226 for (parm_level = parms;
4227 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4228 parm_level = TREE_CHAIN (parm_level))
4230 tree inner_parms = TREE_VALUE (parm_level);
4231 int i;
4232 int ntparms;
4234 ntparms = TREE_VEC_LENGTH (inner_parms);
4235 for (i = 0; i < ntparms; ++i)
4237 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4238 continue;
4240 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4242 if (msg)
4244 no_errors = false;
4245 if (is_friend_decl == 2)
4246 return no_errors;
4248 error (msg, decl);
4249 msg = 0;
4252 /* Clear out the default argument so that we are not
4253 confused later. */
4254 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4258 /* At this point, if we're still interested in issuing messages,
4259 they must apply to classes surrounding the object declared. */
4260 if (msg)
4261 msg = G_("default argument for template parameter for class "
4262 "enclosing %qD");
4265 return no_errors;
4268 /* Worker for push_template_decl_real, called via
4269 for_each_template_parm. DATA is really an int, indicating the
4270 level of the parameters we are interested in. If T is a template
4271 parameter of that level, return nonzero. */
4273 static int
4274 template_parm_this_level_p (tree t, void* data)
4276 int this_level = *(int *)data;
4277 int level;
4279 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4280 level = TEMPLATE_PARM_LEVEL (t);
4281 else
4282 level = TEMPLATE_TYPE_LEVEL (t);
4283 return level == this_level;
4286 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4287 parameters given by current_template_args, or reuses a
4288 previously existing one, if appropriate. Returns the DECL, or an
4289 equivalent one, if it is replaced via a call to duplicate_decls.
4291 If IS_FRIEND is true, DECL is a friend declaration. */
4293 tree
4294 push_template_decl_real (tree decl, bool is_friend)
4296 tree tmpl;
4297 tree args;
4298 tree info;
4299 tree ctx;
4300 int primary;
4301 int is_partial;
4302 int new_template_p = 0;
4303 /* True if the template is a member template, in the sense of
4304 [temp.mem]. */
4305 bool member_template_p = false;
4307 if (decl == error_mark_node || !current_template_parms)
4308 return error_mark_node;
4310 /* See if this is a partial specialization. */
4311 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4312 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4313 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4315 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4316 is_friend = true;
4318 if (is_friend)
4319 /* For a friend, we want the context of the friend function, not
4320 the type of which it is a friend. */
4321 ctx = DECL_CONTEXT (decl);
4322 else if (CP_DECL_CONTEXT (decl)
4323 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4324 /* In the case of a virtual function, we want the class in which
4325 it is defined. */
4326 ctx = CP_DECL_CONTEXT (decl);
4327 else
4328 /* Otherwise, if we're currently defining some class, the DECL
4329 is assumed to be a member of the class. */
4330 ctx = current_scope ();
4332 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4333 ctx = NULL_TREE;
4335 if (!DECL_CONTEXT (decl))
4336 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4338 /* See if this is a primary template. */
4339 if (is_friend && ctx)
4340 /* A friend template that specifies a class context, i.e.
4341 template <typename T> friend void A<T>::f();
4342 is not primary. */
4343 primary = 0;
4344 else
4345 primary = template_parm_scope_p ();
4347 if (primary)
4349 if (DECL_CLASS_SCOPE_P (decl))
4350 member_template_p = true;
4351 if (TREE_CODE (decl) == TYPE_DECL
4352 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4354 error ("template class without a name");
4355 return error_mark_node;
4357 else if (TREE_CODE (decl) == FUNCTION_DECL)
4359 if (DECL_DESTRUCTOR_P (decl))
4361 /* [temp.mem]
4363 A destructor shall not be a member template. */
4364 error ("destructor %qD declared as member template", decl);
4365 return error_mark_node;
4367 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4368 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4369 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4370 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4371 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4372 == void_list_node)))
4374 /* [basic.stc.dynamic.allocation]
4376 An allocation function can be a function
4377 template. ... Template allocation functions shall
4378 have two or more parameters. */
4379 error ("invalid template declaration of %qD", decl);
4380 return error_mark_node;
4383 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4384 && CLASS_TYPE_P (TREE_TYPE (decl)))
4385 /* OK */;
4386 else
4388 error ("template declaration of %q#D", decl);
4389 return error_mark_node;
4393 /* Check to see that the rules regarding the use of default
4394 arguments are not being violated. */
4395 check_default_tmpl_args (decl, current_template_parms,
4396 primary, is_partial, /*is_friend_decl=*/0);
4398 /* Ensure that there are no parameter packs in the type of this
4399 declaration that have not been expanded. */
4400 if (TREE_CODE (decl) == FUNCTION_DECL)
4402 /* Check each of the arguments individually to see if there are
4403 any bare parameter packs. */
4404 tree type = TREE_TYPE (decl);
4405 tree arg = DECL_ARGUMENTS (decl);
4406 tree argtype = TYPE_ARG_TYPES (type);
4408 while (arg && argtype)
4410 if (!FUNCTION_PARAMETER_PACK_P (arg)
4411 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4413 /* This is a PARM_DECL that contains unexpanded parameter
4414 packs. We have already complained about this in the
4415 check_for_bare_parameter_packs call, so just replace
4416 these types with ERROR_MARK_NODE. */
4417 TREE_TYPE (arg) = error_mark_node;
4418 TREE_VALUE (argtype) = error_mark_node;
4421 arg = TREE_CHAIN (arg);
4422 argtype = TREE_CHAIN (argtype);
4425 /* Check for bare parameter packs in the return type and the
4426 exception specifiers. */
4427 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4428 /* Errors were already issued, set return type to int
4429 as the frontend doesn't expect error_mark_node as
4430 the return type. */
4431 TREE_TYPE (type) = integer_type_node;
4432 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4433 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4435 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4437 TREE_TYPE (decl) = error_mark_node;
4438 return error_mark_node;
4441 if (is_partial)
4442 return process_partial_specialization (decl);
4444 args = current_template_args ();
4446 if (!ctx
4447 || TREE_CODE (ctx) == FUNCTION_DECL
4448 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4449 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4451 if (DECL_LANG_SPECIFIC (decl)
4452 && DECL_TEMPLATE_INFO (decl)
4453 && DECL_TI_TEMPLATE (decl))
4454 tmpl = DECL_TI_TEMPLATE (decl);
4455 /* If DECL is a TYPE_DECL for a class-template, then there won't
4456 be DECL_LANG_SPECIFIC. The information equivalent to
4457 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4458 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4459 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4460 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4462 /* Since a template declaration already existed for this
4463 class-type, we must be redeclaring it here. Make sure
4464 that the redeclaration is valid. */
4465 redeclare_class_template (TREE_TYPE (decl),
4466 current_template_parms);
4467 /* We don't need to create a new TEMPLATE_DECL; just use the
4468 one we already had. */
4469 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4471 else
4473 tmpl = build_template_decl (decl, current_template_parms,
4474 member_template_p);
4475 new_template_p = 1;
4477 if (DECL_LANG_SPECIFIC (decl)
4478 && DECL_TEMPLATE_SPECIALIZATION (decl))
4480 /* A specialization of a member template of a template
4481 class. */
4482 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4483 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4484 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4488 else
4490 tree a, t, current, parms;
4491 int i;
4492 tree tinfo = get_template_info (decl);
4494 if (!tinfo)
4496 error ("template definition of non-template %q#D", decl);
4497 return error_mark_node;
4500 tmpl = TI_TEMPLATE (tinfo);
4502 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4503 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4504 && DECL_TEMPLATE_SPECIALIZATION (decl)
4505 && DECL_MEMBER_TEMPLATE_P (tmpl))
4507 tree new_tmpl;
4509 /* The declaration is a specialization of a member
4510 template, declared outside the class. Therefore, the
4511 innermost template arguments will be NULL, so we
4512 replace them with the arguments determined by the
4513 earlier call to check_explicit_specialization. */
4514 args = DECL_TI_ARGS (decl);
4516 new_tmpl
4517 = build_template_decl (decl, current_template_parms,
4518 member_template_p);
4519 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4520 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4521 DECL_TI_TEMPLATE (decl) = new_tmpl;
4522 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4523 DECL_TEMPLATE_INFO (new_tmpl)
4524 = build_template_info (tmpl, args);
4526 register_specialization (new_tmpl,
4527 most_general_template (tmpl),
4528 args,
4529 is_friend, 0);
4530 return decl;
4533 /* Make sure the template headers we got make sense. */
4535 parms = DECL_TEMPLATE_PARMS (tmpl);
4536 i = TMPL_PARMS_DEPTH (parms);
4537 if (TMPL_ARGS_DEPTH (args) != i)
4539 error ("expected %d levels of template parms for %q#D, got %d",
4540 i, decl, TMPL_ARGS_DEPTH (args));
4542 else
4543 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4545 a = TMPL_ARGS_LEVEL (args, i);
4546 t = INNERMOST_TEMPLATE_PARMS (parms);
4548 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4550 if (current == decl)
4551 error ("got %d template parameters for %q#D",
4552 TREE_VEC_LENGTH (a), decl);
4553 else
4554 error ("got %d template parameters for %q#T",
4555 TREE_VEC_LENGTH (a), current);
4556 error (" but %d required", TREE_VEC_LENGTH (t));
4557 return error_mark_node;
4560 if (current == decl)
4561 current = ctx;
4562 else if (current == NULL_TREE)
4563 /* Can happen in erroneous input. */
4564 break;
4565 else
4566 current = (TYPE_P (current)
4567 ? TYPE_CONTEXT (current)
4568 : DECL_CONTEXT (current));
4571 /* Check that the parms are used in the appropriate qualifying scopes
4572 in the declarator. */
4573 if (!comp_template_args
4574 (TI_ARGS (tinfo),
4575 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4577 error ("\
4578 template arguments to %qD do not match original template %qD",
4579 decl, DECL_TEMPLATE_RESULT (tmpl));
4580 if (!uses_template_parms (TI_ARGS (tinfo)))
4581 inform (input_location, "use template<> for an explicit specialization");
4582 /* Avoid crash in import_export_decl. */
4583 DECL_INTERFACE_KNOWN (decl) = 1;
4584 return error_mark_node;
4588 DECL_TEMPLATE_RESULT (tmpl) = decl;
4589 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4591 /* Push template declarations for global functions and types. Note
4592 that we do not try to push a global template friend declared in a
4593 template class; such a thing may well depend on the template
4594 parameters of the class. */
4595 if (new_template_p && !ctx
4596 && !(is_friend && template_class_depth (current_class_type) > 0))
4598 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4599 if (tmpl == error_mark_node)
4600 return error_mark_node;
4602 /* Hide template friend classes that haven't been declared yet. */
4603 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4605 DECL_ANTICIPATED (tmpl) = 1;
4606 DECL_FRIEND_P (tmpl) = 1;
4610 if (primary)
4612 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4613 int i;
4615 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4616 if (DECL_CONV_FN_P (tmpl))
4618 int depth = TMPL_PARMS_DEPTH (parms);
4620 /* It is a conversion operator. See if the type converted to
4621 depends on innermost template operands. */
4623 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4624 depth))
4625 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4628 /* Give template template parms a DECL_CONTEXT of the template
4629 for which they are a parameter. */
4630 parms = INNERMOST_TEMPLATE_PARMS (parms);
4631 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4633 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4634 if (TREE_CODE (parm) == TEMPLATE_DECL)
4635 DECL_CONTEXT (parm) = tmpl;
4639 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4640 back to its most general template. If TMPL is a specialization,
4641 ARGS may only have the innermost set of arguments. Add the missing
4642 argument levels if necessary. */
4643 if (DECL_TEMPLATE_INFO (tmpl))
4644 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4646 info = build_template_info (tmpl, args);
4648 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4649 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4650 else if (DECL_LANG_SPECIFIC (decl))
4651 DECL_TEMPLATE_INFO (decl) = info;
4653 return DECL_TEMPLATE_RESULT (tmpl);
4656 tree
4657 push_template_decl (tree decl)
4659 return push_template_decl_real (decl, false);
4662 /* Called when a class template TYPE is redeclared with the indicated
4663 template PARMS, e.g.:
4665 template <class T> struct S;
4666 template <class T> struct S {}; */
4668 bool
4669 redeclare_class_template (tree type, tree parms)
4671 tree tmpl;
4672 tree tmpl_parms;
4673 int i;
4675 if (!TYPE_TEMPLATE_INFO (type))
4677 error ("%qT is not a template type", type);
4678 return false;
4681 tmpl = TYPE_TI_TEMPLATE (type);
4682 if (!PRIMARY_TEMPLATE_P (tmpl))
4683 /* The type is nested in some template class. Nothing to worry
4684 about here; there are no new template parameters for the nested
4685 type. */
4686 return true;
4688 if (!parms)
4690 error ("template specifiers not specified in declaration of %qD",
4691 tmpl);
4692 return false;
4695 parms = INNERMOST_TEMPLATE_PARMS (parms);
4696 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4698 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4700 error_n (input_location, TREE_VEC_LENGTH (parms),
4701 "redeclared with %d template parameter",
4702 "redeclared with %d template parameters",
4703 TREE_VEC_LENGTH (parms));
4704 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4705 "previous declaration %q+D used %d template parameter",
4706 "previous declaration %q+D used %d template parameters",
4707 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4708 return false;
4711 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4713 tree tmpl_parm;
4714 tree parm;
4715 tree tmpl_default;
4716 tree parm_default;
4718 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4719 || TREE_VEC_ELT (parms, i) == error_mark_node)
4720 continue;
4722 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4723 if (tmpl_parm == error_mark_node)
4724 return false;
4726 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4727 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4728 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4730 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4731 TEMPLATE_DECL. */
4732 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4733 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4734 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4735 || (TREE_CODE (tmpl_parm) != PARM_DECL
4736 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4737 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4738 || (TREE_CODE (tmpl_parm) == PARM_DECL
4739 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4740 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4742 error ("template parameter %q+#D", tmpl_parm);
4743 error ("redeclared here as %q#D", parm);
4744 return false;
4747 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4749 /* We have in [temp.param]:
4751 A template-parameter may not be given default arguments
4752 by two different declarations in the same scope. */
4753 error_at (input_location, "redefinition of default argument for %q#D", parm);
4754 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4755 "original definition appeared here");
4756 return false;
4759 if (parm_default != NULL_TREE)
4760 /* Update the previous template parameters (which are the ones
4761 that will really count) with the new default value. */
4762 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4763 else if (tmpl_default != NULL_TREE)
4764 /* Update the new parameters, too; they'll be used as the
4765 parameters for any members. */
4766 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4769 return true;
4772 /* Simplify EXPR if it is a non-dependent expression. Returns the
4773 (possibly simplified) expression. */
4775 tree
4776 fold_non_dependent_expr (tree expr)
4778 if (expr == NULL_TREE)
4779 return NULL_TREE;
4781 /* If we're in a template, but EXPR isn't value dependent, simplify
4782 it. We're supposed to treat:
4784 template <typename T> void f(T[1 + 1]);
4785 template <typename T> void f(T[2]);
4787 as two declarations of the same function, for example. */
4788 if (processing_template_decl
4789 && !type_dependent_expression_p (expr)
4790 && !value_dependent_expression_p (expr))
4792 HOST_WIDE_INT saved_processing_template_decl;
4794 saved_processing_template_decl = processing_template_decl;
4795 processing_template_decl = 0;
4796 expr = tsubst_copy_and_build (expr,
4797 /*args=*/NULL_TREE,
4798 tf_error,
4799 /*in_decl=*/NULL_TREE,
4800 /*function_p=*/false,
4801 /*integral_constant_expression_p=*/true);
4802 processing_template_decl = saved_processing_template_decl;
4804 return expr;
4807 /* EXPR is an expression which is used in a constant-expression context.
4808 For instance, it could be a VAR_DECL with a constant initializer.
4809 Extract the innermost constant expression.
4811 This is basically a more powerful version of
4812 integral_constant_value, which can be used also in templates where
4813 initializers can maintain a syntactic rather than semantic form
4814 (even if they are non-dependent, for access-checking purposes). */
4816 static tree
4817 fold_decl_constant_value (tree expr)
4819 tree const_expr = expr;
4822 expr = fold_non_dependent_expr (const_expr);
4823 const_expr = integral_constant_value (expr);
4825 while (expr != const_expr);
4827 return expr;
4830 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4831 must be a function or a pointer-to-function type, as specified
4832 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4833 and check that the resulting function has external linkage. */
4835 static tree
4836 convert_nontype_argument_function (tree type, tree expr)
4838 tree fns = expr;
4839 tree fn, fn_no_ptr;
4841 fn = instantiate_type (type, fns, tf_none);
4842 if (fn == error_mark_node)
4843 return error_mark_node;
4845 fn_no_ptr = fn;
4846 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4847 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4848 if (TREE_CODE (fn_no_ptr) == BASELINK)
4849 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4851 /* [temp.arg.nontype]/1
4853 A template-argument for a non-type, non-template template-parameter
4854 shall be one of:
4855 [...]
4856 -- the address of an object or function with external linkage. */
4857 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4859 error ("%qE is not a valid template argument for type %qT "
4860 "because function %qD has not external linkage",
4861 expr, type, fn_no_ptr);
4862 return NULL_TREE;
4865 return fn;
4868 /* Subroutine of convert_nontype_argument.
4869 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4870 Emit an error otherwise. */
4872 static bool
4873 check_valid_ptrmem_cst_expr (tree type, tree expr)
4875 STRIP_NOPS (expr);
4876 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4877 return true;
4878 error ("%qE is not a valid template argument for type %qT",
4879 expr, type);
4880 error ("it must be a pointer-to-member of the form `&X::Y'");
4881 return false;
4884 /* Attempt to convert the non-type template parameter EXPR to the
4885 indicated TYPE. If the conversion is successful, return the
4886 converted value. If the conversion is unsuccessful, return
4887 NULL_TREE if we issued an error message, or error_mark_node if we
4888 did not. We issue error messages for out-and-out bad template
4889 parameters, but not simply because the conversion failed, since we
4890 might be just trying to do argument deduction. Both TYPE and EXPR
4891 must be non-dependent.
4893 The conversion follows the special rules described in
4894 [temp.arg.nontype], and it is much more strict than an implicit
4895 conversion.
4897 This function is called twice for each template argument (see
4898 lookup_template_class for a more accurate description of this
4899 problem). This means that we need to handle expressions which
4900 are not valid in a C++ source, but can be created from the
4901 first call (for instance, casts to perform conversions). These
4902 hacks can go away after we fix the double coercion problem. */
4904 static tree
4905 convert_nontype_argument (tree type, tree expr)
4907 tree expr_type;
4909 /* Detect immediately string literals as invalid non-type argument.
4910 This special-case is not needed for correctness (we would easily
4911 catch this later), but only to provide better diagnostic for this
4912 common user mistake. As suggested by DR 100, we do not mention
4913 linkage issues in the diagnostic as this is not the point. */
4914 if (TREE_CODE (expr) == STRING_CST)
4916 error ("%qE is not a valid template argument for type %qT "
4917 "because string literals can never be used in this context",
4918 expr, type);
4919 return NULL_TREE;
4922 /* If we are in a template, EXPR may be non-dependent, but still
4923 have a syntactic, rather than semantic, form. For example, EXPR
4924 might be a SCOPE_REF, rather than the VAR_DECL to which the
4925 SCOPE_REF refers. Preserving the qualifying scope is necessary
4926 so that access checking can be performed when the template is
4927 instantiated -- but here we need the resolved form so that we can
4928 convert the argument. */
4929 expr = fold_non_dependent_expr (expr);
4930 if (error_operand_p (expr))
4931 return error_mark_node;
4932 expr_type = TREE_TYPE (expr);
4934 /* HACK: Due to double coercion, we can get a
4935 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4936 which is the tree that we built on the first call (see
4937 below when coercing to reference to object or to reference to
4938 function). We just strip everything and get to the arg.
4939 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4940 for examples. */
4941 if (TREE_CODE (expr) == NOP_EXPR)
4943 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4945 /* ??? Maybe we could use convert_from_reference here, but we
4946 would need to relax its constraints because the NOP_EXPR
4947 could actually change the type to something more cv-qualified,
4948 and this is not folded by convert_from_reference. */
4949 tree addr = TREE_OPERAND (expr, 0);
4950 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4951 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4952 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4953 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4954 (TREE_TYPE (expr_type),
4955 TREE_TYPE (TREE_TYPE (addr))));
4957 expr = TREE_OPERAND (addr, 0);
4958 expr_type = TREE_TYPE (expr);
4961 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4962 parameter is a pointer to object, through decay and
4963 qualification conversion. Let's strip everything. */
4964 else if (TYPE_PTROBV_P (type))
4966 STRIP_NOPS (expr);
4967 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4968 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4969 /* Skip the ADDR_EXPR only if it is part of the decay for
4970 an array. Otherwise, it is part of the original argument
4971 in the source code. */
4972 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4973 expr = TREE_OPERAND (expr, 0);
4974 expr_type = TREE_TYPE (expr);
4978 /* [temp.arg.nontype]/5, bullet 1
4980 For a non-type template-parameter of integral or enumeration type,
4981 integral promotions (_conv.prom_) and integral conversions
4982 (_conv.integral_) are applied. */
4983 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4985 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4986 return error_mark_node;
4988 expr = fold_decl_constant_value (expr);
4989 /* Notice that there are constant expressions like '4 % 0' which
4990 do not fold into integer constants. */
4991 if (TREE_CODE (expr) != INTEGER_CST)
4993 error ("%qE is not a valid template argument for type %qT "
4994 "because it is a non-constant expression", expr, type);
4995 return NULL_TREE;
4998 /* At this point, an implicit conversion does what we want,
4999 because we already know that the expression is of integral
5000 type. */
5001 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
5002 if (expr == error_mark_node)
5003 return error_mark_node;
5005 /* Conversion was allowed: fold it to a bare integer constant. */
5006 expr = fold (expr);
5008 /* [temp.arg.nontype]/5, bullet 2
5010 For a non-type template-parameter of type pointer to object,
5011 qualification conversions (_conv.qual_) and the array-to-pointer
5012 conversion (_conv.array_) are applied. */
5013 else if (TYPE_PTROBV_P (type))
5015 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5017 A template-argument for a non-type, non-template template-parameter
5018 shall be one of: [...]
5020 -- the name of a non-type template-parameter;
5021 -- the address of an object or function with external linkage, [...]
5022 expressed as "& id-expression" where the & is optional if the name
5023 refers to a function or array, or if the corresponding
5024 template-parameter is a reference.
5026 Here, we do not care about functions, as they are invalid anyway
5027 for a parameter of type pointer-to-object. */
5029 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5030 /* Non-type template parameters are OK. */
5032 else if (TREE_CODE (expr) != ADDR_EXPR
5033 && TREE_CODE (expr_type) != ARRAY_TYPE)
5035 if (TREE_CODE (expr) == VAR_DECL)
5037 error ("%qD is not a valid template argument "
5038 "because %qD is a variable, not the address of "
5039 "a variable",
5040 expr, expr);
5041 return NULL_TREE;
5043 /* Other values, like integer constants, might be valid
5044 non-type arguments of some other type. */
5045 return error_mark_node;
5047 else
5049 tree decl;
5051 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5052 ? TREE_OPERAND (expr, 0) : expr);
5053 if (TREE_CODE (decl) != VAR_DECL)
5055 error ("%qE is not a valid template argument of type %qT "
5056 "because %qE is not a variable",
5057 expr, type, decl);
5058 return NULL_TREE;
5060 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5062 error ("%qE is not a valid template argument of type %qT "
5063 "because %qD does not have external linkage",
5064 expr, type, decl);
5065 return NULL_TREE;
5069 expr = decay_conversion (expr);
5070 if (expr == error_mark_node)
5071 return error_mark_node;
5073 expr = perform_qualification_conversions (type, expr);
5074 if (expr == error_mark_node)
5075 return error_mark_node;
5077 /* [temp.arg.nontype]/5, bullet 3
5079 For a non-type template-parameter of type reference to object, no
5080 conversions apply. The type referred to by the reference may be more
5081 cv-qualified than the (otherwise identical) type of the
5082 template-argument. The template-parameter is bound directly to the
5083 template-argument, which must be an lvalue. */
5084 else if (TYPE_REF_OBJ_P (type))
5086 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5087 expr_type))
5088 return error_mark_node;
5090 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5092 error ("%qE is not a valid template argument for type %qT "
5093 "because of conflicts in cv-qualification", expr, type);
5094 return NULL_TREE;
5097 if (!real_lvalue_p (expr))
5099 error ("%qE is not a valid template argument for type %qT "
5100 "because it is not an lvalue", expr, type);
5101 return NULL_TREE;
5104 /* [temp.arg.nontype]/1
5106 A template-argument for a non-type, non-template template-parameter
5107 shall be one of: [...]
5109 -- the address of an object or function with external linkage. */
5110 if (TREE_CODE (expr) == INDIRECT_REF
5111 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5113 expr = TREE_OPERAND (expr, 0);
5114 if (DECL_P (expr))
5116 error ("%q#D is not a valid template argument for type %qT "
5117 "because a reference variable does not have a constant "
5118 "address", expr, type);
5119 return NULL_TREE;
5123 if (!DECL_P (expr))
5125 error ("%qE is not a valid template argument for type %qT "
5126 "because it is not an object with external linkage",
5127 expr, type);
5128 return NULL_TREE;
5131 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5133 error ("%qE is not a valid template argument for type %qT "
5134 "because object %qD has not external linkage",
5135 expr, type, expr);
5136 return NULL_TREE;
5139 expr = build_nop (type, build_address (expr));
5141 /* [temp.arg.nontype]/5, bullet 4
5143 For a non-type template-parameter of type pointer to function, only
5144 the function-to-pointer conversion (_conv.func_) is applied. If the
5145 template-argument represents a set of overloaded functions (or a
5146 pointer to such), the matching function is selected from the set
5147 (_over.over_). */
5148 else if (TYPE_PTRFN_P (type))
5150 /* If the argument is a template-id, we might not have enough
5151 context information to decay the pointer. */
5152 if (!type_unknown_p (expr_type))
5154 expr = decay_conversion (expr);
5155 if (expr == error_mark_node)
5156 return error_mark_node;
5159 expr = convert_nontype_argument_function (type, expr);
5160 if (!expr || expr == error_mark_node)
5161 return expr;
5163 if (TREE_CODE (expr) != ADDR_EXPR)
5165 error ("%qE is not a valid template argument for type %qT", expr, type);
5166 error ("it must be the address of a function with external linkage");
5167 return NULL_TREE;
5170 /* [temp.arg.nontype]/5, bullet 5
5172 For a non-type template-parameter of type reference to function, no
5173 conversions apply. If the template-argument represents a set of
5174 overloaded functions, the matching function is selected from the set
5175 (_over.over_). */
5176 else if (TYPE_REFFN_P (type))
5178 if (TREE_CODE (expr) == ADDR_EXPR)
5180 error ("%qE is not a valid template argument for type %qT "
5181 "because it is a pointer", expr, type);
5182 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5183 return NULL_TREE;
5186 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5187 if (!expr || expr == error_mark_node)
5188 return expr;
5190 expr = build_nop (type, build_address (expr));
5192 /* [temp.arg.nontype]/5, bullet 6
5194 For a non-type template-parameter of type pointer to member function,
5195 no conversions apply. If the template-argument represents a set of
5196 overloaded member functions, the matching member function is selected
5197 from the set (_over.over_). */
5198 else if (TYPE_PTRMEMFUNC_P (type))
5200 expr = instantiate_type (type, expr, tf_none);
5201 if (expr == error_mark_node)
5202 return error_mark_node;
5204 /* [temp.arg.nontype] bullet 1 says the pointer to member
5205 expression must be a pointer-to-member constant. */
5206 if (!check_valid_ptrmem_cst_expr (type, expr))
5207 return error_mark_node;
5209 /* There is no way to disable standard conversions in
5210 resolve_address_of_overloaded_function (called by
5211 instantiate_type). It is possible that the call succeeded by
5212 converting &B::I to &D::I (where B is a base of D), so we need
5213 to reject this conversion here.
5215 Actually, even if there was a way to disable standard conversions,
5216 it would still be better to reject them here so that we can
5217 provide a superior diagnostic. */
5218 if (!same_type_p (TREE_TYPE (expr), type))
5220 error ("%qE is not a valid template argument for type %qT "
5221 "because it is of type %qT", expr, type,
5222 TREE_TYPE (expr));
5223 /* If we are just one standard conversion off, explain. */
5224 if (can_convert (type, TREE_TYPE (expr)))
5225 inform (input_location,
5226 "standard conversions are not allowed in this context");
5227 return NULL_TREE;
5230 /* [temp.arg.nontype]/5, bullet 7
5232 For a non-type template-parameter of type pointer to data member,
5233 qualification conversions (_conv.qual_) are applied. */
5234 else if (TYPE_PTRMEM_P (type))
5236 /* [temp.arg.nontype] bullet 1 says the pointer to member
5237 expression must be a pointer-to-member constant. */
5238 if (!check_valid_ptrmem_cst_expr (type, expr))
5239 return error_mark_node;
5241 expr = perform_qualification_conversions (type, expr);
5242 if (expr == error_mark_node)
5243 return expr;
5245 /* A template non-type parameter must be one of the above. */
5246 else
5247 gcc_unreachable ();
5249 /* Sanity check: did we actually convert the argument to the
5250 right type? */
5251 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5252 return expr;
5255 /* Subroutine of coerce_template_template_parms, which returns 1 if
5256 PARM_PARM and ARG_PARM match using the rule for the template
5257 parameters of template template parameters. Both PARM and ARG are
5258 template parameters; the rest of the arguments are the same as for
5259 coerce_template_template_parms.
5261 static int
5262 coerce_template_template_parm (tree parm,
5263 tree arg,
5264 tsubst_flags_t complain,
5265 tree in_decl,
5266 tree outer_args)
5268 if (arg == NULL_TREE || arg == error_mark_node
5269 || parm == NULL_TREE || parm == error_mark_node)
5270 return 0;
5272 if (TREE_CODE (arg) != TREE_CODE (parm))
5273 return 0;
5275 switch (TREE_CODE (parm))
5277 case TEMPLATE_DECL:
5278 /* We encounter instantiations of templates like
5279 template <template <template <class> class> class TT>
5280 class C; */
5282 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5283 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5285 if (!coerce_template_template_parms
5286 (parmparm, argparm, complain, in_decl, outer_args))
5287 return 0;
5289 /* Fall through. */
5291 case TYPE_DECL:
5292 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5293 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5294 /* Argument is a parameter pack but parameter is not. */
5295 return 0;
5296 break;
5298 case PARM_DECL:
5299 /* The tsubst call is used to handle cases such as
5301 template <int> class C {};
5302 template <class T, template <T> class TT> class D {};
5303 D<int, C> d;
5305 i.e. the parameter list of TT depends on earlier parameters. */
5306 if (!uses_template_parms (TREE_TYPE (arg))
5307 && !same_type_p
5308 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5309 TREE_TYPE (arg)))
5310 return 0;
5312 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5313 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5314 /* Argument is a parameter pack but parameter is not. */
5315 return 0;
5317 break;
5319 default:
5320 gcc_unreachable ();
5323 return 1;
5327 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5328 template template parameters. Both PARM_PARMS and ARG_PARMS are
5329 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5330 or PARM_DECL.
5332 Consider the example:
5333 template <class T> class A;
5334 template<template <class U> class TT> class B;
5336 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5337 the parameters to A, and OUTER_ARGS contains A. */
5339 static int
5340 coerce_template_template_parms (tree parm_parms,
5341 tree arg_parms,
5342 tsubst_flags_t complain,
5343 tree in_decl,
5344 tree outer_args)
5346 int nparms, nargs, i;
5347 tree parm, arg;
5348 int variadic_p = 0;
5350 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5351 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5353 nparms = TREE_VEC_LENGTH (parm_parms);
5354 nargs = TREE_VEC_LENGTH (arg_parms);
5356 /* Determine whether we have a parameter pack at the end of the
5357 template template parameter's template parameter list. */
5358 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5360 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5362 if (parm == error_mark_node)
5363 return 0;
5365 switch (TREE_CODE (parm))
5367 case TEMPLATE_DECL:
5368 case TYPE_DECL:
5369 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5370 variadic_p = 1;
5371 break;
5373 case PARM_DECL:
5374 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5375 variadic_p = 1;
5376 break;
5378 default:
5379 gcc_unreachable ();
5383 if (nargs != nparms
5384 && !(variadic_p && nargs >= nparms - 1))
5385 return 0;
5387 /* Check all of the template parameters except the parameter pack at
5388 the end (if any). */
5389 for (i = 0; i < nparms - variadic_p; ++i)
5391 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5392 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5393 continue;
5395 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5396 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5398 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5399 outer_args))
5400 return 0;
5404 if (variadic_p)
5406 /* Check each of the template parameters in the template
5407 argument against the template parameter pack at the end of
5408 the template template parameter. */
5409 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5410 return 0;
5412 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5414 for (; i < nargs; ++i)
5416 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5417 continue;
5419 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5421 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5422 outer_args))
5423 return 0;
5427 return 1;
5430 /* Verifies that the deduced template arguments (in TARGS) for the
5431 template template parameters (in TPARMS) represent valid bindings,
5432 by comparing the template parameter list of each template argument
5433 to the template parameter list of its corresponding template
5434 template parameter, in accordance with DR150. This
5435 routine can only be called after all template arguments have been
5436 deduced. It will return TRUE if all of the template template
5437 parameter bindings are okay, FALSE otherwise. */
5438 bool
5439 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5441 int i, ntparms = TREE_VEC_LENGTH (tparms);
5442 bool ret = true;
5444 /* We're dealing with template parms in this process. */
5445 ++processing_template_decl;
5447 targs = INNERMOST_TEMPLATE_ARGS (targs);
5449 for (i = 0; i < ntparms; ++i)
5451 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5452 tree targ = TREE_VEC_ELT (targs, i);
5454 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5456 tree packed_args = NULL_TREE;
5457 int idx, len = 1;
5459 if (ARGUMENT_PACK_P (targ))
5461 /* Look inside the argument pack. */
5462 packed_args = ARGUMENT_PACK_ARGS (targ);
5463 len = TREE_VEC_LENGTH (packed_args);
5466 for (idx = 0; idx < len; ++idx)
5468 tree targ_parms = NULL_TREE;
5470 if (packed_args)
5471 /* Extract the next argument from the argument
5472 pack. */
5473 targ = TREE_VEC_ELT (packed_args, idx);
5475 if (PACK_EXPANSION_P (targ))
5476 /* Look at the pattern of the pack expansion. */
5477 targ = PACK_EXPANSION_PATTERN (targ);
5479 /* Extract the template parameters from the template
5480 argument. */
5481 if (TREE_CODE (targ) == TEMPLATE_DECL)
5482 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5483 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5484 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5486 /* Verify that we can coerce the template template
5487 parameters from the template argument to the template
5488 parameter. This requires an exact match. */
5489 if (targ_parms
5490 && !coerce_template_template_parms
5491 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5492 targ_parms,
5493 tf_none,
5494 tparm,
5495 targs))
5497 ret = false;
5498 goto out;
5504 out:
5506 --processing_template_decl;
5507 return ret;
5510 /* Convert the indicated template ARG as necessary to match the
5511 indicated template PARM. Returns the converted ARG, or
5512 error_mark_node if the conversion was unsuccessful. Error and
5513 warning messages are issued under control of COMPLAIN. This
5514 conversion is for the Ith parameter in the parameter list. ARGS is
5515 the full set of template arguments deduced so far. */
5517 static tree
5518 convert_template_argument (tree parm,
5519 tree arg,
5520 tree args,
5521 tsubst_flags_t complain,
5522 int i,
5523 tree in_decl)
5525 tree orig_arg;
5526 tree val;
5527 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5529 if (TREE_CODE (arg) == TREE_LIST
5530 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5532 /* The template argument was the name of some
5533 member function. That's usually
5534 invalid, but static members are OK. In any
5535 case, grab the underlying fields/functions
5536 and issue an error later if required. */
5537 orig_arg = TREE_VALUE (arg);
5538 TREE_TYPE (arg) = unknown_type_node;
5541 orig_arg = arg;
5543 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5544 requires_type = (TREE_CODE (parm) == TYPE_DECL
5545 || requires_tmpl_type);
5547 /* When determining whether an argument pack expansion is a template,
5548 look at the pattern. */
5549 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5550 arg = PACK_EXPANSION_PATTERN (arg);
5552 /* Deal with an injected-class-name used as a template template arg. */
5553 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5555 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5556 if (TREE_CODE (t) == TEMPLATE_DECL)
5558 if (complain & tf_warning_or_error)
5559 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5560 " used as template template argument", TYPE_NAME (arg));
5561 else if (flag_pedantic_errors)
5562 t = arg;
5564 arg = t;
5568 is_tmpl_type =
5569 ((TREE_CODE (arg) == TEMPLATE_DECL
5570 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5571 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5572 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5574 if (is_tmpl_type
5575 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5576 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5577 arg = TYPE_STUB_DECL (arg);
5579 is_type = TYPE_P (arg) || is_tmpl_type;
5581 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5582 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5584 permerror (input_location, "to refer to a type member of a template parameter, "
5585 "use %<typename %E%>", orig_arg);
5587 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5588 TREE_OPERAND (arg, 1),
5589 typename_type,
5590 complain & tf_error);
5591 arg = orig_arg;
5592 is_type = 1;
5594 if (is_type != requires_type)
5596 if (in_decl)
5598 if (complain & tf_error)
5600 error ("type/value mismatch at argument %d in template "
5601 "parameter list for %qD",
5602 i + 1, in_decl);
5603 if (is_type)
5604 error (" expected a constant of type %qT, got %qT",
5605 TREE_TYPE (parm),
5606 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5607 else if (requires_tmpl_type)
5608 error (" expected a class template, got %qE", orig_arg);
5609 else
5610 error (" expected a type, got %qE", orig_arg);
5613 return error_mark_node;
5615 if (is_tmpl_type ^ requires_tmpl_type)
5617 if (in_decl && (complain & tf_error))
5619 error ("type/value mismatch at argument %d in template "
5620 "parameter list for %qD",
5621 i + 1, in_decl);
5622 if (is_tmpl_type)
5623 error (" expected a type, got %qT", DECL_NAME (arg));
5624 else
5625 error (" expected a class template, got %qT", orig_arg);
5627 return error_mark_node;
5630 if (is_type)
5632 if (requires_tmpl_type)
5634 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5635 /* The number of argument required is not known yet.
5636 Just accept it for now. */
5637 val = TREE_TYPE (arg);
5638 else
5640 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5641 tree argparm;
5643 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5645 if (coerce_template_template_parms (parmparm, argparm,
5646 complain, in_decl,
5647 args))
5649 val = arg;
5651 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5652 TEMPLATE_DECL. */
5653 if (val != error_mark_node)
5655 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5656 val = TREE_TYPE (val);
5657 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5658 val = make_pack_expansion (val);
5661 else
5663 if (in_decl && (complain & tf_error))
5665 error ("type/value mismatch at argument %d in "
5666 "template parameter list for %qD",
5667 i + 1, in_decl);
5668 error (" expected a template of type %qD, got %qT",
5669 parm, orig_arg);
5672 val = error_mark_node;
5676 else
5677 val = orig_arg;
5678 /* We only form one instance of each template specialization.
5679 Therefore, if we use a non-canonical variant (i.e., a
5680 typedef), any future messages referring to the type will use
5681 the typedef, which is confusing if those future uses do not
5682 themselves also use the typedef. */
5683 if (TYPE_P (val))
5684 val = strip_typedefs (val);
5686 else
5688 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5690 if (invalid_nontype_parm_type_p (t, complain))
5691 return error_mark_node;
5693 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5695 if (same_type_p (t, TREE_TYPE (orig_arg)))
5696 val = orig_arg;
5697 else
5699 /* Not sure if this is reachable, but it doesn't hurt
5700 to be robust. */
5701 error ("type mismatch in nontype parameter pack");
5702 val = error_mark_node;
5705 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5706 /* We used to call digest_init here. However, digest_init
5707 will report errors, which we don't want when complain
5708 is zero. More importantly, digest_init will try too
5709 hard to convert things: for example, `0' should not be
5710 converted to pointer type at this point according to
5711 the standard. Accepting this is not merely an
5712 extension, since deciding whether or not these
5713 conversions can occur is part of determining which
5714 function template to call, or whether a given explicit
5715 argument specification is valid. */
5716 val = convert_nontype_argument (t, orig_arg);
5717 else
5718 val = orig_arg;
5720 if (val == NULL_TREE)
5721 val = error_mark_node;
5722 else if (val == error_mark_node && (complain & tf_error))
5723 error ("could not convert template argument %qE to %qT", orig_arg, t);
5725 if (TREE_CODE (val) == SCOPE_REF)
5727 /* Strip typedefs from the SCOPE_REF. */
5728 tree type = strip_typedefs (TREE_TYPE (val));
5729 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5730 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5731 QUALIFIED_NAME_IS_TEMPLATE (val));
5735 return val;
5738 /* Coerces the remaining template arguments in INNER_ARGS (from
5739 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5740 Returns the coerced argument pack. PARM_IDX is the position of this
5741 parameter in the template parameter list. ARGS is the original
5742 template argument list. */
5743 static tree
5744 coerce_template_parameter_pack (tree parms,
5745 int parm_idx,
5746 tree args,
5747 tree inner_args,
5748 int arg_idx,
5749 tree new_args,
5750 int* lost,
5751 tree in_decl,
5752 tsubst_flags_t complain)
5754 tree parm = TREE_VEC_ELT (parms, parm_idx);
5755 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5756 tree packed_args;
5757 tree argument_pack;
5758 tree packed_types = NULL_TREE;
5760 if (arg_idx > nargs)
5761 arg_idx = nargs;
5763 packed_args = make_tree_vec (nargs - arg_idx);
5765 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5766 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5768 /* When the template parameter is a non-type template
5769 parameter pack whose type uses parameter packs, we need
5770 to look at each of the template arguments
5771 separately. Build a vector of the types for these
5772 non-type template parameters in PACKED_TYPES. */
5773 tree expansion
5774 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5775 packed_types = tsubst_pack_expansion (expansion, args,
5776 complain, in_decl);
5778 if (packed_types == error_mark_node)
5779 return error_mark_node;
5781 /* Check that we have the right number of arguments. */
5782 if (arg_idx < nargs
5783 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5784 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5786 int needed_parms
5787 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5788 error ("wrong number of template arguments (%d, should be %d)",
5789 nargs, needed_parms);
5790 return error_mark_node;
5793 /* If we aren't able to check the actual arguments now
5794 (because they haven't been expanded yet), we can at least
5795 verify that all of the types used for the non-type
5796 template parameter pack are, in fact, valid for non-type
5797 template parameters. */
5798 if (arg_idx < nargs
5799 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5801 int j, len = TREE_VEC_LENGTH (packed_types);
5802 for (j = 0; j < len; ++j)
5804 tree t = TREE_VEC_ELT (packed_types, j);
5805 if (invalid_nontype_parm_type_p (t, complain))
5806 return error_mark_node;
5811 /* Convert the remaining arguments, which will be a part of the
5812 parameter pack "parm". */
5813 for (; arg_idx < nargs; ++arg_idx)
5815 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5816 tree actual_parm = TREE_VALUE (parm);
5818 if (packed_types && !PACK_EXPANSION_P (arg))
5820 /* When we have a vector of types (corresponding to the
5821 non-type template parameter pack that uses parameter
5822 packs in its type, as mention above), and the
5823 argument is not an expansion (which expands to a
5824 currently unknown number of arguments), clone the
5825 parm and give it the next type in PACKED_TYPES. */
5826 actual_parm = copy_node (actual_parm);
5827 TREE_TYPE (actual_parm) =
5828 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5831 if (arg != error_mark_node)
5832 arg = convert_template_argument (actual_parm,
5833 arg, new_args, complain, parm_idx,
5834 in_decl);
5835 if (arg == error_mark_node)
5836 (*lost)++;
5837 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5840 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5841 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5842 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5843 else
5845 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5846 TREE_TYPE (argument_pack)
5847 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5848 TREE_CONSTANT (argument_pack) = 1;
5851 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5852 #ifdef ENABLE_CHECKING
5853 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
5854 TREE_VEC_LENGTH (packed_args));
5855 #endif
5856 return argument_pack;
5859 /* Convert all template arguments to their appropriate types, and
5860 return a vector containing the innermost resulting template
5861 arguments. If any error occurs, return error_mark_node. Error and
5862 warning messages are issued under control of COMPLAIN.
5864 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5865 for arguments not specified in ARGS. Otherwise, if
5866 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5867 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5868 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5869 ARGS. */
5871 static tree
5872 coerce_template_parms (tree parms,
5873 tree args,
5874 tree in_decl,
5875 tsubst_flags_t complain,
5876 bool require_all_args,
5877 bool use_default_args)
5879 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5880 tree inner_args;
5881 tree new_args;
5882 tree new_inner_args;
5883 int saved_unevaluated_operand;
5884 int saved_inhibit_evaluation_warnings;
5886 /* When used as a boolean value, indicates whether this is a
5887 variadic template parameter list. Since it's an int, we can also
5888 subtract it from nparms to get the number of non-variadic
5889 parameters. */
5890 int variadic_p = 0;
5892 nparms = TREE_VEC_LENGTH (parms);
5894 /* Determine if there are any parameter packs. */
5895 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5897 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5898 if (template_parameter_pack_p (tparm))
5899 ++variadic_p;
5902 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5903 /* If there are 0 or 1 parameter packs, we need to expand any argument
5904 packs so that we can deduce a parameter pack from some non-packed args
5905 followed by an argument pack, as in variadic85.C. If there are more
5906 than that, we need to leave argument packs intact so the arguments are
5907 assigned to the right parameter packs. This should only happen when
5908 dealing with a nested class inside a partial specialization of a class
5909 template, as in variadic92.C. */
5910 if (variadic_p <= 1)
5911 inner_args = expand_template_argument_pack (inner_args);
5913 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5914 if ((nargs > nparms && !variadic_p)
5915 || (nargs < nparms - variadic_p
5916 && require_all_args
5917 && (!use_default_args
5918 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5919 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5921 if (complain & tf_error)
5923 const char *or_more = "";
5924 if (variadic_p)
5926 or_more = " or more";
5927 --nparms;
5930 error ("wrong number of template arguments (%d, should be %d%s)",
5931 nargs, nparms, or_more);
5933 if (in_decl)
5934 error ("provided for %q+D", in_decl);
5937 return error_mark_node;
5940 /* We need to evaluate the template arguments, even though this
5941 template-id may be nested within a "sizeof". */
5942 saved_unevaluated_operand = cp_unevaluated_operand;
5943 cp_unevaluated_operand = 0;
5944 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5945 c_inhibit_evaluation_warnings = 0;
5946 new_inner_args = make_tree_vec (nparms);
5947 new_args = add_outermost_template_args (args, new_inner_args);
5948 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5950 tree arg;
5951 tree parm;
5953 /* Get the Ith template parameter. */
5954 parm = TREE_VEC_ELT (parms, parm_idx);
5956 if (parm == error_mark_node)
5958 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5959 continue;
5962 /* Calculate the next argument. */
5963 if (arg_idx < nargs)
5964 arg = TREE_VEC_ELT (inner_args, arg_idx);
5965 else
5966 arg = NULL_TREE;
5968 if (template_parameter_pack_p (TREE_VALUE (parm))
5969 && !(arg && ARGUMENT_PACK_P (arg)))
5971 /* All remaining arguments will be placed in the
5972 template parameter pack PARM. */
5973 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5974 inner_args, arg_idx,
5975 new_args, &lost,
5976 in_decl, complain);
5978 /* Store this argument. */
5979 if (arg == error_mark_node)
5980 lost++;
5981 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5983 /* We are done with all of the arguments. */
5984 arg_idx = nargs;
5986 continue;
5988 else if (arg)
5990 if (PACK_EXPANSION_P (arg))
5992 if (complain & tf_error)
5994 /* FIXME this restriction was removed by N2555; see
5995 bug 35722. */
5996 /* If ARG is a pack expansion, but PARM is not a
5997 template parameter pack (if it were, we would have
5998 handled it above), we're trying to expand into a
5999 fixed-length argument list. */
6000 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6001 sorry ("cannot expand %<%E%> into a fixed-length "
6002 "argument list", arg);
6003 else
6004 sorry ("cannot expand %<%T%> into a fixed-length "
6005 "argument list", arg);
6007 return error_mark_node;
6010 else if (require_all_args)
6012 /* There must be a default arg in this case. */
6013 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6014 complain, in_decl);
6015 /* The position of the first default template argument,
6016 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6017 Record that. */
6018 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6019 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6021 else
6022 break;
6024 if (arg == error_mark_node)
6026 if (complain & tf_error)
6027 error ("template argument %d is invalid", arg_idx + 1);
6029 else if (!arg)
6030 /* This only occurs if there was an error in the template
6031 parameter list itself (which we would already have
6032 reported) that we are trying to recover from, e.g., a class
6033 template with a parameter list such as
6034 template<typename..., typename>. */
6035 return error_mark_node;
6036 else
6037 arg = convert_template_argument (TREE_VALUE (parm),
6038 arg, new_args, complain,
6039 parm_idx, in_decl);
6041 if (arg == error_mark_node)
6042 lost++;
6043 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6045 cp_unevaluated_operand = saved_unevaluated_operand;
6046 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6048 if (lost)
6049 return error_mark_node;
6051 #ifdef ENABLE_CHECKING
6052 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6053 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6054 TREE_VEC_LENGTH (new_inner_args));
6055 #endif
6057 return new_inner_args;
6060 /* Returns 1 if template args OT and NT are equivalent. */
6062 static int
6063 template_args_equal (tree ot, tree nt)
6065 if (nt == ot)
6066 return 1;
6068 if (TREE_CODE (nt) == TREE_VEC)
6069 /* For member templates */
6070 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6071 else if (PACK_EXPANSION_P (ot))
6072 return PACK_EXPANSION_P (nt)
6073 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6074 PACK_EXPANSION_PATTERN (nt));
6075 else if (ARGUMENT_PACK_P (ot))
6077 int i, len;
6078 tree opack, npack;
6080 if (!ARGUMENT_PACK_P (nt))
6081 return 0;
6083 opack = ARGUMENT_PACK_ARGS (ot);
6084 npack = ARGUMENT_PACK_ARGS (nt);
6085 len = TREE_VEC_LENGTH (opack);
6086 if (TREE_VEC_LENGTH (npack) != len)
6087 return 0;
6088 for (i = 0; i < len; ++i)
6089 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6090 TREE_VEC_ELT (npack, i)))
6091 return 0;
6092 return 1;
6094 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6096 /* We get here probably because we are in the middle of substituting
6097 into the pattern of a pack expansion. In that case the
6098 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6099 interested in. So we want to use the initial pack argument for
6100 the comparison. */
6101 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6102 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6103 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6104 return template_args_equal (ot, nt);
6106 else if (TYPE_P (nt))
6107 return TYPE_P (ot) && same_type_p (ot, nt);
6108 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6109 return 0;
6110 else
6111 return cp_tree_equal (ot, nt);
6114 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6115 of template arguments. Returns 0 otherwise. */
6118 comp_template_args (tree oldargs, tree newargs)
6120 int i;
6122 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6123 return 0;
6125 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6127 tree nt = TREE_VEC_ELT (newargs, i);
6128 tree ot = TREE_VEC_ELT (oldargs, i);
6130 if (! template_args_equal (ot, nt))
6131 return 0;
6133 return 1;
6136 static void
6137 add_pending_template (tree d)
6139 tree ti = (TYPE_P (d)
6140 ? CLASSTYPE_TEMPLATE_INFO (d)
6141 : DECL_TEMPLATE_INFO (d));
6142 struct pending_template *pt;
6143 int level;
6145 if (TI_PENDING_TEMPLATE_FLAG (ti))
6146 return;
6148 /* We are called both from instantiate_decl, where we've already had a
6149 tinst_level pushed, and instantiate_template, where we haven't.
6150 Compensate. */
6151 level = !current_tinst_level || current_tinst_level->decl != d;
6153 if (level)
6154 push_tinst_level (d);
6156 pt = GGC_NEW (struct pending_template);
6157 pt->next = NULL;
6158 pt->tinst = current_tinst_level;
6159 if (last_pending_template)
6160 last_pending_template->next = pt;
6161 else
6162 pending_templates = pt;
6164 last_pending_template = pt;
6166 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6168 if (level)
6169 pop_tinst_level ();
6173 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6174 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6175 documentation for TEMPLATE_ID_EXPR. */
6177 tree
6178 lookup_template_function (tree fns, tree arglist)
6180 tree type;
6182 if (fns == error_mark_node || arglist == error_mark_node)
6183 return error_mark_node;
6185 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6186 gcc_assert (fns && (is_overloaded_fn (fns)
6187 || TREE_CODE (fns) == IDENTIFIER_NODE));
6189 if (BASELINK_P (fns))
6191 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6192 unknown_type_node,
6193 BASELINK_FUNCTIONS (fns),
6194 arglist);
6195 return fns;
6198 type = TREE_TYPE (fns);
6199 if (TREE_CODE (fns) == OVERLOAD || !type)
6200 type = unknown_type_node;
6202 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6205 /* Within the scope of a template class S<T>, the name S gets bound
6206 (in build_self_reference) to a TYPE_DECL for the class, not a
6207 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6208 or one of its enclosing classes, and that type is a template,
6209 return the associated TEMPLATE_DECL. Otherwise, the original
6210 DECL is returned.
6212 Also handle the case when DECL is a TREE_LIST of ambiguous
6213 injected-class-names from different bases. */
6215 tree
6216 maybe_get_template_decl_from_type_decl (tree decl)
6218 if (decl == NULL_TREE)
6219 return decl;
6221 /* DR 176: A lookup that finds an injected-class-name (10.2
6222 [class.member.lookup]) can result in an ambiguity in certain cases
6223 (for example, if it is found in more than one base class). If all of
6224 the injected-class-names that are found refer to specializations of
6225 the same class template, and if the name is followed by a
6226 template-argument-list, the reference refers to the class template
6227 itself and not a specialization thereof, and is not ambiguous. */
6228 if (TREE_CODE (decl) == TREE_LIST)
6230 tree t, tmpl = NULL_TREE;
6231 for (t = decl; t; t = TREE_CHAIN (t))
6233 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6234 if (!tmpl)
6235 tmpl = elt;
6236 else if (tmpl != elt)
6237 break;
6239 if (tmpl && t == NULL_TREE)
6240 return tmpl;
6241 else
6242 return decl;
6245 return (decl != NULL_TREE
6246 && DECL_SELF_REFERENCE_P (decl)
6247 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6248 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6251 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6252 parameters, find the desired type.
6254 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6256 IN_DECL, if non-NULL, is the template declaration we are trying to
6257 instantiate.
6259 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6260 the class we are looking up.
6262 Issue error and warning messages under control of COMPLAIN.
6264 If the template class is really a local class in a template
6265 function, then the FUNCTION_CONTEXT is the function in which it is
6266 being instantiated.
6268 ??? Note that this function is currently called *twice* for each
6269 template-id: the first time from the parser, while creating the
6270 incomplete type (finish_template_type), and the second type during the
6271 real instantiation (instantiate_template_class). This is surely something
6272 that we want to avoid. It also causes some problems with argument
6273 coercion (see convert_nontype_argument for more information on this). */
6275 tree
6276 lookup_template_class (tree d1,
6277 tree arglist,
6278 tree in_decl,
6279 tree context,
6280 int entering_scope,
6281 tsubst_flags_t complain)
6283 tree templ = NULL_TREE, parmlist;
6284 tree t;
6285 spec_entry **slot;
6286 spec_entry *entry;
6287 spec_entry elt;
6288 hashval_t hash;
6290 timevar_push (TV_NAME_LOOKUP);
6292 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6294 tree value = innermost_non_namespace_value (d1);
6295 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6296 templ = value;
6297 else
6299 if (context)
6300 push_decl_namespace (context);
6301 templ = lookup_name (d1);
6302 templ = maybe_get_template_decl_from_type_decl (templ);
6303 if (context)
6304 pop_decl_namespace ();
6306 if (templ)
6307 context = DECL_CONTEXT (templ);
6309 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6311 tree type = TREE_TYPE (d1);
6313 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6314 an implicit typename for the second A. Deal with it. */
6315 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6316 type = TREE_TYPE (type);
6318 if (CLASSTYPE_TEMPLATE_INFO (type))
6320 templ = CLASSTYPE_TI_TEMPLATE (type);
6321 d1 = DECL_NAME (templ);
6324 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6325 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6327 templ = TYPE_TI_TEMPLATE (d1);
6328 d1 = DECL_NAME (templ);
6330 else if (TREE_CODE (d1) == TEMPLATE_DECL
6331 && DECL_TEMPLATE_RESULT (d1)
6332 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6334 templ = d1;
6335 d1 = DECL_NAME (templ);
6336 context = DECL_CONTEXT (templ);
6339 /* Issue an error message if we didn't find a template. */
6340 if (! templ)
6342 if (complain & tf_error)
6343 error ("%qT is not a template", d1);
6344 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6347 if (TREE_CODE (templ) != TEMPLATE_DECL
6348 /* Make sure it's a user visible template, if it was named by
6349 the user. */
6350 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6351 && !PRIMARY_TEMPLATE_P (templ)))
6353 if (complain & tf_error)
6355 error ("non-template type %qT used as a template", d1);
6356 if (in_decl)
6357 error ("for template declaration %q+D", in_decl);
6359 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6362 complain &= ~tf_user;
6364 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6366 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6367 template arguments */
6369 tree parm;
6370 tree arglist2;
6371 tree outer;
6373 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6375 /* Consider an example where a template template parameter declared as
6377 template <class T, class U = std::allocator<T> > class TT
6379 The template parameter level of T and U are one level larger than
6380 of TT. To proper process the default argument of U, say when an
6381 instantiation `TT<int>' is seen, we need to build the full
6382 arguments containing {int} as the innermost level. Outer levels,
6383 available when not appearing as default template argument, can be
6384 obtained from the arguments of the enclosing template.
6386 Suppose that TT is later substituted with std::vector. The above
6387 instantiation is `TT<int, std::allocator<T> >' with TT at
6388 level 1, and T at level 2, while the template arguments at level 1
6389 becomes {std::vector} and the inner level 2 is {int}. */
6391 outer = DECL_CONTEXT (templ);
6392 if (outer)
6393 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6394 else if (current_template_parms)
6395 /* This is an argument of the current template, so we haven't set
6396 DECL_CONTEXT yet. */
6397 outer = current_template_args ();
6399 if (outer)
6400 arglist = add_to_template_args (outer, arglist);
6402 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6403 complain,
6404 /*require_all_args=*/true,
6405 /*use_default_args=*/true);
6406 if (arglist2 == error_mark_node
6407 || (!uses_template_parms (arglist2)
6408 && check_instantiated_args (templ, arglist2, complain)))
6409 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6411 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6412 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6414 else
6416 tree template_type = TREE_TYPE (templ);
6417 tree gen_tmpl;
6418 tree type_decl;
6419 tree found = NULL_TREE;
6420 int arg_depth;
6421 int parm_depth;
6422 int is_dependent_type;
6423 int use_partial_inst_tmpl = false;
6425 gen_tmpl = most_general_template (templ);
6426 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6427 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6428 arg_depth = TMPL_ARGS_DEPTH (arglist);
6430 if (arg_depth == 1 && parm_depth > 1)
6432 /* We've been given an incomplete set of template arguments.
6433 For example, given:
6435 template <class T> struct S1 {
6436 template <class U> struct S2 {};
6437 template <class U> struct S2<U*> {};
6440 we will be called with an ARGLIST of `U*', but the
6441 TEMPLATE will be `template <class T> template
6442 <class U> struct S1<T>::S2'. We must fill in the missing
6443 arguments. */
6444 arglist
6445 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6446 arglist);
6447 arg_depth = TMPL_ARGS_DEPTH (arglist);
6450 /* Now we should have enough arguments. */
6451 gcc_assert (parm_depth == arg_depth);
6453 /* From here on, we're only interested in the most general
6454 template. */
6456 /* Calculate the BOUND_ARGS. These will be the args that are
6457 actually tsubst'd into the definition to create the
6458 instantiation. */
6459 if (parm_depth > 1)
6461 /* We have multiple levels of arguments to coerce, at once. */
6462 int i;
6463 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6465 tree bound_args = make_tree_vec (parm_depth);
6467 for (i = saved_depth,
6468 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6469 i > 0 && t != NULL_TREE;
6470 --i, t = TREE_CHAIN (t))
6472 tree a = coerce_template_parms (TREE_VALUE (t),
6473 arglist, gen_tmpl,
6474 complain,
6475 /*require_all_args=*/true,
6476 /*use_default_args=*/true);
6478 /* Don't process further if one of the levels fails. */
6479 if (a == error_mark_node)
6481 /* Restore the ARGLIST to its full size. */
6482 TREE_VEC_LENGTH (arglist) = saved_depth;
6483 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6486 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6488 /* We temporarily reduce the length of the ARGLIST so
6489 that coerce_template_parms will see only the arguments
6490 corresponding to the template parameters it is
6491 examining. */
6492 TREE_VEC_LENGTH (arglist)--;
6495 /* Restore the ARGLIST to its full size. */
6496 TREE_VEC_LENGTH (arglist) = saved_depth;
6498 arglist = bound_args;
6500 else
6501 arglist
6502 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6503 INNERMOST_TEMPLATE_ARGS (arglist),
6504 gen_tmpl,
6505 complain,
6506 /*require_all_args=*/true,
6507 /*use_default_args=*/true);
6509 if (arglist == error_mark_node)
6510 /* We were unable to bind the arguments. */
6511 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6513 /* In the scope of a template class, explicit references to the
6514 template class refer to the type of the template, not any
6515 instantiation of it. For example, in:
6517 template <class T> class C { void f(C<T>); }
6519 the `C<T>' is just the same as `C'. Outside of the
6520 class, however, such a reference is an instantiation. */
6521 if ((entering_scope
6522 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6523 || currently_open_class (template_type))
6524 /* comp_template_args is expensive, check it last. */
6525 && comp_template_args (TYPE_TI_ARGS (template_type),
6526 arglist))
6527 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6529 /* If we already have this specialization, return it. */
6530 elt.tmpl = gen_tmpl;
6531 elt.args = arglist;
6532 hash = hash_specialization (&elt);
6533 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6534 &elt, hash);
6536 if (entry)
6537 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6539 is_dependent_type = uses_template_parms (arglist);
6541 /* If the deduced arguments are invalid, then the binding
6542 failed. */
6543 if (!is_dependent_type
6544 && check_instantiated_args (gen_tmpl,
6545 INNERMOST_TEMPLATE_ARGS (arglist),
6546 complain))
6547 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6549 if (!is_dependent_type
6550 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6551 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6552 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6554 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6555 DECL_NAME (gen_tmpl),
6556 /*tag_scope=*/ts_global);
6557 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6560 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6561 complain, in_decl);
6562 if (!context)
6563 context = global_namespace;
6565 /* Create the type. */
6566 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6568 if (!is_dependent_type)
6570 set_current_access_from_decl (TYPE_NAME (template_type));
6571 t = start_enum (TYPE_IDENTIFIER (template_type),
6572 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6573 arglist, complain, in_decl),
6574 SCOPED_ENUM_P (template_type));
6576 else
6578 /* We don't want to call start_enum for this type, since
6579 the values for the enumeration constants may involve
6580 template parameters. And, no one should be interested
6581 in the enumeration constants for such a type. */
6582 t = cxx_make_type (ENUMERAL_TYPE);
6583 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6586 else
6588 t = make_class_type (TREE_CODE (template_type));
6589 CLASSTYPE_DECLARED_CLASS (t)
6590 = CLASSTYPE_DECLARED_CLASS (template_type);
6591 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6592 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6594 /* A local class. Make sure the decl gets registered properly. */
6595 if (context == current_function_decl)
6596 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6598 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6599 /* This instantiation is another name for the primary
6600 template type. Set the TYPE_CANONICAL field
6601 appropriately. */
6602 TYPE_CANONICAL (t) = template_type;
6603 else if (any_template_arguments_need_structural_equality_p (arglist))
6604 /* Some of the template arguments require structural
6605 equality testing, so this template class requires
6606 structural equality testing. */
6607 SET_TYPE_STRUCTURAL_EQUALITY (t);
6610 /* If we called start_enum or pushtag above, this information
6611 will already be set up. */
6612 if (!TYPE_NAME (t))
6614 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6616 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6617 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6618 DECL_SOURCE_LOCATION (type_decl)
6619 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6621 else
6622 type_decl = TYPE_NAME (t);
6624 TREE_PRIVATE (type_decl)
6625 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6626 TREE_PROTECTED (type_decl)
6627 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6628 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6630 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6631 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6634 /* Let's consider the explicit specialization of a member
6635 of a class template specialization that is implicitely instantiated,
6636 e.g.:
6637 template<class T>
6638 struct S
6640 template<class U> struct M {}; //#0
6643 template<>
6644 template<>
6645 struct S<int>::M<char> //#1
6647 int i;
6649 [temp.expl.spec]/4 says this is valid.
6651 In this case, when we write:
6652 S<int>::M<char> m;
6654 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
6655 the one of #0.
6657 When we encounter #1, we want to store the partial instantiation
6658 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
6660 For all cases other than this "explicit specialization of member of a
6661 class template", we just want to store the most general template into
6662 the CLASSTYPE_TI_TEMPLATE of M.
6664 This case of "explicit specialization of member of a class template"
6665 only happens when:
6666 1/ the enclosing class is an instantiation of, and therefore not
6667 the same as, the context of the most general template, and
6668 2/ we aren't looking at the partial instantiation itself, i.e.
6669 the innermost arguments are not the same as the innermost parms of
6670 the most general template.
6672 So it's only when 1/ and 2/ happens that we want to use the partial
6673 instantiation of the member template in lieu of its most general
6674 template. */
6676 if (PRIMARY_TEMPLATE_P (gen_tmpl)
6677 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
6678 /* the enclosing class must be an instantiation... */
6679 && CLASS_TYPE_P (context)
6680 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
6682 tree partial_inst_args;
6683 TREE_VEC_LENGTH (arglist)--;
6684 ++processing_template_decl;
6685 partial_inst_args =
6686 tsubst (INNERMOST_TEMPLATE_ARGS
6687 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
6688 arglist, complain, NULL_TREE);
6689 --processing_template_decl;
6690 TREE_VEC_LENGTH (arglist)++;
6691 use_partial_inst_tmpl =
6692 /*...and we must not be looking at the partial instantiation
6693 itself. */
6694 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
6695 partial_inst_args);
6698 if (!use_partial_inst_tmpl)
6699 /* This case is easy; there are no member templates involved. */
6700 found = gen_tmpl;
6701 else
6703 /* This is a full instantiation of a member template. Find
6704 the partial instantiation of which this is an instance. */
6706 /* Temporarily reduce by one the number of levels in the ARGLIST
6707 so as to avoid comparing the last set of arguments. */
6708 TREE_VEC_LENGTH (arglist)--;
6709 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6710 TREE_VEC_LENGTH (arglist)++;
6711 found = CLASSTYPE_TI_TEMPLATE (found);
6714 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6716 elt.spec = t;
6717 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6718 &elt, hash, INSERT);
6719 *slot = GGC_NEW (spec_entry);
6720 **slot = elt;
6722 /* Note this use of the partial instantiation so we can check it
6723 later in maybe_process_partial_specialization. */
6724 DECL_TEMPLATE_INSTANTIATIONS (templ)
6725 = tree_cons (arglist, t,
6726 DECL_TEMPLATE_INSTANTIATIONS (templ));
6728 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
6729 /* Now that the type has been registered on the instantiations
6730 list, we set up the enumerators. Because the enumeration
6731 constants may involve the enumeration type itself, we make
6732 sure to register the type first, and then create the
6733 constants. That way, doing tsubst_expr for the enumeration
6734 constants won't result in recursive calls here; we'll find
6735 the instantiation and exit above. */
6736 tsubst_enum (template_type, t, arglist);
6738 if (is_dependent_type)
6739 /* If the type makes use of template parameters, the
6740 code that generates debugging information will crash. */
6741 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6743 /* Possibly limit visibility based on template args. */
6744 TREE_PUBLIC (type_decl) = 1;
6745 determine_visibility (type_decl);
6747 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6749 timevar_pop (TV_NAME_LOOKUP);
6752 struct pair_fn_data
6754 tree_fn_t fn;
6755 void *data;
6756 /* True when we should also visit template parameters that occur in
6757 non-deduced contexts. */
6758 bool include_nondeduced_p;
6759 struct pointer_set_t *visited;
6762 /* Called from for_each_template_parm via walk_tree. */
6764 static tree
6765 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6767 tree t = *tp;
6768 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6769 tree_fn_t fn = pfd->fn;
6770 void *data = pfd->data;
6772 if (TYPE_P (t)
6773 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6774 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6775 pfd->include_nondeduced_p))
6776 return error_mark_node;
6778 switch (TREE_CODE (t))
6780 case RECORD_TYPE:
6781 if (TYPE_PTRMEMFUNC_P (t))
6782 break;
6783 /* Fall through. */
6785 case UNION_TYPE:
6786 case ENUMERAL_TYPE:
6787 if (!TYPE_TEMPLATE_INFO (t))
6788 *walk_subtrees = 0;
6789 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6790 fn, data, pfd->visited,
6791 pfd->include_nondeduced_p))
6792 return error_mark_node;
6793 break;
6795 case INTEGER_TYPE:
6796 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6797 fn, data, pfd->visited,
6798 pfd->include_nondeduced_p)
6799 || for_each_template_parm (TYPE_MAX_VALUE (t),
6800 fn, data, pfd->visited,
6801 pfd->include_nondeduced_p))
6802 return error_mark_node;
6803 break;
6805 case METHOD_TYPE:
6806 /* Since we're not going to walk subtrees, we have to do this
6807 explicitly here. */
6808 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6809 pfd->visited, pfd->include_nondeduced_p))
6810 return error_mark_node;
6811 /* Fall through. */
6813 case FUNCTION_TYPE:
6814 /* Check the return type. */
6815 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6816 pfd->include_nondeduced_p))
6817 return error_mark_node;
6819 /* Check the parameter types. Since default arguments are not
6820 instantiated until they are needed, the TYPE_ARG_TYPES may
6821 contain expressions that involve template parameters. But,
6822 no-one should be looking at them yet. And, once they're
6823 instantiated, they don't contain template parameters, so
6824 there's no point in looking at them then, either. */
6826 tree parm;
6828 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6829 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6830 pfd->visited, pfd->include_nondeduced_p))
6831 return error_mark_node;
6833 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6834 want walk_tree walking into them itself. */
6835 *walk_subtrees = 0;
6837 break;
6839 case TYPEOF_TYPE:
6840 if (pfd->include_nondeduced_p
6841 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6842 pfd->visited,
6843 pfd->include_nondeduced_p))
6844 return error_mark_node;
6845 break;
6847 case FUNCTION_DECL:
6848 case VAR_DECL:
6849 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6850 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6851 pfd->visited, pfd->include_nondeduced_p))
6852 return error_mark_node;
6853 /* Fall through. */
6855 case PARM_DECL:
6856 case CONST_DECL:
6857 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6858 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6859 pfd->visited, pfd->include_nondeduced_p))
6860 return error_mark_node;
6861 if (DECL_CONTEXT (t)
6862 && pfd->include_nondeduced_p
6863 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6864 pfd->visited, pfd->include_nondeduced_p))
6865 return error_mark_node;
6866 break;
6868 case BOUND_TEMPLATE_TEMPLATE_PARM:
6869 /* Record template parameters such as `T' inside `TT<T>'. */
6870 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6871 pfd->include_nondeduced_p))
6872 return error_mark_node;
6873 /* Fall through. */
6875 case TEMPLATE_TEMPLATE_PARM:
6876 case TEMPLATE_TYPE_PARM:
6877 case TEMPLATE_PARM_INDEX:
6878 if (fn && (*fn)(t, data))
6879 return error_mark_node;
6880 else if (!fn)
6881 return error_mark_node;
6882 break;
6884 case TEMPLATE_DECL:
6885 /* A template template parameter is encountered. */
6886 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6887 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6888 pfd->include_nondeduced_p))
6889 return error_mark_node;
6891 /* Already substituted template template parameter */
6892 *walk_subtrees = 0;
6893 break;
6895 case TYPENAME_TYPE:
6896 if (!fn
6897 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6898 data, pfd->visited,
6899 pfd->include_nondeduced_p))
6900 return error_mark_node;
6901 break;
6903 case CONSTRUCTOR:
6904 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6905 && pfd->include_nondeduced_p
6906 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6907 (TREE_TYPE (t)), fn, data,
6908 pfd->visited, pfd->include_nondeduced_p))
6909 return error_mark_node;
6910 break;
6912 case INDIRECT_REF:
6913 case COMPONENT_REF:
6914 /* If there's no type, then this thing must be some expression
6915 involving template parameters. */
6916 if (!fn && !TREE_TYPE (t))
6917 return error_mark_node;
6918 break;
6920 case MODOP_EXPR:
6921 case CAST_EXPR:
6922 case REINTERPRET_CAST_EXPR:
6923 case CONST_CAST_EXPR:
6924 case STATIC_CAST_EXPR:
6925 case DYNAMIC_CAST_EXPR:
6926 case ARROW_EXPR:
6927 case DOTSTAR_EXPR:
6928 case TYPEID_EXPR:
6929 case PSEUDO_DTOR_EXPR:
6930 if (!fn)
6931 return error_mark_node;
6932 break;
6934 default:
6935 break;
6938 /* We didn't find any template parameters we liked. */
6939 return NULL_TREE;
6942 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6943 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6944 call FN with the parameter and the DATA.
6945 If FN returns nonzero, the iteration is terminated, and
6946 for_each_template_parm returns 1. Otherwise, the iteration
6947 continues. If FN never returns a nonzero value, the value
6948 returned by for_each_template_parm is 0. If FN is NULL, it is
6949 considered to be the function which always returns 1.
6951 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6952 parameters that occur in non-deduced contexts. When false, only
6953 visits those template parameters that can be deduced. */
6955 static int
6956 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6957 struct pointer_set_t *visited,
6958 bool include_nondeduced_p)
6960 struct pair_fn_data pfd;
6961 int result;
6963 /* Set up. */
6964 pfd.fn = fn;
6965 pfd.data = data;
6966 pfd.include_nondeduced_p = include_nondeduced_p;
6968 /* Walk the tree. (Conceptually, we would like to walk without
6969 duplicates, but for_each_template_parm_r recursively calls
6970 for_each_template_parm, so we would need to reorganize a fair
6971 bit to use walk_tree_without_duplicates, so we keep our own
6972 visited list.) */
6973 if (visited)
6974 pfd.visited = visited;
6975 else
6976 pfd.visited = pointer_set_create ();
6977 result = cp_walk_tree (&t,
6978 for_each_template_parm_r,
6979 &pfd,
6980 pfd.visited) != NULL_TREE;
6982 /* Clean up. */
6983 if (!visited)
6985 pointer_set_destroy (pfd.visited);
6986 pfd.visited = 0;
6989 return result;
6992 /* Returns true if T depends on any template parameter. */
6995 uses_template_parms (tree t)
6997 bool dependent_p;
6998 int saved_processing_template_decl;
7000 saved_processing_template_decl = processing_template_decl;
7001 if (!saved_processing_template_decl)
7002 processing_template_decl = 1;
7003 if (TYPE_P (t))
7004 dependent_p = dependent_type_p (t);
7005 else if (TREE_CODE (t) == TREE_VEC)
7006 dependent_p = any_dependent_template_arguments_p (t);
7007 else if (TREE_CODE (t) == TREE_LIST)
7008 dependent_p = (uses_template_parms (TREE_VALUE (t))
7009 || uses_template_parms (TREE_CHAIN (t)));
7010 else if (TREE_CODE (t) == TYPE_DECL)
7011 dependent_p = dependent_type_p (TREE_TYPE (t));
7012 else if (DECL_P (t)
7013 || EXPR_P (t)
7014 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7015 || TREE_CODE (t) == OVERLOAD
7016 || TREE_CODE (t) == BASELINK
7017 || TREE_CODE (t) == IDENTIFIER_NODE
7018 || TREE_CODE (t) == TRAIT_EXPR
7019 || TREE_CODE (t) == CONSTRUCTOR
7020 || CONSTANT_CLASS_P (t))
7021 dependent_p = (type_dependent_expression_p (t)
7022 || value_dependent_expression_p (t));
7023 else
7025 gcc_assert (t == error_mark_node);
7026 dependent_p = false;
7029 processing_template_decl = saved_processing_template_decl;
7031 return dependent_p;
7034 /* Returns true if T depends on any template parameter with level LEVEL. */
7037 uses_template_parms_level (tree t, int level)
7039 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7040 /*include_nondeduced_p=*/true);
7043 static int tinst_depth;
7044 extern int max_tinst_depth;
7045 #ifdef GATHER_STATISTICS
7046 int depth_reached;
7047 #endif
7048 static int tinst_level_tick;
7049 static int last_template_error_tick;
7051 /* We're starting to instantiate D; record the template instantiation context
7052 for diagnostics and to restore it later. */
7055 push_tinst_level (tree d)
7057 struct tinst_level *new_level;
7059 if (tinst_depth >= max_tinst_depth)
7061 /* If the instantiation in question still has unbound template parms,
7062 we don't really care if we can't instantiate it, so just return.
7063 This happens with base instantiation for implicit `typename'. */
7064 if (uses_template_parms (d))
7065 return 0;
7067 last_template_error_tick = tinst_level_tick;
7068 error ("template instantiation depth exceeds maximum of %d (use "
7069 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7070 max_tinst_depth, d);
7072 print_instantiation_context ();
7074 return 0;
7077 new_level = GGC_NEW (struct tinst_level);
7078 new_level->decl = d;
7079 new_level->locus = input_location;
7080 new_level->in_system_header_p = in_system_header;
7081 new_level->next = current_tinst_level;
7082 current_tinst_level = new_level;
7084 ++tinst_depth;
7085 #ifdef GATHER_STATISTICS
7086 if (tinst_depth > depth_reached)
7087 depth_reached = tinst_depth;
7088 #endif
7090 ++tinst_level_tick;
7091 return 1;
7094 /* We're done instantiating this template; return to the instantiation
7095 context. */
7097 void
7098 pop_tinst_level (void)
7100 /* Restore the filename and line number stashed away when we started
7101 this instantiation. */
7102 input_location = current_tinst_level->locus;
7103 current_tinst_level = current_tinst_level->next;
7104 --tinst_depth;
7105 ++tinst_level_tick;
7108 /* We're instantiating a deferred template; restore the template
7109 instantiation context in which the instantiation was requested, which
7110 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7112 static tree
7113 reopen_tinst_level (struct tinst_level *level)
7115 struct tinst_level *t;
7117 tinst_depth = 0;
7118 for (t = level; t; t = t->next)
7119 ++tinst_depth;
7121 current_tinst_level = level;
7122 pop_tinst_level ();
7123 return level->decl;
7126 /* Returns the TINST_LEVEL which gives the original instantiation
7127 context. */
7129 struct tinst_level *
7130 outermost_tinst_level (void)
7132 struct tinst_level *level = current_tinst_level;
7133 if (level)
7134 while (level->next)
7135 level = level->next;
7136 return level;
7139 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7141 bool
7142 parameter_of_template_p (tree parm, tree templ)
7144 tree parms;
7145 int i;
7147 if (!parm || !templ)
7148 return false;
7150 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7151 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7153 parms = DECL_TEMPLATE_PARMS (templ);
7154 parms = INNERMOST_TEMPLATE_PARMS (parms);
7156 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7157 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7158 return true;
7160 return false;
7163 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7164 vector of template arguments, as for tsubst.
7166 Returns an appropriate tsubst'd friend declaration. */
7168 static tree
7169 tsubst_friend_function (tree decl, tree args)
7171 tree new_friend;
7173 if (TREE_CODE (decl) == FUNCTION_DECL
7174 && DECL_TEMPLATE_INSTANTIATION (decl)
7175 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7176 /* This was a friend declared with an explicit template
7177 argument list, e.g.:
7179 friend void f<>(T);
7181 to indicate that f was a template instantiation, not a new
7182 function declaration. Now, we have to figure out what
7183 instantiation of what template. */
7185 tree template_id, arglist, fns;
7186 tree new_args;
7187 tree tmpl;
7188 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7190 /* Friend functions are looked up in the containing namespace scope.
7191 We must enter that scope, to avoid finding member functions of the
7192 current class with same name. */
7193 push_nested_namespace (ns);
7194 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7195 tf_warning_or_error, NULL_TREE,
7196 /*integral_constant_expression_p=*/false);
7197 pop_nested_namespace (ns);
7198 arglist = tsubst (DECL_TI_ARGS (decl), args,
7199 tf_warning_or_error, NULL_TREE);
7200 template_id = lookup_template_function (fns, arglist);
7202 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7203 tmpl = determine_specialization (template_id, new_friend,
7204 &new_args,
7205 /*need_member_template=*/0,
7206 TREE_VEC_LENGTH (args),
7207 tsk_none);
7208 return instantiate_template (tmpl, new_args, tf_error);
7211 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7213 /* The NEW_FRIEND will look like an instantiation, to the
7214 compiler, but is not an instantiation from the point of view of
7215 the language. For example, we might have had:
7217 template <class T> struct S {
7218 template <class U> friend void f(T, U);
7221 Then, in S<int>, template <class U> void f(int, U) is not an
7222 instantiation of anything. */
7223 if (new_friend == error_mark_node)
7224 return error_mark_node;
7226 DECL_USE_TEMPLATE (new_friend) = 0;
7227 if (TREE_CODE (decl) == TEMPLATE_DECL)
7229 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7230 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7231 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7234 /* The mangled name for the NEW_FRIEND is incorrect. The function
7235 is not a template instantiation and should not be mangled like
7236 one. Therefore, we forget the mangling here; we'll recompute it
7237 later if we need it. */
7238 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7240 SET_DECL_RTL (new_friend, NULL_RTX);
7241 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7244 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7246 tree old_decl;
7247 tree new_friend_template_info;
7248 tree new_friend_result_template_info;
7249 tree ns;
7250 int new_friend_is_defn;
7252 /* We must save some information from NEW_FRIEND before calling
7253 duplicate decls since that function will free NEW_FRIEND if
7254 possible. */
7255 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7256 new_friend_is_defn =
7257 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7258 (template_for_substitution (new_friend)))
7259 != NULL_TREE);
7260 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7262 /* This declaration is a `primary' template. */
7263 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7265 new_friend_result_template_info
7266 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7268 else
7269 new_friend_result_template_info = NULL_TREE;
7271 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7272 if (new_friend_is_defn)
7273 DECL_INITIAL (new_friend) = error_mark_node;
7275 /* Inside pushdecl_namespace_level, we will push into the
7276 current namespace. However, the friend function should go
7277 into the namespace of the template. */
7278 ns = decl_namespace_context (new_friend);
7279 push_nested_namespace (ns);
7280 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7281 pop_nested_namespace (ns);
7283 if (old_decl == error_mark_node)
7284 return error_mark_node;
7286 if (old_decl != new_friend)
7288 /* This new friend declaration matched an existing
7289 declaration. For example, given:
7291 template <class T> void f(T);
7292 template <class U> class C {
7293 template <class T> friend void f(T) {}
7296 the friend declaration actually provides the definition
7297 of `f', once C has been instantiated for some type. So,
7298 old_decl will be the out-of-class template declaration,
7299 while new_friend is the in-class definition.
7301 But, if `f' was called before this point, the
7302 instantiation of `f' will have DECL_TI_ARGS corresponding
7303 to `T' but not to `U', references to which might appear
7304 in the definition of `f'. Previously, the most general
7305 template for an instantiation of `f' was the out-of-class
7306 version; now it is the in-class version. Therefore, we
7307 run through all specialization of `f', adding to their
7308 DECL_TI_ARGS appropriately. In particular, they need a
7309 new set of outer arguments, corresponding to the
7310 arguments for this class instantiation.
7312 The same situation can arise with something like this:
7314 friend void f(int);
7315 template <class T> class C {
7316 friend void f(T) {}
7319 when `C<int>' is instantiated. Now, `f(int)' is defined
7320 in the class. */
7322 if (!new_friend_is_defn)
7323 /* On the other hand, if the in-class declaration does
7324 *not* provide a definition, then we don't want to alter
7325 existing definitions. We can just leave everything
7326 alone. */
7328 else
7330 tree new_template = TI_TEMPLATE (new_friend_template_info);
7331 tree new_args = TI_ARGS (new_friend_template_info);
7333 /* Overwrite whatever template info was there before, if
7334 any, with the new template information pertaining to
7335 the declaration. */
7336 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7338 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7340 /* We should have called reregister_specialization in
7341 duplicate_decls. */
7342 gcc_assert (retrieve_specialization (new_template,
7343 new_args, 0)
7344 == old_decl);
7346 /* Instantiate it if the global has already been used. */
7347 if (DECL_ODR_USED (old_decl))
7348 instantiate_decl (old_decl, /*defer_ok=*/true,
7349 /*expl_inst_class_mem_p=*/false);
7351 else
7353 tree t;
7355 /* Indicate that the old function template is a partial
7356 instantiation. */
7357 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7358 = new_friend_result_template_info;
7360 gcc_assert (new_template
7361 == most_general_template (new_template));
7362 gcc_assert (new_template != old_decl);
7364 /* Reassign any specializations already in the hash table
7365 to the new more general template, and add the
7366 additional template args. */
7367 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7368 t != NULL_TREE;
7369 t = TREE_CHAIN (t))
7371 tree spec = TREE_VALUE (t);
7372 spec_entry elt;
7374 elt.tmpl = old_decl;
7375 elt.args = DECL_TI_ARGS (spec);
7376 elt.spec = NULL_TREE;
7378 htab_remove_elt (decl_specializations, &elt);
7380 DECL_TI_ARGS (spec)
7381 = add_outermost_template_args (new_args,
7382 DECL_TI_ARGS (spec));
7384 register_specialization
7385 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7388 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7392 /* The information from NEW_FRIEND has been merged into OLD_DECL
7393 by duplicate_decls. */
7394 new_friend = old_decl;
7397 else
7399 tree context = DECL_CONTEXT (new_friend);
7400 bool dependent_p;
7402 /* In the code
7403 template <class T> class C {
7404 template <class U> friend void C1<U>::f (); // case 1
7405 friend void C2<T>::f (); // case 2
7407 we only need to make sure CONTEXT is a complete type for
7408 case 2. To distinguish between the two cases, we note that
7409 CONTEXT of case 1 remains dependent type after tsubst while
7410 this isn't true for case 2. */
7411 ++processing_template_decl;
7412 dependent_p = dependent_type_p (context);
7413 --processing_template_decl;
7415 if (!dependent_p
7416 && !complete_type_or_else (context, NULL_TREE))
7417 return error_mark_node;
7419 if (COMPLETE_TYPE_P (context))
7421 /* Check to see that the declaration is really present, and,
7422 possibly obtain an improved declaration. */
7423 tree fn = check_classfn (context,
7424 new_friend, NULL_TREE);
7426 if (fn)
7427 new_friend = fn;
7431 return new_friend;
7434 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7435 template arguments, as for tsubst.
7437 Returns an appropriate tsubst'd friend type or error_mark_node on
7438 failure. */
7440 static tree
7441 tsubst_friend_class (tree friend_tmpl, tree args)
7443 tree friend_type;
7444 tree tmpl;
7445 tree context;
7447 context = DECL_CONTEXT (friend_tmpl);
7449 if (context)
7451 if (TREE_CODE (context) == NAMESPACE_DECL)
7452 push_nested_namespace (context);
7453 else
7454 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7457 /* Look for a class template declaration. We look for hidden names
7458 because two friend declarations of the same template are the
7459 same. For example, in:
7461 struct A {
7462 template <typename> friend class F;
7464 template <typename> struct B {
7465 template <typename> friend class F;
7468 both F templates are the same. */
7469 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7470 /*block_p=*/true, 0,
7471 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7473 /* But, if we don't find one, it might be because we're in a
7474 situation like this:
7476 template <class T>
7477 struct S {
7478 template <class U>
7479 friend struct S;
7482 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7483 for `S<int>', not the TEMPLATE_DECL. */
7484 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7486 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7487 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7490 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7492 /* The friend template has already been declared. Just
7493 check to see that the declarations match, and install any new
7494 default parameters. We must tsubst the default parameters,
7495 of course. We only need the innermost template parameters
7496 because that is all that redeclare_class_template will look
7497 at. */
7498 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7499 > TMPL_ARGS_DEPTH (args))
7501 tree parms;
7502 location_t saved_input_location;
7503 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7504 args, tf_warning_or_error);
7506 saved_input_location = input_location;
7507 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7508 redeclare_class_template (TREE_TYPE (tmpl), parms);
7509 input_location = saved_input_location;
7513 friend_type = TREE_TYPE (tmpl);
7515 else
7517 /* The friend template has not already been declared. In this
7518 case, the instantiation of the template class will cause the
7519 injection of this template into the global scope. */
7520 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7521 if (tmpl == error_mark_node)
7522 return error_mark_node;
7524 /* The new TMPL is not an instantiation of anything, so we
7525 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7526 the new type because that is supposed to be the corresponding
7527 template decl, i.e., TMPL. */
7528 DECL_USE_TEMPLATE (tmpl) = 0;
7529 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7530 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7531 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7532 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7534 /* Inject this template into the global scope. */
7535 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7538 if (context)
7540 if (TREE_CODE (context) == NAMESPACE_DECL)
7541 pop_nested_namespace (context);
7542 else
7543 pop_nested_class ();
7546 return friend_type;
7549 /* Returns zero if TYPE cannot be completed later due to circularity.
7550 Otherwise returns one. */
7552 static int
7553 can_complete_type_without_circularity (tree type)
7555 if (type == NULL_TREE || type == error_mark_node)
7556 return 0;
7557 else if (COMPLETE_TYPE_P (type))
7558 return 1;
7559 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7560 return can_complete_type_without_circularity (TREE_TYPE (type));
7561 else if (CLASS_TYPE_P (type)
7562 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7563 return 0;
7564 else
7565 return 1;
7568 /* Apply any attributes which had to be deferred until instantiation
7569 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7570 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7572 static void
7573 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7574 tree args, tsubst_flags_t complain, tree in_decl)
7576 tree last_dep = NULL_TREE;
7577 tree t;
7578 tree *p;
7580 for (t = attributes; t; t = TREE_CHAIN (t))
7581 if (ATTR_IS_DEPENDENT (t))
7583 last_dep = t;
7584 attributes = copy_list (attributes);
7585 break;
7588 if (DECL_P (*decl_p))
7590 if (TREE_TYPE (*decl_p) == error_mark_node)
7591 return;
7592 p = &DECL_ATTRIBUTES (*decl_p);
7594 else
7595 p = &TYPE_ATTRIBUTES (*decl_p);
7597 if (last_dep)
7599 tree late_attrs = NULL_TREE;
7600 tree *q = &late_attrs;
7602 for (*p = attributes; *p; )
7604 t = *p;
7605 if (ATTR_IS_DEPENDENT (t))
7607 *p = TREE_CHAIN (t);
7608 TREE_CHAIN (t) = NULL_TREE;
7609 /* If the first attribute argument is an identifier, don't
7610 pass it through tsubst. Attributes like mode, format,
7611 cleanup and several target specific attributes expect it
7612 unmodified. */
7613 if (TREE_VALUE (t)
7614 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7615 && TREE_VALUE (TREE_VALUE (t))
7616 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7617 == IDENTIFIER_NODE))
7619 tree chain
7620 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7621 in_decl,
7622 /*integral_constant_expression_p=*/false);
7623 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7624 TREE_VALUE (t)
7625 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7626 chain);
7628 else
7629 TREE_VALUE (t)
7630 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7631 /*integral_constant_expression_p=*/false);
7632 *q = t;
7633 q = &TREE_CHAIN (t);
7635 else
7636 p = &TREE_CHAIN (t);
7639 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7643 /* Perform (or defer) access check for typedefs that were referenced
7644 from within the template TMPL code.
7645 This is a subroutine of instantiate_template and instantiate_class_template.
7646 TMPL is the template to consider and TARGS is the list of arguments of
7647 that template. */
7649 static void
7650 perform_typedefs_access_check (tree tmpl, tree targs)
7652 location_t saved_location;
7653 int i;
7654 qualified_typedef_usage_t *iter;
7656 if (!tmpl
7657 || (!CLASS_TYPE_P (tmpl)
7658 && TREE_CODE (tmpl) != FUNCTION_DECL))
7659 return;
7661 saved_location = input_location;
7662 for (i = 0;
7663 VEC_iterate (qualified_typedef_usage_t,
7664 get_types_needing_access_check (tmpl),
7665 i, iter);
7666 ++i)
7668 tree type_decl = iter->typedef_decl;
7669 tree type_scope = iter->context;
7671 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7672 continue;
7674 if (uses_template_parms (type_decl))
7675 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7676 if (uses_template_parms (type_scope))
7677 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7679 /* Make access check error messages point to the location
7680 of the use of the typedef. */
7681 input_location = iter->locus;
7682 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7683 type_decl, type_decl);
7685 input_location = saved_location;
7688 tree
7689 instantiate_class_template (tree type)
7691 tree templ, args, pattern, t, member;
7692 tree typedecl;
7693 tree pbinfo;
7694 tree base_list;
7695 unsigned int saved_maximum_field_alignment;
7697 if (type == error_mark_node)
7698 return error_mark_node;
7700 if (TYPE_BEING_DEFINED (type)
7701 || COMPLETE_TYPE_P (type)
7702 || uses_template_parms (type))
7703 return type;
7705 /* Figure out which template is being instantiated. */
7706 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7707 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7709 /* Determine what specialization of the original template to
7710 instantiate. */
7711 t = most_specialized_class (type, templ);
7712 if (t == error_mark_node)
7714 TYPE_BEING_DEFINED (type) = 1;
7715 return error_mark_node;
7717 else if (t)
7719 /* This TYPE is actually an instantiation of a partial
7720 specialization. We replace the innermost set of ARGS with
7721 the arguments appropriate for substitution. For example,
7722 given:
7724 template <class T> struct S {};
7725 template <class T> struct S<T*> {};
7727 and supposing that we are instantiating S<int*>, ARGS will
7728 presently be {int*} -- but we need {int}. */
7729 pattern = TREE_TYPE (t);
7730 args = TREE_PURPOSE (t);
7732 else
7734 pattern = TREE_TYPE (templ);
7735 args = CLASSTYPE_TI_ARGS (type);
7738 /* If the template we're instantiating is incomplete, then clearly
7739 there's nothing we can do. */
7740 if (!COMPLETE_TYPE_P (pattern))
7741 return type;
7743 /* If we've recursively instantiated too many templates, stop. */
7744 if (! push_tinst_level (type))
7745 return type;
7747 /* Now we're really doing the instantiation. Mark the type as in
7748 the process of being defined. */
7749 TYPE_BEING_DEFINED (type) = 1;
7751 /* We may be in the middle of deferred access check. Disable
7752 it now. */
7753 push_deferring_access_checks (dk_no_deferred);
7755 push_to_top_level ();
7756 /* Use #pragma pack from the template context. */
7757 saved_maximum_field_alignment = maximum_field_alignment;
7758 maximum_field_alignment = TYPE_PRECISION (pattern);
7760 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7762 /* Set the input location to the most specialized template definition.
7763 This is needed if tsubsting causes an error. */
7764 typedecl = TYPE_MAIN_DECL (pattern);
7765 input_location = DECL_SOURCE_LOCATION (typedecl);
7767 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7768 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7769 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7770 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7771 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7772 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7773 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7774 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7775 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7776 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7777 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7778 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7779 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7780 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7781 if (ANON_AGGR_TYPE_P (pattern))
7782 SET_ANON_AGGR_TYPE_P (type);
7783 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7785 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7786 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7789 pbinfo = TYPE_BINFO (pattern);
7791 /* We should never instantiate a nested class before its enclosing
7792 class; we need to look up the nested class by name before we can
7793 instantiate it, and that lookup should instantiate the enclosing
7794 class. */
7795 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7796 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7797 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7799 base_list = NULL_TREE;
7800 if (BINFO_N_BASE_BINFOS (pbinfo))
7802 tree pbase_binfo;
7803 tree context = TYPE_CONTEXT (type);
7804 tree pushed_scope;
7805 int i;
7807 /* We must enter the scope containing the type, as that is where
7808 the accessibility of types named in dependent bases are
7809 looked up from. */
7810 pushed_scope = push_scope (context ? context : global_namespace);
7812 /* Substitute into each of the bases to determine the actual
7813 basetypes. */
7814 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7816 tree base;
7817 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7818 tree expanded_bases = NULL_TREE;
7819 int idx, len = 1;
7821 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7823 expanded_bases =
7824 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7825 args, tf_error, NULL_TREE);
7826 if (expanded_bases == error_mark_node)
7827 continue;
7829 len = TREE_VEC_LENGTH (expanded_bases);
7832 for (idx = 0; idx < len; idx++)
7834 if (expanded_bases)
7835 /* Extract the already-expanded base class. */
7836 base = TREE_VEC_ELT (expanded_bases, idx);
7837 else
7838 /* Substitute to figure out the base class. */
7839 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7840 NULL_TREE);
7842 if (base == error_mark_node)
7843 continue;
7845 base_list = tree_cons (access, base, base_list);
7846 if (BINFO_VIRTUAL_P (pbase_binfo))
7847 TREE_TYPE (base_list) = integer_type_node;
7851 /* The list is now in reverse order; correct that. */
7852 base_list = nreverse (base_list);
7854 if (pushed_scope)
7855 pop_scope (pushed_scope);
7857 /* Now call xref_basetypes to set up all the base-class
7858 information. */
7859 xref_basetypes (type, base_list);
7861 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7862 (int) ATTR_FLAG_TYPE_IN_PLACE,
7863 args, tf_error, NULL_TREE);
7865 /* Now that our base classes are set up, enter the scope of the
7866 class, so that name lookups into base classes, etc. will work
7867 correctly. This is precisely analogous to what we do in
7868 begin_class_definition when defining an ordinary non-template
7869 class, except we also need to push the enclosing classes. */
7870 push_nested_class (type);
7872 /* Now members are processed in the order of declaration. */
7873 for (member = CLASSTYPE_DECL_LIST (pattern);
7874 member; member = TREE_CHAIN (member))
7876 tree t = TREE_VALUE (member);
7878 if (TREE_PURPOSE (member))
7880 if (TYPE_P (t))
7882 /* Build new CLASSTYPE_NESTED_UTDS. */
7884 tree newtag;
7885 bool class_template_p;
7887 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7888 && TYPE_LANG_SPECIFIC (t)
7889 && CLASSTYPE_IS_TEMPLATE (t));
7890 /* If the member is a class template, then -- even after
7891 substitution -- there may be dependent types in the
7892 template argument list for the class. We increment
7893 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7894 that function will assume that no types are dependent
7895 when outside of a template. */
7896 if (class_template_p)
7897 ++processing_template_decl;
7898 newtag = tsubst (t, args, tf_error, NULL_TREE);
7899 if (class_template_p)
7900 --processing_template_decl;
7901 if (newtag == error_mark_node)
7902 continue;
7904 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7906 tree name = TYPE_IDENTIFIER (t);
7908 if (class_template_p)
7909 /* Unfortunately, lookup_template_class sets
7910 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7911 instantiation (i.e., for the type of a member
7912 template class nested within a template class.)
7913 This behavior is required for
7914 maybe_process_partial_specialization to work
7915 correctly, but is not accurate in this case;
7916 the TAG is not an instantiation of anything.
7917 (The corresponding TEMPLATE_DECL is an
7918 instantiation, but the TYPE is not.) */
7919 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7921 /* Now, we call pushtag to put this NEWTAG into the scope of
7922 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7923 pushtag calling push_template_decl. We don't have to do
7924 this for enums because it will already have been done in
7925 tsubst_enum. */
7926 if (name)
7927 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7928 pushtag (name, newtag, /*tag_scope=*/ts_current);
7931 else if (TREE_CODE (t) == FUNCTION_DECL
7932 || DECL_FUNCTION_TEMPLATE_P (t))
7934 /* Build new TYPE_METHODS. */
7935 tree r;
7937 if (TREE_CODE (t) == TEMPLATE_DECL)
7938 ++processing_template_decl;
7939 r = tsubst (t, args, tf_error, NULL_TREE);
7940 if (TREE_CODE (t) == TEMPLATE_DECL)
7941 --processing_template_decl;
7942 set_current_access_from_decl (r);
7943 finish_member_declaration (r);
7945 else
7947 /* Build new TYPE_FIELDS. */
7948 if (TREE_CODE (t) == STATIC_ASSERT)
7950 tree condition =
7951 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7952 tf_warning_or_error, NULL_TREE,
7953 /*integral_constant_expression_p=*/true);
7954 finish_static_assert (condition,
7955 STATIC_ASSERT_MESSAGE (t),
7956 STATIC_ASSERT_SOURCE_LOCATION (t),
7957 /*member_p=*/true);
7959 else if (TREE_CODE (t) != CONST_DECL)
7961 tree r;
7963 /* The file and line for this declaration, to
7964 assist in error message reporting. Since we
7965 called push_tinst_level above, we don't need to
7966 restore these. */
7967 input_location = DECL_SOURCE_LOCATION (t);
7969 if (TREE_CODE (t) == TEMPLATE_DECL)
7970 ++processing_template_decl;
7971 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7972 if (TREE_CODE (t) == TEMPLATE_DECL)
7973 --processing_template_decl;
7974 if (TREE_CODE (r) == VAR_DECL)
7976 /* In [temp.inst]:
7978 [t]he initialization (and any associated
7979 side-effects) of a static data member does
7980 not occur unless the static data member is
7981 itself used in a way that requires the
7982 definition of the static data member to
7983 exist.
7985 Therefore, we do not substitute into the
7986 initialized for the static data member here. */
7987 finish_static_data_member_decl
7989 /*init=*/NULL_TREE,
7990 /*init_const_expr_p=*/false,
7991 /*asmspec_tree=*/NULL_TREE,
7992 /*flags=*/0);
7993 if (DECL_INITIALIZED_IN_CLASS_P (r))
7994 check_static_variable_definition (r, TREE_TYPE (r));
7996 else if (TREE_CODE (r) == FIELD_DECL)
7998 /* Determine whether R has a valid type and can be
7999 completed later. If R is invalid, then it is
8000 replaced by error_mark_node so that it will not be
8001 added to TYPE_FIELDS. */
8002 tree rtype = TREE_TYPE (r);
8003 if (can_complete_type_without_circularity (rtype))
8004 complete_type (rtype);
8006 if (!COMPLETE_TYPE_P (rtype))
8008 cxx_incomplete_type_error (r, rtype);
8009 r = error_mark_node;
8013 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8014 such a thing will already have been added to the field
8015 list by tsubst_enum in finish_member_declaration in the
8016 CLASSTYPE_NESTED_UTDS case above. */
8017 if (!(TREE_CODE (r) == TYPE_DECL
8018 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8019 && DECL_ARTIFICIAL (r)))
8021 set_current_access_from_decl (r);
8022 finish_member_declaration (r);
8027 else
8029 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8031 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8033 tree friend_type = t;
8034 bool adjust_processing_template_decl = false;
8036 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8038 /* template <class T> friend class C; */
8039 friend_type = tsubst_friend_class (friend_type, args);
8040 adjust_processing_template_decl = true;
8042 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8044 /* template <class T> friend class C::D; */
8045 friend_type = tsubst (friend_type, args,
8046 tf_warning_or_error, NULL_TREE);
8047 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8048 friend_type = TREE_TYPE (friend_type);
8049 adjust_processing_template_decl = true;
8051 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
8053 /* This could be either
8055 friend class T::C;
8057 when dependent_type_p is false or
8059 template <class U> friend class T::C;
8061 otherwise. */
8062 friend_type = tsubst (friend_type, args,
8063 tf_warning_or_error, NULL_TREE);
8064 /* Bump processing_template_decl for correct
8065 dependent_type_p calculation. */
8066 ++processing_template_decl;
8067 if (dependent_type_p (friend_type))
8068 adjust_processing_template_decl = true;
8069 --processing_template_decl;
8071 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8072 && hidden_name_p (TYPE_NAME (friend_type)))
8074 /* friend class C;
8076 where C hasn't been declared yet. Let's lookup name
8077 from namespace scope directly, bypassing any name that
8078 come from dependent base class. */
8079 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8081 /* The call to xref_tag_from_type does injection for friend
8082 classes. */
8083 push_nested_namespace (ns);
8084 friend_type =
8085 xref_tag_from_type (friend_type, NULL_TREE,
8086 /*tag_scope=*/ts_current);
8087 pop_nested_namespace (ns);
8089 else if (uses_template_parms (friend_type))
8090 /* friend class C<T>; */
8091 friend_type = tsubst (friend_type, args,
8092 tf_warning_or_error, NULL_TREE);
8093 /* Otherwise it's
8095 friend class C;
8097 where C is already declared or
8099 friend class C<int>;
8101 We don't have to do anything in these cases. */
8103 if (adjust_processing_template_decl)
8104 /* Trick make_friend_class into realizing that the friend
8105 we're adding is a template, not an ordinary class. It's
8106 important that we use make_friend_class since it will
8107 perform some error-checking and output cross-reference
8108 information. */
8109 ++processing_template_decl;
8111 if (friend_type != error_mark_node)
8112 make_friend_class (type, friend_type, /*complain=*/false);
8114 if (adjust_processing_template_decl)
8115 --processing_template_decl;
8117 else
8119 /* Build new DECL_FRIENDLIST. */
8120 tree r;
8122 /* The file and line for this declaration, to
8123 assist in error message reporting. Since we
8124 called push_tinst_level above, we don't need to
8125 restore these. */
8126 input_location = DECL_SOURCE_LOCATION (t);
8128 if (TREE_CODE (t) == TEMPLATE_DECL)
8130 ++processing_template_decl;
8131 push_deferring_access_checks (dk_no_check);
8134 r = tsubst_friend_function (t, args);
8135 add_friend (type, r, /*complain=*/false);
8136 if (TREE_CODE (t) == TEMPLATE_DECL)
8138 pop_deferring_access_checks ();
8139 --processing_template_decl;
8145 /* Set the file and line number information to whatever is given for
8146 the class itself. This puts error messages involving generated
8147 implicit functions at a predictable point, and the same point
8148 that would be used for non-template classes. */
8149 input_location = DECL_SOURCE_LOCATION (typedecl);
8151 unreverse_member_declarations (type);
8152 finish_struct_1 (type);
8153 TYPE_BEING_DEFINED (type) = 0;
8155 /* Now that the class is complete, instantiate default arguments for
8156 any member functions. We don't do this earlier because the
8157 default arguments may reference members of the class. */
8158 if (!PRIMARY_TEMPLATE_P (templ))
8159 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
8160 if (TREE_CODE (t) == FUNCTION_DECL
8161 /* Implicitly generated member functions will not have template
8162 information; they are not instantiations, but instead are
8163 created "fresh" for each instantiation. */
8164 && DECL_TEMPLATE_INFO (t))
8165 tsubst_default_arguments (t);
8167 /* Some typedefs referenced from within the template code need to be access
8168 checked at template instantiation time, i.e now. These types were
8169 added to the template at parsing time. Let's get those and perform
8170 the access checks then. */
8171 perform_typedefs_access_check (pattern, args);
8172 perform_deferred_access_checks ();
8173 pop_nested_class ();
8174 maximum_field_alignment = saved_maximum_field_alignment;
8175 pop_from_top_level ();
8176 pop_deferring_access_checks ();
8177 pop_tinst_level ();
8179 /* The vtable for a template class can be emitted in any translation
8180 unit in which the class is instantiated. When there is no key
8181 method, however, finish_struct_1 will already have added TYPE to
8182 the keyed_classes list. */
8183 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8184 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8186 return type;
8189 static tree
8190 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8192 tree r;
8194 if (!t)
8195 r = t;
8196 else if (TYPE_P (t))
8197 r = tsubst (t, args, complain, in_decl);
8198 else
8200 r = tsubst_expr (t, args, complain, in_decl,
8201 /*integral_constant_expression_p=*/true);
8202 r = fold_non_dependent_expr (r);
8204 return r;
8207 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8208 NONTYPE_ARGUMENT_PACK. */
8210 static tree
8211 make_fnparm_pack (tree spec_parm)
8213 /* Collect all of the extra "packed" parameters into an
8214 argument pack. */
8215 tree parmvec;
8216 tree parmtypevec;
8217 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8218 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8219 int i, len = list_length (spec_parm);
8221 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8222 parmvec = make_tree_vec (len);
8223 parmtypevec = make_tree_vec (len);
8224 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
8226 TREE_VEC_ELT (parmvec, i) = spec_parm;
8227 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8230 /* Build the argument packs. */
8231 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8232 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8233 TREE_TYPE (argpack) = argtypepack;
8235 return argpack;
8238 /* Substitute ARGS into T, which is an pack expansion
8239 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8240 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8241 (if only a partial substitution could be performed) or
8242 ERROR_MARK_NODE if there was an error. */
8243 tree
8244 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8245 tree in_decl)
8247 tree pattern;
8248 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8249 int i, len = -1;
8250 tree result;
8251 int incomplete = 0;
8252 bool very_local_specializations = false;
8254 gcc_assert (PACK_EXPANSION_P (t));
8255 pattern = PACK_EXPANSION_PATTERN (t);
8257 /* Determine the argument packs that will instantiate the parameter
8258 packs used in the expansion expression. While we're at it,
8259 compute the number of arguments to be expanded and make sure it
8260 is consistent. */
8261 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8262 pack = TREE_CHAIN (pack))
8264 tree parm_pack = TREE_VALUE (pack);
8265 tree arg_pack = NULL_TREE;
8266 tree orig_arg = NULL_TREE;
8268 if (TREE_CODE (parm_pack) == PARM_DECL)
8270 arg_pack = retrieve_local_specialization (parm_pack);
8271 if (arg_pack == NULL_TREE)
8273 /* This can happen for a parameter name used later in a function
8274 declaration (such as in a late-specified return type). Just
8275 make a dummy decl, since it's only used for its type. */
8276 gcc_assert (cp_unevaluated_operand != 0);
8277 arg_pack = tsubst_decl (parm_pack, args, complain);
8278 arg_pack = make_fnparm_pack (arg_pack);
8281 else
8283 int level, idx, levels;
8284 template_parm_level_and_index (parm_pack, &level, &idx);
8286 levels = TMPL_ARGS_DEPTH (args);
8287 if (level <= levels)
8288 arg_pack = TMPL_ARG (args, level, idx);
8291 orig_arg = arg_pack;
8292 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8293 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8295 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8296 /* This can only happen if we forget to expand an argument
8297 pack somewhere else. Just return an error, silently. */
8299 result = make_tree_vec (1);
8300 TREE_VEC_ELT (result, 0) = error_mark_node;
8301 return result;
8304 if (arg_pack
8305 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8306 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8308 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8309 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8310 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8311 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8312 /* The argument pack that the parameter maps to is just an
8313 expansion of the parameter itself, such as one would
8314 find in the implicit typedef of a class inside the
8315 class itself. Consider this parameter "unsubstituted",
8316 so that we will maintain the outer pack expansion. */
8317 arg_pack = NULL_TREE;
8320 if (arg_pack)
8322 int my_len =
8323 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8325 /* It's all-or-nothing with incomplete argument packs. */
8326 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8327 return error_mark_node;
8329 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8330 incomplete = 1;
8332 if (len < 0)
8333 len = my_len;
8334 else if (len != my_len)
8336 if (incomplete)
8337 /* We got explicit args for some packs but not others;
8338 do nothing now and try again after deduction. */
8339 return t;
8340 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8341 error ("mismatched argument pack lengths while expanding "
8342 "%<%T%>",
8343 pattern);
8344 else
8345 error ("mismatched argument pack lengths while expanding "
8346 "%<%E%>",
8347 pattern);
8348 return error_mark_node;
8351 /* Keep track of the parameter packs and their corresponding
8352 argument packs. */
8353 packs = tree_cons (parm_pack, arg_pack, packs);
8354 TREE_TYPE (packs) = orig_arg;
8356 else
8357 /* We can't substitute for this parameter pack. */
8358 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8359 TREE_VALUE (pack),
8360 unsubstituted_packs);
8363 /* We cannot expand this expansion expression, because we don't have
8364 all of the argument packs we need. Substitute into the pattern
8365 and return a PACK_EXPANSION_*. The caller will need to deal with
8366 that. */
8367 if (unsubstituted_packs)
8369 tree new_pat;
8370 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8371 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8372 /*integral_constant_expression_p=*/false);
8373 else
8374 new_pat = tsubst (pattern, args, complain, in_decl);
8375 return make_pack_expansion (new_pat);
8378 /* We could not find any argument packs that work. */
8379 if (len < 0)
8380 return error_mark_node;
8382 if (!local_specializations)
8384 /* We're in a late-specified return type, so we don't have a local
8385 specializations table. Create one for doing this expansion. */
8386 very_local_specializations = true;
8387 local_specializations = htab_create (37,
8388 hash_local_specialization,
8389 eq_local_specializations,
8390 NULL);
8393 /* For each argument in each argument pack, substitute into the
8394 pattern. */
8395 result = make_tree_vec (len + incomplete);
8396 for (i = 0; i < len + incomplete; ++i)
8398 /* For parameter pack, change the substitution of the parameter
8399 pack to the ith argument in its argument pack, then expand
8400 the pattern. */
8401 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8403 tree parm = TREE_PURPOSE (pack);
8405 if (TREE_CODE (parm) == PARM_DECL)
8407 /* Select the Ith argument from the pack. */
8408 tree arg = make_node (ARGUMENT_PACK_SELECT);
8409 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8410 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8411 mark_used (parm);
8412 register_local_specialization (arg, parm);
8414 else
8416 tree value = parm;
8417 int idx, level;
8418 template_parm_level_and_index (parm, &level, &idx);
8420 if (i < len)
8422 /* Select the Ith argument from the pack. */
8423 value = make_node (ARGUMENT_PACK_SELECT);
8424 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8425 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8428 /* Update the corresponding argument. */
8429 TMPL_ARG (args, level, idx) = value;
8433 /* Substitute into the PATTERN with the altered arguments. */
8434 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8435 TREE_VEC_ELT (result, i) =
8436 tsubst_expr (pattern, args, complain, in_decl,
8437 /*integral_constant_expression_p=*/false);
8438 else
8439 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8441 if (i == len)
8442 /* When we have incomplete argument packs, the last "expanded"
8443 result is itself a pack expansion, which allows us
8444 to deduce more arguments. */
8445 TREE_VEC_ELT (result, i) =
8446 make_pack_expansion (TREE_VEC_ELT (result, i));
8448 if (TREE_VEC_ELT (result, i) == error_mark_node)
8450 result = error_mark_node;
8451 break;
8455 /* Update ARGS to restore the substitution from parameter packs to
8456 their argument packs. */
8457 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8459 tree parm = TREE_PURPOSE (pack);
8461 if (TREE_CODE (parm) == PARM_DECL)
8462 register_local_specialization (TREE_TYPE (pack), parm);
8463 else
8465 int idx, level;
8466 template_parm_level_and_index (parm, &level, &idx);
8468 /* Update the corresponding argument. */
8469 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8470 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8471 TREE_TYPE (pack);
8472 else
8473 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8477 if (very_local_specializations)
8479 htab_delete (local_specializations);
8480 local_specializations = NULL;
8483 return result;
8486 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8487 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8488 parameter packs; all parms generated from a function parameter pack will
8489 have the same DECL_PARM_INDEX. */
8491 tree
8492 get_pattern_parm (tree parm, tree tmpl)
8494 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8495 tree patparm;
8497 if (DECL_ARTIFICIAL (parm))
8499 for (patparm = DECL_ARGUMENTS (pattern);
8500 patparm; patparm = TREE_CHAIN (patparm))
8501 if (DECL_ARTIFICIAL (patparm)
8502 && DECL_NAME (parm) == DECL_NAME (patparm))
8503 break;
8505 else
8507 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8508 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8509 gcc_assert (DECL_PARM_INDEX (patparm)
8510 == DECL_PARM_INDEX (parm));
8513 return patparm;
8516 /* Substitute ARGS into the vector or list of template arguments T. */
8518 static tree
8519 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8521 tree orig_t = t;
8522 int len = TREE_VEC_LENGTH (t);
8523 int need_new = 0, i, expanded_len_adjust = 0, out;
8524 tree *elts = (tree *) alloca (len * sizeof (tree));
8526 for (i = 0; i < len; i++)
8528 tree orig_arg = TREE_VEC_ELT (t, i);
8529 tree new_arg;
8531 if (TREE_CODE (orig_arg) == TREE_VEC)
8532 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8533 else if (PACK_EXPANSION_P (orig_arg))
8535 /* Substitute into an expansion expression. */
8536 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8538 if (TREE_CODE (new_arg) == TREE_VEC)
8539 /* Add to the expanded length adjustment the number of
8540 expanded arguments. We subtract one from this
8541 measurement, because the argument pack expression
8542 itself is already counted as 1 in
8543 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8544 the argument pack is empty. */
8545 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8547 else if (ARGUMENT_PACK_P (orig_arg))
8549 /* Substitute into each of the arguments. */
8550 new_arg = TYPE_P (orig_arg)
8551 ? cxx_make_type (TREE_CODE (orig_arg))
8552 : make_node (TREE_CODE (orig_arg));
8554 SET_ARGUMENT_PACK_ARGS (
8555 new_arg,
8556 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8557 args, complain, in_decl));
8559 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8560 new_arg = error_mark_node;
8562 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8563 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8564 complain, in_decl);
8565 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8567 if (TREE_TYPE (new_arg) == error_mark_node)
8568 new_arg = error_mark_node;
8571 else
8572 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8574 if (new_arg == error_mark_node)
8575 return error_mark_node;
8577 elts[i] = new_arg;
8578 if (new_arg != orig_arg)
8579 need_new = 1;
8582 if (!need_new)
8583 return t;
8585 /* Make space for the expanded arguments coming from template
8586 argument packs. */
8587 t = make_tree_vec (len + expanded_len_adjust);
8588 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8589 arguments for a member template.
8590 In that case each TREE_VEC in ORIG_T represents a level of template
8591 arguments, and ORIG_T won't carry any non defaulted argument count.
8592 It will rather be the nested TREE_VECs that will carry one.
8593 In other words, ORIG_T carries a non defaulted argument count only
8594 if it doesn't contain any nested TREE_VEC. */
8595 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
8597 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
8598 count += expanded_len_adjust;
8599 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
8601 for (i = 0, out = 0; i < len; i++)
8603 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8604 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8605 && TREE_CODE (elts[i]) == TREE_VEC)
8607 int idx;
8609 /* Now expand the template argument pack "in place". */
8610 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8611 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8613 else
8615 TREE_VEC_ELT (t, out) = elts[i];
8616 out++;
8620 return t;
8623 /* Return the result of substituting ARGS into the template parameters
8624 given by PARMS. If there are m levels of ARGS and m + n levels of
8625 PARMS, then the result will contain n levels of PARMS. For
8626 example, if PARMS is `template <class T> template <class U>
8627 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8628 result will be `template <int*, double, class V>'. */
8630 static tree
8631 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8633 tree r = NULL_TREE;
8634 tree* new_parms;
8636 /* When substituting into a template, we must set
8637 PROCESSING_TEMPLATE_DECL as the template parameters may be
8638 dependent if they are based on one-another, and the dependency
8639 predicates are short-circuit outside of templates. */
8640 ++processing_template_decl;
8642 for (new_parms = &r;
8643 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8644 new_parms = &(TREE_CHAIN (*new_parms)),
8645 parms = TREE_CHAIN (parms))
8647 tree new_vec =
8648 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8649 int i;
8651 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8653 tree tuple;
8654 tree default_value;
8655 tree parm_decl;
8657 if (parms == error_mark_node)
8658 continue;
8660 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8662 if (tuple == error_mark_node)
8663 continue;
8665 default_value = TREE_PURPOSE (tuple);
8666 parm_decl = TREE_VALUE (tuple);
8668 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8669 if (TREE_CODE (parm_decl) == PARM_DECL
8670 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8671 parm_decl = error_mark_node;
8672 default_value = tsubst_template_arg (default_value, args,
8673 complain, NULL_TREE);
8675 tuple = build_tree_list (default_value, parm_decl);
8676 TREE_VEC_ELT (new_vec, i) = tuple;
8679 *new_parms =
8680 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8681 - TMPL_ARGS_DEPTH (args)),
8682 new_vec, NULL_TREE);
8685 --processing_template_decl;
8687 return r;
8690 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8691 type T. If T is not an aggregate or enumeration type, it is
8692 handled as if by tsubst. IN_DECL is as for tsubst. If
8693 ENTERING_SCOPE is nonzero, T is the context for a template which
8694 we are presently tsubst'ing. Return the substituted value. */
8696 static tree
8697 tsubst_aggr_type (tree t,
8698 tree args,
8699 tsubst_flags_t complain,
8700 tree in_decl,
8701 int entering_scope)
8703 if (t == NULL_TREE)
8704 return NULL_TREE;
8706 switch (TREE_CODE (t))
8708 case RECORD_TYPE:
8709 if (TYPE_PTRMEMFUNC_P (t))
8710 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8712 /* Else fall through. */
8713 case ENUMERAL_TYPE:
8714 case UNION_TYPE:
8715 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8717 tree argvec;
8718 tree context;
8719 tree r;
8720 int saved_unevaluated_operand;
8721 int saved_inhibit_evaluation_warnings;
8723 /* In "sizeof(X<I>)" we need to evaluate "I". */
8724 saved_unevaluated_operand = cp_unevaluated_operand;
8725 cp_unevaluated_operand = 0;
8726 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8727 c_inhibit_evaluation_warnings = 0;
8729 /* First, determine the context for the type we are looking
8730 up. */
8731 context = TYPE_CONTEXT (t);
8732 if (context)
8734 context = tsubst_aggr_type (context, args, complain,
8735 in_decl, /*entering_scope=*/1);
8736 /* If context is a nested class inside a class template,
8737 it may still need to be instantiated (c++/33959). */
8738 if (TYPE_P (context))
8739 context = complete_type (context);
8742 /* Then, figure out what arguments are appropriate for the
8743 type we are trying to find. For example, given:
8745 template <class T> struct S;
8746 template <class T, class U> void f(T, U) { S<U> su; }
8748 and supposing that we are instantiating f<int, double>,
8749 then our ARGS will be {int, double}, but, when looking up
8750 S we only want {double}. */
8751 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8752 complain, in_decl);
8753 if (argvec == error_mark_node)
8754 r = error_mark_node;
8755 else
8757 r = lookup_template_class (t, argvec, in_decl, context,
8758 entering_scope, complain);
8759 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8762 cp_unevaluated_operand = saved_unevaluated_operand;
8763 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8765 return r;
8767 else
8768 /* This is not a template type, so there's nothing to do. */
8769 return t;
8771 default:
8772 return tsubst (t, args, complain, in_decl);
8776 /* Substitute into the default argument ARG (a default argument for
8777 FN), which has the indicated TYPE. */
8779 tree
8780 tsubst_default_argument (tree fn, tree type, tree arg)
8782 tree saved_class_ptr = NULL_TREE;
8783 tree saved_class_ref = NULL_TREE;
8785 /* This default argument came from a template. Instantiate the
8786 default argument here, not in tsubst. In the case of
8787 something like:
8789 template <class T>
8790 struct S {
8791 static T t();
8792 void f(T = t());
8795 we must be careful to do name lookup in the scope of S<T>,
8796 rather than in the current class. */
8797 push_access_scope (fn);
8798 /* The "this" pointer is not valid in a default argument. */
8799 if (cfun)
8801 saved_class_ptr = current_class_ptr;
8802 cp_function_chain->x_current_class_ptr = NULL_TREE;
8803 saved_class_ref = current_class_ref;
8804 cp_function_chain->x_current_class_ref = NULL_TREE;
8807 push_deferring_access_checks(dk_no_deferred);
8808 /* The default argument expression may cause implicitly defined
8809 member functions to be synthesized, which will result in garbage
8810 collection. We must treat this situation as if we were within
8811 the body of function so as to avoid collecting live data on the
8812 stack. */
8813 ++function_depth;
8814 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8815 tf_warning_or_error, NULL_TREE,
8816 /*integral_constant_expression_p=*/false);
8817 --function_depth;
8818 pop_deferring_access_checks();
8820 /* Restore the "this" pointer. */
8821 if (cfun)
8823 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8824 cp_function_chain->x_current_class_ref = saved_class_ref;
8827 /* Make sure the default argument is reasonable. */
8828 arg = check_default_argument (type, arg);
8830 pop_access_scope (fn);
8832 return arg;
8835 /* Substitute into all the default arguments for FN. */
8837 static void
8838 tsubst_default_arguments (tree fn)
8840 tree arg;
8841 tree tmpl_args;
8843 tmpl_args = DECL_TI_ARGS (fn);
8845 /* If this function is not yet instantiated, we certainly don't need
8846 its default arguments. */
8847 if (uses_template_parms (tmpl_args))
8848 return;
8850 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8851 arg;
8852 arg = TREE_CHAIN (arg))
8853 if (TREE_PURPOSE (arg))
8854 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8855 TREE_VALUE (arg),
8856 TREE_PURPOSE (arg));
8859 /* Substitute the ARGS into the T, which is a _DECL. Return the
8860 result of the substitution. Issue error and warning messages under
8861 control of COMPLAIN. */
8863 static tree
8864 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8866 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8867 location_t saved_loc;
8868 tree r = NULL_TREE;
8869 tree in_decl = t;
8870 hashval_t hash = 0;
8872 /* Set the filename and linenumber to improve error-reporting. */
8873 saved_loc = input_location;
8874 input_location = DECL_SOURCE_LOCATION (t);
8876 switch (TREE_CODE (t))
8878 case TEMPLATE_DECL:
8880 /* We can get here when processing a member function template,
8881 member class template, or template template parameter. */
8882 tree decl = DECL_TEMPLATE_RESULT (t);
8883 tree spec;
8884 tree tmpl_args;
8885 tree full_args;
8887 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8889 /* Template template parameter is treated here. */
8890 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8891 if (new_type == error_mark_node)
8892 RETURN (error_mark_node);
8894 r = copy_decl (t);
8895 TREE_CHAIN (r) = NULL_TREE;
8896 TREE_TYPE (r) = new_type;
8897 DECL_TEMPLATE_RESULT (r)
8898 = build_decl (DECL_SOURCE_LOCATION (decl),
8899 TYPE_DECL, DECL_NAME (decl), new_type);
8900 DECL_TEMPLATE_PARMS (r)
8901 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8902 complain);
8903 TYPE_NAME (new_type) = r;
8904 break;
8907 /* We might already have an instance of this template.
8908 The ARGS are for the surrounding class type, so the
8909 full args contain the tsubst'd args for the context,
8910 plus the innermost args from the template decl. */
8911 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8912 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8913 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8914 /* Because this is a template, the arguments will still be
8915 dependent, even after substitution. If
8916 PROCESSING_TEMPLATE_DECL is not set, the dependency
8917 predicates will short-circuit. */
8918 ++processing_template_decl;
8919 full_args = tsubst_template_args (tmpl_args, args,
8920 complain, in_decl);
8921 --processing_template_decl;
8922 if (full_args == error_mark_node)
8923 RETURN (error_mark_node);
8925 /* If this is a default template template argument,
8926 tsubst might not have changed anything. */
8927 if (full_args == tmpl_args)
8928 RETURN (t);
8930 hash = hash_tmpl_and_args (t, full_args);
8931 spec = retrieve_specialization (t, full_args, hash);
8932 if (spec != NULL_TREE)
8934 r = spec;
8935 break;
8938 /* Make a new template decl. It will be similar to the
8939 original, but will record the current template arguments.
8940 We also create a new function declaration, which is just
8941 like the old one, but points to this new template, rather
8942 than the old one. */
8943 r = copy_decl (t);
8944 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8945 TREE_CHAIN (r) = NULL_TREE;
8947 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
8949 if (TREE_CODE (decl) == TYPE_DECL)
8951 tree new_type;
8952 ++processing_template_decl;
8953 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8954 --processing_template_decl;
8955 if (new_type == error_mark_node)
8956 RETURN (error_mark_node);
8958 TREE_TYPE (r) = new_type;
8959 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8960 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8961 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8962 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8964 else
8966 tree new_decl;
8967 ++processing_template_decl;
8968 new_decl = tsubst (decl, args, complain, in_decl);
8969 --processing_template_decl;
8970 if (new_decl == error_mark_node)
8971 RETURN (error_mark_node);
8973 DECL_TEMPLATE_RESULT (r) = new_decl;
8974 DECL_TI_TEMPLATE (new_decl) = r;
8975 TREE_TYPE (r) = TREE_TYPE (new_decl);
8976 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8977 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8980 SET_DECL_IMPLICIT_INSTANTIATION (r);
8981 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8982 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8984 /* The template parameters for this new template are all the
8985 template parameters for the old template, except the
8986 outermost level of parameters. */
8987 DECL_TEMPLATE_PARMS (r)
8988 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8989 complain);
8991 if (PRIMARY_TEMPLATE_P (t))
8992 DECL_PRIMARY_TEMPLATE (r) = r;
8994 if (TREE_CODE (decl) != TYPE_DECL)
8995 /* Record this non-type partial instantiation. */
8996 register_specialization (r, t,
8997 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8998 false, hash);
9000 break;
9002 case FUNCTION_DECL:
9004 tree ctx;
9005 tree argvec = NULL_TREE;
9006 tree *friends;
9007 tree gen_tmpl;
9008 tree type;
9009 int member;
9010 int args_depth;
9011 int parms_depth;
9013 /* Nobody should be tsubst'ing into non-template functions. */
9014 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9016 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9018 tree spec;
9019 bool dependent_p;
9021 /* If T is not dependent, just return it. We have to
9022 increment PROCESSING_TEMPLATE_DECL because
9023 value_dependent_expression_p assumes that nothing is
9024 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9025 ++processing_template_decl;
9026 dependent_p = value_dependent_expression_p (t);
9027 --processing_template_decl;
9028 if (!dependent_p)
9029 RETURN (t);
9031 /* Calculate the most general template of which R is a
9032 specialization, and the complete set of arguments used to
9033 specialize R. */
9034 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9035 argvec = tsubst_template_args (DECL_TI_ARGS
9036 (DECL_TEMPLATE_RESULT
9037 (DECL_TI_TEMPLATE (t))),
9038 args, complain, in_decl);
9040 /* Check to see if we already have this specialization. */
9041 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9042 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9044 if (spec)
9046 r = spec;
9047 break;
9050 /* We can see more levels of arguments than parameters if
9051 there was a specialization of a member template, like
9052 this:
9054 template <class T> struct S { template <class U> void f(); }
9055 template <> template <class U> void S<int>::f(U);
9057 Here, we'll be substituting into the specialization,
9058 because that's where we can find the code we actually
9059 want to generate, but we'll have enough arguments for
9060 the most general template.
9062 We also deal with the peculiar case:
9064 template <class T> struct S {
9065 template <class U> friend void f();
9067 template <class U> void f() {}
9068 template S<int>;
9069 template void f<double>();
9071 Here, the ARGS for the instantiation of will be {int,
9072 double}. But, we only need as many ARGS as there are
9073 levels of template parameters in CODE_PATTERN. We are
9074 careful not to get fooled into reducing the ARGS in
9075 situations like:
9077 template <class T> struct S { template <class U> void f(U); }
9078 template <class T> template <> void S<T>::f(int) {}
9080 which we can spot because the pattern will be a
9081 specialization in this case. */
9082 args_depth = TMPL_ARGS_DEPTH (args);
9083 parms_depth =
9084 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9085 if (args_depth > parms_depth
9086 && !DECL_TEMPLATE_SPECIALIZATION (t))
9087 args = get_innermost_template_args (args, parms_depth);
9089 else
9091 /* This special case arises when we have something like this:
9093 template <class T> struct S {
9094 friend void f<int>(int, double);
9097 Here, the DECL_TI_TEMPLATE for the friend declaration
9098 will be an IDENTIFIER_NODE. We are being called from
9099 tsubst_friend_function, and we want only to create a
9100 new decl (R) with appropriate types so that we can call
9101 determine_specialization. */
9102 gen_tmpl = NULL_TREE;
9105 if (DECL_CLASS_SCOPE_P (t))
9107 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9108 member = 2;
9109 else
9110 member = 1;
9111 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9112 complain, t, /*entering_scope=*/1);
9114 else
9116 member = 0;
9117 ctx = DECL_CONTEXT (t);
9119 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9120 if (type == error_mark_node)
9121 RETURN (error_mark_node);
9123 /* We do NOT check for matching decls pushed separately at this
9124 point, as they may not represent instantiations of this
9125 template, and in any case are considered separate under the
9126 discrete model. */
9127 r = copy_decl (t);
9128 DECL_USE_TEMPLATE (r) = 0;
9129 TREE_TYPE (r) = type;
9130 /* Clear out the mangled name and RTL for the instantiation. */
9131 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9132 SET_DECL_RTL (r, NULL_RTX);
9133 /* Leave DECL_INITIAL set on deleted instantiations. */
9134 if (!DECL_DELETED_FN (r))
9135 DECL_INITIAL (r) = NULL_TREE;
9136 DECL_CONTEXT (r) = ctx;
9138 if (member && DECL_CONV_FN_P (r))
9139 /* Type-conversion operator. Reconstruct the name, in
9140 case it's the name of one of the template's parameters. */
9141 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9143 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9144 complain, t);
9145 DECL_RESULT (r) = NULL_TREE;
9147 TREE_STATIC (r) = 0;
9148 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9149 DECL_EXTERNAL (r) = 1;
9150 /* If this is an instantiation of a function with internal
9151 linkage, we already know what object file linkage will be
9152 assigned to the instantiation. */
9153 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9154 DECL_DEFER_OUTPUT (r) = 0;
9155 TREE_CHAIN (r) = NULL_TREE;
9156 DECL_PENDING_INLINE_INFO (r) = 0;
9157 DECL_PENDING_INLINE_P (r) = 0;
9158 DECL_SAVED_TREE (r) = NULL_TREE;
9159 DECL_STRUCT_FUNCTION (r) = NULL;
9160 TREE_USED (r) = 0;
9161 /* We'll re-clone as appropriate in instantiate_template. */
9162 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9164 /* If we aren't complaining now, return on error before we register
9165 the specialization so that we'll complain eventually. */
9166 if ((complain & tf_error) == 0
9167 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9168 && !grok_op_properties (r, /*complain=*/false))
9169 RETURN (error_mark_node);
9171 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9172 this in the special friend case mentioned above where
9173 GEN_TMPL is NULL. */
9174 if (gen_tmpl)
9176 DECL_TEMPLATE_INFO (r)
9177 = build_template_info (gen_tmpl, argvec);
9178 SET_DECL_IMPLICIT_INSTANTIATION (r);
9179 register_specialization (r, gen_tmpl, argvec, false, hash);
9181 /* We're not supposed to instantiate default arguments
9182 until they are called, for a template. But, for a
9183 declaration like:
9185 template <class T> void f ()
9186 { extern void g(int i = T()); }
9188 we should do the substitution when the template is
9189 instantiated. We handle the member function case in
9190 instantiate_class_template since the default arguments
9191 might refer to other members of the class. */
9192 if (!member
9193 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9194 && !uses_template_parms (argvec))
9195 tsubst_default_arguments (r);
9197 else
9198 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9200 /* Copy the list of befriending classes. */
9201 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9202 *friends;
9203 friends = &TREE_CHAIN (*friends))
9205 *friends = copy_node (*friends);
9206 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9207 args, complain,
9208 in_decl);
9211 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9213 maybe_retrofit_in_chrg (r);
9214 if (DECL_CONSTRUCTOR_P (r))
9215 grok_ctor_properties (ctx, r);
9216 /* If this is an instantiation of a member template, clone it.
9217 If it isn't, that'll be handled by
9218 clone_constructors_and_destructors. */
9219 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9220 clone_function_decl (r, /*update_method_vec_p=*/0);
9222 else if ((complain & tf_error) != 0
9223 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9224 && !grok_op_properties (r, /*complain=*/true))
9225 RETURN (error_mark_node);
9227 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9228 SET_DECL_FRIEND_CONTEXT (r,
9229 tsubst (DECL_FRIEND_CONTEXT (t),
9230 args, complain, in_decl));
9232 /* Possibly limit visibility based on template args. */
9233 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9234 if (DECL_VISIBILITY_SPECIFIED (t))
9236 DECL_VISIBILITY_SPECIFIED (r) = 0;
9237 DECL_ATTRIBUTES (r)
9238 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9240 determine_visibility (r);
9241 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9242 && !processing_template_decl)
9243 defaulted_late_check (r);
9245 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9246 args, complain, in_decl);
9248 break;
9250 case PARM_DECL:
9252 tree type = NULL_TREE;
9253 int i, len = 1;
9254 tree expanded_types = NULL_TREE;
9255 tree prev_r = NULL_TREE;
9256 tree first_r = NULL_TREE;
9258 if (FUNCTION_PARAMETER_PACK_P (t))
9260 /* If there is a local specialization that isn't a
9261 parameter pack, it means that we're doing a "simple"
9262 substitution from inside tsubst_pack_expansion. Just
9263 return the local specialization (which will be a single
9264 parm). */
9265 tree spec = retrieve_local_specialization (t);
9266 if (spec
9267 && TREE_CODE (spec) == PARM_DECL
9268 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9269 RETURN (spec);
9271 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9272 the parameters in this function parameter pack. */
9273 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9274 complain, in_decl);
9275 if (TREE_CODE (expanded_types) == TREE_VEC)
9277 len = TREE_VEC_LENGTH (expanded_types);
9279 /* Zero-length parameter packs are boring. Just substitute
9280 into the chain. */
9281 if (len == 0)
9282 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9283 TREE_CHAIN (t)));
9285 else
9287 /* All we did was update the type. Make a note of that. */
9288 type = expanded_types;
9289 expanded_types = NULL_TREE;
9293 /* Loop through all of the parameter's we'll build. When T is
9294 a function parameter pack, LEN is the number of expanded
9295 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9296 r = NULL_TREE;
9297 for (i = 0; i < len; ++i)
9299 prev_r = r;
9300 r = copy_node (t);
9301 if (DECL_TEMPLATE_PARM_P (t))
9302 SET_DECL_TEMPLATE_PARM_P (r);
9304 /* An argument of a function parameter pack is not a parameter
9305 pack. */
9306 FUNCTION_PARAMETER_PACK_P (r) = false;
9308 if (expanded_types)
9309 /* We're on the Ith parameter of the function parameter
9310 pack. */
9312 /* Get the Ith type. */
9313 type = TREE_VEC_ELT (expanded_types, i);
9315 if (DECL_NAME (r))
9316 /* Rename the parameter to include the index. */
9317 DECL_NAME (r) =
9318 make_ith_pack_parameter_name (DECL_NAME (r), i);
9320 else if (!type)
9321 /* We're dealing with a normal parameter. */
9322 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9324 type = type_decays_to (type);
9325 TREE_TYPE (r) = type;
9326 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9328 if (DECL_INITIAL (r))
9330 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9331 DECL_INITIAL (r) = TREE_TYPE (r);
9332 else
9333 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9334 complain, in_decl);
9337 DECL_CONTEXT (r) = NULL_TREE;
9339 if (!DECL_TEMPLATE_PARM_P (r))
9340 DECL_ARG_TYPE (r) = type_passed_as (type);
9342 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9343 args, complain, in_decl);
9345 /* Keep track of the first new parameter we
9346 generate. That's what will be returned to the
9347 caller. */
9348 if (!first_r)
9349 first_r = r;
9351 /* Build a proper chain of parameters when substituting
9352 into a function parameter pack. */
9353 if (prev_r)
9354 TREE_CHAIN (prev_r) = r;
9357 if (TREE_CHAIN (t))
9358 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
9359 complain, TREE_CHAIN (t));
9361 /* FIRST_R contains the start of the chain we've built. */
9362 r = first_r;
9364 break;
9366 case FIELD_DECL:
9368 tree type;
9370 r = copy_decl (t);
9371 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9372 if (type == error_mark_node)
9373 RETURN (error_mark_node);
9374 TREE_TYPE (r) = type;
9375 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9377 /* DECL_INITIAL gives the number of bits in a bit-field. */
9378 DECL_INITIAL (r)
9379 = tsubst_expr (DECL_INITIAL (t), args,
9380 complain, in_decl,
9381 /*integral_constant_expression_p=*/true);
9382 /* We don't have to set DECL_CONTEXT here; it is set by
9383 finish_member_declaration. */
9384 TREE_CHAIN (r) = NULL_TREE;
9385 if (VOID_TYPE_P (type))
9386 error ("instantiation of %q+D as type %qT", r, type);
9388 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9389 args, complain, in_decl);
9391 break;
9393 case USING_DECL:
9394 /* We reach here only for member using decls. */
9395 if (DECL_DEPENDENT_P (t))
9397 r = do_class_using_decl
9398 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9399 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9400 if (!r)
9401 r = error_mark_node;
9402 else
9404 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9405 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9408 else
9410 r = copy_node (t);
9411 TREE_CHAIN (r) = NULL_TREE;
9413 break;
9415 case TYPE_DECL:
9416 case VAR_DECL:
9418 tree argvec = NULL_TREE;
9419 tree gen_tmpl = NULL_TREE;
9420 tree spec;
9421 tree tmpl = NULL_TREE;
9422 tree ctx;
9423 tree type = NULL_TREE;
9424 bool local_p;
9426 if (TREE_CODE (t) == TYPE_DECL
9427 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9429 /* If this is the canonical decl, we don't have to
9430 mess with instantiations, and often we can't (for
9431 typename, template type parms and such). Note that
9432 TYPE_NAME is not correct for the above test if
9433 we've copied the type for a typedef. */
9434 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9435 if (type == error_mark_node)
9436 RETURN (error_mark_node);
9437 r = TYPE_NAME (type);
9438 break;
9441 /* Check to see if we already have the specialization we
9442 need. */
9443 spec = NULL_TREE;
9444 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9446 /* T is a static data member or namespace-scope entity.
9447 We have to substitute into namespace-scope variables
9448 (even though such entities are never templates) because
9449 of cases like:
9451 template <class T> void f() { extern T t; }
9453 where the entity referenced is not known until
9454 instantiation time. */
9455 local_p = false;
9456 ctx = DECL_CONTEXT (t);
9457 if (DECL_CLASS_SCOPE_P (t))
9459 ctx = tsubst_aggr_type (ctx, args,
9460 complain,
9461 in_decl, /*entering_scope=*/1);
9462 /* If CTX is unchanged, then T is in fact the
9463 specialization we want. That situation occurs when
9464 referencing a static data member within in its own
9465 class. We can use pointer equality, rather than
9466 same_type_p, because DECL_CONTEXT is always
9467 canonical. */
9468 if (ctx == DECL_CONTEXT (t))
9469 spec = t;
9472 if (!spec)
9474 tmpl = DECL_TI_TEMPLATE (t);
9475 gen_tmpl = most_general_template (tmpl);
9476 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9477 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9478 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9481 else
9483 /* A local variable. */
9484 local_p = true;
9485 /* Subsequent calls to pushdecl will fill this in. */
9486 ctx = NULL_TREE;
9487 spec = retrieve_local_specialization (t);
9489 /* If we already have the specialization we need, there is
9490 nothing more to do. */
9491 if (spec)
9493 r = spec;
9494 break;
9497 /* Create a new node for the specialization we need. */
9498 r = copy_decl (t);
9499 if (type == NULL_TREE)
9501 if (is_typedef_decl (t))
9502 type = DECL_ORIGINAL_TYPE (t);
9503 else
9504 type = TREE_TYPE (t);
9505 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
9506 type = strip_array_domain (type);
9507 type = tsubst (type, args, complain, in_decl);
9509 if (TREE_CODE (r) == VAR_DECL)
9511 /* Even if the original location is out of scope, the
9512 newly substituted one is not. */
9513 DECL_DEAD_FOR_LOCAL (r) = 0;
9514 DECL_INITIALIZED_P (r) = 0;
9515 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9516 if (type == error_mark_node)
9517 RETURN (error_mark_node);
9518 if (TREE_CODE (type) == FUNCTION_TYPE)
9520 /* It may seem that this case cannot occur, since:
9522 typedef void f();
9523 void g() { f x; }
9525 declares a function, not a variable. However:
9527 typedef void f();
9528 template <typename T> void g() { T t; }
9529 template void g<f>();
9531 is an attempt to declare a variable with function
9532 type. */
9533 error ("variable %qD has function type",
9534 /* R is not yet sufficiently initialized, so we
9535 just use its name. */
9536 DECL_NAME (r));
9537 RETURN (error_mark_node);
9539 type = complete_type (type);
9540 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9541 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9542 type = check_var_type (DECL_NAME (r), type);
9544 if (DECL_HAS_VALUE_EXPR_P (t))
9546 tree ve = DECL_VALUE_EXPR (t);
9547 ve = tsubst_expr (ve, args, complain, in_decl,
9548 /*constant_expression_p=*/false);
9549 SET_DECL_VALUE_EXPR (r, ve);
9552 else if (DECL_SELF_REFERENCE_P (t))
9553 SET_DECL_SELF_REFERENCE_P (r);
9554 TREE_TYPE (r) = type;
9555 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9556 DECL_CONTEXT (r) = ctx;
9557 /* Clear out the mangled name and RTL for the instantiation. */
9558 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9559 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9560 SET_DECL_RTL (r, NULL_RTX);
9561 /* The initializer must not be expanded until it is required;
9562 see [temp.inst]. */
9563 DECL_INITIAL (r) = NULL_TREE;
9564 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9565 SET_DECL_RTL (r, NULL_RTX);
9566 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9567 if (TREE_CODE (r) == VAR_DECL)
9569 /* Possibly limit visibility based on template args. */
9570 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9571 if (DECL_VISIBILITY_SPECIFIED (t))
9573 DECL_VISIBILITY_SPECIFIED (r) = 0;
9574 DECL_ATTRIBUTES (r)
9575 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9577 determine_visibility (r);
9580 if (!local_p)
9582 /* A static data member declaration is always marked
9583 external when it is declared in-class, even if an
9584 initializer is present. We mimic the non-template
9585 processing here. */
9586 DECL_EXTERNAL (r) = 1;
9588 register_specialization (r, gen_tmpl, argvec, false, hash);
9589 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9590 SET_DECL_IMPLICIT_INSTANTIATION (r);
9592 else if (cp_unevaluated_operand)
9594 /* We're substituting this var in a decltype outside of its
9595 scope, such as for a lambda return type. Don't add it to
9596 local_specializations, do perform auto deduction. */
9597 tree auto_node = type_uses_auto (type);
9598 tree init
9599 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9600 /*constant_expression_p=*/false);
9602 if (auto_node && init && describable_type (init))
9604 type = do_auto_deduction (type, init, auto_node);
9605 TREE_TYPE (r) = type;
9608 else
9609 register_local_specialization (r, t);
9611 TREE_CHAIN (r) = NULL_TREE;
9613 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9614 /*flags=*/0,
9615 args, complain, in_decl);
9617 /* Preserve a typedef that names a type. */
9618 if (is_typedef_decl (r))
9620 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9621 set_underlying_type (r);
9624 layout_decl (r, 0);
9626 break;
9628 default:
9629 gcc_unreachable ();
9631 #undef RETURN
9633 out:
9634 /* Restore the file and line information. */
9635 input_location = saved_loc;
9637 return r;
9640 /* Substitute into the ARG_TYPES of a function type. */
9642 static tree
9643 tsubst_arg_types (tree arg_types,
9644 tree args,
9645 tsubst_flags_t complain,
9646 tree in_decl)
9648 tree remaining_arg_types;
9649 tree type = NULL_TREE;
9650 int i = 1;
9651 tree expanded_args = NULL_TREE;
9652 tree default_arg;
9654 if (!arg_types || arg_types == void_list_node)
9655 return arg_types;
9657 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9658 args, complain, in_decl);
9659 if (remaining_arg_types == error_mark_node)
9660 return error_mark_node;
9662 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9664 /* For a pack expansion, perform substitution on the
9665 entire expression. Later on, we'll handle the arguments
9666 one-by-one. */
9667 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9668 args, complain, in_decl);
9670 if (TREE_CODE (expanded_args) == TREE_VEC)
9671 /* So that we'll spin through the parameters, one by one. */
9672 i = TREE_VEC_LENGTH (expanded_args);
9673 else
9675 /* We only partially substituted into the parameter
9676 pack. Our type is TYPE_PACK_EXPANSION. */
9677 type = expanded_args;
9678 expanded_args = NULL_TREE;
9682 while (i > 0) {
9683 --i;
9685 if (expanded_args)
9686 type = TREE_VEC_ELT (expanded_args, i);
9687 else if (!type)
9688 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9690 if (type == error_mark_node)
9691 return error_mark_node;
9692 if (VOID_TYPE_P (type))
9694 if (complain & tf_error)
9696 error ("invalid parameter type %qT", type);
9697 if (in_decl)
9698 error ("in declaration %q+D", in_decl);
9700 return error_mark_node;
9703 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9704 top-level qualifiers as required. */
9705 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9707 /* We do not substitute into default arguments here. The standard
9708 mandates that they be instantiated only when needed, which is
9709 done in build_over_call. */
9710 default_arg = TREE_PURPOSE (arg_types);
9712 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9714 /* We've instantiated a template before its default arguments
9715 have been parsed. This can happen for a nested template
9716 class, and is not an error unless we require the default
9717 argument in a call of this function. */
9718 remaining_arg_types =
9719 tree_cons (default_arg, type, remaining_arg_types);
9720 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9721 remaining_arg_types);
9723 else
9724 remaining_arg_types =
9725 hash_tree_cons (default_arg, type, remaining_arg_types);
9728 return remaining_arg_types;
9731 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9732 *not* handle the exception-specification for FNTYPE, because the
9733 initial substitution of explicitly provided template parameters
9734 during argument deduction forbids substitution into the
9735 exception-specification:
9737 [temp.deduct]
9739 All references in the function type of the function template to the
9740 corresponding template parameters are replaced by the specified tem-
9741 plate argument values. If a substitution in a template parameter or
9742 in the function type of the function template results in an invalid
9743 type, type deduction fails. [Note: The equivalent substitution in
9744 exception specifications is done only when the function is instanti-
9745 ated, at which point a program is ill-formed if the substitution
9746 results in an invalid type.] */
9748 static tree
9749 tsubst_function_type (tree t,
9750 tree args,
9751 tsubst_flags_t complain,
9752 tree in_decl)
9754 tree return_type;
9755 tree arg_types;
9756 tree fntype;
9758 /* The TYPE_CONTEXT is not used for function/method types. */
9759 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9761 /* Substitute the return type. */
9762 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9763 if (return_type == error_mark_node)
9764 return error_mark_node;
9765 /* The standard does not presently indicate that creation of a
9766 function type with an invalid return type is a deduction failure.
9767 However, that is clearly analogous to creating an array of "void"
9768 or a reference to a reference. This is core issue #486. */
9769 if (TREE_CODE (return_type) == ARRAY_TYPE
9770 || TREE_CODE (return_type) == FUNCTION_TYPE)
9772 if (complain & tf_error)
9774 if (TREE_CODE (return_type) == ARRAY_TYPE)
9775 error ("function returning an array");
9776 else
9777 error ("function returning a function");
9779 return error_mark_node;
9782 /* Substitute the argument types. */
9783 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9784 complain, in_decl);
9785 if (arg_types == error_mark_node)
9786 return error_mark_node;
9788 /* Construct a new type node and return it. */
9789 if (TREE_CODE (t) == FUNCTION_TYPE)
9790 fntype = build_function_type (return_type, arg_types);
9791 else
9793 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9794 if (! MAYBE_CLASS_TYPE_P (r))
9796 /* [temp.deduct]
9798 Type deduction may fail for any of the following
9799 reasons:
9801 -- Attempting to create "pointer to member of T" when T
9802 is not a class type. */
9803 if (complain & tf_error)
9804 error ("creating pointer to member function of non-class type %qT",
9806 return error_mark_node;
9809 fntype = build_method_type_directly (r, return_type,
9810 TREE_CHAIN (arg_types));
9812 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9813 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9815 return fntype;
9818 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9819 ARGS into that specification, and return the substituted
9820 specification. If there is no specification, return NULL_TREE. */
9822 static tree
9823 tsubst_exception_specification (tree fntype,
9824 tree args,
9825 tsubst_flags_t complain,
9826 tree in_decl)
9828 tree specs;
9829 tree new_specs;
9831 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9832 new_specs = NULL_TREE;
9833 if (specs)
9835 if (! TREE_VALUE (specs))
9836 new_specs = specs;
9837 else
9838 while (specs)
9840 tree spec;
9841 int i, len = 1;
9842 tree expanded_specs = NULL_TREE;
9844 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9846 /* Expand the pack expansion type. */
9847 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9848 args, complain,
9849 in_decl);
9851 if (expanded_specs == error_mark_node)
9852 return error_mark_node;
9853 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9854 len = TREE_VEC_LENGTH (expanded_specs);
9855 else
9857 /* We're substituting into a member template, so
9858 we got a TYPE_PACK_EXPANSION back. Add that
9859 expansion and move on. */
9860 gcc_assert (TREE_CODE (expanded_specs)
9861 == TYPE_PACK_EXPANSION);
9862 new_specs = add_exception_specifier (new_specs,
9863 expanded_specs,
9864 complain);
9865 specs = TREE_CHAIN (specs);
9866 continue;
9870 for (i = 0; i < len; ++i)
9872 if (expanded_specs)
9873 spec = TREE_VEC_ELT (expanded_specs, i);
9874 else
9875 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9876 if (spec == error_mark_node)
9877 return spec;
9878 new_specs = add_exception_specifier (new_specs, spec,
9879 complain);
9882 specs = TREE_CHAIN (specs);
9885 return new_specs;
9888 /* Take the tree structure T and replace template parameters used
9889 therein with the argument vector ARGS. IN_DECL is an associated
9890 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9891 Issue error and warning messages under control of COMPLAIN. Note
9892 that we must be relatively non-tolerant of extensions here, in
9893 order to preserve conformance; if we allow substitutions that
9894 should not be allowed, we may allow argument deductions that should
9895 not succeed, and therefore report ambiguous overload situations
9896 where there are none. In theory, we could allow the substitution,
9897 but indicate that it should have failed, and allow our caller to
9898 make sure that the right thing happens, but we don't try to do this
9899 yet.
9901 This function is used for dealing with types, decls and the like;
9902 for expressions, use tsubst_expr or tsubst_copy. */
9904 tree
9905 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9907 tree type, r;
9909 if (t == NULL_TREE || t == error_mark_node
9910 || t == integer_type_node
9911 || t == void_type_node
9912 || t == char_type_node
9913 || t == unknown_type_node
9914 || TREE_CODE (t) == NAMESPACE_DECL)
9915 return t;
9917 if (DECL_P (t))
9918 return tsubst_decl (t, args, complain);
9920 if (args == NULL_TREE)
9921 return t;
9923 if (TREE_CODE (t) == IDENTIFIER_NODE)
9924 type = IDENTIFIER_TYPE_VALUE (t);
9925 else
9926 type = TREE_TYPE (t);
9928 gcc_assert (type != unknown_type_node);
9930 /* Reuse typedefs. We need to do this to handle dependent attributes,
9931 such as attribute aligned. */
9932 if (TYPE_P (t)
9933 && TYPE_NAME (t)
9934 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9936 tree decl = TYPE_NAME (t);
9938 if (DECL_CLASS_SCOPE_P (decl)
9939 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9940 && uses_template_parms (DECL_CONTEXT (decl)))
9942 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9943 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9944 r = retrieve_specialization (tmpl, gen_args, 0);
9946 else if (DECL_FUNCTION_SCOPE_P (decl)
9947 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9948 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9949 r = retrieve_local_specialization (decl);
9950 else
9951 /* The typedef is from a non-template context. */
9952 return t;
9954 if (r)
9956 r = TREE_TYPE (r);
9957 r = cp_build_qualified_type_real
9958 (r, cp_type_quals (t) | cp_type_quals (r),
9959 complain | tf_ignore_bad_quals);
9960 return r;
9962 /* Else we must be instantiating the typedef, so fall through. */
9965 if (type
9966 && TREE_CODE (t) != TYPENAME_TYPE
9967 && TREE_CODE (t) != TEMPLATE_TYPE_PARM
9968 && TREE_CODE (t) != IDENTIFIER_NODE
9969 && TREE_CODE (t) != FUNCTION_TYPE
9970 && TREE_CODE (t) != METHOD_TYPE)
9971 type = tsubst (type, args, complain, in_decl);
9972 if (type == error_mark_node)
9973 return error_mark_node;
9975 switch (TREE_CODE (t))
9977 case RECORD_TYPE:
9978 case UNION_TYPE:
9979 case ENUMERAL_TYPE:
9980 return tsubst_aggr_type (t, args, complain, in_decl,
9981 /*entering_scope=*/0);
9983 case ERROR_MARK:
9984 case IDENTIFIER_NODE:
9985 case VOID_TYPE:
9986 case REAL_TYPE:
9987 case COMPLEX_TYPE:
9988 case VECTOR_TYPE:
9989 case BOOLEAN_TYPE:
9990 case INTEGER_CST:
9991 case REAL_CST:
9992 case STRING_CST:
9993 return t;
9995 case INTEGER_TYPE:
9996 if (t == integer_type_node)
9997 return t;
9999 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10000 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10001 return t;
10004 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10006 max = tsubst_expr (omax, args, complain, in_decl,
10007 /*integral_constant_expression_p=*/false);
10009 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10010 needed. */
10011 if (TREE_CODE (max) == NOP_EXPR
10012 && TREE_SIDE_EFFECTS (omax)
10013 && !TREE_TYPE (max))
10014 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10016 max = fold_decl_constant_value (max);
10018 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10019 with TREE_SIDE_EFFECTS that indicates this is not an integral
10020 constant expression. */
10021 if (processing_template_decl
10022 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10024 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10025 TREE_SIDE_EFFECTS (max) = 1;
10028 if (TREE_CODE (max) != INTEGER_CST
10029 && !at_function_scope_p ()
10030 && !TREE_SIDE_EFFECTS (max)
10031 && !value_dependent_expression_p (max))
10033 if (complain & tf_error)
10034 error ("array bound is not an integer constant");
10035 return error_mark_node;
10038 /* [temp.deduct]
10040 Type deduction may fail for any of the following
10041 reasons:
10043 Attempting to create an array with a size that is
10044 zero or negative. */
10045 if (integer_zerop (max) && !(complain & tf_error))
10046 /* We must fail if performing argument deduction (as
10047 indicated by the state of complain), so that
10048 another substitution can be found. */
10049 return error_mark_node;
10050 else if (TREE_CODE (max) == INTEGER_CST
10051 && INT_CST_LT (max, integer_zero_node))
10053 if (complain & tf_error)
10054 error ("creating array with negative size (%qE)", max);
10056 return error_mark_node;
10059 return compute_array_index_type (NULL_TREE, max);
10062 case TEMPLATE_TYPE_PARM:
10063 case TEMPLATE_TEMPLATE_PARM:
10064 case BOUND_TEMPLATE_TEMPLATE_PARM:
10065 case TEMPLATE_PARM_INDEX:
10067 int idx;
10068 int level;
10069 int levels;
10070 tree arg = NULL_TREE;
10072 r = NULL_TREE;
10074 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10075 template_parm_level_and_index (t, &level, &idx);
10077 levels = TMPL_ARGS_DEPTH (args);
10078 if (level <= levels)
10080 arg = TMPL_ARG (args, level, idx);
10082 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10083 /* See through ARGUMENT_PACK_SELECT arguments. */
10084 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10087 if (arg == error_mark_node)
10088 return error_mark_node;
10089 else if (arg != NULL_TREE)
10091 if (ARGUMENT_PACK_P (arg))
10092 /* If ARG is an argument pack, we don't actually want to
10093 perform a substitution here, because substitutions
10094 for argument packs are only done
10095 element-by-element. We can get to this point when
10096 substituting the type of a non-type template
10097 parameter pack, when that type actually contains
10098 template parameter packs from an outer template, e.g.,
10100 template<typename... Types> struct A {
10101 template<Types... Values> struct B { };
10102 }; */
10103 return t;
10105 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
10107 int quals;
10108 gcc_assert (TYPE_P (arg));
10110 /* cv-quals from the template are discarded when
10111 substituting in a function or reference type. */
10112 if (TREE_CODE (arg) == FUNCTION_TYPE
10113 || TREE_CODE (arg) == METHOD_TYPE
10114 || TREE_CODE (arg) == REFERENCE_TYPE)
10115 quals = cp_type_quals (arg);
10116 else
10117 quals = cp_type_quals (arg) | cp_type_quals (t);
10119 return cp_build_qualified_type_real
10120 (arg, quals, complain | tf_ignore_bad_quals);
10122 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10124 /* We are processing a type constructed from a
10125 template template parameter. */
10126 tree argvec = tsubst (TYPE_TI_ARGS (t),
10127 args, complain, in_decl);
10128 if (argvec == error_mark_node)
10129 return error_mark_node;
10131 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10132 are resolving nested-types in the signature of a
10133 member function templates. Otherwise ARG is a
10134 TEMPLATE_DECL and is the real template to be
10135 instantiated. */
10136 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10137 arg = TYPE_NAME (arg);
10139 r = lookup_template_class (arg,
10140 argvec, in_decl,
10141 DECL_CONTEXT (arg),
10142 /*entering_scope=*/0,
10143 complain);
10144 return cp_build_qualified_type_real
10145 (r, TYPE_QUALS (t), complain);
10147 else
10148 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10149 return arg;
10152 if (level == 1)
10153 /* This can happen during the attempted tsubst'ing in
10154 unify. This means that we don't yet have any information
10155 about the template parameter in question. */
10156 return t;
10158 /* If we get here, we must have been looking at a parm for a
10159 more deeply nested template. Make a new version of this
10160 template parameter, but with a lower level. */
10161 switch (TREE_CODE (t))
10163 case TEMPLATE_TYPE_PARM:
10164 case TEMPLATE_TEMPLATE_PARM:
10165 case BOUND_TEMPLATE_TEMPLATE_PARM:
10166 if (cp_type_quals (t))
10168 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10169 r = cp_build_qualified_type_real
10170 (r, cp_type_quals (t),
10171 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
10172 ? tf_ignore_bad_quals : 0));
10174 else
10176 r = copy_type (t);
10177 TEMPLATE_TYPE_PARM_INDEX (r)
10178 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10179 r, levels, args, complain);
10180 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10181 TYPE_MAIN_VARIANT (r) = r;
10182 TYPE_POINTER_TO (r) = NULL_TREE;
10183 TYPE_REFERENCE_TO (r) = NULL_TREE;
10185 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10186 /* We have reduced the level of the template
10187 template parameter, but not the levels of its
10188 template parameters, so canonical_type_parameter
10189 will not be able to find the canonical template
10190 template parameter for this level. Thus, we
10191 require structural equality checking to compare
10192 TEMPLATE_TEMPLATE_PARMs. */
10193 SET_TYPE_STRUCTURAL_EQUALITY (r);
10194 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10195 SET_TYPE_STRUCTURAL_EQUALITY (r);
10196 else
10197 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10199 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10201 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10202 complain, in_decl);
10203 if (argvec == error_mark_node)
10204 return error_mark_node;
10206 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10207 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10210 break;
10212 case TEMPLATE_PARM_INDEX:
10213 r = reduce_template_parm_level (t, type, levels, args, complain);
10214 break;
10216 default:
10217 gcc_unreachable ();
10220 return r;
10223 case TREE_LIST:
10225 tree purpose, value, chain;
10227 if (t == void_list_node)
10228 return t;
10230 purpose = TREE_PURPOSE (t);
10231 if (purpose)
10233 purpose = tsubst (purpose, args, complain, in_decl);
10234 if (purpose == error_mark_node)
10235 return error_mark_node;
10237 value = TREE_VALUE (t);
10238 if (value)
10240 value = tsubst (value, args, complain, in_decl);
10241 if (value == error_mark_node)
10242 return error_mark_node;
10244 chain = TREE_CHAIN (t);
10245 if (chain && chain != void_type_node)
10247 chain = tsubst (chain, args, complain, in_decl);
10248 if (chain == error_mark_node)
10249 return error_mark_node;
10251 if (purpose == TREE_PURPOSE (t)
10252 && value == TREE_VALUE (t)
10253 && chain == TREE_CHAIN (t))
10254 return t;
10255 return hash_tree_cons (purpose, value, chain);
10258 case TREE_BINFO:
10259 /* We should never be tsubsting a binfo. */
10260 gcc_unreachable ();
10262 case TREE_VEC:
10263 /* A vector of template arguments. */
10264 gcc_assert (!type);
10265 return tsubst_template_args (t, args, complain, in_decl);
10267 case POINTER_TYPE:
10268 case REFERENCE_TYPE:
10270 enum tree_code code;
10272 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10273 return t;
10275 code = TREE_CODE (t);
10278 /* [temp.deduct]
10280 Type deduction may fail for any of the following
10281 reasons:
10283 -- Attempting to create a pointer to reference type.
10284 -- Attempting to create a reference to a reference type or
10285 a reference to void.
10287 Core issue 106 says that creating a reference to a reference
10288 during instantiation is no longer a cause for failure. We
10289 only enforce this check in strict C++98 mode. */
10290 if ((TREE_CODE (type) == REFERENCE_TYPE
10291 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10292 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10294 static location_t last_loc;
10296 /* We keep track of the last time we issued this error
10297 message to avoid spewing a ton of messages during a
10298 single bad template instantiation. */
10299 if (complain & tf_error
10300 && last_loc != input_location)
10302 if (TREE_CODE (type) == VOID_TYPE)
10303 error ("forming reference to void");
10304 else if (code == POINTER_TYPE)
10305 error ("forming pointer to reference type %qT", type);
10306 else
10307 error ("forming reference to reference type %qT", type);
10308 last_loc = input_location;
10311 return error_mark_node;
10313 else if (code == POINTER_TYPE)
10315 r = build_pointer_type (type);
10316 if (TREE_CODE (type) == METHOD_TYPE)
10317 r = build_ptrmemfunc_type (r);
10319 else if (TREE_CODE (type) == REFERENCE_TYPE)
10320 /* In C++0x, during template argument substitution, when there is an
10321 attempt to create a reference to a reference type, reference
10322 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10324 "If a template-argument for a template-parameter T names a type
10325 that is a reference to a type A, an attempt to create the type
10326 'lvalue reference to cv T' creates the type 'lvalue reference to
10327 A,' while an attempt to create the type type rvalue reference to
10328 cv T' creates the type T"
10330 r = cp_build_reference_type
10331 (TREE_TYPE (type),
10332 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10333 else
10334 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10335 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
10337 if (r != error_mark_node)
10338 /* Will this ever be needed for TYPE_..._TO values? */
10339 layout_type (r);
10341 return r;
10343 case OFFSET_TYPE:
10345 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10346 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10348 /* [temp.deduct]
10350 Type deduction may fail for any of the following
10351 reasons:
10353 -- Attempting to create "pointer to member of T" when T
10354 is not a class type. */
10355 if (complain & tf_error)
10356 error ("creating pointer to member of non-class type %qT", r);
10357 return error_mark_node;
10359 if (TREE_CODE (type) == REFERENCE_TYPE)
10361 if (complain & tf_error)
10362 error ("creating pointer to member reference type %qT", type);
10363 return error_mark_node;
10365 if (TREE_CODE (type) == VOID_TYPE)
10367 if (complain & tf_error)
10368 error ("creating pointer to member of type void");
10369 return error_mark_node;
10371 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10372 if (TREE_CODE (type) == FUNCTION_TYPE)
10374 /* The type of the implicit object parameter gets its
10375 cv-qualifiers from the FUNCTION_TYPE. */
10376 tree memptr;
10377 tree method_type = build_memfn_type (type, r, cp_type_quals (type));
10378 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10379 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10380 complain);
10382 else
10383 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10384 TYPE_QUALS (t),
10385 complain);
10387 case FUNCTION_TYPE:
10388 case METHOD_TYPE:
10390 tree fntype;
10391 tree specs;
10392 fntype = tsubst_function_type (t, args, complain, in_decl);
10393 if (fntype == error_mark_node)
10394 return error_mark_node;
10396 /* Substitute the exception specification. */
10397 specs = tsubst_exception_specification (t, args, complain,
10398 in_decl);
10399 if (specs == error_mark_node)
10400 return error_mark_node;
10401 if (specs)
10402 fntype = build_exception_variant (fntype, specs);
10403 return fntype;
10405 case ARRAY_TYPE:
10407 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10408 if (domain == error_mark_node)
10409 return error_mark_node;
10411 /* As an optimization, we avoid regenerating the array type if
10412 it will obviously be the same as T. */
10413 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10414 return t;
10416 /* These checks should match the ones in grokdeclarator.
10418 [temp.deduct]
10420 The deduction may fail for any of the following reasons:
10422 -- Attempting to create an array with an element type that
10423 is void, a function type, or a reference type, or [DR337]
10424 an abstract class type. */
10425 if (TREE_CODE (type) == VOID_TYPE
10426 || TREE_CODE (type) == FUNCTION_TYPE
10427 || TREE_CODE (type) == REFERENCE_TYPE)
10429 if (complain & tf_error)
10430 error ("creating array of %qT", type);
10431 return error_mark_node;
10433 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10435 if (complain & tf_error)
10436 error ("creating array of %qT, which is an abstract class type",
10437 type);
10438 return error_mark_node;
10441 r = build_cplus_array_type (type, domain);
10443 if (TYPE_USER_ALIGN (t))
10445 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10446 TYPE_USER_ALIGN (r) = 1;
10449 return r;
10452 case PLUS_EXPR:
10453 case MINUS_EXPR:
10455 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10456 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10458 if (e1 == error_mark_node || e2 == error_mark_node)
10459 return error_mark_node;
10461 return fold_build2_loc (input_location,
10462 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10465 case NEGATE_EXPR:
10466 case NOP_EXPR:
10468 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10469 if (e == error_mark_node)
10470 return error_mark_node;
10472 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10475 case TYPENAME_TYPE:
10477 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10478 in_decl, /*entering_scope=*/1);
10479 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10480 complain, in_decl);
10482 if (ctx == error_mark_node || f == error_mark_node)
10483 return error_mark_node;
10485 if (!MAYBE_CLASS_TYPE_P (ctx))
10487 if (complain & tf_error)
10488 error ("%qT is not a class, struct, or union type", ctx);
10489 return error_mark_node;
10491 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10493 /* Normally, make_typename_type does not require that the CTX
10494 have complete type in order to allow things like:
10496 template <class T> struct S { typename S<T>::X Y; };
10498 But, such constructs have already been resolved by this
10499 point, so here CTX really should have complete type, unless
10500 it's a partial instantiation. */
10501 if (!(complain & tf_no_class_instantiations))
10502 ctx = complete_type (ctx);
10503 if (!COMPLETE_TYPE_P (ctx))
10505 if (complain & tf_error)
10506 cxx_incomplete_type_error (NULL_TREE, ctx);
10507 return error_mark_node;
10511 f = make_typename_type (ctx, f, typename_type,
10512 (complain & tf_error) | tf_keep_type_decl);
10513 if (f == error_mark_node)
10514 return f;
10515 if (TREE_CODE (f) == TYPE_DECL)
10517 complain |= tf_ignore_bad_quals;
10518 f = TREE_TYPE (f);
10521 if (TREE_CODE (f) != TYPENAME_TYPE)
10523 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10524 error ("%qT resolves to %qT, which is not an enumeration type",
10525 t, f);
10526 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10527 error ("%qT resolves to %qT, which is is not a class type",
10528 t, f);
10531 return cp_build_qualified_type_real
10532 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10535 case UNBOUND_CLASS_TEMPLATE:
10537 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10538 in_decl, /*entering_scope=*/1);
10539 tree name = TYPE_IDENTIFIER (t);
10540 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10542 if (ctx == error_mark_node || name == error_mark_node)
10543 return error_mark_node;
10545 if (parm_list)
10546 parm_list = tsubst_template_parms (parm_list, args, complain);
10547 return make_unbound_class_template (ctx, name, parm_list, complain);
10550 case INDIRECT_REF:
10551 case ADDR_EXPR:
10552 case CALL_EXPR:
10553 gcc_unreachable ();
10555 case ARRAY_REF:
10557 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10558 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10559 /*integral_constant_expression_p=*/false);
10560 if (e1 == error_mark_node || e2 == error_mark_node)
10561 return error_mark_node;
10563 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10566 case SCOPE_REF:
10568 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10569 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10570 if (e1 == error_mark_node || e2 == error_mark_node)
10571 return error_mark_node;
10573 return build_qualified_name (/*type=*/NULL_TREE,
10574 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10577 case TYPEOF_TYPE:
10579 tree type;
10581 ++cp_unevaluated_operand;
10582 ++c_inhibit_evaluation_warnings;
10584 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10585 complain, in_decl,
10586 /*integral_constant_expression_p=*/false);
10588 --cp_unevaluated_operand;
10589 --c_inhibit_evaluation_warnings;
10591 type = finish_typeof (type);
10592 return cp_build_qualified_type_real (type,
10593 cp_type_quals (t)
10594 | cp_type_quals (type),
10595 complain);
10598 case DECLTYPE_TYPE:
10600 tree type;
10602 ++cp_unevaluated_operand;
10603 ++c_inhibit_evaluation_warnings;
10605 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10606 complain, in_decl,
10607 /*integral_constant_expression_p=*/false);
10609 --cp_unevaluated_operand;
10610 --c_inhibit_evaluation_warnings;
10612 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10613 type = lambda_capture_field_type (type);
10614 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10615 type = lambda_return_type (type);
10616 else
10617 type = finish_decltype_type
10618 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10619 return cp_build_qualified_type_real (type,
10620 cp_type_quals (t)
10621 | cp_type_quals (type),
10622 complain);
10625 case TYPE_ARGUMENT_PACK:
10626 case NONTYPE_ARGUMENT_PACK:
10628 tree r = TYPE_P (t)
10629 ? cxx_make_type (TREE_CODE (t))
10630 : make_node (TREE_CODE (t));
10631 tree packed_out =
10632 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10633 args,
10634 complain,
10635 in_decl);
10636 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10638 /* For template nontype argument packs, also substitute into
10639 the type. */
10640 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10641 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10643 return r;
10645 break;
10647 default:
10648 sorry ("use of %qs in template",
10649 tree_code_name [(int) TREE_CODE (t)]);
10650 return error_mark_node;
10654 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10655 type of the expression on the left-hand side of the "." or "->"
10656 operator. */
10658 static tree
10659 tsubst_baselink (tree baselink, tree object_type,
10660 tree args, tsubst_flags_t complain, tree in_decl)
10662 tree name;
10663 tree qualifying_scope;
10664 tree fns;
10665 tree optype;
10666 tree template_args = 0;
10667 bool template_id_p = false;
10669 /* A baselink indicates a function from a base class. Both the
10670 BASELINK_ACCESS_BINFO and the base class referenced may
10671 indicate bases of the template class, rather than the
10672 instantiated class. In addition, lookups that were not
10673 ambiguous before may be ambiguous now. Therefore, we perform
10674 the lookup again. */
10675 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10676 qualifying_scope = tsubst (qualifying_scope, args,
10677 complain, in_decl);
10678 fns = BASELINK_FUNCTIONS (baselink);
10679 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10680 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10682 template_id_p = true;
10683 template_args = TREE_OPERAND (fns, 1);
10684 fns = TREE_OPERAND (fns, 0);
10685 if (template_args)
10686 template_args = tsubst_template_args (template_args, args,
10687 complain, in_decl);
10689 name = DECL_NAME (get_first_fn (fns));
10690 if (IDENTIFIER_TYPENAME_P (name))
10691 name = mangle_conv_op_name_for_type (optype);
10692 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10694 /* If lookup found a single function, mark it as used at this
10695 point. (If it lookup found multiple functions the one selected
10696 later by overload resolution will be marked as used at that
10697 point.) */
10698 if (BASELINK_P (baselink))
10699 fns = BASELINK_FUNCTIONS (baselink);
10700 if (!template_id_p && !really_overloaded_fn (fns))
10701 mark_used (OVL_CURRENT (fns));
10703 /* Add back the template arguments, if present. */
10704 if (BASELINK_P (baselink) && template_id_p)
10705 BASELINK_FUNCTIONS (baselink)
10706 = build_nt (TEMPLATE_ID_EXPR,
10707 BASELINK_FUNCTIONS (baselink),
10708 template_args);
10709 /* Update the conversion operator type. */
10710 BASELINK_OPTYPE (baselink) = optype;
10712 if (!object_type)
10713 object_type = current_class_type;
10714 return adjust_result_of_qualified_name_lookup (baselink,
10715 qualifying_scope,
10716 object_type);
10719 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10720 true if the qualified-id will be a postfix-expression in-and-of
10721 itself; false if more of the postfix-expression follows the
10722 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10723 of "&". */
10725 static tree
10726 tsubst_qualified_id (tree qualified_id, tree args,
10727 tsubst_flags_t complain, tree in_decl,
10728 bool done, bool address_p)
10730 tree expr;
10731 tree scope;
10732 tree name;
10733 bool is_template;
10734 tree template_args;
10736 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10738 /* Figure out what name to look up. */
10739 name = TREE_OPERAND (qualified_id, 1);
10740 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10742 is_template = true;
10743 template_args = TREE_OPERAND (name, 1);
10744 if (template_args)
10745 template_args = tsubst_template_args (template_args, args,
10746 complain, in_decl);
10747 name = TREE_OPERAND (name, 0);
10749 else
10751 is_template = false;
10752 template_args = NULL_TREE;
10755 /* Substitute into the qualifying scope. When there are no ARGS, we
10756 are just trying to simplify a non-dependent expression. In that
10757 case the qualifying scope may be dependent, and, in any case,
10758 substituting will not help. */
10759 scope = TREE_OPERAND (qualified_id, 0);
10760 if (args)
10762 scope = tsubst (scope, args, complain, in_decl);
10763 expr = tsubst_copy (name, args, complain, in_decl);
10765 else
10766 expr = name;
10768 if (dependent_scope_p (scope))
10769 return build_qualified_name (NULL_TREE, scope, expr,
10770 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10772 if (!BASELINK_P (name) && !DECL_P (expr))
10774 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10776 /* A BIT_NOT_EXPR is used to represent a destructor. */
10777 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10779 error ("qualifying type %qT does not match destructor name ~%qT",
10780 scope, TREE_OPERAND (expr, 0));
10781 expr = error_mark_node;
10783 else
10784 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10785 /*is_type_p=*/0, false);
10787 else
10788 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10789 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10790 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10792 if (complain & tf_error)
10794 error ("dependent-name %qE is parsed as a non-type, but "
10795 "instantiation yields a type", qualified_id);
10796 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10798 return error_mark_node;
10802 if (DECL_P (expr))
10804 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10805 scope);
10806 /* Remember that there was a reference to this entity. */
10807 mark_used (expr);
10810 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10812 if (complain & tf_error)
10813 qualified_name_lookup_error (scope,
10814 TREE_OPERAND (qualified_id, 1),
10815 expr, input_location);
10816 return error_mark_node;
10819 if (is_template)
10820 expr = lookup_template_function (expr, template_args);
10822 if (expr == error_mark_node && complain & tf_error)
10823 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10824 expr, input_location);
10825 else if (TYPE_P (scope))
10827 expr = (adjust_result_of_qualified_name_lookup
10828 (expr, scope, current_class_type));
10829 expr = (finish_qualified_id_expr
10830 (scope, expr, done, address_p,
10831 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10832 /*template_arg_p=*/false));
10835 /* Expressions do not generally have reference type. */
10836 if (TREE_CODE (expr) != SCOPE_REF
10837 /* However, if we're about to form a pointer-to-member, we just
10838 want the referenced member referenced. */
10839 && TREE_CODE (expr) != OFFSET_REF)
10840 expr = convert_from_reference (expr);
10842 return expr;
10845 /* Like tsubst, but deals with expressions. This function just replaces
10846 template parms; to finish processing the resultant expression, use
10847 tsubst_expr. */
10849 static tree
10850 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10852 enum tree_code code;
10853 tree r;
10855 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10856 return t;
10858 code = TREE_CODE (t);
10860 switch (code)
10862 case PARM_DECL:
10863 r = retrieve_local_specialization (t);
10865 if (r == NULL)
10867 tree c;
10868 /* This can happen for a parameter name used later in a function
10869 declaration (such as in a late-specified return type). Just
10870 make a dummy decl, since it's only used for its type. */
10871 gcc_assert (cp_unevaluated_operand != 0);
10872 /* We copy T because want to tsubst the PARM_DECL only,
10873 not the following PARM_DECLs that are chained to T. */
10874 c = copy_node (t);
10875 r = tsubst_decl (c, args, complain);
10876 /* Give it the template pattern as its context; its true context
10877 hasn't been instantiated yet and this is good enough for
10878 mangling. */
10879 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10882 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10883 r = ARGUMENT_PACK_SELECT_ARG (r);
10884 mark_used (r);
10885 return r;
10887 case CONST_DECL:
10889 tree enum_type;
10890 tree v;
10892 if (DECL_TEMPLATE_PARM_P (t))
10893 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10894 /* There is no need to substitute into namespace-scope
10895 enumerators. */
10896 if (DECL_NAMESPACE_SCOPE_P (t))
10897 return t;
10898 /* If ARGS is NULL, then T is known to be non-dependent. */
10899 if (args == NULL_TREE)
10900 return integral_constant_value (t);
10902 /* Unfortunately, we cannot just call lookup_name here.
10903 Consider:
10905 template <int I> int f() {
10906 enum E { a = I };
10907 struct S { void g() { E e = a; } };
10910 When we instantiate f<7>::S::g(), say, lookup_name is not
10911 clever enough to find f<7>::a. */
10912 enum_type
10913 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10914 /*entering_scope=*/0);
10916 for (v = TYPE_VALUES (enum_type);
10917 v != NULL_TREE;
10918 v = TREE_CHAIN (v))
10919 if (TREE_PURPOSE (v) == DECL_NAME (t))
10920 return TREE_VALUE (v);
10922 /* We didn't find the name. That should never happen; if
10923 name-lookup found it during preliminary parsing, we
10924 should find it again here during instantiation. */
10925 gcc_unreachable ();
10927 return t;
10929 case FIELD_DECL:
10930 if (DECL_CONTEXT (t))
10932 tree ctx;
10934 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10935 /*entering_scope=*/1);
10936 if (ctx != DECL_CONTEXT (t))
10938 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10939 if (!r)
10941 if (complain & tf_error)
10942 error ("using invalid field %qD", t);
10943 return error_mark_node;
10945 return r;
10949 return t;
10951 case VAR_DECL:
10952 case FUNCTION_DECL:
10953 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10954 || local_variable_p (t))
10955 t = tsubst (t, args, complain, in_decl);
10956 mark_used (t);
10957 return t;
10959 case BASELINK:
10960 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10962 case TEMPLATE_DECL:
10963 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10964 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10965 args, complain, in_decl);
10966 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10967 return tsubst (t, args, complain, in_decl);
10968 else if (DECL_CLASS_SCOPE_P (t)
10969 && uses_template_parms (DECL_CONTEXT (t)))
10971 /* Template template argument like the following example need
10972 special treatment:
10974 template <template <class> class TT> struct C {};
10975 template <class T> struct D {
10976 template <class U> struct E {};
10977 C<E> c; // #1
10979 D<int> d; // #2
10981 We are processing the template argument `E' in #1 for
10982 the template instantiation #2. Originally, `E' is a
10983 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10984 have to substitute this with one having context `D<int>'. */
10986 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10987 return lookup_field (context, DECL_NAME(t), 0, false);
10989 else
10990 /* Ordinary template template argument. */
10991 return t;
10993 case CAST_EXPR:
10994 case REINTERPRET_CAST_EXPR:
10995 case CONST_CAST_EXPR:
10996 case STATIC_CAST_EXPR:
10997 case DYNAMIC_CAST_EXPR:
10998 case NOP_EXPR:
10999 return build1
11000 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11001 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11003 case SIZEOF_EXPR:
11004 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11006 /* We only want to compute the number of arguments. */
11007 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11008 complain, in_decl);
11009 int len = 0;
11011 if (TREE_CODE (expanded) == TREE_VEC)
11012 len = TREE_VEC_LENGTH (expanded);
11014 if (expanded == error_mark_node)
11015 return error_mark_node;
11016 else if (PACK_EXPANSION_P (expanded)
11017 || (TREE_CODE (expanded) == TREE_VEC
11018 && len > 0
11019 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11021 if (TREE_CODE (expanded) == TREE_VEC)
11022 expanded = TREE_VEC_ELT (expanded, len - 1);
11024 if (TYPE_P (expanded))
11025 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11026 complain & tf_error);
11027 else
11028 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11029 complain & tf_error);
11031 else
11032 return build_int_cst (size_type_node, len);
11034 /* Fall through */
11036 case INDIRECT_REF:
11037 case NEGATE_EXPR:
11038 case TRUTH_NOT_EXPR:
11039 case BIT_NOT_EXPR:
11040 case ADDR_EXPR:
11041 case UNARY_PLUS_EXPR: /* Unary + */
11042 case ALIGNOF_EXPR:
11043 case ARROW_EXPR:
11044 case THROW_EXPR:
11045 case TYPEID_EXPR:
11046 case REALPART_EXPR:
11047 case IMAGPART_EXPR:
11048 return build1
11049 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11050 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11052 case COMPONENT_REF:
11054 tree object;
11055 tree name;
11057 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11058 name = TREE_OPERAND (t, 1);
11059 if (TREE_CODE (name) == BIT_NOT_EXPR)
11061 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11062 complain, in_decl);
11063 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11065 else if (TREE_CODE (name) == SCOPE_REF
11066 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11068 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11069 complain, in_decl);
11070 name = TREE_OPERAND (name, 1);
11071 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11072 complain, in_decl);
11073 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11074 name = build_qualified_name (/*type=*/NULL_TREE,
11075 base, name,
11076 /*template_p=*/false);
11078 else if (TREE_CODE (name) == BASELINK)
11079 name = tsubst_baselink (name,
11080 non_reference (TREE_TYPE (object)),
11081 args, complain,
11082 in_decl);
11083 else
11084 name = tsubst_copy (name, args, complain, in_decl);
11085 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11088 case PLUS_EXPR:
11089 case MINUS_EXPR:
11090 case MULT_EXPR:
11091 case TRUNC_DIV_EXPR:
11092 case CEIL_DIV_EXPR:
11093 case FLOOR_DIV_EXPR:
11094 case ROUND_DIV_EXPR:
11095 case EXACT_DIV_EXPR:
11096 case BIT_AND_EXPR:
11097 case BIT_IOR_EXPR:
11098 case BIT_XOR_EXPR:
11099 case TRUNC_MOD_EXPR:
11100 case FLOOR_MOD_EXPR:
11101 case TRUTH_ANDIF_EXPR:
11102 case TRUTH_ORIF_EXPR:
11103 case TRUTH_AND_EXPR:
11104 case TRUTH_OR_EXPR:
11105 case RSHIFT_EXPR:
11106 case LSHIFT_EXPR:
11107 case RROTATE_EXPR:
11108 case LROTATE_EXPR:
11109 case EQ_EXPR:
11110 case NE_EXPR:
11111 case MAX_EXPR:
11112 case MIN_EXPR:
11113 case LE_EXPR:
11114 case GE_EXPR:
11115 case LT_EXPR:
11116 case GT_EXPR:
11117 case COMPOUND_EXPR:
11118 case DOTSTAR_EXPR:
11119 case MEMBER_REF:
11120 case PREDECREMENT_EXPR:
11121 case PREINCREMENT_EXPR:
11122 case POSTDECREMENT_EXPR:
11123 case POSTINCREMENT_EXPR:
11124 return build_nt
11125 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11126 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11128 case SCOPE_REF:
11129 return build_qualified_name (/*type=*/NULL_TREE,
11130 tsubst_copy (TREE_OPERAND (t, 0),
11131 args, complain, in_decl),
11132 tsubst_copy (TREE_OPERAND (t, 1),
11133 args, complain, in_decl),
11134 QUALIFIED_NAME_IS_TEMPLATE (t));
11136 case ARRAY_REF:
11137 return build_nt
11138 (ARRAY_REF,
11139 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11140 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11141 NULL_TREE, NULL_TREE);
11143 case CALL_EXPR:
11145 int n = VL_EXP_OPERAND_LENGTH (t);
11146 tree result = build_vl_exp (CALL_EXPR, n);
11147 int i;
11148 for (i = 0; i < n; i++)
11149 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11150 complain, in_decl);
11151 return result;
11154 case COND_EXPR:
11155 case MODOP_EXPR:
11156 case PSEUDO_DTOR_EXPR:
11158 r = build_nt
11159 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11160 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11161 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11162 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11163 return r;
11166 case NEW_EXPR:
11168 r = build_nt
11169 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11170 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11171 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11172 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11173 return r;
11176 case DELETE_EXPR:
11178 r = build_nt
11179 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11180 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11181 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11182 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11183 return r;
11186 case TEMPLATE_ID_EXPR:
11188 /* Substituted template arguments */
11189 tree fn = TREE_OPERAND (t, 0);
11190 tree targs = TREE_OPERAND (t, 1);
11192 fn = tsubst_copy (fn, args, complain, in_decl);
11193 if (targs)
11194 targs = tsubst_template_args (targs, args, complain, in_decl);
11196 return lookup_template_function (fn, targs);
11199 case TREE_LIST:
11201 tree purpose, value, chain;
11203 if (t == void_list_node)
11204 return t;
11206 purpose = TREE_PURPOSE (t);
11207 if (purpose)
11208 purpose = tsubst_copy (purpose, args, complain, in_decl);
11209 value = TREE_VALUE (t);
11210 if (value)
11211 value = tsubst_copy (value, args, complain, in_decl);
11212 chain = TREE_CHAIN (t);
11213 if (chain && chain != void_type_node)
11214 chain = tsubst_copy (chain, args, complain, in_decl);
11215 if (purpose == TREE_PURPOSE (t)
11216 && value == TREE_VALUE (t)
11217 && chain == TREE_CHAIN (t))
11218 return t;
11219 return tree_cons (purpose, value, chain);
11222 case RECORD_TYPE:
11223 case UNION_TYPE:
11224 case ENUMERAL_TYPE:
11225 case INTEGER_TYPE:
11226 case TEMPLATE_TYPE_PARM:
11227 case TEMPLATE_TEMPLATE_PARM:
11228 case BOUND_TEMPLATE_TEMPLATE_PARM:
11229 case TEMPLATE_PARM_INDEX:
11230 case POINTER_TYPE:
11231 case REFERENCE_TYPE:
11232 case OFFSET_TYPE:
11233 case FUNCTION_TYPE:
11234 case METHOD_TYPE:
11235 case ARRAY_TYPE:
11236 case TYPENAME_TYPE:
11237 case UNBOUND_CLASS_TEMPLATE:
11238 case TYPEOF_TYPE:
11239 case DECLTYPE_TYPE:
11240 case TYPE_DECL:
11241 return tsubst (t, args, complain, in_decl);
11243 case IDENTIFIER_NODE:
11244 if (IDENTIFIER_TYPENAME_P (t))
11246 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11247 return mangle_conv_op_name_for_type (new_type);
11249 else
11250 return t;
11252 case CONSTRUCTOR:
11253 /* This is handled by tsubst_copy_and_build. */
11254 gcc_unreachable ();
11256 case VA_ARG_EXPR:
11257 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11258 in_decl),
11259 tsubst (TREE_TYPE (t), args, complain, in_decl));
11261 case CLEANUP_POINT_EXPR:
11262 /* We shouldn't have built any of these during initial template
11263 generation. Instead, they should be built during instantiation
11264 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11265 gcc_unreachable ();
11267 case OFFSET_REF:
11268 mark_used (TREE_OPERAND (t, 1));
11269 return t;
11271 case EXPR_PACK_EXPANSION:
11272 error ("invalid use of pack expansion expression");
11273 return error_mark_node;
11275 case NONTYPE_ARGUMENT_PACK:
11276 error ("use %<...%> to expand argument pack");
11277 return error_mark_node;
11279 default:
11280 return t;
11284 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11286 static tree
11287 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11288 tree in_decl)
11290 tree new_clauses = NULL, nc, oc;
11292 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11294 nc = copy_node (oc);
11295 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11296 new_clauses = nc;
11298 switch (OMP_CLAUSE_CODE (nc))
11300 case OMP_CLAUSE_LASTPRIVATE:
11301 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11303 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11304 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11305 in_decl, /*integral_constant_expression_p=*/false);
11306 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11307 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11309 /* FALLTHRU */
11310 case OMP_CLAUSE_PRIVATE:
11311 case OMP_CLAUSE_SHARED:
11312 case OMP_CLAUSE_FIRSTPRIVATE:
11313 case OMP_CLAUSE_REDUCTION:
11314 case OMP_CLAUSE_COPYIN:
11315 case OMP_CLAUSE_COPYPRIVATE:
11316 case OMP_CLAUSE_IF:
11317 case OMP_CLAUSE_NUM_THREADS:
11318 case OMP_CLAUSE_SCHEDULE:
11319 case OMP_CLAUSE_COLLAPSE:
11320 OMP_CLAUSE_OPERAND (nc, 0)
11321 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11322 in_decl, /*integral_constant_expression_p=*/false);
11323 break;
11324 case OMP_CLAUSE_NOWAIT:
11325 case OMP_CLAUSE_ORDERED:
11326 case OMP_CLAUSE_DEFAULT:
11327 case OMP_CLAUSE_UNTIED:
11328 break;
11329 default:
11330 gcc_unreachable ();
11334 return finish_omp_clauses (nreverse (new_clauses));
11337 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11339 static tree
11340 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11341 tree in_decl)
11343 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11345 tree purpose, value, chain;
11347 if (t == NULL)
11348 return t;
11350 if (TREE_CODE (t) != TREE_LIST)
11351 return tsubst_copy_and_build (t, args, complain, in_decl,
11352 /*function_p=*/false,
11353 /*integral_constant_expression_p=*/false);
11355 if (t == void_list_node)
11356 return t;
11358 purpose = TREE_PURPOSE (t);
11359 if (purpose)
11360 purpose = RECUR (purpose);
11361 value = TREE_VALUE (t);
11362 if (value && TREE_CODE (value) != LABEL_DECL)
11363 value = RECUR (value);
11364 chain = TREE_CHAIN (t);
11365 if (chain && chain != void_type_node)
11366 chain = RECUR (chain);
11367 return tree_cons (purpose, value, chain);
11368 #undef RECUR
11371 /* Substitute one OMP_FOR iterator. */
11373 static void
11374 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11375 tree condv, tree incrv, tree *clauses,
11376 tree args, tsubst_flags_t complain, tree in_decl,
11377 bool integral_constant_expression_p)
11379 #define RECUR(NODE) \
11380 tsubst_expr ((NODE), args, complain, in_decl, \
11381 integral_constant_expression_p)
11382 tree decl, init, cond, incr, auto_node;
11384 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11385 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11386 decl = RECUR (TREE_OPERAND (init, 0));
11387 init = TREE_OPERAND (init, 1);
11388 auto_node = type_uses_auto (TREE_TYPE (decl));
11389 if (auto_node && init)
11391 tree init_expr = init;
11392 if (TREE_CODE (init_expr) == DECL_EXPR)
11393 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11394 init_expr = RECUR (init_expr);
11395 TREE_TYPE (decl)
11396 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11398 gcc_assert (!type_dependent_expression_p (decl));
11400 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11402 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11403 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11404 if (TREE_CODE (incr) == MODIFY_EXPR)
11405 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11406 RECUR (TREE_OPERAND (incr, 1)),
11407 complain);
11408 else
11409 incr = RECUR (incr);
11410 TREE_VEC_ELT (declv, i) = decl;
11411 TREE_VEC_ELT (initv, i) = init;
11412 TREE_VEC_ELT (condv, i) = cond;
11413 TREE_VEC_ELT (incrv, i) = incr;
11414 return;
11417 if (init && TREE_CODE (init) != DECL_EXPR)
11419 tree c;
11420 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11422 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11423 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11424 && OMP_CLAUSE_DECL (c) == decl)
11425 break;
11426 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11427 && OMP_CLAUSE_DECL (c) == decl)
11428 error ("iteration variable %qD should not be firstprivate", decl);
11429 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11430 && OMP_CLAUSE_DECL (c) == decl)
11431 error ("iteration variable %qD should not be reduction", decl);
11433 if (c == NULL)
11435 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11436 OMP_CLAUSE_DECL (c) = decl;
11437 c = finish_omp_clauses (c);
11438 if (c)
11440 OMP_CLAUSE_CHAIN (c) = *clauses;
11441 *clauses = c;
11445 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11446 if (COMPARISON_CLASS_P (cond))
11447 cond = build2 (TREE_CODE (cond), boolean_type_node,
11448 RECUR (TREE_OPERAND (cond, 0)),
11449 RECUR (TREE_OPERAND (cond, 1)));
11450 else
11451 cond = RECUR (cond);
11452 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11453 switch (TREE_CODE (incr))
11455 case PREINCREMENT_EXPR:
11456 case PREDECREMENT_EXPR:
11457 case POSTINCREMENT_EXPR:
11458 case POSTDECREMENT_EXPR:
11459 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11460 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11461 break;
11462 case MODIFY_EXPR:
11463 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11464 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11466 tree rhs = TREE_OPERAND (incr, 1);
11467 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11468 RECUR (TREE_OPERAND (incr, 0)),
11469 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11470 RECUR (TREE_OPERAND (rhs, 0)),
11471 RECUR (TREE_OPERAND (rhs, 1))));
11473 else
11474 incr = RECUR (incr);
11475 break;
11476 case MODOP_EXPR:
11477 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11478 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11480 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11481 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11482 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11483 TREE_TYPE (decl), lhs,
11484 RECUR (TREE_OPERAND (incr, 2))));
11486 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11487 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11488 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11490 tree rhs = TREE_OPERAND (incr, 2);
11491 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11492 RECUR (TREE_OPERAND (incr, 0)),
11493 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11494 RECUR (TREE_OPERAND (rhs, 0)),
11495 RECUR (TREE_OPERAND (rhs, 1))));
11497 else
11498 incr = RECUR (incr);
11499 break;
11500 default:
11501 incr = RECUR (incr);
11502 break;
11505 TREE_VEC_ELT (declv, i) = decl;
11506 TREE_VEC_ELT (initv, i) = init;
11507 TREE_VEC_ELT (condv, i) = cond;
11508 TREE_VEC_ELT (incrv, i) = incr;
11509 #undef RECUR
11512 /* Like tsubst_copy for expressions, etc. but also does semantic
11513 processing. */
11515 static tree
11516 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11517 bool integral_constant_expression_p)
11519 #define RECUR(NODE) \
11520 tsubst_expr ((NODE), args, complain, in_decl, \
11521 integral_constant_expression_p)
11523 tree stmt, tmp;
11525 if (t == NULL_TREE || t == error_mark_node)
11526 return t;
11528 if (EXPR_HAS_LOCATION (t))
11529 input_location = EXPR_LOCATION (t);
11530 if (STATEMENT_CODE_P (TREE_CODE (t)))
11531 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11533 switch (TREE_CODE (t))
11535 case STATEMENT_LIST:
11537 tree_stmt_iterator i;
11538 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11539 RECUR (tsi_stmt (i));
11540 break;
11543 case CTOR_INITIALIZER:
11544 finish_mem_initializers (tsubst_initializer_list
11545 (TREE_OPERAND (t, 0), args));
11546 break;
11548 case RETURN_EXPR:
11549 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11550 break;
11552 case EXPR_STMT:
11553 tmp = RECUR (EXPR_STMT_EXPR (t));
11554 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11555 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11556 else
11557 finish_expr_stmt (tmp);
11558 break;
11560 case USING_STMT:
11561 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11562 break;
11564 case DECL_EXPR:
11566 tree decl;
11567 tree init;
11569 decl = DECL_EXPR_DECL (t);
11570 if (TREE_CODE (decl) == LABEL_DECL)
11571 finish_label_decl (DECL_NAME (decl));
11572 else if (TREE_CODE (decl) == USING_DECL)
11574 tree scope = USING_DECL_SCOPE (decl);
11575 tree name = DECL_NAME (decl);
11576 tree decl;
11578 scope = RECUR (scope);
11579 decl = lookup_qualified_name (scope, name,
11580 /*is_type_p=*/false,
11581 /*complain=*/false);
11582 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11583 qualified_name_lookup_error (scope, name, decl, input_location);
11584 else
11585 do_local_using_decl (decl, scope, name);
11587 else
11589 init = DECL_INITIAL (decl);
11590 decl = tsubst (decl, args, complain, in_decl);
11591 if (decl != error_mark_node)
11593 /* By marking the declaration as instantiated, we avoid
11594 trying to instantiate it. Since instantiate_decl can't
11595 handle local variables, and since we've already done
11596 all that needs to be done, that's the right thing to
11597 do. */
11598 if (TREE_CODE (decl) == VAR_DECL)
11599 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11600 if (TREE_CODE (decl) == VAR_DECL
11601 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11602 /* Anonymous aggregates are a special case. */
11603 finish_anon_union (decl);
11604 else
11606 maybe_push_decl (decl);
11607 if (TREE_CODE (decl) == VAR_DECL
11608 && DECL_PRETTY_FUNCTION_P (decl))
11610 /* For __PRETTY_FUNCTION__ we have to adjust the
11611 initializer. */
11612 const char *const name
11613 = cxx_printable_name (current_function_decl, 2);
11614 init = cp_fname_init (name, &TREE_TYPE (decl));
11616 else
11618 tree t = RECUR (init);
11620 if (init && !t)
11621 /* If we had an initializer but it
11622 instantiated to nothing,
11623 value-initialize the object. This will
11624 only occur when the initializer was a
11625 pack expansion where the parameter packs
11626 used in that expansion were of length
11627 zero. */
11628 init = build_value_init (TREE_TYPE (decl));
11629 else
11630 init = t;
11633 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11638 /* A DECL_EXPR can also be used as an expression, in the condition
11639 clause of an if/for/while construct. */
11640 return decl;
11643 case FOR_STMT:
11644 stmt = begin_for_stmt ();
11645 RECUR (FOR_INIT_STMT (t));
11646 finish_for_init_stmt (stmt);
11647 tmp = RECUR (FOR_COND (t));
11648 finish_for_cond (tmp, stmt);
11649 tmp = RECUR (FOR_EXPR (t));
11650 finish_for_expr (tmp, stmt);
11651 RECUR (FOR_BODY (t));
11652 finish_for_stmt (stmt);
11653 break;
11655 case WHILE_STMT:
11656 stmt = begin_while_stmt ();
11657 tmp = RECUR (WHILE_COND (t));
11658 finish_while_stmt_cond (tmp, stmt);
11659 RECUR (WHILE_BODY (t));
11660 finish_while_stmt (stmt);
11661 break;
11663 case DO_STMT:
11664 stmt = begin_do_stmt ();
11665 RECUR (DO_BODY (t));
11666 finish_do_body (stmt);
11667 tmp = RECUR (DO_COND (t));
11668 finish_do_stmt (tmp, stmt);
11669 break;
11671 case IF_STMT:
11672 stmt = begin_if_stmt ();
11673 tmp = RECUR (IF_COND (t));
11674 finish_if_stmt_cond (tmp, stmt);
11675 RECUR (THEN_CLAUSE (t));
11676 finish_then_clause (stmt);
11678 if (ELSE_CLAUSE (t))
11680 begin_else_clause (stmt);
11681 RECUR (ELSE_CLAUSE (t));
11682 finish_else_clause (stmt);
11685 finish_if_stmt (stmt);
11686 break;
11688 case BIND_EXPR:
11689 if (BIND_EXPR_BODY_BLOCK (t))
11690 stmt = begin_function_body ();
11691 else
11692 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11693 ? BCS_TRY_BLOCK : 0);
11695 RECUR (BIND_EXPR_BODY (t));
11697 if (BIND_EXPR_BODY_BLOCK (t))
11698 finish_function_body (stmt);
11699 else
11700 finish_compound_stmt (stmt);
11701 break;
11703 case BREAK_STMT:
11704 finish_break_stmt ();
11705 break;
11707 case CONTINUE_STMT:
11708 finish_continue_stmt ();
11709 break;
11711 case SWITCH_STMT:
11712 stmt = begin_switch_stmt ();
11713 tmp = RECUR (SWITCH_STMT_COND (t));
11714 finish_switch_cond (tmp, stmt);
11715 RECUR (SWITCH_STMT_BODY (t));
11716 finish_switch_stmt (stmt);
11717 break;
11719 case CASE_LABEL_EXPR:
11720 finish_case_label (EXPR_LOCATION (t),
11721 RECUR (CASE_LOW (t)),
11722 RECUR (CASE_HIGH (t)));
11723 break;
11725 case LABEL_EXPR:
11727 tree decl = LABEL_EXPR_LABEL (t);
11728 tree label;
11730 label = finish_label_stmt (DECL_NAME (decl));
11731 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11732 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11734 break;
11736 case GOTO_EXPR:
11737 tmp = GOTO_DESTINATION (t);
11738 if (TREE_CODE (tmp) != LABEL_DECL)
11739 /* Computed goto's must be tsubst'd into. On the other hand,
11740 non-computed gotos must not be; the identifier in question
11741 will have no binding. */
11742 tmp = RECUR (tmp);
11743 else
11744 tmp = DECL_NAME (tmp);
11745 finish_goto_stmt (tmp);
11746 break;
11748 case ASM_EXPR:
11749 tmp = finish_asm_stmt
11750 (ASM_VOLATILE_P (t),
11751 RECUR (ASM_STRING (t)),
11752 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11753 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11754 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11755 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11757 tree asm_expr = tmp;
11758 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11759 asm_expr = TREE_OPERAND (asm_expr, 0);
11760 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11762 break;
11764 case TRY_BLOCK:
11765 if (CLEANUP_P (t))
11767 stmt = begin_try_block ();
11768 RECUR (TRY_STMTS (t));
11769 finish_cleanup_try_block (stmt);
11770 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11772 else
11774 tree compound_stmt = NULL_TREE;
11776 if (FN_TRY_BLOCK_P (t))
11777 stmt = begin_function_try_block (&compound_stmt);
11778 else
11779 stmt = begin_try_block ();
11781 RECUR (TRY_STMTS (t));
11783 if (FN_TRY_BLOCK_P (t))
11784 finish_function_try_block (stmt);
11785 else
11786 finish_try_block (stmt);
11788 RECUR (TRY_HANDLERS (t));
11789 if (FN_TRY_BLOCK_P (t))
11790 finish_function_handler_sequence (stmt, compound_stmt);
11791 else
11792 finish_handler_sequence (stmt);
11794 break;
11796 case HANDLER:
11798 tree decl = HANDLER_PARMS (t);
11800 if (decl)
11802 decl = tsubst (decl, args, complain, in_decl);
11803 /* Prevent instantiate_decl from trying to instantiate
11804 this variable. We've already done all that needs to be
11805 done. */
11806 if (decl != error_mark_node)
11807 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11809 stmt = begin_handler ();
11810 finish_handler_parms (decl, stmt);
11811 RECUR (HANDLER_BODY (t));
11812 finish_handler (stmt);
11814 break;
11816 case TAG_DEFN:
11817 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11818 break;
11820 case STATIC_ASSERT:
11822 tree condition =
11823 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11824 args,
11825 complain, in_decl,
11826 /*integral_constant_expression_p=*/true);
11827 finish_static_assert (condition,
11828 STATIC_ASSERT_MESSAGE (t),
11829 STATIC_ASSERT_SOURCE_LOCATION (t),
11830 /*member_p=*/false);
11832 break;
11834 case OMP_PARALLEL:
11835 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11836 args, complain, in_decl);
11837 stmt = begin_omp_parallel ();
11838 RECUR (OMP_PARALLEL_BODY (t));
11839 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11840 = OMP_PARALLEL_COMBINED (t);
11841 break;
11843 case OMP_TASK:
11844 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11845 args, complain, in_decl);
11846 stmt = begin_omp_task ();
11847 RECUR (OMP_TASK_BODY (t));
11848 finish_omp_task (tmp, stmt);
11849 break;
11851 case OMP_FOR:
11853 tree clauses, body, pre_body;
11854 tree declv, initv, condv, incrv;
11855 int i;
11857 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11858 args, complain, in_decl);
11859 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11860 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11861 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11862 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11864 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11865 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11866 &clauses, args, complain, in_decl,
11867 integral_constant_expression_p);
11869 stmt = begin_omp_structured_block ();
11871 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11872 if (TREE_VEC_ELT (initv, i) == NULL
11873 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11874 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11875 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11877 tree init = RECUR (TREE_VEC_ELT (initv, i));
11878 gcc_assert (init == TREE_VEC_ELT (declv, i));
11879 TREE_VEC_ELT (initv, i) = NULL_TREE;
11881 else
11883 tree decl_expr = TREE_VEC_ELT (initv, i);
11884 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11885 gcc_assert (init != NULL);
11886 TREE_VEC_ELT (initv, i) = RECUR (init);
11887 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11888 RECUR (decl_expr);
11889 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11892 pre_body = push_stmt_list ();
11893 RECUR (OMP_FOR_PRE_BODY (t));
11894 pre_body = pop_stmt_list (pre_body);
11896 body = push_stmt_list ();
11897 RECUR (OMP_FOR_BODY (t));
11898 body = pop_stmt_list (body);
11900 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11901 body, pre_body, clauses);
11903 add_stmt (finish_omp_structured_block (stmt));
11905 break;
11907 case OMP_SECTIONS:
11908 case OMP_SINGLE:
11909 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11910 stmt = push_stmt_list ();
11911 RECUR (OMP_BODY (t));
11912 stmt = pop_stmt_list (stmt);
11914 t = copy_node (t);
11915 OMP_BODY (t) = stmt;
11916 OMP_CLAUSES (t) = tmp;
11917 add_stmt (t);
11918 break;
11920 case OMP_SECTION:
11921 case OMP_CRITICAL:
11922 case OMP_MASTER:
11923 case OMP_ORDERED:
11924 stmt = push_stmt_list ();
11925 RECUR (OMP_BODY (t));
11926 stmt = pop_stmt_list (stmt);
11928 t = copy_node (t);
11929 OMP_BODY (t) = stmt;
11930 add_stmt (t);
11931 break;
11933 case OMP_ATOMIC:
11934 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11936 tree op1 = TREE_OPERAND (t, 1);
11937 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11938 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11939 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11941 break;
11943 case EXPR_PACK_EXPANSION:
11944 error ("invalid use of pack expansion expression");
11945 return error_mark_node;
11947 case NONTYPE_ARGUMENT_PACK:
11948 error ("use %<...%> to expand argument pack");
11949 return error_mark_node;
11951 default:
11952 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11954 return tsubst_copy_and_build (t, args, complain, in_decl,
11955 /*function_p=*/false,
11956 integral_constant_expression_p);
11959 return NULL_TREE;
11960 #undef RECUR
11963 /* T is a postfix-expression that is not being used in a function
11964 call. Return the substituted version of T. */
11966 static tree
11967 tsubst_non_call_postfix_expression (tree t, tree args,
11968 tsubst_flags_t complain,
11969 tree in_decl)
11971 if (TREE_CODE (t) == SCOPE_REF)
11972 t = tsubst_qualified_id (t, args, complain, in_decl,
11973 /*done=*/false, /*address_p=*/false);
11974 else
11975 t = tsubst_copy_and_build (t, args, complain, in_decl,
11976 /*function_p=*/false,
11977 /*integral_constant_expression_p=*/false);
11979 return t;
11982 /* Like tsubst but deals with expressions and performs semantic
11983 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11985 tree
11986 tsubst_copy_and_build (tree t,
11987 tree args,
11988 tsubst_flags_t complain,
11989 tree in_decl,
11990 bool function_p,
11991 bool integral_constant_expression_p)
11993 #define RECUR(NODE) \
11994 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11995 /*function_p=*/false, \
11996 integral_constant_expression_p)
11998 tree op1;
12000 if (t == NULL_TREE || t == error_mark_node)
12001 return t;
12003 switch (TREE_CODE (t))
12005 case USING_DECL:
12006 t = DECL_NAME (t);
12007 /* Fall through. */
12008 case IDENTIFIER_NODE:
12010 tree decl;
12011 cp_id_kind idk;
12012 bool non_integral_constant_expression_p;
12013 const char *error_msg;
12015 if (IDENTIFIER_TYPENAME_P (t))
12017 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12018 t = mangle_conv_op_name_for_type (new_type);
12021 /* Look up the name. */
12022 decl = lookup_name (t);
12024 /* By convention, expressions use ERROR_MARK_NODE to indicate
12025 failure, not NULL_TREE. */
12026 if (decl == NULL_TREE)
12027 decl = error_mark_node;
12029 decl = finish_id_expression (t, decl, NULL_TREE,
12030 &idk,
12031 integral_constant_expression_p,
12032 /*allow_non_integral_constant_expression_p=*/false,
12033 &non_integral_constant_expression_p,
12034 /*template_p=*/false,
12035 /*done=*/true,
12036 /*address_p=*/false,
12037 /*template_arg_p=*/false,
12038 &error_msg,
12039 input_location);
12040 if (error_msg)
12041 error (error_msg);
12042 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12043 decl = unqualified_name_lookup_error (decl);
12044 return decl;
12047 case TEMPLATE_ID_EXPR:
12049 tree object;
12050 tree templ = RECUR (TREE_OPERAND (t, 0));
12051 tree targs = TREE_OPERAND (t, 1);
12053 if (targs)
12054 targs = tsubst_template_args (targs, args, complain, in_decl);
12056 if (TREE_CODE (templ) == COMPONENT_REF)
12058 object = TREE_OPERAND (templ, 0);
12059 templ = TREE_OPERAND (templ, 1);
12061 else
12062 object = NULL_TREE;
12063 templ = lookup_template_function (templ, targs);
12065 if (object)
12066 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12067 object, templ, NULL_TREE);
12068 else
12069 return baselink_for_fns (templ);
12072 case INDIRECT_REF:
12074 tree r = RECUR (TREE_OPERAND (t, 0));
12076 if (REFERENCE_REF_P (t))
12078 /* A type conversion to reference type will be enclosed in
12079 such an indirect ref, but the substitution of the cast
12080 will have also added such an indirect ref. */
12081 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12082 r = convert_from_reference (r);
12084 else
12085 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12086 return r;
12089 case NOP_EXPR:
12090 return build_nop
12091 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12092 RECUR (TREE_OPERAND (t, 0)));
12094 case CAST_EXPR:
12095 case REINTERPRET_CAST_EXPR:
12096 case CONST_CAST_EXPR:
12097 case DYNAMIC_CAST_EXPR:
12098 case STATIC_CAST_EXPR:
12100 tree type;
12101 tree op;
12103 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12104 if (integral_constant_expression_p
12105 && !cast_valid_in_integral_constant_expression_p (type))
12107 if (complain & tf_error)
12108 error ("a cast to a type other than an integral or "
12109 "enumeration type cannot appear in a constant-expression");
12110 return error_mark_node;
12113 op = RECUR (TREE_OPERAND (t, 0));
12115 switch (TREE_CODE (t))
12117 case CAST_EXPR:
12118 return build_functional_cast (type, op, complain);
12119 case REINTERPRET_CAST_EXPR:
12120 return build_reinterpret_cast (type, op, complain);
12121 case CONST_CAST_EXPR:
12122 return build_const_cast (type, op, complain);
12123 case DYNAMIC_CAST_EXPR:
12124 return build_dynamic_cast (type, op, complain);
12125 case STATIC_CAST_EXPR:
12126 return build_static_cast (type, op, complain);
12127 default:
12128 gcc_unreachable ();
12132 case POSTDECREMENT_EXPR:
12133 case POSTINCREMENT_EXPR:
12134 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12135 args, complain, in_decl);
12136 return build_x_unary_op (TREE_CODE (t), op1, complain);
12138 case PREDECREMENT_EXPR:
12139 case PREINCREMENT_EXPR:
12140 case NEGATE_EXPR:
12141 case BIT_NOT_EXPR:
12142 case ABS_EXPR:
12143 case TRUTH_NOT_EXPR:
12144 case UNARY_PLUS_EXPR: /* Unary + */
12145 case REALPART_EXPR:
12146 case IMAGPART_EXPR:
12147 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
12148 complain);
12150 case ADDR_EXPR:
12151 op1 = TREE_OPERAND (t, 0);
12152 if (TREE_CODE (op1) == SCOPE_REF)
12153 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12154 /*done=*/true, /*address_p=*/true);
12155 else
12156 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12157 in_decl);
12158 if (TREE_CODE (op1) == LABEL_DECL)
12159 return finish_label_address_expr (DECL_NAME (op1),
12160 EXPR_LOCATION (op1));
12161 return build_x_unary_op (ADDR_EXPR, op1, complain);
12163 case PLUS_EXPR:
12164 case MINUS_EXPR:
12165 case MULT_EXPR:
12166 case TRUNC_DIV_EXPR:
12167 case CEIL_DIV_EXPR:
12168 case FLOOR_DIV_EXPR:
12169 case ROUND_DIV_EXPR:
12170 case EXACT_DIV_EXPR:
12171 case BIT_AND_EXPR:
12172 case BIT_IOR_EXPR:
12173 case BIT_XOR_EXPR:
12174 case TRUNC_MOD_EXPR:
12175 case FLOOR_MOD_EXPR:
12176 case TRUTH_ANDIF_EXPR:
12177 case TRUTH_ORIF_EXPR:
12178 case TRUTH_AND_EXPR:
12179 case TRUTH_OR_EXPR:
12180 case RSHIFT_EXPR:
12181 case LSHIFT_EXPR:
12182 case RROTATE_EXPR:
12183 case LROTATE_EXPR:
12184 case EQ_EXPR:
12185 case NE_EXPR:
12186 case MAX_EXPR:
12187 case MIN_EXPR:
12188 case LE_EXPR:
12189 case GE_EXPR:
12190 case LT_EXPR:
12191 case GT_EXPR:
12192 case MEMBER_REF:
12193 case DOTSTAR_EXPR:
12194 return build_x_binary_op
12195 (TREE_CODE (t),
12196 RECUR (TREE_OPERAND (t, 0)),
12197 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
12198 ? ERROR_MARK
12199 : TREE_CODE (TREE_OPERAND (t, 0))),
12200 RECUR (TREE_OPERAND (t, 1)),
12201 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
12202 ? ERROR_MARK
12203 : TREE_CODE (TREE_OPERAND (t, 1))),
12204 /*overloaded_p=*/NULL,
12205 complain);
12207 case SCOPE_REF:
12208 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12209 /*address_p=*/false);
12210 case ARRAY_REF:
12211 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12212 args, complain, in_decl);
12213 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12215 case SIZEOF_EXPR:
12216 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12217 return tsubst_copy (t, args, complain, in_decl);
12218 /* Fall through */
12220 case ALIGNOF_EXPR:
12221 op1 = TREE_OPERAND (t, 0);
12222 if (!args)
12224 /* When there are no ARGS, we are trying to evaluate a
12225 non-dependent expression from the parser. Trying to do
12226 the substitutions may not work. */
12227 if (!TYPE_P (op1))
12228 op1 = TREE_TYPE (op1);
12230 else
12232 ++cp_unevaluated_operand;
12233 ++c_inhibit_evaluation_warnings;
12234 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12235 /*function_p=*/false,
12236 /*integral_constant_expression_p=*/false);
12237 --cp_unevaluated_operand;
12238 --c_inhibit_evaluation_warnings;
12240 if (TYPE_P (op1))
12241 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12242 complain & tf_error);
12243 else
12244 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12245 complain & tf_error);
12247 case MODOP_EXPR:
12249 tree r = build_x_modify_expr
12250 (RECUR (TREE_OPERAND (t, 0)),
12251 TREE_CODE (TREE_OPERAND (t, 1)),
12252 RECUR (TREE_OPERAND (t, 2)),
12253 complain);
12254 /* TREE_NO_WARNING must be set if either the expression was
12255 parenthesized or it uses an operator such as >>= rather
12256 than plain assignment. In the former case, it was already
12257 set and must be copied. In the latter case,
12258 build_x_modify_expr sets it and it must not be reset
12259 here. */
12260 if (TREE_NO_WARNING (t))
12261 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12262 return r;
12265 case ARROW_EXPR:
12266 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12267 args, complain, in_decl);
12268 /* Remember that there was a reference to this entity. */
12269 if (DECL_P (op1))
12270 mark_used (op1);
12271 return build_x_arrow (op1);
12273 case NEW_EXPR:
12275 tree placement = RECUR (TREE_OPERAND (t, 0));
12276 tree init = RECUR (TREE_OPERAND (t, 3));
12277 VEC(tree,gc) *placement_vec;
12278 VEC(tree,gc) *init_vec;
12279 tree ret;
12281 if (placement == NULL_TREE)
12282 placement_vec = NULL;
12283 else
12285 placement_vec = make_tree_vector ();
12286 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12287 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12290 /* If there was an initializer in the original tree, but it
12291 instantiated to an empty list, then we should pass a
12292 non-NULL empty vector to tell build_new that it was an
12293 empty initializer() rather than no initializer. This can
12294 only happen when the initializer is a pack expansion whose
12295 parameter packs are of length zero. */
12296 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12297 init_vec = NULL;
12298 else
12300 init_vec = make_tree_vector ();
12301 if (init == void_zero_node)
12302 gcc_assert (init_vec != NULL);
12303 else
12305 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12306 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12310 ret = build_new (&placement_vec,
12311 RECUR (TREE_OPERAND (t, 1)),
12312 RECUR (TREE_OPERAND (t, 2)),
12313 &init_vec,
12314 NEW_EXPR_USE_GLOBAL (t),
12315 complain);
12317 if (placement_vec != NULL)
12318 release_tree_vector (placement_vec);
12319 if (init_vec != NULL)
12320 release_tree_vector (init_vec);
12322 return ret;
12325 case DELETE_EXPR:
12326 return delete_sanity
12327 (RECUR (TREE_OPERAND (t, 0)),
12328 RECUR (TREE_OPERAND (t, 1)),
12329 DELETE_EXPR_USE_VEC (t),
12330 DELETE_EXPR_USE_GLOBAL (t));
12332 case COMPOUND_EXPR:
12333 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12334 RECUR (TREE_OPERAND (t, 1)),
12335 complain);
12337 case CALL_EXPR:
12339 tree function;
12340 VEC(tree,gc) *call_args;
12341 unsigned int nargs, i;
12342 bool qualified_p;
12343 bool koenig_p;
12344 tree ret;
12346 function = CALL_EXPR_FN (t);
12347 /* When we parsed the expression, we determined whether or
12348 not Koenig lookup should be performed. */
12349 koenig_p = KOENIG_LOOKUP_P (t);
12350 if (TREE_CODE (function) == SCOPE_REF)
12352 qualified_p = true;
12353 function = tsubst_qualified_id (function, args, complain, in_decl,
12354 /*done=*/false,
12355 /*address_p=*/false);
12357 else
12359 if (TREE_CODE (function) == COMPONENT_REF)
12361 tree op = TREE_OPERAND (function, 1);
12363 qualified_p = (TREE_CODE (op) == SCOPE_REF
12364 || (BASELINK_P (op)
12365 && BASELINK_QUALIFIED_P (op)));
12367 else
12368 qualified_p = false;
12370 function = tsubst_copy_and_build (function, args, complain,
12371 in_decl,
12372 !qualified_p,
12373 integral_constant_expression_p);
12375 if (BASELINK_P (function))
12376 qualified_p = true;
12379 nargs = call_expr_nargs (t);
12380 call_args = make_tree_vector ();
12381 for (i = 0; i < nargs; ++i)
12383 tree arg = CALL_EXPR_ARG (t, i);
12385 if (!PACK_EXPANSION_P (arg))
12386 VEC_safe_push (tree, gc, call_args,
12387 RECUR (CALL_EXPR_ARG (t, i)));
12388 else
12390 /* Expand the pack expansion and push each entry onto
12391 CALL_ARGS. */
12392 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12393 if (TREE_CODE (arg) == TREE_VEC)
12395 unsigned int len, j;
12397 len = TREE_VEC_LENGTH (arg);
12398 for (j = 0; j < len; ++j)
12400 tree value = TREE_VEC_ELT (arg, j);
12401 if (value != NULL_TREE)
12402 value = convert_from_reference (value);
12403 VEC_safe_push (tree, gc, call_args, value);
12406 else
12408 /* A partial substitution. Add one entry. */
12409 VEC_safe_push (tree, gc, call_args, arg);
12414 /* We do not perform argument-dependent lookup if normal
12415 lookup finds a non-function, in accordance with the
12416 expected resolution of DR 218. */
12417 if (koenig_p
12418 && ((is_overloaded_fn (function)
12419 /* If lookup found a member function, the Koenig lookup is
12420 not appropriate, even if an unqualified-name was used
12421 to denote the function. */
12422 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12423 || TREE_CODE (function) == IDENTIFIER_NODE)
12424 /* Only do this when substitution turns a dependent call
12425 into a non-dependent call. */
12426 && type_dependent_expression_p_push (t)
12427 && !any_type_dependent_arguments_p (call_args))
12428 function = perform_koenig_lookup (function, call_args);
12430 if (TREE_CODE (function) == IDENTIFIER_NODE)
12432 unqualified_name_lookup_error (function);
12433 release_tree_vector (call_args);
12434 return error_mark_node;
12437 /* Remember that there was a reference to this entity. */
12438 if (DECL_P (function))
12439 mark_used (function);
12441 if (TREE_CODE (function) == OFFSET_REF)
12442 ret = build_offset_ref_call_from_tree (function, &call_args);
12443 else if (TREE_CODE (function) == COMPONENT_REF)
12445 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12446 ret = finish_call_expr (function, &call_args,
12447 /*disallow_virtual=*/false,
12448 /*koenig_p=*/false,
12449 complain);
12450 else
12451 ret = (build_new_method_call
12452 (TREE_OPERAND (function, 0),
12453 TREE_OPERAND (function, 1),
12454 &call_args, NULL_TREE,
12455 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12456 /*fn_p=*/NULL,
12457 complain));
12459 else
12460 ret = finish_call_expr (function, &call_args,
12461 /*disallow_virtual=*/qualified_p,
12462 koenig_p,
12463 complain);
12465 release_tree_vector (call_args);
12467 return ret;
12470 case COND_EXPR:
12471 return build_x_conditional_expr
12472 (RECUR (TREE_OPERAND (t, 0)),
12473 RECUR (TREE_OPERAND (t, 1)),
12474 RECUR (TREE_OPERAND (t, 2)),
12475 complain);
12477 case PSEUDO_DTOR_EXPR:
12478 return finish_pseudo_destructor_expr
12479 (RECUR (TREE_OPERAND (t, 0)),
12480 RECUR (TREE_OPERAND (t, 1)),
12481 RECUR (TREE_OPERAND (t, 2)));
12483 case TREE_LIST:
12485 tree purpose, value, chain;
12487 if (t == void_list_node)
12488 return t;
12490 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12491 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12493 /* We have pack expansions, so expand those and
12494 create a new list out of it. */
12495 tree purposevec = NULL_TREE;
12496 tree valuevec = NULL_TREE;
12497 tree chain;
12498 int i, len = -1;
12500 /* Expand the argument expressions. */
12501 if (TREE_PURPOSE (t))
12502 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12503 complain, in_decl);
12504 if (TREE_VALUE (t))
12505 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12506 complain, in_decl);
12508 /* Build the rest of the list. */
12509 chain = TREE_CHAIN (t);
12510 if (chain && chain != void_type_node)
12511 chain = RECUR (chain);
12513 /* Determine the number of arguments. */
12514 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12516 len = TREE_VEC_LENGTH (purposevec);
12517 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12519 else if (TREE_CODE (valuevec) == TREE_VEC)
12520 len = TREE_VEC_LENGTH (valuevec);
12521 else
12523 /* Since we only performed a partial substitution into
12524 the argument pack, we only return a single list
12525 node. */
12526 if (purposevec == TREE_PURPOSE (t)
12527 && valuevec == TREE_VALUE (t)
12528 && chain == TREE_CHAIN (t))
12529 return t;
12531 return tree_cons (purposevec, valuevec, chain);
12534 /* Convert the argument vectors into a TREE_LIST */
12535 i = len;
12536 while (i > 0)
12538 /* Grab the Ith values. */
12539 i--;
12540 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12541 : NULL_TREE;
12542 value
12543 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12544 : NULL_TREE;
12546 /* Build the list (backwards). */
12547 chain = tree_cons (purpose, value, chain);
12550 return chain;
12553 purpose = TREE_PURPOSE (t);
12554 if (purpose)
12555 purpose = RECUR (purpose);
12556 value = TREE_VALUE (t);
12557 if (value)
12558 value = RECUR (value);
12559 chain = TREE_CHAIN (t);
12560 if (chain && chain != void_type_node)
12561 chain = RECUR (chain);
12562 if (purpose == TREE_PURPOSE (t)
12563 && value == TREE_VALUE (t)
12564 && chain == TREE_CHAIN (t))
12565 return t;
12566 return tree_cons (purpose, value, chain);
12569 case COMPONENT_REF:
12571 tree object;
12572 tree object_type;
12573 tree member;
12575 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12576 args, complain, in_decl);
12577 /* Remember that there was a reference to this entity. */
12578 if (DECL_P (object))
12579 mark_used (object);
12580 object_type = TREE_TYPE (object);
12582 member = TREE_OPERAND (t, 1);
12583 if (BASELINK_P (member))
12584 member = tsubst_baselink (member,
12585 non_reference (TREE_TYPE (object)),
12586 args, complain, in_decl);
12587 else
12588 member = tsubst_copy (member, args, complain, in_decl);
12589 if (member == error_mark_node)
12590 return error_mark_node;
12592 if (object_type && !CLASS_TYPE_P (object_type))
12594 if (SCALAR_TYPE_P (object_type))
12596 tree s = NULL_TREE;
12597 tree dtor = member;
12599 if (TREE_CODE (dtor) == SCOPE_REF)
12601 s = TREE_OPERAND (dtor, 0);
12602 dtor = TREE_OPERAND (dtor, 1);
12604 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12606 dtor = TREE_OPERAND (dtor, 0);
12607 if (TYPE_P (dtor))
12608 return finish_pseudo_destructor_expr (object, s, dtor);
12612 else if (TREE_CODE (member) == SCOPE_REF
12613 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12615 tree tmpl;
12616 tree args;
12618 /* Lookup the template functions now that we know what the
12619 scope is. */
12620 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12621 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12622 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12623 /*is_type_p=*/false,
12624 /*complain=*/false);
12625 if (BASELINK_P (member))
12627 BASELINK_FUNCTIONS (member)
12628 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12629 args);
12630 member = (adjust_result_of_qualified_name_lookup
12631 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12632 object_type));
12634 else
12636 qualified_name_lookup_error (object_type, tmpl, member,
12637 input_location);
12638 return error_mark_node;
12641 else if (TREE_CODE (member) == SCOPE_REF
12642 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12643 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12645 if (complain & tf_error)
12647 if (TYPE_P (TREE_OPERAND (member, 0)))
12648 error ("%qT is not a class or namespace",
12649 TREE_OPERAND (member, 0));
12650 else
12651 error ("%qD is not a class or namespace",
12652 TREE_OPERAND (member, 0));
12654 return error_mark_node;
12656 else if (TREE_CODE (member) == FIELD_DECL)
12657 return finish_non_static_data_member (member, object, NULL_TREE);
12659 return finish_class_member_access_expr (object, member,
12660 /*template_p=*/false,
12661 complain);
12664 case THROW_EXPR:
12665 return build_throw
12666 (RECUR (TREE_OPERAND (t, 0)));
12668 case CONSTRUCTOR:
12670 VEC(constructor_elt,gc) *n;
12671 constructor_elt *ce;
12672 unsigned HOST_WIDE_INT idx;
12673 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12674 bool process_index_p;
12675 int newlen;
12676 bool need_copy_p = false;
12677 tree r;
12679 if (type == error_mark_node)
12680 return error_mark_node;
12682 /* digest_init will do the wrong thing if we let it. */
12683 if (type && TYPE_PTRMEMFUNC_P (type))
12684 return t;
12686 /* We do not want to process the index of aggregate
12687 initializers as they are identifier nodes which will be
12688 looked up by digest_init. */
12689 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12691 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12692 newlen = VEC_length (constructor_elt, n);
12693 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12695 if (ce->index && process_index_p)
12696 ce->index = RECUR (ce->index);
12698 if (PACK_EXPANSION_P (ce->value))
12700 /* Substitute into the pack expansion. */
12701 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12702 in_decl);
12704 if (ce->value == error_mark_node)
12706 else if (TREE_VEC_LENGTH (ce->value) == 1)
12707 /* Just move the argument into place. */
12708 ce->value = TREE_VEC_ELT (ce->value, 0);
12709 else
12711 /* Update the length of the final CONSTRUCTOR
12712 arguments vector, and note that we will need to
12713 copy.*/
12714 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12715 need_copy_p = true;
12718 else
12719 ce->value = RECUR (ce->value);
12722 if (need_copy_p)
12724 VEC(constructor_elt,gc) *old_n = n;
12726 n = VEC_alloc (constructor_elt, gc, newlen);
12727 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12728 idx++)
12730 if (TREE_CODE (ce->value) == TREE_VEC)
12732 int i, len = TREE_VEC_LENGTH (ce->value);
12733 for (i = 0; i < len; ++i)
12734 CONSTRUCTOR_APPEND_ELT (n, 0,
12735 TREE_VEC_ELT (ce->value, i));
12737 else
12738 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12742 r = build_constructor (init_list_type_node, n);
12743 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12745 if (TREE_HAS_CONSTRUCTOR (t))
12746 return finish_compound_literal (type, r);
12748 return r;
12751 case TYPEID_EXPR:
12753 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12754 if (TYPE_P (operand_0))
12755 return get_typeid (operand_0);
12756 return build_typeid (operand_0);
12759 case VAR_DECL:
12760 if (!args)
12761 return t;
12762 /* Fall through */
12764 case PARM_DECL:
12766 tree r = tsubst_copy (t, args, complain, in_decl);
12768 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12769 /* If the original type was a reference, we'll be wrapped in
12770 the appropriate INDIRECT_REF. */
12771 r = convert_from_reference (r);
12772 return r;
12775 case VA_ARG_EXPR:
12776 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12777 tsubst_copy (TREE_TYPE (t), args, complain,
12778 in_decl));
12780 case OFFSETOF_EXPR:
12781 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12783 case TRAIT_EXPR:
12785 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12786 complain, in_decl);
12788 tree type2 = TRAIT_EXPR_TYPE2 (t);
12789 if (type2)
12790 type2 = tsubst_copy (type2, args, complain, in_decl);
12792 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12795 case STMT_EXPR:
12797 tree old_stmt_expr = cur_stmt_expr;
12798 tree stmt_expr = begin_stmt_expr ();
12800 cur_stmt_expr = stmt_expr;
12801 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12802 integral_constant_expression_p);
12803 stmt_expr = finish_stmt_expr (stmt_expr, false);
12804 cur_stmt_expr = old_stmt_expr;
12806 /* If the resulting list of expression statement is empty,
12807 fold it further into void_zero_node. */
12808 if (empty_expr_stmt_p (stmt_expr))
12809 stmt_expr = void_zero_node;
12811 return stmt_expr;
12814 case CONST_DECL:
12815 t = tsubst_copy (t, args, complain, in_decl);
12816 /* As in finish_id_expression, we resolve enumeration constants
12817 to their underlying values. */
12818 if (TREE_CODE (t) == CONST_DECL)
12820 used_types_insert (TREE_TYPE (t));
12821 return DECL_INITIAL (t);
12823 return t;
12825 case LAMBDA_EXPR:
12827 tree r = build_lambda_expr ();
12829 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12830 TREE_TYPE (r) = type;
12831 CLASSTYPE_LAMBDA_EXPR (type) = r;
12833 LAMBDA_EXPR_LOCATION (r)
12834 = LAMBDA_EXPR_LOCATION (t);
12835 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12836 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12837 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12838 LAMBDA_EXPR_DISCRIMINATOR (r)
12839 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12840 LAMBDA_EXPR_CAPTURE_LIST (r)
12841 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12842 LAMBDA_EXPR_THIS_CAPTURE (r)
12843 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12844 LAMBDA_EXPR_EXTRA_SCOPE (r)
12845 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12847 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12848 determine_visibility (TYPE_NAME (type));
12849 /* Now that we know visibility, instantiate the type so we have a
12850 declaration of the op() for later calls to lambda_function. */
12851 complete_type (type);
12853 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12854 if (type)
12855 apply_lambda_return_type (r, type);
12857 return build_lambda_object (r);
12860 default:
12861 /* Handle Objective-C++ constructs, if appropriate. */
12863 tree subst
12864 = objcp_tsubst_copy_and_build (t, args, complain,
12865 in_decl, /*function_p=*/false);
12866 if (subst)
12867 return subst;
12869 return tsubst_copy (t, args, complain, in_decl);
12872 #undef RECUR
12875 /* Verify that the instantiated ARGS are valid. For type arguments,
12876 make sure that the type's linkage is ok. For non-type arguments,
12877 make sure they are constants if they are integral or enumerations.
12878 Emit an error under control of COMPLAIN, and return TRUE on error. */
12880 static bool
12881 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12883 if (ARGUMENT_PACK_P (t))
12885 tree vec = ARGUMENT_PACK_ARGS (t);
12886 int len = TREE_VEC_LENGTH (vec);
12887 bool result = false;
12888 int i;
12890 for (i = 0; i < len; ++i)
12891 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12892 result = true;
12893 return result;
12895 else if (TYPE_P (t))
12897 /* [basic.link]: A name with no linkage (notably, the name
12898 of a class or enumeration declared in a local scope)
12899 shall not be used to declare an entity with linkage.
12900 This implies that names with no linkage cannot be used as
12901 template arguments
12903 DR 757 relaxes this restriction for C++0x. */
12904 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
12905 : no_linkage_check (t, /*relaxed_p=*/false));
12907 if (nt)
12909 /* DR 488 makes use of a type with no linkage cause
12910 type deduction to fail. */
12911 if (complain & tf_error)
12913 if (TYPE_ANONYMOUS_P (nt))
12914 error ("%qT is/uses anonymous type", t);
12915 else
12916 error ("template argument for %qD uses local type %qT",
12917 tmpl, t);
12919 return true;
12921 /* In order to avoid all sorts of complications, we do not
12922 allow variably-modified types as template arguments. */
12923 else if (variably_modified_type_p (t, NULL_TREE))
12925 if (complain & tf_error)
12926 error ("%qT is a variably modified type", t);
12927 return true;
12930 /* A non-type argument of integral or enumerated type must be a
12931 constant. */
12932 else if (TREE_TYPE (t)
12933 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12934 && !TREE_CONSTANT (t))
12936 if (complain & tf_error)
12937 error ("integral expression %qE is not constant", t);
12938 return true;
12940 return false;
12943 static bool
12944 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12946 int ix, len = DECL_NTPARMS (tmpl);
12947 bool result = false;
12949 for (ix = 0; ix != len; ix++)
12951 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12952 result = true;
12954 if (result && (complain & tf_error))
12955 error (" trying to instantiate %qD", tmpl);
12956 return result;
12959 /* Instantiate the indicated variable or function template TMPL with
12960 the template arguments in TARG_PTR. */
12962 tree
12963 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12965 tree targ_ptr = orig_args;
12966 tree fndecl;
12967 tree gen_tmpl;
12968 tree spec;
12969 HOST_WIDE_INT saved_processing_template_decl;
12971 if (tmpl == error_mark_node)
12972 return error_mark_node;
12974 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12976 /* If this function is a clone, handle it specially. */
12977 if (DECL_CLONED_FUNCTION_P (tmpl))
12979 tree spec;
12980 tree clone;
12982 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12983 DECL_CLONED_FUNCTION. */
12984 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12985 targ_ptr, complain);
12986 if (spec == error_mark_node)
12987 return error_mark_node;
12989 /* Look for the clone. */
12990 FOR_EACH_CLONE (clone, spec)
12991 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12992 return clone;
12993 /* We should always have found the clone by now. */
12994 gcc_unreachable ();
12995 return NULL_TREE;
12998 /* Check to see if we already have this specialization. */
12999 gen_tmpl = most_general_template (tmpl);
13000 if (tmpl != gen_tmpl)
13001 /* The TMPL is a partial instantiation. To get a full set of
13002 arguments we must add the arguments used to perform the
13003 partial instantiation. */
13004 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
13005 targ_ptr);
13007 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13008 but it doesn't seem to be on the hot path. */
13009 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
13011 gcc_assert (tmpl == gen_tmpl
13012 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
13013 == spec)
13014 || fndecl == NULL_TREE);
13016 if (spec != NULL_TREE)
13017 return spec;
13019 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13020 complain))
13021 return error_mark_node;
13023 /* We are building a FUNCTION_DECL, during which the access of its
13024 parameters and return types have to be checked. However this
13025 FUNCTION_DECL which is the desired context for access checking
13026 is not built yet. We solve this chicken-and-egg problem by
13027 deferring all checks until we have the FUNCTION_DECL. */
13028 push_deferring_access_checks (dk_deferred);
13030 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13031 (because, for example, we have encountered a non-dependent
13032 function call in the body of a template function and must now
13033 determine which of several overloaded functions will be called),
13034 within the instantiation itself we are not processing a
13035 template. */
13036 saved_processing_template_decl = processing_template_decl;
13037 processing_template_decl = 0;
13038 /* Substitute template parameters to obtain the specialization. */
13039 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13040 targ_ptr, complain, gen_tmpl);
13041 processing_template_decl = saved_processing_template_decl;
13042 if (fndecl == error_mark_node)
13043 return error_mark_node;
13045 /* Now we know the specialization, compute access previously
13046 deferred. */
13047 push_access_scope (fndecl);
13049 /* Some typedefs referenced from within the template code need to be access
13050 checked at template instantiation time, i.e now. These types were
13051 added to the template at parsing time. Let's get those and perfom
13052 the acces checks then. */
13053 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13054 perform_deferred_access_checks ();
13055 pop_access_scope (fndecl);
13056 pop_deferring_access_checks ();
13058 /* The DECL_TI_TEMPLATE should always be the immediate parent
13059 template, not the most general template. */
13060 DECL_TI_TEMPLATE (fndecl) = tmpl;
13062 /* If we've just instantiated the main entry point for a function,
13063 instantiate all the alternate entry points as well. We do this
13064 by cloning the instantiation of the main entry point, not by
13065 instantiating the template clones. */
13066 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
13067 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13069 return fndecl;
13072 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13073 NARGS elements of the arguments that are being used when calling
13074 it. TARGS is a vector into which the deduced template arguments
13075 are placed.
13077 Return zero for success, 2 for an incomplete match that doesn't resolve
13078 all the types, and 1 for complete failure. An error message will be
13079 printed only for an incomplete match.
13081 If FN is a conversion operator, or we are trying to produce a specific
13082 specialization, RETURN_TYPE is the return type desired.
13084 The EXPLICIT_TARGS are explicit template arguments provided via a
13085 template-id.
13087 The parameter STRICT is one of:
13089 DEDUCE_CALL:
13090 We are deducing arguments for a function call, as in
13091 [temp.deduct.call].
13093 DEDUCE_CONV:
13094 We are deducing arguments for a conversion function, as in
13095 [temp.deduct.conv].
13097 DEDUCE_EXACT:
13098 We are deducing arguments when doing an explicit instantiation
13099 as in [temp.explicit], when determining an explicit specialization
13100 as in [temp.expl.spec], or when taking the address of a function
13101 template, as in [temp.deduct.funcaddr]. */
13104 fn_type_unification (tree fn,
13105 tree explicit_targs,
13106 tree targs,
13107 const tree *args,
13108 unsigned int nargs,
13109 tree return_type,
13110 unification_kind_t strict,
13111 int flags)
13113 tree parms;
13114 tree fntype;
13115 int result;
13116 bool incomplete_argument_packs_p = false;
13118 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13120 fntype = TREE_TYPE (fn);
13121 if (explicit_targs)
13123 /* [temp.deduct]
13125 The specified template arguments must match the template
13126 parameters in kind (i.e., type, nontype, template), and there
13127 must not be more arguments than there are parameters;
13128 otherwise type deduction fails.
13130 Nontype arguments must match the types of the corresponding
13131 nontype template parameters, or must be convertible to the
13132 types of the corresponding nontype parameters as specified in
13133 _temp.arg.nontype_, otherwise type deduction fails.
13135 All references in the function type of the function template
13136 to the corresponding template parameters are replaced by the
13137 specified template argument values. If a substitution in a
13138 template parameter or in the function type of the function
13139 template results in an invalid type, type deduction fails. */
13140 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13141 int i, len = TREE_VEC_LENGTH (tparms);
13142 tree converted_args;
13143 bool incomplete = false;
13145 if (explicit_targs == error_mark_node)
13146 return 1;
13148 converted_args
13149 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13150 /*require_all_args=*/false,
13151 /*use_default_args=*/false));
13152 if (converted_args == error_mark_node)
13153 return 1;
13155 /* Substitute the explicit args into the function type. This is
13156 necessary so that, for instance, explicitly declared function
13157 arguments can match null pointed constants. If we were given
13158 an incomplete set of explicit args, we must not do semantic
13159 processing during substitution as we could create partial
13160 instantiations. */
13161 for (i = 0; i < len; i++)
13163 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13164 bool parameter_pack = false;
13166 /* Dig out the actual parm. */
13167 if (TREE_CODE (parm) == TYPE_DECL
13168 || TREE_CODE (parm) == TEMPLATE_DECL)
13170 parm = TREE_TYPE (parm);
13171 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13173 else if (TREE_CODE (parm) == PARM_DECL)
13175 parm = DECL_INITIAL (parm);
13176 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13179 if (parameter_pack)
13181 int level, idx;
13182 tree targ;
13183 template_parm_level_and_index (parm, &level, &idx);
13185 /* Mark the argument pack as "incomplete". We could
13186 still deduce more arguments during unification. */
13187 targ = TMPL_ARG (converted_args, level, idx);
13188 if (targ)
13190 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13191 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13192 = ARGUMENT_PACK_ARGS (targ);
13195 /* We have some incomplete argument packs. */
13196 incomplete_argument_packs_p = true;
13200 if (incomplete_argument_packs_p)
13201 /* Any substitution is guaranteed to be incomplete if there
13202 are incomplete argument packs, because we can still deduce
13203 more arguments. */
13204 incomplete = 1;
13205 else
13206 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13208 processing_template_decl += incomplete;
13209 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13210 processing_template_decl -= incomplete;
13212 if (fntype == error_mark_node)
13213 return 1;
13215 /* Place the explicitly specified arguments in TARGS. */
13216 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13217 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13220 /* Never do unification on the 'this' parameter. */
13221 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13223 if (return_type)
13225 tree *new_args;
13227 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13228 new_args = XALLOCAVEC (tree, nargs + 1);
13229 new_args[0] = return_type;
13230 memcpy (new_args + 1, args, nargs * sizeof (tree));
13231 args = new_args;
13232 ++nargs;
13235 /* We allow incomplete unification without an error message here
13236 because the standard doesn't seem to explicitly prohibit it. Our
13237 callers must be ready to deal with unification failures in any
13238 event. */
13239 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13240 targs, parms, args, nargs, /*subr=*/0,
13241 strict, flags);
13243 if (result == 0 && incomplete_argument_packs_p)
13245 int i, len = NUM_TMPL_ARGS (targs);
13247 /* Clear the "incomplete" flags on all argument packs. */
13248 for (i = 0; i < len; i++)
13250 tree arg = TREE_VEC_ELT (targs, i);
13251 if (ARGUMENT_PACK_P (arg))
13253 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13254 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13259 /* Now that we have bindings for all of the template arguments,
13260 ensure that the arguments deduced for the template template
13261 parameters have compatible template parameter lists. We cannot
13262 check this property before we have deduced all template
13263 arguments, because the template parameter types of a template
13264 template parameter might depend on prior template parameters
13265 deduced after the template template parameter. The following
13266 ill-formed example illustrates this issue:
13268 template<typename T, template<T> class C> void f(C<5>, T);
13270 template<int N> struct X {};
13272 void g() {
13273 f(X<5>(), 5l); // error: template argument deduction fails
13276 The template parameter list of 'C' depends on the template type
13277 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13278 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13279 time that we deduce 'C'. */
13280 if (result == 0
13281 && !template_template_parm_bindings_ok_p
13282 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13283 return 1;
13285 if (result == 0)
13286 /* All is well so far. Now, check:
13288 [temp.deduct]
13290 When all template arguments have been deduced, all uses of
13291 template parameters in nondeduced contexts are replaced with
13292 the corresponding deduced argument values. If the
13293 substitution results in an invalid type, as described above,
13294 type deduction fails. */
13296 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13297 if (substed == error_mark_node)
13298 return 1;
13300 /* If we're looking for an exact match, check that what we got
13301 is indeed an exact match. It might not be if some template
13302 parameters are used in non-deduced contexts. */
13303 if (strict == DEDUCE_EXACT)
13305 unsigned int i;
13307 tree sarg
13308 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13309 if (return_type)
13310 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13311 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13312 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13313 return 1;
13317 return result;
13320 /* Adjust types before performing type deduction, as described in
13321 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13322 sections are symmetric. PARM is the type of a function parameter
13323 or the return type of the conversion function. ARG is the type of
13324 the argument passed to the call, or the type of the value
13325 initialized with the result of the conversion function.
13326 ARG_EXPR is the original argument expression, which may be null. */
13328 static int
13329 maybe_adjust_types_for_deduction (unification_kind_t strict,
13330 tree* parm,
13331 tree* arg,
13332 tree arg_expr)
13334 int result = 0;
13336 switch (strict)
13338 case DEDUCE_CALL:
13339 break;
13341 case DEDUCE_CONV:
13343 /* Swap PARM and ARG throughout the remainder of this
13344 function; the handling is precisely symmetric since PARM
13345 will initialize ARG rather than vice versa. */
13346 tree* temp = parm;
13347 parm = arg;
13348 arg = temp;
13349 break;
13352 case DEDUCE_EXACT:
13353 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13354 too, but here handle it by stripping the reference from PARM
13355 rather than by adding it to ARG. */
13356 if (TREE_CODE (*parm) == REFERENCE_TYPE
13357 && TYPE_REF_IS_RVALUE (*parm)
13358 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13359 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13360 && TREE_CODE (*arg) == REFERENCE_TYPE
13361 && !TYPE_REF_IS_RVALUE (*arg))
13362 *parm = TREE_TYPE (*parm);
13363 /* Nothing else to do in this case. */
13364 return 0;
13366 default:
13367 gcc_unreachable ();
13370 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13372 /* [temp.deduct.call]
13374 If P is not a reference type:
13376 --If A is an array type, the pointer type produced by the
13377 array-to-pointer standard conversion (_conv.array_) is
13378 used in place of A for type deduction; otherwise,
13380 --If A is a function type, the pointer type produced by
13381 the function-to-pointer standard conversion
13382 (_conv.func_) is used in place of A for type deduction;
13383 otherwise,
13385 --If A is a cv-qualified type, the top level
13386 cv-qualifiers of A's type are ignored for type
13387 deduction. */
13388 if (TREE_CODE (*arg) == ARRAY_TYPE)
13389 *arg = build_pointer_type (TREE_TYPE (*arg));
13390 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13391 *arg = build_pointer_type (*arg);
13392 else
13393 *arg = TYPE_MAIN_VARIANT (*arg);
13396 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13397 of the form T&&, where T is a template parameter, and the argument
13398 is an lvalue, T is deduced as A& */
13399 if (TREE_CODE (*parm) == REFERENCE_TYPE
13400 && TYPE_REF_IS_RVALUE (*parm)
13401 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13402 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13403 && arg_expr && real_lvalue_p (arg_expr))
13404 *arg = build_reference_type (*arg);
13406 /* [temp.deduct.call]
13408 If P is a cv-qualified type, the top level cv-qualifiers
13409 of P's type are ignored for type deduction. If P is a
13410 reference type, the type referred to by P is used for
13411 type deduction. */
13412 *parm = TYPE_MAIN_VARIANT (*parm);
13413 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13415 *parm = TREE_TYPE (*parm);
13416 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13419 /* DR 322. For conversion deduction, remove a reference type on parm
13420 too (which has been swapped into ARG). */
13421 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13422 *arg = TREE_TYPE (*arg);
13424 return result;
13427 /* Most parms like fn_type_unification.
13429 If SUBR is 1, we're being called recursively (to unify the
13430 arguments of a function or method parameter of a function
13431 template). */
13433 static int
13434 type_unification_real (tree tparms,
13435 tree targs,
13436 tree xparms,
13437 const tree *xargs,
13438 unsigned int xnargs,
13439 int subr,
13440 unification_kind_t strict,
13441 int flags)
13443 tree parm, arg, arg_expr;
13444 int i;
13445 int ntparms = TREE_VEC_LENGTH (tparms);
13446 int sub_strict;
13447 int saw_undeduced = 0;
13448 tree parms;
13449 const tree *args;
13450 unsigned int nargs;
13451 unsigned int ia;
13453 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13454 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13455 gcc_assert (ntparms > 0);
13457 /* Reset the number of non-defaulted template arguments contained
13458 in in TARGS. */
13459 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
13461 switch (strict)
13463 case DEDUCE_CALL:
13464 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13465 | UNIFY_ALLOW_DERIVED);
13466 break;
13468 case DEDUCE_CONV:
13469 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13470 break;
13472 case DEDUCE_EXACT:
13473 sub_strict = UNIFY_ALLOW_NONE;
13474 break;
13476 default:
13477 gcc_unreachable ();
13480 again:
13481 parms = xparms;
13482 args = xargs;
13483 nargs = xnargs;
13485 ia = 0;
13486 while (parms && parms != void_list_node
13487 && ia < nargs)
13489 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13490 break;
13492 parm = TREE_VALUE (parms);
13493 parms = TREE_CHAIN (parms);
13494 arg = args[ia];
13495 ++ia;
13496 arg_expr = NULL;
13498 if (arg == error_mark_node)
13499 return 1;
13500 if (arg == unknown_type_node)
13501 /* We can't deduce anything from this, but we might get all the
13502 template args from other function args. */
13503 continue;
13505 /* Conversions will be performed on a function argument that
13506 corresponds with a function parameter that contains only
13507 non-deducible template parameters and explicitly specified
13508 template parameters. */
13509 if (!uses_template_parms (parm))
13511 tree type;
13513 if (!TYPE_P (arg))
13514 type = TREE_TYPE (arg);
13515 else
13516 type = arg;
13518 if (same_type_p (parm, type))
13519 continue;
13520 if (strict != DEDUCE_EXACT
13521 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13522 flags))
13523 continue;
13525 return 1;
13528 if (!TYPE_P (arg))
13530 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13531 if (type_unknown_p (arg))
13533 /* [temp.deduct.type]
13535 A template-argument can be deduced from a pointer to
13536 function or pointer to member function argument if
13537 the set of overloaded functions does not contain
13538 function templates and at most one of a set of
13539 overloaded functions provides a unique match. */
13540 if (resolve_overloaded_unification
13541 (tparms, targs, parm, arg, strict, sub_strict))
13542 continue;
13544 return 1;
13546 arg_expr = arg;
13547 arg = unlowered_expr_type (arg);
13548 if (arg == error_mark_node)
13549 return 1;
13553 int arg_strict = sub_strict;
13555 if (!subr)
13556 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13557 arg_expr);
13559 if (arg == init_list_type_node && arg_expr)
13560 arg = arg_expr;
13561 if (unify (tparms, targs, parm, arg, arg_strict))
13562 return 1;
13567 if (parms
13568 && parms != void_list_node
13569 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13571 /* Unify the remaining arguments with the pack expansion type. */
13572 tree argvec;
13573 tree parmvec = make_tree_vec (1);
13575 /* Allocate a TREE_VEC and copy in all of the arguments */
13576 argvec = make_tree_vec (nargs - ia);
13577 for (i = 0; ia < nargs; ++ia, ++i)
13578 TREE_VEC_ELT (argvec, i) = args[ia];
13580 /* Copy the parameter into parmvec. */
13581 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13582 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13583 /*call_args_p=*/true, /*subr=*/subr))
13584 return 1;
13586 /* Advance to the end of the list of parameters. */
13587 parms = TREE_CHAIN (parms);
13590 /* Fail if we've reached the end of the parm list, and more args
13591 are present, and the parm list isn't variadic. */
13592 if (ia < nargs && parms == void_list_node)
13593 return 1;
13594 /* Fail if parms are left and they don't have default values. */
13595 if (parms && parms != void_list_node
13596 && TREE_PURPOSE (parms) == NULL_TREE)
13597 return 1;
13599 if (!subr)
13600 for (i = 0; i < ntparms; i++)
13601 if (!TREE_VEC_ELT (targs, i))
13603 tree tparm;
13605 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13606 continue;
13608 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13610 /* If this is an undeduced nontype parameter that depends on
13611 a type parameter, try another pass; its type may have been
13612 deduced from a later argument than the one from which
13613 this parameter can be deduced. */
13614 if (TREE_CODE (tparm) == PARM_DECL
13615 && uses_template_parms (TREE_TYPE (tparm))
13616 && !saw_undeduced++)
13617 goto again;
13619 /* Core issue #226 (C++0x) [temp.deduct]:
13621 If a template argument has not been deduced, its
13622 default template argument, if any, is used.
13624 When we are in C++98 mode, TREE_PURPOSE will either
13625 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13626 to explicitly check cxx_dialect here. */
13627 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13629 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13630 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13631 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13632 arg = convert_template_argument (parm, arg, targs, tf_none,
13633 i, NULL_TREE);
13634 if (arg == error_mark_node)
13635 return 1;
13636 else
13638 TREE_VEC_ELT (targs, i) = arg;
13639 /* The position of the first default template argument,
13640 is also the number of non-defaulted arguments in TARGS.
13641 Record that. */
13642 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13643 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
13644 continue;
13648 /* If the type parameter is a parameter pack, then it will
13649 be deduced to an empty parameter pack. */
13650 if (template_parameter_pack_p (tparm))
13652 tree arg;
13654 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13656 arg = make_node (NONTYPE_ARGUMENT_PACK);
13657 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13658 TREE_CONSTANT (arg) = 1;
13660 else
13661 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13663 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13665 TREE_VEC_ELT (targs, i) = arg;
13666 continue;
13669 return 2;
13671 #ifdef ENABLE_CHECKING
13672 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13673 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
13674 #endif
13676 return 0;
13679 /* Subroutine of type_unification_real. Args are like the variables
13680 at the call site. ARG is an overloaded function (or template-id);
13681 we try deducing template args from each of the overloads, and if
13682 only one succeeds, we go with that. Modifies TARGS and returns
13683 true on success. */
13685 static bool
13686 resolve_overloaded_unification (tree tparms,
13687 tree targs,
13688 tree parm,
13689 tree arg,
13690 unification_kind_t strict,
13691 int sub_strict)
13693 tree tempargs = copy_node (targs);
13694 int good = 0;
13695 tree goodfn = NULL_TREE;
13696 bool addr_p;
13698 if (TREE_CODE (arg) == ADDR_EXPR)
13700 arg = TREE_OPERAND (arg, 0);
13701 addr_p = true;
13703 else
13704 addr_p = false;
13706 if (TREE_CODE (arg) == COMPONENT_REF)
13707 /* Handle `&x' where `x' is some static or non-static member
13708 function name. */
13709 arg = TREE_OPERAND (arg, 1);
13711 if (TREE_CODE (arg) == OFFSET_REF)
13712 arg = TREE_OPERAND (arg, 1);
13714 /* Strip baselink information. */
13715 if (BASELINK_P (arg))
13716 arg = BASELINK_FUNCTIONS (arg);
13718 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13720 /* If we got some explicit template args, we need to plug them into
13721 the affected templates before we try to unify, in case the
13722 explicit args will completely resolve the templates in question. */
13724 tree expl_subargs = TREE_OPERAND (arg, 1);
13725 arg = TREE_OPERAND (arg, 0);
13727 for (; arg; arg = OVL_NEXT (arg))
13729 tree fn = OVL_CURRENT (arg);
13730 tree subargs, elem;
13732 if (TREE_CODE (fn) != TEMPLATE_DECL)
13733 continue;
13735 ++processing_template_decl;
13736 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13737 expl_subargs, /*check_ret=*/false);
13738 if (subargs)
13740 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13741 if (try_one_overload (tparms, targs, tempargs, parm,
13742 elem, strict, sub_strict, addr_p)
13743 && (!goodfn || !decls_match (goodfn, elem)))
13745 goodfn = elem;
13746 ++good;
13749 --processing_template_decl;
13752 else if (TREE_CODE (arg) != OVERLOAD
13753 && TREE_CODE (arg) != FUNCTION_DECL)
13754 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13755 -- but the deduction does not succeed because the expression is
13756 not just the function on its own. */
13757 return false;
13758 else
13759 for (; arg; arg = OVL_NEXT (arg))
13760 if (try_one_overload (tparms, targs, tempargs, parm,
13761 TREE_TYPE (OVL_CURRENT (arg)),
13762 strict, sub_strict, addr_p)
13763 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13765 goodfn = OVL_CURRENT (arg);
13766 ++good;
13769 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13770 to function or pointer to member function argument if the set of
13771 overloaded functions does not contain function templates and at most
13772 one of a set of overloaded functions provides a unique match.
13774 So if we found multiple possibilities, we return success but don't
13775 deduce anything. */
13777 if (good == 1)
13779 int i = TREE_VEC_LENGTH (targs);
13780 for (; i--; )
13781 if (TREE_VEC_ELT (tempargs, i))
13782 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13784 if (good)
13785 return true;
13787 return false;
13790 /* Core DR 115: In contexts where deduction is done and fails, or in
13791 contexts where deduction is not done, if a template argument list is
13792 specified and it, along with any default template arguments, identifies
13793 a single function template specialization, then the template-id is an
13794 lvalue for the function template specialization. */
13796 tree
13797 resolve_nondeduced_context (tree orig_expr)
13799 tree expr, offset, baselink;
13800 bool addr;
13802 if (!type_unknown_p (orig_expr))
13803 return orig_expr;
13805 expr = orig_expr;
13806 addr = false;
13807 offset = NULL_TREE;
13808 baselink = NULL_TREE;
13810 if (TREE_CODE (expr) == ADDR_EXPR)
13812 expr = TREE_OPERAND (expr, 0);
13813 addr = true;
13815 if (TREE_CODE (expr) == OFFSET_REF)
13817 offset = expr;
13818 expr = TREE_OPERAND (expr, 1);
13820 if (TREE_CODE (expr) == BASELINK)
13822 baselink = expr;
13823 expr = BASELINK_FUNCTIONS (expr);
13826 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13828 int good = 0;
13829 tree goodfn = NULL_TREE;
13831 /* If we got some explicit template args, we need to plug them into
13832 the affected templates before we try to unify, in case the
13833 explicit args will completely resolve the templates in question. */
13835 tree expl_subargs = TREE_OPERAND (expr, 1);
13836 tree arg = TREE_OPERAND (expr, 0);
13837 tree badfn = NULL_TREE;
13838 tree badargs = NULL_TREE;
13840 for (; arg; arg = OVL_NEXT (arg))
13842 tree fn = OVL_CURRENT (arg);
13843 tree subargs, elem;
13845 if (TREE_CODE (fn) != TEMPLATE_DECL)
13846 continue;
13848 ++processing_template_decl;
13849 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13850 expl_subargs, /*check_ret=*/false);
13851 if (subargs && !any_dependent_template_arguments_p (subargs))
13853 elem = instantiate_template (fn, subargs, tf_none);
13854 if (elem == error_mark_node)
13856 badfn = fn;
13857 badargs = subargs;
13859 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
13861 goodfn = elem;
13862 ++good;
13865 --processing_template_decl;
13867 if (good == 1)
13869 expr = goodfn;
13870 if (baselink)
13871 expr = build_baselink (BASELINK_BINFO (baselink),
13872 BASELINK_ACCESS_BINFO (baselink),
13873 expr, BASELINK_OPTYPE (baselink));
13874 if (offset)
13875 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
13876 TREE_OPERAND (offset, 0), expr);
13877 if (addr)
13878 expr = build_address (expr);
13879 return expr;
13881 else if (good == 0 && badargs)
13882 /* There were no good options and at least one bad one, so let the
13883 user know what the problem is. */
13884 instantiate_template (badfn, badargs, tf_warning_or_error);
13886 return orig_expr;
13889 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13890 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13891 different overloads deduce different arguments for a given parm.
13892 ADDR_P is true if the expression for which deduction is being
13893 performed was of the form "& fn" rather than simply "fn".
13895 Returns 1 on success. */
13897 static int
13898 try_one_overload (tree tparms,
13899 tree orig_targs,
13900 tree targs,
13901 tree parm,
13902 tree arg,
13903 unification_kind_t strict,
13904 int sub_strict,
13905 bool addr_p)
13907 int nargs;
13908 tree tempargs;
13909 int i;
13911 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13912 to function or pointer to member function argument if the set of
13913 overloaded functions does not contain function templates and at most
13914 one of a set of overloaded functions provides a unique match.
13916 So if this is a template, just return success. */
13918 if (uses_template_parms (arg))
13919 return 1;
13921 if (TREE_CODE (arg) == METHOD_TYPE)
13922 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13923 else if (addr_p)
13924 arg = build_pointer_type (arg);
13926 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13928 /* We don't copy orig_targs for this because if we have already deduced
13929 some template args from previous args, unify would complain when we
13930 try to deduce a template parameter for the same argument, even though
13931 there isn't really a conflict. */
13932 nargs = TREE_VEC_LENGTH (targs);
13933 tempargs = make_tree_vec (nargs);
13935 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13936 return 0;
13938 /* First make sure we didn't deduce anything that conflicts with
13939 explicitly specified args. */
13940 for (i = nargs; i--; )
13942 tree elt = TREE_VEC_ELT (tempargs, i);
13943 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13945 if (!elt)
13946 /*NOP*/;
13947 else if (uses_template_parms (elt))
13948 /* Since we're unifying against ourselves, we will fill in
13949 template args used in the function parm list with our own
13950 template parms. Discard them. */
13951 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13952 else if (oldelt && !template_args_equal (oldelt, elt))
13953 return 0;
13956 for (i = nargs; i--; )
13958 tree elt = TREE_VEC_ELT (tempargs, i);
13960 if (elt)
13961 TREE_VEC_ELT (targs, i) = elt;
13964 return 1;
13967 /* PARM is a template class (perhaps with unbound template
13968 parameters). ARG is a fully instantiated type. If ARG can be
13969 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13970 TARGS are as for unify. */
13972 static tree
13973 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13975 tree copy_of_targs;
13977 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13978 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13979 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13980 return NULL_TREE;
13982 /* We need to make a new template argument vector for the call to
13983 unify. If we used TARGS, we'd clutter it up with the result of
13984 the attempted unification, even if this class didn't work out.
13985 We also don't want to commit ourselves to all the unifications
13986 we've already done, since unification is supposed to be done on
13987 an argument-by-argument basis. In other words, consider the
13988 following pathological case:
13990 template <int I, int J, int K>
13991 struct S {};
13993 template <int I, int J>
13994 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13996 template <int I, int J, int K>
13997 void f(S<I, J, K>, S<I, I, I>);
13999 void g() {
14000 S<0, 0, 0> s0;
14001 S<0, 1, 2> s2;
14003 f(s0, s2);
14006 Now, by the time we consider the unification involving `s2', we
14007 already know that we must have `f<0, 0, 0>'. But, even though
14008 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14009 because there are two ways to unify base classes of S<0, 1, 2>
14010 with S<I, I, I>. If we kept the already deduced knowledge, we
14011 would reject the possibility I=1. */
14012 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
14014 /* If unification failed, we're done. */
14015 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14016 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14017 return NULL_TREE;
14019 return arg;
14022 /* Given a template type PARM and a class type ARG, find the unique
14023 base type in ARG that is an instance of PARM. We do not examine
14024 ARG itself; only its base-classes. If there is not exactly one
14025 appropriate base class, return NULL_TREE. PARM may be the type of
14026 a partial specialization, as well as a plain template type. Used
14027 by unify. */
14029 static tree
14030 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14032 tree rval = NULL_TREE;
14033 tree binfo;
14035 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14037 binfo = TYPE_BINFO (complete_type (arg));
14038 if (!binfo)
14039 /* The type could not be completed. */
14040 return NULL_TREE;
14042 /* Walk in inheritance graph order. The search order is not
14043 important, and this avoids multiple walks of virtual bases. */
14044 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14046 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14048 if (r)
14050 /* If there is more than one satisfactory baseclass, then:
14052 [temp.deduct.call]
14054 If they yield more than one possible deduced A, the type
14055 deduction fails.
14057 applies. */
14058 if (rval && !same_type_p (r, rval))
14059 return NULL_TREE;
14061 rval = r;
14065 return rval;
14068 /* Returns the level of DECL, which declares a template parameter. */
14070 static int
14071 template_decl_level (tree decl)
14073 switch (TREE_CODE (decl))
14075 case TYPE_DECL:
14076 case TEMPLATE_DECL:
14077 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14079 case PARM_DECL:
14080 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14082 default:
14083 gcc_unreachable ();
14085 return 0;
14088 /* Decide whether ARG can be unified with PARM, considering only the
14089 cv-qualifiers of each type, given STRICT as documented for unify.
14090 Returns nonzero iff the unification is OK on that basis. */
14092 static int
14093 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14095 int arg_quals = cp_type_quals (arg);
14096 int parm_quals = cp_type_quals (parm);
14098 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14099 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14101 /* Although a CVR qualifier is ignored when being applied to a
14102 substituted template parameter ([8.3.2]/1 for example), that
14103 does not apply during deduction [14.8.2.4]/1, (even though
14104 that is not explicitly mentioned, [14.8.2.4]/9 indicates
14105 this). Except when we're allowing additional CV qualifiers
14106 at the outer level [14.8.2.1]/3,1st bullet. */
14107 if ((TREE_CODE (arg) == REFERENCE_TYPE
14108 || TREE_CODE (arg) == FUNCTION_TYPE
14109 || TREE_CODE (arg) == METHOD_TYPE)
14110 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14111 return 0;
14113 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14114 && (parm_quals & TYPE_QUAL_RESTRICT))
14115 return 0;
14118 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14119 && (arg_quals & parm_quals) != parm_quals)
14120 return 0;
14122 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14123 && (parm_quals & arg_quals) != arg_quals)
14124 return 0;
14126 return 1;
14129 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14130 void
14131 template_parm_level_and_index (tree parm, int* level, int* index)
14133 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14134 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14135 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14137 *index = TEMPLATE_TYPE_IDX (parm);
14138 *level = TEMPLATE_TYPE_LEVEL (parm);
14140 else
14142 *index = TEMPLATE_PARM_IDX (parm);
14143 *level = TEMPLATE_PARM_LEVEL (parm);
14147 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14148 expansion at the end of PACKED_PARMS. Returns 0 if the type
14149 deduction succeeds, 1 otherwise. STRICT is the same as in
14150 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14151 call argument list. We'll need to adjust the arguments to make them
14152 types. SUBR tells us if this is from a recursive call to
14153 type_unification_real. */
14155 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14156 tree packed_args, int strict, bool call_args_p,
14157 bool subr)
14159 tree parm
14160 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14161 tree pattern = PACK_EXPANSION_PATTERN (parm);
14162 tree pack, packs = NULL_TREE;
14163 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14164 int len = TREE_VEC_LENGTH (packed_args);
14166 /* Determine the parameter packs we will be deducing from the
14167 pattern, and record their current deductions. */
14168 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14169 pack; pack = TREE_CHAIN (pack))
14171 tree parm_pack = TREE_VALUE (pack);
14172 int idx, level;
14174 /* Determine the index and level of this parameter pack. */
14175 template_parm_level_and_index (parm_pack, &level, &idx);
14177 /* Keep track of the parameter packs and their corresponding
14178 argument packs. */
14179 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14180 TREE_TYPE (packs) = make_tree_vec (len - start);
14183 /* Loop through all of the arguments that have not yet been
14184 unified and unify each with the pattern. */
14185 for (i = start; i < len; i++)
14187 tree parm = pattern;
14189 /* For each parameter pack, clear out the deduced value so that
14190 we can deduce it again. */
14191 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14193 int idx, level;
14194 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14196 TMPL_ARG (targs, level, idx) = NULL_TREE;
14199 /* Unify the pattern with the current argument. */
14201 tree arg = TREE_VEC_ELT (packed_args, i);
14202 tree arg_expr = NULL_TREE;
14203 int arg_strict = strict;
14204 bool skip_arg_p = false;
14206 if (call_args_p)
14208 int sub_strict;
14210 /* This mirrors what we do in type_unification_real. */
14211 switch (strict)
14213 case DEDUCE_CALL:
14214 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
14215 | UNIFY_ALLOW_MORE_CV_QUAL
14216 | UNIFY_ALLOW_DERIVED);
14217 break;
14219 case DEDUCE_CONV:
14220 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14221 break;
14223 case DEDUCE_EXACT:
14224 sub_strict = UNIFY_ALLOW_NONE;
14225 break;
14227 default:
14228 gcc_unreachable ();
14231 if (!TYPE_P (arg))
14233 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14234 if (type_unknown_p (arg))
14236 /* [temp.deduct.type] A template-argument can be
14237 deduced from a pointer to function or pointer
14238 to member function argument if the set of
14239 overloaded functions does not contain function
14240 templates and at most one of a set of
14241 overloaded functions provides a unique
14242 match. */
14244 if (resolve_overloaded_unification
14245 (tparms, targs, parm, arg,
14246 (unification_kind_t) strict,
14247 sub_strict)
14248 != 0)
14249 return 1;
14250 skip_arg_p = true;
14253 if (!skip_arg_p)
14255 arg_expr = arg;
14256 arg = unlowered_expr_type (arg);
14257 if (arg == error_mark_node)
14258 return 1;
14262 arg_strict = sub_strict;
14264 if (!subr)
14265 arg_strict |=
14266 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14267 &parm, &arg, arg_expr);
14270 if (!skip_arg_p)
14272 /* For deduction from an init-list we need the actual list. */
14273 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14274 arg = arg_expr;
14275 if (unify (tparms, targs, parm, arg, arg_strict))
14276 return 1;
14280 /* For each parameter pack, collect the deduced value. */
14281 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14283 int idx, level;
14284 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14286 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14287 TMPL_ARG (targs, level, idx);
14291 /* Verify that the results of unification with the parameter packs
14292 produce results consistent with what we've seen before, and make
14293 the deduced argument packs available. */
14294 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14296 tree old_pack = TREE_VALUE (pack);
14297 tree new_args = TREE_TYPE (pack);
14298 int i, len = TREE_VEC_LENGTH (new_args);
14299 int idx, level;
14300 bool nondeduced_p = false;
14302 /* By default keep the original deduced argument pack.
14303 If necessary, more specific code is going to update the
14304 resulting deduced argument later down in this function. */
14305 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14306 TMPL_ARG (targs, level, idx) = old_pack;
14308 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14309 actually deduce anything. */
14310 for (i = 0; i < len && !nondeduced_p; ++i)
14311 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14312 nondeduced_p = true;
14313 if (nondeduced_p)
14314 continue;
14316 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14318 /* Prepend the explicit arguments onto NEW_ARGS. */
14319 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14320 tree old_args = new_args;
14321 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14322 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14324 /* Copy the explicit arguments. */
14325 new_args = make_tree_vec (len);
14326 for (i = 0; i < explicit_len; i++)
14327 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14329 /* Copy the deduced arguments. */
14330 for (; i < len; i++)
14331 TREE_VEC_ELT (new_args, i) =
14332 TREE_VEC_ELT (old_args, i - explicit_len);
14335 if (!old_pack)
14337 tree result;
14338 /* Build the deduced *_ARGUMENT_PACK. */
14339 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14341 result = make_node (NONTYPE_ARGUMENT_PACK);
14342 TREE_TYPE (result) =
14343 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14344 TREE_CONSTANT (result) = 1;
14346 else
14347 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14349 SET_ARGUMENT_PACK_ARGS (result, new_args);
14351 /* Note the deduced argument packs for this parameter
14352 pack. */
14353 TMPL_ARG (targs, level, idx) = result;
14355 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14356 && (ARGUMENT_PACK_ARGS (old_pack)
14357 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14359 /* We only had the explicitly-provided arguments before, but
14360 now we have a complete set of arguments. */
14361 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14363 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14364 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14365 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14367 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14368 new_args))
14369 /* Inconsistent unification of this parameter pack. */
14370 return 1;
14373 return 0;
14376 /* Deduce the value of template parameters. TPARMS is the (innermost)
14377 set of template parameters to a template. TARGS is the bindings
14378 for those template parameters, as determined thus far; TARGS may
14379 include template arguments for outer levels of template parameters
14380 as well. PARM is a parameter to a template function, or a
14381 subcomponent of that parameter; ARG is the corresponding argument.
14382 This function attempts to match PARM with ARG in a manner
14383 consistent with the existing assignments in TARGS. If more values
14384 are deduced, then TARGS is updated.
14386 Returns 0 if the type deduction succeeds, 1 otherwise. The
14387 parameter STRICT is a bitwise or of the following flags:
14389 UNIFY_ALLOW_NONE:
14390 Require an exact match between PARM and ARG.
14391 UNIFY_ALLOW_MORE_CV_QUAL:
14392 Allow the deduced ARG to be more cv-qualified (by qualification
14393 conversion) than ARG.
14394 UNIFY_ALLOW_LESS_CV_QUAL:
14395 Allow the deduced ARG to be less cv-qualified than ARG.
14396 UNIFY_ALLOW_DERIVED:
14397 Allow the deduced ARG to be a template base class of ARG,
14398 or a pointer to a template base class of the type pointed to by
14399 ARG.
14400 UNIFY_ALLOW_INTEGER:
14401 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14402 case for more information.
14403 UNIFY_ALLOW_OUTER_LEVEL:
14404 This is the outermost level of a deduction. Used to determine validity
14405 of qualification conversions. A valid qualification conversion must
14406 have const qualified pointers leading up to the inner type which
14407 requires additional CV quals, except at the outer level, where const
14408 is not required [conv.qual]. It would be normal to set this flag in
14409 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14410 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14411 This is the outermost level of a deduction, and PARM can be more CV
14412 qualified at this point.
14413 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14414 This is the outermost level of a deduction, and PARM can be less CV
14415 qualified at this point. */
14417 static int
14418 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14420 int idx;
14421 tree targ;
14422 tree tparm;
14423 int strict_in = strict;
14425 /* I don't think this will do the right thing with respect to types.
14426 But the only case I've seen it in so far has been array bounds, where
14427 signedness is the only information lost, and I think that will be
14428 okay. */
14429 while (TREE_CODE (parm) == NOP_EXPR)
14430 parm = TREE_OPERAND (parm, 0);
14432 if (arg == error_mark_node)
14433 return 1;
14434 if (arg == unknown_type_node
14435 || arg == init_list_type_node)
14436 /* We can't deduce anything from this, but we might get all the
14437 template args from other function args. */
14438 return 0;
14440 /* If PARM uses template parameters, then we can't bail out here,
14441 even if ARG == PARM, since we won't record unifications for the
14442 template parameters. We might need them if we're trying to
14443 figure out which of two things is more specialized. */
14444 if (arg == parm && !uses_template_parms (parm))
14445 return 0;
14447 /* Handle init lists early, so the rest of the function can assume
14448 we're dealing with a type. */
14449 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14451 tree elt, elttype;
14452 unsigned i;
14453 tree orig_parm = parm;
14455 /* Replace T with std::initializer_list<T> for deduction. */
14456 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14457 && flag_deduce_init_list)
14458 parm = listify (parm);
14460 if (!is_std_init_list (parm))
14461 /* We can only deduce from an initializer list argument if the
14462 parameter is std::initializer_list; otherwise this is a
14463 non-deduced context. */
14464 return 0;
14466 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14468 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14470 int elt_strict = strict;
14471 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14473 tree type = TREE_TYPE (elt);
14474 /* It should only be possible to get here for a call. */
14475 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14476 elt_strict |= maybe_adjust_types_for_deduction
14477 (DEDUCE_CALL, &elttype, &type, elt);
14478 elt = type;
14481 if (unify (tparms, targs, elttype, elt, elt_strict))
14482 return 1;
14485 /* If the std::initializer_list<T> deduction worked, replace the
14486 deduced A with std::initializer_list<A>. */
14487 if (orig_parm != parm)
14489 idx = TEMPLATE_TYPE_IDX (orig_parm);
14490 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14491 targ = listify (targ);
14492 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14494 return 0;
14497 /* Immediately reject some pairs that won't unify because of
14498 cv-qualification mismatches. */
14499 if (TREE_CODE (arg) == TREE_CODE (parm)
14500 && TYPE_P (arg)
14501 /* It is the elements of the array which hold the cv quals of an array
14502 type, and the elements might be template type parms. We'll check
14503 when we recurse. */
14504 && TREE_CODE (arg) != ARRAY_TYPE
14505 /* We check the cv-qualifiers when unifying with template type
14506 parameters below. We want to allow ARG `const T' to unify with
14507 PARM `T' for example, when computing which of two templates
14508 is more specialized, for example. */
14509 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14510 && !check_cv_quals_for_unify (strict_in, arg, parm))
14511 return 1;
14513 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14514 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14515 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14516 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14517 strict &= ~UNIFY_ALLOW_DERIVED;
14518 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14519 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14521 switch (TREE_CODE (parm))
14523 case TYPENAME_TYPE:
14524 case SCOPE_REF:
14525 case UNBOUND_CLASS_TEMPLATE:
14526 /* In a type which contains a nested-name-specifier, template
14527 argument values cannot be deduced for template parameters used
14528 within the nested-name-specifier. */
14529 return 0;
14531 case TEMPLATE_TYPE_PARM:
14532 case TEMPLATE_TEMPLATE_PARM:
14533 case BOUND_TEMPLATE_TEMPLATE_PARM:
14534 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14535 if (tparm == error_mark_node)
14536 return 1;
14538 if (TEMPLATE_TYPE_LEVEL (parm)
14539 != template_decl_level (tparm))
14540 /* The PARM is not one we're trying to unify. Just check
14541 to see if it matches ARG. */
14542 return (TREE_CODE (arg) == TREE_CODE (parm)
14543 && same_type_p (parm, arg)) ? 0 : 1;
14544 idx = TEMPLATE_TYPE_IDX (parm);
14545 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14546 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14548 /* Check for mixed types and values. */
14549 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14550 && TREE_CODE (tparm) != TYPE_DECL)
14551 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14552 && TREE_CODE (tparm) != TEMPLATE_DECL))
14553 return 1;
14555 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14557 /* ARG must be constructed from a template class or a template
14558 template parameter. */
14559 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14560 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14561 return 1;
14564 tree parmvec = TYPE_TI_ARGS (parm);
14565 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14566 tree parm_parms
14567 = DECL_INNERMOST_TEMPLATE_PARMS
14568 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14569 int i, len;
14570 int parm_variadic_p = 0;
14572 /* The resolution to DR150 makes clear that default
14573 arguments for an N-argument may not be used to bind T
14574 to a template template parameter with fewer than N
14575 parameters. It is not safe to permit the binding of
14576 default arguments as an extension, as that may change
14577 the meaning of a conforming program. Consider:
14579 struct Dense { static const unsigned int dim = 1; };
14581 template <template <typename> class View,
14582 typename Block>
14583 void operator+(float, View<Block> const&);
14585 template <typename Block,
14586 unsigned int Dim = Block::dim>
14587 struct Lvalue_proxy { operator float() const; };
14589 void
14590 test_1d (void) {
14591 Lvalue_proxy<Dense> p;
14592 float b;
14593 b + p;
14596 Here, if Lvalue_proxy is permitted to bind to View, then
14597 the global operator+ will be used; if they are not, the
14598 Lvalue_proxy will be converted to float. */
14599 if (coerce_template_parms (parm_parms,
14600 argvec,
14601 TYPE_TI_TEMPLATE (parm),
14602 tf_none,
14603 /*require_all_args=*/true,
14604 /*use_default_args=*/false)
14605 == error_mark_node)
14606 return 1;
14608 /* Deduce arguments T, i from TT<T> or TT<i>.
14609 We check each element of PARMVEC and ARGVEC individually
14610 rather than the whole TREE_VEC since they can have
14611 different number of elements. */
14613 parmvec = expand_template_argument_pack (parmvec);
14614 argvec = expand_template_argument_pack (argvec);
14616 len = TREE_VEC_LENGTH (parmvec);
14618 /* Check if the parameters end in a pack, making them
14619 variadic. */
14620 if (len > 0
14621 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14622 parm_variadic_p = 1;
14624 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14625 return 1;
14627 for (i = 0; i < len - parm_variadic_p; ++i)
14629 if (unify (tparms, targs,
14630 TREE_VEC_ELT (parmvec, i),
14631 TREE_VEC_ELT (argvec, i),
14632 UNIFY_ALLOW_NONE))
14633 return 1;
14636 if (parm_variadic_p
14637 && unify_pack_expansion (tparms, targs,
14638 parmvec, argvec,
14639 UNIFY_ALLOW_NONE,
14640 /*call_args_p=*/false,
14641 /*subr=*/false))
14642 return 1;
14644 arg = TYPE_TI_TEMPLATE (arg);
14646 /* Fall through to deduce template name. */
14649 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14650 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14652 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14654 /* Simple cases: Value already set, does match or doesn't. */
14655 if (targ != NULL_TREE && template_args_equal (targ, arg))
14656 return 0;
14657 else if (targ)
14658 return 1;
14660 else
14662 /* If PARM is `const T' and ARG is only `int', we don't have
14663 a match unless we are allowing additional qualification.
14664 If ARG is `const int' and PARM is just `T' that's OK;
14665 that binds `const int' to `T'. */
14666 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14667 arg, parm))
14668 return 1;
14670 /* Consider the case where ARG is `const volatile int' and
14671 PARM is `const T'. Then, T should be `volatile int'. */
14672 arg = cp_build_qualified_type_real
14673 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14674 if (arg == error_mark_node)
14675 return 1;
14677 /* Simple cases: Value already set, does match or doesn't. */
14678 if (targ != NULL_TREE && same_type_p (targ, arg))
14679 return 0;
14680 else if (targ)
14681 return 1;
14683 /* Make sure that ARG is not a variable-sized array. (Note
14684 that were talking about variable-sized arrays (like
14685 `int[n]'), rather than arrays of unknown size (like
14686 `int[]').) We'll get very confused by such a type since
14687 the bound of the array will not be computable in an
14688 instantiation. Besides, such types are not allowed in
14689 ISO C++, so we can do as we please here. */
14690 if (variably_modified_type_p (arg, NULL_TREE))
14691 return 1;
14693 /* Strip typedefs as in convert_template_argument. */
14694 arg = strip_typedefs (arg);
14697 /* If ARG is a parameter pack or an expansion, we cannot unify
14698 against it unless PARM is also a parameter pack. */
14699 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14700 && !template_parameter_pack_p (parm))
14701 return 1;
14703 /* If the argument deduction results is a METHOD_TYPE,
14704 then there is a problem.
14705 METHOD_TYPE doesn't map to any real C++ type the result of
14706 the deduction can not be of that type. */
14707 if (TREE_CODE (arg) == METHOD_TYPE)
14708 return 1;
14710 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14711 return 0;
14713 case TEMPLATE_PARM_INDEX:
14714 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14715 if (tparm == error_mark_node)
14716 return 1;
14718 if (TEMPLATE_PARM_LEVEL (parm)
14719 != template_decl_level (tparm))
14720 /* The PARM is not one we're trying to unify. Just check
14721 to see if it matches ARG. */
14722 return !(TREE_CODE (arg) == TREE_CODE (parm)
14723 && cp_tree_equal (parm, arg));
14725 idx = TEMPLATE_PARM_IDX (parm);
14726 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14728 if (targ)
14729 return !cp_tree_equal (targ, arg);
14731 /* [temp.deduct.type] If, in the declaration of a function template
14732 with a non-type template-parameter, the non-type
14733 template-parameter is used in an expression in the function
14734 parameter-list and, if the corresponding template-argument is
14735 deduced, the template-argument type shall match the type of the
14736 template-parameter exactly, except that a template-argument
14737 deduced from an array bound may be of any integral type.
14738 The non-type parameter might use already deduced type parameters. */
14739 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14740 if (!TREE_TYPE (arg))
14741 /* Template-parameter dependent expression. Just accept it for now.
14742 It will later be processed in convert_template_argument. */
14744 else if (same_type_p (TREE_TYPE (arg), tparm))
14745 /* OK */;
14746 else if ((strict & UNIFY_ALLOW_INTEGER)
14747 && (TREE_CODE (tparm) == INTEGER_TYPE
14748 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14749 /* Convert the ARG to the type of PARM; the deduced non-type
14750 template argument must exactly match the types of the
14751 corresponding parameter. */
14752 arg = fold (build_nop (tparm, arg));
14753 else if (uses_template_parms (tparm))
14754 /* We haven't deduced the type of this parameter yet. Try again
14755 later. */
14756 return 0;
14757 else
14758 return 1;
14760 /* If ARG is a parameter pack or an expansion, we cannot unify
14761 against it unless PARM is also a parameter pack. */
14762 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14763 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14764 return 1;
14766 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14767 return 0;
14769 case PTRMEM_CST:
14771 /* A pointer-to-member constant can be unified only with
14772 another constant. */
14773 if (TREE_CODE (arg) != PTRMEM_CST)
14774 return 1;
14776 /* Just unify the class member. It would be useless (and possibly
14777 wrong, depending on the strict flags) to unify also
14778 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14779 arg refer to the same variable, even if through different
14780 classes. For instance:
14782 struct A { int x; };
14783 struct B : A { };
14785 Unification of &A::x and &B::x must succeed. */
14786 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14787 PTRMEM_CST_MEMBER (arg), strict);
14790 case POINTER_TYPE:
14792 if (TREE_CODE (arg) != POINTER_TYPE)
14793 return 1;
14795 /* [temp.deduct.call]
14797 A can be another pointer or pointer to member type that can
14798 be converted to the deduced A via a qualification
14799 conversion (_conv.qual_).
14801 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14802 This will allow for additional cv-qualification of the
14803 pointed-to types if appropriate. */
14805 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14806 /* The derived-to-base conversion only persists through one
14807 level of pointers. */
14808 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14810 return unify (tparms, targs, TREE_TYPE (parm),
14811 TREE_TYPE (arg), strict);
14814 case REFERENCE_TYPE:
14815 if (TREE_CODE (arg) != REFERENCE_TYPE)
14816 return 1;
14817 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14818 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14820 case ARRAY_TYPE:
14821 if (TREE_CODE (arg) != ARRAY_TYPE)
14822 return 1;
14823 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14824 != (TYPE_DOMAIN (arg) == NULL_TREE))
14825 return 1;
14826 if (TYPE_DOMAIN (parm) != NULL_TREE)
14828 tree parm_max;
14829 tree arg_max;
14830 bool parm_cst;
14831 bool arg_cst;
14833 /* Our representation of array types uses "N - 1" as the
14834 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14835 not an integer constant. We cannot unify arbitrarily
14836 complex expressions, so we eliminate the MINUS_EXPRs
14837 here. */
14838 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14839 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14840 if (!parm_cst)
14842 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14843 parm_max = TREE_OPERAND (parm_max, 0);
14845 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14846 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14847 if (!arg_cst)
14849 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14850 trying to unify the type of a variable with the type
14851 of a template parameter. For example:
14853 template <unsigned int N>
14854 void f (char (&) [N]);
14855 int g();
14856 void h(int i) {
14857 char a[g(i)];
14858 f(a);
14861 Here, the type of the ARG will be "int [g(i)]", and
14862 may be a SAVE_EXPR, etc. */
14863 if (TREE_CODE (arg_max) != MINUS_EXPR)
14864 return 1;
14865 arg_max = TREE_OPERAND (arg_max, 0);
14868 /* If only one of the bounds used a MINUS_EXPR, compensate
14869 by adding one to the other bound. */
14870 if (parm_cst && !arg_cst)
14871 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14872 integer_type_node,
14873 parm_max,
14874 integer_one_node);
14875 else if (arg_cst && !parm_cst)
14876 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14877 integer_type_node,
14878 arg_max,
14879 integer_one_node);
14881 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14882 return 1;
14884 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14885 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14887 case REAL_TYPE:
14888 case COMPLEX_TYPE:
14889 case VECTOR_TYPE:
14890 case INTEGER_TYPE:
14891 case BOOLEAN_TYPE:
14892 case ENUMERAL_TYPE:
14893 case VOID_TYPE:
14894 if (TREE_CODE (arg) != TREE_CODE (parm))
14895 return 1;
14897 /* We have already checked cv-qualification at the top of the
14898 function. */
14899 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14900 return 1;
14902 /* As far as unification is concerned, this wins. Later checks
14903 will invalidate it if necessary. */
14904 return 0;
14906 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14907 /* Type INTEGER_CST can come from ordinary constant template args. */
14908 case INTEGER_CST:
14909 while (TREE_CODE (arg) == NOP_EXPR)
14910 arg = TREE_OPERAND (arg, 0);
14912 if (TREE_CODE (arg) != INTEGER_CST)
14913 return 1;
14914 return !tree_int_cst_equal (parm, arg);
14916 case TREE_VEC:
14918 int i;
14919 if (TREE_CODE (arg) != TREE_VEC)
14920 return 1;
14921 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14922 return 1;
14923 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14924 if (unify (tparms, targs,
14925 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14926 UNIFY_ALLOW_NONE))
14927 return 1;
14928 return 0;
14931 case RECORD_TYPE:
14932 case UNION_TYPE:
14933 if (TREE_CODE (arg) != TREE_CODE (parm))
14934 return 1;
14936 if (TYPE_PTRMEMFUNC_P (parm))
14938 if (!TYPE_PTRMEMFUNC_P (arg))
14939 return 1;
14941 return unify (tparms, targs,
14942 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14943 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14944 strict);
14947 if (CLASSTYPE_TEMPLATE_INFO (parm))
14949 tree t = NULL_TREE;
14951 if (strict_in & UNIFY_ALLOW_DERIVED)
14953 /* First, we try to unify the PARM and ARG directly. */
14954 t = try_class_unification (tparms, targs,
14955 parm, arg);
14957 if (!t)
14959 /* Fallback to the special case allowed in
14960 [temp.deduct.call]:
14962 If P is a class, and P has the form
14963 template-id, then A can be a derived class of
14964 the deduced A. Likewise, if P is a pointer to
14965 a class of the form template-id, A can be a
14966 pointer to a derived class pointed to by the
14967 deduced A. */
14968 t = get_template_base (tparms, targs, parm, arg);
14970 if (!t)
14971 return 1;
14974 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14975 && (CLASSTYPE_TI_TEMPLATE (parm)
14976 == CLASSTYPE_TI_TEMPLATE (arg)))
14977 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14978 Then, we should unify `int' and `U'. */
14979 t = arg;
14980 else
14981 /* There's no chance of unification succeeding. */
14982 return 1;
14984 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14985 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14987 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14988 return 1;
14989 return 0;
14991 case METHOD_TYPE:
14992 case FUNCTION_TYPE:
14994 unsigned int nargs;
14995 tree *args;
14996 tree a;
14997 unsigned int i;
14999 if (TREE_CODE (arg) != TREE_CODE (parm))
15000 return 1;
15002 /* CV qualifications for methods can never be deduced, they must
15003 match exactly. We need to check them explicitly here,
15004 because type_unification_real treats them as any other
15005 cv-qualified parameter. */
15006 if (TREE_CODE (parm) == METHOD_TYPE
15007 && (!check_cv_quals_for_unify
15008 (UNIFY_ALLOW_NONE,
15009 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
15010 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
15011 return 1;
15013 if (unify (tparms, targs, TREE_TYPE (parm),
15014 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15015 return 1;
15017 nargs = list_length (TYPE_ARG_TYPES (arg));
15018 args = XALLOCAVEC (tree, nargs);
15019 for (a = TYPE_ARG_TYPES (arg), i = 0;
15020 a != NULL_TREE && a != void_list_node;
15021 a = TREE_CHAIN (a), ++i)
15022 args[i] = TREE_VALUE (a);
15023 nargs = i;
15025 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15026 args, nargs, 1, DEDUCE_EXACT,
15027 LOOKUP_NORMAL);
15030 case OFFSET_TYPE:
15031 /* Unify a pointer to member with a pointer to member function, which
15032 deduces the type of the member as a function type. */
15033 if (TYPE_PTRMEMFUNC_P (arg))
15035 tree method_type;
15036 tree fntype;
15037 cp_cv_quals cv_quals;
15039 /* Check top-level cv qualifiers */
15040 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15041 return 1;
15043 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15044 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15045 return 1;
15047 /* Determine the type of the function we are unifying against. */
15048 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15049 fntype =
15050 build_function_type (TREE_TYPE (method_type),
15051 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15053 /* Extract the cv-qualifiers of the member function from the
15054 implicit object parameter and place them on the function
15055 type to be restored later. */
15056 cv_quals =
15057 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
15058 fntype = build_qualified_type (fntype, cv_quals);
15059 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15062 if (TREE_CODE (arg) != OFFSET_TYPE)
15063 return 1;
15064 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15065 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15066 return 1;
15067 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15068 strict);
15070 case CONST_DECL:
15071 if (DECL_TEMPLATE_PARM_P (parm))
15072 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15073 if (arg != integral_constant_value (parm))
15074 return 1;
15075 return 0;
15077 case FIELD_DECL:
15078 case TEMPLATE_DECL:
15079 /* Matched cases are handled by the ARG == PARM test above. */
15080 return 1;
15082 case VAR_DECL:
15083 /* A non-type template parameter that is a variable should be a
15084 an integral constant, in which case, it whould have been
15085 folded into its (constant) value. So we should not be getting
15086 a variable here. */
15087 gcc_unreachable ();
15089 case TYPE_ARGUMENT_PACK:
15090 case NONTYPE_ARGUMENT_PACK:
15092 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15093 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15094 int i, len = TREE_VEC_LENGTH (packed_parms);
15095 int argslen = TREE_VEC_LENGTH (packed_args);
15096 int parm_variadic_p = 0;
15098 for (i = 0; i < len; ++i)
15100 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15102 if (i == len - 1)
15103 /* We can unify against something with a trailing
15104 parameter pack. */
15105 parm_variadic_p = 1;
15106 else
15107 /* Since there is something following the pack
15108 expansion, we cannot unify this template argument
15109 list. */
15110 return 0;
15115 /* If we don't have enough arguments to satisfy the parameters
15116 (not counting the pack expression at the end), or we have
15117 too many arguments for a parameter list that doesn't end in
15118 a pack expression, we can't unify. */
15119 if (argslen < (len - parm_variadic_p)
15120 || (argslen > len && !parm_variadic_p))
15121 return 1;
15123 /* Unify all of the parameters that precede the (optional)
15124 pack expression. */
15125 for (i = 0; i < len - parm_variadic_p; ++i)
15127 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15128 TREE_VEC_ELT (packed_args, i), strict))
15129 return 1;
15132 if (parm_variadic_p)
15133 return unify_pack_expansion (tparms, targs,
15134 packed_parms, packed_args,
15135 strict, /*call_args_p=*/false,
15136 /*subr=*/false);
15137 return 0;
15140 break;
15142 case TYPEOF_TYPE:
15143 case DECLTYPE_TYPE:
15144 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15145 nodes. */
15146 return 0;
15148 case ERROR_MARK:
15149 /* Unification fails if we hit an error node. */
15150 return 1;
15152 default:
15153 gcc_assert (EXPR_P (parm));
15155 /* We must be looking at an expression. This can happen with
15156 something like:
15158 template <int I>
15159 void foo(S<I>, S<I + 2>);
15161 This is a "nondeduced context":
15163 [deduct.type]
15165 The nondeduced contexts are:
15167 --A type that is a template-id in which one or more of
15168 the template-arguments is an expression that references
15169 a template-parameter.
15171 In these cases, we assume deduction succeeded, but don't
15172 actually infer any unifications. */
15174 if (!uses_template_parms (parm)
15175 && !template_args_equal (parm, arg))
15176 return 1;
15177 else
15178 return 0;
15182 /* Note that DECL can be defined in this translation unit, if
15183 required. */
15185 static void
15186 mark_definable (tree decl)
15188 tree clone;
15189 DECL_NOT_REALLY_EXTERN (decl) = 1;
15190 FOR_EACH_CLONE (clone, decl)
15191 DECL_NOT_REALLY_EXTERN (clone) = 1;
15194 /* Called if RESULT is explicitly instantiated, or is a member of an
15195 explicitly instantiated class. */
15197 void
15198 mark_decl_instantiated (tree result, int extern_p)
15200 SET_DECL_EXPLICIT_INSTANTIATION (result);
15202 /* If this entity has already been written out, it's too late to
15203 make any modifications. */
15204 if (TREE_ASM_WRITTEN (result))
15205 return;
15207 if (TREE_CODE (result) != FUNCTION_DECL)
15208 /* The TREE_PUBLIC flag for function declarations will have been
15209 set correctly by tsubst. */
15210 TREE_PUBLIC (result) = 1;
15212 /* This might have been set by an earlier implicit instantiation. */
15213 DECL_COMDAT (result) = 0;
15215 if (extern_p)
15216 DECL_NOT_REALLY_EXTERN (result) = 0;
15217 else
15219 mark_definable (result);
15220 /* Always make artificials weak. */
15221 if (DECL_ARTIFICIAL (result) && flag_weak)
15222 comdat_linkage (result);
15223 /* For WIN32 we also want to put explicit instantiations in
15224 linkonce sections. */
15225 else if (TREE_PUBLIC (result))
15226 maybe_make_one_only (result);
15229 /* If EXTERN_P, then this function will not be emitted -- unless
15230 followed by an explicit instantiation, at which point its linkage
15231 will be adjusted. If !EXTERN_P, then this function will be
15232 emitted here. In neither circumstance do we want
15233 import_export_decl to adjust the linkage. */
15234 DECL_INTERFACE_KNOWN (result) = 1;
15237 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15238 important template arguments. If any are missing, we check whether
15239 they're important by using error_mark_node for substituting into any
15240 args that were used for partial ordering (the ones between ARGS and END)
15241 and seeing if it bubbles up. */
15243 static bool
15244 check_undeduced_parms (tree targs, tree args, tree end)
15246 bool found = false;
15247 int i;
15248 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15249 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15251 found = true;
15252 TREE_VEC_ELT (targs, i) = error_mark_node;
15254 if (found)
15256 for (; args != end; args = TREE_CHAIN (args))
15258 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15259 if (substed == error_mark_node)
15260 return true;
15263 return false;
15266 /* Given two function templates PAT1 and PAT2, return:
15268 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15269 -1 if PAT2 is more specialized than PAT1.
15270 0 if neither is more specialized.
15272 LEN indicates the number of parameters we should consider
15273 (defaulted parameters should not be considered).
15275 The 1998 std underspecified function template partial ordering, and
15276 DR214 addresses the issue. We take pairs of arguments, one from
15277 each of the templates, and deduce them against each other. One of
15278 the templates will be more specialized if all the *other*
15279 template's arguments deduce against its arguments and at least one
15280 of its arguments *does* *not* deduce against the other template's
15281 corresponding argument. Deduction is done as for class templates.
15282 The arguments used in deduction have reference and top level cv
15283 qualifiers removed. Iff both arguments were originally reference
15284 types *and* deduction succeeds in both directions, the template
15285 with the more cv-qualified argument wins for that pairing (if
15286 neither is more cv-qualified, they both are equal). Unlike regular
15287 deduction, after all the arguments have been deduced in this way,
15288 we do *not* verify the deduced template argument values can be
15289 substituted into non-deduced contexts.
15291 The logic can be a bit confusing here, because we look at deduce1 and
15292 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15293 can find template arguments for pat1 to make arg1 look like arg2, that
15294 means that arg2 is at least as specialized as arg1. */
15297 more_specialized_fn (tree pat1, tree pat2, int len)
15299 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15300 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15301 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15302 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15303 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15304 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15305 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15306 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15307 tree origs1, origs2;
15308 bool lose1 = false;
15309 bool lose2 = false;
15311 /* Remove the this parameter from non-static member functions. If
15312 one is a non-static member function and the other is not a static
15313 member function, remove the first parameter from that function
15314 also. This situation occurs for operator functions where we
15315 locate both a member function (with this pointer) and non-member
15316 operator (with explicit first operand). */
15317 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15319 len--; /* LEN is the number of significant arguments for DECL1 */
15320 args1 = TREE_CHAIN (args1);
15321 if (!DECL_STATIC_FUNCTION_P (decl2))
15322 args2 = TREE_CHAIN (args2);
15324 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15326 args2 = TREE_CHAIN (args2);
15327 if (!DECL_STATIC_FUNCTION_P (decl1))
15329 len--;
15330 args1 = TREE_CHAIN (args1);
15334 /* If only one is a conversion operator, they are unordered. */
15335 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15336 return 0;
15338 /* Consider the return type for a conversion function */
15339 if (DECL_CONV_FN_P (decl1))
15341 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15342 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15343 len++;
15346 processing_template_decl++;
15348 origs1 = args1;
15349 origs2 = args2;
15351 while (len--
15352 /* Stop when an ellipsis is seen. */
15353 && args1 != NULL_TREE && args2 != NULL_TREE)
15355 tree arg1 = TREE_VALUE (args1);
15356 tree arg2 = TREE_VALUE (args2);
15357 int deduce1, deduce2;
15358 int quals1 = -1;
15359 int quals2 = -1;
15361 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15362 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15364 /* When both arguments are pack expansions, we need only
15365 unify the patterns themselves. */
15366 arg1 = PACK_EXPANSION_PATTERN (arg1);
15367 arg2 = PACK_EXPANSION_PATTERN (arg2);
15369 /* This is the last comparison we need to do. */
15370 len = 0;
15373 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15375 arg1 = TREE_TYPE (arg1);
15376 quals1 = cp_type_quals (arg1);
15379 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15381 arg2 = TREE_TYPE (arg2);
15382 quals2 = cp_type_quals (arg2);
15385 if ((quals1 < 0) != (quals2 < 0))
15387 /* Only of the args is a reference, see if we should apply
15388 array/function pointer decay to it. This is not part of
15389 DR214, but is, IMHO, consistent with the deduction rules
15390 for the function call itself, and with our earlier
15391 implementation of the underspecified partial ordering
15392 rules. (nathan). */
15393 if (quals1 >= 0)
15395 switch (TREE_CODE (arg1))
15397 case ARRAY_TYPE:
15398 arg1 = TREE_TYPE (arg1);
15399 /* FALLTHROUGH. */
15400 case FUNCTION_TYPE:
15401 arg1 = build_pointer_type (arg1);
15402 break;
15404 default:
15405 break;
15408 else
15410 switch (TREE_CODE (arg2))
15412 case ARRAY_TYPE:
15413 arg2 = TREE_TYPE (arg2);
15414 /* FALLTHROUGH. */
15415 case FUNCTION_TYPE:
15416 arg2 = build_pointer_type (arg2);
15417 break;
15419 default:
15420 break;
15425 arg1 = TYPE_MAIN_VARIANT (arg1);
15426 arg2 = TYPE_MAIN_VARIANT (arg2);
15428 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15430 int i, len2 = list_length (args2);
15431 tree parmvec = make_tree_vec (1);
15432 tree argvec = make_tree_vec (len2);
15433 tree ta = args2;
15435 /* Setup the parameter vector, which contains only ARG1. */
15436 TREE_VEC_ELT (parmvec, 0) = arg1;
15438 /* Setup the argument vector, which contains the remaining
15439 arguments. */
15440 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15441 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15443 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15444 argvec, UNIFY_ALLOW_NONE,
15445 /*call_args_p=*/false,
15446 /*subr=*/0);
15448 /* We cannot deduce in the other direction, because ARG1 is
15449 a pack expansion but ARG2 is not. */
15450 deduce2 = 0;
15452 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15454 int i, len1 = list_length (args1);
15455 tree parmvec = make_tree_vec (1);
15456 tree argvec = make_tree_vec (len1);
15457 tree ta = args1;
15459 /* Setup the parameter vector, which contains only ARG1. */
15460 TREE_VEC_ELT (parmvec, 0) = arg2;
15462 /* Setup the argument vector, which contains the remaining
15463 arguments. */
15464 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15465 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15467 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15468 argvec, UNIFY_ALLOW_NONE,
15469 /*call_args_p=*/false,
15470 /*subr=*/0);
15472 /* We cannot deduce in the other direction, because ARG2 is
15473 a pack expansion but ARG1 is not.*/
15474 deduce1 = 0;
15477 else
15479 /* The normal case, where neither argument is a pack
15480 expansion. */
15481 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15482 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15485 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15486 arg2, then arg2 is not as specialized as arg1. */
15487 if (!deduce1)
15488 lose2 = true;
15489 if (!deduce2)
15490 lose1 = true;
15492 /* "If, for a given type, deduction succeeds in both directions
15493 (i.e., the types are identical after the transformations above)
15494 and if the type from the argument template is more cv-qualified
15495 than the type from the parameter template (as described above)
15496 that type is considered to be more specialized than the other. If
15497 neither type is more cv-qualified than the other then neither type
15498 is more specialized than the other." */
15500 if (deduce1 && deduce2
15501 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
15503 if ((quals1 & quals2) == quals2)
15504 lose2 = true;
15505 if ((quals1 & quals2) == quals1)
15506 lose1 = true;
15509 if (lose1 && lose2)
15510 /* We've failed to deduce something in either direction.
15511 These must be unordered. */
15512 break;
15514 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15515 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15516 /* We have already processed all of the arguments in our
15517 handing of the pack expansion type. */
15518 len = 0;
15520 args1 = TREE_CHAIN (args1);
15521 args2 = TREE_CHAIN (args2);
15524 /* "In most cases, all template parameters must have values in order for
15525 deduction to succeed, but for partial ordering purposes a template
15526 parameter may remain without a value provided it is not used in the
15527 types being used for partial ordering."
15529 Thus, if we are missing any of the targs1 we need to substitute into
15530 origs1, then pat2 is not as specialized as pat1. This can happen when
15531 there is a nondeduced context. */
15532 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15533 lose2 = true;
15534 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15535 lose1 = true;
15537 processing_template_decl--;
15539 /* All things being equal, if the next argument is a pack expansion
15540 for one function but not for the other, prefer the
15541 non-variadic function. FIXME this is bogus; see c++/41958. */
15542 if (lose1 == lose2
15543 && args1 && TREE_VALUE (args1)
15544 && args2 && TREE_VALUE (args2))
15546 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15547 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15550 if (lose1 == lose2)
15551 return 0;
15552 else if (!lose1)
15553 return 1;
15554 else
15555 return -1;
15558 /* Determine which of two partial specializations is more specialized.
15560 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15561 to the first partial specialization. The TREE_VALUE is the
15562 innermost set of template parameters for the partial
15563 specialization. PAT2 is similar, but for the second template.
15565 Return 1 if the first partial specialization is more specialized;
15566 -1 if the second is more specialized; 0 if neither is more
15567 specialized.
15569 See [temp.class.order] for information about determining which of
15570 two templates is more specialized. */
15572 static int
15573 more_specialized_class (tree pat1, tree pat2)
15575 tree targs;
15576 tree tmpl1, tmpl2;
15577 int winner = 0;
15578 bool any_deductions = false;
15580 tmpl1 = TREE_TYPE (pat1);
15581 tmpl2 = TREE_TYPE (pat2);
15583 /* Just like what happens for functions, if we are ordering between
15584 different class template specializations, we may encounter dependent
15585 types in the arguments, and we need our dependency check functions
15586 to behave correctly. */
15587 ++processing_template_decl;
15588 targs = get_class_bindings (TREE_VALUE (pat1),
15589 CLASSTYPE_TI_ARGS (tmpl1),
15590 CLASSTYPE_TI_ARGS (tmpl2));
15591 if (targs)
15593 --winner;
15594 any_deductions = true;
15597 targs = get_class_bindings (TREE_VALUE (pat2),
15598 CLASSTYPE_TI_ARGS (tmpl2),
15599 CLASSTYPE_TI_ARGS (tmpl1));
15600 if (targs)
15602 ++winner;
15603 any_deductions = true;
15605 --processing_template_decl;
15607 /* In the case of a tie where at least one of the class templates
15608 has a parameter pack at the end, the template with the most
15609 non-packed parameters wins. */
15610 if (winner == 0
15611 && any_deductions
15612 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15613 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15615 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15616 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15617 int len1 = TREE_VEC_LENGTH (args1);
15618 int len2 = TREE_VEC_LENGTH (args2);
15620 /* We don't count the pack expansion at the end. */
15621 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15622 --len1;
15623 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15624 --len2;
15626 if (len1 > len2)
15627 return 1;
15628 else if (len1 < len2)
15629 return -1;
15632 return winner;
15635 /* Return the template arguments that will produce the function signature
15636 DECL from the function template FN, with the explicit template
15637 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15638 also match. Return NULL_TREE if no satisfactory arguments could be
15639 found. */
15641 static tree
15642 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15644 int ntparms = DECL_NTPARMS (fn);
15645 tree targs = make_tree_vec (ntparms);
15646 tree decl_type;
15647 tree decl_arg_types;
15648 tree *args;
15649 unsigned int nargs, ix;
15650 tree arg;
15652 /* Substitute the explicit template arguments into the type of DECL.
15653 The call to fn_type_unification will handle substitution into the
15654 FN. */
15655 decl_type = TREE_TYPE (decl);
15656 if (explicit_args && uses_template_parms (decl_type))
15658 tree tmpl;
15659 tree converted_args;
15661 if (DECL_TEMPLATE_INFO (decl))
15662 tmpl = DECL_TI_TEMPLATE (decl);
15663 else
15664 /* We can get here for some invalid specializations. */
15665 return NULL_TREE;
15667 converted_args
15668 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15669 explicit_args, NULL_TREE,
15670 tf_none,
15671 /*require_all_args=*/false,
15672 /*use_default_args=*/false);
15673 if (converted_args == error_mark_node)
15674 return NULL_TREE;
15676 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15677 if (decl_type == error_mark_node)
15678 return NULL_TREE;
15681 /* Never do unification on the 'this' parameter. */
15682 decl_arg_types = skip_artificial_parms_for (decl,
15683 TYPE_ARG_TYPES (decl_type));
15685 nargs = list_length (decl_arg_types);
15686 args = XALLOCAVEC (tree, nargs);
15687 for (arg = decl_arg_types, ix = 0;
15688 arg != NULL_TREE && arg != void_list_node;
15689 arg = TREE_CHAIN (arg), ++ix)
15690 args[ix] = TREE_VALUE (arg);
15692 if (fn_type_unification (fn, explicit_args, targs,
15693 args, ix,
15694 (check_rettype || DECL_CONV_FN_P (fn)
15695 ? TREE_TYPE (decl_type) : NULL_TREE),
15696 DEDUCE_EXACT, LOOKUP_NORMAL))
15697 return NULL_TREE;
15699 return targs;
15702 /* Return the innermost template arguments that, when applied to a
15703 template specialization whose innermost template parameters are
15704 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15705 ARGS.
15707 For example, suppose we have:
15709 template <class T, class U> struct S {};
15710 template <class T> struct S<T*, int> {};
15712 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15713 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15714 int}. The resulting vector will be {double}, indicating that `T'
15715 is bound to `double'. */
15717 static tree
15718 get_class_bindings (tree tparms, tree spec_args, tree args)
15720 int i, ntparms = TREE_VEC_LENGTH (tparms);
15721 tree deduced_args;
15722 tree innermost_deduced_args;
15724 innermost_deduced_args = make_tree_vec (ntparms);
15725 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15727 deduced_args = copy_node (args);
15728 SET_TMPL_ARGS_LEVEL (deduced_args,
15729 TMPL_ARGS_DEPTH (deduced_args),
15730 innermost_deduced_args);
15732 else
15733 deduced_args = innermost_deduced_args;
15735 if (unify (tparms, deduced_args,
15736 INNERMOST_TEMPLATE_ARGS (spec_args),
15737 INNERMOST_TEMPLATE_ARGS (args),
15738 UNIFY_ALLOW_NONE))
15739 return NULL_TREE;
15741 for (i = 0; i < ntparms; ++i)
15742 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15743 return NULL_TREE;
15745 /* Verify that nondeduced template arguments agree with the type
15746 obtained from argument deduction.
15748 For example:
15750 struct A { typedef int X; };
15751 template <class T, class U> struct C {};
15752 template <class T> struct C<T, typename T::X> {};
15754 Then with the instantiation `C<A, int>', we can deduce that
15755 `T' is `A' but unify () does not check whether `typename T::X'
15756 is `int'. */
15757 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15758 if (spec_args == error_mark_node
15759 /* We only need to check the innermost arguments; the other
15760 arguments will always agree. */
15761 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15762 INNERMOST_TEMPLATE_ARGS (args)))
15763 return NULL_TREE;
15765 /* Now that we have bindings for all of the template arguments,
15766 ensure that the arguments deduced for the template template
15767 parameters have compatible template parameter lists. See the use
15768 of template_template_parm_bindings_ok_p in fn_type_unification
15769 for more information. */
15770 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15771 return NULL_TREE;
15773 return deduced_args;
15776 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15777 Return the TREE_LIST node with the most specialized template, if
15778 any. If there is no most specialized template, the error_mark_node
15779 is returned.
15781 Note that this function does not look at, or modify, the
15782 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15783 returned is one of the elements of INSTANTIATIONS, callers may
15784 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15785 and retrieve it from the value returned. */
15787 tree
15788 most_specialized_instantiation (tree templates)
15790 tree fn, champ;
15792 ++processing_template_decl;
15794 champ = templates;
15795 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15797 int fate = 0;
15799 if (get_bindings (TREE_VALUE (champ),
15800 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15801 NULL_TREE, /*check_ret=*/false))
15802 fate--;
15804 if (get_bindings (TREE_VALUE (fn),
15805 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15806 NULL_TREE, /*check_ret=*/false))
15807 fate++;
15809 if (fate == -1)
15810 champ = fn;
15811 else if (!fate)
15813 /* Equally specialized, move to next function. If there
15814 is no next function, nothing's most specialized. */
15815 fn = TREE_CHAIN (fn);
15816 champ = fn;
15817 if (!fn)
15818 break;
15822 if (champ)
15823 /* Now verify that champ is better than everything earlier in the
15824 instantiation list. */
15825 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15826 if (get_bindings (TREE_VALUE (champ),
15827 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15828 NULL_TREE, /*check_ret=*/false)
15829 || !get_bindings (TREE_VALUE (fn),
15830 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15831 NULL_TREE, /*check_ret=*/false))
15833 champ = NULL_TREE;
15834 break;
15837 processing_template_decl--;
15839 if (!champ)
15840 return error_mark_node;
15842 return champ;
15845 /* If DECL is a specialization of some template, return the most
15846 general such template. Otherwise, returns NULL_TREE.
15848 For example, given:
15850 template <class T> struct S { template <class U> void f(U); };
15852 if TMPL is `template <class U> void S<int>::f(U)' this will return
15853 the full template. This function will not trace past partial
15854 specializations, however. For example, given in addition:
15856 template <class T> struct S<T*> { template <class U> void f(U); };
15858 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15859 `template <class T> template <class U> S<T*>::f(U)'. */
15861 tree
15862 most_general_template (tree decl)
15864 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15865 an immediate specialization. */
15866 if (TREE_CODE (decl) == FUNCTION_DECL)
15868 if (DECL_TEMPLATE_INFO (decl)) {
15869 decl = DECL_TI_TEMPLATE (decl);
15871 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15872 template friend. */
15873 if (TREE_CODE (decl) != TEMPLATE_DECL)
15874 return NULL_TREE;
15875 } else
15876 return NULL_TREE;
15879 /* Look for more and more general templates. */
15880 while (DECL_TEMPLATE_INFO (decl))
15882 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15883 (See cp-tree.h for details.) */
15884 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15885 break;
15887 if (CLASS_TYPE_P (TREE_TYPE (decl))
15888 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15889 break;
15891 /* Stop if we run into an explicitly specialized class template. */
15892 if (!DECL_NAMESPACE_SCOPE_P (decl)
15893 && DECL_CONTEXT (decl)
15894 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15895 break;
15897 decl = DECL_TI_TEMPLATE (decl);
15900 return decl;
15903 /* Return the most specialized of the class template partial
15904 specializations of TMPL which can produce TYPE, a specialization of
15905 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15906 a _TYPE node corresponding to the partial specialization, while the
15907 TREE_PURPOSE is the set of template arguments that must be
15908 substituted into the TREE_TYPE in order to generate TYPE.
15910 If the choice of partial specialization is ambiguous, a diagnostic
15911 is issued, and the error_mark_node is returned. If there are no
15912 partial specializations of TMPL matching TYPE, then NULL_TREE is
15913 returned. */
15915 static tree
15916 most_specialized_class (tree type, tree tmpl)
15918 tree list = NULL_TREE;
15919 tree t;
15920 tree champ;
15921 int fate;
15922 bool ambiguous_p;
15923 tree args;
15924 tree outer_args = NULL_TREE;
15926 tmpl = most_general_template (tmpl);
15927 args = CLASSTYPE_TI_ARGS (type);
15929 /* For determining which partial specialization to use, only the
15930 innermost args are interesting. */
15931 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15933 outer_args = strip_innermost_template_args (args, 1);
15934 args = INNERMOST_TEMPLATE_ARGS (args);
15937 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15939 tree partial_spec_args;
15940 tree spec_args;
15941 tree parms = TREE_VALUE (t);
15943 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15944 if (outer_args)
15946 int i;
15948 ++processing_template_decl;
15950 /* Discard the outer levels of args, and then substitute in the
15951 template args from the enclosing class. */
15952 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15953 partial_spec_args = tsubst_template_args
15954 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15956 /* PARMS already refers to just the innermost parms, but the
15957 template parms in partial_spec_args had their levels lowered
15958 by tsubst, so we need to do the same for the parm list. We
15959 can't just tsubst the TREE_VEC itself, as tsubst wants to
15960 treat a TREE_VEC as an argument vector. */
15961 parms = copy_node (parms);
15962 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15963 TREE_VEC_ELT (parms, i) =
15964 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15966 --processing_template_decl;
15969 partial_spec_args =
15970 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15971 add_to_template_args (outer_args,
15972 partial_spec_args),
15973 tmpl, tf_none,
15974 /*require_all_args=*/true,
15975 /*use_default_args=*/true);
15977 if (partial_spec_args == error_mark_node)
15978 return error_mark_node;
15980 spec_args = get_class_bindings (parms,
15981 partial_spec_args,
15982 args);
15983 if (spec_args)
15985 if (outer_args)
15986 spec_args = add_to_template_args (outer_args, spec_args);
15987 list = tree_cons (spec_args, TREE_VALUE (t), list);
15988 TREE_TYPE (list) = TREE_TYPE (t);
15992 if (! list)
15993 return NULL_TREE;
15995 ambiguous_p = false;
15996 t = list;
15997 champ = t;
15998 t = TREE_CHAIN (t);
15999 for (; t; t = TREE_CHAIN (t))
16001 fate = more_specialized_class (champ, t);
16002 if (fate == 1)
16004 else
16006 if (fate == 0)
16008 t = TREE_CHAIN (t);
16009 if (! t)
16011 ambiguous_p = true;
16012 break;
16015 champ = t;
16019 if (!ambiguous_p)
16020 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16022 fate = more_specialized_class (champ, t);
16023 if (fate != 1)
16025 ambiguous_p = true;
16026 break;
16030 if (ambiguous_p)
16032 const char *str;
16033 char *spaces = NULL;
16034 error ("ambiguous class template instantiation for %q#T", type);
16035 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
16036 for (t = list; t; t = TREE_CHAIN (t))
16038 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16039 spaces = spaces ? spaces : get_spaces (str);
16041 free (spaces);
16042 return error_mark_node;
16045 return champ;
16048 /* Explicitly instantiate DECL. */
16050 void
16051 do_decl_instantiation (tree decl, tree storage)
16053 tree result = NULL_TREE;
16054 int extern_p = 0;
16056 if (!decl || decl == error_mark_node)
16057 /* An error occurred, for which grokdeclarator has already issued
16058 an appropriate message. */
16059 return;
16060 else if (! DECL_LANG_SPECIFIC (decl))
16062 error ("explicit instantiation of non-template %q#D", decl);
16063 return;
16065 else if (TREE_CODE (decl) == VAR_DECL)
16067 /* There is an asymmetry here in the way VAR_DECLs and
16068 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16069 the latter, the DECL we get back will be marked as a
16070 template instantiation, and the appropriate
16071 DECL_TEMPLATE_INFO will be set up. This does not happen for
16072 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16073 should handle VAR_DECLs as it currently handles
16074 FUNCTION_DECLs. */
16075 if (!DECL_CLASS_SCOPE_P (decl))
16077 error ("%qD is not a static data member of a class template", decl);
16078 return;
16080 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16081 if (!result || TREE_CODE (result) != VAR_DECL)
16083 error ("no matching template for %qD found", decl);
16084 return;
16086 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16088 error ("type %qT for explicit instantiation %qD does not match "
16089 "declared type %qT", TREE_TYPE (result), decl,
16090 TREE_TYPE (decl));
16091 return;
16094 else if (TREE_CODE (decl) != FUNCTION_DECL)
16096 error ("explicit instantiation of %q#D", decl);
16097 return;
16099 else
16100 result = decl;
16102 /* Check for various error cases. Note that if the explicit
16103 instantiation is valid the RESULT will currently be marked as an
16104 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16105 until we get here. */
16107 if (DECL_TEMPLATE_SPECIALIZATION (result))
16109 /* DR 259 [temp.spec].
16111 Both an explicit instantiation and a declaration of an explicit
16112 specialization shall not appear in a program unless the explicit
16113 instantiation follows a declaration of the explicit specialization.
16115 For a given set of template parameters, if an explicit
16116 instantiation of a template appears after a declaration of an
16117 explicit specialization for that template, the explicit
16118 instantiation has no effect. */
16119 return;
16121 else if (DECL_EXPLICIT_INSTANTIATION (result))
16123 /* [temp.spec]
16125 No program shall explicitly instantiate any template more
16126 than once.
16128 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16129 the first instantiation was `extern' and the second is not,
16130 and EXTERN_P for the opposite case. */
16131 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16132 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16133 /* If an "extern" explicit instantiation follows an ordinary
16134 explicit instantiation, the template is instantiated. */
16135 if (extern_p)
16136 return;
16138 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16140 error ("no matching template for %qD found", result);
16141 return;
16143 else if (!DECL_TEMPLATE_INFO (result))
16145 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16146 return;
16149 if (storage == NULL_TREE)
16151 else if (storage == ridpointers[(int) RID_EXTERN])
16153 if (!in_system_header && (cxx_dialect == cxx98))
16154 pedwarn (input_location, OPT_pedantic,
16155 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16156 "instantiations");
16157 extern_p = 1;
16159 else
16160 error ("storage class %qD applied to template instantiation", storage);
16162 check_explicit_instantiation_namespace (result);
16163 mark_decl_instantiated (result, extern_p);
16164 if (! extern_p)
16165 instantiate_decl (result, /*defer_ok=*/1,
16166 /*expl_inst_class_mem_p=*/false);
16169 static void
16170 mark_class_instantiated (tree t, int extern_p)
16172 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16173 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16174 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
16175 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
16176 if (! extern_p)
16178 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
16179 rest_of_type_compilation (t, 1);
16183 /* Called from do_type_instantiation through binding_table_foreach to
16184 do recursive instantiation for the type bound in ENTRY. */
16185 static void
16186 bt_instantiate_type_proc (binding_entry entry, void *data)
16188 tree storage = *(tree *) data;
16190 if (MAYBE_CLASS_TYPE_P (entry->type)
16191 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
16192 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
16195 /* Called from do_type_instantiation to instantiate a member
16196 (a member function or a static member variable) of an
16197 explicitly instantiated class template. */
16198 static void
16199 instantiate_class_member (tree decl, int extern_p)
16201 mark_decl_instantiated (decl, extern_p);
16202 if (! extern_p)
16203 instantiate_decl (decl, /*defer_ok=*/1,
16204 /*expl_inst_class_mem_p=*/true);
16207 /* Perform an explicit instantiation of template class T. STORAGE, if
16208 non-null, is the RID for extern, inline or static. COMPLAIN is
16209 nonzero if this is called from the parser, zero if called recursively,
16210 since the standard is unclear (as detailed below). */
16212 void
16213 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
16215 int extern_p = 0;
16216 int nomem_p = 0;
16217 int static_p = 0;
16218 int previous_instantiation_extern_p = 0;
16220 if (TREE_CODE (t) == TYPE_DECL)
16221 t = TREE_TYPE (t);
16223 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
16225 error ("explicit instantiation of non-template type %qT", t);
16226 return;
16229 complete_type (t);
16231 if (!COMPLETE_TYPE_P (t))
16233 if (complain & tf_error)
16234 error ("explicit instantiation of %q#T before definition of template",
16236 return;
16239 if (storage != NULL_TREE)
16241 if (!in_system_header)
16243 if (storage == ridpointers[(int) RID_EXTERN])
16245 if (cxx_dialect == cxx98)
16246 pedwarn (input_location, OPT_pedantic,
16247 "ISO C++ 1998 forbids the use of %<extern%> on "
16248 "explicit instantiations");
16250 else
16251 pedwarn (input_location, OPT_pedantic,
16252 "ISO C++ forbids the use of %qE"
16253 " on explicit instantiations", storage);
16256 if (storage == ridpointers[(int) RID_INLINE])
16257 nomem_p = 1;
16258 else if (storage == ridpointers[(int) RID_EXTERN])
16259 extern_p = 1;
16260 else if (storage == ridpointers[(int) RID_STATIC])
16261 static_p = 1;
16262 else
16264 error ("storage class %qD applied to template instantiation",
16265 storage);
16266 extern_p = 0;
16270 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16272 /* DR 259 [temp.spec].
16274 Both an explicit instantiation and a declaration of an explicit
16275 specialization shall not appear in a program unless the explicit
16276 instantiation follows a declaration of the explicit specialization.
16278 For a given set of template parameters, if an explicit
16279 instantiation of a template appears after a declaration of an
16280 explicit specialization for that template, the explicit
16281 instantiation has no effect. */
16282 return;
16284 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16286 /* [temp.spec]
16288 No program shall explicitly instantiate any template more
16289 than once.
16291 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16292 instantiation was `extern'. If EXTERN_P then the second is.
16293 These cases are OK. */
16294 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16296 if (!previous_instantiation_extern_p && !extern_p
16297 && (complain & tf_error))
16298 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16300 /* If we've already instantiated the template, just return now. */
16301 if (!CLASSTYPE_INTERFACE_ONLY (t))
16302 return;
16305 check_explicit_instantiation_namespace (TYPE_NAME (t));
16306 mark_class_instantiated (t, extern_p);
16308 if (nomem_p)
16309 return;
16312 tree tmp;
16314 /* In contrast to implicit instantiation, where only the
16315 declarations, and not the definitions, of members are
16316 instantiated, we have here:
16318 [temp.explicit]
16320 The explicit instantiation of a class template specialization
16321 implies the instantiation of all of its members not
16322 previously explicitly specialized in the translation unit
16323 containing the explicit instantiation.
16325 Of course, we can't instantiate member template classes, since
16326 we don't have any arguments for them. Note that the standard
16327 is unclear on whether the instantiation of the members are
16328 *explicit* instantiations or not. However, the most natural
16329 interpretation is that it should be an explicit instantiation. */
16331 if (! static_p)
16332 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
16333 if (TREE_CODE (tmp) == FUNCTION_DECL
16334 && DECL_TEMPLATE_INSTANTIATION (tmp))
16335 instantiate_class_member (tmp, extern_p);
16337 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
16338 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16339 instantiate_class_member (tmp, extern_p);
16341 if (CLASSTYPE_NESTED_UTDS (t))
16342 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16343 bt_instantiate_type_proc, &storage);
16347 /* Given a function DECL, which is a specialization of TMPL, modify
16348 DECL to be a re-instantiation of TMPL with the same template
16349 arguments. TMPL should be the template into which tsubst'ing
16350 should occur for DECL, not the most general template.
16352 One reason for doing this is a scenario like this:
16354 template <class T>
16355 void f(const T&, int i);
16357 void g() { f(3, 7); }
16359 template <class T>
16360 void f(const T& t, const int i) { }
16362 Note that when the template is first instantiated, with
16363 instantiate_template, the resulting DECL will have no name for the
16364 first parameter, and the wrong type for the second. So, when we go
16365 to instantiate the DECL, we regenerate it. */
16367 static void
16368 regenerate_decl_from_template (tree decl, tree tmpl)
16370 /* The arguments used to instantiate DECL, from the most general
16371 template. */
16372 tree args;
16373 tree code_pattern;
16375 args = DECL_TI_ARGS (decl);
16376 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16378 /* Make sure that we can see identifiers, and compute access
16379 correctly. */
16380 push_access_scope (decl);
16382 if (TREE_CODE (decl) == FUNCTION_DECL)
16384 tree decl_parm;
16385 tree pattern_parm;
16386 tree specs;
16387 int args_depth;
16388 int parms_depth;
16390 args_depth = TMPL_ARGS_DEPTH (args);
16391 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16392 if (args_depth > parms_depth)
16393 args = get_innermost_template_args (args, parms_depth);
16395 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16396 args, tf_error, NULL_TREE);
16397 if (specs)
16398 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16399 specs);
16401 /* Merge parameter declarations. */
16402 decl_parm = skip_artificial_parms_for (decl,
16403 DECL_ARGUMENTS (decl));
16404 pattern_parm
16405 = skip_artificial_parms_for (code_pattern,
16406 DECL_ARGUMENTS (code_pattern));
16407 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16409 tree parm_type;
16410 tree attributes;
16412 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16413 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16414 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16415 NULL_TREE);
16416 parm_type = type_decays_to (parm_type);
16417 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16418 TREE_TYPE (decl_parm) = parm_type;
16419 attributes = DECL_ATTRIBUTES (pattern_parm);
16420 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16422 DECL_ATTRIBUTES (decl_parm) = attributes;
16423 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16425 decl_parm = TREE_CHAIN (decl_parm);
16426 pattern_parm = TREE_CHAIN (pattern_parm);
16428 /* Merge any parameters that match with the function parameter
16429 pack. */
16430 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16432 int i, len;
16433 tree expanded_types;
16434 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16435 the parameters in this function parameter pack. */
16436 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16437 args, tf_error, NULL_TREE);
16438 len = TREE_VEC_LENGTH (expanded_types);
16439 for (i = 0; i < len; i++)
16441 tree parm_type;
16442 tree attributes;
16444 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16445 /* Rename the parameter to include the index. */
16446 DECL_NAME (decl_parm) =
16447 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16448 parm_type = TREE_VEC_ELT (expanded_types, i);
16449 parm_type = type_decays_to (parm_type);
16450 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16451 TREE_TYPE (decl_parm) = parm_type;
16452 attributes = DECL_ATTRIBUTES (pattern_parm);
16453 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16455 DECL_ATTRIBUTES (decl_parm) = attributes;
16456 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16458 decl_parm = TREE_CHAIN (decl_parm);
16461 /* Merge additional specifiers from the CODE_PATTERN. */
16462 if (DECL_DECLARED_INLINE_P (code_pattern)
16463 && !DECL_DECLARED_INLINE_P (decl))
16464 DECL_DECLARED_INLINE_P (decl) = 1;
16466 else if (TREE_CODE (decl) == VAR_DECL)
16468 DECL_INITIAL (decl) =
16469 tsubst_expr (DECL_INITIAL (code_pattern), args,
16470 tf_error, DECL_TI_TEMPLATE (decl),
16471 /*integral_constant_expression_p=*/false);
16472 if (VAR_HAD_UNKNOWN_BOUND (decl))
16473 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
16474 tf_error, DECL_TI_TEMPLATE (decl));
16476 else
16477 gcc_unreachable ();
16479 pop_access_scope (decl);
16482 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16483 substituted to get DECL. */
16485 tree
16486 template_for_substitution (tree decl)
16488 tree tmpl = DECL_TI_TEMPLATE (decl);
16490 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16491 for the instantiation. This is not always the most general
16492 template. Consider, for example:
16494 template <class T>
16495 struct S { template <class U> void f();
16496 template <> void f<int>(); };
16498 and an instantiation of S<double>::f<int>. We want TD to be the
16499 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16500 while (/* An instantiation cannot have a definition, so we need a
16501 more general template. */
16502 DECL_TEMPLATE_INSTANTIATION (tmpl)
16503 /* We must also deal with friend templates. Given:
16505 template <class T> struct S {
16506 template <class U> friend void f() {};
16509 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16510 so far as the language is concerned, but that's still
16511 where we get the pattern for the instantiation from. On
16512 other hand, if the definition comes outside the class, say:
16514 template <class T> struct S {
16515 template <class U> friend void f();
16517 template <class U> friend void f() {}
16519 we don't need to look any further. That's what the check for
16520 DECL_INITIAL is for. */
16521 || (TREE_CODE (decl) == FUNCTION_DECL
16522 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16523 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16525 /* The present template, TD, should not be a definition. If it
16526 were a definition, we should be using it! Note that we
16527 cannot restructure the loop to just keep going until we find
16528 a template with a definition, since that might go too far if
16529 a specialization was declared, but not defined. */
16530 gcc_assert (TREE_CODE (decl) != VAR_DECL
16531 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16533 /* Fetch the more general template. */
16534 tmpl = DECL_TI_TEMPLATE (tmpl);
16537 return tmpl;
16540 /* Returns true if we need to instantiate this template instance even if we
16541 know we aren't going to emit it.. */
16543 bool
16544 always_instantiate_p (tree decl)
16546 /* We always instantiate inline functions so that we can inline them. An
16547 explicit instantiation declaration prohibits implicit instantiation of
16548 non-inline functions. With high levels of optimization, we would
16549 normally inline non-inline functions -- but we're not allowed to do
16550 that for "extern template" functions. Therefore, we check
16551 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16552 return ((TREE_CODE (decl) == FUNCTION_DECL
16553 && DECL_DECLARED_INLINE_P (decl))
16554 /* And we need to instantiate static data members so that
16555 their initializers are available in integral constant
16556 expressions. */
16557 || (TREE_CODE (decl) == VAR_DECL
16558 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16561 /* Produce the definition of D, a _DECL generated from a template. If
16562 DEFER_OK is nonzero, then we don't have to actually do the
16563 instantiation now; we just have to do it sometime. Normally it is
16564 an error if this is an explicit instantiation but D is undefined.
16565 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16566 explicitly instantiated class template. */
16568 tree
16569 instantiate_decl (tree d, int defer_ok,
16570 bool expl_inst_class_mem_p)
16572 tree tmpl = DECL_TI_TEMPLATE (d);
16573 tree gen_args;
16574 tree args;
16575 tree td;
16576 tree code_pattern;
16577 tree spec;
16578 tree gen_tmpl;
16579 bool pattern_defined;
16580 int need_push;
16581 location_t saved_loc = input_location;
16582 bool external_p;
16584 /* This function should only be used to instantiate templates for
16585 functions and static member variables. */
16586 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16587 || TREE_CODE (d) == VAR_DECL);
16589 /* Variables are never deferred; if instantiation is required, they
16590 are instantiated right away. That allows for better code in the
16591 case that an expression refers to the value of the variable --
16592 if the variable has a constant value the referring expression can
16593 take advantage of that fact. */
16594 if (TREE_CODE (d) == VAR_DECL)
16595 defer_ok = 0;
16597 /* Don't instantiate cloned functions. Instead, instantiate the
16598 functions they cloned. */
16599 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16600 d = DECL_CLONED_FUNCTION (d);
16602 if (DECL_TEMPLATE_INSTANTIATED (d)
16603 || DECL_TEMPLATE_SPECIALIZATION (d))
16604 /* D has already been instantiated or explicitly specialized, so
16605 there's nothing for us to do here.
16607 It might seem reasonable to check whether or not D is an explicit
16608 instantiation, and, if so, stop here. But when an explicit
16609 instantiation is deferred until the end of the compilation,
16610 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16611 the instantiation. */
16612 return d;
16614 /* Check to see whether we know that this template will be
16615 instantiated in some other file, as with "extern template"
16616 extension. */
16617 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16619 /* In general, we do not instantiate such templates. */
16620 if (external_p && !always_instantiate_p (d))
16621 return d;
16623 gen_tmpl = most_general_template (tmpl);
16624 gen_args = DECL_TI_ARGS (d);
16626 if (tmpl != gen_tmpl)
16627 /* We should already have the extra args. */
16628 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16629 == TMPL_ARGS_DEPTH (gen_args));
16630 /* And what's in the hash table should match D. */
16631 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16632 || spec == NULL_TREE);
16634 /* This needs to happen before any tsubsting. */
16635 if (! push_tinst_level (d))
16636 return d;
16638 timevar_push (TV_PARSE);
16640 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16641 for the instantiation. */
16642 td = template_for_substitution (d);
16643 code_pattern = DECL_TEMPLATE_RESULT (td);
16645 /* We should never be trying to instantiate a member of a class
16646 template or partial specialization. */
16647 gcc_assert (d != code_pattern);
16649 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16650 || DECL_TEMPLATE_SPECIALIZATION (td))
16651 /* In the case of a friend template whose definition is provided
16652 outside the class, we may have too many arguments. Drop the
16653 ones we don't need. The same is true for specializations. */
16654 args = get_innermost_template_args
16655 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16656 else
16657 args = gen_args;
16659 if (TREE_CODE (d) == FUNCTION_DECL)
16660 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16661 else
16662 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16664 /* We may be in the middle of deferred access check. Disable it now. */
16665 push_deferring_access_checks (dk_no_deferred);
16667 /* Unless an explicit instantiation directive has already determined
16668 the linkage of D, remember that a definition is available for
16669 this entity. */
16670 if (pattern_defined
16671 && !DECL_INTERFACE_KNOWN (d)
16672 && !DECL_NOT_REALLY_EXTERN (d))
16673 mark_definable (d);
16675 input_location = DECL_SOURCE_LOCATION (d);
16677 /* If D is a member of an explicitly instantiated class template,
16678 and no definition is available, treat it like an implicit
16679 instantiation. */
16680 if (!pattern_defined && expl_inst_class_mem_p
16681 && DECL_EXPLICIT_INSTANTIATION (d))
16683 DECL_NOT_REALLY_EXTERN (d) = 0;
16684 DECL_INTERFACE_KNOWN (d) = 0;
16685 SET_DECL_IMPLICIT_INSTANTIATION (d);
16688 /* Recheck the substitutions to obtain any warning messages
16689 about ignoring cv qualifiers. Don't do this for artificial decls,
16690 as it breaks the context-sensitive substitution for lambda op(). */
16691 if (!defer_ok && !DECL_ARTIFICIAL (d))
16693 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16694 tree type = TREE_TYPE (gen);
16696 /* Make sure that we can see identifiers, and compute access
16697 correctly. D is already the target FUNCTION_DECL with the
16698 right context. */
16699 push_access_scope (d);
16701 if (TREE_CODE (gen) == FUNCTION_DECL)
16703 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16704 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16706 /* Don't simply tsubst the function type, as that will give
16707 duplicate warnings about poor parameter qualifications.
16708 The function arguments are the same as the decl_arguments
16709 without the top level cv qualifiers. */
16710 type = TREE_TYPE (type);
16712 tsubst (type, gen_args, tf_warning_or_error, d);
16714 pop_access_scope (d);
16717 /* Defer all other templates, unless we have been explicitly
16718 forbidden from doing so. */
16719 if (/* If there is no definition, we cannot instantiate the
16720 template. */
16721 ! pattern_defined
16722 /* If it's OK to postpone instantiation, do so. */
16723 || defer_ok
16724 /* If this is a static data member that will be defined
16725 elsewhere, we don't want to instantiate the entire data
16726 member, but we do want to instantiate the initializer so that
16727 we can substitute that elsewhere. */
16728 || (external_p && TREE_CODE (d) == VAR_DECL))
16730 /* The definition of the static data member is now required so
16731 we must substitute the initializer. */
16732 if (TREE_CODE (d) == VAR_DECL
16733 && !DECL_INITIAL (d)
16734 && DECL_INITIAL (code_pattern))
16736 tree ns;
16737 tree init;
16739 ns = decl_namespace_context (d);
16740 push_nested_namespace (ns);
16741 push_nested_class (DECL_CONTEXT (d));
16742 init = tsubst_expr (DECL_INITIAL (code_pattern),
16743 args,
16744 tf_warning_or_error, NULL_TREE,
16745 /*integral_constant_expression_p=*/false);
16746 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16747 /*asmspec_tree=*/NULL_TREE,
16748 LOOKUP_ONLYCONVERTING);
16749 pop_nested_class ();
16750 pop_nested_namespace (ns);
16753 /* We restore the source position here because it's used by
16754 add_pending_template. */
16755 input_location = saved_loc;
16757 if (at_eof && !pattern_defined
16758 && DECL_EXPLICIT_INSTANTIATION (d)
16759 && DECL_NOT_REALLY_EXTERN (d))
16760 /* [temp.explicit]
16762 The definition of a non-exported function template, a
16763 non-exported member function template, or a non-exported
16764 member function or static data member of a class template
16765 shall be present in every translation unit in which it is
16766 explicitly instantiated. */
16767 permerror (input_location, "explicit instantiation of %qD "
16768 "but no definition available", d);
16770 /* ??? Historically, we have instantiated inline functions, even
16771 when marked as "extern template". */
16772 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16773 add_pending_template (d);
16774 goto out;
16776 /* Tell the repository that D is available in this translation unit
16777 -- and see if it is supposed to be instantiated here. */
16778 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16780 /* In a PCH file, despite the fact that the repository hasn't
16781 requested instantiation in the PCH it is still possible that
16782 an instantiation will be required in a file that includes the
16783 PCH. */
16784 if (pch_file)
16785 add_pending_template (d);
16786 /* Instantiate inline functions so that the inliner can do its
16787 job, even though we'll not be emitting a copy of this
16788 function. */
16789 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16790 goto out;
16793 need_push = !cfun || !global_bindings_p ();
16794 if (need_push)
16795 push_to_top_level ();
16797 /* Mark D as instantiated so that recursive calls to
16798 instantiate_decl do not try to instantiate it again. */
16799 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16801 /* Regenerate the declaration in case the template has been modified
16802 by a subsequent redeclaration. */
16803 regenerate_decl_from_template (d, td);
16805 /* We already set the file and line above. Reset them now in case
16806 they changed as a result of calling regenerate_decl_from_template. */
16807 input_location = DECL_SOURCE_LOCATION (d);
16809 if (TREE_CODE (d) == VAR_DECL)
16811 tree init;
16813 /* Clear out DECL_RTL; whatever was there before may not be right
16814 since we've reset the type of the declaration. */
16815 SET_DECL_RTL (d, NULL_RTX);
16816 DECL_IN_AGGR_P (d) = 0;
16818 /* The initializer is placed in DECL_INITIAL by
16819 regenerate_decl_from_template. Pull it out so that
16820 cp_finish_decl can process it. */
16821 init = DECL_INITIAL (d);
16822 DECL_INITIAL (d) = NULL_TREE;
16823 DECL_INITIALIZED_P (d) = 0;
16825 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16826 initializer. That function will defer actual emission until
16827 we have a chance to determine linkage. */
16828 DECL_EXTERNAL (d) = 0;
16830 /* Enter the scope of D so that access-checking works correctly. */
16831 push_nested_class (DECL_CONTEXT (d));
16832 cp_finish_decl (d, init, false, NULL_TREE, 0);
16833 pop_nested_class ();
16835 else if (TREE_CODE (d) == FUNCTION_DECL)
16837 htab_t saved_local_specializations;
16838 tree subst_decl;
16839 tree tmpl_parm;
16840 tree spec_parm;
16842 /* Save away the current list, in case we are instantiating one
16843 template from within the body of another. */
16844 saved_local_specializations = local_specializations;
16846 /* Set up the list of local specializations. */
16847 local_specializations = htab_create (37,
16848 hash_local_specialization,
16849 eq_local_specializations,
16850 NULL);
16852 /* Set up context. */
16853 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16855 /* Create substitution entries for the parameters. */
16856 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16857 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16858 spec_parm = DECL_ARGUMENTS (d);
16859 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16861 register_local_specialization (spec_parm, tmpl_parm);
16862 spec_parm = skip_artificial_parms_for (d, spec_parm);
16863 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16865 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16867 register_local_specialization (spec_parm, tmpl_parm);
16868 tmpl_parm = TREE_CHAIN (tmpl_parm);
16869 spec_parm = TREE_CHAIN (spec_parm);
16871 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16873 /* Register the (value) argument pack as a specialization of
16874 TMPL_PARM, then move on. */
16875 tree argpack = make_fnparm_pack (spec_parm);
16876 register_local_specialization (argpack, tmpl_parm);
16877 tmpl_parm = TREE_CHAIN (tmpl_parm);
16878 spec_parm = NULL_TREE;
16880 gcc_assert (!spec_parm);
16882 /* Substitute into the body of the function. */
16883 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16884 tf_warning_or_error, tmpl,
16885 /*integral_constant_expression_p=*/false);
16887 /* Set the current input_location to the end of the function
16888 so that finish_function knows where we are. */
16889 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16891 /* We don't need the local specializations any more. */
16892 htab_delete (local_specializations);
16893 local_specializations = saved_local_specializations;
16895 /* Finish the function. */
16896 d = finish_function (0);
16897 expand_or_defer_fn (d);
16900 /* We're not deferring instantiation any more. */
16901 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16903 if (need_push)
16904 pop_from_top_level ();
16906 out:
16907 input_location = saved_loc;
16908 pop_deferring_access_checks ();
16909 pop_tinst_level ();
16911 timevar_pop (TV_PARSE);
16913 return d;
16916 /* Run through the list of templates that we wish we could
16917 instantiate, and instantiate any we can. RETRIES is the
16918 number of times we retry pending template instantiation. */
16920 void
16921 instantiate_pending_templates (int retries)
16923 int reconsider;
16924 location_t saved_loc = input_location;
16926 /* Instantiating templates may trigger vtable generation. This in turn
16927 may require further template instantiations. We place a limit here
16928 to avoid infinite loop. */
16929 if (pending_templates && retries >= max_tinst_depth)
16931 tree decl = pending_templates->tinst->decl;
16933 error ("template instantiation depth exceeds maximum of %d"
16934 " instantiating %q+D, possibly from virtual table generation"
16935 " (use -ftemplate-depth= to increase the maximum)",
16936 max_tinst_depth, decl);
16937 if (TREE_CODE (decl) == FUNCTION_DECL)
16938 /* Pretend that we defined it. */
16939 DECL_INITIAL (decl) = error_mark_node;
16940 return;
16945 struct pending_template **t = &pending_templates;
16946 struct pending_template *last = NULL;
16947 reconsider = 0;
16948 while (*t)
16950 tree instantiation = reopen_tinst_level ((*t)->tinst);
16951 bool complete = false;
16953 if (TYPE_P (instantiation))
16955 tree fn;
16957 if (!COMPLETE_TYPE_P (instantiation))
16959 instantiate_class_template (instantiation);
16960 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16961 for (fn = TYPE_METHODS (instantiation);
16963 fn = TREE_CHAIN (fn))
16964 if (! DECL_ARTIFICIAL (fn))
16965 instantiate_decl (fn,
16966 /*defer_ok=*/0,
16967 /*expl_inst_class_mem_p=*/false);
16968 if (COMPLETE_TYPE_P (instantiation))
16969 reconsider = 1;
16972 complete = COMPLETE_TYPE_P (instantiation);
16974 else
16976 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16977 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16979 instantiation
16980 = instantiate_decl (instantiation,
16981 /*defer_ok=*/0,
16982 /*expl_inst_class_mem_p=*/false);
16983 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16984 reconsider = 1;
16987 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16988 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16991 if (complete)
16992 /* If INSTANTIATION has been instantiated, then we don't
16993 need to consider it again in the future. */
16994 *t = (*t)->next;
16995 else
16997 last = *t;
16998 t = &(*t)->next;
17000 tinst_depth = 0;
17001 current_tinst_level = NULL;
17003 last_pending_template = last;
17005 while (reconsider);
17007 input_location = saved_loc;
17010 /* Substitute ARGVEC into T, which is a list of initializers for
17011 either base class or a non-static data member. The TREE_PURPOSEs
17012 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17013 instantiate_decl. */
17015 static tree
17016 tsubst_initializer_list (tree t, tree argvec)
17018 tree inits = NULL_TREE;
17020 for (; t; t = TREE_CHAIN (t))
17022 tree decl;
17023 tree init;
17024 tree expanded_bases = NULL_TREE;
17025 tree expanded_arguments = NULL_TREE;
17026 int i, len = 1;
17028 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17030 tree expr;
17031 tree arg;
17033 /* Expand the base class expansion type into separate base
17034 classes. */
17035 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17036 tf_warning_or_error,
17037 NULL_TREE);
17038 if (expanded_bases == error_mark_node)
17039 continue;
17041 /* We'll be building separate TREE_LISTs of arguments for
17042 each base. */
17043 len = TREE_VEC_LENGTH (expanded_bases);
17044 expanded_arguments = make_tree_vec (len);
17045 for (i = 0; i < len; i++)
17046 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17048 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17049 expand each argument in the TREE_VALUE of t. */
17050 expr = make_node (EXPR_PACK_EXPANSION);
17051 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17052 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17054 if (TREE_VALUE (t) == void_type_node)
17055 /* VOID_TYPE_NODE is used to indicate
17056 value-initialization. */
17058 for (i = 0; i < len; i++)
17059 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17061 else
17063 /* Substitute parameter packs into each argument in the
17064 TREE_LIST. */
17065 in_base_initializer = 1;
17066 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17068 tree expanded_exprs;
17070 /* Expand the argument. */
17071 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17072 expanded_exprs
17073 = tsubst_pack_expansion (expr, argvec,
17074 tf_warning_or_error,
17075 NULL_TREE);
17076 if (expanded_exprs == error_mark_node)
17077 continue;
17079 /* Prepend each of the expanded expressions to the
17080 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17081 for (i = 0; i < len; i++)
17083 TREE_VEC_ELT (expanded_arguments, i) =
17084 tree_cons (NULL_TREE,
17085 TREE_VEC_ELT (expanded_exprs, i),
17086 TREE_VEC_ELT (expanded_arguments, i));
17089 in_base_initializer = 0;
17091 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17092 since we built them backwards. */
17093 for (i = 0; i < len; i++)
17095 TREE_VEC_ELT (expanded_arguments, i) =
17096 nreverse (TREE_VEC_ELT (expanded_arguments, i));
17101 for (i = 0; i < len; ++i)
17103 if (expanded_bases)
17105 decl = TREE_VEC_ELT (expanded_bases, i);
17106 decl = expand_member_init (decl);
17107 init = TREE_VEC_ELT (expanded_arguments, i);
17109 else
17111 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
17112 tf_warning_or_error, NULL_TREE);
17114 decl = expand_member_init (decl);
17115 if (decl && !DECL_P (decl))
17116 in_base_initializer = 1;
17118 init = tsubst_expr (TREE_VALUE (t), argvec,
17119 tf_warning_or_error, NULL_TREE,
17120 /*integral_constant_expression_p=*/false);
17121 in_base_initializer = 0;
17124 if (decl)
17126 init = build_tree_list (decl, init);
17127 TREE_CHAIN (init) = inits;
17128 inits = init;
17132 return inits;
17135 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17137 static void
17138 set_current_access_from_decl (tree decl)
17140 if (TREE_PRIVATE (decl))
17141 current_access_specifier = access_private_node;
17142 else if (TREE_PROTECTED (decl))
17143 current_access_specifier = access_protected_node;
17144 else
17145 current_access_specifier = access_public_node;
17148 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17149 is the instantiation (which should have been created with
17150 start_enum) and ARGS are the template arguments to use. */
17152 static void
17153 tsubst_enum (tree tag, tree newtag, tree args)
17155 tree e;
17157 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
17159 tree value;
17160 tree decl;
17162 decl = TREE_VALUE (e);
17163 /* Note that in a template enum, the TREE_VALUE is the
17164 CONST_DECL, not the corresponding INTEGER_CST. */
17165 value = tsubst_expr (DECL_INITIAL (decl),
17166 args, tf_warning_or_error, NULL_TREE,
17167 /*integral_constant_expression_p=*/true);
17169 /* Give this enumeration constant the correct access. */
17170 set_current_access_from_decl (decl);
17172 /* Actually build the enumerator itself. */
17173 build_enumerator (DECL_NAME (decl), value, newtag);
17176 finish_enum (newtag);
17177 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
17178 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
17181 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17182 its type -- but without substituting the innermost set of template
17183 arguments. So, innermost set of template parameters will appear in
17184 the type. */
17186 tree
17187 get_mostly_instantiated_function_type (tree decl)
17189 tree fn_type;
17190 tree tmpl;
17191 tree targs;
17192 tree tparms;
17193 int parm_depth;
17195 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
17196 targs = DECL_TI_ARGS (decl);
17197 tparms = DECL_TEMPLATE_PARMS (tmpl);
17198 parm_depth = TMPL_PARMS_DEPTH (tparms);
17200 /* There should be as many levels of arguments as there are levels
17201 of parameters. */
17202 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
17204 fn_type = TREE_TYPE (tmpl);
17206 if (parm_depth == 1)
17207 /* No substitution is necessary. */
17209 else
17211 int i, save_access_control;
17212 tree partial_args;
17214 /* Replace the innermost level of the TARGS with NULL_TREEs to
17215 let tsubst know not to substitute for those parameters. */
17216 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
17217 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
17218 SET_TMPL_ARGS_LEVEL (partial_args, i,
17219 TMPL_ARGS_LEVEL (targs, i));
17220 SET_TMPL_ARGS_LEVEL (partial_args,
17221 TMPL_ARGS_DEPTH (targs),
17222 make_tree_vec (DECL_NTPARMS (tmpl)));
17224 /* Disable access control as this function is used only during
17225 name-mangling. */
17226 save_access_control = flag_access_control;
17227 flag_access_control = 0;
17229 ++processing_template_decl;
17230 /* Now, do the (partial) substitution to figure out the
17231 appropriate function type. */
17232 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17233 --processing_template_decl;
17235 /* Substitute into the template parameters to obtain the real
17236 innermost set of parameters. This step is important if the
17237 innermost set of template parameters contains value
17238 parameters whose types depend on outer template parameters. */
17239 TREE_VEC_LENGTH (partial_args)--;
17240 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17242 flag_access_control = save_access_control;
17245 return fn_type;
17248 /* Return truthvalue if we're processing a template different from
17249 the last one involved in diagnostics. */
17251 problematic_instantiation_changed (void)
17253 return last_template_error_tick != tinst_level_tick;
17256 /* Remember current template involved in diagnostics. */
17257 void
17258 record_last_problematic_instantiation (void)
17260 last_template_error_tick = tinst_level_tick;
17263 struct tinst_level *
17264 current_instantiation (void)
17266 return current_tinst_level;
17269 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17270 type. Return zero for ok, nonzero for disallowed. Issue error and
17271 warning messages under control of COMPLAIN. */
17273 static int
17274 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17276 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17277 return 0;
17278 else if (POINTER_TYPE_P (type))
17279 return 0;
17280 else if (TYPE_PTR_TO_MEMBER_P (type))
17281 return 0;
17282 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17283 return 0;
17284 else if (TREE_CODE (type) == TYPENAME_TYPE)
17285 return 0;
17287 if (complain & tf_error)
17288 error ("%q#T is not a valid type for a template constant parameter", type);
17289 return 1;
17292 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17293 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17295 static bool
17296 dependent_type_p_r (tree type)
17298 tree scope;
17300 /* [temp.dep.type]
17302 A type is dependent if it is:
17304 -- a template parameter. Template template parameters are types
17305 for us (since TYPE_P holds true for them) so we handle
17306 them here. */
17307 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17308 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17309 return true;
17310 /* -- a qualified-id with a nested-name-specifier which contains a
17311 class-name that names a dependent type or whose unqualified-id
17312 names a dependent type. */
17313 if (TREE_CODE (type) == TYPENAME_TYPE)
17314 return true;
17315 /* -- a cv-qualified type where the cv-unqualified type is
17316 dependent. */
17317 type = TYPE_MAIN_VARIANT (type);
17318 /* -- a compound type constructed from any dependent type. */
17319 if (TYPE_PTR_TO_MEMBER_P (type))
17320 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17321 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17322 (type)));
17323 else if (TREE_CODE (type) == POINTER_TYPE
17324 || TREE_CODE (type) == REFERENCE_TYPE)
17325 return dependent_type_p (TREE_TYPE (type));
17326 else if (TREE_CODE (type) == FUNCTION_TYPE
17327 || TREE_CODE (type) == METHOD_TYPE)
17329 tree arg_type;
17331 if (dependent_type_p (TREE_TYPE (type)))
17332 return true;
17333 for (arg_type = TYPE_ARG_TYPES (type);
17334 arg_type;
17335 arg_type = TREE_CHAIN (arg_type))
17336 if (dependent_type_p (TREE_VALUE (arg_type)))
17337 return true;
17338 return false;
17340 /* -- an array type constructed from any dependent type or whose
17341 size is specified by a constant expression that is
17342 value-dependent. */
17343 if (TREE_CODE (type) == ARRAY_TYPE)
17345 if (TYPE_DOMAIN (type)
17346 && dependent_type_p (TYPE_DOMAIN (type)))
17347 return true;
17348 return dependent_type_p (TREE_TYPE (type));
17350 else if (TREE_CODE (type) == INTEGER_TYPE
17351 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
17353 /* If this is the TYPE_DOMAIN of an array type, consider it
17354 dependent. We already checked for value-dependence in
17355 compute_array_index_type. */
17356 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
17359 /* -- a template-id in which either the template name is a template
17360 parameter ... */
17361 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17362 return true;
17363 /* ... or any of the template arguments is a dependent type or
17364 an expression that is type-dependent or value-dependent. */
17365 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17366 && (any_dependent_template_arguments_p
17367 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17368 return true;
17370 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17371 argument of the `typeof' expression is not type-dependent, then
17372 it should already been have resolved. */
17373 if (TREE_CODE (type) == TYPEOF_TYPE
17374 || TREE_CODE (type) == DECLTYPE_TYPE)
17375 return true;
17377 /* A template argument pack is dependent if any of its packed
17378 arguments are. */
17379 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17381 tree args = ARGUMENT_PACK_ARGS (type);
17382 int i, len = TREE_VEC_LENGTH (args);
17383 for (i = 0; i < len; ++i)
17384 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17385 return true;
17388 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17389 be template parameters. */
17390 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17391 return true;
17393 /* The standard does not specifically mention types that are local
17394 to template functions or local classes, but they should be
17395 considered dependent too. For example:
17397 template <int I> void f() {
17398 enum E { a = I };
17399 S<sizeof (E)> s;
17402 The size of `E' cannot be known until the value of `I' has been
17403 determined. Therefore, `E' must be considered dependent. */
17404 scope = TYPE_CONTEXT (type);
17405 if (scope && TYPE_P (scope))
17406 return dependent_type_p (scope);
17407 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17408 return type_dependent_expression_p (scope);
17410 /* Other types are non-dependent. */
17411 return false;
17414 /* Returns TRUE if TYPE is dependent, in the sense of
17415 [temp.dep.type]. */
17417 bool
17418 dependent_type_p (tree type)
17420 /* If there are no template parameters in scope, then there can't be
17421 any dependent types. */
17422 if (!processing_template_decl)
17424 /* If we are not processing a template, then nobody should be
17425 providing us with a dependent type. */
17426 gcc_assert (type);
17427 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17428 return false;
17431 /* If the type is NULL, we have not computed a type for the entity
17432 in question; in that case, the type is dependent. */
17433 if (!type)
17434 return true;
17436 /* Erroneous types can be considered non-dependent. */
17437 if (type == error_mark_node)
17438 return false;
17440 /* If we have not already computed the appropriate value for TYPE,
17441 do so now. */
17442 if (!TYPE_DEPENDENT_P_VALID (type))
17444 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17445 TYPE_DEPENDENT_P_VALID (type) = 1;
17448 return TYPE_DEPENDENT_P (type);
17451 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17452 lookup. In other words, a dependent type that is not the current
17453 instantiation. */
17455 bool
17456 dependent_scope_p (tree scope)
17458 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17459 && !currently_open_class (scope));
17462 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17464 static bool
17465 dependent_scope_ref_p (tree expression, bool criterion (tree))
17467 tree scope;
17468 tree name;
17470 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
17472 if (!TYPE_P (TREE_OPERAND (expression, 0)))
17473 return true;
17475 scope = TREE_OPERAND (expression, 0);
17476 name = TREE_OPERAND (expression, 1);
17478 /* [temp.dep.expr]
17480 An id-expression is type-dependent if it contains a
17481 nested-name-specifier that contains a class-name that names a
17482 dependent type. */
17483 /* The suggested resolution to Core Issue 224 implies that if the
17484 qualifying type is the current class, then we must peek
17485 inside it. */
17486 if (DECL_P (name)
17487 && currently_open_class (scope)
17488 && !criterion (name))
17489 return false;
17490 if (dependent_type_p (scope))
17491 return true;
17493 return false;
17496 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17497 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17498 expression. */
17500 bool
17501 value_dependent_expression_p (tree expression)
17503 if (!processing_template_decl)
17504 return false;
17506 /* A name declared with a dependent type. */
17507 if (DECL_P (expression) && type_dependent_expression_p (expression))
17508 return true;
17510 switch (TREE_CODE (expression))
17512 case IDENTIFIER_NODE:
17513 /* A name that has not been looked up -- must be dependent. */
17514 return true;
17516 case TEMPLATE_PARM_INDEX:
17517 /* A non-type template parm. */
17518 return true;
17520 case CONST_DECL:
17521 /* A non-type template parm. */
17522 if (DECL_TEMPLATE_PARM_P (expression))
17523 return true;
17524 return value_dependent_expression_p (DECL_INITIAL (expression));
17526 case VAR_DECL:
17527 /* A constant with integral or enumeration type and is initialized
17528 with an expression that is value-dependent. */
17529 if (DECL_INITIAL (expression)
17530 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17531 && value_dependent_expression_p (DECL_INITIAL (expression)))
17532 return true;
17533 return false;
17535 case DYNAMIC_CAST_EXPR:
17536 case STATIC_CAST_EXPR:
17537 case CONST_CAST_EXPR:
17538 case REINTERPRET_CAST_EXPR:
17539 case CAST_EXPR:
17540 /* These expressions are value-dependent if the type to which
17541 the cast occurs is dependent or the expression being casted
17542 is value-dependent. */
17544 tree type = TREE_TYPE (expression);
17546 if (dependent_type_p (type))
17547 return true;
17549 /* A functional cast has a list of operands. */
17550 expression = TREE_OPERAND (expression, 0);
17551 if (!expression)
17553 /* If there are no operands, it must be an expression such
17554 as "int()". This should not happen for aggregate types
17555 because it would form non-constant expressions. */
17556 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17558 return false;
17561 if (TREE_CODE (expression) == TREE_LIST)
17562 return any_value_dependent_elements_p (expression);
17564 return value_dependent_expression_p (expression);
17567 case SIZEOF_EXPR:
17568 case ALIGNOF_EXPR:
17569 /* A `sizeof' expression is value-dependent if the operand is
17570 type-dependent or is a pack expansion. */
17571 expression = TREE_OPERAND (expression, 0);
17572 if (PACK_EXPANSION_P (expression))
17573 return true;
17574 else if (TYPE_P (expression))
17575 return dependent_type_p (expression);
17576 return type_dependent_expression_p (expression);
17578 case SCOPE_REF:
17579 return dependent_scope_ref_p (expression, value_dependent_expression_p);
17581 case COMPONENT_REF:
17582 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17583 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17585 case CALL_EXPR:
17586 /* A CALL_EXPR may appear in a constant expression if it is a
17587 call to a builtin function, e.g., __builtin_constant_p. All
17588 such calls are value-dependent. */
17589 return true;
17591 case NONTYPE_ARGUMENT_PACK:
17592 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17593 is value-dependent. */
17595 tree values = ARGUMENT_PACK_ARGS (expression);
17596 int i, len = TREE_VEC_LENGTH (values);
17598 for (i = 0; i < len; ++i)
17599 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17600 return true;
17602 return false;
17605 case TRAIT_EXPR:
17607 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17608 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17609 || (type2 ? dependent_type_p (type2) : false));
17612 case MODOP_EXPR:
17613 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17614 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17616 default:
17617 /* A constant expression is value-dependent if any subexpression is
17618 value-dependent. */
17619 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17621 case tcc_reference:
17622 case tcc_unary:
17623 return (value_dependent_expression_p
17624 (TREE_OPERAND (expression, 0)));
17626 case tcc_comparison:
17627 case tcc_binary:
17628 return ((value_dependent_expression_p
17629 (TREE_OPERAND (expression, 0)))
17630 || (value_dependent_expression_p
17631 (TREE_OPERAND (expression, 1))));
17633 case tcc_expression:
17634 case tcc_vl_exp:
17636 int i;
17637 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17638 /* In some cases, some of the operands may be missing.
17639 (For example, in the case of PREDECREMENT_EXPR, the
17640 amount to increment by may be missing.) That doesn't
17641 make the expression dependent. */
17642 if (TREE_OPERAND (expression, i)
17643 && (value_dependent_expression_p
17644 (TREE_OPERAND (expression, i))))
17645 return true;
17646 return false;
17649 default:
17650 break;
17654 /* The expression is not value-dependent. */
17655 return false;
17658 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17659 [temp.dep.expr]. */
17661 bool
17662 type_dependent_expression_p (tree expression)
17664 if (!processing_template_decl)
17665 return false;
17667 if (expression == error_mark_node)
17668 return false;
17670 /* An unresolved name is always dependent. */
17671 if (TREE_CODE (expression) == IDENTIFIER_NODE
17672 || TREE_CODE (expression) == USING_DECL)
17673 return true;
17675 /* Some expression forms are never type-dependent. */
17676 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17677 || TREE_CODE (expression) == SIZEOF_EXPR
17678 || TREE_CODE (expression) == ALIGNOF_EXPR
17679 || TREE_CODE (expression) == TRAIT_EXPR
17680 || TREE_CODE (expression) == TYPEID_EXPR
17681 || TREE_CODE (expression) == DELETE_EXPR
17682 || TREE_CODE (expression) == VEC_DELETE_EXPR
17683 || TREE_CODE (expression) == THROW_EXPR)
17684 return false;
17686 /* The types of these expressions depends only on the type to which
17687 the cast occurs. */
17688 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17689 || TREE_CODE (expression) == STATIC_CAST_EXPR
17690 || TREE_CODE (expression) == CONST_CAST_EXPR
17691 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17692 || TREE_CODE (expression) == CAST_EXPR)
17693 return dependent_type_p (TREE_TYPE (expression));
17695 /* The types of these expressions depends only on the type created
17696 by the expression. */
17697 if (TREE_CODE (expression) == NEW_EXPR
17698 || TREE_CODE (expression) == VEC_NEW_EXPR)
17700 /* For NEW_EXPR tree nodes created inside a template, either
17701 the object type itself or a TREE_LIST may appear as the
17702 operand 1. */
17703 tree type = TREE_OPERAND (expression, 1);
17704 if (TREE_CODE (type) == TREE_LIST)
17705 /* This is an array type. We need to check array dimensions
17706 as well. */
17707 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17708 || value_dependent_expression_p
17709 (TREE_OPERAND (TREE_VALUE (type), 1));
17710 else
17711 return dependent_type_p (type);
17714 if (TREE_CODE (expression) == SCOPE_REF
17715 && dependent_scope_ref_p (expression,
17716 type_dependent_expression_p))
17717 return true;
17719 if (TREE_CODE (expression) == FUNCTION_DECL
17720 && DECL_LANG_SPECIFIC (expression)
17721 && DECL_TEMPLATE_INFO (expression)
17722 && (any_dependent_template_arguments_p
17723 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17724 return true;
17726 if (TREE_CODE (expression) == TEMPLATE_DECL
17727 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17728 return false;
17730 if (TREE_CODE (expression) == STMT_EXPR)
17731 expression = stmt_expr_value_expr (expression);
17733 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17735 tree elt;
17736 unsigned i;
17738 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17740 if (type_dependent_expression_p (elt))
17741 return true;
17743 return false;
17746 /* A static data member of the current instantiation with incomplete
17747 array type is type-dependent, as the definition and specializations
17748 can have different bounds. */
17749 if (TREE_CODE (expression) == VAR_DECL
17750 && DECL_CLASS_SCOPE_P (expression)
17751 && dependent_type_p (DECL_CONTEXT (expression))
17752 && VAR_HAD_UNKNOWN_BOUND (expression))
17753 return true;
17755 if (TREE_TYPE (expression) == unknown_type_node)
17757 if (TREE_CODE (expression) == ADDR_EXPR)
17758 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17759 if (TREE_CODE (expression) == COMPONENT_REF
17760 || TREE_CODE (expression) == OFFSET_REF)
17762 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17763 return true;
17764 expression = TREE_OPERAND (expression, 1);
17765 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17766 return false;
17768 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17769 if (TREE_CODE (expression) == SCOPE_REF)
17770 return false;
17772 if (TREE_CODE (expression) == BASELINK)
17773 expression = BASELINK_FUNCTIONS (expression);
17775 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17777 if (any_dependent_template_arguments_p
17778 (TREE_OPERAND (expression, 1)))
17779 return true;
17780 expression = TREE_OPERAND (expression, 0);
17782 gcc_assert (TREE_CODE (expression) == OVERLOAD
17783 || TREE_CODE (expression) == FUNCTION_DECL);
17785 while (expression)
17787 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17788 return true;
17789 expression = OVL_NEXT (expression);
17791 return false;
17794 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17796 return (dependent_type_p (TREE_TYPE (expression)));
17799 /* Like type_dependent_expression_p, but it also works while not processing
17800 a template definition, i.e. during substitution or mangling. */
17802 bool
17803 type_dependent_expression_p_push (tree expr)
17805 bool b;
17806 ++processing_template_decl;
17807 b = type_dependent_expression_p (expr);
17808 --processing_template_decl;
17809 return b;
17812 /* Returns TRUE if ARGS contains a type-dependent expression. */
17814 bool
17815 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17817 unsigned int i;
17818 tree arg;
17820 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17822 if (type_dependent_expression_p (arg))
17823 return true;
17825 return false;
17828 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17829 expressions) contains any value-dependent expressions. */
17831 bool
17832 any_value_dependent_elements_p (const_tree list)
17834 for (; list; list = TREE_CHAIN (list))
17835 if (value_dependent_expression_p (TREE_VALUE (list)))
17836 return true;
17838 return false;
17841 /* Returns TRUE if the ARG (a template argument) is dependent. */
17843 bool
17844 dependent_template_arg_p (tree arg)
17846 if (!processing_template_decl)
17847 return false;
17849 if (TREE_CODE (arg) == TEMPLATE_DECL
17850 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17851 return dependent_template_p (arg);
17852 else if (ARGUMENT_PACK_P (arg))
17854 tree args = ARGUMENT_PACK_ARGS (arg);
17855 int i, len = TREE_VEC_LENGTH (args);
17856 for (i = 0; i < len; ++i)
17858 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17859 return true;
17862 return false;
17864 else if (TYPE_P (arg))
17865 return dependent_type_p (arg);
17866 else
17867 return (type_dependent_expression_p (arg)
17868 || value_dependent_expression_p (arg));
17871 /* Returns true if ARGS (a collection of template arguments) contains
17872 any types that require structural equality testing. */
17874 bool
17875 any_template_arguments_need_structural_equality_p (tree args)
17877 int i;
17878 int j;
17880 if (!args)
17881 return false;
17882 if (args == error_mark_node)
17883 return true;
17885 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17887 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17888 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17890 tree arg = TREE_VEC_ELT (level, j);
17891 tree packed_args = NULL_TREE;
17892 int k, len = 1;
17894 if (ARGUMENT_PACK_P (arg))
17896 /* Look inside the argument pack. */
17897 packed_args = ARGUMENT_PACK_ARGS (arg);
17898 len = TREE_VEC_LENGTH (packed_args);
17901 for (k = 0; k < len; ++k)
17903 if (packed_args)
17904 arg = TREE_VEC_ELT (packed_args, k);
17906 if (error_operand_p (arg))
17907 return true;
17908 else if (TREE_CODE (arg) == TEMPLATE_DECL
17909 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17910 continue;
17911 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17912 return true;
17913 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17914 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17915 return true;
17920 return false;
17923 /* Returns true if ARGS (a collection of template arguments) contains
17924 any dependent arguments. */
17926 bool
17927 any_dependent_template_arguments_p (const_tree args)
17929 int i;
17930 int j;
17932 if (!args)
17933 return false;
17934 if (args == error_mark_node)
17935 return true;
17937 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17939 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17940 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17941 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17942 return true;
17945 return false;
17948 /* Returns TRUE if the template TMPL is dependent. */
17950 bool
17951 dependent_template_p (tree tmpl)
17953 if (TREE_CODE (tmpl) == OVERLOAD)
17955 while (tmpl)
17957 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17958 return true;
17959 tmpl = OVL_CHAIN (tmpl);
17961 return false;
17964 /* Template template parameters are dependent. */
17965 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17966 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17967 return true;
17968 /* So are names that have not been looked up. */
17969 if (TREE_CODE (tmpl) == SCOPE_REF
17970 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17971 return true;
17972 /* So are member templates of dependent classes. */
17973 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17974 return dependent_type_p (DECL_CONTEXT (tmpl));
17975 return false;
17978 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17980 bool
17981 dependent_template_id_p (tree tmpl, tree args)
17983 return (dependent_template_p (tmpl)
17984 || any_dependent_template_arguments_p (args));
17987 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17988 is dependent. */
17990 bool
17991 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17993 int i;
17995 if (!processing_template_decl)
17996 return false;
17998 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
18000 tree decl = TREE_VEC_ELT (declv, i);
18001 tree init = TREE_VEC_ELT (initv, i);
18002 tree cond = TREE_VEC_ELT (condv, i);
18003 tree incr = TREE_VEC_ELT (incrv, i);
18005 if (type_dependent_expression_p (decl))
18006 return true;
18008 if (init && type_dependent_expression_p (init))
18009 return true;
18011 if (type_dependent_expression_p (cond))
18012 return true;
18014 if (COMPARISON_CLASS_P (cond)
18015 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
18016 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
18017 return true;
18019 if (TREE_CODE (incr) == MODOP_EXPR)
18021 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
18022 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
18023 return true;
18025 else if (type_dependent_expression_p (incr))
18026 return true;
18027 else if (TREE_CODE (incr) == MODIFY_EXPR)
18029 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
18030 return true;
18031 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
18033 tree t = TREE_OPERAND (incr, 1);
18034 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
18035 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
18036 return true;
18041 return false;
18044 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18045 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18046 no such TYPE can be found. Note that this function peers inside
18047 uninstantiated templates and therefore should be used only in
18048 extremely limited situations. ONLY_CURRENT_P restricts this
18049 peering to the currently open classes hierarchy (which is required
18050 when comparing types). */
18052 tree
18053 resolve_typename_type (tree type, bool only_current_p)
18055 tree scope;
18056 tree name;
18057 tree decl;
18058 int quals;
18059 tree pushed_scope;
18060 tree result;
18062 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
18064 scope = TYPE_CONTEXT (type);
18065 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18066 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18067 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18068 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18069 identifier of the TYPENAME_TYPE anymore.
18070 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18071 TYPENAME_TYPE instead, we avoid messing up with a possible
18072 typedef variant case. */
18073 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
18075 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18076 it first before we can figure out what NAME refers to. */
18077 if (TREE_CODE (scope) == TYPENAME_TYPE)
18078 scope = resolve_typename_type (scope, only_current_p);
18079 /* If we don't know what SCOPE refers to, then we cannot resolve the
18080 TYPENAME_TYPE. */
18081 if (TREE_CODE (scope) == TYPENAME_TYPE)
18082 return type;
18083 /* If the SCOPE is a template type parameter, we have no way of
18084 resolving the name. */
18085 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
18086 return type;
18087 /* If the SCOPE is not the current instantiation, there's no reason
18088 to look inside it. */
18089 if (only_current_p && !currently_open_class (scope))
18090 return type;
18091 /* If this is a typedef, we don't want to look inside (c++/11987). */
18092 if (typedef_variant_p (type))
18093 return type;
18094 /* If SCOPE isn't the template itself, it will not have a valid
18095 TYPE_FIELDS list. */
18096 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
18097 /* scope is either the template itself or a compatible instantiation
18098 like X<T>, so look up the name in the original template. */
18099 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
18100 else
18101 /* scope is a partial instantiation, so we can't do the lookup or we
18102 will lose the template arguments. */
18103 return type;
18104 /* Enter the SCOPE so that name lookup will be resolved as if we
18105 were in the class definition. In particular, SCOPE will no
18106 longer be considered a dependent type. */
18107 pushed_scope = push_scope (scope);
18108 /* Look up the declaration. */
18109 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
18111 result = NULL_TREE;
18113 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18114 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18115 if (!decl)
18116 /*nop*/;
18117 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
18118 && TREE_CODE (decl) == TYPE_DECL)
18120 result = TREE_TYPE (decl);
18121 if (result == error_mark_node)
18122 result = NULL_TREE;
18124 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
18125 && DECL_CLASS_TEMPLATE_P (decl))
18127 tree tmpl;
18128 tree args;
18129 /* Obtain the template and the arguments. */
18130 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
18131 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
18132 /* Instantiate the template. */
18133 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
18134 /*entering_scope=*/0,
18135 tf_error | tf_user);
18136 if (result == error_mark_node)
18137 result = NULL_TREE;
18140 /* Leave the SCOPE. */
18141 if (pushed_scope)
18142 pop_scope (pushed_scope);
18144 /* If we failed to resolve it, return the original typename. */
18145 if (!result)
18146 return type;
18148 /* If lookup found a typename type, resolve that too. */
18149 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
18151 /* Ill-formed programs can cause infinite recursion here, so we
18152 must catch that. */
18153 TYPENAME_IS_RESOLVING_P (type) = 1;
18154 result = resolve_typename_type (result, only_current_p);
18155 TYPENAME_IS_RESOLVING_P (type) = 0;
18158 /* Qualify the resulting type. */
18159 quals = cp_type_quals (type);
18160 if (quals)
18161 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
18163 return result;
18166 /* EXPR is an expression which is not type-dependent. Return a proxy
18167 for EXPR that can be used to compute the types of larger
18168 expressions containing EXPR. */
18170 tree
18171 build_non_dependent_expr (tree expr)
18173 tree inner_expr;
18175 /* Preserve null pointer constants so that the type of things like
18176 "p == 0" where "p" is a pointer can be determined. */
18177 if (null_ptr_cst_p (expr))
18178 return expr;
18179 /* Preserve OVERLOADs; the functions must be available to resolve
18180 types. */
18181 inner_expr = expr;
18182 if (TREE_CODE (inner_expr) == STMT_EXPR)
18183 inner_expr = stmt_expr_value_expr (inner_expr);
18184 if (TREE_CODE (inner_expr) == ADDR_EXPR)
18185 inner_expr = TREE_OPERAND (inner_expr, 0);
18186 if (TREE_CODE (inner_expr) == COMPONENT_REF)
18187 inner_expr = TREE_OPERAND (inner_expr, 1);
18188 if (is_overloaded_fn (inner_expr)
18189 || TREE_CODE (inner_expr) == OFFSET_REF)
18190 return expr;
18191 /* There is no need to return a proxy for a variable. */
18192 if (TREE_CODE (expr) == VAR_DECL)
18193 return expr;
18194 /* Preserve string constants; conversions from string constants to
18195 "char *" are allowed, even though normally a "const char *"
18196 cannot be used to initialize a "char *". */
18197 if (TREE_CODE (expr) == STRING_CST)
18198 return expr;
18199 /* Preserve arithmetic constants, as an optimization -- there is no
18200 reason to create a new node. */
18201 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
18202 return expr;
18203 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18204 There is at least one place where we want to know that a
18205 particular expression is a throw-expression: when checking a ?:
18206 expression, there are special rules if the second or third
18207 argument is a throw-expression. */
18208 if (TREE_CODE (expr) == THROW_EXPR)
18209 return expr;
18211 if (TREE_CODE (expr) == COND_EXPR)
18212 return build3 (COND_EXPR,
18213 TREE_TYPE (expr),
18214 TREE_OPERAND (expr, 0),
18215 (TREE_OPERAND (expr, 1)
18216 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
18217 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
18218 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
18219 if (TREE_CODE (expr) == COMPOUND_EXPR
18220 && !COMPOUND_EXPR_OVERLOADED (expr))
18221 return build2 (COMPOUND_EXPR,
18222 TREE_TYPE (expr),
18223 TREE_OPERAND (expr, 0),
18224 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
18226 /* If the type is unknown, it can't really be non-dependent */
18227 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
18229 /* Otherwise, build a NON_DEPENDENT_EXPR.
18231 REFERENCE_TYPEs are not stripped for expressions in templates
18232 because doing so would play havoc with mangling. Consider, for
18233 example:
18235 template <typename T> void f<T& g>() { g(); }
18237 In the body of "f", the expression for "g" will have
18238 REFERENCE_TYPE, even though the standard says that it should
18239 not. The reason is that we must preserve the syntactic form of
18240 the expression so that mangling (say) "f<g>" inside the body of
18241 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18242 stripped here. */
18243 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
18246 /* ARGS is a vector of expressions as arguments to a function call.
18247 Replace the arguments with equivalent non-dependent expressions.
18248 This modifies ARGS in place. */
18250 void
18251 make_args_non_dependent (VEC(tree,gc) *args)
18253 unsigned int ix;
18254 tree arg;
18256 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
18258 tree newarg = build_non_dependent_expr (arg);
18259 if (newarg != arg)
18260 VEC_replace (tree, args, ix, newarg);
18264 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18265 with a level one deeper than the actual template parms. */
18267 tree
18268 make_auto (void)
18270 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18271 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18272 TYPE_DECL, get_identifier ("auto"), au);
18273 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18274 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18275 (0, processing_template_decl + 1, processing_template_decl + 1,
18276 TYPE_NAME (au), NULL_TREE);
18277 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18278 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18279 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18281 return au;
18284 /* Given type ARG, return std::initializer_list<ARG>. */
18286 static tree
18287 listify (tree arg)
18289 tree std_init_list = namespace_binding
18290 (get_identifier ("initializer_list"), std_node);
18291 tree argvec;
18292 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18294 error ("deducing from brace-enclosed initializer list requires "
18295 "#include <initializer_list>");
18296 return error_mark_node;
18298 argvec = make_tree_vec (1);
18299 TREE_VEC_ELT (argvec, 0) = arg;
18300 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18301 NULL_TREE, 0, tf_warning_or_error);
18304 /* Replace auto in TYPE with std::initializer_list<auto>. */
18306 static tree
18307 listify_autos (tree type, tree auto_node)
18309 tree init_auto = listify (auto_node);
18310 tree argvec = make_tree_vec (1);
18311 TREE_VEC_ELT (argvec, 0) = init_auto;
18312 if (processing_template_decl)
18313 argvec = add_to_template_args (current_template_args (), argvec);
18314 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18317 /* walk_tree helper for do_auto_deduction. */
18319 static tree
18320 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
18321 void *type)
18323 /* Is this a variable with the type we're looking for? */
18324 if (DECL_P (*tp)
18325 && TREE_TYPE (*tp) == type)
18326 return *tp;
18327 else
18328 return NULL_TREE;
18331 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18332 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18334 tree
18335 do_auto_deduction (tree type, tree init, tree auto_node)
18337 tree parms, tparms, targs;
18338 tree args[1];
18339 tree decl;
18340 int val;
18342 /* The name of the object being declared shall not appear in the
18343 initializer expression. */
18344 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
18345 if (decl)
18347 error ("variable %q#D with %<auto%> type used in its own "
18348 "initializer", decl);
18349 return error_mark_node;
18352 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18353 with either a new invented type template parameter U or, if the
18354 initializer is a braced-init-list (8.5.4), with
18355 std::initializer_list<U>. */
18356 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18357 type = listify_autos (type, auto_node);
18359 parms = build_tree_list (NULL_TREE, type);
18360 args[0] = init;
18361 tparms = make_tree_vec (1);
18362 targs = make_tree_vec (1);
18363 TREE_VEC_ELT (tparms, 0)
18364 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18365 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18366 DEDUCE_CALL, LOOKUP_NORMAL);
18367 if (val > 0)
18369 error ("unable to deduce %qT from %qE", type, init);
18370 return error_mark_node;
18373 /* If the list of declarators contains more than one declarator, the type
18374 of each declared variable is determined as described above. If the
18375 type deduced for the template parameter U is not the same in each
18376 deduction, the program is ill-formed. */
18377 if (TREE_TYPE (auto_node)
18378 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18380 error ("inconsistent deduction for %qT: %qT and then %qT",
18381 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18382 return error_mark_node;
18384 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18386 if (processing_template_decl)
18387 targs = add_to_template_args (current_template_args (), targs);
18388 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18391 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18392 result. */
18394 tree
18395 splice_late_return_type (tree type, tree late_return_type)
18397 tree argvec;
18399 if (late_return_type == NULL_TREE)
18400 return type;
18401 argvec = make_tree_vec (1);
18402 TREE_VEC_ELT (argvec, 0) = late_return_type;
18403 if (processing_template_decl)
18404 argvec = add_to_template_args (current_template_args (), argvec);
18405 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18408 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18410 bool
18411 is_auto (const_tree type)
18413 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18414 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18415 return true;
18416 else
18417 return false;
18420 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18421 appear as a type-specifier for the declaration in question, we don't
18422 have to look through the whole type. */
18424 tree
18425 type_uses_auto (tree type)
18427 enum tree_code code;
18428 if (is_auto (type))
18429 return type;
18431 code = TREE_CODE (type);
18433 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18434 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18435 || code == METHOD_TYPE || code == ARRAY_TYPE)
18436 return type_uses_auto (TREE_TYPE (type));
18438 if (TYPE_PTRMEMFUNC_P (type))
18439 return type_uses_auto (TREE_TYPE (TREE_TYPE
18440 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18442 return NULL_TREE;
18445 /* For a given template T, return the vector of typedefs referenced
18446 in T for which access check is needed at T instantiation time.
18447 T is either a FUNCTION_DECL or a RECORD_TYPE.
18448 Those typedefs were added to T by the function
18449 append_type_to_template_for_access_check. */
18451 VEC(qualified_typedef_usage_t,gc)*
18452 get_types_needing_access_check (tree t)
18454 tree ti;
18455 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18457 if (!t || t == error_mark_node)
18458 return NULL;
18460 if (!(ti = get_template_info (t)))
18461 return NULL;
18463 if (CLASS_TYPE_P (t)
18464 || TREE_CODE (t) == FUNCTION_DECL)
18466 if (!TI_TEMPLATE (ti))
18467 return NULL;
18469 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18472 return result;
18475 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18476 tied to T. That list of typedefs will be access checked at
18477 T instantiation time.
18478 T is either a FUNCTION_DECL or a RECORD_TYPE.
18479 TYPE_DECL is a TYPE_DECL node representing a typedef.
18480 SCOPE is the scope through which TYPE_DECL is accessed.
18481 LOCATION is the location of the usage point of TYPE_DECL.
18483 This function is a subroutine of
18484 append_type_to_template_for_access_check. */
18486 static void
18487 append_type_to_template_for_access_check_1 (tree t,
18488 tree type_decl,
18489 tree scope,
18490 location_t location)
18492 qualified_typedef_usage_t typedef_usage;
18493 tree ti;
18495 if (!t || t == error_mark_node)
18496 return;
18498 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18499 || CLASS_TYPE_P (t))
18500 && type_decl
18501 && TREE_CODE (type_decl) == TYPE_DECL
18502 && scope);
18504 if (!(ti = get_template_info (t)))
18505 return;
18507 gcc_assert (TI_TEMPLATE (ti));
18509 typedef_usage.typedef_decl = type_decl;
18510 typedef_usage.context = scope;
18511 typedef_usage.locus = location;
18513 VEC_safe_push (qualified_typedef_usage_t, gc,
18514 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18515 &typedef_usage);
18518 /* Append TYPE_DECL to the template TEMPL.
18519 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18520 At TEMPL instanciation time, TYPE_DECL will be checked to see
18521 if it can be accessed through SCOPE.
18522 LOCATION is the location of the usage point of TYPE_DECL.
18524 e.g. consider the following code snippet:
18526 class C
18528 typedef int myint;
18531 template<class U> struct S
18533 C::myint mi; // <-- usage point of the typedef C::myint
18536 S<char> s;
18538 At S<char> instantiation time, we need to check the access of C::myint
18539 In other words, we need to check the access of the myint typedef through
18540 the C scope. For that purpose, this function will add the myint typedef
18541 and the scope C through which its being accessed to a list of typedefs
18542 tied to the template S. That list will be walked at template instantiation
18543 time and access check performed on each typedefs it contains.
18544 Note that this particular code snippet should yield an error because
18545 myint is private to C. */
18547 void
18548 append_type_to_template_for_access_check (tree templ,
18549 tree type_decl,
18550 tree scope,
18551 location_t location)
18553 qualified_typedef_usage_t *iter;
18554 int i;
18556 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18558 /* Make sure we don't append the type to the template twice. */
18559 for (i = 0;
18560 VEC_iterate (qualified_typedef_usage_t,
18561 get_types_needing_access_check (templ),
18562 i, iter);
18563 ++i)
18564 if (iter->typedef_decl == type_decl && scope == iter->context)
18565 return;
18567 append_type_to_template_for_access_check_1 (templ, type_decl,
18568 scope, location);
18571 /* Set up the hash tables for template instantiations. */
18573 void
18574 init_template_processing (void)
18576 decl_specializations = htab_create_ggc (37,
18577 hash_specialization,
18578 eq_specializations,
18579 ggc_free);
18580 type_specializations = htab_create_ggc (37,
18581 hash_specialization,
18582 eq_specializations,
18583 ggc_free);
18586 /* Print stats about the template hash tables for -fstats. */
18588 void
18589 print_template_statistics (void)
18591 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
18592 "%f collisions\n", (long) htab_size (decl_specializations),
18593 (long) htab_elements (decl_specializations),
18594 htab_collisions (decl_specializations));
18595 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
18596 "%f collisions\n", (long) htab_size (type_specializations),
18597 (long) htab_elements (type_specializations),
18598 htab_collisions (type_specializations));
18601 #include "gt-cp-pt.h"