* pt.c (check_default_tmpl_args): Uses the parameter source
[official-gcc.git] / gcc / cp / pt.c
blob21d0d3aa4e9cd78f90158c4001abea745030a8ae
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "flags.h"
38 #include "cp-tree.h"
39 #include "c-family/c-common.h"
40 #include "c-family/c-objc.h"
41 #include "cp-objcp-common.h"
42 #include "tree-inline.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "timevar.h"
46 #include "tree-iterator.h"
47 #include "type-utils.h"
48 #include "gimplify.h"
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct GTY ((chain_next ("%h.next"))) pending_template {
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static GTY(()) tree saved_trees;
69 static vec<int> inline_parm_levels;
71 static GTY(()) struct tinst_level *current_tinst_level;
73 static GTY(()) tree saved_access_scope;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
80 /* True if we've recursed into fn_type_unification too many times. */
81 static bool excessive_deduction_depth;
83 struct GTY((for_user)) spec_entry
85 tree tmpl;
86 tree args;
87 tree spec;
90 struct spec_hasher : ggc_hasher<spec_entry *>
92 static hashval_t hash (spec_entry *);
93 static bool equal (spec_entry *, spec_entry *);
96 static GTY (()) hash_table<spec_hasher> *decl_specializations;
98 static GTY (()) hash_table<spec_hasher> *type_specializations;
100 /* Contains canonical template parameter types. The vector is indexed by
101 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
102 TREE_LIST, whose TREE_VALUEs contain the canonical template
103 parameters of various types and levels. */
104 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
106 #define UNIFY_ALLOW_NONE 0
107 #define UNIFY_ALLOW_MORE_CV_QUAL 1
108 #define UNIFY_ALLOW_LESS_CV_QUAL 2
109 #define UNIFY_ALLOW_DERIVED 4
110 #define UNIFY_ALLOW_INTEGER 8
111 #define UNIFY_ALLOW_OUTER_LEVEL 16
112 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
113 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
115 enum template_base_result {
116 tbr_incomplete_type,
117 tbr_ambiguous_baseclass,
118 tbr_success
121 static void push_access_scope (tree);
122 static void pop_access_scope (tree);
123 static bool resolve_overloaded_unification (tree, tree, tree, tree,
124 unification_kind_t, int,
125 bool);
126 static int try_one_overload (tree, tree, tree, tree, tree,
127 unification_kind_t, int, bool, bool);
128 static int unify (tree, tree, tree, tree, int, bool);
129 static void add_pending_template (tree);
130 static tree reopen_tinst_level (struct tinst_level *);
131 static tree tsubst_initializer_list (tree, tree);
132 static tree get_partial_spec_bindings (tree, tree, tree, tree);
133 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
134 bool, bool);
135 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
136 bool, bool);
137 static void tsubst_enum (tree, tree, tree);
138 static tree add_to_template_args (tree, tree);
139 static tree add_outermost_template_args (tree, tree);
140 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
141 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
142 tree);
143 static int type_unification_real (tree, tree, tree, const tree *,
144 unsigned int, int, unification_kind_t, int,
145 vec<deferred_access_check, va_gc> **,
146 bool);
147 static void note_template_header (int);
148 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
149 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
150 static tree convert_template_argument (tree, tree, tree,
151 tsubst_flags_t, int, tree);
152 static int for_each_template_parm (tree, tree_fn_t, void*,
153 hash_set<tree> *, bool);
154 static tree expand_template_argument_pack (tree);
155 static tree build_template_parm_index (int, int, int, tree, tree);
156 static bool inline_needs_template_parms (tree, bool);
157 static void push_inline_template_parms_recursive (tree, int);
158 static tree retrieve_local_specialization (tree);
159 static void register_local_specialization (tree, tree);
160 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
161 static int mark_template_parm (tree, void *);
162 static int template_parm_this_level_p (tree, void *);
163 static tree tsubst_friend_function (tree, tree);
164 static tree tsubst_friend_class (tree, tree);
165 static int can_complete_type_without_circularity (tree);
166 static tree get_bindings (tree, tree, tree, bool);
167 static int template_decl_level (tree);
168 static int check_cv_quals_for_unify (int, tree, tree);
169 static void template_parm_level_and_index (tree, int*, int*);
170 static int unify_pack_expansion (tree, tree, tree,
171 tree, unification_kind_t, bool, bool);
172 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
173 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
175 static void regenerate_decl_from_template (tree, tree);
176 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
177 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
178 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
179 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
180 static bool check_specialization_scope (void);
181 static tree process_partial_specialization (tree);
182 static void set_current_access_from_decl (tree);
183 static enum template_base_result get_template_base (tree, tree, tree, tree,
184 bool , tree *);
185 static tree try_class_unification (tree, tree, tree, tree, bool);
186 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
187 tree, tree);
188 static bool template_template_parm_bindings_ok_p (tree, tree);
189 static int template_args_equal (tree, tree);
190 static void tsubst_default_arguments (tree, tsubst_flags_t);
191 static tree for_each_template_parm_r (tree *, int *, void *);
192 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
193 static void copy_default_args_to_explicit_spec (tree);
194 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
195 static bool dependent_template_arg_p (tree);
196 static bool any_template_arguments_need_structural_equality_p (tree);
197 static bool dependent_type_p_r (tree);
198 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
199 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
200 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
201 static tree tsubst_decl (tree, tree, tsubst_flags_t);
202 static void perform_typedefs_access_check (tree tmpl, tree targs);
203 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
204 location_t);
205 static tree listify (tree);
206 static tree listify_autos (tree, tree);
207 static tree template_parm_to_arg (tree t);
208 static tree current_template_args (void);
209 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
210 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
212 /* Make the current scope suitable for access checking when we are
213 processing T. T can be FUNCTION_DECL for instantiated function
214 template, VAR_DECL for static member variable, or TYPE_DECL for
215 alias template (needed by instantiate_decl). */
217 static void
218 push_access_scope (tree t)
220 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
221 || TREE_CODE (t) == TYPE_DECL);
223 if (DECL_FRIEND_CONTEXT (t))
224 push_nested_class (DECL_FRIEND_CONTEXT (t));
225 else if (DECL_CLASS_SCOPE_P (t))
226 push_nested_class (DECL_CONTEXT (t));
227 else
228 push_to_top_level ();
230 if (TREE_CODE (t) == FUNCTION_DECL)
232 saved_access_scope = tree_cons
233 (NULL_TREE, current_function_decl, saved_access_scope);
234 current_function_decl = t;
238 /* Restore the scope set up by push_access_scope. T is the node we
239 are processing. */
241 static void
242 pop_access_scope (tree t)
244 if (TREE_CODE (t) == FUNCTION_DECL)
246 current_function_decl = TREE_VALUE (saved_access_scope);
247 saved_access_scope = TREE_CHAIN (saved_access_scope);
250 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
251 pop_nested_class ();
252 else
253 pop_from_top_level ();
256 /* Do any processing required when DECL (a member template
257 declaration) is finished. Returns the TEMPLATE_DECL corresponding
258 to DECL, unless it is a specialization, in which case the DECL
259 itself is returned. */
261 tree
262 finish_member_template_decl (tree decl)
264 if (decl == error_mark_node)
265 return error_mark_node;
267 gcc_assert (DECL_P (decl));
269 if (TREE_CODE (decl) == TYPE_DECL)
271 tree type;
273 type = TREE_TYPE (decl);
274 if (type == error_mark_node)
275 return error_mark_node;
276 if (MAYBE_CLASS_TYPE_P (type)
277 && CLASSTYPE_TEMPLATE_INFO (type)
278 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
280 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
281 check_member_template (tmpl);
282 return tmpl;
284 return NULL_TREE;
286 else if (TREE_CODE (decl) == FIELD_DECL)
287 error ("data member %qD cannot be a member template", decl);
288 else if (DECL_TEMPLATE_INFO (decl))
290 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
292 check_member_template (DECL_TI_TEMPLATE (decl));
293 return DECL_TI_TEMPLATE (decl);
295 else
296 return decl;
298 else
299 error ("invalid member template declaration %qD", decl);
301 return error_mark_node;
304 /* Create a template info node. */
306 tree
307 build_template_info (tree template_decl, tree template_args)
309 tree result = make_node (TEMPLATE_INFO);
310 TI_TEMPLATE (result) = template_decl;
311 TI_ARGS (result) = template_args;
312 return result;
315 /* Return the template info node corresponding to T, whatever T is. */
317 tree
318 get_template_info (const_tree t)
320 tree tinfo = NULL_TREE;
322 if (!t || t == error_mark_node)
323 return NULL;
325 if (TREE_CODE (t) == NAMESPACE_DECL)
326 return NULL;
328 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
329 tinfo = DECL_TEMPLATE_INFO (t);
331 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
332 t = TREE_TYPE (t);
334 if (OVERLOAD_TYPE_P (t))
335 tinfo = TYPE_TEMPLATE_INFO (t);
336 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
337 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
339 return tinfo;
342 /* Returns the template nesting level of the indicated class TYPE.
344 For example, in:
345 template <class T>
346 struct A
348 template <class U>
349 struct B {};
352 A<T>::B<U> has depth two, while A<T> has depth one.
353 Both A<T>::B<int> and A<int>::B<U> have depth one, if
354 they are instantiations, not specializations.
356 This function is guaranteed to return 0 if passed NULL_TREE so
357 that, for example, `template_class_depth (current_class_type)' is
358 always safe. */
361 template_class_depth (tree type)
363 int depth;
365 for (depth = 0;
366 type && TREE_CODE (type) != NAMESPACE_DECL;
367 type = (TREE_CODE (type) == FUNCTION_DECL)
368 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
370 tree tinfo = get_template_info (type);
372 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
373 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
374 ++depth;
377 return depth;
380 /* Subroutine of maybe_begin_member_template_processing.
381 Returns true if processing DECL needs us to push template parms. */
383 static bool
384 inline_needs_template_parms (tree decl, bool nsdmi)
386 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
387 return false;
389 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
390 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
393 /* Subroutine of maybe_begin_member_template_processing.
394 Push the template parms in PARMS, starting from LEVELS steps into the
395 chain, and ending at the beginning, since template parms are listed
396 innermost first. */
398 static void
399 push_inline_template_parms_recursive (tree parmlist, int levels)
401 tree parms = TREE_VALUE (parmlist);
402 int i;
404 if (levels > 1)
405 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
407 ++processing_template_decl;
408 current_template_parms
409 = tree_cons (size_int (processing_template_decl),
410 parms, current_template_parms);
411 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
413 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
414 NULL);
415 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
417 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
419 if (error_operand_p (parm))
420 continue;
422 gcc_assert (DECL_P (parm));
424 switch (TREE_CODE (parm))
426 case TYPE_DECL:
427 case TEMPLATE_DECL:
428 pushdecl (parm);
429 break;
431 case PARM_DECL:
433 /* Make a CONST_DECL as is done in process_template_parm.
434 It is ugly that we recreate this here; the original
435 version built in process_template_parm is no longer
436 available. */
437 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
438 CONST_DECL, DECL_NAME (parm),
439 TREE_TYPE (parm));
440 DECL_ARTIFICIAL (decl) = 1;
441 TREE_CONSTANT (decl) = 1;
442 TREE_READONLY (decl) = 1;
443 DECL_INITIAL (decl) = DECL_INITIAL (parm);
444 SET_DECL_TEMPLATE_PARM_P (decl);
445 pushdecl (decl);
447 break;
449 default:
450 gcc_unreachable ();
455 /* Restore the template parameter context for a member template, a
456 friend template defined in a class definition, or a non-template
457 member of template class. */
459 void
460 maybe_begin_member_template_processing (tree decl)
462 tree parms;
463 int levels = 0;
464 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
466 if (nsdmi)
468 tree ctx = DECL_CONTEXT (decl);
469 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
470 /* Disregard full specializations (c++/60999). */
471 && uses_template_parms (ctx)
472 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
475 if (inline_needs_template_parms (decl, nsdmi))
477 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
478 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
480 if (DECL_TEMPLATE_SPECIALIZATION (decl))
482 --levels;
483 parms = TREE_CHAIN (parms);
486 push_inline_template_parms_recursive (parms, levels);
489 /* Remember how many levels of template parameters we pushed so that
490 we can pop them later. */
491 inline_parm_levels.safe_push (levels);
494 /* Undo the effects of maybe_begin_member_template_processing. */
496 void
497 maybe_end_member_template_processing (void)
499 int i;
500 int last;
502 if (inline_parm_levels.length () == 0)
503 return;
505 last = inline_parm_levels.pop ();
506 for (i = 0; i < last; ++i)
508 --processing_template_decl;
509 current_template_parms = TREE_CHAIN (current_template_parms);
510 poplevel (0, 0, 0);
514 /* Return a new template argument vector which contains all of ARGS,
515 but has as its innermost set of arguments the EXTRA_ARGS. */
517 static tree
518 add_to_template_args (tree args, tree extra_args)
520 tree new_args;
521 int extra_depth;
522 int i;
523 int j;
525 if (args == NULL_TREE || extra_args == error_mark_node)
526 return extra_args;
528 extra_depth = TMPL_ARGS_DEPTH (extra_args);
529 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
531 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
532 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
534 for (j = 1; j <= extra_depth; ++j, ++i)
535 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
537 return new_args;
540 /* Like add_to_template_args, but only the outermost ARGS are added to
541 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
542 (EXTRA_ARGS) levels are added. This function is used to combine
543 the template arguments from a partial instantiation with the
544 template arguments used to attain the full instantiation from the
545 partial instantiation. */
547 static tree
548 add_outermost_template_args (tree args, tree extra_args)
550 tree new_args;
552 /* If there are more levels of EXTRA_ARGS than there are ARGS,
553 something very fishy is going on. */
554 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
556 /* If *all* the new arguments will be the EXTRA_ARGS, just return
557 them. */
558 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
559 return extra_args;
561 /* For the moment, we make ARGS look like it contains fewer levels. */
562 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
564 new_args = add_to_template_args (args, extra_args);
566 /* Now, we restore ARGS to its full dimensions. */
567 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
569 return new_args;
572 /* Return the N levels of innermost template arguments from the ARGS. */
574 tree
575 get_innermost_template_args (tree args, int n)
577 tree new_args;
578 int extra_levels;
579 int i;
581 gcc_assert (n >= 0);
583 /* If N is 1, just return the innermost set of template arguments. */
584 if (n == 1)
585 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
587 /* If we're not removing anything, just return the arguments we were
588 given. */
589 extra_levels = TMPL_ARGS_DEPTH (args) - n;
590 gcc_assert (extra_levels >= 0);
591 if (extra_levels == 0)
592 return args;
594 /* Make a new set of arguments, not containing the outer arguments. */
595 new_args = make_tree_vec (n);
596 for (i = 1; i <= n; ++i)
597 SET_TMPL_ARGS_LEVEL (new_args, i,
598 TMPL_ARGS_LEVEL (args, i + extra_levels));
600 return new_args;
603 /* The inverse of get_innermost_template_args: Return all but the innermost
604 EXTRA_LEVELS levels of template arguments from the ARGS. */
606 static tree
607 strip_innermost_template_args (tree args, int extra_levels)
609 tree new_args;
610 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
611 int i;
613 gcc_assert (n >= 0);
615 /* If N is 1, just return the outermost set of template arguments. */
616 if (n == 1)
617 return TMPL_ARGS_LEVEL (args, 1);
619 /* If we're not removing anything, just return the arguments we were
620 given. */
621 gcc_assert (extra_levels >= 0);
622 if (extra_levels == 0)
623 return args;
625 /* Make a new set of arguments, not containing the inner arguments. */
626 new_args = make_tree_vec (n);
627 for (i = 1; i <= n; ++i)
628 SET_TMPL_ARGS_LEVEL (new_args, i,
629 TMPL_ARGS_LEVEL (args, i));
631 return new_args;
634 /* We've got a template header coming up; push to a new level for storing
635 the parms. */
637 void
638 begin_template_parm_list (void)
640 /* We use a non-tag-transparent scope here, which causes pushtag to
641 put tags in this scope, rather than in the enclosing class or
642 namespace scope. This is the right thing, since we want
643 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
644 global template class, push_template_decl handles putting the
645 TEMPLATE_DECL into top-level scope. For a nested template class,
646 e.g.:
648 template <class T> struct S1 {
649 template <class T> struct S2 {};
652 pushtag contains special code to call pushdecl_with_scope on the
653 TEMPLATE_DECL for S2. */
654 begin_scope (sk_template_parms, NULL);
655 ++processing_template_decl;
656 ++processing_template_parmlist;
657 note_template_header (0);
660 /* This routine is called when a specialization is declared. If it is
661 invalid to declare a specialization here, an error is reported and
662 false is returned, otherwise this routine will return true. */
664 static bool
665 check_specialization_scope (void)
667 tree scope = current_scope ();
669 /* [temp.expl.spec]
671 An explicit specialization shall be declared in the namespace of
672 which the template is a member, or, for member templates, in the
673 namespace of which the enclosing class or enclosing class
674 template is a member. An explicit specialization of a member
675 function, member class or static data member of a class template
676 shall be declared in the namespace of which the class template
677 is a member. */
678 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
680 error ("explicit specialization in non-namespace scope %qD", scope);
681 return false;
684 /* [temp.expl.spec]
686 In an explicit specialization declaration for a member of a class
687 template or a member template that appears in namespace scope,
688 the member template and some of its enclosing class templates may
689 remain unspecialized, except that the declaration shall not
690 explicitly specialize a class member template if its enclosing
691 class templates are not explicitly specialized as well. */
692 if (current_template_parms)
694 error ("enclosing class templates are not explicitly specialized");
695 return false;
698 return true;
701 /* We've just seen template <>. */
703 bool
704 begin_specialization (void)
706 begin_scope (sk_template_spec, NULL);
707 note_template_header (1);
708 return check_specialization_scope ();
711 /* Called at then end of processing a declaration preceded by
712 template<>. */
714 void
715 end_specialization (void)
717 finish_scope ();
718 reset_specialization ();
721 /* Any template <>'s that we have seen thus far are not referring to a
722 function specialization. */
724 void
725 reset_specialization (void)
727 processing_specialization = 0;
728 template_header_count = 0;
731 /* We've just seen a template header. If SPECIALIZATION is nonzero,
732 it was of the form template <>. */
734 static void
735 note_template_header (int specialization)
737 processing_specialization = specialization;
738 template_header_count++;
741 /* We're beginning an explicit instantiation. */
743 void
744 begin_explicit_instantiation (void)
746 gcc_assert (!processing_explicit_instantiation);
747 processing_explicit_instantiation = true;
751 void
752 end_explicit_instantiation (void)
754 gcc_assert (processing_explicit_instantiation);
755 processing_explicit_instantiation = false;
758 /* An explicit specialization or partial specialization of TMPL is being
759 declared. Check that the namespace in which the specialization is
760 occurring is permissible. Returns false iff it is invalid to
761 specialize TMPL in the current namespace. */
763 static bool
764 check_specialization_namespace (tree tmpl)
766 tree tpl_ns = decl_namespace_context (tmpl);
768 /* [tmpl.expl.spec]
770 An explicit specialization shall be declared in the namespace of
771 which the template is a member, or, for member templates, in the
772 namespace of which the enclosing class or enclosing class
773 template is a member. An explicit specialization of a member
774 function, member class or static data member of a class template
775 shall be declared in the namespace of which the class template is
776 a member. */
777 if (current_scope() != DECL_CONTEXT (tmpl)
778 && !at_namespace_scope_p ())
780 error ("specialization of %qD must appear at namespace scope", tmpl);
781 return false;
783 if (is_associated_namespace (current_namespace, tpl_ns))
784 /* Same or super-using namespace. */
785 return true;
786 else
788 permerror (input_location, "specialization of %qD in different namespace", tmpl);
789 permerror (input_location, " from definition of %q+#D", tmpl);
790 return false;
794 /* SPEC is an explicit instantiation. Check that it is valid to
795 perform this explicit instantiation in the current namespace. */
797 static void
798 check_explicit_instantiation_namespace (tree spec)
800 tree ns;
802 /* DR 275: An explicit instantiation shall appear in an enclosing
803 namespace of its template. */
804 ns = decl_namespace_context (spec);
805 if (!is_ancestor (current_namespace, ns))
806 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
807 "(which does not enclose namespace %qD)",
808 spec, current_namespace, ns);
811 /* The TYPE is being declared. If it is a template type, that means it
812 is a partial specialization. Do appropriate error-checking. */
814 tree
815 maybe_process_partial_specialization (tree type)
817 tree context;
819 if (type == error_mark_node)
820 return error_mark_node;
822 /* A lambda that appears in specialization context is not itself a
823 specialization. */
824 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
825 return type;
827 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
829 error ("name of class shadows template template parameter %qD",
830 TYPE_NAME (type));
831 return error_mark_node;
834 context = TYPE_CONTEXT (type);
836 if (TYPE_ALIAS_P (type))
838 if (TYPE_TEMPLATE_INFO (type)
839 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
840 error ("specialization of alias template %qD",
841 TYPE_TI_TEMPLATE (type));
842 else
843 error ("explicit specialization of non-template %qT", type);
844 return error_mark_node;
846 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
848 /* This is for ordinary explicit specialization and partial
849 specialization of a template class such as:
851 template <> class C<int>;
855 template <class T> class C<T*>;
857 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
859 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
860 && !COMPLETE_TYPE_P (type))
862 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
863 && !at_namespace_scope_p ())
864 return error_mark_node;
865 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
866 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
867 if (processing_template_decl)
869 if (push_template_decl (TYPE_MAIN_DECL (type))
870 == error_mark_node)
871 return error_mark_node;
874 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
875 error ("specialization of %qT after instantiation", type);
876 else if (errorcount && !processing_specialization
877 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
878 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
879 /* Trying to define a specialization either without a template<> header
880 or in an inappropriate place. We've already given an error, so just
881 bail now so we don't actually define the specialization. */
882 return error_mark_node;
884 else if (CLASS_TYPE_P (type)
885 && !CLASSTYPE_USE_TEMPLATE (type)
886 && CLASSTYPE_TEMPLATE_INFO (type)
887 && context && CLASS_TYPE_P (context)
888 && CLASSTYPE_TEMPLATE_INFO (context))
890 /* This is for an explicit specialization of member class
891 template according to [temp.expl.spec/18]:
893 template <> template <class U> class C<int>::D;
895 The context `C<int>' must be an implicit instantiation.
896 Otherwise this is just a member class template declared
897 earlier like:
899 template <> class C<int> { template <class U> class D; };
900 template <> template <class U> class C<int>::D;
902 In the first case, `C<int>::D' is a specialization of `C<T>::D'
903 while in the second case, `C<int>::D' is a primary template
904 and `C<T>::D' may not exist. */
906 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
907 && !COMPLETE_TYPE_P (type))
909 tree t;
910 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
912 if (current_namespace
913 != decl_namespace_context (tmpl))
915 permerror (input_location, "specializing %q#T in different namespace", type);
916 permerror (input_location, " from definition of %q+#D", tmpl);
919 /* Check for invalid specialization after instantiation:
921 template <> template <> class C<int>::D<int>;
922 template <> template <class U> class C<int>::D; */
924 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
925 t; t = TREE_CHAIN (t))
927 tree inst = TREE_VALUE (t);
928 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
929 || !COMPLETE_OR_OPEN_TYPE_P (inst))
931 /* We already have a full specialization of this partial
932 instantiation, or a full specialization has been
933 looked up but not instantiated. Reassign it to the
934 new member specialization template. */
935 spec_entry elt;
936 spec_entry *entry;
938 elt.tmpl = most_general_template (tmpl);
939 elt.args = CLASSTYPE_TI_ARGS (inst);
940 elt.spec = inst;
942 type_specializations->remove_elt (&elt);
944 elt.tmpl = tmpl;
945 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
947 spec_entry **slot
948 = type_specializations->find_slot (&elt, INSERT);
949 entry = ggc_alloc<spec_entry> ();
950 *entry = elt;
951 *slot = entry;
953 else
954 /* But if we've had an implicit instantiation, that's a
955 problem ([temp.expl.spec]/6). */
956 error ("specialization %qT after instantiation %qT",
957 type, inst);
960 /* Mark TYPE as a specialization. And as a result, we only
961 have one level of template argument for the innermost
962 class template. */
963 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
964 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
965 CLASSTYPE_TI_ARGS (type)
966 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
969 else if (processing_specialization)
971 /* Someday C++0x may allow for enum template specialization. */
972 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
973 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
974 pedwarn (input_location, OPT_Wpedantic, "template specialization "
975 "of %qD not allowed by ISO C++", type);
976 else
978 error ("explicit specialization of non-template %qT", type);
979 return error_mark_node;
983 return type;
986 /* Returns nonzero if we can optimize the retrieval of specializations
987 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
988 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
990 static inline bool
991 optimize_specialization_lookup_p (tree tmpl)
993 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
994 && DECL_CLASS_SCOPE_P (tmpl)
995 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
996 parameter. */
997 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
998 /* The optimized lookup depends on the fact that the
999 template arguments for the member function template apply
1000 purely to the containing class, which is not true if the
1001 containing class is an explicit or partial
1002 specialization. */
1003 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1004 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1005 && !DECL_CONV_FN_P (tmpl)
1006 /* It is possible to have a template that is not a member
1007 template and is not a member of a template class:
1009 template <typename T>
1010 struct S { friend A::f(); };
1012 Here, the friend function is a template, but the context does
1013 not have template information. The optimized lookup relies
1014 on having ARGS be the template arguments for both the class
1015 and the function template. */
1016 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1019 /* Retrieve the specialization (in the sense of [temp.spec] - a
1020 specialization is either an instantiation or an explicit
1021 specialization) of TMPL for the given template ARGS. If there is
1022 no such specialization, return NULL_TREE. The ARGS are a vector of
1023 arguments, or a vector of vectors of arguments, in the case of
1024 templates with more than one level of parameters.
1026 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1027 then we search for a partial specialization matching ARGS. This
1028 parameter is ignored if TMPL is not a class template.
1030 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1031 result is a NONTYPE_ARGUMENT_PACK. */
1033 static tree
1034 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1036 if (tmpl == NULL_TREE)
1037 return NULL_TREE;
1039 if (args == error_mark_node)
1040 return NULL_TREE;
1042 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1043 || TREE_CODE (tmpl) == FIELD_DECL);
1045 /* There should be as many levels of arguments as there are
1046 levels of parameters. */
1047 gcc_assert (TMPL_ARGS_DEPTH (args)
1048 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1049 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1050 : template_class_depth (DECL_CONTEXT (tmpl))));
1052 if (optimize_specialization_lookup_p (tmpl))
1054 tree class_template;
1055 tree class_specialization;
1056 vec<tree, va_gc> *methods;
1057 tree fns;
1058 int idx;
1060 /* The template arguments actually apply to the containing
1061 class. Find the class specialization with those
1062 arguments. */
1063 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1064 class_specialization
1065 = retrieve_specialization (class_template, args, 0);
1066 if (!class_specialization)
1067 return NULL_TREE;
1068 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1069 for the specialization. */
1070 idx = class_method_index_for_fn (class_specialization, tmpl);
1071 if (idx == -1)
1072 return NULL_TREE;
1073 /* Iterate through the methods with the indicated name, looking
1074 for the one that has an instance of TMPL. */
1075 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1076 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1078 tree fn = OVL_CURRENT (fns);
1079 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1080 /* using-declarations can add base methods to the method vec,
1081 and we don't want those here. */
1082 && DECL_CONTEXT (fn) == class_specialization)
1083 return fn;
1085 return NULL_TREE;
1087 else
1089 spec_entry *found;
1090 spec_entry elt;
1091 hash_table<spec_hasher> *specializations;
1093 elt.tmpl = tmpl;
1094 elt.args = args;
1095 elt.spec = NULL_TREE;
1097 if (DECL_CLASS_TEMPLATE_P (tmpl))
1098 specializations = type_specializations;
1099 else
1100 specializations = decl_specializations;
1102 if (hash == 0)
1103 hash = spec_hasher::hash (&elt);
1104 found = specializations->find_with_hash (&elt, hash);
1105 if (found)
1106 return found->spec;
1109 return NULL_TREE;
1112 /* Like retrieve_specialization, but for local declarations. */
1114 static tree
1115 retrieve_local_specialization (tree tmpl)
1117 if (local_specializations == NULL)
1118 return NULL_TREE;
1120 tree *slot = local_specializations->get (tmpl);
1121 return slot ? *slot : NULL_TREE;
1124 /* Returns nonzero iff DECL is a specialization of TMPL. */
1127 is_specialization_of (tree decl, tree tmpl)
1129 tree t;
1131 if (TREE_CODE (decl) == FUNCTION_DECL)
1133 for (t = decl;
1134 t != NULL_TREE;
1135 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1136 if (t == tmpl)
1137 return 1;
1139 else
1141 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1143 for (t = TREE_TYPE (decl);
1144 t != NULL_TREE;
1145 t = CLASSTYPE_USE_TEMPLATE (t)
1146 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1147 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1148 return 1;
1151 return 0;
1154 /* Returns nonzero iff DECL is a specialization of friend declaration
1155 FRIEND_DECL according to [temp.friend]. */
1157 bool
1158 is_specialization_of_friend (tree decl, tree friend_decl)
1160 bool need_template = true;
1161 int template_depth;
1163 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1164 || TREE_CODE (decl) == TYPE_DECL);
1166 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1167 of a template class, we want to check if DECL is a specialization
1168 if this. */
1169 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1170 && DECL_TEMPLATE_INFO (friend_decl)
1171 && !DECL_USE_TEMPLATE (friend_decl))
1173 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1174 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1175 need_template = false;
1177 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1178 && !PRIMARY_TEMPLATE_P (friend_decl))
1179 need_template = false;
1181 /* There is nothing to do if this is not a template friend. */
1182 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1183 return false;
1185 if (is_specialization_of (decl, friend_decl))
1186 return true;
1188 /* [temp.friend/6]
1189 A member of a class template may be declared to be a friend of a
1190 non-template class. In this case, the corresponding member of
1191 every specialization of the class template is a friend of the
1192 class granting friendship.
1194 For example, given a template friend declaration
1196 template <class T> friend void A<T>::f();
1198 the member function below is considered a friend
1200 template <> struct A<int> {
1201 void f();
1204 For this type of template friend, TEMPLATE_DEPTH below will be
1205 nonzero. To determine if DECL is a friend of FRIEND, we first
1206 check if the enclosing class is a specialization of another. */
1208 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1209 if (template_depth
1210 && DECL_CLASS_SCOPE_P (decl)
1211 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1212 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1214 /* Next, we check the members themselves. In order to handle
1215 a few tricky cases, such as when FRIEND_DECL's are
1217 template <class T> friend void A<T>::g(T t);
1218 template <class T> template <T t> friend void A<T>::h();
1220 and DECL's are
1222 void A<int>::g(int);
1223 template <int> void A<int>::h();
1225 we need to figure out ARGS, the template arguments from
1226 the context of DECL. This is required for template substitution
1227 of `T' in the function parameter of `g' and template parameter
1228 of `h' in the above examples. Here ARGS corresponds to `int'. */
1230 tree context = DECL_CONTEXT (decl);
1231 tree args = NULL_TREE;
1232 int current_depth = 0;
1234 while (current_depth < template_depth)
1236 if (CLASSTYPE_TEMPLATE_INFO (context))
1238 if (current_depth == 0)
1239 args = TYPE_TI_ARGS (context);
1240 else
1241 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1242 current_depth++;
1244 context = TYPE_CONTEXT (context);
1247 if (TREE_CODE (decl) == FUNCTION_DECL)
1249 bool is_template;
1250 tree friend_type;
1251 tree decl_type;
1252 tree friend_args_type;
1253 tree decl_args_type;
1255 /* Make sure that both DECL and FRIEND_DECL are templates or
1256 non-templates. */
1257 is_template = DECL_TEMPLATE_INFO (decl)
1258 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1259 if (need_template ^ is_template)
1260 return false;
1261 else if (is_template)
1263 /* If both are templates, check template parameter list. */
1264 tree friend_parms
1265 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1266 args, tf_none);
1267 if (!comp_template_parms
1268 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1269 friend_parms))
1270 return false;
1272 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1274 else
1275 decl_type = TREE_TYPE (decl);
1277 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1278 tf_none, NULL_TREE);
1279 if (friend_type == error_mark_node)
1280 return false;
1282 /* Check if return types match. */
1283 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1284 return false;
1286 /* Check if function parameter types match, ignoring the
1287 `this' parameter. */
1288 friend_args_type = TYPE_ARG_TYPES (friend_type);
1289 decl_args_type = TYPE_ARG_TYPES (decl_type);
1290 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1291 friend_args_type = TREE_CHAIN (friend_args_type);
1292 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1293 decl_args_type = TREE_CHAIN (decl_args_type);
1295 return compparms (decl_args_type, friend_args_type);
1297 else
1299 /* DECL is a TYPE_DECL */
1300 bool is_template;
1301 tree decl_type = TREE_TYPE (decl);
1303 /* Make sure that both DECL and FRIEND_DECL are templates or
1304 non-templates. */
1305 is_template
1306 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1309 if (need_template ^ is_template)
1310 return false;
1311 else if (is_template)
1313 tree friend_parms;
1314 /* If both are templates, check the name of the two
1315 TEMPLATE_DECL's first because is_friend didn't. */
1316 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1317 != DECL_NAME (friend_decl))
1318 return false;
1320 /* Now check template parameter list. */
1321 friend_parms
1322 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1323 args, tf_none);
1324 return comp_template_parms
1325 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1326 friend_parms);
1328 else
1329 return (DECL_NAME (decl)
1330 == DECL_NAME (friend_decl));
1333 return false;
1336 /* Register the specialization SPEC as a specialization of TMPL with
1337 the indicated ARGS. IS_FRIEND indicates whether the specialization
1338 is actually just a friend declaration. Returns SPEC, or an
1339 equivalent prior declaration, if available.
1341 We also store instantiations of field packs in the hash table, even
1342 though they are not themselves templates, to make lookup easier. */
1344 static tree
1345 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1346 hashval_t hash)
1348 tree fn;
1349 spec_entry **slot = NULL;
1350 spec_entry elt;
1352 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1353 || (TREE_CODE (tmpl) == FIELD_DECL
1354 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1356 if (TREE_CODE (spec) == FUNCTION_DECL
1357 && uses_template_parms (DECL_TI_ARGS (spec)))
1358 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1359 register it; we want the corresponding TEMPLATE_DECL instead.
1360 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1361 the more obvious `uses_template_parms (spec)' to avoid problems
1362 with default function arguments. In particular, given
1363 something like this:
1365 template <class T> void f(T t1, T t = T())
1367 the default argument expression is not substituted for in an
1368 instantiation unless and until it is actually needed. */
1369 return spec;
1371 if (optimize_specialization_lookup_p (tmpl))
1372 /* We don't put these specializations in the hash table, but we might
1373 want to give an error about a mismatch. */
1374 fn = retrieve_specialization (tmpl, args, 0);
1375 else
1377 elt.tmpl = tmpl;
1378 elt.args = args;
1379 elt.spec = spec;
1381 if (hash == 0)
1382 hash = spec_hasher::hash (&elt);
1384 slot =
1385 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1386 if (*slot)
1387 fn = ((spec_entry *) *slot)->spec;
1388 else
1389 fn = NULL_TREE;
1392 /* We can sometimes try to re-register a specialization that we've
1393 already got. In particular, regenerate_decl_from_template calls
1394 duplicate_decls which will update the specialization list. But,
1395 we'll still get called again here anyhow. It's more convenient
1396 to simply allow this than to try to prevent it. */
1397 if (fn == spec)
1398 return spec;
1399 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1401 if (DECL_TEMPLATE_INSTANTIATION (fn))
1403 if (DECL_ODR_USED (fn)
1404 || DECL_EXPLICIT_INSTANTIATION (fn))
1406 error ("specialization of %qD after instantiation",
1407 fn);
1408 return error_mark_node;
1410 else
1412 tree clone;
1413 /* This situation should occur only if the first
1414 specialization is an implicit instantiation, the
1415 second is an explicit specialization, and the
1416 implicit instantiation has not yet been used. That
1417 situation can occur if we have implicitly
1418 instantiated a member function and then specialized
1419 it later.
1421 We can also wind up here if a friend declaration that
1422 looked like an instantiation turns out to be a
1423 specialization:
1425 template <class T> void foo(T);
1426 class S { friend void foo<>(int) };
1427 template <> void foo(int);
1429 We transform the existing DECL in place so that any
1430 pointers to it become pointers to the updated
1431 declaration.
1433 If there was a definition for the template, but not
1434 for the specialization, we want this to look as if
1435 there were no definition, and vice versa. */
1436 DECL_INITIAL (fn) = NULL_TREE;
1437 duplicate_decls (spec, fn, is_friend);
1438 /* The call to duplicate_decls will have applied
1439 [temp.expl.spec]:
1441 An explicit specialization of a function template
1442 is inline only if it is explicitly declared to be,
1443 and independently of whether its function template
1446 to the primary function; now copy the inline bits to
1447 the various clones. */
1448 FOR_EACH_CLONE (clone, fn)
1450 DECL_DECLARED_INLINE_P (clone)
1451 = DECL_DECLARED_INLINE_P (fn);
1452 DECL_SOURCE_LOCATION (clone)
1453 = DECL_SOURCE_LOCATION (fn);
1454 DECL_DELETED_FN (clone)
1455 = DECL_DELETED_FN (fn);
1457 check_specialization_namespace (tmpl);
1459 return fn;
1462 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1464 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1465 /* Dup decl failed, but this is a new definition. Set the
1466 line number so any errors match this new
1467 definition. */
1468 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1470 return fn;
1473 else if (fn)
1474 return duplicate_decls (spec, fn, is_friend);
1476 /* A specialization must be declared in the same namespace as the
1477 template it is specializing. */
1478 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1479 && !check_specialization_namespace (tmpl))
1480 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1482 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1484 spec_entry *entry = ggc_alloc<spec_entry> ();
1485 gcc_assert (tmpl && args && spec);
1486 *entry = elt;
1487 *slot = entry;
1488 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1489 && PRIMARY_TEMPLATE_P (tmpl)
1490 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1491 || variable_template_p (tmpl))
1492 /* If TMPL is a forward declaration of a template function, keep a list
1493 of all specializations in case we need to reassign them to a friend
1494 template later in tsubst_friend_function.
1496 Also keep a list of all variable template instantiations so that
1497 process_partial_specialization can check whether a later partial
1498 specialization would have used it. */
1499 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1500 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1503 return spec;
1506 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1507 TMPL and ARGS members, ignores SPEC. */
1509 int comparing_specializations;
1511 bool
1512 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1514 int equal;
1516 ++comparing_specializations;
1517 equal = (e1->tmpl == e2->tmpl
1518 && comp_template_args (e1->args, e2->args));
1519 --comparing_specializations;
1521 return equal;
1524 /* Returns a hash for a template TMPL and template arguments ARGS. */
1526 static hashval_t
1527 hash_tmpl_and_args (tree tmpl, tree args)
1529 hashval_t val = DECL_UID (tmpl);
1530 return iterative_hash_template_arg (args, val);
1533 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1534 ignoring SPEC. */
1536 hashval_t
1537 spec_hasher::hash (spec_entry *e)
1539 return hash_tmpl_and_args (e->tmpl, e->args);
1542 /* Recursively calculate a hash value for a template argument ARG, for use
1543 in the hash tables of template specializations. */
1545 hashval_t
1546 iterative_hash_template_arg (tree arg, hashval_t val)
1548 unsigned HOST_WIDE_INT i;
1549 enum tree_code code;
1550 char tclass;
1552 if (arg == NULL_TREE)
1553 return iterative_hash_object (arg, val);
1555 if (!TYPE_P (arg))
1556 STRIP_NOPS (arg);
1558 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1559 /* We can get one of these when re-hashing a previous entry in the middle
1560 of substituting into a pack expansion. Just look through it. */
1561 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1563 code = TREE_CODE (arg);
1564 tclass = TREE_CODE_CLASS (code);
1566 val = iterative_hash_object (code, val);
1568 switch (code)
1570 case ERROR_MARK:
1571 return val;
1573 case IDENTIFIER_NODE:
1574 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1576 case TREE_VEC:
1578 int i, len = TREE_VEC_LENGTH (arg);
1579 for (i = 0; i < len; ++i)
1580 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1581 return val;
1584 case TYPE_PACK_EXPANSION:
1585 case EXPR_PACK_EXPANSION:
1586 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1587 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1589 case TYPE_ARGUMENT_PACK:
1590 case NONTYPE_ARGUMENT_PACK:
1591 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1593 case TREE_LIST:
1594 for (; arg; arg = TREE_CHAIN (arg))
1595 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1596 return val;
1598 case OVERLOAD:
1599 for (; arg; arg = OVL_NEXT (arg))
1600 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1601 return val;
1603 case CONSTRUCTOR:
1605 tree field, value;
1606 iterative_hash_template_arg (TREE_TYPE (arg), val);
1607 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1609 val = iterative_hash_template_arg (field, val);
1610 val = iterative_hash_template_arg (value, val);
1612 return val;
1615 case PARM_DECL:
1616 if (!DECL_ARTIFICIAL (arg))
1618 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1619 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1621 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1623 case TARGET_EXPR:
1624 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1626 case PTRMEM_CST:
1627 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1628 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1630 case TEMPLATE_PARM_INDEX:
1631 val = iterative_hash_template_arg
1632 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1633 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1634 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1636 case TRAIT_EXPR:
1637 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1638 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1639 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1641 case BASELINK:
1642 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1643 val);
1644 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1645 val);
1647 case MODOP_EXPR:
1648 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1649 code = TREE_CODE (TREE_OPERAND (arg, 1));
1650 val = iterative_hash_object (code, val);
1651 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1653 case LAMBDA_EXPR:
1654 /* A lambda can't appear in a template arg, but don't crash on
1655 erroneous input. */
1656 gcc_assert (seen_error ());
1657 return val;
1659 case CAST_EXPR:
1660 case IMPLICIT_CONV_EXPR:
1661 case STATIC_CAST_EXPR:
1662 case REINTERPRET_CAST_EXPR:
1663 case CONST_CAST_EXPR:
1664 case DYNAMIC_CAST_EXPR:
1665 case NEW_EXPR:
1666 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1667 /* Now hash operands as usual. */
1668 break;
1670 default:
1671 break;
1674 switch (tclass)
1676 case tcc_type:
1677 if (alias_template_specialization_p (arg))
1679 // We want an alias specialization that survived strip_typedefs
1680 // to hash differently from its TYPE_CANONICAL, to avoid hash
1681 // collisions that compare as different in template_args_equal.
1682 // These could be dependent specializations that strip_typedefs
1683 // left alone, or untouched specializations because
1684 // coerce_template_parms returns the unconverted template
1685 // arguments if it sees incomplete argument packs.
1686 tree ti = TYPE_TEMPLATE_INFO (arg);
1687 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1689 if (TYPE_CANONICAL (arg))
1690 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1691 val);
1692 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1693 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1694 /* Otherwise just compare the types during lookup. */
1695 return val;
1697 case tcc_declaration:
1698 case tcc_constant:
1699 return iterative_hash_expr (arg, val);
1701 default:
1702 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1704 unsigned n = cp_tree_operand_length (arg);
1705 for (i = 0; i < n; ++i)
1706 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1707 return val;
1710 gcc_unreachable ();
1711 return 0;
1714 /* Unregister the specialization SPEC as a specialization of TMPL.
1715 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1716 if the SPEC was listed as a specialization of TMPL.
1718 Note that SPEC has been ggc_freed, so we can't look inside it. */
1720 bool
1721 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1723 spec_entry *entry;
1724 spec_entry elt;
1726 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1727 elt.args = TI_ARGS (tinfo);
1728 elt.spec = NULL_TREE;
1730 entry = decl_specializations->find (&elt);
1731 if (entry != NULL)
1733 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1734 gcc_assert (new_spec != NULL_TREE);
1735 entry->spec = new_spec;
1736 return 1;
1739 return 0;
1742 /* Like register_specialization, but for local declarations. We are
1743 registering SPEC, an instantiation of TMPL. */
1745 static void
1746 register_local_specialization (tree spec, tree tmpl)
1748 local_specializations->put (tmpl, spec);
1751 /* TYPE is a class type. Returns true if TYPE is an explicitly
1752 specialized class. */
1754 bool
1755 explicit_class_specialization_p (tree type)
1757 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1758 return false;
1759 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1762 /* Print the list of functions at FNS, going through all the overloads
1763 for each element of the list. Alternatively, FNS can not be a
1764 TREE_LIST, in which case it will be printed together with all the
1765 overloads.
1767 MORE and *STR should respectively be FALSE and NULL when the function
1768 is called from the outside. They are used internally on recursive
1769 calls. print_candidates manages the two parameters and leaves NULL
1770 in *STR when it ends. */
1772 static void
1773 print_candidates_1 (tree fns, bool more, const char **str)
1775 tree fn, fn2;
1776 char *spaces = NULL;
1778 for (fn = fns; fn; fn = OVL_NEXT (fn))
1779 if (TREE_CODE (fn) == TREE_LIST)
1781 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1782 print_candidates_1 (TREE_VALUE (fn2),
1783 TREE_CHAIN (fn2) || more, str);
1785 else
1787 tree cand = OVL_CURRENT (fn);
1788 if (!*str)
1790 /* Pick the prefix string. */
1791 if (!more && !OVL_NEXT (fns))
1793 inform (DECL_SOURCE_LOCATION (cand),
1794 "candidate is: %#D", cand);
1795 continue;
1798 *str = _("candidates are:");
1799 spaces = get_spaces (*str);
1801 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1802 *str = spaces ? spaces : *str;
1805 if (!more)
1807 free (spaces);
1808 *str = NULL;
1812 /* Print the list of candidate FNS in an error message. FNS can also
1813 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1815 void
1816 print_candidates (tree fns)
1818 const char *str = NULL;
1819 print_candidates_1 (fns, false, &str);
1820 gcc_assert (str == NULL);
1823 /* Returns the template (one of the functions given by TEMPLATE_ID)
1824 which can be specialized to match the indicated DECL with the
1825 explicit template args given in TEMPLATE_ID. The DECL may be
1826 NULL_TREE if none is available. In that case, the functions in
1827 TEMPLATE_ID are non-members.
1829 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1830 specialization of a member template.
1832 The TEMPLATE_COUNT is the number of references to qualifying
1833 template classes that appeared in the name of the function. See
1834 check_explicit_specialization for a more accurate description.
1836 TSK indicates what kind of template declaration (if any) is being
1837 declared. TSK_TEMPLATE indicates that the declaration given by
1838 DECL, though a FUNCTION_DECL, has template parameters, and is
1839 therefore a template function.
1841 The template args (those explicitly specified and those deduced)
1842 are output in a newly created vector *TARGS_OUT.
1844 If it is impossible to determine the result, an error message is
1845 issued. The error_mark_node is returned to indicate failure. */
1847 static tree
1848 determine_specialization (tree template_id,
1849 tree decl,
1850 tree* targs_out,
1851 int need_member_template,
1852 int template_count,
1853 tmpl_spec_kind tsk)
1855 tree fns;
1856 tree targs;
1857 tree explicit_targs;
1858 tree candidates = NULL_TREE;
1859 /* A TREE_LIST of templates of which DECL may be a specialization.
1860 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1861 corresponding TREE_PURPOSE is the set of template arguments that,
1862 when used to instantiate the template, would produce a function
1863 with the signature of DECL. */
1864 tree templates = NULL_TREE;
1865 int header_count;
1866 cp_binding_level *b;
1868 *targs_out = NULL_TREE;
1870 if (template_id == error_mark_node || decl == error_mark_node)
1871 return error_mark_node;
1873 /* We shouldn't be specializing a member template of an
1874 unspecialized class template; we already gave an error in
1875 check_specialization_scope, now avoid crashing. */
1876 if (template_count && DECL_CLASS_SCOPE_P (decl)
1877 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1879 gcc_assert (errorcount);
1880 return error_mark_node;
1883 fns = TREE_OPERAND (template_id, 0);
1884 explicit_targs = TREE_OPERAND (template_id, 1);
1886 if (fns == error_mark_node)
1887 return error_mark_node;
1889 /* Check for baselinks. */
1890 if (BASELINK_P (fns))
1891 fns = BASELINK_FUNCTIONS (fns);
1893 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
1895 error ("%qD is not a function template", fns);
1896 return error_mark_node;
1898 else if (VAR_P (decl) && !variable_template_p (fns))
1900 error ("%qD is not a variable template", fns);
1901 return error_mark_node;
1904 /* Count the number of template headers specified for this
1905 specialization. */
1906 header_count = 0;
1907 for (b = current_binding_level;
1908 b->kind == sk_template_parms;
1909 b = b->level_chain)
1910 ++header_count;
1912 if (variable_template_p (fns))
1913 templates = tree_cons (explicit_targs, fns, templates);
1914 else for (; fns; fns = OVL_NEXT (fns))
1916 tree fn = OVL_CURRENT (fns);
1918 if (TREE_CODE (fn) == TEMPLATE_DECL)
1920 tree decl_arg_types;
1921 tree fn_arg_types;
1922 tree insttype;
1924 /* In case of explicit specialization, we need to check if
1925 the number of template headers appearing in the specialization
1926 is correct. This is usually done in check_explicit_specialization,
1927 but the check done there cannot be exhaustive when specializing
1928 member functions. Consider the following code:
1930 template <> void A<int>::f(int);
1931 template <> template <> void A<int>::f(int);
1933 Assuming that A<int> is not itself an explicit specialization
1934 already, the first line specializes "f" which is a non-template
1935 member function, whilst the second line specializes "f" which
1936 is a template member function. So both lines are syntactically
1937 correct, and check_explicit_specialization does not reject
1938 them.
1940 Here, we can do better, as we are matching the specialization
1941 against the declarations. We count the number of template
1942 headers, and we check if they match TEMPLATE_COUNT + 1
1943 (TEMPLATE_COUNT is the number of qualifying template classes,
1944 plus there must be another header for the member template
1945 itself).
1947 Notice that if header_count is zero, this is not a
1948 specialization but rather a template instantiation, so there
1949 is no check we can perform here. */
1950 if (header_count && header_count != template_count + 1)
1951 continue;
1953 /* Check that the number of template arguments at the
1954 innermost level for DECL is the same as for FN. */
1955 if (current_binding_level->kind == sk_template_parms
1956 && !current_binding_level->explicit_spec_p
1957 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1958 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1959 (current_template_parms))))
1960 continue;
1962 /* DECL might be a specialization of FN. */
1963 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1964 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1966 /* For a non-static member function, we need to make sure
1967 that the const qualification is the same. Since
1968 get_bindings does not try to merge the "this" parameter,
1969 we must do the comparison explicitly. */
1970 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1971 && !same_type_p (TREE_VALUE (fn_arg_types),
1972 TREE_VALUE (decl_arg_types)))
1973 continue;
1975 /* Skip the "this" parameter and, for constructors of
1976 classes with virtual bases, the VTT parameter. A
1977 full specialization of a constructor will have a VTT
1978 parameter, but a template never will. */
1979 decl_arg_types
1980 = skip_artificial_parms_for (decl, decl_arg_types);
1981 fn_arg_types
1982 = skip_artificial_parms_for (fn, fn_arg_types);
1984 /* Function templates cannot be specializations; there are
1985 no partial specializations of functions. Therefore, if
1986 the type of DECL does not match FN, there is no
1987 match. */
1988 if (tsk == tsk_template)
1990 if (compparms (fn_arg_types, decl_arg_types))
1991 candidates = tree_cons (NULL_TREE, fn, candidates);
1992 continue;
1995 /* See whether this function might be a specialization of this
1996 template. Suppress access control because we might be trying
1997 to make this specialization a friend, and we have already done
1998 access control for the declaration of the specialization. */
1999 push_deferring_access_checks (dk_no_check);
2000 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2001 pop_deferring_access_checks ();
2003 if (!targs)
2004 /* We cannot deduce template arguments that when used to
2005 specialize TMPL will produce DECL. */
2006 continue;
2008 /* Make sure that the deduced arguments actually work. */
2009 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2010 if (insttype == error_mark_node)
2011 continue;
2012 fn_arg_types
2013 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2014 if (!compparms (fn_arg_types, decl_arg_types))
2015 continue;
2017 /* Save this template, and the arguments deduced. */
2018 templates = tree_cons (targs, fn, templates);
2020 else if (need_member_template)
2021 /* FN is an ordinary member function, and we need a
2022 specialization of a member template. */
2024 else if (TREE_CODE (fn) != FUNCTION_DECL)
2025 /* We can get IDENTIFIER_NODEs here in certain erroneous
2026 cases. */
2028 else if (!DECL_FUNCTION_MEMBER_P (fn))
2029 /* This is just an ordinary non-member function. Nothing can
2030 be a specialization of that. */
2032 else if (DECL_ARTIFICIAL (fn))
2033 /* Cannot specialize functions that are created implicitly. */
2035 else
2037 tree decl_arg_types;
2039 /* This is an ordinary member function. However, since
2040 we're here, we can assume its enclosing class is a
2041 template class. For example,
2043 template <typename T> struct S { void f(); };
2044 template <> void S<int>::f() {}
2046 Here, S<int>::f is a non-template, but S<int> is a
2047 template class. If FN has the same type as DECL, we
2048 might be in business. */
2050 if (!DECL_TEMPLATE_INFO (fn))
2051 /* Its enclosing class is an explicit specialization
2052 of a template class. This is not a candidate. */
2053 continue;
2055 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2056 TREE_TYPE (TREE_TYPE (fn))))
2057 /* The return types differ. */
2058 continue;
2060 /* Adjust the type of DECL in case FN is a static member. */
2061 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2062 if (DECL_STATIC_FUNCTION_P (fn)
2063 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2064 decl_arg_types = TREE_CHAIN (decl_arg_types);
2066 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2067 decl_arg_types))
2068 /* They match! */
2069 candidates = tree_cons (NULL_TREE, fn, candidates);
2073 if (templates && TREE_CHAIN (templates))
2075 /* We have:
2077 [temp.expl.spec]
2079 It is possible for a specialization with a given function
2080 signature to be instantiated from more than one function
2081 template. In such cases, explicit specification of the
2082 template arguments must be used to uniquely identify the
2083 function template specialization being specialized.
2085 Note that here, there's no suggestion that we're supposed to
2086 determine which of the candidate templates is most
2087 specialized. However, we, also have:
2089 [temp.func.order]
2091 Partial ordering of overloaded function template
2092 declarations is used in the following contexts to select
2093 the function template to which a function template
2094 specialization refers:
2096 -- when an explicit specialization refers to a function
2097 template.
2099 So, we do use the partial ordering rules, at least for now.
2100 This extension can only serve to make invalid programs valid,
2101 so it's safe. And, there is strong anecdotal evidence that
2102 the committee intended the partial ordering rules to apply;
2103 the EDG front end has that behavior, and John Spicer claims
2104 that the committee simply forgot to delete the wording in
2105 [temp.expl.spec]. */
2106 tree tmpl = most_specialized_instantiation (templates);
2107 if (tmpl != error_mark_node)
2109 templates = tmpl;
2110 TREE_CHAIN (templates) = NULL_TREE;
2114 if (templates == NULL_TREE && candidates == NULL_TREE)
2116 error ("template-id %qD for %q+D does not match any template "
2117 "declaration", template_id, decl);
2118 if (header_count && header_count != template_count + 1)
2119 inform (input_location, "saw %d %<template<>%>, need %d for "
2120 "specializing a member function template",
2121 header_count, template_count + 1);
2122 return error_mark_node;
2124 else if ((templates && TREE_CHAIN (templates))
2125 || (candidates && TREE_CHAIN (candidates))
2126 || (templates && candidates))
2128 error ("ambiguous template specialization %qD for %q+D",
2129 template_id, decl);
2130 candidates = chainon (candidates, templates);
2131 print_candidates (candidates);
2132 return error_mark_node;
2135 /* We have one, and exactly one, match. */
2136 if (candidates)
2138 tree fn = TREE_VALUE (candidates);
2139 *targs_out = copy_node (DECL_TI_ARGS (fn));
2140 /* DECL is a re-declaration or partial instantiation of a template
2141 function. */
2142 if (TREE_CODE (fn) == TEMPLATE_DECL)
2143 return fn;
2144 /* It was a specialization of an ordinary member function in a
2145 template class. */
2146 return DECL_TI_TEMPLATE (fn);
2149 /* It was a specialization of a template. */
2150 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2151 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2153 *targs_out = copy_node (targs);
2154 SET_TMPL_ARGS_LEVEL (*targs_out,
2155 TMPL_ARGS_DEPTH (*targs_out),
2156 TREE_PURPOSE (templates));
2158 else
2159 *targs_out = TREE_PURPOSE (templates);
2160 return TREE_VALUE (templates);
2163 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2164 but with the default argument values filled in from those in the
2165 TMPL_TYPES. */
2167 static tree
2168 copy_default_args_to_explicit_spec_1 (tree spec_types,
2169 tree tmpl_types)
2171 tree new_spec_types;
2173 if (!spec_types)
2174 return NULL_TREE;
2176 if (spec_types == void_list_node)
2177 return void_list_node;
2179 /* Substitute into the rest of the list. */
2180 new_spec_types =
2181 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2182 TREE_CHAIN (tmpl_types));
2184 /* Add the default argument for this parameter. */
2185 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2186 TREE_VALUE (spec_types),
2187 new_spec_types);
2190 /* DECL is an explicit specialization. Replicate default arguments
2191 from the template it specializes. (That way, code like:
2193 template <class T> void f(T = 3);
2194 template <> void f(double);
2195 void g () { f (); }
2197 works, as required.) An alternative approach would be to look up
2198 the correct default arguments at the call-site, but this approach
2199 is consistent with how implicit instantiations are handled. */
2201 static void
2202 copy_default_args_to_explicit_spec (tree decl)
2204 tree tmpl;
2205 tree spec_types;
2206 tree tmpl_types;
2207 tree new_spec_types;
2208 tree old_type;
2209 tree new_type;
2210 tree t;
2211 tree object_type = NULL_TREE;
2212 tree in_charge = NULL_TREE;
2213 tree vtt = NULL_TREE;
2215 /* See if there's anything we need to do. */
2216 tmpl = DECL_TI_TEMPLATE (decl);
2217 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2218 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2219 if (TREE_PURPOSE (t))
2220 break;
2221 if (!t)
2222 return;
2224 old_type = TREE_TYPE (decl);
2225 spec_types = TYPE_ARG_TYPES (old_type);
2227 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2229 /* Remove the this pointer, but remember the object's type for
2230 CV quals. */
2231 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2232 spec_types = TREE_CHAIN (spec_types);
2233 tmpl_types = TREE_CHAIN (tmpl_types);
2235 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2237 /* DECL may contain more parameters than TMPL due to the extra
2238 in-charge parameter in constructors and destructors. */
2239 in_charge = spec_types;
2240 spec_types = TREE_CHAIN (spec_types);
2242 if (DECL_HAS_VTT_PARM_P (decl))
2244 vtt = spec_types;
2245 spec_types = TREE_CHAIN (spec_types);
2249 /* Compute the merged default arguments. */
2250 new_spec_types =
2251 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2253 /* Compute the new FUNCTION_TYPE. */
2254 if (object_type)
2256 if (vtt)
2257 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2258 TREE_VALUE (vtt),
2259 new_spec_types);
2261 if (in_charge)
2262 /* Put the in-charge parameter back. */
2263 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2264 TREE_VALUE (in_charge),
2265 new_spec_types);
2267 new_type = build_method_type_directly (object_type,
2268 TREE_TYPE (old_type),
2269 new_spec_types);
2271 else
2272 new_type = build_function_type (TREE_TYPE (old_type),
2273 new_spec_types);
2274 new_type = cp_build_type_attribute_variant (new_type,
2275 TYPE_ATTRIBUTES (old_type));
2276 new_type = build_exception_variant (new_type,
2277 TYPE_RAISES_EXCEPTIONS (old_type));
2279 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2280 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2282 TREE_TYPE (decl) = new_type;
2285 /* Return the number of template headers we expect to see for a definition
2286 or specialization of CTYPE or one of its non-template members. */
2289 num_template_headers_for_class (tree ctype)
2291 int num_templates = 0;
2293 while (ctype && CLASS_TYPE_P (ctype))
2295 /* You're supposed to have one `template <...>' for every
2296 template class, but you don't need one for a full
2297 specialization. For example:
2299 template <class T> struct S{};
2300 template <> struct S<int> { void f(); };
2301 void S<int>::f () {}
2303 is correct; there shouldn't be a `template <>' for the
2304 definition of `S<int>::f'. */
2305 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2306 /* If CTYPE does not have template information of any
2307 kind, then it is not a template, nor is it nested
2308 within a template. */
2309 break;
2310 if (explicit_class_specialization_p (ctype))
2311 break;
2312 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2313 ++num_templates;
2315 ctype = TYPE_CONTEXT (ctype);
2318 return num_templates;
2321 /* Do a simple sanity check on the template headers that precede the
2322 variable declaration DECL. */
2324 void
2325 check_template_variable (tree decl)
2327 tree ctx = CP_DECL_CONTEXT (decl);
2328 int wanted = num_template_headers_for_class (ctx);
2329 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2330 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2332 if (cxx_dialect < cxx14)
2333 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2334 "variable templates only available with "
2335 "-std=c++14 or -std=gnu++14");
2337 // Namespace-scope variable templates should have a template header.
2338 ++wanted;
2340 if (template_header_count > wanted)
2342 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2343 "too many template headers for %D (should be %d)",
2344 decl, wanted);
2345 if (warned && CLASS_TYPE_P (ctx)
2346 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2347 inform (DECL_SOURCE_LOCATION (decl),
2348 "members of an explicitly specialized class are defined "
2349 "without a template header");
2353 /* Check to see if the function just declared, as indicated in
2354 DECLARATOR, and in DECL, is a specialization of a function
2355 template. We may also discover that the declaration is an explicit
2356 instantiation at this point.
2358 Returns DECL, or an equivalent declaration that should be used
2359 instead if all goes well. Issues an error message if something is
2360 amiss. Returns error_mark_node if the error is not easily
2361 recoverable.
2363 FLAGS is a bitmask consisting of the following flags:
2365 2: The function has a definition.
2366 4: The function is a friend.
2368 The TEMPLATE_COUNT is the number of references to qualifying
2369 template classes that appeared in the name of the function. For
2370 example, in
2372 template <class T> struct S { void f(); };
2373 void S<int>::f();
2375 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2376 classes are not counted in the TEMPLATE_COUNT, so that in
2378 template <class T> struct S {};
2379 template <> struct S<int> { void f(); }
2380 template <> void S<int>::f();
2382 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2383 invalid; there should be no template <>.)
2385 If the function is a specialization, it is marked as such via
2386 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2387 is set up correctly, and it is added to the list of specializations
2388 for that template. */
2390 tree
2391 check_explicit_specialization (tree declarator,
2392 tree decl,
2393 int template_count,
2394 int flags)
2396 int have_def = flags & 2;
2397 int is_friend = flags & 4;
2398 int specialization = 0;
2399 int explicit_instantiation = 0;
2400 int member_specialization = 0;
2401 tree ctype = DECL_CLASS_CONTEXT (decl);
2402 tree dname = DECL_NAME (decl);
2403 tmpl_spec_kind tsk;
2405 if (is_friend)
2407 if (!processing_specialization)
2408 tsk = tsk_none;
2409 else
2410 tsk = tsk_excessive_parms;
2412 else
2413 tsk = current_tmpl_spec_kind (template_count);
2415 switch (tsk)
2417 case tsk_none:
2418 if (processing_specialization)
2420 specialization = 1;
2421 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2423 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2425 if (is_friend)
2426 /* This could be something like:
2428 template <class T> void f(T);
2429 class S { friend void f<>(int); } */
2430 specialization = 1;
2431 else
2433 /* This case handles bogus declarations like template <>
2434 template <class T> void f<int>(); */
2436 error ("template-id %qD in declaration of primary template",
2437 declarator);
2438 return decl;
2441 break;
2443 case tsk_invalid_member_spec:
2444 /* The error has already been reported in
2445 check_specialization_scope. */
2446 return error_mark_node;
2448 case tsk_invalid_expl_inst:
2449 error ("template parameter list used in explicit instantiation");
2451 /* Fall through. */
2453 case tsk_expl_inst:
2454 if (have_def)
2455 error ("definition provided for explicit instantiation");
2457 explicit_instantiation = 1;
2458 break;
2460 case tsk_excessive_parms:
2461 case tsk_insufficient_parms:
2462 if (tsk == tsk_excessive_parms)
2463 error ("too many template parameter lists in declaration of %qD",
2464 decl);
2465 else if (template_header_count)
2466 error("too few template parameter lists in declaration of %qD", decl);
2467 else
2468 error("explicit specialization of %qD must be introduced by "
2469 "%<template <>%>", decl);
2471 /* Fall through. */
2472 case tsk_expl_spec:
2473 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2474 /* In cases like template<> constexpr bool v = true;
2475 We'll give an error in check_template_variable. */
2476 break;
2478 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2479 if (ctype)
2480 member_specialization = 1;
2481 else
2482 specialization = 1;
2483 break;
2485 case tsk_template:
2486 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2488 /* This case handles bogus declarations like template <>
2489 template <class T> void f<int>(); */
2491 if (!uses_template_parms (declarator))
2492 error ("template-id %qD in declaration of primary template",
2493 declarator);
2494 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2496 /* Partial specialization of variable template. */
2497 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2498 specialization = 1;
2499 goto ok;
2501 else if (cxx_dialect < cxx14)
2502 error ("non-type partial specialization %qD "
2503 "is not allowed", declarator);
2504 else
2505 error ("non-class, non-variable partial specialization %qD "
2506 "is not allowed", declarator);
2507 return decl;
2508 ok:;
2511 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2512 /* This is a specialization of a member template, without
2513 specialization the containing class. Something like:
2515 template <class T> struct S {
2516 template <class U> void f (U);
2518 template <> template <class U> void S<int>::f(U) {}
2520 That's a specialization -- but of the entire template. */
2521 specialization = 1;
2522 break;
2524 default:
2525 gcc_unreachable ();
2528 if ((specialization || member_specialization)
2529 /* This doesn't apply to variable templates. */
2530 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2531 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2533 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2534 for (; t; t = TREE_CHAIN (t))
2535 if (TREE_PURPOSE (t))
2537 permerror (input_location,
2538 "default argument specified in explicit specialization");
2539 break;
2543 if (specialization || member_specialization || explicit_instantiation)
2545 tree tmpl = NULL_TREE;
2546 tree targs = NULL_TREE;
2547 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2549 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2550 if (!was_template_id)
2552 tree fns;
2554 gcc_assert (identifier_p (declarator));
2555 if (ctype)
2556 fns = dname;
2557 else
2559 /* If there is no class context, the explicit instantiation
2560 must be at namespace scope. */
2561 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2563 /* Find the namespace binding, using the declaration
2564 context. */
2565 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2566 false, true);
2567 if (fns == error_mark_node || !is_overloaded_fn (fns))
2569 error ("%qD is not a template function", dname);
2570 fns = error_mark_node;
2572 else
2574 tree fn = OVL_CURRENT (fns);
2575 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2576 CP_DECL_CONTEXT (fn)))
2577 error ("%qD is not declared in %qD",
2578 decl, current_namespace);
2582 declarator = lookup_template_function (fns, NULL_TREE);
2585 if (declarator == error_mark_node)
2586 return error_mark_node;
2588 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2590 if (!explicit_instantiation)
2591 /* A specialization in class scope. This is invalid,
2592 but the error will already have been flagged by
2593 check_specialization_scope. */
2594 return error_mark_node;
2595 else
2597 /* It's not valid to write an explicit instantiation in
2598 class scope, e.g.:
2600 class C { template void f(); }
2602 This case is caught by the parser. However, on
2603 something like:
2605 template class C { void f(); };
2607 (which is invalid) we can get here. The error will be
2608 issued later. */
2612 return decl;
2614 else if (ctype != NULL_TREE
2615 && (identifier_p (TREE_OPERAND (declarator, 0))))
2617 // We'll match variable templates in start_decl.
2618 if (VAR_P (decl))
2619 return decl;
2621 /* Find the list of functions in ctype that have the same
2622 name as the declared function. */
2623 tree name = TREE_OPERAND (declarator, 0);
2624 tree fns = NULL_TREE;
2625 int idx;
2627 if (constructor_name_p (name, ctype))
2629 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2631 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2632 : !CLASSTYPE_DESTRUCTORS (ctype))
2634 /* From [temp.expl.spec]:
2636 If such an explicit specialization for the member
2637 of a class template names an implicitly-declared
2638 special member function (clause _special_), the
2639 program is ill-formed.
2641 Similar language is found in [temp.explicit]. */
2642 error ("specialization of implicitly-declared special member function");
2643 return error_mark_node;
2646 name = is_constructor ? ctor_identifier : dtor_identifier;
2649 if (!DECL_CONV_FN_P (decl))
2651 idx = lookup_fnfields_1 (ctype, name);
2652 if (idx >= 0)
2653 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2655 else
2657 vec<tree, va_gc> *methods;
2658 tree ovl;
2660 /* For a type-conversion operator, we cannot do a
2661 name-based lookup. We might be looking for `operator
2662 int' which will be a specialization of `operator T'.
2663 So, we find *all* the conversion operators, and then
2664 select from them. */
2665 fns = NULL_TREE;
2667 methods = CLASSTYPE_METHOD_VEC (ctype);
2668 if (methods)
2669 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2670 methods->iterate (idx, &ovl);
2671 ++idx)
2673 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2674 /* There are no more conversion functions. */
2675 break;
2677 /* Glue all these conversion functions together
2678 with those we already have. */
2679 for (; ovl; ovl = OVL_NEXT (ovl))
2680 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2684 if (fns == NULL_TREE)
2686 error ("no member function %qD declared in %qT", name, ctype);
2687 return error_mark_node;
2689 else
2690 TREE_OPERAND (declarator, 0) = fns;
2693 /* Figure out what exactly is being specialized at this point.
2694 Note that for an explicit instantiation, even one for a
2695 member function, we cannot tell apriori whether the
2696 instantiation is for a member template, or just a member
2697 function of a template class. Even if a member template is
2698 being instantiated, the member template arguments may be
2699 elided if they can be deduced from the rest of the
2700 declaration. */
2701 tmpl = determine_specialization (declarator, decl,
2702 &targs,
2703 member_specialization,
2704 template_count,
2705 tsk);
2707 if (!tmpl || tmpl == error_mark_node)
2708 /* We couldn't figure out what this declaration was
2709 specializing. */
2710 return error_mark_node;
2711 else
2713 tree gen_tmpl = most_general_template (tmpl);
2715 if (explicit_instantiation)
2717 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2718 is done by do_decl_instantiation later. */
2720 int arg_depth = TMPL_ARGS_DEPTH (targs);
2721 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2723 if (arg_depth > parm_depth)
2725 /* If TMPL is not the most general template (for
2726 example, if TMPL is a friend template that is
2727 injected into namespace scope), then there will
2728 be too many levels of TARGS. Remove some of them
2729 here. */
2730 int i;
2731 tree new_targs;
2733 new_targs = make_tree_vec (parm_depth);
2734 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2735 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2736 = TREE_VEC_ELT (targs, i);
2737 targs = new_targs;
2740 return instantiate_template (tmpl, targs, tf_error);
2743 /* If we thought that the DECL was a member function, but it
2744 turns out to be specializing a static member function,
2745 make DECL a static member function as well. */
2746 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2747 && DECL_STATIC_FUNCTION_P (tmpl)
2748 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2749 revert_static_member_fn (decl);
2751 /* If this is a specialization of a member template of a
2752 template class, we want to return the TEMPLATE_DECL, not
2753 the specialization of it. */
2754 if (tsk == tsk_template && !was_template_id)
2756 tree result = DECL_TEMPLATE_RESULT (tmpl);
2757 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2758 DECL_INITIAL (result) = NULL_TREE;
2759 if (have_def)
2761 tree parm;
2762 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2763 DECL_SOURCE_LOCATION (result)
2764 = DECL_SOURCE_LOCATION (decl);
2765 /* We want to use the argument list specified in the
2766 definition, not in the original declaration. */
2767 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2768 for (parm = DECL_ARGUMENTS (result); parm;
2769 parm = DECL_CHAIN (parm))
2770 DECL_CONTEXT (parm) = result;
2772 return register_specialization (tmpl, gen_tmpl, targs,
2773 is_friend, 0);
2776 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2777 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2779 if (was_template_id)
2780 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
2782 /* Inherit default function arguments from the template
2783 DECL is specializing. */
2784 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2785 copy_default_args_to_explicit_spec (decl);
2787 /* This specialization has the same protection as the
2788 template it specializes. */
2789 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2790 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2792 /* 7.1.1-1 [dcl.stc]
2794 A storage-class-specifier shall not be specified in an
2795 explicit specialization...
2797 The parser rejects these, so unless action is taken here,
2798 explicit function specializations will always appear with
2799 global linkage.
2801 The action recommended by the C++ CWG in response to C++
2802 defect report 605 is to make the storage class and linkage
2803 of the explicit specialization match the templated function:
2805 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2807 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2809 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2810 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2812 /* This specialization has the same linkage and visibility as
2813 the function template it specializes. */
2814 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2815 if (! TREE_PUBLIC (decl))
2817 DECL_INTERFACE_KNOWN (decl) = 1;
2818 DECL_NOT_REALLY_EXTERN (decl) = 1;
2820 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2821 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2823 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2824 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2828 /* If DECL is a friend declaration, declared using an
2829 unqualified name, the namespace associated with DECL may
2830 have been set incorrectly. For example, in:
2832 template <typename T> void f(T);
2833 namespace N {
2834 struct S { friend void f<int>(int); }
2837 we will have set the DECL_CONTEXT for the friend
2838 declaration to N, rather than to the global namespace. */
2839 if (DECL_NAMESPACE_SCOPE_P (decl))
2840 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2842 if (is_friend && !have_def)
2843 /* This is not really a declaration of a specialization.
2844 It's just the name of an instantiation. But, it's not
2845 a request for an instantiation, either. */
2846 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2847 else if (TREE_CODE (decl) == FUNCTION_DECL)
2848 /* A specialization is not necessarily COMDAT. */
2849 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
2850 && DECL_DECLARED_INLINE_P (decl));
2851 else if (TREE_CODE (decl) == VAR_DECL)
2852 DECL_COMDAT (decl) = false;
2854 /* Register this specialization so that we can find it
2855 again. */
2856 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2858 /* A 'structor should already have clones. */
2859 gcc_assert (decl == error_mark_node
2860 || variable_template_p (tmpl)
2861 || !(DECL_CONSTRUCTOR_P (decl)
2862 || DECL_DESTRUCTOR_P (decl))
2863 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2867 return decl;
2870 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2871 parameters. These are represented in the same format used for
2872 DECL_TEMPLATE_PARMS. */
2875 comp_template_parms (const_tree parms1, const_tree parms2)
2877 const_tree p1;
2878 const_tree p2;
2880 if (parms1 == parms2)
2881 return 1;
2883 for (p1 = parms1, p2 = parms2;
2884 p1 != NULL_TREE && p2 != NULL_TREE;
2885 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2887 tree t1 = TREE_VALUE (p1);
2888 tree t2 = TREE_VALUE (p2);
2889 int i;
2891 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2892 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2894 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2895 return 0;
2897 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2899 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2900 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2902 /* If either of the template parameters are invalid, assume
2903 they match for the sake of error recovery. */
2904 if (error_operand_p (parm1) || error_operand_p (parm2))
2905 return 1;
2907 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2908 return 0;
2910 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2911 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2912 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2913 continue;
2914 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2915 return 0;
2919 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2920 /* One set of parameters has more parameters lists than the
2921 other. */
2922 return 0;
2924 return 1;
2927 /* Determine whether PARM is a parameter pack. */
2929 bool
2930 template_parameter_pack_p (const_tree parm)
2932 /* Determine if we have a non-type template parameter pack. */
2933 if (TREE_CODE (parm) == PARM_DECL)
2934 return (DECL_TEMPLATE_PARM_P (parm)
2935 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2936 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2937 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2939 /* If this is a list of template parameters, we could get a
2940 TYPE_DECL or a TEMPLATE_DECL. */
2941 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2942 parm = TREE_TYPE (parm);
2944 /* Otherwise it must be a type template parameter. */
2945 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2946 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2947 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2950 /* Determine if T is a function parameter pack. */
2952 bool
2953 function_parameter_pack_p (const_tree t)
2955 if (t && TREE_CODE (t) == PARM_DECL)
2956 return DECL_PACK_P (t);
2957 return false;
2960 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2961 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2963 tree
2964 get_function_template_decl (const_tree primary_func_tmpl_inst)
2966 if (! primary_func_tmpl_inst
2967 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2968 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2969 return NULL;
2971 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2974 /* Return true iff the function parameter PARAM_DECL was expanded
2975 from the function parameter pack PACK. */
2977 bool
2978 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2980 if (DECL_ARTIFICIAL (param_decl)
2981 || !function_parameter_pack_p (pack))
2982 return false;
2984 /* The parameter pack and its pack arguments have the same
2985 DECL_PARM_INDEX. */
2986 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2989 /* Determine whether ARGS describes a variadic template args list,
2990 i.e., one that is terminated by a template argument pack. */
2992 static bool
2993 template_args_variadic_p (tree args)
2995 int nargs;
2996 tree last_parm;
2998 if (args == NULL_TREE)
2999 return false;
3001 args = INNERMOST_TEMPLATE_ARGS (args);
3002 nargs = TREE_VEC_LENGTH (args);
3004 if (nargs == 0)
3005 return false;
3007 last_parm = TREE_VEC_ELT (args, nargs - 1);
3009 return ARGUMENT_PACK_P (last_parm);
3012 /* Generate a new name for the parameter pack name NAME (an
3013 IDENTIFIER_NODE) that incorporates its */
3015 static tree
3016 make_ith_pack_parameter_name (tree name, int i)
3018 /* Munge the name to include the parameter index. */
3019 #define NUMBUF_LEN 128
3020 char numbuf[NUMBUF_LEN];
3021 char* newname;
3022 int newname_len;
3024 if (name == NULL_TREE)
3025 return name;
3026 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3027 newname_len = IDENTIFIER_LENGTH (name)
3028 + strlen (numbuf) + 2;
3029 newname = (char*)alloca (newname_len);
3030 snprintf (newname, newname_len,
3031 "%s#%i", IDENTIFIER_POINTER (name), i);
3032 return get_identifier (newname);
3035 /* Return true if T is a primary function, class or alias template
3036 instantiation. */
3038 bool
3039 primary_template_instantiation_p (const_tree t)
3041 if (!t)
3042 return false;
3044 if (TREE_CODE (t) == FUNCTION_DECL)
3045 return DECL_LANG_SPECIFIC (t)
3046 && DECL_TEMPLATE_INSTANTIATION (t)
3047 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3048 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3049 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3050 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3051 else if (alias_template_specialization_p (t))
3052 return true;
3053 return false;
3056 /* Return true if PARM is a template template parameter. */
3058 bool
3059 template_template_parameter_p (const_tree parm)
3061 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3064 /* Return true iff PARM is a DECL representing a type template
3065 parameter. */
3067 bool
3068 template_type_parameter_p (const_tree parm)
3070 return (parm
3071 && (TREE_CODE (parm) == TYPE_DECL
3072 || TREE_CODE (parm) == TEMPLATE_DECL)
3073 && DECL_TEMPLATE_PARM_P (parm));
3076 /* Return the template parameters of T if T is a
3077 primary template instantiation, NULL otherwise. */
3079 tree
3080 get_primary_template_innermost_parameters (const_tree t)
3082 tree parms = NULL, template_info = NULL;
3084 if ((template_info = get_template_info (t))
3085 && primary_template_instantiation_p (t))
3086 parms = INNERMOST_TEMPLATE_PARMS
3087 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3089 return parms;
3092 /* Return the template parameters of the LEVELth level from the full list
3093 of template parameters PARMS. */
3095 tree
3096 get_template_parms_at_level (tree parms, int level)
3098 tree p;
3099 if (!parms
3100 || TREE_CODE (parms) != TREE_LIST
3101 || level > TMPL_PARMS_DEPTH (parms))
3102 return NULL_TREE;
3104 for (p = parms; p; p = TREE_CHAIN (p))
3105 if (TMPL_PARMS_DEPTH (p) == level)
3106 return p;
3108 return NULL_TREE;
3111 /* Returns the template arguments of T if T is a template instantiation,
3112 NULL otherwise. */
3114 tree
3115 get_template_innermost_arguments (const_tree t)
3117 tree args = NULL, template_info = NULL;
3119 if ((template_info = get_template_info (t))
3120 && TI_ARGS (template_info))
3121 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3123 return args;
3126 /* Return the argument pack elements of T if T is a template argument pack,
3127 NULL otherwise. */
3129 tree
3130 get_template_argument_pack_elems (const_tree t)
3132 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3133 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3134 return NULL;
3136 return ARGUMENT_PACK_ARGS (t);
3139 /* Structure used to track the progress of find_parameter_packs_r. */
3140 struct find_parameter_pack_data
3142 /* TREE_LIST that will contain all of the parameter packs found by
3143 the traversal. */
3144 tree* parameter_packs;
3146 /* Set of AST nodes that have been visited by the traversal. */
3147 hash_set<tree> *visited;
3150 /* Identifies all of the argument packs that occur in a template
3151 argument and appends them to the TREE_LIST inside DATA, which is a
3152 find_parameter_pack_data structure. This is a subroutine of
3153 make_pack_expansion and uses_parameter_packs. */
3154 static tree
3155 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3157 tree t = *tp;
3158 struct find_parameter_pack_data* ppd =
3159 (struct find_parameter_pack_data*)data;
3160 bool parameter_pack_p = false;
3162 /* Handle type aliases/typedefs. */
3163 if (TYPE_ALIAS_P (t))
3165 if (TYPE_TEMPLATE_INFO (t))
3166 cp_walk_tree (&TYPE_TI_ARGS (t),
3167 &find_parameter_packs_r,
3168 ppd, ppd->visited);
3169 *walk_subtrees = 0;
3170 return NULL_TREE;
3173 /* Identify whether this is a parameter pack or not. */
3174 switch (TREE_CODE (t))
3176 case TEMPLATE_PARM_INDEX:
3177 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3178 parameter_pack_p = true;
3179 break;
3181 case TEMPLATE_TYPE_PARM:
3182 t = TYPE_MAIN_VARIANT (t);
3183 case TEMPLATE_TEMPLATE_PARM:
3184 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3185 parameter_pack_p = true;
3186 break;
3188 case FIELD_DECL:
3189 case PARM_DECL:
3190 if (DECL_PACK_P (t))
3192 /* We don't want to walk into the type of a PARM_DECL,
3193 because we don't want to see the type parameter pack. */
3194 *walk_subtrees = 0;
3195 parameter_pack_p = true;
3197 break;
3199 /* Look through a lambda capture proxy to the field pack. */
3200 case VAR_DECL:
3201 if (DECL_HAS_VALUE_EXPR_P (t))
3203 tree v = DECL_VALUE_EXPR (t);
3204 cp_walk_tree (&v,
3205 &find_parameter_packs_r,
3206 ppd, ppd->visited);
3207 *walk_subtrees = 0;
3209 break;
3211 case BASES:
3212 parameter_pack_p = true;
3213 break;
3214 default:
3215 /* Not a parameter pack. */
3216 break;
3219 if (parameter_pack_p)
3221 /* Add this parameter pack to the list. */
3222 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3225 if (TYPE_P (t))
3226 cp_walk_tree (&TYPE_CONTEXT (t),
3227 &find_parameter_packs_r, ppd, ppd->visited);
3229 /* This switch statement will return immediately if we don't find a
3230 parameter pack. */
3231 switch (TREE_CODE (t))
3233 case TEMPLATE_PARM_INDEX:
3234 return NULL_TREE;
3236 case BOUND_TEMPLATE_TEMPLATE_PARM:
3237 /* Check the template itself. */
3238 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3239 &find_parameter_packs_r, ppd, ppd->visited);
3240 /* Check the template arguments. */
3241 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3242 ppd->visited);
3243 *walk_subtrees = 0;
3244 return NULL_TREE;
3246 case TEMPLATE_TYPE_PARM:
3247 case TEMPLATE_TEMPLATE_PARM:
3248 return NULL_TREE;
3250 case PARM_DECL:
3251 return NULL_TREE;
3253 case RECORD_TYPE:
3254 if (TYPE_PTRMEMFUNC_P (t))
3255 return NULL_TREE;
3256 /* Fall through. */
3258 case UNION_TYPE:
3259 case ENUMERAL_TYPE:
3260 if (TYPE_TEMPLATE_INFO (t))
3261 cp_walk_tree (&TYPE_TI_ARGS (t),
3262 &find_parameter_packs_r, ppd, ppd->visited);
3264 *walk_subtrees = 0;
3265 return NULL_TREE;
3267 case CONSTRUCTOR:
3268 case TEMPLATE_DECL:
3269 cp_walk_tree (&TREE_TYPE (t),
3270 &find_parameter_packs_r, ppd, ppd->visited);
3271 return NULL_TREE;
3273 case TYPENAME_TYPE:
3274 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3275 ppd, ppd->visited);
3276 *walk_subtrees = 0;
3277 return NULL_TREE;
3279 case TYPE_PACK_EXPANSION:
3280 case EXPR_PACK_EXPANSION:
3281 *walk_subtrees = 0;
3282 return NULL_TREE;
3284 case INTEGER_TYPE:
3285 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3286 ppd, ppd->visited);
3287 *walk_subtrees = 0;
3288 return NULL_TREE;
3290 case IDENTIFIER_NODE:
3291 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3292 ppd->visited);
3293 *walk_subtrees = 0;
3294 return NULL_TREE;
3296 default:
3297 return NULL_TREE;
3300 return NULL_TREE;
3303 /* Determines if the expression or type T uses any parameter packs. */
3304 bool
3305 uses_parameter_packs (tree t)
3307 tree parameter_packs = NULL_TREE;
3308 struct find_parameter_pack_data ppd;
3309 ppd.parameter_packs = &parameter_packs;
3310 ppd.visited = new hash_set<tree>;
3311 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3312 delete ppd.visited;
3313 return parameter_packs != NULL_TREE;
3316 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3317 representation a base-class initializer into a parameter pack
3318 expansion. If all goes well, the resulting node will be an
3319 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3320 respectively. */
3321 tree
3322 make_pack_expansion (tree arg)
3324 tree result;
3325 tree parameter_packs = NULL_TREE;
3326 bool for_types = false;
3327 struct find_parameter_pack_data ppd;
3329 if (!arg || arg == error_mark_node)
3330 return arg;
3332 if (TREE_CODE (arg) == TREE_LIST)
3334 /* The only time we will see a TREE_LIST here is for a base
3335 class initializer. In this case, the TREE_PURPOSE will be a
3336 _TYPE node (representing the base class expansion we're
3337 initializing) and the TREE_VALUE will be a TREE_LIST
3338 containing the initialization arguments.
3340 The resulting expansion looks somewhat different from most
3341 expansions. Rather than returning just one _EXPANSION, we
3342 return a TREE_LIST whose TREE_PURPOSE is a
3343 TYPE_PACK_EXPANSION containing the bases that will be
3344 initialized. The TREE_VALUE will be identical to the
3345 original TREE_VALUE, which is a list of arguments that will
3346 be passed to each base. We do not introduce any new pack
3347 expansion nodes into the TREE_VALUE (although it is possible
3348 that some already exist), because the TREE_PURPOSE and
3349 TREE_VALUE all need to be expanded together with the same
3350 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3351 resulting TREE_PURPOSE will mention the parameter packs in
3352 both the bases and the arguments to the bases. */
3353 tree purpose;
3354 tree value;
3355 tree parameter_packs = NULL_TREE;
3357 /* Determine which parameter packs will be used by the base
3358 class expansion. */
3359 ppd.visited = new hash_set<tree>;
3360 ppd.parameter_packs = &parameter_packs;
3361 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3362 &ppd, ppd.visited);
3364 if (parameter_packs == NULL_TREE)
3366 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3367 delete ppd.visited;
3368 return error_mark_node;
3371 if (TREE_VALUE (arg) != void_type_node)
3373 /* Collect the sets of parameter packs used in each of the
3374 initialization arguments. */
3375 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3377 /* Determine which parameter packs will be expanded in this
3378 argument. */
3379 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3380 &ppd, ppd.visited);
3384 delete ppd.visited;
3386 /* Create the pack expansion type for the base type. */
3387 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3388 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3389 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3391 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3392 they will rarely be compared to anything. */
3393 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3395 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3398 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3399 for_types = true;
3401 /* Build the PACK_EXPANSION_* node. */
3402 result = for_types
3403 ? cxx_make_type (TYPE_PACK_EXPANSION)
3404 : make_node (EXPR_PACK_EXPANSION);
3405 SET_PACK_EXPANSION_PATTERN (result, arg);
3406 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3408 /* Propagate type and const-expression information. */
3409 TREE_TYPE (result) = TREE_TYPE (arg);
3410 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3412 else
3413 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3414 they will rarely be compared to anything. */
3415 SET_TYPE_STRUCTURAL_EQUALITY (result);
3417 /* Determine which parameter packs will be expanded. */
3418 ppd.parameter_packs = &parameter_packs;
3419 ppd.visited = new hash_set<tree>;
3420 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3421 delete ppd.visited;
3423 /* Make sure we found some parameter packs. */
3424 if (parameter_packs == NULL_TREE)
3426 if (TYPE_P (arg))
3427 error ("expansion pattern %<%T%> contains no argument packs", arg);
3428 else
3429 error ("expansion pattern %<%E%> contains no argument packs", arg);
3430 return error_mark_node;
3432 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3434 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3436 return result;
3439 /* Checks T for any "bare" parameter packs, which have not yet been
3440 expanded, and issues an error if any are found. This operation can
3441 only be done on full expressions or types (e.g., an expression
3442 statement, "if" condition, etc.), because we could have expressions like:
3444 foo(f(g(h(args)))...)
3446 where "args" is a parameter pack. check_for_bare_parameter_packs
3447 should not be called for the subexpressions args, h(args),
3448 g(h(args)), or f(g(h(args))), because we would produce erroneous
3449 error messages.
3451 Returns TRUE and emits an error if there were bare parameter packs,
3452 returns FALSE otherwise. */
3453 bool
3454 check_for_bare_parameter_packs (tree t)
3456 tree parameter_packs = NULL_TREE;
3457 struct find_parameter_pack_data ppd;
3459 if (!processing_template_decl || !t || t == error_mark_node)
3460 return false;
3462 if (TREE_CODE (t) == TYPE_DECL)
3463 t = TREE_TYPE (t);
3465 ppd.parameter_packs = &parameter_packs;
3466 ppd.visited = new hash_set<tree>;
3467 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3468 delete ppd.visited;
3470 if (parameter_packs)
3472 error ("parameter packs not expanded with %<...%>:");
3473 while (parameter_packs)
3475 tree pack = TREE_VALUE (parameter_packs);
3476 tree name = NULL_TREE;
3478 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3479 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3480 name = TYPE_NAME (pack);
3481 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3482 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3483 else
3484 name = DECL_NAME (pack);
3486 if (name)
3487 inform (input_location, " %qD", name);
3488 else
3489 inform (input_location, " <anonymous>");
3491 parameter_packs = TREE_CHAIN (parameter_packs);
3494 return true;
3497 return false;
3500 /* Expand any parameter packs that occur in the template arguments in
3501 ARGS. */
3502 tree
3503 expand_template_argument_pack (tree args)
3505 tree result_args = NULL_TREE;
3506 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3507 int num_result_args = -1;
3508 int non_default_args_count = -1;
3510 /* First, determine if we need to expand anything, and the number of
3511 slots we'll need. */
3512 for (in_arg = 0; in_arg < nargs; ++in_arg)
3514 tree arg = TREE_VEC_ELT (args, in_arg);
3515 if (arg == NULL_TREE)
3516 return args;
3517 if (ARGUMENT_PACK_P (arg))
3519 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3520 if (num_result_args < 0)
3521 num_result_args = in_arg + num_packed;
3522 else
3523 num_result_args += num_packed;
3525 else
3527 if (num_result_args >= 0)
3528 num_result_args++;
3532 /* If no expansion is necessary, we're done. */
3533 if (num_result_args < 0)
3534 return args;
3536 /* Expand arguments. */
3537 result_args = make_tree_vec (num_result_args);
3538 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3539 non_default_args_count =
3540 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3541 for (in_arg = 0; in_arg < nargs; ++in_arg)
3543 tree arg = TREE_VEC_ELT (args, in_arg);
3544 if (ARGUMENT_PACK_P (arg))
3546 tree packed = ARGUMENT_PACK_ARGS (arg);
3547 int i, num_packed = TREE_VEC_LENGTH (packed);
3548 for (i = 0; i < num_packed; ++i, ++out_arg)
3549 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3550 if (non_default_args_count > 0)
3551 non_default_args_count += num_packed - 1;
3553 else
3555 TREE_VEC_ELT (result_args, out_arg) = arg;
3556 ++out_arg;
3559 if (non_default_args_count >= 0)
3560 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3561 return result_args;
3564 /* Checks if DECL shadows a template parameter.
3566 [temp.local]: A template-parameter shall not be redeclared within its
3567 scope (including nested scopes).
3569 Emits an error and returns TRUE if the DECL shadows a parameter,
3570 returns FALSE otherwise. */
3572 bool
3573 check_template_shadow (tree decl)
3575 tree olddecl;
3577 /* If we're not in a template, we can't possibly shadow a template
3578 parameter. */
3579 if (!current_template_parms)
3580 return true;
3582 /* Figure out what we're shadowing. */
3583 if (TREE_CODE (decl) == OVERLOAD)
3584 decl = OVL_CURRENT (decl);
3585 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3587 /* If there's no previous binding for this name, we're not shadowing
3588 anything, let alone a template parameter. */
3589 if (!olddecl)
3590 return true;
3592 /* If we're not shadowing a template parameter, we're done. Note
3593 that OLDDECL might be an OVERLOAD (or perhaps even an
3594 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3595 node. */
3596 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3597 return true;
3599 /* We check for decl != olddecl to avoid bogus errors for using a
3600 name inside a class. We check TPFI to avoid duplicate errors for
3601 inline member templates. */
3602 if (decl == olddecl
3603 || (DECL_TEMPLATE_PARM_P (decl)
3604 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3605 return true;
3607 /* Don't complain about the injected class name, as we've already
3608 complained about the class itself. */
3609 if (DECL_SELF_REFERENCE_P (decl))
3610 return false;
3612 error ("declaration of %q+#D", decl);
3613 error (" shadows template parm %q+#D", olddecl);
3614 return false;
3617 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3618 ORIG_LEVEL, DECL, and TYPE. */
3620 static tree
3621 build_template_parm_index (int index,
3622 int level,
3623 int orig_level,
3624 tree decl,
3625 tree type)
3627 tree t = make_node (TEMPLATE_PARM_INDEX);
3628 TEMPLATE_PARM_IDX (t) = index;
3629 TEMPLATE_PARM_LEVEL (t) = level;
3630 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3631 TEMPLATE_PARM_DECL (t) = decl;
3632 TREE_TYPE (t) = type;
3633 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3634 TREE_READONLY (t) = TREE_READONLY (decl);
3636 return t;
3639 /* Find the canonical type parameter for the given template type
3640 parameter. Returns the canonical type parameter, which may be TYPE
3641 if no such parameter existed. */
3643 static tree
3644 canonical_type_parameter (tree type)
3646 tree list;
3647 int idx = TEMPLATE_TYPE_IDX (type);
3648 if (!canonical_template_parms)
3649 vec_alloc (canonical_template_parms, idx+1);
3651 while (canonical_template_parms->length () <= (unsigned)idx)
3652 vec_safe_push (canonical_template_parms, NULL_TREE);
3654 list = (*canonical_template_parms)[idx];
3655 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3656 list = TREE_CHAIN (list);
3658 if (list)
3659 return TREE_VALUE (list);
3660 else
3662 (*canonical_template_parms)[idx]
3663 = tree_cons (NULL_TREE, type,
3664 (*canonical_template_parms)[idx]);
3665 return type;
3669 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3670 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3671 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3672 new one is created. */
3674 static tree
3675 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3676 tsubst_flags_t complain)
3678 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3679 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3680 != TEMPLATE_PARM_LEVEL (index) - levels)
3681 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3683 tree orig_decl = TEMPLATE_PARM_DECL (index);
3684 tree decl, t;
3686 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3687 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3688 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3689 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3690 DECL_ARTIFICIAL (decl) = 1;
3691 SET_DECL_TEMPLATE_PARM_P (decl);
3693 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3694 TEMPLATE_PARM_LEVEL (index) - levels,
3695 TEMPLATE_PARM_ORIG_LEVEL (index),
3696 decl, type);
3697 TEMPLATE_PARM_DESCENDANTS (index) = t;
3698 TEMPLATE_PARM_PARAMETER_PACK (t)
3699 = TEMPLATE_PARM_PARAMETER_PACK (index);
3701 /* Template template parameters need this. */
3702 if (TREE_CODE (decl) == TEMPLATE_DECL)
3703 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3704 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3705 args, complain);
3708 return TEMPLATE_PARM_DESCENDANTS (index);
3711 /* Process information from new template parameter PARM and append it
3712 to the LIST being built. This new parameter is a non-type
3713 parameter iff IS_NON_TYPE is true. This new parameter is a
3714 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3715 is in PARM_LOC. */
3717 tree
3718 process_template_parm (tree list, location_t parm_loc, tree parm,
3719 bool is_non_type, bool is_parameter_pack)
3721 tree decl = 0;
3722 tree defval;
3723 int idx = 0;
3725 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3726 defval = TREE_PURPOSE (parm);
3728 if (list)
3730 tree p = tree_last (list);
3732 if (p && TREE_VALUE (p) != error_mark_node)
3734 p = TREE_VALUE (p);
3735 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3736 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3737 else
3738 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3741 ++idx;
3744 if (is_non_type)
3746 parm = TREE_VALUE (parm);
3748 SET_DECL_TEMPLATE_PARM_P (parm);
3750 if (TREE_TYPE (parm) != error_mark_node)
3752 /* [temp.param]
3754 The top-level cv-qualifiers on the template-parameter are
3755 ignored when determining its type. */
3756 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3757 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3758 TREE_TYPE (parm) = error_mark_node;
3759 else if (uses_parameter_packs (TREE_TYPE (parm))
3760 && !is_parameter_pack
3761 /* If we're in a nested template parameter list, the template
3762 template parameter could be a parameter pack. */
3763 && processing_template_parmlist == 1)
3765 /* This template parameter is not a parameter pack, but it
3766 should be. Complain about "bare" parameter packs. */
3767 check_for_bare_parameter_packs (TREE_TYPE (parm));
3769 /* Recover by calling this a parameter pack. */
3770 is_parameter_pack = true;
3774 /* A template parameter is not modifiable. */
3775 TREE_CONSTANT (parm) = 1;
3776 TREE_READONLY (parm) = 1;
3777 decl = build_decl (parm_loc,
3778 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3779 TREE_CONSTANT (decl) = 1;
3780 TREE_READONLY (decl) = 1;
3781 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3782 = build_template_parm_index (idx, processing_template_decl,
3783 processing_template_decl,
3784 decl, TREE_TYPE (parm));
3786 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3787 = is_parameter_pack;
3789 else
3791 tree t;
3792 parm = TREE_VALUE (TREE_VALUE (parm));
3794 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3796 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3797 /* This is for distinguishing between real templates and template
3798 template parameters */
3799 TREE_TYPE (parm) = t;
3800 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3801 decl = parm;
3803 else
3805 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3806 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3807 decl = build_decl (parm_loc,
3808 TYPE_DECL, parm, t);
3811 TYPE_NAME (t) = decl;
3812 TYPE_STUB_DECL (t) = decl;
3813 parm = decl;
3814 TEMPLATE_TYPE_PARM_INDEX (t)
3815 = build_template_parm_index (idx, processing_template_decl,
3816 processing_template_decl,
3817 decl, TREE_TYPE (parm));
3818 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3819 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3821 DECL_ARTIFICIAL (decl) = 1;
3822 SET_DECL_TEMPLATE_PARM_P (decl);
3823 pushdecl (decl);
3824 parm = build_tree_list (defval, parm);
3825 return chainon (list, parm);
3828 /* The end of a template parameter list has been reached. Process the
3829 tree list into a parameter vector, converting each parameter into a more
3830 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3831 as PARM_DECLs. */
3833 tree
3834 end_template_parm_list (tree parms)
3836 int nparms;
3837 tree parm, next;
3838 tree saved_parmlist = make_tree_vec (list_length (parms));
3840 current_template_parms
3841 = tree_cons (size_int (processing_template_decl),
3842 saved_parmlist, current_template_parms);
3844 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3846 next = TREE_CHAIN (parm);
3847 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3848 TREE_CHAIN (parm) = NULL_TREE;
3851 --processing_template_parmlist;
3853 return saved_parmlist;
3856 /* end_template_decl is called after a template declaration is seen. */
3858 void
3859 end_template_decl (void)
3861 reset_specialization ();
3863 if (! processing_template_decl)
3864 return;
3866 /* This matches the pushlevel in begin_template_parm_list. */
3867 finish_scope ();
3869 --processing_template_decl;
3870 current_template_parms = TREE_CHAIN (current_template_parms);
3873 /* Takes a TREE_LIST representing a template parameter and convert it
3874 into an argument suitable to be passed to the type substitution
3875 functions. Note that If the TREE_LIST contains an error_mark
3876 node, the returned argument is error_mark_node. */
3878 static tree
3879 template_parm_to_arg (tree t)
3882 if (t == NULL_TREE
3883 || TREE_CODE (t) != TREE_LIST)
3884 return t;
3886 if (error_operand_p (TREE_VALUE (t)))
3887 return error_mark_node;
3889 t = TREE_VALUE (t);
3891 if (TREE_CODE (t) == TYPE_DECL
3892 || TREE_CODE (t) == TEMPLATE_DECL)
3894 t = TREE_TYPE (t);
3896 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3898 /* Turn this argument into a TYPE_ARGUMENT_PACK
3899 with a single element, which expands T. */
3900 tree vec = make_tree_vec (1);
3901 #ifdef ENABLE_CHECKING
3902 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3903 (vec, TREE_VEC_LENGTH (vec));
3904 #endif
3905 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3907 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3908 SET_ARGUMENT_PACK_ARGS (t, vec);
3911 else
3913 t = DECL_INITIAL (t);
3915 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3917 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3918 with a single element, which expands T. */
3919 tree vec = make_tree_vec (1);
3920 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3921 #ifdef ENABLE_CHECKING
3922 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3923 (vec, TREE_VEC_LENGTH (vec));
3924 #endif
3925 t = convert_from_reference (t);
3926 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3928 t = make_node (NONTYPE_ARGUMENT_PACK);
3929 SET_ARGUMENT_PACK_ARGS (t, vec);
3930 TREE_TYPE (t) = type;
3932 else
3933 t = convert_from_reference (t);
3935 return t;
3938 /* Given a set of template parameters, return them as a set of template
3939 arguments. The template parameters are represented as a TREE_VEC, in
3940 the form documented in cp-tree.h for template arguments. */
3942 static tree
3943 template_parms_to_args (tree parms)
3945 tree header;
3946 tree args = NULL_TREE;
3947 int length = TMPL_PARMS_DEPTH (parms);
3948 int l = length;
3950 /* If there is only one level of template parameters, we do not
3951 create a TREE_VEC of TREE_VECs. Instead, we return a single
3952 TREE_VEC containing the arguments. */
3953 if (length > 1)
3954 args = make_tree_vec (length);
3956 for (header = parms; header; header = TREE_CHAIN (header))
3958 tree a = copy_node (TREE_VALUE (header));
3959 int i;
3961 TREE_TYPE (a) = NULL_TREE;
3962 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3963 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3965 #ifdef ENABLE_CHECKING
3966 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3967 #endif
3969 if (length > 1)
3970 TREE_VEC_ELT (args, --l) = a;
3971 else
3972 args = a;
3975 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3976 /* This can happen for template parms of a template template
3977 parameter, e.g:
3979 template<template<class T, class U> class TT> struct S;
3981 Consider the level of the parms of TT; T and U both have
3982 level 2; TT has no template parm of level 1. So in this case
3983 the first element of full_template_args is NULL_TREE. If we
3984 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3985 of 2. This will make tsubst wrongly consider that T and U
3986 have level 1. Instead, let's create a dummy vector as the
3987 first element of full_template_args so that TMPL_ARGS_DEPTH
3988 returns the correct depth for args. */
3989 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3990 return args;
3993 /* Within the declaration of a template, return the currently active
3994 template parameters as an argument TREE_VEC. */
3996 static tree
3997 current_template_args (void)
3999 return template_parms_to_args (current_template_parms);
4002 /* Update the declared TYPE by doing any lookups which were thought to be
4003 dependent, but are not now that we know the SCOPE of the declarator. */
4005 tree
4006 maybe_update_decl_type (tree orig_type, tree scope)
4008 tree type = orig_type;
4010 if (type == NULL_TREE)
4011 return type;
4013 if (TREE_CODE (orig_type) == TYPE_DECL)
4014 type = TREE_TYPE (type);
4016 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4017 && dependent_type_p (type)
4018 /* Don't bother building up the args in this case. */
4019 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4021 /* tsubst in the args corresponding to the template parameters,
4022 including auto if present. Most things will be unchanged, but
4023 make_typename_type and tsubst_qualified_id will resolve
4024 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4025 tree args = current_template_args ();
4026 tree auto_node = type_uses_auto (type);
4027 tree pushed;
4028 if (auto_node)
4030 tree auto_vec = make_tree_vec (1);
4031 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4032 args = add_to_template_args (args, auto_vec);
4034 pushed = push_scope (scope);
4035 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4036 if (pushed)
4037 pop_scope (scope);
4040 if (type == error_mark_node)
4041 return orig_type;
4043 if (TREE_CODE (orig_type) == TYPE_DECL)
4045 if (same_type_p (type, TREE_TYPE (orig_type)))
4046 type = orig_type;
4047 else
4048 type = TYPE_NAME (type);
4050 return type;
4053 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4054 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4055 a member template. Used by push_template_decl below. */
4057 static tree
4058 build_template_decl (tree decl, tree parms, bool member_template_p)
4060 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4061 DECL_TEMPLATE_PARMS (tmpl) = parms;
4062 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4063 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4064 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4066 return tmpl;
4069 struct template_parm_data
4071 /* The level of the template parameters we are currently
4072 processing. */
4073 int level;
4075 /* The index of the specialization argument we are currently
4076 processing. */
4077 int current_arg;
4079 /* An array whose size is the number of template parameters. The
4080 elements are nonzero if the parameter has been used in any one
4081 of the arguments processed so far. */
4082 int* parms;
4084 /* An array whose size is the number of template arguments. The
4085 elements are nonzero if the argument makes use of template
4086 parameters of this level. */
4087 int* arg_uses_template_parms;
4090 /* Subroutine of push_template_decl used to see if each template
4091 parameter in a partial specialization is used in the explicit
4092 argument list. If T is of the LEVEL given in DATA (which is
4093 treated as a template_parm_data*), then DATA->PARMS is marked
4094 appropriately. */
4096 static int
4097 mark_template_parm (tree t, void* data)
4099 int level;
4100 int idx;
4101 struct template_parm_data* tpd = (struct template_parm_data*) data;
4103 template_parm_level_and_index (t, &level, &idx);
4105 if (level == tpd->level)
4107 tpd->parms[idx] = 1;
4108 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4111 /* Return zero so that for_each_template_parm will continue the
4112 traversal of the tree; we want to mark *every* template parm. */
4113 return 0;
4116 /* Process the partial specialization DECL. */
4118 static tree
4119 process_partial_specialization (tree decl)
4121 tree type = TREE_TYPE (decl);
4122 tree tinfo = get_template_info (decl);
4123 tree maintmpl = TI_TEMPLATE (tinfo);
4124 tree specargs = TI_ARGS (tinfo);
4125 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4126 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4127 tree inner_parms;
4128 tree inst;
4129 int nargs = TREE_VEC_LENGTH (inner_args);
4130 int ntparms;
4131 int i;
4132 bool did_error_intro = false;
4133 struct template_parm_data tpd;
4134 struct template_parm_data tpd2;
4136 gcc_assert (current_template_parms);
4138 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4139 ntparms = TREE_VEC_LENGTH (inner_parms);
4141 /* We check that each of the template parameters given in the
4142 partial specialization is used in the argument list to the
4143 specialization. For example:
4145 template <class T> struct S;
4146 template <class T> struct S<T*>;
4148 The second declaration is OK because `T*' uses the template
4149 parameter T, whereas
4151 template <class T> struct S<int>;
4153 is no good. Even trickier is:
4155 template <class T>
4156 struct S1
4158 template <class U>
4159 struct S2;
4160 template <class U>
4161 struct S2<T>;
4164 The S2<T> declaration is actually invalid; it is a
4165 full-specialization. Of course,
4167 template <class U>
4168 struct S2<T (*)(U)>;
4170 or some such would have been OK. */
4171 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4172 tpd.parms = XALLOCAVEC (int, ntparms);
4173 memset (tpd.parms, 0, sizeof (int) * ntparms);
4175 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4176 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4177 for (i = 0; i < nargs; ++i)
4179 tpd.current_arg = i;
4180 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4181 &mark_template_parm,
4182 &tpd,
4183 NULL,
4184 /*include_nondeduced_p=*/false);
4186 for (i = 0; i < ntparms; ++i)
4187 if (tpd.parms[i] == 0)
4189 /* One of the template parms was not used in a deduced context in the
4190 specialization. */
4191 if (!did_error_intro)
4193 error ("template parameters not deducible in "
4194 "partial specialization:");
4195 did_error_intro = true;
4198 inform (input_location, " %qD",
4199 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4202 if (did_error_intro)
4203 return error_mark_node;
4205 /* [temp.class.spec]
4207 The argument list of the specialization shall not be identical to
4208 the implicit argument list of the primary template. */
4209 tree main_args
4210 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4211 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)))
4212 error ("partial specialization %qD does not specialize "
4213 "any template arguments", decl);
4215 /* A partial specialization that replaces multiple parameters of the
4216 primary template with a pack expansion is less specialized for those
4217 parameters. */
4218 if (nargs < DECL_NTPARMS (maintmpl))
4220 error ("partial specialization is not more specialized than the "
4221 "primary template because it replaces multiple parameters "
4222 "with a pack expansion");
4223 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4224 return decl;
4227 /* [temp.class.spec]
4229 A partially specialized non-type argument expression shall not
4230 involve template parameters of the partial specialization except
4231 when the argument expression is a simple identifier.
4233 The type of a template parameter corresponding to a specialized
4234 non-type argument shall not be dependent on a parameter of the
4235 specialization.
4237 Also, we verify that pack expansions only occur at the
4238 end of the argument list. */
4239 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4240 tpd2.parms = 0;
4241 for (i = 0; i < nargs; ++i)
4243 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4244 tree arg = TREE_VEC_ELT (inner_args, i);
4245 tree packed_args = NULL_TREE;
4246 int j, len = 1;
4248 if (ARGUMENT_PACK_P (arg))
4250 /* Extract the arguments from the argument pack. We'll be
4251 iterating over these in the following loop. */
4252 packed_args = ARGUMENT_PACK_ARGS (arg);
4253 len = TREE_VEC_LENGTH (packed_args);
4256 for (j = 0; j < len; j++)
4258 if (packed_args)
4259 /* Get the Jth argument in the parameter pack. */
4260 arg = TREE_VEC_ELT (packed_args, j);
4262 if (PACK_EXPANSION_P (arg))
4264 /* Pack expansions must come at the end of the
4265 argument list. */
4266 if ((packed_args && j < len - 1)
4267 || (!packed_args && i < nargs - 1))
4269 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4270 error ("parameter pack argument %qE must be at the "
4271 "end of the template argument list", arg);
4272 else
4273 error ("parameter pack argument %qT must be at the "
4274 "end of the template argument list", arg);
4278 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4279 /* We only care about the pattern. */
4280 arg = PACK_EXPANSION_PATTERN (arg);
4282 if (/* These first two lines are the `non-type' bit. */
4283 !TYPE_P (arg)
4284 && TREE_CODE (arg) != TEMPLATE_DECL
4285 /* This next two lines are the `argument expression is not just a
4286 simple identifier' condition and also the `specialized
4287 non-type argument' bit. */
4288 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4289 && !(REFERENCE_REF_P (arg)
4290 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4292 if ((!packed_args && tpd.arg_uses_template_parms[i])
4293 || (packed_args && uses_template_parms (arg)))
4294 error ("template argument %qE involves template parameter(s)",
4295 arg);
4296 else
4298 /* Look at the corresponding template parameter,
4299 marking which template parameters its type depends
4300 upon. */
4301 tree type = TREE_TYPE (parm);
4303 if (!tpd2.parms)
4305 /* We haven't yet initialized TPD2. Do so now. */
4306 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4307 /* The number of parameters here is the number in the
4308 main template, which, as checked in the assertion
4309 above, is NARGS. */
4310 tpd2.parms = XALLOCAVEC (int, nargs);
4311 tpd2.level =
4312 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4315 /* Mark the template parameters. But this time, we're
4316 looking for the template parameters of the main
4317 template, not in the specialization. */
4318 tpd2.current_arg = i;
4319 tpd2.arg_uses_template_parms[i] = 0;
4320 memset (tpd2.parms, 0, sizeof (int) * nargs);
4321 for_each_template_parm (type,
4322 &mark_template_parm,
4323 &tpd2,
4324 NULL,
4325 /*include_nondeduced_p=*/false);
4327 if (tpd2.arg_uses_template_parms [i])
4329 /* The type depended on some template parameters.
4330 If they are fully specialized in the
4331 specialization, that's OK. */
4332 int j;
4333 int count = 0;
4334 for (j = 0; j < nargs; ++j)
4335 if (tpd2.parms[j] != 0
4336 && tpd.arg_uses_template_parms [j])
4337 ++count;
4338 if (count != 0)
4339 error_n (input_location, count,
4340 "type %qT of template argument %qE depends "
4341 "on a template parameter",
4342 "type %qT of template argument %qE depends "
4343 "on template parameters",
4344 type,
4345 arg);
4352 /* We should only get here once. */
4353 if (TREE_CODE (decl) == TYPE_DECL)
4354 gcc_assert (!COMPLETE_TYPE_P (type));
4356 tree tmpl = build_template_decl (decl, current_template_parms,
4357 DECL_MEMBER_TEMPLATE_P (maintmpl));
4358 TREE_TYPE (tmpl) = type;
4359 DECL_TEMPLATE_RESULT (tmpl) = decl;
4360 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4361 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4362 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4364 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4365 = tree_cons (specargs, tmpl,
4366 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4367 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4369 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4370 inst = TREE_CHAIN (inst))
4372 tree instance = TREE_VALUE (inst);
4373 if (TYPE_P (instance)
4374 ? (COMPLETE_TYPE_P (instance)
4375 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4376 : DECL_TEMPLATE_INSTANTIATION (instance))
4378 tree spec = most_specialized_partial_spec (instance, tf_none);
4379 if (spec && TREE_VALUE (spec) == tmpl)
4381 tree inst_decl = (DECL_P (instance)
4382 ? instance : TYPE_NAME (instance));
4383 permerror (input_location,
4384 "partial specialization of %qD after instantiation "
4385 "of %qD", decl, inst_decl);
4390 return decl;
4393 /* PARM is a template parameter of some form; return the corresponding
4394 TEMPLATE_PARM_INDEX. */
4396 static tree
4397 get_template_parm_index (tree parm)
4399 if (TREE_CODE (parm) == PARM_DECL
4400 || TREE_CODE (parm) == CONST_DECL)
4401 parm = DECL_INITIAL (parm);
4402 else if (TREE_CODE (parm) == TYPE_DECL
4403 || TREE_CODE (parm) == TEMPLATE_DECL)
4404 parm = TREE_TYPE (parm);
4405 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4406 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4407 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4408 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4409 return parm;
4412 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4413 parameter packs used by the template parameter PARM. */
4415 static void
4416 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4418 /* A type parm can't refer to another parm. */
4419 if (TREE_CODE (parm) == TYPE_DECL)
4420 return;
4421 else if (TREE_CODE (parm) == PARM_DECL)
4423 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4424 ppd, ppd->visited);
4425 return;
4428 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4430 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4431 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4432 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4435 /* PARM is a template parameter pack. Return any parameter packs used in
4436 its type or the type of any of its template parameters. If there are
4437 any such packs, it will be instantiated into a fixed template parameter
4438 list by partial instantiation rather than be fully deduced. */
4440 tree
4441 fixed_parameter_pack_p (tree parm)
4443 /* This can only be true in a member template. */
4444 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4445 return NULL_TREE;
4446 /* This can only be true for a parameter pack. */
4447 if (!template_parameter_pack_p (parm))
4448 return NULL_TREE;
4449 /* A type parm can't refer to another parm. */
4450 if (TREE_CODE (parm) == TYPE_DECL)
4451 return NULL_TREE;
4453 tree parameter_packs = NULL_TREE;
4454 struct find_parameter_pack_data ppd;
4455 ppd.parameter_packs = &parameter_packs;
4456 ppd.visited = new hash_set<tree>;
4458 fixed_parameter_pack_p_1 (parm, &ppd);
4460 delete ppd.visited;
4461 return parameter_packs;
4464 /* Check that a template declaration's use of default arguments and
4465 parameter packs is not invalid. Here, PARMS are the template
4466 parameters. IS_PRIMARY is true if DECL is the thing declared by
4467 a primary template. IS_PARTIAL is true if DECL is a partial
4468 specialization.
4470 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4471 declaration (but not a definition); 1 indicates a declaration, 2
4472 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4473 emitted for extraneous default arguments.
4475 Returns TRUE if there were no errors found, FALSE otherwise. */
4477 bool
4478 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4479 bool is_partial, int is_friend_decl)
4481 const char *msg;
4482 int last_level_to_check;
4483 tree parm_level;
4484 bool no_errors = true;
4486 /* [temp.param]
4488 A default template-argument shall not be specified in a
4489 function template declaration or a function template definition, nor
4490 in the template-parameter-list of the definition of a member of a
4491 class template. */
4493 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4494 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4495 /* You can't have a function template declaration in a local
4496 scope, nor you can you define a member of a class template in a
4497 local scope. */
4498 return true;
4500 if ((TREE_CODE (decl) == TYPE_DECL
4501 && TREE_TYPE (decl)
4502 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4503 || (TREE_CODE (decl) == FUNCTION_DECL
4504 && LAMBDA_FUNCTION_P (decl)))
4505 /* A lambda doesn't have an explicit declaration; don't complain
4506 about the parms of the enclosing class. */
4507 return true;
4509 if (current_class_type
4510 && !TYPE_BEING_DEFINED (current_class_type)
4511 && DECL_LANG_SPECIFIC (decl)
4512 && DECL_DECLARES_FUNCTION_P (decl)
4513 /* If this is either a friend defined in the scope of the class
4514 or a member function. */
4515 && (DECL_FUNCTION_MEMBER_P (decl)
4516 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4517 : DECL_FRIEND_CONTEXT (decl)
4518 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4519 : false)
4520 /* And, if it was a member function, it really was defined in
4521 the scope of the class. */
4522 && (!DECL_FUNCTION_MEMBER_P (decl)
4523 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4524 /* We already checked these parameters when the template was
4525 declared, so there's no need to do it again now. This function
4526 was defined in class scope, but we're processing its body now
4527 that the class is complete. */
4528 return true;
4530 /* Core issue 226 (C++0x only): the following only applies to class
4531 templates. */
4532 if (is_primary
4533 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4535 /* [temp.param]
4537 If a template-parameter has a default template-argument, all
4538 subsequent template-parameters shall have a default
4539 template-argument supplied. */
4540 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4542 tree inner_parms = TREE_VALUE (parm_level);
4543 int ntparms = TREE_VEC_LENGTH (inner_parms);
4544 int seen_def_arg_p = 0;
4545 int i;
4547 for (i = 0; i < ntparms; ++i)
4549 tree parm = TREE_VEC_ELT (inner_parms, i);
4551 if (parm == error_mark_node)
4552 continue;
4554 if (TREE_PURPOSE (parm))
4555 seen_def_arg_p = 1;
4556 else if (seen_def_arg_p
4557 && !template_parameter_pack_p (TREE_VALUE (parm)))
4559 error ("no default argument for %qD", TREE_VALUE (parm));
4560 /* For better subsequent error-recovery, we indicate that
4561 there should have been a default argument. */
4562 TREE_PURPOSE (parm) = error_mark_node;
4563 no_errors = false;
4565 else if (!is_partial
4566 && !is_friend_decl
4567 /* Don't complain about an enclosing partial
4568 specialization. */
4569 && parm_level == parms
4570 && TREE_CODE (decl) == TYPE_DECL
4571 && i < ntparms - 1
4572 && template_parameter_pack_p (TREE_VALUE (parm))
4573 /* A fixed parameter pack will be partially
4574 instantiated into a fixed length list. */
4575 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4577 /* A primary class template can only have one
4578 parameter pack, at the end of the template
4579 parameter list. */
4581 error ("parameter pack %q+D must be at the end of the"
4582 " template parameter list", TREE_VALUE (parm));
4584 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4585 = error_mark_node;
4586 no_errors = false;
4592 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4593 || is_partial
4594 || !is_primary
4595 || is_friend_decl)
4596 /* For an ordinary class template, default template arguments are
4597 allowed at the innermost level, e.g.:
4598 template <class T = int>
4599 struct S {};
4600 but, in a partial specialization, they're not allowed even
4601 there, as we have in [temp.class.spec]:
4603 The template parameter list of a specialization shall not
4604 contain default template argument values.
4606 So, for a partial specialization, or for a function template
4607 (in C++98/C++03), we look at all of them. */
4609 else
4610 /* But, for a primary class template that is not a partial
4611 specialization we look at all template parameters except the
4612 innermost ones. */
4613 parms = TREE_CHAIN (parms);
4615 /* Figure out what error message to issue. */
4616 if (is_friend_decl == 2)
4617 msg = G_("default template arguments may not be used in function template "
4618 "friend re-declaration");
4619 else if (is_friend_decl)
4620 msg = G_("default template arguments may not be used in function template "
4621 "friend declarations");
4622 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4623 msg = G_("default template arguments may not be used in function templates "
4624 "without -std=c++11 or -std=gnu++11");
4625 else if (is_partial)
4626 msg = G_("default template arguments may not be used in "
4627 "partial specializations");
4628 else
4629 msg = G_("default argument for template parameter for class enclosing %qD");
4631 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4632 /* If we're inside a class definition, there's no need to
4633 examine the parameters to the class itself. On the one
4634 hand, they will be checked when the class is defined, and,
4635 on the other, default arguments are valid in things like:
4636 template <class T = double>
4637 struct S { template <class U> void f(U); };
4638 Here the default argument for `S' has no bearing on the
4639 declaration of `f'. */
4640 last_level_to_check = template_class_depth (current_class_type) + 1;
4641 else
4642 /* Check everything. */
4643 last_level_to_check = 0;
4645 for (parm_level = parms;
4646 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4647 parm_level = TREE_CHAIN (parm_level))
4649 tree inner_parms = TREE_VALUE (parm_level);
4650 int i;
4651 int ntparms;
4653 ntparms = TREE_VEC_LENGTH (inner_parms);
4654 for (i = 0; i < ntparms; ++i)
4656 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4657 continue;
4659 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4661 if (msg)
4663 no_errors = false;
4664 if (is_friend_decl == 2)
4665 return no_errors;
4667 error (msg, decl);
4668 msg = 0;
4671 /* Clear out the default argument so that we are not
4672 confused later. */
4673 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4677 /* At this point, if we're still interested in issuing messages,
4678 they must apply to classes surrounding the object declared. */
4679 if (msg)
4680 msg = G_("default argument for template parameter for class "
4681 "enclosing %qD");
4684 return no_errors;
4687 /* Worker for push_template_decl_real, called via
4688 for_each_template_parm. DATA is really an int, indicating the
4689 level of the parameters we are interested in. If T is a template
4690 parameter of that level, return nonzero. */
4692 static int
4693 template_parm_this_level_p (tree t, void* data)
4695 int this_level = *(int *)data;
4696 int level;
4698 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4699 level = TEMPLATE_PARM_LEVEL (t);
4700 else
4701 level = TEMPLATE_TYPE_LEVEL (t);
4702 return level == this_level;
4705 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4706 parameters given by current_template_args, or reuses a
4707 previously existing one, if appropriate. Returns the DECL, or an
4708 equivalent one, if it is replaced via a call to duplicate_decls.
4710 If IS_FRIEND is true, DECL is a friend declaration. */
4712 tree
4713 push_template_decl_real (tree decl, bool is_friend)
4715 tree tmpl;
4716 tree args;
4717 tree info;
4718 tree ctx;
4719 bool is_primary;
4720 bool is_partial;
4721 int new_template_p = 0;
4722 /* True if the template is a member template, in the sense of
4723 [temp.mem]. */
4724 bool member_template_p = false;
4726 if (decl == error_mark_node || !current_template_parms)
4727 return error_mark_node;
4729 /* See if this is a partial specialization. */
4730 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
4731 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4732 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4733 || (TREE_CODE (decl) == VAR_DECL
4734 && DECL_LANG_SPECIFIC (decl)
4735 && DECL_TEMPLATE_SPECIALIZATION (decl)
4736 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
4738 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4739 is_friend = true;
4741 if (is_friend)
4742 /* For a friend, we want the context of the friend function, not
4743 the type of which it is a friend. */
4744 ctx = CP_DECL_CONTEXT (decl);
4745 else if (CP_DECL_CONTEXT (decl)
4746 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4747 /* In the case of a virtual function, we want the class in which
4748 it is defined. */
4749 ctx = CP_DECL_CONTEXT (decl);
4750 else
4751 /* Otherwise, if we're currently defining some class, the DECL
4752 is assumed to be a member of the class. */
4753 ctx = current_scope ();
4755 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4756 ctx = NULL_TREE;
4758 if (!DECL_CONTEXT (decl))
4759 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4761 /* See if this is a primary template. */
4762 if (is_friend && ctx
4763 && uses_template_parms_level (ctx, processing_template_decl))
4764 /* A friend template that specifies a class context, i.e.
4765 template <typename T> friend void A<T>::f();
4766 is not primary. */
4767 is_primary = false;
4768 else if (TREE_CODE (decl) == TYPE_DECL
4769 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4770 is_primary = false;
4771 else
4772 is_primary = template_parm_scope_p ();
4774 if (is_primary)
4776 if (DECL_CLASS_SCOPE_P (decl))
4777 member_template_p = true;
4778 if (TREE_CODE (decl) == TYPE_DECL
4779 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4781 error ("template class without a name");
4782 return error_mark_node;
4784 else if (TREE_CODE (decl) == FUNCTION_DECL)
4786 if (member_template_p)
4788 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
4789 error ("member template %qD may not have virt-specifiers", decl);
4791 if (DECL_DESTRUCTOR_P (decl))
4793 /* [temp.mem]
4795 A destructor shall not be a member template. */
4796 error ("destructor %qD declared as member template", decl);
4797 return error_mark_node;
4799 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4800 && (!prototype_p (TREE_TYPE (decl))
4801 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4802 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4803 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4804 == void_list_node)))
4806 /* [basic.stc.dynamic.allocation]
4808 An allocation function can be a function
4809 template. ... Template allocation functions shall
4810 have two or more parameters. */
4811 error ("invalid template declaration of %qD", decl);
4812 return error_mark_node;
4815 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4816 && CLASS_TYPE_P (TREE_TYPE (decl)))
4817 /* OK */;
4818 else if (TREE_CODE (decl) == TYPE_DECL
4819 && TYPE_DECL_ALIAS_P (decl))
4820 /* alias-declaration */
4821 gcc_assert (!DECL_ARTIFICIAL (decl));
4822 else if (VAR_P (decl))
4823 /* C++14 variable template. */;
4824 else
4826 error ("template declaration of %q#D", decl);
4827 return error_mark_node;
4831 /* Check to see that the rules regarding the use of default
4832 arguments are not being violated. */
4833 check_default_tmpl_args (decl, current_template_parms,
4834 is_primary, is_partial, /*is_friend_decl=*/0);
4836 /* Ensure that there are no parameter packs in the type of this
4837 declaration that have not been expanded. */
4838 if (TREE_CODE (decl) == FUNCTION_DECL)
4840 /* Check each of the arguments individually to see if there are
4841 any bare parameter packs. */
4842 tree type = TREE_TYPE (decl);
4843 tree arg = DECL_ARGUMENTS (decl);
4844 tree argtype = TYPE_ARG_TYPES (type);
4846 while (arg && argtype)
4848 if (!DECL_PACK_P (arg)
4849 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4851 /* This is a PARM_DECL that contains unexpanded parameter
4852 packs. We have already complained about this in the
4853 check_for_bare_parameter_packs call, so just replace
4854 these types with ERROR_MARK_NODE. */
4855 TREE_TYPE (arg) = error_mark_node;
4856 TREE_VALUE (argtype) = error_mark_node;
4859 arg = DECL_CHAIN (arg);
4860 argtype = TREE_CHAIN (argtype);
4863 /* Check for bare parameter packs in the return type and the
4864 exception specifiers. */
4865 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4866 /* Errors were already issued, set return type to int
4867 as the frontend doesn't expect error_mark_node as
4868 the return type. */
4869 TREE_TYPE (type) = integer_type_node;
4870 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4871 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4873 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4874 && TYPE_DECL_ALIAS_P (decl))
4875 ? DECL_ORIGINAL_TYPE (decl)
4876 : TREE_TYPE (decl)))
4878 TREE_TYPE (decl) = error_mark_node;
4879 return error_mark_node;
4882 if (is_partial)
4883 return process_partial_specialization (decl);
4885 args = current_template_args ();
4887 if (!ctx
4888 || TREE_CODE (ctx) == FUNCTION_DECL
4889 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4890 || (TREE_CODE (decl) == TYPE_DECL
4891 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4892 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4894 if (DECL_LANG_SPECIFIC (decl)
4895 && DECL_TEMPLATE_INFO (decl)
4896 && DECL_TI_TEMPLATE (decl))
4897 tmpl = DECL_TI_TEMPLATE (decl);
4898 /* If DECL is a TYPE_DECL for a class-template, then there won't
4899 be DECL_LANG_SPECIFIC. The information equivalent to
4900 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4901 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4902 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4903 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4905 /* Since a template declaration already existed for this
4906 class-type, we must be redeclaring it here. Make sure
4907 that the redeclaration is valid. */
4908 redeclare_class_template (TREE_TYPE (decl),
4909 current_template_parms);
4910 /* We don't need to create a new TEMPLATE_DECL; just use the
4911 one we already had. */
4912 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4914 else
4916 tmpl = build_template_decl (decl, current_template_parms,
4917 member_template_p);
4918 new_template_p = 1;
4920 if (DECL_LANG_SPECIFIC (decl)
4921 && DECL_TEMPLATE_SPECIALIZATION (decl))
4923 /* A specialization of a member template of a template
4924 class. */
4925 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4926 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4927 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4931 else
4933 tree a, t, current, parms;
4934 int i;
4935 tree tinfo = get_template_info (decl);
4937 if (!tinfo)
4939 error ("template definition of non-template %q#D", decl);
4940 return error_mark_node;
4943 tmpl = TI_TEMPLATE (tinfo);
4945 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4946 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4947 && DECL_TEMPLATE_SPECIALIZATION (decl)
4948 && DECL_MEMBER_TEMPLATE_P (tmpl))
4950 tree new_tmpl;
4952 /* The declaration is a specialization of a member
4953 template, declared outside the class. Therefore, the
4954 innermost template arguments will be NULL, so we
4955 replace them with the arguments determined by the
4956 earlier call to check_explicit_specialization. */
4957 args = DECL_TI_ARGS (decl);
4959 new_tmpl
4960 = build_template_decl (decl, current_template_parms,
4961 member_template_p);
4962 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4963 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4964 DECL_TI_TEMPLATE (decl) = new_tmpl;
4965 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4966 DECL_TEMPLATE_INFO (new_tmpl)
4967 = build_template_info (tmpl, args);
4969 register_specialization (new_tmpl,
4970 most_general_template (tmpl),
4971 args,
4972 is_friend, 0);
4973 return decl;
4976 /* Make sure the template headers we got make sense. */
4978 parms = DECL_TEMPLATE_PARMS (tmpl);
4979 i = TMPL_PARMS_DEPTH (parms);
4980 if (TMPL_ARGS_DEPTH (args) != i)
4982 error ("expected %d levels of template parms for %q#D, got %d",
4983 i, decl, TMPL_ARGS_DEPTH (args));
4984 DECL_INTERFACE_KNOWN (decl) = 1;
4985 return error_mark_node;
4987 else
4988 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4990 a = TMPL_ARGS_LEVEL (args, i);
4991 t = INNERMOST_TEMPLATE_PARMS (parms);
4993 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4995 if (current == decl)
4996 error ("got %d template parameters for %q#D",
4997 TREE_VEC_LENGTH (a), decl);
4998 else
4999 error ("got %d template parameters for %q#T",
5000 TREE_VEC_LENGTH (a), current);
5001 error (" but %d required", TREE_VEC_LENGTH (t));
5002 /* Avoid crash in import_export_decl. */
5003 DECL_INTERFACE_KNOWN (decl) = 1;
5004 return error_mark_node;
5007 if (current == decl)
5008 current = ctx;
5009 else if (current == NULL_TREE)
5010 /* Can happen in erroneous input. */
5011 break;
5012 else
5013 current = get_containing_scope (current);
5016 /* Check that the parms are used in the appropriate qualifying scopes
5017 in the declarator. */
5018 if (!comp_template_args
5019 (TI_ARGS (tinfo),
5020 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5022 error ("\
5023 template arguments to %qD do not match original template %qD",
5024 decl, DECL_TEMPLATE_RESULT (tmpl));
5025 if (!uses_template_parms (TI_ARGS (tinfo)))
5026 inform (input_location, "use template<> for an explicit specialization");
5027 /* Avoid crash in import_export_decl. */
5028 DECL_INTERFACE_KNOWN (decl) = 1;
5029 return error_mark_node;
5033 DECL_TEMPLATE_RESULT (tmpl) = decl;
5034 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5036 /* Push template declarations for global functions and types. Note
5037 that we do not try to push a global template friend declared in a
5038 template class; such a thing may well depend on the template
5039 parameters of the class. */
5040 if (new_template_p && !ctx
5041 && !(is_friend && template_class_depth (current_class_type) > 0))
5043 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5044 if (tmpl == error_mark_node)
5045 return error_mark_node;
5047 /* Hide template friend classes that haven't been declared yet. */
5048 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5050 DECL_ANTICIPATED (tmpl) = 1;
5051 DECL_FRIEND_P (tmpl) = 1;
5055 if (is_primary)
5057 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5058 int i;
5060 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5061 if (DECL_CONV_FN_P (tmpl))
5063 int depth = TMPL_PARMS_DEPTH (parms);
5065 /* It is a conversion operator. See if the type converted to
5066 depends on innermost template operands. */
5068 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5069 depth))
5070 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5073 /* Give template template parms a DECL_CONTEXT of the template
5074 for which they are a parameter. */
5075 parms = INNERMOST_TEMPLATE_PARMS (parms);
5076 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5078 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5079 if (TREE_CODE (parm) == TEMPLATE_DECL)
5080 DECL_CONTEXT (parm) = tmpl;
5084 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5085 back to its most general template. If TMPL is a specialization,
5086 ARGS may only have the innermost set of arguments. Add the missing
5087 argument levels if necessary. */
5088 if (DECL_TEMPLATE_INFO (tmpl))
5089 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5091 info = build_template_info (tmpl, args);
5093 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5094 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5095 else
5097 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5098 retrofit_lang_decl (decl);
5099 if (DECL_LANG_SPECIFIC (decl))
5100 DECL_TEMPLATE_INFO (decl) = info;
5103 if (flag_implicit_templates
5104 && !is_friend
5105 && TREE_PUBLIC (decl)
5106 && VAR_OR_FUNCTION_DECL_P (decl))
5107 /* Set DECL_COMDAT on template instantiations; if we force
5108 them to be emitted by explicit instantiation or -frepo,
5109 mark_needed will tell cgraph to do the right thing. */
5110 DECL_COMDAT (decl) = true;
5112 return DECL_TEMPLATE_RESULT (tmpl);
5115 tree
5116 push_template_decl (tree decl)
5118 return push_template_decl_real (decl, false);
5121 /* FN is an inheriting constructor that inherits from the constructor
5122 template INHERITED; turn FN into a constructor template with a matching
5123 template header. */
5125 tree
5126 add_inherited_template_parms (tree fn, tree inherited)
5128 tree inner_parms
5129 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5130 inner_parms = copy_node (inner_parms);
5131 tree parms
5132 = tree_cons (size_int (processing_template_decl + 1),
5133 inner_parms, current_template_parms);
5134 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5135 tree args = template_parms_to_args (parms);
5136 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5137 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5138 DECL_TEMPLATE_RESULT (tmpl) = fn;
5139 DECL_ARTIFICIAL (tmpl) = true;
5140 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5141 return tmpl;
5144 /* Called when a class template TYPE is redeclared with the indicated
5145 template PARMS, e.g.:
5147 template <class T> struct S;
5148 template <class T> struct S {}; */
5150 bool
5151 redeclare_class_template (tree type, tree parms)
5153 tree tmpl;
5154 tree tmpl_parms;
5155 int i;
5157 if (!TYPE_TEMPLATE_INFO (type))
5159 error ("%qT is not a template type", type);
5160 return false;
5163 tmpl = TYPE_TI_TEMPLATE (type);
5164 if (!PRIMARY_TEMPLATE_P (tmpl))
5165 /* The type is nested in some template class. Nothing to worry
5166 about here; there are no new template parameters for the nested
5167 type. */
5168 return true;
5170 if (!parms)
5172 error ("template specifiers not specified in declaration of %qD",
5173 tmpl);
5174 return false;
5177 parms = INNERMOST_TEMPLATE_PARMS (parms);
5178 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5180 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5182 error_n (input_location, TREE_VEC_LENGTH (parms),
5183 "redeclared with %d template parameter",
5184 "redeclared with %d template parameters",
5185 TREE_VEC_LENGTH (parms));
5186 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5187 "previous declaration %q+D used %d template parameter",
5188 "previous declaration %q+D used %d template parameters",
5189 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5190 return false;
5193 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5195 tree tmpl_parm;
5196 tree parm;
5197 tree tmpl_default;
5198 tree parm_default;
5200 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5201 || TREE_VEC_ELT (parms, i) == error_mark_node)
5202 continue;
5204 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5205 if (error_operand_p (tmpl_parm))
5206 return false;
5208 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5209 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5210 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5212 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5213 TEMPLATE_DECL. */
5214 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5215 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5216 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5217 || (TREE_CODE (tmpl_parm) != PARM_DECL
5218 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5219 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5220 || (TREE_CODE (tmpl_parm) == PARM_DECL
5221 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5222 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5224 error ("template parameter %q+#D", tmpl_parm);
5225 error ("redeclared here as %q#D", parm);
5226 return false;
5229 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5231 /* We have in [temp.param]:
5233 A template-parameter may not be given default arguments
5234 by two different declarations in the same scope. */
5235 error_at (input_location, "redefinition of default argument for %q#D", parm);
5236 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5237 "original definition appeared here");
5238 return false;
5241 if (parm_default != NULL_TREE)
5242 /* Update the previous template parameters (which are the ones
5243 that will really count) with the new default value. */
5244 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5245 else if (tmpl_default != NULL_TREE)
5246 /* Update the new parameters, too; they'll be used as the
5247 parameters for any members. */
5248 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5251 return true;
5254 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5255 to be used when the caller has already checked
5256 (processing_template_decl
5257 && !instantiation_dependent_expression_p (expr)
5258 && potential_constant_expression (expr))
5259 and cleared processing_template_decl. */
5261 tree
5262 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5264 return tsubst_copy_and_build (expr,
5265 /*args=*/NULL_TREE,
5266 complain,
5267 /*in_decl=*/NULL_TREE,
5268 /*function_p=*/false,
5269 /*integral_constant_expression_p=*/true);
5272 /* Simplify EXPR if it is a non-dependent expression. Returns the
5273 (possibly simplified) expression. */
5275 tree
5276 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5278 if (expr == NULL_TREE)
5279 return NULL_TREE;
5281 /* If we're in a template, but EXPR isn't value dependent, simplify
5282 it. We're supposed to treat:
5284 template <typename T> void f(T[1 + 1]);
5285 template <typename T> void f(T[2]);
5287 as two declarations of the same function, for example. */
5288 if (processing_template_decl
5289 && !instantiation_dependent_expression_p (expr)
5290 && potential_constant_expression (expr))
5292 processing_template_decl_sentinel s;
5293 expr = instantiate_non_dependent_expr_internal (expr, complain);
5295 return expr;
5298 tree
5299 instantiate_non_dependent_expr (tree expr)
5301 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5304 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5305 template declaration, or a TYPE_DECL for an alias declaration. */
5307 bool
5308 alias_type_or_template_p (tree t)
5310 if (t == NULL_TREE)
5311 return false;
5312 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5313 || (TYPE_P (t)
5314 && TYPE_NAME (t)
5315 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5316 || DECL_ALIAS_TEMPLATE_P (t));
5319 /* Return TRUE iff T is a specialization of an alias template. */
5321 bool
5322 alias_template_specialization_p (const_tree t)
5324 /* It's an alias template specialization if it's an alias and its
5325 TYPE_NAME is a specialization of a primary template. */
5326 if (TYPE_ALIAS_P (t))
5328 tree name = TYPE_NAME (t);
5329 if (DECL_LANG_SPECIFIC (name))
5330 if (tree ti = DECL_TEMPLATE_INFO (name))
5332 tree tmpl = TI_TEMPLATE (ti);
5333 return PRIMARY_TEMPLATE_P (tmpl);
5336 return false;
5339 /* Return TRUE iff T is a specialization of an alias template with
5340 dependent template-arguments. */
5342 bool
5343 dependent_alias_template_spec_p (const_tree t)
5345 return (alias_template_specialization_p (t)
5346 && (any_dependent_template_arguments_p
5347 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5350 /* Return the number of innermost template parameters in TMPL. */
5352 static int
5353 num_innermost_template_parms (tree tmpl)
5355 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5356 return TREE_VEC_LENGTH (parms);
5359 /* Return either TMPL or another template that it is equivalent to under DR
5360 1286: An alias that just changes the name of a template is equivalent to
5361 the other template. */
5363 static tree
5364 get_underlying_template (tree tmpl)
5366 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5367 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5369 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5370 if (TYPE_TEMPLATE_INFO (result))
5372 tree sub = TYPE_TI_TEMPLATE (result);
5373 if (PRIMARY_TEMPLATE_P (sub)
5374 && (num_innermost_template_parms (tmpl)
5375 == num_innermost_template_parms (sub)))
5377 tree alias_args = INNERMOST_TEMPLATE_ARGS
5378 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5379 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5380 break;
5381 /* The alias type is equivalent to the pattern of the
5382 underlying template, so strip the alias. */
5383 tmpl = sub;
5384 continue;
5387 break;
5389 return tmpl;
5392 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5393 must be a function or a pointer-to-function type, as specified
5394 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5395 and check that the resulting function has external linkage. */
5397 static tree
5398 convert_nontype_argument_function (tree type, tree expr,
5399 tsubst_flags_t complain)
5401 tree fns = expr;
5402 tree fn, fn_no_ptr;
5403 linkage_kind linkage;
5405 fn = instantiate_type (type, fns, tf_none);
5406 if (fn == error_mark_node)
5407 return error_mark_node;
5409 fn_no_ptr = fn;
5410 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5411 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5412 if (BASELINK_P (fn_no_ptr))
5413 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5415 /* [temp.arg.nontype]/1
5417 A template-argument for a non-type, non-template template-parameter
5418 shall be one of:
5419 [...]
5420 -- the address of an object or function with external [C++11: or
5421 internal] linkage. */
5423 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5425 if (complain & tf_error)
5427 error ("%qE is not a valid template argument for type %qT",
5428 expr, type);
5429 if (TYPE_PTR_P (type))
5430 error ("it must be the address of a function with "
5431 "external linkage");
5432 else
5433 error ("it must be the name of a function with "
5434 "external linkage");
5436 return NULL_TREE;
5439 linkage = decl_linkage (fn_no_ptr);
5440 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5442 if (complain & tf_error)
5444 if (cxx_dialect >= cxx11)
5445 error ("%qE is not a valid template argument for type %qT "
5446 "because %qD has no linkage",
5447 expr, type, fn_no_ptr);
5448 else
5449 error ("%qE is not a valid template argument for type %qT "
5450 "because %qD does not have external linkage",
5451 expr, type, fn_no_ptr);
5453 return NULL_TREE;
5456 return fn;
5459 /* Subroutine of convert_nontype_argument.
5460 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5461 Emit an error otherwise. */
5463 static bool
5464 check_valid_ptrmem_cst_expr (tree type, tree expr,
5465 tsubst_flags_t complain)
5467 STRIP_NOPS (expr);
5468 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5469 return true;
5470 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5471 return true;
5472 if (processing_template_decl
5473 && TREE_CODE (expr) == ADDR_EXPR
5474 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5475 return true;
5476 if (complain & tf_error)
5478 error ("%qE is not a valid template argument for type %qT",
5479 expr, type);
5480 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5482 return false;
5485 /* Returns TRUE iff the address of OP is value-dependent.
5487 14.6.2.4 [temp.dep.temp]:
5488 A non-integral non-type template-argument is dependent if its type is
5489 dependent or it has either of the following forms
5490 qualified-id
5491 & qualified-id
5492 and contains a nested-name-specifier which specifies a class-name that
5493 names a dependent type.
5495 We generalize this to just say that the address of a member of a
5496 dependent class is value-dependent; the above doesn't cover the
5497 address of a static data member named with an unqualified-id. */
5499 static bool
5500 has_value_dependent_address (tree op)
5502 /* We could use get_inner_reference here, but there's no need;
5503 this is only relevant for template non-type arguments, which
5504 can only be expressed as &id-expression. */
5505 if (DECL_P (op))
5507 tree ctx = CP_DECL_CONTEXT (op);
5508 if (TYPE_P (ctx) && dependent_type_p (ctx))
5509 return true;
5512 return false;
5515 /* The next set of functions are used for providing helpful explanatory
5516 diagnostics for failed overload resolution. Their messages should be
5517 indented by two spaces for consistency with the messages in
5518 call.c */
5520 static int
5521 unify_success (bool /*explain_p*/)
5523 return 0;
5526 static int
5527 unify_parameter_deduction_failure (bool explain_p, tree parm)
5529 if (explain_p)
5530 inform (input_location,
5531 " couldn't deduce template parameter %qD", parm);
5532 return 1;
5535 static int
5536 unify_invalid (bool /*explain_p*/)
5538 return 1;
5541 static int
5542 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5544 if (explain_p)
5545 inform (input_location,
5546 " types %qT and %qT have incompatible cv-qualifiers",
5547 parm, arg);
5548 return 1;
5551 static int
5552 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5554 if (explain_p)
5555 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5556 return 1;
5559 static int
5560 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5562 if (explain_p)
5563 inform (input_location,
5564 " template parameter %qD is not a parameter pack, but "
5565 "argument %qD is",
5566 parm, arg);
5567 return 1;
5570 static int
5571 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5573 if (explain_p)
5574 inform (input_location,
5575 " template argument %qE does not match "
5576 "pointer-to-member constant %qE",
5577 arg, parm);
5578 return 1;
5581 static int
5582 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5584 if (explain_p)
5585 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5586 return 1;
5589 static int
5590 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5592 if (explain_p)
5593 inform (input_location,
5594 " inconsistent parameter pack deduction with %qT and %qT",
5595 old_arg, new_arg);
5596 return 1;
5599 static int
5600 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5602 if (explain_p)
5604 if (TYPE_P (parm))
5605 inform (input_location,
5606 " deduced conflicting types for parameter %qT (%qT and %qT)",
5607 parm, first, second);
5608 else
5609 inform (input_location,
5610 " deduced conflicting values for non-type parameter "
5611 "%qE (%qE and %qE)", parm, first, second);
5613 return 1;
5616 static int
5617 unify_vla_arg (bool explain_p, tree arg)
5619 if (explain_p)
5620 inform (input_location,
5621 " variable-sized array type %qT is not "
5622 "a valid template argument",
5623 arg);
5624 return 1;
5627 static int
5628 unify_method_type_error (bool explain_p, tree arg)
5630 if (explain_p)
5631 inform (input_location,
5632 " member function type %qT is not a valid template argument",
5633 arg);
5634 return 1;
5637 static int
5638 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
5640 if (explain_p)
5642 if (least_p)
5643 inform_n (input_location, wanted,
5644 " candidate expects at least %d argument, %d provided",
5645 " candidate expects at least %d arguments, %d provided",
5646 wanted, have);
5647 else
5648 inform_n (input_location, wanted,
5649 " candidate expects %d argument, %d provided",
5650 " candidate expects %d arguments, %d provided",
5651 wanted, have);
5653 return 1;
5656 static int
5657 unify_too_many_arguments (bool explain_p, int have, int wanted)
5659 return unify_arity (explain_p, have, wanted);
5662 static int
5663 unify_too_few_arguments (bool explain_p, int have, int wanted,
5664 bool least_p = false)
5666 return unify_arity (explain_p, have, wanted, least_p);
5669 static int
5670 unify_arg_conversion (bool explain_p, tree to_type,
5671 tree from_type, tree arg)
5673 if (explain_p)
5674 inform (EXPR_LOC_OR_LOC (arg, input_location),
5675 " cannot convert %qE (type %qT) to type %qT",
5676 arg, from_type, to_type);
5677 return 1;
5680 static int
5681 unify_no_common_base (bool explain_p, enum template_base_result r,
5682 tree parm, tree arg)
5684 if (explain_p)
5685 switch (r)
5687 case tbr_ambiguous_baseclass:
5688 inform (input_location, " %qT is an ambiguous base class of %qT",
5689 parm, arg);
5690 break;
5691 default:
5692 inform (input_location, " %qT is not derived from %qT", arg, parm);
5693 break;
5695 return 1;
5698 static int
5699 unify_inconsistent_template_template_parameters (bool explain_p)
5701 if (explain_p)
5702 inform (input_location,
5703 " template parameters of a template template argument are "
5704 "inconsistent with other deduced template arguments");
5705 return 1;
5708 static int
5709 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5711 if (explain_p)
5712 inform (input_location,
5713 " can't deduce a template for %qT from non-template type %qT",
5714 parm, arg);
5715 return 1;
5718 static int
5719 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5721 if (explain_p)
5722 inform (input_location,
5723 " template argument %qE does not match %qD", arg, parm);
5724 return 1;
5727 static int
5728 unify_overload_resolution_failure (bool explain_p, tree arg)
5730 if (explain_p)
5731 inform (input_location,
5732 " could not resolve address from overloaded function %qE",
5733 arg);
5734 return 1;
5737 /* Attempt to convert the non-type template parameter EXPR to the
5738 indicated TYPE. If the conversion is successful, return the
5739 converted value. If the conversion is unsuccessful, return
5740 NULL_TREE if we issued an error message, or error_mark_node if we
5741 did not. We issue error messages for out-and-out bad template
5742 parameters, but not simply because the conversion failed, since we
5743 might be just trying to do argument deduction. Both TYPE and EXPR
5744 must be non-dependent.
5746 The conversion follows the special rules described in
5747 [temp.arg.nontype], and it is much more strict than an implicit
5748 conversion.
5750 This function is called twice for each template argument (see
5751 lookup_template_class for a more accurate description of this
5752 problem). This means that we need to handle expressions which
5753 are not valid in a C++ source, but can be created from the
5754 first call (for instance, casts to perform conversions). These
5755 hacks can go away after we fix the double coercion problem. */
5757 static tree
5758 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5760 tree expr_type;
5762 /* Detect immediately string literals as invalid non-type argument.
5763 This special-case is not needed for correctness (we would easily
5764 catch this later), but only to provide better diagnostic for this
5765 common user mistake. As suggested by DR 100, we do not mention
5766 linkage issues in the diagnostic as this is not the point. */
5767 /* FIXME we're making this OK. */
5768 if (TREE_CODE (expr) == STRING_CST)
5770 if (complain & tf_error)
5771 error ("%qE is not a valid template argument for type %qT "
5772 "because string literals can never be used in this context",
5773 expr, type);
5774 return NULL_TREE;
5777 /* Add the ADDR_EXPR now for the benefit of
5778 value_dependent_expression_p. */
5779 if (TYPE_PTROBV_P (type)
5780 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5782 expr = decay_conversion (expr, complain);
5783 if (expr == error_mark_node)
5784 return error_mark_node;
5787 /* If we are in a template, EXPR may be non-dependent, but still
5788 have a syntactic, rather than semantic, form. For example, EXPR
5789 might be a SCOPE_REF, rather than the VAR_DECL to which the
5790 SCOPE_REF refers. Preserving the qualifying scope is necessary
5791 so that access checking can be performed when the template is
5792 instantiated -- but here we need the resolved form so that we can
5793 convert the argument. */
5794 bool non_dep = false;
5795 if (TYPE_REF_OBJ_P (type)
5796 && has_value_dependent_address (expr))
5797 /* If we want the address and it's value-dependent, don't fold. */;
5798 else if (!type_unknown_p (expr)
5799 && processing_template_decl
5800 && !instantiation_dependent_expression_p (expr)
5801 && potential_constant_expression (expr))
5802 non_dep = true;
5803 if (error_operand_p (expr))
5804 return error_mark_node;
5805 expr_type = TREE_TYPE (expr);
5806 if (TREE_CODE (type) == REFERENCE_TYPE)
5807 expr = mark_lvalue_use (expr);
5808 else
5809 expr = mark_rvalue_use (expr);
5811 /* If the argument is non-dependent, perform any conversions in
5812 non-dependent context as well. */
5813 processing_template_decl_sentinel s (non_dep);
5814 if (non_dep)
5815 expr = instantiate_non_dependent_expr_internal (expr, complain);
5817 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5818 to a non-type argument of "nullptr". */
5819 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5820 expr = convert (type, expr);
5822 /* In C++11, integral or enumeration non-type template arguments can be
5823 arbitrary constant expressions. Pointer and pointer to
5824 member arguments can be general constant expressions that evaluate
5825 to a null value, but otherwise still need to be of a specific form. */
5826 if (cxx_dialect >= cxx11)
5828 if (TREE_CODE (expr) == PTRMEM_CST)
5829 /* A PTRMEM_CST is already constant, and a valid template
5830 argument for a parameter of pointer to member type, we just want
5831 to leave it in that form rather than lower it to a
5832 CONSTRUCTOR. */;
5833 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5834 expr = maybe_constant_value (expr);
5835 else if (TYPE_PTR_OR_PTRMEM_P (type))
5837 tree folded = maybe_constant_value (expr);
5838 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5839 : null_member_pointer_value_p (folded))
5840 expr = folded;
5844 /* HACK: Due to double coercion, we can get a
5845 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5846 which is the tree that we built on the first call (see
5847 below when coercing to reference to object or to reference to
5848 function). We just strip everything and get to the arg.
5849 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5850 for examples. */
5851 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5853 tree probe_type, probe = expr;
5854 if (REFERENCE_REF_P (probe))
5855 probe = TREE_OPERAND (probe, 0);
5856 probe_type = TREE_TYPE (probe);
5857 if (TREE_CODE (probe) == NOP_EXPR)
5859 /* ??? Maybe we could use convert_from_reference here, but we
5860 would need to relax its constraints because the NOP_EXPR
5861 could actually change the type to something more cv-qualified,
5862 and this is not folded by convert_from_reference. */
5863 tree addr = TREE_OPERAND (probe, 0);
5864 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5865 && TREE_CODE (addr) == ADDR_EXPR
5866 && TYPE_PTR_P (TREE_TYPE (addr))
5867 && (same_type_ignoring_top_level_qualifiers_p
5868 (TREE_TYPE (probe_type),
5869 TREE_TYPE (TREE_TYPE (addr)))))
5871 expr = TREE_OPERAND (addr, 0);
5872 expr_type = TREE_TYPE (probe_type);
5877 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5878 parameter is a pointer to object, through decay and
5879 qualification conversion. Let's strip everything. */
5880 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5882 tree probe = expr;
5883 STRIP_NOPS (probe);
5884 if (TREE_CODE (probe) == ADDR_EXPR
5885 && TYPE_PTR_P (TREE_TYPE (probe)))
5887 /* Skip the ADDR_EXPR only if it is part of the decay for
5888 an array. Otherwise, it is part of the original argument
5889 in the source code. */
5890 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5891 probe = TREE_OPERAND (probe, 0);
5892 expr = probe;
5893 expr_type = TREE_TYPE (expr);
5897 /* [temp.arg.nontype]/5, bullet 1
5899 For a non-type template-parameter of integral or enumeration type,
5900 integral promotions (_conv.prom_) and integral conversions
5901 (_conv.integral_) are applied. */
5902 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5904 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5905 t = maybe_constant_value (t);
5906 if (t != error_mark_node)
5907 expr = t;
5909 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5910 return error_mark_node;
5912 /* Notice that there are constant expressions like '4 % 0' which
5913 do not fold into integer constants. */
5914 if (TREE_CODE (expr) != INTEGER_CST)
5916 if (complain & tf_error)
5918 int errs = errorcount, warns = warningcount + werrorcount;
5919 if (processing_template_decl
5920 && !require_potential_constant_expression (expr))
5921 return NULL_TREE;
5922 expr = cxx_constant_value (expr);
5923 if (errorcount > errs || warningcount + werrorcount > warns)
5924 inform (EXPR_LOC_OR_LOC (expr, input_location),
5925 "in template argument for type %qT ", type);
5926 if (expr == error_mark_node)
5927 return NULL_TREE;
5928 /* else cxx_constant_value complained but gave us
5929 a real constant, so go ahead. */
5930 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5932 else
5933 return NULL_TREE;
5936 /* Avoid typedef problems. */
5937 if (TREE_TYPE (expr) != type)
5938 expr = fold_convert (type, expr);
5940 /* [temp.arg.nontype]/5, bullet 2
5942 For a non-type template-parameter of type pointer to object,
5943 qualification conversions (_conv.qual_) and the array-to-pointer
5944 conversion (_conv.array_) are applied. */
5945 else if (TYPE_PTROBV_P (type))
5947 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5949 A template-argument for a non-type, non-template template-parameter
5950 shall be one of: [...]
5952 -- the name of a non-type template-parameter;
5953 -- the address of an object or function with external linkage, [...]
5954 expressed as "& id-expression" where the & is optional if the name
5955 refers to a function or array, or if the corresponding
5956 template-parameter is a reference.
5958 Here, we do not care about functions, as they are invalid anyway
5959 for a parameter of type pointer-to-object. */
5961 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5962 /* Non-type template parameters are OK. */
5964 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5965 /* Null pointer values are OK in C++11. */;
5966 else if (TREE_CODE (expr) != ADDR_EXPR
5967 && TREE_CODE (expr_type) != ARRAY_TYPE)
5969 if (VAR_P (expr))
5971 if (complain & tf_error)
5972 error ("%qD is not a valid template argument "
5973 "because %qD is a variable, not the address of "
5974 "a variable", expr, expr);
5975 return NULL_TREE;
5977 if (POINTER_TYPE_P (expr_type))
5979 if (complain & tf_error)
5980 error ("%qE is not a valid template argument for %qT "
5981 "because it is not the address of a variable",
5982 expr, type);
5983 return NULL_TREE;
5985 /* Other values, like integer constants, might be valid
5986 non-type arguments of some other type. */
5987 return error_mark_node;
5989 else
5991 tree decl;
5993 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5994 ? TREE_OPERAND (expr, 0) : expr);
5995 if (!VAR_P (decl))
5997 if (complain & tf_error)
5998 error ("%qE is not a valid template argument of type %qT "
5999 "because %qE is not a variable", expr, type, decl);
6000 return NULL_TREE;
6002 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6004 if (complain & tf_error)
6005 error ("%qE is not a valid template argument of type %qT "
6006 "because %qD does not have external linkage",
6007 expr, type, decl);
6008 return NULL_TREE;
6010 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6012 if (complain & tf_error)
6013 error ("%qE is not a valid template argument of type %qT "
6014 "because %qD has no linkage", expr, type, decl);
6015 return NULL_TREE;
6019 expr = decay_conversion (expr, complain);
6020 if (expr == error_mark_node)
6021 return error_mark_node;
6023 expr = perform_qualification_conversions (type, expr);
6024 if (expr == error_mark_node)
6025 return error_mark_node;
6027 /* [temp.arg.nontype]/5, bullet 3
6029 For a non-type template-parameter of type reference to object, no
6030 conversions apply. The type referred to by the reference may be more
6031 cv-qualified than the (otherwise identical) type of the
6032 template-argument. The template-parameter is bound directly to the
6033 template-argument, which must be an lvalue. */
6034 else if (TYPE_REF_OBJ_P (type))
6036 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6037 expr_type))
6038 return error_mark_node;
6040 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6042 if (complain & tf_error)
6043 error ("%qE is not a valid template argument for type %qT "
6044 "because of conflicts in cv-qualification", expr, type);
6045 return NULL_TREE;
6048 if (!real_lvalue_p (expr))
6050 if (complain & tf_error)
6051 error ("%qE is not a valid template argument for type %qT "
6052 "because it is not an lvalue", expr, type);
6053 return NULL_TREE;
6056 /* [temp.arg.nontype]/1
6058 A template-argument for a non-type, non-template template-parameter
6059 shall be one of: [...]
6061 -- the address of an object or function with external linkage. */
6062 if (INDIRECT_REF_P (expr)
6063 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6065 expr = TREE_OPERAND (expr, 0);
6066 if (DECL_P (expr))
6068 if (complain & tf_error)
6069 error ("%q#D is not a valid template argument for type %qT "
6070 "because a reference variable does not have a constant "
6071 "address", expr, type);
6072 return NULL_TREE;
6076 if (!DECL_P (expr))
6078 if (complain & tf_error)
6079 error ("%qE is not a valid template argument for type %qT "
6080 "because it is not an object with external linkage",
6081 expr, type);
6082 return NULL_TREE;
6085 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6087 if (complain & tf_error)
6088 error ("%qE is not a valid template argument for type %qT "
6089 "because object %qD has not external linkage",
6090 expr, type, expr);
6091 return NULL_TREE;
6094 expr = build_nop (type, build_address (expr));
6096 /* [temp.arg.nontype]/5, bullet 4
6098 For a non-type template-parameter of type pointer to function, only
6099 the function-to-pointer conversion (_conv.func_) is applied. If the
6100 template-argument represents a set of overloaded functions (or a
6101 pointer to such), the matching function is selected from the set
6102 (_over.over_). */
6103 else if (TYPE_PTRFN_P (type))
6105 /* If the argument is a template-id, we might not have enough
6106 context information to decay the pointer. */
6107 if (!type_unknown_p (expr_type))
6109 expr = decay_conversion (expr, complain);
6110 if (expr == error_mark_node)
6111 return error_mark_node;
6114 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6115 /* Null pointer values are OK in C++11. */
6116 return perform_qualification_conversions (type, expr);
6118 expr = convert_nontype_argument_function (type, expr, complain);
6119 if (!expr || expr == error_mark_node)
6120 return expr;
6122 /* [temp.arg.nontype]/5, bullet 5
6124 For a non-type template-parameter of type reference to function, no
6125 conversions apply. If the template-argument represents a set of
6126 overloaded functions, the matching function is selected from the set
6127 (_over.over_). */
6128 else if (TYPE_REFFN_P (type))
6130 if (TREE_CODE (expr) == ADDR_EXPR)
6132 if (complain & tf_error)
6134 error ("%qE is not a valid template argument for type %qT "
6135 "because it is a pointer", expr, type);
6136 inform (input_location, "try using %qE instead",
6137 TREE_OPERAND (expr, 0));
6139 return NULL_TREE;
6142 expr = convert_nontype_argument_function (type, expr, complain);
6143 if (!expr || expr == error_mark_node)
6144 return expr;
6146 expr = build_nop (type, build_address (expr));
6148 /* [temp.arg.nontype]/5, bullet 6
6150 For a non-type template-parameter of type pointer to member function,
6151 no conversions apply. If the template-argument represents a set of
6152 overloaded member functions, the matching member function is selected
6153 from the set (_over.over_). */
6154 else if (TYPE_PTRMEMFUNC_P (type))
6156 expr = instantiate_type (type, expr, tf_none);
6157 if (expr == error_mark_node)
6158 return error_mark_node;
6160 /* [temp.arg.nontype] bullet 1 says the pointer to member
6161 expression must be a pointer-to-member constant. */
6162 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6163 return error_mark_node;
6165 /* There is no way to disable standard conversions in
6166 resolve_address_of_overloaded_function (called by
6167 instantiate_type). It is possible that the call succeeded by
6168 converting &B::I to &D::I (where B is a base of D), so we need
6169 to reject this conversion here.
6171 Actually, even if there was a way to disable standard conversions,
6172 it would still be better to reject them here so that we can
6173 provide a superior diagnostic. */
6174 if (!same_type_p (TREE_TYPE (expr), type))
6176 if (complain & tf_error)
6178 error ("%qE is not a valid template argument for type %qT "
6179 "because it is of type %qT", expr, type,
6180 TREE_TYPE (expr));
6181 /* If we are just one standard conversion off, explain. */
6182 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6183 inform (input_location,
6184 "standard conversions are not allowed in this context");
6186 return NULL_TREE;
6189 /* [temp.arg.nontype]/5, bullet 7
6191 For a non-type template-parameter of type pointer to data member,
6192 qualification conversions (_conv.qual_) are applied. */
6193 else if (TYPE_PTRDATAMEM_P (type))
6195 /* [temp.arg.nontype] bullet 1 says the pointer to member
6196 expression must be a pointer-to-member constant. */
6197 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6198 return error_mark_node;
6200 expr = perform_qualification_conversions (type, expr);
6201 if (expr == error_mark_node)
6202 return expr;
6204 else if (NULLPTR_TYPE_P (type))
6206 if (expr != nullptr_node)
6208 if (complain & tf_error)
6209 error ("%qE is not a valid template argument for type %qT "
6210 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6211 return NULL_TREE;
6213 return expr;
6215 /* A template non-type parameter must be one of the above. */
6216 else
6217 gcc_unreachable ();
6219 /* Sanity check: did we actually convert the argument to the
6220 right type? */
6221 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6222 (type, TREE_TYPE (expr)));
6223 return convert_from_reference (expr);
6226 /* Subroutine of coerce_template_template_parms, which returns 1 if
6227 PARM_PARM and ARG_PARM match using the rule for the template
6228 parameters of template template parameters. Both PARM and ARG are
6229 template parameters; the rest of the arguments are the same as for
6230 coerce_template_template_parms.
6232 static int
6233 coerce_template_template_parm (tree parm,
6234 tree arg,
6235 tsubst_flags_t complain,
6236 tree in_decl,
6237 tree outer_args)
6239 if (arg == NULL_TREE || error_operand_p (arg)
6240 || parm == NULL_TREE || error_operand_p (parm))
6241 return 0;
6243 if (TREE_CODE (arg) != TREE_CODE (parm))
6244 return 0;
6246 switch (TREE_CODE (parm))
6248 case TEMPLATE_DECL:
6249 /* We encounter instantiations of templates like
6250 template <template <template <class> class> class TT>
6251 class C; */
6253 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6254 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6256 if (!coerce_template_template_parms
6257 (parmparm, argparm, complain, in_decl, outer_args))
6258 return 0;
6260 /* Fall through. */
6262 case TYPE_DECL:
6263 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6264 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6265 /* Argument is a parameter pack but parameter is not. */
6266 return 0;
6267 break;
6269 case PARM_DECL:
6270 /* The tsubst call is used to handle cases such as
6272 template <int> class C {};
6273 template <class T, template <T> class TT> class D {};
6274 D<int, C> d;
6276 i.e. the parameter list of TT depends on earlier parameters. */
6277 if (!uses_template_parms (TREE_TYPE (arg))
6278 && !same_type_p
6279 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6280 TREE_TYPE (arg)))
6281 return 0;
6283 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6284 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6285 /* Argument is a parameter pack but parameter is not. */
6286 return 0;
6288 break;
6290 default:
6291 gcc_unreachable ();
6294 return 1;
6298 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6299 template template parameters. Both PARM_PARMS and ARG_PARMS are
6300 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6301 or PARM_DECL.
6303 Consider the example:
6304 template <class T> class A;
6305 template<template <class U> class TT> class B;
6307 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6308 the parameters to A, and OUTER_ARGS contains A. */
6310 static int
6311 coerce_template_template_parms (tree parm_parms,
6312 tree arg_parms,
6313 tsubst_flags_t complain,
6314 tree in_decl,
6315 tree outer_args)
6317 int nparms, nargs, i;
6318 tree parm, arg;
6319 int variadic_p = 0;
6321 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6322 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6324 nparms = TREE_VEC_LENGTH (parm_parms);
6325 nargs = TREE_VEC_LENGTH (arg_parms);
6327 /* Determine whether we have a parameter pack at the end of the
6328 template template parameter's template parameter list. */
6329 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6331 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6333 if (error_operand_p (parm))
6334 return 0;
6336 switch (TREE_CODE (parm))
6338 case TEMPLATE_DECL:
6339 case TYPE_DECL:
6340 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6341 variadic_p = 1;
6342 break;
6344 case PARM_DECL:
6345 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6346 variadic_p = 1;
6347 break;
6349 default:
6350 gcc_unreachable ();
6354 if (nargs != nparms
6355 && !(variadic_p && nargs >= nparms - 1))
6356 return 0;
6358 /* Check all of the template parameters except the parameter pack at
6359 the end (if any). */
6360 for (i = 0; i < nparms - variadic_p; ++i)
6362 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6363 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6364 continue;
6366 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6367 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6369 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6370 outer_args))
6371 return 0;
6375 if (variadic_p)
6377 /* Check each of the template parameters in the template
6378 argument against the template parameter pack at the end of
6379 the template template parameter. */
6380 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6381 return 0;
6383 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6385 for (; i < nargs; ++i)
6387 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6388 continue;
6390 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6392 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6393 outer_args))
6394 return 0;
6398 return 1;
6401 /* Verifies that the deduced template arguments (in TARGS) for the
6402 template template parameters (in TPARMS) represent valid bindings,
6403 by comparing the template parameter list of each template argument
6404 to the template parameter list of its corresponding template
6405 template parameter, in accordance with DR150. This
6406 routine can only be called after all template arguments have been
6407 deduced. It will return TRUE if all of the template template
6408 parameter bindings are okay, FALSE otherwise. */
6409 bool
6410 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6412 int i, ntparms = TREE_VEC_LENGTH (tparms);
6413 bool ret = true;
6415 /* We're dealing with template parms in this process. */
6416 ++processing_template_decl;
6418 targs = INNERMOST_TEMPLATE_ARGS (targs);
6420 for (i = 0; i < ntparms; ++i)
6422 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6423 tree targ = TREE_VEC_ELT (targs, i);
6425 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6427 tree packed_args = NULL_TREE;
6428 int idx, len = 1;
6430 if (ARGUMENT_PACK_P (targ))
6432 /* Look inside the argument pack. */
6433 packed_args = ARGUMENT_PACK_ARGS (targ);
6434 len = TREE_VEC_LENGTH (packed_args);
6437 for (idx = 0; idx < len; ++idx)
6439 tree targ_parms = NULL_TREE;
6441 if (packed_args)
6442 /* Extract the next argument from the argument
6443 pack. */
6444 targ = TREE_VEC_ELT (packed_args, idx);
6446 if (PACK_EXPANSION_P (targ))
6447 /* Look at the pattern of the pack expansion. */
6448 targ = PACK_EXPANSION_PATTERN (targ);
6450 /* Extract the template parameters from the template
6451 argument. */
6452 if (TREE_CODE (targ) == TEMPLATE_DECL)
6453 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6454 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6455 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6457 /* Verify that we can coerce the template template
6458 parameters from the template argument to the template
6459 parameter. This requires an exact match. */
6460 if (targ_parms
6461 && !coerce_template_template_parms
6462 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6463 targ_parms,
6464 tf_none,
6465 tparm,
6466 targs))
6468 ret = false;
6469 goto out;
6475 out:
6477 --processing_template_decl;
6478 return ret;
6481 /* Since type attributes aren't mangled, we need to strip them from
6482 template type arguments. */
6484 static tree
6485 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6487 tree mv;
6488 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6489 return arg;
6490 mv = TYPE_MAIN_VARIANT (arg);
6491 arg = strip_typedefs (arg);
6492 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6493 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6495 if (complain & tf_warning)
6496 warning (0, "ignoring attributes on template argument %qT", arg);
6497 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6498 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6500 return arg;
6503 /* Convert the indicated template ARG as necessary to match the
6504 indicated template PARM. Returns the converted ARG, or
6505 error_mark_node if the conversion was unsuccessful. Error and
6506 warning messages are issued under control of COMPLAIN. This
6507 conversion is for the Ith parameter in the parameter list. ARGS is
6508 the full set of template arguments deduced so far. */
6510 static tree
6511 convert_template_argument (tree parm,
6512 tree arg,
6513 tree args,
6514 tsubst_flags_t complain,
6515 int i,
6516 tree in_decl)
6518 tree orig_arg;
6519 tree val;
6520 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6522 if (parm == error_mark_node)
6523 return error_mark_node;
6525 if (TREE_CODE (arg) == TREE_LIST
6526 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6528 /* The template argument was the name of some
6529 member function. That's usually
6530 invalid, but static members are OK. In any
6531 case, grab the underlying fields/functions
6532 and issue an error later if required. */
6533 orig_arg = TREE_VALUE (arg);
6534 TREE_TYPE (arg) = unknown_type_node;
6537 orig_arg = arg;
6539 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6540 requires_type = (TREE_CODE (parm) == TYPE_DECL
6541 || requires_tmpl_type);
6543 /* When determining whether an argument pack expansion is a template,
6544 look at the pattern. */
6545 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6546 arg = PACK_EXPANSION_PATTERN (arg);
6548 /* Deal with an injected-class-name used as a template template arg. */
6549 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6551 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6552 if (TREE_CODE (t) == TEMPLATE_DECL)
6554 if (cxx_dialect >= cxx11)
6555 /* OK under DR 1004. */;
6556 else if (complain & tf_warning_or_error)
6557 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6558 " used as template template argument", TYPE_NAME (arg));
6559 else if (flag_pedantic_errors)
6560 t = arg;
6562 arg = t;
6566 is_tmpl_type =
6567 ((TREE_CODE (arg) == TEMPLATE_DECL
6568 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6569 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6570 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6571 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6573 if (is_tmpl_type
6574 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6575 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6576 arg = TYPE_STUB_DECL (arg);
6578 is_type = TYPE_P (arg) || is_tmpl_type;
6580 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6581 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6583 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6585 if (complain & tf_error)
6586 error ("invalid use of destructor %qE as a type", orig_arg);
6587 return error_mark_node;
6590 permerror (input_location,
6591 "to refer to a type member of a template parameter, "
6592 "use %<typename %E%>", orig_arg);
6594 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6595 TREE_OPERAND (arg, 1),
6596 typename_type,
6597 complain);
6598 arg = orig_arg;
6599 is_type = 1;
6601 if (is_type != requires_type)
6603 if (in_decl)
6605 if (complain & tf_error)
6607 error ("type/value mismatch at argument %d in template "
6608 "parameter list for %qD",
6609 i + 1, in_decl);
6610 if (is_type)
6611 inform (input_location,
6612 " expected a constant of type %qT, got %qT",
6613 TREE_TYPE (parm),
6614 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6615 else if (requires_tmpl_type)
6616 inform (input_location,
6617 " expected a class template, got %qE", orig_arg);
6618 else
6619 inform (input_location,
6620 " expected a type, got %qE", orig_arg);
6623 return error_mark_node;
6625 if (is_tmpl_type ^ requires_tmpl_type)
6627 if (in_decl && (complain & tf_error))
6629 error ("type/value mismatch at argument %d in template "
6630 "parameter list for %qD",
6631 i + 1, in_decl);
6632 if (is_tmpl_type)
6633 inform (input_location,
6634 " expected a type, got %qT", DECL_NAME (arg));
6635 else
6636 inform (input_location,
6637 " expected a class template, got %qT", orig_arg);
6639 return error_mark_node;
6642 if (is_type)
6644 if (requires_tmpl_type)
6646 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6647 val = orig_arg;
6648 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6649 /* The number of argument required is not known yet.
6650 Just accept it for now. */
6651 val = TREE_TYPE (arg);
6652 else
6654 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6655 tree argparm;
6657 /* Strip alias templates that are equivalent to another
6658 template. */
6659 arg = get_underlying_template (arg);
6660 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6662 if (coerce_template_template_parms (parmparm, argparm,
6663 complain, in_decl,
6664 args))
6666 val = arg;
6668 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6669 TEMPLATE_DECL. */
6670 if (val != error_mark_node)
6672 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6673 val = TREE_TYPE (val);
6674 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6675 val = make_pack_expansion (val);
6678 else
6680 if (in_decl && (complain & tf_error))
6682 error ("type/value mismatch at argument %d in "
6683 "template parameter list for %qD",
6684 i + 1, in_decl);
6685 inform (input_location,
6686 " expected a template of type %qD, got %qT",
6687 parm, orig_arg);
6690 val = error_mark_node;
6694 else
6695 val = orig_arg;
6696 /* We only form one instance of each template specialization.
6697 Therefore, if we use a non-canonical variant (i.e., a
6698 typedef), any future messages referring to the type will use
6699 the typedef, which is confusing if those future uses do not
6700 themselves also use the typedef. */
6701 if (TYPE_P (val))
6702 val = canonicalize_type_argument (val, complain);
6704 else
6706 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6708 if (invalid_nontype_parm_type_p (t, complain))
6709 return error_mark_node;
6711 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6713 if (same_type_p (t, TREE_TYPE (orig_arg)))
6714 val = orig_arg;
6715 else
6717 /* Not sure if this is reachable, but it doesn't hurt
6718 to be robust. */
6719 error ("type mismatch in nontype parameter pack");
6720 val = error_mark_node;
6723 else if (!dependent_template_arg_p (orig_arg)
6724 && !uses_template_parms (t))
6725 /* We used to call digest_init here. However, digest_init
6726 will report errors, which we don't want when complain
6727 is zero. More importantly, digest_init will try too
6728 hard to convert things: for example, `0' should not be
6729 converted to pointer type at this point according to
6730 the standard. Accepting this is not merely an
6731 extension, since deciding whether or not these
6732 conversions can occur is part of determining which
6733 function template to call, or whether a given explicit
6734 argument specification is valid. */
6735 val = convert_nontype_argument (t, orig_arg, complain);
6736 else
6737 val = strip_typedefs_expr (orig_arg);
6739 if (val == NULL_TREE)
6740 val = error_mark_node;
6741 else if (val == error_mark_node && (complain & tf_error))
6742 error ("could not convert template argument %qE to %qT", orig_arg, t);
6744 if (TREE_CODE (val) == SCOPE_REF)
6746 /* Strip typedefs from the SCOPE_REF. */
6747 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6748 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6749 complain);
6750 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6751 QUALIFIED_NAME_IS_TEMPLATE (val));
6755 return val;
6758 /* Coerces the remaining template arguments in INNER_ARGS (from
6759 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6760 Returns the coerced argument pack. PARM_IDX is the position of this
6761 parameter in the template parameter list. ARGS is the original
6762 template argument list. */
6763 static tree
6764 coerce_template_parameter_pack (tree parms,
6765 int parm_idx,
6766 tree args,
6767 tree inner_args,
6768 int arg_idx,
6769 tree new_args,
6770 int* lost,
6771 tree in_decl,
6772 tsubst_flags_t complain)
6774 tree parm = TREE_VEC_ELT (parms, parm_idx);
6775 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6776 tree packed_args;
6777 tree argument_pack;
6778 tree packed_parms = NULL_TREE;
6780 if (arg_idx > nargs)
6781 arg_idx = nargs;
6783 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6785 /* When the template parameter is a non-type template parameter pack
6786 or template template parameter pack whose type or template
6787 parameters use parameter packs, we know exactly how many arguments
6788 we are looking for. Build a vector of the instantiated decls for
6789 these template parameters in PACKED_PARMS. */
6790 /* We can't use make_pack_expansion here because it would interpret a
6791 _DECL as a use rather than a declaration. */
6792 tree decl = TREE_VALUE (parm);
6793 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6794 SET_PACK_EXPANSION_PATTERN (exp, decl);
6795 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6796 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6798 TREE_VEC_LENGTH (args)--;
6799 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6800 TREE_VEC_LENGTH (args)++;
6802 if (packed_parms == error_mark_node)
6803 return error_mark_node;
6805 /* If we're doing a partial instantiation of a member template,
6806 verify that all of the types used for the non-type
6807 template parameter pack are, in fact, valid for non-type
6808 template parameters. */
6809 if (arg_idx < nargs
6810 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6812 int j, len = TREE_VEC_LENGTH (packed_parms);
6813 for (j = 0; j < len; ++j)
6815 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6816 if (invalid_nontype_parm_type_p (t, complain))
6817 return error_mark_node;
6821 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6823 else
6824 packed_args = make_tree_vec (nargs - arg_idx);
6826 /* Convert the remaining arguments, which will be a part of the
6827 parameter pack "parm". */
6828 for (; arg_idx < nargs; ++arg_idx)
6830 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6831 tree actual_parm = TREE_VALUE (parm);
6832 int pack_idx = arg_idx - parm_idx;
6834 if (packed_parms)
6836 /* Once we've packed as many args as we have types, stop. */
6837 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6838 break;
6839 else if (PACK_EXPANSION_P (arg))
6840 /* We don't know how many args we have yet, just
6841 use the unconverted ones for now. */
6842 return NULL_TREE;
6843 else
6844 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6847 if (arg == error_mark_node)
6849 if (complain & tf_error)
6850 error ("template argument %d is invalid", arg_idx + 1);
6852 else
6853 arg = convert_template_argument (actual_parm,
6854 arg, new_args, complain, parm_idx,
6855 in_decl);
6856 if (arg == error_mark_node)
6857 (*lost)++;
6858 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6861 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6862 && TREE_VEC_LENGTH (packed_args) > 0)
6864 if (complain & tf_error)
6865 error ("wrong number of template arguments (%d, should be %d)",
6866 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6867 return error_mark_node;
6870 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6871 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6872 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6873 else
6875 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6876 TREE_TYPE (argument_pack)
6877 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6878 TREE_CONSTANT (argument_pack) = 1;
6881 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6882 #ifdef ENABLE_CHECKING
6883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6884 TREE_VEC_LENGTH (packed_args));
6885 #endif
6886 return argument_pack;
6889 /* Returns the number of pack expansions in the template argument vector
6890 ARGS. */
6892 static int
6893 pack_expansion_args_count (tree args)
6895 int i;
6896 int count = 0;
6897 if (args)
6898 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6900 tree elt = TREE_VEC_ELT (args, i);
6901 if (elt && PACK_EXPANSION_P (elt))
6902 ++count;
6904 return count;
6907 /* Convert all template arguments to their appropriate types, and
6908 return a vector containing the innermost resulting template
6909 arguments. If any error occurs, return error_mark_node. Error and
6910 warning messages are issued under control of COMPLAIN.
6912 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6913 for arguments not specified in ARGS. Otherwise, if
6914 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6915 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6916 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6917 ARGS. */
6919 static tree
6920 coerce_template_parms (tree parms,
6921 tree args,
6922 tree in_decl,
6923 tsubst_flags_t complain,
6924 bool require_all_args,
6925 bool use_default_args)
6927 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6928 tree orig_inner_args;
6929 tree inner_args;
6930 tree new_args;
6931 tree new_inner_args;
6932 int saved_unevaluated_operand;
6933 int saved_inhibit_evaluation_warnings;
6935 /* When used as a boolean value, indicates whether this is a
6936 variadic template parameter list. Since it's an int, we can also
6937 subtract it from nparms to get the number of non-variadic
6938 parameters. */
6939 int variadic_p = 0;
6940 int variadic_args_p = 0;
6941 int post_variadic_parms = 0;
6943 /* Likewise for parameters with default arguments. */
6944 int default_p = 0;
6946 if (args == error_mark_node)
6947 return error_mark_node;
6949 nparms = TREE_VEC_LENGTH (parms);
6951 /* Determine if there are any parameter packs or default arguments. */
6952 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6954 tree parm = TREE_VEC_ELT (parms, parm_idx);
6955 if (variadic_p)
6956 ++post_variadic_parms;
6957 if (template_parameter_pack_p (TREE_VALUE (parm)))
6958 ++variadic_p;
6959 if (TREE_PURPOSE (parm))
6960 ++default_p;
6963 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6964 /* If there are no parameters that follow a parameter pack, we need to
6965 expand any argument packs so that we can deduce a parameter pack from
6966 some non-packed args followed by an argument pack, as in variadic85.C.
6967 If there are such parameters, we need to leave argument packs intact
6968 so the arguments are assigned properly. This can happen when dealing
6969 with a nested class inside a partial specialization of a class
6970 template, as in variadic92.C, or when deducing a template parameter pack
6971 from a sub-declarator, as in variadic114.C. */
6972 if (!post_variadic_parms)
6973 inner_args = expand_template_argument_pack (inner_args);
6975 /* Count any pack expansion args. */
6976 variadic_args_p = pack_expansion_args_count (inner_args);
6978 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6979 if ((nargs > nparms && !variadic_p)
6980 || (nargs < nparms - variadic_p
6981 && require_all_args
6982 && !variadic_args_p
6983 && (!use_default_args
6984 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6985 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6987 if (complain & tf_error)
6989 if (variadic_p || default_p)
6991 nparms -= variadic_p + default_p;
6992 error ("wrong number of template arguments "
6993 "(%d, should be at least %d)", nargs, nparms);
6995 else
6996 error ("wrong number of template arguments "
6997 "(%d, should be %d)", nargs, nparms);
6999 if (in_decl)
7000 inform (input_location, "provided for %q+D", in_decl);
7003 return error_mark_node;
7005 /* We can't pass a pack expansion to a non-pack parameter of an alias
7006 template (DR 1430). */
7007 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7008 && variadic_args_p
7009 && nargs - variadic_args_p < nparms - variadic_p)
7011 if (complain & tf_error)
7013 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7015 tree arg = TREE_VEC_ELT (inner_args, i);
7016 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7018 if (PACK_EXPANSION_P (arg)
7019 && !template_parameter_pack_p (parm))
7021 error ("pack expansion argument for non-pack parameter "
7022 "%qD of alias template %qD", parm, in_decl);
7023 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7024 goto found;
7027 gcc_unreachable ();
7028 found:;
7030 return error_mark_node;
7033 /* We need to evaluate the template arguments, even though this
7034 template-id may be nested within a "sizeof". */
7035 saved_unevaluated_operand = cp_unevaluated_operand;
7036 cp_unevaluated_operand = 0;
7037 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7038 c_inhibit_evaluation_warnings = 0;
7039 new_inner_args = make_tree_vec (nparms);
7040 new_args = add_outermost_template_args (args, new_inner_args);
7041 int pack_adjust = 0;
7042 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7044 tree arg;
7045 tree parm;
7047 /* Get the Ith template parameter. */
7048 parm = TREE_VEC_ELT (parms, parm_idx);
7050 if (parm == error_mark_node)
7052 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7053 continue;
7056 /* Calculate the next argument. */
7057 if (arg_idx < nargs)
7058 arg = TREE_VEC_ELT (inner_args, arg_idx);
7059 else
7060 arg = NULL_TREE;
7062 if (template_parameter_pack_p (TREE_VALUE (parm))
7063 && !(arg && ARGUMENT_PACK_P (arg)))
7065 /* Some arguments will be placed in the
7066 template parameter pack PARM. */
7067 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7068 inner_args, arg_idx,
7069 new_args, &lost,
7070 in_decl, complain);
7072 if (arg == NULL_TREE)
7074 /* We don't know how many args we have yet, just use the
7075 unconverted (and still packed) ones for now. */
7076 new_inner_args = orig_inner_args;
7077 arg_idx = nargs;
7078 break;
7081 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7083 /* Store this argument. */
7084 if (arg == error_mark_node)
7086 lost++;
7087 /* We are done with all of the arguments. */
7088 arg_idx = nargs;
7090 else
7092 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7093 arg_idx += pack_adjust;
7096 continue;
7098 else if (arg)
7100 if (PACK_EXPANSION_P (arg))
7102 /* "If every valid specialization of a variadic template
7103 requires an empty template parameter pack, the template is
7104 ill-formed, no diagnostic required." So check that the
7105 pattern works with this parameter. */
7106 tree pattern = PACK_EXPANSION_PATTERN (arg);
7107 tree conv = convert_template_argument (TREE_VALUE (parm),
7108 pattern, new_args,
7109 complain, parm_idx,
7110 in_decl);
7111 if (conv == error_mark_node)
7113 inform (input_location, "so any instantiation with a "
7114 "non-empty parameter pack would be ill-formed");
7115 ++lost;
7117 else if (TYPE_P (conv) && !TYPE_P (pattern))
7118 /* Recover from missing typename. */
7119 TREE_VEC_ELT (inner_args, arg_idx)
7120 = make_pack_expansion (conv);
7122 /* We don't know how many args we have yet, just
7123 use the unconverted ones for now. */
7124 new_inner_args = inner_args;
7125 arg_idx = nargs;
7126 break;
7129 else if (require_all_args)
7131 /* There must be a default arg in this case. */
7132 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7133 complain, in_decl);
7134 /* The position of the first default template argument,
7135 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7136 Record that. */
7137 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7138 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7139 arg_idx - pack_adjust);
7141 else
7142 break;
7144 if (arg == error_mark_node)
7146 if (complain & tf_error)
7147 error ("template argument %d is invalid", arg_idx + 1);
7149 else if (!arg)
7150 /* This only occurs if there was an error in the template
7151 parameter list itself (which we would already have
7152 reported) that we are trying to recover from, e.g., a class
7153 template with a parameter list such as
7154 template<typename..., typename>. */
7155 ++lost;
7156 else
7157 arg = convert_template_argument (TREE_VALUE (parm),
7158 arg, new_args, complain,
7159 parm_idx, in_decl);
7161 if (arg == error_mark_node)
7162 lost++;
7163 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7165 cp_unevaluated_operand = saved_unevaluated_operand;
7166 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7168 if (variadic_p && arg_idx < nargs)
7170 if (complain & tf_error)
7172 error ("wrong number of template arguments "
7173 "(%d, should be %d)", nargs, arg_idx);
7174 if (in_decl)
7175 error ("provided for %q+D", in_decl);
7177 return error_mark_node;
7180 if (lost)
7181 return error_mark_node;
7183 #ifdef ENABLE_CHECKING
7184 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7185 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7186 TREE_VEC_LENGTH (new_inner_args));
7187 #endif
7189 return new_inner_args;
7192 /* Like coerce_template_parms. If PARMS represents all template
7193 parameters levels, this function returns a vector of vectors
7194 representing all the resulting argument levels. Note that in this
7195 case, only the innermost arguments are coerced because the
7196 outermost ones are supposed to have been coerced already.
7198 Otherwise, if PARMS represents only (the innermost) vector of
7199 parameters, this function returns a vector containing just the
7200 innermost resulting arguments. */
7202 static tree
7203 coerce_innermost_template_parms (tree parms,
7204 tree args,
7205 tree in_decl,
7206 tsubst_flags_t complain,
7207 bool require_all_args,
7208 bool use_default_args)
7210 int parms_depth = TMPL_PARMS_DEPTH (parms);
7211 int args_depth = TMPL_ARGS_DEPTH (args);
7212 tree coerced_args;
7214 if (parms_depth > 1)
7216 coerced_args = make_tree_vec (parms_depth);
7217 tree level;
7218 int cur_depth;
7220 for (level = parms, cur_depth = parms_depth;
7221 parms_depth > 0 && level != NULL_TREE;
7222 level = TREE_CHAIN (level), --cur_depth)
7224 tree l;
7225 if (cur_depth == args_depth)
7226 l = coerce_template_parms (TREE_VALUE (level),
7227 args, in_decl, complain,
7228 require_all_args,
7229 use_default_args);
7230 else
7231 l = TMPL_ARGS_LEVEL (args, cur_depth);
7233 if (l == error_mark_node)
7234 return error_mark_node;
7236 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7239 else
7240 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7241 args, in_decl, complain,
7242 require_all_args,
7243 use_default_args);
7244 return coerced_args;
7247 /* Returns 1 if template args OT and NT are equivalent. */
7249 static int
7250 template_args_equal (tree ot, tree nt)
7252 if (nt == ot)
7253 return 1;
7254 if (nt == NULL_TREE || ot == NULL_TREE)
7255 return false;
7257 if (TREE_CODE (nt) == TREE_VEC)
7258 /* For member templates */
7259 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7260 else if (PACK_EXPANSION_P (ot))
7261 return (PACK_EXPANSION_P (nt)
7262 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7263 PACK_EXPANSION_PATTERN (nt))
7264 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7265 PACK_EXPANSION_EXTRA_ARGS (nt)));
7266 else if (ARGUMENT_PACK_P (ot))
7268 int i, len;
7269 tree opack, npack;
7271 if (!ARGUMENT_PACK_P (nt))
7272 return 0;
7274 opack = ARGUMENT_PACK_ARGS (ot);
7275 npack = ARGUMENT_PACK_ARGS (nt);
7276 len = TREE_VEC_LENGTH (opack);
7277 if (TREE_VEC_LENGTH (npack) != len)
7278 return 0;
7279 for (i = 0; i < len; ++i)
7280 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7281 TREE_VEC_ELT (npack, i)))
7282 return 0;
7283 return 1;
7285 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7287 /* We get here probably because we are in the middle of substituting
7288 into the pattern of a pack expansion. In that case the
7289 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7290 interested in. So we want to use the initial pack argument for
7291 the comparison. */
7292 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7293 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7294 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7295 return template_args_equal (ot, nt);
7297 else if (TYPE_P (nt))
7299 if (!TYPE_P (ot))
7300 return false;
7301 /* Don't treat an alias template specialization with dependent
7302 arguments as equivalent to its underlying type when used as a
7303 template argument; we need them to be distinct so that we
7304 substitute into the specialization arguments at instantiation
7305 time. And aliases can't be equivalent without being ==, so
7306 we don't need to look any deeper. */
7307 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7308 return false;
7309 else
7310 return same_type_p (ot, nt);
7312 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7313 return 0;
7314 else
7315 return cp_tree_equal (ot, nt);
7318 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7319 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7320 NEWARG_PTR with the offending arguments if they are non-NULL. */
7322 static int
7323 comp_template_args_with_info (tree oldargs, tree newargs,
7324 tree *oldarg_ptr, tree *newarg_ptr)
7326 int i;
7328 if (oldargs == newargs)
7329 return 1;
7331 if (!oldargs || !newargs)
7332 return 0;
7334 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7335 return 0;
7337 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7339 tree nt = TREE_VEC_ELT (newargs, i);
7340 tree ot = TREE_VEC_ELT (oldargs, i);
7342 if (! template_args_equal (ot, nt))
7344 if (oldarg_ptr != NULL)
7345 *oldarg_ptr = ot;
7346 if (newarg_ptr != NULL)
7347 *newarg_ptr = nt;
7348 return 0;
7351 return 1;
7354 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7355 of template arguments. Returns 0 otherwise. */
7358 comp_template_args (tree oldargs, tree newargs)
7360 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7363 static void
7364 add_pending_template (tree d)
7366 tree ti = (TYPE_P (d)
7367 ? CLASSTYPE_TEMPLATE_INFO (d)
7368 : DECL_TEMPLATE_INFO (d));
7369 struct pending_template *pt;
7370 int level;
7372 if (TI_PENDING_TEMPLATE_FLAG (ti))
7373 return;
7375 /* We are called both from instantiate_decl, where we've already had a
7376 tinst_level pushed, and instantiate_template, where we haven't.
7377 Compensate. */
7378 level = !current_tinst_level || current_tinst_level->decl != d;
7380 if (level)
7381 push_tinst_level (d);
7383 pt = ggc_alloc<pending_template> ();
7384 pt->next = NULL;
7385 pt->tinst = current_tinst_level;
7386 if (last_pending_template)
7387 last_pending_template->next = pt;
7388 else
7389 pending_templates = pt;
7391 last_pending_template = pt;
7393 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7395 if (level)
7396 pop_tinst_level ();
7400 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7401 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7402 documentation for TEMPLATE_ID_EXPR. */
7404 tree
7405 lookup_template_function (tree fns, tree arglist)
7407 tree type;
7409 if (fns == error_mark_node || arglist == error_mark_node)
7410 return error_mark_node;
7412 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7414 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7416 error ("%q#D is not a function template", fns);
7417 return error_mark_node;
7420 if (BASELINK_P (fns))
7422 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7423 unknown_type_node,
7424 BASELINK_FUNCTIONS (fns),
7425 arglist);
7426 return fns;
7429 type = TREE_TYPE (fns);
7430 if (TREE_CODE (fns) == OVERLOAD || !type)
7431 type = unknown_type_node;
7433 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7436 /* Within the scope of a template class S<T>, the name S gets bound
7437 (in build_self_reference) to a TYPE_DECL for the class, not a
7438 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7439 or one of its enclosing classes, and that type is a template,
7440 return the associated TEMPLATE_DECL. Otherwise, the original
7441 DECL is returned.
7443 Also handle the case when DECL is a TREE_LIST of ambiguous
7444 injected-class-names from different bases. */
7446 tree
7447 maybe_get_template_decl_from_type_decl (tree decl)
7449 if (decl == NULL_TREE)
7450 return decl;
7452 /* DR 176: A lookup that finds an injected-class-name (10.2
7453 [class.member.lookup]) can result in an ambiguity in certain cases
7454 (for example, if it is found in more than one base class). If all of
7455 the injected-class-names that are found refer to specializations of
7456 the same class template, and if the name is followed by a
7457 template-argument-list, the reference refers to the class template
7458 itself and not a specialization thereof, and is not ambiguous. */
7459 if (TREE_CODE (decl) == TREE_LIST)
7461 tree t, tmpl = NULL_TREE;
7462 for (t = decl; t; t = TREE_CHAIN (t))
7464 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7465 if (!tmpl)
7466 tmpl = elt;
7467 else if (tmpl != elt)
7468 break;
7470 if (tmpl && t == NULL_TREE)
7471 return tmpl;
7472 else
7473 return decl;
7476 return (decl != NULL_TREE
7477 && DECL_SELF_REFERENCE_P (decl)
7478 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7479 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7482 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7483 parameters, find the desired type.
7485 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7487 IN_DECL, if non-NULL, is the template declaration we are trying to
7488 instantiate.
7490 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7491 the class we are looking up.
7493 Issue error and warning messages under control of COMPLAIN.
7495 If the template class is really a local class in a template
7496 function, then the FUNCTION_CONTEXT is the function in which it is
7497 being instantiated.
7499 ??? Note that this function is currently called *twice* for each
7500 template-id: the first time from the parser, while creating the
7501 incomplete type (finish_template_type), and the second type during the
7502 real instantiation (instantiate_template_class). This is surely something
7503 that we want to avoid. It also causes some problems with argument
7504 coercion (see convert_nontype_argument for more information on this). */
7506 static tree
7507 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7508 int entering_scope, tsubst_flags_t complain)
7510 tree templ = NULL_TREE, parmlist;
7511 tree t;
7512 spec_entry **slot;
7513 spec_entry *entry;
7514 spec_entry elt;
7515 hashval_t hash;
7517 if (identifier_p (d1))
7519 tree value = innermost_non_namespace_value (d1);
7520 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7521 templ = value;
7522 else
7524 if (context)
7525 push_decl_namespace (context);
7526 templ = lookup_name (d1);
7527 templ = maybe_get_template_decl_from_type_decl (templ);
7528 if (context)
7529 pop_decl_namespace ();
7531 if (templ)
7532 context = DECL_CONTEXT (templ);
7534 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7536 tree type = TREE_TYPE (d1);
7538 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7539 an implicit typename for the second A. Deal with it. */
7540 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7541 type = TREE_TYPE (type);
7543 if (CLASSTYPE_TEMPLATE_INFO (type))
7545 templ = CLASSTYPE_TI_TEMPLATE (type);
7546 d1 = DECL_NAME (templ);
7549 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7550 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7552 templ = TYPE_TI_TEMPLATE (d1);
7553 d1 = DECL_NAME (templ);
7555 else if (DECL_TYPE_TEMPLATE_P (d1))
7557 templ = d1;
7558 d1 = DECL_NAME (templ);
7559 context = DECL_CONTEXT (templ);
7561 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7563 templ = d1;
7564 d1 = DECL_NAME (templ);
7567 /* Issue an error message if we didn't find a template. */
7568 if (! templ)
7570 if (complain & tf_error)
7571 error ("%qT is not a template", d1);
7572 return error_mark_node;
7575 if (TREE_CODE (templ) != TEMPLATE_DECL
7576 /* Make sure it's a user visible template, if it was named by
7577 the user. */
7578 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7579 && !PRIMARY_TEMPLATE_P (templ)))
7581 if (complain & tf_error)
7583 error ("non-template type %qT used as a template", d1);
7584 if (in_decl)
7585 error ("for template declaration %q+D", in_decl);
7587 return error_mark_node;
7590 complain &= ~tf_user;
7592 /* An alias that just changes the name of a template is equivalent to the
7593 other template, so if any of the arguments are pack expansions, strip
7594 the alias to avoid problems with a pack expansion passed to a non-pack
7595 alias template parameter (DR 1430). */
7596 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7597 templ = get_underlying_template (templ);
7599 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7601 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7602 template arguments */
7604 tree parm;
7605 tree arglist2;
7606 tree outer;
7608 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7610 /* Consider an example where a template template parameter declared as
7612 template <class T, class U = std::allocator<T> > class TT
7614 The template parameter level of T and U are one level larger than
7615 of TT. To proper process the default argument of U, say when an
7616 instantiation `TT<int>' is seen, we need to build the full
7617 arguments containing {int} as the innermost level. Outer levels,
7618 available when not appearing as default template argument, can be
7619 obtained from the arguments of the enclosing template.
7621 Suppose that TT is later substituted with std::vector. The above
7622 instantiation is `TT<int, std::allocator<T> >' with TT at
7623 level 1, and T at level 2, while the template arguments at level 1
7624 becomes {std::vector} and the inner level 2 is {int}. */
7626 outer = DECL_CONTEXT (templ);
7627 if (outer)
7628 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7629 else if (current_template_parms)
7630 /* This is an argument of the current template, so we haven't set
7631 DECL_CONTEXT yet. */
7632 outer = current_template_args ();
7634 if (outer)
7635 arglist = add_to_template_args (outer, arglist);
7637 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7638 complain,
7639 /*require_all_args=*/true,
7640 /*use_default_args=*/true);
7641 if (arglist2 == error_mark_node
7642 || (!uses_template_parms (arglist2)
7643 && check_instantiated_args (templ, arglist2, complain)))
7644 return error_mark_node;
7646 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7647 return parm;
7649 else
7651 tree template_type = TREE_TYPE (templ);
7652 tree gen_tmpl;
7653 tree type_decl;
7654 tree found = NULL_TREE;
7655 int arg_depth;
7656 int parm_depth;
7657 int is_dependent_type;
7658 int use_partial_inst_tmpl = false;
7660 if (template_type == error_mark_node)
7661 /* An error occurred while building the template TEMPL, and a
7662 diagnostic has most certainly been emitted for that
7663 already. Let's propagate that error. */
7664 return error_mark_node;
7666 gen_tmpl = most_general_template (templ);
7667 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7668 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7669 arg_depth = TMPL_ARGS_DEPTH (arglist);
7671 if (arg_depth == 1 && parm_depth > 1)
7673 /* We've been given an incomplete set of template arguments.
7674 For example, given:
7676 template <class T> struct S1 {
7677 template <class U> struct S2 {};
7678 template <class U> struct S2<U*> {};
7681 we will be called with an ARGLIST of `U*', but the
7682 TEMPLATE will be `template <class T> template
7683 <class U> struct S1<T>::S2'. We must fill in the missing
7684 arguments. */
7685 arglist
7686 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7687 arglist);
7688 arg_depth = TMPL_ARGS_DEPTH (arglist);
7691 /* Now we should have enough arguments. */
7692 gcc_assert (parm_depth == arg_depth);
7694 /* From here on, we're only interested in the most general
7695 template. */
7697 /* Calculate the BOUND_ARGS. These will be the args that are
7698 actually tsubst'd into the definition to create the
7699 instantiation. */
7700 if (parm_depth > 1)
7702 /* We have multiple levels of arguments to coerce, at once. */
7703 int i;
7704 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7706 tree bound_args = make_tree_vec (parm_depth);
7708 for (i = saved_depth,
7709 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7710 i > 0 && t != NULL_TREE;
7711 --i, t = TREE_CHAIN (t))
7713 tree a;
7714 if (i == saved_depth)
7715 a = coerce_template_parms (TREE_VALUE (t),
7716 arglist, gen_tmpl,
7717 complain,
7718 /*require_all_args=*/true,
7719 /*use_default_args=*/true);
7720 else
7721 /* Outer levels should have already been coerced. */
7722 a = TMPL_ARGS_LEVEL (arglist, i);
7724 /* Don't process further if one of the levels fails. */
7725 if (a == error_mark_node)
7727 /* Restore the ARGLIST to its full size. */
7728 TREE_VEC_LENGTH (arglist) = saved_depth;
7729 return error_mark_node;
7732 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7734 /* We temporarily reduce the length of the ARGLIST so
7735 that coerce_template_parms will see only the arguments
7736 corresponding to the template parameters it is
7737 examining. */
7738 TREE_VEC_LENGTH (arglist)--;
7741 /* Restore the ARGLIST to its full size. */
7742 TREE_VEC_LENGTH (arglist) = saved_depth;
7744 arglist = bound_args;
7746 else
7747 arglist
7748 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7749 INNERMOST_TEMPLATE_ARGS (arglist),
7750 gen_tmpl,
7751 complain,
7752 /*require_all_args=*/true,
7753 /*use_default_args=*/true);
7755 if (arglist == error_mark_node)
7756 /* We were unable to bind the arguments. */
7757 return error_mark_node;
7759 /* In the scope of a template class, explicit references to the
7760 template class refer to the type of the template, not any
7761 instantiation of it. For example, in:
7763 template <class T> class C { void f(C<T>); }
7765 the `C<T>' is just the same as `C'. Outside of the
7766 class, however, such a reference is an instantiation. */
7767 if ((entering_scope
7768 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7769 || currently_open_class (template_type))
7770 /* comp_template_args is expensive, check it last. */
7771 && comp_template_args (TYPE_TI_ARGS (template_type),
7772 arglist))
7773 return template_type;
7775 /* If we already have this specialization, return it. */
7776 elt.tmpl = gen_tmpl;
7777 elt.args = arglist;
7778 hash = spec_hasher::hash (&elt);
7779 entry = type_specializations->find_with_hash (&elt, hash);
7781 if (entry)
7782 return entry->spec;
7784 is_dependent_type = uses_template_parms (arglist);
7786 /* If the deduced arguments are invalid, then the binding
7787 failed. */
7788 if (!is_dependent_type
7789 && check_instantiated_args (gen_tmpl,
7790 INNERMOST_TEMPLATE_ARGS (arglist),
7791 complain))
7792 return error_mark_node;
7794 if (!is_dependent_type
7795 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7796 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7797 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7799 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7800 DECL_NAME (gen_tmpl),
7801 /*tag_scope=*/ts_global);
7802 return found;
7805 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7806 complain, in_decl);
7807 if (context == error_mark_node)
7808 return error_mark_node;
7810 if (!context)
7811 context = global_namespace;
7813 /* Create the type. */
7814 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7816 /* The user referred to a specialization of an alias
7817 template represented by GEN_TMPL.
7819 [temp.alias]/2 says:
7821 When a template-id refers to the specialization of an
7822 alias template, it is equivalent to the associated
7823 type obtained by substitution of its
7824 template-arguments for the template-parameters in the
7825 type-id of the alias template. */
7827 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7828 /* Note that the call above (by indirectly calling
7829 register_specialization in tsubst_decl) registers the
7830 TYPE_DECL representing the specialization of the alias
7831 template. So next time someone substitutes ARGLIST for
7832 the template parms into the alias template (GEN_TMPL),
7833 she'll get that TYPE_DECL back. */
7835 if (t == error_mark_node)
7836 return t;
7838 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7840 if (!is_dependent_type)
7842 set_current_access_from_decl (TYPE_NAME (template_type));
7843 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7844 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7845 arglist, complain, in_decl),
7846 SCOPED_ENUM_P (template_type), NULL);
7848 if (t == error_mark_node)
7849 return t;
7851 else
7853 /* We don't want to call start_enum for this type, since
7854 the values for the enumeration constants may involve
7855 template parameters. And, no one should be interested
7856 in the enumeration constants for such a type. */
7857 t = cxx_make_type (ENUMERAL_TYPE);
7858 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7860 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7861 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7862 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7864 else if (CLASS_TYPE_P (template_type))
7866 t = make_class_type (TREE_CODE (template_type));
7867 CLASSTYPE_DECLARED_CLASS (t)
7868 = CLASSTYPE_DECLARED_CLASS (template_type);
7869 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7870 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7872 /* A local class. Make sure the decl gets registered properly. */
7873 if (context == current_function_decl)
7874 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7876 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7877 /* This instantiation is another name for the primary
7878 template type. Set the TYPE_CANONICAL field
7879 appropriately. */
7880 TYPE_CANONICAL (t) = template_type;
7881 else if (any_template_arguments_need_structural_equality_p (arglist))
7882 /* Some of the template arguments require structural
7883 equality testing, so this template class requires
7884 structural equality testing. */
7885 SET_TYPE_STRUCTURAL_EQUALITY (t);
7887 else
7888 gcc_unreachable ();
7890 /* If we called start_enum or pushtag above, this information
7891 will already be set up. */
7892 if (!TYPE_NAME (t))
7894 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7896 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7897 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7898 DECL_SOURCE_LOCATION (type_decl)
7899 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7901 else
7902 type_decl = TYPE_NAME (t);
7904 if (CLASS_TYPE_P (template_type))
7906 TREE_PRIVATE (type_decl)
7907 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7908 TREE_PROTECTED (type_decl)
7909 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7910 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7912 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7913 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7917 if (OVERLOAD_TYPE_P (t)
7918 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7920 if (tree attributes
7921 = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (template_type)))
7923 if (!TREE_CHAIN (attributes))
7924 TYPE_ATTRIBUTES (t) = attributes;
7925 else
7926 TYPE_ATTRIBUTES (t)
7927 = build_tree_list (TREE_PURPOSE (attributes),
7928 TREE_VALUE (attributes));
7932 /* Let's consider the explicit specialization of a member
7933 of a class template specialization that is implicitly instantiated,
7934 e.g.:
7935 template<class T>
7936 struct S
7938 template<class U> struct M {}; //#0
7941 template<>
7942 template<>
7943 struct S<int>::M<char> //#1
7945 int i;
7947 [temp.expl.spec]/4 says this is valid.
7949 In this case, when we write:
7950 S<int>::M<char> m;
7952 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7953 the one of #0.
7955 When we encounter #1, we want to store the partial instantiation
7956 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7958 For all cases other than this "explicit specialization of member of a
7959 class template", we just want to store the most general template into
7960 the CLASSTYPE_TI_TEMPLATE of M.
7962 This case of "explicit specialization of member of a class template"
7963 only happens when:
7964 1/ the enclosing class is an instantiation of, and therefore not
7965 the same as, the context of the most general template, and
7966 2/ we aren't looking at the partial instantiation itself, i.e.
7967 the innermost arguments are not the same as the innermost parms of
7968 the most general template.
7970 So it's only when 1/ and 2/ happens that we want to use the partial
7971 instantiation of the member template in lieu of its most general
7972 template. */
7974 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7975 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7976 /* the enclosing class must be an instantiation... */
7977 && CLASS_TYPE_P (context)
7978 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7980 tree partial_inst_args;
7981 TREE_VEC_LENGTH (arglist)--;
7982 ++processing_template_decl;
7983 partial_inst_args =
7984 tsubst (INNERMOST_TEMPLATE_ARGS
7985 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7986 arglist, complain, NULL_TREE);
7987 --processing_template_decl;
7988 TREE_VEC_LENGTH (arglist)++;
7989 use_partial_inst_tmpl =
7990 /*...and we must not be looking at the partial instantiation
7991 itself. */
7992 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7993 partial_inst_args);
7996 if (!use_partial_inst_tmpl)
7997 /* This case is easy; there are no member templates involved. */
7998 found = gen_tmpl;
7999 else
8001 /* This is a full instantiation of a member template. Find
8002 the partial instantiation of which this is an instance. */
8004 /* Temporarily reduce by one the number of levels in the ARGLIST
8005 so as to avoid comparing the last set of arguments. */
8006 TREE_VEC_LENGTH (arglist)--;
8007 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8008 TREE_VEC_LENGTH (arglist)++;
8009 /* FOUND is either a proper class type, or an alias
8010 template specialization. In the later case, it's a
8011 TYPE_DECL, resulting from the substituting of arguments
8012 for parameters in the TYPE_DECL of the alias template
8013 done earlier. So be careful while getting the template
8014 of FOUND. */
8015 found = TREE_CODE (found) == TYPE_DECL
8016 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8017 : CLASSTYPE_TI_TEMPLATE (found);
8020 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8022 elt.spec = t;
8023 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8024 entry = ggc_alloc<spec_entry> ();
8025 *entry = elt;
8026 *slot = entry;
8028 /* Note this use of the partial instantiation so we can check it
8029 later in maybe_process_partial_specialization. */
8030 DECL_TEMPLATE_INSTANTIATIONS (found)
8031 = tree_cons (arglist, t,
8032 DECL_TEMPLATE_INSTANTIATIONS (found));
8034 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8035 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8036 /* Now that the type has been registered on the instantiations
8037 list, we set up the enumerators. Because the enumeration
8038 constants may involve the enumeration type itself, we make
8039 sure to register the type first, and then create the
8040 constants. That way, doing tsubst_expr for the enumeration
8041 constants won't result in recursive calls here; we'll find
8042 the instantiation and exit above. */
8043 tsubst_enum (template_type, t, arglist);
8045 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8046 /* If the type makes use of template parameters, the
8047 code that generates debugging information will crash. */
8048 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8050 /* Possibly limit visibility based on template args. */
8051 TREE_PUBLIC (type_decl) = 1;
8052 determine_visibility (type_decl);
8054 inherit_targ_abi_tags (t);
8056 return t;
8060 /* Wrapper for lookup_template_class_1. */
8062 tree
8063 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8064 int entering_scope, tsubst_flags_t complain)
8066 tree ret;
8067 timevar_push (TV_TEMPLATE_INST);
8068 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8069 entering_scope, complain);
8070 timevar_pop (TV_TEMPLATE_INST);
8071 return ret;
8074 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8075 The type of the expression is the unknown_type_node since the
8076 template-id could refer to an explicit or partial specialization. */
8078 tree
8079 lookup_template_variable (tree templ, tree arglist)
8081 tree type = unknown_type_node;
8082 tsubst_flags_t complain = tf_warning_or_error;
8083 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (templ));
8084 arglist = coerce_template_parms (parms, arglist, templ, complain,
8085 /*req_all*/true, /*use_default*/true);
8086 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8090 struct pair_fn_data
8092 tree_fn_t fn;
8093 void *data;
8094 /* True when we should also visit template parameters that occur in
8095 non-deduced contexts. */
8096 bool include_nondeduced_p;
8097 hash_set<tree> *visited;
8100 /* Called from for_each_template_parm via walk_tree. */
8102 static tree
8103 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8105 tree t = *tp;
8106 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8107 tree_fn_t fn = pfd->fn;
8108 void *data = pfd->data;
8110 if (TYPE_P (t)
8111 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8112 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8113 pfd->include_nondeduced_p))
8114 return error_mark_node;
8116 switch (TREE_CODE (t))
8118 case RECORD_TYPE:
8119 if (TYPE_PTRMEMFUNC_P (t))
8120 break;
8121 /* Fall through. */
8123 case UNION_TYPE:
8124 case ENUMERAL_TYPE:
8125 if (!TYPE_TEMPLATE_INFO (t))
8126 *walk_subtrees = 0;
8127 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8128 fn, data, pfd->visited,
8129 pfd->include_nondeduced_p))
8130 return error_mark_node;
8131 break;
8133 case INTEGER_TYPE:
8134 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8135 fn, data, pfd->visited,
8136 pfd->include_nondeduced_p)
8137 || for_each_template_parm (TYPE_MAX_VALUE (t),
8138 fn, data, pfd->visited,
8139 pfd->include_nondeduced_p))
8140 return error_mark_node;
8141 break;
8143 case METHOD_TYPE:
8144 /* Since we're not going to walk subtrees, we have to do this
8145 explicitly here. */
8146 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8147 pfd->visited, pfd->include_nondeduced_p))
8148 return error_mark_node;
8149 /* Fall through. */
8151 case FUNCTION_TYPE:
8152 /* Check the return type. */
8153 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8154 pfd->include_nondeduced_p))
8155 return error_mark_node;
8157 /* Check the parameter types. Since default arguments are not
8158 instantiated until they are needed, the TYPE_ARG_TYPES may
8159 contain expressions that involve template parameters. But,
8160 no-one should be looking at them yet. And, once they're
8161 instantiated, they don't contain template parameters, so
8162 there's no point in looking at them then, either. */
8164 tree parm;
8166 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8167 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8168 pfd->visited, pfd->include_nondeduced_p))
8169 return error_mark_node;
8171 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8172 want walk_tree walking into them itself. */
8173 *walk_subtrees = 0;
8175 break;
8177 case TYPEOF_TYPE:
8178 case UNDERLYING_TYPE:
8179 if (pfd->include_nondeduced_p
8180 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
8181 pfd->visited,
8182 pfd->include_nondeduced_p))
8183 return error_mark_node;
8184 break;
8186 case FUNCTION_DECL:
8187 case VAR_DECL:
8188 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8189 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8190 pfd->visited, pfd->include_nondeduced_p))
8191 return error_mark_node;
8192 /* Fall through. */
8194 case PARM_DECL:
8195 case CONST_DECL:
8196 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8197 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8198 pfd->visited, pfd->include_nondeduced_p))
8199 return error_mark_node;
8200 if (DECL_CONTEXT (t)
8201 && pfd->include_nondeduced_p
8202 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8203 pfd->visited, pfd->include_nondeduced_p))
8204 return error_mark_node;
8205 break;
8207 case BOUND_TEMPLATE_TEMPLATE_PARM:
8208 /* Record template parameters such as `T' inside `TT<T>'. */
8209 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8210 pfd->include_nondeduced_p))
8211 return error_mark_node;
8212 /* Fall through. */
8214 case TEMPLATE_TEMPLATE_PARM:
8215 case TEMPLATE_TYPE_PARM:
8216 case TEMPLATE_PARM_INDEX:
8217 if (fn && (*fn)(t, data))
8218 return error_mark_node;
8219 else if (!fn)
8220 return error_mark_node;
8221 break;
8223 case TEMPLATE_DECL:
8224 /* A template template parameter is encountered. */
8225 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8226 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8227 pfd->include_nondeduced_p))
8228 return error_mark_node;
8230 /* Already substituted template template parameter */
8231 *walk_subtrees = 0;
8232 break;
8234 case TYPENAME_TYPE:
8235 if (!fn
8236 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8237 data, pfd->visited,
8238 pfd->include_nondeduced_p))
8239 return error_mark_node;
8240 break;
8242 case CONSTRUCTOR:
8243 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8244 && pfd->include_nondeduced_p
8245 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8246 (TREE_TYPE (t)), fn, data,
8247 pfd->visited, pfd->include_nondeduced_p))
8248 return error_mark_node;
8249 break;
8251 case INDIRECT_REF:
8252 case COMPONENT_REF:
8253 /* If there's no type, then this thing must be some expression
8254 involving template parameters. */
8255 if (!fn && !TREE_TYPE (t))
8256 return error_mark_node;
8257 break;
8259 case MODOP_EXPR:
8260 case CAST_EXPR:
8261 case IMPLICIT_CONV_EXPR:
8262 case REINTERPRET_CAST_EXPR:
8263 case CONST_CAST_EXPR:
8264 case STATIC_CAST_EXPR:
8265 case DYNAMIC_CAST_EXPR:
8266 case ARROW_EXPR:
8267 case DOTSTAR_EXPR:
8268 case TYPEID_EXPR:
8269 case PSEUDO_DTOR_EXPR:
8270 if (!fn)
8271 return error_mark_node;
8272 break;
8274 default:
8275 break;
8278 /* We didn't find any template parameters we liked. */
8279 return NULL_TREE;
8282 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8283 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8284 call FN with the parameter and the DATA.
8285 If FN returns nonzero, the iteration is terminated, and
8286 for_each_template_parm returns 1. Otherwise, the iteration
8287 continues. If FN never returns a nonzero value, the value
8288 returned by for_each_template_parm is 0. If FN is NULL, it is
8289 considered to be the function which always returns 1.
8291 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8292 parameters that occur in non-deduced contexts. When false, only
8293 visits those template parameters that can be deduced. */
8295 static int
8296 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8297 hash_set<tree> *visited,
8298 bool include_nondeduced_p)
8300 struct pair_fn_data pfd;
8301 int result;
8303 /* Set up. */
8304 pfd.fn = fn;
8305 pfd.data = data;
8306 pfd.include_nondeduced_p = include_nondeduced_p;
8308 /* Walk the tree. (Conceptually, we would like to walk without
8309 duplicates, but for_each_template_parm_r recursively calls
8310 for_each_template_parm, so we would need to reorganize a fair
8311 bit to use walk_tree_without_duplicates, so we keep our own
8312 visited list.) */
8313 if (visited)
8314 pfd.visited = visited;
8315 else
8316 pfd.visited = new hash_set<tree>;
8317 result = cp_walk_tree (&t,
8318 for_each_template_parm_r,
8319 &pfd,
8320 pfd.visited) != NULL_TREE;
8322 /* Clean up. */
8323 if (!visited)
8325 delete pfd.visited;
8326 pfd.visited = 0;
8329 return result;
8332 /* Returns true if T depends on any template parameter. */
8335 uses_template_parms (tree t)
8337 if (t == NULL_TREE)
8338 return false;
8340 bool dependent_p;
8341 int saved_processing_template_decl;
8343 saved_processing_template_decl = processing_template_decl;
8344 if (!saved_processing_template_decl)
8345 processing_template_decl = 1;
8346 if (TYPE_P (t))
8347 dependent_p = dependent_type_p (t);
8348 else if (TREE_CODE (t) == TREE_VEC)
8349 dependent_p = any_dependent_template_arguments_p (t);
8350 else if (TREE_CODE (t) == TREE_LIST)
8351 dependent_p = (uses_template_parms (TREE_VALUE (t))
8352 || uses_template_parms (TREE_CHAIN (t)));
8353 else if (TREE_CODE (t) == TYPE_DECL)
8354 dependent_p = dependent_type_p (TREE_TYPE (t));
8355 else if (DECL_P (t)
8356 || EXPR_P (t)
8357 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8358 || TREE_CODE (t) == OVERLOAD
8359 || BASELINK_P (t)
8360 || identifier_p (t)
8361 || TREE_CODE (t) == TRAIT_EXPR
8362 || TREE_CODE (t) == CONSTRUCTOR
8363 || CONSTANT_CLASS_P (t))
8364 dependent_p = (type_dependent_expression_p (t)
8365 || value_dependent_expression_p (t));
8366 else
8368 gcc_assert (t == error_mark_node);
8369 dependent_p = false;
8372 processing_template_decl = saved_processing_template_decl;
8374 return dependent_p;
8377 /* Returns true iff current_function_decl is an incompletely instantiated
8378 template. Useful instead of processing_template_decl because the latter
8379 is set to 0 during instantiate_non_dependent_expr. */
8381 bool
8382 in_template_function (void)
8384 tree fn = current_function_decl;
8385 bool ret;
8386 ++processing_template_decl;
8387 ret = (fn && DECL_LANG_SPECIFIC (fn)
8388 && DECL_TEMPLATE_INFO (fn)
8389 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8390 --processing_template_decl;
8391 return ret;
8394 /* Returns true if T depends on any template parameter with level LEVEL. */
8397 uses_template_parms_level (tree t, int level)
8399 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8400 /*include_nondeduced_p=*/true);
8403 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8404 ill-formed translation unit, i.e. a variable or function that isn't
8405 usable in a constant expression. */
8407 static inline bool
8408 neglectable_inst_p (tree d)
8410 return (DECL_P (d)
8411 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8412 : decl_maybe_constant_var_p (d)));
8415 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8416 neglectable and instantiated from within an erroneous instantiation. */
8418 static bool
8419 limit_bad_template_recursion (tree decl)
8421 struct tinst_level *lev = current_tinst_level;
8422 int errs = errorcount + sorrycount;
8423 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8424 return false;
8426 for (; lev; lev = lev->next)
8427 if (neglectable_inst_p (lev->decl))
8428 break;
8430 return (lev && errs > lev->errors);
8433 static int tinst_depth;
8434 extern int max_tinst_depth;
8435 int depth_reached;
8437 static GTY(()) struct tinst_level *last_error_tinst_level;
8439 /* We're starting to instantiate D; record the template instantiation context
8440 for diagnostics and to restore it later. */
8442 bool
8443 push_tinst_level (tree d)
8445 return push_tinst_level_loc (d, input_location);
8448 /* We're starting to instantiate D; record the template instantiation context
8449 at LOC for diagnostics and to restore it later. */
8451 bool
8452 push_tinst_level_loc (tree d, location_t loc)
8454 struct tinst_level *new_level;
8456 if (tinst_depth >= max_tinst_depth)
8458 fatal_error ("template instantiation depth exceeds maximum of %d"
8459 " (use -ftemplate-depth= to increase the maximum)",
8460 max_tinst_depth);
8461 return false;
8464 /* If the current instantiation caused problems, don't let it instantiate
8465 anything else. Do allow deduction substitution and decls usable in
8466 constant expressions. */
8467 if (limit_bad_template_recursion (d))
8468 return false;
8470 new_level = ggc_alloc<tinst_level> ();
8471 new_level->decl = d;
8472 new_level->locus = loc;
8473 new_level->errors = errorcount+sorrycount;
8474 new_level->in_system_header_p = in_system_header_at (input_location);
8475 new_level->next = current_tinst_level;
8476 current_tinst_level = new_level;
8478 ++tinst_depth;
8479 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8480 depth_reached = tinst_depth;
8482 return true;
8485 /* We're done instantiating this template; return to the instantiation
8486 context. */
8488 void
8489 pop_tinst_level (void)
8491 /* Restore the filename and line number stashed away when we started
8492 this instantiation. */
8493 input_location = current_tinst_level->locus;
8494 current_tinst_level = current_tinst_level->next;
8495 --tinst_depth;
8498 /* We're instantiating a deferred template; restore the template
8499 instantiation context in which the instantiation was requested, which
8500 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8502 static tree
8503 reopen_tinst_level (struct tinst_level *level)
8505 struct tinst_level *t;
8507 tinst_depth = 0;
8508 for (t = level; t; t = t->next)
8509 ++tinst_depth;
8511 current_tinst_level = level;
8512 pop_tinst_level ();
8513 if (current_tinst_level)
8514 current_tinst_level->errors = errorcount+sorrycount;
8515 return level->decl;
8518 /* Returns the TINST_LEVEL which gives the original instantiation
8519 context. */
8521 struct tinst_level *
8522 outermost_tinst_level (void)
8524 struct tinst_level *level = current_tinst_level;
8525 if (level)
8526 while (level->next)
8527 level = level->next;
8528 return level;
8531 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8532 vector of template arguments, as for tsubst.
8534 Returns an appropriate tsubst'd friend declaration. */
8536 static tree
8537 tsubst_friend_function (tree decl, tree args)
8539 tree new_friend;
8541 if (TREE_CODE (decl) == FUNCTION_DECL
8542 && DECL_TEMPLATE_INSTANTIATION (decl)
8543 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8544 /* This was a friend declared with an explicit template
8545 argument list, e.g.:
8547 friend void f<>(T);
8549 to indicate that f was a template instantiation, not a new
8550 function declaration. Now, we have to figure out what
8551 instantiation of what template. */
8553 tree template_id, arglist, fns;
8554 tree new_args;
8555 tree tmpl;
8556 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8558 /* Friend functions are looked up in the containing namespace scope.
8559 We must enter that scope, to avoid finding member functions of the
8560 current class with same name. */
8561 push_nested_namespace (ns);
8562 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8563 tf_warning_or_error, NULL_TREE,
8564 /*integral_constant_expression_p=*/false);
8565 pop_nested_namespace (ns);
8566 arglist = tsubst (DECL_TI_ARGS (decl), args,
8567 tf_warning_or_error, NULL_TREE);
8568 template_id = lookup_template_function (fns, arglist);
8570 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8571 tmpl = determine_specialization (template_id, new_friend,
8572 &new_args,
8573 /*need_member_template=*/0,
8574 TREE_VEC_LENGTH (args),
8575 tsk_none);
8576 return instantiate_template (tmpl, new_args, tf_error);
8579 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8581 /* The NEW_FRIEND will look like an instantiation, to the
8582 compiler, but is not an instantiation from the point of view of
8583 the language. For example, we might have had:
8585 template <class T> struct S {
8586 template <class U> friend void f(T, U);
8589 Then, in S<int>, template <class U> void f(int, U) is not an
8590 instantiation of anything. */
8591 if (new_friend == error_mark_node)
8592 return error_mark_node;
8594 DECL_USE_TEMPLATE (new_friend) = 0;
8595 if (TREE_CODE (decl) == TEMPLATE_DECL)
8597 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8598 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8599 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8602 /* The mangled name for the NEW_FRIEND is incorrect. The function
8603 is not a template instantiation and should not be mangled like
8604 one. Therefore, we forget the mangling here; we'll recompute it
8605 later if we need it. */
8606 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8608 SET_DECL_RTL (new_friend, NULL);
8609 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8612 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8614 tree old_decl;
8615 tree new_friend_template_info;
8616 tree new_friend_result_template_info;
8617 tree ns;
8618 int new_friend_is_defn;
8620 /* We must save some information from NEW_FRIEND before calling
8621 duplicate decls since that function will free NEW_FRIEND if
8622 possible. */
8623 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8624 new_friend_is_defn =
8625 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8626 (template_for_substitution (new_friend)))
8627 != NULL_TREE);
8628 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8630 /* This declaration is a `primary' template. */
8631 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8633 new_friend_result_template_info
8634 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8636 else
8637 new_friend_result_template_info = NULL_TREE;
8639 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8640 if (new_friend_is_defn)
8641 DECL_INITIAL (new_friend) = error_mark_node;
8643 /* Inside pushdecl_namespace_level, we will push into the
8644 current namespace. However, the friend function should go
8645 into the namespace of the template. */
8646 ns = decl_namespace_context (new_friend);
8647 push_nested_namespace (ns);
8648 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8649 pop_nested_namespace (ns);
8651 if (old_decl == error_mark_node)
8652 return error_mark_node;
8654 if (old_decl != new_friend)
8656 /* This new friend declaration matched an existing
8657 declaration. For example, given:
8659 template <class T> void f(T);
8660 template <class U> class C {
8661 template <class T> friend void f(T) {}
8664 the friend declaration actually provides the definition
8665 of `f', once C has been instantiated for some type. So,
8666 old_decl will be the out-of-class template declaration,
8667 while new_friend is the in-class definition.
8669 But, if `f' was called before this point, the
8670 instantiation of `f' will have DECL_TI_ARGS corresponding
8671 to `T' but not to `U', references to which might appear
8672 in the definition of `f'. Previously, the most general
8673 template for an instantiation of `f' was the out-of-class
8674 version; now it is the in-class version. Therefore, we
8675 run through all specialization of `f', adding to their
8676 DECL_TI_ARGS appropriately. In particular, they need a
8677 new set of outer arguments, corresponding to the
8678 arguments for this class instantiation.
8680 The same situation can arise with something like this:
8682 friend void f(int);
8683 template <class T> class C {
8684 friend void f(T) {}
8687 when `C<int>' is instantiated. Now, `f(int)' is defined
8688 in the class. */
8690 if (!new_friend_is_defn)
8691 /* On the other hand, if the in-class declaration does
8692 *not* provide a definition, then we don't want to alter
8693 existing definitions. We can just leave everything
8694 alone. */
8696 else
8698 tree new_template = TI_TEMPLATE (new_friend_template_info);
8699 tree new_args = TI_ARGS (new_friend_template_info);
8701 /* Overwrite whatever template info was there before, if
8702 any, with the new template information pertaining to
8703 the declaration. */
8704 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8706 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8708 /* We should have called reregister_specialization in
8709 duplicate_decls. */
8710 gcc_assert (retrieve_specialization (new_template,
8711 new_args, 0)
8712 == old_decl);
8714 /* Instantiate it if the global has already been used. */
8715 if (DECL_ODR_USED (old_decl))
8716 instantiate_decl (old_decl, /*defer_ok=*/true,
8717 /*expl_inst_class_mem_p=*/false);
8719 else
8721 tree t;
8723 /* Indicate that the old function template is a partial
8724 instantiation. */
8725 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8726 = new_friend_result_template_info;
8728 gcc_assert (new_template
8729 == most_general_template (new_template));
8730 gcc_assert (new_template != old_decl);
8732 /* Reassign any specializations already in the hash table
8733 to the new more general template, and add the
8734 additional template args. */
8735 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8736 t != NULL_TREE;
8737 t = TREE_CHAIN (t))
8739 tree spec = TREE_VALUE (t);
8740 spec_entry elt;
8742 elt.tmpl = old_decl;
8743 elt.args = DECL_TI_ARGS (spec);
8744 elt.spec = NULL_TREE;
8746 decl_specializations->remove_elt (&elt);
8748 DECL_TI_ARGS (spec)
8749 = add_outermost_template_args (new_args,
8750 DECL_TI_ARGS (spec));
8752 register_specialization
8753 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8756 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8760 /* The information from NEW_FRIEND has been merged into OLD_DECL
8761 by duplicate_decls. */
8762 new_friend = old_decl;
8765 else
8767 tree context = DECL_CONTEXT (new_friend);
8768 bool dependent_p;
8770 /* In the code
8771 template <class T> class C {
8772 template <class U> friend void C1<U>::f (); // case 1
8773 friend void C2<T>::f (); // case 2
8775 we only need to make sure CONTEXT is a complete type for
8776 case 2. To distinguish between the two cases, we note that
8777 CONTEXT of case 1 remains dependent type after tsubst while
8778 this isn't true for case 2. */
8779 ++processing_template_decl;
8780 dependent_p = dependent_type_p (context);
8781 --processing_template_decl;
8783 if (!dependent_p
8784 && !complete_type_or_else (context, NULL_TREE))
8785 return error_mark_node;
8787 if (COMPLETE_TYPE_P (context))
8789 tree fn = new_friend;
8790 /* do_friend adds the TEMPLATE_DECL for any member friend
8791 template even if it isn't a member template, i.e.
8792 template <class T> friend A<T>::f();
8793 Look through it in that case. */
8794 if (TREE_CODE (fn) == TEMPLATE_DECL
8795 && !PRIMARY_TEMPLATE_P (fn))
8796 fn = DECL_TEMPLATE_RESULT (fn);
8797 /* Check to see that the declaration is really present, and,
8798 possibly obtain an improved declaration. */
8799 fn = check_classfn (context, fn, NULL_TREE);
8801 if (fn)
8802 new_friend = fn;
8806 return new_friend;
8809 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8810 template arguments, as for tsubst.
8812 Returns an appropriate tsubst'd friend type or error_mark_node on
8813 failure. */
8815 static tree
8816 tsubst_friend_class (tree friend_tmpl, tree args)
8818 tree friend_type;
8819 tree tmpl;
8820 tree context;
8822 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8824 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8825 return TREE_TYPE (t);
8828 context = CP_DECL_CONTEXT (friend_tmpl);
8830 if (context != global_namespace)
8832 if (TREE_CODE (context) == NAMESPACE_DECL)
8833 push_nested_namespace (context);
8834 else
8835 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8838 /* Look for a class template declaration. We look for hidden names
8839 because two friend declarations of the same template are the
8840 same. For example, in:
8842 struct A {
8843 template <typename> friend class F;
8845 template <typename> struct B {
8846 template <typename> friend class F;
8849 both F templates are the same. */
8850 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8851 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8853 /* But, if we don't find one, it might be because we're in a
8854 situation like this:
8856 template <class T>
8857 struct S {
8858 template <class U>
8859 friend struct S;
8862 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8863 for `S<int>', not the TEMPLATE_DECL. */
8864 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8866 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8867 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8870 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8872 /* The friend template has already been declared. Just
8873 check to see that the declarations match, and install any new
8874 default parameters. We must tsubst the default parameters,
8875 of course. We only need the innermost template parameters
8876 because that is all that redeclare_class_template will look
8877 at. */
8878 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8879 > TMPL_ARGS_DEPTH (args))
8881 tree parms;
8882 location_t saved_input_location;
8883 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8884 args, tf_warning_or_error);
8886 saved_input_location = input_location;
8887 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8888 redeclare_class_template (TREE_TYPE (tmpl), parms);
8889 input_location = saved_input_location;
8893 friend_type = TREE_TYPE (tmpl);
8895 else
8897 /* The friend template has not already been declared. In this
8898 case, the instantiation of the template class will cause the
8899 injection of this template into the global scope. */
8900 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8901 if (tmpl == error_mark_node)
8902 return error_mark_node;
8904 /* The new TMPL is not an instantiation of anything, so we
8905 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8906 the new type because that is supposed to be the corresponding
8907 template decl, i.e., TMPL. */
8908 DECL_USE_TEMPLATE (tmpl) = 0;
8909 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8910 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8911 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8912 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8914 /* Inject this template into the global scope. */
8915 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8918 if (context != global_namespace)
8920 if (TREE_CODE (context) == NAMESPACE_DECL)
8921 pop_nested_namespace (context);
8922 else
8923 pop_nested_class ();
8926 return friend_type;
8929 /* Returns zero if TYPE cannot be completed later due to circularity.
8930 Otherwise returns one. */
8932 static int
8933 can_complete_type_without_circularity (tree type)
8935 if (type == NULL_TREE || type == error_mark_node)
8936 return 0;
8937 else if (COMPLETE_TYPE_P (type))
8938 return 1;
8939 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8940 return can_complete_type_without_circularity (TREE_TYPE (type));
8941 else if (CLASS_TYPE_P (type)
8942 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8943 return 0;
8944 else
8945 return 1;
8948 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8950 /* Apply any attributes which had to be deferred until instantiation
8951 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8952 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8954 static void
8955 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8956 tree args, tsubst_flags_t complain, tree in_decl)
8958 tree last_dep = NULL_TREE;
8959 tree t;
8960 tree *p;
8962 for (t = attributes; t; t = TREE_CHAIN (t))
8963 if (ATTR_IS_DEPENDENT (t))
8965 last_dep = t;
8966 attributes = copy_list (attributes);
8967 break;
8970 if (DECL_P (*decl_p))
8972 if (TREE_TYPE (*decl_p) == error_mark_node)
8973 return;
8974 p = &DECL_ATTRIBUTES (*decl_p);
8976 else
8977 p = &TYPE_ATTRIBUTES (*decl_p);
8979 if (last_dep)
8981 tree late_attrs = NULL_TREE;
8982 tree *q = &late_attrs;
8984 for (*p = attributes; *p; )
8986 t = *p;
8987 if (ATTR_IS_DEPENDENT (t))
8989 *p = TREE_CHAIN (t);
8990 TREE_CHAIN (t) = NULL_TREE;
8991 if ((flag_openmp || flag_cilkplus)
8992 && is_attribute_p ("omp declare simd",
8993 get_attribute_name (t))
8994 && TREE_VALUE (t))
8996 tree clauses = TREE_VALUE (TREE_VALUE (t));
8997 clauses = tsubst_omp_clauses (clauses, true, args,
8998 complain, in_decl);
8999 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9000 clauses = finish_omp_clauses (clauses);
9001 tree parms = DECL_ARGUMENTS (*decl_p);
9002 clauses
9003 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9004 if (clauses)
9005 TREE_VALUE (TREE_VALUE (t)) = clauses;
9006 else
9007 TREE_VALUE (t) = NULL_TREE;
9009 /* If the first attribute argument is an identifier, don't
9010 pass it through tsubst. Attributes like mode, format,
9011 cleanup and several target specific attributes expect it
9012 unmodified. */
9013 else if (attribute_takes_identifier_p (get_attribute_name (t))
9014 && TREE_VALUE (t))
9016 tree chain
9017 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9018 in_decl,
9019 /*integral_constant_expression_p=*/false);
9020 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9021 TREE_VALUE (t)
9022 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9023 chain);
9025 else
9026 TREE_VALUE (t)
9027 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9028 /*integral_constant_expression_p=*/false);
9029 *q = t;
9030 q = &TREE_CHAIN (t);
9032 else
9033 p = &TREE_CHAIN (t);
9036 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9040 /* Perform (or defer) access check for typedefs that were referenced
9041 from within the template TMPL code.
9042 This is a subroutine of instantiate_decl and instantiate_class_template.
9043 TMPL is the template to consider and TARGS is the list of arguments of
9044 that template. */
9046 static void
9047 perform_typedefs_access_check (tree tmpl, tree targs)
9049 location_t saved_location;
9050 unsigned i;
9051 qualified_typedef_usage_t *iter;
9053 if (!tmpl
9054 || (!CLASS_TYPE_P (tmpl)
9055 && TREE_CODE (tmpl) != FUNCTION_DECL))
9056 return;
9058 saved_location = input_location;
9059 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9061 tree type_decl = iter->typedef_decl;
9062 tree type_scope = iter->context;
9064 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9065 continue;
9067 if (uses_template_parms (type_decl))
9068 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9069 if (uses_template_parms (type_scope))
9070 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9072 /* Make access check error messages point to the location
9073 of the use of the typedef. */
9074 input_location = iter->locus;
9075 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9076 type_decl, type_decl,
9077 tf_warning_or_error);
9079 input_location = saved_location;
9082 static tree
9083 instantiate_class_template_1 (tree type)
9085 tree templ, args, pattern, t, member;
9086 tree typedecl;
9087 tree pbinfo;
9088 tree base_list;
9089 unsigned int saved_maximum_field_alignment;
9090 tree fn_context;
9092 if (type == error_mark_node)
9093 return error_mark_node;
9095 if (COMPLETE_OR_OPEN_TYPE_P (type)
9096 || uses_template_parms (type))
9097 return type;
9099 /* Figure out which template is being instantiated. */
9100 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9101 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9103 /* Determine what specialization of the original template to
9104 instantiate. */
9105 t = most_specialized_partial_spec (type, tf_warning_or_error);
9106 if (t == error_mark_node)
9108 TYPE_BEING_DEFINED (type) = 1;
9109 return error_mark_node;
9111 else if (t)
9113 /* This TYPE is actually an instantiation of a partial
9114 specialization. We replace the innermost set of ARGS with
9115 the arguments appropriate for substitution. For example,
9116 given:
9118 template <class T> struct S {};
9119 template <class T> struct S<T*> {};
9121 and supposing that we are instantiating S<int*>, ARGS will
9122 presently be {int*} -- but we need {int}. */
9123 pattern = TREE_TYPE (t);
9124 args = TREE_PURPOSE (t);
9126 else
9128 pattern = TREE_TYPE (templ);
9129 args = CLASSTYPE_TI_ARGS (type);
9132 /* If the template we're instantiating is incomplete, then clearly
9133 there's nothing we can do. */
9134 if (!COMPLETE_TYPE_P (pattern))
9135 return type;
9137 /* If we've recursively instantiated too many templates, stop. */
9138 if (! push_tinst_level (type))
9139 return type;
9141 /* Now we're really doing the instantiation. Mark the type as in
9142 the process of being defined. */
9143 TYPE_BEING_DEFINED (type) = 1;
9145 /* We may be in the middle of deferred access check. Disable
9146 it now. */
9147 push_deferring_access_checks (dk_no_deferred);
9149 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9150 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9151 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9152 fn_context = error_mark_node;
9153 if (!fn_context)
9154 push_to_top_level ();
9155 /* Use #pragma pack from the template context. */
9156 saved_maximum_field_alignment = maximum_field_alignment;
9157 maximum_field_alignment = TYPE_PRECISION (pattern);
9159 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9161 /* Set the input location to the most specialized template definition.
9162 This is needed if tsubsting causes an error. */
9163 typedecl = TYPE_MAIN_DECL (pattern);
9164 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9165 DECL_SOURCE_LOCATION (typedecl);
9167 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9168 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9169 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9170 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9171 if (ANON_AGGR_TYPE_P (pattern))
9172 SET_ANON_AGGR_TYPE_P (type);
9173 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9175 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9176 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9177 /* Adjust visibility for template arguments. */
9178 determine_visibility (TYPE_MAIN_DECL (type));
9180 if (CLASS_TYPE_P (type))
9181 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9183 pbinfo = TYPE_BINFO (pattern);
9185 /* We should never instantiate a nested class before its enclosing
9186 class; we need to look up the nested class by name before we can
9187 instantiate it, and that lookup should instantiate the enclosing
9188 class. */
9189 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9190 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9192 base_list = NULL_TREE;
9193 if (BINFO_N_BASE_BINFOS (pbinfo))
9195 tree pbase_binfo;
9196 tree pushed_scope;
9197 int i;
9199 /* We must enter the scope containing the type, as that is where
9200 the accessibility of types named in dependent bases are
9201 looked up from. */
9202 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9204 /* Substitute into each of the bases to determine the actual
9205 basetypes. */
9206 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9208 tree base;
9209 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9210 tree expanded_bases = NULL_TREE;
9211 int idx, len = 1;
9213 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9215 expanded_bases =
9216 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9217 args, tf_error, NULL_TREE);
9218 if (expanded_bases == error_mark_node)
9219 continue;
9221 len = TREE_VEC_LENGTH (expanded_bases);
9224 for (idx = 0; idx < len; idx++)
9226 if (expanded_bases)
9227 /* Extract the already-expanded base class. */
9228 base = TREE_VEC_ELT (expanded_bases, idx);
9229 else
9230 /* Substitute to figure out the base class. */
9231 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9232 NULL_TREE);
9234 if (base == error_mark_node)
9235 continue;
9237 base_list = tree_cons (access, base, base_list);
9238 if (BINFO_VIRTUAL_P (pbase_binfo))
9239 TREE_TYPE (base_list) = integer_type_node;
9243 /* The list is now in reverse order; correct that. */
9244 base_list = nreverse (base_list);
9246 if (pushed_scope)
9247 pop_scope (pushed_scope);
9249 /* Now call xref_basetypes to set up all the base-class
9250 information. */
9251 xref_basetypes (type, base_list);
9253 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9254 (int) ATTR_FLAG_TYPE_IN_PLACE,
9255 args, tf_error, NULL_TREE);
9256 fixup_attribute_variants (type);
9258 /* Now that our base classes are set up, enter the scope of the
9259 class, so that name lookups into base classes, etc. will work
9260 correctly. This is precisely analogous to what we do in
9261 begin_class_definition when defining an ordinary non-template
9262 class, except we also need to push the enclosing classes. */
9263 push_nested_class (type);
9265 /* Now members are processed in the order of declaration. */
9266 for (member = CLASSTYPE_DECL_LIST (pattern);
9267 member; member = TREE_CHAIN (member))
9269 tree t = TREE_VALUE (member);
9271 if (TREE_PURPOSE (member))
9273 if (TYPE_P (t))
9275 /* Build new CLASSTYPE_NESTED_UTDS. */
9277 tree newtag;
9278 bool class_template_p;
9280 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9281 && TYPE_LANG_SPECIFIC (t)
9282 && CLASSTYPE_IS_TEMPLATE (t));
9283 /* If the member is a class template, then -- even after
9284 substitution -- there may be dependent types in the
9285 template argument list for the class. We increment
9286 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9287 that function will assume that no types are dependent
9288 when outside of a template. */
9289 if (class_template_p)
9290 ++processing_template_decl;
9291 newtag = tsubst (t, args, tf_error, NULL_TREE);
9292 if (class_template_p)
9293 --processing_template_decl;
9294 if (newtag == error_mark_node)
9295 continue;
9297 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9299 tree name = TYPE_IDENTIFIER (t);
9301 if (class_template_p)
9302 /* Unfortunately, lookup_template_class sets
9303 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9304 instantiation (i.e., for the type of a member
9305 template class nested within a template class.)
9306 This behavior is required for
9307 maybe_process_partial_specialization to work
9308 correctly, but is not accurate in this case;
9309 the TAG is not an instantiation of anything.
9310 (The corresponding TEMPLATE_DECL is an
9311 instantiation, but the TYPE is not.) */
9312 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9314 /* Now, we call pushtag to put this NEWTAG into the scope of
9315 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9316 pushtag calling push_template_decl. We don't have to do
9317 this for enums because it will already have been done in
9318 tsubst_enum. */
9319 if (name)
9320 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9321 pushtag (name, newtag, /*tag_scope=*/ts_current);
9324 else if (DECL_DECLARES_FUNCTION_P (t))
9326 /* Build new TYPE_METHODS. */
9327 tree r;
9329 if (TREE_CODE (t) == TEMPLATE_DECL)
9330 ++processing_template_decl;
9331 r = tsubst (t, args, tf_error, NULL_TREE);
9332 if (TREE_CODE (t) == TEMPLATE_DECL)
9333 --processing_template_decl;
9334 set_current_access_from_decl (r);
9335 finish_member_declaration (r);
9336 /* Instantiate members marked with attribute used. */
9337 if (r != error_mark_node && DECL_PRESERVE_P (r))
9338 mark_used (r);
9339 if (TREE_CODE (r) == FUNCTION_DECL
9340 && DECL_OMP_DECLARE_REDUCTION_P (r))
9341 cp_check_omp_declare_reduction (r);
9343 else if (DECL_CLASS_TEMPLATE_P (t)
9344 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9345 /* A closure type for a lambda in a default argument for a
9346 member template. Ignore it; it will be instantiated with
9347 the default argument. */;
9348 else
9350 /* Build new TYPE_FIELDS. */
9351 if (TREE_CODE (t) == STATIC_ASSERT)
9353 tree condition;
9355 ++c_inhibit_evaluation_warnings;
9356 condition =
9357 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9358 tf_warning_or_error, NULL_TREE,
9359 /*integral_constant_expression_p=*/true);
9360 --c_inhibit_evaluation_warnings;
9362 finish_static_assert (condition,
9363 STATIC_ASSERT_MESSAGE (t),
9364 STATIC_ASSERT_SOURCE_LOCATION (t),
9365 /*member_p=*/true);
9367 else if (TREE_CODE (t) != CONST_DECL)
9369 tree r;
9370 tree vec = NULL_TREE;
9371 int len = 1;
9373 /* The file and line for this declaration, to
9374 assist in error message reporting. Since we
9375 called push_tinst_level above, we don't need to
9376 restore these. */
9377 input_location = DECL_SOURCE_LOCATION (t);
9379 if (TREE_CODE (t) == TEMPLATE_DECL)
9380 ++processing_template_decl;
9381 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9382 if (TREE_CODE (t) == TEMPLATE_DECL)
9383 --processing_template_decl;
9385 if (TREE_CODE (r) == TREE_VEC)
9387 /* A capture pack became multiple fields. */
9388 vec = r;
9389 len = TREE_VEC_LENGTH (vec);
9392 for (int i = 0; i < len; ++i)
9394 if (vec)
9395 r = TREE_VEC_ELT (vec, i);
9396 if (VAR_P (r))
9398 /* In [temp.inst]:
9400 [t]he initialization (and any associated
9401 side-effects) of a static data member does
9402 not occur unless the static data member is
9403 itself used in a way that requires the
9404 definition of the static data member to
9405 exist.
9407 Therefore, we do not substitute into the
9408 initialized for the static data member here. */
9409 finish_static_data_member_decl
9411 /*init=*/NULL_TREE,
9412 /*init_const_expr_p=*/false,
9413 /*asmspec_tree=*/NULL_TREE,
9414 /*flags=*/0);
9415 /* Instantiate members marked with attribute used. */
9416 if (r != error_mark_node && DECL_PRESERVE_P (r))
9417 mark_used (r);
9419 else if (TREE_CODE (r) == FIELD_DECL)
9421 /* Determine whether R has a valid type and can be
9422 completed later. If R is invalid, then its type
9423 is replaced by error_mark_node. */
9424 tree rtype = TREE_TYPE (r);
9425 if (can_complete_type_without_circularity (rtype))
9426 complete_type (rtype);
9428 if (!COMPLETE_TYPE_P (rtype))
9430 cxx_incomplete_type_error (r, rtype);
9431 TREE_TYPE (r) = error_mark_node;
9435 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9436 such a thing will already have been added to the field
9437 list by tsubst_enum in finish_member_declaration in the
9438 CLASSTYPE_NESTED_UTDS case above. */
9439 if (!(TREE_CODE (r) == TYPE_DECL
9440 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9441 && DECL_ARTIFICIAL (r)))
9443 set_current_access_from_decl (r);
9444 finish_member_declaration (r);
9450 else
9452 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9453 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9455 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9457 tree friend_type = t;
9458 bool adjust_processing_template_decl = false;
9460 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9462 /* template <class T> friend class C; */
9463 friend_type = tsubst_friend_class (friend_type, args);
9464 adjust_processing_template_decl = true;
9466 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9468 /* template <class T> friend class C::D; */
9469 friend_type = tsubst (friend_type, args,
9470 tf_warning_or_error, NULL_TREE);
9471 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9472 friend_type = TREE_TYPE (friend_type);
9473 adjust_processing_template_decl = true;
9475 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9476 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9478 /* This could be either
9480 friend class T::C;
9482 when dependent_type_p is false or
9484 template <class U> friend class T::C;
9486 otherwise. */
9487 friend_type = tsubst (friend_type, args,
9488 tf_warning_or_error, NULL_TREE);
9489 /* Bump processing_template_decl for correct
9490 dependent_type_p calculation. */
9491 ++processing_template_decl;
9492 if (dependent_type_p (friend_type))
9493 adjust_processing_template_decl = true;
9494 --processing_template_decl;
9496 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9497 && hidden_name_p (TYPE_NAME (friend_type)))
9499 /* friend class C;
9501 where C hasn't been declared yet. Let's lookup name
9502 from namespace scope directly, bypassing any name that
9503 come from dependent base class. */
9504 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9506 /* The call to xref_tag_from_type does injection for friend
9507 classes. */
9508 push_nested_namespace (ns);
9509 friend_type =
9510 xref_tag_from_type (friend_type, NULL_TREE,
9511 /*tag_scope=*/ts_current);
9512 pop_nested_namespace (ns);
9514 else if (uses_template_parms (friend_type))
9515 /* friend class C<T>; */
9516 friend_type = tsubst (friend_type, args,
9517 tf_warning_or_error, NULL_TREE);
9518 /* Otherwise it's
9520 friend class C;
9522 where C is already declared or
9524 friend class C<int>;
9526 We don't have to do anything in these cases. */
9528 if (adjust_processing_template_decl)
9529 /* Trick make_friend_class into realizing that the friend
9530 we're adding is a template, not an ordinary class. It's
9531 important that we use make_friend_class since it will
9532 perform some error-checking and output cross-reference
9533 information. */
9534 ++processing_template_decl;
9536 if (friend_type != error_mark_node)
9537 make_friend_class (type, friend_type, /*complain=*/false);
9539 if (adjust_processing_template_decl)
9540 --processing_template_decl;
9542 else
9544 /* Build new DECL_FRIENDLIST. */
9545 tree r;
9547 /* The file and line for this declaration, to
9548 assist in error message reporting. Since we
9549 called push_tinst_level above, we don't need to
9550 restore these. */
9551 input_location = DECL_SOURCE_LOCATION (t);
9553 if (TREE_CODE (t) == TEMPLATE_DECL)
9555 ++processing_template_decl;
9556 push_deferring_access_checks (dk_no_check);
9559 r = tsubst_friend_function (t, args);
9560 add_friend (type, r, /*complain=*/false);
9561 if (TREE_CODE (t) == TEMPLATE_DECL)
9563 pop_deferring_access_checks ();
9564 --processing_template_decl;
9570 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9572 tree decl = lambda_function (type);
9573 if (decl)
9575 if (!DECL_TEMPLATE_INFO (decl)
9576 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9577 instantiate_decl (decl, false, false);
9579 /* We need to instantiate the capture list from the template
9580 after we've instantiated the closure members, but before we
9581 consider adding the conversion op. Also keep any captures
9582 that may have been added during instantiation of the op(). */
9583 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9584 tree tmpl_cap
9585 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9586 args, tf_warning_or_error, NULL_TREE,
9587 false, false);
9589 LAMBDA_EXPR_CAPTURE_LIST (expr)
9590 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9592 maybe_add_lambda_conv_op (type);
9594 else
9595 gcc_assert (errorcount);
9598 /* Set the file and line number information to whatever is given for
9599 the class itself. This puts error messages involving generated
9600 implicit functions at a predictable point, and the same point
9601 that would be used for non-template classes. */
9602 input_location = DECL_SOURCE_LOCATION (typedecl);
9604 unreverse_member_declarations (type);
9605 finish_struct_1 (type);
9606 TYPE_BEING_DEFINED (type) = 0;
9608 /* We don't instantiate default arguments for member functions. 14.7.1:
9610 The implicit instantiation of a class template specialization causes
9611 the implicit instantiation of the declarations, but not of the
9612 definitions or default arguments, of the class member functions,
9613 member classes, static data members and member templates.... */
9615 /* Some typedefs referenced from within the template code need to be access
9616 checked at template instantiation time, i.e now. These types were
9617 added to the template at parsing time. Let's get those and perform
9618 the access checks then. */
9619 perform_typedefs_access_check (pattern, args);
9620 perform_deferred_access_checks (tf_warning_or_error);
9621 pop_nested_class ();
9622 maximum_field_alignment = saved_maximum_field_alignment;
9623 if (!fn_context)
9624 pop_from_top_level ();
9625 pop_deferring_access_checks ();
9626 pop_tinst_level ();
9628 /* The vtable for a template class can be emitted in any translation
9629 unit in which the class is instantiated. When there is no key
9630 method, however, finish_struct_1 will already have added TYPE to
9631 the keyed_classes list. */
9632 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9633 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9635 return type;
9638 /* Wrapper for instantiate_class_template_1. */
9640 tree
9641 instantiate_class_template (tree type)
9643 tree ret;
9644 timevar_push (TV_TEMPLATE_INST);
9645 ret = instantiate_class_template_1 (type);
9646 timevar_pop (TV_TEMPLATE_INST);
9647 return ret;
9650 static tree
9651 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9653 tree r;
9655 if (!t)
9656 r = t;
9657 else if (TYPE_P (t))
9658 r = tsubst (t, args, complain, in_decl);
9659 else
9661 if (!(complain & tf_warning))
9662 ++c_inhibit_evaluation_warnings;
9663 r = tsubst_expr (t, args, complain, in_decl,
9664 /*integral_constant_expression_p=*/true);
9665 if (!(complain & tf_warning))
9666 --c_inhibit_evaluation_warnings;
9668 return r;
9671 /* Given a function parameter pack TMPL_PARM and some function parameters
9672 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9673 and set *SPEC_P to point at the next point in the list. */
9675 static tree
9676 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9678 /* Collect all of the extra "packed" parameters into an
9679 argument pack. */
9680 tree parmvec;
9681 tree parmtypevec;
9682 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9683 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9684 tree spec_parm = *spec_p;
9685 int i, len;
9687 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9688 if (tmpl_parm
9689 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9690 break;
9692 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9693 parmvec = make_tree_vec (len);
9694 parmtypevec = make_tree_vec (len);
9695 spec_parm = *spec_p;
9696 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9698 TREE_VEC_ELT (parmvec, i) = spec_parm;
9699 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9702 /* Build the argument packs. */
9703 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9704 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9705 TREE_TYPE (argpack) = argtypepack;
9706 *spec_p = spec_parm;
9708 return argpack;
9711 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9712 NONTYPE_ARGUMENT_PACK. */
9714 static tree
9715 make_fnparm_pack (tree spec_parm)
9717 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9720 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9721 pack expansion. */
9723 static bool
9724 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9726 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9727 if (i >= TREE_VEC_LENGTH (vec))
9728 return false;
9729 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9733 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9735 static tree
9736 make_argument_pack_select (tree arg_pack, unsigned index)
9738 tree aps = make_node (ARGUMENT_PACK_SELECT);
9740 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9741 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9743 return aps;
9746 /* This is a subroutine of tsubst_pack_expansion.
9748 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9749 mechanism to store the (non complete list of) arguments of the
9750 substitution and return a non substituted pack expansion, in order
9751 to wait for when we have enough arguments to really perform the
9752 substitution. */
9754 static bool
9755 use_pack_expansion_extra_args_p (tree parm_packs,
9756 int arg_pack_len,
9757 bool has_empty_arg)
9759 /* If one pack has an expansion and another pack has a normal
9760 argument or if one pack has an empty argument and an another
9761 one hasn't then tsubst_pack_expansion cannot perform the
9762 substitution and need to fall back on the
9763 PACK_EXPANSION_EXTRA mechanism. */
9764 if (parm_packs == NULL_TREE)
9765 return false;
9766 else if (has_empty_arg)
9767 return true;
9769 bool has_expansion_arg = false;
9770 for (int i = 0 ; i < arg_pack_len; ++i)
9772 bool has_non_expansion_arg = false;
9773 for (tree parm_pack = parm_packs;
9774 parm_pack;
9775 parm_pack = TREE_CHAIN (parm_pack))
9777 tree arg = TREE_VALUE (parm_pack);
9779 if (argument_pack_element_is_expansion_p (arg, i))
9780 has_expansion_arg = true;
9781 else
9782 has_non_expansion_arg = true;
9785 if (has_expansion_arg && has_non_expansion_arg)
9786 return true;
9788 return false;
9791 /* [temp.variadic]/6 says that:
9793 The instantiation of a pack expansion [...]
9794 produces a list E1,E2, ..., En, where N is the number of elements
9795 in the pack expansion parameters.
9797 This subroutine of tsubst_pack_expansion produces one of these Ei.
9799 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9800 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9801 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9802 INDEX is the index 'i' of the element Ei to produce. ARGS,
9803 COMPLAIN, and IN_DECL are the same parameters as for the
9804 tsubst_pack_expansion function.
9806 The function returns the resulting Ei upon successful completion,
9807 or error_mark_node.
9809 Note that this function possibly modifies the ARGS parameter, so
9810 it's the responsibility of the caller to restore it. */
9812 static tree
9813 gen_elem_of_pack_expansion_instantiation (tree pattern,
9814 tree parm_packs,
9815 unsigned index,
9816 tree args /* This parm gets
9817 modified. */,
9818 tsubst_flags_t complain,
9819 tree in_decl)
9821 tree t;
9822 bool ith_elem_is_expansion = false;
9824 /* For each parameter pack, change the substitution of the parameter
9825 pack to the ith argument in its argument pack, then expand the
9826 pattern. */
9827 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9829 tree parm = TREE_PURPOSE (pack);
9830 tree arg_pack = TREE_VALUE (pack);
9831 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9833 ith_elem_is_expansion |=
9834 argument_pack_element_is_expansion_p (arg_pack, index);
9836 /* Select the Ith argument from the pack. */
9837 if (TREE_CODE (parm) == PARM_DECL
9838 || TREE_CODE (parm) == FIELD_DECL)
9840 if (index == 0)
9842 aps = make_argument_pack_select (arg_pack, index);
9843 mark_used (parm);
9844 register_local_specialization (aps, parm);
9846 else
9847 aps = retrieve_local_specialization (parm);
9849 else
9851 int idx, level;
9852 template_parm_level_and_index (parm, &level, &idx);
9854 if (index == 0)
9856 aps = make_argument_pack_select (arg_pack, index);
9857 /* Update the corresponding argument. */
9858 TMPL_ARG (args, level, idx) = aps;
9860 else
9861 /* Re-use the ARGUMENT_PACK_SELECT. */
9862 aps = TMPL_ARG (args, level, idx);
9864 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9867 /* Substitute into the PATTERN with the (possibly altered)
9868 arguments. */
9869 if (pattern == in_decl)
9870 /* Expanding a fixed parameter pack from
9871 coerce_template_parameter_pack. */
9872 t = tsubst_decl (pattern, args, complain);
9873 else if (!TYPE_P (pattern))
9874 t = tsubst_expr (pattern, args, complain, in_decl,
9875 /*integral_constant_expression_p=*/false);
9876 else
9877 t = tsubst (pattern, args, complain, in_decl);
9879 /* If the Ith argument pack element is a pack expansion, then
9880 the Ith element resulting from the substituting is going to
9881 be a pack expansion as well. */
9882 if (ith_elem_is_expansion)
9883 t = make_pack_expansion (t);
9885 return t;
9888 /* Substitute ARGS into T, which is an pack expansion
9889 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9890 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9891 (if only a partial substitution could be performed) or
9892 ERROR_MARK_NODE if there was an error. */
9893 tree
9894 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9895 tree in_decl)
9897 tree pattern;
9898 tree pack, packs = NULL_TREE;
9899 bool unsubstituted_packs = false;
9900 int i, len = -1;
9901 tree result;
9902 hash_map<tree, tree> *saved_local_specializations = NULL;
9903 bool need_local_specializations = false;
9904 int levels;
9906 gcc_assert (PACK_EXPANSION_P (t));
9907 pattern = PACK_EXPANSION_PATTERN (t);
9909 /* Add in any args remembered from an earlier partial instantiation. */
9910 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9912 levels = TMPL_ARGS_DEPTH (args);
9914 /* Determine the argument packs that will instantiate the parameter
9915 packs used in the expansion expression. While we're at it,
9916 compute the number of arguments to be expanded and make sure it
9917 is consistent. */
9918 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9919 pack = TREE_CHAIN (pack))
9921 tree parm_pack = TREE_VALUE (pack);
9922 tree arg_pack = NULL_TREE;
9923 tree orig_arg = NULL_TREE;
9924 int level = 0;
9926 if (TREE_CODE (parm_pack) == BASES)
9928 if (BASES_DIRECT (parm_pack))
9929 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9930 args, complain, in_decl, false));
9931 else
9932 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9933 args, complain, in_decl, false));
9935 if (TREE_CODE (parm_pack) == PARM_DECL)
9937 if (PACK_EXPANSION_LOCAL_P (t))
9938 arg_pack = retrieve_local_specialization (parm_pack);
9939 else
9941 /* We can't rely on local_specializations for a parameter
9942 name used later in a function declaration (such as in a
9943 late-specified return type). Even if it exists, it might
9944 have the wrong value for a recursive call. Just make a
9945 dummy decl, since it's only used for its type. */
9946 arg_pack = tsubst_decl (parm_pack, args, complain);
9947 if (arg_pack && DECL_PACK_P (arg_pack))
9948 /* Partial instantiation of the parm_pack, we can't build
9949 up an argument pack yet. */
9950 arg_pack = NULL_TREE;
9951 else
9952 arg_pack = make_fnparm_pack (arg_pack);
9953 need_local_specializations = true;
9956 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9957 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9958 else
9960 int idx;
9961 template_parm_level_and_index (parm_pack, &level, &idx);
9963 if (level <= levels)
9964 arg_pack = TMPL_ARG (args, level, idx);
9967 orig_arg = arg_pack;
9968 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9969 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9971 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9972 /* This can only happen if we forget to expand an argument
9973 pack somewhere else. Just return an error, silently. */
9975 result = make_tree_vec (1);
9976 TREE_VEC_ELT (result, 0) = error_mark_node;
9977 return result;
9980 if (arg_pack)
9982 int my_len =
9983 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9985 /* Don't bother trying to do a partial substitution with
9986 incomplete packs; we'll try again after deduction. */
9987 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9988 return t;
9990 if (len < 0)
9991 len = my_len;
9992 else if (len != my_len)
9994 if (!(complain & tf_error))
9995 /* Fail quietly. */;
9996 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9997 error ("mismatched argument pack lengths while expanding "
9998 "%<%T%>",
9999 pattern);
10000 else
10001 error ("mismatched argument pack lengths while expanding "
10002 "%<%E%>",
10003 pattern);
10004 return error_mark_node;
10007 /* Keep track of the parameter packs and their corresponding
10008 argument packs. */
10009 packs = tree_cons (parm_pack, arg_pack, packs);
10010 TREE_TYPE (packs) = orig_arg;
10012 else
10014 /* We can't substitute for this parameter pack. We use a flag as
10015 well as the missing_level counter because function parameter
10016 packs don't have a level. */
10017 unsubstituted_packs = true;
10021 /* If the expansion is just T..., return the matching argument pack. */
10022 if (!unsubstituted_packs
10023 && TREE_PURPOSE (packs) == pattern)
10025 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10026 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10027 || pack_expansion_args_count (args))
10028 return args;
10029 /* Otherwise use the normal path so we get convert_from_reference. */
10032 /* We cannot expand this expansion expression, because we don't have
10033 all of the argument packs we need. */
10034 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10036 /* We got some full packs, but we can't substitute them in until we
10037 have values for all the packs. So remember these until then. */
10039 t = make_pack_expansion (pattern);
10040 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10041 return t;
10043 else if (unsubstituted_packs)
10045 /* There were no real arguments, we're just replacing a parameter
10046 pack with another version of itself. Substitute into the
10047 pattern and return a PACK_EXPANSION_*. The caller will need to
10048 deal with that. */
10049 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10050 t = tsubst_expr (pattern, args, complain, in_decl,
10051 /*integral_constant_expression_p=*/false);
10052 else
10053 t = tsubst (pattern, args, complain, in_decl);
10054 t = make_pack_expansion (t);
10055 return t;
10058 gcc_assert (len >= 0);
10060 if (need_local_specializations)
10062 /* We're in a late-specified return type, so create our own local
10063 specializations map; the current map is either NULL or (in the
10064 case of recursive unification) might have bindings that we don't
10065 want to use or alter. */
10066 saved_local_specializations = local_specializations;
10067 local_specializations = new hash_map<tree, tree>;
10070 /* For each argument in each argument pack, substitute into the
10071 pattern. */
10072 result = make_tree_vec (len);
10073 for (i = 0; i < len; ++i)
10075 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10077 args, complain,
10078 in_decl);
10079 TREE_VEC_ELT (result, i) = t;
10080 if (t == error_mark_node)
10082 result = error_mark_node;
10083 break;
10087 /* Update ARGS to restore the substitution from parameter packs to
10088 their argument packs. */
10089 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10091 tree parm = TREE_PURPOSE (pack);
10093 if (TREE_CODE (parm) == PARM_DECL
10094 || TREE_CODE (parm) == FIELD_DECL)
10095 register_local_specialization (TREE_TYPE (pack), parm);
10096 else
10098 int idx, level;
10100 if (TREE_VALUE (pack) == NULL_TREE)
10101 continue;
10103 template_parm_level_and_index (parm, &level, &idx);
10105 /* Update the corresponding argument. */
10106 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10107 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10108 TREE_TYPE (pack);
10109 else
10110 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10114 if (need_local_specializations)
10116 delete local_specializations;
10117 local_specializations = saved_local_specializations;
10120 return result;
10123 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10124 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10125 parameter packs; all parms generated from a function parameter pack will
10126 have the same DECL_PARM_INDEX. */
10128 tree
10129 get_pattern_parm (tree parm, tree tmpl)
10131 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10132 tree patparm;
10134 if (DECL_ARTIFICIAL (parm))
10136 for (patparm = DECL_ARGUMENTS (pattern);
10137 patparm; patparm = DECL_CHAIN (patparm))
10138 if (DECL_ARTIFICIAL (patparm)
10139 && DECL_NAME (parm) == DECL_NAME (patparm))
10140 break;
10142 else
10144 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10145 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10146 gcc_assert (DECL_PARM_INDEX (patparm)
10147 == DECL_PARM_INDEX (parm));
10150 return patparm;
10153 /* Substitute ARGS into the vector or list of template arguments T. */
10155 static tree
10156 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10158 tree orig_t = t;
10159 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10160 tree *elts;
10162 if (t == error_mark_node)
10163 return error_mark_node;
10165 len = TREE_VEC_LENGTH (t);
10166 elts = XALLOCAVEC (tree, len);
10168 for (i = 0; i < len; i++)
10170 tree orig_arg = TREE_VEC_ELT (t, i);
10171 tree new_arg;
10173 if (TREE_CODE (orig_arg) == TREE_VEC)
10174 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10175 else if (PACK_EXPANSION_P (orig_arg))
10177 /* Substitute into an expansion expression. */
10178 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10180 if (TREE_CODE (new_arg) == TREE_VEC)
10181 /* Add to the expanded length adjustment the number of
10182 expanded arguments. We subtract one from this
10183 measurement, because the argument pack expression
10184 itself is already counted as 1 in
10185 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10186 the argument pack is empty. */
10187 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10189 else if (ARGUMENT_PACK_P (orig_arg))
10191 /* Substitute into each of the arguments. */
10192 new_arg = TYPE_P (orig_arg)
10193 ? cxx_make_type (TREE_CODE (orig_arg))
10194 : make_node (TREE_CODE (orig_arg));
10196 SET_ARGUMENT_PACK_ARGS (
10197 new_arg,
10198 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10199 args, complain, in_decl));
10201 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10202 new_arg = error_mark_node;
10204 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10205 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10206 complain, in_decl);
10207 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10209 if (TREE_TYPE (new_arg) == error_mark_node)
10210 new_arg = error_mark_node;
10213 else
10214 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10216 if (new_arg == error_mark_node)
10217 return error_mark_node;
10219 elts[i] = new_arg;
10220 if (new_arg != orig_arg)
10221 need_new = 1;
10224 if (!need_new)
10225 return t;
10227 /* Make space for the expanded arguments coming from template
10228 argument packs. */
10229 t = make_tree_vec (len + expanded_len_adjust);
10230 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10231 arguments for a member template.
10232 In that case each TREE_VEC in ORIG_T represents a level of template
10233 arguments, and ORIG_T won't carry any non defaulted argument count.
10234 It will rather be the nested TREE_VECs that will carry one.
10235 In other words, ORIG_T carries a non defaulted argument count only
10236 if it doesn't contain any nested TREE_VEC. */
10237 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10239 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10240 count += expanded_len_adjust;
10241 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10243 for (i = 0, out = 0; i < len; i++)
10245 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10246 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10247 && TREE_CODE (elts[i]) == TREE_VEC)
10249 int idx;
10251 /* Now expand the template argument pack "in place". */
10252 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10253 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10255 else
10257 TREE_VEC_ELT (t, out) = elts[i];
10258 out++;
10262 return t;
10265 /* Return the result of substituting ARGS into the template parameters
10266 given by PARMS. If there are m levels of ARGS and m + n levels of
10267 PARMS, then the result will contain n levels of PARMS. For
10268 example, if PARMS is `template <class T> template <class U>
10269 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10270 result will be `template <int*, double, class V>'. */
10272 static tree
10273 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10275 tree r = NULL_TREE;
10276 tree* new_parms;
10278 /* When substituting into a template, we must set
10279 PROCESSING_TEMPLATE_DECL as the template parameters may be
10280 dependent if they are based on one-another, and the dependency
10281 predicates are short-circuit outside of templates. */
10282 ++processing_template_decl;
10284 for (new_parms = &r;
10285 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10286 new_parms = &(TREE_CHAIN (*new_parms)),
10287 parms = TREE_CHAIN (parms))
10289 tree new_vec =
10290 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10291 int i;
10293 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10295 tree tuple;
10297 if (parms == error_mark_node)
10298 continue;
10300 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10302 if (tuple == error_mark_node)
10303 continue;
10305 TREE_VEC_ELT (new_vec, i) =
10306 tsubst_template_parm (tuple, args, complain);
10309 *new_parms =
10310 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10311 - TMPL_ARGS_DEPTH (args)),
10312 new_vec, NULL_TREE);
10315 --processing_template_decl;
10317 return r;
10320 /* Return the result of substituting ARGS into one template parameter
10321 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10322 parameter and which TREE_PURPOSE is the default argument of the
10323 template parameter. */
10325 static tree
10326 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10328 tree default_value, parm_decl;
10330 if (args == NULL_TREE
10331 || t == NULL_TREE
10332 || t == error_mark_node)
10333 return t;
10335 gcc_assert (TREE_CODE (t) == TREE_LIST);
10337 default_value = TREE_PURPOSE (t);
10338 parm_decl = TREE_VALUE (t);
10340 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10341 if (TREE_CODE (parm_decl) == PARM_DECL
10342 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10343 parm_decl = error_mark_node;
10344 default_value = tsubst_template_arg (default_value, args,
10345 complain, NULL_TREE);
10347 return build_tree_list (default_value, parm_decl);
10350 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10351 type T. If T is not an aggregate or enumeration type, it is
10352 handled as if by tsubst. IN_DECL is as for tsubst. If
10353 ENTERING_SCOPE is nonzero, T is the context for a template which
10354 we are presently tsubst'ing. Return the substituted value. */
10356 static tree
10357 tsubst_aggr_type (tree t,
10358 tree args,
10359 tsubst_flags_t complain,
10360 tree in_decl,
10361 int entering_scope)
10363 if (t == NULL_TREE)
10364 return NULL_TREE;
10366 switch (TREE_CODE (t))
10368 case RECORD_TYPE:
10369 if (TYPE_PTRMEMFUNC_P (t))
10370 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10372 /* Else fall through. */
10373 case ENUMERAL_TYPE:
10374 case UNION_TYPE:
10375 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10377 tree argvec;
10378 tree context;
10379 tree r;
10380 int saved_unevaluated_operand;
10381 int saved_inhibit_evaluation_warnings;
10383 /* In "sizeof(X<I>)" we need to evaluate "I". */
10384 saved_unevaluated_operand = cp_unevaluated_operand;
10385 cp_unevaluated_operand = 0;
10386 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10387 c_inhibit_evaluation_warnings = 0;
10389 /* First, determine the context for the type we are looking
10390 up. */
10391 context = TYPE_CONTEXT (t);
10392 if (context && TYPE_P (context))
10394 context = tsubst_aggr_type (context, args, complain,
10395 in_decl, /*entering_scope=*/1);
10396 /* If context is a nested class inside a class template,
10397 it may still need to be instantiated (c++/33959). */
10398 context = complete_type (context);
10401 /* Then, figure out what arguments are appropriate for the
10402 type we are trying to find. For example, given:
10404 template <class T> struct S;
10405 template <class T, class U> void f(T, U) { S<U> su; }
10407 and supposing that we are instantiating f<int, double>,
10408 then our ARGS will be {int, double}, but, when looking up
10409 S we only want {double}. */
10410 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10411 complain, in_decl);
10412 if (argvec == error_mark_node)
10413 r = error_mark_node;
10414 else
10416 r = lookup_template_class (t, argvec, in_decl, context,
10417 entering_scope, complain);
10418 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10421 cp_unevaluated_operand = saved_unevaluated_operand;
10422 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10424 return r;
10426 else
10427 /* This is not a template type, so there's nothing to do. */
10428 return t;
10430 default:
10431 return tsubst (t, args, complain, in_decl);
10435 /* Substitute into the default argument ARG (a default argument for
10436 FN), which has the indicated TYPE. */
10438 tree
10439 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10441 tree saved_class_ptr = NULL_TREE;
10442 tree saved_class_ref = NULL_TREE;
10443 int errs = errorcount + sorrycount;
10445 /* This can happen in invalid code. */
10446 if (TREE_CODE (arg) == DEFAULT_ARG)
10447 return arg;
10449 /* This default argument came from a template. Instantiate the
10450 default argument here, not in tsubst. In the case of
10451 something like:
10453 template <class T>
10454 struct S {
10455 static T t();
10456 void f(T = t());
10459 we must be careful to do name lookup in the scope of S<T>,
10460 rather than in the current class. */
10461 push_access_scope (fn);
10462 /* The "this" pointer is not valid in a default argument. */
10463 if (cfun)
10465 saved_class_ptr = current_class_ptr;
10466 cp_function_chain->x_current_class_ptr = NULL_TREE;
10467 saved_class_ref = current_class_ref;
10468 cp_function_chain->x_current_class_ref = NULL_TREE;
10471 push_deferring_access_checks(dk_no_deferred);
10472 /* The default argument expression may cause implicitly defined
10473 member functions to be synthesized, which will result in garbage
10474 collection. We must treat this situation as if we were within
10475 the body of function so as to avoid collecting live data on the
10476 stack. */
10477 ++function_depth;
10478 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10479 complain, NULL_TREE,
10480 /*integral_constant_expression_p=*/false);
10481 --function_depth;
10482 pop_deferring_access_checks();
10484 /* Restore the "this" pointer. */
10485 if (cfun)
10487 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10488 cp_function_chain->x_current_class_ref = saved_class_ref;
10491 if (errorcount+sorrycount > errs
10492 && (complain & tf_warning_or_error))
10493 inform (input_location,
10494 " when instantiating default argument for call to %D", fn);
10496 /* Make sure the default argument is reasonable. */
10497 arg = check_default_argument (type, arg, complain);
10499 pop_access_scope (fn);
10501 return arg;
10504 /* Substitute into all the default arguments for FN. */
10506 static void
10507 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10509 tree arg;
10510 tree tmpl_args;
10512 tmpl_args = DECL_TI_ARGS (fn);
10514 /* If this function is not yet instantiated, we certainly don't need
10515 its default arguments. */
10516 if (uses_template_parms (tmpl_args))
10517 return;
10518 /* Don't do this again for clones. */
10519 if (DECL_CLONED_FUNCTION_P (fn))
10520 return;
10522 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10523 arg;
10524 arg = TREE_CHAIN (arg))
10525 if (TREE_PURPOSE (arg))
10526 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10527 TREE_VALUE (arg),
10528 TREE_PURPOSE (arg),
10529 complain);
10532 /* Substitute the ARGS into the T, which is a _DECL. Return the
10533 result of the substitution. Issue error and warning messages under
10534 control of COMPLAIN. */
10536 static tree
10537 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10539 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10540 location_t saved_loc;
10541 tree r = NULL_TREE;
10542 tree in_decl = t;
10543 hashval_t hash = 0;
10545 /* Set the filename and linenumber to improve error-reporting. */
10546 saved_loc = input_location;
10547 input_location = DECL_SOURCE_LOCATION (t);
10549 switch (TREE_CODE (t))
10551 case TEMPLATE_DECL:
10553 /* We can get here when processing a member function template,
10554 member class template, or template template parameter. */
10555 tree decl = DECL_TEMPLATE_RESULT (t);
10556 tree spec;
10557 tree tmpl_args;
10558 tree full_args;
10560 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10562 /* Template template parameter is treated here. */
10563 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10564 if (new_type == error_mark_node)
10565 RETURN (error_mark_node);
10566 /* If we get a real template back, return it. This can happen in
10567 the context of most_specialized_partial_spec. */
10568 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10569 return new_type;
10571 r = copy_decl (t);
10572 DECL_CHAIN (r) = NULL_TREE;
10573 TREE_TYPE (r) = new_type;
10574 DECL_TEMPLATE_RESULT (r)
10575 = build_decl (DECL_SOURCE_LOCATION (decl),
10576 TYPE_DECL, DECL_NAME (decl), new_type);
10577 DECL_TEMPLATE_PARMS (r)
10578 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10579 complain);
10580 TYPE_NAME (new_type) = r;
10581 break;
10584 /* We might already have an instance of this template.
10585 The ARGS are for the surrounding class type, so the
10586 full args contain the tsubst'd args for the context,
10587 plus the innermost args from the template decl. */
10588 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10589 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10590 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10591 /* Because this is a template, the arguments will still be
10592 dependent, even after substitution. If
10593 PROCESSING_TEMPLATE_DECL is not set, the dependency
10594 predicates will short-circuit. */
10595 ++processing_template_decl;
10596 full_args = tsubst_template_args (tmpl_args, args,
10597 complain, in_decl);
10598 --processing_template_decl;
10599 if (full_args == error_mark_node)
10600 RETURN (error_mark_node);
10602 /* If this is a default template template argument,
10603 tsubst might not have changed anything. */
10604 if (full_args == tmpl_args)
10605 RETURN (t);
10607 hash = hash_tmpl_and_args (t, full_args);
10608 spec = retrieve_specialization (t, full_args, hash);
10609 if (spec != NULL_TREE)
10611 r = spec;
10612 break;
10615 /* Make a new template decl. It will be similar to the
10616 original, but will record the current template arguments.
10617 We also create a new function declaration, which is just
10618 like the old one, but points to this new template, rather
10619 than the old one. */
10620 r = copy_decl (t);
10621 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10622 DECL_CHAIN (r) = NULL_TREE;
10624 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10626 if (TREE_CODE (decl) == TYPE_DECL
10627 && !TYPE_DECL_ALIAS_P (decl))
10629 tree new_type;
10630 ++processing_template_decl;
10631 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10632 --processing_template_decl;
10633 if (new_type == error_mark_node)
10634 RETURN (error_mark_node);
10636 TREE_TYPE (r) = new_type;
10637 /* For a partial specialization, we need to keep pointing to
10638 the primary template. */
10639 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10640 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10641 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10642 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10643 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10645 else
10647 tree new_decl;
10648 ++processing_template_decl;
10649 new_decl = tsubst (decl, args, complain, in_decl);
10650 --processing_template_decl;
10651 if (new_decl == error_mark_node)
10652 RETURN (error_mark_node);
10654 DECL_TEMPLATE_RESULT (r) = new_decl;
10655 DECL_TI_TEMPLATE (new_decl) = r;
10656 TREE_TYPE (r) = TREE_TYPE (new_decl);
10657 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10658 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10661 SET_DECL_IMPLICIT_INSTANTIATION (r);
10662 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10663 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10665 /* The template parameters for this new template are all the
10666 template parameters for the old template, except the
10667 outermost level of parameters. */
10668 DECL_TEMPLATE_PARMS (r)
10669 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10670 complain);
10672 if (PRIMARY_TEMPLATE_P (t))
10673 DECL_PRIMARY_TEMPLATE (r) = r;
10675 if (TREE_CODE (decl) != TYPE_DECL && TREE_CODE (decl) != VAR_DECL)
10676 /* Record this non-type partial instantiation. */
10677 register_specialization (r, t,
10678 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10679 false, hash);
10681 break;
10683 case FUNCTION_DECL:
10685 tree ctx;
10686 tree argvec = NULL_TREE;
10687 tree *friends;
10688 tree gen_tmpl;
10689 tree type;
10690 int member;
10691 int args_depth;
10692 int parms_depth;
10694 /* Nobody should be tsubst'ing into non-template functions. */
10695 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10697 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10699 tree spec;
10700 bool dependent_p;
10702 /* If T is not dependent, just return it. We have to
10703 increment PROCESSING_TEMPLATE_DECL because
10704 value_dependent_expression_p assumes that nothing is
10705 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10706 ++processing_template_decl;
10707 dependent_p = value_dependent_expression_p (t);
10708 --processing_template_decl;
10709 if (!dependent_p)
10710 RETURN (t);
10712 /* Calculate the most general template of which R is a
10713 specialization, and the complete set of arguments used to
10714 specialize R. */
10715 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10716 argvec = tsubst_template_args (DECL_TI_ARGS
10717 (DECL_TEMPLATE_RESULT
10718 (DECL_TI_TEMPLATE (t))),
10719 args, complain, in_decl);
10720 if (argvec == error_mark_node)
10721 RETURN (error_mark_node);
10723 /* Check to see if we already have this specialization. */
10724 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10725 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10727 if (spec)
10729 r = spec;
10730 break;
10733 /* We can see more levels of arguments than parameters if
10734 there was a specialization of a member template, like
10735 this:
10737 template <class T> struct S { template <class U> void f(); }
10738 template <> template <class U> void S<int>::f(U);
10740 Here, we'll be substituting into the specialization,
10741 because that's where we can find the code we actually
10742 want to generate, but we'll have enough arguments for
10743 the most general template.
10745 We also deal with the peculiar case:
10747 template <class T> struct S {
10748 template <class U> friend void f();
10750 template <class U> void f() {}
10751 template S<int>;
10752 template void f<double>();
10754 Here, the ARGS for the instantiation of will be {int,
10755 double}. But, we only need as many ARGS as there are
10756 levels of template parameters in CODE_PATTERN. We are
10757 careful not to get fooled into reducing the ARGS in
10758 situations like:
10760 template <class T> struct S { template <class U> void f(U); }
10761 template <class T> template <> void S<T>::f(int) {}
10763 which we can spot because the pattern will be a
10764 specialization in this case. */
10765 args_depth = TMPL_ARGS_DEPTH (args);
10766 parms_depth =
10767 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10768 if (args_depth > parms_depth
10769 && !DECL_TEMPLATE_SPECIALIZATION (t))
10770 args = get_innermost_template_args (args, parms_depth);
10772 else
10774 /* This special case arises when we have something like this:
10776 template <class T> struct S {
10777 friend void f<int>(int, double);
10780 Here, the DECL_TI_TEMPLATE for the friend declaration
10781 will be an IDENTIFIER_NODE. We are being called from
10782 tsubst_friend_function, and we want only to create a
10783 new decl (R) with appropriate types so that we can call
10784 determine_specialization. */
10785 gen_tmpl = NULL_TREE;
10788 if (DECL_CLASS_SCOPE_P (t))
10790 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10791 member = 2;
10792 else
10793 member = 1;
10794 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10795 complain, t, /*entering_scope=*/1);
10797 else
10799 member = 0;
10800 ctx = DECL_CONTEXT (t);
10802 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10803 if (type == error_mark_node)
10804 RETURN (error_mark_node);
10806 /* If we hit excessive deduction depth, the type is bogus even if
10807 it isn't error_mark_node, so don't build a decl. */
10808 if (excessive_deduction_depth)
10809 RETURN (error_mark_node);
10811 /* We do NOT check for matching decls pushed separately at this
10812 point, as they may not represent instantiations of this
10813 template, and in any case are considered separate under the
10814 discrete model. */
10815 r = copy_decl (t);
10816 DECL_USE_TEMPLATE (r) = 0;
10817 TREE_TYPE (r) = type;
10818 /* Clear out the mangled name and RTL for the instantiation. */
10819 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10820 SET_DECL_RTL (r, NULL);
10821 /* Leave DECL_INITIAL set on deleted instantiations. */
10822 if (!DECL_DELETED_FN (r))
10823 DECL_INITIAL (r) = NULL_TREE;
10824 DECL_CONTEXT (r) = ctx;
10826 /* OpenMP UDRs have the only argument a reference to the declared
10827 type. We want to diagnose if the declared type is a reference,
10828 which is invalid, but as references to references are usually
10829 quietly merged, diagnose it here. */
10830 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10832 tree argtype
10833 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10834 argtype = tsubst (argtype, args, complain, in_decl);
10835 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10836 error_at (DECL_SOURCE_LOCATION (t),
10837 "reference type %qT in "
10838 "%<#pragma omp declare reduction%>", argtype);
10839 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10840 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10841 argtype);
10844 if (member && DECL_CONV_FN_P (r))
10845 /* Type-conversion operator. Reconstruct the name, in
10846 case it's the name of one of the template's parameters. */
10847 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10849 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10850 complain, t);
10851 DECL_RESULT (r) = NULL_TREE;
10853 TREE_STATIC (r) = 0;
10854 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10855 DECL_EXTERNAL (r) = 1;
10856 /* If this is an instantiation of a function with internal
10857 linkage, we already know what object file linkage will be
10858 assigned to the instantiation. */
10859 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10860 DECL_DEFER_OUTPUT (r) = 0;
10861 DECL_CHAIN (r) = NULL_TREE;
10862 DECL_PENDING_INLINE_INFO (r) = 0;
10863 DECL_PENDING_INLINE_P (r) = 0;
10864 DECL_SAVED_TREE (r) = NULL_TREE;
10865 DECL_STRUCT_FUNCTION (r) = NULL;
10866 TREE_USED (r) = 0;
10867 /* We'll re-clone as appropriate in instantiate_template. */
10868 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10870 /* If we aren't complaining now, return on error before we register
10871 the specialization so that we'll complain eventually. */
10872 if ((complain & tf_error) == 0
10873 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10874 && !grok_op_properties (r, /*complain=*/false))
10875 RETURN (error_mark_node);
10877 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10878 this in the special friend case mentioned above where
10879 GEN_TMPL is NULL. */
10880 if (gen_tmpl)
10882 DECL_TEMPLATE_INFO (r)
10883 = build_template_info (gen_tmpl, argvec);
10884 SET_DECL_IMPLICIT_INSTANTIATION (r);
10886 tree new_r
10887 = register_specialization (r, gen_tmpl, argvec, false, hash);
10888 if (new_r != r)
10889 /* We instantiated this while substituting into
10890 the type earlier (template/friend54.C). */
10891 RETURN (new_r);
10893 /* We're not supposed to instantiate default arguments
10894 until they are called, for a template. But, for a
10895 declaration like:
10897 template <class T> void f ()
10898 { extern void g(int i = T()); }
10900 we should do the substitution when the template is
10901 instantiated. We handle the member function case in
10902 instantiate_class_template since the default arguments
10903 might refer to other members of the class. */
10904 if (!member
10905 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10906 && !uses_template_parms (argvec))
10907 tsubst_default_arguments (r, complain);
10909 else
10910 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10912 /* Copy the list of befriending classes. */
10913 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10914 *friends;
10915 friends = &TREE_CHAIN (*friends))
10917 *friends = copy_node (*friends);
10918 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10919 args, complain,
10920 in_decl);
10923 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10925 maybe_retrofit_in_chrg (r);
10926 if (DECL_CONSTRUCTOR_P (r))
10927 grok_ctor_properties (ctx, r);
10928 if (DECL_INHERITED_CTOR_BASE (r))
10929 deduce_inheriting_ctor (r);
10930 /* If this is an instantiation of a member template, clone it.
10931 If it isn't, that'll be handled by
10932 clone_constructors_and_destructors. */
10933 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10934 clone_function_decl (r, /*update_method_vec_p=*/0);
10936 else if ((complain & tf_error) != 0
10937 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10938 && !grok_op_properties (r, /*complain=*/true))
10939 RETURN (error_mark_node);
10941 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10942 SET_DECL_FRIEND_CONTEXT (r,
10943 tsubst (DECL_FRIEND_CONTEXT (t),
10944 args, complain, in_decl));
10946 /* Possibly limit visibility based on template args. */
10947 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10948 if (DECL_VISIBILITY_SPECIFIED (t))
10950 DECL_VISIBILITY_SPECIFIED (r) = 0;
10951 DECL_ATTRIBUTES (r)
10952 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10954 determine_visibility (r);
10955 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10956 && !processing_template_decl)
10957 defaulted_late_check (r);
10959 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10960 args, complain, in_decl);
10962 break;
10964 case PARM_DECL:
10966 tree type = NULL_TREE;
10967 int i, len = 1;
10968 tree expanded_types = NULL_TREE;
10969 tree prev_r = NULL_TREE;
10970 tree first_r = NULL_TREE;
10972 if (DECL_PACK_P (t))
10974 /* If there is a local specialization that isn't a
10975 parameter pack, it means that we're doing a "simple"
10976 substitution from inside tsubst_pack_expansion. Just
10977 return the local specialization (which will be a single
10978 parm). */
10979 tree spec = retrieve_local_specialization (t);
10980 if (spec
10981 && TREE_CODE (spec) == PARM_DECL
10982 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10983 RETURN (spec);
10985 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10986 the parameters in this function parameter pack. */
10987 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10988 complain, in_decl);
10989 if (TREE_CODE (expanded_types) == TREE_VEC)
10991 len = TREE_VEC_LENGTH (expanded_types);
10993 /* Zero-length parameter packs are boring. Just substitute
10994 into the chain. */
10995 if (len == 0)
10996 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10997 TREE_CHAIN (t)));
10999 else
11001 /* All we did was update the type. Make a note of that. */
11002 type = expanded_types;
11003 expanded_types = NULL_TREE;
11007 /* Loop through all of the parameters we'll build. When T is
11008 a function parameter pack, LEN is the number of expanded
11009 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11010 r = NULL_TREE;
11011 for (i = 0; i < len; ++i)
11013 prev_r = r;
11014 r = copy_node (t);
11015 if (DECL_TEMPLATE_PARM_P (t))
11016 SET_DECL_TEMPLATE_PARM_P (r);
11018 if (expanded_types)
11019 /* We're on the Ith parameter of the function parameter
11020 pack. */
11022 /* Get the Ith type. */
11023 type = TREE_VEC_ELT (expanded_types, i);
11025 /* Rename the parameter to include the index. */
11026 DECL_NAME (r)
11027 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11029 else if (!type)
11030 /* We're dealing with a normal parameter. */
11031 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11033 type = type_decays_to (type);
11034 TREE_TYPE (r) = type;
11035 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11037 if (DECL_INITIAL (r))
11039 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11040 DECL_INITIAL (r) = TREE_TYPE (r);
11041 else
11042 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11043 complain, in_decl);
11046 DECL_CONTEXT (r) = NULL_TREE;
11048 if (!DECL_TEMPLATE_PARM_P (r))
11049 DECL_ARG_TYPE (r) = type_passed_as (type);
11051 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11052 args, complain, in_decl);
11054 /* Keep track of the first new parameter we
11055 generate. That's what will be returned to the
11056 caller. */
11057 if (!first_r)
11058 first_r = r;
11060 /* Build a proper chain of parameters when substituting
11061 into a function parameter pack. */
11062 if (prev_r)
11063 DECL_CHAIN (prev_r) = r;
11066 /* If cp_unevaluated_operand is set, we're just looking for a
11067 single dummy parameter, so don't keep going. */
11068 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11069 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11070 complain, DECL_CHAIN (t));
11072 /* FIRST_R contains the start of the chain we've built. */
11073 r = first_r;
11075 break;
11077 case FIELD_DECL:
11079 tree type = NULL_TREE;
11080 tree vec = NULL_TREE;
11081 tree expanded_types = NULL_TREE;
11082 int len = 1;
11084 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11086 /* This field is a lambda capture pack. Return a TREE_VEC of
11087 the expanded fields to instantiate_class_template_1 and
11088 store them in the specializations hash table as a
11089 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11090 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11091 complain, in_decl);
11092 if (TREE_CODE (expanded_types) == TREE_VEC)
11094 len = TREE_VEC_LENGTH (expanded_types);
11095 vec = make_tree_vec (len);
11097 else
11099 /* All we did was update the type. Make a note of that. */
11100 type = expanded_types;
11101 expanded_types = NULL_TREE;
11105 for (int i = 0; i < len; ++i)
11107 r = copy_decl (t);
11108 if (expanded_types)
11110 type = TREE_VEC_ELT (expanded_types, i);
11111 DECL_NAME (r)
11112 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11114 else if (!type)
11115 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11117 if (type == error_mark_node)
11118 RETURN (error_mark_node);
11119 TREE_TYPE (r) = type;
11120 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11122 if (DECL_C_BIT_FIELD (r))
11123 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11124 non-bit-fields DECL_INITIAL is a non-static data member
11125 initializer, which gets deferred instantiation. */
11126 DECL_INITIAL (r)
11127 = tsubst_expr (DECL_INITIAL (t), args,
11128 complain, in_decl,
11129 /*integral_constant_expression_p=*/true);
11130 else if (DECL_INITIAL (t))
11132 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11133 NSDMI in perform_member_init. Still set DECL_INITIAL
11134 so that we know there is one. */
11135 DECL_INITIAL (r) = void_node;
11136 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11137 retrofit_lang_decl (r);
11138 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11140 /* We don't have to set DECL_CONTEXT here; it is set by
11141 finish_member_declaration. */
11142 DECL_CHAIN (r) = NULL_TREE;
11144 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11145 args, complain, in_decl);
11147 if (vec)
11148 TREE_VEC_ELT (vec, i) = r;
11151 if (vec)
11153 r = vec;
11154 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11155 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11156 SET_ARGUMENT_PACK_ARGS (pack, vec);
11157 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11158 TREE_TYPE (pack) = tpack;
11159 register_specialization (pack, t, args, false, 0);
11162 break;
11164 case USING_DECL:
11165 /* We reach here only for member using decls. We also need to check
11166 uses_template_parms because DECL_DEPENDENT_P is not set for a
11167 using-declaration that designates a member of the current
11168 instantiation (c++/53549). */
11169 if (DECL_DEPENDENT_P (t)
11170 || uses_template_parms (USING_DECL_SCOPE (t)))
11172 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11173 complain, in_decl);
11174 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11175 r = do_class_using_decl (inst_scope, name);
11176 if (!r)
11177 r = error_mark_node;
11178 else
11180 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11181 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11184 else
11186 r = copy_node (t);
11187 DECL_CHAIN (r) = NULL_TREE;
11189 break;
11191 case TYPE_DECL:
11192 case VAR_DECL:
11194 tree argvec = NULL_TREE;
11195 tree gen_tmpl = NULL_TREE;
11196 tree spec;
11197 tree tmpl = NULL_TREE;
11198 tree ctx;
11199 tree type = NULL_TREE;
11200 bool local_p;
11202 if (TREE_TYPE (t) == error_mark_node)
11203 RETURN (error_mark_node);
11205 if (TREE_CODE (t) == TYPE_DECL
11206 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11208 /* If this is the canonical decl, we don't have to
11209 mess with instantiations, and often we can't (for
11210 typename, template type parms and such). Note that
11211 TYPE_NAME is not correct for the above test if
11212 we've copied the type for a typedef. */
11213 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11214 if (type == error_mark_node)
11215 RETURN (error_mark_node);
11216 r = TYPE_NAME (type);
11217 break;
11220 /* Check to see if we already have the specialization we
11221 need. */
11222 spec = NULL_TREE;
11223 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11225 /* T is a static data member or namespace-scope entity.
11226 We have to substitute into namespace-scope variables
11227 (even though such entities are never templates) because
11228 of cases like:
11230 template <class T> void f() { extern T t; }
11232 where the entity referenced is not known until
11233 instantiation time. */
11234 local_p = false;
11235 ctx = DECL_CONTEXT (t);
11236 if (DECL_CLASS_SCOPE_P (t))
11238 ctx = tsubst_aggr_type (ctx, args,
11239 complain,
11240 in_decl, /*entering_scope=*/1);
11241 /* If CTX is unchanged, then T is in fact the
11242 specialization we want. That situation occurs when
11243 referencing a static data member within in its own
11244 class. We can use pointer equality, rather than
11245 same_type_p, because DECL_CONTEXT is always
11246 canonical... */
11247 if (ctx == DECL_CONTEXT (t)
11248 /* ... unless T is a member template; in which
11249 case our caller can be willing to create a
11250 specialization of that template represented
11251 by T. */
11252 && !(DECL_TI_TEMPLATE (t)
11253 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
11254 spec = t;
11257 if (!spec)
11259 tmpl = DECL_TI_TEMPLATE (t);
11260 gen_tmpl = most_general_template (tmpl);
11261 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11262 if (argvec == error_mark_node)
11263 RETURN (error_mark_node);
11264 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11265 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11268 else
11270 /* A local variable. */
11271 local_p = true;
11272 /* Subsequent calls to pushdecl will fill this in. */
11273 ctx = NULL_TREE;
11274 spec = retrieve_local_specialization (t);
11276 /* If we already have the specialization we need, there is
11277 nothing more to do. */
11278 if (spec)
11280 r = spec;
11281 break;
11284 /* Create a new node for the specialization we need. */
11285 r = copy_decl (t);
11286 if (type == NULL_TREE)
11288 if (is_typedef_decl (t))
11289 type = DECL_ORIGINAL_TYPE (t);
11290 else
11291 type = TREE_TYPE (t);
11292 if (VAR_P (t)
11293 && VAR_HAD_UNKNOWN_BOUND (t)
11294 && type != error_mark_node)
11295 type = strip_array_domain (type);
11296 type = tsubst (type, args, complain, in_decl);
11298 if (VAR_P (r))
11300 /* Even if the original location is out of scope, the
11301 newly substituted one is not. */
11302 DECL_DEAD_FOR_LOCAL (r) = 0;
11303 DECL_INITIALIZED_P (r) = 0;
11304 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11305 if (type == error_mark_node)
11306 RETURN (error_mark_node);
11307 if (TREE_CODE (type) == FUNCTION_TYPE)
11309 /* It may seem that this case cannot occur, since:
11311 typedef void f();
11312 void g() { f x; }
11314 declares a function, not a variable. However:
11316 typedef void f();
11317 template <typename T> void g() { T t; }
11318 template void g<f>();
11320 is an attempt to declare a variable with function
11321 type. */
11322 error ("variable %qD has function type",
11323 /* R is not yet sufficiently initialized, so we
11324 just use its name. */
11325 DECL_NAME (r));
11326 RETURN (error_mark_node);
11328 type = complete_type (type);
11329 /* Wait until cp_finish_decl to set this again, to handle
11330 circular dependency (template/instantiate6.C). */
11331 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11332 type = check_var_type (DECL_NAME (r), type);
11334 if (DECL_HAS_VALUE_EXPR_P (t))
11336 tree ve = DECL_VALUE_EXPR (t);
11337 ve = tsubst_expr (ve, args, complain, in_decl,
11338 /*constant_expression_p=*/false);
11339 if (REFERENCE_REF_P (ve))
11341 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11342 ve = TREE_OPERAND (ve, 0);
11344 SET_DECL_VALUE_EXPR (r, ve);
11346 if (TREE_STATIC (r) || DECL_EXTERNAL (r))
11347 set_decl_tls_model (r, decl_tls_model (t));
11349 else if (DECL_SELF_REFERENCE_P (t))
11350 SET_DECL_SELF_REFERENCE_P (r);
11351 TREE_TYPE (r) = type;
11352 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11353 DECL_CONTEXT (r) = ctx;
11354 /* Clear out the mangled name and RTL for the instantiation. */
11355 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11356 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11357 SET_DECL_RTL (r, NULL);
11358 /* The initializer must not be expanded until it is required;
11359 see [temp.inst]. */
11360 DECL_INITIAL (r) = NULL_TREE;
11361 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11362 SET_DECL_RTL (r, NULL);
11363 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11364 if (VAR_P (r))
11366 /* Possibly limit visibility based on template args. */
11367 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11368 if (DECL_VISIBILITY_SPECIFIED (t))
11370 DECL_VISIBILITY_SPECIFIED (r) = 0;
11371 DECL_ATTRIBUTES (r)
11372 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11374 determine_visibility (r);
11377 if (!local_p)
11379 /* A static data member declaration is always marked
11380 external when it is declared in-class, even if an
11381 initializer is present. We mimic the non-template
11382 processing here. */
11383 DECL_EXTERNAL (r) = 1;
11385 register_specialization (r, gen_tmpl, argvec, false, hash);
11386 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11387 SET_DECL_IMPLICIT_INSTANTIATION (r);
11389 else if (!cp_unevaluated_operand)
11390 register_local_specialization (r, t);
11392 DECL_CHAIN (r) = NULL_TREE;
11394 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11395 /*flags=*/0,
11396 args, complain, in_decl);
11398 /* Preserve a typedef that names a type. */
11399 if (is_typedef_decl (r))
11401 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11402 set_underlying_type (r);
11405 layout_decl (r, 0);
11407 break;
11409 default:
11410 gcc_unreachable ();
11412 #undef RETURN
11414 out:
11415 /* Restore the file and line information. */
11416 input_location = saved_loc;
11418 return r;
11421 /* Substitute into the ARG_TYPES of a function type.
11422 If END is a TREE_CHAIN, leave it and any following types
11423 un-substituted. */
11425 static tree
11426 tsubst_arg_types (tree arg_types,
11427 tree args,
11428 tree end,
11429 tsubst_flags_t complain,
11430 tree in_decl)
11432 tree remaining_arg_types;
11433 tree type = NULL_TREE;
11434 int i = 1;
11435 tree expanded_args = NULL_TREE;
11436 tree default_arg;
11438 if (!arg_types || arg_types == void_list_node || arg_types == end)
11439 return arg_types;
11441 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11442 args, end, complain, in_decl);
11443 if (remaining_arg_types == error_mark_node)
11444 return error_mark_node;
11446 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11448 /* For a pack expansion, perform substitution on the
11449 entire expression. Later on, we'll handle the arguments
11450 one-by-one. */
11451 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11452 args, complain, in_decl);
11454 if (TREE_CODE (expanded_args) == TREE_VEC)
11455 /* So that we'll spin through the parameters, one by one. */
11456 i = TREE_VEC_LENGTH (expanded_args);
11457 else
11459 /* We only partially substituted into the parameter
11460 pack. Our type is TYPE_PACK_EXPANSION. */
11461 type = expanded_args;
11462 expanded_args = NULL_TREE;
11466 while (i > 0) {
11467 --i;
11469 if (expanded_args)
11470 type = TREE_VEC_ELT (expanded_args, i);
11471 else if (!type)
11472 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11474 if (type == error_mark_node)
11475 return error_mark_node;
11476 if (VOID_TYPE_P (type))
11478 if (complain & tf_error)
11480 error ("invalid parameter type %qT", type);
11481 if (in_decl)
11482 error ("in declaration %q+D", in_decl);
11484 return error_mark_node;
11486 /* DR 657. */
11487 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11488 return error_mark_node;
11490 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11491 top-level qualifiers as required. */
11492 type = cv_unqualified (type_decays_to (type));
11494 /* We do not substitute into default arguments here. The standard
11495 mandates that they be instantiated only when needed, which is
11496 done in build_over_call. */
11497 default_arg = TREE_PURPOSE (arg_types);
11499 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11501 /* We've instantiated a template before its default arguments
11502 have been parsed. This can happen for a nested template
11503 class, and is not an error unless we require the default
11504 argument in a call of this function. */
11505 remaining_arg_types =
11506 tree_cons (default_arg, type, remaining_arg_types);
11507 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11509 else
11510 remaining_arg_types =
11511 hash_tree_cons (default_arg, type, remaining_arg_types);
11514 return remaining_arg_types;
11517 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11518 *not* handle the exception-specification for FNTYPE, because the
11519 initial substitution of explicitly provided template parameters
11520 during argument deduction forbids substitution into the
11521 exception-specification:
11523 [temp.deduct]
11525 All references in the function type of the function template to the
11526 corresponding template parameters are replaced by the specified tem-
11527 plate argument values. If a substitution in a template parameter or
11528 in the function type of the function template results in an invalid
11529 type, type deduction fails. [Note: The equivalent substitution in
11530 exception specifications is done only when the function is instanti-
11531 ated, at which point a program is ill-formed if the substitution
11532 results in an invalid type.] */
11534 static tree
11535 tsubst_function_type (tree t,
11536 tree args,
11537 tsubst_flags_t complain,
11538 tree in_decl)
11540 tree return_type;
11541 tree arg_types = NULL_TREE;
11542 tree fntype;
11544 /* The TYPE_CONTEXT is not used for function/method types. */
11545 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11547 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11548 failure. */
11549 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
11551 if (late_return_type_p)
11553 /* Substitute the argument types. */
11554 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11555 complain, in_decl);
11556 if (arg_types == error_mark_node)
11557 return error_mark_node;
11559 tree save_ccp = current_class_ptr;
11560 tree save_ccr = current_class_ref;
11561 tree this_type = (TREE_CODE (t) == METHOD_TYPE
11562 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
11563 bool do_inject = this_type && CLASS_TYPE_P (this_type);
11564 if (do_inject)
11566 /* DR 1207: 'this' is in scope in the trailing return type. */
11567 inject_this_parameter (this_type, cp_type_quals (this_type));
11570 /* Substitute the return type. */
11571 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11573 if (do_inject)
11575 current_class_ptr = save_ccp;
11576 current_class_ref = save_ccr;
11579 else
11580 /* Substitute the return type. */
11581 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11583 if (return_type == error_mark_node)
11584 return error_mark_node;
11585 /* DR 486 clarifies that creation of a function type with an
11586 invalid return type is a deduction failure. */
11587 if (TREE_CODE (return_type) == ARRAY_TYPE
11588 || TREE_CODE (return_type) == FUNCTION_TYPE)
11590 if (complain & tf_error)
11592 if (TREE_CODE (return_type) == ARRAY_TYPE)
11593 error ("function returning an array");
11594 else
11595 error ("function returning a function");
11597 return error_mark_node;
11599 /* And DR 657. */
11600 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11601 return error_mark_node;
11603 if (!late_return_type_p)
11605 /* Substitute the argument types. */
11606 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11607 complain, in_decl);
11608 if (arg_types == error_mark_node)
11609 return error_mark_node;
11612 /* Construct a new type node and return it. */
11613 if (TREE_CODE (t) == FUNCTION_TYPE)
11615 fntype = build_function_type (return_type, arg_types);
11616 fntype = apply_memfn_quals (fntype,
11617 type_memfn_quals (t),
11618 type_memfn_rqual (t));
11620 else
11622 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11623 /* Don't pick up extra function qualifiers from the basetype. */
11624 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11625 if (! MAYBE_CLASS_TYPE_P (r))
11627 /* [temp.deduct]
11629 Type deduction may fail for any of the following
11630 reasons:
11632 -- Attempting to create "pointer to member of T" when T
11633 is not a class type. */
11634 if (complain & tf_error)
11635 error ("creating pointer to member function of non-class type %qT",
11637 return error_mark_node;
11640 fntype = build_method_type_directly (r, return_type,
11641 TREE_CHAIN (arg_types));
11642 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11644 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11646 if (late_return_type_p)
11647 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
11649 return fntype;
11652 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11653 ARGS into that specification, and return the substituted
11654 specification. If there is no specification, return NULL_TREE. */
11656 static tree
11657 tsubst_exception_specification (tree fntype,
11658 tree args,
11659 tsubst_flags_t complain,
11660 tree in_decl,
11661 bool defer_ok)
11663 tree specs;
11664 tree new_specs;
11666 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11667 new_specs = NULL_TREE;
11668 if (specs && TREE_PURPOSE (specs))
11670 /* A noexcept-specifier. */
11671 tree expr = TREE_PURPOSE (specs);
11672 if (TREE_CODE (expr) == INTEGER_CST)
11673 new_specs = expr;
11674 else if (defer_ok)
11676 /* Defer instantiation of noexcept-specifiers to avoid
11677 excessive instantiations (c++/49107). */
11678 new_specs = make_node (DEFERRED_NOEXCEPT);
11679 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11681 /* We already partially instantiated this member template,
11682 so combine the new args with the old. */
11683 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11684 = DEFERRED_NOEXCEPT_PATTERN (expr);
11685 DEFERRED_NOEXCEPT_ARGS (new_specs)
11686 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11688 else
11690 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11691 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11694 else
11695 new_specs = tsubst_copy_and_build
11696 (expr, args, complain, in_decl, /*function_p=*/false,
11697 /*integral_constant_expression_p=*/true);
11698 new_specs = build_noexcept_spec (new_specs, complain);
11700 else if (specs)
11702 if (! TREE_VALUE (specs))
11703 new_specs = specs;
11704 else
11705 while (specs)
11707 tree spec;
11708 int i, len = 1;
11709 tree expanded_specs = NULL_TREE;
11711 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11713 /* Expand the pack expansion type. */
11714 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11715 args, complain,
11716 in_decl);
11718 if (expanded_specs == error_mark_node)
11719 return error_mark_node;
11720 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11721 len = TREE_VEC_LENGTH (expanded_specs);
11722 else
11724 /* We're substituting into a member template, so
11725 we got a TYPE_PACK_EXPANSION back. Add that
11726 expansion and move on. */
11727 gcc_assert (TREE_CODE (expanded_specs)
11728 == TYPE_PACK_EXPANSION);
11729 new_specs = add_exception_specifier (new_specs,
11730 expanded_specs,
11731 complain);
11732 specs = TREE_CHAIN (specs);
11733 continue;
11737 for (i = 0; i < len; ++i)
11739 if (expanded_specs)
11740 spec = TREE_VEC_ELT (expanded_specs, i);
11741 else
11742 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11743 if (spec == error_mark_node)
11744 return spec;
11745 new_specs = add_exception_specifier (new_specs, spec,
11746 complain);
11749 specs = TREE_CHAIN (specs);
11752 return new_specs;
11755 /* Take the tree structure T and replace template parameters used
11756 therein with the argument vector ARGS. IN_DECL is an associated
11757 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11758 Issue error and warning messages under control of COMPLAIN. Note
11759 that we must be relatively non-tolerant of extensions here, in
11760 order to preserve conformance; if we allow substitutions that
11761 should not be allowed, we may allow argument deductions that should
11762 not succeed, and therefore report ambiguous overload situations
11763 where there are none. In theory, we could allow the substitution,
11764 but indicate that it should have failed, and allow our caller to
11765 make sure that the right thing happens, but we don't try to do this
11766 yet.
11768 This function is used for dealing with types, decls and the like;
11769 for expressions, use tsubst_expr or tsubst_copy. */
11771 tree
11772 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11774 enum tree_code code;
11775 tree type, r = NULL_TREE;
11777 if (t == NULL_TREE || t == error_mark_node
11778 || t == integer_type_node
11779 || t == void_type_node
11780 || t == char_type_node
11781 || t == unknown_type_node
11782 || TREE_CODE (t) == NAMESPACE_DECL
11783 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11784 return t;
11786 if (DECL_P (t))
11787 return tsubst_decl (t, args, complain);
11789 if (args == NULL_TREE)
11790 return t;
11792 code = TREE_CODE (t);
11794 if (code == IDENTIFIER_NODE)
11795 type = IDENTIFIER_TYPE_VALUE (t);
11796 else
11797 type = TREE_TYPE (t);
11799 gcc_assert (type != unknown_type_node);
11801 /* Reuse typedefs. We need to do this to handle dependent attributes,
11802 such as attribute aligned. */
11803 if (TYPE_P (t)
11804 && typedef_variant_p (t))
11806 tree decl = TYPE_NAME (t);
11808 if (alias_template_specialization_p (t))
11810 /* DECL represents an alias template and we want to
11811 instantiate it. */
11812 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11813 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11814 r = instantiate_alias_template (tmpl, gen_args, complain);
11816 else if (DECL_CLASS_SCOPE_P (decl)
11817 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11818 && uses_template_parms (DECL_CONTEXT (decl)))
11820 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11821 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11822 r = retrieve_specialization (tmpl, gen_args, 0);
11824 else if (DECL_FUNCTION_SCOPE_P (decl)
11825 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11826 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11827 r = retrieve_local_specialization (decl);
11828 else
11829 /* The typedef is from a non-template context. */
11830 return t;
11832 if (r)
11834 r = TREE_TYPE (r);
11835 r = cp_build_qualified_type_real
11836 (r, cp_type_quals (t) | cp_type_quals (r),
11837 complain | tf_ignore_bad_quals);
11838 return r;
11840 else
11842 /* We don't have an instantiation yet, so drop the typedef. */
11843 int quals = cp_type_quals (t);
11844 t = DECL_ORIGINAL_TYPE (decl);
11845 t = cp_build_qualified_type_real (t, quals,
11846 complain | tf_ignore_bad_quals);
11850 if (type
11851 && code != TYPENAME_TYPE
11852 && code != TEMPLATE_TYPE_PARM
11853 && code != IDENTIFIER_NODE
11854 && code != FUNCTION_TYPE
11855 && code != METHOD_TYPE)
11856 type = tsubst (type, args, complain, in_decl);
11857 if (type == error_mark_node)
11858 return error_mark_node;
11860 switch (code)
11862 case RECORD_TYPE:
11863 case UNION_TYPE:
11864 case ENUMERAL_TYPE:
11865 return tsubst_aggr_type (t, args, complain, in_decl,
11866 /*entering_scope=*/0);
11868 case ERROR_MARK:
11869 case IDENTIFIER_NODE:
11870 case VOID_TYPE:
11871 case REAL_TYPE:
11872 case COMPLEX_TYPE:
11873 case VECTOR_TYPE:
11874 case BOOLEAN_TYPE:
11875 case NULLPTR_TYPE:
11876 case LANG_TYPE:
11877 return t;
11879 case INTEGER_TYPE:
11880 if (t == integer_type_node)
11881 return t;
11883 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11884 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11885 return t;
11888 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11890 max = tsubst_expr (omax, args, complain, in_decl,
11891 /*integral_constant_expression_p=*/false);
11893 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11894 needed. */
11895 if (TREE_CODE (max) == NOP_EXPR
11896 && TREE_SIDE_EFFECTS (omax)
11897 && !TREE_TYPE (max))
11898 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11900 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11901 with TREE_SIDE_EFFECTS that indicates this is not an integral
11902 constant expression. */
11903 if (processing_template_decl
11904 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11906 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11907 TREE_SIDE_EFFECTS (max) = 1;
11910 return compute_array_index_type (NULL_TREE, max, complain);
11913 case TEMPLATE_TYPE_PARM:
11914 case TEMPLATE_TEMPLATE_PARM:
11915 case BOUND_TEMPLATE_TEMPLATE_PARM:
11916 case TEMPLATE_PARM_INDEX:
11918 int idx;
11919 int level;
11920 int levels;
11921 tree arg = NULL_TREE;
11923 r = NULL_TREE;
11925 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11926 template_parm_level_and_index (t, &level, &idx);
11928 levels = TMPL_ARGS_DEPTH (args);
11929 if (level <= levels)
11931 arg = TMPL_ARG (args, level, idx);
11933 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11935 /* See through ARGUMENT_PACK_SELECT arguments. */
11936 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11937 /* If the selected argument is an expansion E, that most
11938 likely means we were called from
11939 gen_elem_of_pack_expansion_instantiation during the
11940 substituting of pack an argument pack (which Ith
11941 element is a pack expansion, where I is
11942 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11943 In this case, the Ith element resulting from this
11944 substituting is going to be a pack expansion, which
11945 pattern is the pattern of E. Let's return the
11946 pattern of E, and
11947 gen_elem_of_pack_expansion_instantiation will
11948 build the resulting pack expansion from it. */
11949 if (PACK_EXPANSION_P (arg))
11951 /* Make sure we aren't throwing away arg info. */
11952 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
11953 arg = PACK_EXPANSION_PATTERN (arg);
11958 if (arg == error_mark_node)
11959 return error_mark_node;
11960 else if (arg != NULL_TREE)
11962 if (ARGUMENT_PACK_P (arg))
11963 /* If ARG is an argument pack, we don't actually want to
11964 perform a substitution here, because substitutions
11965 for argument packs are only done
11966 element-by-element. We can get to this point when
11967 substituting the type of a non-type template
11968 parameter pack, when that type actually contains
11969 template parameter packs from an outer template, e.g.,
11971 template<typename... Types> struct A {
11972 template<Types... Values> struct B { };
11973 }; */
11974 return t;
11976 if (code == TEMPLATE_TYPE_PARM)
11978 int quals;
11979 gcc_assert (TYPE_P (arg));
11981 quals = cp_type_quals (arg) | cp_type_quals (t);
11983 return cp_build_qualified_type_real
11984 (arg, quals, complain | tf_ignore_bad_quals);
11986 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11988 /* We are processing a type constructed from a
11989 template template parameter. */
11990 tree argvec = tsubst (TYPE_TI_ARGS (t),
11991 args, complain, in_decl);
11992 if (argvec == error_mark_node)
11993 return error_mark_node;
11995 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11996 || TREE_CODE (arg) == TEMPLATE_DECL
11997 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11999 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12000 /* Consider this code:
12002 template <template <class> class Template>
12003 struct Internal {
12004 template <class Arg> using Bind = Template<Arg>;
12007 template <template <class> class Template, class Arg>
12008 using Instantiate = Template<Arg>; //#0
12010 template <template <class> class Template,
12011 class Argument>
12012 using Bind =
12013 Instantiate<Internal<Template>::template Bind,
12014 Argument>; //#1
12016 When #1 is parsed, the
12017 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12018 parameter `Template' in #0 matches the
12019 UNBOUND_CLASS_TEMPLATE representing the argument
12020 `Internal<Template>::template Bind'; We then want
12021 to assemble the type `Bind<Argument>' that can't
12022 be fully created right now, because
12023 `Internal<Template>' not being complete, the Bind
12024 template cannot be looked up in that context. So
12025 we need to "store" `Bind<Argument>' for later
12026 when the context of Bind becomes complete. Let's
12027 store that in a TYPENAME_TYPE. */
12028 return make_typename_type (TYPE_CONTEXT (arg),
12029 build_nt (TEMPLATE_ID_EXPR,
12030 TYPE_IDENTIFIER (arg),
12031 argvec),
12032 typename_type,
12033 complain);
12035 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12036 are resolving nested-types in the signature of a
12037 member function templates. Otherwise ARG is a
12038 TEMPLATE_DECL and is the real template to be
12039 instantiated. */
12040 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12041 arg = TYPE_NAME (arg);
12043 r = lookup_template_class (arg,
12044 argvec, in_decl,
12045 DECL_CONTEXT (arg),
12046 /*entering_scope=*/0,
12047 complain);
12048 return cp_build_qualified_type_real
12049 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12051 else
12052 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12053 return convert_from_reference (unshare_expr (arg));
12056 if (level == 1)
12057 /* This can happen during the attempted tsubst'ing in
12058 unify. This means that we don't yet have any information
12059 about the template parameter in question. */
12060 return t;
12062 /* Early in template argument deduction substitution, we don't
12063 want to reduce the level of 'auto', or it will be confused
12064 with a normal template parm in subsequent deduction. */
12065 if (is_auto (t) && (complain & tf_partial))
12066 return t;
12068 /* If we get here, we must have been looking at a parm for a
12069 more deeply nested template. Make a new version of this
12070 template parameter, but with a lower level. */
12071 switch (code)
12073 case TEMPLATE_TYPE_PARM:
12074 case TEMPLATE_TEMPLATE_PARM:
12075 case BOUND_TEMPLATE_TEMPLATE_PARM:
12076 if (cp_type_quals (t))
12078 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12079 r = cp_build_qualified_type_real
12080 (r, cp_type_quals (t),
12081 complain | (code == TEMPLATE_TYPE_PARM
12082 ? tf_ignore_bad_quals : 0));
12084 else
12086 r = copy_type (t);
12087 TEMPLATE_TYPE_PARM_INDEX (r)
12088 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12089 r, levels, args, complain);
12090 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12091 TYPE_MAIN_VARIANT (r) = r;
12092 TYPE_POINTER_TO (r) = NULL_TREE;
12093 TYPE_REFERENCE_TO (r) = NULL_TREE;
12095 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12096 /* We have reduced the level of the template
12097 template parameter, but not the levels of its
12098 template parameters, so canonical_type_parameter
12099 will not be able to find the canonical template
12100 template parameter for this level. Thus, we
12101 require structural equality checking to compare
12102 TEMPLATE_TEMPLATE_PARMs. */
12103 SET_TYPE_STRUCTURAL_EQUALITY (r);
12104 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12105 SET_TYPE_STRUCTURAL_EQUALITY (r);
12106 else
12107 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12109 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12111 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12112 complain, in_decl);
12113 if (argvec == error_mark_node)
12114 return error_mark_node;
12116 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12117 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12120 break;
12122 case TEMPLATE_PARM_INDEX:
12123 r = reduce_template_parm_level (t, type, levels, args, complain);
12124 break;
12126 default:
12127 gcc_unreachable ();
12130 return r;
12133 case TREE_LIST:
12135 tree purpose, value, chain;
12137 if (t == void_list_node)
12138 return t;
12140 purpose = TREE_PURPOSE (t);
12141 if (purpose)
12143 purpose = tsubst (purpose, args, complain, in_decl);
12144 if (purpose == error_mark_node)
12145 return error_mark_node;
12147 value = TREE_VALUE (t);
12148 if (value)
12150 value = tsubst (value, args, complain, in_decl);
12151 if (value == error_mark_node)
12152 return error_mark_node;
12154 chain = TREE_CHAIN (t);
12155 if (chain && chain != void_type_node)
12157 chain = tsubst (chain, args, complain, in_decl);
12158 if (chain == error_mark_node)
12159 return error_mark_node;
12161 if (purpose == TREE_PURPOSE (t)
12162 && value == TREE_VALUE (t)
12163 && chain == TREE_CHAIN (t))
12164 return t;
12165 return hash_tree_cons (purpose, value, chain);
12168 case TREE_BINFO:
12169 /* We should never be tsubsting a binfo. */
12170 gcc_unreachable ();
12172 case TREE_VEC:
12173 /* A vector of template arguments. */
12174 gcc_assert (!type);
12175 return tsubst_template_args (t, args, complain, in_decl);
12177 case POINTER_TYPE:
12178 case REFERENCE_TYPE:
12180 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12181 return t;
12183 /* [temp.deduct]
12185 Type deduction may fail for any of the following
12186 reasons:
12188 -- Attempting to create a pointer to reference type.
12189 -- Attempting to create a reference to a reference type or
12190 a reference to void.
12192 Core issue 106 says that creating a reference to a reference
12193 during instantiation is no longer a cause for failure. We
12194 only enforce this check in strict C++98 mode. */
12195 if ((TREE_CODE (type) == REFERENCE_TYPE
12196 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12197 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12199 static location_t last_loc;
12201 /* We keep track of the last time we issued this error
12202 message to avoid spewing a ton of messages during a
12203 single bad template instantiation. */
12204 if (complain & tf_error
12205 && last_loc != input_location)
12207 if (VOID_TYPE_P (type))
12208 error ("forming reference to void");
12209 else if (code == POINTER_TYPE)
12210 error ("forming pointer to reference type %qT", type);
12211 else
12212 error ("forming reference to reference type %qT", type);
12213 last_loc = input_location;
12216 return error_mark_node;
12218 else if (TREE_CODE (type) == FUNCTION_TYPE
12219 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12220 || type_memfn_rqual (type) != REF_QUAL_NONE))
12222 if (complain & tf_error)
12224 if (code == POINTER_TYPE)
12225 error ("forming pointer to qualified function type %qT",
12226 type);
12227 else
12228 error ("forming reference to qualified function type %qT",
12229 type);
12231 return error_mark_node;
12233 else if (code == POINTER_TYPE)
12235 r = build_pointer_type (type);
12236 if (TREE_CODE (type) == METHOD_TYPE)
12237 r = build_ptrmemfunc_type (r);
12239 else if (TREE_CODE (type) == REFERENCE_TYPE)
12240 /* In C++0x, during template argument substitution, when there is an
12241 attempt to create a reference to a reference type, reference
12242 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12244 "If a template-argument for a template-parameter T names a type
12245 that is a reference to a type A, an attempt to create the type
12246 'lvalue reference to cv T' creates the type 'lvalue reference to
12247 A,' while an attempt to create the type type rvalue reference to
12248 cv T' creates the type T"
12250 r = cp_build_reference_type
12251 (TREE_TYPE (type),
12252 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12253 else
12254 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12255 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12257 if (r != error_mark_node)
12258 /* Will this ever be needed for TYPE_..._TO values? */
12259 layout_type (r);
12261 return r;
12263 case OFFSET_TYPE:
12265 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12266 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12268 /* [temp.deduct]
12270 Type deduction may fail for any of the following
12271 reasons:
12273 -- Attempting to create "pointer to member of T" when T
12274 is not a class type. */
12275 if (complain & tf_error)
12276 error ("creating pointer to member of non-class type %qT", r);
12277 return error_mark_node;
12279 if (TREE_CODE (type) == REFERENCE_TYPE)
12281 if (complain & tf_error)
12282 error ("creating pointer to member reference type %qT", type);
12283 return error_mark_node;
12285 if (VOID_TYPE_P (type))
12287 if (complain & tf_error)
12288 error ("creating pointer to member of type void");
12289 return error_mark_node;
12291 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12292 if (TREE_CODE (type) == FUNCTION_TYPE)
12294 /* The type of the implicit object parameter gets its
12295 cv-qualifiers from the FUNCTION_TYPE. */
12296 tree memptr;
12297 tree method_type
12298 = build_memfn_type (type, r, type_memfn_quals (type),
12299 type_memfn_rqual (type));
12300 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12301 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12302 complain);
12304 else
12305 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12306 cp_type_quals (t),
12307 complain);
12309 case FUNCTION_TYPE:
12310 case METHOD_TYPE:
12312 tree fntype;
12313 tree specs;
12314 fntype = tsubst_function_type (t, args, complain, in_decl);
12315 if (fntype == error_mark_node)
12316 return error_mark_node;
12318 /* Substitute the exception specification. */
12319 specs = tsubst_exception_specification (t, args, complain,
12320 in_decl, /*defer_ok*/true);
12321 if (specs == error_mark_node)
12322 return error_mark_node;
12323 if (specs)
12324 fntype = build_exception_variant (fntype, specs);
12325 return fntype;
12327 case ARRAY_TYPE:
12329 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12330 if (domain == error_mark_node)
12331 return error_mark_node;
12333 /* As an optimization, we avoid regenerating the array type if
12334 it will obviously be the same as T. */
12335 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12336 return t;
12338 /* These checks should match the ones in create_array_type_for_decl.
12340 [temp.deduct]
12342 The deduction may fail for any of the following reasons:
12344 -- Attempting to create an array with an element type that
12345 is void, a function type, or a reference type, or [DR337]
12346 an abstract class type. */
12347 if (VOID_TYPE_P (type)
12348 || TREE_CODE (type) == FUNCTION_TYPE
12349 || (TREE_CODE (type) == ARRAY_TYPE
12350 && TYPE_DOMAIN (type) == NULL_TREE)
12351 || TREE_CODE (type) == REFERENCE_TYPE)
12353 if (complain & tf_error)
12354 error ("creating array of %qT", type);
12355 return error_mark_node;
12358 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12359 return error_mark_node;
12361 r = build_cplus_array_type (type, domain);
12363 if (TYPE_USER_ALIGN (t))
12365 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12366 TYPE_USER_ALIGN (r) = 1;
12369 return r;
12372 case TYPENAME_TYPE:
12374 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12375 in_decl, /*entering_scope=*/1);
12376 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12377 complain, in_decl);
12379 if (ctx == error_mark_node || f == error_mark_node)
12380 return error_mark_node;
12382 if (!MAYBE_CLASS_TYPE_P (ctx))
12384 if (complain & tf_error)
12385 error ("%qT is not a class, struct, or union type", ctx);
12386 return error_mark_node;
12388 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12390 /* Normally, make_typename_type does not require that the CTX
12391 have complete type in order to allow things like:
12393 template <class T> struct S { typename S<T>::X Y; };
12395 But, such constructs have already been resolved by this
12396 point, so here CTX really should have complete type, unless
12397 it's a partial instantiation. */
12398 ctx = complete_type (ctx);
12399 if (!COMPLETE_TYPE_P (ctx))
12401 if (complain & tf_error)
12402 cxx_incomplete_type_error (NULL_TREE, ctx);
12403 return error_mark_node;
12407 f = make_typename_type (ctx, f, typename_type,
12408 complain | tf_keep_type_decl);
12409 if (f == error_mark_node)
12410 return f;
12411 if (TREE_CODE (f) == TYPE_DECL)
12413 complain |= tf_ignore_bad_quals;
12414 f = TREE_TYPE (f);
12417 if (TREE_CODE (f) != TYPENAME_TYPE)
12419 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12421 if (complain & tf_error)
12422 error ("%qT resolves to %qT, which is not an enumeration type",
12423 t, f);
12424 else
12425 return error_mark_node;
12427 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12429 if (complain & tf_error)
12430 error ("%qT resolves to %qT, which is is not a class type",
12431 t, f);
12432 else
12433 return error_mark_node;
12437 return cp_build_qualified_type_real
12438 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12441 case UNBOUND_CLASS_TEMPLATE:
12443 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12444 in_decl, /*entering_scope=*/1);
12445 tree name = TYPE_IDENTIFIER (t);
12446 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12448 if (ctx == error_mark_node || name == error_mark_node)
12449 return error_mark_node;
12451 if (parm_list)
12452 parm_list = tsubst_template_parms (parm_list, args, complain);
12453 return make_unbound_class_template (ctx, name, parm_list, complain);
12456 case TYPEOF_TYPE:
12458 tree type;
12460 ++cp_unevaluated_operand;
12461 ++c_inhibit_evaluation_warnings;
12463 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12464 complain, in_decl,
12465 /*integral_constant_expression_p=*/false);
12467 --cp_unevaluated_operand;
12468 --c_inhibit_evaluation_warnings;
12470 type = finish_typeof (type);
12471 return cp_build_qualified_type_real (type,
12472 cp_type_quals (t)
12473 | cp_type_quals (type),
12474 complain);
12477 case DECLTYPE_TYPE:
12479 tree type;
12481 ++cp_unevaluated_operand;
12482 ++c_inhibit_evaluation_warnings;
12484 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12485 complain|tf_decltype, in_decl,
12486 /*function_p*/false,
12487 /*integral_constant_expression*/false);
12489 --cp_unevaluated_operand;
12490 --c_inhibit_evaluation_warnings;
12492 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12493 type = lambda_capture_field_type (type,
12494 DECLTYPE_FOR_INIT_CAPTURE (t));
12495 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12496 type = lambda_proxy_type (type);
12497 else
12499 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12500 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12501 && EXPR_P (type))
12502 /* In a template ~id could be either a complement expression
12503 or an unqualified-id naming a destructor; if instantiating
12504 it produces an expression, it's not an id-expression or
12505 member access. */
12506 id = false;
12507 type = finish_decltype_type (type, id, complain);
12509 return cp_build_qualified_type_real (type,
12510 cp_type_quals (t)
12511 | cp_type_quals (type),
12512 complain | tf_ignore_bad_quals);
12515 case UNDERLYING_TYPE:
12517 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12518 complain, in_decl);
12519 return finish_underlying_type (type);
12522 case TYPE_ARGUMENT_PACK:
12523 case NONTYPE_ARGUMENT_PACK:
12525 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12526 tree packed_out =
12527 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12528 args,
12529 complain,
12530 in_decl);
12531 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12533 /* For template nontype argument packs, also substitute into
12534 the type. */
12535 if (code == NONTYPE_ARGUMENT_PACK)
12536 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12538 return r;
12540 break;
12542 case VOID_CST:
12543 case INTEGER_CST:
12544 case REAL_CST:
12545 case STRING_CST:
12546 case PLUS_EXPR:
12547 case MINUS_EXPR:
12548 case NEGATE_EXPR:
12549 case NOP_EXPR:
12550 case INDIRECT_REF:
12551 case ADDR_EXPR:
12552 case CALL_EXPR:
12553 case ARRAY_REF:
12554 case SCOPE_REF:
12555 /* We should use one of the expression tsubsts for these codes. */
12556 gcc_unreachable ();
12558 default:
12559 sorry ("use of %qs in template", get_tree_code_name (code));
12560 return error_mark_node;
12564 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12565 type of the expression on the left-hand side of the "." or "->"
12566 operator. */
12568 static tree
12569 tsubst_baselink (tree baselink, tree object_type,
12570 tree args, tsubst_flags_t complain, tree in_decl)
12572 tree name;
12573 tree qualifying_scope;
12574 tree fns;
12575 tree optype;
12576 tree template_args = 0;
12577 bool template_id_p = false;
12578 bool qualified = BASELINK_QUALIFIED_P (baselink);
12580 /* A baselink indicates a function from a base class. Both the
12581 BASELINK_ACCESS_BINFO and the base class referenced may
12582 indicate bases of the template class, rather than the
12583 instantiated class. In addition, lookups that were not
12584 ambiguous before may be ambiguous now. Therefore, we perform
12585 the lookup again. */
12586 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12587 qualifying_scope = tsubst (qualifying_scope, args,
12588 complain, in_decl);
12589 fns = BASELINK_FUNCTIONS (baselink);
12590 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12591 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12593 template_id_p = true;
12594 template_args = TREE_OPERAND (fns, 1);
12595 fns = TREE_OPERAND (fns, 0);
12596 if (template_args)
12597 template_args = tsubst_template_args (template_args, args,
12598 complain, in_decl);
12600 name = DECL_NAME (get_first_fn (fns));
12601 if (IDENTIFIER_TYPENAME_P (name))
12602 name = mangle_conv_op_name_for_type (optype);
12603 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12604 if (!baselink)
12605 return error_mark_node;
12607 /* If lookup found a single function, mark it as used at this
12608 point. (If it lookup found multiple functions the one selected
12609 later by overload resolution will be marked as used at that
12610 point.) */
12611 if (BASELINK_P (baselink))
12612 fns = BASELINK_FUNCTIONS (baselink);
12613 if (!template_id_p && !really_overloaded_fn (fns))
12614 mark_used (OVL_CURRENT (fns));
12616 /* Add back the template arguments, if present. */
12617 if (BASELINK_P (baselink) && template_id_p)
12618 BASELINK_FUNCTIONS (baselink)
12619 = build_nt (TEMPLATE_ID_EXPR,
12620 BASELINK_FUNCTIONS (baselink),
12621 template_args);
12622 /* Update the conversion operator type. */
12623 BASELINK_OPTYPE (baselink) = optype;
12625 if (!object_type)
12626 object_type = current_class_type;
12628 if (qualified)
12629 baselink = adjust_result_of_qualified_name_lookup (baselink,
12630 qualifying_scope,
12631 object_type);
12632 return baselink;
12635 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12636 true if the qualified-id will be a postfix-expression in-and-of
12637 itself; false if more of the postfix-expression follows the
12638 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12639 of "&". */
12641 static tree
12642 tsubst_qualified_id (tree qualified_id, tree args,
12643 tsubst_flags_t complain, tree in_decl,
12644 bool done, bool address_p)
12646 tree expr;
12647 tree scope;
12648 tree name;
12649 bool is_template;
12650 tree template_args;
12651 location_t loc = UNKNOWN_LOCATION;
12653 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12655 /* Figure out what name to look up. */
12656 name = TREE_OPERAND (qualified_id, 1);
12657 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12659 is_template = true;
12660 loc = EXPR_LOCATION (name);
12661 template_args = TREE_OPERAND (name, 1);
12662 if (template_args)
12663 template_args = tsubst_template_args (template_args, args,
12664 complain, in_decl);
12665 name = TREE_OPERAND (name, 0);
12667 else
12669 is_template = false;
12670 template_args = NULL_TREE;
12673 /* Substitute into the qualifying scope. When there are no ARGS, we
12674 are just trying to simplify a non-dependent expression. In that
12675 case the qualifying scope may be dependent, and, in any case,
12676 substituting will not help. */
12677 scope = TREE_OPERAND (qualified_id, 0);
12678 if (args)
12680 scope = tsubst (scope, args, complain, in_decl);
12681 expr = tsubst_copy (name, args, complain, in_decl);
12683 else
12684 expr = name;
12686 if (dependent_scope_p (scope))
12688 if (is_template)
12689 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12690 return build_qualified_name (NULL_TREE, scope, expr,
12691 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12694 if (!BASELINK_P (name) && !DECL_P (expr))
12696 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12698 /* A BIT_NOT_EXPR is used to represent a destructor. */
12699 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12701 error ("qualifying type %qT does not match destructor name ~%qT",
12702 scope, TREE_OPERAND (expr, 0));
12703 expr = error_mark_node;
12705 else
12706 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12707 /*is_type_p=*/0, false);
12709 else
12710 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12711 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12712 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12714 if (complain & tf_error)
12716 error ("dependent-name %qE is parsed as a non-type, but "
12717 "instantiation yields a type", qualified_id);
12718 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12720 return error_mark_node;
12724 if (DECL_P (expr))
12726 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12727 scope);
12728 /* Remember that there was a reference to this entity. */
12729 mark_used (expr);
12732 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12734 if (complain & tf_error)
12735 qualified_name_lookup_error (scope,
12736 TREE_OPERAND (qualified_id, 1),
12737 expr, input_location);
12738 return error_mark_node;
12741 if (is_template)
12742 expr = lookup_template_function (expr, template_args);
12744 if (expr == error_mark_node && complain & tf_error)
12745 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12746 expr, input_location);
12747 else if (TYPE_P (scope))
12749 expr = (adjust_result_of_qualified_name_lookup
12750 (expr, scope, current_nonlambda_class_type ()));
12751 expr = (finish_qualified_id_expr
12752 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12753 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12754 /*template_arg_p=*/false, complain));
12757 /* Expressions do not generally have reference type. */
12758 if (TREE_CODE (expr) != SCOPE_REF
12759 /* However, if we're about to form a pointer-to-member, we just
12760 want the referenced member referenced. */
12761 && TREE_CODE (expr) != OFFSET_REF)
12762 expr = convert_from_reference (expr);
12764 return expr;
12767 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12768 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12769 for tsubst. */
12771 static tree
12772 tsubst_init (tree init, tree decl, tree args,
12773 tsubst_flags_t complain, tree in_decl)
12775 if (!init)
12776 return NULL_TREE;
12778 init = tsubst_expr (init, args, complain, in_decl, false);
12780 if (!init)
12782 /* If we had an initializer but it
12783 instantiated to nothing,
12784 value-initialize the object. This will
12785 only occur when the initializer was a
12786 pack expansion where the parameter packs
12787 used in that expansion were of length
12788 zero. */
12789 init = build_value_init (TREE_TYPE (decl),
12790 complain);
12791 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12792 init = get_target_expr_sfinae (init, complain);
12795 return init;
12798 /* Like tsubst, but deals with expressions. This function just replaces
12799 template parms; to finish processing the resultant expression, use
12800 tsubst_copy_and_build or tsubst_expr. */
12802 static tree
12803 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12805 enum tree_code code;
12806 tree r;
12808 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12809 return t;
12811 code = TREE_CODE (t);
12813 switch (code)
12815 case PARM_DECL:
12816 r = retrieve_local_specialization (t);
12818 if (r == NULL_TREE)
12820 /* We get here for a use of 'this' in an NSDMI. */
12821 if (DECL_NAME (t) == this_identifier
12822 && current_function_decl
12823 && DECL_CONSTRUCTOR_P (current_function_decl))
12824 return current_class_ptr;
12826 /* This can happen for a parameter name used later in a function
12827 declaration (such as in a late-specified return type). Just
12828 make a dummy decl, since it's only used for its type. */
12829 gcc_assert (cp_unevaluated_operand != 0);
12830 r = tsubst_decl (t, args, complain);
12831 /* Give it the template pattern as its context; its true context
12832 hasn't been instantiated yet and this is good enough for
12833 mangling. */
12834 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12837 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12838 r = ARGUMENT_PACK_SELECT_ARG (r);
12839 mark_used (r);
12840 return r;
12842 case CONST_DECL:
12844 tree enum_type;
12845 tree v;
12847 if (DECL_TEMPLATE_PARM_P (t))
12848 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12849 /* There is no need to substitute into namespace-scope
12850 enumerators. */
12851 if (DECL_NAMESPACE_SCOPE_P (t))
12852 return t;
12853 /* If ARGS is NULL, then T is known to be non-dependent. */
12854 if (args == NULL_TREE)
12855 return scalar_constant_value (t);
12857 /* Unfortunately, we cannot just call lookup_name here.
12858 Consider:
12860 template <int I> int f() {
12861 enum E { a = I };
12862 struct S { void g() { E e = a; } };
12865 When we instantiate f<7>::S::g(), say, lookup_name is not
12866 clever enough to find f<7>::a. */
12867 enum_type
12868 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12869 /*entering_scope=*/0);
12871 for (v = TYPE_VALUES (enum_type);
12872 v != NULL_TREE;
12873 v = TREE_CHAIN (v))
12874 if (TREE_PURPOSE (v) == DECL_NAME (t))
12875 return TREE_VALUE (v);
12877 /* We didn't find the name. That should never happen; if
12878 name-lookup found it during preliminary parsing, we
12879 should find it again here during instantiation. */
12880 gcc_unreachable ();
12882 return t;
12884 case FIELD_DECL:
12885 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12887 /* Check for a local specialization set up by
12888 tsubst_pack_expansion. */
12889 if (tree r = retrieve_local_specialization (t))
12891 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12892 r = ARGUMENT_PACK_SELECT_ARG (r);
12893 return r;
12896 /* When retrieving a capture pack from a generic lambda, remove the
12897 lambda call op's own template argument list from ARGS. Only the
12898 template arguments active for the closure type should be used to
12899 retrieve the pack specialization. */
12900 if (LAMBDA_FUNCTION_P (current_function_decl)
12901 && (template_class_depth (DECL_CONTEXT (t))
12902 != TMPL_ARGS_DEPTH (args)))
12903 args = strip_innermost_template_args (args, 1);
12905 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12906 tsubst_decl put in the hash table. */
12907 return retrieve_specialization (t, args, 0);
12910 if (DECL_CONTEXT (t))
12912 tree ctx;
12914 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12915 /*entering_scope=*/1);
12916 if (ctx != DECL_CONTEXT (t))
12918 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12919 if (!r)
12921 if (complain & tf_error)
12922 error ("using invalid field %qD", t);
12923 return error_mark_node;
12925 return r;
12929 return t;
12931 case VAR_DECL:
12932 case FUNCTION_DECL:
12933 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12934 r = tsubst (t, args, complain, in_decl);
12935 else if (local_variable_p (t))
12937 r = retrieve_local_specialization (t);
12938 if (r == NULL_TREE)
12940 /* First try name lookup to find the instantiation. */
12941 r = lookup_name (DECL_NAME (t));
12942 if (r)
12944 /* Make sure that the one we found is the one we want. */
12945 tree ctx = tsubst (DECL_CONTEXT (t), args,
12946 complain, in_decl);
12947 if (ctx != DECL_CONTEXT (r))
12948 r = NULL_TREE;
12951 if (r)
12952 /* OK */;
12953 else
12955 /* This can happen for a variable used in a
12956 late-specified return type of a local lambda, or for a
12957 local static or constant. Building a new VAR_DECL
12958 should be OK in all those cases. */
12959 r = tsubst_decl (t, args, complain);
12960 if (decl_maybe_constant_var_p (r))
12962 /* We can't call cp_finish_decl, so handle the
12963 initializer by hand. */
12964 tree init = tsubst_init (DECL_INITIAL (t), r, args,
12965 complain, in_decl);
12966 if (!processing_template_decl)
12967 init = maybe_constant_init (init);
12968 if (processing_template_decl
12969 ? potential_constant_expression (init)
12970 : reduced_constant_expression_p (init))
12971 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
12972 = TREE_CONSTANT (r) = true;
12973 DECL_INITIAL (r) = init;
12975 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
12976 || decl_constant_var_p (r)
12977 || errorcount || sorrycount);
12978 if (!processing_template_decl)
12980 if (TREE_STATIC (r))
12981 rest_of_decl_compilation (r, toplevel_bindings_p (),
12982 at_eof);
12983 else if (decl_constant_var_p (r))
12984 /* A use of a local constant decays to its value.
12985 FIXME update for core DR 696. */
12986 r = scalar_constant_value (r);
12989 /* Remember this for subsequent uses. */
12990 if (local_specializations)
12991 register_local_specialization (r, t);
12994 else
12995 r = t;
12996 mark_used (r);
12997 return r;
12999 case NAMESPACE_DECL:
13000 return t;
13002 case OVERLOAD:
13003 /* An OVERLOAD will always be a non-dependent overload set; an
13004 overload set from function scope will just be represented with an
13005 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13006 gcc_assert (!uses_template_parms (t));
13007 return t;
13009 case BASELINK:
13010 return tsubst_baselink (t, current_nonlambda_class_type (),
13011 args, complain, in_decl);
13013 case TEMPLATE_DECL:
13014 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13015 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13016 args, complain, in_decl);
13017 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13018 return tsubst (t, args, complain, in_decl);
13019 else if (DECL_CLASS_SCOPE_P (t)
13020 && uses_template_parms (DECL_CONTEXT (t)))
13022 /* Template template argument like the following example need
13023 special treatment:
13025 template <template <class> class TT> struct C {};
13026 template <class T> struct D {
13027 template <class U> struct E {};
13028 C<E> c; // #1
13030 D<int> d; // #2
13032 We are processing the template argument `E' in #1 for
13033 the template instantiation #2. Originally, `E' is a
13034 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13035 have to substitute this with one having context `D<int>'. */
13037 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13038 return lookup_field (context, DECL_NAME(t), 0, false);
13040 else
13041 /* Ordinary template template argument. */
13042 return t;
13044 case CAST_EXPR:
13045 case REINTERPRET_CAST_EXPR:
13046 case CONST_CAST_EXPR:
13047 case STATIC_CAST_EXPR:
13048 case DYNAMIC_CAST_EXPR:
13049 case IMPLICIT_CONV_EXPR:
13050 case CONVERT_EXPR:
13051 case NOP_EXPR:
13053 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13054 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13055 return build1 (code, type, op0);
13058 case SIZEOF_EXPR:
13059 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13062 tree expanded, op = TREE_OPERAND (t, 0);
13063 int len = 0;
13065 if (SIZEOF_EXPR_TYPE_P (t))
13066 op = TREE_TYPE (op);
13068 ++cp_unevaluated_operand;
13069 ++c_inhibit_evaluation_warnings;
13070 /* We only want to compute the number of arguments. */
13071 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13072 --cp_unevaluated_operand;
13073 --c_inhibit_evaluation_warnings;
13075 if (TREE_CODE (expanded) == TREE_VEC)
13076 len = TREE_VEC_LENGTH (expanded);
13078 if (expanded == error_mark_node)
13079 return error_mark_node;
13080 else if (PACK_EXPANSION_P (expanded)
13081 || (TREE_CODE (expanded) == TREE_VEC
13082 && len > 0
13083 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13085 if (TREE_CODE (expanded) == TREE_VEC)
13086 expanded = TREE_VEC_ELT (expanded, len - 1);
13088 if (TYPE_P (expanded))
13089 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13090 complain & tf_error);
13091 else
13092 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13093 complain & tf_error);
13095 else
13096 return build_int_cst (size_type_node, len);
13098 if (SIZEOF_EXPR_TYPE_P (t))
13100 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13101 args, complain, in_decl);
13102 r = build1 (NOP_EXPR, r, error_mark_node);
13103 r = build1 (SIZEOF_EXPR,
13104 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13105 SIZEOF_EXPR_TYPE_P (r) = 1;
13106 return r;
13108 /* Fall through */
13110 case INDIRECT_REF:
13111 case NEGATE_EXPR:
13112 case TRUTH_NOT_EXPR:
13113 case BIT_NOT_EXPR:
13114 case ADDR_EXPR:
13115 case UNARY_PLUS_EXPR: /* Unary + */
13116 case ALIGNOF_EXPR:
13117 case AT_ENCODE_EXPR:
13118 case ARROW_EXPR:
13119 case THROW_EXPR:
13120 case TYPEID_EXPR:
13121 case REALPART_EXPR:
13122 case IMAGPART_EXPR:
13123 case PAREN_EXPR:
13125 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13126 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13127 return build1 (code, type, op0);
13130 case COMPONENT_REF:
13132 tree object;
13133 tree name;
13135 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13136 name = TREE_OPERAND (t, 1);
13137 if (TREE_CODE (name) == BIT_NOT_EXPR)
13139 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13140 complain, in_decl);
13141 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13143 else if (TREE_CODE (name) == SCOPE_REF
13144 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
13146 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
13147 complain, in_decl);
13148 name = TREE_OPERAND (name, 1);
13149 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13150 complain, in_decl);
13151 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13152 name = build_qualified_name (/*type=*/NULL_TREE,
13153 base, name,
13154 /*template_p=*/false);
13156 else if (BASELINK_P (name))
13157 name = tsubst_baselink (name,
13158 non_reference (TREE_TYPE (object)),
13159 args, complain,
13160 in_decl);
13161 else
13162 name = tsubst_copy (name, args, complain, in_decl);
13163 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
13166 case PLUS_EXPR:
13167 case MINUS_EXPR:
13168 case MULT_EXPR:
13169 case TRUNC_DIV_EXPR:
13170 case CEIL_DIV_EXPR:
13171 case FLOOR_DIV_EXPR:
13172 case ROUND_DIV_EXPR:
13173 case EXACT_DIV_EXPR:
13174 case BIT_AND_EXPR:
13175 case BIT_IOR_EXPR:
13176 case BIT_XOR_EXPR:
13177 case TRUNC_MOD_EXPR:
13178 case FLOOR_MOD_EXPR:
13179 case TRUTH_ANDIF_EXPR:
13180 case TRUTH_ORIF_EXPR:
13181 case TRUTH_AND_EXPR:
13182 case TRUTH_OR_EXPR:
13183 case RSHIFT_EXPR:
13184 case LSHIFT_EXPR:
13185 case RROTATE_EXPR:
13186 case LROTATE_EXPR:
13187 case EQ_EXPR:
13188 case NE_EXPR:
13189 case MAX_EXPR:
13190 case MIN_EXPR:
13191 case LE_EXPR:
13192 case GE_EXPR:
13193 case LT_EXPR:
13194 case GT_EXPR:
13195 case COMPOUND_EXPR:
13196 case DOTSTAR_EXPR:
13197 case MEMBER_REF:
13198 case PREDECREMENT_EXPR:
13199 case PREINCREMENT_EXPR:
13200 case POSTDECREMENT_EXPR:
13201 case POSTINCREMENT_EXPR:
13203 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13204 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13205 return build_nt (code, op0, op1);
13208 case SCOPE_REF:
13210 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13211 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13212 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
13213 QUALIFIED_NAME_IS_TEMPLATE (t));
13216 case ARRAY_REF:
13218 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13219 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13220 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
13223 case CALL_EXPR:
13225 int n = VL_EXP_OPERAND_LENGTH (t);
13226 tree result = build_vl_exp (CALL_EXPR, n);
13227 int i;
13228 for (i = 0; i < n; i++)
13229 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13230 complain, in_decl);
13231 return result;
13234 case COND_EXPR:
13235 case MODOP_EXPR:
13236 case PSEUDO_DTOR_EXPR:
13237 case VEC_PERM_EXPR:
13239 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13240 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13241 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13242 r = build_nt (code, op0, op1, op2);
13243 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13244 return r;
13247 case NEW_EXPR:
13249 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13250 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13251 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13252 r = build_nt (code, op0, op1, op2);
13253 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13254 return r;
13257 case DELETE_EXPR:
13259 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13260 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13261 r = build_nt (code, op0, op1);
13262 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13263 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13264 return r;
13267 case TEMPLATE_ID_EXPR:
13269 /* Substituted template arguments */
13270 tree fn = TREE_OPERAND (t, 0);
13271 tree targs = TREE_OPERAND (t, 1);
13273 fn = tsubst_copy (fn, args, complain, in_decl);
13274 if (targs)
13275 targs = tsubst_template_args (targs, args, complain, in_decl);
13277 return lookup_template_function (fn, targs);
13280 case TREE_LIST:
13282 tree purpose, value, chain;
13284 if (t == void_list_node)
13285 return t;
13287 purpose = TREE_PURPOSE (t);
13288 if (purpose)
13289 purpose = tsubst_copy (purpose, args, complain, in_decl);
13290 value = TREE_VALUE (t);
13291 if (value)
13292 value = tsubst_copy (value, args, complain, in_decl);
13293 chain = TREE_CHAIN (t);
13294 if (chain && chain != void_type_node)
13295 chain = tsubst_copy (chain, args, complain, in_decl);
13296 if (purpose == TREE_PURPOSE (t)
13297 && value == TREE_VALUE (t)
13298 && chain == TREE_CHAIN (t))
13299 return t;
13300 return tree_cons (purpose, value, chain);
13303 case RECORD_TYPE:
13304 case UNION_TYPE:
13305 case ENUMERAL_TYPE:
13306 case INTEGER_TYPE:
13307 case TEMPLATE_TYPE_PARM:
13308 case TEMPLATE_TEMPLATE_PARM:
13309 case BOUND_TEMPLATE_TEMPLATE_PARM:
13310 case TEMPLATE_PARM_INDEX:
13311 case POINTER_TYPE:
13312 case REFERENCE_TYPE:
13313 case OFFSET_TYPE:
13314 case FUNCTION_TYPE:
13315 case METHOD_TYPE:
13316 case ARRAY_TYPE:
13317 case TYPENAME_TYPE:
13318 case UNBOUND_CLASS_TEMPLATE:
13319 case TYPEOF_TYPE:
13320 case DECLTYPE_TYPE:
13321 case TYPE_DECL:
13322 return tsubst (t, args, complain, in_decl);
13324 case USING_DECL:
13325 t = DECL_NAME (t);
13326 /* Fall through. */
13327 case IDENTIFIER_NODE:
13328 if (IDENTIFIER_TYPENAME_P (t))
13330 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13331 return mangle_conv_op_name_for_type (new_type);
13333 else
13334 return t;
13336 case CONSTRUCTOR:
13337 /* This is handled by tsubst_copy_and_build. */
13338 gcc_unreachable ();
13340 case VA_ARG_EXPR:
13342 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13343 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13344 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
13347 case CLEANUP_POINT_EXPR:
13348 /* We shouldn't have built any of these during initial template
13349 generation. Instead, they should be built during instantiation
13350 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13351 gcc_unreachable ();
13353 case OFFSET_REF:
13355 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13356 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13357 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13358 r = build2 (code, type, op0, op1);
13359 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13360 mark_used (TREE_OPERAND (r, 1));
13361 return r;
13364 case EXPR_PACK_EXPANSION:
13365 error ("invalid use of pack expansion expression");
13366 return error_mark_node;
13368 case NONTYPE_ARGUMENT_PACK:
13369 error ("use %<...%> to expand argument pack");
13370 return error_mark_node;
13372 case VOID_CST:
13373 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
13374 return t;
13376 case INTEGER_CST:
13377 case REAL_CST:
13378 case STRING_CST:
13379 case COMPLEX_CST:
13381 /* Instantiate any typedefs in the type. */
13382 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13383 r = fold_convert (type, t);
13384 gcc_assert (TREE_CODE (r) == code);
13385 return r;
13388 case PTRMEM_CST:
13389 /* These can sometimes show up in a partial instantiation, but never
13390 involve template parms. */
13391 gcc_assert (!uses_template_parms (t));
13392 return t;
13394 default:
13395 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13396 gcc_checking_assert (false);
13397 return t;
13401 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13403 static tree
13404 tsubst_omp_clauses (tree clauses, bool declare_simd,
13405 tree args, tsubst_flags_t complain, tree in_decl)
13407 tree new_clauses = NULL, nc, oc;
13409 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13411 nc = copy_node (oc);
13412 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13413 new_clauses = nc;
13415 switch (OMP_CLAUSE_CODE (nc))
13417 case OMP_CLAUSE_LASTPRIVATE:
13418 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13420 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13421 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13422 in_decl, /*integral_constant_expression_p=*/false);
13423 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13424 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13426 /* FALLTHRU */
13427 case OMP_CLAUSE_PRIVATE:
13428 case OMP_CLAUSE_SHARED:
13429 case OMP_CLAUSE_FIRSTPRIVATE:
13430 case OMP_CLAUSE_COPYIN:
13431 case OMP_CLAUSE_COPYPRIVATE:
13432 case OMP_CLAUSE_IF:
13433 case OMP_CLAUSE_NUM_THREADS:
13434 case OMP_CLAUSE_SCHEDULE:
13435 case OMP_CLAUSE_COLLAPSE:
13436 case OMP_CLAUSE_FINAL:
13437 case OMP_CLAUSE_DEPEND:
13438 case OMP_CLAUSE_FROM:
13439 case OMP_CLAUSE_TO:
13440 case OMP_CLAUSE_UNIFORM:
13441 case OMP_CLAUSE_MAP:
13442 case OMP_CLAUSE_DEVICE:
13443 case OMP_CLAUSE_DIST_SCHEDULE:
13444 case OMP_CLAUSE_NUM_TEAMS:
13445 case OMP_CLAUSE_THREAD_LIMIT:
13446 case OMP_CLAUSE_SAFELEN:
13447 case OMP_CLAUSE_SIMDLEN:
13448 OMP_CLAUSE_OPERAND (nc, 0)
13449 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13450 in_decl, /*integral_constant_expression_p=*/false);
13451 break;
13452 case OMP_CLAUSE_REDUCTION:
13453 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13455 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13456 if (TREE_CODE (placeholder) == SCOPE_REF)
13458 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13459 complain, in_decl);
13460 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13461 = build_qualified_name (NULL_TREE, scope,
13462 TREE_OPERAND (placeholder, 1),
13463 false);
13465 else
13466 gcc_assert (identifier_p (placeholder));
13468 OMP_CLAUSE_OPERAND (nc, 0)
13469 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13470 in_decl, /*integral_constant_expression_p=*/false);
13471 break;
13472 case OMP_CLAUSE_LINEAR:
13473 case OMP_CLAUSE_ALIGNED:
13474 OMP_CLAUSE_OPERAND (nc, 0)
13475 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13476 in_decl, /*integral_constant_expression_p=*/false);
13477 OMP_CLAUSE_OPERAND (nc, 1)
13478 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13479 in_decl, /*integral_constant_expression_p=*/false);
13480 break;
13482 case OMP_CLAUSE_NOWAIT:
13483 case OMP_CLAUSE_ORDERED:
13484 case OMP_CLAUSE_DEFAULT:
13485 case OMP_CLAUSE_UNTIED:
13486 case OMP_CLAUSE_MERGEABLE:
13487 case OMP_CLAUSE_INBRANCH:
13488 case OMP_CLAUSE_NOTINBRANCH:
13489 case OMP_CLAUSE_PROC_BIND:
13490 case OMP_CLAUSE_FOR:
13491 case OMP_CLAUSE_PARALLEL:
13492 case OMP_CLAUSE_SECTIONS:
13493 case OMP_CLAUSE_TASKGROUP:
13494 break;
13495 default:
13496 gcc_unreachable ();
13500 new_clauses = nreverse (new_clauses);
13501 if (!declare_simd)
13502 new_clauses = finish_omp_clauses (new_clauses);
13503 return new_clauses;
13506 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13508 static tree
13509 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13510 tree in_decl)
13512 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13514 tree purpose, value, chain;
13516 if (t == NULL)
13517 return t;
13519 if (TREE_CODE (t) != TREE_LIST)
13520 return tsubst_copy_and_build (t, args, complain, in_decl,
13521 /*function_p=*/false,
13522 /*integral_constant_expression_p=*/false);
13524 if (t == void_list_node)
13525 return t;
13527 purpose = TREE_PURPOSE (t);
13528 if (purpose)
13529 purpose = RECUR (purpose);
13530 value = TREE_VALUE (t);
13531 if (value)
13533 if (TREE_CODE (value) != LABEL_DECL)
13534 value = RECUR (value);
13535 else
13537 value = lookup_label (DECL_NAME (value));
13538 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13539 TREE_USED (value) = 1;
13542 chain = TREE_CHAIN (t);
13543 if (chain && chain != void_type_node)
13544 chain = RECUR (chain);
13545 return tree_cons (purpose, value, chain);
13546 #undef RECUR
13549 /* Substitute one OMP_FOR iterator. */
13551 static void
13552 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13553 tree condv, tree incrv, tree *clauses,
13554 tree args, tsubst_flags_t complain, tree in_decl,
13555 bool integral_constant_expression_p)
13557 #define RECUR(NODE) \
13558 tsubst_expr ((NODE), args, complain, in_decl, \
13559 integral_constant_expression_p)
13560 tree decl, init, cond, incr;
13562 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13563 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13564 decl = TREE_OPERAND (init, 0);
13565 init = TREE_OPERAND (init, 1);
13566 tree decl_expr = NULL_TREE;
13567 if (init && TREE_CODE (init) == DECL_EXPR)
13569 /* We need to jump through some hoops to handle declarations in the
13570 for-init-statement, since we might need to handle auto deduction,
13571 but we need to keep control of initialization. */
13572 decl_expr = init;
13573 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13574 decl = tsubst_decl (decl, args, complain);
13576 else
13577 decl = RECUR (decl);
13578 init = RECUR (init);
13580 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13581 if (auto_node && init)
13582 TREE_TYPE (decl)
13583 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13585 gcc_assert (!type_dependent_expression_p (decl));
13587 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13589 if (decl_expr)
13591 /* Declare the variable, but don't let that initialize it. */
13592 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13593 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13594 RECUR (decl_expr);
13595 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13598 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13599 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13600 if (TREE_CODE (incr) == MODIFY_EXPR)
13602 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13603 tree rhs = RECUR (TREE_OPERAND (incr, 1));
13604 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
13605 NOP_EXPR, rhs, complain);
13607 else
13608 incr = RECUR (incr);
13609 TREE_VEC_ELT (declv, i) = decl;
13610 TREE_VEC_ELT (initv, i) = init;
13611 TREE_VEC_ELT (condv, i) = cond;
13612 TREE_VEC_ELT (incrv, i) = incr;
13613 return;
13616 if (decl_expr)
13618 /* Declare and initialize the variable. */
13619 RECUR (decl_expr);
13620 init = NULL_TREE;
13622 else if (init)
13624 tree c;
13625 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13627 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13628 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13629 && OMP_CLAUSE_DECL (c) == decl)
13630 break;
13631 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13632 && OMP_CLAUSE_DECL (c) == decl)
13633 error ("iteration variable %qD should not be firstprivate", decl);
13634 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13635 && OMP_CLAUSE_DECL (c) == decl)
13636 error ("iteration variable %qD should not be reduction", decl);
13638 if (c == NULL)
13640 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13641 OMP_CLAUSE_DECL (c) = decl;
13642 c = finish_omp_clauses (c);
13643 if (c)
13645 OMP_CLAUSE_CHAIN (c) = *clauses;
13646 *clauses = c;
13650 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13651 if (COMPARISON_CLASS_P (cond))
13653 tree op0 = RECUR (TREE_OPERAND (cond, 0));
13654 tree op1 = RECUR (TREE_OPERAND (cond, 1));
13655 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
13657 else
13658 cond = RECUR (cond);
13659 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13660 switch (TREE_CODE (incr))
13662 case PREINCREMENT_EXPR:
13663 case PREDECREMENT_EXPR:
13664 case POSTINCREMENT_EXPR:
13665 case POSTDECREMENT_EXPR:
13666 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13667 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13668 break;
13669 case MODIFY_EXPR:
13670 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13671 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13673 tree rhs = TREE_OPERAND (incr, 1);
13674 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13675 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13676 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13677 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13678 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13679 rhs0, rhs1));
13681 else
13682 incr = RECUR (incr);
13683 break;
13684 case MODOP_EXPR:
13685 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13686 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13688 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13689 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13690 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13691 TREE_TYPE (decl), lhs,
13692 RECUR (TREE_OPERAND (incr, 2))));
13694 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13695 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13696 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13698 tree rhs = TREE_OPERAND (incr, 2);
13699 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13700 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13701 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13702 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13703 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13704 rhs0, rhs1));
13706 else
13707 incr = RECUR (incr);
13708 break;
13709 default:
13710 incr = RECUR (incr);
13711 break;
13714 TREE_VEC_ELT (declv, i) = decl;
13715 TREE_VEC_ELT (initv, i) = init;
13716 TREE_VEC_ELT (condv, i) = cond;
13717 TREE_VEC_ELT (incrv, i) = incr;
13718 #undef RECUR
13721 /* Like tsubst_copy for expressions, etc. but also does semantic
13722 processing. */
13724 static tree
13725 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13726 bool integral_constant_expression_p)
13728 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13729 #define RECUR(NODE) \
13730 tsubst_expr ((NODE), args, complain, in_decl, \
13731 integral_constant_expression_p)
13733 tree stmt, tmp;
13734 tree r;
13735 location_t loc;
13737 if (t == NULL_TREE || t == error_mark_node)
13738 return t;
13740 loc = input_location;
13741 if (EXPR_HAS_LOCATION (t))
13742 input_location = EXPR_LOCATION (t);
13743 if (STATEMENT_CODE_P (TREE_CODE (t)))
13744 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13746 switch (TREE_CODE (t))
13748 case STATEMENT_LIST:
13750 tree_stmt_iterator i;
13751 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13752 RECUR (tsi_stmt (i));
13753 break;
13756 case CTOR_INITIALIZER:
13757 finish_mem_initializers (tsubst_initializer_list
13758 (TREE_OPERAND (t, 0), args));
13759 break;
13761 case RETURN_EXPR:
13762 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13763 break;
13765 case EXPR_STMT:
13766 tmp = RECUR (EXPR_STMT_EXPR (t));
13767 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13768 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13769 else
13770 finish_expr_stmt (tmp);
13771 break;
13773 case USING_STMT:
13774 do_using_directive (USING_STMT_NAMESPACE (t));
13775 break;
13777 case DECL_EXPR:
13779 tree decl, pattern_decl;
13780 tree init;
13782 pattern_decl = decl = DECL_EXPR_DECL (t);
13783 if (TREE_CODE (decl) == LABEL_DECL)
13784 finish_label_decl (DECL_NAME (decl));
13785 else if (TREE_CODE (decl) == USING_DECL)
13787 tree scope = USING_DECL_SCOPE (decl);
13788 tree name = DECL_NAME (decl);
13789 tree decl;
13791 scope = tsubst (scope, args, complain, in_decl);
13792 decl = lookup_qualified_name (scope, name,
13793 /*is_type_p=*/false,
13794 /*complain=*/false);
13795 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13796 qualified_name_lookup_error (scope, name, decl, input_location);
13797 else
13798 do_local_using_decl (decl, scope, name);
13800 else if (DECL_PACK_P (decl))
13802 /* Don't build up decls for a variadic capture proxy, we'll
13803 instantiate the elements directly as needed. */
13804 break;
13806 else
13808 init = DECL_INITIAL (decl);
13809 decl = tsubst (decl, args, complain, in_decl);
13810 if (decl != error_mark_node)
13812 /* By marking the declaration as instantiated, we avoid
13813 trying to instantiate it. Since instantiate_decl can't
13814 handle local variables, and since we've already done
13815 all that needs to be done, that's the right thing to
13816 do. */
13817 if (VAR_P (decl))
13818 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13819 if (VAR_P (decl)
13820 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13821 /* Anonymous aggregates are a special case. */
13822 finish_anon_union (decl);
13823 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13825 DECL_CONTEXT (decl) = current_function_decl;
13826 if (DECL_NAME (decl) == this_identifier)
13828 tree lam = DECL_CONTEXT (current_function_decl);
13829 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13830 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13832 insert_capture_proxy (decl);
13834 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13835 /* We already did a pushtag. */;
13836 else if (TREE_CODE (decl) == FUNCTION_DECL
13837 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13838 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13840 DECL_CONTEXT (decl) = NULL_TREE;
13841 pushdecl (decl);
13842 DECL_CONTEXT (decl) = current_function_decl;
13843 cp_check_omp_declare_reduction (decl);
13845 else
13847 int const_init = false;
13848 maybe_push_decl (decl);
13849 if (VAR_P (decl)
13850 && DECL_PRETTY_FUNCTION_P (decl))
13852 /* For __PRETTY_FUNCTION__ we have to adjust the
13853 initializer. */
13854 const char *const name
13855 = cxx_printable_name (current_function_decl, 2);
13856 init = cp_fname_init (name, &TREE_TYPE (decl));
13858 else
13859 init = tsubst_init (init, decl, args, complain, in_decl);
13861 if (VAR_P (decl))
13862 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13863 (pattern_decl));
13864 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13869 break;
13872 case FOR_STMT:
13873 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13874 RECUR (FOR_INIT_STMT (t));
13875 finish_for_init_stmt (stmt);
13876 tmp = RECUR (FOR_COND (t));
13877 finish_for_cond (tmp, stmt, false);
13878 tmp = RECUR (FOR_EXPR (t));
13879 finish_for_expr (tmp, stmt);
13880 RECUR (FOR_BODY (t));
13881 finish_for_stmt (stmt);
13882 break;
13884 case RANGE_FOR_STMT:
13886 tree decl, expr;
13887 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13888 decl = RANGE_FOR_DECL (t);
13889 decl = tsubst (decl, args, complain, in_decl);
13890 maybe_push_decl (decl);
13891 expr = RECUR (RANGE_FOR_EXPR (t));
13892 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13893 RECUR (RANGE_FOR_BODY (t));
13894 finish_for_stmt (stmt);
13896 break;
13898 case WHILE_STMT:
13899 stmt = begin_while_stmt ();
13900 tmp = RECUR (WHILE_COND (t));
13901 finish_while_stmt_cond (tmp, stmt, false);
13902 RECUR (WHILE_BODY (t));
13903 finish_while_stmt (stmt);
13904 break;
13906 case DO_STMT:
13907 stmt = begin_do_stmt ();
13908 RECUR (DO_BODY (t));
13909 finish_do_body (stmt);
13910 tmp = RECUR (DO_COND (t));
13911 finish_do_stmt (tmp, stmt, false);
13912 break;
13914 case IF_STMT:
13915 stmt = begin_if_stmt ();
13916 tmp = RECUR (IF_COND (t));
13917 finish_if_stmt_cond (tmp, stmt);
13918 RECUR (THEN_CLAUSE (t));
13919 finish_then_clause (stmt);
13921 if (ELSE_CLAUSE (t))
13923 begin_else_clause (stmt);
13924 RECUR (ELSE_CLAUSE (t));
13925 finish_else_clause (stmt);
13928 finish_if_stmt (stmt);
13929 break;
13931 case BIND_EXPR:
13932 if (BIND_EXPR_BODY_BLOCK (t))
13933 stmt = begin_function_body ();
13934 else
13935 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13936 ? BCS_TRY_BLOCK : 0);
13938 RECUR (BIND_EXPR_BODY (t));
13940 if (BIND_EXPR_BODY_BLOCK (t))
13941 finish_function_body (stmt);
13942 else
13943 finish_compound_stmt (stmt);
13944 break;
13946 case BREAK_STMT:
13947 finish_break_stmt ();
13948 break;
13950 case CONTINUE_STMT:
13951 finish_continue_stmt ();
13952 break;
13954 case SWITCH_STMT:
13955 stmt = begin_switch_stmt ();
13956 tmp = RECUR (SWITCH_STMT_COND (t));
13957 finish_switch_cond (tmp, stmt);
13958 RECUR (SWITCH_STMT_BODY (t));
13959 finish_switch_stmt (stmt);
13960 break;
13962 case CASE_LABEL_EXPR:
13964 tree low = RECUR (CASE_LOW (t));
13965 tree high = RECUR (CASE_HIGH (t));
13966 finish_case_label (EXPR_LOCATION (t), low, high);
13968 break;
13970 case LABEL_EXPR:
13972 tree decl = LABEL_EXPR_LABEL (t);
13973 tree label;
13975 label = finish_label_stmt (DECL_NAME (decl));
13976 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13977 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13979 break;
13981 case GOTO_EXPR:
13982 tmp = GOTO_DESTINATION (t);
13983 if (TREE_CODE (tmp) != LABEL_DECL)
13984 /* Computed goto's must be tsubst'd into. On the other hand,
13985 non-computed gotos must not be; the identifier in question
13986 will have no binding. */
13987 tmp = RECUR (tmp);
13988 else
13989 tmp = DECL_NAME (tmp);
13990 finish_goto_stmt (tmp);
13991 break;
13993 case ASM_EXPR:
13995 tree string = RECUR (ASM_STRING (t));
13996 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
13997 complain, in_decl);
13998 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
13999 complain, in_decl);
14000 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
14001 complain, in_decl);
14002 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
14003 complain, in_decl);
14004 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
14005 clobbers, labels);
14006 tree asm_expr = tmp;
14007 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14008 asm_expr = TREE_OPERAND (asm_expr, 0);
14009 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14011 break;
14013 case TRY_BLOCK:
14014 if (CLEANUP_P (t))
14016 stmt = begin_try_block ();
14017 RECUR (TRY_STMTS (t));
14018 finish_cleanup_try_block (stmt);
14019 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14021 else
14023 tree compound_stmt = NULL_TREE;
14025 if (FN_TRY_BLOCK_P (t))
14026 stmt = begin_function_try_block (&compound_stmt);
14027 else
14028 stmt = begin_try_block ();
14030 RECUR (TRY_STMTS (t));
14032 if (FN_TRY_BLOCK_P (t))
14033 finish_function_try_block (stmt);
14034 else
14035 finish_try_block (stmt);
14037 RECUR (TRY_HANDLERS (t));
14038 if (FN_TRY_BLOCK_P (t))
14039 finish_function_handler_sequence (stmt, compound_stmt);
14040 else
14041 finish_handler_sequence (stmt);
14043 break;
14045 case HANDLER:
14047 tree decl = HANDLER_PARMS (t);
14049 if (decl)
14051 decl = tsubst (decl, args, complain, in_decl);
14052 /* Prevent instantiate_decl from trying to instantiate
14053 this variable. We've already done all that needs to be
14054 done. */
14055 if (decl != error_mark_node)
14056 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14058 stmt = begin_handler ();
14059 finish_handler_parms (decl, stmt);
14060 RECUR (HANDLER_BODY (t));
14061 finish_handler (stmt);
14063 break;
14065 case TAG_DEFN:
14066 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14067 if (CLASS_TYPE_P (tmp))
14069 /* Local classes are not independent templates; they are
14070 instantiated along with their containing function. And this
14071 way we don't have to deal with pushing out of one local class
14072 to instantiate a member of another local class. */
14073 tree fn;
14074 /* Closures are handled by the LAMBDA_EXPR. */
14075 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14076 complete_type (tmp);
14077 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14078 if (!DECL_ARTIFICIAL (fn))
14079 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14081 break;
14083 case STATIC_ASSERT:
14085 tree condition;
14087 ++c_inhibit_evaluation_warnings;
14088 condition =
14089 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14090 args,
14091 complain, in_decl,
14092 /*integral_constant_expression_p=*/true);
14093 --c_inhibit_evaluation_warnings;
14095 finish_static_assert (condition,
14096 STATIC_ASSERT_MESSAGE (t),
14097 STATIC_ASSERT_SOURCE_LOCATION (t),
14098 /*member_p=*/false);
14100 break;
14102 case OMP_PARALLEL:
14103 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
14104 args, complain, in_decl);
14105 stmt = begin_omp_parallel ();
14106 RECUR (OMP_PARALLEL_BODY (t));
14107 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
14108 = OMP_PARALLEL_COMBINED (t);
14109 break;
14111 case OMP_TASK:
14112 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
14113 args, complain, in_decl);
14114 stmt = begin_omp_task ();
14115 RECUR (OMP_TASK_BODY (t));
14116 finish_omp_task (tmp, stmt);
14117 break;
14119 case OMP_FOR:
14120 case OMP_SIMD:
14121 case CILK_SIMD:
14122 case CILK_FOR:
14123 case OMP_DISTRIBUTE:
14125 tree clauses, body, pre_body;
14126 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
14127 tree incrv = NULL_TREE;
14128 int i;
14130 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
14131 args, complain, in_decl);
14132 if (OMP_FOR_INIT (t) != NULL_TREE)
14134 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14135 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14136 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14137 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14140 stmt = begin_omp_structured_block ();
14142 pre_body = push_stmt_list ();
14143 RECUR (OMP_FOR_PRE_BODY (t));
14144 pre_body = pop_stmt_list (pre_body);
14146 if (OMP_FOR_INIT (t) != NULL_TREE)
14147 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
14148 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
14149 &clauses, args, complain, in_decl,
14150 integral_constant_expression_p);
14152 body = push_stmt_list ();
14153 RECUR (OMP_FOR_BODY (t));
14154 body = pop_stmt_list (body);
14156 if (OMP_FOR_INIT (t) != NULL_TREE)
14157 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
14158 condv, incrv, body, pre_body, clauses);
14159 else
14161 t = make_node (TREE_CODE (t));
14162 TREE_TYPE (t) = void_type_node;
14163 OMP_FOR_BODY (t) = body;
14164 OMP_FOR_PRE_BODY (t) = pre_body;
14165 OMP_FOR_CLAUSES (t) = clauses;
14166 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
14167 add_stmt (t);
14170 add_stmt (finish_omp_structured_block (stmt));
14172 break;
14174 case OMP_SECTIONS:
14175 case OMP_SINGLE:
14176 case OMP_TEAMS:
14177 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14178 args, complain, in_decl);
14179 stmt = push_stmt_list ();
14180 RECUR (OMP_BODY (t));
14181 stmt = pop_stmt_list (stmt);
14183 t = copy_node (t);
14184 OMP_BODY (t) = stmt;
14185 OMP_CLAUSES (t) = tmp;
14186 add_stmt (t);
14187 break;
14189 case OMP_TARGET_DATA:
14190 case OMP_TARGET:
14191 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14192 args, complain, in_decl);
14193 keep_next_level (true);
14194 stmt = begin_omp_structured_block ();
14196 RECUR (OMP_BODY (t));
14197 stmt = finish_omp_structured_block (stmt);
14199 t = copy_node (t);
14200 OMP_BODY (t) = stmt;
14201 OMP_CLAUSES (t) = tmp;
14202 add_stmt (t);
14203 break;
14205 case OMP_TARGET_UPDATE:
14206 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14207 args, complain, in_decl);
14208 t = copy_node (t);
14209 OMP_CLAUSES (t) = tmp;
14210 add_stmt (t);
14211 break;
14213 case OMP_SECTION:
14214 case OMP_CRITICAL:
14215 case OMP_MASTER:
14216 case OMP_TASKGROUP:
14217 case OMP_ORDERED:
14218 stmt = push_stmt_list ();
14219 RECUR (OMP_BODY (t));
14220 stmt = pop_stmt_list (stmt);
14222 t = copy_node (t);
14223 OMP_BODY (t) = stmt;
14224 add_stmt (t);
14225 break;
14227 case OMP_ATOMIC:
14228 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14229 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14231 tree op1 = TREE_OPERAND (t, 1);
14232 tree rhs1 = NULL_TREE;
14233 tree lhs, rhs;
14234 if (TREE_CODE (op1) == COMPOUND_EXPR)
14236 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14237 op1 = TREE_OPERAND (op1, 1);
14239 lhs = RECUR (TREE_OPERAND (op1, 0));
14240 rhs = RECUR (TREE_OPERAND (op1, 1));
14241 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14242 NULL_TREE, NULL_TREE, rhs1,
14243 OMP_ATOMIC_SEQ_CST (t));
14245 else
14247 tree op1 = TREE_OPERAND (t, 1);
14248 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14249 tree rhs1 = NULL_TREE;
14250 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14251 enum tree_code opcode = NOP_EXPR;
14252 if (code == OMP_ATOMIC_READ)
14254 v = RECUR (TREE_OPERAND (op1, 0));
14255 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14257 else if (code == OMP_ATOMIC_CAPTURE_OLD
14258 || code == OMP_ATOMIC_CAPTURE_NEW)
14260 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14261 v = RECUR (TREE_OPERAND (op1, 0));
14262 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14263 if (TREE_CODE (op11) == COMPOUND_EXPR)
14265 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14266 op11 = TREE_OPERAND (op11, 1);
14268 lhs = RECUR (TREE_OPERAND (op11, 0));
14269 rhs = RECUR (TREE_OPERAND (op11, 1));
14270 opcode = TREE_CODE (op11);
14271 if (opcode == MODIFY_EXPR)
14272 opcode = NOP_EXPR;
14274 else
14276 code = OMP_ATOMIC;
14277 lhs = RECUR (TREE_OPERAND (op1, 0));
14278 rhs = RECUR (TREE_OPERAND (op1, 1));
14280 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14281 OMP_ATOMIC_SEQ_CST (t));
14283 break;
14285 case TRANSACTION_EXPR:
14287 int flags = 0;
14288 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14289 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14291 if (TRANSACTION_EXPR_IS_STMT (t))
14293 tree body = TRANSACTION_EXPR_BODY (t);
14294 tree noex = NULL_TREE;
14295 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14297 noex = MUST_NOT_THROW_COND (body);
14298 if (noex == NULL_TREE)
14299 noex = boolean_true_node;
14300 body = TREE_OPERAND (body, 0);
14302 stmt = begin_transaction_stmt (input_location, NULL, flags);
14303 RECUR (body);
14304 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14306 else
14308 stmt = build_transaction_expr (EXPR_LOCATION (t),
14309 RECUR (TRANSACTION_EXPR_BODY (t)),
14310 flags, NULL_TREE);
14311 RETURN (stmt);
14314 break;
14316 case MUST_NOT_THROW_EXPR:
14318 tree op0 = RECUR (TREE_OPERAND (t, 0));
14319 tree cond = RECUR (MUST_NOT_THROW_COND (t));
14320 RETURN (build_must_not_throw_expr (op0, cond));
14323 case EXPR_PACK_EXPANSION:
14324 error ("invalid use of pack expansion expression");
14325 RETURN (error_mark_node);
14327 case NONTYPE_ARGUMENT_PACK:
14328 error ("use %<...%> to expand argument pack");
14329 RETURN (error_mark_node);
14331 case CILK_SPAWN_STMT:
14332 cfun->calls_cilk_spawn = 1;
14333 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14335 case CILK_SYNC_STMT:
14336 RETURN (build_cilk_sync ());
14338 case COMPOUND_EXPR:
14339 tmp = RECUR (TREE_OPERAND (t, 0));
14340 if (tmp == NULL_TREE)
14341 /* If the first operand was a statement, we're done with it. */
14342 RETURN (RECUR (TREE_OPERAND (t, 1)));
14343 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14344 RECUR (TREE_OPERAND (t, 1)),
14345 complain));
14347 case ANNOTATE_EXPR:
14348 tmp = RECUR (TREE_OPERAND (t, 0));
14349 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14350 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14352 default:
14353 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14355 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14356 /*function_p=*/false,
14357 integral_constant_expression_p));
14360 RETURN (NULL_TREE);
14361 out:
14362 input_location = loc;
14363 return r;
14364 #undef RECUR
14365 #undef RETURN
14368 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14369 function. For description of the body see comment above
14370 cp_parser_omp_declare_reduction_exprs. */
14372 static void
14373 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14375 if (t == NULL_TREE || t == error_mark_node)
14376 return;
14378 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14380 tree_stmt_iterator tsi;
14381 int i;
14382 tree stmts[7];
14383 memset (stmts, 0, sizeof stmts);
14384 for (i = 0, tsi = tsi_start (t);
14385 i < 7 && !tsi_end_p (tsi);
14386 i++, tsi_next (&tsi))
14387 stmts[i] = tsi_stmt (tsi);
14388 gcc_assert (tsi_end_p (tsi));
14390 if (i >= 3)
14392 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14393 && TREE_CODE (stmts[1]) == DECL_EXPR);
14394 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14395 args, complain, in_decl);
14396 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14397 args, complain, in_decl);
14398 DECL_CONTEXT (omp_out) = current_function_decl;
14399 DECL_CONTEXT (omp_in) = current_function_decl;
14400 keep_next_level (true);
14401 tree block = begin_omp_structured_block ();
14402 tsubst_expr (stmts[2], args, complain, in_decl, false);
14403 block = finish_omp_structured_block (block);
14404 block = maybe_cleanup_point_expr_void (block);
14405 add_decl_expr (omp_out);
14406 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14407 TREE_NO_WARNING (omp_out) = 1;
14408 add_decl_expr (omp_in);
14409 finish_expr_stmt (block);
14411 if (i >= 6)
14413 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14414 && TREE_CODE (stmts[4]) == DECL_EXPR);
14415 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14416 args, complain, in_decl);
14417 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14418 args, complain, in_decl);
14419 DECL_CONTEXT (omp_priv) = current_function_decl;
14420 DECL_CONTEXT (omp_orig) = current_function_decl;
14421 keep_next_level (true);
14422 tree block = begin_omp_structured_block ();
14423 tsubst_expr (stmts[5], args, complain, in_decl, false);
14424 block = finish_omp_structured_block (block);
14425 block = maybe_cleanup_point_expr_void (block);
14426 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14427 add_decl_expr (omp_priv);
14428 add_decl_expr (omp_orig);
14429 finish_expr_stmt (block);
14430 if (i == 7)
14431 add_decl_expr (omp_orig);
14435 /* T is a postfix-expression that is not being used in a function
14436 call. Return the substituted version of T. */
14438 static tree
14439 tsubst_non_call_postfix_expression (tree t, tree args,
14440 tsubst_flags_t complain,
14441 tree in_decl)
14443 if (TREE_CODE (t) == SCOPE_REF)
14444 t = tsubst_qualified_id (t, args, complain, in_decl,
14445 /*done=*/false, /*address_p=*/false);
14446 else
14447 t = tsubst_copy_and_build (t, args, complain, in_decl,
14448 /*function_p=*/false,
14449 /*integral_constant_expression_p=*/false);
14451 return t;
14454 /* Like tsubst but deals with expressions and performs semantic
14455 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14457 tree
14458 tsubst_copy_and_build (tree t,
14459 tree args,
14460 tsubst_flags_t complain,
14461 tree in_decl,
14462 bool function_p,
14463 bool integral_constant_expression_p)
14465 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14466 #define RECUR(NODE) \
14467 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14468 /*function_p=*/false, \
14469 integral_constant_expression_p)
14471 tree retval, op1;
14472 location_t loc;
14474 if (t == NULL_TREE || t == error_mark_node)
14475 return t;
14477 loc = input_location;
14478 if (EXPR_HAS_LOCATION (t))
14479 input_location = EXPR_LOCATION (t);
14481 /* N3276 decltype magic only applies to calls at the top level or on the
14482 right side of a comma. */
14483 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14484 complain &= ~tf_decltype;
14486 switch (TREE_CODE (t))
14488 case USING_DECL:
14489 t = DECL_NAME (t);
14490 /* Fall through. */
14491 case IDENTIFIER_NODE:
14493 tree decl;
14494 cp_id_kind idk;
14495 bool non_integral_constant_expression_p;
14496 const char *error_msg;
14498 if (IDENTIFIER_TYPENAME_P (t))
14500 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14501 t = mangle_conv_op_name_for_type (new_type);
14504 /* Look up the name. */
14505 decl = lookup_name (t);
14507 /* By convention, expressions use ERROR_MARK_NODE to indicate
14508 failure, not NULL_TREE. */
14509 if (decl == NULL_TREE)
14510 decl = error_mark_node;
14512 decl = finish_id_expression (t, decl, NULL_TREE,
14513 &idk,
14514 integral_constant_expression_p,
14515 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14516 &non_integral_constant_expression_p,
14517 /*template_p=*/false,
14518 /*done=*/true,
14519 /*address_p=*/false,
14520 /*template_arg_p=*/false,
14521 &error_msg,
14522 input_location);
14523 if (error_msg)
14524 error (error_msg);
14525 if (!function_p && identifier_p (decl))
14527 if (complain & tf_error)
14528 unqualified_name_lookup_error (decl);
14529 decl = error_mark_node;
14531 RETURN (decl);
14534 case TEMPLATE_ID_EXPR:
14536 tree object;
14537 tree templ = RECUR (TREE_OPERAND (t, 0));
14538 tree targs = TREE_OPERAND (t, 1);
14540 if (targs)
14541 targs = tsubst_template_args (targs, args, complain, in_decl);
14543 if (TREE_CODE (templ) == COMPONENT_REF)
14545 object = TREE_OPERAND (templ, 0);
14546 templ = TREE_OPERAND (templ, 1);
14548 else
14549 object = NULL_TREE;
14550 templ = lookup_template_function (templ, targs);
14552 if (object)
14553 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14554 object, templ, NULL_TREE));
14555 else
14556 RETURN (baselink_for_fns (templ));
14559 case INDIRECT_REF:
14561 tree r = RECUR (TREE_OPERAND (t, 0));
14563 if (REFERENCE_REF_P (t))
14565 /* A type conversion to reference type will be enclosed in
14566 such an indirect ref, but the substitution of the cast
14567 will have also added such an indirect ref. */
14568 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14569 r = convert_from_reference (r);
14571 else
14572 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14573 complain|decltype_flag);
14574 RETURN (r);
14577 case NOP_EXPR:
14579 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14580 tree op0 = RECUR (TREE_OPERAND (t, 0));
14581 RETURN (build_nop (type, op0));
14584 case IMPLICIT_CONV_EXPR:
14586 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14587 tree expr = RECUR (TREE_OPERAND (t, 0));
14588 int flags = LOOKUP_IMPLICIT;
14589 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14590 flags = LOOKUP_NORMAL;
14591 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14592 flags));
14595 case CONVERT_EXPR:
14597 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14598 tree op0 = RECUR (TREE_OPERAND (t, 0));
14599 RETURN (build1 (CONVERT_EXPR, type, op0));
14602 case CAST_EXPR:
14603 case REINTERPRET_CAST_EXPR:
14604 case CONST_CAST_EXPR:
14605 case DYNAMIC_CAST_EXPR:
14606 case STATIC_CAST_EXPR:
14608 tree type;
14609 tree op, r = NULL_TREE;
14611 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14612 if (integral_constant_expression_p
14613 && !cast_valid_in_integral_constant_expression_p (type))
14615 if (complain & tf_error)
14616 error ("a cast to a type other than an integral or "
14617 "enumeration type cannot appear in a constant-expression");
14618 RETURN (error_mark_node);
14621 op = RECUR (TREE_OPERAND (t, 0));
14623 warning_sentinel s(warn_useless_cast);
14624 switch (TREE_CODE (t))
14626 case CAST_EXPR:
14627 r = build_functional_cast (type, op, complain);
14628 break;
14629 case REINTERPRET_CAST_EXPR:
14630 r = build_reinterpret_cast (type, op, complain);
14631 break;
14632 case CONST_CAST_EXPR:
14633 r = build_const_cast (type, op, complain);
14634 break;
14635 case DYNAMIC_CAST_EXPR:
14636 r = build_dynamic_cast (type, op, complain);
14637 break;
14638 case STATIC_CAST_EXPR:
14639 r = build_static_cast (type, op, complain);
14640 break;
14641 default:
14642 gcc_unreachable ();
14645 RETURN (r);
14648 case POSTDECREMENT_EXPR:
14649 case POSTINCREMENT_EXPR:
14650 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14651 args, complain, in_decl);
14652 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14653 complain|decltype_flag));
14655 case PREDECREMENT_EXPR:
14656 case PREINCREMENT_EXPR:
14657 case NEGATE_EXPR:
14658 case BIT_NOT_EXPR:
14659 case ABS_EXPR:
14660 case TRUTH_NOT_EXPR:
14661 case UNARY_PLUS_EXPR: /* Unary + */
14662 case REALPART_EXPR:
14663 case IMAGPART_EXPR:
14664 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14665 RECUR (TREE_OPERAND (t, 0)),
14666 complain|decltype_flag));
14668 case FIX_TRUNC_EXPR:
14669 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14670 0, complain));
14672 case ADDR_EXPR:
14673 op1 = TREE_OPERAND (t, 0);
14674 if (TREE_CODE (op1) == LABEL_DECL)
14675 RETURN (finish_label_address_expr (DECL_NAME (op1),
14676 EXPR_LOCATION (op1)));
14677 if (TREE_CODE (op1) == SCOPE_REF)
14678 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14679 /*done=*/true, /*address_p=*/true);
14680 else
14681 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14682 in_decl);
14683 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14684 complain|decltype_flag));
14686 case PLUS_EXPR:
14687 case MINUS_EXPR:
14688 case MULT_EXPR:
14689 case TRUNC_DIV_EXPR:
14690 case CEIL_DIV_EXPR:
14691 case FLOOR_DIV_EXPR:
14692 case ROUND_DIV_EXPR:
14693 case EXACT_DIV_EXPR:
14694 case BIT_AND_EXPR:
14695 case BIT_IOR_EXPR:
14696 case BIT_XOR_EXPR:
14697 case TRUNC_MOD_EXPR:
14698 case FLOOR_MOD_EXPR:
14699 case TRUTH_ANDIF_EXPR:
14700 case TRUTH_ORIF_EXPR:
14701 case TRUTH_AND_EXPR:
14702 case TRUTH_OR_EXPR:
14703 case RSHIFT_EXPR:
14704 case LSHIFT_EXPR:
14705 case RROTATE_EXPR:
14706 case LROTATE_EXPR:
14707 case EQ_EXPR:
14708 case NE_EXPR:
14709 case MAX_EXPR:
14710 case MIN_EXPR:
14711 case LE_EXPR:
14712 case GE_EXPR:
14713 case LT_EXPR:
14714 case GT_EXPR:
14715 case MEMBER_REF:
14716 case DOTSTAR_EXPR:
14718 warning_sentinel s1(warn_type_limits);
14719 warning_sentinel s2(warn_div_by_zero);
14720 tree op0 = RECUR (TREE_OPERAND (t, 0));
14721 tree op1 = RECUR (TREE_OPERAND (t, 1));
14722 tree r = build_x_binary_op
14723 (input_location, TREE_CODE (t),
14724 op0,
14725 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14726 ? ERROR_MARK
14727 : TREE_CODE (TREE_OPERAND (t, 0))),
14728 op1,
14729 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14730 ? ERROR_MARK
14731 : TREE_CODE (TREE_OPERAND (t, 1))),
14732 /*overload=*/NULL,
14733 complain|decltype_flag);
14734 if (EXPR_P (r) && TREE_NO_WARNING (t))
14735 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14737 RETURN (r);
14740 case POINTER_PLUS_EXPR:
14742 tree op0 = RECUR (TREE_OPERAND (t, 0));
14743 tree op1 = RECUR (TREE_OPERAND (t, 1));
14744 return fold_build_pointer_plus (op0, op1);
14747 case SCOPE_REF:
14748 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14749 /*address_p=*/false));
14750 case ARRAY_REF:
14751 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14752 args, complain, in_decl);
14753 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14754 RECUR (TREE_OPERAND (t, 1)),
14755 complain|decltype_flag));
14757 case ARRAY_NOTATION_REF:
14759 tree start_index, length, stride;
14760 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14761 args, complain, in_decl);
14762 start_index = RECUR (ARRAY_NOTATION_START (t));
14763 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14764 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14765 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14766 length, stride, TREE_TYPE (op1)));
14768 case SIZEOF_EXPR:
14769 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14770 RETURN (tsubst_copy (t, args, complain, in_decl));
14771 /* Fall through */
14773 case ALIGNOF_EXPR:
14775 tree r;
14777 op1 = TREE_OPERAND (t, 0);
14778 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14779 op1 = TREE_TYPE (op1);
14780 if (!args)
14782 /* When there are no ARGS, we are trying to evaluate a
14783 non-dependent expression from the parser. Trying to do
14784 the substitutions may not work. */
14785 if (!TYPE_P (op1))
14786 op1 = TREE_TYPE (op1);
14788 else
14790 ++cp_unevaluated_operand;
14791 ++c_inhibit_evaluation_warnings;
14792 if (TYPE_P (op1))
14793 op1 = tsubst (op1, args, complain, in_decl);
14794 else
14795 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14796 /*function_p=*/false,
14797 /*integral_constant_expression_p=*/
14798 false);
14799 --cp_unevaluated_operand;
14800 --c_inhibit_evaluation_warnings;
14802 if (TYPE_P (op1))
14803 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14804 complain & tf_error);
14805 else
14806 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14807 complain & tf_error);
14808 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14810 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14812 if (!processing_template_decl && TYPE_P (op1))
14814 r = build_min (SIZEOF_EXPR, size_type_node,
14815 build1 (NOP_EXPR, op1, error_mark_node));
14816 SIZEOF_EXPR_TYPE_P (r) = 1;
14818 else
14819 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14820 TREE_SIDE_EFFECTS (r) = 0;
14821 TREE_READONLY (r) = 1;
14823 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14825 RETURN (r);
14828 case AT_ENCODE_EXPR:
14830 op1 = TREE_OPERAND (t, 0);
14831 ++cp_unevaluated_operand;
14832 ++c_inhibit_evaluation_warnings;
14833 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14834 /*function_p=*/false,
14835 /*integral_constant_expression_p=*/false);
14836 --cp_unevaluated_operand;
14837 --c_inhibit_evaluation_warnings;
14838 RETURN (objc_build_encode_expr (op1));
14841 case NOEXCEPT_EXPR:
14842 op1 = TREE_OPERAND (t, 0);
14843 ++cp_unevaluated_operand;
14844 ++c_inhibit_evaluation_warnings;
14845 ++cp_noexcept_operand;
14846 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14847 /*function_p=*/false,
14848 /*integral_constant_expression_p=*/false);
14849 --cp_unevaluated_operand;
14850 --c_inhibit_evaluation_warnings;
14851 --cp_noexcept_operand;
14852 RETURN (finish_noexcept_expr (op1, complain));
14854 case MODOP_EXPR:
14856 warning_sentinel s(warn_div_by_zero);
14857 tree lhs = RECUR (TREE_OPERAND (t, 0));
14858 tree rhs = RECUR (TREE_OPERAND (t, 2));
14859 tree r = build_x_modify_expr
14860 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
14861 complain|decltype_flag);
14862 /* TREE_NO_WARNING must be set if either the expression was
14863 parenthesized or it uses an operator such as >>= rather
14864 than plain assignment. In the former case, it was already
14865 set and must be copied. In the latter case,
14866 build_x_modify_expr sets it and it must not be reset
14867 here. */
14868 if (TREE_NO_WARNING (t))
14869 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14871 RETURN (r);
14874 case ARROW_EXPR:
14875 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14876 args, complain, in_decl);
14877 /* Remember that there was a reference to this entity. */
14878 if (DECL_P (op1))
14879 mark_used (op1);
14880 RETURN (build_x_arrow (input_location, op1, complain));
14882 case NEW_EXPR:
14884 tree placement = RECUR (TREE_OPERAND (t, 0));
14885 tree init = RECUR (TREE_OPERAND (t, 3));
14886 vec<tree, va_gc> *placement_vec;
14887 vec<tree, va_gc> *init_vec;
14888 tree ret;
14890 if (placement == NULL_TREE)
14891 placement_vec = NULL;
14892 else
14894 placement_vec = make_tree_vector ();
14895 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14896 vec_safe_push (placement_vec, TREE_VALUE (placement));
14899 /* If there was an initializer in the original tree, but it
14900 instantiated to an empty list, then we should pass a
14901 non-NULL empty vector to tell build_new that it was an
14902 empty initializer() rather than no initializer. This can
14903 only happen when the initializer is a pack expansion whose
14904 parameter packs are of length zero. */
14905 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14906 init_vec = NULL;
14907 else
14909 init_vec = make_tree_vector ();
14910 if (init == void_node)
14911 gcc_assert (init_vec != NULL);
14912 else
14914 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14915 vec_safe_push (init_vec, TREE_VALUE (init));
14919 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
14920 tree op2 = RECUR (TREE_OPERAND (t, 2));
14921 ret = build_new (&placement_vec, op1, op2, &init_vec,
14922 NEW_EXPR_USE_GLOBAL (t),
14923 complain);
14925 if (placement_vec != NULL)
14926 release_tree_vector (placement_vec);
14927 if (init_vec != NULL)
14928 release_tree_vector (init_vec);
14930 RETURN (ret);
14933 case DELETE_EXPR:
14935 tree op0 = RECUR (TREE_OPERAND (t, 0));
14936 tree op1 = RECUR (TREE_OPERAND (t, 1));
14937 RETURN (delete_sanity (op0, op1,
14938 DELETE_EXPR_USE_VEC (t),
14939 DELETE_EXPR_USE_GLOBAL (t),
14940 complain));
14943 case COMPOUND_EXPR:
14945 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14946 complain & ~tf_decltype, in_decl,
14947 /*function_p=*/false,
14948 integral_constant_expression_p);
14949 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14950 op0,
14951 RECUR (TREE_OPERAND (t, 1)),
14952 complain|decltype_flag));
14955 case CALL_EXPR:
14957 tree function;
14958 vec<tree, va_gc> *call_args;
14959 unsigned int nargs, i;
14960 bool qualified_p;
14961 bool koenig_p;
14962 tree ret;
14964 function = CALL_EXPR_FN (t);
14965 /* When we parsed the expression, we determined whether or
14966 not Koenig lookup should be performed. */
14967 koenig_p = KOENIG_LOOKUP_P (t);
14968 if (TREE_CODE (function) == SCOPE_REF)
14970 qualified_p = true;
14971 function = tsubst_qualified_id (function, args, complain, in_decl,
14972 /*done=*/false,
14973 /*address_p=*/false);
14975 else if (koenig_p && identifier_p (function))
14977 /* Do nothing; calling tsubst_copy_and_build on an identifier
14978 would incorrectly perform unqualified lookup again.
14980 Note that we can also have an IDENTIFIER_NODE if the earlier
14981 unqualified lookup found a member function; in that case
14982 koenig_p will be false and we do want to do the lookup
14983 again to find the instantiated member function.
14985 FIXME but doing that causes c++/15272, so we need to stop
14986 using IDENTIFIER_NODE in that situation. */
14987 qualified_p = false;
14989 else
14991 if (TREE_CODE (function) == COMPONENT_REF)
14993 tree op = TREE_OPERAND (function, 1);
14995 qualified_p = (TREE_CODE (op) == SCOPE_REF
14996 || (BASELINK_P (op)
14997 && BASELINK_QUALIFIED_P (op)));
14999 else
15000 qualified_p = false;
15002 if (TREE_CODE (function) == ADDR_EXPR
15003 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
15004 /* Avoid error about taking the address of a constructor. */
15005 function = TREE_OPERAND (function, 0);
15007 function = tsubst_copy_and_build (function, args, complain,
15008 in_decl,
15009 !qualified_p,
15010 integral_constant_expression_p);
15012 if (BASELINK_P (function))
15013 qualified_p = true;
15016 nargs = call_expr_nargs (t);
15017 call_args = make_tree_vector ();
15018 for (i = 0; i < nargs; ++i)
15020 tree arg = CALL_EXPR_ARG (t, i);
15022 if (!PACK_EXPANSION_P (arg))
15023 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15024 else
15026 /* Expand the pack expansion and push each entry onto
15027 CALL_ARGS. */
15028 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15029 if (TREE_CODE (arg) == TREE_VEC)
15031 unsigned int len, j;
15033 len = TREE_VEC_LENGTH (arg);
15034 for (j = 0; j < len; ++j)
15036 tree value = TREE_VEC_ELT (arg, j);
15037 if (value != NULL_TREE)
15038 value = convert_from_reference (value);
15039 vec_safe_push (call_args, value);
15042 else
15044 /* A partial substitution. Add one entry. */
15045 vec_safe_push (call_args, arg);
15050 /* We do not perform argument-dependent lookup if normal
15051 lookup finds a non-function, in accordance with the
15052 expected resolution of DR 218. */
15053 if (koenig_p
15054 && ((is_overloaded_fn (function)
15055 /* If lookup found a member function, the Koenig lookup is
15056 not appropriate, even if an unqualified-name was used
15057 to denote the function. */
15058 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15059 || identifier_p (function))
15060 /* Only do this when substitution turns a dependent call
15061 into a non-dependent call. */
15062 && type_dependent_expression_p_push (t)
15063 && !any_type_dependent_arguments_p (call_args))
15064 function = perform_koenig_lookup (function, call_args, tf_none);
15066 if (identifier_p (function)
15067 && !any_type_dependent_arguments_p (call_args))
15069 if (koenig_p && (complain & tf_warning_or_error))
15071 /* For backwards compatibility and good diagnostics, try
15072 the unqualified lookup again if we aren't in SFINAE
15073 context. */
15074 tree unq = (tsubst_copy_and_build
15075 (function, args, complain, in_decl, true,
15076 integral_constant_expression_p));
15077 if (unq == error_mark_node)
15078 RETURN (error_mark_node);
15080 if (unq != function)
15082 tree fn = unq;
15083 if (INDIRECT_REF_P (fn))
15084 fn = TREE_OPERAND (fn, 0);
15085 if (TREE_CODE (fn) == COMPONENT_REF)
15086 fn = TREE_OPERAND (fn, 1);
15087 if (is_overloaded_fn (fn))
15088 fn = get_first_fn (fn);
15089 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
15090 "%qD was not declared in this scope, "
15091 "and no declarations were found by "
15092 "argument-dependent lookup at the point "
15093 "of instantiation", function))
15095 if (!DECL_P (fn))
15096 /* Can't say anything more. */;
15097 else if (DECL_CLASS_SCOPE_P (fn))
15099 location_t loc = EXPR_LOC_OR_LOC (t,
15100 input_location);
15101 inform (loc,
15102 "declarations in dependent base %qT are "
15103 "not found by unqualified lookup",
15104 DECL_CLASS_CONTEXT (fn));
15105 if (current_class_ptr)
15106 inform (loc,
15107 "use %<this->%D%> instead", function);
15108 else
15109 inform (loc,
15110 "use %<%T::%D%> instead",
15111 current_class_name, function);
15113 else
15114 inform (0, "%q+D declared here, later in the "
15115 "translation unit", fn);
15117 function = unq;
15120 if (identifier_p (function))
15122 if (complain & tf_error)
15123 unqualified_name_lookup_error (function);
15124 release_tree_vector (call_args);
15125 RETURN (error_mark_node);
15129 /* Remember that there was a reference to this entity. */
15130 if (DECL_P (function))
15131 mark_used (function, complain);
15133 /* Put back tf_decltype for the actual call. */
15134 complain |= decltype_flag;
15136 if (TREE_CODE (function) == OFFSET_REF)
15137 ret = build_offset_ref_call_from_tree (function, &call_args,
15138 complain);
15139 else if (TREE_CODE (function) == COMPONENT_REF)
15141 tree instance = TREE_OPERAND (function, 0);
15142 tree fn = TREE_OPERAND (function, 1);
15144 if (processing_template_decl
15145 && (type_dependent_expression_p (instance)
15146 || (!BASELINK_P (fn)
15147 && TREE_CODE (fn) != FIELD_DECL)
15148 || type_dependent_expression_p (fn)
15149 || any_type_dependent_arguments_p (call_args)))
15150 ret = build_nt_call_vec (function, call_args);
15151 else if (!BASELINK_P (fn))
15152 ret = finish_call_expr (function, &call_args,
15153 /*disallow_virtual=*/false,
15154 /*koenig_p=*/false,
15155 complain);
15156 else
15157 ret = (build_new_method_call
15158 (instance, fn,
15159 &call_args, NULL_TREE,
15160 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
15161 /*fn_p=*/NULL,
15162 complain));
15164 else
15165 ret = finish_call_expr (function, &call_args,
15166 /*disallow_virtual=*/qualified_p,
15167 koenig_p,
15168 complain);
15170 release_tree_vector (call_args);
15172 RETURN (ret);
15175 case COND_EXPR:
15177 tree cond = RECUR (TREE_OPERAND (t, 0));
15178 tree folded_cond = fold_non_dependent_expr (cond);
15179 tree exp1, exp2;
15181 if (TREE_CODE (folded_cond) == INTEGER_CST)
15183 if (integer_zerop (folded_cond))
15185 ++c_inhibit_evaluation_warnings;
15186 exp1 = RECUR (TREE_OPERAND (t, 1));
15187 --c_inhibit_evaluation_warnings;
15188 exp2 = RECUR (TREE_OPERAND (t, 2));
15190 else
15192 exp1 = RECUR (TREE_OPERAND (t, 1));
15193 ++c_inhibit_evaluation_warnings;
15194 exp2 = RECUR (TREE_OPERAND (t, 2));
15195 --c_inhibit_evaluation_warnings;
15197 cond = folded_cond;
15199 else
15201 exp1 = RECUR (TREE_OPERAND (t, 1));
15202 exp2 = RECUR (TREE_OPERAND (t, 2));
15205 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
15206 cond, exp1, exp2, complain));
15209 case PSEUDO_DTOR_EXPR:
15211 tree op0 = RECUR (TREE_OPERAND (t, 0));
15212 tree op1 = RECUR (TREE_OPERAND (t, 1));
15213 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
15214 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
15215 input_location));
15218 case TREE_LIST:
15220 tree purpose, value, chain;
15222 if (t == void_list_node)
15223 RETURN (t);
15225 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15226 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15228 /* We have pack expansions, so expand those and
15229 create a new list out of it. */
15230 tree purposevec = NULL_TREE;
15231 tree valuevec = NULL_TREE;
15232 tree chain;
15233 int i, len = -1;
15235 /* Expand the argument expressions. */
15236 if (TREE_PURPOSE (t))
15237 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15238 complain, in_decl);
15239 if (TREE_VALUE (t))
15240 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15241 complain, in_decl);
15243 /* Build the rest of the list. */
15244 chain = TREE_CHAIN (t);
15245 if (chain && chain != void_type_node)
15246 chain = RECUR (chain);
15248 /* Determine the number of arguments. */
15249 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15251 len = TREE_VEC_LENGTH (purposevec);
15252 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15254 else if (TREE_CODE (valuevec) == TREE_VEC)
15255 len = TREE_VEC_LENGTH (valuevec);
15256 else
15258 /* Since we only performed a partial substitution into
15259 the argument pack, we only RETURN (a single list
15260 node. */
15261 if (purposevec == TREE_PURPOSE (t)
15262 && valuevec == TREE_VALUE (t)
15263 && chain == TREE_CHAIN (t))
15264 RETURN (t);
15266 RETURN (tree_cons (purposevec, valuevec, chain));
15269 /* Convert the argument vectors into a TREE_LIST */
15270 i = len;
15271 while (i > 0)
15273 /* Grab the Ith values. */
15274 i--;
15275 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15276 : NULL_TREE;
15277 value
15278 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15279 : NULL_TREE;
15281 /* Build the list (backwards). */
15282 chain = tree_cons (purpose, value, chain);
15285 RETURN (chain);
15288 purpose = TREE_PURPOSE (t);
15289 if (purpose)
15290 purpose = RECUR (purpose);
15291 value = TREE_VALUE (t);
15292 if (value)
15293 value = RECUR (value);
15294 chain = TREE_CHAIN (t);
15295 if (chain && chain != void_type_node)
15296 chain = RECUR (chain);
15297 if (purpose == TREE_PURPOSE (t)
15298 && value == TREE_VALUE (t)
15299 && chain == TREE_CHAIN (t))
15300 RETURN (t);
15301 RETURN (tree_cons (purpose, value, chain));
15304 case COMPONENT_REF:
15306 tree object;
15307 tree object_type;
15308 tree member;
15309 tree r;
15311 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15312 args, complain, in_decl);
15313 /* Remember that there was a reference to this entity. */
15314 if (DECL_P (object))
15315 mark_used (object);
15316 object_type = TREE_TYPE (object);
15318 member = TREE_OPERAND (t, 1);
15319 if (BASELINK_P (member))
15320 member = tsubst_baselink (member,
15321 non_reference (TREE_TYPE (object)),
15322 args, complain, in_decl);
15323 else
15324 member = tsubst_copy (member, args, complain, in_decl);
15325 if (member == error_mark_node)
15326 RETURN (error_mark_node);
15328 if (type_dependent_expression_p (object))
15329 /* We can't do much here. */;
15330 else if (!CLASS_TYPE_P (object_type))
15332 if (scalarish_type_p (object_type))
15334 tree s = NULL_TREE;
15335 tree dtor = member;
15337 if (TREE_CODE (dtor) == SCOPE_REF)
15339 s = TREE_OPERAND (dtor, 0);
15340 dtor = TREE_OPERAND (dtor, 1);
15342 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15344 dtor = TREE_OPERAND (dtor, 0);
15345 if (TYPE_P (dtor))
15346 RETURN (finish_pseudo_destructor_expr
15347 (object, s, dtor, input_location));
15351 else if (TREE_CODE (member) == SCOPE_REF
15352 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15354 /* Lookup the template functions now that we know what the
15355 scope is. */
15356 tree scope = TREE_OPERAND (member, 0);
15357 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15358 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15359 member = lookup_qualified_name (scope, tmpl,
15360 /*is_type_p=*/false,
15361 /*complain=*/false);
15362 if (BASELINK_P (member))
15364 BASELINK_FUNCTIONS (member)
15365 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15366 args);
15367 member = (adjust_result_of_qualified_name_lookup
15368 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15369 object_type));
15371 else
15373 qualified_name_lookup_error (scope, tmpl, member,
15374 input_location);
15375 RETURN (error_mark_node);
15378 else if (TREE_CODE (member) == SCOPE_REF
15379 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15380 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15382 if (complain & tf_error)
15384 if (TYPE_P (TREE_OPERAND (member, 0)))
15385 error ("%qT is not a class or namespace",
15386 TREE_OPERAND (member, 0));
15387 else
15388 error ("%qD is not a class or namespace",
15389 TREE_OPERAND (member, 0));
15391 RETURN (error_mark_node);
15393 else if (TREE_CODE (member) == FIELD_DECL)
15395 r = finish_non_static_data_member (member, object, NULL_TREE);
15396 if (TREE_CODE (r) == COMPONENT_REF)
15397 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15398 RETURN (r);
15401 r = finish_class_member_access_expr (object, member,
15402 /*template_p=*/false,
15403 complain);
15404 if (TREE_CODE (r) == COMPONENT_REF)
15405 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15406 RETURN (r);
15409 case THROW_EXPR:
15410 RETURN (build_throw
15411 (RECUR (TREE_OPERAND (t, 0))));
15413 case CONSTRUCTOR:
15415 vec<constructor_elt, va_gc> *n;
15416 constructor_elt *ce;
15417 unsigned HOST_WIDE_INT idx;
15418 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15419 bool process_index_p;
15420 int newlen;
15421 bool need_copy_p = false;
15422 tree r;
15424 if (type == error_mark_node)
15425 RETURN (error_mark_node);
15427 /* digest_init will do the wrong thing if we let it. */
15428 if (type && TYPE_PTRMEMFUNC_P (type))
15429 RETURN (t);
15431 /* We do not want to process the index of aggregate
15432 initializers as they are identifier nodes which will be
15433 looked up by digest_init. */
15434 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15436 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15437 newlen = vec_safe_length (n);
15438 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15440 if (ce->index && process_index_p
15441 /* An identifier index is looked up in the type
15442 being initialized, not the current scope. */
15443 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15444 ce->index = RECUR (ce->index);
15446 if (PACK_EXPANSION_P (ce->value))
15448 /* Substitute into the pack expansion. */
15449 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15450 in_decl);
15452 if (ce->value == error_mark_node
15453 || PACK_EXPANSION_P (ce->value))
15455 else if (TREE_VEC_LENGTH (ce->value) == 1)
15456 /* Just move the argument into place. */
15457 ce->value = TREE_VEC_ELT (ce->value, 0);
15458 else
15460 /* Update the length of the final CONSTRUCTOR
15461 arguments vector, and note that we will need to
15462 copy.*/
15463 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15464 need_copy_p = true;
15467 else
15468 ce->value = RECUR (ce->value);
15471 if (need_copy_p)
15473 vec<constructor_elt, va_gc> *old_n = n;
15475 vec_alloc (n, newlen);
15476 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15478 if (TREE_CODE (ce->value) == TREE_VEC)
15480 int i, len = TREE_VEC_LENGTH (ce->value);
15481 for (i = 0; i < len; ++i)
15482 CONSTRUCTOR_APPEND_ELT (n, 0,
15483 TREE_VEC_ELT (ce->value, i));
15485 else
15486 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15490 r = build_constructor (init_list_type_node, n);
15491 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15493 if (TREE_HAS_CONSTRUCTOR (t))
15494 RETURN (finish_compound_literal (type, r, complain));
15496 TREE_TYPE (r) = type;
15497 RETURN (r);
15500 case TYPEID_EXPR:
15502 tree operand_0 = TREE_OPERAND (t, 0);
15503 if (TYPE_P (operand_0))
15505 operand_0 = tsubst (operand_0, args, complain, in_decl);
15506 RETURN (get_typeid (operand_0, complain));
15508 else
15510 operand_0 = RECUR (operand_0);
15511 RETURN (build_typeid (operand_0, complain));
15515 case VAR_DECL:
15516 if (!args)
15517 RETURN (t);
15518 else if (DECL_PACK_P (t))
15520 /* We don't build decls for an instantiation of a
15521 variadic capture proxy, we instantiate the elements
15522 when needed. */
15523 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15524 return RECUR (DECL_VALUE_EXPR (t));
15526 /* Fall through */
15528 case PARM_DECL:
15530 tree r = tsubst_copy (t, args, complain, in_decl);
15531 /* ??? We're doing a subset of finish_id_expression here. */
15532 if (VAR_P (r)
15533 && !processing_template_decl
15534 && !cp_unevaluated_operand
15535 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
15536 && DECL_THREAD_LOCAL_P (r))
15538 if (tree wrap = get_tls_wrapper_fn (r))
15539 /* Replace an evaluated use of the thread_local variable with
15540 a call to its wrapper. */
15541 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
15543 else if (outer_automatic_var_p (r))
15544 r = process_outer_var_ref (r, complain);
15546 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15547 /* If the original type was a reference, we'll be wrapped in
15548 the appropriate INDIRECT_REF. */
15549 r = convert_from_reference (r);
15550 RETURN (r);
15553 case VA_ARG_EXPR:
15555 tree op0 = RECUR (TREE_OPERAND (t, 0));
15556 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15557 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
15560 case OFFSETOF_EXPR:
15561 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
15562 EXPR_LOCATION (t)));
15564 case TRAIT_EXPR:
15566 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15567 complain, in_decl);
15569 tree type2 = TRAIT_EXPR_TYPE2 (t);
15570 if (type2 && TREE_CODE (type2) == TREE_LIST)
15571 type2 = RECUR (type2);
15572 else if (type2)
15573 type2 = tsubst (type2, args, complain, in_decl);
15575 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15578 case STMT_EXPR:
15580 tree old_stmt_expr = cur_stmt_expr;
15581 tree stmt_expr = begin_stmt_expr ();
15583 cur_stmt_expr = stmt_expr;
15584 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15585 integral_constant_expression_p);
15586 stmt_expr = finish_stmt_expr (stmt_expr, false);
15587 cur_stmt_expr = old_stmt_expr;
15589 /* If the resulting list of expression statement is empty,
15590 fold it further into void_node. */
15591 if (empty_expr_stmt_p (stmt_expr))
15592 stmt_expr = void_node;
15594 RETURN (stmt_expr);
15597 case LAMBDA_EXPR:
15599 tree r = build_lambda_expr ();
15601 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15602 LAMBDA_EXPR_CLOSURE (r) = type;
15603 CLASSTYPE_LAMBDA_EXPR (type) = r;
15605 LAMBDA_EXPR_LOCATION (r)
15606 = LAMBDA_EXPR_LOCATION (t);
15607 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15608 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15609 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15610 LAMBDA_EXPR_DISCRIMINATOR (r)
15611 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15612 /* For a function scope, we want to use tsubst so that we don't
15613 complain about referring to an auto function before its return
15614 type has been deduced. Otherwise, we want to use tsubst_copy so
15615 that we look up the existing field/parameter/variable rather
15616 than build a new one. */
15617 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15618 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15619 scope = tsubst (scope, args, complain, in_decl);
15620 else if (scope && TREE_CODE (scope) == PARM_DECL)
15622 /* Look up the parameter we want directly, as tsubst_copy
15623 doesn't do what we need. */
15624 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15625 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15626 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15627 parm = DECL_CHAIN (parm);
15628 scope = parm;
15629 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15630 if (DECL_CONTEXT (scope) == NULL_TREE)
15631 DECL_CONTEXT (scope) = fn;
15633 else
15634 scope = RECUR (scope);
15635 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15636 LAMBDA_EXPR_RETURN_TYPE (r)
15637 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15639 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15640 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15642 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15643 determine_visibility (TYPE_NAME (type));
15644 /* Now that we know visibility, instantiate the type so we have a
15645 declaration of the op() for later calls to lambda_function. */
15646 complete_type (type);
15648 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15650 RETURN (build_lambda_object (r));
15653 case TARGET_EXPR:
15654 /* We can get here for a constant initializer of non-dependent type.
15655 FIXME stop folding in cp_parser_initializer_clause. */
15657 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15658 complain);
15659 RETURN (r);
15662 case TRANSACTION_EXPR:
15663 RETURN (tsubst_expr(t, args, complain, in_decl,
15664 integral_constant_expression_p));
15666 case PAREN_EXPR:
15667 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15669 case VEC_PERM_EXPR:
15671 tree op0 = RECUR (TREE_OPERAND (t, 0));
15672 tree op1 = RECUR (TREE_OPERAND (t, 1));
15673 tree op2 = RECUR (TREE_OPERAND (t, 2));
15674 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
15675 complain));
15678 default:
15679 /* Handle Objective-C++ constructs, if appropriate. */
15681 tree subst
15682 = objcp_tsubst_copy_and_build (t, args, complain,
15683 in_decl, /*function_p=*/false);
15684 if (subst)
15685 RETURN (subst);
15687 RETURN (tsubst_copy (t, args, complain, in_decl));
15690 #undef RECUR
15691 #undef RETURN
15692 out:
15693 input_location = loc;
15694 return retval;
15697 /* Verify that the instantiated ARGS are valid. For type arguments,
15698 make sure that the type's linkage is ok. For non-type arguments,
15699 make sure they are constants if they are integral or enumerations.
15700 Emit an error under control of COMPLAIN, and return TRUE on error. */
15702 static bool
15703 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15705 if (dependent_template_arg_p (t))
15706 return false;
15707 if (ARGUMENT_PACK_P (t))
15709 tree vec = ARGUMENT_PACK_ARGS (t);
15710 int len = TREE_VEC_LENGTH (vec);
15711 bool result = false;
15712 int i;
15714 for (i = 0; i < len; ++i)
15715 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15716 result = true;
15717 return result;
15719 else if (TYPE_P (t))
15721 /* [basic.link]: A name with no linkage (notably, the name
15722 of a class or enumeration declared in a local scope)
15723 shall not be used to declare an entity with linkage.
15724 This implies that names with no linkage cannot be used as
15725 template arguments
15727 DR 757 relaxes this restriction for C++0x. */
15728 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15729 : no_linkage_check (t, /*relaxed_p=*/false));
15731 if (nt)
15733 /* DR 488 makes use of a type with no linkage cause
15734 type deduction to fail. */
15735 if (complain & tf_error)
15737 if (TYPE_ANONYMOUS_P (nt))
15738 error ("%qT is/uses anonymous type", t);
15739 else
15740 error ("template argument for %qD uses local type %qT",
15741 tmpl, t);
15743 return true;
15745 /* In order to avoid all sorts of complications, we do not
15746 allow variably-modified types as template arguments. */
15747 else if (variably_modified_type_p (t, NULL_TREE))
15749 if (complain & tf_error)
15750 error ("%qT is a variably modified type", t);
15751 return true;
15754 /* Class template and alias template arguments should be OK. */
15755 else if (DECL_TYPE_TEMPLATE_P (t))
15757 /* A non-type argument of integral or enumerated type must be a
15758 constant. */
15759 else if (TREE_TYPE (t)
15760 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15761 && !REFERENCE_REF_P (t)
15762 && !TREE_CONSTANT (t))
15764 if (complain & tf_error)
15765 error ("integral expression %qE is not constant", t);
15766 return true;
15768 return false;
15771 static bool
15772 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15774 int ix, len = DECL_NTPARMS (tmpl);
15775 bool result = false;
15777 for (ix = 0; ix != len; ix++)
15779 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15780 result = true;
15782 if (result && (complain & tf_error))
15783 error (" trying to instantiate %qD", tmpl);
15784 return result;
15787 /* We're out of SFINAE context now, so generate diagnostics for the access
15788 errors we saw earlier when instantiating D from TMPL and ARGS. */
15790 static void
15791 recheck_decl_substitution (tree d, tree tmpl, tree args)
15793 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15794 tree type = TREE_TYPE (pattern);
15795 location_t loc = input_location;
15797 push_access_scope (d);
15798 push_deferring_access_checks (dk_no_deferred);
15799 input_location = DECL_SOURCE_LOCATION (pattern);
15800 tsubst (type, args, tf_warning_or_error, d);
15801 input_location = loc;
15802 pop_deferring_access_checks ();
15803 pop_access_scope (d);
15806 /* Instantiate the indicated variable, function, or alias template TMPL with
15807 the template arguments in TARG_PTR. */
15809 static tree
15810 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15812 tree targ_ptr = orig_args;
15813 tree fndecl;
15814 tree gen_tmpl;
15815 tree spec;
15816 bool access_ok = true;
15818 if (tmpl == error_mark_node)
15819 return error_mark_node;
15821 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15823 /* If this function is a clone, handle it specially. */
15824 if (DECL_CLONED_FUNCTION_P (tmpl))
15826 tree spec;
15827 tree clone;
15829 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15830 DECL_CLONED_FUNCTION. */
15831 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15832 targ_ptr, complain);
15833 if (spec == error_mark_node)
15834 return error_mark_node;
15836 /* Look for the clone. */
15837 FOR_EACH_CLONE (clone, spec)
15838 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15839 return clone;
15840 /* We should always have found the clone by now. */
15841 gcc_unreachable ();
15842 return NULL_TREE;
15845 if (targ_ptr == error_mark_node)
15846 return error_mark_node;
15848 /* Check to see if we already have this specialization. */
15849 gen_tmpl = most_general_template (tmpl);
15850 if (tmpl != gen_tmpl)
15851 /* The TMPL is a partial instantiation. To get a full set of
15852 arguments we must add the arguments used to perform the
15853 partial instantiation. */
15854 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15855 targ_ptr);
15857 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15858 but it doesn't seem to be on the hot path. */
15859 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15861 gcc_assert (tmpl == gen_tmpl
15862 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15863 == spec)
15864 || fndecl == NULL_TREE);
15866 if (spec != NULL_TREE)
15868 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15870 if (complain & tf_error)
15871 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15872 return error_mark_node;
15874 return spec;
15877 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15878 complain))
15879 return error_mark_node;
15881 /* We are building a FUNCTION_DECL, during which the access of its
15882 parameters and return types have to be checked. However this
15883 FUNCTION_DECL which is the desired context for access checking
15884 is not built yet. We solve this chicken-and-egg problem by
15885 deferring all checks until we have the FUNCTION_DECL. */
15886 push_deferring_access_checks (dk_deferred);
15888 /* Instantiation of the function happens in the context of the function
15889 template, not the context of the overload resolution we're doing. */
15890 push_to_top_level ();
15891 /* If there are dependent arguments, e.g. because we're doing partial
15892 ordering, make sure processing_template_decl stays set. */
15893 if (uses_template_parms (targ_ptr))
15894 ++processing_template_decl;
15895 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15897 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
15898 complain, gen_tmpl, true);
15899 push_nested_class (ctx);
15902 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
15904 if (VAR_P (pattern))
15906 /* We need to determine if we're using a partial or explicit
15907 specialization now, because the type of the variable could be
15908 different. */
15909 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
15910 tree elt = most_specialized_partial_spec (tid, complain);
15911 if (elt == error_mark_node)
15912 pattern = error_mark_node;
15913 else if (elt)
15915 tmpl = TREE_VALUE (elt);
15916 pattern = DECL_TEMPLATE_RESULT (tmpl);
15917 targ_ptr = TREE_PURPOSE (elt);
15921 /* Substitute template parameters to obtain the specialization. */
15922 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
15923 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15924 pop_nested_class ();
15925 pop_from_top_level ();
15927 if (fndecl == error_mark_node)
15929 pop_deferring_access_checks ();
15930 return error_mark_node;
15933 /* The DECL_TI_TEMPLATE should always be the immediate parent
15934 template, not the most general template. */
15935 DECL_TI_TEMPLATE (fndecl) = tmpl;
15937 /* Now we know the specialization, compute access previously
15938 deferred. */
15939 push_access_scope (fndecl);
15940 if (!perform_deferred_access_checks (complain))
15941 access_ok = false;
15942 pop_access_scope (fndecl);
15943 pop_deferring_access_checks ();
15945 /* If we've just instantiated the main entry point for a function,
15946 instantiate all the alternate entry points as well. We do this
15947 by cloning the instantiation of the main entry point, not by
15948 instantiating the template clones. */
15949 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15950 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15952 if (!access_ok)
15954 if (!(complain & tf_error))
15956 /* Remember to reinstantiate when we're out of SFINAE so the user
15957 can see the errors. */
15958 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15960 return error_mark_node;
15962 return fndecl;
15965 /* Wrapper for instantiate_template_1. */
15967 tree
15968 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15970 tree ret;
15971 timevar_push (TV_TEMPLATE_INST);
15972 ret = instantiate_template_1 (tmpl, orig_args, complain);
15973 timevar_pop (TV_TEMPLATE_INST);
15974 return ret;
15977 /* Instantiate the alias template TMPL with ARGS. Also push a template
15978 instantiation level, which instantiate_template doesn't do because
15979 functions and variables have sufficient context established by the
15980 callers. */
15982 static tree
15983 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15985 struct pending_template *old_last_pend = last_pending_template;
15986 struct tinst_level *old_error_tinst = last_error_tinst_level;
15987 if (tmpl == error_mark_node || args == error_mark_node)
15988 return error_mark_node;
15989 tree tinst = build_tree_list (tmpl, args);
15990 if (!push_tinst_level (tinst))
15992 ggc_free (tinst);
15993 return error_mark_node;
15996 args =
15997 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15998 args, tmpl, complain,
15999 /*require_all_args=*/true,
16000 /*use_default_args=*/true);
16002 tree r = instantiate_template (tmpl, args, complain);
16003 pop_tinst_level ();
16004 /* We can't free this if a pending_template entry or last_error_tinst_level
16005 is pointing at it. */
16006 if (last_pending_template == old_last_pend
16007 && last_error_tinst_level == old_error_tinst)
16008 ggc_free (tinst);
16010 return r;
16013 /* PARM is a template parameter pack for FN. Returns true iff
16014 PARM is used in a deducible way in the argument list of FN. */
16016 static bool
16017 pack_deducible_p (tree parm, tree fn)
16019 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
16020 for (; t; t = TREE_CHAIN (t))
16022 tree type = TREE_VALUE (t);
16023 tree packs;
16024 if (!PACK_EXPANSION_P (type))
16025 continue;
16026 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16027 packs; packs = TREE_CHAIN (packs))
16028 if (template_args_equal (TREE_VALUE (packs), parm))
16030 /* The template parameter pack is used in a function parameter
16031 pack. If this is the end of the parameter list, the
16032 template parameter pack is deducible. */
16033 if (TREE_CHAIN (t) == void_list_node)
16034 return true;
16035 else
16036 /* Otherwise, not. Well, it could be deduced from
16037 a non-pack parameter, but doing so would end up with
16038 a deduction mismatch, so don't bother. */
16039 return false;
16042 /* The template parameter pack isn't used in any function parameter
16043 packs, but it might be used deeper, e.g. tuple<Args...>. */
16044 return true;
16047 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16048 NARGS elements of the arguments that are being used when calling
16049 it. TARGS is a vector into which the deduced template arguments
16050 are placed.
16052 Returns either a FUNCTION_DECL for the matching specialization of FN or
16053 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16054 true, diagnostics will be printed to explain why it failed.
16056 If FN is a conversion operator, or we are trying to produce a specific
16057 specialization, RETURN_TYPE is the return type desired.
16059 The EXPLICIT_TARGS are explicit template arguments provided via a
16060 template-id.
16062 The parameter STRICT is one of:
16064 DEDUCE_CALL:
16065 We are deducing arguments for a function call, as in
16066 [temp.deduct.call].
16068 DEDUCE_CONV:
16069 We are deducing arguments for a conversion function, as in
16070 [temp.deduct.conv].
16072 DEDUCE_EXACT:
16073 We are deducing arguments when doing an explicit instantiation
16074 as in [temp.explicit], when determining an explicit specialization
16075 as in [temp.expl.spec], or when taking the address of a function
16076 template, as in [temp.deduct.funcaddr]. */
16078 tree
16079 fn_type_unification (tree fn,
16080 tree explicit_targs,
16081 tree targs,
16082 const tree *args,
16083 unsigned int nargs,
16084 tree return_type,
16085 unification_kind_t strict,
16086 int flags,
16087 bool explain_p,
16088 bool decltype_p)
16090 tree parms;
16091 tree fntype;
16092 tree decl = NULL_TREE;
16093 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
16094 bool ok;
16095 static int deduction_depth;
16096 struct pending_template *old_last_pend = last_pending_template;
16097 struct tinst_level *old_error_tinst = last_error_tinst_level;
16098 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
16099 tree tinst;
16100 tree r = error_mark_node;
16102 if (decltype_p)
16103 complain |= tf_decltype;
16105 /* In C++0x, it's possible to have a function template whose type depends
16106 on itself recursively. This is most obvious with decltype, but can also
16107 occur with enumeration scope (c++/48969). So we need to catch infinite
16108 recursion and reject the substitution at deduction time; this function
16109 will return error_mark_node for any repeated substitution.
16111 This also catches excessive recursion such as when f<N> depends on
16112 f<N-1> across all integers, and returns error_mark_node for all the
16113 substitutions back up to the initial one.
16115 This is, of course, not reentrant. */
16116 if (excessive_deduction_depth)
16117 return error_mark_node;
16118 tinst = build_tree_list (fn, NULL_TREE);
16119 ++deduction_depth;
16121 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
16123 fntype = TREE_TYPE (fn);
16124 if (explicit_targs)
16126 /* [temp.deduct]
16128 The specified template arguments must match the template
16129 parameters in kind (i.e., type, nontype, template), and there
16130 must not be more arguments than there are parameters;
16131 otherwise type deduction fails.
16133 Nontype arguments must match the types of the corresponding
16134 nontype template parameters, or must be convertible to the
16135 types of the corresponding nontype parameters as specified in
16136 _temp.arg.nontype_, otherwise type deduction fails.
16138 All references in the function type of the function template
16139 to the corresponding template parameters are replaced by the
16140 specified template argument values. If a substitution in a
16141 template parameter or in the function type of the function
16142 template results in an invalid type, type deduction fails. */
16143 int i, len = TREE_VEC_LENGTH (tparms);
16144 location_t loc = input_location;
16145 bool incomplete = false;
16147 /* Adjust any explicit template arguments before entering the
16148 substitution context. */
16149 explicit_targs
16150 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
16151 complain,
16152 /*require_all_args=*/false,
16153 /*use_default_args=*/false));
16154 if (explicit_targs == error_mark_node)
16155 goto fail;
16157 /* Substitute the explicit args into the function type. This is
16158 necessary so that, for instance, explicitly declared function
16159 arguments can match null pointed constants. If we were given
16160 an incomplete set of explicit args, we must not do semantic
16161 processing during substitution as we could create partial
16162 instantiations. */
16163 for (i = 0; i < len; i++)
16165 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16166 bool parameter_pack = false;
16167 tree targ = TREE_VEC_ELT (explicit_targs, i);
16169 /* Dig out the actual parm. */
16170 if (TREE_CODE (parm) == TYPE_DECL
16171 || TREE_CODE (parm) == TEMPLATE_DECL)
16173 parm = TREE_TYPE (parm);
16174 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
16176 else if (TREE_CODE (parm) == PARM_DECL)
16178 parm = DECL_INITIAL (parm);
16179 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
16182 if (!parameter_pack && targ == NULL_TREE)
16183 /* No explicit argument for this template parameter. */
16184 incomplete = true;
16186 if (parameter_pack && pack_deducible_p (parm, fn))
16188 /* Mark the argument pack as "incomplete". We could
16189 still deduce more arguments during unification.
16190 We remove this mark in type_unification_real. */
16191 if (targ)
16193 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
16194 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
16195 = ARGUMENT_PACK_ARGS (targ);
16198 /* We have some incomplete argument packs. */
16199 incomplete = true;
16203 TREE_VALUE (tinst) = explicit_targs;
16204 if (!push_tinst_level (tinst))
16206 excessive_deduction_depth = true;
16207 goto fail;
16209 processing_template_decl += incomplete;
16210 input_location = DECL_SOURCE_LOCATION (fn);
16211 /* Ignore any access checks; we'll see them again in
16212 instantiate_template and they might have the wrong
16213 access path at this point. */
16214 push_deferring_access_checks (dk_deferred);
16215 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
16216 complain | tf_partial, NULL_TREE);
16217 pop_deferring_access_checks ();
16218 input_location = loc;
16219 processing_template_decl -= incomplete;
16220 pop_tinst_level ();
16222 if (fntype == error_mark_node)
16223 goto fail;
16225 /* Place the explicitly specified arguments in TARGS. */
16226 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
16227 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
16230 /* Never do unification on the 'this' parameter. */
16231 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
16233 if (return_type)
16235 tree *new_args;
16237 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
16238 new_args = XALLOCAVEC (tree, nargs + 1);
16239 new_args[0] = return_type;
16240 memcpy (new_args + 1, args, nargs * sizeof (tree));
16241 args = new_args;
16242 ++nargs;
16245 /* We allow incomplete unification without an error message here
16246 because the standard doesn't seem to explicitly prohibit it. Our
16247 callers must be ready to deal with unification failures in any
16248 event. */
16250 TREE_VALUE (tinst) = targs;
16251 /* If we aren't explaining yet, push tinst context so we can see where
16252 any errors (e.g. from class instantiations triggered by instantiation
16253 of default template arguments) come from. If we are explaining, this
16254 context is redundant. */
16255 if (!explain_p && !push_tinst_level (tinst))
16257 excessive_deduction_depth = true;
16258 goto fail;
16261 /* type_unification_real will pass back any access checks from default
16262 template argument substitution. */
16263 vec<deferred_access_check, va_gc> *checks;
16264 checks = NULL;
16266 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16267 targs, parms, args, nargs, /*subr=*/0,
16268 strict, flags, &checks, explain_p);
16269 if (!explain_p)
16270 pop_tinst_level ();
16271 if (!ok)
16272 goto fail;
16274 /* Now that we have bindings for all of the template arguments,
16275 ensure that the arguments deduced for the template template
16276 parameters have compatible template parameter lists. We cannot
16277 check this property before we have deduced all template
16278 arguments, because the template parameter types of a template
16279 template parameter might depend on prior template parameters
16280 deduced after the template template parameter. The following
16281 ill-formed example illustrates this issue:
16283 template<typename T, template<T> class C> void f(C<5>, T);
16285 template<int N> struct X {};
16287 void g() {
16288 f(X<5>(), 5l); // error: template argument deduction fails
16291 The template parameter list of 'C' depends on the template type
16292 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16293 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16294 time that we deduce 'C'. */
16295 if (!template_template_parm_bindings_ok_p
16296 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16298 unify_inconsistent_template_template_parameters (explain_p);
16299 goto fail;
16302 /* All is well so far. Now, check:
16304 [temp.deduct]
16306 When all template arguments have been deduced, all uses of
16307 template parameters in nondeduced contexts are replaced with
16308 the corresponding deduced argument values. If the
16309 substitution results in an invalid type, as described above,
16310 type deduction fails. */
16311 TREE_VALUE (tinst) = targs;
16312 if (!push_tinst_level (tinst))
16314 excessive_deduction_depth = true;
16315 goto fail;
16318 /* Also collect access checks from the instantiation. */
16319 reopen_deferring_access_checks (checks);
16321 decl = instantiate_template (fn, targs, complain);
16323 checks = get_deferred_access_checks ();
16324 pop_deferring_access_checks ();
16326 pop_tinst_level ();
16328 if (decl == error_mark_node)
16329 goto fail;
16331 /* Now perform any access checks encountered during substitution. */
16332 push_access_scope (decl);
16333 ok = perform_access_checks (checks, complain);
16334 pop_access_scope (decl);
16335 if (!ok)
16336 goto fail;
16338 /* If we're looking for an exact match, check that what we got
16339 is indeed an exact match. It might not be if some template
16340 parameters are used in non-deduced contexts. But don't check
16341 for an exact match if we have dependent template arguments;
16342 in that case we're doing partial ordering, and we already know
16343 that we have two candidates that will provide the actual type. */
16344 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16346 tree substed = TREE_TYPE (decl);
16347 unsigned int i;
16349 tree sarg
16350 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16351 if (return_type)
16352 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16353 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16354 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16356 unify_type_mismatch (explain_p, args[i],
16357 TREE_VALUE (sarg));
16358 goto fail;
16362 r = decl;
16364 fail:
16365 --deduction_depth;
16366 if (excessive_deduction_depth)
16368 if (deduction_depth == 0)
16369 /* Reset once we're all the way out. */
16370 excessive_deduction_depth = false;
16373 /* We can't free this if a pending_template entry or last_error_tinst_level
16374 is pointing at it. */
16375 if (last_pending_template == old_last_pend
16376 && last_error_tinst_level == old_error_tinst)
16377 ggc_free (tinst);
16379 return r;
16382 /* Adjust types before performing type deduction, as described in
16383 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16384 sections are symmetric. PARM is the type of a function parameter
16385 or the return type of the conversion function. ARG is the type of
16386 the argument passed to the call, or the type of the value
16387 initialized with the result of the conversion function.
16388 ARG_EXPR is the original argument expression, which may be null. */
16390 static int
16391 maybe_adjust_types_for_deduction (unification_kind_t strict,
16392 tree* parm,
16393 tree* arg,
16394 tree arg_expr)
16396 int result = 0;
16398 switch (strict)
16400 case DEDUCE_CALL:
16401 break;
16403 case DEDUCE_CONV:
16405 /* Swap PARM and ARG throughout the remainder of this
16406 function; the handling is precisely symmetric since PARM
16407 will initialize ARG rather than vice versa. */
16408 tree* temp = parm;
16409 parm = arg;
16410 arg = temp;
16411 break;
16414 case DEDUCE_EXACT:
16415 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16416 too, but here handle it by stripping the reference from PARM
16417 rather than by adding it to ARG. */
16418 if (TREE_CODE (*parm) == REFERENCE_TYPE
16419 && TYPE_REF_IS_RVALUE (*parm)
16420 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16421 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16422 && TREE_CODE (*arg) == REFERENCE_TYPE
16423 && !TYPE_REF_IS_RVALUE (*arg))
16424 *parm = TREE_TYPE (*parm);
16425 /* Nothing else to do in this case. */
16426 return 0;
16428 default:
16429 gcc_unreachable ();
16432 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16434 /* [temp.deduct.call]
16436 If P is not a reference type:
16438 --If A is an array type, the pointer type produced by the
16439 array-to-pointer standard conversion (_conv.array_) is
16440 used in place of A for type deduction; otherwise,
16442 --If A is a function type, the pointer type produced by
16443 the function-to-pointer standard conversion
16444 (_conv.func_) is used in place of A for type deduction;
16445 otherwise,
16447 --If A is a cv-qualified type, the top level
16448 cv-qualifiers of A's type are ignored for type
16449 deduction. */
16450 if (TREE_CODE (*arg) == ARRAY_TYPE)
16451 *arg = build_pointer_type (TREE_TYPE (*arg));
16452 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16453 *arg = build_pointer_type (*arg);
16454 else
16455 *arg = TYPE_MAIN_VARIANT (*arg);
16458 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16459 of the form T&&, where T is a template parameter, and the argument
16460 is an lvalue, T is deduced as A& */
16461 if (TREE_CODE (*parm) == REFERENCE_TYPE
16462 && TYPE_REF_IS_RVALUE (*parm)
16463 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16464 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16465 && (arg_expr ? real_lvalue_p (arg_expr)
16466 /* try_one_overload doesn't provide an arg_expr, but
16467 functions are always lvalues. */
16468 : TREE_CODE (*arg) == FUNCTION_TYPE))
16469 *arg = build_reference_type (*arg);
16471 /* [temp.deduct.call]
16473 If P is a cv-qualified type, the top level cv-qualifiers
16474 of P's type are ignored for type deduction. If P is a
16475 reference type, the type referred to by P is used for
16476 type deduction. */
16477 *parm = TYPE_MAIN_VARIANT (*parm);
16478 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16480 *parm = TREE_TYPE (*parm);
16481 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16484 /* DR 322. For conversion deduction, remove a reference type on parm
16485 too (which has been swapped into ARG). */
16486 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16487 *arg = TREE_TYPE (*arg);
16489 return result;
16492 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16493 template which does contain any deducible template parameters; check if
16494 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16495 unify_one_argument. */
16497 static int
16498 check_non_deducible_conversion (tree parm, tree arg, int strict,
16499 int flags, bool explain_p)
16501 tree type;
16503 if (!TYPE_P (arg))
16504 type = TREE_TYPE (arg);
16505 else
16506 type = arg;
16508 if (same_type_p (parm, type))
16509 return unify_success (explain_p);
16511 if (strict == DEDUCE_CONV)
16513 if (can_convert_arg (type, parm, NULL_TREE, flags,
16514 explain_p ? tf_warning_or_error : tf_none))
16515 return unify_success (explain_p);
16517 else if (strict != DEDUCE_EXACT)
16519 if (can_convert_arg (parm, type,
16520 TYPE_P (arg) ? NULL_TREE : arg,
16521 flags, explain_p ? tf_warning_or_error : tf_none))
16522 return unify_success (explain_p);
16525 if (strict == DEDUCE_EXACT)
16526 return unify_type_mismatch (explain_p, parm, arg);
16527 else
16528 return unify_arg_conversion (explain_p, parm, type, arg);
16531 static bool uses_deducible_template_parms (tree type);
16533 /* Returns true iff the expression EXPR is one from which a template
16534 argument can be deduced. In other words, if it's an undecorated
16535 use of a template non-type parameter. */
16537 static bool
16538 deducible_expression (tree expr)
16540 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16543 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16544 deducible way; that is, if it has a max value of <PARM> - 1. */
16546 static bool
16547 deducible_array_bound (tree domain)
16549 if (domain == NULL_TREE)
16550 return false;
16552 tree max = TYPE_MAX_VALUE (domain);
16553 if (TREE_CODE (max) != MINUS_EXPR)
16554 return false;
16556 return deducible_expression (TREE_OPERAND (max, 0));
16559 /* Returns true iff the template arguments ARGS use a template parameter
16560 in a deducible way. */
16562 static bool
16563 deducible_template_args (tree args)
16565 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16567 bool deducible;
16568 tree elt = TREE_VEC_ELT (args, i);
16569 if (ARGUMENT_PACK_P (elt))
16570 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16571 else
16573 if (PACK_EXPANSION_P (elt))
16574 elt = PACK_EXPANSION_PATTERN (elt);
16575 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16576 deducible = true;
16577 else if (TYPE_P (elt))
16578 deducible = uses_deducible_template_parms (elt);
16579 else
16580 deducible = deducible_expression (elt);
16582 if (deducible)
16583 return true;
16585 return false;
16588 /* Returns true iff TYPE contains any deducible references to template
16589 parameters, as per 14.8.2.5. */
16591 static bool
16592 uses_deducible_template_parms (tree type)
16594 if (PACK_EXPANSION_P (type))
16595 type = PACK_EXPANSION_PATTERN (type);
16597 /* T
16598 cv-list T
16599 TT<T>
16600 TT<i>
16601 TT<> */
16602 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16603 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16604 return true;
16606 /* T*
16608 T&& */
16609 if (POINTER_TYPE_P (type))
16610 return uses_deducible_template_parms (TREE_TYPE (type));
16612 /* T[integer-constant ]
16613 type [i] */
16614 if (TREE_CODE (type) == ARRAY_TYPE)
16615 return (uses_deducible_template_parms (TREE_TYPE (type))
16616 || deducible_array_bound (TYPE_DOMAIN (type)));
16618 /* T type ::*
16619 type T::*
16620 T T::*
16621 T (type ::*)()
16622 type (T::*)()
16623 type (type ::*)(T)
16624 type (T::*)(T)
16625 T (type ::*)(T)
16626 T (T::*)()
16627 T (T::*)(T) */
16628 if (TYPE_PTRMEM_P (type))
16629 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16630 || (uses_deducible_template_parms
16631 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16633 /* template-name <T> (where template-name refers to a class template)
16634 template-name <i> (where template-name refers to a class template) */
16635 if (CLASS_TYPE_P (type)
16636 && CLASSTYPE_TEMPLATE_INFO (type)
16637 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16638 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16639 (CLASSTYPE_TI_ARGS (type)));
16641 /* type (T)
16643 T(T) */
16644 if (TREE_CODE (type) == FUNCTION_TYPE
16645 || TREE_CODE (type) == METHOD_TYPE)
16647 if (uses_deducible_template_parms (TREE_TYPE (type)))
16648 return true;
16649 tree parm = TYPE_ARG_TYPES (type);
16650 if (TREE_CODE (type) == METHOD_TYPE)
16651 parm = TREE_CHAIN (parm);
16652 for (; parm; parm = TREE_CHAIN (parm))
16653 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16654 return true;
16657 return false;
16660 /* Subroutine of type_unification_real and unify_pack_expansion to
16661 handle unification of a single P/A pair. Parameters are as
16662 for those functions. */
16664 static int
16665 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16666 int subr, unification_kind_t strict, int flags,
16667 bool explain_p)
16669 tree arg_expr = NULL_TREE;
16670 int arg_strict;
16672 if (arg == error_mark_node || parm == error_mark_node)
16673 return unify_invalid (explain_p);
16674 if (arg == unknown_type_node)
16675 /* We can't deduce anything from this, but we might get all the
16676 template args from other function args. */
16677 return unify_success (explain_p);
16679 /* Implicit conversions (Clause 4) will be performed on a function
16680 argument to convert it to the type of the corresponding function
16681 parameter if the parameter type contains no template-parameters that
16682 participate in template argument deduction. */
16683 if (TYPE_P (parm) && !uses_template_parms (parm))
16684 /* For function parameters that contain no template-parameters at all,
16685 we have historically checked for convertibility in order to shortcut
16686 consideration of this candidate. */
16687 return check_non_deducible_conversion (parm, arg, strict, flags,
16688 explain_p);
16689 else if (strict == DEDUCE_CALL
16690 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16691 /* For function parameters with only non-deducible template parameters,
16692 just return. */
16693 return unify_success (explain_p);
16695 switch (strict)
16697 case DEDUCE_CALL:
16698 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16699 | UNIFY_ALLOW_MORE_CV_QUAL
16700 | UNIFY_ALLOW_DERIVED);
16701 break;
16703 case DEDUCE_CONV:
16704 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16705 break;
16707 case DEDUCE_EXACT:
16708 arg_strict = UNIFY_ALLOW_NONE;
16709 break;
16711 default:
16712 gcc_unreachable ();
16715 /* We only do these transformations if this is the top-level
16716 parameter_type_list in a call or declaration matching; in other
16717 situations (nested function declarators, template argument lists) we
16718 won't be comparing a type to an expression, and we don't do any type
16719 adjustments. */
16720 if (!subr)
16722 if (!TYPE_P (arg))
16724 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16725 if (type_unknown_p (arg))
16727 /* [temp.deduct.type] A template-argument can be
16728 deduced from a pointer to function or pointer
16729 to member function argument if the set of
16730 overloaded functions does not contain function
16731 templates and at most one of a set of
16732 overloaded functions provides a unique
16733 match. */
16735 if (resolve_overloaded_unification
16736 (tparms, targs, parm, arg, strict,
16737 arg_strict, explain_p))
16738 return unify_success (explain_p);
16739 return unify_overload_resolution_failure (explain_p, arg);
16742 arg_expr = arg;
16743 arg = unlowered_expr_type (arg);
16744 if (arg == error_mark_node)
16745 return unify_invalid (explain_p);
16748 arg_strict |=
16749 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16751 else
16752 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16753 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
16754 return unify_template_argument_mismatch (explain_p, parm, arg);
16756 /* For deduction from an init-list we need the actual list. */
16757 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16758 arg = arg_expr;
16759 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16762 /* Most parms like fn_type_unification.
16764 If SUBR is 1, we're being called recursively (to unify the
16765 arguments of a function or method parameter of a function
16766 template).
16768 CHECKS is a pointer to a vector of access checks encountered while
16769 substituting default template arguments. */
16771 static int
16772 type_unification_real (tree tparms,
16773 tree targs,
16774 tree xparms,
16775 const tree *xargs,
16776 unsigned int xnargs,
16777 int subr,
16778 unification_kind_t strict,
16779 int flags,
16780 vec<deferred_access_check, va_gc> **checks,
16781 bool explain_p)
16783 tree parm, arg;
16784 int i;
16785 int ntparms = TREE_VEC_LENGTH (tparms);
16786 int saw_undeduced = 0;
16787 tree parms;
16788 const tree *args;
16789 unsigned int nargs;
16790 unsigned int ia;
16792 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16793 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16794 gcc_assert (ntparms > 0);
16796 /* Reset the number of non-defaulted template arguments contained
16797 in TARGS. */
16798 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16800 again:
16801 parms = xparms;
16802 args = xargs;
16803 nargs = xnargs;
16805 ia = 0;
16806 while (parms && parms != void_list_node
16807 && ia < nargs)
16809 parm = TREE_VALUE (parms);
16811 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16812 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16813 /* For a function parameter pack that occurs at the end of the
16814 parameter-declaration-list, the type A of each remaining
16815 argument of the call is compared with the type P of the
16816 declarator-id of the function parameter pack. */
16817 break;
16819 parms = TREE_CHAIN (parms);
16821 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16822 /* For a function parameter pack that does not occur at the
16823 end of the parameter-declaration-list, the type of the
16824 parameter pack is a non-deduced context. */
16825 continue;
16827 arg = args[ia];
16828 ++ia;
16830 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16831 flags, explain_p))
16832 return 1;
16835 if (parms
16836 && parms != void_list_node
16837 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16839 /* Unify the remaining arguments with the pack expansion type. */
16840 tree argvec;
16841 tree parmvec = make_tree_vec (1);
16843 /* Allocate a TREE_VEC and copy in all of the arguments */
16844 argvec = make_tree_vec (nargs - ia);
16845 for (i = 0; ia < nargs; ++ia, ++i)
16846 TREE_VEC_ELT (argvec, i) = args[ia];
16848 /* Copy the parameter into parmvec. */
16849 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16850 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16851 /*subr=*/subr, explain_p))
16852 return 1;
16854 /* Advance to the end of the list of parameters. */
16855 parms = TREE_CHAIN (parms);
16858 /* Fail if we've reached the end of the parm list, and more args
16859 are present, and the parm list isn't variadic. */
16860 if (ia < nargs && parms == void_list_node)
16861 return unify_too_many_arguments (explain_p, nargs, ia);
16862 /* Fail if parms are left and they don't have default values and
16863 they aren't all deduced as empty packs (c++/57397). This is
16864 consistent with sufficient_parms_p. */
16865 if (parms && parms != void_list_node
16866 && TREE_PURPOSE (parms) == NULL_TREE)
16868 unsigned int count = nargs;
16869 tree p = parms;
16870 bool type_pack_p;
16873 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
16874 if (!type_pack_p)
16875 count++;
16876 p = TREE_CHAIN (p);
16878 while (p && p != void_list_node);
16879 if (count != nargs)
16880 return unify_too_few_arguments (explain_p, ia, count,
16881 type_pack_p);
16884 if (!subr)
16886 tsubst_flags_t complain = (explain_p
16887 ? tf_warning_or_error
16888 : tf_none);
16890 for (i = 0; i < ntparms; i++)
16892 tree targ = TREE_VEC_ELT (targs, i);
16893 tree tparm = TREE_VEC_ELT (tparms, i);
16895 /* Clear the "incomplete" flags on all argument packs now so that
16896 substituting them into later default arguments works. */
16897 if (targ && ARGUMENT_PACK_P (targ))
16899 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16900 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16903 if (targ || tparm == error_mark_node)
16904 continue;
16905 tparm = TREE_VALUE (tparm);
16907 /* If this is an undeduced nontype parameter that depends on
16908 a type parameter, try another pass; its type may have been
16909 deduced from a later argument than the one from which
16910 this parameter can be deduced. */
16911 if (TREE_CODE (tparm) == PARM_DECL
16912 && uses_template_parms (TREE_TYPE (tparm))
16913 && !saw_undeduced++)
16914 goto again;
16916 /* Core issue #226 (C++0x) [temp.deduct]:
16918 If a template argument has not been deduced, its
16919 default template argument, if any, is used.
16921 When we are in C++98 mode, TREE_PURPOSE will either
16922 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16923 to explicitly check cxx_dialect here. */
16924 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16926 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16927 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16928 reopen_deferring_access_checks (*checks);
16929 location_t save_loc = input_location;
16930 if (DECL_P (parm))
16931 input_location = DECL_SOURCE_LOCATION (parm);
16932 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16933 arg = convert_template_argument (parm, arg, targs, complain,
16934 i, NULL_TREE);
16935 input_location = save_loc;
16936 *checks = get_deferred_access_checks ();
16937 pop_deferring_access_checks ();
16938 if (arg == error_mark_node)
16939 return 1;
16940 else
16942 TREE_VEC_ELT (targs, i) = arg;
16943 /* The position of the first default template argument,
16944 is also the number of non-defaulted arguments in TARGS.
16945 Record that. */
16946 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16947 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16948 continue;
16952 /* If the type parameter is a parameter pack, then it will
16953 be deduced to an empty parameter pack. */
16954 if (template_parameter_pack_p (tparm))
16956 tree arg;
16958 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16960 arg = make_node (NONTYPE_ARGUMENT_PACK);
16961 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16962 TREE_CONSTANT (arg) = 1;
16964 else
16965 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16967 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16969 TREE_VEC_ELT (targs, i) = arg;
16970 continue;
16973 return unify_parameter_deduction_failure (explain_p, tparm);
16976 #ifdef ENABLE_CHECKING
16977 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16978 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16979 #endif
16981 return unify_success (explain_p);
16984 /* Subroutine of type_unification_real. Args are like the variables
16985 at the call site. ARG is an overloaded function (or template-id);
16986 we try deducing template args from each of the overloads, and if
16987 only one succeeds, we go with that. Modifies TARGS and returns
16988 true on success. */
16990 static bool
16991 resolve_overloaded_unification (tree tparms,
16992 tree targs,
16993 tree parm,
16994 tree arg,
16995 unification_kind_t strict,
16996 int sub_strict,
16997 bool explain_p)
16999 tree tempargs = copy_node (targs);
17000 int good = 0;
17001 tree goodfn = NULL_TREE;
17002 bool addr_p;
17004 if (TREE_CODE (arg) == ADDR_EXPR)
17006 arg = TREE_OPERAND (arg, 0);
17007 addr_p = true;
17009 else
17010 addr_p = false;
17012 if (TREE_CODE (arg) == COMPONENT_REF)
17013 /* Handle `&x' where `x' is some static or non-static member
17014 function name. */
17015 arg = TREE_OPERAND (arg, 1);
17017 if (TREE_CODE (arg) == OFFSET_REF)
17018 arg = TREE_OPERAND (arg, 1);
17020 /* Strip baselink information. */
17021 if (BASELINK_P (arg))
17022 arg = BASELINK_FUNCTIONS (arg);
17024 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
17026 /* If we got some explicit template args, we need to plug them into
17027 the affected templates before we try to unify, in case the
17028 explicit args will completely resolve the templates in question. */
17030 int ok = 0;
17031 tree expl_subargs = TREE_OPERAND (arg, 1);
17032 arg = TREE_OPERAND (arg, 0);
17034 for (; arg; arg = OVL_NEXT (arg))
17036 tree fn = OVL_CURRENT (arg);
17037 tree subargs, elem;
17039 if (TREE_CODE (fn) != TEMPLATE_DECL)
17040 continue;
17042 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17043 expl_subargs, NULL_TREE, tf_none,
17044 /*require_all_args=*/true,
17045 /*use_default_args=*/true);
17046 if (subargs != error_mark_node
17047 && !any_dependent_template_arguments_p (subargs))
17049 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
17050 if (try_one_overload (tparms, targs, tempargs, parm,
17051 elem, strict, sub_strict, addr_p, explain_p)
17052 && (!goodfn || !same_type_p (goodfn, elem)))
17054 goodfn = elem;
17055 ++good;
17058 else if (subargs)
17059 ++ok;
17061 /* If no templates (or more than one) are fully resolved by the
17062 explicit arguments, this template-id is a non-deduced context; it
17063 could still be OK if we deduce all template arguments for the
17064 enclosing call through other arguments. */
17065 if (good != 1)
17066 good = ok;
17068 else if (TREE_CODE (arg) != OVERLOAD
17069 && TREE_CODE (arg) != FUNCTION_DECL)
17070 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17071 -- but the deduction does not succeed because the expression is
17072 not just the function on its own. */
17073 return false;
17074 else
17075 for (; arg; arg = OVL_NEXT (arg))
17076 if (try_one_overload (tparms, targs, tempargs, parm,
17077 TREE_TYPE (OVL_CURRENT (arg)),
17078 strict, sub_strict, addr_p, explain_p)
17079 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
17081 goodfn = OVL_CURRENT (arg);
17082 ++good;
17085 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17086 to function or pointer to member function argument if the set of
17087 overloaded functions does not contain function templates and at most
17088 one of a set of overloaded functions provides a unique match.
17090 So if we found multiple possibilities, we return success but don't
17091 deduce anything. */
17093 if (good == 1)
17095 int i = TREE_VEC_LENGTH (targs);
17096 for (; i--; )
17097 if (TREE_VEC_ELT (tempargs, i))
17099 tree old = TREE_VEC_ELT (targs, i);
17100 tree new_ = TREE_VEC_ELT (tempargs, i);
17101 if (new_ && old && ARGUMENT_PACK_P (old)
17102 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
17103 /* Don't forget explicit template arguments in a pack. */
17104 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
17105 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
17106 TREE_VEC_ELT (targs, i) = new_;
17109 if (good)
17110 return true;
17112 return false;
17115 /* Core DR 115: In contexts where deduction is done and fails, or in
17116 contexts where deduction is not done, if a template argument list is
17117 specified and it, along with any default template arguments, identifies
17118 a single function template specialization, then the template-id is an
17119 lvalue for the function template specialization. */
17121 tree
17122 resolve_nondeduced_context (tree orig_expr)
17124 tree expr, offset, baselink;
17125 bool addr;
17127 if (!type_unknown_p (orig_expr))
17128 return orig_expr;
17130 expr = orig_expr;
17131 addr = false;
17132 offset = NULL_TREE;
17133 baselink = NULL_TREE;
17135 if (TREE_CODE (expr) == ADDR_EXPR)
17137 expr = TREE_OPERAND (expr, 0);
17138 addr = true;
17140 if (TREE_CODE (expr) == OFFSET_REF)
17142 offset = expr;
17143 expr = TREE_OPERAND (expr, 1);
17145 if (BASELINK_P (expr))
17147 baselink = expr;
17148 expr = BASELINK_FUNCTIONS (expr);
17151 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
17153 int good = 0;
17154 tree goodfn = NULL_TREE;
17156 /* If we got some explicit template args, we need to plug them into
17157 the affected templates before we try to unify, in case the
17158 explicit args will completely resolve the templates in question. */
17160 tree expl_subargs = TREE_OPERAND (expr, 1);
17161 tree arg = TREE_OPERAND (expr, 0);
17162 tree badfn = NULL_TREE;
17163 tree badargs = NULL_TREE;
17165 for (; arg; arg = OVL_NEXT (arg))
17167 tree fn = OVL_CURRENT (arg);
17168 tree subargs, elem;
17170 if (TREE_CODE (fn) != TEMPLATE_DECL)
17171 continue;
17173 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17174 expl_subargs, NULL_TREE, tf_none,
17175 /*require_all_args=*/true,
17176 /*use_default_args=*/true);
17177 if (subargs != error_mark_node
17178 && !any_dependent_template_arguments_p (subargs))
17180 elem = instantiate_template (fn, subargs, tf_none);
17181 if (elem == error_mark_node)
17183 badfn = fn;
17184 badargs = subargs;
17186 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
17188 goodfn = elem;
17189 ++good;
17193 if (good == 1)
17195 mark_used (goodfn);
17196 expr = goodfn;
17197 if (baselink)
17198 expr = build_baselink (BASELINK_BINFO (baselink),
17199 BASELINK_ACCESS_BINFO (baselink),
17200 expr, BASELINK_OPTYPE (baselink));
17201 if (offset)
17203 tree base
17204 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
17205 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
17207 if (addr)
17208 expr = cp_build_addr_expr (expr, tf_warning_or_error);
17209 return expr;
17211 else if (good == 0 && badargs)
17212 /* There were no good options and at least one bad one, so let the
17213 user know what the problem is. */
17214 instantiate_template (badfn, badargs, tf_warning_or_error);
17216 return orig_expr;
17219 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17220 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17221 different overloads deduce different arguments for a given parm.
17222 ADDR_P is true if the expression for which deduction is being
17223 performed was of the form "& fn" rather than simply "fn".
17225 Returns 1 on success. */
17227 static int
17228 try_one_overload (tree tparms,
17229 tree orig_targs,
17230 tree targs,
17231 tree parm,
17232 tree arg,
17233 unification_kind_t strict,
17234 int sub_strict,
17235 bool addr_p,
17236 bool explain_p)
17238 int nargs;
17239 tree tempargs;
17240 int i;
17242 if (arg == error_mark_node)
17243 return 0;
17245 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17246 to function or pointer to member function argument if the set of
17247 overloaded functions does not contain function templates and at most
17248 one of a set of overloaded functions provides a unique match.
17250 So if this is a template, just return success. */
17252 if (uses_template_parms (arg))
17253 return 1;
17255 if (TREE_CODE (arg) == METHOD_TYPE)
17256 arg = build_ptrmemfunc_type (build_pointer_type (arg));
17257 else if (addr_p)
17258 arg = build_pointer_type (arg);
17260 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17262 /* We don't copy orig_targs for this because if we have already deduced
17263 some template args from previous args, unify would complain when we
17264 try to deduce a template parameter for the same argument, even though
17265 there isn't really a conflict. */
17266 nargs = TREE_VEC_LENGTH (targs);
17267 tempargs = make_tree_vec (nargs);
17269 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17270 return 0;
17272 /* First make sure we didn't deduce anything that conflicts with
17273 explicitly specified args. */
17274 for (i = nargs; i--; )
17276 tree elt = TREE_VEC_ELT (tempargs, i);
17277 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17279 if (!elt)
17280 /*NOP*/;
17281 else if (uses_template_parms (elt))
17282 /* Since we're unifying against ourselves, we will fill in
17283 template args used in the function parm list with our own
17284 template parms. Discard them. */
17285 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17286 else if (oldelt && !template_args_equal (oldelt, elt))
17287 return 0;
17290 for (i = nargs; i--; )
17292 tree elt = TREE_VEC_ELT (tempargs, i);
17294 if (elt)
17295 TREE_VEC_ELT (targs, i) = elt;
17298 return 1;
17301 /* PARM is a template class (perhaps with unbound template
17302 parameters). ARG is a fully instantiated type. If ARG can be
17303 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17304 TARGS are as for unify. */
17306 static tree
17307 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17308 bool explain_p)
17310 tree copy_of_targs;
17312 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17313 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17314 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17315 return NULL_TREE;
17317 /* We need to make a new template argument vector for the call to
17318 unify. If we used TARGS, we'd clutter it up with the result of
17319 the attempted unification, even if this class didn't work out.
17320 We also don't want to commit ourselves to all the unifications
17321 we've already done, since unification is supposed to be done on
17322 an argument-by-argument basis. In other words, consider the
17323 following pathological case:
17325 template <int I, int J, int K>
17326 struct S {};
17328 template <int I, int J>
17329 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17331 template <int I, int J, int K>
17332 void f(S<I, J, K>, S<I, I, I>);
17334 void g() {
17335 S<0, 0, 0> s0;
17336 S<0, 1, 2> s2;
17338 f(s0, s2);
17341 Now, by the time we consider the unification involving `s2', we
17342 already know that we must have `f<0, 0, 0>'. But, even though
17343 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17344 because there are two ways to unify base classes of S<0, 1, 2>
17345 with S<I, I, I>. If we kept the already deduced knowledge, we
17346 would reject the possibility I=1. */
17347 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17349 /* If unification failed, we're done. */
17350 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17351 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17352 return NULL_TREE;
17354 return arg;
17357 /* Given a template type PARM and a class type ARG, find the unique
17358 base type in ARG that is an instance of PARM. We do not examine
17359 ARG itself; only its base-classes. If there is not exactly one
17360 appropriate base class, return NULL_TREE. PARM may be the type of
17361 a partial specialization, as well as a plain template type. Used
17362 by unify. */
17364 static enum template_base_result
17365 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17366 bool explain_p, tree *result)
17368 tree rval = NULL_TREE;
17369 tree binfo;
17371 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17373 binfo = TYPE_BINFO (complete_type (arg));
17374 if (!binfo)
17376 /* The type could not be completed. */
17377 *result = NULL_TREE;
17378 return tbr_incomplete_type;
17381 /* Walk in inheritance graph order. The search order is not
17382 important, and this avoids multiple walks of virtual bases. */
17383 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17385 tree r = try_class_unification (tparms, targs, parm,
17386 BINFO_TYPE (binfo), explain_p);
17388 if (r)
17390 /* If there is more than one satisfactory baseclass, then:
17392 [temp.deduct.call]
17394 If they yield more than one possible deduced A, the type
17395 deduction fails.
17397 applies. */
17398 if (rval && !same_type_p (r, rval))
17400 *result = NULL_TREE;
17401 return tbr_ambiguous_baseclass;
17404 rval = r;
17408 *result = rval;
17409 return tbr_success;
17412 /* Returns the level of DECL, which declares a template parameter. */
17414 static int
17415 template_decl_level (tree decl)
17417 switch (TREE_CODE (decl))
17419 case TYPE_DECL:
17420 case TEMPLATE_DECL:
17421 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17423 case PARM_DECL:
17424 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17426 default:
17427 gcc_unreachable ();
17429 return 0;
17432 /* Decide whether ARG can be unified with PARM, considering only the
17433 cv-qualifiers of each type, given STRICT as documented for unify.
17434 Returns nonzero iff the unification is OK on that basis. */
17436 static int
17437 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17439 int arg_quals = cp_type_quals (arg);
17440 int parm_quals = cp_type_quals (parm);
17442 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17443 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17445 /* Although a CVR qualifier is ignored when being applied to a
17446 substituted template parameter ([8.3.2]/1 for example), that
17447 does not allow us to unify "const T" with "int&" because both
17448 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17449 It is ok when we're allowing additional CV qualifiers
17450 at the outer level [14.8.2.1]/3,1st bullet. */
17451 if ((TREE_CODE (arg) == REFERENCE_TYPE
17452 || TREE_CODE (arg) == FUNCTION_TYPE
17453 || TREE_CODE (arg) == METHOD_TYPE)
17454 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17455 return 0;
17457 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17458 && (parm_quals & TYPE_QUAL_RESTRICT))
17459 return 0;
17462 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17463 && (arg_quals & parm_quals) != parm_quals)
17464 return 0;
17466 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17467 && (parm_quals & arg_quals) != arg_quals)
17468 return 0;
17470 return 1;
17473 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17474 void
17475 template_parm_level_and_index (tree parm, int* level, int* index)
17477 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17478 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17479 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17481 *index = TEMPLATE_TYPE_IDX (parm);
17482 *level = TEMPLATE_TYPE_LEVEL (parm);
17484 else
17486 *index = TEMPLATE_PARM_IDX (parm);
17487 *level = TEMPLATE_PARM_LEVEL (parm);
17491 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17492 do { \
17493 if (unify (TP, TA, P, A, S, EP)) \
17494 return 1; \
17495 } while (0);
17497 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17498 expansion at the end of PACKED_PARMS. Returns 0 if the type
17499 deduction succeeds, 1 otherwise. STRICT is the same as in
17500 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17501 call argument list. We'll need to adjust the arguments to make them
17502 types. SUBR tells us if this is from a recursive call to
17503 type_unification_real, or for comparing two template argument
17504 lists. */
17506 static int
17507 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17508 tree packed_args, unification_kind_t strict,
17509 bool subr, bool explain_p)
17511 tree parm
17512 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17513 tree pattern = PACK_EXPANSION_PATTERN (parm);
17514 tree pack, packs = NULL_TREE;
17515 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17517 packed_args = expand_template_argument_pack (packed_args);
17519 int len = TREE_VEC_LENGTH (packed_args);
17521 /* Determine the parameter packs we will be deducing from the
17522 pattern, and record their current deductions. */
17523 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17524 pack; pack = TREE_CHAIN (pack))
17526 tree parm_pack = TREE_VALUE (pack);
17527 int idx, level;
17529 /* Determine the index and level of this parameter pack. */
17530 template_parm_level_and_index (parm_pack, &level, &idx);
17532 /* Keep track of the parameter packs and their corresponding
17533 argument packs. */
17534 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17535 TREE_TYPE (packs) = make_tree_vec (len - start);
17538 /* Loop through all of the arguments that have not yet been
17539 unified and unify each with the pattern. */
17540 for (i = start; i < len; i++)
17542 tree parm;
17543 bool any_explicit = false;
17544 tree arg = TREE_VEC_ELT (packed_args, i);
17546 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17547 or the element of its argument pack at the current index if
17548 this argument was explicitly specified. */
17549 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17551 int idx, level;
17552 tree arg, pargs;
17553 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17555 arg = NULL_TREE;
17556 if (TREE_VALUE (pack)
17557 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17558 && (i - start < TREE_VEC_LENGTH (pargs)))
17560 any_explicit = true;
17561 arg = TREE_VEC_ELT (pargs, i - start);
17563 TMPL_ARG (targs, level, idx) = arg;
17566 /* If we had explicit template arguments, substitute them into the
17567 pattern before deduction. */
17568 if (any_explicit)
17570 /* Some arguments might still be unspecified or dependent. */
17571 bool dependent;
17572 ++processing_template_decl;
17573 dependent = any_dependent_template_arguments_p (targs);
17574 if (!dependent)
17575 --processing_template_decl;
17576 parm = tsubst (pattern, targs,
17577 explain_p ? tf_warning_or_error : tf_none,
17578 NULL_TREE);
17579 if (dependent)
17580 --processing_template_decl;
17581 if (parm == error_mark_node)
17582 return 1;
17584 else
17585 parm = pattern;
17587 /* Unify the pattern with the current argument. */
17588 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17589 LOOKUP_IMPLICIT, explain_p))
17590 return 1;
17592 /* For each parameter pack, collect the deduced value. */
17593 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17595 int idx, level;
17596 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17598 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17599 TMPL_ARG (targs, level, idx);
17603 /* Verify that the results of unification with the parameter packs
17604 produce results consistent with what we've seen before, and make
17605 the deduced argument packs available. */
17606 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17608 tree old_pack = TREE_VALUE (pack);
17609 tree new_args = TREE_TYPE (pack);
17610 int i, len = TREE_VEC_LENGTH (new_args);
17611 int idx, level;
17612 bool nondeduced_p = false;
17614 /* By default keep the original deduced argument pack.
17615 If necessary, more specific code is going to update the
17616 resulting deduced argument later down in this function. */
17617 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17618 TMPL_ARG (targs, level, idx) = old_pack;
17620 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17621 actually deduce anything. */
17622 for (i = 0; i < len && !nondeduced_p; ++i)
17623 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17624 nondeduced_p = true;
17625 if (nondeduced_p)
17626 continue;
17628 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17630 /* If we had fewer function args than explicit template args,
17631 just use the explicits. */
17632 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17633 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17634 if (len < explicit_len)
17635 new_args = explicit_args;
17638 if (!old_pack)
17640 tree result;
17641 /* Build the deduced *_ARGUMENT_PACK. */
17642 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17644 result = make_node (NONTYPE_ARGUMENT_PACK);
17645 TREE_TYPE (result) =
17646 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17647 TREE_CONSTANT (result) = 1;
17649 else
17650 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17652 SET_ARGUMENT_PACK_ARGS (result, new_args);
17654 /* Note the deduced argument packs for this parameter
17655 pack. */
17656 TMPL_ARG (targs, level, idx) = result;
17658 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17659 && (ARGUMENT_PACK_ARGS (old_pack)
17660 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17662 /* We only had the explicitly-provided arguments before, but
17663 now we have a complete set of arguments. */
17664 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17666 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17667 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17668 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17670 else
17672 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17673 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17675 if (!comp_template_args_with_info (old_args, new_args,
17676 &bad_old_arg, &bad_new_arg))
17677 /* Inconsistent unification of this parameter pack. */
17678 return unify_parameter_pack_inconsistent (explain_p,
17679 bad_old_arg,
17680 bad_new_arg);
17684 return unify_success (explain_p);
17687 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17688 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17689 parameters and return value are as for unify. */
17691 static int
17692 unify_array_domain (tree tparms, tree targs,
17693 tree parm_dom, tree arg_dom,
17694 bool explain_p)
17696 tree parm_max;
17697 tree arg_max;
17698 bool parm_cst;
17699 bool arg_cst;
17701 /* Our representation of array types uses "N - 1" as the
17702 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17703 not an integer constant. We cannot unify arbitrarily
17704 complex expressions, so we eliminate the MINUS_EXPRs
17705 here. */
17706 parm_max = TYPE_MAX_VALUE (parm_dom);
17707 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17708 if (!parm_cst)
17710 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17711 parm_max = TREE_OPERAND (parm_max, 0);
17713 arg_max = TYPE_MAX_VALUE (arg_dom);
17714 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17715 if (!arg_cst)
17717 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17718 trying to unify the type of a variable with the type
17719 of a template parameter. For example:
17721 template <unsigned int N>
17722 void f (char (&) [N]);
17723 int g();
17724 void h(int i) {
17725 char a[g(i)];
17726 f(a);
17729 Here, the type of the ARG will be "int [g(i)]", and
17730 may be a SAVE_EXPR, etc. */
17731 if (TREE_CODE (arg_max) != MINUS_EXPR)
17732 return unify_vla_arg (explain_p, arg_dom);
17733 arg_max = TREE_OPERAND (arg_max, 0);
17736 /* If only one of the bounds used a MINUS_EXPR, compensate
17737 by adding one to the other bound. */
17738 if (parm_cst && !arg_cst)
17739 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17740 integer_type_node,
17741 parm_max,
17742 integer_one_node);
17743 else if (arg_cst && !parm_cst)
17744 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17745 integer_type_node,
17746 arg_max,
17747 integer_one_node);
17749 return unify (tparms, targs, parm_max, arg_max,
17750 UNIFY_ALLOW_INTEGER, explain_p);
17753 /* Deduce the value of template parameters. TPARMS is the (innermost)
17754 set of template parameters to a template. TARGS is the bindings
17755 for those template parameters, as determined thus far; TARGS may
17756 include template arguments for outer levels of template parameters
17757 as well. PARM is a parameter to a template function, or a
17758 subcomponent of that parameter; ARG is the corresponding argument.
17759 This function attempts to match PARM with ARG in a manner
17760 consistent with the existing assignments in TARGS. If more values
17761 are deduced, then TARGS is updated.
17763 Returns 0 if the type deduction succeeds, 1 otherwise. The
17764 parameter STRICT is a bitwise or of the following flags:
17766 UNIFY_ALLOW_NONE:
17767 Require an exact match between PARM and ARG.
17768 UNIFY_ALLOW_MORE_CV_QUAL:
17769 Allow the deduced ARG to be more cv-qualified (by qualification
17770 conversion) than ARG.
17771 UNIFY_ALLOW_LESS_CV_QUAL:
17772 Allow the deduced ARG to be less cv-qualified than ARG.
17773 UNIFY_ALLOW_DERIVED:
17774 Allow the deduced ARG to be a template base class of ARG,
17775 or a pointer to a template base class of the type pointed to by
17776 ARG.
17777 UNIFY_ALLOW_INTEGER:
17778 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17779 case for more information.
17780 UNIFY_ALLOW_OUTER_LEVEL:
17781 This is the outermost level of a deduction. Used to determine validity
17782 of qualification conversions. A valid qualification conversion must
17783 have const qualified pointers leading up to the inner type which
17784 requires additional CV quals, except at the outer level, where const
17785 is not required [conv.qual]. It would be normal to set this flag in
17786 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17787 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17788 This is the outermost level of a deduction, and PARM can be more CV
17789 qualified at this point.
17790 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17791 This is the outermost level of a deduction, and PARM can be less CV
17792 qualified at this point. */
17794 static int
17795 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17796 bool explain_p)
17798 int idx;
17799 tree targ;
17800 tree tparm;
17801 int strict_in = strict;
17803 /* I don't think this will do the right thing with respect to types.
17804 But the only case I've seen it in so far has been array bounds, where
17805 signedness is the only information lost, and I think that will be
17806 okay. */
17807 while (TREE_CODE (parm) == NOP_EXPR)
17808 parm = TREE_OPERAND (parm, 0);
17810 if (arg == error_mark_node)
17811 return unify_invalid (explain_p);
17812 if (arg == unknown_type_node
17813 || arg == init_list_type_node)
17814 /* We can't deduce anything from this, but we might get all the
17815 template args from other function args. */
17816 return unify_success (explain_p);
17818 /* If PARM uses template parameters, then we can't bail out here,
17819 even if ARG == PARM, since we won't record unifications for the
17820 template parameters. We might need them if we're trying to
17821 figure out which of two things is more specialized. */
17822 if (arg == parm && !uses_template_parms (parm))
17823 return unify_success (explain_p);
17825 /* Handle init lists early, so the rest of the function can assume
17826 we're dealing with a type. */
17827 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17829 tree elt, elttype;
17830 unsigned i;
17831 tree orig_parm = parm;
17833 /* Replace T with std::initializer_list<T> for deduction. */
17834 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17835 && flag_deduce_init_list)
17836 parm = listify (parm);
17838 if (!is_std_init_list (parm)
17839 && TREE_CODE (parm) != ARRAY_TYPE)
17840 /* We can only deduce from an initializer list argument if the
17841 parameter is std::initializer_list or an array; otherwise this
17842 is a non-deduced context. */
17843 return unify_success (explain_p);
17845 if (TREE_CODE (parm) == ARRAY_TYPE)
17846 elttype = TREE_TYPE (parm);
17847 else
17848 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17850 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17852 int elt_strict = strict;
17854 if (elt == error_mark_node)
17855 return unify_invalid (explain_p);
17857 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17859 tree type = TREE_TYPE (elt);
17860 /* It should only be possible to get here for a call. */
17861 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17862 elt_strict |= maybe_adjust_types_for_deduction
17863 (DEDUCE_CALL, &elttype, &type, elt);
17864 elt = type;
17867 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17868 explain_p);
17871 if (TREE_CODE (parm) == ARRAY_TYPE
17872 && deducible_array_bound (TYPE_DOMAIN (parm)))
17874 /* Also deduce from the length of the initializer list. */
17875 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17876 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17877 if (idx == error_mark_node)
17878 return unify_invalid (explain_p);
17879 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17880 idx, explain_p);
17883 /* If the std::initializer_list<T> deduction worked, replace the
17884 deduced A with std::initializer_list<A>. */
17885 if (orig_parm != parm)
17887 idx = TEMPLATE_TYPE_IDX (orig_parm);
17888 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17889 targ = listify (targ);
17890 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17892 return unify_success (explain_p);
17895 /* Immediately reject some pairs that won't unify because of
17896 cv-qualification mismatches. */
17897 if (TREE_CODE (arg) == TREE_CODE (parm)
17898 && TYPE_P (arg)
17899 /* It is the elements of the array which hold the cv quals of an array
17900 type, and the elements might be template type parms. We'll check
17901 when we recurse. */
17902 && TREE_CODE (arg) != ARRAY_TYPE
17903 /* We check the cv-qualifiers when unifying with template type
17904 parameters below. We want to allow ARG `const T' to unify with
17905 PARM `T' for example, when computing which of two templates
17906 is more specialized, for example. */
17907 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17908 && !check_cv_quals_for_unify (strict_in, arg, parm))
17909 return unify_cv_qual_mismatch (explain_p, parm, arg);
17911 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17912 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17913 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17914 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17915 strict &= ~UNIFY_ALLOW_DERIVED;
17916 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17917 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17919 switch (TREE_CODE (parm))
17921 case TYPENAME_TYPE:
17922 case SCOPE_REF:
17923 case UNBOUND_CLASS_TEMPLATE:
17924 /* In a type which contains a nested-name-specifier, template
17925 argument values cannot be deduced for template parameters used
17926 within the nested-name-specifier. */
17927 return unify_success (explain_p);
17929 case TEMPLATE_TYPE_PARM:
17930 case TEMPLATE_TEMPLATE_PARM:
17931 case BOUND_TEMPLATE_TEMPLATE_PARM:
17932 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17933 if (error_operand_p (tparm))
17934 return unify_invalid (explain_p);
17936 if (TEMPLATE_TYPE_LEVEL (parm)
17937 != template_decl_level (tparm))
17938 /* The PARM is not one we're trying to unify. Just check
17939 to see if it matches ARG. */
17941 if (TREE_CODE (arg) == TREE_CODE (parm)
17942 && (is_auto (parm) ? is_auto (arg)
17943 : same_type_p (parm, arg)))
17944 return unify_success (explain_p);
17945 else
17946 return unify_type_mismatch (explain_p, parm, arg);
17948 idx = TEMPLATE_TYPE_IDX (parm);
17949 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17950 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17951 if (error_operand_p (tparm))
17952 return unify_invalid (explain_p);
17954 /* Check for mixed types and values. */
17955 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17956 && TREE_CODE (tparm) != TYPE_DECL)
17957 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17958 && TREE_CODE (tparm) != TEMPLATE_DECL))
17959 gcc_unreachable ();
17961 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17963 /* ARG must be constructed from a template class or a template
17964 template parameter. */
17965 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17966 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17967 return unify_template_deduction_failure (explain_p, parm, arg);
17969 tree parmvec = TYPE_TI_ARGS (parm);
17970 /* An alias template name is never deduced. */
17971 if (TYPE_ALIAS_P (arg))
17972 arg = strip_typedefs (arg);
17973 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17974 tree full_argvec = add_to_template_args (targs, argvec);
17975 tree parm_parms
17976 = DECL_INNERMOST_TEMPLATE_PARMS
17977 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17978 int i, len;
17979 int parm_variadic_p = 0;
17981 /* The resolution to DR150 makes clear that default
17982 arguments for an N-argument may not be used to bind T
17983 to a template template parameter with fewer than N
17984 parameters. It is not safe to permit the binding of
17985 default arguments as an extension, as that may change
17986 the meaning of a conforming program. Consider:
17988 struct Dense { static const unsigned int dim = 1; };
17990 template <template <typename> class View,
17991 typename Block>
17992 void operator+(float, View<Block> const&);
17994 template <typename Block,
17995 unsigned int Dim = Block::dim>
17996 struct Lvalue_proxy { operator float() const; };
17998 void
17999 test_1d (void) {
18000 Lvalue_proxy<Dense> p;
18001 float b;
18002 b + p;
18005 Here, if Lvalue_proxy is permitted to bind to View, then
18006 the global operator+ will be used; if they are not, the
18007 Lvalue_proxy will be converted to float. */
18008 if (coerce_template_parms (parm_parms,
18009 full_argvec,
18010 TYPE_TI_TEMPLATE (parm),
18011 (explain_p
18012 ? tf_warning_or_error
18013 : tf_none),
18014 /*require_all_args=*/true,
18015 /*use_default_args=*/false)
18016 == error_mark_node)
18017 return 1;
18019 /* Deduce arguments T, i from TT<T> or TT<i>.
18020 We check each element of PARMVEC and ARGVEC individually
18021 rather than the whole TREE_VEC since they can have
18022 different number of elements. */
18024 parmvec = expand_template_argument_pack (parmvec);
18025 argvec = expand_template_argument_pack (argvec);
18027 len = TREE_VEC_LENGTH (parmvec);
18029 /* Check if the parameters end in a pack, making them
18030 variadic. */
18031 if (len > 0
18032 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
18033 parm_variadic_p = 1;
18035 for (i = 0; i < len - parm_variadic_p; ++i)
18036 /* If the template argument list of P contains a pack
18037 expansion that is not the last template argument, the
18038 entire template argument list is a non-deduced
18039 context. */
18040 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
18041 return unify_success (explain_p);
18043 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
18044 return unify_too_few_arguments (explain_p,
18045 TREE_VEC_LENGTH (argvec), len);
18047 for (i = 0; i < len - parm_variadic_p; ++i)
18049 RECUR_AND_CHECK_FAILURE (tparms, targs,
18050 TREE_VEC_ELT (parmvec, i),
18051 TREE_VEC_ELT (argvec, i),
18052 UNIFY_ALLOW_NONE, explain_p);
18055 if (parm_variadic_p
18056 && unify_pack_expansion (tparms, targs,
18057 parmvec, argvec,
18058 DEDUCE_EXACT,
18059 /*subr=*/true, explain_p))
18060 return 1;
18062 arg = TYPE_TI_TEMPLATE (arg);
18064 /* Fall through to deduce template name. */
18067 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18068 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18070 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18072 /* Simple cases: Value already set, does match or doesn't. */
18073 if (targ != NULL_TREE && template_args_equal (targ, arg))
18074 return unify_success (explain_p);
18075 else if (targ)
18076 return unify_inconsistency (explain_p, parm, targ, arg);
18078 else
18080 /* If PARM is `const T' and ARG is only `int', we don't have
18081 a match unless we are allowing additional qualification.
18082 If ARG is `const int' and PARM is just `T' that's OK;
18083 that binds `const int' to `T'. */
18084 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
18085 arg, parm))
18086 return unify_cv_qual_mismatch (explain_p, parm, arg);
18088 /* Consider the case where ARG is `const volatile int' and
18089 PARM is `const T'. Then, T should be `volatile int'. */
18090 arg = cp_build_qualified_type_real
18091 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
18092 if (arg == error_mark_node)
18093 return unify_invalid (explain_p);
18095 /* Simple cases: Value already set, does match or doesn't. */
18096 if (targ != NULL_TREE && same_type_p (targ, arg))
18097 return unify_success (explain_p);
18098 else if (targ)
18099 return unify_inconsistency (explain_p, parm, targ, arg);
18101 /* Make sure that ARG is not a variable-sized array. (Note
18102 that were talking about variable-sized arrays (like
18103 `int[n]'), rather than arrays of unknown size (like
18104 `int[]').) We'll get very confused by such a type since
18105 the bound of the array is not constant, and therefore
18106 not mangleable. Besides, such types are not allowed in
18107 ISO C++, so we can do as we please here. We do allow
18108 them for 'auto' deduction, since that isn't ABI-exposed. */
18109 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
18110 return unify_vla_arg (explain_p, arg);
18112 /* Strip typedefs as in convert_template_argument. */
18113 arg = canonicalize_type_argument (arg, tf_none);
18116 /* If ARG is a parameter pack or an expansion, we cannot unify
18117 against it unless PARM is also a parameter pack. */
18118 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18119 && !template_parameter_pack_p (parm))
18120 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18122 /* If the argument deduction results is a METHOD_TYPE,
18123 then there is a problem.
18124 METHOD_TYPE doesn't map to any real C++ type the result of
18125 the deduction can not be of that type. */
18126 if (TREE_CODE (arg) == METHOD_TYPE)
18127 return unify_method_type_error (explain_p, arg);
18129 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18130 return unify_success (explain_p);
18132 case TEMPLATE_PARM_INDEX:
18133 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18134 if (error_operand_p (tparm))
18135 return unify_invalid (explain_p);
18137 if (TEMPLATE_PARM_LEVEL (parm)
18138 != template_decl_level (tparm))
18140 /* The PARM is not one we're trying to unify. Just check
18141 to see if it matches ARG. */
18142 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
18143 && cp_tree_equal (parm, arg));
18144 if (result)
18145 unify_expression_unequal (explain_p, parm, arg);
18146 return result;
18149 idx = TEMPLATE_PARM_IDX (parm);
18150 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18152 if (targ)
18154 int x = !cp_tree_equal (targ, arg);
18155 if (x)
18156 unify_inconsistency (explain_p, parm, targ, arg);
18157 return x;
18160 /* [temp.deduct.type] If, in the declaration of a function template
18161 with a non-type template-parameter, the non-type
18162 template-parameter is used in an expression in the function
18163 parameter-list and, if the corresponding template-argument is
18164 deduced, the template-argument type shall match the type of the
18165 template-parameter exactly, except that a template-argument
18166 deduced from an array bound may be of any integral type.
18167 The non-type parameter might use already deduced type parameters. */
18168 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
18169 if (!TREE_TYPE (arg))
18170 /* Template-parameter dependent expression. Just accept it for now.
18171 It will later be processed in convert_template_argument. */
18173 else if (same_type_p (TREE_TYPE (arg), tparm))
18174 /* OK */;
18175 else if ((strict & UNIFY_ALLOW_INTEGER)
18176 && CP_INTEGRAL_TYPE_P (tparm))
18177 /* Convert the ARG to the type of PARM; the deduced non-type
18178 template argument must exactly match the types of the
18179 corresponding parameter. */
18180 arg = fold (build_nop (tparm, arg));
18181 else if (uses_template_parms (tparm))
18182 /* We haven't deduced the type of this parameter yet. Try again
18183 later. */
18184 return unify_success (explain_p);
18185 else
18186 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
18188 /* If ARG is a parameter pack or an expansion, we cannot unify
18189 against it unless PARM is also a parameter pack. */
18190 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18191 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
18192 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18194 arg = strip_typedefs_expr (arg);
18195 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18196 return unify_success (explain_p);
18198 case PTRMEM_CST:
18200 /* A pointer-to-member constant can be unified only with
18201 another constant. */
18202 if (TREE_CODE (arg) != PTRMEM_CST)
18203 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
18205 /* Just unify the class member. It would be useless (and possibly
18206 wrong, depending on the strict flags) to unify also
18207 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18208 arg refer to the same variable, even if through different
18209 classes. For instance:
18211 struct A { int x; };
18212 struct B : A { };
18214 Unification of &A::x and &B::x must succeed. */
18215 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
18216 PTRMEM_CST_MEMBER (arg), strict, explain_p);
18219 case POINTER_TYPE:
18221 if (!TYPE_PTR_P (arg))
18222 return unify_type_mismatch (explain_p, parm, arg);
18224 /* [temp.deduct.call]
18226 A can be another pointer or pointer to member type that can
18227 be converted to the deduced A via a qualification
18228 conversion (_conv.qual_).
18230 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18231 This will allow for additional cv-qualification of the
18232 pointed-to types if appropriate. */
18234 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
18235 /* The derived-to-base conversion only persists through one
18236 level of pointers. */
18237 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
18239 return unify (tparms, targs, TREE_TYPE (parm),
18240 TREE_TYPE (arg), strict, explain_p);
18243 case REFERENCE_TYPE:
18244 if (TREE_CODE (arg) != REFERENCE_TYPE)
18245 return unify_type_mismatch (explain_p, parm, arg);
18246 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18247 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18249 case ARRAY_TYPE:
18250 if (TREE_CODE (arg) != ARRAY_TYPE)
18251 return unify_type_mismatch (explain_p, parm, arg);
18252 if ((TYPE_DOMAIN (parm) == NULL_TREE)
18253 != (TYPE_DOMAIN (arg) == NULL_TREE))
18254 return unify_type_mismatch (explain_p, parm, arg);
18255 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18256 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18257 if (TYPE_DOMAIN (parm) != NULL_TREE)
18258 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18259 TYPE_DOMAIN (arg), explain_p);
18260 return unify_success (explain_p);
18262 case REAL_TYPE:
18263 case COMPLEX_TYPE:
18264 case VECTOR_TYPE:
18265 case INTEGER_TYPE:
18266 case BOOLEAN_TYPE:
18267 case ENUMERAL_TYPE:
18268 case VOID_TYPE:
18269 case NULLPTR_TYPE:
18270 if (TREE_CODE (arg) != TREE_CODE (parm))
18271 return unify_type_mismatch (explain_p, parm, arg);
18273 /* We have already checked cv-qualification at the top of the
18274 function. */
18275 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18276 return unify_type_mismatch (explain_p, parm, arg);
18278 /* As far as unification is concerned, this wins. Later checks
18279 will invalidate it if necessary. */
18280 return unify_success (explain_p);
18282 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18283 /* Type INTEGER_CST can come from ordinary constant template args. */
18284 case INTEGER_CST:
18285 while (TREE_CODE (arg) == NOP_EXPR)
18286 arg = TREE_OPERAND (arg, 0);
18288 if (TREE_CODE (arg) != INTEGER_CST)
18289 return unify_template_argument_mismatch (explain_p, parm, arg);
18290 return (tree_int_cst_equal (parm, arg)
18291 ? unify_success (explain_p)
18292 : unify_template_argument_mismatch (explain_p, parm, arg));
18294 case TREE_VEC:
18296 int i, len, argslen;
18297 int parm_variadic_p = 0;
18299 if (TREE_CODE (arg) != TREE_VEC)
18300 return unify_template_argument_mismatch (explain_p, parm, arg);
18302 len = TREE_VEC_LENGTH (parm);
18303 argslen = TREE_VEC_LENGTH (arg);
18305 /* Check for pack expansions in the parameters. */
18306 for (i = 0; i < len; ++i)
18308 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18310 if (i == len - 1)
18311 /* We can unify against something with a trailing
18312 parameter pack. */
18313 parm_variadic_p = 1;
18314 else
18315 /* [temp.deduct.type]/9: If the template argument list of
18316 P contains a pack expansion that is not the last
18317 template argument, the entire template argument list
18318 is a non-deduced context. */
18319 return unify_success (explain_p);
18323 /* If we don't have enough arguments to satisfy the parameters
18324 (not counting the pack expression at the end), or we have
18325 too many arguments for a parameter list that doesn't end in
18326 a pack expression, we can't unify. */
18327 if (parm_variadic_p
18328 ? argslen < len - parm_variadic_p
18329 : argslen != len)
18330 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18332 /* Unify all of the parameters that precede the (optional)
18333 pack expression. */
18334 for (i = 0; i < len - parm_variadic_p; ++i)
18336 RECUR_AND_CHECK_FAILURE (tparms, targs,
18337 TREE_VEC_ELT (parm, i),
18338 TREE_VEC_ELT (arg, i),
18339 UNIFY_ALLOW_NONE, explain_p);
18341 if (parm_variadic_p)
18342 return unify_pack_expansion (tparms, targs, parm, arg,
18343 DEDUCE_EXACT,
18344 /*subr=*/true, explain_p);
18345 return unify_success (explain_p);
18348 case RECORD_TYPE:
18349 case UNION_TYPE:
18350 if (TREE_CODE (arg) != TREE_CODE (parm))
18351 return unify_type_mismatch (explain_p, parm, arg);
18353 if (TYPE_PTRMEMFUNC_P (parm))
18355 if (!TYPE_PTRMEMFUNC_P (arg))
18356 return unify_type_mismatch (explain_p, parm, arg);
18358 return unify (tparms, targs,
18359 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18360 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18361 strict, explain_p);
18363 else if (TYPE_PTRMEMFUNC_P (arg))
18364 return unify_type_mismatch (explain_p, parm, arg);
18366 if (CLASSTYPE_TEMPLATE_INFO (parm))
18368 tree t = NULL_TREE;
18370 if (strict_in & UNIFY_ALLOW_DERIVED)
18372 /* First, we try to unify the PARM and ARG directly. */
18373 t = try_class_unification (tparms, targs,
18374 parm, arg, explain_p);
18376 if (!t)
18378 /* Fallback to the special case allowed in
18379 [temp.deduct.call]:
18381 If P is a class, and P has the form
18382 template-id, then A can be a derived class of
18383 the deduced A. Likewise, if P is a pointer to
18384 a class of the form template-id, A can be a
18385 pointer to a derived class pointed to by the
18386 deduced A. */
18387 enum template_base_result r;
18388 r = get_template_base (tparms, targs, parm, arg,
18389 explain_p, &t);
18391 if (!t)
18392 return unify_no_common_base (explain_p, r, parm, arg);
18395 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18396 && (CLASSTYPE_TI_TEMPLATE (parm)
18397 == CLASSTYPE_TI_TEMPLATE (arg)))
18398 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18399 Then, we should unify `int' and `U'. */
18400 t = arg;
18401 else
18402 /* There's no chance of unification succeeding. */
18403 return unify_type_mismatch (explain_p, parm, arg);
18405 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18406 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18408 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18409 return unify_type_mismatch (explain_p, parm, arg);
18410 return unify_success (explain_p);
18412 case METHOD_TYPE:
18413 case FUNCTION_TYPE:
18415 unsigned int nargs;
18416 tree *args;
18417 tree a;
18418 unsigned int i;
18420 if (TREE_CODE (arg) != TREE_CODE (parm))
18421 return unify_type_mismatch (explain_p, parm, arg);
18423 /* CV qualifications for methods can never be deduced, they must
18424 match exactly. We need to check them explicitly here,
18425 because type_unification_real treats them as any other
18426 cv-qualified parameter. */
18427 if (TREE_CODE (parm) == METHOD_TYPE
18428 && (!check_cv_quals_for_unify
18429 (UNIFY_ALLOW_NONE,
18430 class_of_this_parm (arg),
18431 class_of_this_parm (parm))))
18432 return unify_cv_qual_mismatch (explain_p, parm, arg);
18434 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18435 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18437 nargs = list_length (TYPE_ARG_TYPES (arg));
18438 args = XALLOCAVEC (tree, nargs);
18439 for (a = TYPE_ARG_TYPES (arg), i = 0;
18440 a != NULL_TREE && a != void_list_node;
18441 a = TREE_CHAIN (a), ++i)
18442 args[i] = TREE_VALUE (a);
18443 nargs = i;
18445 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18446 args, nargs, 1, DEDUCE_EXACT,
18447 LOOKUP_NORMAL, NULL, explain_p);
18450 case OFFSET_TYPE:
18451 /* Unify a pointer to member with a pointer to member function, which
18452 deduces the type of the member as a function type. */
18453 if (TYPE_PTRMEMFUNC_P (arg))
18455 /* Check top-level cv qualifiers */
18456 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18457 return unify_cv_qual_mismatch (explain_p, parm, arg);
18459 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18460 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18461 UNIFY_ALLOW_NONE, explain_p);
18463 /* Determine the type of the function we are unifying against. */
18464 tree fntype = static_fn_type (arg);
18466 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18469 if (TREE_CODE (arg) != OFFSET_TYPE)
18470 return unify_type_mismatch (explain_p, parm, arg);
18471 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18472 TYPE_OFFSET_BASETYPE (arg),
18473 UNIFY_ALLOW_NONE, explain_p);
18474 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18475 strict, explain_p);
18477 case CONST_DECL:
18478 if (DECL_TEMPLATE_PARM_P (parm))
18479 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18480 if (arg != scalar_constant_value (parm))
18481 return unify_template_argument_mismatch (explain_p, parm, arg);
18482 return unify_success (explain_p);
18484 case FIELD_DECL:
18485 case TEMPLATE_DECL:
18486 /* Matched cases are handled by the ARG == PARM test above. */
18487 return unify_template_argument_mismatch (explain_p, parm, arg);
18489 case VAR_DECL:
18490 /* A non-type template parameter that is a variable should be a
18491 an integral constant, in which case, it whould have been
18492 folded into its (constant) value. So we should not be getting
18493 a variable here. */
18494 gcc_unreachable ();
18496 case TYPE_ARGUMENT_PACK:
18497 case NONTYPE_ARGUMENT_PACK:
18498 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18499 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18501 case TYPEOF_TYPE:
18502 case DECLTYPE_TYPE:
18503 case UNDERLYING_TYPE:
18504 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18505 or UNDERLYING_TYPE nodes. */
18506 return unify_success (explain_p);
18508 case ERROR_MARK:
18509 /* Unification fails if we hit an error node. */
18510 return unify_invalid (explain_p);
18512 case INDIRECT_REF:
18513 if (REFERENCE_REF_P (parm))
18515 if (REFERENCE_REF_P (arg))
18516 arg = TREE_OPERAND (arg, 0);
18517 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18518 strict, explain_p);
18520 /* FALLTHRU */
18522 default:
18523 /* An unresolved overload is a nondeduced context. */
18524 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18525 return unify_success (explain_p);
18526 gcc_assert (EXPR_P (parm));
18528 /* We must be looking at an expression. This can happen with
18529 something like:
18531 template <int I>
18532 void foo(S<I>, S<I + 2>);
18534 This is a "nondeduced context":
18536 [deduct.type]
18538 The nondeduced contexts are:
18540 --A type that is a template-id in which one or more of
18541 the template-arguments is an expression that references
18542 a template-parameter.
18544 In these cases, we assume deduction succeeded, but don't
18545 actually infer any unifications. */
18547 if (!uses_template_parms (parm)
18548 && !template_args_equal (parm, arg))
18549 return unify_expression_unequal (explain_p, parm, arg);
18550 else
18551 return unify_success (explain_p);
18554 #undef RECUR_AND_CHECK_FAILURE
18556 /* Note that DECL can be defined in this translation unit, if
18557 required. */
18559 static void
18560 mark_definable (tree decl)
18562 tree clone;
18563 DECL_NOT_REALLY_EXTERN (decl) = 1;
18564 FOR_EACH_CLONE (clone, decl)
18565 DECL_NOT_REALLY_EXTERN (clone) = 1;
18568 /* Called if RESULT is explicitly instantiated, or is a member of an
18569 explicitly instantiated class. */
18571 void
18572 mark_decl_instantiated (tree result, int extern_p)
18574 SET_DECL_EXPLICIT_INSTANTIATION (result);
18576 /* If this entity has already been written out, it's too late to
18577 make any modifications. */
18578 if (TREE_ASM_WRITTEN (result))
18579 return;
18581 /* For anonymous namespace we don't need to do anything. */
18582 if (decl_anon_ns_mem_p (result))
18584 gcc_assert (!TREE_PUBLIC (result));
18585 return;
18588 if (TREE_CODE (result) != FUNCTION_DECL)
18589 /* The TREE_PUBLIC flag for function declarations will have been
18590 set correctly by tsubst. */
18591 TREE_PUBLIC (result) = 1;
18593 /* This might have been set by an earlier implicit instantiation. */
18594 DECL_COMDAT (result) = 0;
18596 if (extern_p)
18597 DECL_NOT_REALLY_EXTERN (result) = 0;
18598 else
18600 mark_definable (result);
18601 mark_needed (result);
18602 /* Always make artificials weak. */
18603 if (DECL_ARTIFICIAL (result) && flag_weak)
18604 comdat_linkage (result);
18605 /* For WIN32 we also want to put explicit instantiations in
18606 linkonce sections. */
18607 else if (TREE_PUBLIC (result))
18608 maybe_make_one_only (result);
18611 /* If EXTERN_P, then this function will not be emitted -- unless
18612 followed by an explicit instantiation, at which point its linkage
18613 will be adjusted. If !EXTERN_P, then this function will be
18614 emitted here. In neither circumstance do we want
18615 import_export_decl to adjust the linkage. */
18616 DECL_INTERFACE_KNOWN (result) = 1;
18619 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18620 important template arguments. If any are missing, we check whether
18621 they're important by using error_mark_node for substituting into any
18622 args that were used for partial ordering (the ones between ARGS and END)
18623 and seeing if it bubbles up. */
18625 static bool
18626 check_undeduced_parms (tree targs, tree args, tree end)
18628 bool found = false;
18629 int i;
18630 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18631 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18633 found = true;
18634 TREE_VEC_ELT (targs, i) = error_mark_node;
18636 if (found)
18638 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18639 if (substed == error_mark_node)
18640 return true;
18642 return false;
18645 /* Given two function templates PAT1 and PAT2, return:
18647 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18648 -1 if PAT2 is more specialized than PAT1.
18649 0 if neither is more specialized.
18651 LEN indicates the number of parameters we should consider
18652 (defaulted parameters should not be considered).
18654 The 1998 std underspecified function template partial ordering, and
18655 DR214 addresses the issue. We take pairs of arguments, one from
18656 each of the templates, and deduce them against each other. One of
18657 the templates will be more specialized if all the *other*
18658 template's arguments deduce against its arguments and at least one
18659 of its arguments *does* *not* deduce against the other template's
18660 corresponding argument. Deduction is done as for class templates.
18661 The arguments used in deduction have reference and top level cv
18662 qualifiers removed. Iff both arguments were originally reference
18663 types *and* deduction succeeds in both directions, an lvalue reference
18664 wins against an rvalue reference and otherwise the template
18665 with the more cv-qualified argument wins for that pairing (if
18666 neither is more cv-qualified, they both are equal). Unlike regular
18667 deduction, after all the arguments have been deduced in this way,
18668 we do *not* verify the deduced template argument values can be
18669 substituted into non-deduced contexts.
18671 The logic can be a bit confusing here, because we look at deduce1 and
18672 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18673 can find template arguments for pat1 to make arg1 look like arg2, that
18674 means that arg2 is at least as specialized as arg1. */
18677 more_specialized_fn (tree pat1, tree pat2, int len)
18679 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18680 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18681 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18682 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18683 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18684 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18685 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18686 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18687 tree origs1, origs2;
18688 bool lose1 = false;
18689 bool lose2 = false;
18691 /* Remove the this parameter from non-static member functions. If
18692 one is a non-static member function and the other is not a static
18693 member function, remove the first parameter from that function
18694 also. This situation occurs for operator functions where we
18695 locate both a member function (with this pointer) and non-member
18696 operator (with explicit first operand). */
18697 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18699 len--; /* LEN is the number of significant arguments for DECL1 */
18700 args1 = TREE_CHAIN (args1);
18701 if (!DECL_STATIC_FUNCTION_P (decl2))
18702 args2 = TREE_CHAIN (args2);
18704 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18706 args2 = TREE_CHAIN (args2);
18707 if (!DECL_STATIC_FUNCTION_P (decl1))
18709 len--;
18710 args1 = TREE_CHAIN (args1);
18714 /* If only one is a conversion operator, they are unordered. */
18715 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18716 return 0;
18718 /* Consider the return type for a conversion function */
18719 if (DECL_CONV_FN_P (decl1))
18721 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18722 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18723 len++;
18726 processing_template_decl++;
18728 origs1 = args1;
18729 origs2 = args2;
18731 while (len--
18732 /* Stop when an ellipsis is seen. */
18733 && args1 != NULL_TREE && args2 != NULL_TREE)
18735 tree arg1 = TREE_VALUE (args1);
18736 tree arg2 = TREE_VALUE (args2);
18737 int deduce1, deduce2;
18738 int quals1 = -1;
18739 int quals2 = -1;
18740 int ref1 = 0;
18741 int ref2 = 0;
18743 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18744 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18746 /* When both arguments are pack expansions, we need only
18747 unify the patterns themselves. */
18748 arg1 = PACK_EXPANSION_PATTERN (arg1);
18749 arg2 = PACK_EXPANSION_PATTERN (arg2);
18751 /* This is the last comparison we need to do. */
18752 len = 0;
18755 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18757 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18758 arg1 = TREE_TYPE (arg1);
18759 quals1 = cp_type_quals (arg1);
18762 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18764 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18765 arg2 = TREE_TYPE (arg2);
18766 quals2 = cp_type_quals (arg2);
18769 arg1 = TYPE_MAIN_VARIANT (arg1);
18770 arg2 = TYPE_MAIN_VARIANT (arg2);
18772 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18774 int i, len2 = list_length (args2);
18775 tree parmvec = make_tree_vec (1);
18776 tree argvec = make_tree_vec (len2);
18777 tree ta = args2;
18779 /* Setup the parameter vector, which contains only ARG1. */
18780 TREE_VEC_ELT (parmvec, 0) = arg1;
18782 /* Setup the argument vector, which contains the remaining
18783 arguments. */
18784 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18785 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18787 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18788 argvec, DEDUCE_EXACT,
18789 /*subr=*/true, /*explain_p=*/false)
18790 == 0);
18792 /* We cannot deduce in the other direction, because ARG1 is
18793 a pack expansion but ARG2 is not. */
18794 deduce2 = 0;
18796 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18798 int i, len1 = list_length (args1);
18799 tree parmvec = make_tree_vec (1);
18800 tree argvec = make_tree_vec (len1);
18801 tree ta = args1;
18803 /* Setup the parameter vector, which contains only ARG1. */
18804 TREE_VEC_ELT (parmvec, 0) = arg2;
18806 /* Setup the argument vector, which contains the remaining
18807 arguments. */
18808 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18809 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18811 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18812 argvec, DEDUCE_EXACT,
18813 /*subr=*/true, /*explain_p=*/false)
18814 == 0);
18816 /* We cannot deduce in the other direction, because ARG2 is
18817 a pack expansion but ARG1 is not.*/
18818 deduce1 = 0;
18821 else
18823 /* The normal case, where neither argument is a pack
18824 expansion. */
18825 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18826 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18827 == 0);
18828 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18829 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18830 == 0);
18833 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18834 arg2, then arg2 is not as specialized as arg1. */
18835 if (!deduce1)
18836 lose2 = true;
18837 if (!deduce2)
18838 lose1 = true;
18840 /* "If, for a given type, deduction succeeds in both directions
18841 (i.e., the types are identical after the transformations above)
18842 and both P and A were reference types (before being replaced with
18843 the type referred to above):
18844 - if the type from the argument template was an lvalue reference and
18845 the type from the parameter template was not, the argument type is
18846 considered to be more specialized than the other; otherwise,
18847 - if the type from the argument template is more cv-qualified
18848 than the type from the parameter template (as described above),
18849 the argument type is considered to be more specialized than the other;
18850 otherwise,
18851 - neither type is more specialized than the other." */
18853 if (deduce1 && deduce2)
18855 if (ref1 && ref2 && ref1 != ref2)
18857 if (ref1 > ref2)
18858 lose1 = true;
18859 else
18860 lose2 = true;
18862 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18864 if ((quals1 & quals2) == quals2)
18865 lose2 = true;
18866 if ((quals1 & quals2) == quals1)
18867 lose1 = true;
18871 if (lose1 && lose2)
18872 /* We've failed to deduce something in either direction.
18873 These must be unordered. */
18874 break;
18876 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18877 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18878 /* We have already processed all of the arguments in our
18879 handing of the pack expansion type. */
18880 len = 0;
18882 args1 = TREE_CHAIN (args1);
18883 args2 = TREE_CHAIN (args2);
18886 /* "In most cases, all template parameters must have values in order for
18887 deduction to succeed, but for partial ordering purposes a template
18888 parameter may remain without a value provided it is not used in the
18889 types being used for partial ordering."
18891 Thus, if we are missing any of the targs1 we need to substitute into
18892 origs1, then pat2 is not as specialized as pat1. This can happen when
18893 there is a nondeduced context. */
18894 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18895 lose2 = true;
18896 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18897 lose1 = true;
18899 processing_template_decl--;
18901 /* All things being equal, if the next argument is a pack expansion
18902 for one function but not for the other, prefer the
18903 non-variadic function. FIXME this is bogus; see c++/41958. */
18904 if (lose1 == lose2
18905 && args1 && TREE_VALUE (args1)
18906 && args2 && TREE_VALUE (args2))
18908 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18909 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18912 if (lose1 == lose2)
18913 return 0;
18914 else if (!lose1)
18915 return 1;
18916 else
18917 return -1;
18920 /* Determine which of two partial specializations of TMPL is more
18921 specialized.
18923 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
18924 to the first partial specialization. The TREE_PURPOSE is the
18925 innermost set of template parameters for the partial
18926 specialization. PAT2 is similar, but for the second template.
18928 Return 1 if the first partial specialization is more specialized;
18929 -1 if the second is more specialized; 0 if neither is more
18930 specialized.
18932 See [temp.class.order] for information about determining which of
18933 two templates is more specialized. */
18935 static int
18936 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
18938 tree targs;
18939 int winner = 0;
18940 bool any_deductions = false;
18942 tree tmpl1 = TREE_VALUE (pat1);
18943 tree tmpl2 = TREE_VALUE (pat2);
18944 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
18945 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
18946 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
18947 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
18949 /* Just like what happens for functions, if we are ordering between
18950 different template specializations, we may encounter dependent
18951 types in the arguments, and we need our dependency check functions
18952 to behave correctly. */
18953 ++processing_template_decl;
18954 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
18955 if (targs)
18957 --winner;
18958 any_deductions = true;
18961 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
18962 if (targs)
18964 ++winner;
18965 any_deductions = true;
18967 --processing_template_decl;
18969 /* In the case of a tie where at least one of the templates
18970 has a parameter pack at the end, the template with the most
18971 non-packed parameters wins. */
18972 if (winner == 0
18973 && any_deductions
18974 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18975 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18977 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18978 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18979 int len1 = TREE_VEC_LENGTH (args1);
18980 int len2 = TREE_VEC_LENGTH (args2);
18982 /* We don't count the pack expansion at the end. */
18983 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18984 --len1;
18985 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18986 --len2;
18988 if (len1 > len2)
18989 return 1;
18990 else if (len1 < len2)
18991 return -1;
18994 return winner;
18997 /* Return the template arguments that will produce the function signature
18998 DECL from the function template FN, with the explicit template
18999 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19000 also match. Return NULL_TREE if no satisfactory arguments could be
19001 found. */
19003 static tree
19004 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
19006 int ntparms = DECL_NTPARMS (fn);
19007 tree targs = make_tree_vec (ntparms);
19008 tree decl_type = TREE_TYPE (decl);
19009 tree decl_arg_types;
19010 tree *args;
19011 unsigned int nargs, ix;
19012 tree arg;
19014 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
19016 /* Never do unification on the 'this' parameter. */
19017 decl_arg_types = skip_artificial_parms_for (decl,
19018 TYPE_ARG_TYPES (decl_type));
19020 nargs = list_length (decl_arg_types);
19021 args = XALLOCAVEC (tree, nargs);
19022 for (arg = decl_arg_types, ix = 0;
19023 arg != NULL_TREE && arg != void_list_node;
19024 arg = TREE_CHAIN (arg), ++ix)
19025 args[ix] = TREE_VALUE (arg);
19027 if (fn_type_unification (fn, explicit_args, targs,
19028 args, ix,
19029 (check_rettype || DECL_CONV_FN_P (fn)
19030 ? TREE_TYPE (decl_type) : NULL_TREE),
19031 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
19032 /*decltype*/false)
19033 == error_mark_node)
19034 return NULL_TREE;
19036 return targs;
19039 /* Return the innermost template arguments that, when applied to a partial
19040 specialization of TMPL whose innermost template parameters are
19041 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19042 ARGS.
19044 For example, suppose we have:
19046 template <class T, class U> struct S {};
19047 template <class T> struct S<T*, int> {};
19049 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19050 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19051 int}. The resulting vector will be {double}, indicating that `T'
19052 is bound to `double'. */
19054 static tree
19055 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
19057 int i, ntparms = TREE_VEC_LENGTH (tparms);
19058 tree deduced_args;
19059 tree innermost_deduced_args;
19061 innermost_deduced_args = make_tree_vec (ntparms);
19062 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19064 deduced_args = copy_node (args);
19065 SET_TMPL_ARGS_LEVEL (deduced_args,
19066 TMPL_ARGS_DEPTH (deduced_args),
19067 innermost_deduced_args);
19069 else
19070 deduced_args = innermost_deduced_args;
19072 if (unify (tparms, deduced_args,
19073 INNERMOST_TEMPLATE_ARGS (spec_args),
19074 INNERMOST_TEMPLATE_ARGS (args),
19075 UNIFY_ALLOW_NONE, /*explain_p=*/false))
19076 return NULL_TREE;
19078 for (i = 0; i < ntparms; ++i)
19079 if (! TREE_VEC_ELT (innermost_deduced_args, i))
19080 return NULL_TREE;
19082 /* Verify that nondeduced template arguments agree with the type
19083 obtained from argument deduction.
19085 For example:
19087 struct A { typedef int X; };
19088 template <class T, class U> struct C {};
19089 template <class T> struct C<T, typename T::X> {};
19091 Then with the instantiation `C<A, int>', we can deduce that
19092 `T' is `A' but unify () does not check whether `typename T::X'
19093 is `int'. */
19094 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
19095 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19096 spec_args, tmpl,
19097 tf_none, false, false);
19098 if (spec_args == error_mark_node
19099 /* We only need to check the innermost arguments; the other
19100 arguments will always agree. */
19101 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
19102 INNERMOST_TEMPLATE_ARGS (args)))
19103 return NULL_TREE;
19105 /* Now that we have bindings for all of the template arguments,
19106 ensure that the arguments deduced for the template template
19107 parameters have compatible template parameter lists. See the use
19108 of template_template_parm_bindings_ok_p in fn_type_unification
19109 for more information. */
19110 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
19111 return NULL_TREE;
19113 return deduced_args;
19116 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19117 Return the TREE_LIST node with the most specialized template, if
19118 any. If there is no most specialized template, the error_mark_node
19119 is returned.
19121 Note that this function does not look at, or modify, the
19122 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19123 returned is one of the elements of INSTANTIATIONS, callers may
19124 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19125 and retrieve it from the value returned. */
19127 tree
19128 most_specialized_instantiation (tree templates)
19130 tree fn, champ;
19132 ++processing_template_decl;
19134 champ = templates;
19135 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
19137 int fate = 0;
19139 if (get_bindings (TREE_VALUE (champ),
19140 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19141 NULL_TREE, /*check_ret=*/true))
19142 fate--;
19144 if (get_bindings (TREE_VALUE (fn),
19145 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19146 NULL_TREE, /*check_ret=*/true))
19147 fate++;
19149 if (fate == -1)
19150 champ = fn;
19151 else if (!fate)
19153 /* Equally specialized, move to next function. If there
19154 is no next function, nothing's most specialized. */
19155 fn = TREE_CHAIN (fn);
19156 champ = fn;
19157 if (!fn)
19158 break;
19162 if (champ)
19163 /* Now verify that champ is better than everything earlier in the
19164 instantiation list. */
19165 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
19166 if (get_bindings (TREE_VALUE (champ),
19167 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19168 NULL_TREE, /*check_ret=*/true)
19169 || !get_bindings (TREE_VALUE (fn),
19170 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19171 NULL_TREE, /*check_ret=*/true))
19173 champ = NULL_TREE;
19174 break;
19177 processing_template_decl--;
19179 if (!champ)
19180 return error_mark_node;
19182 return champ;
19185 /* If DECL is a specialization of some template, return the most
19186 general such template. Otherwise, returns NULL_TREE.
19188 For example, given:
19190 template <class T> struct S { template <class U> void f(U); };
19192 if TMPL is `template <class U> void S<int>::f(U)' this will return
19193 the full template. This function will not trace past partial
19194 specializations, however. For example, given in addition:
19196 template <class T> struct S<T*> { template <class U> void f(U); };
19198 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19199 `template <class T> template <class U> S<T*>::f(U)'. */
19201 tree
19202 most_general_template (tree decl)
19204 if (TREE_CODE (decl) != TEMPLATE_DECL)
19206 if (tree tinfo = get_template_info (decl))
19207 decl = TI_TEMPLATE (tinfo);
19208 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19209 template friend, or a FIELD_DECL for a capture pack. */
19210 if (TREE_CODE (decl) != TEMPLATE_DECL)
19211 return NULL_TREE;
19214 /* Look for more and more general templates. */
19215 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
19217 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19218 (See cp-tree.h for details.) */
19219 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
19220 break;
19222 if (CLASS_TYPE_P (TREE_TYPE (decl))
19223 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
19224 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19225 break;
19227 /* Stop if we run into an explicitly specialized class template. */
19228 if (!DECL_NAMESPACE_SCOPE_P (decl)
19229 && DECL_CONTEXT (decl)
19230 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
19231 break;
19233 decl = DECL_TI_TEMPLATE (decl);
19236 return decl;
19239 /* Return the most specialized of the template partial specializations
19240 which can produce TARGET, a specialization of some class or variable
19241 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19242 a TEMPLATE_DECL node corresponding to the partial specialization, while
19243 the TREE_PURPOSE is the set of template arguments that must be
19244 substituted into the template pattern in order to generate TARGET.
19246 If the choice of partial specialization is ambiguous, a diagnostic
19247 is issued, and the error_mark_node is returned. If there are no
19248 partial specializations matching TARGET, then NULL_TREE is
19249 returned, indicating that the primary template should be used. */
19251 static tree
19252 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
19254 tree list = NULL_TREE;
19255 tree t;
19256 tree champ;
19257 int fate;
19258 bool ambiguous_p;
19259 tree outer_args = NULL_TREE;
19260 tree tmpl, args;
19262 if (TYPE_P (target))
19264 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
19265 tmpl = TI_TEMPLATE (tinfo);
19266 args = TI_ARGS (tinfo);
19268 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
19270 tmpl = TREE_OPERAND (target, 0);
19271 args = TREE_OPERAND (target, 1);
19273 else if (VAR_P (target))
19275 tree tinfo = DECL_TEMPLATE_INFO (target);
19276 tmpl = TI_TEMPLATE (tinfo);
19277 args = TI_ARGS (tinfo);
19279 else
19280 gcc_unreachable ();
19282 tree main_tmpl = most_general_template (tmpl);
19284 /* For determining which partial specialization to use, only the
19285 innermost args are interesting. */
19286 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19288 outer_args = strip_innermost_template_args (args, 1);
19289 args = INNERMOST_TEMPLATE_ARGS (args);
19292 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
19294 tree partial_spec_args;
19295 tree spec_args;
19296 tree spec_tmpl = TREE_VALUE (t);
19298 partial_spec_args = TREE_PURPOSE (t);
19300 ++processing_template_decl;
19302 if (outer_args)
19304 /* Discard the outer levels of args, and then substitute in the
19305 template args from the enclosing class. */
19306 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19307 partial_spec_args = tsubst_template_args
19308 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19310 /* And the same for the partial specialization TEMPLATE_DECL. */
19311 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19314 partial_spec_args =
19315 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19316 partial_spec_args,
19317 tmpl, tf_none,
19318 /*require_all_args=*/true,
19319 /*use_default_args=*/true);
19321 --processing_template_decl;
19323 if (partial_spec_args == error_mark_node)
19324 return error_mark_node;
19325 if (spec_tmpl == error_mark_node)
19326 return error_mark_node;
19328 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19329 spec_args = get_partial_spec_bindings (tmpl, parms,
19330 partial_spec_args,
19331 args);
19332 if (spec_args)
19334 if (outer_args)
19335 spec_args = add_to_template_args (outer_args, spec_args);
19336 list = tree_cons (spec_args, TREE_VALUE (t), list);
19337 TREE_TYPE (list) = TREE_TYPE (t);
19341 if (! list)
19342 return NULL_TREE;
19344 ambiguous_p = false;
19345 t = list;
19346 champ = t;
19347 t = TREE_CHAIN (t);
19348 for (; t; t = TREE_CHAIN (t))
19350 fate = more_specialized_partial_spec (tmpl, champ, t);
19351 if (fate == 1)
19353 else
19355 if (fate == 0)
19357 t = TREE_CHAIN (t);
19358 if (! t)
19360 ambiguous_p = true;
19361 break;
19364 champ = t;
19368 if (!ambiguous_p)
19369 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19371 fate = more_specialized_partial_spec (tmpl, champ, t);
19372 if (fate != 1)
19374 ambiguous_p = true;
19375 break;
19379 if (ambiguous_p)
19381 const char *str;
19382 char *spaces = NULL;
19383 if (!(complain & tf_error))
19384 return error_mark_node;
19385 if (TYPE_P (target))
19386 error ("ambiguous template instantiation for %q#T", target);
19387 else
19388 error ("ambiguous template instantiation for %q#D", target);
19389 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19390 for (t = list; t; t = TREE_CHAIN (t))
19392 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
19393 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
19394 "%s %#S", spaces ? spaces : str, subst);
19395 spaces = spaces ? spaces : get_spaces (str);
19397 free (spaces);
19398 return error_mark_node;
19401 return champ;
19404 /* Explicitly instantiate DECL. */
19406 void
19407 do_decl_instantiation (tree decl, tree storage)
19409 tree result = NULL_TREE;
19410 int extern_p = 0;
19412 if (!decl || decl == error_mark_node)
19413 /* An error occurred, for which grokdeclarator has already issued
19414 an appropriate message. */
19415 return;
19416 else if (! DECL_LANG_SPECIFIC (decl))
19418 error ("explicit instantiation of non-template %q#D", decl);
19419 return;
19422 bool var_templ = (DECL_TEMPLATE_INFO (decl)
19423 && variable_template_p (DECL_TI_TEMPLATE (decl)));
19425 if (VAR_P (decl) && !var_templ)
19427 /* There is an asymmetry here in the way VAR_DECLs and
19428 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19429 the latter, the DECL we get back will be marked as a
19430 template instantiation, and the appropriate
19431 DECL_TEMPLATE_INFO will be set up. This does not happen for
19432 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19433 should handle VAR_DECLs as it currently handles
19434 FUNCTION_DECLs. */
19435 if (!DECL_CLASS_SCOPE_P (decl))
19437 error ("%qD is not a static data member of a class template", decl);
19438 return;
19440 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19441 if (!result || !VAR_P (result))
19443 error ("no matching template for %qD found", decl);
19444 return;
19446 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19448 error ("type %qT for explicit instantiation %qD does not match "
19449 "declared type %qT", TREE_TYPE (result), decl,
19450 TREE_TYPE (decl));
19451 return;
19454 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
19456 error ("explicit instantiation of %q#D", decl);
19457 return;
19459 else
19460 result = decl;
19462 /* Check for various error cases. Note that if the explicit
19463 instantiation is valid the RESULT will currently be marked as an
19464 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19465 until we get here. */
19467 if (DECL_TEMPLATE_SPECIALIZATION (result))
19469 /* DR 259 [temp.spec].
19471 Both an explicit instantiation and a declaration of an explicit
19472 specialization shall not appear in a program unless the explicit
19473 instantiation follows a declaration of the explicit specialization.
19475 For a given set of template parameters, if an explicit
19476 instantiation of a template appears after a declaration of an
19477 explicit specialization for that template, the explicit
19478 instantiation has no effect. */
19479 return;
19481 else if (DECL_EXPLICIT_INSTANTIATION (result))
19483 /* [temp.spec]
19485 No program shall explicitly instantiate any template more
19486 than once.
19488 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19489 the first instantiation was `extern' and the second is not,
19490 and EXTERN_P for the opposite case. */
19491 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19492 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19493 /* If an "extern" explicit instantiation follows an ordinary
19494 explicit instantiation, the template is instantiated. */
19495 if (extern_p)
19496 return;
19498 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19500 error ("no matching template for %qD found", result);
19501 return;
19503 else if (!DECL_TEMPLATE_INFO (result))
19505 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19506 return;
19509 if (storage == NULL_TREE)
19511 else if (storage == ridpointers[(int) RID_EXTERN])
19513 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19514 pedwarn (input_location, OPT_Wpedantic,
19515 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19516 "instantiations");
19517 extern_p = 1;
19519 else
19520 error ("storage class %qD applied to template instantiation", storage);
19522 check_explicit_instantiation_namespace (result);
19523 mark_decl_instantiated (result, extern_p);
19524 if (! extern_p)
19525 instantiate_decl (result, /*defer_ok=*/1,
19526 /*expl_inst_class_mem_p=*/false);
19529 static void
19530 mark_class_instantiated (tree t, int extern_p)
19532 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19533 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19534 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19535 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19536 if (! extern_p)
19538 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19539 rest_of_type_compilation (t, 1);
19543 /* Called from do_type_instantiation through binding_table_foreach to
19544 do recursive instantiation for the type bound in ENTRY. */
19545 static void
19546 bt_instantiate_type_proc (binding_entry entry, void *data)
19548 tree storage = *(tree *) data;
19550 if (MAYBE_CLASS_TYPE_P (entry->type)
19551 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19552 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19555 /* Called from do_type_instantiation to instantiate a member
19556 (a member function or a static member variable) of an
19557 explicitly instantiated class template. */
19558 static void
19559 instantiate_class_member (tree decl, int extern_p)
19561 mark_decl_instantiated (decl, extern_p);
19562 if (! extern_p)
19563 instantiate_decl (decl, /*defer_ok=*/1,
19564 /*expl_inst_class_mem_p=*/true);
19567 /* Perform an explicit instantiation of template class T. STORAGE, if
19568 non-null, is the RID for extern, inline or static. COMPLAIN is
19569 nonzero if this is called from the parser, zero if called recursively,
19570 since the standard is unclear (as detailed below). */
19572 void
19573 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19575 int extern_p = 0;
19576 int nomem_p = 0;
19577 int static_p = 0;
19578 int previous_instantiation_extern_p = 0;
19580 if (TREE_CODE (t) == TYPE_DECL)
19581 t = TREE_TYPE (t);
19583 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19585 tree tmpl =
19586 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19587 if (tmpl)
19588 error ("explicit instantiation of non-class template %qD", tmpl);
19589 else
19590 error ("explicit instantiation of non-template type %qT", t);
19591 return;
19594 complete_type (t);
19596 if (!COMPLETE_TYPE_P (t))
19598 if (complain & tf_error)
19599 error ("explicit instantiation of %q#T before definition of template",
19601 return;
19604 if (storage != NULL_TREE)
19606 if (!in_system_header_at (input_location))
19608 if (storage == ridpointers[(int) RID_EXTERN])
19610 if (cxx_dialect == cxx98)
19611 pedwarn (input_location, OPT_Wpedantic,
19612 "ISO C++ 1998 forbids the use of %<extern%> on "
19613 "explicit instantiations");
19615 else
19616 pedwarn (input_location, OPT_Wpedantic,
19617 "ISO C++ forbids the use of %qE"
19618 " on explicit instantiations", storage);
19621 if (storage == ridpointers[(int) RID_INLINE])
19622 nomem_p = 1;
19623 else if (storage == ridpointers[(int) RID_EXTERN])
19624 extern_p = 1;
19625 else if (storage == ridpointers[(int) RID_STATIC])
19626 static_p = 1;
19627 else
19629 error ("storage class %qD applied to template instantiation",
19630 storage);
19631 extern_p = 0;
19635 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19637 /* DR 259 [temp.spec].
19639 Both an explicit instantiation and a declaration of an explicit
19640 specialization shall not appear in a program unless the explicit
19641 instantiation follows a declaration of the explicit specialization.
19643 For a given set of template parameters, if an explicit
19644 instantiation of a template appears after a declaration of an
19645 explicit specialization for that template, the explicit
19646 instantiation has no effect. */
19647 return;
19649 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19651 /* [temp.spec]
19653 No program shall explicitly instantiate any template more
19654 than once.
19656 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19657 instantiation was `extern'. If EXTERN_P then the second is.
19658 These cases are OK. */
19659 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19661 if (!previous_instantiation_extern_p && !extern_p
19662 && (complain & tf_error))
19663 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19665 /* If we've already instantiated the template, just return now. */
19666 if (!CLASSTYPE_INTERFACE_ONLY (t))
19667 return;
19670 check_explicit_instantiation_namespace (TYPE_NAME (t));
19671 mark_class_instantiated (t, extern_p);
19673 if (nomem_p)
19674 return;
19677 tree tmp;
19679 /* In contrast to implicit instantiation, where only the
19680 declarations, and not the definitions, of members are
19681 instantiated, we have here:
19683 [temp.explicit]
19685 The explicit instantiation of a class template specialization
19686 implies the instantiation of all of its members not
19687 previously explicitly specialized in the translation unit
19688 containing the explicit instantiation.
19690 Of course, we can't instantiate member template classes, since
19691 we don't have any arguments for them. Note that the standard
19692 is unclear on whether the instantiation of the members are
19693 *explicit* instantiations or not. However, the most natural
19694 interpretation is that it should be an explicit instantiation. */
19696 if (! static_p)
19697 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19698 if (TREE_CODE (tmp) == FUNCTION_DECL
19699 && DECL_TEMPLATE_INSTANTIATION (tmp))
19700 instantiate_class_member (tmp, extern_p);
19702 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19703 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19704 instantiate_class_member (tmp, extern_p);
19706 if (CLASSTYPE_NESTED_UTDS (t))
19707 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19708 bt_instantiate_type_proc, &storage);
19712 /* Given a function DECL, which is a specialization of TMPL, modify
19713 DECL to be a re-instantiation of TMPL with the same template
19714 arguments. TMPL should be the template into which tsubst'ing
19715 should occur for DECL, not the most general template.
19717 One reason for doing this is a scenario like this:
19719 template <class T>
19720 void f(const T&, int i);
19722 void g() { f(3, 7); }
19724 template <class T>
19725 void f(const T& t, const int i) { }
19727 Note that when the template is first instantiated, with
19728 instantiate_template, the resulting DECL will have no name for the
19729 first parameter, and the wrong type for the second. So, when we go
19730 to instantiate the DECL, we regenerate it. */
19732 static void
19733 regenerate_decl_from_template (tree decl, tree tmpl)
19735 /* The arguments used to instantiate DECL, from the most general
19736 template. */
19737 tree args;
19738 tree code_pattern;
19740 args = DECL_TI_ARGS (decl);
19741 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19743 /* Make sure that we can see identifiers, and compute access
19744 correctly. */
19745 push_access_scope (decl);
19747 if (TREE_CODE (decl) == FUNCTION_DECL)
19749 tree decl_parm;
19750 tree pattern_parm;
19751 tree specs;
19752 int args_depth;
19753 int parms_depth;
19755 args_depth = TMPL_ARGS_DEPTH (args);
19756 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19757 if (args_depth > parms_depth)
19758 args = get_innermost_template_args (args, parms_depth);
19760 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19761 args, tf_error, NULL_TREE,
19762 /*defer_ok*/false);
19763 if (specs && specs != error_mark_node)
19764 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19765 specs);
19767 /* Merge parameter declarations. */
19768 decl_parm = skip_artificial_parms_for (decl,
19769 DECL_ARGUMENTS (decl));
19770 pattern_parm
19771 = skip_artificial_parms_for (code_pattern,
19772 DECL_ARGUMENTS (code_pattern));
19773 while (decl_parm && !DECL_PACK_P (pattern_parm))
19775 tree parm_type;
19776 tree attributes;
19778 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19779 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19780 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19781 NULL_TREE);
19782 parm_type = type_decays_to (parm_type);
19783 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19784 TREE_TYPE (decl_parm) = parm_type;
19785 attributes = DECL_ATTRIBUTES (pattern_parm);
19786 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19788 DECL_ATTRIBUTES (decl_parm) = attributes;
19789 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19791 decl_parm = DECL_CHAIN (decl_parm);
19792 pattern_parm = DECL_CHAIN (pattern_parm);
19794 /* Merge any parameters that match with the function parameter
19795 pack. */
19796 if (pattern_parm && DECL_PACK_P (pattern_parm))
19798 int i, len;
19799 tree expanded_types;
19800 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19801 the parameters in this function parameter pack. */
19802 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19803 args, tf_error, NULL_TREE);
19804 len = TREE_VEC_LENGTH (expanded_types);
19805 for (i = 0; i < len; i++)
19807 tree parm_type;
19808 tree attributes;
19810 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19811 /* Rename the parameter to include the index. */
19812 DECL_NAME (decl_parm) =
19813 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19814 parm_type = TREE_VEC_ELT (expanded_types, i);
19815 parm_type = type_decays_to (parm_type);
19816 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19817 TREE_TYPE (decl_parm) = parm_type;
19818 attributes = DECL_ATTRIBUTES (pattern_parm);
19819 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19821 DECL_ATTRIBUTES (decl_parm) = attributes;
19822 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19824 decl_parm = DECL_CHAIN (decl_parm);
19827 /* Merge additional specifiers from the CODE_PATTERN. */
19828 if (DECL_DECLARED_INLINE_P (code_pattern)
19829 && !DECL_DECLARED_INLINE_P (decl))
19830 DECL_DECLARED_INLINE_P (decl) = 1;
19832 else if (VAR_P (decl))
19834 DECL_INITIAL (decl) =
19835 tsubst_expr (DECL_INITIAL (code_pattern), args,
19836 tf_error, DECL_TI_TEMPLATE (decl),
19837 /*integral_constant_expression_p=*/false);
19838 if (VAR_HAD_UNKNOWN_BOUND (decl))
19839 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19840 tf_error, DECL_TI_TEMPLATE (decl));
19842 else
19843 gcc_unreachable ();
19845 pop_access_scope (decl);
19848 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19849 substituted to get DECL. */
19851 tree
19852 template_for_substitution (tree decl)
19854 tree tmpl = DECL_TI_TEMPLATE (decl);
19856 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19857 for the instantiation. This is not always the most general
19858 template. Consider, for example:
19860 template <class T>
19861 struct S { template <class U> void f();
19862 template <> void f<int>(); };
19864 and an instantiation of S<double>::f<int>. We want TD to be the
19865 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19866 while (/* An instantiation cannot have a definition, so we need a
19867 more general template. */
19868 DECL_TEMPLATE_INSTANTIATION (tmpl)
19869 /* We must also deal with friend templates. Given:
19871 template <class T> struct S {
19872 template <class U> friend void f() {};
19875 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19876 so far as the language is concerned, but that's still
19877 where we get the pattern for the instantiation from. On
19878 other hand, if the definition comes outside the class, say:
19880 template <class T> struct S {
19881 template <class U> friend void f();
19883 template <class U> friend void f() {}
19885 we don't need to look any further. That's what the check for
19886 DECL_INITIAL is for. */
19887 || (TREE_CODE (decl) == FUNCTION_DECL
19888 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19889 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19891 /* The present template, TD, should not be a definition. If it
19892 were a definition, we should be using it! Note that we
19893 cannot restructure the loop to just keep going until we find
19894 a template with a definition, since that might go too far if
19895 a specialization was declared, but not defined. */
19897 /* Fetch the more general template. */
19898 tmpl = DECL_TI_TEMPLATE (tmpl);
19901 return tmpl;
19904 /* Returns true if we need to instantiate this template instance even if we
19905 know we aren't going to emit it.. */
19907 bool
19908 always_instantiate_p (tree decl)
19910 /* We always instantiate inline functions so that we can inline them. An
19911 explicit instantiation declaration prohibits implicit instantiation of
19912 non-inline functions. With high levels of optimization, we would
19913 normally inline non-inline functions -- but we're not allowed to do
19914 that for "extern template" functions. Therefore, we check
19915 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19916 return ((TREE_CODE (decl) == FUNCTION_DECL
19917 && (DECL_DECLARED_INLINE_P (decl)
19918 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19919 /* And we need to instantiate static data members so that
19920 their initializers are available in integral constant
19921 expressions. */
19922 || (VAR_P (decl)
19923 && decl_maybe_constant_var_p (decl)));
19926 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19927 instantiate it now, modifying TREE_TYPE (fn). */
19929 void
19930 maybe_instantiate_noexcept (tree fn)
19932 tree fntype, spec, noex, clone;
19934 /* Don't instantiate a noexcept-specification from template context. */
19935 if (processing_template_decl)
19936 return;
19938 if (DECL_CLONED_FUNCTION_P (fn))
19939 fn = DECL_CLONED_FUNCTION (fn);
19940 fntype = TREE_TYPE (fn);
19941 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19943 if (!spec || !TREE_PURPOSE (spec))
19944 return;
19946 noex = TREE_PURPOSE (spec);
19948 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19950 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
19951 spec = get_defaulted_eh_spec (fn);
19952 else if (push_tinst_level (fn))
19954 push_access_scope (fn);
19955 push_deferring_access_checks (dk_no_deferred);
19956 input_location = DECL_SOURCE_LOCATION (fn);
19957 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19958 DEFERRED_NOEXCEPT_ARGS (noex),
19959 tf_warning_or_error, fn,
19960 /*function_p=*/false,
19961 /*integral_constant_expression_p=*/true);
19962 pop_deferring_access_checks ();
19963 pop_access_scope (fn);
19964 pop_tinst_level ();
19965 spec = build_noexcept_spec (noex, tf_warning_or_error);
19966 if (spec == error_mark_node)
19967 spec = noexcept_false_spec;
19969 else
19970 spec = noexcept_false_spec;
19972 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19975 FOR_EACH_CLONE (clone, fn)
19977 if (TREE_TYPE (clone) == fntype)
19978 TREE_TYPE (clone) = TREE_TYPE (fn);
19979 else
19980 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19984 /* Produce the definition of D, a _DECL generated from a template. If
19985 DEFER_OK is nonzero, then we don't have to actually do the
19986 instantiation now; we just have to do it sometime. Normally it is
19987 an error if this is an explicit instantiation but D is undefined.
19988 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19989 explicitly instantiated class template. */
19991 tree
19992 instantiate_decl (tree d, int defer_ok,
19993 bool expl_inst_class_mem_p)
19995 tree tmpl = DECL_TI_TEMPLATE (d);
19996 tree gen_args;
19997 tree args;
19998 tree td;
19999 tree code_pattern;
20000 tree spec;
20001 tree gen_tmpl;
20002 bool pattern_defined;
20003 location_t saved_loc = input_location;
20004 int saved_unevaluated_operand = cp_unevaluated_operand;
20005 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20006 bool external_p;
20007 bool deleted_p;
20008 tree fn_context;
20009 bool nested;
20011 /* This function should only be used to instantiate templates for
20012 functions and static member variables. */
20013 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
20015 /* Variables are never deferred; if instantiation is required, they
20016 are instantiated right away. That allows for better code in the
20017 case that an expression refers to the value of the variable --
20018 if the variable has a constant value the referring expression can
20019 take advantage of that fact. */
20020 if (VAR_P (d)
20021 || DECL_DECLARED_CONSTEXPR_P (d))
20022 defer_ok = 0;
20024 /* Don't instantiate cloned functions. Instead, instantiate the
20025 functions they cloned. */
20026 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
20027 d = DECL_CLONED_FUNCTION (d);
20029 if (DECL_TEMPLATE_INSTANTIATED (d)
20030 || (TREE_CODE (d) == FUNCTION_DECL
20031 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
20032 || DECL_TEMPLATE_SPECIALIZATION (d))
20033 /* D has already been instantiated or explicitly specialized, so
20034 there's nothing for us to do here.
20036 It might seem reasonable to check whether or not D is an explicit
20037 instantiation, and, if so, stop here. But when an explicit
20038 instantiation is deferred until the end of the compilation,
20039 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20040 the instantiation. */
20041 return d;
20043 /* Check to see whether we know that this template will be
20044 instantiated in some other file, as with "extern template"
20045 extension. */
20046 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
20048 /* In general, we do not instantiate such templates. */
20049 if (external_p && !always_instantiate_p (d))
20050 return d;
20052 gen_tmpl = most_general_template (tmpl);
20053 gen_args = DECL_TI_ARGS (d);
20055 if (tmpl != gen_tmpl)
20056 /* We should already have the extra args. */
20057 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
20058 == TMPL_ARGS_DEPTH (gen_args));
20059 /* And what's in the hash table should match D. */
20060 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
20061 || spec == NULL_TREE);
20063 /* This needs to happen before any tsubsting. */
20064 if (! push_tinst_level (d))
20065 return d;
20067 timevar_push (TV_TEMPLATE_INST);
20069 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20070 for the instantiation. */
20071 td = template_for_substitution (d);
20072 code_pattern = DECL_TEMPLATE_RESULT (td);
20074 /* We should never be trying to instantiate a member of a class
20075 template or partial specialization. */
20076 gcc_assert (d != code_pattern);
20078 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
20079 || DECL_TEMPLATE_SPECIALIZATION (td))
20080 /* In the case of a friend template whose definition is provided
20081 outside the class, we may have too many arguments. Drop the
20082 ones we don't need. The same is true for specializations. */
20083 args = get_innermost_template_args
20084 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
20085 else
20086 args = gen_args;
20088 if (TREE_CODE (d) == FUNCTION_DECL)
20090 deleted_p = DECL_DELETED_FN (code_pattern);
20091 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
20092 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
20093 || deleted_p);
20095 else
20097 deleted_p = false;
20098 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
20101 /* We may be in the middle of deferred access check. Disable it now. */
20102 push_deferring_access_checks (dk_no_deferred);
20104 /* Unless an explicit instantiation directive has already determined
20105 the linkage of D, remember that a definition is available for
20106 this entity. */
20107 if (pattern_defined
20108 && !DECL_INTERFACE_KNOWN (d)
20109 && !DECL_NOT_REALLY_EXTERN (d))
20110 mark_definable (d);
20112 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
20113 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
20114 input_location = DECL_SOURCE_LOCATION (d);
20116 /* If D is a member of an explicitly instantiated class template,
20117 and no definition is available, treat it like an implicit
20118 instantiation. */
20119 if (!pattern_defined && expl_inst_class_mem_p
20120 && DECL_EXPLICIT_INSTANTIATION (d))
20122 /* Leave linkage flags alone on instantiations with anonymous
20123 visibility. */
20124 if (TREE_PUBLIC (d))
20126 DECL_NOT_REALLY_EXTERN (d) = 0;
20127 DECL_INTERFACE_KNOWN (d) = 0;
20129 SET_DECL_IMPLICIT_INSTANTIATION (d);
20132 /* Defer all other templates, unless we have been explicitly
20133 forbidden from doing so. */
20134 if (/* If there is no definition, we cannot instantiate the
20135 template. */
20136 ! pattern_defined
20137 /* If it's OK to postpone instantiation, do so. */
20138 || defer_ok
20139 /* If this is a static data member that will be defined
20140 elsewhere, we don't want to instantiate the entire data
20141 member, but we do want to instantiate the initializer so that
20142 we can substitute that elsewhere. */
20143 || (external_p && VAR_P (d))
20144 /* Handle here a deleted function too, avoid generating
20145 its body (c++/61080). */
20146 || deleted_p)
20148 /* The definition of the static data member is now required so
20149 we must substitute the initializer. */
20150 if (VAR_P (d)
20151 && !DECL_INITIAL (d)
20152 && DECL_INITIAL (code_pattern))
20154 tree ns;
20155 tree init;
20156 bool const_init = false;
20157 bool enter_context = DECL_CLASS_SCOPE_P (d);
20159 ns = decl_namespace_context (d);
20160 push_nested_namespace (ns);
20161 if (enter_context)
20162 push_nested_class (DECL_CONTEXT (d));
20163 init = tsubst_expr (DECL_INITIAL (code_pattern),
20164 args,
20165 tf_warning_or_error, NULL_TREE,
20166 /*integral_constant_expression_p=*/false);
20167 /* If instantiating the initializer involved instantiating this
20168 again, don't call cp_finish_decl twice. */
20169 if (!DECL_INITIAL (d))
20171 /* Make sure the initializer is still constant, in case of
20172 circular dependency (template/instantiate6.C). */
20173 const_init
20174 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20175 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
20176 /*asmspec_tree=*/NULL_TREE,
20177 LOOKUP_ONLYCONVERTING);
20179 if (enter_context)
20180 pop_nested_class ();
20181 pop_nested_namespace (ns);
20184 /* We restore the source position here because it's used by
20185 add_pending_template. */
20186 input_location = saved_loc;
20188 if (at_eof && !pattern_defined
20189 && DECL_EXPLICIT_INSTANTIATION (d)
20190 && DECL_NOT_REALLY_EXTERN (d))
20191 /* [temp.explicit]
20193 The definition of a non-exported function template, a
20194 non-exported member function template, or a non-exported
20195 member function or static data member of a class template
20196 shall be present in every translation unit in which it is
20197 explicitly instantiated. */
20198 permerror (input_location, "explicit instantiation of %qD "
20199 "but no definition available", d);
20201 /* If we're in unevaluated context, we just wanted to get the
20202 constant value; this isn't an odr use, so don't queue
20203 a full instantiation. */
20204 if (cp_unevaluated_operand != 0)
20205 goto out;
20206 /* ??? Historically, we have instantiated inline functions, even
20207 when marked as "extern template". */
20208 if (!(external_p && VAR_P (d)))
20209 add_pending_template (d);
20210 goto out;
20212 /* Tell the repository that D is available in this translation unit
20213 -- and see if it is supposed to be instantiated here. */
20214 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
20216 /* In a PCH file, despite the fact that the repository hasn't
20217 requested instantiation in the PCH it is still possible that
20218 an instantiation will be required in a file that includes the
20219 PCH. */
20220 if (pch_file)
20221 add_pending_template (d);
20222 /* Instantiate inline functions so that the inliner can do its
20223 job, even though we'll not be emitting a copy of this
20224 function. */
20225 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
20226 goto out;
20229 fn_context = decl_function_context (d);
20230 nested = (current_function_decl != NULL_TREE);
20231 if (!fn_context)
20232 push_to_top_level ();
20233 else
20235 if (nested)
20236 push_function_context ();
20237 cp_unevaluated_operand = 0;
20238 c_inhibit_evaluation_warnings = 0;
20241 /* Mark D as instantiated so that recursive calls to
20242 instantiate_decl do not try to instantiate it again. */
20243 DECL_TEMPLATE_INSTANTIATED (d) = 1;
20245 /* Regenerate the declaration in case the template has been modified
20246 by a subsequent redeclaration. */
20247 regenerate_decl_from_template (d, td);
20249 /* We already set the file and line above. Reset them now in case
20250 they changed as a result of calling regenerate_decl_from_template. */
20251 input_location = DECL_SOURCE_LOCATION (d);
20253 if (VAR_P (d))
20255 tree init;
20256 bool const_init = false;
20258 /* Clear out DECL_RTL; whatever was there before may not be right
20259 since we've reset the type of the declaration. */
20260 SET_DECL_RTL (d, NULL);
20261 DECL_IN_AGGR_P (d) = 0;
20263 /* The initializer is placed in DECL_INITIAL by
20264 regenerate_decl_from_template so we don't need to
20265 push/pop_access_scope again here. Pull it out so that
20266 cp_finish_decl can process it. */
20267 init = DECL_INITIAL (d);
20268 DECL_INITIAL (d) = NULL_TREE;
20269 DECL_INITIALIZED_P (d) = 0;
20271 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20272 initializer. That function will defer actual emission until
20273 we have a chance to determine linkage. */
20274 DECL_EXTERNAL (d) = 0;
20276 /* Enter the scope of D so that access-checking works correctly. */
20277 bool enter_context = DECL_CLASS_SCOPE_P (d);
20278 if (enter_context)
20279 push_nested_class (DECL_CONTEXT (d));
20281 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20282 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
20284 if (enter_context)
20285 pop_nested_class ();
20287 if (variable_template_p (td))
20288 note_variable_template_instantiation (d);
20290 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
20291 synthesize_method (d);
20292 else if (TREE_CODE (d) == FUNCTION_DECL)
20294 hash_map<tree, tree> *saved_local_specializations;
20295 tree subst_decl;
20296 tree tmpl_parm;
20297 tree spec_parm;
20298 tree block = NULL_TREE;
20300 /* Save away the current list, in case we are instantiating one
20301 template from within the body of another. */
20302 saved_local_specializations = local_specializations;
20304 /* Set up the list of local specializations. */
20305 local_specializations = new hash_map<tree, tree>;
20307 /* Set up context. */
20308 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20309 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20310 block = push_stmt_list ();
20311 else
20312 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
20314 /* Some typedefs referenced from within the template code need to be
20315 access checked at template instantiation time, i.e now. These
20316 types were added to the template at parsing time. Let's get those
20317 and perform the access checks then. */
20318 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
20319 gen_args);
20321 /* Create substitution entries for the parameters. */
20322 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
20323 tmpl_parm = DECL_ARGUMENTS (subst_decl);
20324 spec_parm = DECL_ARGUMENTS (d);
20325 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
20327 register_local_specialization (spec_parm, tmpl_parm);
20328 spec_parm = skip_artificial_parms_for (d, spec_parm);
20329 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
20331 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
20333 if (!DECL_PACK_P (tmpl_parm))
20335 register_local_specialization (spec_parm, tmpl_parm);
20336 spec_parm = DECL_CHAIN (spec_parm);
20338 else
20340 /* Register the (value) argument pack as a specialization of
20341 TMPL_PARM, then move on. */
20342 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
20343 register_local_specialization (argpack, tmpl_parm);
20346 gcc_assert (!spec_parm);
20348 /* Substitute into the body of the function. */
20349 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20350 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
20351 tf_warning_or_error, tmpl);
20352 else
20354 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
20355 tf_warning_or_error, tmpl,
20356 /*integral_constant_expression_p=*/false);
20358 /* Set the current input_location to the end of the function
20359 so that finish_function knows where we are. */
20360 input_location
20361 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
20363 /* Remember if we saw an infinite loop in the template. */
20364 current_function_infinite_loop
20365 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
20368 /* We don't need the local specializations any more. */
20369 delete local_specializations;
20370 local_specializations = saved_local_specializations;
20372 /* Finish the function. */
20373 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20374 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20375 DECL_SAVED_TREE (d) = pop_stmt_list (block);
20376 else
20378 d = finish_function (0);
20379 expand_or_defer_fn (d);
20382 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20383 cp_check_omp_declare_reduction (d);
20386 /* We're not deferring instantiation any more. */
20387 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20389 if (!fn_context)
20390 pop_from_top_level ();
20391 else if (nested)
20392 pop_function_context ();
20394 out:
20395 input_location = saved_loc;
20396 cp_unevaluated_operand = saved_unevaluated_operand;
20397 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20398 pop_deferring_access_checks ();
20399 pop_tinst_level ();
20401 timevar_pop (TV_TEMPLATE_INST);
20403 return d;
20406 /* Run through the list of templates that we wish we could
20407 instantiate, and instantiate any we can. RETRIES is the
20408 number of times we retry pending template instantiation. */
20410 void
20411 instantiate_pending_templates (int retries)
20413 int reconsider;
20414 location_t saved_loc = input_location;
20416 /* Instantiating templates may trigger vtable generation. This in turn
20417 may require further template instantiations. We place a limit here
20418 to avoid infinite loop. */
20419 if (pending_templates && retries >= max_tinst_depth)
20421 tree decl = pending_templates->tinst->decl;
20423 fatal_error ("template instantiation depth exceeds maximum of %d"
20424 " instantiating %q+D, possibly from virtual table generation"
20425 " (use -ftemplate-depth= to increase the maximum)",
20426 max_tinst_depth, decl);
20427 if (TREE_CODE (decl) == FUNCTION_DECL)
20428 /* Pretend that we defined it. */
20429 DECL_INITIAL (decl) = error_mark_node;
20430 return;
20435 struct pending_template **t = &pending_templates;
20436 struct pending_template *last = NULL;
20437 reconsider = 0;
20438 while (*t)
20440 tree instantiation = reopen_tinst_level ((*t)->tinst);
20441 bool complete = false;
20443 if (TYPE_P (instantiation))
20445 tree fn;
20447 if (!COMPLETE_TYPE_P (instantiation))
20449 instantiate_class_template (instantiation);
20450 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20451 for (fn = TYPE_METHODS (instantiation);
20453 fn = TREE_CHAIN (fn))
20454 if (! DECL_ARTIFICIAL (fn))
20455 instantiate_decl (fn,
20456 /*defer_ok=*/0,
20457 /*expl_inst_class_mem_p=*/false);
20458 if (COMPLETE_TYPE_P (instantiation))
20459 reconsider = 1;
20462 complete = COMPLETE_TYPE_P (instantiation);
20464 else
20466 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20467 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20469 instantiation
20470 = instantiate_decl (instantiation,
20471 /*defer_ok=*/0,
20472 /*expl_inst_class_mem_p=*/false);
20473 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20474 reconsider = 1;
20477 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20478 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20481 if (complete)
20482 /* If INSTANTIATION has been instantiated, then we don't
20483 need to consider it again in the future. */
20484 *t = (*t)->next;
20485 else
20487 last = *t;
20488 t = &(*t)->next;
20490 tinst_depth = 0;
20491 current_tinst_level = NULL;
20493 last_pending_template = last;
20495 while (reconsider);
20497 input_location = saved_loc;
20500 /* Substitute ARGVEC into T, which is a list of initializers for
20501 either base class or a non-static data member. The TREE_PURPOSEs
20502 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20503 instantiate_decl. */
20505 static tree
20506 tsubst_initializer_list (tree t, tree argvec)
20508 tree inits = NULL_TREE;
20510 for (; t; t = TREE_CHAIN (t))
20512 tree decl;
20513 tree init;
20514 tree expanded_bases = NULL_TREE;
20515 tree expanded_arguments = NULL_TREE;
20516 int i, len = 1;
20518 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20520 tree expr;
20521 tree arg;
20523 /* Expand the base class expansion type into separate base
20524 classes. */
20525 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20526 tf_warning_or_error,
20527 NULL_TREE);
20528 if (expanded_bases == error_mark_node)
20529 continue;
20531 /* We'll be building separate TREE_LISTs of arguments for
20532 each base. */
20533 len = TREE_VEC_LENGTH (expanded_bases);
20534 expanded_arguments = make_tree_vec (len);
20535 for (i = 0; i < len; i++)
20536 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20538 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20539 expand each argument in the TREE_VALUE of t. */
20540 expr = make_node (EXPR_PACK_EXPANSION);
20541 PACK_EXPANSION_LOCAL_P (expr) = true;
20542 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20543 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20545 if (TREE_VALUE (t) == void_type_node)
20546 /* VOID_TYPE_NODE is used to indicate
20547 value-initialization. */
20549 for (i = 0; i < len; i++)
20550 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20552 else
20554 /* Substitute parameter packs into each argument in the
20555 TREE_LIST. */
20556 in_base_initializer = 1;
20557 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20559 tree expanded_exprs;
20561 /* Expand the argument. */
20562 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20563 expanded_exprs
20564 = tsubst_pack_expansion (expr, argvec,
20565 tf_warning_or_error,
20566 NULL_TREE);
20567 if (expanded_exprs == error_mark_node)
20568 continue;
20570 /* Prepend each of the expanded expressions to the
20571 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20572 for (i = 0; i < len; i++)
20574 TREE_VEC_ELT (expanded_arguments, i) =
20575 tree_cons (NULL_TREE,
20576 TREE_VEC_ELT (expanded_exprs, i),
20577 TREE_VEC_ELT (expanded_arguments, i));
20580 in_base_initializer = 0;
20582 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20583 since we built them backwards. */
20584 for (i = 0; i < len; i++)
20586 TREE_VEC_ELT (expanded_arguments, i) =
20587 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20592 for (i = 0; i < len; ++i)
20594 if (expanded_bases)
20596 decl = TREE_VEC_ELT (expanded_bases, i);
20597 decl = expand_member_init (decl);
20598 init = TREE_VEC_ELT (expanded_arguments, i);
20600 else
20602 tree tmp;
20603 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20604 tf_warning_or_error, NULL_TREE);
20606 decl = expand_member_init (decl);
20607 if (decl && !DECL_P (decl))
20608 in_base_initializer = 1;
20610 init = TREE_VALUE (t);
20611 tmp = init;
20612 if (init != void_type_node)
20613 init = tsubst_expr (init, argvec,
20614 tf_warning_or_error, NULL_TREE,
20615 /*integral_constant_expression_p=*/false);
20616 if (init == NULL_TREE && tmp != NULL_TREE)
20617 /* If we had an initializer but it instantiated to nothing,
20618 value-initialize the object. This will only occur when
20619 the initializer was a pack expansion where the parameter
20620 packs used in that expansion were of length zero. */
20621 init = void_type_node;
20622 in_base_initializer = 0;
20625 if (decl)
20627 init = build_tree_list (decl, init);
20628 TREE_CHAIN (init) = inits;
20629 inits = init;
20633 return inits;
20636 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20638 static void
20639 set_current_access_from_decl (tree decl)
20641 if (TREE_PRIVATE (decl))
20642 current_access_specifier = access_private_node;
20643 else if (TREE_PROTECTED (decl))
20644 current_access_specifier = access_protected_node;
20645 else
20646 current_access_specifier = access_public_node;
20649 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20650 is the instantiation (which should have been created with
20651 start_enum) and ARGS are the template arguments to use. */
20653 static void
20654 tsubst_enum (tree tag, tree newtag, tree args)
20656 tree e;
20658 if (SCOPED_ENUM_P (newtag))
20659 begin_scope (sk_scoped_enum, newtag);
20661 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20663 tree value;
20664 tree decl;
20666 decl = TREE_VALUE (e);
20667 /* Note that in a template enum, the TREE_VALUE is the
20668 CONST_DECL, not the corresponding INTEGER_CST. */
20669 value = tsubst_expr (DECL_INITIAL (decl),
20670 args, tf_warning_or_error, NULL_TREE,
20671 /*integral_constant_expression_p=*/true);
20673 /* Give this enumeration constant the correct access. */
20674 set_current_access_from_decl (decl);
20676 /* Actually build the enumerator itself. */
20677 build_enumerator
20678 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20681 if (SCOPED_ENUM_P (newtag))
20682 finish_scope ();
20684 finish_enum_value_list (newtag);
20685 finish_enum (newtag);
20687 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20688 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20691 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20692 its type -- but without substituting the innermost set of template
20693 arguments. So, innermost set of template parameters will appear in
20694 the type. */
20696 tree
20697 get_mostly_instantiated_function_type (tree decl)
20699 tree fn_type;
20700 tree tmpl;
20701 tree targs;
20702 tree tparms;
20703 int parm_depth;
20705 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20706 targs = DECL_TI_ARGS (decl);
20707 tparms = DECL_TEMPLATE_PARMS (tmpl);
20708 parm_depth = TMPL_PARMS_DEPTH (tparms);
20710 /* There should be as many levels of arguments as there are levels
20711 of parameters. */
20712 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20714 fn_type = TREE_TYPE (tmpl);
20716 if (parm_depth == 1)
20717 /* No substitution is necessary. */
20719 else
20721 int i;
20722 tree partial_args;
20724 /* Replace the innermost level of the TARGS with NULL_TREEs to
20725 let tsubst know not to substitute for those parameters. */
20726 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20727 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20728 SET_TMPL_ARGS_LEVEL (partial_args, i,
20729 TMPL_ARGS_LEVEL (targs, i));
20730 SET_TMPL_ARGS_LEVEL (partial_args,
20731 TMPL_ARGS_DEPTH (targs),
20732 make_tree_vec (DECL_NTPARMS (tmpl)));
20734 /* Make sure that we can see identifiers, and compute access
20735 correctly. */
20736 push_access_scope (decl);
20738 ++processing_template_decl;
20739 /* Now, do the (partial) substitution to figure out the
20740 appropriate function type. */
20741 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20742 --processing_template_decl;
20744 /* Substitute into the template parameters to obtain the real
20745 innermost set of parameters. This step is important if the
20746 innermost set of template parameters contains value
20747 parameters whose types depend on outer template parameters. */
20748 TREE_VEC_LENGTH (partial_args)--;
20749 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20751 pop_access_scope (decl);
20754 return fn_type;
20757 /* Return truthvalue if we're processing a template different from
20758 the last one involved in diagnostics. */
20759 bool
20760 problematic_instantiation_changed (void)
20762 return current_tinst_level != last_error_tinst_level;
20765 /* Remember current template involved in diagnostics. */
20766 void
20767 record_last_problematic_instantiation (void)
20769 last_error_tinst_level = current_tinst_level;
20772 struct tinst_level *
20773 current_instantiation (void)
20775 return current_tinst_level;
20778 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20779 type. Return zero for ok, nonzero for disallowed. Issue error and
20780 warning messages under control of COMPLAIN. */
20782 static int
20783 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20785 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20786 return 0;
20787 else if (POINTER_TYPE_P (type))
20788 return 0;
20789 else if (TYPE_PTRMEM_P (type))
20790 return 0;
20791 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20792 return 0;
20793 else if (TREE_CODE (type) == TYPENAME_TYPE)
20794 return 0;
20795 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20796 return 0;
20797 else if (TREE_CODE (type) == NULLPTR_TYPE)
20798 return 0;
20800 if (complain & tf_error)
20802 if (type == error_mark_node)
20803 inform (input_location, "invalid template non-type parameter");
20804 else
20805 error ("%q#T is not a valid type for a template non-type parameter",
20806 type);
20808 return 1;
20811 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20812 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20814 static bool
20815 dependent_type_p_r (tree type)
20817 tree scope;
20819 /* [temp.dep.type]
20821 A type is dependent if it is:
20823 -- a template parameter. Template template parameters are types
20824 for us (since TYPE_P holds true for them) so we handle
20825 them here. */
20826 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20827 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20828 return true;
20829 /* -- a qualified-id with a nested-name-specifier which contains a
20830 class-name that names a dependent type or whose unqualified-id
20831 names a dependent type. */
20832 if (TREE_CODE (type) == TYPENAME_TYPE)
20833 return true;
20834 /* -- a cv-qualified type where the cv-unqualified type is
20835 dependent.
20836 No code is necessary for this bullet; the code below handles
20837 cv-qualified types, and we don't want to strip aliases with
20838 TYPE_MAIN_VARIANT because of DR 1558. */
20839 /* -- a compound type constructed from any dependent type. */
20840 if (TYPE_PTRMEM_P (type))
20841 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20842 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20843 (type)));
20844 else if (TYPE_PTR_P (type)
20845 || TREE_CODE (type) == REFERENCE_TYPE)
20846 return dependent_type_p (TREE_TYPE (type));
20847 else if (TREE_CODE (type) == FUNCTION_TYPE
20848 || TREE_CODE (type) == METHOD_TYPE)
20850 tree arg_type;
20852 if (dependent_type_p (TREE_TYPE (type)))
20853 return true;
20854 for (arg_type = TYPE_ARG_TYPES (type);
20855 arg_type;
20856 arg_type = TREE_CHAIN (arg_type))
20857 if (dependent_type_p (TREE_VALUE (arg_type)))
20858 return true;
20859 return false;
20861 /* -- an array type constructed from any dependent type or whose
20862 size is specified by a constant expression that is
20863 value-dependent.
20865 We checked for type- and value-dependence of the bounds in
20866 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20867 if (TREE_CODE (type) == ARRAY_TYPE)
20869 if (TYPE_DOMAIN (type)
20870 && dependent_type_p (TYPE_DOMAIN (type)))
20871 return true;
20872 return dependent_type_p (TREE_TYPE (type));
20875 /* -- a template-id in which either the template name is a template
20876 parameter ... */
20877 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20878 return true;
20879 /* ... or any of the template arguments is a dependent type or
20880 an expression that is type-dependent or value-dependent. */
20881 else if (TYPE_TEMPLATE_INFO (type)
20882 && (any_dependent_template_arguments_p
20883 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (type)))))
20884 return true;
20886 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20887 dependent; if the argument of the `typeof' expression is not
20888 type-dependent, then it should already been have resolved. */
20889 if (TREE_CODE (type) == TYPEOF_TYPE
20890 || TREE_CODE (type) == DECLTYPE_TYPE
20891 || TREE_CODE (type) == UNDERLYING_TYPE)
20892 return true;
20894 /* A template argument pack is dependent if any of its packed
20895 arguments are. */
20896 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20898 tree args = ARGUMENT_PACK_ARGS (type);
20899 int i, len = TREE_VEC_LENGTH (args);
20900 for (i = 0; i < len; ++i)
20901 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20902 return true;
20905 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20906 be template parameters. */
20907 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20908 return true;
20910 /* The standard does not specifically mention types that are local
20911 to template functions or local classes, but they should be
20912 considered dependent too. For example:
20914 template <int I> void f() {
20915 enum E { a = I };
20916 S<sizeof (E)> s;
20919 The size of `E' cannot be known until the value of `I' has been
20920 determined. Therefore, `E' must be considered dependent. */
20921 scope = TYPE_CONTEXT (type);
20922 if (scope && TYPE_P (scope))
20923 return dependent_type_p (scope);
20924 /* Don't use type_dependent_expression_p here, as it can lead
20925 to infinite recursion trying to determine whether a lambda
20926 nested in a lambda is dependent (c++/47687). */
20927 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20928 && DECL_LANG_SPECIFIC (scope)
20929 && DECL_TEMPLATE_INFO (scope)
20930 && (any_dependent_template_arguments_p
20931 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20932 return true;
20934 /* Other types are non-dependent. */
20935 return false;
20938 /* Returns TRUE if TYPE is dependent, in the sense of
20939 [temp.dep.type]. Note that a NULL type is considered dependent. */
20941 bool
20942 dependent_type_p (tree type)
20944 /* If there are no template parameters in scope, then there can't be
20945 any dependent types. */
20946 if (!processing_template_decl)
20948 /* If we are not processing a template, then nobody should be
20949 providing us with a dependent type. */
20950 gcc_assert (type);
20951 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20952 return false;
20955 /* If the type is NULL, we have not computed a type for the entity
20956 in question; in that case, the type is dependent. */
20957 if (!type)
20958 return true;
20960 /* Erroneous types can be considered non-dependent. */
20961 if (type == error_mark_node)
20962 return false;
20964 /* If we have not already computed the appropriate value for TYPE,
20965 do so now. */
20966 if (!TYPE_DEPENDENT_P_VALID (type))
20968 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20969 TYPE_DEPENDENT_P_VALID (type) = 1;
20972 return TYPE_DEPENDENT_P (type);
20975 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20976 lookup. In other words, a dependent type that is not the current
20977 instantiation. */
20979 bool
20980 dependent_scope_p (tree scope)
20982 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20983 && !currently_open_class (scope));
20986 /* T is a SCOPE_REF; return whether we need to consider it
20987 instantiation-dependent so that we can check access at instantiation
20988 time even though we know which member it resolves to. */
20990 static bool
20991 instantiation_dependent_scope_ref_p (tree t)
20993 if (DECL_P (TREE_OPERAND (t, 1))
20994 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20995 && accessible_in_template_p (TREE_OPERAND (t, 0),
20996 TREE_OPERAND (t, 1)))
20997 return false;
20998 else
20999 return true;
21002 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21003 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21004 expression. */
21006 /* Note that this predicate is not appropriate for general expressions;
21007 only constant expressions (that satisfy potential_constant_expression)
21008 can be tested for value dependence. */
21010 bool
21011 value_dependent_expression_p (tree expression)
21013 if (!processing_template_decl)
21014 return false;
21016 /* A name declared with a dependent type. */
21017 if (DECL_P (expression) && type_dependent_expression_p (expression))
21018 return true;
21020 switch (TREE_CODE (expression))
21022 case IDENTIFIER_NODE:
21023 /* A name that has not been looked up -- must be dependent. */
21024 return true;
21026 case TEMPLATE_PARM_INDEX:
21027 /* A non-type template parm. */
21028 return true;
21030 case CONST_DECL:
21031 /* A non-type template parm. */
21032 if (DECL_TEMPLATE_PARM_P (expression))
21033 return true;
21034 return value_dependent_expression_p (DECL_INITIAL (expression));
21036 case VAR_DECL:
21037 /* A constant with literal type and is initialized
21038 with an expression that is value-dependent.
21040 Note that a non-dependent parenthesized initializer will have
21041 already been replaced with its constant value, so if we see
21042 a TREE_LIST it must be dependent. */
21043 if (DECL_INITIAL (expression)
21044 && decl_constant_var_p (expression)
21045 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
21046 /* cp_finish_decl doesn't fold reference initializers. */
21047 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
21048 || value_dependent_expression_p (DECL_INITIAL (expression))))
21049 return true;
21050 return false;
21052 case DYNAMIC_CAST_EXPR:
21053 case STATIC_CAST_EXPR:
21054 case CONST_CAST_EXPR:
21055 case REINTERPRET_CAST_EXPR:
21056 case CAST_EXPR:
21057 /* These expressions are value-dependent if the type to which
21058 the cast occurs is dependent or the expression being casted
21059 is value-dependent. */
21061 tree type = TREE_TYPE (expression);
21063 if (dependent_type_p (type))
21064 return true;
21066 /* A functional cast has a list of operands. */
21067 expression = TREE_OPERAND (expression, 0);
21068 if (!expression)
21070 /* If there are no operands, it must be an expression such
21071 as "int()". This should not happen for aggregate types
21072 because it would form non-constant expressions. */
21073 gcc_assert (cxx_dialect >= cxx11
21074 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
21076 return false;
21079 if (TREE_CODE (expression) == TREE_LIST)
21080 return any_value_dependent_elements_p (expression);
21082 return value_dependent_expression_p (expression);
21085 case SIZEOF_EXPR:
21086 if (SIZEOF_EXPR_TYPE_P (expression))
21087 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
21088 /* FALLTHRU */
21089 case ALIGNOF_EXPR:
21090 case TYPEID_EXPR:
21091 /* A `sizeof' expression is value-dependent if the operand is
21092 type-dependent or is a pack expansion. */
21093 expression = TREE_OPERAND (expression, 0);
21094 if (PACK_EXPANSION_P (expression))
21095 return true;
21096 else if (TYPE_P (expression))
21097 return dependent_type_p (expression);
21098 return instantiation_dependent_expression_p (expression);
21100 case AT_ENCODE_EXPR:
21101 /* An 'encode' expression is value-dependent if the operand is
21102 type-dependent. */
21103 expression = TREE_OPERAND (expression, 0);
21104 return dependent_type_p (expression);
21106 case NOEXCEPT_EXPR:
21107 expression = TREE_OPERAND (expression, 0);
21108 return instantiation_dependent_expression_p (expression);
21110 case SCOPE_REF:
21111 /* All instantiation-dependent expressions should also be considered
21112 value-dependent. */
21113 return instantiation_dependent_scope_ref_p (expression);
21115 case COMPONENT_REF:
21116 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
21117 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
21119 case NONTYPE_ARGUMENT_PACK:
21120 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21121 is value-dependent. */
21123 tree values = ARGUMENT_PACK_ARGS (expression);
21124 int i, len = TREE_VEC_LENGTH (values);
21126 for (i = 0; i < len; ++i)
21127 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
21128 return true;
21130 return false;
21133 case TRAIT_EXPR:
21135 tree type2 = TRAIT_EXPR_TYPE2 (expression);
21136 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
21137 || (type2 ? dependent_type_p (type2) : false));
21140 case MODOP_EXPR:
21141 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21142 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
21144 case ARRAY_REF:
21145 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21146 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
21148 case ADDR_EXPR:
21150 tree op = TREE_OPERAND (expression, 0);
21151 return (value_dependent_expression_p (op)
21152 || has_value_dependent_address (op));
21155 case CALL_EXPR:
21157 tree fn = get_callee_fndecl (expression);
21158 int i, nargs;
21159 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
21160 return true;
21161 nargs = call_expr_nargs (expression);
21162 for (i = 0; i < nargs; ++i)
21164 tree op = CALL_EXPR_ARG (expression, i);
21165 /* In a call to a constexpr member function, look through the
21166 implicit ADDR_EXPR on the object argument so that it doesn't
21167 cause the call to be considered value-dependent. We also
21168 look through it in potential_constant_expression. */
21169 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
21170 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21171 && TREE_CODE (op) == ADDR_EXPR)
21172 op = TREE_OPERAND (op, 0);
21173 if (value_dependent_expression_p (op))
21174 return true;
21176 return false;
21179 case TEMPLATE_ID_EXPR:
21180 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21181 type-dependent. */
21182 return type_dependent_expression_p (expression);
21184 case CONSTRUCTOR:
21186 unsigned ix;
21187 tree val;
21188 if (dependent_type_p (TREE_TYPE (expression)))
21189 return true;
21190 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
21191 if (value_dependent_expression_p (val))
21192 return true;
21193 return false;
21196 case STMT_EXPR:
21197 /* Treat a GNU statement expression as dependent to avoid crashing
21198 under instantiate_non_dependent_expr; it can't be constant. */
21199 return true;
21201 default:
21202 /* A constant expression is value-dependent if any subexpression is
21203 value-dependent. */
21204 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
21206 case tcc_reference:
21207 case tcc_unary:
21208 case tcc_comparison:
21209 case tcc_binary:
21210 case tcc_expression:
21211 case tcc_vl_exp:
21213 int i, len = cp_tree_operand_length (expression);
21215 for (i = 0; i < len; i++)
21217 tree t = TREE_OPERAND (expression, i);
21219 /* In some cases, some of the operands may be missing.l
21220 (For example, in the case of PREDECREMENT_EXPR, the
21221 amount to increment by may be missing.) That doesn't
21222 make the expression dependent. */
21223 if (t && value_dependent_expression_p (t))
21224 return true;
21227 break;
21228 default:
21229 break;
21231 break;
21234 /* The expression is not value-dependent. */
21235 return false;
21238 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21239 [temp.dep.expr]. Note that an expression with no type is
21240 considered dependent. Other parts of the compiler arrange for an
21241 expression with type-dependent subexpressions to have no type, so
21242 this function doesn't have to be fully recursive. */
21244 bool
21245 type_dependent_expression_p (tree expression)
21247 if (!processing_template_decl)
21248 return false;
21250 if (expression == NULL_TREE || expression == error_mark_node)
21251 return false;
21253 /* An unresolved name is always dependent. */
21254 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
21255 return true;
21257 /* Some expression forms are never type-dependent. */
21258 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
21259 || TREE_CODE (expression) == SIZEOF_EXPR
21260 || TREE_CODE (expression) == ALIGNOF_EXPR
21261 || TREE_CODE (expression) == AT_ENCODE_EXPR
21262 || TREE_CODE (expression) == NOEXCEPT_EXPR
21263 || TREE_CODE (expression) == TRAIT_EXPR
21264 || TREE_CODE (expression) == TYPEID_EXPR
21265 || TREE_CODE (expression) == DELETE_EXPR
21266 || TREE_CODE (expression) == VEC_DELETE_EXPR
21267 || TREE_CODE (expression) == THROW_EXPR)
21268 return false;
21270 /* The types of these expressions depends only on the type to which
21271 the cast occurs. */
21272 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
21273 || TREE_CODE (expression) == STATIC_CAST_EXPR
21274 || TREE_CODE (expression) == CONST_CAST_EXPR
21275 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
21276 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
21277 || TREE_CODE (expression) == CAST_EXPR)
21278 return dependent_type_p (TREE_TYPE (expression));
21280 /* The types of these expressions depends only on the type created
21281 by the expression. */
21282 if (TREE_CODE (expression) == NEW_EXPR
21283 || TREE_CODE (expression) == VEC_NEW_EXPR)
21285 /* For NEW_EXPR tree nodes created inside a template, either
21286 the object type itself or a TREE_LIST may appear as the
21287 operand 1. */
21288 tree type = TREE_OPERAND (expression, 1);
21289 if (TREE_CODE (type) == TREE_LIST)
21290 /* This is an array type. We need to check array dimensions
21291 as well. */
21292 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
21293 || value_dependent_expression_p
21294 (TREE_OPERAND (TREE_VALUE (type), 1));
21295 else
21296 return dependent_type_p (type);
21299 if (TREE_CODE (expression) == SCOPE_REF)
21301 tree scope = TREE_OPERAND (expression, 0);
21302 tree name = TREE_OPERAND (expression, 1);
21304 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21305 contains an identifier associated by name lookup with one or more
21306 declarations declared with a dependent type, or...a
21307 nested-name-specifier or qualified-id that names a member of an
21308 unknown specialization. */
21309 return (type_dependent_expression_p (name)
21310 || dependent_scope_p (scope));
21313 if (TREE_CODE (expression) == FUNCTION_DECL
21314 && DECL_LANG_SPECIFIC (expression)
21315 && DECL_TEMPLATE_INFO (expression)
21316 && (any_dependent_template_arguments_p
21317 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21318 return true;
21320 if (TREE_CODE (expression) == TEMPLATE_DECL
21321 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21322 return false;
21324 if (TREE_CODE (expression) == STMT_EXPR)
21325 expression = stmt_expr_value_expr (expression);
21327 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21329 tree elt;
21330 unsigned i;
21332 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
21334 if (type_dependent_expression_p (elt))
21335 return true;
21337 return false;
21340 /* A static data member of the current instantiation with incomplete
21341 array type is type-dependent, as the definition and specializations
21342 can have different bounds. */
21343 if (VAR_P (expression)
21344 && DECL_CLASS_SCOPE_P (expression)
21345 && dependent_type_p (DECL_CONTEXT (expression))
21346 && VAR_HAD_UNKNOWN_BOUND (expression))
21347 return true;
21349 /* An array of unknown bound depending on a variadic parameter, eg:
21351 template<typename... Args>
21352 void foo (Args... args)
21354 int arr[] = { args... };
21357 template<int... vals>
21358 void bar ()
21360 int arr[] = { vals... };
21363 If the array has no length and has an initializer, it must be that
21364 we couldn't determine its length in cp_complete_array_type because
21365 it is dependent. */
21366 if (VAR_P (expression)
21367 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
21368 && !TYPE_DOMAIN (TREE_TYPE (expression))
21369 && DECL_INITIAL (expression))
21370 return true;
21372 if (TREE_TYPE (expression) == unknown_type_node)
21374 if (TREE_CODE (expression) == ADDR_EXPR)
21375 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21376 if (TREE_CODE (expression) == COMPONENT_REF
21377 || TREE_CODE (expression) == OFFSET_REF)
21379 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
21380 return true;
21381 expression = TREE_OPERAND (expression, 1);
21382 if (identifier_p (expression))
21383 return false;
21385 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21386 if (TREE_CODE (expression) == SCOPE_REF)
21387 return false;
21389 /* Always dependent, on the number of arguments if nothing else. */
21390 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21391 return true;
21393 if (BASELINK_P (expression))
21395 if (BASELINK_OPTYPE (expression)
21396 && dependent_type_p (BASELINK_OPTYPE (expression)))
21397 return true;
21398 expression = BASELINK_FUNCTIONS (expression);
21401 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21403 if (any_dependent_template_arguments_p
21404 (TREE_OPERAND (expression, 1)))
21405 return true;
21406 expression = TREE_OPERAND (expression, 0);
21408 gcc_assert (TREE_CODE (expression) == OVERLOAD
21409 || TREE_CODE (expression) == FUNCTION_DECL);
21411 while (expression)
21413 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21414 return true;
21415 expression = OVL_NEXT (expression);
21417 return false;
21420 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21422 return (dependent_type_p (TREE_TYPE (expression)));
21425 /* walk_tree callback function for instantiation_dependent_expression_p,
21426 below. Returns non-zero if a dependent subexpression is found. */
21428 static tree
21429 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21430 void * /*data*/)
21432 if (TYPE_P (*tp))
21434 /* We don't have to worry about decltype currently because decltype
21435 of an instantiation-dependent expr is a dependent type. This
21436 might change depending on the resolution of DR 1172. */
21437 *walk_subtrees = false;
21438 return NULL_TREE;
21440 enum tree_code code = TREE_CODE (*tp);
21441 switch (code)
21443 /* Don't treat an argument list as dependent just because it has no
21444 TREE_TYPE. */
21445 case TREE_LIST:
21446 case TREE_VEC:
21447 return NULL_TREE;
21449 case VAR_DECL:
21450 case CONST_DECL:
21451 /* A constant with a dependent initializer is dependent. */
21452 if (value_dependent_expression_p (*tp))
21453 return *tp;
21454 break;
21456 case TEMPLATE_PARM_INDEX:
21457 return *tp;
21459 /* Handle expressions with type operands. */
21460 case SIZEOF_EXPR:
21461 case ALIGNOF_EXPR:
21462 case TYPEID_EXPR:
21463 case AT_ENCODE_EXPR:
21465 tree op = TREE_OPERAND (*tp, 0);
21466 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21467 op = TREE_TYPE (op);
21468 if (TYPE_P (op))
21470 if (dependent_type_p (op))
21471 return *tp;
21472 else
21474 *walk_subtrees = false;
21475 return NULL_TREE;
21478 break;
21481 case TRAIT_EXPR:
21482 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
21483 || (TRAIT_EXPR_TYPE2 (*tp)
21484 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
21485 return *tp;
21486 *walk_subtrees = false;
21487 return NULL_TREE;
21489 case COMPONENT_REF:
21490 if (identifier_p (TREE_OPERAND (*tp, 1)))
21491 /* In a template, finish_class_member_access_expr creates a
21492 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21493 type-dependent, so that we can check access control at
21494 instantiation time (PR 42277). See also Core issue 1273. */
21495 return *tp;
21496 break;
21498 case SCOPE_REF:
21499 if (instantiation_dependent_scope_ref_p (*tp))
21500 return *tp;
21501 else
21502 break;
21504 /* Treat statement-expressions as dependent. */
21505 case BIND_EXPR:
21506 return *tp;
21508 default:
21509 break;
21512 if (type_dependent_expression_p (*tp))
21513 return *tp;
21514 else
21515 return NULL_TREE;
21518 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21519 sense defined by the ABI:
21521 "An expression is instantiation-dependent if it is type-dependent
21522 or value-dependent, or it has a subexpression that is type-dependent
21523 or value-dependent." */
21525 bool
21526 instantiation_dependent_expression_p (tree expression)
21528 tree result;
21530 if (!processing_template_decl)
21531 return false;
21533 if (expression == error_mark_node)
21534 return false;
21536 result = cp_walk_tree_without_duplicates (&expression,
21537 instantiation_dependent_r, NULL);
21538 return result != NULL_TREE;
21541 /* Like type_dependent_expression_p, but it also works while not processing
21542 a template definition, i.e. during substitution or mangling. */
21544 bool
21545 type_dependent_expression_p_push (tree expr)
21547 bool b;
21548 ++processing_template_decl;
21549 b = type_dependent_expression_p (expr);
21550 --processing_template_decl;
21551 return b;
21554 /* Returns TRUE if ARGS contains a type-dependent expression. */
21556 bool
21557 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21559 unsigned int i;
21560 tree arg;
21562 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21564 if (type_dependent_expression_p (arg))
21565 return true;
21567 return false;
21570 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21571 expressions) contains any type-dependent expressions. */
21573 bool
21574 any_type_dependent_elements_p (const_tree list)
21576 for (; list; list = TREE_CHAIN (list))
21577 if (type_dependent_expression_p (TREE_VALUE (list)))
21578 return true;
21580 return false;
21583 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21584 expressions) contains any value-dependent expressions. */
21586 bool
21587 any_value_dependent_elements_p (const_tree list)
21589 for (; list; list = TREE_CHAIN (list))
21590 if (value_dependent_expression_p (TREE_VALUE (list)))
21591 return true;
21593 return false;
21596 /* Returns TRUE if the ARG (a template argument) is dependent. */
21598 bool
21599 dependent_template_arg_p (tree arg)
21601 if (!processing_template_decl)
21602 return false;
21604 /* Assume a template argument that was wrongly written by the user
21605 is dependent. This is consistent with what
21606 any_dependent_template_arguments_p [that calls this function]
21607 does. */
21608 if (!arg || arg == error_mark_node)
21609 return true;
21611 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21612 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21614 if (TREE_CODE (arg) == TEMPLATE_DECL
21615 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21616 return dependent_template_p (arg);
21617 else if (ARGUMENT_PACK_P (arg))
21619 tree args = ARGUMENT_PACK_ARGS (arg);
21620 int i, len = TREE_VEC_LENGTH (args);
21621 for (i = 0; i < len; ++i)
21623 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21624 return true;
21627 return false;
21629 else if (TYPE_P (arg))
21630 return dependent_type_p (arg);
21631 else
21632 return (type_dependent_expression_p (arg)
21633 || value_dependent_expression_p (arg));
21636 /* Returns true if ARGS (a collection of template arguments) contains
21637 any types that require structural equality testing. */
21639 bool
21640 any_template_arguments_need_structural_equality_p (tree args)
21642 int i;
21643 int j;
21645 if (!args)
21646 return false;
21647 if (args == error_mark_node)
21648 return true;
21650 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21652 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21653 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21655 tree arg = TREE_VEC_ELT (level, j);
21656 tree packed_args = NULL_TREE;
21657 int k, len = 1;
21659 if (ARGUMENT_PACK_P (arg))
21661 /* Look inside the argument pack. */
21662 packed_args = ARGUMENT_PACK_ARGS (arg);
21663 len = TREE_VEC_LENGTH (packed_args);
21666 for (k = 0; k < len; ++k)
21668 if (packed_args)
21669 arg = TREE_VEC_ELT (packed_args, k);
21671 if (error_operand_p (arg))
21672 return true;
21673 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21674 continue;
21675 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21676 return true;
21677 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21678 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21679 return true;
21684 return false;
21687 /* Returns true if ARGS (a collection of template arguments) contains
21688 any dependent arguments. */
21690 bool
21691 any_dependent_template_arguments_p (const_tree args)
21693 int i;
21694 int j;
21696 if (!args)
21697 return false;
21698 if (args == error_mark_node)
21699 return true;
21701 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21703 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21704 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21705 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21706 return true;
21709 return false;
21712 /* Returns TRUE if the template TMPL is dependent. */
21714 bool
21715 dependent_template_p (tree tmpl)
21717 if (TREE_CODE (tmpl) == OVERLOAD)
21719 while (tmpl)
21721 if (dependent_template_p (OVL_CURRENT (tmpl)))
21722 return true;
21723 tmpl = OVL_NEXT (tmpl);
21725 return false;
21728 /* Template template parameters are dependent. */
21729 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21730 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21731 return true;
21732 /* So are names that have not been looked up. */
21733 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21734 return true;
21735 /* So are member templates of dependent classes. */
21736 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21737 return dependent_type_p (DECL_CONTEXT (tmpl));
21738 return false;
21741 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21743 bool
21744 dependent_template_id_p (tree tmpl, tree args)
21746 return (dependent_template_p (tmpl)
21747 || any_dependent_template_arguments_p (args));
21750 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21751 is dependent. */
21753 bool
21754 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21756 int i;
21758 if (!processing_template_decl)
21759 return false;
21761 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21763 tree decl = TREE_VEC_ELT (declv, i);
21764 tree init = TREE_VEC_ELT (initv, i);
21765 tree cond = TREE_VEC_ELT (condv, i);
21766 tree incr = TREE_VEC_ELT (incrv, i);
21768 if (type_dependent_expression_p (decl))
21769 return true;
21771 if (init && type_dependent_expression_p (init))
21772 return true;
21774 if (type_dependent_expression_p (cond))
21775 return true;
21777 if (COMPARISON_CLASS_P (cond)
21778 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21779 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21780 return true;
21782 if (TREE_CODE (incr) == MODOP_EXPR)
21784 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21785 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21786 return true;
21788 else if (type_dependent_expression_p (incr))
21789 return true;
21790 else if (TREE_CODE (incr) == MODIFY_EXPR)
21792 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21793 return true;
21794 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21796 tree t = TREE_OPERAND (incr, 1);
21797 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21798 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21799 return true;
21804 return false;
21807 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21808 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21809 no such TYPE can be found. Note that this function peers inside
21810 uninstantiated templates and therefore should be used only in
21811 extremely limited situations. ONLY_CURRENT_P restricts this
21812 peering to the currently open classes hierarchy (which is required
21813 when comparing types). */
21815 tree
21816 resolve_typename_type (tree type, bool only_current_p)
21818 tree scope;
21819 tree name;
21820 tree decl;
21821 int quals;
21822 tree pushed_scope;
21823 tree result;
21825 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21827 scope = TYPE_CONTEXT (type);
21828 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21829 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21830 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21831 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21832 identifier of the TYPENAME_TYPE anymore.
21833 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21834 TYPENAME_TYPE instead, we avoid messing up with a possible
21835 typedef variant case. */
21836 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21838 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21839 it first before we can figure out what NAME refers to. */
21840 if (TREE_CODE (scope) == TYPENAME_TYPE)
21842 if (TYPENAME_IS_RESOLVING_P (scope))
21843 /* Given a class template A with a dependent base with nested type C,
21844 typedef typename A::C::C C will land us here, as trying to resolve
21845 the initial A::C leads to the local C typedef, which leads back to
21846 A::C::C. So we break the recursion now. */
21847 return type;
21848 else
21849 scope = resolve_typename_type (scope, only_current_p);
21851 /* If we don't know what SCOPE refers to, then we cannot resolve the
21852 TYPENAME_TYPE. */
21853 if (TREE_CODE (scope) == TYPENAME_TYPE)
21854 return type;
21855 /* If the SCOPE is a template type parameter, we have no way of
21856 resolving the name. */
21857 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21858 return type;
21859 /* If the SCOPE is not the current instantiation, there's no reason
21860 to look inside it. */
21861 if (only_current_p && !currently_open_class (scope))
21862 return type;
21863 /* If this is a typedef, we don't want to look inside (c++/11987). */
21864 if (typedef_variant_p (type))
21865 return type;
21866 /* If SCOPE isn't the template itself, it will not have a valid
21867 TYPE_FIELDS list. */
21868 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21869 /* scope is either the template itself or a compatible instantiation
21870 like X<T>, so look up the name in the original template. */
21871 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21872 else
21873 /* scope is a partial instantiation, so we can't do the lookup or we
21874 will lose the template arguments. */
21875 return type;
21876 /* Enter the SCOPE so that name lookup will be resolved as if we
21877 were in the class definition. In particular, SCOPE will no
21878 longer be considered a dependent type. */
21879 pushed_scope = push_scope (scope);
21880 /* Look up the declaration. */
21881 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21882 tf_warning_or_error);
21884 result = NULL_TREE;
21886 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21887 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21888 if (!decl)
21889 /*nop*/;
21890 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21891 && TREE_CODE (decl) == TYPE_DECL)
21893 result = TREE_TYPE (decl);
21894 if (result == error_mark_node)
21895 result = NULL_TREE;
21897 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21898 && DECL_CLASS_TEMPLATE_P (decl))
21900 tree tmpl;
21901 tree args;
21902 /* Obtain the template and the arguments. */
21903 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21904 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21905 /* Instantiate the template. */
21906 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21907 /*entering_scope=*/0,
21908 tf_error | tf_user);
21909 if (result == error_mark_node)
21910 result = NULL_TREE;
21913 /* Leave the SCOPE. */
21914 if (pushed_scope)
21915 pop_scope (pushed_scope);
21917 /* If we failed to resolve it, return the original typename. */
21918 if (!result)
21919 return type;
21921 /* If lookup found a typename type, resolve that too. */
21922 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21924 /* Ill-formed programs can cause infinite recursion here, so we
21925 must catch that. */
21926 TYPENAME_IS_RESOLVING_P (type) = 1;
21927 result = resolve_typename_type (result, only_current_p);
21928 TYPENAME_IS_RESOLVING_P (type) = 0;
21931 /* Qualify the resulting type. */
21932 quals = cp_type_quals (type);
21933 if (quals)
21934 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21936 return result;
21939 /* EXPR is an expression which is not type-dependent. Return a proxy
21940 for EXPR that can be used to compute the types of larger
21941 expressions containing EXPR. */
21943 tree
21944 build_non_dependent_expr (tree expr)
21946 tree inner_expr;
21948 #ifdef ENABLE_CHECKING
21949 /* Try to get a constant value for all non-dependent expressions in
21950 order to expose bugs in *_dependent_expression_p and constexpr. */
21951 if (cxx_dialect >= cxx11)
21952 fold_non_dependent_expr (expr);
21953 #endif
21955 /* Preserve OVERLOADs; the functions must be available to resolve
21956 types. */
21957 inner_expr = expr;
21958 if (TREE_CODE (inner_expr) == STMT_EXPR)
21959 inner_expr = stmt_expr_value_expr (inner_expr);
21960 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21961 inner_expr = TREE_OPERAND (inner_expr, 0);
21962 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21963 inner_expr = TREE_OPERAND (inner_expr, 1);
21964 if (is_overloaded_fn (inner_expr)
21965 || TREE_CODE (inner_expr) == OFFSET_REF)
21966 return expr;
21967 /* There is no need to return a proxy for a variable. */
21968 if (VAR_P (expr))
21969 return expr;
21970 /* Preserve string constants; conversions from string constants to
21971 "char *" are allowed, even though normally a "const char *"
21972 cannot be used to initialize a "char *". */
21973 if (TREE_CODE (expr) == STRING_CST)
21974 return expr;
21975 /* Preserve void and arithmetic constants, as an optimization -- there is no
21976 reason to create a new node. */
21977 if (TREE_CODE (expr) == VOID_CST
21978 || TREE_CODE (expr) == INTEGER_CST
21979 || TREE_CODE (expr) == REAL_CST)
21980 return expr;
21981 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21982 There is at least one place where we want to know that a
21983 particular expression is a throw-expression: when checking a ?:
21984 expression, there are special rules if the second or third
21985 argument is a throw-expression. */
21986 if (TREE_CODE (expr) == THROW_EXPR)
21987 return expr;
21989 /* Don't wrap an initializer list, we need to be able to look inside. */
21990 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21991 return expr;
21993 /* Don't wrap a dummy object, we need to be able to test for it. */
21994 if (is_dummy_object (expr))
21995 return expr;
21997 if (TREE_CODE (expr) == COND_EXPR)
21998 return build3 (COND_EXPR,
21999 TREE_TYPE (expr),
22000 TREE_OPERAND (expr, 0),
22001 (TREE_OPERAND (expr, 1)
22002 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
22003 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
22004 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
22005 if (TREE_CODE (expr) == COMPOUND_EXPR
22006 && !COMPOUND_EXPR_OVERLOADED (expr))
22007 return build2 (COMPOUND_EXPR,
22008 TREE_TYPE (expr),
22009 TREE_OPERAND (expr, 0),
22010 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
22012 /* If the type is unknown, it can't really be non-dependent */
22013 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
22015 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22016 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
22019 /* ARGS is a vector of expressions as arguments to a function call.
22020 Replace the arguments with equivalent non-dependent expressions.
22021 This modifies ARGS in place. */
22023 void
22024 make_args_non_dependent (vec<tree, va_gc> *args)
22026 unsigned int ix;
22027 tree arg;
22029 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
22031 tree newarg = build_non_dependent_expr (arg);
22032 if (newarg != arg)
22033 (*args)[ix] = newarg;
22037 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22038 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22039 parms. */
22041 static tree
22042 make_auto_1 (tree name)
22044 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
22045 TYPE_NAME (au) = build_decl (input_location,
22046 TYPE_DECL, name, au);
22047 TYPE_STUB_DECL (au) = TYPE_NAME (au);
22048 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
22049 (0, processing_template_decl + 1, processing_template_decl + 1,
22050 TYPE_NAME (au), NULL_TREE);
22051 TYPE_CANONICAL (au) = canonical_type_parameter (au);
22052 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
22053 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
22055 return au;
22058 tree
22059 make_decltype_auto (void)
22061 return make_auto_1 (get_identifier ("decltype(auto)"));
22064 tree
22065 make_auto (void)
22067 return make_auto_1 (get_identifier ("auto"));
22070 /* Given type ARG, return std::initializer_list<ARG>. */
22072 static tree
22073 listify (tree arg)
22075 tree std_init_list = namespace_binding
22076 (get_identifier ("initializer_list"), std_node);
22077 tree argvec;
22078 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
22080 error ("deducing from brace-enclosed initializer list requires "
22081 "#include <initializer_list>");
22082 return error_mark_node;
22084 argvec = make_tree_vec (1);
22085 TREE_VEC_ELT (argvec, 0) = arg;
22086 return lookup_template_class (std_init_list, argvec, NULL_TREE,
22087 NULL_TREE, 0, tf_warning_or_error);
22090 /* Replace auto in TYPE with std::initializer_list<auto>. */
22092 static tree
22093 listify_autos (tree type, tree auto_node)
22095 tree init_auto = listify (auto_node);
22096 tree argvec = make_tree_vec (1);
22097 TREE_VEC_ELT (argvec, 0) = init_auto;
22098 if (processing_template_decl)
22099 argvec = add_to_template_args (current_template_args (), argvec);
22100 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22103 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22104 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22106 tree
22107 do_auto_deduction (tree type, tree init, tree auto_node)
22109 tree targs;
22111 if (init == error_mark_node)
22112 return error_mark_node;
22114 if (type_dependent_expression_p (init))
22115 /* Defining a subset of type-dependent expressions that we can deduce
22116 from ahead of time isn't worth the trouble. */
22117 return type;
22119 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22120 with either a new invented type template parameter U or, if the
22121 initializer is a braced-init-list (8.5.4), with
22122 std::initializer_list<U>. */
22123 if (BRACE_ENCLOSED_INITIALIZER_P (init))
22125 if (!DIRECT_LIST_INIT_P (init))
22126 type = listify_autos (type, auto_node);
22127 else if (CONSTRUCTOR_NELTS (init) == 1)
22128 init = CONSTRUCTOR_ELT (init, 0)->value;
22129 else
22131 if (permerror (input_location, "direct-list-initialization of "
22132 "%<auto%> requires exactly one element"))
22133 inform (input_location,
22134 "for deduction to %<std::initializer_list%>, use copy-"
22135 "list-initialization (i.e. add %<=%> before the %<{%>)");
22136 type = listify_autos (type, auto_node);
22140 init = resolve_nondeduced_context (init);
22142 targs = make_tree_vec (1);
22143 if (AUTO_IS_DECLTYPE (auto_node))
22145 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
22146 && !REF_PARENTHESIZED_P (init)));
22147 TREE_VEC_ELT (targs, 0)
22148 = finish_decltype_type (init, id, tf_warning_or_error);
22149 if (type != auto_node)
22151 error ("%qT as type rather than plain %<decltype(auto)%>", type);
22152 return error_mark_node;
22155 else
22157 tree parms = build_tree_list (NULL_TREE, type);
22158 tree tparms = make_tree_vec (1);
22159 int val;
22161 TREE_VEC_ELT (tparms, 0)
22162 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
22163 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
22164 DEDUCE_CALL, LOOKUP_NORMAL,
22165 NULL, /*explain_p=*/false);
22166 if (val > 0)
22168 if (processing_template_decl)
22169 /* Try again at instantiation time. */
22170 return type;
22171 if (type && type != error_mark_node)
22172 /* If type is error_mark_node a diagnostic must have been
22173 emitted by now. Also, having a mention to '<type error>'
22174 in the diagnostic is not really useful to the user. */
22176 if (cfun && auto_node == current_function_auto_return_pattern
22177 && LAMBDA_FUNCTION_P (current_function_decl))
22178 error ("unable to deduce lambda return type from %qE", init);
22179 else
22180 error ("unable to deduce %qT from %qE", type, init);
22182 return error_mark_node;
22186 /* If the list of declarators contains more than one declarator, the type
22187 of each declared variable is determined as described above. If the
22188 type deduced for the template parameter U is not the same in each
22189 deduction, the program is ill-formed. */
22190 if (TREE_TYPE (auto_node)
22191 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
22193 if (cfun && auto_node == current_function_auto_return_pattern
22194 && LAMBDA_FUNCTION_P (current_function_decl))
22195 error ("inconsistent types %qT and %qT deduced for "
22196 "lambda return type", TREE_TYPE (auto_node),
22197 TREE_VEC_ELT (targs, 0));
22198 else
22199 error ("inconsistent deduction for %qT: %qT and then %qT",
22200 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
22201 return error_mark_node;
22203 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
22205 if (processing_template_decl)
22206 targs = add_to_template_args (current_template_args (), targs);
22207 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
22210 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22211 result. */
22213 tree
22214 splice_late_return_type (tree type, tree late_return_type)
22216 tree argvec;
22218 if (late_return_type == NULL_TREE)
22219 return type;
22220 argvec = make_tree_vec (1);
22221 TREE_VEC_ELT (argvec, 0) = late_return_type;
22222 if (processing_template_parmlist)
22223 /* For a late-specified return type in a template type-parameter, we
22224 need to add a dummy argument level for its parmlist. */
22225 argvec = add_to_template_args
22226 (make_tree_vec (processing_template_parmlist), argvec);
22227 if (current_template_parms)
22228 argvec = add_to_template_args (current_template_args (), argvec);
22229 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22232 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22233 'decltype(auto)'. */
22235 bool
22236 is_auto (const_tree type)
22238 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22239 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
22240 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
22241 return true;
22242 else
22243 return false;
22246 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22247 a use of `auto'. Returns NULL_TREE otherwise. */
22249 tree
22250 type_uses_auto (tree type)
22252 return find_type_usage (type, is_auto);
22255 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22256 'decltype(auto)' or a concept. */
22258 bool
22259 is_auto_or_concept (const_tree type)
22261 return is_auto (type); // or concept
22264 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22265 a concept identifier) iff TYPE contains a use of a generic type. Returns
22266 NULL_TREE otherwise. */
22268 tree
22269 type_uses_auto_or_concept (tree type)
22271 return find_type_usage (type, is_auto_or_concept);
22275 /* For a given template T, return the vector of typedefs referenced
22276 in T for which access check is needed at T instantiation time.
22277 T is either a FUNCTION_DECL or a RECORD_TYPE.
22278 Those typedefs were added to T by the function
22279 append_type_to_template_for_access_check. */
22281 vec<qualified_typedef_usage_t, va_gc> *
22282 get_types_needing_access_check (tree t)
22284 tree ti;
22285 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
22287 if (!t || t == error_mark_node)
22288 return NULL;
22290 if (!(ti = get_template_info (t)))
22291 return NULL;
22293 if (CLASS_TYPE_P (t)
22294 || TREE_CODE (t) == FUNCTION_DECL)
22296 if (!TI_TEMPLATE (ti))
22297 return NULL;
22299 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
22302 return result;
22305 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22306 tied to T. That list of typedefs will be access checked at
22307 T instantiation time.
22308 T is either a FUNCTION_DECL or a RECORD_TYPE.
22309 TYPE_DECL is a TYPE_DECL node representing a typedef.
22310 SCOPE is the scope through which TYPE_DECL is accessed.
22311 LOCATION is the location of the usage point of TYPE_DECL.
22313 This function is a subroutine of
22314 append_type_to_template_for_access_check. */
22316 static void
22317 append_type_to_template_for_access_check_1 (tree t,
22318 tree type_decl,
22319 tree scope,
22320 location_t location)
22322 qualified_typedef_usage_t typedef_usage;
22323 tree ti;
22325 if (!t || t == error_mark_node)
22326 return;
22328 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
22329 || CLASS_TYPE_P (t))
22330 && type_decl
22331 && TREE_CODE (type_decl) == TYPE_DECL
22332 && scope);
22334 if (!(ti = get_template_info (t)))
22335 return;
22337 gcc_assert (TI_TEMPLATE (ti));
22339 typedef_usage.typedef_decl = type_decl;
22340 typedef_usage.context = scope;
22341 typedef_usage.locus = location;
22343 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
22346 /* Append TYPE_DECL to the template TEMPL.
22347 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22348 At TEMPL instanciation time, TYPE_DECL will be checked to see
22349 if it can be accessed through SCOPE.
22350 LOCATION is the location of the usage point of TYPE_DECL.
22352 e.g. consider the following code snippet:
22354 class C
22356 typedef int myint;
22359 template<class U> struct S
22361 C::myint mi; // <-- usage point of the typedef C::myint
22364 S<char> s;
22366 At S<char> instantiation time, we need to check the access of C::myint
22367 In other words, we need to check the access of the myint typedef through
22368 the C scope. For that purpose, this function will add the myint typedef
22369 and the scope C through which its being accessed to a list of typedefs
22370 tied to the template S. That list will be walked at template instantiation
22371 time and access check performed on each typedefs it contains.
22372 Note that this particular code snippet should yield an error because
22373 myint is private to C. */
22375 void
22376 append_type_to_template_for_access_check (tree templ,
22377 tree type_decl,
22378 tree scope,
22379 location_t location)
22381 qualified_typedef_usage_t *iter;
22382 unsigned i;
22384 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
22386 /* Make sure we don't append the type to the template twice. */
22387 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
22388 if (iter->typedef_decl == type_decl && scope == iter->context)
22389 return;
22391 append_type_to_template_for_access_check_1 (templ, type_decl,
22392 scope, location);
22395 /* Convert the generic type parameters in PARM that match the types given in the
22396 range [START_IDX, END_IDX) from the current_template_parms into generic type
22397 packs. */
22399 tree
22400 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22402 tree current = current_template_parms;
22403 int depth = TMPL_PARMS_DEPTH (current);
22404 current = INNERMOST_TEMPLATE_PARMS (current);
22405 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22407 for (int i = 0; i < start_idx; ++i)
22408 TREE_VEC_ELT (replacement, i)
22409 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22411 for (int i = start_idx; i < end_idx; ++i)
22413 /* Create a distinct parameter pack type from the current parm and add it
22414 to the replacement args to tsubst below into the generic function
22415 parameter. */
22417 tree o = TREE_TYPE (TREE_VALUE
22418 (TREE_VEC_ELT (current, i)));
22419 tree t = copy_type (o);
22420 TEMPLATE_TYPE_PARM_INDEX (t)
22421 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22422 o, 0, 0, tf_none);
22423 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22424 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22425 TYPE_MAIN_VARIANT (t) = t;
22426 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22427 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22428 TREE_VEC_ELT (replacement, i) = t;
22429 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22432 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22433 TREE_VEC_ELT (replacement, i)
22434 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22436 /* If there are more levels then build up the replacement with the outer
22437 template parms. */
22438 if (depth > 1)
22439 replacement = add_to_template_args (template_parms_to_args
22440 (TREE_CHAIN (current_template_parms)),
22441 replacement);
22443 return tsubst (parm, replacement, tf_none, NULL_TREE);
22447 /* Set up the hash tables for template instantiations. */
22449 void
22450 init_template_processing (void)
22452 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
22453 type_specializations = hash_table<spec_hasher>::create_ggc (37);
22456 /* Print stats about the template hash tables for -fstats. */
22458 void
22459 print_template_statistics (void)
22461 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22462 "%f collisions\n", (long) decl_specializations->size (),
22463 (long) decl_specializations->elements (),
22464 decl_specializations->collisions ());
22465 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22466 "%f collisions\n", (long) type_specializations->size (),
22467 (long) type_specializations->elements (),
22468 type_specializations->collisions ());
22471 #include "gt-cp-pt.h"