* pt.c (instantiate_decl): Check typedefs access here.
[official-gcc.git] / gcc / cp / pt.c
blob95c6464e61b43cbdb3e034f7f3ce440b2014898e
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 typedef struct GTY(()) spec_entry
85 tree tmpl;
86 tree args;
87 tree spec;
88 } spec_entry;
90 static GTY ((param_is (spec_entry)))
91 htab_t decl_specializations;
93 static GTY ((param_is (spec_entry)))
94 htab_t type_specializations;
96 /* Contains canonical template parameter types. The vector is indexed by
97 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
98 TREE_LIST, whose TREE_VALUEs contain the canonical template
99 parameters of various types and levels. */
100 static GTY(()) VEC(tree,gc) *canonical_template_parms;
102 #define UNIFY_ALLOW_NONE 0
103 #define UNIFY_ALLOW_MORE_CV_QUAL 1
104 #define UNIFY_ALLOW_LESS_CV_QUAL 2
105 #define UNIFY_ALLOW_DERIVED 4
106 #define UNIFY_ALLOW_INTEGER 8
107 #define UNIFY_ALLOW_OUTER_LEVEL 16
108 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
109 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111 enum template_base_result {
112 tbr_incomplete_type,
113 tbr_ambiguous_baseclass,
114 tbr_success
117 static void push_access_scope (tree);
118 static void pop_access_scope (tree);
119 static bool resolve_overloaded_unification (tree, tree, tree, tree,
120 unification_kind_t, int,
121 bool);
122 static int try_one_overload (tree, tree, tree, tree, tree,
123 unification_kind_t, int, bool, bool);
124 static int unify (tree, tree, tree, tree, int, bool);
125 static void add_pending_template (tree);
126 static tree reopen_tinst_level (struct tinst_level *);
127 static tree tsubst_initializer_list (tree, tree);
128 static tree get_class_bindings (tree, tree, tree);
129 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
130 bool, bool);
131 static void tsubst_enum (tree, tree, tree);
132 static tree add_to_template_args (tree, tree);
133 static tree add_outermost_template_args (tree, tree);
134 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
135 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
136 tree);
137 static int type_unification_real (tree, tree, tree, const tree *,
138 unsigned int, int, unification_kind_t, int,
139 bool);
140 static void note_template_header (int);
141 static tree convert_nontype_argument_function (tree, tree);
142 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
143 static tree convert_template_argument (tree, tree, tree,
144 tsubst_flags_t, int, tree);
145 static int for_each_template_parm (tree, tree_fn_t, void*,
146 struct pointer_set_t*, bool);
147 static tree expand_template_argument_pack (tree);
148 static tree build_template_parm_index (int, int, int, tree, tree);
149 static bool inline_needs_template_parms (tree);
150 static void push_inline_template_parms_recursive (tree, int);
151 static tree retrieve_local_specialization (tree);
152 static void register_local_specialization (tree, tree);
153 static hashval_t hash_specialization (const void *p);
154 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
155 static int mark_template_parm (tree, void *);
156 static int template_parm_this_level_p (tree, void *);
157 static tree tsubst_friend_function (tree, tree);
158 static tree tsubst_friend_class (tree, tree);
159 static int can_complete_type_without_circularity (tree);
160 static tree get_bindings (tree, tree, tree, bool);
161 static int template_decl_level (tree);
162 static int check_cv_quals_for_unify (int, tree, tree);
163 static void template_parm_level_and_index (tree, int*, int*);
164 static int unify_pack_expansion (tree, tree, tree,
165 tree, unification_kind_t, bool, bool);
166 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
167 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
168 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
169 static void regenerate_decl_from_template (tree, tree);
170 static tree most_specialized_class (tree, tree, tsubst_flags_t);
171 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
172 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
173 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
174 static bool check_specialization_scope (void);
175 static tree process_partial_specialization (tree);
176 static void set_current_access_from_decl (tree);
177 static enum template_base_result get_template_base (tree, tree, tree, tree,
178 bool , tree *);
179 static tree try_class_unification (tree, tree, tree, tree, bool);
180 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
181 tree, tree);
182 static bool template_template_parm_bindings_ok_p (tree, tree);
183 static int template_args_equal (tree, tree);
184 static void tsubst_default_arguments (tree);
185 static tree for_each_template_parm_r (tree *, int *, void *);
186 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
187 static void copy_default_args_to_explicit_spec (tree);
188 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
189 static bool dependent_template_arg_p (tree);
190 static bool any_template_arguments_need_structural_equality_p (tree);
191 static bool dependent_type_p_r (tree);
192 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
193 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
194 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
195 static tree tsubst_decl (tree, tree, tsubst_flags_t);
196 static void perform_typedefs_access_check (tree tmpl, tree targs);
197 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
198 location_t);
199 static tree listify (tree);
200 static tree listify_autos (tree, tree);
201 static tree template_parm_to_arg (tree t);
202 static bool arg_from_parm_pack_p (tree, tree);
203 static tree current_template_args (void);
204 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
206 /* Make the current scope suitable for access checking when we are
207 processing T. T can be FUNCTION_DECL for instantiated function
208 template, or VAR_DECL for static member variable (need by
209 instantiate_decl). */
211 static void
212 push_access_scope (tree t)
214 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
215 || TREE_CODE (t) == VAR_DECL);
217 if (DECL_FRIEND_CONTEXT (t))
218 push_nested_class (DECL_FRIEND_CONTEXT (t));
219 else if (DECL_CLASS_SCOPE_P (t))
220 push_nested_class (DECL_CONTEXT (t));
221 else
222 push_to_top_level ();
224 if (TREE_CODE (t) == FUNCTION_DECL)
226 saved_access_scope = tree_cons
227 (NULL_TREE, current_function_decl, saved_access_scope);
228 current_function_decl = t;
232 /* Restore the scope set up by push_access_scope. T is the node we
233 are processing. */
235 static void
236 pop_access_scope (tree t)
238 if (TREE_CODE (t) == FUNCTION_DECL)
240 current_function_decl = TREE_VALUE (saved_access_scope);
241 saved_access_scope = TREE_CHAIN (saved_access_scope);
244 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
245 pop_nested_class ();
246 else
247 pop_from_top_level ();
250 /* Do any processing required when DECL (a member template
251 declaration) is finished. Returns the TEMPLATE_DECL corresponding
252 to DECL, unless it is a specialization, in which case the DECL
253 itself is returned. */
255 tree
256 finish_member_template_decl (tree decl)
258 if (decl == error_mark_node)
259 return error_mark_node;
261 gcc_assert (DECL_P (decl));
263 if (TREE_CODE (decl) == TYPE_DECL)
265 tree type;
267 type = TREE_TYPE (decl);
268 if (type == error_mark_node)
269 return error_mark_node;
270 if (MAYBE_CLASS_TYPE_P (type)
271 && CLASSTYPE_TEMPLATE_INFO (type)
272 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
274 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
275 check_member_template (tmpl);
276 return tmpl;
278 return NULL_TREE;
280 else if (TREE_CODE (decl) == FIELD_DECL)
281 error ("data member %qD cannot be a member template", decl);
282 else if (DECL_TEMPLATE_INFO (decl))
284 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
286 check_member_template (DECL_TI_TEMPLATE (decl));
287 return DECL_TI_TEMPLATE (decl);
289 else
290 return decl;
292 else
293 error ("invalid member template declaration %qD", decl);
295 return error_mark_node;
298 /* Create a template info node. */
300 tree
301 build_template_info (tree template_decl, tree template_args)
303 tree result = make_node (TEMPLATE_INFO);
304 TI_TEMPLATE (result) = template_decl;
305 TI_ARGS (result) = template_args;
306 return result;
309 /* Return the template info node corresponding to T, whatever T is. */
311 tree
312 get_template_info (const_tree t)
314 tree tinfo = NULL_TREE;
316 if (!t || t == error_mark_node)
317 return NULL;
319 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
320 tinfo = DECL_TEMPLATE_INFO (t);
322 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
323 t = TREE_TYPE (t);
325 if (TAGGED_TYPE_P (t))
326 tinfo = TYPE_TEMPLATE_INFO (t);
327 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
328 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
330 return tinfo;
333 /* Returns the template nesting level of the indicated class TYPE.
335 For example, in:
336 template <class T>
337 struct A
339 template <class U>
340 struct B {};
343 A<T>::B<U> has depth two, while A<T> has depth one.
344 Both A<T>::B<int> and A<int>::B<U> have depth one, if
345 they are instantiations, not specializations.
347 This function is guaranteed to return 0 if passed NULL_TREE so
348 that, for example, `template_class_depth (current_class_type)' is
349 always safe. */
352 template_class_depth (tree type)
354 int depth;
356 for (depth = 0;
357 type && TREE_CODE (type) != NAMESPACE_DECL;
358 type = (TREE_CODE (type) == FUNCTION_DECL)
359 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
361 tree tinfo = get_template_info (type);
363 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
364 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
365 ++depth;
368 return depth;
371 /* Subroutine of maybe_begin_member_template_processing.
372 Returns true if processing DECL needs us to push template parms. */
374 static bool
375 inline_needs_template_parms (tree decl)
377 if (! DECL_TEMPLATE_INFO (decl))
378 return false;
380 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
381 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
384 /* Subroutine of maybe_begin_member_template_processing.
385 Push the template parms in PARMS, starting from LEVELS steps into the
386 chain, and ending at the beginning, since template parms are listed
387 innermost first. */
389 static void
390 push_inline_template_parms_recursive (tree parmlist, int levels)
392 tree parms = TREE_VALUE (parmlist);
393 int i;
395 if (levels > 1)
396 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
398 ++processing_template_decl;
399 current_template_parms
400 = tree_cons (size_int (processing_template_decl),
401 parms, current_template_parms);
402 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
404 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
405 NULL);
406 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
408 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
410 if (parm == error_mark_node)
411 continue;
413 gcc_assert (DECL_P (parm));
415 switch (TREE_CODE (parm))
417 case TYPE_DECL:
418 case TEMPLATE_DECL:
419 pushdecl (parm);
420 break;
422 case PARM_DECL:
424 /* Make a CONST_DECL as is done in process_template_parm.
425 It is ugly that we recreate this here; the original
426 version built in process_template_parm is no longer
427 available. */
428 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
429 CONST_DECL, DECL_NAME (parm),
430 TREE_TYPE (parm));
431 DECL_ARTIFICIAL (decl) = 1;
432 TREE_CONSTANT (decl) = 1;
433 TREE_READONLY (decl) = 1;
434 DECL_INITIAL (decl) = DECL_INITIAL (parm);
435 SET_DECL_TEMPLATE_PARM_P (decl);
436 pushdecl (decl);
438 break;
440 default:
441 gcc_unreachable ();
446 /* Restore the template parameter context for a member template or
447 a friend template defined in a class definition. */
449 void
450 maybe_begin_member_template_processing (tree decl)
452 tree parms;
453 int levels = 0;
455 if (inline_needs_template_parms (decl))
457 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
458 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
460 if (DECL_TEMPLATE_SPECIALIZATION (decl))
462 --levels;
463 parms = TREE_CHAIN (parms);
466 push_inline_template_parms_recursive (parms, levels);
469 /* Remember how many levels of template parameters we pushed so that
470 we can pop them later. */
471 VEC_safe_push (int, heap, inline_parm_levels, levels);
474 /* Undo the effects of maybe_begin_member_template_processing. */
476 void
477 maybe_end_member_template_processing (void)
479 int i;
480 int last;
482 if (VEC_length (int, inline_parm_levels) == 0)
483 return;
485 last = VEC_pop (int, inline_parm_levels);
486 for (i = 0; i < last; ++i)
488 --processing_template_decl;
489 current_template_parms = TREE_CHAIN (current_template_parms);
490 poplevel (0, 0, 0);
494 /* Return a new template argument vector which contains all of ARGS,
495 but has as its innermost set of arguments the EXTRA_ARGS. */
497 static tree
498 add_to_template_args (tree args, tree extra_args)
500 tree new_args;
501 int extra_depth;
502 int i;
503 int j;
505 if (args == NULL_TREE || extra_args == error_mark_node)
506 return extra_args;
508 extra_depth = TMPL_ARGS_DEPTH (extra_args);
509 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
511 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
512 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
514 for (j = 1; j <= extra_depth; ++j, ++i)
515 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
517 return new_args;
520 /* Like add_to_template_args, but only the outermost ARGS are added to
521 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
522 (EXTRA_ARGS) levels are added. This function is used to combine
523 the template arguments from a partial instantiation with the
524 template arguments used to attain the full instantiation from the
525 partial instantiation. */
527 static tree
528 add_outermost_template_args (tree args, tree extra_args)
530 tree new_args;
532 /* If there are more levels of EXTRA_ARGS than there are ARGS,
533 something very fishy is going on. */
534 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
536 /* If *all* the new arguments will be the EXTRA_ARGS, just return
537 them. */
538 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
539 return extra_args;
541 /* For the moment, we make ARGS look like it contains fewer levels. */
542 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
544 new_args = add_to_template_args (args, extra_args);
546 /* Now, we restore ARGS to its full dimensions. */
547 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
549 return new_args;
552 /* Return the N levels of innermost template arguments from the ARGS. */
554 tree
555 get_innermost_template_args (tree args, int n)
557 tree new_args;
558 int extra_levels;
559 int i;
561 gcc_assert (n >= 0);
563 /* If N is 1, just return the innermost set of template arguments. */
564 if (n == 1)
565 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
567 /* If we're not removing anything, just return the arguments we were
568 given. */
569 extra_levels = TMPL_ARGS_DEPTH (args) - n;
570 gcc_assert (extra_levels >= 0);
571 if (extra_levels == 0)
572 return args;
574 /* Make a new set of arguments, not containing the outer arguments. */
575 new_args = make_tree_vec (n);
576 for (i = 1; i <= n; ++i)
577 SET_TMPL_ARGS_LEVEL (new_args, i,
578 TMPL_ARGS_LEVEL (args, i + extra_levels));
580 return new_args;
583 /* The inverse of get_innermost_template_args: Return all but the innermost
584 EXTRA_LEVELS levels of template arguments from the ARGS. */
586 static tree
587 strip_innermost_template_args (tree args, int extra_levels)
589 tree new_args;
590 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
591 int i;
593 gcc_assert (n >= 0);
595 /* If N is 1, just return the outermost set of template arguments. */
596 if (n == 1)
597 return TMPL_ARGS_LEVEL (args, 1);
599 /* If we're not removing anything, just return the arguments we were
600 given. */
601 gcc_assert (extra_levels >= 0);
602 if (extra_levels == 0)
603 return args;
605 /* Make a new set of arguments, not containing the inner arguments. */
606 new_args = make_tree_vec (n);
607 for (i = 1; i <= n; ++i)
608 SET_TMPL_ARGS_LEVEL (new_args, i,
609 TMPL_ARGS_LEVEL (args, i));
611 return new_args;
614 /* We've got a template header coming up; push to a new level for storing
615 the parms. */
617 void
618 begin_template_parm_list (void)
620 /* We use a non-tag-transparent scope here, which causes pushtag to
621 put tags in this scope, rather than in the enclosing class or
622 namespace scope. This is the right thing, since we want
623 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
624 global template class, push_template_decl handles putting the
625 TEMPLATE_DECL into top-level scope. For a nested template class,
626 e.g.:
628 template <class T> struct S1 {
629 template <class T> struct S2 {};
632 pushtag contains special code to call pushdecl_with_scope on the
633 TEMPLATE_DECL for S2. */
634 begin_scope (sk_template_parms, NULL);
635 ++processing_template_decl;
636 ++processing_template_parmlist;
637 note_template_header (0);
640 /* This routine is called when a specialization is declared. If it is
641 invalid to declare a specialization here, an error is reported and
642 false is returned, otherwise this routine will return true. */
644 static bool
645 check_specialization_scope (void)
647 tree scope = current_scope ();
649 /* [temp.expl.spec]
651 An explicit specialization shall be declared in the namespace of
652 which the template is a member, or, for member templates, in the
653 namespace of which the enclosing class or enclosing class
654 template is a member. An explicit specialization of a member
655 function, member class or static data member of a class template
656 shall be declared in the namespace of which the class template
657 is a member. */
658 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
660 error ("explicit specialization in non-namespace scope %qD", scope);
661 return false;
664 /* [temp.expl.spec]
666 In an explicit specialization declaration for a member of a class
667 template or a member template that appears in namespace scope,
668 the member template and some of its enclosing class templates may
669 remain unspecialized, except that the declaration shall not
670 explicitly specialize a class member template if its enclosing
671 class templates are not explicitly specialized as well. */
672 if (current_template_parms)
674 error ("enclosing class templates are not explicitly specialized");
675 return false;
678 return true;
681 /* We've just seen template <>. */
683 bool
684 begin_specialization (void)
686 begin_scope (sk_template_spec, NULL);
687 note_template_header (1);
688 return check_specialization_scope ();
691 /* Called at then end of processing a declaration preceded by
692 template<>. */
694 void
695 end_specialization (void)
697 finish_scope ();
698 reset_specialization ();
701 /* Any template <>'s that we have seen thus far are not referring to a
702 function specialization. */
704 void
705 reset_specialization (void)
707 processing_specialization = 0;
708 template_header_count = 0;
711 /* We've just seen a template header. If SPECIALIZATION is nonzero,
712 it was of the form template <>. */
714 static void
715 note_template_header (int specialization)
717 processing_specialization = specialization;
718 template_header_count++;
721 /* We're beginning an explicit instantiation. */
723 void
724 begin_explicit_instantiation (void)
726 gcc_assert (!processing_explicit_instantiation);
727 processing_explicit_instantiation = true;
731 void
732 end_explicit_instantiation (void)
734 gcc_assert (processing_explicit_instantiation);
735 processing_explicit_instantiation = false;
738 /* An explicit specialization or partial specialization TMPL is being
739 declared. Check that the namespace in which the specialization is
740 occurring is permissible. Returns false iff it is invalid to
741 specialize TMPL in the current namespace. */
743 static bool
744 check_specialization_namespace (tree tmpl)
746 tree tpl_ns = decl_namespace_context (tmpl);
748 /* [tmpl.expl.spec]
750 An explicit specialization shall be declared in the namespace of
751 which the template is a member, or, for member templates, in the
752 namespace of which the enclosing class or enclosing class
753 template is a member. An explicit specialization of a member
754 function, member class or static data member of a class template
755 shall be declared in the namespace of which the class template is
756 a member. */
757 if (current_scope() != DECL_CONTEXT (tmpl)
758 && !at_namespace_scope_p ())
760 error ("specialization of %qD must appear at namespace scope", tmpl);
761 return false;
763 if (is_associated_namespace (current_namespace, tpl_ns))
764 /* Same or super-using namespace. */
765 return true;
766 else
768 permerror (input_location, "specialization of %qD in different namespace", tmpl);
769 permerror (input_location, " from definition of %q+#D", tmpl);
770 return false;
774 /* SPEC is an explicit instantiation. Check that it is valid to
775 perform this explicit instantiation in the current namespace. */
777 static void
778 check_explicit_instantiation_namespace (tree spec)
780 tree ns;
782 /* DR 275: An explicit instantiation shall appear in an enclosing
783 namespace of its template. */
784 ns = decl_namespace_context (spec);
785 if (!is_ancestor (current_namespace, ns))
786 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
787 "(which does not enclose namespace %qD)",
788 spec, current_namespace, ns);
791 /* The TYPE is being declared. If it is a template type, that means it
792 is a partial specialization. Do appropriate error-checking. */
794 tree
795 maybe_process_partial_specialization (tree type)
797 tree context;
799 if (type == error_mark_node)
800 return error_mark_node;
802 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
804 error ("name of class shadows template template parameter %qD",
805 TYPE_NAME (type));
806 return error_mark_node;
809 context = TYPE_CONTEXT (type);
811 if ((CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
812 /* Consider non-class instantiations of alias templates as
813 well. */
814 || (TYPE_P (type)
815 && TYPE_TEMPLATE_INFO (type)
816 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
817 && DECL_USE_TEMPLATE (TYPE_NAME (type))))
819 /* This is for ordinary explicit specialization and partial
820 specialization of a template class such as:
822 template <> class C<int>;
826 template <class T> class C<T*>;
828 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
830 if (CLASS_TYPE_P (type)
831 && CLASSTYPE_IMPLICIT_INSTANTIATION (type)
832 && !COMPLETE_TYPE_P (type))
834 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
835 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
836 if (processing_template_decl)
838 if (push_template_decl (TYPE_MAIN_DECL (type))
839 == error_mark_node)
840 return error_mark_node;
843 else if (CLASS_TYPE_P (type)
844 && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
845 error ("specialization of %qT after instantiation", type);
847 if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
849 error ("partial specialization of alias template %qD",
850 TYPE_TI_TEMPLATE (type));
851 return error_mark_node;
854 else if (CLASS_TYPE_P (type)
855 && !CLASSTYPE_USE_TEMPLATE (type)
856 && CLASSTYPE_TEMPLATE_INFO (type)
857 && context && CLASS_TYPE_P (context)
858 && CLASSTYPE_TEMPLATE_INFO (context))
860 /* This is for an explicit specialization of member class
861 template according to [temp.expl.spec/18]:
863 template <> template <class U> class C<int>::D;
865 The context `C<int>' must be an implicit instantiation.
866 Otherwise this is just a member class template declared
867 earlier like:
869 template <> class C<int> { template <class U> class D; };
870 template <> template <class U> class C<int>::D;
872 In the first case, `C<int>::D' is a specialization of `C<T>::D'
873 while in the second case, `C<int>::D' is a primary template
874 and `C<T>::D' may not exist. */
876 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
877 && !COMPLETE_TYPE_P (type))
879 tree t;
880 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
882 if (current_namespace
883 != decl_namespace_context (tmpl))
885 permerror (input_location, "specializing %q#T in different namespace", type);
886 permerror (input_location, " from definition of %q+#D", tmpl);
889 /* Check for invalid specialization after instantiation:
891 template <> template <> class C<int>::D<int>;
892 template <> template <class U> class C<int>::D; */
894 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
895 t; t = TREE_CHAIN (t))
897 tree inst = TREE_VALUE (t);
898 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
900 /* We already have a full specialization of this partial
901 instantiation. Reassign it to the new member
902 specialization template. */
903 spec_entry elt;
904 spec_entry *entry;
905 void **slot;
907 elt.tmpl = most_general_template (tmpl);
908 elt.args = CLASSTYPE_TI_ARGS (inst);
909 elt.spec = inst;
911 htab_remove_elt (type_specializations, &elt);
913 elt.tmpl = tmpl;
914 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
916 slot = htab_find_slot (type_specializations, &elt, INSERT);
917 entry = ggc_alloc_spec_entry ();
918 *entry = elt;
919 *slot = entry;
921 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
922 /* But if we've had an implicit instantiation, that's a
923 problem ([temp.expl.spec]/6). */
924 error ("specialization %qT after instantiation %qT",
925 type, inst);
928 /* Mark TYPE as a specialization. And as a result, we only
929 have one level of template argument for the innermost
930 class template. */
931 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
932 CLASSTYPE_TI_ARGS (type)
933 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
936 else if (processing_specialization)
938 /* Someday C++0x may allow for enum template specialization. */
939 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
940 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
941 pedwarn (input_location, OPT_Wpedantic, "template specialization "
942 "of %qD not allowed by ISO C++", type);
943 else
945 error ("explicit specialization of non-template %qT", type);
946 return error_mark_node;
950 return type;
953 /* Returns nonzero if we can optimize the retrieval of specializations
954 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
955 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
957 static inline bool
958 optimize_specialization_lookup_p (tree tmpl)
960 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
961 && DECL_CLASS_SCOPE_P (tmpl)
962 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
963 parameter. */
964 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
965 /* The optimized lookup depends on the fact that the
966 template arguments for the member function template apply
967 purely to the containing class, which is not true if the
968 containing class is an explicit or partial
969 specialization. */
970 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
971 && !DECL_MEMBER_TEMPLATE_P (tmpl)
972 && !DECL_CONV_FN_P (tmpl)
973 /* It is possible to have a template that is not a member
974 template and is not a member of a template class:
976 template <typename T>
977 struct S { friend A::f(); };
979 Here, the friend function is a template, but the context does
980 not have template information. The optimized lookup relies
981 on having ARGS be the template arguments for both the class
982 and the function template. */
983 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
986 /* Retrieve the specialization (in the sense of [temp.spec] - a
987 specialization is either an instantiation or an explicit
988 specialization) of TMPL for the given template ARGS. If there is
989 no such specialization, return NULL_TREE. The ARGS are a vector of
990 arguments, or a vector of vectors of arguments, in the case of
991 templates with more than one level of parameters.
993 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
994 then we search for a partial specialization matching ARGS. This
995 parameter is ignored if TMPL is not a class template. */
997 static tree
998 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1000 if (args == error_mark_node)
1001 return NULL_TREE;
1003 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1005 /* There should be as many levels of arguments as there are
1006 levels of parameters. */
1007 gcc_assert (TMPL_ARGS_DEPTH (args)
1008 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1010 if (optimize_specialization_lookup_p (tmpl))
1012 tree class_template;
1013 tree class_specialization;
1014 VEC(tree,gc) *methods;
1015 tree fns;
1016 int idx;
1018 /* The template arguments actually apply to the containing
1019 class. Find the class specialization with those
1020 arguments. */
1021 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1022 class_specialization
1023 = retrieve_specialization (class_template, args, 0);
1024 if (!class_specialization)
1025 return NULL_TREE;
1026 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1027 for the specialization. */
1028 idx = class_method_index_for_fn (class_specialization, tmpl);
1029 if (idx == -1)
1030 return NULL_TREE;
1031 /* Iterate through the methods with the indicated name, looking
1032 for the one that has an instance of TMPL. */
1033 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1034 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1036 tree fn = OVL_CURRENT (fns);
1037 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1038 /* using-declarations can add base methods to the method vec,
1039 and we don't want those here. */
1040 && DECL_CONTEXT (fn) == class_specialization)
1041 return fn;
1043 return NULL_TREE;
1045 else
1047 spec_entry *found;
1048 spec_entry elt;
1049 htab_t specializations;
1051 elt.tmpl = tmpl;
1052 elt.args = args;
1053 elt.spec = NULL_TREE;
1055 if (DECL_CLASS_TEMPLATE_P (tmpl))
1056 specializations = type_specializations;
1057 else
1058 specializations = decl_specializations;
1060 if (hash == 0)
1061 hash = hash_specialization (&elt);
1062 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1063 if (found)
1064 return found->spec;
1067 return NULL_TREE;
1070 /* Like retrieve_specialization, but for local declarations. */
1072 static tree
1073 retrieve_local_specialization (tree tmpl)
1075 void **slot;
1077 if (local_specializations == NULL)
1078 return NULL_TREE;
1080 slot = pointer_map_contains (local_specializations, tmpl);
1081 return slot ? (tree) *slot : NULL_TREE;
1084 /* Returns nonzero iff DECL is a specialization of TMPL. */
1087 is_specialization_of (tree decl, tree tmpl)
1089 tree t;
1091 if (TREE_CODE (decl) == FUNCTION_DECL)
1093 for (t = decl;
1094 t != NULL_TREE;
1095 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1096 if (t == tmpl)
1097 return 1;
1099 else
1101 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1103 for (t = TREE_TYPE (decl);
1104 t != NULL_TREE;
1105 t = CLASSTYPE_USE_TEMPLATE (t)
1106 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1107 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1108 return 1;
1111 return 0;
1114 /* Returns nonzero iff DECL is a specialization of friend declaration
1115 FRIEND_DECL according to [temp.friend]. */
1117 bool
1118 is_specialization_of_friend (tree decl, tree friend_decl)
1120 bool need_template = true;
1121 int template_depth;
1123 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1124 || TREE_CODE (decl) == TYPE_DECL);
1126 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1127 of a template class, we want to check if DECL is a specialization
1128 if this. */
1129 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1130 && DECL_TEMPLATE_INFO (friend_decl)
1131 && !DECL_USE_TEMPLATE (friend_decl))
1133 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1134 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1135 need_template = false;
1137 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1138 && !PRIMARY_TEMPLATE_P (friend_decl))
1139 need_template = false;
1141 /* There is nothing to do if this is not a template friend. */
1142 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1143 return false;
1145 if (is_specialization_of (decl, friend_decl))
1146 return true;
1148 /* [temp.friend/6]
1149 A member of a class template may be declared to be a friend of a
1150 non-template class. In this case, the corresponding member of
1151 every specialization of the class template is a friend of the
1152 class granting friendship.
1154 For example, given a template friend declaration
1156 template <class T> friend void A<T>::f();
1158 the member function below is considered a friend
1160 template <> struct A<int> {
1161 void f();
1164 For this type of template friend, TEMPLATE_DEPTH below will be
1165 nonzero. To determine if DECL is a friend of FRIEND, we first
1166 check if the enclosing class is a specialization of another. */
1168 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1169 if (template_depth
1170 && DECL_CLASS_SCOPE_P (decl)
1171 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1172 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1174 /* Next, we check the members themselves. In order to handle
1175 a few tricky cases, such as when FRIEND_DECL's are
1177 template <class T> friend void A<T>::g(T t);
1178 template <class T> template <T t> friend void A<T>::h();
1180 and DECL's are
1182 void A<int>::g(int);
1183 template <int> void A<int>::h();
1185 we need to figure out ARGS, the template arguments from
1186 the context of DECL. This is required for template substitution
1187 of `T' in the function parameter of `g' and template parameter
1188 of `h' in the above examples. Here ARGS corresponds to `int'. */
1190 tree context = DECL_CONTEXT (decl);
1191 tree args = NULL_TREE;
1192 int current_depth = 0;
1194 while (current_depth < template_depth)
1196 if (CLASSTYPE_TEMPLATE_INFO (context))
1198 if (current_depth == 0)
1199 args = TYPE_TI_ARGS (context);
1200 else
1201 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1202 current_depth++;
1204 context = TYPE_CONTEXT (context);
1207 if (TREE_CODE (decl) == FUNCTION_DECL)
1209 bool is_template;
1210 tree friend_type;
1211 tree decl_type;
1212 tree friend_args_type;
1213 tree decl_args_type;
1215 /* Make sure that both DECL and FRIEND_DECL are templates or
1216 non-templates. */
1217 is_template = DECL_TEMPLATE_INFO (decl)
1218 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1219 if (need_template ^ is_template)
1220 return false;
1221 else if (is_template)
1223 /* If both are templates, check template parameter list. */
1224 tree friend_parms
1225 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1226 args, tf_none);
1227 if (!comp_template_parms
1228 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1229 friend_parms))
1230 return false;
1232 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1234 else
1235 decl_type = TREE_TYPE (decl);
1237 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1238 tf_none, NULL_TREE);
1239 if (friend_type == error_mark_node)
1240 return false;
1242 /* Check if return types match. */
1243 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1244 return false;
1246 /* Check if function parameter types match, ignoring the
1247 `this' parameter. */
1248 friend_args_type = TYPE_ARG_TYPES (friend_type);
1249 decl_args_type = TYPE_ARG_TYPES (decl_type);
1250 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1251 friend_args_type = TREE_CHAIN (friend_args_type);
1252 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1253 decl_args_type = TREE_CHAIN (decl_args_type);
1255 return compparms (decl_args_type, friend_args_type);
1257 else
1259 /* DECL is a TYPE_DECL */
1260 bool is_template;
1261 tree decl_type = TREE_TYPE (decl);
1263 /* Make sure that both DECL and FRIEND_DECL are templates or
1264 non-templates. */
1265 is_template
1266 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1267 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1269 if (need_template ^ is_template)
1270 return false;
1271 else if (is_template)
1273 tree friend_parms;
1274 /* If both are templates, check the name of the two
1275 TEMPLATE_DECL's first because is_friend didn't. */
1276 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1277 != DECL_NAME (friend_decl))
1278 return false;
1280 /* Now check template parameter list. */
1281 friend_parms
1282 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1283 args, tf_none);
1284 return comp_template_parms
1285 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1286 friend_parms);
1288 else
1289 return (DECL_NAME (decl)
1290 == DECL_NAME (friend_decl));
1293 return false;
1296 /* Register the specialization SPEC as a specialization of TMPL with
1297 the indicated ARGS. IS_FRIEND indicates whether the specialization
1298 is actually just a friend declaration. Returns SPEC, or an
1299 equivalent prior declaration, if available. */
1301 static tree
1302 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1303 hashval_t hash)
1305 tree fn;
1306 void **slot = NULL;
1307 spec_entry elt;
1309 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1311 if (TREE_CODE (spec) == FUNCTION_DECL
1312 && uses_template_parms (DECL_TI_ARGS (spec)))
1313 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1314 register it; we want the corresponding TEMPLATE_DECL instead.
1315 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1316 the more obvious `uses_template_parms (spec)' to avoid problems
1317 with default function arguments. In particular, given
1318 something like this:
1320 template <class T> void f(T t1, T t = T())
1322 the default argument expression is not substituted for in an
1323 instantiation unless and until it is actually needed. */
1324 return spec;
1326 if (optimize_specialization_lookup_p (tmpl))
1327 /* We don't put these specializations in the hash table, but we might
1328 want to give an error about a mismatch. */
1329 fn = retrieve_specialization (tmpl, args, 0);
1330 else
1332 elt.tmpl = tmpl;
1333 elt.args = args;
1334 elt.spec = spec;
1336 if (hash == 0)
1337 hash = hash_specialization (&elt);
1339 slot =
1340 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1341 if (*slot)
1342 fn = ((spec_entry *) *slot)->spec;
1343 else
1344 fn = NULL_TREE;
1347 /* We can sometimes try to re-register a specialization that we've
1348 already got. In particular, regenerate_decl_from_template calls
1349 duplicate_decls which will update the specialization list. But,
1350 we'll still get called again here anyhow. It's more convenient
1351 to simply allow this than to try to prevent it. */
1352 if (fn == spec)
1353 return spec;
1354 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1356 if (DECL_TEMPLATE_INSTANTIATION (fn))
1358 if (DECL_ODR_USED (fn)
1359 || DECL_EXPLICIT_INSTANTIATION (fn))
1361 error ("specialization of %qD after instantiation",
1362 fn);
1363 return error_mark_node;
1365 else
1367 tree clone;
1368 /* This situation should occur only if the first
1369 specialization is an implicit instantiation, the
1370 second is an explicit specialization, and the
1371 implicit instantiation has not yet been used. That
1372 situation can occur if we have implicitly
1373 instantiated a member function and then specialized
1374 it later.
1376 We can also wind up here if a friend declaration that
1377 looked like an instantiation turns out to be a
1378 specialization:
1380 template <class T> void foo(T);
1381 class S { friend void foo<>(int) };
1382 template <> void foo(int);
1384 We transform the existing DECL in place so that any
1385 pointers to it become pointers to the updated
1386 declaration.
1388 If there was a definition for the template, but not
1389 for the specialization, we want this to look as if
1390 there were no definition, and vice versa. */
1391 DECL_INITIAL (fn) = NULL_TREE;
1392 duplicate_decls (spec, fn, is_friend);
1393 /* The call to duplicate_decls will have applied
1394 [temp.expl.spec]:
1396 An explicit specialization of a function template
1397 is inline only if it is explicitly declared to be,
1398 and independently of whether its function template
1401 to the primary function; now copy the inline bits to
1402 the various clones. */
1403 FOR_EACH_CLONE (clone, fn)
1405 DECL_DECLARED_INLINE_P (clone)
1406 = DECL_DECLARED_INLINE_P (fn);
1407 DECL_SOURCE_LOCATION (clone)
1408 = DECL_SOURCE_LOCATION (fn);
1410 check_specialization_namespace (fn);
1412 return fn;
1415 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1417 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1418 /* Dup decl failed, but this is a new definition. Set the
1419 line number so any errors match this new
1420 definition. */
1421 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1423 return fn;
1426 else if (fn)
1427 return duplicate_decls (spec, fn, is_friend);
1429 /* A specialization must be declared in the same namespace as the
1430 template it is specializing. */
1431 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1432 && !check_specialization_namespace (tmpl))
1433 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1435 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1437 spec_entry *entry = ggc_alloc_spec_entry ();
1438 gcc_assert (tmpl && args && spec);
1439 *entry = elt;
1440 *slot = entry;
1441 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1442 && PRIMARY_TEMPLATE_P (tmpl)
1443 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1444 /* TMPL is a forward declaration of a template function; keep a list
1445 of all specializations in case we need to reassign them to a friend
1446 template later in tsubst_friend_function. */
1447 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1448 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1451 return spec;
1454 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1455 TMPL and ARGS members, ignores SPEC. */
1457 static int
1458 eq_specializations (const void *p1, const void *p2)
1460 const spec_entry *e1 = (const spec_entry *)p1;
1461 const spec_entry *e2 = (const spec_entry *)p2;
1463 return (e1->tmpl == e2->tmpl
1464 && comp_template_args (e1->args, e2->args));
1467 /* Returns a hash for a template TMPL and template arguments ARGS. */
1469 static hashval_t
1470 hash_tmpl_and_args (tree tmpl, tree args)
1472 hashval_t val = DECL_UID (tmpl);
1473 return iterative_hash_template_arg (args, val);
1476 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1477 ignoring SPEC. */
1479 static hashval_t
1480 hash_specialization (const void *p)
1482 const spec_entry *e = (const spec_entry *)p;
1483 return hash_tmpl_and_args (e->tmpl, e->args);
1486 /* Recursively calculate a hash value for a template argument ARG, for use
1487 in the hash tables of template specializations. */
1489 hashval_t
1490 iterative_hash_template_arg (tree arg, hashval_t val)
1492 unsigned HOST_WIDE_INT i;
1493 enum tree_code code;
1494 char tclass;
1496 if (arg == NULL_TREE)
1497 return iterative_hash_object (arg, val);
1499 if (!TYPE_P (arg))
1500 STRIP_NOPS (arg);
1502 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1503 /* We can get one of these when re-hashing a previous entry in the middle
1504 of substituting into a pack expansion. Just look through it. */
1505 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1507 code = TREE_CODE (arg);
1508 tclass = TREE_CODE_CLASS (code);
1510 val = iterative_hash_object (code, val);
1512 switch (code)
1514 case ERROR_MARK:
1515 return val;
1517 case IDENTIFIER_NODE:
1518 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1520 case TREE_VEC:
1522 int i, len = TREE_VEC_LENGTH (arg);
1523 for (i = 0; i < len; ++i)
1524 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1525 return val;
1528 case TYPE_PACK_EXPANSION:
1529 case EXPR_PACK_EXPANSION:
1530 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1531 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1533 case TYPE_ARGUMENT_PACK:
1534 case NONTYPE_ARGUMENT_PACK:
1535 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1537 case TREE_LIST:
1538 for (; arg; arg = TREE_CHAIN (arg))
1539 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1540 return val;
1542 case OVERLOAD:
1543 for (; arg; arg = OVL_NEXT (arg))
1544 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1545 return val;
1547 case CONSTRUCTOR:
1549 tree field, value;
1550 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1552 val = iterative_hash_template_arg (field, val);
1553 val = iterative_hash_template_arg (value, val);
1555 return val;
1558 case PARM_DECL:
1559 if (!DECL_ARTIFICIAL (arg))
1561 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1562 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1564 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1566 case TARGET_EXPR:
1567 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1569 case PTRMEM_CST:
1570 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1571 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1573 case TEMPLATE_PARM_INDEX:
1574 val = iterative_hash_template_arg
1575 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1576 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1577 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1579 case TRAIT_EXPR:
1580 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1581 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1582 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1584 case BASELINK:
1585 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1586 val);
1587 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1588 val);
1590 case MODOP_EXPR:
1591 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1592 code = TREE_CODE (TREE_OPERAND (arg, 1));
1593 val = iterative_hash_object (code, val);
1594 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1596 case LAMBDA_EXPR:
1597 /* A lambda can't appear in a template arg, but don't crash on
1598 erroneous input. */
1599 gcc_assert (seen_error ());
1600 return val;
1602 case CAST_EXPR:
1603 case IMPLICIT_CONV_EXPR:
1604 case STATIC_CAST_EXPR:
1605 case REINTERPRET_CAST_EXPR:
1606 case CONST_CAST_EXPR:
1607 case DYNAMIC_CAST_EXPR:
1608 case NEW_EXPR:
1609 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1610 /* Now hash operands as usual. */
1611 break;
1613 default:
1614 break;
1617 switch (tclass)
1619 case tcc_type:
1620 if (TYPE_CANONICAL (arg))
1621 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1622 val);
1623 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1624 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1625 /* Otherwise just compare the types during lookup. */
1626 return val;
1628 case tcc_declaration:
1629 case tcc_constant:
1630 return iterative_hash_expr (arg, val);
1632 default:
1633 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1635 unsigned n = cp_tree_operand_length (arg);
1636 for (i = 0; i < n; ++i)
1637 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1638 return val;
1641 gcc_unreachable ();
1642 return 0;
1645 /* Unregister the specialization SPEC as a specialization of TMPL.
1646 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1647 if the SPEC was listed as a specialization of TMPL.
1649 Note that SPEC has been ggc_freed, so we can't look inside it. */
1651 bool
1652 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1654 spec_entry *entry;
1655 spec_entry elt;
1657 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1658 elt.args = TI_ARGS (tinfo);
1659 elt.spec = NULL_TREE;
1661 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1662 if (entry != NULL)
1664 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1665 gcc_assert (new_spec != NULL_TREE);
1666 entry->spec = new_spec;
1667 return 1;
1670 return 0;
1673 /* Like register_specialization, but for local declarations. We are
1674 registering SPEC, an instantiation of TMPL. */
1676 static void
1677 register_local_specialization (tree spec, tree tmpl)
1679 void **slot;
1681 slot = pointer_map_insert (local_specializations, tmpl);
1682 *slot = spec;
1685 /* TYPE is a class type. Returns true if TYPE is an explicitly
1686 specialized class. */
1688 bool
1689 explicit_class_specialization_p (tree type)
1691 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1692 return false;
1693 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1696 /* Print the list of functions at FNS, going through all the overloads
1697 for each element of the list. Alternatively, FNS can not be a
1698 TREE_LIST, in which case it will be printed together with all the
1699 overloads.
1701 MORE and *STR should respectively be FALSE and NULL when the function
1702 is called from the outside. They are used internally on recursive
1703 calls. print_candidates manages the two parameters and leaves NULL
1704 in *STR when it ends. */
1706 static void
1707 print_candidates_1 (tree fns, bool more, const char **str)
1709 tree fn, fn2;
1710 char *spaces = NULL;
1712 for (fn = fns; fn; fn = OVL_NEXT (fn))
1713 if (TREE_CODE (fn) == TREE_LIST)
1715 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1716 print_candidates_1 (TREE_VALUE (fn2),
1717 TREE_CHAIN (fn2) || more, str);
1719 else
1721 if (!*str)
1723 /* Pick the prefix string. */
1724 if (!more && !OVL_NEXT (fns))
1726 error ("candidate is: %+#D", OVL_CURRENT (fn));
1727 continue;
1730 *str = _("candidates are:");
1731 spaces = get_spaces (*str);
1733 error ("%s %+#D", *str, OVL_CURRENT (fn));
1734 *str = spaces ? spaces : *str;
1737 if (!more)
1739 free (spaces);
1740 *str = NULL;
1744 /* Print the list of candidate FNS in an error message. FNS can also
1745 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1747 void
1748 print_candidates (tree fns)
1750 const char *str = NULL;
1751 print_candidates_1 (fns, false, &str);
1752 gcc_assert (str == NULL);
1755 /* Returns the template (one of the functions given by TEMPLATE_ID)
1756 which can be specialized to match the indicated DECL with the
1757 explicit template args given in TEMPLATE_ID. The DECL may be
1758 NULL_TREE if none is available. In that case, the functions in
1759 TEMPLATE_ID are non-members.
1761 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1762 specialization of a member template.
1764 The TEMPLATE_COUNT is the number of references to qualifying
1765 template classes that appeared in the name of the function. See
1766 check_explicit_specialization for a more accurate description.
1768 TSK indicates what kind of template declaration (if any) is being
1769 declared. TSK_TEMPLATE indicates that the declaration given by
1770 DECL, though a FUNCTION_DECL, has template parameters, and is
1771 therefore a template function.
1773 The template args (those explicitly specified and those deduced)
1774 are output in a newly created vector *TARGS_OUT.
1776 If it is impossible to determine the result, an error message is
1777 issued. The error_mark_node is returned to indicate failure. */
1779 static tree
1780 determine_specialization (tree template_id,
1781 tree decl,
1782 tree* targs_out,
1783 int need_member_template,
1784 int template_count,
1785 tmpl_spec_kind tsk)
1787 tree fns;
1788 tree targs;
1789 tree explicit_targs;
1790 tree candidates = NULL_TREE;
1791 /* A TREE_LIST of templates of which DECL may be a specialization.
1792 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1793 corresponding TREE_PURPOSE is the set of template arguments that,
1794 when used to instantiate the template, would produce a function
1795 with the signature of DECL. */
1796 tree templates = NULL_TREE;
1797 int header_count;
1798 cp_binding_level *b;
1800 *targs_out = NULL_TREE;
1802 if (template_id == error_mark_node || decl == error_mark_node)
1803 return error_mark_node;
1805 fns = TREE_OPERAND (template_id, 0);
1806 explicit_targs = TREE_OPERAND (template_id, 1);
1808 if (fns == error_mark_node)
1809 return error_mark_node;
1811 /* Check for baselinks. */
1812 if (BASELINK_P (fns))
1813 fns = BASELINK_FUNCTIONS (fns);
1815 if (!is_overloaded_fn (fns))
1817 error ("%qD is not a function template", fns);
1818 return error_mark_node;
1821 /* Count the number of template headers specified for this
1822 specialization. */
1823 header_count = 0;
1824 for (b = current_binding_level;
1825 b->kind == sk_template_parms;
1826 b = b->level_chain)
1827 ++header_count;
1829 for (; fns; fns = OVL_NEXT (fns))
1831 tree fn = OVL_CURRENT (fns);
1833 if (TREE_CODE (fn) == TEMPLATE_DECL)
1835 tree decl_arg_types;
1836 tree fn_arg_types;
1837 tree insttype;
1839 /* In case of explicit specialization, we need to check if
1840 the number of template headers appearing in the specialization
1841 is correct. This is usually done in check_explicit_specialization,
1842 but the check done there cannot be exhaustive when specializing
1843 member functions. Consider the following code:
1845 template <> void A<int>::f(int);
1846 template <> template <> void A<int>::f(int);
1848 Assuming that A<int> is not itself an explicit specialization
1849 already, the first line specializes "f" which is a non-template
1850 member function, whilst the second line specializes "f" which
1851 is a template member function. So both lines are syntactically
1852 correct, and check_explicit_specialization does not reject
1853 them.
1855 Here, we can do better, as we are matching the specialization
1856 against the declarations. We count the number of template
1857 headers, and we check if they match TEMPLATE_COUNT + 1
1858 (TEMPLATE_COUNT is the number of qualifying template classes,
1859 plus there must be another header for the member template
1860 itself).
1862 Notice that if header_count is zero, this is not a
1863 specialization but rather a template instantiation, so there
1864 is no check we can perform here. */
1865 if (header_count && header_count != template_count + 1)
1866 continue;
1868 /* Check that the number of template arguments at the
1869 innermost level for DECL is the same as for FN. */
1870 if (current_binding_level->kind == sk_template_parms
1871 && !current_binding_level->explicit_spec_p
1872 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1873 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1874 (current_template_parms))))
1875 continue;
1877 /* DECL might be a specialization of FN. */
1878 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1879 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1881 /* For a non-static member function, we need to make sure
1882 that the const qualification is the same. Since
1883 get_bindings does not try to merge the "this" parameter,
1884 we must do the comparison explicitly. */
1885 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1886 && !same_type_p (TREE_VALUE (fn_arg_types),
1887 TREE_VALUE (decl_arg_types)))
1888 continue;
1890 /* Skip the "this" parameter and, for constructors of
1891 classes with virtual bases, the VTT parameter. A
1892 full specialization of a constructor will have a VTT
1893 parameter, but a template never will. */
1894 decl_arg_types
1895 = skip_artificial_parms_for (decl, decl_arg_types);
1896 fn_arg_types
1897 = skip_artificial_parms_for (fn, fn_arg_types);
1899 /* Function templates cannot be specializations; there are
1900 no partial specializations of functions. Therefore, if
1901 the type of DECL does not match FN, there is no
1902 match. */
1903 if (tsk == tsk_template)
1905 if (compparms (fn_arg_types, decl_arg_types))
1906 candidates = tree_cons (NULL_TREE, fn, candidates);
1907 continue;
1910 /* See whether this function might be a specialization of this
1911 template. */
1912 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1914 if (!targs)
1915 /* We cannot deduce template arguments that when used to
1916 specialize TMPL will produce DECL. */
1917 continue;
1919 /* Make sure that the deduced arguments actually work. */
1920 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1921 if (insttype == error_mark_node)
1922 continue;
1923 fn_arg_types
1924 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1925 if (!compparms (fn_arg_types, decl_arg_types))
1926 continue;
1928 /* Save this template, and the arguments deduced. */
1929 templates = tree_cons (targs, fn, templates);
1931 else if (need_member_template)
1932 /* FN is an ordinary member function, and we need a
1933 specialization of a member template. */
1935 else if (TREE_CODE (fn) != FUNCTION_DECL)
1936 /* We can get IDENTIFIER_NODEs here in certain erroneous
1937 cases. */
1939 else if (!DECL_FUNCTION_MEMBER_P (fn))
1940 /* This is just an ordinary non-member function. Nothing can
1941 be a specialization of that. */
1943 else if (DECL_ARTIFICIAL (fn))
1944 /* Cannot specialize functions that are created implicitly. */
1946 else
1948 tree decl_arg_types;
1950 /* This is an ordinary member function. However, since
1951 we're here, we can assume it's enclosing class is a
1952 template class. For example,
1954 template <typename T> struct S { void f(); };
1955 template <> void S<int>::f() {}
1957 Here, S<int>::f is a non-template, but S<int> is a
1958 template class. If FN has the same type as DECL, we
1959 might be in business. */
1961 if (!DECL_TEMPLATE_INFO (fn))
1962 /* Its enclosing class is an explicit specialization
1963 of a template class. This is not a candidate. */
1964 continue;
1966 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1967 TREE_TYPE (TREE_TYPE (fn))))
1968 /* The return types differ. */
1969 continue;
1971 /* Adjust the type of DECL in case FN is a static member. */
1972 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1973 if (DECL_STATIC_FUNCTION_P (fn)
1974 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1975 decl_arg_types = TREE_CHAIN (decl_arg_types);
1977 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1978 decl_arg_types))
1979 /* They match! */
1980 candidates = tree_cons (NULL_TREE, fn, candidates);
1984 if (templates && TREE_CHAIN (templates))
1986 /* We have:
1988 [temp.expl.spec]
1990 It is possible for a specialization with a given function
1991 signature to be instantiated from more than one function
1992 template. In such cases, explicit specification of the
1993 template arguments must be used to uniquely identify the
1994 function template specialization being specialized.
1996 Note that here, there's no suggestion that we're supposed to
1997 determine which of the candidate templates is most
1998 specialized. However, we, also have:
2000 [temp.func.order]
2002 Partial ordering of overloaded function template
2003 declarations is used in the following contexts to select
2004 the function template to which a function template
2005 specialization refers:
2007 -- when an explicit specialization refers to a function
2008 template.
2010 So, we do use the partial ordering rules, at least for now.
2011 This extension can only serve to make invalid programs valid,
2012 so it's safe. And, there is strong anecdotal evidence that
2013 the committee intended the partial ordering rules to apply;
2014 the EDG front end has that behavior, and John Spicer claims
2015 that the committee simply forgot to delete the wording in
2016 [temp.expl.spec]. */
2017 tree tmpl = most_specialized_instantiation (templates);
2018 if (tmpl != error_mark_node)
2020 templates = tmpl;
2021 TREE_CHAIN (templates) = NULL_TREE;
2025 if (templates == NULL_TREE && candidates == NULL_TREE)
2027 error ("template-id %qD for %q+D does not match any template "
2028 "declaration", template_id, decl);
2029 if (header_count && header_count != template_count + 1)
2030 inform (input_location, "saw %d %<template<>%>, need %d for "
2031 "specializing a member function template",
2032 header_count, template_count + 1);
2033 return error_mark_node;
2035 else if ((templates && TREE_CHAIN (templates))
2036 || (candidates && TREE_CHAIN (candidates))
2037 || (templates && candidates))
2039 error ("ambiguous template specialization %qD for %q+D",
2040 template_id, decl);
2041 candidates = chainon (candidates, templates);
2042 print_candidates (candidates);
2043 return error_mark_node;
2046 /* We have one, and exactly one, match. */
2047 if (candidates)
2049 tree fn = TREE_VALUE (candidates);
2050 *targs_out = copy_node (DECL_TI_ARGS (fn));
2051 /* DECL is a re-declaration or partial instantiation of a template
2052 function. */
2053 if (TREE_CODE (fn) == TEMPLATE_DECL)
2054 return fn;
2055 /* It was a specialization of an ordinary member function in a
2056 template class. */
2057 return DECL_TI_TEMPLATE (fn);
2060 /* It was a specialization of a template. */
2061 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2062 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2064 *targs_out = copy_node (targs);
2065 SET_TMPL_ARGS_LEVEL (*targs_out,
2066 TMPL_ARGS_DEPTH (*targs_out),
2067 TREE_PURPOSE (templates));
2069 else
2070 *targs_out = TREE_PURPOSE (templates);
2071 return TREE_VALUE (templates);
2074 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2075 but with the default argument values filled in from those in the
2076 TMPL_TYPES. */
2078 static tree
2079 copy_default_args_to_explicit_spec_1 (tree spec_types,
2080 tree tmpl_types)
2082 tree new_spec_types;
2084 if (!spec_types)
2085 return NULL_TREE;
2087 if (spec_types == void_list_node)
2088 return void_list_node;
2090 /* Substitute into the rest of the list. */
2091 new_spec_types =
2092 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2093 TREE_CHAIN (tmpl_types));
2095 /* Add the default argument for this parameter. */
2096 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2097 TREE_VALUE (spec_types),
2098 new_spec_types);
2101 /* DECL is an explicit specialization. Replicate default arguments
2102 from the template it specializes. (That way, code like:
2104 template <class T> void f(T = 3);
2105 template <> void f(double);
2106 void g () { f (); }
2108 works, as required.) An alternative approach would be to look up
2109 the correct default arguments at the call-site, but this approach
2110 is consistent with how implicit instantiations are handled. */
2112 static void
2113 copy_default_args_to_explicit_spec (tree decl)
2115 tree tmpl;
2116 tree spec_types;
2117 tree tmpl_types;
2118 tree new_spec_types;
2119 tree old_type;
2120 tree new_type;
2121 tree t;
2122 tree object_type = NULL_TREE;
2123 tree in_charge = NULL_TREE;
2124 tree vtt = NULL_TREE;
2126 /* See if there's anything we need to do. */
2127 tmpl = DECL_TI_TEMPLATE (decl);
2128 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2129 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2130 if (TREE_PURPOSE (t))
2131 break;
2132 if (!t)
2133 return;
2135 old_type = TREE_TYPE (decl);
2136 spec_types = TYPE_ARG_TYPES (old_type);
2138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2140 /* Remove the this pointer, but remember the object's type for
2141 CV quals. */
2142 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2143 spec_types = TREE_CHAIN (spec_types);
2144 tmpl_types = TREE_CHAIN (tmpl_types);
2146 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2148 /* DECL may contain more parameters than TMPL due to the extra
2149 in-charge parameter in constructors and destructors. */
2150 in_charge = spec_types;
2151 spec_types = TREE_CHAIN (spec_types);
2153 if (DECL_HAS_VTT_PARM_P (decl))
2155 vtt = spec_types;
2156 spec_types = TREE_CHAIN (spec_types);
2160 /* Compute the merged default arguments. */
2161 new_spec_types =
2162 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2164 /* Compute the new FUNCTION_TYPE. */
2165 if (object_type)
2167 if (vtt)
2168 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2169 TREE_VALUE (vtt),
2170 new_spec_types);
2172 if (in_charge)
2173 /* Put the in-charge parameter back. */
2174 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2175 TREE_VALUE (in_charge),
2176 new_spec_types);
2178 new_type = build_method_type_directly (object_type,
2179 TREE_TYPE (old_type),
2180 new_spec_types);
2182 else
2183 new_type = build_function_type (TREE_TYPE (old_type),
2184 new_spec_types);
2185 new_type = cp_build_type_attribute_variant (new_type,
2186 TYPE_ATTRIBUTES (old_type));
2187 new_type = build_exception_variant (new_type,
2188 TYPE_RAISES_EXCEPTIONS (old_type));
2189 TREE_TYPE (decl) = new_type;
2192 /* Check to see if the function just declared, as indicated in
2193 DECLARATOR, and in DECL, is a specialization of a function
2194 template. We may also discover that the declaration is an explicit
2195 instantiation at this point.
2197 Returns DECL, or an equivalent declaration that should be used
2198 instead if all goes well. Issues an error message if something is
2199 amiss. Returns error_mark_node if the error is not easily
2200 recoverable.
2202 FLAGS is a bitmask consisting of the following flags:
2204 2: The function has a definition.
2205 4: The function is a friend.
2207 The TEMPLATE_COUNT is the number of references to qualifying
2208 template classes that appeared in the name of the function. For
2209 example, in
2211 template <class T> struct S { void f(); };
2212 void S<int>::f();
2214 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2215 classes are not counted in the TEMPLATE_COUNT, so that in
2217 template <class T> struct S {};
2218 template <> struct S<int> { void f(); }
2219 template <> void S<int>::f();
2221 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2222 invalid; there should be no template <>.)
2224 If the function is a specialization, it is marked as such via
2225 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2226 is set up correctly, and it is added to the list of specializations
2227 for that template. */
2229 tree
2230 check_explicit_specialization (tree declarator,
2231 tree decl,
2232 int template_count,
2233 int flags)
2235 int have_def = flags & 2;
2236 int is_friend = flags & 4;
2237 int specialization = 0;
2238 int explicit_instantiation = 0;
2239 int member_specialization = 0;
2240 tree ctype = DECL_CLASS_CONTEXT (decl);
2241 tree dname = DECL_NAME (decl);
2242 tmpl_spec_kind tsk;
2244 if (is_friend)
2246 if (!processing_specialization)
2247 tsk = tsk_none;
2248 else
2249 tsk = tsk_excessive_parms;
2251 else
2252 tsk = current_tmpl_spec_kind (template_count);
2254 switch (tsk)
2256 case tsk_none:
2257 if (processing_specialization)
2259 specialization = 1;
2260 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2262 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2264 if (is_friend)
2265 /* This could be something like:
2267 template <class T> void f(T);
2268 class S { friend void f<>(int); } */
2269 specialization = 1;
2270 else
2272 /* This case handles bogus declarations like template <>
2273 template <class T> void f<int>(); */
2275 error ("template-id %qD in declaration of primary template",
2276 declarator);
2277 return decl;
2280 break;
2282 case tsk_invalid_member_spec:
2283 /* The error has already been reported in
2284 check_specialization_scope. */
2285 return error_mark_node;
2287 case tsk_invalid_expl_inst:
2288 error ("template parameter list used in explicit instantiation");
2290 /* Fall through. */
2292 case tsk_expl_inst:
2293 if (have_def)
2294 error ("definition provided for explicit instantiation");
2296 explicit_instantiation = 1;
2297 break;
2299 case tsk_excessive_parms:
2300 case tsk_insufficient_parms:
2301 if (tsk == tsk_excessive_parms)
2302 error ("too many template parameter lists in declaration of %qD",
2303 decl);
2304 else if (template_header_count)
2305 error("too few template parameter lists in declaration of %qD", decl);
2306 else
2307 error("explicit specialization of %qD must be introduced by "
2308 "%<template <>%>", decl);
2310 /* Fall through. */
2311 case tsk_expl_spec:
2312 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2313 if (ctype)
2314 member_specialization = 1;
2315 else
2316 specialization = 1;
2317 break;
2319 case tsk_template:
2320 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2322 /* This case handles bogus declarations like template <>
2323 template <class T> void f<int>(); */
2325 if (uses_template_parms (declarator))
2326 error ("function template partial specialization %qD "
2327 "is not allowed", declarator);
2328 else
2329 error ("template-id %qD in declaration of primary template",
2330 declarator);
2331 return decl;
2334 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2335 /* This is a specialization of a member template, without
2336 specialization the containing class. Something like:
2338 template <class T> struct S {
2339 template <class U> void f (U);
2341 template <> template <class U> void S<int>::f(U) {}
2343 That's a specialization -- but of the entire template. */
2344 specialization = 1;
2345 break;
2347 default:
2348 gcc_unreachable ();
2351 if (specialization || member_specialization)
2353 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2354 for (; t; t = TREE_CHAIN (t))
2355 if (TREE_PURPOSE (t))
2357 permerror (input_location,
2358 "default argument specified in explicit specialization");
2359 break;
2363 if (specialization || member_specialization || explicit_instantiation)
2365 tree tmpl = NULL_TREE;
2366 tree targs = NULL_TREE;
2368 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2369 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2371 tree fns;
2373 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2374 if (ctype)
2375 fns = dname;
2376 else
2378 /* If there is no class context, the explicit instantiation
2379 must be at namespace scope. */
2380 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2382 /* Find the namespace binding, using the declaration
2383 context. */
2384 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2385 false, true);
2386 if (fns == error_mark_node || !is_overloaded_fn (fns))
2388 error ("%qD is not a template function", dname);
2389 fns = error_mark_node;
2391 else
2393 tree fn = OVL_CURRENT (fns);
2394 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2395 CP_DECL_CONTEXT (fn)))
2396 error ("%qD is not declared in %qD",
2397 decl, current_namespace);
2401 declarator = lookup_template_function (fns, NULL_TREE);
2404 if (declarator == error_mark_node)
2405 return error_mark_node;
2407 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2409 if (!explicit_instantiation)
2410 /* A specialization in class scope. This is invalid,
2411 but the error will already have been flagged by
2412 check_specialization_scope. */
2413 return error_mark_node;
2414 else
2416 /* It's not valid to write an explicit instantiation in
2417 class scope, e.g.:
2419 class C { template void f(); }
2421 This case is caught by the parser. However, on
2422 something like:
2424 template class C { void f(); };
2426 (which is invalid) we can get here. The error will be
2427 issued later. */
2431 return decl;
2433 else if (ctype != NULL_TREE
2434 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2435 IDENTIFIER_NODE))
2437 /* Find the list of functions in ctype that have the same
2438 name as the declared function. */
2439 tree name = TREE_OPERAND (declarator, 0);
2440 tree fns = NULL_TREE;
2441 int idx;
2443 if (constructor_name_p (name, ctype))
2445 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2447 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2448 : !CLASSTYPE_DESTRUCTORS (ctype))
2450 /* From [temp.expl.spec]:
2452 If such an explicit specialization for the member
2453 of a class template names an implicitly-declared
2454 special member function (clause _special_), the
2455 program is ill-formed.
2457 Similar language is found in [temp.explicit]. */
2458 error ("specialization of implicitly-declared special member function");
2459 return error_mark_node;
2462 name = is_constructor ? ctor_identifier : dtor_identifier;
2465 if (!DECL_CONV_FN_P (decl))
2467 idx = lookup_fnfields_1 (ctype, name);
2468 if (idx >= 0)
2469 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2471 else
2473 VEC(tree,gc) *methods;
2474 tree ovl;
2476 /* For a type-conversion operator, we cannot do a
2477 name-based lookup. We might be looking for `operator
2478 int' which will be a specialization of `operator T'.
2479 So, we find *all* the conversion operators, and then
2480 select from them. */
2481 fns = NULL_TREE;
2483 methods = CLASSTYPE_METHOD_VEC (ctype);
2484 if (methods)
2485 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2486 VEC_iterate (tree, methods, idx, ovl);
2487 ++idx)
2489 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2490 /* There are no more conversion functions. */
2491 break;
2493 /* Glue all these conversion functions together
2494 with those we already have. */
2495 for (; ovl; ovl = OVL_NEXT (ovl))
2496 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2500 if (fns == NULL_TREE)
2502 error ("no member function %qD declared in %qT", name, ctype);
2503 return error_mark_node;
2505 else
2506 TREE_OPERAND (declarator, 0) = fns;
2509 /* Figure out what exactly is being specialized at this point.
2510 Note that for an explicit instantiation, even one for a
2511 member function, we cannot tell apriori whether the
2512 instantiation is for a member template, or just a member
2513 function of a template class. Even if a member template is
2514 being instantiated, the member template arguments may be
2515 elided if they can be deduced from the rest of the
2516 declaration. */
2517 tmpl = determine_specialization (declarator, decl,
2518 &targs,
2519 member_specialization,
2520 template_count,
2521 tsk);
2523 if (!tmpl || tmpl == error_mark_node)
2524 /* We couldn't figure out what this declaration was
2525 specializing. */
2526 return error_mark_node;
2527 else
2529 tree gen_tmpl = most_general_template (tmpl);
2531 if (explicit_instantiation)
2533 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2534 is done by do_decl_instantiation later. */
2536 int arg_depth = TMPL_ARGS_DEPTH (targs);
2537 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2539 if (arg_depth > parm_depth)
2541 /* If TMPL is not the most general template (for
2542 example, if TMPL is a friend template that is
2543 injected into namespace scope), then there will
2544 be too many levels of TARGS. Remove some of them
2545 here. */
2546 int i;
2547 tree new_targs;
2549 new_targs = make_tree_vec (parm_depth);
2550 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2551 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2552 = TREE_VEC_ELT (targs, i);
2553 targs = new_targs;
2556 return instantiate_template (tmpl, targs, tf_error);
2559 /* If we thought that the DECL was a member function, but it
2560 turns out to be specializing a static member function,
2561 make DECL a static member function as well. */
2562 if (DECL_STATIC_FUNCTION_P (tmpl)
2563 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2564 revert_static_member_fn (decl);
2566 /* If this is a specialization of a member template of a
2567 template class, we want to return the TEMPLATE_DECL, not
2568 the specialization of it. */
2569 if (tsk == tsk_template)
2571 tree result = DECL_TEMPLATE_RESULT (tmpl);
2572 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2573 DECL_INITIAL (result) = NULL_TREE;
2574 if (have_def)
2576 tree parm;
2577 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2578 DECL_SOURCE_LOCATION (result)
2579 = DECL_SOURCE_LOCATION (decl);
2580 /* We want to use the argument list specified in the
2581 definition, not in the original declaration. */
2582 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2583 for (parm = DECL_ARGUMENTS (result); parm;
2584 parm = DECL_CHAIN (parm))
2585 DECL_CONTEXT (parm) = result;
2587 return register_specialization (tmpl, gen_tmpl, targs,
2588 is_friend, 0);
2591 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2592 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2594 /* Inherit default function arguments from the template
2595 DECL is specializing. */
2596 copy_default_args_to_explicit_spec (decl);
2598 /* This specialization has the same protection as the
2599 template it specializes. */
2600 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2601 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2603 /* 7.1.1-1 [dcl.stc]
2605 A storage-class-specifier shall not be specified in an
2606 explicit specialization...
2608 The parser rejects these, so unless action is taken here,
2609 explicit function specializations will always appear with
2610 global linkage.
2612 The action recommended by the C++ CWG in response to C++
2613 defect report 605 is to make the storage class and linkage
2614 of the explicit specialization match the templated function:
2616 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2618 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2620 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2621 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2623 /* This specialization has the same linkage and visibility as
2624 the function template it specializes. */
2625 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2626 if (! TREE_PUBLIC (decl))
2628 DECL_INTERFACE_KNOWN (decl) = 1;
2629 DECL_NOT_REALLY_EXTERN (decl) = 1;
2631 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2632 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2634 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2635 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2639 /* If DECL is a friend declaration, declared using an
2640 unqualified name, the namespace associated with DECL may
2641 have been set incorrectly. For example, in:
2643 template <typename T> void f(T);
2644 namespace N {
2645 struct S { friend void f<int>(int); }
2648 we will have set the DECL_CONTEXT for the friend
2649 declaration to N, rather than to the global namespace. */
2650 if (DECL_NAMESPACE_SCOPE_P (decl))
2651 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2653 if (is_friend && !have_def)
2654 /* This is not really a declaration of a specialization.
2655 It's just the name of an instantiation. But, it's not
2656 a request for an instantiation, either. */
2657 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2658 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2659 /* This is indeed a specialization. In case of constructors
2660 and destructors, we need in-charge and not-in-charge
2661 versions in V3 ABI. */
2662 clone_function_decl (decl, /*update_method_vec_p=*/0);
2664 /* Register this specialization so that we can find it
2665 again. */
2666 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2670 return decl;
2673 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2674 parameters. These are represented in the same format used for
2675 DECL_TEMPLATE_PARMS. */
2678 comp_template_parms (const_tree parms1, const_tree parms2)
2680 const_tree p1;
2681 const_tree p2;
2683 if (parms1 == parms2)
2684 return 1;
2686 for (p1 = parms1, p2 = parms2;
2687 p1 != NULL_TREE && p2 != NULL_TREE;
2688 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2690 tree t1 = TREE_VALUE (p1);
2691 tree t2 = TREE_VALUE (p2);
2692 int i;
2694 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2695 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2697 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2698 return 0;
2700 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2702 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2703 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2705 /* If either of the template parameters are invalid, assume
2706 they match for the sake of error recovery. */
2707 if (parm1 == error_mark_node || parm2 == error_mark_node)
2708 return 1;
2710 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2711 return 0;
2713 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2714 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2715 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2716 continue;
2717 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2718 return 0;
2722 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2723 /* One set of parameters has more parameters lists than the
2724 other. */
2725 return 0;
2727 return 1;
2730 /* Determine whether PARM is a parameter pack. */
2732 bool
2733 template_parameter_pack_p (const_tree parm)
2735 /* Determine if we have a non-type template parameter pack. */
2736 if (TREE_CODE (parm) == PARM_DECL)
2737 return (DECL_TEMPLATE_PARM_P (parm)
2738 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2739 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2740 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2742 /* If this is a list of template parameters, we could get a
2743 TYPE_DECL or a TEMPLATE_DECL. */
2744 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2745 parm = TREE_TYPE (parm);
2747 /* Otherwise it must be a type template parameter. */
2748 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2749 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2750 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2753 /* Determine if T is a function parameter pack. */
2755 bool
2756 function_parameter_pack_p (const_tree t)
2758 if (t && TREE_CODE (t) == PARM_DECL)
2759 return FUNCTION_PARAMETER_PACK_P (t);
2760 return false;
2763 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2764 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2766 tree
2767 get_function_template_decl (const_tree primary_func_tmpl_inst)
2769 if (! primary_func_tmpl_inst
2770 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2771 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2772 return NULL;
2774 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2777 /* Return true iff the function parameter PARAM_DECL was expanded
2778 from the function parameter pack PACK. */
2780 bool
2781 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2783 if (DECL_ARTIFICIAL (param_decl)
2784 || !function_parameter_pack_p (pack))
2785 return false;
2787 /* The parameter pack and its pack arguments have the same
2788 DECL_PARM_INDEX. */
2789 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2792 /* Determine whether ARGS describes a variadic template args list,
2793 i.e., one that is terminated by a template argument pack. */
2795 static bool
2796 template_args_variadic_p (tree args)
2798 int nargs;
2799 tree last_parm;
2801 if (args == NULL_TREE)
2802 return false;
2804 args = INNERMOST_TEMPLATE_ARGS (args);
2805 nargs = TREE_VEC_LENGTH (args);
2807 if (nargs == 0)
2808 return false;
2810 last_parm = TREE_VEC_ELT (args, nargs - 1);
2812 return ARGUMENT_PACK_P (last_parm);
2815 /* Generate a new name for the parameter pack name NAME (an
2816 IDENTIFIER_NODE) that incorporates its */
2818 static tree
2819 make_ith_pack_parameter_name (tree name, int i)
2821 /* Munge the name to include the parameter index. */
2822 #define NUMBUF_LEN 128
2823 char numbuf[NUMBUF_LEN];
2824 char* newname;
2825 int newname_len;
2827 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2828 newname_len = IDENTIFIER_LENGTH (name)
2829 + strlen (numbuf) + 2;
2830 newname = (char*)alloca (newname_len);
2831 snprintf (newname, newname_len,
2832 "%s#%i", IDENTIFIER_POINTER (name), i);
2833 return get_identifier (newname);
2836 /* Return true if T is a primary function, class or alias template
2837 instantiation. */
2839 bool
2840 primary_template_instantiation_p (const_tree t)
2842 if (!t)
2843 return false;
2845 if (TREE_CODE (t) == FUNCTION_DECL)
2846 return DECL_LANG_SPECIFIC (t)
2847 && DECL_TEMPLATE_INSTANTIATION (t)
2848 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2849 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2850 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2851 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2852 else if (TYPE_P (t)
2853 && TYPE_TEMPLATE_INFO (t)
2854 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
2855 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t)))
2856 return true;
2857 return false;
2860 /* Return true if PARM is a template template parameter. */
2862 bool
2863 template_template_parameter_p (const_tree parm)
2865 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2868 /* Return true iff PARM is a DECL representing a type template
2869 parameter. */
2871 bool
2872 template_type_parameter_p (const_tree parm)
2874 return (parm
2875 && (TREE_CODE (parm) == TYPE_DECL
2876 || TREE_CODE (parm) == TEMPLATE_DECL)
2877 && DECL_TEMPLATE_PARM_P (parm));
2880 /* Return the template parameters of T if T is a
2881 primary template instantiation, NULL otherwise. */
2883 tree
2884 get_primary_template_innermost_parameters (const_tree t)
2886 tree parms = NULL, template_info = NULL;
2888 if ((template_info = get_template_info (t))
2889 && primary_template_instantiation_p (t))
2890 parms = INNERMOST_TEMPLATE_PARMS
2891 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2893 return parms;
2896 /* Return the template parameters of the LEVELth level from the full list
2897 of template parameters PARMS. */
2899 tree
2900 get_template_parms_at_level (tree parms, int level)
2902 tree p;
2903 if (!parms
2904 || TREE_CODE (parms) != TREE_LIST
2905 || level > TMPL_PARMS_DEPTH (parms))
2906 return NULL_TREE;
2908 for (p = parms; p; p = TREE_CHAIN (p))
2909 if (TMPL_PARMS_DEPTH (p) == level)
2910 return p;
2912 return NULL_TREE;
2915 /* Returns the template arguments of T if T is a template instantiation,
2916 NULL otherwise. */
2918 tree
2919 get_template_innermost_arguments (const_tree t)
2921 tree args = NULL, template_info = NULL;
2923 if ((template_info = get_template_info (t))
2924 && TI_ARGS (template_info))
2925 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2927 return args;
2930 /* Return the argument pack elements of T if T is a template argument pack,
2931 NULL otherwise. */
2933 tree
2934 get_template_argument_pack_elems (const_tree t)
2936 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2937 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2938 return NULL;
2940 return ARGUMENT_PACK_ARGS (t);
2943 /* Structure used to track the progress of find_parameter_packs_r. */
2944 struct find_parameter_pack_data
2946 /* TREE_LIST that will contain all of the parameter packs found by
2947 the traversal. */
2948 tree* parameter_packs;
2950 /* Set of AST nodes that have been visited by the traversal. */
2951 struct pointer_set_t *visited;
2954 /* Identifies all of the argument packs that occur in a template
2955 argument and appends them to the TREE_LIST inside DATA, which is a
2956 find_parameter_pack_data structure. This is a subroutine of
2957 make_pack_expansion and uses_parameter_packs. */
2958 static tree
2959 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2961 tree t = *tp;
2962 struct find_parameter_pack_data* ppd =
2963 (struct find_parameter_pack_data*)data;
2964 bool parameter_pack_p = false;
2966 /* Handle type aliases/typedefs. */
2967 if (TYPE_P (t)
2968 && TYPE_NAME (t)
2969 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
2970 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2972 if (TYPE_TEMPLATE_INFO (t))
2973 cp_walk_tree (&TYPE_TI_ARGS (t),
2974 &find_parameter_packs_r,
2975 ppd, ppd->visited);
2976 *walk_subtrees = 0;
2977 return NULL_TREE;
2980 /* Identify whether this is a parameter pack or not. */
2981 switch (TREE_CODE (t))
2983 case TEMPLATE_PARM_INDEX:
2984 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2985 parameter_pack_p = true;
2986 break;
2988 case TEMPLATE_TYPE_PARM:
2989 t = TYPE_MAIN_VARIANT (t);
2990 case TEMPLATE_TEMPLATE_PARM:
2991 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2992 parameter_pack_p = true;
2993 break;
2995 case PARM_DECL:
2996 if (FUNCTION_PARAMETER_PACK_P (t))
2998 /* We don't want to walk into the type of a PARM_DECL,
2999 because we don't want to see the type parameter pack. */
3000 *walk_subtrees = 0;
3001 parameter_pack_p = true;
3003 break;
3005 case BASES:
3006 parameter_pack_p = true;
3007 break;
3008 default:
3009 /* Not a parameter pack. */
3010 break;
3013 if (parameter_pack_p)
3015 /* Add this parameter pack to the list. */
3016 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3019 if (TYPE_P (t))
3020 cp_walk_tree (&TYPE_CONTEXT (t),
3021 &find_parameter_packs_r, ppd, ppd->visited);
3023 /* This switch statement will return immediately if we don't find a
3024 parameter pack. */
3025 switch (TREE_CODE (t))
3027 case TEMPLATE_PARM_INDEX:
3028 return NULL_TREE;
3030 case BOUND_TEMPLATE_TEMPLATE_PARM:
3031 /* Check the template itself. */
3032 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3033 &find_parameter_packs_r, ppd, ppd->visited);
3034 /* Check the template arguments. */
3035 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3036 ppd->visited);
3037 *walk_subtrees = 0;
3038 return NULL_TREE;
3040 case TEMPLATE_TYPE_PARM:
3041 case TEMPLATE_TEMPLATE_PARM:
3042 return NULL_TREE;
3044 case PARM_DECL:
3045 return NULL_TREE;
3047 case RECORD_TYPE:
3048 if (TYPE_PTRMEMFUNC_P (t))
3049 return NULL_TREE;
3050 /* Fall through. */
3052 case UNION_TYPE:
3053 case ENUMERAL_TYPE:
3054 if (TYPE_TEMPLATE_INFO (t))
3055 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3056 &find_parameter_packs_r, ppd, ppd->visited);
3058 *walk_subtrees = 0;
3059 return NULL_TREE;
3061 case CONSTRUCTOR:
3062 case TEMPLATE_DECL:
3063 cp_walk_tree (&TREE_TYPE (t),
3064 &find_parameter_packs_r, ppd, ppd->visited);
3065 return NULL_TREE;
3067 case TYPENAME_TYPE:
3068 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3069 ppd, ppd->visited);
3070 *walk_subtrees = 0;
3071 return NULL_TREE;
3073 case TYPE_PACK_EXPANSION:
3074 case EXPR_PACK_EXPANSION:
3075 *walk_subtrees = 0;
3076 return NULL_TREE;
3078 case INTEGER_TYPE:
3079 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3080 ppd, ppd->visited);
3081 *walk_subtrees = 0;
3082 return NULL_TREE;
3084 case IDENTIFIER_NODE:
3085 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3086 ppd->visited);
3087 *walk_subtrees = 0;
3088 return NULL_TREE;
3090 default:
3091 return NULL_TREE;
3094 return NULL_TREE;
3097 /* Determines if the expression or type T uses any parameter packs. */
3098 bool
3099 uses_parameter_packs (tree t)
3101 tree parameter_packs = NULL_TREE;
3102 struct find_parameter_pack_data ppd;
3103 ppd.parameter_packs = &parameter_packs;
3104 ppd.visited = pointer_set_create ();
3105 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3106 pointer_set_destroy (ppd.visited);
3107 return parameter_packs != NULL_TREE;
3110 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3111 representation a base-class initializer into a parameter pack
3112 expansion. If all goes well, the resulting node will be an
3113 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3114 respectively. */
3115 tree
3116 make_pack_expansion (tree arg)
3118 tree result;
3119 tree parameter_packs = NULL_TREE;
3120 bool for_types = false;
3121 struct find_parameter_pack_data ppd;
3123 if (!arg || arg == error_mark_node)
3124 return arg;
3126 if (TREE_CODE (arg) == TREE_LIST)
3128 /* The only time we will see a TREE_LIST here is for a base
3129 class initializer. In this case, the TREE_PURPOSE will be a
3130 _TYPE node (representing the base class expansion we're
3131 initializing) and the TREE_VALUE will be a TREE_LIST
3132 containing the initialization arguments.
3134 The resulting expansion looks somewhat different from most
3135 expansions. Rather than returning just one _EXPANSION, we
3136 return a TREE_LIST whose TREE_PURPOSE is a
3137 TYPE_PACK_EXPANSION containing the bases that will be
3138 initialized. The TREE_VALUE will be identical to the
3139 original TREE_VALUE, which is a list of arguments that will
3140 be passed to each base. We do not introduce any new pack
3141 expansion nodes into the TREE_VALUE (although it is possible
3142 that some already exist), because the TREE_PURPOSE and
3143 TREE_VALUE all need to be expanded together with the same
3144 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3145 resulting TREE_PURPOSE will mention the parameter packs in
3146 both the bases and the arguments to the bases. */
3147 tree purpose;
3148 tree value;
3149 tree parameter_packs = NULL_TREE;
3151 /* Determine which parameter packs will be used by the base
3152 class expansion. */
3153 ppd.visited = pointer_set_create ();
3154 ppd.parameter_packs = &parameter_packs;
3155 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3156 &ppd, ppd.visited);
3158 if (parameter_packs == NULL_TREE)
3160 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3161 pointer_set_destroy (ppd.visited);
3162 return error_mark_node;
3165 if (TREE_VALUE (arg) != void_type_node)
3167 /* Collect the sets of parameter packs used in each of the
3168 initialization arguments. */
3169 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3171 /* Determine which parameter packs will be expanded in this
3172 argument. */
3173 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3174 &ppd, ppd.visited);
3178 pointer_set_destroy (ppd.visited);
3180 /* Create the pack expansion type for the base type. */
3181 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3182 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3183 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3185 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3186 they will rarely be compared to anything. */
3187 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3189 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3192 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3193 for_types = true;
3195 /* Build the PACK_EXPANSION_* node. */
3196 result = for_types
3197 ? cxx_make_type (TYPE_PACK_EXPANSION)
3198 : make_node (EXPR_PACK_EXPANSION);
3199 SET_PACK_EXPANSION_PATTERN (result, arg);
3200 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3202 /* Propagate type and const-expression information. */
3203 TREE_TYPE (result) = TREE_TYPE (arg);
3204 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3206 else
3207 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3208 they will rarely be compared to anything. */
3209 SET_TYPE_STRUCTURAL_EQUALITY (result);
3211 /* Determine which parameter packs will be expanded. */
3212 ppd.parameter_packs = &parameter_packs;
3213 ppd.visited = pointer_set_create ();
3214 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3215 pointer_set_destroy (ppd.visited);
3217 /* Make sure we found some parameter packs. */
3218 if (parameter_packs == NULL_TREE)
3220 if (TYPE_P (arg))
3221 error ("expansion pattern %<%T%> contains no argument packs", arg);
3222 else
3223 error ("expansion pattern %<%E%> contains no argument packs", arg);
3224 return error_mark_node;
3226 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3228 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3230 return result;
3233 /* Checks T for any "bare" parameter packs, which have not yet been
3234 expanded, and issues an error if any are found. This operation can
3235 only be done on full expressions or types (e.g., an expression
3236 statement, "if" condition, etc.), because we could have expressions like:
3238 foo(f(g(h(args)))...)
3240 where "args" is a parameter pack. check_for_bare_parameter_packs
3241 should not be called for the subexpressions args, h(args),
3242 g(h(args)), or f(g(h(args))), because we would produce erroneous
3243 error messages.
3245 Returns TRUE and emits an error if there were bare parameter packs,
3246 returns FALSE otherwise. */
3247 bool
3248 check_for_bare_parameter_packs (tree t)
3250 tree parameter_packs = NULL_TREE;
3251 struct find_parameter_pack_data ppd;
3253 if (!processing_template_decl || !t || t == error_mark_node)
3254 return false;
3256 if (TREE_CODE (t) == TYPE_DECL)
3257 t = TREE_TYPE (t);
3259 ppd.parameter_packs = &parameter_packs;
3260 ppd.visited = pointer_set_create ();
3261 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3262 pointer_set_destroy (ppd.visited);
3264 if (parameter_packs)
3266 error ("parameter packs not expanded with %<...%>:");
3267 while (parameter_packs)
3269 tree pack = TREE_VALUE (parameter_packs);
3270 tree name = NULL_TREE;
3272 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3273 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3274 name = TYPE_NAME (pack);
3275 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3276 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3277 else
3278 name = DECL_NAME (pack);
3280 if (name)
3281 inform (input_location, " %qD", name);
3282 else
3283 inform (input_location, " <anonymous>");
3285 parameter_packs = TREE_CHAIN (parameter_packs);
3288 return true;
3291 return false;
3294 /* Expand any parameter packs that occur in the template arguments in
3295 ARGS. */
3296 tree
3297 expand_template_argument_pack (tree args)
3299 tree result_args = NULL_TREE;
3300 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3301 int num_result_args = -1;
3302 int non_default_args_count = -1;
3304 /* First, determine if we need to expand anything, and the number of
3305 slots we'll need. */
3306 for (in_arg = 0; in_arg < nargs; ++in_arg)
3308 tree arg = TREE_VEC_ELT (args, in_arg);
3309 if (arg == NULL_TREE)
3310 return args;
3311 if (ARGUMENT_PACK_P (arg))
3313 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3314 if (num_result_args < 0)
3315 num_result_args = in_arg + num_packed;
3316 else
3317 num_result_args += num_packed;
3319 else
3321 if (num_result_args >= 0)
3322 num_result_args++;
3326 /* If no expansion is necessary, we're done. */
3327 if (num_result_args < 0)
3328 return args;
3330 /* Expand arguments. */
3331 result_args = make_tree_vec (num_result_args);
3332 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3333 non_default_args_count =
3334 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3335 for (in_arg = 0; in_arg < nargs; ++in_arg)
3337 tree arg = TREE_VEC_ELT (args, in_arg);
3338 if (ARGUMENT_PACK_P (arg))
3340 tree packed = ARGUMENT_PACK_ARGS (arg);
3341 int i, num_packed = TREE_VEC_LENGTH (packed);
3342 for (i = 0; i < num_packed; ++i, ++out_arg)
3343 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3344 if (non_default_args_count > 0)
3345 non_default_args_count += num_packed;
3347 else
3349 TREE_VEC_ELT (result_args, out_arg) = arg;
3350 ++out_arg;
3353 if (non_default_args_count >= 0)
3354 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3355 return result_args;
3358 /* Checks if DECL shadows a template parameter.
3360 [temp.local]: A template-parameter shall not be redeclared within its
3361 scope (including nested scopes).
3363 Emits an error and returns TRUE if the DECL shadows a parameter,
3364 returns FALSE otherwise. */
3366 bool
3367 check_template_shadow (tree decl)
3369 tree olddecl;
3371 /* If we're not in a template, we can't possibly shadow a template
3372 parameter. */
3373 if (!current_template_parms)
3374 return true;
3376 /* Figure out what we're shadowing. */
3377 if (TREE_CODE (decl) == OVERLOAD)
3378 decl = OVL_CURRENT (decl);
3379 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3381 /* If there's no previous binding for this name, we're not shadowing
3382 anything, let alone a template parameter. */
3383 if (!olddecl)
3384 return true;
3386 /* If we're not shadowing a template parameter, we're done. Note
3387 that OLDDECL might be an OVERLOAD (or perhaps even an
3388 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3389 node. */
3390 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3391 return true;
3393 /* We check for decl != olddecl to avoid bogus errors for using a
3394 name inside a class. We check TPFI to avoid duplicate errors for
3395 inline member templates. */
3396 if (decl == olddecl
3397 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3398 return true;
3400 error ("declaration of %q+#D", decl);
3401 error (" shadows template parm %q+#D", olddecl);
3402 return false;
3405 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3406 ORIG_LEVEL, DECL, and TYPE. */
3408 static tree
3409 build_template_parm_index (int index,
3410 int level,
3411 int orig_level,
3412 tree decl,
3413 tree type)
3415 tree t = make_node (TEMPLATE_PARM_INDEX);
3416 TEMPLATE_PARM_IDX (t) = index;
3417 TEMPLATE_PARM_LEVEL (t) = level;
3418 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3419 TEMPLATE_PARM_DECL (t) = decl;
3420 TREE_TYPE (t) = type;
3421 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3422 TREE_READONLY (t) = TREE_READONLY (decl);
3424 return t;
3427 /* Find the canonical type parameter for the given template type
3428 parameter. Returns the canonical type parameter, which may be TYPE
3429 if no such parameter existed. */
3431 static tree
3432 canonical_type_parameter (tree type)
3434 tree list;
3435 int idx = TEMPLATE_TYPE_IDX (type);
3436 if (!canonical_template_parms)
3437 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3439 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3440 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3442 list = VEC_index (tree, canonical_template_parms, idx);
3443 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3444 list = TREE_CHAIN (list);
3446 if (list)
3447 return TREE_VALUE (list);
3448 else
3450 VEC_replace(tree, canonical_template_parms, idx,
3451 tree_cons (NULL_TREE, type,
3452 VEC_index (tree, canonical_template_parms, idx)));
3453 return type;
3457 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3458 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3459 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3460 new one is created. */
3462 static tree
3463 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3464 tsubst_flags_t complain)
3466 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3467 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3468 != TEMPLATE_PARM_LEVEL (index) - levels)
3469 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3471 tree orig_decl = TEMPLATE_PARM_DECL (index);
3472 tree decl, t;
3474 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3475 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3476 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3477 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3478 DECL_ARTIFICIAL (decl) = 1;
3479 SET_DECL_TEMPLATE_PARM_P (decl);
3481 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3482 TEMPLATE_PARM_LEVEL (index) - levels,
3483 TEMPLATE_PARM_ORIG_LEVEL (index),
3484 decl, type);
3485 TEMPLATE_PARM_DESCENDANTS (index) = t;
3486 TEMPLATE_PARM_PARAMETER_PACK (t)
3487 = TEMPLATE_PARM_PARAMETER_PACK (index);
3489 /* Template template parameters need this. */
3490 if (TREE_CODE (decl) == TEMPLATE_DECL)
3491 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3492 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3493 args, complain);
3496 return TEMPLATE_PARM_DESCENDANTS (index);
3499 /* Process information from new template parameter PARM and append it
3500 to the LIST being built. This new parameter is a non-type
3501 parameter iff IS_NON_TYPE is true. This new parameter is a
3502 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3503 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3504 parameter list PARM belongs to. This is used used to create a
3505 proper canonical type for the type of PARM that is to be created,
3506 iff PARM is a type. If the size is not known, this parameter shall
3507 be set to 0. */
3509 tree
3510 process_template_parm (tree list, location_t parm_loc, tree parm,
3511 bool is_non_type, bool is_parameter_pack)
3513 tree decl = 0;
3514 tree defval;
3515 tree err_parm_list;
3516 int idx = 0;
3518 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3519 defval = TREE_PURPOSE (parm);
3521 if (list)
3523 tree p = tree_last (list);
3525 if (p && TREE_VALUE (p) != error_mark_node)
3527 p = TREE_VALUE (p);
3528 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3529 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3530 else
3531 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3534 ++idx;
3536 else
3537 idx = 0;
3539 if (is_non_type)
3541 parm = TREE_VALUE (parm);
3543 SET_DECL_TEMPLATE_PARM_P (parm);
3545 if (TREE_TYPE (parm) == error_mark_node)
3547 err_parm_list = build_tree_list (defval, parm);
3548 TREE_VALUE (err_parm_list) = error_mark_node;
3549 return chainon (list, err_parm_list);
3551 else
3553 /* [temp.param]
3555 The top-level cv-qualifiers on the template-parameter are
3556 ignored when determining its type. */
3557 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3558 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3560 err_parm_list = build_tree_list (defval, parm);
3561 TREE_VALUE (err_parm_list) = error_mark_node;
3562 return chainon (list, err_parm_list);
3565 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3567 /* This template parameter is not a parameter pack, but it
3568 should be. Complain about "bare" parameter packs. */
3569 check_for_bare_parameter_packs (TREE_TYPE (parm));
3571 /* Recover by calling this a parameter pack. */
3572 is_parameter_pack = true;
3576 /* A template parameter is not modifiable. */
3577 TREE_CONSTANT (parm) = 1;
3578 TREE_READONLY (parm) = 1;
3579 decl = build_decl (parm_loc,
3580 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3581 TREE_CONSTANT (decl) = 1;
3582 TREE_READONLY (decl) = 1;
3583 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3584 = build_template_parm_index (idx, processing_template_decl,
3585 processing_template_decl,
3586 decl, TREE_TYPE (parm));
3588 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3589 = is_parameter_pack;
3591 else
3593 tree t;
3594 parm = TREE_VALUE (TREE_VALUE (parm));
3596 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3598 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3599 /* This is for distinguishing between real templates and template
3600 template parameters */
3601 TREE_TYPE (parm) = t;
3602 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3603 decl = parm;
3605 else
3607 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3608 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3609 decl = build_decl (parm_loc,
3610 TYPE_DECL, parm, t);
3613 TYPE_NAME (t) = decl;
3614 TYPE_STUB_DECL (t) = decl;
3615 parm = decl;
3616 TEMPLATE_TYPE_PARM_INDEX (t)
3617 = build_template_parm_index (idx, processing_template_decl,
3618 processing_template_decl,
3619 decl, TREE_TYPE (parm));
3620 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3621 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3623 DECL_ARTIFICIAL (decl) = 1;
3624 SET_DECL_TEMPLATE_PARM_P (decl);
3625 pushdecl (decl);
3626 parm = build_tree_list (defval, parm);
3627 return chainon (list, parm);
3630 /* The end of a template parameter list has been reached. Process the
3631 tree list into a parameter vector, converting each parameter into a more
3632 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3633 as PARM_DECLs. */
3635 tree
3636 end_template_parm_list (tree parms)
3638 int nparms;
3639 tree parm, next;
3640 tree saved_parmlist = make_tree_vec (list_length (parms));
3642 current_template_parms
3643 = tree_cons (size_int (processing_template_decl),
3644 saved_parmlist, current_template_parms);
3646 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3648 next = TREE_CHAIN (parm);
3649 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3650 TREE_CHAIN (parm) = NULL_TREE;
3653 --processing_template_parmlist;
3655 return saved_parmlist;
3658 /* end_template_decl is called after a template declaration is seen. */
3660 void
3661 end_template_decl (void)
3663 reset_specialization ();
3665 if (! processing_template_decl)
3666 return;
3668 /* This matches the pushlevel in begin_template_parm_list. */
3669 finish_scope ();
3671 --processing_template_decl;
3672 current_template_parms = TREE_CHAIN (current_template_parms);
3675 /* Takes a TREE_LIST representing a template parameter and convert it
3676 into an argument suitable to be passed to the type substitution
3677 functions. Note that If the TREE_LIST contains an error_mark
3678 node, the returned argument is error_mark_node. */
3680 static tree
3681 template_parm_to_arg (tree t)
3684 if (t == NULL_TREE
3685 || TREE_CODE (t) != TREE_LIST)
3686 return t;
3688 if (error_operand_p (TREE_VALUE (t)))
3689 return error_mark_node;
3691 t = TREE_VALUE (t);
3693 if (TREE_CODE (t) == TYPE_DECL
3694 || TREE_CODE (t) == TEMPLATE_DECL)
3696 t = TREE_TYPE (t);
3698 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3700 /* Turn this argument into a TYPE_ARGUMENT_PACK
3701 with a single element, which expands T. */
3702 tree vec = make_tree_vec (1);
3703 #ifdef ENABLE_CHECKING
3704 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3705 (vec, TREE_VEC_LENGTH (vec));
3706 #endif
3707 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3709 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3710 SET_ARGUMENT_PACK_ARGS (t, vec);
3713 else
3715 t = DECL_INITIAL (t);
3717 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3719 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3720 with a single element, which expands T. */
3721 tree vec = make_tree_vec (1);
3722 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3723 #ifdef ENABLE_CHECKING
3724 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3725 (vec, TREE_VEC_LENGTH (vec));
3726 #endif
3727 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3729 t = make_node (NONTYPE_ARGUMENT_PACK);
3730 SET_ARGUMENT_PACK_ARGS (t, vec);
3731 TREE_TYPE (t) = type;
3734 return t;
3737 /* This function returns TRUE if PARM_PACK is a template parameter
3738 pack and if ARG_PACK is what template_parm_to_arg returned when
3739 passed PARM_PACK. */
3741 static bool
3742 arg_from_parm_pack_p (tree arg_pack, tree parm_pack)
3744 /* For clarity in the comments below let's use the representation
3745 argument_pack<elements>' to denote an argument pack and its
3746 elements.
3748 In the 'if' block below, we want to detect cases where
3749 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
3750 check if ARG_PACK is an argument pack which sole element is
3751 the expansion of PARM_PACK. That argument pack is typically
3752 created by template_parm_to_arg when passed a parameter
3753 pack. */
3755 if (arg_pack
3756 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
3757 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
3759 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
3760 tree pattern = PACK_EXPANSION_PATTERN (expansion);
3761 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
3762 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
3763 /* The argument pack that the parameter maps to is just an
3764 expansion of the parameter itself, such as one would
3765 find in the implicit typedef of a class inside the
3766 class itself. Consider this parameter "unsubstituted",
3767 so that we will maintain the outer pack expansion. */
3768 return true;
3770 return false;
3773 /* Within the declaration of a template, return all levels of template
3774 parameters that apply. The template parameters are represented as
3775 a TREE_VEC, in the form documented in cp-tree.h for template
3776 arguments. */
3778 static tree
3779 current_template_args (void)
3781 tree header;
3782 tree args = NULL_TREE;
3783 int length = TMPL_PARMS_DEPTH (current_template_parms);
3784 int l = length;
3786 /* If there is only one level of template parameters, we do not
3787 create a TREE_VEC of TREE_VECs. Instead, we return a single
3788 TREE_VEC containing the arguments. */
3789 if (length > 1)
3790 args = make_tree_vec (length);
3792 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3794 tree a = copy_node (TREE_VALUE (header));
3795 int i;
3797 TREE_TYPE (a) = NULL_TREE;
3798 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3799 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3801 #ifdef ENABLE_CHECKING
3802 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3803 #endif
3805 if (length > 1)
3806 TREE_VEC_ELT (args, --l) = a;
3807 else
3808 args = a;
3811 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3812 /* This can happen for template parms of a template template
3813 parameter, e.g:
3815 template<template<class T, class U> class TT> struct S;
3817 Consider the level of the parms of TT; T and U both have
3818 level 2; TT has no template parm of level 1. So in this case
3819 the first element of full_template_args is NULL_TREE. If we
3820 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3821 of 2. This will make tsubst wrongly consider that T and U
3822 have level 1. Instead, let's create a dummy vector as the
3823 first element of full_template_args so that TMPL_ARG_DEPTH
3824 returns the correct depth for args. */
3825 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3826 return args;
3829 /* Update the declared TYPE by doing any lookups which were thought to be
3830 dependent, but are not now that we know the SCOPE of the declarator. */
3832 tree
3833 maybe_update_decl_type (tree orig_type, tree scope)
3835 tree type = orig_type;
3837 if (type == NULL_TREE)
3838 return type;
3840 if (TREE_CODE (orig_type) == TYPE_DECL)
3841 type = TREE_TYPE (type);
3843 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3844 && dependent_type_p (type)
3845 /* Don't bother building up the args in this case. */
3846 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3848 /* tsubst in the args corresponding to the template parameters,
3849 including auto if present. Most things will be unchanged, but
3850 make_typename_type and tsubst_qualified_id will resolve
3851 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3852 tree args = current_template_args ();
3853 tree auto_node = type_uses_auto (type);
3854 tree pushed;
3855 if (auto_node)
3857 tree auto_vec = make_tree_vec (1);
3858 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3859 args = add_to_template_args (args, auto_vec);
3861 pushed = push_scope (scope);
3862 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3863 if (pushed)
3864 pop_scope (scope);
3867 if (type == error_mark_node)
3868 return orig_type;
3870 if (TREE_CODE (orig_type) == TYPE_DECL)
3872 if (same_type_p (type, TREE_TYPE (orig_type)))
3873 type = orig_type;
3874 else
3875 type = TYPE_NAME (type);
3877 return type;
3880 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3881 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3882 a member template. Used by push_template_decl below. */
3884 static tree
3885 build_template_decl (tree decl, tree parms, bool member_template_p)
3887 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3888 DECL_TEMPLATE_PARMS (tmpl) = parms;
3889 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3890 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3891 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3893 return tmpl;
3896 struct template_parm_data
3898 /* The level of the template parameters we are currently
3899 processing. */
3900 int level;
3902 /* The index of the specialization argument we are currently
3903 processing. */
3904 int current_arg;
3906 /* An array whose size is the number of template parameters. The
3907 elements are nonzero if the parameter has been used in any one
3908 of the arguments processed so far. */
3909 int* parms;
3911 /* An array whose size is the number of template arguments. The
3912 elements are nonzero if the argument makes use of template
3913 parameters of this level. */
3914 int* arg_uses_template_parms;
3917 /* Subroutine of push_template_decl used to see if each template
3918 parameter in a partial specialization is used in the explicit
3919 argument list. If T is of the LEVEL given in DATA (which is
3920 treated as a template_parm_data*), then DATA->PARMS is marked
3921 appropriately. */
3923 static int
3924 mark_template_parm (tree t, void* data)
3926 int level;
3927 int idx;
3928 struct template_parm_data* tpd = (struct template_parm_data*) data;
3930 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3932 level = TEMPLATE_PARM_LEVEL (t);
3933 idx = TEMPLATE_PARM_IDX (t);
3935 else
3937 level = TEMPLATE_TYPE_LEVEL (t);
3938 idx = TEMPLATE_TYPE_IDX (t);
3941 if (level == tpd->level)
3943 tpd->parms[idx] = 1;
3944 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3947 /* Return zero so that for_each_template_parm will continue the
3948 traversal of the tree; we want to mark *every* template parm. */
3949 return 0;
3952 /* Process the partial specialization DECL. */
3954 static tree
3955 process_partial_specialization (tree decl)
3957 tree type = TREE_TYPE (decl);
3958 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3959 tree specargs = CLASSTYPE_TI_ARGS (type);
3960 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3961 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3962 tree inner_parms;
3963 tree inst;
3964 int nargs = TREE_VEC_LENGTH (inner_args);
3965 int ntparms;
3966 int i;
3967 bool did_error_intro = false;
3968 struct template_parm_data tpd;
3969 struct template_parm_data tpd2;
3971 gcc_assert (current_template_parms);
3973 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3974 ntparms = TREE_VEC_LENGTH (inner_parms);
3976 /* We check that each of the template parameters given in the
3977 partial specialization is used in the argument list to the
3978 specialization. For example:
3980 template <class T> struct S;
3981 template <class T> struct S<T*>;
3983 The second declaration is OK because `T*' uses the template
3984 parameter T, whereas
3986 template <class T> struct S<int>;
3988 is no good. Even trickier is:
3990 template <class T>
3991 struct S1
3993 template <class U>
3994 struct S2;
3995 template <class U>
3996 struct S2<T>;
3999 The S2<T> declaration is actually invalid; it is a
4000 full-specialization. Of course,
4002 template <class U>
4003 struct S2<T (*)(U)>;
4005 or some such would have been OK. */
4006 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4007 tpd.parms = XALLOCAVEC (int, ntparms);
4008 memset (tpd.parms, 0, sizeof (int) * ntparms);
4010 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4011 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4012 for (i = 0; i < nargs; ++i)
4014 tpd.current_arg = i;
4015 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4016 &mark_template_parm,
4017 &tpd,
4018 NULL,
4019 /*include_nondeduced_p=*/false);
4021 for (i = 0; i < ntparms; ++i)
4022 if (tpd.parms[i] == 0)
4024 /* One of the template parms was not used in the
4025 specialization. */
4026 if (!did_error_intro)
4028 error ("template parameters not used in partial specialization:");
4029 did_error_intro = true;
4032 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4035 if (did_error_intro)
4036 return error_mark_node;
4038 /* [temp.class.spec]
4040 The argument list of the specialization shall not be identical to
4041 the implicit argument list of the primary template. */
4042 if (comp_template_args
4043 (inner_args,
4044 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4045 (maintmpl)))))
4046 error ("partial specialization %qT does not specialize any template arguments", type);
4048 /* A partial specialization that replaces multiple parameters of the
4049 primary template with a pack expansion is less specialized for those
4050 parameters. */
4051 if (nargs < DECL_NTPARMS (maintmpl))
4053 error ("partial specialization is not more specialized than the "
4054 "primary template because it replaces multiple parameters "
4055 "with a pack expansion");
4056 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4057 return decl;
4060 /* [temp.class.spec]
4062 A partially specialized non-type argument expression shall not
4063 involve template parameters of the partial specialization except
4064 when the argument expression is a simple identifier.
4066 The type of a template parameter corresponding to a specialized
4067 non-type argument shall not be dependent on a parameter of the
4068 specialization.
4070 Also, we verify that pack expansions only occur at the
4071 end of the argument list. */
4072 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4073 tpd2.parms = 0;
4074 for (i = 0; i < nargs; ++i)
4076 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4077 tree arg = TREE_VEC_ELT (inner_args, i);
4078 tree packed_args = NULL_TREE;
4079 int j, len = 1;
4081 if (ARGUMENT_PACK_P (arg))
4083 /* Extract the arguments from the argument pack. We'll be
4084 iterating over these in the following loop. */
4085 packed_args = ARGUMENT_PACK_ARGS (arg);
4086 len = TREE_VEC_LENGTH (packed_args);
4089 for (j = 0; j < len; j++)
4091 if (packed_args)
4092 /* Get the Jth argument in the parameter pack. */
4093 arg = TREE_VEC_ELT (packed_args, j);
4095 if (PACK_EXPANSION_P (arg))
4097 /* Pack expansions must come at the end of the
4098 argument list. */
4099 if ((packed_args && j < len - 1)
4100 || (!packed_args && i < nargs - 1))
4102 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4103 error ("parameter pack argument %qE must be at the "
4104 "end of the template argument list", arg);
4105 else
4106 error ("parameter pack argument %qT must be at the "
4107 "end of the template argument list", arg);
4111 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4112 /* We only care about the pattern. */
4113 arg = PACK_EXPANSION_PATTERN (arg);
4115 if (/* These first two lines are the `non-type' bit. */
4116 !TYPE_P (arg)
4117 && TREE_CODE (arg) != TEMPLATE_DECL
4118 /* This next line is the `argument expression is not just a
4119 simple identifier' condition and also the `specialized
4120 non-type argument' bit. */
4121 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4123 if ((!packed_args && tpd.arg_uses_template_parms[i])
4124 || (packed_args && uses_template_parms (arg)))
4125 error ("template argument %qE involves template parameter(s)",
4126 arg);
4127 else
4129 /* Look at the corresponding template parameter,
4130 marking which template parameters its type depends
4131 upon. */
4132 tree type = TREE_TYPE (parm);
4134 if (!tpd2.parms)
4136 /* We haven't yet initialized TPD2. Do so now. */
4137 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4138 /* The number of parameters here is the number in the
4139 main template, which, as checked in the assertion
4140 above, is NARGS. */
4141 tpd2.parms = XALLOCAVEC (int, nargs);
4142 tpd2.level =
4143 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4146 /* Mark the template parameters. But this time, we're
4147 looking for the template parameters of the main
4148 template, not in the specialization. */
4149 tpd2.current_arg = i;
4150 tpd2.arg_uses_template_parms[i] = 0;
4151 memset (tpd2.parms, 0, sizeof (int) * nargs);
4152 for_each_template_parm (type,
4153 &mark_template_parm,
4154 &tpd2,
4155 NULL,
4156 /*include_nondeduced_p=*/false);
4158 if (tpd2.arg_uses_template_parms [i])
4160 /* The type depended on some template parameters.
4161 If they are fully specialized in the
4162 specialization, that's OK. */
4163 int j;
4164 int count = 0;
4165 for (j = 0; j < nargs; ++j)
4166 if (tpd2.parms[j] != 0
4167 && tpd.arg_uses_template_parms [j])
4168 ++count;
4169 if (count != 0)
4170 error_n (input_location, count,
4171 "type %qT of template argument %qE depends "
4172 "on a template parameter",
4173 "type %qT of template argument %qE depends "
4174 "on template parameters",
4175 type,
4176 arg);
4183 /* We should only get here once. */
4184 gcc_assert (!COMPLETE_TYPE_P (type));
4186 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4187 = tree_cons (specargs, inner_parms,
4188 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4189 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4191 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4192 inst = TREE_CHAIN (inst))
4194 tree inst_type = TREE_VALUE (inst);
4195 if (COMPLETE_TYPE_P (inst_type)
4196 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4198 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4199 if (spec && TREE_TYPE (spec) == type)
4200 permerror (input_location,
4201 "partial specialization of %qT after instantiation "
4202 "of %qT", type, inst_type);
4206 return decl;
4209 /* Check that a template declaration's use of default arguments and
4210 parameter packs is not invalid. Here, PARMS are the template
4211 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4212 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4213 specialization.
4216 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4217 declaration (but not a definition); 1 indicates a declaration, 2
4218 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4219 emitted for extraneous default arguments.
4221 Returns TRUE if there were no errors found, FALSE otherwise. */
4223 bool
4224 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4225 int is_partial, int is_friend_decl)
4227 const char *msg;
4228 int last_level_to_check;
4229 tree parm_level;
4230 bool no_errors = true;
4232 /* [temp.param]
4234 A default template-argument shall not be specified in a
4235 function template declaration or a function template definition, nor
4236 in the template-parameter-list of the definition of a member of a
4237 class template. */
4239 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4240 /* You can't have a function template declaration in a local
4241 scope, nor you can you define a member of a class template in a
4242 local scope. */
4243 return true;
4245 if (current_class_type
4246 && !TYPE_BEING_DEFINED (current_class_type)
4247 && DECL_LANG_SPECIFIC (decl)
4248 && DECL_DECLARES_FUNCTION_P (decl)
4249 /* If this is either a friend defined in the scope of the class
4250 or a member function. */
4251 && (DECL_FUNCTION_MEMBER_P (decl)
4252 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4253 : DECL_FRIEND_CONTEXT (decl)
4254 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4255 : false)
4256 /* And, if it was a member function, it really was defined in
4257 the scope of the class. */
4258 && (!DECL_FUNCTION_MEMBER_P (decl)
4259 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4260 /* We already checked these parameters when the template was
4261 declared, so there's no need to do it again now. This function
4262 was defined in class scope, but we're processing it's body now
4263 that the class is complete. */
4264 return true;
4266 /* Core issue 226 (C++0x only): the following only applies to class
4267 templates. */
4268 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4270 /* [temp.param]
4272 If a template-parameter has a default template-argument, all
4273 subsequent template-parameters shall have a default
4274 template-argument supplied. */
4275 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4277 tree inner_parms = TREE_VALUE (parm_level);
4278 int ntparms = TREE_VEC_LENGTH (inner_parms);
4279 int seen_def_arg_p = 0;
4280 int i;
4282 for (i = 0; i < ntparms; ++i)
4284 tree parm = TREE_VEC_ELT (inner_parms, i);
4286 if (parm == error_mark_node)
4287 continue;
4289 if (TREE_PURPOSE (parm))
4290 seen_def_arg_p = 1;
4291 else if (seen_def_arg_p
4292 && !template_parameter_pack_p (TREE_VALUE (parm)))
4294 error ("no default argument for %qD", TREE_VALUE (parm));
4295 /* For better subsequent error-recovery, we indicate that
4296 there should have been a default argument. */
4297 TREE_PURPOSE (parm) = error_mark_node;
4298 no_errors = false;
4300 else if (is_primary
4301 && !is_partial
4302 && !is_friend_decl
4303 /* Don't complain about an enclosing partial
4304 specialization. */
4305 && parm_level == parms
4306 && TREE_CODE (decl) == TYPE_DECL
4307 && i < ntparms - 1
4308 && template_parameter_pack_p (TREE_VALUE (parm)))
4310 /* A primary class template can only have one
4311 parameter pack, at the end of the template
4312 parameter list. */
4314 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4315 error ("parameter pack %qE must be at the end of the"
4316 " template parameter list", TREE_VALUE (parm));
4317 else
4318 error ("parameter pack %qT must be at the end of the"
4319 " template parameter list",
4320 TREE_TYPE (TREE_VALUE (parm)));
4322 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4323 = error_mark_node;
4324 no_errors = false;
4330 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4331 || is_partial
4332 || !is_primary
4333 || is_friend_decl)
4334 /* For an ordinary class template, default template arguments are
4335 allowed at the innermost level, e.g.:
4336 template <class T = int>
4337 struct S {};
4338 but, in a partial specialization, they're not allowed even
4339 there, as we have in [temp.class.spec]:
4341 The template parameter list of a specialization shall not
4342 contain default template argument values.
4344 So, for a partial specialization, or for a function template
4345 (in C++98/C++03), we look at all of them. */
4347 else
4348 /* But, for a primary class template that is not a partial
4349 specialization we look at all template parameters except the
4350 innermost ones. */
4351 parms = TREE_CHAIN (parms);
4353 /* Figure out what error message to issue. */
4354 if (is_friend_decl == 2)
4355 msg = G_("default template arguments may not be used in function template "
4356 "friend re-declaration");
4357 else if (is_friend_decl)
4358 msg = G_("default template arguments may not be used in function template "
4359 "friend declarations");
4360 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4361 msg = G_("default template arguments may not be used in function templates "
4362 "without -std=c++11 or -std=gnu++11");
4363 else if (is_partial)
4364 msg = G_("default template arguments may not be used in "
4365 "partial specializations");
4366 else
4367 msg = G_("default argument for template parameter for class enclosing %qD");
4369 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4370 /* If we're inside a class definition, there's no need to
4371 examine the parameters to the class itself. On the one
4372 hand, they will be checked when the class is defined, and,
4373 on the other, default arguments are valid in things like:
4374 template <class T = double>
4375 struct S { template <class U> void f(U); };
4376 Here the default argument for `S' has no bearing on the
4377 declaration of `f'. */
4378 last_level_to_check = template_class_depth (current_class_type) + 1;
4379 else
4380 /* Check everything. */
4381 last_level_to_check = 0;
4383 for (parm_level = parms;
4384 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4385 parm_level = TREE_CHAIN (parm_level))
4387 tree inner_parms = TREE_VALUE (parm_level);
4388 int i;
4389 int ntparms;
4391 ntparms = TREE_VEC_LENGTH (inner_parms);
4392 for (i = 0; i < ntparms; ++i)
4394 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4395 continue;
4397 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4399 if (msg)
4401 no_errors = false;
4402 if (is_friend_decl == 2)
4403 return no_errors;
4405 error (msg, decl);
4406 msg = 0;
4409 /* Clear out the default argument so that we are not
4410 confused later. */
4411 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4415 /* At this point, if we're still interested in issuing messages,
4416 they must apply to classes surrounding the object declared. */
4417 if (msg)
4418 msg = G_("default argument for template parameter for class "
4419 "enclosing %qD");
4422 return no_errors;
4425 /* Worker for push_template_decl_real, called via
4426 for_each_template_parm. DATA is really an int, indicating the
4427 level of the parameters we are interested in. If T is a template
4428 parameter of that level, return nonzero. */
4430 static int
4431 template_parm_this_level_p (tree t, void* data)
4433 int this_level = *(int *)data;
4434 int level;
4436 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4437 level = TEMPLATE_PARM_LEVEL (t);
4438 else
4439 level = TEMPLATE_TYPE_LEVEL (t);
4440 return level == this_level;
4443 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4444 parameters given by current_template_args, or reuses a
4445 previously existing one, if appropriate. Returns the DECL, or an
4446 equivalent one, if it is replaced via a call to duplicate_decls.
4448 If IS_FRIEND is true, DECL is a friend declaration. */
4450 tree
4451 push_template_decl_real (tree decl, bool is_friend)
4453 tree tmpl;
4454 tree args;
4455 tree info;
4456 tree ctx;
4457 int primary;
4458 int is_partial;
4459 int new_template_p = 0;
4460 /* True if the template is a member template, in the sense of
4461 [temp.mem]. */
4462 bool member_template_p = false;
4464 if (decl == error_mark_node || !current_template_parms)
4465 return error_mark_node;
4467 /* See if this is a partial specialization. */
4468 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4469 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4470 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4472 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4473 is_friend = true;
4475 if (is_friend)
4476 /* For a friend, we want the context of the friend function, not
4477 the type of which it is a friend. */
4478 ctx = CP_DECL_CONTEXT (decl);
4479 else if (CP_DECL_CONTEXT (decl)
4480 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4481 /* In the case of a virtual function, we want the class in which
4482 it is defined. */
4483 ctx = CP_DECL_CONTEXT (decl);
4484 else
4485 /* Otherwise, if we're currently defining some class, the DECL
4486 is assumed to be a member of the class. */
4487 ctx = current_scope ();
4489 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4490 ctx = NULL_TREE;
4492 if (!DECL_CONTEXT (decl))
4493 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4495 /* See if this is a primary template. */
4496 if (is_friend && ctx)
4497 /* A friend template that specifies a class context, i.e.
4498 template <typename T> friend void A<T>::f();
4499 is not primary. */
4500 primary = 0;
4501 else
4502 primary = template_parm_scope_p ();
4504 if (primary)
4506 if (DECL_CLASS_SCOPE_P (decl))
4507 member_template_p = true;
4508 if (TREE_CODE (decl) == TYPE_DECL
4509 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4511 error ("template class without a name");
4512 return error_mark_node;
4514 else if (TREE_CODE (decl) == FUNCTION_DECL)
4516 if (DECL_DESTRUCTOR_P (decl))
4518 /* [temp.mem]
4520 A destructor shall not be a member template. */
4521 error ("destructor %qD declared as member template", decl);
4522 return error_mark_node;
4524 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4525 && (!prototype_p (TREE_TYPE (decl))
4526 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4527 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4528 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4529 == void_list_node)))
4531 /* [basic.stc.dynamic.allocation]
4533 An allocation function can be a function
4534 template. ... Template allocation functions shall
4535 have two or more parameters. */
4536 error ("invalid template declaration of %qD", decl);
4537 return error_mark_node;
4540 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4541 && CLASS_TYPE_P (TREE_TYPE (decl)))
4542 /* OK */;
4543 else if (TREE_CODE (decl) == TYPE_DECL
4544 && TYPE_DECL_ALIAS_P (decl))
4545 /* alias-declaration */
4546 gcc_assert (!DECL_ARTIFICIAL (decl));
4547 else
4549 error ("template declaration of %q#D", decl);
4550 return error_mark_node;
4554 /* Check to see that the rules regarding the use of default
4555 arguments are not being violated. */
4556 check_default_tmpl_args (decl, current_template_parms,
4557 primary, is_partial, /*is_friend_decl=*/0);
4559 /* Ensure that there are no parameter packs in the type of this
4560 declaration that have not been expanded. */
4561 if (TREE_CODE (decl) == FUNCTION_DECL)
4563 /* Check each of the arguments individually to see if there are
4564 any bare parameter packs. */
4565 tree type = TREE_TYPE (decl);
4566 tree arg = DECL_ARGUMENTS (decl);
4567 tree argtype = TYPE_ARG_TYPES (type);
4569 while (arg && argtype)
4571 if (!FUNCTION_PARAMETER_PACK_P (arg)
4572 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4574 /* This is a PARM_DECL that contains unexpanded parameter
4575 packs. We have already complained about this in the
4576 check_for_bare_parameter_packs call, so just replace
4577 these types with ERROR_MARK_NODE. */
4578 TREE_TYPE (arg) = error_mark_node;
4579 TREE_VALUE (argtype) = error_mark_node;
4582 arg = DECL_CHAIN (arg);
4583 argtype = TREE_CHAIN (argtype);
4586 /* Check for bare parameter packs in the return type and the
4587 exception specifiers. */
4588 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4589 /* Errors were already issued, set return type to int
4590 as the frontend doesn't expect error_mark_node as
4591 the return type. */
4592 TREE_TYPE (type) = integer_type_node;
4593 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4594 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4596 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4597 && TYPE_DECL_ALIAS_P (decl))
4598 ? DECL_ORIGINAL_TYPE (decl)
4599 : TREE_TYPE (decl)))
4601 TREE_TYPE (decl) = error_mark_node;
4602 return error_mark_node;
4605 if (is_partial)
4606 return process_partial_specialization (decl);
4608 args = current_template_args ();
4610 if (!ctx
4611 || TREE_CODE (ctx) == FUNCTION_DECL
4612 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4613 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4615 if (DECL_LANG_SPECIFIC (decl)
4616 && DECL_TEMPLATE_INFO (decl)
4617 && DECL_TI_TEMPLATE (decl))
4618 tmpl = DECL_TI_TEMPLATE (decl);
4619 /* If DECL is a TYPE_DECL for a class-template, then there won't
4620 be DECL_LANG_SPECIFIC. The information equivalent to
4621 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4622 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4623 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4624 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4626 /* Since a template declaration already existed for this
4627 class-type, we must be redeclaring it here. Make sure
4628 that the redeclaration is valid. */
4629 redeclare_class_template (TREE_TYPE (decl),
4630 current_template_parms);
4631 /* We don't need to create a new TEMPLATE_DECL; just use the
4632 one we already had. */
4633 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4635 else
4637 tmpl = build_template_decl (decl, current_template_parms,
4638 member_template_p);
4639 new_template_p = 1;
4641 if (DECL_LANG_SPECIFIC (decl)
4642 && DECL_TEMPLATE_SPECIALIZATION (decl))
4644 /* A specialization of a member template of a template
4645 class. */
4646 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4647 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4648 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4652 else
4654 tree a, t, current, parms;
4655 int i;
4656 tree tinfo = get_template_info (decl);
4658 if (!tinfo)
4660 error ("template definition of non-template %q#D", decl);
4661 return error_mark_node;
4664 tmpl = TI_TEMPLATE (tinfo);
4666 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4667 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4668 && DECL_TEMPLATE_SPECIALIZATION (decl)
4669 && DECL_MEMBER_TEMPLATE_P (tmpl))
4671 tree new_tmpl;
4673 /* The declaration is a specialization of a member
4674 template, declared outside the class. Therefore, the
4675 innermost template arguments will be NULL, so we
4676 replace them with the arguments determined by the
4677 earlier call to check_explicit_specialization. */
4678 args = DECL_TI_ARGS (decl);
4680 new_tmpl
4681 = build_template_decl (decl, current_template_parms,
4682 member_template_p);
4683 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4684 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4685 DECL_TI_TEMPLATE (decl) = new_tmpl;
4686 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4687 DECL_TEMPLATE_INFO (new_tmpl)
4688 = build_template_info (tmpl, args);
4690 register_specialization (new_tmpl,
4691 most_general_template (tmpl),
4692 args,
4693 is_friend, 0);
4694 return decl;
4697 /* Make sure the template headers we got make sense. */
4699 parms = DECL_TEMPLATE_PARMS (tmpl);
4700 i = TMPL_PARMS_DEPTH (parms);
4701 if (TMPL_ARGS_DEPTH (args) != i)
4703 error ("expected %d levels of template parms for %q#D, got %d",
4704 i, decl, TMPL_ARGS_DEPTH (args));
4706 else
4707 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4709 a = TMPL_ARGS_LEVEL (args, i);
4710 t = INNERMOST_TEMPLATE_PARMS (parms);
4712 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4714 if (current == decl)
4715 error ("got %d template parameters for %q#D",
4716 TREE_VEC_LENGTH (a), decl);
4717 else
4718 error ("got %d template parameters for %q#T",
4719 TREE_VEC_LENGTH (a), current);
4720 error (" but %d required", TREE_VEC_LENGTH (t));
4721 return error_mark_node;
4724 if (current == decl)
4725 current = ctx;
4726 else if (current == NULL_TREE)
4727 /* Can happen in erroneous input. */
4728 break;
4729 else
4730 current = (TYPE_P (current)
4731 ? TYPE_CONTEXT (current)
4732 : DECL_CONTEXT (current));
4735 /* Check that the parms are used in the appropriate qualifying scopes
4736 in the declarator. */
4737 if (!comp_template_args
4738 (TI_ARGS (tinfo),
4739 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4741 error ("\
4742 template arguments to %qD do not match original template %qD",
4743 decl, DECL_TEMPLATE_RESULT (tmpl));
4744 if (!uses_template_parms (TI_ARGS (tinfo)))
4745 inform (input_location, "use template<> for an explicit specialization");
4746 /* Avoid crash in import_export_decl. */
4747 DECL_INTERFACE_KNOWN (decl) = 1;
4748 return error_mark_node;
4752 DECL_TEMPLATE_RESULT (tmpl) = decl;
4753 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4755 /* Push template declarations for global functions and types. Note
4756 that we do not try to push a global template friend declared in a
4757 template class; such a thing may well depend on the template
4758 parameters of the class. */
4759 if (new_template_p && !ctx
4760 && !(is_friend && template_class_depth (current_class_type) > 0))
4762 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4763 if (tmpl == error_mark_node)
4764 return error_mark_node;
4766 /* Hide template friend classes that haven't been declared yet. */
4767 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4769 DECL_ANTICIPATED (tmpl) = 1;
4770 DECL_FRIEND_P (tmpl) = 1;
4774 if (primary)
4776 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4777 int i;
4779 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4780 if (DECL_CONV_FN_P (tmpl))
4782 int depth = TMPL_PARMS_DEPTH (parms);
4784 /* It is a conversion operator. See if the type converted to
4785 depends on innermost template operands. */
4787 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4788 depth))
4789 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4792 /* Give template template parms a DECL_CONTEXT of the template
4793 for which they are a parameter. */
4794 parms = INNERMOST_TEMPLATE_PARMS (parms);
4795 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4797 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4798 if (TREE_CODE (parm) == TEMPLATE_DECL)
4799 DECL_CONTEXT (parm) = tmpl;
4803 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4804 back to its most general template. If TMPL is a specialization,
4805 ARGS may only have the innermost set of arguments. Add the missing
4806 argument levels if necessary. */
4807 if (DECL_TEMPLATE_INFO (tmpl))
4808 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4810 info = build_template_info (tmpl, args);
4812 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4813 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4814 else
4816 if (primary && !DECL_LANG_SPECIFIC (decl))
4817 retrofit_lang_decl (decl);
4818 if (DECL_LANG_SPECIFIC (decl))
4819 DECL_TEMPLATE_INFO (decl) = info;
4822 return DECL_TEMPLATE_RESULT (tmpl);
4825 tree
4826 push_template_decl (tree decl)
4828 return push_template_decl_real (decl, false);
4831 /* Called when a class template TYPE is redeclared with the indicated
4832 template PARMS, e.g.:
4834 template <class T> struct S;
4835 template <class T> struct S {}; */
4837 bool
4838 redeclare_class_template (tree type, tree parms)
4840 tree tmpl;
4841 tree tmpl_parms;
4842 int i;
4844 if (!TYPE_TEMPLATE_INFO (type))
4846 error ("%qT is not a template type", type);
4847 return false;
4850 tmpl = TYPE_TI_TEMPLATE (type);
4851 if (!PRIMARY_TEMPLATE_P (tmpl))
4852 /* The type is nested in some template class. Nothing to worry
4853 about here; there are no new template parameters for the nested
4854 type. */
4855 return true;
4857 if (!parms)
4859 error ("template specifiers not specified in declaration of %qD",
4860 tmpl);
4861 return false;
4864 parms = INNERMOST_TEMPLATE_PARMS (parms);
4865 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4867 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4869 error_n (input_location, TREE_VEC_LENGTH (parms),
4870 "redeclared with %d template parameter",
4871 "redeclared with %d template parameters",
4872 TREE_VEC_LENGTH (parms));
4873 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4874 "previous declaration %q+D used %d template parameter",
4875 "previous declaration %q+D used %d template parameters",
4876 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4877 return false;
4880 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4882 tree tmpl_parm;
4883 tree parm;
4884 tree tmpl_default;
4885 tree parm_default;
4887 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4888 || TREE_VEC_ELT (parms, i) == error_mark_node)
4889 continue;
4891 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4892 if (tmpl_parm == error_mark_node)
4893 return false;
4895 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4896 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4897 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4899 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4900 TEMPLATE_DECL. */
4901 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4902 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4903 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4904 || (TREE_CODE (tmpl_parm) != PARM_DECL
4905 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4906 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4907 || (TREE_CODE (tmpl_parm) == PARM_DECL
4908 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4909 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4911 error ("template parameter %q+#D", tmpl_parm);
4912 error ("redeclared here as %q#D", parm);
4913 return false;
4916 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4918 /* We have in [temp.param]:
4920 A template-parameter may not be given default arguments
4921 by two different declarations in the same scope. */
4922 error_at (input_location, "redefinition of default argument for %q#D", parm);
4923 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4924 "original definition appeared here");
4925 return false;
4928 if (parm_default != NULL_TREE)
4929 /* Update the previous template parameters (which are the ones
4930 that will really count) with the new default value. */
4931 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4932 else if (tmpl_default != NULL_TREE)
4933 /* Update the new parameters, too; they'll be used as the
4934 parameters for any members. */
4935 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4938 return true;
4941 /* Simplify EXPR if it is a non-dependent expression. Returns the
4942 (possibly simplified) expression. */
4944 static tree
4945 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
4947 if (expr == NULL_TREE)
4948 return NULL_TREE;
4950 /* If we're in a template, but EXPR isn't value dependent, simplify
4951 it. We're supposed to treat:
4953 template <typename T> void f(T[1 + 1]);
4954 template <typename T> void f(T[2]);
4956 as two declarations of the same function, for example. */
4957 if (processing_template_decl
4958 && !type_dependent_expression_p (expr)
4959 && potential_constant_expression (expr)
4960 && !value_dependent_expression_p (expr))
4962 HOST_WIDE_INT saved_processing_template_decl;
4964 saved_processing_template_decl = processing_template_decl;
4965 processing_template_decl = 0;
4966 expr = tsubst_copy_and_build (expr,
4967 /*args=*/NULL_TREE,
4968 complain,
4969 /*in_decl=*/NULL_TREE,
4970 /*function_p=*/false,
4971 /*integral_constant_expression_p=*/true);
4972 processing_template_decl = saved_processing_template_decl;
4974 return expr;
4977 tree
4978 fold_non_dependent_expr (tree expr)
4980 return fold_non_dependent_expr_sfinae (expr, tf_error);
4983 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
4984 template declaration, or a TYPE_DECL for an alias declaration. */
4986 bool
4987 alias_type_or_template_p (tree t)
4989 if (t == NULL_TREE)
4990 return false;
4991 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
4992 || (TYPE_P (t)
4993 && TYPE_NAME (t)
4994 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
4995 || DECL_ALIAS_TEMPLATE_P (t));
4998 /* Return TRUE iff is a specialization of an alias template. */
5000 bool
5001 alias_template_specialization_p (tree t)
5003 if (t == NULL_TREE)
5004 return false;
5005 return (primary_template_instantiation_p (t)
5006 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5009 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5010 must be a function or a pointer-to-function type, as specified
5011 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5012 and check that the resulting function has external linkage. */
5014 static tree
5015 convert_nontype_argument_function (tree type, tree expr)
5017 tree fns = expr;
5018 tree fn, fn_no_ptr;
5019 linkage_kind linkage;
5021 fn = instantiate_type (type, fns, tf_none);
5022 if (fn == error_mark_node)
5023 return error_mark_node;
5025 fn_no_ptr = fn;
5026 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5027 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5028 if (BASELINK_P (fn_no_ptr))
5029 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5031 /* [temp.arg.nontype]/1
5033 A template-argument for a non-type, non-template template-parameter
5034 shall be one of:
5035 [...]
5036 -- the address of an object or function with external [C++11: or
5037 internal] linkage. */
5038 linkage = decl_linkage (fn_no_ptr);
5039 if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
5041 if (cxx_dialect >= cxx0x)
5042 error ("%qE is not a valid template argument for type %qT "
5043 "because %qD has no linkage",
5044 expr, type, fn_no_ptr);
5045 else
5046 error ("%qE is not a valid template argument for type %qT "
5047 "because %qD does not have external linkage",
5048 expr, type, fn_no_ptr);
5049 return NULL_TREE;
5052 return fn;
5055 /* Subroutine of convert_nontype_argument.
5056 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5057 Emit an error otherwise. */
5059 static bool
5060 check_valid_ptrmem_cst_expr (tree type, tree expr,
5061 tsubst_flags_t complain)
5063 STRIP_NOPS (expr);
5064 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5065 return true;
5066 if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr))
5067 return true;
5068 if (complain & tf_error)
5070 error ("%qE is not a valid template argument for type %qT",
5071 expr, type);
5072 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5074 return false;
5077 /* Returns TRUE iff the address of OP is value-dependent.
5079 14.6.2.4 [temp.dep.temp]:
5080 A non-integral non-type template-argument is dependent if its type is
5081 dependent or it has either of the following forms
5082 qualified-id
5083 & qualified-id
5084 and contains a nested-name-specifier which specifies a class-name that
5085 names a dependent type.
5087 We generalize this to just say that the address of a member of a
5088 dependent class is value-dependent; the above doesn't cover the
5089 address of a static data member named with an unqualified-id. */
5091 static bool
5092 has_value_dependent_address (tree op)
5094 /* We could use get_inner_reference here, but there's no need;
5095 this is only relevant for template non-type arguments, which
5096 can only be expressed as &id-expression. */
5097 if (DECL_P (op))
5099 tree ctx = CP_DECL_CONTEXT (op);
5100 if (TYPE_P (ctx) && dependent_type_p (ctx))
5101 return true;
5104 return false;
5107 /* The next set of functions are used for providing helpful explanatory
5108 diagnostics for failed overload resolution. Their messages should be
5109 indented by two spaces for consistency with the messages in
5110 call.c */
5112 static int
5113 unify_success (bool explain_p ATTRIBUTE_UNUSED)
5115 return 0;
5118 static int
5119 unify_parameter_deduction_failure (bool explain_p, tree parm)
5121 if (explain_p)
5122 inform (input_location,
5123 " couldn't deduce template parameter %qD", parm);
5124 return 1;
5127 static int
5128 unify_invalid (bool explain_p ATTRIBUTE_UNUSED)
5130 return 1;
5133 static int
5134 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5136 if (explain_p)
5137 inform (input_location,
5138 " types %qT and %qT have incompatible cv-qualifiers",
5139 parm, arg);
5140 return 1;
5143 static int
5144 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5146 if (explain_p)
5147 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5148 return 1;
5151 static int
5152 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5154 if (explain_p)
5155 inform (input_location,
5156 " template parameter %qD is not a parameter pack, but "
5157 "argument %qD is",
5158 parm, arg);
5159 return 1;
5162 static int
5163 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5165 if (explain_p)
5166 inform (input_location,
5167 " template argument %qE does not match "
5168 "pointer-to-member constant %qE",
5169 arg, parm);
5170 return 1;
5173 static int
5174 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5176 if (explain_p)
5177 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5178 return 1;
5181 static int
5182 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5184 if (explain_p)
5185 inform (input_location,
5186 " inconsistent parameter pack deduction with %qT and %qT",
5187 old_arg, new_arg);
5188 return 1;
5191 static int
5192 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5194 if (explain_p)
5196 if (TYPE_P (parm))
5197 inform (input_location,
5198 " deduced conflicting types for parameter %qT (%qT and %qT)",
5199 parm, first, second);
5200 else
5201 inform (input_location,
5202 " deduced conflicting values for non-type parameter "
5203 "%qE (%qE and %qE)", parm, first, second);
5205 return 1;
5208 static int
5209 unify_vla_arg (bool explain_p, tree arg)
5211 if (explain_p)
5212 inform (input_location,
5213 " variable-sized array type %qT is not "
5214 "a valid template argument",
5215 arg);
5216 return 1;
5219 static int
5220 unify_method_type_error (bool explain_p, tree arg)
5222 if (explain_p)
5223 inform (input_location,
5224 " member function type %qT is not a valid template argument",
5225 arg);
5226 return 1;
5229 static int
5230 unify_arity (bool explain_p, int have, int wanted)
5232 if (explain_p)
5233 inform_n (input_location, wanted,
5234 " candidate expects %d argument, %d provided",
5235 " candidate expects %d arguments, %d provided",
5236 wanted, have);
5237 return 1;
5240 static int
5241 unify_too_many_arguments (bool explain_p, int have, int wanted)
5243 return unify_arity (explain_p, have, wanted);
5246 static int
5247 unify_too_few_arguments (bool explain_p, int have, int wanted)
5249 return unify_arity (explain_p, have, wanted);
5252 static int
5253 unify_arg_conversion (bool explain_p, tree to_type,
5254 tree from_type, tree arg)
5256 if (explain_p)
5257 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5258 arg, from_type, to_type);
5259 return 1;
5262 static int
5263 unify_no_common_base (bool explain_p, enum template_base_result r,
5264 tree parm, tree arg)
5266 if (explain_p)
5267 switch (r)
5269 case tbr_ambiguous_baseclass:
5270 inform (input_location, " %qT is an ambiguous base class of %qT",
5271 arg, parm);
5272 break;
5273 default:
5274 inform (input_location, " %qT is not derived from %qT", arg, parm);
5275 break;
5277 return 1;
5280 static int
5281 unify_inconsistent_template_template_parameters (bool explain_p)
5283 if (explain_p)
5284 inform (input_location,
5285 " template parameters of a template template argument are "
5286 "inconsistent with other deduced template arguments");
5287 return 1;
5290 static int
5291 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5293 if (explain_p)
5294 inform (input_location,
5295 " can't deduce a template for %qT from non-template type %qT",
5296 parm, arg);
5297 return 1;
5300 static int
5301 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5303 if (explain_p)
5304 inform (input_location,
5305 " template argument %qE does not match %qD", arg, parm);
5306 return 1;
5309 static int
5310 unify_overload_resolution_failure (bool explain_p, tree arg)
5312 if (explain_p)
5313 inform (input_location,
5314 " could not resolve address from overloaded function %qE",
5315 arg);
5316 return 1;
5319 /* Attempt to convert the non-type template parameter EXPR to the
5320 indicated TYPE. If the conversion is successful, return the
5321 converted value. If the conversion is unsuccessful, return
5322 NULL_TREE if we issued an error message, or error_mark_node if we
5323 did not. We issue error messages for out-and-out bad template
5324 parameters, but not simply because the conversion failed, since we
5325 might be just trying to do argument deduction. Both TYPE and EXPR
5326 must be non-dependent.
5328 The conversion follows the special rules described in
5329 [temp.arg.nontype], and it is much more strict than an implicit
5330 conversion.
5332 This function is called twice for each template argument (see
5333 lookup_template_class for a more accurate description of this
5334 problem). This means that we need to handle expressions which
5335 are not valid in a C++ source, but can be created from the
5336 first call (for instance, casts to perform conversions). These
5337 hacks can go away after we fix the double coercion problem. */
5339 static tree
5340 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5342 tree expr_type;
5344 /* Detect immediately string literals as invalid non-type argument.
5345 This special-case is not needed for correctness (we would easily
5346 catch this later), but only to provide better diagnostic for this
5347 common user mistake. As suggested by DR 100, we do not mention
5348 linkage issues in the diagnostic as this is not the point. */
5349 /* FIXME we're making this OK. */
5350 if (TREE_CODE (expr) == STRING_CST)
5352 if (complain & tf_error)
5353 error ("%qE is not a valid template argument for type %qT "
5354 "because string literals can never be used in this context",
5355 expr, type);
5356 return NULL_TREE;
5359 /* Add the ADDR_EXPR now for the benefit of
5360 value_dependent_expression_p. */
5361 if (TYPE_PTROBV_P (type)
5362 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5364 expr = decay_conversion (expr, complain);
5365 if (expr == error_mark_node)
5366 return error_mark_node;
5369 /* If we are in a template, EXPR may be non-dependent, but still
5370 have a syntactic, rather than semantic, form. For example, EXPR
5371 might be a SCOPE_REF, rather than the VAR_DECL to which the
5372 SCOPE_REF refers. Preserving the qualifying scope is necessary
5373 so that access checking can be performed when the template is
5374 instantiated -- but here we need the resolved form so that we can
5375 convert the argument. */
5376 if (TYPE_REF_OBJ_P (type)
5377 && has_value_dependent_address (expr))
5378 /* If we want the address and it's value-dependent, don't fold. */;
5379 else if (!type_unknown_p (expr))
5380 expr = fold_non_dependent_expr_sfinae (expr, complain);
5381 if (error_operand_p (expr))
5382 return error_mark_node;
5383 expr_type = TREE_TYPE (expr);
5384 if (TREE_CODE (type) == REFERENCE_TYPE)
5385 expr = mark_lvalue_use (expr);
5386 else
5387 expr = mark_rvalue_use (expr);
5389 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5390 to a non-type argument of "nullptr". */
5391 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5392 expr = convert (type, expr);
5394 /* In C++11, integral or enumeration non-type template arguments can be
5395 arbitrary constant expressions. Pointer and pointer to
5396 member arguments can be general constant expressions that evaluate
5397 to a null value, but otherwise still need to be of a specific form. */
5398 if (cxx_dialect >= cxx0x)
5400 if (TREE_CODE (expr) == PTRMEM_CST)
5401 /* A PTRMEM_CST is already constant, and a valid template
5402 argument for a parameter of pointer to member type, we just want
5403 to leave it in that form rather than lower it to a
5404 CONSTRUCTOR. */;
5405 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5406 expr = maybe_constant_value (expr);
5407 else if (TYPE_PTR_OR_PTRMEM_P (type))
5409 tree folded = maybe_constant_value (expr);
5410 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5411 : null_member_pointer_value_p (folded))
5412 expr = folded;
5416 /* HACK: Due to double coercion, we can get a
5417 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5418 which is the tree that we built on the first call (see
5419 below when coercing to reference to object or to reference to
5420 function). We just strip everything and get to the arg.
5421 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5422 for examples. */
5423 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5425 tree probe_type, probe = expr;
5426 if (REFERENCE_REF_P (probe))
5427 probe = TREE_OPERAND (probe, 0);
5428 probe_type = TREE_TYPE (probe);
5429 if (TREE_CODE (probe) == NOP_EXPR)
5431 /* ??? Maybe we could use convert_from_reference here, but we
5432 would need to relax its constraints because the NOP_EXPR
5433 could actually change the type to something more cv-qualified,
5434 and this is not folded by convert_from_reference. */
5435 tree addr = TREE_OPERAND (probe, 0);
5436 gcc_assert (TREE_CODE (probe_type) == REFERENCE_TYPE);
5437 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5438 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5439 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5440 (TREE_TYPE (probe_type),
5441 TREE_TYPE (TREE_TYPE (addr))));
5443 expr = TREE_OPERAND (addr, 0);
5444 expr_type = TREE_TYPE (expr);
5448 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5449 parameter is a pointer to object, through decay and
5450 qualification conversion. Let's strip everything. */
5451 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5453 STRIP_NOPS (expr);
5454 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5455 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5456 /* Skip the ADDR_EXPR only if it is part of the decay for
5457 an array. Otherwise, it is part of the original argument
5458 in the source code. */
5459 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5460 expr = TREE_OPERAND (expr, 0);
5461 expr_type = TREE_TYPE (expr);
5464 /* [temp.arg.nontype]/5, bullet 1
5466 For a non-type template-parameter of integral or enumeration type,
5467 integral promotions (_conv.prom_) and integral conversions
5468 (_conv.integral_) are applied. */
5469 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5471 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5472 t = maybe_constant_value (t);
5473 if (t != error_mark_node)
5474 expr = t;
5476 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5477 return error_mark_node;
5479 /* Notice that there are constant expressions like '4 % 0' which
5480 do not fold into integer constants. */
5481 if (TREE_CODE (expr) != INTEGER_CST)
5483 if (complain & tf_error)
5485 int errs = errorcount, warns = warningcount;
5486 if (processing_template_decl
5487 && !require_potential_constant_expression (expr))
5488 return NULL_TREE;
5489 expr = cxx_constant_value (expr);
5490 if (errorcount > errs || warningcount > warns)
5491 inform (EXPR_LOC_OR_HERE (expr),
5492 "in template argument for type %qT ", type);
5493 if (expr == error_mark_node)
5494 return NULL_TREE;
5495 /* else cxx_constant_value complained but gave us
5496 a real constant, so go ahead. */
5497 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5499 else
5500 return NULL_TREE;
5503 /* [temp.arg.nontype]/5, bullet 2
5505 For a non-type template-parameter of type pointer to object,
5506 qualification conversions (_conv.qual_) and the array-to-pointer
5507 conversion (_conv.array_) are applied. */
5508 else if (TYPE_PTROBV_P (type))
5510 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5512 A template-argument for a non-type, non-template template-parameter
5513 shall be one of: [...]
5515 -- the name of a non-type template-parameter;
5516 -- the address of an object or function with external linkage, [...]
5517 expressed as "& id-expression" where the & is optional if the name
5518 refers to a function or array, or if the corresponding
5519 template-parameter is a reference.
5521 Here, we do not care about functions, as they are invalid anyway
5522 for a parameter of type pointer-to-object. */
5524 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5525 /* Non-type template parameters are OK. */
5527 else if (cxx_dialect >= cxx0x && integer_zerop (expr))
5528 /* Null pointer values are OK in C++11. */;
5529 else if (TREE_CODE (expr) != ADDR_EXPR
5530 && TREE_CODE (expr_type) != ARRAY_TYPE)
5532 if (TREE_CODE (expr) == VAR_DECL)
5534 error ("%qD is not a valid template argument "
5535 "because %qD is a variable, not the address of "
5536 "a variable",
5537 expr, expr);
5538 return NULL_TREE;
5540 /* Other values, like integer constants, might be valid
5541 non-type arguments of some other type. */
5542 return error_mark_node;
5544 else
5546 tree decl;
5548 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5549 ? TREE_OPERAND (expr, 0) : expr);
5550 if (TREE_CODE (decl) != VAR_DECL)
5552 error ("%qE is not a valid template argument of type %qT "
5553 "because %qE is not a variable",
5554 expr, type, decl);
5555 return NULL_TREE;
5557 else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
5559 error ("%qE is not a valid template argument of type %qT "
5560 "because %qD does not have external linkage",
5561 expr, type, decl);
5562 return NULL_TREE;
5564 else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
5566 error ("%qE is not a valid template argument of type %qT "
5567 "because %qD has no linkage",
5568 expr, type, decl);
5569 return NULL_TREE;
5573 expr = decay_conversion (expr, complain);
5574 if (expr == error_mark_node)
5575 return error_mark_node;
5577 expr = perform_qualification_conversions (type, expr);
5578 if (expr == error_mark_node)
5579 return error_mark_node;
5581 /* [temp.arg.nontype]/5, bullet 3
5583 For a non-type template-parameter of type reference to object, no
5584 conversions apply. The type referred to by the reference may be more
5585 cv-qualified than the (otherwise identical) type of the
5586 template-argument. The template-parameter is bound directly to the
5587 template-argument, which must be an lvalue. */
5588 else if (TYPE_REF_OBJ_P (type))
5590 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5591 expr_type))
5592 return error_mark_node;
5594 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5596 error ("%qE is not a valid template argument for type %qT "
5597 "because of conflicts in cv-qualification", expr, type);
5598 return NULL_TREE;
5601 if (!real_lvalue_p (expr))
5603 error ("%qE is not a valid template argument for type %qT "
5604 "because it is not an lvalue", expr, type);
5605 return NULL_TREE;
5608 /* [temp.arg.nontype]/1
5610 A template-argument for a non-type, non-template template-parameter
5611 shall be one of: [...]
5613 -- the address of an object or function with external linkage. */
5614 if (TREE_CODE (expr) == INDIRECT_REF
5615 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5617 expr = TREE_OPERAND (expr, 0);
5618 if (DECL_P (expr))
5620 error ("%q#D is not a valid template argument for type %qT "
5621 "because a reference variable does not have a constant "
5622 "address", expr, type);
5623 return NULL_TREE;
5627 if (!DECL_P (expr))
5629 error ("%qE is not a valid template argument for type %qT "
5630 "because it is not an object with external linkage",
5631 expr, type);
5632 return NULL_TREE;
5635 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5637 error ("%qE is not a valid template argument for type %qT "
5638 "because object %qD has not external linkage",
5639 expr, type, expr);
5640 return NULL_TREE;
5643 expr = build_nop (type, build_address (expr));
5645 /* [temp.arg.nontype]/5, bullet 4
5647 For a non-type template-parameter of type pointer to function, only
5648 the function-to-pointer conversion (_conv.func_) is applied. If the
5649 template-argument represents a set of overloaded functions (or a
5650 pointer to such), the matching function is selected from the set
5651 (_over.over_). */
5652 else if (TYPE_PTRFN_P (type))
5654 /* If the argument is a template-id, we might not have enough
5655 context information to decay the pointer. */
5656 if (!type_unknown_p (expr_type))
5658 expr = decay_conversion (expr, complain);
5659 if (expr == error_mark_node)
5660 return error_mark_node;
5663 if (cxx_dialect >= cxx0x && integer_zerop (expr))
5664 /* Null pointer values are OK in C++11. */
5665 return perform_qualification_conversions (type, expr);
5667 expr = convert_nontype_argument_function (type, expr);
5668 if (!expr || expr == error_mark_node)
5669 return expr;
5671 if (TREE_CODE (expr) != ADDR_EXPR)
5673 error ("%qE is not a valid template argument for type %qT", expr, type);
5674 error ("it must be the address of a function with external linkage");
5675 return NULL_TREE;
5678 /* [temp.arg.nontype]/5, bullet 5
5680 For a non-type template-parameter of type reference to function, no
5681 conversions apply. If the template-argument represents a set of
5682 overloaded functions, the matching function is selected from the set
5683 (_over.over_). */
5684 else if (TYPE_REFFN_P (type))
5686 if (TREE_CODE (expr) == ADDR_EXPR)
5688 error ("%qE is not a valid template argument for type %qT "
5689 "because it is a pointer", expr, type);
5690 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5691 return NULL_TREE;
5694 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5695 if (!expr || expr == error_mark_node)
5696 return expr;
5698 expr = build_nop (type, build_address (expr));
5700 /* [temp.arg.nontype]/5, bullet 6
5702 For a non-type template-parameter of type pointer to member function,
5703 no conversions apply. If the template-argument represents a set of
5704 overloaded member functions, the matching member function is selected
5705 from the set (_over.over_). */
5706 else if (TYPE_PTRMEMFUNC_P (type))
5708 expr = instantiate_type (type, expr, tf_none);
5709 if (expr == error_mark_node)
5710 return error_mark_node;
5712 /* [temp.arg.nontype] bullet 1 says the pointer to member
5713 expression must be a pointer-to-member constant. */
5714 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5715 return error_mark_node;
5717 /* There is no way to disable standard conversions in
5718 resolve_address_of_overloaded_function (called by
5719 instantiate_type). It is possible that the call succeeded by
5720 converting &B::I to &D::I (where B is a base of D), so we need
5721 to reject this conversion here.
5723 Actually, even if there was a way to disable standard conversions,
5724 it would still be better to reject them here so that we can
5725 provide a superior diagnostic. */
5726 if (!same_type_p (TREE_TYPE (expr), type))
5728 error ("%qE is not a valid template argument for type %qT "
5729 "because it is of type %qT", expr, type,
5730 TREE_TYPE (expr));
5731 /* If we are just one standard conversion off, explain. */
5732 if (can_convert (type, TREE_TYPE (expr), complain))
5733 inform (input_location,
5734 "standard conversions are not allowed in this context");
5735 return NULL_TREE;
5738 /* [temp.arg.nontype]/5, bullet 7
5740 For a non-type template-parameter of type pointer to data member,
5741 qualification conversions (_conv.qual_) are applied. */
5742 else if (TYPE_PTRDATAMEM_P (type))
5744 /* [temp.arg.nontype] bullet 1 says the pointer to member
5745 expression must be a pointer-to-member constant. */
5746 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5747 return error_mark_node;
5749 expr = perform_qualification_conversions (type, expr);
5750 if (expr == error_mark_node)
5751 return expr;
5753 else if (NULLPTR_TYPE_P (type))
5755 if (expr != nullptr_node)
5757 error ("%qE is not a valid template argument for type %qT "
5758 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5759 return NULL_TREE;
5761 return expr;
5763 /* A template non-type parameter must be one of the above. */
5764 else
5765 gcc_unreachable ();
5767 /* Sanity check: did we actually convert the argument to the
5768 right type? */
5769 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5770 (type, TREE_TYPE (expr)));
5771 return expr;
5774 /* Subroutine of coerce_template_template_parms, which returns 1 if
5775 PARM_PARM and ARG_PARM match using the rule for the template
5776 parameters of template template parameters. Both PARM and ARG are
5777 template parameters; the rest of the arguments are the same as for
5778 coerce_template_template_parms.
5780 static int
5781 coerce_template_template_parm (tree parm,
5782 tree arg,
5783 tsubst_flags_t complain,
5784 tree in_decl,
5785 tree outer_args)
5787 if (arg == NULL_TREE || arg == error_mark_node
5788 || parm == NULL_TREE || parm == error_mark_node)
5789 return 0;
5791 if (TREE_CODE (arg) != TREE_CODE (parm))
5792 return 0;
5794 switch (TREE_CODE (parm))
5796 case TEMPLATE_DECL:
5797 /* We encounter instantiations of templates like
5798 template <template <template <class> class> class TT>
5799 class C; */
5801 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5802 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5804 if (!coerce_template_template_parms
5805 (parmparm, argparm, complain, in_decl, outer_args))
5806 return 0;
5808 /* Fall through. */
5810 case TYPE_DECL:
5811 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5812 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5813 /* Argument is a parameter pack but parameter is not. */
5814 return 0;
5815 break;
5817 case PARM_DECL:
5818 /* The tsubst call is used to handle cases such as
5820 template <int> class C {};
5821 template <class T, template <T> class TT> class D {};
5822 D<int, C> d;
5824 i.e. the parameter list of TT depends on earlier parameters. */
5825 if (!uses_template_parms (TREE_TYPE (arg))
5826 && !same_type_p
5827 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5828 TREE_TYPE (arg)))
5829 return 0;
5831 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5832 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5833 /* Argument is a parameter pack but parameter is not. */
5834 return 0;
5836 break;
5838 default:
5839 gcc_unreachable ();
5842 return 1;
5846 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5847 template template parameters. Both PARM_PARMS and ARG_PARMS are
5848 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5849 or PARM_DECL.
5851 Consider the example:
5852 template <class T> class A;
5853 template<template <class U> class TT> class B;
5855 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5856 the parameters to A, and OUTER_ARGS contains A. */
5858 static int
5859 coerce_template_template_parms (tree parm_parms,
5860 tree arg_parms,
5861 tsubst_flags_t complain,
5862 tree in_decl,
5863 tree outer_args)
5865 int nparms, nargs, i;
5866 tree parm, arg;
5867 int variadic_p = 0;
5869 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5870 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5872 nparms = TREE_VEC_LENGTH (parm_parms);
5873 nargs = TREE_VEC_LENGTH (arg_parms);
5875 /* Determine whether we have a parameter pack at the end of the
5876 template template parameter's template parameter list. */
5877 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5879 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5881 if (parm == error_mark_node)
5882 return 0;
5884 switch (TREE_CODE (parm))
5886 case TEMPLATE_DECL:
5887 case TYPE_DECL:
5888 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5889 variadic_p = 1;
5890 break;
5892 case PARM_DECL:
5893 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5894 variadic_p = 1;
5895 break;
5897 default:
5898 gcc_unreachable ();
5902 if (nargs != nparms
5903 && !(variadic_p && nargs >= nparms - 1))
5904 return 0;
5906 /* Check all of the template parameters except the parameter pack at
5907 the end (if any). */
5908 for (i = 0; i < nparms - variadic_p; ++i)
5910 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5911 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5912 continue;
5914 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5915 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5917 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5918 outer_args))
5919 return 0;
5923 if (variadic_p)
5925 /* Check each of the template parameters in the template
5926 argument against the template parameter pack at the end of
5927 the template template parameter. */
5928 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5929 return 0;
5931 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5933 for (; i < nargs; ++i)
5935 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5936 continue;
5938 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5940 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5941 outer_args))
5942 return 0;
5946 return 1;
5949 /* Verifies that the deduced template arguments (in TARGS) for the
5950 template template parameters (in TPARMS) represent valid bindings,
5951 by comparing the template parameter list of each template argument
5952 to the template parameter list of its corresponding template
5953 template parameter, in accordance with DR150. This
5954 routine can only be called after all template arguments have been
5955 deduced. It will return TRUE if all of the template template
5956 parameter bindings are okay, FALSE otherwise. */
5957 bool
5958 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5960 int i, ntparms = TREE_VEC_LENGTH (tparms);
5961 bool ret = true;
5963 /* We're dealing with template parms in this process. */
5964 ++processing_template_decl;
5966 targs = INNERMOST_TEMPLATE_ARGS (targs);
5968 for (i = 0; i < ntparms; ++i)
5970 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5971 tree targ = TREE_VEC_ELT (targs, i);
5973 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5975 tree packed_args = NULL_TREE;
5976 int idx, len = 1;
5978 if (ARGUMENT_PACK_P (targ))
5980 /* Look inside the argument pack. */
5981 packed_args = ARGUMENT_PACK_ARGS (targ);
5982 len = TREE_VEC_LENGTH (packed_args);
5985 for (idx = 0; idx < len; ++idx)
5987 tree targ_parms = NULL_TREE;
5989 if (packed_args)
5990 /* Extract the next argument from the argument
5991 pack. */
5992 targ = TREE_VEC_ELT (packed_args, idx);
5994 if (PACK_EXPANSION_P (targ))
5995 /* Look at the pattern of the pack expansion. */
5996 targ = PACK_EXPANSION_PATTERN (targ);
5998 /* Extract the template parameters from the template
5999 argument. */
6000 if (TREE_CODE (targ) == TEMPLATE_DECL)
6001 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6002 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6003 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6005 /* Verify that we can coerce the template template
6006 parameters from the template argument to the template
6007 parameter. This requires an exact match. */
6008 if (targ_parms
6009 && !coerce_template_template_parms
6010 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6011 targ_parms,
6012 tf_none,
6013 tparm,
6014 targs))
6016 ret = false;
6017 goto out;
6023 out:
6025 --processing_template_decl;
6026 return ret;
6029 /* Since type attributes aren't mangled, we need to strip them from
6030 template type arguments. */
6032 static tree
6033 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6035 tree mv;
6036 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6037 return arg;
6038 mv = TYPE_MAIN_VARIANT (arg);
6039 arg = strip_typedefs (arg);
6040 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6041 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6043 if (complain & tf_warning)
6044 warning (0, "ignoring attributes on template argument %qT", arg);
6045 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6046 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6048 return arg;
6051 /* Convert the indicated template ARG as necessary to match the
6052 indicated template PARM. Returns the converted ARG, or
6053 error_mark_node if the conversion was unsuccessful. Error and
6054 warning messages are issued under control of COMPLAIN. This
6055 conversion is for the Ith parameter in the parameter list. ARGS is
6056 the full set of template arguments deduced so far. */
6058 static tree
6059 convert_template_argument (tree parm,
6060 tree arg,
6061 tree args,
6062 tsubst_flags_t complain,
6063 int i,
6064 tree in_decl)
6066 tree orig_arg;
6067 tree val;
6068 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6070 if (TREE_CODE (arg) == TREE_LIST
6071 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6073 /* The template argument was the name of some
6074 member function. That's usually
6075 invalid, but static members are OK. In any
6076 case, grab the underlying fields/functions
6077 and issue an error later if required. */
6078 orig_arg = TREE_VALUE (arg);
6079 TREE_TYPE (arg) = unknown_type_node;
6082 orig_arg = arg;
6084 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6085 requires_type = (TREE_CODE (parm) == TYPE_DECL
6086 || requires_tmpl_type);
6088 /* When determining whether an argument pack expansion is a template,
6089 look at the pattern. */
6090 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6091 arg = PACK_EXPANSION_PATTERN (arg);
6093 /* Deal with an injected-class-name used as a template template arg. */
6094 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6096 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6097 if (TREE_CODE (t) == TEMPLATE_DECL)
6099 if (cxx_dialect >= cxx0x)
6100 /* OK under DR 1004. */;
6101 else if (complain & tf_warning_or_error)
6102 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6103 " used as template template argument", TYPE_NAME (arg));
6104 else if (flag_pedantic_errors)
6105 t = arg;
6107 arg = t;
6111 is_tmpl_type =
6112 ((TREE_CODE (arg) == TEMPLATE_DECL
6113 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6114 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6115 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6116 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6118 if (is_tmpl_type
6119 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6120 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6121 arg = TYPE_STUB_DECL (arg);
6123 is_type = TYPE_P (arg) || is_tmpl_type;
6125 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6126 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6128 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6130 if (complain & tf_error)
6131 error ("invalid use of destructor %qE as a type", orig_arg);
6132 return error_mark_node;
6135 permerror (input_location,
6136 "to refer to a type member of a template parameter, "
6137 "use %<typename %E%>", orig_arg);
6139 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6140 TREE_OPERAND (arg, 1),
6141 typename_type,
6142 complain & tf_error);
6143 arg = orig_arg;
6144 is_type = 1;
6146 if (is_type != requires_type)
6148 if (in_decl)
6150 if (complain & tf_error)
6152 error ("type/value mismatch at argument %d in template "
6153 "parameter list for %qD",
6154 i + 1, in_decl);
6155 if (is_type)
6156 error (" expected a constant of type %qT, got %qT",
6157 TREE_TYPE (parm),
6158 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6159 else if (requires_tmpl_type)
6160 error (" expected a class template, got %qE", orig_arg);
6161 else
6162 error (" expected a type, got %qE", orig_arg);
6165 return error_mark_node;
6167 if (is_tmpl_type ^ requires_tmpl_type)
6169 if (in_decl && (complain & tf_error))
6171 error ("type/value mismatch at argument %d in template "
6172 "parameter list for %qD",
6173 i + 1, in_decl);
6174 if (is_tmpl_type)
6175 error (" expected a type, got %qT", DECL_NAME (arg));
6176 else
6177 error (" expected a class template, got %qT", orig_arg);
6179 return error_mark_node;
6182 if (is_type)
6184 if (requires_tmpl_type)
6186 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6187 val = orig_arg;
6188 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6189 /* The number of argument required is not known yet.
6190 Just accept it for now. */
6191 val = TREE_TYPE (arg);
6192 else
6194 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6195 tree argparm;
6197 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6199 if (coerce_template_template_parms (parmparm, argparm,
6200 complain, in_decl,
6201 args))
6203 val = arg;
6205 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6206 TEMPLATE_DECL. */
6207 if (val != error_mark_node)
6209 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6210 val = TREE_TYPE (val);
6211 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6212 val = make_pack_expansion (val);
6215 else
6217 if (in_decl && (complain & tf_error))
6219 error ("type/value mismatch at argument %d in "
6220 "template parameter list for %qD",
6221 i + 1, in_decl);
6222 error (" expected a template of type %qD, got %qT",
6223 parm, orig_arg);
6226 val = error_mark_node;
6230 else
6231 val = orig_arg;
6232 /* We only form one instance of each template specialization.
6233 Therefore, if we use a non-canonical variant (i.e., a
6234 typedef), any future messages referring to the type will use
6235 the typedef, which is confusing if those future uses do not
6236 themselves also use the typedef. */
6237 if (TYPE_P (val))
6238 val = canonicalize_type_argument (val, complain);
6240 else
6242 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6244 if (invalid_nontype_parm_type_p (t, complain))
6245 return error_mark_node;
6247 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6249 if (same_type_p (t, TREE_TYPE (orig_arg)))
6250 val = orig_arg;
6251 else
6253 /* Not sure if this is reachable, but it doesn't hurt
6254 to be robust. */
6255 error ("type mismatch in nontype parameter pack");
6256 val = error_mark_node;
6259 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6260 /* We used to call digest_init here. However, digest_init
6261 will report errors, which we don't want when complain
6262 is zero. More importantly, digest_init will try too
6263 hard to convert things: for example, `0' should not be
6264 converted to pointer type at this point according to
6265 the standard. Accepting this is not merely an
6266 extension, since deciding whether or not these
6267 conversions can occur is part of determining which
6268 function template to call, or whether a given explicit
6269 argument specification is valid. */
6270 val = convert_nontype_argument (t, orig_arg, complain);
6271 else
6272 val = strip_typedefs_expr (orig_arg);
6274 if (val == NULL_TREE)
6275 val = error_mark_node;
6276 else if (val == error_mark_node && (complain & tf_error))
6277 error ("could not convert template argument %qE to %qT", orig_arg, t);
6279 if (TREE_CODE (val) == SCOPE_REF)
6281 /* Strip typedefs from the SCOPE_REF. */
6282 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6283 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6284 complain);
6285 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6286 QUALIFIED_NAME_IS_TEMPLATE (val));
6290 return val;
6293 /* Coerces the remaining template arguments in INNER_ARGS (from
6294 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6295 Returns the coerced argument pack. PARM_IDX is the position of this
6296 parameter in the template parameter list. ARGS is the original
6297 template argument list. */
6298 static tree
6299 coerce_template_parameter_pack (tree parms,
6300 int parm_idx,
6301 tree args,
6302 tree inner_args,
6303 int arg_idx,
6304 tree new_args,
6305 int* lost,
6306 tree in_decl,
6307 tsubst_flags_t complain)
6309 tree parm = TREE_VEC_ELT (parms, parm_idx);
6310 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6311 tree packed_args;
6312 tree argument_pack;
6313 tree packed_types = NULL_TREE;
6315 if (arg_idx > nargs)
6316 arg_idx = nargs;
6318 packed_args = make_tree_vec (nargs - arg_idx);
6320 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6321 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6323 /* When the template parameter is a non-type template
6324 parameter pack whose type uses parameter packs, we need
6325 to look at each of the template arguments
6326 separately. Build a vector of the types for these
6327 non-type template parameters in PACKED_TYPES. */
6328 tree expansion
6329 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6330 packed_types = tsubst_pack_expansion (expansion, args,
6331 complain, in_decl);
6333 if (packed_types == error_mark_node)
6334 return error_mark_node;
6336 /* Check that we have the right number of arguments. */
6337 if (arg_idx < nargs
6338 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6339 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6341 int needed_parms
6342 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6343 error ("wrong number of template arguments (%d, should be %d)",
6344 nargs, needed_parms);
6345 return error_mark_node;
6348 /* If we aren't able to check the actual arguments now
6349 (because they haven't been expanded yet), we can at least
6350 verify that all of the types used for the non-type
6351 template parameter pack are, in fact, valid for non-type
6352 template parameters. */
6353 if (arg_idx < nargs
6354 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6356 int j, len = TREE_VEC_LENGTH (packed_types);
6357 for (j = 0; j < len; ++j)
6359 tree t = TREE_VEC_ELT (packed_types, j);
6360 if (invalid_nontype_parm_type_p (t, complain))
6361 return error_mark_node;
6366 /* Convert the remaining arguments, which will be a part of the
6367 parameter pack "parm". */
6368 for (; arg_idx < nargs; ++arg_idx)
6370 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6371 tree actual_parm = TREE_VALUE (parm);
6373 if (packed_types && !PACK_EXPANSION_P (arg))
6375 /* When we have a vector of types (corresponding to the
6376 non-type template parameter pack that uses parameter
6377 packs in its type, as mention above), and the
6378 argument is not an expansion (which expands to a
6379 currently unknown number of arguments), clone the
6380 parm and give it the next type in PACKED_TYPES. */
6381 actual_parm = copy_node (actual_parm);
6382 TREE_TYPE (actual_parm) =
6383 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6386 if (arg == error_mark_node)
6388 if (complain & tf_error)
6389 error ("template argument %d is invalid", arg_idx + 1);
6391 else
6392 arg = convert_template_argument (actual_parm,
6393 arg, new_args, complain, parm_idx,
6394 in_decl);
6395 if (arg == error_mark_node)
6396 (*lost)++;
6397 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6400 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6401 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6402 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6403 else
6405 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6406 TREE_TYPE (argument_pack)
6407 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6408 TREE_CONSTANT (argument_pack) = 1;
6411 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6412 #ifdef ENABLE_CHECKING
6413 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6414 TREE_VEC_LENGTH (packed_args));
6415 #endif
6416 return argument_pack;
6419 /* Returns true if the template argument vector ARGS contains
6420 any pack expansions, false otherwise. */
6422 static bool
6423 any_pack_expanson_args_p (tree args)
6425 int i;
6426 if (args)
6427 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6428 if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
6429 return true;
6430 return false;
6433 /* Convert all template arguments to their appropriate types, and
6434 return a vector containing the innermost resulting template
6435 arguments. If any error occurs, return error_mark_node. Error and
6436 warning messages are issued under control of COMPLAIN.
6438 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6439 for arguments not specified in ARGS. Otherwise, if
6440 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6441 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6442 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6443 ARGS. */
6445 static tree
6446 coerce_template_parms (tree parms,
6447 tree args,
6448 tree in_decl,
6449 tsubst_flags_t complain,
6450 bool require_all_args,
6451 bool use_default_args)
6453 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6454 tree inner_args;
6455 tree new_args;
6456 tree new_inner_args;
6457 int saved_unevaluated_operand;
6458 int saved_inhibit_evaluation_warnings;
6460 /* When used as a boolean value, indicates whether this is a
6461 variadic template parameter list. Since it's an int, we can also
6462 subtract it from nparms to get the number of non-variadic
6463 parameters. */
6464 int variadic_p = 0;
6465 int post_variadic_parms = 0;
6467 if (args == error_mark_node)
6468 return error_mark_node;
6470 nparms = TREE_VEC_LENGTH (parms);
6472 /* Determine if there are any parameter packs. */
6473 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6475 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6476 if (variadic_p)
6477 ++post_variadic_parms;
6478 if (template_parameter_pack_p (tparm))
6479 ++variadic_p;
6482 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6483 /* If there are no parameters that follow a parameter pack, we need to
6484 expand any argument packs so that we can deduce a parameter pack from
6485 some non-packed args followed by an argument pack, as in variadic85.C.
6486 If there are such parameters, we need to leave argument packs intact
6487 so the arguments are assigned properly. This can happen when dealing
6488 with a nested class inside a partial specialization of a class
6489 template, as in variadic92.C, or when deducing a template parameter pack
6490 from a sub-declarator, as in variadic114.C. */
6491 if (!post_variadic_parms)
6492 inner_args = expand_template_argument_pack (inner_args);
6494 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6495 if ((nargs > nparms && !variadic_p)
6496 || (nargs < nparms - variadic_p
6497 && require_all_args
6498 && !any_pack_expanson_args_p (inner_args)
6499 && (!use_default_args
6500 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6501 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6503 if (complain & tf_error)
6505 if (variadic_p)
6507 nparms -= variadic_p;
6508 error ("wrong number of template arguments "
6509 "(%d, should be %d or more)", nargs, nparms);
6511 else
6512 error ("wrong number of template arguments "
6513 "(%d, should be %d)", nargs, nparms);
6515 if (in_decl)
6516 error ("provided for %q+D", in_decl);
6519 return error_mark_node;
6522 /* We need to evaluate the template arguments, even though this
6523 template-id may be nested within a "sizeof". */
6524 saved_unevaluated_operand = cp_unevaluated_operand;
6525 cp_unevaluated_operand = 0;
6526 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6527 c_inhibit_evaluation_warnings = 0;
6528 new_inner_args = make_tree_vec (nparms);
6529 new_args = add_outermost_template_args (args, new_inner_args);
6530 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6532 tree arg;
6533 tree parm;
6535 /* Get the Ith template parameter. */
6536 parm = TREE_VEC_ELT (parms, parm_idx);
6538 if (parm == error_mark_node)
6540 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6541 continue;
6544 /* Calculate the next argument. */
6545 if (arg_idx < nargs)
6546 arg = TREE_VEC_ELT (inner_args, arg_idx);
6547 else
6548 arg = NULL_TREE;
6550 if (template_parameter_pack_p (TREE_VALUE (parm))
6551 && !(arg && ARGUMENT_PACK_P (arg)))
6553 /* All remaining arguments will be placed in the
6554 template parameter pack PARM. */
6555 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6556 inner_args, arg_idx,
6557 new_args, &lost,
6558 in_decl, complain);
6560 /* Store this argument. */
6561 if (arg == error_mark_node)
6562 lost++;
6563 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6565 /* We are done with all of the arguments. */
6566 arg_idx = nargs;
6568 continue;
6570 else if (arg)
6572 if (PACK_EXPANSION_P (arg))
6574 /* We don't know how many args we have yet, just
6575 use the unconverted ones for now. */
6576 new_inner_args = inner_args;
6577 break;
6580 else if (require_all_args)
6582 /* There must be a default arg in this case. */
6583 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6584 complain, in_decl);
6585 /* The position of the first default template argument,
6586 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6587 Record that. */
6588 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6589 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6591 else
6592 break;
6594 if (arg == error_mark_node)
6596 if (complain & tf_error)
6597 error ("template argument %d is invalid", arg_idx + 1);
6599 else if (!arg)
6600 /* This only occurs if there was an error in the template
6601 parameter list itself (which we would already have
6602 reported) that we are trying to recover from, e.g., a class
6603 template with a parameter list such as
6604 template<typename..., typename>. */
6605 ++lost;
6606 else
6607 arg = convert_template_argument (TREE_VALUE (parm),
6608 arg, new_args, complain,
6609 parm_idx, in_decl);
6611 if (arg == error_mark_node)
6612 lost++;
6613 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6615 cp_unevaluated_operand = saved_unevaluated_operand;
6616 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6618 if (lost)
6619 return error_mark_node;
6621 #ifdef ENABLE_CHECKING
6622 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6623 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6624 TREE_VEC_LENGTH (new_inner_args));
6625 #endif
6627 return new_inner_args;
6630 /* Returns 1 if template args OT and NT are equivalent. */
6632 static int
6633 template_args_equal (tree ot, tree nt)
6635 if (nt == ot)
6636 return 1;
6637 if (nt == NULL_TREE || ot == NULL_TREE)
6638 return false;
6640 if (TREE_CODE (nt) == TREE_VEC)
6641 /* For member templates */
6642 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6643 else if (PACK_EXPANSION_P (ot))
6644 return (PACK_EXPANSION_P (nt)
6645 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6646 PACK_EXPANSION_PATTERN (nt))
6647 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6648 PACK_EXPANSION_EXTRA_ARGS (nt)));
6649 else if (ARGUMENT_PACK_P (ot))
6651 int i, len;
6652 tree opack, npack;
6654 if (!ARGUMENT_PACK_P (nt))
6655 return 0;
6657 opack = ARGUMENT_PACK_ARGS (ot);
6658 npack = ARGUMENT_PACK_ARGS (nt);
6659 len = TREE_VEC_LENGTH (opack);
6660 if (TREE_VEC_LENGTH (npack) != len)
6661 return 0;
6662 for (i = 0; i < len; ++i)
6663 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6664 TREE_VEC_ELT (npack, i)))
6665 return 0;
6666 return 1;
6668 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6670 /* We get here probably because we are in the middle of substituting
6671 into the pattern of a pack expansion. In that case the
6672 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6673 interested in. So we want to use the initial pack argument for
6674 the comparison. */
6675 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6676 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6677 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6678 return template_args_equal (ot, nt);
6680 else if (TYPE_P (nt))
6681 return TYPE_P (ot) && same_type_p (ot, nt);
6682 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6683 return 0;
6684 else
6685 return cp_tree_equal (ot, nt);
6688 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6689 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6690 NEWARG_PTR with the offending arguments if they are non-NULL. */
6692 static int
6693 comp_template_args_with_info (tree oldargs, tree newargs,
6694 tree *oldarg_ptr, tree *newarg_ptr)
6696 int i;
6698 if (oldargs == newargs)
6699 return 1;
6701 if (!oldargs || !newargs)
6702 return 0;
6704 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6705 return 0;
6707 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6709 tree nt = TREE_VEC_ELT (newargs, i);
6710 tree ot = TREE_VEC_ELT (oldargs, i);
6712 if (! template_args_equal (ot, nt))
6714 if (oldarg_ptr != NULL)
6715 *oldarg_ptr = ot;
6716 if (newarg_ptr != NULL)
6717 *newarg_ptr = nt;
6718 return 0;
6721 return 1;
6724 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6725 of template arguments. Returns 0 otherwise. */
6728 comp_template_args (tree oldargs, tree newargs)
6730 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6733 static void
6734 add_pending_template (tree d)
6736 tree ti = (TYPE_P (d)
6737 ? CLASSTYPE_TEMPLATE_INFO (d)
6738 : DECL_TEMPLATE_INFO (d));
6739 struct pending_template *pt;
6740 int level;
6742 if (TI_PENDING_TEMPLATE_FLAG (ti))
6743 return;
6745 /* We are called both from instantiate_decl, where we've already had a
6746 tinst_level pushed, and instantiate_template, where we haven't.
6747 Compensate. */
6748 level = !current_tinst_level || current_tinst_level->decl != d;
6750 if (level)
6751 push_tinst_level (d);
6753 pt = ggc_alloc_pending_template ();
6754 pt->next = NULL;
6755 pt->tinst = current_tinst_level;
6756 if (last_pending_template)
6757 last_pending_template->next = pt;
6758 else
6759 pending_templates = pt;
6761 last_pending_template = pt;
6763 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6765 if (level)
6766 pop_tinst_level ();
6770 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6771 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6772 documentation for TEMPLATE_ID_EXPR. */
6774 tree
6775 lookup_template_function (tree fns, tree arglist)
6777 tree type;
6779 if (fns == error_mark_node || arglist == error_mark_node)
6780 return error_mark_node;
6782 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6784 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6786 error ("%q#D is not a function template", fns);
6787 return error_mark_node;
6790 if (BASELINK_P (fns))
6792 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6793 unknown_type_node,
6794 BASELINK_FUNCTIONS (fns),
6795 arglist);
6796 return fns;
6799 type = TREE_TYPE (fns);
6800 if (TREE_CODE (fns) == OVERLOAD || !type)
6801 type = unknown_type_node;
6803 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6806 /* Within the scope of a template class S<T>, the name S gets bound
6807 (in build_self_reference) to a TYPE_DECL for the class, not a
6808 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6809 or one of its enclosing classes, and that type is a template,
6810 return the associated TEMPLATE_DECL. Otherwise, the original
6811 DECL is returned.
6813 Also handle the case when DECL is a TREE_LIST of ambiguous
6814 injected-class-names from different bases. */
6816 tree
6817 maybe_get_template_decl_from_type_decl (tree decl)
6819 if (decl == NULL_TREE)
6820 return decl;
6822 /* DR 176: A lookup that finds an injected-class-name (10.2
6823 [class.member.lookup]) can result in an ambiguity in certain cases
6824 (for example, if it is found in more than one base class). If all of
6825 the injected-class-names that are found refer to specializations of
6826 the same class template, and if the name is followed by a
6827 template-argument-list, the reference refers to the class template
6828 itself and not a specialization thereof, and is not ambiguous. */
6829 if (TREE_CODE (decl) == TREE_LIST)
6831 tree t, tmpl = NULL_TREE;
6832 for (t = decl; t; t = TREE_CHAIN (t))
6834 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6835 if (!tmpl)
6836 tmpl = elt;
6837 else if (tmpl != elt)
6838 break;
6840 if (tmpl && t == NULL_TREE)
6841 return tmpl;
6842 else
6843 return decl;
6846 return (decl != NULL_TREE
6847 && DECL_SELF_REFERENCE_P (decl)
6848 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6849 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6852 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6853 parameters, find the desired type.
6855 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6857 IN_DECL, if non-NULL, is the template declaration we are trying to
6858 instantiate.
6860 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6861 the class we are looking up.
6863 Issue error and warning messages under control of COMPLAIN.
6865 If the template class is really a local class in a template
6866 function, then the FUNCTION_CONTEXT is the function in which it is
6867 being instantiated.
6869 ??? Note that this function is currently called *twice* for each
6870 template-id: the first time from the parser, while creating the
6871 incomplete type (finish_template_type), and the second type during the
6872 real instantiation (instantiate_template_class). This is surely something
6873 that we want to avoid. It also causes some problems with argument
6874 coercion (see convert_nontype_argument for more information on this). */
6876 static tree
6877 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
6878 int entering_scope, tsubst_flags_t complain)
6880 tree templ = NULL_TREE, parmlist;
6881 tree t;
6882 void **slot;
6883 spec_entry *entry;
6884 spec_entry elt;
6885 hashval_t hash;
6887 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6889 tree value = innermost_non_namespace_value (d1);
6890 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6891 templ = value;
6892 else
6894 if (context)
6895 push_decl_namespace (context);
6896 templ = lookup_name (d1);
6897 templ = maybe_get_template_decl_from_type_decl (templ);
6898 if (context)
6899 pop_decl_namespace ();
6901 if (templ)
6902 context = DECL_CONTEXT (templ);
6904 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6906 tree type = TREE_TYPE (d1);
6908 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6909 an implicit typename for the second A. Deal with it. */
6910 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6911 type = TREE_TYPE (type);
6913 if (CLASSTYPE_TEMPLATE_INFO (type))
6915 templ = CLASSTYPE_TI_TEMPLATE (type);
6916 d1 = DECL_NAME (templ);
6919 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6920 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6922 templ = TYPE_TI_TEMPLATE (d1);
6923 d1 = DECL_NAME (templ);
6925 else if (TREE_CODE (d1) == TEMPLATE_DECL
6926 && DECL_TEMPLATE_RESULT (d1)
6927 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6929 templ = d1;
6930 d1 = DECL_NAME (templ);
6931 context = DECL_CONTEXT (templ);
6934 /* Issue an error message if we didn't find a template. */
6935 if (! templ)
6937 if (complain & tf_error)
6938 error ("%qT is not a template", d1);
6939 return error_mark_node;
6942 if (TREE_CODE (templ) != TEMPLATE_DECL
6943 /* Make sure it's a user visible template, if it was named by
6944 the user. */
6945 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6946 && !PRIMARY_TEMPLATE_P (templ)))
6948 if (complain & tf_error)
6950 error ("non-template type %qT used as a template", d1);
6951 if (in_decl)
6952 error ("for template declaration %q+D", in_decl);
6954 return error_mark_node;
6957 complain &= ~tf_user;
6959 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6961 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6962 template arguments */
6964 tree parm;
6965 tree arglist2;
6966 tree outer;
6968 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6970 /* Consider an example where a template template parameter declared as
6972 template <class T, class U = std::allocator<T> > class TT
6974 The template parameter level of T and U are one level larger than
6975 of TT. To proper process the default argument of U, say when an
6976 instantiation `TT<int>' is seen, we need to build the full
6977 arguments containing {int} as the innermost level. Outer levels,
6978 available when not appearing as default template argument, can be
6979 obtained from the arguments of the enclosing template.
6981 Suppose that TT is later substituted with std::vector. The above
6982 instantiation is `TT<int, std::allocator<T> >' with TT at
6983 level 1, and T at level 2, while the template arguments at level 1
6984 becomes {std::vector} and the inner level 2 is {int}. */
6986 outer = DECL_CONTEXT (templ);
6987 if (outer)
6988 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6989 else if (current_template_parms)
6990 /* This is an argument of the current template, so we haven't set
6991 DECL_CONTEXT yet. */
6992 outer = current_template_args ();
6994 if (outer)
6995 arglist = add_to_template_args (outer, arglist);
6997 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6998 complain,
6999 /*require_all_args=*/true,
7000 /*use_default_args=*/true);
7001 if (arglist2 == error_mark_node
7002 || (!uses_template_parms (arglist2)
7003 && check_instantiated_args (templ, arglist2, complain)))
7004 return error_mark_node;
7006 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7007 return parm;
7009 else
7011 tree template_type = TREE_TYPE (templ);
7012 tree gen_tmpl;
7013 tree type_decl;
7014 tree found = NULL_TREE;
7015 int arg_depth;
7016 int parm_depth;
7017 int is_dependent_type;
7018 int use_partial_inst_tmpl = false;
7020 if (template_type == error_mark_node)
7021 /* An error occured while building the template TEMPL, and a
7022 diagnostic has most certainly been emitted for that
7023 already. Let's propagate that error. */
7024 return error_mark_node;
7026 gen_tmpl = most_general_template (templ);
7027 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7028 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7029 arg_depth = TMPL_ARGS_DEPTH (arglist);
7031 if (arg_depth == 1 && parm_depth > 1)
7033 /* We've been given an incomplete set of template arguments.
7034 For example, given:
7036 template <class T> struct S1 {
7037 template <class U> struct S2 {};
7038 template <class U> struct S2<U*> {};
7041 we will be called with an ARGLIST of `U*', but the
7042 TEMPLATE will be `template <class T> template
7043 <class U> struct S1<T>::S2'. We must fill in the missing
7044 arguments. */
7045 arglist
7046 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7047 arglist);
7048 arg_depth = TMPL_ARGS_DEPTH (arglist);
7051 /* Now we should have enough arguments. */
7052 gcc_assert (parm_depth == arg_depth);
7054 /* From here on, we're only interested in the most general
7055 template. */
7057 /* Calculate the BOUND_ARGS. These will be the args that are
7058 actually tsubst'd into the definition to create the
7059 instantiation. */
7060 if (parm_depth > 1)
7062 /* We have multiple levels of arguments to coerce, at once. */
7063 int i;
7064 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7066 tree bound_args = make_tree_vec (parm_depth);
7068 for (i = saved_depth,
7069 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7070 i > 0 && t != NULL_TREE;
7071 --i, t = TREE_CHAIN (t))
7073 tree a;
7074 if (i == saved_depth)
7075 a = coerce_template_parms (TREE_VALUE (t),
7076 arglist, gen_tmpl,
7077 complain,
7078 /*require_all_args=*/true,
7079 /*use_default_args=*/true);
7080 else
7081 /* Outer levels should have already been coerced. */
7082 a = TMPL_ARGS_LEVEL (arglist, i);
7084 /* Don't process further if one of the levels fails. */
7085 if (a == error_mark_node)
7087 /* Restore the ARGLIST to its full size. */
7088 TREE_VEC_LENGTH (arglist) = saved_depth;
7089 return error_mark_node;
7092 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7094 /* We temporarily reduce the length of the ARGLIST so
7095 that coerce_template_parms will see only the arguments
7096 corresponding to the template parameters it is
7097 examining. */
7098 TREE_VEC_LENGTH (arglist)--;
7101 /* Restore the ARGLIST to its full size. */
7102 TREE_VEC_LENGTH (arglist) = saved_depth;
7104 arglist = bound_args;
7106 else
7107 arglist
7108 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7109 INNERMOST_TEMPLATE_ARGS (arglist),
7110 gen_tmpl,
7111 complain,
7112 /*require_all_args=*/true,
7113 /*use_default_args=*/true);
7115 if (arglist == error_mark_node)
7116 /* We were unable to bind the arguments. */
7117 return error_mark_node;
7119 /* In the scope of a template class, explicit references to the
7120 template class refer to the type of the template, not any
7121 instantiation of it. For example, in:
7123 template <class T> class C { void f(C<T>); }
7125 the `C<T>' is just the same as `C'. Outside of the
7126 class, however, such a reference is an instantiation. */
7127 if ((entering_scope
7128 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7129 || currently_open_class (template_type))
7130 /* comp_template_args is expensive, check it last. */
7131 && comp_template_args (TYPE_TI_ARGS (template_type),
7132 arglist))
7133 return template_type;
7135 /* If we already have this specialization, return it. */
7136 elt.tmpl = gen_tmpl;
7137 elt.args = arglist;
7138 hash = hash_specialization (&elt);
7139 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7140 &elt, hash);
7142 if (entry)
7143 return entry->spec;
7145 is_dependent_type = uses_template_parms (arglist);
7147 /* If the deduced arguments are invalid, then the binding
7148 failed. */
7149 if (!is_dependent_type
7150 && check_instantiated_args (gen_tmpl,
7151 INNERMOST_TEMPLATE_ARGS (arglist),
7152 complain))
7153 return error_mark_node;
7155 if (!is_dependent_type
7156 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7157 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7158 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7160 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7161 DECL_NAME (gen_tmpl),
7162 /*tag_scope=*/ts_global);
7163 return found;
7166 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7167 complain, in_decl);
7168 if (context == error_mark_node)
7169 return error_mark_node;
7171 if (!context)
7172 context = global_namespace;
7174 /* Create the type. */
7175 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7177 if (!is_dependent_type)
7179 set_current_access_from_decl (TYPE_NAME (template_type));
7180 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7181 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7182 arglist, complain, in_decl),
7183 SCOPED_ENUM_P (template_type), NULL);
7185 else
7187 /* We don't want to call start_enum for this type, since
7188 the values for the enumeration constants may involve
7189 template parameters. And, no one should be interested
7190 in the enumeration constants for such a type. */
7191 t = cxx_make_type (ENUMERAL_TYPE);
7192 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7194 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7195 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7196 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7198 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7200 /* The user referred to a specialization of an alias
7201 template represented by GEN_TMPL.
7203 [temp.alias]/2 says:
7205 When a template-id refers to the specialization of an
7206 alias template, it is equivalent to the associated
7207 type obtained by substitution of its
7208 template-arguments for the template-parameters in the
7209 type-id of the alias template. */
7211 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7212 /* Note that the call above (by indirectly calling
7213 register_specialization in tsubst_decl) registers the
7214 TYPE_DECL representing the specialization of the alias
7215 template. So next time someone substitutes ARGLIST for
7216 the template parms into the alias template (GEN_TMPL),
7217 she'll get that TYPE_DECL back. */
7219 if (t == error_mark_node)
7220 return t;
7222 else if (CLASS_TYPE_P (template_type))
7224 t = make_class_type (TREE_CODE (template_type));
7225 CLASSTYPE_DECLARED_CLASS (t)
7226 = CLASSTYPE_DECLARED_CLASS (template_type);
7227 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7228 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7230 /* A local class. Make sure the decl gets registered properly. */
7231 if (context == current_function_decl)
7232 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7234 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7235 /* This instantiation is another name for the primary
7236 template type. Set the TYPE_CANONICAL field
7237 appropriately. */
7238 TYPE_CANONICAL (t) = template_type;
7239 else if (any_template_arguments_need_structural_equality_p (arglist))
7240 /* Some of the template arguments require structural
7241 equality testing, so this template class requires
7242 structural equality testing. */
7243 SET_TYPE_STRUCTURAL_EQUALITY (t);
7245 else
7246 gcc_unreachable ();
7248 /* If we called start_enum or pushtag above, this information
7249 will already be set up. */
7250 if (!TYPE_NAME (t))
7252 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7254 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7255 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7256 DECL_SOURCE_LOCATION (type_decl)
7257 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7259 else
7260 type_decl = TYPE_NAME (t);
7262 if (CLASS_TYPE_P (template_type))
7264 TREE_PRIVATE (type_decl)
7265 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
7266 TREE_PROTECTED (type_decl)
7267 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
7268 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7270 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7271 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7275 /* Let's consider the explicit specialization of a member
7276 of a class template specialization that is implicitely instantiated,
7277 e.g.:
7278 template<class T>
7279 struct S
7281 template<class U> struct M {}; //#0
7284 template<>
7285 template<>
7286 struct S<int>::M<char> //#1
7288 int i;
7290 [temp.expl.spec]/4 says this is valid.
7292 In this case, when we write:
7293 S<int>::M<char> m;
7295 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7296 the one of #0.
7298 When we encounter #1, we want to store the partial instantiation
7299 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7301 For all cases other than this "explicit specialization of member of a
7302 class template", we just want to store the most general template into
7303 the CLASSTYPE_TI_TEMPLATE of M.
7305 This case of "explicit specialization of member of a class template"
7306 only happens when:
7307 1/ the enclosing class is an instantiation of, and therefore not
7308 the same as, the context of the most general template, and
7309 2/ we aren't looking at the partial instantiation itself, i.e.
7310 the innermost arguments are not the same as the innermost parms of
7311 the most general template.
7313 So it's only when 1/ and 2/ happens that we want to use the partial
7314 instantiation of the member template in lieu of its most general
7315 template. */
7317 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7318 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7319 /* the enclosing class must be an instantiation... */
7320 && CLASS_TYPE_P (context)
7321 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7323 tree partial_inst_args;
7324 TREE_VEC_LENGTH (arglist)--;
7325 ++processing_template_decl;
7326 partial_inst_args =
7327 tsubst (INNERMOST_TEMPLATE_ARGS
7328 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7329 arglist, complain, NULL_TREE);
7330 --processing_template_decl;
7331 TREE_VEC_LENGTH (arglist)++;
7332 use_partial_inst_tmpl =
7333 /*...and we must not be looking at the partial instantiation
7334 itself. */
7335 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7336 partial_inst_args);
7339 if (!use_partial_inst_tmpl)
7340 /* This case is easy; there are no member templates involved. */
7341 found = gen_tmpl;
7342 else
7344 /* This is a full instantiation of a member template. Find
7345 the partial instantiation of which this is an instance. */
7347 /* Temporarily reduce by one the number of levels in the ARGLIST
7348 so as to avoid comparing the last set of arguments. */
7349 TREE_VEC_LENGTH (arglist)--;
7350 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7351 TREE_VEC_LENGTH (arglist)++;
7352 /* FOUND is either a proper class type, or an alias
7353 template specialization. In the later case, it's a
7354 TYPE_DECL, resulting from the substituting of arguments
7355 for parameters in the TYPE_DECL of the alias template
7356 done earlier. So be careful while getting the template
7357 of FOUND. */
7358 found = TREE_CODE (found) == TYPE_DECL
7359 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7360 : CLASSTYPE_TI_TEMPLATE (found);
7363 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7365 elt.spec = t;
7366 slot = htab_find_slot_with_hash (type_specializations,
7367 &elt, hash, INSERT);
7368 entry = ggc_alloc_spec_entry ();
7369 *entry = elt;
7370 *slot = entry;
7372 /* Note this use of the partial instantiation so we can check it
7373 later in maybe_process_partial_specialization. */
7374 DECL_TEMPLATE_INSTANTIATIONS (templ)
7375 = tree_cons (arglist, t,
7376 DECL_TEMPLATE_INSTANTIATIONS (templ));
7378 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
7379 /* Now that the type has been registered on the instantiations
7380 list, we set up the enumerators. Because the enumeration
7381 constants may involve the enumeration type itself, we make
7382 sure to register the type first, and then create the
7383 constants. That way, doing tsubst_expr for the enumeration
7384 constants won't result in recursive calls here; we'll find
7385 the instantiation and exit above. */
7386 tsubst_enum (template_type, t, arglist);
7388 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7389 /* If the type makes use of template parameters, the
7390 code that generates debugging information will crash. */
7391 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7393 /* Possibly limit visibility based on template args. */
7394 TREE_PUBLIC (type_decl) = 1;
7395 determine_visibility (type_decl);
7397 return t;
7401 /* Wrapper for lookup_template_class_1. */
7403 tree
7404 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7405 int entering_scope, tsubst_flags_t complain)
7407 tree ret;
7408 timevar_push (TV_TEMPLATE_INST);
7409 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7410 entering_scope, complain);
7411 timevar_pop (TV_TEMPLATE_INST);
7412 return ret;
7415 struct pair_fn_data
7417 tree_fn_t fn;
7418 void *data;
7419 /* True when we should also visit template parameters that occur in
7420 non-deduced contexts. */
7421 bool include_nondeduced_p;
7422 struct pointer_set_t *visited;
7425 /* Called from for_each_template_parm via walk_tree. */
7427 static tree
7428 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7430 tree t = *tp;
7431 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7432 tree_fn_t fn = pfd->fn;
7433 void *data = pfd->data;
7435 if (TYPE_P (t)
7436 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7437 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7438 pfd->include_nondeduced_p))
7439 return error_mark_node;
7441 switch (TREE_CODE (t))
7443 case RECORD_TYPE:
7444 if (TYPE_PTRMEMFUNC_P (t))
7445 break;
7446 /* Fall through. */
7448 case UNION_TYPE:
7449 case ENUMERAL_TYPE:
7450 if (!TYPE_TEMPLATE_INFO (t))
7451 *walk_subtrees = 0;
7452 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7453 fn, data, pfd->visited,
7454 pfd->include_nondeduced_p))
7455 return error_mark_node;
7456 break;
7458 case INTEGER_TYPE:
7459 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7460 fn, data, pfd->visited,
7461 pfd->include_nondeduced_p)
7462 || for_each_template_parm (TYPE_MAX_VALUE (t),
7463 fn, data, pfd->visited,
7464 pfd->include_nondeduced_p))
7465 return error_mark_node;
7466 break;
7468 case METHOD_TYPE:
7469 /* Since we're not going to walk subtrees, we have to do this
7470 explicitly here. */
7471 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7472 pfd->visited, pfd->include_nondeduced_p))
7473 return error_mark_node;
7474 /* Fall through. */
7476 case FUNCTION_TYPE:
7477 /* Check the return type. */
7478 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7479 pfd->include_nondeduced_p))
7480 return error_mark_node;
7482 /* Check the parameter types. Since default arguments are not
7483 instantiated until they are needed, the TYPE_ARG_TYPES may
7484 contain expressions that involve template parameters. But,
7485 no-one should be looking at them yet. And, once they're
7486 instantiated, they don't contain template parameters, so
7487 there's no point in looking at them then, either. */
7489 tree parm;
7491 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7492 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7493 pfd->visited, pfd->include_nondeduced_p))
7494 return error_mark_node;
7496 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7497 want walk_tree walking into them itself. */
7498 *walk_subtrees = 0;
7500 break;
7502 case TYPEOF_TYPE:
7503 case UNDERLYING_TYPE:
7504 if (pfd->include_nondeduced_p
7505 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7506 pfd->visited,
7507 pfd->include_nondeduced_p))
7508 return error_mark_node;
7509 break;
7511 case FUNCTION_DECL:
7512 case VAR_DECL:
7513 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7514 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7515 pfd->visited, pfd->include_nondeduced_p))
7516 return error_mark_node;
7517 /* Fall through. */
7519 case PARM_DECL:
7520 case CONST_DECL:
7521 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7522 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7523 pfd->visited, pfd->include_nondeduced_p))
7524 return error_mark_node;
7525 if (DECL_CONTEXT (t)
7526 && pfd->include_nondeduced_p
7527 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7528 pfd->visited, pfd->include_nondeduced_p))
7529 return error_mark_node;
7530 break;
7532 case BOUND_TEMPLATE_TEMPLATE_PARM:
7533 /* Record template parameters such as `T' inside `TT<T>'. */
7534 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7535 pfd->include_nondeduced_p))
7536 return error_mark_node;
7537 /* Fall through. */
7539 case TEMPLATE_TEMPLATE_PARM:
7540 case TEMPLATE_TYPE_PARM:
7541 case TEMPLATE_PARM_INDEX:
7542 if (fn && (*fn)(t, data))
7543 return error_mark_node;
7544 else if (!fn)
7545 return error_mark_node;
7546 break;
7548 case TEMPLATE_DECL:
7549 /* A template template parameter is encountered. */
7550 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7551 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7552 pfd->include_nondeduced_p))
7553 return error_mark_node;
7555 /* Already substituted template template parameter */
7556 *walk_subtrees = 0;
7557 break;
7559 case TYPENAME_TYPE:
7560 if (!fn
7561 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7562 data, pfd->visited,
7563 pfd->include_nondeduced_p))
7564 return error_mark_node;
7565 break;
7567 case CONSTRUCTOR:
7568 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7569 && pfd->include_nondeduced_p
7570 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7571 (TREE_TYPE (t)), fn, data,
7572 pfd->visited, pfd->include_nondeduced_p))
7573 return error_mark_node;
7574 break;
7576 case INDIRECT_REF:
7577 case COMPONENT_REF:
7578 /* If there's no type, then this thing must be some expression
7579 involving template parameters. */
7580 if (!fn && !TREE_TYPE (t))
7581 return error_mark_node;
7582 break;
7584 case MODOP_EXPR:
7585 case CAST_EXPR:
7586 case IMPLICIT_CONV_EXPR:
7587 case REINTERPRET_CAST_EXPR:
7588 case CONST_CAST_EXPR:
7589 case STATIC_CAST_EXPR:
7590 case DYNAMIC_CAST_EXPR:
7591 case ARROW_EXPR:
7592 case DOTSTAR_EXPR:
7593 case TYPEID_EXPR:
7594 case PSEUDO_DTOR_EXPR:
7595 if (!fn)
7596 return error_mark_node;
7597 break;
7599 default:
7600 break;
7603 /* We didn't find any template parameters we liked. */
7604 return NULL_TREE;
7607 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7608 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7609 call FN with the parameter and the DATA.
7610 If FN returns nonzero, the iteration is terminated, and
7611 for_each_template_parm returns 1. Otherwise, the iteration
7612 continues. If FN never returns a nonzero value, the value
7613 returned by for_each_template_parm is 0. If FN is NULL, it is
7614 considered to be the function which always returns 1.
7616 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7617 parameters that occur in non-deduced contexts. When false, only
7618 visits those template parameters that can be deduced. */
7620 static int
7621 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7622 struct pointer_set_t *visited,
7623 bool include_nondeduced_p)
7625 struct pair_fn_data pfd;
7626 int result;
7628 /* Set up. */
7629 pfd.fn = fn;
7630 pfd.data = data;
7631 pfd.include_nondeduced_p = include_nondeduced_p;
7633 /* Walk the tree. (Conceptually, we would like to walk without
7634 duplicates, but for_each_template_parm_r recursively calls
7635 for_each_template_parm, so we would need to reorganize a fair
7636 bit to use walk_tree_without_duplicates, so we keep our own
7637 visited list.) */
7638 if (visited)
7639 pfd.visited = visited;
7640 else
7641 pfd.visited = pointer_set_create ();
7642 result = cp_walk_tree (&t,
7643 for_each_template_parm_r,
7644 &pfd,
7645 pfd.visited) != NULL_TREE;
7647 /* Clean up. */
7648 if (!visited)
7650 pointer_set_destroy (pfd.visited);
7651 pfd.visited = 0;
7654 return result;
7657 /* Returns true if T depends on any template parameter. */
7660 uses_template_parms (tree t)
7662 bool dependent_p;
7663 int saved_processing_template_decl;
7665 saved_processing_template_decl = processing_template_decl;
7666 if (!saved_processing_template_decl)
7667 processing_template_decl = 1;
7668 if (TYPE_P (t))
7669 dependent_p = dependent_type_p (t);
7670 else if (TREE_CODE (t) == TREE_VEC)
7671 dependent_p = any_dependent_template_arguments_p (t);
7672 else if (TREE_CODE (t) == TREE_LIST)
7673 dependent_p = (uses_template_parms (TREE_VALUE (t))
7674 || uses_template_parms (TREE_CHAIN (t)));
7675 else if (TREE_CODE (t) == TYPE_DECL)
7676 dependent_p = dependent_type_p (TREE_TYPE (t));
7677 else if (DECL_P (t)
7678 || EXPR_P (t)
7679 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7680 || TREE_CODE (t) == OVERLOAD
7681 || BASELINK_P (t)
7682 || TREE_CODE (t) == IDENTIFIER_NODE
7683 || TREE_CODE (t) == TRAIT_EXPR
7684 || TREE_CODE (t) == CONSTRUCTOR
7685 || CONSTANT_CLASS_P (t))
7686 dependent_p = (type_dependent_expression_p (t)
7687 || value_dependent_expression_p (t));
7688 else
7690 gcc_assert (t == error_mark_node);
7691 dependent_p = false;
7694 processing_template_decl = saved_processing_template_decl;
7696 return dependent_p;
7699 /* Returns true iff current_function_decl is an incompletely instantiated
7700 template. Useful instead of processing_template_decl because the latter
7701 is set to 0 during fold_non_dependent_expr. */
7703 bool
7704 in_template_function (void)
7706 tree fn = current_function_decl;
7707 bool ret;
7708 ++processing_template_decl;
7709 ret = (fn && DECL_LANG_SPECIFIC (fn)
7710 && DECL_TEMPLATE_INFO (fn)
7711 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7712 --processing_template_decl;
7713 return ret;
7716 /* Returns true if T depends on any template parameter with level LEVEL. */
7719 uses_template_parms_level (tree t, int level)
7721 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7722 /*include_nondeduced_p=*/true);
7725 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7726 ill-formed translation unit, i.e. a variable or function that isn't
7727 usable in a constant expression. */
7729 static inline bool
7730 neglectable_inst_p (tree d)
7732 return (DECL_P (d)
7733 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7734 : decl_maybe_constant_var_p (d)));
7737 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7738 neglectable and instantiated from within an erroneous instantiation. */
7740 static bool
7741 limit_bad_template_recursion (tree decl)
7743 struct tinst_level *lev = current_tinst_level;
7744 int errs = errorcount + sorrycount;
7745 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7746 return false;
7748 for (; lev; lev = lev->next)
7749 if (neglectable_inst_p (lev->decl))
7750 break;
7752 return (lev && errs > lev->errors);
7755 static int tinst_depth;
7756 extern int max_tinst_depth;
7757 #ifdef GATHER_STATISTICS
7758 int depth_reached;
7759 #endif
7760 static GTY(()) struct tinst_level *last_error_tinst_level;
7762 /* We're starting to instantiate D; record the template instantiation context
7763 for diagnostics and to restore it later. */
7766 push_tinst_level (tree d)
7768 struct tinst_level *new_level;
7770 if (tinst_depth >= max_tinst_depth)
7772 last_error_tinst_level = current_tinst_level;
7773 if (TREE_CODE (d) == TREE_LIST)
7774 error ("template instantiation depth exceeds maximum of %d (use "
7775 "-ftemplate-depth= to increase the maximum) substituting %qS",
7776 max_tinst_depth, d);
7777 else
7778 error ("template instantiation depth exceeds maximum of %d (use "
7779 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7780 max_tinst_depth, d);
7782 print_instantiation_context ();
7784 return 0;
7787 /* If the current instantiation caused problems, don't let it instantiate
7788 anything else. Do allow deduction substitution and decls usable in
7789 constant expressions. */
7790 if (limit_bad_template_recursion (d))
7791 return 0;
7793 new_level = ggc_alloc_tinst_level ();
7794 new_level->decl = d;
7795 new_level->locus = input_location;
7796 new_level->errors = errorcount+sorrycount;
7797 new_level->in_system_header_p = in_system_header;
7798 new_level->next = current_tinst_level;
7799 current_tinst_level = new_level;
7801 ++tinst_depth;
7802 #ifdef GATHER_STATISTICS
7803 if (tinst_depth > depth_reached)
7804 depth_reached = tinst_depth;
7805 #endif
7807 return 1;
7810 /* We're done instantiating this template; return to the instantiation
7811 context. */
7813 void
7814 pop_tinst_level (void)
7816 /* Restore the filename and line number stashed away when we started
7817 this instantiation. */
7818 input_location = current_tinst_level->locus;
7819 current_tinst_level = current_tinst_level->next;
7820 --tinst_depth;
7823 /* We're instantiating a deferred template; restore the template
7824 instantiation context in which the instantiation was requested, which
7825 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7827 static tree
7828 reopen_tinst_level (struct tinst_level *level)
7830 struct tinst_level *t;
7832 tinst_depth = 0;
7833 for (t = level; t; t = t->next)
7834 ++tinst_depth;
7836 current_tinst_level = level;
7837 pop_tinst_level ();
7838 if (current_tinst_level)
7839 current_tinst_level->errors = errorcount+sorrycount;
7840 return level->decl;
7843 /* Returns the TINST_LEVEL which gives the original instantiation
7844 context. */
7846 struct tinst_level *
7847 outermost_tinst_level (void)
7849 struct tinst_level *level = current_tinst_level;
7850 if (level)
7851 while (level->next)
7852 level = level->next;
7853 return level;
7856 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7857 vector of template arguments, as for tsubst.
7859 Returns an appropriate tsubst'd friend declaration. */
7861 static tree
7862 tsubst_friend_function (tree decl, tree args)
7864 tree new_friend;
7866 if (TREE_CODE (decl) == FUNCTION_DECL
7867 && DECL_TEMPLATE_INSTANTIATION (decl)
7868 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7869 /* This was a friend declared with an explicit template
7870 argument list, e.g.:
7872 friend void f<>(T);
7874 to indicate that f was a template instantiation, not a new
7875 function declaration. Now, we have to figure out what
7876 instantiation of what template. */
7878 tree template_id, arglist, fns;
7879 tree new_args;
7880 tree tmpl;
7881 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7883 /* Friend functions are looked up in the containing namespace scope.
7884 We must enter that scope, to avoid finding member functions of the
7885 current class with same name. */
7886 push_nested_namespace (ns);
7887 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7888 tf_warning_or_error, NULL_TREE,
7889 /*integral_constant_expression_p=*/false);
7890 pop_nested_namespace (ns);
7891 arglist = tsubst (DECL_TI_ARGS (decl), args,
7892 tf_warning_or_error, NULL_TREE);
7893 template_id = lookup_template_function (fns, arglist);
7895 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7896 tmpl = determine_specialization (template_id, new_friend,
7897 &new_args,
7898 /*need_member_template=*/0,
7899 TREE_VEC_LENGTH (args),
7900 tsk_none);
7901 return instantiate_template (tmpl, new_args, tf_error);
7904 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7906 /* The NEW_FRIEND will look like an instantiation, to the
7907 compiler, but is not an instantiation from the point of view of
7908 the language. For example, we might have had:
7910 template <class T> struct S {
7911 template <class U> friend void f(T, U);
7914 Then, in S<int>, template <class U> void f(int, U) is not an
7915 instantiation of anything. */
7916 if (new_friend == error_mark_node)
7917 return error_mark_node;
7919 DECL_USE_TEMPLATE (new_friend) = 0;
7920 if (TREE_CODE (decl) == TEMPLATE_DECL)
7922 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7923 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7924 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7927 /* The mangled name for the NEW_FRIEND is incorrect. The function
7928 is not a template instantiation and should not be mangled like
7929 one. Therefore, we forget the mangling here; we'll recompute it
7930 later if we need it. */
7931 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7933 SET_DECL_RTL (new_friend, NULL);
7934 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7937 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7939 tree old_decl;
7940 tree new_friend_template_info;
7941 tree new_friend_result_template_info;
7942 tree ns;
7943 int new_friend_is_defn;
7945 /* We must save some information from NEW_FRIEND before calling
7946 duplicate decls since that function will free NEW_FRIEND if
7947 possible. */
7948 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7949 new_friend_is_defn =
7950 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7951 (template_for_substitution (new_friend)))
7952 != NULL_TREE);
7953 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7955 /* This declaration is a `primary' template. */
7956 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7958 new_friend_result_template_info
7959 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7961 else
7962 new_friend_result_template_info = NULL_TREE;
7964 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7965 if (new_friend_is_defn)
7966 DECL_INITIAL (new_friend) = error_mark_node;
7968 /* Inside pushdecl_namespace_level, we will push into the
7969 current namespace. However, the friend function should go
7970 into the namespace of the template. */
7971 ns = decl_namespace_context (new_friend);
7972 push_nested_namespace (ns);
7973 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7974 pop_nested_namespace (ns);
7976 if (old_decl == error_mark_node)
7977 return error_mark_node;
7979 if (old_decl != new_friend)
7981 /* This new friend declaration matched an existing
7982 declaration. For example, given:
7984 template <class T> void f(T);
7985 template <class U> class C {
7986 template <class T> friend void f(T) {}
7989 the friend declaration actually provides the definition
7990 of `f', once C has been instantiated for some type. So,
7991 old_decl will be the out-of-class template declaration,
7992 while new_friend is the in-class definition.
7994 But, if `f' was called before this point, the
7995 instantiation of `f' will have DECL_TI_ARGS corresponding
7996 to `T' but not to `U', references to which might appear
7997 in the definition of `f'. Previously, the most general
7998 template for an instantiation of `f' was the out-of-class
7999 version; now it is the in-class version. Therefore, we
8000 run through all specialization of `f', adding to their
8001 DECL_TI_ARGS appropriately. In particular, they need a
8002 new set of outer arguments, corresponding to the
8003 arguments for this class instantiation.
8005 The same situation can arise with something like this:
8007 friend void f(int);
8008 template <class T> class C {
8009 friend void f(T) {}
8012 when `C<int>' is instantiated. Now, `f(int)' is defined
8013 in the class. */
8015 if (!new_friend_is_defn)
8016 /* On the other hand, if the in-class declaration does
8017 *not* provide a definition, then we don't want to alter
8018 existing definitions. We can just leave everything
8019 alone. */
8021 else
8023 tree new_template = TI_TEMPLATE (new_friend_template_info);
8024 tree new_args = TI_ARGS (new_friend_template_info);
8026 /* Overwrite whatever template info was there before, if
8027 any, with the new template information pertaining to
8028 the declaration. */
8029 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8031 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8033 /* We should have called reregister_specialization in
8034 duplicate_decls. */
8035 gcc_assert (retrieve_specialization (new_template,
8036 new_args, 0)
8037 == old_decl);
8039 /* Instantiate it if the global has already been used. */
8040 if (DECL_ODR_USED (old_decl))
8041 instantiate_decl (old_decl, /*defer_ok=*/true,
8042 /*expl_inst_class_mem_p=*/false);
8044 else
8046 tree t;
8048 /* Indicate that the old function template is a partial
8049 instantiation. */
8050 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8051 = new_friend_result_template_info;
8053 gcc_assert (new_template
8054 == most_general_template (new_template));
8055 gcc_assert (new_template != old_decl);
8057 /* Reassign any specializations already in the hash table
8058 to the new more general template, and add the
8059 additional template args. */
8060 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8061 t != NULL_TREE;
8062 t = TREE_CHAIN (t))
8064 tree spec = TREE_VALUE (t);
8065 spec_entry elt;
8067 elt.tmpl = old_decl;
8068 elt.args = DECL_TI_ARGS (spec);
8069 elt.spec = NULL_TREE;
8071 htab_remove_elt (decl_specializations, &elt);
8073 DECL_TI_ARGS (spec)
8074 = add_outermost_template_args (new_args,
8075 DECL_TI_ARGS (spec));
8077 register_specialization
8078 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8081 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8085 /* The information from NEW_FRIEND has been merged into OLD_DECL
8086 by duplicate_decls. */
8087 new_friend = old_decl;
8090 else
8092 tree context = DECL_CONTEXT (new_friend);
8093 bool dependent_p;
8095 /* In the code
8096 template <class T> class C {
8097 template <class U> friend void C1<U>::f (); // case 1
8098 friend void C2<T>::f (); // case 2
8100 we only need to make sure CONTEXT is a complete type for
8101 case 2. To distinguish between the two cases, we note that
8102 CONTEXT of case 1 remains dependent type after tsubst while
8103 this isn't true for case 2. */
8104 ++processing_template_decl;
8105 dependent_p = dependent_type_p (context);
8106 --processing_template_decl;
8108 if (!dependent_p
8109 && !complete_type_or_else (context, NULL_TREE))
8110 return error_mark_node;
8112 if (COMPLETE_TYPE_P (context))
8114 /* Check to see that the declaration is really present, and,
8115 possibly obtain an improved declaration. */
8116 tree fn = check_classfn (context,
8117 new_friend, NULL_TREE);
8119 if (fn)
8120 new_friend = fn;
8124 return new_friend;
8127 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8128 template arguments, as for tsubst.
8130 Returns an appropriate tsubst'd friend type or error_mark_node on
8131 failure. */
8133 static tree
8134 tsubst_friend_class (tree friend_tmpl, tree args)
8136 tree friend_type;
8137 tree tmpl;
8138 tree context;
8140 context = CP_DECL_CONTEXT (friend_tmpl);
8142 if (context != global_namespace)
8144 if (TREE_CODE (context) == NAMESPACE_DECL)
8145 push_nested_namespace (context);
8146 else
8147 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8150 /* Look for a class template declaration. We look for hidden names
8151 because two friend declarations of the same template are the
8152 same. For example, in:
8154 struct A {
8155 template <typename> friend class F;
8157 template <typename> struct B {
8158 template <typename> friend class F;
8161 both F templates are the same. */
8162 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8163 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8165 /* But, if we don't find one, it might be because we're in a
8166 situation like this:
8168 template <class T>
8169 struct S {
8170 template <class U>
8171 friend struct S;
8174 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8175 for `S<int>', not the TEMPLATE_DECL. */
8176 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8178 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8179 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8182 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8184 /* The friend template has already been declared. Just
8185 check to see that the declarations match, and install any new
8186 default parameters. We must tsubst the default parameters,
8187 of course. We only need the innermost template parameters
8188 because that is all that redeclare_class_template will look
8189 at. */
8190 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8191 > TMPL_ARGS_DEPTH (args))
8193 tree parms;
8194 location_t saved_input_location;
8195 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8196 args, tf_warning_or_error);
8198 saved_input_location = input_location;
8199 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8200 redeclare_class_template (TREE_TYPE (tmpl), parms);
8201 input_location = saved_input_location;
8205 friend_type = TREE_TYPE (tmpl);
8207 else
8209 /* The friend template has not already been declared. In this
8210 case, the instantiation of the template class will cause the
8211 injection of this template into the global scope. */
8212 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8213 if (tmpl == error_mark_node)
8214 return error_mark_node;
8216 /* The new TMPL is not an instantiation of anything, so we
8217 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8218 the new type because that is supposed to be the corresponding
8219 template decl, i.e., TMPL. */
8220 DECL_USE_TEMPLATE (tmpl) = 0;
8221 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8222 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8223 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8224 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8226 /* Inject this template into the global scope. */
8227 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8230 if (context != global_namespace)
8232 if (TREE_CODE (context) == NAMESPACE_DECL)
8233 pop_nested_namespace (context);
8234 else
8235 pop_nested_class ();
8238 return friend_type;
8241 /* Returns zero if TYPE cannot be completed later due to circularity.
8242 Otherwise returns one. */
8244 static int
8245 can_complete_type_without_circularity (tree type)
8247 if (type == NULL_TREE || type == error_mark_node)
8248 return 0;
8249 else if (COMPLETE_TYPE_P (type))
8250 return 1;
8251 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8252 return can_complete_type_without_circularity (TREE_TYPE (type));
8253 else if (CLASS_TYPE_P (type)
8254 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8255 return 0;
8256 else
8257 return 1;
8260 /* Apply any attributes which had to be deferred until instantiation
8261 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8262 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8264 static void
8265 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8266 tree args, tsubst_flags_t complain, tree in_decl)
8268 tree last_dep = NULL_TREE;
8269 tree t;
8270 tree *p;
8272 for (t = attributes; t; t = TREE_CHAIN (t))
8273 if (ATTR_IS_DEPENDENT (t))
8275 last_dep = t;
8276 attributes = copy_list (attributes);
8277 break;
8280 if (DECL_P (*decl_p))
8282 if (TREE_TYPE (*decl_p) == error_mark_node)
8283 return;
8284 p = &DECL_ATTRIBUTES (*decl_p);
8286 else
8287 p = &TYPE_ATTRIBUTES (*decl_p);
8289 if (last_dep)
8291 tree late_attrs = NULL_TREE;
8292 tree *q = &late_attrs;
8294 for (*p = attributes; *p; )
8296 t = *p;
8297 if (ATTR_IS_DEPENDENT (t))
8299 *p = TREE_CHAIN (t);
8300 TREE_CHAIN (t) = NULL_TREE;
8301 /* If the first attribute argument is an identifier, don't
8302 pass it through tsubst. Attributes like mode, format,
8303 cleanup and several target specific attributes expect it
8304 unmodified. */
8305 if (TREE_VALUE (t)
8306 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8307 && TREE_VALUE (TREE_VALUE (t))
8308 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8309 == IDENTIFIER_NODE))
8311 tree chain
8312 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8313 in_decl,
8314 /*integral_constant_expression_p=*/false);
8315 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8316 TREE_VALUE (t)
8317 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8318 chain);
8320 else
8321 TREE_VALUE (t)
8322 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8323 /*integral_constant_expression_p=*/false);
8324 *q = t;
8325 q = &TREE_CHAIN (t);
8327 else
8328 p = &TREE_CHAIN (t);
8331 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8335 /* Perform (or defer) access check for typedefs that were referenced
8336 from within the template TMPL code.
8337 This is a subroutine of instantiate_decl and instantiate_class_template.
8338 TMPL is the template to consider and TARGS is the list of arguments of
8339 that template. */
8341 static void
8342 perform_typedefs_access_check (tree tmpl, tree targs)
8344 location_t saved_location;
8345 int i;
8346 qualified_typedef_usage_t *iter;
8348 if (!tmpl
8349 || (!CLASS_TYPE_P (tmpl)
8350 && TREE_CODE (tmpl) != FUNCTION_DECL))
8351 return;
8353 saved_location = input_location;
8354 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
8355 get_types_needing_access_check (tmpl),
8356 i, iter)
8358 tree type_decl = iter->typedef_decl;
8359 tree type_scope = iter->context;
8361 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8362 continue;
8364 if (uses_template_parms (type_decl))
8365 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8366 if (uses_template_parms (type_scope))
8367 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8369 /* Make access check error messages point to the location
8370 of the use of the typedef. */
8371 input_location = iter->locus;
8372 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8373 type_decl, type_decl);
8375 input_location = saved_location;
8378 static tree
8379 instantiate_class_template_1 (tree type)
8381 tree templ, args, pattern, t, member;
8382 tree typedecl;
8383 tree pbinfo;
8384 tree base_list;
8385 unsigned int saved_maximum_field_alignment;
8386 tree fn_context;
8388 if (type == error_mark_node)
8389 return error_mark_node;
8391 if (COMPLETE_OR_OPEN_TYPE_P (type)
8392 || uses_template_parms (type))
8393 return type;
8395 /* Figure out which template is being instantiated. */
8396 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8397 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8399 /* Determine what specialization of the original template to
8400 instantiate. */
8401 t = most_specialized_class (type, templ, tf_warning_or_error);
8402 if (t == error_mark_node)
8404 TYPE_BEING_DEFINED (type) = 1;
8405 return error_mark_node;
8407 else if (t)
8409 /* This TYPE is actually an instantiation of a partial
8410 specialization. We replace the innermost set of ARGS with
8411 the arguments appropriate for substitution. For example,
8412 given:
8414 template <class T> struct S {};
8415 template <class T> struct S<T*> {};
8417 and supposing that we are instantiating S<int*>, ARGS will
8418 presently be {int*} -- but we need {int}. */
8419 pattern = TREE_TYPE (t);
8420 args = TREE_PURPOSE (t);
8422 else
8424 pattern = TREE_TYPE (templ);
8425 args = CLASSTYPE_TI_ARGS (type);
8428 /* If the template we're instantiating is incomplete, then clearly
8429 there's nothing we can do. */
8430 if (!COMPLETE_TYPE_P (pattern))
8431 return type;
8433 /* If we've recursively instantiated too many templates, stop. */
8434 if (! push_tinst_level (type))
8435 return type;
8437 /* Now we're really doing the instantiation. Mark the type as in
8438 the process of being defined. */
8439 TYPE_BEING_DEFINED (type) = 1;
8441 /* We may be in the middle of deferred access check. Disable
8442 it now. */
8443 push_deferring_access_checks (dk_no_deferred);
8445 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8446 if (!fn_context)
8447 push_to_top_level ();
8448 /* Use #pragma pack from the template context. */
8449 saved_maximum_field_alignment = maximum_field_alignment;
8450 maximum_field_alignment = TYPE_PRECISION (pattern);
8452 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8454 /* Set the input location to the most specialized template definition.
8455 This is needed if tsubsting causes an error. */
8456 typedecl = TYPE_MAIN_DECL (pattern);
8457 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8458 DECL_SOURCE_LOCATION (typedecl);
8460 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8461 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8462 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8463 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8464 if (ANON_AGGR_TYPE_P (pattern))
8465 SET_ANON_AGGR_TYPE_P (type);
8466 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8468 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8469 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8470 /* Adjust visibility for template arguments. */
8471 determine_visibility (TYPE_MAIN_DECL (type));
8473 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8475 pbinfo = TYPE_BINFO (pattern);
8477 /* We should never instantiate a nested class before its enclosing
8478 class; we need to look up the nested class by name before we can
8479 instantiate it, and that lookup should instantiate the enclosing
8480 class. */
8481 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8482 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8484 base_list = NULL_TREE;
8485 if (BINFO_N_BASE_BINFOS (pbinfo))
8487 tree pbase_binfo;
8488 tree pushed_scope;
8489 int i;
8491 /* We must enter the scope containing the type, as that is where
8492 the accessibility of types named in dependent bases are
8493 looked up from. */
8494 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8496 /* Substitute into each of the bases to determine the actual
8497 basetypes. */
8498 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8500 tree base;
8501 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8502 tree expanded_bases = NULL_TREE;
8503 int idx, len = 1;
8505 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8507 expanded_bases =
8508 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8509 args, tf_error, NULL_TREE);
8510 if (expanded_bases == error_mark_node)
8511 continue;
8513 len = TREE_VEC_LENGTH (expanded_bases);
8516 for (idx = 0; idx < len; idx++)
8518 if (expanded_bases)
8519 /* Extract the already-expanded base class. */
8520 base = TREE_VEC_ELT (expanded_bases, idx);
8521 else
8522 /* Substitute to figure out the base class. */
8523 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8524 NULL_TREE);
8526 if (base == error_mark_node)
8527 continue;
8529 base_list = tree_cons (access, base, base_list);
8530 if (BINFO_VIRTUAL_P (pbase_binfo))
8531 TREE_TYPE (base_list) = integer_type_node;
8535 /* The list is now in reverse order; correct that. */
8536 base_list = nreverse (base_list);
8538 if (pushed_scope)
8539 pop_scope (pushed_scope);
8541 /* Now call xref_basetypes to set up all the base-class
8542 information. */
8543 xref_basetypes (type, base_list);
8545 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8546 (int) ATTR_FLAG_TYPE_IN_PLACE,
8547 args, tf_error, NULL_TREE);
8548 fixup_attribute_variants (type);
8550 /* Now that our base classes are set up, enter the scope of the
8551 class, so that name lookups into base classes, etc. will work
8552 correctly. This is precisely analogous to what we do in
8553 begin_class_definition when defining an ordinary non-template
8554 class, except we also need to push the enclosing classes. */
8555 push_nested_class (type);
8557 /* Now members are processed in the order of declaration. */
8558 for (member = CLASSTYPE_DECL_LIST (pattern);
8559 member; member = TREE_CHAIN (member))
8561 tree t = TREE_VALUE (member);
8563 if (TREE_PURPOSE (member))
8565 if (TYPE_P (t))
8567 /* Build new CLASSTYPE_NESTED_UTDS. */
8569 tree newtag;
8570 bool class_template_p;
8572 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8573 && TYPE_LANG_SPECIFIC (t)
8574 && CLASSTYPE_IS_TEMPLATE (t));
8575 /* If the member is a class template, then -- even after
8576 substitution -- there may be dependent types in the
8577 template argument list for the class. We increment
8578 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8579 that function will assume that no types are dependent
8580 when outside of a template. */
8581 if (class_template_p)
8582 ++processing_template_decl;
8583 newtag = tsubst (t, args, tf_error, NULL_TREE);
8584 if (class_template_p)
8585 --processing_template_decl;
8586 if (newtag == error_mark_node)
8587 continue;
8589 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8591 tree name = TYPE_IDENTIFIER (t);
8593 if (class_template_p)
8594 /* Unfortunately, lookup_template_class sets
8595 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8596 instantiation (i.e., for the type of a member
8597 template class nested within a template class.)
8598 This behavior is required for
8599 maybe_process_partial_specialization to work
8600 correctly, but is not accurate in this case;
8601 the TAG is not an instantiation of anything.
8602 (The corresponding TEMPLATE_DECL is an
8603 instantiation, but the TYPE is not.) */
8604 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8606 /* Now, we call pushtag to put this NEWTAG into the scope of
8607 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8608 pushtag calling push_template_decl. We don't have to do
8609 this for enums because it will already have been done in
8610 tsubst_enum. */
8611 if (name)
8612 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8613 pushtag (name, newtag, /*tag_scope=*/ts_current);
8616 else if (TREE_CODE (t) == FUNCTION_DECL
8617 || DECL_FUNCTION_TEMPLATE_P (t))
8619 /* Build new TYPE_METHODS. */
8620 tree r;
8622 if (TREE_CODE (t) == TEMPLATE_DECL)
8623 ++processing_template_decl;
8624 r = tsubst (t, args, tf_error, NULL_TREE);
8625 if (TREE_CODE (t) == TEMPLATE_DECL)
8626 --processing_template_decl;
8627 set_current_access_from_decl (r);
8628 finish_member_declaration (r);
8629 /* Instantiate members marked with attribute used. */
8630 if (r != error_mark_node && DECL_PRESERVE_P (r))
8631 mark_used (r);
8633 else
8635 /* Build new TYPE_FIELDS. */
8636 if (TREE_CODE (t) == STATIC_ASSERT)
8638 tree condition;
8640 ++c_inhibit_evaluation_warnings;
8641 condition =
8642 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8643 tf_warning_or_error, NULL_TREE,
8644 /*integral_constant_expression_p=*/true);
8645 --c_inhibit_evaluation_warnings;
8647 finish_static_assert (condition,
8648 STATIC_ASSERT_MESSAGE (t),
8649 STATIC_ASSERT_SOURCE_LOCATION (t),
8650 /*member_p=*/true);
8652 else if (TREE_CODE (t) != CONST_DECL)
8654 tree r;
8656 /* The file and line for this declaration, to
8657 assist in error message reporting. Since we
8658 called push_tinst_level above, we don't need to
8659 restore these. */
8660 input_location = DECL_SOURCE_LOCATION (t);
8662 if (TREE_CODE (t) == TEMPLATE_DECL)
8663 ++processing_template_decl;
8664 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8665 if (TREE_CODE (t) == TEMPLATE_DECL)
8666 --processing_template_decl;
8667 if (TREE_CODE (r) == VAR_DECL)
8669 /* In [temp.inst]:
8671 [t]he initialization (and any associated
8672 side-effects) of a static data member does
8673 not occur unless the static data member is
8674 itself used in a way that requires the
8675 definition of the static data member to
8676 exist.
8678 Therefore, we do not substitute into the
8679 initialized for the static data member here. */
8680 finish_static_data_member_decl
8682 /*init=*/NULL_TREE,
8683 /*init_const_expr_p=*/false,
8684 /*asmspec_tree=*/NULL_TREE,
8685 /*flags=*/0);
8686 /* Instantiate members marked with attribute used. */
8687 if (r != error_mark_node && DECL_PRESERVE_P (r))
8688 mark_used (r);
8690 else if (TREE_CODE (r) == FIELD_DECL)
8692 /* Determine whether R has a valid type and can be
8693 completed later. If R is invalid, then it is
8694 replaced by error_mark_node so that it will not be
8695 added to TYPE_FIELDS. */
8696 tree rtype = TREE_TYPE (r);
8697 if (can_complete_type_without_circularity (rtype))
8698 complete_type (rtype);
8700 if (!COMPLETE_TYPE_P (rtype))
8702 cxx_incomplete_type_error (r, rtype);
8703 r = error_mark_node;
8707 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8708 such a thing will already have been added to the field
8709 list by tsubst_enum in finish_member_declaration in the
8710 CLASSTYPE_NESTED_UTDS case above. */
8711 if (!(TREE_CODE (r) == TYPE_DECL
8712 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8713 && DECL_ARTIFICIAL (r)))
8715 set_current_access_from_decl (r);
8716 finish_member_declaration (r);
8721 else
8723 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8725 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8727 tree friend_type = t;
8728 bool adjust_processing_template_decl = false;
8730 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8732 /* template <class T> friend class C; */
8733 friend_type = tsubst_friend_class (friend_type, args);
8734 adjust_processing_template_decl = true;
8736 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8738 /* template <class T> friend class C::D; */
8739 friend_type = tsubst (friend_type, args,
8740 tf_warning_or_error, NULL_TREE);
8741 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8742 friend_type = TREE_TYPE (friend_type);
8743 adjust_processing_template_decl = true;
8745 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8746 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8748 /* This could be either
8750 friend class T::C;
8752 when dependent_type_p is false or
8754 template <class U> friend class T::C;
8756 otherwise. */
8757 friend_type = tsubst (friend_type, args,
8758 tf_warning_or_error, NULL_TREE);
8759 /* Bump processing_template_decl for correct
8760 dependent_type_p calculation. */
8761 ++processing_template_decl;
8762 if (dependent_type_p (friend_type))
8763 adjust_processing_template_decl = true;
8764 --processing_template_decl;
8766 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8767 && hidden_name_p (TYPE_NAME (friend_type)))
8769 /* friend class C;
8771 where C hasn't been declared yet. Let's lookup name
8772 from namespace scope directly, bypassing any name that
8773 come from dependent base class. */
8774 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8776 /* The call to xref_tag_from_type does injection for friend
8777 classes. */
8778 push_nested_namespace (ns);
8779 friend_type =
8780 xref_tag_from_type (friend_type, NULL_TREE,
8781 /*tag_scope=*/ts_current);
8782 pop_nested_namespace (ns);
8784 else if (uses_template_parms (friend_type))
8785 /* friend class C<T>; */
8786 friend_type = tsubst (friend_type, args,
8787 tf_warning_or_error, NULL_TREE);
8788 /* Otherwise it's
8790 friend class C;
8792 where C is already declared or
8794 friend class C<int>;
8796 We don't have to do anything in these cases. */
8798 if (adjust_processing_template_decl)
8799 /* Trick make_friend_class into realizing that the friend
8800 we're adding is a template, not an ordinary class. It's
8801 important that we use make_friend_class since it will
8802 perform some error-checking and output cross-reference
8803 information. */
8804 ++processing_template_decl;
8806 if (friend_type != error_mark_node)
8807 make_friend_class (type, friend_type, /*complain=*/false);
8809 if (adjust_processing_template_decl)
8810 --processing_template_decl;
8812 else
8814 /* Build new DECL_FRIENDLIST. */
8815 tree r;
8817 /* The file and line for this declaration, to
8818 assist in error message reporting. Since we
8819 called push_tinst_level above, we don't need to
8820 restore these. */
8821 input_location = DECL_SOURCE_LOCATION (t);
8823 if (TREE_CODE (t) == TEMPLATE_DECL)
8825 ++processing_template_decl;
8826 push_deferring_access_checks (dk_no_check);
8829 r = tsubst_friend_function (t, args);
8830 add_friend (type, r, /*complain=*/false);
8831 if (TREE_CODE (t) == TEMPLATE_DECL)
8833 pop_deferring_access_checks ();
8834 --processing_template_decl;
8840 if (CLASSTYPE_LAMBDA_EXPR (type))
8842 tree decl = lambda_function (type);
8843 if (decl)
8845 tree lam = CLASSTYPE_LAMBDA_EXPR (type);
8846 LAMBDA_EXPR_THIS_CAPTURE (lam)
8847 = lookup_field_1 (type, get_identifier ("__this"), false);
8849 instantiate_decl (decl, false, false);
8850 maybe_add_lambda_conv_op (type);
8852 LAMBDA_EXPR_THIS_CAPTURE (lam) = NULL_TREE;
8854 else
8855 gcc_assert (errorcount);
8858 /* Set the file and line number information to whatever is given for
8859 the class itself. This puts error messages involving generated
8860 implicit functions at a predictable point, and the same point
8861 that would be used for non-template classes. */
8862 input_location = DECL_SOURCE_LOCATION (typedecl);
8864 unreverse_member_declarations (type);
8865 finish_struct_1 (type);
8866 TYPE_BEING_DEFINED (type) = 0;
8868 /* We don't instantiate default arguments for member functions. 14.7.1:
8870 The implicit instantiation of a class template specialization causes
8871 the implicit instantiation of the declarations, but not of the
8872 definitions or default arguments, of the class member functions,
8873 member classes, static data members and member templates.... */
8875 /* Some typedefs referenced from within the template code need to be access
8876 checked at template instantiation time, i.e now. These types were
8877 added to the template at parsing time. Let's get those and perform
8878 the access checks then. */
8879 perform_typedefs_access_check (pattern, args);
8880 perform_deferred_access_checks ();
8881 pop_nested_class ();
8882 maximum_field_alignment = saved_maximum_field_alignment;
8883 if (!fn_context)
8884 pop_from_top_level ();
8885 pop_deferring_access_checks ();
8886 pop_tinst_level ();
8888 /* The vtable for a template class can be emitted in any translation
8889 unit in which the class is instantiated. When there is no key
8890 method, however, finish_struct_1 will already have added TYPE to
8891 the keyed_classes list. */
8892 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8893 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8895 return type;
8898 /* Wrapper for instantiate_class_template_1. */
8900 tree
8901 instantiate_class_template (tree type)
8903 tree ret;
8904 timevar_push (TV_TEMPLATE_INST);
8905 ret = instantiate_class_template_1 (type);
8906 timevar_pop (TV_TEMPLATE_INST);
8907 return ret;
8910 static tree
8911 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8913 tree r;
8915 if (!t)
8916 r = t;
8917 else if (TYPE_P (t))
8918 r = tsubst (t, args, complain, in_decl);
8919 else
8921 if (!(complain & tf_warning))
8922 ++c_inhibit_evaluation_warnings;
8923 r = tsubst_expr (t, args, complain, in_decl,
8924 /*integral_constant_expression_p=*/true);
8925 if (!(complain & tf_warning))
8926 --c_inhibit_evaluation_warnings;
8927 /* Preserve the raw-reference nature of T. */
8928 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
8929 && REFERENCE_REF_P (r))
8930 r = TREE_OPERAND (r, 0);
8932 return r;
8935 /* Given a function parameter pack TMPL_PARM and some function parameters
8936 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
8937 and set *SPEC_P to point at the next point in the list. */
8939 static tree
8940 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
8942 /* Collect all of the extra "packed" parameters into an
8943 argument pack. */
8944 tree parmvec;
8945 tree parmtypevec;
8946 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8947 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8948 tree spec_parm = *spec_p;
8949 int i, len;
8951 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
8952 if (tmpl_parm
8953 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
8954 break;
8956 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8957 parmvec = make_tree_vec (len);
8958 parmtypevec = make_tree_vec (len);
8959 spec_parm = *spec_p;
8960 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8962 TREE_VEC_ELT (parmvec, i) = spec_parm;
8963 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8966 /* Build the argument packs. */
8967 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8968 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8969 TREE_TYPE (argpack) = argtypepack;
8970 *spec_p = spec_parm;
8972 return argpack;
8975 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8976 NONTYPE_ARGUMENT_PACK. */
8978 static tree
8979 make_fnparm_pack (tree spec_parm)
8981 return extract_fnparm_pack (NULL_TREE, &spec_parm);
8984 /* Substitute ARGS into T, which is an pack expansion
8985 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8986 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8987 (if only a partial substitution could be performed) or
8988 ERROR_MARK_NODE if there was an error. */
8989 tree
8990 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8991 tree in_decl)
8993 tree pattern;
8994 tree pack, packs = NULL_TREE;
8995 bool unsubstituted_packs = false;
8996 bool real_packs = false;
8997 int missing_level = 0;
8998 int i, len = -1;
8999 tree result;
9000 struct pointer_map_t *saved_local_specializations = NULL;
9001 bool need_local_specializations = false;
9002 int levels;
9004 gcc_assert (PACK_EXPANSION_P (t));
9005 pattern = PACK_EXPANSION_PATTERN (t);
9007 /* Add in any args remembered from an earlier partial instantiation. */
9008 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9010 levels = TMPL_ARGS_DEPTH (args);
9012 /* Determine the argument packs that will instantiate the parameter
9013 packs used in the expansion expression. While we're at it,
9014 compute the number of arguments to be expanded and make sure it
9015 is consistent. */
9016 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9017 pack = TREE_CHAIN (pack))
9019 tree parm_pack = TREE_VALUE (pack);
9020 tree arg_pack = NULL_TREE;
9021 tree orig_arg = NULL_TREE;
9022 int level = 0;
9024 if (TREE_CODE (parm_pack) == BASES)
9026 if (BASES_DIRECT (parm_pack))
9027 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9028 args, complain, in_decl, false));
9029 else
9030 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9031 args, complain, in_decl, false));
9033 if (TREE_CODE (parm_pack) == PARM_DECL)
9035 if (PACK_EXPANSION_LOCAL_P (t))
9036 arg_pack = retrieve_local_specialization (parm_pack);
9037 else
9039 /* We can't rely on local_specializations for a parameter
9040 name used later in a function declaration (such as in a
9041 late-specified return type). Even if it exists, it might
9042 have the wrong value for a recursive call. Just make a
9043 dummy decl, since it's only used for its type. */
9044 arg_pack = tsubst_decl (parm_pack, args, complain);
9045 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9046 /* Partial instantiation of the parm_pack, we can't build
9047 up an argument pack yet. */
9048 arg_pack = NULL_TREE;
9049 else
9050 arg_pack = make_fnparm_pack (arg_pack);
9051 need_local_specializations = true;
9054 else
9056 int idx;
9057 template_parm_level_and_index (parm_pack, &level, &idx);
9059 if (level <= levels)
9060 arg_pack = TMPL_ARG (args, level, idx);
9063 orig_arg = arg_pack;
9064 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9065 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9067 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9068 /* This can only happen if we forget to expand an argument
9069 pack somewhere else. Just return an error, silently. */
9071 result = make_tree_vec (1);
9072 TREE_VEC_ELT (result, 0) = error_mark_node;
9073 return result;
9076 if (arg_from_parm_pack_p (arg_pack, parm_pack))
9077 /* The argument pack that the parameter maps to is just an
9078 expansion of the parameter itself, such as one would find
9079 in the implicit typedef of a class inside the class itself.
9080 Consider this parameter "unsubstituted", so that we will
9081 maintain the outer pack expansion. */
9082 arg_pack = NULL_TREE;
9084 if (arg_pack)
9086 int my_len =
9087 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9089 /* Don't bother trying to do a partial substitution with
9090 incomplete packs; we'll try again after deduction. */
9091 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9092 return t;
9094 if (len < 0)
9095 len = my_len;
9096 else if (len != my_len)
9098 if (!(complain & tf_error))
9099 /* Fail quietly. */;
9100 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9101 error ("mismatched argument pack lengths while expanding "
9102 "%<%T%>",
9103 pattern);
9104 else
9105 error ("mismatched argument pack lengths while expanding "
9106 "%<%E%>",
9107 pattern);
9108 return error_mark_node;
9111 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
9112 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack),
9113 0)))
9114 /* This isn't a real argument pack yet. */;
9115 else
9116 real_packs = true;
9118 /* Keep track of the parameter packs and their corresponding
9119 argument packs. */
9120 packs = tree_cons (parm_pack, arg_pack, packs);
9121 TREE_TYPE (packs) = orig_arg;
9123 else
9125 /* We can't substitute for this parameter pack. We use a flag as
9126 well as the missing_level counter because function parameter
9127 packs don't have a level. */
9128 unsubstituted_packs = true;
9129 if (!missing_level || missing_level > level)
9130 missing_level = level;
9134 /* We cannot expand this expansion expression, because we don't have
9135 all of the argument packs we need. */
9136 if (unsubstituted_packs)
9138 if (real_packs)
9140 /* We got some full packs, but we can't substitute them in until we
9141 have values for all the packs. So remember these until then. */
9142 tree save_args;
9144 t = make_pack_expansion (pattern);
9146 /* The call to add_to_template_args above assumes no overlap
9147 between saved args and new args, so prune away any fake
9148 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9149 if (missing_level && levels >= missing_level)
9151 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
9152 && missing_level > 1);
9153 TREE_VEC_LENGTH (args) = missing_level - 1;
9154 save_args = copy_node (args);
9155 TREE_VEC_LENGTH (args) = levels;
9157 else
9158 save_args = args;
9160 PACK_EXPANSION_EXTRA_ARGS (t) = save_args;
9162 else
9164 /* There were no real arguments, we're just replacing a parameter
9165 pack with another version of itself. Substitute into the
9166 pattern and return a PACK_EXPANSION_*. The caller will need to
9167 deal with that. */
9168 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9169 t = tsubst_expr (pattern, args, complain, in_decl,
9170 /*integral_constant_expression_p=*/false);
9171 else
9172 t = tsubst (pattern, args, complain, in_decl);
9173 t = make_pack_expansion (t);
9175 return t;
9178 /* We could not find any argument packs that work. */
9179 if (len < 0)
9180 return error_mark_node;
9182 if (need_local_specializations)
9184 /* We're in a late-specified return type, so create our own local
9185 specializations map; the current map is either NULL or (in the
9186 case of recursive unification) might have bindings that we don't
9187 want to use or alter. */
9188 saved_local_specializations = local_specializations;
9189 local_specializations = pointer_map_create ();
9192 /* For each argument in each argument pack, substitute into the
9193 pattern. */
9194 result = make_tree_vec (len);
9195 for (i = 0; i < len; ++i)
9197 /* For parameter pack, change the substitution of the parameter
9198 pack to the ith argument in its argument pack, then expand
9199 the pattern. */
9200 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9202 tree parm = TREE_PURPOSE (pack);
9203 tree arg;
9205 /* Select the Ith argument from the pack. */
9206 if (TREE_CODE (parm) == PARM_DECL)
9208 if (i == 0)
9210 arg = make_node (ARGUMENT_PACK_SELECT);
9211 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9212 mark_used (parm);
9213 register_local_specialization (arg, parm);
9215 else
9216 arg = retrieve_local_specialization (parm);
9218 else
9220 int idx, level;
9221 template_parm_level_and_index (parm, &level, &idx);
9223 if (i == 0)
9225 arg = make_node (ARGUMENT_PACK_SELECT);
9226 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9227 /* Update the corresponding argument. */
9228 TMPL_ARG (args, level, idx) = arg;
9230 else
9231 /* Re-use the ARGUMENT_PACK_SELECT. */
9232 arg = TMPL_ARG (args, level, idx);
9234 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
9237 /* Substitute into the PATTERN with the altered arguments. */
9238 if (!TYPE_P (pattern))
9239 TREE_VEC_ELT (result, i) =
9240 tsubst_expr (pattern, args, complain, in_decl,
9241 /*integral_constant_expression_p=*/false);
9242 else
9243 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
9245 if (TREE_VEC_ELT (result, i) == error_mark_node)
9247 result = error_mark_node;
9248 break;
9252 /* Update ARGS to restore the substitution from parameter packs to
9253 their argument packs. */
9254 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9256 tree parm = TREE_PURPOSE (pack);
9258 if (TREE_CODE (parm) == PARM_DECL)
9259 register_local_specialization (TREE_TYPE (pack), parm);
9260 else
9262 int idx, level;
9263 template_parm_level_and_index (parm, &level, &idx);
9265 /* Update the corresponding argument. */
9266 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9267 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9268 TREE_TYPE (pack);
9269 else
9270 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9274 if (need_local_specializations)
9276 pointer_map_destroy (local_specializations);
9277 local_specializations = saved_local_specializations;
9280 return result;
9283 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9284 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9285 parameter packs; all parms generated from a function parameter pack will
9286 have the same DECL_PARM_INDEX. */
9288 tree
9289 get_pattern_parm (tree parm, tree tmpl)
9291 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9292 tree patparm;
9294 if (DECL_ARTIFICIAL (parm))
9296 for (patparm = DECL_ARGUMENTS (pattern);
9297 patparm; patparm = DECL_CHAIN (patparm))
9298 if (DECL_ARTIFICIAL (patparm)
9299 && DECL_NAME (parm) == DECL_NAME (patparm))
9300 break;
9302 else
9304 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9305 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9306 gcc_assert (DECL_PARM_INDEX (patparm)
9307 == DECL_PARM_INDEX (parm));
9310 return patparm;
9313 /* Substitute ARGS into the vector or list of template arguments T. */
9315 static tree
9316 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9318 tree orig_t = t;
9319 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9320 tree *elts;
9322 if (t == error_mark_node)
9323 return error_mark_node;
9325 len = TREE_VEC_LENGTH (t);
9326 elts = XALLOCAVEC (tree, len);
9328 for (i = 0; i < len; i++)
9330 tree orig_arg = TREE_VEC_ELT (t, i);
9331 tree new_arg;
9333 if (TREE_CODE (orig_arg) == TREE_VEC)
9334 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9335 else if (PACK_EXPANSION_P (orig_arg))
9337 /* Substitute into an expansion expression. */
9338 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9340 if (TREE_CODE (new_arg) == TREE_VEC)
9341 /* Add to the expanded length adjustment the number of
9342 expanded arguments. We subtract one from this
9343 measurement, because the argument pack expression
9344 itself is already counted as 1 in
9345 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9346 the argument pack is empty. */
9347 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9349 else if (ARGUMENT_PACK_P (orig_arg))
9351 /* Substitute into each of the arguments. */
9352 new_arg = TYPE_P (orig_arg)
9353 ? cxx_make_type (TREE_CODE (orig_arg))
9354 : make_node (TREE_CODE (orig_arg));
9356 SET_ARGUMENT_PACK_ARGS (
9357 new_arg,
9358 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9359 args, complain, in_decl));
9361 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9362 new_arg = error_mark_node;
9364 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9365 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9366 complain, in_decl);
9367 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9369 if (TREE_TYPE (new_arg) == error_mark_node)
9370 new_arg = error_mark_node;
9373 else
9374 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9376 if (new_arg == error_mark_node)
9377 return error_mark_node;
9379 elts[i] = new_arg;
9380 if (new_arg != orig_arg)
9381 need_new = 1;
9384 if (!need_new)
9385 return t;
9387 /* Make space for the expanded arguments coming from template
9388 argument packs. */
9389 t = make_tree_vec (len + expanded_len_adjust);
9390 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9391 arguments for a member template.
9392 In that case each TREE_VEC in ORIG_T represents a level of template
9393 arguments, and ORIG_T won't carry any non defaulted argument count.
9394 It will rather be the nested TREE_VECs that will carry one.
9395 In other words, ORIG_T carries a non defaulted argument count only
9396 if it doesn't contain any nested TREE_VEC. */
9397 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9399 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9400 count += expanded_len_adjust;
9401 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9403 for (i = 0, out = 0; i < len; i++)
9405 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9406 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9407 && TREE_CODE (elts[i]) == TREE_VEC)
9409 int idx;
9411 /* Now expand the template argument pack "in place". */
9412 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9413 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9415 else
9417 TREE_VEC_ELT (t, out) = elts[i];
9418 out++;
9422 return t;
9425 /* Return the result of substituting ARGS into the template parameters
9426 given by PARMS. If there are m levels of ARGS and m + n levels of
9427 PARMS, then the result will contain n levels of PARMS. For
9428 example, if PARMS is `template <class T> template <class U>
9429 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9430 result will be `template <int*, double, class V>'. */
9432 static tree
9433 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9435 tree r = NULL_TREE;
9436 tree* new_parms;
9438 /* When substituting into a template, we must set
9439 PROCESSING_TEMPLATE_DECL as the template parameters may be
9440 dependent if they are based on one-another, and the dependency
9441 predicates are short-circuit outside of templates. */
9442 ++processing_template_decl;
9444 for (new_parms = &r;
9445 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9446 new_parms = &(TREE_CHAIN (*new_parms)),
9447 parms = TREE_CHAIN (parms))
9449 tree new_vec =
9450 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9451 int i;
9453 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9455 tree tuple;
9457 if (parms == error_mark_node)
9458 continue;
9460 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9462 if (tuple == error_mark_node)
9463 continue;
9465 TREE_VEC_ELT (new_vec, i) =
9466 tsubst_template_parm (tuple, args, complain);
9469 *new_parms =
9470 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9471 - TMPL_ARGS_DEPTH (args)),
9472 new_vec, NULL_TREE);
9475 --processing_template_decl;
9477 return r;
9480 /* Return the result of substituting ARGS into one template parameter
9481 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9482 parameter and which TREE_PURPOSE is the default argument of the
9483 template parameter. */
9485 static tree
9486 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9488 tree default_value, parm_decl;
9490 if (args == NULL_TREE
9491 || t == NULL_TREE
9492 || t == error_mark_node)
9493 return t;
9495 gcc_assert (TREE_CODE (t) == TREE_LIST);
9497 default_value = TREE_PURPOSE (t);
9498 parm_decl = TREE_VALUE (t);
9500 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9501 if (TREE_CODE (parm_decl) == PARM_DECL
9502 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9503 parm_decl = error_mark_node;
9504 default_value = tsubst_template_arg (default_value, args,
9505 complain, NULL_TREE);
9507 return build_tree_list (default_value, parm_decl);
9510 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9511 type T. If T is not an aggregate or enumeration type, it is
9512 handled as if by tsubst. IN_DECL is as for tsubst. If
9513 ENTERING_SCOPE is nonzero, T is the context for a template which
9514 we are presently tsubst'ing. Return the substituted value. */
9516 static tree
9517 tsubst_aggr_type (tree t,
9518 tree args,
9519 tsubst_flags_t complain,
9520 tree in_decl,
9521 int entering_scope)
9523 if (t == NULL_TREE)
9524 return NULL_TREE;
9526 switch (TREE_CODE (t))
9528 case RECORD_TYPE:
9529 if (TYPE_PTRMEMFUNC_P (t))
9530 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9532 /* Else fall through. */
9533 case ENUMERAL_TYPE:
9534 case UNION_TYPE:
9535 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9537 tree argvec;
9538 tree context;
9539 tree r;
9540 int saved_unevaluated_operand;
9541 int saved_inhibit_evaluation_warnings;
9543 /* In "sizeof(X<I>)" we need to evaluate "I". */
9544 saved_unevaluated_operand = cp_unevaluated_operand;
9545 cp_unevaluated_operand = 0;
9546 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9547 c_inhibit_evaluation_warnings = 0;
9549 /* First, determine the context for the type we are looking
9550 up. */
9551 context = TYPE_CONTEXT (t);
9552 if (context && TYPE_P (context))
9554 context = tsubst_aggr_type (context, args, complain,
9555 in_decl, /*entering_scope=*/1);
9556 /* If context is a nested class inside a class template,
9557 it may still need to be instantiated (c++/33959). */
9558 context = complete_type (context);
9561 /* Then, figure out what arguments are appropriate for the
9562 type we are trying to find. For example, given:
9564 template <class T> struct S;
9565 template <class T, class U> void f(T, U) { S<U> su; }
9567 and supposing that we are instantiating f<int, double>,
9568 then our ARGS will be {int, double}, but, when looking up
9569 S we only want {double}. */
9570 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9571 complain, in_decl);
9572 if (argvec == error_mark_node)
9573 r = error_mark_node;
9574 else
9576 r = lookup_template_class (t, argvec, in_decl, context,
9577 entering_scope, complain);
9578 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9581 cp_unevaluated_operand = saved_unevaluated_operand;
9582 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9584 return r;
9586 else
9587 /* This is not a template type, so there's nothing to do. */
9588 return t;
9590 default:
9591 return tsubst (t, args, complain, in_decl);
9595 /* Substitute into the default argument ARG (a default argument for
9596 FN), which has the indicated TYPE. */
9598 tree
9599 tsubst_default_argument (tree fn, tree type, tree arg)
9601 tree saved_class_ptr = NULL_TREE;
9602 tree saved_class_ref = NULL_TREE;
9604 /* This can happen in invalid code. */
9605 if (TREE_CODE (arg) == DEFAULT_ARG)
9606 return arg;
9608 /* This default argument came from a template. Instantiate the
9609 default argument here, not in tsubst. In the case of
9610 something like:
9612 template <class T>
9613 struct S {
9614 static T t();
9615 void f(T = t());
9618 we must be careful to do name lookup in the scope of S<T>,
9619 rather than in the current class. */
9620 push_access_scope (fn);
9621 /* The "this" pointer is not valid in a default argument. */
9622 if (cfun)
9624 saved_class_ptr = current_class_ptr;
9625 cp_function_chain->x_current_class_ptr = NULL_TREE;
9626 saved_class_ref = current_class_ref;
9627 cp_function_chain->x_current_class_ref = NULL_TREE;
9630 push_deferring_access_checks(dk_no_deferred);
9631 /* The default argument expression may cause implicitly defined
9632 member functions to be synthesized, which will result in garbage
9633 collection. We must treat this situation as if we were within
9634 the body of function so as to avoid collecting live data on the
9635 stack. */
9636 ++function_depth;
9637 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9638 tf_warning_or_error, NULL_TREE,
9639 /*integral_constant_expression_p=*/false);
9640 --function_depth;
9641 pop_deferring_access_checks();
9643 /* Restore the "this" pointer. */
9644 if (cfun)
9646 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9647 cp_function_chain->x_current_class_ref = saved_class_ref;
9650 /* Make sure the default argument is reasonable. */
9651 arg = check_default_argument (type, arg);
9653 pop_access_scope (fn);
9655 return arg;
9658 /* Substitute into all the default arguments for FN. */
9660 static void
9661 tsubst_default_arguments (tree fn)
9663 tree arg;
9664 tree tmpl_args;
9666 tmpl_args = DECL_TI_ARGS (fn);
9668 /* If this function is not yet instantiated, we certainly don't need
9669 its default arguments. */
9670 if (uses_template_parms (tmpl_args))
9671 return;
9672 /* Don't do this again for clones. */
9673 if (DECL_CLONED_FUNCTION_P (fn))
9674 return;
9676 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9677 arg;
9678 arg = TREE_CHAIN (arg))
9679 if (TREE_PURPOSE (arg))
9680 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9681 TREE_VALUE (arg),
9682 TREE_PURPOSE (arg));
9685 /* Substitute the ARGS into the T, which is a _DECL. Return the
9686 result of the substitution. Issue error and warning messages under
9687 control of COMPLAIN. */
9689 static tree
9690 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9692 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9693 location_t saved_loc;
9694 tree r = NULL_TREE;
9695 tree in_decl = t;
9696 hashval_t hash = 0;
9698 /* Set the filename and linenumber to improve error-reporting. */
9699 saved_loc = input_location;
9700 input_location = DECL_SOURCE_LOCATION (t);
9702 switch (TREE_CODE (t))
9704 case TEMPLATE_DECL:
9706 /* We can get here when processing a member function template,
9707 member class template, or template template parameter. */
9708 tree decl = DECL_TEMPLATE_RESULT (t);
9709 tree spec;
9710 tree tmpl_args;
9711 tree full_args;
9713 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9715 /* Template template parameter is treated here. */
9716 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9717 if (new_type == error_mark_node)
9718 RETURN (error_mark_node);
9720 r = copy_decl (t);
9721 DECL_CHAIN (r) = NULL_TREE;
9722 TREE_TYPE (r) = new_type;
9723 DECL_TEMPLATE_RESULT (r)
9724 = build_decl (DECL_SOURCE_LOCATION (decl),
9725 TYPE_DECL, DECL_NAME (decl), new_type);
9726 DECL_TEMPLATE_PARMS (r)
9727 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9728 complain);
9729 TYPE_NAME (new_type) = r;
9730 break;
9733 /* We might already have an instance of this template.
9734 The ARGS are for the surrounding class type, so the
9735 full args contain the tsubst'd args for the context,
9736 plus the innermost args from the template decl. */
9737 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9738 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9739 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9740 /* Because this is a template, the arguments will still be
9741 dependent, even after substitution. If
9742 PROCESSING_TEMPLATE_DECL is not set, the dependency
9743 predicates will short-circuit. */
9744 ++processing_template_decl;
9745 full_args = tsubst_template_args (tmpl_args, args,
9746 complain, in_decl);
9747 --processing_template_decl;
9748 if (full_args == error_mark_node)
9749 RETURN (error_mark_node);
9751 /* If this is a default template template argument,
9752 tsubst might not have changed anything. */
9753 if (full_args == tmpl_args)
9754 RETURN (t);
9756 hash = hash_tmpl_and_args (t, full_args);
9757 spec = retrieve_specialization (t, full_args, hash);
9758 if (spec != NULL_TREE)
9760 r = spec;
9761 break;
9764 /* Make a new template decl. It will be similar to the
9765 original, but will record the current template arguments.
9766 We also create a new function declaration, which is just
9767 like the old one, but points to this new template, rather
9768 than the old one. */
9769 r = copy_decl (t);
9770 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9771 DECL_CHAIN (r) = NULL_TREE;
9773 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9775 if (TREE_CODE (decl) == TYPE_DECL
9776 && !TYPE_DECL_ALIAS_P (decl))
9778 tree new_type;
9779 ++processing_template_decl;
9780 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9781 --processing_template_decl;
9782 if (new_type == error_mark_node)
9783 RETURN (error_mark_node);
9785 TREE_TYPE (r) = new_type;
9786 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9787 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9788 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9789 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9791 else
9793 tree new_decl;
9794 ++processing_template_decl;
9795 new_decl = tsubst (decl, args, complain, in_decl);
9796 --processing_template_decl;
9797 if (new_decl == error_mark_node)
9798 RETURN (error_mark_node);
9800 DECL_TEMPLATE_RESULT (r) = new_decl;
9801 DECL_TI_TEMPLATE (new_decl) = r;
9802 TREE_TYPE (r) = TREE_TYPE (new_decl);
9803 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9804 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9807 SET_DECL_IMPLICIT_INSTANTIATION (r);
9808 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9809 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9811 /* The template parameters for this new template are all the
9812 template parameters for the old template, except the
9813 outermost level of parameters. */
9814 DECL_TEMPLATE_PARMS (r)
9815 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9816 complain);
9818 if (PRIMARY_TEMPLATE_P (t))
9819 DECL_PRIMARY_TEMPLATE (r) = r;
9821 if (TREE_CODE (decl) != TYPE_DECL)
9822 /* Record this non-type partial instantiation. */
9823 register_specialization (r, t,
9824 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9825 false, hash);
9827 break;
9829 case FUNCTION_DECL:
9831 tree ctx;
9832 tree argvec = NULL_TREE;
9833 tree *friends;
9834 tree gen_tmpl;
9835 tree type;
9836 int member;
9837 int args_depth;
9838 int parms_depth;
9840 /* Nobody should be tsubst'ing into non-template functions. */
9841 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9843 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9845 tree spec;
9846 bool dependent_p;
9848 /* If T is not dependent, just return it. We have to
9849 increment PROCESSING_TEMPLATE_DECL because
9850 value_dependent_expression_p assumes that nothing is
9851 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9852 ++processing_template_decl;
9853 dependent_p = value_dependent_expression_p (t);
9854 --processing_template_decl;
9855 if (!dependent_p)
9856 RETURN (t);
9858 /* Calculate the most general template of which R is a
9859 specialization, and the complete set of arguments used to
9860 specialize R. */
9861 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9862 argvec = tsubst_template_args (DECL_TI_ARGS
9863 (DECL_TEMPLATE_RESULT
9864 (DECL_TI_TEMPLATE (t))),
9865 args, complain, in_decl);
9866 if (argvec == error_mark_node)
9867 RETURN (error_mark_node);
9869 /* Check to see if we already have this specialization. */
9870 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9871 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9873 if (spec)
9875 r = spec;
9876 break;
9879 /* We can see more levels of arguments than parameters if
9880 there was a specialization of a member template, like
9881 this:
9883 template <class T> struct S { template <class U> void f(); }
9884 template <> template <class U> void S<int>::f(U);
9886 Here, we'll be substituting into the specialization,
9887 because that's where we can find the code we actually
9888 want to generate, but we'll have enough arguments for
9889 the most general template.
9891 We also deal with the peculiar case:
9893 template <class T> struct S {
9894 template <class U> friend void f();
9896 template <class U> void f() {}
9897 template S<int>;
9898 template void f<double>();
9900 Here, the ARGS for the instantiation of will be {int,
9901 double}. But, we only need as many ARGS as there are
9902 levels of template parameters in CODE_PATTERN. We are
9903 careful not to get fooled into reducing the ARGS in
9904 situations like:
9906 template <class T> struct S { template <class U> void f(U); }
9907 template <class T> template <> void S<T>::f(int) {}
9909 which we can spot because the pattern will be a
9910 specialization in this case. */
9911 args_depth = TMPL_ARGS_DEPTH (args);
9912 parms_depth =
9913 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9914 if (args_depth > parms_depth
9915 && !DECL_TEMPLATE_SPECIALIZATION (t))
9916 args = get_innermost_template_args (args, parms_depth);
9918 else
9920 /* This special case arises when we have something like this:
9922 template <class T> struct S {
9923 friend void f<int>(int, double);
9926 Here, the DECL_TI_TEMPLATE for the friend declaration
9927 will be an IDENTIFIER_NODE. We are being called from
9928 tsubst_friend_function, and we want only to create a
9929 new decl (R) with appropriate types so that we can call
9930 determine_specialization. */
9931 gen_tmpl = NULL_TREE;
9934 if (DECL_CLASS_SCOPE_P (t))
9936 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9937 member = 2;
9938 else
9939 member = 1;
9940 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9941 complain, t, /*entering_scope=*/1);
9943 else
9945 member = 0;
9946 ctx = DECL_CONTEXT (t);
9948 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9949 if (type == error_mark_node)
9950 RETURN (error_mark_node);
9952 /* We do NOT check for matching decls pushed separately at this
9953 point, as they may not represent instantiations of this
9954 template, and in any case are considered separate under the
9955 discrete model. */
9956 r = copy_decl (t);
9957 DECL_USE_TEMPLATE (r) = 0;
9958 TREE_TYPE (r) = type;
9959 /* Clear out the mangled name and RTL for the instantiation. */
9960 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9961 SET_DECL_RTL (r, NULL);
9962 /* Leave DECL_INITIAL set on deleted instantiations. */
9963 if (!DECL_DELETED_FN (r))
9964 DECL_INITIAL (r) = NULL_TREE;
9965 DECL_CONTEXT (r) = ctx;
9967 if (member && DECL_CONV_FN_P (r))
9968 /* Type-conversion operator. Reconstruct the name, in
9969 case it's the name of one of the template's parameters. */
9970 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9972 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9973 complain, t);
9974 DECL_RESULT (r) = NULL_TREE;
9976 TREE_STATIC (r) = 0;
9977 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9978 DECL_EXTERNAL (r) = 1;
9979 /* If this is an instantiation of a function with internal
9980 linkage, we already know what object file linkage will be
9981 assigned to the instantiation. */
9982 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9983 DECL_DEFER_OUTPUT (r) = 0;
9984 DECL_CHAIN (r) = NULL_TREE;
9985 DECL_PENDING_INLINE_INFO (r) = 0;
9986 DECL_PENDING_INLINE_P (r) = 0;
9987 DECL_SAVED_TREE (r) = NULL_TREE;
9988 DECL_STRUCT_FUNCTION (r) = NULL;
9989 TREE_USED (r) = 0;
9990 /* We'll re-clone as appropriate in instantiate_template. */
9991 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9993 /* If we aren't complaining now, return on error before we register
9994 the specialization so that we'll complain eventually. */
9995 if ((complain & tf_error) == 0
9996 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9997 && !grok_op_properties (r, /*complain=*/false))
9998 RETURN (error_mark_node);
10000 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10001 this in the special friend case mentioned above where
10002 GEN_TMPL is NULL. */
10003 if (gen_tmpl)
10005 DECL_TEMPLATE_INFO (r)
10006 = build_template_info (gen_tmpl, argvec);
10007 SET_DECL_IMPLICIT_INSTANTIATION (r);
10008 register_specialization (r, gen_tmpl, argvec, false, hash);
10010 /* We're not supposed to instantiate default arguments
10011 until they are called, for a template. But, for a
10012 declaration like:
10014 template <class T> void f ()
10015 { extern void g(int i = T()); }
10017 we should do the substitution when the template is
10018 instantiated. We handle the member function case in
10019 instantiate_class_template since the default arguments
10020 might refer to other members of the class. */
10021 if (!member
10022 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10023 && !uses_template_parms (argvec))
10024 tsubst_default_arguments (r);
10026 else
10027 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10029 /* Copy the list of befriending classes. */
10030 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10031 *friends;
10032 friends = &TREE_CHAIN (*friends))
10034 *friends = copy_node (*friends);
10035 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10036 args, complain,
10037 in_decl);
10040 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10042 maybe_retrofit_in_chrg (r);
10043 if (DECL_CONSTRUCTOR_P (r))
10044 grok_ctor_properties (ctx, r);
10045 /* If this is an instantiation of a member template, clone it.
10046 If it isn't, that'll be handled by
10047 clone_constructors_and_destructors. */
10048 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10049 clone_function_decl (r, /*update_method_vec_p=*/0);
10051 else if ((complain & tf_error) != 0
10052 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10053 && !grok_op_properties (r, /*complain=*/true))
10054 RETURN (error_mark_node);
10056 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10057 SET_DECL_FRIEND_CONTEXT (r,
10058 tsubst (DECL_FRIEND_CONTEXT (t),
10059 args, complain, in_decl));
10061 /* Possibly limit visibility based on template args. */
10062 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10063 if (DECL_VISIBILITY_SPECIFIED (t))
10065 DECL_VISIBILITY_SPECIFIED (r) = 0;
10066 DECL_ATTRIBUTES (r)
10067 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10069 determine_visibility (r);
10070 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10071 && !processing_template_decl)
10072 defaulted_late_check (r);
10074 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10075 args, complain, in_decl);
10077 break;
10079 case PARM_DECL:
10081 tree type = NULL_TREE;
10082 int i, len = 1;
10083 tree expanded_types = NULL_TREE;
10084 tree prev_r = NULL_TREE;
10085 tree first_r = NULL_TREE;
10087 if (FUNCTION_PARAMETER_PACK_P (t))
10089 /* If there is a local specialization that isn't a
10090 parameter pack, it means that we're doing a "simple"
10091 substitution from inside tsubst_pack_expansion. Just
10092 return the local specialization (which will be a single
10093 parm). */
10094 tree spec = retrieve_local_specialization (t);
10095 if (spec
10096 && TREE_CODE (spec) == PARM_DECL
10097 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10098 RETURN (spec);
10100 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10101 the parameters in this function parameter pack. */
10102 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10103 complain, in_decl);
10104 if (TREE_CODE (expanded_types) == TREE_VEC)
10106 len = TREE_VEC_LENGTH (expanded_types);
10108 /* Zero-length parameter packs are boring. Just substitute
10109 into the chain. */
10110 if (len == 0)
10111 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10112 TREE_CHAIN (t)));
10114 else
10116 /* All we did was update the type. Make a note of that. */
10117 type = expanded_types;
10118 expanded_types = NULL_TREE;
10122 /* Loop through all of the parameter's we'll build. When T is
10123 a function parameter pack, LEN is the number of expanded
10124 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10125 r = NULL_TREE;
10126 for (i = 0; i < len; ++i)
10128 prev_r = r;
10129 r = copy_node (t);
10130 if (DECL_TEMPLATE_PARM_P (t))
10131 SET_DECL_TEMPLATE_PARM_P (r);
10133 if (expanded_types)
10134 /* We're on the Ith parameter of the function parameter
10135 pack. */
10137 /* An argument of a function parameter pack is not a parameter
10138 pack. */
10139 FUNCTION_PARAMETER_PACK_P (r) = false;
10141 /* Get the Ith type. */
10142 type = TREE_VEC_ELT (expanded_types, i);
10144 if (DECL_NAME (r))
10145 /* Rename the parameter to include the index. */
10146 DECL_NAME (r) =
10147 make_ith_pack_parameter_name (DECL_NAME (r), i);
10149 else if (!type)
10150 /* We're dealing with a normal parameter. */
10151 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10153 type = type_decays_to (type);
10154 TREE_TYPE (r) = type;
10155 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10157 if (DECL_INITIAL (r))
10159 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10160 DECL_INITIAL (r) = TREE_TYPE (r);
10161 else
10162 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10163 complain, in_decl);
10166 DECL_CONTEXT (r) = NULL_TREE;
10168 if (!DECL_TEMPLATE_PARM_P (r))
10169 DECL_ARG_TYPE (r) = type_passed_as (type);
10171 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10172 args, complain, in_decl);
10174 /* Keep track of the first new parameter we
10175 generate. That's what will be returned to the
10176 caller. */
10177 if (!first_r)
10178 first_r = r;
10180 /* Build a proper chain of parameters when substituting
10181 into a function parameter pack. */
10182 if (prev_r)
10183 DECL_CHAIN (prev_r) = r;
10186 /* If cp_unevaluated_operand is set, we're just looking for a
10187 single dummy parameter, so don't keep going. */
10188 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10189 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10190 complain, DECL_CHAIN (t));
10192 /* FIRST_R contains the start of the chain we've built. */
10193 r = first_r;
10195 break;
10197 case FIELD_DECL:
10199 tree type;
10201 r = copy_decl (t);
10202 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10203 if (type == error_mark_node)
10204 RETURN (error_mark_node);
10205 TREE_TYPE (r) = type;
10206 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10208 if (DECL_C_BIT_FIELD (r))
10209 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10210 non-bit-fields DECL_INITIAL is a non-static data member
10211 initializer, which gets deferred instantiation. */
10212 DECL_INITIAL (r)
10213 = tsubst_expr (DECL_INITIAL (t), args,
10214 complain, in_decl,
10215 /*integral_constant_expression_p=*/true);
10216 else if (DECL_INITIAL (t))
10218 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10219 NSDMI in perform_member_init. Still set DECL_INITIAL
10220 so that we know there is one. */
10221 DECL_INITIAL (r) = void_zero_node;
10222 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10223 retrofit_lang_decl (r);
10224 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10226 /* We don't have to set DECL_CONTEXT here; it is set by
10227 finish_member_declaration. */
10228 DECL_CHAIN (r) = NULL_TREE;
10229 if (VOID_TYPE_P (type))
10230 error ("instantiation of %q+D as type %qT", r, type);
10232 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10233 args, complain, in_decl);
10235 break;
10237 case USING_DECL:
10238 /* We reach here only for member using decls. */
10239 if (DECL_DEPENDENT_P (t))
10241 r = do_class_using_decl
10242 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
10243 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
10244 if (!r)
10245 r = error_mark_node;
10246 else
10248 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10249 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10252 else
10254 r = copy_node (t);
10255 DECL_CHAIN (r) = NULL_TREE;
10257 break;
10259 case TYPE_DECL:
10260 case VAR_DECL:
10262 tree argvec = NULL_TREE;
10263 tree gen_tmpl = NULL_TREE;
10264 tree spec;
10265 tree tmpl = NULL_TREE;
10266 tree ctx;
10267 tree type = NULL_TREE;
10268 bool local_p;
10270 if (TREE_CODE (t) == TYPE_DECL
10271 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10273 /* If this is the canonical decl, we don't have to
10274 mess with instantiations, and often we can't (for
10275 typename, template type parms and such). Note that
10276 TYPE_NAME is not correct for the above test if
10277 we've copied the type for a typedef. */
10278 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10279 if (type == error_mark_node)
10280 RETURN (error_mark_node);
10281 r = TYPE_NAME (type);
10282 break;
10285 /* Check to see if we already have the specialization we
10286 need. */
10287 spec = NULL_TREE;
10288 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10290 /* T is a static data member or namespace-scope entity.
10291 We have to substitute into namespace-scope variables
10292 (even though such entities are never templates) because
10293 of cases like:
10295 template <class T> void f() { extern T t; }
10297 where the entity referenced is not known until
10298 instantiation time. */
10299 local_p = false;
10300 ctx = DECL_CONTEXT (t);
10301 if (DECL_CLASS_SCOPE_P (t))
10303 ctx = tsubst_aggr_type (ctx, args,
10304 complain,
10305 in_decl, /*entering_scope=*/1);
10306 /* If CTX is unchanged, then T is in fact the
10307 specialization we want. That situation occurs when
10308 referencing a static data member within in its own
10309 class. We can use pointer equality, rather than
10310 same_type_p, because DECL_CONTEXT is always
10311 canonical... */
10312 if (ctx == DECL_CONTEXT (t)
10313 && (TREE_CODE (t) != TYPE_DECL
10314 /* ... unless T is a member template; in which
10315 case our caller can be willing to create a
10316 specialization of that template represented
10317 by T. */
10318 || !(DECL_TI_TEMPLATE (t)
10319 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10320 spec = t;
10323 if (!spec)
10325 tmpl = DECL_TI_TEMPLATE (t);
10326 gen_tmpl = most_general_template (tmpl);
10327 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10328 if (argvec == error_mark_node)
10329 RETURN (error_mark_node);
10330 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10331 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10334 else
10336 /* A local variable. */
10337 local_p = true;
10338 /* Subsequent calls to pushdecl will fill this in. */
10339 ctx = NULL_TREE;
10340 spec = retrieve_local_specialization (t);
10342 /* If we already have the specialization we need, there is
10343 nothing more to do. */
10344 if (spec)
10346 r = spec;
10347 break;
10350 /* Create a new node for the specialization we need. */
10351 r = copy_decl (t);
10352 if (type == NULL_TREE)
10354 if (is_typedef_decl (t))
10355 type = DECL_ORIGINAL_TYPE (t);
10356 else
10357 type = TREE_TYPE (t);
10358 if (TREE_CODE (t) == VAR_DECL
10359 && VAR_HAD_UNKNOWN_BOUND (t)
10360 && type != error_mark_node)
10361 type = strip_array_domain (type);
10362 type = tsubst (type, args, complain, in_decl);
10364 if (TREE_CODE (r) == VAR_DECL)
10366 /* Even if the original location is out of scope, the
10367 newly substituted one is not. */
10368 DECL_DEAD_FOR_LOCAL (r) = 0;
10369 DECL_INITIALIZED_P (r) = 0;
10370 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10371 if (type == error_mark_node)
10372 RETURN (error_mark_node);
10373 if (TREE_CODE (type) == FUNCTION_TYPE)
10375 /* It may seem that this case cannot occur, since:
10377 typedef void f();
10378 void g() { f x; }
10380 declares a function, not a variable. However:
10382 typedef void f();
10383 template <typename T> void g() { T t; }
10384 template void g<f>();
10386 is an attempt to declare a variable with function
10387 type. */
10388 error ("variable %qD has function type",
10389 /* R is not yet sufficiently initialized, so we
10390 just use its name. */
10391 DECL_NAME (r));
10392 RETURN (error_mark_node);
10394 type = complete_type (type);
10395 /* Wait until cp_finish_decl to set this again, to handle
10396 circular dependency (template/instantiate6.C). */
10397 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10398 type = check_var_type (DECL_NAME (r), type);
10400 if (DECL_HAS_VALUE_EXPR_P (t))
10402 tree ve = DECL_VALUE_EXPR (t);
10403 ve = tsubst_expr (ve, args, complain, in_decl,
10404 /*constant_expression_p=*/false);
10405 if (REFERENCE_REF_P (ve))
10407 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10408 ve = TREE_OPERAND (ve, 0);
10410 SET_DECL_VALUE_EXPR (r, ve);
10413 else if (DECL_SELF_REFERENCE_P (t))
10414 SET_DECL_SELF_REFERENCE_P (r);
10415 TREE_TYPE (r) = type;
10416 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10417 DECL_CONTEXT (r) = ctx;
10418 /* Clear out the mangled name and RTL for the instantiation. */
10419 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10420 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10421 SET_DECL_RTL (r, NULL);
10422 /* The initializer must not be expanded until it is required;
10423 see [temp.inst]. */
10424 DECL_INITIAL (r) = NULL_TREE;
10425 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10426 SET_DECL_RTL (r, NULL);
10427 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10428 if (TREE_CODE (r) == VAR_DECL)
10430 /* Possibly limit visibility based on template args. */
10431 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10432 if (DECL_VISIBILITY_SPECIFIED (t))
10434 DECL_VISIBILITY_SPECIFIED (r) = 0;
10435 DECL_ATTRIBUTES (r)
10436 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10438 determine_visibility (r);
10441 if (!local_p)
10443 /* A static data member declaration is always marked
10444 external when it is declared in-class, even if an
10445 initializer is present. We mimic the non-template
10446 processing here. */
10447 DECL_EXTERNAL (r) = 1;
10449 register_specialization (r, gen_tmpl, argvec, false, hash);
10450 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10451 SET_DECL_IMPLICIT_INSTANTIATION (r);
10453 else if (cp_unevaluated_operand)
10455 /* We're substituting this var in a decltype outside of its
10456 scope, such as for a lambda return type. Don't add it to
10457 local_specializations, do perform auto deduction. */
10458 tree auto_node = type_uses_auto (type);
10459 if (auto_node)
10461 tree init
10462 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10463 /*constant_expression_p=*/false);
10464 init = resolve_nondeduced_context (init);
10465 TREE_TYPE (r) = type
10466 = do_auto_deduction (type, init, auto_node);
10469 else
10470 register_local_specialization (r, t);
10472 DECL_CHAIN (r) = NULL_TREE;
10474 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10475 /*flags=*/0,
10476 args, complain, in_decl);
10478 /* Preserve a typedef that names a type. */
10479 if (is_typedef_decl (r))
10481 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10482 set_underlying_type (r);
10485 layout_decl (r, 0);
10487 break;
10489 default:
10490 gcc_unreachable ();
10492 #undef RETURN
10494 out:
10495 /* Restore the file and line information. */
10496 input_location = saved_loc;
10498 return r;
10501 /* Substitute into the ARG_TYPES of a function type.
10502 If END is a TREE_CHAIN, leave it and any following types
10503 un-substituted. */
10505 static tree
10506 tsubst_arg_types (tree arg_types,
10507 tree args,
10508 tree end,
10509 tsubst_flags_t complain,
10510 tree in_decl)
10512 tree remaining_arg_types;
10513 tree type = NULL_TREE;
10514 int i = 1;
10515 tree expanded_args = NULL_TREE;
10516 tree default_arg;
10518 if (!arg_types || arg_types == void_list_node || arg_types == end)
10519 return arg_types;
10521 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10522 args, end, complain, in_decl);
10523 if (remaining_arg_types == error_mark_node)
10524 return error_mark_node;
10526 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10528 /* For a pack expansion, perform substitution on the
10529 entire expression. Later on, we'll handle the arguments
10530 one-by-one. */
10531 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10532 args, complain, in_decl);
10534 if (TREE_CODE (expanded_args) == TREE_VEC)
10535 /* So that we'll spin through the parameters, one by one. */
10536 i = TREE_VEC_LENGTH (expanded_args);
10537 else
10539 /* We only partially substituted into the parameter
10540 pack. Our type is TYPE_PACK_EXPANSION. */
10541 type = expanded_args;
10542 expanded_args = NULL_TREE;
10546 while (i > 0) {
10547 --i;
10549 if (expanded_args)
10550 type = TREE_VEC_ELT (expanded_args, i);
10551 else if (!type)
10552 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10554 if (type == error_mark_node)
10555 return error_mark_node;
10556 if (VOID_TYPE_P (type))
10558 if (complain & tf_error)
10560 error ("invalid parameter type %qT", type);
10561 if (in_decl)
10562 error ("in declaration %q+D", in_decl);
10564 return error_mark_node;
10567 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10568 top-level qualifiers as required. */
10569 type = cv_unqualified (type_decays_to (type));
10571 /* We do not substitute into default arguments here. The standard
10572 mandates that they be instantiated only when needed, which is
10573 done in build_over_call. */
10574 default_arg = TREE_PURPOSE (arg_types);
10576 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10578 /* We've instantiated a template before its default arguments
10579 have been parsed. This can happen for a nested template
10580 class, and is not an error unless we require the default
10581 argument in a call of this function. */
10582 remaining_arg_types =
10583 tree_cons (default_arg, type, remaining_arg_types);
10584 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
10585 remaining_arg_types);
10587 else
10588 remaining_arg_types =
10589 hash_tree_cons (default_arg, type, remaining_arg_types);
10592 return remaining_arg_types;
10595 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10596 *not* handle the exception-specification for FNTYPE, because the
10597 initial substitution of explicitly provided template parameters
10598 during argument deduction forbids substitution into the
10599 exception-specification:
10601 [temp.deduct]
10603 All references in the function type of the function template to the
10604 corresponding template parameters are replaced by the specified tem-
10605 plate argument values. If a substitution in a template parameter or
10606 in the function type of the function template results in an invalid
10607 type, type deduction fails. [Note: The equivalent substitution in
10608 exception specifications is done only when the function is instanti-
10609 ated, at which point a program is ill-formed if the substitution
10610 results in an invalid type.] */
10612 static tree
10613 tsubst_function_type (tree t,
10614 tree args,
10615 tsubst_flags_t complain,
10616 tree in_decl)
10618 tree return_type;
10619 tree arg_types;
10620 tree fntype;
10622 /* The TYPE_CONTEXT is not used for function/method types. */
10623 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10625 /* Substitute the return type. */
10626 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10627 if (return_type == error_mark_node)
10628 return error_mark_node;
10629 /* The standard does not presently indicate that creation of a
10630 function type with an invalid return type is a deduction failure.
10631 However, that is clearly analogous to creating an array of "void"
10632 or a reference to a reference. This is core issue #486. */
10633 if (TREE_CODE (return_type) == ARRAY_TYPE
10634 || TREE_CODE (return_type) == FUNCTION_TYPE)
10636 if (complain & tf_error)
10638 if (TREE_CODE (return_type) == ARRAY_TYPE)
10639 error ("function returning an array");
10640 else
10641 error ("function returning a function");
10643 return error_mark_node;
10646 /* Substitute the argument types. */
10647 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
10648 complain, in_decl);
10649 if (arg_types == error_mark_node)
10650 return error_mark_node;
10652 /* Construct a new type node and return it. */
10653 if (TREE_CODE (t) == FUNCTION_TYPE)
10655 fntype = build_function_type (return_type, arg_types);
10656 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10658 else
10660 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10661 if (! MAYBE_CLASS_TYPE_P (r))
10663 /* [temp.deduct]
10665 Type deduction may fail for any of the following
10666 reasons:
10668 -- Attempting to create "pointer to member of T" when T
10669 is not a class type. */
10670 if (complain & tf_error)
10671 error ("creating pointer to member function of non-class type %qT",
10673 return error_mark_node;
10676 fntype = build_method_type_directly (r, return_type,
10677 TREE_CHAIN (arg_types));
10679 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10681 return fntype;
10684 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10685 ARGS into that specification, and return the substituted
10686 specification. If there is no specification, return NULL_TREE. */
10688 static tree
10689 tsubst_exception_specification (tree fntype,
10690 tree args,
10691 tsubst_flags_t complain,
10692 tree in_decl,
10693 bool defer_ok)
10695 tree specs;
10696 tree new_specs;
10698 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10699 new_specs = NULL_TREE;
10700 if (specs && TREE_PURPOSE (specs))
10702 /* A noexcept-specifier. */
10703 tree expr = TREE_PURPOSE (specs);
10704 if (expr == boolean_true_node || expr == boolean_false_node)
10705 new_specs = expr;
10706 else if (defer_ok)
10708 /* Defer instantiation of noexcept-specifiers to avoid
10709 excessive instantiations (c++/49107). */
10710 new_specs = make_node (DEFERRED_NOEXCEPT);
10711 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10713 /* We already partially instantiated this member template,
10714 so combine the new args with the old. */
10715 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10716 = DEFERRED_NOEXCEPT_PATTERN (expr);
10717 DEFERRED_NOEXCEPT_ARGS (new_specs)
10718 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10720 else
10722 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10723 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10726 else
10727 new_specs = tsubst_copy_and_build
10728 (expr, args, complain, in_decl, /*function_p=*/false,
10729 /*integral_constant_expression_p=*/true);
10730 new_specs = build_noexcept_spec (new_specs, complain);
10732 else if (specs)
10734 if (! TREE_VALUE (specs))
10735 new_specs = specs;
10736 else
10737 while (specs)
10739 tree spec;
10740 int i, len = 1;
10741 tree expanded_specs = NULL_TREE;
10743 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10745 /* Expand the pack expansion type. */
10746 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10747 args, complain,
10748 in_decl);
10750 if (expanded_specs == error_mark_node)
10751 return error_mark_node;
10752 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10753 len = TREE_VEC_LENGTH (expanded_specs);
10754 else
10756 /* We're substituting into a member template, so
10757 we got a TYPE_PACK_EXPANSION back. Add that
10758 expansion and move on. */
10759 gcc_assert (TREE_CODE (expanded_specs)
10760 == TYPE_PACK_EXPANSION);
10761 new_specs = add_exception_specifier (new_specs,
10762 expanded_specs,
10763 complain);
10764 specs = TREE_CHAIN (specs);
10765 continue;
10769 for (i = 0; i < len; ++i)
10771 if (expanded_specs)
10772 spec = TREE_VEC_ELT (expanded_specs, i);
10773 else
10774 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10775 if (spec == error_mark_node)
10776 return spec;
10777 new_specs = add_exception_specifier (new_specs, spec,
10778 complain);
10781 specs = TREE_CHAIN (specs);
10784 return new_specs;
10787 /* Take the tree structure T and replace template parameters used
10788 therein with the argument vector ARGS. IN_DECL is an associated
10789 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10790 Issue error and warning messages under control of COMPLAIN. Note
10791 that we must be relatively non-tolerant of extensions here, in
10792 order to preserve conformance; if we allow substitutions that
10793 should not be allowed, we may allow argument deductions that should
10794 not succeed, and therefore report ambiguous overload situations
10795 where there are none. In theory, we could allow the substitution,
10796 but indicate that it should have failed, and allow our caller to
10797 make sure that the right thing happens, but we don't try to do this
10798 yet.
10800 This function is used for dealing with types, decls and the like;
10801 for expressions, use tsubst_expr or tsubst_copy. */
10803 tree
10804 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10806 enum tree_code code;
10807 tree type, r = NULL_TREE;
10809 if (t == NULL_TREE || t == error_mark_node
10810 || t == integer_type_node
10811 || t == void_type_node
10812 || t == char_type_node
10813 || t == unknown_type_node
10814 || TREE_CODE (t) == NAMESPACE_DECL
10815 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10816 return t;
10818 if (DECL_P (t))
10819 return tsubst_decl (t, args, complain);
10821 if (args == NULL_TREE)
10822 return t;
10824 code = TREE_CODE (t);
10826 if (code == IDENTIFIER_NODE)
10827 type = IDENTIFIER_TYPE_VALUE (t);
10828 else
10829 type = TREE_TYPE (t);
10831 gcc_assert (type != unknown_type_node);
10833 /* Reuse typedefs. We need to do this to handle dependent attributes,
10834 such as attribute aligned. */
10835 if (TYPE_P (t)
10836 && typedef_variant_p (t))
10838 tree decl = TYPE_NAME (t);
10840 if (TYPE_DECL_ALIAS_P (decl)
10841 && DECL_LANG_SPECIFIC (decl)
10842 && DECL_TEMPLATE_INFO (decl)
10843 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
10845 /* DECL represents an alias template and we want to
10846 instantiate it. Let's substitute our arguments for the
10847 template parameters into the declaration and get the
10848 resulting type. */
10849 r = tsubst (decl, args, complain, decl);
10851 else if (DECL_CLASS_SCOPE_P (decl)
10852 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10853 && uses_template_parms (DECL_CONTEXT (decl)))
10855 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10856 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10857 r = retrieve_specialization (tmpl, gen_args, 0);
10859 else if (DECL_FUNCTION_SCOPE_P (decl)
10860 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10861 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10862 r = retrieve_local_specialization (decl);
10863 else
10864 /* The typedef is from a non-template context. */
10865 return t;
10867 if (r)
10869 r = TREE_TYPE (r);
10870 r = cp_build_qualified_type_real
10871 (r, cp_type_quals (t) | cp_type_quals (r),
10872 complain | tf_ignore_bad_quals);
10873 return r;
10875 else
10876 /* We don't have an instantiation yet, so drop the typedef. */
10877 t = DECL_ORIGINAL_TYPE (decl);
10880 if (type
10881 && code != TYPENAME_TYPE
10882 && code != TEMPLATE_TYPE_PARM
10883 && code != IDENTIFIER_NODE
10884 && code != FUNCTION_TYPE
10885 && code != METHOD_TYPE)
10886 type = tsubst (type, args, complain, in_decl);
10887 if (type == error_mark_node)
10888 return error_mark_node;
10890 switch (code)
10892 case RECORD_TYPE:
10893 case UNION_TYPE:
10894 case ENUMERAL_TYPE:
10895 return tsubst_aggr_type (t, args, complain, in_decl,
10896 /*entering_scope=*/0);
10898 case ERROR_MARK:
10899 case IDENTIFIER_NODE:
10900 case VOID_TYPE:
10901 case REAL_TYPE:
10902 case COMPLEX_TYPE:
10903 case VECTOR_TYPE:
10904 case BOOLEAN_TYPE:
10905 case NULLPTR_TYPE:
10906 case LANG_TYPE:
10907 return t;
10909 case INTEGER_TYPE:
10910 if (t == integer_type_node)
10911 return t;
10913 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10914 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10915 return t;
10918 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10920 max = tsubst_expr (omax, args, complain, in_decl,
10921 /*integral_constant_expression_p=*/false);
10923 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10924 needed. */
10925 if (TREE_CODE (max) == NOP_EXPR
10926 && TREE_SIDE_EFFECTS (omax)
10927 && !TREE_TYPE (max))
10928 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10930 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10931 with TREE_SIDE_EFFECTS that indicates this is not an integral
10932 constant expression. */
10933 if (processing_template_decl
10934 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10936 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10937 TREE_SIDE_EFFECTS (max) = 1;
10940 return compute_array_index_type (NULL_TREE, max, complain);
10943 case TEMPLATE_TYPE_PARM:
10944 case TEMPLATE_TEMPLATE_PARM:
10945 case BOUND_TEMPLATE_TEMPLATE_PARM:
10946 case TEMPLATE_PARM_INDEX:
10948 int idx;
10949 int level;
10950 int levels;
10951 tree arg = NULL_TREE;
10953 r = NULL_TREE;
10955 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10956 template_parm_level_and_index (t, &level, &idx);
10958 levels = TMPL_ARGS_DEPTH (args);
10959 if (level <= levels)
10961 arg = TMPL_ARG (args, level, idx);
10963 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10964 /* See through ARGUMENT_PACK_SELECT arguments. */
10965 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10968 if (arg == error_mark_node)
10969 return error_mark_node;
10970 else if (arg != NULL_TREE)
10972 if (ARGUMENT_PACK_P (arg))
10973 /* If ARG is an argument pack, we don't actually want to
10974 perform a substitution here, because substitutions
10975 for argument packs are only done
10976 element-by-element. We can get to this point when
10977 substituting the type of a non-type template
10978 parameter pack, when that type actually contains
10979 template parameter packs from an outer template, e.g.,
10981 template<typename... Types> struct A {
10982 template<Types... Values> struct B { };
10983 }; */
10984 return t;
10986 if (code == TEMPLATE_TYPE_PARM)
10988 int quals;
10989 gcc_assert (TYPE_P (arg));
10991 quals = cp_type_quals (arg) | cp_type_quals (t);
10993 return cp_build_qualified_type_real
10994 (arg, quals, complain | tf_ignore_bad_quals);
10996 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10998 /* We are processing a type constructed from a
10999 template template parameter. */
11000 tree argvec = tsubst (TYPE_TI_ARGS (t),
11001 args, complain, in_decl);
11002 if (argvec == error_mark_node)
11003 return error_mark_node;
11005 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11006 || TREE_CODE (arg) == TEMPLATE_DECL
11007 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11009 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11010 /* Consider this code:
11012 template <template <class> class Template>
11013 struct Internal {
11014 template <class Arg> using Bind = Template<Arg>;
11017 template <template <class> class Template, class Arg>
11018 using Instantiate = Template<Arg>; //#0
11020 template <template <class> class Template,
11021 class Argument>
11022 using Bind =
11023 Instantiate<Internal<Template>::template Bind,
11024 Argument>; //#1
11026 When #1 is parsed, the
11027 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11028 parameter `Template' in #0 matches the
11029 UNBOUND_CLASS_TEMPLATE representing the argument
11030 `Internal<Template>::template Bind'; We then want
11031 to assemble the type `Bind<Argument>' that can't
11032 be fully created right now, because
11033 `Internal<Template>' not being complete, the Bind
11034 template cannot be looked up in that context. So
11035 we need to "store" `Bind<Argument>' for later
11036 when the context of Bind becomes complete. Let's
11037 store that in a TYPENAME_TYPE. */
11038 return make_typename_type (TYPE_CONTEXT (arg),
11039 build_nt (TEMPLATE_ID_EXPR,
11040 TYPE_IDENTIFIER (arg),
11041 argvec),
11042 typename_type,
11043 complain);
11045 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11046 are resolving nested-types in the signature of a
11047 member function templates. Otherwise ARG is a
11048 TEMPLATE_DECL and is the real template to be
11049 instantiated. */
11050 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11051 arg = TYPE_NAME (arg);
11053 r = lookup_template_class (arg,
11054 argvec, in_decl,
11055 DECL_CONTEXT (arg),
11056 /*entering_scope=*/0,
11057 complain);
11058 return cp_build_qualified_type_real
11059 (r, cp_type_quals (t), complain);
11061 else
11062 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11063 return convert_from_reference (unshare_expr (arg));
11066 if (level == 1)
11067 /* This can happen during the attempted tsubst'ing in
11068 unify. This means that we don't yet have any information
11069 about the template parameter in question. */
11070 return t;
11072 /* Early in template argument deduction substitution, we don't
11073 want to reduce the level of 'auto', or it will be confused
11074 with a normal template parm in subsequent deduction. */
11075 if (is_auto (t) && (complain & tf_partial))
11076 return t;
11078 /* If we get here, we must have been looking at a parm for a
11079 more deeply nested template. Make a new version of this
11080 template parameter, but with a lower level. */
11081 switch (code)
11083 case TEMPLATE_TYPE_PARM:
11084 case TEMPLATE_TEMPLATE_PARM:
11085 case BOUND_TEMPLATE_TEMPLATE_PARM:
11086 if (cp_type_quals (t))
11088 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11089 r = cp_build_qualified_type_real
11090 (r, cp_type_quals (t),
11091 complain | (code == TEMPLATE_TYPE_PARM
11092 ? tf_ignore_bad_quals : 0));
11094 else
11096 r = copy_type (t);
11097 TEMPLATE_TYPE_PARM_INDEX (r)
11098 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11099 r, levels, args, complain);
11100 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11101 TYPE_MAIN_VARIANT (r) = r;
11102 TYPE_POINTER_TO (r) = NULL_TREE;
11103 TYPE_REFERENCE_TO (r) = NULL_TREE;
11105 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11106 /* We have reduced the level of the template
11107 template parameter, but not the levels of its
11108 template parameters, so canonical_type_parameter
11109 will not be able to find the canonical template
11110 template parameter for this level. Thus, we
11111 require structural equality checking to compare
11112 TEMPLATE_TEMPLATE_PARMs. */
11113 SET_TYPE_STRUCTURAL_EQUALITY (r);
11114 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11115 SET_TYPE_STRUCTURAL_EQUALITY (r);
11116 else
11117 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11119 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11121 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11122 complain, in_decl);
11123 if (argvec == error_mark_node)
11124 return error_mark_node;
11126 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11127 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11130 break;
11132 case TEMPLATE_PARM_INDEX:
11133 r = reduce_template_parm_level (t, type, levels, args, complain);
11134 break;
11136 default:
11137 gcc_unreachable ();
11140 return r;
11143 case TREE_LIST:
11145 tree purpose, value, chain;
11147 if (t == void_list_node)
11148 return t;
11150 purpose = TREE_PURPOSE (t);
11151 if (purpose)
11153 purpose = tsubst (purpose, args, complain, in_decl);
11154 if (purpose == error_mark_node)
11155 return error_mark_node;
11157 value = TREE_VALUE (t);
11158 if (value)
11160 value = tsubst (value, args, complain, in_decl);
11161 if (value == error_mark_node)
11162 return error_mark_node;
11164 chain = TREE_CHAIN (t);
11165 if (chain && chain != void_type_node)
11167 chain = tsubst (chain, args, complain, in_decl);
11168 if (chain == error_mark_node)
11169 return error_mark_node;
11171 if (purpose == TREE_PURPOSE (t)
11172 && value == TREE_VALUE (t)
11173 && chain == TREE_CHAIN (t))
11174 return t;
11175 return hash_tree_cons (purpose, value, chain);
11178 case TREE_BINFO:
11179 /* We should never be tsubsting a binfo. */
11180 gcc_unreachable ();
11182 case TREE_VEC:
11183 /* A vector of template arguments. */
11184 gcc_assert (!type);
11185 return tsubst_template_args (t, args, complain, in_decl);
11187 case POINTER_TYPE:
11188 case REFERENCE_TYPE:
11190 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11191 return t;
11193 /* [temp.deduct]
11195 Type deduction may fail for any of the following
11196 reasons:
11198 -- Attempting to create a pointer to reference type.
11199 -- Attempting to create a reference to a reference type or
11200 a reference to void.
11202 Core issue 106 says that creating a reference to a reference
11203 during instantiation is no longer a cause for failure. We
11204 only enforce this check in strict C++98 mode. */
11205 if ((TREE_CODE (type) == REFERENCE_TYPE
11206 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11207 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11209 static location_t last_loc;
11211 /* We keep track of the last time we issued this error
11212 message to avoid spewing a ton of messages during a
11213 single bad template instantiation. */
11214 if (complain & tf_error
11215 && last_loc != input_location)
11217 if (TREE_CODE (type) == VOID_TYPE)
11218 error ("forming reference to void");
11219 else if (code == POINTER_TYPE)
11220 error ("forming pointer to reference type %qT", type);
11221 else
11222 error ("forming reference to reference type %qT", type);
11223 last_loc = input_location;
11226 return error_mark_node;
11228 else if (code == POINTER_TYPE)
11230 r = build_pointer_type (type);
11231 if (TREE_CODE (type) == METHOD_TYPE)
11232 r = build_ptrmemfunc_type (r);
11234 else if (TREE_CODE (type) == REFERENCE_TYPE)
11235 /* In C++0x, during template argument substitution, when there is an
11236 attempt to create a reference to a reference type, reference
11237 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11239 "If a template-argument for a template-parameter T names a type
11240 that is a reference to a type A, an attempt to create the type
11241 'lvalue reference to cv T' creates the type 'lvalue reference to
11242 A,' while an attempt to create the type type rvalue reference to
11243 cv T' creates the type T"
11245 r = cp_build_reference_type
11246 (TREE_TYPE (type),
11247 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11248 else
11249 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11250 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11252 if (r != error_mark_node)
11253 /* Will this ever be needed for TYPE_..._TO values? */
11254 layout_type (r);
11256 return r;
11258 case OFFSET_TYPE:
11260 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11261 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11263 /* [temp.deduct]
11265 Type deduction may fail for any of the following
11266 reasons:
11268 -- Attempting to create "pointer to member of T" when T
11269 is not a class type. */
11270 if (complain & tf_error)
11271 error ("creating pointer to member of non-class type %qT", r);
11272 return error_mark_node;
11274 if (TREE_CODE (type) == REFERENCE_TYPE)
11276 if (complain & tf_error)
11277 error ("creating pointer to member reference type %qT", type);
11278 return error_mark_node;
11280 if (TREE_CODE (type) == VOID_TYPE)
11282 if (complain & tf_error)
11283 error ("creating pointer to member of type void");
11284 return error_mark_node;
11286 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11287 if (TREE_CODE (type) == FUNCTION_TYPE)
11289 /* The type of the implicit object parameter gets its
11290 cv-qualifiers from the FUNCTION_TYPE. */
11291 tree memptr;
11292 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11293 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11294 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11295 complain);
11297 else
11298 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11299 cp_type_quals (t),
11300 complain);
11302 case FUNCTION_TYPE:
11303 case METHOD_TYPE:
11305 tree fntype;
11306 tree specs;
11307 fntype = tsubst_function_type (t, args, complain, in_decl);
11308 if (fntype == error_mark_node)
11309 return error_mark_node;
11311 /* Substitute the exception specification. */
11312 specs = tsubst_exception_specification (t, args, complain,
11313 in_decl, /*defer_ok*/true);
11314 if (specs == error_mark_node)
11315 return error_mark_node;
11316 if (specs)
11317 fntype = build_exception_variant (fntype, specs);
11318 return fntype;
11320 case ARRAY_TYPE:
11322 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11323 if (domain == error_mark_node)
11324 return error_mark_node;
11326 /* As an optimization, we avoid regenerating the array type if
11327 it will obviously be the same as T. */
11328 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11329 return t;
11331 /* These checks should match the ones in grokdeclarator.
11333 [temp.deduct]
11335 The deduction may fail for any of the following reasons:
11337 -- Attempting to create an array with an element type that
11338 is void, a function type, or a reference type, or [DR337]
11339 an abstract class type. */
11340 if (TREE_CODE (type) == VOID_TYPE
11341 || TREE_CODE (type) == FUNCTION_TYPE
11342 || TREE_CODE (type) == REFERENCE_TYPE)
11344 if (complain & tf_error)
11345 error ("creating array of %qT", type);
11346 return error_mark_node;
11348 if (ABSTRACT_CLASS_TYPE_P (type))
11350 if (complain & tf_error)
11351 error ("creating array of %qT, which is an abstract class type",
11352 type);
11353 return error_mark_node;
11356 r = build_cplus_array_type (type, domain);
11358 if (TYPE_USER_ALIGN (t))
11360 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11361 TYPE_USER_ALIGN (r) = 1;
11364 return r;
11367 case TYPENAME_TYPE:
11369 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11370 in_decl, /*entering_scope=*/1);
11371 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11372 complain, in_decl);
11374 if (ctx == error_mark_node || f == error_mark_node)
11375 return error_mark_node;
11377 if (!MAYBE_CLASS_TYPE_P (ctx))
11379 if (complain & tf_error)
11380 error ("%qT is not a class, struct, or union type", ctx);
11381 return error_mark_node;
11383 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11385 /* Normally, make_typename_type does not require that the CTX
11386 have complete type in order to allow things like:
11388 template <class T> struct S { typename S<T>::X Y; };
11390 But, such constructs have already been resolved by this
11391 point, so here CTX really should have complete type, unless
11392 it's a partial instantiation. */
11393 ctx = complete_type (ctx);
11394 if (!COMPLETE_TYPE_P (ctx))
11396 if (complain & tf_error)
11397 cxx_incomplete_type_error (NULL_TREE, ctx);
11398 return error_mark_node;
11402 f = make_typename_type (ctx, f, typename_type,
11403 (complain & tf_error) | tf_keep_type_decl);
11404 if (f == error_mark_node)
11405 return f;
11406 if (TREE_CODE (f) == TYPE_DECL)
11408 complain |= tf_ignore_bad_quals;
11409 f = TREE_TYPE (f);
11412 if (TREE_CODE (f) != TYPENAME_TYPE)
11414 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11416 if (complain & tf_error)
11417 error ("%qT resolves to %qT, which is not an enumeration type",
11418 t, f);
11419 else
11420 return error_mark_node;
11422 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11424 if (complain & tf_error)
11425 error ("%qT resolves to %qT, which is is not a class type",
11426 t, f);
11427 else
11428 return error_mark_node;
11432 return cp_build_qualified_type_real
11433 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11436 case UNBOUND_CLASS_TEMPLATE:
11438 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11439 in_decl, /*entering_scope=*/1);
11440 tree name = TYPE_IDENTIFIER (t);
11441 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11443 if (ctx == error_mark_node || name == error_mark_node)
11444 return error_mark_node;
11446 if (parm_list)
11447 parm_list = tsubst_template_parms (parm_list, args, complain);
11448 return make_unbound_class_template (ctx, name, parm_list, complain);
11451 case TYPEOF_TYPE:
11453 tree type;
11455 ++cp_unevaluated_operand;
11456 ++c_inhibit_evaluation_warnings;
11458 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11459 complain, in_decl,
11460 /*integral_constant_expression_p=*/false);
11462 --cp_unevaluated_operand;
11463 --c_inhibit_evaluation_warnings;
11465 type = finish_typeof (type);
11466 return cp_build_qualified_type_real (type,
11467 cp_type_quals (t)
11468 | cp_type_quals (type),
11469 complain);
11472 case DECLTYPE_TYPE:
11474 tree type;
11476 ++cp_unevaluated_operand;
11477 ++c_inhibit_evaluation_warnings;
11479 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11480 complain, in_decl,
11481 /*integral_constant_expression_p=*/false);
11483 --cp_unevaluated_operand;
11484 --c_inhibit_evaluation_warnings;
11486 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11487 type = lambda_capture_field_type (type);
11488 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11489 type = lambda_proxy_type (type);
11490 else
11491 type = finish_decltype_type
11492 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11493 return cp_build_qualified_type_real (type,
11494 cp_type_quals (t)
11495 | cp_type_quals (type),
11496 complain);
11499 case UNDERLYING_TYPE:
11501 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11502 complain, in_decl);
11503 return finish_underlying_type (type);
11506 case TYPE_ARGUMENT_PACK:
11507 case NONTYPE_ARGUMENT_PACK:
11509 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11510 tree packed_out =
11511 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11512 args,
11513 complain,
11514 in_decl);
11515 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11517 /* For template nontype argument packs, also substitute into
11518 the type. */
11519 if (code == NONTYPE_ARGUMENT_PACK)
11520 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11522 return r;
11524 break;
11526 case INTEGER_CST:
11527 case REAL_CST:
11528 case STRING_CST:
11529 case PLUS_EXPR:
11530 case MINUS_EXPR:
11531 case NEGATE_EXPR:
11532 case NOP_EXPR:
11533 case INDIRECT_REF:
11534 case ADDR_EXPR:
11535 case CALL_EXPR:
11536 case ARRAY_REF:
11537 case SCOPE_REF:
11538 /* We should use one of the expression tsubsts for these codes. */
11539 gcc_unreachable ();
11541 default:
11542 sorry ("use of %qs in template", tree_code_name [(int) code]);
11543 return error_mark_node;
11547 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11548 type of the expression on the left-hand side of the "." or "->"
11549 operator. */
11551 static tree
11552 tsubst_baselink (tree baselink, tree object_type,
11553 tree args, tsubst_flags_t complain, tree in_decl)
11555 tree name;
11556 tree qualifying_scope;
11557 tree fns;
11558 tree optype;
11559 tree template_args = 0;
11560 bool template_id_p = false;
11561 bool qualified = BASELINK_QUALIFIED_P (baselink);
11563 /* A baselink indicates a function from a base class. Both the
11564 BASELINK_ACCESS_BINFO and the base class referenced may
11565 indicate bases of the template class, rather than the
11566 instantiated class. In addition, lookups that were not
11567 ambiguous before may be ambiguous now. Therefore, we perform
11568 the lookup again. */
11569 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11570 qualifying_scope = tsubst (qualifying_scope, args,
11571 complain, in_decl);
11572 fns = BASELINK_FUNCTIONS (baselink);
11573 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11574 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11576 template_id_p = true;
11577 template_args = TREE_OPERAND (fns, 1);
11578 fns = TREE_OPERAND (fns, 0);
11579 if (template_args)
11580 template_args = tsubst_template_args (template_args, args,
11581 complain, in_decl);
11583 name = DECL_NAME (get_first_fn (fns));
11584 if (IDENTIFIER_TYPENAME_P (name))
11585 name = mangle_conv_op_name_for_type (optype);
11586 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11587 if (!baselink)
11588 return error_mark_node;
11590 /* If lookup found a single function, mark it as used at this
11591 point. (If it lookup found multiple functions the one selected
11592 later by overload resolution will be marked as used at that
11593 point.) */
11594 if (BASELINK_P (baselink))
11595 fns = BASELINK_FUNCTIONS (baselink);
11596 if (!template_id_p && !really_overloaded_fn (fns))
11597 mark_used (OVL_CURRENT (fns));
11599 /* Add back the template arguments, if present. */
11600 if (BASELINK_P (baselink) && template_id_p)
11601 BASELINK_FUNCTIONS (baselink)
11602 = build_nt (TEMPLATE_ID_EXPR,
11603 BASELINK_FUNCTIONS (baselink),
11604 template_args);
11605 /* Update the conversion operator type. */
11606 BASELINK_OPTYPE (baselink) = optype;
11608 if (!object_type)
11609 object_type = current_class_type;
11611 if (qualified)
11612 baselink = adjust_result_of_qualified_name_lookup (baselink,
11613 qualifying_scope,
11614 object_type);
11615 return baselink;
11618 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11619 true if the qualified-id will be a postfix-expression in-and-of
11620 itself; false if more of the postfix-expression follows the
11621 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11622 of "&". */
11624 static tree
11625 tsubst_qualified_id (tree qualified_id, tree args,
11626 tsubst_flags_t complain, tree in_decl,
11627 bool done, bool address_p)
11629 tree expr;
11630 tree scope;
11631 tree name;
11632 bool is_template;
11633 tree template_args;
11634 location_t loc = UNKNOWN_LOCATION;
11636 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11638 /* Figure out what name to look up. */
11639 name = TREE_OPERAND (qualified_id, 1);
11640 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11642 is_template = true;
11643 loc = EXPR_LOCATION (name);
11644 template_args = TREE_OPERAND (name, 1);
11645 if (template_args)
11646 template_args = tsubst_template_args (template_args, args,
11647 complain, in_decl);
11648 name = TREE_OPERAND (name, 0);
11650 else
11652 is_template = false;
11653 template_args = NULL_TREE;
11656 /* Substitute into the qualifying scope. When there are no ARGS, we
11657 are just trying to simplify a non-dependent expression. In that
11658 case the qualifying scope may be dependent, and, in any case,
11659 substituting will not help. */
11660 scope = TREE_OPERAND (qualified_id, 0);
11661 if (args)
11663 scope = tsubst (scope, args, complain, in_decl);
11664 expr = tsubst_copy (name, args, complain, in_decl);
11666 else
11667 expr = name;
11669 if (dependent_scope_p (scope))
11671 if (is_template)
11672 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
11673 return build_qualified_name (NULL_TREE, scope, expr,
11674 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11677 if (!BASELINK_P (name) && !DECL_P (expr))
11679 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11681 /* A BIT_NOT_EXPR is used to represent a destructor. */
11682 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11684 error ("qualifying type %qT does not match destructor name ~%qT",
11685 scope, TREE_OPERAND (expr, 0));
11686 expr = error_mark_node;
11688 else
11689 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11690 /*is_type_p=*/0, false);
11692 else
11693 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11694 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11695 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11697 if (complain & tf_error)
11699 error ("dependent-name %qE is parsed as a non-type, but "
11700 "instantiation yields a type", qualified_id);
11701 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11703 return error_mark_node;
11707 if (DECL_P (expr))
11709 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11710 scope);
11711 /* Remember that there was a reference to this entity. */
11712 mark_used (expr);
11715 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11717 if (complain & tf_error)
11718 qualified_name_lookup_error (scope,
11719 TREE_OPERAND (qualified_id, 1),
11720 expr, input_location);
11721 return error_mark_node;
11724 if (is_template)
11725 expr = lookup_template_function (expr, template_args);
11727 if (expr == error_mark_node && complain & tf_error)
11728 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11729 expr, input_location);
11730 else if (TYPE_P (scope))
11732 expr = (adjust_result_of_qualified_name_lookup
11733 (expr, scope, current_class_type));
11734 expr = (finish_qualified_id_expr
11735 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
11736 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11737 /*template_arg_p=*/false));
11740 /* Expressions do not generally have reference type. */
11741 if (TREE_CODE (expr) != SCOPE_REF
11742 /* However, if we're about to form a pointer-to-member, we just
11743 want the referenced member referenced. */
11744 && TREE_CODE (expr) != OFFSET_REF)
11745 expr = convert_from_reference (expr);
11747 return expr;
11750 /* Like tsubst, but deals with expressions. This function just replaces
11751 template parms; to finish processing the resultant expression, use
11752 tsubst_copy_and_build or tsubst_expr. */
11754 static tree
11755 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11757 enum tree_code code;
11758 tree r;
11760 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11761 return t;
11763 code = TREE_CODE (t);
11765 switch (code)
11767 case PARM_DECL:
11768 r = retrieve_local_specialization (t);
11770 if (r == NULL_TREE)
11772 /* We get here for a use of 'this' in an NSDMI. */
11773 if (DECL_NAME (t) == this_identifier
11774 && at_function_scope_p ()
11775 && DECL_CONSTRUCTOR_P (current_function_decl))
11776 return current_class_ptr;
11778 /* This can happen for a parameter name used later in a function
11779 declaration (such as in a late-specified return type). Just
11780 make a dummy decl, since it's only used for its type. */
11781 gcc_assert (cp_unevaluated_operand != 0);
11782 r = tsubst_decl (t, args, complain);
11783 /* Give it the template pattern as its context; its true context
11784 hasn't been instantiated yet and this is good enough for
11785 mangling. */
11786 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11789 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11790 r = ARGUMENT_PACK_SELECT_ARG (r);
11791 mark_used (r);
11792 return r;
11794 case CONST_DECL:
11796 tree enum_type;
11797 tree v;
11799 if (DECL_TEMPLATE_PARM_P (t))
11800 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11801 /* There is no need to substitute into namespace-scope
11802 enumerators. */
11803 if (DECL_NAMESPACE_SCOPE_P (t))
11804 return t;
11805 /* If ARGS is NULL, then T is known to be non-dependent. */
11806 if (args == NULL_TREE)
11807 return integral_constant_value (t);
11809 /* Unfortunately, we cannot just call lookup_name here.
11810 Consider:
11812 template <int I> int f() {
11813 enum E { a = I };
11814 struct S { void g() { E e = a; } };
11817 When we instantiate f<7>::S::g(), say, lookup_name is not
11818 clever enough to find f<7>::a. */
11819 enum_type
11820 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11821 /*entering_scope=*/0);
11823 for (v = TYPE_VALUES (enum_type);
11824 v != NULL_TREE;
11825 v = TREE_CHAIN (v))
11826 if (TREE_PURPOSE (v) == DECL_NAME (t))
11827 return TREE_VALUE (v);
11829 /* We didn't find the name. That should never happen; if
11830 name-lookup found it during preliminary parsing, we
11831 should find it again here during instantiation. */
11832 gcc_unreachable ();
11834 return t;
11836 case FIELD_DECL:
11837 if (DECL_CONTEXT (t))
11839 tree ctx;
11841 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11842 /*entering_scope=*/1);
11843 if (ctx != DECL_CONTEXT (t))
11845 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11846 if (!r)
11848 if (complain & tf_error)
11849 error ("using invalid field %qD", t);
11850 return error_mark_node;
11852 return r;
11856 return t;
11858 case VAR_DECL:
11859 case FUNCTION_DECL:
11860 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11861 || local_variable_p (t))
11862 t = tsubst (t, args, complain, in_decl);
11863 mark_used (t);
11864 return t;
11866 case NAMESPACE_DECL:
11867 return t;
11869 case OVERLOAD:
11870 /* An OVERLOAD will always be a non-dependent overload set; an
11871 overload set from function scope will just be represented with an
11872 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11873 gcc_assert (!uses_template_parms (t));
11874 return t;
11876 case BASELINK:
11877 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11879 case TEMPLATE_DECL:
11880 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11881 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11882 args, complain, in_decl);
11883 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11884 return tsubst (t, args, complain, in_decl);
11885 else if (DECL_CLASS_SCOPE_P (t)
11886 && uses_template_parms (DECL_CONTEXT (t)))
11888 /* Template template argument like the following example need
11889 special treatment:
11891 template <template <class> class TT> struct C {};
11892 template <class T> struct D {
11893 template <class U> struct E {};
11894 C<E> c; // #1
11896 D<int> d; // #2
11898 We are processing the template argument `E' in #1 for
11899 the template instantiation #2. Originally, `E' is a
11900 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11901 have to substitute this with one having context `D<int>'. */
11903 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11904 return lookup_field (context, DECL_NAME(t), 0, false);
11906 else
11907 /* Ordinary template template argument. */
11908 return t;
11910 case CAST_EXPR:
11911 case REINTERPRET_CAST_EXPR:
11912 case CONST_CAST_EXPR:
11913 case STATIC_CAST_EXPR:
11914 case DYNAMIC_CAST_EXPR:
11915 case IMPLICIT_CONV_EXPR:
11916 case CONVERT_EXPR:
11917 case NOP_EXPR:
11918 return build1
11919 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11920 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11922 case SIZEOF_EXPR:
11923 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11926 tree expanded;
11927 int len = 0;
11929 ++cp_unevaluated_operand;
11930 ++c_inhibit_evaluation_warnings;
11931 /* We only want to compute the number of arguments. */
11932 expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11933 complain, in_decl);
11934 --cp_unevaluated_operand;
11935 --c_inhibit_evaluation_warnings;
11937 if (TREE_CODE (expanded) == TREE_VEC)
11938 len = TREE_VEC_LENGTH (expanded);
11940 if (expanded == error_mark_node)
11941 return error_mark_node;
11942 else if (PACK_EXPANSION_P (expanded)
11943 || (TREE_CODE (expanded) == TREE_VEC
11944 && len > 0
11945 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11947 if (TREE_CODE (expanded) == TREE_VEC)
11948 expanded = TREE_VEC_ELT (expanded, len - 1);
11950 if (TYPE_P (expanded))
11951 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11952 complain & tf_error);
11953 else
11954 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11955 complain & tf_error);
11957 else
11958 return build_int_cst (size_type_node, len);
11960 /* Fall through */
11962 case INDIRECT_REF:
11963 case NEGATE_EXPR:
11964 case TRUTH_NOT_EXPR:
11965 case BIT_NOT_EXPR:
11966 case ADDR_EXPR:
11967 case UNARY_PLUS_EXPR: /* Unary + */
11968 case ALIGNOF_EXPR:
11969 case AT_ENCODE_EXPR:
11970 case ARROW_EXPR:
11971 case THROW_EXPR:
11972 case TYPEID_EXPR:
11973 case REALPART_EXPR:
11974 case IMAGPART_EXPR:
11975 return build1
11976 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11977 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11979 case COMPONENT_REF:
11981 tree object;
11982 tree name;
11984 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11985 name = TREE_OPERAND (t, 1);
11986 if (TREE_CODE (name) == BIT_NOT_EXPR)
11988 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11989 complain, in_decl);
11990 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11992 else if (TREE_CODE (name) == SCOPE_REF
11993 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11995 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11996 complain, in_decl);
11997 name = TREE_OPERAND (name, 1);
11998 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11999 complain, in_decl);
12000 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12001 name = build_qualified_name (/*type=*/NULL_TREE,
12002 base, name,
12003 /*template_p=*/false);
12005 else if (BASELINK_P (name))
12006 name = tsubst_baselink (name,
12007 non_reference (TREE_TYPE (object)),
12008 args, complain,
12009 in_decl);
12010 else
12011 name = tsubst_copy (name, args, complain, in_decl);
12012 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12015 case PLUS_EXPR:
12016 case MINUS_EXPR:
12017 case MULT_EXPR:
12018 case TRUNC_DIV_EXPR:
12019 case CEIL_DIV_EXPR:
12020 case FLOOR_DIV_EXPR:
12021 case ROUND_DIV_EXPR:
12022 case EXACT_DIV_EXPR:
12023 case BIT_AND_EXPR:
12024 case BIT_IOR_EXPR:
12025 case BIT_XOR_EXPR:
12026 case TRUNC_MOD_EXPR:
12027 case FLOOR_MOD_EXPR:
12028 case TRUTH_ANDIF_EXPR:
12029 case TRUTH_ORIF_EXPR:
12030 case TRUTH_AND_EXPR:
12031 case TRUTH_OR_EXPR:
12032 case RSHIFT_EXPR:
12033 case LSHIFT_EXPR:
12034 case RROTATE_EXPR:
12035 case LROTATE_EXPR:
12036 case EQ_EXPR:
12037 case NE_EXPR:
12038 case MAX_EXPR:
12039 case MIN_EXPR:
12040 case LE_EXPR:
12041 case GE_EXPR:
12042 case LT_EXPR:
12043 case GT_EXPR:
12044 case COMPOUND_EXPR:
12045 case DOTSTAR_EXPR:
12046 case MEMBER_REF:
12047 case PREDECREMENT_EXPR:
12048 case PREINCREMENT_EXPR:
12049 case POSTDECREMENT_EXPR:
12050 case POSTINCREMENT_EXPR:
12051 return build_nt
12052 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12053 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12055 case SCOPE_REF:
12056 return build_qualified_name (/*type=*/NULL_TREE,
12057 tsubst_copy (TREE_OPERAND (t, 0),
12058 args, complain, in_decl),
12059 tsubst_copy (TREE_OPERAND (t, 1),
12060 args, complain, in_decl),
12061 QUALIFIED_NAME_IS_TEMPLATE (t));
12063 case ARRAY_REF:
12064 return build_nt
12065 (ARRAY_REF,
12066 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12067 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12068 NULL_TREE, NULL_TREE);
12070 case CALL_EXPR:
12072 int n = VL_EXP_OPERAND_LENGTH (t);
12073 tree result = build_vl_exp (CALL_EXPR, n);
12074 int i;
12075 for (i = 0; i < n; i++)
12076 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12077 complain, in_decl);
12078 return result;
12081 case COND_EXPR:
12082 case MODOP_EXPR:
12083 case PSEUDO_DTOR_EXPR:
12085 r = build_nt
12086 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12087 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12088 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12089 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12090 return r;
12093 case NEW_EXPR:
12095 r = build_nt
12096 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12097 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12098 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12099 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12100 return r;
12103 case DELETE_EXPR:
12105 r = build_nt
12106 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12107 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12108 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12109 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12110 return r;
12113 case TEMPLATE_ID_EXPR:
12115 /* Substituted template arguments */
12116 tree fn = TREE_OPERAND (t, 0);
12117 tree targs = TREE_OPERAND (t, 1);
12119 fn = tsubst_copy (fn, args, complain, in_decl);
12120 if (targs)
12121 targs = tsubst_template_args (targs, args, complain, in_decl);
12123 return lookup_template_function (fn, targs);
12126 case TREE_LIST:
12128 tree purpose, value, chain;
12130 if (t == void_list_node)
12131 return t;
12133 purpose = TREE_PURPOSE (t);
12134 if (purpose)
12135 purpose = tsubst_copy (purpose, args, complain, in_decl);
12136 value = TREE_VALUE (t);
12137 if (value)
12138 value = tsubst_copy (value, args, complain, in_decl);
12139 chain = TREE_CHAIN (t);
12140 if (chain && chain != void_type_node)
12141 chain = tsubst_copy (chain, args, complain, in_decl);
12142 if (purpose == TREE_PURPOSE (t)
12143 && value == TREE_VALUE (t)
12144 && chain == TREE_CHAIN (t))
12145 return t;
12146 return tree_cons (purpose, value, chain);
12149 case RECORD_TYPE:
12150 case UNION_TYPE:
12151 case ENUMERAL_TYPE:
12152 case INTEGER_TYPE:
12153 case TEMPLATE_TYPE_PARM:
12154 case TEMPLATE_TEMPLATE_PARM:
12155 case BOUND_TEMPLATE_TEMPLATE_PARM:
12156 case TEMPLATE_PARM_INDEX:
12157 case POINTER_TYPE:
12158 case REFERENCE_TYPE:
12159 case OFFSET_TYPE:
12160 case FUNCTION_TYPE:
12161 case METHOD_TYPE:
12162 case ARRAY_TYPE:
12163 case TYPENAME_TYPE:
12164 case UNBOUND_CLASS_TEMPLATE:
12165 case TYPEOF_TYPE:
12166 case DECLTYPE_TYPE:
12167 case TYPE_DECL:
12168 return tsubst (t, args, complain, in_decl);
12170 case IDENTIFIER_NODE:
12171 if (IDENTIFIER_TYPENAME_P (t))
12173 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12174 return mangle_conv_op_name_for_type (new_type);
12176 else
12177 return t;
12179 case CONSTRUCTOR:
12180 /* This is handled by tsubst_copy_and_build. */
12181 gcc_unreachable ();
12183 case VA_ARG_EXPR:
12184 return build_x_va_arg (EXPR_LOCATION (t),
12185 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12186 in_decl),
12187 tsubst (TREE_TYPE (t), args, complain, in_decl));
12189 case CLEANUP_POINT_EXPR:
12190 /* We shouldn't have built any of these during initial template
12191 generation. Instead, they should be built during instantiation
12192 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12193 gcc_unreachable ();
12195 case OFFSET_REF:
12196 r = build2
12197 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12198 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12199 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12200 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12201 mark_used (TREE_OPERAND (r, 1));
12202 return r;
12204 case EXPR_PACK_EXPANSION:
12205 error ("invalid use of pack expansion expression");
12206 return error_mark_node;
12208 case NONTYPE_ARGUMENT_PACK:
12209 error ("use %<...%> to expand argument pack");
12210 return error_mark_node;
12212 case INTEGER_CST:
12213 case REAL_CST:
12214 case STRING_CST:
12215 case COMPLEX_CST:
12217 /* Instantiate any typedefs in the type. */
12218 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12219 r = fold_convert (type, t);
12220 gcc_assert (TREE_CODE (r) == code);
12221 return r;
12224 case PTRMEM_CST:
12225 /* These can sometimes show up in a partial instantiation, but never
12226 involve template parms. */
12227 gcc_assert (!uses_template_parms (t));
12228 return t;
12230 default:
12231 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12232 gcc_checking_assert (false);
12233 return t;
12237 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12239 static tree
12240 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12241 tree in_decl)
12243 tree new_clauses = NULL, nc, oc;
12245 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12247 nc = copy_node (oc);
12248 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12249 new_clauses = nc;
12251 switch (OMP_CLAUSE_CODE (nc))
12253 case OMP_CLAUSE_LASTPRIVATE:
12254 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12256 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12257 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12258 in_decl, /*integral_constant_expression_p=*/false);
12259 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12260 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12262 /* FALLTHRU */
12263 case OMP_CLAUSE_PRIVATE:
12264 case OMP_CLAUSE_SHARED:
12265 case OMP_CLAUSE_FIRSTPRIVATE:
12266 case OMP_CLAUSE_REDUCTION:
12267 case OMP_CLAUSE_COPYIN:
12268 case OMP_CLAUSE_COPYPRIVATE:
12269 case OMP_CLAUSE_IF:
12270 case OMP_CLAUSE_NUM_THREADS:
12271 case OMP_CLAUSE_SCHEDULE:
12272 case OMP_CLAUSE_COLLAPSE:
12273 case OMP_CLAUSE_FINAL:
12274 OMP_CLAUSE_OPERAND (nc, 0)
12275 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12276 in_decl, /*integral_constant_expression_p=*/false);
12277 break;
12278 case OMP_CLAUSE_NOWAIT:
12279 case OMP_CLAUSE_ORDERED:
12280 case OMP_CLAUSE_DEFAULT:
12281 case OMP_CLAUSE_UNTIED:
12282 case OMP_CLAUSE_MERGEABLE:
12283 break;
12284 default:
12285 gcc_unreachable ();
12289 return finish_omp_clauses (nreverse (new_clauses));
12292 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12294 static tree
12295 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12296 tree in_decl)
12298 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12300 tree purpose, value, chain;
12302 if (t == NULL)
12303 return t;
12305 if (TREE_CODE (t) != TREE_LIST)
12306 return tsubst_copy_and_build (t, args, complain, in_decl,
12307 /*function_p=*/false,
12308 /*integral_constant_expression_p=*/false);
12310 if (t == void_list_node)
12311 return t;
12313 purpose = TREE_PURPOSE (t);
12314 if (purpose)
12315 purpose = RECUR (purpose);
12316 value = TREE_VALUE (t);
12317 if (value)
12319 if (TREE_CODE (value) != LABEL_DECL)
12320 value = RECUR (value);
12321 else
12323 value = lookup_label (DECL_NAME (value));
12324 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12325 TREE_USED (value) = 1;
12328 chain = TREE_CHAIN (t);
12329 if (chain && chain != void_type_node)
12330 chain = RECUR (chain);
12331 return tree_cons (purpose, value, chain);
12332 #undef RECUR
12335 /* Substitute one OMP_FOR iterator. */
12337 static void
12338 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12339 tree condv, tree incrv, tree *clauses,
12340 tree args, tsubst_flags_t complain, tree in_decl,
12341 bool integral_constant_expression_p)
12343 #define RECUR(NODE) \
12344 tsubst_expr ((NODE), args, complain, in_decl, \
12345 integral_constant_expression_p)
12346 tree decl, init, cond, incr;
12347 bool init_decl;
12349 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12350 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12351 decl = TREE_OPERAND (init, 0);
12352 init = TREE_OPERAND (init, 1);
12353 /* Do this before substituting into decl to handle 'auto'. */
12354 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
12355 init = RECUR (init);
12356 decl = RECUR (decl);
12357 if (init_decl)
12359 gcc_assert (!processing_template_decl);
12360 init = DECL_INITIAL (decl);
12361 DECL_INITIAL (decl) = NULL_TREE;
12364 gcc_assert (!type_dependent_expression_p (decl));
12366 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12368 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12369 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12370 if (TREE_CODE (incr) == MODIFY_EXPR)
12371 incr = build_x_modify_expr (EXPR_LOCATION (incr),
12372 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12373 RECUR (TREE_OPERAND (incr, 1)),
12374 complain);
12375 else
12376 incr = RECUR (incr);
12377 TREE_VEC_ELT (declv, i) = decl;
12378 TREE_VEC_ELT (initv, i) = init;
12379 TREE_VEC_ELT (condv, i) = cond;
12380 TREE_VEC_ELT (incrv, i) = incr;
12381 return;
12384 if (init && !init_decl)
12386 tree c;
12387 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12389 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12390 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12391 && OMP_CLAUSE_DECL (c) == decl)
12392 break;
12393 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12394 && OMP_CLAUSE_DECL (c) == decl)
12395 error ("iteration variable %qD should not be firstprivate", decl);
12396 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12397 && OMP_CLAUSE_DECL (c) == decl)
12398 error ("iteration variable %qD should not be reduction", decl);
12400 if (c == NULL)
12402 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12403 OMP_CLAUSE_DECL (c) = decl;
12404 c = finish_omp_clauses (c);
12405 if (c)
12407 OMP_CLAUSE_CHAIN (c) = *clauses;
12408 *clauses = c;
12412 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12413 if (COMPARISON_CLASS_P (cond))
12414 cond = build2 (TREE_CODE (cond), boolean_type_node,
12415 RECUR (TREE_OPERAND (cond, 0)),
12416 RECUR (TREE_OPERAND (cond, 1)));
12417 else
12418 cond = RECUR (cond);
12419 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12420 switch (TREE_CODE (incr))
12422 case PREINCREMENT_EXPR:
12423 case PREDECREMENT_EXPR:
12424 case POSTINCREMENT_EXPR:
12425 case POSTDECREMENT_EXPR:
12426 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12427 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12428 break;
12429 case MODIFY_EXPR:
12430 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12431 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12433 tree rhs = TREE_OPERAND (incr, 1);
12434 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12435 RECUR (TREE_OPERAND (incr, 0)),
12436 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12437 RECUR (TREE_OPERAND (rhs, 0)),
12438 RECUR (TREE_OPERAND (rhs, 1))));
12440 else
12441 incr = RECUR (incr);
12442 break;
12443 case MODOP_EXPR:
12444 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12445 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12447 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12448 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12449 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12450 TREE_TYPE (decl), lhs,
12451 RECUR (TREE_OPERAND (incr, 2))));
12453 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12454 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12455 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12457 tree rhs = TREE_OPERAND (incr, 2);
12458 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12459 RECUR (TREE_OPERAND (incr, 0)),
12460 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12461 RECUR (TREE_OPERAND (rhs, 0)),
12462 RECUR (TREE_OPERAND (rhs, 1))));
12464 else
12465 incr = RECUR (incr);
12466 break;
12467 default:
12468 incr = RECUR (incr);
12469 break;
12472 TREE_VEC_ELT (declv, i) = decl;
12473 TREE_VEC_ELT (initv, i) = init;
12474 TREE_VEC_ELT (condv, i) = cond;
12475 TREE_VEC_ELT (incrv, i) = incr;
12476 #undef RECUR
12479 /* Like tsubst_copy for expressions, etc. but also does semantic
12480 processing. */
12482 static tree
12483 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12484 bool integral_constant_expression_p)
12486 #define RECUR(NODE) \
12487 tsubst_expr ((NODE), args, complain, in_decl, \
12488 integral_constant_expression_p)
12490 tree stmt, tmp;
12492 if (t == NULL_TREE || t == error_mark_node)
12493 return t;
12495 if (EXPR_HAS_LOCATION (t))
12496 input_location = EXPR_LOCATION (t);
12497 if (STATEMENT_CODE_P (TREE_CODE (t)))
12498 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12500 switch (TREE_CODE (t))
12502 case STATEMENT_LIST:
12504 tree_stmt_iterator i;
12505 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12506 RECUR (tsi_stmt (i));
12507 break;
12510 case CTOR_INITIALIZER:
12511 finish_mem_initializers (tsubst_initializer_list
12512 (TREE_OPERAND (t, 0), args));
12513 break;
12515 case RETURN_EXPR:
12516 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12517 break;
12519 case EXPR_STMT:
12520 tmp = RECUR (EXPR_STMT_EXPR (t));
12521 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12522 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12523 else
12524 finish_expr_stmt (tmp);
12525 break;
12527 case USING_STMT:
12528 do_using_directive (USING_STMT_NAMESPACE (t));
12529 break;
12531 case DECL_EXPR:
12533 tree decl, pattern_decl;
12534 tree init;
12536 pattern_decl = decl = DECL_EXPR_DECL (t);
12537 if (TREE_CODE (decl) == LABEL_DECL)
12538 finish_label_decl (DECL_NAME (decl));
12539 else if (TREE_CODE (decl) == USING_DECL)
12541 tree scope = USING_DECL_SCOPE (decl);
12542 tree name = DECL_NAME (decl);
12543 tree decl;
12545 scope = tsubst (scope, args, complain, in_decl);
12546 decl = lookup_qualified_name (scope, name,
12547 /*is_type_p=*/false,
12548 /*complain=*/false);
12549 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12550 qualified_name_lookup_error (scope, name, decl, input_location);
12551 else
12552 do_local_using_decl (decl, scope, name);
12554 else
12556 init = DECL_INITIAL (decl);
12557 decl = tsubst (decl, args, complain, in_decl);
12558 if (decl != error_mark_node)
12560 /* By marking the declaration as instantiated, we avoid
12561 trying to instantiate it. Since instantiate_decl can't
12562 handle local variables, and since we've already done
12563 all that needs to be done, that's the right thing to
12564 do. */
12565 if (TREE_CODE (decl) == VAR_DECL)
12566 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12567 if (TREE_CODE (decl) == VAR_DECL
12568 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12569 /* Anonymous aggregates are a special case. */
12570 finish_anon_union (decl);
12571 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
12573 DECL_CONTEXT (decl) = current_function_decl;
12574 insert_capture_proxy (decl);
12576 else if (DECL_IMPLICIT_TYPEDEF_P (t))
12577 /* We already did a pushtag. */;
12578 else
12580 int const_init = false;
12581 maybe_push_decl (decl);
12582 if (TREE_CODE (decl) == VAR_DECL
12583 && DECL_PRETTY_FUNCTION_P (decl))
12585 /* For __PRETTY_FUNCTION__ we have to adjust the
12586 initializer. */
12587 const char *const name
12588 = cxx_printable_name (current_function_decl, 2);
12589 init = cp_fname_init (name, &TREE_TYPE (decl));
12591 else
12593 tree t = RECUR (init);
12595 if (init && !t)
12597 /* If we had an initializer but it
12598 instantiated to nothing,
12599 value-initialize the object. This will
12600 only occur when the initializer was a
12601 pack expansion where the parameter packs
12602 used in that expansion were of length
12603 zero. */
12604 init = build_value_init (TREE_TYPE (decl),
12605 complain);
12606 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12607 init = get_target_expr_sfinae (init, complain);
12609 else
12610 init = t;
12613 if (TREE_CODE (decl) == VAR_DECL)
12614 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12615 (pattern_decl));
12616 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12621 break;
12624 case FOR_STMT:
12625 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12626 RECUR (FOR_INIT_STMT (t));
12627 finish_for_init_stmt (stmt);
12628 tmp = RECUR (FOR_COND (t));
12629 finish_for_cond (tmp, stmt);
12630 tmp = RECUR (FOR_EXPR (t));
12631 finish_for_expr (tmp, stmt);
12632 RECUR (FOR_BODY (t));
12633 finish_for_stmt (stmt);
12634 break;
12636 case RANGE_FOR_STMT:
12638 tree decl, expr;
12639 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12640 decl = RANGE_FOR_DECL (t);
12641 decl = tsubst (decl, args, complain, in_decl);
12642 maybe_push_decl (decl);
12643 expr = RECUR (RANGE_FOR_EXPR (t));
12644 stmt = cp_convert_range_for (stmt, decl, expr);
12645 RECUR (RANGE_FOR_BODY (t));
12646 finish_for_stmt (stmt);
12648 break;
12650 case WHILE_STMT:
12651 stmt = begin_while_stmt ();
12652 tmp = RECUR (WHILE_COND (t));
12653 finish_while_stmt_cond (tmp, stmt);
12654 RECUR (WHILE_BODY (t));
12655 finish_while_stmt (stmt);
12656 break;
12658 case DO_STMT:
12659 stmt = begin_do_stmt ();
12660 RECUR (DO_BODY (t));
12661 finish_do_body (stmt);
12662 tmp = RECUR (DO_COND (t));
12663 finish_do_stmt (tmp, stmt);
12664 break;
12666 case IF_STMT:
12667 stmt = begin_if_stmt ();
12668 tmp = RECUR (IF_COND (t));
12669 finish_if_stmt_cond (tmp, stmt);
12670 RECUR (THEN_CLAUSE (t));
12671 finish_then_clause (stmt);
12673 if (ELSE_CLAUSE (t))
12675 begin_else_clause (stmt);
12676 RECUR (ELSE_CLAUSE (t));
12677 finish_else_clause (stmt);
12680 finish_if_stmt (stmt);
12681 break;
12683 case BIND_EXPR:
12684 if (BIND_EXPR_BODY_BLOCK (t))
12685 stmt = begin_function_body ();
12686 else
12687 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12688 ? BCS_TRY_BLOCK : 0);
12690 RECUR (BIND_EXPR_BODY (t));
12692 if (BIND_EXPR_BODY_BLOCK (t))
12693 finish_function_body (stmt);
12694 else
12695 finish_compound_stmt (stmt);
12696 break;
12698 case BREAK_STMT:
12699 finish_break_stmt ();
12700 break;
12702 case CONTINUE_STMT:
12703 finish_continue_stmt ();
12704 break;
12706 case SWITCH_STMT:
12707 stmt = begin_switch_stmt ();
12708 tmp = RECUR (SWITCH_STMT_COND (t));
12709 finish_switch_cond (tmp, stmt);
12710 RECUR (SWITCH_STMT_BODY (t));
12711 finish_switch_stmt (stmt);
12712 break;
12714 case CASE_LABEL_EXPR:
12715 finish_case_label (EXPR_LOCATION (t),
12716 RECUR (CASE_LOW (t)),
12717 RECUR (CASE_HIGH (t)));
12718 break;
12720 case LABEL_EXPR:
12722 tree decl = LABEL_EXPR_LABEL (t);
12723 tree label;
12725 label = finish_label_stmt (DECL_NAME (decl));
12726 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12727 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12729 break;
12731 case GOTO_EXPR:
12732 tmp = GOTO_DESTINATION (t);
12733 if (TREE_CODE (tmp) != LABEL_DECL)
12734 /* Computed goto's must be tsubst'd into. On the other hand,
12735 non-computed gotos must not be; the identifier in question
12736 will have no binding. */
12737 tmp = RECUR (tmp);
12738 else
12739 tmp = DECL_NAME (tmp);
12740 finish_goto_stmt (tmp);
12741 break;
12743 case ASM_EXPR:
12744 tmp = finish_asm_stmt
12745 (ASM_VOLATILE_P (t),
12746 RECUR (ASM_STRING (t)),
12747 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12748 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12749 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12750 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12752 tree asm_expr = tmp;
12753 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12754 asm_expr = TREE_OPERAND (asm_expr, 0);
12755 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12757 break;
12759 case TRY_BLOCK:
12760 if (CLEANUP_P (t))
12762 stmt = begin_try_block ();
12763 RECUR (TRY_STMTS (t));
12764 finish_cleanup_try_block (stmt);
12765 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12767 else
12769 tree compound_stmt = NULL_TREE;
12771 if (FN_TRY_BLOCK_P (t))
12772 stmt = begin_function_try_block (&compound_stmt);
12773 else
12774 stmt = begin_try_block ();
12776 RECUR (TRY_STMTS (t));
12778 if (FN_TRY_BLOCK_P (t))
12779 finish_function_try_block (stmt);
12780 else
12781 finish_try_block (stmt);
12783 RECUR (TRY_HANDLERS (t));
12784 if (FN_TRY_BLOCK_P (t))
12785 finish_function_handler_sequence (stmt, compound_stmt);
12786 else
12787 finish_handler_sequence (stmt);
12789 break;
12791 case HANDLER:
12793 tree decl = HANDLER_PARMS (t);
12795 if (decl)
12797 decl = tsubst (decl, args, complain, in_decl);
12798 /* Prevent instantiate_decl from trying to instantiate
12799 this variable. We've already done all that needs to be
12800 done. */
12801 if (decl != error_mark_node)
12802 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12804 stmt = begin_handler ();
12805 finish_handler_parms (decl, stmt);
12806 RECUR (HANDLER_BODY (t));
12807 finish_handler (stmt);
12809 break;
12811 case TAG_DEFN:
12812 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12813 if (CLASS_TYPE_P (tmp))
12815 /* Local classes are not independent templates; they are
12816 instantiated along with their containing function. And this
12817 way we don't have to deal with pushing out of one local class
12818 to instantiate a member of another local class. */
12819 tree fn;
12820 /* Closures are handled by the LAMBDA_EXPR. */
12821 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
12822 complete_type (tmp);
12823 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
12824 if (!DECL_ARTIFICIAL (fn))
12825 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
12827 break;
12829 case STATIC_ASSERT:
12831 tree condition;
12833 ++c_inhibit_evaluation_warnings;
12834 condition =
12835 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12836 args,
12837 complain, in_decl,
12838 /*integral_constant_expression_p=*/true);
12839 --c_inhibit_evaluation_warnings;
12841 finish_static_assert (condition,
12842 STATIC_ASSERT_MESSAGE (t),
12843 STATIC_ASSERT_SOURCE_LOCATION (t),
12844 /*member_p=*/false);
12846 break;
12848 case OMP_PARALLEL:
12849 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
12850 args, complain, in_decl);
12851 stmt = begin_omp_parallel ();
12852 RECUR (OMP_PARALLEL_BODY (t));
12853 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
12854 = OMP_PARALLEL_COMBINED (t);
12855 break;
12857 case OMP_TASK:
12858 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
12859 args, complain, in_decl);
12860 stmt = begin_omp_task ();
12861 RECUR (OMP_TASK_BODY (t));
12862 finish_omp_task (tmp, stmt);
12863 break;
12865 case OMP_FOR:
12867 tree clauses, body, pre_body;
12868 tree declv, initv, condv, incrv;
12869 int i;
12871 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
12872 args, complain, in_decl);
12873 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12874 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12875 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12876 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12878 stmt = begin_omp_structured_block ();
12880 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
12881 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
12882 &clauses, args, complain, in_decl,
12883 integral_constant_expression_p);
12885 pre_body = push_stmt_list ();
12886 RECUR (OMP_FOR_PRE_BODY (t));
12887 pre_body = pop_stmt_list (pre_body);
12889 body = push_stmt_list ();
12890 RECUR (OMP_FOR_BODY (t));
12891 body = pop_stmt_list (body);
12893 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12894 body, pre_body, clauses);
12896 add_stmt (finish_omp_structured_block (stmt));
12898 break;
12900 case OMP_SECTIONS:
12901 case OMP_SINGLE:
12902 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12903 stmt = push_stmt_list ();
12904 RECUR (OMP_BODY (t));
12905 stmt = pop_stmt_list (stmt);
12907 t = copy_node (t);
12908 OMP_BODY (t) = stmt;
12909 OMP_CLAUSES (t) = tmp;
12910 add_stmt (t);
12911 break;
12913 case OMP_SECTION:
12914 case OMP_CRITICAL:
12915 case OMP_MASTER:
12916 case OMP_ORDERED:
12917 stmt = push_stmt_list ();
12918 RECUR (OMP_BODY (t));
12919 stmt = pop_stmt_list (stmt);
12921 t = copy_node (t);
12922 OMP_BODY (t) = stmt;
12923 add_stmt (t);
12924 break;
12926 case OMP_ATOMIC:
12927 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12928 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
12930 tree op1 = TREE_OPERAND (t, 1);
12931 tree rhs1 = NULL_TREE;
12932 tree lhs, rhs;
12933 if (TREE_CODE (op1) == COMPOUND_EXPR)
12935 rhs1 = RECUR (TREE_OPERAND (op1, 0));
12936 op1 = TREE_OPERAND (op1, 1);
12938 lhs = RECUR (TREE_OPERAND (op1, 0));
12939 rhs = RECUR (TREE_OPERAND (op1, 1));
12940 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
12941 NULL_TREE, NULL_TREE, rhs1);
12943 else
12945 tree op1 = TREE_OPERAND (t, 1);
12946 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
12947 tree rhs1 = NULL_TREE;
12948 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
12949 enum tree_code opcode = NOP_EXPR;
12950 if (code == OMP_ATOMIC_READ)
12952 v = RECUR (TREE_OPERAND (op1, 0));
12953 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
12955 else if (code == OMP_ATOMIC_CAPTURE_OLD
12956 || code == OMP_ATOMIC_CAPTURE_NEW)
12958 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
12959 v = RECUR (TREE_OPERAND (op1, 0));
12960 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
12961 if (TREE_CODE (op11) == COMPOUND_EXPR)
12963 rhs1 = RECUR (TREE_OPERAND (op11, 0));
12964 op11 = TREE_OPERAND (op11, 1);
12966 lhs = RECUR (TREE_OPERAND (op11, 0));
12967 rhs = RECUR (TREE_OPERAND (op11, 1));
12968 opcode = TREE_CODE (op11);
12970 else
12972 code = OMP_ATOMIC;
12973 lhs = RECUR (TREE_OPERAND (op1, 0));
12974 rhs = RECUR (TREE_OPERAND (op1, 1));
12976 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
12978 break;
12980 case TRANSACTION_EXPR:
12982 int flags = 0;
12983 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
12984 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
12986 if (TRANSACTION_EXPR_IS_STMT (t))
12988 tree body = TRANSACTION_EXPR_BODY (t);
12989 tree noex = NULL_TREE;
12990 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
12992 noex = MUST_NOT_THROW_COND (body);
12993 if (noex == NULL_TREE)
12994 noex = boolean_true_node;
12995 body = TREE_OPERAND (body, 0);
12997 stmt = begin_transaction_stmt (input_location, NULL, flags);
12998 RECUR (body);
12999 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13001 else
13003 stmt = build_transaction_expr (EXPR_LOCATION (t),
13004 RECUR (TRANSACTION_EXPR_BODY (t)),
13005 flags, NULL_TREE);
13006 return stmt;
13009 break;
13011 case MUST_NOT_THROW_EXPR:
13012 return build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13013 RECUR (MUST_NOT_THROW_COND (t)));
13015 case EXPR_PACK_EXPANSION:
13016 error ("invalid use of pack expansion expression");
13017 return error_mark_node;
13019 case NONTYPE_ARGUMENT_PACK:
13020 error ("use %<...%> to expand argument pack");
13021 return error_mark_node;
13023 case COMPOUND_EXPR:
13024 tmp = RECUR (TREE_OPERAND (t, 0));
13025 if (tmp == NULL_TREE)
13026 /* If the first operand was a statement, we're done with it. */
13027 return RECUR (TREE_OPERAND (t, 1));
13028 return build_x_compound_expr (EXPR_LOCATION (t), tmp,
13029 RECUR (TREE_OPERAND (t, 1)),
13030 complain);
13032 default:
13033 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13035 return tsubst_copy_and_build (t, args, complain, in_decl,
13036 /*function_p=*/false,
13037 integral_constant_expression_p);
13040 return NULL_TREE;
13041 #undef RECUR
13044 /* T is a postfix-expression that is not being used in a function
13045 call. Return the substituted version of T. */
13047 static tree
13048 tsubst_non_call_postfix_expression (tree t, tree args,
13049 tsubst_flags_t complain,
13050 tree in_decl)
13052 if (TREE_CODE (t) == SCOPE_REF)
13053 t = tsubst_qualified_id (t, args, complain, in_decl,
13054 /*done=*/false, /*address_p=*/false);
13055 else
13056 t = tsubst_copy_and_build (t, args, complain, in_decl,
13057 /*function_p=*/false,
13058 /*integral_constant_expression_p=*/false);
13060 return t;
13063 /* Like tsubst but deals with expressions and performs semantic
13064 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13066 tree
13067 tsubst_copy_and_build (tree t,
13068 tree args,
13069 tsubst_flags_t complain,
13070 tree in_decl,
13071 bool function_p,
13072 bool integral_constant_expression_p)
13074 #define RECUR(NODE) \
13075 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13076 /*function_p=*/false, \
13077 integral_constant_expression_p)
13079 tree op1;
13081 if (t == NULL_TREE || t == error_mark_node)
13082 return t;
13084 switch (TREE_CODE (t))
13086 case USING_DECL:
13087 t = DECL_NAME (t);
13088 /* Fall through. */
13089 case IDENTIFIER_NODE:
13091 tree decl;
13092 cp_id_kind idk;
13093 bool non_integral_constant_expression_p;
13094 const char *error_msg;
13096 if (IDENTIFIER_TYPENAME_P (t))
13098 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13099 t = mangle_conv_op_name_for_type (new_type);
13102 /* Look up the name. */
13103 decl = lookup_name (t);
13105 /* By convention, expressions use ERROR_MARK_NODE to indicate
13106 failure, not NULL_TREE. */
13107 if (decl == NULL_TREE)
13108 decl = error_mark_node;
13110 decl = finish_id_expression (t, decl, NULL_TREE,
13111 &idk,
13112 integral_constant_expression_p,
13113 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
13114 &non_integral_constant_expression_p,
13115 /*template_p=*/false,
13116 /*done=*/true,
13117 /*address_p=*/false,
13118 /*template_arg_p=*/false,
13119 &error_msg,
13120 input_location);
13121 if (error_msg)
13122 error (error_msg);
13123 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
13125 if (complain & tf_error)
13126 unqualified_name_lookup_error (decl);
13127 decl = error_mark_node;
13129 return decl;
13132 case TEMPLATE_ID_EXPR:
13134 tree object;
13135 tree templ = RECUR (TREE_OPERAND (t, 0));
13136 tree targs = TREE_OPERAND (t, 1);
13138 if (targs)
13139 targs = tsubst_template_args (targs, args, complain, in_decl);
13141 if (TREE_CODE (templ) == COMPONENT_REF)
13143 object = TREE_OPERAND (templ, 0);
13144 templ = TREE_OPERAND (templ, 1);
13146 else
13147 object = NULL_TREE;
13148 templ = lookup_template_function (templ, targs);
13150 if (object)
13151 return build3 (COMPONENT_REF, TREE_TYPE (templ),
13152 object, templ, NULL_TREE);
13153 else
13154 return baselink_for_fns (templ);
13157 case INDIRECT_REF:
13159 tree r = RECUR (TREE_OPERAND (t, 0));
13161 if (REFERENCE_REF_P (t))
13163 /* A type conversion to reference type will be enclosed in
13164 such an indirect ref, but the substitution of the cast
13165 will have also added such an indirect ref. */
13166 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13167 r = convert_from_reference (r);
13169 else
13170 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, complain);
13171 return r;
13174 case NOP_EXPR:
13175 return build_nop
13176 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13177 RECUR (TREE_OPERAND (t, 0)));
13179 case IMPLICIT_CONV_EXPR:
13181 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13182 tree expr = RECUR (TREE_OPERAND (t, 0));
13183 int flags = LOOKUP_IMPLICIT;
13184 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13185 flags = LOOKUP_NORMAL;
13186 return perform_implicit_conversion_flags (type, expr, complain,
13187 flags);
13190 case CONVERT_EXPR:
13191 return build1
13192 (CONVERT_EXPR,
13193 tsubst (TREE_TYPE (t), args, complain, in_decl),
13194 RECUR (TREE_OPERAND (t, 0)));
13196 case CAST_EXPR:
13197 case REINTERPRET_CAST_EXPR:
13198 case CONST_CAST_EXPR:
13199 case DYNAMIC_CAST_EXPR:
13200 case STATIC_CAST_EXPR:
13202 tree type;
13203 tree op, r = NULL_TREE;
13205 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13206 if (integral_constant_expression_p
13207 && !cast_valid_in_integral_constant_expression_p (type))
13209 if (complain & tf_error)
13210 error ("a cast to a type other than an integral or "
13211 "enumeration type cannot appear in a constant-expression");
13212 return error_mark_node;
13215 op = RECUR (TREE_OPERAND (t, 0));
13217 ++c_inhibit_evaluation_warnings;
13218 switch (TREE_CODE (t))
13220 case CAST_EXPR:
13221 r = build_functional_cast (type, op, complain);
13222 break;
13223 case REINTERPRET_CAST_EXPR:
13224 r = build_reinterpret_cast (type, op, complain);
13225 break;
13226 case CONST_CAST_EXPR:
13227 r = build_const_cast (type, op, complain);
13228 break;
13229 case DYNAMIC_CAST_EXPR:
13230 r = build_dynamic_cast (type, op, complain);
13231 break;
13232 case STATIC_CAST_EXPR:
13233 r = build_static_cast (type, op, complain);
13234 break;
13235 default:
13236 gcc_unreachable ();
13238 --c_inhibit_evaluation_warnings;
13240 return r;
13243 case POSTDECREMENT_EXPR:
13244 case POSTINCREMENT_EXPR:
13245 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13246 args, complain, in_decl);
13247 return build_x_unary_op (input_location, TREE_CODE (t), op1, complain);
13249 case PREDECREMENT_EXPR:
13250 case PREINCREMENT_EXPR:
13251 case NEGATE_EXPR:
13252 case BIT_NOT_EXPR:
13253 case ABS_EXPR:
13254 case TRUTH_NOT_EXPR:
13255 case UNARY_PLUS_EXPR: /* Unary + */
13256 case REALPART_EXPR:
13257 case IMAGPART_EXPR:
13258 return build_x_unary_op (input_location, TREE_CODE (t),
13259 RECUR (TREE_OPERAND (t, 0)), complain);
13261 case FIX_TRUNC_EXPR:
13262 return cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
13263 0, complain);
13265 case ADDR_EXPR:
13266 op1 = TREE_OPERAND (t, 0);
13267 if (TREE_CODE (op1) == LABEL_DECL)
13268 return finish_label_address_expr (DECL_NAME (op1),
13269 EXPR_LOCATION (op1));
13270 if (TREE_CODE (op1) == SCOPE_REF)
13271 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13272 /*done=*/true, /*address_p=*/true);
13273 else
13274 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13275 in_decl);
13276 return build_x_unary_op (input_location, ADDR_EXPR, op1, complain);
13278 case PLUS_EXPR:
13279 case MINUS_EXPR:
13280 case MULT_EXPR:
13281 case TRUNC_DIV_EXPR:
13282 case CEIL_DIV_EXPR:
13283 case FLOOR_DIV_EXPR:
13284 case ROUND_DIV_EXPR:
13285 case EXACT_DIV_EXPR:
13286 case BIT_AND_EXPR:
13287 case BIT_IOR_EXPR:
13288 case BIT_XOR_EXPR:
13289 case TRUNC_MOD_EXPR:
13290 case FLOOR_MOD_EXPR:
13291 case TRUTH_ANDIF_EXPR:
13292 case TRUTH_ORIF_EXPR:
13293 case TRUTH_AND_EXPR:
13294 case TRUTH_OR_EXPR:
13295 case RSHIFT_EXPR:
13296 case LSHIFT_EXPR:
13297 case RROTATE_EXPR:
13298 case LROTATE_EXPR:
13299 case EQ_EXPR:
13300 case NE_EXPR:
13301 case MAX_EXPR:
13302 case MIN_EXPR:
13303 case LE_EXPR:
13304 case GE_EXPR:
13305 case LT_EXPR:
13306 case GT_EXPR:
13307 case MEMBER_REF:
13308 case DOTSTAR_EXPR:
13310 tree r;
13312 ++c_inhibit_evaluation_warnings;
13314 r = build_x_binary_op
13315 (input_location, TREE_CODE (t),
13316 RECUR (TREE_OPERAND (t, 0)),
13317 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13318 ? ERROR_MARK
13319 : TREE_CODE (TREE_OPERAND (t, 0))),
13320 RECUR (TREE_OPERAND (t, 1)),
13321 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13322 ? ERROR_MARK
13323 : TREE_CODE (TREE_OPERAND (t, 1))),
13324 /*overload=*/NULL,
13325 complain);
13326 if (EXPR_P (r) && TREE_NO_WARNING (t))
13327 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13329 --c_inhibit_evaluation_warnings;
13331 return r;
13334 case SCOPE_REF:
13335 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13336 /*address_p=*/false);
13337 case ARRAY_REF:
13338 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13339 args, complain, in_decl);
13340 return build_x_array_ref (EXPR_LOCATION (t), op1,
13341 RECUR (TREE_OPERAND (t, 1)), complain);
13343 case SIZEOF_EXPR:
13344 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13345 return tsubst_copy (t, args, complain, in_decl);
13346 /* Fall through */
13348 case ALIGNOF_EXPR:
13349 op1 = TREE_OPERAND (t, 0);
13350 if (!args)
13352 /* When there are no ARGS, we are trying to evaluate a
13353 non-dependent expression from the parser. Trying to do
13354 the substitutions may not work. */
13355 if (!TYPE_P (op1))
13356 op1 = TREE_TYPE (op1);
13358 else
13360 ++cp_unevaluated_operand;
13361 ++c_inhibit_evaluation_warnings;
13362 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13363 /*function_p=*/false,
13364 /*integral_constant_expression_p=*/false);
13365 --cp_unevaluated_operand;
13366 --c_inhibit_evaluation_warnings;
13368 if (TYPE_P (op1))
13369 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13370 complain & tf_error);
13371 else
13372 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13373 complain & tf_error);
13375 case AT_ENCODE_EXPR:
13377 op1 = TREE_OPERAND (t, 0);
13378 ++cp_unevaluated_operand;
13379 ++c_inhibit_evaluation_warnings;
13380 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13381 /*function_p=*/false,
13382 /*integral_constant_expression_p=*/false);
13383 --cp_unevaluated_operand;
13384 --c_inhibit_evaluation_warnings;
13385 return objc_build_encode_expr (op1);
13388 case NOEXCEPT_EXPR:
13389 op1 = TREE_OPERAND (t, 0);
13390 ++cp_unevaluated_operand;
13391 ++c_inhibit_evaluation_warnings;
13392 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13393 /*function_p=*/false,
13394 /*integral_constant_expression_p=*/false);
13395 --cp_unevaluated_operand;
13396 --c_inhibit_evaluation_warnings;
13397 return finish_noexcept_expr (op1, complain);
13399 case MODOP_EXPR:
13401 tree r = build_x_modify_expr
13402 (EXPR_LOCATION (t),
13403 RECUR (TREE_OPERAND (t, 0)),
13404 TREE_CODE (TREE_OPERAND (t, 1)),
13405 RECUR (TREE_OPERAND (t, 2)),
13406 complain);
13407 /* TREE_NO_WARNING must be set if either the expression was
13408 parenthesized or it uses an operator such as >>= rather
13409 than plain assignment. In the former case, it was already
13410 set and must be copied. In the latter case,
13411 build_x_modify_expr sets it and it must not be reset
13412 here. */
13413 if (TREE_NO_WARNING (t))
13414 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13415 return r;
13418 case ARROW_EXPR:
13419 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13420 args, complain, in_decl);
13421 /* Remember that there was a reference to this entity. */
13422 if (DECL_P (op1))
13423 mark_used (op1);
13424 return build_x_arrow (input_location, op1, complain);
13426 case NEW_EXPR:
13428 tree placement = RECUR (TREE_OPERAND (t, 0));
13429 tree init = RECUR (TREE_OPERAND (t, 3));
13430 VEC(tree,gc) *placement_vec;
13431 VEC(tree,gc) *init_vec;
13432 tree ret;
13434 if (placement == NULL_TREE)
13435 placement_vec = NULL;
13436 else
13438 placement_vec = make_tree_vector ();
13439 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13440 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
13443 /* If there was an initializer in the original tree, but it
13444 instantiated to an empty list, then we should pass a
13445 non-NULL empty vector to tell build_new that it was an
13446 empty initializer() rather than no initializer. This can
13447 only happen when the initializer is a pack expansion whose
13448 parameter packs are of length zero. */
13449 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13450 init_vec = NULL;
13451 else
13453 init_vec = make_tree_vector ();
13454 if (init == void_zero_node)
13455 gcc_assert (init_vec != NULL);
13456 else
13458 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13459 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
13463 ret = build_new (&placement_vec,
13464 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13465 RECUR (TREE_OPERAND (t, 2)),
13466 &init_vec,
13467 NEW_EXPR_USE_GLOBAL (t),
13468 complain);
13470 if (placement_vec != NULL)
13471 release_tree_vector (placement_vec);
13472 if (init_vec != NULL)
13473 release_tree_vector (init_vec);
13475 return ret;
13478 case DELETE_EXPR:
13479 return delete_sanity
13480 (RECUR (TREE_OPERAND (t, 0)),
13481 RECUR (TREE_OPERAND (t, 1)),
13482 DELETE_EXPR_USE_VEC (t),
13483 DELETE_EXPR_USE_GLOBAL (t),
13484 complain);
13486 case COMPOUND_EXPR:
13487 return build_x_compound_expr (EXPR_LOCATION (t),
13488 RECUR (TREE_OPERAND (t, 0)),
13489 RECUR (TREE_OPERAND (t, 1)),
13490 complain);
13492 case CALL_EXPR:
13494 tree function;
13495 VEC(tree,gc) *call_args;
13496 unsigned int nargs, i;
13497 bool qualified_p;
13498 bool koenig_p;
13499 tree ret;
13501 function = CALL_EXPR_FN (t);
13502 /* When we parsed the expression, we determined whether or
13503 not Koenig lookup should be performed. */
13504 koenig_p = KOENIG_LOOKUP_P (t);
13505 if (TREE_CODE (function) == SCOPE_REF)
13507 qualified_p = true;
13508 function = tsubst_qualified_id (function, args, complain, in_decl,
13509 /*done=*/false,
13510 /*address_p=*/false);
13512 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13514 /* Do nothing; calling tsubst_copy_and_build on an identifier
13515 would incorrectly perform unqualified lookup again.
13517 Note that we can also have an IDENTIFIER_NODE if the earlier
13518 unqualified lookup found a member function; in that case
13519 koenig_p will be false and we do want to do the lookup
13520 again to find the instantiated member function.
13522 FIXME but doing that causes c++/15272, so we need to stop
13523 using IDENTIFIER_NODE in that situation. */
13524 qualified_p = false;
13526 else
13528 if (TREE_CODE (function) == COMPONENT_REF)
13530 tree op = TREE_OPERAND (function, 1);
13532 qualified_p = (TREE_CODE (op) == SCOPE_REF
13533 || (BASELINK_P (op)
13534 && BASELINK_QUALIFIED_P (op)));
13536 else
13537 qualified_p = false;
13539 function = tsubst_copy_and_build (function, args, complain,
13540 in_decl,
13541 !qualified_p,
13542 integral_constant_expression_p);
13544 if (BASELINK_P (function))
13545 qualified_p = true;
13548 nargs = call_expr_nargs (t);
13549 call_args = make_tree_vector ();
13550 for (i = 0; i < nargs; ++i)
13552 tree arg = CALL_EXPR_ARG (t, i);
13554 if (!PACK_EXPANSION_P (arg))
13555 VEC_safe_push (tree, gc, call_args,
13556 RECUR (CALL_EXPR_ARG (t, i)));
13557 else
13559 /* Expand the pack expansion and push each entry onto
13560 CALL_ARGS. */
13561 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13562 if (TREE_CODE (arg) == TREE_VEC)
13564 unsigned int len, j;
13566 len = TREE_VEC_LENGTH (arg);
13567 for (j = 0; j < len; ++j)
13569 tree value = TREE_VEC_ELT (arg, j);
13570 if (value != NULL_TREE)
13571 value = convert_from_reference (value);
13572 VEC_safe_push (tree, gc, call_args, value);
13575 else
13577 /* A partial substitution. Add one entry. */
13578 VEC_safe_push (tree, gc, call_args, arg);
13583 /* We do not perform argument-dependent lookup if normal
13584 lookup finds a non-function, in accordance with the
13585 expected resolution of DR 218. */
13586 if (koenig_p
13587 && ((is_overloaded_fn (function)
13588 /* If lookup found a member function, the Koenig lookup is
13589 not appropriate, even if an unqualified-name was used
13590 to denote the function. */
13591 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13592 || TREE_CODE (function) == IDENTIFIER_NODE)
13593 /* Only do this when substitution turns a dependent call
13594 into a non-dependent call. */
13595 && type_dependent_expression_p_push (t)
13596 && !any_type_dependent_arguments_p (call_args))
13597 function = perform_koenig_lookup (function, call_args, false,
13598 tf_none);
13600 if (TREE_CODE (function) == IDENTIFIER_NODE
13601 && !any_type_dependent_arguments_p (call_args))
13603 if (koenig_p && (complain & tf_warning_or_error))
13605 /* For backwards compatibility and good diagnostics, try
13606 the unqualified lookup again if we aren't in SFINAE
13607 context. */
13608 tree unq = (tsubst_copy_and_build
13609 (function, args, complain, in_decl, true,
13610 integral_constant_expression_p));
13611 if (unq == error_mark_node)
13612 return error_mark_node;
13614 if (unq != function)
13616 tree fn = unq;
13617 if (TREE_CODE (fn) == INDIRECT_REF)
13618 fn = TREE_OPERAND (fn, 0);
13619 if (TREE_CODE (fn) == COMPONENT_REF)
13620 fn = TREE_OPERAND (fn, 1);
13621 if (is_overloaded_fn (fn))
13622 fn = get_first_fn (fn);
13623 permerror (EXPR_LOC_OR_HERE (t),
13624 "%qD was not declared in this scope, "
13625 "and no declarations were found by "
13626 "argument-dependent lookup at the point "
13627 "of instantiation", function);
13628 if (!DECL_P (fn))
13629 /* Can't say anything more. */;
13630 else if (DECL_CLASS_SCOPE_P (fn))
13632 inform (EXPR_LOC_OR_HERE (t),
13633 "declarations in dependent base %qT are "
13634 "not found by unqualified lookup",
13635 DECL_CLASS_CONTEXT (fn));
13636 if (current_class_ptr)
13637 inform (EXPR_LOC_OR_HERE (t),
13638 "use %<this->%D%> instead", function);
13639 else
13640 inform (EXPR_LOC_OR_HERE (t),
13641 "use %<%T::%D%> instead",
13642 current_class_name, function);
13644 else
13645 inform (0, "%q+D declared here, later in the "
13646 "translation unit", fn);
13647 function = unq;
13650 if (TREE_CODE (function) == IDENTIFIER_NODE)
13652 unqualified_name_lookup_error (function);
13653 release_tree_vector (call_args);
13654 return error_mark_node;
13658 /* Remember that there was a reference to this entity. */
13659 if (DECL_P (function))
13660 mark_used (function);
13662 if (TREE_CODE (function) == OFFSET_REF)
13663 ret = build_offset_ref_call_from_tree (function, &call_args);
13664 else if (TREE_CODE (function) == COMPONENT_REF)
13666 tree instance = TREE_OPERAND (function, 0);
13667 tree fn = TREE_OPERAND (function, 1);
13669 if (processing_template_decl
13670 && (type_dependent_expression_p (instance)
13671 || (!BASELINK_P (fn)
13672 && TREE_CODE (fn) != FIELD_DECL)
13673 || type_dependent_expression_p (fn)
13674 || any_type_dependent_arguments_p (call_args)))
13675 ret = build_nt_call_vec (function, call_args);
13676 else if (!BASELINK_P (fn))
13677 ret = finish_call_expr (function, &call_args,
13678 /*disallow_virtual=*/false,
13679 /*koenig_p=*/false,
13680 complain);
13681 else
13682 ret = (build_new_method_call
13683 (instance, fn,
13684 &call_args, NULL_TREE,
13685 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
13686 /*fn_p=*/NULL,
13687 complain));
13689 else
13690 ret = finish_call_expr (function, &call_args,
13691 /*disallow_virtual=*/qualified_p,
13692 koenig_p,
13693 complain);
13695 release_tree_vector (call_args);
13697 return ret;
13700 case COND_EXPR:
13702 tree cond = RECUR (TREE_OPERAND (t, 0));
13703 tree exp1, exp2;
13705 if (TREE_CODE (cond) == INTEGER_CST)
13707 if (integer_zerop (cond))
13709 ++c_inhibit_evaluation_warnings;
13710 exp1 = RECUR (TREE_OPERAND (t, 1));
13711 --c_inhibit_evaluation_warnings;
13712 exp2 = RECUR (TREE_OPERAND (t, 2));
13714 else
13716 exp1 = RECUR (TREE_OPERAND (t, 1));
13717 ++c_inhibit_evaluation_warnings;
13718 exp2 = RECUR (TREE_OPERAND (t, 2));
13719 --c_inhibit_evaluation_warnings;
13722 else
13724 exp1 = RECUR (TREE_OPERAND (t, 1));
13725 exp2 = RECUR (TREE_OPERAND (t, 2));
13728 return build_x_conditional_expr (EXPR_LOCATION (t),
13729 cond, exp1, exp2, complain);
13732 case PSEUDO_DTOR_EXPR:
13733 return finish_pseudo_destructor_expr
13734 (RECUR (TREE_OPERAND (t, 0)),
13735 RECUR (TREE_OPERAND (t, 1)),
13736 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl));
13738 case TREE_LIST:
13740 tree purpose, value, chain;
13742 if (t == void_list_node)
13743 return t;
13745 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13746 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13748 /* We have pack expansions, so expand those and
13749 create a new list out of it. */
13750 tree purposevec = NULL_TREE;
13751 tree valuevec = NULL_TREE;
13752 tree chain;
13753 int i, len = -1;
13755 /* Expand the argument expressions. */
13756 if (TREE_PURPOSE (t))
13757 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13758 complain, in_decl);
13759 if (TREE_VALUE (t))
13760 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13761 complain, in_decl);
13763 /* Build the rest of the list. */
13764 chain = TREE_CHAIN (t);
13765 if (chain && chain != void_type_node)
13766 chain = RECUR (chain);
13768 /* Determine the number of arguments. */
13769 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13771 len = TREE_VEC_LENGTH (purposevec);
13772 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13774 else if (TREE_CODE (valuevec) == TREE_VEC)
13775 len = TREE_VEC_LENGTH (valuevec);
13776 else
13778 /* Since we only performed a partial substitution into
13779 the argument pack, we only return a single list
13780 node. */
13781 if (purposevec == TREE_PURPOSE (t)
13782 && valuevec == TREE_VALUE (t)
13783 && chain == TREE_CHAIN (t))
13784 return t;
13786 return tree_cons (purposevec, valuevec, chain);
13789 /* Convert the argument vectors into a TREE_LIST */
13790 i = len;
13791 while (i > 0)
13793 /* Grab the Ith values. */
13794 i--;
13795 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13796 : NULL_TREE;
13797 value
13798 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
13799 : NULL_TREE;
13801 /* Build the list (backwards). */
13802 chain = tree_cons (purpose, value, chain);
13805 return chain;
13808 purpose = TREE_PURPOSE (t);
13809 if (purpose)
13810 purpose = RECUR (purpose);
13811 value = TREE_VALUE (t);
13812 if (value)
13813 value = RECUR (value);
13814 chain = TREE_CHAIN (t);
13815 if (chain && chain != void_type_node)
13816 chain = RECUR (chain);
13817 if (purpose == TREE_PURPOSE (t)
13818 && value == TREE_VALUE (t)
13819 && chain == TREE_CHAIN (t))
13820 return t;
13821 return tree_cons (purpose, value, chain);
13824 case COMPONENT_REF:
13826 tree object;
13827 tree object_type;
13828 tree member;
13830 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13831 args, complain, in_decl);
13832 /* Remember that there was a reference to this entity. */
13833 if (DECL_P (object))
13834 mark_used (object);
13835 object_type = TREE_TYPE (object);
13837 member = TREE_OPERAND (t, 1);
13838 if (BASELINK_P (member))
13839 member = tsubst_baselink (member,
13840 non_reference (TREE_TYPE (object)),
13841 args, complain, in_decl);
13842 else
13843 member = tsubst_copy (member, args, complain, in_decl);
13844 if (member == error_mark_node)
13845 return error_mark_node;
13847 if (type_dependent_expression_p (object))
13848 /* We can't do much here. */;
13849 else if (!CLASS_TYPE_P (object_type))
13851 if (SCALAR_TYPE_P (object_type))
13853 tree s = NULL_TREE;
13854 tree dtor = member;
13856 if (TREE_CODE (dtor) == SCOPE_REF)
13858 s = TREE_OPERAND (dtor, 0);
13859 dtor = TREE_OPERAND (dtor, 1);
13861 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
13863 dtor = TREE_OPERAND (dtor, 0);
13864 if (TYPE_P (dtor))
13865 return finish_pseudo_destructor_expr (object, s, dtor);
13869 else if (TREE_CODE (member) == SCOPE_REF
13870 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
13872 /* Lookup the template functions now that we know what the
13873 scope is. */
13874 tree scope = TREE_OPERAND (member, 0);
13875 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
13876 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
13877 member = lookup_qualified_name (scope, tmpl,
13878 /*is_type_p=*/false,
13879 /*complain=*/false);
13880 if (BASELINK_P (member))
13882 BASELINK_FUNCTIONS (member)
13883 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
13884 args);
13885 member = (adjust_result_of_qualified_name_lookup
13886 (member, BINFO_TYPE (BASELINK_BINFO (member)),
13887 object_type));
13889 else
13891 qualified_name_lookup_error (scope, tmpl, member,
13892 input_location);
13893 return error_mark_node;
13896 else if (TREE_CODE (member) == SCOPE_REF
13897 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
13898 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
13900 if (complain & tf_error)
13902 if (TYPE_P (TREE_OPERAND (member, 0)))
13903 error ("%qT is not a class or namespace",
13904 TREE_OPERAND (member, 0));
13905 else
13906 error ("%qD is not a class or namespace",
13907 TREE_OPERAND (member, 0));
13909 return error_mark_node;
13911 else if (TREE_CODE (member) == FIELD_DECL)
13912 return finish_non_static_data_member (member, object, NULL_TREE);
13914 return finish_class_member_access_expr (object, member,
13915 /*template_p=*/false,
13916 complain);
13919 case THROW_EXPR:
13920 return build_throw
13921 (RECUR (TREE_OPERAND (t, 0)));
13923 case CONSTRUCTOR:
13925 VEC(constructor_elt,gc) *n;
13926 constructor_elt *ce;
13927 unsigned HOST_WIDE_INT idx;
13928 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13929 bool process_index_p;
13930 int newlen;
13931 bool need_copy_p = false;
13932 tree r;
13934 if (type == error_mark_node)
13935 return error_mark_node;
13937 /* digest_init will do the wrong thing if we let it. */
13938 if (type && TYPE_PTRMEMFUNC_P (type))
13939 return t;
13941 /* We do not want to process the index of aggregate
13942 initializers as they are identifier nodes which will be
13943 looked up by digest_init. */
13944 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
13946 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
13947 newlen = VEC_length (constructor_elt, n);
13948 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
13950 if (ce->index && process_index_p)
13951 ce->index = RECUR (ce->index);
13953 if (PACK_EXPANSION_P (ce->value))
13955 /* Substitute into the pack expansion. */
13956 ce->value = tsubst_pack_expansion (ce->value, args, complain,
13957 in_decl);
13959 if (ce->value == error_mark_node
13960 || PACK_EXPANSION_P (ce->value))
13962 else if (TREE_VEC_LENGTH (ce->value) == 1)
13963 /* Just move the argument into place. */
13964 ce->value = TREE_VEC_ELT (ce->value, 0);
13965 else
13967 /* Update the length of the final CONSTRUCTOR
13968 arguments vector, and note that we will need to
13969 copy.*/
13970 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
13971 need_copy_p = true;
13974 else
13975 ce->value = RECUR (ce->value);
13978 if (need_copy_p)
13980 VEC(constructor_elt,gc) *old_n = n;
13982 n = VEC_alloc (constructor_elt, gc, newlen);
13983 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
13985 if (TREE_CODE (ce->value) == TREE_VEC)
13987 int i, len = TREE_VEC_LENGTH (ce->value);
13988 for (i = 0; i < len; ++i)
13989 CONSTRUCTOR_APPEND_ELT (n, 0,
13990 TREE_VEC_ELT (ce->value, i));
13992 else
13993 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
13997 r = build_constructor (init_list_type_node, n);
13998 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14000 if (TREE_HAS_CONSTRUCTOR (t))
14001 return finish_compound_literal (type, r, complain);
14003 TREE_TYPE (r) = type;
14004 return r;
14007 case TYPEID_EXPR:
14009 tree operand_0 = TREE_OPERAND (t, 0);
14010 if (TYPE_P (operand_0))
14012 operand_0 = tsubst (operand_0, args, complain, in_decl);
14013 return get_typeid (operand_0);
14015 else
14017 operand_0 = RECUR (operand_0);
14018 return build_typeid (operand_0);
14022 case VAR_DECL:
14023 if (!args)
14024 return t;
14025 /* Fall through */
14027 case PARM_DECL:
14029 tree r = tsubst_copy (t, args, complain, in_decl);
14031 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14032 /* If the original type was a reference, we'll be wrapped in
14033 the appropriate INDIRECT_REF. */
14034 r = convert_from_reference (r);
14035 return r;
14038 case VA_ARG_EXPR:
14039 return build_x_va_arg (EXPR_LOCATION (t),
14040 RECUR (TREE_OPERAND (t, 0)),
14041 tsubst (TREE_TYPE (t), args, complain, in_decl));
14043 case OFFSETOF_EXPR:
14044 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
14046 case TRAIT_EXPR:
14048 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14049 complain, in_decl);
14051 tree type2 = TRAIT_EXPR_TYPE2 (t);
14052 if (type2)
14053 type2 = tsubst_copy (type2, args, complain, in_decl);
14055 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
14058 case STMT_EXPR:
14060 tree old_stmt_expr = cur_stmt_expr;
14061 tree stmt_expr = begin_stmt_expr ();
14063 cur_stmt_expr = stmt_expr;
14064 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14065 integral_constant_expression_p);
14066 stmt_expr = finish_stmt_expr (stmt_expr, false);
14067 cur_stmt_expr = old_stmt_expr;
14069 /* If the resulting list of expression statement is empty,
14070 fold it further into void_zero_node. */
14071 if (empty_expr_stmt_p (stmt_expr))
14072 stmt_expr = void_zero_node;
14074 return stmt_expr;
14077 case LAMBDA_EXPR:
14079 tree r = build_lambda_expr ();
14081 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14082 LAMBDA_EXPR_CLOSURE (r) = type;
14083 CLASSTYPE_LAMBDA_EXPR (type) = r;
14085 LAMBDA_EXPR_LOCATION (r)
14086 = LAMBDA_EXPR_LOCATION (t);
14087 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14088 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14089 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14090 LAMBDA_EXPR_DISCRIMINATOR (r)
14091 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14092 LAMBDA_EXPR_EXTRA_SCOPE (r)
14093 = tsubst (LAMBDA_EXPR_EXTRA_SCOPE (t), args, complain, in_decl);
14094 LAMBDA_EXPR_RETURN_TYPE (r)
14095 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
14097 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
14098 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
14100 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14101 determine_visibility (TYPE_NAME (type));
14102 /* Now that we know visibility, instantiate the type so we have a
14103 declaration of the op() for later calls to lambda_function. */
14104 complete_type (type);
14106 /* The capture list refers to closure members, so this needs to
14107 wait until after we finish instantiating the type. */
14108 LAMBDA_EXPR_CAPTURE_LIST (r)
14109 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
14111 return build_lambda_object (r);
14114 case TARGET_EXPR:
14115 /* We can get here for a constant initializer of non-dependent type.
14116 FIXME stop folding in cp_parser_initializer_clause. */
14117 gcc_assert (TREE_CONSTANT (t));
14119 tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t)));
14120 TREE_CONSTANT (r) = true;
14121 return r;
14124 case TRANSACTION_EXPR:
14125 return tsubst_expr(t, args, complain, in_decl,
14126 integral_constant_expression_p);
14128 default:
14129 /* Handle Objective-C++ constructs, if appropriate. */
14131 tree subst
14132 = objcp_tsubst_copy_and_build (t, args, complain,
14133 in_decl, /*function_p=*/false);
14134 if (subst)
14135 return subst;
14137 return tsubst_copy (t, args, complain, in_decl);
14140 #undef RECUR
14143 /* Verify that the instantiated ARGS are valid. For type arguments,
14144 make sure that the type's linkage is ok. For non-type arguments,
14145 make sure they are constants if they are integral or enumerations.
14146 Emit an error under control of COMPLAIN, and return TRUE on error. */
14148 static bool
14149 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
14151 if (ARGUMENT_PACK_P (t))
14153 tree vec = ARGUMENT_PACK_ARGS (t);
14154 int len = TREE_VEC_LENGTH (vec);
14155 bool result = false;
14156 int i;
14158 for (i = 0; i < len; ++i)
14159 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
14160 result = true;
14161 return result;
14163 else if (TYPE_P (t))
14165 /* [basic.link]: A name with no linkage (notably, the name
14166 of a class or enumeration declared in a local scope)
14167 shall not be used to declare an entity with linkage.
14168 This implies that names with no linkage cannot be used as
14169 template arguments
14171 DR 757 relaxes this restriction for C++0x. */
14172 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
14173 : no_linkage_check (t, /*relaxed_p=*/false));
14175 if (nt)
14177 /* DR 488 makes use of a type with no linkage cause
14178 type deduction to fail. */
14179 if (complain & tf_error)
14181 if (TYPE_ANONYMOUS_P (nt))
14182 error ("%qT is/uses anonymous type", t);
14183 else
14184 error ("template argument for %qD uses local type %qT",
14185 tmpl, t);
14187 return true;
14189 /* In order to avoid all sorts of complications, we do not
14190 allow variably-modified types as template arguments. */
14191 else if (variably_modified_type_p (t, NULL_TREE))
14193 if (complain & tf_error)
14194 error ("%qT is a variably modified type", t);
14195 return true;
14198 /* A non-type argument of integral or enumerated type must be a
14199 constant. */
14200 else if (TREE_TYPE (t)
14201 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
14202 && !TREE_CONSTANT (t))
14204 if (complain & tf_error)
14205 error ("integral expression %qE is not constant", t);
14206 return true;
14208 return false;
14211 static bool
14212 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
14214 int ix, len = DECL_NTPARMS (tmpl);
14215 bool result = false;
14217 for (ix = 0; ix != len; ix++)
14219 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
14220 result = true;
14222 if (result && (complain & tf_error))
14223 error (" trying to instantiate %qD", tmpl);
14224 return result;
14227 /* In C++0x, it's possible to have a function template whose type depends
14228 on itself recursively. This is most obvious with decltype, but can also
14229 occur with enumeration scope (c++/48969). So we need to catch infinite
14230 recursion and reject the substitution at deduction time; this function
14231 will return error_mark_node for any repeated substitution.
14233 This also catches excessive recursion such as when f<N> depends on
14234 f<N-1> across all integers, and returns error_mark_node for all the
14235 substitutions back up to the initial one.
14237 This is, of course, not reentrant. */
14239 static tree
14240 deduction_tsubst_fntype (tree fn, tree targs, tsubst_flags_t complain)
14242 static bool excessive_deduction_depth;
14243 static int deduction_depth;
14244 struct pending_template *old_last_pend = last_pending_template;
14245 struct tinst_level *old_error_tinst = last_error_tinst_level;
14247 tree fntype = TREE_TYPE (fn);
14248 tree tinst;
14249 tree r;
14251 if (excessive_deduction_depth)
14252 return error_mark_node;
14254 tinst = build_tree_list (fn, targs);
14255 if (!push_tinst_level (tinst))
14257 excessive_deduction_depth = true;
14258 ggc_free (tinst);
14259 return error_mark_node;
14262 input_location = DECL_SOURCE_LOCATION (fn);
14263 ++deduction_depth;
14264 /* We will do access checks in instantiate_template. */
14265 push_deferring_access_checks (dk_deferred);
14266 r = tsubst (fntype, targs, complain, NULL_TREE);
14267 pop_deferring_access_checks ();
14268 --deduction_depth;
14270 if (excessive_deduction_depth)
14272 r = error_mark_node;
14273 if (deduction_depth == 0)
14274 /* Reset once we're all the way out. */
14275 excessive_deduction_depth = false;
14278 pop_tinst_level ();
14279 /* We can't free this if a pending_template entry or last_error_tinst_level
14280 is pointing at it. */
14281 if (last_pending_template == old_last_pend
14282 && last_error_tinst_level == old_error_tinst)
14283 ggc_free (tinst);
14284 return r;
14287 /* Instantiate the indicated variable or function template TMPL with
14288 the template arguments in TARG_PTR. */
14290 static tree
14291 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14293 tree targ_ptr = orig_args;
14294 tree fndecl;
14295 tree gen_tmpl;
14296 tree spec;
14298 if (tmpl == error_mark_node)
14299 return error_mark_node;
14301 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14303 /* If this function is a clone, handle it specially. */
14304 if (DECL_CLONED_FUNCTION_P (tmpl))
14306 tree spec;
14307 tree clone;
14309 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14310 DECL_CLONED_FUNCTION. */
14311 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14312 targ_ptr, complain);
14313 if (spec == error_mark_node)
14314 return error_mark_node;
14316 /* Look for the clone. */
14317 FOR_EACH_CLONE (clone, spec)
14318 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14319 return clone;
14320 /* We should always have found the clone by now. */
14321 gcc_unreachable ();
14322 return NULL_TREE;
14325 /* Check to see if we already have this specialization. */
14326 gen_tmpl = most_general_template (tmpl);
14327 if (tmpl != gen_tmpl)
14328 /* The TMPL is a partial instantiation. To get a full set of
14329 arguments we must add the arguments used to perform the
14330 partial instantiation. */
14331 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14332 targ_ptr);
14334 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14335 but it doesn't seem to be on the hot path. */
14336 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14338 gcc_assert (tmpl == gen_tmpl
14339 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14340 == spec)
14341 || fndecl == NULL_TREE);
14343 if (spec != NULL_TREE)
14344 return spec;
14346 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14347 complain))
14348 return error_mark_node;
14350 /* We are building a FUNCTION_DECL, during which the access of its
14351 parameters and return types have to be checked. However this
14352 FUNCTION_DECL which is the desired context for access checking
14353 is not built yet. We solve this chicken-and-egg problem by
14354 deferring all checks until we have the FUNCTION_DECL. */
14355 push_deferring_access_checks (dk_deferred);
14357 /* Instantiation of the function happens in the context of the function
14358 template, not the context of the overload resolution we're doing. */
14359 push_to_top_level ();
14360 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14362 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
14363 complain, gen_tmpl);
14364 push_nested_class (ctx);
14366 /* Substitute template parameters to obtain the specialization. */
14367 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14368 targ_ptr, complain, gen_tmpl);
14369 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14370 pop_nested_class ();
14371 pop_from_top_level ();
14373 if (fndecl == error_mark_node)
14374 return error_mark_node;
14376 /* The DECL_TI_TEMPLATE should always be the immediate parent
14377 template, not the most general template. */
14378 DECL_TI_TEMPLATE (fndecl) = tmpl;
14380 /* Now we know the specialization, compute access previously
14381 deferred. */
14382 push_access_scope (fndecl);
14383 perform_deferred_access_checks ();
14384 pop_access_scope (fndecl);
14385 pop_deferring_access_checks ();
14387 /* If we've just instantiated the main entry point for a function,
14388 instantiate all the alternate entry points as well. We do this
14389 by cloning the instantiation of the main entry point, not by
14390 instantiating the template clones. */
14391 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14392 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14394 return fndecl;
14397 /* Wrapper for instantiate_template_1. */
14399 tree
14400 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14402 tree ret;
14403 timevar_push (TV_TEMPLATE_INST);
14404 ret = instantiate_template_1 (tmpl, orig_args, complain);
14405 timevar_pop (TV_TEMPLATE_INST);
14406 return ret;
14409 /* PARM is a template parameter pack for FN. Returns true iff
14410 PARM is used in a deducible way in the argument list of FN. */
14412 static bool
14413 pack_deducible_p (tree parm, tree fn)
14415 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
14416 for (; t; t = TREE_CHAIN (t))
14418 tree type = TREE_VALUE (t);
14419 tree packs;
14420 if (!PACK_EXPANSION_P (type))
14421 continue;
14422 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
14423 packs; packs = TREE_CHAIN (packs))
14424 if (TREE_VALUE (packs) == parm)
14426 /* The template parameter pack is used in a function parameter
14427 pack. If this is the end of the parameter list, the
14428 template parameter pack is deducible. */
14429 if (TREE_CHAIN (t) == void_list_node)
14430 return true;
14431 else
14432 /* Otherwise, not. Well, it could be deduced from
14433 a non-pack parameter, but doing so would end up with
14434 a deduction mismatch, so don't bother. */
14435 return false;
14438 /* The template parameter pack isn't used in any function parameter
14439 packs, but it might be used deeper, e.g. tuple<Args...>. */
14440 return true;
14443 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14444 NARGS elements of the arguments that are being used when calling
14445 it. TARGS is a vector into which the deduced template arguments
14446 are placed.
14448 Return zero for success, 2 for an incomplete match that doesn't resolve
14449 all the types, and 1 for complete failure. An error message will be
14450 printed only for an incomplete match.
14452 If FN is a conversion operator, or we are trying to produce a specific
14453 specialization, RETURN_TYPE is the return type desired.
14455 The EXPLICIT_TARGS are explicit template arguments provided via a
14456 template-id.
14458 The parameter STRICT is one of:
14460 DEDUCE_CALL:
14461 We are deducing arguments for a function call, as in
14462 [temp.deduct.call].
14464 DEDUCE_CONV:
14465 We are deducing arguments for a conversion function, as in
14466 [temp.deduct.conv].
14468 DEDUCE_EXACT:
14469 We are deducing arguments when doing an explicit instantiation
14470 as in [temp.explicit], when determining an explicit specialization
14471 as in [temp.expl.spec], or when taking the address of a function
14472 template, as in [temp.deduct.funcaddr]. */
14475 fn_type_unification (tree fn,
14476 tree explicit_targs,
14477 tree targs,
14478 const tree *args,
14479 unsigned int nargs,
14480 tree return_type,
14481 unification_kind_t strict,
14482 int flags,
14483 bool explain_p)
14485 tree parms;
14486 tree fntype;
14487 int result;
14489 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14491 fntype = TREE_TYPE (fn);
14492 if (explicit_targs)
14494 /* [temp.deduct]
14496 The specified template arguments must match the template
14497 parameters in kind (i.e., type, nontype, template), and there
14498 must not be more arguments than there are parameters;
14499 otherwise type deduction fails.
14501 Nontype arguments must match the types of the corresponding
14502 nontype template parameters, or must be convertible to the
14503 types of the corresponding nontype parameters as specified in
14504 _temp.arg.nontype_, otherwise type deduction fails.
14506 All references in the function type of the function template
14507 to the corresponding template parameters are replaced by the
14508 specified template argument values. If a substitution in a
14509 template parameter or in the function type of the function
14510 template results in an invalid type, type deduction fails. */
14511 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14512 int i, len = TREE_VEC_LENGTH (tparms);
14513 tree converted_args;
14514 bool incomplete = false;
14516 if (explicit_targs == error_mark_node)
14517 return unify_invalid (explain_p);
14519 converted_args
14520 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
14521 (explain_p
14522 ? tf_warning_or_error
14523 : tf_none),
14524 /*require_all_args=*/false,
14525 /*use_default_args=*/false));
14526 if (converted_args == error_mark_node)
14527 return 1;
14529 /* Substitute the explicit args into the function type. This is
14530 necessary so that, for instance, explicitly declared function
14531 arguments can match null pointed constants. If we were given
14532 an incomplete set of explicit args, we must not do semantic
14533 processing during substitution as we could create partial
14534 instantiations. */
14535 for (i = 0; i < len; i++)
14537 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14538 bool parameter_pack = false;
14539 tree targ = TREE_VEC_ELT (converted_args, i);
14541 /* Dig out the actual parm. */
14542 if (TREE_CODE (parm) == TYPE_DECL
14543 || TREE_CODE (parm) == TEMPLATE_DECL)
14545 parm = TREE_TYPE (parm);
14546 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
14548 else if (TREE_CODE (parm) == PARM_DECL)
14550 parm = DECL_INITIAL (parm);
14551 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
14554 if (!parameter_pack && targ == NULL_TREE)
14555 /* No explicit argument for this template parameter. */
14556 incomplete = true;
14558 if (parameter_pack && pack_deducible_p (parm, fn))
14560 /* Mark the argument pack as "incomplete". We could
14561 still deduce more arguments during unification.
14562 We remove this mark in type_unification_real. */
14563 if (targ)
14565 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
14566 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
14567 = ARGUMENT_PACK_ARGS (targ);
14570 /* We have some incomplete argument packs. */
14571 incomplete = true;
14575 processing_template_decl += incomplete;
14576 fntype = deduction_tsubst_fntype (fn, converted_args,
14577 (explain_p
14578 ? tf_warning_or_error
14579 : tf_none) | tf_partial);
14580 processing_template_decl -= incomplete;
14582 if (fntype == error_mark_node)
14583 return 1;
14585 /* Place the explicitly specified arguments in TARGS. */
14586 for (i = NUM_TMPL_ARGS (converted_args); i--;)
14587 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
14590 /* Never do unification on the 'this' parameter. */
14591 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
14593 if (return_type)
14595 tree *new_args;
14597 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
14598 new_args = XALLOCAVEC (tree, nargs + 1);
14599 new_args[0] = return_type;
14600 memcpy (new_args + 1, args, nargs * sizeof (tree));
14601 args = new_args;
14602 ++nargs;
14605 /* We allow incomplete unification without an error message here
14606 because the standard doesn't seem to explicitly prohibit it. Our
14607 callers must be ready to deal with unification failures in any
14608 event. */
14609 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
14610 targs, parms, args, nargs, /*subr=*/0,
14611 strict, flags, explain_p);
14613 /* Now that we have bindings for all of the template arguments,
14614 ensure that the arguments deduced for the template template
14615 parameters have compatible template parameter lists. We cannot
14616 check this property before we have deduced all template
14617 arguments, because the template parameter types of a template
14618 template parameter might depend on prior template parameters
14619 deduced after the template template parameter. The following
14620 ill-formed example illustrates this issue:
14622 template<typename T, template<T> class C> void f(C<5>, T);
14624 template<int N> struct X {};
14626 void g() {
14627 f(X<5>(), 5l); // error: template argument deduction fails
14630 The template parameter list of 'C' depends on the template type
14631 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14632 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14633 time that we deduce 'C'. */
14634 if (result == 0
14635 && !template_template_parm_bindings_ok_p
14636 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
14637 return unify_inconsistent_template_template_parameters (explain_p);
14639 if (result == 0)
14640 /* All is well so far. Now, check:
14642 [temp.deduct]
14644 When all template arguments have been deduced, all uses of
14645 template parameters in nondeduced contexts are replaced with
14646 the corresponding deduced argument values. If the
14647 substitution results in an invalid type, as described above,
14648 type deduction fails. */
14650 tree substed = deduction_tsubst_fntype (fn, targs,
14651 (explain_p
14652 ? tf_warning_or_error
14653 : tf_none));
14654 if (substed == error_mark_node)
14655 return 1;
14657 /* If we're looking for an exact match, check that what we got
14658 is indeed an exact match. It might not be if some template
14659 parameters are used in non-deduced contexts. */
14660 if (strict == DEDUCE_EXACT)
14662 unsigned int i;
14664 tree sarg
14665 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
14666 if (return_type)
14667 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
14668 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
14669 if (!same_type_p (args[i], TREE_VALUE (sarg)))
14670 return unify_type_mismatch (explain_p, args[i],
14671 TREE_VALUE (sarg));
14675 return result;
14678 /* Adjust types before performing type deduction, as described in
14679 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14680 sections are symmetric. PARM is the type of a function parameter
14681 or the return type of the conversion function. ARG is the type of
14682 the argument passed to the call, or the type of the value
14683 initialized with the result of the conversion function.
14684 ARG_EXPR is the original argument expression, which may be null. */
14686 static int
14687 maybe_adjust_types_for_deduction (unification_kind_t strict,
14688 tree* parm,
14689 tree* arg,
14690 tree arg_expr)
14692 int result = 0;
14694 switch (strict)
14696 case DEDUCE_CALL:
14697 break;
14699 case DEDUCE_CONV:
14701 /* Swap PARM and ARG throughout the remainder of this
14702 function; the handling is precisely symmetric since PARM
14703 will initialize ARG rather than vice versa. */
14704 tree* temp = parm;
14705 parm = arg;
14706 arg = temp;
14707 break;
14710 case DEDUCE_EXACT:
14711 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14712 too, but here handle it by stripping the reference from PARM
14713 rather than by adding it to ARG. */
14714 if (TREE_CODE (*parm) == REFERENCE_TYPE
14715 && TYPE_REF_IS_RVALUE (*parm)
14716 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14717 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14718 && TREE_CODE (*arg) == REFERENCE_TYPE
14719 && !TYPE_REF_IS_RVALUE (*arg))
14720 *parm = TREE_TYPE (*parm);
14721 /* Nothing else to do in this case. */
14722 return 0;
14724 default:
14725 gcc_unreachable ();
14728 if (TREE_CODE (*parm) != REFERENCE_TYPE)
14730 /* [temp.deduct.call]
14732 If P is not a reference type:
14734 --If A is an array type, the pointer type produced by the
14735 array-to-pointer standard conversion (_conv.array_) is
14736 used in place of A for type deduction; otherwise,
14738 --If A is a function type, the pointer type produced by
14739 the function-to-pointer standard conversion
14740 (_conv.func_) is used in place of A for type deduction;
14741 otherwise,
14743 --If A is a cv-qualified type, the top level
14744 cv-qualifiers of A's type are ignored for type
14745 deduction. */
14746 if (TREE_CODE (*arg) == ARRAY_TYPE)
14747 *arg = build_pointer_type (TREE_TYPE (*arg));
14748 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
14749 *arg = build_pointer_type (*arg);
14750 else
14751 *arg = TYPE_MAIN_VARIANT (*arg);
14754 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14755 of the form T&&, where T is a template parameter, and the argument
14756 is an lvalue, T is deduced as A& */
14757 if (TREE_CODE (*parm) == REFERENCE_TYPE
14758 && TYPE_REF_IS_RVALUE (*parm)
14759 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14760 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14761 && (arg_expr ? real_lvalue_p (arg_expr)
14762 /* try_one_overload doesn't provide an arg_expr, but
14763 functions are always lvalues. */
14764 : TREE_CODE (*arg) == FUNCTION_TYPE))
14765 *arg = build_reference_type (*arg);
14767 /* [temp.deduct.call]
14769 If P is a cv-qualified type, the top level cv-qualifiers
14770 of P's type are ignored for type deduction. If P is a
14771 reference type, the type referred to by P is used for
14772 type deduction. */
14773 *parm = TYPE_MAIN_VARIANT (*parm);
14774 if (TREE_CODE (*parm) == REFERENCE_TYPE)
14776 *parm = TREE_TYPE (*parm);
14777 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14780 /* DR 322. For conversion deduction, remove a reference type on parm
14781 too (which has been swapped into ARG). */
14782 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
14783 *arg = TREE_TYPE (*arg);
14785 return result;
14788 /* Subroutine of unify_one_argument. PARM is a function parameter of a
14789 template which does contain any deducible template parameters; check if
14790 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
14791 unify_one_argument. */
14793 static int
14794 check_non_deducible_conversion (tree parm, tree arg, int strict,
14795 int flags, bool explain_p)
14797 tree type;
14799 if (!TYPE_P (arg))
14800 type = TREE_TYPE (arg);
14801 else
14802 type = arg;
14804 if (same_type_p (parm, type))
14805 return unify_success (explain_p);
14807 if (strict == DEDUCE_CONV)
14809 if (can_convert_arg (type, parm, NULL_TREE, flags,
14810 explain_p ? tf_warning_or_error : tf_none))
14811 return unify_success (explain_p);
14813 else if (strict != DEDUCE_EXACT)
14815 if (can_convert_arg (parm, type,
14816 TYPE_P (arg) ? NULL_TREE : arg,
14817 flags, explain_p ? tf_warning_or_error : tf_none))
14818 return unify_success (explain_p);
14821 if (strict == DEDUCE_EXACT)
14822 return unify_type_mismatch (explain_p, parm, arg);
14823 else
14824 return unify_arg_conversion (explain_p, parm, type, arg);
14827 /* Subroutine of type_unification_real and unify_pack_expansion to
14828 handle unification of a single P/A pair. Parameters are as
14829 for those functions. */
14831 static int
14832 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
14833 int subr, unification_kind_t strict, int flags,
14834 bool explain_p)
14836 tree arg_expr = NULL_TREE;
14837 int arg_strict;
14839 if (arg == error_mark_node || parm == error_mark_node)
14840 return unify_invalid (explain_p);
14841 if (arg == unknown_type_node)
14842 /* We can't deduce anything from this, but we might get all the
14843 template args from other function args. */
14844 return unify_success (explain_p);
14846 /* FIXME uses_deducible_template_parms */
14847 if (TYPE_P (parm) && !uses_template_parms (parm))
14848 return check_non_deducible_conversion (parm, arg, strict, flags,
14849 explain_p);
14851 switch (strict)
14853 case DEDUCE_CALL:
14854 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
14855 | UNIFY_ALLOW_MORE_CV_QUAL
14856 | UNIFY_ALLOW_DERIVED);
14857 break;
14859 case DEDUCE_CONV:
14860 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14861 break;
14863 case DEDUCE_EXACT:
14864 arg_strict = UNIFY_ALLOW_NONE;
14865 break;
14867 default:
14868 gcc_unreachable ();
14871 /* We only do these transformations if this is the top-level
14872 parameter_type_list in a call or declaration matching; in other
14873 situations (nested function declarators, template argument lists) we
14874 won't be comparing a type to an expression, and we don't do any type
14875 adjustments. */
14876 if (!subr)
14878 if (!TYPE_P (arg))
14880 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14881 if (type_unknown_p (arg))
14883 /* [temp.deduct.type] A template-argument can be
14884 deduced from a pointer to function or pointer
14885 to member function argument if the set of
14886 overloaded functions does not contain function
14887 templates and at most one of a set of
14888 overloaded functions provides a unique
14889 match. */
14891 if (resolve_overloaded_unification
14892 (tparms, targs, parm, arg, strict,
14893 arg_strict, explain_p))
14894 return unify_success (explain_p);
14895 return unify_overload_resolution_failure (explain_p, arg);
14898 arg_expr = arg;
14899 arg = unlowered_expr_type (arg);
14900 if (arg == error_mark_node)
14901 return unify_invalid (explain_p);
14904 arg_strict |=
14905 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
14907 else
14908 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
14909 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
14911 /* For deduction from an init-list we need the actual list. */
14912 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14913 arg = arg_expr;
14914 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
14917 /* Most parms like fn_type_unification.
14919 If SUBR is 1, we're being called recursively (to unify the
14920 arguments of a function or method parameter of a function
14921 template). */
14923 static int
14924 type_unification_real (tree tparms,
14925 tree targs,
14926 tree xparms,
14927 const tree *xargs,
14928 unsigned int xnargs,
14929 int subr,
14930 unification_kind_t strict,
14931 int flags,
14932 bool explain_p)
14934 tree parm, arg;
14935 int i;
14936 int ntparms = TREE_VEC_LENGTH (tparms);
14937 int saw_undeduced = 0;
14938 tree parms;
14939 const tree *args;
14940 unsigned int nargs;
14941 unsigned int ia;
14943 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
14944 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
14945 gcc_assert (ntparms > 0);
14947 /* Reset the number of non-defaulted template arguments contained
14948 in TARGS. */
14949 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
14951 again:
14952 parms = xparms;
14953 args = xargs;
14954 nargs = xnargs;
14956 ia = 0;
14957 while (parms && parms != void_list_node
14958 && ia < nargs)
14960 parm = TREE_VALUE (parms);
14962 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
14963 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
14964 /* For a function parameter pack that occurs at the end of the
14965 parameter-declaration-list, the type A of each remaining
14966 argument of the call is compared with the type P of the
14967 declarator-id of the function parameter pack. */
14968 break;
14970 parms = TREE_CHAIN (parms);
14972 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
14973 /* For a function parameter pack that does not occur at the
14974 end of the parameter-declaration-list, the type of the
14975 parameter pack is a non-deduced context. */
14976 continue;
14978 arg = args[ia];
14979 ++ia;
14981 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
14982 flags, explain_p))
14983 return 1;
14986 if (parms
14987 && parms != void_list_node
14988 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
14990 /* Unify the remaining arguments with the pack expansion type. */
14991 tree argvec;
14992 tree parmvec = make_tree_vec (1);
14994 /* Allocate a TREE_VEC and copy in all of the arguments */
14995 argvec = make_tree_vec (nargs - ia);
14996 for (i = 0; ia < nargs; ++ia, ++i)
14997 TREE_VEC_ELT (argvec, i) = args[ia];
14999 /* Copy the parameter into parmvec. */
15000 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
15001 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
15002 /*subr=*/subr, explain_p))
15003 return 1;
15005 /* Advance to the end of the list of parameters. */
15006 parms = TREE_CHAIN (parms);
15009 /* Fail if we've reached the end of the parm list, and more args
15010 are present, and the parm list isn't variadic. */
15011 if (ia < nargs && parms == void_list_node)
15012 return unify_too_many_arguments (explain_p, nargs, ia);
15013 /* Fail if parms are left and they don't have default values. */
15014 if (parms && parms != void_list_node
15015 && TREE_PURPOSE (parms) == NULL_TREE)
15017 unsigned int count = nargs;
15018 tree p = parms;
15019 while (p && p != void_list_node)
15021 count++;
15022 p = TREE_CHAIN (p);
15024 return unify_too_few_arguments (explain_p, ia, count);
15027 if (!subr)
15029 tsubst_flags_t complain = (explain_p
15030 ? tf_warning_or_error
15031 : tf_none);
15033 /* Check to see if we need another pass before we start clearing
15034 ARGUMENT_PACK_INCOMPLETE_P. */
15035 for (i = 0; i < ntparms; i++)
15037 tree targ = TREE_VEC_ELT (targs, i);
15038 tree tparm = TREE_VEC_ELT (tparms, i);
15040 if (targ || tparm == error_mark_node)
15041 continue;
15042 tparm = TREE_VALUE (tparm);
15044 /* If this is an undeduced nontype parameter that depends on
15045 a type parameter, try another pass; its type may have been
15046 deduced from a later argument than the one from which
15047 this parameter can be deduced. */
15048 if (TREE_CODE (tparm) == PARM_DECL
15049 && uses_template_parms (TREE_TYPE (tparm))
15050 && !saw_undeduced++)
15051 goto again;
15054 for (i = 0; i < ntparms; i++)
15056 tree targ = TREE_VEC_ELT (targs, i);
15057 tree tparm = TREE_VEC_ELT (tparms, i);
15059 /* Clear the "incomplete" flags on all argument packs now so that
15060 substituting them into later default arguments works. */
15061 if (targ && ARGUMENT_PACK_P (targ))
15063 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
15064 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
15067 if (targ || tparm == error_mark_node)
15068 continue;
15069 tparm = TREE_VALUE (tparm);
15071 /* Core issue #226 (C++0x) [temp.deduct]:
15073 If a template argument has not been deduced, its
15074 default template argument, if any, is used.
15076 When we are in C++98 mode, TREE_PURPOSE will either
15077 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15078 to explicitly check cxx_dialect here. */
15079 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
15081 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15082 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
15083 location_t save_loc = input_location;
15084 if (DECL_P (parm))
15085 input_location = DECL_SOURCE_LOCATION (parm);
15086 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
15087 arg = convert_template_argument (parm, arg, targs, complain,
15088 i, NULL_TREE);
15089 input_location = save_loc;
15090 if (arg == error_mark_node)
15091 return 1;
15092 else
15094 TREE_VEC_ELT (targs, i) = arg;
15095 /* The position of the first default template argument,
15096 is also the number of non-defaulted arguments in TARGS.
15097 Record that. */
15098 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15099 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
15100 continue;
15104 /* If the type parameter is a parameter pack, then it will
15105 be deduced to an empty parameter pack. */
15106 if (template_parameter_pack_p (tparm))
15108 tree arg;
15110 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
15112 arg = make_node (NONTYPE_ARGUMENT_PACK);
15113 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
15114 TREE_CONSTANT (arg) = 1;
15116 else
15117 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
15119 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
15121 TREE_VEC_ELT (targs, i) = arg;
15122 continue;
15125 return unify_parameter_deduction_failure (explain_p, tparm);
15128 #ifdef ENABLE_CHECKING
15129 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15130 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
15131 #endif
15133 return unify_success (explain_p);
15136 /* Subroutine of type_unification_real. Args are like the variables
15137 at the call site. ARG is an overloaded function (or template-id);
15138 we try deducing template args from each of the overloads, and if
15139 only one succeeds, we go with that. Modifies TARGS and returns
15140 true on success. */
15142 static bool
15143 resolve_overloaded_unification (tree tparms,
15144 tree targs,
15145 tree parm,
15146 tree arg,
15147 unification_kind_t strict,
15148 int sub_strict,
15149 bool explain_p)
15151 tree tempargs = copy_node (targs);
15152 int good = 0;
15153 tree goodfn = NULL_TREE;
15154 bool addr_p;
15156 if (TREE_CODE (arg) == ADDR_EXPR)
15158 arg = TREE_OPERAND (arg, 0);
15159 addr_p = true;
15161 else
15162 addr_p = false;
15164 if (TREE_CODE (arg) == COMPONENT_REF)
15165 /* Handle `&x' where `x' is some static or non-static member
15166 function name. */
15167 arg = TREE_OPERAND (arg, 1);
15169 if (TREE_CODE (arg) == OFFSET_REF)
15170 arg = TREE_OPERAND (arg, 1);
15172 /* Strip baselink information. */
15173 if (BASELINK_P (arg))
15174 arg = BASELINK_FUNCTIONS (arg);
15176 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
15178 /* If we got some explicit template args, we need to plug them into
15179 the affected templates before we try to unify, in case the
15180 explicit args will completely resolve the templates in question. */
15182 int ok = 0;
15183 tree expl_subargs = TREE_OPERAND (arg, 1);
15184 arg = TREE_OPERAND (arg, 0);
15186 for (; arg; arg = OVL_NEXT (arg))
15188 tree fn = OVL_CURRENT (arg);
15189 tree subargs, elem;
15191 if (TREE_CODE (fn) != TEMPLATE_DECL)
15192 continue;
15194 ++processing_template_decl;
15195 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
15196 expl_subargs, /*check_ret=*/false);
15197 if (subargs && !any_dependent_template_arguments_p (subargs))
15199 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
15200 if (try_one_overload (tparms, targs, tempargs, parm,
15201 elem, strict, sub_strict, addr_p, explain_p)
15202 && (!goodfn || !same_type_p (goodfn, elem)))
15204 goodfn = elem;
15205 ++good;
15208 else if (subargs)
15209 ++ok;
15210 --processing_template_decl;
15212 /* If no templates (or more than one) are fully resolved by the
15213 explicit arguments, this template-id is a non-deduced context; it
15214 could still be OK if we deduce all template arguments for the
15215 enclosing call through other arguments. */
15216 if (good != 1)
15217 good = ok;
15219 else if (TREE_CODE (arg) != OVERLOAD
15220 && TREE_CODE (arg) != FUNCTION_DECL)
15221 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15222 -- but the deduction does not succeed because the expression is
15223 not just the function on its own. */
15224 return false;
15225 else
15226 for (; arg; arg = OVL_NEXT (arg))
15227 if (try_one_overload (tparms, targs, tempargs, parm,
15228 TREE_TYPE (OVL_CURRENT (arg)),
15229 strict, sub_strict, addr_p, explain_p)
15230 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
15232 goodfn = OVL_CURRENT (arg);
15233 ++good;
15236 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15237 to function or pointer to member function argument if the set of
15238 overloaded functions does not contain function templates and at most
15239 one of a set of overloaded functions provides a unique match.
15241 So if we found multiple possibilities, we return success but don't
15242 deduce anything. */
15244 if (good == 1)
15246 int i = TREE_VEC_LENGTH (targs);
15247 for (; i--; )
15248 if (TREE_VEC_ELT (tempargs, i))
15249 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
15251 if (good)
15252 return true;
15254 return false;
15257 /* Core DR 115: In contexts where deduction is done and fails, or in
15258 contexts where deduction is not done, if a template argument list is
15259 specified and it, along with any default template arguments, identifies
15260 a single function template specialization, then the template-id is an
15261 lvalue for the function template specialization. */
15263 tree
15264 resolve_nondeduced_context (tree orig_expr)
15266 tree expr, offset, baselink;
15267 bool addr;
15269 if (!type_unknown_p (orig_expr))
15270 return orig_expr;
15272 expr = orig_expr;
15273 addr = false;
15274 offset = NULL_TREE;
15275 baselink = NULL_TREE;
15277 if (TREE_CODE (expr) == ADDR_EXPR)
15279 expr = TREE_OPERAND (expr, 0);
15280 addr = true;
15282 if (TREE_CODE (expr) == OFFSET_REF)
15284 offset = expr;
15285 expr = TREE_OPERAND (expr, 1);
15287 if (BASELINK_P (expr))
15289 baselink = expr;
15290 expr = BASELINK_FUNCTIONS (expr);
15293 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
15295 int good = 0;
15296 tree goodfn = NULL_TREE;
15298 /* If we got some explicit template args, we need to plug them into
15299 the affected templates before we try to unify, in case the
15300 explicit args will completely resolve the templates in question. */
15302 tree expl_subargs = TREE_OPERAND (expr, 1);
15303 tree arg = TREE_OPERAND (expr, 0);
15304 tree badfn = NULL_TREE;
15305 tree badargs = NULL_TREE;
15307 for (; arg; arg = OVL_NEXT (arg))
15309 tree fn = OVL_CURRENT (arg);
15310 tree subargs, elem;
15312 if (TREE_CODE (fn) != TEMPLATE_DECL)
15313 continue;
15315 ++processing_template_decl;
15316 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
15317 expl_subargs, /*check_ret=*/false);
15318 if (subargs && !any_dependent_template_arguments_p (subargs))
15320 elem = instantiate_template (fn, subargs, tf_none);
15321 if (elem == error_mark_node)
15323 badfn = fn;
15324 badargs = subargs;
15326 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15328 goodfn = elem;
15329 ++good;
15332 --processing_template_decl;
15334 if (good == 1)
15336 mark_used (goodfn);
15337 expr = goodfn;
15338 if (baselink)
15339 expr = build_baselink (BASELINK_BINFO (baselink),
15340 BASELINK_ACCESS_BINFO (baselink),
15341 expr, BASELINK_OPTYPE (baselink));
15342 if (offset)
15344 tree base
15345 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15346 expr = build_offset_ref (base, expr, addr);
15348 if (addr)
15349 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15350 return expr;
15352 else if (good == 0 && badargs)
15353 /* There were no good options and at least one bad one, so let the
15354 user know what the problem is. */
15355 instantiate_template (badfn, badargs, tf_warning_or_error);
15357 return orig_expr;
15360 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15361 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15362 different overloads deduce different arguments for a given parm.
15363 ADDR_P is true if the expression for which deduction is being
15364 performed was of the form "& fn" rather than simply "fn".
15366 Returns 1 on success. */
15368 static int
15369 try_one_overload (tree tparms,
15370 tree orig_targs,
15371 tree targs,
15372 tree parm,
15373 tree arg,
15374 unification_kind_t strict,
15375 int sub_strict,
15376 bool addr_p,
15377 bool explain_p)
15379 int nargs;
15380 tree tempargs;
15381 int i;
15383 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15384 to function or pointer to member function argument if the set of
15385 overloaded functions does not contain function templates and at most
15386 one of a set of overloaded functions provides a unique match.
15388 So if this is a template, just return success. */
15390 if (uses_template_parms (arg))
15391 return 1;
15393 if (TREE_CODE (arg) == METHOD_TYPE)
15394 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15395 else if (addr_p)
15396 arg = build_pointer_type (arg);
15398 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15400 /* We don't copy orig_targs for this because if we have already deduced
15401 some template args from previous args, unify would complain when we
15402 try to deduce a template parameter for the same argument, even though
15403 there isn't really a conflict. */
15404 nargs = TREE_VEC_LENGTH (targs);
15405 tempargs = make_tree_vec (nargs);
15407 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15408 return 0;
15410 /* First make sure we didn't deduce anything that conflicts with
15411 explicitly specified args. */
15412 for (i = nargs; i--; )
15414 tree elt = TREE_VEC_ELT (tempargs, i);
15415 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15417 if (!elt)
15418 /*NOP*/;
15419 else if (uses_template_parms (elt))
15420 /* Since we're unifying against ourselves, we will fill in
15421 template args used in the function parm list with our own
15422 template parms. Discard them. */
15423 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15424 else if (oldelt && !template_args_equal (oldelt, elt))
15425 return 0;
15428 for (i = nargs; i--; )
15430 tree elt = TREE_VEC_ELT (tempargs, i);
15432 if (elt)
15433 TREE_VEC_ELT (targs, i) = elt;
15436 return 1;
15439 /* PARM is a template class (perhaps with unbound template
15440 parameters). ARG is a fully instantiated type. If ARG can be
15441 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15442 TARGS are as for unify. */
15444 static tree
15445 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15446 bool explain_p)
15448 tree copy_of_targs;
15450 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15451 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15452 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15453 return NULL_TREE;
15455 /* We need to make a new template argument vector for the call to
15456 unify. If we used TARGS, we'd clutter it up with the result of
15457 the attempted unification, even if this class didn't work out.
15458 We also don't want to commit ourselves to all the unifications
15459 we've already done, since unification is supposed to be done on
15460 an argument-by-argument basis. In other words, consider the
15461 following pathological case:
15463 template <int I, int J, int K>
15464 struct S {};
15466 template <int I, int J>
15467 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15469 template <int I, int J, int K>
15470 void f(S<I, J, K>, S<I, I, I>);
15472 void g() {
15473 S<0, 0, 0> s0;
15474 S<0, 1, 2> s2;
15476 f(s0, s2);
15479 Now, by the time we consider the unification involving `s2', we
15480 already know that we must have `f<0, 0, 0>'. But, even though
15481 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15482 because there are two ways to unify base classes of S<0, 1, 2>
15483 with S<I, I, I>. If we kept the already deduced knowledge, we
15484 would reject the possibility I=1. */
15485 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
15487 /* If unification failed, we're done. */
15488 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
15489 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
15490 return NULL_TREE;
15492 return arg;
15495 /* Given a template type PARM and a class type ARG, find the unique
15496 base type in ARG that is an instance of PARM. We do not examine
15497 ARG itself; only its base-classes. If there is not exactly one
15498 appropriate base class, return NULL_TREE. PARM may be the type of
15499 a partial specialization, as well as a plain template type. Used
15500 by unify. */
15502 static enum template_base_result
15503 get_template_base (tree tparms, tree targs, tree parm, tree arg,
15504 bool explain_p, tree *result)
15506 tree rval = NULL_TREE;
15507 tree binfo;
15509 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
15511 binfo = TYPE_BINFO (complete_type (arg));
15512 if (!binfo)
15514 /* The type could not be completed. */
15515 *result = NULL_TREE;
15516 return tbr_incomplete_type;
15519 /* Walk in inheritance graph order. The search order is not
15520 important, and this avoids multiple walks of virtual bases. */
15521 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
15523 tree r = try_class_unification (tparms, targs, parm,
15524 BINFO_TYPE (binfo), explain_p);
15526 if (r)
15528 /* If there is more than one satisfactory baseclass, then:
15530 [temp.deduct.call]
15532 If they yield more than one possible deduced A, the type
15533 deduction fails.
15535 applies. */
15536 if (rval && !same_type_p (r, rval))
15538 *result = NULL_TREE;
15539 return tbr_ambiguous_baseclass;
15542 rval = r;
15546 *result = rval;
15547 return tbr_success;
15550 /* Returns the level of DECL, which declares a template parameter. */
15552 static int
15553 template_decl_level (tree decl)
15555 switch (TREE_CODE (decl))
15557 case TYPE_DECL:
15558 case TEMPLATE_DECL:
15559 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
15561 case PARM_DECL:
15562 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
15564 default:
15565 gcc_unreachable ();
15567 return 0;
15570 /* Decide whether ARG can be unified with PARM, considering only the
15571 cv-qualifiers of each type, given STRICT as documented for unify.
15572 Returns nonzero iff the unification is OK on that basis. */
15574 static int
15575 check_cv_quals_for_unify (int strict, tree arg, tree parm)
15577 int arg_quals = cp_type_quals (arg);
15578 int parm_quals = cp_type_quals (parm);
15580 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15581 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15583 /* Although a CVR qualifier is ignored when being applied to a
15584 substituted template parameter ([8.3.2]/1 for example), that
15585 does not allow us to unify "const T" with "int&" because both
15586 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15587 It is ok when we're allowing additional CV qualifiers
15588 at the outer level [14.8.2.1]/3,1st bullet. */
15589 if ((TREE_CODE (arg) == REFERENCE_TYPE
15590 || TREE_CODE (arg) == FUNCTION_TYPE
15591 || TREE_CODE (arg) == METHOD_TYPE)
15592 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
15593 return 0;
15595 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
15596 && (parm_quals & TYPE_QUAL_RESTRICT))
15597 return 0;
15600 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15601 && (arg_quals & parm_quals) != parm_quals)
15602 return 0;
15604 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
15605 && (parm_quals & arg_quals) != arg_quals)
15606 return 0;
15608 return 1;
15611 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15612 void
15613 template_parm_level_and_index (tree parm, int* level, int* index)
15615 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15616 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15617 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15619 *index = TEMPLATE_TYPE_IDX (parm);
15620 *level = TEMPLATE_TYPE_LEVEL (parm);
15622 else
15624 *index = TEMPLATE_PARM_IDX (parm);
15625 *level = TEMPLATE_PARM_LEVEL (parm);
15629 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15630 do { \
15631 if (unify (TP, TA, P, A, S, EP)) \
15632 return 1; \
15633 } while (0);
15635 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15636 expansion at the end of PACKED_PARMS. Returns 0 if the type
15637 deduction succeeds, 1 otherwise. STRICT is the same as in
15638 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15639 call argument list. We'll need to adjust the arguments to make them
15640 types. SUBR tells us if this is from a recursive call to
15641 type_unification_real, or for comparing two template argument
15642 lists. */
15644 static int
15645 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
15646 tree packed_args, unification_kind_t strict,
15647 bool subr, bool explain_p)
15649 tree parm
15650 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
15651 tree pattern = PACK_EXPANSION_PATTERN (parm);
15652 tree pack, packs = NULL_TREE;
15653 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
15654 int len = TREE_VEC_LENGTH (packed_args);
15656 /* Determine the parameter packs we will be deducing from the
15657 pattern, and record their current deductions. */
15658 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
15659 pack; pack = TREE_CHAIN (pack))
15661 tree parm_pack = TREE_VALUE (pack);
15662 int idx, level;
15664 /* Determine the index and level of this parameter pack. */
15665 template_parm_level_and_index (parm_pack, &level, &idx);
15667 /* Keep track of the parameter packs and their corresponding
15668 argument packs. */
15669 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
15670 TREE_TYPE (packs) = make_tree_vec (len - start);
15673 /* Loop through all of the arguments that have not yet been
15674 unified and unify each with the pattern. */
15675 for (i = start; i < len; i++)
15677 tree parm;
15678 bool any_explicit = false;
15679 tree arg = TREE_VEC_ELT (packed_args, i);
15681 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15682 or the element of its argument pack at the current index if
15683 this argument was explicitly specified. */
15684 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15686 int idx, level;
15687 tree arg, pargs;
15688 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15690 arg = NULL_TREE;
15691 if (TREE_VALUE (pack)
15692 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
15693 && (i < TREE_VEC_LENGTH (pargs)))
15695 any_explicit = true;
15696 arg = TREE_VEC_ELT (pargs, i);
15698 TMPL_ARG (targs, level, idx) = arg;
15701 /* If we had explicit template arguments, substitute them into the
15702 pattern before deduction. */
15703 if (any_explicit)
15705 /* Some arguments might still be unspecified or dependent. */
15706 bool dependent;
15707 ++processing_template_decl;
15708 dependent = any_dependent_template_arguments_p (targs);
15709 if (!dependent)
15710 --processing_template_decl;
15711 parm = tsubst (pattern, targs,
15712 explain_p ? tf_warning_or_error : tf_none,
15713 NULL_TREE);
15714 if (dependent)
15715 --processing_template_decl;
15716 if (parm == error_mark_node)
15717 return 1;
15719 else
15720 parm = pattern;
15722 /* Unify the pattern with the current argument. */
15723 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15724 LOOKUP_IMPLICIT, explain_p))
15725 return 1;
15727 /* For each parameter pack, collect the deduced value. */
15728 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15730 int idx, level;
15731 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15733 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
15734 TMPL_ARG (targs, level, idx);
15738 /* Verify that the results of unification with the parameter packs
15739 produce results consistent with what we've seen before, and make
15740 the deduced argument packs available. */
15741 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15743 tree old_pack = TREE_VALUE (pack);
15744 tree new_args = TREE_TYPE (pack);
15745 int i, len = TREE_VEC_LENGTH (new_args);
15746 int idx, level;
15747 bool nondeduced_p = false;
15749 /* By default keep the original deduced argument pack.
15750 If necessary, more specific code is going to update the
15751 resulting deduced argument later down in this function. */
15752 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15753 TMPL_ARG (targs, level, idx) = old_pack;
15755 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15756 actually deduce anything. */
15757 for (i = 0; i < len && !nondeduced_p; ++i)
15758 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
15759 nondeduced_p = true;
15760 if (nondeduced_p)
15761 continue;
15763 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
15765 /* If we had fewer function args than explicit template args,
15766 just use the explicits. */
15767 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15768 int explicit_len = TREE_VEC_LENGTH (explicit_args);
15769 if (len < explicit_len)
15770 new_args = explicit_args;
15773 if (!old_pack)
15775 tree result;
15776 /* Build the deduced *_ARGUMENT_PACK. */
15777 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
15779 result = make_node (NONTYPE_ARGUMENT_PACK);
15780 TREE_TYPE (result) =
15781 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
15782 TREE_CONSTANT (result) = 1;
15784 else
15785 result = cxx_make_type (TYPE_ARGUMENT_PACK);
15787 SET_ARGUMENT_PACK_ARGS (result, new_args);
15789 /* Note the deduced argument packs for this parameter
15790 pack. */
15791 TMPL_ARG (targs, level, idx) = result;
15793 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
15794 && (ARGUMENT_PACK_ARGS (old_pack)
15795 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
15797 /* We only had the explicitly-provided arguments before, but
15798 now we have a complete set of arguments. */
15799 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15801 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
15802 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
15803 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
15805 else
15807 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
15808 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
15810 if (!comp_template_args_with_info (old_args, new_args,
15811 &bad_old_arg, &bad_new_arg))
15812 /* Inconsistent unification of this parameter pack. */
15813 return unify_parameter_pack_inconsistent (explain_p,
15814 bad_old_arg,
15815 bad_new_arg);
15819 return unify_success (explain_p);
15822 /* Deduce the value of template parameters. TPARMS is the (innermost)
15823 set of template parameters to a template. TARGS is the bindings
15824 for those template parameters, as determined thus far; TARGS may
15825 include template arguments for outer levels of template parameters
15826 as well. PARM is a parameter to a template function, or a
15827 subcomponent of that parameter; ARG is the corresponding argument.
15828 This function attempts to match PARM with ARG in a manner
15829 consistent with the existing assignments in TARGS. If more values
15830 are deduced, then TARGS is updated.
15832 Returns 0 if the type deduction succeeds, 1 otherwise. The
15833 parameter STRICT is a bitwise or of the following flags:
15835 UNIFY_ALLOW_NONE:
15836 Require an exact match between PARM and ARG.
15837 UNIFY_ALLOW_MORE_CV_QUAL:
15838 Allow the deduced ARG to be more cv-qualified (by qualification
15839 conversion) than ARG.
15840 UNIFY_ALLOW_LESS_CV_QUAL:
15841 Allow the deduced ARG to be less cv-qualified than ARG.
15842 UNIFY_ALLOW_DERIVED:
15843 Allow the deduced ARG to be a template base class of ARG,
15844 or a pointer to a template base class of the type pointed to by
15845 ARG.
15846 UNIFY_ALLOW_INTEGER:
15847 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
15848 case for more information.
15849 UNIFY_ALLOW_OUTER_LEVEL:
15850 This is the outermost level of a deduction. Used to determine validity
15851 of qualification conversions. A valid qualification conversion must
15852 have const qualified pointers leading up to the inner type which
15853 requires additional CV quals, except at the outer level, where const
15854 is not required [conv.qual]. It would be normal to set this flag in
15855 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
15856 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
15857 This is the outermost level of a deduction, and PARM can be more CV
15858 qualified at this point.
15859 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
15860 This is the outermost level of a deduction, and PARM can be less CV
15861 qualified at this point. */
15863 static int
15864 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
15865 bool explain_p)
15867 int idx;
15868 tree targ;
15869 tree tparm;
15870 int strict_in = strict;
15872 /* I don't think this will do the right thing with respect to types.
15873 But the only case I've seen it in so far has been array bounds, where
15874 signedness is the only information lost, and I think that will be
15875 okay. */
15876 while (TREE_CODE (parm) == NOP_EXPR)
15877 parm = TREE_OPERAND (parm, 0);
15879 if (arg == error_mark_node)
15880 return unify_invalid (explain_p);
15881 if (arg == unknown_type_node
15882 || arg == init_list_type_node)
15883 /* We can't deduce anything from this, but we might get all the
15884 template args from other function args. */
15885 return unify_success (explain_p);
15887 /* If PARM uses template parameters, then we can't bail out here,
15888 even if ARG == PARM, since we won't record unifications for the
15889 template parameters. We might need them if we're trying to
15890 figure out which of two things is more specialized. */
15891 if (arg == parm && !uses_template_parms (parm))
15892 return unify_success (explain_p);
15894 /* Handle init lists early, so the rest of the function can assume
15895 we're dealing with a type. */
15896 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
15898 tree elt, elttype;
15899 unsigned i;
15900 tree orig_parm = parm;
15902 /* Replace T with std::initializer_list<T> for deduction. */
15903 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15904 && flag_deduce_init_list)
15905 parm = listify (parm);
15907 if (!is_std_init_list (parm))
15908 /* We can only deduce from an initializer list argument if the
15909 parameter is std::initializer_list; otherwise this is a
15910 non-deduced context. */
15911 return unify_success (explain_p);
15913 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
15915 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
15917 int elt_strict = strict;
15919 if (elt == error_mark_node)
15920 return unify_invalid (explain_p);
15922 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
15924 tree type = TREE_TYPE (elt);
15925 /* It should only be possible to get here for a call. */
15926 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
15927 elt_strict |= maybe_adjust_types_for_deduction
15928 (DEDUCE_CALL, &elttype, &type, elt);
15929 elt = type;
15932 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
15933 explain_p);
15936 /* If the std::initializer_list<T> deduction worked, replace the
15937 deduced A with std::initializer_list<A>. */
15938 if (orig_parm != parm)
15940 idx = TEMPLATE_TYPE_IDX (orig_parm);
15941 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15942 targ = listify (targ);
15943 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
15945 return unify_success (explain_p);
15948 /* Immediately reject some pairs that won't unify because of
15949 cv-qualification mismatches. */
15950 if (TREE_CODE (arg) == TREE_CODE (parm)
15951 && TYPE_P (arg)
15952 /* It is the elements of the array which hold the cv quals of an array
15953 type, and the elements might be template type parms. We'll check
15954 when we recurse. */
15955 && TREE_CODE (arg) != ARRAY_TYPE
15956 /* We check the cv-qualifiers when unifying with template type
15957 parameters below. We want to allow ARG `const T' to unify with
15958 PARM `T' for example, when computing which of two templates
15959 is more specialized, for example. */
15960 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
15961 && !check_cv_quals_for_unify (strict_in, arg, parm))
15962 return unify_cv_qual_mismatch (explain_p, parm, arg);
15964 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
15965 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
15966 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
15967 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
15968 strict &= ~UNIFY_ALLOW_DERIVED;
15969 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15970 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
15972 switch (TREE_CODE (parm))
15974 case TYPENAME_TYPE:
15975 case SCOPE_REF:
15976 case UNBOUND_CLASS_TEMPLATE:
15977 /* In a type which contains a nested-name-specifier, template
15978 argument values cannot be deduced for template parameters used
15979 within the nested-name-specifier. */
15980 return unify_success (explain_p);
15982 case TEMPLATE_TYPE_PARM:
15983 case TEMPLATE_TEMPLATE_PARM:
15984 case BOUND_TEMPLATE_TEMPLATE_PARM:
15985 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15986 if (tparm == error_mark_node)
15987 return unify_invalid (explain_p);
15989 if (TEMPLATE_TYPE_LEVEL (parm)
15990 != template_decl_level (tparm))
15991 /* The PARM is not one we're trying to unify. Just check
15992 to see if it matches ARG. */
15994 if (TREE_CODE (arg) == TREE_CODE (parm)
15995 && (is_auto (parm) ? is_auto (arg)
15996 : same_type_p (parm, arg)))
15997 return unify_success (explain_p);
15998 else
15999 return unify_type_mismatch (explain_p, parm, arg);
16001 idx = TEMPLATE_TYPE_IDX (parm);
16002 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16003 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
16004 if (tparm == error_mark_node)
16005 return unify_invalid (explain_p);
16007 /* Check for mixed types and values. */
16008 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16009 && TREE_CODE (tparm) != TYPE_DECL)
16010 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16011 && TREE_CODE (tparm) != TEMPLATE_DECL))
16012 gcc_unreachable ();
16014 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16016 /* ARG must be constructed from a template class or a template
16017 template parameter. */
16018 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
16019 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
16020 return unify_template_deduction_failure (explain_p, parm, arg);
16023 tree parmvec = TYPE_TI_ARGS (parm);
16024 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
16025 tree full_argvec = add_to_template_args (targs, argvec);
16026 tree parm_parms
16027 = DECL_INNERMOST_TEMPLATE_PARMS
16028 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
16029 int i, len;
16030 int parm_variadic_p = 0;
16032 /* The resolution to DR150 makes clear that default
16033 arguments for an N-argument may not be used to bind T
16034 to a template template parameter with fewer than N
16035 parameters. It is not safe to permit the binding of
16036 default arguments as an extension, as that may change
16037 the meaning of a conforming program. Consider:
16039 struct Dense { static const unsigned int dim = 1; };
16041 template <template <typename> class View,
16042 typename Block>
16043 void operator+(float, View<Block> const&);
16045 template <typename Block,
16046 unsigned int Dim = Block::dim>
16047 struct Lvalue_proxy { operator float() const; };
16049 void
16050 test_1d (void) {
16051 Lvalue_proxy<Dense> p;
16052 float b;
16053 b + p;
16056 Here, if Lvalue_proxy is permitted to bind to View, then
16057 the global operator+ will be used; if they are not, the
16058 Lvalue_proxy will be converted to float. */
16059 if (coerce_template_parms (parm_parms,
16060 full_argvec,
16061 TYPE_TI_TEMPLATE (parm),
16062 (explain_p
16063 ? tf_warning_or_error
16064 : tf_none),
16065 /*require_all_args=*/true,
16066 /*use_default_args=*/false)
16067 == error_mark_node)
16068 return 1;
16070 /* Deduce arguments T, i from TT<T> or TT<i>.
16071 We check each element of PARMVEC and ARGVEC individually
16072 rather than the whole TREE_VEC since they can have
16073 different number of elements. */
16075 parmvec = expand_template_argument_pack (parmvec);
16076 argvec = expand_template_argument_pack (argvec);
16078 len = TREE_VEC_LENGTH (parmvec);
16080 /* Check if the parameters end in a pack, making them
16081 variadic. */
16082 if (len > 0
16083 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
16084 parm_variadic_p = 1;
16086 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
16087 return unify_too_few_arguments (explain_p,
16088 TREE_VEC_LENGTH (argvec), len);
16090 for (i = 0; i < len - parm_variadic_p; ++i)
16092 RECUR_AND_CHECK_FAILURE (tparms, targs,
16093 TREE_VEC_ELT (parmvec, i),
16094 TREE_VEC_ELT (argvec, i),
16095 UNIFY_ALLOW_NONE, explain_p);
16098 if (parm_variadic_p
16099 && unify_pack_expansion (tparms, targs,
16100 parmvec, argvec,
16101 DEDUCE_EXACT,
16102 /*subr=*/true, explain_p))
16103 return 1;
16105 arg = TYPE_TI_TEMPLATE (arg);
16107 /* Fall through to deduce template name. */
16110 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16111 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16113 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16115 /* Simple cases: Value already set, does match or doesn't. */
16116 if (targ != NULL_TREE && template_args_equal (targ, arg))
16117 return unify_success (explain_p);
16118 else if (targ)
16119 return unify_inconsistency (explain_p, parm, targ, arg);
16121 else
16123 /* If PARM is `const T' and ARG is only `int', we don't have
16124 a match unless we are allowing additional qualification.
16125 If ARG is `const int' and PARM is just `T' that's OK;
16126 that binds `const int' to `T'. */
16127 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
16128 arg, parm))
16129 return unify_cv_qual_mismatch (explain_p, parm, arg);
16131 /* Consider the case where ARG is `const volatile int' and
16132 PARM is `const T'. Then, T should be `volatile int'. */
16133 arg = cp_build_qualified_type_real
16134 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
16135 if (arg == error_mark_node)
16136 return unify_invalid (explain_p);
16138 /* Simple cases: Value already set, does match or doesn't. */
16139 if (targ != NULL_TREE && same_type_p (targ, arg))
16140 return unify_success (explain_p);
16141 else if (targ)
16142 return unify_inconsistency (explain_p, parm, targ, arg);
16144 /* Make sure that ARG is not a variable-sized array. (Note
16145 that were talking about variable-sized arrays (like
16146 `int[n]'), rather than arrays of unknown size (like
16147 `int[]').) We'll get very confused by such a type since
16148 the bound of the array is not constant, and therefore
16149 not mangleable. Besides, such types are not allowed in
16150 ISO C++, so we can do as we please here. We do allow
16151 them for 'auto' deduction, since that isn't ABI-exposed. */
16152 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
16153 return unify_vla_arg (explain_p, arg);
16155 /* Strip typedefs as in convert_template_argument. */
16156 arg = canonicalize_type_argument (arg, tf_none);
16159 /* If ARG is a parameter pack or an expansion, we cannot unify
16160 against it unless PARM is also a parameter pack. */
16161 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16162 && !template_parameter_pack_p (parm))
16163 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16165 /* If the argument deduction results is a METHOD_TYPE,
16166 then there is a problem.
16167 METHOD_TYPE doesn't map to any real C++ type the result of
16168 the deduction can not be of that type. */
16169 if (TREE_CODE (arg) == METHOD_TYPE)
16170 return unify_method_type_error (explain_p, arg);
16172 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16173 return unify_success (explain_p);
16175 case TEMPLATE_PARM_INDEX:
16176 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16177 if (tparm == error_mark_node)
16178 return unify_invalid (explain_p);
16180 if (TEMPLATE_PARM_LEVEL (parm)
16181 != template_decl_level (tparm))
16183 /* The PARM is not one we're trying to unify. Just check
16184 to see if it matches ARG. */
16185 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
16186 && cp_tree_equal (parm, arg));
16187 if (result)
16188 unify_expression_unequal (explain_p, parm, arg);
16189 return result;
16192 idx = TEMPLATE_PARM_IDX (parm);
16193 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16195 if (targ)
16197 int x = !cp_tree_equal (targ, arg);
16198 if (x)
16199 unify_inconsistency (explain_p, parm, targ, arg);
16200 return x;
16203 /* [temp.deduct.type] If, in the declaration of a function template
16204 with a non-type template-parameter, the non-type
16205 template-parameter is used in an expression in the function
16206 parameter-list and, if the corresponding template-argument is
16207 deduced, the template-argument type shall match the type of the
16208 template-parameter exactly, except that a template-argument
16209 deduced from an array bound may be of any integral type.
16210 The non-type parameter might use already deduced type parameters. */
16211 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
16212 if (!TREE_TYPE (arg))
16213 /* Template-parameter dependent expression. Just accept it for now.
16214 It will later be processed in convert_template_argument. */
16216 else if (same_type_p (TREE_TYPE (arg), tparm))
16217 /* OK */;
16218 else if ((strict & UNIFY_ALLOW_INTEGER)
16219 && (TREE_CODE (tparm) == INTEGER_TYPE
16220 || TREE_CODE (tparm) == BOOLEAN_TYPE))
16221 /* Convert the ARG to the type of PARM; the deduced non-type
16222 template argument must exactly match the types of the
16223 corresponding parameter. */
16224 arg = fold (build_nop (tparm, arg));
16225 else if (uses_template_parms (tparm))
16226 /* We haven't deduced the type of this parameter yet. Try again
16227 later. */
16228 return unify_success (explain_p);
16229 else
16230 return unify_type_mismatch (explain_p, tparm, arg);
16232 /* If ARG is a parameter pack or an expansion, we cannot unify
16233 against it unless PARM is also a parameter pack. */
16234 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16235 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
16236 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16238 arg = strip_typedefs_expr (arg);
16239 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16240 return unify_success (explain_p);
16242 case PTRMEM_CST:
16244 /* A pointer-to-member constant can be unified only with
16245 another constant. */
16246 if (TREE_CODE (arg) != PTRMEM_CST)
16247 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
16249 /* Just unify the class member. It would be useless (and possibly
16250 wrong, depending on the strict flags) to unify also
16251 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16252 arg refer to the same variable, even if through different
16253 classes. For instance:
16255 struct A { int x; };
16256 struct B : A { };
16258 Unification of &A::x and &B::x must succeed. */
16259 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
16260 PTRMEM_CST_MEMBER (arg), strict, explain_p);
16263 case POINTER_TYPE:
16265 if (TREE_CODE (arg) != POINTER_TYPE)
16266 return unify_type_mismatch (explain_p, parm, arg);
16268 /* [temp.deduct.call]
16270 A can be another pointer or pointer to member type that can
16271 be converted to the deduced A via a qualification
16272 conversion (_conv.qual_).
16274 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16275 This will allow for additional cv-qualification of the
16276 pointed-to types if appropriate. */
16278 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16279 /* The derived-to-base conversion only persists through one
16280 level of pointers. */
16281 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16283 return unify (tparms, targs, TREE_TYPE (parm),
16284 TREE_TYPE (arg), strict, explain_p);
16287 case REFERENCE_TYPE:
16288 if (TREE_CODE (arg) != REFERENCE_TYPE)
16289 return unify_type_mismatch (explain_p, parm, arg);
16290 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16291 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16293 case ARRAY_TYPE:
16294 if (TREE_CODE (arg) != ARRAY_TYPE)
16295 return unify_type_mismatch (explain_p, parm, arg);
16296 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16297 != (TYPE_DOMAIN (arg) == NULL_TREE))
16298 return unify_type_mismatch (explain_p, parm, arg);
16299 if (TYPE_DOMAIN (parm) != NULL_TREE)
16301 tree parm_max;
16302 tree arg_max;
16303 bool parm_cst;
16304 bool arg_cst;
16306 /* Our representation of array types uses "N - 1" as the
16307 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16308 not an integer constant. We cannot unify arbitrarily
16309 complex expressions, so we eliminate the MINUS_EXPRs
16310 here. */
16311 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16312 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16313 if (!parm_cst)
16315 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16316 parm_max = TREE_OPERAND (parm_max, 0);
16318 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16319 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16320 if (!arg_cst)
16322 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16323 trying to unify the type of a variable with the type
16324 of a template parameter. For example:
16326 template <unsigned int N>
16327 void f (char (&) [N]);
16328 int g();
16329 void h(int i) {
16330 char a[g(i)];
16331 f(a);
16334 Here, the type of the ARG will be "int [g(i)]", and
16335 may be a SAVE_EXPR, etc. */
16336 if (TREE_CODE (arg_max) != MINUS_EXPR)
16337 return unify_vla_arg (explain_p, arg);
16338 arg_max = TREE_OPERAND (arg_max, 0);
16341 /* If only one of the bounds used a MINUS_EXPR, compensate
16342 by adding one to the other bound. */
16343 if (parm_cst && !arg_cst)
16344 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16345 integer_type_node,
16346 parm_max,
16347 integer_one_node);
16348 else if (arg_cst && !parm_cst)
16349 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16350 integer_type_node,
16351 arg_max,
16352 integer_one_node);
16354 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16355 UNIFY_ALLOW_INTEGER, explain_p);
16357 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16358 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16360 case REAL_TYPE:
16361 case COMPLEX_TYPE:
16362 case VECTOR_TYPE:
16363 case INTEGER_TYPE:
16364 case BOOLEAN_TYPE:
16365 case ENUMERAL_TYPE:
16366 case VOID_TYPE:
16367 case NULLPTR_TYPE:
16368 if (TREE_CODE (arg) != TREE_CODE (parm))
16369 return unify_type_mismatch (explain_p, parm, arg);
16371 /* We have already checked cv-qualification at the top of the
16372 function. */
16373 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16374 return unify_type_mismatch (explain_p, parm, arg);
16376 /* As far as unification is concerned, this wins. Later checks
16377 will invalidate it if necessary. */
16378 return unify_success (explain_p);
16380 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16381 /* Type INTEGER_CST can come from ordinary constant template args. */
16382 case INTEGER_CST:
16383 while (TREE_CODE (arg) == NOP_EXPR)
16384 arg = TREE_OPERAND (arg, 0);
16386 if (TREE_CODE (arg) != INTEGER_CST)
16387 return unify_template_argument_mismatch (explain_p, parm, arg);
16388 return (tree_int_cst_equal (parm, arg)
16389 ? unify_success (explain_p)
16390 : unify_template_argument_mismatch (explain_p, parm, arg));
16392 case TREE_VEC:
16394 int i, len, argslen;
16395 int parm_variadic_p = 0;
16397 if (TREE_CODE (arg) != TREE_VEC)
16398 return unify_template_argument_mismatch (explain_p, parm, arg);
16400 len = TREE_VEC_LENGTH (parm);
16401 argslen = TREE_VEC_LENGTH (arg);
16403 /* Check for pack expansions in the parameters. */
16404 for (i = 0; i < len; ++i)
16406 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
16408 if (i == len - 1)
16409 /* We can unify against something with a trailing
16410 parameter pack. */
16411 parm_variadic_p = 1;
16412 else
16413 /* [temp.deduct.type]/9: If the template argument list of
16414 P contains a pack expansion that is not the last
16415 template argument, the entire template argument list
16416 is a non-deduced context. */
16417 return unify_success (explain_p);
16421 /* If we don't have enough arguments to satisfy the parameters
16422 (not counting the pack expression at the end), or we have
16423 too many arguments for a parameter list that doesn't end in
16424 a pack expression, we can't unify. */
16425 if (parm_variadic_p
16426 ? argslen < len - parm_variadic_p
16427 : argslen != len)
16428 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
16430 /* Unify all of the parameters that precede the (optional)
16431 pack expression. */
16432 for (i = 0; i < len - parm_variadic_p; ++i)
16434 RECUR_AND_CHECK_FAILURE (tparms, targs,
16435 TREE_VEC_ELT (parm, i),
16436 TREE_VEC_ELT (arg, i),
16437 UNIFY_ALLOW_NONE, explain_p);
16439 if (parm_variadic_p)
16440 return unify_pack_expansion (tparms, targs, parm, arg,
16441 DEDUCE_EXACT,
16442 /*subr=*/true, explain_p);
16443 return unify_success (explain_p);
16446 case RECORD_TYPE:
16447 case UNION_TYPE:
16448 if (TREE_CODE (arg) != TREE_CODE (parm))
16449 return unify_type_mismatch (explain_p, parm, arg);
16451 if (TYPE_PTRMEMFUNC_P (parm))
16453 if (!TYPE_PTRMEMFUNC_P (arg))
16454 return unify_type_mismatch (explain_p, parm, arg);
16456 return unify (tparms, targs,
16457 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16458 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16459 strict, explain_p);
16462 if (CLASSTYPE_TEMPLATE_INFO (parm))
16464 tree t = NULL_TREE;
16466 if (strict_in & UNIFY_ALLOW_DERIVED)
16468 /* First, we try to unify the PARM and ARG directly. */
16469 t = try_class_unification (tparms, targs,
16470 parm, arg, explain_p);
16472 if (!t)
16474 /* Fallback to the special case allowed in
16475 [temp.deduct.call]:
16477 If P is a class, and P has the form
16478 template-id, then A can be a derived class of
16479 the deduced A. Likewise, if P is a pointer to
16480 a class of the form template-id, A can be a
16481 pointer to a derived class pointed to by the
16482 deduced A. */
16483 enum template_base_result r;
16484 r = get_template_base (tparms, targs, parm, arg,
16485 explain_p, &t);
16487 if (!t)
16488 return unify_no_common_base (explain_p, r, parm, arg);
16491 else if (CLASSTYPE_TEMPLATE_INFO (arg)
16492 && (CLASSTYPE_TI_TEMPLATE (parm)
16493 == CLASSTYPE_TI_TEMPLATE (arg)))
16494 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16495 Then, we should unify `int' and `U'. */
16496 t = arg;
16497 else
16498 /* There's no chance of unification succeeding. */
16499 return unify_type_mismatch (explain_p, parm, arg);
16501 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
16502 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
16504 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
16505 return unify_type_mismatch (explain_p, parm, arg);
16506 return unify_success (explain_p);
16508 case METHOD_TYPE:
16509 case FUNCTION_TYPE:
16511 unsigned int nargs;
16512 tree *args;
16513 tree a;
16514 unsigned int i;
16516 if (TREE_CODE (arg) != TREE_CODE (parm))
16517 return unify_type_mismatch (explain_p, parm, arg);
16519 /* CV qualifications for methods can never be deduced, they must
16520 match exactly. We need to check them explicitly here,
16521 because type_unification_real treats them as any other
16522 cv-qualified parameter. */
16523 if (TREE_CODE (parm) == METHOD_TYPE
16524 && (!check_cv_quals_for_unify
16525 (UNIFY_ALLOW_NONE,
16526 class_of_this_parm (arg),
16527 class_of_this_parm (parm))))
16528 return unify_cv_qual_mismatch (explain_p, parm, arg);
16530 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
16531 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
16533 nargs = list_length (TYPE_ARG_TYPES (arg));
16534 args = XALLOCAVEC (tree, nargs);
16535 for (a = TYPE_ARG_TYPES (arg), i = 0;
16536 a != NULL_TREE && a != void_list_node;
16537 a = TREE_CHAIN (a), ++i)
16538 args[i] = TREE_VALUE (a);
16539 nargs = i;
16541 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
16542 args, nargs, 1, DEDUCE_EXACT,
16543 LOOKUP_NORMAL, explain_p);
16546 case OFFSET_TYPE:
16547 /* Unify a pointer to member with a pointer to member function, which
16548 deduces the type of the member as a function type. */
16549 if (TYPE_PTRMEMFUNC_P (arg))
16551 tree method_type;
16552 tree fntype;
16554 /* Check top-level cv qualifiers */
16555 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
16556 return unify_cv_qual_mismatch (explain_p, parm, arg);
16558 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16559 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
16560 UNIFY_ALLOW_NONE, explain_p);
16562 /* Determine the type of the function we are unifying against. */
16563 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
16564 fntype =
16565 build_function_type (TREE_TYPE (method_type),
16566 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
16568 /* Extract the cv-qualifiers of the member function from the
16569 implicit object parameter and place them on the function
16570 type to be restored later. */
16571 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
16572 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
16575 if (TREE_CODE (arg) != OFFSET_TYPE)
16576 return unify_type_mismatch (explain_p, parm, arg);
16577 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16578 TYPE_OFFSET_BASETYPE (arg),
16579 UNIFY_ALLOW_NONE, explain_p);
16580 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16581 strict, explain_p);
16583 case CONST_DECL:
16584 if (DECL_TEMPLATE_PARM_P (parm))
16585 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
16586 if (arg != integral_constant_value (parm))
16587 return unify_template_argument_mismatch (explain_p, parm, arg);
16588 return unify_success (explain_p);
16590 case FIELD_DECL:
16591 case TEMPLATE_DECL:
16592 /* Matched cases are handled by the ARG == PARM test above. */
16593 return unify_template_argument_mismatch (explain_p, parm, arg);
16595 case VAR_DECL:
16596 /* A non-type template parameter that is a variable should be a
16597 an integral constant, in which case, it whould have been
16598 folded into its (constant) value. So we should not be getting
16599 a variable here. */
16600 gcc_unreachable ();
16602 case TYPE_ARGUMENT_PACK:
16603 case NONTYPE_ARGUMENT_PACK:
16604 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
16605 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
16607 case TYPEOF_TYPE:
16608 case DECLTYPE_TYPE:
16609 case UNDERLYING_TYPE:
16610 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16611 or UNDERLYING_TYPE nodes. */
16612 return unify_success (explain_p);
16614 case ERROR_MARK:
16615 /* Unification fails if we hit an error node. */
16616 return unify_invalid (explain_p);
16618 default:
16619 /* An unresolved overload is a nondeduced context. */
16620 if (is_overloaded_fn (parm) || type_unknown_p (parm))
16621 return unify_success (explain_p);
16622 gcc_assert (EXPR_P (parm));
16624 /* We must be looking at an expression. This can happen with
16625 something like:
16627 template <int I>
16628 void foo(S<I>, S<I + 2>);
16630 This is a "nondeduced context":
16632 [deduct.type]
16634 The nondeduced contexts are:
16636 --A type that is a template-id in which one or more of
16637 the template-arguments is an expression that references
16638 a template-parameter.
16640 In these cases, we assume deduction succeeded, but don't
16641 actually infer any unifications. */
16643 if (!uses_template_parms (parm)
16644 && !template_args_equal (parm, arg))
16645 return unify_expression_unequal (explain_p, parm, arg);
16646 else
16647 return unify_success (explain_p);
16650 #undef RECUR_AND_CHECK_FAILURE
16652 /* Note that DECL can be defined in this translation unit, if
16653 required. */
16655 static void
16656 mark_definable (tree decl)
16658 tree clone;
16659 DECL_NOT_REALLY_EXTERN (decl) = 1;
16660 FOR_EACH_CLONE (clone, decl)
16661 DECL_NOT_REALLY_EXTERN (clone) = 1;
16664 /* Called if RESULT is explicitly instantiated, or is a member of an
16665 explicitly instantiated class. */
16667 void
16668 mark_decl_instantiated (tree result, int extern_p)
16670 SET_DECL_EXPLICIT_INSTANTIATION (result);
16672 /* If this entity has already been written out, it's too late to
16673 make any modifications. */
16674 if (TREE_ASM_WRITTEN (result))
16675 return;
16677 if (TREE_CODE (result) != FUNCTION_DECL)
16678 /* The TREE_PUBLIC flag for function declarations will have been
16679 set correctly by tsubst. */
16680 TREE_PUBLIC (result) = 1;
16682 /* This might have been set by an earlier implicit instantiation. */
16683 DECL_COMDAT (result) = 0;
16685 if (extern_p)
16686 DECL_NOT_REALLY_EXTERN (result) = 0;
16687 else
16689 mark_definable (result);
16690 /* Always make artificials weak. */
16691 if (DECL_ARTIFICIAL (result) && flag_weak)
16692 comdat_linkage (result);
16693 /* For WIN32 we also want to put explicit instantiations in
16694 linkonce sections. */
16695 else if (TREE_PUBLIC (result))
16696 maybe_make_one_only (result);
16699 /* If EXTERN_P, then this function will not be emitted -- unless
16700 followed by an explicit instantiation, at which point its linkage
16701 will be adjusted. If !EXTERN_P, then this function will be
16702 emitted here. In neither circumstance do we want
16703 import_export_decl to adjust the linkage. */
16704 DECL_INTERFACE_KNOWN (result) = 1;
16707 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16708 important template arguments. If any are missing, we check whether
16709 they're important by using error_mark_node for substituting into any
16710 args that were used for partial ordering (the ones between ARGS and END)
16711 and seeing if it bubbles up. */
16713 static bool
16714 check_undeduced_parms (tree targs, tree args, tree end)
16716 bool found = false;
16717 int i;
16718 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
16719 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
16721 found = true;
16722 TREE_VEC_ELT (targs, i) = error_mark_node;
16724 if (found)
16726 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
16727 if (substed == error_mark_node)
16728 return true;
16730 return false;
16733 /* Given two function templates PAT1 and PAT2, return:
16735 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16736 -1 if PAT2 is more specialized than PAT1.
16737 0 if neither is more specialized.
16739 LEN indicates the number of parameters we should consider
16740 (defaulted parameters should not be considered).
16742 The 1998 std underspecified function template partial ordering, and
16743 DR214 addresses the issue. We take pairs of arguments, one from
16744 each of the templates, and deduce them against each other. One of
16745 the templates will be more specialized if all the *other*
16746 template's arguments deduce against its arguments and at least one
16747 of its arguments *does* *not* deduce against the other template's
16748 corresponding argument. Deduction is done as for class templates.
16749 The arguments used in deduction have reference and top level cv
16750 qualifiers removed. Iff both arguments were originally reference
16751 types *and* deduction succeeds in both directions, the template
16752 with the more cv-qualified argument wins for that pairing (if
16753 neither is more cv-qualified, they both are equal). Unlike regular
16754 deduction, after all the arguments have been deduced in this way,
16755 we do *not* verify the deduced template argument values can be
16756 substituted into non-deduced contexts.
16758 The logic can be a bit confusing here, because we look at deduce1 and
16759 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16760 can find template arguments for pat1 to make arg1 look like arg2, that
16761 means that arg2 is at least as specialized as arg1. */
16764 more_specialized_fn (tree pat1, tree pat2, int len)
16766 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
16767 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
16768 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
16769 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
16770 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
16771 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
16772 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
16773 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
16774 tree origs1, origs2;
16775 bool lose1 = false;
16776 bool lose2 = false;
16778 /* Remove the this parameter from non-static member functions. If
16779 one is a non-static member function and the other is not a static
16780 member function, remove the first parameter from that function
16781 also. This situation occurs for operator functions where we
16782 locate both a member function (with this pointer) and non-member
16783 operator (with explicit first operand). */
16784 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
16786 len--; /* LEN is the number of significant arguments for DECL1 */
16787 args1 = TREE_CHAIN (args1);
16788 if (!DECL_STATIC_FUNCTION_P (decl2))
16789 args2 = TREE_CHAIN (args2);
16791 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
16793 args2 = TREE_CHAIN (args2);
16794 if (!DECL_STATIC_FUNCTION_P (decl1))
16796 len--;
16797 args1 = TREE_CHAIN (args1);
16801 /* If only one is a conversion operator, they are unordered. */
16802 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
16803 return 0;
16805 /* Consider the return type for a conversion function */
16806 if (DECL_CONV_FN_P (decl1))
16808 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
16809 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
16810 len++;
16813 processing_template_decl++;
16815 origs1 = args1;
16816 origs2 = args2;
16818 while (len--
16819 /* Stop when an ellipsis is seen. */
16820 && args1 != NULL_TREE && args2 != NULL_TREE)
16822 tree arg1 = TREE_VALUE (args1);
16823 tree arg2 = TREE_VALUE (args2);
16824 int deduce1, deduce2;
16825 int quals1 = -1;
16826 int quals2 = -1;
16828 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16829 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16831 /* When both arguments are pack expansions, we need only
16832 unify the patterns themselves. */
16833 arg1 = PACK_EXPANSION_PATTERN (arg1);
16834 arg2 = PACK_EXPANSION_PATTERN (arg2);
16836 /* This is the last comparison we need to do. */
16837 len = 0;
16840 if (TREE_CODE (arg1) == REFERENCE_TYPE)
16842 arg1 = TREE_TYPE (arg1);
16843 quals1 = cp_type_quals (arg1);
16846 if (TREE_CODE (arg2) == REFERENCE_TYPE)
16848 arg2 = TREE_TYPE (arg2);
16849 quals2 = cp_type_quals (arg2);
16852 arg1 = TYPE_MAIN_VARIANT (arg1);
16853 arg2 = TYPE_MAIN_VARIANT (arg2);
16855 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
16857 int i, len2 = list_length (args2);
16858 tree parmvec = make_tree_vec (1);
16859 tree argvec = make_tree_vec (len2);
16860 tree ta = args2;
16862 /* Setup the parameter vector, which contains only ARG1. */
16863 TREE_VEC_ELT (parmvec, 0) = arg1;
16865 /* Setup the argument vector, which contains the remaining
16866 arguments. */
16867 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
16868 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16870 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
16871 argvec, DEDUCE_EXACT,
16872 /*subr=*/true, /*explain_p=*/false)
16873 == 0);
16875 /* We cannot deduce in the other direction, because ARG1 is
16876 a pack expansion but ARG2 is not. */
16877 deduce2 = 0;
16879 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16881 int i, len1 = list_length (args1);
16882 tree parmvec = make_tree_vec (1);
16883 tree argvec = make_tree_vec (len1);
16884 tree ta = args1;
16886 /* Setup the parameter vector, which contains only ARG1. */
16887 TREE_VEC_ELT (parmvec, 0) = arg2;
16889 /* Setup the argument vector, which contains the remaining
16890 arguments. */
16891 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
16892 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16894 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
16895 argvec, DEDUCE_EXACT,
16896 /*subr=*/true, /*explain_p=*/false)
16897 == 0);
16899 /* We cannot deduce in the other direction, because ARG2 is
16900 a pack expansion but ARG1 is not.*/
16901 deduce1 = 0;
16904 else
16906 /* The normal case, where neither argument is a pack
16907 expansion. */
16908 deduce1 = (unify (tparms1, targs1, arg1, arg2,
16909 UNIFY_ALLOW_NONE, /*explain_p=*/false)
16910 == 0);
16911 deduce2 = (unify (tparms2, targs2, arg2, arg1,
16912 UNIFY_ALLOW_NONE, /*explain_p=*/false)
16913 == 0);
16916 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16917 arg2, then arg2 is not as specialized as arg1. */
16918 if (!deduce1)
16919 lose2 = true;
16920 if (!deduce2)
16921 lose1 = true;
16923 /* "If, for a given type, deduction succeeds in both directions
16924 (i.e., the types are identical after the transformations above)
16925 and if the type from the argument template is more cv-qualified
16926 than the type from the parameter template (as described above)
16927 that type is considered to be more specialized than the other. If
16928 neither type is more cv-qualified than the other then neither type
16929 is more specialized than the other." */
16931 if (deduce1 && deduce2
16932 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
16934 if ((quals1 & quals2) == quals2)
16935 lose2 = true;
16936 if ((quals1 & quals2) == quals1)
16937 lose1 = true;
16940 if (lose1 && lose2)
16941 /* We've failed to deduce something in either direction.
16942 These must be unordered. */
16943 break;
16945 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16946 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16947 /* We have already processed all of the arguments in our
16948 handing of the pack expansion type. */
16949 len = 0;
16951 args1 = TREE_CHAIN (args1);
16952 args2 = TREE_CHAIN (args2);
16955 /* "In most cases, all template parameters must have values in order for
16956 deduction to succeed, but for partial ordering purposes a template
16957 parameter may remain without a value provided it is not used in the
16958 types being used for partial ordering."
16960 Thus, if we are missing any of the targs1 we need to substitute into
16961 origs1, then pat2 is not as specialized as pat1. This can happen when
16962 there is a nondeduced context. */
16963 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
16964 lose2 = true;
16965 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
16966 lose1 = true;
16968 processing_template_decl--;
16970 /* All things being equal, if the next argument is a pack expansion
16971 for one function but not for the other, prefer the
16972 non-variadic function. FIXME this is bogus; see c++/41958. */
16973 if (lose1 == lose2
16974 && args1 && TREE_VALUE (args1)
16975 && args2 && TREE_VALUE (args2))
16977 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
16978 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
16981 if (lose1 == lose2)
16982 return 0;
16983 else if (!lose1)
16984 return 1;
16985 else
16986 return -1;
16989 /* Determine which of two partial specializations is more specialized.
16991 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16992 to the first partial specialization. The TREE_VALUE is the
16993 innermost set of template parameters for the partial
16994 specialization. PAT2 is similar, but for the second template.
16996 Return 1 if the first partial specialization is more specialized;
16997 -1 if the second is more specialized; 0 if neither is more
16998 specialized.
17000 See [temp.class.order] for information about determining which of
17001 two templates is more specialized. */
17003 static int
17004 more_specialized_class (tree pat1, tree pat2)
17006 tree targs;
17007 tree tmpl1, tmpl2;
17008 int winner = 0;
17009 bool any_deductions = false;
17011 tmpl1 = TREE_TYPE (pat1);
17012 tmpl2 = TREE_TYPE (pat2);
17014 /* Just like what happens for functions, if we are ordering between
17015 different class template specializations, we may encounter dependent
17016 types in the arguments, and we need our dependency check functions
17017 to behave correctly. */
17018 ++processing_template_decl;
17019 targs = get_class_bindings (TREE_VALUE (pat1),
17020 CLASSTYPE_TI_ARGS (tmpl1),
17021 CLASSTYPE_TI_ARGS (tmpl2));
17022 if (targs)
17024 --winner;
17025 any_deductions = true;
17028 targs = get_class_bindings (TREE_VALUE (pat2),
17029 CLASSTYPE_TI_ARGS (tmpl2),
17030 CLASSTYPE_TI_ARGS (tmpl1));
17031 if (targs)
17033 ++winner;
17034 any_deductions = true;
17036 --processing_template_decl;
17038 /* In the case of a tie where at least one of the class templates
17039 has a parameter pack at the end, the template with the most
17040 non-packed parameters wins. */
17041 if (winner == 0
17042 && any_deductions
17043 && (template_args_variadic_p (TREE_PURPOSE (pat1))
17044 || template_args_variadic_p (TREE_PURPOSE (pat2))))
17046 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
17047 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
17048 int len1 = TREE_VEC_LENGTH (args1);
17049 int len2 = TREE_VEC_LENGTH (args2);
17051 /* We don't count the pack expansion at the end. */
17052 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
17053 --len1;
17054 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
17055 --len2;
17057 if (len1 > len2)
17058 return 1;
17059 else if (len1 < len2)
17060 return -1;
17063 return winner;
17066 /* Return the template arguments that will produce the function signature
17067 DECL from the function template FN, with the explicit template
17068 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17069 also match. Return NULL_TREE if no satisfactory arguments could be
17070 found. */
17072 static tree
17073 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
17075 int ntparms = DECL_NTPARMS (fn);
17076 tree targs = make_tree_vec (ntparms);
17077 tree decl_type;
17078 tree decl_arg_types;
17079 tree *args;
17080 unsigned int nargs, ix;
17081 tree arg;
17083 /* Substitute the explicit template arguments into the type of DECL.
17084 The call to fn_type_unification will handle substitution into the
17085 FN. */
17086 decl_type = TREE_TYPE (decl);
17087 if (explicit_args && decl == DECL_TEMPLATE_RESULT (fn))
17089 tree tmpl;
17090 tree converted_args;
17092 if (DECL_TEMPLATE_INFO (decl))
17093 tmpl = DECL_TI_TEMPLATE (decl);
17094 else
17095 /* We can get here for some invalid specializations. */
17096 return NULL_TREE;
17098 converted_args
17099 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17100 explicit_args, NULL_TREE,
17101 tf_none,
17102 /*require_all_args=*/false,
17103 /*use_default_args=*/false);
17104 if (converted_args == error_mark_node)
17105 return NULL_TREE;
17107 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
17108 if (decl_type == error_mark_node)
17109 return NULL_TREE;
17112 /* Never do unification on the 'this' parameter. */
17113 decl_arg_types = skip_artificial_parms_for (decl,
17114 TYPE_ARG_TYPES (decl_type));
17116 nargs = list_length (decl_arg_types);
17117 args = XALLOCAVEC (tree, nargs);
17118 for (arg = decl_arg_types, ix = 0;
17119 arg != NULL_TREE && arg != void_list_node;
17120 arg = TREE_CHAIN (arg), ++ix)
17121 args[ix] = TREE_VALUE (arg);
17123 if (fn_type_unification (fn, explicit_args, targs,
17124 args, ix,
17125 (check_rettype || DECL_CONV_FN_P (fn)
17126 ? TREE_TYPE (decl_type) : NULL_TREE),
17127 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false))
17128 return NULL_TREE;
17130 return targs;
17133 /* Return the innermost template arguments that, when applied to a
17134 template specialization whose innermost template parameters are
17135 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17136 ARGS.
17138 For example, suppose we have:
17140 template <class T, class U> struct S {};
17141 template <class T> struct S<T*, int> {};
17143 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17144 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17145 int}. The resulting vector will be {double}, indicating that `T'
17146 is bound to `double'. */
17148 static tree
17149 get_class_bindings (tree tparms, tree spec_args, tree args)
17151 int i, ntparms = TREE_VEC_LENGTH (tparms);
17152 tree deduced_args;
17153 tree innermost_deduced_args;
17155 innermost_deduced_args = make_tree_vec (ntparms);
17156 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17158 deduced_args = copy_node (args);
17159 SET_TMPL_ARGS_LEVEL (deduced_args,
17160 TMPL_ARGS_DEPTH (deduced_args),
17161 innermost_deduced_args);
17163 else
17164 deduced_args = innermost_deduced_args;
17166 if (unify (tparms, deduced_args,
17167 INNERMOST_TEMPLATE_ARGS (spec_args),
17168 INNERMOST_TEMPLATE_ARGS (args),
17169 UNIFY_ALLOW_NONE, /*explain_p=*/false))
17170 return NULL_TREE;
17172 for (i = 0; i < ntparms; ++i)
17173 if (! TREE_VEC_ELT (innermost_deduced_args, i))
17174 return NULL_TREE;
17176 /* Verify that nondeduced template arguments agree with the type
17177 obtained from argument deduction.
17179 For example:
17181 struct A { typedef int X; };
17182 template <class T, class U> struct C {};
17183 template <class T> struct C<T, typename T::X> {};
17185 Then with the instantiation `C<A, int>', we can deduce that
17186 `T' is `A' but unify () does not check whether `typename T::X'
17187 is `int'. */
17188 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
17189 if (spec_args == error_mark_node
17190 /* We only need to check the innermost arguments; the other
17191 arguments will always agree. */
17192 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
17193 INNERMOST_TEMPLATE_ARGS (args)))
17194 return NULL_TREE;
17196 /* Now that we have bindings for all of the template arguments,
17197 ensure that the arguments deduced for the template template
17198 parameters have compatible template parameter lists. See the use
17199 of template_template_parm_bindings_ok_p in fn_type_unification
17200 for more information. */
17201 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
17202 return NULL_TREE;
17204 return deduced_args;
17207 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17208 Return the TREE_LIST node with the most specialized template, if
17209 any. If there is no most specialized template, the error_mark_node
17210 is returned.
17212 Note that this function does not look at, or modify, the
17213 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17214 returned is one of the elements of INSTANTIATIONS, callers may
17215 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17216 and retrieve it from the value returned. */
17218 tree
17219 most_specialized_instantiation (tree templates)
17221 tree fn, champ;
17223 ++processing_template_decl;
17225 champ = templates;
17226 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17228 int fate = 0;
17230 if (get_bindings (TREE_VALUE (champ),
17231 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17232 NULL_TREE, /*check_ret=*/true))
17233 fate--;
17235 if (get_bindings (TREE_VALUE (fn),
17236 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17237 NULL_TREE, /*check_ret=*/true))
17238 fate++;
17240 if (fate == -1)
17241 champ = fn;
17242 else if (!fate)
17244 /* Equally specialized, move to next function. If there
17245 is no next function, nothing's most specialized. */
17246 fn = TREE_CHAIN (fn);
17247 champ = fn;
17248 if (!fn)
17249 break;
17253 if (champ)
17254 /* Now verify that champ is better than everything earlier in the
17255 instantiation list. */
17256 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17257 if (get_bindings (TREE_VALUE (champ),
17258 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17259 NULL_TREE, /*check_ret=*/true)
17260 || !get_bindings (TREE_VALUE (fn),
17261 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17262 NULL_TREE, /*check_ret=*/true))
17264 champ = NULL_TREE;
17265 break;
17268 processing_template_decl--;
17270 if (!champ)
17271 return error_mark_node;
17273 return champ;
17276 /* If DECL is a specialization of some template, return the most
17277 general such template. Otherwise, returns NULL_TREE.
17279 For example, given:
17281 template <class T> struct S { template <class U> void f(U); };
17283 if TMPL is `template <class U> void S<int>::f(U)' this will return
17284 the full template. This function will not trace past partial
17285 specializations, however. For example, given in addition:
17287 template <class T> struct S<T*> { template <class U> void f(U); };
17289 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17290 `template <class T> template <class U> S<T*>::f(U)'. */
17292 tree
17293 most_general_template (tree decl)
17295 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17296 an immediate specialization. */
17297 if (TREE_CODE (decl) == FUNCTION_DECL)
17299 if (DECL_TEMPLATE_INFO (decl)) {
17300 decl = DECL_TI_TEMPLATE (decl);
17302 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17303 template friend. */
17304 if (TREE_CODE (decl) != TEMPLATE_DECL)
17305 return NULL_TREE;
17306 } else
17307 return NULL_TREE;
17310 /* Look for more and more general templates. */
17311 while (DECL_TEMPLATE_INFO (decl))
17313 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17314 (See cp-tree.h for details.) */
17315 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17316 break;
17318 if (CLASS_TYPE_P (TREE_TYPE (decl))
17319 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17320 break;
17322 /* Stop if we run into an explicitly specialized class template. */
17323 if (!DECL_NAMESPACE_SCOPE_P (decl)
17324 && DECL_CONTEXT (decl)
17325 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17326 break;
17328 decl = DECL_TI_TEMPLATE (decl);
17331 return decl;
17334 /* Return the most specialized of the class template partial
17335 specializations of TMPL which can produce TYPE, a specialization of
17336 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17337 a _TYPE node corresponding to the partial specialization, while the
17338 TREE_PURPOSE is the set of template arguments that must be
17339 substituted into the TREE_TYPE in order to generate TYPE.
17341 If the choice of partial specialization is ambiguous, a diagnostic
17342 is issued, and the error_mark_node is returned. If there are no
17343 partial specializations of TMPL matching TYPE, then NULL_TREE is
17344 returned. */
17346 static tree
17347 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17349 tree list = NULL_TREE;
17350 tree t;
17351 tree champ;
17352 int fate;
17353 bool ambiguous_p;
17354 tree args;
17355 tree outer_args = NULL_TREE;
17357 tmpl = most_general_template (tmpl);
17358 args = CLASSTYPE_TI_ARGS (type);
17360 /* For determining which partial specialization to use, only the
17361 innermost args are interesting. */
17362 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17364 outer_args = strip_innermost_template_args (args, 1);
17365 args = INNERMOST_TEMPLATE_ARGS (args);
17368 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17370 tree partial_spec_args;
17371 tree spec_args;
17372 tree parms = TREE_VALUE (t);
17374 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17376 ++processing_template_decl;
17378 if (outer_args)
17380 int i;
17382 /* Discard the outer levels of args, and then substitute in the
17383 template args from the enclosing class. */
17384 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17385 partial_spec_args = tsubst_template_args
17386 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17388 /* PARMS already refers to just the innermost parms, but the
17389 template parms in partial_spec_args had their levels lowered
17390 by tsubst, so we need to do the same for the parm list. We
17391 can't just tsubst the TREE_VEC itself, as tsubst wants to
17392 treat a TREE_VEC as an argument vector. */
17393 parms = copy_node (parms);
17394 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17395 TREE_VEC_ELT (parms, i) =
17396 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17400 partial_spec_args =
17401 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17402 add_to_template_args (outer_args,
17403 partial_spec_args),
17404 tmpl, tf_none,
17405 /*require_all_args=*/true,
17406 /*use_default_args=*/true);
17408 --processing_template_decl;
17410 if (partial_spec_args == error_mark_node)
17411 return error_mark_node;
17413 spec_args = get_class_bindings (parms,
17414 partial_spec_args,
17415 args);
17416 if (spec_args)
17418 if (outer_args)
17419 spec_args = add_to_template_args (outer_args, spec_args);
17420 list = tree_cons (spec_args, TREE_VALUE (t), list);
17421 TREE_TYPE (list) = TREE_TYPE (t);
17425 if (! list)
17426 return NULL_TREE;
17428 ambiguous_p = false;
17429 t = list;
17430 champ = t;
17431 t = TREE_CHAIN (t);
17432 for (; t; t = TREE_CHAIN (t))
17434 fate = more_specialized_class (champ, t);
17435 if (fate == 1)
17437 else
17439 if (fate == 0)
17441 t = TREE_CHAIN (t);
17442 if (! t)
17444 ambiguous_p = true;
17445 break;
17448 champ = t;
17452 if (!ambiguous_p)
17453 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17455 fate = more_specialized_class (champ, t);
17456 if (fate != 1)
17458 ambiguous_p = true;
17459 break;
17463 if (ambiguous_p)
17465 const char *str;
17466 char *spaces = NULL;
17467 if (!(complain & tf_error))
17468 return error_mark_node;
17469 error ("ambiguous class template instantiation for %q#T", type);
17470 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17471 for (t = list; t; t = TREE_CHAIN (t))
17473 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17474 spaces = spaces ? spaces : get_spaces (str);
17476 free (spaces);
17477 return error_mark_node;
17480 return champ;
17483 /* Explicitly instantiate DECL. */
17485 void
17486 do_decl_instantiation (tree decl, tree storage)
17488 tree result = NULL_TREE;
17489 int extern_p = 0;
17491 if (!decl || decl == error_mark_node)
17492 /* An error occurred, for which grokdeclarator has already issued
17493 an appropriate message. */
17494 return;
17495 else if (! DECL_LANG_SPECIFIC (decl))
17497 error ("explicit instantiation of non-template %q#D", decl);
17498 return;
17500 else if (TREE_CODE (decl) == VAR_DECL)
17502 /* There is an asymmetry here in the way VAR_DECLs and
17503 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17504 the latter, the DECL we get back will be marked as a
17505 template instantiation, and the appropriate
17506 DECL_TEMPLATE_INFO will be set up. This does not happen for
17507 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17508 should handle VAR_DECLs as it currently handles
17509 FUNCTION_DECLs. */
17510 if (!DECL_CLASS_SCOPE_P (decl))
17512 error ("%qD is not a static data member of a class template", decl);
17513 return;
17515 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
17516 if (!result || TREE_CODE (result) != VAR_DECL)
17518 error ("no matching template for %qD found", decl);
17519 return;
17521 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
17523 error ("type %qT for explicit instantiation %qD does not match "
17524 "declared type %qT", TREE_TYPE (result), decl,
17525 TREE_TYPE (decl));
17526 return;
17529 else if (TREE_CODE (decl) != FUNCTION_DECL)
17531 error ("explicit instantiation of %q#D", decl);
17532 return;
17534 else
17535 result = decl;
17537 /* Check for various error cases. Note that if the explicit
17538 instantiation is valid the RESULT will currently be marked as an
17539 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17540 until we get here. */
17542 if (DECL_TEMPLATE_SPECIALIZATION (result))
17544 /* DR 259 [temp.spec].
17546 Both an explicit instantiation and a declaration of an explicit
17547 specialization shall not appear in a program unless the explicit
17548 instantiation follows a declaration of the explicit specialization.
17550 For a given set of template parameters, if an explicit
17551 instantiation of a template appears after a declaration of an
17552 explicit specialization for that template, the explicit
17553 instantiation has no effect. */
17554 return;
17556 else if (DECL_EXPLICIT_INSTANTIATION (result))
17558 /* [temp.spec]
17560 No program shall explicitly instantiate any template more
17561 than once.
17563 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17564 the first instantiation was `extern' and the second is not,
17565 and EXTERN_P for the opposite case. */
17566 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
17567 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
17568 /* If an "extern" explicit instantiation follows an ordinary
17569 explicit instantiation, the template is instantiated. */
17570 if (extern_p)
17571 return;
17573 else if (!DECL_IMPLICIT_INSTANTIATION (result))
17575 error ("no matching template for %qD found", result);
17576 return;
17578 else if (!DECL_TEMPLATE_INFO (result))
17580 permerror (input_location, "explicit instantiation of non-template %q#D", result);
17581 return;
17584 if (storage == NULL_TREE)
17586 else if (storage == ridpointers[(int) RID_EXTERN])
17588 if (!in_system_header && (cxx_dialect == cxx98))
17589 pedwarn (input_location, OPT_Wpedantic,
17590 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17591 "instantiations");
17592 extern_p = 1;
17594 else
17595 error ("storage class %qD applied to template instantiation", storage);
17597 check_explicit_instantiation_namespace (result);
17598 mark_decl_instantiated (result, extern_p);
17599 if (! extern_p)
17600 instantiate_decl (result, /*defer_ok=*/1,
17601 /*expl_inst_class_mem_p=*/false);
17604 static void
17605 mark_class_instantiated (tree t, int extern_p)
17607 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
17608 SET_CLASSTYPE_INTERFACE_KNOWN (t);
17609 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
17610 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
17611 if (! extern_p)
17613 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
17614 rest_of_type_compilation (t, 1);
17618 /* Called from do_type_instantiation through binding_table_foreach to
17619 do recursive instantiation for the type bound in ENTRY. */
17620 static void
17621 bt_instantiate_type_proc (binding_entry entry, void *data)
17623 tree storage = *(tree *) data;
17625 if (MAYBE_CLASS_TYPE_P (entry->type)
17626 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
17627 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
17630 /* Called from do_type_instantiation to instantiate a member
17631 (a member function or a static member variable) of an
17632 explicitly instantiated class template. */
17633 static void
17634 instantiate_class_member (tree decl, int extern_p)
17636 mark_decl_instantiated (decl, extern_p);
17637 if (! extern_p)
17638 instantiate_decl (decl, /*defer_ok=*/1,
17639 /*expl_inst_class_mem_p=*/true);
17642 /* Perform an explicit instantiation of template class T. STORAGE, if
17643 non-null, is the RID for extern, inline or static. COMPLAIN is
17644 nonzero if this is called from the parser, zero if called recursively,
17645 since the standard is unclear (as detailed below). */
17647 void
17648 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
17650 int extern_p = 0;
17651 int nomem_p = 0;
17652 int static_p = 0;
17653 int previous_instantiation_extern_p = 0;
17655 if (TREE_CODE (t) == TYPE_DECL)
17656 t = TREE_TYPE (t);
17658 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
17660 tree tmpl =
17661 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
17662 if (tmpl)
17663 error ("explicit instantiation of non-class template %qD", tmpl);
17664 else
17665 error ("explicit instantiation of non-template type %qT", t);
17666 return;
17669 complete_type (t);
17671 if (!COMPLETE_TYPE_P (t))
17673 if (complain & tf_error)
17674 error ("explicit instantiation of %q#T before definition of template",
17676 return;
17679 if (storage != NULL_TREE)
17681 if (!in_system_header)
17683 if (storage == ridpointers[(int) RID_EXTERN])
17685 if (cxx_dialect == cxx98)
17686 pedwarn (input_location, OPT_Wpedantic,
17687 "ISO C++ 1998 forbids the use of %<extern%> on "
17688 "explicit instantiations");
17690 else
17691 pedwarn (input_location, OPT_Wpedantic,
17692 "ISO C++ forbids the use of %qE"
17693 " on explicit instantiations", storage);
17696 if (storage == ridpointers[(int) RID_INLINE])
17697 nomem_p = 1;
17698 else if (storage == ridpointers[(int) RID_EXTERN])
17699 extern_p = 1;
17700 else if (storage == ridpointers[(int) RID_STATIC])
17701 static_p = 1;
17702 else
17704 error ("storage class %qD applied to template instantiation",
17705 storage);
17706 extern_p = 0;
17710 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
17712 /* DR 259 [temp.spec].
17714 Both an explicit instantiation and a declaration of an explicit
17715 specialization shall not appear in a program unless the explicit
17716 instantiation follows a declaration of the explicit specialization.
17718 For a given set of template parameters, if an explicit
17719 instantiation of a template appears after a declaration of an
17720 explicit specialization for that template, the explicit
17721 instantiation has no effect. */
17722 return;
17724 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
17726 /* [temp.spec]
17728 No program shall explicitly instantiate any template more
17729 than once.
17731 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17732 instantiation was `extern'. If EXTERN_P then the second is.
17733 These cases are OK. */
17734 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
17736 if (!previous_instantiation_extern_p && !extern_p
17737 && (complain & tf_error))
17738 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
17740 /* If we've already instantiated the template, just return now. */
17741 if (!CLASSTYPE_INTERFACE_ONLY (t))
17742 return;
17745 check_explicit_instantiation_namespace (TYPE_NAME (t));
17746 mark_class_instantiated (t, extern_p);
17748 if (nomem_p)
17749 return;
17752 tree tmp;
17754 /* In contrast to implicit instantiation, where only the
17755 declarations, and not the definitions, of members are
17756 instantiated, we have here:
17758 [temp.explicit]
17760 The explicit instantiation of a class template specialization
17761 implies the instantiation of all of its members not
17762 previously explicitly specialized in the translation unit
17763 containing the explicit instantiation.
17765 Of course, we can't instantiate member template classes, since
17766 we don't have any arguments for them. Note that the standard
17767 is unclear on whether the instantiation of the members are
17768 *explicit* instantiations or not. However, the most natural
17769 interpretation is that it should be an explicit instantiation. */
17771 if (! static_p)
17772 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
17773 if (TREE_CODE (tmp) == FUNCTION_DECL
17774 && DECL_TEMPLATE_INSTANTIATION (tmp))
17775 instantiate_class_member (tmp, extern_p);
17777 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
17778 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
17779 instantiate_class_member (tmp, extern_p);
17781 if (CLASSTYPE_NESTED_UTDS (t))
17782 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
17783 bt_instantiate_type_proc, &storage);
17787 /* Given a function DECL, which is a specialization of TMPL, modify
17788 DECL to be a re-instantiation of TMPL with the same template
17789 arguments. TMPL should be the template into which tsubst'ing
17790 should occur for DECL, not the most general template.
17792 One reason for doing this is a scenario like this:
17794 template <class T>
17795 void f(const T&, int i);
17797 void g() { f(3, 7); }
17799 template <class T>
17800 void f(const T& t, const int i) { }
17802 Note that when the template is first instantiated, with
17803 instantiate_template, the resulting DECL will have no name for the
17804 first parameter, and the wrong type for the second. So, when we go
17805 to instantiate the DECL, we regenerate it. */
17807 static void
17808 regenerate_decl_from_template (tree decl, tree tmpl)
17810 /* The arguments used to instantiate DECL, from the most general
17811 template. */
17812 tree args;
17813 tree code_pattern;
17815 args = DECL_TI_ARGS (decl);
17816 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
17818 /* Make sure that we can see identifiers, and compute access
17819 correctly. */
17820 push_access_scope (decl);
17822 if (TREE_CODE (decl) == FUNCTION_DECL)
17824 tree decl_parm;
17825 tree pattern_parm;
17826 tree specs;
17827 int args_depth;
17828 int parms_depth;
17830 args_depth = TMPL_ARGS_DEPTH (args);
17831 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
17832 if (args_depth > parms_depth)
17833 args = get_innermost_template_args (args, parms_depth);
17835 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
17836 args, tf_error, NULL_TREE,
17837 /*defer_ok*/false);
17838 if (specs && specs != error_mark_node)
17839 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
17840 specs);
17842 /* Merge parameter declarations. */
17843 decl_parm = skip_artificial_parms_for (decl,
17844 DECL_ARGUMENTS (decl));
17845 pattern_parm
17846 = skip_artificial_parms_for (code_pattern,
17847 DECL_ARGUMENTS (code_pattern));
17848 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
17850 tree parm_type;
17851 tree attributes;
17853 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17854 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
17855 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
17856 NULL_TREE);
17857 parm_type = type_decays_to (parm_type);
17858 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17859 TREE_TYPE (decl_parm) = parm_type;
17860 attributes = DECL_ATTRIBUTES (pattern_parm);
17861 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17863 DECL_ATTRIBUTES (decl_parm) = attributes;
17864 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17866 decl_parm = DECL_CHAIN (decl_parm);
17867 pattern_parm = DECL_CHAIN (pattern_parm);
17869 /* Merge any parameters that match with the function parameter
17870 pack. */
17871 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
17873 int i, len;
17874 tree expanded_types;
17875 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17876 the parameters in this function parameter pack. */
17877 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
17878 args, tf_error, NULL_TREE);
17879 len = TREE_VEC_LENGTH (expanded_types);
17880 for (i = 0; i < len; i++)
17882 tree parm_type;
17883 tree attributes;
17885 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17886 /* Rename the parameter to include the index. */
17887 DECL_NAME (decl_parm) =
17888 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
17889 parm_type = TREE_VEC_ELT (expanded_types, i);
17890 parm_type = type_decays_to (parm_type);
17891 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17892 TREE_TYPE (decl_parm) = parm_type;
17893 attributes = DECL_ATTRIBUTES (pattern_parm);
17894 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17896 DECL_ATTRIBUTES (decl_parm) = attributes;
17897 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17899 decl_parm = DECL_CHAIN (decl_parm);
17902 /* Merge additional specifiers from the CODE_PATTERN. */
17903 if (DECL_DECLARED_INLINE_P (code_pattern)
17904 && !DECL_DECLARED_INLINE_P (decl))
17905 DECL_DECLARED_INLINE_P (decl) = 1;
17907 else if (TREE_CODE (decl) == VAR_DECL)
17909 DECL_INITIAL (decl) =
17910 tsubst_expr (DECL_INITIAL (code_pattern), args,
17911 tf_error, DECL_TI_TEMPLATE (decl),
17912 /*integral_constant_expression_p=*/false);
17913 if (VAR_HAD_UNKNOWN_BOUND (decl))
17914 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
17915 tf_error, DECL_TI_TEMPLATE (decl));
17917 else
17918 gcc_unreachable ();
17920 pop_access_scope (decl);
17923 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17924 substituted to get DECL. */
17926 tree
17927 template_for_substitution (tree decl)
17929 tree tmpl = DECL_TI_TEMPLATE (decl);
17931 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17932 for the instantiation. This is not always the most general
17933 template. Consider, for example:
17935 template <class T>
17936 struct S { template <class U> void f();
17937 template <> void f<int>(); };
17939 and an instantiation of S<double>::f<int>. We want TD to be the
17940 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17941 while (/* An instantiation cannot have a definition, so we need a
17942 more general template. */
17943 DECL_TEMPLATE_INSTANTIATION (tmpl)
17944 /* We must also deal with friend templates. Given:
17946 template <class T> struct S {
17947 template <class U> friend void f() {};
17950 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17951 so far as the language is concerned, but that's still
17952 where we get the pattern for the instantiation from. On
17953 other hand, if the definition comes outside the class, say:
17955 template <class T> struct S {
17956 template <class U> friend void f();
17958 template <class U> friend void f() {}
17960 we don't need to look any further. That's what the check for
17961 DECL_INITIAL is for. */
17962 || (TREE_CODE (decl) == FUNCTION_DECL
17963 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
17964 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
17966 /* The present template, TD, should not be a definition. If it
17967 were a definition, we should be using it! Note that we
17968 cannot restructure the loop to just keep going until we find
17969 a template with a definition, since that might go too far if
17970 a specialization was declared, but not defined. */
17971 gcc_assert (TREE_CODE (decl) != VAR_DECL
17972 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
17974 /* Fetch the more general template. */
17975 tmpl = DECL_TI_TEMPLATE (tmpl);
17978 return tmpl;
17981 /* Returns true if we need to instantiate this template instance even if we
17982 know we aren't going to emit it.. */
17984 bool
17985 always_instantiate_p (tree decl)
17987 /* We always instantiate inline functions so that we can inline them. An
17988 explicit instantiation declaration prohibits implicit instantiation of
17989 non-inline functions. With high levels of optimization, we would
17990 normally inline non-inline functions -- but we're not allowed to do
17991 that for "extern template" functions. Therefore, we check
17992 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17993 return ((TREE_CODE (decl) == FUNCTION_DECL
17994 && (DECL_DECLARED_INLINE_P (decl)
17995 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
17996 /* And we need to instantiate static data members so that
17997 their initializers are available in integral constant
17998 expressions. */
17999 || (TREE_CODE (decl) == VAR_DECL
18000 && decl_maybe_constant_var_p (decl)));
18003 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18004 instantiate it now, modifying TREE_TYPE (fn). */
18006 void
18007 maybe_instantiate_noexcept (tree fn)
18009 tree fntype, spec, noex, clone;
18011 if (DECL_CLONED_FUNCTION_P (fn))
18012 fn = DECL_CLONED_FUNCTION (fn);
18013 fntype = TREE_TYPE (fn);
18014 spec = TYPE_RAISES_EXCEPTIONS (fntype);
18016 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
18017 return;
18019 noex = TREE_PURPOSE (spec);
18021 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
18023 if (push_tinst_level (fn))
18025 push_access_scope (fn);
18026 input_location = DECL_SOURCE_LOCATION (fn);
18027 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
18028 DEFERRED_NOEXCEPT_ARGS (noex),
18029 tf_warning_or_error, fn,
18030 /*function_p=*/false,
18031 /*integral_constant_expression_p=*/true);
18032 pop_access_scope (fn);
18033 pop_tinst_level ();
18034 spec = build_noexcept_spec (noex, tf_warning_or_error);
18035 if (spec == error_mark_node)
18036 spec = noexcept_false_spec;
18038 else
18039 spec = noexcept_false_spec;
18041 else
18043 /* This is an implicitly declared function, so NOEX is a list of
18044 other functions to evaluate and merge. */
18045 tree elt;
18046 spec = noexcept_true_spec;
18047 for (elt = noex; elt; elt = OVL_NEXT (elt))
18049 tree fn = OVL_CURRENT (elt);
18050 tree subspec;
18051 maybe_instantiate_noexcept (fn);
18052 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
18053 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
18057 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
18059 FOR_EACH_CLONE (clone, fn)
18061 if (TREE_TYPE (clone) == fntype)
18062 TREE_TYPE (clone) = TREE_TYPE (fn);
18063 else
18064 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
18068 /* Produce the definition of D, a _DECL generated from a template. If
18069 DEFER_OK is nonzero, then we don't have to actually do the
18070 instantiation now; we just have to do it sometime. Normally it is
18071 an error if this is an explicit instantiation but D is undefined.
18072 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18073 explicitly instantiated class template. */
18075 tree
18076 instantiate_decl (tree d, int defer_ok,
18077 bool expl_inst_class_mem_p)
18079 tree tmpl = DECL_TI_TEMPLATE (d);
18080 tree gen_args;
18081 tree args;
18082 tree td;
18083 tree code_pattern;
18084 tree spec;
18085 tree gen_tmpl;
18086 bool pattern_defined;
18087 location_t saved_loc = input_location;
18088 bool external_p;
18089 tree fn_context;
18090 bool nested;
18092 /* This function should only be used to instantiate templates for
18093 functions and static member variables. */
18094 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
18095 || TREE_CODE (d) == VAR_DECL);
18097 /* Variables are never deferred; if instantiation is required, they
18098 are instantiated right away. That allows for better code in the
18099 case that an expression refers to the value of the variable --
18100 if the variable has a constant value the referring expression can
18101 take advantage of that fact. */
18102 if (TREE_CODE (d) == VAR_DECL
18103 || DECL_DECLARED_CONSTEXPR_P (d))
18104 defer_ok = 0;
18106 /* Don't instantiate cloned functions. Instead, instantiate the
18107 functions they cloned. */
18108 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
18109 d = DECL_CLONED_FUNCTION (d);
18111 if (DECL_TEMPLATE_INSTANTIATED (d)
18112 || (TREE_CODE (d) == FUNCTION_DECL
18113 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
18114 || DECL_TEMPLATE_SPECIALIZATION (d))
18115 /* D has already been instantiated or explicitly specialized, so
18116 there's nothing for us to do here.
18118 It might seem reasonable to check whether or not D is an explicit
18119 instantiation, and, if so, stop here. But when an explicit
18120 instantiation is deferred until the end of the compilation,
18121 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18122 the instantiation. */
18123 return d;
18125 /* Check to see whether we know that this template will be
18126 instantiated in some other file, as with "extern template"
18127 extension. */
18128 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
18130 /* In general, we do not instantiate such templates. */
18131 if (external_p && !always_instantiate_p (d))
18132 return d;
18134 gen_tmpl = most_general_template (tmpl);
18135 gen_args = DECL_TI_ARGS (d);
18137 if (tmpl != gen_tmpl)
18138 /* We should already have the extra args. */
18139 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
18140 == TMPL_ARGS_DEPTH (gen_args));
18141 /* And what's in the hash table should match D. */
18142 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
18143 || spec == NULL_TREE);
18145 /* This needs to happen before any tsubsting. */
18146 if (! push_tinst_level (d))
18147 return d;
18149 timevar_push (TV_TEMPLATE_INST);
18151 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18152 for the instantiation. */
18153 td = template_for_substitution (d);
18154 code_pattern = DECL_TEMPLATE_RESULT (td);
18156 /* We should never be trying to instantiate a member of a class
18157 template or partial specialization. */
18158 gcc_assert (d != code_pattern);
18160 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
18161 || DECL_TEMPLATE_SPECIALIZATION (td))
18162 /* In the case of a friend template whose definition is provided
18163 outside the class, we may have too many arguments. Drop the
18164 ones we don't need. The same is true for specializations. */
18165 args = get_innermost_template_args
18166 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
18167 else
18168 args = gen_args;
18170 if (TREE_CODE (d) == FUNCTION_DECL)
18171 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
18172 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
18173 else
18174 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
18176 /* We may be in the middle of deferred access check. Disable it now. */
18177 push_deferring_access_checks (dk_no_deferred);
18179 /* Unless an explicit instantiation directive has already determined
18180 the linkage of D, remember that a definition is available for
18181 this entity. */
18182 if (pattern_defined
18183 && !DECL_INTERFACE_KNOWN (d)
18184 && !DECL_NOT_REALLY_EXTERN (d))
18185 mark_definable (d);
18187 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
18188 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
18189 input_location = DECL_SOURCE_LOCATION (d);
18191 /* If D is a member of an explicitly instantiated class template,
18192 and no definition is available, treat it like an implicit
18193 instantiation. */
18194 if (!pattern_defined && expl_inst_class_mem_p
18195 && DECL_EXPLICIT_INSTANTIATION (d))
18197 /* Leave linkage flags alone on instantiations with anonymous
18198 visibility. */
18199 if (TREE_PUBLIC (d))
18201 DECL_NOT_REALLY_EXTERN (d) = 0;
18202 DECL_INTERFACE_KNOWN (d) = 0;
18204 SET_DECL_IMPLICIT_INSTANTIATION (d);
18207 if (TREE_CODE (d) == FUNCTION_DECL)
18208 maybe_instantiate_noexcept (d);
18210 /* Recheck the substitutions to obtain any warning messages
18211 about ignoring cv qualifiers. Don't do this for artificial decls,
18212 as it breaks the context-sensitive substitution for lambda op(). */
18213 if (!defer_ok && !DECL_ARTIFICIAL (d))
18215 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
18216 tree type = TREE_TYPE (gen);
18218 /* Make sure that we can see identifiers, and compute access
18219 correctly. D is already the target FUNCTION_DECL with the
18220 right context. */
18221 push_access_scope (d);
18223 if (TREE_CODE (gen) == FUNCTION_DECL)
18225 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
18226 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
18227 d, /*defer_ok*/true);
18228 /* Don't simply tsubst the function type, as that will give
18229 duplicate warnings about poor parameter qualifications.
18230 The function arguments are the same as the decl_arguments
18231 without the top level cv qualifiers. */
18232 type = TREE_TYPE (type);
18234 tsubst (type, gen_args, tf_warning_or_error, d);
18236 pop_access_scope (d);
18239 /* Defer all other templates, unless we have been explicitly
18240 forbidden from doing so. */
18241 if (/* If there is no definition, we cannot instantiate the
18242 template. */
18243 ! pattern_defined
18244 /* If it's OK to postpone instantiation, do so. */
18245 || defer_ok
18246 /* If this is a static data member that will be defined
18247 elsewhere, we don't want to instantiate the entire data
18248 member, but we do want to instantiate the initializer so that
18249 we can substitute that elsewhere. */
18250 || (external_p && TREE_CODE (d) == VAR_DECL))
18252 /* The definition of the static data member is now required so
18253 we must substitute the initializer. */
18254 if (TREE_CODE (d) == VAR_DECL
18255 && !DECL_INITIAL (d)
18256 && DECL_INITIAL (code_pattern))
18258 tree ns;
18259 tree init;
18260 bool const_init = false;
18262 ns = decl_namespace_context (d);
18263 push_nested_namespace (ns);
18264 push_nested_class (DECL_CONTEXT (d));
18265 init = tsubst_expr (DECL_INITIAL (code_pattern),
18266 args,
18267 tf_warning_or_error, NULL_TREE,
18268 /*integral_constant_expression_p=*/false);
18269 /* Make sure the initializer is still constant, in case of
18270 circular dependency (template/instantiate6.C). */
18271 const_init
18272 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18273 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18274 /*asmspec_tree=*/NULL_TREE,
18275 LOOKUP_ONLYCONVERTING);
18276 pop_nested_class ();
18277 pop_nested_namespace (ns);
18280 /* We restore the source position here because it's used by
18281 add_pending_template. */
18282 input_location = saved_loc;
18284 if (at_eof && !pattern_defined
18285 && DECL_EXPLICIT_INSTANTIATION (d)
18286 && DECL_NOT_REALLY_EXTERN (d))
18287 /* [temp.explicit]
18289 The definition of a non-exported function template, a
18290 non-exported member function template, or a non-exported
18291 member function or static data member of a class template
18292 shall be present in every translation unit in which it is
18293 explicitly instantiated. */
18294 permerror (input_location, "explicit instantiation of %qD "
18295 "but no definition available", d);
18297 /* If we're in unevaluated context, we just wanted to get the
18298 constant value; this isn't an odr use, so don't queue
18299 a full instantiation. */
18300 if (cp_unevaluated_operand != 0)
18301 goto out;
18302 /* ??? Historically, we have instantiated inline functions, even
18303 when marked as "extern template". */
18304 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18305 add_pending_template (d);
18306 goto out;
18308 /* Tell the repository that D is available in this translation unit
18309 -- and see if it is supposed to be instantiated here. */
18310 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18312 /* In a PCH file, despite the fact that the repository hasn't
18313 requested instantiation in the PCH it is still possible that
18314 an instantiation will be required in a file that includes the
18315 PCH. */
18316 if (pch_file)
18317 add_pending_template (d);
18318 /* Instantiate inline functions so that the inliner can do its
18319 job, even though we'll not be emitting a copy of this
18320 function. */
18321 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18322 goto out;
18325 fn_context = decl_function_context (d);
18326 nested = (current_function_decl != NULL_TREE);
18327 if (!fn_context)
18328 push_to_top_level ();
18329 else if (nested)
18330 push_function_context ();
18332 /* Mark D as instantiated so that recursive calls to
18333 instantiate_decl do not try to instantiate it again. */
18334 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18336 /* Regenerate the declaration in case the template has been modified
18337 by a subsequent redeclaration. */
18338 regenerate_decl_from_template (d, td);
18340 /* We already set the file and line above. Reset them now in case
18341 they changed as a result of calling regenerate_decl_from_template. */
18342 input_location = DECL_SOURCE_LOCATION (d);
18344 if (TREE_CODE (d) == VAR_DECL)
18346 tree init;
18347 bool const_init = false;
18349 /* Clear out DECL_RTL; whatever was there before may not be right
18350 since we've reset the type of the declaration. */
18351 SET_DECL_RTL (d, NULL);
18352 DECL_IN_AGGR_P (d) = 0;
18354 /* The initializer is placed in DECL_INITIAL by
18355 regenerate_decl_from_template so we don't need to
18356 push/pop_access_scope again here. Pull it out so that
18357 cp_finish_decl can process it. */
18358 init = DECL_INITIAL (d);
18359 DECL_INITIAL (d) = NULL_TREE;
18360 DECL_INITIALIZED_P (d) = 0;
18362 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18363 initializer. That function will defer actual emission until
18364 we have a chance to determine linkage. */
18365 DECL_EXTERNAL (d) = 0;
18367 /* Enter the scope of D so that access-checking works correctly. */
18368 push_nested_class (DECL_CONTEXT (d));
18369 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18370 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18371 pop_nested_class ();
18373 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18374 synthesize_method (d);
18375 else if (TREE_CODE (d) == FUNCTION_DECL)
18377 struct pointer_map_t *saved_local_specializations;
18378 tree subst_decl;
18379 tree tmpl_parm;
18380 tree spec_parm;
18382 /* Save away the current list, in case we are instantiating one
18383 template from within the body of another. */
18384 saved_local_specializations = local_specializations;
18386 /* Set up the list of local specializations. */
18387 local_specializations = pointer_map_create ();
18389 /* Set up context. */
18390 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18392 /* Some typedefs referenced from within the template code need to be
18393 access checked at template instantiation time, i.e now. These
18394 types were added to the template at parsing time. Let's get those
18395 and perform the access checks then. */
18396 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
18397 gen_args);
18399 /* Create substitution entries for the parameters. */
18400 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18401 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18402 spec_parm = DECL_ARGUMENTS (d);
18403 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18405 register_local_specialization (spec_parm, tmpl_parm);
18406 spec_parm = skip_artificial_parms_for (d, spec_parm);
18407 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18409 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18411 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18413 register_local_specialization (spec_parm, tmpl_parm);
18414 spec_parm = DECL_CHAIN (spec_parm);
18416 else
18418 /* Register the (value) argument pack as a specialization of
18419 TMPL_PARM, then move on. */
18420 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18421 register_local_specialization (argpack, tmpl_parm);
18424 gcc_assert (!spec_parm);
18426 /* Substitute into the body of the function. */
18427 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18428 tf_warning_or_error, tmpl,
18429 /*integral_constant_expression_p=*/false);
18431 /* Set the current input_location to the end of the function
18432 so that finish_function knows where we are. */
18433 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18435 /* We don't need the local specializations any more. */
18436 pointer_map_destroy (local_specializations);
18437 local_specializations = saved_local_specializations;
18439 /* Finish the function. */
18440 d = finish_function (0);
18441 expand_or_defer_fn (d);
18444 /* We're not deferring instantiation any more. */
18445 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18447 if (!fn_context)
18448 pop_from_top_level ();
18449 else if (nested)
18450 pop_function_context ();
18452 out:
18453 input_location = saved_loc;
18454 pop_deferring_access_checks ();
18455 pop_tinst_level ();
18457 timevar_pop (TV_TEMPLATE_INST);
18459 return d;
18462 /* Run through the list of templates that we wish we could
18463 instantiate, and instantiate any we can. RETRIES is the
18464 number of times we retry pending template instantiation. */
18466 void
18467 instantiate_pending_templates (int retries)
18469 int reconsider;
18470 location_t saved_loc = input_location;
18472 /* Instantiating templates may trigger vtable generation. This in turn
18473 may require further template instantiations. We place a limit here
18474 to avoid infinite loop. */
18475 if (pending_templates && retries >= max_tinst_depth)
18477 tree decl = pending_templates->tinst->decl;
18479 error ("template instantiation depth exceeds maximum of %d"
18480 " instantiating %q+D, possibly from virtual table generation"
18481 " (use -ftemplate-depth= to increase the maximum)",
18482 max_tinst_depth, decl);
18483 if (TREE_CODE (decl) == FUNCTION_DECL)
18484 /* Pretend that we defined it. */
18485 DECL_INITIAL (decl) = error_mark_node;
18486 return;
18491 struct pending_template **t = &pending_templates;
18492 struct pending_template *last = NULL;
18493 reconsider = 0;
18494 while (*t)
18496 tree instantiation = reopen_tinst_level ((*t)->tinst);
18497 bool complete = false;
18499 if (TYPE_P (instantiation))
18501 tree fn;
18503 if (!COMPLETE_TYPE_P (instantiation))
18505 instantiate_class_template (instantiation);
18506 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18507 for (fn = TYPE_METHODS (instantiation);
18509 fn = TREE_CHAIN (fn))
18510 if (! DECL_ARTIFICIAL (fn))
18511 instantiate_decl (fn,
18512 /*defer_ok=*/0,
18513 /*expl_inst_class_mem_p=*/false);
18514 if (COMPLETE_TYPE_P (instantiation))
18515 reconsider = 1;
18518 complete = COMPLETE_TYPE_P (instantiation);
18520 else
18522 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
18523 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
18525 instantiation
18526 = instantiate_decl (instantiation,
18527 /*defer_ok=*/0,
18528 /*expl_inst_class_mem_p=*/false);
18529 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
18530 reconsider = 1;
18533 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
18534 || DECL_TEMPLATE_INSTANTIATED (instantiation));
18537 if (complete)
18538 /* If INSTANTIATION has been instantiated, then we don't
18539 need to consider it again in the future. */
18540 *t = (*t)->next;
18541 else
18543 last = *t;
18544 t = &(*t)->next;
18546 tinst_depth = 0;
18547 current_tinst_level = NULL;
18549 last_pending_template = last;
18551 while (reconsider);
18553 input_location = saved_loc;
18556 /* Substitute ARGVEC into T, which is a list of initializers for
18557 either base class or a non-static data member. The TREE_PURPOSEs
18558 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18559 instantiate_decl. */
18561 static tree
18562 tsubst_initializer_list (tree t, tree argvec)
18564 tree inits = NULL_TREE;
18566 for (; t; t = TREE_CHAIN (t))
18568 tree decl;
18569 tree init;
18570 tree expanded_bases = NULL_TREE;
18571 tree expanded_arguments = NULL_TREE;
18572 int i, len = 1;
18574 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
18576 tree expr;
18577 tree arg;
18579 /* Expand the base class expansion type into separate base
18580 classes. */
18581 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
18582 tf_warning_or_error,
18583 NULL_TREE);
18584 if (expanded_bases == error_mark_node)
18585 continue;
18587 /* We'll be building separate TREE_LISTs of arguments for
18588 each base. */
18589 len = TREE_VEC_LENGTH (expanded_bases);
18590 expanded_arguments = make_tree_vec (len);
18591 for (i = 0; i < len; i++)
18592 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
18594 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18595 expand each argument in the TREE_VALUE of t. */
18596 expr = make_node (EXPR_PACK_EXPANSION);
18597 PACK_EXPANSION_LOCAL_P (expr) = true;
18598 PACK_EXPANSION_PARAMETER_PACKS (expr) =
18599 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
18601 if (TREE_VALUE (t) == void_type_node)
18602 /* VOID_TYPE_NODE is used to indicate
18603 value-initialization. */
18605 for (i = 0; i < len; i++)
18606 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
18608 else
18610 /* Substitute parameter packs into each argument in the
18611 TREE_LIST. */
18612 in_base_initializer = 1;
18613 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
18615 tree expanded_exprs;
18617 /* Expand the argument. */
18618 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
18619 expanded_exprs
18620 = tsubst_pack_expansion (expr, argvec,
18621 tf_warning_or_error,
18622 NULL_TREE);
18623 if (expanded_exprs == error_mark_node)
18624 continue;
18626 /* Prepend each of the expanded expressions to the
18627 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18628 for (i = 0; i < len; i++)
18630 TREE_VEC_ELT (expanded_arguments, i) =
18631 tree_cons (NULL_TREE,
18632 TREE_VEC_ELT (expanded_exprs, i),
18633 TREE_VEC_ELT (expanded_arguments, i));
18636 in_base_initializer = 0;
18638 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18639 since we built them backwards. */
18640 for (i = 0; i < len; i++)
18642 TREE_VEC_ELT (expanded_arguments, i) =
18643 nreverse (TREE_VEC_ELT (expanded_arguments, i));
18648 for (i = 0; i < len; ++i)
18650 if (expanded_bases)
18652 decl = TREE_VEC_ELT (expanded_bases, i);
18653 decl = expand_member_init (decl);
18654 init = TREE_VEC_ELT (expanded_arguments, i);
18656 else
18658 tree tmp;
18659 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
18660 tf_warning_or_error, NULL_TREE);
18662 decl = expand_member_init (decl);
18663 if (decl && !DECL_P (decl))
18664 in_base_initializer = 1;
18666 init = TREE_VALUE (t);
18667 tmp = init;
18668 if (init != void_type_node)
18669 init = tsubst_expr (init, argvec,
18670 tf_warning_or_error, NULL_TREE,
18671 /*integral_constant_expression_p=*/false);
18672 if (init == NULL_TREE && tmp != NULL_TREE)
18673 /* If we had an initializer but it instantiated to nothing,
18674 value-initialize the object. This will only occur when
18675 the initializer was a pack expansion where the parameter
18676 packs used in that expansion were of length zero. */
18677 init = void_type_node;
18678 in_base_initializer = 0;
18681 if (decl)
18683 init = build_tree_list (decl, init);
18684 TREE_CHAIN (init) = inits;
18685 inits = init;
18689 return inits;
18692 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18694 static void
18695 set_current_access_from_decl (tree decl)
18697 if (TREE_PRIVATE (decl))
18698 current_access_specifier = access_private_node;
18699 else if (TREE_PROTECTED (decl))
18700 current_access_specifier = access_protected_node;
18701 else
18702 current_access_specifier = access_public_node;
18705 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18706 is the instantiation (which should have been created with
18707 start_enum) and ARGS are the template arguments to use. */
18709 static void
18710 tsubst_enum (tree tag, tree newtag, tree args)
18712 tree e;
18714 if (SCOPED_ENUM_P (newtag))
18715 begin_scope (sk_scoped_enum, newtag);
18717 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
18719 tree value;
18720 tree decl;
18722 decl = TREE_VALUE (e);
18723 /* Note that in a template enum, the TREE_VALUE is the
18724 CONST_DECL, not the corresponding INTEGER_CST. */
18725 value = tsubst_expr (DECL_INITIAL (decl),
18726 args, tf_warning_or_error, NULL_TREE,
18727 /*integral_constant_expression_p=*/true);
18729 /* Give this enumeration constant the correct access. */
18730 set_current_access_from_decl (decl);
18732 /* Actually build the enumerator itself. */
18733 build_enumerator
18734 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
18737 if (SCOPED_ENUM_P (newtag))
18738 finish_scope ();
18740 finish_enum_value_list (newtag);
18741 finish_enum (newtag);
18743 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
18744 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
18747 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18748 its type -- but without substituting the innermost set of template
18749 arguments. So, innermost set of template parameters will appear in
18750 the type. */
18752 tree
18753 get_mostly_instantiated_function_type (tree decl)
18755 tree fn_type;
18756 tree tmpl;
18757 tree targs;
18758 tree tparms;
18759 int parm_depth;
18761 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
18762 targs = DECL_TI_ARGS (decl);
18763 tparms = DECL_TEMPLATE_PARMS (tmpl);
18764 parm_depth = TMPL_PARMS_DEPTH (tparms);
18766 /* There should be as many levels of arguments as there are levels
18767 of parameters. */
18768 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
18770 fn_type = TREE_TYPE (tmpl);
18772 if (parm_depth == 1)
18773 /* No substitution is necessary. */
18775 else
18777 int i;
18778 tree partial_args;
18780 /* Replace the innermost level of the TARGS with NULL_TREEs to
18781 let tsubst know not to substitute for those parameters. */
18782 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
18783 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
18784 SET_TMPL_ARGS_LEVEL (partial_args, i,
18785 TMPL_ARGS_LEVEL (targs, i));
18786 SET_TMPL_ARGS_LEVEL (partial_args,
18787 TMPL_ARGS_DEPTH (targs),
18788 make_tree_vec (DECL_NTPARMS (tmpl)));
18790 /* Make sure that we can see identifiers, and compute access
18791 correctly. */
18792 push_access_scope (decl);
18794 ++processing_template_decl;
18795 /* Now, do the (partial) substitution to figure out the
18796 appropriate function type. */
18797 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
18798 --processing_template_decl;
18800 /* Substitute into the template parameters to obtain the real
18801 innermost set of parameters. This step is important if the
18802 innermost set of template parameters contains value
18803 parameters whose types depend on outer template parameters. */
18804 TREE_VEC_LENGTH (partial_args)--;
18805 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
18807 pop_access_scope (decl);
18810 return fn_type;
18813 /* Return truthvalue if we're processing a template different from
18814 the last one involved in diagnostics. */
18816 problematic_instantiation_changed (void)
18818 return current_tinst_level != last_error_tinst_level;
18821 /* Remember current template involved in diagnostics. */
18822 void
18823 record_last_problematic_instantiation (void)
18825 last_error_tinst_level = current_tinst_level;
18828 struct tinst_level *
18829 current_instantiation (void)
18831 return current_tinst_level;
18834 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18835 type. Return zero for ok, nonzero for disallowed. Issue error and
18836 warning messages under control of COMPLAIN. */
18838 static int
18839 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
18841 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
18842 return 0;
18843 else if (POINTER_TYPE_P (type))
18844 return 0;
18845 else if (TYPE_PTRMEM_P (type))
18846 return 0;
18847 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18848 return 0;
18849 else if (TREE_CODE (type) == TYPENAME_TYPE)
18850 return 0;
18851 else if (TREE_CODE (type) == DECLTYPE_TYPE)
18852 return 0;
18853 else if (TREE_CODE (type) == NULLPTR_TYPE)
18854 return 0;
18856 if (complain & tf_error)
18858 if (type == error_mark_node)
18859 inform (input_location, "invalid template non-type parameter");
18860 else
18861 error ("%q#T is not a valid type for a template non-type parameter",
18862 type);
18864 return 1;
18867 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18868 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18870 static bool
18871 dependent_type_p_r (tree type)
18873 tree scope;
18875 /* [temp.dep.type]
18877 A type is dependent if it is:
18879 -- a template parameter. Template template parameters are types
18880 for us (since TYPE_P holds true for them) so we handle
18881 them here. */
18882 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18883 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
18884 return true;
18885 /* -- a qualified-id with a nested-name-specifier which contains a
18886 class-name that names a dependent type or whose unqualified-id
18887 names a dependent type. */
18888 if (TREE_CODE (type) == TYPENAME_TYPE)
18889 return true;
18890 /* -- a cv-qualified type where the cv-unqualified type is
18891 dependent. */
18892 type = TYPE_MAIN_VARIANT (type);
18893 /* -- a compound type constructed from any dependent type. */
18894 if (TYPE_PTRMEM_P (type))
18895 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
18896 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
18897 (type)));
18898 else if (TREE_CODE (type) == POINTER_TYPE
18899 || TREE_CODE (type) == REFERENCE_TYPE)
18900 return dependent_type_p (TREE_TYPE (type));
18901 else if (TREE_CODE (type) == FUNCTION_TYPE
18902 || TREE_CODE (type) == METHOD_TYPE)
18904 tree arg_type;
18906 if (dependent_type_p (TREE_TYPE (type)))
18907 return true;
18908 for (arg_type = TYPE_ARG_TYPES (type);
18909 arg_type;
18910 arg_type = TREE_CHAIN (arg_type))
18911 if (dependent_type_p (TREE_VALUE (arg_type)))
18912 return true;
18913 return false;
18915 /* -- an array type constructed from any dependent type or whose
18916 size is specified by a constant expression that is
18917 value-dependent.
18919 We checked for type- and value-dependence of the bounds in
18920 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
18921 if (TREE_CODE (type) == ARRAY_TYPE)
18923 if (TYPE_DOMAIN (type)
18924 && dependent_type_p (TYPE_DOMAIN (type)))
18925 return true;
18926 return dependent_type_p (TREE_TYPE (type));
18929 /* -- a template-id in which either the template name is a template
18930 parameter ... */
18931 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18932 return true;
18933 /* ... or any of the template arguments is a dependent type or
18934 an expression that is type-dependent or value-dependent. */
18935 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
18936 && (any_dependent_template_arguments_p
18937 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
18938 return true;
18940 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
18941 dependent; if the argument of the `typeof' expression is not
18942 type-dependent, then it should already been have resolved. */
18943 if (TREE_CODE (type) == TYPEOF_TYPE
18944 || TREE_CODE (type) == DECLTYPE_TYPE
18945 || TREE_CODE (type) == UNDERLYING_TYPE)
18946 return true;
18948 /* A template argument pack is dependent if any of its packed
18949 arguments are. */
18950 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
18952 tree args = ARGUMENT_PACK_ARGS (type);
18953 int i, len = TREE_VEC_LENGTH (args);
18954 for (i = 0; i < len; ++i)
18955 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18956 return true;
18959 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
18960 be template parameters. */
18961 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
18962 return true;
18964 /* The standard does not specifically mention types that are local
18965 to template functions or local classes, but they should be
18966 considered dependent too. For example:
18968 template <int I> void f() {
18969 enum E { a = I };
18970 S<sizeof (E)> s;
18973 The size of `E' cannot be known until the value of `I' has been
18974 determined. Therefore, `E' must be considered dependent. */
18975 scope = TYPE_CONTEXT (type);
18976 if (scope && TYPE_P (scope))
18977 return dependent_type_p (scope);
18978 /* Don't use type_dependent_expression_p here, as it can lead
18979 to infinite recursion trying to determine whether a lambda
18980 nested in a lambda is dependent (c++/47687). */
18981 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
18982 && DECL_LANG_SPECIFIC (scope)
18983 && DECL_TEMPLATE_INFO (scope)
18984 && (any_dependent_template_arguments_p
18985 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
18986 return true;
18988 /* Other types are non-dependent. */
18989 return false;
18992 /* Returns TRUE if TYPE is dependent, in the sense of
18993 [temp.dep.type]. Note that a NULL type is considered dependent. */
18995 bool
18996 dependent_type_p (tree type)
18998 /* If there are no template parameters in scope, then there can't be
18999 any dependent types. */
19000 if (!processing_template_decl)
19002 /* If we are not processing a template, then nobody should be
19003 providing us with a dependent type. */
19004 gcc_assert (type);
19005 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
19006 return false;
19009 /* If the type is NULL, we have not computed a type for the entity
19010 in question; in that case, the type is dependent. */
19011 if (!type)
19012 return true;
19014 /* Erroneous types can be considered non-dependent. */
19015 if (type == error_mark_node)
19016 return false;
19018 /* If we have not already computed the appropriate value for TYPE,
19019 do so now. */
19020 if (!TYPE_DEPENDENT_P_VALID (type))
19022 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
19023 TYPE_DEPENDENT_P_VALID (type) = 1;
19026 return TYPE_DEPENDENT_P (type);
19029 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19030 lookup. In other words, a dependent type that is not the current
19031 instantiation. */
19033 bool
19034 dependent_scope_p (tree scope)
19036 return (scope && TYPE_P (scope) && dependent_type_p (scope)
19037 && !currently_open_class (scope));
19040 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19041 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19042 expression. */
19044 /* Note that this predicate is not appropriate for general expressions;
19045 only constant expressions (that satisfy potential_constant_expression)
19046 can be tested for value dependence.
19048 We should really also have a predicate for "instantiation-dependent".
19050 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
19051 (what about instantiation-dependent constant-expressions?)
19052 is_late_template_attribute: defer if instantiation-dependent.
19053 compute_array_index_type: proceed if constant and not t- or v-dependent
19054 if instantiation-dependent, need to remember full expression
19055 uses_template_parms: FIXME - need to audit callers
19056 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
19057 dependent_type_p [array_type]: dependent if index type is dependent
19058 (or non-constant?)
19059 static_assert - instantiation-dependent */
19061 bool
19062 value_dependent_expression_p (tree expression)
19064 if (!processing_template_decl)
19065 return false;
19067 /* A name declared with a dependent type. */
19068 if (DECL_P (expression) && type_dependent_expression_p (expression))
19069 return true;
19071 switch (TREE_CODE (expression))
19073 case IDENTIFIER_NODE:
19074 /* A name that has not been looked up -- must be dependent. */
19075 return true;
19077 case TEMPLATE_PARM_INDEX:
19078 /* A non-type template parm. */
19079 return true;
19081 case CONST_DECL:
19082 /* A non-type template parm. */
19083 if (DECL_TEMPLATE_PARM_P (expression))
19084 return true;
19085 return value_dependent_expression_p (DECL_INITIAL (expression));
19087 case VAR_DECL:
19088 /* A constant with literal type and is initialized
19089 with an expression that is value-dependent. */
19090 if (DECL_INITIAL (expression)
19091 && decl_constant_var_p (expression)
19092 && value_dependent_expression_p (DECL_INITIAL (expression)))
19093 return true;
19094 return false;
19096 case DYNAMIC_CAST_EXPR:
19097 case STATIC_CAST_EXPR:
19098 case CONST_CAST_EXPR:
19099 case REINTERPRET_CAST_EXPR:
19100 case CAST_EXPR:
19101 /* These expressions are value-dependent if the type to which
19102 the cast occurs is dependent or the expression being casted
19103 is value-dependent. */
19105 tree type = TREE_TYPE (expression);
19107 if (dependent_type_p (type))
19108 return true;
19110 /* A functional cast has a list of operands. */
19111 expression = TREE_OPERAND (expression, 0);
19112 if (!expression)
19114 /* If there are no operands, it must be an expression such
19115 as "int()". This should not happen for aggregate types
19116 because it would form non-constant expressions. */
19117 gcc_assert (cxx_dialect >= cxx0x
19118 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
19120 return false;
19123 if (TREE_CODE (expression) == TREE_LIST)
19124 return any_value_dependent_elements_p (expression);
19126 return value_dependent_expression_p (expression);
19129 case SIZEOF_EXPR:
19130 case ALIGNOF_EXPR:
19131 case TYPEID_EXPR:
19132 /* A `sizeof' expression is value-dependent if the operand is
19133 type-dependent or is a pack expansion. */
19134 expression = TREE_OPERAND (expression, 0);
19135 if (PACK_EXPANSION_P (expression))
19136 return true;
19137 else if (TYPE_P (expression))
19138 return dependent_type_p (expression);
19139 return type_dependent_expression_p (expression);
19141 case AT_ENCODE_EXPR:
19142 /* An 'encode' expression is value-dependent if the operand is
19143 type-dependent. */
19144 expression = TREE_OPERAND (expression, 0);
19145 return dependent_type_p (expression);
19147 case NOEXCEPT_EXPR:
19148 expression = TREE_OPERAND (expression, 0);
19149 return type_dependent_expression_p (expression);
19151 case SCOPE_REF:
19153 tree name = TREE_OPERAND (expression, 1);
19154 return value_dependent_expression_p (name);
19157 case COMPONENT_REF:
19158 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
19159 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
19161 case NONTYPE_ARGUMENT_PACK:
19162 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19163 is value-dependent. */
19165 tree values = ARGUMENT_PACK_ARGS (expression);
19166 int i, len = TREE_VEC_LENGTH (values);
19168 for (i = 0; i < len; ++i)
19169 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
19170 return true;
19172 return false;
19175 case TRAIT_EXPR:
19177 tree type2 = TRAIT_EXPR_TYPE2 (expression);
19178 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
19179 || (type2 ? dependent_type_p (type2) : false));
19182 case MODOP_EXPR:
19183 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19184 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
19186 case ARRAY_REF:
19187 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19188 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
19190 case ADDR_EXPR:
19192 tree op = TREE_OPERAND (expression, 0);
19193 return (value_dependent_expression_p (op)
19194 || has_value_dependent_address (op));
19197 case CALL_EXPR:
19199 tree fn = get_callee_fndecl (expression);
19200 int i, nargs;
19201 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
19202 return true;
19203 nargs = call_expr_nargs (expression);
19204 for (i = 0; i < nargs; ++i)
19206 tree op = CALL_EXPR_ARG (expression, i);
19207 /* In a call to a constexpr member function, look through the
19208 implicit ADDR_EXPR on the object argument so that it doesn't
19209 cause the call to be considered value-dependent. We also
19210 look through it in potential_constant_expression. */
19211 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
19212 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19213 && TREE_CODE (op) == ADDR_EXPR)
19214 op = TREE_OPERAND (op, 0);
19215 if (value_dependent_expression_p (op))
19216 return true;
19218 return false;
19221 case TEMPLATE_ID_EXPR:
19222 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19223 type-dependent. */
19224 return type_dependent_expression_p (expression);
19226 case CONSTRUCTOR:
19228 unsigned ix;
19229 tree val;
19230 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
19231 if (value_dependent_expression_p (val))
19232 return true;
19233 return false;
19236 case STMT_EXPR:
19237 /* Treat a GNU statement expression as dependent to avoid crashing
19238 under fold_non_dependent_expr; it can't be constant. */
19239 return true;
19241 default:
19242 /* A constant expression is value-dependent if any subexpression is
19243 value-dependent. */
19244 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
19246 case tcc_reference:
19247 case tcc_unary:
19248 case tcc_comparison:
19249 case tcc_binary:
19250 case tcc_expression:
19251 case tcc_vl_exp:
19253 int i, len = cp_tree_operand_length (expression);
19255 for (i = 0; i < len; i++)
19257 tree t = TREE_OPERAND (expression, i);
19259 /* In some cases, some of the operands may be missing.l
19260 (For example, in the case of PREDECREMENT_EXPR, the
19261 amount to increment by may be missing.) That doesn't
19262 make the expression dependent. */
19263 if (t && value_dependent_expression_p (t))
19264 return true;
19267 break;
19268 default:
19269 break;
19271 break;
19274 /* The expression is not value-dependent. */
19275 return false;
19278 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19279 [temp.dep.expr]. Note that an expression with no type is
19280 considered dependent. Other parts of the compiler arrange for an
19281 expression with type-dependent subexpressions to have no type, so
19282 this function doesn't have to be fully recursive. */
19284 bool
19285 type_dependent_expression_p (tree expression)
19287 if (!processing_template_decl)
19288 return false;
19290 if (expression == error_mark_node)
19291 return false;
19293 /* An unresolved name is always dependent. */
19294 if (TREE_CODE (expression) == IDENTIFIER_NODE
19295 || TREE_CODE (expression) == USING_DECL)
19296 return true;
19298 /* Some expression forms are never type-dependent. */
19299 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19300 || TREE_CODE (expression) == SIZEOF_EXPR
19301 || TREE_CODE (expression) == ALIGNOF_EXPR
19302 || TREE_CODE (expression) == AT_ENCODE_EXPR
19303 || TREE_CODE (expression) == NOEXCEPT_EXPR
19304 || TREE_CODE (expression) == TRAIT_EXPR
19305 || TREE_CODE (expression) == TYPEID_EXPR
19306 || TREE_CODE (expression) == DELETE_EXPR
19307 || TREE_CODE (expression) == VEC_DELETE_EXPR
19308 || TREE_CODE (expression) == THROW_EXPR)
19309 return false;
19311 /* The types of these expressions depends only on the type to which
19312 the cast occurs. */
19313 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19314 || TREE_CODE (expression) == STATIC_CAST_EXPR
19315 || TREE_CODE (expression) == CONST_CAST_EXPR
19316 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19317 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
19318 || TREE_CODE (expression) == CAST_EXPR)
19319 return dependent_type_p (TREE_TYPE (expression));
19321 /* The types of these expressions depends only on the type created
19322 by the expression. */
19323 if (TREE_CODE (expression) == NEW_EXPR
19324 || TREE_CODE (expression) == VEC_NEW_EXPR)
19326 /* For NEW_EXPR tree nodes created inside a template, either
19327 the object type itself or a TREE_LIST may appear as the
19328 operand 1. */
19329 tree type = TREE_OPERAND (expression, 1);
19330 if (TREE_CODE (type) == TREE_LIST)
19331 /* This is an array type. We need to check array dimensions
19332 as well. */
19333 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19334 || value_dependent_expression_p
19335 (TREE_OPERAND (TREE_VALUE (type), 1));
19336 else
19337 return dependent_type_p (type);
19340 if (TREE_CODE (expression) == SCOPE_REF)
19342 tree scope = TREE_OPERAND (expression, 0);
19343 tree name = TREE_OPERAND (expression, 1);
19345 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19346 contains an identifier associated by name lookup with one or more
19347 declarations declared with a dependent type, or...a
19348 nested-name-specifier or qualified-id that names a member of an
19349 unknown specialization. */
19350 return (type_dependent_expression_p (name)
19351 || dependent_scope_p (scope));
19354 if (TREE_CODE (expression) == FUNCTION_DECL
19355 && DECL_LANG_SPECIFIC (expression)
19356 && DECL_TEMPLATE_INFO (expression)
19357 && (any_dependent_template_arguments_p
19358 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19359 return true;
19361 if (TREE_CODE (expression) == TEMPLATE_DECL
19362 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19363 return false;
19365 if (TREE_CODE (expression) == STMT_EXPR)
19366 expression = stmt_expr_value_expr (expression);
19368 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19370 tree elt;
19371 unsigned i;
19373 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19375 if (type_dependent_expression_p (elt))
19376 return true;
19378 return false;
19381 /* A static data member of the current instantiation with incomplete
19382 array type is type-dependent, as the definition and specializations
19383 can have different bounds. */
19384 if (TREE_CODE (expression) == VAR_DECL
19385 && DECL_CLASS_SCOPE_P (expression)
19386 && dependent_type_p (DECL_CONTEXT (expression))
19387 && VAR_HAD_UNKNOWN_BOUND (expression))
19388 return true;
19390 if (TREE_TYPE (expression) == unknown_type_node)
19392 if (TREE_CODE (expression) == ADDR_EXPR)
19393 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19394 if (TREE_CODE (expression) == COMPONENT_REF
19395 || TREE_CODE (expression) == OFFSET_REF)
19397 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19398 return true;
19399 expression = TREE_OPERAND (expression, 1);
19400 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19401 return false;
19403 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19404 if (TREE_CODE (expression) == SCOPE_REF)
19405 return false;
19407 if (BASELINK_P (expression))
19408 expression = BASELINK_FUNCTIONS (expression);
19410 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19412 if (any_dependent_template_arguments_p
19413 (TREE_OPERAND (expression, 1)))
19414 return true;
19415 expression = TREE_OPERAND (expression, 0);
19417 gcc_assert (TREE_CODE (expression) == OVERLOAD
19418 || TREE_CODE (expression) == FUNCTION_DECL);
19420 while (expression)
19422 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19423 return true;
19424 expression = OVL_NEXT (expression);
19426 return false;
19429 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19431 return (dependent_type_p (TREE_TYPE (expression)));
19434 /* Like type_dependent_expression_p, but it also works while not processing
19435 a template definition, i.e. during substitution or mangling. */
19437 bool
19438 type_dependent_expression_p_push (tree expr)
19440 bool b;
19441 ++processing_template_decl;
19442 b = type_dependent_expression_p (expr);
19443 --processing_template_decl;
19444 return b;
19447 /* Returns TRUE if ARGS contains a type-dependent expression. */
19449 bool
19450 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
19452 unsigned int i;
19453 tree arg;
19455 FOR_EACH_VEC_ELT (tree, args, i, arg)
19457 if (type_dependent_expression_p (arg))
19458 return true;
19460 return false;
19463 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19464 expressions) contains any type-dependent expressions. */
19466 bool
19467 any_type_dependent_elements_p (const_tree list)
19469 for (; list; list = TREE_CHAIN (list))
19470 if (value_dependent_expression_p (TREE_VALUE (list)))
19471 return true;
19473 return false;
19476 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19477 expressions) contains any value-dependent expressions. */
19479 bool
19480 any_value_dependent_elements_p (const_tree list)
19482 for (; list; list = TREE_CHAIN (list))
19483 if (value_dependent_expression_p (TREE_VALUE (list)))
19484 return true;
19486 return false;
19489 /* Returns TRUE if the ARG (a template argument) is dependent. */
19491 bool
19492 dependent_template_arg_p (tree arg)
19494 if (!processing_template_decl)
19495 return false;
19497 /* Assume a template argument that was wrongly written by the user
19498 is dependent. This is consistent with what
19499 any_dependent_template_arguments_p [that calls this function]
19500 does. */
19501 if (!arg || arg == error_mark_node)
19502 return true;
19504 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
19505 arg = ARGUMENT_PACK_SELECT_ARG (arg);
19507 if (TREE_CODE (arg) == TEMPLATE_DECL
19508 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19509 return dependent_template_p (arg);
19510 else if (ARGUMENT_PACK_P (arg))
19512 tree args = ARGUMENT_PACK_ARGS (arg);
19513 int i, len = TREE_VEC_LENGTH (args);
19514 for (i = 0; i < len; ++i)
19516 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19517 return true;
19520 return false;
19522 else if (TYPE_P (arg))
19523 return dependent_type_p (arg);
19524 else
19525 return (type_dependent_expression_p (arg)
19526 || value_dependent_expression_p (arg));
19529 /* Returns true if ARGS (a collection of template arguments) contains
19530 any types that require structural equality testing. */
19532 bool
19533 any_template_arguments_need_structural_equality_p (tree args)
19535 int i;
19536 int j;
19538 if (!args)
19539 return false;
19540 if (args == error_mark_node)
19541 return true;
19543 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19545 tree level = TMPL_ARGS_LEVEL (args, i + 1);
19546 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19548 tree arg = TREE_VEC_ELT (level, j);
19549 tree packed_args = NULL_TREE;
19550 int k, len = 1;
19552 if (ARGUMENT_PACK_P (arg))
19554 /* Look inside the argument pack. */
19555 packed_args = ARGUMENT_PACK_ARGS (arg);
19556 len = TREE_VEC_LENGTH (packed_args);
19559 for (k = 0; k < len; ++k)
19561 if (packed_args)
19562 arg = TREE_VEC_ELT (packed_args, k);
19564 if (error_operand_p (arg))
19565 return true;
19566 else if (TREE_CODE (arg) == TEMPLATE_DECL
19567 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19568 continue;
19569 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
19570 return true;
19571 else if (!TYPE_P (arg) && TREE_TYPE (arg)
19572 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
19573 return true;
19578 return false;
19581 /* Returns true if ARGS (a collection of template arguments) contains
19582 any dependent arguments. */
19584 bool
19585 any_dependent_template_arguments_p (const_tree args)
19587 int i;
19588 int j;
19590 if (!args)
19591 return false;
19592 if (args == error_mark_node)
19593 return true;
19595 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19597 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
19598 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19599 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
19600 return true;
19603 return false;
19606 /* Returns TRUE if the template TMPL is dependent. */
19608 bool
19609 dependent_template_p (tree tmpl)
19611 if (TREE_CODE (tmpl) == OVERLOAD)
19613 while (tmpl)
19615 if (dependent_template_p (OVL_CURRENT (tmpl)))
19616 return true;
19617 tmpl = OVL_NEXT (tmpl);
19619 return false;
19622 /* Template template parameters are dependent. */
19623 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
19624 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
19625 return true;
19626 /* So are names that have not been looked up. */
19627 if (TREE_CODE (tmpl) == SCOPE_REF
19628 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
19629 return true;
19630 /* So are member templates of dependent classes. */
19631 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
19632 return dependent_type_p (DECL_CONTEXT (tmpl));
19633 return false;
19636 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19638 bool
19639 dependent_template_id_p (tree tmpl, tree args)
19641 return (dependent_template_p (tmpl)
19642 || any_dependent_template_arguments_p (args));
19645 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19646 is dependent. */
19648 bool
19649 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
19651 int i;
19653 if (!processing_template_decl)
19654 return false;
19656 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
19658 tree decl = TREE_VEC_ELT (declv, i);
19659 tree init = TREE_VEC_ELT (initv, i);
19660 tree cond = TREE_VEC_ELT (condv, i);
19661 tree incr = TREE_VEC_ELT (incrv, i);
19663 if (type_dependent_expression_p (decl))
19664 return true;
19666 if (init && type_dependent_expression_p (init))
19667 return true;
19669 if (type_dependent_expression_p (cond))
19670 return true;
19672 if (COMPARISON_CLASS_P (cond)
19673 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
19674 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
19675 return true;
19677 if (TREE_CODE (incr) == MODOP_EXPR)
19679 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
19680 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
19681 return true;
19683 else if (type_dependent_expression_p (incr))
19684 return true;
19685 else if (TREE_CODE (incr) == MODIFY_EXPR)
19687 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
19688 return true;
19689 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
19691 tree t = TREE_OPERAND (incr, 1);
19692 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
19693 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
19694 return true;
19699 return false;
19702 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19703 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19704 no such TYPE can be found. Note that this function peers inside
19705 uninstantiated templates and therefore should be used only in
19706 extremely limited situations. ONLY_CURRENT_P restricts this
19707 peering to the currently open classes hierarchy (which is required
19708 when comparing types). */
19710 tree
19711 resolve_typename_type (tree type, bool only_current_p)
19713 tree scope;
19714 tree name;
19715 tree decl;
19716 int quals;
19717 tree pushed_scope;
19718 tree result;
19720 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
19722 scope = TYPE_CONTEXT (type);
19723 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19724 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19725 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19726 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19727 identifier of the TYPENAME_TYPE anymore.
19728 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19729 TYPENAME_TYPE instead, we avoid messing up with a possible
19730 typedef variant case. */
19731 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
19733 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19734 it first before we can figure out what NAME refers to. */
19735 if (TREE_CODE (scope) == TYPENAME_TYPE)
19736 scope = resolve_typename_type (scope, only_current_p);
19737 /* If we don't know what SCOPE refers to, then we cannot resolve the
19738 TYPENAME_TYPE. */
19739 if (TREE_CODE (scope) == TYPENAME_TYPE)
19740 return type;
19741 /* If the SCOPE is a template type parameter, we have no way of
19742 resolving the name. */
19743 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
19744 return type;
19745 /* If the SCOPE is not the current instantiation, there's no reason
19746 to look inside it. */
19747 if (only_current_p && !currently_open_class (scope))
19748 return type;
19749 /* If this is a typedef, we don't want to look inside (c++/11987). */
19750 if (typedef_variant_p (type))
19751 return type;
19752 /* If SCOPE isn't the template itself, it will not have a valid
19753 TYPE_FIELDS list. */
19754 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
19755 /* scope is either the template itself or a compatible instantiation
19756 like X<T>, so look up the name in the original template. */
19757 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
19758 else
19759 /* scope is a partial instantiation, so we can't do the lookup or we
19760 will lose the template arguments. */
19761 return type;
19762 /* Enter the SCOPE so that name lookup will be resolved as if we
19763 were in the class definition. In particular, SCOPE will no
19764 longer be considered a dependent type. */
19765 pushed_scope = push_scope (scope);
19766 /* Look up the declaration. */
19767 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
19768 tf_warning_or_error);
19770 result = NULL_TREE;
19772 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19773 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19774 if (!decl)
19775 /*nop*/;
19776 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
19777 && TREE_CODE (decl) == TYPE_DECL)
19779 result = TREE_TYPE (decl);
19780 if (result == error_mark_node)
19781 result = NULL_TREE;
19783 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
19784 && DECL_CLASS_TEMPLATE_P (decl))
19786 tree tmpl;
19787 tree args;
19788 /* Obtain the template and the arguments. */
19789 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
19790 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
19791 /* Instantiate the template. */
19792 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
19793 /*entering_scope=*/0,
19794 tf_error | tf_user);
19795 if (result == error_mark_node)
19796 result = NULL_TREE;
19799 /* Leave the SCOPE. */
19800 if (pushed_scope)
19801 pop_scope (pushed_scope);
19803 /* If we failed to resolve it, return the original typename. */
19804 if (!result)
19805 return type;
19807 /* If lookup found a typename type, resolve that too. */
19808 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
19810 /* Ill-formed programs can cause infinite recursion here, so we
19811 must catch that. */
19812 TYPENAME_IS_RESOLVING_P (type) = 1;
19813 result = resolve_typename_type (result, only_current_p);
19814 TYPENAME_IS_RESOLVING_P (type) = 0;
19817 /* Qualify the resulting type. */
19818 quals = cp_type_quals (type);
19819 if (quals)
19820 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
19822 return result;
19825 /* EXPR is an expression which is not type-dependent. Return a proxy
19826 for EXPR that can be used to compute the types of larger
19827 expressions containing EXPR. */
19829 tree
19830 build_non_dependent_expr (tree expr)
19832 tree inner_expr;
19834 #ifdef ENABLE_CHECKING
19835 /* Try to get a constant value for all non-type-dependent expressions in
19836 order to expose bugs in *_dependent_expression_p and constexpr. */
19837 if (cxx_dialect >= cxx0x)
19838 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
19839 #endif
19841 /* Preserve OVERLOADs; the functions must be available to resolve
19842 types. */
19843 inner_expr = expr;
19844 if (TREE_CODE (inner_expr) == STMT_EXPR)
19845 inner_expr = stmt_expr_value_expr (inner_expr);
19846 if (TREE_CODE (inner_expr) == ADDR_EXPR)
19847 inner_expr = TREE_OPERAND (inner_expr, 0);
19848 if (TREE_CODE (inner_expr) == COMPONENT_REF)
19849 inner_expr = TREE_OPERAND (inner_expr, 1);
19850 if (is_overloaded_fn (inner_expr)
19851 || TREE_CODE (inner_expr) == OFFSET_REF)
19852 return expr;
19853 /* There is no need to return a proxy for a variable. */
19854 if (TREE_CODE (expr) == VAR_DECL)
19855 return expr;
19856 /* Preserve string constants; conversions from string constants to
19857 "char *" are allowed, even though normally a "const char *"
19858 cannot be used to initialize a "char *". */
19859 if (TREE_CODE (expr) == STRING_CST)
19860 return expr;
19861 /* Preserve arithmetic constants, as an optimization -- there is no
19862 reason to create a new node. */
19863 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
19864 return expr;
19865 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
19866 There is at least one place where we want to know that a
19867 particular expression is a throw-expression: when checking a ?:
19868 expression, there are special rules if the second or third
19869 argument is a throw-expression. */
19870 if (TREE_CODE (expr) == THROW_EXPR)
19871 return expr;
19873 /* Don't wrap an initializer list, we need to be able to look inside. */
19874 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
19875 return expr;
19877 /* Don't wrap a dummy object, we need to be able to test for it. */
19878 if (is_dummy_object (expr))
19879 return expr;
19881 if (TREE_CODE (expr) == COND_EXPR)
19882 return build3 (COND_EXPR,
19883 TREE_TYPE (expr),
19884 TREE_OPERAND (expr, 0),
19885 (TREE_OPERAND (expr, 1)
19886 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
19887 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
19888 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
19889 if (TREE_CODE (expr) == COMPOUND_EXPR
19890 && !COMPOUND_EXPR_OVERLOADED (expr))
19891 return build2 (COMPOUND_EXPR,
19892 TREE_TYPE (expr),
19893 TREE_OPERAND (expr, 0),
19894 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
19896 /* If the type is unknown, it can't really be non-dependent */
19897 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
19899 /* Otherwise, build a NON_DEPENDENT_EXPR. */
19900 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
19903 /* ARGS is a vector of expressions as arguments to a function call.
19904 Replace the arguments with equivalent non-dependent expressions.
19905 This modifies ARGS in place. */
19907 void
19908 make_args_non_dependent (VEC(tree,gc) *args)
19910 unsigned int ix;
19911 tree arg;
19913 FOR_EACH_VEC_ELT (tree, args, ix, arg)
19915 tree newarg = build_non_dependent_expr (arg);
19916 if (newarg != arg)
19917 VEC_replace (tree, args, ix, newarg);
19921 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
19922 with a level one deeper than the actual template parms. */
19924 tree
19925 make_auto (void)
19927 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
19928 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
19929 TYPE_DECL, get_identifier ("auto"), au);
19930 TYPE_STUB_DECL (au) = TYPE_NAME (au);
19931 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
19932 (0, processing_template_decl + 1, processing_template_decl + 1,
19933 TYPE_NAME (au), NULL_TREE);
19934 TYPE_CANONICAL (au) = canonical_type_parameter (au);
19935 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
19936 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
19938 return au;
19941 /* Given type ARG, return std::initializer_list<ARG>. */
19943 static tree
19944 listify (tree arg)
19946 tree std_init_list = namespace_binding
19947 (get_identifier ("initializer_list"), std_node);
19948 tree argvec;
19949 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
19951 error ("deducing from brace-enclosed initializer list requires "
19952 "#include <initializer_list>");
19953 return error_mark_node;
19955 argvec = make_tree_vec (1);
19956 TREE_VEC_ELT (argvec, 0) = arg;
19957 return lookup_template_class (std_init_list, argvec, NULL_TREE,
19958 NULL_TREE, 0, tf_warning_or_error);
19961 /* Replace auto in TYPE with std::initializer_list<auto>. */
19963 static tree
19964 listify_autos (tree type, tree auto_node)
19966 tree init_auto = listify (auto_node);
19967 tree argvec = make_tree_vec (1);
19968 TREE_VEC_ELT (argvec, 0) = init_auto;
19969 if (processing_template_decl)
19970 argvec = add_to_template_args (current_template_args (), argvec);
19971 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
19974 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
19975 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
19977 tree
19978 do_auto_deduction (tree type, tree init, tree auto_node)
19980 tree parms, tparms, targs;
19981 tree args[1];
19982 int val;
19984 if (init == error_mark_node)
19985 return error_mark_node;
19987 if (type_dependent_expression_p (init))
19988 /* Defining a subset of type-dependent expressions that we can deduce
19989 from ahead of time isn't worth the trouble. */
19990 return type;
19992 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
19993 with either a new invented type template parameter U or, if the
19994 initializer is a braced-init-list (8.5.4), with
19995 std::initializer_list<U>. */
19996 if (BRACE_ENCLOSED_INITIALIZER_P (init))
19997 type = listify_autos (type, auto_node);
19999 init = resolve_nondeduced_context (init);
20001 parms = build_tree_list (NULL_TREE, type);
20002 args[0] = init;
20003 tparms = make_tree_vec (1);
20004 targs = make_tree_vec (1);
20005 TREE_VEC_ELT (tparms, 0)
20006 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
20007 val = type_unification_real (tparms, targs, parms, args, 1, 0,
20008 DEDUCE_CALL, LOOKUP_NORMAL,
20009 /*explain_p=*/false);
20010 if (val > 0)
20012 if (processing_template_decl)
20013 /* Try again at instantiation time. */
20014 return type;
20015 if (type && type != error_mark_node)
20016 /* If type is error_mark_node a diagnostic must have been
20017 emitted by now. Also, having a mention to '<type error>'
20018 in the diagnostic is not really useful to the user. */
20020 if (cfun && auto_node == current_function_auto_return_pattern
20021 && LAMBDA_FUNCTION_P (current_function_decl))
20022 error ("unable to deduce lambda return type from %qE", init);
20023 else
20024 error ("unable to deduce %qT from %qE", type, init);
20026 return error_mark_node;
20029 /* If the list of declarators contains more than one declarator, the type
20030 of each declared variable is determined as described above. If the
20031 type deduced for the template parameter U is not the same in each
20032 deduction, the program is ill-formed. */
20033 if (TREE_TYPE (auto_node)
20034 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
20036 if (cfun && auto_node == current_function_auto_return_pattern
20037 && LAMBDA_FUNCTION_P (current_function_decl))
20038 error ("inconsistent types %qT and %qT deduced for "
20039 "lambda return type", TREE_TYPE (auto_node),
20040 TREE_VEC_ELT (targs, 0));
20041 else
20042 error ("inconsistent deduction for %qT: %qT and then %qT",
20043 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
20044 return error_mark_node;
20046 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
20048 if (processing_template_decl)
20049 targs = add_to_template_args (current_template_args (), targs);
20050 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
20053 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20054 result. */
20056 tree
20057 splice_late_return_type (tree type, tree late_return_type)
20059 tree argvec;
20061 if (late_return_type == NULL_TREE)
20062 return type;
20063 argvec = make_tree_vec (1);
20064 TREE_VEC_ELT (argvec, 0) = late_return_type;
20065 if (processing_template_parmlist)
20066 /* For a late-specified return type in a template type-parameter, we
20067 need to add a dummy argument level for its parmlist. */
20068 argvec = add_to_template_args
20069 (make_tree_vec (processing_template_parmlist), argvec);
20070 if (current_template_parms)
20071 argvec = add_to_template_args (current_template_args (), argvec);
20072 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20075 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20077 bool
20078 is_auto (const_tree type)
20080 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20081 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
20082 return true;
20083 else
20084 return false;
20087 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20088 appear as a type-specifier for the declaration in question, we don't
20089 have to look through the whole type. */
20091 tree
20092 type_uses_auto (tree type)
20094 enum tree_code code;
20095 if (is_auto (type))
20096 return type;
20098 code = TREE_CODE (type);
20100 if (code == POINTER_TYPE || code == REFERENCE_TYPE
20101 || code == OFFSET_TYPE || code == FUNCTION_TYPE
20102 || code == METHOD_TYPE || code == ARRAY_TYPE)
20103 return type_uses_auto (TREE_TYPE (type));
20105 if (TYPE_PTRMEMFUNC_P (type))
20106 return type_uses_auto (TREE_TYPE (TREE_TYPE
20107 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
20109 return NULL_TREE;
20112 /* For a given template T, return the vector of typedefs referenced
20113 in T for which access check is needed at T instantiation time.
20114 T is either a FUNCTION_DECL or a RECORD_TYPE.
20115 Those typedefs were added to T by the function
20116 append_type_to_template_for_access_check. */
20118 VEC(qualified_typedef_usage_t,gc)*
20119 get_types_needing_access_check (tree t)
20121 tree ti;
20122 VEC(qualified_typedef_usage_t,gc) *result = NULL;
20124 if (!t || t == error_mark_node)
20125 return NULL;
20127 if (!(ti = get_template_info (t)))
20128 return NULL;
20130 if (CLASS_TYPE_P (t)
20131 || TREE_CODE (t) == FUNCTION_DECL)
20133 if (!TI_TEMPLATE (ti))
20134 return NULL;
20136 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
20139 return result;
20142 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20143 tied to T. That list of typedefs will be access checked at
20144 T instantiation time.
20145 T is either a FUNCTION_DECL or a RECORD_TYPE.
20146 TYPE_DECL is a TYPE_DECL node representing a typedef.
20147 SCOPE is the scope through which TYPE_DECL is accessed.
20148 LOCATION is the location of the usage point of TYPE_DECL.
20150 This function is a subroutine of
20151 append_type_to_template_for_access_check. */
20153 static void
20154 append_type_to_template_for_access_check_1 (tree t,
20155 tree type_decl,
20156 tree scope,
20157 location_t location)
20159 qualified_typedef_usage_t typedef_usage;
20160 tree ti;
20162 if (!t || t == error_mark_node)
20163 return;
20165 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
20166 || CLASS_TYPE_P (t))
20167 && type_decl
20168 && TREE_CODE (type_decl) == TYPE_DECL
20169 && scope);
20171 if (!(ti = get_template_info (t)))
20172 return;
20174 gcc_assert (TI_TEMPLATE (ti));
20176 typedef_usage.typedef_decl = type_decl;
20177 typedef_usage.context = scope;
20178 typedef_usage.locus = location;
20180 VEC_safe_push (qualified_typedef_usage_t, gc,
20181 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
20182 &typedef_usage);
20185 /* Append TYPE_DECL to the template TEMPL.
20186 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20187 At TEMPL instanciation time, TYPE_DECL will be checked to see
20188 if it can be accessed through SCOPE.
20189 LOCATION is the location of the usage point of TYPE_DECL.
20191 e.g. consider the following code snippet:
20193 class C
20195 typedef int myint;
20198 template<class U> struct S
20200 C::myint mi; // <-- usage point of the typedef C::myint
20203 S<char> s;
20205 At S<char> instantiation time, we need to check the access of C::myint
20206 In other words, we need to check the access of the myint typedef through
20207 the C scope. For that purpose, this function will add the myint typedef
20208 and the scope C through which its being accessed to a list of typedefs
20209 tied to the template S. That list will be walked at template instantiation
20210 time and access check performed on each typedefs it contains.
20211 Note that this particular code snippet should yield an error because
20212 myint is private to C. */
20214 void
20215 append_type_to_template_for_access_check (tree templ,
20216 tree type_decl,
20217 tree scope,
20218 location_t location)
20220 qualified_typedef_usage_t *iter;
20221 int i;
20223 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
20225 /* Make sure we don't append the type to the template twice. */
20226 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
20227 get_types_needing_access_check (templ),
20228 i, iter)
20229 if (iter->typedef_decl == type_decl && scope == iter->context)
20230 return;
20232 append_type_to_template_for_access_check_1 (templ, type_decl,
20233 scope, location);
20236 /* Set up the hash tables for template instantiations. */
20238 void
20239 init_template_processing (void)
20241 decl_specializations = htab_create_ggc (37,
20242 hash_specialization,
20243 eq_specializations,
20244 ggc_free);
20245 type_specializations = htab_create_ggc (37,
20246 hash_specialization,
20247 eq_specializations,
20248 ggc_free);
20251 /* Print stats about the template hash tables for -fstats. */
20253 void
20254 print_template_statistics (void)
20256 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
20257 "%f collisions\n", (long) htab_size (decl_specializations),
20258 (long) htab_elements (decl_specializations),
20259 htab_collisions (decl_specializations));
20260 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
20261 "%f collisions\n", (long) htab_size (type_specializations),
20262 (long) htab_elements (type_specializations),
20263 htab_collisions (type_specializations));
20266 #include "gt-cp-pt.h"