PR c++/54437
[official-gcc.git] / gcc / cp / pt.c
blob6f6235c009de8b1674eca9442f2aabbedb24560d
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "intl.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "tree-iterator.h"
46 #include "vecprim.h"
48 /* The type of functions taking a tree, and some additional data, and
49 returning an int. */
50 typedef int (*tree_fn_t) (tree, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY ((chain_next ("%h.next"))) pending_template {
56 struct pending_template *next;
57 struct tinst_level *tinst;
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static VEC(int,heap) *inline_parm_levels;
69 static GTY(()) struct tinst_level *current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static struct pointer_map_t *local_specializations;
83 /* True if we've recursed into fn_type_unification too many times. */
84 static bool excessive_deduction_depth;
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 enum template_base_result {
115 tbr_incomplete_type,
116 tbr_ambiguous_baseclass,
117 tbr_success
120 static void push_access_scope (tree);
121 static void pop_access_scope (tree);
122 static bool resolve_overloaded_unification (tree, tree, tree, tree,
123 unification_kind_t, int,
124 bool);
125 static int try_one_overload (tree, tree, tree, tree, tree,
126 unification_kind_t, int, bool, bool);
127 static int unify (tree, tree, tree, tree, int, bool);
128 static void add_pending_template (tree);
129 static tree reopen_tinst_level (struct tinst_level *);
130 static tree tsubst_initializer_list (tree, tree);
131 static tree get_class_bindings (tree, tree, tree, tree);
132 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
133 bool, bool);
134 static void tsubst_enum (tree, tree, tree);
135 static tree add_to_template_args (tree, tree);
136 static tree add_outermost_template_args (tree, tree);
137 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
138 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
139 tree);
140 static int type_unification_real (tree, tree, tree, const tree *,
141 unsigned int, int, unification_kind_t, int,
142 bool);
143 static void note_template_header (int);
144 static tree convert_nontype_argument_function (tree, tree);
145 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
146 static tree convert_template_argument (tree, tree, tree,
147 tsubst_flags_t, int, tree);
148 static int for_each_template_parm (tree, tree_fn_t, void*,
149 struct pointer_set_t*, bool);
150 static tree expand_template_argument_pack (tree);
151 static tree build_template_parm_index (int, int, int, tree, tree);
152 static bool inline_needs_template_parms (tree);
153 static void push_inline_template_parms_recursive (tree, int);
154 static tree retrieve_local_specialization (tree);
155 static void register_local_specialization (tree, tree);
156 static hashval_t hash_specialization (const void *p);
157 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
158 static int mark_template_parm (tree, void *);
159 static int template_parm_this_level_p (tree, void *);
160 static tree tsubst_friend_function (tree, tree);
161 static tree tsubst_friend_class (tree, tree);
162 static int can_complete_type_without_circularity (tree);
163 static tree get_bindings (tree, tree, tree, bool);
164 static int template_decl_level (tree);
165 static int check_cv_quals_for_unify (int, tree, tree);
166 static void template_parm_level_and_index (tree, int*, int*);
167 static int unify_pack_expansion (tree, tree, tree,
168 tree, unification_kind_t, bool, bool);
169 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
170 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
171 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
172 static void regenerate_decl_from_template (tree, tree);
173 static tree most_specialized_class (tree, tree, tsubst_flags_t);
174 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
175 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
176 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
177 static bool check_specialization_scope (void);
178 static tree process_partial_specialization (tree);
179 static void set_current_access_from_decl (tree);
180 static enum template_base_result get_template_base (tree, tree, tree, tree,
181 bool , tree *);
182 static tree try_class_unification (tree, tree, tree, tree, bool);
183 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
184 tree, tree);
185 static bool template_template_parm_bindings_ok_p (tree, tree);
186 static int template_args_equal (tree, tree);
187 static void tsubst_default_arguments (tree);
188 static tree for_each_template_parm_r (tree *, int *, void *);
189 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
190 static void copy_default_args_to_explicit_spec (tree);
191 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
192 static bool dependent_template_arg_p (tree);
193 static bool any_template_arguments_need_structural_equality_p (tree);
194 static bool dependent_type_p_r (tree);
195 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
196 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
197 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
198 static tree tsubst_decl (tree, tree, tsubst_flags_t);
199 static void perform_typedefs_access_check (tree tmpl, tree targs);
200 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
201 location_t);
202 static tree listify (tree);
203 static tree listify_autos (tree, tree);
204 static tree template_parm_to_arg (tree t);
205 static bool arg_from_parm_pack_p (tree, tree);
206 static tree current_template_args (void);
207 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
209 /* Make the current scope suitable for access checking when we are
210 processing T. T can be FUNCTION_DECL for instantiated function
211 template, or VAR_DECL for static member variable (need by
212 instantiate_decl). */
214 static void
215 push_access_scope (tree t)
217 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
218 || TREE_CODE (t) == VAR_DECL);
220 if (DECL_FRIEND_CONTEXT (t))
221 push_nested_class (DECL_FRIEND_CONTEXT (t));
222 else if (DECL_CLASS_SCOPE_P (t))
223 push_nested_class (DECL_CONTEXT (t));
224 else
225 push_to_top_level ();
227 if (TREE_CODE (t) == FUNCTION_DECL)
229 saved_access_scope = tree_cons
230 (NULL_TREE, current_function_decl, saved_access_scope);
231 current_function_decl = t;
235 /* Restore the scope set up by push_access_scope. T is the node we
236 are processing. */
238 static void
239 pop_access_scope (tree t)
241 if (TREE_CODE (t) == FUNCTION_DECL)
243 current_function_decl = TREE_VALUE (saved_access_scope);
244 saved_access_scope = TREE_CHAIN (saved_access_scope);
247 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
248 pop_nested_class ();
249 else
250 pop_from_top_level ();
253 /* Do any processing required when DECL (a member template
254 declaration) is finished. Returns the TEMPLATE_DECL corresponding
255 to DECL, unless it is a specialization, in which case the DECL
256 itself is returned. */
258 tree
259 finish_member_template_decl (tree decl)
261 if (decl == error_mark_node)
262 return error_mark_node;
264 gcc_assert (DECL_P (decl));
266 if (TREE_CODE (decl) == TYPE_DECL)
268 tree type;
270 type = TREE_TYPE (decl);
271 if (type == error_mark_node)
272 return error_mark_node;
273 if (MAYBE_CLASS_TYPE_P (type)
274 && CLASSTYPE_TEMPLATE_INFO (type)
275 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
277 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
278 check_member_template (tmpl);
279 return tmpl;
281 return NULL_TREE;
283 else if (TREE_CODE (decl) == FIELD_DECL)
284 error ("data member %qD cannot be a member template", decl);
285 else if (DECL_TEMPLATE_INFO (decl))
287 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
289 check_member_template (DECL_TI_TEMPLATE (decl));
290 return DECL_TI_TEMPLATE (decl);
292 else
293 return decl;
295 else
296 error ("invalid member template declaration %qD", decl);
298 return error_mark_node;
301 /* Create a template info node. */
303 tree
304 build_template_info (tree template_decl, tree template_args)
306 tree result = make_node (TEMPLATE_INFO);
307 TI_TEMPLATE (result) = template_decl;
308 TI_ARGS (result) = template_args;
309 return result;
312 /* Return the template info node corresponding to T, whatever T is. */
314 tree
315 get_template_info (const_tree t)
317 tree tinfo = NULL_TREE;
319 if (!t || t == error_mark_node)
320 return NULL;
322 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
323 tinfo = DECL_TEMPLATE_INFO (t);
325 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
326 t = TREE_TYPE (t);
328 if (TAGGED_TYPE_P (t))
329 tinfo = TYPE_TEMPLATE_INFO (t);
330 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
331 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
333 return tinfo;
336 /* Returns the template nesting level of the indicated class TYPE.
338 For example, in:
339 template <class T>
340 struct A
342 template <class U>
343 struct B {};
346 A<T>::B<U> has depth two, while A<T> has depth one.
347 Both A<T>::B<int> and A<int>::B<U> have depth one, if
348 they are instantiations, not specializations.
350 This function is guaranteed to return 0 if passed NULL_TREE so
351 that, for example, `template_class_depth (current_class_type)' is
352 always safe. */
355 template_class_depth (tree type)
357 int depth;
359 for (depth = 0;
360 type && TREE_CODE (type) != NAMESPACE_DECL;
361 type = (TREE_CODE (type) == FUNCTION_DECL)
362 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
364 tree tinfo = get_template_info (type);
366 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
367 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
368 ++depth;
371 return depth;
374 /* Subroutine of maybe_begin_member_template_processing.
375 Returns true if processing DECL needs us to push template parms. */
377 static bool
378 inline_needs_template_parms (tree decl)
380 if (! DECL_TEMPLATE_INFO (decl))
381 return false;
383 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
384 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
387 /* Subroutine of maybe_begin_member_template_processing.
388 Push the template parms in PARMS, starting from LEVELS steps into the
389 chain, and ending at the beginning, since template parms are listed
390 innermost first. */
392 static void
393 push_inline_template_parms_recursive (tree parmlist, int levels)
395 tree parms = TREE_VALUE (parmlist);
396 int i;
398 if (levels > 1)
399 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
401 ++processing_template_decl;
402 current_template_parms
403 = tree_cons (size_int (processing_template_decl),
404 parms, current_template_parms);
405 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
407 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
408 NULL);
409 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
411 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
413 if (parm == error_mark_node)
414 continue;
416 gcc_assert (DECL_P (parm));
418 switch (TREE_CODE (parm))
420 case TYPE_DECL:
421 case TEMPLATE_DECL:
422 pushdecl (parm);
423 break;
425 case PARM_DECL:
427 /* Make a CONST_DECL as is done in process_template_parm.
428 It is ugly that we recreate this here; the original
429 version built in process_template_parm is no longer
430 available. */
431 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
432 CONST_DECL, DECL_NAME (parm),
433 TREE_TYPE (parm));
434 DECL_ARTIFICIAL (decl) = 1;
435 TREE_CONSTANT (decl) = 1;
436 TREE_READONLY (decl) = 1;
437 DECL_INITIAL (decl) = DECL_INITIAL (parm);
438 SET_DECL_TEMPLATE_PARM_P (decl);
439 pushdecl (decl);
441 break;
443 default:
444 gcc_unreachable ();
449 /* Restore the template parameter context for a member template or
450 a friend template defined in a class definition. */
452 void
453 maybe_begin_member_template_processing (tree decl)
455 tree parms;
456 int levels = 0;
458 if (inline_needs_template_parms (decl))
460 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
461 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
463 if (DECL_TEMPLATE_SPECIALIZATION (decl))
465 --levels;
466 parms = TREE_CHAIN (parms);
469 push_inline_template_parms_recursive (parms, levels);
472 /* Remember how many levels of template parameters we pushed so that
473 we can pop them later. */
474 VEC_safe_push (int, heap, inline_parm_levels, levels);
477 /* Undo the effects of maybe_begin_member_template_processing. */
479 void
480 maybe_end_member_template_processing (void)
482 int i;
483 int last;
485 if (VEC_length (int, inline_parm_levels) == 0)
486 return;
488 last = VEC_pop (int, inline_parm_levels);
489 for (i = 0; i < last; ++i)
491 --processing_template_decl;
492 current_template_parms = TREE_CHAIN (current_template_parms);
493 poplevel (0, 0, 0);
497 /* Return a new template argument vector which contains all of ARGS,
498 but has as its innermost set of arguments the EXTRA_ARGS. */
500 static tree
501 add_to_template_args (tree args, tree extra_args)
503 tree new_args;
504 int extra_depth;
505 int i;
506 int j;
508 if (args == NULL_TREE || extra_args == error_mark_node)
509 return extra_args;
511 extra_depth = TMPL_ARGS_DEPTH (extra_args);
512 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
514 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
515 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
517 for (j = 1; j <= extra_depth; ++j, ++i)
518 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
520 return new_args;
523 /* Like add_to_template_args, but only the outermost ARGS are added to
524 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
525 (EXTRA_ARGS) levels are added. This function is used to combine
526 the template arguments from a partial instantiation with the
527 template arguments used to attain the full instantiation from the
528 partial instantiation. */
530 static tree
531 add_outermost_template_args (tree args, tree extra_args)
533 tree new_args;
535 /* If there are more levels of EXTRA_ARGS than there are ARGS,
536 something very fishy is going on. */
537 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
539 /* If *all* the new arguments will be the EXTRA_ARGS, just return
540 them. */
541 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
542 return extra_args;
544 /* For the moment, we make ARGS look like it contains fewer levels. */
545 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
547 new_args = add_to_template_args (args, extra_args);
549 /* Now, we restore ARGS to its full dimensions. */
550 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
552 return new_args;
555 /* Return the N levels of innermost template arguments from the ARGS. */
557 tree
558 get_innermost_template_args (tree args, int n)
560 tree new_args;
561 int extra_levels;
562 int i;
564 gcc_assert (n >= 0);
566 /* If N is 1, just return the innermost set of template arguments. */
567 if (n == 1)
568 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
570 /* If we're not removing anything, just return the arguments we were
571 given. */
572 extra_levels = TMPL_ARGS_DEPTH (args) - n;
573 gcc_assert (extra_levels >= 0);
574 if (extra_levels == 0)
575 return args;
577 /* Make a new set of arguments, not containing the outer arguments. */
578 new_args = make_tree_vec (n);
579 for (i = 1; i <= n; ++i)
580 SET_TMPL_ARGS_LEVEL (new_args, i,
581 TMPL_ARGS_LEVEL (args, i + extra_levels));
583 return new_args;
586 /* The inverse of get_innermost_template_args: Return all but the innermost
587 EXTRA_LEVELS levels of template arguments from the ARGS. */
589 static tree
590 strip_innermost_template_args (tree args, int extra_levels)
592 tree new_args;
593 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
594 int i;
596 gcc_assert (n >= 0);
598 /* If N is 1, just return the outermost set of template arguments. */
599 if (n == 1)
600 return TMPL_ARGS_LEVEL (args, 1);
602 /* If we're not removing anything, just return the arguments we were
603 given. */
604 gcc_assert (extra_levels >= 0);
605 if (extra_levels == 0)
606 return args;
608 /* Make a new set of arguments, not containing the inner arguments. */
609 new_args = make_tree_vec (n);
610 for (i = 1; i <= n; ++i)
611 SET_TMPL_ARGS_LEVEL (new_args, i,
612 TMPL_ARGS_LEVEL (args, i));
614 return new_args;
617 /* We've got a template header coming up; push to a new level for storing
618 the parms. */
620 void
621 begin_template_parm_list (void)
623 /* We use a non-tag-transparent scope here, which causes pushtag to
624 put tags in this scope, rather than in the enclosing class or
625 namespace scope. This is the right thing, since we want
626 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
627 global template class, push_template_decl handles putting the
628 TEMPLATE_DECL into top-level scope. For a nested template class,
629 e.g.:
631 template <class T> struct S1 {
632 template <class T> struct S2 {};
635 pushtag contains special code to call pushdecl_with_scope on the
636 TEMPLATE_DECL for S2. */
637 begin_scope (sk_template_parms, NULL);
638 ++processing_template_decl;
639 ++processing_template_parmlist;
640 note_template_header (0);
643 /* This routine is called when a specialization is declared. If it is
644 invalid to declare a specialization here, an error is reported and
645 false is returned, otherwise this routine will return true. */
647 static bool
648 check_specialization_scope (void)
650 tree scope = current_scope ();
652 /* [temp.expl.spec]
654 An explicit specialization shall be declared in the namespace of
655 which the template is a member, or, for member templates, in the
656 namespace of which the enclosing class or enclosing class
657 template is a member. An explicit specialization of a member
658 function, member class or static data member of a class template
659 shall be declared in the namespace of which the class template
660 is a member. */
661 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
663 error ("explicit specialization in non-namespace scope %qD", scope);
664 return false;
667 /* [temp.expl.spec]
669 In an explicit specialization declaration for a member of a class
670 template or a member template that appears in namespace scope,
671 the member template and some of its enclosing class templates may
672 remain unspecialized, except that the declaration shall not
673 explicitly specialize a class member template if its enclosing
674 class templates are not explicitly specialized as well. */
675 if (current_template_parms)
677 error ("enclosing class templates are not explicitly specialized");
678 return false;
681 return true;
684 /* We've just seen template <>. */
686 bool
687 begin_specialization (void)
689 begin_scope (sk_template_spec, NULL);
690 note_template_header (1);
691 return check_specialization_scope ();
694 /* Called at then end of processing a declaration preceded by
695 template<>. */
697 void
698 end_specialization (void)
700 finish_scope ();
701 reset_specialization ();
704 /* Any template <>'s that we have seen thus far are not referring to a
705 function specialization. */
707 void
708 reset_specialization (void)
710 processing_specialization = 0;
711 template_header_count = 0;
714 /* We've just seen a template header. If SPECIALIZATION is nonzero,
715 it was of the form template <>. */
717 static void
718 note_template_header (int specialization)
720 processing_specialization = specialization;
721 template_header_count++;
724 /* We're beginning an explicit instantiation. */
726 void
727 begin_explicit_instantiation (void)
729 gcc_assert (!processing_explicit_instantiation);
730 processing_explicit_instantiation = true;
734 void
735 end_explicit_instantiation (void)
737 gcc_assert (processing_explicit_instantiation);
738 processing_explicit_instantiation = false;
741 /* An explicit specialization or partial specialization of TMPL is being
742 declared. Check that the namespace in which the specialization is
743 occurring is permissible. Returns false iff it is invalid to
744 specialize TMPL in the current namespace. */
746 static bool
747 check_specialization_namespace (tree tmpl)
749 tree tpl_ns = decl_namespace_context (tmpl);
751 /* [tmpl.expl.spec]
753 An explicit specialization shall be declared in the namespace of
754 which the template is a member, or, for member templates, in the
755 namespace of which the enclosing class or enclosing class
756 template is a member. An explicit specialization of a member
757 function, member class or static data member of a class template
758 shall be declared in the namespace of which the class template is
759 a member. */
760 if (current_scope() != DECL_CONTEXT (tmpl)
761 && !at_namespace_scope_p ())
763 error ("specialization of %qD must appear at namespace scope", tmpl);
764 return false;
766 if (is_associated_namespace (current_namespace, tpl_ns))
767 /* Same or super-using namespace. */
768 return true;
769 else
771 permerror (input_location, "specialization of %qD in different namespace", tmpl);
772 permerror (input_location, " from definition of %q+#D", tmpl);
773 return false;
777 /* SPEC is an explicit instantiation. Check that it is valid to
778 perform this explicit instantiation in the current namespace. */
780 static void
781 check_explicit_instantiation_namespace (tree spec)
783 tree ns;
785 /* DR 275: An explicit instantiation shall appear in an enclosing
786 namespace of its template. */
787 ns = decl_namespace_context (spec);
788 if (!is_ancestor (current_namespace, ns))
789 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
790 "(which does not enclose namespace %qD)",
791 spec, current_namespace, ns);
794 /* The TYPE is being declared. If it is a template type, that means it
795 is a partial specialization. Do appropriate error-checking. */
797 tree
798 maybe_process_partial_specialization (tree type)
800 tree context;
802 if (type == error_mark_node)
803 return error_mark_node;
805 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
807 error ("name of class shadows template template parameter %qD",
808 TYPE_NAME (type));
809 return error_mark_node;
812 context = TYPE_CONTEXT (type);
814 if ((CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
815 /* Consider non-class instantiations of alias templates as
816 well. */
817 || (TYPE_P (type)
818 && TYPE_TEMPLATE_INFO (type)
819 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
820 && DECL_USE_TEMPLATE (TYPE_NAME (type))))
822 /* This is for ordinary explicit specialization and partial
823 specialization of a template class such as:
825 template <> class C<int>;
829 template <class T> class C<T*>;
831 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
833 if (CLASS_TYPE_P (type)
834 && CLASSTYPE_IMPLICIT_INSTANTIATION (type)
835 && !COMPLETE_TYPE_P (type))
837 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
838 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
839 if (processing_template_decl)
841 if (push_template_decl (TYPE_MAIN_DECL (type))
842 == error_mark_node)
843 return error_mark_node;
846 else if (CLASS_TYPE_P (type)
847 && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
848 error ("specialization of %qT after instantiation", type);
850 if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
852 error ("partial specialization of alias template %qD",
853 TYPE_TI_TEMPLATE (type));
854 return error_mark_node;
857 else if (CLASS_TYPE_P (type)
858 && !CLASSTYPE_USE_TEMPLATE (type)
859 && CLASSTYPE_TEMPLATE_INFO (type)
860 && context && CLASS_TYPE_P (context)
861 && CLASSTYPE_TEMPLATE_INFO (context))
863 /* This is for an explicit specialization of member class
864 template according to [temp.expl.spec/18]:
866 template <> template <class U> class C<int>::D;
868 The context `C<int>' must be an implicit instantiation.
869 Otherwise this is just a member class template declared
870 earlier like:
872 template <> class C<int> { template <class U> class D; };
873 template <> template <class U> class C<int>::D;
875 In the first case, `C<int>::D' is a specialization of `C<T>::D'
876 while in the second case, `C<int>::D' is a primary template
877 and `C<T>::D' may not exist. */
879 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
880 && !COMPLETE_TYPE_P (type))
882 tree t;
883 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
885 if (current_namespace
886 != decl_namespace_context (tmpl))
888 permerror (input_location, "specializing %q#T in different namespace", type);
889 permerror (input_location, " from definition of %q+#D", tmpl);
892 /* Check for invalid specialization after instantiation:
894 template <> template <> class C<int>::D<int>;
895 template <> template <class U> class C<int>::D; */
897 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
898 t; t = TREE_CHAIN (t))
900 tree inst = TREE_VALUE (t);
901 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
903 /* We already have a full specialization of this partial
904 instantiation. Reassign it to the new member
905 specialization template. */
906 spec_entry elt;
907 spec_entry *entry;
908 void **slot;
910 elt.tmpl = most_general_template (tmpl);
911 elt.args = CLASSTYPE_TI_ARGS (inst);
912 elt.spec = inst;
914 htab_remove_elt (type_specializations, &elt);
916 elt.tmpl = tmpl;
917 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
919 slot = htab_find_slot (type_specializations, &elt, INSERT);
920 entry = ggc_alloc_spec_entry ();
921 *entry = elt;
922 *slot = entry;
924 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
925 /* But if we've had an implicit instantiation, that's a
926 problem ([temp.expl.spec]/6). */
927 error ("specialization %qT after instantiation %qT",
928 type, inst);
931 /* Mark TYPE as a specialization. And as a result, we only
932 have one level of template argument for the innermost
933 class template. */
934 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
935 CLASSTYPE_TI_ARGS (type)
936 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
939 else if (processing_specialization)
941 /* Someday C++0x may allow for enum template specialization. */
942 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
943 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
944 pedwarn (input_location, OPT_Wpedantic, "template specialization "
945 "of %qD not allowed by ISO C++", type);
946 else
948 error ("explicit specialization of non-template %qT", type);
949 return error_mark_node;
953 return type;
956 /* Returns nonzero if we can optimize the retrieval of specializations
957 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
958 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
960 static inline bool
961 optimize_specialization_lookup_p (tree tmpl)
963 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
964 && DECL_CLASS_SCOPE_P (tmpl)
965 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
966 parameter. */
967 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
968 /* The optimized lookup depends on the fact that the
969 template arguments for the member function template apply
970 purely to the containing class, which is not true if the
971 containing class is an explicit or partial
972 specialization. */
973 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
974 && !DECL_MEMBER_TEMPLATE_P (tmpl)
975 && !DECL_CONV_FN_P (tmpl)
976 /* It is possible to have a template that is not a member
977 template and is not a member of a template class:
979 template <typename T>
980 struct S { friend A::f(); };
982 Here, the friend function is a template, but the context does
983 not have template information. The optimized lookup relies
984 on having ARGS be the template arguments for both the class
985 and the function template. */
986 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
989 /* Retrieve the specialization (in the sense of [temp.spec] - a
990 specialization is either an instantiation or an explicit
991 specialization) of TMPL for the given template ARGS. If there is
992 no such specialization, return NULL_TREE. The ARGS are a vector of
993 arguments, or a vector of vectors of arguments, in the case of
994 templates with more than one level of parameters.
996 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
997 then we search for a partial specialization matching ARGS. This
998 parameter is ignored if TMPL is not a class template. */
1000 static tree
1001 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1003 if (args == error_mark_node)
1004 return NULL_TREE;
1006 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1008 /* There should be as many levels of arguments as there are
1009 levels of parameters. */
1010 gcc_assert (TMPL_ARGS_DEPTH (args)
1011 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1013 if (optimize_specialization_lookup_p (tmpl))
1015 tree class_template;
1016 tree class_specialization;
1017 VEC(tree,gc) *methods;
1018 tree fns;
1019 int idx;
1021 /* The template arguments actually apply to the containing
1022 class. Find the class specialization with those
1023 arguments. */
1024 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1025 class_specialization
1026 = retrieve_specialization (class_template, args, 0);
1027 if (!class_specialization)
1028 return NULL_TREE;
1029 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1030 for the specialization. */
1031 idx = class_method_index_for_fn (class_specialization, tmpl);
1032 if (idx == -1)
1033 return NULL_TREE;
1034 /* Iterate through the methods with the indicated name, looking
1035 for the one that has an instance of TMPL. */
1036 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1037 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1039 tree fn = OVL_CURRENT (fns);
1040 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1041 /* using-declarations can add base methods to the method vec,
1042 and we don't want those here. */
1043 && DECL_CONTEXT (fn) == class_specialization)
1044 return fn;
1046 return NULL_TREE;
1048 else
1050 spec_entry *found;
1051 spec_entry elt;
1052 htab_t specializations;
1054 elt.tmpl = tmpl;
1055 elt.args = args;
1056 elt.spec = NULL_TREE;
1058 if (DECL_CLASS_TEMPLATE_P (tmpl))
1059 specializations = type_specializations;
1060 else
1061 specializations = decl_specializations;
1063 if (hash == 0)
1064 hash = hash_specialization (&elt);
1065 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1066 if (found)
1067 return found->spec;
1070 return NULL_TREE;
1073 /* Like retrieve_specialization, but for local declarations. */
1075 static tree
1076 retrieve_local_specialization (tree tmpl)
1078 void **slot;
1080 if (local_specializations == NULL)
1081 return NULL_TREE;
1083 slot = pointer_map_contains (local_specializations, tmpl);
1084 return slot ? (tree) *slot : NULL_TREE;
1087 /* Returns nonzero iff DECL is a specialization of TMPL. */
1090 is_specialization_of (tree decl, tree tmpl)
1092 tree t;
1094 if (TREE_CODE (decl) == FUNCTION_DECL)
1096 for (t = decl;
1097 t != NULL_TREE;
1098 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1099 if (t == tmpl)
1100 return 1;
1102 else
1104 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1106 for (t = TREE_TYPE (decl);
1107 t != NULL_TREE;
1108 t = CLASSTYPE_USE_TEMPLATE (t)
1109 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1110 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1111 return 1;
1114 return 0;
1117 /* Returns nonzero iff DECL is a specialization of friend declaration
1118 FRIEND_DECL according to [temp.friend]. */
1120 bool
1121 is_specialization_of_friend (tree decl, tree friend_decl)
1123 bool need_template = true;
1124 int template_depth;
1126 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1127 || TREE_CODE (decl) == TYPE_DECL);
1129 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1130 of a template class, we want to check if DECL is a specialization
1131 if this. */
1132 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1133 && DECL_TEMPLATE_INFO (friend_decl)
1134 && !DECL_USE_TEMPLATE (friend_decl))
1136 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1137 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1138 need_template = false;
1140 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1141 && !PRIMARY_TEMPLATE_P (friend_decl))
1142 need_template = false;
1144 /* There is nothing to do if this is not a template friend. */
1145 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1146 return false;
1148 if (is_specialization_of (decl, friend_decl))
1149 return true;
1151 /* [temp.friend/6]
1152 A member of a class template may be declared to be a friend of a
1153 non-template class. In this case, the corresponding member of
1154 every specialization of the class template is a friend of the
1155 class granting friendship.
1157 For example, given a template friend declaration
1159 template <class T> friend void A<T>::f();
1161 the member function below is considered a friend
1163 template <> struct A<int> {
1164 void f();
1167 For this type of template friend, TEMPLATE_DEPTH below will be
1168 nonzero. To determine if DECL is a friend of FRIEND, we first
1169 check if the enclosing class is a specialization of another. */
1171 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1172 if (template_depth
1173 && DECL_CLASS_SCOPE_P (decl)
1174 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1175 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1177 /* Next, we check the members themselves. In order to handle
1178 a few tricky cases, such as when FRIEND_DECL's are
1180 template <class T> friend void A<T>::g(T t);
1181 template <class T> template <T t> friend void A<T>::h();
1183 and DECL's are
1185 void A<int>::g(int);
1186 template <int> void A<int>::h();
1188 we need to figure out ARGS, the template arguments from
1189 the context of DECL. This is required for template substitution
1190 of `T' in the function parameter of `g' and template parameter
1191 of `h' in the above examples. Here ARGS corresponds to `int'. */
1193 tree context = DECL_CONTEXT (decl);
1194 tree args = NULL_TREE;
1195 int current_depth = 0;
1197 while (current_depth < template_depth)
1199 if (CLASSTYPE_TEMPLATE_INFO (context))
1201 if (current_depth == 0)
1202 args = TYPE_TI_ARGS (context);
1203 else
1204 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1205 current_depth++;
1207 context = TYPE_CONTEXT (context);
1210 if (TREE_CODE (decl) == FUNCTION_DECL)
1212 bool is_template;
1213 tree friend_type;
1214 tree decl_type;
1215 tree friend_args_type;
1216 tree decl_args_type;
1218 /* Make sure that both DECL and FRIEND_DECL are templates or
1219 non-templates. */
1220 is_template = DECL_TEMPLATE_INFO (decl)
1221 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1222 if (need_template ^ is_template)
1223 return false;
1224 else if (is_template)
1226 /* If both are templates, check template parameter list. */
1227 tree friend_parms
1228 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1229 args, tf_none);
1230 if (!comp_template_parms
1231 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1232 friend_parms))
1233 return false;
1235 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1237 else
1238 decl_type = TREE_TYPE (decl);
1240 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1241 tf_none, NULL_TREE);
1242 if (friend_type == error_mark_node)
1243 return false;
1245 /* Check if return types match. */
1246 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1247 return false;
1249 /* Check if function parameter types match, ignoring the
1250 `this' parameter. */
1251 friend_args_type = TYPE_ARG_TYPES (friend_type);
1252 decl_args_type = TYPE_ARG_TYPES (decl_type);
1253 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1254 friend_args_type = TREE_CHAIN (friend_args_type);
1255 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1256 decl_args_type = TREE_CHAIN (decl_args_type);
1258 return compparms (decl_args_type, friend_args_type);
1260 else
1262 /* DECL is a TYPE_DECL */
1263 bool is_template;
1264 tree decl_type = TREE_TYPE (decl);
1266 /* Make sure that both DECL and FRIEND_DECL are templates or
1267 non-templates. */
1268 is_template
1269 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1270 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1272 if (need_template ^ is_template)
1273 return false;
1274 else if (is_template)
1276 tree friend_parms;
1277 /* If both are templates, check the name of the two
1278 TEMPLATE_DECL's first because is_friend didn't. */
1279 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1280 != DECL_NAME (friend_decl))
1281 return false;
1283 /* Now check template parameter list. */
1284 friend_parms
1285 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1286 args, tf_none);
1287 return comp_template_parms
1288 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1289 friend_parms);
1291 else
1292 return (DECL_NAME (decl)
1293 == DECL_NAME (friend_decl));
1296 return false;
1299 /* Register the specialization SPEC as a specialization of TMPL with
1300 the indicated ARGS. IS_FRIEND indicates whether the specialization
1301 is actually just a friend declaration. Returns SPEC, or an
1302 equivalent prior declaration, if available. */
1304 static tree
1305 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1306 hashval_t hash)
1308 tree fn;
1309 void **slot = NULL;
1310 spec_entry elt;
1312 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1314 if (TREE_CODE (spec) == FUNCTION_DECL
1315 && uses_template_parms (DECL_TI_ARGS (spec)))
1316 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1317 register it; we want the corresponding TEMPLATE_DECL instead.
1318 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1319 the more obvious `uses_template_parms (spec)' to avoid problems
1320 with default function arguments. In particular, given
1321 something like this:
1323 template <class T> void f(T t1, T t = T())
1325 the default argument expression is not substituted for in an
1326 instantiation unless and until it is actually needed. */
1327 return spec;
1329 if (optimize_specialization_lookup_p (tmpl))
1330 /* We don't put these specializations in the hash table, but we might
1331 want to give an error about a mismatch. */
1332 fn = retrieve_specialization (tmpl, args, 0);
1333 else
1335 elt.tmpl = tmpl;
1336 elt.args = args;
1337 elt.spec = spec;
1339 if (hash == 0)
1340 hash = hash_specialization (&elt);
1342 slot =
1343 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1344 if (*slot)
1345 fn = ((spec_entry *) *slot)->spec;
1346 else
1347 fn = NULL_TREE;
1350 /* We can sometimes try to re-register a specialization that we've
1351 already got. In particular, regenerate_decl_from_template calls
1352 duplicate_decls which will update the specialization list. But,
1353 we'll still get called again here anyhow. It's more convenient
1354 to simply allow this than to try to prevent it. */
1355 if (fn == spec)
1356 return spec;
1357 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1359 if (DECL_TEMPLATE_INSTANTIATION (fn))
1361 if (DECL_ODR_USED (fn)
1362 || DECL_EXPLICIT_INSTANTIATION (fn))
1364 error ("specialization of %qD after instantiation",
1365 fn);
1366 return error_mark_node;
1368 else
1370 tree clone;
1371 /* This situation should occur only if the first
1372 specialization is an implicit instantiation, the
1373 second is an explicit specialization, and the
1374 implicit instantiation has not yet been used. That
1375 situation can occur if we have implicitly
1376 instantiated a member function and then specialized
1377 it later.
1379 We can also wind up here if a friend declaration that
1380 looked like an instantiation turns out to be a
1381 specialization:
1383 template <class T> void foo(T);
1384 class S { friend void foo<>(int) };
1385 template <> void foo(int);
1387 We transform the existing DECL in place so that any
1388 pointers to it become pointers to the updated
1389 declaration.
1391 If there was a definition for the template, but not
1392 for the specialization, we want this to look as if
1393 there were no definition, and vice versa. */
1394 DECL_INITIAL (fn) = NULL_TREE;
1395 duplicate_decls (spec, fn, is_friend);
1396 /* The call to duplicate_decls will have applied
1397 [temp.expl.spec]:
1399 An explicit specialization of a function template
1400 is inline only if it is explicitly declared to be,
1401 and independently of whether its function template
1404 to the primary function; now copy the inline bits to
1405 the various clones. */
1406 FOR_EACH_CLONE (clone, fn)
1408 DECL_DECLARED_INLINE_P (clone)
1409 = DECL_DECLARED_INLINE_P (fn);
1410 DECL_SOURCE_LOCATION (clone)
1411 = DECL_SOURCE_LOCATION (fn);
1413 check_specialization_namespace (tmpl);
1415 return fn;
1418 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1420 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1421 /* Dup decl failed, but this is a new definition. Set the
1422 line number so any errors match this new
1423 definition. */
1424 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1426 return fn;
1429 else if (fn)
1430 return duplicate_decls (spec, fn, is_friend);
1432 /* A specialization must be declared in the same namespace as the
1433 template it is specializing. */
1434 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1435 && !check_specialization_namespace (tmpl))
1436 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1438 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1440 spec_entry *entry = ggc_alloc_spec_entry ();
1441 gcc_assert (tmpl && args && spec);
1442 *entry = elt;
1443 *slot = entry;
1444 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1445 && PRIMARY_TEMPLATE_P (tmpl)
1446 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1447 /* TMPL is a forward declaration of a template function; keep a list
1448 of all specializations in case we need to reassign them to a friend
1449 template later in tsubst_friend_function. */
1450 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1451 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1454 return spec;
1457 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1458 TMPL and ARGS members, ignores SPEC. */
1460 static int
1461 eq_specializations (const void *p1, const void *p2)
1463 const spec_entry *e1 = (const spec_entry *)p1;
1464 const spec_entry *e2 = (const spec_entry *)p2;
1466 return (e1->tmpl == e2->tmpl
1467 && comp_template_args (e1->args, e2->args));
1470 /* Returns a hash for a template TMPL and template arguments ARGS. */
1472 static hashval_t
1473 hash_tmpl_and_args (tree tmpl, tree args)
1475 hashval_t val = DECL_UID (tmpl);
1476 return iterative_hash_template_arg (args, val);
1479 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1480 ignoring SPEC. */
1482 static hashval_t
1483 hash_specialization (const void *p)
1485 const spec_entry *e = (const spec_entry *)p;
1486 return hash_tmpl_and_args (e->tmpl, e->args);
1489 /* Recursively calculate a hash value for a template argument ARG, for use
1490 in the hash tables of template specializations. */
1492 hashval_t
1493 iterative_hash_template_arg (tree arg, hashval_t val)
1495 unsigned HOST_WIDE_INT i;
1496 enum tree_code code;
1497 char tclass;
1499 if (arg == NULL_TREE)
1500 return iterative_hash_object (arg, val);
1502 if (!TYPE_P (arg))
1503 STRIP_NOPS (arg);
1505 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1506 /* We can get one of these when re-hashing a previous entry in the middle
1507 of substituting into a pack expansion. Just look through it. */
1508 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1510 code = TREE_CODE (arg);
1511 tclass = TREE_CODE_CLASS (code);
1513 val = iterative_hash_object (code, val);
1515 switch (code)
1517 case ERROR_MARK:
1518 return val;
1520 case IDENTIFIER_NODE:
1521 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1523 case TREE_VEC:
1525 int i, len = TREE_VEC_LENGTH (arg);
1526 for (i = 0; i < len; ++i)
1527 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1528 return val;
1531 case TYPE_PACK_EXPANSION:
1532 case EXPR_PACK_EXPANSION:
1533 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1534 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1536 case TYPE_ARGUMENT_PACK:
1537 case NONTYPE_ARGUMENT_PACK:
1538 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1540 case TREE_LIST:
1541 for (; arg; arg = TREE_CHAIN (arg))
1542 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1543 return val;
1545 case OVERLOAD:
1546 for (; arg; arg = OVL_NEXT (arg))
1547 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1548 return val;
1550 case CONSTRUCTOR:
1552 tree field, value;
1553 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1555 val = iterative_hash_template_arg (field, val);
1556 val = iterative_hash_template_arg (value, val);
1558 return val;
1561 case PARM_DECL:
1562 if (!DECL_ARTIFICIAL (arg))
1564 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1565 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1567 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1569 case TARGET_EXPR:
1570 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1572 case PTRMEM_CST:
1573 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1574 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1576 case TEMPLATE_PARM_INDEX:
1577 val = iterative_hash_template_arg
1578 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1579 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1580 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1582 case TRAIT_EXPR:
1583 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1584 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1585 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1587 case BASELINK:
1588 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1589 val);
1590 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1591 val);
1593 case MODOP_EXPR:
1594 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1595 code = TREE_CODE (TREE_OPERAND (arg, 1));
1596 val = iterative_hash_object (code, val);
1597 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1599 case LAMBDA_EXPR:
1600 /* A lambda can't appear in a template arg, but don't crash on
1601 erroneous input. */
1602 gcc_assert (seen_error ());
1603 return val;
1605 case CAST_EXPR:
1606 case IMPLICIT_CONV_EXPR:
1607 case STATIC_CAST_EXPR:
1608 case REINTERPRET_CAST_EXPR:
1609 case CONST_CAST_EXPR:
1610 case DYNAMIC_CAST_EXPR:
1611 case NEW_EXPR:
1612 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1613 /* Now hash operands as usual. */
1614 break;
1616 default:
1617 break;
1620 switch (tclass)
1622 case tcc_type:
1623 if (TYPE_CANONICAL (arg))
1624 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1625 val);
1626 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1627 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1628 /* Otherwise just compare the types during lookup. */
1629 return val;
1631 case tcc_declaration:
1632 case tcc_constant:
1633 return iterative_hash_expr (arg, val);
1635 default:
1636 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1638 unsigned n = cp_tree_operand_length (arg);
1639 for (i = 0; i < n; ++i)
1640 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1641 return val;
1644 gcc_unreachable ();
1645 return 0;
1648 /* Unregister the specialization SPEC as a specialization of TMPL.
1649 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1650 if the SPEC was listed as a specialization of TMPL.
1652 Note that SPEC has been ggc_freed, so we can't look inside it. */
1654 bool
1655 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1657 spec_entry *entry;
1658 spec_entry elt;
1660 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1661 elt.args = TI_ARGS (tinfo);
1662 elt.spec = NULL_TREE;
1664 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1665 if (entry != NULL)
1667 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1668 gcc_assert (new_spec != NULL_TREE);
1669 entry->spec = new_spec;
1670 return 1;
1673 return 0;
1676 /* Like register_specialization, but for local declarations. We are
1677 registering SPEC, an instantiation of TMPL. */
1679 static void
1680 register_local_specialization (tree spec, tree tmpl)
1682 void **slot;
1684 slot = pointer_map_insert (local_specializations, tmpl);
1685 *slot = spec;
1688 /* TYPE is a class type. Returns true if TYPE is an explicitly
1689 specialized class. */
1691 bool
1692 explicit_class_specialization_p (tree type)
1694 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1695 return false;
1696 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1699 /* Print the list of functions at FNS, going through all the overloads
1700 for each element of the list. Alternatively, FNS can not be a
1701 TREE_LIST, in which case it will be printed together with all the
1702 overloads.
1704 MORE and *STR should respectively be FALSE and NULL when the function
1705 is called from the outside. They are used internally on recursive
1706 calls. print_candidates manages the two parameters and leaves NULL
1707 in *STR when it ends. */
1709 static void
1710 print_candidates_1 (tree fns, bool more, const char **str)
1712 tree fn, fn2;
1713 char *spaces = NULL;
1715 for (fn = fns; fn; fn = OVL_NEXT (fn))
1716 if (TREE_CODE (fn) == TREE_LIST)
1718 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1719 print_candidates_1 (TREE_VALUE (fn2),
1720 TREE_CHAIN (fn2) || more, str);
1722 else
1724 tree cand = OVL_CURRENT (fn);
1725 if (!*str)
1727 /* Pick the prefix string. */
1728 if (!more && !OVL_NEXT (fns))
1730 inform (DECL_SOURCE_LOCATION (cand),
1731 "candidate is: %#D", cand);
1732 continue;
1735 *str = _("candidates are:");
1736 spaces = get_spaces (*str);
1738 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1739 *str = spaces ? spaces : *str;
1742 if (!more)
1744 free (spaces);
1745 *str = NULL;
1749 /* Print the list of candidate FNS in an error message. FNS can also
1750 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1752 void
1753 print_candidates (tree fns)
1755 const char *str = NULL;
1756 print_candidates_1 (fns, false, &str);
1757 gcc_assert (str == NULL);
1760 /* Returns the template (one of the functions given by TEMPLATE_ID)
1761 which can be specialized to match the indicated DECL with the
1762 explicit template args given in TEMPLATE_ID. The DECL may be
1763 NULL_TREE if none is available. In that case, the functions in
1764 TEMPLATE_ID are non-members.
1766 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1767 specialization of a member template.
1769 The TEMPLATE_COUNT is the number of references to qualifying
1770 template classes that appeared in the name of the function. See
1771 check_explicit_specialization for a more accurate description.
1773 TSK indicates what kind of template declaration (if any) is being
1774 declared. TSK_TEMPLATE indicates that the declaration given by
1775 DECL, though a FUNCTION_DECL, has template parameters, and is
1776 therefore a template function.
1778 The template args (those explicitly specified and those deduced)
1779 are output in a newly created vector *TARGS_OUT.
1781 If it is impossible to determine the result, an error message is
1782 issued. The error_mark_node is returned to indicate failure. */
1784 static tree
1785 determine_specialization (tree template_id,
1786 tree decl,
1787 tree* targs_out,
1788 int need_member_template,
1789 int template_count,
1790 tmpl_spec_kind tsk)
1792 tree fns;
1793 tree targs;
1794 tree explicit_targs;
1795 tree candidates = NULL_TREE;
1796 /* A TREE_LIST of templates of which DECL may be a specialization.
1797 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1798 corresponding TREE_PURPOSE is the set of template arguments that,
1799 when used to instantiate the template, would produce a function
1800 with the signature of DECL. */
1801 tree templates = NULL_TREE;
1802 int header_count;
1803 cp_binding_level *b;
1805 *targs_out = NULL_TREE;
1807 if (template_id == error_mark_node || decl == error_mark_node)
1808 return error_mark_node;
1810 /* We shouldn't be specializing a member template of an
1811 unspecialized class template; we already gave an error in
1812 check_specialization_scope, now avoid crashing. */
1813 if (template_count && DECL_CLASS_SCOPE_P (decl)
1814 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1816 gcc_assert (errorcount);
1817 return error_mark_node;
1820 fns = TREE_OPERAND (template_id, 0);
1821 explicit_targs = TREE_OPERAND (template_id, 1);
1823 if (fns == error_mark_node)
1824 return error_mark_node;
1826 /* Check for baselinks. */
1827 if (BASELINK_P (fns))
1828 fns = BASELINK_FUNCTIONS (fns);
1830 if (!is_overloaded_fn (fns))
1832 error ("%qD is not a function template", fns);
1833 return error_mark_node;
1836 /* Count the number of template headers specified for this
1837 specialization. */
1838 header_count = 0;
1839 for (b = current_binding_level;
1840 b->kind == sk_template_parms;
1841 b = b->level_chain)
1842 ++header_count;
1844 for (; fns; fns = OVL_NEXT (fns))
1846 tree fn = OVL_CURRENT (fns);
1848 if (TREE_CODE (fn) == TEMPLATE_DECL)
1850 tree decl_arg_types;
1851 tree fn_arg_types;
1852 tree insttype;
1854 /* In case of explicit specialization, we need to check if
1855 the number of template headers appearing in the specialization
1856 is correct. This is usually done in check_explicit_specialization,
1857 but the check done there cannot be exhaustive when specializing
1858 member functions. Consider the following code:
1860 template <> void A<int>::f(int);
1861 template <> template <> void A<int>::f(int);
1863 Assuming that A<int> is not itself an explicit specialization
1864 already, the first line specializes "f" which is a non-template
1865 member function, whilst the second line specializes "f" which
1866 is a template member function. So both lines are syntactically
1867 correct, and check_explicit_specialization does not reject
1868 them.
1870 Here, we can do better, as we are matching the specialization
1871 against the declarations. We count the number of template
1872 headers, and we check if they match TEMPLATE_COUNT + 1
1873 (TEMPLATE_COUNT is the number of qualifying template classes,
1874 plus there must be another header for the member template
1875 itself).
1877 Notice that if header_count is zero, this is not a
1878 specialization but rather a template instantiation, so there
1879 is no check we can perform here. */
1880 if (header_count && header_count != template_count + 1)
1881 continue;
1883 /* Check that the number of template arguments at the
1884 innermost level for DECL is the same as for FN. */
1885 if (current_binding_level->kind == sk_template_parms
1886 && !current_binding_level->explicit_spec_p
1887 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1888 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1889 (current_template_parms))))
1890 continue;
1892 /* DECL might be a specialization of FN. */
1893 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1894 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1896 /* For a non-static member function, we need to make sure
1897 that the const qualification is the same. Since
1898 get_bindings does not try to merge the "this" parameter,
1899 we must do the comparison explicitly. */
1900 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1901 && !same_type_p (TREE_VALUE (fn_arg_types),
1902 TREE_VALUE (decl_arg_types)))
1903 continue;
1905 /* Skip the "this" parameter and, for constructors of
1906 classes with virtual bases, the VTT parameter. A
1907 full specialization of a constructor will have a VTT
1908 parameter, but a template never will. */
1909 decl_arg_types
1910 = skip_artificial_parms_for (decl, decl_arg_types);
1911 fn_arg_types
1912 = skip_artificial_parms_for (fn, fn_arg_types);
1914 /* Function templates cannot be specializations; there are
1915 no partial specializations of functions. Therefore, if
1916 the type of DECL does not match FN, there is no
1917 match. */
1918 if (tsk == tsk_template)
1920 if (compparms (fn_arg_types, decl_arg_types))
1921 candidates = tree_cons (NULL_TREE, fn, candidates);
1922 continue;
1925 /* See whether this function might be a specialization of this
1926 template. Suppress access control because we might be trying
1927 to make this specialization a friend, and we have already done
1928 access control for the declaration of the specialization. */
1929 push_deferring_access_checks (dk_no_check);
1930 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1931 pop_deferring_access_checks ();
1933 if (!targs)
1934 /* We cannot deduce template arguments that when used to
1935 specialize TMPL will produce DECL. */
1936 continue;
1938 /* Make sure that the deduced arguments actually work. */
1939 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1940 if (insttype == error_mark_node)
1941 continue;
1942 fn_arg_types
1943 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1944 if (!compparms (fn_arg_types, decl_arg_types))
1945 continue;
1947 /* Save this template, and the arguments deduced. */
1948 templates = tree_cons (targs, fn, templates);
1950 else if (need_member_template)
1951 /* FN is an ordinary member function, and we need a
1952 specialization of a member template. */
1954 else if (TREE_CODE (fn) != FUNCTION_DECL)
1955 /* We can get IDENTIFIER_NODEs here in certain erroneous
1956 cases. */
1958 else if (!DECL_FUNCTION_MEMBER_P (fn))
1959 /* This is just an ordinary non-member function. Nothing can
1960 be a specialization of that. */
1962 else if (DECL_ARTIFICIAL (fn))
1963 /* Cannot specialize functions that are created implicitly. */
1965 else
1967 tree decl_arg_types;
1969 /* This is an ordinary member function. However, since
1970 we're here, we can assume it's enclosing class is a
1971 template class. For example,
1973 template <typename T> struct S { void f(); };
1974 template <> void S<int>::f() {}
1976 Here, S<int>::f is a non-template, but S<int> is a
1977 template class. If FN has the same type as DECL, we
1978 might be in business. */
1980 if (!DECL_TEMPLATE_INFO (fn))
1981 /* Its enclosing class is an explicit specialization
1982 of a template class. This is not a candidate. */
1983 continue;
1985 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1986 TREE_TYPE (TREE_TYPE (fn))))
1987 /* The return types differ. */
1988 continue;
1990 /* Adjust the type of DECL in case FN is a static member. */
1991 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1992 if (DECL_STATIC_FUNCTION_P (fn)
1993 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1994 decl_arg_types = TREE_CHAIN (decl_arg_types);
1996 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1997 decl_arg_types))
1998 /* They match! */
1999 candidates = tree_cons (NULL_TREE, fn, candidates);
2003 if (templates && TREE_CHAIN (templates))
2005 /* We have:
2007 [temp.expl.spec]
2009 It is possible for a specialization with a given function
2010 signature to be instantiated from more than one function
2011 template. In such cases, explicit specification of the
2012 template arguments must be used to uniquely identify the
2013 function template specialization being specialized.
2015 Note that here, there's no suggestion that we're supposed to
2016 determine which of the candidate templates is most
2017 specialized. However, we, also have:
2019 [temp.func.order]
2021 Partial ordering of overloaded function template
2022 declarations is used in the following contexts to select
2023 the function template to which a function template
2024 specialization refers:
2026 -- when an explicit specialization refers to a function
2027 template.
2029 So, we do use the partial ordering rules, at least for now.
2030 This extension can only serve to make invalid programs valid,
2031 so it's safe. And, there is strong anecdotal evidence that
2032 the committee intended the partial ordering rules to apply;
2033 the EDG front end has that behavior, and John Spicer claims
2034 that the committee simply forgot to delete the wording in
2035 [temp.expl.spec]. */
2036 tree tmpl = most_specialized_instantiation (templates);
2037 if (tmpl != error_mark_node)
2039 templates = tmpl;
2040 TREE_CHAIN (templates) = NULL_TREE;
2044 if (templates == NULL_TREE && candidates == NULL_TREE)
2046 error ("template-id %qD for %q+D does not match any template "
2047 "declaration", template_id, decl);
2048 if (header_count && header_count != template_count + 1)
2049 inform (input_location, "saw %d %<template<>%>, need %d for "
2050 "specializing a member function template",
2051 header_count, template_count + 1);
2052 return error_mark_node;
2054 else if ((templates && TREE_CHAIN (templates))
2055 || (candidates && TREE_CHAIN (candidates))
2056 || (templates && candidates))
2058 error ("ambiguous template specialization %qD for %q+D",
2059 template_id, decl);
2060 candidates = chainon (candidates, templates);
2061 print_candidates (candidates);
2062 return error_mark_node;
2065 /* We have one, and exactly one, match. */
2066 if (candidates)
2068 tree fn = TREE_VALUE (candidates);
2069 *targs_out = copy_node (DECL_TI_ARGS (fn));
2070 /* DECL is a re-declaration or partial instantiation of a template
2071 function. */
2072 if (TREE_CODE (fn) == TEMPLATE_DECL)
2073 return fn;
2074 /* It was a specialization of an ordinary member function in a
2075 template class. */
2076 return DECL_TI_TEMPLATE (fn);
2079 /* It was a specialization of a template. */
2080 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2081 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2083 *targs_out = copy_node (targs);
2084 SET_TMPL_ARGS_LEVEL (*targs_out,
2085 TMPL_ARGS_DEPTH (*targs_out),
2086 TREE_PURPOSE (templates));
2088 else
2089 *targs_out = TREE_PURPOSE (templates);
2090 return TREE_VALUE (templates);
2093 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2094 but with the default argument values filled in from those in the
2095 TMPL_TYPES. */
2097 static tree
2098 copy_default_args_to_explicit_spec_1 (tree spec_types,
2099 tree tmpl_types)
2101 tree new_spec_types;
2103 if (!spec_types)
2104 return NULL_TREE;
2106 if (spec_types == void_list_node)
2107 return void_list_node;
2109 /* Substitute into the rest of the list. */
2110 new_spec_types =
2111 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2112 TREE_CHAIN (tmpl_types));
2114 /* Add the default argument for this parameter. */
2115 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2116 TREE_VALUE (spec_types),
2117 new_spec_types);
2120 /* DECL is an explicit specialization. Replicate default arguments
2121 from the template it specializes. (That way, code like:
2123 template <class T> void f(T = 3);
2124 template <> void f(double);
2125 void g () { f (); }
2127 works, as required.) An alternative approach would be to look up
2128 the correct default arguments at the call-site, but this approach
2129 is consistent with how implicit instantiations are handled. */
2131 static void
2132 copy_default_args_to_explicit_spec (tree decl)
2134 tree tmpl;
2135 tree spec_types;
2136 tree tmpl_types;
2137 tree new_spec_types;
2138 tree old_type;
2139 tree new_type;
2140 tree t;
2141 tree object_type = NULL_TREE;
2142 tree in_charge = NULL_TREE;
2143 tree vtt = NULL_TREE;
2145 /* See if there's anything we need to do. */
2146 tmpl = DECL_TI_TEMPLATE (decl);
2147 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2148 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2149 if (TREE_PURPOSE (t))
2150 break;
2151 if (!t)
2152 return;
2154 old_type = TREE_TYPE (decl);
2155 spec_types = TYPE_ARG_TYPES (old_type);
2157 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2159 /* Remove the this pointer, but remember the object's type for
2160 CV quals. */
2161 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2162 spec_types = TREE_CHAIN (spec_types);
2163 tmpl_types = TREE_CHAIN (tmpl_types);
2165 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2167 /* DECL may contain more parameters than TMPL due to the extra
2168 in-charge parameter in constructors and destructors. */
2169 in_charge = spec_types;
2170 spec_types = TREE_CHAIN (spec_types);
2172 if (DECL_HAS_VTT_PARM_P (decl))
2174 vtt = spec_types;
2175 spec_types = TREE_CHAIN (spec_types);
2179 /* Compute the merged default arguments. */
2180 new_spec_types =
2181 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2183 /* Compute the new FUNCTION_TYPE. */
2184 if (object_type)
2186 if (vtt)
2187 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2188 TREE_VALUE (vtt),
2189 new_spec_types);
2191 if (in_charge)
2192 /* Put the in-charge parameter back. */
2193 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2194 TREE_VALUE (in_charge),
2195 new_spec_types);
2197 new_type = build_method_type_directly (object_type,
2198 TREE_TYPE (old_type),
2199 new_spec_types);
2201 else
2202 new_type = build_function_type (TREE_TYPE (old_type),
2203 new_spec_types);
2204 new_type = cp_build_type_attribute_variant (new_type,
2205 TYPE_ATTRIBUTES (old_type));
2206 new_type = build_exception_variant (new_type,
2207 TYPE_RAISES_EXCEPTIONS (old_type));
2208 TREE_TYPE (decl) = new_type;
2211 /* Return the number of template headers we expect to see for a definition
2212 or specialization of CTYPE or one of its non-template members. */
2215 num_template_headers_for_class (tree ctype)
2217 int template_count = 0;
2218 tree t = ctype;
2219 while (t != NULL_TREE && CLASS_TYPE_P (t))
2221 /* You're supposed to have one `template <...>' for every
2222 template class, but you don't need one for a full
2223 specialization. For example:
2225 template <class T> struct S{};
2226 template <> struct S<int> { void f(); };
2227 void S<int>::f () {}
2229 is correct; there shouldn't be a `template <>' for the
2230 definition of `S<int>::f'. */
2231 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
2232 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
2233 /* T is an explicit (not partial) specialization. All
2234 containing classes must therefore also be explicitly
2235 specialized. */
2236 break;
2237 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
2238 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
2239 template_count += 1;
2241 t = TYPE_MAIN_DECL (t);
2242 t = DECL_CONTEXT (t);
2245 return template_count;
2248 /* Do a simple sanity check on the template headers that precede the
2249 variable declaration DECL. */
2251 void
2252 check_template_variable (tree decl)
2254 tree ctx = CP_DECL_CONTEXT (decl);
2255 int wanted = num_template_headers_for_class (ctx);
2256 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2257 permerror (DECL_SOURCE_LOCATION (decl),
2258 "%qD is not a static data member of a class template", decl);
2259 else if (template_header_count > wanted)
2261 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2262 "too many template headers for %D (should be %d)",
2263 decl, wanted);
2264 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2265 inform (DECL_SOURCE_LOCATION (decl),
2266 "members of an explicitly specialized class are defined "
2267 "without a template header");
2271 /* Check to see if the function just declared, as indicated in
2272 DECLARATOR, and in DECL, is a specialization of a function
2273 template. We may also discover that the declaration is an explicit
2274 instantiation at this point.
2276 Returns DECL, or an equivalent declaration that should be used
2277 instead if all goes well. Issues an error message if something is
2278 amiss. Returns error_mark_node if the error is not easily
2279 recoverable.
2281 FLAGS is a bitmask consisting of the following flags:
2283 2: The function has a definition.
2284 4: The function is a friend.
2286 The TEMPLATE_COUNT is the number of references to qualifying
2287 template classes that appeared in the name of the function. For
2288 example, in
2290 template <class T> struct S { void f(); };
2291 void S<int>::f();
2293 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2294 classes are not counted in the TEMPLATE_COUNT, so that in
2296 template <class T> struct S {};
2297 template <> struct S<int> { void f(); }
2298 template <> void S<int>::f();
2300 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2301 invalid; there should be no template <>.)
2303 If the function is a specialization, it is marked as such via
2304 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2305 is set up correctly, and it is added to the list of specializations
2306 for that template. */
2308 tree
2309 check_explicit_specialization (tree declarator,
2310 tree decl,
2311 int template_count,
2312 int flags)
2314 int have_def = flags & 2;
2315 int is_friend = flags & 4;
2316 int specialization = 0;
2317 int explicit_instantiation = 0;
2318 int member_specialization = 0;
2319 tree ctype = DECL_CLASS_CONTEXT (decl);
2320 tree dname = DECL_NAME (decl);
2321 tmpl_spec_kind tsk;
2323 if (is_friend)
2325 if (!processing_specialization)
2326 tsk = tsk_none;
2327 else
2328 tsk = tsk_excessive_parms;
2330 else
2331 tsk = current_tmpl_spec_kind (template_count);
2333 switch (tsk)
2335 case tsk_none:
2336 if (processing_specialization)
2338 specialization = 1;
2339 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2341 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2343 if (is_friend)
2344 /* This could be something like:
2346 template <class T> void f(T);
2347 class S { friend void f<>(int); } */
2348 specialization = 1;
2349 else
2351 /* This case handles bogus declarations like template <>
2352 template <class T> void f<int>(); */
2354 error ("template-id %qD in declaration of primary template",
2355 declarator);
2356 return decl;
2359 break;
2361 case tsk_invalid_member_spec:
2362 /* The error has already been reported in
2363 check_specialization_scope. */
2364 return error_mark_node;
2366 case tsk_invalid_expl_inst:
2367 error ("template parameter list used in explicit instantiation");
2369 /* Fall through. */
2371 case tsk_expl_inst:
2372 if (have_def)
2373 error ("definition provided for explicit instantiation");
2375 explicit_instantiation = 1;
2376 break;
2378 case tsk_excessive_parms:
2379 case tsk_insufficient_parms:
2380 if (tsk == tsk_excessive_parms)
2381 error ("too many template parameter lists in declaration of %qD",
2382 decl);
2383 else if (template_header_count)
2384 error("too few template parameter lists in declaration of %qD", decl);
2385 else
2386 error("explicit specialization of %qD must be introduced by "
2387 "%<template <>%>", decl);
2389 /* Fall through. */
2390 case tsk_expl_spec:
2391 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2392 if (ctype)
2393 member_specialization = 1;
2394 else
2395 specialization = 1;
2396 break;
2398 case tsk_template:
2399 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2401 /* This case handles bogus declarations like template <>
2402 template <class T> void f<int>(); */
2404 if (uses_template_parms (declarator))
2405 error ("function template partial specialization %qD "
2406 "is not allowed", declarator);
2407 else
2408 error ("template-id %qD in declaration of primary template",
2409 declarator);
2410 return decl;
2413 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2414 /* This is a specialization of a member template, without
2415 specialization the containing class. Something like:
2417 template <class T> struct S {
2418 template <class U> void f (U);
2420 template <> template <class U> void S<int>::f(U) {}
2422 That's a specialization -- but of the entire template. */
2423 specialization = 1;
2424 break;
2426 default:
2427 gcc_unreachable ();
2430 if (specialization || member_specialization)
2432 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2433 for (; t; t = TREE_CHAIN (t))
2434 if (TREE_PURPOSE (t))
2436 permerror (input_location,
2437 "default argument specified in explicit specialization");
2438 break;
2442 if (specialization || member_specialization || explicit_instantiation)
2444 tree tmpl = NULL_TREE;
2445 tree targs = NULL_TREE;
2447 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2448 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2450 tree fns;
2452 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2453 if (ctype)
2454 fns = dname;
2455 else
2457 /* If there is no class context, the explicit instantiation
2458 must be at namespace scope. */
2459 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2461 /* Find the namespace binding, using the declaration
2462 context. */
2463 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2464 false, true);
2465 if (fns == error_mark_node || !is_overloaded_fn (fns))
2467 error ("%qD is not a template function", dname);
2468 fns = error_mark_node;
2470 else
2472 tree fn = OVL_CURRENT (fns);
2473 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2474 CP_DECL_CONTEXT (fn)))
2475 error ("%qD is not declared in %qD",
2476 decl, current_namespace);
2480 declarator = lookup_template_function (fns, NULL_TREE);
2483 if (declarator == error_mark_node)
2484 return error_mark_node;
2486 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2488 if (!explicit_instantiation)
2489 /* A specialization in class scope. This is invalid,
2490 but the error will already have been flagged by
2491 check_specialization_scope. */
2492 return error_mark_node;
2493 else
2495 /* It's not valid to write an explicit instantiation in
2496 class scope, e.g.:
2498 class C { template void f(); }
2500 This case is caught by the parser. However, on
2501 something like:
2503 template class C { void f(); };
2505 (which is invalid) we can get here. The error will be
2506 issued later. */
2510 return decl;
2512 else if (ctype != NULL_TREE
2513 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2514 IDENTIFIER_NODE))
2516 /* Find the list of functions in ctype that have the same
2517 name as the declared function. */
2518 tree name = TREE_OPERAND (declarator, 0);
2519 tree fns = NULL_TREE;
2520 int idx;
2522 if (constructor_name_p (name, ctype))
2524 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2526 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2527 : !CLASSTYPE_DESTRUCTORS (ctype))
2529 /* From [temp.expl.spec]:
2531 If such an explicit specialization for the member
2532 of a class template names an implicitly-declared
2533 special member function (clause _special_), the
2534 program is ill-formed.
2536 Similar language is found in [temp.explicit]. */
2537 error ("specialization of implicitly-declared special member function");
2538 return error_mark_node;
2541 name = is_constructor ? ctor_identifier : dtor_identifier;
2544 if (!DECL_CONV_FN_P (decl))
2546 idx = lookup_fnfields_1 (ctype, name);
2547 if (idx >= 0)
2548 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2550 else
2552 VEC(tree,gc) *methods;
2553 tree ovl;
2555 /* For a type-conversion operator, we cannot do a
2556 name-based lookup. We might be looking for `operator
2557 int' which will be a specialization of `operator T'.
2558 So, we find *all* the conversion operators, and then
2559 select from them. */
2560 fns = NULL_TREE;
2562 methods = CLASSTYPE_METHOD_VEC (ctype);
2563 if (methods)
2564 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2565 VEC_iterate (tree, methods, idx, ovl);
2566 ++idx)
2568 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2569 /* There are no more conversion functions. */
2570 break;
2572 /* Glue all these conversion functions together
2573 with those we already have. */
2574 for (; ovl; ovl = OVL_NEXT (ovl))
2575 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2579 if (fns == NULL_TREE)
2581 error ("no member function %qD declared in %qT", name, ctype);
2582 return error_mark_node;
2584 else
2585 TREE_OPERAND (declarator, 0) = fns;
2588 /* Figure out what exactly is being specialized at this point.
2589 Note that for an explicit instantiation, even one for a
2590 member function, we cannot tell apriori whether the
2591 instantiation is for a member template, or just a member
2592 function of a template class. Even if a member template is
2593 being instantiated, the member template arguments may be
2594 elided if they can be deduced from the rest of the
2595 declaration. */
2596 tmpl = determine_specialization (declarator, decl,
2597 &targs,
2598 member_specialization,
2599 template_count,
2600 tsk);
2602 if (!tmpl || tmpl == error_mark_node)
2603 /* We couldn't figure out what this declaration was
2604 specializing. */
2605 return error_mark_node;
2606 else
2608 tree gen_tmpl = most_general_template (tmpl);
2610 if (explicit_instantiation)
2612 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2613 is done by do_decl_instantiation later. */
2615 int arg_depth = TMPL_ARGS_DEPTH (targs);
2616 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2618 if (arg_depth > parm_depth)
2620 /* If TMPL is not the most general template (for
2621 example, if TMPL is a friend template that is
2622 injected into namespace scope), then there will
2623 be too many levels of TARGS. Remove some of them
2624 here. */
2625 int i;
2626 tree new_targs;
2628 new_targs = make_tree_vec (parm_depth);
2629 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2630 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2631 = TREE_VEC_ELT (targs, i);
2632 targs = new_targs;
2635 return instantiate_template (tmpl, targs, tf_error);
2638 /* If we thought that the DECL was a member function, but it
2639 turns out to be specializing a static member function,
2640 make DECL a static member function as well. */
2641 if (DECL_STATIC_FUNCTION_P (tmpl)
2642 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2643 revert_static_member_fn (decl);
2645 /* If this is a specialization of a member template of a
2646 template class, we want to return the TEMPLATE_DECL, not
2647 the specialization of it. */
2648 if (tsk == tsk_template)
2650 tree result = DECL_TEMPLATE_RESULT (tmpl);
2651 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2652 DECL_INITIAL (result) = NULL_TREE;
2653 if (have_def)
2655 tree parm;
2656 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2657 DECL_SOURCE_LOCATION (result)
2658 = DECL_SOURCE_LOCATION (decl);
2659 /* We want to use the argument list specified in the
2660 definition, not in the original declaration. */
2661 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2662 for (parm = DECL_ARGUMENTS (result); parm;
2663 parm = DECL_CHAIN (parm))
2664 DECL_CONTEXT (parm) = result;
2666 return register_specialization (tmpl, gen_tmpl, targs,
2667 is_friend, 0);
2670 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2671 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2673 /* Inherit default function arguments from the template
2674 DECL is specializing. */
2675 copy_default_args_to_explicit_spec (decl);
2677 /* This specialization has the same protection as the
2678 template it specializes. */
2679 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2680 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2682 /* 7.1.1-1 [dcl.stc]
2684 A storage-class-specifier shall not be specified in an
2685 explicit specialization...
2687 The parser rejects these, so unless action is taken here,
2688 explicit function specializations will always appear with
2689 global linkage.
2691 The action recommended by the C++ CWG in response to C++
2692 defect report 605 is to make the storage class and linkage
2693 of the explicit specialization match the templated function:
2695 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2697 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2699 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2700 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2702 /* This specialization has the same linkage and visibility as
2703 the function template it specializes. */
2704 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2705 if (! TREE_PUBLIC (decl))
2707 DECL_INTERFACE_KNOWN (decl) = 1;
2708 DECL_NOT_REALLY_EXTERN (decl) = 1;
2710 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2711 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2713 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2714 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2718 /* If DECL is a friend declaration, declared using an
2719 unqualified name, the namespace associated with DECL may
2720 have been set incorrectly. For example, in:
2722 template <typename T> void f(T);
2723 namespace N {
2724 struct S { friend void f<int>(int); }
2727 we will have set the DECL_CONTEXT for the friend
2728 declaration to N, rather than to the global namespace. */
2729 if (DECL_NAMESPACE_SCOPE_P (decl))
2730 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2732 if (is_friend && !have_def)
2733 /* This is not really a declaration of a specialization.
2734 It's just the name of an instantiation. But, it's not
2735 a request for an instantiation, either. */
2736 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2737 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2738 /* This is indeed a specialization. In case of constructors
2739 and destructors, we need in-charge and not-in-charge
2740 versions in V3 ABI. */
2741 clone_function_decl (decl, /*update_method_vec_p=*/0);
2743 /* Register this specialization so that we can find it
2744 again. */
2745 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2749 return decl;
2752 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2753 parameters. These are represented in the same format used for
2754 DECL_TEMPLATE_PARMS. */
2757 comp_template_parms (const_tree parms1, const_tree parms2)
2759 const_tree p1;
2760 const_tree p2;
2762 if (parms1 == parms2)
2763 return 1;
2765 for (p1 = parms1, p2 = parms2;
2766 p1 != NULL_TREE && p2 != NULL_TREE;
2767 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2769 tree t1 = TREE_VALUE (p1);
2770 tree t2 = TREE_VALUE (p2);
2771 int i;
2773 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2774 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2776 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2777 return 0;
2779 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2781 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2782 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2784 /* If either of the template parameters are invalid, assume
2785 they match for the sake of error recovery. */
2786 if (parm1 == error_mark_node || parm2 == error_mark_node)
2787 return 1;
2789 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2790 return 0;
2792 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2793 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2794 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2795 continue;
2796 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2797 return 0;
2801 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2802 /* One set of parameters has more parameters lists than the
2803 other. */
2804 return 0;
2806 return 1;
2809 /* Determine whether PARM is a parameter pack. */
2811 bool
2812 template_parameter_pack_p (const_tree parm)
2814 /* Determine if we have a non-type template parameter pack. */
2815 if (TREE_CODE (parm) == PARM_DECL)
2816 return (DECL_TEMPLATE_PARM_P (parm)
2817 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2818 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2819 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2821 /* If this is a list of template parameters, we could get a
2822 TYPE_DECL or a TEMPLATE_DECL. */
2823 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2824 parm = TREE_TYPE (parm);
2826 /* Otherwise it must be a type template parameter. */
2827 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2828 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2829 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2832 /* Determine if T is a function parameter pack. */
2834 bool
2835 function_parameter_pack_p (const_tree t)
2837 if (t && TREE_CODE (t) == PARM_DECL)
2838 return FUNCTION_PARAMETER_PACK_P (t);
2839 return false;
2842 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2843 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2845 tree
2846 get_function_template_decl (const_tree primary_func_tmpl_inst)
2848 if (! primary_func_tmpl_inst
2849 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2850 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2851 return NULL;
2853 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2856 /* Return true iff the function parameter PARAM_DECL was expanded
2857 from the function parameter pack PACK. */
2859 bool
2860 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2862 if (DECL_ARTIFICIAL (param_decl)
2863 || !function_parameter_pack_p (pack))
2864 return false;
2866 /* The parameter pack and its pack arguments have the same
2867 DECL_PARM_INDEX. */
2868 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2871 /* Determine whether ARGS describes a variadic template args list,
2872 i.e., one that is terminated by a template argument pack. */
2874 static bool
2875 template_args_variadic_p (tree args)
2877 int nargs;
2878 tree last_parm;
2880 if (args == NULL_TREE)
2881 return false;
2883 args = INNERMOST_TEMPLATE_ARGS (args);
2884 nargs = TREE_VEC_LENGTH (args);
2886 if (nargs == 0)
2887 return false;
2889 last_parm = TREE_VEC_ELT (args, nargs - 1);
2891 return ARGUMENT_PACK_P (last_parm);
2894 /* Generate a new name for the parameter pack name NAME (an
2895 IDENTIFIER_NODE) that incorporates its */
2897 static tree
2898 make_ith_pack_parameter_name (tree name, int i)
2900 /* Munge the name to include the parameter index. */
2901 #define NUMBUF_LEN 128
2902 char numbuf[NUMBUF_LEN];
2903 char* newname;
2904 int newname_len;
2906 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2907 newname_len = IDENTIFIER_LENGTH (name)
2908 + strlen (numbuf) + 2;
2909 newname = (char*)alloca (newname_len);
2910 snprintf (newname, newname_len,
2911 "%s#%i", IDENTIFIER_POINTER (name), i);
2912 return get_identifier (newname);
2915 /* Return true if T is a primary function, class or alias template
2916 instantiation. */
2918 bool
2919 primary_template_instantiation_p (const_tree t)
2921 if (!t)
2922 return false;
2924 if (TREE_CODE (t) == FUNCTION_DECL)
2925 return DECL_LANG_SPECIFIC (t)
2926 && DECL_TEMPLATE_INSTANTIATION (t)
2927 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2928 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2929 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2930 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2931 else if (TYPE_P (t)
2932 && TYPE_TEMPLATE_INFO (t)
2933 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
2934 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t)))
2935 return true;
2936 return false;
2939 /* Return true if PARM is a template template parameter. */
2941 bool
2942 template_template_parameter_p (const_tree parm)
2944 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2947 /* Return true iff PARM is a DECL representing a type template
2948 parameter. */
2950 bool
2951 template_type_parameter_p (const_tree parm)
2953 return (parm
2954 && (TREE_CODE (parm) == TYPE_DECL
2955 || TREE_CODE (parm) == TEMPLATE_DECL)
2956 && DECL_TEMPLATE_PARM_P (parm));
2959 /* Return the template parameters of T if T is a
2960 primary template instantiation, NULL otherwise. */
2962 tree
2963 get_primary_template_innermost_parameters (const_tree t)
2965 tree parms = NULL, template_info = NULL;
2967 if ((template_info = get_template_info (t))
2968 && primary_template_instantiation_p (t))
2969 parms = INNERMOST_TEMPLATE_PARMS
2970 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2972 return parms;
2975 /* Return the template parameters of the LEVELth level from the full list
2976 of template parameters PARMS. */
2978 tree
2979 get_template_parms_at_level (tree parms, int level)
2981 tree p;
2982 if (!parms
2983 || TREE_CODE (parms) != TREE_LIST
2984 || level > TMPL_PARMS_DEPTH (parms))
2985 return NULL_TREE;
2987 for (p = parms; p; p = TREE_CHAIN (p))
2988 if (TMPL_PARMS_DEPTH (p) == level)
2989 return p;
2991 return NULL_TREE;
2994 /* Returns the template arguments of T if T is a template instantiation,
2995 NULL otherwise. */
2997 tree
2998 get_template_innermost_arguments (const_tree t)
3000 tree args = NULL, template_info = NULL;
3002 if ((template_info = get_template_info (t))
3003 && TI_ARGS (template_info))
3004 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3006 return args;
3009 /* Return the argument pack elements of T if T is a template argument pack,
3010 NULL otherwise. */
3012 tree
3013 get_template_argument_pack_elems (const_tree t)
3015 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3016 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3017 return NULL;
3019 return ARGUMENT_PACK_ARGS (t);
3022 /* Structure used to track the progress of find_parameter_packs_r. */
3023 struct find_parameter_pack_data
3025 /* TREE_LIST that will contain all of the parameter packs found by
3026 the traversal. */
3027 tree* parameter_packs;
3029 /* Set of AST nodes that have been visited by the traversal. */
3030 struct pointer_set_t *visited;
3033 /* Identifies all of the argument packs that occur in a template
3034 argument and appends them to the TREE_LIST inside DATA, which is a
3035 find_parameter_pack_data structure. This is a subroutine of
3036 make_pack_expansion and uses_parameter_packs. */
3037 static tree
3038 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3040 tree t = *tp;
3041 struct find_parameter_pack_data* ppd =
3042 (struct find_parameter_pack_data*)data;
3043 bool parameter_pack_p = false;
3045 /* Handle type aliases/typedefs. */
3046 if (TYPE_P (t)
3047 && TYPE_NAME (t)
3048 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
3049 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3051 if (TYPE_TEMPLATE_INFO (t))
3052 cp_walk_tree (&TYPE_TI_ARGS (t),
3053 &find_parameter_packs_r,
3054 ppd, ppd->visited);
3055 *walk_subtrees = 0;
3056 return NULL_TREE;
3059 /* Identify whether this is a parameter pack or not. */
3060 switch (TREE_CODE (t))
3062 case TEMPLATE_PARM_INDEX:
3063 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3064 parameter_pack_p = true;
3065 break;
3067 case TEMPLATE_TYPE_PARM:
3068 t = TYPE_MAIN_VARIANT (t);
3069 case TEMPLATE_TEMPLATE_PARM:
3070 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3071 parameter_pack_p = true;
3072 break;
3074 case PARM_DECL:
3075 if (FUNCTION_PARAMETER_PACK_P (t))
3077 /* We don't want to walk into the type of a PARM_DECL,
3078 because we don't want to see the type parameter pack. */
3079 *walk_subtrees = 0;
3080 parameter_pack_p = true;
3082 break;
3084 case BASES:
3085 parameter_pack_p = true;
3086 break;
3087 default:
3088 /* Not a parameter pack. */
3089 break;
3092 if (parameter_pack_p)
3094 /* Add this parameter pack to the list. */
3095 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3098 if (TYPE_P (t))
3099 cp_walk_tree (&TYPE_CONTEXT (t),
3100 &find_parameter_packs_r, ppd, ppd->visited);
3102 /* This switch statement will return immediately if we don't find a
3103 parameter pack. */
3104 switch (TREE_CODE (t))
3106 case TEMPLATE_PARM_INDEX:
3107 return NULL_TREE;
3109 case BOUND_TEMPLATE_TEMPLATE_PARM:
3110 /* Check the template itself. */
3111 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3112 &find_parameter_packs_r, ppd, ppd->visited);
3113 /* Check the template arguments. */
3114 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3115 ppd->visited);
3116 *walk_subtrees = 0;
3117 return NULL_TREE;
3119 case TEMPLATE_TYPE_PARM:
3120 case TEMPLATE_TEMPLATE_PARM:
3121 return NULL_TREE;
3123 case PARM_DECL:
3124 return NULL_TREE;
3126 case RECORD_TYPE:
3127 if (TYPE_PTRMEMFUNC_P (t))
3128 return NULL_TREE;
3129 /* Fall through. */
3131 case UNION_TYPE:
3132 case ENUMERAL_TYPE:
3133 if (TYPE_TEMPLATE_INFO (t))
3134 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3135 &find_parameter_packs_r, ppd, ppd->visited);
3137 *walk_subtrees = 0;
3138 return NULL_TREE;
3140 case CONSTRUCTOR:
3141 case TEMPLATE_DECL:
3142 cp_walk_tree (&TREE_TYPE (t),
3143 &find_parameter_packs_r, ppd, ppd->visited);
3144 return NULL_TREE;
3146 case TYPENAME_TYPE:
3147 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3148 ppd, ppd->visited);
3149 *walk_subtrees = 0;
3150 return NULL_TREE;
3152 case TYPE_PACK_EXPANSION:
3153 case EXPR_PACK_EXPANSION:
3154 *walk_subtrees = 0;
3155 return NULL_TREE;
3157 case INTEGER_TYPE:
3158 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3159 ppd, ppd->visited);
3160 *walk_subtrees = 0;
3161 return NULL_TREE;
3163 case IDENTIFIER_NODE:
3164 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3165 ppd->visited);
3166 *walk_subtrees = 0;
3167 return NULL_TREE;
3169 default:
3170 return NULL_TREE;
3173 return NULL_TREE;
3176 /* Determines if the expression or type T uses any parameter packs. */
3177 bool
3178 uses_parameter_packs (tree t)
3180 tree parameter_packs = NULL_TREE;
3181 struct find_parameter_pack_data ppd;
3182 ppd.parameter_packs = &parameter_packs;
3183 ppd.visited = pointer_set_create ();
3184 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3185 pointer_set_destroy (ppd.visited);
3186 return parameter_packs != NULL_TREE;
3189 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3190 representation a base-class initializer into a parameter pack
3191 expansion. If all goes well, the resulting node will be an
3192 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3193 respectively. */
3194 tree
3195 make_pack_expansion (tree arg)
3197 tree result;
3198 tree parameter_packs = NULL_TREE;
3199 bool for_types = false;
3200 struct find_parameter_pack_data ppd;
3202 if (!arg || arg == error_mark_node)
3203 return arg;
3205 if (TREE_CODE (arg) == TREE_LIST)
3207 /* The only time we will see a TREE_LIST here is for a base
3208 class initializer. In this case, the TREE_PURPOSE will be a
3209 _TYPE node (representing the base class expansion we're
3210 initializing) and the TREE_VALUE will be a TREE_LIST
3211 containing the initialization arguments.
3213 The resulting expansion looks somewhat different from most
3214 expansions. Rather than returning just one _EXPANSION, we
3215 return a TREE_LIST whose TREE_PURPOSE is a
3216 TYPE_PACK_EXPANSION containing the bases that will be
3217 initialized. The TREE_VALUE will be identical to the
3218 original TREE_VALUE, which is a list of arguments that will
3219 be passed to each base. We do not introduce any new pack
3220 expansion nodes into the TREE_VALUE (although it is possible
3221 that some already exist), because the TREE_PURPOSE and
3222 TREE_VALUE all need to be expanded together with the same
3223 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3224 resulting TREE_PURPOSE will mention the parameter packs in
3225 both the bases and the arguments to the bases. */
3226 tree purpose;
3227 tree value;
3228 tree parameter_packs = NULL_TREE;
3230 /* Determine which parameter packs will be used by the base
3231 class expansion. */
3232 ppd.visited = pointer_set_create ();
3233 ppd.parameter_packs = &parameter_packs;
3234 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3235 &ppd, ppd.visited);
3237 if (parameter_packs == NULL_TREE)
3239 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3240 pointer_set_destroy (ppd.visited);
3241 return error_mark_node;
3244 if (TREE_VALUE (arg) != void_type_node)
3246 /* Collect the sets of parameter packs used in each of the
3247 initialization arguments. */
3248 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3250 /* Determine which parameter packs will be expanded in this
3251 argument. */
3252 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3253 &ppd, ppd.visited);
3257 pointer_set_destroy (ppd.visited);
3259 /* Create the pack expansion type for the base type. */
3260 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3261 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3262 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3264 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3265 they will rarely be compared to anything. */
3266 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3268 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3271 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3272 for_types = true;
3274 /* Build the PACK_EXPANSION_* node. */
3275 result = for_types
3276 ? cxx_make_type (TYPE_PACK_EXPANSION)
3277 : make_node (EXPR_PACK_EXPANSION);
3278 SET_PACK_EXPANSION_PATTERN (result, arg);
3279 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3281 /* Propagate type and const-expression information. */
3282 TREE_TYPE (result) = TREE_TYPE (arg);
3283 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3285 else
3286 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3287 they will rarely be compared to anything. */
3288 SET_TYPE_STRUCTURAL_EQUALITY (result);
3290 /* Determine which parameter packs will be expanded. */
3291 ppd.parameter_packs = &parameter_packs;
3292 ppd.visited = pointer_set_create ();
3293 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3294 pointer_set_destroy (ppd.visited);
3296 /* Make sure we found some parameter packs. */
3297 if (parameter_packs == NULL_TREE)
3299 if (TYPE_P (arg))
3300 error ("expansion pattern %<%T%> contains no argument packs", arg);
3301 else
3302 error ("expansion pattern %<%E%> contains no argument packs", arg);
3303 return error_mark_node;
3305 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3307 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3309 return result;
3312 /* Checks T for any "bare" parameter packs, which have not yet been
3313 expanded, and issues an error if any are found. This operation can
3314 only be done on full expressions or types (e.g., an expression
3315 statement, "if" condition, etc.), because we could have expressions like:
3317 foo(f(g(h(args)))...)
3319 where "args" is a parameter pack. check_for_bare_parameter_packs
3320 should not be called for the subexpressions args, h(args),
3321 g(h(args)), or f(g(h(args))), because we would produce erroneous
3322 error messages.
3324 Returns TRUE and emits an error if there were bare parameter packs,
3325 returns FALSE otherwise. */
3326 bool
3327 check_for_bare_parameter_packs (tree t)
3329 tree parameter_packs = NULL_TREE;
3330 struct find_parameter_pack_data ppd;
3332 if (!processing_template_decl || !t || t == error_mark_node)
3333 return false;
3335 if (TREE_CODE (t) == TYPE_DECL)
3336 t = TREE_TYPE (t);
3338 ppd.parameter_packs = &parameter_packs;
3339 ppd.visited = pointer_set_create ();
3340 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3341 pointer_set_destroy (ppd.visited);
3343 if (parameter_packs)
3345 error ("parameter packs not expanded with %<...%>:");
3346 while (parameter_packs)
3348 tree pack = TREE_VALUE (parameter_packs);
3349 tree name = NULL_TREE;
3351 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3352 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3353 name = TYPE_NAME (pack);
3354 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3355 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3356 else
3357 name = DECL_NAME (pack);
3359 if (name)
3360 inform (input_location, " %qD", name);
3361 else
3362 inform (input_location, " <anonymous>");
3364 parameter_packs = TREE_CHAIN (parameter_packs);
3367 return true;
3370 return false;
3373 /* Expand any parameter packs that occur in the template arguments in
3374 ARGS. */
3375 tree
3376 expand_template_argument_pack (tree args)
3378 tree result_args = NULL_TREE;
3379 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3380 int num_result_args = -1;
3381 int non_default_args_count = -1;
3383 /* First, determine if we need to expand anything, and the number of
3384 slots we'll need. */
3385 for (in_arg = 0; in_arg < nargs; ++in_arg)
3387 tree arg = TREE_VEC_ELT (args, in_arg);
3388 if (arg == NULL_TREE)
3389 return args;
3390 if (ARGUMENT_PACK_P (arg))
3392 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3393 if (num_result_args < 0)
3394 num_result_args = in_arg + num_packed;
3395 else
3396 num_result_args += num_packed;
3398 else
3400 if (num_result_args >= 0)
3401 num_result_args++;
3405 /* If no expansion is necessary, we're done. */
3406 if (num_result_args < 0)
3407 return args;
3409 /* Expand arguments. */
3410 result_args = make_tree_vec (num_result_args);
3411 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3412 non_default_args_count =
3413 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3414 for (in_arg = 0; in_arg < nargs; ++in_arg)
3416 tree arg = TREE_VEC_ELT (args, in_arg);
3417 if (ARGUMENT_PACK_P (arg))
3419 tree packed = ARGUMENT_PACK_ARGS (arg);
3420 int i, num_packed = TREE_VEC_LENGTH (packed);
3421 for (i = 0; i < num_packed; ++i, ++out_arg)
3422 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3423 if (non_default_args_count > 0)
3424 non_default_args_count += num_packed;
3426 else
3428 TREE_VEC_ELT (result_args, out_arg) = arg;
3429 ++out_arg;
3432 if (non_default_args_count >= 0)
3433 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3434 return result_args;
3437 /* Checks if DECL shadows a template parameter.
3439 [temp.local]: A template-parameter shall not be redeclared within its
3440 scope (including nested scopes).
3442 Emits an error and returns TRUE if the DECL shadows a parameter,
3443 returns FALSE otherwise. */
3445 bool
3446 check_template_shadow (tree decl)
3448 tree olddecl;
3450 /* If we're not in a template, we can't possibly shadow a template
3451 parameter. */
3452 if (!current_template_parms)
3453 return true;
3455 /* Figure out what we're shadowing. */
3456 if (TREE_CODE (decl) == OVERLOAD)
3457 decl = OVL_CURRENT (decl);
3458 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3460 /* If there's no previous binding for this name, we're not shadowing
3461 anything, let alone a template parameter. */
3462 if (!olddecl)
3463 return true;
3465 /* If we're not shadowing a template parameter, we're done. Note
3466 that OLDDECL might be an OVERLOAD (or perhaps even an
3467 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3468 node. */
3469 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3470 return true;
3472 /* We check for decl != olddecl to avoid bogus errors for using a
3473 name inside a class. We check TPFI to avoid duplicate errors for
3474 inline member templates. */
3475 if (decl == olddecl
3476 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3477 return true;
3479 error ("declaration of %q+#D", decl);
3480 error (" shadows template parm %q+#D", olddecl);
3481 return false;
3484 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3485 ORIG_LEVEL, DECL, and TYPE. */
3487 static tree
3488 build_template_parm_index (int index,
3489 int level,
3490 int orig_level,
3491 tree decl,
3492 tree type)
3494 tree t = make_node (TEMPLATE_PARM_INDEX);
3495 TEMPLATE_PARM_IDX (t) = index;
3496 TEMPLATE_PARM_LEVEL (t) = level;
3497 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3498 TEMPLATE_PARM_DECL (t) = decl;
3499 TREE_TYPE (t) = type;
3500 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3501 TREE_READONLY (t) = TREE_READONLY (decl);
3503 return t;
3506 /* Find the canonical type parameter for the given template type
3507 parameter. Returns the canonical type parameter, which may be TYPE
3508 if no such parameter existed. */
3510 static tree
3511 canonical_type_parameter (tree type)
3513 tree list;
3514 int idx = TEMPLATE_TYPE_IDX (type);
3515 if (!canonical_template_parms)
3516 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3518 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3519 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3521 list = VEC_index (tree, canonical_template_parms, idx);
3522 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3523 list = TREE_CHAIN (list);
3525 if (list)
3526 return TREE_VALUE (list);
3527 else
3529 VEC_replace(tree, canonical_template_parms, idx,
3530 tree_cons (NULL_TREE, type,
3531 VEC_index (tree, canonical_template_parms, idx)));
3532 return type;
3536 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3537 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3538 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3539 new one is created. */
3541 static tree
3542 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3543 tsubst_flags_t complain)
3545 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3546 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3547 != TEMPLATE_PARM_LEVEL (index) - levels)
3548 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3550 tree orig_decl = TEMPLATE_PARM_DECL (index);
3551 tree decl, t;
3553 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3554 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3555 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3556 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3557 DECL_ARTIFICIAL (decl) = 1;
3558 SET_DECL_TEMPLATE_PARM_P (decl);
3560 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3561 TEMPLATE_PARM_LEVEL (index) - levels,
3562 TEMPLATE_PARM_ORIG_LEVEL (index),
3563 decl, type);
3564 TEMPLATE_PARM_DESCENDANTS (index) = t;
3565 TEMPLATE_PARM_PARAMETER_PACK (t)
3566 = TEMPLATE_PARM_PARAMETER_PACK (index);
3568 /* Template template parameters need this. */
3569 if (TREE_CODE (decl) == TEMPLATE_DECL)
3570 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3571 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3572 args, complain);
3575 return TEMPLATE_PARM_DESCENDANTS (index);
3578 /* Process information from new template parameter PARM and append it
3579 to the LIST being built. This new parameter is a non-type
3580 parameter iff IS_NON_TYPE is true. This new parameter is a
3581 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3582 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3583 parameter list PARM belongs to. This is used used to create a
3584 proper canonical type for the type of PARM that is to be created,
3585 iff PARM is a type. If the size is not known, this parameter shall
3586 be set to 0. */
3588 tree
3589 process_template_parm (tree list, location_t parm_loc, tree parm,
3590 bool is_non_type, bool is_parameter_pack)
3592 tree decl = 0;
3593 tree defval;
3594 tree err_parm_list;
3595 int idx = 0;
3597 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3598 defval = TREE_PURPOSE (parm);
3600 if (list)
3602 tree p = tree_last (list);
3604 if (p && TREE_VALUE (p) != error_mark_node)
3606 p = TREE_VALUE (p);
3607 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3608 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3609 else
3610 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3613 ++idx;
3615 else
3616 idx = 0;
3618 if (is_non_type)
3620 parm = TREE_VALUE (parm);
3622 SET_DECL_TEMPLATE_PARM_P (parm);
3624 if (TREE_TYPE (parm) == error_mark_node)
3626 err_parm_list = build_tree_list (defval, parm);
3627 TREE_VALUE (err_parm_list) = error_mark_node;
3628 return chainon (list, err_parm_list);
3630 else
3632 /* [temp.param]
3634 The top-level cv-qualifiers on the template-parameter are
3635 ignored when determining its type. */
3636 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3637 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3639 err_parm_list = build_tree_list (defval, parm);
3640 TREE_VALUE (err_parm_list) = error_mark_node;
3641 return chainon (list, err_parm_list);
3644 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3646 /* This template parameter is not a parameter pack, but it
3647 should be. Complain about "bare" parameter packs. */
3648 check_for_bare_parameter_packs (TREE_TYPE (parm));
3650 /* Recover by calling this a parameter pack. */
3651 is_parameter_pack = true;
3655 /* A template parameter is not modifiable. */
3656 TREE_CONSTANT (parm) = 1;
3657 TREE_READONLY (parm) = 1;
3658 decl = build_decl (parm_loc,
3659 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3660 TREE_CONSTANT (decl) = 1;
3661 TREE_READONLY (decl) = 1;
3662 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3663 = build_template_parm_index (idx, processing_template_decl,
3664 processing_template_decl,
3665 decl, TREE_TYPE (parm));
3667 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3668 = is_parameter_pack;
3670 else
3672 tree t;
3673 parm = TREE_VALUE (TREE_VALUE (parm));
3675 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3677 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3678 /* This is for distinguishing between real templates and template
3679 template parameters */
3680 TREE_TYPE (parm) = t;
3681 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3682 decl = parm;
3684 else
3686 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3687 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3688 decl = build_decl (parm_loc,
3689 TYPE_DECL, parm, t);
3692 TYPE_NAME (t) = decl;
3693 TYPE_STUB_DECL (t) = decl;
3694 parm = decl;
3695 TEMPLATE_TYPE_PARM_INDEX (t)
3696 = build_template_parm_index (idx, processing_template_decl,
3697 processing_template_decl,
3698 decl, TREE_TYPE (parm));
3699 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3700 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3702 DECL_ARTIFICIAL (decl) = 1;
3703 SET_DECL_TEMPLATE_PARM_P (decl);
3704 pushdecl (decl);
3705 parm = build_tree_list (defval, parm);
3706 return chainon (list, parm);
3709 /* The end of a template parameter list has been reached. Process the
3710 tree list into a parameter vector, converting each parameter into a more
3711 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3712 as PARM_DECLs. */
3714 tree
3715 end_template_parm_list (tree parms)
3717 int nparms;
3718 tree parm, next;
3719 tree saved_parmlist = make_tree_vec (list_length (parms));
3721 current_template_parms
3722 = tree_cons (size_int (processing_template_decl),
3723 saved_parmlist, current_template_parms);
3725 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3727 next = TREE_CHAIN (parm);
3728 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3729 TREE_CHAIN (parm) = NULL_TREE;
3732 --processing_template_parmlist;
3734 return saved_parmlist;
3737 /* end_template_decl is called after a template declaration is seen. */
3739 void
3740 end_template_decl (void)
3742 reset_specialization ();
3744 if (! processing_template_decl)
3745 return;
3747 /* This matches the pushlevel in begin_template_parm_list. */
3748 finish_scope ();
3750 --processing_template_decl;
3751 current_template_parms = TREE_CHAIN (current_template_parms);
3754 /* Takes a TREE_LIST representing a template parameter and convert it
3755 into an argument suitable to be passed to the type substitution
3756 functions. Note that If the TREE_LIST contains an error_mark
3757 node, the returned argument is error_mark_node. */
3759 static tree
3760 template_parm_to_arg (tree t)
3763 if (t == NULL_TREE
3764 || TREE_CODE (t) != TREE_LIST)
3765 return t;
3767 if (error_operand_p (TREE_VALUE (t)))
3768 return error_mark_node;
3770 t = TREE_VALUE (t);
3772 if (TREE_CODE (t) == TYPE_DECL
3773 || TREE_CODE (t) == TEMPLATE_DECL)
3775 t = TREE_TYPE (t);
3777 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3779 /* Turn this argument into a TYPE_ARGUMENT_PACK
3780 with a single element, which expands T. */
3781 tree vec = make_tree_vec (1);
3782 #ifdef ENABLE_CHECKING
3783 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3784 (vec, TREE_VEC_LENGTH (vec));
3785 #endif
3786 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3788 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3789 SET_ARGUMENT_PACK_ARGS (t, vec);
3792 else
3794 t = DECL_INITIAL (t);
3796 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3798 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3799 with a single element, which expands T. */
3800 tree vec = make_tree_vec (1);
3801 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3802 #ifdef ENABLE_CHECKING
3803 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3804 (vec, TREE_VEC_LENGTH (vec));
3805 #endif
3806 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3808 t = make_node (NONTYPE_ARGUMENT_PACK);
3809 SET_ARGUMENT_PACK_ARGS (t, vec);
3810 TREE_TYPE (t) = type;
3813 return t;
3816 /* This function returns TRUE if PARM_PACK is a template parameter
3817 pack and if ARG_PACK is what template_parm_to_arg returned when
3818 passed PARM_PACK. */
3820 static bool
3821 arg_from_parm_pack_p (tree arg_pack, tree parm_pack)
3823 /* For clarity in the comments below let's use the representation
3824 argument_pack<elements>' to denote an argument pack and its
3825 elements.
3827 In the 'if' block below, we want to detect cases where
3828 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
3829 check if ARG_PACK is an argument pack which sole element is
3830 the expansion of PARM_PACK. That argument pack is typically
3831 created by template_parm_to_arg when passed a parameter
3832 pack. */
3834 if (arg_pack
3835 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
3836 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
3838 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
3839 tree pattern = PACK_EXPANSION_PATTERN (expansion);
3840 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
3841 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
3842 /* The argument pack that the parameter maps to is just an
3843 expansion of the parameter itself, such as one would
3844 find in the implicit typedef of a class inside the
3845 class itself. Consider this parameter "unsubstituted",
3846 so that we will maintain the outer pack expansion. */
3847 return true;
3849 return false;
3852 /* Within the declaration of a template, return all levels of template
3853 parameters that apply. The template parameters are represented as
3854 a TREE_VEC, in the form documented in cp-tree.h for template
3855 arguments. */
3857 static tree
3858 current_template_args (void)
3860 tree header;
3861 tree args = NULL_TREE;
3862 int length = TMPL_PARMS_DEPTH (current_template_parms);
3863 int l = length;
3865 /* If there is only one level of template parameters, we do not
3866 create a TREE_VEC of TREE_VECs. Instead, we return a single
3867 TREE_VEC containing the arguments. */
3868 if (length > 1)
3869 args = make_tree_vec (length);
3871 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3873 tree a = copy_node (TREE_VALUE (header));
3874 int i;
3876 TREE_TYPE (a) = NULL_TREE;
3877 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3878 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3880 #ifdef ENABLE_CHECKING
3881 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3882 #endif
3884 if (length > 1)
3885 TREE_VEC_ELT (args, --l) = a;
3886 else
3887 args = a;
3890 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3891 /* This can happen for template parms of a template template
3892 parameter, e.g:
3894 template<template<class T, class U> class TT> struct S;
3896 Consider the level of the parms of TT; T and U both have
3897 level 2; TT has no template parm of level 1. So in this case
3898 the first element of full_template_args is NULL_TREE. If we
3899 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3900 of 2. This will make tsubst wrongly consider that T and U
3901 have level 1. Instead, let's create a dummy vector as the
3902 first element of full_template_args so that TMPL_ARG_DEPTH
3903 returns the correct depth for args. */
3904 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3905 return args;
3908 /* Update the declared TYPE by doing any lookups which were thought to be
3909 dependent, but are not now that we know the SCOPE of the declarator. */
3911 tree
3912 maybe_update_decl_type (tree orig_type, tree scope)
3914 tree type = orig_type;
3916 if (type == NULL_TREE)
3917 return type;
3919 if (TREE_CODE (orig_type) == TYPE_DECL)
3920 type = TREE_TYPE (type);
3922 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3923 && dependent_type_p (type)
3924 /* Don't bother building up the args in this case. */
3925 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3927 /* tsubst in the args corresponding to the template parameters,
3928 including auto if present. Most things will be unchanged, but
3929 make_typename_type and tsubst_qualified_id will resolve
3930 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3931 tree args = current_template_args ();
3932 tree auto_node = type_uses_auto (type);
3933 tree pushed;
3934 if (auto_node)
3936 tree auto_vec = make_tree_vec (1);
3937 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3938 args = add_to_template_args (args, auto_vec);
3940 pushed = push_scope (scope);
3941 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3942 if (pushed)
3943 pop_scope (scope);
3946 if (type == error_mark_node)
3947 return orig_type;
3949 if (TREE_CODE (orig_type) == TYPE_DECL)
3951 if (same_type_p (type, TREE_TYPE (orig_type)))
3952 type = orig_type;
3953 else
3954 type = TYPE_NAME (type);
3956 return type;
3959 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3960 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3961 a member template. Used by push_template_decl below. */
3963 static tree
3964 build_template_decl (tree decl, tree parms, bool member_template_p)
3966 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3967 DECL_TEMPLATE_PARMS (tmpl) = parms;
3968 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3969 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3970 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3972 return tmpl;
3975 struct template_parm_data
3977 /* The level of the template parameters we are currently
3978 processing. */
3979 int level;
3981 /* The index of the specialization argument we are currently
3982 processing. */
3983 int current_arg;
3985 /* An array whose size is the number of template parameters. The
3986 elements are nonzero if the parameter has been used in any one
3987 of the arguments processed so far. */
3988 int* parms;
3990 /* An array whose size is the number of template arguments. The
3991 elements are nonzero if the argument makes use of template
3992 parameters of this level. */
3993 int* arg_uses_template_parms;
3996 /* Subroutine of push_template_decl used to see if each template
3997 parameter in a partial specialization is used in the explicit
3998 argument list. If T is of the LEVEL given in DATA (which is
3999 treated as a template_parm_data*), then DATA->PARMS is marked
4000 appropriately. */
4002 static int
4003 mark_template_parm (tree t, void* data)
4005 int level;
4006 int idx;
4007 struct template_parm_data* tpd = (struct template_parm_data*) data;
4009 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4011 level = TEMPLATE_PARM_LEVEL (t);
4012 idx = TEMPLATE_PARM_IDX (t);
4014 else
4016 level = TEMPLATE_TYPE_LEVEL (t);
4017 idx = TEMPLATE_TYPE_IDX (t);
4020 if (level == tpd->level)
4022 tpd->parms[idx] = 1;
4023 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4026 /* Return zero so that for_each_template_parm will continue the
4027 traversal of the tree; we want to mark *every* template parm. */
4028 return 0;
4031 /* Process the partial specialization DECL. */
4033 static tree
4034 process_partial_specialization (tree decl)
4036 tree type = TREE_TYPE (decl);
4037 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4038 tree specargs = CLASSTYPE_TI_ARGS (type);
4039 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4040 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4041 tree inner_parms;
4042 tree inst;
4043 int nargs = TREE_VEC_LENGTH (inner_args);
4044 int ntparms;
4045 int i;
4046 bool did_error_intro = false;
4047 struct template_parm_data tpd;
4048 struct template_parm_data tpd2;
4050 gcc_assert (current_template_parms);
4052 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4053 ntparms = TREE_VEC_LENGTH (inner_parms);
4055 /* We check that each of the template parameters given in the
4056 partial specialization is used in the argument list to the
4057 specialization. For example:
4059 template <class T> struct S;
4060 template <class T> struct S<T*>;
4062 The second declaration is OK because `T*' uses the template
4063 parameter T, whereas
4065 template <class T> struct S<int>;
4067 is no good. Even trickier is:
4069 template <class T>
4070 struct S1
4072 template <class U>
4073 struct S2;
4074 template <class U>
4075 struct S2<T>;
4078 The S2<T> declaration is actually invalid; it is a
4079 full-specialization. Of course,
4081 template <class U>
4082 struct S2<T (*)(U)>;
4084 or some such would have been OK. */
4085 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4086 tpd.parms = XALLOCAVEC (int, ntparms);
4087 memset (tpd.parms, 0, sizeof (int) * ntparms);
4089 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4090 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4091 for (i = 0; i < nargs; ++i)
4093 tpd.current_arg = i;
4094 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4095 &mark_template_parm,
4096 &tpd,
4097 NULL,
4098 /*include_nondeduced_p=*/false);
4100 for (i = 0; i < ntparms; ++i)
4101 if (tpd.parms[i] == 0)
4103 /* One of the template parms was not used in the
4104 specialization. */
4105 if (!did_error_intro)
4107 error ("template parameters not used in partial specialization:");
4108 did_error_intro = true;
4111 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4114 if (did_error_intro)
4115 return error_mark_node;
4117 /* [temp.class.spec]
4119 The argument list of the specialization shall not be identical to
4120 the implicit argument list of the primary template. */
4121 if (comp_template_args
4122 (inner_args,
4123 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4124 (maintmpl)))))
4125 error ("partial specialization %qT does not specialize any template arguments", type);
4127 /* A partial specialization that replaces multiple parameters of the
4128 primary template with a pack expansion is less specialized for those
4129 parameters. */
4130 if (nargs < DECL_NTPARMS (maintmpl))
4132 error ("partial specialization is not more specialized than the "
4133 "primary template because it replaces multiple parameters "
4134 "with a pack expansion");
4135 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4136 return decl;
4139 /* [temp.class.spec]
4141 A partially specialized non-type argument expression shall not
4142 involve template parameters of the partial specialization except
4143 when the argument expression is a simple identifier.
4145 The type of a template parameter corresponding to a specialized
4146 non-type argument shall not be dependent on a parameter of the
4147 specialization.
4149 Also, we verify that pack expansions only occur at the
4150 end of the argument list. */
4151 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4152 tpd2.parms = 0;
4153 for (i = 0; i < nargs; ++i)
4155 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4156 tree arg = TREE_VEC_ELT (inner_args, i);
4157 tree packed_args = NULL_TREE;
4158 int j, len = 1;
4160 if (ARGUMENT_PACK_P (arg))
4162 /* Extract the arguments from the argument pack. We'll be
4163 iterating over these in the following loop. */
4164 packed_args = ARGUMENT_PACK_ARGS (arg);
4165 len = TREE_VEC_LENGTH (packed_args);
4168 for (j = 0; j < len; j++)
4170 if (packed_args)
4171 /* Get the Jth argument in the parameter pack. */
4172 arg = TREE_VEC_ELT (packed_args, j);
4174 if (PACK_EXPANSION_P (arg))
4176 /* Pack expansions must come at the end of the
4177 argument list. */
4178 if ((packed_args && j < len - 1)
4179 || (!packed_args && i < nargs - 1))
4181 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4182 error ("parameter pack argument %qE must be at the "
4183 "end of the template argument list", arg);
4184 else
4185 error ("parameter pack argument %qT must be at the "
4186 "end of the template argument list", arg);
4190 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4191 /* We only care about the pattern. */
4192 arg = PACK_EXPANSION_PATTERN (arg);
4194 if (/* These first two lines are the `non-type' bit. */
4195 !TYPE_P (arg)
4196 && TREE_CODE (arg) != TEMPLATE_DECL
4197 /* This next line is the `argument expression is not just a
4198 simple identifier' condition and also the `specialized
4199 non-type argument' bit. */
4200 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4202 if ((!packed_args && tpd.arg_uses_template_parms[i])
4203 || (packed_args && uses_template_parms (arg)))
4204 error ("template argument %qE involves template parameter(s)",
4205 arg);
4206 else
4208 /* Look at the corresponding template parameter,
4209 marking which template parameters its type depends
4210 upon. */
4211 tree type = TREE_TYPE (parm);
4213 if (!tpd2.parms)
4215 /* We haven't yet initialized TPD2. Do so now. */
4216 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4217 /* The number of parameters here is the number in the
4218 main template, which, as checked in the assertion
4219 above, is NARGS. */
4220 tpd2.parms = XALLOCAVEC (int, nargs);
4221 tpd2.level =
4222 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4225 /* Mark the template parameters. But this time, we're
4226 looking for the template parameters of the main
4227 template, not in the specialization. */
4228 tpd2.current_arg = i;
4229 tpd2.arg_uses_template_parms[i] = 0;
4230 memset (tpd2.parms, 0, sizeof (int) * nargs);
4231 for_each_template_parm (type,
4232 &mark_template_parm,
4233 &tpd2,
4234 NULL,
4235 /*include_nondeduced_p=*/false);
4237 if (tpd2.arg_uses_template_parms [i])
4239 /* The type depended on some template parameters.
4240 If they are fully specialized in the
4241 specialization, that's OK. */
4242 int j;
4243 int count = 0;
4244 for (j = 0; j < nargs; ++j)
4245 if (tpd2.parms[j] != 0
4246 && tpd.arg_uses_template_parms [j])
4247 ++count;
4248 if (count != 0)
4249 error_n (input_location, count,
4250 "type %qT of template argument %qE depends "
4251 "on a template parameter",
4252 "type %qT of template argument %qE depends "
4253 "on template parameters",
4254 type,
4255 arg);
4262 /* We should only get here once. */
4263 gcc_assert (!COMPLETE_TYPE_P (type));
4265 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4266 = tree_cons (specargs, inner_parms,
4267 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4268 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4270 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4271 inst = TREE_CHAIN (inst))
4273 tree inst_type = TREE_VALUE (inst);
4274 if (COMPLETE_TYPE_P (inst_type)
4275 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4277 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4278 if (spec && TREE_TYPE (spec) == type)
4279 permerror (input_location,
4280 "partial specialization of %qT after instantiation "
4281 "of %qT", type, inst_type);
4285 return decl;
4288 /* Check that a template declaration's use of default arguments and
4289 parameter packs is not invalid. Here, PARMS are the template
4290 parameters. IS_PRIMARY is true if DECL is the thing declared by
4291 a primary template. IS_PARTIAL is true if DECL is a partial
4292 specialization.
4294 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4295 declaration (but not a definition); 1 indicates a declaration, 2
4296 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4297 emitted for extraneous default arguments.
4299 Returns TRUE if there were no errors found, FALSE otherwise. */
4301 bool
4302 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4303 bool is_partial, int is_friend_decl)
4305 const char *msg;
4306 int last_level_to_check;
4307 tree parm_level;
4308 bool no_errors = true;
4310 /* [temp.param]
4312 A default template-argument shall not be specified in a
4313 function template declaration or a function template definition, nor
4314 in the template-parameter-list of the definition of a member of a
4315 class template. */
4317 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4318 /* You can't have a function template declaration in a local
4319 scope, nor you can you define a member of a class template in a
4320 local scope. */
4321 return true;
4323 if (current_class_type
4324 && !TYPE_BEING_DEFINED (current_class_type)
4325 && DECL_LANG_SPECIFIC (decl)
4326 && DECL_DECLARES_FUNCTION_P (decl)
4327 /* If this is either a friend defined in the scope of the class
4328 or a member function. */
4329 && (DECL_FUNCTION_MEMBER_P (decl)
4330 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4331 : DECL_FRIEND_CONTEXT (decl)
4332 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4333 : false)
4334 /* And, if it was a member function, it really was defined in
4335 the scope of the class. */
4336 && (!DECL_FUNCTION_MEMBER_P (decl)
4337 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4338 /* We already checked these parameters when the template was
4339 declared, so there's no need to do it again now. This function
4340 was defined in class scope, but we're processing it's body now
4341 that the class is complete. */
4342 return true;
4344 /* Core issue 226 (C++0x only): the following only applies to class
4345 templates. */
4346 if (is_primary
4347 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4349 /* [temp.param]
4351 If a template-parameter has a default template-argument, all
4352 subsequent template-parameters shall have a default
4353 template-argument supplied. */
4354 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4356 tree inner_parms = TREE_VALUE (parm_level);
4357 int ntparms = TREE_VEC_LENGTH (inner_parms);
4358 int seen_def_arg_p = 0;
4359 int i;
4361 for (i = 0; i < ntparms; ++i)
4363 tree parm = TREE_VEC_ELT (inner_parms, i);
4365 if (parm == error_mark_node)
4366 continue;
4368 if (TREE_PURPOSE (parm))
4369 seen_def_arg_p = 1;
4370 else if (seen_def_arg_p
4371 && !template_parameter_pack_p (TREE_VALUE (parm)))
4373 error ("no default argument for %qD", TREE_VALUE (parm));
4374 /* For better subsequent error-recovery, we indicate that
4375 there should have been a default argument. */
4376 TREE_PURPOSE (parm) = error_mark_node;
4377 no_errors = false;
4379 else if (!is_partial
4380 && !is_friend_decl
4381 /* Don't complain about an enclosing partial
4382 specialization. */
4383 && parm_level == parms
4384 && TREE_CODE (decl) == TYPE_DECL
4385 && i < ntparms - 1
4386 && template_parameter_pack_p (TREE_VALUE (parm)))
4388 /* A primary class template can only have one
4389 parameter pack, at the end of the template
4390 parameter list. */
4392 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4393 error ("parameter pack %qE must be at the end of the"
4394 " template parameter list", TREE_VALUE (parm));
4395 else
4396 error ("parameter pack %qT must be at the end of the"
4397 " template parameter list",
4398 TREE_TYPE (TREE_VALUE (parm)));
4400 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4401 = error_mark_node;
4402 no_errors = false;
4408 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4409 || is_partial
4410 || !is_primary
4411 || is_friend_decl)
4412 /* For an ordinary class template, default template arguments are
4413 allowed at the innermost level, e.g.:
4414 template <class T = int>
4415 struct S {};
4416 but, in a partial specialization, they're not allowed even
4417 there, as we have in [temp.class.spec]:
4419 The template parameter list of a specialization shall not
4420 contain default template argument values.
4422 So, for a partial specialization, or for a function template
4423 (in C++98/C++03), we look at all of them. */
4425 else
4426 /* But, for a primary class template that is not a partial
4427 specialization we look at all template parameters except the
4428 innermost ones. */
4429 parms = TREE_CHAIN (parms);
4431 /* Figure out what error message to issue. */
4432 if (is_friend_decl == 2)
4433 msg = G_("default template arguments may not be used in function template "
4434 "friend re-declaration");
4435 else if (is_friend_decl)
4436 msg = G_("default template arguments may not be used in function template "
4437 "friend declarations");
4438 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4439 msg = G_("default template arguments may not be used in function templates "
4440 "without -std=c++11 or -std=gnu++11");
4441 else if (is_partial)
4442 msg = G_("default template arguments may not be used in "
4443 "partial specializations");
4444 else
4445 msg = G_("default argument for template parameter for class enclosing %qD");
4447 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4448 /* If we're inside a class definition, there's no need to
4449 examine the parameters to the class itself. On the one
4450 hand, they will be checked when the class is defined, and,
4451 on the other, default arguments are valid in things like:
4452 template <class T = double>
4453 struct S { template <class U> void f(U); };
4454 Here the default argument for `S' has no bearing on the
4455 declaration of `f'. */
4456 last_level_to_check = template_class_depth (current_class_type) + 1;
4457 else
4458 /* Check everything. */
4459 last_level_to_check = 0;
4461 for (parm_level = parms;
4462 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4463 parm_level = TREE_CHAIN (parm_level))
4465 tree inner_parms = TREE_VALUE (parm_level);
4466 int i;
4467 int ntparms;
4469 ntparms = TREE_VEC_LENGTH (inner_parms);
4470 for (i = 0; i < ntparms; ++i)
4472 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4473 continue;
4475 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4477 if (msg)
4479 no_errors = false;
4480 if (is_friend_decl == 2)
4481 return no_errors;
4483 error (msg, decl);
4484 msg = 0;
4487 /* Clear out the default argument so that we are not
4488 confused later. */
4489 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4493 /* At this point, if we're still interested in issuing messages,
4494 they must apply to classes surrounding the object declared. */
4495 if (msg)
4496 msg = G_("default argument for template parameter for class "
4497 "enclosing %qD");
4500 return no_errors;
4503 /* Worker for push_template_decl_real, called via
4504 for_each_template_parm. DATA is really an int, indicating the
4505 level of the parameters we are interested in. If T is a template
4506 parameter of that level, return nonzero. */
4508 static int
4509 template_parm_this_level_p (tree t, void* data)
4511 int this_level = *(int *)data;
4512 int level;
4514 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4515 level = TEMPLATE_PARM_LEVEL (t);
4516 else
4517 level = TEMPLATE_TYPE_LEVEL (t);
4518 return level == this_level;
4521 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4522 parameters given by current_template_args, or reuses a
4523 previously existing one, if appropriate. Returns the DECL, or an
4524 equivalent one, if it is replaced via a call to duplicate_decls.
4526 If IS_FRIEND is true, DECL is a friend declaration. */
4528 tree
4529 push_template_decl_real (tree decl, bool is_friend)
4531 tree tmpl;
4532 tree args;
4533 tree info;
4534 tree ctx;
4535 bool is_primary;
4536 bool is_partial;
4537 int new_template_p = 0;
4538 /* True if the template is a member template, in the sense of
4539 [temp.mem]. */
4540 bool member_template_p = false;
4542 if (decl == error_mark_node || !current_template_parms)
4543 return error_mark_node;
4545 /* See if this is a partial specialization. */
4546 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4547 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4548 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4550 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4551 is_friend = true;
4553 if (is_friend)
4554 /* For a friend, we want the context of the friend function, not
4555 the type of which it is a friend. */
4556 ctx = CP_DECL_CONTEXT (decl);
4557 else if (CP_DECL_CONTEXT (decl)
4558 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4559 /* In the case of a virtual function, we want the class in which
4560 it is defined. */
4561 ctx = CP_DECL_CONTEXT (decl);
4562 else
4563 /* Otherwise, if we're currently defining some class, the DECL
4564 is assumed to be a member of the class. */
4565 ctx = current_scope ();
4567 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4568 ctx = NULL_TREE;
4570 if (!DECL_CONTEXT (decl))
4571 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4573 /* See if this is a primary template. */
4574 if (is_friend && ctx)
4575 /* A friend template that specifies a class context, i.e.
4576 template <typename T> friend void A<T>::f();
4577 is not primary. */
4578 is_primary = false;
4579 else
4580 is_primary = template_parm_scope_p ();
4582 if (is_primary)
4584 if (DECL_CLASS_SCOPE_P (decl))
4585 member_template_p = true;
4586 if (TREE_CODE (decl) == TYPE_DECL
4587 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4589 error ("template class without a name");
4590 return error_mark_node;
4592 else if (TREE_CODE (decl) == FUNCTION_DECL)
4594 if (DECL_DESTRUCTOR_P (decl))
4596 /* [temp.mem]
4598 A destructor shall not be a member template. */
4599 error ("destructor %qD declared as member template", decl);
4600 return error_mark_node;
4602 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4603 && (!prototype_p (TREE_TYPE (decl))
4604 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4605 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4606 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4607 == void_list_node)))
4609 /* [basic.stc.dynamic.allocation]
4611 An allocation function can be a function
4612 template. ... Template allocation functions shall
4613 have two or more parameters. */
4614 error ("invalid template declaration of %qD", decl);
4615 return error_mark_node;
4618 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4619 && CLASS_TYPE_P (TREE_TYPE (decl)))
4620 /* OK */;
4621 else if (TREE_CODE (decl) == TYPE_DECL
4622 && TYPE_DECL_ALIAS_P (decl))
4623 /* alias-declaration */
4624 gcc_assert (!DECL_ARTIFICIAL (decl));
4625 else
4627 error ("template declaration of %q#D", decl);
4628 return error_mark_node;
4632 /* Check to see that the rules regarding the use of default
4633 arguments are not being violated. */
4634 check_default_tmpl_args (decl, current_template_parms,
4635 is_primary, is_partial, /*is_friend_decl=*/0);
4637 /* Ensure that there are no parameter packs in the type of this
4638 declaration that have not been expanded. */
4639 if (TREE_CODE (decl) == FUNCTION_DECL)
4641 /* Check each of the arguments individually to see if there are
4642 any bare parameter packs. */
4643 tree type = TREE_TYPE (decl);
4644 tree arg = DECL_ARGUMENTS (decl);
4645 tree argtype = TYPE_ARG_TYPES (type);
4647 while (arg && argtype)
4649 if (!FUNCTION_PARAMETER_PACK_P (arg)
4650 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4652 /* This is a PARM_DECL that contains unexpanded parameter
4653 packs. We have already complained about this in the
4654 check_for_bare_parameter_packs call, so just replace
4655 these types with ERROR_MARK_NODE. */
4656 TREE_TYPE (arg) = error_mark_node;
4657 TREE_VALUE (argtype) = error_mark_node;
4660 arg = DECL_CHAIN (arg);
4661 argtype = TREE_CHAIN (argtype);
4664 /* Check for bare parameter packs in the return type and the
4665 exception specifiers. */
4666 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4667 /* Errors were already issued, set return type to int
4668 as the frontend doesn't expect error_mark_node as
4669 the return type. */
4670 TREE_TYPE (type) = integer_type_node;
4671 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4672 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4674 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4675 && TYPE_DECL_ALIAS_P (decl))
4676 ? DECL_ORIGINAL_TYPE (decl)
4677 : TREE_TYPE (decl)))
4679 TREE_TYPE (decl) = error_mark_node;
4680 return error_mark_node;
4683 if (is_partial)
4684 return process_partial_specialization (decl);
4686 args = current_template_args ();
4688 if (!ctx
4689 || TREE_CODE (ctx) == FUNCTION_DECL
4690 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4691 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4693 if (DECL_LANG_SPECIFIC (decl)
4694 && DECL_TEMPLATE_INFO (decl)
4695 && DECL_TI_TEMPLATE (decl))
4696 tmpl = DECL_TI_TEMPLATE (decl);
4697 /* If DECL is a TYPE_DECL for a class-template, then there won't
4698 be DECL_LANG_SPECIFIC. The information equivalent to
4699 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4700 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4701 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4702 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4704 /* Since a template declaration already existed for this
4705 class-type, we must be redeclaring it here. Make sure
4706 that the redeclaration is valid. */
4707 redeclare_class_template (TREE_TYPE (decl),
4708 current_template_parms);
4709 /* We don't need to create a new TEMPLATE_DECL; just use the
4710 one we already had. */
4711 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4713 else
4715 tmpl = build_template_decl (decl, current_template_parms,
4716 member_template_p);
4717 new_template_p = 1;
4719 if (DECL_LANG_SPECIFIC (decl)
4720 && DECL_TEMPLATE_SPECIALIZATION (decl))
4722 /* A specialization of a member template of a template
4723 class. */
4724 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4725 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4726 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4730 else
4732 tree a, t, current, parms;
4733 int i;
4734 tree tinfo = get_template_info (decl);
4736 if (!tinfo)
4738 error ("template definition of non-template %q#D", decl);
4739 return error_mark_node;
4742 tmpl = TI_TEMPLATE (tinfo);
4744 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4745 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4746 && DECL_TEMPLATE_SPECIALIZATION (decl)
4747 && DECL_MEMBER_TEMPLATE_P (tmpl))
4749 tree new_tmpl;
4751 /* The declaration is a specialization of a member
4752 template, declared outside the class. Therefore, the
4753 innermost template arguments will be NULL, so we
4754 replace them with the arguments determined by the
4755 earlier call to check_explicit_specialization. */
4756 args = DECL_TI_ARGS (decl);
4758 new_tmpl
4759 = build_template_decl (decl, current_template_parms,
4760 member_template_p);
4761 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4762 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4763 DECL_TI_TEMPLATE (decl) = new_tmpl;
4764 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4765 DECL_TEMPLATE_INFO (new_tmpl)
4766 = build_template_info (tmpl, args);
4768 register_specialization (new_tmpl,
4769 most_general_template (tmpl),
4770 args,
4771 is_friend, 0);
4772 return decl;
4775 /* Make sure the template headers we got make sense. */
4777 parms = DECL_TEMPLATE_PARMS (tmpl);
4778 i = TMPL_PARMS_DEPTH (parms);
4779 if (TMPL_ARGS_DEPTH (args) != i)
4781 error ("expected %d levels of template parms for %q#D, got %d",
4782 i, decl, TMPL_ARGS_DEPTH (args));
4784 else
4785 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4787 a = TMPL_ARGS_LEVEL (args, i);
4788 t = INNERMOST_TEMPLATE_PARMS (parms);
4790 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4792 if (current == decl)
4793 error ("got %d template parameters for %q#D",
4794 TREE_VEC_LENGTH (a), decl);
4795 else
4796 error ("got %d template parameters for %q#T",
4797 TREE_VEC_LENGTH (a), current);
4798 error (" but %d required", TREE_VEC_LENGTH (t));
4799 return error_mark_node;
4802 if (current == decl)
4803 current = ctx;
4804 else if (current == NULL_TREE)
4805 /* Can happen in erroneous input. */
4806 break;
4807 else
4808 current = (TYPE_P (current)
4809 ? TYPE_CONTEXT (current)
4810 : DECL_CONTEXT (current));
4813 /* Check that the parms are used in the appropriate qualifying scopes
4814 in the declarator. */
4815 if (!comp_template_args
4816 (TI_ARGS (tinfo),
4817 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4819 error ("\
4820 template arguments to %qD do not match original template %qD",
4821 decl, DECL_TEMPLATE_RESULT (tmpl));
4822 if (!uses_template_parms (TI_ARGS (tinfo)))
4823 inform (input_location, "use template<> for an explicit specialization");
4824 /* Avoid crash in import_export_decl. */
4825 DECL_INTERFACE_KNOWN (decl) = 1;
4826 return error_mark_node;
4830 DECL_TEMPLATE_RESULT (tmpl) = decl;
4831 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4833 /* Push template declarations for global functions and types. Note
4834 that we do not try to push a global template friend declared in a
4835 template class; such a thing may well depend on the template
4836 parameters of the class. */
4837 if (new_template_p && !ctx
4838 && !(is_friend && template_class_depth (current_class_type) > 0))
4840 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4841 if (tmpl == error_mark_node)
4842 return error_mark_node;
4844 /* Hide template friend classes that haven't been declared yet. */
4845 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4847 DECL_ANTICIPATED (tmpl) = 1;
4848 DECL_FRIEND_P (tmpl) = 1;
4852 if (is_primary)
4854 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4855 int i;
4857 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4858 if (DECL_CONV_FN_P (tmpl))
4860 int depth = TMPL_PARMS_DEPTH (parms);
4862 /* It is a conversion operator. See if the type converted to
4863 depends on innermost template operands. */
4865 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4866 depth))
4867 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4870 /* Give template template parms a DECL_CONTEXT of the template
4871 for which they are a parameter. */
4872 parms = INNERMOST_TEMPLATE_PARMS (parms);
4873 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4875 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4876 if (TREE_CODE (parm) == TEMPLATE_DECL)
4877 DECL_CONTEXT (parm) = tmpl;
4881 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4882 back to its most general template. If TMPL is a specialization,
4883 ARGS may only have the innermost set of arguments. Add the missing
4884 argument levels if necessary. */
4885 if (DECL_TEMPLATE_INFO (tmpl))
4886 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4888 info = build_template_info (tmpl, args);
4890 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4891 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4892 else
4894 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4895 retrofit_lang_decl (decl);
4896 if (DECL_LANG_SPECIFIC (decl))
4897 DECL_TEMPLATE_INFO (decl) = info;
4900 return DECL_TEMPLATE_RESULT (tmpl);
4903 tree
4904 push_template_decl (tree decl)
4906 return push_template_decl_real (decl, false);
4909 /* Called when a class template TYPE is redeclared with the indicated
4910 template PARMS, e.g.:
4912 template <class T> struct S;
4913 template <class T> struct S {}; */
4915 bool
4916 redeclare_class_template (tree type, tree parms)
4918 tree tmpl;
4919 tree tmpl_parms;
4920 int i;
4922 if (!TYPE_TEMPLATE_INFO (type))
4924 error ("%qT is not a template type", type);
4925 return false;
4928 tmpl = TYPE_TI_TEMPLATE (type);
4929 if (!PRIMARY_TEMPLATE_P (tmpl))
4930 /* The type is nested in some template class. Nothing to worry
4931 about here; there are no new template parameters for the nested
4932 type. */
4933 return true;
4935 if (!parms)
4937 error ("template specifiers not specified in declaration of %qD",
4938 tmpl);
4939 return false;
4942 parms = INNERMOST_TEMPLATE_PARMS (parms);
4943 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4945 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4947 error_n (input_location, TREE_VEC_LENGTH (parms),
4948 "redeclared with %d template parameter",
4949 "redeclared with %d template parameters",
4950 TREE_VEC_LENGTH (parms));
4951 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4952 "previous declaration %q+D used %d template parameter",
4953 "previous declaration %q+D used %d template parameters",
4954 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4955 return false;
4958 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4960 tree tmpl_parm;
4961 tree parm;
4962 tree tmpl_default;
4963 tree parm_default;
4965 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4966 || TREE_VEC_ELT (parms, i) == error_mark_node)
4967 continue;
4969 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4970 if (tmpl_parm == error_mark_node)
4971 return false;
4973 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4974 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4975 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4977 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4978 TEMPLATE_DECL. */
4979 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4980 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4981 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4982 || (TREE_CODE (tmpl_parm) != PARM_DECL
4983 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4984 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4985 || (TREE_CODE (tmpl_parm) == PARM_DECL
4986 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4987 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4989 error ("template parameter %q+#D", tmpl_parm);
4990 error ("redeclared here as %q#D", parm);
4991 return false;
4994 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4996 /* We have in [temp.param]:
4998 A template-parameter may not be given default arguments
4999 by two different declarations in the same scope. */
5000 error_at (input_location, "redefinition of default argument for %q#D", parm);
5001 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5002 "original definition appeared here");
5003 return false;
5006 if (parm_default != NULL_TREE)
5007 /* Update the previous template parameters (which are the ones
5008 that will really count) with the new default value. */
5009 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5010 else if (tmpl_default != NULL_TREE)
5011 /* Update the new parameters, too; they'll be used as the
5012 parameters for any members. */
5013 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5016 return true;
5019 /* Simplify EXPR if it is a non-dependent expression. Returns the
5020 (possibly simplified) expression. */
5022 static tree
5023 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5025 if (expr == NULL_TREE)
5026 return NULL_TREE;
5028 /* If we're in a template, but EXPR isn't value dependent, simplify
5029 it. We're supposed to treat:
5031 template <typename T> void f(T[1 + 1]);
5032 template <typename T> void f(T[2]);
5034 as two declarations of the same function, for example. */
5035 if (processing_template_decl
5036 && !type_dependent_expression_p (expr)
5037 && potential_constant_expression (expr)
5038 && !value_dependent_expression_p (expr))
5040 HOST_WIDE_INT saved_processing_template_decl;
5042 saved_processing_template_decl = processing_template_decl;
5043 processing_template_decl = 0;
5044 expr = tsubst_copy_and_build (expr,
5045 /*args=*/NULL_TREE,
5046 complain,
5047 /*in_decl=*/NULL_TREE,
5048 /*function_p=*/false,
5049 /*integral_constant_expression_p=*/true);
5050 processing_template_decl = saved_processing_template_decl;
5052 return expr;
5055 tree
5056 fold_non_dependent_expr (tree expr)
5058 return fold_non_dependent_expr_sfinae (expr, tf_error);
5061 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5062 template declaration, or a TYPE_DECL for an alias declaration. */
5064 bool
5065 alias_type_or_template_p (tree t)
5067 if (t == NULL_TREE)
5068 return false;
5069 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5070 || (TYPE_P (t)
5071 && TYPE_NAME (t)
5072 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5073 || DECL_ALIAS_TEMPLATE_P (t));
5076 /* Return TRUE iff is a specialization of an alias template. */
5078 bool
5079 alias_template_specialization_p (tree t)
5081 if (t == NULL_TREE)
5082 return false;
5083 return (primary_template_instantiation_p (t)
5084 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5087 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5088 must be a function or a pointer-to-function type, as specified
5089 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5090 and check that the resulting function has external linkage. */
5092 static tree
5093 convert_nontype_argument_function (tree type, tree expr)
5095 tree fns = expr;
5096 tree fn, fn_no_ptr;
5097 linkage_kind linkage;
5099 fn = instantiate_type (type, fns, tf_none);
5100 if (fn == error_mark_node)
5101 return error_mark_node;
5103 fn_no_ptr = fn;
5104 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5105 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5106 if (BASELINK_P (fn_no_ptr))
5107 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5109 /* [temp.arg.nontype]/1
5111 A template-argument for a non-type, non-template template-parameter
5112 shall be one of:
5113 [...]
5114 -- the address of an object or function with external [C++11: or
5115 internal] linkage. */
5116 linkage = decl_linkage (fn_no_ptr);
5117 if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
5119 if (cxx_dialect >= cxx0x)
5120 error ("%qE is not a valid template argument for type %qT "
5121 "because %qD has no linkage",
5122 expr, type, fn_no_ptr);
5123 else
5124 error ("%qE is not a valid template argument for type %qT "
5125 "because %qD does not have external linkage",
5126 expr, type, fn_no_ptr);
5127 return NULL_TREE;
5130 return fn;
5133 /* Subroutine of convert_nontype_argument.
5134 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5135 Emit an error otherwise. */
5137 static bool
5138 check_valid_ptrmem_cst_expr (tree type, tree expr,
5139 tsubst_flags_t complain)
5141 STRIP_NOPS (expr);
5142 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5143 return true;
5144 if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr))
5145 return true;
5146 if (complain & tf_error)
5148 error ("%qE is not a valid template argument for type %qT",
5149 expr, type);
5150 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5152 return false;
5155 /* Returns TRUE iff the address of OP is value-dependent.
5157 14.6.2.4 [temp.dep.temp]:
5158 A non-integral non-type template-argument is dependent if its type is
5159 dependent or it has either of the following forms
5160 qualified-id
5161 & qualified-id
5162 and contains a nested-name-specifier which specifies a class-name that
5163 names a dependent type.
5165 We generalize this to just say that the address of a member of a
5166 dependent class is value-dependent; the above doesn't cover the
5167 address of a static data member named with an unqualified-id. */
5169 static bool
5170 has_value_dependent_address (tree op)
5172 /* We could use get_inner_reference here, but there's no need;
5173 this is only relevant for template non-type arguments, which
5174 can only be expressed as &id-expression. */
5175 if (DECL_P (op))
5177 tree ctx = CP_DECL_CONTEXT (op);
5178 if (TYPE_P (ctx) && dependent_type_p (ctx))
5179 return true;
5182 return false;
5185 /* The next set of functions are used for providing helpful explanatory
5186 diagnostics for failed overload resolution. Their messages should be
5187 indented by two spaces for consistency with the messages in
5188 call.c */
5190 static int
5191 unify_success (bool explain_p ATTRIBUTE_UNUSED)
5193 return 0;
5196 static int
5197 unify_parameter_deduction_failure (bool explain_p, tree parm)
5199 if (explain_p)
5200 inform (input_location,
5201 " couldn't deduce template parameter %qD", parm);
5202 return 1;
5205 static int
5206 unify_invalid (bool explain_p ATTRIBUTE_UNUSED)
5208 return 1;
5211 static int
5212 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5214 if (explain_p)
5215 inform (input_location,
5216 " types %qT and %qT have incompatible cv-qualifiers",
5217 parm, arg);
5218 return 1;
5221 static int
5222 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5224 if (explain_p)
5225 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5226 return 1;
5229 static int
5230 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5232 if (explain_p)
5233 inform (input_location,
5234 " template parameter %qD is not a parameter pack, but "
5235 "argument %qD is",
5236 parm, arg);
5237 return 1;
5240 static int
5241 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5243 if (explain_p)
5244 inform (input_location,
5245 " template argument %qE does not match "
5246 "pointer-to-member constant %qE",
5247 arg, parm);
5248 return 1;
5251 static int
5252 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5254 if (explain_p)
5255 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5256 return 1;
5259 static int
5260 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5262 if (explain_p)
5263 inform (input_location,
5264 " inconsistent parameter pack deduction with %qT and %qT",
5265 old_arg, new_arg);
5266 return 1;
5269 static int
5270 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5272 if (explain_p)
5274 if (TYPE_P (parm))
5275 inform (input_location,
5276 " deduced conflicting types for parameter %qT (%qT and %qT)",
5277 parm, first, second);
5278 else
5279 inform (input_location,
5280 " deduced conflicting values for non-type parameter "
5281 "%qE (%qE and %qE)", parm, first, second);
5283 return 1;
5286 static int
5287 unify_vla_arg (bool explain_p, tree arg)
5289 if (explain_p)
5290 inform (input_location,
5291 " variable-sized array type %qT is not "
5292 "a valid template argument",
5293 arg);
5294 return 1;
5297 static int
5298 unify_method_type_error (bool explain_p, tree arg)
5300 if (explain_p)
5301 inform (input_location,
5302 " member function type %qT is not a valid template argument",
5303 arg);
5304 return 1;
5307 static int
5308 unify_arity (bool explain_p, int have, int wanted)
5310 if (explain_p)
5311 inform_n (input_location, wanted,
5312 " candidate expects %d argument, %d provided",
5313 " candidate expects %d arguments, %d provided",
5314 wanted, have);
5315 return 1;
5318 static int
5319 unify_too_many_arguments (bool explain_p, int have, int wanted)
5321 return unify_arity (explain_p, have, wanted);
5324 static int
5325 unify_too_few_arguments (bool explain_p, int have, int wanted)
5327 return unify_arity (explain_p, have, wanted);
5330 static int
5331 unify_arg_conversion (bool explain_p, tree to_type,
5332 tree from_type, tree arg)
5334 if (explain_p)
5335 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5336 arg, from_type, to_type);
5337 return 1;
5340 static int
5341 unify_no_common_base (bool explain_p, enum template_base_result r,
5342 tree parm, tree arg)
5344 if (explain_p)
5345 switch (r)
5347 case tbr_ambiguous_baseclass:
5348 inform (input_location, " %qT is an ambiguous base class of %qT",
5349 arg, parm);
5350 break;
5351 default:
5352 inform (input_location, " %qT is not derived from %qT", arg, parm);
5353 break;
5355 return 1;
5358 static int
5359 unify_inconsistent_template_template_parameters (bool explain_p)
5361 if (explain_p)
5362 inform (input_location,
5363 " template parameters of a template template argument are "
5364 "inconsistent with other deduced template arguments");
5365 return 1;
5368 static int
5369 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5371 if (explain_p)
5372 inform (input_location,
5373 " can't deduce a template for %qT from non-template type %qT",
5374 parm, arg);
5375 return 1;
5378 static int
5379 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5381 if (explain_p)
5382 inform (input_location,
5383 " template argument %qE does not match %qD", arg, parm);
5384 return 1;
5387 static int
5388 unify_overload_resolution_failure (bool explain_p, tree arg)
5390 if (explain_p)
5391 inform (input_location,
5392 " could not resolve address from overloaded function %qE",
5393 arg);
5394 return 1;
5397 /* Attempt to convert the non-type template parameter EXPR to the
5398 indicated TYPE. If the conversion is successful, return the
5399 converted value. If the conversion is unsuccessful, return
5400 NULL_TREE if we issued an error message, or error_mark_node if we
5401 did not. We issue error messages for out-and-out bad template
5402 parameters, but not simply because the conversion failed, since we
5403 might be just trying to do argument deduction. Both TYPE and EXPR
5404 must be non-dependent.
5406 The conversion follows the special rules described in
5407 [temp.arg.nontype], and it is much more strict than an implicit
5408 conversion.
5410 This function is called twice for each template argument (see
5411 lookup_template_class for a more accurate description of this
5412 problem). This means that we need to handle expressions which
5413 are not valid in a C++ source, but can be created from the
5414 first call (for instance, casts to perform conversions). These
5415 hacks can go away after we fix the double coercion problem. */
5417 static tree
5418 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5420 tree expr_type;
5422 /* Detect immediately string literals as invalid non-type argument.
5423 This special-case is not needed for correctness (we would easily
5424 catch this later), but only to provide better diagnostic for this
5425 common user mistake. As suggested by DR 100, we do not mention
5426 linkage issues in the diagnostic as this is not the point. */
5427 /* FIXME we're making this OK. */
5428 if (TREE_CODE (expr) == STRING_CST)
5430 if (complain & tf_error)
5431 error ("%qE is not a valid template argument for type %qT "
5432 "because string literals can never be used in this context",
5433 expr, type);
5434 return NULL_TREE;
5437 /* Add the ADDR_EXPR now for the benefit of
5438 value_dependent_expression_p. */
5439 if (TYPE_PTROBV_P (type)
5440 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5442 expr = decay_conversion (expr, complain);
5443 if (expr == error_mark_node)
5444 return error_mark_node;
5447 /* If we are in a template, EXPR may be non-dependent, but still
5448 have a syntactic, rather than semantic, form. For example, EXPR
5449 might be a SCOPE_REF, rather than the VAR_DECL to which the
5450 SCOPE_REF refers. Preserving the qualifying scope is necessary
5451 so that access checking can be performed when the template is
5452 instantiated -- but here we need the resolved form so that we can
5453 convert the argument. */
5454 if (TYPE_REF_OBJ_P (type)
5455 && has_value_dependent_address (expr))
5456 /* If we want the address and it's value-dependent, don't fold. */;
5457 else if (!type_unknown_p (expr))
5458 expr = fold_non_dependent_expr_sfinae (expr, complain);
5459 if (error_operand_p (expr))
5460 return error_mark_node;
5461 expr_type = TREE_TYPE (expr);
5462 if (TREE_CODE (type) == REFERENCE_TYPE)
5463 expr = mark_lvalue_use (expr);
5464 else
5465 expr = mark_rvalue_use (expr);
5467 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5468 to a non-type argument of "nullptr". */
5469 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5470 expr = convert (type, expr);
5472 /* In C++11, integral or enumeration non-type template arguments can be
5473 arbitrary constant expressions. Pointer and pointer to
5474 member arguments can be general constant expressions that evaluate
5475 to a null value, but otherwise still need to be of a specific form. */
5476 if (cxx_dialect >= cxx0x)
5478 if (TREE_CODE (expr) == PTRMEM_CST)
5479 /* A PTRMEM_CST is already constant, and a valid template
5480 argument for a parameter of pointer to member type, we just want
5481 to leave it in that form rather than lower it to a
5482 CONSTRUCTOR. */;
5483 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5484 expr = maybe_constant_value (expr);
5485 else if (TYPE_PTR_OR_PTRMEM_P (type))
5487 tree folded = maybe_constant_value (expr);
5488 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5489 : null_member_pointer_value_p (folded))
5490 expr = folded;
5494 /* HACK: Due to double coercion, we can get a
5495 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5496 which is the tree that we built on the first call (see
5497 below when coercing to reference to object or to reference to
5498 function). We just strip everything and get to the arg.
5499 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5500 for examples. */
5501 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5503 tree probe_type, probe = expr;
5504 if (REFERENCE_REF_P (probe))
5505 probe = TREE_OPERAND (probe, 0);
5506 probe_type = TREE_TYPE (probe);
5507 if (TREE_CODE (probe) == NOP_EXPR)
5509 /* ??? Maybe we could use convert_from_reference here, but we
5510 would need to relax its constraints because the NOP_EXPR
5511 could actually change the type to something more cv-qualified,
5512 and this is not folded by convert_from_reference. */
5513 tree addr = TREE_OPERAND (probe, 0);
5514 gcc_assert (TREE_CODE (probe_type) == REFERENCE_TYPE);
5515 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5516 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5517 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5518 (TREE_TYPE (probe_type),
5519 TREE_TYPE (TREE_TYPE (addr))));
5521 expr = TREE_OPERAND (addr, 0);
5522 expr_type = TREE_TYPE (expr);
5526 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5527 parameter is a pointer to object, through decay and
5528 qualification conversion. Let's strip everything. */
5529 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5531 STRIP_NOPS (expr);
5532 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5533 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5534 /* Skip the ADDR_EXPR only if it is part of the decay for
5535 an array. Otherwise, it is part of the original argument
5536 in the source code. */
5537 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5538 expr = TREE_OPERAND (expr, 0);
5539 expr_type = TREE_TYPE (expr);
5542 /* [temp.arg.nontype]/5, bullet 1
5544 For a non-type template-parameter of integral or enumeration type,
5545 integral promotions (_conv.prom_) and integral conversions
5546 (_conv.integral_) are applied. */
5547 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5549 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5550 t = maybe_constant_value (t);
5551 if (t != error_mark_node)
5552 expr = t;
5554 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5555 return error_mark_node;
5557 /* Notice that there are constant expressions like '4 % 0' which
5558 do not fold into integer constants. */
5559 if (TREE_CODE (expr) != INTEGER_CST)
5561 if (complain & tf_error)
5563 int errs = errorcount, warns = warningcount;
5564 if (processing_template_decl
5565 && !require_potential_constant_expression (expr))
5566 return NULL_TREE;
5567 expr = cxx_constant_value (expr);
5568 if (errorcount > errs || warningcount > warns)
5569 inform (EXPR_LOC_OR_HERE (expr),
5570 "in template argument for type %qT ", type);
5571 if (expr == error_mark_node)
5572 return NULL_TREE;
5573 /* else cxx_constant_value complained but gave us
5574 a real constant, so go ahead. */
5575 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5577 else
5578 return NULL_TREE;
5581 /* [temp.arg.nontype]/5, bullet 2
5583 For a non-type template-parameter of type pointer to object,
5584 qualification conversions (_conv.qual_) and the array-to-pointer
5585 conversion (_conv.array_) are applied. */
5586 else if (TYPE_PTROBV_P (type))
5588 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5590 A template-argument for a non-type, non-template template-parameter
5591 shall be one of: [...]
5593 -- the name of a non-type template-parameter;
5594 -- the address of an object or function with external linkage, [...]
5595 expressed as "& id-expression" where the & is optional if the name
5596 refers to a function or array, or if the corresponding
5597 template-parameter is a reference.
5599 Here, we do not care about functions, as they are invalid anyway
5600 for a parameter of type pointer-to-object. */
5602 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5603 /* Non-type template parameters are OK. */
5605 else if (cxx_dialect >= cxx0x && integer_zerop (expr))
5606 /* Null pointer values are OK in C++11. */;
5607 else if (TREE_CODE (expr) != ADDR_EXPR
5608 && TREE_CODE (expr_type) != ARRAY_TYPE)
5610 if (TREE_CODE (expr) == VAR_DECL)
5612 error ("%qD is not a valid template argument "
5613 "because %qD is a variable, not the address of "
5614 "a variable",
5615 expr, expr);
5616 return NULL_TREE;
5618 /* Other values, like integer constants, might be valid
5619 non-type arguments of some other type. */
5620 return error_mark_node;
5622 else
5624 tree decl;
5626 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5627 ? TREE_OPERAND (expr, 0) : expr);
5628 if (TREE_CODE (decl) != VAR_DECL)
5630 error ("%qE is not a valid template argument of type %qT "
5631 "because %qE is not a variable",
5632 expr, type, decl);
5633 return NULL_TREE;
5635 else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
5637 error ("%qE is not a valid template argument of type %qT "
5638 "because %qD does not have external linkage",
5639 expr, type, decl);
5640 return NULL_TREE;
5642 else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
5644 error ("%qE is not a valid template argument of type %qT "
5645 "because %qD has no linkage",
5646 expr, type, decl);
5647 return NULL_TREE;
5651 expr = decay_conversion (expr, complain);
5652 if (expr == error_mark_node)
5653 return error_mark_node;
5655 expr = perform_qualification_conversions (type, expr);
5656 if (expr == error_mark_node)
5657 return error_mark_node;
5659 /* [temp.arg.nontype]/5, bullet 3
5661 For a non-type template-parameter of type reference to object, no
5662 conversions apply. The type referred to by the reference may be more
5663 cv-qualified than the (otherwise identical) type of the
5664 template-argument. The template-parameter is bound directly to the
5665 template-argument, which must be an lvalue. */
5666 else if (TYPE_REF_OBJ_P (type))
5668 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5669 expr_type))
5670 return error_mark_node;
5672 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5674 error ("%qE is not a valid template argument for type %qT "
5675 "because of conflicts in cv-qualification", expr, type);
5676 return NULL_TREE;
5679 if (!real_lvalue_p (expr))
5681 error ("%qE is not a valid template argument for type %qT "
5682 "because it is not an lvalue", expr, type);
5683 return NULL_TREE;
5686 /* [temp.arg.nontype]/1
5688 A template-argument for a non-type, non-template template-parameter
5689 shall be one of: [...]
5691 -- the address of an object or function with external linkage. */
5692 if (TREE_CODE (expr) == INDIRECT_REF
5693 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5695 expr = TREE_OPERAND (expr, 0);
5696 if (DECL_P (expr))
5698 error ("%q#D is not a valid template argument for type %qT "
5699 "because a reference variable does not have a constant "
5700 "address", expr, type);
5701 return NULL_TREE;
5705 if (!DECL_P (expr))
5707 error ("%qE is not a valid template argument for type %qT "
5708 "because it is not an object with external linkage",
5709 expr, type);
5710 return NULL_TREE;
5713 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5715 error ("%qE is not a valid template argument for type %qT "
5716 "because object %qD has not external linkage",
5717 expr, type, expr);
5718 return NULL_TREE;
5721 expr = build_nop (type, build_address (expr));
5723 /* [temp.arg.nontype]/5, bullet 4
5725 For a non-type template-parameter of type pointer to function, only
5726 the function-to-pointer conversion (_conv.func_) is applied. If the
5727 template-argument represents a set of overloaded functions (or a
5728 pointer to such), the matching function is selected from the set
5729 (_over.over_). */
5730 else if (TYPE_PTRFN_P (type))
5732 /* If the argument is a template-id, we might not have enough
5733 context information to decay the pointer. */
5734 if (!type_unknown_p (expr_type))
5736 expr = decay_conversion (expr, complain);
5737 if (expr == error_mark_node)
5738 return error_mark_node;
5741 if (cxx_dialect >= cxx0x && integer_zerop (expr))
5742 /* Null pointer values are OK in C++11. */
5743 return perform_qualification_conversions (type, expr);
5745 expr = convert_nontype_argument_function (type, expr);
5746 if (!expr || expr == error_mark_node)
5747 return expr;
5749 if (TREE_CODE (expr) != ADDR_EXPR)
5751 error ("%qE is not a valid template argument for type %qT", expr, type);
5752 error ("it must be the address of a function with external linkage");
5753 return NULL_TREE;
5756 /* [temp.arg.nontype]/5, bullet 5
5758 For a non-type template-parameter of type reference to function, no
5759 conversions apply. If the template-argument represents a set of
5760 overloaded functions, the matching function is selected from the set
5761 (_over.over_). */
5762 else if (TYPE_REFFN_P (type))
5764 if (TREE_CODE (expr) == ADDR_EXPR)
5766 error ("%qE is not a valid template argument for type %qT "
5767 "because it is a pointer", expr, type);
5768 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5769 return NULL_TREE;
5772 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5773 if (!expr || expr == error_mark_node)
5774 return expr;
5776 expr = build_nop (type, build_address (expr));
5778 /* [temp.arg.nontype]/5, bullet 6
5780 For a non-type template-parameter of type pointer to member function,
5781 no conversions apply. If the template-argument represents a set of
5782 overloaded member functions, the matching member function is selected
5783 from the set (_over.over_). */
5784 else if (TYPE_PTRMEMFUNC_P (type))
5786 expr = instantiate_type (type, expr, tf_none);
5787 if (expr == error_mark_node)
5788 return error_mark_node;
5790 /* [temp.arg.nontype] bullet 1 says the pointer to member
5791 expression must be a pointer-to-member constant. */
5792 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5793 return error_mark_node;
5795 /* There is no way to disable standard conversions in
5796 resolve_address_of_overloaded_function (called by
5797 instantiate_type). It is possible that the call succeeded by
5798 converting &B::I to &D::I (where B is a base of D), so we need
5799 to reject this conversion here.
5801 Actually, even if there was a way to disable standard conversions,
5802 it would still be better to reject them here so that we can
5803 provide a superior diagnostic. */
5804 if (!same_type_p (TREE_TYPE (expr), type))
5806 error ("%qE is not a valid template argument for type %qT "
5807 "because it is of type %qT", expr, type,
5808 TREE_TYPE (expr));
5809 /* If we are just one standard conversion off, explain. */
5810 if (can_convert (type, TREE_TYPE (expr), complain))
5811 inform (input_location,
5812 "standard conversions are not allowed in this context");
5813 return NULL_TREE;
5816 /* [temp.arg.nontype]/5, bullet 7
5818 For a non-type template-parameter of type pointer to data member,
5819 qualification conversions (_conv.qual_) are applied. */
5820 else if (TYPE_PTRDATAMEM_P (type))
5822 /* [temp.arg.nontype] bullet 1 says the pointer to member
5823 expression must be a pointer-to-member constant. */
5824 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5825 return error_mark_node;
5827 expr = perform_qualification_conversions (type, expr);
5828 if (expr == error_mark_node)
5829 return expr;
5831 else if (NULLPTR_TYPE_P (type))
5833 if (expr != nullptr_node)
5835 error ("%qE is not a valid template argument for type %qT "
5836 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5837 return NULL_TREE;
5839 return expr;
5841 /* A template non-type parameter must be one of the above. */
5842 else
5843 gcc_unreachable ();
5845 /* Sanity check: did we actually convert the argument to the
5846 right type? */
5847 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5848 (type, TREE_TYPE (expr)));
5849 return expr;
5852 /* Subroutine of coerce_template_template_parms, which returns 1 if
5853 PARM_PARM and ARG_PARM match using the rule for the template
5854 parameters of template template parameters. Both PARM and ARG are
5855 template parameters; the rest of the arguments are the same as for
5856 coerce_template_template_parms.
5858 static int
5859 coerce_template_template_parm (tree parm,
5860 tree arg,
5861 tsubst_flags_t complain,
5862 tree in_decl,
5863 tree outer_args)
5865 if (arg == NULL_TREE || arg == error_mark_node
5866 || parm == NULL_TREE || parm == error_mark_node)
5867 return 0;
5869 if (TREE_CODE (arg) != TREE_CODE (parm))
5870 return 0;
5872 switch (TREE_CODE (parm))
5874 case TEMPLATE_DECL:
5875 /* We encounter instantiations of templates like
5876 template <template <template <class> class> class TT>
5877 class C; */
5879 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5880 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5882 if (!coerce_template_template_parms
5883 (parmparm, argparm, complain, in_decl, outer_args))
5884 return 0;
5886 /* Fall through. */
5888 case TYPE_DECL:
5889 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5890 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5891 /* Argument is a parameter pack but parameter is not. */
5892 return 0;
5893 break;
5895 case PARM_DECL:
5896 /* The tsubst call is used to handle cases such as
5898 template <int> class C {};
5899 template <class T, template <T> class TT> class D {};
5900 D<int, C> d;
5902 i.e. the parameter list of TT depends on earlier parameters. */
5903 if (!uses_template_parms (TREE_TYPE (arg))
5904 && !same_type_p
5905 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5906 TREE_TYPE (arg)))
5907 return 0;
5909 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5910 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5911 /* Argument is a parameter pack but parameter is not. */
5912 return 0;
5914 break;
5916 default:
5917 gcc_unreachable ();
5920 return 1;
5924 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5925 template template parameters. Both PARM_PARMS and ARG_PARMS are
5926 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5927 or PARM_DECL.
5929 Consider the example:
5930 template <class T> class A;
5931 template<template <class U> class TT> class B;
5933 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5934 the parameters to A, and OUTER_ARGS contains A. */
5936 static int
5937 coerce_template_template_parms (tree parm_parms,
5938 tree arg_parms,
5939 tsubst_flags_t complain,
5940 tree in_decl,
5941 tree outer_args)
5943 int nparms, nargs, i;
5944 tree parm, arg;
5945 int variadic_p = 0;
5947 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5948 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5950 nparms = TREE_VEC_LENGTH (parm_parms);
5951 nargs = TREE_VEC_LENGTH (arg_parms);
5953 /* Determine whether we have a parameter pack at the end of the
5954 template template parameter's template parameter list. */
5955 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5957 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5959 if (parm == error_mark_node)
5960 return 0;
5962 switch (TREE_CODE (parm))
5964 case TEMPLATE_DECL:
5965 case TYPE_DECL:
5966 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5967 variadic_p = 1;
5968 break;
5970 case PARM_DECL:
5971 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5972 variadic_p = 1;
5973 break;
5975 default:
5976 gcc_unreachable ();
5980 if (nargs != nparms
5981 && !(variadic_p && nargs >= nparms - 1))
5982 return 0;
5984 /* Check all of the template parameters except the parameter pack at
5985 the end (if any). */
5986 for (i = 0; i < nparms - variadic_p; ++i)
5988 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5989 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5990 continue;
5992 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5993 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5995 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5996 outer_args))
5997 return 0;
6001 if (variadic_p)
6003 /* Check each of the template parameters in the template
6004 argument against the template parameter pack at the end of
6005 the template template parameter. */
6006 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6007 return 0;
6009 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6011 for (; i < nargs; ++i)
6013 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6014 continue;
6016 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6018 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6019 outer_args))
6020 return 0;
6024 return 1;
6027 /* Verifies that the deduced template arguments (in TARGS) for the
6028 template template parameters (in TPARMS) represent valid bindings,
6029 by comparing the template parameter list of each template argument
6030 to the template parameter list of its corresponding template
6031 template parameter, in accordance with DR150. This
6032 routine can only be called after all template arguments have been
6033 deduced. It will return TRUE if all of the template template
6034 parameter bindings are okay, FALSE otherwise. */
6035 bool
6036 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6038 int i, ntparms = TREE_VEC_LENGTH (tparms);
6039 bool ret = true;
6041 /* We're dealing with template parms in this process. */
6042 ++processing_template_decl;
6044 targs = INNERMOST_TEMPLATE_ARGS (targs);
6046 for (i = 0; i < ntparms; ++i)
6048 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6049 tree targ = TREE_VEC_ELT (targs, i);
6051 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6053 tree packed_args = NULL_TREE;
6054 int idx, len = 1;
6056 if (ARGUMENT_PACK_P (targ))
6058 /* Look inside the argument pack. */
6059 packed_args = ARGUMENT_PACK_ARGS (targ);
6060 len = TREE_VEC_LENGTH (packed_args);
6063 for (idx = 0; idx < len; ++idx)
6065 tree targ_parms = NULL_TREE;
6067 if (packed_args)
6068 /* Extract the next argument from the argument
6069 pack. */
6070 targ = TREE_VEC_ELT (packed_args, idx);
6072 if (PACK_EXPANSION_P (targ))
6073 /* Look at the pattern of the pack expansion. */
6074 targ = PACK_EXPANSION_PATTERN (targ);
6076 /* Extract the template parameters from the template
6077 argument. */
6078 if (TREE_CODE (targ) == TEMPLATE_DECL)
6079 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6080 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6081 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6083 /* Verify that we can coerce the template template
6084 parameters from the template argument to the template
6085 parameter. This requires an exact match. */
6086 if (targ_parms
6087 && !coerce_template_template_parms
6088 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6089 targ_parms,
6090 tf_none,
6091 tparm,
6092 targs))
6094 ret = false;
6095 goto out;
6101 out:
6103 --processing_template_decl;
6104 return ret;
6107 /* Since type attributes aren't mangled, we need to strip them from
6108 template type arguments. */
6110 static tree
6111 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6113 tree mv;
6114 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6115 return arg;
6116 mv = TYPE_MAIN_VARIANT (arg);
6117 arg = strip_typedefs (arg);
6118 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6119 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6121 if (complain & tf_warning)
6122 warning (0, "ignoring attributes on template argument %qT", arg);
6123 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6124 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6126 return arg;
6129 /* Convert the indicated template ARG as necessary to match the
6130 indicated template PARM. Returns the converted ARG, or
6131 error_mark_node if the conversion was unsuccessful. Error and
6132 warning messages are issued under control of COMPLAIN. This
6133 conversion is for the Ith parameter in the parameter list. ARGS is
6134 the full set of template arguments deduced so far. */
6136 static tree
6137 convert_template_argument (tree parm,
6138 tree arg,
6139 tree args,
6140 tsubst_flags_t complain,
6141 int i,
6142 tree in_decl)
6144 tree orig_arg;
6145 tree val;
6146 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6148 if (TREE_CODE (arg) == TREE_LIST
6149 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6151 /* The template argument was the name of some
6152 member function. That's usually
6153 invalid, but static members are OK. In any
6154 case, grab the underlying fields/functions
6155 and issue an error later if required. */
6156 orig_arg = TREE_VALUE (arg);
6157 TREE_TYPE (arg) = unknown_type_node;
6160 orig_arg = arg;
6162 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6163 requires_type = (TREE_CODE (parm) == TYPE_DECL
6164 || requires_tmpl_type);
6166 /* When determining whether an argument pack expansion is a template,
6167 look at the pattern. */
6168 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6169 arg = PACK_EXPANSION_PATTERN (arg);
6171 /* Deal with an injected-class-name used as a template template arg. */
6172 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6174 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6175 if (TREE_CODE (t) == TEMPLATE_DECL)
6177 if (cxx_dialect >= cxx0x)
6178 /* OK under DR 1004. */;
6179 else if (complain & tf_warning_or_error)
6180 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6181 " used as template template argument", TYPE_NAME (arg));
6182 else if (flag_pedantic_errors)
6183 t = arg;
6185 arg = t;
6189 is_tmpl_type =
6190 ((TREE_CODE (arg) == TEMPLATE_DECL
6191 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6192 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6193 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6194 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6196 if (is_tmpl_type
6197 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6198 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6199 arg = TYPE_STUB_DECL (arg);
6201 is_type = TYPE_P (arg) || is_tmpl_type;
6203 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6204 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6206 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6208 if (complain & tf_error)
6209 error ("invalid use of destructor %qE as a type", orig_arg);
6210 return error_mark_node;
6213 permerror (input_location,
6214 "to refer to a type member of a template parameter, "
6215 "use %<typename %E%>", orig_arg);
6217 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6218 TREE_OPERAND (arg, 1),
6219 typename_type,
6220 complain);
6221 arg = orig_arg;
6222 is_type = 1;
6224 if (is_type != requires_type)
6226 if (in_decl)
6228 if (complain & tf_error)
6230 error ("type/value mismatch at argument %d in template "
6231 "parameter list for %qD",
6232 i + 1, in_decl);
6233 if (is_type)
6234 error (" expected a constant of type %qT, got %qT",
6235 TREE_TYPE (parm),
6236 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6237 else if (requires_tmpl_type)
6238 error (" expected a class template, got %qE", orig_arg);
6239 else
6240 error (" expected a type, got %qE", orig_arg);
6243 return error_mark_node;
6245 if (is_tmpl_type ^ requires_tmpl_type)
6247 if (in_decl && (complain & tf_error))
6249 error ("type/value mismatch at argument %d in template "
6250 "parameter list for %qD",
6251 i + 1, in_decl);
6252 if (is_tmpl_type)
6253 error (" expected a type, got %qT", DECL_NAME (arg));
6254 else
6255 error (" expected a class template, got %qT", orig_arg);
6257 return error_mark_node;
6260 if (is_type)
6262 if (requires_tmpl_type)
6264 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6265 val = orig_arg;
6266 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6267 /* The number of argument required is not known yet.
6268 Just accept it for now. */
6269 val = TREE_TYPE (arg);
6270 else
6272 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6273 tree argparm;
6275 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6277 if (coerce_template_template_parms (parmparm, argparm,
6278 complain, in_decl,
6279 args))
6281 val = arg;
6283 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6284 TEMPLATE_DECL. */
6285 if (val != error_mark_node)
6287 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6288 val = TREE_TYPE (val);
6289 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6290 val = make_pack_expansion (val);
6293 else
6295 if (in_decl && (complain & tf_error))
6297 error ("type/value mismatch at argument %d in "
6298 "template parameter list for %qD",
6299 i + 1, in_decl);
6300 error (" expected a template of type %qD, got %qT",
6301 parm, orig_arg);
6304 val = error_mark_node;
6308 else
6309 val = orig_arg;
6310 /* We only form one instance of each template specialization.
6311 Therefore, if we use a non-canonical variant (i.e., a
6312 typedef), any future messages referring to the type will use
6313 the typedef, which is confusing if those future uses do not
6314 themselves also use the typedef. */
6315 if (TYPE_P (val))
6316 val = canonicalize_type_argument (val, complain);
6318 else
6320 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6322 if (invalid_nontype_parm_type_p (t, complain))
6323 return error_mark_node;
6325 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6327 if (same_type_p (t, TREE_TYPE (orig_arg)))
6328 val = orig_arg;
6329 else
6331 /* Not sure if this is reachable, but it doesn't hurt
6332 to be robust. */
6333 error ("type mismatch in nontype parameter pack");
6334 val = error_mark_node;
6337 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6338 /* We used to call digest_init here. However, digest_init
6339 will report errors, which we don't want when complain
6340 is zero. More importantly, digest_init will try too
6341 hard to convert things: for example, `0' should not be
6342 converted to pointer type at this point according to
6343 the standard. Accepting this is not merely an
6344 extension, since deciding whether or not these
6345 conversions can occur is part of determining which
6346 function template to call, or whether a given explicit
6347 argument specification is valid. */
6348 val = convert_nontype_argument (t, orig_arg, complain);
6349 else
6350 val = strip_typedefs_expr (orig_arg);
6352 if (val == NULL_TREE)
6353 val = error_mark_node;
6354 else if (val == error_mark_node && (complain & tf_error))
6355 error ("could not convert template argument %qE to %qT", orig_arg, t);
6357 if (TREE_CODE (val) == SCOPE_REF)
6359 /* Strip typedefs from the SCOPE_REF. */
6360 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6361 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6362 complain);
6363 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6364 QUALIFIED_NAME_IS_TEMPLATE (val));
6368 return val;
6371 /* Coerces the remaining template arguments in INNER_ARGS (from
6372 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6373 Returns the coerced argument pack. PARM_IDX is the position of this
6374 parameter in the template parameter list. ARGS is the original
6375 template argument list. */
6376 static tree
6377 coerce_template_parameter_pack (tree parms,
6378 int parm_idx,
6379 tree args,
6380 tree inner_args,
6381 int arg_idx,
6382 tree new_args,
6383 int* lost,
6384 tree in_decl,
6385 tsubst_flags_t complain)
6387 tree parm = TREE_VEC_ELT (parms, parm_idx);
6388 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6389 tree packed_args;
6390 tree argument_pack;
6391 tree packed_types = NULL_TREE;
6393 if (arg_idx > nargs)
6394 arg_idx = nargs;
6396 packed_args = make_tree_vec (nargs - arg_idx);
6398 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6399 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6401 /* When the template parameter is a non-type template
6402 parameter pack whose type uses parameter packs, we need
6403 to look at each of the template arguments
6404 separately. Build a vector of the types for these
6405 non-type template parameters in PACKED_TYPES. */
6406 tree expansion
6407 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6408 packed_types = tsubst_pack_expansion (expansion, args,
6409 complain, in_decl);
6411 if (packed_types == error_mark_node)
6412 return error_mark_node;
6414 /* Check that we have the right number of arguments. */
6415 if (arg_idx < nargs
6416 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6417 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6419 int needed_parms
6420 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6421 error ("wrong number of template arguments (%d, should be %d)",
6422 nargs, needed_parms);
6423 return error_mark_node;
6426 /* If we aren't able to check the actual arguments now
6427 (because they haven't been expanded yet), we can at least
6428 verify that all of the types used for the non-type
6429 template parameter pack are, in fact, valid for non-type
6430 template parameters. */
6431 if (arg_idx < nargs
6432 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6434 int j, len = TREE_VEC_LENGTH (packed_types);
6435 for (j = 0; j < len; ++j)
6437 tree t = TREE_VEC_ELT (packed_types, j);
6438 if (invalid_nontype_parm_type_p (t, complain))
6439 return error_mark_node;
6444 /* Convert the remaining arguments, which will be a part of the
6445 parameter pack "parm". */
6446 for (; arg_idx < nargs; ++arg_idx)
6448 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6449 tree actual_parm = TREE_VALUE (parm);
6451 if (packed_types && !PACK_EXPANSION_P (arg))
6453 /* When we have a vector of types (corresponding to the
6454 non-type template parameter pack that uses parameter
6455 packs in its type, as mention above), and the
6456 argument is not an expansion (which expands to a
6457 currently unknown number of arguments), clone the
6458 parm and give it the next type in PACKED_TYPES. */
6459 actual_parm = copy_node (actual_parm);
6460 TREE_TYPE (actual_parm) =
6461 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6464 if (arg == error_mark_node)
6466 if (complain & tf_error)
6467 error ("template argument %d is invalid", arg_idx + 1);
6469 else
6470 arg = convert_template_argument (actual_parm,
6471 arg, new_args, complain, parm_idx,
6472 in_decl);
6473 if (arg == error_mark_node)
6474 (*lost)++;
6475 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6478 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6479 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6480 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6481 else
6483 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6484 TREE_TYPE (argument_pack)
6485 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6486 TREE_CONSTANT (argument_pack) = 1;
6489 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6490 #ifdef ENABLE_CHECKING
6491 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6492 TREE_VEC_LENGTH (packed_args));
6493 #endif
6494 return argument_pack;
6497 /* Returns true if the template argument vector ARGS contains
6498 any pack expansions, false otherwise. */
6500 static bool
6501 any_pack_expanson_args_p (tree args)
6503 int i;
6504 if (args)
6505 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6506 if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
6507 return true;
6508 return false;
6511 /* Convert all template arguments to their appropriate types, and
6512 return a vector containing the innermost resulting template
6513 arguments. If any error occurs, return error_mark_node. Error and
6514 warning messages are issued under control of COMPLAIN.
6516 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6517 for arguments not specified in ARGS. Otherwise, if
6518 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6519 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6520 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6521 ARGS. */
6523 static tree
6524 coerce_template_parms (tree parms,
6525 tree args,
6526 tree in_decl,
6527 tsubst_flags_t complain,
6528 bool require_all_args,
6529 bool use_default_args)
6531 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6532 tree inner_args;
6533 tree new_args;
6534 tree new_inner_args;
6535 int saved_unevaluated_operand;
6536 int saved_inhibit_evaluation_warnings;
6538 /* When used as a boolean value, indicates whether this is a
6539 variadic template parameter list. Since it's an int, we can also
6540 subtract it from nparms to get the number of non-variadic
6541 parameters. */
6542 int variadic_p = 0;
6543 int post_variadic_parms = 0;
6545 if (args == error_mark_node)
6546 return error_mark_node;
6548 nparms = TREE_VEC_LENGTH (parms);
6550 /* Determine if there are any parameter packs. */
6551 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6553 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6554 if (variadic_p)
6555 ++post_variadic_parms;
6556 if (template_parameter_pack_p (tparm))
6557 ++variadic_p;
6560 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6561 /* If there are no parameters that follow a parameter pack, we need to
6562 expand any argument packs so that we can deduce a parameter pack from
6563 some non-packed args followed by an argument pack, as in variadic85.C.
6564 If there are such parameters, we need to leave argument packs intact
6565 so the arguments are assigned properly. This can happen when dealing
6566 with a nested class inside a partial specialization of a class
6567 template, as in variadic92.C, or when deducing a template parameter pack
6568 from a sub-declarator, as in variadic114.C. */
6569 if (!post_variadic_parms)
6570 inner_args = expand_template_argument_pack (inner_args);
6572 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6573 if ((nargs > nparms && !variadic_p)
6574 || (nargs < nparms - variadic_p
6575 && require_all_args
6576 && !any_pack_expanson_args_p (inner_args)
6577 && (!use_default_args
6578 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6579 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6581 if (complain & tf_error)
6583 if (variadic_p)
6585 nparms -= variadic_p;
6586 error ("wrong number of template arguments "
6587 "(%d, should be %d or more)", nargs, nparms);
6589 else
6590 error ("wrong number of template arguments "
6591 "(%d, should be %d)", nargs, nparms);
6593 if (in_decl)
6594 error ("provided for %q+D", in_decl);
6597 return error_mark_node;
6600 /* We need to evaluate the template arguments, even though this
6601 template-id may be nested within a "sizeof". */
6602 saved_unevaluated_operand = cp_unevaluated_operand;
6603 cp_unevaluated_operand = 0;
6604 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6605 c_inhibit_evaluation_warnings = 0;
6606 new_inner_args = make_tree_vec (nparms);
6607 new_args = add_outermost_template_args (args, new_inner_args);
6608 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6610 tree arg;
6611 tree parm;
6613 /* Get the Ith template parameter. */
6614 parm = TREE_VEC_ELT (parms, parm_idx);
6616 if (parm == error_mark_node)
6618 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6619 continue;
6622 /* Calculate the next argument. */
6623 if (arg_idx < nargs)
6624 arg = TREE_VEC_ELT (inner_args, arg_idx);
6625 else
6626 arg = NULL_TREE;
6628 if (template_parameter_pack_p (TREE_VALUE (parm))
6629 && !(arg && ARGUMENT_PACK_P (arg)))
6631 /* All remaining arguments will be placed in the
6632 template parameter pack PARM. */
6633 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6634 inner_args, arg_idx,
6635 new_args, &lost,
6636 in_decl, complain);
6638 /* Store this argument. */
6639 if (arg == error_mark_node)
6640 lost++;
6641 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6643 /* We are done with all of the arguments. */
6644 arg_idx = nargs;
6646 continue;
6648 else if (arg)
6650 if (PACK_EXPANSION_P (arg))
6652 /* We don't know how many args we have yet, just
6653 use the unconverted ones for now. */
6654 new_inner_args = inner_args;
6655 break;
6658 else if (require_all_args)
6660 /* There must be a default arg in this case. */
6661 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6662 complain, in_decl);
6663 /* The position of the first default template argument,
6664 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6665 Record that. */
6666 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6667 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6669 else
6670 break;
6672 if (arg == error_mark_node)
6674 if (complain & tf_error)
6675 error ("template argument %d is invalid", arg_idx + 1);
6677 else if (!arg)
6678 /* This only occurs if there was an error in the template
6679 parameter list itself (which we would already have
6680 reported) that we are trying to recover from, e.g., a class
6681 template with a parameter list such as
6682 template<typename..., typename>. */
6683 ++lost;
6684 else
6685 arg = convert_template_argument (TREE_VALUE (parm),
6686 arg, new_args, complain,
6687 parm_idx, in_decl);
6689 if (arg == error_mark_node)
6690 lost++;
6691 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6693 cp_unevaluated_operand = saved_unevaluated_operand;
6694 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6696 if (lost)
6697 return error_mark_node;
6699 #ifdef ENABLE_CHECKING
6700 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6701 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6702 TREE_VEC_LENGTH (new_inner_args));
6703 #endif
6705 return new_inner_args;
6708 /* Returns 1 if template args OT and NT are equivalent. */
6710 static int
6711 template_args_equal (tree ot, tree nt)
6713 if (nt == ot)
6714 return 1;
6715 if (nt == NULL_TREE || ot == NULL_TREE)
6716 return false;
6718 if (TREE_CODE (nt) == TREE_VEC)
6719 /* For member templates */
6720 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6721 else if (PACK_EXPANSION_P (ot))
6722 return (PACK_EXPANSION_P (nt)
6723 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6724 PACK_EXPANSION_PATTERN (nt))
6725 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6726 PACK_EXPANSION_EXTRA_ARGS (nt)));
6727 else if (ARGUMENT_PACK_P (ot))
6729 int i, len;
6730 tree opack, npack;
6732 if (!ARGUMENT_PACK_P (nt))
6733 return 0;
6735 opack = ARGUMENT_PACK_ARGS (ot);
6736 npack = ARGUMENT_PACK_ARGS (nt);
6737 len = TREE_VEC_LENGTH (opack);
6738 if (TREE_VEC_LENGTH (npack) != len)
6739 return 0;
6740 for (i = 0; i < len; ++i)
6741 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6742 TREE_VEC_ELT (npack, i)))
6743 return 0;
6744 return 1;
6746 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6748 /* We get here probably because we are in the middle of substituting
6749 into the pattern of a pack expansion. In that case the
6750 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6751 interested in. So we want to use the initial pack argument for
6752 the comparison. */
6753 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6754 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6755 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6756 return template_args_equal (ot, nt);
6758 else if (TYPE_P (nt))
6759 return TYPE_P (ot) && same_type_p (ot, nt);
6760 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6761 return 0;
6762 else
6763 return cp_tree_equal (ot, nt);
6766 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6767 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6768 NEWARG_PTR with the offending arguments if they are non-NULL. */
6770 static int
6771 comp_template_args_with_info (tree oldargs, tree newargs,
6772 tree *oldarg_ptr, tree *newarg_ptr)
6774 int i;
6776 if (oldargs == newargs)
6777 return 1;
6779 if (!oldargs || !newargs)
6780 return 0;
6782 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6783 return 0;
6785 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6787 tree nt = TREE_VEC_ELT (newargs, i);
6788 tree ot = TREE_VEC_ELT (oldargs, i);
6790 if (! template_args_equal (ot, nt))
6792 if (oldarg_ptr != NULL)
6793 *oldarg_ptr = ot;
6794 if (newarg_ptr != NULL)
6795 *newarg_ptr = nt;
6796 return 0;
6799 return 1;
6802 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6803 of template arguments. Returns 0 otherwise. */
6806 comp_template_args (tree oldargs, tree newargs)
6808 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6811 static void
6812 add_pending_template (tree d)
6814 tree ti = (TYPE_P (d)
6815 ? CLASSTYPE_TEMPLATE_INFO (d)
6816 : DECL_TEMPLATE_INFO (d));
6817 struct pending_template *pt;
6818 int level;
6820 if (TI_PENDING_TEMPLATE_FLAG (ti))
6821 return;
6823 /* We are called both from instantiate_decl, where we've already had a
6824 tinst_level pushed, and instantiate_template, where we haven't.
6825 Compensate. */
6826 level = !current_tinst_level || current_tinst_level->decl != d;
6828 if (level)
6829 push_tinst_level (d);
6831 pt = ggc_alloc_pending_template ();
6832 pt->next = NULL;
6833 pt->tinst = current_tinst_level;
6834 if (last_pending_template)
6835 last_pending_template->next = pt;
6836 else
6837 pending_templates = pt;
6839 last_pending_template = pt;
6841 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6843 if (level)
6844 pop_tinst_level ();
6848 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6849 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6850 documentation for TEMPLATE_ID_EXPR. */
6852 tree
6853 lookup_template_function (tree fns, tree arglist)
6855 tree type;
6857 if (fns == error_mark_node || arglist == error_mark_node)
6858 return error_mark_node;
6860 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6862 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6864 error ("%q#D is not a function template", fns);
6865 return error_mark_node;
6868 if (BASELINK_P (fns))
6870 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6871 unknown_type_node,
6872 BASELINK_FUNCTIONS (fns),
6873 arglist);
6874 return fns;
6877 type = TREE_TYPE (fns);
6878 if (TREE_CODE (fns) == OVERLOAD || !type)
6879 type = unknown_type_node;
6881 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6884 /* Within the scope of a template class S<T>, the name S gets bound
6885 (in build_self_reference) to a TYPE_DECL for the class, not a
6886 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6887 or one of its enclosing classes, and that type is a template,
6888 return the associated TEMPLATE_DECL. Otherwise, the original
6889 DECL is returned.
6891 Also handle the case when DECL is a TREE_LIST of ambiguous
6892 injected-class-names from different bases. */
6894 tree
6895 maybe_get_template_decl_from_type_decl (tree decl)
6897 if (decl == NULL_TREE)
6898 return decl;
6900 /* DR 176: A lookup that finds an injected-class-name (10.2
6901 [class.member.lookup]) can result in an ambiguity in certain cases
6902 (for example, if it is found in more than one base class). If all of
6903 the injected-class-names that are found refer to specializations of
6904 the same class template, and if the name is followed by a
6905 template-argument-list, the reference refers to the class template
6906 itself and not a specialization thereof, and is not ambiguous. */
6907 if (TREE_CODE (decl) == TREE_LIST)
6909 tree t, tmpl = NULL_TREE;
6910 for (t = decl; t; t = TREE_CHAIN (t))
6912 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6913 if (!tmpl)
6914 tmpl = elt;
6915 else if (tmpl != elt)
6916 break;
6918 if (tmpl && t == NULL_TREE)
6919 return tmpl;
6920 else
6921 return decl;
6924 return (decl != NULL_TREE
6925 && DECL_SELF_REFERENCE_P (decl)
6926 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6927 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6930 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6931 parameters, find the desired type.
6933 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6935 IN_DECL, if non-NULL, is the template declaration we are trying to
6936 instantiate.
6938 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6939 the class we are looking up.
6941 Issue error and warning messages under control of COMPLAIN.
6943 If the template class is really a local class in a template
6944 function, then the FUNCTION_CONTEXT is the function in which it is
6945 being instantiated.
6947 ??? Note that this function is currently called *twice* for each
6948 template-id: the first time from the parser, while creating the
6949 incomplete type (finish_template_type), and the second type during the
6950 real instantiation (instantiate_template_class). This is surely something
6951 that we want to avoid. It also causes some problems with argument
6952 coercion (see convert_nontype_argument for more information on this). */
6954 static tree
6955 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
6956 int entering_scope, tsubst_flags_t complain)
6958 tree templ = NULL_TREE, parmlist;
6959 tree t;
6960 void **slot;
6961 spec_entry *entry;
6962 spec_entry elt;
6963 hashval_t hash;
6965 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6967 tree value = innermost_non_namespace_value (d1);
6968 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6969 templ = value;
6970 else
6972 if (context)
6973 push_decl_namespace (context);
6974 templ = lookup_name (d1);
6975 templ = maybe_get_template_decl_from_type_decl (templ);
6976 if (context)
6977 pop_decl_namespace ();
6979 if (templ)
6980 context = DECL_CONTEXT (templ);
6982 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6984 tree type = TREE_TYPE (d1);
6986 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6987 an implicit typename for the second A. Deal with it. */
6988 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6989 type = TREE_TYPE (type);
6991 if (CLASSTYPE_TEMPLATE_INFO (type))
6993 templ = CLASSTYPE_TI_TEMPLATE (type);
6994 d1 = DECL_NAME (templ);
6997 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6998 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7000 templ = TYPE_TI_TEMPLATE (d1);
7001 d1 = DECL_NAME (templ);
7003 else if (TREE_CODE (d1) == TEMPLATE_DECL
7004 && DECL_TEMPLATE_RESULT (d1)
7005 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7007 templ = d1;
7008 d1 = DECL_NAME (templ);
7009 context = DECL_CONTEXT (templ);
7012 /* Issue an error message if we didn't find a template. */
7013 if (! templ)
7015 if (complain & tf_error)
7016 error ("%qT is not a template", d1);
7017 return error_mark_node;
7020 if (TREE_CODE (templ) != TEMPLATE_DECL
7021 /* Make sure it's a user visible template, if it was named by
7022 the user. */
7023 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7024 && !PRIMARY_TEMPLATE_P (templ)))
7026 if (complain & tf_error)
7028 error ("non-template type %qT used as a template", d1);
7029 if (in_decl)
7030 error ("for template declaration %q+D", in_decl);
7032 return error_mark_node;
7035 complain &= ~tf_user;
7037 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7039 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7040 template arguments */
7042 tree parm;
7043 tree arglist2;
7044 tree outer;
7046 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7048 /* Consider an example where a template template parameter declared as
7050 template <class T, class U = std::allocator<T> > class TT
7052 The template parameter level of T and U are one level larger than
7053 of TT. To proper process the default argument of U, say when an
7054 instantiation `TT<int>' is seen, we need to build the full
7055 arguments containing {int} as the innermost level. Outer levels,
7056 available when not appearing as default template argument, can be
7057 obtained from the arguments of the enclosing template.
7059 Suppose that TT is later substituted with std::vector. The above
7060 instantiation is `TT<int, std::allocator<T> >' with TT at
7061 level 1, and T at level 2, while the template arguments at level 1
7062 becomes {std::vector} and the inner level 2 is {int}. */
7064 outer = DECL_CONTEXT (templ);
7065 if (outer)
7066 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7067 else if (current_template_parms)
7068 /* This is an argument of the current template, so we haven't set
7069 DECL_CONTEXT yet. */
7070 outer = current_template_args ();
7072 if (outer)
7073 arglist = add_to_template_args (outer, arglist);
7075 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7076 complain,
7077 /*require_all_args=*/true,
7078 /*use_default_args=*/true);
7079 if (arglist2 == error_mark_node
7080 || (!uses_template_parms (arglist2)
7081 && check_instantiated_args (templ, arglist2, complain)))
7082 return error_mark_node;
7084 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7085 return parm;
7087 else
7089 tree template_type = TREE_TYPE (templ);
7090 tree gen_tmpl;
7091 tree type_decl;
7092 tree found = NULL_TREE;
7093 int arg_depth;
7094 int parm_depth;
7095 int is_dependent_type;
7096 int use_partial_inst_tmpl = false;
7098 if (template_type == error_mark_node)
7099 /* An error occured while building the template TEMPL, and a
7100 diagnostic has most certainly been emitted for that
7101 already. Let's propagate that error. */
7102 return error_mark_node;
7104 gen_tmpl = most_general_template (templ);
7105 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7106 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7107 arg_depth = TMPL_ARGS_DEPTH (arglist);
7109 if (arg_depth == 1 && parm_depth > 1)
7111 /* We've been given an incomplete set of template arguments.
7112 For example, given:
7114 template <class T> struct S1 {
7115 template <class U> struct S2 {};
7116 template <class U> struct S2<U*> {};
7119 we will be called with an ARGLIST of `U*', but the
7120 TEMPLATE will be `template <class T> template
7121 <class U> struct S1<T>::S2'. We must fill in the missing
7122 arguments. */
7123 arglist
7124 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7125 arglist);
7126 arg_depth = TMPL_ARGS_DEPTH (arglist);
7129 /* Now we should have enough arguments. */
7130 gcc_assert (parm_depth == arg_depth);
7132 /* From here on, we're only interested in the most general
7133 template. */
7135 /* Calculate the BOUND_ARGS. These will be the args that are
7136 actually tsubst'd into the definition to create the
7137 instantiation. */
7138 if (parm_depth > 1)
7140 /* We have multiple levels of arguments to coerce, at once. */
7141 int i;
7142 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7144 tree bound_args = make_tree_vec (parm_depth);
7146 for (i = saved_depth,
7147 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7148 i > 0 && t != NULL_TREE;
7149 --i, t = TREE_CHAIN (t))
7151 tree a;
7152 if (i == saved_depth)
7153 a = coerce_template_parms (TREE_VALUE (t),
7154 arglist, gen_tmpl,
7155 complain,
7156 /*require_all_args=*/true,
7157 /*use_default_args=*/true);
7158 else
7159 /* Outer levels should have already been coerced. */
7160 a = TMPL_ARGS_LEVEL (arglist, i);
7162 /* Don't process further if one of the levels fails. */
7163 if (a == error_mark_node)
7165 /* Restore the ARGLIST to its full size. */
7166 TREE_VEC_LENGTH (arglist) = saved_depth;
7167 return error_mark_node;
7170 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7172 /* We temporarily reduce the length of the ARGLIST so
7173 that coerce_template_parms will see only the arguments
7174 corresponding to the template parameters it is
7175 examining. */
7176 TREE_VEC_LENGTH (arglist)--;
7179 /* Restore the ARGLIST to its full size. */
7180 TREE_VEC_LENGTH (arglist) = saved_depth;
7182 arglist = bound_args;
7184 else
7185 arglist
7186 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7187 INNERMOST_TEMPLATE_ARGS (arglist),
7188 gen_tmpl,
7189 complain,
7190 /*require_all_args=*/true,
7191 /*use_default_args=*/true);
7193 if (arglist == error_mark_node)
7194 /* We were unable to bind the arguments. */
7195 return error_mark_node;
7197 /* In the scope of a template class, explicit references to the
7198 template class refer to the type of the template, not any
7199 instantiation of it. For example, in:
7201 template <class T> class C { void f(C<T>); }
7203 the `C<T>' is just the same as `C'. Outside of the
7204 class, however, such a reference is an instantiation. */
7205 if ((entering_scope
7206 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7207 || currently_open_class (template_type))
7208 /* comp_template_args is expensive, check it last. */
7209 && comp_template_args (TYPE_TI_ARGS (template_type),
7210 arglist))
7211 return template_type;
7213 /* If we already have this specialization, return it. */
7214 elt.tmpl = gen_tmpl;
7215 elt.args = arglist;
7216 hash = hash_specialization (&elt);
7217 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7218 &elt, hash);
7220 if (entry)
7221 return entry->spec;
7223 is_dependent_type = uses_template_parms (arglist);
7225 /* If the deduced arguments are invalid, then the binding
7226 failed. */
7227 if (!is_dependent_type
7228 && check_instantiated_args (gen_tmpl,
7229 INNERMOST_TEMPLATE_ARGS (arglist),
7230 complain))
7231 return error_mark_node;
7233 if (!is_dependent_type
7234 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7235 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7236 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7238 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7239 DECL_NAME (gen_tmpl),
7240 /*tag_scope=*/ts_global);
7241 return found;
7244 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7245 complain, in_decl);
7246 if (context == error_mark_node)
7247 return error_mark_node;
7249 if (!context)
7250 context = global_namespace;
7252 /* Create the type. */
7253 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7255 if (!is_dependent_type)
7257 set_current_access_from_decl (TYPE_NAME (template_type));
7258 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7259 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7260 arglist, complain, in_decl),
7261 SCOPED_ENUM_P (template_type), NULL);
7263 else
7265 /* We don't want to call start_enum for this type, since
7266 the values for the enumeration constants may involve
7267 template parameters. And, no one should be interested
7268 in the enumeration constants for such a type. */
7269 t = cxx_make_type (ENUMERAL_TYPE);
7270 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7272 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7273 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7274 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7276 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7278 /* The user referred to a specialization of an alias
7279 template represented by GEN_TMPL.
7281 [temp.alias]/2 says:
7283 When a template-id refers to the specialization of an
7284 alias template, it is equivalent to the associated
7285 type obtained by substitution of its
7286 template-arguments for the template-parameters in the
7287 type-id of the alias template. */
7289 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7290 /* Note that the call above (by indirectly calling
7291 register_specialization in tsubst_decl) registers the
7292 TYPE_DECL representing the specialization of the alias
7293 template. So next time someone substitutes ARGLIST for
7294 the template parms into the alias template (GEN_TMPL),
7295 she'll get that TYPE_DECL back. */
7297 if (t == error_mark_node)
7298 return t;
7300 else if (CLASS_TYPE_P (template_type))
7302 t = make_class_type (TREE_CODE (template_type));
7303 CLASSTYPE_DECLARED_CLASS (t)
7304 = CLASSTYPE_DECLARED_CLASS (template_type);
7305 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7306 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7308 /* A local class. Make sure the decl gets registered properly. */
7309 if (context == current_function_decl)
7310 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7312 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7313 /* This instantiation is another name for the primary
7314 template type. Set the TYPE_CANONICAL field
7315 appropriately. */
7316 TYPE_CANONICAL (t) = template_type;
7317 else if (any_template_arguments_need_structural_equality_p (arglist))
7318 /* Some of the template arguments require structural
7319 equality testing, so this template class requires
7320 structural equality testing. */
7321 SET_TYPE_STRUCTURAL_EQUALITY (t);
7323 else
7324 gcc_unreachable ();
7326 /* If we called start_enum or pushtag above, this information
7327 will already be set up. */
7328 if (!TYPE_NAME (t))
7330 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7332 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7333 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7334 DECL_SOURCE_LOCATION (type_decl)
7335 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7337 else
7338 type_decl = TYPE_NAME (t);
7340 if (CLASS_TYPE_P (template_type))
7342 TREE_PRIVATE (type_decl)
7343 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
7344 TREE_PROTECTED (type_decl)
7345 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
7346 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7348 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7349 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7353 /* Let's consider the explicit specialization of a member
7354 of a class template specialization that is implicitely instantiated,
7355 e.g.:
7356 template<class T>
7357 struct S
7359 template<class U> struct M {}; //#0
7362 template<>
7363 template<>
7364 struct S<int>::M<char> //#1
7366 int i;
7368 [temp.expl.spec]/4 says this is valid.
7370 In this case, when we write:
7371 S<int>::M<char> m;
7373 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7374 the one of #0.
7376 When we encounter #1, we want to store the partial instantiation
7377 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7379 For all cases other than this "explicit specialization of member of a
7380 class template", we just want to store the most general template into
7381 the CLASSTYPE_TI_TEMPLATE of M.
7383 This case of "explicit specialization of member of a class template"
7384 only happens when:
7385 1/ the enclosing class is an instantiation of, and therefore not
7386 the same as, the context of the most general template, and
7387 2/ we aren't looking at the partial instantiation itself, i.e.
7388 the innermost arguments are not the same as the innermost parms of
7389 the most general template.
7391 So it's only when 1/ and 2/ happens that we want to use the partial
7392 instantiation of the member template in lieu of its most general
7393 template. */
7395 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7396 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7397 /* the enclosing class must be an instantiation... */
7398 && CLASS_TYPE_P (context)
7399 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7401 tree partial_inst_args;
7402 TREE_VEC_LENGTH (arglist)--;
7403 ++processing_template_decl;
7404 partial_inst_args =
7405 tsubst (INNERMOST_TEMPLATE_ARGS
7406 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7407 arglist, complain, NULL_TREE);
7408 --processing_template_decl;
7409 TREE_VEC_LENGTH (arglist)++;
7410 use_partial_inst_tmpl =
7411 /*...and we must not be looking at the partial instantiation
7412 itself. */
7413 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7414 partial_inst_args);
7417 if (!use_partial_inst_tmpl)
7418 /* This case is easy; there are no member templates involved. */
7419 found = gen_tmpl;
7420 else
7422 /* This is a full instantiation of a member template. Find
7423 the partial instantiation of which this is an instance. */
7425 /* Temporarily reduce by one the number of levels in the ARGLIST
7426 so as to avoid comparing the last set of arguments. */
7427 TREE_VEC_LENGTH (arglist)--;
7428 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7429 TREE_VEC_LENGTH (arglist)++;
7430 /* FOUND is either a proper class type, or an alias
7431 template specialization. In the later case, it's a
7432 TYPE_DECL, resulting from the substituting of arguments
7433 for parameters in the TYPE_DECL of the alias template
7434 done earlier. So be careful while getting the template
7435 of FOUND. */
7436 found = TREE_CODE (found) == TYPE_DECL
7437 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7438 : CLASSTYPE_TI_TEMPLATE (found);
7441 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7443 elt.spec = t;
7444 slot = htab_find_slot_with_hash (type_specializations,
7445 &elt, hash, INSERT);
7446 entry = ggc_alloc_spec_entry ();
7447 *entry = elt;
7448 *slot = entry;
7450 /* Note this use of the partial instantiation so we can check it
7451 later in maybe_process_partial_specialization. */
7452 DECL_TEMPLATE_INSTANTIATIONS (templ)
7453 = tree_cons (arglist, t,
7454 DECL_TEMPLATE_INSTANTIATIONS (templ));
7456 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
7457 /* Now that the type has been registered on the instantiations
7458 list, we set up the enumerators. Because the enumeration
7459 constants may involve the enumeration type itself, we make
7460 sure to register the type first, and then create the
7461 constants. That way, doing tsubst_expr for the enumeration
7462 constants won't result in recursive calls here; we'll find
7463 the instantiation and exit above. */
7464 tsubst_enum (template_type, t, arglist);
7466 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7467 /* If the type makes use of template parameters, the
7468 code that generates debugging information will crash. */
7469 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7471 /* Possibly limit visibility based on template args. */
7472 TREE_PUBLIC (type_decl) = 1;
7473 determine_visibility (type_decl);
7475 return t;
7479 /* Wrapper for lookup_template_class_1. */
7481 tree
7482 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7483 int entering_scope, tsubst_flags_t complain)
7485 tree ret;
7486 timevar_push (TV_TEMPLATE_INST);
7487 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7488 entering_scope, complain);
7489 timevar_pop (TV_TEMPLATE_INST);
7490 return ret;
7493 struct pair_fn_data
7495 tree_fn_t fn;
7496 void *data;
7497 /* True when we should also visit template parameters that occur in
7498 non-deduced contexts. */
7499 bool include_nondeduced_p;
7500 struct pointer_set_t *visited;
7503 /* Called from for_each_template_parm via walk_tree. */
7505 static tree
7506 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7508 tree t = *tp;
7509 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7510 tree_fn_t fn = pfd->fn;
7511 void *data = pfd->data;
7513 if (TYPE_P (t)
7514 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7515 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7516 pfd->include_nondeduced_p))
7517 return error_mark_node;
7519 switch (TREE_CODE (t))
7521 case RECORD_TYPE:
7522 if (TYPE_PTRMEMFUNC_P (t))
7523 break;
7524 /* Fall through. */
7526 case UNION_TYPE:
7527 case ENUMERAL_TYPE:
7528 if (!TYPE_TEMPLATE_INFO (t))
7529 *walk_subtrees = 0;
7530 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7531 fn, data, pfd->visited,
7532 pfd->include_nondeduced_p))
7533 return error_mark_node;
7534 break;
7536 case INTEGER_TYPE:
7537 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7538 fn, data, pfd->visited,
7539 pfd->include_nondeduced_p)
7540 || for_each_template_parm (TYPE_MAX_VALUE (t),
7541 fn, data, pfd->visited,
7542 pfd->include_nondeduced_p))
7543 return error_mark_node;
7544 break;
7546 case METHOD_TYPE:
7547 /* Since we're not going to walk subtrees, we have to do this
7548 explicitly here. */
7549 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7550 pfd->visited, pfd->include_nondeduced_p))
7551 return error_mark_node;
7552 /* Fall through. */
7554 case FUNCTION_TYPE:
7555 /* Check the return type. */
7556 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7557 pfd->include_nondeduced_p))
7558 return error_mark_node;
7560 /* Check the parameter types. Since default arguments are not
7561 instantiated until they are needed, the TYPE_ARG_TYPES may
7562 contain expressions that involve template parameters. But,
7563 no-one should be looking at them yet. And, once they're
7564 instantiated, they don't contain template parameters, so
7565 there's no point in looking at them then, either. */
7567 tree parm;
7569 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7570 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7571 pfd->visited, pfd->include_nondeduced_p))
7572 return error_mark_node;
7574 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7575 want walk_tree walking into them itself. */
7576 *walk_subtrees = 0;
7578 break;
7580 case TYPEOF_TYPE:
7581 case UNDERLYING_TYPE:
7582 if (pfd->include_nondeduced_p
7583 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7584 pfd->visited,
7585 pfd->include_nondeduced_p))
7586 return error_mark_node;
7587 break;
7589 case FUNCTION_DECL:
7590 case VAR_DECL:
7591 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7592 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7593 pfd->visited, pfd->include_nondeduced_p))
7594 return error_mark_node;
7595 /* Fall through. */
7597 case PARM_DECL:
7598 case CONST_DECL:
7599 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7600 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7601 pfd->visited, pfd->include_nondeduced_p))
7602 return error_mark_node;
7603 if (DECL_CONTEXT (t)
7604 && pfd->include_nondeduced_p
7605 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7606 pfd->visited, pfd->include_nondeduced_p))
7607 return error_mark_node;
7608 break;
7610 case BOUND_TEMPLATE_TEMPLATE_PARM:
7611 /* Record template parameters such as `T' inside `TT<T>'. */
7612 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7613 pfd->include_nondeduced_p))
7614 return error_mark_node;
7615 /* Fall through. */
7617 case TEMPLATE_TEMPLATE_PARM:
7618 case TEMPLATE_TYPE_PARM:
7619 case TEMPLATE_PARM_INDEX:
7620 if (fn && (*fn)(t, data))
7621 return error_mark_node;
7622 else if (!fn)
7623 return error_mark_node;
7624 break;
7626 case TEMPLATE_DECL:
7627 /* A template template parameter is encountered. */
7628 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7629 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7630 pfd->include_nondeduced_p))
7631 return error_mark_node;
7633 /* Already substituted template template parameter */
7634 *walk_subtrees = 0;
7635 break;
7637 case TYPENAME_TYPE:
7638 if (!fn
7639 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7640 data, pfd->visited,
7641 pfd->include_nondeduced_p))
7642 return error_mark_node;
7643 break;
7645 case CONSTRUCTOR:
7646 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7647 && pfd->include_nondeduced_p
7648 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7649 (TREE_TYPE (t)), fn, data,
7650 pfd->visited, pfd->include_nondeduced_p))
7651 return error_mark_node;
7652 break;
7654 case INDIRECT_REF:
7655 case COMPONENT_REF:
7656 /* If there's no type, then this thing must be some expression
7657 involving template parameters. */
7658 if (!fn && !TREE_TYPE (t))
7659 return error_mark_node;
7660 break;
7662 case MODOP_EXPR:
7663 case CAST_EXPR:
7664 case IMPLICIT_CONV_EXPR:
7665 case REINTERPRET_CAST_EXPR:
7666 case CONST_CAST_EXPR:
7667 case STATIC_CAST_EXPR:
7668 case DYNAMIC_CAST_EXPR:
7669 case ARROW_EXPR:
7670 case DOTSTAR_EXPR:
7671 case TYPEID_EXPR:
7672 case PSEUDO_DTOR_EXPR:
7673 if (!fn)
7674 return error_mark_node;
7675 break;
7677 default:
7678 break;
7681 /* We didn't find any template parameters we liked. */
7682 return NULL_TREE;
7685 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7686 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7687 call FN with the parameter and the DATA.
7688 If FN returns nonzero, the iteration is terminated, and
7689 for_each_template_parm returns 1. Otherwise, the iteration
7690 continues. If FN never returns a nonzero value, the value
7691 returned by for_each_template_parm is 0. If FN is NULL, it is
7692 considered to be the function which always returns 1.
7694 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7695 parameters that occur in non-deduced contexts. When false, only
7696 visits those template parameters that can be deduced. */
7698 static int
7699 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7700 struct pointer_set_t *visited,
7701 bool include_nondeduced_p)
7703 struct pair_fn_data pfd;
7704 int result;
7706 /* Set up. */
7707 pfd.fn = fn;
7708 pfd.data = data;
7709 pfd.include_nondeduced_p = include_nondeduced_p;
7711 /* Walk the tree. (Conceptually, we would like to walk without
7712 duplicates, but for_each_template_parm_r recursively calls
7713 for_each_template_parm, so we would need to reorganize a fair
7714 bit to use walk_tree_without_duplicates, so we keep our own
7715 visited list.) */
7716 if (visited)
7717 pfd.visited = visited;
7718 else
7719 pfd.visited = pointer_set_create ();
7720 result = cp_walk_tree (&t,
7721 for_each_template_parm_r,
7722 &pfd,
7723 pfd.visited) != NULL_TREE;
7725 /* Clean up. */
7726 if (!visited)
7728 pointer_set_destroy (pfd.visited);
7729 pfd.visited = 0;
7732 return result;
7735 /* Returns true if T depends on any template parameter. */
7738 uses_template_parms (tree t)
7740 bool dependent_p;
7741 int saved_processing_template_decl;
7743 saved_processing_template_decl = processing_template_decl;
7744 if (!saved_processing_template_decl)
7745 processing_template_decl = 1;
7746 if (TYPE_P (t))
7747 dependent_p = dependent_type_p (t);
7748 else if (TREE_CODE (t) == TREE_VEC)
7749 dependent_p = any_dependent_template_arguments_p (t);
7750 else if (TREE_CODE (t) == TREE_LIST)
7751 dependent_p = (uses_template_parms (TREE_VALUE (t))
7752 || uses_template_parms (TREE_CHAIN (t)));
7753 else if (TREE_CODE (t) == TYPE_DECL)
7754 dependent_p = dependent_type_p (TREE_TYPE (t));
7755 else if (DECL_P (t)
7756 || EXPR_P (t)
7757 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7758 || TREE_CODE (t) == OVERLOAD
7759 || BASELINK_P (t)
7760 || TREE_CODE (t) == IDENTIFIER_NODE
7761 || TREE_CODE (t) == TRAIT_EXPR
7762 || TREE_CODE (t) == CONSTRUCTOR
7763 || CONSTANT_CLASS_P (t))
7764 dependent_p = (type_dependent_expression_p (t)
7765 || value_dependent_expression_p (t));
7766 else
7768 gcc_assert (t == error_mark_node);
7769 dependent_p = false;
7772 processing_template_decl = saved_processing_template_decl;
7774 return dependent_p;
7777 /* Returns true iff current_function_decl is an incompletely instantiated
7778 template. Useful instead of processing_template_decl because the latter
7779 is set to 0 during fold_non_dependent_expr. */
7781 bool
7782 in_template_function (void)
7784 tree fn = current_function_decl;
7785 bool ret;
7786 ++processing_template_decl;
7787 ret = (fn && DECL_LANG_SPECIFIC (fn)
7788 && DECL_TEMPLATE_INFO (fn)
7789 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7790 --processing_template_decl;
7791 return ret;
7794 /* Returns true if T depends on any template parameter with level LEVEL. */
7797 uses_template_parms_level (tree t, int level)
7799 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7800 /*include_nondeduced_p=*/true);
7803 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7804 ill-formed translation unit, i.e. a variable or function that isn't
7805 usable in a constant expression. */
7807 static inline bool
7808 neglectable_inst_p (tree d)
7810 return (DECL_P (d)
7811 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7812 : decl_maybe_constant_var_p (d)));
7815 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7816 neglectable and instantiated from within an erroneous instantiation. */
7818 static bool
7819 limit_bad_template_recursion (tree decl)
7821 struct tinst_level *lev = current_tinst_level;
7822 int errs = errorcount + sorrycount;
7823 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7824 return false;
7826 for (; lev; lev = lev->next)
7827 if (neglectable_inst_p (lev->decl))
7828 break;
7830 return (lev && errs > lev->errors);
7833 static int tinst_depth;
7834 extern int max_tinst_depth;
7835 int depth_reached;
7837 static GTY(()) struct tinst_level *last_error_tinst_level;
7839 /* We're starting to instantiate D; record the template instantiation context
7840 for diagnostics and to restore it later. */
7843 push_tinst_level (tree d)
7845 struct tinst_level *new_level;
7847 if (tinst_depth >= max_tinst_depth)
7849 last_error_tinst_level = current_tinst_level;
7850 if (TREE_CODE (d) == TREE_LIST)
7851 error ("template instantiation depth exceeds maximum of %d (use "
7852 "-ftemplate-depth= to increase the maximum) substituting %qS",
7853 max_tinst_depth, d);
7854 else
7855 error ("template instantiation depth exceeds maximum of %d (use "
7856 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7857 max_tinst_depth, d);
7859 print_instantiation_context ();
7861 return 0;
7864 /* If the current instantiation caused problems, don't let it instantiate
7865 anything else. Do allow deduction substitution and decls usable in
7866 constant expressions. */
7867 if (limit_bad_template_recursion (d))
7868 return 0;
7870 new_level = ggc_alloc_tinst_level ();
7871 new_level->decl = d;
7872 new_level->locus = input_location;
7873 new_level->errors = errorcount+sorrycount;
7874 new_level->in_system_header_p = in_system_header;
7875 new_level->next = current_tinst_level;
7876 current_tinst_level = new_level;
7878 ++tinst_depth;
7879 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
7880 depth_reached = tinst_depth;
7882 return 1;
7885 /* We're done instantiating this template; return to the instantiation
7886 context. */
7888 void
7889 pop_tinst_level (void)
7891 /* Restore the filename and line number stashed away when we started
7892 this instantiation. */
7893 input_location = current_tinst_level->locus;
7894 current_tinst_level = current_tinst_level->next;
7895 --tinst_depth;
7898 /* We're instantiating a deferred template; restore the template
7899 instantiation context in which the instantiation was requested, which
7900 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7902 static tree
7903 reopen_tinst_level (struct tinst_level *level)
7905 struct tinst_level *t;
7907 tinst_depth = 0;
7908 for (t = level; t; t = t->next)
7909 ++tinst_depth;
7911 current_tinst_level = level;
7912 pop_tinst_level ();
7913 if (current_tinst_level)
7914 current_tinst_level->errors = errorcount+sorrycount;
7915 return level->decl;
7918 /* Returns the TINST_LEVEL which gives the original instantiation
7919 context. */
7921 struct tinst_level *
7922 outermost_tinst_level (void)
7924 struct tinst_level *level = current_tinst_level;
7925 if (level)
7926 while (level->next)
7927 level = level->next;
7928 return level;
7931 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7932 vector of template arguments, as for tsubst.
7934 Returns an appropriate tsubst'd friend declaration. */
7936 static tree
7937 tsubst_friend_function (tree decl, tree args)
7939 tree new_friend;
7941 if (TREE_CODE (decl) == FUNCTION_DECL
7942 && DECL_TEMPLATE_INSTANTIATION (decl)
7943 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7944 /* This was a friend declared with an explicit template
7945 argument list, e.g.:
7947 friend void f<>(T);
7949 to indicate that f was a template instantiation, not a new
7950 function declaration. Now, we have to figure out what
7951 instantiation of what template. */
7953 tree template_id, arglist, fns;
7954 tree new_args;
7955 tree tmpl;
7956 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7958 /* Friend functions are looked up in the containing namespace scope.
7959 We must enter that scope, to avoid finding member functions of the
7960 current class with same name. */
7961 push_nested_namespace (ns);
7962 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7963 tf_warning_or_error, NULL_TREE,
7964 /*integral_constant_expression_p=*/false);
7965 pop_nested_namespace (ns);
7966 arglist = tsubst (DECL_TI_ARGS (decl), args,
7967 tf_warning_or_error, NULL_TREE);
7968 template_id = lookup_template_function (fns, arglist);
7970 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7971 tmpl = determine_specialization (template_id, new_friend,
7972 &new_args,
7973 /*need_member_template=*/0,
7974 TREE_VEC_LENGTH (args),
7975 tsk_none);
7976 return instantiate_template (tmpl, new_args, tf_error);
7979 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7981 /* The NEW_FRIEND will look like an instantiation, to the
7982 compiler, but is not an instantiation from the point of view of
7983 the language. For example, we might have had:
7985 template <class T> struct S {
7986 template <class U> friend void f(T, U);
7989 Then, in S<int>, template <class U> void f(int, U) is not an
7990 instantiation of anything. */
7991 if (new_friend == error_mark_node)
7992 return error_mark_node;
7994 DECL_USE_TEMPLATE (new_friend) = 0;
7995 if (TREE_CODE (decl) == TEMPLATE_DECL)
7997 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7998 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7999 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8002 /* The mangled name for the NEW_FRIEND is incorrect. The function
8003 is not a template instantiation and should not be mangled like
8004 one. Therefore, we forget the mangling here; we'll recompute it
8005 later if we need it. */
8006 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8008 SET_DECL_RTL (new_friend, NULL);
8009 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8012 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8014 tree old_decl;
8015 tree new_friend_template_info;
8016 tree new_friend_result_template_info;
8017 tree ns;
8018 int new_friend_is_defn;
8020 /* We must save some information from NEW_FRIEND before calling
8021 duplicate decls since that function will free NEW_FRIEND if
8022 possible. */
8023 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8024 new_friend_is_defn =
8025 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8026 (template_for_substitution (new_friend)))
8027 != NULL_TREE);
8028 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8030 /* This declaration is a `primary' template. */
8031 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8033 new_friend_result_template_info
8034 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8036 else
8037 new_friend_result_template_info = NULL_TREE;
8039 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8040 if (new_friend_is_defn)
8041 DECL_INITIAL (new_friend) = error_mark_node;
8043 /* Inside pushdecl_namespace_level, we will push into the
8044 current namespace. However, the friend function should go
8045 into the namespace of the template. */
8046 ns = decl_namespace_context (new_friend);
8047 push_nested_namespace (ns);
8048 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8049 pop_nested_namespace (ns);
8051 if (old_decl == error_mark_node)
8052 return error_mark_node;
8054 if (old_decl != new_friend)
8056 /* This new friend declaration matched an existing
8057 declaration. For example, given:
8059 template <class T> void f(T);
8060 template <class U> class C {
8061 template <class T> friend void f(T) {}
8064 the friend declaration actually provides the definition
8065 of `f', once C has been instantiated for some type. So,
8066 old_decl will be the out-of-class template declaration,
8067 while new_friend is the in-class definition.
8069 But, if `f' was called before this point, the
8070 instantiation of `f' will have DECL_TI_ARGS corresponding
8071 to `T' but not to `U', references to which might appear
8072 in the definition of `f'. Previously, the most general
8073 template for an instantiation of `f' was the out-of-class
8074 version; now it is the in-class version. Therefore, we
8075 run through all specialization of `f', adding to their
8076 DECL_TI_ARGS appropriately. In particular, they need a
8077 new set of outer arguments, corresponding to the
8078 arguments for this class instantiation.
8080 The same situation can arise with something like this:
8082 friend void f(int);
8083 template <class T> class C {
8084 friend void f(T) {}
8087 when `C<int>' is instantiated. Now, `f(int)' is defined
8088 in the class. */
8090 if (!new_friend_is_defn)
8091 /* On the other hand, if the in-class declaration does
8092 *not* provide a definition, then we don't want to alter
8093 existing definitions. We can just leave everything
8094 alone. */
8096 else
8098 tree new_template = TI_TEMPLATE (new_friend_template_info);
8099 tree new_args = TI_ARGS (new_friend_template_info);
8101 /* Overwrite whatever template info was there before, if
8102 any, with the new template information pertaining to
8103 the declaration. */
8104 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8106 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8108 /* We should have called reregister_specialization in
8109 duplicate_decls. */
8110 gcc_assert (retrieve_specialization (new_template,
8111 new_args, 0)
8112 == old_decl);
8114 /* Instantiate it if the global has already been used. */
8115 if (DECL_ODR_USED (old_decl))
8116 instantiate_decl (old_decl, /*defer_ok=*/true,
8117 /*expl_inst_class_mem_p=*/false);
8119 else
8121 tree t;
8123 /* Indicate that the old function template is a partial
8124 instantiation. */
8125 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8126 = new_friend_result_template_info;
8128 gcc_assert (new_template
8129 == most_general_template (new_template));
8130 gcc_assert (new_template != old_decl);
8132 /* Reassign any specializations already in the hash table
8133 to the new more general template, and add the
8134 additional template args. */
8135 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8136 t != NULL_TREE;
8137 t = TREE_CHAIN (t))
8139 tree spec = TREE_VALUE (t);
8140 spec_entry elt;
8142 elt.tmpl = old_decl;
8143 elt.args = DECL_TI_ARGS (spec);
8144 elt.spec = NULL_TREE;
8146 htab_remove_elt (decl_specializations, &elt);
8148 DECL_TI_ARGS (spec)
8149 = add_outermost_template_args (new_args,
8150 DECL_TI_ARGS (spec));
8152 register_specialization
8153 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8156 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8160 /* The information from NEW_FRIEND has been merged into OLD_DECL
8161 by duplicate_decls. */
8162 new_friend = old_decl;
8165 else
8167 tree context = DECL_CONTEXT (new_friend);
8168 bool dependent_p;
8170 /* In the code
8171 template <class T> class C {
8172 template <class U> friend void C1<U>::f (); // case 1
8173 friend void C2<T>::f (); // case 2
8175 we only need to make sure CONTEXT is a complete type for
8176 case 2. To distinguish between the two cases, we note that
8177 CONTEXT of case 1 remains dependent type after tsubst while
8178 this isn't true for case 2. */
8179 ++processing_template_decl;
8180 dependent_p = dependent_type_p (context);
8181 --processing_template_decl;
8183 if (!dependent_p
8184 && !complete_type_or_else (context, NULL_TREE))
8185 return error_mark_node;
8187 if (COMPLETE_TYPE_P (context))
8189 /* Check to see that the declaration is really present, and,
8190 possibly obtain an improved declaration. */
8191 tree fn = check_classfn (context,
8192 new_friend, NULL_TREE);
8194 if (fn)
8195 new_friend = fn;
8199 return new_friend;
8202 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8203 template arguments, as for tsubst.
8205 Returns an appropriate tsubst'd friend type or error_mark_node on
8206 failure. */
8208 static tree
8209 tsubst_friend_class (tree friend_tmpl, tree args)
8211 tree friend_type;
8212 tree tmpl;
8213 tree context;
8215 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8217 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8218 return TREE_TYPE (t);
8221 context = CP_DECL_CONTEXT (friend_tmpl);
8223 if (context != global_namespace)
8225 if (TREE_CODE (context) == NAMESPACE_DECL)
8226 push_nested_namespace (context);
8227 else
8228 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8231 /* Look for a class template declaration. We look for hidden names
8232 because two friend declarations of the same template are the
8233 same. For example, in:
8235 struct A {
8236 template <typename> friend class F;
8238 template <typename> struct B {
8239 template <typename> friend class F;
8242 both F templates are the same. */
8243 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8244 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8246 /* But, if we don't find one, it might be because we're in a
8247 situation like this:
8249 template <class T>
8250 struct S {
8251 template <class U>
8252 friend struct S;
8255 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8256 for `S<int>', not the TEMPLATE_DECL. */
8257 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8259 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8260 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8263 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8265 /* The friend template has already been declared. Just
8266 check to see that the declarations match, and install any new
8267 default parameters. We must tsubst the default parameters,
8268 of course. We only need the innermost template parameters
8269 because that is all that redeclare_class_template will look
8270 at. */
8271 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8272 > TMPL_ARGS_DEPTH (args))
8274 tree parms;
8275 location_t saved_input_location;
8276 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8277 args, tf_warning_or_error);
8279 saved_input_location = input_location;
8280 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8281 redeclare_class_template (TREE_TYPE (tmpl), parms);
8282 input_location = saved_input_location;
8286 friend_type = TREE_TYPE (tmpl);
8288 else
8290 /* The friend template has not already been declared. In this
8291 case, the instantiation of the template class will cause the
8292 injection of this template into the global scope. */
8293 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8294 if (tmpl == error_mark_node)
8295 return error_mark_node;
8297 /* The new TMPL is not an instantiation of anything, so we
8298 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8299 the new type because that is supposed to be the corresponding
8300 template decl, i.e., TMPL. */
8301 DECL_USE_TEMPLATE (tmpl) = 0;
8302 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8303 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8304 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8305 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8307 /* Inject this template into the global scope. */
8308 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8311 if (context != global_namespace)
8313 if (TREE_CODE (context) == NAMESPACE_DECL)
8314 pop_nested_namespace (context);
8315 else
8316 pop_nested_class ();
8319 return friend_type;
8322 /* Returns zero if TYPE cannot be completed later due to circularity.
8323 Otherwise returns one. */
8325 static int
8326 can_complete_type_without_circularity (tree type)
8328 if (type == NULL_TREE || type == error_mark_node)
8329 return 0;
8330 else if (COMPLETE_TYPE_P (type))
8331 return 1;
8332 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8333 return can_complete_type_without_circularity (TREE_TYPE (type));
8334 else if (CLASS_TYPE_P (type)
8335 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8336 return 0;
8337 else
8338 return 1;
8341 /* Apply any attributes which had to be deferred until instantiation
8342 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8343 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8345 static void
8346 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8347 tree args, tsubst_flags_t complain, tree in_decl)
8349 tree last_dep = NULL_TREE;
8350 tree t;
8351 tree *p;
8353 for (t = attributes; t; t = TREE_CHAIN (t))
8354 if (ATTR_IS_DEPENDENT (t))
8356 last_dep = t;
8357 attributes = copy_list (attributes);
8358 break;
8361 if (DECL_P (*decl_p))
8363 if (TREE_TYPE (*decl_p) == error_mark_node)
8364 return;
8365 p = &DECL_ATTRIBUTES (*decl_p);
8367 else
8368 p = &TYPE_ATTRIBUTES (*decl_p);
8370 if (last_dep)
8372 tree late_attrs = NULL_TREE;
8373 tree *q = &late_attrs;
8375 for (*p = attributes; *p; )
8377 t = *p;
8378 if (ATTR_IS_DEPENDENT (t))
8380 *p = TREE_CHAIN (t);
8381 TREE_CHAIN (t) = NULL_TREE;
8382 /* If the first attribute argument is an identifier, don't
8383 pass it through tsubst. Attributes like mode, format,
8384 cleanup and several target specific attributes expect it
8385 unmodified. */
8386 if (TREE_VALUE (t)
8387 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8388 && TREE_VALUE (TREE_VALUE (t))
8389 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8390 == IDENTIFIER_NODE))
8392 tree chain
8393 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8394 in_decl,
8395 /*integral_constant_expression_p=*/false);
8396 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8397 TREE_VALUE (t)
8398 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8399 chain);
8401 else
8402 TREE_VALUE (t)
8403 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8404 /*integral_constant_expression_p=*/false);
8405 *q = t;
8406 q = &TREE_CHAIN (t);
8408 else
8409 p = &TREE_CHAIN (t);
8412 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8416 /* Perform (or defer) access check for typedefs that were referenced
8417 from within the template TMPL code.
8418 This is a subroutine of instantiate_decl and instantiate_class_template.
8419 TMPL is the template to consider and TARGS is the list of arguments of
8420 that template. */
8422 static void
8423 perform_typedefs_access_check (tree tmpl, tree targs)
8425 location_t saved_location;
8426 int i;
8427 qualified_typedef_usage_t *iter;
8429 if (!tmpl
8430 || (!CLASS_TYPE_P (tmpl)
8431 && TREE_CODE (tmpl) != FUNCTION_DECL))
8432 return;
8434 saved_location = input_location;
8435 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
8436 get_types_needing_access_check (tmpl),
8437 i, iter)
8439 tree type_decl = iter->typedef_decl;
8440 tree type_scope = iter->context;
8442 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8443 continue;
8445 if (uses_template_parms (type_decl))
8446 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8447 if (uses_template_parms (type_scope))
8448 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8450 /* Make access check error messages point to the location
8451 of the use of the typedef. */
8452 input_location = iter->locus;
8453 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8454 type_decl, type_decl,
8455 tf_warning_or_error);
8457 input_location = saved_location;
8460 static tree
8461 instantiate_class_template_1 (tree type)
8463 tree templ, args, pattern, t, member;
8464 tree typedecl;
8465 tree pbinfo;
8466 tree base_list;
8467 unsigned int saved_maximum_field_alignment;
8468 tree fn_context;
8470 if (type == error_mark_node)
8471 return error_mark_node;
8473 if (COMPLETE_OR_OPEN_TYPE_P (type)
8474 || uses_template_parms (type))
8475 return type;
8477 /* Figure out which template is being instantiated. */
8478 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8479 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8481 /* Determine what specialization of the original template to
8482 instantiate. */
8483 t = most_specialized_class (type, templ, tf_warning_or_error);
8484 if (t == error_mark_node)
8486 TYPE_BEING_DEFINED (type) = 1;
8487 return error_mark_node;
8489 else if (t)
8491 /* This TYPE is actually an instantiation of a partial
8492 specialization. We replace the innermost set of ARGS with
8493 the arguments appropriate for substitution. For example,
8494 given:
8496 template <class T> struct S {};
8497 template <class T> struct S<T*> {};
8499 and supposing that we are instantiating S<int*>, ARGS will
8500 presently be {int*} -- but we need {int}. */
8501 pattern = TREE_TYPE (t);
8502 args = TREE_PURPOSE (t);
8504 else
8506 pattern = TREE_TYPE (templ);
8507 args = CLASSTYPE_TI_ARGS (type);
8510 /* If the template we're instantiating is incomplete, then clearly
8511 there's nothing we can do. */
8512 if (!COMPLETE_TYPE_P (pattern))
8513 return type;
8515 /* If we've recursively instantiated too many templates, stop. */
8516 if (! push_tinst_level (type))
8517 return type;
8519 /* Now we're really doing the instantiation. Mark the type as in
8520 the process of being defined. */
8521 TYPE_BEING_DEFINED (type) = 1;
8523 /* We may be in the middle of deferred access check. Disable
8524 it now. */
8525 push_deferring_access_checks (dk_no_deferred);
8527 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8528 if (!fn_context)
8529 push_to_top_level ();
8530 /* Use #pragma pack from the template context. */
8531 saved_maximum_field_alignment = maximum_field_alignment;
8532 maximum_field_alignment = TYPE_PRECISION (pattern);
8534 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8536 /* Set the input location to the most specialized template definition.
8537 This is needed if tsubsting causes an error. */
8538 typedecl = TYPE_MAIN_DECL (pattern);
8539 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8540 DECL_SOURCE_LOCATION (typedecl);
8542 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8543 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8544 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8545 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8546 if (ANON_AGGR_TYPE_P (pattern))
8547 SET_ANON_AGGR_TYPE_P (type);
8548 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8550 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8551 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8552 /* Adjust visibility for template arguments. */
8553 determine_visibility (TYPE_MAIN_DECL (type));
8555 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8557 pbinfo = TYPE_BINFO (pattern);
8559 /* We should never instantiate a nested class before its enclosing
8560 class; we need to look up the nested class by name before we can
8561 instantiate it, and that lookup should instantiate the enclosing
8562 class. */
8563 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8564 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8566 base_list = NULL_TREE;
8567 if (BINFO_N_BASE_BINFOS (pbinfo))
8569 tree pbase_binfo;
8570 tree pushed_scope;
8571 int i;
8573 /* We must enter the scope containing the type, as that is where
8574 the accessibility of types named in dependent bases are
8575 looked up from. */
8576 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8578 /* Substitute into each of the bases to determine the actual
8579 basetypes. */
8580 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8582 tree base;
8583 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8584 tree expanded_bases = NULL_TREE;
8585 int idx, len = 1;
8587 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8589 expanded_bases =
8590 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8591 args, tf_error, NULL_TREE);
8592 if (expanded_bases == error_mark_node)
8593 continue;
8595 len = TREE_VEC_LENGTH (expanded_bases);
8598 for (idx = 0; idx < len; idx++)
8600 if (expanded_bases)
8601 /* Extract the already-expanded base class. */
8602 base = TREE_VEC_ELT (expanded_bases, idx);
8603 else
8604 /* Substitute to figure out the base class. */
8605 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8606 NULL_TREE);
8608 if (base == error_mark_node)
8609 continue;
8611 base_list = tree_cons (access, base, base_list);
8612 if (BINFO_VIRTUAL_P (pbase_binfo))
8613 TREE_TYPE (base_list) = integer_type_node;
8617 /* The list is now in reverse order; correct that. */
8618 base_list = nreverse (base_list);
8620 if (pushed_scope)
8621 pop_scope (pushed_scope);
8623 /* Now call xref_basetypes to set up all the base-class
8624 information. */
8625 xref_basetypes (type, base_list);
8627 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8628 (int) ATTR_FLAG_TYPE_IN_PLACE,
8629 args, tf_error, NULL_TREE);
8630 fixup_attribute_variants (type);
8632 /* Now that our base classes are set up, enter the scope of the
8633 class, so that name lookups into base classes, etc. will work
8634 correctly. This is precisely analogous to what we do in
8635 begin_class_definition when defining an ordinary non-template
8636 class, except we also need to push the enclosing classes. */
8637 push_nested_class (type);
8639 /* Now members are processed in the order of declaration. */
8640 for (member = CLASSTYPE_DECL_LIST (pattern);
8641 member; member = TREE_CHAIN (member))
8643 tree t = TREE_VALUE (member);
8645 if (TREE_PURPOSE (member))
8647 if (TYPE_P (t))
8649 /* Build new CLASSTYPE_NESTED_UTDS. */
8651 tree newtag;
8652 bool class_template_p;
8654 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8655 && TYPE_LANG_SPECIFIC (t)
8656 && CLASSTYPE_IS_TEMPLATE (t));
8657 /* If the member is a class template, then -- even after
8658 substitution -- there may be dependent types in the
8659 template argument list for the class. We increment
8660 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8661 that function will assume that no types are dependent
8662 when outside of a template. */
8663 if (class_template_p)
8664 ++processing_template_decl;
8665 newtag = tsubst (t, args, tf_error, NULL_TREE);
8666 if (class_template_p)
8667 --processing_template_decl;
8668 if (newtag == error_mark_node)
8669 continue;
8671 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8673 tree name = TYPE_IDENTIFIER (t);
8675 if (class_template_p)
8676 /* Unfortunately, lookup_template_class sets
8677 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8678 instantiation (i.e., for the type of a member
8679 template class nested within a template class.)
8680 This behavior is required for
8681 maybe_process_partial_specialization to work
8682 correctly, but is not accurate in this case;
8683 the TAG is not an instantiation of anything.
8684 (The corresponding TEMPLATE_DECL is an
8685 instantiation, but the TYPE is not.) */
8686 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8688 /* Now, we call pushtag to put this NEWTAG into the scope of
8689 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8690 pushtag calling push_template_decl. We don't have to do
8691 this for enums because it will already have been done in
8692 tsubst_enum. */
8693 if (name)
8694 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8695 pushtag (name, newtag, /*tag_scope=*/ts_current);
8698 else if (TREE_CODE (t) == FUNCTION_DECL
8699 || DECL_FUNCTION_TEMPLATE_P (t))
8701 /* Build new TYPE_METHODS. */
8702 tree r;
8704 if (TREE_CODE (t) == TEMPLATE_DECL)
8705 ++processing_template_decl;
8706 r = tsubst (t, args, tf_error, NULL_TREE);
8707 if (TREE_CODE (t) == TEMPLATE_DECL)
8708 --processing_template_decl;
8709 set_current_access_from_decl (r);
8710 finish_member_declaration (r);
8711 /* Instantiate members marked with attribute used. */
8712 if (r != error_mark_node && DECL_PRESERVE_P (r))
8713 mark_used (r);
8715 else
8717 /* Build new TYPE_FIELDS. */
8718 if (TREE_CODE (t) == STATIC_ASSERT)
8720 tree condition;
8722 ++c_inhibit_evaluation_warnings;
8723 condition =
8724 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8725 tf_warning_or_error, NULL_TREE,
8726 /*integral_constant_expression_p=*/true);
8727 --c_inhibit_evaluation_warnings;
8729 finish_static_assert (condition,
8730 STATIC_ASSERT_MESSAGE (t),
8731 STATIC_ASSERT_SOURCE_LOCATION (t),
8732 /*member_p=*/true);
8734 else if (TREE_CODE (t) != CONST_DECL)
8736 tree r;
8738 /* The file and line for this declaration, to
8739 assist in error message reporting. Since we
8740 called push_tinst_level above, we don't need to
8741 restore these. */
8742 input_location = DECL_SOURCE_LOCATION (t);
8744 if (TREE_CODE (t) == TEMPLATE_DECL)
8745 ++processing_template_decl;
8746 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8747 if (TREE_CODE (t) == TEMPLATE_DECL)
8748 --processing_template_decl;
8749 if (TREE_CODE (r) == VAR_DECL)
8751 /* In [temp.inst]:
8753 [t]he initialization (and any associated
8754 side-effects) of a static data member does
8755 not occur unless the static data member is
8756 itself used in a way that requires the
8757 definition of the static data member to
8758 exist.
8760 Therefore, we do not substitute into the
8761 initialized for the static data member here. */
8762 finish_static_data_member_decl
8764 /*init=*/NULL_TREE,
8765 /*init_const_expr_p=*/false,
8766 /*asmspec_tree=*/NULL_TREE,
8767 /*flags=*/0);
8768 /* Instantiate members marked with attribute used. */
8769 if (r != error_mark_node && DECL_PRESERVE_P (r))
8770 mark_used (r);
8772 else if (TREE_CODE (r) == FIELD_DECL)
8774 /* Determine whether R has a valid type and can be
8775 completed later. If R is invalid, then it is
8776 replaced by error_mark_node so that it will not be
8777 added to TYPE_FIELDS. */
8778 tree rtype = TREE_TYPE (r);
8779 if (can_complete_type_without_circularity (rtype))
8780 complete_type (rtype);
8782 if (!COMPLETE_TYPE_P (rtype))
8784 cxx_incomplete_type_error (r, rtype);
8785 r = error_mark_node;
8789 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8790 such a thing will already have been added to the field
8791 list by tsubst_enum in finish_member_declaration in the
8792 CLASSTYPE_NESTED_UTDS case above. */
8793 if (!(TREE_CODE (r) == TYPE_DECL
8794 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8795 && DECL_ARTIFICIAL (r)))
8797 set_current_access_from_decl (r);
8798 finish_member_declaration (r);
8803 else
8805 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
8806 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8808 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8810 tree friend_type = t;
8811 bool adjust_processing_template_decl = false;
8813 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8815 /* template <class T> friend class C; */
8816 friend_type = tsubst_friend_class (friend_type, args);
8817 adjust_processing_template_decl = true;
8819 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8821 /* template <class T> friend class C::D; */
8822 friend_type = tsubst (friend_type, args,
8823 tf_warning_or_error, NULL_TREE);
8824 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8825 friend_type = TREE_TYPE (friend_type);
8826 adjust_processing_template_decl = true;
8828 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8829 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8831 /* This could be either
8833 friend class T::C;
8835 when dependent_type_p is false or
8837 template <class U> friend class T::C;
8839 otherwise. */
8840 friend_type = tsubst (friend_type, args,
8841 tf_warning_or_error, NULL_TREE);
8842 /* Bump processing_template_decl for correct
8843 dependent_type_p calculation. */
8844 ++processing_template_decl;
8845 if (dependent_type_p (friend_type))
8846 adjust_processing_template_decl = true;
8847 --processing_template_decl;
8849 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8850 && hidden_name_p (TYPE_NAME (friend_type)))
8852 /* friend class C;
8854 where C hasn't been declared yet. Let's lookup name
8855 from namespace scope directly, bypassing any name that
8856 come from dependent base class. */
8857 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8859 /* The call to xref_tag_from_type does injection for friend
8860 classes. */
8861 push_nested_namespace (ns);
8862 friend_type =
8863 xref_tag_from_type (friend_type, NULL_TREE,
8864 /*tag_scope=*/ts_current);
8865 pop_nested_namespace (ns);
8867 else if (uses_template_parms (friend_type))
8868 /* friend class C<T>; */
8869 friend_type = tsubst (friend_type, args,
8870 tf_warning_or_error, NULL_TREE);
8871 /* Otherwise it's
8873 friend class C;
8875 where C is already declared or
8877 friend class C<int>;
8879 We don't have to do anything in these cases. */
8881 if (adjust_processing_template_decl)
8882 /* Trick make_friend_class into realizing that the friend
8883 we're adding is a template, not an ordinary class. It's
8884 important that we use make_friend_class since it will
8885 perform some error-checking and output cross-reference
8886 information. */
8887 ++processing_template_decl;
8889 if (friend_type != error_mark_node)
8890 make_friend_class (type, friend_type, /*complain=*/false);
8892 if (adjust_processing_template_decl)
8893 --processing_template_decl;
8895 else
8897 /* Build new DECL_FRIENDLIST. */
8898 tree r;
8900 /* The file and line for this declaration, to
8901 assist in error message reporting. Since we
8902 called push_tinst_level above, we don't need to
8903 restore these. */
8904 input_location = DECL_SOURCE_LOCATION (t);
8906 if (TREE_CODE (t) == TEMPLATE_DECL)
8908 ++processing_template_decl;
8909 push_deferring_access_checks (dk_no_check);
8912 r = tsubst_friend_function (t, args);
8913 add_friend (type, r, /*complain=*/false);
8914 if (TREE_CODE (t) == TEMPLATE_DECL)
8916 pop_deferring_access_checks ();
8917 --processing_template_decl;
8923 if (CLASSTYPE_LAMBDA_EXPR (type))
8925 tree decl = lambda_function (type);
8926 if (decl)
8928 tree lam = CLASSTYPE_LAMBDA_EXPR (type);
8929 LAMBDA_EXPR_THIS_CAPTURE (lam)
8930 = lookup_field_1 (type, get_identifier ("__this"), false);
8932 instantiate_decl (decl, false, false);
8933 maybe_add_lambda_conv_op (type);
8935 LAMBDA_EXPR_THIS_CAPTURE (lam) = NULL_TREE;
8937 else
8938 gcc_assert (errorcount);
8941 /* Set the file and line number information to whatever is given for
8942 the class itself. This puts error messages involving generated
8943 implicit functions at a predictable point, and the same point
8944 that would be used for non-template classes. */
8945 input_location = DECL_SOURCE_LOCATION (typedecl);
8947 unreverse_member_declarations (type);
8948 finish_struct_1 (type);
8949 TYPE_BEING_DEFINED (type) = 0;
8951 /* We don't instantiate default arguments for member functions. 14.7.1:
8953 The implicit instantiation of a class template specialization causes
8954 the implicit instantiation of the declarations, but not of the
8955 definitions or default arguments, of the class member functions,
8956 member classes, static data members and member templates.... */
8958 /* Some typedefs referenced from within the template code need to be access
8959 checked at template instantiation time, i.e now. These types were
8960 added to the template at parsing time. Let's get those and perform
8961 the access checks then. */
8962 perform_typedefs_access_check (pattern, args);
8963 perform_deferred_access_checks (tf_warning_or_error);
8964 pop_nested_class ();
8965 maximum_field_alignment = saved_maximum_field_alignment;
8966 if (!fn_context)
8967 pop_from_top_level ();
8968 pop_deferring_access_checks ();
8969 pop_tinst_level ();
8971 /* The vtable for a template class can be emitted in any translation
8972 unit in which the class is instantiated. When there is no key
8973 method, however, finish_struct_1 will already have added TYPE to
8974 the keyed_classes list. */
8975 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8976 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8978 return type;
8981 /* Wrapper for instantiate_class_template_1. */
8983 tree
8984 instantiate_class_template (tree type)
8986 tree ret;
8987 timevar_push (TV_TEMPLATE_INST);
8988 ret = instantiate_class_template_1 (type);
8989 timevar_pop (TV_TEMPLATE_INST);
8990 return ret;
8993 static tree
8994 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8996 tree r;
8998 if (!t)
8999 r = t;
9000 else if (TYPE_P (t))
9001 r = tsubst (t, args, complain, in_decl);
9002 else
9004 if (!(complain & tf_warning))
9005 ++c_inhibit_evaluation_warnings;
9006 r = tsubst_expr (t, args, complain, in_decl,
9007 /*integral_constant_expression_p=*/true);
9008 if (!(complain & tf_warning))
9009 --c_inhibit_evaluation_warnings;
9010 /* Preserve the raw-reference nature of T. */
9011 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9012 && REFERENCE_REF_P (r))
9013 r = TREE_OPERAND (r, 0);
9015 return r;
9018 /* Given a function parameter pack TMPL_PARM and some function parameters
9019 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9020 and set *SPEC_P to point at the next point in the list. */
9022 static tree
9023 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9025 /* Collect all of the extra "packed" parameters into an
9026 argument pack. */
9027 tree parmvec;
9028 tree parmtypevec;
9029 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9030 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9031 tree spec_parm = *spec_p;
9032 int i, len;
9034 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9035 if (tmpl_parm
9036 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9037 break;
9039 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9040 parmvec = make_tree_vec (len);
9041 parmtypevec = make_tree_vec (len);
9042 spec_parm = *spec_p;
9043 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9045 TREE_VEC_ELT (parmvec, i) = spec_parm;
9046 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9049 /* Build the argument packs. */
9050 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9051 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9052 TREE_TYPE (argpack) = argtypepack;
9053 *spec_p = spec_parm;
9055 return argpack;
9058 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9059 NONTYPE_ARGUMENT_PACK. */
9061 static tree
9062 make_fnparm_pack (tree spec_parm)
9064 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9067 /* Substitute ARGS into T, which is an pack expansion
9068 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9069 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9070 (if only a partial substitution could be performed) or
9071 ERROR_MARK_NODE if there was an error. */
9072 tree
9073 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9074 tree in_decl)
9076 tree pattern;
9077 tree pack, packs = NULL_TREE;
9078 bool unsubstituted_packs = false;
9079 bool real_packs = false;
9080 int missing_level = 0;
9081 int i, len = -1;
9082 tree result;
9083 struct pointer_map_t *saved_local_specializations = NULL;
9084 bool need_local_specializations = false;
9085 int levels;
9087 gcc_assert (PACK_EXPANSION_P (t));
9088 pattern = PACK_EXPANSION_PATTERN (t);
9090 /* Add in any args remembered from an earlier partial instantiation. */
9091 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9093 levels = TMPL_ARGS_DEPTH (args);
9095 /* Determine the argument packs that will instantiate the parameter
9096 packs used in the expansion expression. While we're at it,
9097 compute the number of arguments to be expanded and make sure it
9098 is consistent. */
9099 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9100 pack = TREE_CHAIN (pack))
9102 tree parm_pack = TREE_VALUE (pack);
9103 tree arg_pack = NULL_TREE;
9104 tree orig_arg = NULL_TREE;
9105 int level = 0;
9107 if (TREE_CODE (parm_pack) == BASES)
9109 if (BASES_DIRECT (parm_pack))
9110 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9111 args, complain, in_decl, false));
9112 else
9113 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9114 args, complain, in_decl, false));
9116 if (TREE_CODE (parm_pack) == PARM_DECL)
9118 if (PACK_EXPANSION_LOCAL_P (t))
9119 arg_pack = retrieve_local_specialization (parm_pack);
9120 else
9122 /* We can't rely on local_specializations for a parameter
9123 name used later in a function declaration (such as in a
9124 late-specified return type). Even if it exists, it might
9125 have the wrong value for a recursive call. Just make a
9126 dummy decl, since it's only used for its type. */
9127 arg_pack = tsubst_decl (parm_pack, args, complain);
9128 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9129 /* Partial instantiation of the parm_pack, we can't build
9130 up an argument pack yet. */
9131 arg_pack = NULL_TREE;
9132 else
9133 arg_pack = make_fnparm_pack (arg_pack);
9134 need_local_specializations = true;
9137 else
9139 int idx;
9140 template_parm_level_and_index (parm_pack, &level, &idx);
9142 if (level <= levels)
9143 arg_pack = TMPL_ARG (args, level, idx);
9146 orig_arg = arg_pack;
9147 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9148 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9150 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9151 /* This can only happen if we forget to expand an argument
9152 pack somewhere else. Just return an error, silently. */
9154 result = make_tree_vec (1);
9155 TREE_VEC_ELT (result, 0) = error_mark_node;
9156 return result;
9159 if (arg_from_parm_pack_p (arg_pack, parm_pack))
9160 /* The argument pack that the parameter maps to is just an
9161 expansion of the parameter itself, such as one would find
9162 in the implicit typedef of a class inside the class itself.
9163 Consider this parameter "unsubstituted", so that we will
9164 maintain the outer pack expansion. */
9165 arg_pack = NULL_TREE;
9167 if (arg_pack)
9169 int my_len =
9170 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9172 /* Don't bother trying to do a partial substitution with
9173 incomplete packs; we'll try again after deduction. */
9174 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9175 return t;
9177 if (len < 0)
9178 len = my_len;
9179 else if (len != my_len)
9181 if (!(complain & tf_error))
9182 /* Fail quietly. */;
9183 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9184 error ("mismatched argument pack lengths while expanding "
9185 "%<%T%>",
9186 pattern);
9187 else
9188 error ("mismatched argument pack lengths while expanding "
9189 "%<%E%>",
9190 pattern);
9191 return error_mark_node;
9194 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
9195 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack),
9196 0)))
9197 /* This isn't a real argument pack yet. */;
9198 else
9199 real_packs = true;
9201 /* Keep track of the parameter packs and their corresponding
9202 argument packs. */
9203 packs = tree_cons (parm_pack, arg_pack, packs);
9204 TREE_TYPE (packs) = orig_arg;
9206 else
9208 /* We can't substitute for this parameter pack. We use a flag as
9209 well as the missing_level counter because function parameter
9210 packs don't have a level. */
9211 unsubstituted_packs = true;
9212 if (!missing_level || missing_level > level)
9213 missing_level = level;
9217 /* We cannot expand this expansion expression, because we don't have
9218 all of the argument packs we need. */
9219 if (unsubstituted_packs)
9221 if (real_packs)
9223 /* We got some full packs, but we can't substitute them in until we
9224 have values for all the packs. So remember these until then. */
9225 tree save_args;
9227 t = make_pack_expansion (pattern);
9229 /* The call to add_to_template_args above assumes no overlap
9230 between saved args and new args, so prune away any fake
9231 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9232 if (missing_level && levels >= missing_level)
9234 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
9235 && missing_level > 1);
9236 TREE_VEC_LENGTH (args) = missing_level - 1;
9237 save_args = copy_node (args);
9238 TREE_VEC_LENGTH (args) = levels;
9240 else
9241 save_args = args;
9243 PACK_EXPANSION_EXTRA_ARGS (t) = save_args;
9245 else
9247 /* There were no real arguments, we're just replacing a parameter
9248 pack with another version of itself. Substitute into the
9249 pattern and return a PACK_EXPANSION_*. The caller will need to
9250 deal with that. */
9251 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9252 t = tsubst_expr (pattern, args, complain, in_decl,
9253 /*integral_constant_expression_p=*/false);
9254 else
9255 t = tsubst (pattern, args, complain, in_decl);
9256 t = make_pack_expansion (t);
9258 return t;
9261 /* We could not find any argument packs that work. */
9262 if (len < 0)
9263 return error_mark_node;
9265 if (need_local_specializations)
9267 /* We're in a late-specified return type, so create our own local
9268 specializations map; the current map is either NULL or (in the
9269 case of recursive unification) might have bindings that we don't
9270 want to use or alter. */
9271 saved_local_specializations = local_specializations;
9272 local_specializations = pointer_map_create ();
9275 /* For each argument in each argument pack, substitute into the
9276 pattern. */
9277 result = make_tree_vec (len);
9278 for (i = 0; i < len; ++i)
9280 /* For parameter pack, change the substitution of the parameter
9281 pack to the ith argument in its argument pack, then expand
9282 the pattern. */
9283 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9285 tree parm = TREE_PURPOSE (pack);
9286 tree arg;
9288 /* Select the Ith argument from the pack. */
9289 if (TREE_CODE (parm) == PARM_DECL)
9291 if (i == 0)
9293 arg = make_node (ARGUMENT_PACK_SELECT);
9294 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9295 mark_used (parm);
9296 register_local_specialization (arg, parm);
9298 else
9299 arg = retrieve_local_specialization (parm);
9301 else
9303 int idx, level;
9304 template_parm_level_and_index (parm, &level, &idx);
9306 if (i == 0)
9308 arg = make_node (ARGUMENT_PACK_SELECT);
9309 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9310 /* Update the corresponding argument. */
9311 TMPL_ARG (args, level, idx) = arg;
9313 else
9314 /* Re-use the ARGUMENT_PACK_SELECT. */
9315 arg = TMPL_ARG (args, level, idx);
9317 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
9320 /* Substitute into the PATTERN with the altered arguments. */
9321 if (!TYPE_P (pattern))
9322 TREE_VEC_ELT (result, i) =
9323 tsubst_expr (pattern, args, complain, in_decl,
9324 /*integral_constant_expression_p=*/false);
9325 else
9326 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
9328 if (TREE_VEC_ELT (result, i) == error_mark_node)
9330 result = error_mark_node;
9331 break;
9335 /* Update ARGS to restore the substitution from parameter packs to
9336 their argument packs. */
9337 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9339 tree parm = TREE_PURPOSE (pack);
9341 if (TREE_CODE (parm) == PARM_DECL)
9342 register_local_specialization (TREE_TYPE (pack), parm);
9343 else
9345 int idx, level;
9346 template_parm_level_and_index (parm, &level, &idx);
9348 /* Update the corresponding argument. */
9349 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9350 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9351 TREE_TYPE (pack);
9352 else
9353 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9357 if (need_local_specializations)
9359 pointer_map_destroy (local_specializations);
9360 local_specializations = saved_local_specializations;
9363 return result;
9366 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9367 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9368 parameter packs; all parms generated from a function parameter pack will
9369 have the same DECL_PARM_INDEX. */
9371 tree
9372 get_pattern_parm (tree parm, tree tmpl)
9374 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9375 tree patparm;
9377 if (DECL_ARTIFICIAL (parm))
9379 for (patparm = DECL_ARGUMENTS (pattern);
9380 patparm; patparm = DECL_CHAIN (patparm))
9381 if (DECL_ARTIFICIAL (patparm)
9382 && DECL_NAME (parm) == DECL_NAME (patparm))
9383 break;
9385 else
9387 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9388 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9389 gcc_assert (DECL_PARM_INDEX (patparm)
9390 == DECL_PARM_INDEX (parm));
9393 return patparm;
9396 /* Substitute ARGS into the vector or list of template arguments T. */
9398 static tree
9399 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9401 tree orig_t = t;
9402 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9403 tree *elts;
9405 if (t == error_mark_node)
9406 return error_mark_node;
9408 len = TREE_VEC_LENGTH (t);
9409 elts = XALLOCAVEC (tree, len);
9411 for (i = 0; i < len; i++)
9413 tree orig_arg = TREE_VEC_ELT (t, i);
9414 tree new_arg;
9416 if (TREE_CODE (orig_arg) == TREE_VEC)
9417 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9418 else if (PACK_EXPANSION_P (orig_arg))
9420 /* Substitute into an expansion expression. */
9421 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9423 if (TREE_CODE (new_arg) == TREE_VEC)
9424 /* Add to the expanded length adjustment the number of
9425 expanded arguments. We subtract one from this
9426 measurement, because the argument pack expression
9427 itself is already counted as 1 in
9428 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9429 the argument pack is empty. */
9430 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9432 else if (ARGUMENT_PACK_P (orig_arg))
9434 /* Substitute into each of the arguments. */
9435 new_arg = TYPE_P (orig_arg)
9436 ? cxx_make_type (TREE_CODE (orig_arg))
9437 : make_node (TREE_CODE (orig_arg));
9439 SET_ARGUMENT_PACK_ARGS (
9440 new_arg,
9441 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9442 args, complain, in_decl));
9444 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9445 new_arg = error_mark_node;
9447 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9448 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9449 complain, in_decl);
9450 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9452 if (TREE_TYPE (new_arg) == error_mark_node)
9453 new_arg = error_mark_node;
9456 else
9457 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9459 if (new_arg == error_mark_node)
9460 return error_mark_node;
9462 elts[i] = new_arg;
9463 if (new_arg != orig_arg)
9464 need_new = 1;
9467 if (!need_new)
9468 return t;
9470 /* Make space for the expanded arguments coming from template
9471 argument packs. */
9472 t = make_tree_vec (len + expanded_len_adjust);
9473 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9474 arguments for a member template.
9475 In that case each TREE_VEC in ORIG_T represents a level of template
9476 arguments, and ORIG_T won't carry any non defaulted argument count.
9477 It will rather be the nested TREE_VECs that will carry one.
9478 In other words, ORIG_T carries a non defaulted argument count only
9479 if it doesn't contain any nested TREE_VEC. */
9480 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9482 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9483 count += expanded_len_adjust;
9484 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9486 for (i = 0, out = 0; i < len; i++)
9488 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9489 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9490 && TREE_CODE (elts[i]) == TREE_VEC)
9492 int idx;
9494 /* Now expand the template argument pack "in place". */
9495 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9496 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9498 else
9500 TREE_VEC_ELT (t, out) = elts[i];
9501 out++;
9505 return t;
9508 /* Return the result of substituting ARGS into the template parameters
9509 given by PARMS. If there are m levels of ARGS and m + n levels of
9510 PARMS, then the result will contain n levels of PARMS. For
9511 example, if PARMS is `template <class T> template <class U>
9512 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9513 result will be `template <int*, double, class V>'. */
9515 static tree
9516 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9518 tree r = NULL_TREE;
9519 tree* new_parms;
9521 /* When substituting into a template, we must set
9522 PROCESSING_TEMPLATE_DECL as the template parameters may be
9523 dependent if they are based on one-another, and the dependency
9524 predicates are short-circuit outside of templates. */
9525 ++processing_template_decl;
9527 for (new_parms = &r;
9528 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9529 new_parms = &(TREE_CHAIN (*new_parms)),
9530 parms = TREE_CHAIN (parms))
9532 tree new_vec =
9533 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9534 int i;
9536 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9538 tree tuple;
9540 if (parms == error_mark_node)
9541 continue;
9543 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9545 if (tuple == error_mark_node)
9546 continue;
9548 TREE_VEC_ELT (new_vec, i) =
9549 tsubst_template_parm (tuple, args, complain);
9552 *new_parms =
9553 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9554 - TMPL_ARGS_DEPTH (args)),
9555 new_vec, NULL_TREE);
9558 --processing_template_decl;
9560 return r;
9563 /* Return the result of substituting ARGS into one template parameter
9564 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9565 parameter and which TREE_PURPOSE is the default argument of the
9566 template parameter. */
9568 static tree
9569 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9571 tree default_value, parm_decl;
9573 if (args == NULL_TREE
9574 || t == NULL_TREE
9575 || t == error_mark_node)
9576 return t;
9578 gcc_assert (TREE_CODE (t) == TREE_LIST);
9580 default_value = TREE_PURPOSE (t);
9581 parm_decl = TREE_VALUE (t);
9583 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9584 if (TREE_CODE (parm_decl) == PARM_DECL
9585 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9586 parm_decl = error_mark_node;
9587 default_value = tsubst_template_arg (default_value, args,
9588 complain, NULL_TREE);
9590 return build_tree_list (default_value, parm_decl);
9593 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9594 type T. If T is not an aggregate or enumeration type, it is
9595 handled as if by tsubst. IN_DECL is as for tsubst. If
9596 ENTERING_SCOPE is nonzero, T is the context for a template which
9597 we are presently tsubst'ing. Return the substituted value. */
9599 static tree
9600 tsubst_aggr_type (tree t,
9601 tree args,
9602 tsubst_flags_t complain,
9603 tree in_decl,
9604 int entering_scope)
9606 if (t == NULL_TREE)
9607 return NULL_TREE;
9609 switch (TREE_CODE (t))
9611 case RECORD_TYPE:
9612 if (TYPE_PTRMEMFUNC_P (t))
9613 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9615 /* Else fall through. */
9616 case ENUMERAL_TYPE:
9617 case UNION_TYPE:
9618 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9620 tree argvec;
9621 tree context;
9622 tree r;
9623 int saved_unevaluated_operand;
9624 int saved_inhibit_evaluation_warnings;
9626 /* In "sizeof(X<I>)" we need to evaluate "I". */
9627 saved_unevaluated_operand = cp_unevaluated_operand;
9628 cp_unevaluated_operand = 0;
9629 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9630 c_inhibit_evaluation_warnings = 0;
9632 /* First, determine the context for the type we are looking
9633 up. */
9634 context = TYPE_CONTEXT (t);
9635 if (context && TYPE_P (context))
9637 context = tsubst_aggr_type (context, args, complain,
9638 in_decl, /*entering_scope=*/1);
9639 /* If context is a nested class inside a class template,
9640 it may still need to be instantiated (c++/33959). */
9641 context = complete_type (context);
9644 /* Then, figure out what arguments are appropriate for the
9645 type we are trying to find. For example, given:
9647 template <class T> struct S;
9648 template <class T, class U> void f(T, U) { S<U> su; }
9650 and supposing that we are instantiating f<int, double>,
9651 then our ARGS will be {int, double}, but, when looking up
9652 S we only want {double}. */
9653 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9654 complain, in_decl);
9655 if (argvec == error_mark_node)
9656 r = error_mark_node;
9657 else
9659 r = lookup_template_class (t, argvec, in_decl, context,
9660 entering_scope, complain);
9661 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9664 cp_unevaluated_operand = saved_unevaluated_operand;
9665 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9667 return r;
9669 else
9670 /* This is not a template type, so there's nothing to do. */
9671 return t;
9673 default:
9674 return tsubst (t, args, complain, in_decl);
9678 /* Substitute into the default argument ARG (a default argument for
9679 FN), which has the indicated TYPE. */
9681 tree
9682 tsubst_default_argument (tree fn, tree type, tree arg)
9684 tree saved_class_ptr = NULL_TREE;
9685 tree saved_class_ref = NULL_TREE;
9686 int errs = errorcount + sorrycount;
9688 /* This can happen in invalid code. */
9689 if (TREE_CODE (arg) == DEFAULT_ARG)
9690 return arg;
9692 /* This default argument came from a template. Instantiate the
9693 default argument here, not in tsubst. In the case of
9694 something like:
9696 template <class T>
9697 struct S {
9698 static T t();
9699 void f(T = t());
9702 we must be careful to do name lookup in the scope of S<T>,
9703 rather than in the current class. */
9704 push_access_scope (fn);
9705 /* The "this" pointer is not valid in a default argument. */
9706 if (cfun)
9708 saved_class_ptr = current_class_ptr;
9709 cp_function_chain->x_current_class_ptr = NULL_TREE;
9710 saved_class_ref = current_class_ref;
9711 cp_function_chain->x_current_class_ref = NULL_TREE;
9714 push_deferring_access_checks(dk_no_deferred);
9715 /* The default argument expression may cause implicitly defined
9716 member functions to be synthesized, which will result in garbage
9717 collection. We must treat this situation as if we were within
9718 the body of function so as to avoid collecting live data on the
9719 stack. */
9720 ++function_depth;
9721 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9722 tf_warning_or_error, NULL_TREE,
9723 /*integral_constant_expression_p=*/false);
9724 --function_depth;
9725 pop_deferring_access_checks();
9727 /* Restore the "this" pointer. */
9728 if (cfun)
9730 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9731 cp_function_chain->x_current_class_ref = saved_class_ref;
9734 if (errorcount+sorrycount > errs)
9735 inform (input_location,
9736 " when instantiating default argument for call to %D", fn);
9738 /* Make sure the default argument is reasonable. */
9739 arg = check_default_argument (type, arg);
9741 pop_access_scope (fn);
9743 return arg;
9746 /* Substitute into all the default arguments for FN. */
9748 static void
9749 tsubst_default_arguments (tree fn)
9751 tree arg;
9752 tree tmpl_args;
9754 tmpl_args = DECL_TI_ARGS (fn);
9756 /* If this function is not yet instantiated, we certainly don't need
9757 its default arguments. */
9758 if (uses_template_parms (tmpl_args))
9759 return;
9760 /* Don't do this again for clones. */
9761 if (DECL_CLONED_FUNCTION_P (fn))
9762 return;
9764 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9765 arg;
9766 arg = TREE_CHAIN (arg))
9767 if (TREE_PURPOSE (arg))
9768 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9769 TREE_VALUE (arg),
9770 TREE_PURPOSE (arg));
9773 /* Substitute the ARGS into the T, which is a _DECL. Return the
9774 result of the substitution. Issue error and warning messages under
9775 control of COMPLAIN. */
9777 static tree
9778 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9780 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9781 location_t saved_loc;
9782 tree r = NULL_TREE;
9783 tree in_decl = t;
9784 hashval_t hash = 0;
9786 /* Set the filename and linenumber to improve error-reporting. */
9787 saved_loc = input_location;
9788 input_location = DECL_SOURCE_LOCATION (t);
9790 switch (TREE_CODE (t))
9792 case TEMPLATE_DECL:
9794 /* We can get here when processing a member function template,
9795 member class template, or template template parameter. */
9796 tree decl = DECL_TEMPLATE_RESULT (t);
9797 tree spec;
9798 tree tmpl_args;
9799 tree full_args;
9801 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9803 /* Template template parameter is treated here. */
9804 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9805 if (new_type == error_mark_node)
9806 RETURN (error_mark_node);
9808 r = copy_decl (t);
9809 DECL_CHAIN (r) = NULL_TREE;
9810 TREE_TYPE (r) = new_type;
9811 DECL_TEMPLATE_RESULT (r)
9812 = build_decl (DECL_SOURCE_LOCATION (decl),
9813 TYPE_DECL, DECL_NAME (decl), new_type);
9814 DECL_TEMPLATE_PARMS (r)
9815 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9816 complain);
9817 TYPE_NAME (new_type) = r;
9818 break;
9821 /* We might already have an instance of this template.
9822 The ARGS are for the surrounding class type, so the
9823 full args contain the tsubst'd args for the context,
9824 plus the innermost args from the template decl. */
9825 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9826 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9827 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9828 /* Because this is a template, the arguments will still be
9829 dependent, even after substitution. If
9830 PROCESSING_TEMPLATE_DECL is not set, the dependency
9831 predicates will short-circuit. */
9832 ++processing_template_decl;
9833 full_args = tsubst_template_args (tmpl_args, args,
9834 complain, in_decl);
9835 --processing_template_decl;
9836 if (full_args == error_mark_node)
9837 RETURN (error_mark_node);
9839 /* If this is a default template template argument,
9840 tsubst might not have changed anything. */
9841 if (full_args == tmpl_args)
9842 RETURN (t);
9844 hash = hash_tmpl_and_args (t, full_args);
9845 spec = retrieve_specialization (t, full_args, hash);
9846 if (spec != NULL_TREE)
9848 r = spec;
9849 break;
9852 /* Make a new template decl. It will be similar to the
9853 original, but will record the current template arguments.
9854 We also create a new function declaration, which is just
9855 like the old one, but points to this new template, rather
9856 than the old one. */
9857 r = copy_decl (t);
9858 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9859 DECL_CHAIN (r) = NULL_TREE;
9861 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9863 if (TREE_CODE (decl) == TYPE_DECL
9864 && !TYPE_DECL_ALIAS_P (decl))
9866 tree new_type;
9867 ++processing_template_decl;
9868 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9869 --processing_template_decl;
9870 if (new_type == error_mark_node)
9871 RETURN (error_mark_node);
9873 TREE_TYPE (r) = new_type;
9874 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9875 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9876 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9877 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9879 else
9881 tree new_decl;
9882 ++processing_template_decl;
9883 new_decl = tsubst (decl, args, complain, in_decl);
9884 --processing_template_decl;
9885 if (new_decl == error_mark_node)
9886 RETURN (error_mark_node);
9888 DECL_TEMPLATE_RESULT (r) = new_decl;
9889 DECL_TI_TEMPLATE (new_decl) = r;
9890 TREE_TYPE (r) = TREE_TYPE (new_decl);
9891 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9892 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9895 SET_DECL_IMPLICIT_INSTANTIATION (r);
9896 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9897 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9899 /* The template parameters for this new template are all the
9900 template parameters for the old template, except the
9901 outermost level of parameters. */
9902 DECL_TEMPLATE_PARMS (r)
9903 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9904 complain);
9906 if (PRIMARY_TEMPLATE_P (t))
9907 DECL_PRIMARY_TEMPLATE (r) = r;
9909 if (TREE_CODE (decl) != TYPE_DECL)
9910 /* Record this non-type partial instantiation. */
9911 register_specialization (r, t,
9912 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9913 false, hash);
9915 break;
9917 case FUNCTION_DECL:
9919 tree ctx;
9920 tree argvec = NULL_TREE;
9921 tree *friends;
9922 tree gen_tmpl;
9923 tree type;
9924 int member;
9925 int args_depth;
9926 int parms_depth;
9928 /* Nobody should be tsubst'ing into non-template functions. */
9929 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9931 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9933 tree spec;
9934 bool dependent_p;
9936 /* If T is not dependent, just return it. We have to
9937 increment PROCESSING_TEMPLATE_DECL because
9938 value_dependent_expression_p assumes that nothing is
9939 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9940 ++processing_template_decl;
9941 dependent_p = value_dependent_expression_p (t);
9942 --processing_template_decl;
9943 if (!dependent_p)
9944 RETURN (t);
9946 /* Calculate the most general template of which R is a
9947 specialization, and the complete set of arguments used to
9948 specialize R. */
9949 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9950 argvec = tsubst_template_args (DECL_TI_ARGS
9951 (DECL_TEMPLATE_RESULT
9952 (DECL_TI_TEMPLATE (t))),
9953 args, complain, in_decl);
9954 if (argvec == error_mark_node)
9955 RETURN (error_mark_node);
9957 /* Check to see if we already have this specialization. */
9958 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9959 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9961 if (spec)
9963 r = spec;
9964 break;
9967 /* We can see more levels of arguments than parameters if
9968 there was a specialization of a member template, like
9969 this:
9971 template <class T> struct S { template <class U> void f(); }
9972 template <> template <class U> void S<int>::f(U);
9974 Here, we'll be substituting into the specialization,
9975 because that's where we can find the code we actually
9976 want to generate, but we'll have enough arguments for
9977 the most general template.
9979 We also deal with the peculiar case:
9981 template <class T> struct S {
9982 template <class U> friend void f();
9984 template <class U> void f() {}
9985 template S<int>;
9986 template void f<double>();
9988 Here, the ARGS for the instantiation of will be {int,
9989 double}. But, we only need as many ARGS as there are
9990 levels of template parameters in CODE_PATTERN. We are
9991 careful not to get fooled into reducing the ARGS in
9992 situations like:
9994 template <class T> struct S { template <class U> void f(U); }
9995 template <class T> template <> void S<T>::f(int) {}
9997 which we can spot because the pattern will be a
9998 specialization in this case. */
9999 args_depth = TMPL_ARGS_DEPTH (args);
10000 parms_depth =
10001 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10002 if (args_depth > parms_depth
10003 && !DECL_TEMPLATE_SPECIALIZATION (t))
10004 args = get_innermost_template_args (args, parms_depth);
10006 else
10008 /* This special case arises when we have something like this:
10010 template <class T> struct S {
10011 friend void f<int>(int, double);
10014 Here, the DECL_TI_TEMPLATE for the friend declaration
10015 will be an IDENTIFIER_NODE. We are being called from
10016 tsubst_friend_function, and we want only to create a
10017 new decl (R) with appropriate types so that we can call
10018 determine_specialization. */
10019 gen_tmpl = NULL_TREE;
10022 if (DECL_CLASS_SCOPE_P (t))
10024 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10025 member = 2;
10026 else
10027 member = 1;
10028 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10029 complain, t, /*entering_scope=*/1);
10031 else
10033 member = 0;
10034 ctx = DECL_CONTEXT (t);
10036 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10037 if (type == error_mark_node)
10038 RETURN (error_mark_node);
10040 /* If we hit excessive deduction depth, the type is bogus even if
10041 it isn't error_mark_node, so don't build a decl. */
10042 if (excessive_deduction_depth)
10043 RETURN (error_mark_node);
10045 /* We do NOT check for matching decls pushed separately at this
10046 point, as they may not represent instantiations of this
10047 template, and in any case are considered separate under the
10048 discrete model. */
10049 r = copy_decl (t);
10050 DECL_USE_TEMPLATE (r) = 0;
10051 TREE_TYPE (r) = type;
10052 /* Clear out the mangled name and RTL for the instantiation. */
10053 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10054 SET_DECL_RTL (r, NULL);
10055 /* Leave DECL_INITIAL set on deleted instantiations. */
10056 if (!DECL_DELETED_FN (r))
10057 DECL_INITIAL (r) = NULL_TREE;
10058 DECL_CONTEXT (r) = ctx;
10060 if (member && DECL_CONV_FN_P (r))
10061 /* Type-conversion operator. Reconstruct the name, in
10062 case it's the name of one of the template's parameters. */
10063 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10065 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10066 complain, t);
10067 DECL_RESULT (r) = NULL_TREE;
10069 TREE_STATIC (r) = 0;
10070 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10071 DECL_EXTERNAL (r) = 1;
10072 /* If this is an instantiation of a function with internal
10073 linkage, we already know what object file linkage will be
10074 assigned to the instantiation. */
10075 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10076 DECL_DEFER_OUTPUT (r) = 0;
10077 DECL_CHAIN (r) = NULL_TREE;
10078 DECL_PENDING_INLINE_INFO (r) = 0;
10079 DECL_PENDING_INLINE_P (r) = 0;
10080 DECL_SAVED_TREE (r) = NULL_TREE;
10081 DECL_STRUCT_FUNCTION (r) = NULL;
10082 TREE_USED (r) = 0;
10083 /* We'll re-clone as appropriate in instantiate_template. */
10084 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10086 /* If we aren't complaining now, return on error before we register
10087 the specialization so that we'll complain eventually. */
10088 if ((complain & tf_error) == 0
10089 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10090 && !grok_op_properties (r, /*complain=*/false))
10091 RETURN (error_mark_node);
10093 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10094 this in the special friend case mentioned above where
10095 GEN_TMPL is NULL. */
10096 if (gen_tmpl)
10098 DECL_TEMPLATE_INFO (r)
10099 = build_template_info (gen_tmpl, argvec);
10100 SET_DECL_IMPLICIT_INSTANTIATION (r);
10101 register_specialization (r, gen_tmpl, argvec, false, hash);
10103 /* We're not supposed to instantiate default arguments
10104 until they are called, for a template. But, for a
10105 declaration like:
10107 template <class T> void f ()
10108 { extern void g(int i = T()); }
10110 we should do the substitution when the template is
10111 instantiated. We handle the member function case in
10112 instantiate_class_template since the default arguments
10113 might refer to other members of the class. */
10114 if (!member
10115 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10116 && !uses_template_parms (argvec))
10117 tsubst_default_arguments (r);
10119 else
10120 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10122 /* Copy the list of befriending classes. */
10123 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10124 *friends;
10125 friends = &TREE_CHAIN (*friends))
10127 *friends = copy_node (*friends);
10128 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10129 args, complain,
10130 in_decl);
10133 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10135 maybe_retrofit_in_chrg (r);
10136 if (DECL_CONSTRUCTOR_P (r))
10137 grok_ctor_properties (ctx, r);
10138 /* If this is an instantiation of a member template, clone it.
10139 If it isn't, that'll be handled by
10140 clone_constructors_and_destructors. */
10141 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10142 clone_function_decl (r, /*update_method_vec_p=*/0);
10144 else if ((complain & tf_error) != 0
10145 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10146 && !grok_op_properties (r, /*complain=*/true))
10147 RETURN (error_mark_node);
10149 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10150 SET_DECL_FRIEND_CONTEXT (r,
10151 tsubst (DECL_FRIEND_CONTEXT (t),
10152 args, complain, in_decl));
10154 /* Possibly limit visibility based on template args. */
10155 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10156 if (DECL_VISIBILITY_SPECIFIED (t))
10158 DECL_VISIBILITY_SPECIFIED (r) = 0;
10159 DECL_ATTRIBUTES (r)
10160 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10162 determine_visibility (r);
10163 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10164 && !processing_template_decl)
10165 defaulted_late_check (r);
10167 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10168 args, complain, in_decl);
10170 break;
10172 case PARM_DECL:
10174 tree type = NULL_TREE;
10175 int i, len = 1;
10176 tree expanded_types = NULL_TREE;
10177 tree prev_r = NULL_TREE;
10178 tree first_r = NULL_TREE;
10180 if (FUNCTION_PARAMETER_PACK_P (t))
10182 /* If there is a local specialization that isn't a
10183 parameter pack, it means that we're doing a "simple"
10184 substitution from inside tsubst_pack_expansion. Just
10185 return the local specialization (which will be a single
10186 parm). */
10187 tree spec = retrieve_local_specialization (t);
10188 if (spec
10189 && TREE_CODE (spec) == PARM_DECL
10190 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10191 RETURN (spec);
10193 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10194 the parameters in this function parameter pack. */
10195 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10196 complain, in_decl);
10197 if (TREE_CODE (expanded_types) == TREE_VEC)
10199 len = TREE_VEC_LENGTH (expanded_types);
10201 /* Zero-length parameter packs are boring. Just substitute
10202 into the chain. */
10203 if (len == 0)
10204 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10205 TREE_CHAIN (t)));
10207 else
10209 /* All we did was update the type. Make a note of that. */
10210 type = expanded_types;
10211 expanded_types = NULL_TREE;
10215 /* Loop through all of the parameter's we'll build. When T is
10216 a function parameter pack, LEN is the number of expanded
10217 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10218 r = NULL_TREE;
10219 for (i = 0; i < len; ++i)
10221 prev_r = r;
10222 r = copy_node (t);
10223 if (DECL_TEMPLATE_PARM_P (t))
10224 SET_DECL_TEMPLATE_PARM_P (r);
10226 if (expanded_types)
10227 /* We're on the Ith parameter of the function parameter
10228 pack. */
10230 /* An argument of a function parameter pack is not a parameter
10231 pack. */
10232 FUNCTION_PARAMETER_PACK_P (r) = false;
10234 /* Get the Ith type. */
10235 type = TREE_VEC_ELT (expanded_types, i);
10237 if (DECL_NAME (r))
10238 /* Rename the parameter to include the index. */
10239 DECL_NAME (r) =
10240 make_ith_pack_parameter_name (DECL_NAME (r), i);
10242 else if (!type)
10243 /* We're dealing with a normal parameter. */
10244 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10246 type = type_decays_to (type);
10247 TREE_TYPE (r) = type;
10248 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10250 if (DECL_INITIAL (r))
10252 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10253 DECL_INITIAL (r) = TREE_TYPE (r);
10254 else
10255 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10256 complain, in_decl);
10259 DECL_CONTEXT (r) = NULL_TREE;
10261 if (!DECL_TEMPLATE_PARM_P (r))
10262 DECL_ARG_TYPE (r) = type_passed_as (type);
10264 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10265 args, complain, in_decl);
10267 /* Keep track of the first new parameter we
10268 generate. That's what will be returned to the
10269 caller. */
10270 if (!first_r)
10271 first_r = r;
10273 /* Build a proper chain of parameters when substituting
10274 into a function parameter pack. */
10275 if (prev_r)
10276 DECL_CHAIN (prev_r) = r;
10279 /* If cp_unevaluated_operand is set, we're just looking for a
10280 single dummy parameter, so don't keep going. */
10281 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10282 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10283 complain, DECL_CHAIN (t));
10285 /* FIRST_R contains the start of the chain we've built. */
10286 r = first_r;
10288 break;
10290 case FIELD_DECL:
10292 tree type;
10294 r = copy_decl (t);
10295 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10296 if (type == error_mark_node)
10297 RETURN (error_mark_node);
10298 TREE_TYPE (r) = type;
10299 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10301 if (DECL_C_BIT_FIELD (r))
10302 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10303 non-bit-fields DECL_INITIAL is a non-static data member
10304 initializer, which gets deferred instantiation. */
10305 DECL_INITIAL (r)
10306 = tsubst_expr (DECL_INITIAL (t), args,
10307 complain, in_decl,
10308 /*integral_constant_expression_p=*/true);
10309 else if (DECL_INITIAL (t))
10311 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10312 NSDMI in perform_member_init. Still set DECL_INITIAL
10313 so that we know there is one. */
10314 DECL_INITIAL (r) = void_zero_node;
10315 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10316 retrofit_lang_decl (r);
10317 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10319 /* We don't have to set DECL_CONTEXT here; it is set by
10320 finish_member_declaration. */
10321 DECL_CHAIN (r) = NULL_TREE;
10322 if (VOID_TYPE_P (type))
10323 error ("instantiation of %q+D as type %qT", r, type);
10325 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10326 args, complain, in_decl);
10328 break;
10330 case USING_DECL:
10331 /* We reach here only for member using decls. We also need to check
10332 uses_template_parms because DECL_DEPENDENT_P is not set for a
10333 using-declaration that designates a member of the current
10334 instantiation (c++/53549). */
10335 if (DECL_DEPENDENT_P (t)
10336 || uses_template_parms (USING_DECL_SCOPE (t)))
10338 r = do_class_using_decl
10339 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
10340 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
10341 if (!r)
10342 r = error_mark_node;
10343 else
10345 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10346 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10349 else
10351 r = copy_node (t);
10352 DECL_CHAIN (r) = NULL_TREE;
10354 break;
10356 case TYPE_DECL:
10357 case VAR_DECL:
10359 tree argvec = NULL_TREE;
10360 tree gen_tmpl = NULL_TREE;
10361 tree spec;
10362 tree tmpl = NULL_TREE;
10363 tree ctx;
10364 tree type = NULL_TREE;
10365 bool local_p;
10367 if (TREE_CODE (t) == TYPE_DECL
10368 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10370 /* If this is the canonical decl, we don't have to
10371 mess with instantiations, and often we can't (for
10372 typename, template type parms and such). Note that
10373 TYPE_NAME is not correct for the above test if
10374 we've copied the type for a typedef. */
10375 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10376 if (type == error_mark_node)
10377 RETURN (error_mark_node);
10378 r = TYPE_NAME (type);
10379 break;
10382 /* Check to see if we already have the specialization we
10383 need. */
10384 spec = NULL_TREE;
10385 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10387 /* T is a static data member or namespace-scope entity.
10388 We have to substitute into namespace-scope variables
10389 (even though such entities are never templates) because
10390 of cases like:
10392 template <class T> void f() { extern T t; }
10394 where the entity referenced is not known until
10395 instantiation time. */
10396 local_p = false;
10397 ctx = DECL_CONTEXT (t);
10398 if (DECL_CLASS_SCOPE_P (t))
10400 ctx = tsubst_aggr_type (ctx, args,
10401 complain,
10402 in_decl, /*entering_scope=*/1);
10403 /* If CTX is unchanged, then T is in fact the
10404 specialization we want. That situation occurs when
10405 referencing a static data member within in its own
10406 class. We can use pointer equality, rather than
10407 same_type_p, because DECL_CONTEXT is always
10408 canonical... */
10409 if (ctx == DECL_CONTEXT (t)
10410 && (TREE_CODE (t) != TYPE_DECL
10411 /* ... unless T is a member template; in which
10412 case our caller can be willing to create a
10413 specialization of that template represented
10414 by T. */
10415 || !(DECL_TI_TEMPLATE (t)
10416 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10417 spec = t;
10420 if (!spec)
10422 tmpl = DECL_TI_TEMPLATE (t);
10423 gen_tmpl = most_general_template (tmpl);
10424 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10425 if (argvec == error_mark_node)
10426 RETURN (error_mark_node);
10427 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10428 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10431 else
10433 /* A local variable. */
10434 local_p = true;
10435 /* Subsequent calls to pushdecl will fill this in. */
10436 ctx = NULL_TREE;
10437 spec = retrieve_local_specialization (t);
10439 /* If we already have the specialization we need, there is
10440 nothing more to do. */
10441 if (spec)
10443 r = spec;
10444 break;
10447 /* Create a new node for the specialization we need. */
10448 r = copy_decl (t);
10449 if (type == NULL_TREE)
10451 if (is_typedef_decl (t))
10452 type = DECL_ORIGINAL_TYPE (t);
10453 else
10454 type = TREE_TYPE (t);
10455 if (TREE_CODE (t) == VAR_DECL
10456 && VAR_HAD_UNKNOWN_BOUND (t)
10457 && type != error_mark_node)
10458 type = strip_array_domain (type);
10459 type = tsubst (type, args, complain, in_decl);
10461 if (TREE_CODE (r) == VAR_DECL)
10463 /* Even if the original location is out of scope, the
10464 newly substituted one is not. */
10465 DECL_DEAD_FOR_LOCAL (r) = 0;
10466 DECL_INITIALIZED_P (r) = 0;
10467 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10468 if (type == error_mark_node)
10469 RETURN (error_mark_node);
10470 if (TREE_CODE (type) == FUNCTION_TYPE)
10472 /* It may seem that this case cannot occur, since:
10474 typedef void f();
10475 void g() { f x; }
10477 declares a function, not a variable. However:
10479 typedef void f();
10480 template <typename T> void g() { T t; }
10481 template void g<f>();
10483 is an attempt to declare a variable with function
10484 type. */
10485 error ("variable %qD has function type",
10486 /* R is not yet sufficiently initialized, so we
10487 just use its name. */
10488 DECL_NAME (r));
10489 RETURN (error_mark_node);
10491 type = complete_type (type);
10492 /* Wait until cp_finish_decl to set this again, to handle
10493 circular dependency (template/instantiate6.C). */
10494 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10495 type = check_var_type (DECL_NAME (r), type);
10497 if (DECL_HAS_VALUE_EXPR_P (t))
10499 tree ve = DECL_VALUE_EXPR (t);
10500 ve = tsubst_expr (ve, args, complain, in_decl,
10501 /*constant_expression_p=*/false);
10502 if (REFERENCE_REF_P (ve))
10504 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10505 ve = TREE_OPERAND (ve, 0);
10507 SET_DECL_VALUE_EXPR (r, ve);
10510 else if (DECL_SELF_REFERENCE_P (t))
10511 SET_DECL_SELF_REFERENCE_P (r);
10512 TREE_TYPE (r) = type;
10513 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10514 DECL_CONTEXT (r) = ctx;
10515 /* Clear out the mangled name and RTL for the instantiation. */
10516 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10517 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10518 SET_DECL_RTL (r, NULL);
10519 /* The initializer must not be expanded until it is required;
10520 see [temp.inst]. */
10521 DECL_INITIAL (r) = NULL_TREE;
10522 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10523 SET_DECL_RTL (r, NULL);
10524 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10525 if (TREE_CODE (r) == VAR_DECL)
10527 /* Possibly limit visibility based on template args. */
10528 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10529 if (DECL_VISIBILITY_SPECIFIED (t))
10531 DECL_VISIBILITY_SPECIFIED (r) = 0;
10532 DECL_ATTRIBUTES (r)
10533 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10535 determine_visibility (r);
10538 if (!local_p)
10540 /* A static data member declaration is always marked
10541 external when it is declared in-class, even if an
10542 initializer is present. We mimic the non-template
10543 processing here. */
10544 DECL_EXTERNAL (r) = 1;
10546 register_specialization (r, gen_tmpl, argvec, false, hash);
10547 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10548 SET_DECL_IMPLICIT_INSTANTIATION (r);
10550 else if (cp_unevaluated_operand)
10552 /* We're substituting this var in a decltype outside of its
10553 scope, such as for a lambda return type. Don't add it to
10554 local_specializations, do perform auto deduction. */
10555 tree auto_node = type_uses_auto (type);
10556 if (auto_node)
10558 tree init
10559 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10560 /*constant_expression_p=*/false);
10561 init = resolve_nondeduced_context (init);
10562 TREE_TYPE (r) = type
10563 = do_auto_deduction (type, init, auto_node);
10566 else
10567 register_local_specialization (r, t);
10569 DECL_CHAIN (r) = NULL_TREE;
10571 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10572 /*flags=*/0,
10573 args, complain, in_decl);
10575 /* Preserve a typedef that names a type. */
10576 if (is_typedef_decl (r))
10578 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10579 set_underlying_type (r);
10582 layout_decl (r, 0);
10584 break;
10586 default:
10587 gcc_unreachable ();
10589 #undef RETURN
10591 out:
10592 /* Restore the file and line information. */
10593 input_location = saved_loc;
10595 return r;
10598 /* Substitute into the ARG_TYPES of a function type.
10599 If END is a TREE_CHAIN, leave it and any following types
10600 un-substituted. */
10602 static tree
10603 tsubst_arg_types (tree arg_types,
10604 tree args,
10605 tree end,
10606 tsubst_flags_t complain,
10607 tree in_decl)
10609 tree remaining_arg_types;
10610 tree type = NULL_TREE;
10611 int i = 1;
10612 tree expanded_args = NULL_TREE;
10613 tree default_arg;
10615 if (!arg_types || arg_types == void_list_node || arg_types == end)
10616 return arg_types;
10618 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10619 args, end, complain, in_decl);
10620 if (remaining_arg_types == error_mark_node)
10621 return error_mark_node;
10623 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10625 /* For a pack expansion, perform substitution on the
10626 entire expression. Later on, we'll handle the arguments
10627 one-by-one. */
10628 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10629 args, complain, in_decl);
10631 if (TREE_CODE (expanded_args) == TREE_VEC)
10632 /* So that we'll spin through the parameters, one by one. */
10633 i = TREE_VEC_LENGTH (expanded_args);
10634 else
10636 /* We only partially substituted into the parameter
10637 pack. Our type is TYPE_PACK_EXPANSION. */
10638 type = expanded_args;
10639 expanded_args = NULL_TREE;
10643 while (i > 0) {
10644 --i;
10646 if (expanded_args)
10647 type = TREE_VEC_ELT (expanded_args, i);
10648 else if (!type)
10649 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10651 if (type == error_mark_node)
10652 return error_mark_node;
10653 if (VOID_TYPE_P (type))
10655 if (complain & tf_error)
10657 error ("invalid parameter type %qT", type);
10658 if (in_decl)
10659 error ("in declaration %q+D", in_decl);
10661 return error_mark_node;
10664 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10665 top-level qualifiers as required. */
10666 type = cv_unqualified (type_decays_to (type));
10668 /* We do not substitute into default arguments here. The standard
10669 mandates that they be instantiated only when needed, which is
10670 done in build_over_call. */
10671 default_arg = TREE_PURPOSE (arg_types);
10673 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10675 /* We've instantiated a template before its default arguments
10676 have been parsed. This can happen for a nested template
10677 class, and is not an error unless we require the default
10678 argument in a call of this function. */
10679 remaining_arg_types =
10680 tree_cons (default_arg, type, remaining_arg_types);
10681 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
10682 remaining_arg_types);
10684 else
10685 remaining_arg_types =
10686 hash_tree_cons (default_arg, type, remaining_arg_types);
10689 return remaining_arg_types;
10692 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10693 *not* handle the exception-specification for FNTYPE, because the
10694 initial substitution of explicitly provided template parameters
10695 during argument deduction forbids substitution into the
10696 exception-specification:
10698 [temp.deduct]
10700 All references in the function type of the function template to the
10701 corresponding template parameters are replaced by the specified tem-
10702 plate argument values. If a substitution in a template parameter or
10703 in the function type of the function template results in an invalid
10704 type, type deduction fails. [Note: The equivalent substitution in
10705 exception specifications is done only when the function is instanti-
10706 ated, at which point a program is ill-formed if the substitution
10707 results in an invalid type.] */
10709 static tree
10710 tsubst_function_type (tree t,
10711 tree args,
10712 tsubst_flags_t complain,
10713 tree in_decl)
10715 tree return_type;
10716 tree arg_types;
10717 tree fntype;
10719 /* The TYPE_CONTEXT is not used for function/method types. */
10720 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10722 /* Substitute the return type. */
10723 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10724 if (return_type == error_mark_node)
10725 return error_mark_node;
10726 /* The standard does not presently indicate that creation of a
10727 function type with an invalid return type is a deduction failure.
10728 However, that is clearly analogous to creating an array of "void"
10729 or a reference to a reference. This is core issue #486. */
10730 if (TREE_CODE (return_type) == ARRAY_TYPE
10731 || TREE_CODE (return_type) == FUNCTION_TYPE)
10733 if (complain & tf_error)
10735 if (TREE_CODE (return_type) == ARRAY_TYPE)
10736 error ("function returning an array");
10737 else
10738 error ("function returning a function");
10740 return error_mark_node;
10743 /* Substitute the argument types. */
10744 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
10745 complain, in_decl);
10746 if (arg_types == error_mark_node)
10747 return error_mark_node;
10749 /* Construct a new type node and return it. */
10750 if (TREE_CODE (t) == FUNCTION_TYPE)
10752 fntype = build_function_type (return_type, arg_types);
10753 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10755 else
10757 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10758 if (! MAYBE_CLASS_TYPE_P (r))
10760 /* [temp.deduct]
10762 Type deduction may fail for any of the following
10763 reasons:
10765 -- Attempting to create "pointer to member of T" when T
10766 is not a class type. */
10767 if (complain & tf_error)
10768 error ("creating pointer to member function of non-class type %qT",
10770 return error_mark_node;
10773 fntype = build_method_type_directly (r, return_type,
10774 TREE_CHAIN (arg_types));
10776 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10778 return fntype;
10781 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10782 ARGS into that specification, and return the substituted
10783 specification. If there is no specification, return NULL_TREE. */
10785 static tree
10786 tsubst_exception_specification (tree fntype,
10787 tree args,
10788 tsubst_flags_t complain,
10789 tree in_decl,
10790 bool defer_ok)
10792 tree specs;
10793 tree new_specs;
10795 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10796 new_specs = NULL_TREE;
10797 if (specs && TREE_PURPOSE (specs))
10799 /* A noexcept-specifier. */
10800 tree expr = TREE_PURPOSE (specs);
10801 if (expr == boolean_true_node || expr == boolean_false_node)
10802 new_specs = expr;
10803 else if (defer_ok)
10805 /* Defer instantiation of noexcept-specifiers to avoid
10806 excessive instantiations (c++/49107). */
10807 new_specs = make_node (DEFERRED_NOEXCEPT);
10808 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10810 /* We already partially instantiated this member template,
10811 so combine the new args with the old. */
10812 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10813 = DEFERRED_NOEXCEPT_PATTERN (expr);
10814 DEFERRED_NOEXCEPT_ARGS (new_specs)
10815 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10817 else
10819 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10820 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10823 else
10824 new_specs = tsubst_copy_and_build
10825 (expr, args, complain, in_decl, /*function_p=*/false,
10826 /*integral_constant_expression_p=*/true);
10827 new_specs = build_noexcept_spec (new_specs, complain);
10829 else if (specs)
10831 if (! TREE_VALUE (specs))
10832 new_specs = specs;
10833 else
10834 while (specs)
10836 tree spec;
10837 int i, len = 1;
10838 tree expanded_specs = NULL_TREE;
10840 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10842 /* Expand the pack expansion type. */
10843 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10844 args, complain,
10845 in_decl);
10847 if (expanded_specs == error_mark_node)
10848 return error_mark_node;
10849 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10850 len = TREE_VEC_LENGTH (expanded_specs);
10851 else
10853 /* We're substituting into a member template, so
10854 we got a TYPE_PACK_EXPANSION back. Add that
10855 expansion and move on. */
10856 gcc_assert (TREE_CODE (expanded_specs)
10857 == TYPE_PACK_EXPANSION);
10858 new_specs = add_exception_specifier (new_specs,
10859 expanded_specs,
10860 complain);
10861 specs = TREE_CHAIN (specs);
10862 continue;
10866 for (i = 0; i < len; ++i)
10868 if (expanded_specs)
10869 spec = TREE_VEC_ELT (expanded_specs, i);
10870 else
10871 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10872 if (spec == error_mark_node)
10873 return spec;
10874 new_specs = add_exception_specifier (new_specs, spec,
10875 complain);
10878 specs = TREE_CHAIN (specs);
10881 return new_specs;
10884 /* Take the tree structure T and replace template parameters used
10885 therein with the argument vector ARGS. IN_DECL is an associated
10886 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10887 Issue error and warning messages under control of COMPLAIN. Note
10888 that we must be relatively non-tolerant of extensions here, in
10889 order to preserve conformance; if we allow substitutions that
10890 should not be allowed, we may allow argument deductions that should
10891 not succeed, and therefore report ambiguous overload situations
10892 where there are none. In theory, we could allow the substitution,
10893 but indicate that it should have failed, and allow our caller to
10894 make sure that the right thing happens, but we don't try to do this
10895 yet.
10897 This function is used for dealing with types, decls and the like;
10898 for expressions, use tsubst_expr or tsubst_copy. */
10900 tree
10901 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10903 enum tree_code code;
10904 tree type, r = NULL_TREE;
10906 if (t == NULL_TREE || t == error_mark_node
10907 || t == integer_type_node
10908 || t == void_type_node
10909 || t == char_type_node
10910 || t == unknown_type_node
10911 || TREE_CODE (t) == NAMESPACE_DECL
10912 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10913 return t;
10915 if (DECL_P (t))
10916 return tsubst_decl (t, args, complain);
10918 if (args == NULL_TREE)
10919 return t;
10921 code = TREE_CODE (t);
10923 if (code == IDENTIFIER_NODE)
10924 type = IDENTIFIER_TYPE_VALUE (t);
10925 else
10926 type = TREE_TYPE (t);
10928 gcc_assert (type != unknown_type_node);
10930 /* Reuse typedefs. We need to do this to handle dependent attributes,
10931 such as attribute aligned. */
10932 if (TYPE_P (t)
10933 && typedef_variant_p (t))
10935 tree decl = TYPE_NAME (t);
10937 if (TYPE_DECL_ALIAS_P (decl)
10938 && DECL_LANG_SPECIFIC (decl)
10939 && DECL_TEMPLATE_INFO (decl)
10940 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
10942 /* DECL represents an alias template and we want to
10943 instantiate it. Let's substitute our arguments for the
10944 template parameters into the declaration and get the
10945 resulting type. */
10946 r = tsubst (decl, args, complain, decl);
10948 else if (DECL_CLASS_SCOPE_P (decl)
10949 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10950 && uses_template_parms (DECL_CONTEXT (decl)))
10952 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10953 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10954 r = retrieve_specialization (tmpl, gen_args, 0);
10956 else if (DECL_FUNCTION_SCOPE_P (decl)
10957 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10958 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10959 r = retrieve_local_specialization (decl);
10960 else
10961 /* The typedef is from a non-template context. */
10962 return t;
10964 if (r)
10966 r = TREE_TYPE (r);
10967 r = cp_build_qualified_type_real
10968 (r, cp_type_quals (t) | cp_type_quals (r),
10969 complain | tf_ignore_bad_quals);
10970 return r;
10972 else
10973 /* We don't have an instantiation yet, so drop the typedef. */
10974 t = DECL_ORIGINAL_TYPE (decl);
10977 if (type
10978 && code != TYPENAME_TYPE
10979 && code != TEMPLATE_TYPE_PARM
10980 && code != IDENTIFIER_NODE
10981 && code != FUNCTION_TYPE
10982 && code != METHOD_TYPE)
10983 type = tsubst (type, args, complain, in_decl);
10984 if (type == error_mark_node)
10985 return error_mark_node;
10987 switch (code)
10989 case RECORD_TYPE:
10990 case UNION_TYPE:
10991 case ENUMERAL_TYPE:
10992 return tsubst_aggr_type (t, args, complain, in_decl,
10993 /*entering_scope=*/0);
10995 case ERROR_MARK:
10996 case IDENTIFIER_NODE:
10997 case VOID_TYPE:
10998 case REAL_TYPE:
10999 case COMPLEX_TYPE:
11000 case VECTOR_TYPE:
11001 case BOOLEAN_TYPE:
11002 case NULLPTR_TYPE:
11003 case LANG_TYPE:
11004 return t;
11006 case INTEGER_TYPE:
11007 if (t == integer_type_node)
11008 return t;
11010 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11011 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11012 return t;
11015 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11017 max = tsubst_expr (omax, args, complain, in_decl,
11018 /*integral_constant_expression_p=*/false);
11020 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11021 needed. */
11022 if (TREE_CODE (max) == NOP_EXPR
11023 && TREE_SIDE_EFFECTS (omax)
11024 && !TREE_TYPE (max))
11025 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11027 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11028 with TREE_SIDE_EFFECTS that indicates this is not an integral
11029 constant expression. */
11030 if (processing_template_decl
11031 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11033 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11034 TREE_SIDE_EFFECTS (max) = 1;
11037 return compute_array_index_type (NULL_TREE, max, complain);
11040 case TEMPLATE_TYPE_PARM:
11041 case TEMPLATE_TEMPLATE_PARM:
11042 case BOUND_TEMPLATE_TEMPLATE_PARM:
11043 case TEMPLATE_PARM_INDEX:
11045 int idx;
11046 int level;
11047 int levels;
11048 tree arg = NULL_TREE;
11050 r = NULL_TREE;
11052 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11053 template_parm_level_and_index (t, &level, &idx);
11055 levels = TMPL_ARGS_DEPTH (args);
11056 if (level <= levels)
11058 arg = TMPL_ARG (args, level, idx);
11060 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11061 /* See through ARGUMENT_PACK_SELECT arguments. */
11062 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11065 if (arg == error_mark_node)
11066 return error_mark_node;
11067 else if (arg != NULL_TREE)
11069 if (ARGUMENT_PACK_P (arg))
11070 /* If ARG is an argument pack, we don't actually want to
11071 perform a substitution here, because substitutions
11072 for argument packs are only done
11073 element-by-element. We can get to this point when
11074 substituting the type of a non-type template
11075 parameter pack, when that type actually contains
11076 template parameter packs from an outer template, e.g.,
11078 template<typename... Types> struct A {
11079 template<Types... Values> struct B { };
11080 }; */
11081 return t;
11083 if (code == TEMPLATE_TYPE_PARM)
11085 int quals;
11086 gcc_assert (TYPE_P (arg));
11088 quals = cp_type_quals (arg) | cp_type_quals (t);
11090 return cp_build_qualified_type_real
11091 (arg, quals, complain | tf_ignore_bad_quals);
11093 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11095 /* We are processing a type constructed from a
11096 template template parameter. */
11097 tree argvec = tsubst (TYPE_TI_ARGS (t),
11098 args, complain, in_decl);
11099 if (argvec == error_mark_node)
11100 return error_mark_node;
11102 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11103 || TREE_CODE (arg) == TEMPLATE_DECL
11104 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11106 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11107 /* Consider this code:
11109 template <template <class> class Template>
11110 struct Internal {
11111 template <class Arg> using Bind = Template<Arg>;
11114 template <template <class> class Template, class Arg>
11115 using Instantiate = Template<Arg>; //#0
11117 template <template <class> class Template,
11118 class Argument>
11119 using Bind =
11120 Instantiate<Internal<Template>::template Bind,
11121 Argument>; //#1
11123 When #1 is parsed, the
11124 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11125 parameter `Template' in #0 matches the
11126 UNBOUND_CLASS_TEMPLATE representing the argument
11127 `Internal<Template>::template Bind'; We then want
11128 to assemble the type `Bind<Argument>' that can't
11129 be fully created right now, because
11130 `Internal<Template>' not being complete, the Bind
11131 template cannot be looked up in that context. So
11132 we need to "store" `Bind<Argument>' for later
11133 when the context of Bind becomes complete. Let's
11134 store that in a TYPENAME_TYPE. */
11135 return make_typename_type (TYPE_CONTEXT (arg),
11136 build_nt (TEMPLATE_ID_EXPR,
11137 TYPE_IDENTIFIER (arg),
11138 argvec),
11139 typename_type,
11140 complain);
11142 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11143 are resolving nested-types in the signature of a
11144 member function templates. Otherwise ARG is a
11145 TEMPLATE_DECL and is the real template to be
11146 instantiated. */
11147 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11148 arg = TYPE_NAME (arg);
11150 r = lookup_template_class (arg,
11151 argvec, in_decl,
11152 DECL_CONTEXT (arg),
11153 /*entering_scope=*/0,
11154 complain);
11155 return cp_build_qualified_type_real
11156 (r, cp_type_quals (t), complain);
11158 else
11159 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11160 return convert_from_reference (unshare_expr (arg));
11163 if (level == 1)
11164 /* This can happen during the attempted tsubst'ing in
11165 unify. This means that we don't yet have any information
11166 about the template parameter in question. */
11167 return t;
11169 /* Early in template argument deduction substitution, we don't
11170 want to reduce the level of 'auto', or it will be confused
11171 with a normal template parm in subsequent deduction. */
11172 if (is_auto (t) && (complain & tf_partial))
11173 return t;
11175 /* If we get here, we must have been looking at a parm for a
11176 more deeply nested template. Make a new version of this
11177 template parameter, but with a lower level. */
11178 switch (code)
11180 case TEMPLATE_TYPE_PARM:
11181 case TEMPLATE_TEMPLATE_PARM:
11182 case BOUND_TEMPLATE_TEMPLATE_PARM:
11183 if (cp_type_quals (t))
11185 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11186 r = cp_build_qualified_type_real
11187 (r, cp_type_quals (t),
11188 complain | (code == TEMPLATE_TYPE_PARM
11189 ? tf_ignore_bad_quals : 0));
11191 else
11193 r = copy_type (t);
11194 TEMPLATE_TYPE_PARM_INDEX (r)
11195 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11196 r, levels, args, complain);
11197 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11198 TYPE_MAIN_VARIANT (r) = r;
11199 TYPE_POINTER_TO (r) = NULL_TREE;
11200 TYPE_REFERENCE_TO (r) = NULL_TREE;
11202 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11203 /* We have reduced the level of the template
11204 template parameter, but not the levels of its
11205 template parameters, so canonical_type_parameter
11206 will not be able to find the canonical template
11207 template parameter for this level. Thus, we
11208 require structural equality checking to compare
11209 TEMPLATE_TEMPLATE_PARMs. */
11210 SET_TYPE_STRUCTURAL_EQUALITY (r);
11211 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11212 SET_TYPE_STRUCTURAL_EQUALITY (r);
11213 else
11214 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11216 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11218 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11219 complain, in_decl);
11220 if (argvec == error_mark_node)
11221 return error_mark_node;
11223 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11224 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11227 break;
11229 case TEMPLATE_PARM_INDEX:
11230 r = reduce_template_parm_level (t, type, levels, args, complain);
11231 break;
11233 default:
11234 gcc_unreachable ();
11237 return r;
11240 case TREE_LIST:
11242 tree purpose, value, chain;
11244 if (t == void_list_node)
11245 return t;
11247 purpose = TREE_PURPOSE (t);
11248 if (purpose)
11250 purpose = tsubst (purpose, args, complain, in_decl);
11251 if (purpose == error_mark_node)
11252 return error_mark_node;
11254 value = TREE_VALUE (t);
11255 if (value)
11257 value = tsubst (value, args, complain, in_decl);
11258 if (value == error_mark_node)
11259 return error_mark_node;
11261 chain = TREE_CHAIN (t);
11262 if (chain && chain != void_type_node)
11264 chain = tsubst (chain, args, complain, in_decl);
11265 if (chain == error_mark_node)
11266 return error_mark_node;
11268 if (purpose == TREE_PURPOSE (t)
11269 && value == TREE_VALUE (t)
11270 && chain == TREE_CHAIN (t))
11271 return t;
11272 return hash_tree_cons (purpose, value, chain);
11275 case TREE_BINFO:
11276 /* We should never be tsubsting a binfo. */
11277 gcc_unreachable ();
11279 case TREE_VEC:
11280 /* A vector of template arguments. */
11281 gcc_assert (!type);
11282 return tsubst_template_args (t, args, complain, in_decl);
11284 case POINTER_TYPE:
11285 case REFERENCE_TYPE:
11287 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11288 return t;
11290 /* [temp.deduct]
11292 Type deduction may fail for any of the following
11293 reasons:
11295 -- Attempting to create a pointer to reference type.
11296 -- Attempting to create a reference to a reference type or
11297 a reference to void.
11299 Core issue 106 says that creating a reference to a reference
11300 during instantiation is no longer a cause for failure. We
11301 only enforce this check in strict C++98 mode. */
11302 if ((TREE_CODE (type) == REFERENCE_TYPE
11303 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11304 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11306 static location_t last_loc;
11308 /* We keep track of the last time we issued this error
11309 message to avoid spewing a ton of messages during a
11310 single bad template instantiation. */
11311 if (complain & tf_error
11312 && last_loc != input_location)
11314 if (TREE_CODE (type) == VOID_TYPE)
11315 error ("forming reference to void");
11316 else if (code == POINTER_TYPE)
11317 error ("forming pointer to reference type %qT", type);
11318 else
11319 error ("forming reference to reference type %qT", type);
11320 last_loc = input_location;
11323 return error_mark_node;
11325 else if (code == POINTER_TYPE)
11327 r = build_pointer_type (type);
11328 if (TREE_CODE (type) == METHOD_TYPE)
11329 r = build_ptrmemfunc_type (r);
11331 else if (TREE_CODE (type) == REFERENCE_TYPE)
11332 /* In C++0x, during template argument substitution, when there is an
11333 attempt to create a reference to a reference type, reference
11334 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11336 "If a template-argument for a template-parameter T names a type
11337 that is a reference to a type A, an attempt to create the type
11338 'lvalue reference to cv T' creates the type 'lvalue reference to
11339 A,' while an attempt to create the type type rvalue reference to
11340 cv T' creates the type T"
11342 r = cp_build_reference_type
11343 (TREE_TYPE (type),
11344 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11345 else
11346 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11347 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11349 if (r != error_mark_node)
11350 /* Will this ever be needed for TYPE_..._TO values? */
11351 layout_type (r);
11353 return r;
11355 case OFFSET_TYPE:
11357 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11358 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11360 /* [temp.deduct]
11362 Type deduction may fail for any of the following
11363 reasons:
11365 -- Attempting to create "pointer to member of T" when T
11366 is not a class type. */
11367 if (complain & tf_error)
11368 error ("creating pointer to member of non-class type %qT", r);
11369 return error_mark_node;
11371 if (TREE_CODE (type) == REFERENCE_TYPE)
11373 if (complain & tf_error)
11374 error ("creating pointer to member reference type %qT", type);
11375 return error_mark_node;
11377 if (TREE_CODE (type) == VOID_TYPE)
11379 if (complain & tf_error)
11380 error ("creating pointer to member of type void");
11381 return error_mark_node;
11383 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11384 if (TREE_CODE (type) == FUNCTION_TYPE)
11386 /* The type of the implicit object parameter gets its
11387 cv-qualifiers from the FUNCTION_TYPE. */
11388 tree memptr;
11389 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11390 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11391 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11392 complain);
11394 else
11395 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11396 cp_type_quals (t),
11397 complain);
11399 case FUNCTION_TYPE:
11400 case METHOD_TYPE:
11402 tree fntype;
11403 tree specs;
11404 fntype = tsubst_function_type (t, args, complain, in_decl);
11405 if (fntype == error_mark_node)
11406 return error_mark_node;
11408 /* Substitute the exception specification. */
11409 specs = tsubst_exception_specification (t, args, complain,
11410 in_decl, /*defer_ok*/true);
11411 if (specs == error_mark_node)
11412 return error_mark_node;
11413 if (specs)
11414 fntype = build_exception_variant (fntype, specs);
11415 return fntype;
11417 case ARRAY_TYPE:
11419 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11420 if (domain == error_mark_node)
11421 return error_mark_node;
11423 /* As an optimization, we avoid regenerating the array type if
11424 it will obviously be the same as T. */
11425 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11426 return t;
11428 /* These checks should match the ones in grokdeclarator.
11430 [temp.deduct]
11432 The deduction may fail for any of the following reasons:
11434 -- Attempting to create an array with an element type that
11435 is void, a function type, or a reference type, or [DR337]
11436 an abstract class type. */
11437 if (TREE_CODE (type) == VOID_TYPE
11438 || TREE_CODE (type) == FUNCTION_TYPE
11439 || TREE_CODE (type) == REFERENCE_TYPE)
11441 if (complain & tf_error)
11442 error ("creating array of %qT", type);
11443 return error_mark_node;
11445 if (ABSTRACT_CLASS_TYPE_P (type))
11447 if (complain & tf_error)
11448 error ("creating array of %qT, which is an abstract class type",
11449 type);
11450 return error_mark_node;
11453 r = build_cplus_array_type (type, domain);
11455 if (TYPE_USER_ALIGN (t))
11457 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11458 TYPE_USER_ALIGN (r) = 1;
11461 return r;
11464 case TYPENAME_TYPE:
11466 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11467 in_decl, /*entering_scope=*/1);
11468 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11469 complain, in_decl);
11471 if (ctx == error_mark_node || f == error_mark_node)
11472 return error_mark_node;
11474 if (!MAYBE_CLASS_TYPE_P (ctx))
11476 if (complain & tf_error)
11477 error ("%qT is not a class, struct, or union type", ctx);
11478 return error_mark_node;
11480 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11482 /* Normally, make_typename_type does not require that the CTX
11483 have complete type in order to allow things like:
11485 template <class T> struct S { typename S<T>::X Y; };
11487 But, such constructs have already been resolved by this
11488 point, so here CTX really should have complete type, unless
11489 it's a partial instantiation. */
11490 ctx = complete_type (ctx);
11491 if (!COMPLETE_TYPE_P (ctx))
11493 if (complain & tf_error)
11494 cxx_incomplete_type_error (NULL_TREE, ctx);
11495 return error_mark_node;
11499 f = make_typename_type (ctx, f, typename_type,
11500 complain | tf_keep_type_decl);
11501 if (f == error_mark_node)
11502 return f;
11503 if (TREE_CODE (f) == TYPE_DECL)
11505 complain |= tf_ignore_bad_quals;
11506 f = TREE_TYPE (f);
11509 if (TREE_CODE (f) != TYPENAME_TYPE)
11511 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11513 if (complain & tf_error)
11514 error ("%qT resolves to %qT, which is not an enumeration type",
11515 t, f);
11516 else
11517 return error_mark_node;
11519 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11521 if (complain & tf_error)
11522 error ("%qT resolves to %qT, which is is not a class type",
11523 t, f);
11524 else
11525 return error_mark_node;
11529 return cp_build_qualified_type_real
11530 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11533 case UNBOUND_CLASS_TEMPLATE:
11535 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11536 in_decl, /*entering_scope=*/1);
11537 tree name = TYPE_IDENTIFIER (t);
11538 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11540 if (ctx == error_mark_node || name == error_mark_node)
11541 return error_mark_node;
11543 if (parm_list)
11544 parm_list = tsubst_template_parms (parm_list, args, complain);
11545 return make_unbound_class_template (ctx, name, parm_list, complain);
11548 case TYPEOF_TYPE:
11550 tree type;
11552 ++cp_unevaluated_operand;
11553 ++c_inhibit_evaluation_warnings;
11555 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11556 complain, in_decl,
11557 /*integral_constant_expression_p=*/false);
11559 --cp_unevaluated_operand;
11560 --c_inhibit_evaluation_warnings;
11562 type = finish_typeof (type);
11563 return cp_build_qualified_type_real (type,
11564 cp_type_quals (t)
11565 | cp_type_quals (type),
11566 complain);
11569 case DECLTYPE_TYPE:
11571 tree type;
11573 ++cp_unevaluated_operand;
11574 ++c_inhibit_evaluation_warnings;
11576 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11577 complain, in_decl,
11578 /*integral_constant_expression_p=*/false);
11580 --cp_unevaluated_operand;
11581 --c_inhibit_evaluation_warnings;
11583 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11584 type = lambda_capture_field_type (type);
11585 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11586 type = lambda_proxy_type (type);
11587 else
11588 type = finish_decltype_type
11589 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11590 return cp_build_qualified_type_real (type,
11591 cp_type_quals (t)
11592 | cp_type_quals (type),
11593 complain);
11596 case UNDERLYING_TYPE:
11598 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11599 complain, in_decl);
11600 return finish_underlying_type (type);
11603 case TYPE_ARGUMENT_PACK:
11604 case NONTYPE_ARGUMENT_PACK:
11606 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11607 tree packed_out =
11608 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11609 args,
11610 complain,
11611 in_decl);
11612 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11614 /* For template nontype argument packs, also substitute into
11615 the type. */
11616 if (code == NONTYPE_ARGUMENT_PACK)
11617 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11619 return r;
11621 break;
11623 case INTEGER_CST:
11624 case REAL_CST:
11625 case STRING_CST:
11626 case PLUS_EXPR:
11627 case MINUS_EXPR:
11628 case NEGATE_EXPR:
11629 case NOP_EXPR:
11630 case INDIRECT_REF:
11631 case ADDR_EXPR:
11632 case CALL_EXPR:
11633 case ARRAY_REF:
11634 case SCOPE_REF:
11635 /* We should use one of the expression tsubsts for these codes. */
11636 gcc_unreachable ();
11638 default:
11639 sorry ("use of %qs in template", tree_code_name [(int) code]);
11640 return error_mark_node;
11644 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11645 type of the expression on the left-hand side of the "." or "->"
11646 operator. */
11648 static tree
11649 tsubst_baselink (tree baselink, tree object_type,
11650 tree args, tsubst_flags_t complain, tree in_decl)
11652 tree name;
11653 tree qualifying_scope;
11654 tree fns;
11655 tree optype;
11656 tree template_args = 0;
11657 bool template_id_p = false;
11658 bool qualified = BASELINK_QUALIFIED_P (baselink);
11660 /* A baselink indicates a function from a base class. Both the
11661 BASELINK_ACCESS_BINFO and the base class referenced may
11662 indicate bases of the template class, rather than the
11663 instantiated class. In addition, lookups that were not
11664 ambiguous before may be ambiguous now. Therefore, we perform
11665 the lookup again. */
11666 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11667 qualifying_scope = tsubst (qualifying_scope, args,
11668 complain, in_decl);
11669 fns = BASELINK_FUNCTIONS (baselink);
11670 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11671 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11673 template_id_p = true;
11674 template_args = TREE_OPERAND (fns, 1);
11675 fns = TREE_OPERAND (fns, 0);
11676 if (template_args)
11677 template_args = tsubst_template_args (template_args, args,
11678 complain, in_decl);
11680 name = DECL_NAME (get_first_fn (fns));
11681 if (IDENTIFIER_TYPENAME_P (name))
11682 name = mangle_conv_op_name_for_type (optype);
11683 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11684 if (!baselink)
11685 return error_mark_node;
11687 /* If lookup found a single function, mark it as used at this
11688 point. (If it lookup found multiple functions the one selected
11689 later by overload resolution will be marked as used at that
11690 point.) */
11691 if (BASELINK_P (baselink))
11692 fns = BASELINK_FUNCTIONS (baselink);
11693 if (!template_id_p && !really_overloaded_fn (fns))
11694 mark_used (OVL_CURRENT (fns));
11696 /* Add back the template arguments, if present. */
11697 if (BASELINK_P (baselink) && template_id_p)
11698 BASELINK_FUNCTIONS (baselink)
11699 = build_nt (TEMPLATE_ID_EXPR,
11700 BASELINK_FUNCTIONS (baselink),
11701 template_args);
11702 /* Update the conversion operator type. */
11703 BASELINK_OPTYPE (baselink) = optype;
11705 if (!object_type)
11706 object_type = current_class_type;
11708 if (qualified)
11709 baselink = adjust_result_of_qualified_name_lookup (baselink,
11710 qualifying_scope,
11711 object_type);
11712 return baselink;
11715 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11716 true if the qualified-id will be a postfix-expression in-and-of
11717 itself; false if more of the postfix-expression follows the
11718 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11719 of "&". */
11721 static tree
11722 tsubst_qualified_id (tree qualified_id, tree args,
11723 tsubst_flags_t complain, tree in_decl,
11724 bool done, bool address_p)
11726 tree expr;
11727 tree scope;
11728 tree name;
11729 bool is_template;
11730 tree template_args;
11731 location_t loc = UNKNOWN_LOCATION;
11733 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11735 /* Figure out what name to look up. */
11736 name = TREE_OPERAND (qualified_id, 1);
11737 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11739 is_template = true;
11740 loc = EXPR_LOCATION (name);
11741 template_args = TREE_OPERAND (name, 1);
11742 if (template_args)
11743 template_args = tsubst_template_args (template_args, args,
11744 complain, in_decl);
11745 name = TREE_OPERAND (name, 0);
11747 else
11749 is_template = false;
11750 template_args = NULL_TREE;
11753 /* Substitute into the qualifying scope. When there are no ARGS, we
11754 are just trying to simplify a non-dependent expression. In that
11755 case the qualifying scope may be dependent, and, in any case,
11756 substituting will not help. */
11757 scope = TREE_OPERAND (qualified_id, 0);
11758 if (args)
11760 scope = tsubst (scope, args, complain, in_decl);
11761 expr = tsubst_copy (name, args, complain, in_decl);
11763 else
11764 expr = name;
11766 if (dependent_scope_p (scope))
11768 if (is_template)
11769 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
11770 return build_qualified_name (NULL_TREE, scope, expr,
11771 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11774 if (!BASELINK_P (name) && !DECL_P (expr))
11776 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11778 /* A BIT_NOT_EXPR is used to represent a destructor. */
11779 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11781 error ("qualifying type %qT does not match destructor name ~%qT",
11782 scope, TREE_OPERAND (expr, 0));
11783 expr = error_mark_node;
11785 else
11786 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11787 /*is_type_p=*/0, false);
11789 else
11790 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11791 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11792 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11794 if (complain & tf_error)
11796 error ("dependent-name %qE is parsed as a non-type, but "
11797 "instantiation yields a type", qualified_id);
11798 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11800 return error_mark_node;
11804 if (DECL_P (expr))
11806 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11807 scope);
11808 /* Remember that there was a reference to this entity. */
11809 mark_used (expr);
11812 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11814 if (complain & tf_error)
11815 qualified_name_lookup_error (scope,
11816 TREE_OPERAND (qualified_id, 1),
11817 expr, input_location);
11818 return error_mark_node;
11821 if (is_template)
11822 expr = lookup_template_function (expr, template_args);
11824 if (expr == error_mark_node && complain & tf_error)
11825 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11826 expr, input_location);
11827 else if (TYPE_P (scope))
11829 expr = (adjust_result_of_qualified_name_lookup
11830 (expr, scope, current_class_type));
11831 expr = (finish_qualified_id_expr
11832 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
11833 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11834 /*template_arg_p=*/false));
11837 /* Expressions do not generally have reference type. */
11838 if (TREE_CODE (expr) != SCOPE_REF
11839 /* However, if we're about to form a pointer-to-member, we just
11840 want the referenced member referenced. */
11841 && TREE_CODE (expr) != OFFSET_REF)
11842 expr = convert_from_reference (expr);
11844 return expr;
11847 /* Like tsubst, but deals with expressions. This function just replaces
11848 template parms; to finish processing the resultant expression, use
11849 tsubst_copy_and_build or tsubst_expr. */
11851 static tree
11852 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11854 enum tree_code code;
11855 tree r;
11857 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11858 return t;
11860 code = TREE_CODE (t);
11862 switch (code)
11864 case PARM_DECL:
11865 r = retrieve_local_specialization (t);
11867 if (r == NULL_TREE)
11869 /* We get here for a use of 'this' in an NSDMI. */
11870 if (DECL_NAME (t) == this_identifier
11871 && at_function_scope_p ()
11872 && DECL_CONSTRUCTOR_P (current_function_decl))
11873 return current_class_ptr;
11875 /* This can happen for a parameter name used later in a function
11876 declaration (such as in a late-specified return type). Just
11877 make a dummy decl, since it's only used for its type. */
11878 gcc_assert (cp_unevaluated_operand != 0);
11879 r = tsubst_decl (t, args, complain);
11880 /* Give it the template pattern as its context; its true context
11881 hasn't been instantiated yet and this is good enough for
11882 mangling. */
11883 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11886 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11887 r = ARGUMENT_PACK_SELECT_ARG (r);
11888 mark_used (r);
11889 return r;
11891 case CONST_DECL:
11893 tree enum_type;
11894 tree v;
11896 if (DECL_TEMPLATE_PARM_P (t))
11897 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11898 /* There is no need to substitute into namespace-scope
11899 enumerators. */
11900 if (DECL_NAMESPACE_SCOPE_P (t))
11901 return t;
11902 /* If ARGS is NULL, then T is known to be non-dependent. */
11903 if (args == NULL_TREE)
11904 return integral_constant_value (t);
11906 /* Unfortunately, we cannot just call lookup_name here.
11907 Consider:
11909 template <int I> int f() {
11910 enum E { a = I };
11911 struct S { void g() { E e = a; } };
11914 When we instantiate f<7>::S::g(), say, lookup_name is not
11915 clever enough to find f<7>::a. */
11916 enum_type
11917 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11918 /*entering_scope=*/0);
11920 for (v = TYPE_VALUES (enum_type);
11921 v != NULL_TREE;
11922 v = TREE_CHAIN (v))
11923 if (TREE_PURPOSE (v) == DECL_NAME (t))
11924 return TREE_VALUE (v);
11926 /* We didn't find the name. That should never happen; if
11927 name-lookup found it during preliminary parsing, we
11928 should find it again here during instantiation. */
11929 gcc_unreachable ();
11931 return t;
11933 case FIELD_DECL:
11934 if (DECL_CONTEXT (t))
11936 tree ctx;
11938 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11939 /*entering_scope=*/1);
11940 if (ctx != DECL_CONTEXT (t))
11942 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11943 if (!r)
11945 if (complain & tf_error)
11946 error ("using invalid field %qD", t);
11947 return error_mark_node;
11949 return r;
11953 return t;
11955 case VAR_DECL:
11956 case FUNCTION_DECL:
11957 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11958 || local_variable_p (t))
11959 t = tsubst (t, args, complain, in_decl);
11960 mark_used (t);
11961 return t;
11963 case NAMESPACE_DECL:
11964 return t;
11966 case OVERLOAD:
11967 /* An OVERLOAD will always be a non-dependent overload set; an
11968 overload set from function scope will just be represented with an
11969 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11970 gcc_assert (!uses_template_parms (t));
11971 return t;
11973 case BASELINK:
11974 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11976 case TEMPLATE_DECL:
11977 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11978 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11979 args, complain, in_decl);
11980 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11981 return tsubst (t, args, complain, in_decl);
11982 else if (DECL_CLASS_SCOPE_P (t)
11983 && uses_template_parms (DECL_CONTEXT (t)))
11985 /* Template template argument like the following example need
11986 special treatment:
11988 template <template <class> class TT> struct C {};
11989 template <class T> struct D {
11990 template <class U> struct E {};
11991 C<E> c; // #1
11993 D<int> d; // #2
11995 We are processing the template argument `E' in #1 for
11996 the template instantiation #2. Originally, `E' is a
11997 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11998 have to substitute this with one having context `D<int>'. */
12000 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12001 return lookup_field (context, DECL_NAME(t), 0, false);
12003 else
12004 /* Ordinary template template argument. */
12005 return t;
12007 case CAST_EXPR:
12008 case REINTERPRET_CAST_EXPR:
12009 case CONST_CAST_EXPR:
12010 case STATIC_CAST_EXPR:
12011 case DYNAMIC_CAST_EXPR:
12012 case IMPLICIT_CONV_EXPR:
12013 case CONVERT_EXPR:
12014 case NOP_EXPR:
12015 return build1
12016 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12017 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12019 case SIZEOF_EXPR:
12020 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12023 tree expanded;
12024 int len = 0;
12026 ++cp_unevaluated_operand;
12027 ++c_inhibit_evaluation_warnings;
12028 /* We only want to compute the number of arguments. */
12029 expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
12030 complain, in_decl);
12031 --cp_unevaluated_operand;
12032 --c_inhibit_evaluation_warnings;
12034 if (TREE_CODE (expanded) == TREE_VEC)
12035 len = TREE_VEC_LENGTH (expanded);
12037 if (expanded == error_mark_node)
12038 return error_mark_node;
12039 else if (PACK_EXPANSION_P (expanded)
12040 || (TREE_CODE (expanded) == TREE_VEC
12041 && len > 0
12042 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12044 if (TREE_CODE (expanded) == TREE_VEC)
12045 expanded = TREE_VEC_ELT (expanded, len - 1);
12047 if (TYPE_P (expanded))
12048 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12049 complain & tf_error);
12050 else
12051 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12052 complain & tf_error);
12054 else
12055 return build_int_cst (size_type_node, len);
12057 /* Fall through */
12059 case INDIRECT_REF:
12060 case NEGATE_EXPR:
12061 case TRUTH_NOT_EXPR:
12062 case BIT_NOT_EXPR:
12063 case ADDR_EXPR:
12064 case UNARY_PLUS_EXPR: /* Unary + */
12065 case ALIGNOF_EXPR:
12066 case AT_ENCODE_EXPR:
12067 case ARROW_EXPR:
12068 case THROW_EXPR:
12069 case TYPEID_EXPR:
12070 case REALPART_EXPR:
12071 case IMAGPART_EXPR:
12072 return build1
12073 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12074 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12076 case COMPONENT_REF:
12078 tree object;
12079 tree name;
12081 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12082 name = TREE_OPERAND (t, 1);
12083 if (TREE_CODE (name) == BIT_NOT_EXPR)
12085 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12086 complain, in_decl);
12087 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12089 else if (TREE_CODE (name) == SCOPE_REF
12090 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12092 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12093 complain, in_decl);
12094 name = TREE_OPERAND (name, 1);
12095 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12096 complain, in_decl);
12097 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12098 name = build_qualified_name (/*type=*/NULL_TREE,
12099 base, name,
12100 /*template_p=*/false);
12102 else if (BASELINK_P (name))
12103 name = tsubst_baselink (name,
12104 non_reference (TREE_TYPE (object)),
12105 args, complain,
12106 in_decl);
12107 else
12108 name = tsubst_copy (name, args, complain, in_decl);
12109 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12112 case PLUS_EXPR:
12113 case MINUS_EXPR:
12114 case MULT_EXPR:
12115 case TRUNC_DIV_EXPR:
12116 case CEIL_DIV_EXPR:
12117 case FLOOR_DIV_EXPR:
12118 case ROUND_DIV_EXPR:
12119 case EXACT_DIV_EXPR:
12120 case BIT_AND_EXPR:
12121 case BIT_IOR_EXPR:
12122 case BIT_XOR_EXPR:
12123 case TRUNC_MOD_EXPR:
12124 case FLOOR_MOD_EXPR:
12125 case TRUTH_ANDIF_EXPR:
12126 case TRUTH_ORIF_EXPR:
12127 case TRUTH_AND_EXPR:
12128 case TRUTH_OR_EXPR:
12129 case RSHIFT_EXPR:
12130 case LSHIFT_EXPR:
12131 case RROTATE_EXPR:
12132 case LROTATE_EXPR:
12133 case EQ_EXPR:
12134 case NE_EXPR:
12135 case MAX_EXPR:
12136 case MIN_EXPR:
12137 case LE_EXPR:
12138 case GE_EXPR:
12139 case LT_EXPR:
12140 case GT_EXPR:
12141 case COMPOUND_EXPR:
12142 case DOTSTAR_EXPR:
12143 case MEMBER_REF:
12144 case PREDECREMENT_EXPR:
12145 case PREINCREMENT_EXPR:
12146 case POSTDECREMENT_EXPR:
12147 case POSTINCREMENT_EXPR:
12148 return build_nt
12149 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12150 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12152 case SCOPE_REF:
12153 return build_qualified_name (/*type=*/NULL_TREE,
12154 tsubst_copy (TREE_OPERAND (t, 0),
12155 args, complain, in_decl),
12156 tsubst_copy (TREE_OPERAND (t, 1),
12157 args, complain, in_decl),
12158 QUALIFIED_NAME_IS_TEMPLATE (t));
12160 case ARRAY_REF:
12161 return build_nt
12162 (ARRAY_REF,
12163 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12164 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12165 NULL_TREE, NULL_TREE);
12167 case CALL_EXPR:
12169 int n = VL_EXP_OPERAND_LENGTH (t);
12170 tree result = build_vl_exp (CALL_EXPR, n);
12171 int i;
12172 for (i = 0; i < n; i++)
12173 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12174 complain, in_decl);
12175 return result;
12178 case COND_EXPR:
12179 case MODOP_EXPR:
12180 case PSEUDO_DTOR_EXPR:
12182 r = build_nt
12183 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12184 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12185 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12186 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12187 return r;
12190 case NEW_EXPR:
12192 r = build_nt
12193 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12194 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12195 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12196 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12197 return r;
12200 case DELETE_EXPR:
12202 r = build_nt
12203 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12204 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12205 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12206 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12207 return r;
12210 case TEMPLATE_ID_EXPR:
12212 /* Substituted template arguments */
12213 tree fn = TREE_OPERAND (t, 0);
12214 tree targs = TREE_OPERAND (t, 1);
12216 fn = tsubst_copy (fn, args, complain, in_decl);
12217 if (targs)
12218 targs = tsubst_template_args (targs, args, complain, in_decl);
12220 return lookup_template_function (fn, targs);
12223 case TREE_LIST:
12225 tree purpose, value, chain;
12227 if (t == void_list_node)
12228 return t;
12230 purpose = TREE_PURPOSE (t);
12231 if (purpose)
12232 purpose = tsubst_copy (purpose, args, complain, in_decl);
12233 value = TREE_VALUE (t);
12234 if (value)
12235 value = tsubst_copy (value, args, complain, in_decl);
12236 chain = TREE_CHAIN (t);
12237 if (chain && chain != void_type_node)
12238 chain = tsubst_copy (chain, args, complain, in_decl);
12239 if (purpose == TREE_PURPOSE (t)
12240 && value == TREE_VALUE (t)
12241 && chain == TREE_CHAIN (t))
12242 return t;
12243 return tree_cons (purpose, value, chain);
12246 case RECORD_TYPE:
12247 case UNION_TYPE:
12248 case ENUMERAL_TYPE:
12249 case INTEGER_TYPE:
12250 case TEMPLATE_TYPE_PARM:
12251 case TEMPLATE_TEMPLATE_PARM:
12252 case BOUND_TEMPLATE_TEMPLATE_PARM:
12253 case TEMPLATE_PARM_INDEX:
12254 case POINTER_TYPE:
12255 case REFERENCE_TYPE:
12256 case OFFSET_TYPE:
12257 case FUNCTION_TYPE:
12258 case METHOD_TYPE:
12259 case ARRAY_TYPE:
12260 case TYPENAME_TYPE:
12261 case UNBOUND_CLASS_TEMPLATE:
12262 case TYPEOF_TYPE:
12263 case DECLTYPE_TYPE:
12264 case TYPE_DECL:
12265 return tsubst (t, args, complain, in_decl);
12267 case IDENTIFIER_NODE:
12268 if (IDENTIFIER_TYPENAME_P (t))
12270 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12271 return mangle_conv_op_name_for_type (new_type);
12273 else
12274 return t;
12276 case CONSTRUCTOR:
12277 /* This is handled by tsubst_copy_and_build. */
12278 gcc_unreachable ();
12280 case VA_ARG_EXPR:
12281 return build_x_va_arg (EXPR_LOCATION (t),
12282 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12283 in_decl),
12284 tsubst (TREE_TYPE (t), args, complain, in_decl));
12286 case CLEANUP_POINT_EXPR:
12287 /* We shouldn't have built any of these during initial template
12288 generation. Instead, they should be built during instantiation
12289 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12290 gcc_unreachable ();
12292 case OFFSET_REF:
12293 r = build2
12294 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12295 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12296 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12297 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12298 mark_used (TREE_OPERAND (r, 1));
12299 return r;
12301 case EXPR_PACK_EXPANSION:
12302 error ("invalid use of pack expansion expression");
12303 return error_mark_node;
12305 case NONTYPE_ARGUMENT_PACK:
12306 error ("use %<...%> to expand argument pack");
12307 return error_mark_node;
12309 case INTEGER_CST:
12310 case REAL_CST:
12311 case STRING_CST:
12312 case COMPLEX_CST:
12314 /* Instantiate any typedefs in the type. */
12315 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12316 r = fold_convert (type, t);
12317 gcc_assert (TREE_CODE (r) == code);
12318 return r;
12321 case PTRMEM_CST:
12322 /* These can sometimes show up in a partial instantiation, but never
12323 involve template parms. */
12324 gcc_assert (!uses_template_parms (t));
12325 return t;
12327 default:
12328 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12329 gcc_checking_assert (false);
12330 return t;
12334 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12336 static tree
12337 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12338 tree in_decl)
12340 tree new_clauses = NULL, nc, oc;
12342 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12344 nc = copy_node (oc);
12345 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12346 new_clauses = nc;
12348 switch (OMP_CLAUSE_CODE (nc))
12350 case OMP_CLAUSE_LASTPRIVATE:
12351 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12353 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12354 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12355 in_decl, /*integral_constant_expression_p=*/false);
12356 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12357 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12359 /* FALLTHRU */
12360 case OMP_CLAUSE_PRIVATE:
12361 case OMP_CLAUSE_SHARED:
12362 case OMP_CLAUSE_FIRSTPRIVATE:
12363 case OMP_CLAUSE_REDUCTION:
12364 case OMP_CLAUSE_COPYIN:
12365 case OMP_CLAUSE_COPYPRIVATE:
12366 case OMP_CLAUSE_IF:
12367 case OMP_CLAUSE_NUM_THREADS:
12368 case OMP_CLAUSE_SCHEDULE:
12369 case OMP_CLAUSE_COLLAPSE:
12370 case OMP_CLAUSE_FINAL:
12371 OMP_CLAUSE_OPERAND (nc, 0)
12372 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12373 in_decl, /*integral_constant_expression_p=*/false);
12374 break;
12375 case OMP_CLAUSE_NOWAIT:
12376 case OMP_CLAUSE_ORDERED:
12377 case OMP_CLAUSE_DEFAULT:
12378 case OMP_CLAUSE_UNTIED:
12379 case OMP_CLAUSE_MERGEABLE:
12380 break;
12381 default:
12382 gcc_unreachable ();
12386 return finish_omp_clauses (nreverse (new_clauses));
12389 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12391 static tree
12392 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12393 tree in_decl)
12395 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12397 tree purpose, value, chain;
12399 if (t == NULL)
12400 return t;
12402 if (TREE_CODE (t) != TREE_LIST)
12403 return tsubst_copy_and_build (t, args, complain, in_decl,
12404 /*function_p=*/false,
12405 /*integral_constant_expression_p=*/false);
12407 if (t == void_list_node)
12408 return t;
12410 purpose = TREE_PURPOSE (t);
12411 if (purpose)
12412 purpose = RECUR (purpose);
12413 value = TREE_VALUE (t);
12414 if (value)
12416 if (TREE_CODE (value) != LABEL_DECL)
12417 value = RECUR (value);
12418 else
12420 value = lookup_label (DECL_NAME (value));
12421 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12422 TREE_USED (value) = 1;
12425 chain = TREE_CHAIN (t);
12426 if (chain && chain != void_type_node)
12427 chain = RECUR (chain);
12428 return tree_cons (purpose, value, chain);
12429 #undef RECUR
12432 /* Substitute one OMP_FOR iterator. */
12434 static void
12435 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12436 tree condv, tree incrv, tree *clauses,
12437 tree args, tsubst_flags_t complain, tree in_decl,
12438 bool integral_constant_expression_p)
12440 #define RECUR(NODE) \
12441 tsubst_expr ((NODE), args, complain, in_decl, \
12442 integral_constant_expression_p)
12443 tree decl, init, cond, incr;
12444 bool init_decl;
12446 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12447 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12448 decl = TREE_OPERAND (init, 0);
12449 init = TREE_OPERAND (init, 1);
12450 /* Do this before substituting into decl to handle 'auto'. */
12451 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
12452 init = RECUR (init);
12453 decl = RECUR (decl);
12454 if (init_decl)
12456 gcc_assert (!processing_template_decl);
12457 init = DECL_INITIAL (decl);
12458 DECL_INITIAL (decl) = NULL_TREE;
12461 gcc_assert (!type_dependent_expression_p (decl));
12463 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12465 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12466 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12467 if (TREE_CODE (incr) == MODIFY_EXPR)
12468 incr = build_x_modify_expr (EXPR_LOCATION (incr),
12469 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12470 RECUR (TREE_OPERAND (incr, 1)),
12471 complain);
12472 else
12473 incr = RECUR (incr);
12474 TREE_VEC_ELT (declv, i) = decl;
12475 TREE_VEC_ELT (initv, i) = init;
12476 TREE_VEC_ELT (condv, i) = cond;
12477 TREE_VEC_ELT (incrv, i) = incr;
12478 return;
12481 if (init && !init_decl)
12483 tree c;
12484 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12486 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12487 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12488 && OMP_CLAUSE_DECL (c) == decl)
12489 break;
12490 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12491 && OMP_CLAUSE_DECL (c) == decl)
12492 error ("iteration variable %qD should not be firstprivate", decl);
12493 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12494 && OMP_CLAUSE_DECL (c) == decl)
12495 error ("iteration variable %qD should not be reduction", decl);
12497 if (c == NULL)
12499 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12500 OMP_CLAUSE_DECL (c) = decl;
12501 c = finish_omp_clauses (c);
12502 if (c)
12504 OMP_CLAUSE_CHAIN (c) = *clauses;
12505 *clauses = c;
12509 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12510 if (COMPARISON_CLASS_P (cond))
12511 cond = build2 (TREE_CODE (cond), boolean_type_node,
12512 RECUR (TREE_OPERAND (cond, 0)),
12513 RECUR (TREE_OPERAND (cond, 1)));
12514 else
12515 cond = RECUR (cond);
12516 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12517 switch (TREE_CODE (incr))
12519 case PREINCREMENT_EXPR:
12520 case PREDECREMENT_EXPR:
12521 case POSTINCREMENT_EXPR:
12522 case POSTDECREMENT_EXPR:
12523 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12524 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12525 break;
12526 case MODIFY_EXPR:
12527 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12528 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12530 tree rhs = TREE_OPERAND (incr, 1);
12531 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12532 RECUR (TREE_OPERAND (incr, 0)),
12533 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12534 RECUR (TREE_OPERAND (rhs, 0)),
12535 RECUR (TREE_OPERAND (rhs, 1))));
12537 else
12538 incr = RECUR (incr);
12539 break;
12540 case MODOP_EXPR:
12541 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12542 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12544 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12545 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12546 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12547 TREE_TYPE (decl), lhs,
12548 RECUR (TREE_OPERAND (incr, 2))));
12550 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12551 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12552 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12554 tree rhs = TREE_OPERAND (incr, 2);
12555 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12556 RECUR (TREE_OPERAND (incr, 0)),
12557 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12558 RECUR (TREE_OPERAND (rhs, 0)),
12559 RECUR (TREE_OPERAND (rhs, 1))));
12561 else
12562 incr = RECUR (incr);
12563 break;
12564 default:
12565 incr = RECUR (incr);
12566 break;
12569 TREE_VEC_ELT (declv, i) = decl;
12570 TREE_VEC_ELT (initv, i) = init;
12571 TREE_VEC_ELT (condv, i) = cond;
12572 TREE_VEC_ELT (incrv, i) = incr;
12573 #undef RECUR
12576 /* Like tsubst_copy for expressions, etc. but also does semantic
12577 processing. */
12579 static tree
12580 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12581 bool integral_constant_expression_p)
12583 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12584 #define RECUR(NODE) \
12585 tsubst_expr ((NODE), args, complain, in_decl, \
12586 integral_constant_expression_p)
12588 tree stmt, tmp;
12589 tree r;
12590 location_t loc;
12592 if (t == NULL_TREE || t == error_mark_node)
12593 return t;
12595 loc = input_location;
12596 if (EXPR_HAS_LOCATION (t))
12597 input_location = EXPR_LOCATION (t);
12598 if (STATEMENT_CODE_P (TREE_CODE (t)))
12599 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12601 switch (TREE_CODE (t))
12603 case STATEMENT_LIST:
12605 tree_stmt_iterator i;
12606 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12607 RECUR (tsi_stmt (i));
12608 break;
12611 case CTOR_INITIALIZER:
12612 finish_mem_initializers (tsubst_initializer_list
12613 (TREE_OPERAND (t, 0), args));
12614 break;
12616 case RETURN_EXPR:
12617 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12618 break;
12620 case EXPR_STMT:
12621 tmp = RECUR (EXPR_STMT_EXPR (t));
12622 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12623 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12624 else
12625 finish_expr_stmt (tmp);
12626 break;
12628 case USING_STMT:
12629 do_using_directive (USING_STMT_NAMESPACE (t));
12630 break;
12632 case DECL_EXPR:
12634 tree decl, pattern_decl;
12635 tree init;
12637 pattern_decl = decl = DECL_EXPR_DECL (t);
12638 if (TREE_CODE (decl) == LABEL_DECL)
12639 finish_label_decl (DECL_NAME (decl));
12640 else if (TREE_CODE (decl) == USING_DECL)
12642 tree scope = USING_DECL_SCOPE (decl);
12643 tree name = DECL_NAME (decl);
12644 tree decl;
12646 scope = tsubst (scope, args, complain, in_decl);
12647 decl = lookup_qualified_name (scope, name,
12648 /*is_type_p=*/false,
12649 /*complain=*/false);
12650 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12651 qualified_name_lookup_error (scope, name, decl, input_location);
12652 else
12653 do_local_using_decl (decl, scope, name);
12655 else
12657 init = DECL_INITIAL (decl);
12658 decl = tsubst (decl, args, complain, in_decl);
12659 if (decl != error_mark_node)
12661 /* By marking the declaration as instantiated, we avoid
12662 trying to instantiate it. Since instantiate_decl can't
12663 handle local variables, and since we've already done
12664 all that needs to be done, that's the right thing to
12665 do. */
12666 if (TREE_CODE (decl) == VAR_DECL)
12667 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12668 if (TREE_CODE (decl) == VAR_DECL
12669 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12670 /* Anonymous aggregates are a special case. */
12671 finish_anon_union (decl);
12672 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
12674 DECL_CONTEXT (decl) = current_function_decl;
12675 insert_capture_proxy (decl);
12677 else if (DECL_IMPLICIT_TYPEDEF_P (t))
12678 /* We already did a pushtag. */;
12679 else
12681 int const_init = false;
12682 maybe_push_decl (decl);
12683 if (TREE_CODE (decl) == VAR_DECL
12684 && DECL_PRETTY_FUNCTION_P (decl))
12686 /* For __PRETTY_FUNCTION__ we have to adjust the
12687 initializer. */
12688 const char *const name
12689 = cxx_printable_name (current_function_decl, 2);
12690 init = cp_fname_init (name, &TREE_TYPE (decl));
12692 else
12694 tree t = RECUR (init);
12696 if (init && !t)
12698 /* If we had an initializer but it
12699 instantiated to nothing,
12700 value-initialize the object. This will
12701 only occur when the initializer was a
12702 pack expansion where the parameter packs
12703 used in that expansion were of length
12704 zero. */
12705 init = build_value_init (TREE_TYPE (decl),
12706 complain);
12707 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12708 init = get_target_expr_sfinae (init, complain);
12710 else
12711 init = t;
12714 if (TREE_CODE (decl) == VAR_DECL)
12715 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12716 (pattern_decl));
12717 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12722 break;
12725 case FOR_STMT:
12726 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12727 RECUR (FOR_INIT_STMT (t));
12728 finish_for_init_stmt (stmt);
12729 tmp = RECUR (FOR_COND (t));
12730 finish_for_cond (tmp, stmt);
12731 tmp = RECUR (FOR_EXPR (t));
12732 finish_for_expr (tmp, stmt);
12733 RECUR (FOR_BODY (t));
12734 finish_for_stmt (stmt);
12735 break;
12737 case RANGE_FOR_STMT:
12739 tree decl, expr;
12740 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12741 decl = RANGE_FOR_DECL (t);
12742 decl = tsubst (decl, args, complain, in_decl);
12743 maybe_push_decl (decl);
12744 expr = RECUR (RANGE_FOR_EXPR (t));
12745 stmt = cp_convert_range_for (stmt, decl, expr);
12746 RECUR (RANGE_FOR_BODY (t));
12747 finish_for_stmt (stmt);
12749 break;
12751 case WHILE_STMT:
12752 stmt = begin_while_stmt ();
12753 tmp = RECUR (WHILE_COND (t));
12754 finish_while_stmt_cond (tmp, stmt);
12755 RECUR (WHILE_BODY (t));
12756 finish_while_stmt (stmt);
12757 break;
12759 case DO_STMT:
12760 stmt = begin_do_stmt ();
12761 RECUR (DO_BODY (t));
12762 finish_do_body (stmt);
12763 tmp = RECUR (DO_COND (t));
12764 finish_do_stmt (tmp, stmt);
12765 break;
12767 case IF_STMT:
12768 stmt = begin_if_stmt ();
12769 tmp = RECUR (IF_COND (t));
12770 finish_if_stmt_cond (tmp, stmt);
12771 RECUR (THEN_CLAUSE (t));
12772 finish_then_clause (stmt);
12774 if (ELSE_CLAUSE (t))
12776 begin_else_clause (stmt);
12777 RECUR (ELSE_CLAUSE (t));
12778 finish_else_clause (stmt);
12781 finish_if_stmt (stmt);
12782 break;
12784 case BIND_EXPR:
12785 if (BIND_EXPR_BODY_BLOCK (t))
12786 stmt = begin_function_body ();
12787 else
12788 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12789 ? BCS_TRY_BLOCK : 0);
12791 RECUR (BIND_EXPR_BODY (t));
12793 if (BIND_EXPR_BODY_BLOCK (t))
12794 finish_function_body (stmt);
12795 else
12796 finish_compound_stmt (stmt);
12797 break;
12799 case BREAK_STMT:
12800 finish_break_stmt ();
12801 break;
12803 case CONTINUE_STMT:
12804 finish_continue_stmt ();
12805 break;
12807 case SWITCH_STMT:
12808 stmt = begin_switch_stmt ();
12809 tmp = RECUR (SWITCH_STMT_COND (t));
12810 finish_switch_cond (tmp, stmt);
12811 RECUR (SWITCH_STMT_BODY (t));
12812 finish_switch_stmt (stmt);
12813 break;
12815 case CASE_LABEL_EXPR:
12816 finish_case_label (EXPR_LOCATION (t),
12817 RECUR (CASE_LOW (t)),
12818 RECUR (CASE_HIGH (t)));
12819 break;
12821 case LABEL_EXPR:
12823 tree decl = LABEL_EXPR_LABEL (t);
12824 tree label;
12826 label = finish_label_stmt (DECL_NAME (decl));
12827 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12828 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12830 break;
12832 case GOTO_EXPR:
12833 tmp = GOTO_DESTINATION (t);
12834 if (TREE_CODE (tmp) != LABEL_DECL)
12835 /* Computed goto's must be tsubst'd into. On the other hand,
12836 non-computed gotos must not be; the identifier in question
12837 will have no binding. */
12838 tmp = RECUR (tmp);
12839 else
12840 tmp = DECL_NAME (tmp);
12841 finish_goto_stmt (tmp);
12842 break;
12844 case ASM_EXPR:
12845 tmp = finish_asm_stmt
12846 (ASM_VOLATILE_P (t),
12847 RECUR (ASM_STRING (t)),
12848 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12849 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12850 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12851 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12853 tree asm_expr = tmp;
12854 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12855 asm_expr = TREE_OPERAND (asm_expr, 0);
12856 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12858 break;
12860 case TRY_BLOCK:
12861 if (CLEANUP_P (t))
12863 stmt = begin_try_block ();
12864 RECUR (TRY_STMTS (t));
12865 finish_cleanup_try_block (stmt);
12866 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12868 else
12870 tree compound_stmt = NULL_TREE;
12872 if (FN_TRY_BLOCK_P (t))
12873 stmt = begin_function_try_block (&compound_stmt);
12874 else
12875 stmt = begin_try_block ();
12877 RECUR (TRY_STMTS (t));
12879 if (FN_TRY_BLOCK_P (t))
12880 finish_function_try_block (stmt);
12881 else
12882 finish_try_block (stmt);
12884 RECUR (TRY_HANDLERS (t));
12885 if (FN_TRY_BLOCK_P (t))
12886 finish_function_handler_sequence (stmt, compound_stmt);
12887 else
12888 finish_handler_sequence (stmt);
12890 break;
12892 case HANDLER:
12894 tree decl = HANDLER_PARMS (t);
12896 if (decl)
12898 decl = tsubst (decl, args, complain, in_decl);
12899 /* Prevent instantiate_decl from trying to instantiate
12900 this variable. We've already done all that needs to be
12901 done. */
12902 if (decl != error_mark_node)
12903 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12905 stmt = begin_handler ();
12906 finish_handler_parms (decl, stmt);
12907 RECUR (HANDLER_BODY (t));
12908 finish_handler (stmt);
12910 break;
12912 case TAG_DEFN:
12913 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12914 if (CLASS_TYPE_P (tmp))
12916 /* Local classes are not independent templates; they are
12917 instantiated along with their containing function. And this
12918 way we don't have to deal with pushing out of one local class
12919 to instantiate a member of another local class. */
12920 tree fn;
12921 /* Closures are handled by the LAMBDA_EXPR. */
12922 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
12923 complete_type (tmp);
12924 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
12925 if (!DECL_ARTIFICIAL (fn))
12926 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
12928 break;
12930 case STATIC_ASSERT:
12932 tree condition;
12934 ++c_inhibit_evaluation_warnings;
12935 condition =
12936 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12937 args,
12938 complain, in_decl,
12939 /*integral_constant_expression_p=*/true);
12940 --c_inhibit_evaluation_warnings;
12942 finish_static_assert (condition,
12943 STATIC_ASSERT_MESSAGE (t),
12944 STATIC_ASSERT_SOURCE_LOCATION (t),
12945 /*member_p=*/false);
12947 break;
12949 case OMP_PARALLEL:
12950 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
12951 args, complain, in_decl);
12952 stmt = begin_omp_parallel ();
12953 RECUR (OMP_PARALLEL_BODY (t));
12954 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
12955 = OMP_PARALLEL_COMBINED (t);
12956 break;
12958 case OMP_TASK:
12959 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
12960 args, complain, in_decl);
12961 stmt = begin_omp_task ();
12962 RECUR (OMP_TASK_BODY (t));
12963 finish_omp_task (tmp, stmt);
12964 break;
12966 case OMP_FOR:
12968 tree clauses, body, pre_body;
12969 tree declv, initv, condv, incrv;
12970 int i;
12972 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
12973 args, complain, in_decl);
12974 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12975 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12976 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12977 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12979 stmt = begin_omp_structured_block ();
12981 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
12982 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
12983 &clauses, args, complain, in_decl,
12984 integral_constant_expression_p);
12986 pre_body = push_stmt_list ();
12987 RECUR (OMP_FOR_PRE_BODY (t));
12988 pre_body = pop_stmt_list (pre_body);
12990 body = push_stmt_list ();
12991 RECUR (OMP_FOR_BODY (t));
12992 body = pop_stmt_list (body);
12994 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12995 body, pre_body, clauses);
12997 add_stmt (finish_omp_structured_block (stmt));
12999 break;
13001 case OMP_SECTIONS:
13002 case OMP_SINGLE:
13003 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
13004 stmt = push_stmt_list ();
13005 RECUR (OMP_BODY (t));
13006 stmt = pop_stmt_list (stmt);
13008 t = copy_node (t);
13009 OMP_BODY (t) = stmt;
13010 OMP_CLAUSES (t) = tmp;
13011 add_stmt (t);
13012 break;
13014 case OMP_SECTION:
13015 case OMP_CRITICAL:
13016 case OMP_MASTER:
13017 case OMP_ORDERED:
13018 stmt = push_stmt_list ();
13019 RECUR (OMP_BODY (t));
13020 stmt = pop_stmt_list (stmt);
13022 t = copy_node (t);
13023 OMP_BODY (t) = stmt;
13024 add_stmt (t);
13025 break;
13027 case OMP_ATOMIC:
13028 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13029 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13031 tree op1 = TREE_OPERAND (t, 1);
13032 tree rhs1 = NULL_TREE;
13033 tree lhs, rhs;
13034 if (TREE_CODE (op1) == COMPOUND_EXPR)
13036 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13037 op1 = TREE_OPERAND (op1, 1);
13039 lhs = RECUR (TREE_OPERAND (op1, 0));
13040 rhs = RECUR (TREE_OPERAND (op1, 1));
13041 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13042 NULL_TREE, NULL_TREE, rhs1);
13044 else
13046 tree op1 = TREE_OPERAND (t, 1);
13047 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13048 tree rhs1 = NULL_TREE;
13049 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13050 enum tree_code opcode = NOP_EXPR;
13051 if (code == OMP_ATOMIC_READ)
13053 v = RECUR (TREE_OPERAND (op1, 0));
13054 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13056 else if (code == OMP_ATOMIC_CAPTURE_OLD
13057 || code == OMP_ATOMIC_CAPTURE_NEW)
13059 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13060 v = RECUR (TREE_OPERAND (op1, 0));
13061 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13062 if (TREE_CODE (op11) == COMPOUND_EXPR)
13064 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13065 op11 = TREE_OPERAND (op11, 1);
13067 lhs = RECUR (TREE_OPERAND (op11, 0));
13068 rhs = RECUR (TREE_OPERAND (op11, 1));
13069 opcode = TREE_CODE (op11);
13071 else
13073 code = OMP_ATOMIC;
13074 lhs = RECUR (TREE_OPERAND (op1, 0));
13075 rhs = RECUR (TREE_OPERAND (op1, 1));
13077 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
13079 break;
13081 case TRANSACTION_EXPR:
13083 int flags = 0;
13084 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13085 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13087 if (TRANSACTION_EXPR_IS_STMT (t))
13089 tree body = TRANSACTION_EXPR_BODY (t);
13090 tree noex = NULL_TREE;
13091 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13093 noex = MUST_NOT_THROW_COND (body);
13094 if (noex == NULL_TREE)
13095 noex = boolean_true_node;
13096 body = TREE_OPERAND (body, 0);
13098 stmt = begin_transaction_stmt (input_location, NULL, flags);
13099 RECUR (body);
13100 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13102 else
13104 stmt = build_transaction_expr (EXPR_LOCATION (t),
13105 RECUR (TRANSACTION_EXPR_BODY (t)),
13106 flags, NULL_TREE);
13107 RETURN (stmt);
13110 break;
13112 case MUST_NOT_THROW_EXPR:
13113 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13114 RECUR (MUST_NOT_THROW_COND (t))));
13116 case EXPR_PACK_EXPANSION:
13117 error ("invalid use of pack expansion expression");
13118 RETURN (error_mark_node);
13120 case NONTYPE_ARGUMENT_PACK:
13121 error ("use %<...%> to expand argument pack");
13122 RETURN (error_mark_node);
13124 case COMPOUND_EXPR:
13125 tmp = RECUR (TREE_OPERAND (t, 0));
13126 if (tmp == NULL_TREE)
13127 /* If the first operand was a statement, we're done with it. */
13128 RETURN (RECUR (TREE_OPERAND (t, 1)));
13129 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13130 RECUR (TREE_OPERAND (t, 1)),
13131 complain));
13133 default:
13134 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13136 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13137 /*function_p=*/false,
13138 integral_constant_expression_p));
13141 RETURN (NULL_TREE);
13142 out:
13143 input_location = loc;
13144 return r;
13145 #undef RECUR
13146 #undef RETURN
13149 /* T is a postfix-expression that is not being used in a function
13150 call. Return the substituted version of T. */
13152 static tree
13153 tsubst_non_call_postfix_expression (tree t, tree args,
13154 tsubst_flags_t complain,
13155 tree in_decl)
13157 if (TREE_CODE (t) == SCOPE_REF)
13158 t = tsubst_qualified_id (t, args, complain, in_decl,
13159 /*done=*/false, /*address_p=*/false);
13160 else
13161 t = tsubst_copy_and_build (t, args, complain, in_decl,
13162 /*function_p=*/false,
13163 /*integral_constant_expression_p=*/false);
13165 return t;
13168 /* Like tsubst but deals with expressions and performs semantic
13169 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13171 tree
13172 tsubst_copy_and_build (tree t,
13173 tree args,
13174 tsubst_flags_t complain,
13175 tree in_decl,
13176 bool function_p,
13177 bool integral_constant_expression_p)
13179 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13180 #define RECUR(NODE) \
13181 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13182 /*function_p=*/false, \
13183 integral_constant_expression_p)
13185 tree retval, op1;
13186 location_t loc;
13188 if (t == NULL_TREE || t == error_mark_node)
13189 return t;
13191 loc = input_location;
13192 if (EXPR_HAS_LOCATION (t))
13193 input_location = EXPR_LOCATION (t);
13195 switch (TREE_CODE (t))
13197 case USING_DECL:
13198 t = DECL_NAME (t);
13199 /* Fall through. */
13200 case IDENTIFIER_NODE:
13202 tree decl;
13203 cp_id_kind idk;
13204 bool non_integral_constant_expression_p;
13205 const char *error_msg;
13207 if (IDENTIFIER_TYPENAME_P (t))
13209 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13210 t = mangle_conv_op_name_for_type (new_type);
13213 /* Look up the name. */
13214 decl = lookup_name (t);
13216 /* By convention, expressions use ERROR_MARK_NODE to indicate
13217 failure, not NULL_TREE. */
13218 if (decl == NULL_TREE)
13219 decl = error_mark_node;
13221 decl = finish_id_expression (t, decl, NULL_TREE,
13222 &idk,
13223 integral_constant_expression_p,
13224 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
13225 &non_integral_constant_expression_p,
13226 /*template_p=*/false,
13227 /*done=*/true,
13228 /*address_p=*/false,
13229 /*template_arg_p=*/false,
13230 &error_msg,
13231 input_location);
13232 if (error_msg)
13233 error (error_msg);
13234 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
13236 if (complain & tf_error)
13237 unqualified_name_lookup_error (decl);
13238 decl = error_mark_node;
13240 RETURN (decl);
13243 case TEMPLATE_ID_EXPR:
13245 tree object;
13246 tree templ = RECUR (TREE_OPERAND (t, 0));
13247 tree targs = TREE_OPERAND (t, 1);
13249 if (targs)
13250 targs = tsubst_template_args (targs, args, complain, in_decl);
13252 if (TREE_CODE (templ) == COMPONENT_REF)
13254 object = TREE_OPERAND (templ, 0);
13255 templ = TREE_OPERAND (templ, 1);
13257 else
13258 object = NULL_TREE;
13259 templ = lookup_template_function (templ, targs);
13261 if (object)
13262 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13263 object, templ, NULL_TREE));
13264 else
13265 RETURN (baselink_for_fns (templ));
13268 case INDIRECT_REF:
13270 tree r = RECUR (TREE_OPERAND (t, 0));
13272 if (REFERENCE_REF_P (t))
13274 /* A type conversion to reference type will be enclosed in
13275 such an indirect ref, but the substitution of the cast
13276 will have also added such an indirect ref. */
13277 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13278 r = convert_from_reference (r);
13280 else
13281 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, complain);
13282 RETURN (r);
13285 case NOP_EXPR:
13286 RETURN (build_nop
13287 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13288 RECUR (TREE_OPERAND (t, 0))));
13290 case IMPLICIT_CONV_EXPR:
13292 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13293 tree expr = RECUR (TREE_OPERAND (t, 0));
13294 int flags = LOOKUP_IMPLICIT;
13295 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13296 flags = LOOKUP_NORMAL;
13297 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13298 flags));
13301 case CONVERT_EXPR:
13302 RETURN (build1
13303 (CONVERT_EXPR,
13304 tsubst (TREE_TYPE (t), args, complain, in_decl),
13305 RECUR (TREE_OPERAND (t, 0))));
13307 case CAST_EXPR:
13308 case REINTERPRET_CAST_EXPR:
13309 case CONST_CAST_EXPR:
13310 case DYNAMIC_CAST_EXPR:
13311 case STATIC_CAST_EXPR:
13313 tree type;
13314 tree op, r = NULL_TREE;
13316 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13317 if (integral_constant_expression_p
13318 && !cast_valid_in_integral_constant_expression_p (type))
13320 if (complain & tf_error)
13321 error ("a cast to a type other than an integral or "
13322 "enumeration type cannot appear in a constant-expression");
13323 RETURN (error_mark_node);
13326 op = RECUR (TREE_OPERAND (t, 0));
13328 ++c_inhibit_evaluation_warnings;
13329 switch (TREE_CODE (t))
13331 case CAST_EXPR:
13332 r = build_functional_cast (type, op, complain);
13333 break;
13334 case REINTERPRET_CAST_EXPR:
13335 r = build_reinterpret_cast (type, op, complain);
13336 break;
13337 case CONST_CAST_EXPR:
13338 r = build_const_cast (type, op, complain);
13339 break;
13340 case DYNAMIC_CAST_EXPR:
13341 r = build_dynamic_cast (type, op, complain);
13342 break;
13343 case STATIC_CAST_EXPR:
13344 r = build_static_cast (type, op, complain);
13345 break;
13346 default:
13347 gcc_unreachable ();
13349 --c_inhibit_evaluation_warnings;
13351 RETURN (r);
13354 case POSTDECREMENT_EXPR:
13355 case POSTINCREMENT_EXPR:
13356 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13357 args, complain, in_decl);
13358 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, complain));
13360 case PREDECREMENT_EXPR:
13361 case PREINCREMENT_EXPR:
13362 case NEGATE_EXPR:
13363 case BIT_NOT_EXPR:
13364 case ABS_EXPR:
13365 case TRUTH_NOT_EXPR:
13366 case UNARY_PLUS_EXPR: /* Unary + */
13367 case REALPART_EXPR:
13368 case IMAGPART_EXPR:
13369 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
13370 RECUR (TREE_OPERAND (t, 0)), complain));
13372 case FIX_TRUNC_EXPR:
13373 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
13374 0, complain));
13376 case ADDR_EXPR:
13377 op1 = TREE_OPERAND (t, 0);
13378 if (TREE_CODE (op1) == LABEL_DECL)
13379 RETURN (finish_label_address_expr (DECL_NAME (op1),
13380 EXPR_LOCATION (op1)));
13381 if (TREE_CODE (op1) == SCOPE_REF)
13382 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13383 /*done=*/true, /*address_p=*/true);
13384 else
13385 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13386 in_decl);
13387 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, complain));
13389 case PLUS_EXPR:
13390 case MINUS_EXPR:
13391 case MULT_EXPR:
13392 case TRUNC_DIV_EXPR:
13393 case CEIL_DIV_EXPR:
13394 case FLOOR_DIV_EXPR:
13395 case ROUND_DIV_EXPR:
13396 case EXACT_DIV_EXPR:
13397 case BIT_AND_EXPR:
13398 case BIT_IOR_EXPR:
13399 case BIT_XOR_EXPR:
13400 case TRUNC_MOD_EXPR:
13401 case FLOOR_MOD_EXPR:
13402 case TRUTH_ANDIF_EXPR:
13403 case TRUTH_ORIF_EXPR:
13404 case TRUTH_AND_EXPR:
13405 case TRUTH_OR_EXPR:
13406 case RSHIFT_EXPR:
13407 case LSHIFT_EXPR:
13408 case RROTATE_EXPR:
13409 case LROTATE_EXPR:
13410 case EQ_EXPR:
13411 case NE_EXPR:
13412 case MAX_EXPR:
13413 case MIN_EXPR:
13414 case LE_EXPR:
13415 case GE_EXPR:
13416 case LT_EXPR:
13417 case GT_EXPR:
13418 case MEMBER_REF:
13419 case DOTSTAR_EXPR:
13421 tree r;
13423 ++c_inhibit_evaluation_warnings;
13425 r = build_x_binary_op
13426 (input_location, TREE_CODE (t),
13427 RECUR (TREE_OPERAND (t, 0)),
13428 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13429 ? ERROR_MARK
13430 : TREE_CODE (TREE_OPERAND (t, 0))),
13431 RECUR (TREE_OPERAND (t, 1)),
13432 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13433 ? ERROR_MARK
13434 : TREE_CODE (TREE_OPERAND (t, 1))),
13435 /*overload=*/NULL,
13436 complain);
13437 if (EXPR_P (r) && TREE_NO_WARNING (t))
13438 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13440 --c_inhibit_evaluation_warnings;
13442 RETURN (r);
13445 case SCOPE_REF:
13446 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13447 /*address_p=*/false));
13448 case ARRAY_REF:
13449 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13450 args, complain, in_decl);
13451 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
13452 RECUR (TREE_OPERAND (t, 1)), complain));
13454 case SIZEOF_EXPR:
13455 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13456 RETURN (tsubst_copy (t, args, complain, in_decl));
13457 /* Fall through */
13459 case ALIGNOF_EXPR:
13460 op1 = TREE_OPERAND (t, 0);
13461 if (!args)
13463 /* When there are no ARGS, we are trying to evaluate a
13464 non-dependent expression from the parser. Trying to do
13465 the substitutions may not work. */
13466 if (!TYPE_P (op1))
13467 op1 = TREE_TYPE (op1);
13469 else
13471 ++cp_unevaluated_operand;
13472 ++c_inhibit_evaluation_warnings;
13473 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13474 /*function_p=*/false,
13475 /*integral_constant_expression_p=*/false);
13476 --cp_unevaluated_operand;
13477 --c_inhibit_evaluation_warnings;
13479 if (TYPE_P (op1))
13480 RETURN (cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13481 complain & tf_error));
13482 else
13483 RETURN (cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13484 complain & tf_error));
13486 case AT_ENCODE_EXPR:
13488 op1 = TREE_OPERAND (t, 0);
13489 ++cp_unevaluated_operand;
13490 ++c_inhibit_evaluation_warnings;
13491 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13492 /*function_p=*/false,
13493 /*integral_constant_expression_p=*/false);
13494 --cp_unevaluated_operand;
13495 --c_inhibit_evaluation_warnings;
13496 RETURN (objc_build_encode_expr (op1));
13499 case NOEXCEPT_EXPR:
13500 op1 = TREE_OPERAND (t, 0);
13501 ++cp_unevaluated_operand;
13502 ++c_inhibit_evaluation_warnings;
13503 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13504 /*function_p=*/false,
13505 /*integral_constant_expression_p=*/false);
13506 --cp_unevaluated_operand;
13507 --c_inhibit_evaluation_warnings;
13508 RETURN (finish_noexcept_expr (op1, complain));
13510 case MODOP_EXPR:
13512 tree r = build_x_modify_expr
13513 (EXPR_LOCATION (t),
13514 RECUR (TREE_OPERAND (t, 0)),
13515 TREE_CODE (TREE_OPERAND (t, 1)),
13516 RECUR (TREE_OPERAND (t, 2)),
13517 complain);
13518 /* TREE_NO_WARNING must be set if either the expression was
13519 parenthesized or it uses an operator such as >>= rather
13520 than plain assignment. In the former case, it was already
13521 set and must be copied. In the latter case,
13522 build_x_modify_expr sets it and it must not be reset
13523 here. */
13524 if (TREE_NO_WARNING (t))
13525 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13526 RETURN (r);
13529 case ARROW_EXPR:
13530 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13531 args, complain, in_decl);
13532 /* Remember that there was a reference to this entity. */
13533 if (DECL_P (op1))
13534 mark_used (op1);
13535 RETURN (build_x_arrow (input_location, op1, complain));
13537 case NEW_EXPR:
13539 tree placement = RECUR (TREE_OPERAND (t, 0));
13540 tree init = RECUR (TREE_OPERAND (t, 3));
13541 VEC(tree,gc) *placement_vec;
13542 VEC(tree,gc) *init_vec;
13543 tree ret;
13545 if (placement == NULL_TREE)
13546 placement_vec = NULL;
13547 else
13549 placement_vec = make_tree_vector ();
13550 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13551 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
13554 /* If there was an initializer in the original tree, but it
13555 instantiated to an empty list, then we should pass a
13556 non-NULL empty vector to tell build_new that it was an
13557 empty initializer() rather than no initializer. This can
13558 only happen when the initializer is a pack expansion whose
13559 parameter packs are of length zero. */
13560 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13561 init_vec = NULL;
13562 else
13564 init_vec = make_tree_vector ();
13565 if (init == void_zero_node)
13566 gcc_assert (init_vec != NULL);
13567 else
13569 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13570 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
13574 ret = build_new (&placement_vec,
13575 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13576 RECUR (TREE_OPERAND (t, 2)),
13577 &init_vec,
13578 NEW_EXPR_USE_GLOBAL (t),
13579 complain);
13581 if (placement_vec != NULL)
13582 release_tree_vector (placement_vec);
13583 if (init_vec != NULL)
13584 release_tree_vector (init_vec);
13586 RETURN (ret);
13589 case DELETE_EXPR:
13590 RETURN (delete_sanity
13591 (RECUR (TREE_OPERAND (t, 0)),
13592 RECUR (TREE_OPERAND (t, 1)),
13593 DELETE_EXPR_USE_VEC (t),
13594 DELETE_EXPR_USE_GLOBAL (t),
13595 complain));
13597 case COMPOUND_EXPR:
13598 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
13599 RECUR (TREE_OPERAND (t, 0)),
13600 RECUR (TREE_OPERAND (t, 1)),
13601 complain));
13603 case CALL_EXPR:
13605 tree function;
13606 VEC(tree,gc) *call_args;
13607 unsigned int nargs, i;
13608 bool qualified_p;
13609 bool koenig_p;
13610 tree ret;
13612 function = CALL_EXPR_FN (t);
13613 /* When we parsed the expression, we determined whether or
13614 not Koenig lookup should be performed. */
13615 koenig_p = KOENIG_LOOKUP_P (t);
13616 if (TREE_CODE (function) == SCOPE_REF)
13618 qualified_p = true;
13619 function = tsubst_qualified_id (function, args, complain, in_decl,
13620 /*done=*/false,
13621 /*address_p=*/false);
13623 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13625 /* Do nothing; calling tsubst_copy_and_build on an identifier
13626 would incorrectly perform unqualified lookup again.
13628 Note that we can also have an IDENTIFIER_NODE if the earlier
13629 unqualified lookup found a member function; in that case
13630 koenig_p will be false and we do want to do the lookup
13631 again to find the instantiated member function.
13633 FIXME but doing that causes c++/15272, so we need to stop
13634 using IDENTIFIER_NODE in that situation. */
13635 qualified_p = false;
13637 else
13639 if (TREE_CODE (function) == COMPONENT_REF)
13641 tree op = TREE_OPERAND (function, 1);
13643 qualified_p = (TREE_CODE (op) == SCOPE_REF
13644 || (BASELINK_P (op)
13645 && BASELINK_QUALIFIED_P (op)));
13647 else
13648 qualified_p = false;
13650 function = tsubst_copy_and_build (function, args, complain,
13651 in_decl,
13652 !qualified_p,
13653 integral_constant_expression_p);
13655 if (BASELINK_P (function))
13656 qualified_p = true;
13659 nargs = call_expr_nargs (t);
13660 call_args = make_tree_vector ();
13661 for (i = 0; i < nargs; ++i)
13663 tree arg = CALL_EXPR_ARG (t, i);
13665 if (!PACK_EXPANSION_P (arg))
13666 VEC_safe_push (tree, gc, call_args,
13667 RECUR (CALL_EXPR_ARG (t, i)));
13668 else
13670 /* Expand the pack expansion and push each entry onto
13671 CALL_ARGS. */
13672 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13673 if (TREE_CODE (arg) == TREE_VEC)
13675 unsigned int len, j;
13677 len = TREE_VEC_LENGTH (arg);
13678 for (j = 0; j < len; ++j)
13680 tree value = TREE_VEC_ELT (arg, j);
13681 if (value != NULL_TREE)
13682 value = convert_from_reference (value);
13683 VEC_safe_push (tree, gc, call_args, value);
13686 else
13688 /* A partial substitution. Add one entry. */
13689 VEC_safe_push (tree, gc, call_args, arg);
13694 /* We do not perform argument-dependent lookup if normal
13695 lookup finds a non-function, in accordance with the
13696 expected resolution of DR 218. */
13697 if (koenig_p
13698 && ((is_overloaded_fn (function)
13699 /* If lookup found a member function, the Koenig lookup is
13700 not appropriate, even if an unqualified-name was used
13701 to denote the function. */
13702 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13703 || TREE_CODE (function) == IDENTIFIER_NODE)
13704 /* Only do this when substitution turns a dependent call
13705 into a non-dependent call. */
13706 && type_dependent_expression_p_push (t)
13707 && !any_type_dependent_arguments_p (call_args))
13708 function = perform_koenig_lookup (function, call_args, false,
13709 tf_none);
13711 if (TREE_CODE (function) == IDENTIFIER_NODE
13712 && !any_type_dependent_arguments_p (call_args))
13714 if (koenig_p && (complain & tf_warning_or_error))
13716 /* For backwards compatibility and good diagnostics, try
13717 the unqualified lookup again if we aren't in SFINAE
13718 context. */
13719 tree unq = (tsubst_copy_and_build
13720 (function, args, complain, in_decl, true,
13721 integral_constant_expression_p));
13722 if (unq == error_mark_node)
13723 RETURN (error_mark_node);
13725 if (unq != function)
13727 tree fn = unq;
13728 if (TREE_CODE (fn) == INDIRECT_REF)
13729 fn = TREE_OPERAND (fn, 0);
13730 if (TREE_CODE (fn) == COMPONENT_REF)
13731 fn = TREE_OPERAND (fn, 1);
13732 if (is_overloaded_fn (fn))
13733 fn = get_first_fn (fn);
13734 permerror (EXPR_LOC_OR_HERE (t),
13735 "%qD was not declared in this scope, "
13736 "and no declarations were found by "
13737 "argument-dependent lookup at the point "
13738 "of instantiation", function);
13739 if (!DECL_P (fn))
13740 /* Can't say anything more. */;
13741 else if (DECL_CLASS_SCOPE_P (fn))
13743 inform (EXPR_LOC_OR_HERE (t),
13744 "declarations in dependent base %qT are "
13745 "not found by unqualified lookup",
13746 DECL_CLASS_CONTEXT (fn));
13747 if (current_class_ptr)
13748 inform (EXPR_LOC_OR_HERE (t),
13749 "use %<this->%D%> instead", function);
13750 else
13751 inform (EXPR_LOC_OR_HERE (t),
13752 "use %<%T::%D%> instead",
13753 current_class_name, function);
13755 else
13756 inform (0, "%q+D declared here, later in the "
13757 "translation unit", fn);
13758 function = unq;
13761 if (TREE_CODE (function) == IDENTIFIER_NODE)
13763 unqualified_name_lookup_error (function);
13764 release_tree_vector (call_args);
13765 RETURN (error_mark_node);
13769 /* Remember that there was a reference to this entity. */
13770 if (DECL_P (function))
13771 mark_used (function);
13773 if (TREE_CODE (function) == OFFSET_REF)
13774 ret = build_offset_ref_call_from_tree (function, &call_args);
13775 else if (TREE_CODE (function) == COMPONENT_REF)
13777 tree instance = TREE_OPERAND (function, 0);
13778 tree fn = TREE_OPERAND (function, 1);
13780 if (processing_template_decl
13781 && (type_dependent_expression_p (instance)
13782 || (!BASELINK_P (fn)
13783 && TREE_CODE (fn) != FIELD_DECL)
13784 || type_dependent_expression_p (fn)
13785 || any_type_dependent_arguments_p (call_args)))
13786 ret = build_nt_call_vec (function, call_args);
13787 else if (!BASELINK_P (fn))
13788 ret = finish_call_expr (function, &call_args,
13789 /*disallow_virtual=*/false,
13790 /*koenig_p=*/false,
13791 complain);
13792 else
13793 ret = (build_new_method_call
13794 (instance, fn,
13795 &call_args, NULL_TREE,
13796 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
13797 /*fn_p=*/NULL,
13798 complain));
13800 else
13801 ret = finish_call_expr (function, &call_args,
13802 /*disallow_virtual=*/qualified_p,
13803 koenig_p,
13804 complain);
13806 release_tree_vector (call_args);
13808 RETURN (ret);
13811 case COND_EXPR:
13813 tree cond = RECUR (TREE_OPERAND (t, 0));
13814 tree exp1, exp2;
13816 if (TREE_CODE (cond) == INTEGER_CST)
13818 if (integer_zerop (cond))
13820 ++c_inhibit_evaluation_warnings;
13821 exp1 = RECUR (TREE_OPERAND (t, 1));
13822 --c_inhibit_evaluation_warnings;
13823 exp2 = RECUR (TREE_OPERAND (t, 2));
13825 else
13827 exp1 = RECUR (TREE_OPERAND (t, 1));
13828 ++c_inhibit_evaluation_warnings;
13829 exp2 = RECUR (TREE_OPERAND (t, 2));
13830 --c_inhibit_evaluation_warnings;
13833 else
13835 exp1 = RECUR (TREE_OPERAND (t, 1));
13836 exp2 = RECUR (TREE_OPERAND (t, 2));
13839 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
13840 cond, exp1, exp2, complain));
13843 case PSEUDO_DTOR_EXPR:
13844 RETURN (finish_pseudo_destructor_expr
13845 (RECUR (TREE_OPERAND (t, 0)),
13846 RECUR (TREE_OPERAND (t, 1)),
13847 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl)));
13849 case TREE_LIST:
13851 tree purpose, value, chain;
13853 if (t == void_list_node)
13854 RETURN (t);
13856 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13857 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13859 /* We have pack expansions, so expand those and
13860 create a new list out of it. */
13861 tree purposevec = NULL_TREE;
13862 tree valuevec = NULL_TREE;
13863 tree chain;
13864 int i, len = -1;
13866 /* Expand the argument expressions. */
13867 if (TREE_PURPOSE (t))
13868 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13869 complain, in_decl);
13870 if (TREE_VALUE (t))
13871 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13872 complain, in_decl);
13874 /* Build the rest of the list. */
13875 chain = TREE_CHAIN (t);
13876 if (chain && chain != void_type_node)
13877 chain = RECUR (chain);
13879 /* Determine the number of arguments. */
13880 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13882 len = TREE_VEC_LENGTH (purposevec);
13883 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13885 else if (TREE_CODE (valuevec) == TREE_VEC)
13886 len = TREE_VEC_LENGTH (valuevec);
13887 else
13889 /* Since we only performed a partial substitution into
13890 the argument pack, we only RETURN (a single list
13891 node. */
13892 if (purposevec == TREE_PURPOSE (t)
13893 && valuevec == TREE_VALUE (t)
13894 && chain == TREE_CHAIN (t))
13895 RETURN (t);
13897 RETURN (tree_cons (purposevec, valuevec, chain));
13900 /* Convert the argument vectors into a TREE_LIST */
13901 i = len;
13902 while (i > 0)
13904 /* Grab the Ith values. */
13905 i--;
13906 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13907 : NULL_TREE;
13908 value
13909 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
13910 : NULL_TREE;
13912 /* Build the list (backwards). */
13913 chain = tree_cons (purpose, value, chain);
13916 RETURN (chain);
13919 purpose = TREE_PURPOSE (t);
13920 if (purpose)
13921 purpose = RECUR (purpose);
13922 value = TREE_VALUE (t);
13923 if (value)
13924 value = RECUR (value);
13925 chain = TREE_CHAIN (t);
13926 if (chain && chain != void_type_node)
13927 chain = RECUR (chain);
13928 if (purpose == TREE_PURPOSE (t)
13929 && value == TREE_VALUE (t)
13930 && chain == TREE_CHAIN (t))
13931 RETURN (t);
13932 RETURN (tree_cons (purpose, value, chain));
13935 case COMPONENT_REF:
13937 tree object;
13938 tree object_type;
13939 tree member;
13941 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13942 args, complain, in_decl);
13943 /* Remember that there was a reference to this entity. */
13944 if (DECL_P (object))
13945 mark_used (object);
13946 object_type = TREE_TYPE (object);
13948 member = TREE_OPERAND (t, 1);
13949 if (BASELINK_P (member))
13950 member = tsubst_baselink (member,
13951 non_reference (TREE_TYPE (object)),
13952 args, complain, in_decl);
13953 else
13954 member = tsubst_copy (member, args, complain, in_decl);
13955 if (member == error_mark_node)
13956 RETURN (error_mark_node);
13958 if (type_dependent_expression_p (object))
13959 /* We can't do much here. */;
13960 else if (!CLASS_TYPE_P (object_type))
13962 if (scalarish_type_p (object_type))
13964 tree s = NULL_TREE;
13965 tree dtor = member;
13967 if (TREE_CODE (dtor) == SCOPE_REF)
13969 s = TREE_OPERAND (dtor, 0);
13970 dtor = TREE_OPERAND (dtor, 1);
13972 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
13974 dtor = TREE_OPERAND (dtor, 0);
13975 if (TYPE_P (dtor))
13976 RETURN (finish_pseudo_destructor_expr (object, s, dtor));
13980 else if (TREE_CODE (member) == SCOPE_REF
13981 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
13983 /* Lookup the template functions now that we know what the
13984 scope is. */
13985 tree scope = TREE_OPERAND (member, 0);
13986 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
13987 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
13988 member = lookup_qualified_name (scope, tmpl,
13989 /*is_type_p=*/false,
13990 /*complain=*/false);
13991 if (BASELINK_P (member))
13993 BASELINK_FUNCTIONS (member)
13994 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
13995 args);
13996 member = (adjust_result_of_qualified_name_lookup
13997 (member, BINFO_TYPE (BASELINK_BINFO (member)),
13998 object_type));
14000 else
14002 qualified_name_lookup_error (scope, tmpl, member,
14003 input_location);
14004 RETURN (error_mark_node);
14007 else if (TREE_CODE (member) == SCOPE_REF
14008 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14009 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14011 if (complain & tf_error)
14013 if (TYPE_P (TREE_OPERAND (member, 0)))
14014 error ("%qT is not a class or namespace",
14015 TREE_OPERAND (member, 0));
14016 else
14017 error ("%qD is not a class or namespace",
14018 TREE_OPERAND (member, 0));
14020 RETURN (error_mark_node);
14022 else if (TREE_CODE (member) == FIELD_DECL)
14023 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14025 RETURN (finish_class_member_access_expr (object, member,
14026 /*template_p=*/false,
14027 complain));
14030 case THROW_EXPR:
14031 RETURN (build_throw
14032 (RECUR (TREE_OPERAND (t, 0))));
14034 case CONSTRUCTOR:
14036 VEC(constructor_elt,gc) *n;
14037 constructor_elt *ce;
14038 unsigned HOST_WIDE_INT idx;
14039 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14040 bool process_index_p;
14041 int newlen;
14042 bool need_copy_p = false;
14043 tree r;
14045 if (type == error_mark_node)
14046 RETURN (error_mark_node);
14048 /* digest_init will do the wrong thing if we let it. */
14049 if (type && TYPE_PTRMEMFUNC_P (type))
14050 RETURN (t);
14052 /* We do not want to process the index of aggregate
14053 initializers as they are identifier nodes which will be
14054 looked up by digest_init. */
14055 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14057 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
14058 newlen = VEC_length (constructor_elt, n);
14059 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
14061 if (ce->index && process_index_p)
14062 ce->index = RECUR (ce->index);
14064 if (PACK_EXPANSION_P (ce->value))
14066 /* Substitute into the pack expansion. */
14067 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14068 in_decl);
14070 if (ce->value == error_mark_node
14071 || PACK_EXPANSION_P (ce->value))
14073 else if (TREE_VEC_LENGTH (ce->value) == 1)
14074 /* Just move the argument into place. */
14075 ce->value = TREE_VEC_ELT (ce->value, 0);
14076 else
14078 /* Update the length of the final CONSTRUCTOR
14079 arguments vector, and note that we will need to
14080 copy.*/
14081 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14082 need_copy_p = true;
14085 else
14086 ce->value = RECUR (ce->value);
14089 if (need_copy_p)
14091 VEC(constructor_elt,gc) *old_n = n;
14093 n = VEC_alloc (constructor_elt, gc, newlen);
14094 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
14096 if (TREE_CODE (ce->value) == TREE_VEC)
14098 int i, len = TREE_VEC_LENGTH (ce->value);
14099 for (i = 0; i < len; ++i)
14100 CONSTRUCTOR_APPEND_ELT (n, 0,
14101 TREE_VEC_ELT (ce->value, i));
14103 else
14104 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14108 r = build_constructor (init_list_type_node, n);
14109 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14111 if (TREE_HAS_CONSTRUCTOR (t))
14112 RETURN (finish_compound_literal (type, r, complain));
14114 TREE_TYPE (r) = type;
14115 RETURN (r);
14118 case TYPEID_EXPR:
14120 tree operand_0 = TREE_OPERAND (t, 0);
14121 if (TYPE_P (operand_0))
14123 operand_0 = tsubst (operand_0, args, complain, in_decl);
14124 RETURN (get_typeid (operand_0));
14126 else
14128 operand_0 = RECUR (operand_0);
14129 RETURN (build_typeid (operand_0));
14133 case VAR_DECL:
14134 if (!args)
14135 RETURN (t);
14136 /* Fall through */
14138 case PARM_DECL:
14140 tree r = tsubst_copy (t, args, complain, in_decl);
14142 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14143 /* If the original type was a reference, we'll be wrapped in
14144 the appropriate INDIRECT_REF. */
14145 r = convert_from_reference (r);
14146 RETURN (r);
14149 case VA_ARG_EXPR:
14150 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14151 RECUR (TREE_OPERAND (t, 0)),
14152 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14154 case OFFSETOF_EXPR:
14155 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14157 case TRAIT_EXPR:
14159 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14160 complain, in_decl);
14162 tree type2 = TRAIT_EXPR_TYPE2 (t);
14163 if (type2)
14164 type2 = tsubst_copy (type2, args, complain, in_decl);
14166 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14169 case STMT_EXPR:
14171 tree old_stmt_expr = cur_stmt_expr;
14172 tree stmt_expr = begin_stmt_expr ();
14174 cur_stmt_expr = stmt_expr;
14175 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14176 integral_constant_expression_p);
14177 stmt_expr = finish_stmt_expr (stmt_expr, false);
14178 cur_stmt_expr = old_stmt_expr;
14180 /* If the resulting list of expression statement is empty,
14181 fold it further into void_zero_node. */
14182 if (empty_expr_stmt_p (stmt_expr))
14183 stmt_expr = void_zero_node;
14185 RETURN (stmt_expr);
14188 case LAMBDA_EXPR:
14190 tree r = build_lambda_expr ();
14192 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14193 LAMBDA_EXPR_CLOSURE (r) = type;
14194 CLASSTYPE_LAMBDA_EXPR (type) = r;
14196 LAMBDA_EXPR_LOCATION (r)
14197 = LAMBDA_EXPR_LOCATION (t);
14198 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14199 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14200 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14201 LAMBDA_EXPR_DISCRIMINATOR (r)
14202 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14203 LAMBDA_EXPR_EXTRA_SCOPE (r)
14204 = tsubst (LAMBDA_EXPR_EXTRA_SCOPE (t), args, complain, in_decl);
14205 LAMBDA_EXPR_RETURN_TYPE (r)
14206 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
14208 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
14209 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
14211 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14212 determine_visibility (TYPE_NAME (type));
14213 /* Now that we know visibility, instantiate the type so we have a
14214 declaration of the op() for later calls to lambda_function. */
14215 complete_type (type);
14217 /* The capture list refers to closure members, so this needs to
14218 wait until after we finish instantiating the type. */
14219 LAMBDA_EXPR_CAPTURE_LIST (r)
14220 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
14222 RETURN (build_lambda_object (r));
14225 case TARGET_EXPR:
14226 /* We can get here for a constant initializer of non-dependent type.
14227 FIXME stop folding in cp_parser_initializer_clause. */
14228 gcc_assert (TREE_CONSTANT (t));
14230 tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t)));
14231 TREE_CONSTANT (r) = true;
14232 RETURN (r);
14235 case TRANSACTION_EXPR:
14236 RETURN (tsubst_expr(t, args, complain, in_decl,
14237 integral_constant_expression_p));
14239 default:
14240 /* Handle Objective-C++ constructs, if appropriate. */
14242 tree subst
14243 = objcp_tsubst_copy_and_build (t, args, complain,
14244 in_decl, /*function_p=*/false);
14245 if (subst)
14246 RETURN (subst);
14248 RETURN (tsubst_copy (t, args, complain, in_decl));
14251 #undef RECUR
14252 #undef RETURN
14253 out:
14254 input_location = loc;
14255 return retval;
14258 /* Verify that the instantiated ARGS are valid. For type arguments,
14259 make sure that the type's linkage is ok. For non-type arguments,
14260 make sure they are constants if they are integral or enumerations.
14261 Emit an error under control of COMPLAIN, and return TRUE on error. */
14263 static bool
14264 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
14266 if (ARGUMENT_PACK_P (t))
14268 tree vec = ARGUMENT_PACK_ARGS (t);
14269 int len = TREE_VEC_LENGTH (vec);
14270 bool result = false;
14271 int i;
14273 for (i = 0; i < len; ++i)
14274 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
14275 result = true;
14276 return result;
14278 else if (TYPE_P (t))
14280 /* [basic.link]: A name with no linkage (notably, the name
14281 of a class or enumeration declared in a local scope)
14282 shall not be used to declare an entity with linkage.
14283 This implies that names with no linkage cannot be used as
14284 template arguments
14286 DR 757 relaxes this restriction for C++0x. */
14287 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
14288 : no_linkage_check (t, /*relaxed_p=*/false));
14290 if (nt)
14292 /* DR 488 makes use of a type with no linkage cause
14293 type deduction to fail. */
14294 if (complain & tf_error)
14296 if (TYPE_ANONYMOUS_P (nt))
14297 error ("%qT is/uses anonymous type", t);
14298 else
14299 error ("template argument for %qD uses local type %qT",
14300 tmpl, t);
14302 return true;
14304 /* In order to avoid all sorts of complications, we do not
14305 allow variably-modified types as template arguments. */
14306 else if (variably_modified_type_p (t, NULL_TREE))
14308 if (complain & tf_error)
14309 error ("%qT is a variably modified type", t);
14310 return true;
14313 /* A non-type argument of integral or enumerated type must be a
14314 constant. */
14315 else if (TREE_TYPE (t)
14316 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
14317 && !TREE_CONSTANT (t))
14319 if (complain & tf_error)
14320 error ("integral expression %qE is not constant", t);
14321 return true;
14323 return false;
14326 static bool
14327 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
14329 int ix, len = DECL_NTPARMS (tmpl);
14330 bool result = false;
14332 for (ix = 0; ix != len; ix++)
14334 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
14335 result = true;
14337 if (result && (complain & tf_error))
14338 error (" trying to instantiate %qD", tmpl);
14339 return result;
14342 /* We're out of SFINAE context now, so generate diagnostics for the access
14343 errors we saw earlier when instantiating D from TMPL and ARGS. */
14345 static void
14346 recheck_decl_substitution (tree d, tree tmpl, tree args)
14348 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
14349 tree type = TREE_TYPE (pattern);
14350 location_t loc = input_location;
14352 push_access_scope (d);
14353 push_deferring_access_checks (dk_no_deferred);
14354 input_location = DECL_SOURCE_LOCATION (pattern);
14355 tsubst (type, args, tf_warning_or_error, d);
14356 input_location = loc;
14357 pop_deferring_access_checks ();
14358 pop_access_scope (d);
14361 /* Instantiate the indicated variable or function template TMPL with
14362 the template arguments in TARG_PTR. */
14364 static tree
14365 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14367 tree targ_ptr = orig_args;
14368 tree fndecl;
14369 tree gen_tmpl;
14370 tree spec;
14371 bool access_ok = true;
14373 if (tmpl == error_mark_node)
14374 return error_mark_node;
14376 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14378 /* If this function is a clone, handle it specially. */
14379 if (DECL_CLONED_FUNCTION_P (tmpl))
14381 tree spec;
14382 tree clone;
14384 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14385 DECL_CLONED_FUNCTION. */
14386 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14387 targ_ptr, complain);
14388 if (spec == error_mark_node)
14389 return error_mark_node;
14391 /* Look for the clone. */
14392 FOR_EACH_CLONE (clone, spec)
14393 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14394 return clone;
14395 /* We should always have found the clone by now. */
14396 gcc_unreachable ();
14397 return NULL_TREE;
14400 /* Check to see if we already have this specialization. */
14401 gen_tmpl = most_general_template (tmpl);
14402 if (tmpl != gen_tmpl)
14403 /* The TMPL is a partial instantiation. To get a full set of
14404 arguments we must add the arguments used to perform the
14405 partial instantiation. */
14406 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14407 targ_ptr);
14409 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14410 but it doesn't seem to be on the hot path. */
14411 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14413 gcc_assert (tmpl == gen_tmpl
14414 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14415 == spec)
14416 || fndecl == NULL_TREE);
14418 if (spec != NULL_TREE)
14420 if (FNDECL_HAS_ACCESS_ERRORS (spec))
14422 if (complain & tf_error)
14423 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
14424 return error_mark_node;
14426 return spec;
14429 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14430 complain))
14431 return error_mark_node;
14433 /* We are building a FUNCTION_DECL, during which the access of its
14434 parameters and return types have to be checked. However this
14435 FUNCTION_DECL which is the desired context for access checking
14436 is not built yet. We solve this chicken-and-egg problem by
14437 deferring all checks until we have the FUNCTION_DECL. */
14438 push_deferring_access_checks (dk_deferred);
14440 /* Instantiation of the function happens in the context of the function
14441 template, not the context of the overload resolution we're doing. */
14442 push_to_top_level ();
14443 /* If there are dependent arguments, e.g. because we're doing partial
14444 ordering, make sure processing_template_decl stays set. */
14445 if (uses_template_parms (targ_ptr))
14446 ++processing_template_decl;
14447 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14449 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
14450 complain, gen_tmpl);
14451 push_nested_class (ctx);
14453 /* Substitute template parameters to obtain the specialization. */
14454 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14455 targ_ptr, complain, gen_tmpl);
14456 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14457 pop_nested_class ();
14458 pop_from_top_level ();
14460 if (fndecl == error_mark_node)
14462 pop_deferring_access_checks ();
14463 return error_mark_node;
14466 /* The DECL_TI_TEMPLATE should always be the immediate parent
14467 template, not the most general template. */
14468 DECL_TI_TEMPLATE (fndecl) = tmpl;
14470 /* Now we know the specialization, compute access previously
14471 deferred. */
14472 push_access_scope (fndecl);
14473 if (!perform_deferred_access_checks (complain))
14474 access_ok = false;
14475 pop_access_scope (fndecl);
14476 pop_deferring_access_checks ();
14478 /* If we've just instantiated the main entry point for a function,
14479 instantiate all the alternate entry points as well. We do this
14480 by cloning the instantiation of the main entry point, not by
14481 instantiating the template clones. */
14482 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14483 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14485 if (!access_ok)
14487 if (!(complain & tf_error))
14489 /* Remember to reinstantiate when we're out of SFINAE so the user
14490 can see the errors. */
14491 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
14493 return error_mark_node;
14495 return fndecl;
14498 /* Wrapper for instantiate_template_1. */
14500 tree
14501 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14503 tree ret;
14504 timevar_push (TV_TEMPLATE_INST);
14505 ret = instantiate_template_1 (tmpl, orig_args, complain);
14506 timevar_pop (TV_TEMPLATE_INST);
14507 return ret;
14510 /* PARM is a template parameter pack for FN. Returns true iff
14511 PARM is used in a deducible way in the argument list of FN. */
14513 static bool
14514 pack_deducible_p (tree parm, tree fn)
14516 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
14517 for (; t; t = TREE_CHAIN (t))
14519 tree type = TREE_VALUE (t);
14520 tree packs;
14521 if (!PACK_EXPANSION_P (type))
14522 continue;
14523 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
14524 packs; packs = TREE_CHAIN (packs))
14525 if (TREE_VALUE (packs) == parm)
14527 /* The template parameter pack is used in a function parameter
14528 pack. If this is the end of the parameter list, the
14529 template parameter pack is deducible. */
14530 if (TREE_CHAIN (t) == void_list_node)
14531 return true;
14532 else
14533 /* Otherwise, not. Well, it could be deduced from
14534 a non-pack parameter, but doing so would end up with
14535 a deduction mismatch, so don't bother. */
14536 return false;
14539 /* The template parameter pack isn't used in any function parameter
14540 packs, but it might be used deeper, e.g. tuple<Args...>. */
14541 return true;
14544 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14545 NARGS elements of the arguments that are being used when calling
14546 it. TARGS is a vector into which the deduced template arguments
14547 are placed.
14549 Return zero for success, 2 for an incomplete match that doesn't resolve
14550 all the types, and 1 for complete failure. An error message will be
14551 printed only for an incomplete match.
14553 If FN is a conversion operator, or we are trying to produce a specific
14554 specialization, RETURN_TYPE is the return type desired.
14556 The EXPLICIT_TARGS are explicit template arguments provided via a
14557 template-id.
14559 The parameter STRICT is one of:
14561 DEDUCE_CALL:
14562 We are deducing arguments for a function call, as in
14563 [temp.deduct.call].
14565 DEDUCE_CONV:
14566 We are deducing arguments for a conversion function, as in
14567 [temp.deduct.conv].
14569 DEDUCE_EXACT:
14570 We are deducing arguments when doing an explicit instantiation
14571 as in [temp.explicit], when determining an explicit specialization
14572 as in [temp.expl.spec], or when taking the address of a function
14573 template, as in [temp.deduct.funcaddr]. */
14575 tree
14576 fn_type_unification (tree fn,
14577 tree explicit_targs,
14578 tree targs,
14579 const tree *args,
14580 unsigned int nargs,
14581 tree return_type,
14582 unification_kind_t strict,
14583 int flags,
14584 bool explain_p)
14586 tree parms;
14587 tree fntype;
14588 tree decl = NULL_TREE;
14589 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
14590 bool ok;
14591 static int deduction_depth;
14592 struct pending_template *old_last_pend = last_pending_template;
14593 struct tinst_level *old_error_tinst = last_error_tinst_level;
14594 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14595 tree tinst;
14596 tree r = error_mark_node;
14598 /* Adjust any explicit template arguments before entering the
14599 substitution context. */
14600 if (explicit_targs)
14602 explicit_targs
14603 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
14604 complain,
14605 /*require_all_args=*/false,
14606 /*use_default_args=*/false));
14607 if (explicit_targs == error_mark_node)
14608 return error_mark_node;
14611 /* In C++0x, it's possible to have a function template whose type depends
14612 on itself recursively. This is most obvious with decltype, but can also
14613 occur with enumeration scope (c++/48969). So we need to catch infinite
14614 recursion and reject the substitution at deduction time; this function
14615 will return error_mark_node for any repeated substitution.
14617 This also catches excessive recursion such as when f<N> depends on
14618 f<N-1> across all integers, and returns error_mark_node for all the
14619 substitutions back up to the initial one.
14621 This is, of course, not reentrant. */
14622 if (excessive_deduction_depth)
14623 return error_mark_node;
14624 tinst = build_tree_list (fn, targs);
14625 if (!push_tinst_level (tinst))
14627 excessive_deduction_depth = true;
14628 ggc_free (tinst);
14629 return error_mark_node;
14631 ++deduction_depth;
14632 push_deferring_access_checks (dk_deferred);
14634 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14636 fntype = TREE_TYPE (fn);
14637 if (explicit_targs)
14639 /* [temp.deduct]
14641 The specified template arguments must match the template
14642 parameters in kind (i.e., type, nontype, template), and there
14643 must not be more arguments than there are parameters;
14644 otherwise type deduction fails.
14646 Nontype arguments must match the types of the corresponding
14647 nontype template parameters, or must be convertible to the
14648 types of the corresponding nontype parameters as specified in
14649 _temp.arg.nontype_, otherwise type deduction fails.
14651 All references in the function type of the function template
14652 to the corresponding template parameters are replaced by the
14653 specified template argument values. If a substitution in a
14654 template parameter or in the function type of the function
14655 template results in an invalid type, type deduction fails. */
14656 int i, len = TREE_VEC_LENGTH (tparms);
14657 location_t loc = input_location;
14658 bool incomplete = false;
14660 /* Substitute the explicit args into the function type. This is
14661 necessary so that, for instance, explicitly declared function
14662 arguments can match null pointed constants. If we were given
14663 an incomplete set of explicit args, we must not do semantic
14664 processing during substitution as we could create partial
14665 instantiations. */
14666 for (i = 0; i < len; i++)
14668 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14669 bool parameter_pack = false;
14670 tree targ = TREE_VEC_ELT (explicit_targs, i);
14672 /* Dig out the actual parm. */
14673 if (TREE_CODE (parm) == TYPE_DECL
14674 || TREE_CODE (parm) == TEMPLATE_DECL)
14676 parm = TREE_TYPE (parm);
14677 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
14679 else if (TREE_CODE (parm) == PARM_DECL)
14681 parm = DECL_INITIAL (parm);
14682 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
14685 if (!parameter_pack && targ == NULL_TREE)
14686 /* No explicit argument for this template parameter. */
14687 incomplete = true;
14689 if (parameter_pack && pack_deducible_p (parm, fn))
14691 /* Mark the argument pack as "incomplete". We could
14692 still deduce more arguments during unification.
14693 We remove this mark in type_unification_real. */
14694 if (targ)
14696 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
14697 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
14698 = ARGUMENT_PACK_ARGS (targ);
14701 /* We have some incomplete argument packs. */
14702 incomplete = true;
14706 processing_template_decl += incomplete;
14707 input_location = DECL_SOURCE_LOCATION (fn);
14708 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
14709 complain | tf_partial, NULL_TREE);
14710 input_location = loc;
14711 processing_template_decl -= incomplete;
14713 if (fntype == error_mark_node)
14714 goto fail;
14716 /* Place the explicitly specified arguments in TARGS. */
14717 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
14718 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
14721 /* Never do unification on the 'this' parameter. */
14722 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
14724 if (return_type)
14726 tree *new_args;
14728 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
14729 new_args = XALLOCAVEC (tree, nargs + 1);
14730 new_args[0] = return_type;
14731 memcpy (new_args + 1, args, nargs * sizeof (tree));
14732 args = new_args;
14733 ++nargs;
14736 /* We allow incomplete unification without an error message here
14737 because the standard doesn't seem to explicitly prohibit it. Our
14738 callers must be ready to deal with unification failures in any
14739 event. */
14741 pop_tinst_level ();
14742 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
14743 targs, parms, args, nargs, /*subr=*/0,
14744 strict, flags, explain_p);
14745 push_tinst_level (tinst);
14746 if (!ok)
14747 goto fail;
14749 /* Now that we have bindings for all of the template arguments,
14750 ensure that the arguments deduced for the template template
14751 parameters have compatible template parameter lists. We cannot
14752 check this property before we have deduced all template
14753 arguments, because the template parameter types of a template
14754 template parameter might depend on prior template parameters
14755 deduced after the template template parameter. The following
14756 ill-formed example illustrates this issue:
14758 template<typename T, template<T> class C> void f(C<5>, T);
14760 template<int N> struct X {};
14762 void g() {
14763 f(X<5>(), 5l); // error: template argument deduction fails
14766 The template parameter list of 'C' depends on the template type
14767 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14768 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14769 time that we deduce 'C'. */
14770 if (!template_template_parm_bindings_ok_p
14771 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
14773 unify_inconsistent_template_template_parameters (explain_p);
14774 goto fail;
14777 /* All is well so far. Now, check:
14779 [temp.deduct]
14781 When all template arguments have been deduced, all uses of
14782 template parameters in nondeduced contexts are replaced with
14783 the corresponding deduced argument values. If the
14784 substitution results in an invalid type, as described above,
14785 type deduction fails. */
14786 decl = instantiate_template (fn, targs, complain);
14787 if (decl == error_mark_node)
14788 goto fail;
14790 /* Now perform any access checks encountered during deduction, such as
14791 for default template arguments. */
14792 push_access_scope (decl);
14793 ok = perform_deferred_access_checks (complain);
14794 pop_access_scope (decl);
14795 if (!ok)
14796 goto fail;
14798 /* If we're looking for an exact match, check that what we got
14799 is indeed an exact match. It might not be if some template
14800 parameters are used in non-deduced contexts. */
14801 if (strict == DEDUCE_EXACT)
14803 tree substed = TREE_TYPE (decl);
14804 unsigned int i;
14806 tree sarg
14807 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
14808 if (return_type)
14809 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
14810 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
14811 if (!same_type_p (args[i], TREE_VALUE (sarg)))
14813 unify_type_mismatch (explain_p, args[i],
14814 TREE_VALUE (sarg));
14815 goto fail;
14819 r = decl;
14821 fail:
14822 pop_deferring_access_checks ();
14823 --deduction_depth;
14824 if (excessive_deduction_depth)
14826 if (deduction_depth == 0)
14827 /* Reset once we're all the way out. */
14828 excessive_deduction_depth = false;
14831 pop_tinst_level ();
14832 /* We can't free this if a pending_template entry or last_error_tinst_level
14833 is pointing at it. */
14834 if (last_pending_template == old_last_pend
14835 && last_error_tinst_level == old_error_tinst)
14836 ggc_free (tinst);
14838 return r;
14841 /* Adjust types before performing type deduction, as described in
14842 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14843 sections are symmetric. PARM is the type of a function parameter
14844 or the return type of the conversion function. ARG is the type of
14845 the argument passed to the call, or the type of the value
14846 initialized with the result of the conversion function.
14847 ARG_EXPR is the original argument expression, which may be null. */
14849 static int
14850 maybe_adjust_types_for_deduction (unification_kind_t strict,
14851 tree* parm,
14852 tree* arg,
14853 tree arg_expr)
14855 int result = 0;
14857 switch (strict)
14859 case DEDUCE_CALL:
14860 break;
14862 case DEDUCE_CONV:
14864 /* Swap PARM and ARG throughout the remainder of this
14865 function; the handling is precisely symmetric since PARM
14866 will initialize ARG rather than vice versa. */
14867 tree* temp = parm;
14868 parm = arg;
14869 arg = temp;
14870 break;
14873 case DEDUCE_EXACT:
14874 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14875 too, but here handle it by stripping the reference from PARM
14876 rather than by adding it to ARG. */
14877 if (TREE_CODE (*parm) == REFERENCE_TYPE
14878 && TYPE_REF_IS_RVALUE (*parm)
14879 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14880 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14881 && TREE_CODE (*arg) == REFERENCE_TYPE
14882 && !TYPE_REF_IS_RVALUE (*arg))
14883 *parm = TREE_TYPE (*parm);
14884 /* Nothing else to do in this case. */
14885 return 0;
14887 default:
14888 gcc_unreachable ();
14891 if (TREE_CODE (*parm) != REFERENCE_TYPE)
14893 /* [temp.deduct.call]
14895 If P is not a reference type:
14897 --If A is an array type, the pointer type produced by the
14898 array-to-pointer standard conversion (_conv.array_) is
14899 used in place of A for type deduction; otherwise,
14901 --If A is a function type, the pointer type produced by
14902 the function-to-pointer standard conversion
14903 (_conv.func_) is used in place of A for type deduction;
14904 otherwise,
14906 --If A is a cv-qualified type, the top level
14907 cv-qualifiers of A's type are ignored for type
14908 deduction. */
14909 if (TREE_CODE (*arg) == ARRAY_TYPE)
14910 *arg = build_pointer_type (TREE_TYPE (*arg));
14911 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
14912 *arg = build_pointer_type (*arg);
14913 else
14914 *arg = TYPE_MAIN_VARIANT (*arg);
14917 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14918 of the form T&&, where T is a template parameter, and the argument
14919 is an lvalue, T is deduced as A& */
14920 if (TREE_CODE (*parm) == REFERENCE_TYPE
14921 && TYPE_REF_IS_RVALUE (*parm)
14922 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14923 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14924 && (arg_expr ? real_lvalue_p (arg_expr)
14925 /* try_one_overload doesn't provide an arg_expr, but
14926 functions are always lvalues. */
14927 : TREE_CODE (*arg) == FUNCTION_TYPE))
14928 *arg = build_reference_type (*arg);
14930 /* [temp.deduct.call]
14932 If P is a cv-qualified type, the top level cv-qualifiers
14933 of P's type are ignored for type deduction. If P is a
14934 reference type, the type referred to by P is used for
14935 type deduction. */
14936 *parm = TYPE_MAIN_VARIANT (*parm);
14937 if (TREE_CODE (*parm) == REFERENCE_TYPE)
14939 *parm = TREE_TYPE (*parm);
14940 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14943 /* DR 322. For conversion deduction, remove a reference type on parm
14944 too (which has been swapped into ARG). */
14945 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
14946 *arg = TREE_TYPE (*arg);
14948 return result;
14951 /* Subroutine of unify_one_argument. PARM is a function parameter of a
14952 template which does contain any deducible template parameters; check if
14953 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
14954 unify_one_argument. */
14956 static int
14957 check_non_deducible_conversion (tree parm, tree arg, int strict,
14958 int flags, bool explain_p)
14960 tree type;
14962 if (!TYPE_P (arg))
14963 type = TREE_TYPE (arg);
14964 else
14965 type = arg;
14967 if (same_type_p (parm, type))
14968 return unify_success (explain_p);
14970 if (strict == DEDUCE_CONV)
14972 if (can_convert_arg (type, parm, NULL_TREE, flags,
14973 explain_p ? tf_warning_or_error : tf_none))
14974 return unify_success (explain_p);
14976 else if (strict != DEDUCE_EXACT)
14978 if (can_convert_arg (parm, type,
14979 TYPE_P (arg) ? NULL_TREE : arg,
14980 flags, explain_p ? tf_warning_or_error : tf_none))
14981 return unify_success (explain_p);
14984 if (strict == DEDUCE_EXACT)
14985 return unify_type_mismatch (explain_p, parm, arg);
14986 else
14987 return unify_arg_conversion (explain_p, parm, type, arg);
14990 /* Subroutine of type_unification_real and unify_pack_expansion to
14991 handle unification of a single P/A pair. Parameters are as
14992 for those functions. */
14994 static int
14995 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
14996 int subr, unification_kind_t strict, int flags,
14997 bool explain_p)
14999 tree arg_expr = NULL_TREE;
15000 int arg_strict;
15002 if (arg == error_mark_node || parm == error_mark_node)
15003 return unify_invalid (explain_p);
15004 if (arg == unknown_type_node)
15005 /* We can't deduce anything from this, but we might get all the
15006 template args from other function args. */
15007 return unify_success (explain_p);
15009 /* FIXME uses_deducible_template_parms */
15010 if (TYPE_P (parm) && !uses_template_parms (parm))
15011 return check_non_deducible_conversion (parm, arg, strict, flags,
15012 explain_p);
15014 switch (strict)
15016 case DEDUCE_CALL:
15017 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
15018 | UNIFY_ALLOW_MORE_CV_QUAL
15019 | UNIFY_ALLOW_DERIVED);
15020 break;
15022 case DEDUCE_CONV:
15023 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
15024 break;
15026 case DEDUCE_EXACT:
15027 arg_strict = UNIFY_ALLOW_NONE;
15028 break;
15030 default:
15031 gcc_unreachable ();
15034 /* We only do these transformations if this is the top-level
15035 parameter_type_list in a call or declaration matching; in other
15036 situations (nested function declarators, template argument lists) we
15037 won't be comparing a type to an expression, and we don't do any type
15038 adjustments. */
15039 if (!subr)
15041 if (!TYPE_P (arg))
15043 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
15044 if (type_unknown_p (arg))
15046 /* [temp.deduct.type] A template-argument can be
15047 deduced from a pointer to function or pointer
15048 to member function argument if the set of
15049 overloaded functions does not contain function
15050 templates and at most one of a set of
15051 overloaded functions provides a unique
15052 match. */
15054 if (resolve_overloaded_unification
15055 (tparms, targs, parm, arg, strict,
15056 arg_strict, explain_p))
15057 return unify_success (explain_p);
15058 return unify_overload_resolution_failure (explain_p, arg);
15061 arg_expr = arg;
15062 arg = unlowered_expr_type (arg);
15063 if (arg == error_mark_node)
15064 return unify_invalid (explain_p);
15067 arg_strict |=
15068 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
15070 else
15071 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
15072 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
15074 /* For deduction from an init-list we need the actual list. */
15075 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
15076 arg = arg_expr;
15077 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
15080 /* Most parms like fn_type_unification.
15082 If SUBR is 1, we're being called recursively (to unify the
15083 arguments of a function or method parameter of a function
15084 template). */
15086 static int
15087 type_unification_real (tree tparms,
15088 tree targs,
15089 tree xparms,
15090 const tree *xargs,
15091 unsigned int xnargs,
15092 int subr,
15093 unification_kind_t strict,
15094 int flags,
15095 bool explain_p)
15097 tree parm, arg;
15098 int i;
15099 int ntparms = TREE_VEC_LENGTH (tparms);
15100 int saw_undeduced = 0;
15101 tree parms;
15102 const tree *args;
15103 unsigned int nargs;
15104 unsigned int ia;
15106 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
15107 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
15108 gcc_assert (ntparms > 0);
15110 /* Reset the number of non-defaulted template arguments contained
15111 in TARGS. */
15112 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
15114 again:
15115 parms = xparms;
15116 args = xargs;
15117 nargs = xnargs;
15119 ia = 0;
15120 while (parms && parms != void_list_node
15121 && ia < nargs)
15123 parm = TREE_VALUE (parms);
15125 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
15126 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
15127 /* For a function parameter pack that occurs at the end of the
15128 parameter-declaration-list, the type A of each remaining
15129 argument of the call is compared with the type P of the
15130 declarator-id of the function parameter pack. */
15131 break;
15133 parms = TREE_CHAIN (parms);
15135 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
15136 /* For a function parameter pack that does not occur at the
15137 end of the parameter-declaration-list, the type of the
15138 parameter pack is a non-deduced context. */
15139 continue;
15141 arg = args[ia];
15142 ++ia;
15144 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15145 flags, explain_p))
15146 return 1;
15149 if (parms
15150 && parms != void_list_node
15151 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
15153 /* Unify the remaining arguments with the pack expansion type. */
15154 tree argvec;
15155 tree parmvec = make_tree_vec (1);
15157 /* Allocate a TREE_VEC and copy in all of the arguments */
15158 argvec = make_tree_vec (nargs - ia);
15159 for (i = 0; ia < nargs; ++ia, ++i)
15160 TREE_VEC_ELT (argvec, i) = args[ia];
15162 /* Copy the parameter into parmvec. */
15163 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
15164 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
15165 /*subr=*/subr, explain_p))
15166 return 1;
15168 /* Advance to the end of the list of parameters. */
15169 parms = TREE_CHAIN (parms);
15172 /* Fail if we've reached the end of the parm list, and more args
15173 are present, and the parm list isn't variadic. */
15174 if (ia < nargs && parms == void_list_node)
15175 return unify_too_many_arguments (explain_p, nargs, ia);
15176 /* Fail if parms are left and they don't have default values. */
15177 if (parms && parms != void_list_node
15178 && TREE_PURPOSE (parms) == NULL_TREE)
15180 unsigned int count = nargs;
15181 tree p = parms;
15182 while (p && p != void_list_node)
15184 count++;
15185 p = TREE_CHAIN (p);
15187 return unify_too_few_arguments (explain_p, ia, count);
15190 if (!subr)
15192 tsubst_flags_t complain = (explain_p
15193 ? tf_warning_or_error
15194 : tf_none);
15196 /* Check to see if we need another pass before we start clearing
15197 ARGUMENT_PACK_INCOMPLETE_P. */
15198 for (i = 0; i < ntparms; i++)
15200 tree targ = TREE_VEC_ELT (targs, i);
15201 tree tparm = TREE_VEC_ELT (tparms, i);
15203 if (targ || tparm == error_mark_node)
15204 continue;
15205 tparm = TREE_VALUE (tparm);
15207 /* If this is an undeduced nontype parameter that depends on
15208 a type parameter, try another pass; its type may have been
15209 deduced from a later argument than the one from which
15210 this parameter can be deduced. */
15211 if (TREE_CODE (tparm) == PARM_DECL
15212 && uses_template_parms (TREE_TYPE (tparm))
15213 && !saw_undeduced++)
15214 goto again;
15217 for (i = 0; i < ntparms; i++)
15219 tree targ = TREE_VEC_ELT (targs, i);
15220 tree tparm = TREE_VEC_ELT (tparms, i);
15222 /* Clear the "incomplete" flags on all argument packs now so that
15223 substituting them into later default arguments works. */
15224 if (targ && ARGUMENT_PACK_P (targ))
15226 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
15227 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
15230 if (targ || tparm == error_mark_node)
15231 continue;
15232 tparm = TREE_VALUE (tparm);
15234 /* Core issue #226 (C++0x) [temp.deduct]:
15236 If a template argument has not been deduced, its
15237 default template argument, if any, is used.
15239 When we are in C++98 mode, TREE_PURPOSE will either
15240 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15241 to explicitly check cxx_dialect here. */
15242 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
15244 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15245 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
15246 location_t save_loc = input_location;
15247 if (DECL_P (parm))
15248 input_location = DECL_SOURCE_LOCATION (parm);
15249 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
15250 arg = convert_template_argument (parm, arg, targs, complain,
15251 i, NULL_TREE);
15252 input_location = save_loc;
15253 if (arg == error_mark_node)
15254 return 1;
15255 else
15257 TREE_VEC_ELT (targs, i) = arg;
15258 /* The position of the first default template argument,
15259 is also the number of non-defaulted arguments in TARGS.
15260 Record that. */
15261 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15262 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
15263 continue;
15267 /* If the type parameter is a parameter pack, then it will
15268 be deduced to an empty parameter pack. */
15269 if (template_parameter_pack_p (tparm))
15271 tree arg;
15273 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
15275 arg = make_node (NONTYPE_ARGUMENT_PACK);
15276 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
15277 TREE_CONSTANT (arg) = 1;
15279 else
15280 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
15282 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
15284 TREE_VEC_ELT (targs, i) = arg;
15285 continue;
15288 return unify_parameter_deduction_failure (explain_p, tparm);
15291 #ifdef ENABLE_CHECKING
15292 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15293 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
15294 #endif
15296 return unify_success (explain_p);
15299 /* Subroutine of type_unification_real. Args are like the variables
15300 at the call site. ARG is an overloaded function (or template-id);
15301 we try deducing template args from each of the overloads, and if
15302 only one succeeds, we go with that. Modifies TARGS and returns
15303 true on success. */
15305 static bool
15306 resolve_overloaded_unification (tree tparms,
15307 tree targs,
15308 tree parm,
15309 tree arg,
15310 unification_kind_t strict,
15311 int sub_strict,
15312 bool explain_p)
15314 tree tempargs = copy_node (targs);
15315 int good = 0;
15316 tree goodfn = NULL_TREE;
15317 bool addr_p;
15319 if (TREE_CODE (arg) == ADDR_EXPR)
15321 arg = TREE_OPERAND (arg, 0);
15322 addr_p = true;
15324 else
15325 addr_p = false;
15327 if (TREE_CODE (arg) == COMPONENT_REF)
15328 /* Handle `&x' where `x' is some static or non-static member
15329 function name. */
15330 arg = TREE_OPERAND (arg, 1);
15332 if (TREE_CODE (arg) == OFFSET_REF)
15333 arg = TREE_OPERAND (arg, 1);
15335 /* Strip baselink information. */
15336 if (BASELINK_P (arg))
15337 arg = BASELINK_FUNCTIONS (arg);
15339 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
15341 /* If we got some explicit template args, we need to plug them into
15342 the affected templates before we try to unify, in case the
15343 explicit args will completely resolve the templates in question. */
15345 int ok = 0;
15346 tree expl_subargs = TREE_OPERAND (arg, 1);
15347 arg = TREE_OPERAND (arg, 0);
15349 for (; arg; arg = OVL_NEXT (arg))
15351 tree fn = OVL_CURRENT (arg);
15352 tree subargs, elem;
15354 if (TREE_CODE (fn) != TEMPLATE_DECL)
15355 continue;
15357 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15358 expl_subargs, NULL_TREE, tf_none,
15359 /*require_all_args=*/true,
15360 /*use_default_args=*/true);
15361 if (subargs != error_mark_node
15362 && !any_dependent_template_arguments_p (subargs))
15364 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
15365 if (try_one_overload (tparms, targs, tempargs, parm,
15366 elem, strict, sub_strict, addr_p, explain_p)
15367 && (!goodfn || !same_type_p (goodfn, elem)))
15369 goodfn = elem;
15370 ++good;
15373 else if (subargs)
15374 ++ok;
15376 /* If no templates (or more than one) are fully resolved by the
15377 explicit arguments, this template-id is a non-deduced context; it
15378 could still be OK if we deduce all template arguments for the
15379 enclosing call through other arguments. */
15380 if (good != 1)
15381 good = ok;
15383 else if (TREE_CODE (arg) != OVERLOAD
15384 && TREE_CODE (arg) != FUNCTION_DECL)
15385 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15386 -- but the deduction does not succeed because the expression is
15387 not just the function on its own. */
15388 return false;
15389 else
15390 for (; arg; arg = OVL_NEXT (arg))
15391 if (try_one_overload (tparms, targs, tempargs, parm,
15392 TREE_TYPE (OVL_CURRENT (arg)),
15393 strict, sub_strict, addr_p, explain_p)
15394 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
15396 goodfn = OVL_CURRENT (arg);
15397 ++good;
15400 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15401 to function or pointer to member function argument if the set of
15402 overloaded functions does not contain function templates and at most
15403 one of a set of overloaded functions provides a unique match.
15405 So if we found multiple possibilities, we return success but don't
15406 deduce anything. */
15408 if (good == 1)
15410 int i = TREE_VEC_LENGTH (targs);
15411 for (; i--; )
15412 if (TREE_VEC_ELT (tempargs, i))
15413 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
15415 if (good)
15416 return true;
15418 return false;
15421 /* Core DR 115: In contexts where deduction is done and fails, or in
15422 contexts where deduction is not done, if a template argument list is
15423 specified and it, along with any default template arguments, identifies
15424 a single function template specialization, then the template-id is an
15425 lvalue for the function template specialization. */
15427 tree
15428 resolve_nondeduced_context (tree orig_expr)
15430 tree expr, offset, baselink;
15431 bool addr;
15433 if (!type_unknown_p (orig_expr))
15434 return orig_expr;
15436 expr = orig_expr;
15437 addr = false;
15438 offset = NULL_TREE;
15439 baselink = NULL_TREE;
15441 if (TREE_CODE (expr) == ADDR_EXPR)
15443 expr = TREE_OPERAND (expr, 0);
15444 addr = true;
15446 if (TREE_CODE (expr) == OFFSET_REF)
15448 offset = expr;
15449 expr = TREE_OPERAND (expr, 1);
15451 if (BASELINK_P (expr))
15453 baselink = expr;
15454 expr = BASELINK_FUNCTIONS (expr);
15457 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
15459 int good = 0;
15460 tree goodfn = NULL_TREE;
15462 /* If we got some explicit template args, we need to plug them into
15463 the affected templates before we try to unify, in case the
15464 explicit args will completely resolve the templates in question. */
15466 tree expl_subargs = TREE_OPERAND (expr, 1);
15467 tree arg = TREE_OPERAND (expr, 0);
15468 tree badfn = NULL_TREE;
15469 tree badargs = NULL_TREE;
15471 for (; arg; arg = OVL_NEXT (arg))
15473 tree fn = OVL_CURRENT (arg);
15474 tree subargs, elem;
15476 if (TREE_CODE (fn) != TEMPLATE_DECL)
15477 continue;
15479 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15480 expl_subargs, NULL_TREE, tf_none,
15481 /*require_all_args=*/true,
15482 /*use_default_args=*/true);
15483 if (subargs != error_mark_node
15484 && !any_dependent_template_arguments_p (subargs))
15486 elem = instantiate_template (fn, subargs, tf_none);
15487 if (elem == error_mark_node)
15489 badfn = fn;
15490 badargs = subargs;
15492 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15494 goodfn = elem;
15495 ++good;
15499 if (good == 1)
15501 mark_used (goodfn);
15502 expr = goodfn;
15503 if (baselink)
15504 expr = build_baselink (BASELINK_BINFO (baselink),
15505 BASELINK_ACCESS_BINFO (baselink),
15506 expr, BASELINK_OPTYPE (baselink));
15507 if (offset)
15509 tree base
15510 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15511 expr = build_offset_ref (base, expr, addr);
15513 if (addr)
15514 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15515 return expr;
15517 else if (good == 0 && badargs)
15518 /* There were no good options and at least one bad one, so let the
15519 user know what the problem is. */
15520 instantiate_template (badfn, badargs, tf_warning_or_error);
15522 return orig_expr;
15525 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15526 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15527 different overloads deduce different arguments for a given parm.
15528 ADDR_P is true if the expression for which deduction is being
15529 performed was of the form "& fn" rather than simply "fn".
15531 Returns 1 on success. */
15533 static int
15534 try_one_overload (tree tparms,
15535 tree orig_targs,
15536 tree targs,
15537 tree parm,
15538 tree arg,
15539 unification_kind_t strict,
15540 int sub_strict,
15541 bool addr_p,
15542 bool explain_p)
15544 int nargs;
15545 tree tempargs;
15546 int i;
15548 if (arg == error_mark_node)
15549 return 0;
15551 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15552 to function or pointer to member function argument if the set of
15553 overloaded functions does not contain function templates and at most
15554 one of a set of overloaded functions provides a unique match.
15556 So if this is a template, just return success. */
15558 if (uses_template_parms (arg))
15559 return 1;
15561 if (TREE_CODE (arg) == METHOD_TYPE)
15562 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15563 else if (addr_p)
15564 arg = build_pointer_type (arg);
15566 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15568 /* We don't copy orig_targs for this because if we have already deduced
15569 some template args from previous args, unify would complain when we
15570 try to deduce a template parameter for the same argument, even though
15571 there isn't really a conflict. */
15572 nargs = TREE_VEC_LENGTH (targs);
15573 tempargs = make_tree_vec (nargs);
15575 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15576 return 0;
15578 /* First make sure we didn't deduce anything that conflicts with
15579 explicitly specified args. */
15580 for (i = nargs; i--; )
15582 tree elt = TREE_VEC_ELT (tempargs, i);
15583 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15585 if (!elt)
15586 /*NOP*/;
15587 else if (uses_template_parms (elt))
15588 /* Since we're unifying against ourselves, we will fill in
15589 template args used in the function parm list with our own
15590 template parms. Discard them. */
15591 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15592 else if (oldelt && !template_args_equal (oldelt, elt))
15593 return 0;
15596 for (i = nargs; i--; )
15598 tree elt = TREE_VEC_ELT (tempargs, i);
15600 if (elt)
15601 TREE_VEC_ELT (targs, i) = elt;
15604 return 1;
15607 /* PARM is a template class (perhaps with unbound template
15608 parameters). ARG is a fully instantiated type. If ARG can be
15609 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15610 TARGS are as for unify. */
15612 static tree
15613 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15614 bool explain_p)
15616 tree copy_of_targs;
15618 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15619 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15620 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15621 return NULL_TREE;
15623 /* We need to make a new template argument vector for the call to
15624 unify. If we used TARGS, we'd clutter it up with the result of
15625 the attempted unification, even if this class didn't work out.
15626 We also don't want to commit ourselves to all the unifications
15627 we've already done, since unification is supposed to be done on
15628 an argument-by-argument basis. In other words, consider the
15629 following pathological case:
15631 template <int I, int J, int K>
15632 struct S {};
15634 template <int I, int J>
15635 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15637 template <int I, int J, int K>
15638 void f(S<I, J, K>, S<I, I, I>);
15640 void g() {
15641 S<0, 0, 0> s0;
15642 S<0, 1, 2> s2;
15644 f(s0, s2);
15647 Now, by the time we consider the unification involving `s2', we
15648 already know that we must have `f<0, 0, 0>'. But, even though
15649 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15650 because there are two ways to unify base classes of S<0, 1, 2>
15651 with S<I, I, I>. If we kept the already deduced knowledge, we
15652 would reject the possibility I=1. */
15653 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
15655 /* If unification failed, we're done. */
15656 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
15657 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
15658 return NULL_TREE;
15660 return arg;
15663 /* Given a template type PARM and a class type ARG, find the unique
15664 base type in ARG that is an instance of PARM. We do not examine
15665 ARG itself; only its base-classes. If there is not exactly one
15666 appropriate base class, return NULL_TREE. PARM may be the type of
15667 a partial specialization, as well as a plain template type. Used
15668 by unify. */
15670 static enum template_base_result
15671 get_template_base (tree tparms, tree targs, tree parm, tree arg,
15672 bool explain_p, tree *result)
15674 tree rval = NULL_TREE;
15675 tree binfo;
15677 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
15679 binfo = TYPE_BINFO (complete_type (arg));
15680 if (!binfo)
15682 /* The type could not be completed. */
15683 *result = NULL_TREE;
15684 return tbr_incomplete_type;
15687 /* Walk in inheritance graph order. The search order is not
15688 important, and this avoids multiple walks of virtual bases. */
15689 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
15691 tree r = try_class_unification (tparms, targs, parm,
15692 BINFO_TYPE (binfo), explain_p);
15694 if (r)
15696 /* If there is more than one satisfactory baseclass, then:
15698 [temp.deduct.call]
15700 If they yield more than one possible deduced A, the type
15701 deduction fails.
15703 applies. */
15704 if (rval && !same_type_p (r, rval))
15706 *result = NULL_TREE;
15707 return tbr_ambiguous_baseclass;
15710 rval = r;
15714 *result = rval;
15715 return tbr_success;
15718 /* Returns the level of DECL, which declares a template parameter. */
15720 static int
15721 template_decl_level (tree decl)
15723 switch (TREE_CODE (decl))
15725 case TYPE_DECL:
15726 case TEMPLATE_DECL:
15727 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
15729 case PARM_DECL:
15730 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
15732 default:
15733 gcc_unreachable ();
15735 return 0;
15738 /* Decide whether ARG can be unified with PARM, considering only the
15739 cv-qualifiers of each type, given STRICT as documented for unify.
15740 Returns nonzero iff the unification is OK on that basis. */
15742 static int
15743 check_cv_quals_for_unify (int strict, tree arg, tree parm)
15745 int arg_quals = cp_type_quals (arg);
15746 int parm_quals = cp_type_quals (parm);
15748 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15749 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15751 /* Although a CVR qualifier is ignored when being applied to a
15752 substituted template parameter ([8.3.2]/1 for example), that
15753 does not allow us to unify "const T" with "int&" because both
15754 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15755 It is ok when we're allowing additional CV qualifiers
15756 at the outer level [14.8.2.1]/3,1st bullet. */
15757 if ((TREE_CODE (arg) == REFERENCE_TYPE
15758 || TREE_CODE (arg) == FUNCTION_TYPE
15759 || TREE_CODE (arg) == METHOD_TYPE)
15760 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
15761 return 0;
15763 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
15764 && (parm_quals & TYPE_QUAL_RESTRICT))
15765 return 0;
15768 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15769 && (arg_quals & parm_quals) != parm_quals)
15770 return 0;
15772 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
15773 && (parm_quals & arg_quals) != arg_quals)
15774 return 0;
15776 return 1;
15779 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15780 void
15781 template_parm_level_and_index (tree parm, int* level, int* index)
15783 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15784 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15785 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15787 *index = TEMPLATE_TYPE_IDX (parm);
15788 *level = TEMPLATE_TYPE_LEVEL (parm);
15790 else
15792 *index = TEMPLATE_PARM_IDX (parm);
15793 *level = TEMPLATE_PARM_LEVEL (parm);
15797 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15798 do { \
15799 if (unify (TP, TA, P, A, S, EP)) \
15800 return 1; \
15801 } while (0);
15803 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15804 expansion at the end of PACKED_PARMS. Returns 0 if the type
15805 deduction succeeds, 1 otherwise. STRICT is the same as in
15806 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15807 call argument list. We'll need to adjust the arguments to make them
15808 types. SUBR tells us if this is from a recursive call to
15809 type_unification_real, or for comparing two template argument
15810 lists. */
15812 static int
15813 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
15814 tree packed_args, unification_kind_t strict,
15815 bool subr, bool explain_p)
15817 tree parm
15818 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
15819 tree pattern = PACK_EXPANSION_PATTERN (parm);
15820 tree pack, packs = NULL_TREE;
15821 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
15822 int len = TREE_VEC_LENGTH (packed_args);
15824 /* Determine the parameter packs we will be deducing from the
15825 pattern, and record their current deductions. */
15826 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
15827 pack; pack = TREE_CHAIN (pack))
15829 tree parm_pack = TREE_VALUE (pack);
15830 int idx, level;
15832 /* Determine the index and level of this parameter pack. */
15833 template_parm_level_and_index (parm_pack, &level, &idx);
15835 /* Keep track of the parameter packs and their corresponding
15836 argument packs. */
15837 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
15838 TREE_TYPE (packs) = make_tree_vec (len - start);
15841 /* Loop through all of the arguments that have not yet been
15842 unified and unify each with the pattern. */
15843 for (i = start; i < len; i++)
15845 tree parm;
15846 bool any_explicit = false;
15847 tree arg = TREE_VEC_ELT (packed_args, i);
15849 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15850 or the element of its argument pack at the current index if
15851 this argument was explicitly specified. */
15852 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15854 int idx, level;
15855 tree arg, pargs;
15856 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15858 arg = NULL_TREE;
15859 if (TREE_VALUE (pack)
15860 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
15861 && (i < TREE_VEC_LENGTH (pargs)))
15863 any_explicit = true;
15864 arg = TREE_VEC_ELT (pargs, i);
15866 TMPL_ARG (targs, level, idx) = arg;
15869 /* If we had explicit template arguments, substitute them into the
15870 pattern before deduction. */
15871 if (any_explicit)
15873 /* Some arguments might still be unspecified or dependent. */
15874 bool dependent;
15875 ++processing_template_decl;
15876 dependent = any_dependent_template_arguments_p (targs);
15877 if (!dependent)
15878 --processing_template_decl;
15879 parm = tsubst (pattern, targs,
15880 explain_p ? tf_warning_or_error : tf_none,
15881 NULL_TREE);
15882 if (dependent)
15883 --processing_template_decl;
15884 if (parm == error_mark_node)
15885 return 1;
15887 else
15888 parm = pattern;
15890 /* Unify the pattern with the current argument. */
15891 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15892 LOOKUP_IMPLICIT, explain_p))
15893 return 1;
15895 /* For each parameter pack, collect the deduced value. */
15896 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15898 int idx, level;
15899 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15901 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
15902 TMPL_ARG (targs, level, idx);
15906 /* Verify that the results of unification with the parameter packs
15907 produce results consistent with what we've seen before, and make
15908 the deduced argument packs available. */
15909 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15911 tree old_pack = TREE_VALUE (pack);
15912 tree new_args = TREE_TYPE (pack);
15913 int i, len = TREE_VEC_LENGTH (new_args);
15914 int idx, level;
15915 bool nondeduced_p = false;
15917 /* By default keep the original deduced argument pack.
15918 If necessary, more specific code is going to update the
15919 resulting deduced argument later down in this function. */
15920 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15921 TMPL_ARG (targs, level, idx) = old_pack;
15923 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15924 actually deduce anything. */
15925 for (i = 0; i < len && !nondeduced_p; ++i)
15926 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
15927 nondeduced_p = true;
15928 if (nondeduced_p)
15929 continue;
15931 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
15933 /* If we had fewer function args than explicit template args,
15934 just use the explicits. */
15935 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15936 int explicit_len = TREE_VEC_LENGTH (explicit_args);
15937 if (len < explicit_len)
15938 new_args = explicit_args;
15941 if (!old_pack)
15943 tree result;
15944 /* Build the deduced *_ARGUMENT_PACK. */
15945 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
15947 result = make_node (NONTYPE_ARGUMENT_PACK);
15948 TREE_TYPE (result) =
15949 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
15950 TREE_CONSTANT (result) = 1;
15952 else
15953 result = cxx_make_type (TYPE_ARGUMENT_PACK);
15955 SET_ARGUMENT_PACK_ARGS (result, new_args);
15957 /* Note the deduced argument packs for this parameter
15958 pack. */
15959 TMPL_ARG (targs, level, idx) = result;
15961 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
15962 && (ARGUMENT_PACK_ARGS (old_pack)
15963 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
15965 /* We only had the explicitly-provided arguments before, but
15966 now we have a complete set of arguments. */
15967 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15969 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
15970 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
15971 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
15973 else
15975 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
15976 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
15978 if (!comp_template_args_with_info (old_args, new_args,
15979 &bad_old_arg, &bad_new_arg))
15980 /* Inconsistent unification of this parameter pack. */
15981 return unify_parameter_pack_inconsistent (explain_p,
15982 bad_old_arg,
15983 bad_new_arg);
15987 return unify_success (explain_p);
15990 /* Deduce the value of template parameters. TPARMS is the (innermost)
15991 set of template parameters to a template. TARGS is the bindings
15992 for those template parameters, as determined thus far; TARGS may
15993 include template arguments for outer levels of template parameters
15994 as well. PARM is a parameter to a template function, or a
15995 subcomponent of that parameter; ARG is the corresponding argument.
15996 This function attempts to match PARM with ARG in a manner
15997 consistent with the existing assignments in TARGS. If more values
15998 are deduced, then TARGS is updated.
16000 Returns 0 if the type deduction succeeds, 1 otherwise. The
16001 parameter STRICT is a bitwise or of the following flags:
16003 UNIFY_ALLOW_NONE:
16004 Require an exact match between PARM and ARG.
16005 UNIFY_ALLOW_MORE_CV_QUAL:
16006 Allow the deduced ARG to be more cv-qualified (by qualification
16007 conversion) than ARG.
16008 UNIFY_ALLOW_LESS_CV_QUAL:
16009 Allow the deduced ARG to be less cv-qualified than ARG.
16010 UNIFY_ALLOW_DERIVED:
16011 Allow the deduced ARG to be a template base class of ARG,
16012 or a pointer to a template base class of the type pointed to by
16013 ARG.
16014 UNIFY_ALLOW_INTEGER:
16015 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16016 case for more information.
16017 UNIFY_ALLOW_OUTER_LEVEL:
16018 This is the outermost level of a deduction. Used to determine validity
16019 of qualification conversions. A valid qualification conversion must
16020 have const qualified pointers leading up to the inner type which
16021 requires additional CV quals, except at the outer level, where const
16022 is not required [conv.qual]. It would be normal to set this flag in
16023 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16024 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16025 This is the outermost level of a deduction, and PARM can be more CV
16026 qualified at this point.
16027 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16028 This is the outermost level of a deduction, and PARM can be less CV
16029 qualified at this point. */
16031 static int
16032 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
16033 bool explain_p)
16035 int idx;
16036 tree targ;
16037 tree tparm;
16038 int strict_in = strict;
16040 /* I don't think this will do the right thing with respect to types.
16041 But the only case I've seen it in so far has been array bounds, where
16042 signedness is the only information lost, and I think that will be
16043 okay. */
16044 while (TREE_CODE (parm) == NOP_EXPR)
16045 parm = TREE_OPERAND (parm, 0);
16047 if (arg == error_mark_node)
16048 return unify_invalid (explain_p);
16049 if (arg == unknown_type_node
16050 || arg == init_list_type_node)
16051 /* We can't deduce anything from this, but we might get all the
16052 template args from other function args. */
16053 return unify_success (explain_p);
16055 /* If PARM uses template parameters, then we can't bail out here,
16056 even if ARG == PARM, since we won't record unifications for the
16057 template parameters. We might need them if we're trying to
16058 figure out which of two things is more specialized. */
16059 if (arg == parm && !uses_template_parms (parm))
16060 return unify_success (explain_p);
16062 /* Handle init lists early, so the rest of the function can assume
16063 we're dealing with a type. */
16064 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
16066 tree elt, elttype;
16067 unsigned i;
16068 tree orig_parm = parm;
16070 /* Replace T with std::initializer_list<T> for deduction. */
16071 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16072 && flag_deduce_init_list)
16073 parm = listify (parm);
16075 if (!is_std_init_list (parm))
16076 /* We can only deduce from an initializer list argument if the
16077 parameter is std::initializer_list; otherwise this is a
16078 non-deduced context. */
16079 return unify_success (explain_p);
16081 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
16083 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
16085 int elt_strict = strict;
16087 if (elt == error_mark_node)
16088 return unify_invalid (explain_p);
16090 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
16092 tree type = TREE_TYPE (elt);
16093 /* It should only be possible to get here for a call. */
16094 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
16095 elt_strict |= maybe_adjust_types_for_deduction
16096 (DEDUCE_CALL, &elttype, &type, elt);
16097 elt = type;
16100 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
16101 explain_p);
16104 /* If the std::initializer_list<T> deduction worked, replace the
16105 deduced A with std::initializer_list<A>. */
16106 if (orig_parm != parm)
16108 idx = TEMPLATE_TYPE_IDX (orig_parm);
16109 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16110 targ = listify (targ);
16111 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
16113 return unify_success (explain_p);
16116 /* Immediately reject some pairs that won't unify because of
16117 cv-qualification mismatches. */
16118 if (TREE_CODE (arg) == TREE_CODE (parm)
16119 && TYPE_P (arg)
16120 /* It is the elements of the array which hold the cv quals of an array
16121 type, and the elements might be template type parms. We'll check
16122 when we recurse. */
16123 && TREE_CODE (arg) != ARRAY_TYPE
16124 /* We check the cv-qualifiers when unifying with template type
16125 parameters below. We want to allow ARG `const T' to unify with
16126 PARM `T' for example, when computing which of two templates
16127 is more specialized, for example. */
16128 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
16129 && !check_cv_quals_for_unify (strict_in, arg, parm))
16130 return unify_cv_qual_mismatch (explain_p, parm, arg);
16132 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
16133 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
16134 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
16135 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
16136 strict &= ~UNIFY_ALLOW_DERIVED;
16137 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16138 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
16140 switch (TREE_CODE (parm))
16142 case TYPENAME_TYPE:
16143 case SCOPE_REF:
16144 case UNBOUND_CLASS_TEMPLATE:
16145 /* In a type which contains a nested-name-specifier, template
16146 argument values cannot be deduced for template parameters used
16147 within the nested-name-specifier. */
16148 return unify_success (explain_p);
16150 case TEMPLATE_TYPE_PARM:
16151 case TEMPLATE_TEMPLATE_PARM:
16152 case BOUND_TEMPLATE_TEMPLATE_PARM:
16153 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16154 if (tparm == error_mark_node)
16155 return unify_invalid (explain_p);
16157 if (TEMPLATE_TYPE_LEVEL (parm)
16158 != template_decl_level (tparm))
16159 /* The PARM is not one we're trying to unify. Just check
16160 to see if it matches ARG. */
16162 if (TREE_CODE (arg) == TREE_CODE (parm)
16163 && (is_auto (parm) ? is_auto (arg)
16164 : same_type_p (parm, arg)))
16165 return unify_success (explain_p);
16166 else
16167 return unify_type_mismatch (explain_p, parm, arg);
16169 idx = TEMPLATE_TYPE_IDX (parm);
16170 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16171 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
16172 if (tparm == error_mark_node)
16173 return unify_invalid (explain_p);
16175 /* Check for mixed types and values. */
16176 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16177 && TREE_CODE (tparm) != TYPE_DECL)
16178 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16179 && TREE_CODE (tparm) != TEMPLATE_DECL))
16180 gcc_unreachable ();
16182 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16184 /* ARG must be constructed from a template class or a template
16185 template parameter. */
16186 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
16187 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
16188 return unify_template_deduction_failure (explain_p, parm, arg);
16191 tree parmvec = TYPE_TI_ARGS (parm);
16192 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
16193 tree full_argvec = add_to_template_args (targs, argvec);
16194 tree parm_parms
16195 = DECL_INNERMOST_TEMPLATE_PARMS
16196 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
16197 int i, len;
16198 int parm_variadic_p = 0;
16200 /* The resolution to DR150 makes clear that default
16201 arguments for an N-argument may not be used to bind T
16202 to a template template parameter with fewer than N
16203 parameters. It is not safe to permit the binding of
16204 default arguments as an extension, as that may change
16205 the meaning of a conforming program. Consider:
16207 struct Dense { static const unsigned int dim = 1; };
16209 template <template <typename> class View,
16210 typename Block>
16211 void operator+(float, View<Block> const&);
16213 template <typename Block,
16214 unsigned int Dim = Block::dim>
16215 struct Lvalue_proxy { operator float() const; };
16217 void
16218 test_1d (void) {
16219 Lvalue_proxy<Dense> p;
16220 float b;
16221 b + p;
16224 Here, if Lvalue_proxy is permitted to bind to View, then
16225 the global operator+ will be used; if they are not, the
16226 Lvalue_proxy will be converted to float. */
16227 if (coerce_template_parms (parm_parms,
16228 full_argvec,
16229 TYPE_TI_TEMPLATE (parm),
16230 (explain_p
16231 ? tf_warning_or_error
16232 : tf_none),
16233 /*require_all_args=*/true,
16234 /*use_default_args=*/false)
16235 == error_mark_node)
16236 return 1;
16238 /* Deduce arguments T, i from TT<T> or TT<i>.
16239 We check each element of PARMVEC and ARGVEC individually
16240 rather than the whole TREE_VEC since they can have
16241 different number of elements. */
16243 parmvec = expand_template_argument_pack (parmvec);
16244 argvec = expand_template_argument_pack (argvec);
16246 len = TREE_VEC_LENGTH (parmvec);
16248 /* Check if the parameters end in a pack, making them
16249 variadic. */
16250 if (len > 0
16251 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
16252 parm_variadic_p = 1;
16254 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
16255 return unify_too_few_arguments (explain_p,
16256 TREE_VEC_LENGTH (argvec), len);
16258 for (i = 0; i < len - parm_variadic_p; ++i)
16260 RECUR_AND_CHECK_FAILURE (tparms, targs,
16261 TREE_VEC_ELT (parmvec, i),
16262 TREE_VEC_ELT (argvec, i),
16263 UNIFY_ALLOW_NONE, explain_p);
16266 if (parm_variadic_p
16267 && unify_pack_expansion (tparms, targs,
16268 parmvec, argvec,
16269 DEDUCE_EXACT,
16270 /*subr=*/true, explain_p))
16271 return 1;
16273 arg = TYPE_TI_TEMPLATE (arg);
16275 /* Fall through to deduce template name. */
16278 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16279 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16281 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16283 /* Simple cases: Value already set, does match or doesn't. */
16284 if (targ != NULL_TREE && template_args_equal (targ, arg))
16285 return unify_success (explain_p);
16286 else if (targ)
16287 return unify_inconsistency (explain_p, parm, targ, arg);
16289 else
16291 /* If PARM is `const T' and ARG is only `int', we don't have
16292 a match unless we are allowing additional qualification.
16293 If ARG is `const int' and PARM is just `T' that's OK;
16294 that binds `const int' to `T'. */
16295 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
16296 arg, parm))
16297 return unify_cv_qual_mismatch (explain_p, parm, arg);
16299 /* Consider the case where ARG is `const volatile int' and
16300 PARM is `const T'. Then, T should be `volatile int'. */
16301 arg = cp_build_qualified_type_real
16302 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
16303 if (arg == error_mark_node)
16304 return unify_invalid (explain_p);
16306 /* Simple cases: Value already set, does match or doesn't. */
16307 if (targ != NULL_TREE && same_type_p (targ, arg))
16308 return unify_success (explain_p);
16309 else if (targ)
16310 return unify_inconsistency (explain_p, parm, targ, arg);
16312 /* Make sure that ARG is not a variable-sized array. (Note
16313 that were talking about variable-sized arrays (like
16314 `int[n]'), rather than arrays of unknown size (like
16315 `int[]').) We'll get very confused by such a type since
16316 the bound of the array is not constant, and therefore
16317 not mangleable. Besides, such types are not allowed in
16318 ISO C++, so we can do as we please here. We do allow
16319 them for 'auto' deduction, since that isn't ABI-exposed. */
16320 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
16321 return unify_vla_arg (explain_p, arg);
16323 /* Strip typedefs as in convert_template_argument. */
16324 arg = canonicalize_type_argument (arg, tf_none);
16327 /* If ARG is a parameter pack or an expansion, we cannot unify
16328 against it unless PARM is also a parameter pack. */
16329 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16330 && !template_parameter_pack_p (parm))
16331 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16333 /* If the argument deduction results is a METHOD_TYPE,
16334 then there is a problem.
16335 METHOD_TYPE doesn't map to any real C++ type the result of
16336 the deduction can not be of that type. */
16337 if (TREE_CODE (arg) == METHOD_TYPE)
16338 return unify_method_type_error (explain_p, arg);
16340 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16341 return unify_success (explain_p);
16343 case TEMPLATE_PARM_INDEX:
16344 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16345 if (tparm == error_mark_node)
16346 return unify_invalid (explain_p);
16348 if (TEMPLATE_PARM_LEVEL (parm)
16349 != template_decl_level (tparm))
16351 /* The PARM is not one we're trying to unify. Just check
16352 to see if it matches ARG. */
16353 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
16354 && cp_tree_equal (parm, arg));
16355 if (result)
16356 unify_expression_unequal (explain_p, parm, arg);
16357 return result;
16360 idx = TEMPLATE_PARM_IDX (parm);
16361 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16363 if (targ)
16365 int x = !cp_tree_equal (targ, arg);
16366 if (x)
16367 unify_inconsistency (explain_p, parm, targ, arg);
16368 return x;
16371 /* [temp.deduct.type] If, in the declaration of a function template
16372 with a non-type template-parameter, the non-type
16373 template-parameter is used in an expression in the function
16374 parameter-list and, if the corresponding template-argument is
16375 deduced, the template-argument type shall match the type of the
16376 template-parameter exactly, except that a template-argument
16377 deduced from an array bound may be of any integral type.
16378 The non-type parameter might use already deduced type parameters. */
16379 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
16380 if (!TREE_TYPE (arg))
16381 /* Template-parameter dependent expression. Just accept it for now.
16382 It will later be processed in convert_template_argument. */
16384 else if (same_type_p (TREE_TYPE (arg), tparm))
16385 /* OK */;
16386 else if ((strict & UNIFY_ALLOW_INTEGER)
16387 && (TREE_CODE (tparm) == INTEGER_TYPE
16388 || TREE_CODE (tparm) == BOOLEAN_TYPE))
16389 /* Convert the ARG to the type of PARM; the deduced non-type
16390 template argument must exactly match the types of the
16391 corresponding parameter. */
16392 arg = fold (build_nop (tparm, arg));
16393 else if (uses_template_parms (tparm))
16394 /* We haven't deduced the type of this parameter yet. Try again
16395 later. */
16396 return unify_success (explain_p);
16397 else
16398 return unify_type_mismatch (explain_p, tparm, arg);
16400 /* If ARG is a parameter pack or an expansion, we cannot unify
16401 against it unless PARM is also a parameter pack. */
16402 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16403 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
16404 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16406 arg = strip_typedefs_expr (arg);
16407 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16408 return unify_success (explain_p);
16410 case PTRMEM_CST:
16412 /* A pointer-to-member constant can be unified only with
16413 another constant. */
16414 if (TREE_CODE (arg) != PTRMEM_CST)
16415 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
16417 /* Just unify the class member. It would be useless (and possibly
16418 wrong, depending on the strict flags) to unify also
16419 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16420 arg refer to the same variable, even if through different
16421 classes. For instance:
16423 struct A { int x; };
16424 struct B : A { };
16426 Unification of &A::x and &B::x must succeed. */
16427 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
16428 PTRMEM_CST_MEMBER (arg), strict, explain_p);
16431 case POINTER_TYPE:
16433 if (TREE_CODE (arg) != POINTER_TYPE)
16434 return unify_type_mismatch (explain_p, parm, arg);
16436 /* [temp.deduct.call]
16438 A can be another pointer or pointer to member type that can
16439 be converted to the deduced A via a qualification
16440 conversion (_conv.qual_).
16442 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16443 This will allow for additional cv-qualification of the
16444 pointed-to types if appropriate. */
16446 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16447 /* The derived-to-base conversion only persists through one
16448 level of pointers. */
16449 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16451 return unify (tparms, targs, TREE_TYPE (parm),
16452 TREE_TYPE (arg), strict, explain_p);
16455 case REFERENCE_TYPE:
16456 if (TREE_CODE (arg) != REFERENCE_TYPE)
16457 return unify_type_mismatch (explain_p, parm, arg);
16458 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16459 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16461 case ARRAY_TYPE:
16462 if (TREE_CODE (arg) != ARRAY_TYPE)
16463 return unify_type_mismatch (explain_p, parm, arg);
16464 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16465 != (TYPE_DOMAIN (arg) == NULL_TREE))
16466 return unify_type_mismatch (explain_p, parm, arg);
16467 if (TYPE_DOMAIN (parm) != NULL_TREE)
16469 tree parm_max;
16470 tree arg_max;
16471 bool parm_cst;
16472 bool arg_cst;
16474 /* Our representation of array types uses "N - 1" as the
16475 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16476 not an integer constant. We cannot unify arbitrarily
16477 complex expressions, so we eliminate the MINUS_EXPRs
16478 here. */
16479 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16480 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16481 if (!parm_cst)
16483 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16484 parm_max = TREE_OPERAND (parm_max, 0);
16486 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16487 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16488 if (!arg_cst)
16490 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16491 trying to unify the type of a variable with the type
16492 of a template parameter. For example:
16494 template <unsigned int N>
16495 void f (char (&) [N]);
16496 int g();
16497 void h(int i) {
16498 char a[g(i)];
16499 f(a);
16502 Here, the type of the ARG will be "int [g(i)]", and
16503 may be a SAVE_EXPR, etc. */
16504 if (TREE_CODE (arg_max) != MINUS_EXPR)
16505 return unify_vla_arg (explain_p, arg);
16506 arg_max = TREE_OPERAND (arg_max, 0);
16509 /* If only one of the bounds used a MINUS_EXPR, compensate
16510 by adding one to the other bound. */
16511 if (parm_cst && !arg_cst)
16512 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16513 integer_type_node,
16514 parm_max,
16515 integer_one_node);
16516 else if (arg_cst && !parm_cst)
16517 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16518 integer_type_node,
16519 arg_max,
16520 integer_one_node);
16522 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16523 UNIFY_ALLOW_INTEGER, explain_p);
16525 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16526 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16528 case REAL_TYPE:
16529 case COMPLEX_TYPE:
16530 case VECTOR_TYPE:
16531 case INTEGER_TYPE:
16532 case BOOLEAN_TYPE:
16533 case ENUMERAL_TYPE:
16534 case VOID_TYPE:
16535 case NULLPTR_TYPE:
16536 if (TREE_CODE (arg) != TREE_CODE (parm))
16537 return unify_type_mismatch (explain_p, parm, arg);
16539 /* We have already checked cv-qualification at the top of the
16540 function. */
16541 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16542 return unify_type_mismatch (explain_p, parm, arg);
16544 /* As far as unification is concerned, this wins. Later checks
16545 will invalidate it if necessary. */
16546 return unify_success (explain_p);
16548 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16549 /* Type INTEGER_CST can come from ordinary constant template args. */
16550 case INTEGER_CST:
16551 while (TREE_CODE (arg) == NOP_EXPR)
16552 arg = TREE_OPERAND (arg, 0);
16554 if (TREE_CODE (arg) != INTEGER_CST)
16555 return unify_template_argument_mismatch (explain_p, parm, arg);
16556 return (tree_int_cst_equal (parm, arg)
16557 ? unify_success (explain_p)
16558 : unify_template_argument_mismatch (explain_p, parm, arg));
16560 case TREE_VEC:
16562 int i, len, argslen;
16563 int parm_variadic_p = 0;
16565 if (TREE_CODE (arg) != TREE_VEC)
16566 return unify_template_argument_mismatch (explain_p, parm, arg);
16568 len = TREE_VEC_LENGTH (parm);
16569 argslen = TREE_VEC_LENGTH (arg);
16571 /* Check for pack expansions in the parameters. */
16572 for (i = 0; i < len; ++i)
16574 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
16576 if (i == len - 1)
16577 /* We can unify against something with a trailing
16578 parameter pack. */
16579 parm_variadic_p = 1;
16580 else
16581 /* [temp.deduct.type]/9: If the template argument list of
16582 P contains a pack expansion that is not the last
16583 template argument, the entire template argument list
16584 is a non-deduced context. */
16585 return unify_success (explain_p);
16589 /* If we don't have enough arguments to satisfy the parameters
16590 (not counting the pack expression at the end), or we have
16591 too many arguments for a parameter list that doesn't end in
16592 a pack expression, we can't unify. */
16593 if (parm_variadic_p
16594 ? argslen < len - parm_variadic_p
16595 : argslen != len)
16596 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
16598 /* Unify all of the parameters that precede the (optional)
16599 pack expression. */
16600 for (i = 0; i < len - parm_variadic_p; ++i)
16602 RECUR_AND_CHECK_FAILURE (tparms, targs,
16603 TREE_VEC_ELT (parm, i),
16604 TREE_VEC_ELT (arg, i),
16605 UNIFY_ALLOW_NONE, explain_p);
16607 if (parm_variadic_p)
16608 return unify_pack_expansion (tparms, targs, parm, arg,
16609 DEDUCE_EXACT,
16610 /*subr=*/true, explain_p);
16611 return unify_success (explain_p);
16614 case RECORD_TYPE:
16615 case UNION_TYPE:
16616 if (TREE_CODE (arg) != TREE_CODE (parm))
16617 return unify_type_mismatch (explain_p, parm, arg);
16619 if (TYPE_PTRMEMFUNC_P (parm))
16621 if (!TYPE_PTRMEMFUNC_P (arg))
16622 return unify_type_mismatch (explain_p, parm, arg);
16624 return unify (tparms, targs,
16625 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16626 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16627 strict, explain_p);
16630 if (CLASSTYPE_TEMPLATE_INFO (parm))
16632 tree t = NULL_TREE;
16634 if (strict_in & UNIFY_ALLOW_DERIVED)
16636 /* First, we try to unify the PARM and ARG directly. */
16637 t = try_class_unification (tparms, targs,
16638 parm, arg, explain_p);
16640 if (!t)
16642 /* Fallback to the special case allowed in
16643 [temp.deduct.call]:
16645 If P is a class, and P has the form
16646 template-id, then A can be a derived class of
16647 the deduced A. Likewise, if P is a pointer to
16648 a class of the form template-id, A can be a
16649 pointer to a derived class pointed to by the
16650 deduced A. */
16651 enum template_base_result r;
16652 r = get_template_base (tparms, targs, parm, arg,
16653 explain_p, &t);
16655 if (!t)
16656 return unify_no_common_base (explain_p, r, parm, arg);
16659 else if (CLASSTYPE_TEMPLATE_INFO (arg)
16660 && (CLASSTYPE_TI_TEMPLATE (parm)
16661 == CLASSTYPE_TI_TEMPLATE (arg)))
16662 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16663 Then, we should unify `int' and `U'. */
16664 t = arg;
16665 else
16666 /* There's no chance of unification succeeding. */
16667 return unify_type_mismatch (explain_p, parm, arg);
16669 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
16670 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
16672 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
16673 return unify_type_mismatch (explain_p, parm, arg);
16674 return unify_success (explain_p);
16676 case METHOD_TYPE:
16677 case FUNCTION_TYPE:
16679 unsigned int nargs;
16680 tree *args;
16681 tree a;
16682 unsigned int i;
16684 if (TREE_CODE (arg) != TREE_CODE (parm))
16685 return unify_type_mismatch (explain_p, parm, arg);
16687 /* CV qualifications for methods can never be deduced, they must
16688 match exactly. We need to check them explicitly here,
16689 because type_unification_real treats them as any other
16690 cv-qualified parameter. */
16691 if (TREE_CODE (parm) == METHOD_TYPE
16692 && (!check_cv_quals_for_unify
16693 (UNIFY_ALLOW_NONE,
16694 class_of_this_parm (arg),
16695 class_of_this_parm (parm))))
16696 return unify_cv_qual_mismatch (explain_p, parm, arg);
16698 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
16699 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
16701 nargs = list_length (TYPE_ARG_TYPES (arg));
16702 args = XALLOCAVEC (tree, nargs);
16703 for (a = TYPE_ARG_TYPES (arg), i = 0;
16704 a != NULL_TREE && a != void_list_node;
16705 a = TREE_CHAIN (a), ++i)
16706 args[i] = TREE_VALUE (a);
16707 nargs = i;
16709 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
16710 args, nargs, 1, DEDUCE_EXACT,
16711 LOOKUP_NORMAL, explain_p);
16714 case OFFSET_TYPE:
16715 /* Unify a pointer to member with a pointer to member function, which
16716 deduces the type of the member as a function type. */
16717 if (TYPE_PTRMEMFUNC_P (arg))
16719 tree method_type;
16720 tree fntype;
16722 /* Check top-level cv qualifiers */
16723 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
16724 return unify_cv_qual_mismatch (explain_p, parm, arg);
16726 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16727 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
16728 UNIFY_ALLOW_NONE, explain_p);
16730 /* Determine the type of the function we are unifying against. */
16731 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
16732 fntype =
16733 build_function_type (TREE_TYPE (method_type),
16734 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
16736 /* Extract the cv-qualifiers of the member function from the
16737 implicit object parameter and place them on the function
16738 type to be restored later. */
16739 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
16740 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
16743 if (TREE_CODE (arg) != OFFSET_TYPE)
16744 return unify_type_mismatch (explain_p, parm, arg);
16745 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16746 TYPE_OFFSET_BASETYPE (arg),
16747 UNIFY_ALLOW_NONE, explain_p);
16748 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16749 strict, explain_p);
16751 case CONST_DECL:
16752 if (DECL_TEMPLATE_PARM_P (parm))
16753 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
16754 if (arg != integral_constant_value (parm))
16755 return unify_template_argument_mismatch (explain_p, parm, arg);
16756 return unify_success (explain_p);
16758 case FIELD_DECL:
16759 case TEMPLATE_DECL:
16760 /* Matched cases are handled by the ARG == PARM test above. */
16761 return unify_template_argument_mismatch (explain_p, parm, arg);
16763 case VAR_DECL:
16764 /* A non-type template parameter that is a variable should be a
16765 an integral constant, in which case, it whould have been
16766 folded into its (constant) value. So we should not be getting
16767 a variable here. */
16768 gcc_unreachable ();
16770 case TYPE_ARGUMENT_PACK:
16771 case NONTYPE_ARGUMENT_PACK:
16772 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
16773 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
16775 case TYPEOF_TYPE:
16776 case DECLTYPE_TYPE:
16777 case UNDERLYING_TYPE:
16778 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16779 or UNDERLYING_TYPE nodes. */
16780 return unify_success (explain_p);
16782 case ERROR_MARK:
16783 /* Unification fails if we hit an error node. */
16784 return unify_invalid (explain_p);
16786 default:
16787 /* An unresolved overload is a nondeduced context. */
16788 if (is_overloaded_fn (parm) || type_unknown_p (parm))
16789 return unify_success (explain_p);
16790 gcc_assert (EXPR_P (parm));
16792 /* We must be looking at an expression. This can happen with
16793 something like:
16795 template <int I>
16796 void foo(S<I>, S<I + 2>);
16798 This is a "nondeduced context":
16800 [deduct.type]
16802 The nondeduced contexts are:
16804 --A type that is a template-id in which one or more of
16805 the template-arguments is an expression that references
16806 a template-parameter.
16808 In these cases, we assume deduction succeeded, but don't
16809 actually infer any unifications. */
16811 if (!uses_template_parms (parm)
16812 && !template_args_equal (parm, arg))
16813 return unify_expression_unequal (explain_p, parm, arg);
16814 else
16815 return unify_success (explain_p);
16818 #undef RECUR_AND_CHECK_FAILURE
16820 /* Note that DECL can be defined in this translation unit, if
16821 required. */
16823 static void
16824 mark_definable (tree decl)
16826 tree clone;
16827 DECL_NOT_REALLY_EXTERN (decl) = 1;
16828 FOR_EACH_CLONE (clone, decl)
16829 DECL_NOT_REALLY_EXTERN (clone) = 1;
16832 /* Called if RESULT is explicitly instantiated, or is a member of an
16833 explicitly instantiated class. */
16835 void
16836 mark_decl_instantiated (tree result, int extern_p)
16838 SET_DECL_EXPLICIT_INSTANTIATION (result);
16840 /* If this entity has already been written out, it's too late to
16841 make any modifications. */
16842 if (TREE_ASM_WRITTEN (result))
16843 return;
16845 if (TREE_CODE (result) != FUNCTION_DECL)
16846 /* The TREE_PUBLIC flag for function declarations will have been
16847 set correctly by tsubst. */
16848 TREE_PUBLIC (result) = 1;
16850 /* This might have been set by an earlier implicit instantiation. */
16851 DECL_COMDAT (result) = 0;
16853 if (extern_p)
16854 DECL_NOT_REALLY_EXTERN (result) = 0;
16855 else
16857 mark_definable (result);
16858 /* Always make artificials weak. */
16859 if (DECL_ARTIFICIAL (result) && flag_weak)
16860 comdat_linkage (result);
16861 /* For WIN32 we also want to put explicit instantiations in
16862 linkonce sections. */
16863 else if (TREE_PUBLIC (result))
16864 maybe_make_one_only (result);
16867 /* If EXTERN_P, then this function will not be emitted -- unless
16868 followed by an explicit instantiation, at which point its linkage
16869 will be adjusted. If !EXTERN_P, then this function will be
16870 emitted here. In neither circumstance do we want
16871 import_export_decl to adjust the linkage. */
16872 DECL_INTERFACE_KNOWN (result) = 1;
16875 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16876 important template arguments. If any are missing, we check whether
16877 they're important by using error_mark_node for substituting into any
16878 args that were used for partial ordering (the ones between ARGS and END)
16879 and seeing if it bubbles up. */
16881 static bool
16882 check_undeduced_parms (tree targs, tree args, tree end)
16884 bool found = false;
16885 int i;
16886 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
16887 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
16889 found = true;
16890 TREE_VEC_ELT (targs, i) = error_mark_node;
16892 if (found)
16894 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
16895 if (substed == error_mark_node)
16896 return true;
16898 return false;
16901 /* Given two function templates PAT1 and PAT2, return:
16903 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16904 -1 if PAT2 is more specialized than PAT1.
16905 0 if neither is more specialized.
16907 LEN indicates the number of parameters we should consider
16908 (defaulted parameters should not be considered).
16910 The 1998 std underspecified function template partial ordering, and
16911 DR214 addresses the issue. We take pairs of arguments, one from
16912 each of the templates, and deduce them against each other. One of
16913 the templates will be more specialized if all the *other*
16914 template's arguments deduce against its arguments and at least one
16915 of its arguments *does* *not* deduce against the other template's
16916 corresponding argument. Deduction is done as for class templates.
16917 The arguments used in deduction have reference and top level cv
16918 qualifiers removed. Iff both arguments were originally reference
16919 types *and* deduction succeeds in both directions, the template
16920 with the more cv-qualified argument wins for that pairing (if
16921 neither is more cv-qualified, they both are equal). Unlike regular
16922 deduction, after all the arguments have been deduced in this way,
16923 we do *not* verify the deduced template argument values can be
16924 substituted into non-deduced contexts.
16926 The logic can be a bit confusing here, because we look at deduce1 and
16927 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16928 can find template arguments for pat1 to make arg1 look like arg2, that
16929 means that arg2 is at least as specialized as arg1. */
16932 more_specialized_fn (tree pat1, tree pat2, int len)
16934 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
16935 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
16936 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
16937 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
16938 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
16939 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
16940 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
16941 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
16942 tree origs1, origs2;
16943 bool lose1 = false;
16944 bool lose2 = false;
16946 /* Remove the this parameter from non-static member functions. If
16947 one is a non-static member function and the other is not a static
16948 member function, remove the first parameter from that function
16949 also. This situation occurs for operator functions where we
16950 locate both a member function (with this pointer) and non-member
16951 operator (with explicit first operand). */
16952 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
16954 len--; /* LEN is the number of significant arguments for DECL1 */
16955 args1 = TREE_CHAIN (args1);
16956 if (!DECL_STATIC_FUNCTION_P (decl2))
16957 args2 = TREE_CHAIN (args2);
16959 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
16961 args2 = TREE_CHAIN (args2);
16962 if (!DECL_STATIC_FUNCTION_P (decl1))
16964 len--;
16965 args1 = TREE_CHAIN (args1);
16969 /* If only one is a conversion operator, they are unordered. */
16970 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
16971 return 0;
16973 /* Consider the return type for a conversion function */
16974 if (DECL_CONV_FN_P (decl1))
16976 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
16977 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
16978 len++;
16981 processing_template_decl++;
16983 origs1 = args1;
16984 origs2 = args2;
16986 while (len--
16987 /* Stop when an ellipsis is seen. */
16988 && args1 != NULL_TREE && args2 != NULL_TREE)
16990 tree arg1 = TREE_VALUE (args1);
16991 tree arg2 = TREE_VALUE (args2);
16992 int deduce1, deduce2;
16993 int quals1 = -1;
16994 int quals2 = -1;
16996 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16997 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16999 /* When both arguments are pack expansions, we need only
17000 unify the patterns themselves. */
17001 arg1 = PACK_EXPANSION_PATTERN (arg1);
17002 arg2 = PACK_EXPANSION_PATTERN (arg2);
17004 /* This is the last comparison we need to do. */
17005 len = 0;
17008 if (TREE_CODE (arg1) == REFERENCE_TYPE)
17010 arg1 = TREE_TYPE (arg1);
17011 quals1 = cp_type_quals (arg1);
17014 if (TREE_CODE (arg2) == REFERENCE_TYPE)
17016 arg2 = TREE_TYPE (arg2);
17017 quals2 = cp_type_quals (arg2);
17020 arg1 = TYPE_MAIN_VARIANT (arg1);
17021 arg2 = TYPE_MAIN_VARIANT (arg2);
17023 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
17025 int i, len2 = list_length (args2);
17026 tree parmvec = make_tree_vec (1);
17027 tree argvec = make_tree_vec (len2);
17028 tree ta = args2;
17030 /* Setup the parameter vector, which contains only ARG1. */
17031 TREE_VEC_ELT (parmvec, 0) = arg1;
17033 /* Setup the argument vector, which contains the remaining
17034 arguments. */
17035 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
17036 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17038 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
17039 argvec, DEDUCE_EXACT,
17040 /*subr=*/true, /*explain_p=*/false)
17041 == 0);
17043 /* We cannot deduce in the other direction, because ARG1 is
17044 a pack expansion but ARG2 is not. */
17045 deduce2 = 0;
17047 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17049 int i, len1 = list_length (args1);
17050 tree parmvec = make_tree_vec (1);
17051 tree argvec = make_tree_vec (len1);
17052 tree ta = args1;
17054 /* Setup the parameter vector, which contains only ARG1. */
17055 TREE_VEC_ELT (parmvec, 0) = arg2;
17057 /* Setup the argument vector, which contains the remaining
17058 arguments. */
17059 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
17060 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17062 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
17063 argvec, DEDUCE_EXACT,
17064 /*subr=*/true, /*explain_p=*/false)
17065 == 0);
17067 /* We cannot deduce in the other direction, because ARG2 is
17068 a pack expansion but ARG1 is not.*/
17069 deduce1 = 0;
17072 else
17074 /* The normal case, where neither argument is a pack
17075 expansion. */
17076 deduce1 = (unify (tparms1, targs1, arg1, arg2,
17077 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17078 == 0);
17079 deduce2 = (unify (tparms2, targs2, arg2, arg1,
17080 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17081 == 0);
17084 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17085 arg2, then arg2 is not as specialized as arg1. */
17086 if (!deduce1)
17087 lose2 = true;
17088 if (!deduce2)
17089 lose1 = true;
17091 /* "If, for a given type, deduction succeeds in both directions
17092 (i.e., the types are identical after the transformations above)
17093 and if the type from the argument template is more cv-qualified
17094 than the type from the parameter template (as described above)
17095 that type is considered to be more specialized than the other. If
17096 neither type is more cv-qualified than the other then neither type
17097 is more specialized than the other." */
17099 if (deduce1 && deduce2
17100 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
17102 if ((quals1 & quals2) == quals2)
17103 lose2 = true;
17104 if ((quals1 & quals2) == quals1)
17105 lose1 = true;
17108 if (lose1 && lose2)
17109 /* We've failed to deduce something in either direction.
17110 These must be unordered. */
17111 break;
17113 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17114 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17115 /* We have already processed all of the arguments in our
17116 handing of the pack expansion type. */
17117 len = 0;
17119 args1 = TREE_CHAIN (args1);
17120 args2 = TREE_CHAIN (args2);
17123 /* "In most cases, all template parameters must have values in order for
17124 deduction to succeed, but for partial ordering purposes a template
17125 parameter may remain without a value provided it is not used in the
17126 types being used for partial ordering."
17128 Thus, if we are missing any of the targs1 we need to substitute into
17129 origs1, then pat2 is not as specialized as pat1. This can happen when
17130 there is a nondeduced context. */
17131 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
17132 lose2 = true;
17133 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
17134 lose1 = true;
17136 processing_template_decl--;
17138 /* All things being equal, if the next argument is a pack expansion
17139 for one function but not for the other, prefer the
17140 non-variadic function. FIXME this is bogus; see c++/41958. */
17141 if (lose1 == lose2
17142 && args1 && TREE_VALUE (args1)
17143 && args2 && TREE_VALUE (args2))
17145 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
17146 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
17149 if (lose1 == lose2)
17150 return 0;
17151 else if (!lose1)
17152 return 1;
17153 else
17154 return -1;
17157 /* Determine which of two partial specializations of MAIN_TMPL is more
17158 specialized.
17160 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17161 to the first partial specialization. The TREE_VALUE is the
17162 innermost set of template parameters for the partial
17163 specialization. PAT2 is similar, but for the second template.
17165 Return 1 if the first partial specialization is more specialized;
17166 -1 if the second is more specialized; 0 if neither is more
17167 specialized.
17169 See [temp.class.order] for information about determining which of
17170 two templates is more specialized. */
17172 static int
17173 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
17175 tree targs;
17176 tree tmpl1, tmpl2;
17177 int winner = 0;
17178 bool any_deductions = false;
17180 tmpl1 = TREE_TYPE (pat1);
17181 tmpl2 = TREE_TYPE (pat2);
17183 /* Just like what happens for functions, if we are ordering between
17184 different class template specializations, we may encounter dependent
17185 types in the arguments, and we need our dependency check functions
17186 to behave correctly. */
17187 ++processing_template_decl;
17188 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
17189 CLASSTYPE_TI_ARGS (tmpl1),
17190 CLASSTYPE_TI_ARGS (tmpl2));
17191 if (targs)
17193 --winner;
17194 any_deductions = true;
17197 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
17198 CLASSTYPE_TI_ARGS (tmpl2),
17199 CLASSTYPE_TI_ARGS (tmpl1));
17200 if (targs)
17202 ++winner;
17203 any_deductions = true;
17205 --processing_template_decl;
17207 /* In the case of a tie where at least one of the class templates
17208 has a parameter pack at the end, the template with the most
17209 non-packed parameters wins. */
17210 if (winner == 0
17211 && any_deductions
17212 && (template_args_variadic_p (TREE_PURPOSE (pat1))
17213 || template_args_variadic_p (TREE_PURPOSE (pat2))))
17215 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
17216 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
17217 int len1 = TREE_VEC_LENGTH (args1);
17218 int len2 = TREE_VEC_LENGTH (args2);
17220 /* We don't count the pack expansion at the end. */
17221 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
17222 --len1;
17223 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
17224 --len2;
17226 if (len1 > len2)
17227 return 1;
17228 else if (len1 < len2)
17229 return -1;
17232 return winner;
17235 /* Return the template arguments that will produce the function signature
17236 DECL from the function template FN, with the explicit template
17237 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17238 also match. Return NULL_TREE if no satisfactory arguments could be
17239 found. */
17241 static tree
17242 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
17244 int ntparms = DECL_NTPARMS (fn);
17245 tree targs = make_tree_vec (ntparms);
17246 tree decl_type = TREE_TYPE (decl);
17247 tree decl_arg_types;
17248 tree *args;
17249 unsigned int nargs, ix;
17250 tree arg;
17252 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
17254 /* Never do unification on the 'this' parameter. */
17255 decl_arg_types = skip_artificial_parms_for (decl,
17256 TYPE_ARG_TYPES (decl_type));
17258 nargs = list_length (decl_arg_types);
17259 args = XALLOCAVEC (tree, nargs);
17260 for (arg = decl_arg_types, ix = 0;
17261 arg != NULL_TREE && arg != void_list_node;
17262 arg = TREE_CHAIN (arg), ++ix)
17263 args[ix] = TREE_VALUE (arg);
17265 if (fn_type_unification (fn, explicit_args, targs,
17266 args, ix,
17267 (check_rettype || DECL_CONV_FN_P (fn)
17268 ? TREE_TYPE (decl_type) : NULL_TREE),
17269 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false)
17270 == error_mark_node)
17271 return NULL_TREE;
17273 return targs;
17276 /* Return the innermost template arguments that, when applied to a partial
17277 specialization of MAIN_TMPL whose innermost template parameters are
17278 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17279 ARGS.
17281 For example, suppose we have:
17283 template <class T, class U> struct S {};
17284 template <class T> struct S<T*, int> {};
17286 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17287 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17288 int}. The resulting vector will be {double}, indicating that `T'
17289 is bound to `double'. */
17291 static tree
17292 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
17294 int i, ntparms = TREE_VEC_LENGTH (tparms);
17295 tree deduced_args;
17296 tree innermost_deduced_args;
17298 innermost_deduced_args = make_tree_vec (ntparms);
17299 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17301 deduced_args = copy_node (args);
17302 SET_TMPL_ARGS_LEVEL (deduced_args,
17303 TMPL_ARGS_DEPTH (deduced_args),
17304 innermost_deduced_args);
17306 else
17307 deduced_args = innermost_deduced_args;
17309 if (unify (tparms, deduced_args,
17310 INNERMOST_TEMPLATE_ARGS (spec_args),
17311 INNERMOST_TEMPLATE_ARGS (args),
17312 UNIFY_ALLOW_NONE, /*explain_p=*/false))
17313 return NULL_TREE;
17315 for (i = 0; i < ntparms; ++i)
17316 if (! TREE_VEC_ELT (innermost_deduced_args, i))
17317 return NULL_TREE;
17319 /* Verify that nondeduced template arguments agree with the type
17320 obtained from argument deduction.
17322 For example:
17324 struct A { typedef int X; };
17325 template <class T, class U> struct C {};
17326 template <class T> struct C<T, typename T::X> {};
17328 Then with the instantiation `C<A, int>', we can deduce that
17329 `T' is `A' but unify () does not check whether `typename T::X'
17330 is `int'. */
17331 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
17332 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
17333 spec_args, main_tmpl,
17334 tf_none, false, false);
17335 if (spec_args == error_mark_node
17336 /* We only need to check the innermost arguments; the other
17337 arguments will always agree. */
17338 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
17339 INNERMOST_TEMPLATE_ARGS (args)))
17340 return NULL_TREE;
17342 /* Now that we have bindings for all of the template arguments,
17343 ensure that the arguments deduced for the template template
17344 parameters have compatible template parameter lists. See the use
17345 of template_template_parm_bindings_ok_p in fn_type_unification
17346 for more information. */
17347 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
17348 return NULL_TREE;
17350 return deduced_args;
17353 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17354 Return the TREE_LIST node with the most specialized template, if
17355 any. If there is no most specialized template, the error_mark_node
17356 is returned.
17358 Note that this function does not look at, or modify, the
17359 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17360 returned is one of the elements of INSTANTIATIONS, callers may
17361 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17362 and retrieve it from the value returned. */
17364 tree
17365 most_specialized_instantiation (tree templates)
17367 tree fn, champ;
17369 ++processing_template_decl;
17371 champ = templates;
17372 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17374 int fate = 0;
17376 if (get_bindings (TREE_VALUE (champ),
17377 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17378 NULL_TREE, /*check_ret=*/true))
17379 fate--;
17381 if (get_bindings (TREE_VALUE (fn),
17382 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17383 NULL_TREE, /*check_ret=*/true))
17384 fate++;
17386 if (fate == -1)
17387 champ = fn;
17388 else if (!fate)
17390 /* Equally specialized, move to next function. If there
17391 is no next function, nothing's most specialized. */
17392 fn = TREE_CHAIN (fn);
17393 champ = fn;
17394 if (!fn)
17395 break;
17399 if (champ)
17400 /* Now verify that champ is better than everything earlier in the
17401 instantiation list. */
17402 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17403 if (get_bindings (TREE_VALUE (champ),
17404 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17405 NULL_TREE, /*check_ret=*/true)
17406 || !get_bindings (TREE_VALUE (fn),
17407 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17408 NULL_TREE, /*check_ret=*/true))
17410 champ = NULL_TREE;
17411 break;
17414 processing_template_decl--;
17416 if (!champ)
17417 return error_mark_node;
17419 return champ;
17422 /* If DECL is a specialization of some template, return the most
17423 general such template. Otherwise, returns NULL_TREE.
17425 For example, given:
17427 template <class T> struct S { template <class U> void f(U); };
17429 if TMPL is `template <class U> void S<int>::f(U)' this will return
17430 the full template. This function will not trace past partial
17431 specializations, however. For example, given in addition:
17433 template <class T> struct S<T*> { template <class U> void f(U); };
17435 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17436 `template <class T> template <class U> S<T*>::f(U)'. */
17438 tree
17439 most_general_template (tree decl)
17441 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17442 an immediate specialization. */
17443 if (TREE_CODE (decl) == FUNCTION_DECL)
17445 if (DECL_TEMPLATE_INFO (decl)) {
17446 decl = DECL_TI_TEMPLATE (decl);
17448 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17449 template friend. */
17450 if (TREE_CODE (decl) != TEMPLATE_DECL)
17451 return NULL_TREE;
17452 } else
17453 return NULL_TREE;
17456 /* Look for more and more general templates. */
17457 while (DECL_TEMPLATE_INFO (decl))
17459 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17460 (See cp-tree.h for details.) */
17461 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17462 break;
17464 if (CLASS_TYPE_P (TREE_TYPE (decl))
17465 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17466 break;
17468 /* Stop if we run into an explicitly specialized class template. */
17469 if (!DECL_NAMESPACE_SCOPE_P (decl)
17470 && DECL_CONTEXT (decl)
17471 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17472 break;
17474 decl = DECL_TI_TEMPLATE (decl);
17477 return decl;
17480 /* Return the most specialized of the class template partial
17481 specializations of TMPL which can produce TYPE, a specialization of
17482 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17483 a _TYPE node corresponding to the partial specialization, while the
17484 TREE_PURPOSE is the set of template arguments that must be
17485 substituted into the TREE_TYPE in order to generate TYPE.
17487 If the choice of partial specialization is ambiguous, a diagnostic
17488 is issued, and the error_mark_node is returned. If there are no
17489 partial specializations of TMPL matching TYPE, then NULL_TREE is
17490 returned. */
17492 static tree
17493 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17495 tree list = NULL_TREE;
17496 tree t;
17497 tree champ;
17498 int fate;
17499 bool ambiguous_p;
17500 tree args;
17501 tree outer_args = NULL_TREE;
17503 tmpl = most_general_template (tmpl);
17504 args = CLASSTYPE_TI_ARGS (type);
17506 /* For determining which partial specialization to use, only the
17507 innermost args are interesting. */
17508 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17510 outer_args = strip_innermost_template_args (args, 1);
17511 args = INNERMOST_TEMPLATE_ARGS (args);
17514 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17516 tree partial_spec_args;
17517 tree spec_args;
17518 tree parms = TREE_VALUE (t);
17520 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17522 ++processing_template_decl;
17524 if (outer_args)
17526 int i;
17528 /* Discard the outer levels of args, and then substitute in the
17529 template args from the enclosing class. */
17530 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17531 partial_spec_args = tsubst_template_args
17532 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17534 /* PARMS already refers to just the innermost parms, but the
17535 template parms in partial_spec_args had their levels lowered
17536 by tsubst, so we need to do the same for the parm list. We
17537 can't just tsubst the TREE_VEC itself, as tsubst wants to
17538 treat a TREE_VEC as an argument vector. */
17539 parms = copy_node (parms);
17540 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17541 TREE_VEC_ELT (parms, i) =
17542 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17546 partial_spec_args =
17547 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17548 add_to_template_args (outer_args,
17549 partial_spec_args),
17550 tmpl, tf_none,
17551 /*require_all_args=*/true,
17552 /*use_default_args=*/true);
17554 --processing_template_decl;
17556 if (partial_spec_args == error_mark_node)
17557 return error_mark_node;
17559 spec_args = get_class_bindings (tmpl, parms,
17560 partial_spec_args,
17561 args);
17562 if (spec_args)
17564 if (outer_args)
17565 spec_args = add_to_template_args (outer_args, spec_args);
17566 list = tree_cons (spec_args, TREE_VALUE (t), list);
17567 TREE_TYPE (list) = TREE_TYPE (t);
17571 if (! list)
17572 return NULL_TREE;
17574 ambiguous_p = false;
17575 t = list;
17576 champ = t;
17577 t = TREE_CHAIN (t);
17578 for (; t; t = TREE_CHAIN (t))
17580 fate = more_specialized_class (tmpl, champ, t);
17581 if (fate == 1)
17583 else
17585 if (fate == 0)
17587 t = TREE_CHAIN (t);
17588 if (! t)
17590 ambiguous_p = true;
17591 break;
17594 champ = t;
17598 if (!ambiguous_p)
17599 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17601 fate = more_specialized_class (tmpl, champ, t);
17602 if (fate != 1)
17604 ambiguous_p = true;
17605 break;
17609 if (ambiguous_p)
17611 const char *str;
17612 char *spaces = NULL;
17613 if (!(complain & tf_error))
17614 return error_mark_node;
17615 error ("ambiguous class template instantiation for %q#T", type);
17616 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17617 for (t = list; t; t = TREE_CHAIN (t))
17619 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17620 spaces = spaces ? spaces : get_spaces (str);
17622 free (spaces);
17623 return error_mark_node;
17626 return champ;
17629 /* Explicitly instantiate DECL. */
17631 void
17632 do_decl_instantiation (tree decl, tree storage)
17634 tree result = NULL_TREE;
17635 int extern_p = 0;
17637 if (!decl || decl == error_mark_node)
17638 /* An error occurred, for which grokdeclarator has already issued
17639 an appropriate message. */
17640 return;
17641 else if (! DECL_LANG_SPECIFIC (decl))
17643 error ("explicit instantiation of non-template %q#D", decl);
17644 return;
17646 else if (TREE_CODE (decl) == VAR_DECL)
17648 /* There is an asymmetry here in the way VAR_DECLs and
17649 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17650 the latter, the DECL we get back will be marked as a
17651 template instantiation, and the appropriate
17652 DECL_TEMPLATE_INFO will be set up. This does not happen for
17653 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17654 should handle VAR_DECLs as it currently handles
17655 FUNCTION_DECLs. */
17656 if (!DECL_CLASS_SCOPE_P (decl))
17658 error ("%qD is not a static data member of a class template", decl);
17659 return;
17661 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
17662 if (!result || TREE_CODE (result) != VAR_DECL)
17664 error ("no matching template for %qD found", decl);
17665 return;
17667 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
17669 error ("type %qT for explicit instantiation %qD does not match "
17670 "declared type %qT", TREE_TYPE (result), decl,
17671 TREE_TYPE (decl));
17672 return;
17675 else if (TREE_CODE (decl) != FUNCTION_DECL)
17677 error ("explicit instantiation of %q#D", decl);
17678 return;
17680 else
17681 result = decl;
17683 /* Check for various error cases. Note that if the explicit
17684 instantiation is valid the RESULT will currently be marked as an
17685 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17686 until we get here. */
17688 if (DECL_TEMPLATE_SPECIALIZATION (result))
17690 /* DR 259 [temp.spec].
17692 Both an explicit instantiation and a declaration of an explicit
17693 specialization shall not appear in a program unless the explicit
17694 instantiation follows a declaration of the explicit specialization.
17696 For a given set of template parameters, if an explicit
17697 instantiation of a template appears after a declaration of an
17698 explicit specialization for that template, the explicit
17699 instantiation has no effect. */
17700 return;
17702 else if (DECL_EXPLICIT_INSTANTIATION (result))
17704 /* [temp.spec]
17706 No program shall explicitly instantiate any template more
17707 than once.
17709 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17710 the first instantiation was `extern' and the second is not,
17711 and EXTERN_P for the opposite case. */
17712 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
17713 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
17714 /* If an "extern" explicit instantiation follows an ordinary
17715 explicit instantiation, the template is instantiated. */
17716 if (extern_p)
17717 return;
17719 else if (!DECL_IMPLICIT_INSTANTIATION (result))
17721 error ("no matching template for %qD found", result);
17722 return;
17724 else if (!DECL_TEMPLATE_INFO (result))
17726 permerror (input_location, "explicit instantiation of non-template %q#D", result);
17727 return;
17730 if (storage == NULL_TREE)
17732 else if (storage == ridpointers[(int) RID_EXTERN])
17734 if (!in_system_header && (cxx_dialect == cxx98))
17735 pedwarn (input_location, OPT_Wpedantic,
17736 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17737 "instantiations");
17738 extern_p = 1;
17740 else
17741 error ("storage class %qD applied to template instantiation", storage);
17743 check_explicit_instantiation_namespace (result);
17744 mark_decl_instantiated (result, extern_p);
17745 if (! extern_p)
17746 instantiate_decl (result, /*defer_ok=*/1,
17747 /*expl_inst_class_mem_p=*/false);
17750 static void
17751 mark_class_instantiated (tree t, int extern_p)
17753 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
17754 SET_CLASSTYPE_INTERFACE_KNOWN (t);
17755 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
17756 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
17757 if (! extern_p)
17759 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
17760 rest_of_type_compilation (t, 1);
17764 /* Called from do_type_instantiation through binding_table_foreach to
17765 do recursive instantiation for the type bound in ENTRY. */
17766 static void
17767 bt_instantiate_type_proc (binding_entry entry, void *data)
17769 tree storage = *(tree *) data;
17771 if (MAYBE_CLASS_TYPE_P (entry->type)
17772 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
17773 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
17776 /* Called from do_type_instantiation to instantiate a member
17777 (a member function or a static member variable) of an
17778 explicitly instantiated class template. */
17779 static void
17780 instantiate_class_member (tree decl, int extern_p)
17782 mark_decl_instantiated (decl, extern_p);
17783 if (! extern_p)
17784 instantiate_decl (decl, /*defer_ok=*/1,
17785 /*expl_inst_class_mem_p=*/true);
17788 /* Perform an explicit instantiation of template class T. STORAGE, if
17789 non-null, is the RID for extern, inline or static. COMPLAIN is
17790 nonzero if this is called from the parser, zero if called recursively,
17791 since the standard is unclear (as detailed below). */
17793 void
17794 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
17796 int extern_p = 0;
17797 int nomem_p = 0;
17798 int static_p = 0;
17799 int previous_instantiation_extern_p = 0;
17801 if (TREE_CODE (t) == TYPE_DECL)
17802 t = TREE_TYPE (t);
17804 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
17806 tree tmpl =
17807 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
17808 if (tmpl)
17809 error ("explicit instantiation of non-class template %qD", tmpl);
17810 else
17811 error ("explicit instantiation of non-template type %qT", t);
17812 return;
17815 complete_type (t);
17817 if (!COMPLETE_TYPE_P (t))
17819 if (complain & tf_error)
17820 error ("explicit instantiation of %q#T before definition of template",
17822 return;
17825 if (storage != NULL_TREE)
17827 if (!in_system_header)
17829 if (storage == ridpointers[(int) RID_EXTERN])
17831 if (cxx_dialect == cxx98)
17832 pedwarn (input_location, OPT_Wpedantic,
17833 "ISO C++ 1998 forbids the use of %<extern%> on "
17834 "explicit instantiations");
17836 else
17837 pedwarn (input_location, OPT_Wpedantic,
17838 "ISO C++ forbids the use of %qE"
17839 " on explicit instantiations", storage);
17842 if (storage == ridpointers[(int) RID_INLINE])
17843 nomem_p = 1;
17844 else if (storage == ridpointers[(int) RID_EXTERN])
17845 extern_p = 1;
17846 else if (storage == ridpointers[(int) RID_STATIC])
17847 static_p = 1;
17848 else
17850 error ("storage class %qD applied to template instantiation",
17851 storage);
17852 extern_p = 0;
17856 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
17858 /* DR 259 [temp.spec].
17860 Both an explicit instantiation and a declaration of an explicit
17861 specialization shall not appear in a program unless the explicit
17862 instantiation follows a declaration of the explicit specialization.
17864 For a given set of template parameters, if an explicit
17865 instantiation of a template appears after a declaration of an
17866 explicit specialization for that template, the explicit
17867 instantiation has no effect. */
17868 return;
17870 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
17872 /* [temp.spec]
17874 No program shall explicitly instantiate any template more
17875 than once.
17877 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17878 instantiation was `extern'. If EXTERN_P then the second is.
17879 These cases are OK. */
17880 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
17882 if (!previous_instantiation_extern_p && !extern_p
17883 && (complain & tf_error))
17884 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
17886 /* If we've already instantiated the template, just return now. */
17887 if (!CLASSTYPE_INTERFACE_ONLY (t))
17888 return;
17891 check_explicit_instantiation_namespace (TYPE_NAME (t));
17892 mark_class_instantiated (t, extern_p);
17894 if (nomem_p)
17895 return;
17898 tree tmp;
17900 /* In contrast to implicit instantiation, where only the
17901 declarations, and not the definitions, of members are
17902 instantiated, we have here:
17904 [temp.explicit]
17906 The explicit instantiation of a class template specialization
17907 implies the instantiation of all of its members not
17908 previously explicitly specialized in the translation unit
17909 containing the explicit instantiation.
17911 Of course, we can't instantiate member template classes, since
17912 we don't have any arguments for them. Note that the standard
17913 is unclear on whether the instantiation of the members are
17914 *explicit* instantiations or not. However, the most natural
17915 interpretation is that it should be an explicit instantiation. */
17917 if (! static_p)
17918 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
17919 if (TREE_CODE (tmp) == FUNCTION_DECL
17920 && DECL_TEMPLATE_INSTANTIATION (tmp))
17921 instantiate_class_member (tmp, extern_p);
17923 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
17924 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
17925 instantiate_class_member (tmp, extern_p);
17927 if (CLASSTYPE_NESTED_UTDS (t))
17928 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
17929 bt_instantiate_type_proc, &storage);
17933 /* Given a function DECL, which is a specialization of TMPL, modify
17934 DECL to be a re-instantiation of TMPL with the same template
17935 arguments. TMPL should be the template into which tsubst'ing
17936 should occur for DECL, not the most general template.
17938 One reason for doing this is a scenario like this:
17940 template <class T>
17941 void f(const T&, int i);
17943 void g() { f(3, 7); }
17945 template <class T>
17946 void f(const T& t, const int i) { }
17948 Note that when the template is first instantiated, with
17949 instantiate_template, the resulting DECL will have no name for the
17950 first parameter, and the wrong type for the second. So, when we go
17951 to instantiate the DECL, we regenerate it. */
17953 static void
17954 regenerate_decl_from_template (tree decl, tree tmpl)
17956 /* The arguments used to instantiate DECL, from the most general
17957 template. */
17958 tree args;
17959 tree code_pattern;
17961 args = DECL_TI_ARGS (decl);
17962 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
17964 /* Make sure that we can see identifiers, and compute access
17965 correctly. */
17966 push_access_scope (decl);
17968 if (TREE_CODE (decl) == FUNCTION_DECL)
17970 tree decl_parm;
17971 tree pattern_parm;
17972 tree specs;
17973 int args_depth;
17974 int parms_depth;
17976 args_depth = TMPL_ARGS_DEPTH (args);
17977 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
17978 if (args_depth > parms_depth)
17979 args = get_innermost_template_args (args, parms_depth);
17981 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
17982 args, tf_error, NULL_TREE,
17983 /*defer_ok*/false);
17984 if (specs && specs != error_mark_node)
17985 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
17986 specs);
17988 /* Merge parameter declarations. */
17989 decl_parm = skip_artificial_parms_for (decl,
17990 DECL_ARGUMENTS (decl));
17991 pattern_parm
17992 = skip_artificial_parms_for (code_pattern,
17993 DECL_ARGUMENTS (code_pattern));
17994 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
17996 tree parm_type;
17997 tree attributes;
17999 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18000 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
18001 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
18002 NULL_TREE);
18003 parm_type = type_decays_to (parm_type);
18004 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18005 TREE_TYPE (decl_parm) = parm_type;
18006 attributes = DECL_ATTRIBUTES (pattern_parm);
18007 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18009 DECL_ATTRIBUTES (decl_parm) = attributes;
18010 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18012 decl_parm = DECL_CHAIN (decl_parm);
18013 pattern_parm = DECL_CHAIN (pattern_parm);
18015 /* Merge any parameters that match with the function parameter
18016 pack. */
18017 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
18019 int i, len;
18020 tree expanded_types;
18021 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18022 the parameters in this function parameter pack. */
18023 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
18024 args, tf_error, NULL_TREE);
18025 len = TREE_VEC_LENGTH (expanded_types);
18026 for (i = 0; i < len; i++)
18028 tree parm_type;
18029 tree attributes;
18031 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18032 /* Rename the parameter to include the index. */
18033 DECL_NAME (decl_parm) =
18034 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
18035 parm_type = TREE_VEC_ELT (expanded_types, i);
18036 parm_type = type_decays_to (parm_type);
18037 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18038 TREE_TYPE (decl_parm) = parm_type;
18039 attributes = DECL_ATTRIBUTES (pattern_parm);
18040 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18042 DECL_ATTRIBUTES (decl_parm) = attributes;
18043 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18045 decl_parm = DECL_CHAIN (decl_parm);
18048 /* Merge additional specifiers from the CODE_PATTERN. */
18049 if (DECL_DECLARED_INLINE_P (code_pattern)
18050 && !DECL_DECLARED_INLINE_P (decl))
18051 DECL_DECLARED_INLINE_P (decl) = 1;
18053 else if (TREE_CODE (decl) == VAR_DECL)
18055 DECL_INITIAL (decl) =
18056 tsubst_expr (DECL_INITIAL (code_pattern), args,
18057 tf_error, DECL_TI_TEMPLATE (decl),
18058 /*integral_constant_expression_p=*/false);
18059 if (VAR_HAD_UNKNOWN_BOUND (decl))
18060 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
18061 tf_error, DECL_TI_TEMPLATE (decl));
18063 else
18064 gcc_unreachable ();
18066 pop_access_scope (decl);
18069 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18070 substituted to get DECL. */
18072 tree
18073 template_for_substitution (tree decl)
18075 tree tmpl = DECL_TI_TEMPLATE (decl);
18077 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18078 for the instantiation. This is not always the most general
18079 template. Consider, for example:
18081 template <class T>
18082 struct S { template <class U> void f();
18083 template <> void f<int>(); };
18085 and an instantiation of S<double>::f<int>. We want TD to be the
18086 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18087 while (/* An instantiation cannot have a definition, so we need a
18088 more general template. */
18089 DECL_TEMPLATE_INSTANTIATION (tmpl)
18090 /* We must also deal with friend templates. Given:
18092 template <class T> struct S {
18093 template <class U> friend void f() {};
18096 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18097 so far as the language is concerned, but that's still
18098 where we get the pattern for the instantiation from. On
18099 other hand, if the definition comes outside the class, say:
18101 template <class T> struct S {
18102 template <class U> friend void f();
18104 template <class U> friend void f() {}
18106 we don't need to look any further. That's what the check for
18107 DECL_INITIAL is for. */
18108 || (TREE_CODE (decl) == FUNCTION_DECL
18109 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
18110 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
18112 /* The present template, TD, should not be a definition. If it
18113 were a definition, we should be using it! Note that we
18114 cannot restructure the loop to just keep going until we find
18115 a template with a definition, since that might go too far if
18116 a specialization was declared, but not defined. */
18117 gcc_assert (TREE_CODE (decl) != VAR_DECL
18118 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
18120 /* Fetch the more general template. */
18121 tmpl = DECL_TI_TEMPLATE (tmpl);
18124 return tmpl;
18127 /* Returns true if we need to instantiate this template instance even if we
18128 know we aren't going to emit it.. */
18130 bool
18131 always_instantiate_p (tree decl)
18133 /* We always instantiate inline functions so that we can inline them. An
18134 explicit instantiation declaration prohibits implicit instantiation of
18135 non-inline functions. With high levels of optimization, we would
18136 normally inline non-inline functions -- but we're not allowed to do
18137 that for "extern template" functions. Therefore, we check
18138 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18139 return ((TREE_CODE (decl) == FUNCTION_DECL
18140 && (DECL_DECLARED_INLINE_P (decl)
18141 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
18142 /* And we need to instantiate static data members so that
18143 their initializers are available in integral constant
18144 expressions. */
18145 || (TREE_CODE (decl) == VAR_DECL
18146 && decl_maybe_constant_var_p (decl)));
18149 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18150 instantiate it now, modifying TREE_TYPE (fn). */
18152 void
18153 maybe_instantiate_noexcept (tree fn)
18155 tree fntype, spec, noex, clone;
18157 if (DECL_CLONED_FUNCTION_P (fn))
18158 fn = DECL_CLONED_FUNCTION (fn);
18159 fntype = TREE_TYPE (fn);
18160 spec = TYPE_RAISES_EXCEPTIONS (fntype);
18162 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
18163 return;
18165 noex = TREE_PURPOSE (spec);
18167 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
18169 if (push_tinst_level (fn))
18171 push_access_scope (fn);
18172 input_location = DECL_SOURCE_LOCATION (fn);
18173 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
18174 DEFERRED_NOEXCEPT_ARGS (noex),
18175 tf_warning_or_error, fn,
18176 /*function_p=*/false,
18177 /*integral_constant_expression_p=*/true);
18178 pop_access_scope (fn);
18179 pop_tinst_level ();
18180 spec = build_noexcept_spec (noex, tf_warning_or_error);
18181 if (spec == error_mark_node)
18182 spec = noexcept_false_spec;
18184 else
18185 spec = noexcept_false_spec;
18187 else
18189 /* This is an implicitly declared function, so NOEX is a list of
18190 other functions to evaluate and merge. */
18191 tree elt;
18192 spec = noexcept_true_spec;
18193 for (elt = noex; elt; elt = OVL_NEXT (elt))
18195 tree fn = OVL_CURRENT (elt);
18196 tree subspec;
18197 maybe_instantiate_noexcept (fn);
18198 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
18199 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
18203 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
18205 FOR_EACH_CLONE (clone, fn)
18207 if (TREE_TYPE (clone) == fntype)
18208 TREE_TYPE (clone) = TREE_TYPE (fn);
18209 else
18210 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
18214 /* Produce the definition of D, a _DECL generated from a template. If
18215 DEFER_OK is nonzero, then we don't have to actually do the
18216 instantiation now; we just have to do it sometime. Normally it is
18217 an error if this is an explicit instantiation but D is undefined.
18218 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18219 explicitly instantiated class template. */
18221 tree
18222 instantiate_decl (tree d, int defer_ok,
18223 bool expl_inst_class_mem_p)
18225 tree tmpl = DECL_TI_TEMPLATE (d);
18226 tree gen_args;
18227 tree args;
18228 tree td;
18229 tree code_pattern;
18230 tree spec;
18231 tree gen_tmpl;
18232 bool pattern_defined;
18233 location_t saved_loc = input_location;
18234 bool external_p;
18235 tree fn_context;
18236 bool nested;
18238 /* This function should only be used to instantiate templates for
18239 functions and static member variables. */
18240 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
18241 || TREE_CODE (d) == VAR_DECL);
18243 /* Variables are never deferred; if instantiation is required, they
18244 are instantiated right away. That allows for better code in the
18245 case that an expression refers to the value of the variable --
18246 if the variable has a constant value the referring expression can
18247 take advantage of that fact. */
18248 if (TREE_CODE (d) == VAR_DECL
18249 || DECL_DECLARED_CONSTEXPR_P (d))
18250 defer_ok = 0;
18252 /* Don't instantiate cloned functions. Instead, instantiate the
18253 functions they cloned. */
18254 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
18255 d = DECL_CLONED_FUNCTION (d);
18257 if (DECL_TEMPLATE_INSTANTIATED (d)
18258 || (TREE_CODE (d) == FUNCTION_DECL
18259 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
18260 || DECL_TEMPLATE_SPECIALIZATION (d))
18261 /* D has already been instantiated or explicitly specialized, so
18262 there's nothing for us to do here.
18264 It might seem reasonable to check whether or not D is an explicit
18265 instantiation, and, if so, stop here. But when an explicit
18266 instantiation is deferred until the end of the compilation,
18267 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18268 the instantiation. */
18269 return d;
18271 /* Check to see whether we know that this template will be
18272 instantiated in some other file, as with "extern template"
18273 extension. */
18274 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
18276 /* In general, we do not instantiate such templates. */
18277 if (external_p && !always_instantiate_p (d))
18278 return d;
18280 gen_tmpl = most_general_template (tmpl);
18281 gen_args = DECL_TI_ARGS (d);
18283 if (tmpl != gen_tmpl)
18284 /* We should already have the extra args. */
18285 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
18286 == TMPL_ARGS_DEPTH (gen_args));
18287 /* And what's in the hash table should match D. */
18288 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
18289 || spec == NULL_TREE);
18291 /* This needs to happen before any tsubsting. */
18292 if (! push_tinst_level (d))
18293 return d;
18295 timevar_push (TV_TEMPLATE_INST);
18297 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18298 for the instantiation. */
18299 td = template_for_substitution (d);
18300 code_pattern = DECL_TEMPLATE_RESULT (td);
18302 /* We should never be trying to instantiate a member of a class
18303 template or partial specialization. */
18304 gcc_assert (d != code_pattern);
18306 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
18307 || DECL_TEMPLATE_SPECIALIZATION (td))
18308 /* In the case of a friend template whose definition is provided
18309 outside the class, we may have too many arguments. Drop the
18310 ones we don't need. The same is true for specializations. */
18311 args = get_innermost_template_args
18312 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
18313 else
18314 args = gen_args;
18316 if (TREE_CODE (d) == FUNCTION_DECL)
18317 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
18318 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
18319 else
18320 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
18322 /* We may be in the middle of deferred access check. Disable it now. */
18323 push_deferring_access_checks (dk_no_deferred);
18325 /* Unless an explicit instantiation directive has already determined
18326 the linkage of D, remember that a definition is available for
18327 this entity. */
18328 if (pattern_defined
18329 && !DECL_INTERFACE_KNOWN (d)
18330 && !DECL_NOT_REALLY_EXTERN (d))
18331 mark_definable (d);
18333 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
18334 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
18335 input_location = DECL_SOURCE_LOCATION (d);
18337 /* If D is a member of an explicitly instantiated class template,
18338 and no definition is available, treat it like an implicit
18339 instantiation. */
18340 if (!pattern_defined && expl_inst_class_mem_p
18341 && DECL_EXPLICIT_INSTANTIATION (d))
18343 /* Leave linkage flags alone on instantiations with anonymous
18344 visibility. */
18345 if (TREE_PUBLIC (d))
18347 DECL_NOT_REALLY_EXTERN (d) = 0;
18348 DECL_INTERFACE_KNOWN (d) = 0;
18350 SET_DECL_IMPLICIT_INSTANTIATION (d);
18353 if (TREE_CODE (d) == FUNCTION_DECL)
18354 maybe_instantiate_noexcept (d);
18356 /* Defer all other templates, unless we have been explicitly
18357 forbidden from doing so. */
18358 if (/* If there is no definition, we cannot instantiate the
18359 template. */
18360 ! pattern_defined
18361 /* If it's OK to postpone instantiation, do so. */
18362 || defer_ok
18363 /* If this is a static data member that will be defined
18364 elsewhere, we don't want to instantiate the entire data
18365 member, but we do want to instantiate the initializer so that
18366 we can substitute that elsewhere. */
18367 || (external_p && TREE_CODE (d) == VAR_DECL))
18369 /* The definition of the static data member is now required so
18370 we must substitute the initializer. */
18371 if (TREE_CODE (d) == VAR_DECL
18372 && !DECL_INITIAL (d)
18373 && DECL_INITIAL (code_pattern))
18375 tree ns;
18376 tree init;
18377 bool const_init = false;
18379 ns = decl_namespace_context (d);
18380 push_nested_namespace (ns);
18381 push_nested_class (DECL_CONTEXT (d));
18382 init = tsubst_expr (DECL_INITIAL (code_pattern),
18383 args,
18384 tf_warning_or_error, NULL_TREE,
18385 /*integral_constant_expression_p=*/false);
18386 /* Make sure the initializer is still constant, in case of
18387 circular dependency (template/instantiate6.C). */
18388 const_init
18389 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18390 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18391 /*asmspec_tree=*/NULL_TREE,
18392 LOOKUP_ONLYCONVERTING);
18393 pop_nested_class ();
18394 pop_nested_namespace (ns);
18397 /* We restore the source position here because it's used by
18398 add_pending_template. */
18399 input_location = saved_loc;
18401 if (at_eof && !pattern_defined
18402 && DECL_EXPLICIT_INSTANTIATION (d)
18403 && DECL_NOT_REALLY_EXTERN (d))
18404 /* [temp.explicit]
18406 The definition of a non-exported function template, a
18407 non-exported member function template, or a non-exported
18408 member function or static data member of a class template
18409 shall be present in every translation unit in which it is
18410 explicitly instantiated. */
18411 permerror (input_location, "explicit instantiation of %qD "
18412 "but no definition available", d);
18414 /* If we're in unevaluated context, we just wanted to get the
18415 constant value; this isn't an odr use, so don't queue
18416 a full instantiation. */
18417 if (cp_unevaluated_operand != 0)
18418 goto out;
18419 /* ??? Historically, we have instantiated inline functions, even
18420 when marked as "extern template". */
18421 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18422 add_pending_template (d);
18423 goto out;
18425 /* Tell the repository that D is available in this translation unit
18426 -- and see if it is supposed to be instantiated here. */
18427 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18429 /* In a PCH file, despite the fact that the repository hasn't
18430 requested instantiation in the PCH it is still possible that
18431 an instantiation will be required in a file that includes the
18432 PCH. */
18433 if (pch_file)
18434 add_pending_template (d);
18435 /* Instantiate inline functions so that the inliner can do its
18436 job, even though we'll not be emitting a copy of this
18437 function. */
18438 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18439 goto out;
18442 fn_context = decl_function_context (d);
18443 nested = (current_function_decl != NULL_TREE);
18444 if (!fn_context)
18445 push_to_top_level ();
18446 else if (nested)
18447 push_function_context ();
18449 /* Mark D as instantiated so that recursive calls to
18450 instantiate_decl do not try to instantiate it again. */
18451 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18453 /* Regenerate the declaration in case the template has been modified
18454 by a subsequent redeclaration. */
18455 regenerate_decl_from_template (d, td);
18457 /* We already set the file and line above. Reset them now in case
18458 they changed as a result of calling regenerate_decl_from_template. */
18459 input_location = DECL_SOURCE_LOCATION (d);
18461 if (TREE_CODE (d) == VAR_DECL)
18463 tree init;
18464 bool const_init = false;
18466 /* Clear out DECL_RTL; whatever was there before may not be right
18467 since we've reset the type of the declaration. */
18468 SET_DECL_RTL (d, NULL);
18469 DECL_IN_AGGR_P (d) = 0;
18471 /* The initializer is placed in DECL_INITIAL by
18472 regenerate_decl_from_template so we don't need to
18473 push/pop_access_scope again here. Pull it out so that
18474 cp_finish_decl can process it. */
18475 init = DECL_INITIAL (d);
18476 DECL_INITIAL (d) = NULL_TREE;
18477 DECL_INITIALIZED_P (d) = 0;
18479 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18480 initializer. That function will defer actual emission until
18481 we have a chance to determine linkage. */
18482 DECL_EXTERNAL (d) = 0;
18484 /* Enter the scope of D so that access-checking works correctly. */
18485 push_nested_class (DECL_CONTEXT (d));
18486 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18487 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18488 pop_nested_class ();
18490 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18491 synthesize_method (d);
18492 else if (TREE_CODE (d) == FUNCTION_DECL)
18494 struct pointer_map_t *saved_local_specializations;
18495 tree subst_decl;
18496 tree tmpl_parm;
18497 tree spec_parm;
18499 /* Save away the current list, in case we are instantiating one
18500 template from within the body of another. */
18501 saved_local_specializations = local_specializations;
18503 /* Set up the list of local specializations. */
18504 local_specializations = pointer_map_create ();
18506 /* Set up context. */
18507 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18509 /* Some typedefs referenced from within the template code need to be
18510 access checked at template instantiation time, i.e now. These
18511 types were added to the template at parsing time. Let's get those
18512 and perform the access checks then. */
18513 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
18514 gen_args);
18516 /* Create substitution entries for the parameters. */
18517 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18518 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18519 spec_parm = DECL_ARGUMENTS (d);
18520 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18522 register_local_specialization (spec_parm, tmpl_parm);
18523 spec_parm = skip_artificial_parms_for (d, spec_parm);
18524 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18526 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18528 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18530 register_local_specialization (spec_parm, tmpl_parm);
18531 spec_parm = DECL_CHAIN (spec_parm);
18533 else
18535 /* Register the (value) argument pack as a specialization of
18536 TMPL_PARM, then move on. */
18537 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18538 register_local_specialization (argpack, tmpl_parm);
18541 gcc_assert (!spec_parm);
18543 /* Substitute into the body of the function. */
18544 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18545 tf_warning_or_error, tmpl,
18546 /*integral_constant_expression_p=*/false);
18548 /* Set the current input_location to the end of the function
18549 so that finish_function knows where we are. */
18550 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18552 /* We don't need the local specializations any more. */
18553 pointer_map_destroy (local_specializations);
18554 local_specializations = saved_local_specializations;
18556 /* Finish the function. */
18557 d = finish_function (0);
18558 expand_or_defer_fn (d);
18561 /* We're not deferring instantiation any more. */
18562 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18564 if (!fn_context)
18565 pop_from_top_level ();
18566 else if (nested)
18567 pop_function_context ();
18569 out:
18570 input_location = saved_loc;
18571 pop_deferring_access_checks ();
18572 pop_tinst_level ();
18574 timevar_pop (TV_TEMPLATE_INST);
18576 return d;
18579 /* Run through the list of templates that we wish we could
18580 instantiate, and instantiate any we can. RETRIES is the
18581 number of times we retry pending template instantiation. */
18583 void
18584 instantiate_pending_templates (int retries)
18586 int reconsider;
18587 location_t saved_loc = input_location;
18589 /* Instantiating templates may trigger vtable generation. This in turn
18590 may require further template instantiations. We place a limit here
18591 to avoid infinite loop. */
18592 if (pending_templates && retries >= max_tinst_depth)
18594 tree decl = pending_templates->tinst->decl;
18596 error ("template instantiation depth exceeds maximum of %d"
18597 " instantiating %q+D, possibly from virtual table generation"
18598 " (use -ftemplate-depth= to increase the maximum)",
18599 max_tinst_depth, decl);
18600 if (TREE_CODE (decl) == FUNCTION_DECL)
18601 /* Pretend that we defined it. */
18602 DECL_INITIAL (decl) = error_mark_node;
18603 return;
18608 struct pending_template **t = &pending_templates;
18609 struct pending_template *last = NULL;
18610 reconsider = 0;
18611 while (*t)
18613 tree instantiation = reopen_tinst_level ((*t)->tinst);
18614 bool complete = false;
18616 if (TYPE_P (instantiation))
18618 tree fn;
18620 if (!COMPLETE_TYPE_P (instantiation))
18622 instantiate_class_template (instantiation);
18623 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18624 for (fn = TYPE_METHODS (instantiation);
18626 fn = TREE_CHAIN (fn))
18627 if (! DECL_ARTIFICIAL (fn))
18628 instantiate_decl (fn,
18629 /*defer_ok=*/0,
18630 /*expl_inst_class_mem_p=*/false);
18631 if (COMPLETE_TYPE_P (instantiation))
18632 reconsider = 1;
18635 complete = COMPLETE_TYPE_P (instantiation);
18637 else
18639 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
18640 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
18642 instantiation
18643 = instantiate_decl (instantiation,
18644 /*defer_ok=*/0,
18645 /*expl_inst_class_mem_p=*/false);
18646 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
18647 reconsider = 1;
18650 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
18651 || DECL_TEMPLATE_INSTANTIATED (instantiation));
18654 if (complete)
18655 /* If INSTANTIATION has been instantiated, then we don't
18656 need to consider it again in the future. */
18657 *t = (*t)->next;
18658 else
18660 last = *t;
18661 t = &(*t)->next;
18663 tinst_depth = 0;
18664 current_tinst_level = NULL;
18666 last_pending_template = last;
18668 while (reconsider);
18670 input_location = saved_loc;
18673 /* Substitute ARGVEC into T, which is a list of initializers for
18674 either base class or a non-static data member. The TREE_PURPOSEs
18675 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18676 instantiate_decl. */
18678 static tree
18679 tsubst_initializer_list (tree t, tree argvec)
18681 tree inits = NULL_TREE;
18683 for (; t; t = TREE_CHAIN (t))
18685 tree decl;
18686 tree init;
18687 tree expanded_bases = NULL_TREE;
18688 tree expanded_arguments = NULL_TREE;
18689 int i, len = 1;
18691 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
18693 tree expr;
18694 tree arg;
18696 /* Expand the base class expansion type into separate base
18697 classes. */
18698 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
18699 tf_warning_or_error,
18700 NULL_TREE);
18701 if (expanded_bases == error_mark_node)
18702 continue;
18704 /* We'll be building separate TREE_LISTs of arguments for
18705 each base. */
18706 len = TREE_VEC_LENGTH (expanded_bases);
18707 expanded_arguments = make_tree_vec (len);
18708 for (i = 0; i < len; i++)
18709 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
18711 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18712 expand each argument in the TREE_VALUE of t. */
18713 expr = make_node (EXPR_PACK_EXPANSION);
18714 PACK_EXPANSION_LOCAL_P (expr) = true;
18715 PACK_EXPANSION_PARAMETER_PACKS (expr) =
18716 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
18718 if (TREE_VALUE (t) == void_type_node)
18719 /* VOID_TYPE_NODE is used to indicate
18720 value-initialization. */
18722 for (i = 0; i < len; i++)
18723 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
18725 else
18727 /* Substitute parameter packs into each argument in the
18728 TREE_LIST. */
18729 in_base_initializer = 1;
18730 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
18732 tree expanded_exprs;
18734 /* Expand the argument. */
18735 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
18736 expanded_exprs
18737 = tsubst_pack_expansion (expr, argvec,
18738 tf_warning_or_error,
18739 NULL_TREE);
18740 if (expanded_exprs == error_mark_node)
18741 continue;
18743 /* Prepend each of the expanded expressions to the
18744 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18745 for (i = 0; i < len; i++)
18747 TREE_VEC_ELT (expanded_arguments, i) =
18748 tree_cons (NULL_TREE,
18749 TREE_VEC_ELT (expanded_exprs, i),
18750 TREE_VEC_ELT (expanded_arguments, i));
18753 in_base_initializer = 0;
18755 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18756 since we built them backwards. */
18757 for (i = 0; i < len; i++)
18759 TREE_VEC_ELT (expanded_arguments, i) =
18760 nreverse (TREE_VEC_ELT (expanded_arguments, i));
18765 for (i = 0; i < len; ++i)
18767 if (expanded_bases)
18769 decl = TREE_VEC_ELT (expanded_bases, i);
18770 decl = expand_member_init (decl);
18771 init = TREE_VEC_ELT (expanded_arguments, i);
18773 else
18775 tree tmp;
18776 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
18777 tf_warning_or_error, NULL_TREE);
18779 decl = expand_member_init (decl);
18780 if (decl && !DECL_P (decl))
18781 in_base_initializer = 1;
18783 init = TREE_VALUE (t);
18784 tmp = init;
18785 if (init != void_type_node)
18786 init = tsubst_expr (init, argvec,
18787 tf_warning_or_error, NULL_TREE,
18788 /*integral_constant_expression_p=*/false);
18789 if (init == NULL_TREE && tmp != NULL_TREE)
18790 /* If we had an initializer but it instantiated to nothing,
18791 value-initialize the object. This will only occur when
18792 the initializer was a pack expansion where the parameter
18793 packs used in that expansion were of length zero. */
18794 init = void_type_node;
18795 in_base_initializer = 0;
18798 if (decl)
18800 init = build_tree_list (decl, init);
18801 TREE_CHAIN (init) = inits;
18802 inits = init;
18806 return inits;
18809 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18811 static void
18812 set_current_access_from_decl (tree decl)
18814 if (TREE_PRIVATE (decl))
18815 current_access_specifier = access_private_node;
18816 else if (TREE_PROTECTED (decl))
18817 current_access_specifier = access_protected_node;
18818 else
18819 current_access_specifier = access_public_node;
18822 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18823 is the instantiation (which should have been created with
18824 start_enum) and ARGS are the template arguments to use. */
18826 static void
18827 tsubst_enum (tree tag, tree newtag, tree args)
18829 tree e;
18831 if (SCOPED_ENUM_P (newtag))
18832 begin_scope (sk_scoped_enum, newtag);
18834 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
18836 tree value;
18837 tree decl;
18839 decl = TREE_VALUE (e);
18840 /* Note that in a template enum, the TREE_VALUE is the
18841 CONST_DECL, not the corresponding INTEGER_CST. */
18842 value = tsubst_expr (DECL_INITIAL (decl),
18843 args, tf_warning_or_error, NULL_TREE,
18844 /*integral_constant_expression_p=*/true);
18846 /* Give this enumeration constant the correct access. */
18847 set_current_access_from_decl (decl);
18849 /* Actually build the enumerator itself. */
18850 build_enumerator
18851 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
18854 if (SCOPED_ENUM_P (newtag))
18855 finish_scope ();
18857 finish_enum_value_list (newtag);
18858 finish_enum (newtag);
18860 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
18861 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
18864 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18865 its type -- but without substituting the innermost set of template
18866 arguments. So, innermost set of template parameters will appear in
18867 the type. */
18869 tree
18870 get_mostly_instantiated_function_type (tree decl)
18872 tree fn_type;
18873 tree tmpl;
18874 tree targs;
18875 tree tparms;
18876 int parm_depth;
18878 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
18879 targs = DECL_TI_ARGS (decl);
18880 tparms = DECL_TEMPLATE_PARMS (tmpl);
18881 parm_depth = TMPL_PARMS_DEPTH (tparms);
18883 /* There should be as many levels of arguments as there are levels
18884 of parameters. */
18885 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
18887 fn_type = TREE_TYPE (tmpl);
18889 if (parm_depth == 1)
18890 /* No substitution is necessary. */
18892 else
18894 int i;
18895 tree partial_args;
18897 /* Replace the innermost level of the TARGS with NULL_TREEs to
18898 let tsubst know not to substitute for those parameters. */
18899 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
18900 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
18901 SET_TMPL_ARGS_LEVEL (partial_args, i,
18902 TMPL_ARGS_LEVEL (targs, i));
18903 SET_TMPL_ARGS_LEVEL (partial_args,
18904 TMPL_ARGS_DEPTH (targs),
18905 make_tree_vec (DECL_NTPARMS (tmpl)));
18907 /* Make sure that we can see identifiers, and compute access
18908 correctly. */
18909 push_access_scope (decl);
18911 ++processing_template_decl;
18912 /* Now, do the (partial) substitution to figure out the
18913 appropriate function type. */
18914 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
18915 --processing_template_decl;
18917 /* Substitute into the template parameters to obtain the real
18918 innermost set of parameters. This step is important if the
18919 innermost set of template parameters contains value
18920 parameters whose types depend on outer template parameters. */
18921 TREE_VEC_LENGTH (partial_args)--;
18922 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
18924 pop_access_scope (decl);
18927 return fn_type;
18930 /* Return truthvalue if we're processing a template different from
18931 the last one involved in diagnostics. */
18933 problematic_instantiation_changed (void)
18935 return current_tinst_level != last_error_tinst_level;
18938 /* Remember current template involved in diagnostics. */
18939 void
18940 record_last_problematic_instantiation (void)
18942 last_error_tinst_level = current_tinst_level;
18945 struct tinst_level *
18946 current_instantiation (void)
18948 return current_tinst_level;
18951 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18952 type. Return zero for ok, nonzero for disallowed. Issue error and
18953 warning messages under control of COMPLAIN. */
18955 static int
18956 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
18958 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
18959 return 0;
18960 else if (POINTER_TYPE_P (type))
18961 return 0;
18962 else if (TYPE_PTRMEM_P (type))
18963 return 0;
18964 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18965 return 0;
18966 else if (TREE_CODE (type) == TYPENAME_TYPE)
18967 return 0;
18968 else if (TREE_CODE (type) == DECLTYPE_TYPE)
18969 return 0;
18970 else if (TREE_CODE (type) == NULLPTR_TYPE)
18971 return 0;
18973 if (complain & tf_error)
18975 if (type == error_mark_node)
18976 inform (input_location, "invalid template non-type parameter");
18977 else
18978 error ("%q#T is not a valid type for a template non-type parameter",
18979 type);
18981 return 1;
18984 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18985 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18987 static bool
18988 dependent_type_p_r (tree type)
18990 tree scope;
18992 /* [temp.dep.type]
18994 A type is dependent if it is:
18996 -- a template parameter. Template template parameters are types
18997 for us (since TYPE_P holds true for them) so we handle
18998 them here. */
18999 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19000 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
19001 return true;
19002 /* -- a qualified-id with a nested-name-specifier which contains a
19003 class-name that names a dependent type or whose unqualified-id
19004 names a dependent type. */
19005 if (TREE_CODE (type) == TYPENAME_TYPE)
19006 return true;
19007 /* -- a cv-qualified type where the cv-unqualified type is
19008 dependent. */
19009 type = TYPE_MAIN_VARIANT (type);
19010 /* -- a compound type constructed from any dependent type. */
19011 if (TYPE_PTRMEM_P (type))
19012 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
19013 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19014 (type)));
19015 else if (TREE_CODE (type) == POINTER_TYPE
19016 || TREE_CODE (type) == REFERENCE_TYPE)
19017 return dependent_type_p (TREE_TYPE (type));
19018 else if (TREE_CODE (type) == FUNCTION_TYPE
19019 || TREE_CODE (type) == METHOD_TYPE)
19021 tree arg_type;
19023 if (dependent_type_p (TREE_TYPE (type)))
19024 return true;
19025 for (arg_type = TYPE_ARG_TYPES (type);
19026 arg_type;
19027 arg_type = TREE_CHAIN (arg_type))
19028 if (dependent_type_p (TREE_VALUE (arg_type)))
19029 return true;
19030 return false;
19032 /* -- an array type constructed from any dependent type or whose
19033 size is specified by a constant expression that is
19034 value-dependent.
19036 We checked for type- and value-dependence of the bounds in
19037 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19038 if (TREE_CODE (type) == ARRAY_TYPE)
19040 if (TYPE_DOMAIN (type)
19041 && dependent_type_p (TYPE_DOMAIN (type)))
19042 return true;
19043 return dependent_type_p (TREE_TYPE (type));
19046 /* -- a template-id in which either the template name is a template
19047 parameter ... */
19048 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19049 return true;
19050 /* ... or any of the template arguments is a dependent type or
19051 an expression that is type-dependent or value-dependent. */
19052 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
19053 && (any_dependent_template_arguments_p
19054 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
19055 return true;
19057 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19058 dependent; if the argument of the `typeof' expression is not
19059 type-dependent, then it should already been have resolved. */
19060 if (TREE_CODE (type) == TYPEOF_TYPE
19061 || TREE_CODE (type) == DECLTYPE_TYPE
19062 || TREE_CODE (type) == UNDERLYING_TYPE)
19063 return true;
19065 /* A template argument pack is dependent if any of its packed
19066 arguments are. */
19067 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
19069 tree args = ARGUMENT_PACK_ARGS (type);
19070 int i, len = TREE_VEC_LENGTH (args);
19071 for (i = 0; i < len; ++i)
19072 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19073 return true;
19076 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19077 be template parameters. */
19078 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
19079 return true;
19081 /* The standard does not specifically mention types that are local
19082 to template functions or local classes, but they should be
19083 considered dependent too. For example:
19085 template <int I> void f() {
19086 enum E { a = I };
19087 S<sizeof (E)> s;
19090 The size of `E' cannot be known until the value of `I' has been
19091 determined. Therefore, `E' must be considered dependent. */
19092 scope = TYPE_CONTEXT (type);
19093 if (scope && TYPE_P (scope))
19094 return dependent_type_p (scope);
19095 /* Don't use type_dependent_expression_p here, as it can lead
19096 to infinite recursion trying to determine whether a lambda
19097 nested in a lambda is dependent (c++/47687). */
19098 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
19099 && DECL_LANG_SPECIFIC (scope)
19100 && DECL_TEMPLATE_INFO (scope)
19101 && (any_dependent_template_arguments_p
19102 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
19103 return true;
19105 /* Other types are non-dependent. */
19106 return false;
19109 /* Returns TRUE if TYPE is dependent, in the sense of
19110 [temp.dep.type]. Note that a NULL type is considered dependent. */
19112 bool
19113 dependent_type_p (tree type)
19115 /* If there are no template parameters in scope, then there can't be
19116 any dependent types. */
19117 if (!processing_template_decl)
19119 /* If we are not processing a template, then nobody should be
19120 providing us with a dependent type. */
19121 gcc_assert (type);
19122 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
19123 return false;
19126 /* If the type is NULL, we have not computed a type for the entity
19127 in question; in that case, the type is dependent. */
19128 if (!type)
19129 return true;
19131 /* Erroneous types can be considered non-dependent. */
19132 if (type == error_mark_node)
19133 return false;
19135 /* If we have not already computed the appropriate value for TYPE,
19136 do so now. */
19137 if (!TYPE_DEPENDENT_P_VALID (type))
19139 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
19140 TYPE_DEPENDENT_P_VALID (type) = 1;
19143 return TYPE_DEPENDENT_P (type);
19146 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19147 lookup. In other words, a dependent type that is not the current
19148 instantiation. */
19150 bool
19151 dependent_scope_p (tree scope)
19153 return (scope && TYPE_P (scope) && dependent_type_p (scope)
19154 && !currently_open_class (scope));
19157 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19158 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19159 expression. */
19161 /* Note that this predicate is not appropriate for general expressions;
19162 only constant expressions (that satisfy potential_constant_expression)
19163 can be tested for value dependence. */
19165 bool
19166 value_dependent_expression_p (tree expression)
19168 if (!processing_template_decl)
19169 return false;
19171 /* A name declared with a dependent type. */
19172 if (DECL_P (expression) && type_dependent_expression_p (expression))
19173 return true;
19175 switch (TREE_CODE (expression))
19177 case IDENTIFIER_NODE:
19178 /* A name that has not been looked up -- must be dependent. */
19179 return true;
19181 case TEMPLATE_PARM_INDEX:
19182 /* A non-type template parm. */
19183 return true;
19185 case CONST_DECL:
19186 /* A non-type template parm. */
19187 if (DECL_TEMPLATE_PARM_P (expression))
19188 return true;
19189 return value_dependent_expression_p (DECL_INITIAL (expression));
19191 case VAR_DECL:
19192 /* A constant with literal type and is initialized
19193 with an expression that is value-dependent. */
19194 if (DECL_INITIAL (expression)
19195 && decl_constant_var_p (expression)
19196 && value_dependent_expression_p (DECL_INITIAL (expression)))
19197 return true;
19198 return false;
19200 case DYNAMIC_CAST_EXPR:
19201 case STATIC_CAST_EXPR:
19202 case CONST_CAST_EXPR:
19203 case REINTERPRET_CAST_EXPR:
19204 case CAST_EXPR:
19205 /* These expressions are value-dependent if the type to which
19206 the cast occurs is dependent or the expression being casted
19207 is value-dependent. */
19209 tree type = TREE_TYPE (expression);
19211 if (dependent_type_p (type))
19212 return true;
19214 /* A functional cast has a list of operands. */
19215 expression = TREE_OPERAND (expression, 0);
19216 if (!expression)
19218 /* If there are no operands, it must be an expression such
19219 as "int()". This should not happen for aggregate types
19220 because it would form non-constant expressions. */
19221 gcc_assert (cxx_dialect >= cxx0x
19222 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
19224 return false;
19227 if (TREE_CODE (expression) == TREE_LIST)
19228 return any_value_dependent_elements_p (expression);
19230 return value_dependent_expression_p (expression);
19233 case SIZEOF_EXPR:
19234 case ALIGNOF_EXPR:
19235 case TYPEID_EXPR:
19236 /* A `sizeof' expression is value-dependent if the operand is
19237 type-dependent or is a pack expansion. */
19238 expression = TREE_OPERAND (expression, 0);
19239 if (PACK_EXPANSION_P (expression))
19240 return true;
19241 else if (TYPE_P (expression))
19242 return dependent_type_p (expression);
19243 return instantiation_dependent_expression_p (expression);
19245 case AT_ENCODE_EXPR:
19246 /* An 'encode' expression is value-dependent if the operand is
19247 type-dependent. */
19248 expression = TREE_OPERAND (expression, 0);
19249 return dependent_type_p (expression);
19251 case NOEXCEPT_EXPR:
19252 expression = TREE_OPERAND (expression, 0);
19253 return instantiation_dependent_expression_p (expression);
19255 case SCOPE_REF:
19256 /* instantiation_dependent_r treats this as dependent so that we
19257 check access at instantiation time, and all instantiation-dependent
19258 expressions should also be considered value-dependent. */
19259 return true;
19261 case COMPONENT_REF:
19262 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
19263 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
19265 case NONTYPE_ARGUMENT_PACK:
19266 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19267 is value-dependent. */
19269 tree values = ARGUMENT_PACK_ARGS (expression);
19270 int i, len = TREE_VEC_LENGTH (values);
19272 for (i = 0; i < len; ++i)
19273 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
19274 return true;
19276 return false;
19279 case TRAIT_EXPR:
19281 tree type2 = TRAIT_EXPR_TYPE2 (expression);
19282 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
19283 || (type2 ? dependent_type_p (type2) : false));
19286 case MODOP_EXPR:
19287 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19288 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
19290 case ARRAY_REF:
19291 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19292 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
19294 case ADDR_EXPR:
19296 tree op = TREE_OPERAND (expression, 0);
19297 return (value_dependent_expression_p (op)
19298 || has_value_dependent_address (op));
19301 case CALL_EXPR:
19303 tree fn = get_callee_fndecl (expression);
19304 int i, nargs;
19305 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
19306 return true;
19307 nargs = call_expr_nargs (expression);
19308 for (i = 0; i < nargs; ++i)
19310 tree op = CALL_EXPR_ARG (expression, i);
19311 /* In a call to a constexpr member function, look through the
19312 implicit ADDR_EXPR on the object argument so that it doesn't
19313 cause the call to be considered value-dependent. We also
19314 look through it in potential_constant_expression. */
19315 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
19316 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19317 && TREE_CODE (op) == ADDR_EXPR)
19318 op = TREE_OPERAND (op, 0);
19319 if (value_dependent_expression_p (op))
19320 return true;
19322 return false;
19325 case TEMPLATE_ID_EXPR:
19326 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19327 type-dependent. */
19328 return type_dependent_expression_p (expression);
19330 case CONSTRUCTOR:
19332 unsigned ix;
19333 tree val;
19334 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
19335 if (value_dependent_expression_p (val))
19336 return true;
19337 return false;
19340 case STMT_EXPR:
19341 /* Treat a GNU statement expression as dependent to avoid crashing
19342 under fold_non_dependent_expr; it can't be constant. */
19343 return true;
19345 default:
19346 /* A constant expression is value-dependent if any subexpression is
19347 value-dependent. */
19348 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
19350 case tcc_reference:
19351 case tcc_unary:
19352 case tcc_comparison:
19353 case tcc_binary:
19354 case tcc_expression:
19355 case tcc_vl_exp:
19357 int i, len = cp_tree_operand_length (expression);
19359 for (i = 0; i < len; i++)
19361 tree t = TREE_OPERAND (expression, i);
19363 /* In some cases, some of the operands may be missing.l
19364 (For example, in the case of PREDECREMENT_EXPR, the
19365 amount to increment by may be missing.) That doesn't
19366 make the expression dependent. */
19367 if (t && value_dependent_expression_p (t))
19368 return true;
19371 break;
19372 default:
19373 break;
19375 break;
19378 /* The expression is not value-dependent. */
19379 return false;
19382 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19383 [temp.dep.expr]. Note that an expression with no type is
19384 considered dependent. Other parts of the compiler arrange for an
19385 expression with type-dependent subexpressions to have no type, so
19386 this function doesn't have to be fully recursive. */
19388 bool
19389 type_dependent_expression_p (tree expression)
19391 if (!processing_template_decl)
19392 return false;
19394 if (expression == error_mark_node)
19395 return false;
19397 /* An unresolved name is always dependent. */
19398 if (TREE_CODE (expression) == IDENTIFIER_NODE
19399 || TREE_CODE (expression) == USING_DECL)
19400 return true;
19402 /* Some expression forms are never type-dependent. */
19403 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19404 || TREE_CODE (expression) == SIZEOF_EXPR
19405 || TREE_CODE (expression) == ALIGNOF_EXPR
19406 || TREE_CODE (expression) == AT_ENCODE_EXPR
19407 || TREE_CODE (expression) == NOEXCEPT_EXPR
19408 || TREE_CODE (expression) == TRAIT_EXPR
19409 || TREE_CODE (expression) == TYPEID_EXPR
19410 || TREE_CODE (expression) == DELETE_EXPR
19411 || TREE_CODE (expression) == VEC_DELETE_EXPR
19412 || TREE_CODE (expression) == THROW_EXPR)
19413 return false;
19415 /* The types of these expressions depends only on the type to which
19416 the cast occurs. */
19417 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19418 || TREE_CODE (expression) == STATIC_CAST_EXPR
19419 || TREE_CODE (expression) == CONST_CAST_EXPR
19420 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19421 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
19422 || TREE_CODE (expression) == CAST_EXPR)
19423 return dependent_type_p (TREE_TYPE (expression));
19425 /* The types of these expressions depends only on the type created
19426 by the expression. */
19427 if (TREE_CODE (expression) == NEW_EXPR
19428 || TREE_CODE (expression) == VEC_NEW_EXPR)
19430 /* For NEW_EXPR tree nodes created inside a template, either
19431 the object type itself or a TREE_LIST may appear as the
19432 operand 1. */
19433 tree type = TREE_OPERAND (expression, 1);
19434 if (TREE_CODE (type) == TREE_LIST)
19435 /* This is an array type. We need to check array dimensions
19436 as well. */
19437 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19438 || value_dependent_expression_p
19439 (TREE_OPERAND (TREE_VALUE (type), 1));
19440 else
19441 return dependent_type_p (type);
19444 if (TREE_CODE (expression) == SCOPE_REF)
19446 tree scope = TREE_OPERAND (expression, 0);
19447 tree name = TREE_OPERAND (expression, 1);
19449 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19450 contains an identifier associated by name lookup with one or more
19451 declarations declared with a dependent type, or...a
19452 nested-name-specifier or qualified-id that names a member of an
19453 unknown specialization. */
19454 return (type_dependent_expression_p (name)
19455 || dependent_scope_p (scope));
19458 if (TREE_CODE (expression) == FUNCTION_DECL
19459 && DECL_LANG_SPECIFIC (expression)
19460 && DECL_TEMPLATE_INFO (expression)
19461 && (any_dependent_template_arguments_p
19462 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19463 return true;
19465 if (TREE_CODE (expression) == TEMPLATE_DECL
19466 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19467 return false;
19469 if (TREE_CODE (expression) == STMT_EXPR)
19470 expression = stmt_expr_value_expr (expression);
19472 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19474 tree elt;
19475 unsigned i;
19477 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19479 if (type_dependent_expression_p (elt))
19480 return true;
19482 return false;
19485 /* A static data member of the current instantiation with incomplete
19486 array type is type-dependent, as the definition and specializations
19487 can have different bounds. */
19488 if (TREE_CODE (expression) == VAR_DECL
19489 && DECL_CLASS_SCOPE_P (expression)
19490 && dependent_type_p (DECL_CONTEXT (expression))
19491 && VAR_HAD_UNKNOWN_BOUND (expression))
19492 return true;
19494 if (TREE_TYPE (expression) == unknown_type_node)
19496 if (TREE_CODE (expression) == ADDR_EXPR)
19497 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19498 if (TREE_CODE (expression) == COMPONENT_REF
19499 || TREE_CODE (expression) == OFFSET_REF)
19501 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19502 return true;
19503 expression = TREE_OPERAND (expression, 1);
19504 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19505 return false;
19507 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19508 if (TREE_CODE (expression) == SCOPE_REF)
19509 return false;
19511 if (BASELINK_P (expression))
19512 expression = BASELINK_FUNCTIONS (expression);
19514 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19516 if (any_dependent_template_arguments_p
19517 (TREE_OPERAND (expression, 1)))
19518 return true;
19519 expression = TREE_OPERAND (expression, 0);
19521 gcc_assert (TREE_CODE (expression) == OVERLOAD
19522 || TREE_CODE (expression) == FUNCTION_DECL);
19524 while (expression)
19526 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19527 return true;
19528 expression = OVL_NEXT (expression);
19530 return false;
19533 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19535 return (dependent_type_p (TREE_TYPE (expression)));
19538 /* walk_tree callback function for instantiation_dependent_expression_p,
19539 below. Returns non-zero if a dependent subexpression is found. */
19541 static tree
19542 instantiation_dependent_r (tree *tp, int *walk_subtrees,
19543 void *data ATTRIBUTE_UNUSED)
19545 if (TYPE_P (*tp))
19547 /* We don't have to worry about decltype currently because decltype
19548 of an instantiation-dependent expr is a dependent type. This
19549 might change depending on the resolution of DR 1172. */
19550 *walk_subtrees = false;
19551 return NULL_TREE;
19553 enum tree_code code = TREE_CODE (*tp);
19554 switch (code)
19556 /* Don't treat an argument list as dependent just because it has no
19557 TREE_TYPE. */
19558 case TREE_LIST:
19559 case TREE_VEC:
19560 return NULL_TREE;
19562 case TEMPLATE_PARM_INDEX:
19563 return *tp;
19565 /* Handle expressions with type operands. */
19566 case SIZEOF_EXPR:
19567 case ALIGNOF_EXPR:
19568 case TYPEID_EXPR:
19569 case AT_ENCODE_EXPR:
19570 case TRAIT_EXPR:
19572 tree op = TREE_OPERAND (*tp, 0);
19573 if (TYPE_P (op))
19575 if (dependent_type_p (op)
19576 || (code == TRAIT_EXPR
19577 && dependent_type_p (TREE_OPERAND (*tp, 1))))
19578 return *tp;
19579 else
19581 *walk_subtrees = false;
19582 return NULL_TREE;
19585 break;
19588 case COMPONENT_REF:
19589 if (TREE_CODE (TREE_OPERAND (*tp, 1)) == IDENTIFIER_NODE)
19590 /* In a template, finish_class_member_access_expr creates a
19591 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19592 type-dependent, so that we can check access control at
19593 instantiation time (PR 42277). See also Core issue 1273. */
19594 return *tp;
19595 break;
19597 case SCOPE_REF:
19598 /* Similarly, finish_qualified_id_expr builds up a SCOPE_REF in a
19599 template so that we can check access at instantiation time even
19600 though we know which member it resolves to. */
19601 return *tp;
19603 default:
19604 break;
19607 if (type_dependent_expression_p (*tp))
19608 return *tp;
19609 else
19610 return NULL_TREE;
19613 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19614 sense defined by the ABI:
19616 "An expression is instantiation-dependent if it is type-dependent
19617 or value-dependent, or it has a subexpression that is type-dependent
19618 or value-dependent." */
19620 bool
19621 instantiation_dependent_expression_p (tree expression)
19623 tree result;
19625 if (!processing_template_decl)
19626 return false;
19628 if (expression == error_mark_node)
19629 return false;
19631 result = cp_walk_tree_without_duplicates (&expression,
19632 instantiation_dependent_r, NULL);
19633 return result != NULL_TREE;
19636 /* Like type_dependent_expression_p, but it also works while not processing
19637 a template definition, i.e. during substitution or mangling. */
19639 bool
19640 type_dependent_expression_p_push (tree expr)
19642 bool b;
19643 ++processing_template_decl;
19644 b = type_dependent_expression_p (expr);
19645 --processing_template_decl;
19646 return b;
19649 /* Returns TRUE if ARGS contains a type-dependent expression. */
19651 bool
19652 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
19654 unsigned int i;
19655 tree arg;
19657 FOR_EACH_VEC_ELT (tree, args, i, arg)
19659 if (type_dependent_expression_p (arg))
19660 return true;
19662 return false;
19665 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19666 expressions) contains any type-dependent expressions. */
19668 bool
19669 any_type_dependent_elements_p (const_tree list)
19671 for (; list; list = TREE_CHAIN (list))
19672 if (value_dependent_expression_p (TREE_VALUE (list)))
19673 return true;
19675 return false;
19678 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19679 expressions) contains any value-dependent expressions. */
19681 bool
19682 any_value_dependent_elements_p (const_tree list)
19684 for (; list; list = TREE_CHAIN (list))
19685 if (value_dependent_expression_p (TREE_VALUE (list)))
19686 return true;
19688 return false;
19691 /* Returns TRUE if the ARG (a template argument) is dependent. */
19693 bool
19694 dependent_template_arg_p (tree arg)
19696 if (!processing_template_decl)
19697 return false;
19699 /* Assume a template argument that was wrongly written by the user
19700 is dependent. This is consistent with what
19701 any_dependent_template_arguments_p [that calls this function]
19702 does. */
19703 if (!arg || arg == error_mark_node)
19704 return true;
19706 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
19707 arg = ARGUMENT_PACK_SELECT_ARG (arg);
19709 if (TREE_CODE (arg) == TEMPLATE_DECL
19710 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19711 return dependent_template_p (arg);
19712 else if (ARGUMENT_PACK_P (arg))
19714 tree args = ARGUMENT_PACK_ARGS (arg);
19715 int i, len = TREE_VEC_LENGTH (args);
19716 for (i = 0; i < len; ++i)
19718 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19719 return true;
19722 return false;
19724 else if (TYPE_P (arg))
19725 return dependent_type_p (arg);
19726 else
19727 return (type_dependent_expression_p (arg)
19728 || value_dependent_expression_p (arg));
19731 /* Returns true if ARGS (a collection of template arguments) contains
19732 any types that require structural equality testing. */
19734 bool
19735 any_template_arguments_need_structural_equality_p (tree args)
19737 int i;
19738 int j;
19740 if (!args)
19741 return false;
19742 if (args == error_mark_node)
19743 return true;
19745 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19747 tree level = TMPL_ARGS_LEVEL (args, i + 1);
19748 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19750 tree arg = TREE_VEC_ELT (level, j);
19751 tree packed_args = NULL_TREE;
19752 int k, len = 1;
19754 if (ARGUMENT_PACK_P (arg))
19756 /* Look inside the argument pack. */
19757 packed_args = ARGUMENT_PACK_ARGS (arg);
19758 len = TREE_VEC_LENGTH (packed_args);
19761 for (k = 0; k < len; ++k)
19763 if (packed_args)
19764 arg = TREE_VEC_ELT (packed_args, k);
19766 if (error_operand_p (arg))
19767 return true;
19768 else if (TREE_CODE (arg) == TEMPLATE_DECL
19769 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19770 continue;
19771 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
19772 return true;
19773 else if (!TYPE_P (arg) && TREE_TYPE (arg)
19774 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
19775 return true;
19780 return false;
19783 /* Returns true if ARGS (a collection of template arguments) contains
19784 any dependent arguments. */
19786 bool
19787 any_dependent_template_arguments_p (const_tree args)
19789 int i;
19790 int j;
19792 if (!args)
19793 return false;
19794 if (args == error_mark_node)
19795 return true;
19797 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19799 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
19800 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19801 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
19802 return true;
19805 return false;
19808 /* Returns TRUE if the template TMPL is dependent. */
19810 bool
19811 dependent_template_p (tree tmpl)
19813 if (TREE_CODE (tmpl) == OVERLOAD)
19815 while (tmpl)
19817 if (dependent_template_p (OVL_CURRENT (tmpl)))
19818 return true;
19819 tmpl = OVL_NEXT (tmpl);
19821 return false;
19824 /* Template template parameters are dependent. */
19825 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
19826 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
19827 return true;
19828 /* So are names that have not been looked up. */
19829 if (TREE_CODE (tmpl) == SCOPE_REF
19830 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
19831 return true;
19832 /* So are member templates of dependent classes. */
19833 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
19834 return dependent_type_p (DECL_CONTEXT (tmpl));
19835 return false;
19838 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19840 bool
19841 dependent_template_id_p (tree tmpl, tree args)
19843 return (dependent_template_p (tmpl)
19844 || any_dependent_template_arguments_p (args));
19847 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19848 is dependent. */
19850 bool
19851 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
19853 int i;
19855 if (!processing_template_decl)
19856 return false;
19858 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
19860 tree decl = TREE_VEC_ELT (declv, i);
19861 tree init = TREE_VEC_ELT (initv, i);
19862 tree cond = TREE_VEC_ELT (condv, i);
19863 tree incr = TREE_VEC_ELT (incrv, i);
19865 if (type_dependent_expression_p (decl))
19866 return true;
19868 if (init && type_dependent_expression_p (init))
19869 return true;
19871 if (type_dependent_expression_p (cond))
19872 return true;
19874 if (COMPARISON_CLASS_P (cond)
19875 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
19876 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
19877 return true;
19879 if (TREE_CODE (incr) == MODOP_EXPR)
19881 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
19882 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
19883 return true;
19885 else if (type_dependent_expression_p (incr))
19886 return true;
19887 else if (TREE_CODE (incr) == MODIFY_EXPR)
19889 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
19890 return true;
19891 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
19893 tree t = TREE_OPERAND (incr, 1);
19894 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
19895 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
19896 return true;
19901 return false;
19904 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19905 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19906 no such TYPE can be found. Note that this function peers inside
19907 uninstantiated templates and therefore should be used only in
19908 extremely limited situations. ONLY_CURRENT_P restricts this
19909 peering to the currently open classes hierarchy (which is required
19910 when comparing types). */
19912 tree
19913 resolve_typename_type (tree type, bool only_current_p)
19915 tree scope;
19916 tree name;
19917 tree decl;
19918 int quals;
19919 tree pushed_scope;
19920 tree result;
19922 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
19924 scope = TYPE_CONTEXT (type);
19925 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19926 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19927 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19928 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19929 identifier of the TYPENAME_TYPE anymore.
19930 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19931 TYPENAME_TYPE instead, we avoid messing up with a possible
19932 typedef variant case. */
19933 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
19935 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19936 it first before we can figure out what NAME refers to. */
19937 if (TREE_CODE (scope) == TYPENAME_TYPE)
19938 scope = resolve_typename_type (scope, only_current_p);
19939 /* If we don't know what SCOPE refers to, then we cannot resolve the
19940 TYPENAME_TYPE. */
19941 if (TREE_CODE (scope) == TYPENAME_TYPE)
19942 return type;
19943 /* If the SCOPE is a template type parameter, we have no way of
19944 resolving the name. */
19945 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
19946 return type;
19947 /* If the SCOPE is not the current instantiation, there's no reason
19948 to look inside it. */
19949 if (only_current_p && !currently_open_class (scope))
19950 return type;
19951 /* If this is a typedef, we don't want to look inside (c++/11987). */
19952 if (typedef_variant_p (type))
19953 return type;
19954 /* If SCOPE isn't the template itself, it will not have a valid
19955 TYPE_FIELDS list. */
19956 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
19957 /* scope is either the template itself or a compatible instantiation
19958 like X<T>, so look up the name in the original template. */
19959 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
19960 else
19961 /* scope is a partial instantiation, so we can't do the lookup or we
19962 will lose the template arguments. */
19963 return type;
19964 /* Enter the SCOPE so that name lookup will be resolved as if we
19965 were in the class definition. In particular, SCOPE will no
19966 longer be considered a dependent type. */
19967 pushed_scope = push_scope (scope);
19968 /* Look up the declaration. */
19969 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
19970 tf_warning_or_error);
19972 result = NULL_TREE;
19974 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19975 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19976 if (!decl)
19977 /*nop*/;
19978 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
19979 && TREE_CODE (decl) == TYPE_DECL)
19981 result = TREE_TYPE (decl);
19982 if (result == error_mark_node)
19983 result = NULL_TREE;
19985 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
19986 && DECL_CLASS_TEMPLATE_P (decl))
19988 tree tmpl;
19989 tree args;
19990 /* Obtain the template and the arguments. */
19991 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
19992 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
19993 /* Instantiate the template. */
19994 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
19995 /*entering_scope=*/0,
19996 tf_error | tf_user);
19997 if (result == error_mark_node)
19998 result = NULL_TREE;
20001 /* Leave the SCOPE. */
20002 if (pushed_scope)
20003 pop_scope (pushed_scope);
20005 /* If we failed to resolve it, return the original typename. */
20006 if (!result)
20007 return type;
20009 /* If lookup found a typename type, resolve that too. */
20010 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
20012 /* Ill-formed programs can cause infinite recursion here, so we
20013 must catch that. */
20014 TYPENAME_IS_RESOLVING_P (type) = 1;
20015 result = resolve_typename_type (result, only_current_p);
20016 TYPENAME_IS_RESOLVING_P (type) = 0;
20019 /* Qualify the resulting type. */
20020 quals = cp_type_quals (type);
20021 if (quals)
20022 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
20024 return result;
20027 /* EXPR is an expression which is not type-dependent. Return a proxy
20028 for EXPR that can be used to compute the types of larger
20029 expressions containing EXPR. */
20031 tree
20032 build_non_dependent_expr (tree expr)
20034 tree inner_expr;
20036 #ifdef ENABLE_CHECKING
20037 /* Try to get a constant value for all non-type-dependent expressions in
20038 order to expose bugs in *_dependent_expression_p and constexpr. */
20039 if (cxx_dialect >= cxx0x)
20040 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
20041 #endif
20043 /* Preserve OVERLOADs; the functions must be available to resolve
20044 types. */
20045 inner_expr = expr;
20046 if (TREE_CODE (inner_expr) == STMT_EXPR)
20047 inner_expr = stmt_expr_value_expr (inner_expr);
20048 if (TREE_CODE (inner_expr) == ADDR_EXPR)
20049 inner_expr = TREE_OPERAND (inner_expr, 0);
20050 if (TREE_CODE (inner_expr) == COMPONENT_REF)
20051 inner_expr = TREE_OPERAND (inner_expr, 1);
20052 if (is_overloaded_fn (inner_expr)
20053 || TREE_CODE (inner_expr) == OFFSET_REF)
20054 return expr;
20055 /* There is no need to return a proxy for a variable. */
20056 if (TREE_CODE (expr) == VAR_DECL)
20057 return expr;
20058 /* Preserve string constants; conversions from string constants to
20059 "char *" are allowed, even though normally a "const char *"
20060 cannot be used to initialize a "char *". */
20061 if (TREE_CODE (expr) == STRING_CST)
20062 return expr;
20063 /* Preserve arithmetic constants, as an optimization -- there is no
20064 reason to create a new node. */
20065 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
20066 return expr;
20067 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20068 There is at least one place where we want to know that a
20069 particular expression is a throw-expression: when checking a ?:
20070 expression, there are special rules if the second or third
20071 argument is a throw-expression. */
20072 if (TREE_CODE (expr) == THROW_EXPR)
20073 return expr;
20075 /* Don't wrap an initializer list, we need to be able to look inside. */
20076 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
20077 return expr;
20079 /* Don't wrap a dummy object, we need to be able to test for it. */
20080 if (is_dummy_object (expr))
20081 return expr;
20083 if (TREE_CODE (expr) == COND_EXPR)
20084 return build3 (COND_EXPR,
20085 TREE_TYPE (expr),
20086 TREE_OPERAND (expr, 0),
20087 (TREE_OPERAND (expr, 1)
20088 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
20089 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
20090 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
20091 if (TREE_CODE (expr) == COMPOUND_EXPR
20092 && !COMPOUND_EXPR_OVERLOADED (expr))
20093 return build2 (COMPOUND_EXPR,
20094 TREE_TYPE (expr),
20095 TREE_OPERAND (expr, 0),
20096 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
20098 /* If the type is unknown, it can't really be non-dependent */
20099 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
20101 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20102 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
20105 /* ARGS is a vector of expressions as arguments to a function call.
20106 Replace the arguments with equivalent non-dependent expressions.
20107 This modifies ARGS in place. */
20109 void
20110 make_args_non_dependent (VEC(tree,gc) *args)
20112 unsigned int ix;
20113 tree arg;
20115 FOR_EACH_VEC_ELT (tree, args, ix, arg)
20117 tree newarg = build_non_dependent_expr (arg);
20118 if (newarg != arg)
20119 VEC_replace (tree, args, ix, newarg);
20123 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20124 with a level one deeper than the actual template parms. */
20126 tree
20127 make_auto (void)
20129 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
20130 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
20131 TYPE_DECL, get_identifier ("auto"), au);
20132 TYPE_STUB_DECL (au) = TYPE_NAME (au);
20133 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
20134 (0, processing_template_decl + 1, processing_template_decl + 1,
20135 TYPE_NAME (au), NULL_TREE);
20136 TYPE_CANONICAL (au) = canonical_type_parameter (au);
20137 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
20138 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
20140 return au;
20143 /* Given type ARG, return std::initializer_list<ARG>. */
20145 static tree
20146 listify (tree arg)
20148 tree std_init_list = namespace_binding
20149 (get_identifier ("initializer_list"), std_node);
20150 tree argvec;
20151 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
20153 error ("deducing from brace-enclosed initializer list requires "
20154 "#include <initializer_list>");
20155 return error_mark_node;
20157 argvec = make_tree_vec (1);
20158 TREE_VEC_ELT (argvec, 0) = arg;
20159 return lookup_template_class (std_init_list, argvec, NULL_TREE,
20160 NULL_TREE, 0, tf_warning_or_error);
20163 /* Replace auto in TYPE with std::initializer_list<auto>. */
20165 static tree
20166 listify_autos (tree type, tree auto_node)
20168 tree init_auto = listify (auto_node);
20169 tree argvec = make_tree_vec (1);
20170 TREE_VEC_ELT (argvec, 0) = init_auto;
20171 if (processing_template_decl)
20172 argvec = add_to_template_args (current_template_args (), argvec);
20173 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20176 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20177 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20179 tree
20180 do_auto_deduction (tree type, tree init, tree auto_node)
20182 tree parms, tparms, targs;
20183 tree args[1];
20184 int val;
20186 if (init == error_mark_node)
20187 return error_mark_node;
20189 if (type_dependent_expression_p (init))
20190 /* Defining a subset of type-dependent expressions that we can deduce
20191 from ahead of time isn't worth the trouble. */
20192 return type;
20194 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20195 with either a new invented type template parameter U or, if the
20196 initializer is a braced-init-list (8.5.4), with
20197 std::initializer_list<U>. */
20198 if (BRACE_ENCLOSED_INITIALIZER_P (init))
20199 type = listify_autos (type, auto_node);
20201 init = resolve_nondeduced_context (init);
20203 parms = build_tree_list (NULL_TREE, type);
20204 args[0] = init;
20205 tparms = make_tree_vec (1);
20206 targs = make_tree_vec (1);
20207 TREE_VEC_ELT (tparms, 0)
20208 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
20209 val = type_unification_real (tparms, targs, parms, args, 1, 0,
20210 DEDUCE_CALL, LOOKUP_NORMAL,
20211 /*explain_p=*/false);
20212 if (val > 0)
20214 if (processing_template_decl)
20215 /* Try again at instantiation time. */
20216 return type;
20217 if (type && type != error_mark_node)
20218 /* If type is error_mark_node a diagnostic must have been
20219 emitted by now. Also, having a mention to '<type error>'
20220 in the diagnostic is not really useful to the user. */
20222 if (cfun && auto_node == current_function_auto_return_pattern
20223 && LAMBDA_FUNCTION_P (current_function_decl))
20224 error ("unable to deduce lambda return type from %qE", init);
20225 else
20226 error ("unable to deduce %qT from %qE", type, init);
20228 return error_mark_node;
20231 /* If the list of declarators contains more than one declarator, the type
20232 of each declared variable is determined as described above. If the
20233 type deduced for the template parameter U is not the same in each
20234 deduction, the program is ill-formed. */
20235 if (TREE_TYPE (auto_node)
20236 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
20238 if (cfun && auto_node == current_function_auto_return_pattern
20239 && LAMBDA_FUNCTION_P (current_function_decl))
20240 error ("inconsistent types %qT and %qT deduced for "
20241 "lambda return type", TREE_TYPE (auto_node),
20242 TREE_VEC_ELT (targs, 0));
20243 else
20244 error ("inconsistent deduction for %qT: %qT and then %qT",
20245 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
20246 return error_mark_node;
20248 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
20250 if (processing_template_decl)
20251 targs = add_to_template_args (current_template_args (), targs);
20252 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
20255 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20256 result. */
20258 tree
20259 splice_late_return_type (tree type, tree late_return_type)
20261 tree argvec;
20263 if (late_return_type == NULL_TREE)
20264 return type;
20265 argvec = make_tree_vec (1);
20266 TREE_VEC_ELT (argvec, 0) = late_return_type;
20267 if (processing_template_parmlist)
20268 /* For a late-specified return type in a template type-parameter, we
20269 need to add a dummy argument level for its parmlist. */
20270 argvec = add_to_template_args
20271 (make_tree_vec (processing_template_parmlist), argvec);
20272 if (current_template_parms)
20273 argvec = add_to_template_args (current_template_args (), argvec);
20274 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20277 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20279 bool
20280 is_auto (const_tree type)
20282 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20283 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
20284 return true;
20285 else
20286 return false;
20289 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20290 appear as a type-specifier for the declaration in question, we don't
20291 have to look through the whole type. */
20293 tree
20294 type_uses_auto (tree type)
20296 enum tree_code code;
20297 if (is_auto (type))
20298 return type;
20300 code = TREE_CODE (type);
20302 if (code == POINTER_TYPE || code == REFERENCE_TYPE
20303 || code == OFFSET_TYPE || code == FUNCTION_TYPE
20304 || code == METHOD_TYPE || code == ARRAY_TYPE)
20305 return type_uses_auto (TREE_TYPE (type));
20307 if (TYPE_PTRMEMFUNC_P (type))
20308 return type_uses_auto (TREE_TYPE (TREE_TYPE
20309 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
20311 return NULL_TREE;
20314 /* For a given template T, return the vector of typedefs referenced
20315 in T for which access check is needed at T instantiation time.
20316 T is either a FUNCTION_DECL or a RECORD_TYPE.
20317 Those typedefs were added to T by the function
20318 append_type_to_template_for_access_check. */
20320 VEC(qualified_typedef_usage_t,gc)*
20321 get_types_needing_access_check (tree t)
20323 tree ti;
20324 VEC(qualified_typedef_usage_t,gc) *result = NULL;
20326 if (!t || t == error_mark_node)
20327 return NULL;
20329 if (!(ti = get_template_info (t)))
20330 return NULL;
20332 if (CLASS_TYPE_P (t)
20333 || TREE_CODE (t) == FUNCTION_DECL)
20335 if (!TI_TEMPLATE (ti))
20336 return NULL;
20338 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
20341 return result;
20344 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20345 tied to T. That list of typedefs will be access checked at
20346 T instantiation time.
20347 T is either a FUNCTION_DECL or a RECORD_TYPE.
20348 TYPE_DECL is a TYPE_DECL node representing a typedef.
20349 SCOPE is the scope through which TYPE_DECL is accessed.
20350 LOCATION is the location of the usage point of TYPE_DECL.
20352 This function is a subroutine of
20353 append_type_to_template_for_access_check. */
20355 static void
20356 append_type_to_template_for_access_check_1 (tree t,
20357 tree type_decl,
20358 tree scope,
20359 location_t location)
20361 qualified_typedef_usage_t typedef_usage;
20362 tree ti;
20364 if (!t || t == error_mark_node)
20365 return;
20367 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
20368 || CLASS_TYPE_P (t))
20369 && type_decl
20370 && TREE_CODE (type_decl) == TYPE_DECL
20371 && scope);
20373 if (!(ti = get_template_info (t)))
20374 return;
20376 gcc_assert (TI_TEMPLATE (ti));
20378 typedef_usage.typedef_decl = type_decl;
20379 typedef_usage.context = scope;
20380 typedef_usage.locus = location;
20382 VEC_safe_push (qualified_typedef_usage_t, gc,
20383 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
20384 &typedef_usage);
20387 /* Append TYPE_DECL to the template TEMPL.
20388 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20389 At TEMPL instanciation time, TYPE_DECL will be checked to see
20390 if it can be accessed through SCOPE.
20391 LOCATION is the location of the usage point of TYPE_DECL.
20393 e.g. consider the following code snippet:
20395 class C
20397 typedef int myint;
20400 template<class U> struct S
20402 C::myint mi; // <-- usage point of the typedef C::myint
20405 S<char> s;
20407 At S<char> instantiation time, we need to check the access of C::myint
20408 In other words, we need to check the access of the myint typedef through
20409 the C scope. For that purpose, this function will add the myint typedef
20410 and the scope C through which its being accessed to a list of typedefs
20411 tied to the template S. That list will be walked at template instantiation
20412 time and access check performed on each typedefs it contains.
20413 Note that this particular code snippet should yield an error because
20414 myint is private to C. */
20416 void
20417 append_type_to_template_for_access_check (tree templ,
20418 tree type_decl,
20419 tree scope,
20420 location_t location)
20422 qualified_typedef_usage_t *iter;
20423 int i;
20425 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
20427 /* Make sure we don't append the type to the template twice. */
20428 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
20429 get_types_needing_access_check (templ),
20430 i, iter)
20431 if (iter->typedef_decl == type_decl && scope == iter->context)
20432 return;
20434 append_type_to_template_for_access_check_1 (templ, type_decl,
20435 scope, location);
20438 /* Set up the hash tables for template instantiations. */
20440 void
20441 init_template_processing (void)
20443 decl_specializations = htab_create_ggc (37,
20444 hash_specialization,
20445 eq_specializations,
20446 ggc_free);
20447 type_specializations = htab_create_ggc (37,
20448 hash_specialization,
20449 eq_specializations,
20450 ggc_free);
20453 /* Print stats about the template hash tables for -fstats. */
20455 void
20456 print_template_statistics (void)
20458 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
20459 "%f collisions\n", (long) htab_size (decl_specializations),
20460 (long) htab_elements (decl_specializations),
20461 htab_collisions (decl_specializations));
20462 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
20463 "%f collisions\n", (long) htab_size (type_specializations),
20464 (long) htab_elements (type_specializations),
20465 htab_collisions (type_specializations));
20468 #include "gt-cp-pt.h"