Merge from trunk
[official-gcc.git] / gcc / cp / pt.c
blobe327da4d0a4c4b03a818f240a77091c1c1159076
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 "pointer-set.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimplify.h"
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
66 int processing_template_parmlist;
67 static int template_header_count;
69 static GTY(()) tree saved_trees;
70 static vec<int> inline_parm_levels;
72 static GTY(()) struct tinst_level *current_tinst_level;
74 static GTY(()) tree saved_access_scope;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
81 // -------------------------------------------------------------------------- //
82 // Local Specialization Stack
84 // Implementation of the RAII helper for creating new local
85 // specializations.
86 local_specialization_stack::local_specialization_stack ()
87 : saved (local_specializations)
89 local_specializations = pointer_map_create ();
92 local_specialization_stack::~local_specialization_stack ()
94 pointer_map_destroy (local_specializations);
95 local_specializations = saved;
98 /* True if we've recursed into fn_type_unification too many times. */
99 static bool excessive_deduction_depth;
101 typedef struct GTY(()) spec_entry
103 tree tmpl;
104 tree args;
105 tree spec;
106 } spec_entry;
108 static GTY ((param_is (spec_entry)))
109 htab_t decl_specializations;
111 static GTY ((param_is (spec_entry)))
112 htab_t type_specializations;
114 /* Contains canonical template parameter types. The vector is indexed by
115 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
116 TREE_LIST, whose TREE_VALUEs contain the canonical template
117 parameters of various types and levels. */
118 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
120 #define UNIFY_ALLOW_NONE 0
121 #define UNIFY_ALLOW_MORE_CV_QUAL 1
122 #define UNIFY_ALLOW_LESS_CV_QUAL 2
123 #define UNIFY_ALLOW_DERIVED 4
124 #define UNIFY_ALLOW_INTEGER 8
125 #define UNIFY_ALLOW_OUTER_LEVEL 16
126 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
127 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
129 enum template_base_result {
130 tbr_incomplete_type,
131 tbr_ambiguous_baseclass,
132 tbr_success
135 static void push_access_scope (tree);
136 static void pop_access_scope (tree);
137 static bool resolve_overloaded_unification (tree, tree, tree, tree,
138 unification_kind_t, int,
139 bool);
140 static int try_one_overload (tree, tree, tree, tree, tree,
141 unification_kind_t, int, bool, bool);
142 static int unify (tree, tree, tree, tree, int, bool);
143 static void add_pending_template (tree);
144 static tree reopen_tinst_level (struct tinst_level *);
145 static tree tsubst_initializer_list (tree, tree);
146 static tree get_class_bindings (tree, tree, tree, tree, tree);
147 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
148 bool, bool);
149 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static void tsubst_enum (tree, tree, tree);
152 static tree add_to_template_args (tree, tree);
153 static tree add_outermost_template_args (tree, tree);
154 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
155 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
156 tree);
157 static int type_unification_real (tree, tree, tree, const tree *,
158 unsigned int, int, unification_kind_t, int,
159 vec<deferred_access_check, va_gc> **,
160 bool);
161 static void note_template_header (int);
162 static tree convert_nontype_argument_function (tree, tree);
163 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
164 static tree convert_template_argument (tree, tree, tree,
165 tsubst_flags_t, int, tree);
166 static int for_each_template_parm (tree, tree_fn_t, void*,
167 struct pointer_set_t*, bool);
168 static tree expand_template_argument_pack (tree);
169 static tree build_template_parm_index (int, int, int, tree, tree);
170 static bool inline_needs_template_parms (tree, bool);
171 static void push_inline_template_parms_recursive (tree, int);
172 static hashval_t hash_specialization (const void *p);
173 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
174 static int mark_template_parm (tree, void *);
175 static int template_parm_this_level_p (tree, void *);
176 static tree tsubst_friend_function (tree, tree);
177 static tree tsubst_friend_class (tree, tree);
178 static int can_complete_type_without_circularity (tree);
179 static tree get_bindings (tree, tree, tree, bool);
180 static int template_decl_level (tree);
181 static int check_cv_quals_for_unify (int, tree, tree);
182 static void template_parm_level_and_index (tree, int*, int*);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
186 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
188 static void regenerate_decl_from_template (tree, tree);
189 static tree most_specialized_class (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
200 tree, tree);
201 static bool template_template_parm_bindings_ok_p (tree, tree);
202 static int template_args_equal (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
213 static tree tsubst_decl (tree, tree, tsubst_flags_t);
214 static void perform_typedefs_access_check (tree tmpl, tree targs);
215 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
216 location_t);
217 static tree listify (tree);
218 static tree listify_autos (tree, tree);
219 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
220 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
222 /* Make the current scope suitable for access checking when we are
223 processing T. T can be FUNCTION_DECL for instantiated function
224 template, VAR_DECL for static member variable, or TYPE_DECL for
225 alias template (needed by instantiate_decl). */
227 static void
228 push_access_scope (tree t)
230 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
231 || TREE_CODE (t) == TYPE_DECL);
233 if (DECL_FRIEND_CONTEXT (t))
234 push_nested_class (DECL_FRIEND_CONTEXT (t));
235 else if (DECL_CLASS_SCOPE_P (t))
236 push_nested_class (DECL_CONTEXT (t));
237 else
238 push_to_top_level ();
240 if (TREE_CODE (t) == FUNCTION_DECL)
242 saved_access_scope = tree_cons
243 (NULL_TREE, current_function_decl, saved_access_scope);
244 current_function_decl = t;
248 /* Restore the scope set up by push_access_scope. T is the node we
249 are processing. */
251 static void
252 pop_access_scope (tree t)
254 if (TREE_CODE (t) == FUNCTION_DECL)
256 current_function_decl = TREE_VALUE (saved_access_scope);
257 saved_access_scope = TREE_CHAIN (saved_access_scope);
260 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
261 pop_nested_class ();
262 else
263 pop_from_top_level ();
266 /* Do any processing required when DECL (a member template
267 declaration) is finished. Returns the TEMPLATE_DECL corresponding
268 to DECL, unless it is a specialization, in which case the DECL
269 itself is returned. */
271 tree
272 finish_member_template_decl (tree decl)
274 if (decl == error_mark_node)
275 return error_mark_node;
277 gcc_assert (DECL_P (decl));
279 if (TREE_CODE (decl) == TYPE_DECL)
281 tree type;
283 type = TREE_TYPE (decl);
284 if (type == error_mark_node)
285 return error_mark_node;
286 if (MAYBE_CLASS_TYPE_P (type)
287 && CLASSTYPE_TEMPLATE_INFO (type)
288 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
290 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
291 check_member_template (tmpl);
292 return tmpl;
294 return NULL_TREE;
296 else if (TREE_CODE (decl) == FIELD_DECL)
297 error ("data member %qD cannot be a member template", decl);
298 else if (DECL_TEMPLATE_INFO (decl))
300 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
302 check_member_template (DECL_TI_TEMPLATE (decl));
303 return DECL_TI_TEMPLATE (decl);
305 else
306 return decl;
308 else
309 error ("invalid member template declaration %qD", decl);
311 return error_mark_node;
314 /* Create a template info node. */
316 tree
317 build_template_info (tree template_decl, tree template_args)
319 tree result = make_node (TEMPLATE_INFO);
320 TI_TEMPLATE (result) = template_decl;
321 TI_ARGS (result) = template_args;
322 return result;
325 /* Return the template info node corresponding to T, whatever T is. */
327 tree
328 get_template_info (const_tree t)
330 tree tinfo = NULL_TREE;
332 if (!t || t == error_mark_node)
333 return NULL;
335 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
336 tinfo = DECL_TEMPLATE_INFO (t);
338 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
339 t = TREE_TYPE (t);
341 if (OVERLOAD_TYPE_P (t))
342 tinfo = TYPE_TEMPLATE_INFO (t);
343 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
344 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
346 return tinfo;
349 /* Returns the template nesting level of the indicated class TYPE.
351 For example, in:
352 template <class T>
353 struct A
355 template <class U>
356 struct B {};
359 A<T>::B<U> has depth two, while A<T> has depth one.
360 Both A<T>::B<int> and A<int>::B<U> have depth one, if
361 they are instantiations, not specializations.
363 This function is guaranteed to return 0 if passed NULL_TREE so
364 that, for example, `template_class_depth (current_class_type)' is
365 always safe. */
368 template_class_depth (tree type)
370 int depth;
372 for (depth = 0;
373 type && TREE_CODE (type) != NAMESPACE_DECL;
374 type = (TREE_CODE (type) == FUNCTION_DECL)
375 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
377 tree tinfo = get_template_info (type);
379 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
380 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
381 ++depth;
384 return depth;
387 /* Subroutine of maybe_begin_member_template_processing.
388 Returns true if processing DECL needs us to push template parms. */
390 static bool
391 inline_needs_template_parms (tree decl, bool nsdmi)
393 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
394 return false;
396 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
397 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
400 /* Subroutine of maybe_begin_member_template_processing.
401 Push the template parms in PARMS, starting from LEVELS steps into the
402 chain, and ending at the beginning, since template parms are listed
403 innermost first. */
405 static void
406 push_inline_template_parms_recursive (tree parmlist, int levels)
408 tree parms = TREE_VALUE (parmlist);
409 int i;
411 if (levels > 1)
412 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
414 ++processing_template_decl;
415 current_template_parms
416 = tree_cons (size_int (processing_template_decl),
417 parms, current_template_parms);
418 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
420 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
421 NULL);
422 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
424 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
426 if (parm == error_mark_node)
427 continue;
429 gcc_assert (DECL_P (parm));
431 switch (TREE_CODE (parm))
433 case TYPE_DECL:
434 case TEMPLATE_DECL:
435 pushdecl (parm);
436 break;
438 case PARM_DECL:
440 /* Make a CONST_DECL as is done in process_template_parm.
441 It is ugly that we recreate this here; the original
442 version built in process_template_parm is no longer
443 available. */
444 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
445 CONST_DECL, DECL_NAME (parm),
446 TREE_TYPE (parm));
447 DECL_ARTIFICIAL (decl) = 1;
448 TREE_CONSTANT (decl) = 1;
449 TREE_READONLY (decl) = 1;
450 DECL_INITIAL (decl) = DECL_INITIAL (parm);
451 SET_DECL_TEMPLATE_PARM_P (decl);
452 pushdecl (decl);
454 break;
456 default:
457 gcc_unreachable ();
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
466 void
467 maybe_begin_member_template_processing (tree decl)
469 tree parms;
470 int levels = 0;
471 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
473 if (nsdmi)
474 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
475 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
476 : NULL_TREE);
478 if (inline_needs_template_parms (decl, nsdmi))
480 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
481 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
483 if (DECL_TEMPLATE_SPECIALIZATION (decl))
485 --levels;
486 parms = TREE_CHAIN (parms);
489 push_inline_template_parms_recursive (parms, levels);
492 /* Remember how many levels of template parameters we pushed so that
493 we can pop them later. */
494 inline_parm_levels.safe_push (levels);
497 /* Undo the effects of maybe_begin_member_template_processing. */
499 void
500 maybe_end_member_template_processing (void)
502 int i;
503 int last;
505 if (inline_parm_levels.length () == 0)
506 return;
508 last = inline_parm_levels.pop ();
509 for (i = 0; i < last; ++i)
511 --processing_template_decl;
512 current_template_parms = TREE_CHAIN (current_template_parms);
513 poplevel (0, 0, 0);
517 /* Return a new template argument vector which contains all of ARGS,
518 but has as its innermost set of arguments the EXTRA_ARGS. */
520 static tree
521 add_to_template_args (tree args, tree extra_args)
523 tree new_args;
524 int extra_depth;
525 int i;
526 int j;
528 if (args == NULL_TREE || extra_args == error_mark_node)
529 return extra_args;
531 extra_depth = TMPL_ARGS_DEPTH (extra_args);
532 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
534 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
535 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
537 for (j = 1; j <= extra_depth; ++j, ++i)
538 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
540 return new_args;
543 /* Like add_to_template_args, but only the outermost ARGS are added to
544 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
545 (EXTRA_ARGS) levels are added. This function is used to combine
546 the template arguments from a partial instantiation with the
547 template arguments used to attain the full instantiation from the
548 partial instantiation. */
550 static tree
551 add_outermost_template_args (tree args, tree extra_args)
553 tree new_args;
555 /* If there are more levels of EXTRA_ARGS than there are ARGS,
556 something very fishy is going on. */
557 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
559 /* If *all* the new arguments will be the EXTRA_ARGS, just return
560 them. */
561 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
562 return extra_args;
564 /* For the moment, we make ARGS look like it contains fewer levels. */
565 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
567 new_args = add_to_template_args (args, extra_args);
569 /* Now, we restore ARGS to its full dimensions. */
570 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
572 return new_args;
575 /* Return the N levels of innermost template arguments from the ARGS. */
577 tree
578 get_innermost_template_args (tree args, int n)
580 tree new_args;
581 int extra_levels;
582 int i;
584 gcc_assert (n >= 0);
586 /* If N is 1, just return the innermost set of template arguments. */
587 if (n == 1)
588 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
590 /* If we're not removing anything, just return the arguments we were
591 given. */
592 extra_levels = TMPL_ARGS_DEPTH (args) - n;
593 gcc_assert (extra_levels >= 0);
594 if (extra_levels == 0)
595 return args;
597 /* Make a new set of arguments, not containing the outer arguments. */
598 new_args = make_tree_vec (n);
599 for (i = 1; i <= n; ++i)
600 SET_TMPL_ARGS_LEVEL (new_args, i,
601 TMPL_ARGS_LEVEL (args, i + extra_levels));
603 return new_args;
606 /* The inverse of get_innermost_template_args: Return all but the innermost
607 EXTRA_LEVELS levels of template arguments from the ARGS. */
609 static tree
610 strip_innermost_template_args (tree args, int extra_levels)
612 tree new_args;
613 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
614 int i;
616 gcc_assert (n >= 0);
618 /* If N is 1, just return the outermost set of template arguments. */
619 if (n == 1)
620 return TMPL_ARGS_LEVEL (args, 1);
622 /* If we're not removing anything, just return the arguments we were
623 given. */
624 gcc_assert (extra_levels >= 0);
625 if (extra_levels == 0)
626 return args;
628 /* Make a new set of arguments, not containing the inner arguments. */
629 new_args = make_tree_vec (n);
630 for (i = 1; i <= n; ++i)
631 SET_TMPL_ARGS_LEVEL (new_args, i,
632 TMPL_ARGS_LEVEL (args, i));
634 return new_args;
637 /* We've got a template header coming up; push to a new level for storing
638 the parms. */
640 void
641 begin_template_parm_list (void)
643 /* We use a non-tag-transparent scope here, which causes pushtag to
644 put tags in this scope, rather than in the enclosing class or
645 namespace scope. This is the right thing, since we want
646 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
647 global template class, push_template_decl handles putting the
648 TEMPLATE_DECL into top-level scope. For a nested template class,
649 e.g.:
651 template <class T> struct S1 {
652 template <class T> struct S2 {};
655 pushtag contains special code to call pushdecl_with_scope on the
656 TEMPLATE_DECL for S2. */
657 begin_scope (sk_template_parms, NULL);
658 ++processing_template_decl;
659 ++processing_template_parmlist;
660 note_template_header (0);
663 /* This routine is called when a specialization is declared. If it is
664 invalid to declare a specialization here, an error is reported and
665 false is returned, otherwise this routine will return true. */
667 static bool
668 check_specialization_scope (void)
670 tree scope = current_scope ();
672 /* [temp.expl.spec]
674 An explicit specialization shall be declared in the namespace of
675 which the template is a member, or, for member templates, in the
676 namespace of which the enclosing class or enclosing class
677 template is a member. An explicit specialization of a member
678 function, member class or static data member of a class template
679 shall be declared in the namespace of which the class template
680 is a member. */
681 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
683 error ("explicit specialization in non-namespace scope %qD", scope);
684 return false;
687 /* [temp.expl.spec]
689 In an explicit specialization declaration for a member of a class
690 template or a member template that appears in namespace scope,
691 the member template and some of its enclosing class templates may
692 remain unspecialized, except that the declaration shall not
693 explicitly specialize a class member template if its enclosing
694 class templates are not explicitly specialized as well. */
695 if (current_template_parms)
697 error ("enclosing class templates are not explicitly specialized");
698 return false;
701 return true;
704 /* We've just seen template <>. */
706 bool
707 begin_specialization (void)
709 begin_scope (sk_template_spec, NULL);
710 note_template_header (1);
711 return check_specialization_scope ();
714 /* Called at then end of processing a declaration preceded by
715 template<>. */
717 void
718 end_specialization (void)
720 finish_scope ();
721 reset_specialization ();
724 /* Any template <>'s that we have seen thus far are not referring to a
725 function specialization. */
727 void
728 reset_specialization (void)
730 processing_specialization = 0;
731 template_header_count = 0;
734 /* We've just seen a template header. If SPECIALIZATION is nonzero,
735 it was of the form template <>. */
737 static void
738 note_template_header (int specialization)
740 processing_specialization = specialization;
741 template_header_count++;
744 /* We're beginning an explicit instantiation. */
746 void
747 begin_explicit_instantiation (void)
749 gcc_assert (!processing_explicit_instantiation);
750 processing_explicit_instantiation = true;
754 void
755 end_explicit_instantiation (void)
757 gcc_assert (processing_explicit_instantiation);
758 processing_explicit_instantiation = false;
761 /* An explicit specialization or partial specialization of TMPL is being
762 declared. Check that the namespace in which the specialization is
763 occurring is permissible. Returns false iff it is invalid to
764 specialize TMPL in the current namespace. */
766 static bool
767 check_specialization_namespace (tree tmpl)
769 tree tpl_ns = decl_namespace_context (tmpl);
771 /* [tmpl.expl.spec]
773 An explicit specialization shall be declared in the namespace of
774 which the template is a member, or, for member templates, in the
775 namespace of which the enclosing class or enclosing class
776 template is a member. An explicit specialization of a member
777 function, member class or static data member of a class template
778 shall be declared in the namespace of which the class template is
779 a member. */
780 if (current_scope() != DECL_CONTEXT (tmpl)
781 && !at_namespace_scope_p ())
783 error ("specialization of %qD must appear at namespace scope", tmpl);
784 return false;
786 if (is_associated_namespace (current_namespace, tpl_ns))
787 /* Same or super-using namespace. */
788 return true;
789 else
791 permerror (input_location, "specialization of %qD in different namespace", tmpl);
792 permerror (input_location, " from definition of %q+#D", tmpl);
793 return false;
797 /* SPEC is an explicit instantiation. Check that it is valid to
798 perform this explicit instantiation in the current namespace. */
800 static void
801 check_explicit_instantiation_namespace (tree spec)
803 tree ns;
805 /* DR 275: An explicit instantiation shall appear in an enclosing
806 namespace of its template. */
807 ns = decl_namespace_context (spec);
808 if (!is_ancestor (current_namespace, ns))
809 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
810 "(which does not enclose namespace %qD)",
811 spec, current_namespace, ns);
814 // Returns the TEMPLATE_DECL that defines the partial template specialization
815 // TYPE. If TYPE is not found in the list of partial specializations, then
816 // it must be an explicit specialization.
817 static tree
818 get_specializing_template_decl (tree type)
820 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
821 tree decl = TYPE_MAIN_DECL (type);
822 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
823 while (specs)
825 tree spec = TREE_VALUE (specs);
826 if (DECL_TEMPLATE_RESULT (spec) == decl)
827 return spec;
828 specs = TREE_CHAIN (specs);
830 return NULL_TREE;
833 // Return the template constraints for the template specialization TYPE.
834 static inline tree
835 get_specialization_constraints (tree type)
837 // If TYPE is a partial specialization, return the constraints for
838 // that type. If it is an explicit specialization, return null since
839 // non-templates cannot be constrained.
840 if (tree d = get_specializing_template_decl (type))
841 return DECL_CONSTRAINTS (d);
842 else
843 return NULL_TREE;
846 // Returns the type of a template specialization only if that
847 // specialization needs to defined. Otherwise (e.g., if the type has
848 // already been defined), the function returns NULL_TREE.
849 static tree
850 maybe_new_partial_specialization (tree type)
852 // An implicit instantiation of an incomplete type implies
853 // the definition of a new class template.
855 // template<typename T>
856 // struct S;
858 // template<typename T>
859 // struct S<T*>;
861 // Here, S<T*> is an implicit instantiation of S whose type
862 // is incomplete.
863 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
864 return type;
866 // It can also be the case that is a completed specialization.
867 // Continuing the previous example, suppose we also declare:
869 // template<typename T>
870 // requires Integral<T>
871 // struct S<T*>;
873 // Here, S<T*> refers to the specialization S<T*> defined
874 // above. However, we need to differentiate definitions because
875 // we intend to define a new partial specialization. In this case,
876 // we rely on the fact that the constraints are different for
877 // this declaration than that above.
879 // Note that we also get here for injected class names and
880 // late-parsed template definitions. We must ensure that we
881 // do not create new type declarations for those cases.
882 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
884 tree constr = get_specialization_constraints (type);
885 if (!equivalent_constraints (current_template_reqs, constr))
887 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
888 tree args = CLASSTYPE_TI_ARGS (type);
890 // Create a new type node (and corresponding type decl) that
891 // for the newly declared specialization.
892 tree t = make_class_type (TREE_CODE (type));
893 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
894 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
895 TYPE_CANONICAL (t) = t;
896 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
897 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
899 // Build the corresponding type decl.
900 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
901 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
902 DECL_SOURCE_LOCATION (d) = input_location;
904 return t;
907 return NULL;
910 /* The TYPE is being declared. If it is a template type, that means it
911 is a partial specialization. Do appropriate error-checking. */
913 tree
914 maybe_process_partial_specialization (tree type)
916 tree context;
918 if (type == error_mark_node)
919 return error_mark_node;
921 /* A lambda that appears in specialization context is not itself a
922 specialization. */
923 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
924 return type;
926 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
928 error ("name of class shadows template template parameter %qD",
929 TYPE_NAME (type));
930 return error_mark_node;
933 context = TYPE_CONTEXT (type);
935 if (TYPE_ALIAS_P (type))
937 if (TYPE_TEMPLATE_INFO (type)
938 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
939 error ("specialization of alias template %qD",
940 TYPE_TI_TEMPLATE (type));
941 else
942 error ("explicit specialization of non-template %qT", type);
943 return error_mark_node;
945 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
947 /* This is for ordinary explicit specialization and partial
948 specialization of a template class such as:
950 template <> class C<int>;
954 template <class T> class C<T*>;
956 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
958 if (tree t = maybe_new_partial_specialization (type))
960 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t));
961 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
962 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
963 if (processing_template_decl)
965 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
966 if (decl == error_mark_node)
967 return error_mark_node;
968 return TREE_TYPE (decl);
971 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
972 error ("specialization of %qT after instantiation", type);
973 else if (errorcount && !processing_specialization
974 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
975 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
976 /* Trying to define a specialization either without a template<> header
977 or in an inappropriate place. We've already given an error, so just
978 bail now so we don't actually define the specialization. */
979 return error_mark_node;
981 else if (CLASS_TYPE_P (type)
982 && !CLASSTYPE_USE_TEMPLATE (type)
983 && CLASSTYPE_TEMPLATE_INFO (type)
984 && context && CLASS_TYPE_P (context)
985 && CLASSTYPE_TEMPLATE_INFO (context))
987 /* This is for an explicit specialization of member class
988 template according to [temp.expl.spec/18]:
990 template <> template <class U> class C<int>::D;
992 The context `C<int>' must be an implicit instantiation.
993 Otherwise this is just a member class template declared
994 earlier like:
996 template <> class C<int> { template <class U> class D; };
997 template <> template <class U> class C<int>::D;
999 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1000 while in the second case, `C<int>::D' is a primary template
1001 and `C<T>::D' may not exist. */
1003 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1004 && !COMPLETE_TYPE_P (type))
1006 tree t;
1007 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1009 if (current_namespace
1010 != decl_namespace_context (tmpl))
1012 permerror (input_location, "specializing %q#T in different namespace", type);
1013 permerror (input_location, " from definition of %q+#D", tmpl);
1016 /* Check for invalid specialization after instantiation:
1018 template <> template <> class C<int>::D<int>;
1019 template <> template <class U> class C<int>::D; */
1021 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1022 t; t = TREE_CHAIN (t))
1024 tree inst = TREE_VALUE (t);
1025 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1026 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1028 /* We already have a full specialization of this partial
1029 instantiation, or a full specialization has been
1030 looked up but not instantiated. Reassign it to the
1031 new member specialization template. */
1032 spec_entry elt;
1033 spec_entry *entry;
1034 void **slot;
1036 elt.tmpl = most_general_template (tmpl);
1037 elt.args = CLASSTYPE_TI_ARGS (inst);
1038 elt.spec = inst;
1040 htab_remove_elt (type_specializations, &elt);
1042 elt.tmpl = tmpl;
1043 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1045 slot = htab_find_slot (type_specializations, &elt, INSERT);
1046 entry = ggc_alloc_spec_entry ();
1047 *entry = elt;
1048 *slot = entry;
1050 else
1051 /* But if we've had an implicit instantiation, that's a
1052 problem ([temp.expl.spec]/6). */
1053 error ("specialization %qT after instantiation %qT",
1054 type, inst);
1057 /* Mark TYPE as a specialization. And as a result, we only
1058 have one level of template argument for the innermost
1059 class template. */
1060 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1061 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1062 CLASSTYPE_TI_ARGS (type)
1063 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1066 else if (processing_specialization)
1068 /* Someday C++0x may allow for enum template specialization. */
1069 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1070 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1071 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1072 "of %qD not allowed by ISO C++", type);
1073 else
1075 error ("explicit specialization of non-template %qT", type);
1076 return error_mark_node;
1080 return type;
1083 /* Returns nonzero if we can optimize the retrieval of specializations
1084 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1085 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1087 static inline bool
1088 optimize_specialization_lookup_p (tree tmpl)
1090 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1091 && DECL_CLASS_SCOPE_P (tmpl)
1092 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1093 parameter. */
1094 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1095 /* The optimized lookup depends on the fact that the
1096 template arguments for the member function template apply
1097 purely to the containing class, which is not true if the
1098 containing class is an explicit or partial
1099 specialization. */
1100 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1101 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1102 && !DECL_CONV_FN_P (tmpl)
1103 /* It is possible to have a template that is not a member
1104 template and is not a member of a template class:
1106 template <typename T>
1107 struct S { friend A::f(); };
1109 Here, the friend function is a template, but the context does
1110 not have template information. The optimized lookup relies
1111 on having ARGS be the template arguments for both the class
1112 and the function template. */
1113 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1116 /* Retrieve the specialization (in the sense of [temp.spec] - a
1117 specialization is either an instantiation or an explicit
1118 specialization) of TMPL for the given template ARGS. If there is
1119 no such specialization, return NULL_TREE. The ARGS are a vector of
1120 arguments, or a vector of vectors of arguments, in the case of
1121 templates with more than one level of parameters.
1123 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1124 then we search for a partial specialization matching ARGS. This
1125 parameter is ignored if TMPL is not a class template.
1127 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1128 result is a NONTYPE_ARGUMENT_PACK. */
1130 static tree
1131 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1133 if (tmpl == NULL_TREE)
1134 return NULL_TREE;
1136 if (args == error_mark_node)
1137 return NULL_TREE;
1139 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1140 || TREE_CODE (tmpl) == FIELD_DECL);
1142 /* There should be as many levels of arguments as there are
1143 levels of parameters. */
1144 gcc_assert (TMPL_ARGS_DEPTH (args)
1145 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1146 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1147 : template_class_depth (DECL_CONTEXT (tmpl))));
1149 if (optimize_specialization_lookup_p (tmpl))
1151 tree class_template;
1152 tree class_specialization;
1153 vec<tree, va_gc> *methods;
1154 tree fns;
1155 int idx;
1157 /* The template arguments actually apply to the containing
1158 class. Find the class specialization with those
1159 arguments. */
1160 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1161 class_specialization
1162 = retrieve_specialization (class_template, args, 0);
1163 if (!class_specialization)
1164 return NULL_TREE;
1165 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1166 for the specialization. */
1167 idx = class_method_index_for_fn (class_specialization, tmpl);
1168 if (idx == -1)
1169 return NULL_TREE;
1170 /* Iterate through the methods with the indicated name, looking
1171 for the one that has an instance of TMPL. */
1172 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1173 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1175 tree fn = OVL_CURRENT (fns);
1176 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1177 /* using-declarations can add base methods to the method vec,
1178 and we don't want those here. */
1179 && DECL_CONTEXT (fn) == class_specialization)
1180 return fn;
1182 return NULL_TREE;
1184 else
1186 spec_entry *found;
1187 spec_entry elt;
1188 htab_t specializations;
1190 elt.tmpl = tmpl;
1191 elt.args = args;
1192 elt.spec = NULL_TREE;
1194 if (DECL_CLASS_TEMPLATE_P (tmpl))
1195 specializations = type_specializations;
1196 else
1197 specializations = decl_specializations;
1199 if (hash == 0)
1200 hash = hash_specialization (&elt);
1201 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1202 if (found)
1203 return found->spec;
1206 return NULL_TREE;
1209 /* Like retrieve_specialization, but for local declarations. */
1211 tree
1212 retrieve_local_specialization (tree tmpl)
1214 void **slot;
1216 if (local_specializations == NULL)
1217 return NULL_TREE;
1219 slot = pointer_map_contains (local_specializations, tmpl);
1220 return slot ? (tree) *slot : NULL_TREE;
1223 /* Returns nonzero iff DECL is a specialization of TMPL. */
1226 is_specialization_of (tree decl, tree tmpl)
1228 tree t;
1230 if (TREE_CODE (decl) == FUNCTION_DECL)
1232 for (t = decl;
1233 t != NULL_TREE;
1234 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1235 if (t == tmpl)
1236 return 1;
1238 else
1240 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1242 for (t = TREE_TYPE (decl);
1243 t != NULL_TREE;
1244 t = CLASSTYPE_USE_TEMPLATE (t)
1245 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1246 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1247 return 1;
1250 return 0;
1253 /* Returns nonzero iff DECL is a specialization of friend declaration
1254 FRIEND_DECL according to [temp.friend]. */
1256 bool
1257 is_specialization_of_friend (tree decl, tree friend_decl)
1259 bool need_template = true;
1260 int template_depth;
1262 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1263 || TREE_CODE (decl) == TYPE_DECL);
1265 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1266 of a template class, we want to check if DECL is a specialization
1267 if this. */
1268 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1269 && DECL_TEMPLATE_INFO (friend_decl)
1270 && !DECL_USE_TEMPLATE (friend_decl))
1272 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1273 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1274 need_template = false;
1276 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1277 && !PRIMARY_TEMPLATE_P (friend_decl))
1278 need_template = false;
1280 /* There is nothing to do if this is not a template friend. */
1281 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1282 return false;
1284 if (is_specialization_of (decl, friend_decl))
1285 return true;
1287 /* [temp.friend/6]
1288 A member of a class template may be declared to be a friend of a
1289 non-template class. In this case, the corresponding member of
1290 every specialization of the class template is a friend of the
1291 class granting friendship.
1293 For example, given a template friend declaration
1295 template <class T> friend void A<T>::f();
1297 the member function below is considered a friend
1299 template <> struct A<int> {
1300 void f();
1303 For this type of template friend, TEMPLATE_DEPTH below will be
1304 nonzero. To determine if DECL is a friend of FRIEND, we first
1305 check if the enclosing class is a specialization of another. */
1307 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1308 if (template_depth
1309 && DECL_CLASS_SCOPE_P (decl)
1310 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1311 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1313 /* Next, we check the members themselves. In order to handle
1314 a few tricky cases, such as when FRIEND_DECL's are
1316 template <class T> friend void A<T>::g(T t);
1317 template <class T> template <T t> friend void A<T>::h();
1319 and DECL's are
1321 void A<int>::g(int);
1322 template <int> void A<int>::h();
1324 we need to figure out ARGS, the template arguments from
1325 the context of DECL. This is required for template substitution
1326 of `T' in the function parameter of `g' and template parameter
1327 of `h' in the above examples. Here ARGS corresponds to `int'. */
1329 tree context = DECL_CONTEXT (decl);
1330 tree args = NULL_TREE;
1331 int current_depth = 0;
1333 while (current_depth < template_depth)
1335 if (CLASSTYPE_TEMPLATE_INFO (context))
1337 if (current_depth == 0)
1338 args = TYPE_TI_ARGS (context);
1339 else
1340 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1341 current_depth++;
1343 context = TYPE_CONTEXT (context);
1346 if (TREE_CODE (decl) == FUNCTION_DECL)
1348 bool is_template;
1349 tree friend_type;
1350 tree decl_type;
1351 tree friend_args_type;
1352 tree decl_args_type;
1354 /* Make sure that both DECL and FRIEND_DECL are templates or
1355 non-templates. */
1356 is_template = DECL_TEMPLATE_INFO (decl)
1357 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1358 if (need_template ^ is_template)
1359 return false;
1360 else if (is_template)
1362 /* If both are templates, check template parameter list. */
1363 tree friend_parms
1364 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1365 args, tf_none);
1366 if (!comp_template_parms
1367 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1368 friend_parms))
1369 return false;
1371 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1373 else
1374 decl_type = TREE_TYPE (decl);
1376 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1377 tf_none, NULL_TREE);
1378 if (friend_type == error_mark_node)
1379 return false;
1381 /* Check if return types match. */
1382 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1383 return false;
1385 /* Check if function parameter types match, ignoring the
1386 `this' parameter. */
1387 friend_args_type = TYPE_ARG_TYPES (friend_type);
1388 decl_args_type = TYPE_ARG_TYPES (decl_type);
1389 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1390 friend_args_type = TREE_CHAIN (friend_args_type);
1391 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1392 decl_args_type = TREE_CHAIN (decl_args_type);
1394 return compparms (decl_args_type, friend_args_type);
1396 else
1398 /* DECL is a TYPE_DECL */
1399 bool is_template;
1400 tree decl_type = TREE_TYPE (decl);
1402 /* Make sure that both DECL and FRIEND_DECL are templates or
1403 non-templates. */
1404 is_template
1405 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1406 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1408 if (need_template ^ is_template)
1409 return false;
1410 else if (is_template)
1412 tree friend_parms;
1413 /* If both are templates, check the name of the two
1414 TEMPLATE_DECL's first because is_friend didn't. */
1415 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1416 != DECL_NAME (friend_decl))
1417 return false;
1419 /* Now check template parameter list. */
1420 friend_parms
1421 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1422 args, tf_none);
1423 return comp_template_parms
1424 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1425 friend_parms);
1427 else
1428 return (DECL_NAME (decl)
1429 == DECL_NAME (friend_decl));
1432 return false;
1435 /* Register the specialization SPEC as a specialization of TMPL with
1436 the indicated ARGS. IS_FRIEND indicates whether the specialization
1437 is actually just a friend declaration. Returns SPEC, or an
1438 equivalent prior declaration, if available.
1440 We also store instantiations of field packs in the hash table, even
1441 though they are not themselves templates, to make lookup easier. */
1443 static tree
1444 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1445 hashval_t hash)
1447 tree fn;
1448 void **slot = NULL;
1449 spec_entry elt;
1451 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1452 || (TREE_CODE (tmpl) == FIELD_DECL
1453 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1455 if (TREE_CODE (spec) == FUNCTION_DECL
1456 && uses_template_parms (DECL_TI_ARGS (spec)))
1457 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1458 register it; we want the corresponding TEMPLATE_DECL instead.
1459 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1460 the more obvious `uses_template_parms (spec)' to avoid problems
1461 with default function arguments. In particular, given
1462 something like this:
1464 template <class T> void f(T t1, T t = T())
1466 the default argument expression is not substituted for in an
1467 instantiation unless and until it is actually needed. */
1468 return spec;
1470 if (optimize_specialization_lookup_p (tmpl))
1471 /* We don't put these specializations in the hash table, but we might
1472 want to give an error about a mismatch. */
1473 fn = retrieve_specialization (tmpl, args, 0);
1474 else
1476 elt.tmpl = tmpl;
1477 elt.args = args;
1478 elt.spec = spec;
1480 if (hash == 0)
1481 hash = hash_specialization (&elt);
1483 slot =
1484 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1485 if (*slot)
1486 fn = ((spec_entry *) *slot)->spec;
1487 else
1488 fn = NULL_TREE;
1491 /* We can sometimes try to re-register a specialization that we've
1492 already got. In particular, regenerate_decl_from_template calls
1493 duplicate_decls which will update the specialization list. But,
1494 we'll still get called again here anyhow. It's more convenient
1495 to simply allow this than to try to prevent it. */
1496 if (fn == spec)
1497 return spec;
1498 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1500 if (DECL_TEMPLATE_INSTANTIATION (fn))
1502 if (DECL_ODR_USED (fn)
1503 || DECL_EXPLICIT_INSTANTIATION (fn))
1505 error ("specialization of %qD after instantiation",
1506 fn);
1507 return error_mark_node;
1509 else
1511 tree clone;
1512 /* This situation should occur only if the first
1513 specialization is an implicit instantiation, the
1514 second is an explicit specialization, and the
1515 implicit instantiation has not yet been used. That
1516 situation can occur if we have implicitly
1517 instantiated a member function and then specialized
1518 it later.
1520 We can also wind up here if a friend declaration that
1521 looked like an instantiation turns out to be a
1522 specialization:
1524 template <class T> void foo(T);
1525 class S { friend void foo<>(int) };
1526 template <> void foo(int);
1528 We transform the existing DECL in place so that any
1529 pointers to it become pointers to the updated
1530 declaration.
1532 If there was a definition for the template, but not
1533 for the specialization, we want this to look as if
1534 there were no definition, and vice versa. */
1535 DECL_INITIAL (fn) = NULL_TREE;
1536 duplicate_decls (spec, fn, is_friend);
1537 /* The call to duplicate_decls will have applied
1538 [temp.expl.spec]:
1540 An explicit specialization of a function template
1541 is inline only if it is explicitly declared to be,
1542 and independently of whether its function template
1545 to the primary function; now copy the inline bits to
1546 the various clones. */
1547 FOR_EACH_CLONE (clone, fn)
1549 DECL_DECLARED_INLINE_P (clone)
1550 = DECL_DECLARED_INLINE_P (fn);
1551 DECL_SOURCE_LOCATION (clone)
1552 = DECL_SOURCE_LOCATION (fn);
1553 DECL_DELETED_FN (clone)
1554 = DECL_DELETED_FN (fn);
1556 check_specialization_namespace (tmpl);
1558 return fn;
1561 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1563 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1564 /* Dup decl failed, but this is a new definition. Set the
1565 line number so any errors match this new
1566 definition. */
1567 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1569 return fn;
1572 else if (fn)
1573 return duplicate_decls (spec, fn, is_friend);
1575 /* A specialization must be declared in the same namespace as the
1576 template it is specializing. */
1577 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1578 && !check_specialization_namespace (tmpl))
1579 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1581 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1583 spec_entry *entry = ggc_alloc_spec_entry ();
1584 gcc_assert (tmpl && args && spec);
1585 *entry = elt;
1586 *slot = entry;
1587 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1588 && PRIMARY_TEMPLATE_P (tmpl)
1589 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1590 /* TMPL is a forward declaration of a template function; keep a list
1591 of all specializations in case we need to reassign them to a friend
1592 template later in tsubst_friend_function. */
1593 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1594 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1597 return spec;
1600 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1601 TMPL and ARGS members, ignores SPEC. */
1603 int comparing_specializations;
1605 static int
1606 eq_specializations (const void *p1, const void *p2)
1608 const spec_entry *e1 = (const spec_entry *)p1;
1609 const spec_entry *e2 = (const spec_entry *)p2;
1610 int equal;
1612 ++comparing_specializations;
1613 equal = (e1->tmpl == e2->tmpl
1614 && comp_template_args (e1->args, e2->args));
1615 --comparing_specializations;
1617 return equal;
1620 /* Returns a hash for a template TMPL and template arguments ARGS. */
1622 static hashval_t
1623 hash_tmpl_and_args (tree tmpl, tree args)
1625 hashval_t val = DECL_UID (tmpl);
1626 return iterative_hash_template_arg (args, val);
1629 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1630 ignoring SPEC. */
1632 static hashval_t
1633 hash_specialization (const void *p)
1635 const spec_entry *e = (const spec_entry *)p;
1636 return hash_tmpl_and_args (e->tmpl, e->args);
1639 /* Recursively calculate a hash value for a template argument ARG, for use
1640 in the hash tables of template specializations. */
1642 hashval_t
1643 iterative_hash_template_arg (tree arg, hashval_t val)
1645 unsigned HOST_WIDE_INT i;
1646 enum tree_code code;
1647 char tclass;
1649 if (arg == NULL_TREE)
1650 return iterative_hash_object (arg, val);
1652 if (!TYPE_P (arg))
1653 STRIP_NOPS (arg);
1655 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1656 /* We can get one of these when re-hashing a previous entry in the middle
1657 of substituting into a pack expansion. Just look through it. */
1658 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1660 code = TREE_CODE (arg);
1661 tclass = TREE_CODE_CLASS (code);
1663 val = iterative_hash_object (code, val);
1665 switch (code)
1667 case ERROR_MARK:
1668 return val;
1670 case IDENTIFIER_NODE:
1671 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1673 case TREE_VEC:
1675 int i, len = TREE_VEC_LENGTH (arg);
1676 for (i = 0; i < len; ++i)
1677 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1678 return val;
1681 case TYPE_PACK_EXPANSION:
1682 case EXPR_PACK_EXPANSION:
1683 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1684 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1686 case TYPE_ARGUMENT_PACK:
1687 case NONTYPE_ARGUMENT_PACK:
1688 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1690 case TREE_LIST:
1691 for (; arg; arg = TREE_CHAIN (arg))
1692 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1693 return val;
1695 case OVERLOAD:
1696 for (; arg; arg = OVL_NEXT (arg))
1697 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1698 return val;
1700 case CONSTRUCTOR:
1702 tree field, value;
1703 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1705 val = iterative_hash_template_arg (field, val);
1706 val = iterative_hash_template_arg (value, val);
1708 return val;
1711 case PARM_DECL:
1712 if (!DECL_ARTIFICIAL (arg))
1714 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1715 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1717 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1719 case TARGET_EXPR:
1720 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1722 case PTRMEM_CST:
1723 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1724 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1726 case TEMPLATE_PARM_INDEX:
1727 val = iterative_hash_template_arg
1728 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1729 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1730 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1732 case TRAIT_EXPR:
1733 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1734 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1735 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1737 case BASELINK:
1738 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1739 val);
1740 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1741 val);
1743 case MODOP_EXPR:
1744 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1745 code = TREE_CODE (TREE_OPERAND (arg, 1));
1746 val = iterative_hash_object (code, val);
1747 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1749 case LAMBDA_EXPR:
1750 /* A lambda can't appear in a template arg, but don't crash on
1751 erroneous input. */
1752 gcc_assert (seen_error ());
1753 return val;
1755 case CAST_EXPR:
1756 case IMPLICIT_CONV_EXPR:
1757 case STATIC_CAST_EXPR:
1758 case REINTERPRET_CAST_EXPR:
1759 case CONST_CAST_EXPR:
1760 case DYNAMIC_CAST_EXPR:
1761 case NEW_EXPR:
1762 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1763 /* Now hash operands as usual. */
1764 break;
1766 default:
1767 break;
1770 switch (tclass)
1772 case tcc_type:
1773 if (TYPE_CANONICAL (arg))
1774 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1775 val);
1776 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1777 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1778 /* Otherwise just compare the types during lookup. */
1779 return val;
1781 case tcc_declaration:
1782 case tcc_constant:
1783 return iterative_hash_expr (arg, val);
1785 default:
1786 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1788 unsigned n = cp_tree_operand_length (arg);
1789 for (i = 0; i < n; ++i)
1790 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1791 return val;
1794 gcc_unreachable ();
1795 return 0;
1798 /* Unregister the specialization SPEC as a specialization of TMPL.
1799 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1800 if the SPEC was listed as a specialization of TMPL.
1802 Note that SPEC has been ggc_freed, so we can't look inside it. */
1804 bool
1805 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1807 spec_entry *entry;
1808 spec_entry elt;
1810 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1811 elt.args = TI_ARGS (tinfo);
1812 elt.spec = NULL_TREE;
1814 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1815 if (entry != NULL)
1817 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1818 gcc_assert (new_spec != NULL_TREE);
1819 entry->spec = new_spec;
1820 return 1;
1823 return 0;
1826 /* Like register_specialization, but for local declarations. We are
1827 registering SPEC, an instantiation of TMPL. */
1829 void
1830 register_local_specialization (tree spec, tree tmpl)
1832 void **slot;
1834 slot = pointer_map_insert (local_specializations, tmpl);
1835 *slot = spec;
1838 /* TYPE is a class type. Returns true if TYPE is an explicitly
1839 specialized class. */
1841 bool
1842 explicit_class_specialization_p (tree type)
1844 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1845 return false;
1846 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1849 /* Print the list of functions at FNS, going through all the overloads
1850 for each element of the list. Alternatively, FNS can not be a
1851 TREE_LIST, in which case it will be printed together with all the
1852 overloads.
1854 MORE and *STR should respectively be FALSE and NULL when the function
1855 is called from the outside. They are used internally on recursive
1856 calls. print_candidates manages the two parameters and leaves NULL
1857 in *STR when it ends. */
1859 static void
1860 print_candidates_1 (tree fns, bool more, const char **str)
1862 tree fn, fn2;
1863 char *spaces = NULL;
1865 for (fn = fns; fn; fn = OVL_NEXT (fn))
1866 if (TREE_CODE (fn) == TREE_LIST)
1868 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1869 print_candidates_1 (TREE_VALUE (fn2),
1870 TREE_CHAIN (fn2) || more, str);
1872 else
1874 tree cand = OVL_CURRENT (fn);
1875 if (!*str)
1877 /* Pick the prefix string. */
1878 if (!more && !OVL_NEXT (fns))
1880 inform (DECL_SOURCE_LOCATION (cand),
1881 "candidate is: %#D", cand);
1882 continue;
1885 *str = _("candidates are:");
1886 spaces = get_spaces (*str);
1888 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1889 *str = spaces ? spaces : *str;
1892 if (!more)
1894 free (spaces);
1895 *str = NULL;
1899 /* Print the list of candidate FNS in an error message. FNS can also
1900 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1902 void
1903 print_candidates (tree fns)
1905 const char *str = NULL;
1906 print_candidates_1 (fns, false, &str);
1907 gcc_assert (str == NULL);
1910 /* Returns the template (one of the functions given by TEMPLATE_ID)
1911 which can be specialized to match the indicated DECL with the
1912 explicit template args given in TEMPLATE_ID. The DECL may be
1913 NULL_TREE if none is available. In that case, the functions in
1914 TEMPLATE_ID are non-members.
1916 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1917 specialization of a member template.
1919 The TEMPLATE_COUNT is the number of references to qualifying
1920 template classes that appeared in the name of the function. See
1921 check_explicit_specialization for a more accurate description.
1923 TSK indicates what kind of template declaration (if any) is being
1924 declared. TSK_TEMPLATE indicates that the declaration given by
1925 DECL, though a FUNCTION_DECL, has template parameters, and is
1926 therefore a template function.
1928 The template args (those explicitly specified and those deduced)
1929 are output in a newly created vector *TARGS_OUT.
1931 If it is impossible to determine the result, an error message is
1932 issued. The error_mark_node is returned to indicate failure. */
1934 static tree
1935 determine_specialization (tree template_id,
1936 tree decl,
1937 tree* targs_out,
1938 int need_member_template,
1939 int template_count,
1940 tmpl_spec_kind tsk)
1942 tree fns;
1943 tree targs;
1944 tree explicit_targs;
1945 tree candidates = NULL_TREE;
1946 /* A TREE_LIST of templates of which DECL may be a specialization.
1947 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1948 corresponding TREE_PURPOSE is the set of template arguments that,
1949 when used to instantiate the template, would produce a function
1950 with the signature of DECL. */
1951 tree templates = NULL_TREE;
1952 int header_count;
1953 cp_binding_level *b;
1955 *targs_out = NULL_TREE;
1957 if (template_id == error_mark_node || decl == error_mark_node)
1958 return error_mark_node;
1960 /* We shouldn't be specializing a member template of an
1961 unspecialized class template; we already gave an error in
1962 check_specialization_scope, now avoid crashing. */
1963 if (template_count && DECL_CLASS_SCOPE_P (decl)
1964 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1966 gcc_assert (errorcount);
1967 return error_mark_node;
1970 fns = TREE_OPERAND (template_id, 0);
1971 explicit_targs = TREE_OPERAND (template_id, 1);
1973 if (fns == error_mark_node)
1974 return error_mark_node;
1976 /* Check for baselinks. */
1977 if (BASELINK_P (fns))
1978 fns = BASELINK_FUNCTIONS (fns);
1980 if (!is_overloaded_fn (fns))
1982 error ("%qD is not a function template", fns);
1983 return error_mark_node;
1986 /* Count the number of template headers specified for this
1987 specialization. */
1988 header_count = 0;
1989 for (b = current_binding_level;
1990 b->kind == sk_template_parms;
1991 b = b->level_chain)
1992 ++header_count;
1994 for (; fns; fns = OVL_NEXT (fns))
1996 tree fn = OVL_CURRENT (fns);
1998 if (TREE_CODE (fn) == TEMPLATE_DECL)
2000 tree decl_arg_types;
2001 tree fn_arg_types;
2002 tree insttype;
2004 /* In case of explicit specialization, we need to check if
2005 the number of template headers appearing in the specialization
2006 is correct. This is usually done in check_explicit_specialization,
2007 but the check done there cannot be exhaustive when specializing
2008 member functions. Consider the following code:
2010 template <> void A<int>::f(int);
2011 template <> template <> void A<int>::f(int);
2013 Assuming that A<int> is not itself an explicit specialization
2014 already, the first line specializes "f" which is a non-template
2015 member function, whilst the second line specializes "f" which
2016 is a template member function. So both lines are syntactically
2017 correct, and check_explicit_specialization does not reject
2018 them.
2020 Here, we can do better, as we are matching the specialization
2021 against the declarations. We count the number of template
2022 headers, and we check if they match TEMPLATE_COUNT + 1
2023 (TEMPLATE_COUNT is the number of qualifying template classes,
2024 plus there must be another header for the member template
2025 itself).
2027 Notice that if header_count is zero, this is not a
2028 specialization but rather a template instantiation, so there
2029 is no check we can perform here. */
2030 if (header_count && header_count != template_count + 1)
2031 continue;
2033 /* Check that the number of template arguments at the
2034 innermost level for DECL is the same as for FN. */
2035 if (current_binding_level->kind == sk_template_parms
2036 && !current_binding_level->explicit_spec_p
2037 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2038 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2039 (current_template_parms))))
2040 continue;
2042 /* DECL might be a specialization of FN. */
2043 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2044 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2046 /* For a non-static member function, we need to make sure
2047 that the const qualification is the same. Since
2048 get_bindings does not try to merge the "this" parameter,
2049 we must do the comparison explicitly. */
2050 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2051 && !same_type_p (TREE_VALUE (fn_arg_types),
2052 TREE_VALUE (decl_arg_types)))
2053 continue;
2055 /* Skip the "this" parameter and, for constructors of
2056 classes with virtual bases, the VTT parameter. A
2057 full specialization of a constructor will have a VTT
2058 parameter, but a template never will. */
2059 decl_arg_types
2060 = skip_artificial_parms_for (decl, decl_arg_types);
2061 fn_arg_types
2062 = skip_artificial_parms_for (fn, fn_arg_types);
2064 /* Function templates cannot be specializations; there are
2065 no partial specializations of functions. Therefore, if
2066 the type of DECL does not match FN, there is no
2067 match. */
2068 if (tsk == tsk_template)
2070 if (compparms (fn_arg_types, decl_arg_types))
2071 candidates = tree_cons (NULL_TREE, fn, candidates);
2072 continue;
2075 /* See whether this function might be a specialization of this
2076 template. Suppress access control because we might be trying
2077 to make this specialization a friend, and we have already done
2078 access control for the declaration of the specialization. */
2079 push_deferring_access_checks (dk_no_check);
2080 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2081 pop_deferring_access_checks ();
2083 if (!targs)
2084 /* We cannot deduce template arguments that when used to
2085 specialize TMPL will produce DECL. */
2086 continue;
2088 // Make sure that the deduced arguments actually work. First,
2089 // check that any template constraints are satisfied.
2091 // TODO: Make sure that we get reasonable diagnostics for these
2092 // kinds of failures.
2093 if (!check_template_constraints (fn, targs))
2094 continue;
2096 // Then, try to form the new function type.
2097 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2098 if (insttype == error_mark_node)
2099 continue;
2100 fn_arg_types
2101 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2102 if (!compparms (fn_arg_types, decl_arg_types))
2103 continue;
2105 /* Save this template, and the arguments deduced. */
2106 templates = tree_cons (targs, fn, templates);
2108 else if (need_member_template)
2109 /* FN is an ordinary member function, and we need a
2110 specialization of a member template. */
2112 else if (TREE_CODE (fn) != FUNCTION_DECL)
2113 /* We can get IDENTIFIER_NODEs here in certain erroneous
2114 cases. */
2116 else if (!DECL_FUNCTION_MEMBER_P (fn))
2117 /* This is just an ordinary non-member function. Nothing can
2118 be a specialization of that. */
2120 else if (DECL_ARTIFICIAL (fn))
2121 /* Cannot specialize functions that are created implicitly. */
2123 else
2125 tree decl_arg_types;
2127 /* This is an ordinary member function. However, since
2128 we're here, we can assume its enclosing class is a
2129 template class. For example,
2131 template <typename T> struct S { void f(); };
2132 template <> void S<int>::f() {}
2134 Here, S<int>::f is a non-template, but S<int> is a
2135 template class. If FN has the same type as DECL, we
2136 might be in business. */
2138 if (!DECL_TEMPLATE_INFO (fn))
2139 /* Its enclosing class is an explicit specialization
2140 of a template class. This is not a candidate. */
2141 continue;
2143 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2144 TREE_TYPE (TREE_TYPE (fn))))
2145 /* The return types differ. */
2146 continue;
2148 /* Adjust the type of DECL in case FN is a static member. */
2149 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2150 if (DECL_STATIC_FUNCTION_P (fn)
2151 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2152 decl_arg_types = TREE_CHAIN (decl_arg_types);
2154 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2155 decl_arg_types))
2156 /* They match! */
2157 candidates = tree_cons (NULL_TREE, fn, candidates);
2161 if (templates && TREE_CHAIN (templates))
2163 /* We have:
2165 [temp.expl.spec]
2167 It is possible for a specialization with a given function
2168 signature to be instantiated from more than one function
2169 template. In such cases, explicit specification of the
2170 template arguments must be used to uniquely identify the
2171 function template specialization being specialized.
2173 Note that here, there's no suggestion that we're supposed to
2174 determine which of the candidate templates is most
2175 specialized. However, we, also have:
2177 [temp.func.order]
2179 Partial ordering of overloaded function template
2180 declarations is used in the following contexts to select
2181 the function template to which a function template
2182 specialization refers:
2184 -- when an explicit specialization refers to a function
2185 template.
2187 So, we do use the partial ordering rules, at least for now.
2188 This extension can only serve to make invalid programs valid,
2189 so it's safe. And, there is strong anecdotal evidence that
2190 the committee intended the partial ordering rules to apply;
2191 the EDG front end has that behavior, and John Spicer claims
2192 that the committee simply forgot to delete the wording in
2193 [temp.expl.spec]. */
2194 tree tmpl = most_specialized_instantiation (templates);
2195 if (tmpl != error_mark_node)
2197 templates = tmpl;
2198 TREE_CHAIN (templates) = NULL_TREE;
2202 if (templates == NULL_TREE && candidates == NULL_TREE)
2204 error ("template-id %qD for %q+D does not match any template "
2205 "declaration", template_id, decl);
2206 if (header_count && header_count != template_count + 1)
2207 inform (input_location, "saw %d %<template<>%>, need %d for "
2208 "specializing a member function template",
2209 header_count, template_count + 1);
2210 return error_mark_node;
2212 else if ((templates && TREE_CHAIN (templates))
2213 || (candidates && TREE_CHAIN (candidates))
2214 || (templates && candidates))
2216 error ("ambiguous template specialization %qD for %q+D",
2217 template_id, decl);
2218 candidates = chainon (candidates, templates);
2219 print_candidates (candidates);
2220 return error_mark_node;
2223 /* We have one, and exactly one, match. */
2224 if (candidates)
2226 tree fn = TREE_VALUE (candidates);
2227 *targs_out = copy_node (DECL_TI_ARGS (fn));
2228 /* DECL is a re-declaration or partial instantiation of a template
2229 function. */
2230 if (TREE_CODE (fn) == TEMPLATE_DECL)
2231 return fn;
2232 /* It was a specialization of an ordinary member function in a
2233 template class. */
2234 return DECL_TI_TEMPLATE (fn);
2237 /* It was a specialization of a template. */
2238 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2239 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2241 *targs_out = copy_node (targs);
2242 SET_TMPL_ARGS_LEVEL (*targs_out,
2243 TMPL_ARGS_DEPTH (*targs_out),
2244 TREE_PURPOSE (templates));
2246 else
2247 *targs_out = TREE_PURPOSE (templates);
2248 return TREE_VALUE (templates);
2251 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2252 but with the default argument values filled in from those in the
2253 TMPL_TYPES. */
2255 static tree
2256 copy_default_args_to_explicit_spec_1 (tree spec_types,
2257 tree tmpl_types)
2259 tree new_spec_types;
2261 if (!spec_types)
2262 return NULL_TREE;
2264 if (spec_types == void_list_node)
2265 return void_list_node;
2267 /* Substitute into the rest of the list. */
2268 new_spec_types =
2269 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2270 TREE_CHAIN (tmpl_types));
2272 /* Add the default argument for this parameter. */
2273 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2274 TREE_VALUE (spec_types),
2275 new_spec_types);
2278 /* DECL is an explicit specialization. Replicate default arguments
2279 from the template it specializes. (That way, code like:
2281 template <class T> void f(T = 3);
2282 template <> void f(double);
2283 void g () { f (); }
2285 works, as required.) An alternative approach would be to look up
2286 the correct default arguments at the call-site, but this approach
2287 is consistent with how implicit instantiations are handled. */
2289 static void
2290 copy_default_args_to_explicit_spec (tree decl)
2292 tree tmpl;
2293 tree spec_types;
2294 tree tmpl_types;
2295 tree new_spec_types;
2296 tree old_type;
2297 tree new_type;
2298 tree t;
2299 tree object_type = NULL_TREE;
2300 tree in_charge = NULL_TREE;
2301 tree vtt = NULL_TREE;
2303 /* See if there's anything we need to do. */
2304 tmpl = DECL_TI_TEMPLATE (decl);
2305 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2306 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2307 if (TREE_PURPOSE (t))
2308 break;
2309 if (!t)
2310 return;
2312 old_type = TREE_TYPE (decl);
2313 spec_types = TYPE_ARG_TYPES (old_type);
2315 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2317 /* Remove the this pointer, but remember the object's type for
2318 CV quals. */
2319 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2320 spec_types = TREE_CHAIN (spec_types);
2321 tmpl_types = TREE_CHAIN (tmpl_types);
2323 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2325 /* DECL may contain more parameters than TMPL due to the extra
2326 in-charge parameter in constructors and destructors. */
2327 in_charge = spec_types;
2328 spec_types = TREE_CHAIN (spec_types);
2330 if (DECL_HAS_VTT_PARM_P (decl))
2332 vtt = spec_types;
2333 spec_types = TREE_CHAIN (spec_types);
2337 /* Compute the merged default arguments. */
2338 new_spec_types =
2339 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2341 /* Compute the new FUNCTION_TYPE. */
2342 if (object_type)
2344 if (vtt)
2345 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2346 TREE_VALUE (vtt),
2347 new_spec_types);
2349 if (in_charge)
2350 /* Put the in-charge parameter back. */
2351 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2352 TREE_VALUE (in_charge),
2353 new_spec_types);
2355 new_type = build_method_type_directly (object_type,
2356 TREE_TYPE (old_type),
2357 new_spec_types);
2359 else
2360 new_type = build_function_type (TREE_TYPE (old_type),
2361 new_spec_types);
2362 new_type = cp_build_type_attribute_variant (new_type,
2363 TYPE_ATTRIBUTES (old_type));
2364 new_type = build_exception_variant (new_type,
2365 TYPE_RAISES_EXCEPTIONS (old_type));
2366 TREE_TYPE (decl) = new_type;
2369 /* Return the number of template headers we expect to see for a definition
2370 or specialization of CTYPE or one of its non-template members. */
2373 num_template_headers_for_class (tree ctype)
2375 int num_templates = 0;
2377 while (ctype && CLASS_TYPE_P (ctype))
2379 /* You're supposed to have one `template <...>' for every
2380 template class, but you don't need one for a full
2381 specialization. For example:
2383 template <class T> struct S{};
2384 template <> struct S<int> { void f(); };
2385 void S<int>::f () {}
2387 is correct; there shouldn't be a `template <>' for the
2388 definition of `S<int>::f'. */
2389 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2390 /* If CTYPE does not have template information of any
2391 kind, then it is not a template, nor is it nested
2392 within a template. */
2393 break;
2394 if (explicit_class_specialization_p (ctype))
2395 break;
2396 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2397 ++num_templates;
2399 ctype = TYPE_CONTEXT (ctype);
2402 return num_templates;
2405 /* Do a simple sanity check on the template headers that precede the
2406 variable declaration DECL. */
2408 void
2409 check_template_variable (tree decl)
2411 tree ctx = CP_DECL_CONTEXT (decl);
2412 int wanted = num_template_headers_for_class (ctx);
2413 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2414 permerror (DECL_SOURCE_LOCATION (decl),
2415 "%qD is not a static data member of a class template", decl);
2416 else if (template_header_count > wanted)
2418 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2419 "too many template headers for %D (should be %d)",
2420 decl, wanted);
2421 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2422 inform (DECL_SOURCE_LOCATION (decl),
2423 "members of an explicitly specialized class are defined "
2424 "without a template header");
2428 /* Check to see if the function just declared, as indicated in
2429 DECLARATOR, and in DECL, is a specialization of a function
2430 template. We may also discover that the declaration is an explicit
2431 instantiation at this point.
2433 Returns DECL, or an equivalent declaration that should be used
2434 instead if all goes well. Issues an error message if something is
2435 amiss. Returns error_mark_node if the error is not easily
2436 recoverable.
2438 FLAGS is a bitmask consisting of the following flags:
2440 2: The function has a definition.
2441 4: The function is a friend.
2443 The TEMPLATE_COUNT is the number of references to qualifying
2444 template classes that appeared in the name of the function. For
2445 example, in
2447 template <class T> struct S { void f(); };
2448 void S<int>::f();
2450 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2451 classes are not counted in the TEMPLATE_COUNT, so that in
2453 template <class T> struct S {};
2454 template <> struct S<int> { void f(); }
2455 template <> void S<int>::f();
2457 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2458 invalid; there should be no template <>.)
2460 If the function is a specialization, it is marked as such via
2461 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2462 is set up correctly, and it is added to the list of specializations
2463 for that template. */
2465 tree
2466 check_explicit_specialization (tree declarator,
2467 tree decl,
2468 int template_count,
2469 int flags)
2471 int have_def = flags & 2;
2472 int is_friend = flags & 4;
2473 bool is_concept = flags & 8;
2474 int specialization = 0;
2475 int explicit_instantiation = 0;
2476 int member_specialization = 0;
2477 tree ctype = DECL_CLASS_CONTEXT (decl);
2478 tree dname = DECL_NAME (decl);
2479 tmpl_spec_kind tsk;
2481 if (is_friend)
2483 if (!processing_specialization)
2484 tsk = tsk_none;
2485 else
2486 tsk = tsk_excessive_parms;
2488 else
2489 tsk = current_tmpl_spec_kind (template_count);
2491 switch (tsk)
2493 case tsk_none:
2494 if (processing_specialization)
2496 specialization = 1;
2497 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2499 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2501 if (is_friend)
2502 /* This could be something like:
2504 template <class T> void f(T);
2505 class S { friend void f<>(int); } */
2506 specialization = 1;
2507 else
2509 /* This case handles bogus declarations like template <>
2510 template <class T> void f<int>(); */
2512 error ("template-id %qD in declaration of primary template",
2513 declarator);
2514 return decl;
2517 break;
2519 case tsk_invalid_member_spec:
2520 /* The error has already been reported in
2521 check_specialization_scope. */
2522 return error_mark_node;
2524 case tsk_invalid_expl_inst:
2525 error ("template parameter list used in explicit instantiation");
2527 /* Fall through. */
2529 case tsk_expl_inst:
2530 if (have_def)
2531 error ("definition provided for explicit instantiation");
2533 explicit_instantiation = 1;
2534 break;
2536 case tsk_excessive_parms:
2537 case tsk_insufficient_parms:
2538 if (tsk == tsk_excessive_parms)
2539 error ("too many template parameter lists in declaration of %qD",
2540 decl);
2541 else if (template_header_count)
2542 error("too few template parameter lists in declaration of %qD", decl);
2543 else
2544 error("explicit specialization of %qD must be introduced by "
2545 "%<template <>%>", decl);
2547 /* Fall through. */
2548 case tsk_expl_spec:
2549 if (is_concept)
2550 error ("explicit specialization declared %<concept%>");
2551 // if (DECL_DECLARED_CONCEPT_P (decl))
2552 // error ("explicit specialization of concept %qD", decl);
2554 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2555 if (ctype)
2556 member_specialization = 1;
2557 else
2558 specialization = 1;
2559 break;
2561 case tsk_template:
2562 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2564 /* This case handles bogus declarations like template <>
2565 template <class T> void f<int>(); */
2567 if (uses_template_parms (declarator))
2568 error ("function template partial specialization %qD "
2569 "is not allowed", declarator);
2570 else
2571 error ("template-id %qD in declaration of primary template",
2572 declarator);
2573 return decl;
2576 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2577 /* This is a specialization of a member template, without
2578 specialization the containing class. Something like:
2580 template <class T> struct S {
2581 template <class U> void f (U);
2583 template <> template <class U> void S<int>::f(U) {}
2585 That's a specialization -- but of the entire template. */
2586 specialization = 1;
2587 break;
2589 default:
2590 gcc_unreachable ();
2593 if (specialization || member_specialization)
2595 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2596 for (; t; t = TREE_CHAIN (t))
2597 if (TREE_PURPOSE (t))
2599 permerror (input_location,
2600 "default argument specified in explicit specialization");
2601 break;
2605 if (specialization || member_specialization || explicit_instantiation)
2607 tree tmpl = NULL_TREE;
2608 tree targs = NULL_TREE;
2610 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2611 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2613 tree fns;
2615 gcc_assert (identifier_p (declarator));
2616 if (ctype)
2617 fns = dname;
2618 else
2620 /* If there is no class context, the explicit instantiation
2621 must be at namespace scope. */
2622 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2624 /* Find the namespace binding, using the declaration
2625 context. */
2626 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2627 false, true);
2628 if (fns == error_mark_node || !is_overloaded_fn (fns))
2630 error ("%qD is not a template function", dname);
2631 fns = error_mark_node;
2633 else
2635 tree fn = OVL_CURRENT (fns);
2636 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2637 CP_DECL_CONTEXT (fn)))
2638 error ("%qD is not declared in %qD",
2639 decl, current_namespace);
2643 declarator = lookup_template_function (fns, NULL_TREE);
2646 if (declarator == error_mark_node)
2647 return error_mark_node;
2649 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2651 if (!explicit_instantiation)
2652 /* A specialization in class scope. This is invalid,
2653 but the error will already have been flagged by
2654 check_specialization_scope. */
2655 return error_mark_node;
2656 else
2658 /* It's not valid to write an explicit instantiation in
2659 class scope, e.g.:
2661 class C { template void f(); }
2663 This case is caught by the parser. However, on
2664 something like:
2666 template class C { void f(); };
2668 (which is invalid) we can get here. The error will be
2669 issued later. */
2673 return decl;
2675 else if (ctype != NULL_TREE
2676 && (identifier_p (TREE_OPERAND (declarator, 0))))
2678 /* Find the list of functions in ctype that have the same
2679 name as the declared function. */
2680 tree name = TREE_OPERAND (declarator, 0);
2681 tree fns = NULL_TREE;
2682 int idx;
2684 if (constructor_name_p (name, ctype))
2686 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2688 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2689 : !CLASSTYPE_DESTRUCTORS (ctype))
2691 /* From [temp.expl.spec]:
2693 If such an explicit specialization for the member
2694 of a class template names an implicitly-declared
2695 special member function (clause _special_), the
2696 program is ill-formed.
2698 Similar language is found in [temp.explicit]. */
2699 error ("specialization of implicitly-declared special member function");
2700 return error_mark_node;
2703 name = is_constructor ? ctor_identifier : dtor_identifier;
2706 if (!DECL_CONV_FN_P (decl))
2708 idx = lookup_fnfields_1 (ctype, name);
2709 if (idx >= 0)
2710 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2712 else
2714 vec<tree, va_gc> *methods;
2715 tree ovl;
2717 /* For a type-conversion operator, we cannot do a
2718 name-based lookup. We might be looking for `operator
2719 int' which will be a specialization of `operator T'.
2720 So, we find *all* the conversion operators, and then
2721 select from them. */
2722 fns = NULL_TREE;
2724 methods = CLASSTYPE_METHOD_VEC (ctype);
2725 if (methods)
2726 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2727 methods->iterate (idx, &ovl);
2728 ++idx)
2730 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2731 /* There are no more conversion functions. */
2732 break;
2734 /* Glue all these conversion functions together
2735 with those we already have. */
2736 for (; ovl; ovl = OVL_NEXT (ovl))
2737 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2741 if (fns == NULL_TREE)
2743 error ("no member function %qD declared in %qT", name, ctype);
2744 return error_mark_node;
2746 else
2747 TREE_OPERAND (declarator, 0) = fns;
2750 /* Figure out what exactly is being specialized at this point.
2751 Note that for an explicit instantiation, even one for a
2752 member function, we cannot tell apriori whether the
2753 instantiation is for a member template, or just a member
2754 function of a template class. Even if a member template is
2755 being instantiated, the member template arguments may be
2756 elided if they can be deduced from the rest of the
2757 declaration. */
2758 tmpl = determine_specialization (declarator, decl,
2759 &targs,
2760 member_specialization,
2761 template_count,
2762 tsk);
2764 if (!tmpl || tmpl == error_mark_node)
2765 /* We couldn't figure out what this declaration was
2766 specializing. */
2767 return error_mark_node;
2768 else
2770 tree gen_tmpl = most_general_template (tmpl);
2772 if (explicit_instantiation)
2774 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2775 is done by do_decl_instantiation later. */
2777 int arg_depth = TMPL_ARGS_DEPTH (targs);
2778 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2780 if (arg_depth > parm_depth)
2782 /* If TMPL is not the most general template (for
2783 example, if TMPL is a friend template that is
2784 injected into namespace scope), then there will
2785 be too many levels of TARGS. Remove some of them
2786 here. */
2787 int i;
2788 tree new_targs;
2790 new_targs = make_tree_vec (parm_depth);
2791 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2792 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2793 = TREE_VEC_ELT (targs, i);
2794 targs = new_targs;
2797 return instantiate_template (tmpl, targs, tf_error);
2800 /* If we thought that the DECL was a member function, but it
2801 turns out to be specializing a static member function,
2802 make DECL a static member function as well. */
2803 if (DECL_STATIC_FUNCTION_P (tmpl)
2804 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2805 revert_static_member_fn (decl);
2807 /* If this is a specialization of a member template of a
2808 template class, we want to return the TEMPLATE_DECL, not
2809 the specialization of it. */
2810 if (tsk == tsk_template)
2812 tree result = DECL_TEMPLATE_RESULT (tmpl);
2813 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2814 DECL_INITIAL (result) = NULL_TREE;
2815 if (have_def)
2817 tree parm;
2818 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2819 DECL_SOURCE_LOCATION (result)
2820 = DECL_SOURCE_LOCATION (decl);
2821 /* We want to use the argument list specified in the
2822 definition, not in the original declaration. */
2823 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2824 for (parm = DECL_ARGUMENTS (result); parm;
2825 parm = DECL_CHAIN (parm))
2826 DECL_CONTEXT (parm) = result;
2828 return register_specialization (tmpl, gen_tmpl, targs,
2829 is_friend, 0);
2832 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2833 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2835 /* Inherit default function arguments from the template
2836 DECL is specializing. */
2837 copy_default_args_to_explicit_spec (decl);
2839 /* This specialization has the same protection as the
2840 template it specializes. */
2841 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2842 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2844 /* 7.1.1-1 [dcl.stc]
2846 A storage-class-specifier shall not be specified in an
2847 explicit specialization...
2849 The parser rejects these, so unless action is taken here,
2850 explicit function specializations will always appear with
2851 global linkage.
2853 The action recommended by the C++ CWG in response to C++
2854 defect report 605 is to make the storage class and linkage
2855 of the explicit specialization match the templated function:
2857 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2859 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2861 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2862 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2864 // A concept cannot be specialized.
2865 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
2867 error ("explicit specialization of concept %qD", gen_tmpl);
2868 return error_mark_node;
2871 /* This specialization has the same linkage and visibility as
2872 the function template it specializes. */
2873 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2874 if (! TREE_PUBLIC (decl))
2876 DECL_INTERFACE_KNOWN (decl) = 1;
2877 DECL_NOT_REALLY_EXTERN (decl) = 1;
2879 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2880 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2882 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2883 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2887 /* If DECL is a friend declaration, declared using an
2888 unqualified name, the namespace associated with DECL may
2889 have been set incorrectly. For example, in:
2891 template <typename T> void f(T);
2892 namespace N {
2893 struct S { friend void f<int>(int); }
2896 we will have set the DECL_CONTEXT for the friend
2897 declaration to N, rather than to the global namespace. */
2898 if (DECL_NAMESPACE_SCOPE_P (decl))
2899 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2901 if (is_friend && !have_def)
2902 /* This is not really a declaration of a specialization.
2903 It's just the name of an instantiation. But, it's not
2904 a request for an instantiation, either. */
2905 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2907 /* Register this specialization so that we can find it
2908 again. */
2909 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2911 /* A 'structor should already have clones. */
2912 gcc_assert (decl == error_mark_node
2913 || !(DECL_CONSTRUCTOR_P (decl)
2914 || DECL_DESTRUCTOR_P (decl))
2915 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2919 return decl;
2922 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2923 parameters. These are represented in the same format used for
2924 DECL_TEMPLATE_PARMS. */
2927 comp_template_parms (const_tree parms1, const_tree parms2)
2929 const_tree p1;
2930 const_tree p2;
2932 if (parms1 == parms2)
2933 return 1;
2935 for (p1 = parms1, p2 = parms2;
2936 p1 != NULL_TREE && p2 != NULL_TREE;
2937 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2939 tree t1 = TREE_VALUE (p1);
2940 tree t2 = TREE_VALUE (p2);
2941 int i;
2943 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2944 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2946 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2947 return 0;
2949 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2951 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2952 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2954 /* If either of the template parameters are invalid, assume
2955 they match for the sake of error recovery. */
2956 if (parm1 == error_mark_node || parm2 == error_mark_node)
2957 return 1;
2959 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2960 return 0;
2962 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2963 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2964 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2965 continue;
2966 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2967 return 0;
2971 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2972 /* One set of parameters has more parameters lists than the
2973 other. */
2974 return 0;
2976 return 1;
2979 /* Determine whether PARM is a parameter pack. */
2981 bool
2982 template_parameter_pack_p (const_tree parm)
2984 /* Determine if we have a non-type template parameter pack. */
2985 if (TREE_CODE (parm) == PARM_DECL)
2986 return (DECL_TEMPLATE_PARM_P (parm)
2987 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2988 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2989 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2991 /* If this is a list of template parameters, we could get a
2992 TYPE_DECL or a TEMPLATE_DECL. */
2993 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2994 parm = TREE_TYPE (parm);
2996 /* Otherwise it must be a type template parameter. */
2997 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2998 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2999 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3002 /* Determine if T is a function parameter pack. */
3004 bool
3005 function_parameter_pack_p (const_tree t)
3007 if (t && TREE_CODE (t) == PARM_DECL)
3008 return DECL_PACK_P (t);
3009 return false;
3012 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3013 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3015 tree
3016 get_function_template_decl (const_tree primary_func_tmpl_inst)
3018 if (! primary_func_tmpl_inst
3019 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3020 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3021 return NULL;
3023 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3026 /* Return true iff the function parameter PARAM_DECL was expanded
3027 from the function parameter pack PACK. */
3029 bool
3030 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3032 if (DECL_ARTIFICIAL (param_decl)
3033 || !function_parameter_pack_p (pack))
3034 return false;
3036 /* The parameter pack and its pack arguments have the same
3037 DECL_PARM_INDEX. */
3038 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3041 /* Determine whether ARGS describes a variadic template args list,
3042 i.e., one that is terminated by a template argument pack. */
3044 static bool
3045 template_args_variadic_p (tree args)
3047 int nargs;
3048 tree last_parm;
3050 if (args == NULL_TREE)
3051 return false;
3053 args = INNERMOST_TEMPLATE_ARGS (args);
3054 nargs = TREE_VEC_LENGTH (args);
3056 if (nargs == 0)
3057 return false;
3059 last_parm = TREE_VEC_ELT (args, nargs - 1);
3061 return ARGUMENT_PACK_P (last_parm);
3064 /* Generate a new name for the parameter pack name NAME (an
3065 IDENTIFIER_NODE) that incorporates its */
3067 static tree
3068 make_ith_pack_parameter_name (tree name, int i)
3070 /* Munge the name to include the parameter index. */
3071 #define NUMBUF_LEN 128
3072 char numbuf[NUMBUF_LEN];
3073 char* newname;
3074 int newname_len;
3076 if (name == NULL_TREE)
3077 return name;
3078 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3079 newname_len = IDENTIFIER_LENGTH (name)
3080 + strlen (numbuf) + 2;
3081 newname = (char*)alloca (newname_len);
3082 snprintf (newname, newname_len,
3083 "%s#%i", IDENTIFIER_POINTER (name), i);
3084 return get_identifier (newname);
3087 /* Return true if T is a primary function, class or alias template
3088 instantiation. */
3090 bool
3091 primary_template_instantiation_p (const_tree t)
3093 if (!t)
3094 return false;
3096 if (TREE_CODE (t) == FUNCTION_DECL)
3097 return DECL_LANG_SPECIFIC (t)
3098 && DECL_TEMPLATE_INSTANTIATION (t)
3099 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3100 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3101 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3102 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3103 else if (alias_template_specialization_p (t))
3104 return true;
3105 return false;
3108 /* Return true if PARM is a template template parameter. */
3110 bool
3111 template_template_parameter_p (const_tree parm)
3113 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3116 /* Return true iff PARM is a DECL representing a type template
3117 parameter. */
3119 bool
3120 template_type_parameter_p (const_tree parm)
3122 return (parm
3123 && (TREE_CODE (parm) == TYPE_DECL
3124 || TREE_CODE (parm) == TEMPLATE_DECL)
3125 && DECL_TEMPLATE_PARM_P (parm));
3128 /* Return the template parameters of T if T is a
3129 primary template instantiation, NULL otherwise. */
3131 tree
3132 get_primary_template_innermost_parameters (const_tree t)
3134 tree parms = NULL, template_info = NULL;
3136 if ((template_info = get_template_info (t))
3137 && primary_template_instantiation_p (t))
3138 parms = INNERMOST_TEMPLATE_PARMS
3139 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3141 return parms;
3144 /* Return the template parameters of the LEVELth level from the full list
3145 of template parameters PARMS. */
3147 tree
3148 get_template_parms_at_level (tree parms, int level)
3150 tree p;
3151 if (!parms
3152 || TREE_CODE (parms) != TREE_LIST
3153 || level > TMPL_PARMS_DEPTH (parms))
3154 return NULL_TREE;
3156 for (p = parms; p; p = TREE_CHAIN (p))
3157 if (TMPL_PARMS_DEPTH (p) == level)
3158 return p;
3160 return NULL_TREE;
3163 /* Returns the template arguments of T if T is a template instantiation,
3164 NULL otherwise. */
3166 tree
3167 get_template_innermost_arguments (const_tree t)
3169 tree args = NULL, template_info = NULL;
3171 if ((template_info = get_template_info (t))
3172 && TI_ARGS (template_info))
3173 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3175 return args;
3178 /* Return the argument pack elements of T if T is a template argument pack,
3179 NULL otherwise. */
3181 tree
3182 get_template_argument_pack_elems (const_tree t)
3184 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3185 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3186 return NULL;
3188 return ARGUMENT_PACK_ARGS (t);
3191 /* Structure used to track the progress of find_parameter_packs_r. */
3192 struct find_parameter_pack_data
3194 /* TREE_LIST that will contain all of the parameter packs found by
3195 the traversal. */
3196 tree* parameter_packs;
3198 /* Set of AST nodes that have been visited by the traversal. */
3199 struct pointer_set_t *visited;
3202 /* Identifies all of the argument packs that occur in a template
3203 argument and appends them to the TREE_LIST inside DATA, which is a
3204 find_parameter_pack_data structure. This is a subroutine of
3205 make_pack_expansion and uses_parameter_packs. */
3206 static tree
3207 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3209 tree t = *tp;
3210 struct find_parameter_pack_data* ppd =
3211 (struct find_parameter_pack_data*)data;
3212 bool parameter_pack_p = false;
3214 /* Handle type aliases/typedefs. */
3215 if (TYPE_ALIAS_P (t))
3217 if (TYPE_TEMPLATE_INFO (t))
3218 cp_walk_tree (&TYPE_TI_ARGS (t),
3219 &find_parameter_packs_r,
3220 ppd, ppd->visited);
3221 *walk_subtrees = 0;
3222 return NULL_TREE;
3225 /* Identify whether this is a parameter pack or not. */
3226 switch (TREE_CODE (t))
3228 case TEMPLATE_PARM_INDEX:
3229 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3230 parameter_pack_p = true;
3231 break;
3233 case TEMPLATE_TYPE_PARM:
3234 t = TYPE_MAIN_VARIANT (t);
3235 case TEMPLATE_TEMPLATE_PARM:
3236 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3237 parameter_pack_p = true;
3238 break;
3240 case FIELD_DECL:
3241 case PARM_DECL:
3242 if (DECL_PACK_P (t))
3244 /* We don't want to walk into the type of a PARM_DECL,
3245 because we don't want to see the type parameter pack. */
3246 *walk_subtrees = 0;
3247 parameter_pack_p = true;
3249 break;
3251 /* Look through a lambda capture proxy to the field pack. */
3252 case VAR_DECL:
3253 if (DECL_HAS_VALUE_EXPR_P (t))
3255 tree v = DECL_VALUE_EXPR (t);
3256 cp_walk_tree (&v,
3257 &find_parameter_packs_r,
3258 ppd, ppd->visited);
3259 *walk_subtrees = 0;
3261 break;
3263 case BASES:
3264 parameter_pack_p = true;
3265 break;
3266 default:
3267 /* Not a parameter pack. */
3268 break;
3271 if (parameter_pack_p)
3273 /* Add this parameter pack to the list. */
3274 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3277 if (TYPE_P (t))
3278 cp_walk_tree (&TYPE_CONTEXT (t),
3279 &find_parameter_packs_r, ppd, ppd->visited);
3281 /* This switch statement will return immediately if we don't find a
3282 parameter pack. */
3283 switch (TREE_CODE (t))
3285 case TEMPLATE_PARM_INDEX:
3286 return NULL_TREE;
3288 case BOUND_TEMPLATE_TEMPLATE_PARM:
3289 /* Check the template itself. */
3290 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3291 &find_parameter_packs_r, ppd, ppd->visited);
3292 /* Check the template arguments. */
3293 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3294 ppd->visited);
3295 *walk_subtrees = 0;
3296 return NULL_TREE;
3298 case TEMPLATE_TYPE_PARM:
3299 case TEMPLATE_TEMPLATE_PARM:
3300 return NULL_TREE;
3302 case PARM_DECL:
3303 return NULL_TREE;
3305 case RECORD_TYPE:
3306 if (TYPE_PTRMEMFUNC_P (t))
3307 return NULL_TREE;
3308 /* Fall through. */
3310 case UNION_TYPE:
3311 case ENUMERAL_TYPE:
3312 if (TYPE_TEMPLATE_INFO (t))
3313 cp_walk_tree (&TYPE_TI_ARGS (t),
3314 &find_parameter_packs_r, ppd, ppd->visited);
3316 *walk_subtrees = 0;
3317 return NULL_TREE;
3319 case CONSTRUCTOR:
3320 case TEMPLATE_DECL:
3321 cp_walk_tree (&TREE_TYPE (t),
3322 &find_parameter_packs_r, ppd, ppd->visited);
3323 return NULL_TREE;
3325 case TYPENAME_TYPE:
3326 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3327 ppd, ppd->visited);
3328 *walk_subtrees = 0;
3329 return NULL_TREE;
3331 case TYPE_PACK_EXPANSION:
3332 case EXPR_PACK_EXPANSION:
3333 *walk_subtrees = 0;
3334 return NULL_TREE;
3336 case INTEGER_TYPE:
3337 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3338 ppd, ppd->visited);
3339 *walk_subtrees = 0;
3340 return NULL_TREE;
3342 case IDENTIFIER_NODE:
3343 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3344 ppd->visited);
3345 *walk_subtrees = 0;
3346 return NULL_TREE;
3348 default:
3349 return NULL_TREE;
3352 return NULL_TREE;
3355 /* Determines if the expression or type T uses any parameter packs. */
3356 bool
3357 uses_parameter_packs (tree t)
3359 tree parameter_packs = NULL_TREE;
3360 struct find_parameter_pack_data ppd;
3361 ppd.parameter_packs = &parameter_packs;
3362 ppd.visited = pointer_set_create ();
3363 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3364 pointer_set_destroy (ppd.visited);
3365 return parameter_packs != NULL_TREE;
3368 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3369 representation a base-class initializer into a parameter pack
3370 expansion. If all goes well, the resulting node will be an
3371 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3372 respectively. */
3373 tree
3374 make_pack_expansion (tree arg)
3376 tree result;
3377 tree parameter_packs = NULL_TREE;
3378 bool for_types = false;
3379 struct find_parameter_pack_data ppd;
3381 if (!arg || arg == error_mark_node)
3382 return arg;
3384 if (TREE_CODE (arg) == TREE_LIST)
3386 /* The only time we will see a TREE_LIST here is for a base
3387 class initializer. In this case, the TREE_PURPOSE will be a
3388 _TYPE node (representing the base class expansion we're
3389 initializing) and the TREE_VALUE will be a TREE_LIST
3390 containing the initialization arguments.
3392 The resulting expansion looks somewhat different from most
3393 expansions. Rather than returning just one _EXPANSION, we
3394 return a TREE_LIST whose TREE_PURPOSE is a
3395 TYPE_PACK_EXPANSION containing the bases that will be
3396 initialized. The TREE_VALUE will be identical to the
3397 original TREE_VALUE, which is a list of arguments that will
3398 be passed to each base. We do not introduce any new pack
3399 expansion nodes into the TREE_VALUE (although it is possible
3400 that some already exist), because the TREE_PURPOSE and
3401 TREE_VALUE all need to be expanded together with the same
3402 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3403 resulting TREE_PURPOSE will mention the parameter packs in
3404 both the bases and the arguments to the bases. */
3405 tree purpose;
3406 tree value;
3407 tree parameter_packs = NULL_TREE;
3409 /* Determine which parameter packs will be used by the base
3410 class expansion. */
3411 ppd.visited = pointer_set_create ();
3412 ppd.parameter_packs = &parameter_packs;
3413 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3414 &ppd, ppd.visited);
3416 if (parameter_packs == NULL_TREE)
3418 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3419 pointer_set_destroy (ppd.visited);
3420 return error_mark_node;
3423 if (TREE_VALUE (arg) != void_type_node)
3425 /* Collect the sets of parameter packs used in each of the
3426 initialization arguments. */
3427 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3429 /* Determine which parameter packs will be expanded in this
3430 argument. */
3431 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3432 &ppd, ppd.visited);
3436 pointer_set_destroy (ppd.visited);
3438 /* Create the pack expansion type for the base type. */
3439 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3440 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3441 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3443 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3444 they will rarely be compared to anything. */
3445 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3447 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3450 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3451 for_types = true;
3453 /* Build the PACK_EXPANSION_* node. */
3454 result = for_types
3455 ? cxx_make_type (TYPE_PACK_EXPANSION)
3456 : make_node (EXPR_PACK_EXPANSION);
3457 SET_PACK_EXPANSION_PATTERN (result, arg);
3458 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3460 /* Propagate type and const-expression information. */
3461 TREE_TYPE (result) = TREE_TYPE (arg);
3462 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3464 else
3465 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3466 they will rarely be compared to anything. */
3467 SET_TYPE_STRUCTURAL_EQUALITY (result);
3469 /* Determine which parameter packs will be expanded. */
3470 ppd.parameter_packs = &parameter_packs;
3471 ppd.visited = pointer_set_create ();
3472 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3473 pointer_set_destroy (ppd.visited);
3475 /* Make sure we found some parameter packs. */
3476 if (parameter_packs == NULL_TREE)
3478 if (TYPE_P (arg))
3479 error ("expansion pattern %<%T%> contains no argument packs", arg);
3480 else
3481 error ("expansion pattern %<%E%> contains no argument packs", arg);
3482 return error_mark_node;
3484 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3486 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3488 return result;
3491 /* Checks T for any "bare" parameter packs, which have not yet been
3492 expanded, and issues an error if any are found. This operation can
3493 only be done on full expressions or types (e.g., an expression
3494 statement, "if" condition, etc.), because we could have expressions like:
3496 foo(f(g(h(args)))...)
3498 where "args" is a parameter pack. check_for_bare_parameter_packs
3499 should not be called for the subexpressions args, h(args),
3500 g(h(args)), or f(g(h(args))), because we would produce erroneous
3501 error messages.
3503 Returns TRUE and emits an error if there were bare parameter packs,
3504 returns FALSE otherwise. */
3505 bool
3506 check_for_bare_parameter_packs (tree t)
3508 tree parameter_packs = NULL_TREE;
3509 struct find_parameter_pack_data ppd;
3511 if (!processing_template_decl || !t || t == error_mark_node)
3512 return false;
3514 if (TREE_CODE (t) == TYPE_DECL)
3515 t = TREE_TYPE (t);
3517 ppd.parameter_packs = &parameter_packs;
3518 ppd.visited = pointer_set_create ();
3519 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3520 pointer_set_destroy (ppd.visited);
3522 if (parameter_packs)
3524 error ("parameter packs not expanded with %<...%>:");
3525 while (parameter_packs)
3527 tree pack = TREE_VALUE (parameter_packs);
3528 tree name = NULL_TREE;
3530 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3531 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3532 name = TYPE_NAME (pack);
3533 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3534 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3535 else
3536 name = DECL_NAME (pack);
3538 if (name)
3539 inform (input_location, " %qD", name);
3540 else
3541 inform (input_location, " <anonymous>");
3543 parameter_packs = TREE_CHAIN (parameter_packs);
3546 return true;
3549 return false;
3552 /* Expand any parameter packs that occur in the template arguments in
3553 ARGS. */
3554 tree
3555 expand_template_argument_pack (tree args)
3557 tree result_args = NULL_TREE;
3558 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3559 int num_result_args = -1;
3560 int non_default_args_count = -1;
3562 /* First, determine if we need to expand anything, and the number of
3563 slots we'll need. */
3564 for (in_arg = 0; in_arg < nargs; ++in_arg)
3566 tree arg = TREE_VEC_ELT (args, in_arg);
3567 if (arg == NULL_TREE)
3568 return args;
3569 if (ARGUMENT_PACK_P (arg))
3571 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3572 if (num_result_args < 0)
3573 num_result_args = in_arg + num_packed;
3574 else
3575 num_result_args += num_packed;
3577 else
3579 if (num_result_args >= 0)
3580 num_result_args++;
3584 /* If no expansion is necessary, we're done. */
3585 if (num_result_args < 0)
3586 return args;
3588 /* Expand arguments. */
3589 result_args = make_tree_vec (num_result_args);
3590 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3591 non_default_args_count =
3592 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3593 for (in_arg = 0; in_arg < nargs; ++in_arg)
3595 tree arg = TREE_VEC_ELT (args, in_arg);
3596 if (ARGUMENT_PACK_P (arg))
3598 tree packed = ARGUMENT_PACK_ARGS (arg);
3599 int i, num_packed = TREE_VEC_LENGTH (packed);
3600 for (i = 0; i < num_packed; ++i, ++out_arg)
3601 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3602 if (non_default_args_count > 0)
3603 non_default_args_count += num_packed - 1;
3605 else
3607 TREE_VEC_ELT (result_args, out_arg) = arg;
3608 ++out_arg;
3611 if (non_default_args_count >= 0)
3612 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3613 return result_args;
3616 /* Checks if DECL shadows a template parameter.
3618 [temp.local]: A template-parameter shall not be redeclared within its
3619 scope (including nested scopes).
3621 Emits an error and returns TRUE if the DECL shadows a parameter,
3622 returns FALSE otherwise. */
3624 bool
3625 check_template_shadow (tree decl)
3627 tree olddecl;
3629 /* If we're not in a template, we can't possibly shadow a template
3630 parameter. */
3631 if (!current_template_parms)
3632 return true;
3634 /* Figure out what we're shadowing. */
3635 if (TREE_CODE (decl) == OVERLOAD)
3636 decl = OVL_CURRENT (decl);
3637 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3639 /* If there's no previous binding for this name, we're not shadowing
3640 anything, let alone a template parameter. */
3641 if (!olddecl)
3642 return true;
3644 /* If we're not shadowing a template parameter, we're done. Note
3645 that OLDDECL might be an OVERLOAD (or perhaps even an
3646 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3647 node. */
3648 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3649 return true;
3651 /* We check for decl != olddecl to avoid bogus errors for using a
3652 name inside a class. We check TPFI to avoid duplicate errors for
3653 inline member templates. */
3654 if (decl == olddecl
3655 || (DECL_TEMPLATE_PARM_P (decl)
3656 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3657 return true;
3659 /* Don't complain about the injected class name, as we've already
3660 complained about the class itself. */
3661 if (DECL_SELF_REFERENCE_P (decl))
3662 return false;
3664 error ("declaration of %q+#D", decl);
3665 error (" shadows template parm %q+#D", olddecl);
3666 return false;
3669 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3670 ORIG_LEVEL, DECL, and TYPE. */
3672 static tree
3673 build_template_parm_index (int index,
3674 int level,
3675 int orig_level,
3676 tree decl,
3677 tree type)
3679 tree t = make_node (TEMPLATE_PARM_INDEX);
3680 TEMPLATE_PARM_IDX (t) = index;
3681 TEMPLATE_PARM_LEVEL (t) = level;
3682 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3683 TEMPLATE_PARM_DECL (t) = decl;
3684 TREE_TYPE (t) = type;
3685 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3686 TREE_READONLY (t) = TREE_READONLY (decl);
3688 return t;
3691 /* Find the canonical type parameter for the given template type
3692 parameter. Returns the canonical type parameter, which may be TYPE
3693 if no such parameter existed. */
3695 static tree
3696 canonical_type_parameter (tree type)
3698 tree list;
3699 int idx = TEMPLATE_TYPE_IDX (type);
3700 if (!canonical_template_parms)
3701 vec_alloc (canonical_template_parms, idx+1);
3703 while (canonical_template_parms->length () <= (unsigned)idx)
3704 vec_safe_push (canonical_template_parms, NULL_TREE);
3706 list = (*canonical_template_parms)[idx];
3707 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3708 list = TREE_CHAIN (list);
3710 if (list)
3711 return TREE_VALUE (list);
3712 else
3714 (*canonical_template_parms)[idx]
3715 = tree_cons (NULL_TREE, type,
3716 (*canonical_template_parms)[idx]);
3717 return type;
3721 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3722 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3723 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3724 new one is created. */
3726 static tree
3727 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3728 tsubst_flags_t complain)
3730 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3731 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3732 != TEMPLATE_PARM_LEVEL (index) - levels)
3733 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3735 tree orig_decl = TEMPLATE_PARM_DECL (index);
3736 tree decl, t;
3738 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3739 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3740 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3741 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3742 DECL_ARTIFICIAL (decl) = 1;
3743 SET_DECL_TEMPLATE_PARM_P (decl);
3745 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3746 TEMPLATE_PARM_LEVEL (index) - levels,
3747 TEMPLATE_PARM_ORIG_LEVEL (index),
3748 decl, type);
3749 TEMPLATE_PARM_DESCENDANTS (index) = t;
3750 TEMPLATE_PARM_PARAMETER_PACK (t)
3751 = TEMPLATE_PARM_PARAMETER_PACK (index);
3753 /* Template template parameters need this. */
3754 if (TREE_CODE (decl) == TEMPLATE_DECL)
3755 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3756 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3757 args, complain);
3760 return TEMPLATE_PARM_DESCENDANTS (index);
3763 /* Process information from new template parameter PARM and append it
3764 to the LIST being built. This new parameter is a non-type
3765 parameter iff IS_NON_TYPE is true. This new parameter is a
3766 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3767 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3768 parameter list PARM belongs to. This is used used to create a
3769 proper canonical type for the type of PARM that is to be created,
3770 iff PARM is a type. If the size is not known, this parameter shall
3771 be set to 0. */
3773 tree
3774 process_template_parm (tree list, location_t parm_loc, tree parm,
3775 bool is_non_type, bool is_parameter_pack)
3777 tree decl = 0;
3778 tree err_parm_list;
3779 int idx = 0;
3781 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3782 tree defval = TREE_PURPOSE (parm);
3783 tree constr = TREE_TYPE (parm);
3784 tree reqs = NULL_TREE;
3786 if (list)
3788 tree p = tree_last (list);
3790 if (p && TREE_VALUE (p) != error_mark_node)
3792 p = TREE_VALUE (p);
3793 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3794 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3795 else
3796 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3799 ++idx;
3801 else
3802 idx = 0;
3804 if (is_non_type)
3806 parm = TREE_VALUE (parm);
3808 SET_DECL_TEMPLATE_PARM_P (parm);
3810 if (TREE_TYPE (parm) == error_mark_node)
3812 err_parm_list = build_tree_list (defval, parm);
3813 TREE_VALUE (err_parm_list) = error_mark_node;
3814 return chainon (list, err_parm_list);
3816 else
3818 /* [temp.param]
3820 The top-level cv-qualifiers on the template-parameter are
3821 ignored when determining its type. */
3822 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3823 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3825 err_parm_list = build_tree_list (defval, parm);
3826 TREE_VALUE (err_parm_list) = error_mark_node;
3827 return chainon (list, err_parm_list);
3830 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack
3831 /* If we're in a nested template parameter list, the template
3832 template parameter could be a parameter pack. */
3833 && processing_template_parmlist == 1)
3835 /* This template parameter is not a parameter pack, but it
3836 should be. Complain about "bare" parameter packs. */
3837 check_for_bare_parameter_packs (TREE_TYPE (parm));
3839 /* Recover by calling this a parameter pack. */
3840 is_parameter_pack = true;
3844 /* A template parameter is not modifiable. */
3845 TREE_CONSTANT (parm) = 1;
3846 TREE_READONLY (parm) = 1;
3847 decl = build_decl (parm_loc,
3848 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3849 TREE_CONSTANT (decl) = 1;
3850 TREE_READONLY (decl) = 1;
3851 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3852 = build_template_parm_index (idx, processing_template_decl,
3853 processing_template_decl,
3854 decl, TREE_TYPE (parm));
3856 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3857 = is_parameter_pack;
3859 // Build requirements for the parameter.
3860 reqs = finish_shorthand_requirement (parm, constr);
3862 else
3864 tree t;
3865 parm = TREE_VALUE (TREE_VALUE (parm));
3867 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3869 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3870 /* This is for distinguishing between real templates and template
3871 template parameters */
3872 TREE_TYPE (parm) = t;
3873 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3874 decl = parm;
3876 else
3878 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3879 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3880 decl = build_decl (parm_loc,
3881 TYPE_DECL, parm, t);
3884 TYPE_NAME (t) = decl;
3885 TYPE_STUB_DECL (t) = decl;
3886 parm = decl;
3887 TEMPLATE_TYPE_PARM_INDEX (t)
3888 = build_template_parm_index (idx, processing_template_decl,
3889 processing_template_decl,
3890 decl, TREE_TYPE (parm));
3891 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3892 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3894 // Build requirements for the type/template parameter.
3895 reqs = finish_shorthand_requirement (parm, constr);
3897 DECL_ARTIFICIAL (decl) = 1;
3898 SET_DECL_TEMPLATE_PARM_P (decl);
3899 pushdecl (decl);
3901 // Build the parameter node linking the parameter declaration, its
3902 // default argument (if any), and its constraints (if any).
3903 parm = build_tree_list (defval, parm);
3904 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
3906 return chainon (list, parm);
3909 /* The end of a template parameter list has been reached. Process the
3910 tree list into a parameter vector, converting each parameter into a more
3911 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3912 as PARM_DECLs. */
3914 tree
3915 end_template_parm_list (tree parms)
3917 int nparms;
3918 tree parm, next;
3919 tree saved_parmlist = make_tree_vec (list_length (parms));
3921 current_template_parms
3922 = tree_cons (size_int (processing_template_decl),
3923 saved_parmlist, current_template_parms);
3925 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3927 next = TREE_CHAIN (parm);
3928 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3929 TREE_CHAIN (parm) = NULL_TREE;
3932 --processing_template_parmlist;
3934 return saved_parmlist;
3937 // Explicitly indicate the end of the template parameter list. We assume
3938 // that the current template parameters have been constructed and/or
3939 // managed explicitly, as when creating new template template parameters
3940 // from a shorthand constraint.
3941 void
3942 end_template_parm_list ()
3944 --processing_template_parmlist;
3947 /* end_template_decl is called after a template declaration is seen. */
3949 void
3950 end_template_decl (void)
3952 reset_specialization ();
3954 if (! processing_template_decl)
3955 return;
3957 /* This matches the pushlevel in begin_template_parm_list. */
3958 finish_scope ();
3960 --processing_template_decl;
3961 current_template_parms = TREE_CHAIN (current_template_parms);
3964 /* Takes a TREE_LIST representing a template parameter and convert it
3965 into an argument suitable to be passed to the type substitution
3966 functions. Note that If the TREE_LIST contains an error_mark
3967 node, the returned argument is error_mark_node. */
3969 tree
3970 template_parm_to_arg (tree t)
3973 if (t == NULL_TREE
3974 || TREE_CODE (t) != TREE_LIST)
3975 return t;
3977 if (error_operand_p (TREE_VALUE (t)))
3978 return error_mark_node;
3980 t = TREE_VALUE (t);
3982 if (TREE_CODE (t) == TYPE_DECL
3983 || TREE_CODE (t) == TEMPLATE_DECL)
3985 t = TREE_TYPE (t);
3987 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3989 /* Turn this argument into a TYPE_ARGUMENT_PACK
3990 with a single element, which expands T. */
3991 tree vec = make_tree_vec (1);
3992 #ifdef ENABLE_CHECKING
3993 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3994 (vec, TREE_VEC_LENGTH (vec));
3995 #endif
3996 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3998 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3999 SET_ARGUMENT_PACK_ARGS (t, vec);
4002 else
4004 t = DECL_INITIAL (t);
4006 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4008 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4009 with a single element, which expands T. */
4010 tree vec = make_tree_vec (1);
4011 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4012 #ifdef ENABLE_CHECKING
4013 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4014 (vec, TREE_VEC_LENGTH (vec));
4015 #endif
4016 t = convert_from_reference (t);
4017 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4019 t = make_node (NONTYPE_ARGUMENT_PACK);
4020 SET_ARGUMENT_PACK_ARGS (t, vec);
4021 TREE_TYPE (t) = type;
4023 else
4024 t = convert_from_reference (t);
4026 return t;
4029 /* Given a set of template parameters, return them as a set of template
4030 arguments. The template parameters are represented as a TREE_VEC, in
4031 the form documented in cp-tree.h for template arguments. */
4033 static tree
4034 template_parms_to_args (tree parms)
4036 tree header;
4037 tree args = NULL_TREE;
4038 int length = TMPL_PARMS_DEPTH (parms);
4039 int l = length;
4041 /* If there is only one level of template parameters, we do not
4042 create a TREE_VEC of TREE_VECs. Instead, we return a single
4043 TREE_VEC containing the arguments. */
4044 if (length > 1)
4045 args = make_tree_vec (length);
4047 for (header = parms; header; header = TREE_CHAIN (header))
4049 tree a = copy_node (TREE_VALUE (header));
4050 int i;
4052 TREE_TYPE (a) = NULL_TREE;
4053 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4054 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4056 #ifdef ENABLE_CHECKING
4057 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4058 #endif
4060 if (length > 1)
4061 TREE_VEC_ELT (args, --l) = a;
4062 else
4063 args = a;
4066 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
4067 /* This can happen for template parms of a template template
4068 parameter, e.g:
4070 template<template<class T, class U> class TT> struct S;
4072 Consider the level of the parms of TT; T and U both have
4073 level 2; TT has no template parm of level 1. So in this case
4074 the first element of full_template_args is NULL_TREE. If we
4075 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4076 of 2. This will make tsubst wrongly consider that T and U
4077 have level 1. Instead, let's create a dummy vector as the
4078 first element of full_template_args so that TMPL_ARGS_DEPTH
4079 returns the correct depth for args. */
4080 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4081 return args;
4084 /* Within the declaration of a template, return the currently active
4085 template parameters as an argument TREE_VEC. */
4087 static tree
4088 current_template_args (void)
4090 return template_parms_to_args (current_template_parms);
4093 /* Update the declared TYPE by doing any lookups which were thought to be
4094 dependent, but are not now that we know the SCOPE of the declarator. */
4096 tree
4097 maybe_update_decl_type (tree orig_type, tree scope)
4099 tree type = orig_type;
4101 if (type == NULL_TREE)
4102 return type;
4104 if (TREE_CODE (orig_type) == TYPE_DECL)
4105 type = TREE_TYPE (type);
4107 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4108 && dependent_type_p (type)
4109 /* Don't bother building up the args in this case. */
4110 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4112 /* tsubst in the args corresponding to the template parameters,
4113 including auto if present. Most things will be unchanged, but
4114 make_typename_type and tsubst_qualified_id will resolve
4115 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4116 tree args = current_template_args ();
4117 tree auto_node = type_uses_auto (type);
4118 tree pushed;
4119 if (auto_node)
4121 tree auto_vec = make_tree_vec (1);
4122 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4123 args = add_to_template_args (args, auto_vec);
4125 pushed = push_scope (scope);
4126 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4127 if (pushed)
4128 pop_scope (scope);
4131 if (type == error_mark_node)
4132 return orig_type;
4134 if (TREE_CODE (orig_type) == TYPE_DECL)
4136 if (same_type_p (type, TREE_TYPE (orig_type)))
4137 type = orig_type;
4138 else
4139 type = TYPE_NAME (type);
4141 return type;
4144 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4145 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4146 the new template is a member template. */
4148 tree
4149 build_template_decl (tree decl, tree parms, tree constr, bool member_template_p)
4151 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4152 DECL_TEMPLATE_PARMS (tmpl) = parms;
4153 DECL_CONSTRAINTS (tmpl) = constr;
4154 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4155 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4156 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4158 return tmpl;
4161 struct template_parm_data
4163 /* The level of the template parameters we are currently
4164 processing. */
4165 int level;
4167 /* The index of the specialization argument we are currently
4168 processing. */
4169 int current_arg;
4171 /* An array whose size is the number of template parameters. The
4172 elements are nonzero if the parameter has been used in any one
4173 of the arguments processed so far. */
4174 int* parms;
4176 /* An array whose size is the number of template arguments. The
4177 elements are nonzero if the argument makes use of template
4178 parameters of this level. */
4179 int* arg_uses_template_parms;
4182 /* Subroutine of push_template_decl used to see if each template
4183 parameter in a partial specialization is used in the explicit
4184 argument list. If T is of the LEVEL given in DATA (which is
4185 treated as a template_parm_data*), then DATA->PARMS is marked
4186 appropriately. */
4188 static int
4189 mark_template_parm (tree t, void* data)
4191 int level;
4192 int idx;
4193 struct template_parm_data* tpd = (struct template_parm_data*) data;
4195 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4197 level = TEMPLATE_PARM_LEVEL (t);
4198 idx = TEMPLATE_PARM_IDX (t);
4200 else
4202 level = TEMPLATE_TYPE_LEVEL (t);
4203 idx = TEMPLATE_TYPE_IDX (t);
4206 if (level == tpd->level)
4208 tpd->parms[idx] = 1;
4209 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4212 /* Return zero so that for_each_template_parm will continue the
4213 traversal of the tree; we want to mark *every* template parm. */
4214 return 0;
4217 /* Process the partial specialization DECL. */
4219 static tree
4220 process_partial_specialization (tree decl)
4222 tree type = TREE_TYPE (decl);
4223 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4224 tree specargs = CLASSTYPE_TI_ARGS (type);
4225 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4226 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4227 tree inner_parms;
4228 tree inst;
4229 int nargs = TREE_VEC_LENGTH (inner_args);
4230 int ntparms;
4231 int i;
4232 bool did_error_intro = false;
4233 struct template_parm_data tpd;
4234 struct template_parm_data tpd2;
4236 gcc_assert (current_template_parms);
4238 // When defining a constrained partial specialization, DECL's type
4239 // will have been assigned to the canonical type of the primary.
4240 // That is:
4242 // template<typename T>
4243 // struct S; // Has canonical type S<T>
4245 // template<typename T>
4246 // requires C<T>
4247 // struct S<T>; // binds to the primary template.
4249 // However, the the constraints differ, so we should be constructing
4250 // a new type. We do this by making the DECL's type its own
4251 // canonical type.
4253 // TODO: Do we need to compare the current requirements to make
4254 // sure this isn't a redeclaration?
4255 if (current_template_reqs)
4256 TYPE_CANONICAL (type) = type;
4258 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4259 ntparms = TREE_VEC_LENGTH (inner_parms);
4261 /* We check that each of the template parameters given in the
4262 partial specialization is used in the argument list to the
4263 specialization. For example:
4265 template <class T> struct S;
4266 template <class T> struct S<T*>;
4268 The second declaration is OK because `T*' uses the template
4269 parameter T, whereas
4271 template <class T> struct S<int>;
4273 is no good. Even trickier is:
4275 template <class T>
4276 struct S1
4278 template <class U>
4279 struct S2;
4280 template <class U>
4281 struct S2<T>;
4284 The S2<T> declaration is actually invalid; it is a
4285 full-specialization. Of course,
4287 template <class U>
4288 struct S2<T (*)(U)>;
4290 or some such would have been OK. */
4291 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4292 tpd.parms = XALLOCAVEC (int, ntparms);
4293 memset (tpd.parms, 0, sizeof (int) * ntparms);
4295 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4296 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4297 for (i = 0; i < nargs; ++i)
4299 tpd.current_arg = i;
4300 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4301 &mark_template_parm,
4302 &tpd,
4303 NULL,
4304 /*include_nondeduced_p=*/false);
4306 for (i = 0; i < ntparms; ++i)
4307 if (tpd.parms[i] == 0)
4309 /* One of the template parms was not used in the
4310 specialization. */
4311 if (!did_error_intro)
4313 error ("template parameters not used in partial specialization:");
4314 did_error_intro = true;
4317 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4320 if (did_error_intro)
4321 return error_mark_node;
4323 /* [temp.class.spec]
4325 The argument list of the specialization shall not be identical to
4326 the implicit argument list of the primary template.
4328 Note that concepts allow partial specializations with the same list of
4329 arguments but different constraints. */
4330 tree main_type = TREE_TYPE (maintmpl);
4331 tree main_args = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (main_type));
4332 tree main_constr = DECL_CONSTRAINTS (maintmpl);
4333 if (comp_template_args (inner_args, main_args)
4334 && equivalent_constraints (current_template_reqs, main_constr))
4335 error ("partial specialization %qT does not specialize any "
4336 "template arguments", type);
4338 /* A partial specialization that replaces multiple parameters of the
4339 primary template with a pack expansion is less specialized for those
4340 parameters. */
4341 if (nargs < DECL_NTPARMS (maintmpl))
4343 error ("partial specialization is not more specialized than the "
4344 "primary template because it replaces multiple parameters "
4345 "with a pack expansion");
4346 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4347 return decl;
4350 /* [temp.class.spec]
4352 A partially specialized non-type argument expression shall not
4353 involve template parameters of the partial specialization except
4354 when the argument expression is a simple identifier.
4356 The type of a template parameter corresponding to a specialized
4357 non-type argument shall not be dependent on a parameter of the
4358 specialization.
4360 Also, we verify that pack expansions only occur at the
4361 end of the argument list. */
4362 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4363 tpd2.parms = 0;
4364 for (i = 0; i < nargs; ++i)
4366 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4367 tree arg = TREE_VEC_ELT (inner_args, i);
4368 tree packed_args = NULL_TREE;
4369 int j, len = 1;
4371 if (ARGUMENT_PACK_P (arg))
4373 /* Extract the arguments from the argument pack. We'll be
4374 iterating over these in the following loop. */
4375 packed_args = ARGUMENT_PACK_ARGS (arg);
4376 len = TREE_VEC_LENGTH (packed_args);
4379 for (j = 0; j < len; j++)
4381 if (packed_args)
4382 /* Get the Jth argument in the parameter pack. */
4383 arg = TREE_VEC_ELT (packed_args, j);
4385 if (PACK_EXPANSION_P (arg))
4387 /* Pack expansions must come at the end of the
4388 argument list. */
4389 if ((packed_args && j < len - 1)
4390 || (!packed_args && i < nargs - 1))
4392 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4393 error ("parameter pack argument %qE must be at the "
4394 "end of the template argument list", arg);
4395 else
4396 error ("parameter pack argument %qT must be at the "
4397 "end of the template argument list", arg);
4401 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4402 /* We only care about the pattern. */
4403 arg = PACK_EXPANSION_PATTERN (arg);
4405 if (/* These first two lines are the `non-type' bit. */
4406 !TYPE_P (arg)
4407 && TREE_CODE (arg) != TEMPLATE_DECL
4408 /* This next two lines are the `argument expression is not just a
4409 simple identifier' condition and also the `specialized
4410 non-type argument' bit. */
4411 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4412 && !(REFERENCE_REF_P (arg)
4413 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4415 if ((!packed_args && tpd.arg_uses_template_parms[i])
4416 || (packed_args && uses_template_parms (arg)))
4417 error ("template argument %qE involves template parameter(s)",
4418 arg);
4419 else
4421 /* Look at the corresponding template parameter,
4422 marking which template parameters its type depends
4423 upon. */
4424 tree type = TREE_TYPE (parm);
4426 if (!tpd2.parms)
4428 /* We haven't yet initialized TPD2. Do so now. */
4429 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4430 /* The number of parameters here is the number in the
4431 main template, which, as checked in the assertion
4432 above, is NARGS. */
4433 tpd2.parms = XALLOCAVEC (int, nargs);
4434 tpd2.level =
4435 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4438 /* Mark the template parameters. But this time, we're
4439 looking for the template parameters of the main
4440 template, not in the specialization. */
4441 tpd2.current_arg = i;
4442 tpd2.arg_uses_template_parms[i] = 0;
4443 memset (tpd2.parms, 0, sizeof (int) * nargs);
4444 for_each_template_parm (type,
4445 &mark_template_parm,
4446 &tpd2,
4447 NULL,
4448 /*include_nondeduced_p=*/false);
4450 if (tpd2.arg_uses_template_parms [i])
4452 /* The type depended on some template parameters.
4453 If they are fully specialized in the
4454 specialization, that's OK. */
4455 int j;
4456 int count = 0;
4457 for (j = 0; j < nargs; ++j)
4458 if (tpd2.parms[j] != 0
4459 && tpd.arg_uses_template_parms [j])
4460 ++count;
4461 if (count != 0)
4462 error_n (input_location, count,
4463 "type %qT of template argument %qE depends "
4464 "on a template parameter",
4465 "type %qT of template argument %qE depends "
4466 "on template parameters",
4467 type,
4468 arg);
4475 /* We should only get here once. */
4476 gcc_assert (!COMPLETE_TYPE_P (type));
4478 // Build the template decl.
4479 tree tmpl = build_template_decl (decl,
4480 current_template_parms,
4481 current_template_reqs,
4482 DECL_MEMBER_TEMPLATE_P (maintmpl));
4483 TREE_TYPE (tmpl) = type;
4484 DECL_TEMPLATE_RESULT (tmpl) = decl;
4485 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4486 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4487 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4489 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4490 = tree_cons (specargs, tmpl,
4491 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4492 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4494 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4495 inst = TREE_CHAIN (inst))
4497 tree inst_type = TREE_VALUE (inst);
4498 if (COMPLETE_TYPE_P (inst_type)
4499 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4501 tree spec = most_specialized_class (inst_type, tf_none);
4502 if (spec && TREE_TYPE (spec) == type)
4503 permerror (input_location,
4504 "partial specialization of %qT after instantiation "
4505 "of %qT", type, inst_type);
4509 return decl;
4512 /* PARM is a template parameter of some form; return the corresponding
4513 TEMPLATE_PARM_INDEX. */
4515 static tree
4516 get_template_parm_index (tree parm)
4518 if (TREE_CODE (parm) == PARM_DECL
4519 || TREE_CODE (parm) == CONST_DECL)
4520 parm = DECL_INITIAL (parm);
4521 else if (TREE_CODE (parm) == TYPE_DECL
4522 || TREE_CODE (parm) == TEMPLATE_DECL)
4523 parm = TREE_TYPE (parm);
4524 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4525 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4526 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4527 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4528 return parm;
4531 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4532 parameter packs used by the template parameter PARM. */
4534 static void
4535 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4537 /* A type parm can't refer to another parm. */
4538 if (TREE_CODE (parm) == TYPE_DECL)
4539 return;
4540 else if (TREE_CODE (parm) == PARM_DECL)
4542 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4543 ppd, ppd->visited);
4544 return;
4547 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4549 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4550 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4551 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4554 /* PARM is a template parameter pack. Return any parameter packs used in
4555 its type or the type of any of its template parameters. If there are
4556 any such packs, it will be instantiated into a fixed template parameter
4557 list by partial instantiation rather than be fully deduced. */
4559 tree
4560 fixed_parameter_pack_p (tree parm)
4562 /* This can only be true in a member template. */
4563 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4564 return NULL_TREE;
4565 /* This can only be true for a parameter pack. */
4566 if (!template_parameter_pack_p (parm))
4567 return NULL_TREE;
4568 /* A type parm can't refer to another parm. */
4569 if (TREE_CODE (parm) == TYPE_DECL)
4570 return NULL_TREE;
4572 tree parameter_packs = NULL_TREE;
4573 struct find_parameter_pack_data ppd;
4574 ppd.parameter_packs = &parameter_packs;
4575 ppd.visited = pointer_set_create ();
4577 fixed_parameter_pack_p_1 (parm, &ppd);
4579 pointer_set_destroy (ppd.visited);
4580 return parameter_packs;
4583 /* Check that a template declaration's use of default arguments and
4584 parameter packs is not invalid. Here, PARMS are the template
4585 parameters. IS_PRIMARY is true if DECL is the thing declared by
4586 a primary template. IS_PARTIAL is true if DECL is a partial
4587 specialization.
4589 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4590 declaration (but not a definition); 1 indicates a declaration, 2
4591 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4592 emitted for extraneous default arguments.
4594 Returns TRUE if there were no errors found, FALSE otherwise. */
4596 bool
4597 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4598 bool is_partial, int is_friend_decl)
4600 const char *msg;
4601 int last_level_to_check;
4602 tree parm_level;
4603 bool no_errors = true;
4605 /* [temp.param]
4607 A default template-argument shall not be specified in a
4608 function template declaration or a function template definition, nor
4609 in the template-parameter-list of the definition of a member of a
4610 class template. */
4612 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4613 /* You can't have a function template declaration in a local
4614 scope, nor you can you define a member of a class template in a
4615 local scope. */
4616 return true;
4618 if (TREE_CODE (decl) == TYPE_DECL
4619 && TREE_TYPE (decl)
4620 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4621 /* A lambda doesn't have an explicit declaration; don't complain
4622 about the parms of the enclosing class. */
4623 return true;
4625 if (current_class_type
4626 && !TYPE_BEING_DEFINED (current_class_type)
4627 && DECL_LANG_SPECIFIC (decl)
4628 && DECL_DECLARES_FUNCTION_P (decl)
4629 /* If this is either a friend defined in the scope of the class
4630 or a member function. */
4631 && (DECL_FUNCTION_MEMBER_P (decl)
4632 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4633 : DECL_FRIEND_CONTEXT (decl)
4634 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4635 : false)
4636 /* And, if it was a member function, it really was defined in
4637 the scope of the class. */
4638 && (!DECL_FUNCTION_MEMBER_P (decl)
4639 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4640 /* We already checked these parameters when the template was
4641 declared, so there's no need to do it again now. This function
4642 was defined in class scope, but we're processing its body now
4643 that the class is complete. */
4644 return true;
4646 /* Core issue 226 (C++0x only): the following only applies to class
4647 templates. */
4648 if (is_primary
4649 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4651 /* [temp.param]
4653 If a template-parameter has a default template-argument, all
4654 subsequent template-parameters shall have a default
4655 template-argument supplied. */
4656 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4658 tree inner_parms = TREE_VALUE (parm_level);
4659 int ntparms = TREE_VEC_LENGTH (inner_parms);
4660 int seen_def_arg_p = 0;
4661 int i;
4663 for (i = 0; i < ntparms; ++i)
4665 tree parm = TREE_VEC_ELT (inner_parms, i);
4667 if (parm == error_mark_node)
4668 continue;
4670 if (TREE_PURPOSE (parm))
4671 seen_def_arg_p = 1;
4672 else if (seen_def_arg_p
4673 && !template_parameter_pack_p (TREE_VALUE (parm)))
4675 error ("no default argument for %qD", TREE_VALUE (parm));
4676 /* For better subsequent error-recovery, we indicate that
4677 there should have been a default argument. */
4678 TREE_PURPOSE (parm) = error_mark_node;
4679 no_errors = false;
4681 else if (!is_partial
4682 && !is_friend_decl
4683 /* Don't complain about an enclosing partial
4684 specialization. */
4685 && parm_level == parms
4686 && TREE_CODE (decl) == TYPE_DECL
4687 && i < ntparms - 1
4688 && template_parameter_pack_p (TREE_VALUE (parm))
4689 /* A fixed parameter pack will be partially
4690 instantiated into a fixed length list. */
4691 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4693 /* A primary class template can only have one
4694 parameter pack, at the end of the template
4695 parameter list. */
4697 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4698 error ("parameter pack %qE must be at the end of the"
4699 " template parameter list", TREE_VALUE (parm));
4700 else
4701 error ("parameter pack %qT must be at the end of the"
4702 " template parameter list",
4703 TREE_TYPE (TREE_VALUE (parm)));
4705 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4706 = error_mark_node;
4707 no_errors = false;
4713 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4714 || is_partial
4715 || !is_primary
4716 || is_friend_decl)
4717 /* For an ordinary class template, default template arguments are
4718 allowed at the innermost level, e.g.:
4719 template <class T = int>
4720 struct S {};
4721 but, in a partial specialization, they're not allowed even
4722 there, as we have in [temp.class.spec]:
4724 The template parameter list of a specialization shall not
4725 contain default template argument values.
4727 So, for a partial specialization, or for a function template
4728 (in C++98/C++03), we look at all of them. */
4730 else
4731 /* But, for a primary class template that is not a partial
4732 specialization we look at all template parameters except the
4733 innermost ones. */
4734 parms = TREE_CHAIN (parms);
4736 /* Figure out what error message to issue. */
4737 if (is_friend_decl == 2)
4738 msg = G_("default template arguments may not be used in function template "
4739 "friend re-declaration");
4740 else if (is_friend_decl)
4741 msg = G_("default template arguments may not be used in function template "
4742 "friend declarations");
4743 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4744 msg = G_("default template arguments may not be used in function templates "
4745 "without -std=c++11 or -std=gnu++11");
4746 else if (is_partial)
4747 msg = G_("default template arguments may not be used in "
4748 "partial specializations");
4749 else
4750 msg = G_("default argument for template parameter for class enclosing %qD");
4752 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4753 /* If we're inside a class definition, there's no need to
4754 examine the parameters to the class itself. On the one
4755 hand, they will be checked when the class is defined, and,
4756 on the other, default arguments are valid in things like:
4757 template <class T = double>
4758 struct S { template <class U> void f(U); };
4759 Here the default argument for `S' has no bearing on the
4760 declaration of `f'. */
4761 last_level_to_check = template_class_depth (current_class_type) + 1;
4762 else
4763 /* Check everything. */
4764 last_level_to_check = 0;
4766 for (parm_level = parms;
4767 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4768 parm_level = TREE_CHAIN (parm_level))
4770 tree inner_parms = TREE_VALUE (parm_level);
4771 int i;
4772 int ntparms;
4774 ntparms = TREE_VEC_LENGTH (inner_parms);
4775 for (i = 0; i < ntparms; ++i)
4777 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4778 continue;
4780 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4782 if (msg)
4784 no_errors = false;
4785 if (is_friend_decl == 2)
4786 return no_errors;
4788 error (msg, decl);
4789 msg = 0;
4792 /* Clear out the default argument so that we are not
4793 confused later. */
4794 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4798 /* At this point, if we're still interested in issuing messages,
4799 they must apply to classes surrounding the object declared. */
4800 if (msg)
4801 msg = G_("default argument for template parameter for class "
4802 "enclosing %qD");
4805 return no_errors;
4808 /* Worker for push_template_decl_real, called via
4809 for_each_template_parm. DATA is really an int, indicating the
4810 level of the parameters we are interested in. If T is a template
4811 parameter of that level, return nonzero. */
4813 static int
4814 template_parm_this_level_p (tree t, void* data)
4816 int this_level = *(int *)data;
4817 int level;
4819 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4820 level = TEMPLATE_PARM_LEVEL (t);
4821 else
4822 level = TEMPLATE_TYPE_LEVEL (t);
4823 return level == this_level;
4826 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4827 parameters given by current_template_args, or reuses a
4828 previously existing one, if appropriate. Returns the DECL, or an
4829 equivalent one, if it is replaced via a call to duplicate_decls.
4831 If IS_FRIEND is true, DECL is a friend declaration. */
4833 tree
4834 push_template_decl_real (tree decl, bool is_friend)
4836 tree tmpl;
4837 tree args;
4838 tree info;
4839 tree ctx;
4840 bool is_primary;
4841 bool is_partial;
4842 int new_template_p = 0;
4843 /* True if the template is a member template, in the sense of
4844 [temp.mem]. */
4845 bool member_template_p = false;
4847 if (decl == error_mark_node || !current_template_parms)
4848 return error_mark_node;
4850 /* See if this is a partial specialization. */
4851 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4852 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4853 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4855 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4856 is_friend = true;
4858 if (is_friend)
4859 /* For a friend, we want the context of the friend function, not
4860 the type of which it is a friend. */
4861 ctx = CP_DECL_CONTEXT (decl);
4862 else if (CP_DECL_CONTEXT (decl)
4863 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4864 /* In the case of a virtual function, we want the class in which
4865 it is defined. */
4866 ctx = CP_DECL_CONTEXT (decl);
4867 else
4868 /* Otherwise, if we're currently defining some class, the DECL
4869 is assumed to be a member of the class. */
4870 ctx = current_scope ();
4872 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4873 ctx = NULL_TREE;
4875 if (!DECL_CONTEXT (decl))
4876 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4878 /* See if this is a primary template. */
4879 if (is_friend && ctx
4880 && uses_template_parms_level (ctx, processing_template_decl))
4881 /* A friend template that specifies a class context, i.e.
4882 template <typename T> friend void A<T>::f();
4883 is not primary. */
4884 is_primary = false;
4885 else
4886 is_primary = template_parm_scope_p ();
4888 if (is_primary)
4890 if (DECL_CLASS_SCOPE_P (decl))
4891 member_template_p = true;
4892 if (TREE_CODE (decl) == TYPE_DECL
4893 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4895 error ("template class without a name");
4896 return error_mark_node;
4898 else if (TREE_CODE (decl) == FUNCTION_DECL)
4900 if (DECL_DESTRUCTOR_P (decl))
4902 /* [temp.mem]
4904 A destructor shall not be a member template. */
4905 error ("destructor %qD declared as member template", decl);
4906 return error_mark_node;
4908 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4909 && (!prototype_p (TREE_TYPE (decl))
4910 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4911 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4912 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4913 == void_list_node)))
4915 /* [basic.stc.dynamic.allocation]
4917 An allocation function can be a function
4918 template. ... Template allocation functions shall
4919 have two or more parameters. */
4920 error ("invalid template declaration of %qD", decl);
4921 return error_mark_node;
4924 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4925 && CLASS_TYPE_P (TREE_TYPE (decl)))
4926 /* OK */;
4927 else if (TREE_CODE (decl) == TYPE_DECL
4928 && TYPE_DECL_ALIAS_P (decl))
4929 /* alias-declaration */
4930 gcc_assert (!DECL_ARTIFICIAL (decl));
4931 else
4933 error ("template declaration of %q#D", decl);
4934 return error_mark_node;
4938 /* Check to see that the rules regarding the use of default
4939 arguments are not being violated. */
4940 check_default_tmpl_args (decl, current_template_parms,
4941 is_primary, is_partial, /*is_friend_decl=*/0);
4943 /* Ensure that there are no parameter packs in the type of this
4944 declaration that have not been expanded. */
4945 if (TREE_CODE (decl) == FUNCTION_DECL)
4947 /* Check each of the arguments individually to see if there are
4948 any bare parameter packs. */
4949 tree type = TREE_TYPE (decl);
4950 tree arg = DECL_ARGUMENTS (decl);
4951 tree argtype = TYPE_ARG_TYPES (type);
4953 while (arg && argtype)
4955 if (!DECL_PACK_P (arg)
4956 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4958 /* This is a PARM_DECL that contains unexpanded parameter
4959 packs. We have already complained about this in the
4960 check_for_bare_parameter_packs call, so just replace
4961 these types with ERROR_MARK_NODE. */
4962 TREE_TYPE (arg) = error_mark_node;
4963 TREE_VALUE (argtype) = error_mark_node;
4966 arg = DECL_CHAIN (arg);
4967 argtype = TREE_CHAIN (argtype);
4970 /* Check for bare parameter packs in the return type and the
4971 exception specifiers. */
4972 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4973 /* Errors were already issued, set return type to int
4974 as the frontend doesn't expect error_mark_node as
4975 the return type. */
4976 TREE_TYPE (type) = integer_type_node;
4977 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4978 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4980 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4981 && TYPE_DECL_ALIAS_P (decl))
4982 ? DECL_ORIGINAL_TYPE (decl)
4983 : TREE_TYPE (decl)))
4985 TREE_TYPE (decl) = error_mark_node;
4986 return error_mark_node;
4989 if (is_partial)
4990 return process_partial_specialization (decl);
4992 args = current_template_args ();
4994 if (!ctx
4995 || TREE_CODE (ctx) == FUNCTION_DECL
4996 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4997 || (TREE_CODE (decl) == TYPE_DECL
4998 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4999 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5001 if (DECL_LANG_SPECIFIC (decl)
5002 && DECL_TEMPLATE_INFO (decl)
5003 && DECL_TI_TEMPLATE (decl))
5004 tmpl = DECL_TI_TEMPLATE (decl);
5005 /* If DECL is a TYPE_DECL for a class-template, then there won't
5006 be DECL_LANG_SPECIFIC. The information equivalent to
5007 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5008 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5009 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5010 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5012 /* Since a template declaration already existed for this
5013 class-type, we must be redeclaring it here. Make sure
5014 that the redeclaration is valid. */
5015 redeclare_class_template (TREE_TYPE (decl),
5016 current_template_parms,
5017 current_template_reqs);
5018 /* We don't need to create a new TEMPLATE_DECL; just use the
5019 one we already had. */
5020 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5022 else
5024 tmpl = build_template_decl (decl,
5025 current_template_parms,
5026 current_template_reqs,
5027 member_template_p);
5028 new_template_p = 1;
5030 if (DECL_LANG_SPECIFIC (decl)
5031 && DECL_TEMPLATE_SPECIALIZATION (decl))
5033 /* A specialization of a member template of a template
5034 class. */
5035 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5036 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5037 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5041 else
5043 tree a, t, current, parms;
5044 int i;
5045 tree tinfo = get_template_info (decl);
5047 if (!tinfo)
5049 error ("template definition of non-template %q#D", decl);
5050 return error_mark_node;
5053 tmpl = TI_TEMPLATE (tinfo);
5055 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5056 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5057 && DECL_TEMPLATE_SPECIALIZATION (decl)
5058 && DECL_MEMBER_TEMPLATE_P (tmpl))
5060 tree new_tmpl;
5062 /* The declaration is a specialization of a member
5063 template, declared outside the class. Therefore, the
5064 innermost template arguments will be NULL, so we
5065 replace them with the arguments determined by the
5066 earlier call to check_explicit_specialization. */
5067 args = DECL_TI_ARGS (decl);
5069 new_tmpl = build_template_decl (decl,
5070 current_template_parms,
5071 current_template_reqs,
5072 member_template_p);
5073 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5074 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5075 DECL_TI_TEMPLATE (decl) = new_tmpl;
5076 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5077 DECL_TEMPLATE_INFO (new_tmpl) = build_template_info (tmpl, args);
5079 register_specialization (new_tmpl,
5080 most_general_template (tmpl),
5081 args,
5082 is_friend, 0);
5083 return decl;
5086 /* Make sure the template headers we got make sense. */
5088 parms = DECL_TEMPLATE_PARMS (tmpl);
5089 i = TMPL_PARMS_DEPTH (parms);
5090 if (TMPL_ARGS_DEPTH (args) != i)
5092 error ("expected %d levels of template parms for %q#D, got %d",
5093 i, decl, TMPL_ARGS_DEPTH (args));
5094 DECL_INTERFACE_KNOWN (decl) = 1;
5095 return error_mark_node;
5097 else
5098 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5100 a = TMPL_ARGS_LEVEL (args, i);
5101 t = INNERMOST_TEMPLATE_PARMS (parms);
5103 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5105 if (current == decl)
5106 error ("got %d template parameters for %q#D",
5107 TREE_VEC_LENGTH (a), decl);
5108 else
5109 error ("got %d template parameters for %q#T",
5110 TREE_VEC_LENGTH (a), current);
5111 error (" but %d required", TREE_VEC_LENGTH (t));
5112 /* Avoid crash in import_export_decl. */
5113 DECL_INTERFACE_KNOWN (decl) = 1;
5114 return error_mark_node;
5117 if (current == decl)
5118 current = ctx;
5119 else if (current == NULL_TREE)
5120 /* Can happen in erroneous input. */
5121 break;
5122 else
5123 current = get_containing_scope (current);
5126 /* Check that the parms are used in the appropriate qualifying scopes
5127 in the declarator. */
5128 if (!comp_template_args
5129 (TI_ARGS (tinfo),
5130 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5132 error ("\
5133 template arguments to %qD do not match original template %qD",
5134 decl, DECL_TEMPLATE_RESULT (tmpl));
5135 if (!uses_template_parms (TI_ARGS (tinfo)))
5136 inform (input_location, "use template<> for an explicit specialization");
5137 /* Avoid crash in import_export_decl. */
5138 DECL_INTERFACE_KNOWN (decl) = 1;
5139 return error_mark_node;
5143 DECL_TEMPLATE_RESULT (tmpl) = decl;
5144 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5146 /* Push template declarations for global functions and types. Note
5147 that we do not try to push a global template friend declared in a
5148 template class; such a thing may well depend on the template
5149 parameters of the class. */
5150 if (new_template_p && !ctx
5151 && !(is_friend && template_class_depth (current_class_type) > 0))
5153 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5154 if (tmpl == error_mark_node)
5155 return error_mark_node;
5157 /* Hide template friend classes that haven't been declared yet. */
5158 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5160 DECL_ANTICIPATED (tmpl) = 1;
5161 DECL_FRIEND_P (tmpl) = 1;
5165 if (is_primary)
5167 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5168 int i;
5170 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5171 if (DECL_CONV_FN_P (tmpl))
5173 int depth = TMPL_PARMS_DEPTH (parms);
5175 /* It is a conversion operator. See if the type converted to
5176 depends on innermost template operands. */
5178 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5179 depth))
5180 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5183 /* Give template template parms a DECL_CONTEXT of the template
5184 for which they are a parameter. */
5185 parms = INNERMOST_TEMPLATE_PARMS (parms);
5186 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5188 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5189 if (TREE_CODE (parm) == TEMPLATE_DECL)
5190 DECL_CONTEXT (parm) = tmpl;
5194 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5195 back to its most general template. If TMPL is a specialization,
5196 ARGS may only have the innermost set of arguments. Add the missing
5197 argument levels if necessary. */
5198 if (DECL_TEMPLATE_INFO (tmpl))
5199 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5201 info = build_template_info (tmpl, args);
5203 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5204 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5205 else
5207 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5208 retrofit_lang_decl (decl);
5209 if (DECL_LANG_SPECIFIC (decl))
5210 DECL_TEMPLATE_INFO (decl) = info;
5213 return DECL_TEMPLATE_RESULT (tmpl);
5216 tree
5217 push_template_decl (tree decl)
5219 return push_template_decl_real (decl, false);
5222 /* FN is an inheriting constructor that inherits from the constructor
5223 template INHERITED; turn FN into a constructor template with a matching
5224 template header. */
5226 tree
5227 add_inherited_template_parms (tree fn, tree inherited)
5229 tree inner_parms
5230 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5231 inner_parms = copy_node (inner_parms);
5232 tree parms
5233 = tree_cons (size_int (processing_template_decl + 1),
5234 inner_parms, current_template_parms);
5235 tree tmpl = build_template_decl (fn, parms, NULL_TREE, /*member*/true);
5236 tree args = template_parms_to_args (parms);
5238 // If the inherited constructor was constrained, then also
5239 // propagate the constraints to the new declaration by
5240 // rewriting them in terms of the local template parameters.
5241 tree cons = DECL_CONSTRAINTS (inherited);
5242 if (cons)
5244 ++processing_template_decl;
5245 tree reqs = instantiate_requirements (CI_REQUIREMENTS (cons), args);
5246 --processing_template_decl;
5247 DECL_CONSTRAINTS (tmpl) = make_constraints (reqs);
5250 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5251 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5252 DECL_TEMPLATE_RESULT (tmpl) = fn;
5253 DECL_ARTIFICIAL (tmpl) = true;
5254 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5255 return tmpl;
5258 /* Called when a class template TYPE is redeclared with the indicated
5259 template PARMS, e.g.:
5261 template <class T> struct S;
5262 template <class T> struct S {}; */
5264 bool
5265 redeclare_class_template (tree type, tree parms, tree cons)
5267 tree tmpl;
5268 tree tmpl_parms;
5269 int i;
5271 if (!TYPE_TEMPLATE_INFO (type))
5273 error ("%qT is not a template type", type);
5274 return false;
5277 tmpl = TYPE_TI_TEMPLATE (type);
5278 if (!PRIMARY_TEMPLATE_P (tmpl))
5279 /* The type is nested in some template class. Nothing to worry
5280 about here; there are no new template parameters for the nested
5281 type. */
5282 return true;
5284 if (!parms)
5286 error ("template specifiers not specified in declaration of %qD",
5287 tmpl);
5288 return false;
5291 parms = INNERMOST_TEMPLATE_PARMS (parms);
5292 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5294 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5296 error_n (input_location, TREE_VEC_LENGTH (parms),
5297 "redeclared with %d template parameter",
5298 "redeclared with %d template parameters",
5299 TREE_VEC_LENGTH (parms));
5300 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5301 "previous declaration %q+D used %d template parameter",
5302 "previous declaration %q+D used %d template parameters",
5303 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5304 return false;
5307 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5309 tree tmpl_parm;
5310 tree parm;
5311 tree tmpl_default;
5312 tree parm_default;
5314 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5315 || TREE_VEC_ELT (parms, i) == error_mark_node)
5316 continue;
5318 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5319 if (tmpl_parm == error_mark_node)
5320 return false;
5322 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5323 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5324 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5326 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5327 TEMPLATE_DECL. */
5328 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5329 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5330 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5331 || (TREE_CODE (tmpl_parm) != PARM_DECL
5332 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5333 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5334 || (TREE_CODE (tmpl_parm) == PARM_DECL
5335 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5336 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5338 error ("template parameter %q+#D", tmpl_parm);
5339 error ("redeclared here as %q#D", parm);
5340 return false;
5343 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5345 /* We have in [temp.param]:
5347 A template-parameter may not be given default arguments
5348 by two different declarations in the same scope. */
5349 error_at (input_location, "redefinition of default argument for %q#D", parm);
5350 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5351 "original definition appeared here");
5352 return false;
5355 if (parm_default != NULL_TREE)
5356 /* Update the previous template parameters (which are the ones
5357 that will really count) with the new default value. */
5358 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5359 else if (tmpl_default != NULL_TREE)
5360 /* Update the new parameters, too; they'll be used as the
5361 parameters for any members. */
5362 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5365 // Cannot redeclare a class template with a different set of constraints.
5366 if (!equivalent_constraints (DECL_CONSTRAINTS (tmpl), cons))
5368 error_at (input_location, "redeclaration %q#D with different "
5369 "constraints", tmpl);
5370 inform (DECL_SOURCE_LOCATION (tmpl),
5371 "original declaration appeared here");
5374 return true;
5377 /* Simplify EXPR if it is a non-dependent expression. Returns the
5378 (possibly simplified) expression. */
5380 tree
5381 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5383 if (expr == NULL_TREE)
5384 return NULL_TREE;
5386 /* If we're in a template, but EXPR isn't value dependent, simplify
5387 it. We're supposed to treat:
5389 template <typename T> void f(T[1 + 1]);
5390 template <typename T> void f(T[2]);
5392 as two declarations of the same function, for example. */
5393 if (processing_template_decl
5394 && !instantiation_dependent_expression_p (expr)
5395 && potential_constant_expression (expr))
5397 HOST_WIDE_INT saved_processing_template_decl;
5399 saved_processing_template_decl = processing_template_decl;
5400 processing_template_decl = 0;
5401 expr = tsubst_copy_and_build (expr,
5402 /*args=*/NULL_TREE,
5403 complain,
5404 /*in_decl=*/NULL_TREE,
5405 /*function_p=*/false,
5406 /*integral_constant_expression_p=*/true);
5407 processing_template_decl = saved_processing_template_decl;
5409 return expr;
5412 tree
5413 fold_non_dependent_expr (tree expr)
5415 return fold_non_dependent_expr_sfinae (expr, tf_error);
5418 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5419 template declaration, or a TYPE_DECL for an alias declaration. */
5421 bool
5422 alias_type_or_template_p (tree t)
5424 if (t == NULL_TREE)
5425 return false;
5426 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5427 || (TYPE_P (t)
5428 && TYPE_NAME (t)
5429 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5430 || DECL_ALIAS_TEMPLATE_P (t));
5433 /* Return TRUE iff is a specialization of an alias template. */
5435 bool
5436 alias_template_specialization_p (const_tree t)
5438 if (t == NULL_TREE)
5439 return false;
5441 return (TYPE_P (t)
5442 && TYPE_TEMPLATE_INFO (t)
5443 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5444 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5447 /* Return the number of innermost template parameters in TMPL. */
5449 static int
5450 num_innermost_template_parms (tree tmpl)
5452 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5453 return TREE_VEC_LENGTH (parms);
5456 /* Return either TMPL or another template that it is equivalent to under DR
5457 1286: An alias that just changes the name of a template is equivalent to
5458 the other template. */
5460 static tree
5461 get_underlying_template (tree tmpl)
5463 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5464 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5466 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5467 if (TYPE_TEMPLATE_INFO (result))
5469 tree sub = TYPE_TI_TEMPLATE (result);
5470 if (PRIMARY_TEMPLATE_P (sub)
5471 && (num_innermost_template_parms (tmpl)
5472 == num_innermost_template_parms (sub)))
5474 tree alias_args = INNERMOST_TEMPLATE_ARGS
5475 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5476 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5477 break;
5478 /* The alias type is equivalent to the pattern of the
5479 underlying template, so strip the alias. */
5480 tmpl = sub;
5481 continue;
5484 break;
5486 return tmpl;
5489 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5490 must be a function or a pointer-to-function type, as specified
5491 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5492 and check that the resulting function has external linkage. */
5494 static tree
5495 convert_nontype_argument_function (tree type, tree expr)
5497 tree fns = expr;
5498 tree fn, fn_no_ptr;
5499 linkage_kind linkage;
5501 fn = instantiate_type (type, fns, tf_none);
5502 if (fn == error_mark_node)
5503 return error_mark_node;
5505 fn_no_ptr = fn;
5506 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5507 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5508 if (BASELINK_P (fn_no_ptr))
5509 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5511 /* [temp.arg.nontype]/1
5513 A template-argument for a non-type, non-template template-parameter
5514 shall be one of:
5515 [...]
5516 -- the address of an object or function with external [C++11: or
5517 internal] linkage. */
5519 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5521 error ("%qE is not a valid template argument for type %qT", expr, type);
5522 if (TYPE_PTR_P (type))
5523 error ("it must be the address of a function with external linkage");
5524 else
5525 error ("it must be the name of a function with external linkage");
5526 return NULL_TREE;
5529 linkage = decl_linkage (fn_no_ptr);
5530 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5532 if (cxx_dialect >= cxx11)
5533 error ("%qE is not a valid template argument for type %qT "
5534 "because %qD has no linkage",
5535 expr, type, fn_no_ptr);
5536 else
5537 error ("%qE is not a valid template argument for type %qT "
5538 "because %qD does not have external linkage",
5539 expr, type, fn_no_ptr);
5540 return NULL_TREE;
5543 return fn;
5546 /* Subroutine of convert_nontype_argument.
5547 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5548 Emit an error otherwise. */
5550 static bool
5551 check_valid_ptrmem_cst_expr (tree type, tree expr,
5552 tsubst_flags_t complain)
5554 STRIP_NOPS (expr);
5555 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5556 return true;
5557 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5558 return true;
5559 if (complain & tf_error)
5561 error ("%qE is not a valid template argument for type %qT",
5562 expr, type);
5563 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5565 return false;
5568 /* Returns TRUE iff the address of OP is value-dependent.
5570 14.6.2.4 [temp.dep.temp]:
5571 A non-integral non-type template-argument is dependent if its type is
5572 dependent or it has either of the following forms
5573 qualified-id
5574 & qualified-id
5575 and contains a nested-name-specifier which specifies a class-name that
5576 names a dependent type.
5578 We generalize this to just say that the address of a member of a
5579 dependent class is value-dependent; the above doesn't cover the
5580 address of a static data member named with an unqualified-id. */
5582 static bool
5583 has_value_dependent_address (tree op)
5585 /* We could use get_inner_reference here, but there's no need;
5586 this is only relevant for template non-type arguments, which
5587 can only be expressed as &id-expression. */
5588 if (DECL_P (op))
5590 tree ctx = CP_DECL_CONTEXT (op);
5591 if (TYPE_P (ctx) && dependent_type_p (ctx))
5592 return true;
5595 return false;
5598 /* The next set of functions are used for providing helpful explanatory
5599 diagnostics for failed overload resolution. Their messages should be
5600 indented by two spaces for consistency with the messages in
5601 call.c */
5603 static int
5604 unify_success (bool /*explain_p*/)
5606 return 0;
5609 static int
5610 unify_parameter_deduction_failure (bool explain_p, tree parm)
5612 if (explain_p)
5613 inform (input_location,
5614 " couldn't deduce template parameter %qD", parm);
5615 return 1;
5618 static int
5619 unify_invalid (bool /*explain_p*/)
5621 return 1;
5624 static int
5625 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5627 if (explain_p)
5628 inform (input_location,
5629 " types %qT and %qT have incompatible cv-qualifiers",
5630 parm, arg);
5631 return 1;
5634 static int
5635 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5637 if (explain_p)
5638 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5639 return 1;
5642 static int
5643 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5645 if (explain_p)
5646 inform (input_location,
5647 " template parameter %qD is not a parameter pack, but "
5648 "argument %qD is",
5649 parm, arg);
5650 return 1;
5653 static int
5654 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5656 if (explain_p)
5657 inform (input_location,
5658 " template argument %qE does not match "
5659 "pointer-to-member constant %qE",
5660 arg, parm);
5661 return 1;
5664 static int
5665 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5667 if (explain_p)
5668 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5669 return 1;
5672 static int
5673 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5675 if (explain_p)
5676 inform (input_location,
5677 " inconsistent parameter pack deduction with %qT and %qT",
5678 old_arg, new_arg);
5679 return 1;
5682 static int
5683 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5685 if (explain_p)
5687 if (TYPE_P (parm))
5688 inform (input_location,
5689 " deduced conflicting types for parameter %qT (%qT and %qT)",
5690 parm, first, second);
5691 else
5692 inform (input_location,
5693 " deduced conflicting values for non-type parameter "
5694 "%qE (%qE and %qE)", parm, first, second);
5696 return 1;
5699 static int
5700 unify_vla_arg (bool explain_p, tree arg)
5702 if (explain_p)
5703 inform (input_location,
5704 " variable-sized array type %qT is not "
5705 "a valid template argument",
5706 arg);
5707 return 1;
5710 static int
5711 unify_method_type_error (bool explain_p, tree arg)
5713 if (explain_p)
5714 inform (input_location,
5715 " member function type %qT is not a valid template argument",
5716 arg);
5717 return 1;
5720 static int
5721 unify_arity (bool explain_p, int have, int wanted)
5723 if (explain_p)
5724 inform_n (input_location, wanted,
5725 " candidate expects %d argument, %d provided",
5726 " candidate expects %d arguments, %d provided",
5727 wanted, have);
5728 return 1;
5731 static int
5732 unify_too_many_arguments (bool explain_p, int have, int wanted)
5734 return unify_arity (explain_p, have, wanted);
5737 static int
5738 unify_too_few_arguments (bool explain_p, int have, int wanted)
5740 return unify_arity (explain_p, have, wanted);
5743 static int
5744 unify_arg_conversion (bool explain_p, tree to_type,
5745 tree from_type, tree arg)
5747 if (explain_p)
5748 inform (EXPR_LOC_OR_LOC (arg, input_location),
5749 " cannot convert %qE (type %qT) to type %qT",
5750 arg, from_type, to_type);
5751 return 1;
5754 static int
5755 unify_no_common_base (bool explain_p, enum template_base_result r,
5756 tree parm, tree arg)
5758 if (explain_p)
5759 switch (r)
5761 case tbr_ambiguous_baseclass:
5762 inform (input_location, " %qT is an ambiguous base class of %qT",
5763 parm, arg);
5764 break;
5765 default:
5766 inform (input_location, " %qT is not derived from %qT", arg, parm);
5767 break;
5769 return 1;
5772 static int
5773 unify_inconsistent_template_template_parameters (bool explain_p)
5775 if (explain_p)
5776 inform (input_location,
5777 " template parameters of a template template argument are "
5778 "inconsistent with other deduced template arguments");
5779 return 1;
5782 static int
5783 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5785 if (explain_p)
5786 inform (input_location,
5787 " can't deduce a template for %qT from non-template type %qT",
5788 parm, arg);
5789 return 1;
5792 static int
5793 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5795 if (explain_p)
5796 inform (input_location,
5797 " template argument %qE does not match %qD", arg, parm);
5798 return 1;
5801 static int
5802 unify_overload_resolution_failure (bool explain_p, tree arg)
5804 if (explain_p)
5805 inform (input_location,
5806 " could not resolve address from overloaded function %qE",
5807 arg);
5808 return 1;
5811 /* Attempt to convert the non-type template parameter EXPR to the
5812 indicated TYPE. If the conversion is successful, return the
5813 converted value. If the conversion is unsuccessful, return
5814 NULL_TREE if we issued an error message, or error_mark_node if we
5815 did not. We issue error messages for out-and-out bad template
5816 parameters, but not simply because the conversion failed, since we
5817 might be just trying to do argument deduction. Both TYPE and EXPR
5818 must be non-dependent.
5820 The conversion follows the special rules described in
5821 [temp.arg.nontype], and it is much more strict than an implicit
5822 conversion.
5824 This function is called twice for each template argument (see
5825 lookup_template_class for a more accurate description of this
5826 problem). This means that we need to handle expressions which
5827 are not valid in a C++ source, but can be created from the
5828 first call (for instance, casts to perform conversions). These
5829 hacks can go away after we fix the double coercion problem. */
5831 static tree
5832 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5834 tree expr_type;
5836 /* Detect immediately string literals as invalid non-type argument.
5837 This special-case is not needed for correctness (we would easily
5838 catch this later), but only to provide better diagnostic for this
5839 common user mistake. As suggested by DR 100, we do not mention
5840 linkage issues in the diagnostic as this is not the point. */
5841 /* FIXME we're making this OK. */
5842 if (TREE_CODE (expr) == STRING_CST)
5844 if (complain & tf_error)
5845 error ("%qE is not a valid template argument for type %qT "
5846 "because string literals can never be used in this context",
5847 expr, type);
5848 return NULL_TREE;
5851 /* Add the ADDR_EXPR now for the benefit of
5852 value_dependent_expression_p. */
5853 if (TYPE_PTROBV_P (type)
5854 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5856 expr = decay_conversion (expr, complain);
5857 if (expr == error_mark_node)
5858 return error_mark_node;
5861 /* If we are in a template, EXPR may be non-dependent, but still
5862 have a syntactic, rather than semantic, form. For example, EXPR
5863 might be a SCOPE_REF, rather than the VAR_DECL to which the
5864 SCOPE_REF refers. Preserving the qualifying scope is necessary
5865 so that access checking can be performed when the template is
5866 instantiated -- but here we need the resolved form so that we can
5867 convert the argument. */
5868 if (TYPE_REF_OBJ_P (type)
5869 && has_value_dependent_address (expr))
5870 /* If we want the address and it's value-dependent, don't fold. */;
5871 else if (!type_unknown_p (expr))
5872 expr = fold_non_dependent_expr_sfinae (expr, complain);
5873 if (error_operand_p (expr))
5874 return error_mark_node;
5875 expr_type = TREE_TYPE (expr);
5876 if (TREE_CODE (type) == REFERENCE_TYPE)
5877 expr = mark_lvalue_use (expr);
5878 else
5879 expr = mark_rvalue_use (expr);
5881 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5882 to a non-type argument of "nullptr". */
5883 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5884 expr = convert (type, expr);
5886 /* In C++11, integral or enumeration non-type template arguments can be
5887 arbitrary constant expressions. Pointer and pointer to
5888 member arguments can be general constant expressions that evaluate
5889 to a null value, but otherwise still need to be of a specific form. */
5890 if (cxx_dialect >= cxx11)
5892 if (TREE_CODE (expr) == PTRMEM_CST)
5893 /* A PTRMEM_CST is already constant, and a valid template
5894 argument for a parameter of pointer to member type, we just want
5895 to leave it in that form rather than lower it to a
5896 CONSTRUCTOR. */;
5897 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5898 expr = maybe_constant_value (expr);
5899 else if (TYPE_PTR_OR_PTRMEM_P (type))
5901 tree folded = maybe_constant_value (expr);
5902 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5903 : null_member_pointer_value_p (folded))
5904 expr = folded;
5908 /* HACK: Due to double coercion, we can get a
5909 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5910 which is the tree that we built on the first call (see
5911 below when coercing to reference to object or to reference to
5912 function). We just strip everything and get to the arg.
5913 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5914 for examples. */
5915 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5917 tree probe_type, probe = expr;
5918 if (REFERENCE_REF_P (probe))
5919 probe = TREE_OPERAND (probe, 0);
5920 probe_type = TREE_TYPE (probe);
5921 if (TREE_CODE (probe) == NOP_EXPR)
5923 /* ??? Maybe we could use convert_from_reference here, but we
5924 would need to relax its constraints because the NOP_EXPR
5925 could actually change the type to something more cv-qualified,
5926 and this is not folded by convert_from_reference. */
5927 tree addr = TREE_OPERAND (probe, 0);
5928 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5929 && TREE_CODE (addr) == ADDR_EXPR
5930 && TYPE_PTR_P (TREE_TYPE (addr))
5931 && (same_type_ignoring_top_level_qualifiers_p
5932 (TREE_TYPE (probe_type),
5933 TREE_TYPE (TREE_TYPE (addr)))))
5935 expr = TREE_OPERAND (addr, 0);
5936 expr_type = TREE_TYPE (probe_type);
5941 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5942 parameter is a pointer to object, through decay and
5943 qualification conversion. Let's strip everything. */
5944 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5946 tree probe = expr;
5947 STRIP_NOPS (probe);
5948 if (TREE_CODE (probe) == ADDR_EXPR
5949 && TYPE_PTR_P (TREE_TYPE (probe)))
5951 /* Skip the ADDR_EXPR only if it is part of the decay for
5952 an array. Otherwise, it is part of the original argument
5953 in the source code. */
5954 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5955 probe = TREE_OPERAND (probe, 0);
5956 expr = probe;
5957 expr_type = TREE_TYPE (expr);
5961 /* [temp.arg.nontype]/5, bullet 1
5963 For a non-type template-parameter of integral or enumeration type,
5964 integral promotions (_conv.prom_) and integral conversions
5965 (_conv.integral_) are applied. */
5966 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5968 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5969 t = maybe_constant_value (t);
5970 if (t != error_mark_node)
5971 expr = t;
5973 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5974 return error_mark_node;
5976 /* Notice that there are constant expressions like '4 % 0' which
5977 do not fold into integer constants. */
5978 if (TREE_CODE (expr) != INTEGER_CST)
5980 if (complain & tf_error)
5982 int errs = errorcount, warns = warningcount + werrorcount;
5983 if (processing_template_decl
5984 && !require_potential_constant_expression (expr))
5985 return NULL_TREE;
5986 expr = cxx_constant_value (expr);
5987 if (errorcount > errs || warningcount + werrorcount > warns)
5988 inform (EXPR_LOC_OR_LOC (expr, input_location),
5989 "in template argument for type %qT ", type);
5990 if (expr == error_mark_node)
5991 return NULL_TREE;
5992 /* else cxx_constant_value complained but gave us
5993 a real constant, so go ahead. */
5994 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5996 else
5997 return NULL_TREE;
6000 /* Avoid typedef problems. */
6001 if (TREE_TYPE (expr) != type)
6002 expr = fold_convert (type, expr);
6004 /* [temp.arg.nontype]/5, bullet 2
6006 For a non-type template-parameter of type pointer to object,
6007 qualification conversions (_conv.qual_) and the array-to-pointer
6008 conversion (_conv.array_) are applied. */
6009 else if (TYPE_PTROBV_P (type))
6011 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6013 A template-argument for a non-type, non-template template-parameter
6014 shall be one of: [...]
6016 -- the name of a non-type template-parameter;
6017 -- the address of an object or function with external linkage, [...]
6018 expressed as "& id-expression" where the & is optional if the name
6019 refers to a function or array, or if the corresponding
6020 template-parameter is a reference.
6022 Here, we do not care about functions, as they are invalid anyway
6023 for a parameter of type pointer-to-object. */
6025 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6026 /* Non-type template parameters are OK. */
6028 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6029 /* Null pointer values are OK in C++11. */;
6030 else if (TREE_CODE (expr) != ADDR_EXPR
6031 && TREE_CODE (expr_type) != ARRAY_TYPE)
6033 if (VAR_P (expr))
6035 error ("%qD is not a valid template argument "
6036 "because %qD is a variable, not the address of "
6037 "a variable",
6038 expr, expr);
6039 return NULL_TREE;
6041 if (POINTER_TYPE_P (expr_type))
6043 error ("%qE is not a valid template argument for %qT "
6044 "because it is not the address of a variable",
6045 expr, type);
6046 return NULL_TREE;
6048 /* Other values, like integer constants, might be valid
6049 non-type arguments of some other type. */
6050 return error_mark_node;
6052 else
6054 tree decl;
6056 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6057 ? TREE_OPERAND (expr, 0) : expr);
6058 if (!VAR_P (decl))
6060 error ("%qE is not a valid template argument of type %qT "
6061 "because %qE is not a variable",
6062 expr, type, decl);
6063 return NULL_TREE;
6065 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6067 error ("%qE is not a valid template argument of type %qT "
6068 "because %qD does not have external linkage",
6069 expr, type, decl);
6070 return NULL_TREE;
6072 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6074 error ("%qE is not a valid template argument of type %qT "
6075 "because %qD has no linkage",
6076 expr, type, decl);
6077 return NULL_TREE;
6081 expr = decay_conversion (expr, complain);
6082 if (expr == error_mark_node)
6083 return error_mark_node;
6085 expr = perform_qualification_conversions (type, expr);
6086 if (expr == error_mark_node)
6087 return error_mark_node;
6089 /* [temp.arg.nontype]/5, bullet 3
6091 For a non-type template-parameter of type reference to object, no
6092 conversions apply. The type referred to by the reference may be more
6093 cv-qualified than the (otherwise identical) type of the
6094 template-argument. The template-parameter is bound directly to the
6095 template-argument, which must be an lvalue. */
6096 else if (TYPE_REF_OBJ_P (type))
6098 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6099 expr_type))
6100 return error_mark_node;
6102 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6104 error ("%qE is not a valid template argument for type %qT "
6105 "because of conflicts in cv-qualification", expr, type);
6106 return NULL_TREE;
6109 if (!real_lvalue_p (expr))
6111 error ("%qE is not a valid template argument for type %qT "
6112 "because it is not an lvalue", expr, type);
6113 return NULL_TREE;
6116 /* [temp.arg.nontype]/1
6118 A template-argument for a non-type, non-template template-parameter
6119 shall be one of: [...]
6121 -- the address of an object or function with external linkage. */
6122 if (INDIRECT_REF_P (expr)
6123 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6125 expr = TREE_OPERAND (expr, 0);
6126 if (DECL_P (expr))
6128 error ("%q#D is not a valid template argument for type %qT "
6129 "because a reference variable does not have a constant "
6130 "address", expr, type);
6131 return NULL_TREE;
6135 if (!DECL_P (expr))
6137 error ("%qE is not a valid template argument for type %qT "
6138 "because it is not an object with external linkage",
6139 expr, type);
6140 return NULL_TREE;
6143 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6145 error ("%qE is not a valid template argument for type %qT "
6146 "because object %qD has not external linkage",
6147 expr, type, expr);
6148 return NULL_TREE;
6151 expr = build_nop (type, build_address (expr));
6153 /* [temp.arg.nontype]/5, bullet 4
6155 For a non-type template-parameter of type pointer to function, only
6156 the function-to-pointer conversion (_conv.func_) is applied. If the
6157 template-argument represents a set of overloaded functions (or a
6158 pointer to such), the matching function is selected from the set
6159 (_over.over_). */
6160 else if (TYPE_PTRFN_P (type))
6162 /* If the argument is a template-id, we might not have enough
6163 context information to decay the pointer. */
6164 if (!type_unknown_p (expr_type))
6166 expr = decay_conversion (expr, complain);
6167 if (expr == error_mark_node)
6168 return error_mark_node;
6171 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6172 /* Null pointer values are OK in C++11. */
6173 return perform_qualification_conversions (type, expr);
6175 expr = convert_nontype_argument_function (type, expr);
6176 if (!expr || expr == error_mark_node)
6177 return expr;
6179 /* [temp.arg.nontype]/5, bullet 5
6181 For a non-type template-parameter of type reference to function, no
6182 conversions apply. If the template-argument represents a set of
6183 overloaded functions, the matching function is selected from the set
6184 (_over.over_). */
6185 else if (TYPE_REFFN_P (type))
6187 if (TREE_CODE (expr) == ADDR_EXPR)
6189 error ("%qE is not a valid template argument for type %qT "
6190 "because it is a pointer", expr, type);
6191 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
6192 return NULL_TREE;
6195 expr = convert_nontype_argument_function (type, expr);
6196 if (!expr || expr == error_mark_node)
6197 return expr;
6199 expr = build_nop (type, build_address (expr));
6201 /* [temp.arg.nontype]/5, bullet 6
6203 For a non-type template-parameter of type pointer to member function,
6204 no conversions apply. If the template-argument represents a set of
6205 overloaded member functions, the matching member function is selected
6206 from the set (_over.over_). */
6207 else if (TYPE_PTRMEMFUNC_P (type))
6209 expr = instantiate_type (type, expr, tf_none);
6210 if (expr == error_mark_node)
6211 return error_mark_node;
6213 /* [temp.arg.nontype] bullet 1 says the pointer to member
6214 expression must be a pointer-to-member constant. */
6215 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6216 return error_mark_node;
6218 /* There is no way to disable standard conversions in
6219 resolve_address_of_overloaded_function (called by
6220 instantiate_type). It is possible that the call succeeded by
6221 converting &B::I to &D::I (where B is a base of D), so we need
6222 to reject this conversion here.
6224 Actually, even if there was a way to disable standard conversions,
6225 it would still be better to reject them here so that we can
6226 provide a superior diagnostic. */
6227 if (!same_type_p (TREE_TYPE (expr), type))
6229 error ("%qE is not a valid template argument for type %qT "
6230 "because it is of type %qT", expr, type,
6231 TREE_TYPE (expr));
6232 /* If we are just one standard conversion off, explain. */
6233 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6234 inform (input_location,
6235 "standard conversions are not allowed in this context");
6236 return NULL_TREE;
6239 /* [temp.arg.nontype]/5, bullet 7
6241 For a non-type template-parameter of type pointer to data member,
6242 qualification conversions (_conv.qual_) are applied. */
6243 else if (TYPE_PTRDATAMEM_P (type))
6245 /* [temp.arg.nontype] bullet 1 says the pointer to member
6246 expression must be a pointer-to-member constant. */
6247 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6248 return error_mark_node;
6250 expr = perform_qualification_conversions (type, expr);
6251 if (expr == error_mark_node)
6252 return expr;
6254 else if (NULLPTR_TYPE_P (type))
6256 if (expr != nullptr_node)
6258 error ("%qE is not a valid template argument for type %qT "
6259 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6260 return NULL_TREE;
6262 return expr;
6264 /* A template non-type parameter must be one of the above. */
6265 else
6266 gcc_unreachable ();
6268 /* Sanity check: did we actually convert the argument to the
6269 right type? */
6270 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6271 (type, TREE_TYPE (expr)));
6272 return expr;
6275 /* Subroutine of coerce_template_template_parms, which returns 1 if
6276 PARM_PARM and ARG_PARM match using the rule for the template
6277 parameters of template template parameters. Both PARM and ARG are
6278 template parameters; the rest of the arguments are the same as for
6279 coerce_template_template_parms.
6281 static int
6282 coerce_template_template_parm (tree parm,
6283 tree arg,
6284 tsubst_flags_t complain,
6285 tree in_decl,
6286 tree outer_args)
6288 if (arg == NULL_TREE || arg == error_mark_node
6289 || parm == NULL_TREE || parm == error_mark_node)
6290 return 0;
6292 if (TREE_CODE (arg) != TREE_CODE (parm))
6293 return 0;
6295 switch (TREE_CODE (parm))
6297 case TEMPLATE_DECL:
6298 /* We encounter instantiations of templates like
6299 template <template <template <class> class> class TT>
6300 class C; */
6302 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6303 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6305 if (!coerce_template_template_parms
6306 (parmparm, argparm, complain, in_decl, outer_args))
6307 return 0;
6309 /* Fall through. */
6311 case TYPE_DECL:
6312 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6313 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6314 /* Argument is a parameter pack but parameter is not. */
6315 return 0;
6316 break;
6318 case PARM_DECL:
6319 /* The tsubst call is used to handle cases such as
6321 template <int> class C {};
6322 template <class T, template <T> class TT> class D {};
6323 D<int, C> d;
6325 i.e. the parameter list of TT depends on earlier parameters. */
6326 if (!uses_template_parms (TREE_TYPE (arg))
6327 && !same_type_p
6328 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6329 TREE_TYPE (arg)))
6330 return 0;
6332 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6333 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6334 /* Argument is a parameter pack but parameter is not. */
6335 return 0;
6337 break;
6339 default:
6340 gcc_unreachable ();
6343 return 1;
6347 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6348 template template parameters. Both PARM_PARMS and ARG_PARMS are
6349 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6350 or PARM_DECL.
6352 Consider the example:
6353 template <class T> class A;
6354 template<template <class U> class TT> class B;
6356 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6357 the parameters to A, and OUTER_ARGS contains A. */
6359 static int
6360 coerce_template_template_parms (tree parm_parms,
6361 tree arg_parms,
6362 tsubst_flags_t complain,
6363 tree in_decl,
6364 tree outer_args)
6366 int nparms, nargs, i;
6367 tree parm, arg;
6368 int variadic_p = 0;
6370 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6371 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6373 nparms = TREE_VEC_LENGTH (parm_parms);
6374 nargs = TREE_VEC_LENGTH (arg_parms);
6376 /* Determine whether we have a parameter pack at the end of the
6377 template template parameter's template parameter list. */
6378 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6380 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6382 if (parm == error_mark_node)
6383 return 0;
6385 switch (TREE_CODE (parm))
6387 case TEMPLATE_DECL:
6388 case TYPE_DECL:
6389 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6390 variadic_p = 1;
6391 break;
6393 case PARM_DECL:
6394 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6395 variadic_p = 1;
6396 break;
6398 default:
6399 gcc_unreachable ();
6403 if (nargs != nparms
6404 && !(variadic_p && nargs >= nparms - 1))
6405 return 0;
6407 /* Check all of the template parameters except the parameter pack at
6408 the end (if any). */
6409 for (i = 0; i < nparms - variadic_p; ++i)
6411 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6412 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6413 continue;
6415 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6416 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6418 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6419 outer_args))
6420 return 0;
6424 if (variadic_p)
6426 /* Check each of the template parameters in the template
6427 argument against the template parameter pack at the end of
6428 the template template parameter. */
6429 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6430 return 0;
6432 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6434 for (; i < nargs; ++i)
6436 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6437 continue;
6439 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6441 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6442 outer_args))
6443 return 0;
6447 return 1;
6450 /* Verifies that the deduced template arguments (in TARGS) for the
6451 template template parameters (in TPARMS) represent valid bindings,
6452 by comparing the template parameter list of each template argument
6453 to the template parameter list of its corresponding template
6454 template parameter, in accordance with DR150. This
6455 routine can only be called after all template arguments have been
6456 deduced. It will return TRUE if all of the template template
6457 parameter bindings are okay, FALSE otherwise. */
6458 bool
6459 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6461 int i, ntparms = TREE_VEC_LENGTH (tparms);
6462 bool ret = true;
6464 /* We're dealing with template parms in this process. */
6465 ++processing_template_decl;
6467 targs = INNERMOST_TEMPLATE_ARGS (targs);
6469 for (i = 0; i < ntparms; ++i)
6471 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6472 tree targ = TREE_VEC_ELT (targs, i);
6474 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6476 tree packed_args = NULL_TREE;
6477 int idx, len = 1;
6479 if (ARGUMENT_PACK_P (targ))
6481 /* Look inside the argument pack. */
6482 packed_args = ARGUMENT_PACK_ARGS (targ);
6483 len = TREE_VEC_LENGTH (packed_args);
6486 for (idx = 0; idx < len; ++idx)
6488 tree targ_parms = NULL_TREE;
6490 if (packed_args)
6491 /* Extract the next argument from the argument
6492 pack. */
6493 targ = TREE_VEC_ELT (packed_args, idx);
6495 if (PACK_EXPANSION_P (targ))
6496 /* Look at the pattern of the pack expansion. */
6497 targ = PACK_EXPANSION_PATTERN (targ);
6499 /* Extract the template parameters from the template
6500 argument. */
6501 if (TREE_CODE (targ) == TEMPLATE_DECL)
6502 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6503 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6504 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6506 /* Verify that we can coerce the template template
6507 parameters from the template argument to the template
6508 parameter. This requires an exact match. */
6509 if (targ_parms
6510 && !coerce_template_template_parms
6511 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6512 targ_parms,
6513 tf_none,
6514 tparm,
6515 targs))
6517 ret = false;
6518 goto out;
6524 out:
6526 --processing_template_decl;
6527 return ret;
6530 /* Since type attributes aren't mangled, we need to strip them from
6531 template type arguments. */
6533 static tree
6534 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6536 tree mv;
6537 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6538 return arg;
6539 mv = TYPE_MAIN_VARIANT (arg);
6540 arg = strip_typedefs (arg);
6541 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6542 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6544 if (complain & tf_warning)
6545 warning (0, "ignoring attributes on template argument %qT", arg);
6546 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6547 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6549 return arg;
6552 // A template declaration can be substituted for a constrained
6553 // template template parameter only when the argument is more
6554 // constrained than the parameter.
6555 static bool
6556 is_compatible_template_arg (tree parm, tree arg)
6558 // TODO: The argument may not have a decl result. This seems to
6559 // happen in some classes with nested template template
6560 // parameters (e.g., a rebind struct in a class taking a template
6561 // template parameter. If this is the case, just return true and
6562 // allow things to happen as they always did.
6563 if (TREE_CODE (arg) == TEMPLATE_DECL)
6565 if (!DECL_TEMPLATE_RESULT (arg))
6566 return true;
6569 tree parmcons = DECL_CONSTRAINTS (parm);
6570 tree argcons = DECL_CONSTRAINTS (arg);
6572 // If the template parameter is constrained, we need to rewrite its
6573 // constraints in terms of the ARG's template parameters. This ensures
6574 // that all of the template parameter types will have the same depth.
6576 // Note that this is only valid when coerce_template_template_parm is
6577 // true for the innermost template parameters of PARM and ARG. In other
6578 // words, because coercion is successful, this conversion will be valid.
6579 if (parmcons)
6581 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
6582 ++processing_template_decl;
6583 tree reqs = instantiate_requirements (CI_REQUIREMENTS (parmcons), args);
6584 --processing_template_decl;
6585 if (reqs == error_mark_node)
6586 return false;
6587 parmcons = make_constraints (reqs);
6590 return more_constraints (argcons, parmcons);
6593 // Convert a placeholder argument into a binding to the original
6594 // parameter. The original parameter is saved as the TREE_TYPE of
6595 // ARG.
6596 static inline tree
6597 convert_placeholder_argument (tree parm, tree arg)
6599 TREE_TYPE (arg) = parm;
6600 return arg;
6603 /* Convert the indicated template ARG as necessary to match the
6604 indicated template PARM. Returns the converted ARG, or
6605 error_mark_node if the conversion was unsuccessful. Error and
6606 warning messages are issued under control of COMPLAIN. This
6607 conversion is for the Ith parameter in the parameter list. ARGS is
6608 the full set of template arguments deduced so far. */
6610 static tree
6611 convert_template_argument (tree parm,
6612 tree arg,
6613 tree args,
6614 tsubst_flags_t complain,
6615 int i,
6616 tree in_decl)
6618 tree orig_arg;
6619 tree val;
6620 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6622 // Trivially convert placeholders.
6623 if (TREE_CODE (arg) == PLACEHOLDER_EXPR)
6624 return convert_placeholder_argument (parm, arg);
6626 if (TREE_CODE (arg) == TREE_LIST
6627 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6629 /* The template argument was the name of some
6630 member function. That's usually
6631 invalid, but static members are OK. In any
6632 case, grab the underlying fields/functions
6633 and issue an error later if required. */
6634 orig_arg = TREE_VALUE (arg);
6635 TREE_TYPE (arg) = unknown_type_node;
6638 orig_arg = arg;
6640 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6641 requires_type = (TREE_CODE (parm) == TYPE_DECL
6642 || requires_tmpl_type);
6644 /* When determining whether an argument pack expansion is a template,
6645 look at the pattern. */
6646 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6647 arg = PACK_EXPANSION_PATTERN (arg);
6649 /* Deal with an injected-class-name used as a template template arg. */
6650 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6652 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6653 if (TREE_CODE (t) == TEMPLATE_DECL)
6655 if (cxx_dialect >= cxx11)
6656 /* OK under DR 1004. */;
6657 else if (complain & tf_warning_or_error)
6658 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6659 " used as template template argument", TYPE_NAME (arg));
6660 else if (flag_pedantic_errors)
6661 t = arg;
6663 arg = t;
6667 is_tmpl_type =
6668 ((TREE_CODE (arg) == TEMPLATE_DECL
6669 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6670 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6671 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6672 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6674 if (is_tmpl_type
6675 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6676 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6677 arg = TYPE_STUB_DECL (arg);
6679 is_type = TYPE_P (arg) || is_tmpl_type;
6681 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6682 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6684 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6686 if (complain & tf_error)
6687 error ("invalid use of destructor %qE as a type", orig_arg);
6688 return error_mark_node;
6691 permerror (input_location,
6692 "to refer to a type member of a template parameter, "
6693 "use %<typename %E%>", orig_arg);
6695 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6696 TREE_OPERAND (arg, 1),
6697 typename_type,
6698 complain);
6699 arg = orig_arg;
6700 is_type = 1;
6702 if (is_type != requires_type)
6704 if (in_decl)
6706 if (complain & tf_error)
6708 error ("type/value mismatch at argument %d in template "
6709 "parameter list for %qD",
6710 i + 1, in_decl);
6711 if (is_type)
6712 error (" expected a constant of type %qT, got %qT",
6713 TREE_TYPE (parm),
6714 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6715 else if (requires_tmpl_type)
6716 error (" expected a class template, got %qE", orig_arg);
6717 else
6718 error (" expected a type, got %qE", orig_arg);
6721 return error_mark_node;
6723 if (is_tmpl_type ^ requires_tmpl_type)
6725 if (in_decl && (complain & tf_error))
6727 error ("type/value mismatch at argument %d in template "
6728 "parameter list for %qD",
6729 i + 1, in_decl);
6730 if (is_tmpl_type)
6731 error (" expected a type, got %qT", DECL_NAME (arg));
6732 else
6733 error (" expected a class template, got %qT", orig_arg);
6735 return error_mark_node;
6738 if (is_type)
6740 if (requires_tmpl_type)
6742 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6743 val = orig_arg;
6744 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6745 /* The number of argument required is not known yet.
6746 Just accept it for now. */
6747 val = TREE_TYPE (arg);
6748 else
6750 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6751 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6753 /* Strip alias templates that are equivalent to another
6754 template. */
6755 arg = get_underlying_template (arg);
6756 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6758 if (coerce_template_template_parms (parmparm, argparm,
6759 complain, in_decl,
6760 args))
6762 val = arg;
6764 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6765 TEMPLATE_DECL. */
6766 if (val != error_mark_node)
6768 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6769 val = TREE_TYPE (val);
6770 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6771 val = make_pack_expansion (val);
6774 else
6776 if (in_decl && (complain & tf_error))
6778 error ("type/value mismatch at argument %d in "
6779 "template parameter list for %qD",
6780 i + 1, in_decl);
6781 error (" expected a template of type %qD, got %qT",
6782 parm, orig_arg);
6785 val = error_mark_node;
6788 // Check that the constraints are compatible before allowing the
6789 // substitution.
6790 if (val != error_mark_node)
6791 if (!is_compatible_template_arg (parm, arg))
6793 if (in_decl && (complain & tf_error))
6795 error ("constraint mismatch at argument %d in "
6796 "template parameter list for %qD",
6797 i + 1, in_decl);
6798 inform (input_location, " expected %qD but got %qD",
6799 parm, arg);
6801 val = error_mark_node;
6805 else
6806 val = orig_arg;
6807 /* We only form one instance of each template specialization.
6808 Therefore, if we use a non-canonical variant (i.e., a
6809 typedef), any future messages referring to the type will use
6810 the typedef, which is confusing if those future uses do not
6811 themselves also use the typedef. */
6812 if (TYPE_P (val))
6813 val = canonicalize_type_argument (val, complain);
6815 else
6817 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6819 if (invalid_nontype_parm_type_p (t, complain))
6820 return error_mark_node;
6822 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6824 if (same_type_p (t, TREE_TYPE (orig_arg)))
6825 val = orig_arg;
6826 else
6828 /* Not sure if this is reachable, but it doesn't hurt
6829 to be robust. */
6830 error ("type mismatch in nontype parameter pack");
6831 val = error_mark_node;
6834 else if (!dependent_template_arg_p (orig_arg)
6835 && !uses_template_parms (t))
6836 /* We used to call digest_init here. However, digest_init
6837 will report errors, which we don't want when complain
6838 is zero. More importantly, digest_init will try too
6839 hard to convert things: for example, `0' should not be
6840 converted to pointer type at this point according to
6841 the standard. Accepting this is not merely an
6842 extension, since deciding whether or not these
6843 conversions can occur is part of determining which
6844 function template to call, or whether a given explicit
6845 argument specification is valid. */
6846 val = convert_nontype_argument (t, orig_arg, complain);
6847 else
6848 val = strip_typedefs_expr (orig_arg);
6850 if (val == NULL_TREE)
6851 val = error_mark_node;
6852 else if (val == error_mark_node && (complain & tf_error))
6853 error ("could not convert template argument %qE to %qT", orig_arg, t);
6855 if (TREE_CODE (val) == SCOPE_REF)
6857 /* Strip typedefs from the SCOPE_REF. */
6858 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6859 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6860 complain);
6861 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6862 QUALIFIED_NAME_IS_TEMPLATE (val));
6866 return val;
6869 /* Coerces the remaining template arguments in INNER_ARGS (from
6870 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6871 Returns the coerced argument pack. PARM_IDX is the position of this
6872 parameter in the template parameter list. ARGS is the original
6873 template argument list. */
6874 static tree
6875 coerce_template_parameter_pack (tree parms,
6876 int parm_idx,
6877 tree args,
6878 tree inner_args,
6879 int arg_idx,
6880 tree new_args,
6881 int* lost,
6882 tree in_decl,
6883 tsubst_flags_t complain)
6885 tree parm = TREE_VEC_ELT (parms, parm_idx);
6886 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6887 tree packed_args;
6888 tree argument_pack;
6889 tree packed_parms = NULL_TREE;
6891 if (arg_idx > nargs)
6892 arg_idx = nargs;
6894 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6896 /* When the template parameter is a non-type template parameter pack
6897 or template template parameter pack whose type or template
6898 parameters use parameter packs, we know exactly how many arguments
6899 we are looking for. Build a vector of the instantiated decls for
6900 these template parameters in PACKED_PARMS. */
6901 /* We can't use make_pack_expansion here because it would interpret a
6902 _DECL as a use rather than a declaration. */
6903 tree decl = TREE_VALUE (parm);
6904 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6905 SET_PACK_EXPANSION_PATTERN (exp, decl);
6906 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6907 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6909 TREE_VEC_LENGTH (args)--;
6910 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6911 TREE_VEC_LENGTH (args)++;
6913 if (packed_parms == error_mark_node)
6914 return error_mark_node;
6916 /* If we're doing a partial instantiation of a member template,
6917 verify that all of the types used for the non-type
6918 template parameter pack are, in fact, valid for non-type
6919 template parameters. */
6920 if (arg_idx < nargs
6921 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6923 int j, len = TREE_VEC_LENGTH (packed_parms);
6924 for (j = 0; j < len; ++j)
6926 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6927 if (invalid_nontype_parm_type_p (t, complain))
6928 return error_mark_node;
6932 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6934 else
6935 packed_args = make_tree_vec (nargs - arg_idx);
6937 /* Convert the remaining arguments, which will be a part of the
6938 parameter pack "parm". */
6939 for (; arg_idx < nargs; ++arg_idx)
6941 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6942 tree actual_parm = TREE_VALUE (parm);
6943 int pack_idx = arg_idx - parm_idx;
6945 if (packed_parms)
6947 /* Once we've packed as many args as we have types, stop. */
6948 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6949 break;
6950 else if (PACK_EXPANSION_P (arg))
6951 /* We don't know how many args we have yet, just
6952 use the unconverted ones for now. */
6953 return NULL_TREE;
6954 else
6955 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6958 if (arg == error_mark_node)
6960 if (complain & tf_error)
6961 error ("template argument %d is invalid", arg_idx + 1);
6963 else
6964 arg = convert_template_argument (actual_parm,
6965 arg, new_args, complain, parm_idx,
6966 in_decl);
6967 if (arg == error_mark_node)
6968 (*lost)++;
6969 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6972 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6973 && TREE_VEC_LENGTH (packed_args) > 0)
6975 error ("wrong number of template arguments (%d, should be %d)",
6976 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6977 return error_mark_node;
6980 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6981 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6982 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6983 else
6985 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6986 TREE_TYPE (argument_pack)
6987 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6988 TREE_CONSTANT (argument_pack) = 1;
6991 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6992 #ifdef ENABLE_CHECKING
6993 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6994 TREE_VEC_LENGTH (packed_args));
6995 #endif
6996 return argument_pack;
6999 /* Returns the number of pack expansions in the template argument vector
7000 ARGS. */
7002 static int
7003 pack_expansion_args_count (tree args)
7005 int i;
7006 int count = 0;
7007 if (args)
7008 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7010 tree elt = TREE_VEC_ELT (args, i);
7011 if (elt && PACK_EXPANSION_P (elt))
7012 ++count;
7014 return count;
7017 /* Convert all template arguments to their appropriate types, and
7018 return a vector containing the innermost resulting template
7019 arguments. If any error occurs, return error_mark_node. Error and
7020 warning messages are issued under control of COMPLAIN.
7022 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7023 for arguments not specified in ARGS. Otherwise, if
7024 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7025 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7026 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7027 ARGS. */
7029 static tree
7030 coerce_template_parms (tree parms,
7031 tree args,
7032 tree in_decl,
7033 tsubst_flags_t complain,
7034 bool require_all_args,
7035 bool use_default_args)
7037 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7038 tree orig_inner_args;
7039 tree inner_args;
7040 tree new_args;
7041 tree new_inner_args;
7042 int saved_unevaluated_operand;
7043 int saved_inhibit_evaluation_warnings;
7045 /* When used as a boolean value, indicates whether this is a
7046 variadic template parameter list. Since it's an int, we can also
7047 subtract it from nparms to get the number of non-variadic
7048 parameters. */
7049 int variadic_p = 0;
7050 int variadic_args_p = 0;
7051 int post_variadic_parms = 0;
7053 if (args == error_mark_node)
7054 return error_mark_node;
7056 nparms = TREE_VEC_LENGTH (parms);
7058 /* Determine if there are any parameter packs. */
7059 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7061 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
7062 if (variadic_p)
7063 ++post_variadic_parms;
7064 if (template_parameter_pack_p (tparm))
7065 ++variadic_p;
7068 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7069 /* If there are no parameters that follow a parameter pack, we need to
7070 expand any argument packs so that we can deduce a parameter pack from
7071 some non-packed args followed by an argument pack, as in variadic85.C.
7072 If there are such parameters, we need to leave argument packs intact
7073 so the arguments are assigned properly. This can happen when dealing
7074 with a nested class inside a partial specialization of a class
7075 template, as in variadic92.C, or when deducing a template parameter pack
7076 from a sub-declarator, as in variadic114.C. */
7077 if (!post_variadic_parms)
7078 inner_args = expand_template_argument_pack (inner_args);
7080 /* Count any pack expansion args. */
7081 variadic_args_p = pack_expansion_args_count (inner_args);
7083 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7084 if ((nargs > nparms && !variadic_p)
7085 || (nargs < nparms - variadic_p
7086 && require_all_args
7087 && !variadic_args_p
7088 && (!use_default_args
7089 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7090 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7092 if (complain & tf_error)
7094 if (variadic_p)
7096 nparms -= variadic_p;
7097 error ("wrong number of template arguments "
7098 "(%d, should be %d or more)", nargs, nparms);
7100 else
7101 error ("wrong number of template arguments "
7102 "(%d, should be %d)", nargs, nparms);
7104 if (in_decl)
7105 error ("provided for %q+D", in_decl);
7108 return error_mark_node;
7110 /* We can't pass a pack expansion to a non-pack parameter of an alias
7111 template (DR 1430). */
7112 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7113 && variadic_args_p
7114 && nargs - variadic_args_p < nparms - variadic_p)
7116 if (complain & tf_error)
7118 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7120 tree arg = TREE_VEC_ELT (inner_args, i);
7121 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7123 if (PACK_EXPANSION_P (arg)
7124 && !template_parameter_pack_p (parm))
7126 error ("pack expansion argument for non-pack parameter "
7127 "%qD of alias template %qD", parm, in_decl);
7128 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7129 goto found;
7132 gcc_unreachable ();
7133 found:;
7135 return error_mark_node;
7138 /* We need to evaluate the template arguments, even though this
7139 template-id may be nested within a "sizeof". */
7140 saved_unevaluated_operand = cp_unevaluated_operand;
7141 cp_unevaluated_operand = 0;
7142 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7143 c_inhibit_evaluation_warnings = 0;
7144 new_inner_args = make_tree_vec (nparms);
7145 new_args = add_outermost_template_args (args, new_inner_args);
7146 int pack_adjust = 0;
7147 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7149 tree arg;
7150 tree parm;
7152 /* Get the Ith template parameter. */
7153 parm = TREE_VEC_ELT (parms, parm_idx);
7155 if (parm == error_mark_node)
7157 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7158 continue;
7161 /* Calculate the next argument. */
7162 if (arg_idx < nargs)
7163 arg = TREE_VEC_ELT (inner_args, arg_idx);
7164 else
7165 arg = NULL_TREE;
7167 if (template_parameter_pack_p (TREE_VALUE (parm))
7168 && !(arg && ARGUMENT_PACK_P (arg)))
7170 /* Some arguments will be placed in the
7171 template parameter pack PARM. */
7172 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7173 inner_args, arg_idx,
7174 new_args, &lost,
7175 in_decl, complain);
7177 if (arg == NULL_TREE)
7179 /* We don't know how many args we have yet, just use the
7180 unconverted (and still packed) ones for now. */
7181 new_inner_args = orig_inner_args;
7182 arg_idx = nargs;
7183 break;
7186 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7188 /* Store this argument. */
7189 if (arg == error_mark_node)
7191 lost++;
7192 /* We are done with all of the arguments. */
7193 arg_idx = nargs;
7195 else
7197 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7198 arg_idx += pack_adjust;
7201 continue;
7203 else if (arg)
7205 if (PACK_EXPANSION_P (arg))
7207 /* We don't know how many args we have yet, just
7208 use the unconverted ones for now. */
7209 new_inner_args = inner_args;
7210 arg_idx = nargs;
7211 break;
7214 else if (require_all_args)
7216 /* There must be a default arg in this case. */
7217 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7218 complain, in_decl);
7219 /* The position of the first default template argument,
7220 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7221 Record that. */
7222 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7223 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7224 arg_idx - pack_adjust);
7226 else
7227 break;
7229 if (arg == error_mark_node)
7231 if (complain & tf_error)
7232 error ("template argument %d is invalid", arg_idx + 1);
7234 else if (!arg)
7235 /* This only occurs if there was an error in the template
7236 parameter list itself (which we would already have
7237 reported) that we are trying to recover from, e.g., a class
7238 template with a parameter list such as
7239 template<typename..., typename>. */
7240 ++lost;
7241 else
7242 arg = convert_template_argument (TREE_VALUE (parm),
7243 arg, new_args, complain,
7244 parm_idx, in_decl);
7246 if (arg == error_mark_node)
7247 lost++;
7248 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7250 cp_unevaluated_operand = saved_unevaluated_operand;
7251 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7253 if (variadic_p && arg_idx < nargs)
7255 if (complain & tf_error)
7257 error ("wrong number of template arguments "
7258 "(%d, should be %d)", nargs, arg_idx);
7259 if (in_decl)
7260 error ("provided for %q+D", in_decl);
7262 return error_mark_node;
7265 if (lost)
7266 return error_mark_node;
7268 #ifdef ENABLE_CHECKING
7269 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7270 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7271 TREE_VEC_LENGTH (new_inner_args));
7272 #endif
7274 return new_inner_args;
7277 // Convert all template arguments to their appropriate types, and
7278 // return a vector containing the innermost resulting template
7279 // arguments. If any error occurs, return error_mark_node. Error and
7280 // warning messages are not issued.
7282 // Note that no function argument deduction is performed, and default
7283 // arguments are used to fill in unspecified arguments.
7284 tree
7285 coerce_template_parms (tree parms, tree args, tree in_decl)
7287 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7290 /* Like coerce_template_parms. If PARMS represents all template
7291 parameters levels, this function returns a vector of vectors
7292 representing all the resulting argument levels. Note that in this
7293 case, only the innermost arguments are coerced because the
7294 outermost ones are supposed to have been coerced already.
7296 Otherwise, if PARMS represents only (the innermost) vector of
7297 parameters, this function returns a vector containing just the
7298 innermost resulting arguments. */
7300 static tree
7301 coerce_innermost_template_parms (tree parms,
7302 tree args,
7303 tree in_decl,
7304 tsubst_flags_t complain,
7305 bool require_all_args,
7306 bool use_default_args)
7308 int parms_depth = TMPL_PARMS_DEPTH (parms);
7309 int args_depth = TMPL_ARGS_DEPTH (args);
7310 tree coerced_args;
7312 if (parms_depth > 1)
7314 coerced_args = make_tree_vec (parms_depth);
7315 tree level;
7316 int cur_depth;
7318 for (level = parms, cur_depth = parms_depth;
7319 parms_depth > 0 && level != NULL_TREE;
7320 level = TREE_CHAIN (level), --cur_depth)
7322 tree l;
7323 if (cur_depth == args_depth)
7324 l = coerce_template_parms (TREE_VALUE (level),
7325 args, in_decl, complain,
7326 require_all_args,
7327 use_default_args);
7328 else
7329 l = TMPL_ARGS_LEVEL (args, cur_depth);
7331 if (l == error_mark_node)
7332 return error_mark_node;
7334 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7337 else
7338 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7339 args, in_decl, complain,
7340 require_all_args,
7341 use_default_args);
7342 return coerced_args;
7345 /* Returns 1 if template args OT and NT are equivalent. */
7347 static int
7348 template_args_equal (tree ot, tree nt)
7350 if (nt == ot)
7351 return 1;
7352 if (nt == NULL_TREE || ot == NULL_TREE)
7353 return false;
7355 if (TREE_CODE (nt) == TREE_VEC)
7356 /* For member templates */
7357 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7358 else if (PACK_EXPANSION_P (ot))
7359 return (PACK_EXPANSION_P (nt)
7360 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7361 PACK_EXPANSION_PATTERN (nt))
7362 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7363 PACK_EXPANSION_EXTRA_ARGS (nt)));
7364 else if (ARGUMENT_PACK_P (ot))
7366 int i, len;
7367 tree opack, npack;
7369 if (!ARGUMENT_PACK_P (nt))
7370 return 0;
7372 opack = ARGUMENT_PACK_ARGS (ot);
7373 npack = ARGUMENT_PACK_ARGS (nt);
7374 len = TREE_VEC_LENGTH (opack);
7375 if (TREE_VEC_LENGTH (npack) != len)
7376 return 0;
7377 for (i = 0; i < len; ++i)
7378 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7379 TREE_VEC_ELT (npack, i)))
7380 return 0;
7381 return 1;
7383 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7385 /* We get here probably because we are in the middle of substituting
7386 into the pattern of a pack expansion. In that case the
7387 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7388 interested in. So we want to use the initial pack argument for
7389 the comparison. */
7390 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7391 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7392 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7393 return template_args_equal (ot, nt);
7395 else if (TYPE_P (nt))
7396 return TYPE_P (ot) && same_type_p (ot, nt);
7397 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7398 return 0;
7399 else
7400 return cp_tree_equal (ot, nt);
7403 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7404 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7405 NEWARG_PTR with the offending arguments if they are non-NULL. */
7407 static int
7408 comp_template_args_with_info (tree oldargs, tree newargs,
7409 tree *oldarg_ptr, tree *newarg_ptr)
7411 int i;
7413 if (oldargs == newargs)
7414 return 1;
7416 if (!oldargs || !newargs)
7417 return 0;
7419 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7420 return 0;
7422 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7424 tree nt = TREE_VEC_ELT (newargs, i);
7425 tree ot = TREE_VEC_ELT (oldargs, i);
7427 if (! template_args_equal (ot, nt))
7429 if (oldarg_ptr != NULL)
7430 *oldarg_ptr = ot;
7431 if (newarg_ptr != NULL)
7432 *newarg_ptr = nt;
7433 return 0;
7436 return 1;
7439 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7440 of template arguments. Returns 0 otherwise. */
7443 comp_template_args (tree oldargs, tree newargs)
7445 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7448 static void
7449 add_pending_template (tree d)
7451 tree ti = (TYPE_P (d)
7452 ? CLASSTYPE_TEMPLATE_INFO (d)
7453 : DECL_TEMPLATE_INFO (d));
7454 struct pending_template *pt;
7455 int level;
7457 if (TI_PENDING_TEMPLATE_FLAG (ti))
7458 return;
7460 /* We are called both from instantiate_decl, where we've already had a
7461 tinst_level pushed, and instantiate_template, where we haven't.
7462 Compensate. */
7463 level = !current_tinst_level || current_tinst_level->decl != d;
7465 if (level)
7466 push_tinst_level (d);
7468 pt = ggc_alloc_pending_template ();
7469 pt->next = NULL;
7470 pt->tinst = current_tinst_level;
7471 if (last_pending_template)
7472 last_pending_template->next = pt;
7473 else
7474 pending_templates = pt;
7476 last_pending_template = pt;
7478 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7480 if (level)
7481 pop_tinst_level ();
7485 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7486 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7487 documentation for TEMPLATE_ID_EXPR. */
7489 tree
7490 lookup_template_function (tree fns, tree arglist)
7492 tree type;
7494 if (fns == error_mark_node || arglist == error_mark_node)
7495 return error_mark_node;
7497 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7499 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7501 error ("%q#D is not a function template", fns);
7502 return error_mark_node;
7505 if (BASELINK_P (fns))
7507 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7508 unknown_type_node,
7509 BASELINK_FUNCTIONS (fns),
7510 arglist);
7511 return fns;
7514 type = TREE_TYPE (fns);
7515 if (TREE_CODE (fns) == OVERLOAD || !type)
7516 type = unknown_type_node;
7518 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7521 /* Within the scope of a template class S<T>, the name S gets bound
7522 (in build_self_reference) to a TYPE_DECL for the class, not a
7523 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7524 or one of its enclosing classes, and that type is a template,
7525 return the associated TEMPLATE_DECL. Otherwise, the original
7526 DECL is returned.
7528 Also handle the case when DECL is a TREE_LIST of ambiguous
7529 injected-class-names from different bases. */
7531 tree
7532 maybe_get_template_decl_from_type_decl (tree decl)
7534 if (decl == NULL_TREE)
7535 return decl;
7537 /* DR 176: A lookup that finds an injected-class-name (10.2
7538 [class.member.lookup]) can result in an ambiguity in certain cases
7539 (for example, if it is found in more than one base class). If all of
7540 the injected-class-names that are found refer to specializations of
7541 the same class template, and if the name is followed by a
7542 template-argument-list, the reference refers to the class template
7543 itself and not a specialization thereof, and is not ambiguous. */
7544 if (TREE_CODE (decl) == TREE_LIST)
7546 tree t, tmpl = NULL_TREE;
7547 for (t = decl; t; t = TREE_CHAIN (t))
7549 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7550 if (!tmpl)
7551 tmpl = elt;
7552 else if (tmpl != elt)
7553 break;
7555 if (tmpl && t == NULL_TREE)
7556 return tmpl;
7557 else
7558 return decl;
7561 return (decl != NULL_TREE
7562 && DECL_SELF_REFERENCE_P (decl)
7563 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7564 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7567 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7568 parameters, find the desired type.
7570 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7572 IN_DECL, if non-NULL, is the template declaration we are trying to
7573 instantiate.
7575 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7576 the class we are looking up.
7578 Issue error and warning messages under control of COMPLAIN.
7580 If the template class is really a local class in a template
7581 function, then the FUNCTION_CONTEXT is the function in which it is
7582 being instantiated.
7584 ??? Note that this function is currently called *twice* for each
7585 template-id: the first time from the parser, while creating the
7586 incomplete type (finish_template_type), and the second type during the
7587 real instantiation (instantiate_template_class). This is surely something
7588 that we want to avoid. It also causes some problems with argument
7589 coercion (see convert_nontype_argument for more information on this). */
7591 static tree
7592 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7593 int entering_scope, tsubst_flags_t complain)
7595 tree templ = NULL_TREE, parmlist;
7596 tree t;
7597 void **slot;
7598 spec_entry *entry;
7599 spec_entry elt;
7600 hashval_t hash;
7602 if (identifier_p (d1))
7604 tree value = innermost_non_namespace_value (d1);
7605 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7606 templ = value;
7607 else
7609 if (context)
7610 push_decl_namespace (context);
7611 templ = lookup_name (d1);
7612 templ = maybe_get_template_decl_from_type_decl (templ);
7613 if (context)
7614 pop_decl_namespace ();
7616 if (templ)
7617 context = DECL_CONTEXT (templ);
7619 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7621 tree type = TREE_TYPE (d1);
7623 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7624 an implicit typename for the second A. Deal with it. */
7625 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7626 type = TREE_TYPE (type);
7628 if (CLASSTYPE_TEMPLATE_INFO (type))
7630 templ = CLASSTYPE_TI_TEMPLATE (type);
7631 d1 = DECL_NAME (templ);
7634 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7635 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7637 templ = TYPE_TI_TEMPLATE (d1);
7638 d1 = DECL_NAME (templ);
7640 else if (TREE_CODE (d1) == TEMPLATE_DECL
7641 && DECL_TEMPLATE_RESULT (d1)
7642 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7644 templ = d1;
7645 d1 = DECL_NAME (templ);
7646 context = DECL_CONTEXT (templ);
7648 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7650 templ = d1;
7651 d1 = DECL_NAME (templ);
7654 /* Issue an error message if we didn't find a template. */
7655 if (! templ)
7657 if (complain & tf_error)
7658 error ("%qT is not a template", d1);
7659 return error_mark_node;
7662 if (TREE_CODE (templ) != TEMPLATE_DECL
7663 /* Make sure it's a user visible template, if it was named by
7664 the user. */
7665 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7666 && !PRIMARY_TEMPLATE_P (templ)))
7668 if (complain & tf_error)
7670 error ("non-template type %qT used as a template", d1);
7671 if (in_decl)
7672 error ("for template declaration %q+D", in_decl);
7674 return error_mark_node;
7677 complain &= ~tf_user;
7679 /* An alias that just changes the name of a template is equivalent to the
7680 other template, so if any of the arguments are pack expansions, strip
7681 the alias to avoid problems with a pack expansion passed to a non-pack
7682 alias template parameter (DR 1430). */
7683 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7684 templ = get_underlying_template (templ);
7686 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7688 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7689 template arguments */
7691 tree parm;
7692 tree arglist2;
7693 tree outer;
7695 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7697 /* Consider an example where a template template parameter declared as
7699 template <class T, class U = std::allocator<T> > class TT
7701 The template parameter level of T and U are one level larger than
7702 of TT. To proper process the default argument of U, say when an
7703 instantiation `TT<int>' is seen, we need to build the full
7704 arguments containing {int} as the innermost level. Outer levels,
7705 available when not appearing as default template argument, can be
7706 obtained from the arguments of the enclosing template.
7708 Suppose that TT is later substituted with std::vector. The above
7709 instantiation is `TT<int, std::allocator<T> >' with TT at
7710 level 1, and T at level 2, while the template arguments at level 1
7711 becomes {std::vector} and the inner level 2 is {int}. */
7713 outer = DECL_CONTEXT (templ);
7714 if (outer)
7715 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7716 else if (current_template_parms)
7717 /* This is an argument of the current template, so we haven't set
7718 DECL_CONTEXT yet. */
7719 outer = current_template_args ();
7721 if (outer)
7722 arglist = add_to_template_args (outer, arglist);
7724 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7725 complain,
7726 /*require_all_args=*/true,
7727 /*use_default_args=*/true);
7728 if (arglist2 == error_mark_node
7729 || (!uses_template_parms (arglist2)
7730 && check_instantiated_args (templ, arglist2, complain)))
7731 return error_mark_node;
7733 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7734 return parm;
7736 else
7738 tree template_type = TREE_TYPE (templ);
7739 tree gen_tmpl;
7740 tree type_decl;
7741 tree found = NULL_TREE;
7742 int arg_depth;
7743 int parm_depth;
7744 int is_dependent_type;
7745 int use_partial_inst_tmpl = false;
7747 if (template_type == error_mark_node)
7748 /* An error occurred while building the template TEMPL, and a
7749 diagnostic has most certainly been emitted for that
7750 already. Let's propagate that error. */
7751 return error_mark_node;
7753 gen_tmpl = most_general_template (templ);
7754 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7755 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7756 arg_depth = TMPL_ARGS_DEPTH (arglist);
7758 if (arg_depth == 1 && parm_depth > 1)
7760 /* We've been given an incomplete set of template arguments.
7761 For example, given:
7763 template <class T> struct S1 {
7764 template <class U> struct S2 {};
7765 template <class U> struct S2<U*> {};
7768 we will be called with an ARGLIST of `U*', but the
7769 TEMPLATE will be `template <class T> template
7770 <class U> struct S1<T>::S2'. We must fill in the missing
7771 arguments. */
7772 arglist
7773 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7774 arglist);
7775 arg_depth = TMPL_ARGS_DEPTH (arglist);
7778 /* Now we should have enough arguments. */
7779 gcc_assert (parm_depth == arg_depth);
7781 /* From here on, we're only interested in the most general
7782 template. */
7784 /* Calculate the BOUND_ARGS. These will be the args that are
7785 actually tsubst'd into the definition to create the
7786 instantiation. */
7787 if (parm_depth > 1)
7789 /* We have multiple levels of arguments to coerce, at once. */
7790 int i;
7791 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7793 tree bound_args = make_tree_vec (parm_depth);
7795 for (i = saved_depth,
7796 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7797 i > 0 && t != NULL_TREE;
7798 --i, t = TREE_CHAIN (t))
7800 tree a;
7801 if (i == saved_depth)
7802 a = coerce_template_parms (TREE_VALUE (t),
7803 arglist, gen_tmpl,
7804 complain,
7805 /*require_all_args=*/true,
7806 /*use_default_args=*/true);
7807 else
7808 /* Outer levels should have already been coerced. */
7809 a = TMPL_ARGS_LEVEL (arglist, i);
7811 /* Don't process further if one of the levels fails. */
7812 if (a == error_mark_node)
7814 /* Restore the ARGLIST to its full size. */
7815 TREE_VEC_LENGTH (arglist) = saved_depth;
7816 return error_mark_node;
7819 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7821 /* We temporarily reduce the length of the ARGLIST so
7822 that coerce_template_parms will see only the arguments
7823 corresponding to the template parameters it is
7824 examining. */
7825 TREE_VEC_LENGTH (arglist)--;
7828 /* Restore the ARGLIST to its full size. */
7829 TREE_VEC_LENGTH (arglist) = saved_depth;
7831 arglist = bound_args;
7833 else
7834 arglist
7835 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7836 INNERMOST_TEMPLATE_ARGS (arglist),
7837 gen_tmpl,
7838 complain,
7839 /*require_all_args=*/true,
7840 /*use_default_args=*/true);
7842 if (arglist == error_mark_node)
7843 /* We were unable to bind the arguments. */
7844 return error_mark_node;
7846 /* In the scope of a template class, explicit references to the
7847 template class refer to the type of the template, not any
7848 instantiation of it. For example, in:
7850 template <class T> class C { void f(C<T>); }
7852 the `C<T>' is just the same as `C'. Outside of the
7853 class, however, such a reference is an instantiation. */
7854 if ((entering_scope
7855 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7856 || currently_open_class (template_type))
7857 /* comp_template_args is expensive, check it last. */
7858 && comp_template_args (TYPE_TI_ARGS (template_type),
7859 arglist))
7860 return template_type;
7862 /* If we already have this specialization, return it. */
7863 elt.tmpl = gen_tmpl;
7864 elt.args = arglist;
7865 hash = hash_specialization (&elt);
7866 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7867 &elt, hash);
7869 if (entry)
7870 return entry->spec;
7872 // If the the template's constraints are not satisfied, then we
7873 // cannot form a valid type.
7875 // Note that the check is deferred until after the hash-lookup.
7876 // This prevents redundant checks on specializations.
7877 if (!check_template_constraints (gen_tmpl, arglist))
7879 // Diagnose constraints here since they are not diagnosed
7880 // anywhere else.
7881 if (complain & tf_error)
7883 error ("template argument deduction failure");
7884 diagnose_constraints (input_location, gen_tmpl, arglist);
7886 return error_mark_node;
7889 is_dependent_type = uses_template_parms (arglist);
7891 /* If the deduced arguments are invalid, then the binding
7892 failed. */
7893 if (!is_dependent_type
7894 && check_instantiated_args (gen_tmpl,
7895 INNERMOST_TEMPLATE_ARGS (arglist),
7896 complain))
7897 return error_mark_node;
7899 if (!is_dependent_type
7900 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7901 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7902 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7904 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7905 DECL_NAME (gen_tmpl),
7906 /*tag_scope=*/ts_global);
7907 return found;
7910 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7911 complain, in_decl);
7912 if (context == error_mark_node)
7913 return error_mark_node;
7915 if (!context)
7916 context = global_namespace;
7918 /* Create the type. */
7919 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7921 /* The user referred to a specialization of an alias
7922 template represented by GEN_TMPL.
7924 [temp.alias]/2 says:
7926 When a template-id refers to the specialization of an
7927 alias template, it is equivalent to the associated
7928 type obtained by substitution of its
7929 template-arguments for the template-parameters in the
7930 type-id of the alias template. */
7932 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7933 /* Note that the call above (by indirectly calling
7934 register_specialization in tsubst_decl) registers the
7935 TYPE_DECL representing the specialization of the alias
7936 template. So next time someone substitutes ARGLIST for
7937 the template parms into the alias template (GEN_TMPL),
7938 she'll get that TYPE_DECL back. */
7940 if (t == error_mark_node)
7941 return t;
7943 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7945 if (!is_dependent_type)
7947 set_current_access_from_decl (TYPE_NAME (template_type));
7948 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7949 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7950 arglist, complain, in_decl),
7951 SCOPED_ENUM_P (template_type), NULL);
7953 if (t == error_mark_node)
7954 return t;
7956 else
7958 /* We don't want to call start_enum for this type, since
7959 the values for the enumeration constants may involve
7960 template parameters. And, no one should be interested
7961 in the enumeration constants for such a type. */
7962 t = cxx_make_type (ENUMERAL_TYPE);
7963 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7965 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7966 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7967 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7969 else if (CLASS_TYPE_P (template_type))
7971 t = make_class_type (TREE_CODE (template_type));
7972 CLASSTYPE_DECLARED_CLASS (t)
7973 = CLASSTYPE_DECLARED_CLASS (template_type);
7974 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7975 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7977 /* A local class. Make sure the decl gets registered properly. */
7978 if (context == current_function_decl)
7979 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7981 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7982 /* This instantiation is another name for the primary
7983 template type. Set the TYPE_CANONICAL field
7984 appropriately. */
7985 TYPE_CANONICAL (t) = template_type;
7986 else if (any_template_arguments_need_structural_equality_p (arglist))
7987 /* Some of the template arguments require structural
7988 equality testing, so this template class requires
7989 structural equality testing. */
7990 SET_TYPE_STRUCTURAL_EQUALITY (t);
7992 else
7993 gcc_unreachable ();
7995 /* If we called start_enum or pushtag above, this information
7996 will already be set up. */
7997 if (!TYPE_NAME (t))
7999 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8001 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8002 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8003 DECL_SOURCE_LOCATION (type_decl)
8004 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8006 else
8007 type_decl = TYPE_NAME (t);
8009 if (CLASS_TYPE_P (template_type))
8011 TREE_PRIVATE (type_decl)
8012 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8013 TREE_PROTECTED (type_decl)
8014 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8015 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8017 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8018 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8022 /* Let's consider the explicit specialization of a member
8023 of a class template specialization that is implicitly instantiated,
8024 e.g.:
8025 template<class T>
8026 struct S
8028 template<class U> struct M {}; //#0
8031 template<>
8032 template<>
8033 struct S<int>::M<char> //#1
8035 int i;
8037 [temp.expl.spec]/4 says this is valid.
8039 In this case, when we write:
8040 S<int>::M<char> m;
8042 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8043 the one of #0.
8045 When we encounter #1, we want to store the partial instantiation
8046 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8048 For all cases other than this "explicit specialization of member of a
8049 class template", we just want to store the most general template into
8050 the CLASSTYPE_TI_TEMPLATE of M.
8052 This case of "explicit specialization of member of a class template"
8053 only happens when:
8054 1/ the enclosing class is an instantiation of, and therefore not
8055 the same as, the context of the most general template, and
8056 2/ we aren't looking at the partial instantiation itself, i.e.
8057 the innermost arguments are not the same as the innermost parms of
8058 the most general template.
8060 So it's only when 1/ and 2/ happens that we want to use the partial
8061 instantiation of the member template in lieu of its most general
8062 template. */
8064 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8065 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8066 /* the enclosing class must be an instantiation... */
8067 && CLASS_TYPE_P (context)
8068 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8070 tree partial_inst_args;
8071 TREE_VEC_LENGTH (arglist)--;
8072 ++processing_template_decl;
8073 partial_inst_args =
8074 tsubst (INNERMOST_TEMPLATE_ARGS
8075 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8076 arglist, complain, NULL_TREE);
8077 --processing_template_decl;
8078 TREE_VEC_LENGTH (arglist)++;
8079 use_partial_inst_tmpl =
8080 /*...and we must not be looking at the partial instantiation
8081 itself. */
8082 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8083 partial_inst_args);
8086 if (!use_partial_inst_tmpl)
8087 /* This case is easy; there are no member templates involved. */
8088 found = gen_tmpl;
8089 else
8091 /* This is a full instantiation of a member template. Find
8092 the partial instantiation of which this is an instance. */
8094 /* Temporarily reduce by one the number of levels in the ARGLIST
8095 so as to avoid comparing the last set of arguments. */
8096 TREE_VEC_LENGTH (arglist)--;
8097 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8098 TREE_VEC_LENGTH (arglist)++;
8099 /* FOUND is either a proper class type, or an alias
8100 template specialization. In the later case, it's a
8101 TYPE_DECL, resulting from the substituting of arguments
8102 for parameters in the TYPE_DECL of the alias template
8103 done earlier. So be careful while getting the template
8104 of FOUND. */
8105 found = TREE_CODE (found) == TYPE_DECL
8106 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8107 : CLASSTYPE_TI_TEMPLATE (found);
8110 // Build template info for the new specialization.
8111 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8113 elt.spec = t;
8114 slot = htab_find_slot_with_hash (type_specializations,
8115 &elt, hash, INSERT);
8116 entry = ggc_alloc_spec_entry ();
8117 *entry = elt;
8118 *slot = entry;
8120 /* Note this use of the partial instantiation so we can check it
8121 later in maybe_process_partial_specialization. */
8122 DECL_TEMPLATE_INSTANTIATIONS (found)
8123 = tree_cons (arglist, t,
8124 DECL_TEMPLATE_INSTANTIATIONS (found));
8126 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8127 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8128 /* Now that the type has been registered on the instantiations
8129 list, we set up the enumerators. Because the enumeration
8130 constants may involve the enumeration type itself, we make
8131 sure to register the type first, and then create the
8132 constants. That way, doing tsubst_expr for the enumeration
8133 constants won't result in recursive calls here; we'll find
8134 the instantiation and exit above. */
8135 tsubst_enum (template_type, t, arglist);
8137 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8138 /* If the type makes use of template parameters, the
8139 code that generates debugging information will crash. */
8140 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8142 /* Possibly limit visibility based on template args. */
8143 TREE_PUBLIC (type_decl) = 1;
8144 determine_visibility (type_decl);
8146 return t;
8150 /* Wrapper for lookup_template_class_1. */
8152 tree
8153 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8154 int entering_scope, tsubst_flags_t complain)
8156 tree ret;
8157 timevar_push (TV_TEMPLATE_INST);
8158 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8159 entering_scope, complain);
8160 timevar_pop (TV_TEMPLATE_INST);
8161 return ret;
8164 struct pair_fn_data
8166 tree_fn_t fn;
8167 void *data;
8168 /* True when we should also visit template parameters that occur in
8169 non-deduced contexts. */
8170 bool include_nondeduced_p;
8171 struct pointer_set_t *visited;
8174 /* Called from for_each_template_parm via walk_tree. */
8176 static tree
8177 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8179 tree t = *tp;
8180 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8181 tree_fn_t fn = pfd->fn;
8182 void *data = pfd->data;
8184 if (TYPE_P (t)
8185 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8186 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8187 pfd->include_nondeduced_p))
8188 return error_mark_node;
8190 switch (TREE_CODE (t))
8192 case RECORD_TYPE:
8193 if (TYPE_PTRMEMFUNC_P (t))
8194 break;
8195 /* Fall through. */
8197 case UNION_TYPE:
8198 case ENUMERAL_TYPE:
8199 if (!TYPE_TEMPLATE_INFO (t))
8200 *walk_subtrees = 0;
8201 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8202 fn, data, pfd->visited,
8203 pfd->include_nondeduced_p))
8204 return error_mark_node;
8205 break;
8207 case INTEGER_TYPE:
8208 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8209 fn, data, pfd->visited,
8210 pfd->include_nondeduced_p)
8211 || for_each_template_parm (TYPE_MAX_VALUE (t),
8212 fn, data, pfd->visited,
8213 pfd->include_nondeduced_p))
8214 return error_mark_node;
8215 break;
8217 case METHOD_TYPE:
8218 /* Since we're not going to walk subtrees, we have to do this
8219 explicitly here. */
8220 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8221 pfd->visited, pfd->include_nondeduced_p))
8222 return error_mark_node;
8223 /* Fall through. */
8225 case FUNCTION_TYPE:
8226 /* Check the return type. */
8227 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8228 pfd->include_nondeduced_p))
8229 return error_mark_node;
8231 /* Check the parameter types. Since default arguments are not
8232 instantiated until they are needed, the TYPE_ARG_TYPES may
8233 contain expressions that involve template parameters. But,
8234 no-one should be looking at them yet. And, once they're
8235 instantiated, they don't contain template parameters, so
8236 there's no point in looking at them then, either. */
8238 tree parm;
8240 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8241 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8242 pfd->visited, pfd->include_nondeduced_p))
8243 return error_mark_node;
8245 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8246 want walk_tree walking into them itself. */
8247 *walk_subtrees = 0;
8249 break;
8251 case TYPEOF_TYPE:
8252 case UNDERLYING_TYPE:
8253 if (pfd->include_nondeduced_p
8254 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
8255 pfd->visited,
8256 pfd->include_nondeduced_p))
8257 return error_mark_node;
8258 break;
8260 case FUNCTION_DECL:
8261 case VAR_DECL:
8262 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8263 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8264 pfd->visited, pfd->include_nondeduced_p))
8265 return error_mark_node;
8266 /* Fall through. */
8268 case PARM_DECL:
8269 case CONST_DECL:
8270 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8271 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8272 pfd->visited, pfd->include_nondeduced_p))
8273 return error_mark_node;
8274 if (DECL_CONTEXT (t)
8275 && pfd->include_nondeduced_p
8276 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8277 pfd->visited, pfd->include_nondeduced_p))
8278 return error_mark_node;
8279 break;
8281 case BOUND_TEMPLATE_TEMPLATE_PARM:
8282 /* Record template parameters such as `T' inside `TT<T>'. */
8283 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8284 pfd->include_nondeduced_p))
8285 return error_mark_node;
8286 /* Fall through. */
8288 case TEMPLATE_TEMPLATE_PARM:
8289 case TEMPLATE_TYPE_PARM:
8290 case TEMPLATE_PARM_INDEX:
8291 if (fn && (*fn)(t, data))
8292 return error_mark_node;
8293 else if (!fn)
8294 return error_mark_node;
8295 break;
8297 case TEMPLATE_DECL:
8298 /* A template template parameter is encountered. */
8299 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8300 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8301 pfd->include_nondeduced_p))
8302 return error_mark_node;
8304 /* Already substituted template template parameter */
8305 *walk_subtrees = 0;
8306 break;
8308 case TYPENAME_TYPE:
8309 if (!fn
8310 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8311 data, pfd->visited,
8312 pfd->include_nondeduced_p))
8313 return error_mark_node;
8314 break;
8316 case CONSTRUCTOR:
8317 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8318 && pfd->include_nondeduced_p
8319 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8320 (TREE_TYPE (t)), fn, data,
8321 pfd->visited, pfd->include_nondeduced_p))
8322 return error_mark_node;
8323 break;
8325 case INDIRECT_REF:
8326 case COMPONENT_REF:
8327 /* If there's no type, then this thing must be some expression
8328 involving template parameters. */
8329 if (!fn && !TREE_TYPE (t))
8330 return error_mark_node;
8331 break;
8333 case MODOP_EXPR:
8334 case CAST_EXPR:
8335 case IMPLICIT_CONV_EXPR:
8336 case REINTERPRET_CAST_EXPR:
8337 case CONST_CAST_EXPR:
8338 case STATIC_CAST_EXPR:
8339 case DYNAMIC_CAST_EXPR:
8340 case ARROW_EXPR:
8341 case DOTSTAR_EXPR:
8342 case TYPEID_EXPR:
8343 case PSEUDO_DTOR_EXPR:
8344 if (!fn)
8345 return error_mark_node;
8346 break;
8348 default:
8349 break;
8352 /* We didn't find any template parameters we liked. */
8353 return NULL_TREE;
8356 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8357 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8358 call FN with the parameter and the DATA.
8359 If FN returns nonzero, the iteration is terminated, and
8360 for_each_template_parm returns 1. Otherwise, the iteration
8361 continues. If FN never returns a nonzero value, the value
8362 returned by for_each_template_parm is 0. If FN is NULL, it is
8363 considered to be the function which always returns 1.
8365 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8366 parameters that occur in non-deduced contexts. When false, only
8367 visits those template parameters that can be deduced. */
8369 static int
8370 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8371 struct pointer_set_t *visited,
8372 bool include_nondeduced_p)
8374 struct pair_fn_data pfd;
8375 int result;
8377 /* Set up. */
8378 pfd.fn = fn;
8379 pfd.data = data;
8380 pfd.include_nondeduced_p = include_nondeduced_p;
8382 /* Walk the tree. (Conceptually, we would like to walk without
8383 duplicates, but for_each_template_parm_r recursively calls
8384 for_each_template_parm, so we would need to reorganize a fair
8385 bit to use walk_tree_without_duplicates, so we keep our own
8386 visited list.) */
8387 if (visited)
8388 pfd.visited = visited;
8389 else
8390 pfd.visited = pointer_set_create ();
8391 result = cp_walk_tree (&t,
8392 for_each_template_parm_r,
8393 &pfd,
8394 pfd.visited) != NULL_TREE;
8396 /* Clean up. */
8397 if (!visited)
8399 pointer_set_destroy (pfd.visited);
8400 pfd.visited = 0;
8403 return result;
8406 /* Returns true if T depends on any template parameter. */
8409 uses_template_parms (tree t)
8411 bool dependent_p;
8412 int saved_processing_template_decl;
8414 saved_processing_template_decl = processing_template_decl;
8415 if (!saved_processing_template_decl)
8416 processing_template_decl = 1;
8417 if (TYPE_P (t))
8418 dependent_p = dependent_type_p (t);
8419 else if (TREE_CODE (t) == TREE_VEC)
8420 dependent_p = any_dependent_template_arguments_p (t);
8421 else if (TREE_CODE (t) == TREE_LIST)
8422 dependent_p = (uses_template_parms (TREE_VALUE (t))
8423 || uses_template_parms (TREE_CHAIN (t)));
8424 else if (TREE_CODE (t) == TYPE_DECL)
8425 dependent_p = dependent_type_p (TREE_TYPE (t));
8426 else if (DECL_P (t)
8427 || EXPR_P (t)
8428 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8429 || TREE_CODE (t) == OVERLOAD
8430 || BASELINK_P (t)
8431 || identifier_p (t)
8432 || TREE_CODE (t) == TRAIT_EXPR
8433 || TREE_CODE (t) == CONSTRUCTOR
8434 || CONSTANT_CLASS_P (t))
8435 dependent_p = (type_dependent_expression_p (t)
8436 || value_dependent_expression_p (t));
8437 else
8439 gcc_assert (t == error_mark_node);
8440 dependent_p = false;
8443 processing_template_decl = saved_processing_template_decl;
8445 return dependent_p;
8448 /* Returns true iff current_function_decl is an incompletely instantiated
8449 template. Useful instead of processing_template_decl because the latter
8450 is set to 0 during fold_non_dependent_expr. */
8452 bool
8453 in_template_function (void)
8455 tree fn = current_function_decl;
8456 bool ret;
8457 ++processing_template_decl;
8458 ret = (fn && DECL_LANG_SPECIFIC (fn)
8459 && DECL_TEMPLATE_INFO (fn)
8460 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8461 --processing_template_decl;
8462 return ret;
8465 /* Returns true if T depends on any template parameter with level LEVEL. */
8468 uses_template_parms_level (tree t, int level)
8470 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8471 /*include_nondeduced_p=*/true);
8474 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8475 ill-formed translation unit, i.e. a variable or function that isn't
8476 usable in a constant expression. */
8478 static inline bool
8479 neglectable_inst_p (tree d)
8481 return (DECL_P (d)
8482 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8483 : decl_maybe_constant_var_p (d)));
8486 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8487 neglectable and instantiated from within an erroneous instantiation. */
8489 static bool
8490 limit_bad_template_recursion (tree decl)
8492 struct tinst_level *lev = current_tinst_level;
8493 int errs = errorcount + sorrycount;
8494 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8495 return false;
8497 for (; lev; lev = lev->next)
8498 if (neglectable_inst_p (lev->decl))
8499 break;
8501 return (lev && errs > lev->errors);
8504 static int tinst_depth;
8505 extern int max_tinst_depth;
8506 int depth_reached;
8508 static GTY(()) struct tinst_level *last_error_tinst_level;
8510 /* We're starting to instantiate D; record the template instantiation context
8511 for diagnostics and to restore it later. */
8514 push_tinst_level (tree d)
8516 struct tinst_level *new_level;
8518 if (tinst_depth >= max_tinst_depth)
8520 last_error_tinst_level = current_tinst_level;
8521 if (TREE_CODE (d) == TREE_LIST)
8522 error ("template instantiation depth exceeds maximum of %d (use "
8523 "-ftemplate-depth= to increase the maximum) substituting %qS",
8524 max_tinst_depth, d);
8525 else
8526 error ("template instantiation depth exceeds maximum of %d (use "
8527 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8528 max_tinst_depth, d);
8530 print_instantiation_context ();
8532 return 0;
8535 /* If the current instantiation caused problems, don't let it instantiate
8536 anything else. Do allow deduction substitution and decls usable in
8537 constant expressions. */
8538 if (limit_bad_template_recursion (d))
8539 return 0;
8541 new_level = ggc_alloc_tinst_level ();
8542 new_level->decl = d;
8543 new_level->locus = input_location;
8544 new_level->errors = errorcount+sorrycount;
8545 new_level->in_system_header_p = in_system_header_at (input_location);
8546 new_level->next = current_tinst_level;
8547 current_tinst_level = new_level;
8549 ++tinst_depth;
8550 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8551 depth_reached = tinst_depth;
8553 return 1;
8556 /* We're done instantiating this template; return to the instantiation
8557 context. */
8559 void
8560 pop_tinst_level (void)
8562 /* Restore the filename and line number stashed away when we started
8563 this instantiation. */
8564 input_location = current_tinst_level->locus;
8565 current_tinst_level = current_tinst_level->next;
8566 --tinst_depth;
8569 /* We're instantiating a deferred template; restore the template
8570 instantiation context in which the instantiation was requested, which
8571 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8573 static tree
8574 reopen_tinst_level (struct tinst_level *level)
8576 struct tinst_level *t;
8578 tinst_depth = 0;
8579 for (t = level; t; t = t->next)
8580 ++tinst_depth;
8582 current_tinst_level = level;
8583 pop_tinst_level ();
8584 if (current_tinst_level)
8585 current_tinst_level->errors = errorcount+sorrycount;
8586 return level->decl;
8589 /* Returns the TINST_LEVEL which gives the original instantiation
8590 context. */
8592 struct tinst_level *
8593 outermost_tinst_level (void)
8595 struct tinst_level *level = current_tinst_level;
8596 if (level)
8597 while (level->next)
8598 level = level->next;
8599 return level;
8602 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8603 vector of template arguments, as for tsubst.
8605 Returns an appropriate tsubst'd friend declaration. */
8607 static tree
8608 tsubst_friend_function (tree decl, tree args)
8610 tree new_friend;
8612 if (TREE_CODE (decl) == FUNCTION_DECL
8613 && DECL_TEMPLATE_INSTANTIATION (decl)
8614 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8615 /* This was a friend declared with an explicit template
8616 argument list, e.g.:
8618 friend void f<>(T);
8620 to indicate that f was a template instantiation, not a new
8621 function declaration. Now, we have to figure out what
8622 instantiation of what template. */
8624 tree template_id, arglist, fns;
8625 tree new_args;
8626 tree tmpl;
8627 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8629 /* Friend functions are looked up in the containing namespace scope.
8630 We must enter that scope, to avoid finding member functions of the
8631 current class with same name. */
8632 push_nested_namespace (ns);
8633 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8634 tf_warning_or_error, NULL_TREE,
8635 /*integral_constant_expression_p=*/false);
8636 pop_nested_namespace (ns);
8637 arglist = tsubst (DECL_TI_ARGS (decl), args,
8638 tf_warning_or_error, NULL_TREE);
8639 template_id = lookup_template_function (fns, arglist);
8641 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8642 tmpl = determine_specialization (template_id, new_friend,
8643 &new_args,
8644 /*need_member_template=*/0,
8645 TREE_VEC_LENGTH (args),
8646 tsk_none);
8647 return instantiate_template (tmpl, new_args, tf_error);
8650 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8652 /* The NEW_FRIEND will look like an instantiation, to the
8653 compiler, but is not an instantiation from the point of view of
8654 the language. For example, we might have had:
8656 template <class T> struct S {
8657 template <class U> friend void f(T, U);
8660 Then, in S<int>, template <class U> void f(int, U) is not an
8661 instantiation of anything. */
8662 if (new_friend == error_mark_node)
8663 return error_mark_node;
8665 DECL_USE_TEMPLATE (new_friend) = 0;
8666 if (TREE_CODE (decl) == TEMPLATE_DECL)
8668 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8669 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8670 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8673 /* The mangled name for the NEW_FRIEND is incorrect. The function
8674 is not a template instantiation and should not be mangled like
8675 one. Therefore, we forget the mangling here; we'll recompute it
8676 later if we need it. */
8677 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8679 SET_DECL_RTL (new_friend, NULL);
8680 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8683 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8685 tree old_decl;
8686 tree new_friend_template_info;
8687 tree new_friend_result_template_info;
8688 tree ns;
8689 int new_friend_is_defn;
8691 /* We must save some information from NEW_FRIEND before calling
8692 duplicate decls since that function will free NEW_FRIEND if
8693 possible. */
8694 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8695 new_friend_is_defn =
8696 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8697 (template_for_substitution (new_friend)))
8698 != NULL_TREE);
8699 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8701 /* This declaration is a `primary' template. */
8702 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8704 new_friend_result_template_info
8705 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8707 else
8708 new_friend_result_template_info = NULL_TREE;
8710 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8711 if (new_friend_is_defn)
8712 DECL_INITIAL (new_friend) = error_mark_node;
8714 /* Inside pushdecl_namespace_level, we will push into the
8715 current namespace. However, the friend function should go
8716 into the namespace of the template. */
8717 ns = decl_namespace_context (new_friend);
8718 push_nested_namespace (ns);
8719 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8720 pop_nested_namespace (ns);
8722 if (old_decl == error_mark_node)
8723 return error_mark_node;
8725 if (old_decl != new_friend)
8727 /* This new friend declaration matched an existing
8728 declaration. For example, given:
8730 template <class T> void f(T);
8731 template <class U> class C {
8732 template <class T> friend void f(T) {}
8735 the friend declaration actually provides the definition
8736 of `f', once C has been instantiated for some type. So,
8737 old_decl will be the out-of-class template declaration,
8738 while new_friend is the in-class definition.
8740 But, if `f' was called before this point, the
8741 instantiation of `f' will have DECL_TI_ARGS corresponding
8742 to `T' but not to `U', references to which might appear
8743 in the definition of `f'. Previously, the most general
8744 template for an instantiation of `f' was the out-of-class
8745 version; now it is the in-class version. Therefore, we
8746 run through all specialization of `f', adding to their
8747 DECL_TI_ARGS appropriately. In particular, they need a
8748 new set of outer arguments, corresponding to the
8749 arguments for this class instantiation.
8751 The same situation can arise with something like this:
8753 friend void f(int);
8754 template <class T> class C {
8755 friend void f(T) {}
8758 when `C<int>' is instantiated. Now, `f(int)' is defined
8759 in the class. */
8761 if (!new_friend_is_defn)
8762 /* On the other hand, if the in-class declaration does
8763 *not* provide a definition, then we don't want to alter
8764 existing definitions. We can just leave everything
8765 alone. */
8767 else
8769 tree new_template = TI_TEMPLATE (new_friend_template_info);
8770 tree new_args = TI_ARGS (new_friend_template_info);
8772 /* Overwrite whatever template info was there before, if
8773 any, with the new template information pertaining to
8774 the declaration. */
8775 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8777 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8779 /* We should have called reregister_specialization in
8780 duplicate_decls. */
8781 gcc_assert (retrieve_specialization (new_template,
8782 new_args, 0)
8783 == old_decl);
8785 /* Instantiate it if the global has already been used. */
8786 if (DECL_ODR_USED (old_decl))
8787 instantiate_decl (old_decl, /*defer_ok=*/true,
8788 /*expl_inst_class_mem_p=*/false);
8790 else
8792 tree t;
8794 /* Indicate that the old function template is a partial
8795 instantiation. */
8796 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8797 = new_friend_result_template_info;
8799 gcc_assert (new_template
8800 == most_general_template (new_template));
8801 gcc_assert (new_template != old_decl);
8803 /* Reassign any specializations already in the hash table
8804 to the new more general template, and add the
8805 additional template args. */
8806 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8807 t != NULL_TREE;
8808 t = TREE_CHAIN (t))
8810 tree spec = TREE_VALUE (t);
8811 spec_entry elt;
8813 elt.tmpl = old_decl;
8814 elt.args = DECL_TI_ARGS (spec);
8815 elt.spec = NULL_TREE;
8817 htab_remove_elt (decl_specializations, &elt);
8819 DECL_TI_ARGS (spec)
8820 = add_outermost_template_args (new_args,
8821 DECL_TI_ARGS (spec));
8823 register_specialization
8824 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8827 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8831 /* The information from NEW_FRIEND has been merged into OLD_DECL
8832 by duplicate_decls. */
8833 new_friend = old_decl;
8836 else
8838 tree context = DECL_CONTEXT (new_friend);
8839 bool dependent_p;
8841 /* In the code
8842 template <class T> class C {
8843 template <class U> friend void C1<U>::f (); // case 1
8844 friend void C2<T>::f (); // case 2
8846 we only need to make sure CONTEXT is a complete type for
8847 case 2. To distinguish between the two cases, we note that
8848 CONTEXT of case 1 remains dependent type after tsubst while
8849 this isn't true for case 2. */
8850 ++processing_template_decl;
8851 dependent_p = dependent_type_p (context);
8852 --processing_template_decl;
8854 if (!dependent_p
8855 && !complete_type_or_else (context, NULL_TREE))
8856 return error_mark_node;
8858 if (COMPLETE_TYPE_P (context))
8860 tree fn = new_friend;
8861 /* do_friend adds the TEMPLATE_DECL for any member friend
8862 template even if it isn't a member template, i.e.
8863 template <class T> friend A<T>::f();
8864 Look through it in that case. */
8865 if (TREE_CODE (fn) == TEMPLATE_DECL
8866 && !PRIMARY_TEMPLATE_P (fn))
8867 fn = DECL_TEMPLATE_RESULT (fn);
8868 /* Check to see that the declaration is really present, and,
8869 possibly obtain an improved declaration. */
8870 fn = check_classfn (context, fn, NULL_TREE);
8872 if (fn)
8873 new_friend = fn;
8877 return new_friend;
8880 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8881 template arguments, as for tsubst.
8883 Returns an appropriate tsubst'd friend type or error_mark_node on
8884 failure. */
8886 static tree
8887 tsubst_friend_class (tree friend_tmpl, tree args)
8889 tree friend_type;
8890 tree tmpl;
8891 tree context;
8893 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8895 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8896 return TREE_TYPE (t);
8899 context = CP_DECL_CONTEXT (friend_tmpl);
8901 if (context != global_namespace)
8903 if (TREE_CODE (context) == NAMESPACE_DECL)
8904 push_nested_namespace (context);
8905 else
8906 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8909 /* Look for a class template declaration. We look for hidden names
8910 because two friend declarations of the same template are the
8911 same. For example, in:
8913 struct A {
8914 template <typename> friend class F;
8916 template <typename> struct B {
8917 template <typename> friend class F;
8920 both F templates are the same. */
8921 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8922 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8924 /* But, if we don't find one, it might be because we're in a
8925 situation like this:
8927 template <class T>
8928 struct S {
8929 template <class U>
8930 friend struct S;
8933 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8934 for `S<int>', not the TEMPLATE_DECL. */
8935 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8937 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8938 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8941 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8943 /* The friend template has already been declared. Just
8944 check to see that the declarations match, and install any new
8945 default parameters. We must tsubst the default parameters,
8946 of course. We only need the innermost template parameters
8947 because that is all that redeclare_class_template will look
8948 at. */
8949 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8950 > TMPL_ARGS_DEPTH (args))
8952 tree parms;
8953 location_t saved_input_location;
8954 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8955 args, tf_warning_or_error);
8957 saved_input_location = input_location;
8958 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8959 tree cons = DECL_CONSTRAINTS (tmpl);
8960 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
8961 input_location = saved_input_location;
8965 friend_type = TREE_TYPE (tmpl);
8967 else
8969 /* The friend template has not already been declared. In this
8970 case, the instantiation of the template class will cause the
8971 injection of this template into the global scope. */
8972 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8973 if (tmpl == error_mark_node)
8974 return error_mark_node;
8976 /* The new TMPL is not an instantiation of anything, so we
8977 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8978 the new type because that is supposed to be the corresponding
8979 template decl, i.e., TMPL. */
8980 DECL_USE_TEMPLATE (tmpl) = 0;
8981 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8982 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8983 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8984 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8986 /* Inject this template into the global scope. */
8987 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8990 if (context != global_namespace)
8992 if (TREE_CODE (context) == NAMESPACE_DECL)
8993 pop_nested_namespace (context);
8994 else
8995 pop_nested_class ();
8998 return friend_type;
9001 /* Returns zero if TYPE cannot be completed later due to circularity.
9002 Otherwise returns one. */
9004 static int
9005 can_complete_type_without_circularity (tree type)
9007 if (type == NULL_TREE || type == error_mark_node)
9008 return 0;
9009 else if (COMPLETE_TYPE_P (type))
9010 return 1;
9011 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
9012 return can_complete_type_without_circularity (TREE_TYPE (type));
9013 else if (CLASS_TYPE_P (type)
9014 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9015 return 0;
9016 else
9017 return 1;
9020 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
9022 /* Apply any attributes which had to be deferred until instantiation
9023 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9024 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9026 static void
9027 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9028 tree args, tsubst_flags_t complain, tree in_decl)
9030 tree last_dep = NULL_TREE;
9031 tree t;
9032 tree *p;
9034 for (t = attributes; t; t = TREE_CHAIN (t))
9035 if (ATTR_IS_DEPENDENT (t))
9037 last_dep = t;
9038 attributes = copy_list (attributes);
9039 break;
9042 if (DECL_P (*decl_p))
9044 if (TREE_TYPE (*decl_p) == error_mark_node)
9045 return;
9046 p = &DECL_ATTRIBUTES (*decl_p);
9048 else
9049 p = &TYPE_ATTRIBUTES (*decl_p);
9051 if (last_dep)
9053 tree late_attrs = NULL_TREE;
9054 tree *q = &late_attrs;
9056 for (*p = attributes; *p; )
9058 t = *p;
9059 if (ATTR_IS_DEPENDENT (t))
9061 *p = TREE_CHAIN (t);
9062 TREE_CHAIN (t) = NULL_TREE;
9063 if ((flag_openmp || flag_cilkplus)
9064 && is_attribute_p ("omp declare simd",
9065 get_attribute_name (t))
9066 && TREE_VALUE (t))
9068 tree clauses = TREE_VALUE (TREE_VALUE (t));
9069 clauses = tsubst_omp_clauses (clauses, true, args,
9070 complain, in_decl);
9071 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9072 clauses = finish_omp_clauses (clauses);
9073 tree parms = DECL_ARGUMENTS (*decl_p);
9074 clauses
9075 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9076 if (clauses)
9077 TREE_VALUE (TREE_VALUE (t)) = clauses;
9078 else
9079 TREE_VALUE (t) = NULL_TREE;
9081 /* If the first attribute argument is an identifier, don't
9082 pass it through tsubst. Attributes like mode, format,
9083 cleanup and several target specific attributes expect it
9084 unmodified. */
9085 else if (attribute_takes_identifier_p (get_attribute_name (t))
9086 && TREE_VALUE (t))
9088 tree chain
9089 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9090 in_decl,
9091 /*integral_constant_expression_p=*/false);
9092 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9093 TREE_VALUE (t)
9094 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9095 chain);
9097 else
9098 TREE_VALUE (t)
9099 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9100 /*integral_constant_expression_p=*/false);
9101 *q = t;
9102 q = &TREE_CHAIN (t);
9104 else
9105 p = &TREE_CHAIN (t);
9108 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9112 /* Perform (or defer) access check for typedefs that were referenced
9113 from within the template TMPL code.
9114 This is a subroutine of instantiate_decl and instantiate_class_template.
9115 TMPL is the template to consider and TARGS is the list of arguments of
9116 that template. */
9118 static void
9119 perform_typedefs_access_check (tree tmpl, tree targs)
9121 location_t saved_location;
9122 unsigned i;
9123 qualified_typedef_usage_t *iter;
9125 if (!tmpl
9126 || (!CLASS_TYPE_P (tmpl)
9127 && TREE_CODE (tmpl) != FUNCTION_DECL))
9128 return;
9130 saved_location = input_location;
9131 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9133 tree type_decl = iter->typedef_decl;
9134 tree type_scope = iter->context;
9136 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9137 continue;
9139 if (uses_template_parms (type_decl))
9140 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9141 if (uses_template_parms (type_scope))
9142 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9144 /* Make access check error messages point to the location
9145 of the use of the typedef. */
9146 input_location = iter->locus;
9147 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9148 type_decl, type_decl,
9149 tf_warning_or_error);
9151 input_location = saved_location;
9154 static tree
9155 instantiate_class_template_1 (tree type)
9157 tree templ, args, pattern, t, member;
9158 tree typedecl;
9159 tree pbinfo;
9160 tree base_list;
9161 unsigned int saved_maximum_field_alignment;
9162 tree fn_context;
9164 if (type == error_mark_node)
9165 return error_mark_node;
9167 if (COMPLETE_OR_OPEN_TYPE_P (type)
9168 || uses_template_parms (type))
9169 return type;
9171 /* Figure out which template is being instantiated. */
9172 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9173 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9175 /* Determine what specialization of the original template to
9176 instantiate. */
9177 t = most_specialized_class (type, tf_warning_or_error);
9178 if (t == error_mark_node)
9180 TYPE_BEING_DEFINED (type) = 1;
9181 return error_mark_node;
9183 else if (t)
9185 /* This TYPE is actually an instantiation of a partial
9186 specialization. We replace the innermost set of ARGS with
9187 the arguments appropriate for substitution. For example,
9188 given:
9190 template <class T> struct S {};
9191 template <class T> struct S<T*> {};
9193 and supposing that we are instantiating S<int*>, ARGS will
9194 presently be {int*} -- but we need {int}. */
9195 pattern = TREE_TYPE (t);
9196 args = TREE_PURPOSE (t);
9198 else
9200 pattern = TREE_TYPE (templ);
9201 args = CLASSTYPE_TI_ARGS (type);
9204 /* If the template we're instantiating is incomplete, then clearly
9205 there's nothing we can do. */
9206 if (!COMPLETE_TYPE_P (pattern))
9207 return type;
9209 /* If we've recursively instantiated too many templates, stop. */
9210 if (! push_tinst_level (type))
9211 return type;
9213 /* Now we're really doing the instantiation. Mark the type as in
9214 the process of being defined. */
9215 TYPE_BEING_DEFINED (type) = 1;
9217 /* We may be in the middle of deferred access check. Disable
9218 it now. */
9219 push_deferring_access_checks (dk_no_deferred);
9221 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9222 if (!fn_context)
9223 push_to_top_level ();
9224 /* Use #pragma pack from the template context. */
9225 saved_maximum_field_alignment = maximum_field_alignment;
9226 maximum_field_alignment = TYPE_PRECISION (pattern);
9228 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9230 /* Set the input location to the most specialized template definition.
9231 This is needed if tsubsting causes an error. */
9232 typedecl = TYPE_MAIN_DECL (pattern);
9233 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9234 DECL_SOURCE_LOCATION (typedecl);
9236 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9237 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9238 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9239 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9240 if (ANON_AGGR_TYPE_P (pattern))
9241 SET_ANON_AGGR_TYPE_P (type);
9242 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9244 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9245 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9246 /* Adjust visibility for template arguments. */
9247 determine_visibility (TYPE_MAIN_DECL (type));
9249 if (CLASS_TYPE_P (type))
9250 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9252 pbinfo = TYPE_BINFO (pattern);
9254 /* We should never instantiate a nested class before its enclosing
9255 class; we need to look up the nested class by name before we can
9256 instantiate it, and that lookup should instantiate the enclosing
9257 class. */
9258 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9259 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9261 base_list = NULL_TREE;
9262 if (BINFO_N_BASE_BINFOS (pbinfo))
9264 tree pbase_binfo;
9265 tree pushed_scope;
9266 int i;
9268 /* We must enter the scope containing the type, as that is where
9269 the accessibility of types named in dependent bases are
9270 looked up from. */
9271 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9273 /* Substitute into each of the bases to determine the actual
9274 basetypes. */
9275 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9277 tree base;
9278 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9279 tree expanded_bases = NULL_TREE;
9280 int idx, len = 1;
9282 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9284 expanded_bases =
9285 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9286 args, tf_error, NULL_TREE);
9287 if (expanded_bases == error_mark_node)
9288 continue;
9290 len = TREE_VEC_LENGTH (expanded_bases);
9293 for (idx = 0; idx < len; idx++)
9295 if (expanded_bases)
9296 /* Extract the already-expanded base class. */
9297 base = TREE_VEC_ELT (expanded_bases, idx);
9298 else
9299 /* Substitute to figure out the base class. */
9300 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9301 NULL_TREE);
9303 if (base == error_mark_node)
9304 continue;
9306 base_list = tree_cons (access, base, base_list);
9307 if (BINFO_VIRTUAL_P (pbase_binfo))
9308 TREE_TYPE (base_list) = integer_type_node;
9312 /* The list is now in reverse order; correct that. */
9313 base_list = nreverse (base_list);
9315 if (pushed_scope)
9316 pop_scope (pushed_scope);
9318 /* Now call xref_basetypes to set up all the base-class
9319 information. */
9320 xref_basetypes (type, base_list);
9322 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9323 (int) ATTR_FLAG_TYPE_IN_PLACE,
9324 args, tf_error, NULL_TREE);
9325 fixup_attribute_variants (type);
9327 /* Now that our base classes are set up, enter the scope of the
9328 class, so that name lookups into base classes, etc. will work
9329 correctly. This is precisely analogous to what we do in
9330 begin_class_definition when defining an ordinary non-template
9331 class, except we also need to push the enclosing classes. */
9332 push_nested_class (type);
9334 /* Now members are processed in the order of declaration. */
9335 for (member = CLASSTYPE_DECL_LIST (pattern);
9336 member; member = TREE_CHAIN (member))
9338 tree t = TREE_VALUE (member);
9340 if (TREE_PURPOSE (member))
9342 if (TYPE_P (t))
9344 /* Build new CLASSTYPE_NESTED_UTDS. */
9346 tree newtag;
9347 bool class_template_p;
9349 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9350 && TYPE_LANG_SPECIFIC (t)
9351 && CLASSTYPE_IS_TEMPLATE (t));
9352 /* If the member is a class template, then -- even after
9353 substitution -- there may be dependent types in the
9354 template argument list for the class. We increment
9355 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9356 that function will assume that no types are dependent
9357 when outside of a template. */
9358 if (class_template_p)
9359 ++processing_template_decl;
9360 newtag = tsubst (t, args, tf_error, NULL_TREE);
9361 if (class_template_p)
9362 --processing_template_decl;
9363 if (newtag == error_mark_node)
9364 continue;
9366 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9368 tree name = TYPE_IDENTIFIER (t);
9370 if (class_template_p)
9371 /* Unfortunately, lookup_template_class sets
9372 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9373 instantiation (i.e., for the type of a member
9374 template class nested within a template class.)
9375 This behavior is required for
9376 maybe_process_partial_specialization to work
9377 correctly, but is not accurate in this case;
9378 the TAG is not an instantiation of anything.
9379 (The corresponding TEMPLATE_DECL is an
9380 instantiation, but the TYPE is not.) */
9381 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9383 /* Now, we call pushtag to put this NEWTAG into the scope of
9384 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9385 pushtag calling push_template_decl. We don't have to do
9386 this for enums because it will already have been done in
9387 tsubst_enum. */
9388 if (name)
9389 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9390 pushtag (name, newtag, /*tag_scope=*/ts_current);
9393 else if (DECL_DECLARES_FUNCTION_P (t))
9395 /* Build new TYPE_METHODS. */
9396 tree r;
9398 if (TREE_CODE (t) == TEMPLATE_DECL)
9399 ++processing_template_decl;
9400 r = tsubst (t, args, tf_error, NULL_TREE);
9401 if (TREE_CODE (t) == TEMPLATE_DECL)
9402 --processing_template_decl;
9403 set_current_access_from_decl (r);
9404 finish_member_declaration (r);
9405 /* Instantiate members marked with attribute used. */
9406 if (r != error_mark_node && DECL_PRESERVE_P (r))
9407 mark_used (r);
9408 if (TREE_CODE (r) == FUNCTION_DECL
9409 && DECL_OMP_DECLARE_REDUCTION_P (r))
9410 cp_check_omp_declare_reduction (r);
9412 else
9414 /* Build new TYPE_FIELDS. */
9415 if (TREE_CODE (t) == STATIC_ASSERT)
9417 tree condition;
9419 ++c_inhibit_evaluation_warnings;
9420 condition =
9421 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9422 tf_warning_or_error, NULL_TREE,
9423 /*integral_constant_expression_p=*/true);
9424 --c_inhibit_evaluation_warnings;
9426 finish_static_assert (condition,
9427 STATIC_ASSERT_MESSAGE (t),
9428 STATIC_ASSERT_SOURCE_LOCATION (t),
9429 /*member_p=*/true);
9431 else if (TREE_CODE (t) != CONST_DECL)
9433 tree r;
9434 tree vec = NULL_TREE;
9435 int len = 1;
9437 /* The file and line for this declaration, to
9438 assist in error message reporting. Since we
9439 called push_tinst_level above, we don't need to
9440 restore these. */
9441 input_location = DECL_SOURCE_LOCATION (t);
9443 if (TREE_CODE (t) == TEMPLATE_DECL)
9444 ++processing_template_decl;
9445 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9446 if (TREE_CODE (t) == TEMPLATE_DECL)
9447 --processing_template_decl;
9449 if (TREE_CODE (r) == TREE_VEC)
9451 /* A capture pack became multiple fields. */
9452 vec = r;
9453 len = TREE_VEC_LENGTH (vec);
9456 for (int i = 0; i < len; ++i)
9458 if (vec)
9459 r = TREE_VEC_ELT (vec, i);
9460 if (VAR_P (r))
9462 /* In [temp.inst]:
9464 [t]he initialization (and any associated
9465 side-effects) of a static data member does
9466 not occur unless the static data member is
9467 itself used in a way that requires the
9468 definition of the static data member to
9469 exist.
9471 Therefore, we do not substitute into the
9472 initialized for the static data member here. */
9473 finish_static_data_member_decl
9475 /*init=*/NULL_TREE,
9476 /*init_const_expr_p=*/false,
9477 /*asmspec_tree=*/NULL_TREE,
9478 /*flags=*/0);
9479 /* Instantiate members marked with attribute used. */
9480 if (r != error_mark_node && DECL_PRESERVE_P (r))
9481 mark_used (r);
9483 else if (TREE_CODE (r) == FIELD_DECL)
9485 /* Determine whether R has a valid type and can be
9486 completed later. If R is invalid, then its type
9487 is replaced by error_mark_node. */
9488 tree rtype = TREE_TYPE (r);
9489 if (can_complete_type_without_circularity (rtype))
9490 complete_type (rtype);
9492 if (!COMPLETE_TYPE_P (rtype))
9494 cxx_incomplete_type_error (r, rtype);
9495 TREE_TYPE (r) = error_mark_node;
9499 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9500 such a thing will already have been added to the field
9501 list by tsubst_enum in finish_member_declaration in the
9502 CLASSTYPE_NESTED_UTDS case above. */
9503 if (!(TREE_CODE (r) == TYPE_DECL
9504 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9505 && DECL_ARTIFICIAL (r)))
9507 set_current_access_from_decl (r);
9508 finish_member_declaration (r);
9514 else
9516 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9517 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9519 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9521 tree friend_type = t;
9522 bool adjust_processing_template_decl = false;
9524 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9526 /* template <class T> friend class C; */
9527 friend_type = tsubst_friend_class (friend_type, args);
9528 adjust_processing_template_decl = true;
9530 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9532 /* template <class T> friend class C::D; */
9533 friend_type = tsubst (friend_type, args,
9534 tf_warning_or_error, NULL_TREE);
9535 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9536 friend_type = TREE_TYPE (friend_type);
9537 adjust_processing_template_decl = true;
9539 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9540 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9542 /* This could be either
9544 friend class T::C;
9546 when dependent_type_p is false or
9548 template <class U> friend class T::C;
9550 otherwise. */
9551 friend_type = tsubst (friend_type, args,
9552 tf_warning_or_error, NULL_TREE);
9553 /* Bump processing_template_decl for correct
9554 dependent_type_p calculation. */
9555 ++processing_template_decl;
9556 if (dependent_type_p (friend_type))
9557 adjust_processing_template_decl = true;
9558 --processing_template_decl;
9560 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9561 && hidden_name_p (TYPE_NAME (friend_type)))
9563 /* friend class C;
9565 where C hasn't been declared yet. Let's lookup name
9566 from namespace scope directly, bypassing any name that
9567 come from dependent base class. */
9568 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9570 /* The call to xref_tag_from_type does injection for friend
9571 classes. */
9572 push_nested_namespace (ns);
9573 friend_type =
9574 xref_tag_from_type (friend_type, NULL_TREE,
9575 /*tag_scope=*/ts_current);
9576 pop_nested_namespace (ns);
9578 else if (uses_template_parms (friend_type))
9579 /* friend class C<T>; */
9580 friend_type = tsubst (friend_type, args,
9581 tf_warning_or_error, NULL_TREE);
9582 /* Otherwise it's
9584 friend class C;
9586 where C is already declared or
9588 friend class C<int>;
9590 We don't have to do anything in these cases. */
9592 if (adjust_processing_template_decl)
9593 /* Trick make_friend_class into realizing that the friend
9594 we're adding is a template, not an ordinary class. It's
9595 important that we use make_friend_class since it will
9596 perform some error-checking and output cross-reference
9597 information. */
9598 ++processing_template_decl;
9600 if (friend_type != error_mark_node)
9601 make_friend_class (type, friend_type, /*complain=*/false);
9603 if (adjust_processing_template_decl)
9604 --processing_template_decl;
9606 else
9608 /* Build new DECL_FRIENDLIST. */
9609 tree r;
9611 /* The file and line for this declaration, to
9612 assist in error message reporting. Since we
9613 called push_tinst_level above, we don't need to
9614 restore these. */
9615 input_location = DECL_SOURCE_LOCATION (t);
9617 if (TREE_CODE (t) == TEMPLATE_DECL)
9619 ++processing_template_decl;
9620 push_deferring_access_checks (dk_no_check);
9623 r = tsubst_friend_function (t, args);
9624 add_friend (type, r, /*complain=*/false);
9625 if (TREE_CODE (t) == TEMPLATE_DECL)
9627 pop_deferring_access_checks ();
9628 --processing_template_decl;
9634 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9636 tree decl = lambda_function (type);
9637 if (decl)
9639 if (!DECL_TEMPLATE_INFO (decl)
9640 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9641 instantiate_decl (decl, false, false);
9643 /* We need to instantiate the capture list from the template
9644 after we've instantiated the closure members, but before we
9645 consider adding the conversion op. Also keep any captures
9646 that may have been added during instantiation of the op(). */
9647 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9648 tree tmpl_cap
9649 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9650 args, tf_warning_or_error, NULL_TREE,
9651 false, false);
9653 LAMBDA_EXPR_CAPTURE_LIST (expr)
9654 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9656 maybe_add_lambda_conv_op (type);
9658 else
9659 gcc_assert (errorcount);
9662 /* Set the file and line number information to whatever is given for
9663 the class itself. This puts error messages involving generated
9664 implicit functions at a predictable point, and the same point
9665 that would be used for non-template classes. */
9666 input_location = DECL_SOURCE_LOCATION (typedecl);
9668 unreverse_member_declarations (type);
9669 finish_struct_1 (type);
9670 TYPE_BEING_DEFINED (type) = 0;
9672 /* We don't instantiate default arguments for member functions. 14.7.1:
9674 The implicit instantiation of a class template specialization causes
9675 the implicit instantiation of the declarations, but not of the
9676 definitions or default arguments, of the class member functions,
9677 member classes, static data members and member templates.... */
9679 /* Some typedefs referenced from within the template code need to be access
9680 checked at template instantiation time, i.e now. These types were
9681 added to the template at parsing time. Let's get those and perform
9682 the access checks then. */
9683 perform_typedefs_access_check (pattern, args);
9684 perform_deferred_access_checks (tf_warning_or_error);
9685 pop_nested_class ();
9686 maximum_field_alignment = saved_maximum_field_alignment;
9687 if (!fn_context)
9688 pop_from_top_level ();
9689 pop_deferring_access_checks ();
9690 pop_tinst_level ();
9692 /* The vtable for a template class can be emitted in any translation
9693 unit in which the class is instantiated. When there is no key
9694 method, however, finish_struct_1 will already have added TYPE to
9695 the keyed_classes list. */
9696 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9697 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9699 return type;
9702 /* Wrapper for instantiate_class_template_1. */
9704 tree
9705 instantiate_class_template (tree type)
9707 tree ret;
9708 timevar_push (TV_TEMPLATE_INST);
9709 ret = instantiate_class_template_1 (type);
9710 timevar_pop (TV_TEMPLATE_INST);
9711 return ret;
9714 static tree
9715 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9717 tree r;
9719 if (!t)
9720 r = t;
9721 else if (TYPE_P (t))
9722 r = tsubst (t, args, complain, in_decl);
9723 else
9725 if (!(complain & tf_warning))
9726 ++c_inhibit_evaluation_warnings;
9727 r = tsubst_expr (t, args, complain, in_decl,
9728 /*integral_constant_expression_p=*/true);
9729 if (!(complain & tf_warning))
9730 --c_inhibit_evaluation_warnings;
9732 return r;
9735 /* Given a function parameter pack TMPL_PARM and some function parameters
9736 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9737 and set *SPEC_P to point at the next point in the list. */
9739 tree
9740 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9742 /* Collect all of the extra "packed" parameters into an
9743 argument pack. */
9744 tree parmvec;
9745 tree parmtypevec;
9746 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9747 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9748 tree spec_parm = *spec_p;
9749 int i, len;
9751 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9752 if (tmpl_parm
9753 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9754 break;
9756 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9757 parmvec = make_tree_vec (len);
9758 parmtypevec = make_tree_vec (len);
9759 spec_parm = *spec_p;
9760 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9762 TREE_VEC_ELT (parmvec, i) = spec_parm;
9763 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9766 /* Build the argument packs. */
9767 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9768 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9769 TREE_TYPE (argpack) = argtypepack;
9770 *spec_p = spec_parm;
9772 return argpack;
9775 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9776 NONTYPE_ARGUMENT_PACK. */
9778 static tree
9779 make_fnparm_pack (tree spec_parm)
9781 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9784 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9785 pack expansion. */
9787 static bool
9788 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9790 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9791 if (i >= TREE_VEC_LENGTH (vec))
9792 return false;
9793 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9797 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9799 static tree
9800 make_argument_pack_select (tree arg_pack, unsigned index)
9802 tree aps = make_node (ARGUMENT_PACK_SELECT);
9804 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9805 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9807 return aps;
9810 /* This is a subroutine of tsubst_pack_expansion.
9812 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9813 mechanism to store the (non complete list of) arguments of the
9814 substitution and return a non substituted pack expansion, in order
9815 to wait for when we have enough arguments to really perform the
9816 substitution. */
9818 static bool
9819 use_pack_expansion_extra_args_p (tree parm_packs,
9820 int arg_pack_len,
9821 bool has_empty_arg)
9823 /* If one pack has an expansion and another pack has a normal
9824 argument or if one pack has an empty argument and an another
9825 one hasn't then tsubst_pack_expansion cannot perform the
9826 substitution and need to fall back on the
9827 PACK_EXPANSION_EXTRA mechanism. */
9828 if (parm_packs == NULL_TREE)
9829 return false;
9830 else if (has_empty_arg)
9831 return true;
9833 bool has_expansion_arg = false;
9834 for (int i = 0 ; i < arg_pack_len; ++i)
9836 bool has_non_expansion_arg = false;
9837 for (tree parm_pack = parm_packs;
9838 parm_pack;
9839 parm_pack = TREE_CHAIN (parm_pack))
9841 tree arg = TREE_VALUE (parm_pack);
9843 if (argument_pack_element_is_expansion_p (arg, i))
9844 has_expansion_arg = true;
9845 else
9846 has_non_expansion_arg = true;
9849 if (has_expansion_arg && has_non_expansion_arg)
9850 return true;
9852 return false;
9855 /* [temp.variadic]/6 says that:
9857 The instantiation of a pack expansion [...]
9858 produces a list E1,E2, ..., En, where N is the number of elements
9859 in the pack expansion parameters.
9861 This subroutine of tsubst_pack_expansion produces one of these Ei.
9863 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9864 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9865 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9866 INDEX is the index 'i' of the element Ei to produce. ARGS,
9867 COMPLAIN, and IN_DECL are the same parameters as for the
9868 tsubst_pack_expansion function.
9870 The function returns the resulting Ei upon successful completion,
9871 or error_mark_node.
9873 Note that this function possibly modifies the ARGS parameter, so
9874 it's the responsibility of the caller to restore it. */
9876 static tree
9877 gen_elem_of_pack_expansion_instantiation (tree pattern,
9878 tree parm_packs,
9879 unsigned index,
9880 tree args /* This parm gets
9881 modified. */,
9882 tsubst_flags_t complain,
9883 tree in_decl)
9885 tree t;
9886 bool ith_elem_is_expansion = false;
9888 /* For each parameter pack, change the substitution of the parameter
9889 pack to the ith argument in its argument pack, then expand the
9890 pattern. */
9891 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9893 tree parm = TREE_PURPOSE (pack);
9894 tree arg_pack = TREE_VALUE (pack);
9895 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9897 ith_elem_is_expansion |=
9898 argument_pack_element_is_expansion_p (arg_pack, index);
9900 /* Select the Ith argument from the pack. */
9901 if (TREE_CODE (parm) == PARM_DECL
9902 || TREE_CODE (parm) == FIELD_DECL)
9904 if (index == 0)
9906 aps = make_argument_pack_select (arg_pack, index);
9907 mark_used (parm);
9908 register_local_specialization (aps, parm);
9910 else
9911 aps = retrieve_local_specialization (parm);
9913 else
9915 int idx, level;
9916 template_parm_level_and_index (parm, &level, &idx);
9918 if (index == 0)
9920 aps = make_argument_pack_select (arg_pack, index);
9921 /* Update the corresponding argument. */
9922 TMPL_ARG (args, level, idx) = aps;
9924 else
9925 /* Re-use the ARGUMENT_PACK_SELECT. */
9926 aps = TMPL_ARG (args, level, idx);
9928 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9931 /* Substitute into the PATTERN with the (possibly altered)
9932 arguments. */
9933 if (pattern == in_decl)
9934 /* Expanding a fixed parameter pack from
9935 coerce_template_parameter_pack. */
9936 t = tsubst_decl (pattern, args, complain);
9937 else if (!TYPE_P (pattern))
9938 t = tsubst_expr (pattern, args, complain, in_decl,
9939 /*integral_constant_expression_p=*/false);
9940 else
9941 t = tsubst (pattern, args, complain, in_decl);
9943 /* If the Ith argument pack element is a pack expansion, then
9944 the Ith element resulting from the substituting is going to
9945 be a pack expansion as well. */
9946 if (ith_elem_is_expansion)
9947 t = make_pack_expansion (t);
9949 return t;
9952 // Substitute args into the pack expansion T, and rewrite the resulting
9953 // list as a conjuntion of the specified terms. If the result is an empty
9954 // expression, return boolean_true_node.
9955 tree
9956 tsubst_pack_conjunction (tree t, tree args, tsubst_flags_t complain,
9957 tree in_decl)
9959 tree terms = tsubst_pack_expansion (t, args, complain, in_decl);
9961 // If the resulting expression is type- or value-dependent, then
9962 // return it after setting the result type to bool (so it can be
9963 // expanded as a conjunction).
9965 // This happens when instantiating constrained variadic
9966 // member function templates.
9967 if (instantiation_dependent_expression_p (terms))
9969 TREE_TYPE (terms) = boolean_type_node;
9970 return terms;
9973 // Conjoin requirements. An empty conjunction is equivalent to ture.
9974 if (tree reqs = conjoin_requirements (terms))
9975 return reqs;
9976 else
9977 return boolean_true_node;
9981 /* Substitute ARGS into T, which is an pack expansion
9982 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9983 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9984 (if only a partial substitution could be performed) or
9985 ERROR_MARK_NODE if there was an error. */
9986 tree
9987 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9988 tree in_decl)
9990 tree pattern;
9991 tree pack, packs = NULL_TREE;
9992 bool unsubstituted_packs = false;
9993 int i, len = -1;
9994 tree result;
9995 struct pointer_map_t *saved_local_specializations = NULL;
9996 bool need_local_specializations = false;
9997 int levels;
9999 gcc_assert (PACK_EXPANSION_P (t));
10000 pattern = PACK_EXPANSION_PATTERN (t);
10002 /* Add in any args remembered from an earlier partial instantiation. */
10003 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10005 levels = TMPL_ARGS_DEPTH (args);
10007 /* Determine the argument packs that will instantiate the parameter
10008 packs used in the expansion expression. While we're at it,
10009 compute the number of arguments to be expanded and make sure it
10010 is consistent. */
10011 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10012 pack = TREE_CHAIN (pack))
10014 tree parm_pack = TREE_VALUE (pack);
10015 tree arg_pack = NULL_TREE;
10016 tree orig_arg = NULL_TREE;
10017 int level = 0;
10019 if (TREE_CODE (parm_pack) == BASES)
10021 if (BASES_DIRECT (parm_pack))
10022 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10023 args, complain, in_decl, false));
10024 else
10025 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10026 args, complain, in_decl, false));
10028 if (TREE_CODE (parm_pack) == PARM_DECL)
10030 if (PACK_EXPANSION_LOCAL_P (t))
10031 arg_pack = retrieve_local_specialization (parm_pack);
10032 else
10034 /* We can't rely on local_specializations for a parameter
10035 name used later in a function declaration (such as in a
10036 late-specified return type). Even if it exists, it might
10037 have the wrong value for a recursive call. Just make a
10038 dummy decl, since it's only used for its type. */
10039 arg_pack = tsubst_decl (parm_pack, args, complain);
10040 if (arg_pack && DECL_PACK_P (arg_pack))
10041 /* Partial instantiation of the parm_pack, we can't build
10042 up an argument pack yet. */
10043 arg_pack = NULL_TREE;
10044 else
10045 arg_pack = make_fnparm_pack (arg_pack);
10046 need_local_specializations = true;
10049 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10050 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10051 else
10053 int idx;
10054 template_parm_level_and_index (parm_pack, &level, &idx);
10056 if (level <= levels)
10057 arg_pack = TMPL_ARG (args, level, idx);
10060 orig_arg = arg_pack;
10061 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
10062 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
10064 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
10065 /* This can only happen if we forget to expand an argument
10066 pack somewhere else. Just return an error, silently. */
10068 result = make_tree_vec (1);
10069 TREE_VEC_ELT (result, 0) = error_mark_node;
10070 return result;
10073 if (arg_pack)
10075 int my_len =
10076 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10078 /* Don't bother trying to do a partial substitution with
10079 incomplete packs; we'll try again after deduction. */
10080 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10081 return t;
10083 if (len < 0)
10084 len = my_len;
10085 else if (len != my_len)
10087 if (!(complain & tf_error))
10088 /* Fail quietly. */;
10089 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10090 error ("mismatched argument pack lengths while expanding "
10091 "%<%T%>",
10092 pattern);
10093 else
10094 error ("mismatched argument pack lengths while expanding "
10095 "%<%E%>",
10096 pattern);
10097 return error_mark_node;
10100 /* Keep track of the parameter packs and their corresponding
10101 argument packs. */
10102 packs = tree_cons (parm_pack, arg_pack, packs);
10103 TREE_TYPE (packs) = orig_arg;
10105 else
10107 /* We can't substitute for this parameter pack. We use a flag as
10108 well as the missing_level counter because function parameter
10109 packs don't have a level. */
10110 unsubstituted_packs = true;
10114 /* We cannot expand this expansion expression, because we don't have
10115 all of the argument packs we need. */
10116 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10118 /* We got some full packs, but we can't substitute them in until we
10119 have values for all the packs. So remember these until then. */
10121 t = make_pack_expansion (pattern);
10122 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10123 return t;
10125 else if (unsubstituted_packs)
10127 /* There were no real arguments, we're just replacing a parameter
10128 pack with another version of itself. Substitute into the
10129 pattern and return a PACK_EXPANSION_*. The caller will need to
10130 deal with that. */
10131 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10132 t = tsubst_expr (pattern, args, complain, in_decl,
10133 /*integral_constant_expression_p=*/false);
10134 else
10135 t = tsubst (pattern, args, complain, in_decl);
10136 t = make_pack_expansion (t);
10137 return t;
10140 gcc_assert (len >= 0);
10142 if (need_local_specializations)
10144 /* We're in a late-specified return type, so create our own local
10145 specializations map; the current map is either NULL or (in the
10146 case of recursive unification) might have bindings that we don't
10147 want to use or alter. */
10148 saved_local_specializations = local_specializations;
10149 local_specializations = pointer_map_create ();
10152 /* For each argument in each argument pack, substitute into the
10153 pattern. */
10154 result = make_tree_vec (len);
10155 for (i = 0; i < len; ++i)
10157 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10159 args, complain,
10160 in_decl);
10161 TREE_VEC_ELT (result, i) = t;
10162 if (t == error_mark_node)
10164 result = error_mark_node;
10165 break;
10169 /* Update ARGS to restore the substitution from parameter packs to
10170 their argument packs. */
10171 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10173 tree parm = TREE_PURPOSE (pack);
10175 if (TREE_CODE (parm) == PARM_DECL
10176 || TREE_CODE (parm) == FIELD_DECL)
10177 register_local_specialization (TREE_TYPE (pack), parm);
10178 else
10180 int idx, level;
10182 if (TREE_VALUE (pack) == NULL_TREE)
10183 continue;
10185 template_parm_level_and_index (parm, &level, &idx);
10187 /* Update the corresponding argument. */
10188 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10189 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10190 TREE_TYPE (pack);
10191 else
10192 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10196 if (need_local_specializations)
10198 pointer_map_destroy (local_specializations);
10199 local_specializations = saved_local_specializations;
10202 return result;
10205 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10206 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10207 parameter packs; all parms generated from a function parameter pack will
10208 have the same DECL_PARM_INDEX. */
10210 tree
10211 get_pattern_parm (tree parm, tree tmpl)
10213 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10214 tree patparm;
10216 if (DECL_ARTIFICIAL (parm))
10218 for (patparm = DECL_ARGUMENTS (pattern);
10219 patparm; patparm = DECL_CHAIN (patparm))
10220 if (DECL_ARTIFICIAL (patparm)
10221 && DECL_NAME (parm) == DECL_NAME (patparm))
10222 break;
10224 else
10226 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10227 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10228 gcc_assert (DECL_PARM_INDEX (patparm)
10229 == DECL_PARM_INDEX (parm));
10232 return patparm;
10235 /* Substitute ARGS into the vector or list of template arguments T. */
10237 static tree
10238 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10240 tree orig_t = t;
10241 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10242 tree *elts;
10244 if (t == error_mark_node)
10245 return error_mark_node;
10247 len = TREE_VEC_LENGTH (t);
10248 elts = XALLOCAVEC (tree, len);
10250 for (i = 0; i < len; i++)
10252 tree orig_arg = TREE_VEC_ELT (t, i);
10253 tree new_arg;
10255 if (TREE_CODE (orig_arg) == TREE_VEC)
10256 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10257 else if (PACK_EXPANSION_P (orig_arg))
10259 /* Substitute into an expansion expression. */
10260 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10262 if (TREE_CODE (new_arg) == TREE_VEC)
10263 /* Add to the expanded length adjustment the number of
10264 expanded arguments. We subtract one from this
10265 measurement, because the argument pack expression
10266 itself is already counted as 1 in
10267 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10268 the argument pack is empty. */
10269 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10271 else if (ARGUMENT_PACK_P (orig_arg))
10273 /* Substitute into each of the arguments. */
10274 new_arg = TYPE_P (orig_arg)
10275 ? cxx_make_type (TREE_CODE (orig_arg))
10276 : make_node (TREE_CODE (orig_arg));
10278 SET_ARGUMENT_PACK_ARGS (
10279 new_arg,
10280 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10281 args, complain, in_decl));
10283 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10284 new_arg = error_mark_node;
10286 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10287 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10288 complain, in_decl);
10289 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10291 if (TREE_TYPE (new_arg) == error_mark_node)
10292 new_arg = error_mark_node;
10295 else
10296 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10298 if (new_arg == error_mark_node)
10299 return error_mark_node;
10301 elts[i] = new_arg;
10302 if (new_arg != orig_arg)
10303 need_new = 1;
10306 if (!need_new)
10307 return t;
10309 /* Make space for the expanded arguments coming from template
10310 argument packs. */
10311 t = make_tree_vec (len + expanded_len_adjust);
10312 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10313 arguments for a member template.
10314 In that case each TREE_VEC in ORIG_T represents a level of template
10315 arguments, and ORIG_T won't carry any non defaulted argument count.
10316 It will rather be the nested TREE_VECs that will carry one.
10317 In other words, ORIG_T carries a non defaulted argument count only
10318 if it doesn't contain any nested TREE_VEC. */
10319 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10321 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10322 count += expanded_len_adjust;
10323 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10325 for (i = 0, out = 0; i < len; i++)
10327 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10328 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10329 && TREE_CODE (elts[i]) == TREE_VEC)
10331 int idx;
10333 /* Now expand the template argument pack "in place". */
10334 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10335 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10337 else
10339 TREE_VEC_ELT (t, out) = elts[i];
10340 out++;
10344 return t;
10347 /* Return the result of substituting ARGS into the template parameters
10348 given by PARMS. If there are m levels of ARGS and m + n levels of
10349 PARMS, then the result will contain n levels of PARMS. For
10350 example, if PARMS is `template <class T> template <class U>
10351 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10352 result will be `template <int*, double, class V>'. */
10354 static tree
10355 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10357 tree r = NULL_TREE;
10358 tree* new_parms;
10360 /* When substituting into a template, we must set
10361 PROCESSING_TEMPLATE_DECL as the template parameters may be
10362 dependent if they are based on one-another, and the dependency
10363 predicates are short-circuit outside of templates. */
10364 ++processing_template_decl;
10366 for (new_parms = &r;
10367 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10368 new_parms = &(TREE_CHAIN (*new_parms)),
10369 parms = TREE_CHAIN (parms))
10371 tree new_vec =
10372 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10373 int i;
10375 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10377 tree tuple;
10379 if (parms == error_mark_node)
10380 continue;
10382 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10384 if (tuple == error_mark_node)
10385 continue;
10387 TREE_VEC_ELT (new_vec, i) =
10388 tsubst_template_parm (tuple, args, complain);
10391 *new_parms =
10392 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10393 - TMPL_ARGS_DEPTH (args)),
10394 new_vec, NULL_TREE);
10397 --processing_template_decl;
10399 return r;
10402 /* Return the result of substituting ARGS into one template parameter
10403 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10404 parameter and which TREE_PURPOSE is the default argument of the
10405 template parameter. */
10407 static tree
10408 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10410 tree default_value, parm_decl;
10412 if (args == NULL_TREE
10413 || t == NULL_TREE
10414 || t == error_mark_node)
10415 return t;
10417 gcc_assert (TREE_CODE (t) == TREE_LIST);
10419 default_value = TREE_PURPOSE (t);
10420 parm_decl = TREE_VALUE (t);
10422 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10423 if (TREE_CODE (parm_decl) == PARM_DECL
10424 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10425 parm_decl = error_mark_node;
10426 default_value = tsubst_template_arg (default_value, args,
10427 complain, NULL_TREE);
10429 return build_tree_list (default_value, parm_decl);
10432 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10433 type T. If T is not an aggregate or enumeration type, it is
10434 handled as if by tsubst. IN_DECL is as for tsubst. If
10435 ENTERING_SCOPE is nonzero, T is the context for a template which
10436 we are presently tsubst'ing. Return the substituted value. */
10438 static tree
10439 tsubst_aggr_type (tree t,
10440 tree args,
10441 tsubst_flags_t complain,
10442 tree in_decl,
10443 int entering_scope)
10445 if (t == NULL_TREE)
10446 return NULL_TREE;
10448 switch (TREE_CODE (t))
10450 case RECORD_TYPE:
10451 if (TYPE_PTRMEMFUNC_P (t))
10452 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10454 /* Else fall through. */
10455 case ENUMERAL_TYPE:
10456 case UNION_TYPE:
10457 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10459 tree argvec;
10460 tree context;
10461 tree r;
10462 int saved_unevaluated_operand;
10463 int saved_inhibit_evaluation_warnings;
10465 /* In "sizeof(X<I>)" we need to evaluate "I". */
10466 saved_unevaluated_operand = cp_unevaluated_operand;
10467 cp_unevaluated_operand = 0;
10468 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10469 c_inhibit_evaluation_warnings = 0;
10471 /* First, determine the context for the type we are looking
10472 up. */
10473 context = TYPE_CONTEXT (t);
10474 if (context && TYPE_P (context))
10476 context = tsubst_aggr_type (context, args, complain,
10477 in_decl, /*entering_scope=*/1);
10478 /* If context is a nested class inside a class template,
10479 it may still need to be instantiated (c++/33959). */
10480 context = complete_type (context);
10483 /* Then, figure out what arguments are appropriate for the
10484 type we are trying to find. For example, given:
10486 template <class T> struct S;
10487 template <class T, class U> void f(T, U) { S<U> su; }
10489 and supposing that we are instantiating f<int, double>,
10490 then our ARGS will be {int, double}, but, when looking up
10491 S we only want {double}. */
10492 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10493 complain, in_decl);
10494 if (argvec == error_mark_node)
10495 r = error_mark_node;
10496 else
10498 r = lookup_template_class (t, argvec, in_decl, context,
10499 entering_scope, complain);
10500 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10503 cp_unevaluated_operand = saved_unevaluated_operand;
10504 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10506 return r;
10508 else
10509 /* This is not a template type, so there's nothing to do. */
10510 return t;
10512 default:
10513 return tsubst (t, args, complain, in_decl);
10517 /* Substitute into the default argument ARG (a default argument for
10518 FN), which has the indicated TYPE. */
10520 tree
10521 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10523 tree saved_class_ptr = NULL_TREE;
10524 tree saved_class_ref = NULL_TREE;
10525 int errs = errorcount + sorrycount;
10527 /* This can happen in invalid code. */
10528 if (TREE_CODE (arg) == DEFAULT_ARG)
10529 return arg;
10531 /* This default argument came from a template. Instantiate the
10532 default argument here, not in tsubst. In the case of
10533 something like:
10535 template <class T>
10536 struct S {
10537 static T t();
10538 void f(T = t());
10541 we must be careful to do name lookup in the scope of S<T>,
10542 rather than in the current class. */
10543 push_access_scope (fn);
10544 /* The "this" pointer is not valid in a default argument. */
10545 if (cfun)
10547 saved_class_ptr = current_class_ptr;
10548 cp_function_chain->x_current_class_ptr = NULL_TREE;
10549 saved_class_ref = current_class_ref;
10550 cp_function_chain->x_current_class_ref = NULL_TREE;
10553 push_deferring_access_checks(dk_no_deferred);
10554 /* The default argument expression may cause implicitly defined
10555 member functions to be synthesized, which will result in garbage
10556 collection. We must treat this situation as if we were within
10557 the body of function so as to avoid collecting live data on the
10558 stack. */
10559 ++function_depth;
10560 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10561 complain, NULL_TREE,
10562 /*integral_constant_expression_p=*/false);
10563 --function_depth;
10564 pop_deferring_access_checks();
10566 /* Restore the "this" pointer. */
10567 if (cfun)
10569 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10570 cp_function_chain->x_current_class_ref = saved_class_ref;
10573 if (errorcount+sorrycount > errs
10574 && (complain & tf_warning_or_error))
10575 inform (input_location,
10576 " when instantiating default argument for call to %D", fn);
10578 /* Make sure the default argument is reasonable. */
10579 arg = check_default_argument (type, arg, complain);
10581 pop_access_scope (fn);
10583 return arg;
10586 /* Substitute into all the default arguments for FN. */
10588 static void
10589 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10591 tree arg;
10592 tree tmpl_args;
10594 tmpl_args = DECL_TI_ARGS (fn);
10596 /* If this function is not yet instantiated, we certainly don't need
10597 its default arguments. */
10598 if (uses_template_parms (tmpl_args))
10599 return;
10600 /* Don't do this again for clones. */
10601 if (DECL_CLONED_FUNCTION_P (fn))
10602 return;
10604 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10605 arg;
10606 arg = TREE_CHAIN (arg))
10607 if (TREE_PURPOSE (arg))
10608 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10609 TREE_VALUE (arg),
10610 TREE_PURPOSE (arg),
10611 complain);
10614 /* Substitute the ARGS into the T, which is a _DECL. Return the
10615 result of the substitution. Issue error and warning messages under
10616 control of COMPLAIN. */
10618 static tree
10619 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10621 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10622 location_t saved_loc;
10623 tree r = NULL_TREE;
10624 tree in_decl = t;
10625 hashval_t hash = 0;
10627 /* Set the filename and linenumber to improve error-reporting. */
10628 saved_loc = input_location;
10629 input_location = DECL_SOURCE_LOCATION (t);
10631 switch (TREE_CODE (t))
10633 case TEMPLATE_DECL:
10635 /* We can get here when processing a member function template,
10636 member class template, or template template parameter. */
10637 tree decl = DECL_TEMPLATE_RESULT (t);
10638 tree type = TREE_TYPE (t);
10639 tree spec;
10640 tree tmpl_args;
10641 tree full_args;
10643 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10645 /* Template template parameter is treated here. */
10646 tree new_type = tsubst (type, args, complain, in_decl);
10647 if (new_type == error_mark_node)
10648 RETURN (error_mark_node);
10649 /* If we get a real template back, return it. This can happen in
10650 the context of most_specialized_class. */
10651 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10652 return new_type;
10654 r = copy_decl (t);
10655 DECL_CHAIN (r) = NULL_TREE;
10656 TREE_TYPE (r) = new_type;
10657 DECL_TEMPLATE_RESULT (r)
10658 = build_decl (DECL_SOURCE_LOCATION (decl),
10659 TYPE_DECL, DECL_NAME (decl), new_type);
10660 DECL_TEMPLATE_PARMS (r)
10661 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10662 complain);
10663 TYPE_NAME (new_type) = r;
10664 break;
10667 /* We might already have an instance of this template.
10668 The ARGS are for the surrounding class type, so the
10669 full args contain the tsubst'd args for the context,
10670 plus the innermost args from the template decl. */
10671 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10672 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10673 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10674 /* Because this is a template, the arguments will still be
10675 dependent, even after substitution. If
10676 PROCESSING_TEMPLATE_DECL is not set, the dependency
10677 predicates will short-circuit. */
10678 ++processing_template_decl;
10679 full_args = tsubst_template_args (tmpl_args, args,
10680 complain, in_decl);
10681 --processing_template_decl;
10682 if (full_args == error_mark_node)
10683 RETURN (error_mark_node);
10685 /* If this is a default template template argument,
10686 tsubst might not have changed anything. */
10687 if (full_args == tmpl_args)
10688 RETURN (t);
10690 hash = hash_tmpl_and_args (t, full_args);
10691 spec = retrieve_specialization (t, full_args, hash);
10692 if (spec != NULL_TREE)
10694 r = spec;
10695 break;
10698 /* Make a new template decl. It will be similar to the
10699 original, but will record the current template arguments.
10700 We also create a new function declaration, which is just
10701 like the old one, but points to this new template, rather
10702 than the old one. */
10703 r = copy_decl (t);
10704 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10705 DECL_CHAIN (r) = NULL_TREE;
10707 // Build new template info linking to the original template decl.
10708 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10710 if (TREE_CODE (decl) == TYPE_DECL
10711 && !TYPE_DECL_ALIAS_P (decl))
10713 tree new_type;
10714 ++processing_template_decl;
10715 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10716 --processing_template_decl;
10717 if (new_type == error_mark_node)
10718 RETURN (error_mark_node);
10720 TREE_TYPE (r) = new_type;
10721 /* For a partial specialization, we need to keep pointing to
10722 the primary template. */
10723 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10724 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10725 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10726 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10727 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10729 else
10731 tree new_decl;
10732 ++processing_template_decl;
10733 new_decl = tsubst (decl, args, complain, in_decl);
10734 --processing_template_decl;
10735 if (new_decl == error_mark_node)
10736 RETURN (error_mark_node);
10738 DECL_TEMPLATE_RESULT (r) = new_decl;
10739 DECL_TI_TEMPLATE (new_decl) = r;
10740 TREE_TYPE (r) = TREE_TYPE (new_decl);
10741 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10742 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10745 SET_DECL_IMPLICIT_INSTANTIATION (r);
10746 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10747 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10749 /* The template parameters for this new template are all the
10750 template parameters for the old template, except the
10751 outermost level of parameters. */
10752 DECL_TEMPLATE_PARMS (r)
10753 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10754 complain);
10756 // If constrained, also instantiate requirements.
10757 // TODO: Instantiate shorthand constraints on parameters also.
10758 // See tsubst_template_parms for that.
10759 if (tree ci = DECL_CONSTRAINTS (t))
10761 tree reqs = instantiate_requirements (CI_SPELLING (ci), args);
10762 DECL_CONSTRAINTS (r) = finish_template_requirements (reqs);
10765 if (PRIMARY_TEMPLATE_P (t))
10766 DECL_PRIMARY_TEMPLATE (r) = r;
10768 if (TREE_CODE (decl) != TYPE_DECL)
10769 /* Record this non-type partial instantiation. */
10770 register_specialization (r, t,
10771 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10772 false, hash);
10774 break;
10776 case FUNCTION_DECL:
10778 tree ctx;
10779 tree argvec = NULL_TREE;
10780 tree *friends;
10781 tree gen_tmpl;
10782 tree type;
10783 int member;
10784 int args_depth;
10785 int parms_depth;
10787 /* Nobody should be tsubst'ing into non-template functions. */
10788 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10790 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10792 tree spec;
10793 bool dependent_p;
10795 /* If T is not dependent, just return it. We have to
10796 increment PROCESSING_TEMPLATE_DECL because
10797 value_dependent_expression_p assumes that nothing is
10798 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10799 ++processing_template_decl;
10800 dependent_p = value_dependent_expression_p (t);
10801 --processing_template_decl;
10802 if (!dependent_p)
10803 RETURN (t);
10805 /* Calculate the most general template of which R is a
10806 specialization, and the complete set of arguments used to
10807 specialize R. */
10808 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10809 argvec = tsubst_template_args (DECL_TI_ARGS
10810 (DECL_TEMPLATE_RESULT
10811 (DECL_TI_TEMPLATE (t))),
10812 args, complain, in_decl);
10813 if (argvec == error_mark_node)
10814 RETURN (error_mark_node);
10816 /* Check to see if we already have this specialization. */
10817 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10818 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10820 if (spec)
10822 r = spec;
10823 break;
10826 /* We can see more levels of arguments than parameters if
10827 there was a specialization of a member template, like
10828 this:
10830 template <class T> struct S { template <class U> void f(); }
10831 template <> template <class U> void S<int>::f(U);
10833 Here, we'll be substituting into the specialization,
10834 because that's where we can find the code we actually
10835 want to generate, but we'll have enough arguments for
10836 the most general template.
10838 We also deal with the peculiar case:
10840 template <class T> struct S {
10841 template <class U> friend void f();
10843 template <class U> void f() {}
10844 template S<int>;
10845 template void f<double>();
10847 Here, the ARGS for the instantiation of will be {int,
10848 double}. But, we only need as many ARGS as there are
10849 levels of template parameters in CODE_PATTERN. We are
10850 careful not to get fooled into reducing the ARGS in
10851 situations like:
10853 template <class T> struct S { template <class U> void f(U); }
10854 template <class T> template <> void S<T>::f(int) {}
10856 which we can spot because the pattern will be a
10857 specialization in this case. */
10858 args_depth = TMPL_ARGS_DEPTH (args);
10859 parms_depth =
10860 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10861 if (args_depth > parms_depth
10862 && !DECL_TEMPLATE_SPECIALIZATION (t))
10863 args = get_innermost_template_args (args, parms_depth);
10865 else
10867 /* This special case arises when we have something like this:
10869 template <class T> struct S {
10870 friend void f<int>(int, double);
10873 Here, the DECL_TI_TEMPLATE for the friend declaration
10874 will be an IDENTIFIER_NODE. We are being called from
10875 tsubst_friend_function, and we want only to create a
10876 new decl (R) with appropriate types so that we can call
10877 determine_specialization. */
10878 gen_tmpl = NULL_TREE;
10881 if (DECL_CLASS_SCOPE_P (t))
10883 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10884 member = 2;
10885 else
10886 member = 1;
10887 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10888 complain, t, /*entering_scope=*/1);
10890 else
10892 member = 0;
10893 ctx = DECL_CONTEXT (t);
10895 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10896 if (type == error_mark_node)
10897 RETURN (error_mark_node);
10899 /* If we hit excessive deduction depth, the type is bogus even if
10900 it isn't error_mark_node, so don't build a decl. */
10901 if (excessive_deduction_depth)
10902 RETURN (error_mark_node);
10904 /* We do NOT check for matching decls pushed separately at this
10905 point, as they may not represent instantiations of this
10906 template, and in any case are considered separate under the
10907 discrete model. */
10908 r = copy_decl (t);
10909 DECL_USE_TEMPLATE (r) = 0;
10910 TREE_TYPE (r) = type;
10911 /* Clear out the mangled name and RTL for the instantiation. */
10912 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10913 SET_DECL_RTL (r, NULL);
10914 /* Leave DECL_INITIAL set on deleted instantiations. */
10915 if (!DECL_DELETED_FN (r))
10916 DECL_INITIAL (r) = NULL_TREE;
10917 DECL_CONTEXT (r) = ctx;
10919 /* OpenMP UDRs have the only argument a reference to the declared
10920 type. We want to diagnose if the declared type is a reference,
10921 which is invalid, but as references to references are usually
10922 quietly merged, diagnose it here. */
10923 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10925 tree argtype
10926 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10927 argtype = tsubst (argtype, args, complain, in_decl);
10928 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10929 error_at (DECL_SOURCE_LOCATION (t),
10930 "reference type %qT in "
10931 "%<#pragma omp declare reduction%>", argtype);
10932 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10933 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10934 argtype);
10937 if (member && DECL_CONV_FN_P (r))
10938 /* Type-conversion operator. Reconstruct the name, in
10939 case it's the name of one of the template's parameters. */
10940 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10942 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10943 complain, t);
10944 DECL_RESULT (r) = NULL_TREE;
10946 TREE_STATIC (r) = 0;
10947 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10948 DECL_EXTERNAL (r) = 1;
10949 /* If this is an instantiation of a function with internal
10950 linkage, we already know what object file linkage will be
10951 assigned to the instantiation. */
10952 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10953 DECL_DEFER_OUTPUT (r) = 0;
10954 DECL_CHAIN (r) = NULL_TREE;
10955 DECL_PENDING_INLINE_INFO (r) = 0;
10956 DECL_PENDING_INLINE_P (r) = 0;
10957 DECL_SAVED_TREE (r) = NULL_TREE;
10958 DECL_STRUCT_FUNCTION (r) = NULL;
10959 TREE_USED (r) = 0;
10960 /* We'll re-clone as appropriate in instantiate_template. */
10961 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10963 /* If we aren't complaining now, return on error before we register
10964 the specialization so that we'll complain eventually. */
10965 if ((complain & tf_error) == 0
10966 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10967 && !grok_op_properties (r, /*complain=*/false))
10968 RETURN (error_mark_node);
10970 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10971 this in the special friend case mentioned above where
10972 GEN_TMPL is NULL. */
10973 if (gen_tmpl)
10975 DECL_TEMPLATE_INFO (r) = build_template_info (gen_tmpl, argvec);
10976 SET_DECL_IMPLICIT_INSTANTIATION (r);
10978 tree new_r
10979 = register_specialization (r, gen_tmpl, argvec, false, hash);
10980 if (new_r != r)
10981 /* We instantiated this while substituting into
10982 the type earlier (template/friend54.C). */
10983 RETURN (new_r);
10985 /* We're not supposed to instantiate default arguments
10986 until they are called, for a template. But, for a
10987 declaration like:
10989 template <class T> void f ()
10990 { extern void g(int i = T()); }
10992 we should do the substitution when the template is
10993 instantiated. We handle the member function case in
10994 instantiate_class_template since the default arguments
10995 might refer to other members of the class. */
10996 if (!member
10997 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10998 && !uses_template_parms (argvec))
10999 tsubst_default_arguments (r, complain);
11001 else
11002 DECL_TEMPLATE_INFO (r) = NULL_TREE;
11004 /* Copy the list of befriending classes. */
11005 for (friends = &DECL_BEFRIENDING_CLASSES (r);
11006 *friends;
11007 friends = &TREE_CHAIN (*friends))
11009 *friends = copy_node (*friends);
11010 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
11011 args, complain,
11012 in_decl);
11015 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
11017 maybe_retrofit_in_chrg (r);
11018 if (DECL_CONSTRUCTOR_P (r))
11019 grok_ctor_properties (ctx, r);
11020 if (DECL_INHERITED_CTOR_BASE (r))
11021 deduce_inheriting_ctor (r);
11022 /* If this is an instantiation of a member template, clone it.
11023 If it isn't, that'll be handled by
11024 clone_constructors_and_destructors. */
11025 if (PRIMARY_TEMPLATE_P (gen_tmpl))
11026 clone_function_decl (r, /*update_method_vec_p=*/0);
11028 else if ((complain & tf_error) != 0
11029 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11030 && !grok_op_properties (r, /*complain=*/true))
11031 RETURN (error_mark_node);
11033 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
11034 SET_DECL_FRIEND_CONTEXT (r,
11035 tsubst (DECL_FRIEND_CONTEXT (t),
11036 args, complain, in_decl));
11038 /* Possibly limit visibility based on template args. */
11039 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11040 if (DECL_VISIBILITY_SPECIFIED (t))
11042 DECL_VISIBILITY_SPECIFIED (r) = 0;
11043 DECL_ATTRIBUTES (r)
11044 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11046 determine_visibility (r);
11047 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
11048 && !processing_template_decl)
11049 defaulted_late_check (r);
11051 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11052 args, complain, in_decl);
11054 break;
11056 case PARM_DECL:
11058 tree type = NULL_TREE;
11059 int i, len = 1;
11060 tree expanded_types = NULL_TREE;
11061 tree prev_r = NULL_TREE;
11062 tree first_r = NULL_TREE;
11064 if (DECL_PACK_P (t))
11066 /* If there is a local specialization that isn't a
11067 parameter pack, it means that we're doing a "simple"
11068 substitution from inside tsubst_pack_expansion. Just
11069 return the local specialization (which will be a single
11070 parm). */
11071 tree spec = retrieve_local_specialization (t);
11072 if (spec
11073 && TREE_CODE (spec) == PARM_DECL
11074 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11075 RETURN (spec);
11077 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11078 the parameters in this function parameter pack. */
11079 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11080 complain, in_decl);
11081 if (TREE_CODE (expanded_types) == TREE_VEC)
11083 len = TREE_VEC_LENGTH (expanded_types);
11085 /* Zero-length parameter packs are boring. Just substitute
11086 into the chain. */
11087 if (len == 0)
11088 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11089 TREE_CHAIN (t)));
11091 else
11093 /* All we did was update the type. Make a note of that. */
11094 type = expanded_types;
11095 expanded_types = NULL_TREE;
11099 /* Loop through all of the parameters we'll build. When T is
11100 a function parameter pack, LEN is the number of expanded
11101 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11102 r = NULL_TREE;
11103 for (i = 0; i < len; ++i)
11105 prev_r = r;
11106 r = copy_node (t);
11107 if (DECL_TEMPLATE_PARM_P (t))
11108 SET_DECL_TEMPLATE_PARM_P (r);
11110 if (expanded_types)
11111 /* We're on the Ith parameter of the function parameter
11112 pack. */
11114 /* Get the Ith type. */
11115 type = TREE_VEC_ELT (expanded_types, i);
11117 // An argument of a function parameter pack is a function
11118 // parameter pack if its type is also a pack. This can
11119 // happen when instantiating templates with other template
11120 // parameters.
11121 // DECL_PACK_P (r) = PACK_EXPANSION_P (type);
11123 /* Rename the parameter to include the index. */
11124 DECL_NAME (r)
11125 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11127 else if (!type)
11128 /* We're dealing with a normal parameter. */
11129 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11131 type = type_decays_to (type);
11132 TREE_TYPE (r) = type;
11133 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11135 if (DECL_INITIAL (r))
11137 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11138 DECL_INITIAL (r) = TREE_TYPE (r);
11139 else
11140 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11141 complain, in_decl);
11144 DECL_CONTEXT (r) = NULL_TREE;
11146 if (!DECL_TEMPLATE_PARM_P (r))
11147 DECL_ARG_TYPE (r) = type_passed_as (type);
11149 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11150 args, complain, in_decl);
11152 /* Keep track of the first new parameter we
11153 generate. That's what will be returned to the
11154 caller. */
11155 if (!first_r)
11156 first_r = r;
11158 /* Build a proper chain of parameters when substituting
11159 into a function parameter pack. */
11160 if (prev_r)
11161 DECL_CHAIN (prev_r) = r;
11164 /* If cp_unevaluated_operand is set, we're just looking for a
11165 single dummy parameter, so don't keep going. */
11166 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11167 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11168 complain, DECL_CHAIN (t));
11170 /* FIRST_R contains the start of the chain we've built. */
11171 r = first_r;
11173 break;
11175 case FIELD_DECL:
11177 tree type = NULL_TREE;
11178 tree vec = NULL_TREE;
11179 tree expanded_types = NULL_TREE;
11180 int len = 1;
11182 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11184 /* This field is a lambda capture pack. Return a TREE_VEC of
11185 the expanded fields to instantiate_class_template_1 and
11186 store them in the specializations hash table as a
11187 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11188 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11189 complain, in_decl);
11190 if (TREE_CODE (expanded_types) == TREE_VEC)
11192 len = TREE_VEC_LENGTH (expanded_types);
11193 vec = make_tree_vec (len);
11195 else
11197 /* All we did was update the type. Make a note of that. */
11198 type = expanded_types;
11199 expanded_types = NULL_TREE;
11203 for (int i = 0; i < len; ++i)
11205 r = copy_decl (t);
11206 if (expanded_types)
11208 type = TREE_VEC_ELT (expanded_types, i);
11209 DECL_NAME (r)
11210 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11212 else if (!type)
11213 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11215 if (type == error_mark_node)
11216 RETURN (error_mark_node);
11217 TREE_TYPE (r) = type;
11218 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11220 if (DECL_C_BIT_FIELD (r))
11221 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11222 non-bit-fields DECL_INITIAL is a non-static data member
11223 initializer, which gets deferred instantiation. */
11224 DECL_INITIAL (r)
11225 = tsubst_expr (DECL_INITIAL (t), args,
11226 complain, in_decl,
11227 /*integral_constant_expression_p=*/true);
11228 else if (DECL_INITIAL (t))
11230 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11231 NSDMI in perform_member_init. Still set DECL_INITIAL
11232 so that we know there is one. */
11233 DECL_INITIAL (r) = void_zero_node;
11234 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11235 retrofit_lang_decl (r);
11236 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11238 /* We don't have to set DECL_CONTEXT here; it is set by
11239 finish_member_declaration. */
11240 DECL_CHAIN (r) = NULL_TREE;
11242 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11243 args, complain, in_decl);
11245 if (vec)
11246 TREE_VEC_ELT (vec, i) = r;
11249 if (vec)
11251 r = vec;
11252 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11253 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11254 SET_ARGUMENT_PACK_ARGS (pack, vec);
11255 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11256 TREE_TYPE (pack) = tpack;
11257 register_specialization (pack, t, args, false, 0);
11260 break;
11262 case USING_DECL:
11263 /* We reach here only for member using decls. We also need to check
11264 uses_template_parms because DECL_DEPENDENT_P is not set for a
11265 using-declaration that designates a member of the current
11266 instantiation (c++/53549). */
11267 if (DECL_DEPENDENT_P (t)
11268 || uses_template_parms (USING_DECL_SCOPE (t)))
11270 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11271 complain, in_decl);
11272 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11273 r = do_class_using_decl (inst_scope, name);
11274 if (!r)
11275 r = error_mark_node;
11276 else
11278 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11279 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11282 else
11284 r = copy_node (t);
11285 DECL_CHAIN (r) = NULL_TREE;
11287 break;
11289 case TYPE_DECL:
11290 case VAR_DECL:
11292 tree argvec = NULL_TREE;
11293 tree gen_tmpl = NULL_TREE;
11294 tree spec;
11295 tree tmpl = NULL_TREE;
11296 tree ctx;
11297 tree type = NULL_TREE;
11298 bool local_p;
11300 if (TREE_TYPE (t) == error_mark_node)
11301 RETURN (error_mark_node);
11303 if (TREE_CODE (t) == TYPE_DECL
11304 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11306 /* If this is the canonical decl, we don't have to
11307 mess with instantiations, and often we can't (for
11308 typename, template type parms and such). Note that
11309 TYPE_NAME is not correct for the above test if
11310 we've copied the type for a typedef. */
11311 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11312 if (type == error_mark_node)
11313 RETURN (error_mark_node);
11314 r = TYPE_NAME (type);
11315 break;
11318 /* Check to see if we already have the specialization we
11319 need. */
11320 spec = NULL_TREE;
11321 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11323 /* T is a static data member or namespace-scope entity.
11324 We have to substitute into namespace-scope variables
11325 (even though such entities are never templates) because
11326 of cases like:
11328 template <class T> void f() { extern T t; }
11330 where the entity referenced is not known until
11331 instantiation time. */
11332 local_p = false;
11333 ctx = DECL_CONTEXT (t);
11334 if (DECL_CLASS_SCOPE_P (t))
11336 ctx = tsubst_aggr_type (ctx, args,
11337 complain,
11338 in_decl, /*entering_scope=*/1);
11339 /* If CTX is unchanged, then T is in fact the
11340 specialization we want. That situation occurs when
11341 referencing a static data member within in its own
11342 class. We can use pointer equality, rather than
11343 same_type_p, because DECL_CONTEXT is always
11344 canonical... */
11345 if (ctx == DECL_CONTEXT (t)
11346 && (TREE_CODE (t) != TYPE_DECL
11347 /* ... unless T is a member template; in which
11348 case our caller can be willing to create a
11349 specialization of that template represented
11350 by T. */
11351 || !(DECL_TI_TEMPLATE (t)
11352 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
11353 spec = t;
11356 if (!spec)
11358 tmpl = DECL_TI_TEMPLATE (t);
11359 gen_tmpl = most_general_template (tmpl);
11360 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11361 if (argvec == error_mark_node)
11362 RETURN (error_mark_node);
11363 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11364 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11367 else
11369 /* A local variable. */
11370 local_p = true;
11371 /* Subsequent calls to pushdecl will fill this in. */
11372 ctx = NULL_TREE;
11373 spec = retrieve_local_specialization (t);
11375 /* If we already have the specialization we need, there is
11376 nothing more to do. */
11377 if (spec)
11379 r = spec;
11380 break;
11383 /* Create a new node for the specialization we need. */
11384 r = copy_decl (t);
11385 if (type == NULL_TREE)
11387 if (is_typedef_decl (t))
11388 type = DECL_ORIGINAL_TYPE (t);
11389 else
11390 type = TREE_TYPE (t);
11391 if (VAR_P (t)
11392 && VAR_HAD_UNKNOWN_BOUND (t)
11393 && type != error_mark_node)
11394 type = strip_array_domain (type);
11395 type = tsubst (type, args, complain, in_decl);
11397 if (VAR_P (r))
11399 /* Even if the original location is out of scope, the
11400 newly substituted one is not. */
11401 DECL_DEAD_FOR_LOCAL (r) = 0;
11402 DECL_INITIALIZED_P (r) = 0;
11403 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11404 if (type == error_mark_node)
11405 RETURN (error_mark_node);
11406 if (TREE_CODE (type) == FUNCTION_TYPE)
11408 /* It may seem that this case cannot occur, since:
11410 typedef void f();
11411 void g() { f x; }
11413 declares a function, not a variable. However:
11415 typedef void f();
11416 template <typename T> void g() { T t; }
11417 template void g<f>();
11419 is an attempt to declare a variable with function
11420 type. */
11421 error ("variable %qD has function type",
11422 /* R is not yet sufficiently initialized, so we
11423 just use its name. */
11424 DECL_NAME (r));
11425 RETURN (error_mark_node);
11427 type = complete_type (type);
11428 /* Wait until cp_finish_decl to set this again, to handle
11429 circular dependency (template/instantiate6.C). */
11430 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11431 type = check_var_type (DECL_NAME (r), type);
11433 if (DECL_HAS_VALUE_EXPR_P (t))
11435 tree ve = DECL_VALUE_EXPR (t);
11436 ve = tsubst_expr (ve, args, complain, in_decl,
11437 /*constant_expression_p=*/false);
11438 if (REFERENCE_REF_P (ve))
11440 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11441 ve = TREE_OPERAND (ve, 0);
11443 SET_DECL_VALUE_EXPR (r, ve);
11446 else if (DECL_SELF_REFERENCE_P (t))
11447 SET_DECL_SELF_REFERENCE_P (r);
11448 TREE_TYPE (r) = type;
11449 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11450 DECL_CONTEXT (r) = ctx;
11451 /* Clear out the mangled name and RTL for the instantiation. */
11452 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11453 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11454 SET_DECL_RTL (r, NULL);
11455 /* The initializer must not be expanded until it is required;
11456 see [temp.inst]. */
11457 DECL_INITIAL (r) = NULL_TREE;
11458 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11459 SET_DECL_RTL (r, NULL);
11460 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11461 if (VAR_P (r))
11463 /* Possibly limit visibility based on template args. */
11464 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11465 if (DECL_VISIBILITY_SPECIFIED (t))
11467 DECL_VISIBILITY_SPECIFIED (r) = 0;
11468 DECL_ATTRIBUTES (r)
11469 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11471 determine_visibility (r);
11474 if (!local_p)
11476 /* A static data member declaration is always marked
11477 external when it is declared in-class, even if an
11478 initializer is present. We mimic the non-template
11479 processing here. */
11480 DECL_EXTERNAL (r) = 1;
11482 register_specialization (r, gen_tmpl, argvec, false, hash);
11484 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11485 SET_DECL_IMPLICIT_INSTANTIATION (r);
11487 else if (!cp_unevaluated_operand)
11488 register_local_specialization (r, t);
11490 DECL_CHAIN (r) = NULL_TREE;
11492 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11493 /*flags=*/0,
11494 args, complain, in_decl);
11496 /* Preserve a typedef that names a type. */
11497 if (is_typedef_decl (r))
11499 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11500 set_underlying_type (r);
11503 layout_decl (r, 0);
11505 break;
11507 default:
11508 gcc_unreachable ();
11510 #undef RETURN
11512 out:
11513 /* Restore the file and line information. */
11514 input_location = saved_loc;
11516 return r;
11519 /* Substitute into the ARG_TYPES of a function type.
11520 If END is a TREE_CHAIN, leave it and any following types
11521 un-substituted. */
11523 static tree
11524 tsubst_arg_types (tree arg_types,
11525 tree args,
11526 tree end,
11527 tsubst_flags_t complain,
11528 tree in_decl)
11530 tree remaining_arg_types;
11531 tree type = NULL_TREE;
11532 int i = 1;
11533 tree expanded_args = NULL_TREE;
11534 tree default_arg;
11536 if (!arg_types || arg_types == void_list_node || arg_types == end)
11537 return arg_types;
11539 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11540 args, end, complain, in_decl);
11541 if (remaining_arg_types == error_mark_node)
11542 return error_mark_node;
11544 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11546 /* For a pack expansion, perform substitution on the
11547 entire expression. Later on, we'll handle the arguments
11548 one-by-one. */
11549 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11550 args, complain, in_decl);
11552 if (TREE_CODE (expanded_args) == TREE_VEC)
11553 /* So that we'll spin through the parameters, one by one. */
11554 i = TREE_VEC_LENGTH (expanded_args);
11555 else
11557 /* We only partially substituted into the parameter
11558 pack. Our type is TYPE_PACK_EXPANSION. */
11559 type = expanded_args;
11560 expanded_args = NULL_TREE;
11564 while (i > 0) {
11565 --i;
11567 if (expanded_args)
11568 type = TREE_VEC_ELT (expanded_args, i);
11569 else if (!type)
11570 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11572 if (type == error_mark_node)
11573 return error_mark_node;
11574 if (VOID_TYPE_P (type))
11576 if (complain & tf_error)
11578 error ("invalid parameter type %qT", type);
11579 if (in_decl)
11580 error ("in declaration %q+D", in_decl);
11582 return error_mark_node;
11584 /* DR 657. */
11585 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11586 return error_mark_node;
11588 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11589 top-level qualifiers as required. */
11590 type = cv_unqualified (type_decays_to (type));
11592 /* We do not substitute into default arguments here. The standard
11593 mandates that they be instantiated only when needed, which is
11594 done in build_over_call. */
11595 default_arg = TREE_PURPOSE (arg_types);
11597 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11599 /* We've instantiated a template before its default arguments
11600 have been parsed. This can happen for a nested template
11601 class, and is not an error unless we require the default
11602 argument in a call of this function. */
11603 remaining_arg_types =
11604 tree_cons (default_arg, type, remaining_arg_types);
11605 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11607 else
11608 remaining_arg_types =
11609 hash_tree_cons (default_arg, type, remaining_arg_types);
11612 return remaining_arg_types;
11615 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11616 *not* handle the exception-specification for FNTYPE, because the
11617 initial substitution of explicitly provided template parameters
11618 during argument deduction forbids substitution into the
11619 exception-specification:
11621 [temp.deduct]
11623 All references in the function type of the function template to the
11624 corresponding template parameters are replaced by the specified tem-
11625 plate argument values. If a substitution in a template parameter or
11626 in the function type of the function template results in an invalid
11627 type, type deduction fails. [Note: The equivalent substitution in
11628 exception specifications is done only when the function is instanti-
11629 ated, at which point a program is ill-formed if the substitution
11630 results in an invalid type.] */
11632 static tree
11633 tsubst_function_type (tree t,
11634 tree args,
11635 tsubst_flags_t complain,
11636 tree in_decl)
11638 tree return_type;
11639 tree arg_types;
11640 tree fntype;
11642 /* The TYPE_CONTEXT is not used for function/method types. */
11643 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11645 /* Substitute the return type. */
11646 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11647 if (return_type == error_mark_node)
11648 return error_mark_node;
11649 /* DR 486 clarifies that creation of a function type with an
11650 invalid return type is a deduction failure. */
11651 if (TREE_CODE (return_type) == ARRAY_TYPE
11652 || TREE_CODE (return_type) == FUNCTION_TYPE)
11654 if (complain & tf_error)
11656 if (TREE_CODE (return_type) == ARRAY_TYPE)
11657 error ("function returning an array");
11658 else
11659 error ("function returning a function");
11661 return error_mark_node;
11663 /* And DR 657. */
11664 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11665 return error_mark_node;
11667 /* Substitute the argument types. */
11668 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11669 complain, in_decl);
11670 if (arg_types == error_mark_node)
11671 return error_mark_node;
11673 /* Construct a new type node and return it. */
11674 if (TREE_CODE (t) == FUNCTION_TYPE)
11676 fntype = build_function_type (return_type, arg_types);
11677 fntype = apply_memfn_quals (fntype,
11678 type_memfn_quals (t),
11679 type_memfn_rqual (t));
11681 else
11683 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11684 /* Don't pick up extra function qualifiers from the basetype. */
11685 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11686 if (! MAYBE_CLASS_TYPE_P (r))
11688 /* [temp.deduct]
11690 Type deduction may fail for any of the following
11691 reasons:
11693 -- Attempting to create "pointer to member of T" when T
11694 is not a class type. */
11695 if (complain & tf_error)
11696 error ("creating pointer to member function of non-class type %qT",
11698 return error_mark_node;
11701 fntype = build_method_type_directly (r, return_type,
11702 TREE_CHAIN (arg_types));
11703 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11705 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11707 return fntype;
11710 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11711 ARGS into that specification, and return the substituted
11712 specification. If there is no specification, return NULL_TREE. */
11714 static tree
11715 tsubst_exception_specification (tree fntype,
11716 tree args,
11717 tsubst_flags_t complain,
11718 tree in_decl,
11719 bool defer_ok)
11721 tree specs;
11722 tree new_specs;
11724 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11725 new_specs = NULL_TREE;
11726 if (specs && TREE_PURPOSE (specs))
11728 /* A noexcept-specifier. */
11729 tree expr = TREE_PURPOSE (specs);
11730 if (TREE_CODE (expr) == INTEGER_CST)
11731 new_specs = expr;
11732 else if (defer_ok)
11734 /* Defer instantiation of noexcept-specifiers to avoid
11735 excessive instantiations (c++/49107). */
11736 new_specs = make_node (DEFERRED_NOEXCEPT);
11737 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11739 /* We already partially instantiated this member template,
11740 so combine the new args with the old. */
11741 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11742 = DEFERRED_NOEXCEPT_PATTERN (expr);
11743 DEFERRED_NOEXCEPT_ARGS (new_specs)
11744 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11746 else
11748 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11749 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11752 else
11753 new_specs = tsubst_copy_and_build
11754 (expr, args, complain, in_decl, /*function_p=*/false,
11755 /*integral_constant_expression_p=*/true);
11756 new_specs = build_noexcept_spec (new_specs, complain);
11758 else if (specs)
11760 if (! TREE_VALUE (specs))
11761 new_specs = specs;
11762 else
11763 while (specs)
11765 tree spec;
11766 int i, len = 1;
11767 tree expanded_specs = NULL_TREE;
11769 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11771 /* Expand the pack expansion type. */
11772 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11773 args, complain,
11774 in_decl);
11776 if (expanded_specs == error_mark_node)
11777 return error_mark_node;
11778 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11779 len = TREE_VEC_LENGTH (expanded_specs);
11780 else
11782 /* We're substituting into a member template, so
11783 we got a TYPE_PACK_EXPANSION back. Add that
11784 expansion and move on. */
11785 gcc_assert (TREE_CODE (expanded_specs)
11786 == TYPE_PACK_EXPANSION);
11787 new_specs = add_exception_specifier (new_specs,
11788 expanded_specs,
11789 complain);
11790 specs = TREE_CHAIN (specs);
11791 continue;
11795 for (i = 0; i < len; ++i)
11797 if (expanded_specs)
11798 spec = TREE_VEC_ELT (expanded_specs, i);
11799 else
11800 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11801 if (spec == error_mark_node)
11802 return spec;
11803 new_specs = add_exception_specifier (new_specs, spec,
11804 complain);
11807 specs = TREE_CHAIN (specs);
11810 return new_specs;
11813 /* Take the tree structure T and replace template parameters used
11814 therein with the argument vector ARGS. IN_DECL is an associated
11815 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11816 Issue error and warning messages under control of COMPLAIN. Note
11817 that we must be relatively non-tolerant of extensions here, in
11818 order to preserve conformance; if we allow substitutions that
11819 should not be allowed, we may allow argument deductions that should
11820 not succeed, and therefore report ambiguous overload situations
11821 where there are none. In theory, we could allow the substitution,
11822 but indicate that it should have failed, and allow our caller to
11823 make sure that the right thing happens, but we don't try to do this
11824 yet.
11826 This function is used for dealing with types, decls and the like;
11827 for expressions, use tsubst_expr or tsubst_copy. */
11829 tree
11830 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11832 enum tree_code code;
11833 tree type, r = NULL_TREE;
11835 if (t == NULL_TREE || t == error_mark_node
11836 || t == integer_type_node
11837 || t == void_type_node
11838 || t == char_type_node
11839 || t == unknown_type_node
11840 || TREE_CODE (t) == NAMESPACE_DECL
11841 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11842 return t;
11844 if (DECL_P (t))
11845 return tsubst_decl (t, args, complain);
11847 if (args == NULL_TREE)
11848 return t;
11850 code = TREE_CODE (t);
11852 if (code == IDENTIFIER_NODE)
11853 type = IDENTIFIER_TYPE_VALUE (t);
11854 else
11855 type = TREE_TYPE (t);
11857 gcc_assert (type != unknown_type_node);
11859 /* Reuse typedefs. We need to do this to handle dependent attributes,
11860 such as attribute aligned. */
11861 if (TYPE_P (t)
11862 && typedef_variant_p (t))
11864 tree decl = TYPE_NAME (t);
11866 if (alias_template_specialization_p (t))
11868 /* DECL represents an alias template and we want to
11869 instantiate it. */
11870 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11871 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11872 r = instantiate_alias_template (tmpl, gen_args, complain);
11874 else if (DECL_CLASS_SCOPE_P (decl)
11875 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11876 && uses_template_parms (DECL_CONTEXT (decl)))
11878 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11879 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11880 r = retrieve_specialization (tmpl, gen_args, 0);
11882 else if (DECL_FUNCTION_SCOPE_P (decl)
11883 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11884 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11885 r = retrieve_local_specialization (decl);
11886 else
11887 /* The typedef is from a non-template context. */
11888 return t;
11890 if (r)
11892 r = TREE_TYPE (r);
11893 r = cp_build_qualified_type_real
11894 (r, cp_type_quals (t) | cp_type_quals (r),
11895 complain | tf_ignore_bad_quals);
11896 return r;
11898 else
11900 /* We don't have an instantiation yet, so drop the typedef. */
11901 int quals = cp_type_quals (t);
11902 t = DECL_ORIGINAL_TYPE (decl);
11903 t = cp_build_qualified_type_real (t, quals,
11904 complain | tf_ignore_bad_quals);
11908 if (type
11909 && code != TYPENAME_TYPE
11910 && code != TEMPLATE_TYPE_PARM
11911 && code != IDENTIFIER_NODE
11912 && code != FUNCTION_TYPE
11913 && code != METHOD_TYPE)
11914 type = tsubst (type, args, complain, in_decl);
11915 if (type == error_mark_node)
11916 return error_mark_node;
11918 switch (code)
11920 case RECORD_TYPE:
11921 case UNION_TYPE:
11922 case ENUMERAL_TYPE:
11923 return tsubst_aggr_type (t, args, complain, in_decl,
11924 /*entering_scope=*/0);
11926 case ERROR_MARK:
11927 case IDENTIFIER_NODE:
11928 case VOID_TYPE:
11929 case REAL_TYPE:
11930 case COMPLEX_TYPE:
11931 case VECTOR_TYPE:
11932 case BOOLEAN_TYPE:
11933 case NULLPTR_TYPE:
11934 case LANG_TYPE:
11935 return t;
11937 case INTEGER_TYPE:
11938 if (t == integer_type_node)
11939 return t;
11941 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11942 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11943 return t;
11946 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11948 max = tsubst_expr (omax, args, complain, in_decl,
11949 /*integral_constant_expression_p=*/false);
11951 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11952 needed. */
11953 if (TREE_CODE (max) == NOP_EXPR
11954 && TREE_SIDE_EFFECTS (omax)
11955 && !TREE_TYPE (max))
11956 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11958 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11959 with TREE_SIDE_EFFECTS that indicates this is not an integral
11960 constant expression. */
11961 if (processing_template_decl
11962 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11964 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11965 TREE_SIDE_EFFECTS (max) = 1;
11968 return compute_array_index_type (NULL_TREE, max, complain);
11971 case TEMPLATE_TYPE_PARM:
11972 case TEMPLATE_TEMPLATE_PARM:
11973 case BOUND_TEMPLATE_TEMPLATE_PARM:
11974 case TEMPLATE_PARM_INDEX:
11976 int idx;
11977 int level;
11978 int levels;
11979 tree arg = NULL_TREE;
11981 r = NULL_TREE;
11983 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11984 template_parm_level_and_index (t, &level, &idx);
11986 levels = TMPL_ARGS_DEPTH (args);
11987 if (level <= levels)
11989 arg = TMPL_ARG (args, level, idx);
11991 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11993 /* See through ARGUMENT_PACK_SELECT arguments. */
11994 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11995 /* If the selected argument is an expansion E, that most
11996 likely means we were called from
11997 gen_elem_of_pack_expansion_instantiation during the
11998 substituting of pack an argument pack (which Ith
11999 element is a pack expansion, where I is
12000 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12001 In this case, the Ith element resulting from this
12002 substituting is going to be a pack expansion, which
12003 pattern is the pattern of E. Let's return the
12004 pattern of E, and
12005 gen_elem_of_pack_expansion_instantiation will
12006 build the resulting pack expansion from it. */
12007 if (PACK_EXPANSION_P (arg))
12008 arg = PACK_EXPANSION_PATTERN (arg);
12012 if (arg == error_mark_node)
12013 return error_mark_node;
12014 else if (arg != NULL_TREE)
12016 if (ARGUMENT_PACK_P (arg))
12017 /* If ARG is an argument pack, we don't actually want to
12018 perform a substitution here, because substitutions
12019 for argument packs are only done
12020 element-by-element. We can get to this point when
12021 substituting the type of a non-type template
12022 parameter pack, when that type actually contains
12023 template parameter packs from an outer template, e.g.,
12025 template<typename... Types> struct A {
12026 template<Types... Values> struct B { };
12027 }; */
12028 return t;
12030 if (code == TEMPLATE_TYPE_PARM)
12032 int quals;
12033 gcc_assert (TYPE_P (arg));
12035 quals = cp_type_quals (arg) | cp_type_quals (t);
12037 return cp_build_qualified_type_real
12038 (arg, quals, complain | tf_ignore_bad_quals);
12040 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12042 /* We are processing a type constructed from a
12043 template template parameter. */
12044 tree argvec = tsubst (TYPE_TI_ARGS (t),
12045 args, complain, in_decl);
12046 if (argvec == error_mark_node)
12047 return error_mark_node;
12049 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12050 || TREE_CODE (arg) == TEMPLATE_DECL
12051 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12053 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12054 /* Consider this code:
12056 template <template <class> class Template>
12057 struct Internal {
12058 template <class Arg> using Bind = Template<Arg>;
12061 template <template <class> class Template, class Arg>
12062 using Instantiate = Template<Arg>; //#0
12064 template <template <class> class Template,
12065 class Argument>
12066 using Bind =
12067 Instantiate<Internal<Template>::template Bind,
12068 Argument>; //#1
12070 When #1 is parsed, the
12071 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12072 parameter `Template' in #0 matches the
12073 UNBOUND_CLASS_TEMPLATE representing the argument
12074 `Internal<Template>::template Bind'; We then want
12075 to assemble the type `Bind<Argument>' that can't
12076 be fully created right now, because
12077 `Internal<Template>' not being complete, the Bind
12078 template cannot be looked up in that context. So
12079 we need to "store" `Bind<Argument>' for later
12080 when the context of Bind becomes complete. Let's
12081 store that in a TYPENAME_TYPE. */
12082 return make_typename_type (TYPE_CONTEXT (arg),
12083 build_nt (TEMPLATE_ID_EXPR,
12084 TYPE_IDENTIFIER (arg),
12085 argvec),
12086 typename_type,
12087 complain);
12089 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12090 are resolving nested-types in the signature of a
12091 member function templates. Otherwise ARG is a
12092 TEMPLATE_DECL and is the real template to be
12093 instantiated. */
12094 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12095 arg = TYPE_NAME (arg);
12097 r = lookup_template_class (arg,
12098 argvec, in_decl,
12099 DECL_CONTEXT (arg),
12100 /*entering_scope=*/0,
12101 complain);
12102 return cp_build_qualified_type_real
12103 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12105 else
12106 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12107 return convert_from_reference (unshare_expr (arg));
12110 if (level == 1)
12111 /* This can happen during the attempted tsubst'ing in
12112 unify. This means that we don't yet have any information
12113 about the template parameter in question. */
12114 return t;
12116 /* Early in template argument deduction substitution, we don't
12117 want to reduce the level of 'auto', or it will be confused
12118 with a normal template parm in subsequent deduction. */
12119 if (is_auto (t) && (complain & tf_partial))
12120 return t;
12122 /* If we get here, we must have been looking at a parm for a
12123 more deeply nested template. Make a new version of this
12124 template parameter, but with a lower level. */
12125 switch (code)
12127 case TEMPLATE_TYPE_PARM:
12128 case TEMPLATE_TEMPLATE_PARM:
12129 case BOUND_TEMPLATE_TEMPLATE_PARM:
12130 if (cp_type_quals (t))
12132 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12133 r = cp_build_qualified_type_real
12134 (r, cp_type_quals (t),
12135 complain | (code == TEMPLATE_TYPE_PARM
12136 ? tf_ignore_bad_quals : 0));
12138 else
12140 r = copy_type (t);
12141 TEMPLATE_TYPE_PARM_INDEX (r)
12142 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12143 r, levels, args, complain);
12144 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12145 TYPE_MAIN_VARIANT (r) = r;
12146 TYPE_POINTER_TO (r) = NULL_TREE;
12147 TYPE_REFERENCE_TO (r) = NULL_TREE;
12149 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12150 /* We have reduced the level of the template
12151 template parameter, but not the levels of its
12152 template parameters, so canonical_type_parameter
12153 will not be able to find the canonical template
12154 template parameter for this level. Thus, we
12155 require structural equality checking to compare
12156 TEMPLATE_TEMPLATE_PARMs. */
12157 SET_TYPE_STRUCTURAL_EQUALITY (r);
12158 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12159 SET_TYPE_STRUCTURAL_EQUALITY (r);
12160 else
12161 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12163 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12165 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12166 complain, in_decl);
12167 if (argvec == error_mark_node)
12168 return error_mark_node;
12170 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12171 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12174 break;
12176 case TEMPLATE_PARM_INDEX:
12177 r = reduce_template_parm_level (t, type, levels, args, complain);
12178 break;
12180 default:
12181 gcc_unreachable ();
12184 return r;
12187 case TREE_LIST:
12189 tree purpose, value, chain;
12191 if (t == void_list_node)
12192 return t;
12194 purpose = TREE_PURPOSE (t);
12195 if (purpose)
12197 purpose = tsubst (purpose, args, complain, in_decl);
12198 if (purpose == error_mark_node)
12199 return error_mark_node;
12201 value = TREE_VALUE (t);
12202 if (value)
12204 value = tsubst (value, args, complain, in_decl);
12205 if (value == error_mark_node)
12206 return error_mark_node;
12208 chain = TREE_CHAIN (t);
12209 if (chain && chain != void_type_node)
12211 chain = tsubst (chain, args, complain, in_decl);
12212 if (chain == error_mark_node)
12213 return error_mark_node;
12215 if (purpose == TREE_PURPOSE (t)
12216 && value == TREE_VALUE (t)
12217 && chain == TREE_CHAIN (t))
12218 return t;
12219 return hash_tree_cons (purpose, value, chain);
12222 case TREE_BINFO:
12223 /* We should never be tsubsting a binfo. */
12224 gcc_unreachable ();
12226 case TREE_VEC:
12227 /* A vector of template arguments. */
12228 gcc_assert (!type);
12229 return tsubst_template_args (t, args, complain, in_decl);
12231 case POINTER_TYPE:
12232 case REFERENCE_TYPE:
12234 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12235 return t;
12237 /* [temp.deduct]
12239 Type deduction may fail for any of the following
12240 reasons:
12242 -- Attempting to create a pointer to reference type.
12243 -- Attempting to create a reference to a reference type or
12244 a reference to void.
12246 Core issue 106 says that creating a reference to a reference
12247 during instantiation is no longer a cause for failure. We
12248 only enforce this check in strict C++98 mode. */
12249 if ((TREE_CODE (type) == REFERENCE_TYPE
12250 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12251 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12253 static location_t last_loc;
12255 /* We keep track of the last time we issued this error
12256 message to avoid spewing a ton of messages during a
12257 single bad template instantiation. */
12258 if (complain & tf_error
12259 && last_loc != input_location)
12261 if (VOID_TYPE_P (type))
12262 error ("forming reference to void");
12263 else if (code == POINTER_TYPE)
12264 error ("forming pointer to reference type %qT", type);
12265 else
12266 error ("forming reference to reference type %qT", type);
12267 last_loc = input_location;
12270 return error_mark_node;
12272 else if (TREE_CODE (type) == FUNCTION_TYPE
12273 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12274 || type_memfn_rqual (type) != REF_QUAL_NONE))
12276 if (complain & tf_error)
12278 if (code == POINTER_TYPE)
12279 error ("forming pointer to qualified function type %qT",
12280 type);
12281 else
12282 error ("forming reference to qualified function type %qT",
12283 type);
12285 return error_mark_node;
12287 else if (code == POINTER_TYPE)
12289 r = build_pointer_type (type);
12290 if (TREE_CODE (type) == METHOD_TYPE)
12291 r = build_ptrmemfunc_type (r);
12293 else if (TREE_CODE (type) == REFERENCE_TYPE)
12294 /* In C++0x, during template argument substitution, when there is an
12295 attempt to create a reference to a reference type, reference
12296 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12298 "If a template-argument for a template-parameter T names a type
12299 that is a reference to a type A, an attempt to create the type
12300 'lvalue reference to cv T' creates the type 'lvalue reference to
12301 A,' while an attempt to create the type type rvalue reference to
12302 cv T' creates the type T"
12304 r = cp_build_reference_type
12305 (TREE_TYPE (type),
12306 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12307 else
12308 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12309 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12311 if (cxx_dialect >= cxx1y
12312 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
12313 && array_of_runtime_bound_p (type)
12314 && (flag_iso || warn_vla > 0))
12316 if (complain & tf_warning_or_error)
12317 pedwarn
12318 (input_location, OPT_Wvla,
12319 code == REFERENCE_TYPE
12320 ? G_("cannot declare reference to array of runtime bound")
12321 : G_("cannot declare pointer to array of runtime bound"));
12322 else
12323 r = error_mark_node;
12326 if (r != error_mark_node)
12327 /* Will this ever be needed for TYPE_..._TO values? */
12328 layout_type (r);
12330 return r;
12332 case OFFSET_TYPE:
12334 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12335 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12337 /* [temp.deduct]
12339 Type deduction may fail for any of the following
12340 reasons:
12342 -- Attempting to create "pointer to member of T" when T
12343 is not a class type. */
12344 if (complain & tf_error)
12345 error ("creating pointer to member of non-class type %qT", r);
12346 return error_mark_node;
12348 if (TREE_CODE (type) == REFERENCE_TYPE)
12350 if (complain & tf_error)
12351 error ("creating pointer to member reference type %qT", type);
12352 return error_mark_node;
12354 if (VOID_TYPE_P (type))
12356 if (complain & tf_error)
12357 error ("creating pointer to member of type void");
12358 return error_mark_node;
12360 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12361 if (TREE_CODE (type) == FUNCTION_TYPE)
12363 /* The type of the implicit object parameter gets its
12364 cv-qualifiers from the FUNCTION_TYPE. */
12365 tree memptr;
12366 tree method_type
12367 = build_memfn_type (type, r, type_memfn_quals (type),
12368 type_memfn_rqual (type));
12369 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12370 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12371 complain);
12373 else
12374 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12375 cp_type_quals (t),
12376 complain);
12378 case FUNCTION_TYPE:
12379 case METHOD_TYPE:
12381 tree fntype;
12382 tree specs;
12383 fntype = tsubst_function_type (t, args, complain, in_decl);
12384 if (fntype == error_mark_node)
12385 return error_mark_node;
12387 /* Substitute the exception specification. */
12388 specs = tsubst_exception_specification (t, args, complain,
12389 in_decl, /*defer_ok*/true);
12390 if (specs == error_mark_node)
12391 return error_mark_node;
12392 if (specs)
12393 fntype = build_exception_variant (fntype, specs);
12394 return fntype;
12396 case ARRAY_TYPE:
12398 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12399 if (domain == error_mark_node)
12400 return error_mark_node;
12402 /* As an optimization, we avoid regenerating the array type if
12403 it will obviously be the same as T. */
12404 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12405 return t;
12407 /* These checks should match the ones in grokdeclarator.
12409 [temp.deduct]
12411 The deduction may fail for any of the following reasons:
12413 -- Attempting to create an array with an element type that
12414 is void, a function type, or a reference type, or [DR337]
12415 an abstract class type. */
12416 if (VOID_TYPE_P (type)
12417 || TREE_CODE (type) == FUNCTION_TYPE
12418 || TREE_CODE (type) == REFERENCE_TYPE)
12420 if (complain & tf_error)
12421 error ("creating array of %qT", type);
12422 return error_mark_node;
12425 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12426 return error_mark_node;
12428 r = build_cplus_array_type (type, domain);
12430 if (TYPE_USER_ALIGN (t))
12432 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12433 TYPE_USER_ALIGN (r) = 1;
12436 return r;
12439 case TYPENAME_TYPE:
12441 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12442 in_decl, /*entering_scope=*/1);
12443 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12444 complain, in_decl);
12446 if (ctx == error_mark_node || f == error_mark_node)
12447 return error_mark_node;
12449 if (!MAYBE_CLASS_TYPE_P (ctx))
12451 if (complain & tf_error)
12452 error ("%qT is not a class, struct, or union type", ctx);
12453 return error_mark_node;
12455 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12457 /* Normally, make_typename_type does not require that the CTX
12458 have complete type in order to allow things like:
12460 template <class T> struct S { typename S<T>::X Y; };
12462 But, such constructs have already been resolved by this
12463 point, so here CTX really should have complete type, unless
12464 it's a partial instantiation. */
12465 ctx = complete_type (ctx);
12466 if (!COMPLETE_TYPE_P (ctx))
12468 if (complain & tf_error)
12469 cxx_incomplete_type_error (NULL_TREE, ctx);
12470 return error_mark_node;
12474 f = make_typename_type (ctx, f, typename_type,
12475 complain | tf_keep_type_decl);
12476 if (f == error_mark_node)
12477 return f;
12478 if (TREE_CODE (f) == TYPE_DECL)
12480 complain |= tf_ignore_bad_quals;
12481 f = TREE_TYPE (f);
12484 if (TREE_CODE (f) != TYPENAME_TYPE)
12486 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12488 if (complain & tf_error)
12489 error ("%qT resolves to %qT, which is not an enumeration type",
12490 t, f);
12491 else
12492 return error_mark_node;
12494 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12496 if (complain & tf_error)
12497 error ("%qT resolves to %qT, which is is not a class type",
12498 t, f);
12499 else
12500 return error_mark_node;
12504 return cp_build_qualified_type_real
12505 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12508 case UNBOUND_CLASS_TEMPLATE:
12510 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12511 in_decl, /*entering_scope=*/1);
12512 tree name = TYPE_IDENTIFIER (t);
12513 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12515 if (ctx == error_mark_node || name == error_mark_node)
12516 return error_mark_node;
12518 if (parm_list)
12519 parm_list = tsubst_template_parms (parm_list, args, complain);
12520 return make_unbound_class_template (ctx, name, parm_list, complain);
12523 case TYPEOF_TYPE:
12525 tree type;
12527 ++cp_unevaluated_operand;
12528 ++c_inhibit_evaluation_warnings;
12530 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12531 complain, in_decl,
12532 /*integral_constant_expression_p=*/false);
12534 --cp_unevaluated_operand;
12535 --c_inhibit_evaluation_warnings;
12537 type = finish_typeof (type);
12538 return cp_build_qualified_type_real (type,
12539 cp_type_quals (t)
12540 | cp_type_quals (type),
12541 complain);
12544 case DECLTYPE_TYPE:
12546 tree type;
12548 ++cp_unevaluated_operand;
12549 ++c_inhibit_evaluation_warnings;
12551 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12552 complain|tf_decltype, in_decl,
12553 /*function_p*/false,
12554 /*integral_constant_expression*/false);
12556 --cp_unevaluated_operand;
12557 --c_inhibit_evaluation_warnings;
12559 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12560 type = lambda_capture_field_type (type,
12561 DECLTYPE_FOR_INIT_CAPTURE (t));
12562 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12563 type = lambda_proxy_type (type);
12564 else
12566 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12567 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12568 && EXPR_P (type))
12569 /* In a template ~id could be either a complement expression
12570 or an unqualified-id naming a destructor; if instantiating
12571 it produces an expression, it's not an id-expression or
12572 member access. */
12573 id = false;
12574 type = finish_decltype_type (type, id, complain);
12576 return cp_build_qualified_type_real (type,
12577 cp_type_quals (t)
12578 | cp_type_quals (type),
12579 complain);
12582 case UNDERLYING_TYPE:
12584 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12585 complain, in_decl);
12586 return finish_underlying_type (type);
12589 case TYPE_ARGUMENT_PACK:
12590 case NONTYPE_ARGUMENT_PACK:
12592 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12593 tree packed_out =
12594 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12595 args,
12596 complain,
12597 in_decl);
12598 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12600 /* For template nontype argument packs, also substitute into
12601 the type. */
12602 if (code == NONTYPE_ARGUMENT_PACK)
12603 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12605 return r;
12607 break;
12609 case INTEGER_CST:
12610 case REAL_CST:
12611 case STRING_CST:
12612 case PLUS_EXPR:
12613 case MINUS_EXPR:
12614 case NEGATE_EXPR:
12615 case NOP_EXPR:
12616 case INDIRECT_REF:
12617 case ADDR_EXPR:
12618 case CALL_EXPR:
12619 case ARRAY_REF:
12620 case SCOPE_REF:
12621 /* We should use one of the expression tsubsts for these codes. */
12622 gcc_unreachable ();
12624 default:
12625 sorry ("use of %qs in template", get_tree_code_name (code));
12626 return error_mark_node;
12630 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12631 type of the expression on the left-hand side of the "." or "->"
12632 operator. */
12634 static tree
12635 tsubst_baselink (tree baselink, tree object_type,
12636 tree args, tsubst_flags_t complain, tree in_decl)
12638 tree name;
12639 tree qualifying_scope;
12640 tree fns;
12641 tree optype;
12642 tree template_args = 0;
12643 bool template_id_p = false;
12644 bool qualified = BASELINK_QUALIFIED_P (baselink);
12646 /* A baselink indicates a function from a base class. Both the
12647 BASELINK_ACCESS_BINFO and the base class referenced may
12648 indicate bases of the template class, rather than the
12649 instantiated class. In addition, lookups that were not
12650 ambiguous before may be ambiguous now. Therefore, we perform
12651 the lookup again. */
12652 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12653 qualifying_scope = tsubst (qualifying_scope, args,
12654 complain, in_decl);
12655 fns = BASELINK_FUNCTIONS (baselink);
12656 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12657 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12659 template_id_p = true;
12660 template_args = TREE_OPERAND (fns, 1);
12661 fns = TREE_OPERAND (fns, 0);
12662 if (template_args)
12663 template_args = tsubst_template_args (template_args, args,
12664 complain, in_decl);
12666 name = DECL_NAME (get_first_fn (fns));
12667 if (IDENTIFIER_TYPENAME_P (name))
12668 name = mangle_conv_op_name_for_type (optype);
12669 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12670 if (!baselink)
12671 return error_mark_node;
12673 /* If lookup found a single function, mark it as used at this
12674 point. (If it lookup found multiple functions the one selected
12675 later by overload resolution will be marked as used at that
12676 point.) */
12677 if (BASELINK_P (baselink))
12678 fns = BASELINK_FUNCTIONS (baselink);
12679 if (!template_id_p && !really_overloaded_fn (fns))
12680 mark_used (OVL_CURRENT (fns));
12682 /* Add back the template arguments, if present. */
12683 if (BASELINK_P (baselink) && template_id_p)
12684 BASELINK_FUNCTIONS (baselink)
12685 = build_nt (TEMPLATE_ID_EXPR,
12686 BASELINK_FUNCTIONS (baselink),
12687 template_args);
12688 /* Update the conversion operator type. */
12689 BASELINK_OPTYPE (baselink) = optype;
12691 if (!object_type)
12692 object_type = current_class_type;
12694 if (qualified)
12695 baselink = adjust_result_of_qualified_name_lookup (baselink,
12696 qualifying_scope,
12697 object_type);
12698 return baselink;
12701 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12702 true if the qualified-id will be a postfix-expression in-and-of
12703 itself; false if more of the postfix-expression follows the
12704 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12705 of "&". */
12707 static tree
12708 tsubst_qualified_id (tree qualified_id, tree args,
12709 tsubst_flags_t complain, tree in_decl,
12710 bool done, bool address_p)
12712 tree expr;
12713 tree scope;
12714 tree name;
12715 bool is_template;
12716 tree template_args;
12717 location_t loc = UNKNOWN_LOCATION;
12719 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12721 /* Figure out what name to look up. */
12722 name = TREE_OPERAND (qualified_id, 1);
12723 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12725 is_template = true;
12726 loc = EXPR_LOCATION (name);
12727 template_args = TREE_OPERAND (name, 1);
12728 if (template_args)
12729 template_args = tsubst_template_args (template_args, args,
12730 complain, in_decl);
12731 name = TREE_OPERAND (name, 0);
12733 else
12735 is_template = false;
12736 template_args = NULL_TREE;
12739 /* Substitute into the qualifying scope. When there are no ARGS, we
12740 are just trying to simplify a non-dependent expression. In that
12741 case the qualifying scope may be dependent, and, in any case,
12742 substituting will not help. */
12743 scope = TREE_OPERAND (qualified_id, 0);
12744 if (args)
12746 scope = tsubst (scope, args, complain, in_decl);
12747 expr = tsubst_copy (name, args, complain, in_decl);
12749 else
12750 expr = name;
12752 if (dependent_scope_p (scope))
12754 if (is_template)
12755 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12756 return build_qualified_name (NULL_TREE, scope, expr,
12757 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12760 if (!BASELINK_P (name) && !DECL_P (expr))
12762 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12764 /* A BIT_NOT_EXPR is used to represent a destructor. */
12765 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12767 error ("qualifying type %qT does not match destructor name ~%qT",
12768 scope, TREE_OPERAND (expr, 0));
12769 expr = error_mark_node;
12771 else
12772 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12773 /*is_type_p=*/0, false);
12775 else
12776 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12777 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12778 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12780 if (complain & tf_error)
12782 error ("dependent-name %qE is parsed as a non-type, but "
12783 "instantiation yields a type", qualified_id);
12784 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12786 return error_mark_node;
12790 if (DECL_P (expr))
12792 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12793 scope);
12794 /* Remember that there was a reference to this entity. */
12795 mark_used (expr);
12798 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12800 if (complain & tf_error)
12801 qualified_name_lookup_error (scope,
12802 TREE_OPERAND (qualified_id, 1),
12803 expr, input_location);
12804 return error_mark_node;
12807 if (is_template)
12808 expr = lookup_template_function (expr, template_args);
12810 if (expr == error_mark_node && complain & tf_error)
12811 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12812 expr, input_location);
12813 else if (TYPE_P (scope))
12815 expr = (adjust_result_of_qualified_name_lookup
12816 (expr, scope, current_nonlambda_class_type ()));
12817 expr = (finish_qualified_id_expr
12818 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12819 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12820 /*template_arg_p=*/false, complain));
12823 /* Expressions do not generally have reference type. */
12824 if (TREE_CODE (expr) != SCOPE_REF
12825 /* However, if we're about to form a pointer-to-member, we just
12826 want the referenced member referenced. */
12827 && TREE_CODE (expr) != OFFSET_REF)
12828 expr = convert_from_reference (expr);
12830 return expr;
12833 /* Like tsubst, but deals with expressions. This function just replaces
12834 template parms; to finish processing the resultant expression, use
12835 tsubst_copy_and_build or tsubst_expr. */
12837 static tree
12838 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12840 enum tree_code code;
12841 tree r;
12843 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12844 return t;
12846 code = TREE_CODE (t);
12848 switch (code)
12850 case PARM_DECL:
12851 r = retrieve_local_specialization (t);
12853 if (r == NULL_TREE)
12855 /* We get here for a use of 'this' in an NSDMI. */
12856 if (DECL_NAME (t) == this_identifier
12857 && at_function_scope_p ()
12858 && DECL_CONSTRUCTOR_P (current_function_decl))
12859 return current_class_ptr;
12861 /* This can happen for a parameter name used later in a function
12862 declaration (such as in a late-specified return type). Just
12863 make a dummy decl, since it's only used for its type. */
12864 gcc_assert (cp_unevaluated_operand != 0);
12865 r = tsubst_decl (t, args, complain);
12866 /* Give it the template pattern as its context; its true context
12867 hasn't been instantiated yet and this is good enough for
12868 mangling. */
12869 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12872 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12873 r = ARGUMENT_PACK_SELECT_ARG (r);
12874 mark_used (r);
12875 return r;
12877 case CONST_DECL:
12879 tree enum_type;
12880 tree v;
12882 if (DECL_TEMPLATE_PARM_P (t))
12883 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12884 /* There is no need to substitute into namespace-scope
12885 enumerators. */
12886 if (DECL_NAMESPACE_SCOPE_P (t))
12887 return t;
12888 /* If ARGS is NULL, then T is known to be non-dependent. */
12889 if (args == NULL_TREE)
12890 return integral_constant_value (t);
12892 /* Unfortunately, we cannot just call lookup_name here.
12893 Consider:
12895 template <int I> int f() {
12896 enum E { a = I };
12897 struct S { void g() { E e = a; } };
12900 When we instantiate f<7>::S::g(), say, lookup_name is not
12901 clever enough to find f<7>::a. */
12902 enum_type
12903 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12904 /*entering_scope=*/0);
12906 for (v = TYPE_VALUES (enum_type);
12907 v != NULL_TREE;
12908 v = TREE_CHAIN (v))
12909 if (TREE_PURPOSE (v) == DECL_NAME (t))
12910 return TREE_VALUE (v);
12912 /* We didn't find the name. That should never happen; if
12913 name-lookup found it during preliminary parsing, we
12914 should find it again here during instantiation. */
12915 gcc_unreachable ();
12917 return t;
12919 case FIELD_DECL:
12920 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12922 /* Check for a local specialization set up by
12923 tsubst_pack_expansion. */
12924 if (tree r = retrieve_local_specialization (t))
12926 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12927 r = ARGUMENT_PACK_SELECT_ARG (r);
12928 return r;
12931 /* When retrieving a capture pack from a generic lambda, remove the
12932 lambda call op's own template argument list from ARGS. Only the
12933 template arguments active for the closure type should be used to
12934 retrieve the pack specialization. */
12935 if (LAMBDA_FUNCTION_P (current_function_decl)
12936 && (template_class_depth (DECL_CONTEXT (t))
12937 != TMPL_ARGS_DEPTH (args)))
12938 args = strip_innermost_template_args (args, 1);
12940 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12941 tsubst_decl put in the hash table. */
12942 return retrieve_specialization (t, args, 0);
12945 if (DECL_CONTEXT (t))
12947 tree ctx;
12949 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12950 /*entering_scope=*/1);
12951 if (ctx != DECL_CONTEXT (t))
12953 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12954 if (!r)
12956 if (complain & tf_error)
12957 error ("using invalid field %qD", t);
12958 return error_mark_node;
12960 return r;
12964 return t;
12966 case VAR_DECL:
12967 case FUNCTION_DECL:
12968 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12969 r = tsubst (t, args, complain, in_decl);
12970 else if (local_variable_p (t))
12972 r = retrieve_local_specialization (t);
12973 if (r == NULL_TREE)
12975 if (DECL_ANON_UNION_VAR_P (t))
12977 /* Just use name lookup to find a member alias for an
12978 anonymous union, but then add it to the hash table. */
12979 r = lookup_name (DECL_NAME (t));
12980 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12981 register_local_specialization (r, t);
12983 else
12985 /* This can happen for a variable used in a late-specified
12986 return type of a local lambda. Just make a dummy decl
12987 since it's only used for its type. */
12988 if (cp_unevaluated_operand)
12989 return tsubst_decl (t, args, complain);
12990 gcc_assert (errorcount || sorrycount);
12991 return error_mark_node;
12995 else
12996 r = t;
12997 mark_used (r);
12998 return r;
13000 case NAMESPACE_DECL:
13001 return t;
13003 case OVERLOAD:
13004 /* An OVERLOAD will always be a non-dependent overload set; an
13005 overload set from function scope will just be represented with an
13006 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13007 gcc_assert (!uses_template_parms (t));
13008 return t;
13010 case BASELINK:
13011 return tsubst_baselink (t, current_nonlambda_class_type (),
13012 args, complain, in_decl);
13014 case TEMPLATE_DECL:
13015 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13016 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13017 args, complain, in_decl);
13018 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13019 return tsubst (t, args, complain, in_decl);
13020 else if (DECL_CLASS_SCOPE_P (t)
13021 && uses_template_parms (DECL_CONTEXT (t)))
13023 /* Template template argument like the following example need
13024 special treatment:
13026 template <template <class> class TT> struct C {};
13027 template <class T> struct D {
13028 template <class U> struct E {};
13029 C<E> c; // #1
13031 D<int> d; // #2
13033 We are processing the template argument `E' in #1 for
13034 the template instantiation #2. Originally, `E' is a
13035 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13036 have to substitute this with one having context `D<int>'. */
13038 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13039 return lookup_field (context, DECL_NAME(t), 0, false);
13041 else
13042 /* Ordinary template template argument. */
13043 return t;
13045 case CAST_EXPR:
13046 case REINTERPRET_CAST_EXPR:
13047 case CONST_CAST_EXPR:
13048 case STATIC_CAST_EXPR:
13049 case DYNAMIC_CAST_EXPR:
13050 case IMPLICIT_CONV_EXPR:
13051 case CONVERT_EXPR:
13052 case NOP_EXPR:
13053 return build1
13054 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
13055 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
13057 case SIZEOF_EXPR:
13058 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13061 tree expanded, op = TREE_OPERAND (t, 0);
13062 int len = 0;
13064 if (SIZEOF_EXPR_TYPE_P (t))
13065 op = TREE_TYPE (op);
13067 ++cp_unevaluated_operand;
13068 ++c_inhibit_evaluation_warnings;
13069 /* We only want to compute the number of arguments. */
13070 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13071 --cp_unevaluated_operand;
13072 --c_inhibit_evaluation_warnings;
13074 if (TREE_CODE (expanded) == TREE_VEC)
13075 len = TREE_VEC_LENGTH (expanded);
13077 if (expanded == error_mark_node)
13078 return error_mark_node;
13079 else if (PACK_EXPANSION_P (expanded)
13080 || (TREE_CODE (expanded) == TREE_VEC
13081 && len > 0
13082 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13084 if (TREE_CODE (expanded) == TREE_VEC)
13085 expanded = TREE_VEC_ELT (expanded, len - 1);
13087 if (TYPE_P (expanded))
13088 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13089 complain & tf_error);
13090 else
13091 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13092 complain & tf_error);
13094 else
13095 return build_int_cst (size_type_node, len);
13097 if (SIZEOF_EXPR_TYPE_P (t))
13099 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13100 args, complain, in_decl);
13101 r = build1 (NOP_EXPR, r, error_mark_node);
13102 r = build1 (SIZEOF_EXPR,
13103 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13104 SIZEOF_EXPR_TYPE_P (r) = 1;
13105 return r;
13107 /* Fall through */
13109 case INDIRECT_REF:
13110 case NEGATE_EXPR:
13111 case TRUTH_NOT_EXPR:
13112 case BIT_NOT_EXPR:
13113 case ADDR_EXPR:
13114 case UNARY_PLUS_EXPR: /* Unary + */
13115 case ALIGNOF_EXPR:
13116 case AT_ENCODE_EXPR:
13117 case ARROW_EXPR:
13118 case THROW_EXPR:
13119 case TYPEID_EXPR:
13120 case REALPART_EXPR:
13121 case IMAGPART_EXPR:
13122 case PAREN_EXPR:
13123 return build1
13124 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
13125 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
13127 case COMPONENT_REF:
13129 tree object;
13130 tree name;
13132 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13133 name = TREE_OPERAND (t, 1);
13134 if (TREE_CODE (name) == BIT_NOT_EXPR)
13136 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13137 complain, in_decl);
13138 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13140 else if (TREE_CODE (name) == SCOPE_REF
13141 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
13143 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
13144 complain, in_decl);
13145 name = TREE_OPERAND (name, 1);
13146 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13147 complain, in_decl);
13148 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13149 name = build_qualified_name (/*type=*/NULL_TREE,
13150 base, name,
13151 /*template_p=*/false);
13153 else if (BASELINK_P (name))
13154 name = tsubst_baselink (name,
13155 non_reference (TREE_TYPE (object)),
13156 args, complain,
13157 in_decl);
13158 else
13159 name = tsubst_copy (name, args, complain, in_decl);
13160 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
13163 case PLUS_EXPR:
13164 case MINUS_EXPR:
13165 case MULT_EXPR:
13166 case TRUNC_DIV_EXPR:
13167 case CEIL_DIV_EXPR:
13168 case FLOOR_DIV_EXPR:
13169 case ROUND_DIV_EXPR:
13170 case EXACT_DIV_EXPR:
13171 case BIT_AND_EXPR:
13172 case BIT_IOR_EXPR:
13173 case BIT_XOR_EXPR:
13174 case TRUNC_MOD_EXPR:
13175 case FLOOR_MOD_EXPR:
13176 case TRUTH_ANDIF_EXPR:
13177 case TRUTH_ORIF_EXPR:
13178 case TRUTH_AND_EXPR:
13179 case TRUTH_OR_EXPR:
13180 case RSHIFT_EXPR:
13181 case LSHIFT_EXPR:
13182 case RROTATE_EXPR:
13183 case LROTATE_EXPR:
13184 case EQ_EXPR:
13185 case NE_EXPR:
13186 case MAX_EXPR:
13187 case MIN_EXPR:
13188 case LE_EXPR:
13189 case GE_EXPR:
13190 case LT_EXPR:
13191 case GT_EXPR:
13192 case COMPOUND_EXPR:
13193 case DOTSTAR_EXPR:
13194 case MEMBER_REF:
13195 case PREDECREMENT_EXPR:
13196 case PREINCREMENT_EXPR:
13197 case POSTDECREMENT_EXPR:
13198 case POSTINCREMENT_EXPR:
13199 return build_nt
13200 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13201 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13203 case SCOPE_REF:
13204 return build_qualified_name (/*type=*/NULL_TREE,
13205 tsubst_copy (TREE_OPERAND (t, 0),
13206 args, complain, in_decl),
13207 tsubst_copy (TREE_OPERAND (t, 1),
13208 args, complain, in_decl),
13209 QUALIFIED_NAME_IS_TEMPLATE (t));
13211 case ARRAY_REF:
13212 return build_nt
13213 (ARRAY_REF,
13214 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13215 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
13216 NULL_TREE, NULL_TREE);
13218 case CALL_EXPR:
13220 int n = VL_EXP_OPERAND_LENGTH (t);
13221 tree result = build_vl_exp (CALL_EXPR, n);
13222 int i;
13223 for (i = 0; i < n; i++)
13224 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13225 complain, in_decl);
13226 return result;
13229 case COND_EXPR:
13230 case MODOP_EXPR:
13231 case PSEUDO_DTOR_EXPR:
13232 case VEC_PERM_EXPR:
13234 r = build_nt
13235 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13236 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
13237 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
13238 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13239 return r;
13242 case NEW_EXPR:
13244 r = build_nt
13245 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13246 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
13247 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
13248 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13249 return r;
13252 case DELETE_EXPR:
13254 r = build_nt
13255 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13256 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13257 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13258 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13259 return r;
13262 case TEMPLATE_ID_EXPR:
13264 /* Substituted template arguments */
13265 tree fn = TREE_OPERAND (t, 0);
13266 tree targs = TREE_OPERAND (t, 1);
13268 fn = tsubst_copy (fn, args, complain, in_decl);
13269 if (targs)
13270 targs = tsubst_template_args (targs, args, complain, in_decl);
13272 return lookup_template_function (fn, targs);
13275 case TREE_LIST:
13277 tree purpose, value, chain;
13279 if (t == void_list_node)
13280 return t;
13282 purpose = TREE_PURPOSE (t);
13283 if (purpose)
13284 purpose = tsubst_copy (purpose, args, complain, in_decl);
13285 value = TREE_VALUE (t);
13286 if (value)
13287 value = tsubst_copy (value, args, complain, in_decl);
13288 chain = TREE_CHAIN (t);
13289 if (chain && chain != void_type_node)
13290 chain = tsubst_copy (chain, args, complain, in_decl);
13291 if (purpose == TREE_PURPOSE (t)
13292 && value == TREE_VALUE (t)
13293 && chain == TREE_CHAIN (t))
13294 return t;
13295 return tree_cons (purpose, value, chain);
13298 case RECORD_TYPE:
13299 case UNION_TYPE:
13300 case ENUMERAL_TYPE:
13301 case INTEGER_TYPE:
13302 case REAL_TYPE:
13303 case BOOLEAN_TYPE:
13304 case TEMPLATE_TYPE_PARM:
13305 case TEMPLATE_TEMPLATE_PARM:
13306 case BOUND_TEMPLATE_TEMPLATE_PARM:
13307 case TEMPLATE_PARM_INDEX:
13308 case POINTER_TYPE:
13309 case REFERENCE_TYPE:
13310 case OFFSET_TYPE:
13311 case FUNCTION_TYPE:
13312 case METHOD_TYPE:
13313 case ARRAY_TYPE:
13314 case TYPENAME_TYPE:
13315 case UNBOUND_CLASS_TEMPLATE:
13316 case TYPEOF_TYPE:
13317 case DECLTYPE_TYPE:
13318 case TYPE_DECL:
13319 return tsubst (t, args, complain, in_decl);
13321 case USING_DECL:
13322 t = DECL_NAME (t);
13323 /* Fall through. */
13324 case IDENTIFIER_NODE:
13325 if (IDENTIFIER_TYPENAME_P (t))
13327 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13328 return mangle_conv_op_name_for_type (new_type);
13330 else
13331 return t;
13333 case CONSTRUCTOR:
13334 /* This is handled by tsubst_copy_and_build. */
13335 gcc_unreachable ();
13337 case VA_ARG_EXPR:
13338 return build_x_va_arg (EXPR_LOCATION (t),
13339 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
13340 in_decl),
13341 tsubst (TREE_TYPE (t), args, complain, in_decl));
13343 case CLEANUP_POINT_EXPR:
13344 /* We shouldn't have built any of these during initial template
13345 generation. Instead, they should be built during instantiation
13346 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13347 gcc_unreachable ();
13349 case OFFSET_REF:
13350 r = build2
13351 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
13352 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13353 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13354 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13355 mark_used (TREE_OPERAND (r, 1));
13356 return r;
13358 case EXPR_PACK_EXPANSION:
13359 // Expansions of variadic constraints are viable, expanding
13360 // to a conunction of the expanded terms.
13361 if (TREE_TYPE (t) == boolean_type_node)
13362 return tsubst_pack_conjunction (t, args, complain, in_decl);
13364 error ("invalid use of pack expansion expression");
13365 return error_mark_node;
13367 case NONTYPE_ARGUMENT_PACK:
13368 error ("use %<...%> to expand argument pack");
13369 return error_mark_node;
13371 case INTEGER_CST:
13372 case REAL_CST:
13373 case STRING_CST:
13374 case COMPLEX_CST:
13376 /* Instantiate any typedefs in the type. */
13377 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13378 r = fold_convert (type, t);
13379 gcc_assert (TREE_CODE (r) == code);
13380 return r;
13383 case PTRMEM_CST:
13384 /* These can sometimes show up in a partial instantiation, but never
13385 involve template parms. */
13386 gcc_assert (!uses_template_parms (t));
13387 return t;
13389 default:
13390 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13391 gcc_checking_assert (false);
13392 return t;
13396 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13398 static tree
13399 tsubst_omp_clauses (tree clauses, bool declare_simd,
13400 tree args, tsubst_flags_t complain, tree in_decl)
13402 tree new_clauses = NULL, nc, oc;
13404 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13406 nc = copy_node (oc);
13407 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13408 new_clauses = nc;
13410 switch (OMP_CLAUSE_CODE (nc))
13412 case OMP_CLAUSE_LASTPRIVATE:
13413 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13415 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13416 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13417 in_decl, /*integral_constant_expression_p=*/false);
13418 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13419 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13421 /* FALLTHRU */
13422 case OMP_CLAUSE_PRIVATE:
13423 case OMP_CLAUSE_SHARED:
13424 case OMP_CLAUSE_FIRSTPRIVATE:
13425 case OMP_CLAUSE_COPYIN:
13426 case OMP_CLAUSE_COPYPRIVATE:
13427 case OMP_CLAUSE_IF:
13428 case OMP_CLAUSE_NUM_THREADS:
13429 case OMP_CLAUSE_SCHEDULE:
13430 case OMP_CLAUSE_COLLAPSE:
13431 case OMP_CLAUSE_FINAL:
13432 case OMP_CLAUSE_DEPEND:
13433 case OMP_CLAUSE_FROM:
13434 case OMP_CLAUSE_TO:
13435 case OMP_CLAUSE_UNIFORM:
13436 case OMP_CLAUSE_MAP:
13437 case OMP_CLAUSE_DEVICE:
13438 case OMP_CLAUSE_DIST_SCHEDULE:
13439 case OMP_CLAUSE_NUM_TEAMS:
13440 case OMP_CLAUSE_THREAD_LIMIT:
13441 case OMP_CLAUSE_SAFELEN:
13442 case OMP_CLAUSE_SIMDLEN:
13443 OMP_CLAUSE_OPERAND (nc, 0)
13444 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13445 in_decl, /*integral_constant_expression_p=*/false);
13446 break;
13447 case OMP_CLAUSE_REDUCTION:
13448 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13450 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13451 if (TREE_CODE (placeholder) == SCOPE_REF)
13453 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13454 complain, in_decl);
13455 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13456 = build_qualified_name (NULL_TREE, scope,
13457 TREE_OPERAND (placeholder, 1),
13458 false);
13460 else
13461 gcc_assert (identifier_p (placeholder));
13463 OMP_CLAUSE_OPERAND (nc, 0)
13464 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13465 in_decl, /*integral_constant_expression_p=*/false);
13466 break;
13467 case OMP_CLAUSE_LINEAR:
13468 case OMP_CLAUSE_ALIGNED:
13469 OMP_CLAUSE_OPERAND (nc, 0)
13470 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13471 in_decl, /*integral_constant_expression_p=*/false);
13472 OMP_CLAUSE_OPERAND (nc, 1)
13473 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13474 in_decl, /*integral_constant_expression_p=*/false);
13475 break;
13477 case OMP_CLAUSE_NOWAIT:
13478 case OMP_CLAUSE_ORDERED:
13479 case OMP_CLAUSE_DEFAULT:
13480 case OMP_CLAUSE_UNTIED:
13481 case OMP_CLAUSE_MERGEABLE:
13482 case OMP_CLAUSE_INBRANCH:
13483 case OMP_CLAUSE_NOTINBRANCH:
13484 case OMP_CLAUSE_PROC_BIND:
13485 case OMP_CLAUSE_FOR:
13486 case OMP_CLAUSE_PARALLEL:
13487 case OMP_CLAUSE_SECTIONS:
13488 case OMP_CLAUSE_TASKGROUP:
13489 break;
13490 default:
13491 gcc_unreachable ();
13495 new_clauses = nreverse (new_clauses);
13496 if (!declare_simd)
13497 new_clauses = finish_omp_clauses (new_clauses);
13498 return new_clauses;
13501 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13503 static tree
13504 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13505 tree in_decl)
13507 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13509 tree purpose, value, chain;
13511 if (t == NULL)
13512 return t;
13514 if (TREE_CODE (t) != TREE_LIST)
13515 return tsubst_copy_and_build (t, args, complain, in_decl,
13516 /*function_p=*/false,
13517 /*integral_constant_expression_p=*/false);
13519 if (t == void_list_node)
13520 return t;
13522 purpose = TREE_PURPOSE (t);
13523 if (purpose)
13524 purpose = RECUR (purpose);
13525 value = TREE_VALUE (t);
13526 if (value)
13528 if (TREE_CODE (value) != LABEL_DECL)
13529 value = RECUR (value);
13530 else
13532 value = lookup_label (DECL_NAME (value));
13533 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13534 TREE_USED (value) = 1;
13537 chain = TREE_CHAIN (t);
13538 if (chain && chain != void_type_node)
13539 chain = RECUR (chain);
13540 return tree_cons (purpose, value, chain);
13541 #undef RECUR
13544 /* Substitute one OMP_FOR iterator. */
13546 static void
13547 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13548 tree condv, tree incrv, tree *clauses,
13549 tree args, tsubst_flags_t complain, tree in_decl,
13550 bool integral_constant_expression_p)
13552 #define RECUR(NODE) \
13553 tsubst_expr ((NODE), args, complain, in_decl, \
13554 integral_constant_expression_p)
13555 tree decl, init, cond, incr;
13557 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13558 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13559 decl = TREE_OPERAND (init, 0);
13560 init = TREE_OPERAND (init, 1);
13561 tree decl_expr = NULL_TREE;
13562 if (init && TREE_CODE (init) == DECL_EXPR)
13564 /* We need to jump through some hoops to handle declarations in the
13565 for-init-statement, since we might need to handle auto deduction,
13566 but we need to keep control of initialization. */
13567 decl_expr = init;
13568 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13569 decl = tsubst_decl (decl, args, complain);
13571 else
13572 decl = RECUR (decl);
13573 init = RECUR (init);
13575 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13576 if (auto_node && init)
13577 TREE_TYPE (decl)
13578 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13580 gcc_assert (!type_dependent_expression_p (decl));
13582 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13584 if (decl_expr)
13586 /* Declare the variable, but don't let that initialize it. */
13587 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13588 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13589 RECUR (decl_expr);
13590 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13593 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13594 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13595 if (TREE_CODE (incr) == MODIFY_EXPR)
13596 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13597 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13598 RECUR (TREE_OPERAND (incr, 1)),
13599 complain);
13600 else
13601 incr = RECUR (incr);
13602 TREE_VEC_ELT (declv, i) = decl;
13603 TREE_VEC_ELT (initv, i) = init;
13604 TREE_VEC_ELT (condv, i) = cond;
13605 TREE_VEC_ELT (incrv, i) = incr;
13606 return;
13609 if (decl_expr)
13611 /* Declare and initialize the variable. */
13612 RECUR (decl_expr);
13613 init = NULL_TREE;
13615 else if (init)
13617 tree c;
13618 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13620 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13621 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13622 && OMP_CLAUSE_DECL (c) == decl)
13623 break;
13624 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13625 && OMP_CLAUSE_DECL (c) == decl)
13626 error ("iteration variable %qD should not be firstprivate", decl);
13627 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13628 && OMP_CLAUSE_DECL (c) == decl)
13629 error ("iteration variable %qD should not be reduction", decl);
13631 if (c == NULL)
13633 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13634 OMP_CLAUSE_DECL (c) = decl;
13635 c = finish_omp_clauses (c);
13636 if (c)
13638 OMP_CLAUSE_CHAIN (c) = *clauses;
13639 *clauses = c;
13643 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13644 if (COMPARISON_CLASS_P (cond))
13645 cond = build2 (TREE_CODE (cond), boolean_type_node,
13646 RECUR (TREE_OPERAND (cond, 0)),
13647 RECUR (TREE_OPERAND (cond, 1)));
13648 else
13649 cond = RECUR (cond);
13650 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13651 switch (TREE_CODE (incr))
13653 case PREINCREMENT_EXPR:
13654 case PREDECREMENT_EXPR:
13655 case POSTINCREMENT_EXPR:
13656 case POSTDECREMENT_EXPR:
13657 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13658 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13659 break;
13660 case MODIFY_EXPR:
13661 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13662 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13664 tree rhs = TREE_OPERAND (incr, 1);
13665 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13666 RECUR (TREE_OPERAND (incr, 0)),
13667 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13668 RECUR (TREE_OPERAND (rhs, 0)),
13669 RECUR (TREE_OPERAND (rhs, 1))));
13671 else
13672 incr = RECUR (incr);
13673 break;
13674 case MODOP_EXPR:
13675 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13676 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13678 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13679 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13680 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13681 TREE_TYPE (decl), lhs,
13682 RECUR (TREE_OPERAND (incr, 2))));
13684 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13685 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13686 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13688 tree rhs = TREE_OPERAND (incr, 2);
13689 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13690 RECUR (TREE_OPERAND (incr, 0)),
13691 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13692 RECUR (TREE_OPERAND (rhs, 0)),
13693 RECUR (TREE_OPERAND (rhs, 1))));
13695 else
13696 incr = RECUR (incr);
13697 break;
13698 default:
13699 incr = RECUR (incr);
13700 break;
13703 TREE_VEC_ELT (declv, i) = decl;
13704 TREE_VEC_ELT (initv, i) = init;
13705 TREE_VEC_ELT (condv, i) = cond;
13706 TREE_VEC_ELT (incrv, i) = incr;
13707 #undef RECUR
13710 /* Like tsubst_copy for expressions, etc. but also does semantic
13711 processing. */
13713 tree
13714 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13715 bool integral_constant_expression_p)
13717 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13718 #define RECUR(NODE) \
13719 tsubst_expr ((NODE), args, complain, in_decl, \
13720 integral_constant_expression_p)
13722 tree stmt, tmp;
13723 tree r;
13724 location_t loc;
13726 if (t == NULL_TREE || t == error_mark_node)
13727 return t;
13729 loc = input_location;
13730 if (EXPR_HAS_LOCATION (t))
13731 input_location = EXPR_LOCATION (t);
13732 if (STATEMENT_CODE_P (TREE_CODE (t)))
13733 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13735 switch (TREE_CODE (t))
13737 case STATEMENT_LIST:
13739 tree_stmt_iterator i;
13740 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13741 RECUR (tsi_stmt (i));
13742 break;
13745 case CTOR_INITIALIZER:
13746 finish_mem_initializers (tsubst_initializer_list
13747 (TREE_OPERAND (t, 0), args));
13748 break;
13750 case RETURN_EXPR:
13751 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13752 break;
13754 case EXPR_STMT:
13755 tmp = RECUR (EXPR_STMT_EXPR (t));
13756 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13757 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13758 else
13759 finish_expr_stmt (tmp);
13760 break;
13762 case USING_STMT:
13763 do_using_directive (USING_STMT_NAMESPACE (t));
13764 break;
13766 case DECL_EXPR:
13768 tree decl, pattern_decl;
13769 tree init;
13771 pattern_decl = decl = DECL_EXPR_DECL (t);
13772 if (TREE_CODE (decl) == LABEL_DECL)
13773 finish_label_decl (DECL_NAME (decl));
13774 else if (TREE_CODE (decl) == USING_DECL)
13776 tree scope = USING_DECL_SCOPE (decl);
13777 tree name = DECL_NAME (decl);
13778 tree decl;
13780 scope = tsubst (scope, args, complain, in_decl);
13781 decl = lookup_qualified_name (scope, name,
13782 /*is_type_p=*/false,
13783 /*complain=*/false);
13784 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13785 qualified_name_lookup_error (scope, name, decl, input_location);
13786 else
13787 do_local_using_decl (decl, scope, name);
13789 else if (DECL_PACK_P (decl))
13791 /* Don't build up decls for a variadic capture proxy, we'll
13792 instantiate the elements directly as needed. */
13793 break;
13795 else
13797 init = DECL_INITIAL (decl);
13798 decl = tsubst (decl, args, complain, in_decl);
13799 if (decl != error_mark_node)
13801 /* By marking the declaration as instantiated, we avoid
13802 trying to instantiate it. Since instantiate_decl can't
13803 handle local variables, and since we've already done
13804 all that needs to be done, that's the right thing to
13805 do. */
13806 if (VAR_P (decl))
13807 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13808 if (VAR_P (decl)
13809 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13810 /* Anonymous aggregates are a special case. */
13811 finish_anon_union (decl);
13812 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13814 DECL_CONTEXT (decl) = current_function_decl;
13815 if (DECL_NAME (decl) == this_identifier)
13817 tree lam = DECL_CONTEXT (current_function_decl);
13818 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13819 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13821 insert_capture_proxy (decl);
13823 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13824 /* We already did a pushtag. */;
13825 else if (TREE_CODE (decl) == FUNCTION_DECL
13826 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13827 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13829 DECL_CONTEXT (decl) = NULL_TREE;
13830 pushdecl (decl);
13831 DECL_CONTEXT (decl) = current_function_decl;
13832 cp_check_omp_declare_reduction (decl);
13834 else
13836 int const_init = false;
13837 maybe_push_decl (decl);
13838 if (VAR_P (decl)
13839 && DECL_PRETTY_FUNCTION_P (decl))
13841 /* For __PRETTY_FUNCTION__ we have to adjust the
13842 initializer. */
13843 const char *const name
13844 = cxx_printable_name (current_function_decl, 2);
13845 init = cp_fname_init (name, &TREE_TYPE (decl));
13847 else
13849 tree t = RECUR (init);
13851 if (init && !t)
13853 /* If we had an initializer but it
13854 instantiated to nothing,
13855 value-initialize the object. This will
13856 only occur when the initializer was a
13857 pack expansion where the parameter packs
13858 used in that expansion were of length
13859 zero. */
13860 init = build_value_init (TREE_TYPE (decl),
13861 complain);
13862 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13863 init = get_target_expr_sfinae (init, complain);
13865 else
13866 init = t;
13869 if (VAR_P (decl))
13870 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13871 (pattern_decl));
13872 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13877 break;
13880 case FOR_STMT:
13881 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13882 RECUR (FOR_INIT_STMT (t));
13883 finish_for_init_stmt (stmt);
13884 tmp = RECUR (FOR_COND (t));
13885 finish_for_cond (tmp, stmt, false);
13886 tmp = RECUR (FOR_EXPR (t));
13887 finish_for_expr (tmp, stmt);
13888 RECUR (FOR_BODY (t));
13889 finish_for_stmt (stmt);
13890 break;
13892 case RANGE_FOR_STMT:
13894 tree decl, expr;
13895 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13896 decl = RANGE_FOR_DECL (t);
13897 decl = tsubst (decl, args, complain, in_decl);
13898 maybe_push_decl (decl);
13899 expr = RECUR (RANGE_FOR_EXPR (t));
13900 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13901 RECUR (RANGE_FOR_BODY (t));
13902 finish_for_stmt (stmt);
13904 break;
13906 case WHILE_STMT:
13907 stmt = begin_while_stmt ();
13908 tmp = RECUR (WHILE_COND (t));
13909 finish_while_stmt_cond (tmp, stmt, false);
13910 RECUR (WHILE_BODY (t));
13911 finish_while_stmt (stmt);
13912 break;
13914 case DO_STMT:
13915 stmt = begin_do_stmt ();
13916 RECUR (DO_BODY (t));
13917 finish_do_body (stmt);
13918 tmp = RECUR (DO_COND (t));
13919 finish_do_stmt (tmp, stmt, false);
13920 break;
13922 case IF_STMT:
13923 stmt = begin_if_stmt ();
13924 tmp = RECUR (IF_COND (t));
13925 finish_if_stmt_cond (tmp, stmt);
13926 RECUR (THEN_CLAUSE (t));
13927 finish_then_clause (stmt);
13929 if (ELSE_CLAUSE (t))
13931 begin_else_clause (stmt);
13932 RECUR (ELSE_CLAUSE (t));
13933 finish_else_clause (stmt);
13936 finish_if_stmt (stmt);
13937 break;
13939 case BIND_EXPR:
13940 if (BIND_EXPR_BODY_BLOCK (t))
13941 stmt = begin_function_body ();
13942 else
13943 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13944 ? BCS_TRY_BLOCK : 0);
13946 RECUR (BIND_EXPR_BODY (t));
13948 if (BIND_EXPR_BODY_BLOCK (t))
13949 finish_function_body (stmt);
13950 else
13951 finish_compound_stmt (stmt);
13952 break;
13954 case BREAK_STMT:
13955 finish_break_stmt ();
13956 break;
13958 case CONTINUE_STMT:
13959 finish_continue_stmt ();
13960 break;
13962 case SWITCH_STMT:
13963 stmt = begin_switch_stmt ();
13964 tmp = RECUR (SWITCH_STMT_COND (t));
13965 finish_switch_cond (tmp, stmt);
13966 RECUR (SWITCH_STMT_BODY (t));
13967 finish_switch_stmt (stmt);
13968 break;
13970 case CASE_LABEL_EXPR:
13971 finish_case_label (EXPR_LOCATION (t),
13972 RECUR (CASE_LOW (t)),
13973 RECUR (CASE_HIGH (t)));
13974 break;
13976 case LABEL_EXPR:
13978 tree decl = LABEL_EXPR_LABEL (t);
13979 tree label;
13981 label = finish_label_stmt (DECL_NAME (decl));
13982 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13983 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13985 break;
13987 case GOTO_EXPR:
13988 tmp = GOTO_DESTINATION (t);
13989 if (TREE_CODE (tmp) != LABEL_DECL)
13990 /* Computed goto's must be tsubst'd into. On the other hand,
13991 non-computed gotos must not be; the identifier in question
13992 will have no binding. */
13993 tmp = RECUR (tmp);
13994 else
13995 tmp = DECL_NAME (tmp);
13996 finish_goto_stmt (tmp);
13997 break;
13999 case ASM_EXPR:
14000 tmp = finish_asm_stmt
14001 (ASM_VOLATILE_P (t),
14002 RECUR (ASM_STRING (t)),
14003 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
14004 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
14005 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
14006 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
14008 tree asm_expr = tmp;
14009 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14010 asm_expr = TREE_OPERAND (asm_expr, 0);
14011 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14013 break;
14015 case TRY_BLOCK:
14016 if (CLEANUP_P (t))
14018 stmt = begin_try_block ();
14019 RECUR (TRY_STMTS (t));
14020 finish_cleanup_try_block (stmt);
14021 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14023 else
14025 tree compound_stmt = NULL_TREE;
14027 if (FN_TRY_BLOCK_P (t))
14028 stmt = begin_function_try_block (&compound_stmt);
14029 else
14030 stmt = begin_try_block ();
14032 RECUR (TRY_STMTS (t));
14034 if (FN_TRY_BLOCK_P (t))
14035 finish_function_try_block (stmt);
14036 else
14037 finish_try_block (stmt);
14039 RECUR (TRY_HANDLERS (t));
14040 if (FN_TRY_BLOCK_P (t))
14041 finish_function_handler_sequence (stmt, compound_stmt);
14042 else
14043 finish_handler_sequence (stmt);
14045 break;
14047 case HANDLER:
14049 tree decl = HANDLER_PARMS (t);
14051 if (decl)
14053 decl = tsubst (decl, args, complain, in_decl);
14054 /* Prevent instantiate_decl from trying to instantiate
14055 this variable. We've already done all that needs to be
14056 done. */
14057 if (decl != error_mark_node)
14058 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14060 stmt = begin_handler ();
14061 finish_handler_parms (decl, stmt);
14062 RECUR (HANDLER_BODY (t));
14063 finish_handler (stmt);
14065 break;
14067 case TAG_DEFN:
14068 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14069 if (CLASS_TYPE_P (tmp))
14071 /* Local classes are not independent templates; they are
14072 instantiated along with their containing function. And this
14073 way we don't have to deal with pushing out of one local class
14074 to instantiate a member of another local class. */
14075 tree fn;
14076 /* Closures are handled by the LAMBDA_EXPR. */
14077 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14078 complete_type (tmp);
14079 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14080 if (!DECL_ARTIFICIAL (fn))
14081 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14083 break;
14085 case STATIC_ASSERT:
14087 tree condition;
14089 ++c_inhibit_evaluation_warnings;
14090 condition =
14091 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14092 args,
14093 complain, in_decl,
14094 /*integral_constant_expression_p=*/true);
14095 --c_inhibit_evaluation_warnings;
14097 finish_static_assert (condition,
14098 STATIC_ASSERT_MESSAGE (t),
14099 STATIC_ASSERT_SOURCE_LOCATION (t),
14100 /*member_p=*/false);
14102 break;
14104 case OMP_PARALLEL:
14105 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
14106 args, complain, in_decl);
14107 stmt = begin_omp_parallel ();
14108 RECUR (OMP_PARALLEL_BODY (t));
14109 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
14110 = OMP_PARALLEL_COMBINED (t);
14111 break;
14113 case OMP_TASK:
14114 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
14115 args, complain, in_decl);
14116 stmt = begin_omp_task ();
14117 RECUR (OMP_TASK_BODY (t));
14118 finish_omp_task (tmp, stmt);
14119 break;
14121 case OMP_FOR:
14122 case OMP_SIMD:
14123 case CILK_SIMD:
14124 case OMP_DISTRIBUTE:
14126 tree clauses, body, pre_body;
14127 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
14128 tree incrv = NULL_TREE;
14129 int i;
14131 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
14132 args, complain, in_decl);
14133 if (OMP_FOR_INIT (t) != NULL_TREE)
14135 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14136 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14137 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14138 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14141 stmt = begin_omp_structured_block ();
14143 pre_body = push_stmt_list ();
14144 RECUR (OMP_FOR_PRE_BODY (t));
14145 pre_body = pop_stmt_list (pre_body);
14147 if (OMP_FOR_INIT (t) != NULL_TREE)
14148 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
14149 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
14150 &clauses, args, complain, in_decl,
14151 integral_constant_expression_p);
14153 body = push_stmt_list ();
14154 RECUR (OMP_FOR_BODY (t));
14155 body = pop_stmt_list (body);
14157 if (OMP_FOR_INIT (t) != NULL_TREE)
14158 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
14159 condv, incrv, body, pre_body, clauses);
14160 else
14162 t = make_node (TREE_CODE (t));
14163 TREE_TYPE (t) = void_type_node;
14164 OMP_FOR_BODY (t) = body;
14165 OMP_FOR_PRE_BODY (t) = pre_body;
14166 OMP_FOR_CLAUSES (t) = clauses;
14167 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
14168 add_stmt (t);
14171 add_stmt (finish_omp_structured_block (stmt));
14173 break;
14175 case OMP_SECTIONS:
14176 case OMP_SINGLE:
14177 case OMP_TEAMS:
14178 case OMP_TARGET_DATA:
14179 case OMP_TARGET:
14180 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14181 args, complain, in_decl);
14182 stmt = push_stmt_list ();
14183 RECUR (OMP_BODY (t));
14184 stmt = pop_stmt_list (stmt);
14186 t = copy_node (t);
14187 OMP_BODY (t) = stmt;
14188 OMP_CLAUSES (t) = tmp;
14189 add_stmt (t);
14190 break;
14192 case OMP_TARGET_UPDATE:
14193 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14194 args, complain, in_decl);
14195 t = copy_node (t);
14196 OMP_CLAUSES (t) = tmp;
14197 add_stmt (t);
14198 break;
14200 case OMP_SECTION:
14201 case OMP_CRITICAL:
14202 case OMP_MASTER:
14203 case OMP_TASKGROUP:
14204 case OMP_ORDERED:
14205 stmt = push_stmt_list ();
14206 RECUR (OMP_BODY (t));
14207 stmt = pop_stmt_list (stmt);
14209 t = copy_node (t);
14210 OMP_BODY (t) = stmt;
14211 add_stmt (t);
14212 break;
14214 case OMP_ATOMIC:
14215 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14216 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14218 tree op1 = TREE_OPERAND (t, 1);
14219 tree rhs1 = NULL_TREE;
14220 tree lhs, rhs;
14221 if (TREE_CODE (op1) == COMPOUND_EXPR)
14223 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14224 op1 = TREE_OPERAND (op1, 1);
14226 lhs = RECUR (TREE_OPERAND (op1, 0));
14227 rhs = RECUR (TREE_OPERAND (op1, 1));
14228 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14229 NULL_TREE, NULL_TREE, rhs1,
14230 OMP_ATOMIC_SEQ_CST (t));
14232 else
14234 tree op1 = TREE_OPERAND (t, 1);
14235 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14236 tree rhs1 = NULL_TREE;
14237 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14238 enum tree_code opcode = NOP_EXPR;
14239 if (code == OMP_ATOMIC_READ)
14241 v = RECUR (TREE_OPERAND (op1, 0));
14242 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14244 else if (code == OMP_ATOMIC_CAPTURE_OLD
14245 || code == OMP_ATOMIC_CAPTURE_NEW)
14247 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14248 v = RECUR (TREE_OPERAND (op1, 0));
14249 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14250 if (TREE_CODE (op11) == COMPOUND_EXPR)
14252 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14253 op11 = TREE_OPERAND (op11, 1);
14255 lhs = RECUR (TREE_OPERAND (op11, 0));
14256 rhs = RECUR (TREE_OPERAND (op11, 1));
14257 opcode = TREE_CODE (op11);
14258 if (opcode == MODIFY_EXPR)
14259 opcode = NOP_EXPR;
14261 else
14263 code = OMP_ATOMIC;
14264 lhs = RECUR (TREE_OPERAND (op1, 0));
14265 rhs = RECUR (TREE_OPERAND (op1, 1));
14267 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14268 OMP_ATOMIC_SEQ_CST (t));
14270 break;
14272 case TRANSACTION_EXPR:
14274 int flags = 0;
14275 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14276 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14278 if (TRANSACTION_EXPR_IS_STMT (t))
14280 tree body = TRANSACTION_EXPR_BODY (t);
14281 tree noex = NULL_TREE;
14282 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14284 noex = MUST_NOT_THROW_COND (body);
14285 if (noex == NULL_TREE)
14286 noex = boolean_true_node;
14287 body = TREE_OPERAND (body, 0);
14289 stmt = begin_transaction_stmt (input_location, NULL, flags);
14290 RECUR (body);
14291 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14293 else
14295 stmt = build_transaction_expr (EXPR_LOCATION (t),
14296 RECUR (TRANSACTION_EXPR_BODY (t)),
14297 flags, NULL_TREE);
14298 RETURN (stmt);
14301 break;
14303 case MUST_NOT_THROW_EXPR:
14304 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
14305 RECUR (MUST_NOT_THROW_COND (t))));
14307 case EXPR_PACK_EXPANSION:
14308 // Expansions of variadic constraints are viable, expanding
14309 // to a conunction of the expanded terms.
14310 if (TREE_TYPE (t) == boolean_type_node)
14311 return tsubst_pack_conjunction (t, args, complain, in_decl);
14313 error ("invalid use of pack expansion expression");
14314 RETURN (error_mark_node);
14316 case NONTYPE_ARGUMENT_PACK:
14317 error ("use %<...%> to expand argument pack");
14318 RETURN (error_mark_node);
14320 case CILK_SPAWN_STMT:
14321 cfun->calls_cilk_spawn = 1;
14322 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14324 case CILK_SYNC_STMT:
14325 RETURN (build_cilk_sync ());
14327 case COMPOUND_EXPR:
14328 tmp = RECUR (TREE_OPERAND (t, 0));
14329 if (tmp == NULL_TREE)
14330 /* If the first operand was a statement, we're done with it. */
14331 RETURN (RECUR (TREE_OPERAND (t, 1)));
14332 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14333 RECUR (TREE_OPERAND (t, 1)),
14334 complain));
14336 case ANNOTATE_EXPR:
14337 tmp = RECUR (TREE_OPERAND (t, 0));
14338 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14339 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14341 default:
14342 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14344 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14345 /*function_p=*/false,
14346 integral_constant_expression_p));
14349 RETURN (NULL_TREE);
14350 out:
14351 input_location = loc;
14352 return r;
14353 #undef RECUR
14354 #undef RETURN
14357 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14358 function. For description of the body see comment above
14359 cp_parser_omp_declare_reduction_exprs. */
14361 static void
14362 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14364 if (t == NULL_TREE || t == error_mark_node)
14365 return;
14367 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14369 tree_stmt_iterator tsi;
14370 int i;
14371 tree stmts[7];
14372 memset (stmts, 0, sizeof stmts);
14373 for (i = 0, tsi = tsi_start (t);
14374 i < 7 && !tsi_end_p (tsi);
14375 i++, tsi_next (&tsi))
14376 stmts[i] = tsi_stmt (tsi);
14377 gcc_assert (tsi_end_p (tsi));
14379 if (i >= 3)
14381 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14382 && TREE_CODE (stmts[1]) == DECL_EXPR);
14383 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14384 args, complain, in_decl);
14385 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14386 args, complain, in_decl);
14387 DECL_CONTEXT (omp_out) = current_function_decl;
14388 DECL_CONTEXT (omp_in) = current_function_decl;
14389 keep_next_level (true);
14390 tree block = begin_omp_structured_block ();
14391 tsubst_expr (stmts[2], args, complain, in_decl, false);
14392 block = finish_omp_structured_block (block);
14393 block = maybe_cleanup_point_expr_void (block);
14394 add_decl_expr (omp_out);
14395 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14396 TREE_NO_WARNING (omp_out) = 1;
14397 add_decl_expr (omp_in);
14398 finish_expr_stmt (block);
14400 if (i >= 6)
14402 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14403 && TREE_CODE (stmts[4]) == DECL_EXPR);
14404 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14405 args, complain, in_decl);
14406 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14407 args, complain, in_decl);
14408 DECL_CONTEXT (omp_priv) = current_function_decl;
14409 DECL_CONTEXT (omp_orig) = current_function_decl;
14410 keep_next_level (true);
14411 tree block = begin_omp_structured_block ();
14412 tsubst_expr (stmts[5], args, complain, in_decl, false);
14413 block = finish_omp_structured_block (block);
14414 block = maybe_cleanup_point_expr_void (block);
14415 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14416 add_decl_expr (omp_priv);
14417 add_decl_expr (omp_orig);
14418 finish_expr_stmt (block);
14419 if (i == 7)
14420 add_decl_expr (omp_orig);
14424 /* T is a postfix-expression that is not being used in a function
14425 call. Return the substituted version of T. */
14427 static tree
14428 tsubst_non_call_postfix_expression (tree t, tree args,
14429 tsubst_flags_t complain,
14430 tree in_decl)
14432 if (TREE_CODE (t) == SCOPE_REF)
14433 t = tsubst_qualified_id (t, args, complain, in_decl,
14434 /*done=*/false, /*address_p=*/false);
14435 else
14436 t = tsubst_copy_and_build (t, args, complain, in_decl,
14437 /*function_p=*/false,
14438 /*integral_constant_expression_p=*/false);
14440 return t;
14443 /* Sentinel to disable certain warnings during template substitution. */
14445 struct warning_sentinel {
14446 int &flag;
14447 int val;
14448 warning_sentinel(int& flag, bool suppress=true)
14449 : flag(flag), val(flag) { if (suppress) flag = 0; }
14450 ~warning_sentinel() { flag = val; }
14453 /* Like tsubst but deals with expressions and performs semantic
14454 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14456 tree
14457 tsubst_copy_and_build (tree t,
14458 tree args,
14459 tsubst_flags_t complain,
14460 tree in_decl,
14461 bool function_p,
14462 bool integral_constant_expression_p)
14464 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14465 #define RECUR(NODE) \
14466 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14467 /*function_p=*/false, \
14468 integral_constant_expression_p)
14470 tree retval, op1;
14471 location_t loc;
14473 if (t == NULL_TREE || t == error_mark_node)
14474 return t;
14476 loc = input_location;
14477 if (EXPR_HAS_LOCATION (t))
14478 input_location = EXPR_LOCATION (t);
14480 /* N3276 decltype magic only applies to calls at the top level or on the
14481 right side of a comma. */
14482 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14483 complain &= ~tf_decltype;
14485 switch (TREE_CODE (t))
14487 case USING_DECL:
14488 t = DECL_NAME (t);
14489 /* Fall through. */
14490 case IDENTIFIER_NODE:
14492 tree decl;
14493 cp_id_kind idk;
14494 bool non_integral_constant_expression_p;
14495 const char *error_msg;
14497 if (IDENTIFIER_TYPENAME_P (t))
14499 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14500 t = mangle_conv_op_name_for_type (new_type);
14503 /* Look up the name. */
14504 decl = lookup_name (t);
14506 /* By convention, expressions use ERROR_MARK_NODE to indicate
14507 failure, not NULL_TREE. */
14508 if (decl == NULL_TREE)
14509 decl = error_mark_node;
14511 decl = finish_id_expression (t, decl, NULL_TREE,
14512 &idk,
14513 integral_constant_expression_p,
14514 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14515 &non_integral_constant_expression_p,
14516 /*template_p=*/false,
14517 /*done=*/true,
14518 /*address_p=*/false,
14519 /*template_arg_p=*/false,
14520 &error_msg,
14521 input_location);
14522 if (error_msg)
14523 error (error_msg);
14524 if (!function_p && identifier_p (decl))
14526 if (complain & tf_error)
14527 unqualified_name_lookup_error (decl);
14528 decl = error_mark_node;
14530 RETURN (decl);
14533 case TEMPLATE_ID_EXPR:
14535 tree object;
14536 tree templ = RECUR (TREE_OPERAND (t, 0));
14537 tree targs = TREE_OPERAND (t, 1);
14539 if (targs)
14540 targs = tsubst_template_args (targs, args, complain, in_decl);
14542 if (TREE_CODE (templ) == COMPONENT_REF)
14544 object = TREE_OPERAND (templ, 0);
14545 templ = TREE_OPERAND (templ, 1);
14547 else
14548 object = NULL_TREE;
14549 templ = lookup_template_function (templ, targs);
14551 if (object)
14552 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14553 object, templ, NULL_TREE));
14554 else
14555 RETURN (baselink_for_fns (templ));
14558 case INDIRECT_REF:
14560 tree r = RECUR (TREE_OPERAND (t, 0));
14562 if (REFERENCE_REF_P (t))
14564 /* A type conversion to reference type will be enclosed in
14565 such an indirect ref, but the substitution of the cast
14566 will have also added such an indirect ref. */
14567 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14568 r = convert_from_reference (r);
14570 else
14571 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14572 complain|decltype_flag);
14573 RETURN (r);
14576 case NOP_EXPR:
14577 RETURN (build_nop
14578 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14579 RECUR (TREE_OPERAND (t, 0))));
14581 case IMPLICIT_CONV_EXPR:
14583 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14584 tree expr = RECUR (TREE_OPERAND (t, 0));
14585 int flags = LOOKUP_IMPLICIT;
14586 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14587 flags = LOOKUP_NORMAL;
14588 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14589 flags));
14592 case CONVERT_EXPR:
14593 RETURN (build1
14594 (CONVERT_EXPR,
14595 tsubst (TREE_TYPE (t), args, complain, in_decl),
14596 RECUR (TREE_OPERAND (t, 0))));
14598 case CAST_EXPR:
14599 case REINTERPRET_CAST_EXPR:
14600 case CONST_CAST_EXPR:
14601 case DYNAMIC_CAST_EXPR:
14602 case STATIC_CAST_EXPR:
14604 tree type;
14605 tree op, r = NULL_TREE;
14607 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14608 if (integral_constant_expression_p
14609 && !cast_valid_in_integral_constant_expression_p (type))
14611 if (complain & tf_error)
14612 error ("a cast to a type other than an integral or "
14613 "enumeration type cannot appear in a constant-expression");
14614 RETURN (error_mark_node);
14617 op = RECUR (TREE_OPERAND (t, 0));
14619 warning_sentinel s(warn_useless_cast);
14620 switch (TREE_CODE (t))
14622 case CAST_EXPR:
14623 r = build_functional_cast (type, op, complain);
14624 break;
14625 case REINTERPRET_CAST_EXPR:
14626 r = build_reinterpret_cast (type, op, complain);
14627 break;
14628 case CONST_CAST_EXPR:
14629 r = build_const_cast (type, op, complain);
14630 break;
14631 case DYNAMIC_CAST_EXPR:
14632 r = build_dynamic_cast (type, op, complain);
14633 break;
14634 case STATIC_CAST_EXPR:
14635 r = build_static_cast (type, op, complain);
14636 break;
14637 default:
14638 gcc_unreachable ();
14641 RETURN (r);
14644 case POSTDECREMENT_EXPR:
14645 case POSTINCREMENT_EXPR:
14646 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14647 args, complain, in_decl);
14648 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14649 complain|decltype_flag));
14651 case PREDECREMENT_EXPR:
14652 case PREINCREMENT_EXPR:
14653 case NEGATE_EXPR:
14654 case BIT_NOT_EXPR:
14655 case ABS_EXPR:
14656 case TRUTH_NOT_EXPR:
14657 case UNARY_PLUS_EXPR: /* Unary + */
14658 case REALPART_EXPR:
14659 case IMAGPART_EXPR:
14660 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14661 RECUR (TREE_OPERAND (t, 0)),
14662 complain|decltype_flag));
14664 case FIX_TRUNC_EXPR:
14665 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14666 0, complain));
14668 case ADDR_EXPR:
14669 op1 = TREE_OPERAND (t, 0);
14670 if (TREE_CODE (op1) == LABEL_DECL)
14671 RETURN (finish_label_address_expr (DECL_NAME (op1),
14672 EXPR_LOCATION (op1)));
14673 if (TREE_CODE (op1) == SCOPE_REF)
14674 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14675 /*done=*/true, /*address_p=*/true);
14676 else
14677 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14678 in_decl);
14679 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14680 complain|decltype_flag));
14682 case PLUS_EXPR:
14683 case MINUS_EXPR:
14684 case MULT_EXPR:
14685 case TRUNC_DIV_EXPR:
14686 case CEIL_DIV_EXPR:
14687 case FLOOR_DIV_EXPR:
14688 case ROUND_DIV_EXPR:
14689 case EXACT_DIV_EXPR:
14690 case BIT_AND_EXPR:
14691 case BIT_IOR_EXPR:
14692 case BIT_XOR_EXPR:
14693 case TRUNC_MOD_EXPR:
14694 case FLOOR_MOD_EXPR:
14695 case TRUTH_ANDIF_EXPR:
14696 case TRUTH_ORIF_EXPR:
14697 case TRUTH_AND_EXPR:
14698 case TRUTH_OR_EXPR:
14699 case RSHIFT_EXPR:
14700 case LSHIFT_EXPR:
14701 case RROTATE_EXPR:
14702 case LROTATE_EXPR:
14703 case EQ_EXPR:
14704 case NE_EXPR:
14705 case MAX_EXPR:
14706 case MIN_EXPR:
14707 case LE_EXPR:
14708 case GE_EXPR:
14709 case LT_EXPR:
14710 case GT_EXPR:
14711 case MEMBER_REF:
14712 case DOTSTAR_EXPR:
14714 warning_sentinel s1(warn_type_limits);
14715 warning_sentinel s2(warn_div_by_zero);
14716 tree r = build_x_binary_op
14717 (input_location, TREE_CODE (t),
14718 RECUR (TREE_OPERAND (t, 0)),
14719 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14720 ? ERROR_MARK
14721 : TREE_CODE (TREE_OPERAND (t, 0))),
14722 RECUR (TREE_OPERAND (t, 1)),
14723 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14724 ? ERROR_MARK
14725 : TREE_CODE (TREE_OPERAND (t, 1))),
14726 /*overload=*/NULL,
14727 complain|decltype_flag);
14728 if (EXPR_P (r) && TREE_NO_WARNING (t))
14729 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14731 RETURN (r);
14734 case POINTER_PLUS_EXPR:
14735 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14736 RECUR (TREE_OPERAND (t, 1)));
14738 case SCOPE_REF:
14739 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14740 /*address_p=*/false));
14741 case ARRAY_REF:
14742 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14743 args, complain, in_decl);
14744 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14745 RECUR (TREE_OPERAND (t, 1)),
14746 complain|decltype_flag));
14748 case ARRAY_NOTATION_REF:
14750 tree start_index, length, stride;
14751 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14752 args, complain, in_decl);
14753 start_index = RECUR (ARRAY_NOTATION_START (t));
14754 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14755 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14756 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14757 length, stride, TREE_TYPE (op1)));
14759 case SIZEOF_EXPR:
14760 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14761 RETURN (tsubst_copy (t, args, complain, in_decl));
14762 /* Fall through */
14764 case ALIGNOF_EXPR:
14766 tree r;
14768 op1 = TREE_OPERAND (t, 0);
14769 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14770 op1 = TREE_TYPE (op1);
14771 if (!args)
14773 /* When there are no ARGS, we are trying to evaluate a
14774 non-dependent expression from the parser. Trying to do
14775 the substitutions may not work. */
14776 if (!TYPE_P (op1))
14777 op1 = TREE_TYPE (op1);
14779 else
14781 ++cp_unevaluated_operand;
14782 ++c_inhibit_evaluation_warnings;
14783 if (TYPE_P (op1))
14784 op1 = tsubst (op1, args, complain, in_decl);
14785 else
14786 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14787 /*function_p=*/false,
14788 /*integral_constant_expression_p=*/
14789 false);
14790 --cp_unevaluated_operand;
14791 --c_inhibit_evaluation_warnings;
14793 if (TYPE_P (op1))
14794 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14795 complain & tf_error);
14796 else
14797 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14798 complain & tf_error);
14799 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14801 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14803 if (!processing_template_decl && TYPE_P (op1))
14805 r = build_min (SIZEOF_EXPR, size_type_node,
14806 build1 (NOP_EXPR, op1, error_mark_node));
14807 SIZEOF_EXPR_TYPE_P (r) = 1;
14809 else
14810 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14811 TREE_SIDE_EFFECTS (r) = 0;
14812 TREE_READONLY (r) = 1;
14814 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14816 RETURN (r);
14819 case AT_ENCODE_EXPR:
14821 op1 = TREE_OPERAND (t, 0);
14822 ++cp_unevaluated_operand;
14823 ++c_inhibit_evaluation_warnings;
14824 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14825 /*function_p=*/false,
14826 /*integral_constant_expression_p=*/false);
14827 --cp_unevaluated_operand;
14828 --c_inhibit_evaluation_warnings;
14829 RETURN (objc_build_encode_expr (op1));
14832 case NOEXCEPT_EXPR:
14833 op1 = TREE_OPERAND (t, 0);
14834 ++cp_unevaluated_operand;
14835 ++c_inhibit_evaluation_warnings;
14836 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14837 /*function_p=*/false,
14838 /*integral_constant_expression_p=*/false);
14839 --cp_unevaluated_operand;
14840 --c_inhibit_evaluation_warnings;
14841 RETURN (finish_noexcept_expr (op1, complain));
14843 case MODOP_EXPR:
14845 warning_sentinel s(warn_div_by_zero);
14846 tree r = build_x_modify_expr
14847 (EXPR_LOCATION (t),
14848 RECUR (TREE_OPERAND (t, 0)),
14849 TREE_CODE (TREE_OPERAND (t, 1)),
14850 RECUR (TREE_OPERAND (t, 2)),
14851 complain|decltype_flag);
14852 /* TREE_NO_WARNING must be set if either the expression was
14853 parenthesized or it uses an operator such as >>= rather
14854 than plain assignment. In the former case, it was already
14855 set and must be copied. In the latter case,
14856 build_x_modify_expr sets it and it must not be reset
14857 here. */
14858 if (TREE_NO_WARNING (t))
14859 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14861 RETURN (r);
14864 case ARROW_EXPR:
14865 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14866 args, complain, in_decl);
14867 /* Remember that there was a reference to this entity. */
14868 if (DECL_P (op1))
14869 mark_used (op1);
14870 RETURN (build_x_arrow (input_location, op1, complain));
14872 case NEW_EXPR:
14874 tree placement = RECUR (TREE_OPERAND (t, 0));
14875 tree init = RECUR (TREE_OPERAND (t, 3));
14876 vec<tree, va_gc> *placement_vec;
14877 vec<tree, va_gc> *init_vec;
14878 tree ret;
14880 if (placement == NULL_TREE)
14881 placement_vec = NULL;
14882 else
14884 placement_vec = make_tree_vector ();
14885 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14886 vec_safe_push (placement_vec, TREE_VALUE (placement));
14889 /* If there was an initializer in the original tree, but it
14890 instantiated to an empty list, then we should pass a
14891 non-NULL empty vector to tell build_new that it was an
14892 empty initializer() rather than no initializer. This can
14893 only happen when the initializer is a pack expansion whose
14894 parameter packs are of length zero. */
14895 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14896 init_vec = NULL;
14897 else
14899 init_vec = make_tree_vector ();
14900 if (init == void_zero_node)
14901 gcc_assert (init_vec != NULL);
14902 else
14904 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14905 vec_safe_push (init_vec, TREE_VALUE (init));
14909 ret = build_new (&placement_vec,
14910 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14911 RECUR (TREE_OPERAND (t, 2)),
14912 &init_vec,
14913 NEW_EXPR_USE_GLOBAL (t),
14914 complain);
14916 if (placement_vec != NULL)
14917 release_tree_vector (placement_vec);
14918 if (init_vec != NULL)
14919 release_tree_vector (init_vec);
14921 RETURN (ret);
14924 case DELETE_EXPR:
14925 RETURN (delete_sanity
14926 (RECUR (TREE_OPERAND (t, 0)),
14927 RECUR (TREE_OPERAND (t, 1)),
14928 DELETE_EXPR_USE_VEC (t),
14929 DELETE_EXPR_USE_GLOBAL (t),
14930 complain));
14932 case COMPOUND_EXPR:
14934 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14935 complain & ~tf_decltype, in_decl,
14936 /*function_p=*/false,
14937 integral_constant_expression_p);
14938 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14939 op0,
14940 RECUR (TREE_OPERAND (t, 1)),
14941 complain|decltype_flag));
14944 case CALL_EXPR:
14946 tree function;
14947 vec<tree, va_gc> *call_args;
14948 unsigned int nargs, i;
14949 bool qualified_p;
14950 bool koenig_p;
14951 tree ret;
14953 function = CALL_EXPR_FN (t);
14954 /* When we parsed the expression, we determined whether or
14955 not Koenig lookup should be performed. */
14956 koenig_p = KOENIG_LOOKUP_P (t);
14957 if (TREE_CODE (function) == SCOPE_REF)
14959 qualified_p = true;
14960 function = tsubst_qualified_id (function, args, complain, in_decl,
14961 /*done=*/false,
14962 /*address_p=*/false);
14964 else if (koenig_p && identifier_p (function))
14966 /* Do nothing; calling tsubst_copy_and_build on an identifier
14967 would incorrectly perform unqualified lookup again.
14969 Note that we can also have an IDENTIFIER_NODE if the earlier
14970 unqualified lookup found a member function; in that case
14971 koenig_p will be false and we do want to do the lookup
14972 again to find the instantiated member function.
14974 FIXME but doing that causes c++/15272, so we need to stop
14975 using IDENTIFIER_NODE in that situation. */
14976 qualified_p = false;
14978 else
14980 if (TREE_CODE (function) == COMPONENT_REF)
14982 tree op = TREE_OPERAND (function, 1);
14984 qualified_p = (TREE_CODE (op) == SCOPE_REF
14985 || (BASELINK_P (op)
14986 && BASELINK_QUALIFIED_P (op)));
14988 else
14989 qualified_p = false;
14991 if (TREE_CODE (function) == ADDR_EXPR
14992 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14993 /* Avoid error about taking the address of a constructor. */
14994 function = TREE_OPERAND (function, 0);
14996 function = tsubst_copy_and_build (function, args, complain,
14997 in_decl,
14998 !qualified_p,
14999 integral_constant_expression_p);
15001 if (BASELINK_P (function))
15002 qualified_p = true;
15005 nargs = call_expr_nargs (t);
15006 call_args = make_tree_vector ();
15007 for (i = 0; i < nargs; ++i)
15009 tree arg = CALL_EXPR_ARG (t, i);
15011 if (!PACK_EXPANSION_P (arg))
15012 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15013 else
15015 /* Expand the pack expansion and push each entry onto
15016 CALL_ARGS. */
15017 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15018 if (TREE_CODE (arg) == TREE_VEC)
15020 unsigned int len, j;
15022 len = TREE_VEC_LENGTH (arg);
15023 for (j = 0; j < len; ++j)
15025 tree value = TREE_VEC_ELT (arg, j);
15026 if (value != NULL_TREE)
15027 value = convert_from_reference (value);
15028 vec_safe_push (call_args, value);
15031 else
15033 /* A partial substitution. Add one entry. */
15034 vec_safe_push (call_args, arg);
15039 /* We do not perform argument-dependent lookup if normal
15040 lookup finds a non-function, in accordance with the
15041 expected resolution of DR 218. */
15042 if (koenig_p
15043 && ((is_overloaded_fn (function)
15044 /* If lookup found a member function, the Koenig lookup is
15045 not appropriate, even if an unqualified-name was used
15046 to denote the function. */
15047 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15048 || identifier_p (function))
15049 /* Only do this when substitution turns a dependent call
15050 into a non-dependent call. */
15051 && type_dependent_expression_p_push (t)
15052 && !any_type_dependent_arguments_p (call_args))
15053 function = perform_koenig_lookup (function, call_args, tf_none);
15055 if (identifier_p (function)
15056 && !any_type_dependent_arguments_p (call_args))
15058 if (koenig_p && (complain & tf_warning_or_error))
15060 /* For backwards compatibility and good diagnostics, try
15061 the unqualified lookup again if we aren't in SFINAE
15062 context. */
15063 tree unq = (tsubst_copy_and_build
15064 (function, args, complain, in_decl, true,
15065 integral_constant_expression_p));
15066 if (unq == error_mark_node)
15067 RETURN (error_mark_node);
15069 if (unq != function)
15071 tree fn = unq;
15072 if (INDIRECT_REF_P (fn))
15073 fn = TREE_OPERAND (fn, 0);
15074 if (TREE_CODE (fn) == COMPONENT_REF)
15075 fn = TREE_OPERAND (fn, 1);
15076 if (is_overloaded_fn (fn))
15077 fn = get_first_fn (fn);
15078 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
15079 "%qD was not declared in this scope, "
15080 "and no declarations were found by "
15081 "argument-dependent lookup at the point "
15082 "of instantiation", function))
15084 if (!DECL_P (fn))
15085 /* Can't say anything more. */;
15086 else if (DECL_CLASS_SCOPE_P (fn))
15088 location_t loc = EXPR_LOC_OR_LOC (t,
15089 input_location);
15090 inform (loc,
15091 "declarations in dependent base %qT are "
15092 "not found by unqualified lookup",
15093 DECL_CLASS_CONTEXT (fn));
15094 if (current_class_ptr)
15095 inform (loc,
15096 "use %<this->%D%> instead", function);
15097 else
15098 inform (loc,
15099 "use %<%T::%D%> instead",
15100 current_class_name, function);
15102 else
15103 inform (0, "%q+D declared here, later in the "
15104 "translation unit", fn);
15106 function = unq;
15109 if (identifier_p (function))
15111 if (complain & tf_error)
15112 unqualified_name_lookup_error (function);
15113 release_tree_vector (call_args);
15114 RETURN (error_mark_node);
15118 /* Remember that there was a reference to this entity. */
15119 if (DECL_P (function))
15120 mark_used (function);
15122 /* Put back tf_decltype for the actual call. */
15123 complain |= decltype_flag;
15125 if (TREE_CODE (function) == OFFSET_REF)
15126 ret = build_offset_ref_call_from_tree (function, &call_args,
15127 complain);
15128 else if (TREE_CODE (function) == COMPONENT_REF)
15130 tree instance = TREE_OPERAND (function, 0);
15131 tree fn = TREE_OPERAND (function, 1);
15133 if (processing_template_decl
15134 && (type_dependent_expression_p (instance)
15135 || (!BASELINK_P (fn)
15136 && TREE_CODE (fn) != FIELD_DECL)
15137 || type_dependent_expression_p (fn)
15138 || any_type_dependent_arguments_p (call_args)))
15139 ret = build_nt_call_vec (function, call_args);
15140 else if (!BASELINK_P (fn))
15141 ret = finish_call_expr (function, &call_args,
15142 /*disallow_virtual=*/false,
15143 /*koenig_p=*/false,
15144 complain);
15145 else
15146 ret = (build_new_method_call
15147 (instance, fn,
15148 &call_args, NULL_TREE,
15149 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
15150 /*fn_p=*/NULL,
15151 complain));
15153 else
15154 ret = finish_call_expr (function, &call_args,
15155 /*disallow_virtual=*/qualified_p,
15156 koenig_p,
15157 complain);
15159 release_tree_vector (call_args);
15161 RETURN (ret);
15164 case COND_EXPR:
15166 tree cond = RECUR (TREE_OPERAND (t, 0));
15167 tree exp1, exp2;
15169 if (TREE_CODE (cond) == INTEGER_CST)
15171 if (integer_zerop (cond))
15173 ++c_inhibit_evaluation_warnings;
15174 exp1 = RECUR (TREE_OPERAND (t, 1));
15175 --c_inhibit_evaluation_warnings;
15176 exp2 = RECUR (TREE_OPERAND (t, 2));
15178 else
15180 exp1 = RECUR (TREE_OPERAND (t, 1));
15181 ++c_inhibit_evaluation_warnings;
15182 exp2 = RECUR (TREE_OPERAND (t, 2));
15183 --c_inhibit_evaluation_warnings;
15186 else
15188 exp1 = RECUR (TREE_OPERAND (t, 1));
15189 exp2 = RECUR (TREE_OPERAND (t, 2));
15192 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
15193 cond, exp1, exp2, complain));
15196 case PSEUDO_DTOR_EXPR:
15197 RETURN (finish_pseudo_destructor_expr
15198 (RECUR (TREE_OPERAND (t, 0)),
15199 RECUR (TREE_OPERAND (t, 1)),
15200 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
15201 input_location));
15203 case TREE_LIST:
15205 tree purpose, value, chain;
15207 if (t == void_list_node)
15208 RETURN (t);
15210 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15211 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15213 /* We have pack expansions, so expand those and
15214 create a new list out of it. */
15215 tree purposevec = NULL_TREE;
15216 tree valuevec = NULL_TREE;
15217 tree chain;
15218 int i, len = -1;
15220 /* Expand the argument expressions. */
15221 if (TREE_PURPOSE (t))
15222 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15223 complain, in_decl);
15224 if (TREE_VALUE (t))
15225 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15226 complain, in_decl);
15228 /* Build the rest of the list. */
15229 chain = TREE_CHAIN (t);
15230 if (chain && chain != void_type_node)
15231 chain = RECUR (chain);
15233 /* Determine the number of arguments. */
15234 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15236 len = TREE_VEC_LENGTH (purposevec);
15237 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15239 else if (TREE_CODE (valuevec) == TREE_VEC)
15240 len = TREE_VEC_LENGTH (valuevec);
15241 else
15243 /* Since we only performed a partial substitution into
15244 the argument pack, we only RETURN (a single list
15245 node. */
15246 if (purposevec == TREE_PURPOSE (t)
15247 && valuevec == TREE_VALUE (t)
15248 && chain == TREE_CHAIN (t))
15249 RETURN (t);
15251 RETURN (tree_cons (purposevec, valuevec, chain));
15254 /* Convert the argument vectors into a TREE_LIST */
15255 i = len;
15256 while (i > 0)
15258 /* Grab the Ith values. */
15259 i--;
15260 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15261 : NULL_TREE;
15262 value
15263 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15264 : NULL_TREE;
15266 /* Build the list (backwards). */
15267 chain = tree_cons (purpose, value, chain);
15270 RETURN (chain);
15273 purpose = TREE_PURPOSE (t);
15274 if (purpose)
15275 purpose = RECUR (purpose);
15276 value = TREE_VALUE (t);
15277 if (value)
15278 value = RECUR (value);
15279 chain = TREE_CHAIN (t);
15280 if (chain && chain != void_type_node)
15281 chain = RECUR (chain);
15282 if (purpose == TREE_PURPOSE (t)
15283 && value == TREE_VALUE (t)
15284 && chain == TREE_CHAIN (t))
15285 RETURN (t);
15286 RETURN (tree_cons (purpose, value, chain));
15289 case COMPONENT_REF:
15291 tree object;
15292 tree object_type;
15293 tree member;
15294 tree r;
15296 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15297 args, complain, in_decl);
15298 /* Remember that there was a reference to this entity. */
15299 if (DECL_P (object))
15300 mark_used (object);
15301 object_type = TREE_TYPE (object);
15303 member = TREE_OPERAND (t, 1);
15304 if (BASELINK_P (member))
15305 member = tsubst_baselink (member,
15306 non_reference (TREE_TYPE (object)),
15307 args, complain, in_decl);
15308 else
15309 member = tsubst_copy (member, args, complain, in_decl);
15310 if (member == error_mark_node)
15311 RETURN (error_mark_node);
15313 if (type_dependent_expression_p (object))
15314 /* We can't do much here. */;
15315 else if (!CLASS_TYPE_P (object_type))
15317 if (scalarish_type_p (object_type))
15319 tree s = NULL_TREE;
15320 tree dtor = member;
15322 if (TREE_CODE (dtor) == SCOPE_REF)
15324 s = TREE_OPERAND (dtor, 0);
15325 dtor = TREE_OPERAND (dtor, 1);
15327 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15329 dtor = TREE_OPERAND (dtor, 0);
15330 if (TYPE_P (dtor))
15331 RETURN (finish_pseudo_destructor_expr
15332 (object, s, dtor, input_location));
15336 else if (TREE_CODE (member) == SCOPE_REF
15337 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15339 /* Lookup the template functions now that we know what the
15340 scope is. */
15341 tree scope = TREE_OPERAND (member, 0);
15342 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15343 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15344 member = lookup_qualified_name (scope, tmpl,
15345 /*is_type_p=*/false,
15346 /*complain=*/false);
15347 if (BASELINK_P (member))
15349 BASELINK_FUNCTIONS (member)
15350 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15351 args);
15352 member = (adjust_result_of_qualified_name_lookup
15353 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15354 object_type));
15356 else
15358 qualified_name_lookup_error (scope, tmpl, member,
15359 input_location);
15360 RETURN (error_mark_node);
15363 else if (TREE_CODE (member) == SCOPE_REF
15364 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15365 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15367 if (complain & tf_error)
15369 if (TYPE_P (TREE_OPERAND (member, 0)))
15370 error ("%qT is not a class or namespace",
15371 TREE_OPERAND (member, 0));
15372 else
15373 error ("%qD is not a class or namespace",
15374 TREE_OPERAND (member, 0));
15376 RETURN (error_mark_node);
15378 else if (TREE_CODE (member) == FIELD_DECL)
15380 r = finish_non_static_data_member (member, object, NULL_TREE);
15381 if (TREE_CODE (r) == COMPONENT_REF)
15382 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15383 RETURN (r);
15386 r = finish_class_member_access_expr (object, member,
15387 /*template_p=*/false,
15388 complain);
15389 if (TREE_CODE (r) == COMPONENT_REF)
15390 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15391 RETURN (r);
15394 case THROW_EXPR:
15395 RETURN (build_throw
15396 (RECUR (TREE_OPERAND (t, 0))));
15398 case CONSTRUCTOR:
15400 vec<constructor_elt, va_gc> *n;
15401 constructor_elt *ce;
15402 unsigned HOST_WIDE_INT idx;
15403 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15404 bool process_index_p;
15405 int newlen;
15406 bool need_copy_p = false;
15407 tree r;
15409 if (type == error_mark_node)
15410 RETURN (error_mark_node);
15412 /* digest_init will do the wrong thing if we let it. */
15413 if (type && TYPE_PTRMEMFUNC_P (type))
15414 RETURN (t);
15416 /* We do not want to process the index of aggregate
15417 initializers as they are identifier nodes which will be
15418 looked up by digest_init. */
15419 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15421 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15422 newlen = vec_safe_length (n);
15423 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15425 if (ce->index && process_index_p
15426 /* An identifier index is looked up in the type
15427 being initialized, not the current scope. */
15428 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15429 ce->index = RECUR (ce->index);
15431 if (PACK_EXPANSION_P (ce->value))
15433 /* Substitute into the pack expansion. */
15434 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15435 in_decl);
15437 if (ce->value == error_mark_node
15438 || PACK_EXPANSION_P (ce->value))
15440 else if (TREE_VEC_LENGTH (ce->value) == 1)
15441 /* Just move the argument into place. */
15442 ce->value = TREE_VEC_ELT (ce->value, 0);
15443 else
15445 /* Update the length of the final CONSTRUCTOR
15446 arguments vector, and note that we will need to
15447 copy.*/
15448 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15449 need_copy_p = true;
15452 else
15453 ce->value = RECUR (ce->value);
15456 if (need_copy_p)
15458 vec<constructor_elt, va_gc> *old_n = n;
15460 vec_alloc (n, newlen);
15461 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15463 if (TREE_CODE (ce->value) == TREE_VEC)
15465 int i, len = TREE_VEC_LENGTH (ce->value);
15466 for (i = 0; i < len; ++i)
15467 CONSTRUCTOR_APPEND_ELT (n, 0,
15468 TREE_VEC_ELT (ce->value, i));
15470 else
15471 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15475 r = build_constructor (init_list_type_node, n);
15476 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15478 if (TREE_HAS_CONSTRUCTOR (t))
15479 RETURN (finish_compound_literal (type, r, complain));
15481 TREE_TYPE (r) = type;
15482 RETURN (r);
15485 case TYPEID_EXPR:
15487 tree operand_0 = TREE_OPERAND (t, 0);
15488 if (TYPE_P (operand_0))
15490 operand_0 = tsubst (operand_0, args, complain, in_decl);
15491 RETURN (get_typeid (operand_0, complain));
15493 else
15495 operand_0 = RECUR (operand_0);
15496 RETURN (build_typeid (operand_0, complain));
15500 case VAR_DECL:
15501 if (!args)
15502 RETURN (t);
15503 else if (DECL_PACK_P (t))
15505 /* We don't build decls for an instantiation of a
15506 variadic capture proxy, we instantiate the elements
15507 when needed. */
15508 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15509 return RECUR (DECL_VALUE_EXPR (t));
15511 /* Fall through */
15513 case PARM_DECL:
15515 tree r = tsubst_copy (t, args, complain, in_decl);
15517 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15518 /* If the original type was a reference, we'll be wrapped in
15519 the appropriate INDIRECT_REF. */
15520 r = convert_from_reference (r);
15522 // If the type of the argument is a pack expansion, then
15523 // the parameter must also be expanded.
15524 if (PACK_EXPANSION_P (TREE_TYPE (r)))
15525 r = make_pack_expansion (r);
15527 RETURN (r);
15530 case VA_ARG_EXPR:
15531 RETURN (build_x_va_arg (EXPR_LOCATION (t),
15532 RECUR (TREE_OPERAND (t, 0)),
15533 tsubst (TREE_TYPE (t), args, complain, in_decl)));
15535 case OFFSETOF_EXPR:
15536 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
15538 case TRAIT_EXPR:
15540 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15541 complain, in_decl);
15543 tree type2 = TRAIT_EXPR_TYPE2 (t);
15544 if (type2)
15545 type2 = tsubst (type2, args, complain, in_decl);
15547 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15550 case STMT_EXPR:
15552 tree old_stmt_expr = cur_stmt_expr;
15553 tree stmt_expr = begin_stmt_expr ();
15555 cur_stmt_expr = stmt_expr;
15556 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15557 integral_constant_expression_p);
15558 stmt_expr = finish_stmt_expr (stmt_expr, false);
15559 cur_stmt_expr = old_stmt_expr;
15561 /* If the resulting list of expression statement is empty,
15562 fold it further into void_zero_node. */
15563 if (empty_expr_stmt_p (stmt_expr))
15564 stmt_expr = void_zero_node;
15566 RETURN (stmt_expr);
15569 case LAMBDA_EXPR:
15571 tree r = build_lambda_expr ();
15573 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15574 LAMBDA_EXPR_CLOSURE (r) = type;
15575 CLASSTYPE_LAMBDA_EXPR (type) = r;
15577 LAMBDA_EXPR_LOCATION (r)
15578 = LAMBDA_EXPR_LOCATION (t);
15579 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15580 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15581 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15582 LAMBDA_EXPR_DISCRIMINATOR (r)
15583 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15584 /* For a function scope, we want to use tsubst so that we don't
15585 complain about referring to an auto function before its return
15586 type has been deduced. Otherwise, we want to use tsubst_copy so
15587 that we look up the existing field/parameter/variable rather
15588 than build a new one. */
15589 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15590 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15591 scope = tsubst (scope, args, complain, in_decl);
15592 else if (scope && TREE_CODE (scope) == PARM_DECL)
15594 /* Look up the parameter we want directly, as tsubst_copy
15595 doesn't do what we need. */
15596 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15597 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15598 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15599 parm = DECL_CHAIN (parm);
15600 scope = parm;
15601 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15602 if (DECL_CONTEXT (scope) == NULL_TREE)
15603 DECL_CONTEXT (scope) = fn;
15605 else
15606 scope = RECUR (scope);
15607 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15608 LAMBDA_EXPR_RETURN_TYPE (r)
15609 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15611 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15612 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15614 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15615 determine_visibility (TYPE_NAME (type));
15616 /* Now that we know visibility, instantiate the type so we have a
15617 declaration of the op() for later calls to lambda_function. */
15618 complete_type (type);
15620 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15622 RETURN (build_lambda_object (r));
15625 case TARGET_EXPR:
15626 /* We can get here for a constant initializer of non-dependent type.
15627 FIXME stop folding in cp_parser_initializer_clause. */
15629 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15630 complain);
15631 RETURN (r);
15634 case TRANSACTION_EXPR:
15635 RETURN (tsubst_expr(t, args, complain, in_decl,
15636 integral_constant_expression_p));
15638 case PAREN_EXPR:
15639 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15641 case VEC_PERM_EXPR:
15642 RETURN (build_x_vec_perm_expr (input_location,
15643 RECUR (TREE_OPERAND (t, 0)),
15644 RECUR (TREE_OPERAND (t, 1)),
15645 RECUR (TREE_OPERAND (t, 2)),
15646 complain));
15648 case REQUIRES_EXPR:
15649 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
15651 case VALIDEXPR_EXPR:
15652 RETURN (tsubst_validexpr_expr (t, args, in_decl));
15654 case VALIDTYPE_EXPR:
15655 RETURN (tsubst_validtype_expr (t, args, in_decl));
15657 case CONSTEXPR_EXPR:
15658 RETURN (tsubst_constexpr_expr (t, args, in_decl));
15660 // Normally, *_REQ are reduced out of requiremetns when used
15661 // as constraints. If a concept is checked directly via e.g.,
15662 // a static_assert, however, these appear in the input tree.
15664 case EXPR_REQ:
15665 RETURN (tsubst_expr_req (t, args, in_decl));
15667 case TYPE_REQ:
15668 RETURN (tsubst_type_req (t, args, in_decl));
15670 case NESTED_REQ:
15671 RETURN (tsubst_nested_req (t, args, in_decl));
15673 default:
15674 /* Handle Objective-C++ constructs, if appropriate. */
15676 tree subst
15677 = objcp_tsubst_copy_and_build (t, args, complain,
15678 in_decl, /*function_p=*/false);
15679 if (subst)
15680 RETURN (subst);
15682 RETURN (tsubst_copy (t, args, complain, in_decl));
15685 #undef RECUR
15686 #undef RETURN
15687 out:
15688 input_location = loc;
15689 return retval;
15692 /* Verify that the instantiated ARGS are valid. For type arguments,
15693 make sure that the type's linkage is ok. For non-type arguments,
15694 make sure they are constants if they are integral or enumerations.
15695 Emit an error under control of COMPLAIN, and return TRUE on error. */
15697 static bool
15698 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15700 if (dependent_template_arg_p (t))
15701 return false;
15702 if (ARGUMENT_PACK_P (t))
15704 tree vec = ARGUMENT_PACK_ARGS (t);
15705 int len = TREE_VEC_LENGTH (vec);
15706 bool result = false;
15707 int i;
15709 for (i = 0; i < len; ++i)
15710 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15711 result = true;
15712 return result;
15714 else if (TYPE_P (t))
15716 /* [basic.link]: A name with no linkage (notably, the name
15717 of a class or enumeration declared in a local scope)
15718 shall not be used to declare an entity with linkage.
15719 This implies that names with no linkage cannot be used as
15720 template arguments
15722 DR 757 relaxes this restriction for C++0x. */
15723 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15724 : no_linkage_check (t, /*relaxed_p=*/false));
15726 if (nt)
15728 /* DR 488 makes use of a type with no linkage cause
15729 type deduction to fail. */
15730 if (complain & tf_error)
15732 if (TYPE_ANONYMOUS_P (nt))
15733 error ("%qT is/uses anonymous type", t);
15734 else
15735 error ("template argument for %qD uses local type %qT",
15736 tmpl, t);
15738 return true;
15740 /* In order to avoid all sorts of complications, we do not
15741 allow variably-modified types as template arguments. */
15742 else if (variably_modified_type_p (t, NULL_TREE))
15744 if (complain & tf_error)
15745 error ("%qT is a variably modified type", t);
15746 return true;
15749 /* Class template and alias template arguments should be OK. */
15750 else if (DECL_TYPE_TEMPLATE_P (t))
15752 /* A non-type argument of integral or enumerated type must be a
15753 constant. */
15754 else if (TREE_TYPE (t)
15755 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15756 && !TREE_CONSTANT (t))
15758 if (complain & tf_error)
15759 error ("integral expression %qE is not constant", t);
15760 return true;
15762 return false;
15765 static bool
15766 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15768 int ix, len = DECL_NTPARMS (tmpl);
15769 bool result = false;
15771 for (ix = 0; ix != len; ix++)
15773 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15774 result = true;
15776 if (result && (complain & tf_error))
15777 error (" trying to instantiate %qD", tmpl);
15778 return result;
15781 /* We're out of SFINAE context now, so generate diagnostics for the access
15782 errors we saw earlier when instantiating D from TMPL and ARGS. */
15784 static void
15785 recheck_decl_substitution (tree d, tree tmpl, tree args)
15787 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15788 tree type = TREE_TYPE (pattern);
15789 location_t loc = input_location;
15791 push_access_scope (d);
15792 push_deferring_access_checks (dk_no_deferred);
15793 input_location = DECL_SOURCE_LOCATION (pattern);
15794 tsubst (type, args, tf_warning_or_error, d);
15795 input_location = loc;
15796 pop_deferring_access_checks ();
15797 pop_access_scope (d);
15800 /* Instantiate the indicated variable, function, or alias template TMPL with
15801 the template arguments in TARG_PTR. */
15803 static tree
15804 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15806 tree targ_ptr = orig_args;
15807 tree fndecl;
15808 tree gen_tmpl;
15809 tree spec;
15810 bool access_ok = true;
15812 if (tmpl == error_mark_node)
15813 return error_mark_node;
15815 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15817 /* If this function is a clone, handle it specially. */
15818 if (DECL_CLONED_FUNCTION_P (tmpl))
15820 tree spec;
15821 tree clone;
15823 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15824 DECL_CLONED_FUNCTION. */
15825 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15826 targ_ptr, complain);
15827 if (spec == error_mark_node)
15828 return error_mark_node;
15830 /* Look for the clone. */
15831 FOR_EACH_CLONE (clone, spec)
15832 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15833 return clone;
15834 /* We should always have found the clone by now. */
15835 gcc_unreachable ();
15836 return NULL_TREE;
15839 if (targ_ptr == error_mark_node)
15840 return error_mark_node;
15842 /* Check to see if we already have this specialization. */
15843 gen_tmpl = most_general_template (tmpl);
15844 if (tmpl != gen_tmpl)
15845 /* The TMPL is a partial instantiation. To get a full set of
15846 arguments we must add the arguments used to perform the
15847 partial instantiation. */
15848 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15849 targ_ptr);
15851 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15852 but it doesn't seem to be on the hot path. */
15853 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15855 gcc_assert (tmpl == gen_tmpl
15856 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15857 == spec)
15858 || fndecl == NULL_TREE);
15860 if (spec != NULL_TREE)
15862 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15864 if (complain & tf_error)
15865 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15866 return error_mark_node;
15868 return spec;
15871 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15872 complain))
15873 return error_mark_node;
15875 /* We are building a FUNCTION_DECL, during which the access of its
15876 parameters and return types have to be checked. However this
15877 FUNCTION_DECL which is the desired context for access checking
15878 is not built yet. We solve this chicken-and-egg problem by
15879 deferring all checks until we have the FUNCTION_DECL. */
15880 push_deferring_access_checks (dk_deferred);
15882 /* Instantiation of the function happens in the context of the function
15883 template, not the context of the overload resolution we're doing. */
15884 push_to_top_level ();
15885 /* If there are dependent arguments, e.g. because we're doing partial
15886 ordering, make sure processing_template_decl stays set. */
15887 if (uses_template_parms (targ_ptr))
15888 ++processing_template_decl;
15889 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15891 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15892 complain, gen_tmpl);
15893 push_nested_class (ctx);
15895 /* Substitute template parameters to obtain the specialization. */
15896 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15897 targ_ptr, complain, gen_tmpl);
15898 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15899 pop_nested_class ();
15900 pop_from_top_level ();
15902 if (fndecl == error_mark_node)
15904 pop_deferring_access_checks ();
15905 return error_mark_node;
15908 /* The DECL_TI_TEMPLATE should always be the immediate parent
15909 template, not the most general template. */
15910 DECL_TI_TEMPLATE (fndecl) = tmpl;
15912 /* Now we know the specialization, compute access previously
15913 deferred. */
15914 push_access_scope (fndecl);
15915 if (!perform_deferred_access_checks (complain))
15916 access_ok = false;
15917 pop_access_scope (fndecl);
15918 pop_deferring_access_checks ();
15920 /* If we've just instantiated the main entry point for a function,
15921 instantiate all the alternate entry points as well. We do this
15922 by cloning the instantiation of the main entry point, not by
15923 instantiating the template clones. */
15924 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15925 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15927 if (!access_ok)
15929 if (!(complain & tf_error))
15931 /* Remember to reinstantiate when we're out of SFINAE so the user
15932 can see the errors. */
15933 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15935 return error_mark_node;
15937 return fndecl;
15940 /* Wrapper for instantiate_template_1. */
15942 tree
15943 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15945 tree ret;
15946 timevar_push (TV_TEMPLATE_INST);
15947 ret = instantiate_template_1 (tmpl, orig_args, complain);
15948 timevar_pop (TV_TEMPLATE_INST);
15949 return ret;
15952 /* Instantiate the alias template TMPL with ARGS. Also push a template
15953 instantiation level, which instantiate_template doesn't do because
15954 functions and variables have sufficient context established by the
15955 callers. */
15957 static tree
15958 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15960 struct pending_template *old_last_pend = last_pending_template;
15961 struct tinst_level *old_error_tinst = last_error_tinst_level;
15962 if (tmpl == error_mark_node || args == error_mark_node)
15963 return error_mark_node;
15964 tree tinst = build_tree_list (tmpl, args);
15965 if (!push_tinst_level (tinst))
15967 ggc_free (tinst);
15968 return error_mark_node;
15971 args =
15972 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15973 args, tmpl, complain,
15974 /*require_all_args=*/true,
15975 /*use_default_args=*/true);
15977 tree r = instantiate_template (tmpl, args, complain);
15978 pop_tinst_level ();
15979 /* We can't free this if a pending_template entry or last_error_tinst_level
15980 is pointing at it. */
15981 if (last_pending_template == old_last_pend
15982 && last_error_tinst_level == old_error_tinst)
15983 ggc_free (tinst);
15985 return r;
15988 /* PARM is a template parameter pack for FN. Returns true iff
15989 PARM is used in a deducible way in the argument list of FN. */
15991 static bool
15992 pack_deducible_p (tree parm, tree fn)
15994 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15995 for (; t; t = TREE_CHAIN (t))
15997 tree type = TREE_VALUE (t);
15998 tree packs;
15999 if (!PACK_EXPANSION_P (type))
16000 continue;
16001 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16002 packs; packs = TREE_CHAIN (packs))
16003 if (TREE_VALUE (packs) == parm)
16005 /* The template parameter pack is used in a function parameter
16006 pack. If this is the end of the parameter list, the
16007 template parameter pack is deducible. */
16008 if (TREE_CHAIN (t) == void_list_node)
16009 return true;
16010 else
16011 /* Otherwise, not. Well, it could be deduced from
16012 a non-pack parameter, but doing so would end up with
16013 a deduction mismatch, so don't bother. */
16014 return false;
16017 /* The template parameter pack isn't used in any function parameter
16018 packs, but it might be used deeper, e.g. tuple<Args...>. */
16019 return true;
16022 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16023 NARGS elements of the arguments that are being used when calling
16024 it. TARGS is a vector into which the deduced template arguments
16025 are placed.
16027 Returns either a FUNCTION_DECL for the matching specialization of FN or
16028 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16029 true, diagnostics will be printed to explain why it failed.
16031 If FN is a conversion operator, or we are trying to produce a specific
16032 specialization, RETURN_TYPE is the return type desired.
16034 The EXPLICIT_TARGS are explicit template arguments provided via a
16035 template-id.
16037 The parameter STRICT is one of:
16039 DEDUCE_CALL:
16040 We are deducing arguments for a function call, as in
16041 [temp.deduct.call].
16043 DEDUCE_CONV:
16044 We are deducing arguments for a conversion function, as in
16045 [temp.deduct.conv].
16047 DEDUCE_EXACT:
16048 We are deducing arguments when doing an explicit instantiation
16049 as in [temp.explicit], when determining an explicit specialization
16050 as in [temp.expl.spec], or when taking the address of a function
16051 template, as in [temp.deduct.funcaddr]. */
16053 tree
16054 fn_type_unification (tree fn,
16055 tree explicit_targs,
16056 tree targs,
16057 const tree *args,
16058 unsigned int nargs,
16059 tree return_type,
16060 unification_kind_t strict,
16061 int flags,
16062 bool explain_p,
16063 bool decltype_p)
16065 tree parms;
16066 tree fntype;
16067 tree decl = NULL_TREE;
16068 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
16069 bool ok;
16070 static int deduction_depth;
16071 struct pending_template *old_last_pend = last_pending_template;
16072 struct tinst_level *old_error_tinst = last_error_tinst_level;
16073 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
16074 tree tinst;
16075 tree r = error_mark_node;
16077 if (decltype_p)
16078 complain |= tf_decltype;
16080 /* In C++0x, it's possible to have a function template whose type depends
16081 on itself recursively. This is most obvious with decltype, but can also
16082 occur with enumeration scope (c++/48969). So we need to catch infinite
16083 recursion and reject the substitution at deduction time; this function
16084 will return error_mark_node for any repeated substitution.
16086 This also catches excessive recursion such as when f<N> depends on
16087 f<N-1> across all integers, and returns error_mark_node for all the
16088 substitutions back up to the initial one.
16090 This is, of course, not reentrant. */
16091 if (excessive_deduction_depth)
16092 return error_mark_node;
16093 tinst = build_tree_list (fn, NULL_TREE);
16094 ++deduction_depth;
16096 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
16098 fntype = TREE_TYPE (fn);
16099 if (explicit_targs)
16101 /* [temp.deduct]
16103 The specified template arguments must match the template
16104 parameters in kind (i.e., type, nontype, template), and there
16105 must not be more arguments than there are parameters;
16106 otherwise type deduction fails.
16108 Nontype arguments must match the types of the corresponding
16109 nontype template parameters, or must be convertible to the
16110 types of the corresponding nontype parameters as specified in
16111 _temp.arg.nontype_, otherwise type deduction fails.
16113 All references in the function type of the function template
16114 to the corresponding template parameters are replaced by the
16115 specified template argument values. If a substitution in a
16116 template parameter or in the function type of the function
16117 template results in an invalid type, type deduction fails. */
16118 int i, len = TREE_VEC_LENGTH (tparms);
16119 location_t loc = input_location;
16120 bool incomplete = false;
16122 /* Adjust any explicit template arguments before entering the
16123 substitution context. */
16124 explicit_targs
16125 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
16126 complain,
16127 /*require_all_args=*/false,
16128 /*use_default_args=*/false));
16129 if (explicit_targs == error_mark_node)
16130 goto fail;
16132 /* Substitute the explicit args into the function type. This is
16133 necessary so that, for instance, explicitly declared function
16134 arguments can match null pointed constants. If we were given
16135 an incomplete set of explicit args, we must not do semantic
16136 processing during substitution as we could create partial
16137 instantiations. */
16138 for (i = 0; i < len; i++)
16140 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16141 bool parameter_pack = false;
16142 tree targ = TREE_VEC_ELT (explicit_targs, i);
16144 /* Dig out the actual parm. */
16145 if (TREE_CODE (parm) == TYPE_DECL
16146 || TREE_CODE (parm) == TEMPLATE_DECL)
16148 parm = TREE_TYPE (parm);
16149 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
16151 else if (TREE_CODE (parm) == PARM_DECL)
16153 parm = DECL_INITIAL (parm);
16154 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
16157 if (!parameter_pack && targ == NULL_TREE)
16158 /* No explicit argument for this template parameter. */
16159 incomplete = true;
16161 if (parameter_pack && pack_deducible_p (parm, fn))
16163 /* Mark the argument pack as "incomplete". We could
16164 still deduce more arguments during unification.
16165 We remove this mark in type_unification_real. */
16166 if (targ)
16168 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
16169 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
16170 = ARGUMENT_PACK_ARGS (targ);
16173 /* We have some incomplete argument packs. */
16174 incomplete = true;
16178 TREE_VALUE (tinst) = explicit_targs;
16179 if (!push_tinst_level (tinst))
16181 excessive_deduction_depth = true;
16182 goto fail;
16184 processing_template_decl += incomplete;
16185 input_location = DECL_SOURCE_LOCATION (fn);
16186 /* Ignore any access checks; we'll see them again in
16187 instantiate_template and they might have the wrong
16188 access path at this point. */
16189 push_deferring_access_checks (dk_deferred);
16190 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
16191 complain | tf_partial, NULL_TREE);
16192 pop_deferring_access_checks ();
16193 input_location = loc;
16194 processing_template_decl -= incomplete;
16195 pop_tinst_level ();
16197 if (fntype == error_mark_node)
16198 goto fail;
16200 /* Place the explicitly specified arguments in TARGS. */
16201 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
16202 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
16205 /* Never do unification on the 'this' parameter. */
16206 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
16208 if (return_type)
16210 tree *new_args;
16212 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
16213 new_args = XALLOCAVEC (tree, nargs + 1);
16214 new_args[0] = return_type;
16215 memcpy (new_args + 1, args, nargs * sizeof (tree));
16216 args = new_args;
16217 ++nargs;
16220 /* We allow incomplete unification without an error message here
16221 because the standard doesn't seem to explicitly prohibit it. Our
16222 callers must be ready to deal with unification failures in any
16223 event. */
16225 TREE_VALUE (tinst) = targs;
16226 /* If we aren't explaining yet, push tinst context so we can see where
16227 any errors (e.g. from class instantiations triggered by instantiation
16228 of default template arguments) come from. If we are explaining, this
16229 context is redundant. */
16230 if (!explain_p && !push_tinst_level (tinst))
16232 excessive_deduction_depth = true;
16233 goto fail;
16236 /* type_unification_real will pass back any access checks from default
16237 template argument substitution. */
16238 vec<deferred_access_check, va_gc> *checks;
16239 checks = NULL;
16241 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16242 targs, parms, args, nargs, /*subr=*/0,
16243 strict, flags, &checks, explain_p);
16244 if (!explain_p)
16245 pop_tinst_level ();
16246 if (!ok)
16247 goto fail;
16249 /* Now that we have bindings for all of the template arguments,
16250 ensure that the arguments deduced for the template template
16251 parameters have compatible template parameter lists. We cannot
16252 check this property before we have deduced all template
16253 arguments, because the template parameter types of a template
16254 template parameter might depend on prior template parameters
16255 deduced after the template template parameter. The following
16256 ill-formed example illustrates this issue:
16258 template<typename T, template<T> class C> void f(C<5>, T);
16260 template<int N> struct X {};
16262 void g() {
16263 f(X<5>(), 5l); // error: template argument deduction fails
16266 The template parameter list of 'C' depends on the template type
16267 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16268 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16269 time that we deduce 'C'. */
16270 if (!template_template_parm_bindings_ok_p
16271 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16273 unify_inconsistent_template_template_parameters (explain_p);
16274 goto fail;
16277 // All is well so far. Now, check that the template constraints
16278 // are satisfied.
16279 if (!check_template_constraints (fn, targs))
16281 if (explain_p)
16282 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
16283 return error_mark_node;
16286 /* All is well so far. Now, check:
16288 [temp.deduct]
16290 When all template arguments have been deduced, all uses of
16291 template parameters in nondeduced contexts are replaced with
16292 the corresponding deduced argument values. If the
16293 substitution results in an invalid type, as described above,
16294 type deduction fails. */
16295 TREE_VALUE (tinst) = targs;
16296 if (!push_tinst_level (tinst))
16298 excessive_deduction_depth = true;
16299 goto fail;
16302 /* Also collect access checks from the instantiation. */
16303 reopen_deferring_access_checks (checks);
16305 decl = instantiate_template (fn, targs, complain);
16307 checks = get_deferred_access_checks ();
16308 pop_deferring_access_checks ();
16310 pop_tinst_level ();
16312 if (decl == error_mark_node)
16313 goto fail;
16315 /* Now perform any access checks encountered during substitution. */
16316 push_access_scope (decl);
16317 ok = perform_access_checks (checks, complain);
16318 pop_access_scope (decl);
16319 if (!ok)
16320 goto fail;
16322 /* If we're looking for an exact match, check that what we got
16323 is indeed an exact match. It might not be if some template
16324 parameters are used in non-deduced contexts. But don't check
16325 for an exact match if we have dependent template arguments;
16326 in that case we're doing partial ordering, and we already know
16327 that we have two candidates that will provide the actual type. */
16328 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16330 tree substed = TREE_TYPE (decl);
16331 unsigned int i;
16333 tree sarg
16334 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16335 if (return_type)
16336 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16337 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16338 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16340 unify_type_mismatch (explain_p, args[i],
16341 TREE_VALUE (sarg));
16342 goto fail;
16346 r = decl;
16348 fail:
16349 --deduction_depth;
16350 if (excessive_deduction_depth)
16352 if (deduction_depth == 0)
16353 /* Reset once we're all the way out. */
16354 excessive_deduction_depth = false;
16357 /* We can't free this if a pending_template entry or last_error_tinst_level
16358 is pointing at it. */
16359 if (last_pending_template == old_last_pend
16360 && last_error_tinst_level == old_error_tinst)
16361 ggc_free (tinst);
16363 return r;
16366 /* Adjust types before performing type deduction, as described in
16367 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16368 sections are symmetric. PARM is the type of a function parameter
16369 or the return type of the conversion function. ARG is the type of
16370 the argument passed to the call, or the type of the value
16371 initialized with the result of the conversion function.
16372 ARG_EXPR is the original argument expression, which may be null. */
16374 static int
16375 maybe_adjust_types_for_deduction (unification_kind_t strict,
16376 tree* parm,
16377 tree* arg,
16378 tree arg_expr)
16380 int result = 0;
16382 switch (strict)
16384 case DEDUCE_CALL:
16385 break;
16387 case DEDUCE_CONV:
16389 /* Swap PARM and ARG throughout the remainder of this
16390 function; the handling is precisely symmetric since PARM
16391 will initialize ARG rather than vice versa. */
16392 tree* temp = parm;
16393 parm = arg;
16394 arg = temp;
16395 break;
16398 case DEDUCE_EXACT:
16399 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16400 too, but here handle it by stripping the reference from PARM
16401 rather than by adding it to ARG. */
16402 if (TREE_CODE (*parm) == REFERENCE_TYPE
16403 && TYPE_REF_IS_RVALUE (*parm)
16404 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16405 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16406 && TREE_CODE (*arg) == REFERENCE_TYPE
16407 && !TYPE_REF_IS_RVALUE (*arg))
16408 *parm = TREE_TYPE (*parm);
16409 /* Nothing else to do in this case. */
16410 return 0;
16412 default:
16413 gcc_unreachable ();
16416 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16418 /* [temp.deduct.call]
16420 If P is not a reference type:
16422 --If A is an array type, the pointer type produced by the
16423 array-to-pointer standard conversion (_conv.array_) is
16424 used in place of A for type deduction; otherwise,
16426 --If A is a function type, the pointer type produced by
16427 the function-to-pointer standard conversion
16428 (_conv.func_) is used in place of A for type deduction;
16429 otherwise,
16431 --If A is a cv-qualified type, the top level
16432 cv-qualifiers of A's type are ignored for type
16433 deduction. */
16434 if (TREE_CODE (*arg) == ARRAY_TYPE)
16435 *arg = build_pointer_type (TREE_TYPE (*arg));
16436 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16437 *arg = build_pointer_type (*arg);
16438 else
16439 *arg = TYPE_MAIN_VARIANT (*arg);
16442 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16443 of the form T&&, where T is a template parameter, and the argument
16444 is an lvalue, T is deduced as A& */
16445 if (TREE_CODE (*parm) == REFERENCE_TYPE
16446 && TYPE_REF_IS_RVALUE (*parm)
16447 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16448 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16449 && (arg_expr ? real_lvalue_p (arg_expr)
16450 /* try_one_overload doesn't provide an arg_expr, but
16451 functions are always lvalues. */
16452 : TREE_CODE (*arg) == FUNCTION_TYPE))
16453 *arg = build_reference_type (*arg);
16455 /* [temp.deduct.call]
16457 If P is a cv-qualified type, the top level cv-qualifiers
16458 of P's type are ignored for type deduction. If P is a
16459 reference type, the type referred to by P is used for
16460 type deduction. */
16461 *parm = TYPE_MAIN_VARIANT (*parm);
16462 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16464 *parm = TREE_TYPE (*parm);
16465 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16468 /* DR 322. For conversion deduction, remove a reference type on parm
16469 too (which has been swapped into ARG). */
16470 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16471 *arg = TREE_TYPE (*arg);
16473 return result;
16476 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16477 template which does contain any deducible template parameters; check if
16478 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16479 unify_one_argument. */
16481 static int
16482 check_non_deducible_conversion (tree parm, tree arg, int strict,
16483 int flags, bool explain_p)
16485 tree type;
16487 if (!TYPE_P (arg))
16488 type = TREE_TYPE (arg);
16489 else
16490 type = arg;
16492 if (same_type_p (parm, type))
16493 return unify_success (explain_p);
16495 if (strict == DEDUCE_CONV)
16497 if (can_convert_arg (type, parm, NULL_TREE, flags,
16498 explain_p ? tf_warning_or_error : tf_none))
16499 return unify_success (explain_p);
16501 else if (strict != DEDUCE_EXACT)
16503 if (can_convert_arg (parm, type,
16504 TYPE_P (arg) ? NULL_TREE : arg,
16505 flags, explain_p ? tf_warning_or_error : tf_none))
16506 return unify_success (explain_p);
16509 if (strict == DEDUCE_EXACT)
16510 return unify_type_mismatch (explain_p, parm, arg);
16511 else
16512 return unify_arg_conversion (explain_p, parm, type, arg);
16515 static bool uses_deducible_template_parms (tree type);
16517 /* Returns true iff the expression EXPR is one from which a template
16518 argument can be deduced. In other words, if it's an undecorated
16519 use of a template non-type parameter. */
16521 static bool
16522 deducible_expression (tree expr)
16524 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16527 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16528 deducible way; that is, if it has a max value of <PARM> - 1. */
16530 static bool
16531 deducible_array_bound (tree domain)
16533 if (domain == NULL_TREE)
16534 return false;
16536 tree max = TYPE_MAX_VALUE (domain);
16537 if (TREE_CODE (max) != MINUS_EXPR)
16538 return false;
16540 return deducible_expression (TREE_OPERAND (max, 0));
16543 /* Returns true iff the template arguments ARGS use a template parameter
16544 in a deducible way. */
16546 static bool
16547 deducible_template_args (tree args)
16549 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16551 bool deducible;
16552 tree elt = TREE_VEC_ELT (args, i);
16553 if (ARGUMENT_PACK_P (elt))
16554 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16555 else
16557 if (PACK_EXPANSION_P (elt))
16558 elt = PACK_EXPANSION_PATTERN (elt);
16559 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16560 deducible = true;
16561 else if (TYPE_P (elt))
16562 deducible = uses_deducible_template_parms (elt);
16563 else
16564 deducible = deducible_expression (elt);
16566 if (deducible)
16567 return true;
16569 return false;
16572 /* Returns true iff TYPE contains any deducible references to template
16573 parameters, as per 14.8.2.5. */
16575 static bool
16576 uses_deducible_template_parms (tree type)
16578 if (PACK_EXPANSION_P (type))
16579 type = PACK_EXPANSION_PATTERN (type);
16581 /* T
16582 cv-list T
16583 TT<T>
16584 TT<i>
16585 TT<> */
16586 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16587 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16588 return true;
16590 /* T*
16592 T&& */
16593 if (POINTER_TYPE_P (type))
16594 return uses_deducible_template_parms (TREE_TYPE (type));
16596 /* T[integer-constant ]
16597 type [i] */
16598 if (TREE_CODE (type) == ARRAY_TYPE)
16599 return (uses_deducible_template_parms (TREE_TYPE (type))
16600 || deducible_array_bound (TYPE_DOMAIN (type)));
16602 /* T type ::*
16603 type T::*
16604 T T::*
16605 T (type ::*)()
16606 type (T::*)()
16607 type (type ::*)(T)
16608 type (T::*)(T)
16609 T (type ::*)(T)
16610 T (T::*)()
16611 T (T::*)(T) */
16612 if (TYPE_PTRMEM_P (type))
16613 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16614 || (uses_deducible_template_parms
16615 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16617 /* template-name <T> (where template-name refers to a class template)
16618 template-name <i> (where template-name refers to a class template) */
16619 if (CLASS_TYPE_P (type)
16620 && CLASSTYPE_TEMPLATE_INFO (type)
16621 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16622 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16623 (CLASSTYPE_TI_ARGS (type)));
16625 /* type (T)
16627 T(T) */
16628 if (TREE_CODE (type) == FUNCTION_TYPE
16629 || TREE_CODE (type) == METHOD_TYPE)
16631 if (uses_deducible_template_parms (TREE_TYPE (type)))
16632 return true;
16633 tree parm = TYPE_ARG_TYPES (type);
16634 if (TREE_CODE (type) == METHOD_TYPE)
16635 parm = TREE_CHAIN (parm);
16636 for (; parm; parm = TREE_CHAIN (parm))
16637 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16638 return true;
16641 return false;
16644 /* Subroutine of type_unification_real and unify_pack_expansion to
16645 handle unification of a single P/A pair. Parameters are as
16646 for those functions. */
16648 static int
16649 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16650 int subr, unification_kind_t strict, int flags,
16651 bool explain_p)
16653 tree arg_expr = NULL_TREE;
16654 int arg_strict;
16656 if (arg == error_mark_node || parm == error_mark_node)
16657 return unify_invalid (explain_p);
16658 if (arg == unknown_type_node)
16659 /* We can't deduce anything from this, but we might get all the
16660 template args from other function args. */
16661 return unify_success (explain_p);
16663 /* Implicit conversions (Clause 4) will be performed on a function
16664 argument to convert it to the type of the corresponding function
16665 parameter if the parameter type contains no template-parameters that
16666 participate in template argument deduction. */
16667 if (TYPE_P (parm) && !uses_template_parms (parm))
16668 /* For function parameters that contain no template-parameters at all,
16669 we have historically checked for convertibility in order to shortcut
16670 consideration of this candidate. */
16671 return check_non_deducible_conversion (parm, arg, strict, flags,
16672 explain_p);
16673 else if (strict == DEDUCE_CALL
16674 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16675 /* For function parameters with only non-deducible template parameters,
16676 just return. */
16677 return unify_success (explain_p);
16679 switch (strict)
16681 case DEDUCE_CALL:
16682 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16683 | UNIFY_ALLOW_MORE_CV_QUAL
16684 | UNIFY_ALLOW_DERIVED);
16685 break;
16687 case DEDUCE_CONV:
16688 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16689 break;
16691 case DEDUCE_EXACT:
16692 arg_strict = UNIFY_ALLOW_NONE;
16693 break;
16695 default:
16696 gcc_unreachable ();
16699 /* We only do these transformations if this is the top-level
16700 parameter_type_list in a call or declaration matching; in other
16701 situations (nested function declarators, template argument lists) we
16702 won't be comparing a type to an expression, and we don't do any type
16703 adjustments. */
16704 if (!subr)
16706 if (!TYPE_P (arg))
16708 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16709 if (type_unknown_p (arg))
16711 /* [temp.deduct.type] A template-argument can be
16712 deduced from a pointer to function or pointer
16713 to member function argument if the set of
16714 overloaded functions does not contain function
16715 templates and at most one of a set of
16716 overloaded functions provides a unique
16717 match. */
16719 if (resolve_overloaded_unification
16720 (tparms, targs, parm, arg, strict,
16721 arg_strict, explain_p))
16722 return unify_success (explain_p);
16723 return unify_overload_resolution_failure (explain_p, arg);
16726 arg_expr = arg;
16727 arg = unlowered_expr_type (arg);
16728 if (arg == error_mark_node)
16729 return unify_invalid (explain_p);
16732 arg_strict |=
16733 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16735 else
16736 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16737 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16739 /* For deduction from an init-list we need the actual list. */
16740 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16741 arg = arg_expr;
16742 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16745 /* Most parms like fn_type_unification.
16747 If SUBR is 1, we're being called recursively (to unify the
16748 arguments of a function or method parameter of a function
16749 template).
16751 CHECKS is a pointer to a vector of access checks encountered while
16752 substituting default template arguments. */
16754 static int
16755 type_unification_real (tree tparms,
16756 tree targs,
16757 tree xparms,
16758 const tree *xargs,
16759 unsigned int xnargs,
16760 int subr,
16761 unification_kind_t strict,
16762 int flags,
16763 vec<deferred_access_check, va_gc> **checks,
16764 bool explain_p)
16766 tree parm, arg;
16767 int i;
16768 int ntparms = TREE_VEC_LENGTH (tparms);
16769 int saw_undeduced = 0;
16770 tree parms;
16771 const tree *args;
16772 unsigned int nargs;
16773 unsigned int ia;
16775 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16776 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16777 gcc_assert (ntparms > 0);
16779 /* Reset the number of non-defaulted template arguments contained
16780 in TARGS. */
16781 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16783 again:
16784 parms = xparms;
16785 args = xargs;
16786 nargs = xnargs;
16788 ia = 0;
16789 while (parms && parms != void_list_node
16790 && ia < nargs)
16792 parm = TREE_VALUE (parms);
16794 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16795 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16796 /* For a function parameter pack that occurs at the end of the
16797 parameter-declaration-list, the type A of each remaining
16798 argument of the call is compared with the type P of the
16799 declarator-id of the function parameter pack. */
16800 break;
16802 parms = TREE_CHAIN (parms);
16804 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16805 /* For a function parameter pack that does not occur at the
16806 end of the parameter-declaration-list, the type of the
16807 parameter pack is a non-deduced context. */
16808 continue;
16810 arg = args[ia];
16811 ++ia;
16813 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16814 flags, explain_p))
16815 return 1;
16818 if (parms
16819 && parms != void_list_node
16820 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16822 /* Unify the remaining arguments with the pack expansion type. */
16823 tree argvec;
16824 tree parmvec = make_tree_vec (1);
16826 /* Allocate a TREE_VEC and copy in all of the arguments */
16827 argvec = make_tree_vec (nargs - ia);
16828 for (i = 0; ia < nargs; ++ia, ++i)
16829 TREE_VEC_ELT (argvec, i) = args[ia];
16831 /* Copy the parameter into parmvec. */
16832 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16833 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16834 /*subr=*/subr, explain_p))
16835 return 1;
16837 /* Advance to the end of the list of parameters. */
16838 parms = TREE_CHAIN (parms);
16841 /* Fail if we've reached the end of the parm list, and more args
16842 are present, and the parm list isn't variadic. */
16843 if (ia < nargs && parms == void_list_node)
16844 return unify_too_many_arguments (explain_p, nargs, ia);
16845 /* Fail if parms are left and they don't have default values. */
16846 if (parms && parms != void_list_node
16847 && TREE_PURPOSE (parms) == NULL_TREE)
16849 unsigned int count = nargs;
16850 tree p = parms;
16851 while (p && p != void_list_node)
16853 count++;
16854 p = TREE_CHAIN (p);
16856 return unify_too_few_arguments (explain_p, ia, count);
16859 if (!subr)
16861 tsubst_flags_t complain = (explain_p
16862 ? tf_warning_or_error
16863 : tf_none);
16865 for (i = 0; i < ntparms; i++)
16867 tree targ = TREE_VEC_ELT (targs, i);
16868 tree tparm = TREE_VEC_ELT (tparms, i);
16870 /* Clear the "incomplete" flags on all argument packs now so that
16871 substituting them into later default arguments works. */
16872 if (targ && ARGUMENT_PACK_P (targ))
16874 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16875 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16878 if (targ || tparm == error_mark_node)
16879 continue;
16880 tparm = TREE_VALUE (tparm);
16882 /* If this is an undeduced nontype parameter that depends on
16883 a type parameter, try another pass; its type may have been
16884 deduced from a later argument than the one from which
16885 this parameter can be deduced. */
16886 if (TREE_CODE (tparm) == PARM_DECL
16887 && uses_template_parms (TREE_TYPE (tparm))
16888 && !saw_undeduced++)
16889 goto again;
16891 /* Core issue #226 (C++0x) [temp.deduct]:
16893 If a template argument has not been deduced, its
16894 default template argument, if any, is used.
16896 When we are in C++98 mode, TREE_PURPOSE will either
16897 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16898 to explicitly check cxx_dialect here. */
16899 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16901 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16902 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16903 reopen_deferring_access_checks (*checks);
16904 location_t save_loc = input_location;
16905 if (DECL_P (parm))
16906 input_location = DECL_SOURCE_LOCATION (parm);
16907 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16908 arg = convert_template_argument (parm, arg, targs, complain,
16909 i, NULL_TREE);
16910 input_location = save_loc;
16911 *checks = get_deferred_access_checks ();
16912 pop_deferring_access_checks ();
16913 if (arg == error_mark_node)
16914 return 1;
16915 else
16917 TREE_VEC_ELT (targs, i) = arg;
16918 /* The position of the first default template argument,
16919 is also the number of non-defaulted arguments in TARGS.
16920 Record that. */
16921 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16922 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16923 continue;
16927 /* If the type parameter is a parameter pack, then it will
16928 be deduced to an empty parameter pack. */
16929 if (template_parameter_pack_p (tparm))
16931 tree arg;
16933 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16935 arg = make_node (NONTYPE_ARGUMENT_PACK);
16936 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16937 TREE_CONSTANT (arg) = 1;
16939 else
16940 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16942 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16944 TREE_VEC_ELT (targs, i) = arg;
16945 continue;
16948 return unify_parameter_deduction_failure (explain_p, tparm);
16951 #ifdef ENABLE_CHECKING
16952 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16953 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16954 #endif
16956 return unify_success (explain_p);
16959 /* Subroutine of type_unification_real. Args are like the variables
16960 at the call site. ARG is an overloaded function (or template-id);
16961 we try deducing template args from each of the overloads, and if
16962 only one succeeds, we go with that. Modifies TARGS and returns
16963 true on success. */
16965 static bool
16966 resolve_overloaded_unification (tree tparms,
16967 tree targs,
16968 tree parm,
16969 tree arg,
16970 unification_kind_t strict,
16971 int sub_strict,
16972 bool explain_p)
16974 tree tempargs = copy_node (targs);
16975 int good = 0;
16976 tree goodfn = NULL_TREE;
16977 bool addr_p;
16979 if (TREE_CODE (arg) == ADDR_EXPR)
16981 arg = TREE_OPERAND (arg, 0);
16982 addr_p = true;
16984 else
16985 addr_p = false;
16987 if (TREE_CODE (arg) == COMPONENT_REF)
16988 /* Handle `&x' where `x' is some static or non-static member
16989 function name. */
16990 arg = TREE_OPERAND (arg, 1);
16992 if (TREE_CODE (arg) == OFFSET_REF)
16993 arg = TREE_OPERAND (arg, 1);
16995 /* Strip baselink information. */
16996 if (BASELINK_P (arg))
16997 arg = BASELINK_FUNCTIONS (arg);
16999 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
17001 /* If we got some explicit template args, we need to plug them into
17002 the affected templates before we try to unify, in case the
17003 explicit args will completely resolve the templates in question. */
17005 int ok = 0;
17006 tree expl_subargs = TREE_OPERAND (arg, 1);
17007 arg = TREE_OPERAND (arg, 0);
17009 for (; arg; arg = OVL_NEXT (arg))
17011 tree fn = OVL_CURRENT (arg);
17012 tree subargs, elem;
17014 if (TREE_CODE (fn) != TEMPLATE_DECL)
17015 continue;
17017 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17018 expl_subargs, NULL_TREE, tf_none,
17019 /*require_all_args=*/true,
17020 /*use_default_args=*/true);
17021 if (subargs != error_mark_node
17022 && !any_dependent_template_arguments_p (subargs))
17024 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
17025 if (try_one_overload (tparms, targs, tempargs, parm,
17026 elem, strict, sub_strict, addr_p, explain_p)
17027 && (!goodfn || !same_type_p (goodfn, elem)))
17029 goodfn = elem;
17030 ++good;
17033 else if (subargs)
17034 ++ok;
17036 /* If no templates (or more than one) are fully resolved by the
17037 explicit arguments, this template-id is a non-deduced context; it
17038 could still be OK if we deduce all template arguments for the
17039 enclosing call through other arguments. */
17040 if (good != 1)
17041 good = ok;
17043 else if (TREE_CODE (arg) != OVERLOAD
17044 && TREE_CODE (arg) != FUNCTION_DECL)
17045 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17046 -- but the deduction does not succeed because the expression is
17047 not just the function on its own. */
17048 return false;
17049 else
17050 for (; arg; arg = OVL_NEXT (arg))
17051 if (try_one_overload (tparms, targs, tempargs, parm,
17052 TREE_TYPE (OVL_CURRENT (arg)),
17053 strict, sub_strict, addr_p, explain_p)
17054 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
17056 goodfn = OVL_CURRENT (arg);
17057 ++good;
17060 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17061 to function or pointer to member function argument if the set of
17062 overloaded functions does not contain function templates and at most
17063 one of a set of overloaded functions provides a unique match.
17065 So if we found multiple possibilities, we return success but don't
17066 deduce anything. */
17068 if (good == 1)
17070 int i = TREE_VEC_LENGTH (targs);
17071 for (; i--; )
17072 if (TREE_VEC_ELT (tempargs, i))
17073 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
17075 if (good)
17076 return true;
17078 return false;
17081 /* Core DR 115: In contexts where deduction is done and fails, or in
17082 contexts where deduction is not done, if a template argument list is
17083 specified and it, along with any default template arguments, identifies
17084 a single function template specialization, then the template-id is an
17085 lvalue for the function template specialization. */
17087 tree
17088 resolve_nondeduced_context (tree orig_expr)
17090 tree expr, offset, baselink;
17091 bool addr;
17093 if (!type_unknown_p (orig_expr))
17094 return orig_expr;
17096 expr = orig_expr;
17097 addr = false;
17098 offset = NULL_TREE;
17099 baselink = NULL_TREE;
17101 if (TREE_CODE (expr) == ADDR_EXPR)
17103 expr = TREE_OPERAND (expr, 0);
17104 addr = true;
17106 if (TREE_CODE (expr) == OFFSET_REF)
17108 offset = expr;
17109 expr = TREE_OPERAND (expr, 1);
17111 if (BASELINK_P (expr))
17113 baselink = expr;
17114 expr = BASELINK_FUNCTIONS (expr);
17117 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
17119 int good = 0;
17120 tree goodfn = NULL_TREE;
17122 /* If we got some explicit template args, we need to plug them into
17123 the affected templates before we try to unify, in case the
17124 explicit args will completely resolve the templates in question. */
17126 tree expl_subargs = TREE_OPERAND (expr, 1);
17127 tree arg = TREE_OPERAND (expr, 0);
17128 tree badfn = NULL_TREE;
17129 tree badargs = NULL_TREE;
17131 for (; arg; arg = OVL_NEXT (arg))
17133 tree fn = OVL_CURRENT (arg);
17134 tree subargs, elem;
17136 if (TREE_CODE (fn) != TEMPLATE_DECL)
17137 continue;
17139 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17140 expl_subargs, NULL_TREE, tf_none,
17141 /*require_all_args=*/true,
17142 /*use_default_args=*/true);
17143 if (subargs != error_mark_node
17144 && !any_dependent_template_arguments_p (subargs))
17146 elem = instantiate_template (fn, subargs, tf_none);
17147 if (elem == error_mark_node)
17149 badfn = fn;
17150 badargs = subargs;
17152 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
17154 goodfn = elem;
17155 ++good;
17159 if (good == 1)
17161 mark_used (goodfn);
17162 expr = goodfn;
17163 if (baselink)
17164 expr = build_baselink (BASELINK_BINFO (baselink),
17165 BASELINK_ACCESS_BINFO (baselink),
17166 expr, BASELINK_OPTYPE (baselink));
17167 if (offset)
17169 tree base
17170 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
17171 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
17173 if (addr)
17174 expr = cp_build_addr_expr (expr, tf_warning_or_error);
17175 return expr;
17177 else if (good == 0 && badargs)
17178 /* There were no good options and at least one bad one, so let the
17179 user know what the problem is. */
17180 instantiate_template (badfn, badargs, tf_warning_or_error);
17182 return orig_expr;
17185 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17186 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17187 different overloads deduce different arguments for a given parm.
17188 ADDR_P is true if the expression for which deduction is being
17189 performed was of the form "& fn" rather than simply "fn".
17191 Returns 1 on success. */
17193 static int
17194 try_one_overload (tree tparms,
17195 tree orig_targs,
17196 tree targs,
17197 tree parm,
17198 tree arg,
17199 unification_kind_t strict,
17200 int sub_strict,
17201 bool addr_p,
17202 bool explain_p)
17204 int nargs;
17205 tree tempargs;
17206 int i;
17208 if (arg == error_mark_node)
17209 return 0;
17211 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17212 to function or pointer to member function argument if the set of
17213 overloaded functions does not contain function templates and at most
17214 one of a set of overloaded functions provides a unique match.
17216 So if this is a template, just return success. */
17218 if (uses_template_parms (arg))
17219 return 1;
17221 if (TREE_CODE (arg) == METHOD_TYPE)
17222 arg = build_ptrmemfunc_type (build_pointer_type (arg));
17223 else if (addr_p)
17224 arg = build_pointer_type (arg);
17226 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17228 /* We don't copy orig_targs for this because if we have already deduced
17229 some template args from previous args, unify would complain when we
17230 try to deduce a template parameter for the same argument, even though
17231 there isn't really a conflict. */
17232 nargs = TREE_VEC_LENGTH (targs);
17233 tempargs = make_tree_vec (nargs);
17235 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17236 return 0;
17238 /* First make sure we didn't deduce anything that conflicts with
17239 explicitly specified args. */
17240 for (i = nargs; i--; )
17242 tree elt = TREE_VEC_ELT (tempargs, i);
17243 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17245 if (!elt)
17246 /*NOP*/;
17247 else if (uses_template_parms (elt))
17248 /* Since we're unifying against ourselves, we will fill in
17249 template args used in the function parm list with our own
17250 template parms. Discard them. */
17251 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17252 else if (oldelt && !template_args_equal (oldelt, elt))
17253 return 0;
17256 for (i = nargs; i--; )
17258 tree elt = TREE_VEC_ELT (tempargs, i);
17260 if (elt)
17261 TREE_VEC_ELT (targs, i) = elt;
17264 return 1;
17267 /* PARM is a template class (perhaps with unbound template
17268 parameters). ARG is a fully instantiated type. If ARG can be
17269 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17270 TARGS are as for unify. */
17272 static tree
17273 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17274 bool explain_p)
17276 tree copy_of_targs;
17278 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17279 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17280 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17281 return NULL_TREE;
17283 /* We need to make a new template argument vector for the call to
17284 unify. If we used TARGS, we'd clutter it up with the result of
17285 the attempted unification, even if this class didn't work out.
17286 We also don't want to commit ourselves to all the unifications
17287 we've already done, since unification is supposed to be done on
17288 an argument-by-argument basis. In other words, consider the
17289 following pathological case:
17291 template <int I, int J, int K>
17292 struct S {};
17294 template <int I, int J>
17295 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17297 template <int I, int J, int K>
17298 void f(S<I, J, K>, S<I, I, I>);
17300 void g() {
17301 S<0, 0, 0> s0;
17302 S<0, 1, 2> s2;
17304 f(s0, s2);
17307 Now, by the time we consider the unification involving `s2', we
17308 already know that we must have `f<0, 0, 0>'. But, even though
17309 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17310 because there are two ways to unify base classes of S<0, 1, 2>
17311 with S<I, I, I>. If we kept the already deduced knowledge, we
17312 would reject the possibility I=1. */
17313 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17315 /* If unification failed, we're done. */
17316 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17317 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17318 return NULL_TREE;
17320 return arg;
17323 /* Given a template type PARM and a class type ARG, find the unique
17324 base type in ARG that is an instance of PARM. We do not examine
17325 ARG itself; only its base-classes. If there is not exactly one
17326 appropriate base class, return NULL_TREE. PARM may be the type of
17327 a partial specialization, as well as a plain template type. Used
17328 by unify. */
17330 static enum template_base_result
17331 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17332 bool explain_p, tree *result)
17334 tree rval = NULL_TREE;
17335 tree binfo;
17337 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17339 binfo = TYPE_BINFO (complete_type (arg));
17340 if (!binfo)
17342 /* The type could not be completed. */
17343 *result = NULL_TREE;
17344 return tbr_incomplete_type;
17347 /* Walk in inheritance graph order. The search order is not
17348 important, and this avoids multiple walks of virtual bases. */
17349 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17351 tree r = try_class_unification (tparms, targs, parm,
17352 BINFO_TYPE (binfo), explain_p);
17354 if (r)
17356 /* If there is more than one satisfactory baseclass, then:
17358 [temp.deduct.call]
17360 If they yield more than one possible deduced A, the type
17361 deduction fails.
17363 applies. */
17364 if (rval && !same_type_p (r, rval))
17366 *result = NULL_TREE;
17367 return tbr_ambiguous_baseclass;
17370 rval = r;
17374 *result = rval;
17375 return tbr_success;
17378 /* Returns the level of DECL, which declares a template parameter. */
17380 static int
17381 template_decl_level (tree decl)
17383 switch (TREE_CODE (decl))
17385 case TYPE_DECL:
17386 case TEMPLATE_DECL:
17387 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17389 case PARM_DECL:
17390 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17392 default:
17393 gcc_unreachable ();
17395 return 0;
17398 /* Decide whether ARG can be unified with PARM, considering only the
17399 cv-qualifiers of each type, given STRICT as documented for unify.
17400 Returns nonzero iff the unification is OK on that basis. */
17402 static int
17403 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17405 int arg_quals = cp_type_quals (arg);
17406 int parm_quals = cp_type_quals (parm);
17408 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17409 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17411 /* Although a CVR qualifier is ignored when being applied to a
17412 substituted template parameter ([8.3.2]/1 for example), that
17413 does not allow us to unify "const T" with "int&" because both
17414 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17415 It is ok when we're allowing additional CV qualifiers
17416 at the outer level [14.8.2.1]/3,1st bullet. */
17417 if ((TREE_CODE (arg) == REFERENCE_TYPE
17418 || TREE_CODE (arg) == FUNCTION_TYPE
17419 || TREE_CODE (arg) == METHOD_TYPE)
17420 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17421 return 0;
17423 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17424 && (parm_quals & TYPE_QUAL_RESTRICT))
17425 return 0;
17428 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17429 && (arg_quals & parm_quals) != parm_quals)
17430 return 0;
17432 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17433 && (parm_quals & arg_quals) != arg_quals)
17434 return 0;
17436 return 1;
17439 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17440 void
17441 template_parm_level_and_index (tree parm, int* level, int* index)
17443 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17444 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17445 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17447 *index = TEMPLATE_TYPE_IDX (parm);
17448 *level = TEMPLATE_TYPE_LEVEL (parm);
17450 else
17452 *index = TEMPLATE_PARM_IDX (parm);
17453 *level = TEMPLATE_PARM_LEVEL (parm);
17457 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17458 do { \
17459 if (unify (TP, TA, P, A, S, EP)) \
17460 return 1; \
17461 } while (0);
17463 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17464 expansion at the end of PACKED_PARMS. Returns 0 if the type
17465 deduction succeeds, 1 otherwise. STRICT is the same as in
17466 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17467 call argument list. We'll need to adjust the arguments to make them
17468 types. SUBR tells us if this is from a recursive call to
17469 type_unification_real, or for comparing two template argument
17470 lists. */
17472 static int
17473 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17474 tree packed_args, unification_kind_t strict,
17475 bool subr, bool explain_p)
17477 tree parm
17478 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17479 tree pattern = PACK_EXPANSION_PATTERN (parm);
17480 tree pack, packs = NULL_TREE;
17481 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17483 packed_args = expand_template_argument_pack (packed_args);
17485 int len = TREE_VEC_LENGTH (packed_args);
17487 /* Determine the parameter packs we will be deducing from the
17488 pattern, and record their current deductions. */
17489 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17490 pack; pack = TREE_CHAIN (pack))
17492 tree parm_pack = TREE_VALUE (pack);
17493 int idx, level;
17495 /* Determine the index and level of this parameter pack. */
17496 template_parm_level_and_index (parm_pack, &level, &idx);
17498 /* Keep track of the parameter packs and their corresponding
17499 argument packs. */
17500 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17501 TREE_TYPE (packs) = make_tree_vec (len - start);
17504 /* Loop through all of the arguments that have not yet been
17505 unified and unify each with the pattern. */
17506 for (i = start; i < len; i++)
17508 tree parm;
17509 bool any_explicit = false;
17510 tree arg = TREE_VEC_ELT (packed_args, i);
17512 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17513 or the element of its argument pack at the current index if
17514 this argument was explicitly specified. */
17515 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17517 int idx, level;
17518 tree arg, pargs;
17519 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17521 arg = NULL_TREE;
17522 if (TREE_VALUE (pack)
17523 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17524 && (i - start < TREE_VEC_LENGTH (pargs)))
17526 any_explicit = true;
17527 arg = TREE_VEC_ELT (pargs, i - start);
17529 TMPL_ARG (targs, level, idx) = arg;
17532 /* If we had explicit template arguments, substitute them into the
17533 pattern before deduction. */
17534 if (any_explicit)
17536 /* Some arguments might still be unspecified or dependent. */
17537 bool dependent;
17538 ++processing_template_decl;
17539 dependent = any_dependent_template_arguments_p (targs);
17540 if (!dependent)
17541 --processing_template_decl;
17542 parm = tsubst (pattern, targs,
17543 explain_p ? tf_warning_or_error : tf_none,
17544 NULL_TREE);
17545 if (dependent)
17546 --processing_template_decl;
17547 if (parm == error_mark_node)
17548 return 1;
17550 else
17551 parm = pattern;
17553 /* Unify the pattern with the current argument. */
17554 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17555 LOOKUP_IMPLICIT, explain_p))
17556 return 1;
17558 /* For each parameter pack, collect the deduced value. */
17559 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17561 int idx, level;
17562 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17564 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17565 TMPL_ARG (targs, level, idx);
17569 /* Verify that the results of unification with the parameter packs
17570 produce results consistent with what we've seen before, and make
17571 the deduced argument packs available. */
17572 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17574 tree old_pack = TREE_VALUE (pack);
17575 tree new_args = TREE_TYPE (pack);
17576 int i, len = TREE_VEC_LENGTH (new_args);
17577 int idx, level;
17578 bool nondeduced_p = false;
17580 /* By default keep the original deduced argument pack.
17581 If necessary, more specific code is going to update the
17582 resulting deduced argument later down in this function. */
17583 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17584 TMPL_ARG (targs, level, idx) = old_pack;
17586 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17587 actually deduce anything. */
17588 for (i = 0; i < len && !nondeduced_p; ++i)
17589 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17590 nondeduced_p = true;
17591 if (nondeduced_p)
17592 continue;
17594 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17596 /* If we had fewer function args than explicit template args,
17597 just use the explicits. */
17598 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17599 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17600 if (len < explicit_len)
17601 new_args = explicit_args;
17604 if (!old_pack)
17606 tree result;
17607 /* Build the deduced *_ARGUMENT_PACK. */
17608 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17610 result = make_node (NONTYPE_ARGUMENT_PACK);
17611 TREE_TYPE (result) =
17612 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17613 TREE_CONSTANT (result) = 1;
17615 else
17616 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17618 SET_ARGUMENT_PACK_ARGS (result, new_args);
17620 /* Note the deduced argument packs for this parameter
17621 pack. */
17622 TMPL_ARG (targs, level, idx) = result;
17624 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17625 && (ARGUMENT_PACK_ARGS (old_pack)
17626 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17628 /* We only had the explicitly-provided arguments before, but
17629 now we have a complete set of arguments. */
17630 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17632 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17633 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17634 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17636 else
17638 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17639 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17641 if (!comp_template_args_with_info (old_args, new_args,
17642 &bad_old_arg, &bad_new_arg))
17643 /* Inconsistent unification of this parameter pack. */
17644 return unify_parameter_pack_inconsistent (explain_p,
17645 bad_old_arg,
17646 bad_new_arg);
17650 return unify_success (explain_p);
17653 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17654 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17655 parameters and return value are as for unify. */
17657 static int
17658 unify_array_domain (tree tparms, tree targs,
17659 tree parm_dom, tree arg_dom,
17660 bool explain_p)
17662 tree parm_max;
17663 tree arg_max;
17664 bool parm_cst;
17665 bool arg_cst;
17667 /* Our representation of array types uses "N - 1" as the
17668 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17669 not an integer constant. We cannot unify arbitrarily
17670 complex expressions, so we eliminate the MINUS_EXPRs
17671 here. */
17672 parm_max = TYPE_MAX_VALUE (parm_dom);
17673 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17674 if (!parm_cst)
17676 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17677 parm_max = TREE_OPERAND (parm_max, 0);
17679 arg_max = TYPE_MAX_VALUE (arg_dom);
17680 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17681 if (!arg_cst)
17683 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17684 trying to unify the type of a variable with the type
17685 of a template parameter. For example:
17687 template <unsigned int N>
17688 void f (char (&) [N]);
17689 int g();
17690 void h(int i) {
17691 char a[g(i)];
17692 f(a);
17695 Here, the type of the ARG will be "int [g(i)]", and
17696 may be a SAVE_EXPR, etc. */
17697 if (TREE_CODE (arg_max) != MINUS_EXPR)
17698 return unify_vla_arg (explain_p, arg_dom);
17699 arg_max = TREE_OPERAND (arg_max, 0);
17702 /* If only one of the bounds used a MINUS_EXPR, compensate
17703 by adding one to the other bound. */
17704 if (parm_cst && !arg_cst)
17705 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17706 integer_type_node,
17707 parm_max,
17708 integer_one_node);
17709 else if (arg_cst && !parm_cst)
17710 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17711 integer_type_node,
17712 arg_max,
17713 integer_one_node);
17715 return unify (tparms, targs, parm_max, arg_max,
17716 UNIFY_ALLOW_INTEGER, explain_p);
17719 /* Deduce the value of template parameters. TPARMS is the (innermost)
17720 set of template parameters to a template. TARGS is the bindings
17721 for those template parameters, as determined thus far; TARGS may
17722 include template arguments for outer levels of template parameters
17723 as well. PARM is a parameter to a template function, or a
17724 subcomponent of that parameter; ARG is the corresponding argument.
17725 This function attempts to match PARM with ARG in a manner
17726 consistent with the existing assignments in TARGS. If more values
17727 are deduced, then TARGS is updated.
17729 Returns 0 if the type deduction succeeds, 1 otherwise. The
17730 parameter STRICT is a bitwise or of the following flags:
17732 UNIFY_ALLOW_NONE:
17733 Require an exact match between PARM and ARG.
17734 UNIFY_ALLOW_MORE_CV_QUAL:
17735 Allow the deduced ARG to be more cv-qualified (by qualification
17736 conversion) than ARG.
17737 UNIFY_ALLOW_LESS_CV_QUAL:
17738 Allow the deduced ARG to be less cv-qualified than ARG.
17739 UNIFY_ALLOW_DERIVED:
17740 Allow the deduced ARG to be a template base class of ARG,
17741 or a pointer to a template base class of the type pointed to by
17742 ARG.
17743 UNIFY_ALLOW_INTEGER:
17744 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17745 case for more information.
17746 UNIFY_ALLOW_OUTER_LEVEL:
17747 This is the outermost level of a deduction. Used to determine validity
17748 of qualification conversions. A valid qualification conversion must
17749 have const qualified pointers leading up to the inner type which
17750 requires additional CV quals, except at the outer level, where const
17751 is not required [conv.qual]. It would be normal to set this flag in
17752 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17753 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17754 This is the outermost level of a deduction, and PARM can be more CV
17755 qualified at this point.
17756 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17757 This is the outermost level of a deduction, and PARM can be less CV
17758 qualified at this point. */
17760 static int
17761 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17762 bool explain_p)
17764 int idx;
17765 tree targ;
17766 tree tparm;
17767 int strict_in = strict;
17769 /* I don't think this will do the right thing with respect to types.
17770 But the only case I've seen it in so far has been array bounds, where
17771 signedness is the only information lost, and I think that will be
17772 okay. */
17773 while (TREE_CODE (parm) == NOP_EXPR)
17774 parm = TREE_OPERAND (parm, 0);
17776 if (arg == error_mark_node)
17777 return unify_invalid (explain_p);
17778 if (arg == unknown_type_node
17779 || arg == init_list_type_node)
17780 /* We can't deduce anything from this, but we might get all the
17781 template args from other function args. */
17782 return unify_success (explain_p);
17784 /* If PARM uses template parameters, then we can't bail out here,
17785 even if ARG == PARM, since we won't record unifications for the
17786 template parameters. We might need them if we're trying to
17787 figure out which of two things is more specialized. */
17788 if (arg == parm && !uses_template_parms (parm))
17789 return unify_success (explain_p);
17791 /* Handle init lists early, so the rest of the function can assume
17792 we're dealing with a type. */
17793 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17795 tree elt, elttype;
17796 unsigned i;
17797 tree orig_parm = parm;
17799 /* Replace T with std::initializer_list<T> for deduction. */
17800 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17801 && flag_deduce_init_list)
17802 parm = listify (parm);
17804 if (!is_std_init_list (parm)
17805 && TREE_CODE (parm) != ARRAY_TYPE)
17806 /* We can only deduce from an initializer list argument if the
17807 parameter is std::initializer_list or an array; otherwise this
17808 is a non-deduced context. */
17809 return unify_success (explain_p);
17811 if (TREE_CODE (parm) == ARRAY_TYPE)
17812 elttype = TREE_TYPE (parm);
17813 else
17814 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17818 int elt_strict = strict;
17820 if (elt == error_mark_node)
17821 return unify_invalid (explain_p);
17823 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17825 tree type = TREE_TYPE (elt);
17826 /* It should only be possible to get here for a call. */
17827 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17828 elt_strict |= maybe_adjust_types_for_deduction
17829 (DEDUCE_CALL, &elttype, &type, elt);
17830 elt = type;
17833 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17834 explain_p);
17837 if (TREE_CODE (parm) == ARRAY_TYPE
17838 && deducible_array_bound (TYPE_DOMAIN (parm)))
17840 /* Also deduce from the length of the initializer list. */
17841 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17842 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17843 if (idx == error_mark_node)
17844 return unify_invalid (explain_p);
17845 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17846 idx, explain_p);
17849 /* If the std::initializer_list<T> deduction worked, replace the
17850 deduced A with std::initializer_list<A>. */
17851 if (orig_parm != parm)
17853 idx = TEMPLATE_TYPE_IDX (orig_parm);
17854 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17855 targ = listify (targ);
17856 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17858 return unify_success (explain_p);
17861 /* Immediately reject some pairs that won't unify because of
17862 cv-qualification mismatches. */
17863 if (TREE_CODE (arg) == TREE_CODE (parm)
17864 && TYPE_P (arg)
17865 /* It is the elements of the array which hold the cv quals of an array
17866 type, and the elements might be template type parms. We'll check
17867 when we recurse. */
17868 && TREE_CODE (arg) != ARRAY_TYPE
17869 /* We check the cv-qualifiers when unifying with template type
17870 parameters below. We want to allow ARG `const T' to unify with
17871 PARM `T' for example, when computing which of two templates
17872 is more specialized, for example. */
17873 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17874 && !check_cv_quals_for_unify (strict_in, arg, parm))
17875 return unify_cv_qual_mismatch (explain_p, parm, arg);
17877 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17878 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17879 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17880 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17881 strict &= ~UNIFY_ALLOW_DERIVED;
17882 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17883 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17885 switch (TREE_CODE (parm))
17887 case TYPENAME_TYPE:
17888 case SCOPE_REF:
17889 case UNBOUND_CLASS_TEMPLATE:
17890 /* In a type which contains a nested-name-specifier, template
17891 argument values cannot be deduced for template parameters used
17892 within the nested-name-specifier. */
17893 return unify_success (explain_p);
17895 case TEMPLATE_TYPE_PARM:
17896 case TEMPLATE_TEMPLATE_PARM:
17897 case BOUND_TEMPLATE_TEMPLATE_PARM:
17898 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17899 if (tparm == error_mark_node)
17900 return unify_invalid (explain_p);
17902 if (TEMPLATE_TYPE_LEVEL (parm)
17903 != template_decl_level (tparm))
17904 /* The PARM is not one we're trying to unify. Just check
17905 to see if it matches ARG. */
17907 if (TREE_CODE (arg) == TREE_CODE (parm)
17908 && (is_auto (parm) ? is_auto (arg)
17909 : same_type_p (parm, arg)))
17910 return unify_success (explain_p);
17911 else
17912 return unify_type_mismatch (explain_p, parm, arg);
17914 idx = TEMPLATE_TYPE_IDX (parm);
17915 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17916 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17917 if (tparm == error_mark_node)
17918 return unify_invalid (explain_p);
17920 /* Check for mixed types and values. */
17921 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17922 && TREE_CODE (tparm) != TYPE_DECL)
17923 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17924 && TREE_CODE (tparm) != TEMPLATE_DECL))
17925 gcc_unreachable ();
17927 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17929 /* ARG must be constructed from a template class or a template
17930 template parameter. */
17931 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17932 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17933 return unify_template_deduction_failure (explain_p, parm, arg);
17935 tree parmvec = TYPE_TI_ARGS (parm);
17936 /* An alias template name is never deduced. */
17937 if (TYPE_ALIAS_P (arg))
17938 arg = strip_typedefs (arg);
17939 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17940 tree full_argvec = add_to_template_args (targs, argvec);
17941 tree parm_parms
17942 = DECL_INNERMOST_TEMPLATE_PARMS
17943 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17944 int i, len;
17945 int parm_variadic_p = 0;
17947 /* The resolution to DR150 makes clear that default
17948 arguments for an N-argument may not be used to bind T
17949 to a template template parameter with fewer than N
17950 parameters. It is not safe to permit the binding of
17951 default arguments as an extension, as that may change
17952 the meaning of a conforming program. Consider:
17954 struct Dense { static const unsigned int dim = 1; };
17956 template <template <typename> class View,
17957 typename Block>
17958 void operator+(float, View<Block> const&);
17960 template <typename Block,
17961 unsigned int Dim = Block::dim>
17962 struct Lvalue_proxy { operator float() const; };
17964 void
17965 test_1d (void) {
17966 Lvalue_proxy<Dense> p;
17967 float b;
17968 b + p;
17971 Here, if Lvalue_proxy is permitted to bind to View, then
17972 the global operator+ will be used; if they are not, the
17973 Lvalue_proxy will be converted to float. */
17974 if (coerce_template_parms (parm_parms,
17975 full_argvec,
17976 TYPE_TI_TEMPLATE (parm),
17977 (explain_p
17978 ? tf_warning_or_error
17979 : tf_none),
17980 /*require_all_args=*/true,
17981 /*use_default_args=*/false)
17982 == error_mark_node)
17983 return 1;
17985 /* Deduce arguments T, i from TT<T> or TT<i>.
17986 We check each element of PARMVEC and ARGVEC individually
17987 rather than the whole TREE_VEC since they can have
17988 different number of elements. */
17990 parmvec = expand_template_argument_pack (parmvec);
17991 argvec = expand_template_argument_pack (argvec);
17993 len = TREE_VEC_LENGTH (parmvec);
17995 /* Check if the parameters end in a pack, making them
17996 variadic. */
17997 if (len > 0
17998 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17999 parm_variadic_p = 1;
18001 for (i = 0; i < len - parm_variadic_p; ++i)
18002 /* If the template argument list of P contains a pack
18003 expansion that is not the last template argument, the
18004 entire template argument list is a non-deduced
18005 context. */
18006 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
18007 return unify_success (explain_p);
18009 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
18010 return unify_too_few_arguments (explain_p,
18011 TREE_VEC_LENGTH (argvec), len);
18013 for (i = 0; i < len - parm_variadic_p; ++i)
18015 RECUR_AND_CHECK_FAILURE (tparms, targs,
18016 TREE_VEC_ELT (parmvec, i),
18017 TREE_VEC_ELT (argvec, i),
18018 UNIFY_ALLOW_NONE, explain_p);
18021 if (parm_variadic_p
18022 && unify_pack_expansion (tparms, targs,
18023 parmvec, argvec,
18024 DEDUCE_EXACT,
18025 /*subr=*/true, explain_p))
18026 return 1;
18028 arg = TYPE_TI_TEMPLATE (arg);
18030 /* Fall through to deduce template name. */
18033 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18034 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18036 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18038 /* Simple cases: Value already set, does match or doesn't. */
18039 if (targ != NULL_TREE && template_args_equal (targ, arg))
18040 return unify_success (explain_p);
18041 else if (targ)
18042 return unify_inconsistency (explain_p, parm, targ, arg);
18044 else
18046 /* If PARM is `const T' and ARG is only `int', we don't have
18047 a match unless we are allowing additional qualification.
18048 If ARG is `const int' and PARM is just `T' that's OK;
18049 that binds `const int' to `T'. */
18050 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
18051 arg, parm))
18052 return unify_cv_qual_mismatch (explain_p, parm, arg);
18054 /* Consider the case where ARG is `const volatile int' and
18055 PARM is `const T'. Then, T should be `volatile int'. */
18056 arg = cp_build_qualified_type_real
18057 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
18058 if (arg == error_mark_node)
18059 return unify_invalid (explain_p);
18061 /* Simple cases: Value already set, does match or doesn't. */
18062 if (targ != NULL_TREE && same_type_p (targ, arg))
18063 return unify_success (explain_p);
18064 else if (targ)
18065 return unify_inconsistency (explain_p, parm, targ, arg);
18067 /* Make sure that ARG is not a variable-sized array. (Note
18068 that were talking about variable-sized arrays (like
18069 `int[n]'), rather than arrays of unknown size (like
18070 `int[]').) We'll get very confused by such a type since
18071 the bound of the array is not constant, and therefore
18072 not mangleable. Besides, such types are not allowed in
18073 ISO C++, so we can do as we please here. We do allow
18074 them for 'auto' deduction, since that isn't ABI-exposed. */
18075 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
18076 return unify_vla_arg (explain_p, arg);
18078 /* Strip typedefs as in convert_template_argument. */
18079 arg = canonicalize_type_argument (arg, tf_none);
18082 /* If ARG is a parameter pack or an expansion, we cannot unify
18083 against it unless PARM is also a parameter pack. */
18084 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18085 && !template_parameter_pack_p (parm))
18086 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18088 /* If the argument deduction results is a METHOD_TYPE,
18089 then there is a problem.
18090 METHOD_TYPE doesn't map to any real C++ type the result of
18091 the deduction can not be of that type. */
18092 if (TREE_CODE (arg) == METHOD_TYPE)
18093 return unify_method_type_error (explain_p, arg);
18095 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18096 return unify_success (explain_p);
18098 case TEMPLATE_PARM_INDEX:
18099 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18100 if (tparm == error_mark_node)
18101 return unify_invalid (explain_p);
18103 if (TEMPLATE_PARM_LEVEL (parm)
18104 != template_decl_level (tparm))
18106 /* The PARM is not one we're trying to unify. Just check
18107 to see if it matches ARG. */
18108 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
18109 && cp_tree_equal (parm, arg));
18110 if (result)
18111 unify_expression_unequal (explain_p, parm, arg);
18112 return result;
18115 idx = TEMPLATE_PARM_IDX (parm);
18116 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18118 if (targ)
18120 int x = !cp_tree_equal (targ, arg);
18121 if (x)
18122 unify_inconsistency (explain_p, parm, targ, arg);
18123 return x;
18126 /* [temp.deduct.type] If, in the declaration of a function template
18127 with a non-type template-parameter, the non-type
18128 template-parameter is used in an expression in the function
18129 parameter-list and, if the corresponding template-argument is
18130 deduced, the template-argument type shall match the type of the
18131 template-parameter exactly, except that a template-argument
18132 deduced from an array bound may be of any integral type.
18133 The non-type parameter might use already deduced type parameters. */
18134 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
18135 if (!TREE_TYPE (arg))
18136 /* Template-parameter dependent expression. Just accept it for now.
18137 It will later be processed in convert_template_argument. */
18139 else if (same_type_p (TREE_TYPE (arg), tparm))
18140 /* OK */;
18141 else if ((strict & UNIFY_ALLOW_INTEGER)
18142 && CP_INTEGRAL_TYPE_P (tparm))
18143 /* Convert the ARG to the type of PARM; the deduced non-type
18144 template argument must exactly match the types of the
18145 corresponding parameter. */
18146 arg = fold (build_nop (tparm, arg));
18147 else if (uses_template_parms (tparm))
18148 /* We haven't deduced the type of this parameter yet. Try again
18149 later. */
18150 return unify_success (explain_p);
18151 else
18152 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
18154 /* If ARG is a parameter pack or an expansion, we cannot unify
18155 against it unless PARM is also a parameter pack. */
18156 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18157 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
18158 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18160 arg = strip_typedefs_expr (arg);
18161 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18162 return unify_success (explain_p);
18164 case PTRMEM_CST:
18166 /* A pointer-to-member constant can be unified only with
18167 another constant. */
18168 if (TREE_CODE (arg) != PTRMEM_CST)
18169 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
18171 /* Just unify the class member. It would be useless (and possibly
18172 wrong, depending on the strict flags) to unify also
18173 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18174 arg refer to the same variable, even if through different
18175 classes. For instance:
18177 struct A { int x; };
18178 struct B : A { };
18180 Unification of &A::x and &B::x must succeed. */
18181 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
18182 PTRMEM_CST_MEMBER (arg), strict, explain_p);
18185 case POINTER_TYPE:
18187 if (!TYPE_PTR_P (arg))
18188 return unify_type_mismatch (explain_p, parm, arg);
18190 /* [temp.deduct.call]
18192 A can be another pointer or pointer to member type that can
18193 be converted to the deduced A via a qualification
18194 conversion (_conv.qual_).
18196 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18197 This will allow for additional cv-qualification of the
18198 pointed-to types if appropriate. */
18200 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
18201 /* The derived-to-base conversion only persists through one
18202 level of pointers. */
18203 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
18205 return unify (tparms, targs, TREE_TYPE (parm),
18206 TREE_TYPE (arg), strict, explain_p);
18209 case REFERENCE_TYPE:
18210 if (TREE_CODE (arg) != REFERENCE_TYPE)
18211 return unify_type_mismatch (explain_p, parm, arg);
18212 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18213 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18215 case ARRAY_TYPE:
18216 if (TREE_CODE (arg) != ARRAY_TYPE)
18217 return unify_type_mismatch (explain_p, parm, arg);
18218 if ((TYPE_DOMAIN (parm) == NULL_TREE)
18219 != (TYPE_DOMAIN (arg) == NULL_TREE))
18220 return unify_type_mismatch (explain_p, parm, arg);
18221 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18222 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18223 if (TYPE_DOMAIN (parm) != NULL_TREE)
18224 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18225 TYPE_DOMAIN (arg), explain_p);
18226 return unify_success (explain_p);
18228 case REAL_TYPE:
18229 case COMPLEX_TYPE:
18230 case VECTOR_TYPE:
18231 case INTEGER_TYPE:
18232 case BOOLEAN_TYPE:
18233 case ENUMERAL_TYPE:
18234 case VOID_TYPE:
18235 case NULLPTR_TYPE:
18236 if (TREE_CODE (arg) != TREE_CODE (parm))
18237 return unify_type_mismatch (explain_p, parm, arg);
18239 /* We have already checked cv-qualification at the top of the
18240 function. */
18241 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18242 return unify_type_mismatch (explain_p, parm, arg);
18244 /* As far as unification is concerned, this wins. Later checks
18245 will invalidate it if necessary. */
18246 return unify_success (explain_p);
18248 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18249 /* Type INTEGER_CST can come from ordinary constant template args. */
18250 case INTEGER_CST:
18251 while (TREE_CODE (arg) == NOP_EXPR)
18252 arg = TREE_OPERAND (arg, 0);
18254 if (TREE_CODE (arg) != INTEGER_CST)
18255 return unify_template_argument_mismatch (explain_p, parm, arg);
18256 return (tree_int_cst_equal (parm, arg)
18257 ? unify_success (explain_p)
18258 : unify_template_argument_mismatch (explain_p, parm, arg));
18260 case TREE_VEC:
18262 int i, len, argslen;
18263 int parm_variadic_p = 0;
18265 if (TREE_CODE (arg) != TREE_VEC)
18266 return unify_template_argument_mismatch (explain_p, parm, arg);
18268 len = TREE_VEC_LENGTH (parm);
18269 argslen = TREE_VEC_LENGTH (arg);
18271 /* Check for pack expansions in the parameters. */
18272 for (i = 0; i < len; ++i)
18274 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18276 if (i == len - 1)
18277 /* We can unify against something with a trailing
18278 parameter pack. */
18279 parm_variadic_p = 1;
18280 else
18281 /* [temp.deduct.type]/9: If the template argument list of
18282 P contains a pack expansion that is not the last
18283 template argument, the entire template argument list
18284 is a non-deduced context. */
18285 return unify_success (explain_p);
18289 /* If we don't have enough arguments to satisfy the parameters
18290 (not counting the pack expression at the end), or we have
18291 too many arguments for a parameter list that doesn't end in
18292 a pack expression, we can't unify. */
18293 if (parm_variadic_p
18294 ? argslen < len - parm_variadic_p
18295 : argslen != len)
18296 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18298 /* Unify all of the parameters that precede the (optional)
18299 pack expression. */
18300 for (i = 0; i < len - parm_variadic_p; ++i)
18302 RECUR_AND_CHECK_FAILURE (tparms, targs,
18303 TREE_VEC_ELT (parm, i),
18304 TREE_VEC_ELT (arg, i),
18305 UNIFY_ALLOW_NONE, explain_p);
18307 if (parm_variadic_p)
18308 return unify_pack_expansion (tparms, targs, parm, arg,
18309 DEDUCE_EXACT,
18310 /*subr=*/true, explain_p);
18311 return unify_success (explain_p);
18314 case RECORD_TYPE:
18315 case UNION_TYPE:
18316 if (TREE_CODE (arg) != TREE_CODE (parm))
18317 return unify_type_mismatch (explain_p, parm, arg);
18319 if (TYPE_PTRMEMFUNC_P (parm))
18321 if (!TYPE_PTRMEMFUNC_P (arg))
18322 return unify_type_mismatch (explain_p, parm, arg);
18324 return unify (tparms, targs,
18325 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18326 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18327 strict, explain_p);
18330 if (CLASSTYPE_TEMPLATE_INFO (parm))
18332 tree t = NULL_TREE;
18334 if (strict_in & UNIFY_ALLOW_DERIVED)
18336 /* First, we try to unify the PARM and ARG directly. */
18337 t = try_class_unification (tparms, targs,
18338 parm, arg, explain_p);
18340 if (!t)
18342 /* Fallback to the special case allowed in
18343 [temp.deduct.call]:
18345 If P is a class, and P has the form
18346 template-id, then A can be a derived class of
18347 the deduced A. Likewise, if P is a pointer to
18348 a class of the form template-id, A can be a
18349 pointer to a derived class pointed to by the
18350 deduced A. */
18351 enum template_base_result r;
18352 r = get_template_base (tparms, targs, parm, arg,
18353 explain_p, &t);
18355 if (!t)
18356 return unify_no_common_base (explain_p, r, parm, arg);
18359 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18360 && (CLASSTYPE_TI_TEMPLATE (parm)
18361 == CLASSTYPE_TI_TEMPLATE (arg)))
18362 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18363 Then, we should unify `int' and `U'. */
18364 t = arg;
18365 else
18366 /* There's no chance of unification succeeding. */
18367 return unify_type_mismatch (explain_p, parm, arg);
18369 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18370 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18372 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18373 return unify_type_mismatch (explain_p, parm, arg);
18374 return unify_success (explain_p);
18376 case METHOD_TYPE:
18377 case FUNCTION_TYPE:
18379 unsigned int nargs;
18380 tree *args;
18381 tree a;
18382 unsigned int i;
18384 if (TREE_CODE (arg) != TREE_CODE (parm))
18385 return unify_type_mismatch (explain_p, parm, arg);
18387 /* CV qualifications for methods can never be deduced, they must
18388 match exactly. We need to check them explicitly here,
18389 because type_unification_real treats them as any other
18390 cv-qualified parameter. */
18391 if (TREE_CODE (parm) == METHOD_TYPE
18392 && (!check_cv_quals_for_unify
18393 (UNIFY_ALLOW_NONE,
18394 class_of_this_parm (arg),
18395 class_of_this_parm (parm))))
18396 return unify_cv_qual_mismatch (explain_p, parm, arg);
18398 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18399 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18401 nargs = list_length (TYPE_ARG_TYPES (arg));
18402 args = XALLOCAVEC (tree, nargs);
18403 for (a = TYPE_ARG_TYPES (arg), i = 0;
18404 a != NULL_TREE && a != void_list_node;
18405 a = TREE_CHAIN (a), ++i)
18406 args[i] = TREE_VALUE (a);
18407 nargs = i;
18409 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18410 args, nargs, 1, DEDUCE_EXACT,
18411 LOOKUP_NORMAL, NULL, explain_p);
18414 case OFFSET_TYPE:
18415 /* Unify a pointer to member with a pointer to member function, which
18416 deduces the type of the member as a function type. */
18417 if (TYPE_PTRMEMFUNC_P (arg))
18419 /* Check top-level cv qualifiers */
18420 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18421 return unify_cv_qual_mismatch (explain_p, parm, arg);
18423 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18424 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18425 UNIFY_ALLOW_NONE, explain_p);
18427 /* Determine the type of the function we are unifying against. */
18428 tree fntype = static_fn_type (arg);
18430 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18433 if (TREE_CODE (arg) != OFFSET_TYPE)
18434 return unify_type_mismatch (explain_p, parm, arg);
18435 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18436 TYPE_OFFSET_BASETYPE (arg),
18437 UNIFY_ALLOW_NONE, explain_p);
18438 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18439 strict, explain_p);
18441 case CONST_DECL:
18442 if (DECL_TEMPLATE_PARM_P (parm))
18443 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18444 if (arg != integral_constant_value (parm))
18445 return unify_template_argument_mismatch (explain_p, parm, arg);
18446 return unify_success (explain_p);
18448 case FIELD_DECL:
18449 case TEMPLATE_DECL:
18450 /* Matched cases are handled by the ARG == PARM test above. */
18451 return unify_template_argument_mismatch (explain_p, parm, arg);
18453 case VAR_DECL:
18454 /* A non-type template parameter that is a variable should be a
18455 an integral constant, in which case, it whould have been
18456 folded into its (constant) value. So we should not be getting
18457 a variable here. */
18458 gcc_unreachable ();
18460 case TYPE_ARGUMENT_PACK:
18461 case NONTYPE_ARGUMENT_PACK:
18462 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18463 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18465 case TYPEOF_TYPE:
18466 case DECLTYPE_TYPE:
18467 case UNDERLYING_TYPE:
18468 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18469 or UNDERLYING_TYPE nodes. */
18470 return unify_success (explain_p);
18472 case ERROR_MARK:
18473 /* Unification fails if we hit an error node. */
18474 return unify_invalid (explain_p);
18476 case INDIRECT_REF:
18477 if (REFERENCE_REF_P (parm))
18478 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18479 strict, explain_p);
18480 /* FALLTHRU */
18482 default:
18483 /* An unresolved overload is a nondeduced context. */
18484 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18485 return unify_success (explain_p);
18486 gcc_assert (EXPR_P (parm));
18488 /* We must be looking at an expression. This can happen with
18489 something like:
18491 template <int I>
18492 void foo(S<I>, S<I + 2>);
18494 This is a "nondeduced context":
18496 [deduct.type]
18498 The nondeduced contexts are:
18500 --A type that is a template-id in which one or more of
18501 the template-arguments is an expression that references
18502 a template-parameter.
18504 In these cases, we assume deduction succeeded, but don't
18505 actually infer any unifications. */
18507 if (!uses_template_parms (parm)
18508 && !template_args_equal (parm, arg))
18509 return unify_expression_unequal (explain_p, parm, arg);
18510 else
18511 return unify_success (explain_p);
18514 #undef RECUR_AND_CHECK_FAILURE
18516 /* Note that DECL can be defined in this translation unit, if
18517 required. */
18519 static void
18520 mark_definable (tree decl)
18522 tree clone;
18523 DECL_NOT_REALLY_EXTERN (decl) = 1;
18524 FOR_EACH_CLONE (clone, decl)
18525 DECL_NOT_REALLY_EXTERN (clone) = 1;
18528 /* Called if RESULT is explicitly instantiated, or is a member of an
18529 explicitly instantiated class. */
18531 void
18532 mark_decl_instantiated (tree result, int extern_p)
18534 SET_DECL_EXPLICIT_INSTANTIATION (result);
18536 /* If this entity has already been written out, it's too late to
18537 make any modifications. */
18538 if (TREE_ASM_WRITTEN (result))
18539 return;
18541 /* For anonymous namespace we don't need to do anything. */
18542 if (decl_anon_ns_mem_p (result))
18544 gcc_assert (!TREE_PUBLIC (result));
18545 return;
18548 if (TREE_CODE (result) != FUNCTION_DECL)
18549 /* The TREE_PUBLIC flag for function declarations will have been
18550 set correctly by tsubst. */
18551 TREE_PUBLIC (result) = 1;
18553 /* This might have been set by an earlier implicit instantiation. */
18554 DECL_COMDAT (result) = 0;
18556 if (extern_p)
18557 DECL_NOT_REALLY_EXTERN (result) = 0;
18558 else
18560 mark_definable (result);
18561 mark_needed (result);
18562 /* Always make artificials weak. */
18563 if (DECL_ARTIFICIAL (result) && flag_weak)
18564 comdat_linkage (result);
18565 /* For WIN32 we also want to put explicit instantiations in
18566 linkonce sections. */
18567 else if (TREE_PUBLIC (result))
18568 maybe_make_one_only (result);
18571 /* If EXTERN_P, then this function will not be emitted -- unless
18572 followed by an explicit instantiation, at which point its linkage
18573 will be adjusted. If !EXTERN_P, then this function will be
18574 emitted here. In neither circumstance do we want
18575 import_export_decl to adjust the linkage. */
18576 DECL_INTERFACE_KNOWN (result) = 1;
18579 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18580 important template arguments. If any are missing, we check whether
18581 they're important by using error_mark_node for substituting into any
18582 args that were used for partial ordering (the ones between ARGS and END)
18583 and seeing if it bubbles up. */
18585 static bool
18586 check_undeduced_parms (tree targs, tree args, tree end)
18588 bool found = false;
18589 int i;
18590 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18591 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18593 found = true;
18594 TREE_VEC_ELT (targs, i) = error_mark_node;
18596 if (found)
18598 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18599 if (substed == error_mark_node)
18600 return true;
18602 return false;
18605 /* Given two function templates PAT1 and PAT2, return:
18607 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18608 -1 if PAT2 is more specialized than PAT1.
18609 0 if neither is more specialized.
18611 LEN indicates the number of parameters we should consider
18612 (defaulted parameters should not be considered).
18614 The 1998 std underspecified function template partial ordering, and
18615 DR214 addresses the issue. We take pairs of arguments, one from
18616 each of the templates, and deduce them against each other. One of
18617 the templates will be more specialized if all the *other*
18618 template's arguments deduce against its arguments and at least one
18619 of its arguments *does* *not* deduce against the other template's
18620 corresponding argument. Deduction is done as for class templates.
18621 The arguments used in deduction have reference and top level cv
18622 qualifiers removed. Iff both arguments were originally reference
18623 types *and* deduction succeeds in both directions, an lvalue reference
18624 wins against an rvalue reference and otherwise the template
18625 with the more cv-qualified argument wins for that pairing (if
18626 neither is more cv-qualified, they both are equal). Unlike regular
18627 deduction, after all the arguments have been deduced in this way,
18628 we do *not* verify the deduced template argument values can be
18629 substituted into non-deduced contexts.
18631 The logic can be a bit confusing here, because we look at deduce1 and
18632 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18633 can find template arguments for pat1 to make arg1 look like arg2, that
18634 means that arg2 is at least as specialized as arg1. */
18637 more_specialized_fn (tree pat1, tree pat2, int len)
18639 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18640 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18641 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18642 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18643 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18644 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18645 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18646 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18647 tree origs1, origs2;
18648 bool lose1 = false;
18649 bool lose2 = false;
18651 /* Remove the this parameter from non-static member functions. If
18652 one is a non-static member function and the other is not a static
18653 member function, remove the first parameter from that function
18654 also. This situation occurs for operator functions where we
18655 locate both a member function (with this pointer) and non-member
18656 operator (with explicit first operand). */
18657 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18659 len--; /* LEN is the number of significant arguments for DECL1 */
18660 args1 = TREE_CHAIN (args1);
18661 if (!DECL_STATIC_FUNCTION_P (decl2))
18662 args2 = TREE_CHAIN (args2);
18664 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18666 args2 = TREE_CHAIN (args2);
18667 if (!DECL_STATIC_FUNCTION_P (decl1))
18669 len--;
18670 args1 = TREE_CHAIN (args1);
18674 /* If only one is a conversion operator, they are unordered. */
18675 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18676 return 0;
18678 /* Consider the return type for a conversion function */
18679 if (DECL_CONV_FN_P (decl1))
18681 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18682 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18683 len++;
18686 processing_template_decl++;
18688 origs1 = args1;
18689 origs2 = args2;
18691 while (len--
18692 /* Stop when an ellipsis is seen. */
18693 && args1 != NULL_TREE && args2 != NULL_TREE)
18695 tree arg1 = TREE_VALUE (args1);
18696 tree arg2 = TREE_VALUE (args2);
18697 int deduce1, deduce2;
18698 int quals1 = -1;
18699 int quals2 = -1;
18700 int ref1 = 0;
18701 int ref2 = 0;
18703 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18704 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18706 /* When both arguments are pack expansions, we need only
18707 unify the patterns themselves. */
18708 arg1 = PACK_EXPANSION_PATTERN (arg1);
18709 arg2 = PACK_EXPANSION_PATTERN (arg2);
18711 /* This is the last comparison we need to do. */
18712 len = 0;
18715 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18717 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18718 arg1 = TREE_TYPE (arg1);
18719 quals1 = cp_type_quals (arg1);
18722 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18724 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18725 arg2 = TREE_TYPE (arg2);
18726 quals2 = cp_type_quals (arg2);
18729 arg1 = TYPE_MAIN_VARIANT (arg1);
18730 arg2 = TYPE_MAIN_VARIANT (arg2);
18732 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18734 int i, len2 = list_length (args2);
18735 tree parmvec = make_tree_vec (1);
18736 tree argvec = make_tree_vec (len2);
18737 tree ta = args2;
18739 /* Setup the parameter vector, which contains only ARG1. */
18740 TREE_VEC_ELT (parmvec, 0) = arg1;
18742 /* Setup the argument vector, which contains the remaining
18743 arguments. */
18744 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18745 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18747 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18748 argvec, DEDUCE_EXACT,
18749 /*subr=*/true, /*explain_p=*/false)
18750 == 0);
18752 /* We cannot deduce in the other direction, because ARG1 is
18753 a pack expansion but ARG2 is not. */
18754 deduce2 = 0;
18756 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18758 int i, len1 = list_length (args1);
18759 tree parmvec = make_tree_vec (1);
18760 tree argvec = make_tree_vec (len1);
18761 tree ta = args1;
18763 /* Setup the parameter vector, which contains only ARG1. */
18764 TREE_VEC_ELT (parmvec, 0) = arg2;
18766 /* Setup the argument vector, which contains the remaining
18767 arguments. */
18768 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18769 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18771 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18772 argvec, DEDUCE_EXACT,
18773 /*subr=*/true, /*explain_p=*/false)
18774 == 0);
18776 /* We cannot deduce in the other direction, because ARG2 is
18777 a pack expansion but ARG1 is not.*/
18778 deduce1 = 0;
18781 else
18783 /* The normal case, where neither argument is a pack
18784 expansion. */
18785 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18786 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18787 == 0);
18788 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18789 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18790 == 0);
18793 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18794 arg2, then arg2 is not as specialized as arg1. */
18795 if (!deduce1)
18796 lose2 = true;
18797 if (!deduce2)
18798 lose1 = true;
18800 /* "If, for a given type, deduction succeeds in both directions
18801 (i.e., the types are identical after the transformations above)
18802 and both P and A were reference types (before being replaced with
18803 the type referred to above):
18804 - if the type from the argument template was an lvalue reference and
18805 the type from the parameter template was not, the argument type is
18806 considered to be more specialized than the other; otherwise,
18807 - if the type from the argument template is more cv-qualified
18808 than the type from the parameter template (as described above),
18809 the argument type is considered to be more specialized than the other;
18810 otherwise,
18811 - neither type is more specialized than the other." */
18813 if (deduce1 && deduce2)
18815 if (ref1 && ref2 && ref1 != ref2)
18817 if (ref1 > ref2)
18818 lose1 = true;
18819 else
18820 lose2 = true;
18822 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18824 if ((quals1 & quals2) == quals2)
18825 lose2 = true;
18826 if ((quals1 & quals2) == quals1)
18827 lose1 = true;
18831 if (lose1 && lose2)
18832 /* We've failed to deduce something in either direction.
18833 These must be unordered. */
18834 break;
18836 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18837 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18838 /* We have already processed all of the arguments in our
18839 handing of the pack expansion type. */
18840 len = 0;
18842 args1 = TREE_CHAIN (args1);
18843 args2 = TREE_CHAIN (args2);
18846 /* "In most cases, all template parameters must have values in order for
18847 deduction to succeed, but for partial ordering purposes a template
18848 parameter may remain without a value provided it is not used in the
18849 types being used for partial ordering."
18851 Thus, if we are missing any of the targs1 we need to substitute into
18852 origs1, then pat2 is not as specialized as pat1. This can happen when
18853 there is a nondeduced context. */
18854 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18855 lose2 = true;
18856 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18857 lose1 = true;
18859 processing_template_decl--;
18861 /* All things being equal, if the next argument is a pack expansion
18862 for one function but not for the other, prefer the
18863 non-variadic function. FIXME this is bogus; see c++/41958. */
18864 if (lose1 == lose2
18865 && args1 && TREE_VALUE (args1)
18866 && args2 && TREE_VALUE (args2))
18868 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18869 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18872 // All things still being equal, determine if one is more constrained.
18873 if (lose1 == lose2)
18875 lose1 = !more_constrained (pat1, pat2);
18876 lose2 = !more_constrained (pat2, pat1);
18879 if (lose1 == lose2)
18880 return 0;
18881 else if (!lose1)
18882 return 1;
18883 else
18884 return -1;
18887 /* Determine which of two partial specializations of TMPL is more
18888 specialized.
18890 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18891 to the first partial specialization. The TREE_VALUE is the
18892 innermost set of template parameters for the partial
18893 specialization. PAT2 is similar, but for the second template.
18895 Return 1 if the first partial specialization is more specialized;
18896 -1 if the second is more specialized; 0 if neither is more
18897 specialized.
18899 See [temp.class.order] for information about determining which of
18900 two templates is more specialized. */
18902 static int
18903 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18905 tree targs;
18906 int winner = 0;
18907 bool any_deductions = false;
18909 tree type1 = TREE_TYPE (pat1);
18910 tree type2 = TREE_TYPE (pat2);
18911 tree tmpl1 = get_specializing_template_decl (type1);
18912 tree tmpl2 = get_specializing_template_decl (type2);
18914 /* Just like what happens for functions, if we are ordering between
18915 different class template specializations, we may encounter dependent
18916 types in the arguments, and we need our dependency check functions
18917 to behave correctly. */
18918 ++processing_template_decl;
18919 targs = get_class_bindings (tmpl, tmpl1, TREE_VALUE (pat1),
18920 CLASSTYPE_TI_ARGS (type1),
18921 CLASSTYPE_TI_ARGS (type2));
18922 if (targs)
18924 --winner;
18925 any_deductions = true;
18928 targs = get_class_bindings (tmpl, tmpl2, TREE_VALUE (pat2),
18929 CLASSTYPE_TI_ARGS (type2),
18930 CLASSTYPE_TI_ARGS (type1));
18931 if (targs)
18933 ++winner;
18934 any_deductions = true;
18936 --processing_template_decl;
18938 /* In the case of a tie where at least one of the class templates
18939 has a parameter pack at the end, the template with the most
18940 non-packed parameters wins. */
18941 if (winner == 0
18942 && any_deductions
18943 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18944 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18946 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18947 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18948 int len1 = TREE_VEC_LENGTH (args1);
18949 int len2 = TREE_VEC_LENGTH (args2);
18951 /* We don't count the pack expansion at the end. */
18952 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18953 --len1;
18954 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18955 --len2;
18957 if (len1 > len2)
18958 return 1;
18959 else if (len1 < len2)
18960 return -1;
18963 if (more_constrained (tmpl1, tmpl2))
18964 ++winner;
18965 if (more_constrained (tmpl2, tmpl1))
18966 --winner;
18968 return winner;
18971 /* Return the template arguments that will produce the function signature
18972 DECL from the function template FN, with the explicit template
18973 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18974 also match. Return NULL_TREE if no satisfactory arguments could be
18975 found. */
18977 static tree
18978 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18980 int ntparms = DECL_NTPARMS (fn);
18981 tree targs = make_tree_vec (ntparms);
18982 tree decl_type = TREE_TYPE (decl);
18983 tree decl_arg_types;
18984 tree *args;
18985 unsigned int nargs, ix;
18986 tree arg;
18988 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18990 /* Never do unification on the 'this' parameter. */
18991 decl_arg_types = skip_artificial_parms_for (decl,
18992 TYPE_ARG_TYPES (decl_type));
18994 nargs = list_length (decl_arg_types);
18995 args = XALLOCAVEC (tree, nargs);
18996 for (arg = decl_arg_types, ix = 0;
18997 arg != NULL_TREE && arg != void_list_node;
18998 arg = TREE_CHAIN (arg), ++ix)
18999 args[ix] = TREE_VALUE (arg);
19001 if (fn_type_unification (fn, explicit_args, targs,
19002 args, ix,
19003 (check_rettype || DECL_CONV_FN_P (fn)
19004 ? TREE_TYPE (decl_type) : NULL_TREE),
19005 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
19006 /*decltype*/false)
19007 == error_mark_node)
19008 return NULL_TREE;
19010 return targs;
19013 /* Return the innermost template arguments that, when applied to a partial
19014 specialization of TMPL whose innermost template parameters are
19015 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19016 ARGS.
19018 For example, suppose we have:
19020 template <class T, class U> struct S {};
19021 template <class T> struct S<T*, int> {};
19023 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19024 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19025 int}. The resulting vector will be {double}, indicating that `T'
19026 is bound to `double'. */
19028 static tree
19029 get_class_bindings (tree tmpl, tree spec_tmpl, tree tparms,
19030 tree spec_args, tree args)
19032 int i, ntparms = TREE_VEC_LENGTH (tparms);
19033 tree deduced_args;
19034 tree innermost_deduced_args;
19036 innermost_deduced_args = make_tree_vec (ntparms);
19037 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19039 deduced_args = copy_node (args);
19040 SET_TMPL_ARGS_LEVEL (deduced_args,
19041 TMPL_ARGS_DEPTH (deduced_args),
19042 innermost_deduced_args);
19044 else
19045 deduced_args = innermost_deduced_args;
19047 if (unify (tparms, deduced_args,
19048 INNERMOST_TEMPLATE_ARGS (spec_args),
19049 INNERMOST_TEMPLATE_ARGS (args),
19050 UNIFY_ALLOW_NONE, /*explain_p=*/false))
19051 return NULL_TREE;
19053 for (i = 0; i < ntparms; ++i)
19054 if (! TREE_VEC_ELT (innermost_deduced_args, i))
19055 return NULL_TREE;
19057 /* Verify that nondeduced template arguments agree with the type
19058 obtained from argument deduction.
19060 For example:
19062 struct A { typedef int X; };
19063 template <class T, class U> struct C {};
19064 template <class T> struct C<T, typename T::X> {};
19066 Then with the instantiation `C<A, int>', we can deduce that
19067 `T' is `A' but unify () does not check whether `typename T::X'
19068 is `int'. */
19069 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
19070 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19071 spec_args, tmpl,
19072 tf_none, false, false);
19073 if (spec_args == error_mark_node
19074 /* We only need to check the innermost arguments; the other
19075 arguments will always agree. */
19076 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
19077 INNERMOST_TEMPLATE_ARGS (args)))
19078 return NULL_TREE;
19080 /* Now that we have bindings for all of the template arguments,
19081 ensure that the arguments deduced for the template template
19082 parameters have compatible template parameter lists. See the use
19083 of template_template_parm_bindings_ok_p in fn_type_unification
19084 for more information. */
19085 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
19086 return NULL_TREE;
19088 // Having computed and checked the binding, make sure that the
19089 // deduced arguments actually satisfy the template constraints.
19090 // If not, it is equivalent to having failed to compute the binding.
19091 if (!check_template_constraints (spec_tmpl, deduced_args))
19092 return NULL_TREE;
19094 return deduced_args;
19097 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19098 Return the TREE_LIST node with the most specialized template, if
19099 any. If there is no most specialized template, the error_mark_node
19100 is returned.
19102 Note that this function does not look at, or modify, the
19103 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19104 returned is one of the elements of INSTANTIATIONS, callers may
19105 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19106 and retrieve it from the value returned. */
19108 tree
19109 most_specialized_instantiation (tree templates)
19111 tree fn, champ;
19113 ++processing_template_decl;
19115 champ = templates;
19116 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
19118 int fate = 0;
19120 if (get_bindings (TREE_VALUE (champ),
19121 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19122 NULL_TREE, /*check_ret=*/true))
19123 fate--;
19125 if (get_bindings (TREE_VALUE (fn),
19126 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19127 NULL_TREE, /*check_ret=*/true))
19128 fate++;
19130 if (fate == -1)
19131 champ = fn;
19132 else if (!fate)
19134 /* Equally specialized, move to next function. If there
19135 is no next function, nothing's most specialized. */
19136 fn = TREE_CHAIN (fn);
19137 champ = fn;
19138 if (!fn)
19139 break;
19143 if (champ)
19144 /* Now verify that champ is better than everything earlier in the
19145 instantiation list. */
19146 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
19147 if (get_bindings (TREE_VALUE (champ),
19148 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19149 NULL_TREE, /*check_ret=*/true)
19150 || !get_bindings (TREE_VALUE (fn),
19151 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19152 NULL_TREE, /*check_ret=*/true))
19154 champ = NULL_TREE;
19155 break;
19158 processing_template_decl--;
19160 if (!champ)
19161 return error_mark_node;
19163 return champ;
19166 /* If DECL is a specialization of some template, return the most
19167 general such template. Otherwise, returns NULL_TREE.
19169 For example, given:
19171 template <class T> struct S { template <class U> void f(U); };
19173 if TMPL is `template <class U> void S<int>::f(U)' this will return
19174 the full template. This function will not trace past partial
19175 specializations, however. For example, given in addition:
19177 template <class T> struct S<T*> { template <class U> void f(U); };
19179 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19180 `template <class T> template <class U> S<T*>::f(U)'. */
19182 tree
19183 most_general_template (tree decl)
19185 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
19186 an immediate specialization. */
19187 if (TREE_CODE (decl) == FUNCTION_DECL)
19189 if (DECL_TEMPLATE_INFO (decl)) {
19190 decl = DECL_TI_TEMPLATE (decl);
19192 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
19193 template friend. */
19194 if (TREE_CODE (decl) != TEMPLATE_DECL)
19195 return NULL_TREE;
19196 } else
19197 return NULL_TREE;
19200 /* Look for more and more general templates. */
19201 while (DECL_TEMPLATE_INFO (decl))
19203 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19204 (See cp-tree.h for details.) */
19205 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
19206 break;
19208 if (CLASS_TYPE_P (TREE_TYPE (decl))
19209 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19210 break;
19212 /* Stop if we run into an explicitly specialized class template. */
19213 if (!DECL_NAMESPACE_SCOPE_P (decl)
19214 && DECL_CONTEXT (decl)
19215 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
19216 break;
19218 decl = DECL_TI_TEMPLATE (decl);
19221 return decl;
19224 /* Return the most specialized of the class template partial
19225 specializations which can produce TYPE, a specialization of some class
19226 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
19227 a _TYPE node corresponding to the partial specialization, while the
19228 TREE_PURPOSE is the set of template arguments that must be
19229 substituted into the TREE_TYPE in order to generate TYPE.
19231 If the choice of partial specialization is ambiguous, a diagnostic
19232 is issued, and the error_mark_node is returned. If there are no
19233 partial specializations matching TYPE, then NULL_TREE is
19234 returned, indicating that the primary template should be used. */
19236 static tree
19237 most_specialized_class (tree type, tsubst_flags_t complain)
19239 tree list = NULL_TREE;
19240 tree t;
19241 tree champ;
19242 int fate;
19243 bool ambiguous_p;
19244 tree outer_args = NULL_TREE;
19246 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
19247 tree main_tmpl = most_general_template (tmpl);
19248 tree args = CLASSTYPE_TI_ARGS (type);
19250 /* For determining which partial specialization to use, only the
19251 innermost args are interesting. */
19252 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19254 outer_args = strip_innermost_template_args (args, 1);
19255 args = INNERMOST_TEMPLATE_ARGS (args);
19258 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
19260 tree partial_spec_args;
19261 tree spec_args;
19262 tree spec_tmpl = TREE_VALUE (t);
19263 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19265 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
19267 ++processing_template_decl;
19269 if (outer_args)
19271 /* Discard the outer levels of args, and then substitute in the
19272 template args from the enclosing class. */
19273 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19274 partial_spec_args = tsubst_template_args
19275 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19277 /* And the same for the partial specialization TEMPLATE_DECL. */
19278 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19281 partial_spec_args =
19282 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19283 partial_spec_args,
19284 tmpl, tf_none,
19285 /*require_all_args=*/true,
19286 /*use_default_args=*/true);
19288 --processing_template_decl;
19290 if (partial_spec_args == error_mark_node)
19291 return error_mark_node;
19292 if (spec_tmpl == error_mark_node)
19293 return error_mark_node;
19295 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19296 spec_args = get_class_bindings (tmpl, spec_tmpl, parms,
19297 partial_spec_args,
19298 args);
19299 if (spec_args)
19301 if (outer_args)
19302 spec_args = add_to_template_args (outer_args, spec_args);
19303 list = tree_cons (spec_args, orig_parms, list);
19304 TREE_TYPE (list) = TREE_TYPE (t);
19308 if (! list)
19309 return NULL_TREE;
19311 ambiguous_p = false;
19312 t = list;
19313 champ = t;
19314 t = TREE_CHAIN (t);
19315 for (; t; t = TREE_CHAIN (t))
19317 fate = more_specialized_class (tmpl, champ, t);
19318 if (fate == 1)
19320 else
19322 if (fate == 0)
19324 t = TREE_CHAIN (t);
19325 if (! t)
19327 ambiguous_p = true;
19328 break;
19331 champ = t;
19335 if (!ambiguous_p)
19336 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19338 fate = more_specialized_class (tmpl, champ, t);
19339 if (fate != 1)
19341 ambiguous_p = true;
19342 break;
19346 if (ambiguous_p)
19348 const char *str;
19349 char *spaces = NULL;
19350 if (!(complain & tf_error))
19351 return error_mark_node;
19352 error ("ambiguous class template instantiation for %q#T", type);
19353 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19354 for (t = list; t; t = TREE_CHAIN (t))
19356 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
19357 spaces = spaces ? spaces : get_spaces (str);
19359 free (spaces);
19360 return error_mark_node;
19363 return champ;
19366 /* Explicitly instantiate DECL. */
19368 void
19369 do_decl_instantiation (tree decl, tree storage)
19371 tree result = NULL_TREE;
19372 int extern_p = 0;
19374 if (!decl || decl == error_mark_node)
19375 /* An error occurred, for which grokdeclarator has already issued
19376 an appropriate message. */
19377 return;
19378 else if (! DECL_LANG_SPECIFIC (decl))
19380 error ("explicit instantiation of non-template %q#D", decl);
19381 return;
19383 else if (VAR_P (decl))
19385 /* There is an asymmetry here in the way VAR_DECLs and
19386 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19387 the latter, the DECL we get back will be marked as a
19388 template instantiation, and the appropriate
19389 DECL_TEMPLATE_INFO will be set up. This does not happen for
19390 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19391 should handle VAR_DECLs as it currently handles
19392 FUNCTION_DECLs. */
19393 if (!DECL_CLASS_SCOPE_P (decl))
19395 error ("%qD is not a static data member of a class template", decl);
19396 return;
19398 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19399 if (!result || !VAR_P (result))
19401 error ("no matching template for %qD found", decl);
19402 return;
19404 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19406 error ("type %qT for explicit instantiation %qD does not match "
19407 "declared type %qT", TREE_TYPE (result), decl,
19408 TREE_TYPE (decl));
19409 return;
19412 else if (TREE_CODE (decl) != FUNCTION_DECL)
19414 error ("explicit instantiation of %q#D", decl);
19415 return;
19417 else
19418 result = decl;
19420 /* Check for various error cases. Note that if the explicit
19421 instantiation is valid the RESULT will currently be marked as an
19422 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19423 until we get here. */
19425 if (DECL_TEMPLATE_SPECIALIZATION (result))
19427 /* DR 259 [temp.spec].
19429 Both an explicit instantiation and a declaration of an explicit
19430 specialization shall not appear in a program unless the explicit
19431 instantiation follows a declaration of the explicit specialization.
19433 For a given set of template parameters, if an explicit
19434 instantiation of a template appears after a declaration of an
19435 explicit specialization for that template, the explicit
19436 instantiation has no effect. */
19437 return;
19439 else if (DECL_EXPLICIT_INSTANTIATION (result))
19441 /* [temp.spec]
19443 No program shall explicitly instantiate any template more
19444 than once.
19446 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19447 the first instantiation was `extern' and the second is not,
19448 and EXTERN_P for the opposite case. */
19449 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19450 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19451 /* If an "extern" explicit instantiation follows an ordinary
19452 explicit instantiation, the template is instantiated. */
19453 if (extern_p)
19454 return;
19456 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19458 error ("no matching template for %qD found", result);
19459 return;
19461 else if (!DECL_TEMPLATE_INFO (result))
19463 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19464 return;
19467 if (storage == NULL_TREE)
19469 else if (storage == ridpointers[(int) RID_EXTERN])
19471 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19472 pedwarn (input_location, OPT_Wpedantic,
19473 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19474 "instantiations");
19475 extern_p = 1;
19477 else
19478 error ("storage class %qD applied to template instantiation", storage);
19480 check_explicit_instantiation_namespace (result);
19481 mark_decl_instantiated (result, extern_p);
19482 if (! extern_p)
19483 instantiate_decl (result, /*defer_ok=*/1,
19484 /*expl_inst_class_mem_p=*/false);
19487 static void
19488 mark_class_instantiated (tree t, int extern_p)
19490 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19491 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19492 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19493 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19494 if (! extern_p)
19496 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19497 rest_of_type_compilation (t, 1);
19501 /* Called from do_type_instantiation through binding_table_foreach to
19502 do recursive instantiation for the type bound in ENTRY. */
19503 static void
19504 bt_instantiate_type_proc (binding_entry entry, void *data)
19506 tree storage = *(tree *) data;
19508 if (MAYBE_CLASS_TYPE_P (entry->type)
19509 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19510 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19513 /* Called from do_type_instantiation to instantiate a member
19514 (a member function or a static member variable) of an
19515 explicitly instantiated class template. */
19516 static void
19517 instantiate_class_member (tree decl, int extern_p)
19519 mark_decl_instantiated (decl, extern_p);
19520 if (! extern_p)
19521 instantiate_decl (decl, /*defer_ok=*/1,
19522 /*expl_inst_class_mem_p=*/true);
19525 /* Perform an explicit instantiation of template class T. STORAGE, if
19526 non-null, is the RID for extern, inline or static. COMPLAIN is
19527 nonzero if this is called from the parser, zero if called recursively,
19528 since the standard is unclear (as detailed below). */
19530 void
19531 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19533 int extern_p = 0;
19534 int nomem_p = 0;
19535 int static_p = 0;
19536 int previous_instantiation_extern_p = 0;
19538 if (TREE_CODE (t) == TYPE_DECL)
19539 t = TREE_TYPE (t);
19541 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19543 tree tmpl =
19544 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19545 if (tmpl)
19546 error ("explicit instantiation of non-class template %qD", tmpl);
19547 else
19548 error ("explicit instantiation of non-template type %qT", t);
19549 return;
19552 complete_type (t);
19554 if (!COMPLETE_TYPE_P (t))
19556 if (complain & tf_error)
19557 error ("explicit instantiation of %q#T before definition of template",
19559 return;
19562 if (storage != NULL_TREE)
19564 if (!in_system_header_at (input_location))
19566 if (storage == ridpointers[(int) RID_EXTERN])
19568 if (cxx_dialect == cxx98)
19569 pedwarn (input_location, OPT_Wpedantic,
19570 "ISO C++ 1998 forbids the use of %<extern%> on "
19571 "explicit instantiations");
19573 else
19574 pedwarn (input_location, OPT_Wpedantic,
19575 "ISO C++ forbids the use of %qE"
19576 " on explicit instantiations", storage);
19579 if (storage == ridpointers[(int) RID_INLINE])
19580 nomem_p = 1;
19581 else if (storage == ridpointers[(int) RID_EXTERN])
19582 extern_p = 1;
19583 else if (storage == ridpointers[(int) RID_STATIC])
19584 static_p = 1;
19585 else
19587 error ("storage class %qD applied to template instantiation",
19588 storage);
19589 extern_p = 0;
19593 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19595 /* DR 259 [temp.spec].
19597 Both an explicit instantiation and a declaration of an explicit
19598 specialization shall not appear in a program unless the explicit
19599 instantiation follows a declaration of the explicit specialization.
19601 For a given set of template parameters, if an explicit
19602 instantiation of a template appears after a declaration of an
19603 explicit specialization for that template, the explicit
19604 instantiation has no effect. */
19605 return;
19607 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19609 /* [temp.spec]
19611 No program shall explicitly instantiate any template more
19612 than once.
19614 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19615 instantiation was `extern'. If EXTERN_P then the second is.
19616 These cases are OK. */
19617 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19619 if (!previous_instantiation_extern_p && !extern_p
19620 && (complain & tf_error))
19621 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19623 /* If we've already instantiated the template, just return now. */
19624 if (!CLASSTYPE_INTERFACE_ONLY (t))
19625 return;
19628 check_explicit_instantiation_namespace (TYPE_NAME (t));
19629 mark_class_instantiated (t, extern_p);
19631 if (nomem_p)
19632 return;
19635 tree tmp;
19637 /* In contrast to implicit instantiation, where only the
19638 declarations, and not the definitions, of members are
19639 instantiated, we have here:
19641 [temp.explicit]
19643 The explicit instantiation of a class template specialization
19644 implies the instantiation of all of its members not
19645 previously explicitly specialized in the translation unit
19646 containing the explicit instantiation.
19648 Of course, we can't instantiate member template classes, since
19649 we don't have any arguments for them. Note that the standard
19650 is unclear on whether the instantiation of the members are
19651 *explicit* instantiations or not. However, the most natural
19652 interpretation is that it should be an explicit instantiation. */
19654 if (! static_p)
19655 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19656 if (TREE_CODE (tmp) == FUNCTION_DECL
19657 && DECL_TEMPLATE_INSTANTIATION (tmp))
19658 instantiate_class_member (tmp, extern_p);
19660 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19661 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19662 instantiate_class_member (tmp, extern_p);
19664 if (CLASSTYPE_NESTED_UTDS (t))
19665 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19666 bt_instantiate_type_proc, &storage);
19670 /* Given a function DECL, which is a specialization of TMPL, modify
19671 DECL to be a re-instantiation of TMPL with the same template
19672 arguments. TMPL should be the template into which tsubst'ing
19673 should occur for DECL, not the most general template.
19675 One reason for doing this is a scenario like this:
19677 template <class T>
19678 void f(const T&, int i);
19680 void g() { f(3, 7); }
19682 template <class T>
19683 void f(const T& t, const int i) { }
19685 Note that when the template is first instantiated, with
19686 instantiate_template, the resulting DECL will have no name for the
19687 first parameter, and the wrong type for the second. So, when we go
19688 to instantiate the DECL, we regenerate it. */
19690 static void
19691 regenerate_decl_from_template (tree decl, tree tmpl)
19693 /* The arguments used to instantiate DECL, from the most general
19694 template. */
19695 tree args;
19696 tree code_pattern;
19698 args = DECL_TI_ARGS (decl);
19699 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19701 /* Make sure that we can see identifiers, and compute access
19702 correctly. */
19703 push_access_scope (decl);
19705 if (TREE_CODE (decl) == FUNCTION_DECL)
19707 tree decl_parm;
19708 tree pattern_parm;
19709 tree specs;
19710 int args_depth;
19711 int parms_depth;
19713 args_depth = TMPL_ARGS_DEPTH (args);
19714 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19715 if (args_depth > parms_depth)
19716 args = get_innermost_template_args (args, parms_depth);
19718 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19719 args, tf_error, NULL_TREE,
19720 /*defer_ok*/false);
19721 if (specs && specs != error_mark_node)
19722 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19723 specs);
19725 /* Merge parameter declarations. */
19726 decl_parm = skip_artificial_parms_for (decl,
19727 DECL_ARGUMENTS (decl));
19728 pattern_parm
19729 = skip_artificial_parms_for (code_pattern,
19730 DECL_ARGUMENTS (code_pattern));
19731 while (decl_parm && !DECL_PACK_P (pattern_parm))
19733 tree parm_type;
19734 tree attributes;
19736 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19737 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19738 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19739 NULL_TREE);
19740 parm_type = type_decays_to (parm_type);
19741 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19742 TREE_TYPE (decl_parm) = parm_type;
19743 attributes = DECL_ATTRIBUTES (pattern_parm);
19744 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19746 DECL_ATTRIBUTES (decl_parm) = attributes;
19747 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19749 decl_parm = DECL_CHAIN (decl_parm);
19750 pattern_parm = DECL_CHAIN (pattern_parm);
19752 /* Merge any parameters that match with the function parameter
19753 pack. */
19754 if (pattern_parm && DECL_PACK_P (pattern_parm))
19756 int i, len;
19757 tree expanded_types;
19758 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19759 the parameters in this function parameter pack. */
19760 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19761 args, tf_error, NULL_TREE);
19762 len = TREE_VEC_LENGTH (expanded_types);
19763 for (i = 0; i < len; i++)
19765 tree parm_type;
19766 tree attributes;
19768 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19769 /* Rename the parameter to include the index. */
19770 DECL_NAME (decl_parm) =
19771 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19772 parm_type = TREE_VEC_ELT (expanded_types, i);
19773 parm_type = type_decays_to (parm_type);
19774 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19775 TREE_TYPE (decl_parm) = parm_type;
19776 attributes = DECL_ATTRIBUTES (pattern_parm);
19777 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19779 DECL_ATTRIBUTES (decl_parm) = attributes;
19780 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19782 decl_parm = DECL_CHAIN (decl_parm);
19785 /* Merge additional specifiers from the CODE_PATTERN. */
19786 if (DECL_DECLARED_INLINE_P (code_pattern)
19787 && !DECL_DECLARED_INLINE_P (decl))
19788 DECL_DECLARED_INLINE_P (decl) = 1;
19790 else if (VAR_P (decl))
19792 DECL_INITIAL (decl) =
19793 tsubst_expr (DECL_INITIAL (code_pattern), args,
19794 tf_error, DECL_TI_TEMPLATE (decl),
19795 /*integral_constant_expression_p=*/false);
19796 if (VAR_HAD_UNKNOWN_BOUND (decl))
19797 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19798 tf_error, DECL_TI_TEMPLATE (decl));
19800 else
19801 gcc_unreachable ();
19803 pop_access_scope (decl);
19806 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19807 substituted to get DECL. */
19809 tree
19810 template_for_substitution (tree decl)
19812 tree tmpl = DECL_TI_TEMPLATE (decl);
19814 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19815 for the instantiation. This is not always the most general
19816 template. Consider, for example:
19818 template <class T>
19819 struct S { template <class U> void f();
19820 template <> void f<int>(); };
19822 and an instantiation of S<double>::f<int>. We want TD to be the
19823 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19824 while (/* An instantiation cannot have a definition, so we need a
19825 more general template. */
19826 DECL_TEMPLATE_INSTANTIATION (tmpl)
19827 /* We must also deal with friend templates. Given:
19829 template <class T> struct S {
19830 template <class U> friend void f() {};
19833 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19834 so far as the language is concerned, but that's still
19835 where we get the pattern for the instantiation from. On
19836 other hand, if the definition comes outside the class, say:
19838 template <class T> struct S {
19839 template <class U> friend void f();
19841 template <class U> friend void f() {}
19843 we don't need to look any further. That's what the check for
19844 DECL_INITIAL is for. */
19845 || (TREE_CODE (decl) == FUNCTION_DECL
19846 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19847 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19849 /* The present template, TD, should not be a definition. If it
19850 were a definition, we should be using it! Note that we
19851 cannot restructure the loop to just keep going until we find
19852 a template with a definition, since that might go too far if
19853 a specialization was declared, but not defined. */
19854 gcc_assert (!VAR_P (decl)
19855 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19857 /* Fetch the more general template. */
19858 tmpl = DECL_TI_TEMPLATE (tmpl);
19861 return tmpl;
19864 /* Returns true if we need to instantiate this template instance even if we
19865 know we aren't going to emit it.. */
19867 bool
19868 always_instantiate_p (tree decl)
19870 /* We always instantiate inline functions so that we can inline them. An
19871 explicit instantiation declaration prohibits implicit instantiation of
19872 non-inline functions. With high levels of optimization, we would
19873 normally inline non-inline functions -- but we're not allowed to do
19874 that for "extern template" functions. Therefore, we check
19875 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19876 return ((TREE_CODE (decl) == FUNCTION_DECL
19877 && (DECL_DECLARED_INLINE_P (decl)
19878 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19879 /* And we need to instantiate static data members so that
19880 their initializers are available in integral constant
19881 expressions. */
19882 || (VAR_P (decl)
19883 && decl_maybe_constant_var_p (decl)));
19886 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19887 instantiate it now, modifying TREE_TYPE (fn). */
19889 void
19890 maybe_instantiate_noexcept (tree fn)
19892 tree fntype, spec, noex, clone;
19894 /* Don't instantiate a noexcept-specification from template context. */
19895 if (processing_template_decl)
19896 return;
19898 if (DECL_CLONED_FUNCTION_P (fn))
19899 fn = DECL_CLONED_FUNCTION (fn);
19900 fntype = TREE_TYPE (fn);
19901 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19903 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19904 return;
19906 noex = TREE_PURPOSE (spec);
19908 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19910 if (push_tinst_level (fn))
19912 push_access_scope (fn);
19913 push_deferring_access_checks (dk_no_deferred);
19914 input_location = DECL_SOURCE_LOCATION (fn);
19915 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19916 DEFERRED_NOEXCEPT_ARGS (noex),
19917 tf_warning_or_error, fn,
19918 /*function_p=*/false,
19919 /*integral_constant_expression_p=*/true);
19920 pop_deferring_access_checks ();
19921 pop_access_scope (fn);
19922 pop_tinst_level ();
19923 spec = build_noexcept_spec (noex, tf_warning_or_error);
19924 if (spec == error_mark_node)
19925 spec = noexcept_false_spec;
19927 else
19928 spec = noexcept_false_spec;
19930 else
19932 /* This is an implicitly declared function, so NOEX is a list of
19933 other functions to evaluate and merge. */
19934 tree elt;
19935 spec = noexcept_true_spec;
19936 for (elt = noex; elt; elt = OVL_NEXT (elt))
19938 tree fn = OVL_CURRENT (elt);
19939 tree subspec;
19940 maybe_instantiate_noexcept (fn);
19941 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19942 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19946 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19948 FOR_EACH_CLONE (clone, fn)
19950 if (TREE_TYPE (clone) == fntype)
19951 TREE_TYPE (clone) = TREE_TYPE (fn);
19952 else
19953 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19957 /* Produce the definition of D, a _DECL generated from a template. If
19958 DEFER_OK is nonzero, then we don't have to actually do the
19959 instantiation now; we just have to do it sometime. Normally it is
19960 an error if this is an explicit instantiation but D is undefined.
19961 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19962 explicitly instantiated class template. */
19964 tree
19965 instantiate_decl (tree d, int defer_ok,
19966 bool expl_inst_class_mem_p)
19968 tree tmpl = DECL_TI_TEMPLATE (d);
19969 tree gen_args;
19970 tree args;
19971 tree td;
19972 tree code_pattern;
19973 tree spec;
19974 tree gen_tmpl;
19975 bool pattern_defined;
19976 location_t saved_loc = input_location;
19977 int saved_unevaluated_operand = cp_unevaluated_operand;
19978 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19979 bool external_p;
19980 tree fn_context;
19981 bool nested;
19983 /* This function should only be used to instantiate templates for
19984 functions and static member variables. */
19985 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19987 /* Variables are never deferred; if instantiation is required, they
19988 are instantiated right away. That allows for better code in the
19989 case that an expression refers to the value of the variable --
19990 if the variable has a constant value the referring expression can
19991 take advantage of that fact. */
19992 if (VAR_P (d)
19993 || DECL_DECLARED_CONSTEXPR_P (d))
19994 defer_ok = 0;
19996 /* Don't instantiate cloned functions. Instead, instantiate the
19997 functions they cloned. */
19998 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19999 d = DECL_CLONED_FUNCTION (d);
20001 if (DECL_TEMPLATE_INSTANTIATED (d)
20002 || (TREE_CODE (d) == FUNCTION_DECL
20003 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
20004 || DECL_TEMPLATE_SPECIALIZATION (d))
20005 /* D has already been instantiated or explicitly specialized, so
20006 there's nothing for us to do here.
20008 It might seem reasonable to check whether or not D is an explicit
20009 instantiation, and, if so, stop here. But when an explicit
20010 instantiation is deferred until the end of the compilation,
20011 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20012 the instantiation. */
20013 return d;
20015 /* Check to see whether we know that this template will be
20016 instantiated in some other file, as with "extern template"
20017 extension. */
20018 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
20020 /* In general, we do not instantiate such templates. */
20021 if (external_p && !always_instantiate_p (d))
20022 return d;
20024 gen_tmpl = most_general_template (tmpl);
20025 gen_args = DECL_TI_ARGS (d);
20027 if (tmpl != gen_tmpl)
20028 /* We should already have the extra args. */
20029 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
20030 == TMPL_ARGS_DEPTH (gen_args));
20031 /* And what's in the hash table should match D. */
20032 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
20033 || spec == NULL_TREE);
20035 /* This needs to happen before any tsubsting. */
20036 if (! push_tinst_level (d))
20037 return d;
20039 timevar_push (TV_TEMPLATE_INST);
20041 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20042 for the instantiation. */
20043 td = template_for_substitution (d);
20044 code_pattern = DECL_TEMPLATE_RESULT (td);
20046 /* We should never be trying to instantiate a member of a class
20047 template or partial specialization. */
20048 gcc_assert (d != code_pattern);
20050 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
20051 || DECL_TEMPLATE_SPECIALIZATION (td))
20052 /* In the case of a friend template whose definition is provided
20053 outside the class, we may have too many arguments. Drop the
20054 ones we don't need. The same is true for specializations. */
20055 args = get_innermost_template_args
20056 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
20057 else
20058 args = gen_args;
20060 if (TREE_CODE (d) == FUNCTION_DECL)
20061 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
20062 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
20063 else
20064 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
20066 /* We may be in the middle of deferred access check. Disable it now. */
20067 push_deferring_access_checks (dk_no_deferred);
20069 /* Unless an explicit instantiation directive has already determined
20070 the linkage of D, remember that a definition is available for
20071 this entity. */
20072 if (pattern_defined
20073 && !DECL_INTERFACE_KNOWN (d)
20074 && !DECL_NOT_REALLY_EXTERN (d))
20075 mark_definable (d);
20077 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
20078 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
20079 input_location = DECL_SOURCE_LOCATION (d);
20081 /* If D is a member of an explicitly instantiated class template,
20082 and no definition is available, treat it like an implicit
20083 instantiation. */
20084 if (!pattern_defined && expl_inst_class_mem_p
20085 && DECL_EXPLICIT_INSTANTIATION (d))
20087 /* Leave linkage flags alone on instantiations with anonymous
20088 visibility. */
20089 if (TREE_PUBLIC (d))
20091 DECL_NOT_REALLY_EXTERN (d) = 0;
20092 DECL_INTERFACE_KNOWN (d) = 0;
20094 SET_DECL_IMPLICIT_INSTANTIATION (d);
20097 if (TREE_CODE (d) == FUNCTION_DECL)
20098 maybe_instantiate_noexcept (d);
20100 /* Defer all other templates, unless we have been explicitly
20101 forbidden from doing so. */
20102 if (/* If there is no definition, we cannot instantiate the
20103 template. */
20104 ! pattern_defined
20105 /* If it's OK to postpone instantiation, do so. */
20106 || defer_ok
20107 /* If this is a static data member that will be defined
20108 elsewhere, we don't want to instantiate the entire data
20109 member, but we do want to instantiate the initializer so that
20110 we can substitute that elsewhere. */
20111 || (external_p && VAR_P (d)))
20113 /* The definition of the static data member is now required so
20114 we must substitute the initializer. */
20115 if (VAR_P (d)
20116 && !DECL_INITIAL (d)
20117 && DECL_INITIAL (code_pattern))
20119 tree ns;
20120 tree init;
20121 bool const_init = false;
20123 ns = decl_namespace_context (d);
20124 push_nested_namespace (ns);
20125 push_nested_class (DECL_CONTEXT (d));
20126 init = tsubst_expr (DECL_INITIAL (code_pattern),
20127 args,
20128 tf_warning_or_error, NULL_TREE,
20129 /*integral_constant_expression_p=*/false);
20130 /* Make sure the initializer is still constant, in case of
20131 circular dependency (template/instantiate6.C). */
20132 const_init
20133 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20134 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
20135 /*asmspec_tree=*/NULL_TREE,
20136 LOOKUP_ONLYCONVERTING);
20137 pop_nested_class ();
20138 pop_nested_namespace (ns);
20141 /* We restore the source position here because it's used by
20142 add_pending_template. */
20143 input_location = saved_loc;
20145 if (at_eof && !pattern_defined
20146 && DECL_EXPLICIT_INSTANTIATION (d)
20147 && DECL_NOT_REALLY_EXTERN (d))
20148 /* [temp.explicit]
20150 The definition of a non-exported function template, a
20151 non-exported member function template, or a non-exported
20152 member function or static data member of a class template
20153 shall be present in every translation unit in which it is
20154 explicitly instantiated. */
20155 permerror (input_location, "explicit instantiation of %qD "
20156 "but no definition available", d);
20158 /* If we're in unevaluated context, we just wanted to get the
20159 constant value; this isn't an odr use, so don't queue
20160 a full instantiation. */
20161 if (cp_unevaluated_operand != 0)
20162 goto out;
20163 /* ??? Historically, we have instantiated inline functions, even
20164 when marked as "extern template". */
20165 if (!(external_p && VAR_P (d)))
20166 add_pending_template (d);
20167 goto out;
20169 /* Tell the repository that D is available in this translation unit
20170 -- and see if it is supposed to be instantiated here. */
20171 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
20173 /* In a PCH file, despite the fact that the repository hasn't
20174 requested instantiation in the PCH it is still possible that
20175 an instantiation will be required in a file that includes the
20176 PCH. */
20177 if (pch_file)
20178 add_pending_template (d);
20179 /* Instantiate inline functions so that the inliner can do its
20180 job, even though we'll not be emitting a copy of this
20181 function. */
20182 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
20183 goto out;
20186 fn_context = decl_function_context (d);
20187 nested = (current_function_decl != NULL_TREE);
20188 if (!fn_context)
20189 push_to_top_level ();
20190 else
20192 if (nested)
20193 push_function_context ();
20194 cp_unevaluated_operand = 0;
20195 c_inhibit_evaluation_warnings = 0;
20198 /* Mark D as instantiated so that recursive calls to
20199 instantiate_decl do not try to instantiate it again. */
20200 DECL_TEMPLATE_INSTANTIATED (d) = 1;
20202 /* Regenerate the declaration in case the template has been modified
20203 by a subsequent redeclaration. */
20204 regenerate_decl_from_template (d, td);
20206 /* We already set the file and line above. Reset them now in case
20207 they changed as a result of calling regenerate_decl_from_template. */
20208 input_location = DECL_SOURCE_LOCATION (d);
20210 if (VAR_P (d))
20212 tree init;
20213 bool const_init = false;
20215 /* Clear out DECL_RTL; whatever was there before may not be right
20216 since we've reset the type of the declaration. */
20217 SET_DECL_RTL (d, NULL);
20218 DECL_IN_AGGR_P (d) = 0;
20220 /* The initializer is placed in DECL_INITIAL by
20221 regenerate_decl_from_template so we don't need to
20222 push/pop_access_scope again here. Pull it out so that
20223 cp_finish_decl can process it. */
20224 init = DECL_INITIAL (d);
20225 DECL_INITIAL (d) = NULL_TREE;
20226 DECL_INITIALIZED_P (d) = 0;
20228 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20229 initializer. That function will defer actual emission until
20230 we have a chance to determine linkage. */
20231 DECL_EXTERNAL (d) = 0;
20233 /* Enter the scope of D so that access-checking works correctly. */
20234 push_nested_class (DECL_CONTEXT (d));
20235 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20236 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
20237 pop_nested_class ();
20239 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
20240 synthesize_method (d);
20241 else if (TREE_CODE (d) == FUNCTION_DECL)
20243 struct pointer_map_t *saved_local_specializations;
20244 tree subst_decl;
20245 tree tmpl_parm;
20246 tree spec_parm;
20247 tree block = NULL_TREE;
20249 /* Save away the current list, in case we are instantiating one
20250 template from within the body of another. */
20251 saved_local_specializations = local_specializations;
20253 /* Set up the list of local specializations. */
20254 local_specializations = pointer_map_create ();
20256 /* Set up context. */
20257 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20258 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20259 block = push_stmt_list ();
20260 else
20261 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
20263 /* Some typedefs referenced from within the template code need to be
20264 access checked at template instantiation time, i.e now. These
20265 types were added to the template at parsing time. Let's get those
20266 and perform the access checks then. */
20267 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
20268 gen_args);
20270 /* Create substitution entries for the parameters. */
20271 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
20272 tmpl_parm = DECL_ARGUMENTS (subst_decl);
20273 spec_parm = DECL_ARGUMENTS (d);
20274 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
20276 register_local_specialization (spec_parm, tmpl_parm);
20277 spec_parm = skip_artificial_parms_for (d, spec_parm);
20278 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
20280 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
20282 if (!DECL_PACK_P (tmpl_parm))
20284 register_local_specialization (spec_parm, tmpl_parm);
20285 spec_parm = DECL_CHAIN (spec_parm);
20287 else
20289 /* Register the (value) argument pack as a specialization of
20290 TMPL_PARM, then move on. */
20291 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
20292 register_local_specialization (argpack, tmpl_parm);
20295 gcc_assert (!spec_parm);
20297 /* Substitute into the body of the function. */
20298 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20299 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
20300 tf_warning_or_error, tmpl);
20301 else
20303 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
20304 tf_warning_or_error, tmpl,
20305 /*integral_constant_expression_p=*/false);
20307 /* Set the current input_location to the end of the function
20308 so that finish_function knows where we are. */
20309 input_location
20310 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
20312 /* Remember if we saw an infinite loop in the template. */
20313 current_function_infinite_loop
20314 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
20317 /* We don't need the local specializations any more. */
20318 pointer_map_destroy (local_specializations);
20319 local_specializations = saved_local_specializations;
20321 /* Finish the function. */
20322 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20323 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20324 DECL_SAVED_TREE (d) = pop_stmt_list (block);
20325 else
20327 d = finish_function (0);
20328 expand_or_defer_fn (d);
20331 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20332 cp_check_omp_declare_reduction (d);
20335 /* We're not deferring instantiation any more. */
20336 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20338 if (!fn_context)
20339 pop_from_top_level ();
20340 else if (nested)
20341 pop_function_context ();
20343 out:
20344 input_location = saved_loc;
20345 cp_unevaluated_operand = saved_unevaluated_operand;
20346 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20347 pop_deferring_access_checks ();
20348 pop_tinst_level ();
20350 timevar_pop (TV_TEMPLATE_INST);
20352 return d;
20355 /* Run through the list of templates that we wish we could
20356 instantiate, and instantiate any we can. RETRIES is the
20357 number of times we retry pending template instantiation. */
20359 void
20360 instantiate_pending_templates (int retries)
20362 int reconsider;
20363 location_t saved_loc = input_location;
20365 /* Instantiating templates may trigger vtable generation. This in turn
20366 may require further template instantiations. We place a limit here
20367 to avoid infinite loop. */
20368 if (pending_templates && retries >= max_tinst_depth)
20370 tree decl = pending_templates->tinst->decl;
20372 error ("template instantiation depth exceeds maximum of %d"
20373 " instantiating %q+D, possibly from virtual table generation"
20374 " (use -ftemplate-depth= to increase the maximum)",
20375 max_tinst_depth, decl);
20376 if (TREE_CODE (decl) == FUNCTION_DECL)
20377 /* Pretend that we defined it. */
20378 DECL_INITIAL (decl) = error_mark_node;
20379 return;
20384 struct pending_template **t = &pending_templates;
20385 struct pending_template *last = NULL;
20386 reconsider = 0;
20387 while (*t)
20389 tree instantiation = reopen_tinst_level ((*t)->tinst);
20390 bool complete = false;
20392 if (TYPE_P (instantiation))
20394 tree fn;
20396 if (!COMPLETE_TYPE_P (instantiation))
20398 instantiate_class_template (instantiation);
20399 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20400 for (fn = TYPE_METHODS (instantiation);
20402 fn = TREE_CHAIN (fn))
20403 if (! DECL_ARTIFICIAL (fn))
20404 instantiate_decl (fn,
20405 /*defer_ok=*/0,
20406 /*expl_inst_class_mem_p=*/false);
20407 if (COMPLETE_TYPE_P (instantiation))
20408 reconsider = 1;
20411 complete = COMPLETE_TYPE_P (instantiation);
20413 else
20415 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20416 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20418 instantiation
20419 = instantiate_decl (instantiation,
20420 /*defer_ok=*/0,
20421 /*expl_inst_class_mem_p=*/false);
20422 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20423 reconsider = 1;
20426 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20427 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20430 if (complete)
20431 /* If INSTANTIATION has been instantiated, then we don't
20432 need to consider it again in the future. */
20433 *t = (*t)->next;
20434 else
20436 last = *t;
20437 t = &(*t)->next;
20439 tinst_depth = 0;
20440 current_tinst_level = NULL;
20442 last_pending_template = last;
20444 while (reconsider);
20446 input_location = saved_loc;
20449 /* Substitute ARGVEC into T, which is a list of initializers for
20450 either base class or a non-static data member. The TREE_PURPOSEs
20451 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20452 instantiate_decl. */
20454 static tree
20455 tsubst_initializer_list (tree t, tree argvec)
20457 tree inits = NULL_TREE;
20459 for (; t; t = TREE_CHAIN (t))
20461 tree decl;
20462 tree init;
20463 tree expanded_bases = NULL_TREE;
20464 tree expanded_arguments = NULL_TREE;
20465 int i, len = 1;
20467 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20469 tree expr;
20470 tree arg;
20472 /* Expand the base class expansion type into separate base
20473 classes. */
20474 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20475 tf_warning_or_error,
20476 NULL_TREE);
20477 if (expanded_bases == error_mark_node)
20478 continue;
20480 /* We'll be building separate TREE_LISTs of arguments for
20481 each base. */
20482 len = TREE_VEC_LENGTH (expanded_bases);
20483 expanded_arguments = make_tree_vec (len);
20484 for (i = 0; i < len; i++)
20485 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20487 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20488 expand each argument in the TREE_VALUE of t. */
20489 expr = make_node (EXPR_PACK_EXPANSION);
20490 PACK_EXPANSION_LOCAL_P (expr) = true;
20491 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20492 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20494 if (TREE_VALUE (t) == void_type_node)
20495 /* VOID_TYPE_NODE is used to indicate
20496 value-initialization. */
20498 for (i = 0; i < len; i++)
20499 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20501 else
20503 /* Substitute parameter packs into each argument in the
20504 TREE_LIST. */
20505 in_base_initializer = 1;
20506 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20508 tree expanded_exprs;
20510 /* Expand the argument. */
20511 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20512 expanded_exprs
20513 = tsubst_pack_expansion (expr, argvec,
20514 tf_warning_or_error,
20515 NULL_TREE);
20516 if (expanded_exprs == error_mark_node)
20517 continue;
20519 /* Prepend each of the expanded expressions to the
20520 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20521 for (i = 0; i < len; i++)
20523 TREE_VEC_ELT (expanded_arguments, i) =
20524 tree_cons (NULL_TREE,
20525 TREE_VEC_ELT (expanded_exprs, i),
20526 TREE_VEC_ELT (expanded_arguments, i));
20529 in_base_initializer = 0;
20531 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20532 since we built them backwards. */
20533 for (i = 0; i < len; i++)
20535 TREE_VEC_ELT (expanded_arguments, i) =
20536 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20541 for (i = 0; i < len; ++i)
20543 if (expanded_bases)
20545 decl = TREE_VEC_ELT (expanded_bases, i);
20546 decl = expand_member_init (decl);
20547 init = TREE_VEC_ELT (expanded_arguments, i);
20549 else
20551 tree tmp;
20552 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20553 tf_warning_or_error, NULL_TREE);
20555 decl = expand_member_init (decl);
20556 if (decl && !DECL_P (decl))
20557 in_base_initializer = 1;
20559 init = TREE_VALUE (t);
20560 tmp = init;
20561 if (init != void_type_node)
20562 init = tsubst_expr (init, argvec,
20563 tf_warning_or_error, NULL_TREE,
20564 /*integral_constant_expression_p=*/false);
20565 if (init == NULL_TREE && tmp != NULL_TREE)
20566 /* If we had an initializer but it instantiated to nothing,
20567 value-initialize the object. This will only occur when
20568 the initializer was a pack expansion where the parameter
20569 packs used in that expansion were of length zero. */
20570 init = void_type_node;
20571 in_base_initializer = 0;
20574 if (decl)
20576 init = build_tree_list (decl, init);
20577 TREE_CHAIN (init) = inits;
20578 inits = init;
20582 return inits;
20585 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20587 static void
20588 set_current_access_from_decl (tree decl)
20590 if (TREE_PRIVATE (decl))
20591 current_access_specifier = access_private_node;
20592 else if (TREE_PROTECTED (decl))
20593 current_access_specifier = access_protected_node;
20594 else
20595 current_access_specifier = access_public_node;
20598 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20599 is the instantiation (which should have been created with
20600 start_enum) and ARGS are the template arguments to use. */
20602 static void
20603 tsubst_enum (tree tag, tree newtag, tree args)
20605 tree e;
20607 if (SCOPED_ENUM_P (newtag))
20608 begin_scope (sk_scoped_enum, newtag);
20610 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20612 tree value;
20613 tree decl;
20615 decl = TREE_VALUE (e);
20616 /* Note that in a template enum, the TREE_VALUE is the
20617 CONST_DECL, not the corresponding INTEGER_CST. */
20618 value = tsubst_expr (DECL_INITIAL (decl),
20619 args, tf_warning_or_error, NULL_TREE,
20620 /*integral_constant_expression_p=*/true);
20622 /* Give this enumeration constant the correct access. */
20623 set_current_access_from_decl (decl);
20625 /* Actually build the enumerator itself. */
20626 build_enumerator
20627 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20630 if (SCOPED_ENUM_P (newtag))
20631 finish_scope ();
20633 finish_enum_value_list (newtag);
20634 finish_enum (newtag);
20636 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20637 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20640 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20641 its type -- but without substituting the innermost set of template
20642 arguments. So, innermost set of template parameters will appear in
20643 the type. */
20645 tree
20646 get_mostly_instantiated_function_type (tree decl)
20648 tree fn_type;
20649 tree tmpl;
20650 tree targs;
20651 tree tparms;
20652 int parm_depth;
20654 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20655 targs = DECL_TI_ARGS (decl);
20656 tparms = DECL_TEMPLATE_PARMS (tmpl);
20657 parm_depth = TMPL_PARMS_DEPTH (tparms);
20659 /* There should be as many levels of arguments as there are levels
20660 of parameters. */
20661 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20663 fn_type = TREE_TYPE (tmpl);
20665 if (parm_depth == 1)
20666 /* No substitution is necessary. */
20668 else
20670 int i;
20671 tree partial_args;
20673 /* Replace the innermost level of the TARGS with NULL_TREEs to
20674 let tsubst know not to substitute for those parameters. */
20675 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20676 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20677 SET_TMPL_ARGS_LEVEL (partial_args, i,
20678 TMPL_ARGS_LEVEL (targs, i));
20679 SET_TMPL_ARGS_LEVEL (partial_args,
20680 TMPL_ARGS_DEPTH (targs),
20681 make_tree_vec (DECL_NTPARMS (tmpl)));
20683 /* Make sure that we can see identifiers, and compute access
20684 correctly. */
20685 push_access_scope (decl);
20687 ++processing_template_decl;
20688 /* Now, do the (partial) substitution to figure out the
20689 appropriate function type. */
20690 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20691 --processing_template_decl;
20693 /* Substitute into the template parameters to obtain the real
20694 innermost set of parameters. This step is important if the
20695 innermost set of template parameters contains value
20696 parameters whose types depend on outer template parameters. */
20697 TREE_VEC_LENGTH (partial_args)--;
20698 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20700 pop_access_scope (decl);
20703 return fn_type;
20706 /* Return truthvalue if we're processing a template different from
20707 the last one involved in diagnostics. */
20709 problematic_instantiation_changed (void)
20711 return current_tinst_level != last_error_tinst_level;
20714 /* Remember current template involved in diagnostics. */
20715 void
20716 record_last_problematic_instantiation (void)
20718 last_error_tinst_level = current_tinst_level;
20721 struct tinst_level *
20722 current_instantiation (void)
20724 return current_tinst_level;
20727 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20728 type. Return zero for ok, nonzero for disallowed. Issue error and
20729 warning messages under control of COMPLAIN. */
20731 static int
20732 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20734 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20735 return 0;
20736 else if (POINTER_TYPE_P (type))
20737 return 0;
20738 else if (TYPE_PTRMEM_P (type))
20739 return 0;
20740 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20741 return 0;
20742 else if (TREE_CODE (type) == TYPENAME_TYPE)
20743 return 0;
20744 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20745 return 0;
20746 else if (TREE_CODE (type) == NULLPTR_TYPE)
20747 return 0;
20749 if (complain & tf_error)
20751 if (type == error_mark_node)
20752 inform (input_location, "invalid template non-type parameter");
20753 else
20754 error ("%q#T is not a valid type for a template non-type parameter",
20755 type);
20757 return 1;
20760 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20761 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20763 static bool
20764 dependent_type_p_r (tree type)
20766 tree scope;
20768 /* [temp.dep.type]
20770 A type is dependent if it is:
20772 -- a template parameter. Template template parameters are types
20773 for us (since TYPE_P holds true for them) so we handle
20774 them here. */
20775 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20776 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20777 return true;
20778 /* -- a qualified-id with a nested-name-specifier which contains a
20779 class-name that names a dependent type or whose unqualified-id
20780 names a dependent type. */
20781 if (TREE_CODE (type) == TYPENAME_TYPE)
20782 return true;
20783 /* -- a cv-qualified type where the cv-unqualified type is
20784 dependent. */
20785 type = TYPE_MAIN_VARIANT (type);
20786 /* -- a compound type constructed from any dependent type. */
20787 if (TYPE_PTRMEM_P (type))
20788 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20789 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20790 (type)));
20791 else if (TYPE_PTR_P (type)
20792 || TREE_CODE (type) == REFERENCE_TYPE)
20793 return dependent_type_p (TREE_TYPE (type));
20794 else if (TREE_CODE (type) == FUNCTION_TYPE
20795 || TREE_CODE (type) == METHOD_TYPE)
20797 tree arg_type;
20799 if (dependent_type_p (TREE_TYPE (type)))
20800 return true;
20801 for (arg_type = TYPE_ARG_TYPES (type);
20802 arg_type;
20803 arg_type = TREE_CHAIN (arg_type))
20804 if (dependent_type_p (TREE_VALUE (arg_type)))
20805 return true;
20806 return false;
20808 /* -- an array type constructed from any dependent type or whose
20809 size is specified by a constant expression that is
20810 value-dependent.
20812 We checked for type- and value-dependence of the bounds in
20813 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20814 if (TREE_CODE (type) == ARRAY_TYPE)
20816 if (TYPE_DOMAIN (type)
20817 && dependent_type_p (TYPE_DOMAIN (type)))
20818 return true;
20819 return dependent_type_p (TREE_TYPE (type));
20822 /* -- a template-id in which either the template name is a template
20823 parameter ... */
20824 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20825 return true;
20826 /* ... or any of the template arguments is a dependent type or
20827 an expression that is type-dependent or value-dependent. */
20828 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20829 && (any_dependent_template_arguments_p
20830 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20831 return true;
20833 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20834 dependent; if the argument of the `typeof' expression is not
20835 type-dependent, then it should already been have resolved. */
20836 if (TREE_CODE (type) == TYPEOF_TYPE
20837 || TREE_CODE (type) == DECLTYPE_TYPE
20838 || TREE_CODE (type) == UNDERLYING_TYPE)
20839 return true;
20841 /* A template argument pack is dependent if any of its packed
20842 arguments are. */
20843 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20845 tree args = ARGUMENT_PACK_ARGS (type);
20846 int i, len = TREE_VEC_LENGTH (args);
20847 for (i = 0; i < len; ++i)
20848 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20849 return true;
20852 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20853 be template parameters. */
20854 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20855 return true;
20857 /* The standard does not specifically mention types that are local
20858 to template functions or local classes, but they should be
20859 considered dependent too. For example:
20861 template <int I> void f() {
20862 enum E { a = I };
20863 S<sizeof (E)> s;
20866 The size of `E' cannot be known until the value of `I' has been
20867 determined. Therefore, `E' must be considered dependent. */
20868 scope = TYPE_CONTEXT (type);
20869 if (scope && TYPE_P (scope))
20870 return dependent_type_p (scope);
20871 /* Don't use type_dependent_expression_p here, as it can lead
20872 to infinite recursion trying to determine whether a lambda
20873 nested in a lambda is dependent (c++/47687). */
20874 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20875 && DECL_LANG_SPECIFIC (scope)
20876 && DECL_TEMPLATE_INFO (scope)
20877 && (any_dependent_template_arguments_p
20878 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20879 return true;
20881 /* Other types are non-dependent. */
20882 return false;
20885 /* Returns TRUE if TYPE is dependent, in the sense of
20886 [temp.dep.type]. Note that a NULL type is considered dependent. */
20888 bool
20889 dependent_type_p (tree type)
20891 /* If there are no template parameters in scope, then there can't be
20892 any dependent types. */
20893 if (!processing_template_decl)
20895 /* If we are not processing a template, then nobody should be
20896 providing us with a dependent type. */
20897 gcc_assert (type);
20898 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20899 return false;
20902 /* If the type is NULL, we have not computed a type for the entity
20903 in question; in that case, the type is dependent. */
20904 if (!type)
20905 return true;
20907 /* Erroneous types can be considered non-dependent. */
20908 if (type == error_mark_node)
20909 return false;
20911 /* If we have not already computed the appropriate value for TYPE,
20912 do so now. */
20913 if (!TYPE_DEPENDENT_P_VALID (type))
20915 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20916 TYPE_DEPENDENT_P_VALID (type) = 1;
20919 return TYPE_DEPENDENT_P (type);
20922 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20923 lookup. In other words, a dependent type that is not the current
20924 instantiation. */
20926 bool
20927 dependent_scope_p (tree scope)
20929 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20930 && !currently_open_class (scope));
20933 /* T is a SCOPE_REF; return whether we need to consider it
20934 instantiation-dependent so that we can check access at instantiation
20935 time even though we know which member it resolves to. */
20937 static bool
20938 instantiation_dependent_scope_ref_p (tree t)
20940 if (DECL_P (TREE_OPERAND (t, 1))
20941 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20942 && accessible_in_template_p (TREE_OPERAND (t, 0),
20943 TREE_OPERAND (t, 1)))
20944 return false;
20945 else
20946 return true;
20949 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20950 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20951 expression. */
20953 /* Note that this predicate is not appropriate for general expressions;
20954 only constant expressions (that satisfy potential_constant_expression)
20955 can be tested for value dependence. */
20957 bool
20958 value_dependent_expression_p (tree expression)
20960 if (!processing_template_decl)
20961 return false;
20963 /* A name declared with a dependent type. */
20964 if (DECL_P (expression) && type_dependent_expression_p (expression))
20965 return true;
20967 switch (TREE_CODE (expression))
20969 case IDENTIFIER_NODE:
20970 /* A name that has not been looked up -- must be dependent. */
20971 return true;
20973 case TEMPLATE_PARM_INDEX:
20974 /* A non-type template parm. */
20975 return true;
20977 case CONST_DECL:
20978 /* A non-type template parm. */
20979 if (DECL_TEMPLATE_PARM_P (expression))
20980 return true;
20981 return value_dependent_expression_p (DECL_INITIAL (expression));
20983 case VAR_DECL:
20984 /* A constant with literal type and is initialized
20985 with an expression that is value-dependent.
20987 Note that a non-dependent parenthesized initializer will have
20988 already been replaced with its constant value, so if we see
20989 a TREE_LIST it must be dependent. */
20990 if (DECL_INITIAL (expression)
20991 && decl_constant_var_p (expression)
20992 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20993 || value_dependent_expression_p (DECL_INITIAL (expression))))
20994 return true;
20995 return false;
20997 case DYNAMIC_CAST_EXPR:
20998 case STATIC_CAST_EXPR:
20999 case CONST_CAST_EXPR:
21000 case REINTERPRET_CAST_EXPR:
21001 case CAST_EXPR:
21002 /* These expressions are value-dependent if the type to which
21003 the cast occurs is dependent or the expression being casted
21004 is value-dependent. */
21006 tree type = TREE_TYPE (expression);
21008 if (dependent_type_p (type))
21009 return true;
21011 /* A functional cast has a list of operands. */
21012 expression = TREE_OPERAND (expression, 0);
21013 if (!expression)
21015 /* If there are no operands, it must be an expression such
21016 as "int()". This should not happen for aggregate types
21017 because it would form non-constant expressions. */
21018 gcc_assert (cxx_dialect >= cxx11
21019 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
21021 return false;
21024 if (TREE_CODE (expression) == TREE_LIST)
21025 return any_value_dependent_elements_p (expression);
21027 return value_dependent_expression_p (expression);
21030 case SIZEOF_EXPR:
21031 if (SIZEOF_EXPR_TYPE_P (expression))
21032 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
21033 /* FALLTHRU */
21034 case ALIGNOF_EXPR:
21035 case TYPEID_EXPR:
21036 /* A `sizeof' expression is value-dependent if the operand is
21037 type-dependent or is a pack expansion. */
21038 expression = TREE_OPERAND (expression, 0);
21039 if (PACK_EXPANSION_P (expression))
21040 return true;
21041 else if (TYPE_P (expression))
21042 return dependent_type_p (expression);
21043 return instantiation_dependent_expression_p (expression);
21045 case AT_ENCODE_EXPR:
21046 /* An 'encode' expression is value-dependent if the operand is
21047 type-dependent. */
21048 expression = TREE_OPERAND (expression, 0);
21049 return dependent_type_p (expression);
21051 case NOEXCEPT_EXPR:
21052 expression = TREE_OPERAND (expression, 0);
21053 return instantiation_dependent_expression_p (expression);
21055 case SCOPE_REF:
21056 /* All instantiation-dependent expressions should also be considered
21057 value-dependent. */
21058 return instantiation_dependent_scope_ref_p (expression);
21060 case COMPONENT_REF:
21061 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
21062 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
21064 case NONTYPE_ARGUMENT_PACK:
21065 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21066 is value-dependent. */
21068 tree values = ARGUMENT_PACK_ARGS (expression);
21069 int i, len = TREE_VEC_LENGTH (values);
21071 for (i = 0; i < len; ++i)
21072 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
21073 return true;
21075 return false;
21078 case TRAIT_EXPR:
21080 tree type2 = TRAIT_EXPR_TYPE2 (expression);
21081 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
21082 || (type2 ? dependent_type_p (type2) : false));
21085 case MODOP_EXPR:
21086 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21087 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
21089 case ARRAY_REF:
21090 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21091 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
21093 case ADDR_EXPR:
21095 tree op = TREE_OPERAND (expression, 0);
21096 return (value_dependent_expression_p (op)
21097 || has_value_dependent_address (op));
21100 case CALL_EXPR:
21102 tree fn = get_callee_fndecl (expression);
21103 int i, nargs;
21104 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
21105 return true;
21106 nargs = call_expr_nargs (expression);
21107 for (i = 0; i < nargs; ++i)
21109 tree op = CALL_EXPR_ARG (expression, i);
21110 /* In a call to a constexpr member function, look through the
21111 implicit ADDR_EXPR on the object argument so that it doesn't
21112 cause the call to be considered value-dependent. We also
21113 look through it in potential_constant_expression. */
21114 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
21115 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21116 && TREE_CODE (op) == ADDR_EXPR)
21117 op = TREE_OPERAND (op, 0);
21118 if (value_dependent_expression_p (op))
21119 return true;
21121 return false;
21124 case TEMPLATE_ID_EXPR:
21125 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21126 type-dependent. */
21127 return type_dependent_expression_p (expression);
21129 case CONSTRUCTOR:
21131 unsigned ix;
21132 tree val;
21133 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
21134 if (value_dependent_expression_p (val))
21135 return true;
21136 return false;
21139 case STMT_EXPR:
21140 /* Treat a GNU statement expression as dependent to avoid crashing
21141 under fold_non_dependent_expr; it can't be constant. */
21142 return true;
21144 default:
21145 /* A constant expression is value-dependent if any subexpression is
21146 value-dependent. */
21147 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
21149 case tcc_reference:
21150 case tcc_unary:
21151 case tcc_comparison:
21152 case tcc_binary:
21153 case tcc_expression:
21154 case tcc_vl_exp:
21156 int i, len = cp_tree_operand_length (expression);
21158 for (i = 0; i < len; i++)
21160 tree t = TREE_OPERAND (expression, i);
21162 /* In some cases, some of the operands may be missing.l
21163 (For example, in the case of PREDECREMENT_EXPR, the
21164 amount to increment by may be missing.) That doesn't
21165 make the expression dependent. */
21166 if (t && value_dependent_expression_p (t))
21167 return true;
21170 break;
21171 default:
21172 break;
21174 break;
21177 /* The expression is not value-dependent. */
21178 return false;
21181 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21182 [temp.dep.expr]. Note that an expression with no type is
21183 considered dependent. Other parts of the compiler arrange for an
21184 expression with type-dependent subexpressions to have no type, so
21185 this function doesn't have to be fully recursive. */
21187 bool
21188 type_dependent_expression_p (tree expression)
21190 if (!processing_template_decl)
21191 return false;
21193 if (expression == NULL_TREE || expression == error_mark_node)
21194 return false;
21196 /* An unresolved name is always dependent. */
21197 if (identifier_p (expression)
21198 || TREE_CODE (expression) == USING_DECL
21199 || TREE_CODE (expression) == PLACEHOLDER_EXPR)
21200 return true;
21202 /* Some expression forms are never type-dependent. */
21203 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
21204 || TREE_CODE (expression) == SIZEOF_EXPR
21205 || TREE_CODE (expression) == ALIGNOF_EXPR
21206 || TREE_CODE (expression) == AT_ENCODE_EXPR
21207 || TREE_CODE (expression) == NOEXCEPT_EXPR
21208 || TREE_CODE (expression) == TRAIT_EXPR
21209 || TREE_CODE (expression) == TYPEID_EXPR
21210 || TREE_CODE (expression) == DELETE_EXPR
21211 || TREE_CODE (expression) == VEC_DELETE_EXPR
21212 || TREE_CODE (expression) == THROW_EXPR)
21213 return false;
21215 /* The types of these expressions depends only on the type to which
21216 the cast occurs. */
21217 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
21218 || TREE_CODE (expression) == STATIC_CAST_EXPR
21219 || TREE_CODE (expression) == CONST_CAST_EXPR
21220 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
21221 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
21222 || TREE_CODE (expression) == CAST_EXPR)
21223 return dependent_type_p (TREE_TYPE (expression));
21225 /* The types of these expressions depends only on the type created
21226 by the expression. */
21227 if (TREE_CODE (expression) == NEW_EXPR
21228 || TREE_CODE (expression) == VEC_NEW_EXPR)
21230 /* For NEW_EXPR tree nodes created inside a template, either
21231 the object type itself or a TREE_LIST may appear as the
21232 operand 1. */
21233 tree type = TREE_OPERAND (expression, 1);
21234 if (TREE_CODE (type) == TREE_LIST)
21235 /* This is an array type. We need to check array dimensions
21236 as well. */
21237 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
21238 || value_dependent_expression_p
21239 (TREE_OPERAND (TREE_VALUE (type), 1));
21240 else
21241 return dependent_type_p (type);
21244 if (TREE_CODE (expression) == SCOPE_REF)
21246 tree scope = TREE_OPERAND (expression, 0);
21247 tree name = TREE_OPERAND (expression, 1);
21249 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21250 contains an identifier associated by name lookup with one or more
21251 declarations declared with a dependent type, or...a
21252 nested-name-specifier or qualified-id that names a member of an
21253 unknown specialization. */
21254 return (type_dependent_expression_p (name)
21255 || dependent_scope_p (scope));
21258 if (TREE_CODE (expression) == FUNCTION_DECL
21259 && DECL_LANG_SPECIFIC (expression)
21260 && DECL_TEMPLATE_INFO (expression)
21261 && (any_dependent_template_arguments_p
21262 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21263 return true;
21265 if (TREE_CODE (expression) == TEMPLATE_DECL
21266 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21267 return false;
21269 if (TREE_CODE (expression) == STMT_EXPR)
21270 expression = stmt_expr_value_expr (expression);
21272 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21274 tree elt;
21275 unsigned i;
21277 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
21279 if (type_dependent_expression_p (elt))
21280 return true;
21282 return false;
21285 /* A static data member of the current instantiation with incomplete
21286 array type is type-dependent, as the definition and specializations
21287 can have different bounds. */
21288 if (VAR_P (expression)
21289 && DECL_CLASS_SCOPE_P (expression)
21290 && dependent_type_p (DECL_CONTEXT (expression))
21291 && VAR_HAD_UNKNOWN_BOUND (expression))
21292 return true;
21294 /* An array of unknown bound depending on a variadic parameter, eg:
21296 template<typename... Args>
21297 void foo (Args... args)
21299 int arr[] = { args... };
21302 template<int... vals>
21303 void bar ()
21305 int arr[] = { vals... };
21308 If the array has no length and has an initializer, it must be that
21309 we couldn't determine its length in cp_complete_array_type because
21310 it is dependent. */
21311 if (VAR_P (expression)
21312 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
21313 && !TYPE_DOMAIN (TREE_TYPE (expression))
21314 && DECL_INITIAL (expression))
21315 return true;
21317 if (TREE_TYPE (expression) == unknown_type_node)
21319 if (TREE_CODE (expression) == ADDR_EXPR)
21320 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21321 if (TREE_CODE (expression) == COMPONENT_REF
21322 || TREE_CODE (expression) == OFFSET_REF)
21324 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
21325 return true;
21326 expression = TREE_OPERAND (expression, 1);
21327 if (identifier_p (expression))
21328 return false;
21330 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21331 if (TREE_CODE (expression) == SCOPE_REF)
21332 return false;
21334 /* Always dependent, on the number of arguments if nothing else. */
21335 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21336 return true;
21338 if (BASELINK_P (expression))
21339 expression = BASELINK_FUNCTIONS (expression);
21341 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21343 if (any_dependent_template_arguments_p
21344 (TREE_OPERAND (expression, 1)))
21345 return true;
21346 expression = TREE_OPERAND (expression, 0);
21348 gcc_assert (TREE_CODE (expression) == OVERLOAD
21349 || TREE_CODE (expression) == FUNCTION_DECL);
21351 while (expression)
21353 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21354 return true;
21355 expression = OVL_NEXT (expression);
21357 return false;
21360 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21362 return (dependent_type_p (TREE_TYPE (expression)));
21365 /* walk_tree callback function for instantiation_dependent_expression_p,
21366 below. Returns non-zero if a dependent subexpression is found. */
21368 static tree
21369 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21370 void * /*data*/)
21372 if (TYPE_P (*tp))
21374 /* We don't have to worry about decltype currently because decltype
21375 of an instantiation-dependent expr is a dependent type. This
21376 might change depending on the resolution of DR 1172. */
21377 *walk_subtrees = false;
21378 return NULL_TREE;
21380 enum tree_code code = TREE_CODE (*tp);
21381 switch (code)
21383 /* Don't treat an argument list as dependent just because it has no
21384 TREE_TYPE. */
21385 case TREE_LIST:
21386 case TREE_VEC:
21387 return NULL_TREE;
21389 case VAR_DECL:
21390 case CONST_DECL:
21391 /* A constant with a dependent initializer is dependent. */
21392 if (value_dependent_expression_p (*tp))
21393 return *tp;
21394 break;
21396 case TEMPLATE_PARM_INDEX:
21397 return *tp;
21399 /* Handle expressions with type operands. */
21400 case SIZEOF_EXPR:
21401 case ALIGNOF_EXPR:
21402 case TYPEID_EXPR:
21403 case AT_ENCODE_EXPR:
21405 tree op = TREE_OPERAND (*tp, 0);
21406 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21407 op = TREE_TYPE (op);
21408 if (TYPE_P (op))
21410 if (dependent_type_p (op))
21411 return *tp;
21412 else
21414 *walk_subtrees = false;
21415 return NULL_TREE;
21418 break;
21421 case TRAIT_EXPR:
21423 bool l = dependent_type_p (TRAIT_EXPR_TYPE1 (*tp));
21424 bool r = is_binary_trait (TRAIT_EXPR_KIND (*tp))
21425 ? dependent_type_p (TRAIT_EXPR_TYPE1 (*tp)) : false;
21426 if (l || r)
21427 return *tp;
21428 *walk_subtrees = false;
21429 return NULL_TREE;
21432 case COMPONENT_REF:
21433 if (identifier_p (TREE_OPERAND (*tp, 1)))
21434 /* In a template, finish_class_member_access_expr creates a
21435 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21436 type-dependent, so that we can check access control at
21437 instantiation time (PR 42277). See also Core issue 1273. */
21438 return *tp;
21439 break;
21441 case SCOPE_REF:
21442 if (instantiation_dependent_scope_ref_p (*tp))
21443 return *tp;
21444 else
21445 break;
21447 /* Treat statement-expressions as dependent. */
21448 case BIND_EXPR:
21449 return *tp;
21451 default:
21452 break;
21455 if (type_dependent_expression_p (*tp))
21456 return *tp;
21457 else
21458 return NULL_TREE;
21461 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21462 sense defined by the ABI:
21464 "An expression is instantiation-dependent if it is type-dependent
21465 or value-dependent, or it has a subexpression that is type-dependent
21466 or value-dependent." */
21468 bool
21469 instantiation_dependent_expression_p (tree expression)
21471 tree result;
21473 if (!processing_template_decl)
21474 return false;
21476 if (expression == error_mark_node)
21477 return false;
21479 result = cp_walk_tree_without_duplicates (&expression,
21480 instantiation_dependent_r, NULL);
21481 return result != NULL_TREE;
21484 /* Like type_dependent_expression_p, but it also works while not processing
21485 a template definition, i.e. during substitution or mangling. */
21487 bool
21488 type_dependent_expression_p_push (tree expr)
21490 bool b;
21491 ++processing_template_decl;
21492 b = type_dependent_expression_p (expr);
21493 --processing_template_decl;
21494 return b;
21497 /* Returns TRUE if ARGS contains a type-dependent expression. */
21499 bool
21500 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21502 unsigned int i;
21503 tree arg;
21505 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21507 if (type_dependent_expression_p (arg))
21508 return true;
21510 return false;
21513 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21514 expressions) contains any type-dependent expressions. */
21516 bool
21517 any_type_dependent_elements_p (const_tree list)
21519 for (; list; list = TREE_CHAIN (list))
21520 if (type_dependent_expression_p (TREE_VALUE (list)))
21521 return true;
21523 return false;
21526 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21527 expressions) contains any value-dependent expressions. */
21529 bool
21530 any_value_dependent_elements_p (const_tree list)
21532 for (; list; list = TREE_CHAIN (list))
21533 if (value_dependent_expression_p (TREE_VALUE (list)))
21534 return true;
21536 return false;
21539 /* Returns TRUE if the ARG (a template argument) is dependent. */
21541 bool
21542 dependent_template_arg_p (tree arg)
21544 if (!processing_template_decl)
21545 return false;
21547 /* Assume a template argument that was wrongly written by the user
21548 is dependent. This is consistent with what
21549 any_dependent_template_arguments_p [that calls this function]
21550 does. */
21551 if (!arg || arg == error_mark_node)
21552 return true;
21554 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21555 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21557 if (TREE_CODE (arg) == TEMPLATE_DECL
21558 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21559 return dependent_template_p (arg);
21560 else if (ARGUMENT_PACK_P (arg))
21562 tree args = ARGUMENT_PACK_ARGS (arg);
21563 int i, len = TREE_VEC_LENGTH (args);
21564 for (i = 0; i < len; ++i)
21566 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21567 return true;
21570 return false;
21572 else if (TYPE_P (arg))
21573 return dependent_type_p (arg);
21574 else
21575 return (type_dependent_expression_p (arg)
21576 || value_dependent_expression_p (arg));
21579 /* Returns true if ARGS (a collection of template arguments) contains
21580 any types that require structural equality testing. */
21582 bool
21583 any_template_arguments_need_structural_equality_p (tree args)
21585 int i;
21586 int j;
21588 if (!args)
21589 return false;
21590 if (args == error_mark_node)
21591 return true;
21593 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21595 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21596 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21598 tree arg = TREE_VEC_ELT (level, j);
21599 tree packed_args = NULL_TREE;
21600 int k, len = 1;
21602 if (ARGUMENT_PACK_P (arg))
21604 /* Look inside the argument pack. */
21605 packed_args = ARGUMENT_PACK_ARGS (arg);
21606 len = TREE_VEC_LENGTH (packed_args);
21609 for (k = 0; k < len; ++k)
21611 if (packed_args)
21612 arg = TREE_VEC_ELT (packed_args, k);
21614 if (error_operand_p (arg))
21615 return true;
21616 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21617 continue;
21618 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21619 return true;
21620 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21621 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21622 return true;
21627 return false;
21630 /* Returns true if ARGS (a collection of template arguments) contains
21631 any dependent arguments. */
21633 bool
21634 any_dependent_template_arguments_p (const_tree args)
21636 int i;
21637 int j;
21639 if (!args)
21640 return false;
21641 if (args == error_mark_node)
21642 return true;
21644 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21646 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21647 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21648 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21649 return true;
21652 return false;
21655 /* Returns TRUE if the template TMPL is dependent. */
21657 bool
21658 dependent_template_p (tree tmpl)
21660 if (TREE_CODE (tmpl) == OVERLOAD)
21662 while (tmpl)
21664 if (dependent_template_p (OVL_CURRENT (tmpl)))
21665 return true;
21666 tmpl = OVL_NEXT (tmpl);
21668 return false;
21671 /* Template template parameters are dependent. */
21672 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21673 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21674 return true;
21675 /* So are names that have not been looked up. */
21676 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21677 return true;
21678 /* So are member templates of dependent classes. */
21679 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21680 return dependent_type_p (DECL_CONTEXT (tmpl));
21681 return false;
21684 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21686 bool
21687 dependent_template_id_p (tree tmpl, tree args)
21689 return (dependent_template_p (tmpl)
21690 || any_dependent_template_arguments_p (args));
21693 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21694 is dependent. */
21696 bool
21697 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21699 int i;
21701 if (!processing_template_decl)
21702 return false;
21704 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21706 tree decl = TREE_VEC_ELT (declv, i);
21707 tree init = TREE_VEC_ELT (initv, i);
21708 tree cond = TREE_VEC_ELT (condv, i);
21709 tree incr = TREE_VEC_ELT (incrv, i);
21711 if (type_dependent_expression_p (decl))
21712 return true;
21714 if (init && type_dependent_expression_p (init))
21715 return true;
21717 if (type_dependent_expression_p (cond))
21718 return true;
21720 if (COMPARISON_CLASS_P (cond)
21721 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21722 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21723 return true;
21725 if (TREE_CODE (incr) == MODOP_EXPR)
21727 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21728 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21729 return true;
21731 else if (type_dependent_expression_p (incr))
21732 return true;
21733 else if (TREE_CODE (incr) == MODIFY_EXPR)
21735 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21736 return true;
21737 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21739 tree t = TREE_OPERAND (incr, 1);
21740 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21741 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21742 return true;
21747 return false;
21750 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21751 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21752 no such TYPE can be found. Note that this function peers inside
21753 uninstantiated templates and therefore should be used only in
21754 extremely limited situations. ONLY_CURRENT_P restricts this
21755 peering to the currently open classes hierarchy (which is required
21756 when comparing types). */
21758 tree
21759 resolve_typename_type (tree type, bool only_current_p)
21761 tree scope;
21762 tree name;
21763 tree decl;
21764 int quals;
21765 tree pushed_scope;
21766 tree result;
21768 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21770 scope = TYPE_CONTEXT (type);
21771 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21772 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21773 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21774 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21775 identifier of the TYPENAME_TYPE anymore.
21776 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21777 TYPENAME_TYPE instead, we avoid messing up with a possible
21778 typedef variant case. */
21779 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21781 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21782 it first before we can figure out what NAME refers to. */
21783 if (TREE_CODE (scope) == TYPENAME_TYPE)
21785 if (TYPENAME_IS_RESOLVING_P (scope))
21786 /* Given a class template A with a dependent base with nested type C,
21787 typedef typename A::C::C C will land us here, as trying to resolve
21788 the initial A::C leads to the local C typedef, which leads back to
21789 A::C::C. So we break the recursion now. */
21790 return type;
21791 else
21792 scope = resolve_typename_type (scope, only_current_p);
21794 /* If we don't know what SCOPE refers to, then we cannot resolve the
21795 TYPENAME_TYPE. */
21796 if (TREE_CODE (scope) == TYPENAME_TYPE)
21797 return type;
21798 /* If the SCOPE is a template type parameter, we have no way of
21799 resolving the name. */
21800 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21801 return type;
21802 /* If the SCOPE is not the current instantiation, there's no reason
21803 to look inside it. */
21804 if (only_current_p && !currently_open_class (scope))
21805 return type;
21806 /* If this is a typedef, we don't want to look inside (c++/11987). */
21807 if (typedef_variant_p (type))
21808 return type;
21809 /* If SCOPE isn't the template itself, it will not have a valid
21810 TYPE_FIELDS list. */
21811 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21812 /* scope is either the template itself or a compatible instantiation
21813 like X<T>, so look up the name in the original template. */
21814 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21815 else
21816 /* scope is a partial instantiation, so we can't do the lookup or we
21817 will lose the template arguments. */
21818 return type;
21819 /* Enter the SCOPE so that name lookup will be resolved as if we
21820 were in the class definition. In particular, SCOPE will no
21821 longer be considered a dependent type. */
21822 pushed_scope = push_scope (scope);
21823 /* Look up the declaration. */
21824 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21825 tf_warning_or_error);
21827 result = NULL_TREE;
21829 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21830 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21831 if (!decl)
21832 /*nop*/;
21833 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21834 && TREE_CODE (decl) == TYPE_DECL)
21836 result = TREE_TYPE (decl);
21837 if (result == error_mark_node)
21838 result = NULL_TREE;
21840 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21841 && DECL_CLASS_TEMPLATE_P (decl))
21843 tree tmpl;
21844 tree args;
21845 /* Obtain the template and the arguments. */
21846 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21847 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21848 /* Instantiate the template. */
21849 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21850 /*entering_scope=*/0,
21851 tf_error | tf_user);
21852 if (result == error_mark_node)
21853 result = NULL_TREE;
21856 /* Leave the SCOPE. */
21857 if (pushed_scope)
21858 pop_scope (pushed_scope);
21860 /* If we failed to resolve it, return the original typename. */
21861 if (!result)
21862 return type;
21864 /* If lookup found a typename type, resolve that too. */
21865 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21867 /* Ill-formed programs can cause infinite recursion here, so we
21868 must catch that. */
21869 TYPENAME_IS_RESOLVING_P (type) = 1;
21870 result = resolve_typename_type (result, only_current_p);
21871 TYPENAME_IS_RESOLVING_P (type) = 0;
21874 /* Qualify the resulting type. */
21875 quals = cp_type_quals (type);
21876 if (quals)
21877 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21879 return result;
21882 /* EXPR is an expression which is not type-dependent. Return a proxy
21883 for EXPR that can be used to compute the types of larger
21884 expressions containing EXPR. */
21886 tree
21887 build_non_dependent_expr (tree expr)
21889 tree inner_expr;
21891 #ifdef ENABLE_CHECKING
21892 /* Try to get a constant value for all non-dependent expressions in
21893 order to expose bugs in *_dependent_expression_p and constexpr. */
21894 if (cxx_dialect >= cxx11)
21895 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21896 #endif
21898 /* Preserve OVERLOADs; the functions must be available to resolve
21899 types. */
21900 inner_expr = expr;
21901 if (TREE_CODE (inner_expr) == STMT_EXPR)
21902 inner_expr = stmt_expr_value_expr (inner_expr);
21903 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21904 inner_expr = TREE_OPERAND (inner_expr, 0);
21905 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21906 inner_expr = TREE_OPERAND (inner_expr, 1);
21907 if (is_overloaded_fn (inner_expr)
21908 || TREE_CODE (inner_expr) == OFFSET_REF)
21909 return expr;
21910 /* There is no need to return a proxy for a variable. */
21911 if (VAR_P (expr))
21912 return expr;
21913 /* Preserve string constants; conversions from string constants to
21914 "char *" are allowed, even though normally a "const char *"
21915 cannot be used to initialize a "char *". */
21916 if (TREE_CODE (expr) == STRING_CST)
21917 return expr;
21918 /* Preserve arithmetic constants, as an optimization -- there is no
21919 reason to create a new node. */
21920 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21921 return expr;
21922 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21923 There is at least one place where we want to know that a
21924 particular expression is a throw-expression: when checking a ?:
21925 expression, there are special rules if the second or third
21926 argument is a throw-expression. */
21927 if (TREE_CODE (expr) == THROW_EXPR)
21928 return expr;
21930 /* Don't wrap an initializer list, we need to be able to look inside. */
21931 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21932 return expr;
21934 /* Don't wrap a dummy object, we need to be able to test for it. */
21935 if (is_dummy_object (expr))
21936 return expr;
21938 if (TREE_CODE (expr) == COND_EXPR)
21939 return build3 (COND_EXPR,
21940 TREE_TYPE (expr),
21941 TREE_OPERAND (expr, 0),
21942 (TREE_OPERAND (expr, 1)
21943 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21944 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21945 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21946 if (TREE_CODE (expr) == COMPOUND_EXPR
21947 && !COMPOUND_EXPR_OVERLOADED (expr))
21948 return build2 (COMPOUND_EXPR,
21949 TREE_TYPE (expr),
21950 TREE_OPERAND (expr, 0),
21951 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21953 /* If the type is unknown, it can't really be non-dependent */
21954 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21956 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21957 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21960 /* ARGS is a vector of expressions as arguments to a function call.
21961 Replace the arguments with equivalent non-dependent expressions.
21962 This modifies ARGS in place. */
21964 void
21965 make_args_non_dependent (vec<tree, va_gc> *args)
21967 unsigned int ix;
21968 tree arg;
21970 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21972 tree newarg = build_non_dependent_expr (arg);
21973 if (newarg != arg)
21974 (*args)[ix] = newarg;
21978 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21979 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21980 parms. */
21982 static tree
21983 make_auto_1 (tree name)
21985 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21986 TYPE_NAME (au) = build_decl (input_location,
21987 TYPE_DECL, name, au);
21988 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21989 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21990 (0, processing_template_decl + 1, processing_template_decl + 1,
21991 TYPE_NAME (au), NULL_TREE);
21992 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21993 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21994 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21996 return au;
21999 tree
22000 make_decltype_auto (void)
22002 return make_auto_1 (get_identifier ("decltype(auto)"));
22005 tree
22006 make_auto (void)
22008 return make_auto_1 (get_identifier ("auto"));
22011 /* Given type ARG, return std::initializer_list<ARG>. */
22013 static tree
22014 listify (tree arg)
22016 tree std_init_list = namespace_binding
22017 (get_identifier ("initializer_list"), std_node);
22018 tree argvec;
22019 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
22021 error ("deducing from brace-enclosed initializer list requires "
22022 "#include <initializer_list>");
22023 return error_mark_node;
22025 argvec = make_tree_vec (1);
22026 TREE_VEC_ELT (argvec, 0) = arg;
22027 return lookup_template_class (std_init_list, argvec, NULL_TREE,
22028 NULL_TREE, 0, tf_warning_or_error);
22031 /* Replace auto in TYPE with std::initializer_list<auto>. */
22033 static tree
22034 listify_autos (tree type, tree auto_node)
22036 tree init_auto = listify (auto_node);
22037 tree argvec = make_tree_vec (1);
22038 TREE_VEC_ELT (argvec, 0) = init_auto;
22039 if (processing_template_decl)
22040 argvec = add_to_template_args (current_template_args (), argvec);
22041 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22044 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22045 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22047 tree
22048 do_auto_deduction (tree type, tree init, tree auto_node)
22050 tree targs;
22052 if (init == error_mark_node)
22053 return error_mark_node;
22055 if (type_dependent_expression_p (init))
22056 /* Defining a subset of type-dependent expressions that we can deduce
22057 from ahead of time isn't worth the trouble. */
22058 return type;
22060 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22061 with either a new invented type template parameter U or, if the
22062 initializer is a braced-init-list (8.5.4), with
22063 std::initializer_list<U>. */
22064 if (BRACE_ENCLOSED_INITIALIZER_P (init))
22065 type = listify_autos (type, auto_node);
22067 init = resolve_nondeduced_context (init);
22069 targs = make_tree_vec (1);
22070 if (AUTO_IS_DECLTYPE (auto_node))
22072 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
22073 && !REF_PARENTHESIZED_P (init)));
22074 TREE_VEC_ELT (targs, 0)
22075 = finish_decltype_type (init, id, tf_warning_or_error);
22076 if (type != auto_node)
22078 error ("%qT as type rather than plain %<decltype(auto)%>", type);
22079 return error_mark_node;
22082 else
22084 tree parms = build_tree_list (NULL_TREE, type);
22085 tree tparms = make_tree_vec (1);
22086 int val;
22088 TREE_VEC_ELT (tparms, 0)
22089 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
22090 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
22091 DEDUCE_CALL, LOOKUP_NORMAL,
22092 NULL, /*explain_p=*/false);
22093 if (val > 0)
22095 if (processing_template_decl)
22096 /* Try again at instantiation time. */
22097 return type;
22098 if (type && type != error_mark_node)
22099 /* If type is error_mark_node a diagnostic must have been
22100 emitted by now. Also, having a mention to '<type error>'
22101 in the diagnostic is not really useful to the user. */
22103 if (cfun && auto_node == current_function_auto_return_pattern
22104 && LAMBDA_FUNCTION_P (current_function_decl))
22105 error ("unable to deduce lambda return type from %qE", init);
22106 else
22107 error ("unable to deduce %qT from %qE", type, init);
22109 return error_mark_node;
22113 /* If the list of declarators contains more than one declarator, the type
22114 of each declared variable is determined as described above. If the
22115 type deduced for the template parameter U is not the same in each
22116 deduction, the program is ill-formed. */
22117 if (TREE_TYPE (auto_node)
22118 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
22120 if (cfun && auto_node == current_function_auto_return_pattern
22121 && LAMBDA_FUNCTION_P (current_function_decl))
22122 error ("inconsistent types %qT and %qT deduced for "
22123 "lambda return type", TREE_TYPE (auto_node),
22124 TREE_VEC_ELT (targs, 0));
22125 else
22126 error ("inconsistent deduction for %qT: %qT and then %qT",
22127 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
22128 return error_mark_node;
22130 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
22132 if (processing_template_decl)
22133 targs = add_to_template_args (current_template_args (), targs);
22134 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
22137 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22138 result. */
22140 tree
22141 splice_late_return_type (tree type, tree late_return_type)
22143 tree argvec;
22145 if (late_return_type == NULL_TREE)
22146 return type;
22147 argvec = make_tree_vec (1);
22148 TREE_VEC_ELT (argvec, 0) = late_return_type;
22149 if (processing_template_parmlist)
22150 /* For a late-specified return type in a template type-parameter, we
22151 need to add a dummy argument level for its parmlist. */
22152 argvec = add_to_template_args
22153 (make_tree_vec (processing_template_parmlist), argvec);
22154 if (current_template_parms)
22155 argvec = add_to_template_args (current_template_args (), argvec);
22156 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22159 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22160 'decltype(auto)'. */
22162 bool
22163 is_auto (const_tree type)
22165 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22166 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
22167 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
22168 return true;
22169 else
22170 return false;
22173 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22174 a use of `auto'. Returns NULL_TREE otherwise. */
22176 tree
22177 type_uses_auto (tree type)
22179 return find_type_usage (type, is_auto);
22182 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22183 'decltype(auto)' or a concept. */
22185 bool
22186 is_auto_or_concept (const_tree type)
22188 return is_auto (type); // or concept
22191 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22192 a concept identifier) iff TYPE contains a use of a generic type. Returns
22193 NULL_TREE otherwise. */
22195 tree
22196 type_uses_auto_or_concept (tree type)
22198 return find_type_usage (type, is_auto_or_concept);
22202 /* For a given template T, return the vector of typedefs referenced
22203 in T for which access check is needed at T instantiation time.
22204 T is either a FUNCTION_DECL or a RECORD_TYPE.
22205 Those typedefs were added to T by the function
22206 append_type_to_template_for_access_check. */
22208 vec<qualified_typedef_usage_t, va_gc> *
22209 get_types_needing_access_check (tree t)
22211 tree ti;
22212 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
22214 if (!t || t == error_mark_node)
22215 return NULL;
22217 if (!(ti = get_template_info (t)))
22218 return NULL;
22220 if (CLASS_TYPE_P (t)
22221 || TREE_CODE (t) == FUNCTION_DECL)
22223 if (!TI_TEMPLATE (ti))
22224 return NULL;
22226 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
22229 return result;
22232 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22233 tied to T. That list of typedefs will be access checked at
22234 T instantiation time.
22235 T is either a FUNCTION_DECL or a RECORD_TYPE.
22236 TYPE_DECL is a TYPE_DECL node representing a typedef.
22237 SCOPE is the scope through which TYPE_DECL is accessed.
22238 LOCATION is the location of the usage point of TYPE_DECL.
22240 This function is a subroutine of
22241 append_type_to_template_for_access_check. */
22243 static void
22244 append_type_to_template_for_access_check_1 (tree t,
22245 tree type_decl,
22246 tree scope,
22247 location_t location)
22249 qualified_typedef_usage_t typedef_usage;
22250 tree ti;
22252 if (!t || t == error_mark_node)
22253 return;
22255 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
22256 || CLASS_TYPE_P (t))
22257 && type_decl
22258 && TREE_CODE (type_decl) == TYPE_DECL
22259 && scope);
22261 if (!(ti = get_template_info (t)))
22262 return;
22264 gcc_assert (TI_TEMPLATE (ti));
22266 typedef_usage.typedef_decl = type_decl;
22267 typedef_usage.context = scope;
22268 typedef_usage.locus = location;
22270 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
22273 /* Append TYPE_DECL to the template TEMPL.
22274 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22275 At TEMPL instanciation time, TYPE_DECL will be checked to see
22276 if it can be accessed through SCOPE.
22277 LOCATION is the location of the usage point of TYPE_DECL.
22279 e.g. consider the following code snippet:
22281 class C
22283 typedef int myint;
22286 template<class U> struct S
22288 C::myint mi; // <-- usage point of the typedef C::myint
22291 S<char> s;
22293 At S<char> instantiation time, we need to check the access of C::myint
22294 In other words, we need to check the access of the myint typedef through
22295 the C scope. For that purpose, this function will add the myint typedef
22296 and the scope C through which its being accessed to a list of typedefs
22297 tied to the template S. That list will be walked at template instantiation
22298 time and access check performed on each typedefs it contains.
22299 Note that this particular code snippet should yield an error because
22300 myint is private to C. */
22302 void
22303 append_type_to_template_for_access_check (tree templ,
22304 tree type_decl,
22305 tree scope,
22306 location_t location)
22308 qualified_typedef_usage_t *iter;
22309 unsigned i;
22311 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
22313 /* Make sure we don't append the type to the template twice. */
22314 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
22315 if (iter->typedef_decl == type_decl && scope == iter->context)
22316 return;
22318 append_type_to_template_for_access_check_1 (templ, type_decl,
22319 scope, location);
22322 /* Convert the generic type parameters in PARM that match the types given in the
22323 range [START_IDX, END_IDX) from the current_template_parms into generic type
22324 packs. */
22326 tree
22327 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22329 tree current = current_template_parms;
22330 int depth = TMPL_PARMS_DEPTH (current);
22331 current = INNERMOST_TEMPLATE_PARMS (current);
22332 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22334 for (int i = 0; i < start_idx; ++i)
22335 TREE_VEC_ELT (replacement, i)
22336 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22338 for (int i = start_idx; i < end_idx; ++i)
22340 /* Create a distinct parameter pack type from the current parm and add it
22341 to the replacement args to tsubst below into the generic function
22342 parameter. */
22344 tree o = TREE_TYPE (TREE_VALUE
22345 (TREE_VEC_ELT (current, i)));
22346 tree t = copy_type (o);
22347 TEMPLATE_TYPE_PARM_INDEX (t)
22348 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22349 o, 0, 0, tf_none);
22350 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22351 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22352 TYPE_MAIN_VARIANT (t) = t;
22353 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22354 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22355 TREE_VEC_ELT (replacement, i) = t;
22356 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22359 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22360 TREE_VEC_ELT (replacement, i)
22361 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22363 /* If there are more levels then build up the replacement with the outer
22364 template parms. */
22365 if (depth > 1)
22366 replacement = add_to_template_args (template_parms_to_args
22367 (TREE_CHAIN (current_template_parms)),
22368 replacement);
22370 return tsubst (parm, replacement, tf_none, NULL_TREE);
22374 /* Set up the hash tables for template instantiations. */
22376 void
22377 init_template_processing (void)
22379 decl_specializations = htab_create_ggc (37,
22380 hash_specialization,
22381 eq_specializations,
22382 ggc_free);
22383 type_specializations = htab_create_ggc (37,
22384 hash_specialization,
22385 eq_specializations,
22386 ggc_free);
22389 /* Print stats about the template hash tables for -fstats. */
22391 void
22392 print_template_statistics (void)
22394 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22395 "%f collisions\n", (long) htab_size (decl_specializations),
22396 (long) htab_elements (decl_specializations),
22397 htab_collisions (decl_specializations));
22398 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22399 "%f collisions\n", (long) htab_size (type_specializations),
22400 (long) htab_elements (type_specializations),
22401 htab_collisions (type_specializations));
22404 #include "gt-cp-pt.h"