* pt.c (tsubst <{NON,}TYPE_ARGUMENT_PACK>: Simplify control flow
[official-gcc.git] / gcc / cp / pt.c
blob97d0b4840fcb12d827ad1e686e4e5f90560b446b
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2016 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 "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
44 /* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46 typedef int (*tree_fn_t) (tree, void*);
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
56 static GTY(()) struct pending_template *pending_templates;
57 static GTY(()) struct pending_template *last_pending_template;
59 int processing_template_parmlist;
60 static int template_header_count;
62 static GTY(()) tree saved_trees;
63 static vec<int> inline_parm_levels;
65 static GTY(()) struct tinst_level *current_tinst_level;
67 static GTY(()) tree saved_access_scope;
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr;
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
77 // Implementation of the RAII helper for creating new local
78 // specializations.
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
82 local_specializations = new hash_map<tree, tree>;
85 local_specialization_stack::~local_specialization_stack ()
87 delete local_specializations;
88 local_specializations = saved;
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth;
94 struct GTY((for_user)) spec_entry
96 tree tmpl;
97 tree args;
98 tree spec;
101 struct spec_hasher : ggc_ptr_hash<spec_entry>
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
107 static GTY (()) hash_table<spec_hasher> *decl_specializations;
109 static GTY (()) hash_table<spec_hasher> *type_specializations;
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
126 enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
132 static void push_access_scope (tree);
133 static void pop_access_scope (tree);
134 static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137 static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139 static int unify (tree, tree, tree, tree, int, bool);
140 static void add_pending_template (tree);
141 static tree reopen_tinst_level (struct tinst_level *);
142 static tree tsubst_initializer_list (tree, tree);
143 static tree get_partial_spec_bindings (tree, tree, tree);
144 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
147 bool, bool);
148 static void tsubst_enum (tree, tree, tree);
149 static tree add_to_template_args (tree, tree);
150 static tree add_outermost_template_args (tree, tree);
151 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
152 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
153 tree);
154 static int type_unification_real (tree, tree, tree, const tree *,
155 unsigned int, int, unification_kind_t, int,
156 vec<deferred_access_check, va_gc> **,
157 bool);
158 static void note_template_header (int);
159 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
160 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
161 static tree convert_template_argument (tree, tree, tree,
162 tsubst_flags_t, int, tree);
163 static tree for_each_template_parm (tree, tree_fn_t, void*,
164 hash_set<tree> *, bool, tree_fn_t = NULL);
165 static tree expand_template_argument_pack (tree);
166 static tree build_template_parm_index (int, int, int, tree, tree);
167 static bool inline_needs_template_parms (tree, bool);
168 static void push_inline_template_parms_recursive (tree, int);
169 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170 static int mark_template_parm (tree, void *);
171 static int template_parm_this_level_p (tree, void *);
172 static tree tsubst_friend_function (tree, tree);
173 static tree tsubst_friend_class (tree, tree);
174 static int can_complete_type_without_circularity (tree);
175 static tree get_bindings (tree, tree, tree, bool);
176 static int template_decl_level (tree);
177 static int check_cv_quals_for_unify (int, tree, tree);
178 static void template_parm_level_and_index (tree, int*, int*);
179 static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181 static tree copy_template_args (tree);
182 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
184 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189 static bool check_specialization_scope (void);
190 static tree process_partial_specialization (tree);
191 static void set_current_access_from_decl (tree);
192 static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194 static tree try_class_unification (tree, tree, tree, tree, bool);
195 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197 static bool template_template_parm_bindings_ok_p (tree, tree);
198 static void tsubst_default_arguments (tree, tsubst_flags_t);
199 static tree for_each_template_parm_r (tree *, int *, void *);
200 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
201 static void copy_default_args_to_explicit_spec (tree);
202 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
203 static bool dependent_template_arg_p (tree);
204 static bool any_template_arguments_need_structural_equality_p (tree);
205 static bool dependent_type_p_r (tree);
206 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_decl (tree, tree, tsubst_flags_t);
208 static void perform_typedefs_access_check (tree tmpl, tree targs);
209 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
219 /* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
224 static void
225 push_access_scope (tree t)
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
228 || TREE_CODE (t) == TYPE_DECL);
230 if (DECL_FRIEND_CONTEXT (t))
231 push_nested_class (DECL_FRIEND_CONTEXT (t));
232 else if (DECL_CLASS_SCOPE_P (t))
233 push_nested_class (DECL_CONTEXT (t));
234 else
235 push_to_top_level ();
237 if (TREE_CODE (t) == FUNCTION_DECL)
239 saved_access_scope = tree_cons
240 (NULL_TREE, current_function_decl, saved_access_scope);
241 current_function_decl = t;
245 /* Restore the scope set up by push_access_scope. T is the node we
246 are processing. */
248 static void
249 pop_access_scope (tree t)
251 if (TREE_CODE (t) == FUNCTION_DECL)
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
257 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
268 tree
269 finish_member_template_decl (tree decl)
271 if (decl == error_mark_node)
272 return error_mark_node;
274 gcc_assert (DECL_P (decl));
276 if (TREE_CODE (decl) == TYPE_DECL)
278 tree type;
280 type = TREE_TYPE (decl);
281 if (type == error_mark_node)
282 return error_mark_node;
283 if (MAYBE_CLASS_TYPE_P (type)
284 && CLASSTYPE_TEMPLATE_INFO (type)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
287 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
288 check_member_template (tmpl);
289 return tmpl;
291 return NULL_TREE;
293 else if (TREE_CODE (decl) == FIELD_DECL)
294 error ("data member %qD cannot be a member template", decl);
295 else if (DECL_TEMPLATE_INFO (decl))
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
299 check_member_template (DECL_TI_TEMPLATE (decl));
300 return DECL_TI_TEMPLATE (decl);
302 else
303 return decl;
305 else
306 error ("invalid member template declaration %qD", decl);
308 return error_mark_node;
311 /* Create a template info node. */
313 tree
314 build_template_info (tree template_decl, tree template_args)
316 tree result = make_node (TEMPLATE_INFO);
317 TI_TEMPLATE (result) = template_decl;
318 TI_ARGS (result) = template_args;
319 return result;
322 /* Return the template info node corresponding to T, whatever T is. */
324 tree
325 get_template_info (const_tree t)
327 tree tinfo = NULL_TREE;
329 if (!t || t == error_mark_node)
330 return NULL;
332 if (TREE_CODE (t) == NAMESPACE_DECL
333 || TREE_CODE (t) == PARM_DECL)
334 return NULL;
336 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
337 tinfo = DECL_TEMPLATE_INFO (t);
339 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
340 t = TREE_TYPE (t);
342 if (OVERLOAD_TYPE_P (t))
343 tinfo = TYPE_TEMPLATE_INFO (t);
344 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
345 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
347 return tinfo;
350 /* Returns the template nesting level of the indicated class TYPE.
352 For example, in:
353 template <class T>
354 struct A
356 template <class U>
357 struct B {};
360 A<T>::B<U> has depth two, while A<T> has depth one.
361 Both A<T>::B<int> and A<int>::B<U> have depth one, if
362 they are instantiations, not specializations.
364 This function is guaranteed to return 0 if passed NULL_TREE so
365 that, for example, `template_class_depth (current_class_type)' is
366 always safe. */
369 template_class_depth (tree type)
371 int depth;
373 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
375 tree tinfo = get_template_info (type);
377 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
378 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
379 ++depth;
381 if (DECL_P (type))
382 type = CP_DECL_CONTEXT (type);
383 else if (LAMBDA_TYPE_P (type))
384 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
385 else
386 type = CP_TYPE_CONTEXT (type);
389 return depth;
392 /* Subroutine of maybe_begin_member_template_processing.
393 Returns true if processing DECL needs us to push template parms. */
395 static bool
396 inline_needs_template_parms (tree decl, bool nsdmi)
398 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
399 return false;
401 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
402 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
405 /* Subroutine of maybe_begin_member_template_processing.
406 Push the template parms in PARMS, starting from LEVELS steps into the
407 chain, and ending at the beginning, since template parms are listed
408 innermost first. */
410 static void
411 push_inline_template_parms_recursive (tree parmlist, int levels)
413 tree parms = TREE_VALUE (parmlist);
414 int i;
416 if (levels > 1)
417 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
419 ++processing_template_decl;
420 current_template_parms
421 = tree_cons (size_int (processing_template_decl),
422 parms, current_template_parms);
423 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
425 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
426 NULL);
427 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
431 if (error_operand_p (parm))
432 continue;
434 gcc_assert (DECL_P (parm));
436 switch (TREE_CODE (parm))
438 case TYPE_DECL:
439 case TEMPLATE_DECL:
440 pushdecl (parm);
441 break;
443 case PARM_DECL:
444 /* Push the CONST_DECL. */
445 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
446 break;
448 default:
449 gcc_unreachable ();
454 /* Restore the template parameter context for a member template, a
455 friend template defined in a class definition, or a non-template
456 member of template class. */
458 void
459 maybe_begin_member_template_processing (tree decl)
461 tree parms;
462 int levels = 0;
463 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
465 if (nsdmi)
467 tree ctx = DECL_CONTEXT (decl);
468 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
469 /* Disregard full specializations (c++/60999). */
470 && uses_template_parms (ctx)
471 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
474 if (inline_needs_template_parms (decl, nsdmi))
476 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
477 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
479 if (DECL_TEMPLATE_SPECIALIZATION (decl))
481 --levels;
482 parms = TREE_CHAIN (parms);
485 push_inline_template_parms_recursive (parms, levels);
488 /* Remember how many levels of template parameters we pushed so that
489 we can pop them later. */
490 inline_parm_levels.safe_push (levels);
493 /* Undo the effects of maybe_begin_member_template_processing. */
495 void
496 maybe_end_member_template_processing (void)
498 int i;
499 int last;
501 if (inline_parm_levels.length () == 0)
502 return;
504 last = inline_parm_levels.pop ();
505 for (i = 0; i < last; ++i)
507 --processing_template_decl;
508 current_template_parms = TREE_CHAIN (current_template_parms);
509 poplevel (0, 0, 0);
513 /* Return a new template argument vector which contains all of ARGS,
514 but has as its innermost set of arguments the EXTRA_ARGS. */
516 static tree
517 add_to_template_args (tree args, tree extra_args)
519 tree new_args;
520 int extra_depth;
521 int i;
522 int j;
524 if (args == NULL_TREE || extra_args == error_mark_node)
525 return extra_args;
527 extra_depth = TMPL_ARGS_DEPTH (extra_args);
528 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
530 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
531 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
533 for (j = 1; j <= extra_depth; ++j, ++i)
534 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
536 return new_args;
539 /* Like add_to_template_args, but only the outermost ARGS are added to
540 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
541 (EXTRA_ARGS) levels are added. This function is used to combine
542 the template arguments from a partial instantiation with the
543 template arguments used to attain the full instantiation from the
544 partial instantiation. */
546 static tree
547 add_outermost_template_args (tree args, tree extra_args)
549 tree new_args;
551 /* If there are more levels of EXTRA_ARGS than there are ARGS,
552 something very fishy is going on. */
553 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
555 /* If *all* the new arguments will be the EXTRA_ARGS, just return
556 them. */
557 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
558 return extra_args;
560 /* For the moment, we make ARGS look like it contains fewer levels. */
561 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
563 new_args = add_to_template_args (args, extra_args);
565 /* Now, we restore ARGS to its full dimensions. */
566 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
568 return new_args;
571 /* Return the N levels of innermost template arguments from the ARGS. */
573 tree
574 get_innermost_template_args (tree args, int n)
576 tree new_args;
577 int extra_levels;
578 int i;
580 gcc_assert (n >= 0);
582 /* If N is 1, just return the innermost set of template arguments. */
583 if (n == 1)
584 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
586 /* If we're not removing anything, just return the arguments we were
587 given. */
588 extra_levels = TMPL_ARGS_DEPTH (args) - n;
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
593 /* Make a new set of arguments, not containing the outer arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i + extra_levels));
599 return new_args;
602 /* The inverse of get_innermost_template_args: Return all but the innermost
603 EXTRA_LEVELS levels of template arguments from the ARGS. */
605 static tree
606 strip_innermost_template_args (tree args, int extra_levels)
608 tree new_args;
609 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
610 int i;
612 gcc_assert (n >= 0);
614 /* If N is 1, just return the outermost set of template arguments. */
615 if (n == 1)
616 return TMPL_ARGS_LEVEL (args, 1);
618 /* If we're not removing anything, just return the arguments we were
619 given. */
620 gcc_assert (extra_levels >= 0);
621 if (extra_levels == 0)
622 return args;
624 /* Make a new set of arguments, not containing the inner arguments. */
625 new_args = make_tree_vec (n);
626 for (i = 1; i <= n; ++i)
627 SET_TMPL_ARGS_LEVEL (new_args, i,
628 TMPL_ARGS_LEVEL (args, i));
630 return new_args;
633 /* We've got a template header coming up; push to a new level for storing
634 the parms. */
636 void
637 begin_template_parm_list (void)
639 /* We use a non-tag-transparent scope here, which causes pushtag to
640 put tags in this scope, rather than in the enclosing class or
641 namespace scope. This is the right thing, since we want
642 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
643 global template class, push_template_decl handles putting the
644 TEMPLATE_DECL into top-level scope. For a nested template class,
645 e.g.:
647 template <class T> struct S1 {
648 template <class T> struct S2 {};
651 pushtag contains special code to call pushdecl_with_scope on the
652 TEMPLATE_DECL for S2. */
653 begin_scope (sk_template_parms, NULL);
654 ++processing_template_decl;
655 ++processing_template_parmlist;
656 note_template_header (0);
658 /* Add a dummy parameter level while we process the parameter list. */
659 current_template_parms
660 = tree_cons (size_int (processing_template_decl),
661 make_tree_vec (0),
662 current_template_parms);
665 /* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
669 static bool
670 check_specialization_scope (void)
672 tree scope = current_scope ();
674 /* [temp.expl.spec]
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
682 is a member. */
683 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
685 error ("explicit specialization in non-namespace scope %qD", scope);
686 return false;
689 /* [temp.expl.spec]
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms)
699 error ("enclosing class templates are not explicitly specialized");
700 return false;
703 return true;
706 /* We've just seen template <>. */
708 bool
709 begin_specialization (void)
711 begin_scope (sk_template_spec, NULL);
712 note_template_header (1);
713 return check_specialization_scope ();
716 /* Called at then end of processing a declaration preceded by
717 template<>. */
719 void
720 end_specialization (void)
722 finish_scope ();
723 reset_specialization ();
726 /* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
729 void
730 reset_specialization (void)
732 processing_specialization = 0;
733 template_header_count = 0;
736 /* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
739 static void
740 note_template_header (int specialization)
742 processing_specialization = specialization;
743 template_header_count++;
746 /* We're beginning an explicit instantiation. */
748 void
749 begin_explicit_instantiation (void)
751 gcc_assert (!processing_explicit_instantiation);
752 processing_explicit_instantiation = true;
756 void
757 end_explicit_instantiation (void)
759 gcc_assert (processing_explicit_instantiation);
760 processing_explicit_instantiation = false;
763 /* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
768 static bool
769 check_specialization_namespace (tree tmpl)
771 tree tpl_ns = decl_namespace_context (tmpl);
773 /* [tmpl.expl.spec]
775 An explicit specialization shall be declared in a namespace enclosing the
776 specialized template. An explicit specialization whose declarator-id is
777 not qualified shall be declared in the nearest enclosing namespace of the
778 template, or, if the namespace is inline (7.3.1), any namespace from its
779 enclosing namespace set. */
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;
787 if (cxx_dialect < cxx11
788 ? is_associated_namespace (current_namespace, tpl_ns)
789 : is_ancestor (current_namespace, tpl_ns))
790 /* Same or enclosing namespace. */
791 return true;
792 else
794 permerror (input_location,
795 "specialization of %qD in different namespace", tmpl);
796 inform (DECL_SOURCE_LOCATION (tmpl),
797 " from definition of %q#D", tmpl);
798 return false;
802 /* SPEC is an explicit instantiation. Check that it is valid to
803 perform this explicit instantiation in the current namespace. */
805 static void
806 check_explicit_instantiation_namespace (tree spec)
808 tree ns;
810 /* DR 275: An explicit instantiation shall appear in an enclosing
811 namespace of its template. */
812 ns = decl_namespace_context (spec);
813 if (!is_ancestor (current_namespace, ns))
814 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
815 "(which does not enclose namespace %qD)",
816 spec, current_namespace, ns);
819 // Returns the type of a template specialization only if that
820 // specialization needs to be defined. Otherwise (e.g., if the type has
821 // already been defined), the function returns NULL_TREE.
822 static tree
823 maybe_new_partial_specialization (tree type)
825 // An implicit instantiation of an incomplete type implies
826 // the definition of a new class template.
828 // template<typename T>
829 // struct S;
831 // template<typename T>
832 // struct S<T*>;
834 // Here, S<T*> is an implicit instantiation of S whose type
835 // is incomplete.
836 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
837 return type;
839 // It can also be the case that TYPE is a completed specialization.
840 // Continuing the previous example, suppose we also declare:
842 // template<typename T>
843 // requires Integral<T>
844 // struct S<T*>;
846 // Here, S<T*> refers to the specialization S<T*> defined
847 // above. However, we need to differentiate definitions because
848 // we intend to define a new partial specialization. In this case,
849 // we rely on the fact that the constraints are different for
850 // this declaration than that above.
852 // Note that we also get here for injected class names and
853 // late-parsed template definitions. We must ensure that we
854 // do not create new type declarations for those cases.
855 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
857 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
858 tree args = CLASSTYPE_TI_ARGS (type);
860 // If there are no template parameters, this cannot be a new
861 // partial template specializtion?
862 if (!current_template_parms)
863 return NULL_TREE;
865 // The injected-class-name is not a new partial specialization.
866 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
867 return NULL_TREE;
869 // If the constraints are not the same as those of the primary
870 // then, we can probably create a new specialization.
871 tree type_constr = current_template_constraints ();
873 if (type == TREE_TYPE (tmpl))
875 tree main_constr = get_constraints (tmpl);
876 if (equivalent_constraints (type_constr, main_constr))
877 return NULL_TREE;
880 // Also, if there's a pre-existing specialization with matching
881 // constraints, then this also isn't new.
882 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
883 while (specs)
885 tree spec_tmpl = TREE_VALUE (specs);
886 tree spec_args = TREE_PURPOSE (specs);
887 tree spec_constr = get_constraints (spec_tmpl);
888 if (comp_template_args (args, spec_args)
889 && equivalent_constraints (type_constr, spec_constr))
890 return NULL_TREE;
891 specs = TREE_CHAIN (specs);
894 // Create a new type node (and corresponding type decl)
895 // for the newly declared specialization.
896 tree t = make_class_type (TREE_CODE (type));
897 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
898 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
900 /* We only need a separate type node for storing the definition of this
901 partial specialization; uses of S<T*> are unconstrained, so all are
902 equivalent. So keep TYPE_CANONICAL the same. */
903 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
905 // Build the corresponding type decl.
906 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
907 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
908 DECL_SOURCE_LOCATION (d) = input_location;
910 return t;
913 return NULL_TREE;
916 /* The TYPE is being declared. If it is a template type, that means it
917 is a partial specialization. Do appropriate error-checking. */
919 tree
920 maybe_process_partial_specialization (tree type)
922 tree context;
924 if (type == error_mark_node)
925 return error_mark_node;
927 /* A lambda that appears in specialization context is not itself a
928 specialization. */
929 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
930 return type;
932 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
934 error ("name of class shadows template template parameter %qD",
935 TYPE_NAME (type));
936 return error_mark_node;
939 context = TYPE_CONTEXT (type);
941 if (TYPE_ALIAS_P (type))
943 if (TYPE_TEMPLATE_INFO (type)
944 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
945 error ("specialization of alias template %qD",
946 TYPE_TI_TEMPLATE (type));
947 else
948 error ("explicit specialization of non-template %qT", type);
949 return error_mark_node;
951 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
953 /* This is for ordinary explicit specialization and partial
954 specialization of a template class such as:
956 template <> class C<int>;
960 template <class T> class C<T*>;
962 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
964 if (tree t = maybe_new_partial_specialization (type))
966 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
967 && !at_namespace_scope_p ())
968 return error_mark_node;
969 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
970 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
971 if (processing_template_decl)
973 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
974 if (decl == error_mark_node)
975 return error_mark_node;
976 return TREE_TYPE (decl);
979 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
980 error ("specialization of %qT after instantiation", type);
981 else if (errorcount && !processing_specialization
982 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
983 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
984 /* Trying to define a specialization either without a template<> header
985 or in an inappropriate place. We've already given an error, so just
986 bail now so we don't actually define the specialization. */
987 return error_mark_node;
989 else if (CLASS_TYPE_P (type)
990 && !CLASSTYPE_USE_TEMPLATE (type)
991 && CLASSTYPE_TEMPLATE_INFO (type)
992 && context && CLASS_TYPE_P (context)
993 && CLASSTYPE_TEMPLATE_INFO (context))
995 /* This is for an explicit specialization of member class
996 template according to [temp.expl.spec/18]:
998 template <> template <class U> class C<int>::D;
1000 The context `C<int>' must be an implicit instantiation.
1001 Otherwise this is just a member class template declared
1002 earlier like:
1004 template <> class C<int> { template <class U> class D; };
1005 template <> template <class U> class C<int>::D;
1007 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1008 while in the second case, `C<int>::D' is a primary template
1009 and `C<T>::D' may not exist. */
1011 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1012 && !COMPLETE_TYPE_P (type))
1014 tree t;
1015 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1017 if (current_namespace
1018 != decl_namespace_context (tmpl))
1020 permerror (input_location,
1021 "specializing %q#T in different namespace", type);
1022 permerror (DECL_SOURCE_LOCATION (tmpl),
1023 " from definition of %q#D", tmpl);
1026 /* Check for invalid specialization after instantiation:
1028 template <> template <> class C<int>::D<int>;
1029 template <> template <class U> class C<int>::D; */
1031 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1032 t; t = TREE_CHAIN (t))
1034 tree inst = TREE_VALUE (t);
1035 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1036 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1038 /* We already have a full specialization of this partial
1039 instantiation, or a full specialization has been
1040 looked up but not instantiated. Reassign it to the
1041 new member specialization template. */
1042 spec_entry elt;
1043 spec_entry *entry;
1045 elt.tmpl = most_general_template (tmpl);
1046 elt.args = CLASSTYPE_TI_ARGS (inst);
1047 elt.spec = inst;
1049 type_specializations->remove_elt (&elt);
1051 elt.tmpl = tmpl;
1052 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1054 spec_entry **slot
1055 = type_specializations->find_slot (&elt, INSERT);
1056 entry = ggc_alloc<spec_entry> ();
1057 *entry = elt;
1058 *slot = entry;
1060 else
1061 /* But if we've had an implicit instantiation, that's a
1062 problem ([temp.expl.spec]/6). */
1063 error ("specialization %qT after instantiation %qT",
1064 type, inst);
1067 /* Mark TYPE as a specialization. And as a result, we only
1068 have one level of template argument for the innermost
1069 class template. */
1070 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1071 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1072 CLASSTYPE_TI_ARGS (type)
1073 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1076 else if (processing_specialization)
1078 /* Someday C++0x may allow for enum template specialization. */
1079 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1080 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1081 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1082 "of %qD not allowed by ISO C++", type);
1083 else
1085 error ("explicit specialization of non-template %qT", type);
1086 return error_mark_node;
1090 return type;
1093 /* Returns nonzero if we can optimize the retrieval of specializations
1094 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1095 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1097 static inline bool
1098 optimize_specialization_lookup_p (tree tmpl)
1100 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1101 && DECL_CLASS_SCOPE_P (tmpl)
1102 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1103 parameter. */
1104 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1105 /* The optimized lookup depends on the fact that the
1106 template arguments for the member function template apply
1107 purely to the containing class, which is not true if the
1108 containing class is an explicit or partial
1109 specialization. */
1110 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1111 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1112 && !DECL_CONV_FN_P (tmpl)
1113 /* It is possible to have a template that is not a member
1114 template and is not a member of a template class:
1116 template <typename T>
1117 struct S { friend A::f(); };
1119 Here, the friend function is a template, but the context does
1120 not have template information. The optimized lookup relies
1121 on having ARGS be the template arguments for both the class
1122 and the function template. */
1123 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1126 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1127 gone through coerce_template_parms by now. */
1129 static void
1130 verify_unstripped_args (tree args)
1132 ++processing_template_decl;
1133 if (!any_dependent_template_arguments_p (args))
1135 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1136 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1138 tree arg = TREE_VEC_ELT (inner, i);
1139 if (TREE_CODE (arg) == TEMPLATE_DECL)
1140 /* OK */;
1141 else if (TYPE_P (arg))
1142 gcc_assert (strip_typedefs (arg, NULL) == arg);
1143 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1144 /* Allow typedefs on the type of a non-type argument, since a
1145 parameter can have them. */;
1146 else
1147 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1150 --processing_template_decl;
1153 /* Retrieve the specialization (in the sense of [temp.spec] - a
1154 specialization is either an instantiation or an explicit
1155 specialization) of TMPL for the given template ARGS. If there is
1156 no such specialization, return NULL_TREE. The ARGS are a vector of
1157 arguments, or a vector of vectors of arguments, in the case of
1158 templates with more than one level of parameters.
1160 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1161 then we search for a partial specialization matching ARGS. This
1162 parameter is ignored if TMPL is not a class template.
1164 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1165 result is a NONTYPE_ARGUMENT_PACK. */
1167 static tree
1168 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1170 if (tmpl == NULL_TREE)
1171 return NULL_TREE;
1173 if (args == error_mark_node)
1174 return NULL_TREE;
1176 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1177 || TREE_CODE (tmpl) == FIELD_DECL);
1179 /* There should be as many levels of arguments as there are
1180 levels of parameters. */
1181 gcc_assert (TMPL_ARGS_DEPTH (args)
1182 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1183 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1184 : template_class_depth (DECL_CONTEXT (tmpl))));
1186 if (flag_checking)
1187 verify_unstripped_args (args);
1189 if (optimize_specialization_lookup_p (tmpl))
1191 tree class_template;
1192 tree class_specialization;
1193 vec<tree, va_gc> *methods;
1194 tree fns;
1195 int idx;
1197 /* The template arguments actually apply to the containing
1198 class. Find the class specialization with those
1199 arguments. */
1200 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1201 class_specialization
1202 = retrieve_specialization (class_template, args, 0);
1203 if (!class_specialization)
1204 return NULL_TREE;
1205 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1206 for the specialization. */
1207 idx = class_method_index_for_fn (class_specialization, tmpl);
1208 if (idx == -1)
1209 return NULL_TREE;
1210 /* Iterate through the methods with the indicated name, looking
1211 for the one that has an instance of TMPL. */
1212 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1213 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1215 tree fn = OVL_CURRENT (fns);
1216 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1217 /* using-declarations can add base methods to the method vec,
1218 and we don't want those here. */
1219 && DECL_CONTEXT (fn) == class_specialization)
1220 return fn;
1222 return NULL_TREE;
1224 else
1226 spec_entry *found;
1227 spec_entry elt;
1228 hash_table<spec_hasher> *specializations;
1230 elt.tmpl = tmpl;
1231 elt.args = args;
1232 elt.spec = NULL_TREE;
1234 if (DECL_CLASS_TEMPLATE_P (tmpl))
1235 specializations = type_specializations;
1236 else
1237 specializations = decl_specializations;
1239 if (hash == 0)
1240 hash = spec_hasher::hash (&elt);
1241 found = specializations->find_with_hash (&elt, hash);
1242 if (found)
1243 return found->spec;
1246 return NULL_TREE;
1249 /* Like retrieve_specialization, but for local declarations. */
1251 tree
1252 retrieve_local_specialization (tree tmpl)
1254 if (local_specializations == NULL)
1255 return NULL_TREE;
1257 tree *slot = local_specializations->get (tmpl);
1258 return slot ? *slot : NULL_TREE;
1261 /* Returns nonzero iff DECL is a specialization of TMPL. */
1264 is_specialization_of (tree decl, tree tmpl)
1266 tree t;
1268 if (TREE_CODE (decl) == FUNCTION_DECL)
1270 for (t = decl;
1271 t != NULL_TREE;
1272 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1273 if (t == tmpl)
1274 return 1;
1276 else
1278 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1280 for (t = TREE_TYPE (decl);
1281 t != NULL_TREE;
1282 t = CLASSTYPE_USE_TEMPLATE (t)
1283 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1284 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1285 return 1;
1288 return 0;
1291 /* Returns nonzero iff DECL is a specialization of friend declaration
1292 FRIEND_DECL according to [temp.friend]. */
1294 bool
1295 is_specialization_of_friend (tree decl, tree friend_decl)
1297 bool need_template = true;
1298 int template_depth;
1300 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1301 || TREE_CODE (decl) == TYPE_DECL);
1303 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1304 of a template class, we want to check if DECL is a specialization
1305 if this. */
1306 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1307 && DECL_TEMPLATE_INFO (friend_decl)
1308 && !DECL_USE_TEMPLATE (friend_decl))
1310 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1311 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1312 need_template = false;
1314 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1315 && !PRIMARY_TEMPLATE_P (friend_decl))
1316 need_template = false;
1318 /* There is nothing to do if this is not a template friend. */
1319 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1320 return false;
1322 if (is_specialization_of (decl, friend_decl))
1323 return true;
1325 /* [temp.friend/6]
1326 A member of a class template may be declared to be a friend of a
1327 non-template class. In this case, the corresponding member of
1328 every specialization of the class template is a friend of the
1329 class granting friendship.
1331 For example, given a template friend declaration
1333 template <class T> friend void A<T>::f();
1335 the member function below is considered a friend
1337 template <> struct A<int> {
1338 void f();
1341 For this type of template friend, TEMPLATE_DEPTH below will be
1342 nonzero. To determine if DECL is a friend of FRIEND, we first
1343 check if the enclosing class is a specialization of another. */
1345 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1346 if (template_depth
1347 && DECL_CLASS_SCOPE_P (decl)
1348 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1349 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1351 /* Next, we check the members themselves. In order to handle
1352 a few tricky cases, such as when FRIEND_DECL's are
1354 template <class T> friend void A<T>::g(T t);
1355 template <class T> template <T t> friend void A<T>::h();
1357 and DECL's are
1359 void A<int>::g(int);
1360 template <int> void A<int>::h();
1362 we need to figure out ARGS, the template arguments from
1363 the context of DECL. This is required for template substitution
1364 of `T' in the function parameter of `g' and template parameter
1365 of `h' in the above examples. Here ARGS corresponds to `int'. */
1367 tree context = DECL_CONTEXT (decl);
1368 tree args = NULL_TREE;
1369 int current_depth = 0;
1371 while (current_depth < template_depth)
1373 if (CLASSTYPE_TEMPLATE_INFO (context))
1375 if (current_depth == 0)
1376 args = TYPE_TI_ARGS (context);
1377 else
1378 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1379 current_depth++;
1381 context = TYPE_CONTEXT (context);
1384 if (TREE_CODE (decl) == FUNCTION_DECL)
1386 bool is_template;
1387 tree friend_type;
1388 tree decl_type;
1389 tree friend_args_type;
1390 tree decl_args_type;
1392 /* Make sure that both DECL and FRIEND_DECL are templates or
1393 non-templates. */
1394 is_template = DECL_TEMPLATE_INFO (decl)
1395 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1396 if (need_template ^ is_template)
1397 return false;
1398 else if (is_template)
1400 /* If both are templates, check template parameter list. */
1401 tree friend_parms
1402 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1403 args, tf_none);
1404 if (!comp_template_parms
1405 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1406 friend_parms))
1407 return false;
1409 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1411 else
1412 decl_type = TREE_TYPE (decl);
1414 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1415 tf_none, NULL_TREE);
1416 if (friend_type == error_mark_node)
1417 return false;
1419 /* Check if return types match. */
1420 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1421 return false;
1423 /* Check if function parameter types match, ignoring the
1424 `this' parameter. */
1425 friend_args_type = TYPE_ARG_TYPES (friend_type);
1426 decl_args_type = TYPE_ARG_TYPES (decl_type);
1427 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1428 friend_args_type = TREE_CHAIN (friend_args_type);
1429 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1430 decl_args_type = TREE_CHAIN (decl_args_type);
1432 return compparms (decl_args_type, friend_args_type);
1434 else
1436 /* DECL is a TYPE_DECL */
1437 bool is_template;
1438 tree decl_type = TREE_TYPE (decl);
1440 /* Make sure that both DECL and FRIEND_DECL are templates or
1441 non-templates. */
1442 is_template
1443 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1444 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1446 if (need_template ^ is_template)
1447 return false;
1448 else if (is_template)
1450 tree friend_parms;
1451 /* If both are templates, check the name of the two
1452 TEMPLATE_DECL's first because is_friend didn't. */
1453 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1454 != DECL_NAME (friend_decl))
1455 return false;
1457 /* Now check template parameter list. */
1458 friend_parms
1459 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1460 args, tf_none);
1461 return comp_template_parms
1462 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1463 friend_parms);
1465 else
1466 return (DECL_NAME (decl)
1467 == DECL_NAME (friend_decl));
1470 return false;
1473 /* Register the specialization SPEC as a specialization of TMPL with
1474 the indicated ARGS. IS_FRIEND indicates whether the specialization
1475 is actually just a friend declaration. Returns SPEC, or an
1476 equivalent prior declaration, if available.
1478 We also store instantiations of field packs in the hash table, even
1479 though they are not themselves templates, to make lookup easier. */
1481 static tree
1482 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1483 hashval_t hash)
1485 tree fn;
1486 spec_entry **slot = NULL;
1487 spec_entry elt;
1489 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1490 || (TREE_CODE (tmpl) == FIELD_DECL
1491 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1493 if (TREE_CODE (spec) == FUNCTION_DECL
1494 && uses_template_parms (DECL_TI_ARGS (spec)))
1495 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1496 register it; we want the corresponding TEMPLATE_DECL instead.
1497 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1498 the more obvious `uses_template_parms (spec)' to avoid problems
1499 with default function arguments. In particular, given
1500 something like this:
1502 template <class T> void f(T t1, T t = T())
1504 the default argument expression is not substituted for in an
1505 instantiation unless and until it is actually needed. */
1506 return spec;
1508 if (optimize_specialization_lookup_p (tmpl))
1509 /* We don't put these specializations in the hash table, but we might
1510 want to give an error about a mismatch. */
1511 fn = retrieve_specialization (tmpl, args, 0);
1512 else
1514 elt.tmpl = tmpl;
1515 elt.args = args;
1516 elt.spec = spec;
1518 if (hash == 0)
1519 hash = spec_hasher::hash (&elt);
1521 slot =
1522 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1523 if (*slot)
1524 fn = ((spec_entry *) *slot)->spec;
1525 else
1526 fn = NULL_TREE;
1529 /* We can sometimes try to re-register a specialization that we've
1530 already got. In particular, regenerate_decl_from_template calls
1531 duplicate_decls which will update the specialization list. But,
1532 we'll still get called again here anyhow. It's more convenient
1533 to simply allow this than to try to prevent it. */
1534 if (fn == spec)
1535 return spec;
1536 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1538 if (DECL_TEMPLATE_INSTANTIATION (fn))
1540 if (DECL_ODR_USED (fn)
1541 || DECL_EXPLICIT_INSTANTIATION (fn))
1543 error ("specialization of %qD after instantiation",
1544 fn);
1545 return error_mark_node;
1547 else
1549 tree clone;
1550 /* This situation should occur only if the first
1551 specialization is an implicit instantiation, the
1552 second is an explicit specialization, and the
1553 implicit instantiation has not yet been used. That
1554 situation can occur if we have implicitly
1555 instantiated a member function and then specialized
1556 it later.
1558 We can also wind up here if a friend declaration that
1559 looked like an instantiation turns out to be a
1560 specialization:
1562 template <class T> void foo(T);
1563 class S { friend void foo<>(int) };
1564 template <> void foo(int);
1566 We transform the existing DECL in place so that any
1567 pointers to it become pointers to the updated
1568 declaration.
1570 If there was a definition for the template, but not
1571 for the specialization, we want this to look as if
1572 there were no definition, and vice versa. */
1573 DECL_INITIAL (fn) = NULL_TREE;
1574 duplicate_decls (spec, fn, is_friend);
1575 /* The call to duplicate_decls will have applied
1576 [temp.expl.spec]:
1578 An explicit specialization of a function template
1579 is inline only if it is explicitly declared to be,
1580 and independently of whether its function template
1583 to the primary function; now copy the inline bits to
1584 the various clones. */
1585 FOR_EACH_CLONE (clone, fn)
1587 DECL_DECLARED_INLINE_P (clone)
1588 = DECL_DECLARED_INLINE_P (fn);
1589 DECL_SOURCE_LOCATION (clone)
1590 = DECL_SOURCE_LOCATION (fn);
1591 DECL_DELETED_FN (clone)
1592 = DECL_DELETED_FN (fn);
1594 check_specialization_namespace (tmpl);
1596 return fn;
1599 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1601 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1602 /* Dup decl failed, but this is a new definition. Set the
1603 line number so any errors match this new
1604 definition. */
1605 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1607 return fn;
1610 else if (fn)
1611 return duplicate_decls (spec, fn, is_friend);
1613 /* A specialization must be declared in the same namespace as the
1614 template it is specializing. */
1615 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1616 && !check_specialization_namespace (tmpl))
1617 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1619 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1621 spec_entry *entry = ggc_alloc<spec_entry> ();
1622 gcc_assert (tmpl && args && spec);
1623 *entry = elt;
1624 *slot = entry;
1625 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1626 && PRIMARY_TEMPLATE_P (tmpl)
1627 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1628 || variable_template_p (tmpl))
1629 /* If TMPL is a forward declaration of a template function, keep a list
1630 of all specializations in case we need to reassign them to a friend
1631 template later in tsubst_friend_function.
1633 Also keep a list of all variable template instantiations so that
1634 process_partial_specialization can check whether a later partial
1635 specialization would have used it. */
1636 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1637 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1640 return spec;
1643 /* Returns true iff two spec_entry nodes are equivalent. */
1645 int comparing_specializations;
1647 bool
1648 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1650 int equal;
1652 ++comparing_specializations;
1653 equal = (e1->tmpl == e2->tmpl
1654 && comp_template_args (e1->args, e2->args));
1655 if (equal && flag_concepts
1656 /* tmpl could be a FIELD_DECL for a capture pack. */
1657 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1658 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1659 && uses_template_parms (e1->args))
1661 /* Partial specializations of a variable template can be distinguished by
1662 constraints. */
1663 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1664 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1665 equal = equivalent_constraints (c1, c2);
1667 --comparing_specializations;
1669 return equal;
1672 /* Returns a hash for a template TMPL and template arguments ARGS. */
1674 static hashval_t
1675 hash_tmpl_and_args (tree tmpl, tree args)
1677 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1678 return iterative_hash_template_arg (args, val);
1681 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1682 ignoring SPEC. */
1684 hashval_t
1685 spec_hasher::hash (spec_entry *e)
1687 return hash_tmpl_and_args (e->tmpl, e->args);
1690 /* Recursively calculate a hash value for a template argument ARG, for use
1691 in the hash tables of template specializations. */
1693 hashval_t
1694 iterative_hash_template_arg (tree arg, hashval_t val)
1696 unsigned HOST_WIDE_INT i;
1697 enum tree_code code;
1698 char tclass;
1700 if (arg == NULL_TREE)
1701 return iterative_hash_object (arg, val);
1703 if (!TYPE_P (arg))
1704 STRIP_NOPS (arg);
1706 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1707 gcc_unreachable ();
1709 code = TREE_CODE (arg);
1710 tclass = TREE_CODE_CLASS (code);
1712 val = iterative_hash_object (code, val);
1714 switch (code)
1716 case ERROR_MARK:
1717 return val;
1719 case IDENTIFIER_NODE:
1720 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1722 case TREE_VEC:
1724 int i, len = TREE_VEC_LENGTH (arg);
1725 for (i = 0; i < len; ++i)
1726 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1727 return val;
1730 case TYPE_PACK_EXPANSION:
1731 case EXPR_PACK_EXPANSION:
1732 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1733 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1735 case TYPE_ARGUMENT_PACK:
1736 case NONTYPE_ARGUMENT_PACK:
1737 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1739 case TREE_LIST:
1740 for (; arg; arg = TREE_CHAIN (arg))
1741 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1742 return val;
1744 case OVERLOAD:
1745 for (; arg; arg = OVL_NEXT (arg))
1746 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1747 return val;
1749 case CONSTRUCTOR:
1751 tree field, value;
1752 iterative_hash_template_arg (TREE_TYPE (arg), val);
1753 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1755 val = iterative_hash_template_arg (field, val);
1756 val = iterative_hash_template_arg (value, val);
1758 return val;
1761 case PARM_DECL:
1762 if (!DECL_ARTIFICIAL (arg))
1764 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1765 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1767 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1769 case TARGET_EXPR:
1770 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1772 case PTRMEM_CST:
1773 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1774 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1776 case TEMPLATE_PARM_INDEX:
1777 val = iterative_hash_template_arg
1778 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1779 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1780 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1782 case TRAIT_EXPR:
1783 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1784 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1785 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1787 case BASELINK:
1788 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1789 val);
1790 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1791 val);
1793 case MODOP_EXPR:
1794 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1795 code = TREE_CODE (TREE_OPERAND (arg, 1));
1796 val = iterative_hash_object (code, val);
1797 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1799 case LAMBDA_EXPR:
1800 /* A lambda can't appear in a template arg, but don't crash on
1801 erroneous input. */
1802 gcc_assert (seen_error ());
1803 return val;
1805 case CAST_EXPR:
1806 case IMPLICIT_CONV_EXPR:
1807 case STATIC_CAST_EXPR:
1808 case REINTERPRET_CAST_EXPR:
1809 case CONST_CAST_EXPR:
1810 case DYNAMIC_CAST_EXPR:
1811 case NEW_EXPR:
1812 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1813 /* Now hash operands as usual. */
1814 break;
1816 default:
1817 break;
1820 switch (tclass)
1822 case tcc_type:
1823 if (alias_template_specialization_p (arg))
1825 // We want an alias specialization that survived strip_typedefs
1826 // to hash differently from its TYPE_CANONICAL, to avoid hash
1827 // collisions that compare as different in template_args_equal.
1828 // These could be dependent specializations that strip_typedefs
1829 // left alone, or untouched specializations because
1830 // coerce_template_parms returns the unconverted template
1831 // arguments if it sees incomplete argument packs.
1832 tree ti = TYPE_TEMPLATE_INFO (arg);
1833 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1835 if (TYPE_CANONICAL (arg))
1836 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1837 val);
1838 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1839 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1840 /* Otherwise just compare the types during lookup. */
1841 return val;
1843 case tcc_declaration:
1844 case tcc_constant:
1845 return iterative_hash_expr (arg, val);
1847 default:
1848 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1850 unsigned n = cp_tree_operand_length (arg);
1851 for (i = 0; i < n; ++i)
1852 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1853 return val;
1856 gcc_unreachable ();
1857 return 0;
1860 /* Unregister the specialization SPEC as a specialization of TMPL.
1861 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1862 if the SPEC was listed as a specialization of TMPL.
1864 Note that SPEC has been ggc_freed, so we can't look inside it. */
1866 bool
1867 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1869 spec_entry *entry;
1870 spec_entry elt;
1872 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1873 elt.args = TI_ARGS (tinfo);
1874 elt.spec = NULL_TREE;
1876 entry = decl_specializations->find (&elt);
1877 if (entry != NULL)
1879 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1880 gcc_assert (new_spec != NULL_TREE);
1881 entry->spec = new_spec;
1882 return 1;
1885 return 0;
1888 /* Like register_specialization, but for local declarations. We are
1889 registering SPEC, an instantiation of TMPL. */
1891 void
1892 register_local_specialization (tree spec, tree tmpl)
1894 local_specializations->put (tmpl, spec);
1897 /* TYPE is a class type. Returns true if TYPE is an explicitly
1898 specialized class. */
1900 bool
1901 explicit_class_specialization_p (tree type)
1903 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1904 return false;
1905 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1908 /* Print the list of functions at FNS, going through all the overloads
1909 for each element of the list. Alternatively, FNS can not be a
1910 TREE_LIST, in which case it will be printed together with all the
1911 overloads.
1913 MORE and *STR should respectively be FALSE and NULL when the function
1914 is called from the outside. They are used internally on recursive
1915 calls. print_candidates manages the two parameters and leaves NULL
1916 in *STR when it ends. */
1918 static void
1919 print_candidates_1 (tree fns, bool more, const char **str)
1921 tree fn, fn2;
1922 char *spaces = NULL;
1924 for (fn = fns; fn; fn = OVL_NEXT (fn))
1925 if (TREE_CODE (fn) == TREE_LIST)
1927 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1928 print_candidates_1 (TREE_VALUE (fn2),
1929 TREE_CHAIN (fn2) || more, str);
1931 else
1933 tree cand = OVL_CURRENT (fn);
1934 if (!*str)
1936 /* Pick the prefix string. */
1937 if (!more && !OVL_NEXT (fns))
1939 inform (DECL_SOURCE_LOCATION (cand),
1940 "candidate is: %#D", cand);
1941 continue;
1944 *str = _("candidates are:");
1945 spaces = get_spaces (*str);
1947 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1948 *str = spaces ? spaces : *str;
1951 if (!more)
1953 free (spaces);
1954 *str = NULL;
1958 /* Print the list of candidate FNS in an error message. FNS can also
1959 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1961 void
1962 print_candidates (tree fns)
1964 const char *str = NULL;
1965 print_candidates_1 (fns, false, &str);
1966 gcc_assert (str == NULL);
1969 /* Get a (possibly) constrained template declaration for the
1970 purpose of ordering candidates. */
1971 static tree
1972 get_template_for_ordering (tree list)
1974 gcc_assert (TREE_CODE (list) == TREE_LIST);
1975 tree f = TREE_VALUE (list);
1976 if (tree ti = DECL_TEMPLATE_INFO (f))
1977 return TI_TEMPLATE (ti);
1978 return f;
1981 /* Among candidates having the same signature, return the
1982 most constrained or NULL_TREE if there is no best candidate.
1983 If the signatures of candidates vary (e.g., template
1984 specialization vs. member function), then there can be no
1985 most constrained.
1987 Note that we don't compare constraints on the functions
1988 themselves, but rather those of their templates. */
1989 static tree
1990 most_constrained_function (tree candidates)
1992 // Try to find the best candidate in a first pass.
1993 tree champ = candidates;
1994 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1996 int winner = more_constrained (get_template_for_ordering (champ),
1997 get_template_for_ordering (c));
1998 if (winner == -1)
1999 champ = c; // The candidate is more constrained
2000 else if (winner == 0)
2001 return NULL_TREE; // Neither is more constrained
2004 // Verify that the champ is better than previous candidates.
2005 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2006 if (!more_constrained (get_template_for_ordering (champ),
2007 get_template_for_ordering (c)))
2008 return NULL_TREE;
2011 return champ;
2015 /* Returns the template (one of the functions given by TEMPLATE_ID)
2016 which can be specialized to match the indicated DECL with the
2017 explicit template args given in TEMPLATE_ID. The DECL may be
2018 NULL_TREE if none is available. In that case, the functions in
2019 TEMPLATE_ID are non-members.
2021 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2022 specialization of a member template.
2024 The TEMPLATE_COUNT is the number of references to qualifying
2025 template classes that appeared in the name of the function. See
2026 check_explicit_specialization for a more accurate description.
2028 TSK indicates what kind of template declaration (if any) is being
2029 declared. TSK_TEMPLATE indicates that the declaration given by
2030 DECL, though a FUNCTION_DECL, has template parameters, and is
2031 therefore a template function.
2033 The template args (those explicitly specified and those deduced)
2034 are output in a newly created vector *TARGS_OUT.
2036 If it is impossible to determine the result, an error message is
2037 issued. The error_mark_node is returned to indicate failure. */
2039 static tree
2040 determine_specialization (tree template_id,
2041 tree decl,
2042 tree* targs_out,
2043 int need_member_template,
2044 int template_count,
2045 tmpl_spec_kind tsk)
2047 tree fns;
2048 tree targs;
2049 tree explicit_targs;
2050 tree candidates = NULL_TREE;
2052 /* A TREE_LIST of templates of which DECL may be a specialization.
2053 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2054 corresponding TREE_PURPOSE is the set of template arguments that,
2055 when used to instantiate the template, would produce a function
2056 with the signature of DECL. */
2057 tree templates = NULL_TREE;
2058 int header_count;
2059 cp_binding_level *b;
2061 *targs_out = NULL_TREE;
2063 if (template_id == error_mark_node || decl == error_mark_node)
2064 return error_mark_node;
2066 /* We shouldn't be specializing a member template of an
2067 unspecialized class template; we already gave an error in
2068 check_specialization_scope, now avoid crashing. */
2069 if (template_count && DECL_CLASS_SCOPE_P (decl)
2070 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2072 gcc_assert (errorcount);
2073 return error_mark_node;
2076 fns = TREE_OPERAND (template_id, 0);
2077 explicit_targs = TREE_OPERAND (template_id, 1);
2079 if (fns == error_mark_node)
2080 return error_mark_node;
2082 /* Check for baselinks. */
2083 if (BASELINK_P (fns))
2084 fns = BASELINK_FUNCTIONS (fns);
2086 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2088 error ("%qD is not a function template", fns);
2089 return error_mark_node;
2091 else if (VAR_P (decl) && !variable_template_p (fns))
2093 error ("%qD is not a variable template", fns);
2094 return error_mark_node;
2097 /* Count the number of template headers specified for this
2098 specialization. */
2099 header_count = 0;
2100 for (b = current_binding_level;
2101 b->kind == sk_template_parms;
2102 b = b->level_chain)
2103 ++header_count;
2105 tree orig_fns = fns;
2107 if (variable_template_p (fns))
2109 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2110 targs = coerce_template_parms (parms, explicit_targs, fns,
2111 tf_warning_or_error,
2112 /*req_all*/true, /*use_defarg*/true);
2113 if (targs != error_mark_node)
2114 templates = tree_cons (targs, fns, templates);
2116 else for (; fns; fns = OVL_NEXT (fns))
2118 tree fn = OVL_CURRENT (fns);
2120 if (TREE_CODE (fn) == TEMPLATE_DECL)
2122 tree decl_arg_types;
2123 tree fn_arg_types;
2124 tree insttype;
2126 /* In case of explicit specialization, we need to check if
2127 the number of template headers appearing in the specialization
2128 is correct. This is usually done in check_explicit_specialization,
2129 but the check done there cannot be exhaustive when specializing
2130 member functions. Consider the following code:
2132 template <> void A<int>::f(int);
2133 template <> template <> void A<int>::f(int);
2135 Assuming that A<int> is not itself an explicit specialization
2136 already, the first line specializes "f" which is a non-template
2137 member function, whilst the second line specializes "f" which
2138 is a template member function. So both lines are syntactically
2139 correct, and check_explicit_specialization does not reject
2140 them.
2142 Here, we can do better, as we are matching the specialization
2143 against the declarations. We count the number of template
2144 headers, and we check if they match TEMPLATE_COUNT + 1
2145 (TEMPLATE_COUNT is the number of qualifying template classes,
2146 plus there must be another header for the member template
2147 itself).
2149 Notice that if header_count is zero, this is not a
2150 specialization but rather a template instantiation, so there
2151 is no check we can perform here. */
2152 if (header_count && header_count != template_count + 1)
2153 continue;
2155 /* Check that the number of template arguments at the
2156 innermost level for DECL is the same as for FN. */
2157 if (current_binding_level->kind == sk_template_parms
2158 && !current_binding_level->explicit_spec_p
2159 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2160 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2161 (current_template_parms))))
2162 continue;
2164 /* DECL might be a specialization of FN. */
2165 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2166 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2168 /* For a non-static member function, we need to make sure
2169 that the const qualification is the same. Since
2170 get_bindings does not try to merge the "this" parameter,
2171 we must do the comparison explicitly. */
2172 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2173 && !same_type_p (TREE_VALUE (fn_arg_types),
2174 TREE_VALUE (decl_arg_types)))
2175 continue;
2177 /* Skip the "this" parameter and, for constructors of
2178 classes with virtual bases, the VTT parameter. A
2179 full specialization of a constructor will have a VTT
2180 parameter, but a template never will. */
2181 decl_arg_types
2182 = skip_artificial_parms_for (decl, decl_arg_types);
2183 fn_arg_types
2184 = skip_artificial_parms_for (fn, fn_arg_types);
2186 /* Function templates cannot be specializations; there are
2187 no partial specializations of functions. Therefore, if
2188 the type of DECL does not match FN, there is no
2189 match.
2191 Note that it should never be the case that we have both
2192 candidates added here, and for regular member functions
2193 below. */
2194 if (tsk == tsk_template)
2196 if (compparms (fn_arg_types, decl_arg_types))
2197 candidates = tree_cons (NULL_TREE, fn, candidates);
2198 continue;
2201 /* See whether this function might be a specialization of this
2202 template. Suppress access control because we might be trying
2203 to make this specialization a friend, and we have already done
2204 access control for the declaration of the specialization. */
2205 push_deferring_access_checks (dk_no_check);
2206 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2207 pop_deferring_access_checks ();
2209 if (!targs)
2210 /* We cannot deduce template arguments that when used to
2211 specialize TMPL will produce DECL. */
2212 continue;
2214 /* Remove, from the set of candidates, all those functions
2215 whose constraints are not satisfied. */
2216 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2217 continue;
2219 // Then, try to form the new function type.
2220 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2221 if (insttype == error_mark_node)
2222 continue;
2223 fn_arg_types
2224 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2225 if (!compparms (fn_arg_types, decl_arg_types))
2226 continue;
2228 /* Save this template, and the arguments deduced. */
2229 templates = tree_cons (targs, fn, templates);
2231 else if (need_member_template)
2232 /* FN is an ordinary member function, and we need a
2233 specialization of a member template. */
2235 else if (TREE_CODE (fn) != FUNCTION_DECL)
2236 /* We can get IDENTIFIER_NODEs here in certain erroneous
2237 cases. */
2239 else if (!DECL_FUNCTION_MEMBER_P (fn))
2240 /* This is just an ordinary non-member function. Nothing can
2241 be a specialization of that. */
2243 else if (DECL_ARTIFICIAL (fn))
2244 /* Cannot specialize functions that are created implicitly. */
2246 else
2248 tree decl_arg_types;
2250 /* This is an ordinary member function. However, since
2251 we're here, we can assume its enclosing class is a
2252 template class. For example,
2254 template <typename T> struct S { void f(); };
2255 template <> void S<int>::f() {}
2257 Here, S<int>::f is a non-template, but S<int> is a
2258 template class. If FN has the same type as DECL, we
2259 might be in business. */
2261 if (!DECL_TEMPLATE_INFO (fn))
2262 /* Its enclosing class is an explicit specialization
2263 of a template class. This is not a candidate. */
2264 continue;
2266 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2267 TREE_TYPE (TREE_TYPE (fn))))
2268 /* The return types differ. */
2269 continue;
2271 /* Adjust the type of DECL in case FN is a static member. */
2272 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2273 if (DECL_STATIC_FUNCTION_P (fn)
2274 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2275 decl_arg_types = TREE_CHAIN (decl_arg_types);
2277 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2278 decl_arg_types))
2279 continue;
2281 // If the deduced arguments do not satisfy the constraints,
2282 // this is not a candidate.
2283 if (flag_concepts && !constraints_satisfied_p (fn))
2284 continue;
2286 // Add the candidate.
2287 candidates = tree_cons (NULL_TREE, fn, candidates);
2291 if (templates && TREE_CHAIN (templates))
2293 /* We have:
2295 [temp.expl.spec]
2297 It is possible for a specialization with a given function
2298 signature to be instantiated from more than one function
2299 template. In such cases, explicit specification of the
2300 template arguments must be used to uniquely identify the
2301 function template specialization being specialized.
2303 Note that here, there's no suggestion that we're supposed to
2304 determine which of the candidate templates is most
2305 specialized. However, we, also have:
2307 [temp.func.order]
2309 Partial ordering of overloaded function template
2310 declarations is used in the following contexts to select
2311 the function template to which a function template
2312 specialization refers:
2314 -- when an explicit specialization refers to a function
2315 template.
2317 So, we do use the partial ordering rules, at least for now.
2318 This extension can only serve to make invalid programs valid,
2319 so it's safe. And, there is strong anecdotal evidence that
2320 the committee intended the partial ordering rules to apply;
2321 the EDG front end has that behavior, and John Spicer claims
2322 that the committee simply forgot to delete the wording in
2323 [temp.expl.spec]. */
2324 tree tmpl = most_specialized_instantiation (templates);
2325 if (tmpl != error_mark_node)
2327 templates = tmpl;
2328 TREE_CHAIN (templates) = NULL_TREE;
2332 // Concepts allows multiple declarations of member functions
2333 // with the same signature. Like above, we need to rely on
2334 // on the partial ordering of those candidates to determine which
2335 // is the best.
2336 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2338 if (tree cand = most_constrained_function (candidates))
2340 candidates = cand;
2341 TREE_CHAIN (cand) = NULL_TREE;
2345 if (templates == NULL_TREE && candidates == NULL_TREE)
2347 error ("template-id %qD for %q+D does not match any template "
2348 "declaration", template_id, decl);
2349 if (header_count && header_count != template_count + 1)
2350 inform (input_location, "saw %d %<template<>%>, need %d for "
2351 "specializing a member function template",
2352 header_count, template_count + 1);
2353 else
2354 print_candidates (orig_fns);
2355 return error_mark_node;
2357 else if ((templates && TREE_CHAIN (templates))
2358 || (candidates && TREE_CHAIN (candidates))
2359 || (templates && candidates))
2361 error ("ambiguous template specialization %qD for %q+D",
2362 template_id, decl);
2363 candidates = chainon (candidates, templates);
2364 print_candidates (candidates);
2365 return error_mark_node;
2368 /* We have one, and exactly one, match. */
2369 if (candidates)
2371 tree fn = TREE_VALUE (candidates);
2372 *targs_out = copy_node (DECL_TI_ARGS (fn));
2374 // Propagate the candidate's constraints to the declaration.
2375 set_constraints (decl, get_constraints (fn));
2377 /* DECL is a re-declaration or partial instantiation of a template
2378 function. */
2379 if (TREE_CODE (fn) == TEMPLATE_DECL)
2380 return fn;
2381 /* It was a specialization of an ordinary member function in a
2382 template class. */
2383 return DECL_TI_TEMPLATE (fn);
2386 /* It was a specialization of a template. */
2387 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2388 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2390 *targs_out = copy_node (targs);
2391 SET_TMPL_ARGS_LEVEL (*targs_out,
2392 TMPL_ARGS_DEPTH (*targs_out),
2393 TREE_PURPOSE (templates));
2395 else
2396 *targs_out = TREE_PURPOSE (templates);
2397 return TREE_VALUE (templates);
2400 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2401 but with the default argument values filled in from those in the
2402 TMPL_TYPES. */
2404 static tree
2405 copy_default_args_to_explicit_spec_1 (tree spec_types,
2406 tree tmpl_types)
2408 tree new_spec_types;
2410 if (!spec_types)
2411 return NULL_TREE;
2413 if (spec_types == void_list_node)
2414 return void_list_node;
2416 /* Substitute into the rest of the list. */
2417 new_spec_types =
2418 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2419 TREE_CHAIN (tmpl_types));
2421 /* Add the default argument for this parameter. */
2422 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2423 TREE_VALUE (spec_types),
2424 new_spec_types);
2427 /* DECL is an explicit specialization. Replicate default arguments
2428 from the template it specializes. (That way, code like:
2430 template <class T> void f(T = 3);
2431 template <> void f(double);
2432 void g () { f (); }
2434 works, as required.) An alternative approach would be to look up
2435 the correct default arguments at the call-site, but this approach
2436 is consistent with how implicit instantiations are handled. */
2438 static void
2439 copy_default_args_to_explicit_spec (tree decl)
2441 tree tmpl;
2442 tree spec_types;
2443 tree tmpl_types;
2444 tree new_spec_types;
2445 tree old_type;
2446 tree new_type;
2447 tree t;
2448 tree object_type = NULL_TREE;
2449 tree in_charge = NULL_TREE;
2450 tree vtt = NULL_TREE;
2452 /* See if there's anything we need to do. */
2453 tmpl = DECL_TI_TEMPLATE (decl);
2454 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2455 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2456 if (TREE_PURPOSE (t))
2457 break;
2458 if (!t)
2459 return;
2461 old_type = TREE_TYPE (decl);
2462 spec_types = TYPE_ARG_TYPES (old_type);
2464 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2466 /* Remove the this pointer, but remember the object's type for
2467 CV quals. */
2468 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2469 spec_types = TREE_CHAIN (spec_types);
2470 tmpl_types = TREE_CHAIN (tmpl_types);
2472 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2474 /* DECL may contain more parameters than TMPL due to the extra
2475 in-charge parameter in constructors and destructors. */
2476 in_charge = spec_types;
2477 spec_types = TREE_CHAIN (spec_types);
2479 if (DECL_HAS_VTT_PARM_P (decl))
2481 vtt = spec_types;
2482 spec_types = TREE_CHAIN (spec_types);
2486 /* Compute the merged default arguments. */
2487 new_spec_types =
2488 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2490 /* Compute the new FUNCTION_TYPE. */
2491 if (object_type)
2493 if (vtt)
2494 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2495 TREE_VALUE (vtt),
2496 new_spec_types);
2498 if (in_charge)
2499 /* Put the in-charge parameter back. */
2500 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2501 TREE_VALUE (in_charge),
2502 new_spec_types);
2504 new_type = build_method_type_directly (object_type,
2505 TREE_TYPE (old_type),
2506 new_spec_types);
2508 else
2509 new_type = build_function_type (TREE_TYPE (old_type),
2510 new_spec_types);
2511 new_type = cp_build_type_attribute_variant (new_type,
2512 TYPE_ATTRIBUTES (old_type));
2513 new_type = build_exception_variant (new_type,
2514 TYPE_RAISES_EXCEPTIONS (old_type));
2516 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2517 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2519 TREE_TYPE (decl) = new_type;
2522 /* Return the number of template headers we expect to see for a definition
2523 or specialization of CTYPE or one of its non-template members. */
2526 num_template_headers_for_class (tree ctype)
2528 int num_templates = 0;
2530 while (ctype && CLASS_TYPE_P (ctype))
2532 /* You're supposed to have one `template <...>' for every
2533 template class, but you don't need one for a full
2534 specialization. For example:
2536 template <class T> struct S{};
2537 template <> struct S<int> { void f(); };
2538 void S<int>::f () {}
2540 is correct; there shouldn't be a `template <>' for the
2541 definition of `S<int>::f'. */
2542 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2543 /* If CTYPE does not have template information of any
2544 kind, then it is not a template, nor is it nested
2545 within a template. */
2546 break;
2547 if (explicit_class_specialization_p (ctype))
2548 break;
2549 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2550 ++num_templates;
2552 ctype = TYPE_CONTEXT (ctype);
2555 return num_templates;
2558 /* Do a simple sanity check on the template headers that precede the
2559 variable declaration DECL. */
2561 void
2562 check_template_variable (tree decl)
2564 tree ctx = CP_DECL_CONTEXT (decl);
2565 int wanted = num_template_headers_for_class (ctx);
2566 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2567 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2569 if (cxx_dialect < cxx14)
2570 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2571 "variable templates only available with "
2572 "-std=c++14 or -std=gnu++14");
2574 // Namespace-scope variable templates should have a template header.
2575 ++wanted;
2577 if (template_header_count > wanted)
2579 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2580 "too many template headers for %D (should be %d)",
2581 decl, wanted);
2582 if (warned && CLASS_TYPE_P (ctx)
2583 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2584 inform (DECL_SOURCE_LOCATION (decl),
2585 "members of an explicitly specialized class are defined "
2586 "without a template header");
2590 /* An explicit specialization whose declarator-id or class-head-name is not
2591 qualified shall be declared in the nearest enclosing namespace of the
2592 template, or, if the namespace is inline (7.3.1), any namespace from its
2593 enclosing namespace set.
2595 If the name declared in the explicit instantiation is an unqualified name,
2596 the explicit instantiation shall appear in the namespace where its template
2597 is declared or, if that namespace is inline (7.3.1), any namespace from its
2598 enclosing namespace set. */
2600 void
2601 check_unqualified_spec_or_inst (tree t, location_t loc)
2603 tree tmpl = most_general_template (t);
2604 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2605 && !is_associated_namespace (current_namespace,
2606 CP_DECL_CONTEXT (tmpl)))
2608 if (processing_specialization)
2609 permerror (loc, "explicit specialization of %qD outside its "
2610 "namespace must use a nested-name-specifier", tmpl);
2611 else if (processing_explicit_instantiation
2612 && cxx_dialect >= cxx11)
2613 /* This was allowed in C++98, so only pedwarn. */
2614 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2615 "outside its namespace must use a nested-name-"
2616 "specifier", tmpl);
2620 /* Check to see if the function just declared, as indicated in
2621 DECLARATOR, and in DECL, is a specialization of a function
2622 template. We may also discover that the declaration is an explicit
2623 instantiation at this point.
2625 Returns DECL, or an equivalent declaration that should be used
2626 instead if all goes well. Issues an error message if something is
2627 amiss. Returns error_mark_node if the error is not easily
2628 recoverable.
2630 FLAGS is a bitmask consisting of the following flags:
2632 2: The function has a definition.
2633 4: The function is a friend.
2635 The TEMPLATE_COUNT is the number of references to qualifying
2636 template classes that appeared in the name of the function. For
2637 example, in
2639 template <class T> struct S { void f(); };
2640 void S<int>::f();
2642 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2643 classes are not counted in the TEMPLATE_COUNT, so that in
2645 template <class T> struct S {};
2646 template <> struct S<int> { void f(); }
2647 template <> void S<int>::f();
2649 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2650 invalid; there should be no template <>.)
2652 If the function is a specialization, it is marked as such via
2653 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2654 is set up correctly, and it is added to the list of specializations
2655 for that template. */
2657 tree
2658 check_explicit_specialization (tree declarator,
2659 tree decl,
2660 int template_count,
2661 int flags)
2663 int have_def = flags & 2;
2664 int is_friend = flags & 4;
2665 bool is_concept = flags & 8;
2666 int specialization = 0;
2667 int explicit_instantiation = 0;
2668 int member_specialization = 0;
2669 tree ctype = DECL_CLASS_CONTEXT (decl);
2670 tree dname = DECL_NAME (decl);
2671 tmpl_spec_kind tsk;
2673 if (is_friend)
2675 if (!processing_specialization)
2676 tsk = tsk_none;
2677 else
2678 tsk = tsk_excessive_parms;
2680 else
2681 tsk = current_tmpl_spec_kind (template_count);
2683 switch (tsk)
2685 case tsk_none:
2686 if (processing_specialization && !VAR_P (decl))
2688 specialization = 1;
2689 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2691 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2693 if (is_friend)
2694 /* This could be something like:
2696 template <class T> void f(T);
2697 class S { friend void f<>(int); } */
2698 specialization = 1;
2699 else
2701 /* This case handles bogus declarations like template <>
2702 template <class T> void f<int>(); */
2704 error ("template-id %qD in declaration of primary template",
2705 declarator);
2706 return decl;
2709 break;
2711 case tsk_invalid_member_spec:
2712 /* The error has already been reported in
2713 check_specialization_scope. */
2714 return error_mark_node;
2716 case tsk_invalid_expl_inst:
2717 error ("template parameter list used in explicit instantiation");
2719 /* Fall through. */
2721 case tsk_expl_inst:
2722 if (have_def)
2723 error ("definition provided for explicit instantiation");
2725 explicit_instantiation = 1;
2726 break;
2728 case tsk_excessive_parms:
2729 case tsk_insufficient_parms:
2730 if (tsk == tsk_excessive_parms)
2731 error ("too many template parameter lists in declaration of %qD",
2732 decl);
2733 else if (template_header_count)
2734 error("too few template parameter lists in declaration of %qD", decl);
2735 else
2736 error("explicit specialization of %qD must be introduced by "
2737 "%<template <>%>", decl);
2739 /* Fall through. */
2740 case tsk_expl_spec:
2741 if (is_concept)
2742 error ("explicit specialization declared %<concept%>");
2744 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2745 /* In cases like template<> constexpr bool v = true;
2746 We'll give an error in check_template_variable. */
2747 break;
2749 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2750 if (ctype)
2751 member_specialization = 1;
2752 else
2753 specialization = 1;
2754 break;
2756 case tsk_template:
2757 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2759 /* This case handles bogus declarations like template <>
2760 template <class T> void f<int>(); */
2762 if (!uses_template_parms (declarator))
2763 error ("template-id %qD in declaration of primary template",
2764 declarator);
2765 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2767 /* Partial specialization of variable template. */
2768 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2769 specialization = 1;
2770 goto ok;
2772 else if (cxx_dialect < cxx14)
2773 error ("non-type partial specialization %qD "
2774 "is not allowed", declarator);
2775 else
2776 error ("non-class, non-variable partial specialization %qD "
2777 "is not allowed", declarator);
2778 return decl;
2779 ok:;
2782 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2783 /* This is a specialization of a member template, without
2784 specialization the containing class. Something like:
2786 template <class T> struct S {
2787 template <class U> void f (U);
2789 template <> template <class U> void S<int>::f(U) {}
2791 That's a specialization -- but of the entire template. */
2792 specialization = 1;
2793 break;
2795 default:
2796 gcc_unreachable ();
2799 if ((specialization || member_specialization)
2800 /* This doesn't apply to variable templates. */
2801 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2802 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2804 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2805 for (; t; t = TREE_CHAIN (t))
2806 if (TREE_PURPOSE (t))
2808 permerror (input_location,
2809 "default argument specified in explicit specialization");
2810 break;
2814 if (specialization || member_specialization || explicit_instantiation)
2816 tree tmpl = NULL_TREE;
2817 tree targs = NULL_TREE;
2818 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2820 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2821 if (!was_template_id)
2823 tree fns;
2825 gcc_assert (identifier_p (declarator));
2826 if (ctype)
2827 fns = dname;
2828 else
2830 /* If there is no class context, the explicit instantiation
2831 must be at namespace scope. */
2832 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2834 /* Find the namespace binding, using the declaration
2835 context. */
2836 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2837 false, true);
2838 if (fns == error_mark_node)
2839 /* If lookup fails, look for a friend declaration so we can
2840 give a better diagnostic. */
2841 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2842 /*type*/false, /*complain*/true,
2843 /*hidden*/true);
2845 if (fns == error_mark_node || !is_overloaded_fn (fns))
2847 error ("%qD is not a template function", dname);
2848 fns = error_mark_node;
2852 declarator = lookup_template_function (fns, NULL_TREE);
2855 if (declarator == error_mark_node)
2856 return error_mark_node;
2858 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2860 if (!explicit_instantiation)
2861 /* A specialization in class scope. This is invalid,
2862 but the error will already have been flagged by
2863 check_specialization_scope. */
2864 return error_mark_node;
2865 else
2867 /* It's not valid to write an explicit instantiation in
2868 class scope, e.g.:
2870 class C { template void f(); }
2872 This case is caught by the parser. However, on
2873 something like:
2875 template class C { void f(); };
2877 (which is invalid) we can get here. The error will be
2878 issued later. */
2882 return decl;
2884 else if (ctype != NULL_TREE
2885 && (identifier_p (TREE_OPERAND (declarator, 0))))
2887 // We'll match variable templates in start_decl.
2888 if (VAR_P (decl))
2889 return decl;
2891 /* Find the list of functions in ctype that have the same
2892 name as the declared function. */
2893 tree name = TREE_OPERAND (declarator, 0);
2894 tree fns = NULL_TREE;
2895 int idx;
2897 if (constructor_name_p (name, ctype))
2899 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2901 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2902 : !CLASSTYPE_DESTRUCTORS (ctype))
2904 /* From [temp.expl.spec]:
2906 If such an explicit specialization for the member
2907 of a class template names an implicitly-declared
2908 special member function (clause _special_), the
2909 program is ill-formed.
2911 Similar language is found in [temp.explicit]. */
2912 error ("specialization of implicitly-declared special member function");
2913 return error_mark_node;
2916 name = is_constructor ? ctor_identifier : dtor_identifier;
2919 if (!DECL_CONV_FN_P (decl))
2921 idx = lookup_fnfields_1 (ctype, name);
2922 if (idx >= 0)
2923 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2925 else
2927 vec<tree, va_gc> *methods;
2928 tree ovl;
2930 /* For a type-conversion operator, we cannot do a
2931 name-based lookup. We might be looking for `operator
2932 int' which will be a specialization of `operator T'.
2933 So, we find *all* the conversion operators, and then
2934 select from them. */
2935 fns = NULL_TREE;
2937 methods = CLASSTYPE_METHOD_VEC (ctype);
2938 if (methods)
2939 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2940 methods->iterate (idx, &ovl);
2941 ++idx)
2943 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2944 /* There are no more conversion functions. */
2945 break;
2947 /* Glue all these conversion functions together
2948 with those we already have. */
2949 for (; ovl; ovl = OVL_NEXT (ovl))
2950 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2954 if (fns == NULL_TREE)
2956 error ("no member function %qD declared in %qT", name, ctype);
2957 return error_mark_node;
2959 else
2960 TREE_OPERAND (declarator, 0) = fns;
2963 /* Figure out what exactly is being specialized at this point.
2964 Note that for an explicit instantiation, even one for a
2965 member function, we cannot tell apriori whether the
2966 instantiation is for a member template, or just a member
2967 function of a template class. Even if a member template is
2968 being instantiated, the member template arguments may be
2969 elided if they can be deduced from the rest of the
2970 declaration. */
2971 tmpl = determine_specialization (declarator, decl,
2972 &targs,
2973 member_specialization,
2974 template_count,
2975 tsk);
2977 if (!tmpl || tmpl == error_mark_node)
2978 /* We couldn't figure out what this declaration was
2979 specializing. */
2980 return error_mark_node;
2981 else
2983 if (TREE_CODE (decl) == FUNCTION_DECL
2984 && DECL_HIDDEN_FRIEND_P (tmpl))
2986 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2987 "friend declaration %qD is not visible to "
2988 "explicit specialization", tmpl))
2989 inform (DECL_SOURCE_LOCATION (tmpl),
2990 "friend declaration here");
2992 else if (!ctype && !is_friend
2993 && CP_DECL_CONTEXT (decl) == current_namespace)
2994 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2996 tree gen_tmpl = most_general_template (tmpl);
2998 if (explicit_instantiation)
3000 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3001 is done by do_decl_instantiation later. */
3003 int arg_depth = TMPL_ARGS_DEPTH (targs);
3004 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3006 if (arg_depth > parm_depth)
3008 /* If TMPL is not the most general template (for
3009 example, if TMPL is a friend template that is
3010 injected into namespace scope), then there will
3011 be too many levels of TARGS. Remove some of them
3012 here. */
3013 int i;
3014 tree new_targs;
3016 new_targs = make_tree_vec (parm_depth);
3017 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3018 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3019 = TREE_VEC_ELT (targs, i);
3020 targs = new_targs;
3023 return instantiate_template (tmpl, targs, tf_error);
3026 /* If we thought that the DECL was a member function, but it
3027 turns out to be specializing a static member function,
3028 make DECL a static member function as well. */
3029 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3030 && DECL_STATIC_FUNCTION_P (tmpl)
3031 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3032 revert_static_member_fn (decl);
3034 /* If this is a specialization of a member template of a
3035 template class, we want to return the TEMPLATE_DECL, not
3036 the specialization of it. */
3037 if (tsk == tsk_template && !was_template_id)
3039 tree result = DECL_TEMPLATE_RESULT (tmpl);
3040 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3041 DECL_INITIAL (result) = NULL_TREE;
3042 if (have_def)
3044 tree parm;
3045 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3046 DECL_SOURCE_LOCATION (result)
3047 = DECL_SOURCE_LOCATION (decl);
3048 /* We want to use the argument list specified in the
3049 definition, not in the original declaration. */
3050 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3051 for (parm = DECL_ARGUMENTS (result); parm;
3052 parm = DECL_CHAIN (parm))
3053 DECL_CONTEXT (parm) = result;
3055 return register_specialization (tmpl, gen_tmpl, targs,
3056 is_friend, 0);
3059 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3060 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3062 if (was_template_id)
3063 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3065 /* Inherit default function arguments from the template
3066 DECL is specializing. */
3067 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3068 copy_default_args_to_explicit_spec (decl);
3070 /* This specialization has the same protection as the
3071 template it specializes. */
3072 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3073 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3075 /* 7.1.1-1 [dcl.stc]
3077 A storage-class-specifier shall not be specified in an
3078 explicit specialization...
3080 The parser rejects these, so unless action is taken here,
3081 explicit function specializations will always appear with
3082 global linkage.
3084 The action recommended by the C++ CWG in response to C++
3085 defect report 605 is to make the storage class and linkage
3086 of the explicit specialization match the templated function:
3088 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3090 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3092 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3093 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3095 /* A concept cannot be specialized. */
3096 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3098 error ("explicit specialization of function concept %qD",
3099 gen_tmpl);
3100 return error_mark_node;
3103 /* This specialization has the same linkage and visibility as
3104 the function template it specializes. */
3105 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3106 if (! TREE_PUBLIC (decl))
3108 DECL_INTERFACE_KNOWN (decl) = 1;
3109 DECL_NOT_REALLY_EXTERN (decl) = 1;
3111 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3112 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3114 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3115 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3119 /* If DECL is a friend declaration, declared using an
3120 unqualified name, the namespace associated with DECL may
3121 have been set incorrectly. For example, in:
3123 template <typename T> void f(T);
3124 namespace N {
3125 struct S { friend void f<int>(int); }
3128 we will have set the DECL_CONTEXT for the friend
3129 declaration to N, rather than to the global namespace. */
3130 if (DECL_NAMESPACE_SCOPE_P (decl))
3131 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3133 if (is_friend && !have_def)
3134 /* This is not really a declaration of a specialization.
3135 It's just the name of an instantiation. But, it's not
3136 a request for an instantiation, either. */
3137 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3138 else if (TREE_CODE (decl) == FUNCTION_DECL)
3139 /* A specialization is not necessarily COMDAT. */
3140 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3141 && DECL_DECLARED_INLINE_P (decl));
3142 else if (VAR_P (decl))
3143 DECL_COMDAT (decl) = false;
3145 /* If this is a full specialization, register it so that we can find
3146 it again. Partial specializations will be registered in
3147 process_partial_specialization. */
3148 if (!processing_template_decl)
3149 decl = register_specialization (decl, gen_tmpl, targs,
3150 is_friend, 0);
3152 /* A 'structor should already have clones. */
3153 gcc_assert (decl == error_mark_node
3154 || variable_template_p (tmpl)
3155 || !(DECL_CONSTRUCTOR_P (decl)
3156 || DECL_DESTRUCTOR_P (decl))
3157 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3161 return decl;
3164 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3165 parameters. These are represented in the same format used for
3166 DECL_TEMPLATE_PARMS. */
3169 comp_template_parms (const_tree parms1, const_tree parms2)
3171 const_tree p1;
3172 const_tree p2;
3174 if (parms1 == parms2)
3175 return 1;
3177 for (p1 = parms1, p2 = parms2;
3178 p1 != NULL_TREE && p2 != NULL_TREE;
3179 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3181 tree t1 = TREE_VALUE (p1);
3182 tree t2 = TREE_VALUE (p2);
3183 int i;
3185 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3186 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3188 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3189 return 0;
3191 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3193 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3194 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3196 /* If either of the template parameters are invalid, assume
3197 they match for the sake of error recovery. */
3198 if (error_operand_p (parm1) || error_operand_p (parm2))
3199 return 1;
3201 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3202 return 0;
3204 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3205 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3206 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3207 continue;
3208 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3209 return 0;
3213 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3214 /* One set of parameters has more parameters lists than the
3215 other. */
3216 return 0;
3218 return 1;
3221 /* Determine whether PARM is a parameter pack. */
3223 bool
3224 template_parameter_pack_p (const_tree parm)
3226 /* Determine if we have a non-type template parameter pack. */
3227 if (TREE_CODE (parm) == PARM_DECL)
3228 return (DECL_TEMPLATE_PARM_P (parm)
3229 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3230 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3231 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3233 /* If this is a list of template parameters, we could get a
3234 TYPE_DECL or a TEMPLATE_DECL. */
3235 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3236 parm = TREE_TYPE (parm);
3238 /* Otherwise it must be a type template parameter. */
3239 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3240 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3241 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3244 /* Determine if T is a function parameter pack. */
3246 bool
3247 function_parameter_pack_p (const_tree t)
3249 if (t && TREE_CODE (t) == PARM_DECL)
3250 return DECL_PACK_P (t);
3251 return false;
3254 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3255 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3257 tree
3258 get_function_template_decl (const_tree primary_func_tmpl_inst)
3260 if (! primary_func_tmpl_inst
3261 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3262 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3263 return NULL;
3265 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3268 /* Return true iff the function parameter PARAM_DECL was expanded
3269 from the function parameter pack PACK. */
3271 bool
3272 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3274 if (DECL_ARTIFICIAL (param_decl)
3275 || !function_parameter_pack_p (pack))
3276 return false;
3278 /* The parameter pack and its pack arguments have the same
3279 DECL_PARM_INDEX. */
3280 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3283 /* Determine whether ARGS describes a variadic template args list,
3284 i.e., one that is terminated by a template argument pack. */
3286 static bool
3287 template_args_variadic_p (tree args)
3289 int nargs;
3290 tree last_parm;
3292 if (args == NULL_TREE)
3293 return false;
3295 args = INNERMOST_TEMPLATE_ARGS (args);
3296 nargs = TREE_VEC_LENGTH (args);
3298 if (nargs == 0)
3299 return false;
3301 last_parm = TREE_VEC_ELT (args, nargs - 1);
3303 return ARGUMENT_PACK_P (last_parm);
3306 /* Generate a new name for the parameter pack name NAME (an
3307 IDENTIFIER_NODE) that incorporates its */
3309 static tree
3310 make_ith_pack_parameter_name (tree name, int i)
3312 /* Munge the name to include the parameter index. */
3313 #define NUMBUF_LEN 128
3314 char numbuf[NUMBUF_LEN];
3315 char* newname;
3316 int newname_len;
3318 if (name == NULL_TREE)
3319 return name;
3320 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3321 newname_len = IDENTIFIER_LENGTH (name)
3322 + strlen (numbuf) + 2;
3323 newname = (char*)alloca (newname_len);
3324 snprintf (newname, newname_len,
3325 "%s#%i", IDENTIFIER_POINTER (name), i);
3326 return get_identifier (newname);
3329 /* Return true if T is a primary function, class or alias template
3330 instantiation. */
3332 bool
3333 primary_template_instantiation_p (const_tree t)
3335 if (!t)
3336 return false;
3338 if (TREE_CODE (t) == FUNCTION_DECL)
3339 return DECL_LANG_SPECIFIC (t)
3340 && DECL_TEMPLATE_INSTANTIATION (t)
3341 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3342 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3343 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3344 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3345 else if (alias_template_specialization_p (t))
3346 return true;
3347 return false;
3350 /* Return true if PARM is a template template parameter. */
3352 bool
3353 template_template_parameter_p (const_tree parm)
3355 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3358 /* Return true iff PARM is a DECL representing a type template
3359 parameter. */
3361 bool
3362 template_type_parameter_p (const_tree parm)
3364 return (parm
3365 && (TREE_CODE (parm) == TYPE_DECL
3366 || TREE_CODE (parm) == TEMPLATE_DECL)
3367 && DECL_TEMPLATE_PARM_P (parm));
3370 /* Return the template parameters of T if T is a
3371 primary template instantiation, NULL otherwise. */
3373 tree
3374 get_primary_template_innermost_parameters (const_tree t)
3376 tree parms = NULL, template_info = NULL;
3378 if ((template_info = get_template_info (t))
3379 && primary_template_instantiation_p (t))
3380 parms = INNERMOST_TEMPLATE_PARMS
3381 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3383 return parms;
3386 /* Return the template parameters of the LEVELth level from the full list
3387 of template parameters PARMS. */
3389 tree
3390 get_template_parms_at_level (tree parms, int level)
3392 tree p;
3393 if (!parms
3394 || TREE_CODE (parms) != TREE_LIST
3395 || level > TMPL_PARMS_DEPTH (parms))
3396 return NULL_TREE;
3398 for (p = parms; p; p = TREE_CHAIN (p))
3399 if (TMPL_PARMS_DEPTH (p) == level)
3400 return p;
3402 return NULL_TREE;
3405 /* Returns the template arguments of T if T is a template instantiation,
3406 NULL otherwise. */
3408 tree
3409 get_template_innermost_arguments (const_tree t)
3411 tree args = NULL, template_info = NULL;
3413 if ((template_info = get_template_info (t))
3414 && TI_ARGS (template_info))
3415 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3417 return args;
3420 /* Return the argument pack elements of T if T is a template argument pack,
3421 NULL otherwise. */
3423 tree
3424 get_template_argument_pack_elems (const_tree t)
3426 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3427 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3428 return NULL;
3430 return ARGUMENT_PACK_ARGS (t);
3433 /* Structure used to track the progress of find_parameter_packs_r. */
3434 struct find_parameter_pack_data
3436 /* TREE_LIST that will contain all of the parameter packs found by
3437 the traversal. */
3438 tree* parameter_packs;
3440 /* Set of AST nodes that have been visited by the traversal. */
3441 hash_set<tree> *visited;
3443 /* True iff we're making a type pack expansion. */
3444 bool type_pack_expansion_p;
3447 /* Identifies all of the argument packs that occur in a template
3448 argument and appends them to the TREE_LIST inside DATA, which is a
3449 find_parameter_pack_data structure. This is a subroutine of
3450 make_pack_expansion and uses_parameter_packs. */
3451 static tree
3452 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3454 tree t = *tp;
3455 struct find_parameter_pack_data* ppd =
3456 (struct find_parameter_pack_data*)data;
3457 bool parameter_pack_p = false;
3459 /* Handle type aliases/typedefs. */
3460 if (TYPE_ALIAS_P (t))
3462 if (TYPE_TEMPLATE_INFO (t))
3463 cp_walk_tree (&TYPE_TI_ARGS (t),
3464 &find_parameter_packs_r,
3465 ppd, ppd->visited);
3466 *walk_subtrees = 0;
3467 return NULL_TREE;
3470 /* Identify whether this is a parameter pack or not. */
3471 switch (TREE_CODE (t))
3473 case TEMPLATE_PARM_INDEX:
3474 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3475 parameter_pack_p = true;
3476 break;
3478 case TEMPLATE_TYPE_PARM:
3479 t = TYPE_MAIN_VARIANT (t);
3480 /* FALLTHRU */
3481 case TEMPLATE_TEMPLATE_PARM:
3482 /* If the placeholder appears in the decl-specifier-seq of a function
3483 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3484 is a pack expansion, the invented template parameter is a template
3485 parameter pack. */
3486 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3487 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3488 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3489 parameter_pack_p = true;
3490 break;
3492 case FIELD_DECL:
3493 case PARM_DECL:
3494 if (DECL_PACK_P (t))
3496 /* We don't want to walk into the type of a PARM_DECL,
3497 because we don't want to see the type parameter pack. */
3498 *walk_subtrees = 0;
3499 parameter_pack_p = true;
3501 break;
3503 /* Look through a lambda capture proxy to the field pack. */
3504 case VAR_DECL:
3505 if (DECL_HAS_VALUE_EXPR_P (t))
3507 tree v = DECL_VALUE_EXPR (t);
3508 cp_walk_tree (&v,
3509 &find_parameter_packs_r,
3510 ppd, ppd->visited);
3511 *walk_subtrees = 0;
3513 else if (variable_template_specialization_p (t))
3515 cp_walk_tree (&DECL_TI_ARGS (t),
3516 find_parameter_packs_r,
3517 ppd, ppd->visited);
3518 *walk_subtrees = 0;
3520 break;
3522 case BASES:
3523 parameter_pack_p = true;
3524 break;
3525 default:
3526 /* Not a parameter pack. */
3527 break;
3530 if (parameter_pack_p)
3532 /* Add this parameter pack to the list. */
3533 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3536 if (TYPE_P (t))
3537 cp_walk_tree (&TYPE_CONTEXT (t),
3538 &find_parameter_packs_r, ppd, ppd->visited);
3540 /* This switch statement will return immediately if we don't find a
3541 parameter pack. */
3542 switch (TREE_CODE (t))
3544 case TEMPLATE_PARM_INDEX:
3545 return NULL_TREE;
3547 case BOUND_TEMPLATE_TEMPLATE_PARM:
3548 /* Check the template itself. */
3549 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3550 &find_parameter_packs_r, ppd, ppd->visited);
3551 /* Check the template arguments. */
3552 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3553 ppd->visited);
3554 *walk_subtrees = 0;
3555 return NULL_TREE;
3557 case TEMPLATE_TYPE_PARM:
3558 case TEMPLATE_TEMPLATE_PARM:
3559 return NULL_TREE;
3561 case PARM_DECL:
3562 return NULL_TREE;
3564 case RECORD_TYPE:
3565 if (TYPE_PTRMEMFUNC_P (t))
3566 return NULL_TREE;
3567 /* Fall through. */
3569 case UNION_TYPE:
3570 case ENUMERAL_TYPE:
3571 if (TYPE_TEMPLATE_INFO (t))
3572 cp_walk_tree (&TYPE_TI_ARGS (t),
3573 &find_parameter_packs_r, ppd, ppd->visited);
3575 *walk_subtrees = 0;
3576 return NULL_TREE;
3578 case CONSTRUCTOR:
3579 case TEMPLATE_DECL:
3580 cp_walk_tree (&TREE_TYPE (t),
3581 &find_parameter_packs_r, ppd, ppd->visited);
3582 return NULL_TREE;
3584 case TYPENAME_TYPE:
3585 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3586 ppd, ppd->visited);
3587 *walk_subtrees = 0;
3588 return NULL_TREE;
3590 case TYPE_PACK_EXPANSION:
3591 case EXPR_PACK_EXPANSION:
3592 *walk_subtrees = 0;
3593 return NULL_TREE;
3595 case INTEGER_TYPE:
3596 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3597 ppd, ppd->visited);
3598 *walk_subtrees = 0;
3599 return NULL_TREE;
3601 case IDENTIFIER_NODE:
3602 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3603 ppd->visited);
3604 *walk_subtrees = 0;
3605 return NULL_TREE;
3607 case DECLTYPE_TYPE:
3609 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3610 type_pack_expansion_p to false so that any placeholders
3611 within the expression don't get marked as parameter packs. */
3612 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3613 ppd->type_pack_expansion_p = false;
3614 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3615 ppd, ppd->visited);
3616 ppd->type_pack_expansion_p = type_pack_expansion_p;
3617 *walk_subtrees = 0;
3618 return NULL_TREE;
3621 default:
3622 return NULL_TREE;
3625 return NULL_TREE;
3628 /* Determines if the expression or type T uses any parameter packs. */
3629 bool
3630 uses_parameter_packs (tree t)
3632 tree parameter_packs = NULL_TREE;
3633 struct find_parameter_pack_data ppd;
3634 ppd.parameter_packs = &parameter_packs;
3635 ppd.visited = new hash_set<tree>;
3636 ppd.type_pack_expansion_p = false;
3637 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3638 delete ppd.visited;
3639 return parameter_packs != NULL_TREE;
3642 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3643 representation a base-class initializer into a parameter pack
3644 expansion. If all goes well, the resulting node will be an
3645 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3646 respectively. */
3647 tree
3648 make_pack_expansion (tree arg)
3650 tree result;
3651 tree parameter_packs = NULL_TREE;
3652 bool for_types = false;
3653 struct find_parameter_pack_data ppd;
3655 if (!arg || arg == error_mark_node)
3656 return arg;
3658 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3660 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3661 class initializer. In this case, the TREE_PURPOSE will be a
3662 _TYPE node (representing the base class expansion we're
3663 initializing) and the TREE_VALUE will be a TREE_LIST
3664 containing the initialization arguments.
3666 The resulting expansion looks somewhat different from most
3667 expansions. Rather than returning just one _EXPANSION, we
3668 return a TREE_LIST whose TREE_PURPOSE is a
3669 TYPE_PACK_EXPANSION containing the bases that will be
3670 initialized. The TREE_VALUE will be identical to the
3671 original TREE_VALUE, which is a list of arguments that will
3672 be passed to each base. We do not introduce any new pack
3673 expansion nodes into the TREE_VALUE (although it is possible
3674 that some already exist), because the TREE_PURPOSE and
3675 TREE_VALUE all need to be expanded together with the same
3676 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3677 resulting TREE_PURPOSE will mention the parameter packs in
3678 both the bases and the arguments to the bases. */
3679 tree purpose;
3680 tree value;
3681 tree parameter_packs = NULL_TREE;
3683 /* Determine which parameter packs will be used by the base
3684 class expansion. */
3685 ppd.visited = new hash_set<tree>;
3686 ppd.parameter_packs = &parameter_packs;
3687 ppd.type_pack_expansion_p = true;
3688 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3689 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3690 &ppd, ppd.visited);
3692 if (parameter_packs == NULL_TREE)
3694 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3695 delete ppd.visited;
3696 return error_mark_node;
3699 if (TREE_VALUE (arg) != void_type_node)
3701 /* Collect the sets of parameter packs used in each of the
3702 initialization arguments. */
3703 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3705 /* Determine which parameter packs will be expanded in this
3706 argument. */
3707 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3708 &ppd, ppd.visited);
3712 delete ppd.visited;
3714 /* Create the pack expansion type for the base type. */
3715 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3716 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3717 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3719 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3720 they will rarely be compared to anything. */
3721 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3723 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3726 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3727 for_types = true;
3729 /* Build the PACK_EXPANSION_* node. */
3730 result = for_types
3731 ? cxx_make_type (TYPE_PACK_EXPANSION)
3732 : make_node (EXPR_PACK_EXPANSION);
3733 SET_PACK_EXPANSION_PATTERN (result, arg);
3734 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3736 /* Propagate type and const-expression information. */
3737 TREE_TYPE (result) = TREE_TYPE (arg);
3738 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3739 /* Mark this read now, since the expansion might be length 0. */
3740 mark_exp_read (arg);
3742 else
3743 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3744 they will rarely be compared to anything. */
3745 SET_TYPE_STRUCTURAL_EQUALITY (result);
3747 /* Determine which parameter packs will be expanded. */
3748 ppd.parameter_packs = &parameter_packs;
3749 ppd.visited = new hash_set<tree>;
3750 ppd.type_pack_expansion_p = TYPE_P (arg);
3751 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3752 delete ppd.visited;
3754 /* Make sure we found some parameter packs. */
3755 if (parameter_packs == NULL_TREE)
3757 if (TYPE_P (arg))
3758 error ("expansion pattern %<%T%> contains no argument packs", arg);
3759 else
3760 error ("expansion pattern %<%E%> contains no argument packs", arg);
3761 return error_mark_node;
3763 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3765 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3767 return result;
3770 /* Checks T for any "bare" parameter packs, which have not yet been
3771 expanded, and issues an error if any are found. This operation can
3772 only be done on full expressions or types (e.g., an expression
3773 statement, "if" condition, etc.), because we could have expressions like:
3775 foo(f(g(h(args)))...)
3777 where "args" is a parameter pack. check_for_bare_parameter_packs
3778 should not be called for the subexpressions args, h(args),
3779 g(h(args)), or f(g(h(args))), because we would produce erroneous
3780 error messages.
3782 Returns TRUE and emits an error if there were bare parameter packs,
3783 returns FALSE otherwise. */
3784 bool
3785 check_for_bare_parameter_packs (tree t)
3787 tree parameter_packs = NULL_TREE;
3788 struct find_parameter_pack_data ppd;
3790 if (!processing_template_decl || !t || t == error_mark_node)
3791 return false;
3793 if (TREE_CODE (t) == TYPE_DECL)
3794 t = TREE_TYPE (t);
3796 ppd.parameter_packs = &parameter_packs;
3797 ppd.visited = new hash_set<tree>;
3798 ppd.type_pack_expansion_p = false;
3799 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3800 delete ppd.visited;
3802 if (parameter_packs)
3804 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3805 error_at (loc, "parameter packs not expanded with %<...%>:");
3806 while (parameter_packs)
3808 tree pack = TREE_VALUE (parameter_packs);
3809 tree name = NULL_TREE;
3811 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3812 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3813 name = TYPE_NAME (pack);
3814 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3815 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3816 else
3817 name = DECL_NAME (pack);
3819 if (name)
3820 inform (loc, " %qD", name);
3821 else
3822 inform (loc, " <anonymous>");
3824 parameter_packs = TREE_CHAIN (parameter_packs);
3827 return true;
3830 return false;
3833 /* Expand any parameter packs that occur in the template arguments in
3834 ARGS. */
3835 tree
3836 expand_template_argument_pack (tree args)
3838 tree result_args = NULL_TREE;
3839 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3840 int num_result_args = -1;
3841 int non_default_args_count = -1;
3843 /* First, determine if we need to expand anything, and the number of
3844 slots we'll need. */
3845 for (in_arg = 0; in_arg < nargs; ++in_arg)
3847 tree arg = TREE_VEC_ELT (args, in_arg);
3848 if (arg == NULL_TREE)
3849 return args;
3850 if (ARGUMENT_PACK_P (arg))
3852 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3853 if (num_result_args < 0)
3854 num_result_args = in_arg + num_packed;
3855 else
3856 num_result_args += num_packed;
3858 else
3860 if (num_result_args >= 0)
3861 num_result_args++;
3865 /* If no expansion is necessary, we're done. */
3866 if (num_result_args < 0)
3867 return args;
3869 /* Expand arguments. */
3870 result_args = make_tree_vec (num_result_args);
3871 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3872 non_default_args_count =
3873 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3874 for (in_arg = 0; in_arg < nargs; ++in_arg)
3876 tree arg = TREE_VEC_ELT (args, in_arg);
3877 if (ARGUMENT_PACK_P (arg))
3879 tree packed = ARGUMENT_PACK_ARGS (arg);
3880 int i, num_packed = TREE_VEC_LENGTH (packed);
3881 for (i = 0; i < num_packed; ++i, ++out_arg)
3882 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3883 if (non_default_args_count > 0)
3884 non_default_args_count += num_packed - 1;
3886 else
3888 TREE_VEC_ELT (result_args, out_arg) = arg;
3889 ++out_arg;
3892 if (non_default_args_count >= 0)
3893 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3894 return result_args;
3897 /* Checks if DECL shadows a template parameter.
3899 [temp.local]: A template-parameter shall not be redeclared within its
3900 scope (including nested scopes).
3902 Emits an error and returns TRUE if the DECL shadows a parameter,
3903 returns FALSE otherwise. */
3905 bool
3906 check_template_shadow (tree decl)
3908 tree olddecl;
3910 /* If we're not in a template, we can't possibly shadow a template
3911 parameter. */
3912 if (!current_template_parms)
3913 return true;
3915 /* Figure out what we're shadowing. */
3916 if (TREE_CODE (decl) == OVERLOAD)
3917 decl = OVL_CURRENT (decl);
3918 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3920 /* If there's no previous binding for this name, we're not shadowing
3921 anything, let alone a template parameter. */
3922 if (!olddecl)
3923 return true;
3925 /* If we're not shadowing a template parameter, we're done. Note
3926 that OLDDECL might be an OVERLOAD (or perhaps even an
3927 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3928 node. */
3929 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3930 return true;
3932 /* We check for decl != olddecl to avoid bogus errors for using a
3933 name inside a class. We check TPFI to avoid duplicate errors for
3934 inline member templates. */
3935 if (decl == olddecl
3936 || (DECL_TEMPLATE_PARM_P (decl)
3937 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3938 return true;
3940 /* Don't complain about the injected class name, as we've already
3941 complained about the class itself. */
3942 if (DECL_SELF_REFERENCE_P (decl))
3943 return false;
3945 if (DECL_TEMPLATE_PARM_P (decl))
3946 error ("declaration of template parameter %q+D shadows "
3947 "template parameter", decl);
3948 else
3949 error ("declaration of %q+#D shadows template parameter", decl);
3950 inform (DECL_SOURCE_LOCATION (olddecl),
3951 "template parameter %qD declared here", olddecl);
3952 return false;
3955 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3956 ORIG_LEVEL, DECL, and TYPE. */
3958 static tree
3959 build_template_parm_index (int index,
3960 int level,
3961 int orig_level,
3962 tree decl,
3963 tree type)
3965 tree t = make_node (TEMPLATE_PARM_INDEX);
3966 TEMPLATE_PARM_IDX (t) = index;
3967 TEMPLATE_PARM_LEVEL (t) = level;
3968 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3969 TEMPLATE_PARM_DECL (t) = decl;
3970 TREE_TYPE (t) = type;
3971 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3972 TREE_READONLY (t) = TREE_READONLY (decl);
3974 return t;
3977 /* Find the canonical type parameter for the given template type
3978 parameter. Returns the canonical type parameter, which may be TYPE
3979 if no such parameter existed. */
3981 static tree
3982 canonical_type_parameter (tree type)
3984 tree list;
3985 int idx = TEMPLATE_TYPE_IDX (type);
3986 if (!canonical_template_parms)
3987 vec_alloc (canonical_template_parms, idx+1);
3989 while (canonical_template_parms->length () <= (unsigned)idx)
3990 vec_safe_push (canonical_template_parms, NULL_TREE);
3992 list = (*canonical_template_parms)[idx];
3993 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3994 list = TREE_CHAIN (list);
3996 if (list)
3997 return TREE_VALUE (list);
3998 else
4000 (*canonical_template_parms)[idx]
4001 = tree_cons (NULL_TREE, type,
4002 (*canonical_template_parms)[idx]);
4003 return type;
4007 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4008 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4009 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4010 new one is created. */
4012 static tree
4013 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4014 tsubst_flags_t complain)
4016 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4017 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4018 != TEMPLATE_PARM_LEVEL (index) - levels)
4019 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4021 tree orig_decl = TEMPLATE_PARM_DECL (index);
4022 tree decl, t;
4024 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4025 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4026 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4027 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4028 DECL_ARTIFICIAL (decl) = 1;
4029 SET_DECL_TEMPLATE_PARM_P (decl);
4031 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4032 TEMPLATE_PARM_LEVEL (index) - levels,
4033 TEMPLATE_PARM_ORIG_LEVEL (index),
4034 decl, type);
4035 TEMPLATE_PARM_DESCENDANTS (index) = t;
4036 TEMPLATE_PARM_PARAMETER_PACK (t)
4037 = TEMPLATE_PARM_PARAMETER_PACK (index);
4039 /* Template template parameters need this. */
4040 if (TREE_CODE (decl) == TEMPLATE_DECL)
4042 DECL_TEMPLATE_RESULT (decl)
4043 = build_decl (DECL_SOURCE_LOCATION (decl),
4044 TYPE_DECL, DECL_NAME (decl), type);
4045 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4046 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4047 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4051 return TEMPLATE_PARM_DESCENDANTS (index);
4054 /* Process information from new template parameter PARM and append it
4055 to the LIST being built. This new parameter is a non-type
4056 parameter iff IS_NON_TYPE is true. This new parameter is a
4057 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4058 is in PARM_LOC. */
4060 tree
4061 process_template_parm (tree list, location_t parm_loc, tree parm,
4062 bool is_non_type, bool is_parameter_pack)
4064 tree decl = 0;
4065 int idx = 0;
4067 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4068 tree defval = TREE_PURPOSE (parm);
4069 tree constr = TREE_TYPE (parm);
4071 if (list)
4073 tree p = tree_last (list);
4075 if (p && TREE_VALUE (p) != error_mark_node)
4077 p = TREE_VALUE (p);
4078 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4079 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4080 else
4081 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4084 ++idx;
4087 if (is_non_type)
4089 parm = TREE_VALUE (parm);
4091 SET_DECL_TEMPLATE_PARM_P (parm);
4093 if (TREE_TYPE (parm) != error_mark_node)
4095 /* [temp.param]
4097 The top-level cv-qualifiers on the template-parameter are
4098 ignored when determining its type. */
4099 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4100 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4101 TREE_TYPE (parm) = error_mark_node;
4102 else if (uses_parameter_packs (TREE_TYPE (parm))
4103 && !is_parameter_pack
4104 /* If we're in a nested template parameter list, the template
4105 template parameter could be a parameter pack. */
4106 && processing_template_parmlist == 1)
4108 /* This template parameter is not a parameter pack, but it
4109 should be. Complain about "bare" parameter packs. */
4110 check_for_bare_parameter_packs (TREE_TYPE (parm));
4112 /* Recover by calling this a parameter pack. */
4113 is_parameter_pack = true;
4117 /* A template parameter is not modifiable. */
4118 TREE_CONSTANT (parm) = 1;
4119 TREE_READONLY (parm) = 1;
4120 decl = build_decl (parm_loc,
4121 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4122 TREE_CONSTANT (decl) = 1;
4123 TREE_READONLY (decl) = 1;
4124 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4125 = build_template_parm_index (idx, processing_template_decl,
4126 processing_template_decl,
4127 decl, TREE_TYPE (parm));
4129 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4130 = is_parameter_pack;
4132 else
4134 tree t;
4135 parm = TREE_VALUE (TREE_VALUE (parm));
4137 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4139 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4140 /* This is for distinguishing between real templates and template
4141 template parameters */
4142 TREE_TYPE (parm) = t;
4143 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4144 decl = parm;
4146 else
4148 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4149 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4150 decl = build_decl (parm_loc,
4151 TYPE_DECL, parm, t);
4154 TYPE_NAME (t) = decl;
4155 TYPE_STUB_DECL (t) = decl;
4156 parm = decl;
4157 TEMPLATE_TYPE_PARM_INDEX (t)
4158 = build_template_parm_index (idx, processing_template_decl,
4159 processing_template_decl,
4160 decl, TREE_TYPE (parm));
4161 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4162 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4164 DECL_ARTIFICIAL (decl) = 1;
4165 SET_DECL_TEMPLATE_PARM_P (decl);
4167 /* Build requirements for the type/template parameter.
4168 This must be done after SET_DECL_TEMPLATE_PARM_P or
4169 process_template_parm could fail. */
4170 tree reqs = finish_shorthand_constraint (parm, constr);
4172 pushdecl (decl);
4174 /* Build the parameter node linking the parameter declaration,
4175 its default argument (if any), and its constraints (if any). */
4176 parm = build_tree_list (defval, parm);
4177 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4179 return chainon (list, parm);
4182 /* The end of a template parameter list has been reached. Process the
4183 tree list into a parameter vector, converting each parameter into a more
4184 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4185 as PARM_DECLs. */
4187 tree
4188 end_template_parm_list (tree parms)
4190 int nparms;
4191 tree parm, next;
4192 tree saved_parmlist = make_tree_vec (list_length (parms));
4194 /* Pop the dummy parameter level and add the real one. */
4195 current_template_parms = TREE_CHAIN (current_template_parms);
4197 current_template_parms
4198 = tree_cons (size_int (processing_template_decl),
4199 saved_parmlist, current_template_parms);
4201 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4203 next = TREE_CHAIN (parm);
4204 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4205 TREE_CHAIN (parm) = NULL_TREE;
4208 --processing_template_parmlist;
4210 return saved_parmlist;
4213 // Explicitly indicate the end of the template parameter list. We assume
4214 // that the current template parameters have been constructed and/or
4215 // managed explicitly, as when creating new template template parameters
4216 // from a shorthand constraint.
4217 void
4218 end_template_parm_list ()
4220 --processing_template_parmlist;
4223 /* end_template_decl is called after a template declaration is seen. */
4225 void
4226 end_template_decl (void)
4228 reset_specialization ();
4230 if (! processing_template_decl)
4231 return;
4233 /* This matches the pushlevel in begin_template_parm_list. */
4234 finish_scope ();
4236 --processing_template_decl;
4237 current_template_parms = TREE_CHAIN (current_template_parms);
4240 /* Takes a TREE_LIST representing a template parameter and convert it
4241 into an argument suitable to be passed to the type substitution
4242 functions. Note that If the TREE_LIST contains an error_mark
4243 node, the returned argument is error_mark_node. */
4245 tree
4246 template_parm_to_arg (tree t)
4249 if (t == NULL_TREE
4250 || TREE_CODE (t) != TREE_LIST)
4251 return t;
4253 if (error_operand_p (TREE_VALUE (t)))
4254 return error_mark_node;
4256 t = TREE_VALUE (t);
4258 if (TREE_CODE (t) == TYPE_DECL
4259 || TREE_CODE (t) == TEMPLATE_DECL)
4261 t = TREE_TYPE (t);
4263 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4265 /* Turn this argument into a TYPE_ARGUMENT_PACK
4266 with a single element, which expands T. */
4267 tree vec = make_tree_vec (1);
4268 if (CHECKING_P)
4269 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4271 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4273 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4274 SET_ARGUMENT_PACK_ARGS (t, vec);
4277 else
4279 t = DECL_INITIAL (t);
4281 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4283 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4284 with a single element, which expands T. */
4285 tree vec = make_tree_vec (1);
4286 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4287 if (CHECKING_P)
4288 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4290 t = convert_from_reference (t);
4291 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4293 t = make_node (NONTYPE_ARGUMENT_PACK);
4294 SET_ARGUMENT_PACK_ARGS (t, vec);
4295 TREE_TYPE (t) = type;
4297 else
4298 t = convert_from_reference (t);
4300 return t;
4303 /* Given a single level of template parameters (a TREE_VEC), return it
4304 as a set of template arguments. */
4306 static tree
4307 template_parms_level_to_args (tree parms)
4309 tree a = copy_node (parms);
4310 TREE_TYPE (a) = NULL_TREE;
4311 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4312 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4314 if (CHECKING_P)
4315 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4317 return a;
4320 /* Given a set of template parameters, return them as a set of template
4321 arguments. The template parameters are represented as a TREE_VEC, in
4322 the form documented in cp-tree.h for template arguments. */
4324 static tree
4325 template_parms_to_args (tree parms)
4327 tree header;
4328 tree args = NULL_TREE;
4329 int length = TMPL_PARMS_DEPTH (parms);
4330 int l = length;
4332 /* If there is only one level of template parameters, we do not
4333 create a TREE_VEC of TREE_VECs. Instead, we return a single
4334 TREE_VEC containing the arguments. */
4335 if (length > 1)
4336 args = make_tree_vec (length);
4338 for (header = parms; header; header = TREE_CHAIN (header))
4340 tree a = template_parms_level_to_args (TREE_VALUE (header));
4342 if (length > 1)
4343 TREE_VEC_ELT (args, --l) = a;
4344 else
4345 args = a;
4348 return args;
4351 /* Within the declaration of a template, return the currently active
4352 template parameters as an argument TREE_VEC. */
4354 static tree
4355 current_template_args (void)
4357 return template_parms_to_args (current_template_parms);
4360 /* Update the declared TYPE by doing any lookups which were thought to be
4361 dependent, but are not now that we know the SCOPE of the declarator. */
4363 tree
4364 maybe_update_decl_type (tree orig_type, tree scope)
4366 tree type = orig_type;
4368 if (type == NULL_TREE)
4369 return type;
4371 if (TREE_CODE (orig_type) == TYPE_DECL)
4372 type = TREE_TYPE (type);
4374 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4375 && dependent_type_p (type)
4376 /* Don't bother building up the args in this case. */
4377 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4379 /* tsubst in the args corresponding to the template parameters,
4380 including auto if present. Most things will be unchanged, but
4381 make_typename_type and tsubst_qualified_id will resolve
4382 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4383 tree args = current_template_args ();
4384 tree auto_node = type_uses_auto (type);
4385 tree pushed;
4386 if (auto_node)
4388 tree auto_vec = make_tree_vec (1);
4389 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4390 args = add_to_template_args (args, auto_vec);
4392 pushed = push_scope (scope);
4393 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4394 if (pushed)
4395 pop_scope (scope);
4398 if (type == error_mark_node)
4399 return orig_type;
4401 if (TREE_CODE (orig_type) == TYPE_DECL)
4403 if (same_type_p (type, TREE_TYPE (orig_type)))
4404 type = orig_type;
4405 else
4406 type = TYPE_NAME (type);
4408 return type;
4411 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4412 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4413 the new template is a member template. */
4415 tree
4416 build_template_decl (tree decl, tree parms, bool member_template_p)
4418 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4419 DECL_TEMPLATE_PARMS (tmpl) = parms;
4420 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4421 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4422 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4424 return tmpl;
4427 struct template_parm_data
4429 /* The level of the template parameters we are currently
4430 processing. */
4431 int level;
4433 /* The index of the specialization argument we are currently
4434 processing. */
4435 int current_arg;
4437 /* An array whose size is the number of template parameters. The
4438 elements are nonzero if the parameter has been used in any one
4439 of the arguments processed so far. */
4440 int* parms;
4442 /* An array whose size is the number of template arguments. The
4443 elements are nonzero if the argument makes use of template
4444 parameters of this level. */
4445 int* arg_uses_template_parms;
4448 /* Subroutine of push_template_decl used to see if each template
4449 parameter in a partial specialization is used in the explicit
4450 argument list. If T is of the LEVEL given in DATA (which is
4451 treated as a template_parm_data*), then DATA->PARMS is marked
4452 appropriately. */
4454 static int
4455 mark_template_parm (tree t, void* data)
4457 int level;
4458 int idx;
4459 struct template_parm_data* tpd = (struct template_parm_data*) data;
4461 template_parm_level_and_index (t, &level, &idx);
4463 if (level == tpd->level)
4465 tpd->parms[idx] = 1;
4466 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4469 /* Return zero so that for_each_template_parm will continue the
4470 traversal of the tree; we want to mark *every* template parm. */
4471 return 0;
4474 /* Process the partial specialization DECL. */
4476 static tree
4477 process_partial_specialization (tree decl)
4479 tree type = TREE_TYPE (decl);
4480 tree tinfo = get_template_info (decl);
4481 tree maintmpl = TI_TEMPLATE (tinfo);
4482 tree specargs = TI_ARGS (tinfo);
4483 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4484 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4485 tree inner_parms;
4486 tree inst;
4487 int nargs = TREE_VEC_LENGTH (inner_args);
4488 int ntparms;
4489 int i;
4490 bool did_error_intro = false;
4491 struct template_parm_data tpd;
4492 struct template_parm_data tpd2;
4494 gcc_assert (current_template_parms);
4496 /* A concept cannot be specialized. */
4497 if (flag_concepts && variable_concept_p (maintmpl))
4499 error ("specialization of variable concept %q#D", maintmpl);
4500 return error_mark_node;
4503 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4504 ntparms = TREE_VEC_LENGTH (inner_parms);
4506 /* We check that each of the template parameters given in the
4507 partial specialization is used in the argument list to the
4508 specialization. For example:
4510 template <class T> struct S;
4511 template <class T> struct S<T*>;
4513 The second declaration is OK because `T*' uses the template
4514 parameter T, whereas
4516 template <class T> struct S<int>;
4518 is no good. Even trickier is:
4520 template <class T>
4521 struct S1
4523 template <class U>
4524 struct S2;
4525 template <class U>
4526 struct S2<T>;
4529 The S2<T> declaration is actually invalid; it is a
4530 full-specialization. Of course,
4532 template <class U>
4533 struct S2<T (*)(U)>;
4535 or some such would have been OK. */
4536 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4537 tpd.parms = XALLOCAVEC (int, ntparms);
4538 memset (tpd.parms, 0, sizeof (int) * ntparms);
4540 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4541 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4542 for (i = 0; i < nargs; ++i)
4544 tpd.current_arg = i;
4545 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4546 &mark_template_parm,
4547 &tpd,
4548 NULL,
4549 /*include_nondeduced_p=*/false);
4551 for (i = 0; i < ntparms; ++i)
4552 if (tpd.parms[i] == 0)
4554 /* One of the template parms was not used in a deduced context in the
4555 specialization. */
4556 if (!did_error_intro)
4558 error ("template parameters not deducible in "
4559 "partial specialization:");
4560 did_error_intro = true;
4563 inform (input_location, " %qD",
4564 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4567 if (did_error_intro)
4568 return error_mark_node;
4570 /* [temp.class.spec]
4572 The argument list of the specialization shall not be identical to
4573 the implicit argument list of the primary template. */
4574 tree main_args
4575 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4576 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4577 && (!flag_concepts
4578 || !strictly_subsumes (current_template_constraints (),
4579 get_constraints (maintmpl))))
4581 if (!flag_concepts)
4582 error ("partial specialization %q+D does not specialize "
4583 "any template arguments", decl);
4584 else
4585 error ("partial specialization %q+D does not specialize any "
4586 "template arguments and is not more constrained than", decl);
4587 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4590 /* A partial specialization that replaces multiple parameters of the
4591 primary template with a pack expansion is less specialized for those
4592 parameters. */
4593 if (nargs < DECL_NTPARMS (maintmpl))
4595 error ("partial specialization is not more specialized than the "
4596 "primary template because it replaces multiple parameters "
4597 "with a pack expansion");
4598 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4599 return decl;
4602 /* [temp.class.spec]
4604 A partially specialized non-type argument expression shall not
4605 involve template parameters of the partial specialization except
4606 when the argument expression is a simple identifier.
4608 The type of a template parameter corresponding to a specialized
4609 non-type argument shall not be dependent on a parameter of the
4610 specialization.
4612 Also, we verify that pack expansions only occur at the
4613 end of the argument list. */
4614 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4615 tpd2.parms = 0;
4616 for (i = 0; i < nargs; ++i)
4618 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4619 tree arg = TREE_VEC_ELT (inner_args, i);
4620 tree packed_args = NULL_TREE;
4621 int j, len = 1;
4623 if (ARGUMENT_PACK_P (arg))
4625 /* Extract the arguments from the argument pack. We'll be
4626 iterating over these in the following loop. */
4627 packed_args = ARGUMENT_PACK_ARGS (arg);
4628 len = TREE_VEC_LENGTH (packed_args);
4631 for (j = 0; j < len; j++)
4633 if (packed_args)
4634 /* Get the Jth argument in the parameter pack. */
4635 arg = TREE_VEC_ELT (packed_args, j);
4637 if (PACK_EXPANSION_P (arg))
4639 /* Pack expansions must come at the end of the
4640 argument list. */
4641 if ((packed_args && j < len - 1)
4642 || (!packed_args && i < nargs - 1))
4644 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4645 error ("parameter pack argument %qE must be at the "
4646 "end of the template argument list", arg);
4647 else
4648 error ("parameter pack argument %qT must be at the "
4649 "end of the template argument list", arg);
4653 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4654 /* We only care about the pattern. */
4655 arg = PACK_EXPANSION_PATTERN (arg);
4657 if (/* These first two lines are the `non-type' bit. */
4658 !TYPE_P (arg)
4659 && TREE_CODE (arg) != TEMPLATE_DECL
4660 /* This next two lines are the `argument expression is not just a
4661 simple identifier' condition and also the `specialized
4662 non-type argument' bit. */
4663 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4664 && !(REFERENCE_REF_P (arg)
4665 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4667 if ((!packed_args && tpd.arg_uses_template_parms[i])
4668 || (packed_args && uses_template_parms (arg)))
4669 error ("template argument %qE involves template parameter(s)",
4670 arg);
4671 else
4673 /* Look at the corresponding template parameter,
4674 marking which template parameters its type depends
4675 upon. */
4676 tree type = TREE_TYPE (parm);
4678 if (!tpd2.parms)
4680 /* We haven't yet initialized TPD2. Do so now. */
4681 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4682 /* The number of parameters here is the number in the
4683 main template, which, as checked in the assertion
4684 above, is NARGS. */
4685 tpd2.parms = XALLOCAVEC (int, nargs);
4686 tpd2.level =
4687 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4690 /* Mark the template parameters. But this time, we're
4691 looking for the template parameters of the main
4692 template, not in the specialization. */
4693 tpd2.current_arg = i;
4694 tpd2.arg_uses_template_parms[i] = 0;
4695 memset (tpd2.parms, 0, sizeof (int) * nargs);
4696 for_each_template_parm (type,
4697 &mark_template_parm,
4698 &tpd2,
4699 NULL,
4700 /*include_nondeduced_p=*/false);
4702 if (tpd2.arg_uses_template_parms [i])
4704 /* The type depended on some template parameters.
4705 If they are fully specialized in the
4706 specialization, that's OK. */
4707 int j;
4708 int count = 0;
4709 for (j = 0; j < nargs; ++j)
4710 if (tpd2.parms[j] != 0
4711 && tpd.arg_uses_template_parms [j])
4712 ++count;
4713 if (count != 0)
4714 error_n (input_location, count,
4715 "type %qT of template argument %qE depends "
4716 "on a template parameter",
4717 "type %qT of template argument %qE depends "
4718 "on template parameters",
4719 type,
4720 arg);
4727 /* We should only get here once. */
4728 if (TREE_CODE (decl) == TYPE_DECL)
4729 gcc_assert (!COMPLETE_TYPE_P (type));
4731 // Build the template decl.
4732 tree tmpl = build_template_decl (decl, current_template_parms,
4733 DECL_MEMBER_TEMPLATE_P (maintmpl));
4734 TREE_TYPE (tmpl) = type;
4735 DECL_TEMPLATE_RESULT (tmpl) = decl;
4736 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4737 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4738 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4740 if (VAR_P (decl))
4741 /* We didn't register this in check_explicit_specialization so we could
4742 wait until the constraints were set. */
4743 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4744 else
4745 associate_classtype_constraints (type);
4747 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4748 = tree_cons (specargs, tmpl,
4749 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4750 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4752 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4753 inst = TREE_CHAIN (inst))
4755 tree instance = TREE_VALUE (inst);
4756 if (TYPE_P (instance)
4757 ? (COMPLETE_TYPE_P (instance)
4758 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4759 : DECL_TEMPLATE_INSTANTIATION (instance))
4761 tree spec = most_specialized_partial_spec (instance, tf_none);
4762 tree inst_decl = (DECL_P (instance)
4763 ? instance : TYPE_NAME (instance));
4764 if (!spec)
4765 /* OK */;
4766 else if (spec == error_mark_node)
4767 permerror (input_location,
4768 "declaration of %qD ambiguates earlier template "
4769 "instantiation for %qD", decl, inst_decl);
4770 else if (TREE_VALUE (spec) == tmpl)
4771 permerror (input_location,
4772 "partial specialization of %qD after instantiation "
4773 "of %qD", decl, inst_decl);
4777 return decl;
4780 /* PARM is a template parameter of some form; return the corresponding
4781 TEMPLATE_PARM_INDEX. */
4783 static tree
4784 get_template_parm_index (tree parm)
4786 if (TREE_CODE (parm) == PARM_DECL
4787 || TREE_CODE (parm) == CONST_DECL)
4788 parm = DECL_INITIAL (parm);
4789 else if (TREE_CODE (parm) == TYPE_DECL
4790 || TREE_CODE (parm) == TEMPLATE_DECL)
4791 parm = TREE_TYPE (parm);
4792 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4793 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4794 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4795 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4796 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4797 return parm;
4800 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4801 parameter packs used by the template parameter PARM. */
4803 static void
4804 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4806 /* A type parm can't refer to another parm. */
4807 if (TREE_CODE (parm) == TYPE_DECL)
4808 return;
4809 else if (TREE_CODE (parm) == PARM_DECL)
4811 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4812 ppd, ppd->visited);
4813 return;
4816 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4818 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4819 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4820 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4823 /* PARM is a template parameter pack. Return any parameter packs used in
4824 its type or the type of any of its template parameters. If there are
4825 any such packs, it will be instantiated into a fixed template parameter
4826 list by partial instantiation rather than be fully deduced. */
4828 tree
4829 fixed_parameter_pack_p (tree parm)
4831 /* This can only be true in a member template. */
4832 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4833 return NULL_TREE;
4834 /* This can only be true for a parameter pack. */
4835 if (!template_parameter_pack_p (parm))
4836 return NULL_TREE;
4837 /* A type parm can't refer to another parm. */
4838 if (TREE_CODE (parm) == TYPE_DECL)
4839 return NULL_TREE;
4841 tree parameter_packs = NULL_TREE;
4842 struct find_parameter_pack_data ppd;
4843 ppd.parameter_packs = &parameter_packs;
4844 ppd.visited = new hash_set<tree>;
4845 ppd.type_pack_expansion_p = false;
4847 fixed_parameter_pack_p_1 (parm, &ppd);
4849 delete ppd.visited;
4850 return parameter_packs;
4853 /* Check that a template declaration's use of default arguments and
4854 parameter packs is not invalid. Here, PARMS are the template
4855 parameters. IS_PRIMARY is true if DECL is the thing declared by
4856 a primary template. IS_PARTIAL is true if DECL is a partial
4857 specialization.
4859 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4860 declaration (but not a definition); 1 indicates a declaration, 2
4861 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4862 emitted for extraneous default arguments.
4864 Returns TRUE if there were no errors found, FALSE otherwise. */
4866 bool
4867 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4868 bool is_partial, int is_friend_decl)
4870 const char *msg;
4871 int last_level_to_check;
4872 tree parm_level;
4873 bool no_errors = true;
4875 /* [temp.param]
4877 A default template-argument shall not be specified in a
4878 function template declaration or a function template definition, nor
4879 in the template-parameter-list of the definition of a member of a
4880 class template. */
4882 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4883 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4884 /* You can't have a function template declaration in a local
4885 scope, nor you can you define a member of a class template in a
4886 local scope. */
4887 return true;
4889 if ((TREE_CODE (decl) == TYPE_DECL
4890 && TREE_TYPE (decl)
4891 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4892 || (TREE_CODE (decl) == FUNCTION_DECL
4893 && LAMBDA_FUNCTION_P (decl)))
4894 /* A lambda doesn't have an explicit declaration; don't complain
4895 about the parms of the enclosing class. */
4896 return true;
4898 if (current_class_type
4899 && !TYPE_BEING_DEFINED (current_class_type)
4900 && DECL_LANG_SPECIFIC (decl)
4901 && DECL_DECLARES_FUNCTION_P (decl)
4902 /* If this is either a friend defined in the scope of the class
4903 or a member function. */
4904 && (DECL_FUNCTION_MEMBER_P (decl)
4905 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4906 : DECL_FRIEND_CONTEXT (decl)
4907 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4908 : false)
4909 /* And, if it was a member function, it really was defined in
4910 the scope of the class. */
4911 && (!DECL_FUNCTION_MEMBER_P (decl)
4912 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4913 /* We already checked these parameters when the template was
4914 declared, so there's no need to do it again now. This function
4915 was defined in class scope, but we're processing its body now
4916 that the class is complete. */
4917 return true;
4919 /* Core issue 226 (C++0x only): the following only applies to class
4920 templates. */
4921 if (is_primary
4922 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4924 /* [temp.param]
4926 If a template-parameter has a default template-argument, all
4927 subsequent template-parameters shall have a default
4928 template-argument supplied. */
4929 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4931 tree inner_parms = TREE_VALUE (parm_level);
4932 int ntparms = TREE_VEC_LENGTH (inner_parms);
4933 int seen_def_arg_p = 0;
4934 int i;
4936 for (i = 0; i < ntparms; ++i)
4938 tree parm = TREE_VEC_ELT (inner_parms, i);
4940 if (parm == error_mark_node)
4941 continue;
4943 if (TREE_PURPOSE (parm))
4944 seen_def_arg_p = 1;
4945 else if (seen_def_arg_p
4946 && !template_parameter_pack_p (TREE_VALUE (parm)))
4948 error ("no default argument for %qD", TREE_VALUE (parm));
4949 /* For better subsequent error-recovery, we indicate that
4950 there should have been a default argument. */
4951 TREE_PURPOSE (parm) = error_mark_node;
4952 no_errors = false;
4954 else if (!is_partial
4955 && !is_friend_decl
4956 /* Don't complain about an enclosing partial
4957 specialization. */
4958 && parm_level == parms
4959 && TREE_CODE (decl) == TYPE_DECL
4960 && i < ntparms - 1
4961 && template_parameter_pack_p (TREE_VALUE (parm))
4962 /* A fixed parameter pack will be partially
4963 instantiated into a fixed length list. */
4964 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4966 /* A primary class template can only have one
4967 parameter pack, at the end of the template
4968 parameter list. */
4970 error ("parameter pack %q+D must be at the end of the"
4971 " template parameter list", TREE_VALUE (parm));
4973 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4974 = error_mark_node;
4975 no_errors = false;
4981 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4982 || is_partial
4983 || !is_primary
4984 || is_friend_decl)
4985 /* For an ordinary class template, default template arguments are
4986 allowed at the innermost level, e.g.:
4987 template <class T = int>
4988 struct S {};
4989 but, in a partial specialization, they're not allowed even
4990 there, as we have in [temp.class.spec]:
4992 The template parameter list of a specialization shall not
4993 contain default template argument values.
4995 So, for a partial specialization, or for a function template
4996 (in C++98/C++03), we look at all of them. */
4998 else
4999 /* But, for a primary class template that is not a partial
5000 specialization we look at all template parameters except the
5001 innermost ones. */
5002 parms = TREE_CHAIN (parms);
5004 /* Figure out what error message to issue. */
5005 if (is_friend_decl == 2)
5006 msg = G_("default template arguments may not be used in function template "
5007 "friend re-declaration");
5008 else if (is_friend_decl)
5009 msg = G_("default template arguments may not be used in function template "
5010 "friend declarations");
5011 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5012 msg = G_("default template arguments may not be used in function templates "
5013 "without -std=c++11 or -std=gnu++11");
5014 else if (is_partial)
5015 msg = G_("default template arguments may not be used in "
5016 "partial specializations");
5017 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5018 msg = G_("default argument for template parameter for class enclosing %qD");
5019 else
5020 /* Per [temp.param]/9, "A default template-argument shall not be
5021 specified in the template-parameter-lists of the definition of
5022 a member of a class template that appears outside of the member's
5023 class.", thus if we aren't handling a member of a class template
5024 there is no need to examine the parameters. */
5025 return true;
5027 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5028 /* If we're inside a class definition, there's no need to
5029 examine the parameters to the class itself. On the one
5030 hand, they will be checked when the class is defined, and,
5031 on the other, default arguments are valid in things like:
5032 template <class T = double>
5033 struct S { template <class U> void f(U); };
5034 Here the default argument for `S' has no bearing on the
5035 declaration of `f'. */
5036 last_level_to_check = template_class_depth (current_class_type) + 1;
5037 else
5038 /* Check everything. */
5039 last_level_to_check = 0;
5041 for (parm_level = parms;
5042 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5043 parm_level = TREE_CHAIN (parm_level))
5045 tree inner_parms = TREE_VALUE (parm_level);
5046 int i;
5047 int ntparms;
5049 ntparms = TREE_VEC_LENGTH (inner_parms);
5050 for (i = 0; i < ntparms; ++i)
5052 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5053 continue;
5055 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5057 if (msg)
5059 no_errors = false;
5060 if (is_friend_decl == 2)
5061 return no_errors;
5063 error (msg, decl);
5064 msg = 0;
5067 /* Clear out the default argument so that we are not
5068 confused later. */
5069 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5073 /* At this point, if we're still interested in issuing messages,
5074 they must apply to classes surrounding the object declared. */
5075 if (msg)
5076 msg = G_("default argument for template parameter for class "
5077 "enclosing %qD");
5080 return no_errors;
5083 /* Worker for push_template_decl_real, called via
5084 for_each_template_parm. DATA is really an int, indicating the
5085 level of the parameters we are interested in. If T is a template
5086 parameter of that level, return nonzero. */
5088 static int
5089 template_parm_this_level_p (tree t, void* data)
5091 int this_level = *(int *)data;
5092 int level;
5094 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5095 level = TEMPLATE_PARM_LEVEL (t);
5096 else
5097 level = TEMPLATE_TYPE_LEVEL (t);
5098 return level == this_level;
5101 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5102 DATA is really an int, indicating the innermost outer level of parameters.
5103 If T is a template parameter of that level or further out, return
5104 nonzero. */
5106 static int
5107 template_parm_outer_level (tree t, void *data)
5109 int this_level = *(int *)data;
5110 int level;
5112 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5113 level = TEMPLATE_PARM_LEVEL (t);
5114 else
5115 level = TEMPLATE_TYPE_LEVEL (t);
5116 return level <= this_level;
5119 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5120 parameters given by current_template_args, or reuses a
5121 previously existing one, if appropriate. Returns the DECL, or an
5122 equivalent one, if it is replaced via a call to duplicate_decls.
5124 If IS_FRIEND is true, DECL is a friend declaration. */
5126 tree
5127 push_template_decl_real (tree decl, bool is_friend)
5129 tree tmpl;
5130 tree args;
5131 tree info;
5132 tree ctx;
5133 bool is_primary;
5134 bool is_partial;
5135 int new_template_p = 0;
5136 /* True if the template is a member template, in the sense of
5137 [temp.mem]. */
5138 bool member_template_p = false;
5140 if (decl == error_mark_node || !current_template_parms)
5141 return error_mark_node;
5143 /* See if this is a partial specialization. */
5144 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5145 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5146 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5147 || (VAR_P (decl)
5148 && DECL_LANG_SPECIFIC (decl)
5149 && DECL_TEMPLATE_SPECIALIZATION (decl)
5150 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5152 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5153 is_friend = true;
5155 if (is_friend)
5156 /* For a friend, we want the context of the friend function, not
5157 the type of which it is a friend. */
5158 ctx = CP_DECL_CONTEXT (decl);
5159 else if (CP_DECL_CONTEXT (decl)
5160 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5161 /* In the case of a virtual function, we want the class in which
5162 it is defined. */
5163 ctx = CP_DECL_CONTEXT (decl);
5164 else
5165 /* Otherwise, if we're currently defining some class, the DECL
5166 is assumed to be a member of the class. */
5167 ctx = current_scope ();
5169 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5170 ctx = NULL_TREE;
5172 if (!DECL_CONTEXT (decl))
5173 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5175 /* See if this is a primary template. */
5176 if (is_friend && ctx
5177 && uses_template_parms_level (ctx, processing_template_decl))
5178 /* A friend template that specifies a class context, i.e.
5179 template <typename T> friend void A<T>::f();
5180 is not primary. */
5181 is_primary = false;
5182 else if (TREE_CODE (decl) == TYPE_DECL
5183 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5184 is_primary = false;
5185 else
5186 is_primary = template_parm_scope_p ();
5188 if (is_primary)
5190 warning (OPT_Wtemplates, "template %qD declared", decl);
5192 if (DECL_CLASS_SCOPE_P (decl))
5193 member_template_p = true;
5194 if (TREE_CODE (decl) == TYPE_DECL
5195 && anon_aggrname_p (DECL_NAME (decl)))
5197 error ("template class without a name");
5198 return error_mark_node;
5200 else if (TREE_CODE (decl) == FUNCTION_DECL)
5202 if (member_template_p)
5204 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5205 error ("member template %qD may not have virt-specifiers", decl);
5207 if (DECL_DESTRUCTOR_P (decl))
5209 /* [temp.mem]
5211 A destructor shall not be a member template. */
5212 error ("destructor %qD declared as member template", decl);
5213 return error_mark_node;
5215 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5216 && (!prototype_p (TREE_TYPE (decl))
5217 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5218 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5219 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5220 == void_list_node)))
5222 /* [basic.stc.dynamic.allocation]
5224 An allocation function can be a function
5225 template. ... Template allocation functions shall
5226 have two or more parameters. */
5227 error ("invalid template declaration of %qD", decl);
5228 return error_mark_node;
5231 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5232 && CLASS_TYPE_P (TREE_TYPE (decl)))
5233 /* OK */;
5234 else if (TREE_CODE (decl) == TYPE_DECL
5235 && TYPE_DECL_ALIAS_P (decl))
5236 /* alias-declaration */
5237 gcc_assert (!DECL_ARTIFICIAL (decl));
5238 else if (VAR_P (decl))
5239 /* C++14 variable template. */;
5240 else
5242 error ("template declaration of %q#D", decl);
5243 return error_mark_node;
5247 /* Check to see that the rules regarding the use of default
5248 arguments are not being violated. */
5249 check_default_tmpl_args (decl, current_template_parms,
5250 is_primary, is_partial, /*is_friend_decl=*/0);
5252 /* Ensure that there are no parameter packs in the type of this
5253 declaration that have not been expanded. */
5254 if (TREE_CODE (decl) == FUNCTION_DECL)
5256 /* Check each of the arguments individually to see if there are
5257 any bare parameter packs. */
5258 tree type = TREE_TYPE (decl);
5259 tree arg = DECL_ARGUMENTS (decl);
5260 tree argtype = TYPE_ARG_TYPES (type);
5262 while (arg && argtype)
5264 if (!DECL_PACK_P (arg)
5265 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5267 /* This is a PARM_DECL that contains unexpanded parameter
5268 packs. We have already complained about this in the
5269 check_for_bare_parameter_packs call, so just replace
5270 these types with ERROR_MARK_NODE. */
5271 TREE_TYPE (arg) = error_mark_node;
5272 TREE_VALUE (argtype) = error_mark_node;
5275 arg = DECL_CHAIN (arg);
5276 argtype = TREE_CHAIN (argtype);
5279 /* Check for bare parameter packs in the return type and the
5280 exception specifiers. */
5281 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5282 /* Errors were already issued, set return type to int
5283 as the frontend doesn't expect error_mark_node as
5284 the return type. */
5285 TREE_TYPE (type) = integer_type_node;
5286 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5287 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5289 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5290 && TYPE_DECL_ALIAS_P (decl))
5291 ? DECL_ORIGINAL_TYPE (decl)
5292 : TREE_TYPE (decl)))
5294 TREE_TYPE (decl) = error_mark_node;
5295 return error_mark_node;
5298 if (is_partial)
5299 return process_partial_specialization (decl);
5301 args = current_template_args ();
5303 if (!ctx
5304 || TREE_CODE (ctx) == FUNCTION_DECL
5305 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5306 || (TREE_CODE (decl) == TYPE_DECL
5307 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5308 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5310 if (DECL_LANG_SPECIFIC (decl)
5311 && DECL_TEMPLATE_INFO (decl)
5312 && DECL_TI_TEMPLATE (decl))
5313 tmpl = DECL_TI_TEMPLATE (decl);
5314 /* If DECL is a TYPE_DECL for a class-template, then there won't
5315 be DECL_LANG_SPECIFIC. The information equivalent to
5316 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5317 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5318 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5319 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5321 /* Since a template declaration already existed for this
5322 class-type, we must be redeclaring it here. Make sure
5323 that the redeclaration is valid. */
5324 redeclare_class_template (TREE_TYPE (decl),
5325 current_template_parms,
5326 current_template_constraints ());
5327 /* We don't need to create a new TEMPLATE_DECL; just use the
5328 one we already had. */
5329 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5331 else
5333 tmpl = build_template_decl (decl, current_template_parms,
5334 member_template_p);
5335 new_template_p = 1;
5337 if (DECL_LANG_SPECIFIC (decl)
5338 && DECL_TEMPLATE_SPECIALIZATION (decl))
5340 /* A specialization of a member template of a template
5341 class. */
5342 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5343 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5344 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5348 else
5350 tree a, t, current, parms;
5351 int i;
5352 tree tinfo = get_template_info (decl);
5354 if (!tinfo)
5356 error ("template definition of non-template %q#D", decl);
5357 return error_mark_node;
5360 tmpl = TI_TEMPLATE (tinfo);
5362 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5363 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5364 && DECL_TEMPLATE_SPECIALIZATION (decl)
5365 && DECL_MEMBER_TEMPLATE_P (tmpl))
5367 tree new_tmpl;
5369 /* The declaration is a specialization of a member
5370 template, declared outside the class. Therefore, the
5371 innermost template arguments will be NULL, so we
5372 replace them with the arguments determined by the
5373 earlier call to check_explicit_specialization. */
5374 args = DECL_TI_ARGS (decl);
5376 new_tmpl
5377 = build_template_decl (decl, current_template_parms,
5378 member_template_p);
5379 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5380 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5381 DECL_TI_TEMPLATE (decl) = new_tmpl;
5382 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5383 DECL_TEMPLATE_INFO (new_tmpl)
5384 = build_template_info (tmpl, args);
5386 register_specialization (new_tmpl,
5387 most_general_template (tmpl),
5388 args,
5389 is_friend, 0);
5390 return decl;
5393 /* Make sure the template headers we got make sense. */
5395 parms = DECL_TEMPLATE_PARMS (tmpl);
5396 i = TMPL_PARMS_DEPTH (parms);
5397 if (TMPL_ARGS_DEPTH (args) != i)
5399 error ("expected %d levels of template parms for %q#D, got %d",
5400 i, decl, TMPL_ARGS_DEPTH (args));
5401 DECL_INTERFACE_KNOWN (decl) = 1;
5402 return error_mark_node;
5404 else
5405 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5407 a = TMPL_ARGS_LEVEL (args, i);
5408 t = INNERMOST_TEMPLATE_PARMS (parms);
5410 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5412 if (current == decl)
5413 error ("got %d template parameters for %q#D",
5414 TREE_VEC_LENGTH (a), decl);
5415 else
5416 error ("got %d template parameters for %q#T",
5417 TREE_VEC_LENGTH (a), current);
5418 error (" but %d required", TREE_VEC_LENGTH (t));
5419 /* Avoid crash in import_export_decl. */
5420 DECL_INTERFACE_KNOWN (decl) = 1;
5421 return error_mark_node;
5424 if (current == decl)
5425 current = ctx;
5426 else if (current == NULL_TREE)
5427 /* Can happen in erroneous input. */
5428 break;
5429 else
5430 current = get_containing_scope (current);
5433 /* Check that the parms are used in the appropriate qualifying scopes
5434 in the declarator. */
5435 if (!comp_template_args
5436 (TI_ARGS (tinfo),
5437 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5439 error ("\
5440 template arguments to %qD do not match original template %qD",
5441 decl, DECL_TEMPLATE_RESULT (tmpl));
5442 if (!uses_template_parms (TI_ARGS (tinfo)))
5443 inform (input_location, "use template<> for an explicit specialization");
5444 /* Avoid crash in import_export_decl. */
5445 DECL_INTERFACE_KNOWN (decl) = 1;
5446 return error_mark_node;
5450 DECL_TEMPLATE_RESULT (tmpl) = decl;
5451 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5453 /* Push template declarations for global functions and types. Note
5454 that we do not try to push a global template friend declared in a
5455 template class; such a thing may well depend on the template
5456 parameters of the class. */
5457 if (new_template_p && !ctx
5458 && !(is_friend && template_class_depth (current_class_type) > 0))
5460 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5461 if (tmpl == error_mark_node)
5462 return error_mark_node;
5464 /* Hide template friend classes that haven't been declared yet. */
5465 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5467 DECL_ANTICIPATED (tmpl) = 1;
5468 DECL_FRIEND_P (tmpl) = 1;
5472 if (is_primary)
5474 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5475 int i;
5477 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5478 if (DECL_CONV_FN_P (tmpl))
5480 int depth = TMPL_PARMS_DEPTH (parms);
5482 /* It is a conversion operator. See if the type converted to
5483 depends on innermost template operands. */
5485 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5486 depth))
5487 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5490 /* Give template template parms a DECL_CONTEXT of the template
5491 for which they are a parameter. */
5492 parms = INNERMOST_TEMPLATE_PARMS (parms);
5493 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5495 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5496 if (TREE_CODE (parm) == TEMPLATE_DECL)
5497 DECL_CONTEXT (parm) = tmpl;
5500 if (TREE_CODE (decl) == TYPE_DECL
5501 && TYPE_DECL_ALIAS_P (decl)
5502 && complex_alias_template_p (tmpl))
5503 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5506 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5507 back to its most general template. If TMPL is a specialization,
5508 ARGS may only have the innermost set of arguments. Add the missing
5509 argument levels if necessary. */
5510 if (DECL_TEMPLATE_INFO (tmpl))
5511 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5513 info = build_template_info (tmpl, args);
5515 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5516 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5517 else
5519 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5520 retrofit_lang_decl (decl);
5521 if (DECL_LANG_SPECIFIC (decl))
5522 DECL_TEMPLATE_INFO (decl) = info;
5525 if (flag_implicit_templates
5526 && !is_friend
5527 && TREE_PUBLIC (decl)
5528 && VAR_OR_FUNCTION_DECL_P (decl))
5529 /* Set DECL_COMDAT on template instantiations; if we force
5530 them to be emitted by explicit instantiation or -frepo,
5531 mark_needed will tell cgraph to do the right thing. */
5532 DECL_COMDAT (decl) = true;
5534 return DECL_TEMPLATE_RESULT (tmpl);
5537 tree
5538 push_template_decl (tree decl)
5540 return push_template_decl_real (decl, false);
5543 /* FN is an inheriting constructor that inherits from the constructor
5544 template INHERITED; turn FN into a constructor template with a matching
5545 template header. */
5547 tree
5548 add_inherited_template_parms (tree fn, tree inherited)
5550 tree inner_parms
5551 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5552 inner_parms = copy_node (inner_parms);
5553 tree parms
5554 = tree_cons (size_int (processing_template_decl + 1),
5555 inner_parms, current_template_parms);
5556 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5557 tree args = template_parms_to_args (parms);
5558 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5559 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5560 DECL_TEMPLATE_RESULT (tmpl) = fn;
5561 DECL_ARTIFICIAL (tmpl) = true;
5562 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5563 return tmpl;
5566 /* Called when a class template TYPE is redeclared with the indicated
5567 template PARMS, e.g.:
5569 template <class T> struct S;
5570 template <class T> struct S {}; */
5572 bool
5573 redeclare_class_template (tree type, tree parms, tree cons)
5575 tree tmpl;
5576 tree tmpl_parms;
5577 int i;
5579 if (!TYPE_TEMPLATE_INFO (type))
5581 error ("%qT is not a template type", type);
5582 return false;
5585 tmpl = TYPE_TI_TEMPLATE (type);
5586 if (!PRIMARY_TEMPLATE_P (tmpl))
5587 /* The type is nested in some template class. Nothing to worry
5588 about here; there are no new template parameters for the nested
5589 type. */
5590 return true;
5592 if (!parms)
5594 error ("template specifiers not specified in declaration of %qD",
5595 tmpl);
5596 return false;
5599 parms = INNERMOST_TEMPLATE_PARMS (parms);
5600 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5602 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5604 error_n (input_location, TREE_VEC_LENGTH (parms),
5605 "redeclared with %d template parameter",
5606 "redeclared with %d template parameters",
5607 TREE_VEC_LENGTH (parms));
5608 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5609 "previous declaration %qD used %d template parameter",
5610 "previous declaration %qD used %d template parameters",
5611 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5612 return false;
5615 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5617 tree tmpl_parm;
5618 tree parm;
5619 tree tmpl_default;
5620 tree parm_default;
5622 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5623 || TREE_VEC_ELT (parms, i) == error_mark_node)
5624 continue;
5626 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5627 if (error_operand_p (tmpl_parm))
5628 return false;
5630 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5631 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5632 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5634 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5635 TEMPLATE_DECL. */
5636 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5637 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5638 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5639 || (TREE_CODE (tmpl_parm) != PARM_DECL
5640 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5641 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5642 || (TREE_CODE (tmpl_parm) == PARM_DECL
5643 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5644 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5646 error ("template parameter %q+#D", tmpl_parm);
5647 error ("redeclared here as %q#D", parm);
5648 return false;
5651 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5653 /* We have in [temp.param]:
5655 A template-parameter may not be given default arguments
5656 by two different declarations in the same scope. */
5657 error_at (input_location, "redefinition of default argument for %q#D", parm);
5658 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5659 "original definition appeared here");
5660 return false;
5663 if (parm_default != NULL_TREE)
5664 /* Update the previous template parameters (which are the ones
5665 that will really count) with the new default value. */
5666 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5667 else if (tmpl_default != NULL_TREE)
5668 /* Update the new parameters, too; they'll be used as the
5669 parameters for any members. */
5670 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5672 /* Give each template template parm in this redeclaration a
5673 DECL_CONTEXT of the template for which they are a parameter. */
5674 if (TREE_CODE (parm) == TEMPLATE_DECL)
5676 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5677 DECL_CONTEXT (parm) = tmpl;
5681 // Cannot redeclare a class template with a different set of constraints.
5682 if (!equivalent_constraints (get_constraints (tmpl), cons))
5684 error_at (input_location, "redeclaration %q#D with different "
5685 "constraints", tmpl);
5686 inform (DECL_SOURCE_LOCATION (tmpl),
5687 "original declaration appeared here");
5690 return true;
5693 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5694 to be used when the caller has already checked
5695 (processing_template_decl
5696 && !instantiation_dependent_expression_p (expr)
5697 && potential_constant_expression (expr))
5698 and cleared processing_template_decl. */
5700 tree
5701 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5703 return tsubst_copy_and_build (expr,
5704 /*args=*/NULL_TREE,
5705 complain,
5706 /*in_decl=*/NULL_TREE,
5707 /*function_p=*/false,
5708 /*integral_constant_expression_p=*/true);
5711 /* Simplify EXPR if it is a non-dependent expression. Returns the
5712 (possibly simplified) expression. */
5714 tree
5715 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5717 if (expr == NULL_TREE)
5718 return NULL_TREE;
5720 /* If we're in a template, but EXPR isn't value dependent, simplify
5721 it. We're supposed to treat:
5723 template <typename T> void f(T[1 + 1]);
5724 template <typename T> void f(T[2]);
5726 as two declarations of the same function, for example. */
5727 if (processing_template_decl
5728 && potential_nondependent_constant_expression (expr))
5730 processing_template_decl_sentinel s;
5731 expr = instantiate_non_dependent_expr_internal (expr, complain);
5733 return expr;
5736 tree
5737 instantiate_non_dependent_expr (tree expr)
5739 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5742 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5743 an uninstantiated expression. */
5745 tree
5746 instantiate_non_dependent_or_null (tree expr)
5748 if (expr == NULL_TREE)
5749 return NULL_TREE;
5750 if (processing_template_decl)
5752 if (!potential_nondependent_constant_expression (expr))
5753 expr = NULL_TREE;
5754 else
5756 processing_template_decl_sentinel s;
5757 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5760 return expr;
5763 /* True iff T is a specialization of a variable template. */
5765 bool
5766 variable_template_specialization_p (tree t)
5768 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5769 return false;
5770 tree tmpl = DECL_TI_TEMPLATE (t);
5771 return variable_template_p (tmpl);
5774 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5775 template declaration, or a TYPE_DECL for an alias declaration. */
5777 bool
5778 alias_type_or_template_p (tree t)
5780 if (t == NULL_TREE)
5781 return false;
5782 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5783 || (TYPE_P (t)
5784 && TYPE_NAME (t)
5785 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5786 || DECL_ALIAS_TEMPLATE_P (t));
5789 /* Return TRUE iff T is a specialization of an alias template. */
5791 bool
5792 alias_template_specialization_p (const_tree t)
5794 /* It's an alias template specialization if it's an alias and its
5795 TYPE_NAME is a specialization of a primary template. */
5796 if (TYPE_ALIAS_P (t))
5798 tree name = TYPE_NAME (t);
5799 if (DECL_LANG_SPECIFIC (name))
5800 if (tree ti = DECL_TEMPLATE_INFO (name))
5802 tree tmpl = TI_TEMPLATE (ti);
5803 return PRIMARY_TEMPLATE_P (tmpl);
5806 return false;
5809 /* An alias template is complex from a SFINAE perspective if a template-id
5810 using that alias can be ill-formed when the expansion is not, as with
5811 the void_t template. We determine this by checking whether the
5812 expansion for the alias template uses all its template parameters. */
5814 struct uses_all_template_parms_data
5816 int level;
5817 bool *seen;
5820 static int
5821 uses_all_template_parms_r (tree t, void *data_)
5823 struct uses_all_template_parms_data &data
5824 = *(struct uses_all_template_parms_data*)data_;
5825 tree idx = get_template_parm_index (t);
5827 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5828 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5829 return 0;
5832 static bool
5833 complex_alias_template_p (const_tree tmpl)
5835 struct uses_all_template_parms_data data;
5836 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5837 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5838 data.level = TMPL_PARMS_DEPTH (parms);
5839 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5840 data.seen = XALLOCAVEC (bool, len);
5841 for (int i = 0; i < len; ++i)
5842 data.seen[i] = false;
5844 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5845 for (int i = 0; i < len; ++i)
5846 if (!data.seen[i])
5847 return true;
5848 return false;
5851 /* Return TRUE iff T is a specialization of a complex alias template with
5852 dependent template-arguments. */
5854 bool
5855 dependent_alias_template_spec_p (const_tree t)
5857 return (alias_template_specialization_p (t)
5858 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5859 && (any_dependent_template_arguments_p
5860 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5863 /* Return the number of innermost template parameters in TMPL. */
5865 static int
5866 num_innermost_template_parms (tree tmpl)
5868 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5869 return TREE_VEC_LENGTH (parms);
5872 /* Return either TMPL or another template that it is equivalent to under DR
5873 1286: An alias that just changes the name of a template is equivalent to
5874 the other template. */
5876 static tree
5877 get_underlying_template (tree tmpl)
5879 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5880 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5882 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5883 if (TYPE_TEMPLATE_INFO (result))
5885 tree sub = TYPE_TI_TEMPLATE (result);
5886 if (PRIMARY_TEMPLATE_P (sub)
5887 && (num_innermost_template_parms (tmpl)
5888 == num_innermost_template_parms (sub)))
5890 tree alias_args = INNERMOST_TEMPLATE_ARGS
5891 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5892 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5893 break;
5894 /* The alias type is equivalent to the pattern of the
5895 underlying template, so strip the alias. */
5896 tmpl = sub;
5897 continue;
5900 break;
5902 return tmpl;
5905 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5906 must be a reference-to-function or a pointer-to-function type, as specified
5907 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5908 and check that the resulting function has external linkage. */
5910 static tree
5911 convert_nontype_argument_function (tree type, tree expr,
5912 tsubst_flags_t complain)
5914 tree fns = expr;
5915 tree fn, fn_no_ptr;
5916 linkage_kind linkage;
5918 fn = instantiate_type (type, fns, tf_none);
5919 if (fn == error_mark_node)
5920 return error_mark_node;
5922 fn_no_ptr = strip_fnptr_conv (fn);
5923 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5924 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5925 if (BASELINK_P (fn_no_ptr))
5926 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5928 /* [temp.arg.nontype]/1
5930 A template-argument for a non-type, non-template template-parameter
5931 shall be one of:
5932 [...]
5933 -- the address of an object or function with external [C++11: or
5934 internal] linkage. */
5936 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5938 if (complain & tf_error)
5940 error ("%qE is not a valid template argument for type %qT",
5941 expr, type);
5942 if (TYPE_PTR_P (type))
5943 inform (input_location, "it must be the address of a function "
5944 "with external linkage");
5945 else
5946 inform (input_location, "it must be the name of a function with "
5947 "external linkage");
5949 return NULL_TREE;
5952 linkage = decl_linkage (fn_no_ptr);
5953 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5955 if (complain & tf_error)
5957 if (cxx_dialect >= cxx11)
5958 error ("%qE is not a valid template argument for type %qT "
5959 "because %qD has no linkage",
5960 expr, type, fn_no_ptr);
5961 else
5962 error ("%qE is not a valid template argument for type %qT "
5963 "because %qD does not have external linkage",
5964 expr, type, fn_no_ptr);
5966 return NULL_TREE;
5969 if (TREE_CODE (type) == REFERENCE_TYPE)
5970 fn = build_address (fn);
5971 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
5972 fn = build_nop (type, fn);
5974 return fn;
5977 /* Subroutine of convert_nontype_argument.
5978 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5979 Emit an error otherwise. */
5981 static bool
5982 check_valid_ptrmem_cst_expr (tree type, tree expr,
5983 tsubst_flags_t complain)
5985 STRIP_NOPS (expr);
5986 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5987 return true;
5988 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5989 return true;
5990 if (processing_template_decl
5991 && TREE_CODE (expr) == ADDR_EXPR
5992 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5993 return true;
5994 if (complain & tf_error)
5996 error ("%qE is not a valid template argument for type %qT",
5997 expr, type);
5998 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6000 return false;
6003 /* Returns TRUE iff the address of OP is value-dependent.
6005 14.6.2.4 [temp.dep.temp]:
6006 A non-integral non-type template-argument is dependent if its type is
6007 dependent or it has either of the following forms
6008 qualified-id
6009 & qualified-id
6010 and contains a nested-name-specifier which specifies a class-name that
6011 names a dependent type.
6013 We generalize this to just say that the address of a member of a
6014 dependent class is value-dependent; the above doesn't cover the
6015 address of a static data member named with an unqualified-id. */
6017 static bool
6018 has_value_dependent_address (tree op)
6020 /* We could use get_inner_reference here, but there's no need;
6021 this is only relevant for template non-type arguments, which
6022 can only be expressed as &id-expression. */
6023 if (DECL_P (op))
6025 tree ctx = CP_DECL_CONTEXT (op);
6026 if (TYPE_P (ctx) && dependent_type_p (ctx))
6027 return true;
6030 return false;
6033 /* The next set of functions are used for providing helpful explanatory
6034 diagnostics for failed overload resolution. Their messages should be
6035 indented by two spaces for consistency with the messages in
6036 call.c */
6038 static int
6039 unify_success (bool /*explain_p*/)
6041 return 0;
6044 static int
6045 unify_parameter_deduction_failure (bool explain_p, tree parm)
6047 if (explain_p)
6048 inform (input_location,
6049 " couldn't deduce template parameter %qD", parm);
6050 return 1;
6053 static int
6054 unify_invalid (bool /*explain_p*/)
6056 return 1;
6059 static int
6060 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6062 if (explain_p)
6063 inform (input_location,
6064 " types %qT and %qT have incompatible cv-qualifiers",
6065 parm, arg);
6066 return 1;
6069 static int
6070 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6072 if (explain_p)
6073 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6074 return 1;
6077 static int
6078 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6080 if (explain_p)
6081 inform (input_location,
6082 " template parameter %qD is not a parameter pack, but "
6083 "argument %qD is",
6084 parm, arg);
6085 return 1;
6088 static int
6089 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6091 if (explain_p)
6092 inform (input_location,
6093 " template argument %qE does not match "
6094 "pointer-to-member constant %qE",
6095 arg, parm);
6096 return 1;
6099 static int
6100 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6102 if (explain_p)
6103 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6104 return 1;
6107 static int
6108 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6110 if (explain_p)
6111 inform (input_location,
6112 " inconsistent parameter pack deduction with %qT and %qT",
6113 old_arg, new_arg);
6114 return 1;
6117 static int
6118 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6120 if (explain_p)
6122 if (TYPE_P (parm))
6123 inform (input_location,
6124 " deduced conflicting types for parameter %qT (%qT and %qT)",
6125 parm, first, second);
6126 else
6127 inform (input_location,
6128 " deduced conflicting values for non-type parameter "
6129 "%qE (%qE and %qE)", parm, first, second);
6131 return 1;
6134 static int
6135 unify_vla_arg (bool explain_p, tree arg)
6137 if (explain_p)
6138 inform (input_location,
6139 " variable-sized array type %qT is not "
6140 "a valid template argument",
6141 arg);
6142 return 1;
6145 static int
6146 unify_method_type_error (bool explain_p, tree arg)
6148 if (explain_p)
6149 inform (input_location,
6150 " member function type %qT is not a valid template argument",
6151 arg);
6152 return 1;
6155 static int
6156 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6158 if (explain_p)
6160 if (least_p)
6161 inform_n (input_location, wanted,
6162 " candidate expects at least %d argument, %d provided",
6163 " candidate expects at least %d arguments, %d provided",
6164 wanted, have);
6165 else
6166 inform_n (input_location, wanted,
6167 " candidate expects %d argument, %d provided",
6168 " candidate expects %d arguments, %d provided",
6169 wanted, have);
6171 return 1;
6174 static int
6175 unify_too_many_arguments (bool explain_p, int have, int wanted)
6177 return unify_arity (explain_p, have, wanted);
6180 static int
6181 unify_too_few_arguments (bool explain_p, int have, int wanted,
6182 bool least_p = false)
6184 return unify_arity (explain_p, have, wanted, least_p);
6187 static int
6188 unify_arg_conversion (bool explain_p, tree to_type,
6189 tree from_type, tree arg)
6191 if (explain_p)
6192 inform (EXPR_LOC_OR_LOC (arg, input_location),
6193 " cannot convert %qE (type %qT) to type %qT",
6194 arg, from_type, to_type);
6195 return 1;
6198 static int
6199 unify_no_common_base (bool explain_p, enum template_base_result r,
6200 tree parm, tree arg)
6202 if (explain_p)
6203 switch (r)
6205 case tbr_ambiguous_baseclass:
6206 inform (input_location, " %qT is an ambiguous base class of %qT",
6207 parm, arg);
6208 break;
6209 default:
6210 inform (input_location, " %qT is not derived from %qT", arg, parm);
6211 break;
6213 return 1;
6216 static int
6217 unify_inconsistent_template_template_parameters (bool explain_p)
6219 if (explain_p)
6220 inform (input_location,
6221 " template parameters of a template template argument are "
6222 "inconsistent with other deduced template arguments");
6223 return 1;
6226 static int
6227 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6229 if (explain_p)
6230 inform (input_location,
6231 " can't deduce a template for %qT from non-template type %qT",
6232 parm, arg);
6233 return 1;
6236 static int
6237 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6239 if (explain_p)
6240 inform (input_location,
6241 " template argument %qE does not match %qE", arg, parm);
6242 return 1;
6245 static int
6246 unify_overload_resolution_failure (bool explain_p, tree arg)
6248 if (explain_p)
6249 inform (input_location,
6250 " could not resolve address from overloaded function %qE",
6251 arg);
6252 return 1;
6255 /* Attempt to convert the non-type template parameter EXPR to the
6256 indicated TYPE. If the conversion is successful, return the
6257 converted value. If the conversion is unsuccessful, return
6258 NULL_TREE if we issued an error message, or error_mark_node if we
6259 did not. We issue error messages for out-and-out bad template
6260 parameters, but not simply because the conversion failed, since we
6261 might be just trying to do argument deduction. Both TYPE and EXPR
6262 must be non-dependent.
6264 The conversion follows the special rules described in
6265 [temp.arg.nontype], and it is much more strict than an implicit
6266 conversion.
6268 This function is called twice for each template argument (see
6269 lookup_template_class for a more accurate description of this
6270 problem). This means that we need to handle expressions which
6271 are not valid in a C++ source, but can be created from the
6272 first call (for instance, casts to perform conversions). These
6273 hacks can go away after we fix the double coercion problem. */
6275 static tree
6276 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6278 tree expr_type;
6280 /* Detect immediately string literals as invalid non-type argument.
6281 This special-case is not needed for correctness (we would easily
6282 catch this later), but only to provide better diagnostic for this
6283 common user mistake. As suggested by DR 100, we do not mention
6284 linkage issues in the diagnostic as this is not the point. */
6285 /* FIXME we're making this OK. */
6286 if (TREE_CODE (expr) == STRING_CST)
6288 if (complain & tf_error)
6289 error ("%qE is not a valid template argument for type %qT "
6290 "because string literals can never be used in this context",
6291 expr, type);
6292 return NULL_TREE;
6295 /* Add the ADDR_EXPR now for the benefit of
6296 value_dependent_expression_p. */
6297 if (TYPE_PTROBV_P (type)
6298 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6300 expr = decay_conversion (expr, complain);
6301 if (expr == error_mark_node)
6302 return error_mark_node;
6305 /* If we are in a template, EXPR may be non-dependent, but still
6306 have a syntactic, rather than semantic, form. For example, EXPR
6307 might be a SCOPE_REF, rather than the VAR_DECL to which the
6308 SCOPE_REF refers. Preserving the qualifying scope is necessary
6309 so that access checking can be performed when the template is
6310 instantiated -- but here we need the resolved form so that we can
6311 convert the argument. */
6312 bool non_dep = false;
6313 if (TYPE_REF_OBJ_P (type)
6314 && has_value_dependent_address (expr))
6315 /* If we want the address and it's value-dependent, don't fold. */;
6316 else if (processing_template_decl
6317 && potential_nondependent_constant_expression (expr))
6318 non_dep = true;
6319 if (error_operand_p (expr))
6320 return error_mark_node;
6321 expr_type = TREE_TYPE (expr);
6322 if (TREE_CODE (type) == REFERENCE_TYPE)
6323 expr = mark_lvalue_use (expr);
6324 else
6325 expr = mark_rvalue_use (expr);
6327 /* If the argument is non-dependent, perform any conversions in
6328 non-dependent context as well. */
6329 processing_template_decl_sentinel s (non_dep);
6330 if (non_dep)
6331 expr = instantiate_non_dependent_expr_internal (expr, complain);
6333 if (value_dependent_expression_p (expr))
6334 expr = canonicalize_expr_argument (expr, complain);
6336 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6337 to a non-type argument of "nullptr". */
6338 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6339 expr = fold_simple (convert (type, expr));
6341 /* In C++11, integral or enumeration non-type template arguments can be
6342 arbitrary constant expressions. Pointer and pointer to
6343 member arguments can be general constant expressions that evaluate
6344 to a null value, but otherwise still need to be of a specific form. */
6345 if (cxx_dialect >= cxx11)
6347 if (TREE_CODE (expr) == PTRMEM_CST)
6348 /* A PTRMEM_CST is already constant, and a valid template
6349 argument for a parameter of pointer to member type, we just want
6350 to leave it in that form rather than lower it to a
6351 CONSTRUCTOR. */;
6352 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6353 expr = maybe_constant_value (expr);
6354 else if (cxx_dialect >= cxx1z)
6356 if (TREE_CODE (type) != REFERENCE_TYPE)
6357 expr = maybe_constant_value (expr);
6358 else if (REFERENCE_REF_P (expr))
6360 expr = TREE_OPERAND (expr, 0);
6361 expr = maybe_constant_value (expr);
6362 expr = convert_from_reference (expr);
6365 else if (TYPE_PTR_OR_PTRMEM_P (type))
6367 tree folded = maybe_constant_value (expr);
6368 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6369 : null_member_pointer_value_p (folded))
6370 expr = folded;
6374 /* HACK: Due to double coercion, we can get a
6375 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6376 which is the tree that we built on the first call (see
6377 below when coercing to reference to object or to reference to
6378 function). We just strip everything and get to the arg.
6379 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6380 for examples. */
6381 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6383 tree probe_type, probe = expr;
6384 if (REFERENCE_REF_P (probe))
6385 probe = TREE_OPERAND (probe, 0);
6386 probe_type = TREE_TYPE (probe);
6387 if (TREE_CODE (probe) == NOP_EXPR)
6389 /* ??? Maybe we could use convert_from_reference here, but we
6390 would need to relax its constraints because the NOP_EXPR
6391 could actually change the type to something more cv-qualified,
6392 and this is not folded by convert_from_reference. */
6393 tree addr = TREE_OPERAND (probe, 0);
6394 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6395 && TREE_CODE (addr) == ADDR_EXPR
6396 && TYPE_PTR_P (TREE_TYPE (addr))
6397 && (same_type_ignoring_top_level_qualifiers_p
6398 (TREE_TYPE (probe_type),
6399 TREE_TYPE (TREE_TYPE (addr)))))
6401 expr = TREE_OPERAND (addr, 0);
6402 expr_type = TREE_TYPE (probe_type);
6407 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6408 parameter is a pointer to object, through decay and
6409 qualification conversion. Let's strip everything. */
6410 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6412 tree probe = expr;
6413 STRIP_NOPS (probe);
6414 if (TREE_CODE (probe) == ADDR_EXPR
6415 && TYPE_PTR_P (TREE_TYPE (probe)))
6417 /* Skip the ADDR_EXPR only if it is part of the decay for
6418 an array. Otherwise, it is part of the original argument
6419 in the source code. */
6420 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6421 probe = TREE_OPERAND (probe, 0);
6422 expr = probe;
6423 expr_type = TREE_TYPE (expr);
6427 /* [temp.arg.nontype]/5, bullet 1
6429 For a non-type template-parameter of integral or enumeration type,
6430 integral promotions (_conv.prom_) and integral conversions
6431 (_conv.integral_) are applied. */
6432 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6434 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6435 t = maybe_constant_value (t);
6436 if (t != error_mark_node)
6437 expr = t;
6439 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6440 return error_mark_node;
6442 /* Notice that there are constant expressions like '4 % 0' which
6443 do not fold into integer constants. */
6444 if (TREE_CODE (expr) != INTEGER_CST
6445 && !value_dependent_expression_p (expr))
6447 if (complain & tf_error)
6449 int errs = errorcount, warns = warningcount + werrorcount;
6450 if (processing_template_decl
6451 && !require_potential_constant_expression (expr))
6452 return NULL_TREE;
6453 expr = cxx_constant_value (expr);
6454 if (errorcount > errs || warningcount + werrorcount > warns)
6455 inform (EXPR_LOC_OR_LOC (expr, input_location),
6456 "in template argument for type %qT ", type);
6457 if (expr == error_mark_node)
6458 return NULL_TREE;
6459 /* else cxx_constant_value complained but gave us
6460 a real constant, so go ahead. */
6461 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6463 else
6464 return NULL_TREE;
6467 /* Avoid typedef problems. */
6468 if (TREE_TYPE (expr) != type)
6469 expr = fold_convert (type, expr);
6471 /* [temp.arg.nontype]/5, bullet 2
6473 For a non-type template-parameter of type pointer to object,
6474 qualification conversions (_conv.qual_) and the array-to-pointer
6475 conversion (_conv.array_) are applied. */
6476 else if (TYPE_PTROBV_P (type))
6478 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6480 A template-argument for a non-type, non-template template-parameter
6481 shall be one of: [...]
6483 -- the name of a non-type template-parameter;
6484 -- the address of an object or function with external linkage, [...]
6485 expressed as "& id-expression" where the & is optional if the name
6486 refers to a function or array, or if the corresponding
6487 template-parameter is a reference.
6489 Here, we do not care about functions, as they are invalid anyway
6490 for a parameter of type pointer-to-object. */
6492 if (value_dependent_expression_p (expr))
6493 /* Non-type template parameters are OK. */
6495 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6496 /* Null pointer values are OK in C++11. */;
6497 else if (TREE_CODE (expr) != ADDR_EXPR
6498 && TREE_CODE (expr_type) != ARRAY_TYPE)
6500 if (VAR_P (expr))
6502 if (complain & tf_error)
6503 error ("%qD is not a valid template argument "
6504 "because %qD is a variable, not the address of "
6505 "a variable", expr, expr);
6506 return NULL_TREE;
6508 if (POINTER_TYPE_P (expr_type))
6510 if (complain & tf_error)
6511 error ("%qE is not a valid template argument for %qT "
6512 "because it is not the address of a variable",
6513 expr, type);
6514 return NULL_TREE;
6516 /* Other values, like integer constants, might be valid
6517 non-type arguments of some other type. */
6518 return error_mark_node;
6520 else
6522 tree decl;
6524 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6525 ? TREE_OPERAND (expr, 0) : expr);
6526 if (!VAR_P (decl))
6528 if (complain & tf_error)
6529 error ("%qE is not a valid template argument of type %qT "
6530 "because %qE is not a variable", expr, type, decl);
6531 return NULL_TREE;
6533 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6535 if (complain & tf_error)
6536 error ("%qE is not a valid template argument of type %qT "
6537 "because %qD does not have external linkage",
6538 expr, type, decl);
6539 return NULL_TREE;
6541 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6543 if (complain & tf_error)
6544 error ("%qE is not a valid template argument of type %qT "
6545 "because %qD has no linkage", expr, type, decl);
6546 return NULL_TREE;
6550 expr = decay_conversion (expr, complain);
6551 if (expr == error_mark_node)
6552 return error_mark_node;
6554 expr = perform_qualification_conversions (type, expr);
6555 if (expr == error_mark_node)
6556 return error_mark_node;
6558 /* [temp.arg.nontype]/5, bullet 3
6560 For a non-type template-parameter of type reference to object, no
6561 conversions apply. The type referred to by the reference may be more
6562 cv-qualified than the (otherwise identical) type of the
6563 template-argument. The template-parameter is bound directly to the
6564 template-argument, which must be an lvalue. */
6565 else if (TYPE_REF_OBJ_P (type))
6567 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6568 expr_type))
6569 return error_mark_node;
6571 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6573 if (complain & tf_error)
6574 error ("%qE is not a valid template argument for type %qT "
6575 "because of conflicts in cv-qualification", expr, type);
6576 return NULL_TREE;
6579 if (!lvalue_p (expr))
6581 if (complain & tf_error)
6582 error ("%qE is not a valid template argument for type %qT "
6583 "because it is not an lvalue", expr, type);
6584 return NULL_TREE;
6587 /* [temp.arg.nontype]/1
6589 A template-argument for a non-type, non-template template-parameter
6590 shall be one of: [...]
6592 -- the address of an object or function with external linkage. */
6593 if (INDIRECT_REF_P (expr)
6594 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6596 expr = TREE_OPERAND (expr, 0);
6597 if (DECL_P (expr))
6599 if (complain & tf_error)
6600 error ("%q#D is not a valid template argument for type %qT "
6601 "because a reference variable does not have a constant "
6602 "address", expr, type);
6603 return NULL_TREE;
6607 if (!value_dependent_expression_p (expr))
6609 if (!DECL_P (expr))
6611 if (complain & tf_error)
6612 error ("%qE is not a valid template argument for type %qT "
6613 "because it is not an object with linkage",
6614 expr, type);
6615 return NULL_TREE;
6618 /* DR 1155 allows internal linkage in C++11 and up. */
6619 linkage_kind linkage = decl_linkage (expr);
6620 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6622 if (complain & tf_error)
6623 error ("%qE is not a valid template argument for type %qT "
6624 "because object %qD does not have linkage",
6625 expr, type, expr);
6626 return NULL_TREE;
6629 expr = build_nop (type, build_address (expr));
6632 /* [temp.arg.nontype]/5, bullet 4
6634 For a non-type template-parameter of type pointer to function, only
6635 the function-to-pointer conversion (_conv.func_) is applied. If the
6636 template-argument represents a set of overloaded functions (or a
6637 pointer to such), the matching function is selected from the set
6638 (_over.over_). */
6639 else if (TYPE_PTRFN_P (type))
6641 /* If the argument is a template-id, we might not have enough
6642 context information to decay the pointer. */
6643 if (!type_unknown_p (expr_type))
6645 expr = decay_conversion (expr, complain);
6646 if (expr == error_mark_node)
6647 return error_mark_node;
6650 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6651 /* Null pointer values are OK in C++11. */
6652 return perform_qualification_conversions (type, expr);
6654 if (!value_dependent_expression_p (expr))
6655 expr = convert_nontype_argument_function (type, expr, complain);
6656 if (!expr || expr == error_mark_node)
6657 return expr;
6659 /* [temp.arg.nontype]/5, bullet 5
6661 For a non-type template-parameter of type reference to function, no
6662 conversions apply. If the template-argument represents a set of
6663 overloaded functions, the matching function is selected from the set
6664 (_over.over_). */
6665 else if (TYPE_REFFN_P (type))
6667 if (TREE_CODE (expr) == ADDR_EXPR)
6669 if (complain & tf_error)
6671 error ("%qE is not a valid template argument for type %qT "
6672 "because it is a pointer", expr, type);
6673 inform (input_location, "try using %qE instead",
6674 TREE_OPERAND (expr, 0));
6676 return NULL_TREE;
6679 if (!value_dependent_expression_p (expr))
6680 expr = convert_nontype_argument_function (type, expr, complain);
6681 if (!expr || expr == error_mark_node)
6682 return expr;
6684 /* [temp.arg.nontype]/5, bullet 6
6686 For a non-type template-parameter of type pointer to member function,
6687 no conversions apply. If the template-argument represents a set of
6688 overloaded member functions, the matching member function is selected
6689 from the set (_over.over_). */
6690 else if (TYPE_PTRMEMFUNC_P (type))
6692 expr = instantiate_type (type, expr, tf_none);
6693 if (expr == error_mark_node)
6694 return error_mark_node;
6696 /* [temp.arg.nontype] bullet 1 says the pointer to member
6697 expression must be a pointer-to-member constant. */
6698 if (!value_dependent_expression_p (expr)
6699 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6700 return error_mark_node;
6702 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6703 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6704 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6705 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6707 /* There is no way to disable standard conversions in
6708 resolve_address_of_overloaded_function (called by
6709 instantiate_type). It is possible that the call succeeded by
6710 converting &B::I to &D::I (where B is a base of D), so we need
6711 to reject this conversion here.
6713 Actually, even if there was a way to disable standard conversions,
6714 it would still be better to reject them here so that we can
6715 provide a superior diagnostic. */
6716 if (!same_type_p (TREE_TYPE (expr), type))
6718 if (complain & tf_error)
6720 error ("%qE is not a valid template argument for type %qT "
6721 "because it is of type %qT", expr, type,
6722 TREE_TYPE (expr));
6723 /* If we are just one standard conversion off, explain. */
6724 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6725 inform (input_location,
6726 "standard conversions are not allowed in this context");
6728 return NULL_TREE;
6731 /* [temp.arg.nontype]/5, bullet 7
6733 For a non-type template-parameter of type pointer to data member,
6734 qualification conversions (_conv.qual_) are applied. */
6735 else if (TYPE_PTRDATAMEM_P (type))
6737 /* [temp.arg.nontype] bullet 1 says the pointer to member
6738 expression must be a pointer-to-member constant. */
6739 if (!value_dependent_expression_p (expr)
6740 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6741 return error_mark_node;
6743 expr = perform_qualification_conversions (type, expr);
6744 if (expr == error_mark_node)
6745 return expr;
6747 else if (NULLPTR_TYPE_P (type))
6749 if (expr != nullptr_node)
6751 if (complain & tf_error)
6752 error ("%qE is not a valid template argument for type %qT "
6753 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6754 return NULL_TREE;
6756 return expr;
6758 /* A template non-type parameter must be one of the above. */
6759 else
6760 gcc_unreachable ();
6762 /* Sanity check: did we actually convert the argument to the
6763 right type? */
6764 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6765 (type, TREE_TYPE (expr)));
6766 return convert_from_reference (expr);
6769 /* Subroutine of coerce_template_template_parms, which returns 1 if
6770 PARM_PARM and ARG_PARM match using the rule for the template
6771 parameters of template template parameters. Both PARM and ARG are
6772 template parameters; the rest of the arguments are the same as for
6773 coerce_template_template_parms.
6775 static int
6776 coerce_template_template_parm (tree parm,
6777 tree arg,
6778 tsubst_flags_t complain,
6779 tree in_decl,
6780 tree outer_args)
6782 if (arg == NULL_TREE || error_operand_p (arg)
6783 || parm == NULL_TREE || error_operand_p (parm))
6784 return 0;
6786 if (TREE_CODE (arg) != TREE_CODE (parm))
6787 return 0;
6789 switch (TREE_CODE (parm))
6791 case TEMPLATE_DECL:
6792 /* We encounter instantiations of templates like
6793 template <template <template <class> class> class TT>
6794 class C; */
6796 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6797 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6799 if (!coerce_template_template_parms
6800 (parmparm, argparm, complain, in_decl, outer_args))
6801 return 0;
6803 /* Fall through. */
6805 case TYPE_DECL:
6806 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6807 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6808 /* Argument is a parameter pack but parameter is not. */
6809 return 0;
6810 break;
6812 case PARM_DECL:
6813 /* The tsubst call is used to handle cases such as
6815 template <int> class C {};
6816 template <class T, template <T> class TT> class D {};
6817 D<int, C> d;
6819 i.e. the parameter list of TT depends on earlier parameters. */
6820 if (!uses_template_parms (TREE_TYPE (arg)))
6822 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6823 if (!uses_template_parms (t)
6824 && !same_type_p (t, TREE_TYPE (arg)))
6825 return 0;
6828 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6829 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6830 /* Argument is a parameter pack but parameter is not. */
6831 return 0;
6833 break;
6835 default:
6836 gcc_unreachable ();
6839 return 1;
6843 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6844 template template parameters. Both PARM_PARMS and ARG_PARMS are
6845 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6846 or PARM_DECL.
6848 Consider the example:
6849 template <class T> class A;
6850 template<template <class U> class TT> class B;
6852 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6853 the parameters to A, and OUTER_ARGS contains A. */
6855 static int
6856 coerce_template_template_parms (tree parm_parms,
6857 tree arg_parms,
6858 tsubst_flags_t complain,
6859 tree in_decl,
6860 tree outer_args)
6862 int nparms, nargs, i;
6863 tree parm, arg;
6864 int variadic_p = 0;
6866 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6867 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6869 nparms = TREE_VEC_LENGTH (parm_parms);
6870 nargs = TREE_VEC_LENGTH (arg_parms);
6872 /* Determine whether we have a parameter pack at the end of the
6873 template template parameter's template parameter list. */
6874 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6876 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6878 if (error_operand_p (parm))
6879 return 0;
6881 switch (TREE_CODE (parm))
6883 case TEMPLATE_DECL:
6884 case TYPE_DECL:
6885 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6886 variadic_p = 1;
6887 break;
6889 case PARM_DECL:
6890 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6891 variadic_p = 1;
6892 break;
6894 default:
6895 gcc_unreachable ();
6899 if (nargs != nparms
6900 && !(variadic_p && nargs >= nparms - 1))
6901 return 0;
6903 /* Check all of the template parameters except the parameter pack at
6904 the end (if any). */
6905 for (i = 0; i < nparms - variadic_p; ++i)
6907 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6908 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6909 continue;
6911 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6912 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6914 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6915 outer_args))
6916 return 0;
6920 if (variadic_p)
6922 /* Check each of the template parameters in the template
6923 argument against the template parameter pack at the end of
6924 the template template parameter. */
6925 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6926 return 0;
6928 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6930 for (; i < nargs; ++i)
6932 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6933 continue;
6935 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6937 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6938 outer_args))
6939 return 0;
6943 return 1;
6946 /* Verifies that the deduced template arguments (in TARGS) for the
6947 template template parameters (in TPARMS) represent valid bindings,
6948 by comparing the template parameter list of each template argument
6949 to the template parameter list of its corresponding template
6950 template parameter, in accordance with DR150. This
6951 routine can only be called after all template arguments have been
6952 deduced. It will return TRUE if all of the template template
6953 parameter bindings are okay, FALSE otherwise. */
6954 bool
6955 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6957 int i, ntparms = TREE_VEC_LENGTH (tparms);
6958 bool ret = true;
6960 /* We're dealing with template parms in this process. */
6961 ++processing_template_decl;
6963 targs = INNERMOST_TEMPLATE_ARGS (targs);
6965 for (i = 0; i < ntparms; ++i)
6967 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6968 tree targ = TREE_VEC_ELT (targs, i);
6970 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6972 tree packed_args = NULL_TREE;
6973 int idx, len = 1;
6975 if (ARGUMENT_PACK_P (targ))
6977 /* Look inside the argument pack. */
6978 packed_args = ARGUMENT_PACK_ARGS (targ);
6979 len = TREE_VEC_LENGTH (packed_args);
6982 for (idx = 0; idx < len; ++idx)
6984 tree targ_parms = NULL_TREE;
6986 if (packed_args)
6987 /* Extract the next argument from the argument
6988 pack. */
6989 targ = TREE_VEC_ELT (packed_args, idx);
6991 if (PACK_EXPANSION_P (targ))
6992 /* Look at the pattern of the pack expansion. */
6993 targ = PACK_EXPANSION_PATTERN (targ);
6995 /* Extract the template parameters from the template
6996 argument. */
6997 if (TREE_CODE (targ) == TEMPLATE_DECL)
6998 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6999 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7000 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7002 /* Verify that we can coerce the template template
7003 parameters from the template argument to the template
7004 parameter. This requires an exact match. */
7005 if (targ_parms
7006 && !coerce_template_template_parms
7007 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7008 targ_parms,
7009 tf_none,
7010 tparm,
7011 targs))
7013 ret = false;
7014 goto out;
7020 out:
7022 --processing_template_decl;
7023 return ret;
7026 /* Since type attributes aren't mangled, we need to strip them from
7027 template type arguments. */
7029 static tree
7030 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7032 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7033 return arg;
7034 bool removed_attributes = false;
7035 tree canon = strip_typedefs (arg, &removed_attributes);
7036 if (removed_attributes
7037 && (complain & tf_warning))
7038 warning (OPT_Wignored_attributes,
7039 "ignoring attributes on template argument %qT", arg);
7040 return canon;
7043 /* And from inside dependent non-type arguments like sizeof(Type). */
7045 static tree
7046 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7048 if (!arg || arg == error_mark_node)
7049 return arg;
7050 bool removed_attributes = false;
7051 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7052 if (removed_attributes
7053 && (complain & tf_warning))
7054 warning (OPT_Wignored_attributes,
7055 "ignoring attributes in template argument %qE", arg);
7056 return canon;
7059 // A template declaration can be substituted for a constrained
7060 // template template parameter only when the argument is more
7061 // constrained than the parameter.
7062 static bool
7063 is_compatible_template_arg (tree parm, tree arg)
7065 tree parm_cons = get_constraints (parm);
7067 /* For now, allow constrained template template arguments
7068 and unconstrained template template parameters. */
7069 if (parm_cons == NULL_TREE)
7070 return true;
7072 tree arg_cons = get_constraints (arg);
7074 // If the template parameter is constrained, we need to rewrite its
7075 // constraints in terms of the ARG's template parameters. This ensures
7076 // that all of the template parameter types will have the same depth.
7078 // Note that this is only valid when coerce_template_template_parm is
7079 // true for the innermost template parameters of PARM and ARG. In other
7080 // words, because coercion is successful, this conversion will be valid.
7081 if (parm_cons)
7083 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7084 parm_cons = tsubst_constraint_info (parm_cons,
7085 INNERMOST_TEMPLATE_ARGS (args),
7086 tf_none, NULL_TREE);
7087 if (parm_cons == error_mark_node)
7088 return false;
7091 return subsumes (parm_cons, arg_cons);
7094 // Convert a placeholder argument into a binding to the original
7095 // parameter. The original parameter is saved as the TREE_TYPE of
7096 // ARG.
7097 static inline tree
7098 convert_wildcard_argument (tree parm, tree arg)
7100 TREE_TYPE (arg) = parm;
7101 return arg;
7104 /* Convert the indicated template ARG as necessary to match the
7105 indicated template PARM. Returns the converted ARG, or
7106 error_mark_node if the conversion was unsuccessful. Error and
7107 warning messages are issued under control of COMPLAIN. This
7108 conversion is for the Ith parameter in the parameter list. ARGS is
7109 the full set of template arguments deduced so far. */
7111 static tree
7112 convert_template_argument (tree parm,
7113 tree arg,
7114 tree args,
7115 tsubst_flags_t complain,
7116 int i,
7117 tree in_decl)
7119 tree orig_arg;
7120 tree val;
7121 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7123 if (parm == error_mark_node)
7124 return error_mark_node;
7126 /* Trivially convert placeholders. */
7127 if (TREE_CODE (arg) == WILDCARD_DECL)
7128 return convert_wildcard_argument (parm, arg);
7130 if (TREE_CODE (arg) == TREE_LIST
7131 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7133 /* The template argument was the name of some
7134 member function. That's usually
7135 invalid, but static members are OK. In any
7136 case, grab the underlying fields/functions
7137 and issue an error later if required. */
7138 orig_arg = TREE_VALUE (arg);
7139 TREE_TYPE (arg) = unknown_type_node;
7142 orig_arg = arg;
7144 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7145 requires_type = (TREE_CODE (parm) == TYPE_DECL
7146 || requires_tmpl_type);
7148 /* When determining whether an argument pack expansion is a template,
7149 look at the pattern. */
7150 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7151 arg = PACK_EXPANSION_PATTERN (arg);
7153 /* Deal with an injected-class-name used as a template template arg. */
7154 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7156 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7157 if (TREE_CODE (t) == TEMPLATE_DECL)
7159 if (cxx_dialect >= cxx11)
7160 /* OK under DR 1004. */;
7161 else if (complain & tf_warning_or_error)
7162 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7163 " used as template template argument", TYPE_NAME (arg));
7164 else if (flag_pedantic_errors)
7165 t = arg;
7167 arg = t;
7171 is_tmpl_type =
7172 ((TREE_CODE (arg) == TEMPLATE_DECL
7173 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7174 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7175 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7176 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7178 if (is_tmpl_type
7179 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7180 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7181 arg = TYPE_STUB_DECL (arg);
7183 is_type = TYPE_P (arg) || is_tmpl_type;
7185 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7186 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7188 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7190 if (complain & tf_error)
7191 error ("invalid use of destructor %qE as a type", orig_arg);
7192 return error_mark_node;
7195 permerror (input_location,
7196 "to refer to a type member of a template parameter, "
7197 "use %<typename %E%>", orig_arg);
7199 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7200 TREE_OPERAND (arg, 1),
7201 typename_type,
7202 complain);
7203 arg = orig_arg;
7204 is_type = 1;
7206 if (is_type != requires_type)
7208 if (in_decl)
7210 if (complain & tf_error)
7212 error ("type/value mismatch at argument %d in template "
7213 "parameter list for %qD",
7214 i + 1, in_decl);
7215 if (is_type)
7216 inform (input_location,
7217 " expected a constant of type %qT, got %qT",
7218 TREE_TYPE (parm),
7219 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7220 else if (requires_tmpl_type)
7221 inform (input_location,
7222 " expected a class template, got %qE", orig_arg);
7223 else
7224 inform (input_location,
7225 " expected a type, got %qE", orig_arg);
7228 return error_mark_node;
7230 if (is_tmpl_type ^ requires_tmpl_type)
7232 if (in_decl && (complain & tf_error))
7234 error ("type/value mismatch at argument %d in template "
7235 "parameter list for %qD",
7236 i + 1, in_decl);
7237 if (is_tmpl_type)
7238 inform (input_location,
7239 " expected a type, got %qT", DECL_NAME (arg));
7240 else
7241 inform (input_location,
7242 " expected a class template, got %qT", orig_arg);
7244 return error_mark_node;
7247 if (is_type)
7249 if (requires_tmpl_type)
7251 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7252 val = orig_arg;
7253 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7254 /* The number of argument required is not known yet.
7255 Just accept it for now. */
7256 val = TREE_TYPE (arg);
7257 else
7259 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7260 tree argparm;
7262 /* Strip alias templates that are equivalent to another
7263 template. */
7264 arg = get_underlying_template (arg);
7265 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7267 if (coerce_template_template_parms (parmparm, argparm,
7268 complain, in_decl,
7269 args))
7271 val = arg;
7273 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7274 TEMPLATE_DECL. */
7275 if (val != error_mark_node)
7277 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7278 val = TREE_TYPE (val);
7279 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7280 val = make_pack_expansion (val);
7283 else
7285 if (in_decl && (complain & tf_error))
7287 error ("type/value mismatch at argument %d in "
7288 "template parameter list for %qD",
7289 i + 1, in_decl);
7290 inform (input_location,
7291 " expected a template of type %qD, got %qT",
7292 parm, orig_arg);
7295 val = error_mark_node;
7298 // Check that the constraints are compatible before allowing the
7299 // substitution.
7300 if (val != error_mark_node)
7301 if (!is_compatible_template_arg (parm, arg))
7303 if (in_decl && (complain & tf_error))
7305 error ("constraint mismatch at argument %d in "
7306 "template parameter list for %qD",
7307 i + 1, in_decl);
7308 inform (input_location, " expected %qD but got %qD",
7309 parm, arg);
7311 val = error_mark_node;
7315 else
7316 val = orig_arg;
7317 /* We only form one instance of each template specialization.
7318 Therefore, if we use a non-canonical variant (i.e., a
7319 typedef), any future messages referring to the type will use
7320 the typedef, which is confusing if those future uses do not
7321 themselves also use the typedef. */
7322 if (TYPE_P (val))
7323 val = canonicalize_type_argument (val, complain);
7325 else
7327 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7329 if (tree a = type_uses_auto (t))
7331 t = do_auto_deduction (t, arg, a, complain, adc_unspecified);
7332 if (t == error_mark_node)
7333 return error_mark_node;
7336 if (invalid_nontype_parm_type_p (t, complain))
7337 return error_mark_node;
7339 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7341 if (same_type_p (t, TREE_TYPE (orig_arg)))
7342 val = orig_arg;
7343 else
7345 /* Not sure if this is reachable, but it doesn't hurt
7346 to be robust. */
7347 error ("type mismatch in nontype parameter pack");
7348 val = error_mark_node;
7351 else if (!type_dependent_expression_p (orig_arg)
7352 && !uses_template_parms (t))
7353 /* We used to call digest_init here. However, digest_init
7354 will report errors, which we don't want when complain
7355 is zero. More importantly, digest_init will try too
7356 hard to convert things: for example, `0' should not be
7357 converted to pointer type at this point according to
7358 the standard. Accepting this is not merely an
7359 extension, since deciding whether or not these
7360 conversions can occur is part of determining which
7361 function template to call, or whether a given explicit
7362 argument specification is valid. */
7363 val = convert_nontype_argument (t, orig_arg, complain);
7364 else
7365 val = canonicalize_expr_argument (orig_arg, complain);
7367 if (val == NULL_TREE)
7368 val = error_mark_node;
7369 else if (val == error_mark_node && (complain & tf_error))
7370 error ("could not convert template argument %qE from %qT to %qT",
7371 orig_arg, TREE_TYPE (orig_arg), t);
7373 if (INDIRECT_REF_P (val))
7375 /* Reject template arguments that are references to built-in
7376 functions with no library fallbacks. */
7377 const_tree inner = TREE_OPERAND (val, 0);
7378 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7379 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner))) == FUNCTION_TYPE
7380 && TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7381 && 0 < TREE_OPERAND_LENGTH (inner)
7382 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7383 return error_mark_node;
7386 if (TREE_CODE (val) == SCOPE_REF)
7388 /* Strip typedefs from the SCOPE_REF. */
7389 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7390 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7391 complain);
7392 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7393 QUALIFIED_NAME_IS_TEMPLATE (val));
7397 return val;
7400 /* Coerces the remaining template arguments in INNER_ARGS (from
7401 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7402 Returns the coerced argument pack. PARM_IDX is the position of this
7403 parameter in the template parameter list. ARGS is the original
7404 template argument list. */
7405 static tree
7406 coerce_template_parameter_pack (tree parms,
7407 int parm_idx,
7408 tree args,
7409 tree inner_args,
7410 int arg_idx,
7411 tree new_args,
7412 int* lost,
7413 tree in_decl,
7414 tsubst_flags_t complain)
7416 tree parm = TREE_VEC_ELT (parms, parm_idx);
7417 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7418 tree packed_args;
7419 tree argument_pack;
7420 tree packed_parms = NULL_TREE;
7422 if (arg_idx > nargs)
7423 arg_idx = nargs;
7425 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7427 /* When the template parameter is a non-type template parameter pack
7428 or template template parameter pack whose type or template
7429 parameters use parameter packs, we know exactly how many arguments
7430 we are looking for. Build a vector of the instantiated decls for
7431 these template parameters in PACKED_PARMS. */
7432 /* We can't use make_pack_expansion here because it would interpret a
7433 _DECL as a use rather than a declaration. */
7434 tree decl = TREE_VALUE (parm);
7435 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7436 SET_PACK_EXPANSION_PATTERN (exp, decl);
7437 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7438 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7440 TREE_VEC_LENGTH (args)--;
7441 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7442 TREE_VEC_LENGTH (args)++;
7444 if (packed_parms == error_mark_node)
7445 return error_mark_node;
7447 /* If we're doing a partial instantiation of a member template,
7448 verify that all of the types used for the non-type
7449 template parameter pack are, in fact, valid for non-type
7450 template parameters. */
7451 if (arg_idx < nargs
7452 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7454 int j, len = TREE_VEC_LENGTH (packed_parms);
7455 for (j = 0; j < len; ++j)
7457 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7458 if (invalid_nontype_parm_type_p (t, complain))
7459 return error_mark_node;
7461 /* We don't know how many args we have yet, just
7462 use the unconverted ones for now. */
7463 return NULL_TREE;
7466 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7468 /* Check if we have a placeholder pack, which indicates we're
7469 in the context of a introduction list. In that case we want
7470 to match this pack to the single placeholder. */
7471 else if (arg_idx < nargs
7472 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7473 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7475 nargs = arg_idx + 1;
7476 packed_args = make_tree_vec (1);
7478 else
7479 packed_args = make_tree_vec (nargs - arg_idx);
7481 /* Convert the remaining arguments, which will be a part of the
7482 parameter pack "parm". */
7483 int first_pack_arg = arg_idx;
7484 for (; arg_idx < nargs; ++arg_idx)
7486 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7487 tree actual_parm = TREE_VALUE (parm);
7488 int pack_idx = arg_idx - first_pack_arg;
7490 if (packed_parms)
7492 /* Once we've packed as many args as we have types, stop. */
7493 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7494 break;
7495 else if (PACK_EXPANSION_P (arg))
7496 /* We don't know how many args we have yet, just
7497 use the unconverted ones for now. */
7498 return NULL_TREE;
7499 else
7500 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7503 if (arg == error_mark_node)
7505 if (complain & tf_error)
7506 error ("template argument %d is invalid", arg_idx + 1);
7508 else
7509 arg = convert_template_argument (actual_parm,
7510 arg, new_args, complain, parm_idx,
7511 in_decl);
7512 if (arg == error_mark_node)
7513 (*lost)++;
7514 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7517 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7518 && TREE_VEC_LENGTH (packed_args) > 0)
7520 if (complain & tf_error)
7521 error ("wrong number of template arguments (%d, should be %d)",
7522 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7523 return error_mark_node;
7526 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7527 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7528 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7529 else
7531 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7532 TREE_TYPE (argument_pack)
7533 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7534 TREE_CONSTANT (argument_pack) = 1;
7537 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7538 if (CHECKING_P)
7539 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7540 TREE_VEC_LENGTH (packed_args));
7541 return argument_pack;
7544 /* Returns the number of pack expansions in the template argument vector
7545 ARGS. */
7547 static int
7548 pack_expansion_args_count (tree args)
7550 int i;
7551 int count = 0;
7552 if (args)
7553 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7555 tree elt = TREE_VEC_ELT (args, i);
7556 if (elt && PACK_EXPANSION_P (elt))
7557 ++count;
7559 return count;
7562 /* Convert all template arguments to their appropriate types, and
7563 return a vector containing the innermost resulting template
7564 arguments. If any error occurs, return error_mark_node. Error and
7565 warning messages are issued under control of COMPLAIN.
7567 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7568 for arguments not specified in ARGS. Otherwise, if
7569 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7570 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7571 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7572 ARGS. */
7574 static tree
7575 coerce_template_parms (tree parms,
7576 tree args,
7577 tree in_decl,
7578 tsubst_flags_t complain,
7579 bool require_all_args,
7580 bool use_default_args)
7582 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7583 tree orig_inner_args;
7584 tree inner_args;
7585 tree new_args;
7586 tree new_inner_args;
7587 int saved_unevaluated_operand;
7588 int saved_inhibit_evaluation_warnings;
7590 /* When used as a boolean value, indicates whether this is a
7591 variadic template parameter list. Since it's an int, we can also
7592 subtract it from nparms to get the number of non-variadic
7593 parameters. */
7594 int variadic_p = 0;
7595 int variadic_args_p = 0;
7596 int post_variadic_parms = 0;
7598 /* Likewise for parameters with default arguments. */
7599 int default_p = 0;
7601 if (args == error_mark_node)
7602 return error_mark_node;
7604 nparms = TREE_VEC_LENGTH (parms);
7606 /* Determine if there are any parameter packs or default arguments. */
7607 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7609 tree parm = TREE_VEC_ELT (parms, parm_idx);
7610 if (variadic_p)
7611 ++post_variadic_parms;
7612 if (template_parameter_pack_p (TREE_VALUE (parm)))
7613 ++variadic_p;
7614 if (TREE_PURPOSE (parm))
7615 ++default_p;
7618 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7619 /* If there are no parameters that follow a parameter pack, we need to
7620 expand any argument packs so that we can deduce a parameter pack from
7621 some non-packed args followed by an argument pack, as in variadic85.C.
7622 If there are such parameters, we need to leave argument packs intact
7623 so the arguments are assigned properly. This can happen when dealing
7624 with a nested class inside a partial specialization of a class
7625 template, as in variadic92.C, or when deducing a template parameter pack
7626 from a sub-declarator, as in variadic114.C. */
7627 if (!post_variadic_parms)
7628 inner_args = expand_template_argument_pack (inner_args);
7630 /* Count any pack expansion args. */
7631 variadic_args_p = pack_expansion_args_count (inner_args);
7633 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7634 if ((nargs > nparms && !variadic_p)
7635 || (nargs < nparms - variadic_p
7636 && require_all_args
7637 && !variadic_args_p
7638 && (!use_default_args
7639 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7640 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7642 if (complain & tf_error)
7644 if (variadic_p || default_p)
7646 nparms -= variadic_p + default_p;
7647 error ("wrong number of template arguments "
7648 "(%d, should be at least %d)", nargs, nparms);
7650 else
7651 error ("wrong number of template arguments "
7652 "(%d, should be %d)", nargs, nparms);
7654 if (in_decl)
7655 inform (DECL_SOURCE_LOCATION (in_decl),
7656 "provided for %qD", in_decl);
7659 return error_mark_node;
7661 /* We can't pass a pack expansion to a non-pack parameter of an alias
7662 template (DR 1430). */
7663 else if (in_decl
7664 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7665 || concept_template_p (in_decl))
7666 && variadic_args_p
7667 && nargs - variadic_args_p < nparms - variadic_p)
7669 if (complain & tf_error)
7671 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7673 tree arg = TREE_VEC_ELT (inner_args, i);
7674 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7676 if (PACK_EXPANSION_P (arg)
7677 && !template_parameter_pack_p (parm))
7679 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7680 error_at (location_of (arg),
7681 "pack expansion argument for non-pack parameter "
7682 "%qD of alias template %qD", parm, in_decl);
7683 else
7684 error_at (location_of (arg),
7685 "pack expansion argument for non-pack parameter "
7686 "%qD of concept %qD", parm, in_decl);
7687 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7688 goto found;
7691 gcc_unreachable ();
7692 found:;
7694 return error_mark_node;
7697 /* We need to evaluate the template arguments, even though this
7698 template-id may be nested within a "sizeof". */
7699 saved_unevaluated_operand = cp_unevaluated_operand;
7700 cp_unevaluated_operand = 0;
7701 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7702 c_inhibit_evaluation_warnings = 0;
7703 new_inner_args = make_tree_vec (nparms);
7704 new_args = add_outermost_template_args (args, new_inner_args);
7705 int pack_adjust = 0;
7706 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7708 tree arg;
7709 tree parm;
7711 /* Get the Ith template parameter. */
7712 parm = TREE_VEC_ELT (parms, parm_idx);
7714 if (parm == error_mark_node)
7716 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7717 continue;
7720 /* Calculate the next argument. */
7721 if (arg_idx < nargs)
7722 arg = TREE_VEC_ELT (inner_args, arg_idx);
7723 else
7724 arg = NULL_TREE;
7726 if (template_parameter_pack_p (TREE_VALUE (parm))
7727 && !(arg && ARGUMENT_PACK_P (arg)))
7729 /* Some arguments will be placed in the
7730 template parameter pack PARM. */
7731 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7732 inner_args, arg_idx,
7733 new_args, &lost,
7734 in_decl, complain);
7736 if (arg == NULL_TREE)
7738 /* We don't know how many args we have yet, just use the
7739 unconverted (and still packed) ones for now. */
7740 new_inner_args = orig_inner_args;
7741 arg_idx = nargs;
7742 break;
7745 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7747 /* Store this argument. */
7748 if (arg == error_mark_node)
7750 lost++;
7751 /* We are done with all of the arguments. */
7752 arg_idx = nargs;
7754 else
7756 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7757 arg_idx += pack_adjust;
7760 continue;
7762 else if (arg)
7764 if (PACK_EXPANSION_P (arg))
7766 /* "If every valid specialization of a variadic template
7767 requires an empty template parameter pack, the template is
7768 ill-formed, no diagnostic required." So check that the
7769 pattern works with this parameter. */
7770 tree pattern = PACK_EXPANSION_PATTERN (arg);
7771 tree conv = convert_template_argument (TREE_VALUE (parm),
7772 pattern, new_args,
7773 complain, parm_idx,
7774 in_decl);
7775 if (conv == error_mark_node)
7777 inform (input_location, "so any instantiation with a "
7778 "non-empty parameter pack would be ill-formed");
7779 ++lost;
7781 else if (TYPE_P (conv) && !TYPE_P (pattern))
7782 /* Recover from missing typename. */
7783 TREE_VEC_ELT (inner_args, arg_idx)
7784 = make_pack_expansion (conv);
7786 /* We don't know how many args we have yet, just
7787 use the unconverted ones for now. */
7788 new_inner_args = inner_args;
7789 arg_idx = nargs;
7790 break;
7793 else if (require_all_args)
7795 /* There must be a default arg in this case. */
7796 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7797 complain, in_decl);
7798 /* The position of the first default template argument,
7799 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7800 Record that. */
7801 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7802 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7803 arg_idx - pack_adjust);
7805 else
7806 break;
7808 if (arg == error_mark_node)
7810 if (complain & tf_error)
7811 error ("template argument %d is invalid", arg_idx + 1);
7813 else if (!arg)
7814 /* This only occurs if there was an error in the template
7815 parameter list itself (which we would already have
7816 reported) that we are trying to recover from, e.g., a class
7817 template with a parameter list such as
7818 template<typename..., typename>. */
7819 ++lost;
7820 else
7821 arg = convert_template_argument (TREE_VALUE (parm),
7822 arg, new_args, complain,
7823 parm_idx, in_decl);
7825 if (arg == error_mark_node)
7826 lost++;
7827 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7829 cp_unevaluated_operand = saved_unevaluated_operand;
7830 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7832 if (variadic_p && arg_idx < nargs)
7834 if (complain & tf_error)
7836 error ("wrong number of template arguments "
7837 "(%d, should be %d)", nargs, arg_idx);
7838 if (in_decl)
7839 error ("provided for %q+D", in_decl);
7841 return error_mark_node;
7844 if (lost)
7845 return error_mark_node;
7847 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7848 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7849 TREE_VEC_LENGTH (new_inner_args));
7851 return new_inner_args;
7854 /* Convert all template arguments to their appropriate types, and
7855 return a vector containing the innermost resulting template
7856 arguments. If any error occurs, return error_mark_node. Error and
7857 warning messages are not issued.
7859 Note that no function argument deduction is performed, and default
7860 arguments are used to fill in unspecified arguments. */
7861 tree
7862 coerce_template_parms (tree parms, tree args, tree in_decl)
7864 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7867 /* Convert all template arguments to their appropriate type, and
7868 instantiate default arguments as needed. This returns a vector
7869 containing the innermost resulting template arguments, or
7870 error_mark_node if unsuccessful. */
7871 tree
7872 coerce_template_parms (tree parms, tree args, tree in_decl,
7873 tsubst_flags_t complain)
7875 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7878 /* Like coerce_template_parms. If PARMS represents all template
7879 parameters levels, this function returns a vector of vectors
7880 representing all the resulting argument levels. Note that in this
7881 case, only the innermost arguments are coerced because the
7882 outermost ones are supposed to have been coerced already.
7884 Otherwise, if PARMS represents only (the innermost) vector of
7885 parameters, this function returns a vector containing just the
7886 innermost resulting arguments. */
7888 static tree
7889 coerce_innermost_template_parms (tree parms,
7890 tree args,
7891 tree in_decl,
7892 tsubst_flags_t complain,
7893 bool require_all_args,
7894 bool use_default_args)
7896 int parms_depth = TMPL_PARMS_DEPTH (parms);
7897 int args_depth = TMPL_ARGS_DEPTH (args);
7898 tree coerced_args;
7900 if (parms_depth > 1)
7902 coerced_args = make_tree_vec (parms_depth);
7903 tree level;
7904 int cur_depth;
7906 for (level = parms, cur_depth = parms_depth;
7907 parms_depth > 0 && level != NULL_TREE;
7908 level = TREE_CHAIN (level), --cur_depth)
7910 tree l;
7911 if (cur_depth == args_depth)
7912 l = coerce_template_parms (TREE_VALUE (level),
7913 args, in_decl, complain,
7914 require_all_args,
7915 use_default_args);
7916 else
7917 l = TMPL_ARGS_LEVEL (args, cur_depth);
7919 if (l == error_mark_node)
7920 return error_mark_node;
7922 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7925 else
7926 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7927 args, in_decl, complain,
7928 require_all_args,
7929 use_default_args);
7930 return coerced_args;
7933 /* Returns 1 if template args OT and NT are equivalent. */
7936 template_args_equal (tree ot, tree nt)
7938 if (nt == ot)
7939 return 1;
7940 if (nt == NULL_TREE || ot == NULL_TREE)
7941 return false;
7943 if (TREE_CODE (nt) == TREE_VEC)
7944 /* For member templates */
7945 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7946 else if (PACK_EXPANSION_P (ot))
7947 return (PACK_EXPANSION_P (nt)
7948 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7949 PACK_EXPANSION_PATTERN (nt))
7950 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7951 PACK_EXPANSION_EXTRA_ARGS (nt)));
7952 else if (ARGUMENT_PACK_P (ot))
7954 int i, len;
7955 tree opack, npack;
7957 if (!ARGUMENT_PACK_P (nt))
7958 return 0;
7960 opack = ARGUMENT_PACK_ARGS (ot);
7961 npack = ARGUMENT_PACK_ARGS (nt);
7962 len = TREE_VEC_LENGTH (opack);
7963 if (TREE_VEC_LENGTH (npack) != len)
7964 return 0;
7965 for (i = 0; i < len; ++i)
7966 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7967 TREE_VEC_ELT (npack, i)))
7968 return 0;
7969 return 1;
7971 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7972 gcc_unreachable ();
7973 else if (TYPE_P (nt))
7975 if (!TYPE_P (ot))
7976 return false;
7977 /* Don't treat an alias template specialization with dependent
7978 arguments as equivalent to its underlying type when used as a
7979 template argument; we need them to be distinct so that we
7980 substitute into the specialization arguments at instantiation
7981 time. And aliases can't be equivalent without being ==, so
7982 we don't need to look any deeper. */
7983 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7984 return false;
7985 else
7986 return same_type_p (ot, nt);
7988 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7989 return 0;
7990 else
7992 /* Try to treat a template non-type argument that has been converted
7993 to the parameter type as equivalent to one that hasn't yet. */
7994 for (enum tree_code code1 = TREE_CODE (ot);
7995 CONVERT_EXPR_CODE_P (code1)
7996 || code1 == NON_LVALUE_EXPR;
7997 code1 = TREE_CODE (ot))
7998 ot = TREE_OPERAND (ot, 0);
7999 for (enum tree_code code2 = TREE_CODE (nt);
8000 CONVERT_EXPR_CODE_P (code2)
8001 || code2 == NON_LVALUE_EXPR;
8002 code2 = TREE_CODE (nt))
8003 nt = TREE_OPERAND (nt, 0);
8005 return cp_tree_equal (ot, nt);
8009 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8010 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8011 NEWARG_PTR with the offending arguments if they are non-NULL. */
8014 comp_template_args (tree oldargs, tree newargs,
8015 tree *oldarg_ptr, tree *newarg_ptr)
8017 int i;
8019 if (oldargs == newargs)
8020 return 1;
8022 if (!oldargs || !newargs)
8023 return 0;
8025 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8026 return 0;
8028 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8030 tree nt = TREE_VEC_ELT (newargs, i);
8031 tree ot = TREE_VEC_ELT (oldargs, i);
8033 if (! template_args_equal (ot, nt))
8035 if (oldarg_ptr != NULL)
8036 *oldarg_ptr = ot;
8037 if (newarg_ptr != NULL)
8038 *newarg_ptr = nt;
8039 return 0;
8042 return 1;
8045 static void
8046 add_pending_template (tree d)
8048 tree ti = (TYPE_P (d)
8049 ? CLASSTYPE_TEMPLATE_INFO (d)
8050 : DECL_TEMPLATE_INFO (d));
8051 struct pending_template *pt;
8052 int level;
8054 if (TI_PENDING_TEMPLATE_FLAG (ti))
8055 return;
8057 /* We are called both from instantiate_decl, where we've already had a
8058 tinst_level pushed, and instantiate_template, where we haven't.
8059 Compensate. */
8060 level = !current_tinst_level || current_tinst_level->decl != d;
8062 if (level)
8063 push_tinst_level (d);
8065 pt = ggc_alloc<pending_template> ();
8066 pt->next = NULL;
8067 pt->tinst = current_tinst_level;
8068 if (last_pending_template)
8069 last_pending_template->next = pt;
8070 else
8071 pending_templates = pt;
8073 last_pending_template = pt;
8075 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8077 if (level)
8078 pop_tinst_level ();
8082 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8083 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8084 documentation for TEMPLATE_ID_EXPR. */
8086 tree
8087 lookup_template_function (tree fns, tree arglist)
8089 tree type;
8091 if (fns == error_mark_node || arglist == error_mark_node)
8092 return error_mark_node;
8094 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8096 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8098 error ("%q#D is not a function template", fns);
8099 return error_mark_node;
8102 if (BASELINK_P (fns))
8104 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8105 unknown_type_node,
8106 BASELINK_FUNCTIONS (fns),
8107 arglist);
8108 return fns;
8111 type = TREE_TYPE (fns);
8112 if (TREE_CODE (fns) == OVERLOAD || !type)
8113 type = unknown_type_node;
8115 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8118 /* Within the scope of a template class S<T>, the name S gets bound
8119 (in build_self_reference) to a TYPE_DECL for the class, not a
8120 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8121 or one of its enclosing classes, and that type is a template,
8122 return the associated TEMPLATE_DECL. Otherwise, the original
8123 DECL is returned.
8125 Also handle the case when DECL is a TREE_LIST of ambiguous
8126 injected-class-names from different bases. */
8128 tree
8129 maybe_get_template_decl_from_type_decl (tree decl)
8131 if (decl == NULL_TREE)
8132 return decl;
8134 /* DR 176: A lookup that finds an injected-class-name (10.2
8135 [class.member.lookup]) can result in an ambiguity in certain cases
8136 (for example, if it is found in more than one base class). If all of
8137 the injected-class-names that are found refer to specializations of
8138 the same class template, and if the name is followed by a
8139 template-argument-list, the reference refers to the class template
8140 itself and not a specialization thereof, and is not ambiguous. */
8141 if (TREE_CODE (decl) == TREE_LIST)
8143 tree t, tmpl = NULL_TREE;
8144 for (t = decl; t; t = TREE_CHAIN (t))
8146 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8147 if (!tmpl)
8148 tmpl = elt;
8149 else if (tmpl != elt)
8150 break;
8152 if (tmpl && t == NULL_TREE)
8153 return tmpl;
8154 else
8155 return decl;
8158 return (decl != NULL_TREE
8159 && DECL_SELF_REFERENCE_P (decl)
8160 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8161 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8164 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8165 parameters, find the desired type.
8167 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8169 IN_DECL, if non-NULL, is the template declaration we are trying to
8170 instantiate.
8172 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8173 the class we are looking up.
8175 Issue error and warning messages under control of COMPLAIN.
8177 If the template class is really a local class in a template
8178 function, then the FUNCTION_CONTEXT is the function in which it is
8179 being instantiated.
8181 ??? Note that this function is currently called *twice* for each
8182 template-id: the first time from the parser, while creating the
8183 incomplete type (finish_template_type), and the second type during the
8184 real instantiation (instantiate_template_class). This is surely something
8185 that we want to avoid. It also causes some problems with argument
8186 coercion (see convert_nontype_argument for more information on this). */
8188 static tree
8189 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8190 int entering_scope, tsubst_flags_t complain)
8192 tree templ = NULL_TREE, parmlist;
8193 tree t;
8194 spec_entry **slot;
8195 spec_entry *entry;
8196 spec_entry elt;
8197 hashval_t hash;
8199 if (identifier_p (d1))
8201 tree value = innermost_non_namespace_value (d1);
8202 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8203 templ = value;
8204 else
8206 if (context)
8207 push_decl_namespace (context);
8208 templ = lookup_name (d1);
8209 templ = maybe_get_template_decl_from_type_decl (templ);
8210 if (context)
8211 pop_decl_namespace ();
8213 if (templ)
8214 context = DECL_CONTEXT (templ);
8216 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8218 tree type = TREE_TYPE (d1);
8220 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8221 an implicit typename for the second A. Deal with it. */
8222 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8223 type = TREE_TYPE (type);
8225 if (CLASSTYPE_TEMPLATE_INFO (type))
8227 templ = CLASSTYPE_TI_TEMPLATE (type);
8228 d1 = DECL_NAME (templ);
8231 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8232 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8234 templ = TYPE_TI_TEMPLATE (d1);
8235 d1 = DECL_NAME (templ);
8237 else if (DECL_TYPE_TEMPLATE_P (d1))
8239 templ = d1;
8240 d1 = DECL_NAME (templ);
8241 context = DECL_CONTEXT (templ);
8243 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8245 templ = d1;
8246 d1 = DECL_NAME (templ);
8249 /* Issue an error message if we didn't find a template. */
8250 if (! templ)
8252 if (complain & tf_error)
8253 error ("%qT is not a template", d1);
8254 return error_mark_node;
8257 if (TREE_CODE (templ) != TEMPLATE_DECL
8258 /* Make sure it's a user visible template, if it was named by
8259 the user. */
8260 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8261 && !PRIMARY_TEMPLATE_P (templ)))
8263 if (complain & tf_error)
8265 error ("non-template type %qT used as a template", d1);
8266 if (in_decl)
8267 error ("for template declaration %q+D", in_decl);
8269 return error_mark_node;
8272 complain &= ~tf_user;
8274 /* An alias that just changes the name of a template is equivalent to the
8275 other template, so if any of the arguments are pack expansions, strip
8276 the alias to avoid problems with a pack expansion passed to a non-pack
8277 alias template parameter (DR 1430). */
8278 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8279 templ = get_underlying_template (templ);
8281 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8283 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8284 template arguments */
8286 tree parm;
8287 tree arglist2;
8288 tree outer;
8290 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8292 /* Consider an example where a template template parameter declared as
8294 template <class T, class U = std::allocator<T> > class TT
8296 The template parameter level of T and U are one level larger than
8297 of TT. To proper process the default argument of U, say when an
8298 instantiation `TT<int>' is seen, we need to build the full
8299 arguments containing {int} as the innermost level. Outer levels,
8300 available when not appearing as default template argument, can be
8301 obtained from the arguments of the enclosing template.
8303 Suppose that TT is later substituted with std::vector. The above
8304 instantiation is `TT<int, std::allocator<T> >' with TT at
8305 level 1, and T at level 2, while the template arguments at level 1
8306 becomes {std::vector} and the inner level 2 is {int}. */
8308 outer = DECL_CONTEXT (templ);
8309 if (outer)
8310 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8311 else if (current_template_parms)
8313 /* This is an argument of the current template, so we haven't set
8314 DECL_CONTEXT yet. */
8315 tree relevant_template_parms;
8317 /* Parameter levels that are greater than the level of the given
8318 template template parm are irrelevant. */
8319 relevant_template_parms = current_template_parms;
8320 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8321 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8322 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8324 outer = template_parms_to_args (relevant_template_parms);
8327 if (outer)
8328 arglist = add_to_template_args (outer, arglist);
8330 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8331 complain,
8332 /*require_all_args=*/true,
8333 /*use_default_args=*/true);
8334 if (arglist2 == error_mark_node
8335 || (!uses_template_parms (arglist2)
8336 && check_instantiated_args (templ, arglist2, complain)))
8337 return error_mark_node;
8339 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8340 return parm;
8342 else
8344 tree template_type = TREE_TYPE (templ);
8345 tree gen_tmpl;
8346 tree type_decl;
8347 tree found = NULL_TREE;
8348 int arg_depth;
8349 int parm_depth;
8350 int is_dependent_type;
8351 int use_partial_inst_tmpl = false;
8353 if (template_type == error_mark_node)
8354 /* An error occurred while building the template TEMPL, and a
8355 diagnostic has most certainly been emitted for that
8356 already. Let's propagate that error. */
8357 return error_mark_node;
8359 gen_tmpl = most_general_template (templ);
8360 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8361 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8362 arg_depth = TMPL_ARGS_DEPTH (arglist);
8364 if (arg_depth == 1 && parm_depth > 1)
8366 /* We've been given an incomplete set of template arguments.
8367 For example, given:
8369 template <class T> struct S1 {
8370 template <class U> struct S2 {};
8371 template <class U> struct S2<U*> {};
8374 we will be called with an ARGLIST of `U*', but the
8375 TEMPLATE will be `template <class T> template
8376 <class U> struct S1<T>::S2'. We must fill in the missing
8377 arguments. */
8378 arglist
8379 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8380 arglist);
8381 arg_depth = TMPL_ARGS_DEPTH (arglist);
8384 /* Now we should have enough arguments. */
8385 gcc_assert (parm_depth == arg_depth);
8387 /* From here on, we're only interested in the most general
8388 template. */
8390 /* Calculate the BOUND_ARGS. These will be the args that are
8391 actually tsubst'd into the definition to create the
8392 instantiation. */
8393 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8394 complain,
8395 /*require_all_args=*/true,
8396 /*use_default_args=*/true);
8398 if (arglist == error_mark_node)
8399 /* We were unable to bind the arguments. */
8400 return error_mark_node;
8402 /* In the scope of a template class, explicit references to the
8403 template class refer to the type of the template, not any
8404 instantiation of it. For example, in:
8406 template <class T> class C { void f(C<T>); }
8408 the `C<T>' is just the same as `C'. Outside of the
8409 class, however, such a reference is an instantiation. */
8410 if ((entering_scope
8411 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8412 || currently_open_class (template_type))
8413 /* comp_template_args is expensive, check it last. */
8414 && comp_template_args (TYPE_TI_ARGS (template_type),
8415 arglist))
8416 return template_type;
8418 /* If we already have this specialization, return it. */
8419 elt.tmpl = gen_tmpl;
8420 elt.args = arglist;
8421 elt.spec = NULL_TREE;
8422 hash = spec_hasher::hash (&elt);
8423 entry = type_specializations->find_with_hash (&elt, hash);
8425 if (entry)
8426 return entry->spec;
8428 /* If the the template's constraints are not satisfied,
8429 then we cannot form a valid type.
8431 Note that the check is deferred until after the hash
8432 lookup. This prevents redundant checks on previously
8433 instantiated specializations. */
8434 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8436 if (complain & tf_error)
8438 error ("template constraint failure");
8439 diagnose_constraints (input_location, gen_tmpl, arglist);
8441 return error_mark_node;
8444 is_dependent_type = uses_template_parms (arglist);
8446 /* If the deduced arguments are invalid, then the binding
8447 failed. */
8448 if (!is_dependent_type
8449 && check_instantiated_args (gen_tmpl,
8450 INNERMOST_TEMPLATE_ARGS (arglist),
8451 complain))
8452 return error_mark_node;
8454 if (!is_dependent_type
8455 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8456 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8457 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8459 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8460 DECL_NAME (gen_tmpl),
8461 /*tag_scope=*/ts_global);
8462 return found;
8465 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8466 complain, in_decl);
8467 if (context == error_mark_node)
8468 return error_mark_node;
8470 if (!context)
8471 context = global_namespace;
8473 /* Create the type. */
8474 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8476 /* The user referred to a specialization of an alias
8477 template represented by GEN_TMPL.
8479 [temp.alias]/2 says:
8481 When a template-id refers to the specialization of an
8482 alias template, it is equivalent to the associated
8483 type obtained by substitution of its
8484 template-arguments for the template-parameters in the
8485 type-id of the alias template. */
8487 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8488 /* Note that the call above (by indirectly calling
8489 register_specialization in tsubst_decl) registers the
8490 TYPE_DECL representing the specialization of the alias
8491 template. So next time someone substitutes ARGLIST for
8492 the template parms into the alias template (GEN_TMPL),
8493 she'll get that TYPE_DECL back. */
8495 if (t == error_mark_node)
8496 return t;
8498 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8500 if (!is_dependent_type)
8502 set_current_access_from_decl (TYPE_NAME (template_type));
8503 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8504 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8505 arglist, complain, in_decl),
8506 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8507 arglist, complain, in_decl),
8508 SCOPED_ENUM_P (template_type), NULL);
8510 if (t == error_mark_node)
8511 return t;
8513 else
8515 /* We don't want to call start_enum for this type, since
8516 the values for the enumeration constants may involve
8517 template parameters. And, no one should be interested
8518 in the enumeration constants for such a type. */
8519 t = cxx_make_type (ENUMERAL_TYPE);
8520 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8522 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8523 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8524 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8526 else if (CLASS_TYPE_P (template_type))
8528 t = make_class_type (TREE_CODE (template_type));
8529 CLASSTYPE_DECLARED_CLASS (t)
8530 = CLASSTYPE_DECLARED_CLASS (template_type);
8531 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8533 /* A local class. Make sure the decl gets registered properly. */
8534 if (context == current_function_decl)
8535 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8537 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8538 /* This instantiation is another name for the primary
8539 template type. Set the TYPE_CANONICAL field
8540 appropriately. */
8541 TYPE_CANONICAL (t) = template_type;
8542 else if (any_template_arguments_need_structural_equality_p (arglist))
8543 /* Some of the template arguments require structural
8544 equality testing, so this template class requires
8545 structural equality testing. */
8546 SET_TYPE_STRUCTURAL_EQUALITY (t);
8548 else
8549 gcc_unreachable ();
8551 /* If we called start_enum or pushtag above, this information
8552 will already be set up. */
8553 if (!TYPE_NAME (t))
8555 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8557 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8558 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8559 DECL_SOURCE_LOCATION (type_decl)
8560 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8562 else
8563 type_decl = TYPE_NAME (t);
8565 if (CLASS_TYPE_P (template_type))
8567 TREE_PRIVATE (type_decl)
8568 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8569 TREE_PROTECTED (type_decl)
8570 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8571 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8573 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8574 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8578 if (OVERLOAD_TYPE_P (t)
8579 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8581 static const char *tags[] = {"abi_tag", "may_alias"};
8583 for (unsigned ix = 0; ix != 2; ix++)
8585 tree attributes
8586 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8588 if (attributes)
8589 TYPE_ATTRIBUTES (t)
8590 = tree_cons (TREE_PURPOSE (attributes),
8591 TREE_VALUE (attributes),
8592 TYPE_ATTRIBUTES (t));
8596 /* Let's consider the explicit specialization of a member
8597 of a class template specialization that is implicitly instantiated,
8598 e.g.:
8599 template<class T>
8600 struct S
8602 template<class U> struct M {}; //#0
8605 template<>
8606 template<>
8607 struct S<int>::M<char> //#1
8609 int i;
8611 [temp.expl.spec]/4 says this is valid.
8613 In this case, when we write:
8614 S<int>::M<char> m;
8616 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8617 the one of #0.
8619 When we encounter #1, we want to store the partial instantiation
8620 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8622 For all cases other than this "explicit specialization of member of a
8623 class template", we just want to store the most general template into
8624 the CLASSTYPE_TI_TEMPLATE of M.
8626 This case of "explicit specialization of member of a class template"
8627 only happens when:
8628 1/ the enclosing class is an instantiation of, and therefore not
8629 the same as, the context of the most general template, and
8630 2/ we aren't looking at the partial instantiation itself, i.e.
8631 the innermost arguments are not the same as the innermost parms of
8632 the most general template.
8634 So it's only when 1/ and 2/ happens that we want to use the partial
8635 instantiation of the member template in lieu of its most general
8636 template. */
8638 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8639 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8640 /* the enclosing class must be an instantiation... */
8641 && CLASS_TYPE_P (context)
8642 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8644 tree partial_inst_args;
8645 TREE_VEC_LENGTH (arglist)--;
8646 ++processing_template_decl;
8647 partial_inst_args =
8648 tsubst (INNERMOST_TEMPLATE_ARGS
8649 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8650 arglist, complain, NULL_TREE);
8651 --processing_template_decl;
8652 TREE_VEC_LENGTH (arglist)++;
8653 if (partial_inst_args == error_mark_node)
8654 return error_mark_node;
8655 use_partial_inst_tmpl =
8656 /*...and we must not be looking at the partial instantiation
8657 itself. */
8658 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8659 partial_inst_args);
8662 if (!use_partial_inst_tmpl)
8663 /* This case is easy; there are no member templates involved. */
8664 found = gen_tmpl;
8665 else
8667 /* This is a full instantiation of a member template. Find
8668 the partial instantiation of which this is an instance. */
8670 /* Temporarily reduce by one the number of levels in the ARGLIST
8671 so as to avoid comparing the last set of arguments. */
8672 TREE_VEC_LENGTH (arglist)--;
8673 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8674 TREE_VEC_LENGTH (arglist)++;
8675 /* FOUND is either a proper class type, or an alias
8676 template specialization. In the later case, it's a
8677 TYPE_DECL, resulting from the substituting of arguments
8678 for parameters in the TYPE_DECL of the alias template
8679 done earlier. So be careful while getting the template
8680 of FOUND. */
8681 found = TREE_CODE (found) == TEMPLATE_DECL
8682 ? found
8683 : TREE_CODE (found) == TYPE_DECL
8684 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8685 : CLASSTYPE_TI_TEMPLATE (found);
8688 // Build template info for the new specialization.
8689 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8691 elt.spec = t;
8692 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8693 entry = ggc_alloc<spec_entry> ();
8694 *entry = elt;
8695 *slot = entry;
8697 /* Note this use of the partial instantiation so we can check it
8698 later in maybe_process_partial_specialization. */
8699 DECL_TEMPLATE_INSTANTIATIONS (found)
8700 = tree_cons (arglist, t,
8701 DECL_TEMPLATE_INSTANTIATIONS (found));
8703 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8704 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8705 /* Now that the type has been registered on the instantiations
8706 list, we set up the enumerators. Because the enumeration
8707 constants may involve the enumeration type itself, we make
8708 sure to register the type first, and then create the
8709 constants. That way, doing tsubst_expr for the enumeration
8710 constants won't result in recursive calls here; we'll find
8711 the instantiation and exit above. */
8712 tsubst_enum (template_type, t, arglist);
8714 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8715 /* If the type makes use of template parameters, the
8716 code that generates debugging information will crash. */
8717 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8719 /* Possibly limit visibility based on template args. */
8720 TREE_PUBLIC (type_decl) = 1;
8721 determine_visibility (type_decl);
8723 inherit_targ_abi_tags (t);
8725 return t;
8729 /* Wrapper for lookup_template_class_1. */
8731 tree
8732 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8733 int entering_scope, tsubst_flags_t complain)
8735 tree ret;
8736 timevar_push (TV_TEMPLATE_INST);
8737 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8738 entering_scope, complain);
8739 timevar_pop (TV_TEMPLATE_INST);
8740 return ret;
8743 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8745 tree
8746 lookup_template_variable (tree templ, tree arglist)
8748 /* The type of the expression is NULL_TREE since the template-id could refer
8749 to an explicit or partial specialization. */
8750 tree type = NULL_TREE;
8751 if (flag_concepts && variable_concept_p (templ))
8752 /* Except that concepts are always bool. */
8753 type = boolean_type_node;
8754 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8757 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8759 tree
8760 finish_template_variable (tree var, tsubst_flags_t complain)
8762 tree templ = TREE_OPERAND (var, 0);
8763 tree arglist = TREE_OPERAND (var, 1);
8765 /* We never want to return a VAR_DECL for a variable concept, since they
8766 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8767 bool concept_p = flag_concepts && variable_concept_p (templ);
8768 if (concept_p && processing_template_decl)
8769 return var;
8771 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8772 arglist = add_outermost_template_args (tmpl_args, arglist);
8774 tree parms = DECL_TEMPLATE_PARMS (templ);
8775 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8776 /*req_all*/true,
8777 /*use_default*/true);
8779 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
8781 if (complain & tf_error)
8783 error ("use of invalid variable template %qE", var);
8784 diagnose_constraints (location_of (var), templ, arglist);
8786 return error_mark_node;
8789 /* If a template-id refers to a specialization of a variable
8790 concept, then the expression is true if and only if the
8791 concept's constraints are satisfied by the given template
8792 arguments.
8794 NOTE: This is an extension of Concepts Lite TS that
8795 allows constraints to be used in expressions. */
8796 if (concept_p)
8798 tree decl = DECL_TEMPLATE_RESULT (templ);
8799 return evaluate_variable_concept (decl, arglist);
8802 return instantiate_template (templ, arglist, complain);
8805 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
8806 TARGS template args, and instantiate it if it's not dependent. */
8808 static tree
8809 lookup_and_finish_template_variable (tree templ, tree targs,
8810 tsubst_flags_t complain)
8812 templ = lookup_template_variable (templ, targs);
8813 if (!any_dependent_template_arguments_p (targs))
8815 templ = finish_template_variable (templ, complain);
8816 mark_used (templ);
8819 return convert_from_reference (templ);
8823 struct pair_fn_data
8825 tree_fn_t fn;
8826 tree_fn_t any_fn;
8827 void *data;
8828 /* True when we should also visit template parameters that occur in
8829 non-deduced contexts. */
8830 bool include_nondeduced_p;
8831 hash_set<tree> *visited;
8834 /* Called from for_each_template_parm via walk_tree. */
8836 static tree
8837 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8839 tree t = *tp;
8840 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8841 tree_fn_t fn = pfd->fn;
8842 void *data = pfd->data;
8843 tree result = NULL_TREE;
8845 #define WALK_SUBTREE(NODE) \
8846 do \
8848 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8849 pfd->include_nondeduced_p, \
8850 pfd->any_fn); \
8851 if (result) goto out; \
8853 while (0)
8855 if (pfd->any_fn && (*pfd->any_fn)(t, data))
8856 return t;
8858 if (TYPE_P (t)
8859 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
8860 WALK_SUBTREE (TYPE_CONTEXT (t));
8862 switch (TREE_CODE (t))
8864 case RECORD_TYPE:
8865 if (TYPE_PTRMEMFUNC_P (t))
8866 break;
8867 /* Fall through. */
8869 case UNION_TYPE:
8870 case ENUMERAL_TYPE:
8871 if (!TYPE_TEMPLATE_INFO (t))
8872 *walk_subtrees = 0;
8873 else
8874 WALK_SUBTREE (TYPE_TI_ARGS (t));
8875 break;
8877 case INTEGER_TYPE:
8878 WALK_SUBTREE (TYPE_MIN_VALUE (t));
8879 WALK_SUBTREE (TYPE_MAX_VALUE (t));
8880 break;
8882 case METHOD_TYPE:
8883 /* Since we're not going to walk subtrees, we have to do this
8884 explicitly here. */
8885 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
8886 /* Fall through. */
8888 case FUNCTION_TYPE:
8889 /* Check the return type. */
8890 WALK_SUBTREE (TREE_TYPE (t));
8892 /* Check the parameter types. Since default arguments are not
8893 instantiated until they are needed, the TYPE_ARG_TYPES may
8894 contain expressions that involve template parameters. But,
8895 no-one should be looking at them yet. And, once they're
8896 instantiated, they don't contain template parameters, so
8897 there's no point in looking at them then, either. */
8899 tree parm;
8901 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8902 WALK_SUBTREE (TREE_VALUE (parm));
8904 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8905 want walk_tree walking into them itself. */
8906 *walk_subtrees = 0;
8909 if (flag_noexcept_type)
8911 tree spec = TYPE_RAISES_EXCEPTIONS (t);
8912 if (spec)
8913 WALK_SUBTREE (TREE_PURPOSE (spec));
8915 break;
8917 case TYPEOF_TYPE:
8918 case UNDERLYING_TYPE:
8919 if (pfd->include_nondeduced_p
8920 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8921 pfd->visited,
8922 pfd->include_nondeduced_p,
8923 pfd->any_fn))
8924 return error_mark_node;
8925 break;
8927 case FUNCTION_DECL:
8928 case VAR_DECL:
8929 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8930 WALK_SUBTREE (DECL_TI_ARGS (t));
8931 /* Fall through. */
8933 case PARM_DECL:
8934 case CONST_DECL:
8935 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
8936 WALK_SUBTREE (DECL_INITIAL (t));
8937 if (DECL_CONTEXT (t)
8938 && pfd->include_nondeduced_p)
8939 WALK_SUBTREE (DECL_CONTEXT (t));
8940 break;
8942 case BOUND_TEMPLATE_TEMPLATE_PARM:
8943 /* Record template parameters such as `T' inside `TT<T>'. */
8944 WALK_SUBTREE (TYPE_TI_ARGS (t));
8945 /* Fall through. */
8947 case TEMPLATE_TEMPLATE_PARM:
8948 case TEMPLATE_TYPE_PARM:
8949 case TEMPLATE_PARM_INDEX:
8950 if (fn && (*fn)(t, data))
8951 return t;
8952 else if (!fn)
8953 return t;
8955 /* In C++17 we can deduce a type argument from the type of a non-type
8956 argument. */
8957 if (cxx_dialect >= cxx1z
8958 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
8959 WALK_SUBTREE (TREE_TYPE (t));
8960 break;
8962 case TEMPLATE_DECL:
8963 /* A template template parameter is encountered. */
8964 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8965 WALK_SUBTREE (TREE_TYPE (t));
8967 /* Already substituted template template parameter */
8968 *walk_subtrees = 0;
8969 break;
8971 case TYPENAME_TYPE:
8972 /* A template-id in a TYPENAME_TYPE might be a deduced context after
8973 partial instantiation. */
8974 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
8975 break;
8977 case CONSTRUCTOR:
8978 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8979 && pfd->include_nondeduced_p)
8980 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
8981 break;
8983 case INDIRECT_REF:
8984 case COMPONENT_REF:
8985 /* If there's no type, then this thing must be some expression
8986 involving template parameters. */
8987 if (!fn && !TREE_TYPE (t))
8988 return error_mark_node;
8989 break;
8991 case MODOP_EXPR:
8992 case CAST_EXPR:
8993 case IMPLICIT_CONV_EXPR:
8994 case REINTERPRET_CAST_EXPR:
8995 case CONST_CAST_EXPR:
8996 case STATIC_CAST_EXPR:
8997 case DYNAMIC_CAST_EXPR:
8998 case ARROW_EXPR:
8999 case DOTSTAR_EXPR:
9000 case TYPEID_EXPR:
9001 case PSEUDO_DTOR_EXPR:
9002 if (!fn)
9003 return error_mark_node;
9004 break;
9006 default:
9007 break;
9010 #undef WALK_SUBTREE
9012 /* We didn't find any template parameters we liked. */
9013 out:
9014 return result;
9017 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9018 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9019 call FN with the parameter and the DATA.
9020 If FN returns nonzero, the iteration is terminated, and
9021 for_each_template_parm returns 1. Otherwise, the iteration
9022 continues. If FN never returns a nonzero value, the value
9023 returned by for_each_template_parm is 0. If FN is NULL, it is
9024 considered to be the function which always returns 1.
9026 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9027 parameters that occur in non-deduced contexts. When false, only
9028 visits those template parameters that can be deduced. */
9030 static tree
9031 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9032 hash_set<tree> *visited,
9033 bool include_nondeduced_p,
9034 tree_fn_t any_fn)
9036 struct pair_fn_data pfd;
9037 tree result;
9039 /* Set up. */
9040 pfd.fn = fn;
9041 pfd.any_fn = any_fn;
9042 pfd.data = data;
9043 pfd.include_nondeduced_p = include_nondeduced_p;
9045 /* Walk the tree. (Conceptually, we would like to walk without
9046 duplicates, but for_each_template_parm_r recursively calls
9047 for_each_template_parm, so we would need to reorganize a fair
9048 bit to use walk_tree_without_duplicates, so we keep our own
9049 visited list.) */
9050 if (visited)
9051 pfd.visited = visited;
9052 else
9053 pfd.visited = new hash_set<tree>;
9054 result = cp_walk_tree (&t,
9055 for_each_template_parm_r,
9056 &pfd,
9057 pfd.visited);
9059 /* Clean up. */
9060 if (!visited)
9062 delete pfd.visited;
9063 pfd.visited = 0;
9066 return result;
9069 /* Returns true if T depends on any template parameter. */
9072 uses_template_parms (tree t)
9074 if (t == NULL_TREE)
9075 return false;
9077 bool dependent_p;
9078 int saved_processing_template_decl;
9080 saved_processing_template_decl = processing_template_decl;
9081 if (!saved_processing_template_decl)
9082 processing_template_decl = 1;
9083 if (TYPE_P (t))
9084 dependent_p = dependent_type_p (t);
9085 else if (TREE_CODE (t) == TREE_VEC)
9086 dependent_p = any_dependent_template_arguments_p (t);
9087 else if (TREE_CODE (t) == TREE_LIST)
9088 dependent_p = (uses_template_parms (TREE_VALUE (t))
9089 || uses_template_parms (TREE_CHAIN (t)));
9090 else if (TREE_CODE (t) == TYPE_DECL)
9091 dependent_p = dependent_type_p (TREE_TYPE (t));
9092 else if (DECL_P (t)
9093 || EXPR_P (t)
9094 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9095 || TREE_CODE (t) == OVERLOAD
9096 || BASELINK_P (t)
9097 || identifier_p (t)
9098 || TREE_CODE (t) == TRAIT_EXPR
9099 || TREE_CODE (t) == CONSTRUCTOR
9100 || CONSTANT_CLASS_P (t))
9101 dependent_p = (type_dependent_expression_p (t)
9102 || value_dependent_expression_p (t));
9103 else
9105 gcc_assert (t == error_mark_node);
9106 dependent_p = false;
9109 processing_template_decl = saved_processing_template_decl;
9111 return dependent_p;
9114 /* Returns true iff current_function_decl is an incompletely instantiated
9115 template. Useful instead of processing_template_decl because the latter
9116 is set to 0 during instantiate_non_dependent_expr. */
9118 bool
9119 in_template_function (void)
9121 tree fn = current_function_decl;
9122 bool ret;
9123 ++processing_template_decl;
9124 ret = (fn && DECL_LANG_SPECIFIC (fn)
9125 && DECL_TEMPLATE_INFO (fn)
9126 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9127 --processing_template_decl;
9128 return ret;
9131 /* Returns true if T depends on any template parameter with level LEVEL. */
9133 bool
9134 uses_template_parms_level (tree t, int level)
9136 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9137 /*include_nondeduced_p=*/true);
9140 /* Returns true if the signature of DECL depends on any template parameter from
9141 its enclosing class. */
9143 bool
9144 uses_outer_template_parms (tree decl)
9146 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9147 if (depth == 0)
9148 return false;
9149 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9150 &depth, NULL, /*include_nondeduced_p=*/true))
9151 return true;
9152 if (PRIMARY_TEMPLATE_P (decl)
9153 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9154 (DECL_TEMPLATE_PARMS (decl)),
9155 template_parm_outer_level,
9156 &depth, NULL, /*include_nondeduced_p=*/true))
9157 return true;
9158 tree ci = get_constraints (decl);
9159 if (ci)
9160 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9161 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9162 &depth, NULL, /*nondeduced*/true))
9163 return true;
9164 return false;
9167 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9168 ill-formed translation unit, i.e. a variable or function that isn't
9169 usable in a constant expression. */
9171 static inline bool
9172 neglectable_inst_p (tree d)
9174 return (DECL_P (d)
9175 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9176 : decl_maybe_constant_var_p (d)));
9179 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9180 neglectable and instantiated from within an erroneous instantiation. */
9182 static bool
9183 limit_bad_template_recursion (tree decl)
9185 struct tinst_level *lev = current_tinst_level;
9186 int errs = errorcount + sorrycount;
9187 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9188 return false;
9190 for (; lev; lev = lev->next)
9191 if (neglectable_inst_p (lev->decl))
9192 break;
9194 return (lev && errs > lev->errors);
9197 static int tinst_depth;
9198 extern int max_tinst_depth;
9199 int depth_reached;
9201 static GTY(()) struct tinst_level *last_error_tinst_level;
9203 /* We're starting to instantiate D; record the template instantiation context
9204 for diagnostics and to restore it later. */
9206 bool
9207 push_tinst_level (tree d)
9209 return push_tinst_level_loc (d, input_location);
9212 /* We're starting to instantiate D; record the template instantiation context
9213 at LOC for diagnostics and to restore it later. */
9215 bool
9216 push_tinst_level_loc (tree d, location_t loc)
9218 struct tinst_level *new_level;
9220 if (tinst_depth >= max_tinst_depth)
9222 /* Tell error.c not to try to instantiate any templates. */
9223 at_eof = 2;
9224 fatal_error (input_location,
9225 "template instantiation depth exceeds maximum of %d"
9226 " (use -ftemplate-depth= to increase the maximum)",
9227 max_tinst_depth);
9228 return false;
9231 /* If the current instantiation caused problems, don't let it instantiate
9232 anything else. Do allow deduction substitution and decls usable in
9233 constant expressions. */
9234 if (limit_bad_template_recursion (d))
9235 return false;
9237 /* When not -quiet, dump template instantiations other than functions, since
9238 announce_function will take care of those. */
9239 if (!quiet_flag
9240 && TREE_CODE (d) != TREE_LIST
9241 && TREE_CODE (d) != FUNCTION_DECL)
9242 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9244 new_level = ggc_alloc<tinst_level> ();
9245 new_level->decl = d;
9246 new_level->locus = loc;
9247 new_level->errors = errorcount+sorrycount;
9248 new_level->in_system_header_p = in_system_header_at (input_location);
9249 new_level->next = current_tinst_level;
9250 current_tinst_level = new_level;
9252 ++tinst_depth;
9253 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9254 depth_reached = tinst_depth;
9256 return true;
9259 /* We're done instantiating this template; return to the instantiation
9260 context. */
9262 void
9263 pop_tinst_level (void)
9265 /* Restore the filename and line number stashed away when we started
9266 this instantiation. */
9267 input_location = current_tinst_level->locus;
9268 current_tinst_level = current_tinst_level->next;
9269 --tinst_depth;
9272 /* We're instantiating a deferred template; restore the template
9273 instantiation context in which the instantiation was requested, which
9274 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9276 static tree
9277 reopen_tinst_level (struct tinst_level *level)
9279 struct tinst_level *t;
9281 tinst_depth = 0;
9282 for (t = level; t; t = t->next)
9283 ++tinst_depth;
9285 current_tinst_level = level;
9286 pop_tinst_level ();
9287 if (current_tinst_level)
9288 current_tinst_level->errors = errorcount+sorrycount;
9289 return level->decl;
9292 /* Returns the TINST_LEVEL which gives the original instantiation
9293 context. */
9295 struct tinst_level *
9296 outermost_tinst_level (void)
9298 struct tinst_level *level = current_tinst_level;
9299 if (level)
9300 while (level->next)
9301 level = level->next;
9302 return level;
9305 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9306 vector of template arguments, as for tsubst.
9308 Returns an appropriate tsubst'd friend declaration. */
9310 static tree
9311 tsubst_friend_function (tree decl, tree args)
9313 tree new_friend;
9315 if (TREE_CODE (decl) == FUNCTION_DECL
9316 && DECL_TEMPLATE_INSTANTIATION (decl)
9317 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9318 /* This was a friend declared with an explicit template
9319 argument list, e.g.:
9321 friend void f<>(T);
9323 to indicate that f was a template instantiation, not a new
9324 function declaration. Now, we have to figure out what
9325 instantiation of what template. */
9327 tree template_id, arglist, fns;
9328 tree new_args;
9329 tree tmpl;
9330 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9332 /* Friend functions are looked up in the containing namespace scope.
9333 We must enter that scope, to avoid finding member functions of the
9334 current class with same name. */
9335 push_nested_namespace (ns);
9336 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9337 tf_warning_or_error, NULL_TREE,
9338 /*integral_constant_expression_p=*/false);
9339 pop_nested_namespace (ns);
9340 arglist = tsubst (DECL_TI_ARGS (decl), args,
9341 tf_warning_or_error, NULL_TREE);
9342 template_id = lookup_template_function (fns, arglist);
9344 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9345 tmpl = determine_specialization (template_id, new_friend,
9346 &new_args,
9347 /*need_member_template=*/0,
9348 TREE_VEC_LENGTH (args),
9349 tsk_none);
9350 return instantiate_template (tmpl, new_args, tf_error);
9353 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9355 /* The NEW_FRIEND will look like an instantiation, to the
9356 compiler, but is not an instantiation from the point of view of
9357 the language. For example, we might have had:
9359 template <class T> struct S {
9360 template <class U> friend void f(T, U);
9363 Then, in S<int>, template <class U> void f(int, U) is not an
9364 instantiation of anything. */
9365 if (new_friend == error_mark_node)
9366 return error_mark_node;
9368 DECL_USE_TEMPLATE (new_friend) = 0;
9369 if (TREE_CODE (decl) == TEMPLATE_DECL)
9371 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9372 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9373 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9376 /* The mangled name for the NEW_FRIEND is incorrect. The function
9377 is not a template instantiation and should not be mangled like
9378 one. Therefore, we forget the mangling here; we'll recompute it
9379 later if we need it. */
9380 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9382 SET_DECL_RTL (new_friend, NULL);
9383 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9386 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9388 tree old_decl;
9389 tree new_friend_template_info;
9390 tree new_friend_result_template_info;
9391 tree ns;
9392 int new_friend_is_defn;
9394 /* We must save some information from NEW_FRIEND before calling
9395 duplicate decls since that function will free NEW_FRIEND if
9396 possible. */
9397 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9398 new_friend_is_defn =
9399 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9400 (template_for_substitution (new_friend)))
9401 != NULL_TREE);
9402 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9404 /* This declaration is a `primary' template. */
9405 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9407 new_friend_result_template_info
9408 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9410 else
9411 new_friend_result_template_info = NULL_TREE;
9413 /* Inside pushdecl_namespace_level, we will push into the
9414 current namespace. However, the friend function should go
9415 into the namespace of the template. */
9416 ns = decl_namespace_context (new_friend);
9417 push_nested_namespace (ns);
9418 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9419 pop_nested_namespace (ns);
9421 if (old_decl == error_mark_node)
9422 return error_mark_node;
9424 if (old_decl != new_friend)
9426 /* This new friend declaration matched an existing
9427 declaration. For example, given:
9429 template <class T> void f(T);
9430 template <class U> class C {
9431 template <class T> friend void f(T) {}
9434 the friend declaration actually provides the definition
9435 of `f', once C has been instantiated for some type. So,
9436 old_decl will be the out-of-class template declaration,
9437 while new_friend is the in-class definition.
9439 But, if `f' was called before this point, the
9440 instantiation of `f' will have DECL_TI_ARGS corresponding
9441 to `T' but not to `U', references to which might appear
9442 in the definition of `f'. Previously, the most general
9443 template for an instantiation of `f' was the out-of-class
9444 version; now it is the in-class version. Therefore, we
9445 run through all specialization of `f', adding to their
9446 DECL_TI_ARGS appropriately. In particular, they need a
9447 new set of outer arguments, corresponding to the
9448 arguments for this class instantiation.
9450 The same situation can arise with something like this:
9452 friend void f(int);
9453 template <class T> class C {
9454 friend void f(T) {}
9457 when `C<int>' is instantiated. Now, `f(int)' is defined
9458 in the class. */
9460 if (!new_friend_is_defn)
9461 /* On the other hand, if the in-class declaration does
9462 *not* provide a definition, then we don't want to alter
9463 existing definitions. We can just leave everything
9464 alone. */
9466 else
9468 tree new_template = TI_TEMPLATE (new_friend_template_info);
9469 tree new_args = TI_ARGS (new_friend_template_info);
9471 /* Overwrite whatever template info was there before, if
9472 any, with the new template information pertaining to
9473 the declaration. */
9474 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9476 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9478 /* We should have called reregister_specialization in
9479 duplicate_decls. */
9480 gcc_assert (retrieve_specialization (new_template,
9481 new_args, 0)
9482 == old_decl);
9484 /* Instantiate it if the global has already been used. */
9485 if (DECL_ODR_USED (old_decl))
9486 instantiate_decl (old_decl, /*defer_ok=*/true,
9487 /*expl_inst_class_mem_p=*/false);
9489 else
9491 tree t;
9493 /* Indicate that the old function template is a partial
9494 instantiation. */
9495 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9496 = new_friend_result_template_info;
9498 gcc_assert (new_template
9499 == most_general_template (new_template));
9500 gcc_assert (new_template != old_decl);
9502 /* Reassign any specializations already in the hash table
9503 to the new more general template, and add the
9504 additional template args. */
9505 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9506 t != NULL_TREE;
9507 t = TREE_CHAIN (t))
9509 tree spec = TREE_VALUE (t);
9510 spec_entry elt;
9512 elt.tmpl = old_decl;
9513 elt.args = DECL_TI_ARGS (spec);
9514 elt.spec = NULL_TREE;
9516 decl_specializations->remove_elt (&elt);
9518 DECL_TI_ARGS (spec)
9519 = add_outermost_template_args (new_args,
9520 DECL_TI_ARGS (spec));
9522 register_specialization
9523 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9526 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9530 /* The information from NEW_FRIEND has been merged into OLD_DECL
9531 by duplicate_decls. */
9532 new_friend = old_decl;
9535 else
9537 tree context = DECL_CONTEXT (new_friend);
9538 bool dependent_p;
9540 /* In the code
9541 template <class T> class C {
9542 template <class U> friend void C1<U>::f (); // case 1
9543 friend void C2<T>::f (); // case 2
9545 we only need to make sure CONTEXT is a complete type for
9546 case 2. To distinguish between the two cases, we note that
9547 CONTEXT of case 1 remains dependent type after tsubst while
9548 this isn't true for case 2. */
9549 ++processing_template_decl;
9550 dependent_p = dependent_type_p (context);
9551 --processing_template_decl;
9553 if (!dependent_p
9554 && !complete_type_or_else (context, NULL_TREE))
9555 return error_mark_node;
9557 if (COMPLETE_TYPE_P (context))
9559 tree fn = new_friend;
9560 /* do_friend adds the TEMPLATE_DECL for any member friend
9561 template even if it isn't a member template, i.e.
9562 template <class T> friend A<T>::f();
9563 Look through it in that case. */
9564 if (TREE_CODE (fn) == TEMPLATE_DECL
9565 && !PRIMARY_TEMPLATE_P (fn))
9566 fn = DECL_TEMPLATE_RESULT (fn);
9567 /* Check to see that the declaration is really present, and,
9568 possibly obtain an improved declaration. */
9569 fn = check_classfn (context, fn, NULL_TREE);
9571 if (fn)
9572 new_friend = fn;
9576 return new_friend;
9579 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9580 template arguments, as for tsubst.
9582 Returns an appropriate tsubst'd friend type or error_mark_node on
9583 failure. */
9585 static tree
9586 tsubst_friend_class (tree friend_tmpl, tree args)
9588 tree friend_type;
9589 tree tmpl;
9590 tree context;
9592 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9594 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9595 return TREE_TYPE (t);
9598 context = CP_DECL_CONTEXT (friend_tmpl);
9600 if (context != global_namespace)
9602 if (TREE_CODE (context) == NAMESPACE_DECL)
9603 push_nested_namespace (context);
9604 else
9605 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9608 /* Look for a class template declaration. We look for hidden names
9609 because two friend declarations of the same template are the
9610 same. For example, in:
9612 struct A {
9613 template <typename> friend class F;
9615 template <typename> struct B {
9616 template <typename> friend class F;
9619 both F templates are the same. */
9620 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9621 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9623 /* But, if we don't find one, it might be because we're in a
9624 situation like this:
9626 template <class T>
9627 struct S {
9628 template <class U>
9629 friend struct S;
9632 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9633 for `S<int>', not the TEMPLATE_DECL. */
9634 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9636 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9637 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9640 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9642 /* The friend template has already been declared. Just
9643 check to see that the declarations match, and install any new
9644 default parameters. We must tsubst the default parameters,
9645 of course. We only need the innermost template parameters
9646 because that is all that redeclare_class_template will look
9647 at. */
9648 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9649 > TMPL_ARGS_DEPTH (args))
9651 tree parms;
9652 location_t saved_input_location;
9653 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9654 args, tf_warning_or_error);
9656 saved_input_location = input_location;
9657 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9658 tree cons = get_constraints (tmpl);
9659 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9660 input_location = saved_input_location;
9664 friend_type = TREE_TYPE (tmpl);
9666 else
9668 /* The friend template has not already been declared. In this
9669 case, the instantiation of the template class will cause the
9670 injection of this template into the global scope. */
9671 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9672 if (tmpl == error_mark_node)
9673 return error_mark_node;
9675 /* The new TMPL is not an instantiation of anything, so we
9676 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9677 the new type because that is supposed to be the corresponding
9678 template decl, i.e., TMPL. */
9679 DECL_USE_TEMPLATE (tmpl) = 0;
9680 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9681 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9682 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9683 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9685 /* Inject this template into the global scope. */
9686 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9689 if (context != global_namespace)
9691 if (TREE_CODE (context) == NAMESPACE_DECL)
9692 pop_nested_namespace (context);
9693 else
9694 pop_nested_class ();
9697 return friend_type;
9700 /* Returns zero if TYPE cannot be completed later due to circularity.
9701 Otherwise returns one. */
9703 static int
9704 can_complete_type_without_circularity (tree type)
9706 if (type == NULL_TREE || type == error_mark_node)
9707 return 0;
9708 else if (COMPLETE_TYPE_P (type))
9709 return 1;
9710 else if (TREE_CODE (type) == ARRAY_TYPE)
9711 return can_complete_type_without_circularity (TREE_TYPE (type));
9712 else if (CLASS_TYPE_P (type)
9713 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9714 return 0;
9715 else
9716 return 1;
9719 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9720 tsubst_flags_t, tree);
9722 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9723 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9725 static tree
9726 tsubst_attribute (tree t, tree *decl_p, tree args,
9727 tsubst_flags_t complain, tree in_decl)
9729 gcc_assert (ATTR_IS_DEPENDENT (t));
9731 tree val = TREE_VALUE (t);
9732 if (val == NULL_TREE)
9733 /* Nothing to do. */;
9734 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9735 && is_attribute_p ("omp declare simd",
9736 get_attribute_name (t)))
9738 tree clauses = TREE_VALUE (val);
9739 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9740 complain, in_decl);
9741 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9742 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9743 tree parms = DECL_ARGUMENTS (*decl_p);
9744 clauses
9745 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9746 if (clauses)
9747 val = build_tree_list (NULL_TREE, clauses);
9748 else
9749 val = NULL_TREE;
9751 /* If the first attribute argument is an identifier, don't
9752 pass it through tsubst. Attributes like mode, format,
9753 cleanup and several target specific attributes expect it
9754 unmodified. */
9755 else if (attribute_takes_identifier_p (get_attribute_name (t)))
9757 tree chain
9758 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
9759 /*integral_constant_expression_p=*/false);
9760 if (chain != TREE_CHAIN (val))
9761 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
9763 else if (PACK_EXPANSION_P (val))
9765 /* An attribute pack expansion. */
9766 tree purp = TREE_PURPOSE (t);
9767 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
9768 int len = TREE_VEC_LENGTH (pack);
9769 tree list = NULL_TREE;
9770 tree *q = &list;
9771 for (int i = 0; i < len; ++i)
9773 tree elt = TREE_VEC_ELT (pack, i);
9774 *q = build_tree_list (purp, elt);
9775 q = &TREE_CHAIN (*q);
9777 return list;
9779 else
9780 val = tsubst_expr (val, args, complain, in_decl,
9781 /*integral_constant_expression_p=*/false);
9783 if (val != TREE_VALUE (t))
9784 return build_tree_list (TREE_PURPOSE (t), val);
9785 return t;
9788 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
9789 unchanged or a new TREE_LIST chain. */
9791 static tree
9792 tsubst_attributes (tree attributes, tree args,
9793 tsubst_flags_t complain, tree in_decl)
9795 tree last_dep = NULL_TREE;
9797 for (tree t = attributes; t; t = TREE_CHAIN (t))
9798 if (ATTR_IS_DEPENDENT (t))
9800 last_dep = t;
9801 attributes = copy_list (attributes);
9802 break;
9805 if (last_dep)
9806 for (tree *p = &attributes; *p; )
9808 tree t = *p;
9809 if (ATTR_IS_DEPENDENT (t))
9811 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
9812 if (subst != t)
9814 *p = subst;
9816 p = &TREE_CHAIN (*p);
9817 while (*p);
9818 *p = TREE_CHAIN (t);
9819 continue;
9822 p = &TREE_CHAIN (*p);
9825 return attributes;
9828 /* Apply any attributes which had to be deferred until instantiation
9829 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9830 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9832 static void
9833 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9834 tree args, tsubst_flags_t complain, tree in_decl)
9836 tree last_dep = NULL_TREE;
9837 tree t;
9838 tree *p;
9840 for (t = attributes; t; t = TREE_CHAIN (t))
9841 if (ATTR_IS_DEPENDENT (t))
9843 last_dep = t;
9844 attributes = copy_list (attributes);
9845 break;
9848 if (DECL_P (*decl_p))
9850 if (TREE_TYPE (*decl_p) == error_mark_node)
9851 return;
9852 p = &DECL_ATTRIBUTES (*decl_p);
9854 else
9855 p = &TYPE_ATTRIBUTES (*decl_p);
9857 if (last_dep)
9859 tree late_attrs = NULL_TREE;
9860 tree *q = &late_attrs;
9862 for (*p = attributes; *p; )
9864 t = *p;
9865 if (ATTR_IS_DEPENDENT (t))
9867 *p = TREE_CHAIN (t);
9868 TREE_CHAIN (t) = NULL_TREE;
9869 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
9871 q = &TREE_CHAIN (*q);
9872 while (*q);
9874 else
9875 p = &TREE_CHAIN (t);
9878 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9882 /* Perform (or defer) access check for typedefs that were referenced
9883 from within the template TMPL code.
9884 This is a subroutine of instantiate_decl and instantiate_class_template.
9885 TMPL is the template to consider and TARGS is the list of arguments of
9886 that template. */
9888 static void
9889 perform_typedefs_access_check (tree tmpl, tree targs)
9891 location_t saved_location;
9892 unsigned i;
9893 qualified_typedef_usage_t *iter;
9895 if (!tmpl
9896 || (!CLASS_TYPE_P (tmpl)
9897 && TREE_CODE (tmpl) != FUNCTION_DECL))
9898 return;
9900 saved_location = input_location;
9901 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9903 tree type_decl = iter->typedef_decl;
9904 tree type_scope = iter->context;
9906 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9907 continue;
9909 if (uses_template_parms (type_decl))
9910 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9911 if (uses_template_parms (type_scope))
9912 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9914 /* Make access check error messages point to the location
9915 of the use of the typedef. */
9916 input_location = iter->locus;
9917 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9918 type_decl, type_decl,
9919 tf_warning_or_error);
9921 input_location = saved_location;
9924 static tree
9925 instantiate_class_template_1 (tree type)
9927 tree templ, args, pattern, t, member;
9928 tree typedecl;
9929 tree pbinfo;
9930 tree base_list;
9931 unsigned int saved_maximum_field_alignment;
9932 tree fn_context;
9934 if (type == error_mark_node)
9935 return error_mark_node;
9937 if (COMPLETE_OR_OPEN_TYPE_P (type)
9938 || uses_template_parms (type))
9939 return type;
9941 /* Figure out which template is being instantiated. */
9942 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9943 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9945 /* Determine what specialization of the original template to
9946 instantiate. */
9947 t = most_specialized_partial_spec (type, tf_warning_or_error);
9948 if (t == error_mark_node)
9950 TYPE_BEING_DEFINED (type) = 1;
9951 return error_mark_node;
9953 else if (t)
9955 /* This TYPE is actually an instantiation of a partial
9956 specialization. We replace the innermost set of ARGS with
9957 the arguments appropriate for substitution. For example,
9958 given:
9960 template <class T> struct S {};
9961 template <class T> struct S<T*> {};
9963 and supposing that we are instantiating S<int*>, ARGS will
9964 presently be {int*} -- but we need {int}. */
9965 pattern = TREE_TYPE (t);
9966 args = TREE_PURPOSE (t);
9968 else
9970 pattern = TREE_TYPE (templ);
9971 args = CLASSTYPE_TI_ARGS (type);
9974 /* If the template we're instantiating is incomplete, then clearly
9975 there's nothing we can do. */
9976 if (!COMPLETE_TYPE_P (pattern))
9977 return type;
9979 /* If we've recursively instantiated too many templates, stop. */
9980 if (! push_tinst_level (type))
9981 return type;
9983 /* Now we're really doing the instantiation. Mark the type as in
9984 the process of being defined. */
9985 TYPE_BEING_DEFINED (type) = 1;
9987 /* We may be in the middle of deferred access check. Disable
9988 it now. */
9989 push_deferring_access_checks (dk_no_deferred);
9991 int saved_unevaluated_operand = cp_unevaluated_operand;
9992 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9994 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9995 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9996 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9997 fn_context = error_mark_node;
9998 if (!fn_context)
9999 push_to_top_level ();
10000 else
10002 cp_unevaluated_operand = 0;
10003 c_inhibit_evaluation_warnings = 0;
10005 /* Use #pragma pack from the template context. */
10006 saved_maximum_field_alignment = maximum_field_alignment;
10007 maximum_field_alignment = TYPE_PRECISION (pattern);
10009 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10011 /* Set the input location to the most specialized template definition.
10012 This is needed if tsubsting causes an error. */
10013 typedecl = TYPE_MAIN_DECL (pattern);
10014 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10015 DECL_SOURCE_LOCATION (typedecl);
10017 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10018 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10019 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10020 if (ANON_AGGR_TYPE_P (pattern))
10021 SET_ANON_AGGR_TYPE_P (type);
10022 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10024 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10025 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10026 /* Adjust visibility for template arguments. */
10027 determine_visibility (TYPE_MAIN_DECL (type));
10029 if (CLASS_TYPE_P (type))
10030 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10032 pbinfo = TYPE_BINFO (pattern);
10034 /* We should never instantiate a nested class before its enclosing
10035 class; we need to look up the nested class by name before we can
10036 instantiate it, and that lookup should instantiate the enclosing
10037 class. */
10038 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10039 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10041 base_list = NULL_TREE;
10042 if (BINFO_N_BASE_BINFOS (pbinfo))
10044 tree pbase_binfo;
10045 tree pushed_scope;
10046 int i;
10048 /* We must enter the scope containing the type, as that is where
10049 the accessibility of types named in dependent bases are
10050 looked up from. */
10051 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10053 /* Substitute into each of the bases to determine the actual
10054 basetypes. */
10055 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10057 tree base;
10058 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10059 tree expanded_bases = NULL_TREE;
10060 int idx, len = 1;
10062 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10064 expanded_bases =
10065 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10066 args, tf_error, NULL_TREE);
10067 if (expanded_bases == error_mark_node)
10068 continue;
10070 len = TREE_VEC_LENGTH (expanded_bases);
10073 for (idx = 0; idx < len; idx++)
10075 if (expanded_bases)
10076 /* Extract the already-expanded base class. */
10077 base = TREE_VEC_ELT (expanded_bases, idx);
10078 else
10079 /* Substitute to figure out the base class. */
10080 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10081 NULL_TREE);
10083 if (base == error_mark_node)
10084 continue;
10086 base_list = tree_cons (access, base, base_list);
10087 if (BINFO_VIRTUAL_P (pbase_binfo))
10088 TREE_TYPE (base_list) = integer_type_node;
10092 /* The list is now in reverse order; correct that. */
10093 base_list = nreverse (base_list);
10095 if (pushed_scope)
10096 pop_scope (pushed_scope);
10098 /* Now call xref_basetypes to set up all the base-class
10099 information. */
10100 xref_basetypes (type, base_list);
10102 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10103 (int) ATTR_FLAG_TYPE_IN_PLACE,
10104 args, tf_error, NULL_TREE);
10105 fixup_attribute_variants (type);
10107 /* Now that our base classes are set up, enter the scope of the
10108 class, so that name lookups into base classes, etc. will work
10109 correctly. This is precisely analogous to what we do in
10110 begin_class_definition when defining an ordinary non-template
10111 class, except we also need to push the enclosing classes. */
10112 push_nested_class (type);
10114 /* Now members are processed in the order of declaration. */
10115 for (member = CLASSTYPE_DECL_LIST (pattern);
10116 member; member = TREE_CHAIN (member))
10118 tree t = TREE_VALUE (member);
10120 if (TREE_PURPOSE (member))
10122 if (TYPE_P (t))
10124 /* Build new CLASSTYPE_NESTED_UTDS. */
10126 tree newtag;
10127 bool class_template_p;
10129 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10130 && TYPE_LANG_SPECIFIC (t)
10131 && CLASSTYPE_IS_TEMPLATE (t));
10132 /* If the member is a class template, then -- even after
10133 substitution -- there may be dependent types in the
10134 template argument list for the class. We increment
10135 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10136 that function will assume that no types are dependent
10137 when outside of a template. */
10138 if (class_template_p)
10139 ++processing_template_decl;
10140 newtag = tsubst (t, args, tf_error, NULL_TREE);
10141 if (class_template_p)
10142 --processing_template_decl;
10143 if (newtag == error_mark_node)
10144 continue;
10146 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10148 tree name = TYPE_IDENTIFIER (t);
10150 if (class_template_p)
10151 /* Unfortunately, lookup_template_class sets
10152 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10153 instantiation (i.e., for the type of a member
10154 template class nested within a template class.)
10155 This behavior is required for
10156 maybe_process_partial_specialization to work
10157 correctly, but is not accurate in this case;
10158 the TAG is not an instantiation of anything.
10159 (The corresponding TEMPLATE_DECL is an
10160 instantiation, but the TYPE is not.) */
10161 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10163 /* Now, we call pushtag to put this NEWTAG into the scope of
10164 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10165 pushtag calling push_template_decl. We don't have to do
10166 this for enums because it will already have been done in
10167 tsubst_enum. */
10168 if (name)
10169 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10170 pushtag (name, newtag, /*tag_scope=*/ts_current);
10173 else if (DECL_DECLARES_FUNCTION_P (t))
10175 /* Build new TYPE_METHODS. */
10176 tree r;
10178 if (TREE_CODE (t) == TEMPLATE_DECL)
10179 ++processing_template_decl;
10180 r = tsubst (t, args, tf_error, NULL_TREE);
10181 if (TREE_CODE (t) == TEMPLATE_DECL)
10182 --processing_template_decl;
10183 set_current_access_from_decl (r);
10184 finish_member_declaration (r);
10185 /* Instantiate members marked with attribute used. */
10186 if (r != error_mark_node && DECL_PRESERVE_P (r))
10187 mark_used (r);
10188 if (TREE_CODE (r) == FUNCTION_DECL
10189 && DECL_OMP_DECLARE_REDUCTION_P (r))
10190 cp_check_omp_declare_reduction (r);
10192 else if (DECL_CLASS_TEMPLATE_P (t)
10193 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10194 /* A closure type for a lambda in a default argument for a
10195 member template. Ignore it; it will be instantiated with
10196 the default argument. */;
10197 else
10199 /* Build new TYPE_FIELDS. */
10200 if (TREE_CODE (t) == STATIC_ASSERT)
10202 tree condition;
10204 ++c_inhibit_evaluation_warnings;
10205 condition =
10206 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10207 tf_warning_or_error, NULL_TREE,
10208 /*integral_constant_expression_p=*/true);
10209 --c_inhibit_evaluation_warnings;
10211 finish_static_assert (condition,
10212 STATIC_ASSERT_MESSAGE (t),
10213 STATIC_ASSERT_SOURCE_LOCATION (t),
10214 /*member_p=*/true);
10216 else if (TREE_CODE (t) != CONST_DECL)
10218 tree r;
10219 tree vec = NULL_TREE;
10220 int len = 1;
10222 /* The file and line for this declaration, to
10223 assist in error message reporting. Since we
10224 called push_tinst_level above, we don't need to
10225 restore these. */
10226 input_location = DECL_SOURCE_LOCATION (t);
10228 if (TREE_CODE (t) == TEMPLATE_DECL)
10229 ++processing_template_decl;
10230 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10231 if (TREE_CODE (t) == TEMPLATE_DECL)
10232 --processing_template_decl;
10234 if (TREE_CODE (r) == TREE_VEC)
10236 /* A capture pack became multiple fields. */
10237 vec = r;
10238 len = TREE_VEC_LENGTH (vec);
10241 for (int i = 0; i < len; ++i)
10243 if (vec)
10244 r = TREE_VEC_ELT (vec, i);
10245 if (VAR_P (r))
10247 /* In [temp.inst]:
10249 [t]he initialization (and any associated
10250 side-effects) of a static data member does
10251 not occur unless the static data member is
10252 itself used in a way that requires the
10253 definition of the static data member to
10254 exist.
10256 Therefore, we do not substitute into the
10257 initialized for the static data member here. */
10258 finish_static_data_member_decl
10260 /*init=*/NULL_TREE,
10261 /*init_const_expr_p=*/false,
10262 /*asmspec_tree=*/NULL_TREE,
10263 /*flags=*/0);
10264 /* Instantiate members marked with attribute used. */
10265 if (r != error_mark_node && DECL_PRESERVE_P (r))
10266 mark_used (r);
10268 else if (TREE_CODE (r) == FIELD_DECL)
10270 /* Determine whether R has a valid type and can be
10271 completed later. If R is invalid, then its type
10272 is replaced by error_mark_node. */
10273 tree rtype = TREE_TYPE (r);
10274 if (can_complete_type_without_circularity (rtype))
10275 complete_type (rtype);
10277 if (!complete_or_array_type_p (rtype))
10279 /* If R's type couldn't be completed and
10280 it isn't a flexible array member (whose
10281 type is incomplete by definition) give
10282 an error. */
10283 cxx_incomplete_type_error (r, rtype);
10284 TREE_TYPE (r) = error_mark_node;
10288 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10289 such a thing will already have been added to the field
10290 list by tsubst_enum in finish_member_declaration in the
10291 CLASSTYPE_NESTED_UTDS case above. */
10292 if (!(TREE_CODE (r) == TYPE_DECL
10293 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10294 && DECL_ARTIFICIAL (r)))
10296 set_current_access_from_decl (r);
10297 finish_member_declaration (r);
10303 else
10305 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10306 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10308 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10310 tree friend_type = t;
10311 bool adjust_processing_template_decl = false;
10313 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10315 /* template <class T> friend class C; */
10316 friend_type = tsubst_friend_class (friend_type, args);
10317 adjust_processing_template_decl = true;
10319 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10321 /* template <class T> friend class C::D; */
10322 friend_type = tsubst (friend_type, args,
10323 tf_warning_or_error, NULL_TREE);
10324 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10325 friend_type = TREE_TYPE (friend_type);
10326 adjust_processing_template_decl = true;
10328 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10329 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10331 /* This could be either
10333 friend class T::C;
10335 when dependent_type_p is false or
10337 template <class U> friend class T::C;
10339 otherwise. */
10340 /* Bump processing_template_decl in case this is something like
10341 template <class T> friend struct A<T>::B. */
10342 ++processing_template_decl;
10343 friend_type = tsubst (friend_type, args,
10344 tf_warning_or_error, NULL_TREE);
10345 if (dependent_type_p (friend_type))
10346 adjust_processing_template_decl = true;
10347 --processing_template_decl;
10349 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10350 && hidden_name_p (TYPE_NAME (friend_type)))
10352 /* friend class C;
10354 where C hasn't been declared yet. Let's lookup name
10355 from namespace scope directly, bypassing any name that
10356 come from dependent base class. */
10357 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10359 /* The call to xref_tag_from_type does injection for friend
10360 classes. */
10361 push_nested_namespace (ns);
10362 friend_type =
10363 xref_tag_from_type (friend_type, NULL_TREE,
10364 /*tag_scope=*/ts_current);
10365 pop_nested_namespace (ns);
10367 else if (uses_template_parms (friend_type))
10368 /* friend class C<T>; */
10369 friend_type = tsubst (friend_type, args,
10370 tf_warning_or_error, NULL_TREE);
10371 /* Otherwise it's
10373 friend class C;
10375 where C is already declared or
10377 friend class C<int>;
10379 We don't have to do anything in these cases. */
10381 if (adjust_processing_template_decl)
10382 /* Trick make_friend_class into realizing that the friend
10383 we're adding is a template, not an ordinary class. It's
10384 important that we use make_friend_class since it will
10385 perform some error-checking and output cross-reference
10386 information. */
10387 ++processing_template_decl;
10389 if (friend_type != error_mark_node)
10390 make_friend_class (type, friend_type, /*complain=*/false);
10392 if (adjust_processing_template_decl)
10393 --processing_template_decl;
10395 else
10397 /* Build new DECL_FRIENDLIST. */
10398 tree r;
10400 /* The file and line for this declaration, to
10401 assist in error message reporting. Since we
10402 called push_tinst_level above, we don't need to
10403 restore these. */
10404 input_location = DECL_SOURCE_LOCATION (t);
10406 if (TREE_CODE (t) == TEMPLATE_DECL)
10408 ++processing_template_decl;
10409 push_deferring_access_checks (dk_no_check);
10412 r = tsubst_friend_function (t, args);
10413 add_friend (type, r, /*complain=*/false);
10414 if (TREE_CODE (t) == TEMPLATE_DECL)
10416 pop_deferring_access_checks ();
10417 --processing_template_decl;
10423 if (fn_context)
10425 /* Restore these before substituting into the lambda capture
10426 initializers. */
10427 cp_unevaluated_operand = saved_unevaluated_operand;
10428 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10431 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10433 tree decl = lambda_function (type);
10434 if (decl)
10436 if (cxx_dialect >= cxx1z)
10437 CLASSTYPE_LITERAL_P (type) = true;
10439 if (!DECL_TEMPLATE_INFO (decl)
10440 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10442 /* Set function_depth to avoid garbage collection. */
10443 ++function_depth;
10444 instantiate_decl (decl, false, false);
10445 --function_depth;
10448 /* We need to instantiate the capture list from the template
10449 after we've instantiated the closure members, but before we
10450 consider adding the conversion op. Also keep any captures
10451 that may have been added during instantiation of the op(). */
10452 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10453 tree tmpl_cap
10454 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10455 args, tf_warning_or_error, NULL_TREE,
10456 false, false);
10458 LAMBDA_EXPR_CAPTURE_LIST (expr)
10459 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10461 maybe_add_lambda_conv_op (type);
10463 else
10464 gcc_assert (errorcount);
10467 /* Set the file and line number information to whatever is given for
10468 the class itself. This puts error messages involving generated
10469 implicit functions at a predictable point, and the same point
10470 that would be used for non-template classes. */
10471 input_location = DECL_SOURCE_LOCATION (typedecl);
10473 unreverse_member_declarations (type);
10474 finish_struct_1 (type);
10475 TYPE_BEING_DEFINED (type) = 0;
10477 /* We don't instantiate default arguments for member functions. 14.7.1:
10479 The implicit instantiation of a class template specialization causes
10480 the implicit instantiation of the declarations, but not of the
10481 definitions or default arguments, of the class member functions,
10482 member classes, static data members and member templates.... */
10484 /* Some typedefs referenced from within the template code need to be access
10485 checked at template instantiation time, i.e now. These types were
10486 added to the template at parsing time. Let's get those and perform
10487 the access checks then. */
10488 perform_typedefs_access_check (pattern, args);
10489 perform_deferred_access_checks (tf_warning_or_error);
10490 pop_nested_class ();
10491 maximum_field_alignment = saved_maximum_field_alignment;
10492 if (!fn_context)
10493 pop_from_top_level ();
10494 pop_deferring_access_checks ();
10495 pop_tinst_level ();
10497 /* The vtable for a template class can be emitted in any translation
10498 unit in which the class is instantiated. When there is no key
10499 method, however, finish_struct_1 will already have added TYPE to
10500 the keyed_classes list. */
10501 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10502 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10504 return type;
10507 /* Wrapper for instantiate_class_template_1. */
10509 tree
10510 instantiate_class_template (tree type)
10512 tree ret;
10513 timevar_push (TV_TEMPLATE_INST);
10514 ret = instantiate_class_template_1 (type);
10515 timevar_pop (TV_TEMPLATE_INST);
10516 return ret;
10519 static tree
10520 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10522 tree r;
10524 if (!t)
10525 r = t;
10526 else if (TYPE_P (t))
10527 r = tsubst (t, args, complain, in_decl);
10528 else
10530 if (!(complain & tf_warning))
10531 ++c_inhibit_evaluation_warnings;
10532 r = tsubst_expr (t, args, complain, in_decl,
10533 /*integral_constant_expression_p=*/true);
10534 if (!(complain & tf_warning))
10535 --c_inhibit_evaluation_warnings;
10537 return r;
10540 /* Given a function parameter pack TMPL_PARM and some function parameters
10541 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10542 and set *SPEC_P to point at the next point in the list. */
10544 tree
10545 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10547 /* Collect all of the extra "packed" parameters into an
10548 argument pack. */
10549 tree parmvec;
10550 tree parmtypevec;
10551 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10552 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10553 tree spec_parm = *spec_p;
10554 int i, len;
10556 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10557 if (tmpl_parm
10558 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10559 break;
10561 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10562 parmvec = make_tree_vec (len);
10563 parmtypevec = make_tree_vec (len);
10564 spec_parm = *spec_p;
10565 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10567 TREE_VEC_ELT (parmvec, i) = spec_parm;
10568 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10571 /* Build the argument packs. */
10572 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10573 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10574 TREE_TYPE (argpack) = argtypepack;
10575 *spec_p = spec_parm;
10577 return argpack;
10580 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10581 NONTYPE_ARGUMENT_PACK. */
10583 static tree
10584 make_fnparm_pack (tree spec_parm)
10586 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10589 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10590 pack expansion with no extra args, 2 if it has extra args, or 0
10591 if it is not a pack expansion. */
10593 static int
10594 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10596 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10597 if (i >= TREE_VEC_LENGTH (vec))
10598 return 0;
10599 tree elt = TREE_VEC_ELT (vec, i);
10600 if (DECL_P (elt))
10601 /* A decl pack is itself an expansion. */
10602 elt = TREE_TYPE (elt);
10603 if (!PACK_EXPANSION_P (elt))
10604 return 0;
10605 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10606 return 2;
10607 return 1;
10611 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10613 static tree
10614 make_argument_pack_select (tree arg_pack, unsigned index)
10616 tree aps = make_node (ARGUMENT_PACK_SELECT);
10618 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10619 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10621 return aps;
10624 /* This is a subroutine of tsubst_pack_expansion.
10626 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10627 mechanism to store the (non complete list of) arguments of the
10628 substitution and return a non substituted pack expansion, in order
10629 to wait for when we have enough arguments to really perform the
10630 substitution. */
10632 static bool
10633 use_pack_expansion_extra_args_p (tree parm_packs,
10634 int arg_pack_len,
10635 bool has_empty_arg)
10637 /* If one pack has an expansion and another pack has a normal
10638 argument or if one pack has an empty argument and an another
10639 one hasn't then tsubst_pack_expansion cannot perform the
10640 substitution and need to fall back on the
10641 PACK_EXPANSION_EXTRA mechanism. */
10642 if (parm_packs == NULL_TREE)
10643 return false;
10644 else if (has_empty_arg)
10645 return true;
10647 bool has_expansion_arg = false;
10648 for (int i = 0 ; i < arg_pack_len; ++i)
10650 bool has_non_expansion_arg = false;
10651 for (tree parm_pack = parm_packs;
10652 parm_pack;
10653 parm_pack = TREE_CHAIN (parm_pack))
10655 tree arg = TREE_VALUE (parm_pack);
10657 int exp = argument_pack_element_is_expansion_p (arg, i);
10658 if (exp == 2)
10659 /* We can't substitute a pack expansion with extra args into
10660 our pattern. */
10661 return true;
10662 else if (exp)
10663 has_expansion_arg = true;
10664 else
10665 has_non_expansion_arg = true;
10668 if (has_expansion_arg && has_non_expansion_arg)
10669 return true;
10671 return false;
10674 /* [temp.variadic]/6 says that:
10676 The instantiation of a pack expansion [...]
10677 produces a list E1,E2, ..., En, where N is the number of elements
10678 in the pack expansion parameters.
10680 This subroutine of tsubst_pack_expansion produces one of these Ei.
10682 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10683 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10684 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10685 INDEX is the index 'i' of the element Ei to produce. ARGS,
10686 COMPLAIN, and IN_DECL are the same parameters as for the
10687 tsubst_pack_expansion function.
10689 The function returns the resulting Ei upon successful completion,
10690 or error_mark_node.
10692 Note that this function possibly modifies the ARGS parameter, so
10693 it's the responsibility of the caller to restore it. */
10695 static tree
10696 gen_elem_of_pack_expansion_instantiation (tree pattern,
10697 tree parm_packs,
10698 unsigned index,
10699 tree args /* This parm gets
10700 modified. */,
10701 tsubst_flags_t complain,
10702 tree in_decl)
10704 tree t;
10705 bool ith_elem_is_expansion = false;
10707 /* For each parameter pack, change the substitution of the parameter
10708 pack to the ith argument in its argument pack, then expand the
10709 pattern. */
10710 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10712 tree parm = TREE_PURPOSE (pack);
10713 tree arg_pack = TREE_VALUE (pack);
10714 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10716 ith_elem_is_expansion |=
10717 argument_pack_element_is_expansion_p (arg_pack, index);
10719 /* Select the Ith argument from the pack. */
10720 if (TREE_CODE (parm) == PARM_DECL
10721 || TREE_CODE (parm) == FIELD_DECL)
10723 if (index == 0)
10725 aps = make_argument_pack_select (arg_pack, index);
10726 if (!mark_used (parm, complain) && !(complain & tf_error))
10727 return error_mark_node;
10728 register_local_specialization (aps, parm);
10730 else
10731 aps = retrieve_local_specialization (parm);
10733 else
10735 int idx, level;
10736 template_parm_level_and_index (parm, &level, &idx);
10738 if (index == 0)
10740 aps = make_argument_pack_select (arg_pack, index);
10741 /* Update the corresponding argument. */
10742 TMPL_ARG (args, level, idx) = aps;
10744 else
10745 /* Re-use the ARGUMENT_PACK_SELECT. */
10746 aps = TMPL_ARG (args, level, idx);
10748 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10751 /* Substitute into the PATTERN with the (possibly altered)
10752 arguments. */
10753 if (pattern == in_decl)
10754 /* Expanding a fixed parameter pack from
10755 coerce_template_parameter_pack. */
10756 t = tsubst_decl (pattern, args, complain);
10757 else if (pattern == error_mark_node)
10758 t = error_mark_node;
10759 else if (constraint_p (pattern))
10761 if (processing_template_decl)
10762 t = tsubst_constraint (pattern, args, complain, in_decl);
10763 else
10764 t = (constraints_satisfied_p (pattern, args)
10765 ? boolean_true_node : boolean_false_node);
10767 else if (!TYPE_P (pattern))
10768 t = tsubst_expr (pattern, args, complain, in_decl,
10769 /*integral_constant_expression_p=*/false);
10770 else
10771 t = tsubst (pattern, args, complain, in_decl);
10773 /* If the Ith argument pack element is a pack expansion, then
10774 the Ith element resulting from the substituting is going to
10775 be a pack expansion as well. */
10776 if (ith_elem_is_expansion)
10777 t = make_pack_expansion (t);
10779 return t;
10782 /* When the unexpanded parameter pack in a fold expression expands to an empty
10783 sequence, the value of the expression is as follows; the program is
10784 ill-formed if the operator is not listed in this table.
10786 && true
10787 || false
10788 , void() */
10790 tree
10791 expand_empty_fold (tree t, tsubst_flags_t complain)
10793 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
10794 if (!FOLD_EXPR_MODIFY_P (t))
10795 switch (code)
10797 case TRUTH_ANDIF_EXPR:
10798 return boolean_true_node;
10799 case TRUTH_ORIF_EXPR:
10800 return boolean_false_node;
10801 case COMPOUND_EXPR:
10802 return void_node;
10803 default:
10804 break;
10807 if (complain & tf_error)
10808 error_at (location_of (t),
10809 "fold of empty expansion over %O", code);
10810 return error_mark_node;
10813 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10814 form an expression that combines the two terms using the
10815 operator of T. */
10817 static tree
10818 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
10820 tree op = FOLD_EXPR_OP (t);
10821 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
10823 // Handle compound assignment operators.
10824 if (FOLD_EXPR_MODIFY_P (t))
10825 return build_x_modify_expr (input_location, left, code, right, complain);
10827 switch (code)
10829 case COMPOUND_EXPR:
10830 return build_x_compound_expr (input_location, left, right, complain);
10831 case DOTSTAR_EXPR:
10832 return build_m_component_ref (left, right, complain);
10833 default:
10834 return build_x_binary_op (input_location, code,
10835 left, TREE_CODE (left),
10836 right, TREE_CODE (right),
10837 /*overload=*/NULL,
10838 complain);
10842 /* Substitute ARGS into the pack of a fold expression T. */
10844 static inline tree
10845 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10847 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
10850 /* Substitute ARGS into the pack of a fold expression T. */
10852 static inline tree
10853 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10855 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
10858 /* Expand a PACK of arguments into a grouped as left fold.
10859 Given a pack containing elements A0, A1, ..., An and an
10860 operator @, this builds the expression:
10862 ((A0 @ A1) @ A2) ... @ An
10864 Note that PACK must not be empty.
10866 The operator is defined by the original fold expression T. */
10868 static tree
10869 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
10871 tree left = TREE_VEC_ELT (pack, 0);
10872 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
10874 tree right = TREE_VEC_ELT (pack, i);
10875 left = fold_expression (t, left, right, complain);
10877 return left;
10880 /* Substitute into a unary left fold expression. */
10882 static tree
10883 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
10884 tree in_decl)
10886 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10887 if (pack == error_mark_node)
10888 return error_mark_node;
10889 if (PACK_EXPANSION_P (pack))
10891 tree r = copy_node (t);
10892 FOLD_EXPR_PACK (r) = pack;
10893 return r;
10895 if (TREE_VEC_LENGTH (pack) == 0)
10896 return expand_empty_fold (t, complain);
10897 else
10898 return expand_left_fold (t, pack, complain);
10901 /* Substitute into a binary left fold expression.
10903 Do ths by building a single (non-empty) vector of argumnts and
10904 building the expression from those elements. */
10906 static tree
10907 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
10908 tree in_decl)
10910 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10911 if (pack == error_mark_node)
10912 return error_mark_node;
10913 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10914 if (init == error_mark_node)
10915 return error_mark_node;
10917 if (PACK_EXPANSION_P (pack))
10919 tree r = copy_node (t);
10920 FOLD_EXPR_PACK (r) = pack;
10921 FOLD_EXPR_INIT (r) = init;
10922 return r;
10925 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
10926 TREE_VEC_ELT (vec, 0) = init;
10927 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
10928 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
10930 return expand_left_fold (t, vec, complain);
10933 /* Expand a PACK of arguments into a grouped as right fold.
10934 Given a pack containing elementns A0, A1, ..., and an
10935 operator @, this builds the expression:
10937 A0@ ... (An-2 @ (An-1 @ An))
10939 Note that PACK must not be empty.
10941 The operator is defined by the original fold expression T. */
10943 tree
10944 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
10946 // Build the expression.
10947 int n = TREE_VEC_LENGTH (pack);
10948 tree right = TREE_VEC_ELT (pack, n - 1);
10949 for (--n; n != 0; --n)
10951 tree left = TREE_VEC_ELT (pack, n - 1);
10952 right = fold_expression (t, left, right, complain);
10954 return right;
10957 /* Substitute into a unary right fold expression. */
10959 static tree
10960 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
10961 tree in_decl)
10963 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10964 if (pack == error_mark_node)
10965 return error_mark_node;
10966 if (PACK_EXPANSION_P (pack))
10968 tree r = copy_node (t);
10969 FOLD_EXPR_PACK (r) = pack;
10970 return r;
10972 if (TREE_VEC_LENGTH (pack) == 0)
10973 return expand_empty_fold (t, complain);
10974 else
10975 return expand_right_fold (t, pack, complain);
10978 /* Substitute into a binary right fold expression.
10980 Do ths by building a single (non-empty) vector of arguments and
10981 building the expression from those elements. */
10983 static tree
10984 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
10985 tree in_decl)
10987 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10988 if (pack == error_mark_node)
10989 return error_mark_node;
10990 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10991 if (init == error_mark_node)
10992 return error_mark_node;
10994 if (PACK_EXPANSION_P (pack))
10996 tree r = copy_node (t);
10997 FOLD_EXPR_PACK (r) = pack;
10998 FOLD_EXPR_INIT (r) = init;
10999 return r;
11002 int n = TREE_VEC_LENGTH (pack);
11003 tree vec = make_tree_vec (n + 1);
11004 for (int i = 0; i < n; ++i)
11005 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11006 TREE_VEC_ELT (vec, n) = init;
11008 return expand_right_fold (t, vec, complain);
11012 /* Substitute ARGS into T, which is an pack expansion
11013 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11014 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11015 (if only a partial substitution could be performed) or
11016 ERROR_MARK_NODE if there was an error. */
11017 tree
11018 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11019 tree in_decl)
11021 tree pattern;
11022 tree pack, packs = NULL_TREE;
11023 bool unsubstituted_packs = false;
11024 int i, len = -1;
11025 tree result;
11026 hash_map<tree, tree> *saved_local_specializations = NULL;
11027 bool need_local_specializations = false;
11028 int levels;
11030 gcc_assert (PACK_EXPANSION_P (t));
11031 pattern = PACK_EXPANSION_PATTERN (t);
11033 /* Add in any args remembered from an earlier partial instantiation. */
11034 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11036 levels = TMPL_ARGS_DEPTH (args);
11038 /* Determine the argument packs that will instantiate the parameter
11039 packs used in the expansion expression. While we're at it,
11040 compute the number of arguments to be expanded and make sure it
11041 is consistent. */
11042 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11043 pack = TREE_CHAIN (pack))
11045 tree parm_pack = TREE_VALUE (pack);
11046 tree arg_pack = NULL_TREE;
11047 tree orig_arg = NULL_TREE;
11048 int level = 0;
11050 if (TREE_CODE (parm_pack) == BASES)
11052 if (BASES_DIRECT (parm_pack))
11053 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11054 args, complain, in_decl, false));
11055 else
11056 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11057 args, complain, in_decl, false));
11059 if (TREE_CODE (parm_pack) == PARM_DECL)
11061 /* We know we have correct local_specializations if this
11062 expansion is at function scope, or if we're dealing with a
11063 local parameter in a requires expression; for the latter,
11064 tsubst_requires_expr set it up appropriately. */
11065 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11066 arg_pack = retrieve_local_specialization (parm_pack);
11067 else
11068 /* We can't rely on local_specializations for a parameter
11069 name used later in a function declaration (such as in a
11070 late-specified return type). Even if it exists, it might
11071 have the wrong value for a recursive call. */
11072 need_local_specializations = true;
11074 if (!arg_pack)
11076 /* This parameter pack was used in an unevaluated context. Just
11077 make a dummy decl, since it's only used for its type. */
11078 arg_pack = tsubst_decl (parm_pack, args, complain);
11079 if (arg_pack && DECL_PACK_P (arg_pack))
11080 /* Partial instantiation of the parm_pack, we can't build
11081 up an argument pack yet. */
11082 arg_pack = NULL_TREE;
11083 else
11084 arg_pack = make_fnparm_pack (arg_pack);
11087 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11088 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11089 else
11091 int idx;
11092 template_parm_level_and_index (parm_pack, &level, &idx);
11094 if (level <= levels)
11095 arg_pack = TMPL_ARG (args, level, idx);
11098 orig_arg = arg_pack;
11099 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11100 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11102 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11103 /* This can only happen if we forget to expand an argument
11104 pack somewhere else. Just return an error, silently. */
11106 result = make_tree_vec (1);
11107 TREE_VEC_ELT (result, 0) = error_mark_node;
11108 return result;
11111 if (arg_pack)
11113 int my_len =
11114 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11116 /* Don't bother trying to do a partial substitution with
11117 incomplete packs; we'll try again after deduction. */
11118 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11119 return t;
11121 if (len < 0)
11122 len = my_len;
11123 else if (len != my_len)
11125 if (!(complain & tf_error))
11126 /* Fail quietly. */;
11127 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11128 error ("mismatched argument pack lengths while expanding "
11129 "%<%T%>",
11130 pattern);
11131 else
11132 error ("mismatched argument pack lengths while expanding "
11133 "%<%E%>",
11134 pattern);
11135 return error_mark_node;
11138 /* Keep track of the parameter packs and their corresponding
11139 argument packs. */
11140 packs = tree_cons (parm_pack, arg_pack, packs);
11141 TREE_TYPE (packs) = orig_arg;
11143 else
11145 /* We can't substitute for this parameter pack. We use a flag as
11146 well as the missing_level counter because function parameter
11147 packs don't have a level. */
11148 gcc_assert (processing_template_decl);
11149 unsubstituted_packs = true;
11153 /* If the expansion is just T..., return the matching argument pack, unless
11154 we need to call convert_from_reference on all the elements. This is an
11155 important optimization; see c++/68422. */
11156 if (!unsubstituted_packs
11157 && TREE_PURPOSE (packs) == pattern)
11159 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11160 /* Types need no adjustment, nor does sizeof..., and if we still have
11161 some pack expansion args we won't do anything yet. */
11162 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11163 || PACK_EXPANSION_SIZEOF_P (t)
11164 || pack_expansion_args_count (args))
11165 return args;
11166 /* Also optimize expression pack expansions if we can tell that the
11167 elements won't have reference type. */
11168 tree type = TREE_TYPE (pattern);
11169 if (type && TREE_CODE (type) != REFERENCE_TYPE
11170 && !PACK_EXPANSION_P (type)
11171 && !WILDCARD_TYPE_P (type))
11172 return args;
11173 /* Otherwise use the normal path so we get convert_from_reference. */
11176 /* We cannot expand this expansion expression, because we don't have
11177 all of the argument packs we need. */
11178 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11180 /* We got some full packs, but we can't substitute them in until we
11181 have values for all the packs. So remember these until then. */
11183 t = make_pack_expansion (pattern);
11184 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11185 return t;
11187 else if (unsubstituted_packs)
11189 /* There were no real arguments, we're just replacing a parameter
11190 pack with another version of itself. Substitute into the
11191 pattern and return a PACK_EXPANSION_*. The caller will need to
11192 deal with that. */
11193 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11194 t = tsubst_expr (pattern, args, complain, in_decl,
11195 /*integral_constant_expression_p=*/false);
11196 else
11197 t = tsubst (pattern, args, complain, in_decl);
11198 t = make_pack_expansion (t);
11199 return t;
11202 gcc_assert (len >= 0);
11204 if (need_local_specializations)
11206 /* We're in a late-specified return type, so create our own local
11207 specializations map; the current map is either NULL or (in the
11208 case of recursive unification) might have bindings that we don't
11209 want to use or alter. */
11210 saved_local_specializations = local_specializations;
11211 local_specializations = new hash_map<tree, tree>;
11214 /* For each argument in each argument pack, substitute into the
11215 pattern. */
11216 result = make_tree_vec (len);
11217 tree elem_args = copy_template_args (args);
11218 for (i = 0; i < len; ++i)
11220 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11222 elem_args, complain,
11223 in_decl);
11224 TREE_VEC_ELT (result, i) = t;
11225 if (t == error_mark_node)
11227 result = error_mark_node;
11228 break;
11232 /* Update ARGS to restore the substitution from parameter packs to
11233 their argument packs. */
11234 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11236 tree parm = TREE_PURPOSE (pack);
11238 if (TREE_CODE (parm) == PARM_DECL
11239 || TREE_CODE (parm) == FIELD_DECL)
11240 register_local_specialization (TREE_TYPE (pack), parm);
11241 else
11243 int idx, level;
11245 if (TREE_VALUE (pack) == NULL_TREE)
11246 continue;
11248 template_parm_level_and_index (parm, &level, &idx);
11250 /* Update the corresponding argument. */
11251 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11252 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11253 TREE_TYPE (pack);
11254 else
11255 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11259 if (need_local_specializations)
11261 delete local_specializations;
11262 local_specializations = saved_local_specializations;
11265 /* If the dependent pack arguments were such that we end up with only a
11266 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11267 if (len == 1 && TREE_CODE (result) == TREE_VEC
11268 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11269 return TREE_VEC_ELT (result, 0);
11271 return result;
11274 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11275 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11276 parameter packs; all parms generated from a function parameter pack will
11277 have the same DECL_PARM_INDEX. */
11279 tree
11280 get_pattern_parm (tree parm, tree tmpl)
11282 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11283 tree patparm;
11285 if (DECL_ARTIFICIAL (parm))
11287 for (patparm = DECL_ARGUMENTS (pattern);
11288 patparm; patparm = DECL_CHAIN (patparm))
11289 if (DECL_ARTIFICIAL (patparm)
11290 && DECL_NAME (parm) == DECL_NAME (patparm))
11291 break;
11293 else
11295 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11296 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11297 gcc_assert (DECL_PARM_INDEX (patparm)
11298 == DECL_PARM_INDEX (parm));
11301 return patparm;
11304 /* Make an argument pack out of the TREE_VEC VEC. */
11306 static tree
11307 make_argument_pack (tree vec)
11309 tree pack;
11310 tree elt = TREE_VEC_ELT (vec, 0);
11311 if (TYPE_P (elt))
11312 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11313 else
11315 pack = make_node (NONTYPE_ARGUMENT_PACK);
11316 TREE_TYPE (pack) = TREE_TYPE (elt);
11317 TREE_CONSTANT (pack) = 1;
11319 SET_ARGUMENT_PACK_ARGS (pack, vec);
11320 return pack;
11323 /* Return an exact copy of template args T that can be modified
11324 independently. */
11326 static tree
11327 copy_template_args (tree t)
11329 if (t == error_mark_node)
11330 return t;
11332 int len = TREE_VEC_LENGTH (t);
11333 tree new_vec = make_tree_vec (len);
11335 for (int i = 0; i < len; ++i)
11337 tree elt = TREE_VEC_ELT (t, i);
11338 if (elt && TREE_CODE (elt) == TREE_VEC)
11339 elt = copy_template_args (elt);
11340 TREE_VEC_ELT (new_vec, i) = elt;
11343 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11344 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11346 return new_vec;
11349 /* Substitute ARGS into the vector or list of template arguments T. */
11351 static tree
11352 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11354 tree orig_t = t;
11355 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11356 tree *elts;
11358 if (t == error_mark_node)
11359 return error_mark_node;
11361 len = TREE_VEC_LENGTH (t);
11362 elts = XALLOCAVEC (tree, len);
11364 for (i = 0; i < len; i++)
11366 tree orig_arg = TREE_VEC_ELT (t, i);
11367 tree new_arg;
11369 if (TREE_CODE (orig_arg) == TREE_VEC)
11370 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11371 else if (PACK_EXPANSION_P (orig_arg))
11373 /* Substitute into an expansion expression. */
11374 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11376 if (TREE_CODE (new_arg) == TREE_VEC)
11377 /* Add to the expanded length adjustment the number of
11378 expanded arguments. We subtract one from this
11379 measurement, because the argument pack expression
11380 itself is already counted as 1 in
11381 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11382 the argument pack is empty. */
11383 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11385 else if (ARGUMENT_PACK_P (orig_arg))
11387 /* Substitute into each of the arguments. */
11388 new_arg = TYPE_P (orig_arg)
11389 ? cxx_make_type (TREE_CODE (orig_arg))
11390 : make_node (TREE_CODE (orig_arg));
11392 SET_ARGUMENT_PACK_ARGS (
11393 new_arg,
11394 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11395 args, complain, in_decl));
11397 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11398 new_arg = error_mark_node;
11400 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11401 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11402 complain, in_decl);
11403 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11405 if (TREE_TYPE (new_arg) == error_mark_node)
11406 new_arg = error_mark_node;
11409 else
11410 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11412 if (new_arg == error_mark_node)
11413 return error_mark_node;
11415 elts[i] = new_arg;
11416 if (new_arg != orig_arg)
11417 need_new = 1;
11420 if (!need_new)
11421 return t;
11423 /* Make space for the expanded arguments coming from template
11424 argument packs. */
11425 t = make_tree_vec (len + expanded_len_adjust);
11426 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11427 arguments for a member template.
11428 In that case each TREE_VEC in ORIG_T represents a level of template
11429 arguments, and ORIG_T won't carry any non defaulted argument count.
11430 It will rather be the nested TREE_VECs that will carry one.
11431 In other words, ORIG_T carries a non defaulted argument count only
11432 if it doesn't contain any nested TREE_VEC. */
11433 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11435 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11436 count += expanded_len_adjust;
11437 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11439 for (i = 0, out = 0; i < len; i++)
11441 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11442 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11443 && TREE_CODE (elts[i]) == TREE_VEC)
11445 int idx;
11447 /* Now expand the template argument pack "in place". */
11448 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11449 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11451 else
11453 TREE_VEC_ELT (t, out) = elts[i];
11454 out++;
11458 return t;
11461 /* Substitute ARGS into one level PARMS of template parameters. */
11463 static tree
11464 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11466 if (parms == error_mark_node)
11467 return error_mark_node;
11469 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11471 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11473 tree tuple = TREE_VEC_ELT (parms, i);
11475 if (tuple == error_mark_node)
11476 continue;
11478 TREE_VEC_ELT (new_vec, i) =
11479 tsubst_template_parm (tuple, args, complain);
11482 return new_vec;
11485 /* Return the result of substituting ARGS into the template parameters
11486 given by PARMS. If there are m levels of ARGS and m + n levels of
11487 PARMS, then the result will contain n levels of PARMS. For
11488 example, if PARMS is `template <class T> template <class U>
11489 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11490 result will be `template <int*, double, class V>'. */
11492 static tree
11493 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11495 tree r = NULL_TREE;
11496 tree* new_parms;
11498 /* When substituting into a template, we must set
11499 PROCESSING_TEMPLATE_DECL as the template parameters may be
11500 dependent if they are based on one-another, and the dependency
11501 predicates are short-circuit outside of templates. */
11502 ++processing_template_decl;
11504 for (new_parms = &r;
11505 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11506 new_parms = &(TREE_CHAIN (*new_parms)),
11507 parms = TREE_CHAIN (parms))
11509 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11510 args, complain);
11511 *new_parms =
11512 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11513 - TMPL_ARGS_DEPTH (args)),
11514 new_vec, NULL_TREE);
11517 --processing_template_decl;
11519 return r;
11522 /* Return the result of substituting ARGS into one template parameter
11523 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11524 parameter and which TREE_PURPOSE is the default argument of the
11525 template parameter. */
11527 static tree
11528 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11530 tree default_value, parm_decl;
11532 if (args == NULL_TREE
11533 || t == NULL_TREE
11534 || t == error_mark_node)
11535 return t;
11537 gcc_assert (TREE_CODE (t) == TREE_LIST);
11539 default_value = TREE_PURPOSE (t);
11540 parm_decl = TREE_VALUE (t);
11542 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11543 if (TREE_CODE (parm_decl) == PARM_DECL
11544 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11545 parm_decl = error_mark_node;
11546 default_value = tsubst_template_arg (default_value, args,
11547 complain, NULL_TREE);
11549 return build_tree_list (default_value, parm_decl);
11552 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11553 type T. If T is not an aggregate or enumeration type, it is
11554 handled as if by tsubst. IN_DECL is as for tsubst. If
11555 ENTERING_SCOPE is nonzero, T is the context for a template which
11556 we are presently tsubst'ing. Return the substituted value. */
11558 static tree
11559 tsubst_aggr_type (tree t,
11560 tree args,
11561 tsubst_flags_t complain,
11562 tree in_decl,
11563 int entering_scope)
11565 if (t == NULL_TREE)
11566 return NULL_TREE;
11568 switch (TREE_CODE (t))
11570 case RECORD_TYPE:
11571 if (TYPE_PTRMEMFUNC_P (t))
11572 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11574 /* Fall through. */
11575 case ENUMERAL_TYPE:
11576 case UNION_TYPE:
11577 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11579 tree argvec;
11580 tree context;
11581 tree r;
11582 int saved_unevaluated_operand;
11583 int saved_inhibit_evaluation_warnings;
11585 /* In "sizeof(X<I>)" we need to evaluate "I". */
11586 saved_unevaluated_operand = cp_unevaluated_operand;
11587 cp_unevaluated_operand = 0;
11588 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11589 c_inhibit_evaluation_warnings = 0;
11591 /* First, determine the context for the type we are looking
11592 up. */
11593 context = TYPE_CONTEXT (t);
11594 if (context && TYPE_P (context))
11596 context = tsubst_aggr_type (context, args, complain,
11597 in_decl, /*entering_scope=*/1);
11598 /* If context is a nested class inside a class template,
11599 it may still need to be instantiated (c++/33959). */
11600 context = complete_type (context);
11603 /* Then, figure out what arguments are appropriate for the
11604 type we are trying to find. For example, given:
11606 template <class T> struct S;
11607 template <class T, class U> void f(T, U) { S<U> su; }
11609 and supposing that we are instantiating f<int, double>,
11610 then our ARGS will be {int, double}, but, when looking up
11611 S we only want {double}. */
11612 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11613 complain, in_decl);
11614 if (argvec == error_mark_node)
11615 r = error_mark_node;
11616 else
11618 r = lookup_template_class (t, argvec, in_decl, context,
11619 entering_scope, complain);
11620 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11623 cp_unevaluated_operand = saved_unevaluated_operand;
11624 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11626 return r;
11628 else
11629 /* This is not a template type, so there's nothing to do. */
11630 return t;
11632 default:
11633 return tsubst (t, args, complain, in_decl);
11637 /* Substitute into the default argument ARG (a default argument for
11638 FN), which has the indicated TYPE. */
11640 tree
11641 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11643 tree saved_class_ptr = NULL_TREE;
11644 tree saved_class_ref = NULL_TREE;
11645 int errs = errorcount + sorrycount;
11647 /* This can happen in invalid code. */
11648 if (TREE_CODE (arg) == DEFAULT_ARG)
11649 return arg;
11651 /* This default argument came from a template. Instantiate the
11652 default argument here, not in tsubst. In the case of
11653 something like:
11655 template <class T>
11656 struct S {
11657 static T t();
11658 void f(T = t());
11661 we must be careful to do name lookup in the scope of S<T>,
11662 rather than in the current class. */
11663 push_access_scope (fn);
11664 /* The "this" pointer is not valid in a default argument. */
11665 if (cfun)
11667 saved_class_ptr = current_class_ptr;
11668 cp_function_chain->x_current_class_ptr = NULL_TREE;
11669 saved_class_ref = current_class_ref;
11670 cp_function_chain->x_current_class_ref = NULL_TREE;
11673 push_deferring_access_checks(dk_no_deferred);
11674 /* The default argument expression may cause implicitly defined
11675 member functions to be synthesized, which will result in garbage
11676 collection. We must treat this situation as if we were within
11677 the body of function so as to avoid collecting live data on the
11678 stack. */
11679 ++function_depth;
11680 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11681 complain, NULL_TREE,
11682 /*integral_constant_expression_p=*/false);
11683 --function_depth;
11684 pop_deferring_access_checks();
11686 /* Restore the "this" pointer. */
11687 if (cfun)
11689 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11690 cp_function_chain->x_current_class_ref = saved_class_ref;
11693 if (errorcount+sorrycount > errs
11694 && (complain & tf_warning_or_error))
11695 inform (input_location,
11696 " when instantiating default argument for call to %D", fn);
11698 /* Make sure the default argument is reasonable. */
11699 arg = check_default_argument (type, arg, complain);
11701 pop_access_scope (fn);
11703 return arg;
11706 /* Substitute into all the default arguments for FN. */
11708 static void
11709 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11711 tree arg;
11712 tree tmpl_args;
11714 tmpl_args = DECL_TI_ARGS (fn);
11716 /* If this function is not yet instantiated, we certainly don't need
11717 its default arguments. */
11718 if (uses_template_parms (tmpl_args))
11719 return;
11720 /* Don't do this again for clones. */
11721 if (DECL_CLONED_FUNCTION_P (fn))
11722 return;
11724 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11725 arg;
11726 arg = TREE_CHAIN (arg))
11727 if (TREE_PURPOSE (arg))
11728 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11729 TREE_VALUE (arg),
11730 TREE_PURPOSE (arg),
11731 complain);
11734 /* Substitute the ARGS into the T, which is a _DECL. Return the
11735 result of the substitution. Issue error and warning messages under
11736 control of COMPLAIN. */
11738 static tree
11739 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11741 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11742 location_t saved_loc;
11743 tree r = NULL_TREE;
11744 tree in_decl = t;
11745 hashval_t hash = 0;
11747 /* Set the filename and linenumber to improve error-reporting. */
11748 saved_loc = input_location;
11749 input_location = DECL_SOURCE_LOCATION (t);
11751 switch (TREE_CODE (t))
11753 case TEMPLATE_DECL:
11755 /* We can get here when processing a member function template,
11756 member class template, or template template parameter. */
11757 tree decl = DECL_TEMPLATE_RESULT (t);
11758 tree spec;
11759 tree tmpl_args;
11760 tree full_args;
11762 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11764 /* Template template parameter is treated here. */
11765 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11766 if (new_type == error_mark_node)
11767 r = error_mark_node;
11768 /* If we get a real template back, return it. This can happen in
11769 the context of most_specialized_partial_spec. */
11770 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11771 r = new_type;
11772 else
11773 /* The new TEMPLATE_DECL was built in
11774 reduce_template_parm_level. */
11775 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
11776 break;
11779 /* We might already have an instance of this template.
11780 The ARGS are for the surrounding class type, so the
11781 full args contain the tsubst'd args for the context,
11782 plus the innermost args from the template decl. */
11783 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11784 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11785 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11786 /* Because this is a template, the arguments will still be
11787 dependent, even after substitution. If
11788 PROCESSING_TEMPLATE_DECL is not set, the dependency
11789 predicates will short-circuit. */
11790 ++processing_template_decl;
11791 full_args = tsubst_template_args (tmpl_args, args,
11792 complain, in_decl);
11793 --processing_template_decl;
11794 if (full_args == error_mark_node)
11795 RETURN (error_mark_node);
11797 /* If this is a default template template argument,
11798 tsubst might not have changed anything. */
11799 if (full_args == tmpl_args)
11800 RETURN (t);
11802 hash = hash_tmpl_and_args (t, full_args);
11803 spec = retrieve_specialization (t, full_args, hash);
11804 if (spec != NULL_TREE)
11806 r = spec;
11807 break;
11810 /* Make a new template decl. It will be similar to the
11811 original, but will record the current template arguments.
11812 We also create a new function declaration, which is just
11813 like the old one, but points to this new template, rather
11814 than the old one. */
11815 r = copy_decl (t);
11816 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11817 DECL_CHAIN (r) = NULL_TREE;
11819 // Build new template info linking to the original template decl.
11820 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11822 if (TREE_CODE (decl) == TYPE_DECL
11823 && !TYPE_DECL_ALIAS_P (decl))
11825 tree new_type;
11826 ++processing_template_decl;
11827 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11828 --processing_template_decl;
11829 if (new_type == error_mark_node)
11830 RETURN (error_mark_node);
11832 TREE_TYPE (r) = new_type;
11833 /* For a partial specialization, we need to keep pointing to
11834 the primary template. */
11835 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11836 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11837 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11838 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11839 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11841 else
11843 tree new_decl;
11844 ++processing_template_decl;
11845 new_decl = tsubst (decl, args, complain, in_decl);
11846 --processing_template_decl;
11847 if (new_decl == error_mark_node)
11848 RETURN (error_mark_node);
11850 DECL_TEMPLATE_RESULT (r) = new_decl;
11851 DECL_TI_TEMPLATE (new_decl) = r;
11852 TREE_TYPE (r) = TREE_TYPE (new_decl);
11853 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11854 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11857 SET_DECL_IMPLICIT_INSTANTIATION (r);
11858 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11859 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11861 /* The template parameters for this new template are all the
11862 template parameters for the old template, except the
11863 outermost level of parameters. */
11864 DECL_TEMPLATE_PARMS (r)
11865 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11866 complain);
11868 if (PRIMARY_TEMPLATE_P (t))
11869 DECL_PRIMARY_TEMPLATE (r) = r;
11871 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
11872 /* Record this non-type partial instantiation. */
11873 register_specialization (r, t,
11874 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11875 false, hash);
11877 break;
11879 case FUNCTION_DECL:
11881 tree ctx;
11882 tree argvec = NULL_TREE;
11883 tree *friends;
11884 tree gen_tmpl;
11885 tree type;
11886 int member;
11887 int args_depth;
11888 int parms_depth;
11890 /* Nobody should be tsubst'ing into non-template functions. */
11891 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11893 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11895 tree spec;
11897 /* If T is not dependent, just return it. */
11898 if (!uses_template_parms (DECL_TI_ARGS (t)))
11899 RETURN (t);
11901 /* Calculate the most general template of which R is a
11902 specialization, and the complete set of arguments used to
11903 specialize R. */
11904 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11905 argvec = tsubst_template_args (DECL_TI_ARGS
11906 (DECL_TEMPLATE_RESULT
11907 (DECL_TI_TEMPLATE (t))),
11908 args, complain, in_decl);
11909 if (argvec == error_mark_node)
11910 RETURN (error_mark_node);
11912 /* Check to see if we already have this specialization. */
11913 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11914 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11916 if (spec)
11918 r = spec;
11919 break;
11922 /* We can see more levels of arguments than parameters if
11923 there was a specialization of a member template, like
11924 this:
11926 template <class T> struct S { template <class U> void f(); }
11927 template <> template <class U> void S<int>::f(U);
11929 Here, we'll be substituting into the specialization,
11930 because that's where we can find the code we actually
11931 want to generate, but we'll have enough arguments for
11932 the most general template.
11934 We also deal with the peculiar case:
11936 template <class T> struct S {
11937 template <class U> friend void f();
11939 template <class U> void f() {}
11940 template S<int>;
11941 template void f<double>();
11943 Here, the ARGS for the instantiation of will be {int,
11944 double}. But, we only need as many ARGS as there are
11945 levels of template parameters in CODE_PATTERN. We are
11946 careful not to get fooled into reducing the ARGS in
11947 situations like:
11949 template <class T> struct S { template <class U> void f(U); }
11950 template <class T> template <> void S<T>::f(int) {}
11952 which we can spot because the pattern will be a
11953 specialization in this case. */
11954 args_depth = TMPL_ARGS_DEPTH (args);
11955 parms_depth =
11956 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11957 if (args_depth > parms_depth
11958 && !DECL_TEMPLATE_SPECIALIZATION (t))
11959 args = get_innermost_template_args (args, parms_depth);
11961 else
11963 /* This special case arises when we have something like this:
11965 template <class T> struct S {
11966 friend void f<int>(int, double);
11969 Here, the DECL_TI_TEMPLATE for the friend declaration
11970 will be an IDENTIFIER_NODE. We are being called from
11971 tsubst_friend_function, and we want only to create a
11972 new decl (R) with appropriate types so that we can call
11973 determine_specialization. */
11974 gen_tmpl = NULL_TREE;
11977 if (DECL_CLASS_SCOPE_P (t))
11979 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11980 member = 2;
11981 else
11982 member = 1;
11983 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11984 complain, t, /*entering_scope=*/1);
11986 else
11988 member = 0;
11989 ctx = DECL_CONTEXT (t);
11991 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
11992 if (type == error_mark_node)
11993 RETURN (error_mark_node);
11995 /* If we hit excessive deduction depth, the type is bogus even if
11996 it isn't error_mark_node, so don't build a decl. */
11997 if (excessive_deduction_depth)
11998 RETURN (error_mark_node);
12000 /* We do NOT check for matching decls pushed separately at this
12001 point, as they may not represent instantiations of this
12002 template, and in any case are considered separate under the
12003 discrete model. */
12004 r = copy_decl (t);
12005 DECL_USE_TEMPLATE (r) = 0;
12006 TREE_TYPE (r) = type;
12007 /* Clear out the mangled name and RTL for the instantiation. */
12008 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12009 SET_DECL_RTL (r, NULL);
12010 /* Leave DECL_INITIAL set on deleted instantiations. */
12011 if (!DECL_DELETED_FN (r))
12012 DECL_INITIAL (r) = NULL_TREE;
12013 DECL_CONTEXT (r) = ctx;
12015 /* OpenMP UDRs have the only argument a reference to the declared
12016 type. We want to diagnose if the declared type is a reference,
12017 which is invalid, but as references to references are usually
12018 quietly merged, diagnose it here. */
12019 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12021 tree argtype
12022 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12023 argtype = tsubst (argtype, args, complain, in_decl);
12024 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12025 error_at (DECL_SOURCE_LOCATION (t),
12026 "reference type %qT in "
12027 "%<#pragma omp declare reduction%>", argtype);
12028 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12029 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12030 argtype);
12033 if (member && DECL_CONV_FN_P (r))
12034 /* Type-conversion operator. Reconstruct the name, in
12035 case it's the name of one of the template's parameters. */
12036 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12038 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12039 complain, t);
12040 DECL_RESULT (r) = NULL_TREE;
12042 TREE_STATIC (r) = 0;
12043 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12044 DECL_EXTERNAL (r) = 1;
12045 /* If this is an instantiation of a function with internal
12046 linkage, we already know what object file linkage will be
12047 assigned to the instantiation. */
12048 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12049 DECL_DEFER_OUTPUT (r) = 0;
12050 DECL_CHAIN (r) = NULL_TREE;
12051 DECL_PENDING_INLINE_INFO (r) = 0;
12052 DECL_PENDING_INLINE_P (r) = 0;
12053 DECL_SAVED_TREE (r) = NULL_TREE;
12054 DECL_STRUCT_FUNCTION (r) = NULL;
12055 TREE_USED (r) = 0;
12056 /* We'll re-clone as appropriate in instantiate_template. */
12057 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12059 /* If we aren't complaining now, return on error before we register
12060 the specialization so that we'll complain eventually. */
12061 if ((complain & tf_error) == 0
12062 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12063 && !grok_op_properties (r, /*complain=*/false))
12064 RETURN (error_mark_node);
12066 /* When instantiating a constrained member, substitute
12067 into the constraints to create a new constraint. */
12068 if (tree ci = get_constraints (t))
12069 if (member)
12071 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12072 set_constraints (r, ci);
12075 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12076 this in the special friend case mentioned above where
12077 GEN_TMPL is NULL. */
12078 if (gen_tmpl)
12080 DECL_TEMPLATE_INFO (r)
12081 = build_template_info (gen_tmpl, argvec);
12082 SET_DECL_IMPLICIT_INSTANTIATION (r);
12084 tree new_r
12085 = register_specialization (r, gen_tmpl, argvec, false, hash);
12086 if (new_r != r)
12087 /* We instantiated this while substituting into
12088 the type earlier (template/friend54.C). */
12089 RETURN (new_r);
12091 /* We're not supposed to instantiate default arguments
12092 until they are called, for a template. But, for a
12093 declaration like:
12095 template <class T> void f ()
12096 { extern void g(int i = T()); }
12098 we should do the substitution when the template is
12099 instantiated. We handle the member function case in
12100 instantiate_class_template since the default arguments
12101 might refer to other members of the class. */
12102 if (!member
12103 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12104 && !uses_template_parms (argvec))
12105 tsubst_default_arguments (r, complain);
12107 else
12108 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12110 /* Copy the list of befriending classes. */
12111 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12112 *friends;
12113 friends = &TREE_CHAIN (*friends))
12115 *friends = copy_node (*friends);
12116 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12117 args, complain,
12118 in_decl);
12121 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12123 maybe_retrofit_in_chrg (r);
12124 if (DECL_CONSTRUCTOR_P (r))
12125 grok_ctor_properties (ctx, r);
12126 if (DECL_INHERITED_CTOR (r))
12127 deduce_inheriting_ctor (r);
12128 /* If this is an instantiation of a member template, clone it.
12129 If it isn't, that'll be handled by
12130 clone_constructors_and_destructors. */
12131 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12132 clone_function_decl (r, /*update_method_vec_p=*/0);
12134 else if ((complain & tf_error) != 0
12135 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12136 && !grok_op_properties (r, /*complain=*/true))
12137 RETURN (error_mark_node);
12139 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12140 SET_DECL_FRIEND_CONTEXT (r,
12141 tsubst (DECL_FRIEND_CONTEXT (t),
12142 args, complain, in_decl));
12144 /* Possibly limit visibility based on template args. */
12145 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12146 if (DECL_VISIBILITY_SPECIFIED (t))
12148 DECL_VISIBILITY_SPECIFIED (r) = 0;
12149 DECL_ATTRIBUTES (r)
12150 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12152 determine_visibility (r);
12153 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12154 && !processing_template_decl)
12155 defaulted_late_check (r);
12157 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12158 args, complain, in_decl);
12160 break;
12162 case PARM_DECL:
12164 tree type = NULL_TREE;
12165 int i, len = 1;
12166 tree expanded_types = NULL_TREE;
12167 tree prev_r = NULL_TREE;
12168 tree first_r = NULL_TREE;
12170 if (DECL_PACK_P (t))
12172 /* If there is a local specialization that isn't a
12173 parameter pack, it means that we're doing a "simple"
12174 substitution from inside tsubst_pack_expansion. Just
12175 return the local specialization (which will be a single
12176 parm). */
12177 tree spec = retrieve_local_specialization (t);
12178 if (spec
12179 && TREE_CODE (spec) == PARM_DECL
12180 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12181 RETURN (spec);
12183 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12184 the parameters in this function parameter pack. */
12185 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12186 complain, in_decl);
12187 if (TREE_CODE (expanded_types) == TREE_VEC)
12189 len = TREE_VEC_LENGTH (expanded_types);
12191 /* Zero-length parameter packs are boring. Just substitute
12192 into the chain. */
12193 if (len == 0)
12194 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12195 TREE_CHAIN (t)));
12197 else
12199 /* All we did was update the type. Make a note of that. */
12200 type = expanded_types;
12201 expanded_types = NULL_TREE;
12205 /* Loop through all of the parameters we'll build. When T is
12206 a function parameter pack, LEN is the number of expanded
12207 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12208 r = NULL_TREE;
12209 for (i = 0; i < len; ++i)
12211 prev_r = r;
12212 r = copy_node (t);
12213 if (DECL_TEMPLATE_PARM_P (t))
12214 SET_DECL_TEMPLATE_PARM_P (r);
12216 if (expanded_types)
12217 /* We're on the Ith parameter of the function parameter
12218 pack. */
12220 /* Get the Ith type. */
12221 type = TREE_VEC_ELT (expanded_types, i);
12223 /* Rename the parameter to include the index. */
12224 DECL_NAME (r)
12225 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12227 else if (!type)
12228 /* We're dealing with a normal parameter. */
12229 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12231 type = type_decays_to (type);
12232 TREE_TYPE (r) = type;
12233 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12235 if (DECL_INITIAL (r))
12237 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12238 DECL_INITIAL (r) = TREE_TYPE (r);
12239 else
12240 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12241 complain, in_decl);
12244 DECL_CONTEXT (r) = NULL_TREE;
12246 if (!DECL_TEMPLATE_PARM_P (r))
12247 DECL_ARG_TYPE (r) = type_passed_as (type);
12249 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12250 args, complain, in_decl);
12252 /* Keep track of the first new parameter we
12253 generate. That's what will be returned to the
12254 caller. */
12255 if (!first_r)
12256 first_r = r;
12258 /* Build a proper chain of parameters when substituting
12259 into a function parameter pack. */
12260 if (prev_r)
12261 DECL_CHAIN (prev_r) = r;
12264 /* If cp_unevaluated_operand is set, we're just looking for a
12265 single dummy parameter, so don't keep going. */
12266 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12267 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12268 complain, DECL_CHAIN (t));
12270 /* FIRST_R contains the start of the chain we've built. */
12271 r = first_r;
12273 break;
12275 case FIELD_DECL:
12277 tree type = NULL_TREE;
12278 tree vec = NULL_TREE;
12279 tree expanded_types = NULL_TREE;
12280 int len = 1;
12282 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12284 /* This field is a lambda capture pack. Return a TREE_VEC of
12285 the expanded fields to instantiate_class_template_1 and
12286 store them in the specializations hash table as a
12287 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12288 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12289 complain, in_decl);
12290 if (TREE_CODE (expanded_types) == TREE_VEC)
12292 len = TREE_VEC_LENGTH (expanded_types);
12293 vec = make_tree_vec (len);
12295 else
12297 /* All we did was update the type. Make a note of that. */
12298 type = expanded_types;
12299 expanded_types = NULL_TREE;
12303 for (int i = 0; i < len; ++i)
12305 r = copy_decl (t);
12306 if (expanded_types)
12308 type = TREE_VEC_ELT (expanded_types, i);
12309 DECL_NAME (r)
12310 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12312 else if (!type)
12313 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12315 if (type == error_mark_node)
12316 RETURN (error_mark_node);
12317 TREE_TYPE (r) = type;
12318 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12320 if (DECL_C_BIT_FIELD (r))
12321 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12322 non-bit-fields DECL_INITIAL is a non-static data member
12323 initializer, which gets deferred instantiation. */
12324 DECL_INITIAL (r)
12325 = tsubst_expr (DECL_INITIAL (t), args,
12326 complain, in_decl,
12327 /*integral_constant_expression_p=*/true);
12328 else if (DECL_INITIAL (t))
12330 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12331 NSDMI in perform_member_init. Still set DECL_INITIAL
12332 so that we know there is one. */
12333 DECL_INITIAL (r) = void_node;
12334 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12335 retrofit_lang_decl (r);
12336 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12338 /* We don't have to set DECL_CONTEXT here; it is set by
12339 finish_member_declaration. */
12340 DECL_CHAIN (r) = NULL_TREE;
12342 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12343 args, complain, in_decl);
12345 if (vec)
12346 TREE_VEC_ELT (vec, i) = r;
12349 if (vec)
12351 r = vec;
12352 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12353 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12354 SET_ARGUMENT_PACK_ARGS (pack, vec);
12355 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12356 TREE_TYPE (pack) = tpack;
12357 register_specialization (pack, t, args, false, 0);
12360 break;
12362 case USING_DECL:
12363 /* We reach here only for member using decls. We also need to check
12364 uses_template_parms because DECL_DEPENDENT_P is not set for a
12365 using-declaration that designates a member of the current
12366 instantiation (c++/53549). */
12367 if (DECL_DEPENDENT_P (t)
12368 || uses_template_parms (USING_DECL_SCOPE (t)))
12370 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12371 complain, in_decl);
12372 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12373 r = do_class_using_decl (inst_scope, name);
12374 if (!r)
12375 r = error_mark_node;
12376 else
12378 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12379 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12382 else
12384 r = copy_node (t);
12385 DECL_CHAIN (r) = NULL_TREE;
12387 break;
12389 case TYPE_DECL:
12390 case VAR_DECL:
12392 tree argvec = NULL_TREE;
12393 tree gen_tmpl = NULL_TREE;
12394 tree spec;
12395 tree tmpl = NULL_TREE;
12396 tree ctx;
12397 tree type = NULL_TREE;
12398 bool local_p;
12400 if (TREE_TYPE (t) == error_mark_node)
12401 RETURN (error_mark_node);
12403 if (TREE_CODE (t) == TYPE_DECL
12404 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12406 /* If this is the canonical decl, we don't have to
12407 mess with instantiations, and often we can't (for
12408 typename, template type parms and such). Note that
12409 TYPE_NAME is not correct for the above test if
12410 we've copied the type for a typedef. */
12411 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12412 if (type == error_mark_node)
12413 RETURN (error_mark_node);
12414 r = TYPE_NAME (type);
12415 break;
12418 /* Check to see if we already have the specialization we
12419 need. */
12420 spec = NULL_TREE;
12421 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12423 /* T is a static data member or namespace-scope entity.
12424 We have to substitute into namespace-scope variables
12425 (not just variable templates) because of cases like:
12427 template <class T> void f() { extern T t; }
12429 where the entity referenced is not known until
12430 instantiation time. */
12431 local_p = false;
12432 ctx = DECL_CONTEXT (t);
12433 if (DECL_CLASS_SCOPE_P (t))
12435 ctx = tsubst_aggr_type (ctx, args,
12436 complain,
12437 in_decl, /*entering_scope=*/1);
12438 /* If CTX is unchanged, then T is in fact the
12439 specialization we want. That situation occurs when
12440 referencing a static data member within in its own
12441 class. We can use pointer equality, rather than
12442 same_type_p, because DECL_CONTEXT is always
12443 canonical... */
12444 if (ctx == DECL_CONTEXT (t)
12445 /* ... unless T is a member template; in which
12446 case our caller can be willing to create a
12447 specialization of that template represented
12448 by T. */
12449 && !(DECL_TI_TEMPLATE (t)
12450 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12451 spec = t;
12454 if (!spec)
12456 tmpl = DECL_TI_TEMPLATE (t);
12457 gen_tmpl = most_general_template (tmpl);
12458 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12459 if (argvec != error_mark_node)
12460 argvec = (coerce_innermost_template_parms
12461 (DECL_TEMPLATE_PARMS (gen_tmpl),
12462 argvec, t, complain,
12463 /*all*/true, /*defarg*/true));
12464 if (argvec == error_mark_node)
12465 RETURN (error_mark_node);
12466 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12467 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12470 else
12472 /* A local variable. */
12473 local_p = true;
12474 /* Subsequent calls to pushdecl will fill this in. */
12475 ctx = NULL_TREE;
12476 /* Unless this is a reference to a static variable from an
12477 enclosing function, in which case we need to fill it in now. */
12478 if (TREE_STATIC (t))
12480 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12481 if (fn != current_function_decl)
12482 ctx = fn;
12484 spec = retrieve_local_specialization (t);
12486 /* If we already have the specialization we need, there is
12487 nothing more to do. */
12488 if (spec)
12490 r = spec;
12491 break;
12494 /* Create a new node for the specialization we need. */
12495 r = copy_decl (t);
12496 if (type == NULL_TREE)
12498 if (is_typedef_decl (t))
12499 type = DECL_ORIGINAL_TYPE (t);
12500 else
12501 type = TREE_TYPE (t);
12502 if (VAR_P (t)
12503 && VAR_HAD_UNKNOWN_BOUND (t)
12504 && type != error_mark_node)
12505 type = strip_array_domain (type);
12506 type = tsubst (type, args, complain, in_decl);
12508 if (VAR_P (r))
12510 /* Even if the original location is out of scope, the
12511 newly substituted one is not. */
12512 DECL_DEAD_FOR_LOCAL (r) = 0;
12513 DECL_INITIALIZED_P (r) = 0;
12514 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12515 if (type == error_mark_node)
12516 RETURN (error_mark_node);
12517 if (TREE_CODE (type) == FUNCTION_TYPE)
12519 /* It may seem that this case cannot occur, since:
12521 typedef void f();
12522 void g() { f x; }
12524 declares a function, not a variable. However:
12526 typedef void f();
12527 template <typename T> void g() { T t; }
12528 template void g<f>();
12530 is an attempt to declare a variable with function
12531 type. */
12532 error ("variable %qD has function type",
12533 /* R is not yet sufficiently initialized, so we
12534 just use its name. */
12535 DECL_NAME (r));
12536 RETURN (error_mark_node);
12538 type = complete_type (type);
12539 /* Wait until cp_finish_decl to set this again, to handle
12540 circular dependency (template/instantiate6.C). */
12541 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12542 type = check_var_type (DECL_NAME (r), type);
12544 if (DECL_HAS_VALUE_EXPR_P (t))
12546 tree ve = DECL_VALUE_EXPR (t);
12547 ve = tsubst_expr (ve, args, complain, in_decl,
12548 /*constant_expression_p=*/false);
12549 if (REFERENCE_REF_P (ve))
12551 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12552 ve = TREE_OPERAND (ve, 0);
12554 SET_DECL_VALUE_EXPR (r, ve);
12556 if (CP_DECL_THREAD_LOCAL_P (r)
12557 && !processing_template_decl)
12558 set_decl_tls_model (r, decl_default_tls_model (r));
12560 else if (DECL_SELF_REFERENCE_P (t))
12561 SET_DECL_SELF_REFERENCE_P (r);
12562 TREE_TYPE (r) = type;
12563 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12564 DECL_CONTEXT (r) = ctx;
12565 /* Clear out the mangled name and RTL for the instantiation. */
12566 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12567 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12568 SET_DECL_RTL (r, NULL);
12569 /* The initializer must not be expanded until it is required;
12570 see [temp.inst]. */
12571 DECL_INITIAL (r) = NULL_TREE;
12572 if (VAR_P (r))
12573 SET_DECL_MODE (r, VOIDmode);
12574 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12575 SET_DECL_RTL (r, NULL);
12576 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12577 if (VAR_P (r))
12579 /* Possibly limit visibility based on template args. */
12580 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12581 if (DECL_VISIBILITY_SPECIFIED (t))
12583 DECL_VISIBILITY_SPECIFIED (r) = 0;
12584 DECL_ATTRIBUTES (r)
12585 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12587 determine_visibility (r);
12590 if (!local_p)
12592 /* A static data member declaration is always marked
12593 external when it is declared in-class, even if an
12594 initializer is present. We mimic the non-template
12595 processing here. */
12596 DECL_EXTERNAL (r) = 1;
12597 if (DECL_NAMESPACE_SCOPE_P (t))
12598 DECL_NOT_REALLY_EXTERN (r) = 1;
12600 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12601 SET_DECL_IMPLICIT_INSTANTIATION (r);
12602 register_specialization (r, gen_tmpl, argvec, false, hash);
12604 else
12606 if (DECL_LANG_SPECIFIC (r))
12607 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12608 if (!cp_unevaluated_operand)
12609 register_local_specialization (r, t);
12612 DECL_CHAIN (r) = NULL_TREE;
12614 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12615 /*flags=*/0,
12616 args, complain, in_decl);
12618 /* Preserve a typedef that names a type. */
12619 if (is_typedef_decl (r))
12621 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12622 set_underlying_type (r);
12623 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12624 /* An alias template specialization can be dependent
12625 even if its underlying type is not. */
12626 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12629 layout_decl (r, 0);
12631 break;
12633 default:
12634 gcc_unreachable ();
12636 #undef RETURN
12638 out:
12639 /* Restore the file and line information. */
12640 input_location = saved_loc;
12642 return r;
12645 /* Substitute into the ARG_TYPES of a function type.
12646 If END is a TREE_CHAIN, leave it and any following types
12647 un-substituted. */
12649 static tree
12650 tsubst_arg_types (tree arg_types,
12651 tree args,
12652 tree end,
12653 tsubst_flags_t complain,
12654 tree in_decl)
12656 tree remaining_arg_types;
12657 tree type = NULL_TREE;
12658 int i = 1;
12659 tree expanded_args = NULL_TREE;
12660 tree default_arg;
12662 if (!arg_types || arg_types == void_list_node || arg_types == end)
12663 return arg_types;
12665 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12666 args, end, complain, in_decl);
12667 if (remaining_arg_types == error_mark_node)
12668 return error_mark_node;
12670 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12672 /* For a pack expansion, perform substitution on the
12673 entire expression. Later on, we'll handle the arguments
12674 one-by-one. */
12675 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12676 args, complain, in_decl);
12678 if (TREE_CODE (expanded_args) == TREE_VEC)
12679 /* So that we'll spin through the parameters, one by one. */
12680 i = TREE_VEC_LENGTH (expanded_args);
12681 else
12683 /* We only partially substituted into the parameter
12684 pack. Our type is TYPE_PACK_EXPANSION. */
12685 type = expanded_args;
12686 expanded_args = NULL_TREE;
12690 while (i > 0) {
12691 --i;
12693 if (expanded_args)
12694 type = TREE_VEC_ELT (expanded_args, i);
12695 else if (!type)
12696 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12698 if (type == error_mark_node)
12699 return error_mark_node;
12700 if (VOID_TYPE_P (type))
12702 if (complain & tf_error)
12704 error ("invalid parameter type %qT", type);
12705 if (in_decl)
12706 error ("in declaration %q+D", in_decl);
12708 return error_mark_node;
12710 /* DR 657. */
12711 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12712 return error_mark_node;
12714 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12715 top-level qualifiers as required. */
12716 type = cv_unqualified (type_decays_to (type));
12718 /* We do not substitute into default arguments here. The standard
12719 mandates that they be instantiated only when needed, which is
12720 done in build_over_call. */
12721 default_arg = TREE_PURPOSE (arg_types);
12723 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12725 /* We've instantiated a template before its default arguments
12726 have been parsed. This can happen for a nested template
12727 class, and is not an error unless we require the default
12728 argument in a call of this function. */
12729 remaining_arg_types =
12730 tree_cons (default_arg, type, remaining_arg_types);
12731 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12733 else
12734 remaining_arg_types =
12735 hash_tree_cons (default_arg, type, remaining_arg_types);
12738 return remaining_arg_types;
12741 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12742 *not* handle the exception-specification for FNTYPE, because the
12743 initial substitution of explicitly provided template parameters
12744 during argument deduction forbids substitution into the
12745 exception-specification:
12747 [temp.deduct]
12749 All references in the function type of the function template to the
12750 corresponding template parameters are replaced by the specified tem-
12751 plate argument values. If a substitution in a template parameter or
12752 in the function type of the function template results in an invalid
12753 type, type deduction fails. [Note: The equivalent substitution in
12754 exception specifications is done only when the function is instanti-
12755 ated, at which point a program is ill-formed if the substitution
12756 results in an invalid type.] */
12758 static tree
12759 tsubst_function_type (tree t,
12760 tree args,
12761 tsubst_flags_t complain,
12762 tree in_decl)
12764 tree return_type;
12765 tree arg_types = NULL_TREE;
12766 tree fntype;
12768 /* The TYPE_CONTEXT is not used for function/method types. */
12769 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12771 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12772 failure. */
12773 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12775 if (late_return_type_p)
12777 /* Substitute the argument types. */
12778 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12779 complain, in_decl);
12780 if (arg_types == error_mark_node)
12781 return error_mark_node;
12783 tree save_ccp = current_class_ptr;
12784 tree save_ccr = current_class_ref;
12785 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12786 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12787 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12788 if (do_inject)
12790 /* DR 1207: 'this' is in scope in the trailing return type. */
12791 inject_this_parameter (this_type, cp_type_quals (this_type));
12794 /* Substitute the return type. */
12795 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12797 if (do_inject)
12799 current_class_ptr = save_ccp;
12800 current_class_ref = save_ccr;
12803 else
12804 /* Substitute the return type. */
12805 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12807 if (return_type == error_mark_node)
12808 return error_mark_node;
12809 /* DR 486 clarifies that creation of a function type with an
12810 invalid return type is a deduction failure. */
12811 if (TREE_CODE (return_type) == ARRAY_TYPE
12812 || TREE_CODE (return_type) == FUNCTION_TYPE)
12814 if (complain & tf_error)
12816 if (TREE_CODE (return_type) == ARRAY_TYPE)
12817 error ("function returning an array");
12818 else
12819 error ("function returning a function");
12821 return error_mark_node;
12823 /* And DR 657. */
12824 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12825 return error_mark_node;
12827 if (!late_return_type_p)
12829 /* Substitute the argument types. */
12830 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12831 complain, in_decl);
12832 if (arg_types == error_mark_node)
12833 return error_mark_node;
12836 /* Construct a new type node and return it. */
12837 if (TREE_CODE (t) == FUNCTION_TYPE)
12839 fntype = build_function_type (return_type, arg_types);
12840 fntype = apply_memfn_quals (fntype,
12841 type_memfn_quals (t),
12842 type_memfn_rqual (t));
12844 else
12846 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12847 /* Don't pick up extra function qualifiers from the basetype. */
12848 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12849 if (! MAYBE_CLASS_TYPE_P (r))
12851 /* [temp.deduct]
12853 Type deduction may fail for any of the following
12854 reasons:
12856 -- Attempting to create "pointer to member of T" when T
12857 is not a class type. */
12858 if (complain & tf_error)
12859 error ("creating pointer to member function of non-class type %qT",
12861 return error_mark_node;
12864 fntype = build_method_type_directly (r, return_type,
12865 TREE_CHAIN (arg_types));
12866 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12868 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12870 if (late_return_type_p)
12871 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12873 return fntype;
12876 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12877 ARGS into that specification, and return the substituted
12878 specification. If there is no specification, return NULL_TREE. */
12880 static tree
12881 tsubst_exception_specification (tree fntype,
12882 tree args,
12883 tsubst_flags_t complain,
12884 tree in_decl,
12885 bool defer_ok)
12887 tree specs;
12888 tree new_specs;
12890 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12891 new_specs = NULL_TREE;
12892 if (specs && TREE_PURPOSE (specs))
12894 /* A noexcept-specifier. */
12895 tree expr = TREE_PURPOSE (specs);
12896 if (TREE_CODE (expr) == INTEGER_CST)
12897 new_specs = expr;
12898 else if (defer_ok)
12900 /* Defer instantiation of noexcept-specifiers to avoid
12901 excessive instantiations (c++/49107). */
12902 new_specs = make_node (DEFERRED_NOEXCEPT);
12903 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12905 /* We already partially instantiated this member template,
12906 so combine the new args with the old. */
12907 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12908 = DEFERRED_NOEXCEPT_PATTERN (expr);
12909 DEFERRED_NOEXCEPT_ARGS (new_specs)
12910 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12912 else
12914 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12915 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12918 else
12919 new_specs = tsubst_copy_and_build
12920 (expr, args, complain, in_decl, /*function_p=*/false,
12921 /*integral_constant_expression_p=*/true);
12922 new_specs = build_noexcept_spec (new_specs, complain);
12924 else if (specs)
12926 if (! TREE_VALUE (specs))
12927 new_specs = specs;
12928 else
12929 while (specs)
12931 tree spec;
12932 int i, len = 1;
12933 tree expanded_specs = NULL_TREE;
12935 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12937 /* Expand the pack expansion type. */
12938 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12939 args, complain,
12940 in_decl);
12942 if (expanded_specs == error_mark_node)
12943 return error_mark_node;
12944 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12945 len = TREE_VEC_LENGTH (expanded_specs);
12946 else
12948 /* We're substituting into a member template, so
12949 we got a TYPE_PACK_EXPANSION back. Add that
12950 expansion and move on. */
12951 gcc_assert (TREE_CODE (expanded_specs)
12952 == TYPE_PACK_EXPANSION);
12953 new_specs = add_exception_specifier (new_specs,
12954 expanded_specs,
12955 complain);
12956 specs = TREE_CHAIN (specs);
12957 continue;
12961 for (i = 0; i < len; ++i)
12963 if (expanded_specs)
12964 spec = TREE_VEC_ELT (expanded_specs, i);
12965 else
12966 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12967 if (spec == error_mark_node)
12968 return spec;
12969 new_specs = add_exception_specifier (new_specs, spec,
12970 complain);
12973 specs = TREE_CHAIN (specs);
12976 return new_specs;
12979 /* Take the tree structure T and replace template parameters used
12980 therein with the argument vector ARGS. IN_DECL is an associated
12981 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12982 Issue error and warning messages under control of COMPLAIN. Note
12983 that we must be relatively non-tolerant of extensions here, in
12984 order to preserve conformance; if we allow substitutions that
12985 should not be allowed, we may allow argument deductions that should
12986 not succeed, and therefore report ambiguous overload situations
12987 where there are none. In theory, we could allow the substitution,
12988 but indicate that it should have failed, and allow our caller to
12989 make sure that the right thing happens, but we don't try to do this
12990 yet.
12992 This function is used for dealing with types, decls and the like;
12993 for expressions, use tsubst_expr or tsubst_copy. */
12995 tree
12996 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12998 enum tree_code code;
12999 tree type, r = NULL_TREE;
13001 if (t == NULL_TREE || t == error_mark_node
13002 || t == integer_type_node
13003 || t == void_type_node
13004 || t == char_type_node
13005 || t == unknown_type_node
13006 || TREE_CODE (t) == NAMESPACE_DECL
13007 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13008 return t;
13010 if (DECL_P (t))
13011 return tsubst_decl (t, args, complain);
13013 if (args == NULL_TREE)
13014 return t;
13016 code = TREE_CODE (t);
13018 if (code == IDENTIFIER_NODE)
13019 type = IDENTIFIER_TYPE_VALUE (t);
13020 else
13021 type = TREE_TYPE (t);
13023 gcc_assert (type != unknown_type_node);
13025 /* Reuse typedefs. We need to do this to handle dependent attributes,
13026 such as attribute aligned. */
13027 if (TYPE_P (t)
13028 && typedef_variant_p (t))
13030 tree decl = TYPE_NAME (t);
13032 if (alias_template_specialization_p (t))
13034 /* DECL represents an alias template and we want to
13035 instantiate it. */
13036 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13037 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13038 r = instantiate_alias_template (tmpl, gen_args, complain);
13040 else if (DECL_CLASS_SCOPE_P (decl)
13041 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13042 && uses_template_parms (DECL_CONTEXT (decl)))
13044 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13045 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13046 r = retrieve_specialization (tmpl, gen_args, 0);
13048 else if (DECL_FUNCTION_SCOPE_P (decl)
13049 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13050 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13051 r = retrieve_local_specialization (decl);
13052 else
13053 /* The typedef is from a non-template context. */
13054 return t;
13056 if (r)
13058 r = TREE_TYPE (r);
13059 r = cp_build_qualified_type_real
13060 (r, cp_type_quals (t) | cp_type_quals (r),
13061 complain | tf_ignore_bad_quals);
13062 return r;
13064 else
13066 /* We don't have an instantiation yet, so drop the typedef. */
13067 int quals = cp_type_quals (t);
13068 t = DECL_ORIGINAL_TYPE (decl);
13069 t = cp_build_qualified_type_real (t, quals,
13070 complain | tf_ignore_bad_quals);
13074 bool fndecl_type = (complain & tf_fndecl_type);
13075 complain &= ~tf_fndecl_type;
13077 if (type
13078 && code != TYPENAME_TYPE
13079 && code != TEMPLATE_TYPE_PARM
13080 && code != IDENTIFIER_NODE
13081 && code != FUNCTION_TYPE
13082 && code != METHOD_TYPE)
13083 type = tsubst (type, args, complain, in_decl);
13084 if (type == error_mark_node)
13085 return error_mark_node;
13087 switch (code)
13089 case RECORD_TYPE:
13090 case UNION_TYPE:
13091 case ENUMERAL_TYPE:
13092 return tsubst_aggr_type (t, args, complain, in_decl,
13093 /*entering_scope=*/0);
13095 case ERROR_MARK:
13096 case IDENTIFIER_NODE:
13097 case VOID_TYPE:
13098 case REAL_TYPE:
13099 case COMPLEX_TYPE:
13100 case VECTOR_TYPE:
13101 case BOOLEAN_TYPE:
13102 case NULLPTR_TYPE:
13103 case LANG_TYPE:
13104 return t;
13106 case INTEGER_TYPE:
13107 if (t == integer_type_node)
13108 return t;
13110 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13111 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13112 return t;
13115 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13117 max = tsubst_expr (omax, args, complain, in_decl,
13118 /*integral_constant_expression_p=*/false);
13120 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13121 needed. */
13122 if (TREE_CODE (max) == NOP_EXPR
13123 && TREE_SIDE_EFFECTS (omax)
13124 && !TREE_TYPE (max))
13125 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13127 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13128 with TREE_SIDE_EFFECTS that indicates this is not an integral
13129 constant expression. */
13130 if (processing_template_decl
13131 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13133 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13134 TREE_SIDE_EFFECTS (max) = 1;
13137 return compute_array_index_type (NULL_TREE, max, complain);
13140 case TEMPLATE_TYPE_PARM:
13141 case TEMPLATE_TEMPLATE_PARM:
13142 case BOUND_TEMPLATE_TEMPLATE_PARM:
13143 case TEMPLATE_PARM_INDEX:
13145 int idx;
13146 int level;
13147 int levels;
13148 tree arg = NULL_TREE;
13150 /* Early in template argument deduction substitution, we don't
13151 want to reduce the level of 'auto', or it will be confused
13152 with a normal template parm in subsequent deduction. */
13153 if (is_auto (t) && (complain & tf_partial))
13154 return t;
13156 r = NULL_TREE;
13158 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13159 template_parm_level_and_index (t, &level, &idx);
13161 levels = TMPL_ARGS_DEPTH (args);
13162 if (level <= levels
13163 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13165 arg = TMPL_ARG (args, level, idx);
13167 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13169 /* See through ARGUMENT_PACK_SELECT arguments. */
13170 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13171 /* If the selected argument is an expansion E, that most
13172 likely means we were called from
13173 gen_elem_of_pack_expansion_instantiation during the
13174 substituting of pack an argument pack (which Ith
13175 element is a pack expansion, where I is
13176 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13177 In this case, the Ith element resulting from this
13178 substituting is going to be a pack expansion, which
13179 pattern is the pattern of E. Let's return the
13180 pattern of E, and
13181 gen_elem_of_pack_expansion_instantiation will
13182 build the resulting pack expansion from it. */
13183 if (PACK_EXPANSION_P (arg))
13185 /* Make sure we aren't throwing away arg info. */
13186 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13187 arg = PACK_EXPANSION_PATTERN (arg);
13192 if (arg == error_mark_node)
13193 return error_mark_node;
13194 else if (arg != NULL_TREE)
13196 if (ARGUMENT_PACK_P (arg))
13197 /* If ARG is an argument pack, we don't actually want to
13198 perform a substitution here, because substitutions
13199 for argument packs are only done
13200 element-by-element. We can get to this point when
13201 substituting the type of a non-type template
13202 parameter pack, when that type actually contains
13203 template parameter packs from an outer template, e.g.,
13205 template<typename... Types> struct A {
13206 template<Types... Values> struct B { };
13207 }; */
13208 return t;
13210 if (code == TEMPLATE_TYPE_PARM)
13212 int quals;
13213 gcc_assert (TYPE_P (arg));
13215 quals = cp_type_quals (arg) | cp_type_quals (t);
13217 return cp_build_qualified_type_real
13218 (arg, quals, complain | tf_ignore_bad_quals);
13220 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13222 /* We are processing a type constructed from a
13223 template template parameter. */
13224 tree argvec = tsubst (TYPE_TI_ARGS (t),
13225 args, complain, in_decl);
13226 if (argvec == error_mark_node)
13227 return error_mark_node;
13229 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13230 || TREE_CODE (arg) == TEMPLATE_DECL
13231 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13233 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13234 /* Consider this code:
13236 template <template <class> class Template>
13237 struct Internal {
13238 template <class Arg> using Bind = Template<Arg>;
13241 template <template <class> class Template, class Arg>
13242 using Instantiate = Template<Arg>; //#0
13244 template <template <class> class Template,
13245 class Argument>
13246 using Bind =
13247 Instantiate<Internal<Template>::template Bind,
13248 Argument>; //#1
13250 When #1 is parsed, the
13251 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13252 parameter `Template' in #0 matches the
13253 UNBOUND_CLASS_TEMPLATE representing the argument
13254 `Internal<Template>::template Bind'; We then want
13255 to assemble the type `Bind<Argument>' that can't
13256 be fully created right now, because
13257 `Internal<Template>' not being complete, the Bind
13258 template cannot be looked up in that context. So
13259 we need to "store" `Bind<Argument>' for later
13260 when the context of Bind becomes complete. Let's
13261 store that in a TYPENAME_TYPE. */
13262 return make_typename_type (TYPE_CONTEXT (arg),
13263 build_nt (TEMPLATE_ID_EXPR,
13264 TYPE_IDENTIFIER (arg),
13265 argvec),
13266 typename_type,
13267 complain);
13269 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13270 are resolving nested-types in the signature of a
13271 member function templates. Otherwise ARG is a
13272 TEMPLATE_DECL and is the real template to be
13273 instantiated. */
13274 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13275 arg = TYPE_NAME (arg);
13277 r = lookup_template_class (arg,
13278 argvec, in_decl,
13279 DECL_CONTEXT (arg),
13280 /*entering_scope=*/0,
13281 complain);
13282 return cp_build_qualified_type_real
13283 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13285 else if (code == TEMPLATE_TEMPLATE_PARM)
13286 return arg;
13287 else
13288 /* TEMPLATE_PARM_INDEX. */
13289 return convert_from_reference (unshare_expr (arg));
13292 if (level == 1)
13293 /* This can happen during the attempted tsubst'ing in
13294 unify. This means that we don't yet have any information
13295 about the template parameter in question. */
13296 return t;
13298 /* If we get here, we must have been looking at a parm for a
13299 more deeply nested template. Make a new version of this
13300 template parameter, but with a lower level. */
13301 switch (code)
13303 case TEMPLATE_TYPE_PARM:
13304 case TEMPLATE_TEMPLATE_PARM:
13305 case BOUND_TEMPLATE_TEMPLATE_PARM:
13306 if (cp_type_quals (t))
13308 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13309 r = cp_build_qualified_type_real
13310 (r, cp_type_quals (t),
13311 complain | (code == TEMPLATE_TYPE_PARM
13312 ? tf_ignore_bad_quals : 0));
13314 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13315 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13316 && (r = (TEMPLATE_PARM_DESCENDANTS
13317 (TEMPLATE_TYPE_PARM_INDEX (t))))
13318 && (r = TREE_TYPE (r))
13319 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13320 /* Break infinite recursion when substituting the constraints
13321 of a constrained placeholder. */;
13322 else
13324 r = copy_type (t);
13325 TEMPLATE_TYPE_PARM_INDEX (r)
13326 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13327 r, levels, args, complain);
13328 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13329 TYPE_MAIN_VARIANT (r) = r;
13330 TYPE_POINTER_TO (r) = NULL_TREE;
13331 TYPE_REFERENCE_TO (r) = NULL_TREE;
13333 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13335 /* Propagate constraints on placeholders. */
13336 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13337 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13338 = tsubst_constraint (constr, args, complain, in_decl);
13339 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13341 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13342 pl = tsubst (pl, args, complain, in_decl);
13343 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13347 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13348 /* We have reduced the level of the template
13349 template parameter, but not the levels of its
13350 template parameters, so canonical_type_parameter
13351 will not be able to find the canonical template
13352 template parameter for this level. Thus, we
13353 require structural equality checking to compare
13354 TEMPLATE_TEMPLATE_PARMs. */
13355 SET_TYPE_STRUCTURAL_EQUALITY (r);
13356 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13357 SET_TYPE_STRUCTURAL_EQUALITY (r);
13358 else
13359 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13361 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13363 tree tinfo = TYPE_TEMPLATE_INFO (t);
13364 /* We might need to substitute into the types of non-type
13365 template parameters. */
13366 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13367 complain, in_decl);
13368 if (tmpl == error_mark_node)
13369 return error_mark_node;
13370 tree argvec = tsubst (TI_ARGS (tinfo), args,
13371 complain, in_decl);
13372 if (argvec == error_mark_node)
13373 return error_mark_node;
13375 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13376 = build_template_info (tmpl, argvec);
13379 break;
13381 case TEMPLATE_PARM_INDEX:
13382 r = reduce_template_parm_level (t, type, levels, args, complain);
13383 break;
13385 default:
13386 gcc_unreachable ();
13389 return r;
13392 case TREE_LIST:
13394 tree purpose, value, chain;
13396 if (t == void_list_node)
13397 return t;
13399 purpose = TREE_PURPOSE (t);
13400 if (purpose)
13402 purpose = tsubst (purpose, args, complain, in_decl);
13403 if (purpose == error_mark_node)
13404 return error_mark_node;
13406 value = TREE_VALUE (t);
13407 if (value)
13409 value = tsubst (value, args, complain, in_decl);
13410 if (value == error_mark_node)
13411 return error_mark_node;
13413 chain = TREE_CHAIN (t);
13414 if (chain && chain != void_type_node)
13416 chain = tsubst (chain, args, complain, in_decl);
13417 if (chain == error_mark_node)
13418 return error_mark_node;
13420 if (purpose == TREE_PURPOSE (t)
13421 && value == TREE_VALUE (t)
13422 && chain == TREE_CHAIN (t))
13423 return t;
13424 return hash_tree_cons (purpose, value, chain);
13427 case TREE_BINFO:
13428 /* We should never be tsubsting a binfo. */
13429 gcc_unreachable ();
13431 case TREE_VEC:
13432 /* A vector of template arguments. */
13433 gcc_assert (!type);
13434 return tsubst_template_args (t, args, complain, in_decl);
13436 case POINTER_TYPE:
13437 case REFERENCE_TYPE:
13439 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13440 return t;
13442 /* [temp.deduct]
13444 Type deduction may fail for any of the following
13445 reasons:
13447 -- Attempting to create a pointer to reference type.
13448 -- Attempting to create a reference to a reference type or
13449 a reference to void.
13451 Core issue 106 says that creating a reference to a reference
13452 during instantiation is no longer a cause for failure. We
13453 only enforce this check in strict C++98 mode. */
13454 if ((TREE_CODE (type) == REFERENCE_TYPE
13455 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13456 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13458 static location_t last_loc;
13460 /* We keep track of the last time we issued this error
13461 message to avoid spewing a ton of messages during a
13462 single bad template instantiation. */
13463 if (complain & tf_error
13464 && last_loc != input_location)
13466 if (VOID_TYPE_P (type))
13467 error ("forming reference to void");
13468 else if (code == POINTER_TYPE)
13469 error ("forming pointer to reference type %qT", type);
13470 else
13471 error ("forming reference to reference type %qT", type);
13472 last_loc = input_location;
13475 return error_mark_node;
13477 else if (TREE_CODE (type) == FUNCTION_TYPE
13478 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13479 || type_memfn_rqual (type) != REF_QUAL_NONE))
13481 if (complain & tf_error)
13483 if (code == POINTER_TYPE)
13484 error ("forming pointer to qualified function type %qT",
13485 type);
13486 else
13487 error ("forming reference to qualified function type %qT",
13488 type);
13490 return error_mark_node;
13492 else if (code == POINTER_TYPE)
13494 r = build_pointer_type (type);
13495 if (TREE_CODE (type) == METHOD_TYPE)
13496 r = build_ptrmemfunc_type (r);
13498 else if (TREE_CODE (type) == REFERENCE_TYPE)
13499 /* In C++0x, during template argument substitution, when there is an
13500 attempt to create a reference to a reference type, reference
13501 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13503 "If a template-argument for a template-parameter T names a type
13504 that is a reference to a type A, an attempt to create the type
13505 'lvalue reference to cv T' creates the type 'lvalue reference to
13506 A,' while an attempt to create the type type rvalue reference to
13507 cv T' creates the type T"
13509 r = cp_build_reference_type
13510 (TREE_TYPE (type),
13511 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13512 else
13513 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13514 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13516 if (r != error_mark_node)
13517 /* Will this ever be needed for TYPE_..._TO values? */
13518 layout_type (r);
13520 return r;
13522 case OFFSET_TYPE:
13524 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13525 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13527 /* [temp.deduct]
13529 Type deduction may fail for any of the following
13530 reasons:
13532 -- Attempting to create "pointer to member of T" when T
13533 is not a class type. */
13534 if (complain & tf_error)
13535 error ("creating pointer to member of non-class type %qT", r);
13536 return error_mark_node;
13538 if (TREE_CODE (type) == REFERENCE_TYPE)
13540 if (complain & tf_error)
13541 error ("creating pointer to member reference type %qT", type);
13542 return error_mark_node;
13544 if (VOID_TYPE_P (type))
13546 if (complain & tf_error)
13547 error ("creating pointer to member of type void");
13548 return error_mark_node;
13550 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13551 if (TREE_CODE (type) == FUNCTION_TYPE)
13553 /* The type of the implicit object parameter gets its
13554 cv-qualifiers from the FUNCTION_TYPE. */
13555 tree memptr;
13556 tree method_type
13557 = build_memfn_type (type, r, type_memfn_quals (type),
13558 type_memfn_rqual (type));
13559 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13560 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13561 complain);
13563 else
13564 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13565 cp_type_quals (t),
13566 complain);
13568 case FUNCTION_TYPE:
13569 case METHOD_TYPE:
13571 tree fntype;
13572 tree specs;
13573 fntype = tsubst_function_type (t, args, complain, in_decl);
13574 if (fntype == error_mark_node)
13575 return error_mark_node;
13577 /* Substitute the exception specification. */
13578 specs = tsubst_exception_specification (t, args, complain, in_decl,
13579 /*defer_ok*/fndecl_type);
13580 if (specs == error_mark_node)
13581 return error_mark_node;
13582 if (specs)
13583 fntype = build_exception_variant (fntype, specs);
13584 return fntype;
13586 case ARRAY_TYPE:
13588 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13589 if (domain == error_mark_node)
13590 return error_mark_node;
13592 /* As an optimization, we avoid regenerating the array type if
13593 it will obviously be the same as T. */
13594 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13595 return t;
13597 /* These checks should match the ones in create_array_type_for_decl.
13599 [temp.deduct]
13601 The deduction may fail for any of the following reasons:
13603 -- Attempting to create an array with an element type that
13604 is void, a function type, or a reference type, or [DR337]
13605 an abstract class type. */
13606 if (VOID_TYPE_P (type)
13607 || TREE_CODE (type) == FUNCTION_TYPE
13608 || (TREE_CODE (type) == ARRAY_TYPE
13609 && TYPE_DOMAIN (type) == NULL_TREE)
13610 || TREE_CODE (type) == REFERENCE_TYPE)
13612 if (complain & tf_error)
13613 error ("creating array of %qT", type);
13614 return error_mark_node;
13617 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13618 return error_mark_node;
13620 r = build_cplus_array_type (type, domain);
13622 if (TYPE_USER_ALIGN (t))
13624 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13625 TYPE_USER_ALIGN (r) = 1;
13628 return r;
13631 case TYPENAME_TYPE:
13633 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13634 in_decl, /*entering_scope=*/1);
13635 if (ctx == error_mark_node)
13636 return error_mark_node;
13638 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13639 complain, in_decl);
13640 if (f == error_mark_node)
13641 return error_mark_node;
13643 if (!MAYBE_CLASS_TYPE_P (ctx))
13645 if (complain & tf_error)
13646 error ("%qT is not a class, struct, or union type", ctx);
13647 return error_mark_node;
13649 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13651 /* Normally, make_typename_type does not require that the CTX
13652 have complete type in order to allow things like:
13654 template <class T> struct S { typename S<T>::X Y; };
13656 But, such constructs have already been resolved by this
13657 point, so here CTX really should have complete type, unless
13658 it's a partial instantiation. */
13659 ctx = complete_type (ctx);
13660 if (!COMPLETE_TYPE_P (ctx))
13662 if (complain & tf_error)
13663 cxx_incomplete_type_error (NULL_TREE, ctx);
13664 return error_mark_node;
13668 f = make_typename_type (ctx, f, typename_type,
13669 complain | tf_keep_type_decl);
13670 if (f == error_mark_node)
13671 return f;
13672 if (TREE_CODE (f) == TYPE_DECL)
13674 complain |= tf_ignore_bad_quals;
13675 f = TREE_TYPE (f);
13678 if (TREE_CODE (f) != TYPENAME_TYPE)
13680 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13682 if (complain & tf_error)
13683 error ("%qT resolves to %qT, which is not an enumeration type",
13684 t, f);
13685 else
13686 return error_mark_node;
13688 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13690 if (complain & tf_error)
13691 error ("%qT resolves to %qT, which is is not a class type",
13692 t, f);
13693 else
13694 return error_mark_node;
13698 return cp_build_qualified_type_real
13699 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13702 case UNBOUND_CLASS_TEMPLATE:
13704 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13705 in_decl, /*entering_scope=*/1);
13706 tree name = TYPE_IDENTIFIER (t);
13707 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13709 if (ctx == error_mark_node || name == error_mark_node)
13710 return error_mark_node;
13712 if (parm_list)
13713 parm_list = tsubst_template_parms (parm_list, args, complain);
13714 return make_unbound_class_template (ctx, name, parm_list, complain);
13717 case TYPEOF_TYPE:
13719 tree type;
13721 ++cp_unevaluated_operand;
13722 ++c_inhibit_evaluation_warnings;
13724 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13725 complain, in_decl,
13726 /*integral_constant_expression_p=*/false);
13728 --cp_unevaluated_operand;
13729 --c_inhibit_evaluation_warnings;
13731 type = finish_typeof (type);
13732 return cp_build_qualified_type_real (type,
13733 cp_type_quals (t)
13734 | cp_type_quals (type),
13735 complain);
13738 case DECLTYPE_TYPE:
13740 tree type;
13742 ++cp_unevaluated_operand;
13743 ++c_inhibit_evaluation_warnings;
13745 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13746 complain|tf_decltype, in_decl,
13747 /*function_p*/false,
13748 /*integral_constant_expression*/false);
13750 if (DECLTYPE_FOR_INIT_CAPTURE (t))
13752 if (type == NULL_TREE)
13754 if (complain & tf_error)
13755 error ("empty initializer in lambda init-capture");
13756 type = error_mark_node;
13758 else if (TREE_CODE (type) == TREE_LIST)
13759 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
13762 --cp_unevaluated_operand;
13763 --c_inhibit_evaluation_warnings;
13765 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13766 type = lambda_capture_field_type (type,
13767 DECLTYPE_FOR_INIT_CAPTURE (t));
13768 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13769 type = lambda_proxy_type (type);
13770 else
13772 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13773 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13774 && EXPR_P (type))
13775 /* In a template ~id could be either a complement expression
13776 or an unqualified-id naming a destructor; if instantiating
13777 it produces an expression, it's not an id-expression or
13778 member access. */
13779 id = false;
13780 type = finish_decltype_type (type, id, complain);
13782 return cp_build_qualified_type_real (type,
13783 cp_type_quals (t)
13784 | cp_type_quals (type),
13785 complain | tf_ignore_bad_quals);
13788 case UNDERLYING_TYPE:
13790 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13791 complain, in_decl);
13792 return finish_underlying_type (type);
13795 case TYPE_ARGUMENT_PACK:
13796 case NONTYPE_ARGUMENT_PACK:
13798 tree r;
13800 if (code == NONTYPE_ARGUMENT_PACK)
13802 r = make_node (code);
13803 /* Set the already-substituted type. */
13804 TREE_TYPE (r) = type;
13806 else
13807 r = cxx_make_type (code);
13809 tree pack_args = ARGUMENT_PACK_ARGS (t);
13810 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
13811 SET_ARGUMENT_PACK_ARGS (r, pack_args);
13813 return r;
13816 case VOID_CST:
13817 case INTEGER_CST:
13818 case REAL_CST:
13819 case STRING_CST:
13820 case PLUS_EXPR:
13821 case MINUS_EXPR:
13822 case NEGATE_EXPR:
13823 case NOP_EXPR:
13824 case INDIRECT_REF:
13825 case ADDR_EXPR:
13826 case CALL_EXPR:
13827 case ARRAY_REF:
13828 case SCOPE_REF:
13829 /* We should use one of the expression tsubsts for these codes. */
13830 gcc_unreachable ();
13832 default:
13833 sorry ("use of %qs in template", get_tree_code_name (code));
13834 return error_mark_node;
13838 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13839 type of the expression on the left-hand side of the "." or "->"
13840 operator. */
13842 static tree
13843 tsubst_baselink (tree baselink, tree object_type,
13844 tree args, tsubst_flags_t complain, tree in_decl)
13846 tree name;
13847 tree qualifying_scope;
13848 tree fns;
13849 tree optype;
13850 tree template_args = 0;
13851 bool template_id_p = false;
13852 bool qualified = BASELINK_QUALIFIED_P (baselink);
13854 /* A baselink indicates a function from a base class. Both the
13855 BASELINK_ACCESS_BINFO and the base class referenced may
13856 indicate bases of the template class, rather than the
13857 instantiated class. In addition, lookups that were not
13858 ambiguous before may be ambiguous now. Therefore, we perform
13859 the lookup again. */
13860 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13861 qualifying_scope = tsubst (qualifying_scope, args,
13862 complain, in_decl);
13863 fns = BASELINK_FUNCTIONS (baselink);
13864 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13865 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13867 template_id_p = true;
13868 template_args = TREE_OPERAND (fns, 1);
13869 fns = TREE_OPERAND (fns, 0);
13870 if (template_args)
13871 template_args = tsubst_template_args (template_args, args,
13872 complain, in_decl);
13874 name = DECL_NAME (get_first_fn (fns));
13875 if (IDENTIFIER_TYPENAME_P (name))
13876 name = mangle_conv_op_name_for_type (optype);
13877 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13878 if (!baselink)
13880 if (constructor_name_p (name, qualifying_scope))
13882 if (complain & tf_error)
13883 error ("cannot call constructor %<%T::%D%> directly",
13884 qualifying_scope, name);
13886 return error_mark_node;
13889 /* If lookup found a single function, mark it as used at this
13890 point. (If it lookup found multiple functions the one selected
13891 later by overload resolution will be marked as used at that
13892 point.) */
13893 if (BASELINK_P (baselink))
13894 fns = BASELINK_FUNCTIONS (baselink);
13895 if (!template_id_p && !really_overloaded_fn (fns)
13896 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13897 return error_mark_node;
13899 /* Add back the template arguments, if present. */
13900 if (BASELINK_P (baselink) && template_id_p)
13901 BASELINK_FUNCTIONS (baselink)
13902 = build2 (TEMPLATE_ID_EXPR,
13903 unknown_type_node,
13904 BASELINK_FUNCTIONS (baselink),
13905 template_args);
13906 /* Update the conversion operator type. */
13907 if (BASELINK_P (baselink))
13908 BASELINK_OPTYPE (baselink) = optype;
13910 if (!object_type)
13911 object_type = current_class_type;
13913 if (qualified || name == complete_dtor_identifier)
13915 baselink = adjust_result_of_qualified_name_lookup (baselink,
13916 qualifying_scope,
13917 object_type);
13918 if (!qualified)
13919 /* We need to call adjust_result_of_qualified_name_lookup in case the
13920 destructor names a base class, but we unset BASELINK_QUALIFIED_P
13921 so that we still get virtual function binding. */
13922 BASELINK_QUALIFIED_P (baselink) = false;
13924 return baselink;
13927 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13928 true if the qualified-id will be a postfix-expression in-and-of
13929 itself; false if more of the postfix-expression follows the
13930 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13931 of "&". */
13933 static tree
13934 tsubst_qualified_id (tree qualified_id, tree args,
13935 tsubst_flags_t complain, tree in_decl,
13936 bool done, bool address_p)
13938 tree expr;
13939 tree scope;
13940 tree name;
13941 bool is_template;
13942 tree template_args;
13943 location_t loc = UNKNOWN_LOCATION;
13945 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13947 /* Figure out what name to look up. */
13948 name = TREE_OPERAND (qualified_id, 1);
13949 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13951 is_template = true;
13952 loc = EXPR_LOCATION (name);
13953 template_args = TREE_OPERAND (name, 1);
13954 if (template_args)
13955 template_args = tsubst_template_args (template_args, args,
13956 complain, in_decl);
13957 if (template_args == error_mark_node)
13958 return error_mark_node;
13959 name = TREE_OPERAND (name, 0);
13961 else
13963 is_template = false;
13964 template_args = NULL_TREE;
13967 /* Substitute into the qualifying scope. When there are no ARGS, we
13968 are just trying to simplify a non-dependent expression. In that
13969 case the qualifying scope may be dependent, and, in any case,
13970 substituting will not help. */
13971 scope = TREE_OPERAND (qualified_id, 0);
13972 if (args)
13974 scope = tsubst (scope, args, complain, in_decl);
13975 expr = tsubst_copy (name, args, complain, in_decl);
13977 else
13978 expr = name;
13980 if (dependent_scope_p (scope))
13982 if (is_template)
13983 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13984 tree r = build_qualified_name (NULL_TREE, scope, expr,
13985 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13986 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
13987 return r;
13990 if (!BASELINK_P (name) && !DECL_P (expr))
13992 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13994 /* A BIT_NOT_EXPR is used to represent a destructor. */
13995 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13997 error ("qualifying type %qT does not match destructor name ~%qT",
13998 scope, TREE_OPERAND (expr, 0));
13999 expr = error_mark_node;
14001 else
14002 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14003 /*is_type_p=*/0, false);
14005 else
14006 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14007 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14008 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14010 if (complain & tf_error)
14012 error ("dependent-name %qE is parsed as a non-type, but "
14013 "instantiation yields a type", qualified_id);
14014 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14016 return error_mark_node;
14020 if (DECL_P (expr))
14022 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14023 scope);
14024 /* Remember that there was a reference to this entity. */
14025 if (!mark_used (expr, complain) && !(complain & tf_error))
14026 return error_mark_node;
14029 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14031 if (complain & tf_error)
14032 qualified_name_lookup_error (scope,
14033 TREE_OPERAND (qualified_id, 1),
14034 expr, input_location);
14035 return error_mark_node;
14038 if (is_template)
14040 if (variable_template_p (expr))
14041 expr = lookup_and_finish_template_variable (expr, template_args,
14042 complain);
14043 else
14044 expr = lookup_template_function (expr, template_args);
14047 if (expr == error_mark_node && complain & tf_error)
14048 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14049 expr, input_location);
14050 else if (TYPE_P (scope))
14052 expr = (adjust_result_of_qualified_name_lookup
14053 (expr, scope, current_nonlambda_class_type ()));
14054 expr = (finish_qualified_id_expr
14055 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14056 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14057 /*template_arg_p=*/false, complain));
14060 /* Expressions do not generally have reference type. */
14061 if (TREE_CODE (expr) != SCOPE_REF
14062 /* However, if we're about to form a pointer-to-member, we just
14063 want the referenced member referenced. */
14064 && TREE_CODE (expr) != OFFSET_REF)
14065 expr = convert_from_reference (expr);
14067 if (REF_PARENTHESIZED_P (qualified_id))
14068 expr = force_paren_expr (expr);
14070 return expr;
14073 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14074 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14075 for tsubst. */
14077 static tree
14078 tsubst_init (tree init, tree decl, tree args,
14079 tsubst_flags_t complain, tree in_decl)
14081 if (!init)
14082 return NULL_TREE;
14084 init = tsubst_expr (init, args, complain, in_decl, false);
14086 if (!init && TREE_TYPE (decl) != error_mark_node)
14088 /* If we had an initializer but it
14089 instantiated to nothing,
14090 value-initialize the object. This will
14091 only occur when the initializer was a
14092 pack expansion where the parameter packs
14093 used in that expansion were of length
14094 zero. */
14095 init = build_value_init (TREE_TYPE (decl),
14096 complain);
14097 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14098 init = get_target_expr_sfinae (init, complain);
14101 return init;
14104 /* Like tsubst, but deals with expressions. This function just replaces
14105 template parms; to finish processing the resultant expression, use
14106 tsubst_copy_and_build or tsubst_expr. */
14108 static tree
14109 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14111 enum tree_code code;
14112 tree r;
14114 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14115 return t;
14117 code = TREE_CODE (t);
14119 switch (code)
14121 case PARM_DECL:
14122 r = retrieve_local_specialization (t);
14124 if (r == NULL_TREE)
14126 /* We get here for a use of 'this' in an NSDMI as part of a
14127 constructor call or as part of an aggregate initialization. */
14128 if (DECL_NAME (t) == this_identifier
14129 && ((current_function_decl
14130 && DECL_CONSTRUCTOR_P (current_function_decl))
14131 || (current_class_ref
14132 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14133 return current_class_ptr;
14135 /* This can happen for a parameter name used later in a function
14136 declaration (such as in a late-specified return type). Just
14137 make a dummy decl, since it's only used for its type. */
14138 gcc_assert (cp_unevaluated_operand != 0);
14139 r = tsubst_decl (t, args, complain);
14140 /* Give it the template pattern as its context; its true context
14141 hasn't been instantiated yet and this is good enough for
14142 mangling. */
14143 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14146 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14147 r = ARGUMENT_PACK_SELECT_ARG (r);
14148 if (!mark_used (r, complain) && !(complain & tf_error))
14149 return error_mark_node;
14150 return r;
14152 case CONST_DECL:
14154 tree enum_type;
14155 tree v;
14157 if (DECL_TEMPLATE_PARM_P (t))
14158 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14159 /* There is no need to substitute into namespace-scope
14160 enumerators. */
14161 if (DECL_NAMESPACE_SCOPE_P (t))
14162 return t;
14163 /* If ARGS is NULL, then T is known to be non-dependent. */
14164 if (args == NULL_TREE)
14165 return scalar_constant_value (t);
14167 /* Unfortunately, we cannot just call lookup_name here.
14168 Consider:
14170 template <int I> int f() {
14171 enum E { a = I };
14172 struct S { void g() { E e = a; } };
14175 When we instantiate f<7>::S::g(), say, lookup_name is not
14176 clever enough to find f<7>::a. */
14177 enum_type
14178 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14179 /*entering_scope=*/0);
14181 for (v = TYPE_VALUES (enum_type);
14182 v != NULL_TREE;
14183 v = TREE_CHAIN (v))
14184 if (TREE_PURPOSE (v) == DECL_NAME (t))
14185 return TREE_VALUE (v);
14187 /* We didn't find the name. That should never happen; if
14188 name-lookup found it during preliminary parsing, we
14189 should find it again here during instantiation. */
14190 gcc_unreachable ();
14192 return t;
14194 case FIELD_DECL:
14195 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14197 /* Check for a local specialization set up by
14198 tsubst_pack_expansion. */
14199 if (tree r = retrieve_local_specialization (t))
14201 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14202 r = ARGUMENT_PACK_SELECT_ARG (r);
14203 return r;
14206 /* When retrieving a capture pack from a generic lambda, remove the
14207 lambda call op's own template argument list from ARGS. Only the
14208 template arguments active for the closure type should be used to
14209 retrieve the pack specialization. */
14210 if (LAMBDA_FUNCTION_P (current_function_decl)
14211 && (template_class_depth (DECL_CONTEXT (t))
14212 != TMPL_ARGS_DEPTH (args)))
14213 args = strip_innermost_template_args (args, 1);
14215 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14216 tsubst_decl put in the hash table. */
14217 return retrieve_specialization (t, args, 0);
14220 if (DECL_CONTEXT (t))
14222 tree ctx;
14224 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14225 /*entering_scope=*/1);
14226 if (ctx != DECL_CONTEXT (t))
14228 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14229 if (!r)
14231 if (complain & tf_error)
14232 error ("using invalid field %qD", t);
14233 return error_mark_node;
14235 return r;
14239 return t;
14241 case VAR_DECL:
14242 case FUNCTION_DECL:
14243 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14244 r = tsubst (t, args, complain, in_decl);
14245 else if (local_variable_p (t)
14246 && uses_template_parms (DECL_CONTEXT (t)))
14248 r = retrieve_local_specialization (t);
14249 if (r == NULL_TREE)
14251 /* First try name lookup to find the instantiation. */
14252 r = lookup_name (DECL_NAME (t));
14253 if (r)
14255 /* Make sure that the one we found is the one we want. */
14256 tree ctx = DECL_CONTEXT (t);
14257 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14258 ctx = tsubst (ctx, args, complain, in_decl);
14259 if (ctx != DECL_CONTEXT (r))
14260 r = NULL_TREE;
14263 if (r)
14264 /* OK */;
14265 else
14267 /* This can happen for a variable used in a
14268 late-specified return type of a local lambda, or for a
14269 local static or constant. Building a new VAR_DECL
14270 should be OK in all those cases. */
14271 r = tsubst_decl (t, args, complain);
14272 if (decl_maybe_constant_var_p (r))
14274 /* We can't call cp_finish_decl, so handle the
14275 initializer by hand. */
14276 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14277 complain, in_decl);
14278 if (!processing_template_decl)
14279 init = maybe_constant_init (init);
14280 if (processing_template_decl
14281 ? potential_constant_expression (init)
14282 : reduced_constant_expression_p (init))
14283 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14284 = TREE_CONSTANT (r) = true;
14285 DECL_INITIAL (r) = init;
14287 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14288 || decl_constant_var_p (r)
14289 || errorcount || sorrycount);
14290 if (!processing_template_decl
14291 && !TREE_STATIC (r))
14292 r = process_outer_var_ref (r, complain);
14294 /* Remember this for subsequent uses. */
14295 if (local_specializations)
14296 register_local_specialization (r, t);
14299 else
14300 r = t;
14301 if (!mark_used (r, complain))
14302 return error_mark_node;
14303 return r;
14305 case NAMESPACE_DECL:
14306 return t;
14308 case OVERLOAD:
14309 /* An OVERLOAD will always be a non-dependent overload set; an
14310 overload set from function scope will just be represented with an
14311 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14312 gcc_assert (!uses_template_parms (t));
14313 return t;
14315 case BASELINK:
14316 return tsubst_baselink (t, current_nonlambda_class_type (),
14317 args, complain, in_decl);
14319 case TEMPLATE_DECL:
14320 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14321 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14322 args, complain, in_decl);
14323 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14324 return tsubst (t, args, complain, in_decl);
14325 else if (DECL_CLASS_SCOPE_P (t)
14326 && uses_template_parms (DECL_CONTEXT (t)))
14328 /* Template template argument like the following example need
14329 special treatment:
14331 template <template <class> class TT> struct C {};
14332 template <class T> struct D {
14333 template <class U> struct E {};
14334 C<E> c; // #1
14336 D<int> d; // #2
14338 We are processing the template argument `E' in #1 for
14339 the template instantiation #2. Originally, `E' is a
14340 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14341 have to substitute this with one having context `D<int>'. */
14343 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14344 return lookup_field (context, DECL_NAME(t), 0, false);
14346 else
14347 /* Ordinary template template argument. */
14348 return t;
14350 case CAST_EXPR:
14351 case REINTERPRET_CAST_EXPR:
14352 case CONST_CAST_EXPR:
14353 case STATIC_CAST_EXPR:
14354 case DYNAMIC_CAST_EXPR:
14355 case IMPLICIT_CONV_EXPR:
14356 case CONVERT_EXPR:
14357 case NOP_EXPR:
14359 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14360 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14361 return build1 (code, type, op0);
14364 case SIZEOF_EXPR:
14365 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14366 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14368 tree expanded, op = TREE_OPERAND (t, 0);
14369 int len = 0;
14371 if (SIZEOF_EXPR_TYPE_P (t))
14372 op = TREE_TYPE (op);
14374 ++cp_unevaluated_operand;
14375 ++c_inhibit_evaluation_warnings;
14376 /* We only want to compute the number of arguments. */
14377 if (PACK_EXPANSION_P (op))
14378 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14379 else
14380 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14381 args, complain, in_decl);
14382 --cp_unevaluated_operand;
14383 --c_inhibit_evaluation_warnings;
14385 if (TREE_CODE (expanded) == TREE_VEC)
14387 len = TREE_VEC_LENGTH (expanded);
14388 /* Set TREE_USED for the benefit of -Wunused. */
14389 for (int i = 0; i < len; i++)
14390 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14391 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14394 if (expanded == error_mark_node)
14395 return error_mark_node;
14396 else if (PACK_EXPANSION_P (expanded)
14397 || (TREE_CODE (expanded) == TREE_VEC
14398 && pack_expansion_args_count (expanded)))
14401 if (PACK_EXPANSION_P (expanded))
14402 /* OK. */;
14403 else if (TREE_VEC_LENGTH (expanded) == 1)
14404 expanded = TREE_VEC_ELT (expanded, 0);
14405 else
14406 expanded = make_argument_pack (expanded);
14408 if (TYPE_P (expanded))
14409 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14410 complain & tf_error);
14411 else
14412 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14413 complain & tf_error);
14415 else
14416 return build_int_cst (size_type_node, len);
14418 if (SIZEOF_EXPR_TYPE_P (t))
14420 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14421 args, complain, in_decl);
14422 r = build1 (NOP_EXPR, r, error_mark_node);
14423 r = build1 (SIZEOF_EXPR,
14424 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14425 SIZEOF_EXPR_TYPE_P (r) = 1;
14426 return r;
14428 /* Fall through */
14430 case INDIRECT_REF:
14431 case NEGATE_EXPR:
14432 case TRUTH_NOT_EXPR:
14433 case BIT_NOT_EXPR:
14434 case ADDR_EXPR:
14435 case UNARY_PLUS_EXPR: /* Unary + */
14436 case ALIGNOF_EXPR:
14437 case AT_ENCODE_EXPR:
14438 case ARROW_EXPR:
14439 case THROW_EXPR:
14440 case TYPEID_EXPR:
14441 case REALPART_EXPR:
14442 case IMAGPART_EXPR:
14443 case PAREN_EXPR:
14445 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14446 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14447 return build1 (code, type, op0);
14450 case COMPONENT_REF:
14452 tree object;
14453 tree name;
14455 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14456 name = TREE_OPERAND (t, 1);
14457 if (TREE_CODE (name) == BIT_NOT_EXPR)
14459 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14460 complain, in_decl);
14461 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14463 else if (TREE_CODE (name) == SCOPE_REF
14464 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14466 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14467 complain, in_decl);
14468 name = TREE_OPERAND (name, 1);
14469 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14470 complain, in_decl);
14471 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14472 name = build_qualified_name (/*type=*/NULL_TREE,
14473 base, name,
14474 /*template_p=*/false);
14476 else if (BASELINK_P (name))
14477 name = tsubst_baselink (name,
14478 non_reference (TREE_TYPE (object)),
14479 args, complain,
14480 in_decl);
14481 else
14482 name = tsubst_copy (name, args, complain, in_decl);
14483 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14486 case PLUS_EXPR:
14487 case MINUS_EXPR:
14488 case MULT_EXPR:
14489 case TRUNC_DIV_EXPR:
14490 case CEIL_DIV_EXPR:
14491 case FLOOR_DIV_EXPR:
14492 case ROUND_DIV_EXPR:
14493 case EXACT_DIV_EXPR:
14494 case BIT_AND_EXPR:
14495 case BIT_IOR_EXPR:
14496 case BIT_XOR_EXPR:
14497 case TRUNC_MOD_EXPR:
14498 case FLOOR_MOD_EXPR:
14499 case TRUTH_ANDIF_EXPR:
14500 case TRUTH_ORIF_EXPR:
14501 case TRUTH_AND_EXPR:
14502 case TRUTH_OR_EXPR:
14503 case RSHIFT_EXPR:
14504 case LSHIFT_EXPR:
14505 case RROTATE_EXPR:
14506 case LROTATE_EXPR:
14507 case EQ_EXPR:
14508 case NE_EXPR:
14509 case MAX_EXPR:
14510 case MIN_EXPR:
14511 case LE_EXPR:
14512 case GE_EXPR:
14513 case LT_EXPR:
14514 case GT_EXPR:
14515 case COMPOUND_EXPR:
14516 case DOTSTAR_EXPR:
14517 case MEMBER_REF:
14518 case PREDECREMENT_EXPR:
14519 case PREINCREMENT_EXPR:
14520 case POSTDECREMENT_EXPR:
14521 case POSTINCREMENT_EXPR:
14523 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14524 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14525 return build_nt (code, op0, op1);
14528 case SCOPE_REF:
14530 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14531 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14532 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14533 QUALIFIED_NAME_IS_TEMPLATE (t));
14536 case ARRAY_REF:
14538 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14539 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14540 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14543 case CALL_EXPR:
14545 int n = VL_EXP_OPERAND_LENGTH (t);
14546 tree result = build_vl_exp (CALL_EXPR, n);
14547 int i;
14548 for (i = 0; i < n; i++)
14549 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14550 complain, in_decl);
14551 return result;
14554 case COND_EXPR:
14555 case MODOP_EXPR:
14556 case PSEUDO_DTOR_EXPR:
14557 case VEC_PERM_EXPR:
14559 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14560 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14561 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14562 r = build_nt (code, op0, op1, op2);
14563 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14564 return r;
14567 case NEW_EXPR:
14569 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14570 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14571 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14572 r = build_nt (code, op0, op1, op2);
14573 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14574 return r;
14577 case DELETE_EXPR:
14579 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14580 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14581 r = build_nt (code, op0, op1);
14582 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14583 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14584 return r;
14587 case TEMPLATE_ID_EXPR:
14589 /* Substituted template arguments */
14590 tree fn = TREE_OPERAND (t, 0);
14591 tree targs = TREE_OPERAND (t, 1);
14593 fn = tsubst_copy (fn, args, complain, in_decl);
14594 if (targs)
14595 targs = tsubst_template_args (targs, args, complain, in_decl);
14597 return lookup_template_function (fn, targs);
14600 case TREE_LIST:
14602 tree purpose, value, chain;
14604 if (t == void_list_node)
14605 return t;
14607 purpose = TREE_PURPOSE (t);
14608 if (purpose)
14609 purpose = tsubst_copy (purpose, args, complain, in_decl);
14610 value = TREE_VALUE (t);
14611 if (value)
14612 value = tsubst_copy (value, args, complain, in_decl);
14613 chain = TREE_CHAIN (t);
14614 if (chain && chain != void_type_node)
14615 chain = tsubst_copy (chain, args, complain, in_decl);
14616 if (purpose == TREE_PURPOSE (t)
14617 && value == TREE_VALUE (t)
14618 && chain == TREE_CHAIN (t))
14619 return t;
14620 return tree_cons (purpose, value, chain);
14623 case RECORD_TYPE:
14624 case UNION_TYPE:
14625 case ENUMERAL_TYPE:
14626 case INTEGER_TYPE:
14627 case TEMPLATE_TYPE_PARM:
14628 case TEMPLATE_TEMPLATE_PARM:
14629 case BOUND_TEMPLATE_TEMPLATE_PARM:
14630 case TEMPLATE_PARM_INDEX:
14631 case POINTER_TYPE:
14632 case REFERENCE_TYPE:
14633 case OFFSET_TYPE:
14634 case FUNCTION_TYPE:
14635 case METHOD_TYPE:
14636 case ARRAY_TYPE:
14637 case TYPENAME_TYPE:
14638 case UNBOUND_CLASS_TEMPLATE:
14639 case TYPEOF_TYPE:
14640 case DECLTYPE_TYPE:
14641 case TYPE_DECL:
14642 return tsubst (t, args, complain, in_decl);
14644 case USING_DECL:
14645 t = DECL_NAME (t);
14646 /* Fall through. */
14647 case IDENTIFIER_NODE:
14648 if (IDENTIFIER_TYPENAME_P (t))
14650 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14651 return mangle_conv_op_name_for_type (new_type);
14653 else
14654 return t;
14656 case CONSTRUCTOR:
14657 /* This is handled by tsubst_copy_and_build. */
14658 gcc_unreachable ();
14660 case VA_ARG_EXPR:
14662 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14663 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14664 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14667 case CLEANUP_POINT_EXPR:
14668 /* We shouldn't have built any of these during initial template
14669 generation. Instead, they should be built during instantiation
14670 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14671 gcc_unreachable ();
14673 case OFFSET_REF:
14675 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14676 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14677 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14678 r = build2 (code, type, op0, op1);
14679 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14680 if (!mark_used (TREE_OPERAND (r, 1), complain)
14681 && !(complain & tf_error))
14682 return error_mark_node;
14683 return r;
14686 case EXPR_PACK_EXPANSION:
14687 error ("invalid use of pack expansion expression");
14688 return error_mark_node;
14690 case NONTYPE_ARGUMENT_PACK:
14691 error ("use %<...%> to expand argument pack");
14692 return error_mark_node;
14694 case VOID_CST:
14695 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14696 return t;
14698 case INTEGER_CST:
14699 case REAL_CST:
14700 case STRING_CST:
14701 case COMPLEX_CST:
14703 /* Instantiate any typedefs in the type. */
14704 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14705 r = fold_convert (type, t);
14706 gcc_assert (TREE_CODE (r) == code);
14707 return r;
14710 case PTRMEM_CST:
14711 /* These can sometimes show up in a partial instantiation, but never
14712 involve template parms. */
14713 gcc_assert (!uses_template_parms (t));
14714 return t;
14716 case UNARY_LEFT_FOLD_EXPR:
14717 return tsubst_unary_left_fold (t, args, complain, in_decl);
14718 case UNARY_RIGHT_FOLD_EXPR:
14719 return tsubst_unary_right_fold (t, args, complain, in_decl);
14720 case BINARY_LEFT_FOLD_EXPR:
14721 return tsubst_binary_left_fold (t, args, complain, in_decl);
14722 case BINARY_RIGHT_FOLD_EXPR:
14723 return tsubst_binary_right_fold (t, args, complain, in_decl);
14725 default:
14726 /* We shouldn't get here, but keep going if !flag_checking. */
14727 if (flag_checking)
14728 gcc_unreachable ();
14729 return t;
14733 /* Helper function for tsubst_omp_clauses, used for instantiation of
14734 OMP_CLAUSE_DECL of clauses. */
14736 static tree
14737 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14738 tree in_decl)
14740 if (decl == NULL_TREE)
14741 return NULL_TREE;
14743 /* Handle an OpenMP array section represented as a TREE_LIST (or
14744 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14745 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14746 TREE_LIST. We can handle it exactly the same as an array section
14747 (purpose, value, and a chain), even though the nomenclature
14748 (low_bound, length, etc) is different. */
14749 if (TREE_CODE (decl) == TREE_LIST)
14751 tree low_bound
14752 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14753 /*integral_constant_expression_p=*/false);
14754 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14755 /*integral_constant_expression_p=*/false);
14756 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14757 in_decl);
14758 if (TREE_PURPOSE (decl) == low_bound
14759 && TREE_VALUE (decl) == length
14760 && TREE_CHAIN (decl) == chain)
14761 return decl;
14762 tree ret = tree_cons (low_bound, length, chain);
14763 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
14764 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
14765 return ret;
14767 tree ret = tsubst_expr (decl, args, complain, in_decl,
14768 /*integral_constant_expression_p=*/false);
14769 /* Undo convert_from_reference tsubst_expr could have called. */
14770 if (decl
14771 && REFERENCE_REF_P (ret)
14772 && !REFERENCE_REF_P (decl))
14773 ret = TREE_OPERAND (ret, 0);
14774 return ret;
14777 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14779 static tree
14780 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
14781 tree args, tsubst_flags_t complain, tree in_decl)
14783 tree new_clauses = NULL_TREE, nc, oc;
14784 tree linear_no_step = NULL_TREE;
14786 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14788 nc = copy_node (oc);
14789 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14790 new_clauses = nc;
14792 switch (OMP_CLAUSE_CODE (nc))
14794 case OMP_CLAUSE_LASTPRIVATE:
14795 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14797 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14798 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14799 in_decl, /*integral_constant_expression_p=*/false);
14800 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14801 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14803 /* FALLTHRU */
14804 case OMP_CLAUSE_PRIVATE:
14805 case OMP_CLAUSE_SHARED:
14806 case OMP_CLAUSE_FIRSTPRIVATE:
14807 case OMP_CLAUSE_COPYIN:
14808 case OMP_CLAUSE_COPYPRIVATE:
14809 case OMP_CLAUSE_UNIFORM:
14810 case OMP_CLAUSE_DEPEND:
14811 case OMP_CLAUSE_FROM:
14812 case OMP_CLAUSE_TO:
14813 case OMP_CLAUSE_MAP:
14814 case OMP_CLAUSE_USE_DEVICE_PTR:
14815 case OMP_CLAUSE_IS_DEVICE_PTR:
14816 OMP_CLAUSE_DECL (nc)
14817 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14818 in_decl);
14819 break;
14820 case OMP_CLAUSE_IF:
14821 case OMP_CLAUSE_NUM_THREADS:
14822 case OMP_CLAUSE_SCHEDULE:
14823 case OMP_CLAUSE_COLLAPSE:
14824 case OMP_CLAUSE_FINAL:
14825 case OMP_CLAUSE_DEVICE:
14826 case OMP_CLAUSE_DIST_SCHEDULE:
14827 case OMP_CLAUSE_NUM_TEAMS:
14828 case OMP_CLAUSE_THREAD_LIMIT:
14829 case OMP_CLAUSE_SAFELEN:
14830 case OMP_CLAUSE_SIMDLEN:
14831 case OMP_CLAUSE_NUM_TASKS:
14832 case OMP_CLAUSE_GRAINSIZE:
14833 case OMP_CLAUSE_PRIORITY:
14834 case OMP_CLAUSE_ORDERED:
14835 case OMP_CLAUSE_HINT:
14836 case OMP_CLAUSE_NUM_GANGS:
14837 case OMP_CLAUSE_NUM_WORKERS:
14838 case OMP_CLAUSE_VECTOR_LENGTH:
14839 case OMP_CLAUSE_WORKER:
14840 case OMP_CLAUSE_VECTOR:
14841 case OMP_CLAUSE_ASYNC:
14842 case OMP_CLAUSE_WAIT:
14843 OMP_CLAUSE_OPERAND (nc, 0)
14844 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14845 in_decl, /*integral_constant_expression_p=*/false);
14846 break;
14847 case OMP_CLAUSE_REDUCTION:
14848 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14850 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14851 if (TREE_CODE (placeholder) == SCOPE_REF)
14853 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14854 complain, in_decl);
14855 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14856 = build_qualified_name (NULL_TREE, scope,
14857 TREE_OPERAND (placeholder, 1),
14858 false);
14860 else
14861 gcc_assert (identifier_p (placeholder));
14863 OMP_CLAUSE_DECL (nc)
14864 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14865 in_decl);
14866 break;
14867 case OMP_CLAUSE_GANG:
14868 case OMP_CLAUSE_ALIGNED:
14869 OMP_CLAUSE_DECL (nc)
14870 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14871 in_decl);
14872 OMP_CLAUSE_OPERAND (nc, 1)
14873 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14874 in_decl, /*integral_constant_expression_p=*/false);
14875 break;
14876 case OMP_CLAUSE_LINEAR:
14877 OMP_CLAUSE_DECL (nc)
14878 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14879 in_decl);
14880 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
14882 gcc_assert (!linear_no_step);
14883 linear_no_step = nc;
14885 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
14886 OMP_CLAUSE_LINEAR_STEP (nc)
14887 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
14888 complain, in_decl);
14889 else
14890 OMP_CLAUSE_LINEAR_STEP (nc)
14891 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
14892 in_decl,
14893 /*integral_constant_expression_p=*/false);
14894 break;
14895 case OMP_CLAUSE_NOWAIT:
14896 case OMP_CLAUSE_DEFAULT:
14897 case OMP_CLAUSE_UNTIED:
14898 case OMP_CLAUSE_MERGEABLE:
14899 case OMP_CLAUSE_INBRANCH:
14900 case OMP_CLAUSE_NOTINBRANCH:
14901 case OMP_CLAUSE_PROC_BIND:
14902 case OMP_CLAUSE_FOR:
14903 case OMP_CLAUSE_PARALLEL:
14904 case OMP_CLAUSE_SECTIONS:
14905 case OMP_CLAUSE_TASKGROUP:
14906 case OMP_CLAUSE_NOGROUP:
14907 case OMP_CLAUSE_THREADS:
14908 case OMP_CLAUSE_SIMD:
14909 case OMP_CLAUSE_DEFAULTMAP:
14910 case OMP_CLAUSE_INDEPENDENT:
14911 case OMP_CLAUSE_AUTO:
14912 case OMP_CLAUSE_SEQ:
14913 break;
14914 case OMP_CLAUSE_TILE:
14916 tree lnc, loc;
14917 for (lnc = OMP_CLAUSE_TILE_LIST (nc),
14918 loc = OMP_CLAUSE_TILE_LIST (oc);
14919 loc;
14920 loc = TREE_CHAIN (loc), lnc = TREE_CHAIN (lnc))
14922 TREE_VALUE (lnc) = tsubst_expr (TREE_VALUE (loc), args,
14923 complain, in_decl, false);
14926 break;
14927 default:
14928 gcc_unreachable ();
14930 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
14931 switch (OMP_CLAUSE_CODE (nc))
14933 case OMP_CLAUSE_SHARED:
14934 case OMP_CLAUSE_PRIVATE:
14935 case OMP_CLAUSE_FIRSTPRIVATE:
14936 case OMP_CLAUSE_LASTPRIVATE:
14937 case OMP_CLAUSE_COPYPRIVATE:
14938 case OMP_CLAUSE_LINEAR:
14939 case OMP_CLAUSE_REDUCTION:
14940 case OMP_CLAUSE_USE_DEVICE_PTR:
14941 case OMP_CLAUSE_IS_DEVICE_PTR:
14942 /* tsubst_expr on SCOPE_REF results in returning
14943 finish_non_static_data_member result. Undo that here. */
14944 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
14945 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
14946 == IDENTIFIER_NODE))
14948 tree t = OMP_CLAUSE_DECL (nc);
14949 tree v = t;
14950 while (v)
14951 switch (TREE_CODE (v))
14953 case COMPONENT_REF:
14954 case MEM_REF:
14955 case INDIRECT_REF:
14956 CASE_CONVERT:
14957 case POINTER_PLUS_EXPR:
14958 v = TREE_OPERAND (v, 0);
14959 continue;
14960 case PARM_DECL:
14961 if (DECL_CONTEXT (v) == current_function_decl
14962 && DECL_ARTIFICIAL (v)
14963 && DECL_NAME (v) == this_identifier)
14964 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
14965 /* FALLTHRU */
14966 default:
14967 v = NULL_TREE;
14968 break;
14971 else if (VAR_P (OMP_CLAUSE_DECL (oc))
14972 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
14973 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
14974 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
14975 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
14977 tree decl = OMP_CLAUSE_DECL (nc);
14978 if (VAR_P (decl))
14980 if (!DECL_LANG_SPECIFIC (decl))
14981 retrofit_lang_decl (decl);
14982 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
14985 break;
14986 default:
14987 break;
14991 new_clauses = nreverse (new_clauses);
14992 if (ort != C_ORT_OMP_DECLARE_SIMD)
14994 new_clauses = finish_omp_clauses (new_clauses, ort);
14995 if (linear_no_step)
14996 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
14997 if (nc == linear_no_step)
14999 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15000 break;
15003 return new_clauses;
15006 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15008 static tree
15009 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15010 tree in_decl)
15012 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15014 tree purpose, value, chain;
15016 if (t == NULL)
15017 return t;
15019 if (TREE_CODE (t) != TREE_LIST)
15020 return tsubst_copy_and_build (t, args, complain, in_decl,
15021 /*function_p=*/false,
15022 /*integral_constant_expression_p=*/false);
15024 if (t == void_list_node)
15025 return t;
15027 purpose = TREE_PURPOSE (t);
15028 if (purpose)
15029 purpose = RECUR (purpose);
15030 value = TREE_VALUE (t);
15031 if (value)
15033 if (TREE_CODE (value) != LABEL_DECL)
15034 value = RECUR (value);
15035 else
15037 value = lookup_label (DECL_NAME (value));
15038 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15039 TREE_USED (value) = 1;
15042 chain = TREE_CHAIN (t);
15043 if (chain && chain != void_type_node)
15044 chain = RECUR (chain);
15045 return tree_cons (purpose, value, chain);
15046 #undef RECUR
15049 /* Used to temporarily communicate the list of #pragma omp parallel
15050 clauses to #pragma omp for instantiation if they are combined
15051 together. */
15053 static tree *omp_parallel_combined_clauses;
15055 /* Substitute one OMP_FOR iterator. */
15057 static void
15058 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15059 tree initv, tree condv, tree incrv, tree *clauses,
15060 tree args, tsubst_flags_t complain, tree in_decl,
15061 bool integral_constant_expression_p)
15063 #define RECUR(NODE) \
15064 tsubst_expr ((NODE), args, complain, in_decl, \
15065 integral_constant_expression_p)
15066 tree decl, init, cond, incr;
15068 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15069 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15071 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15073 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15074 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15077 decl = TREE_OPERAND (init, 0);
15078 init = TREE_OPERAND (init, 1);
15079 tree decl_expr = NULL_TREE;
15080 if (init && TREE_CODE (init) == DECL_EXPR)
15082 /* We need to jump through some hoops to handle declarations in the
15083 init-statement, since we might need to handle auto deduction,
15084 but we need to keep control of initialization. */
15085 decl_expr = init;
15086 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15087 decl = tsubst_decl (decl, args, complain);
15089 else
15091 if (TREE_CODE (decl) == SCOPE_REF)
15093 decl = RECUR (decl);
15094 if (TREE_CODE (decl) == COMPONENT_REF)
15096 tree v = decl;
15097 while (v)
15098 switch (TREE_CODE (v))
15100 case COMPONENT_REF:
15101 case MEM_REF:
15102 case INDIRECT_REF:
15103 CASE_CONVERT:
15104 case POINTER_PLUS_EXPR:
15105 v = TREE_OPERAND (v, 0);
15106 continue;
15107 case PARM_DECL:
15108 if (DECL_CONTEXT (v) == current_function_decl
15109 && DECL_ARTIFICIAL (v)
15110 && DECL_NAME (v) == this_identifier)
15112 decl = TREE_OPERAND (decl, 1);
15113 decl = omp_privatize_field (decl, false);
15115 /* FALLTHRU */
15116 default:
15117 v = NULL_TREE;
15118 break;
15122 else
15123 decl = RECUR (decl);
15125 init = RECUR (init);
15127 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15128 if (auto_node && init)
15129 TREE_TYPE (decl)
15130 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15132 gcc_assert (!type_dependent_expression_p (decl));
15134 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15136 if (decl_expr)
15138 /* Declare the variable, but don't let that initialize it. */
15139 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15140 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15141 RECUR (decl_expr);
15142 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15145 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15146 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15147 if (TREE_CODE (incr) == MODIFY_EXPR)
15149 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15150 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15151 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15152 NOP_EXPR, rhs, complain);
15154 else
15155 incr = RECUR (incr);
15156 TREE_VEC_ELT (declv, i) = decl;
15157 TREE_VEC_ELT (initv, i) = init;
15158 TREE_VEC_ELT (condv, i) = cond;
15159 TREE_VEC_ELT (incrv, i) = incr;
15160 return;
15163 if (decl_expr)
15165 /* Declare and initialize the variable. */
15166 RECUR (decl_expr);
15167 init = NULL_TREE;
15169 else if (init)
15171 tree *pc;
15172 int j;
15173 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15175 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15177 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15178 && OMP_CLAUSE_DECL (*pc) == decl)
15179 break;
15180 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15181 && OMP_CLAUSE_DECL (*pc) == decl)
15183 if (j)
15184 break;
15185 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15186 tree c = *pc;
15187 *pc = OMP_CLAUSE_CHAIN (c);
15188 OMP_CLAUSE_CHAIN (c) = *clauses;
15189 *clauses = c;
15191 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15192 && OMP_CLAUSE_DECL (*pc) == decl)
15194 error ("iteration variable %qD should not be firstprivate",
15195 decl);
15196 *pc = OMP_CLAUSE_CHAIN (*pc);
15198 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15199 && OMP_CLAUSE_DECL (*pc) == decl)
15201 error ("iteration variable %qD should not be reduction",
15202 decl);
15203 *pc = OMP_CLAUSE_CHAIN (*pc);
15205 else
15206 pc = &OMP_CLAUSE_CHAIN (*pc);
15208 if (*pc)
15209 break;
15211 if (*pc == NULL_TREE)
15213 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15214 OMP_CLAUSE_DECL (c) = decl;
15215 c = finish_omp_clauses (c, C_ORT_OMP);
15216 if (c)
15218 OMP_CLAUSE_CHAIN (c) = *clauses;
15219 *clauses = c;
15223 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15224 if (COMPARISON_CLASS_P (cond))
15226 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15227 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15228 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15230 else
15231 cond = RECUR (cond);
15232 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15233 switch (TREE_CODE (incr))
15235 case PREINCREMENT_EXPR:
15236 case PREDECREMENT_EXPR:
15237 case POSTINCREMENT_EXPR:
15238 case POSTDECREMENT_EXPR:
15239 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15240 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15241 break;
15242 case MODIFY_EXPR:
15243 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15244 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15246 tree rhs = TREE_OPERAND (incr, 1);
15247 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15248 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15249 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15250 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15251 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15252 rhs0, rhs1));
15254 else
15255 incr = RECUR (incr);
15256 break;
15257 case MODOP_EXPR:
15258 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15259 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15261 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15262 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15263 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15264 TREE_TYPE (decl), lhs,
15265 RECUR (TREE_OPERAND (incr, 2))));
15267 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15268 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15269 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15271 tree rhs = TREE_OPERAND (incr, 2);
15272 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15273 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15274 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15275 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15276 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15277 rhs0, rhs1));
15279 else
15280 incr = RECUR (incr);
15281 break;
15282 default:
15283 incr = RECUR (incr);
15284 break;
15287 TREE_VEC_ELT (declv, i) = decl;
15288 TREE_VEC_ELT (initv, i) = init;
15289 TREE_VEC_ELT (condv, i) = cond;
15290 TREE_VEC_ELT (incrv, i) = incr;
15291 #undef RECUR
15294 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15295 of OMP_TARGET's body. */
15297 static tree
15298 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15300 *walk_subtrees = 0;
15301 switch (TREE_CODE (*tp))
15303 case OMP_TEAMS:
15304 return *tp;
15305 case BIND_EXPR:
15306 case STATEMENT_LIST:
15307 *walk_subtrees = 1;
15308 break;
15309 default:
15310 break;
15312 return NULL_TREE;
15315 /* Helper function for tsubst_expr. For decomposition declaration
15316 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15317 also the corresponding decls representing the identifiers
15318 of the decomposition declaration. Return DECL if successful
15319 or error_mark_node otherwise, set *FIRST to the first decl
15320 in the list chained through DECL_CHAIN and *CNT to the number
15321 of such decls. */
15323 static tree
15324 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15325 tsubst_flags_t complain, tree in_decl, tree *first,
15326 unsigned int *cnt)
15328 tree decl2, decl3, prev = decl;
15329 *cnt = 0;
15330 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15331 for (decl2 = DECL_CHAIN (pattern_decl);
15332 decl2
15333 && VAR_P (decl2)
15334 && DECL_DECOMPOSITION_P (decl2)
15335 && DECL_NAME (decl2);
15336 decl2 = DECL_CHAIN (decl2))
15338 (*cnt)++;
15339 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15340 tree v = DECL_VALUE_EXPR (decl2);
15341 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15342 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15343 decl3 = tsubst (decl2, args, complain, in_decl);
15344 SET_DECL_VALUE_EXPR (decl2, v);
15345 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15346 if (VAR_P (decl3))
15347 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15348 maybe_push_decl (decl3);
15349 if (error_operand_p (decl3))
15350 decl = error_mark_node;
15351 else if (decl != error_mark_node
15352 && DECL_CHAIN (decl3) != prev)
15354 gcc_assert (errorcount);
15355 decl = error_mark_node;
15357 else
15358 prev = decl3;
15360 *first = prev;
15361 return decl;
15364 /* Like tsubst_copy for expressions, etc. but also does semantic
15365 processing. */
15367 tree
15368 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15369 bool integral_constant_expression_p)
15371 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15372 #define RECUR(NODE) \
15373 tsubst_expr ((NODE), args, complain, in_decl, \
15374 integral_constant_expression_p)
15376 tree stmt, tmp;
15377 tree r;
15378 location_t loc;
15380 if (t == NULL_TREE || t == error_mark_node)
15381 return t;
15383 loc = input_location;
15384 if (EXPR_HAS_LOCATION (t))
15385 input_location = EXPR_LOCATION (t);
15386 if (STATEMENT_CODE_P (TREE_CODE (t)))
15387 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15389 switch (TREE_CODE (t))
15391 case STATEMENT_LIST:
15393 tree_stmt_iterator i;
15394 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15395 RECUR (tsi_stmt (i));
15396 break;
15399 case CTOR_INITIALIZER:
15400 finish_mem_initializers (tsubst_initializer_list
15401 (TREE_OPERAND (t, 0), args));
15402 break;
15404 case RETURN_EXPR:
15405 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15406 break;
15408 case EXPR_STMT:
15409 tmp = RECUR (EXPR_STMT_EXPR (t));
15410 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15411 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15412 else
15413 finish_expr_stmt (tmp);
15414 break;
15416 case USING_STMT:
15417 do_using_directive (USING_STMT_NAMESPACE (t));
15418 break;
15420 case DECL_EXPR:
15422 tree decl, pattern_decl;
15423 tree init;
15425 pattern_decl = decl = DECL_EXPR_DECL (t);
15426 if (TREE_CODE (decl) == LABEL_DECL)
15427 finish_label_decl (DECL_NAME (decl));
15428 else if (TREE_CODE (decl) == USING_DECL)
15430 tree scope = USING_DECL_SCOPE (decl);
15431 tree name = DECL_NAME (decl);
15433 scope = tsubst (scope, args, complain, in_decl);
15434 decl = lookup_qualified_name (scope, name,
15435 /*is_type_p=*/false,
15436 /*complain=*/false);
15437 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15438 qualified_name_lookup_error (scope, name, decl, input_location);
15439 else
15440 do_local_using_decl (decl, scope, name);
15442 else if (DECL_PACK_P (decl))
15444 /* Don't build up decls for a variadic capture proxy, we'll
15445 instantiate the elements directly as needed. */
15446 break;
15448 else
15450 init = DECL_INITIAL (decl);
15451 decl = tsubst (decl, args, complain, in_decl);
15452 if (decl != error_mark_node)
15454 /* By marking the declaration as instantiated, we avoid
15455 trying to instantiate it. Since instantiate_decl can't
15456 handle local variables, and since we've already done
15457 all that needs to be done, that's the right thing to
15458 do. */
15459 if (VAR_P (decl))
15460 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15461 if (VAR_P (decl)
15462 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15463 /* Anonymous aggregates are a special case. */
15464 finish_anon_union (decl);
15465 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15467 DECL_CONTEXT (decl) = current_function_decl;
15468 if (DECL_NAME (decl) == this_identifier)
15470 tree lam = DECL_CONTEXT (current_function_decl);
15471 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15472 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15474 insert_capture_proxy (decl);
15476 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15477 /* We already did a pushtag. */;
15478 else if (TREE_CODE (decl) == FUNCTION_DECL
15479 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15480 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15482 DECL_CONTEXT (decl) = NULL_TREE;
15483 pushdecl (decl);
15484 DECL_CONTEXT (decl) = current_function_decl;
15485 cp_check_omp_declare_reduction (decl);
15487 else
15489 int const_init = false;
15490 maybe_push_decl (decl);
15491 if (VAR_P (decl)
15492 && DECL_PRETTY_FUNCTION_P (decl))
15494 /* For __PRETTY_FUNCTION__ we have to adjust the
15495 initializer. */
15496 const char *const name
15497 = cxx_printable_name (current_function_decl, 2);
15498 init = cp_fname_init (name, &TREE_TYPE (decl));
15500 else
15501 init = tsubst_init (init, decl, args, complain, in_decl);
15503 if (VAR_P (decl))
15504 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15505 (pattern_decl));
15506 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15507 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15509 unsigned int cnt;
15510 tree first;
15511 decl = tsubst_decomp_names (decl, pattern_decl, args,
15512 complain, in_decl, &first,
15513 &cnt);
15514 if (decl != error_mark_node)
15515 cp_finish_decomp (decl, first, cnt);
15521 break;
15524 case FOR_STMT:
15525 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15526 RECUR (FOR_INIT_STMT (t));
15527 finish_init_stmt (stmt);
15528 tmp = RECUR (FOR_COND (t));
15529 finish_for_cond (tmp, stmt, false);
15530 tmp = RECUR (FOR_EXPR (t));
15531 finish_for_expr (tmp, stmt);
15532 RECUR (FOR_BODY (t));
15533 finish_for_stmt (stmt);
15534 break;
15536 case RANGE_FOR_STMT:
15538 tree decl, expr;
15539 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15540 decl = RANGE_FOR_DECL (t);
15541 decl = tsubst (decl, args, complain, in_decl);
15542 maybe_push_decl (decl);
15543 expr = RECUR (RANGE_FOR_EXPR (t));
15544 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15546 unsigned int cnt;
15547 tree first;
15548 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15549 complain, in_decl, &first, &cnt);
15550 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15551 RANGE_FOR_IVDEP (t));
15553 else
15554 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15555 RANGE_FOR_IVDEP (t));
15556 RECUR (RANGE_FOR_BODY (t));
15557 finish_for_stmt (stmt);
15559 break;
15561 case WHILE_STMT:
15562 stmt = begin_while_stmt ();
15563 tmp = RECUR (WHILE_COND (t));
15564 finish_while_stmt_cond (tmp, stmt, false);
15565 RECUR (WHILE_BODY (t));
15566 finish_while_stmt (stmt);
15567 break;
15569 case DO_STMT:
15570 stmt = begin_do_stmt ();
15571 RECUR (DO_BODY (t));
15572 finish_do_body (stmt);
15573 tmp = RECUR (DO_COND (t));
15574 finish_do_stmt (tmp, stmt, false);
15575 break;
15577 case IF_STMT:
15578 stmt = begin_if_stmt ();
15579 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15580 tmp = RECUR (IF_COND (t));
15581 tmp = finish_if_stmt_cond (tmp, stmt);
15582 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15583 /* Don't instantiate the THEN_CLAUSE. */;
15584 else
15586 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15587 if (inhibit)
15588 ++c_inhibit_evaluation_warnings;
15589 RECUR (THEN_CLAUSE (t));
15590 if (inhibit)
15591 --c_inhibit_evaluation_warnings;
15593 finish_then_clause (stmt);
15595 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15596 /* Don't instantiate the ELSE_CLAUSE. */;
15597 else if (ELSE_CLAUSE (t))
15599 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15600 begin_else_clause (stmt);
15601 if (inhibit)
15602 ++c_inhibit_evaluation_warnings;
15603 RECUR (ELSE_CLAUSE (t));
15604 if (inhibit)
15605 --c_inhibit_evaluation_warnings;
15606 finish_else_clause (stmt);
15609 finish_if_stmt (stmt);
15610 break;
15612 case BIND_EXPR:
15613 if (BIND_EXPR_BODY_BLOCK (t))
15614 stmt = begin_function_body ();
15615 else
15616 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15617 ? BCS_TRY_BLOCK : 0);
15619 RECUR (BIND_EXPR_BODY (t));
15621 if (BIND_EXPR_BODY_BLOCK (t))
15622 finish_function_body (stmt);
15623 else
15624 finish_compound_stmt (stmt);
15625 break;
15627 case BREAK_STMT:
15628 finish_break_stmt ();
15629 break;
15631 case CONTINUE_STMT:
15632 finish_continue_stmt ();
15633 break;
15635 case SWITCH_STMT:
15636 stmt = begin_switch_stmt ();
15637 tmp = RECUR (SWITCH_STMT_COND (t));
15638 finish_switch_cond (tmp, stmt);
15639 RECUR (SWITCH_STMT_BODY (t));
15640 finish_switch_stmt (stmt);
15641 break;
15643 case CASE_LABEL_EXPR:
15645 tree low = RECUR (CASE_LOW (t));
15646 tree high = RECUR (CASE_HIGH (t));
15647 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15648 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15649 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15650 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15652 break;
15654 case LABEL_EXPR:
15656 tree decl = LABEL_EXPR_LABEL (t);
15657 tree label;
15659 label = finish_label_stmt (DECL_NAME (decl));
15660 if (TREE_CODE (label) == LABEL_DECL)
15661 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15662 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15663 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15665 break;
15667 case GOTO_EXPR:
15668 tmp = GOTO_DESTINATION (t);
15669 if (TREE_CODE (tmp) != LABEL_DECL)
15670 /* Computed goto's must be tsubst'd into. On the other hand,
15671 non-computed gotos must not be; the identifier in question
15672 will have no binding. */
15673 tmp = RECUR (tmp);
15674 else
15675 tmp = DECL_NAME (tmp);
15676 finish_goto_stmt (tmp);
15677 break;
15679 case ASM_EXPR:
15681 tree string = RECUR (ASM_STRING (t));
15682 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15683 complain, in_decl);
15684 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15685 complain, in_decl);
15686 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15687 complain, in_decl);
15688 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15689 complain, in_decl);
15690 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15691 clobbers, labels);
15692 tree asm_expr = tmp;
15693 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15694 asm_expr = TREE_OPERAND (asm_expr, 0);
15695 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15697 break;
15699 case TRY_BLOCK:
15700 if (CLEANUP_P (t))
15702 stmt = begin_try_block ();
15703 RECUR (TRY_STMTS (t));
15704 finish_cleanup_try_block (stmt);
15705 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15707 else
15709 tree compound_stmt = NULL_TREE;
15711 if (FN_TRY_BLOCK_P (t))
15712 stmt = begin_function_try_block (&compound_stmt);
15713 else
15714 stmt = begin_try_block ();
15716 RECUR (TRY_STMTS (t));
15718 if (FN_TRY_BLOCK_P (t))
15719 finish_function_try_block (stmt);
15720 else
15721 finish_try_block (stmt);
15723 RECUR (TRY_HANDLERS (t));
15724 if (FN_TRY_BLOCK_P (t))
15725 finish_function_handler_sequence (stmt, compound_stmt);
15726 else
15727 finish_handler_sequence (stmt);
15729 break;
15731 case HANDLER:
15733 tree decl = HANDLER_PARMS (t);
15735 if (decl)
15737 decl = tsubst (decl, args, complain, in_decl);
15738 /* Prevent instantiate_decl from trying to instantiate
15739 this variable. We've already done all that needs to be
15740 done. */
15741 if (decl != error_mark_node)
15742 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15744 stmt = begin_handler ();
15745 finish_handler_parms (decl, stmt);
15746 RECUR (HANDLER_BODY (t));
15747 finish_handler (stmt);
15749 break;
15751 case TAG_DEFN:
15752 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
15753 if (CLASS_TYPE_P (tmp))
15755 /* Local classes are not independent templates; they are
15756 instantiated along with their containing function. And this
15757 way we don't have to deal with pushing out of one local class
15758 to instantiate a member of another local class. */
15759 tree fn;
15760 /* Closures are handled by the LAMBDA_EXPR. */
15761 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
15762 complete_type (tmp);
15763 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
15764 if (!DECL_ARTIFICIAL (fn))
15765 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
15767 break;
15769 case STATIC_ASSERT:
15771 tree condition;
15773 ++c_inhibit_evaluation_warnings;
15774 condition =
15775 tsubst_expr (STATIC_ASSERT_CONDITION (t),
15776 args,
15777 complain, in_decl,
15778 /*integral_constant_expression_p=*/true);
15779 --c_inhibit_evaluation_warnings;
15781 finish_static_assert (condition,
15782 STATIC_ASSERT_MESSAGE (t),
15783 STATIC_ASSERT_SOURCE_LOCATION (t),
15784 /*member_p=*/false);
15786 break;
15788 case OACC_KERNELS:
15789 case OACC_PARALLEL:
15790 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
15791 in_decl);
15792 stmt = begin_omp_parallel ();
15793 RECUR (OMP_BODY (t));
15794 finish_omp_construct (TREE_CODE (t), stmt, tmp);
15795 break;
15797 case OMP_PARALLEL:
15798 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
15799 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
15800 complain, in_decl);
15801 if (OMP_PARALLEL_COMBINED (t))
15802 omp_parallel_combined_clauses = &tmp;
15803 stmt = begin_omp_parallel ();
15804 RECUR (OMP_PARALLEL_BODY (t));
15805 gcc_assert (omp_parallel_combined_clauses == NULL);
15806 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15807 = OMP_PARALLEL_COMBINED (t);
15808 pop_omp_privatization_clauses (r);
15809 break;
15811 case OMP_TASK:
15812 r = push_omp_privatization_clauses (false);
15813 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
15814 complain, in_decl);
15815 stmt = begin_omp_task ();
15816 RECUR (OMP_TASK_BODY (t));
15817 finish_omp_task (tmp, stmt);
15818 pop_omp_privatization_clauses (r);
15819 break;
15821 case OMP_FOR:
15822 case OMP_SIMD:
15823 case CILK_SIMD:
15824 case CILK_FOR:
15825 case OMP_DISTRIBUTE:
15826 case OMP_TASKLOOP:
15827 case OACC_LOOP:
15829 tree clauses, body, pre_body;
15830 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15831 tree orig_declv = NULL_TREE;
15832 tree incrv = NULL_TREE;
15833 enum c_omp_region_type ort = C_ORT_OMP;
15834 int i;
15836 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
15837 ort = C_ORT_CILK;
15838 else if (TREE_CODE (t) == OACC_LOOP)
15839 ort = C_ORT_ACC;
15841 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
15842 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
15843 in_decl);
15844 if (OMP_FOR_INIT (t) != NULL_TREE)
15846 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15847 if (OMP_FOR_ORIG_DECLS (t))
15848 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15849 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15850 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15851 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15854 stmt = begin_omp_structured_block ();
15856 pre_body = push_stmt_list ();
15857 RECUR (OMP_FOR_PRE_BODY (t));
15858 pre_body = pop_stmt_list (pre_body);
15860 if (OMP_FOR_INIT (t) != NULL_TREE)
15861 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15862 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
15863 incrv, &clauses, args, complain, in_decl,
15864 integral_constant_expression_p);
15865 omp_parallel_combined_clauses = NULL;
15867 body = push_stmt_list ();
15868 RECUR (OMP_FOR_BODY (t));
15869 body = pop_stmt_list (body);
15871 if (OMP_FOR_INIT (t) != NULL_TREE)
15872 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
15873 orig_declv, initv, condv, incrv, body, pre_body,
15874 NULL, clauses);
15875 else
15877 t = make_node (TREE_CODE (t));
15878 TREE_TYPE (t) = void_type_node;
15879 OMP_FOR_BODY (t) = body;
15880 OMP_FOR_PRE_BODY (t) = pre_body;
15881 OMP_FOR_CLAUSES (t) = clauses;
15882 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15883 add_stmt (t);
15886 add_stmt (finish_omp_structured_block (stmt));
15887 pop_omp_privatization_clauses (r);
15889 break;
15891 case OMP_SECTIONS:
15892 omp_parallel_combined_clauses = NULL;
15893 /* FALLTHRU */
15894 case OMP_SINGLE:
15895 case OMP_TEAMS:
15896 case OMP_CRITICAL:
15897 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
15898 && OMP_TEAMS_COMBINED (t));
15899 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
15900 in_decl);
15901 stmt = push_stmt_list ();
15902 RECUR (OMP_BODY (t));
15903 stmt = pop_stmt_list (stmt);
15905 t = copy_node (t);
15906 OMP_BODY (t) = stmt;
15907 OMP_CLAUSES (t) = tmp;
15908 add_stmt (t);
15909 pop_omp_privatization_clauses (r);
15910 break;
15912 case OACC_DATA:
15913 case OMP_TARGET_DATA:
15914 case OMP_TARGET:
15915 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
15916 ? C_ORT_ACC : C_ORT_OMP, args, complain,
15917 in_decl);
15918 keep_next_level (true);
15919 stmt = begin_omp_structured_block ();
15921 RECUR (OMP_BODY (t));
15922 stmt = finish_omp_structured_block (stmt);
15924 t = copy_node (t);
15925 OMP_BODY (t) = stmt;
15926 OMP_CLAUSES (t) = tmp;
15927 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
15929 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
15930 if (teams)
15932 /* For combined target teams, ensure the num_teams and
15933 thread_limit clause expressions are evaluated on the host,
15934 before entering the target construct. */
15935 tree c;
15936 for (c = OMP_TEAMS_CLAUSES (teams);
15937 c; c = OMP_CLAUSE_CHAIN (c))
15938 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15939 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15940 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15942 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15943 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
15944 if (expr == error_mark_node)
15945 continue;
15946 tmp = TARGET_EXPR_SLOT (expr);
15947 add_stmt (expr);
15948 OMP_CLAUSE_OPERAND (c, 0) = expr;
15949 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15950 OMP_CLAUSE_FIRSTPRIVATE);
15951 OMP_CLAUSE_DECL (tc) = tmp;
15952 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
15953 OMP_TARGET_CLAUSES (t) = tc;
15957 add_stmt (t);
15958 break;
15960 case OACC_DECLARE:
15961 t = copy_node (t);
15962 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
15963 complain, in_decl);
15964 OACC_DECLARE_CLAUSES (t) = tmp;
15965 add_stmt (t);
15966 break;
15968 case OMP_TARGET_UPDATE:
15969 case OMP_TARGET_ENTER_DATA:
15970 case OMP_TARGET_EXIT_DATA:
15971 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
15972 complain, in_decl);
15973 t = copy_node (t);
15974 OMP_STANDALONE_CLAUSES (t) = tmp;
15975 add_stmt (t);
15976 break;
15978 case OACC_ENTER_DATA:
15979 case OACC_EXIT_DATA:
15980 case OACC_UPDATE:
15981 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
15982 complain, in_decl);
15983 t = copy_node (t);
15984 OMP_STANDALONE_CLAUSES (t) = tmp;
15985 add_stmt (t);
15986 break;
15988 case OMP_ORDERED:
15989 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
15990 complain, in_decl);
15991 stmt = push_stmt_list ();
15992 RECUR (OMP_BODY (t));
15993 stmt = pop_stmt_list (stmt);
15995 t = copy_node (t);
15996 OMP_BODY (t) = stmt;
15997 OMP_ORDERED_CLAUSES (t) = tmp;
15998 add_stmt (t);
15999 break;
16001 case OMP_SECTION:
16002 case OMP_MASTER:
16003 case OMP_TASKGROUP:
16004 stmt = push_stmt_list ();
16005 RECUR (OMP_BODY (t));
16006 stmt = pop_stmt_list (stmt);
16008 t = copy_node (t);
16009 OMP_BODY (t) = stmt;
16010 add_stmt (t);
16011 break;
16013 case OMP_ATOMIC:
16014 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16015 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16017 tree op1 = TREE_OPERAND (t, 1);
16018 tree rhs1 = NULL_TREE;
16019 tree lhs, rhs;
16020 if (TREE_CODE (op1) == COMPOUND_EXPR)
16022 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16023 op1 = TREE_OPERAND (op1, 1);
16025 lhs = RECUR (TREE_OPERAND (op1, 0));
16026 rhs = RECUR (TREE_OPERAND (op1, 1));
16027 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16028 NULL_TREE, NULL_TREE, rhs1,
16029 OMP_ATOMIC_SEQ_CST (t));
16031 else
16033 tree op1 = TREE_OPERAND (t, 1);
16034 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16035 tree rhs1 = NULL_TREE;
16036 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16037 enum tree_code opcode = NOP_EXPR;
16038 if (code == OMP_ATOMIC_READ)
16040 v = RECUR (TREE_OPERAND (op1, 0));
16041 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16043 else if (code == OMP_ATOMIC_CAPTURE_OLD
16044 || code == OMP_ATOMIC_CAPTURE_NEW)
16046 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16047 v = RECUR (TREE_OPERAND (op1, 0));
16048 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16049 if (TREE_CODE (op11) == COMPOUND_EXPR)
16051 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16052 op11 = TREE_OPERAND (op11, 1);
16054 lhs = RECUR (TREE_OPERAND (op11, 0));
16055 rhs = RECUR (TREE_OPERAND (op11, 1));
16056 opcode = TREE_CODE (op11);
16057 if (opcode == MODIFY_EXPR)
16058 opcode = NOP_EXPR;
16060 else
16062 code = OMP_ATOMIC;
16063 lhs = RECUR (TREE_OPERAND (op1, 0));
16064 rhs = RECUR (TREE_OPERAND (op1, 1));
16066 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16067 OMP_ATOMIC_SEQ_CST (t));
16069 break;
16071 case TRANSACTION_EXPR:
16073 int flags = 0;
16074 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16075 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16077 if (TRANSACTION_EXPR_IS_STMT (t))
16079 tree body = TRANSACTION_EXPR_BODY (t);
16080 tree noex = NULL_TREE;
16081 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16083 noex = MUST_NOT_THROW_COND (body);
16084 if (noex == NULL_TREE)
16085 noex = boolean_true_node;
16086 body = TREE_OPERAND (body, 0);
16088 stmt = begin_transaction_stmt (input_location, NULL, flags);
16089 RECUR (body);
16090 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16092 else
16094 stmt = build_transaction_expr (EXPR_LOCATION (t),
16095 RECUR (TRANSACTION_EXPR_BODY (t)),
16096 flags, NULL_TREE);
16097 RETURN (stmt);
16100 break;
16102 case MUST_NOT_THROW_EXPR:
16104 tree op0 = RECUR (TREE_OPERAND (t, 0));
16105 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16106 RETURN (build_must_not_throw_expr (op0, cond));
16109 case EXPR_PACK_EXPANSION:
16110 error ("invalid use of pack expansion expression");
16111 RETURN (error_mark_node);
16113 case NONTYPE_ARGUMENT_PACK:
16114 error ("use %<...%> to expand argument pack");
16115 RETURN (error_mark_node);
16117 case CILK_SPAWN_STMT:
16118 cfun->calls_cilk_spawn = 1;
16119 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16121 case CILK_SYNC_STMT:
16122 RETURN (build_cilk_sync ());
16124 case COMPOUND_EXPR:
16125 tmp = RECUR (TREE_OPERAND (t, 0));
16126 if (tmp == NULL_TREE)
16127 /* If the first operand was a statement, we're done with it. */
16128 RETURN (RECUR (TREE_OPERAND (t, 1)));
16129 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16130 RECUR (TREE_OPERAND (t, 1)),
16131 complain));
16133 case ANNOTATE_EXPR:
16134 tmp = RECUR (TREE_OPERAND (t, 0));
16135 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16136 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16138 default:
16139 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16141 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16142 /*function_p=*/false,
16143 integral_constant_expression_p));
16146 RETURN (NULL_TREE);
16147 out:
16148 input_location = loc;
16149 return r;
16150 #undef RECUR
16151 #undef RETURN
16154 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16155 function. For description of the body see comment above
16156 cp_parser_omp_declare_reduction_exprs. */
16158 static void
16159 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16161 if (t == NULL_TREE || t == error_mark_node)
16162 return;
16164 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16166 tree_stmt_iterator tsi;
16167 int i;
16168 tree stmts[7];
16169 memset (stmts, 0, sizeof stmts);
16170 for (i = 0, tsi = tsi_start (t);
16171 i < 7 && !tsi_end_p (tsi);
16172 i++, tsi_next (&tsi))
16173 stmts[i] = tsi_stmt (tsi);
16174 gcc_assert (tsi_end_p (tsi));
16176 if (i >= 3)
16178 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16179 && TREE_CODE (stmts[1]) == DECL_EXPR);
16180 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16181 args, complain, in_decl);
16182 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16183 args, complain, in_decl);
16184 DECL_CONTEXT (omp_out) = current_function_decl;
16185 DECL_CONTEXT (omp_in) = current_function_decl;
16186 keep_next_level (true);
16187 tree block = begin_omp_structured_block ();
16188 tsubst_expr (stmts[2], args, complain, in_decl, false);
16189 block = finish_omp_structured_block (block);
16190 block = maybe_cleanup_point_expr_void (block);
16191 add_decl_expr (omp_out);
16192 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16193 TREE_NO_WARNING (omp_out) = 1;
16194 add_decl_expr (omp_in);
16195 finish_expr_stmt (block);
16197 if (i >= 6)
16199 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16200 && TREE_CODE (stmts[4]) == DECL_EXPR);
16201 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16202 args, complain, in_decl);
16203 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16204 args, complain, in_decl);
16205 DECL_CONTEXT (omp_priv) = current_function_decl;
16206 DECL_CONTEXT (omp_orig) = current_function_decl;
16207 keep_next_level (true);
16208 tree block = begin_omp_structured_block ();
16209 tsubst_expr (stmts[5], args, complain, in_decl, false);
16210 block = finish_omp_structured_block (block);
16211 block = maybe_cleanup_point_expr_void (block);
16212 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16213 add_decl_expr (omp_priv);
16214 add_decl_expr (omp_orig);
16215 finish_expr_stmt (block);
16216 if (i == 7)
16217 add_decl_expr (omp_orig);
16221 /* T is a postfix-expression that is not being used in a function
16222 call. Return the substituted version of T. */
16224 static tree
16225 tsubst_non_call_postfix_expression (tree t, tree args,
16226 tsubst_flags_t complain,
16227 tree in_decl)
16229 if (TREE_CODE (t) == SCOPE_REF)
16230 t = tsubst_qualified_id (t, args, complain, in_decl,
16231 /*done=*/false, /*address_p=*/false);
16232 else
16233 t = tsubst_copy_and_build (t, args, complain, in_decl,
16234 /*function_p=*/false,
16235 /*integral_constant_expression_p=*/false);
16237 return t;
16240 /* Like tsubst but deals with expressions and performs semantic
16241 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16243 tree
16244 tsubst_copy_and_build (tree t,
16245 tree args,
16246 tsubst_flags_t complain,
16247 tree in_decl,
16248 bool function_p,
16249 bool integral_constant_expression_p)
16251 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16252 #define RECUR(NODE) \
16253 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16254 /*function_p=*/false, \
16255 integral_constant_expression_p)
16257 tree retval, op1;
16258 location_t loc;
16260 if (t == NULL_TREE || t == error_mark_node)
16261 return t;
16263 loc = input_location;
16264 if (EXPR_HAS_LOCATION (t))
16265 input_location = EXPR_LOCATION (t);
16267 /* N3276 decltype magic only applies to calls at the top level or on the
16268 right side of a comma. */
16269 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16270 complain &= ~tf_decltype;
16272 switch (TREE_CODE (t))
16274 case USING_DECL:
16275 t = DECL_NAME (t);
16276 /* Fall through. */
16277 case IDENTIFIER_NODE:
16279 tree decl;
16280 cp_id_kind idk;
16281 bool non_integral_constant_expression_p;
16282 const char *error_msg;
16284 if (IDENTIFIER_TYPENAME_P (t))
16286 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16287 t = mangle_conv_op_name_for_type (new_type);
16290 /* Look up the name. */
16291 decl = lookup_name (t);
16293 /* By convention, expressions use ERROR_MARK_NODE to indicate
16294 failure, not NULL_TREE. */
16295 if (decl == NULL_TREE)
16296 decl = error_mark_node;
16298 decl = finish_id_expression (t, decl, NULL_TREE,
16299 &idk,
16300 integral_constant_expression_p,
16301 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16302 &non_integral_constant_expression_p,
16303 /*template_p=*/false,
16304 /*done=*/true,
16305 /*address_p=*/false,
16306 /*template_arg_p=*/false,
16307 &error_msg,
16308 input_location);
16309 if (error_msg)
16310 error (error_msg);
16311 if (!function_p && identifier_p (decl))
16313 if (complain & tf_error)
16314 unqualified_name_lookup_error (decl);
16315 decl = error_mark_node;
16317 RETURN (decl);
16320 case TEMPLATE_ID_EXPR:
16322 tree object;
16323 tree templ = RECUR (TREE_OPERAND (t, 0));
16324 tree targs = TREE_OPERAND (t, 1);
16326 if (targs)
16327 targs = tsubst_template_args (targs, args, complain, in_decl);
16328 if (targs == error_mark_node)
16329 return error_mark_node;
16331 if (variable_template_p (templ))
16332 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16334 if (TREE_CODE (templ) == COMPONENT_REF)
16336 object = TREE_OPERAND (templ, 0);
16337 templ = TREE_OPERAND (templ, 1);
16339 else
16340 object = NULL_TREE;
16341 templ = lookup_template_function (templ, targs);
16343 if (object)
16344 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16345 object, templ, NULL_TREE));
16346 else
16347 RETURN (baselink_for_fns (templ));
16350 case INDIRECT_REF:
16352 tree r = RECUR (TREE_OPERAND (t, 0));
16354 if (REFERENCE_REF_P (t))
16356 /* A type conversion to reference type will be enclosed in
16357 such an indirect ref, but the substitution of the cast
16358 will have also added such an indirect ref. */
16359 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16360 r = convert_from_reference (r);
16362 else
16363 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16364 complain|decltype_flag);
16366 if (TREE_CODE (r) == INDIRECT_REF)
16367 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16369 RETURN (r);
16372 case NOP_EXPR:
16374 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16375 tree op0 = RECUR (TREE_OPERAND (t, 0));
16376 RETURN (build_nop (type, op0));
16379 case IMPLICIT_CONV_EXPR:
16381 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16382 tree expr = RECUR (TREE_OPERAND (t, 0));
16383 int flags = LOOKUP_IMPLICIT;
16384 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16385 flags = LOOKUP_NORMAL;
16386 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16387 flags));
16390 case CONVERT_EXPR:
16392 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16393 tree op0 = RECUR (TREE_OPERAND (t, 0));
16394 RETURN (build1 (CONVERT_EXPR, type, op0));
16397 case CAST_EXPR:
16398 case REINTERPRET_CAST_EXPR:
16399 case CONST_CAST_EXPR:
16400 case DYNAMIC_CAST_EXPR:
16401 case STATIC_CAST_EXPR:
16403 tree type;
16404 tree op, r = NULL_TREE;
16406 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16407 if (integral_constant_expression_p
16408 && !cast_valid_in_integral_constant_expression_p (type))
16410 if (complain & tf_error)
16411 error ("a cast to a type other than an integral or "
16412 "enumeration type cannot appear in a constant-expression");
16413 RETURN (error_mark_node);
16416 op = RECUR (TREE_OPERAND (t, 0));
16418 warning_sentinel s(warn_useless_cast);
16419 switch (TREE_CODE (t))
16421 case CAST_EXPR:
16422 r = build_functional_cast (type, op, complain);
16423 break;
16424 case REINTERPRET_CAST_EXPR:
16425 r = build_reinterpret_cast (type, op, complain);
16426 break;
16427 case CONST_CAST_EXPR:
16428 r = build_const_cast (type, op, complain);
16429 break;
16430 case DYNAMIC_CAST_EXPR:
16431 r = build_dynamic_cast (type, op, complain);
16432 break;
16433 case STATIC_CAST_EXPR:
16434 r = build_static_cast (type, op, complain);
16435 break;
16436 default:
16437 gcc_unreachable ();
16440 RETURN (r);
16443 case POSTDECREMENT_EXPR:
16444 case POSTINCREMENT_EXPR:
16445 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16446 args, complain, in_decl);
16447 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16448 complain|decltype_flag));
16450 case PREDECREMENT_EXPR:
16451 case PREINCREMENT_EXPR:
16452 case NEGATE_EXPR:
16453 case BIT_NOT_EXPR:
16454 case ABS_EXPR:
16455 case TRUTH_NOT_EXPR:
16456 case UNARY_PLUS_EXPR: /* Unary + */
16457 case REALPART_EXPR:
16458 case IMAGPART_EXPR:
16459 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16460 RECUR (TREE_OPERAND (t, 0)),
16461 complain|decltype_flag));
16463 case FIX_TRUNC_EXPR:
16464 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16465 false, complain));
16467 case ADDR_EXPR:
16468 op1 = TREE_OPERAND (t, 0);
16469 if (TREE_CODE (op1) == LABEL_DECL)
16470 RETURN (finish_label_address_expr (DECL_NAME (op1),
16471 EXPR_LOCATION (op1)));
16472 if (TREE_CODE (op1) == SCOPE_REF)
16473 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16474 /*done=*/true, /*address_p=*/true);
16475 else
16476 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16477 in_decl);
16478 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16479 complain|decltype_flag));
16481 case PLUS_EXPR:
16482 case MINUS_EXPR:
16483 case MULT_EXPR:
16484 case TRUNC_DIV_EXPR:
16485 case CEIL_DIV_EXPR:
16486 case FLOOR_DIV_EXPR:
16487 case ROUND_DIV_EXPR:
16488 case EXACT_DIV_EXPR:
16489 case BIT_AND_EXPR:
16490 case BIT_IOR_EXPR:
16491 case BIT_XOR_EXPR:
16492 case TRUNC_MOD_EXPR:
16493 case FLOOR_MOD_EXPR:
16494 case TRUTH_ANDIF_EXPR:
16495 case TRUTH_ORIF_EXPR:
16496 case TRUTH_AND_EXPR:
16497 case TRUTH_OR_EXPR:
16498 case RSHIFT_EXPR:
16499 case LSHIFT_EXPR:
16500 case RROTATE_EXPR:
16501 case LROTATE_EXPR:
16502 case EQ_EXPR:
16503 case NE_EXPR:
16504 case MAX_EXPR:
16505 case MIN_EXPR:
16506 case LE_EXPR:
16507 case GE_EXPR:
16508 case LT_EXPR:
16509 case GT_EXPR:
16510 case MEMBER_REF:
16511 case DOTSTAR_EXPR:
16513 warning_sentinel s1(warn_type_limits);
16514 warning_sentinel s2(warn_div_by_zero);
16515 warning_sentinel s3(warn_logical_op);
16516 warning_sentinel s4(warn_tautological_compare);
16517 tree op0 = RECUR (TREE_OPERAND (t, 0));
16518 tree op1 = RECUR (TREE_OPERAND (t, 1));
16519 tree r = build_x_binary_op
16520 (input_location, TREE_CODE (t),
16521 op0,
16522 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16523 ? ERROR_MARK
16524 : TREE_CODE (TREE_OPERAND (t, 0))),
16525 op1,
16526 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16527 ? ERROR_MARK
16528 : TREE_CODE (TREE_OPERAND (t, 1))),
16529 /*overload=*/NULL,
16530 complain|decltype_flag);
16531 if (EXPR_P (r) && TREE_NO_WARNING (t))
16532 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16534 RETURN (r);
16537 case POINTER_PLUS_EXPR:
16539 tree op0 = RECUR (TREE_OPERAND (t, 0));
16540 tree op1 = RECUR (TREE_OPERAND (t, 1));
16541 return fold_build_pointer_plus (op0, op1);
16544 case SCOPE_REF:
16545 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16546 /*address_p=*/false));
16547 case ARRAY_REF:
16548 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16549 args, complain, in_decl);
16550 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16551 RECUR (TREE_OPERAND (t, 1)),
16552 complain|decltype_flag));
16554 case ARRAY_NOTATION_REF:
16556 tree start_index, length, stride;
16557 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16558 args, complain, in_decl);
16559 start_index = RECUR (ARRAY_NOTATION_START (t));
16560 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16561 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16562 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16563 length, stride, TREE_TYPE (op1)));
16565 case SIZEOF_EXPR:
16566 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16567 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16568 RETURN (tsubst_copy (t, args, complain, in_decl));
16569 /* Fall through */
16571 case ALIGNOF_EXPR:
16573 tree r;
16575 op1 = TREE_OPERAND (t, 0);
16576 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16577 op1 = TREE_TYPE (op1);
16578 if (!args)
16580 /* When there are no ARGS, we are trying to evaluate a
16581 non-dependent expression from the parser. Trying to do
16582 the substitutions may not work. */
16583 if (!TYPE_P (op1))
16584 op1 = TREE_TYPE (op1);
16586 else
16588 ++cp_unevaluated_operand;
16589 ++c_inhibit_evaluation_warnings;
16590 if (TYPE_P (op1))
16591 op1 = tsubst (op1, args, complain, in_decl);
16592 else
16593 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16594 /*function_p=*/false,
16595 /*integral_constant_expression_p=*/
16596 false);
16597 --cp_unevaluated_operand;
16598 --c_inhibit_evaluation_warnings;
16600 if (TYPE_P (op1))
16601 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16602 complain & tf_error);
16603 else
16604 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16605 complain & tf_error);
16606 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16608 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16610 if (!processing_template_decl && TYPE_P (op1))
16612 r = build_min (SIZEOF_EXPR, size_type_node,
16613 build1 (NOP_EXPR, op1, error_mark_node));
16614 SIZEOF_EXPR_TYPE_P (r) = 1;
16616 else
16617 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16618 TREE_SIDE_EFFECTS (r) = 0;
16619 TREE_READONLY (r) = 1;
16621 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16623 RETURN (r);
16626 case AT_ENCODE_EXPR:
16628 op1 = TREE_OPERAND (t, 0);
16629 ++cp_unevaluated_operand;
16630 ++c_inhibit_evaluation_warnings;
16631 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16632 /*function_p=*/false,
16633 /*integral_constant_expression_p=*/false);
16634 --cp_unevaluated_operand;
16635 --c_inhibit_evaluation_warnings;
16636 RETURN (objc_build_encode_expr (op1));
16639 case NOEXCEPT_EXPR:
16640 op1 = TREE_OPERAND (t, 0);
16641 ++cp_unevaluated_operand;
16642 ++c_inhibit_evaluation_warnings;
16643 ++cp_noexcept_operand;
16644 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16645 /*function_p=*/false,
16646 /*integral_constant_expression_p=*/false);
16647 --cp_unevaluated_operand;
16648 --c_inhibit_evaluation_warnings;
16649 --cp_noexcept_operand;
16650 RETURN (finish_noexcept_expr (op1, complain));
16652 case MODOP_EXPR:
16654 warning_sentinel s(warn_div_by_zero);
16655 tree lhs = RECUR (TREE_OPERAND (t, 0));
16656 tree rhs = RECUR (TREE_OPERAND (t, 2));
16657 tree r = build_x_modify_expr
16658 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16659 complain|decltype_flag);
16660 /* TREE_NO_WARNING must be set if either the expression was
16661 parenthesized or it uses an operator such as >>= rather
16662 than plain assignment. In the former case, it was already
16663 set and must be copied. In the latter case,
16664 build_x_modify_expr sets it and it must not be reset
16665 here. */
16666 if (TREE_NO_WARNING (t))
16667 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16669 RETURN (r);
16672 case ARROW_EXPR:
16673 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16674 args, complain, in_decl);
16675 /* Remember that there was a reference to this entity. */
16676 if (DECL_P (op1)
16677 && !mark_used (op1, complain) && !(complain & tf_error))
16678 RETURN (error_mark_node);
16679 RETURN (build_x_arrow (input_location, op1, complain));
16681 case NEW_EXPR:
16683 tree placement = RECUR (TREE_OPERAND (t, 0));
16684 tree init = RECUR (TREE_OPERAND (t, 3));
16685 vec<tree, va_gc> *placement_vec;
16686 vec<tree, va_gc> *init_vec;
16687 tree ret;
16689 if (placement == NULL_TREE)
16690 placement_vec = NULL;
16691 else
16693 placement_vec = make_tree_vector ();
16694 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16695 vec_safe_push (placement_vec, TREE_VALUE (placement));
16698 /* If there was an initializer in the original tree, but it
16699 instantiated to an empty list, then we should pass a
16700 non-NULL empty vector to tell build_new that it was an
16701 empty initializer() rather than no initializer. This can
16702 only happen when the initializer is a pack expansion whose
16703 parameter packs are of length zero. */
16704 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16705 init_vec = NULL;
16706 else
16708 init_vec = make_tree_vector ();
16709 if (init == void_node)
16710 gcc_assert (init_vec != NULL);
16711 else
16713 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16714 vec_safe_push (init_vec, TREE_VALUE (init));
16718 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16719 tree op2 = RECUR (TREE_OPERAND (t, 2));
16720 ret = build_new (&placement_vec, op1, op2, &init_vec,
16721 NEW_EXPR_USE_GLOBAL (t),
16722 complain);
16724 if (placement_vec != NULL)
16725 release_tree_vector (placement_vec);
16726 if (init_vec != NULL)
16727 release_tree_vector (init_vec);
16729 RETURN (ret);
16732 case DELETE_EXPR:
16734 tree op0 = RECUR (TREE_OPERAND (t, 0));
16735 tree op1 = RECUR (TREE_OPERAND (t, 1));
16736 RETURN (delete_sanity (op0, op1,
16737 DELETE_EXPR_USE_VEC (t),
16738 DELETE_EXPR_USE_GLOBAL (t),
16739 complain));
16742 case COMPOUND_EXPR:
16744 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
16745 complain & ~tf_decltype, in_decl,
16746 /*function_p=*/false,
16747 integral_constant_expression_p);
16748 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
16749 op0,
16750 RECUR (TREE_OPERAND (t, 1)),
16751 complain|decltype_flag));
16754 case CALL_EXPR:
16756 tree function;
16757 vec<tree, va_gc> *call_args;
16758 unsigned int nargs, i;
16759 bool qualified_p;
16760 bool koenig_p;
16761 tree ret;
16763 function = CALL_EXPR_FN (t);
16764 /* Internal function with no arguments. */
16765 if (function == NULL_TREE && call_expr_nargs (t) == 0)
16766 RETURN (t);
16768 /* When we parsed the expression, we determined whether or
16769 not Koenig lookup should be performed. */
16770 koenig_p = KOENIG_LOOKUP_P (t);
16771 if (function == NULL_TREE)
16773 koenig_p = false;
16774 qualified_p = false;
16776 else if (TREE_CODE (function) == SCOPE_REF)
16778 qualified_p = true;
16779 function = tsubst_qualified_id (function, args, complain, in_decl,
16780 /*done=*/false,
16781 /*address_p=*/false);
16783 else if (koenig_p && identifier_p (function))
16785 /* Do nothing; calling tsubst_copy_and_build on an identifier
16786 would incorrectly perform unqualified lookup again.
16788 Note that we can also have an IDENTIFIER_NODE if the earlier
16789 unqualified lookup found a member function; in that case
16790 koenig_p will be false and we do want to do the lookup
16791 again to find the instantiated member function.
16793 FIXME but doing that causes c++/15272, so we need to stop
16794 using IDENTIFIER_NODE in that situation. */
16795 qualified_p = false;
16797 else
16799 if (TREE_CODE (function) == COMPONENT_REF)
16801 tree op = TREE_OPERAND (function, 1);
16803 qualified_p = (TREE_CODE (op) == SCOPE_REF
16804 || (BASELINK_P (op)
16805 && BASELINK_QUALIFIED_P (op)));
16807 else
16808 qualified_p = false;
16810 if (TREE_CODE (function) == ADDR_EXPR
16811 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
16812 /* Avoid error about taking the address of a constructor. */
16813 function = TREE_OPERAND (function, 0);
16815 function = tsubst_copy_and_build (function, args, complain,
16816 in_decl,
16817 !qualified_p,
16818 integral_constant_expression_p);
16820 if (BASELINK_P (function))
16821 qualified_p = true;
16824 nargs = call_expr_nargs (t);
16825 call_args = make_tree_vector ();
16826 for (i = 0; i < nargs; ++i)
16828 tree arg = CALL_EXPR_ARG (t, i);
16830 if (!PACK_EXPANSION_P (arg))
16831 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
16832 else
16834 /* Expand the pack expansion and push each entry onto
16835 CALL_ARGS. */
16836 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
16837 if (TREE_CODE (arg) == TREE_VEC)
16839 unsigned int len, j;
16841 len = TREE_VEC_LENGTH (arg);
16842 for (j = 0; j < len; ++j)
16844 tree value = TREE_VEC_ELT (arg, j);
16845 if (value != NULL_TREE)
16846 value = convert_from_reference (value);
16847 vec_safe_push (call_args, value);
16850 else
16852 /* A partial substitution. Add one entry. */
16853 vec_safe_push (call_args, arg);
16858 /* We do not perform argument-dependent lookup if normal
16859 lookup finds a non-function, in accordance with the
16860 expected resolution of DR 218. */
16861 if (koenig_p
16862 && ((is_overloaded_fn (function)
16863 /* If lookup found a member function, the Koenig lookup is
16864 not appropriate, even if an unqualified-name was used
16865 to denote the function. */
16866 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
16867 || identifier_p (function))
16868 /* Only do this when substitution turns a dependent call
16869 into a non-dependent call. */
16870 && type_dependent_expression_p_push (t)
16871 && !any_type_dependent_arguments_p (call_args))
16872 function = perform_koenig_lookup (function, call_args, tf_none);
16874 if (function != NULL_TREE
16875 && identifier_p (function)
16876 && !any_type_dependent_arguments_p (call_args))
16878 if (koenig_p && (complain & tf_warning_or_error))
16880 /* For backwards compatibility and good diagnostics, try
16881 the unqualified lookup again if we aren't in SFINAE
16882 context. */
16883 tree unq = (tsubst_copy_and_build
16884 (function, args, complain, in_decl, true,
16885 integral_constant_expression_p));
16886 if (unq == error_mark_node)
16888 release_tree_vector (call_args);
16889 RETURN (error_mark_node);
16892 if (unq != function)
16894 tree fn = unq;
16895 if (INDIRECT_REF_P (fn))
16896 fn = TREE_OPERAND (fn, 0);
16897 if (TREE_CODE (fn) == COMPONENT_REF)
16898 fn = TREE_OPERAND (fn, 1);
16899 if (is_overloaded_fn (fn))
16900 fn = get_first_fn (fn);
16901 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16902 "%qD was not declared in this scope, "
16903 "and no declarations were found by "
16904 "argument-dependent lookup at the point "
16905 "of instantiation", function))
16907 if (!DECL_P (fn))
16908 /* Can't say anything more. */;
16909 else if (DECL_CLASS_SCOPE_P (fn))
16911 location_t loc = EXPR_LOC_OR_LOC (t,
16912 input_location);
16913 inform (loc,
16914 "declarations in dependent base %qT are "
16915 "not found by unqualified lookup",
16916 DECL_CLASS_CONTEXT (fn));
16917 if (current_class_ptr)
16918 inform (loc,
16919 "use %<this->%D%> instead", function);
16920 else
16921 inform (loc,
16922 "use %<%T::%D%> instead",
16923 current_class_name, function);
16925 else
16926 inform (DECL_SOURCE_LOCATION (fn),
16927 "%qD declared here, later in the "
16928 "translation unit", fn);
16930 function = unq;
16933 if (identifier_p (function))
16935 if (complain & tf_error)
16936 unqualified_name_lookup_error (function);
16937 release_tree_vector (call_args);
16938 RETURN (error_mark_node);
16942 /* Remember that there was a reference to this entity. */
16943 if (function != NULL_TREE
16944 && DECL_P (function)
16945 && !mark_used (function, complain) && !(complain & tf_error))
16947 release_tree_vector (call_args);
16948 RETURN (error_mark_node);
16951 /* Put back tf_decltype for the actual call. */
16952 complain |= decltype_flag;
16954 if (function == NULL_TREE)
16955 switch (CALL_EXPR_IFN (t))
16957 case IFN_LAUNDER:
16958 gcc_assert (nargs == 1);
16959 if (vec_safe_length (call_args) != 1)
16961 error_at (EXPR_LOC_OR_LOC (t, input_location),
16962 "wrong number of arguments to "
16963 "%<__builtin_launder%>");
16964 ret = error_mark_node;
16966 else
16967 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
16968 input_location),
16969 (*call_args)[0], complain);
16970 break;
16972 default:
16973 /* Unsupported internal function with arguments. */
16974 gcc_unreachable ();
16976 else if (TREE_CODE (function) == OFFSET_REF)
16977 ret = build_offset_ref_call_from_tree (function, &call_args,
16978 complain);
16979 else if (TREE_CODE (function) == COMPONENT_REF)
16981 tree instance = TREE_OPERAND (function, 0);
16982 tree fn = TREE_OPERAND (function, 1);
16984 if (processing_template_decl
16985 && (type_dependent_expression_p (instance)
16986 || (!BASELINK_P (fn)
16987 && TREE_CODE (fn) != FIELD_DECL)
16988 || type_dependent_expression_p (fn)
16989 || any_type_dependent_arguments_p (call_args)))
16990 ret = build_nt_call_vec (function, call_args);
16991 else if (!BASELINK_P (fn))
16992 ret = finish_call_expr (function, &call_args,
16993 /*disallow_virtual=*/false,
16994 /*koenig_p=*/false,
16995 complain);
16996 else
16997 ret = (build_new_method_call
16998 (instance, fn,
16999 &call_args, NULL_TREE,
17000 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17001 /*fn_p=*/NULL,
17002 complain));
17004 else
17005 ret = finish_call_expr (function, &call_args,
17006 /*disallow_virtual=*/qualified_p,
17007 koenig_p,
17008 complain);
17010 release_tree_vector (call_args);
17012 if (ret != error_mark_node)
17014 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17015 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17016 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17017 bool thk = CALL_FROM_THUNK_P (t);
17018 if (op || ord || rev || thk)
17020 function = extract_call_expr (ret);
17021 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17022 CALL_EXPR_ORDERED_ARGS (function) = ord;
17023 CALL_EXPR_REVERSE_ARGS (function) = rev;
17024 if (thk)
17026 CALL_FROM_THUNK_P (function) = true;
17027 /* The thunk location is not interesting. */
17028 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17033 RETURN (ret);
17036 case COND_EXPR:
17038 tree cond = RECUR (TREE_OPERAND (t, 0));
17039 tree folded_cond = fold_non_dependent_expr (cond);
17040 tree exp1, exp2;
17042 if (TREE_CODE (folded_cond) == INTEGER_CST)
17044 if (integer_zerop (folded_cond))
17046 ++c_inhibit_evaluation_warnings;
17047 exp1 = RECUR (TREE_OPERAND (t, 1));
17048 --c_inhibit_evaluation_warnings;
17049 exp2 = RECUR (TREE_OPERAND (t, 2));
17051 else
17053 exp1 = RECUR (TREE_OPERAND (t, 1));
17054 ++c_inhibit_evaluation_warnings;
17055 exp2 = RECUR (TREE_OPERAND (t, 2));
17056 --c_inhibit_evaluation_warnings;
17058 cond = folded_cond;
17060 else
17062 exp1 = RECUR (TREE_OPERAND (t, 1));
17063 exp2 = RECUR (TREE_OPERAND (t, 2));
17066 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17067 cond, exp1, exp2, complain));
17070 case PSEUDO_DTOR_EXPR:
17072 tree op0 = RECUR (TREE_OPERAND (t, 0));
17073 tree op1 = RECUR (TREE_OPERAND (t, 1));
17074 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17075 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17076 input_location));
17079 case TREE_LIST:
17081 tree purpose, value, chain;
17083 if (t == void_list_node)
17084 RETURN (t);
17086 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17087 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17089 /* We have pack expansions, so expand those and
17090 create a new list out of it. */
17091 tree purposevec = NULL_TREE;
17092 tree valuevec = NULL_TREE;
17093 tree chain;
17094 int i, len = -1;
17096 /* Expand the argument expressions. */
17097 if (TREE_PURPOSE (t))
17098 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17099 complain, in_decl);
17100 if (TREE_VALUE (t))
17101 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17102 complain, in_decl);
17104 /* Build the rest of the list. */
17105 chain = TREE_CHAIN (t);
17106 if (chain && chain != void_type_node)
17107 chain = RECUR (chain);
17109 /* Determine the number of arguments. */
17110 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17112 len = TREE_VEC_LENGTH (purposevec);
17113 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17115 else if (TREE_CODE (valuevec) == TREE_VEC)
17116 len = TREE_VEC_LENGTH (valuevec);
17117 else
17119 /* Since we only performed a partial substitution into
17120 the argument pack, we only RETURN (a single list
17121 node. */
17122 if (purposevec == TREE_PURPOSE (t)
17123 && valuevec == TREE_VALUE (t)
17124 && chain == TREE_CHAIN (t))
17125 RETURN (t);
17127 RETURN (tree_cons (purposevec, valuevec, chain));
17130 /* Convert the argument vectors into a TREE_LIST */
17131 i = len;
17132 while (i > 0)
17134 /* Grab the Ith values. */
17135 i--;
17136 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17137 : NULL_TREE;
17138 value
17139 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17140 : NULL_TREE;
17142 /* Build the list (backwards). */
17143 chain = tree_cons (purpose, value, chain);
17146 RETURN (chain);
17149 purpose = TREE_PURPOSE (t);
17150 if (purpose)
17151 purpose = RECUR (purpose);
17152 value = TREE_VALUE (t);
17153 if (value)
17154 value = RECUR (value);
17155 chain = TREE_CHAIN (t);
17156 if (chain && chain != void_type_node)
17157 chain = RECUR (chain);
17158 if (purpose == TREE_PURPOSE (t)
17159 && value == TREE_VALUE (t)
17160 && chain == TREE_CHAIN (t))
17161 RETURN (t);
17162 RETURN (tree_cons (purpose, value, chain));
17165 case COMPONENT_REF:
17167 tree object;
17168 tree object_type;
17169 tree member;
17170 tree r;
17172 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17173 args, complain, in_decl);
17174 /* Remember that there was a reference to this entity. */
17175 if (DECL_P (object)
17176 && !mark_used (object, complain) && !(complain & tf_error))
17177 RETURN (error_mark_node);
17178 object_type = TREE_TYPE (object);
17180 member = TREE_OPERAND (t, 1);
17181 if (BASELINK_P (member))
17182 member = tsubst_baselink (member,
17183 non_reference (TREE_TYPE (object)),
17184 args, complain, in_decl);
17185 else
17186 member = tsubst_copy (member, args, complain, in_decl);
17187 if (member == error_mark_node)
17188 RETURN (error_mark_node);
17190 if (type_dependent_expression_p (object))
17191 /* We can't do much here. */;
17192 else if (!CLASS_TYPE_P (object_type))
17194 if (scalarish_type_p (object_type))
17196 tree s = NULL_TREE;
17197 tree dtor = member;
17199 if (TREE_CODE (dtor) == SCOPE_REF)
17201 s = TREE_OPERAND (dtor, 0);
17202 dtor = TREE_OPERAND (dtor, 1);
17204 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17206 dtor = TREE_OPERAND (dtor, 0);
17207 if (TYPE_P (dtor))
17208 RETURN (finish_pseudo_destructor_expr
17209 (object, s, dtor, input_location));
17213 else if (TREE_CODE (member) == SCOPE_REF
17214 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17216 /* Lookup the template functions now that we know what the
17217 scope is. */
17218 tree scope = TREE_OPERAND (member, 0);
17219 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17220 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17221 member = lookup_qualified_name (scope, tmpl,
17222 /*is_type_p=*/false,
17223 /*complain=*/false);
17224 if (BASELINK_P (member))
17226 BASELINK_FUNCTIONS (member)
17227 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17228 args);
17229 member = (adjust_result_of_qualified_name_lookup
17230 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17231 object_type));
17233 else
17235 qualified_name_lookup_error (scope, tmpl, member,
17236 input_location);
17237 RETURN (error_mark_node);
17240 else if (TREE_CODE (member) == SCOPE_REF
17241 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17242 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17244 if (complain & tf_error)
17246 if (TYPE_P (TREE_OPERAND (member, 0)))
17247 error ("%qT is not a class or namespace",
17248 TREE_OPERAND (member, 0));
17249 else
17250 error ("%qD is not a class or namespace",
17251 TREE_OPERAND (member, 0));
17253 RETURN (error_mark_node);
17255 else if (TREE_CODE (member) == FIELD_DECL)
17257 r = finish_non_static_data_member (member, object, NULL_TREE);
17258 if (TREE_CODE (r) == COMPONENT_REF)
17259 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17260 RETURN (r);
17263 r = finish_class_member_access_expr (object, member,
17264 /*template_p=*/false,
17265 complain);
17266 if (TREE_CODE (r) == COMPONENT_REF)
17267 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17268 RETURN (r);
17271 case THROW_EXPR:
17272 RETURN (build_throw
17273 (RECUR (TREE_OPERAND (t, 0))));
17275 case CONSTRUCTOR:
17277 vec<constructor_elt, va_gc> *n;
17278 constructor_elt *ce;
17279 unsigned HOST_WIDE_INT idx;
17280 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17281 bool process_index_p;
17282 int newlen;
17283 bool need_copy_p = false;
17284 tree r;
17286 if (type == error_mark_node)
17287 RETURN (error_mark_node);
17289 /* digest_init will do the wrong thing if we let it. */
17290 if (type && TYPE_PTRMEMFUNC_P (type))
17291 RETURN (t);
17293 /* We do not want to process the index of aggregate
17294 initializers as they are identifier nodes which will be
17295 looked up by digest_init. */
17296 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17298 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17299 newlen = vec_safe_length (n);
17300 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17302 if (ce->index && process_index_p
17303 /* An identifier index is looked up in the type
17304 being initialized, not the current scope. */
17305 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17306 ce->index = RECUR (ce->index);
17308 if (PACK_EXPANSION_P (ce->value))
17310 /* Substitute into the pack expansion. */
17311 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17312 in_decl);
17314 if (ce->value == error_mark_node
17315 || PACK_EXPANSION_P (ce->value))
17317 else if (TREE_VEC_LENGTH (ce->value) == 1)
17318 /* Just move the argument into place. */
17319 ce->value = TREE_VEC_ELT (ce->value, 0);
17320 else
17322 /* Update the length of the final CONSTRUCTOR
17323 arguments vector, and note that we will need to
17324 copy.*/
17325 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17326 need_copy_p = true;
17329 else
17330 ce->value = RECUR (ce->value);
17333 if (need_copy_p)
17335 vec<constructor_elt, va_gc> *old_n = n;
17337 vec_alloc (n, newlen);
17338 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17340 if (TREE_CODE (ce->value) == TREE_VEC)
17342 int i, len = TREE_VEC_LENGTH (ce->value);
17343 for (i = 0; i < len; ++i)
17344 CONSTRUCTOR_APPEND_ELT (n, 0,
17345 TREE_VEC_ELT (ce->value, i));
17347 else
17348 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17352 r = build_constructor (init_list_type_node, n);
17353 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17355 if (TREE_HAS_CONSTRUCTOR (t))
17356 RETURN (finish_compound_literal (type, r, complain));
17358 TREE_TYPE (r) = type;
17359 RETURN (r);
17362 case TYPEID_EXPR:
17364 tree operand_0 = TREE_OPERAND (t, 0);
17365 if (TYPE_P (operand_0))
17367 operand_0 = tsubst (operand_0, args, complain, in_decl);
17368 RETURN (get_typeid (operand_0, complain));
17370 else
17372 operand_0 = RECUR (operand_0);
17373 RETURN (build_typeid (operand_0, complain));
17377 case VAR_DECL:
17378 if (!args)
17379 RETURN (t);
17380 else if (DECL_PACK_P (t))
17382 /* We don't build decls for an instantiation of a
17383 variadic capture proxy, we instantiate the elements
17384 when needed. */
17385 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17386 return RECUR (DECL_VALUE_EXPR (t));
17388 /* Fall through */
17390 case PARM_DECL:
17392 tree r = tsubst_copy (t, args, complain, in_decl);
17393 /* ??? We're doing a subset of finish_id_expression here. */
17394 if (VAR_P (r)
17395 && !processing_template_decl
17396 && !cp_unevaluated_operand
17397 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17398 && CP_DECL_THREAD_LOCAL_P (r))
17400 if (tree wrap = get_tls_wrapper_fn (r))
17401 /* Replace an evaluated use of the thread_local variable with
17402 a call to its wrapper. */
17403 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17405 else if (outer_automatic_var_p (r))
17407 r = process_outer_var_ref (r, complain);
17408 if (is_capture_proxy (r))
17409 register_local_specialization (r, t);
17412 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17413 /* If the original type was a reference, we'll be wrapped in
17414 the appropriate INDIRECT_REF. */
17415 r = convert_from_reference (r);
17416 RETURN (r);
17419 case VA_ARG_EXPR:
17421 tree op0 = RECUR (TREE_OPERAND (t, 0));
17422 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17423 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17426 case OFFSETOF_EXPR:
17427 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
17428 EXPR_LOCATION (t)));
17430 case ADDRESSOF_EXPR:
17431 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17432 RECUR (TREE_OPERAND (t, 0)), complain));
17434 case TRAIT_EXPR:
17436 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17437 complain, in_decl);
17439 tree type2 = TRAIT_EXPR_TYPE2 (t);
17440 if (type2 && TREE_CODE (type2) == TREE_LIST)
17441 type2 = RECUR (type2);
17442 else if (type2)
17443 type2 = tsubst (type2, args, complain, in_decl);
17445 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17448 case STMT_EXPR:
17450 tree old_stmt_expr = cur_stmt_expr;
17451 tree stmt_expr = begin_stmt_expr ();
17453 cur_stmt_expr = stmt_expr;
17454 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17455 integral_constant_expression_p);
17456 stmt_expr = finish_stmt_expr (stmt_expr, false);
17457 cur_stmt_expr = old_stmt_expr;
17459 /* If the resulting list of expression statement is empty,
17460 fold it further into void_node. */
17461 if (empty_expr_stmt_p (stmt_expr))
17462 stmt_expr = void_node;
17464 RETURN (stmt_expr);
17467 case LAMBDA_EXPR:
17469 tree r = build_lambda_expr ();
17471 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17472 LAMBDA_EXPR_CLOSURE (r) = type;
17473 CLASSTYPE_LAMBDA_EXPR (type) = r;
17475 LAMBDA_EXPR_LOCATION (r)
17476 = LAMBDA_EXPR_LOCATION (t);
17477 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17478 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17479 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17480 LAMBDA_EXPR_DISCRIMINATOR (r)
17481 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17482 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17483 if (!scope)
17484 /* No substitution needed. */;
17485 else if (VAR_OR_FUNCTION_DECL_P (scope))
17486 /* For a function or variable scope, we want to use tsubst so that we
17487 don't complain about referring to an auto before deduction. */
17488 scope = tsubst (scope, args, complain, in_decl);
17489 else if (TREE_CODE (scope) == PARM_DECL)
17491 /* Look up the parameter we want directly, as tsubst_copy
17492 doesn't do what we need. */
17493 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17494 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17495 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17496 parm = DECL_CHAIN (parm);
17497 scope = parm;
17498 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17499 if (DECL_CONTEXT (scope) == NULL_TREE)
17500 DECL_CONTEXT (scope) = fn;
17502 else if (TREE_CODE (scope) == FIELD_DECL)
17503 /* For a field, use tsubst_copy so that we look up the existing field
17504 rather than build a new one. */
17505 scope = RECUR (scope);
17506 else
17507 gcc_unreachable ();
17508 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17510 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17511 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17513 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17514 determine_visibility (TYPE_NAME (type));
17515 /* Now that we know visibility, instantiate the type so we have a
17516 declaration of the op() for later calls to lambda_function. */
17517 complete_type (type);
17519 if (tree fn = lambda_function (type))
17520 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17522 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17524 insert_pending_capture_proxies ();
17526 RETURN (build_lambda_object (r));
17529 case TARGET_EXPR:
17530 /* We can get here for a constant initializer of non-dependent type.
17531 FIXME stop folding in cp_parser_initializer_clause. */
17533 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17534 complain);
17535 RETURN (r);
17538 case TRANSACTION_EXPR:
17539 RETURN (tsubst_expr(t, args, complain, in_decl,
17540 integral_constant_expression_p));
17542 case PAREN_EXPR:
17543 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17545 case VEC_PERM_EXPR:
17547 tree op0 = RECUR (TREE_OPERAND (t, 0));
17548 tree op1 = RECUR (TREE_OPERAND (t, 1));
17549 tree op2 = RECUR (TREE_OPERAND (t, 2));
17550 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17551 complain));
17554 case REQUIRES_EXPR:
17555 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17557 default:
17558 /* Handle Objective-C++ constructs, if appropriate. */
17560 tree subst
17561 = objcp_tsubst_copy_and_build (t, args, complain,
17562 in_decl, /*function_p=*/false);
17563 if (subst)
17564 RETURN (subst);
17566 RETURN (tsubst_copy (t, args, complain, in_decl));
17569 #undef RECUR
17570 #undef RETURN
17571 out:
17572 input_location = loc;
17573 return retval;
17576 /* Verify that the instantiated ARGS are valid. For type arguments,
17577 make sure that the type's linkage is ok. For non-type arguments,
17578 make sure they are constants if they are integral or enumerations.
17579 Emit an error under control of COMPLAIN, and return TRUE on error. */
17581 static bool
17582 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17584 if (dependent_template_arg_p (t))
17585 return false;
17586 if (ARGUMENT_PACK_P (t))
17588 tree vec = ARGUMENT_PACK_ARGS (t);
17589 int len = TREE_VEC_LENGTH (vec);
17590 bool result = false;
17591 int i;
17593 for (i = 0; i < len; ++i)
17594 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17595 result = true;
17596 return result;
17598 else if (TYPE_P (t))
17600 /* [basic.link]: A name with no linkage (notably, the name
17601 of a class or enumeration declared in a local scope)
17602 shall not be used to declare an entity with linkage.
17603 This implies that names with no linkage cannot be used as
17604 template arguments
17606 DR 757 relaxes this restriction for C++0x. */
17607 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17608 : no_linkage_check (t, /*relaxed_p=*/false));
17610 if (nt)
17612 /* DR 488 makes use of a type with no linkage cause
17613 type deduction to fail. */
17614 if (complain & tf_error)
17616 if (TYPE_UNNAMED_P (nt))
17617 error ("%qT is/uses unnamed type", t);
17618 else
17619 error ("template argument for %qD uses local type %qT",
17620 tmpl, t);
17622 return true;
17624 /* In order to avoid all sorts of complications, we do not
17625 allow variably-modified types as template arguments. */
17626 else if (variably_modified_type_p (t, NULL_TREE))
17628 if (complain & tf_error)
17629 error ("%qT is a variably modified type", t);
17630 return true;
17633 /* Class template and alias template arguments should be OK. */
17634 else if (DECL_TYPE_TEMPLATE_P (t))
17636 /* A non-type argument of integral or enumerated type must be a
17637 constant. */
17638 else if (TREE_TYPE (t)
17639 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17640 && !REFERENCE_REF_P (t)
17641 && !TREE_CONSTANT (t))
17643 if (complain & tf_error)
17644 error ("integral expression %qE is not constant", t);
17645 return true;
17647 return false;
17650 static bool
17651 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17653 int ix, len = DECL_NTPARMS (tmpl);
17654 bool result = false;
17656 for (ix = 0; ix != len; ix++)
17658 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17659 result = true;
17661 if (result && (complain & tf_error))
17662 error (" trying to instantiate %qD", tmpl);
17663 return result;
17666 /* We're out of SFINAE context now, so generate diagnostics for the access
17667 errors we saw earlier when instantiating D from TMPL and ARGS. */
17669 static void
17670 recheck_decl_substitution (tree d, tree tmpl, tree args)
17672 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17673 tree type = TREE_TYPE (pattern);
17674 location_t loc = input_location;
17676 push_access_scope (d);
17677 push_deferring_access_checks (dk_no_deferred);
17678 input_location = DECL_SOURCE_LOCATION (pattern);
17679 tsubst (type, args, tf_warning_or_error, d);
17680 input_location = loc;
17681 pop_deferring_access_checks ();
17682 pop_access_scope (d);
17685 /* Instantiate the indicated variable, function, or alias template TMPL with
17686 the template arguments in TARG_PTR. */
17688 static tree
17689 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17691 tree targ_ptr = orig_args;
17692 tree fndecl;
17693 tree gen_tmpl;
17694 tree spec;
17695 bool access_ok = true;
17697 if (tmpl == error_mark_node)
17698 return error_mark_node;
17700 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
17702 /* If this function is a clone, handle it specially. */
17703 if (DECL_CLONED_FUNCTION_P (tmpl))
17705 tree spec;
17706 tree clone;
17708 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17709 DECL_CLONED_FUNCTION. */
17710 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
17711 targ_ptr, complain);
17712 if (spec == error_mark_node)
17713 return error_mark_node;
17715 /* Look for the clone. */
17716 FOR_EACH_CLONE (clone, spec)
17717 if (DECL_NAME (clone) == DECL_NAME (tmpl))
17718 return clone;
17719 /* We should always have found the clone by now. */
17720 gcc_unreachable ();
17721 return NULL_TREE;
17724 if (targ_ptr == error_mark_node)
17725 return error_mark_node;
17727 /* Check to see if we already have this specialization. */
17728 gen_tmpl = most_general_template (tmpl);
17729 if (TMPL_ARGS_DEPTH (targ_ptr)
17730 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
17731 /* targ_ptr only has the innermost template args, so add the outer ones
17732 from tmpl, which could be either a partial instantiation or gen_tmpl (in
17733 the case of a non-dependent call within a template definition). */
17734 targ_ptr = (add_outermost_template_args
17735 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
17736 targ_ptr));
17738 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17739 but it doesn't seem to be on the hot path. */
17740 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
17742 gcc_assert (tmpl == gen_tmpl
17743 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
17744 == spec)
17745 || fndecl == NULL_TREE);
17747 if (spec != NULL_TREE)
17749 if (FNDECL_HAS_ACCESS_ERRORS (spec))
17751 if (complain & tf_error)
17752 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
17753 return error_mark_node;
17755 return spec;
17758 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
17759 complain))
17760 return error_mark_node;
17762 /* We are building a FUNCTION_DECL, during which the access of its
17763 parameters and return types have to be checked. However this
17764 FUNCTION_DECL which is the desired context for access checking
17765 is not built yet. We solve this chicken-and-egg problem by
17766 deferring all checks until we have the FUNCTION_DECL. */
17767 push_deferring_access_checks (dk_deferred);
17769 /* Instantiation of the function happens in the context of the function
17770 template, not the context of the overload resolution we're doing. */
17771 push_to_top_level ();
17772 /* If there are dependent arguments, e.g. because we're doing partial
17773 ordering, make sure processing_template_decl stays set. */
17774 if (uses_template_parms (targ_ptr))
17775 ++processing_template_decl;
17776 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17778 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
17779 complain, gen_tmpl, true);
17780 push_nested_class (ctx);
17783 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
17785 fndecl = NULL_TREE;
17786 if (VAR_P (pattern))
17788 /* We need to determine if we're using a partial or explicit
17789 specialization now, because the type of the variable could be
17790 different. */
17791 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
17792 tree elt = most_specialized_partial_spec (tid, complain);
17793 if (elt == error_mark_node)
17794 pattern = error_mark_node;
17795 else if (elt)
17797 tree partial_tmpl = TREE_VALUE (elt);
17798 tree partial_args = TREE_PURPOSE (elt);
17799 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
17800 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
17804 /* Substitute template parameters to obtain the specialization. */
17805 if (fndecl == NULL_TREE)
17806 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
17807 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17808 pop_nested_class ();
17809 pop_from_top_level ();
17811 if (fndecl == error_mark_node)
17813 pop_deferring_access_checks ();
17814 return error_mark_node;
17817 /* The DECL_TI_TEMPLATE should always be the immediate parent
17818 template, not the most general template. */
17819 DECL_TI_TEMPLATE (fndecl) = tmpl;
17820 DECL_TI_ARGS (fndecl) = targ_ptr;
17822 /* Now we know the specialization, compute access previously
17823 deferred. Do no access control for inheriting constructors,
17824 as we already checked access for the inherited constructor. */
17825 if (!(flag_new_inheriting_ctors
17826 && DECL_INHERITED_CTOR (fndecl)))
17828 push_access_scope (fndecl);
17829 if (!perform_deferred_access_checks (complain))
17830 access_ok = false;
17831 pop_access_scope (fndecl);
17833 pop_deferring_access_checks ();
17835 /* If we've just instantiated the main entry point for a function,
17836 instantiate all the alternate entry points as well. We do this
17837 by cloning the instantiation of the main entry point, not by
17838 instantiating the template clones. */
17839 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
17840 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
17842 if (!access_ok)
17844 if (!(complain & tf_error))
17846 /* Remember to reinstantiate when we're out of SFINAE so the user
17847 can see the errors. */
17848 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
17850 return error_mark_node;
17852 return fndecl;
17855 /* Wrapper for instantiate_template_1. */
17857 tree
17858 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
17860 tree ret;
17861 timevar_push (TV_TEMPLATE_INST);
17862 ret = instantiate_template_1 (tmpl, orig_args, complain);
17863 timevar_pop (TV_TEMPLATE_INST);
17864 return ret;
17867 /* Instantiate the alias template TMPL with ARGS. Also push a template
17868 instantiation level, which instantiate_template doesn't do because
17869 functions and variables have sufficient context established by the
17870 callers. */
17872 static tree
17873 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
17875 struct pending_template *old_last_pend = last_pending_template;
17876 struct tinst_level *old_error_tinst = last_error_tinst_level;
17877 if (tmpl == error_mark_node || args == error_mark_node)
17878 return error_mark_node;
17879 tree tinst = build_tree_list (tmpl, args);
17880 if (!push_tinst_level (tinst))
17882 ggc_free (tinst);
17883 return error_mark_node;
17886 args =
17887 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
17888 args, tmpl, complain,
17889 /*require_all_args=*/true,
17890 /*use_default_args=*/true);
17892 tree r = instantiate_template (tmpl, args, complain);
17893 pop_tinst_level ();
17894 /* We can't free this if a pending_template entry or last_error_tinst_level
17895 is pointing at it. */
17896 if (last_pending_template == old_last_pend
17897 && last_error_tinst_level == old_error_tinst)
17898 ggc_free (tinst);
17900 return r;
17903 /* PARM is a template parameter pack for FN. Returns true iff
17904 PARM is used in a deducible way in the argument list of FN. */
17906 static bool
17907 pack_deducible_p (tree parm, tree fn)
17909 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
17910 for (; t; t = TREE_CHAIN (t))
17912 tree type = TREE_VALUE (t);
17913 tree packs;
17914 if (!PACK_EXPANSION_P (type))
17915 continue;
17916 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
17917 packs; packs = TREE_CHAIN (packs))
17918 if (template_args_equal (TREE_VALUE (packs), parm))
17920 /* The template parameter pack is used in a function parameter
17921 pack. If this is the end of the parameter list, the
17922 template parameter pack is deducible. */
17923 if (TREE_CHAIN (t) == void_list_node)
17924 return true;
17925 else
17926 /* Otherwise, not. Well, it could be deduced from
17927 a non-pack parameter, but doing so would end up with
17928 a deduction mismatch, so don't bother. */
17929 return false;
17932 /* The template parameter pack isn't used in any function parameter
17933 packs, but it might be used deeper, e.g. tuple<Args...>. */
17934 return true;
17937 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17938 NARGS elements of the arguments that are being used when calling
17939 it. TARGS is a vector into which the deduced template arguments
17940 are placed.
17942 Returns either a FUNCTION_DECL for the matching specialization of FN or
17943 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17944 true, diagnostics will be printed to explain why it failed.
17946 If FN is a conversion operator, or we are trying to produce a specific
17947 specialization, RETURN_TYPE is the return type desired.
17949 The EXPLICIT_TARGS are explicit template arguments provided via a
17950 template-id.
17952 The parameter STRICT is one of:
17954 DEDUCE_CALL:
17955 We are deducing arguments for a function call, as in
17956 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17957 deducing arguments for a call to the result of a conversion
17958 function template, as in [over.call.object].
17960 DEDUCE_CONV:
17961 We are deducing arguments for a conversion function, as in
17962 [temp.deduct.conv].
17964 DEDUCE_EXACT:
17965 We are deducing arguments when doing an explicit instantiation
17966 as in [temp.explicit], when determining an explicit specialization
17967 as in [temp.expl.spec], or when taking the address of a function
17968 template, as in [temp.deduct.funcaddr]. */
17970 tree
17971 fn_type_unification (tree fn,
17972 tree explicit_targs,
17973 tree targs,
17974 const tree *args,
17975 unsigned int nargs,
17976 tree return_type,
17977 unification_kind_t strict,
17978 int flags,
17979 bool explain_p,
17980 bool decltype_p)
17982 tree parms;
17983 tree fntype;
17984 tree decl = NULL_TREE;
17985 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17986 bool ok;
17987 static int deduction_depth;
17988 struct pending_template *old_last_pend = last_pending_template;
17989 struct tinst_level *old_error_tinst = last_error_tinst_level;
17991 tree orig_fn = fn;
17992 if (flag_new_inheriting_ctors)
17993 fn = strip_inheriting_ctors (fn);
17995 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17996 tree tinst;
17997 tree r = error_mark_node;
17999 tree full_targs = targs;
18000 if (TMPL_ARGS_DEPTH (targs)
18001 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18002 full_targs = (add_outermost_template_args
18003 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18004 targs));
18006 if (decltype_p)
18007 complain |= tf_decltype;
18009 /* In C++0x, it's possible to have a function template whose type depends
18010 on itself recursively. This is most obvious with decltype, but can also
18011 occur with enumeration scope (c++/48969). So we need to catch infinite
18012 recursion and reject the substitution at deduction time; this function
18013 will return error_mark_node for any repeated substitution.
18015 This also catches excessive recursion such as when f<N> depends on
18016 f<N-1> across all integers, and returns error_mark_node for all the
18017 substitutions back up to the initial one.
18019 This is, of course, not reentrant. */
18020 if (excessive_deduction_depth)
18021 return error_mark_node;
18022 tinst = build_tree_list (fn, NULL_TREE);
18023 ++deduction_depth;
18025 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18027 fntype = TREE_TYPE (fn);
18028 if (explicit_targs)
18030 /* [temp.deduct]
18032 The specified template arguments must match the template
18033 parameters in kind (i.e., type, nontype, template), and there
18034 must not be more arguments than there are parameters;
18035 otherwise type deduction fails.
18037 Nontype arguments must match the types of the corresponding
18038 nontype template parameters, or must be convertible to the
18039 types of the corresponding nontype parameters as specified in
18040 _temp.arg.nontype_, otherwise type deduction fails.
18042 All references in the function type of the function template
18043 to the corresponding template parameters are replaced by the
18044 specified template argument values. If a substitution in a
18045 template parameter or in the function type of the function
18046 template results in an invalid type, type deduction fails. */
18047 int i, len = TREE_VEC_LENGTH (tparms);
18048 location_t loc = input_location;
18049 bool incomplete = false;
18051 if (explicit_targs == error_mark_node)
18052 goto fail;
18054 if (TMPL_ARGS_DEPTH (explicit_targs)
18055 < TMPL_ARGS_DEPTH (full_targs))
18056 explicit_targs = add_outermost_template_args (full_targs,
18057 explicit_targs);
18059 /* Adjust any explicit template arguments before entering the
18060 substitution context. */
18061 explicit_targs
18062 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18063 complain,
18064 /*require_all_args=*/false,
18065 /*use_default_args=*/false));
18066 if (explicit_targs == error_mark_node)
18067 goto fail;
18069 /* Substitute the explicit args into the function type. This is
18070 necessary so that, for instance, explicitly declared function
18071 arguments can match null pointed constants. If we were given
18072 an incomplete set of explicit args, we must not do semantic
18073 processing during substitution as we could create partial
18074 instantiations. */
18075 for (i = 0; i < len; i++)
18077 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18078 bool parameter_pack = false;
18079 tree targ = TREE_VEC_ELT (explicit_targs, i);
18081 /* Dig out the actual parm. */
18082 if (TREE_CODE (parm) == TYPE_DECL
18083 || TREE_CODE (parm) == TEMPLATE_DECL)
18085 parm = TREE_TYPE (parm);
18086 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18088 else if (TREE_CODE (parm) == PARM_DECL)
18090 parm = DECL_INITIAL (parm);
18091 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18094 if (!parameter_pack && targ == NULL_TREE)
18095 /* No explicit argument for this template parameter. */
18096 incomplete = true;
18098 if (parameter_pack && pack_deducible_p (parm, fn))
18100 /* Mark the argument pack as "incomplete". We could
18101 still deduce more arguments during unification.
18102 We remove this mark in type_unification_real. */
18103 if (targ)
18105 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18106 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18107 = ARGUMENT_PACK_ARGS (targ);
18110 /* We have some incomplete argument packs. */
18111 incomplete = true;
18115 TREE_VALUE (tinst) = explicit_targs;
18116 if (!push_tinst_level (tinst))
18118 excessive_deduction_depth = true;
18119 goto fail;
18121 processing_template_decl += incomplete;
18122 input_location = DECL_SOURCE_LOCATION (fn);
18123 /* Ignore any access checks; we'll see them again in
18124 instantiate_template and they might have the wrong
18125 access path at this point. */
18126 push_deferring_access_checks (dk_deferred);
18127 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18128 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18129 pop_deferring_access_checks ();
18130 input_location = loc;
18131 processing_template_decl -= incomplete;
18132 pop_tinst_level ();
18134 if (fntype == error_mark_node)
18135 goto fail;
18137 /* Place the explicitly specified arguments in TARGS. */
18138 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18139 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18140 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18143 /* Never do unification on the 'this' parameter. */
18144 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18146 if (return_type && strict == DEDUCE_CALL)
18148 /* We're deducing for a call to the result of a template conversion
18149 function. The parms we really want are in return_type. */
18150 if (POINTER_TYPE_P (return_type))
18151 return_type = TREE_TYPE (return_type);
18152 parms = TYPE_ARG_TYPES (return_type);
18154 else if (return_type)
18156 tree *new_args;
18158 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18159 new_args = XALLOCAVEC (tree, nargs + 1);
18160 new_args[0] = return_type;
18161 memcpy (new_args + 1, args, nargs * sizeof (tree));
18162 args = new_args;
18163 ++nargs;
18166 /* We allow incomplete unification without an error message here
18167 because the standard doesn't seem to explicitly prohibit it. Our
18168 callers must be ready to deal with unification failures in any
18169 event. */
18171 TREE_VALUE (tinst) = targs;
18172 /* If we aren't explaining yet, push tinst context so we can see where
18173 any errors (e.g. from class instantiations triggered by instantiation
18174 of default template arguments) come from. If we are explaining, this
18175 context is redundant. */
18176 if (!explain_p && !push_tinst_level (tinst))
18178 excessive_deduction_depth = true;
18179 goto fail;
18182 /* type_unification_real will pass back any access checks from default
18183 template argument substitution. */
18184 vec<deferred_access_check, va_gc> *checks;
18185 checks = NULL;
18187 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18188 full_targs, parms, args, nargs, /*subr=*/0,
18189 strict, flags, &checks, explain_p);
18190 if (!explain_p)
18191 pop_tinst_level ();
18192 if (!ok)
18193 goto fail;
18195 /* Now that we have bindings for all of the template arguments,
18196 ensure that the arguments deduced for the template template
18197 parameters have compatible template parameter lists. We cannot
18198 check this property before we have deduced all template
18199 arguments, because the template parameter types of a template
18200 template parameter might depend on prior template parameters
18201 deduced after the template template parameter. The following
18202 ill-formed example illustrates this issue:
18204 template<typename T, template<T> class C> void f(C<5>, T);
18206 template<int N> struct X {};
18208 void g() {
18209 f(X<5>(), 5l); // error: template argument deduction fails
18212 The template parameter list of 'C' depends on the template type
18213 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18214 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18215 time that we deduce 'C'. */
18216 if (!template_template_parm_bindings_ok_p
18217 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18219 unify_inconsistent_template_template_parameters (explain_p);
18220 goto fail;
18223 /* All is well so far. Now, check:
18225 [temp.deduct]
18227 When all template arguments have been deduced, all uses of
18228 template parameters in nondeduced contexts are replaced with
18229 the corresponding deduced argument values. If the
18230 substitution results in an invalid type, as described above,
18231 type deduction fails. */
18232 TREE_VALUE (tinst) = targs;
18233 if (!push_tinst_level (tinst))
18235 excessive_deduction_depth = true;
18236 goto fail;
18239 /* Also collect access checks from the instantiation. */
18240 reopen_deferring_access_checks (checks);
18242 decl = instantiate_template (fn, targs, complain);
18244 checks = get_deferred_access_checks ();
18245 pop_deferring_access_checks ();
18247 pop_tinst_level ();
18249 if (decl == error_mark_node)
18250 goto fail;
18252 /* Now perform any access checks encountered during substitution. */
18253 push_access_scope (decl);
18254 ok = perform_access_checks (checks, complain);
18255 pop_access_scope (decl);
18256 if (!ok)
18257 goto fail;
18259 /* If we're looking for an exact match, check that what we got
18260 is indeed an exact match. It might not be if some template
18261 parameters are used in non-deduced contexts. But don't check
18262 for an exact match if we have dependent template arguments;
18263 in that case we're doing partial ordering, and we already know
18264 that we have two candidates that will provide the actual type. */
18265 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18267 tree substed = TREE_TYPE (decl);
18268 unsigned int i;
18270 tree sarg
18271 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18272 if (return_type)
18273 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18274 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18275 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18277 unify_type_mismatch (explain_p, args[i],
18278 TREE_VALUE (sarg));
18279 goto fail;
18283 /* After doing deduction with the inherited constructor, actually return an
18284 instantiation of the inheriting constructor. */
18285 if (orig_fn != fn)
18286 decl = instantiate_template (orig_fn, targs, complain);
18288 r = decl;
18290 fail:
18291 --deduction_depth;
18292 if (excessive_deduction_depth)
18294 if (deduction_depth == 0)
18295 /* Reset once we're all the way out. */
18296 excessive_deduction_depth = false;
18299 /* We can't free this if a pending_template entry or last_error_tinst_level
18300 is pointing at it. */
18301 if (last_pending_template == old_last_pend
18302 && last_error_tinst_level == old_error_tinst)
18303 ggc_free (tinst);
18305 return r;
18308 /* Adjust types before performing type deduction, as described in
18309 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18310 sections are symmetric. PARM is the type of a function parameter
18311 or the return type of the conversion function. ARG is the type of
18312 the argument passed to the call, or the type of the value
18313 initialized with the result of the conversion function.
18314 ARG_EXPR is the original argument expression, which may be null. */
18316 static int
18317 maybe_adjust_types_for_deduction (unification_kind_t strict,
18318 tree* parm,
18319 tree* arg,
18320 tree arg_expr)
18322 int result = 0;
18324 switch (strict)
18326 case DEDUCE_CALL:
18327 break;
18329 case DEDUCE_CONV:
18330 /* Swap PARM and ARG throughout the remainder of this
18331 function; the handling is precisely symmetric since PARM
18332 will initialize ARG rather than vice versa. */
18333 std::swap (parm, arg);
18334 break;
18336 case DEDUCE_EXACT:
18337 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18338 too, but here handle it by stripping the reference from PARM
18339 rather than by adding it to ARG. */
18340 if (TREE_CODE (*parm) == REFERENCE_TYPE
18341 && TYPE_REF_IS_RVALUE (*parm)
18342 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18343 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18344 && TREE_CODE (*arg) == REFERENCE_TYPE
18345 && !TYPE_REF_IS_RVALUE (*arg))
18346 *parm = TREE_TYPE (*parm);
18347 /* Nothing else to do in this case. */
18348 return 0;
18350 default:
18351 gcc_unreachable ();
18354 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18356 /* [temp.deduct.call]
18358 If P is not a reference type:
18360 --If A is an array type, the pointer type produced by the
18361 array-to-pointer standard conversion (_conv.array_) is
18362 used in place of A for type deduction; otherwise,
18364 --If A is a function type, the pointer type produced by
18365 the function-to-pointer standard conversion
18366 (_conv.func_) is used in place of A for type deduction;
18367 otherwise,
18369 --If A is a cv-qualified type, the top level
18370 cv-qualifiers of A's type are ignored for type
18371 deduction. */
18372 if (TREE_CODE (*arg) == ARRAY_TYPE)
18373 *arg = build_pointer_type (TREE_TYPE (*arg));
18374 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18375 *arg = build_pointer_type (*arg);
18376 else
18377 *arg = TYPE_MAIN_VARIANT (*arg);
18380 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
18381 of the form T&&, where T is a template parameter, and the argument
18382 is an lvalue, T is deduced as A& */
18383 if (TREE_CODE (*parm) == REFERENCE_TYPE
18384 && TYPE_REF_IS_RVALUE (*parm)
18385 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18386 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18387 && (arg_expr ? lvalue_p (arg_expr)
18388 /* try_one_overload doesn't provide an arg_expr, but
18389 functions are always lvalues. */
18390 : TREE_CODE (*arg) == FUNCTION_TYPE))
18391 *arg = build_reference_type (*arg);
18393 /* [temp.deduct.call]
18395 If P is a cv-qualified type, the top level cv-qualifiers
18396 of P's type are ignored for type deduction. If P is a
18397 reference type, the type referred to by P is used for
18398 type deduction. */
18399 *parm = TYPE_MAIN_VARIANT (*parm);
18400 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18402 *parm = TREE_TYPE (*parm);
18403 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18406 /* DR 322. For conversion deduction, remove a reference type on parm
18407 too (which has been swapped into ARG). */
18408 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18409 *arg = TREE_TYPE (*arg);
18411 return result;
18414 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18415 template which does contain any deducible template parameters; check if
18416 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18417 unify_one_argument. */
18419 static int
18420 check_non_deducible_conversion (tree parm, tree arg, int strict,
18421 int flags, bool explain_p)
18423 tree type;
18425 if (!TYPE_P (arg))
18426 type = TREE_TYPE (arg);
18427 else
18428 type = arg;
18430 if (same_type_p (parm, type))
18431 return unify_success (explain_p);
18433 if (strict == DEDUCE_CONV)
18435 if (can_convert_arg (type, parm, NULL_TREE, flags,
18436 explain_p ? tf_warning_or_error : tf_none))
18437 return unify_success (explain_p);
18439 else if (strict != DEDUCE_EXACT)
18441 if (can_convert_arg (parm, type,
18442 TYPE_P (arg) ? NULL_TREE : arg,
18443 flags, explain_p ? tf_warning_or_error : tf_none))
18444 return unify_success (explain_p);
18447 if (strict == DEDUCE_EXACT)
18448 return unify_type_mismatch (explain_p, parm, arg);
18449 else
18450 return unify_arg_conversion (explain_p, parm, type, arg);
18453 static bool uses_deducible_template_parms (tree type);
18455 /* Returns true iff the expression EXPR is one from which a template
18456 argument can be deduced. In other words, if it's an undecorated
18457 use of a template non-type parameter. */
18459 static bool
18460 deducible_expression (tree expr)
18462 /* Strip implicit conversions. */
18463 while (CONVERT_EXPR_P (expr))
18464 expr = TREE_OPERAND (expr, 0);
18465 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18468 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18469 deducible way; that is, if it has a max value of <PARM> - 1. */
18471 static bool
18472 deducible_array_bound (tree domain)
18474 if (domain == NULL_TREE)
18475 return false;
18477 tree max = TYPE_MAX_VALUE (domain);
18478 if (TREE_CODE (max) != MINUS_EXPR)
18479 return false;
18481 return deducible_expression (TREE_OPERAND (max, 0));
18484 /* Returns true iff the template arguments ARGS use a template parameter
18485 in a deducible way. */
18487 static bool
18488 deducible_template_args (tree args)
18490 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18492 bool deducible;
18493 tree elt = TREE_VEC_ELT (args, i);
18494 if (ARGUMENT_PACK_P (elt))
18495 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18496 else
18498 if (PACK_EXPANSION_P (elt))
18499 elt = PACK_EXPANSION_PATTERN (elt);
18500 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18501 deducible = true;
18502 else if (TYPE_P (elt))
18503 deducible = uses_deducible_template_parms (elt);
18504 else
18505 deducible = deducible_expression (elt);
18507 if (deducible)
18508 return true;
18510 return false;
18513 /* Returns true iff TYPE contains any deducible references to template
18514 parameters, as per 14.8.2.5. */
18516 static bool
18517 uses_deducible_template_parms (tree type)
18519 if (PACK_EXPANSION_P (type))
18520 type = PACK_EXPANSION_PATTERN (type);
18522 /* T
18523 cv-list T
18524 TT<T>
18525 TT<i>
18526 TT<> */
18527 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18528 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18529 return true;
18531 /* T*
18533 T&& */
18534 if (POINTER_TYPE_P (type))
18535 return uses_deducible_template_parms (TREE_TYPE (type));
18537 /* T[integer-constant ]
18538 type [i] */
18539 if (TREE_CODE (type) == ARRAY_TYPE)
18540 return (uses_deducible_template_parms (TREE_TYPE (type))
18541 || deducible_array_bound (TYPE_DOMAIN (type)));
18543 /* T type ::*
18544 type T::*
18545 T T::*
18546 T (type ::*)()
18547 type (T::*)()
18548 type (type ::*)(T)
18549 type (T::*)(T)
18550 T (type ::*)(T)
18551 T (T::*)()
18552 T (T::*)(T) */
18553 if (TYPE_PTRMEM_P (type))
18554 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18555 || (uses_deducible_template_parms
18556 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18558 /* template-name <T> (where template-name refers to a class template)
18559 template-name <i> (where template-name refers to a class template) */
18560 if (CLASS_TYPE_P (type)
18561 && CLASSTYPE_TEMPLATE_INFO (type)
18562 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18563 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18564 (CLASSTYPE_TI_ARGS (type)));
18566 /* type (T)
18568 T(T) */
18569 if (TREE_CODE (type) == FUNCTION_TYPE
18570 || TREE_CODE (type) == METHOD_TYPE)
18572 if (uses_deducible_template_parms (TREE_TYPE (type)))
18573 return true;
18574 tree parm = TYPE_ARG_TYPES (type);
18575 if (TREE_CODE (type) == METHOD_TYPE)
18576 parm = TREE_CHAIN (parm);
18577 for (; parm; parm = TREE_CHAIN (parm))
18578 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18579 return true;
18582 return false;
18585 /* Subroutine of type_unification_real and unify_pack_expansion to
18586 handle unification of a single P/A pair. Parameters are as
18587 for those functions. */
18589 static int
18590 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18591 int subr, unification_kind_t strict,
18592 bool explain_p)
18594 tree arg_expr = NULL_TREE;
18595 int arg_strict;
18597 if (arg == error_mark_node || parm == error_mark_node)
18598 return unify_invalid (explain_p);
18599 if (arg == unknown_type_node)
18600 /* We can't deduce anything from this, but we might get all the
18601 template args from other function args. */
18602 return unify_success (explain_p);
18604 /* Implicit conversions (Clause 4) will be performed on a function
18605 argument to convert it to the type of the corresponding function
18606 parameter if the parameter type contains no template-parameters that
18607 participate in template argument deduction. */
18608 if (strict != DEDUCE_EXACT
18609 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18610 /* For function parameters with no deducible template parameters,
18611 just return. We'll check non-dependent conversions later. */
18612 return unify_success (explain_p);
18614 switch (strict)
18616 case DEDUCE_CALL:
18617 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18618 | UNIFY_ALLOW_MORE_CV_QUAL
18619 | UNIFY_ALLOW_DERIVED);
18620 break;
18622 case DEDUCE_CONV:
18623 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18624 break;
18626 case DEDUCE_EXACT:
18627 arg_strict = UNIFY_ALLOW_NONE;
18628 break;
18630 default:
18631 gcc_unreachable ();
18634 /* We only do these transformations if this is the top-level
18635 parameter_type_list in a call or declaration matching; in other
18636 situations (nested function declarators, template argument lists) we
18637 won't be comparing a type to an expression, and we don't do any type
18638 adjustments. */
18639 if (!subr)
18641 if (!TYPE_P (arg))
18643 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18644 if (type_unknown_p (arg))
18646 /* [temp.deduct.type] A template-argument can be
18647 deduced from a pointer to function or pointer
18648 to member function argument if the set of
18649 overloaded functions does not contain function
18650 templates and at most one of a set of
18651 overloaded functions provides a unique
18652 match. */
18654 if (resolve_overloaded_unification
18655 (tparms, targs, parm, arg, strict,
18656 arg_strict, explain_p))
18657 return unify_success (explain_p);
18658 return unify_overload_resolution_failure (explain_p, arg);
18661 arg_expr = arg;
18662 arg = unlowered_expr_type (arg);
18663 if (arg == error_mark_node)
18664 return unify_invalid (explain_p);
18667 arg_strict |=
18668 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18670 else
18671 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18672 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18673 return unify_template_argument_mismatch (explain_p, parm, arg);
18675 /* For deduction from an init-list we need the actual list. */
18676 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18677 arg = arg_expr;
18678 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18681 /* for_each_template_parm callback that always returns 0. */
18683 static int
18684 zero_r (tree, void *)
18686 return 0;
18689 /* for_each_template_parm any_fn callback to handle deduction of a template
18690 type argument from the type of an array bound. */
18692 static int
18693 array_deduction_r (tree t, void *data)
18695 tree_pair_p d = (tree_pair_p)data;
18696 tree &tparms = d->purpose;
18697 tree &targs = d->value;
18699 if (TREE_CODE (t) == ARRAY_TYPE)
18700 if (tree dom = TYPE_DOMAIN (t))
18701 if (tree max = TYPE_MAX_VALUE (dom))
18703 if (TREE_CODE (max) == MINUS_EXPR)
18704 max = TREE_OPERAND (max, 0);
18705 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
18706 unify (tparms, targs, TREE_TYPE (max), size_type_node,
18707 UNIFY_ALLOW_NONE, /*explain*/false);
18710 /* Keep walking. */
18711 return 0;
18714 /* Try to deduce any not-yet-deduced template type arguments from the type of
18715 an array bound. This is handled separately from unify because 14.8.2.5 says
18716 "The type of a type parameter is only deduced from an array bound if it is
18717 not otherwise deduced." */
18719 static void
18720 try_array_deduction (tree tparms, tree targs, tree parm)
18722 tree_pair_s data = { tparms, targs };
18723 hash_set<tree> visited;
18724 for_each_template_parm (parm, zero_r, &data, &visited,
18725 /*nondeduced*/false, array_deduction_r);
18728 /* Most parms like fn_type_unification.
18730 If SUBR is 1, we're being called recursively (to unify the
18731 arguments of a function or method parameter of a function
18732 template).
18734 CHECKS is a pointer to a vector of access checks encountered while
18735 substituting default template arguments. */
18737 static int
18738 type_unification_real (tree tparms,
18739 tree full_targs,
18740 tree xparms,
18741 const tree *xargs,
18742 unsigned int xnargs,
18743 int subr,
18744 unification_kind_t strict,
18745 int flags,
18746 vec<deferred_access_check, va_gc> **checks,
18747 bool explain_p)
18749 tree parm, arg;
18750 int i;
18751 int ntparms = TREE_VEC_LENGTH (tparms);
18752 int saw_undeduced = 0;
18753 tree parms;
18754 const tree *args;
18755 unsigned int nargs;
18756 unsigned int ia;
18758 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
18759 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
18760 gcc_assert (ntparms > 0);
18762 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
18764 /* Reset the number of non-defaulted template arguments contained
18765 in TARGS. */
18766 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
18768 again:
18769 parms = xparms;
18770 args = xargs;
18771 nargs = xnargs;
18773 ia = 0;
18774 while (parms && parms != void_list_node
18775 && ia < nargs)
18777 parm = TREE_VALUE (parms);
18779 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18780 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
18781 /* For a function parameter pack that occurs at the end of the
18782 parameter-declaration-list, the type A of each remaining
18783 argument of the call is compared with the type P of the
18784 declarator-id of the function parameter pack. */
18785 break;
18787 parms = TREE_CHAIN (parms);
18789 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18790 /* For a function parameter pack that does not occur at the
18791 end of the parameter-declaration-list, the type of the
18792 parameter pack is a non-deduced context. */
18793 continue;
18795 arg = args[ia];
18796 ++ia;
18798 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
18799 explain_p))
18800 return 1;
18803 if (parms
18804 && parms != void_list_node
18805 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
18807 /* Unify the remaining arguments with the pack expansion type. */
18808 tree argvec;
18809 tree parmvec = make_tree_vec (1);
18811 /* Allocate a TREE_VEC and copy in all of the arguments */
18812 argvec = make_tree_vec (nargs - ia);
18813 for (i = 0; ia < nargs; ++ia, ++i)
18814 TREE_VEC_ELT (argvec, i) = args[ia];
18816 /* Copy the parameter into parmvec. */
18817 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
18818 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
18819 /*subr=*/subr, explain_p))
18820 return 1;
18822 /* Advance to the end of the list of parameters. */
18823 parms = TREE_CHAIN (parms);
18826 /* Fail if we've reached the end of the parm list, and more args
18827 are present, and the parm list isn't variadic. */
18828 if (ia < nargs && parms == void_list_node)
18829 return unify_too_many_arguments (explain_p, nargs, ia);
18830 /* Fail if parms are left and they don't have default values and
18831 they aren't all deduced as empty packs (c++/57397). This is
18832 consistent with sufficient_parms_p. */
18833 if (parms && parms != void_list_node
18834 && TREE_PURPOSE (parms) == NULL_TREE)
18836 unsigned int count = nargs;
18837 tree p = parms;
18838 bool type_pack_p;
18841 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
18842 if (!type_pack_p)
18843 count++;
18844 p = TREE_CHAIN (p);
18846 while (p && p != void_list_node);
18847 if (count != nargs)
18848 return unify_too_few_arguments (explain_p, ia, count,
18849 type_pack_p);
18852 if (!subr)
18854 tsubst_flags_t complain = (explain_p
18855 ? tf_warning_or_error
18856 : tf_none);
18857 bool tried_array_deduction = (cxx_dialect < cxx1z);
18859 for (i = 0; i < ntparms; i++)
18861 tree targ = TREE_VEC_ELT (targs, i);
18862 tree tparm = TREE_VEC_ELT (tparms, i);
18864 /* Clear the "incomplete" flags on all argument packs now so that
18865 substituting them into later default arguments works. */
18866 if (targ && ARGUMENT_PACK_P (targ))
18868 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
18869 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
18872 if (targ || tparm == error_mark_node)
18873 continue;
18874 tparm = TREE_VALUE (tparm);
18876 if (TREE_CODE (tparm) == TYPE_DECL
18877 && !tried_array_deduction)
18879 try_array_deduction (tparms, targs, xparms);
18880 tried_array_deduction = true;
18881 if (TREE_VEC_ELT (targs, i))
18882 continue;
18885 /* If this is an undeduced nontype parameter that depends on
18886 a type parameter, try another pass; its type may have been
18887 deduced from a later argument than the one from which
18888 this parameter can be deduced. */
18889 if (TREE_CODE (tparm) == PARM_DECL
18890 && uses_template_parms (TREE_TYPE (tparm))
18891 && saw_undeduced < 2)
18893 saw_undeduced = 1;
18894 continue;
18897 /* Core issue #226 (C++0x) [temp.deduct]:
18899 If a template argument has not been deduced, its
18900 default template argument, if any, is used.
18902 When we are in C++98 mode, TREE_PURPOSE will either
18903 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18904 to explicitly check cxx_dialect here. */
18905 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
18906 /* OK, there is a default argument. Wait until after the
18907 conversion check to do substitution. */
18908 continue;
18910 /* If the type parameter is a parameter pack, then it will
18911 be deduced to an empty parameter pack. */
18912 if (template_parameter_pack_p (tparm))
18914 tree arg;
18916 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
18918 arg = make_node (NONTYPE_ARGUMENT_PACK);
18919 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
18920 TREE_CONSTANT (arg) = 1;
18922 else
18923 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
18925 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
18927 TREE_VEC_ELT (targs, i) = arg;
18928 continue;
18931 return unify_parameter_deduction_failure (explain_p, tparm);
18934 /* DR 1391: All parameters have args, now check non-dependent parms for
18935 convertibility. */
18936 if (saw_undeduced < 2)
18937 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
18938 parms && parms != void_list_node && ia < nargs; )
18940 parm = TREE_VALUE (parms);
18942 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18943 && (!TREE_CHAIN (parms)
18944 || TREE_CHAIN (parms) == void_list_node))
18945 /* For a function parameter pack that occurs at the end of the
18946 parameter-declaration-list, the type A of each remaining
18947 argument of the call is compared with the type P of the
18948 declarator-id of the function parameter pack. */
18949 break;
18951 parms = TREE_CHAIN (parms);
18953 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18954 /* For a function parameter pack that does not occur at the
18955 end of the parameter-declaration-list, the type of the
18956 parameter pack is a non-deduced context. */
18957 continue;
18959 arg = args[ia];
18960 ++ia;
18962 if (uses_template_parms (parm))
18963 continue;
18964 if (check_non_deducible_conversion (parm, arg, strict, flags,
18965 explain_p))
18966 return 1;
18969 /* Now substitute into the default template arguments. */
18970 for (i = 0; i < ntparms; i++)
18972 tree targ = TREE_VEC_ELT (targs, i);
18973 tree tparm = TREE_VEC_ELT (tparms, i);
18975 if (targ || tparm == error_mark_node)
18976 continue;
18977 tree parm = TREE_VALUE (tparm);
18979 if (TREE_CODE (parm) == PARM_DECL
18980 && uses_template_parms (TREE_TYPE (parm))
18981 && saw_undeduced < 2)
18982 continue;
18984 tree arg = TREE_PURPOSE (tparm);
18985 reopen_deferring_access_checks (*checks);
18986 location_t save_loc = input_location;
18987 if (DECL_P (parm))
18988 input_location = DECL_SOURCE_LOCATION (parm);
18989 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
18990 arg = convert_template_argument (parm, arg, full_targs, complain,
18991 i, NULL_TREE);
18992 input_location = save_loc;
18993 *checks = get_deferred_access_checks ();
18994 pop_deferring_access_checks ();
18995 if (arg == error_mark_node)
18996 return 1;
18997 else
18999 TREE_VEC_ELT (targs, i) = arg;
19000 /* The position of the first default template argument,
19001 is also the number of non-defaulted arguments in TARGS.
19002 Record that. */
19003 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19004 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19005 continue;
19009 if (saw_undeduced++ == 1)
19010 goto again;
19013 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19014 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19016 return unify_success (explain_p);
19019 /* Subroutine of type_unification_real. Args are like the variables
19020 at the call site. ARG is an overloaded function (or template-id);
19021 we try deducing template args from each of the overloads, and if
19022 only one succeeds, we go with that. Modifies TARGS and returns
19023 true on success. */
19025 static bool
19026 resolve_overloaded_unification (tree tparms,
19027 tree targs,
19028 tree parm,
19029 tree arg,
19030 unification_kind_t strict,
19031 int sub_strict,
19032 bool explain_p)
19034 tree tempargs = copy_node (targs);
19035 int good = 0;
19036 tree goodfn = NULL_TREE;
19037 bool addr_p;
19039 if (TREE_CODE (arg) == ADDR_EXPR)
19041 arg = TREE_OPERAND (arg, 0);
19042 addr_p = true;
19044 else
19045 addr_p = false;
19047 if (TREE_CODE (arg) == COMPONENT_REF)
19048 /* Handle `&x' where `x' is some static or non-static member
19049 function name. */
19050 arg = TREE_OPERAND (arg, 1);
19052 if (TREE_CODE (arg) == OFFSET_REF)
19053 arg = TREE_OPERAND (arg, 1);
19055 /* Strip baselink information. */
19056 if (BASELINK_P (arg))
19057 arg = BASELINK_FUNCTIONS (arg);
19059 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19061 /* If we got some explicit template args, we need to plug them into
19062 the affected templates before we try to unify, in case the
19063 explicit args will completely resolve the templates in question. */
19065 int ok = 0;
19066 tree expl_subargs = TREE_OPERAND (arg, 1);
19067 arg = TREE_OPERAND (arg, 0);
19069 for (; arg; arg = OVL_NEXT (arg))
19071 tree fn = OVL_CURRENT (arg);
19072 tree subargs, elem;
19074 if (TREE_CODE (fn) != TEMPLATE_DECL)
19075 continue;
19077 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19078 expl_subargs, NULL_TREE, tf_none,
19079 /*require_all_args=*/true,
19080 /*use_default_args=*/true);
19081 if (subargs != error_mark_node
19082 && !any_dependent_template_arguments_p (subargs))
19084 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19085 if (try_one_overload (tparms, targs, tempargs, parm,
19086 elem, strict, sub_strict, addr_p, explain_p)
19087 && (!goodfn || !same_type_p (goodfn, elem)))
19089 goodfn = elem;
19090 ++good;
19093 else if (subargs)
19094 ++ok;
19096 /* If no templates (or more than one) are fully resolved by the
19097 explicit arguments, this template-id is a non-deduced context; it
19098 could still be OK if we deduce all template arguments for the
19099 enclosing call through other arguments. */
19100 if (good != 1)
19101 good = ok;
19103 else if (TREE_CODE (arg) != OVERLOAD
19104 && TREE_CODE (arg) != FUNCTION_DECL)
19105 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19106 -- but the deduction does not succeed because the expression is
19107 not just the function on its own. */
19108 return false;
19109 else
19110 for (; arg; arg = OVL_NEXT (arg))
19111 if (try_one_overload (tparms, targs, tempargs, parm,
19112 TREE_TYPE (OVL_CURRENT (arg)),
19113 strict, sub_strict, addr_p, explain_p)
19114 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
19116 goodfn = OVL_CURRENT (arg);
19117 ++good;
19120 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19121 to function or pointer to member function argument if the set of
19122 overloaded functions does not contain function templates and at most
19123 one of a set of overloaded functions provides a unique match.
19125 So if we found multiple possibilities, we return success but don't
19126 deduce anything. */
19128 if (good == 1)
19130 int i = TREE_VEC_LENGTH (targs);
19131 for (; i--; )
19132 if (TREE_VEC_ELT (tempargs, i))
19134 tree old = TREE_VEC_ELT (targs, i);
19135 tree new_ = TREE_VEC_ELT (tempargs, i);
19136 if (new_ && old && ARGUMENT_PACK_P (old)
19137 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19138 /* Don't forget explicit template arguments in a pack. */
19139 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19140 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19141 TREE_VEC_ELT (targs, i) = new_;
19144 if (good)
19145 return true;
19147 return false;
19150 /* Core DR 115: In contexts where deduction is done and fails, or in
19151 contexts where deduction is not done, if a template argument list is
19152 specified and it, along with any default template arguments, identifies
19153 a single function template specialization, then the template-id is an
19154 lvalue for the function template specialization. */
19156 tree
19157 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19159 tree expr, offset, baselink;
19160 bool addr;
19162 if (!type_unknown_p (orig_expr))
19163 return orig_expr;
19165 expr = orig_expr;
19166 addr = false;
19167 offset = NULL_TREE;
19168 baselink = NULL_TREE;
19170 if (TREE_CODE (expr) == ADDR_EXPR)
19172 expr = TREE_OPERAND (expr, 0);
19173 addr = true;
19175 if (TREE_CODE (expr) == OFFSET_REF)
19177 offset = expr;
19178 expr = TREE_OPERAND (expr, 1);
19180 if (BASELINK_P (expr))
19182 baselink = expr;
19183 expr = BASELINK_FUNCTIONS (expr);
19186 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19188 int good = 0;
19189 tree goodfn = NULL_TREE;
19191 /* If we got some explicit template args, we need to plug them into
19192 the affected templates before we try to unify, in case the
19193 explicit args will completely resolve the templates in question. */
19195 tree expl_subargs = TREE_OPERAND (expr, 1);
19196 tree arg = TREE_OPERAND (expr, 0);
19197 tree badfn = NULL_TREE;
19198 tree badargs = NULL_TREE;
19200 for (; arg; arg = OVL_NEXT (arg))
19202 tree fn = OVL_CURRENT (arg);
19203 tree subargs, elem;
19205 if (TREE_CODE (fn) != TEMPLATE_DECL)
19206 continue;
19208 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19209 expl_subargs, NULL_TREE, tf_none,
19210 /*require_all_args=*/true,
19211 /*use_default_args=*/true);
19212 if (subargs != error_mark_node
19213 && !any_dependent_template_arguments_p (subargs))
19215 elem = instantiate_template (fn, subargs, tf_none);
19216 if (elem == error_mark_node)
19218 badfn = fn;
19219 badargs = subargs;
19221 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19223 goodfn = elem;
19224 ++good;
19228 if (good == 1)
19230 mark_used (goodfn);
19231 expr = goodfn;
19232 if (baselink)
19233 expr = build_baselink (BASELINK_BINFO (baselink),
19234 BASELINK_ACCESS_BINFO (baselink),
19235 expr, BASELINK_OPTYPE (baselink));
19236 if (offset)
19238 tree base
19239 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19240 expr = build_offset_ref (base, expr, addr, complain);
19242 if (addr)
19243 expr = cp_build_addr_expr (expr, complain);
19244 return expr;
19246 else if (good == 0 && badargs && (complain & tf_error))
19247 /* There were no good options and at least one bad one, so let the
19248 user know what the problem is. */
19249 instantiate_template (badfn, badargs, complain);
19251 return orig_expr;
19254 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19255 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19256 different overloads deduce different arguments for a given parm.
19257 ADDR_P is true if the expression for which deduction is being
19258 performed was of the form "& fn" rather than simply "fn".
19260 Returns 1 on success. */
19262 static int
19263 try_one_overload (tree tparms,
19264 tree orig_targs,
19265 tree targs,
19266 tree parm,
19267 tree arg,
19268 unification_kind_t strict,
19269 int sub_strict,
19270 bool addr_p,
19271 bool explain_p)
19273 int nargs;
19274 tree tempargs;
19275 int i;
19277 if (arg == error_mark_node)
19278 return 0;
19280 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19281 to function or pointer to member function argument if the set of
19282 overloaded functions does not contain function templates and at most
19283 one of a set of overloaded functions provides a unique match.
19285 So if this is a template, just return success. */
19287 if (uses_template_parms (arg))
19288 return 1;
19290 if (TREE_CODE (arg) == METHOD_TYPE)
19291 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19292 else if (addr_p)
19293 arg = build_pointer_type (arg);
19295 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19297 /* We don't copy orig_targs for this because if we have already deduced
19298 some template args from previous args, unify would complain when we
19299 try to deduce a template parameter for the same argument, even though
19300 there isn't really a conflict. */
19301 nargs = TREE_VEC_LENGTH (targs);
19302 tempargs = make_tree_vec (nargs);
19304 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19305 return 0;
19307 /* First make sure we didn't deduce anything that conflicts with
19308 explicitly specified args. */
19309 for (i = nargs; i--; )
19311 tree elt = TREE_VEC_ELT (tempargs, i);
19312 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19314 if (!elt)
19315 /*NOP*/;
19316 else if (uses_template_parms (elt))
19317 /* Since we're unifying against ourselves, we will fill in
19318 template args used in the function parm list with our own
19319 template parms. Discard them. */
19320 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19321 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19323 /* Check that the argument at each index of the deduced argument pack
19324 is equivalent to the corresponding explicitly specified argument.
19325 We may have deduced more arguments than were explicitly specified,
19326 and that's OK. */
19327 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
19328 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
19329 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
19331 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19332 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19334 if (TREE_VEC_LENGTH (deduced_pack)
19335 < TREE_VEC_LENGTH (explicit_pack))
19336 return 0;
19338 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19339 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19340 TREE_VEC_ELT (deduced_pack, j)))
19341 return 0;
19343 else if (oldelt && !template_args_equal (oldelt, elt))
19344 return 0;
19347 for (i = nargs; i--; )
19349 tree elt = TREE_VEC_ELT (tempargs, i);
19351 if (elt)
19352 TREE_VEC_ELT (targs, i) = elt;
19355 return 1;
19358 /* PARM is a template class (perhaps with unbound template
19359 parameters). ARG is a fully instantiated type. If ARG can be
19360 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19361 TARGS are as for unify. */
19363 static tree
19364 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19365 bool explain_p)
19367 tree copy_of_targs;
19369 if (!CLASSTYPE_TEMPLATE_INFO (arg)
19370 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19371 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
19372 return NULL_TREE;
19374 /* We need to make a new template argument vector for the call to
19375 unify. If we used TARGS, we'd clutter it up with the result of
19376 the attempted unification, even if this class didn't work out.
19377 We also don't want to commit ourselves to all the unifications
19378 we've already done, since unification is supposed to be done on
19379 an argument-by-argument basis. In other words, consider the
19380 following pathological case:
19382 template <int I, int J, int K>
19383 struct S {};
19385 template <int I, int J>
19386 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19388 template <int I, int J, int K>
19389 void f(S<I, J, K>, S<I, I, I>);
19391 void g() {
19392 S<0, 0, 0> s0;
19393 S<0, 1, 2> s2;
19395 f(s0, s2);
19398 Now, by the time we consider the unification involving `s2', we
19399 already know that we must have `f<0, 0, 0>'. But, even though
19400 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19401 because there are two ways to unify base classes of S<0, 1, 2>
19402 with S<I, I, I>. If we kept the already deduced knowledge, we
19403 would reject the possibility I=1. */
19404 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19406 /* If unification failed, we're done. */
19407 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19408 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19409 return NULL_TREE;
19411 return arg;
19414 /* Given a template type PARM and a class type ARG, find the unique
19415 base type in ARG that is an instance of PARM. We do not examine
19416 ARG itself; only its base-classes. If there is not exactly one
19417 appropriate base class, return NULL_TREE. PARM may be the type of
19418 a partial specialization, as well as a plain template type. Used
19419 by unify. */
19421 static enum template_base_result
19422 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19423 bool explain_p, tree *result)
19425 tree rval = NULL_TREE;
19426 tree binfo;
19428 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19430 binfo = TYPE_BINFO (complete_type (arg));
19431 if (!binfo)
19433 /* The type could not be completed. */
19434 *result = NULL_TREE;
19435 return tbr_incomplete_type;
19438 /* Walk in inheritance graph order. The search order is not
19439 important, and this avoids multiple walks of virtual bases. */
19440 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19442 tree r = try_class_unification (tparms, targs, parm,
19443 BINFO_TYPE (binfo), explain_p);
19445 if (r)
19447 /* If there is more than one satisfactory baseclass, then:
19449 [temp.deduct.call]
19451 If they yield more than one possible deduced A, the type
19452 deduction fails.
19454 applies. */
19455 if (rval && !same_type_p (r, rval))
19457 *result = NULL_TREE;
19458 return tbr_ambiguous_baseclass;
19461 rval = r;
19465 *result = rval;
19466 return tbr_success;
19469 /* Returns the level of DECL, which declares a template parameter. */
19471 static int
19472 template_decl_level (tree decl)
19474 switch (TREE_CODE (decl))
19476 case TYPE_DECL:
19477 case TEMPLATE_DECL:
19478 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19480 case PARM_DECL:
19481 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19483 default:
19484 gcc_unreachable ();
19486 return 0;
19489 /* Decide whether ARG can be unified with PARM, considering only the
19490 cv-qualifiers of each type, given STRICT as documented for unify.
19491 Returns nonzero iff the unification is OK on that basis. */
19493 static int
19494 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19496 int arg_quals = cp_type_quals (arg);
19497 int parm_quals = cp_type_quals (parm);
19499 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19500 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19502 /* Although a CVR qualifier is ignored when being applied to a
19503 substituted template parameter ([8.3.2]/1 for example), that
19504 does not allow us to unify "const T" with "int&" because both
19505 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19506 It is ok when we're allowing additional CV qualifiers
19507 at the outer level [14.8.2.1]/3,1st bullet. */
19508 if ((TREE_CODE (arg) == REFERENCE_TYPE
19509 || TREE_CODE (arg) == FUNCTION_TYPE
19510 || TREE_CODE (arg) == METHOD_TYPE)
19511 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19512 return 0;
19514 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19515 && (parm_quals & TYPE_QUAL_RESTRICT))
19516 return 0;
19519 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19520 && (arg_quals & parm_quals) != parm_quals)
19521 return 0;
19523 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19524 && (parm_quals & arg_quals) != arg_quals)
19525 return 0;
19527 return 1;
19530 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19531 void
19532 template_parm_level_and_index (tree parm, int* level, int* index)
19534 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19535 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19536 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19538 *index = TEMPLATE_TYPE_IDX (parm);
19539 *level = TEMPLATE_TYPE_LEVEL (parm);
19541 else
19543 *index = TEMPLATE_PARM_IDX (parm);
19544 *level = TEMPLATE_PARM_LEVEL (parm);
19548 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19549 do { \
19550 if (unify (TP, TA, P, A, S, EP)) \
19551 return 1; \
19552 } while (0)
19554 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19555 expansion at the end of PACKED_PARMS. Returns 0 if the type
19556 deduction succeeds, 1 otherwise. STRICT is the same as in
19557 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19558 function call argument list. We'll need to adjust the arguments to make them
19559 types. SUBR tells us if this is from a recursive call to
19560 type_unification_real, or for comparing two template argument
19561 lists. */
19563 static int
19564 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19565 tree packed_args, unification_kind_t strict,
19566 bool subr, bool explain_p)
19568 tree parm
19569 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19570 tree pattern = PACK_EXPANSION_PATTERN (parm);
19571 tree pack, packs = NULL_TREE;
19572 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19574 packed_args = expand_template_argument_pack (packed_args);
19576 int len = TREE_VEC_LENGTH (packed_args);
19578 /* Determine the parameter packs we will be deducing from the
19579 pattern, and record their current deductions. */
19580 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19581 pack; pack = TREE_CHAIN (pack))
19583 tree parm_pack = TREE_VALUE (pack);
19584 int idx, level;
19586 /* Determine the index and level of this parameter pack. */
19587 template_parm_level_and_index (parm_pack, &level, &idx);
19589 /* Keep track of the parameter packs and their corresponding
19590 argument packs. */
19591 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19592 TREE_TYPE (packs) = make_tree_vec (len - start);
19595 /* Loop through all of the arguments that have not yet been
19596 unified and unify each with the pattern. */
19597 for (i = start; i < len; i++)
19599 tree parm;
19600 bool any_explicit = false;
19601 tree arg = TREE_VEC_ELT (packed_args, i);
19603 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19604 or the element of its argument pack at the current index if
19605 this argument was explicitly specified. */
19606 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19608 int idx, level;
19609 tree arg, pargs;
19610 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19612 arg = NULL_TREE;
19613 if (TREE_VALUE (pack)
19614 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19615 && (i - start < TREE_VEC_LENGTH (pargs)))
19617 any_explicit = true;
19618 arg = TREE_VEC_ELT (pargs, i - start);
19620 TMPL_ARG (targs, level, idx) = arg;
19623 /* If we had explicit template arguments, substitute them into the
19624 pattern before deduction. */
19625 if (any_explicit)
19627 /* Some arguments might still be unspecified or dependent. */
19628 bool dependent;
19629 ++processing_template_decl;
19630 dependent = any_dependent_template_arguments_p (targs);
19631 if (!dependent)
19632 --processing_template_decl;
19633 parm = tsubst (pattern, targs,
19634 explain_p ? tf_warning_or_error : tf_none,
19635 NULL_TREE);
19636 if (dependent)
19637 --processing_template_decl;
19638 if (parm == error_mark_node)
19639 return 1;
19641 else
19642 parm = pattern;
19644 /* Unify the pattern with the current argument. */
19645 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19646 explain_p))
19647 return 1;
19649 /* For each parameter pack, collect the deduced value. */
19650 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19652 int idx, level;
19653 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19655 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19656 TMPL_ARG (targs, level, idx);
19660 /* Verify that the results of unification with the parameter packs
19661 produce results consistent with what we've seen before, and make
19662 the deduced argument packs available. */
19663 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19665 tree old_pack = TREE_VALUE (pack);
19666 tree new_args = TREE_TYPE (pack);
19667 int i, len = TREE_VEC_LENGTH (new_args);
19668 int idx, level;
19669 bool nondeduced_p = false;
19671 /* By default keep the original deduced argument pack.
19672 If necessary, more specific code is going to update the
19673 resulting deduced argument later down in this function. */
19674 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19675 TMPL_ARG (targs, level, idx) = old_pack;
19677 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19678 actually deduce anything. */
19679 for (i = 0; i < len && !nondeduced_p; ++i)
19680 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
19681 nondeduced_p = true;
19682 if (nondeduced_p)
19683 continue;
19685 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
19687 /* If we had fewer function args than explicit template args,
19688 just use the explicits. */
19689 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19690 int explicit_len = TREE_VEC_LENGTH (explicit_args);
19691 if (len < explicit_len)
19692 new_args = explicit_args;
19695 if (!old_pack)
19697 tree result;
19698 /* Build the deduced *_ARGUMENT_PACK. */
19699 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
19701 result = make_node (NONTYPE_ARGUMENT_PACK);
19702 TREE_TYPE (result) =
19703 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
19704 TREE_CONSTANT (result) = 1;
19706 else
19707 result = cxx_make_type (TYPE_ARGUMENT_PACK);
19709 SET_ARGUMENT_PACK_ARGS (result, new_args);
19711 /* Note the deduced argument packs for this parameter
19712 pack. */
19713 TMPL_ARG (targs, level, idx) = result;
19715 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
19716 && (ARGUMENT_PACK_ARGS (old_pack)
19717 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
19719 /* We only had the explicitly-provided arguments before, but
19720 now we have a complete set of arguments. */
19721 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19723 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
19724 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
19725 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
19727 else
19729 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
19730 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
19732 if (!comp_template_args (old_args, new_args,
19733 &bad_old_arg, &bad_new_arg))
19734 /* Inconsistent unification of this parameter pack. */
19735 return unify_parameter_pack_inconsistent (explain_p,
19736 bad_old_arg,
19737 bad_new_arg);
19741 return unify_success (explain_p);
19744 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19745 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19746 parameters and return value are as for unify. */
19748 static int
19749 unify_array_domain (tree tparms, tree targs,
19750 tree parm_dom, tree arg_dom,
19751 bool explain_p)
19753 tree parm_max;
19754 tree arg_max;
19755 bool parm_cst;
19756 bool arg_cst;
19758 /* Our representation of array types uses "N - 1" as the
19759 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19760 not an integer constant. We cannot unify arbitrarily
19761 complex expressions, so we eliminate the MINUS_EXPRs
19762 here. */
19763 parm_max = TYPE_MAX_VALUE (parm_dom);
19764 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
19765 if (!parm_cst)
19767 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
19768 parm_max = TREE_OPERAND (parm_max, 0);
19770 arg_max = TYPE_MAX_VALUE (arg_dom);
19771 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
19772 if (!arg_cst)
19774 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19775 trying to unify the type of a variable with the type
19776 of a template parameter. For example:
19778 template <unsigned int N>
19779 void f (char (&) [N]);
19780 int g();
19781 void h(int i) {
19782 char a[g(i)];
19783 f(a);
19786 Here, the type of the ARG will be "int [g(i)]", and
19787 may be a SAVE_EXPR, etc. */
19788 if (TREE_CODE (arg_max) != MINUS_EXPR)
19789 return unify_vla_arg (explain_p, arg_dom);
19790 arg_max = TREE_OPERAND (arg_max, 0);
19793 /* If only one of the bounds used a MINUS_EXPR, compensate
19794 by adding one to the other bound. */
19795 if (parm_cst && !arg_cst)
19796 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
19797 integer_type_node,
19798 parm_max,
19799 integer_one_node);
19800 else if (arg_cst && !parm_cst)
19801 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
19802 integer_type_node,
19803 arg_max,
19804 integer_one_node);
19806 return unify (tparms, targs, parm_max, arg_max,
19807 UNIFY_ALLOW_INTEGER, explain_p);
19810 /* Deduce the value of template parameters. TPARMS is the (innermost)
19811 set of template parameters to a template. TARGS is the bindings
19812 for those template parameters, as determined thus far; TARGS may
19813 include template arguments for outer levels of template parameters
19814 as well. PARM is a parameter to a template function, or a
19815 subcomponent of that parameter; ARG is the corresponding argument.
19816 This function attempts to match PARM with ARG in a manner
19817 consistent with the existing assignments in TARGS. If more values
19818 are deduced, then TARGS is updated.
19820 Returns 0 if the type deduction succeeds, 1 otherwise. The
19821 parameter STRICT is a bitwise or of the following flags:
19823 UNIFY_ALLOW_NONE:
19824 Require an exact match between PARM and ARG.
19825 UNIFY_ALLOW_MORE_CV_QUAL:
19826 Allow the deduced ARG to be more cv-qualified (by qualification
19827 conversion) than ARG.
19828 UNIFY_ALLOW_LESS_CV_QUAL:
19829 Allow the deduced ARG to be less cv-qualified than ARG.
19830 UNIFY_ALLOW_DERIVED:
19831 Allow the deduced ARG to be a template base class of ARG,
19832 or a pointer to a template base class of the type pointed to by
19833 ARG.
19834 UNIFY_ALLOW_INTEGER:
19835 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19836 case for more information.
19837 UNIFY_ALLOW_OUTER_LEVEL:
19838 This is the outermost level of a deduction. Used to determine validity
19839 of qualification conversions. A valid qualification conversion must
19840 have const qualified pointers leading up to the inner type which
19841 requires additional CV quals, except at the outer level, where const
19842 is not required [conv.qual]. It would be normal to set this flag in
19843 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19844 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19845 This is the outermost level of a deduction, and PARM can be more CV
19846 qualified at this point.
19847 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19848 This is the outermost level of a deduction, and PARM can be less CV
19849 qualified at this point. */
19851 static int
19852 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
19853 bool explain_p)
19855 int idx;
19856 tree targ;
19857 tree tparm;
19858 int strict_in = strict;
19859 tsubst_flags_t complain = (explain_p
19860 ? tf_warning_or_error
19861 : tf_none);
19863 /* I don't think this will do the right thing with respect to types.
19864 But the only case I've seen it in so far has been array bounds, where
19865 signedness is the only information lost, and I think that will be
19866 okay. */
19867 while (CONVERT_EXPR_P (parm))
19868 parm = TREE_OPERAND (parm, 0);
19870 if (arg == error_mark_node)
19871 return unify_invalid (explain_p);
19872 if (arg == unknown_type_node
19873 || arg == init_list_type_node)
19874 /* We can't deduce anything from this, but we might get all the
19875 template args from other function args. */
19876 return unify_success (explain_p);
19878 /* If PARM uses template parameters, then we can't bail out here,
19879 even if ARG == PARM, since we won't record unifications for the
19880 template parameters. We might need them if we're trying to
19881 figure out which of two things is more specialized. */
19882 if (arg == parm && !uses_template_parms (parm))
19883 return unify_success (explain_p);
19885 /* Handle init lists early, so the rest of the function can assume
19886 we're dealing with a type. */
19887 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
19889 tree elt, elttype;
19890 unsigned i;
19891 tree orig_parm = parm;
19893 /* Replace T with std::initializer_list<T> for deduction. */
19894 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19895 && flag_deduce_init_list)
19896 parm = listify (parm);
19898 if (!is_std_init_list (parm)
19899 && TREE_CODE (parm) != ARRAY_TYPE)
19900 /* We can only deduce from an initializer list argument if the
19901 parameter is std::initializer_list or an array; otherwise this
19902 is a non-deduced context. */
19903 return unify_success (explain_p);
19905 if (TREE_CODE (parm) == ARRAY_TYPE)
19906 elttype = TREE_TYPE (parm);
19907 else
19909 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
19910 /* Deduction is defined in terms of a single type, so just punt
19911 on the (bizarre) std::initializer_list<T...>. */
19912 if (PACK_EXPANSION_P (elttype))
19913 return unify_success (explain_p);
19916 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
19918 int elt_strict = strict;
19920 if (elt == error_mark_node)
19921 return unify_invalid (explain_p);
19923 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
19925 tree type = TREE_TYPE (elt);
19926 if (type == error_mark_node)
19927 return unify_invalid (explain_p);
19928 /* It should only be possible to get here for a call. */
19929 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
19930 elt_strict |= maybe_adjust_types_for_deduction
19931 (DEDUCE_CALL, &elttype, &type, elt);
19932 elt = type;
19935 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
19936 explain_p);
19939 if (TREE_CODE (parm) == ARRAY_TYPE
19940 && deducible_array_bound (TYPE_DOMAIN (parm)))
19942 /* Also deduce from the length of the initializer list. */
19943 tree max = size_int (CONSTRUCTOR_NELTS (arg));
19944 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
19945 if (idx == error_mark_node)
19946 return unify_invalid (explain_p);
19947 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19948 idx, explain_p);
19951 /* If the std::initializer_list<T> deduction worked, replace the
19952 deduced A with std::initializer_list<A>. */
19953 if (orig_parm != parm)
19955 idx = TEMPLATE_TYPE_IDX (orig_parm);
19956 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19957 targ = listify (targ);
19958 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
19960 return unify_success (explain_p);
19963 /* Immediately reject some pairs that won't unify because of
19964 cv-qualification mismatches. */
19965 if (TREE_CODE (arg) == TREE_CODE (parm)
19966 && TYPE_P (arg)
19967 /* It is the elements of the array which hold the cv quals of an array
19968 type, and the elements might be template type parms. We'll check
19969 when we recurse. */
19970 && TREE_CODE (arg) != ARRAY_TYPE
19971 /* We check the cv-qualifiers when unifying with template type
19972 parameters below. We want to allow ARG `const T' to unify with
19973 PARM `T' for example, when computing which of two templates
19974 is more specialized, for example. */
19975 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
19976 && !check_cv_quals_for_unify (strict_in, arg, parm))
19977 return unify_cv_qual_mismatch (explain_p, parm, arg);
19979 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
19980 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
19981 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
19982 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
19983 strict &= ~UNIFY_ALLOW_DERIVED;
19984 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19985 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
19987 switch (TREE_CODE (parm))
19989 case TYPENAME_TYPE:
19990 case SCOPE_REF:
19991 case UNBOUND_CLASS_TEMPLATE:
19992 /* In a type which contains a nested-name-specifier, template
19993 argument values cannot be deduced for template parameters used
19994 within the nested-name-specifier. */
19995 return unify_success (explain_p);
19997 case TEMPLATE_TYPE_PARM:
19998 case TEMPLATE_TEMPLATE_PARM:
19999 case BOUND_TEMPLATE_TEMPLATE_PARM:
20000 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20001 if (error_operand_p (tparm))
20002 return unify_invalid (explain_p);
20004 if (TEMPLATE_TYPE_LEVEL (parm)
20005 != template_decl_level (tparm))
20006 /* The PARM is not one we're trying to unify. Just check
20007 to see if it matches ARG. */
20009 if (TREE_CODE (arg) == TREE_CODE (parm)
20010 && (is_auto (parm) ? is_auto (arg)
20011 : same_type_p (parm, arg)))
20012 return unify_success (explain_p);
20013 else
20014 return unify_type_mismatch (explain_p, parm, arg);
20016 idx = TEMPLATE_TYPE_IDX (parm);
20017 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20018 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20019 if (error_operand_p (tparm))
20020 return unify_invalid (explain_p);
20022 /* Check for mixed types and values. */
20023 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20024 && TREE_CODE (tparm) != TYPE_DECL)
20025 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20026 && TREE_CODE (tparm) != TEMPLATE_DECL))
20027 gcc_unreachable ();
20029 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20031 /* ARG must be constructed from a template class or a template
20032 template parameter. */
20033 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20034 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20035 return unify_template_deduction_failure (explain_p, parm, arg);
20037 tree parmvec = TYPE_TI_ARGS (parm);
20038 /* An alias template name is never deduced. */
20039 if (TYPE_ALIAS_P (arg))
20040 arg = strip_typedefs (arg);
20041 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
20042 tree full_argvec = add_to_template_args (targs, argvec);
20043 tree parm_parms
20044 = DECL_INNERMOST_TEMPLATE_PARMS
20045 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
20046 int i, len;
20047 int parm_variadic_p = 0;
20049 /* The resolution to DR150 makes clear that default
20050 arguments for an N-argument may not be used to bind T
20051 to a template template parameter with fewer than N
20052 parameters. It is not safe to permit the binding of
20053 default arguments as an extension, as that may change
20054 the meaning of a conforming program. Consider:
20056 struct Dense { static const unsigned int dim = 1; };
20058 template <template <typename> class View,
20059 typename Block>
20060 void operator+(float, View<Block> const&);
20062 template <typename Block,
20063 unsigned int Dim = Block::dim>
20064 struct Lvalue_proxy { operator float() const; };
20066 void
20067 test_1d (void) {
20068 Lvalue_proxy<Dense> p;
20069 float b;
20070 b + p;
20073 Here, if Lvalue_proxy is permitted to bind to View, then
20074 the global operator+ will be used; if they are not, the
20075 Lvalue_proxy will be converted to float. */
20076 if (coerce_template_parms (parm_parms,
20077 full_argvec,
20078 TYPE_TI_TEMPLATE (parm),
20079 complain,
20080 /*require_all_args=*/true,
20081 /*use_default_args=*/false)
20082 == error_mark_node)
20083 return 1;
20085 /* Deduce arguments T, i from TT<T> or TT<i>.
20086 We check each element of PARMVEC and ARGVEC individually
20087 rather than the whole TREE_VEC since they can have
20088 different number of elements. */
20090 parmvec = expand_template_argument_pack (parmvec);
20091 argvec = expand_template_argument_pack (argvec);
20093 len = TREE_VEC_LENGTH (parmvec);
20095 /* Check if the parameters end in a pack, making them
20096 variadic. */
20097 if (len > 0
20098 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
20099 parm_variadic_p = 1;
20101 for (i = 0; i < len - parm_variadic_p; ++i)
20102 /* If the template argument list of P contains a pack
20103 expansion that is not the last template argument, the
20104 entire template argument list is a non-deduced
20105 context. */
20106 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
20107 return unify_success (explain_p);
20109 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
20110 return unify_too_few_arguments (explain_p,
20111 TREE_VEC_LENGTH (argvec), len);
20113 for (i = 0; i < len - parm_variadic_p; ++i)
20115 RECUR_AND_CHECK_FAILURE (tparms, targs,
20116 TREE_VEC_ELT (parmvec, i),
20117 TREE_VEC_ELT (argvec, i),
20118 UNIFY_ALLOW_NONE, explain_p);
20121 if (parm_variadic_p
20122 && unify_pack_expansion (tparms, targs,
20123 parmvec, argvec,
20124 DEDUCE_EXACT,
20125 /*subr=*/true, explain_p))
20126 return 1;
20128 arg = TYPE_TI_TEMPLATE (arg);
20130 /* Fall through to deduce template name. */
20133 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20134 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20136 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20138 /* Simple cases: Value already set, does match or doesn't. */
20139 if (targ != NULL_TREE && template_args_equal (targ, arg))
20140 return unify_success (explain_p);
20141 else if (targ)
20142 return unify_inconsistency (explain_p, parm, targ, arg);
20144 else
20146 /* If PARM is `const T' and ARG is only `int', we don't have
20147 a match unless we are allowing additional qualification.
20148 If ARG is `const int' and PARM is just `T' that's OK;
20149 that binds `const int' to `T'. */
20150 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20151 arg, parm))
20152 return unify_cv_qual_mismatch (explain_p, parm, arg);
20154 /* Consider the case where ARG is `const volatile int' and
20155 PARM is `const T'. Then, T should be `volatile int'. */
20156 arg = cp_build_qualified_type_real
20157 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20158 if (arg == error_mark_node)
20159 return unify_invalid (explain_p);
20161 /* Simple cases: Value already set, does match or doesn't. */
20162 if (targ != NULL_TREE && same_type_p (targ, arg))
20163 return unify_success (explain_p);
20164 else if (targ)
20165 return unify_inconsistency (explain_p, parm, targ, arg);
20167 /* Make sure that ARG is not a variable-sized array. (Note
20168 that were talking about variable-sized arrays (like
20169 `int[n]'), rather than arrays of unknown size (like
20170 `int[]').) We'll get very confused by such a type since
20171 the bound of the array is not constant, and therefore
20172 not mangleable. Besides, such types are not allowed in
20173 ISO C++, so we can do as we please here. We do allow
20174 them for 'auto' deduction, since that isn't ABI-exposed. */
20175 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20176 return unify_vla_arg (explain_p, arg);
20178 /* Strip typedefs as in convert_template_argument. */
20179 arg = canonicalize_type_argument (arg, tf_none);
20182 /* If ARG is a parameter pack or an expansion, we cannot unify
20183 against it unless PARM is also a parameter pack. */
20184 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20185 && !template_parameter_pack_p (parm))
20186 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20188 /* If the argument deduction results is a METHOD_TYPE,
20189 then there is a problem.
20190 METHOD_TYPE doesn't map to any real C++ type the result of
20191 the deduction can not be of that type. */
20192 if (TREE_CODE (arg) == METHOD_TYPE)
20193 return unify_method_type_error (explain_p, arg);
20195 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20196 return unify_success (explain_p);
20198 case TEMPLATE_PARM_INDEX:
20199 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20200 if (error_operand_p (tparm))
20201 return unify_invalid (explain_p);
20203 if (TEMPLATE_PARM_LEVEL (parm)
20204 != template_decl_level (tparm))
20206 /* The PARM is not one we're trying to unify. Just check
20207 to see if it matches ARG. */
20208 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20209 && cp_tree_equal (parm, arg));
20210 if (result)
20211 unify_expression_unequal (explain_p, parm, arg);
20212 return result;
20215 idx = TEMPLATE_PARM_IDX (parm);
20216 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20218 if (targ)
20220 int x = !cp_tree_equal (targ, arg);
20221 if (x)
20222 unify_inconsistency (explain_p, parm, targ, arg);
20223 return x;
20226 if (cxx_dialect >= cxx1z
20227 /* We deduce from array bounds in try_array_deduction. */
20228 && !(strict & UNIFY_ALLOW_INTEGER)
20229 && uses_template_parms (TREE_TYPE (parm))
20230 && !type_uses_auto (TREE_TYPE (parm)))
20232 tree atype = TREE_TYPE (arg);
20233 RECUR_AND_CHECK_FAILURE (tparms, targs,
20234 TREE_TYPE (parm), atype,
20235 UNIFY_ALLOW_NONE, explain_p);
20238 /* [temp.deduct.type] If, in the declaration of a function template
20239 with a non-type template-parameter, the non-type
20240 template-parameter is used in an expression in the function
20241 parameter-list and, if the corresponding template-argument is
20242 deduced, the template-argument type shall match the type of the
20243 template-parameter exactly, except that a template-argument
20244 deduced from an array bound may be of any integral type.
20245 The non-type parameter might use already deduced type parameters. */
20246 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20247 if (tree a = type_uses_auto (tparm))
20249 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20250 if (tparm == error_mark_node)
20251 return 1;
20254 if (!TREE_TYPE (arg))
20255 /* Template-parameter dependent expression. Just accept it for now.
20256 It will later be processed in convert_template_argument. */
20258 else if (same_type_p (TREE_TYPE (arg), tparm))
20259 /* OK */;
20260 else if ((strict & UNIFY_ALLOW_INTEGER)
20261 && CP_INTEGRAL_TYPE_P (tparm))
20262 /* Convert the ARG to the type of PARM; the deduced non-type
20263 template argument must exactly match the types of the
20264 corresponding parameter. */
20265 arg = fold (build_nop (tparm, arg));
20266 else if (uses_template_parms (tparm))
20267 /* We haven't deduced the type of this parameter yet. Try again
20268 later. */
20269 return unify_success (explain_p);
20270 else
20271 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20273 /* If ARG is a parameter pack or an expansion, we cannot unify
20274 against it unless PARM is also a parameter pack. */
20275 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20276 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20277 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20280 bool removed_attr = false;
20281 arg = strip_typedefs_expr (arg, &removed_attr);
20283 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20284 return unify_success (explain_p);
20286 case PTRMEM_CST:
20288 /* A pointer-to-member constant can be unified only with
20289 another constant. */
20290 if (TREE_CODE (arg) != PTRMEM_CST)
20291 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20293 /* Just unify the class member. It would be useless (and possibly
20294 wrong, depending on the strict flags) to unify also
20295 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20296 arg refer to the same variable, even if through different
20297 classes. For instance:
20299 struct A { int x; };
20300 struct B : A { };
20302 Unification of &A::x and &B::x must succeed. */
20303 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20304 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20307 case POINTER_TYPE:
20309 if (!TYPE_PTR_P (arg))
20310 return unify_type_mismatch (explain_p, parm, arg);
20312 /* [temp.deduct.call]
20314 A can be another pointer or pointer to member type that can
20315 be converted to the deduced A via a qualification
20316 conversion (_conv.qual_).
20318 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20319 This will allow for additional cv-qualification of the
20320 pointed-to types if appropriate. */
20322 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20323 /* The derived-to-base conversion only persists through one
20324 level of pointers. */
20325 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20327 return unify (tparms, targs, TREE_TYPE (parm),
20328 TREE_TYPE (arg), strict, explain_p);
20331 case REFERENCE_TYPE:
20332 if (TREE_CODE (arg) != REFERENCE_TYPE)
20333 return unify_type_mismatch (explain_p, parm, arg);
20334 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20335 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20337 case ARRAY_TYPE:
20338 if (TREE_CODE (arg) != ARRAY_TYPE)
20339 return unify_type_mismatch (explain_p, parm, arg);
20340 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20341 != (TYPE_DOMAIN (arg) == NULL_TREE))
20342 return unify_type_mismatch (explain_p, parm, arg);
20343 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20344 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20345 if (TYPE_DOMAIN (parm) != NULL_TREE)
20346 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20347 TYPE_DOMAIN (arg), explain_p);
20348 return unify_success (explain_p);
20350 case REAL_TYPE:
20351 case COMPLEX_TYPE:
20352 case VECTOR_TYPE:
20353 case INTEGER_TYPE:
20354 case BOOLEAN_TYPE:
20355 case ENUMERAL_TYPE:
20356 case VOID_TYPE:
20357 case NULLPTR_TYPE:
20358 if (TREE_CODE (arg) != TREE_CODE (parm))
20359 return unify_type_mismatch (explain_p, parm, arg);
20361 /* We have already checked cv-qualification at the top of the
20362 function. */
20363 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20364 return unify_type_mismatch (explain_p, parm, arg);
20366 /* As far as unification is concerned, this wins. Later checks
20367 will invalidate it if necessary. */
20368 return unify_success (explain_p);
20370 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20371 /* Type INTEGER_CST can come from ordinary constant template args. */
20372 case INTEGER_CST:
20373 while (CONVERT_EXPR_P (arg))
20374 arg = TREE_OPERAND (arg, 0);
20376 if (TREE_CODE (arg) != INTEGER_CST)
20377 return unify_template_argument_mismatch (explain_p, parm, arg);
20378 return (tree_int_cst_equal (parm, arg)
20379 ? unify_success (explain_p)
20380 : unify_template_argument_mismatch (explain_p, parm, arg));
20382 case TREE_VEC:
20384 int i, len, argslen;
20385 int parm_variadic_p = 0;
20387 if (TREE_CODE (arg) != TREE_VEC)
20388 return unify_template_argument_mismatch (explain_p, parm, arg);
20390 len = TREE_VEC_LENGTH (parm);
20391 argslen = TREE_VEC_LENGTH (arg);
20393 /* Check for pack expansions in the parameters. */
20394 for (i = 0; i < len; ++i)
20396 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20398 if (i == len - 1)
20399 /* We can unify against something with a trailing
20400 parameter pack. */
20401 parm_variadic_p = 1;
20402 else
20403 /* [temp.deduct.type]/9: If the template argument list of
20404 P contains a pack expansion that is not the last
20405 template argument, the entire template argument list
20406 is a non-deduced context. */
20407 return unify_success (explain_p);
20411 /* If we don't have enough arguments to satisfy the parameters
20412 (not counting the pack expression at the end), or we have
20413 too many arguments for a parameter list that doesn't end in
20414 a pack expression, we can't unify. */
20415 if (parm_variadic_p
20416 ? argslen < len - parm_variadic_p
20417 : argslen != len)
20418 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20420 /* Unify all of the parameters that precede the (optional)
20421 pack expression. */
20422 for (i = 0; i < len - parm_variadic_p; ++i)
20424 RECUR_AND_CHECK_FAILURE (tparms, targs,
20425 TREE_VEC_ELT (parm, i),
20426 TREE_VEC_ELT (arg, i),
20427 UNIFY_ALLOW_NONE, explain_p);
20429 if (parm_variadic_p)
20430 return unify_pack_expansion (tparms, targs, parm, arg,
20431 DEDUCE_EXACT,
20432 /*subr=*/true, explain_p);
20433 return unify_success (explain_p);
20436 case RECORD_TYPE:
20437 case UNION_TYPE:
20438 if (TREE_CODE (arg) != TREE_CODE (parm))
20439 return unify_type_mismatch (explain_p, parm, arg);
20441 if (TYPE_PTRMEMFUNC_P (parm))
20443 if (!TYPE_PTRMEMFUNC_P (arg))
20444 return unify_type_mismatch (explain_p, parm, arg);
20446 return unify (tparms, targs,
20447 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20448 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20449 strict, explain_p);
20451 else if (TYPE_PTRMEMFUNC_P (arg))
20452 return unify_type_mismatch (explain_p, parm, arg);
20454 if (CLASSTYPE_TEMPLATE_INFO (parm))
20456 tree t = NULL_TREE;
20458 if (strict_in & UNIFY_ALLOW_DERIVED)
20460 /* First, we try to unify the PARM and ARG directly. */
20461 t = try_class_unification (tparms, targs,
20462 parm, arg, explain_p);
20464 if (!t)
20466 /* Fallback to the special case allowed in
20467 [temp.deduct.call]:
20469 If P is a class, and P has the form
20470 template-id, then A can be a derived class of
20471 the deduced A. Likewise, if P is a pointer to
20472 a class of the form template-id, A can be a
20473 pointer to a derived class pointed to by the
20474 deduced A. */
20475 enum template_base_result r;
20476 r = get_template_base (tparms, targs, parm, arg,
20477 explain_p, &t);
20479 if (!t)
20481 /* Don't give the derived diagnostic if we're
20482 already dealing with the same template. */
20483 bool same_template
20484 = (CLASSTYPE_TEMPLATE_INFO (arg)
20485 && (CLASSTYPE_TI_TEMPLATE (parm)
20486 == CLASSTYPE_TI_TEMPLATE (arg)));
20487 return unify_no_common_base (explain_p && !same_template,
20488 r, parm, arg);
20492 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20493 && (CLASSTYPE_TI_TEMPLATE (parm)
20494 == CLASSTYPE_TI_TEMPLATE (arg)))
20495 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20496 Then, we should unify `int' and `U'. */
20497 t = arg;
20498 else
20499 /* There's no chance of unification succeeding. */
20500 return unify_type_mismatch (explain_p, parm, arg);
20502 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20503 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20505 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20506 return unify_type_mismatch (explain_p, parm, arg);
20507 return unify_success (explain_p);
20509 case METHOD_TYPE:
20510 case FUNCTION_TYPE:
20512 unsigned int nargs;
20513 tree *args;
20514 tree a;
20515 unsigned int i;
20517 if (TREE_CODE (arg) != TREE_CODE (parm))
20518 return unify_type_mismatch (explain_p, parm, arg);
20520 /* CV qualifications for methods can never be deduced, they must
20521 match exactly. We need to check them explicitly here,
20522 because type_unification_real treats them as any other
20523 cv-qualified parameter. */
20524 if (TREE_CODE (parm) == METHOD_TYPE
20525 && (!check_cv_quals_for_unify
20526 (UNIFY_ALLOW_NONE,
20527 class_of_this_parm (arg),
20528 class_of_this_parm (parm))))
20529 return unify_cv_qual_mismatch (explain_p, parm, arg);
20530 if (TREE_CODE (arg) == FUNCTION_TYPE
20531 && type_memfn_quals (parm) != type_memfn_quals (arg))
20532 return unify_cv_qual_mismatch (explain_p, parm, arg);
20533 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20534 return unify_type_mismatch (explain_p, parm, arg);
20536 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20537 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20539 nargs = list_length (TYPE_ARG_TYPES (arg));
20540 args = XALLOCAVEC (tree, nargs);
20541 for (a = TYPE_ARG_TYPES (arg), i = 0;
20542 a != NULL_TREE && a != void_list_node;
20543 a = TREE_CHAIN (a), ++i)
20544 args[i] = TREE_VALUE (a);
20545 nargs = i;
20547 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20548 args, nargs, 1, DEDUCE_EXACT,
20549 LOOKUP_NORMAL, NULL, explain_p))
20550 return 1;
20552 if (flag_noexcept_type)
20554 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20555 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20556 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20557 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20558 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20559 && uses_template_parms (TREE_PURPOSE (pspec)))
20560 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20561 TREE_PURPOSE (aspec),
20562 UNIFY_ALLOW_NONE, explain_p);
20563 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20564 return unify_type_mismatch (explain_p, parm, arg);
20567 return 0;
20570 case OFFSET_TYPE:
20571 /* Unify a pointer to member with a pointer to member function, which
20572 deduces the type of the member as a function type. */
20573 if (TYPE_PTRMEMFUNC_P (arg))
20575 /* Check top-level cv qualifiers */
20576 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20577 return unify_cv_qual_mismatch (explain_p, parm, arg);
20579 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20580 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20581 UNIFY_ALLOW_NONE, explain_p);
20583 /* Determine the type of the function we are unifying against. */
20584 tree fntype = static_fn_type (arg);
20586 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20589 if (TREE_CODE (arg) != OFFSET_TYPE)
20590 return unify_type_mismatch (explain_p, parm, arg);
20591 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20592 TYPE_OFFSET_BASETYPE (arg),
20593 UNIFY_ALLOW_NONE, explain_p);
20594 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20595 strict, explain_p);
20597 case CONST_DECL:
20598 if (DECL_TEMPLATE_PARM_P (parm))
20599 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20600 if (arg != scalar_constant_value (parm))
20601 return unify_template_argument_mismatch (explain_p, parm, arg);
20602 return unify_success (explain_p);
20604 case FIELD_DECL:
20605 case TEMPLATE_DECL:
20606 /* Matched cases are handled by the ARG == PARM test above. */
20607 return unify_template_argument_mismatch (explain_p, parm, arg);
20609 case VAR_DECL:
20610 /* We might get a variable as a non-type template argument in parm if the
20611 corresponding parameter is type-dependent. Make any necessary
20612 adjustments based on whether arg is a reference. */
20613 if (CONSTANT_CLASS_P (arg))
20614 parm = fold_non_dependent_expr (parm);
20615 else if (REFERENCE_REF_P (arg))
20617 tree sub = TREE_OPERAND (arg, 0);
20618 STRIP_NOPS (sub);
20619 if (TREE_CODE (sub) == ADDR_EXPR)
20620 arg = TREE_OPERAND (sub, 0);
20622 /* Now use the normal expression code to check whether they match. */
20623 goto expr;
20625 case TYPE_ARGUMENT_PACK:
20626 case NONTYPE_ARGUMENT_PACK:
20627 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20628 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20630 case TYPEOF_TYPE:
20631 case DECLTYPE_TYPE:
20632 case UNDERLYING_TYPE:
20633 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20634 or UNDERLYING_TYPE nodes. */
20635 return unify_success (explain_p);
20637 case ERROR_MARK:
20638 /* Unification fails if we hit an error node. */
20639 return unify_invalid (explain_p);
20641 case INDIRECT_REF:
20642 if (REFERENCE_REF_P (parm))
20644 if (REFERENCE_REF_P (arg))
20645 arg = TREE_OPERAND (arg, 0);
20646 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20647 strict, explain_p);
20649 /* FALLTHRU */
20651 default:
20652 /* An unresolved overload is a nondeduced context. */
20653 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20654 return unify_success (explain_p);
20655 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20656 expr:
20657 /* We must be looking at an expression. This can happen with
20658 something like:
20660 template <int I>
20661 void foo(S<I>, S<I + 2>);
20663 This is a "nondeduced context":
20665 [deduct.type]
20667 The nondeduced contexts are:
20669 --A type that is a template-id in which one or more of
20670 the template-arguments is an expression that references
20671 a template-parameter.
20673 In these cases, we assume deduction succeeded, but don't
20674 actually infer any unifications. */
20676 if (!uses_template_parms (parm)
20677 && !template_args_equal (parm, arg))
20678 return unify_expression_unequal (explain_p, parm, arg);
20679 else
20680 return unify_success (explain_p);
20683 #undef RECUR_AND_CHECK_FAILURE
20685 /* Note that DECL can be defined in this translation unit, if
20686 required. */
20688 static void
20689 mark_definable (tree decl)
20691 tree clone;
20692 DECL_NOT_REALLY_EXTERN (decl) = 1;
20693 FOR_EACH_CLONE (clone, decl)
20694 DECL_NOT_REALLY_EXTERN (clone) = 1;
20697 /* Called if RESULT is explicitly instantiated, or is a member of an
20698 explicitly instantiated class. */
20700 void
20701 mark_decl_instantiated (tree result, int extern_p)
20703 SET_DECL_EXPLICIT_INSTANTIATION (result);
20705 /* If this entity has already been written out, it's too late to
20706 make any modifications. */
20707 if (TREE_ASM_WRITTEN (result))
20708 return;
20710 /* For anonymous namespace we don't need to do anything. */
20711 if (decl_anon_ns_mem_p (result))
20713 gcc_assert (!TREE_PUBLIC (result));
20714 return;
20717 if (TREE_CODE (result) != FUNCTION_DECL)
20718 /* The TREE_PUBLIC flag for function declarations will have been
20719 set correctly by tsubst. */
20720 TREE_PUBLIC (result) = 1;
20722 /* This might have been set by an earlier implicit instantiation. */
20723 DECL_COMDAT (result) = 0;
20725 if (extern_p)
20726 DECL_NOT_REALLY_EXTERN (result) = 0;
20727 else
20729 mark_definable (result);
20730 mark_needed (result);
20731 /* Always make artificials weak. */
20732 if (DECL_ARTIFICIAL (result) && flag_weak)
20733 comdat_linkage (result);
20734 /* For WIN32 we also want to put explicit instantiations in
20735 linkonce sections. */
20736 else if (TREE_PUBLIC (result))
20737 maybe_make_one_only (result);
20740 /* If EXTERN_P, then this function will not be emitted -- unless
20741 followed by an explicit instantiation, at which point its linkage
20742 will be adjusted. If !EXTERN_P, then this function will be
20743 emitted here. In neither circumstance do we want
20744 import_export_decl to adjust the linkage. */
20745 DECL_INTERFACE_KNOWN (result) = 1;
20748 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20749 important template arguments. If any are missing, we check whether
20750 they're important by using error_mark_node for substituting into any
20751 args that were used for partial ordering (the ones between ARGS and END)
20752 and seeing if it bubbles up. */
20754 static bool
20755 check_undeduced_parms (tree targs, tree args, tree end)
20757 bool found = false;
20758 int i;
20759 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
20760 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
20762 found = true;
20763 TREE_VEC_ELT (targs, i) = error_mark_node;
20765 if (found)
20767 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
20768 if (substed == error_mark_node)
20769 return true;
20771 return false;
20774 /* Given two function templates PAT1 and PAT2, return:
20776 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20777 -1 if PAT2 is more specialized than PAT1.
20778 0 if neither is more specialized.
20780 LEN indicates the number of parameters we should consider
20781 (defaulted parameters should not be considered).
20783 The 1998 std underspecified function template partial ordering, and
20784 DR214 addresses the issue. We take pairs of arguments, one from
20785 each of the templates, and deduce them against each other. One of
20786 the templates will be more specialized if all the *other*
20787 template's arguments deduce against its arguments and at least one
20788 of its arguments *does* *not* deduce against the other template's
20789 corresponding argument. Deduction is done as for class templates.
20790 The arguments used in deduction have reference and top level cv
20791 qualifiers removed. Iff both arguments were originally reference
20792 types *and* deduction succeeds in both directions, an lvalue reference
20793 wins against an rvalue reference and otherwise the template
20794 with the more cv-qualified argument wins for that pairing (if
20795 neither is more cv-qualified, they both are equal). Unlike regular
20796 deduction, after all the arguments have been deduced in this way,
20797 we do *not* verify the deduced template argument values can be
20798 substituted into non-deduced contexts.
20800 The logic can be a bit confusing here, because we look at deduce1 and
20801 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20802 can find template arguments for pat1 to make arg1 look like arg2, that
20803 means that arg2 is at least as specialized as arg1. */
20806 more_specialized_fn (tree pat1, tree pat2, int len)
20808 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
20809 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
20810 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
20811 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
20812 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
20813 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
20814 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
20815 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
20816 tree origs1, origs2;
20817 bool lose1 = false;
20818 bool lose2 = false;
20820 /* Remove the this parameter from non-static member functions. If
20821 one is a non-static member function and the other is not a static
20822 member function, remove the first parameter from that function
20823 also. This situation occurs for operator functions where we
20824 locate both a member function (with this pointer) and non-member
20825 operator (with explicit first operand). */
20826 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
20828 len--; /* LEN is the number of significant arguments for DECL1 */
20829 args1 = TREE_CHAIN (args1);
20830 if (!DECL_STATIC_FUNCTION_P (decl2))
20831 args2 = TREE_CHAIN (args2);
20833 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
20835 args2 = TREE_CHAIN (args2);
20836 if (!DECL_STATIC_FUNCTION_P (decl1))
20838 len--;
20839 args1 = TREE_CHAIN (args1);
20843 /* If only one is a conversion operator, they are unordered. */
20844 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
20845 return 0;
20847 /* Consider the return type for a conversion function */
20848 if (DECL_CONV_FN_P (decl1))
20850 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
20851 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
20852 len++;
20855 processing_template_decl++;
20857 origs1 = args1;
20858 origs2 = args2;
20860 while (len--
20861 /* Stop when an ellipsis is seen. */
20862 && args1 != NULL_TREE && args2 != NULL_TREE)
20864 tree arg1 = TREE_VALUE (args1);
20865 tree arg2 = TREE_VALUE (args2);
20866 int deduce1, deduce2;
20867 int quals1 = -1;
20868 int quals2 = -1;
20869 int ref1 = 0;
20870 int ref2 = 0;
20872 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20873 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20875 /* When both arguments are pack expansions, we need only
20876 unify the patterns themselves. */
20877 arg1 = PACK_EXPANSION_PATTERN (arg1);
20878 arg2 = PACK_EXPANSION_PATTERN (arg2);
20880 /* This is the last comparison we need to do. */
20881 len = 0;
20884 if (TREE_CODE (arg1) == REFERENCE_TYPE)
20886 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
20887 arg1 = TREE_TYPE (arg1);
20888 quals1 = cp_type_quals (arg1);
20891 if (TREE_CODE (arg2) == REFERENCE_TYPE)
20893 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
20894 arg2 = TREE_TYPE (arg2);
20895 quals2 = cp_type_quals (arg2);
20898 arg1 = TYPE_MAIN_VARIANT (arg1);
20899 arg2 = TYPE_MAIN_VARIANT (arg2);
20901 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
20903 int i, len2 = remaining_arguments (args2);
20904 tree parmvec = make_tree_vec (1);
20905 tree argvec = make_tree_vec (len2);
20906 tree ta = args2;
20908 /* Setup the parameter vector, which contains only ARG1. */
20909 TREE_VEC_ELT (parmvec, 0) = arg1;
20911 /* Setup the argument vector, which contains the remaining
20912 arguments. */
20913 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
20914 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20916 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
20917 argvec, DEDUCE_EXACT,
20918 /*subr=*/true, /*explain_p=*/false)
20919 == 0);
20921 /* We cannot deduce in the other direction, because ARG1 is
20922 a pack expansion but ARG2 is not. */
20923 deduce2 = 0;
20925 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20927 int i, len1 = remaining_arguments (args1);
20928 tree parmvec = make_tree_vec (1);
20929 tree argvec = make_tree_vec (len1);
20930 tree ta = args1;
20932 /* Setup the parameter vector, which contains only ARG1. */
20933 TREE_VEC_ELT (parmvec, 0) = arg2;
20935 /* Setup the argument vector, which contains the remaining
20936 arguments. */
20937 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
20938 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20940 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
20941 argvec, DEDUCE_EXACT,
20942 /*subr=*/true, /*explain_p=*/false)
20943 == 0);
20945 /* We cannot deduce in the other direction, because ARG2 is
20946 a pack expansion but ARG1 is not.*/
20947 deduce1 = 0;
20950 else
20952 /* The normal case, where neither argument is a pack
20953 expansion. */
20954 deduce1 = (unify (tparms1, targs1, arg1, arg2,
20955 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20956 == 0);
20957 deduce2 = (unify (tparms2, targs2, arg2, arg1,
20958 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20959 == 0);
20962 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20963 arg2, then arg2 is not as specialized as arg1. */
20964 if (!deduce1)
20965 lose2 = true;
20966 if (!deduce2)
20967 lose1 = true;
20969 /* "If, for a given type, deduction succeeds in both directions
20970 (i.e., the types are identical after the transformations above)
20971 and both P and A were reference types (before being replaced with
20972 the type referred to above):
20973 - if the type from the argument template was an lvalue reference and
20974 the type from the parameter template was not, the argument type is
20975 considered to be more specialized than the other; otherwise,
20976 - if the type from the argument template is more cv-qualified
20977 than the type from the parameter template (as described above),
20978 the argument type is considered to be more specialized than the other;
20979 otherwise,
20980 - neither type is more specialized than the other." */
20982 if (deduce1 && deduce2)
20984 if (ref1 && ref2 && ref1 != ref2)
20986 if (ref1 > ref2)
20987 lose1 = true;
20988 else
20989 lose2 = true;
20991 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
20993 if ((quals1 & quals2) == quals2)
20994 lose2 = true;
20995 if ((quals1 & quals2) == quals1)
20996 lose1 = true;
21000 if (lose1 && lose2)
21001 /* We've failed to deduce something in either direction.
21002 These must be unordered. */
21003 break;
21005 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21006 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21007 /* We have already processed all of the arguments in our
21008 handing of the pack expansion type. */
21009 len = 0;
21011 args1 = TREE_CHAIN (args1);
21012 args2 = TREE_CHAIN (args2);
21015 /* "In most cases, all template parameters must have values in order for
21016 deduction to succeed, but for partial ordering purposes a template
21017 parameter may remain without a value provided it is not used in the
21018 types being used for partial ordering."
21020 Thus, if we are missing any of the targs1 we need to substitute into
21021 origs1, then pat2 is not as specialized as pat1. This can happen when
21022 there is a nondeduced context. */
21023 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21024 lose2 = true;
21025 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21026 lose1 = true;
21028 processing_template_decl--;
21030 /* If both deductions succeed, the partial ordering selects the more
21031 constrained template. */
21032 if (!lose1 && !lose2)
21034 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21035 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21036 lose1 = !subsumes_constraints (c1, c2);
21037 lose2 = !subsumes_constraints (c2, c1);
21040 /* All things being equal, if the next argument is a pack expansion
21041 for one function but not for the other, prefer the
21042 non-variadic function. FIXME this is bogus; see c++/41958. */
21043 if (lose1 == lose2
21044 && args1 && TREE_VALUE (args1)
21045 && args2 && TREE_VALUE (args2))
21047 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21048 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21051 if (lose1 == lose2)
21052 return 0;
21053 else if (!lose1)
21054 return 1;
21055 else
21056 return -1;
21059 /* Determine which of two partial specializations of TMPL is more
21060 specialized.
21062 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21063 to the first partial specialization. The TREE_PURPOSE is the
21064 innermost set of template parameters for the partial
21065 specialization. PAT2 is similar, but for the second template.
21067 Return 1 if the first partial specialization is more specialized;
21068 -1 if the second is more specialized; 0 if neither is more
21069 specialized.
21071 See [temp.class.order] for information about determining which of
21072 two templates is more specialized. */
21074 static int
21075 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21077 tree targs;
21078 int winner = 0;
21079 bool any_deductions = false;
21081 tree tmpl1 = TREE_VALUE (pat1);
21082 tree tmpl2 = TREE_VALUE (pat2);
21083 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21084 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21086 /* Just like what happens for functions, if we are ordering between
21087 different template specializations, we may encounter dependent
21088 types in the arguments, and we need our dependency check functions
21089 to behave correctly. */
21090 ++processing_template_decl;
21091 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21092 if (targs)
21094 --winner;
21095 any_deductions = true;
21098 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21099 if (targs)
21101 ++winner;
21102 any_deductions = true;
21104 --processing_template_decl;
21106 /* If both deductions succeed, the partial ordering selects the more
21107 constrained template. */
21108 if (!winner && any_deductions)
21109 return more_constrained (tmpl1, tmpl2);
21111 /* In the case of a tie where at least one of the templates
21112 has a parameter pack at the end, the template with the most
21113 non-packed parameters wins. */
21114 if (winner == 0
21115 && any_deductions
21116 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21117 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21119 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21120 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21121 int len1 = TREE_VEC_LENGTH (args1);
21122 int len2 = TREE_VEC_LENGTH (args2);
21124 /* We don't count the pack expansion at the end. */
21125 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21126 --len1;
21127 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21128 --len2;
21130 if (len1 > len2)
21131 return 1;
21132 else if (len1 < len2)
21133 return -1;
21136 return winner;
21139 /* Return the template arguments that will produce the function signature
21140 DECL from the function template FN, with the explicit template
21141 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21142 also match. Return NULL_TREE if no satisfactory arguments could be
21143 found. */
21145 static tree
21146 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21148 int ntparms = DECL_NTPARMS (fn);
21149 tree targs = make_tree_vec (ntparms);
21150 tree decl_type = TREE_TYPE (decl);
21151 tree decl_arg_types;
21152 tree *args;
21153 unsigned int nargs, ix;
21154 tree arg;
21156 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21158 /* Never do unification on the 'this' parameter. */
21159 decl_arg_types = skip_artificial_parms_for (decl,
21160 TYPE_ARG_TYPES (decl_type));
21162 nargs = list_length (decl_arg_types);
21163 args = XALLOCAVEC (tree, nargs);
21164 for (arg = decl_arg_types, ix = 0;
21165 arg != NULL_TREE && arg != void_list_node;
21166 arg = TREE_CHAIN (arg), ++ix)
21167 args[ix] = TREE_VALUE (arg);
21169 if (fn_type_unification (fn, explicit_args, targs,
21170 args, ix,
21171 (check_rettype || DECL_CONV_FN_P (fn)
21172 ? TREE_TYPE (decl_type) : NULL_TREE),
21173 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21174 /*decltype*/false)
21175 == error_mark_node)
21176 return NULL_TREE;
21178 return targs;
21181 /* Return the innermost template arguments that, when applied to a partial
21182 specialization SPEC_TMPL of TMPL, yield the ARGS.
21184 For example, suppose we have:
21186 template <class T, class U> struct S {};
21187 template <class T> struct S<T*, int> {};
21189 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21190 partial specialization and the ARGS will be {double*, int}. The resulting
21191 vector will be {double}, indicating that `T' is bound to `double'. */
21193 static tree
21194 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21196 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21197 tree spec_args
21198 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21199 int i, ntparms = TREE_VEC_LENGTH (tparms);
21200 tree deduced_args;
21201 tree innermost_deduced_args;
21203 innermost_deduced_args = make_tree_vec (ntparms);
21204 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21206 deduced_args = copy_node (args);
21207 SET_TMPL_ARGS_LEVEL (deduced_args,
21208 TMPL_ARGS_DEPTH (deduced_args),
21209 innermost_deduced_args);
21211 else
21212 deduced_args = innermost_deduced_args;
21214 bool tried_array_deduction = (cxx_dialect < cxx1z);
21215 again:
21216 if (unify (tparms, deduced_args,
21217 INNERMOST_TEMPLATE_ARGS (spec_args),
21218 INNERMOST_TEMPLATE_ARGS (args),
21219 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21220 return NULL_TREE;
21222 for (i = 0; i < ntparms; ++i)
21223 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21225 if (!tried_array_deduction)
21227 try_array_deduction (tparms, innermost_deduced_args,
21228 INNERMOST_TEMPLATE_ARGS (spec_args));
21229 tried_array_deduction = true;
21230 if (TREE_VEC_ELT (innermost_deduced_args, i))
21231 goto again;
21233 return NULL_TREE;
21236 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21237 if (!push_tinst_level (tinst))
21239 excessive_deduction_depth = true;
21240 return NULL_TREE;
21243 /* Verify that nondeduced template arguments agree with the type
21244 obtained from argument deduction.
21246 For example:
21248 struct A { typedef int X; };
21249 template <class T, class U> struct C {};
21250 template <class T> struct C<T, typename T::X> {};
21252 Then with the instantiation `C<A, int>', we can deduce that
21253 `T' is `A' but unify () does not check whether `typename T::X'
21254 is `int'. */
21255 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21256 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21257 spec_args, tmpl,
21258 tf_none, false, false);
21260 pop_tinst_level ();
21262 if (spec_args == error_mark_node
21263 /* We only need to check the innermost arguments; the other
21264 arguments will always agree. */
21265 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
21266 INNERMOST_TEMPLATE_ARGS (args)))
21267 return NULL_TREE;
21269 /* Now that we have bindings for all of the template arguments,
21270 ensure that the arguments deduced for the template template
21271 parameters have compatible template parameter lists. See the use
21272 of template_template_parm_bindings_ok_p in fn_type_unification
21273 for more information. */
21274 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21275 return NULL_TREE;
21277 return deduced_args;
21280 // Compare two function templates T1 and T2 by deducing bindings
21281 // from one against the other. If both deductions succeed, compare
21282 // constraints to see which is more constrained.
21283 static int
21284 more_specialized_inst (tree t1, tree t2)
21286 int fate = 0;
21287 int count = 0;
21289 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21291 --fate;
21292 ++count;
21295 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21297 ++fate;
21298 ++count;
21301 // If both deductions succeed, then one may be more constrained.
21302 if (count == 2 && fate == 0)
21303 fate = more_constrained (t1, t2);
21305 return fate;
21308 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21309 Return the TREE_LIST node with the most specialized template, if
21310 any. If there is no most specialized template, the error_mark_node
21311 is returned.
21313 Note that this function does not look at, or modify, the
21314 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21315 returned is one of the elements of INSTANTIATIONS, callers may
21316 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21317 and retrieve it from the value returned. */
21319 tree
21320 most_specialized_instantiation (tree templates)
21322 tree fn, champ;
21324 ++processing_template_decl;
21326 champ = templates;
21327 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21329 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21330 if (fate == -1)
21331 champ = fn;
21332 else if (!fate)
21334 /* Equally specialized, move to next function. If there
21335 is no next function, nothing's most specialized. */
21336 fn = TREE_CHAIN (fn);
21337 champ = fn;
21338 if (!fn)
21339 break;
21343 if (champ)
21344 /* Now verify that champ is better than everything earlier in the
21345 instantiation list. */
21346 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21347 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21349 champ = NULL_TREE;
21350 break;
21354 processing_template_decl--;
21356 if (!champ)
21357 return error_mark_node;
21359 return champ;
21362 /* If DECL is a specialization of some template, return the most
21363 general such template. Otherwise, returns NULL_TREE.
21365 For example, given:
21367 template <class T> struct S { template <class U> void f(U); };
21369 if TMPL is `template <class U> void S<int>::f(U)' this will return
21370 the full template. This function will not trace past partial
21371 specializations, however. For example, given in addition:
21373 template <class T> struct S<T*> { template <class U> void f(U); };
21375 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21376 `template <class T> template <class U> S<T*>::f(U)'. */
21378 tree
21379 most_general_template (tree decl)
21381 if (TREE_CODE (decl) != TEMPLATE_DECL)
21383 if (tree tinfo = get_template_info (decl))
21384 decl = TI_TEMPLATE (tinfo);
21385 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21386 template friend, or a FIELD_DECL for a capture pack. */
21387 if (TREE_CODE (decl) != TEMPLATE_DECL)
21388 return NULL_TREE;
21391 /* Look for more and more general templates. */
21392 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21394 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21395 (See cp-tree.h for details.) */
21396 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21397 break;
21399 if (CLASS_TYPE_P (TREE_TYPE (decl))
21400 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21401 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21402 break;
21404 /* Stop if we run into an explicitly specialized class template. */
21405 if (!DECL_NAMESPACE_SCOPE_P (decl)
21406 && DECL_CONTEXT (decl)
21407 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21408 break;
21410 decl = DECL_TI_TEMPLATE (decl);
21413 return decl;
21416 /* Return the most specialized of the template partial specializations
21417 which can produce TARGET, a specialization of some class or variable
21418 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21419 a TEMPLATE_DECL node corresponding to the partial specialization, while
21420 the TREE_PURPOSE is the set of template arguments that must be
21421 substituted into the template pattern in order to generate TARGET.
21423 If the choice of partial specialization is ambiguous, a diagnostic
21424 is issued, and the error_mark_node is returned. If there are no
21425 partial specializations matching TARGET, then NULL_TREE is
21426 returned, indicating that the primary template should be used. */
21428 static tree
21429 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21431 tree list = NULL_TREE;
21432 tree t;
21433 tree champ;
21434 int fate;
21435 bool ambiguous_p;
21436 tree outer_args = NULL_TREE;
21437 tree tmpl, args;
21439 if (TYPE_P (target))
21441 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21442 tmpl = TI_TEMPLATE (tinfo);
21443 args = TI_ARGS (tinfo);
21445 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21447 tmpl = TREE_OPERAND (target, 0);
21448 args = TREE_OPERAND (target, 1);
21450 else if (VAR_P (target))
21452 tree tinfo = DECL_TEMPLATE_INFO (target);
21453 tmpl = TI_TEMPLATE (tinfo);
21454 args = TI_ARGS (tinfo);
21456 else
21457 gcc_unreachable ();
21459 tree main_tmpl = most_general_template (tmpl);
21461 /* For determining which partial specialization to use, only the
21462 innermost args are interesting. */
21463 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21465 outer_args = strip_innermost_template_args (args, 1);
21466 args = INNERMOST_TEMPLATE_ARGS (args);
21469 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21471 tree spec_args;
21472 tree spec_tmpl = TREE_VALUE (t);
21474 if (outer_args)
21476 /* Substitute in the template args from the enclosing class. */
21477 ++processing_template_decl;
21478 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21479 --processing_template_decl;
21482 if (spec_tmpl == error_mark_node)
21483 return error_mark_node;
21485 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21486 if (spec_args)
21488 if (outer_args)
21489 spec_args = add_to_template_args (outer_args, spec_args);
21491 /* Keep the candidate only if the constraints are satisfied,
21492 or if we're not compiling with concepts. */
21493 if (!flag_concepts
21494 || constraints_satisfied_p (spec_tmpl, spec_args))
21496 list = tree_cons (spec_args, TREE_VALUE (t), list);
21497 TREE_TYPE (list) = TREE_TYPE (t);
21502 if (! list)
21503 return NULL_TREE;
21505 ambiguous_p = false;
21506 t = list;
21507 champ = t;
21508 t = TREE_CHAIN (t);
21509 for (; t; t = TREE_CHAIN (t))
21511 fate = more_specialized_partial_spec (tmpl, champ, t);
21512 if (fate == 1)
21514 else
21516 if (fate == 0)
21518 t = TREE_CHAIN (t);
21519 if (! t)
21521 ambiguous_p = true;
21522 break;
21525 champ = t;
21529 if (!ambiguous_p)
21530 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21532 fate = more_specialized_partial_spec (tmpl, champ, t);
21533 if (fate != 1)
21535 ambiguous_p = true;
21536 break;
21540 if (ambiguous_p)
21542 const char *str;
21543 char *spaces = NULL;
21544 if (!(complain & tf_error))
21545 return error_mark_node;
21546 if (TYPE_P (target))
21547 error ("ambiguous template instantiation for %q#T", target);
21548 else
21549 error ("ambiguous template instantiation for %q#D", target);
21550 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21551 for (t = list; t; t = TREE_CHAIN (t))
21553 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21554 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21555 "%s %#S", spaces ? spaces : str, subst);
21556 spaces = spaces ? spaces : get_spaces (str);
21558 free (spaces);
21559 return error_mark_node;
21562 return champ;
21565 /* Explicitly instantiate DECL. */
21567 void
21568 do_decl_instantiation (tree decl, tree storage)
21570 tree result = NULL_TREE;
21571 int extern_p = 0;
21573 if (!decl || decl == error_mark_node)
21574 /* An error occurred, for which grokdeclarator has already issued
21575 an appropriate message. */
21576 return;
21577 else if (! DECL_LANG_SPECIFIC (decl))
21579 error ("explicit instantiation of non-template %q#D", decl);
21580 return;
21583 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21584 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21586 if (VAR_P (decl) && !var_templ)
21588 /* There is an asymmetry here in the way VAR_DECLs and
21589 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21590 the latter, the DECL we get back will be marked as a
21591 template instantiation, and the appropriate
21592 DECL_TEMPLATE_INFO will be set up. This does not happen for
21593 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21594 should handle VAR_DECLs as it currently handles
21595 FUNCTION_DECLs. */
21596 if (!DECL_CLASS_SCOPE_P (decl))
21598 error ("%qD is not a static data member of a class template", decl);
21599 return;
21601 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21602 if (!result || !VAR_P (result))
21604 error ("no matching template for %qD found", decl);
21605 return;
21607 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21609 error ("type %qT for explicit instantiation %qD does not match "
21610 "declared type %qT", TREE_TYPE (result), decl,
21611 TREE_TYPE (decl));
21612 return;
21615 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21617 error ("explicit instantiation of %q#D", decl);
21618 return;
21620 else
21621 result = decl;
21623 /* Check for various error cases. Note that if the explicit
21624 instantiation is valid the RESULT will currently be marked as an
21625 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21626 until we get here. */
21628 if (DECL_TEMPLATE_SPECIALIZATION (result))
21630 /* DR 259 [temp.spec].
21632 Both an explicit instantiation and a declaration of an explicit
21633 specialization shall not appear in a program unless the explicit
21634 instantiation follows a declaration of the explicit specialization.
21636 For a given set of template parameters, if an explicit
21637 instantiation of a template appears after a declaration of an
21638 explicit specialization for that template, the explicit
21639 instantiation has no effect. */
21640 return;
21642 else if (DECL_EXPLICIT_INSTANTIATION (result))
21644 /* [temp.spec]
21646 No program shall explicitly instantiate any template more
21647 than once.
21649 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21650 the first instantiation was `extern' and the second is not,
21651 and EXTERN_P for the opposite case. */
21652 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21653 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21654 /* If an "extern" explicit instantiation follows an ordinary
21655 explicit instantiation, the template is instantiated. */
21656 if (extern_p)
21657 return;
21659 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21661 error ("no matching template for %qD found", result);
21662 return;
21664 else if (!DECL_TEMPLATE_INFO (result))
21666 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21667 return;
21670 if (storage == NULL_TREE)
21672 else if (storage == ridpointers[(int) RID_EXTERN])
21674 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21675 pedwarn (input_location, OPT_Wpedantic,
21676 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21677 "instantiations");
21678 extern_p = 1;
21680 else
21681 error ("storage class %qD applied to template instantiation", storage);
21683 check_explicit_instantiation_namespace (result);
21684 mark_decl_instantiated (result, extern_p);
21685 if (! extern_p)
21686 instantiate_decl (result, /*defer_ok=*/1,
21687 /*expl_inst_class_mem_p=*/false);
21690 static void
21691 mark_class_instantiated (tree t, int extern_p)
21693 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21694 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21695 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21696 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21697 if (! extern_p)
21699 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21700 rest_of_type_compilation (t, 1);
21704 /* Called from do_type_instantiation through binding_table_foreach to
21705 do recursive instantiation for the type bound in ENTRY. */
21706 static void
21707 bt_instantiate_type_proc (binding_entry entry, void *data)
21709 tree storage = *(tree *) data;
21711 if (MAYBE_CLASS_TYPE_P (entry->type)
21712 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
21713 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
21716 /* Called from do_type_instantiation to instantiate a member
21717 (a member function or a static member variable) of an
21718 explicitly instantiated class template. */
21719 static void
21720 instantiate_class_member (tree decl, int extern_p)
21722 mark_decl_instantiated (decl, extern_p);
21723 if (! extern_p)
21724 instantiate_decl (decl, /*defer_ok=*/1,
21725 /*expl_inst_class_mem_p=*/true);
21728 /* Perform an explicit instantiation of template class T. STORAGE, if
21729 non-null, is the RID for extern, inline or static. COMPLAIN is
21730 nonzero if this is called from the parser, zero if called recursively,
21731 since the standard is unclear (as detailed below). */
21733 void
21734 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
21736 int extern_p = 0;
21737 int nomem_p = 0;
21738 int static_p = 0;
21739 int previous_instantiation_extern_p = 0;
21741 if (TREE_CODE (t) == TYPE_DECL)
21742 t = TREE_TYPE (t);
21744 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
21746 tree tmpl =
21747 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
21748 if (tmpl)
21749 error ("explicit instantiation of non-class template %qD", tmpl);
21750 else
21751 error ("explicit instantiation of non-template type %qT", t);
21752 return;
21755 complete_type (t);
21757 if (!COMPLETE_TYPE_P (t))
21759 if (complain & tf_error)
21760 error ("explicit instantiation of %q#T before definition of template",
21762 return;
21765 if (storage != NULL_TREE)
21767 if (!in_system_header_at (input_location))
21769 if (storage == ridpointers[(int) RID_EXTERN])
21771 if (cxx_dialect == cxx98)
21772 pedwarn (input_location, OPT_Wpedantic,
21773 "ISO C++ 1998 forbids the use of %<extern%> on "
21774 "explicit instantiations");
21776 else
21777 pedwarn (input_location, OPT_Wpedantic,
21778 "ISO C++ forbids the use of %qE"
21779 " on explicit instantiations", storage);
21782 if (storage == ridpointers[(int) RID_INLINE])
21783 nomem_p = 1;
21784 else if (storage == ridpointers[(int) RID_EXTERN])
21785 extern_p = 1;
21786 else if (storage == ridpointers[(int) RID_STATIC])
21787 static_p = 1;
21788 else
21790 error ("storage class %qD applied to template instantiation",
21791 storage);
21792 extern_p = 0;
21796 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
21798 /* DR 259 [temp.spec].
21800 Both an explicit instantiation and a declaration of an explicit
21801 specialization shall not appear in a program unless the explicit
21802 instantiation follows a declaration of the explicit specialization.
21804 For a given set of template parameters, if an explicit
21805 instantiation of a template appears after a declaration of an
21806 explicit specialization for that template, the explicit
21807 instantiation has no effect. */
21808 return;
21810 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
21812 /* [temp.spec]
21814 No program shall explicitly instantiate any template more
21815 than once.
21817 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21818 instantiation was `extern'. If EXTERN_P then the second is.
21819 These cases are OK. */
21820 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
21822 if (!previous_instantiation_extern_p && !extern_p
21823 && (complain & tf_error))
21824 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
21826 /* If we've already instantiated the template, just return now. */
21827 if (!CLASSTYPE_INTERFACE_ONLY (t))
21828 return;
21831 check_explicit_instantiation_namespace (TYPE_NAME (t));
21832 mark_class_instantiated (t, extern_p);
21834 if (nomem_p)
21835 return;
21838 tree tmp;
21840 /* In contrast to implicit instantiation, where only the
21841 declarations, and not the definitions, of members are
21842 instantiated, we have here:
21844 [temp.explicit]
21846 The explicit instantiation of a class template specialization
21847 implies the instantiation of all of its members not
21848 previously explicitly specialized in the translation unit
21849 containing the explicit instantiation.
21851 Of course, we can't instantiate member template classes, since
21852 we don't have any arguments for them. Note that the standard
21853 is unclear on whether the instantiation of the members are
21854 *explicit* instantiations or not. However, the most natural
21855 interpretation is that it should be an explicit instantiation. */
21857 if (! static_p)
21858 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
21859 if (TREE_CODE (tmp) == FUNCTION_DECL
21860 && DECL_TEMPLATE_INSTANTIATION (tmp)
21861 && user_provided_p (tmp))
21862 instantiate_class_member (tmp, extern_p);
21864 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
21865 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
21866 instantiate_class_member (tmp, extern_p);
21868 if (CLASSTYPE_NESTED_UTDS (t))
21869 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
21870 bt_instantiate_type_proc, &storage);
21874 /* Given a function DECL, which is a specialization of TMPL, modify
21875 DECL to be a re-instantiation of TMPL with the same template
21876 arguments. TMPL should be the template into which tsubst'ing
21877 should occur for DECL, not the most general template.
21879 One reason for doing this is a scenario like this:
21881 template <class T>
21882 void f(const T&, int i);
21884 void g() { f(3, 7); }
21886 template <class T>
21887 void f(const T& t, const int i) { }
21889 Note that when the template is first instantiated, with
21890 instantiate_template, the resulting DECL will have no name for the
21891 first parameter, and the wrong type for the second. So, when we go
21892 to instantiate the DECL, we regenerate it. */
21894 static void
21895 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
21897 /* The arguments used to instantiate DECL, from the most general
21898 template. */
21899 tree code_pattern;
21901 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
21903 /* Make sure that we can see identifiers, and compute access
21904 correctly. */
21905 push_access_scope (decl);
21907 if (TREE_CODE (decl) == FUNCTION_DECL)
21909 tree decl_parm;
21910 tree pattern_parm;
21911 tree specs;
21912 int args_depth;
21913 int parms_depth;
21915 args_depth = TMPL_ARGS_DEPTH (args);
21916 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
21917 if (args_depth > parms_depth)
21918 args = get_innermost_template_args (args, parms_depth);
21920 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
21921 args, tf_error, NULL_TREE,
21922 /*defer_ok*/false);
21923 if (specs && specs != error_mark_node)
21924 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
21925 specs);
21927 /* Merge parameter declarations. */
21928 decl_parm = skip_artificial_parms_for (decl,
21929 DECL_ARGUMENTS (decl));
21930 pattern_parm
21931 = skip_artificial_parms_for (code_pattern,
21932 DECL_ARGUMENTS (code_pattern));
21933 while (decl_parm && !DECL_PACK_P (pattern_parm))
21935 tree parm_type;
21936 tree attributes;
21938 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21939 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
21940 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
21941 NULL_TREE);
21942 parm_type = type_decays_to (parm_type);
21943 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21944 TREE_TYPE (decl_parm) = parm_type;
21945 attributes = DECL_ATTRIBUTES (pattern_parm);
21946 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21948 DECL_ATTRIBUTES (decl_parm) = attributes;
21949 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21951 decl_parm = DECL_CHAIN (decl_parm);
21952 pattern_parm = DECL_CHAIN (pattern_parm);
21954 /* Merge any parameters that match with the function parameter
21955 pack. */
21956 if (pattern_parm && DECL_PACK_P (pattern_parm))
21958 int i, len;
21959 tree expanded_types;
21960 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21961 the parameters in this function parameter pack. */
21962 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
21963 args, tf_error, NULL_TREE);
21964 len = TREE_VEC_LENGTH (expanded_types);
21965 for (i = 0; i < len; i++)
21967 tree parm_type;
21968 tree attributes;
21970 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21971 /* Rename the parameter to include the index. */
21972 DECL_NAME (decl_parm) =
21973 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
21974 parm_type = TREE_VEC_ELT (expanded_types, i);
21975 parm_type = type_decays_to (parm_type);
21976 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21977 TREE_TYPE (decl_parm) = parm_type;
21978 attributes = DECL_ATTRIBUTES (pattern_parm);
21979 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21981 DECL_ATTRIBUTES (decl_parm) = attributes;
21982 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21984 decl_parm = DECL_CHAIN (decl_parm);
21987 /* Merge additional specifiers from the CODE_PATTERN. */
21988 if (DECL_DECLARED_INLINE_P (code_pattern)
21989 && !DECL_DECLARED_INLINE_P (decl))
21990 DECL_DECLARED_INLINE_P (decl) = 1;
21992 else if (VAR_P (decl))
21994 DECL_INITIAL (decl) =
21995 tsubst_expr (DECL_INITIAL (code_pattern), args,
21996 tf_error, DECL_TI_TEMPLATE (decl),
21997 /*integral_constant_expression_p=*/false);
21998 if (VAR_HAD_UNKNOWN_BOUND (decl))
21999 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22000 tf_error, DECL_TI_TEMPLATE (decl));
22002 else
22003 gcc_unreachable ();
22005 pop_access_scope (decl);
22008 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22009 substituted to get DECL. */
22011 tree
22012 template_for_substitution (tree decl)
22014 tree tmpl = DECL_TI_TEMPLATE (decl);
22016 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22017 for the instantiation. This is not always the most general
22018 template. Consider, for example:
22020 template <class T>
22021 struct S { template <class U> void f();
22022 template <> void f<int>(); };
22024 and an instantiation of S<double>::f<int>. We want TD to be the
22025 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22026 while (/* An instantiation cannot have a definition, so we need a
22027 more general template. */
22028 DECL_TEMPLATE_INSTANTIATION (tmpl)
22029 /* We must also deal with friend templates. Given:
22031 template <class T> struct S {
22032 template <class U> friend void f() {};
22035 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22036 so far as the language is concerned, but that's still
22037 where we get the pattern for the instantiation from. On
22038 other hand, if the definition comes outside the class, say:
22040 template <class T> struct S {
22041 template <class U> friend void f();
22043 template <class U> friend void f() {}
22045 we don't need to look any further. That's what the check for
22046 DECL_INITIAL is for. */
22047 || (TREE_CODE (decl) == FUNCTION_DECL
22048 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22049 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22051 /* The present template, TD, should not be a definition. If it
22052 were a definition, we should be using it! Note that we
22053 cannot restructure the loop to just keep going until we find
22054 a template with a definition, since that might go too far if
22055 a specialization was declared, but not defined. */
22057 /* Fetch the more general template. */
22058 tmpl = DECL_TI_TEMPLATE (tmpl);
22061 return tmpl;
22064 /* Returns true if we need to instantiate this template instance even if we
22065 know we aren't going to emit it. */
22067 bool
22068 always_instantiate_p (tree decl)
22070 /* We always instantiate inline functions so that we can inline them. An
22071 explicit instantiation declaration prohibits implicit instantiation of
22072 non-inline functions. With high levels of optimization, we would
22073 normally inline non-inline functions -- but we're not allowed to do
22074 that for "extern template" functions. Therefore, we check
22075 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22076 return ((TREE_CODE (decl) == FUNCTION_DECL
22077 && (DECL_DECLARED_INLINE_P (decl)
22078 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22079 /* And we need to instantiate static data members so that
22080 their initializers are available in integral constant
22081 expressions. */
22082 || (VAR_P (decl)
22083 && decl_maybe_constant_var_p (decl)));
22086 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22087 instantiate it now, modifying TREE_TYPE (fn). */
22089 void
22090 maybe_instantiate_noexcept (tree fn)
22092 tree fntype, spec, noex, clone;
22094 /* Don't instantiate a noexcept-specification from template context. */
22095 if (processing_template_decl)
22096 return;
22098 if (DECL_CLONED_FUNCTION_P (fn))
22099 fn = DECL_CLONED_FUNCTION (fn);
22100 fntype = TREE_TYPE (fn);
22101 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22103 if (!spec || !TREE_PURPOSE (spec))
22104 return;
22106 noex = TREE_PURPOSE (spec);
22108 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22110 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22111 spec = get_defaulted_eh_spec (fn);
22112 else if (push_tinst_level (fn))
22114 push_access_scope (fn);
22115 push_deferring_access_checks (dk_no_deferred);
22116 input_location = DECL_SOURCE_LOCATION (fn);
22117 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22118 DEFERRED_NOEXCEPT_ARGS (noex),
22119 tf_warning_or_error, fn,
22120 /*function_p=*/false,
22121 /*integral_constant_expression_p=*/true);
22122 pop_deferring_access_checks ();
22123 pop_access_scope (fn);
22124 pop_tinst_level ();
22125 spec = build_noexcept_spec (noex, tf_warning_or_error);
22126 if (spec == error_mark_node)
22127 spec = noexcept_false_spec;
22129 else
22130 spec = noexcept_false_spec;
22132 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22135 FOR_EACH_CLONE (clone, fn)
22137 if (TREE_TYPE (clone) == fntype)
22138 TREE_TYPE (clone) = TREE_TYPE (fn);
22139 else
22140 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22144 /* Produce the definition of D, a _DECL generated from a template. If
22145 DEFER_OK is nonzero, then we don't have to actually do the
22146 instantiation now; we just have to do it sometime. Normally it is
22147 an error if this is an explicit instantiation but D is undefined.
22148 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
22149 explicitly instantiated class template. */
22151 tree
22152 instantiate_decl (tree d, int defer_ok,
22153 bool expl_inst_class_mem_p)
22155 tree tmpl = DECL_TI_TEMPLATE (d);
22156 tree gen_args;
22157 tree args;
22158 tree td;
22159 tree code_pattern;
22160 tree spec;
22161 tree gen_tmpl;
22162 bool pattern_defined;
22163 location_t saved_loc = input_location;
22164 int saved_unevaluated_operand = cp_unevaluated_operand;
22165 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22166 bool external_p;
22167 bool deleted_p;
22168 tree fn_context;
22169 bool nested = false;
22171 /* This function should only be used to instantiate templates for
22172 functions and static member variables. */
22173 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22175 /* A concept is never instantiated. */
22176 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22178 /* Variables are never deferred; if instantiation is required, they
22179 are instantiated right away. That allows for better code in the
22180 case that an expression refers to the value of the variable --
22181 if the variable has a constant value the referring expression can
22182 take advantage of that fact. */
22183 if (VAR_P (d))
22184 defer_ok = 0;
22186 /* Don't instantiate cloned functions. Instead, instantiate the
22187 functions they cloned. */
22188 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22189 d = DECL_CLONED_FUNCTION (d);
22191 if (DECL_TEMPLATE_INSTANTIATED (d)
22192 || (TREE_CODE (d) == FUNCTION_DECL
22193 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22194 || DECL_TEMPLATE_SPECIALIZATION (d))
22195 /* D has already been instantiated or explicitly specialized, so
22196 there's nothing for us to do here.
22198 It might seem reasonable to check whether or not D is an explicit
22199 instantiation, and, if so, stop here. But when an explicit
22200 instantiation is deferred until the end of the compilation,
22201 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22202 the instantiation. */
22203 return d;
22205 /* Check to see whether we know that this template will be
22206 instantiated in some other file, as with "extern template"
22207 extension. */
22208 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22210 /* In general, we do not instantiate such templates. */
22211 if (external_p && !always_instantiate_p (d))
22212 return d;
22214 gen_tmpl = most_general_template (tmpl);
22215 gen_args = DECL_TI_ARGS (d);
22217 if (tmpl != gen_tmpl)
22218 /* We should already have the extra args. */
22219 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22220 == TMPL_ARGS_DEPTH (gen_args));
22221 /* And what's in the hash table should match D. */
22222 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22223 || spec == NULL_TREE);
22225 /* This needs to happen before any tsubsting. */
22226 if (! push_tinst_level (d))
22227 return d;
22229 timevar_push (TV_TEMPLATE_INST);
22231 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22232 for the instantiation. */
22233 td = template_for_substitution (d);
22234 args = gen_args;
22236 if (VAR_P (d))
22238 /* Look up an explicit specialization, if any. */
22239 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22240 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22241 if (elt && elt != error_mark_node)
22243 td = TREE_VALUE (elt);
22244 args = TREE_PURPOSE (elt);
22248 code_pattern = DECL_TEMPLATE_RESULT (td);
22250 /* We should never be trying to instantiate a member of a class
22251 template or partial specialization. */
22252 gcc_assert (d != code_pattern);
22254 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22255 || DECL_TEMPLATE_SPECIALIZATION (td))
22256 /* In the case of a friend template whose definition is provided
22257 outside the class, we may have too many arguments. Drop the
22258 ones we don't need. The same is true for specializations. */
22259 args = get_innermost_template_args
22260 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22262 if (TREE_CODE (d) == FUNCTION_DECL)
22264 deleted_p = DECL_DELETED_FN (code_pattern);
22265 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22266 && DECL_INITIAL (code_pattern) != error_mark_node)
22267 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22268 || deleted_p);
22270 else
22272 deleted_p = false;
22273 if (DECL_CLASS_SCOPE_P (code_pattern))
22274 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22275 || DECL_INLINE_VAR_P (code_pattern));
22276 else
22277 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22280 /* We may be in the middle of deferred access check. Disable it now. */
22281 push_deferring_access_checks (dk_no_deferred);
22283 /* Unless an explicit instantiation directive has already determined
22284 the linkage of D, remember that a definition is available for
22285 this entity. */
22286 if (pattern_defined
22287 && !DECL_INTERFACE_KNOWN (d)
22288 && !DECL_NOT_REALLY_EXTERN (d))
22289 mark_definable (d);
22291 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22292 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22293 input_location = DECL_SOURCE_LOCATION (d);
22295 /* If D is a member of an explicitly instantiated class template,
22296 and no definition is available, treat it like an implicit
22297 instantiation. */
22298 if (!pattern_defined && expl_inst_class_mem_p
22299 && DECL_EXPLICIT_INSTANTIATION (d))
22301 /* Leave linkage flags alone on instantiations with anonymous
22302 visibility. */
22303 if (TREE_PUBLIC (d))
22305 DECL_NOT_REALLY_EXTERN (d) = 0;
22306 DECL_INTERFACE_KNOWN (d) = 0;
22308 SET_DECL_IMPLICIT_INSTANTIATION (d);
22311 /* Defer all other templates, unless we have been explicitly
22312 forbidden from doing so. */
22313 if (/* If there is no definition, we cannot instantiate the
22314 template. */
22315 ! pattern_defined
22316 /* If it's OK to postpone instantiation, do so. */
22317 || defer_ok
22318 /* If this is a static data member that will be defined
22319 elsewhere, we don't want to instantiate the entire data
22320 member, but we do want to instantiate the initializer so that
22321 we can substitute that elsewhere. */
22322 || (external_p && VAR_P (d))
22323 /* Handle here a deleted function too, avoid generating
22324 its body (c++/61080). */
22325 || deleted_p)
22327 /* The definition of the static data member is now required so
22328 we must substitute the initializer. */
22329 if (VAR_P (d)
22330 && !DECL_INITIAL (d)
22331 && DECL_INITIAL (code_pattern))
22333 tree ns;
22334 tree init;
22335 bool const_init = false;
22336 bool enter_context = DECL_CLASS_SCOPE_P (d);
22338 ns = decl_namespace_context (d);
22339 push_nested_namespace (ns);
22340 if (enter_context)
22341 push_nested_class (DECL_CONTEXT (d));
22342 init = tsubst_expr (DECL_INITIAL (code_pattern),
22343 args,
22344 tf_warning_or_error, NULL_TREE,
22345 /*integral_constant_expression_p=*/false);
22346 /* If instantiating the initializer involved instantiating this
22347 again, don't call cp_finish_decl twice. */
22348 if (!DECL_INITIAL (d))
22350 /* Make sure the initializer is still constant, in case of
22351 circular dependency (template/instantiate6.C). */
22352 const_init
22353 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22354 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22355 /*asmspec_tree=*/NULL_TREE,
22356 LOOKUP_ONLYCONVERTING);
22358 if (enter_context)
22359 pop_nested_class ();
22360 pop_nested_namespace (ns);
22363 /* We restore the source position here because it's used by
22364 add_pending_template. */
22365 input_location = saved_loc;
22367 if (at_eof && !pattern_defined
22368 && DECL_EXPLICIT_INSTANTIATION (d)
22369 && DECL_NOT_REALLY_EXTERN (d))
22370 /* [temp.explicit]
22372 The definition of a non-exported function template, a
22373 non-exported member function template, or a non-exported
22374 member function or static data member of a class template
22375 shall be present in every translation unit in which it is
22376 explicitly instantiated. */
22377 permerror (input_location, "explicit instantiation of %qD "
22378 "but no definition available", d);
22380 /* If we're in unevaluated context, we just wanted to get the
22381 constant value; this isn't an odr use, so don't queue
22382 a full instantiation. */
22383 if (cp_unevaluated_operand != 0)
22384 goto out;
22385 /* ??? Historically, we have instantiated inline functions, even
22386 when marked as "extern template". */
22387 if (!(external_p && VAR_P (d)))
22388 add_pending_template (d);
22389 goto out;
22391 /* Tell the repository that D is available in this translation unit
22392 -- and see if it is supposed to be instantiated here. */
22393 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22395 /* In a PCH file, despite the fact that the repository hasn't
22396 requested instantiation in the PCH it is still possible that
22397 an instantiation will be required in a file that includes the
22398 PCH. */
22399 if (pch_file)
22400 add_pending_template (d);
22401 /* Instantiate inline functions so that the inliner can do its
22402 job, even though we'll not be emitting a copy of this
22403 function. */
22404 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22405 goto out;
22408 fn_context = decl_function_context (d);
22409 nested = (current_function_decl != NULL_TREE);
22410 vec<tree> omp_privatization_save;
22411 if (nested)
22412 save_omp_privatization_clauses (omp_privatization_save);
22414 if (!fn_context)
22415 push_to_top_level ();
22416 else
22418 if (nested)
22419 push_function_context ();
22420 cp_unevaluated_operand = 0;
22421 c_inhibit_evaluation_warnings = 0;
22424 /* Mark D as instantiated so that recursive calls to
22425 instantiate_decl do not try to instantiate it again. */
22426 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22428 /* Regenerate the declaration in case the template has been modified
22429 by a subsequent redeclaration. */
22430 regenerate_decl_from_template (d, td, args);
22432 /* We already set the file and line above. Reset them now in case
22433 they changed as a result of calling regenerate_decl_from_template. */
22434 input_location = DECL_SOURCE_LOCATION (d);
22436 if (VAR_P (d))
22438 tree init;
22439 bool const_init = false;
22441 /* Clear out DECL_RTL; whatever was there before may not be right
22442 since we've reset the type of the declaration. */
22443 SET_DECL_RTL (d, NULL);
22444 DECL_IN_AGGR_P (d) = 0;
22446 /* The initializer is placed in DECL_INITIAL by
22447 regenerate_decl_from_template so we don't need to
22448 push/pop_access_scope again here. Pull it out so that
22449 cp_finish_decl can process it. */
22450 init = DECL_INITIAL (d);
22451 DECL_INITIAL (d) = NULL_TREE;
22452 DECL_INITIALIZED_P (d) = 0;
22454 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22455 initializer. That function will defer actual emission until
22456 we have a chance to determine linkage. */
22457 DECL_EXTERNAL (d) = 0;
22459 /* Enter the scope of D so that access-checking works correctly. */
22460 bool enter_context = DECL_CLASS_SCOPE_P (d);
22461 if (enter_context)
22462 push_nested_class (DECL_CONTEXT (d));
22464 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22465 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22467 if (enter_context)
22468 pop_nested_class ();
22470 if (variable_template_p (gen_tmpl))
22471 note_variable_template_instantiation (d);
22473 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22474 synthesize_method (d);
22475 else if (TREE_CODE (d) == FUNCTION_DECL)
22477 hash_map<tree, tree> *saved_local_specializations;
22478 tree tmpl_parm;
22479 tree spec_parm;
22480 tree block = NULL_TREE;
22482 /* Save away the current list, in case we are instantiating one
22483 template from within the body of another. */
22484 saved_local_specializations = local_specializations;
22486 /* Set up the list of local specializations. */
22487 local_specializations = new hash_map<tree, tree>;
22489 /* Set up context. */
22490 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22491 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22492 block = push_stmt_list ();
22493 else
22494 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22496 /* Some typedefs referenced from within the template code need to be
22497 access checked at template instantiation time, i.e now. These
22498 types were added to the template at parsing time. Let's get those
22499 and perform the access checks then. */
22500 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22501 args);
22503 /* Create substitution entries for the parameters. */
22504 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22505 spec_parm = DECL_ARGUMENTS (d);
22506 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22508 register_local_specialization (spec_parm, tmpl_parm);
22509 spec_parm = skip_artificial_parms_for (d, spec_parm);
22510 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22512 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22514 if (!DECL_PACK_P (tmpl_parm))
22516 register_local_specialization (spec_parm, tmpl_parm);
22517 spec_parm = DECL_CHAIN (spec_parm);
22519 else
22521 /* Register the (value) argument pack as a specialization of
22522 TMPL_PARM, then move on. */
22523 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22524 register_local_specialization (argpack, tmpl_parm);
22527 gcc_assert (!spec_parm);
22529 /* Substitute into the body of the function. */
22530 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22531 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22532 tf_warning_or_error, tmpl);
22533 else
22535 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22536 tf_warning_or_error, tmpl,
22537 /*integral_constant_expression_p=*/false);
22539 /* Set the current input_location to the end of the function
22540 so that finish_function knows where we are. */
22541 input_location
22542 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22544 /* Remember if we saw an infinite loop in the template. */
22545 current_function_infinite_loop
22546 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22549 /* We don't need the local specializations any more. */
22550 delete local_specializations;
22551 local_specializations = saved_local_specializations;
22553 /* Finish the function. */
22554 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22555 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22556 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22557 else
22559 d = finish_function (0);
22560 expand_or_defer_fn (d);
22563 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22564 cp_check_omp_declare_reduction (d);
22567 /* We're not deferring instantiation any more. */
22568 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22570 if (!fn_context)
22571 pop_from_top_level ();
22572 else if (nested)
22573 pop_function_context ();
22575 out:
22576 input_location = saved_loc;
22577 cp_unevaluated_operand = saved_unevaluated_operand;
22578 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22579 pop_deferring_access_checks ();
22580 pop_tinst_level ();
22581 if (nested)
22582 restore_omp_privatization_clauses (omp_privatization_save);
22584 timevar_pop (TV_TEMPLATE_INST);
22586 return d;
22589 /* Run through the list of templates that we wish we could
22590 instantiate, and instantiate any we can. RETRIES is the
22591 number of times we retry pending template instantiation. */
22593 void
22594 instantiate_pending_templates (int retries)
22596 int reconsider;
22597 location_t saved_loc = input_location;
22599 /* Instantiating templates may trigger vtable generation. This in turn
22600 may require further template instantiations. We place a limit here
22601 to avoid infinite loop. */
22602 if (pending_templates && retries >= max_tinst_depth)
22604 tree decl = pending_templates->tinst->decl;
22606 fatal_error (input_location,
22607 "template instantiation depth exceeds maximum of %d"
22608 " instantiating %q+D, possibly from virtual table generation"
22609 " (use -ftemplate-depth= to increase the maximum)",
22610 max_tinst_depth, decl);
22611 if (TREE_CODE (decl) == FUNCTION_DECL)
22612 /* Pretend that we defined it. */
22613 DECL_INITIAL (decl) = error_mark_node;
22614 return;
22619 struct pending_template **t = &pending_templates;
22620 struct pending_template *last = NULL;
22621 reconsider = 0;
22622 while (*t)
22624 tree instantiation = reopen_tinst_level ((*t)->tinst);
22625 bool complete = false;
22627 if (TYPE_P (instantiation))
22629 tree fn;
22631 if (!COMPLETE_TYPE_P (instantiation))
22633 instantiate_class_template (instantiation);
22634 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22635 for (fn = TYPE_METHODS (instantiation);
22637 fn = TREE_CHAIN (fn))
22638 if (! DECL_ARTIFICIAL (fn))
22639 instantiate_decl (fn,
22640 /*defer_ok=*/0,
22641 /*expl_inst_class_mem_p=*/false);
22642 if (COMPLETE_TYPE_P (instantiation))
22643 reconsider = 1;
22646 complete = COMPLETE_TYPE_P (instantiation);
22648 else
22650 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22651 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22653 instantiation
22654 = instantiate_decl (instantiation,
22655 /*defer_ok=*/0,
22656 /*expl_inst_class_mem_p=*/false);
22657 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22658 reconsider = 1;
22661 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22662 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22665 if (complete)
22666 /* If INSTANTIATION has been instantiated, then we don't
22667 need to consider it again in the future. */
22668 *t = (*t)->next;
22669 else
22671 last = *t;
22672 t = &(*t)->next;
22674 tinst_depth = 0;
22675 current_tinst_level = NULL;
22677 last_pending_template = last;
22679 while (reconsider);
22681 input_location = saved_loc;
22684 /* Substitute ARGVEC into T, which is a list of initializers for
22685 either base class or a non-static data member. The TREE_PURPOSEs
22686 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22687 instantiate_decl. */
22689 static tree
22690 tsubst_initializer_list (tree t, tree argvec)
22692 tree inits = NULL_TREE;
22694 for (; t; t = TREE_CHAIN (t))
22696 tree decl;
22697 tree init;
22698 tree expanded_bases = NULL_TREE;
22699 tree expanded_arguments = NULL_TREE;
22700 int i, len = 1;
22702 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
22704 tree expr;
22705 tree arg;
22707 /* Expand the base class expansion type into separate base
22708 classes. */
22709 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
22710 tf_warning_or_error,
22711 NULL_TREE);
22712 if (expanded_bases == error_mark_node)
22713 continue;
22715 /* We'll be building separate TREE_LISTs of arguments for
22716 each base. */
22717 len = TREE_VEC_LENGTH (expanded_bases);
22718 expanded_arguments = make_tree_vec (len);
22719 for (i = 0; i < len; i++)
22720 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
22722 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22723 expand each argument in the TREE_VALUE of t. */
22724 expr = make_node (EXPR_PACK_EXPANSION);
22725 PACK_EXPANSION_LOCAL_P (expr) = true;
22726 PACK_EXPANSION_PARAMETER_PACKS (expr) =
22727 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
22729 if (TREE_VALUE (t) == void_type_node)
22730 /* VOID_TYPE_NODE is used to indicate
22731 value-initialization. */
22733 for (i = 0; i < len; i++)
22734 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
22736 else
22738 /* Substitute parameter packs into each argument in the
22739 TREE_LIST. */
22740 in_base_initializer = 1;
22741 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
22743 tree expanded_exprs;
22745 /* Expand the argument. */
22746 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
22747 expanded_exprs
22748 = tsubst_pack_expansion (expr, argvec,
22749 tf_warning_or_error,
22750 NULL_TREE);
22751 if (expanded_exprs == error_mark_node)
22752 continue;
22754 /* Prepend each of the expanded expressions to the
22755 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22756 for (i = 0; i < len; i++)
22758 TREE_VEC_ELT (expanded_arguments, i) =
22759 tree_cons (NULL_TREE,
22760 TREE_VEC_ELT (expanded_exprs, i),
22761 TREE_VEC_ELT (expanded_arguments, i));
22764 in_base_initializer = 0;
22766 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22767 since we built them backwards. */
22768 for (i = 0; i < len; i++)
22770 TREE_VEC_ELT (expanded_arguments, i) =
22771 nreverse (TREE_VEC_ELT (expanded_arguments, i));
22776 for (i = 0; i < len; ++i)
22778 if (expanded_bases)
22780 decl = TREE_VEC_ELT (expanded_bases, i);
22781 decl = expand_member_init (decl);
22782 init = TREE_VEC_ELT (expanded_arguments, i);
22784 else
22786 tree tmp;
22787 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
22788 tf_warning_or_error, NULL_TREE);
22790 decl = expand_member_init (decl);
22791 if (decl && !DECL_P (decl))
22792 in_base_initializer = 1;
22794 init = TREE_VALUE (t);
22795 tmp = init;
22796 if (init != void_type_node)
22797 init = tsubst_expr (init, argvec,
22798 tf_warning_or_error, NULL_TREE,
22799 /*integral_constant_expression_p=*/false);
22800 if (init == NULL_TREE && tmp != NULL_TREE)
22801 /* If we had an initializer but it instantiated to nothing,
22802 value-initialize the object. This will only occur when
22803 the initializer was a pack expansion where the parameter
22804 packs used in that expansion were of length zero. */
22805 init = void_type_node;
22806 in_base_initializer = 0;
22809 if (decl)
22811 init = build_tree_list (decl, init);
22812 TREE_CHAIN (init) = inits;
22813 inits = init;
22817 return inits;
22820 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22822 static void
22823 set_current_access_from_decl (tree decl)
22825 if (TREE_PRIVATE (decl))
22826 current_access_specifier = access_private_node;
22827 else if (TREE_PROTECTED (decl))
22828 current_access_specifier = access_protected_node;
22829 else
22830 current_access_specifier = access_public_node;
22833 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22834 is the instantiation (which should have been created with
22835 start_enum) and ARGS are the template arguments to use. */
22837 static void
22838 tsubst_enum (tree tag, tree newtag, tree args)
22840 tree e;
22842 if (SCOPED_ENUM_P (newtag))
22843 begin_scope (sk_scoped_enum, newtag);
22845 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
22847 tree value;
22848 tree decl;
22850 decl = TREE_VALUE (e);
22851 /* Note that in a template enum, the TREE_VALUE is the
22852 CONST_DECL, not the corresponding INTEGER_CST. */
22853 value = tsubst_expr (DECL_INITIAL (decl),
22854 args, tf_warning_or_error, NULL_TREE,
22855 /*integral_constant_expression_p=*/true);
22857 /* Give this enumeration constant the correct access. */
22858 set_current_access_from_decl (decl);
22860 /* Actually build the enumerator itself. Here we're assuming that
22861 enumerators can't have dependent attributes. */
22862 build_enumerator (DECL_NAME (decl), value, newtag,
22863 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
22866 if (SCOPED_ENUM_P (newtag))
22867 finish_scope ();
22869 finish_enum_value_list (newtag);
22870 finish_enum (newtag);
22872 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
22873 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
22876 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22877 its type -- but without substituting the innermost set of template
22878 arguments. So, innermost set of template parameters will appear in
22879 the type. */
22881 tree
22882 get_mostly_instantiated_function_type (tree decl)
22884 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22885 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
22888 /* Return truthvalue if we're processing a template different from
22889 the last one involved in diagnostics. */
22890 bool
22891 problematic_instantiation_changed (void)
22893 return current_tinst_level != last_error_tinst_level;
22896 /* Remember current template involved in diagnostics. */
22897 void
22898 record_last_problematic_instantiation (void)
22900 last_error_tinst_level = current_tinst_level;
22903 struct tinst_level *
22904 current_instantiation (void)
22906 return current_tinst_level;
22909 /* Return TRUE if current_function_decl is being instantiated, false
22910 otherwise. */
22912 bool
22913 instantiating_current_function_p (void)
22915 return (current_instantiation ()
22916 && current_instantiation ()->decl == current_function_decl);
22919 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22920 type. Return zero for ok, nonzero for disallowed. Issue error and
22921 warning messages under control of COMPLAIN. */
22923 static int
22924 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
22926 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
22927 return 0;
22928 else if (POINTER_TYPE_P (type))
22929 return 0;
22930 else if (TYPE_PTRMEM_P (type))
22931 return 0;
22932 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
22933 return 0;
22934 else if (TREE_CODE (type) == TYPENAME_TYPE)
22935 return 0;
22936 else if (TREE_CODE (type) == DECLTYPE_TYPE)
22937 return 0;
22938 else if (TREE_CODE (type) == NULLPTR_TYPE)
22939 return 0;
22940 /* A bound template template parm could later be instantiated to have a valid
22941 nontype parm type via an alias template. */
22942 else if (cxx_dialect >= cxx11
22943 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22944 return 0;
22946 if (complain & tf_error)
22948 if (type == error_mark_node)
22949 inform (input_location, "invalid template non-type parameter");
22950 else
22951 error ("%q#T is not a valid type for a template non-type parameter",
22952 type);
22954 return 1;
22957 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22958 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22960 static bool
22961 dependent_type_p_r (tree type)
22963 tree scope;
22965 /* [temp.dep.type]
22967 A type is dependent if it is:
22969 -- a template parameter. Template template parameters are types
22970 for us (since TYPE_P holds true for them) so we handle
22971 them here. */
22972 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22973 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
22974 return true;
22975 /* -- a qualified-id with a nested-name-specifier which contains a
22976 class-name that names a dependent type or whose unqualified-id
22977 names a dependent type. */
22978 if (TREE_CODE (type) == TYPENAME_TYPE)
22979 return true;
22981 /* An alias template specialization can be dependent even if the
22982 resulting type is not. */
22983 if (dependent_alias_template_spec_p (type))
22984 return true;
22986 /* -- a cv-qualified type where the cv-unqualified type is
22987 dependent.
22988 No code is necessary for this bullet; the code below handles
22989 cv-qualified types, and we don't want to strip aliases with
22990 TYPE_MAIN_VARIANT because of DR 1558. */
22991 /* -- a compound type constructed from any dependent type. */
22992 if (TYPE_PTRMEM_P (type))
22993 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
22994 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22995 (type)));
22996 else if (TYPE_PTR_P (type)
22997 || TREE_CODE (type) == REFERENCE_TYPE)
22998 return dependent_type_p (TREE_TYPE (type));
22999 else if (TREE_CODE (type) == FUNCTION_TYPE
23000 || TREE_CODE (type) == METHOD_TYPE)
23002 tree arg_type;
23004 if (dependent_type_p (TREE_TYPE (type)))
23005 return true;
23006 for (arg_type = TYPE_ARG_TYPES (type);
23007 arg_type;
23008 arg_type = TREE_CHAIN (arg_type))
23009 if (dependent_type_p (TREE_VALUE (arg_type)))
23010 return true;
23011 return false;
23013 /* -- an array type constructed from any dependent type or whose
23014 size is specified by a constant expression that is
23015 value-dependent.
23017 We checked for type- and value-dependence of the bounds in
23018 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23019 if (TREE_CODE (type) == ARRAY_TYPE)
23021 if (TYPE_DOMAIN (type)
23022 && dependent_type_p (TYPE_DOMAIN (type)))
23023 return true;
23024 return dependent_type_p (TREE_TYPE (type));
23027 /* -- a template-id in which either the template name is a template
23028 parameter ... */
23029 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23030 return true;
23031 /* ... or any of the template arguments is a dependent type or
23032 an expression that is type-dependent or value-dependent. */
23033 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23034 && (any_dependent_template_arguments_p
23035 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23036 return true;
23038 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23039 dependent; if the argument of the `typeof' expression is not
23040 type-dependent, then it should already been have resolved. */
23041 if (TREE_CODE (type) == TYPEOF_TYPE
23042 || TREE_CODE (type) == DECLTYPE_TYPE
23043 || TREE_CODE (type) == UNDERLYING_TYPE)
23044 return true;
23046 /* A template argument pack is dependent if any of its packed
23047 arguments are. */
23048 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23050 tree args = ARGUMENT_PACK_ARGS (type);
23051 int i, len = TREE_VEC_LENGTH (args);
23052 for (i = 0; i < len; ++i)
23053 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23054 return true;
23057 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23058 be template parameters. */
23059 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23060 return true;
23062 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23063 return true;
23065 /* The standard does not specifically mention types that are local
23066 to template functions or local classes, but they should be
23067 considered dependent too. For example:
23069 template <int I> void f() {
23070 enum E { a = I };
23071 S<sizeof (E)> s;
23074 The size of `E' cannot be known until the value of `I' has been
23075 determined. Therefore, `E' must be considered dependent. */
23076 scope = TYPE_CONTEXT (type);
23077 if (scope && TYPE_P (scope))
23078 return dependent_type_p (scope);
23079 /* Don't use type_dependent_expression_p here, as it can lead
23080 to infinite recursion trying to determine whether a lambda
23081 nested in a lambda is dependent (c++/47687). */
23082 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23083 && DECL_LANG_SPECIFIC (scope)
23084 && DECL_TEMPLATE_INFO (scope)
23085 && (any_dependent_template_arguments_p
23086 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23087 return true;
23089 /* Other types are non-dependent. */
23090 return false;
23093 /* Returns TRUE if TYPE is dependent, in the sense of
23094 [temp.dep.type]. Note that a NULL type is considered dependent. */
23096 bool
23097 dependent_type_p (tree type)
23099 /* If there are no template parameters in scope, then there can't be
23100 any dependent types. */
23101 if (!processing_template_decl)
23103 /* If we are not processing a template, then nobody should be
23104 providing us with a dependent type. */
23105 gcc_assert (type);
23106 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23107 return false;
23110 /* If the type is NULL, we have not computed a type for the entity
23111 in question; in that case, the type is dependent. */
23112 if (!type)
23113 return true;
23115 /* Erroneous types can be considered non-dependent. */
23116 if (type == error_mark_node)
23117 return false;
23119 /* If we have not already computed the appropriate value for TYPE,
23120 do so now. */
23121 if (!TYPE_DEPENDENT_P_VALID (type))
23123 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23124 TYPE_DEPENDENT_P_VALID (type) = 1;
23127 return TYPE_DEPENDENT_P (type);
23130 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23131 lookup. In other words, a dependent type that is not the current
23132 instantiation. */
23134 bool
23135 dependent_scope_p (tree scope)
23137 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23138 && !currently_open_class (scope));
23141 /* T is a SCOPE_REF; return whether we need to consider it
23142 instantiation-dependent so that we can check access at instantiation
23143 time even though we know which member it resolves to. */
23145 static bool
23146 instantiation_dependent_scope_ref_p (tree t)
23148 if (DECL_P (TREE_OPERAND (t, 1))
23149 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23150 && accessible_in_template_p (TREE_OPERAND (t, 0),
23151 TREE_OPERAND (t, 1)))
23152 return false;
23153 else
23154 return true;
23157 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23158 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23159 expression. */
23161 /* Note that this predicate is not appropriate for general expressions;
23162 only constant expressions (that satisfy potential_constant_expression)
23163 can be tested for value dependence. */
23165 bool
23166 value_dependent_expression_p (tree expression)
23168 if (!processing_template_decl || expression == NULL_TREE)
23169 return false;
23171 /* A name declared with a dependent type. */
23172 if (DECL_P (expression) && type_dependent_expression_p (expression))
23173 return true;
23175 switch (TREE_CODE (expression))
23177 case BASELINK:
23178 /* A dependent member function of the current instantiation. */
23179 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23181 case FUNCTION_DECL:
23182 /* A dependent member function of the current instantiation. */
23183 if (DECL_CLASS_SCOPE_P (expression)
23184 && dependent_type_p (DECL_CONTEXT (expression)))
23185 return true;
23186 break;
23188 case IDENTIFIER_NODE:
23189 /* A name that has not been looked up -- must be dependent. */
23190 return true;
23192 case TEMPLATE_PARM_INDEX:
23193 /* A non-type template parm. */
23194 return true;
23196 case CONST_DECL:
23197 /* A non-type template parm. */
23198 if (DECL_TEMPLATE_PARM_P (expression))
23199 return true;
23200 return value_dependent_expression_p (DECL_INITIAL (expression));
23202 case VAR_DECL:
23203 /* A constant with literal type and is initialized
23204 with an expression that is value-dependent.
23206 Note that a non-dependent parenthesized initializer will have
23207 already been replaced with its constant value, so if we see
23208 a TREE_LIST it must be dependent. */
23209 if (DECL_INITIAL (expression)
23210 && decl_constant_var_p (expression)
23211 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23212 /* cp_finish_decl doesn't fold reference initializers. */
23213 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23214 || type_dependent_expression_p (DECL_INITIAL (expression))
23215 || value_dependent_expression_p (DECL_INITIAL (expression))))
23216 return true;
23217 return false;
23219 case DYNAMIC_CAST_EXPR:
23220 case STATIC_CAST_EXPR:
23221 case CONST_CAST_EXPR:
23222 case REINTERPRET_CAST_EXPR:
23223 case CAST_EXPR:
23224 /* These expressions are value-dependent if the type to which
23225 the cast occurs is dependent or the expression being casted
23226 is value-dependent. */
23228 tree type = TREE_TYPE (expression);
23230 if (dependent_type_p (type))
23231 return true;
23233 /* A functional cast has a list of operands. */
23234 expression = TREE_OPERAND (expression, 0);
23235 if (!expression)
23237 /* If there are no operands, it must be an expression such
23238 as "int()". This should not happen for aggregate types
23239 because it would form non-constant expressions. */
23240 gcc_assert (cxx_dialect >= cxx11
23241 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23243 return false;
23246 if (TREE_CODE (expression) == TREE_LIST)
23247 return any_value_dependent_elements_p (expression);
23249 return value_dependent_expression_p (expression);
23252 case SIZEOF_EXPR:
23253 if (SIZEOF_EXPR_TYPE_P (expression))
23254 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23255 /* FALLTHRU */
23256 case ALIGNOF_EXPR:
23257 case TYPEID_EXPR:
23258 /* A `sizeof' expression is value-dependent if the operand is
23259 type-dependent or is a pack expansion. */
23260 expression = TREE_OPERAND (expression, 0);
23261 if (PACK_EXPANSION_P (expression))
23262 return true;
23263 else if (TYPE_P (expression))
23264 return dependent_type_p (expression);
23265 return instantiation_dependent_uneval_expression_p (expression);
23267 case AT_ENCODE_EXPR:
23268 /* An 'encode' expression is value-dependent if the operand is
23269 type-dependent. */
23270 expression = TREE_OPERAND (expression, 0);
23271 return dependent_type_p (expression);
23273 case NOEXCEPT_EXPR:
23274 expression = TREE_OPERAND (expression, 0);
23275 return instantiation_dependent_uneval_expression_p (expression);
23277 case SCOPE_REF:
23278 /* All instantiation-dependent expressions should also be considered
23279 value-dependent. */
23280 return instantiation_dependent_scope_ref_p (expression);
23282 case COMPONENT_REF:
23283 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23284 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23286 case NONTYPE_ARGUMENT_PACK:
23287 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23288 is value-dependent. */
23290 tree values = ARGUMENT_PACK_ARGS (expression);
23291 int i, len = TREE_VEC_LENGTH (values);
23293 for (i = 0; i < len; ++i)
23294 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23295 return true;
23297 return false;
23300 case TRAIT_EXPR:
23302 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23303 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23304 || (type2 ? dependent_type_p (type2) : false));
23307 case MODOP_EXPR:
23308 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23309 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23311 case ARRAY_REF:
23312 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23313 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23315 case ADDR_EXPR:
23317 tree op = TREE_OPERAND (expression, 0);
23318 return (value_dependent_expression_p (op)
23319 || has_value_dependent_address (op));
23322 case REQUIRES_EXPR:
23323 /* Treat all requires-expressions as value-dependent so
23324 we don't try to fold them. */
23325 return true;
23327 case TYPE_REQ:
23328 return dependent_type_p (TREE_OPERAND (expression, 0));
23330 case CALL_EXPR:
23332 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23333 return true;
23334 tree fn = get_callee_fndecl (expression);
23335 int i, nargs;
23336 nargs = call_expr_nargs (expression);
23337 for (i = 0; i < nargs; ++i)
23339 tree op = CALL_EXPR_ARG (expression, i);
23340 /* In a call to a constexpr member function, look through the
23341 implicit ADDR_EXPR on the object argument so that it doesn't
23342 cause the call to be considered value-dependent. We also
23343 look through it in potential_constant_expression. */
23344 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23345 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23346 && TREE_CODE (op) == ADDR_EXPR)
23347 op = TREE_OPERAND (op, 0);
23348 if (value_dependent_expression_p (op))
23349 return true;
23351 return false;
23354 case TEMPLATE_ID_EXPR:
23355 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23356 type-dependent. */
23357 return type_dependent_expression_p (expression)
23358 || variable_concept_p (TREE_OPERAND (expression, 0));
23360 case CONSTRUCTOR:
23362 unsigned ix;
23363 tree val;
23364 if (dependent_type_p (TREE_TYPE (expression)))
23365 return true;
23366 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23367 if (value_dependent_expression_p (val))
23368 return true;
23369 return false;
23372 case STMT_EXPR:
23373 /* Treat a GNU statement expression as dependent to avoid crashing
23374 under instantiate_non_dependent_expr; it can't be constant. */
23375 return true;
23377 default:
23378 /* A constant expression is value-dependent if any subexpression is
23379 value-dependent. */
23380 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23382 case tcc_reference:
23383 case tcc_unary:
23384 case tcc_comparison:
23385 case tcc_binary:
23386 case tcc_expression:
23387 case tcc_vl_exp:
23389 int i, len = cp_tree_operand_length (expression);
23391 for (i = 0; i < len; i++)
23393 tree t = TREE_OPERAND (expression, i);
23395 /* In some cases, some of the operands may be missing.l
23396 (For example, in the case of PREDECREMENT_EXPR, the
23397 amount to increment by may be missing.) That doesn't
23398 make the expression dependent. */
23399 if (t && value_dependent_expression_p (t))
23400 return true;
23403 break;
23404 default:
23405 break;
23407 break;
23410 /* The expression is not value-dependent. */
23411 return false;
23414 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23415 [temp.dep.expr]. Note that an expression with no type is
23416 considered dependent. Other parts of the compiler arrange for an
23417 expression with type-dependent subexpressions to have no type, so
23418 this function doesn't have to be fully recursive. */
23420 bool
23421 type_dependent_expression_p (tree expression)
23423 if (!processing_template_decl)
23424 return false;
23426 if (expression == NULL_TREE || expression == error_mark_node)
23427 return false;
23429 /* An unresolved name is always dependent. */
23430 if (identifier_p (expression)
23431 || TREE_CODE (expression) == USING_DECL
23432 || TREE_CODE (expression) == WILDCARD_DECL)
23433 return true;
23435 /* A fold expression is type-dependent. */
23436 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23437 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23438 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23439 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23440 return true;
23442 /* Some expression forms are never type-dependent. */
23443 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23444 || TREE_CODE (expression) == SIZEOF_EXPR
23445 || TREE_CODE (expression) == ALIGNOF_EXPR
23446 || TREE_CODE (expression) == AT_ENCODE_EXPR
23447 || TREE_CODE (expression) == NOEXCEPT_EXPR
23448 || TREE_CODE (expression) == TRAIT_EXPR
23449 || TREE_CODE (expression) == TYPEID_EXPR
23450 || TREE_CODE (expression) == DELETE_EXPR
23451 || TREE_CODE (expression) == VEC_DELETE_EXPR
23452 || TREE_CODE (expression) == THROW_EXPR
23453 || TREE_CODE (expression) == REQUIRES_EXPR)
23454 return false;
23456 /* The types of these expressions depends only on the type to which
23457 the cast occurs. */
23458 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23459 || TREE_CODE (expression) == STATIC_CAST_EXPR
23460 || TREE_CODE (expression) == CONST_CAST_EXPR
23461 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23462 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23463 || TREE_CODE (expression) == CAST_EXPR)
23464 return dependent_type_p (TREE_TYPE (expression));
23466 /* The types of these expressions depends only on the type created
23467 by the expression. */
23468 if (TREE_CODE (expression) == NEW_EXPR
23469 || TREE_CODE (expression) == VEC_NEW_EXPR)
23471 /* For NEW_EXPR tree nodes created inside a template, either
23472 the object type itself or a TREE_LIST may appear as the
23473 operand 1. */
23474 tree type = TREE_OPERAND (expression, 1);
23475 if (TREE_CODE (type) == TREE_LIST)
23476 /* This is an array type. We need to check array dimensions
23477 as well. */
23478 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23479 || value_dependent_expression_p
23480 (TREE_OPERAND (TREE_VALUE (type), 1));
23481 else
23482 return dependent_type_p (type);
23485 if (TREE_CODE (expression) == SCOPE_REF)
23487 tree scope = TREE_OPERAND (expression, 0);
23488 tree name = TREE_OPERAND (expression, 1);
23490 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23491 contains an identifier associated by name lookup with one or more
23492 declarations declared with a dependent type, or...a
23493 nested-name-specifier or qualified-id that names a member of an
23494 unknown specialization. */
23495 return (type_dependent_expression_p (name)
23496 || dependent_scope_p (scope));
23499 if (TREE_CODE (expression) == TEMPLATE_DECL
23500 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23501 return uses_outer_template_parms (expression);
23503 if (TREE_CODE (expression) == STMT_EXPR)
23504 expression = stmt_expr_value_expr (expression);
23506 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23508 tree elt;
23509 unsigned i;
23511 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23513 if (type_dependent_expression_p (elt))
23514 return true;
23516 return false;
23519 /* A static data member of the current instantiation with incomplete
23520 array type is type-dependent, as the definition and specializations
23521 can have different bounds. */
23522 if (VAR_P (expression)
23523 && DECL_CLASS_SCOPE_P (expression)
23524 && dependent_type_p (DECL_CONTEXT (expression))
23525 && VAR_HAD_UNKNOWN_BOUND (expression))
23526 return true;
23528 /* An array of unknown bound depending on a variadic parameter, eg:
23530 template<typename... Args>
23531 void foo (Args... args)
23533 int arr[] = { args... };
23536 template<int... vals>
23537 void bar ()
23539 int arr[] = { vals... };
23542 If the array has no length and has an initializer, it must be that
23543 we couldn't determine its length in cp_complete_array_type because
23544 it is dependent. */
23545 if (VAR_P (expression)
23546 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23547 && !TYPE_DOMAIN (TREE_TYPE (expression))
23548 && DECL_INITIAL (expression))
23549 return true;
23551 /* A function or variable template-id is type-dependent if it has any
23552 dependent template arguments. Note that we only consider the innermost
23553 template arguments here, since those are the ones that come from the
23554 template-id; the template arguments for the enclosing class do not make it
23555 type-dependent, they only make a member function value-dependent. */
23556 if (VAR_OR_FUNCTION_DECL_P (expression)
23557 && DECL_LANG_SPECIFIC (expression)
23558 && DECL_TEMPLATE_INFO (expression)
23559 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23560 && (any_dependent_template_arguments_p
23561 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23562 return true;
23564 /* Always dependent, on the number of arguments if nothing else. */
23565 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23566 return true;
23568 if (TREE_TYPE (expression) == unknown_type_node)
23570 if (TREE_CODE (expression) == ADDR_EXPR)
23571 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23572 if (TREE_CODE (expression) == COMPONENT_REF
23573 || TREE_CODE (expression) == OFFSET_REF)
23575 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23576 return true;
23577 expression = TREE_OPERAND (expression, 1);
23578 if (identifier_p (expression))
23579 return false;
23581 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23582 if (TREE_CODE (expression) == SCOPE_REF)
23583 return false;
23585 if (BASELINK_P (expression))
23587 if (BASELINK_OPTYPE (expression)
23588 && dependent_type_p (BASELINK_OPTYPE (expression)))
23589 return true;
23590 expression = BASELINK_FUNCTIONS (expression);
23593 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23595 if (any_dependent_template_arguments_p
23596 (TREE_OPERAND (expression, 1)))
23597 return true;
23598 expression = TREE_OPERAND (expression, 0);
23599 if (identifier_p (expression))
23600 return true;
23603 gcc_assert (TREE_CODE (expression) == OVERLOAD
23604 || TREE_CODE (expression) == FUNCTION_DECL);
23606 while (expression)
23608 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23609 return true;
23610 expression = OVL_NEXT (expression);
23612 return false;
23615 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23617 /* Dependent type attributes might not have made it from the decl to
23618 the type yet. */
23619 if (DECL_P (expression)
23620 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23621 return true;
23623 return (dependent_type_p (TREE_TYPE (expression)));
23626 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23627 type-dependent if the expression refers to a member of the current
23628 instantiation and the type of the referenced member is dependent, or the
23629 class member access expression refers to a member of an unknown
23630 specialization.
23632 This function returns true if the OBJECT in such a class member access
23633 expression is of an unknown specialization. */
23635 bool
23636 type_dependent_object_expression_p (tree object)
23638 tree scope = TREE_TYPE (object);
23639 return (!scope || dependent_scope_p (scope));
23642 /* walk_tree callback function for instantiation_dependent_expression_p,
23643 below. Returns non-zero if a dependent subexpression is found. */
23645 static tree
23646 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23647 void * /*data*/)
23649 if (TYPE_P (*tp))
23651 /* We don't have to worry about decltype currently because decltype
23652 of an instantiation-dependent expr is a dependent type. This
23653 might change depending on the resolution of DR 1172. */
23654 *walk_subtrees = false;
23655 return NULL_TREE;
23657 enum tree_code code = TREE_CODE (*tp);
23658 switch (code)
23660 /* Don't treat an argument list as dependent just because it has no
23661 TREE_TYPE. */
23662 case TREE_LIST:
23663 case TREE_VEC:
23664 return NULL_TREE;
23666 case TEMPLATE_PARM_INDEX:
23667 return *tp;
23669 /* Handle expressions with type operands. */
23670 case SIZEOF_EXPR:
23671 case ALIGNOF_EXPR:
23672 case TYPEID_EXPR:
23673 case AT_ENCODE_EXPR:
23675 tree op = TREE_OPERAND (*tp, 0);
23676 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
23677 op = TREE_TYPE (op);
23678 if (TYPE_P (op))
23680 if (dependent_type_p (op))
23681 return *tp;
23682 else
23684 *walk_subtrees = false;
23685 return NULL_TREE;
23688 break;
23691 case COMPONENT_REF:
23692 if (identifier_p (TREE_OPERAND (*tp, 1)))
23693 /* In a template, finish_class_member_access_expr creates a
23694 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
23695 type-dependent, so that we can check access control at
23696 instantiation time (PR 42277). See also Core issue 1273. */
23697 return *tp;
23698 break;
23700 case SCOPE_REF:
23701 if (instantiation_dependent_scope_ref_p (*tp))
23702 return *tp;
23703 else
23704 break;
23706 /* Treat statement-expressions as dependent. */
23707 case BIND_EXPR:
23708 return *tp;
23710 /* Treat requires-expressions as dependent. */
23711 case REQUIRES_EXPR:
23712 return *tp;
23714 case CALL_EXPR:
23715 /* Treat calls to function concepts as dependent. */
23716 if (function_concept_check_p (*tp))
23717 return *tp;
23718 break;
23720 case TEMPLATE_ID_EXPR:
23721 /* And variable concepts. */
23722 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
23723 return *tp;
23724 break;
23726 default:
23727 break;
23730 if (type_dependent_expression_p (*tp))
23731 return *tp;
23732 else
23733 return NULL_TREE;
23736 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23737 sense defined by the ABI:
23739 "An expression is instantiation-dependent if it is type-dependent
23740 or value-dependent, or it has a subexpression that is type-dependent
23741 or value-dependent."
23743 Except don't actually check value-dependence for unevaluated expressions,
23744 because in sizeof(i) we don't care about the value of i. Checking
23745 type-dependence will in turn check value-dependence of array bounds/template
23746 arguments as needed. */
23748 bool
23749 instantiation_dependent_uneval_expression_p (tree expression)
23751 tree result;
23753 if (!processing_template_decl)
23754 return false;
23756 if (expression == error_mark_node)
23757 return false;
23759 result = cp_walk_tree_without_duplicates (&expression,
23760 instantiation_dependent_r, NULL);
23761 return result != NULL_TREE;
23764 /* As above, but also check value-dependence of the expression as a whole. */
23766 bool
23767 instantiation_dependent_expression_p (tree expression)
23769 return (instantiation_dependent_uneval_expression_p (expression)
23770 || value_dependent_expression_p (expression));
23773 /* Like type_dependent_expression_p, but it also works while not processing
23774 a template definition, i.e. during substitution or mangling. */
23776 bool
23777 type_dependent_expression_p_push (tree expr)
23779 bool b;
23780 ++processing_template_decl;
23781 b = type_dependent_expression_p (expr);
23782 --processing_template_decl;
23783 return b;
23786 /* Returns TRUE if ARGS contains a type-dependent expression. */
23788 bool
23789 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
23791 unsigned int i;
23792 tree arg;
23794 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
23796 if (type_dependent_expression_p (arg))
23797 return true;
23799 return false;
23802 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23803 expressions) contains any type-dependent expressions. */
23805 bool
23806 any_type_dependent_elements_p (const_tree list)
23808 for (; list; list = TREE_CHAIN (list))
23809 if (type_dependent_expression_p (TREE_VALUE (list)))
23810 return true;
23812 return false;
23815 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23816 expressions) contains any value-dependent expressions. */
23818 bool
23819 any_value_dependent_elements_p (const_tree list)
23821 for (; list; list = TREE_CHAIN (list))
23822 if (value_dependent_expression_p (TREE_VALUE (list)))
23823 return true;
23825 return false;
23828 /* Returns TRUE if the ARG (a template argument) is dependent. */
23830 bool
23831 dependent_template_arg_p (tree arg)
23833 if (!processing_template_decl)
23834 return false;
23836 /* Assume a template argument that was wrongly written by the user
23837 is dependent. This is consistent with what
23838 any_dependent_template_arguments_p [that calls this function]
23839 does. */
23840 if (!arg || arg == error_mark_node)
23841 return true;
23843 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
23844 arg = ARGUMENT_PACK_SELECT_ARG (arg);
23846 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
23847 return true;
23848 if (TREE_CODE (arg) == TEMPLATE_DECL)
23850 if (DECL_TEMPLATE_PARM_P (arg))
23851 return true;
23852 /* A member template of a dependent class is not necessarily
23853 type-dependent, but it is a dependent template argument because it
23854 will be a member of an unknown specialization to that template. */
23855 tree scope = CP_DECL_CONTEXT (arg);
23856 return TYPE_P (scope) && dependent_type_p (scope);
23858 else if (ARGUMENT_PACK_P (arg))
23860 tree args = ARGUMENT_PACK_ARGS (arg);
23861 int i, len = TREE_VEC_LENGTH (args);
23862 for (i = 0; i < len; ++i)
23864 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23865 return true;
23868 return false;
23870 else if (TYPE_P (arg))
23871 return dependent_type_p (arg);
23872 else
23873 return (type_dependent_expression_p (arg)
23874 || value_dependent_expression_p (arg));
23877 /* Returns true if ARGS (a collection of template arguments) contains
23878 any types that require structural equality testing. */
23880 bool
23881 any_template_arguments_need_structural_equality_p (tree args)
23883 int i;
23884 int j;
23886 if (!args)
23887 return false;
23888 if (args == error_mark_node)
23889 return true;
23891 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23893 tree level = TMPL_ARGS_LEVEL (args, i + 1);
23894 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23896 tree arg = TREE_VEC_ELT (level, j);
23897 tree packed_args = NULL_TREE;
23898 int k, len = 1;
23900 if (ARGUMENT_PACK_P (arg))
23902 /* Look inside the argument pack. */
23903 packed_args = ARGUMENT_PACK_ARGS (arg);
23904 len = TREE_VEC_LENGTH (packed_args);
23907 for (k = 0; k < len; ++k)
23909 if (packed_args)
23910 arg = TREE_VEC_ELT (packed_args, k);
23912 if (error_operand_p (arg))
23913 return true;
23914 else if (TREE_CODE (arg) == TEMPLATE_DECL)
23915 continue;
23916 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
23917 return true;
23918 else if (!TYPE_P (arg) && TREE_TYPE (arg)
23919 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
23920 return true;
23925 return false;
23928 /* Returns true if ARGS (a collection of template arguments) contains
23929 any dependent arguments. */
23931 bool
23932 any_dependent_template_arguments_p (const_tree args)
23934 int i;
23935 int j;
23937 if (!args)
23938 return false;
23939 if (args == error_mark_node)
23940 return true;
23942 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23944 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
23945 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23946 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
23947 return true;
23950 return false;
23953 /* Returns TRUE if the template TMPL is type-dependent. */
23955 bool
23956 dependent_template_p (tree tmpl)
23958 if (TREE_CODE (tmpl) == OVERLOAD)
23960 while (tmpl)
23962 if (dependent_template_p (OVL_CURRENT (tmpl)))
23963 return true;
23964 tmpl = OVL_NEXT (tmpl);
23966 return false;
23969 /* Template template parameters are dependent. */
23970 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
23971 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
23972 return true;
23973 /* So are names that have not been looked up. */
23974 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
23975 return true;
23976 return false;
23979 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23981 bool
23982 dependent_template_id_p (tree tmpl, tree args)
23984 return (dependent_template_p (tmpl)
23985 || any_dependent_template_arguments_p (args));
23988 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23989 are dependent. */
23991 bool
23992 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
23994 int i;
23996 if (!processing_template_decl)
23997 return false;
23999 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24001 tree decl = TREE_VEC_ELT (declv, i);
24002 tree init = TREE_VEC_ELT (initv, i);
24003 tree cond = TREE_VEC_ELT (condv, i);
24004 tree incr = TREE_VEC_ELT (incrv, i);
24006 if (type_dependent_expression_p (decl)
24007 || TREE_CODE (decl) == SCOPE_REF)
24008 return true;
24010 if (init && type_dependent_expression_p (init))
24011 return true;
24013 if (type_dependent_expression_p (cond))
24014 return true;
24016 if (COMPARISON_CLASS_P (cond)
24017 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24018 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24019 return true;
24021 if (TREE_CODE (incr) == MODOP_EXPR)
24023 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24024 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24025 return true;
24027 else if (type_dependent_expression_p (incr))
24028 return true;
24029 else if (TREE_CODE (incr) == MODIFY_EXPR)
24031 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24032 return true;
24033 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24035 tree t = TREE_OPERAND (incr, 1);
24036 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24037 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24038 return true;
24043 return false;
24046 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24047 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24048 no such TYPE can be found. Note that this function peers inside
24049 uninstantiated templates and therefore should be used only in
24050 extremely limited situations. ONLY_CURRENT_P restricts this
24051 peering to the currently open classes hierarchy (which is required
24052 when comparing types). */
24054 tree
24055 resolve_typename_type (tree type, bool only_current_p)
24057 tree scope;
24058 tree name;
24059 tree decl;
24060 int quals;
24061 tree pushed_scope;
24062 tree result;
24064 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24066 scope = TYPE_CONTEXT (type);
24067 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24068 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24069 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24070 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24071 identifier of the TYPENAME_TYPE anymore.
24072 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24073 TYPENAME_TYPE instead, we avoid messing up with a possible
24074 typedef variant case. */
24075 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24077 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24078 it first before we can figure out what NAME refers to. */
24079 if (TREE_CODE (scope) == TYPENAME_TYPE)
24081 if (TYPENAME_IS_RESOLVING_P (scope))
24082 /* Given a class template A with a dependent base with nested type C,
24083 typedef typename A::C::C C will land us here, as trying to resolve
24084 the initial A::C leads to the local C typedef, which leads back to
24085 A::C::C. So we break the recursion now. */
24086 return type;
24087 else
24088 scope = resolve_typename_type (scope, only_current_p);
24090 /* If we don't know what SCOPE refers to, then we cannot resolve the
24091 TYPENAME_TYPE. */
24092 if (!CLASS_TYPE_P (scope))
24093 return type;
24094 /* If this is a typedef, we don't want to look inside (c++/11987). */
24095 if (typedef_variant_p (type))
24096 return type;
24097 /* If SCOPE isn't the template itself, it will not have a valid
24098 TYPE_FIELDS list. */
24099 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24100 /* scope is either the template itself or a compatible instantiation
24101 like X<T>, so look up the name in the original template. */
24102 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24103 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24104 gcc_checking_assert (uses_template_parms (scope));
24105 /* If scope has no fields, it can't be a current instantiation. Check this
24106 before currently_open_class to avoid infinite recursion (71515). */
24107 if (!TYPE_FIELDS (scope))
24108 return type;
24109 /* If the SCOPE is not the current instantiation, there's no reason
24110 to look inside it. */
24111 if (only_current_p && !currently_open_class (scope))
24112 return type;
24113 /* Enter the SCOPE so that name lookup will be resolved as if we
24114 were in the class definition. In particular, SCOPE will no
24115 longer be considered a dependent type. */
24116 pushed_scope = push_scope (scope);
24117 /* Look up the declaration. */
24118 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24119 tf_warning_or_error);
24121 result = NULL_TREE;
24123 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24124 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24125 if (!decl)
24126 /*nop*/;
24127 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24128 && TREE_CODE (decl) == TYPE_DECL)
24130 result = TREE_TYPE (decl);
24131 if (result == error_mark_node)
24132 result = NULL_TREE;
24134 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24135 && DECL_CLASS_TEMPLATE_P (decl))
24137 tree tmpl;
24138 tree args;
24139 /* Obtain the template and the arguments. */
24140 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24141 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24142 /* Instantiate the template. */
24143 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24144 /*entering_scope=*/0,
24145 tf_error | tf_user);
24146 if (result == error_mark_node)
24147 result = NULL_TREE;
24150 /* Leave the SCOPE. */
24151 if (pushed_scope)
24152 pop_scope (pushed_scope);
24154 /* If we failed to resolve it, return the original typename. */
24155 if (!result)
24156 return type;
24158 /* If lookup found a typename type, resolve that too. */
24159 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24161 /* Ill-formed programs can cause infinite recursion here, so we
24162 must catch that. */
24163 TYPENAME_IS_RESOLVING_P (result) = 1;
24164 result = resolve_typename_type (result, only_current_p);
24165 TYPENAME_IS_RESOLVING_P (result) = 0;
24168 /* Qualify the resulting type. */
24169 quals = cp_type_quals (type);
24170 if (quals)
24171 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24173 return result;
24176 /* EXPR is an expression which is not type-dependent. Return a proxy
24177 for EXPR that can be used to compute the types of larger
24178 expressions containing EXPR. */
24180 tree
24181 build_non_dependent_expr (tree expr)
24183 tree inner_expr;
24185 /* When checking, try to get a constant value for all non-dependent
24186 expressions in order to expose bugs in *_dependent_expression_p
24187 and constexpr. This can affect code generation, see PR70704, so
24188 only do this for -fchecking=2. */
24189 if (flag_checking > 1
24190 && cxx_dialect >= cxx11
24191 /* Don't do this during nsdmi parsing as it can lead to
24192 unexpected recursive instantiations. */
24193 && !parsing_nsdmi ()
24194 /* Don't do this during concept expansion either and for
24195 the same reason. */
24196 && !expanding_concept ())
24197 fold_non_dependent_expr (expr);
24199 /* Preserve OVERLOADs; the functions must be available to resolve
24200 types. */
24201 inner_expr = expr;
24202 if (TREE_CODE (inner_expr) == STMT_EXPR)
24203 inner_expr = stmt_expr_value_expr (inner_expr);
24204 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24205 inner_expr = TREE_OPERAND (inner_expr, 0);
24206 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24207 inner_expr = TREE_OPERAND (inner_expr, 1);
24208 if (is_overloaded_fn (inner_expr)
24209 || TREE_CODE (inner_expr) == OFFSET_REF)
24210 return expr;
24211 /* There is no need to return a proxy for a variable. */
24212 if (VAR_P (expr))
24213 return expr;
24214 /* Preserve string constants; conversions from string constants to
24215 "char *" are allowed, even though normally a "const char *"
24216 cannot be used to initialize a "char *". */
24217 if (TREE_CODE (expr) == STRING_CST)
24218 return expr;
24219 /* Preserve void and arithmetic constants, as an optimization -- there is no
24220 reason to create a new node. */
24221 if (TREE_CODE (expr) == VOID_CST
24222 || TREE_CODE (expr) == INTEGER_CST
24223 || TREE_CODE (expr) == REAL_CST)
24224 return expr;
24225 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24226 There is at least one place where we want to know that a
24227 particular expression is a throw-expression: when checking a ?:
24228 expression, there are special rules if the second or third
24229 argument is a throw-expression. */
24230 if (TREE_CODE (expr) == THROW_EXPR)
24231 return expr;
24233 /* Don't wrap an initializer list, we need to be able to look inside. */
24234 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24235 return expr;
24237 /* Don't wrap a dummy object, we need to be able to test for it. */
24238 if (is_dummy_object (expr))
24239 return expr;
24241 if (TREE_CODE (expr) == COND_EXPR)
24242 return build3 (COND_EXPR,
24243 TREE_TYPE (expr),
24244 TREE_OPERAND (expr, 0),
24245 (TREE_OPERAND (expr, 1)
24246 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24247 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24248 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24249 if (TREE_CODE (expr) == COMPOUND_EXPR
24250 && !COMPOUND_EXPR_OVERLOADED (expr))
24251 return build2 (COMPOUND_EXPR,
24252 TREE_TYPE (expr),
24253 TREE_OPERAND (expr, 0),
24254 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24256 /* If the type is unknown, it can't really be non-dependent */
24257 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24259 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24260 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24263 /* ARGS is a vector of expressions as arguments to a function call.
24264 Replace the arguments with equivalent non-dependent expressions.
24265 This modifies ARGS in place. */
24267 void
24268 make_args_non_dependent (vec<tree, va_gc> *args)
24270 unsigned int ix;
24271 tree arg;
24273 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24275 tree newarg = build_non_dependent_expr (arg);
24276 if (newarg != arg)
24277 (*args)[ix] = newarg;
24281 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24282 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24283 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24285 static tree
24286 make_auto_1 (tree name, bool set_canonical)
24288 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24289 TYPE_NAME (au) = build_decl (input_location,
24290 TYPE_DECL, name, au);
24291 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24292 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24293 (0, processing_template_decl + 1, processing_template_decl + 1,
24294 TYPE_NAME (au), NULL_TREE);
24295 if (set_canonical)
24296 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24297 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24298 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24300 return au;
24303 tree
24304 make_decltype_auto (void)
24306 return make_auto_1 (decltype_auto_identifier, true);
24309 tree
24310 make_auto (void)
24312 return make_auto_1 (auto_identifier, true);
24315 /* Return a C++17 deduction placeholder for class template TMPL. */
24317 tree
24318 make_template_placeholder (tree tmpl)
24320 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24321 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24322 return t;
24325 /* Make a "constrained auto" type-specifier. This is an
24326 auto type with constraints that must be associated after
24327 deduction. The constraint is formed from the given
24328 CONC and its optional sequence of arguments, which are
24329 non-null if written as partial-concept-id. */
24331 tree
24332 make_constrained_auto (tree con, tree args)
24334 tree type = make_auto_1 (auto_identifier, false);
24336 /* Build the constraint. */
24337 tree tmpl = DECL_TI_TEMPLATE (con);
24338 tree expr;
24339 if (VAR_P (con))
24340 expr = build_concept_check (tmpl, type, args);
24341 else
24342 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24344 tree constr = normalize_expression (expr);
24345 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24347 /* Our canonical type depends on the constraint. */
24348 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24350 /* Attach the constraint to the type declaration. */
24351 tree decl = TYPE_NAME (type);
24352 return decl;
24355 /* Given type ARG, return std::initializer_list<ARG>. */
24357 static tree
24358 listify (tree arg)
24360 tree std_init_list = namespace_binding
24361 (get_identifier ("initializer_list"), std_node);
24362 tree argvec;
24363 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24365 error ("deducing from brace-enclosed initializer list requires "
24366 "#include <initializer_list>");
24367 return error_mark_node;
24369 argvec = make_tree_vec (1);
24370 TREE_VEC_ELT (argvec, 0) = arg;
24371 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24372 NULL_TREE, 0, tf_warning_or_error);
24375 /* Replace auto in TYPE with std::initializer_list<auto>. */
24377 static tree
24378 listify_autos (tree type, tree auto_node)
24380 tree init_auto = listify (auto_node);
24381 tree argvec = make_tree_vec (1);
24382 TREE_VEC_ELT (argvec, 0) = init_auto;
24383 if (processing_template_decl)
24384 argvec = add_to_template_args (current_template_args (), argvec);
24385 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24388 /* Hash traits for hashing possibly constrained 'auto'
24389 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24391 struct auto_hash : default_hash_traits<tree>
24393 static inline hashval_t hash (tree);
24394 static inline bool equal (tree, tree);
24397 /* Hash the 'auto' T. */
24399 inline hashval_t
24400 auto_hash::hash (tree t)
24402 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24403 /* Matching constrained-type-specifiers denote the same template
24404 parameter, so hash the constraint. */
24405 return hash_placeholder_constraint (c);
24406 else
24407 /* But unconstrained autos are all separate, so just hash the pointer. */
24408 return iterative_hash_object (t, 0);
24411 /* Compare two 'auto's. */
24413 inline bool
24414 auto_hash::equal (tree t1, tree t2)
24416 if (t1 == t2)
24417 return true;
24419 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24420 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24422 /* Two unconstrained autos are distinct. */
24423 if (!c1 || !c2)
24424 return false;
24426 return equivalent_placeholder_constraints (c1, c2);
24429 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24430 constrained) auto, add it to the vector. */
24432 static int
24433 extract_autos_r (tree t, void *data)
24435 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24436 if (is_auto_or_concept (t))
24438 /* All the autos were built with index 0; fix that up now. */
24439 tree *p = hash.find_slot (t, INSERT);
24440 unsigned idx;
24441 if (*p)
24442 /* If this is a repeated constrained-type-specifier, use the index we
24443 chose before. */
24444 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24445 else
24447 /* Otherwise this is new, so use the current count. */
24448 *p = t;
24449 idx = hash.elements () - 1;
24451 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24454 /* Always keep walking. */
24455 return 0;
24458 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24459 says they can appear anywhere in the type. */
24461 static tree
24462 extract_autos (tree type)
24464 hash_set<tree> visited;
24465 hash_table<auto_hash> hash (2);
24467 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24469 tree tree_vec = make_tree_vec (hash.elements());
24470 for (hash_table<auto_hash>::iterator iter = hash.begin();
24471 iter != hash.end(); ++iter)
24473 tree elt = *iter;
24474 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24475 TREE_VEC_ELT (tree_vec, i)
24476 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24479 return tree_vec;
24482 /* The stem for deduction guide names. */
24483 const char *const dguide_base = "__dguide_";
24485 /* Return the name for a deduction guide for class template TMPL. */
24487 tree
24488 dguide_name (tree tmpl)
24490 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24491 tree tname = TYPE_IDENTIFIER (type);
24492 char *buf = (char *) alloca (1 + strlen (dguide_base)
24493 + IDENTIFIER_LENGTH (tname));
24494 memcpy (buf, dguide_base, strlen (dguide_base));
24495 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24496 IDENTIFIER_LENGTH (tname) + 1);
24497 tree dname = get_identifier (buf);
24498 TREE_TYPE (dname) = type;
24499 return dname;
24502 /* True if NAME is the name of a deduction guide. */
24504 bool
24505 dguide_name_p (tree name)
24507 return (TREE_TYPE (name)
24508 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24509 strlen (dguide_base)));
24512 /* True if FN is a deduction guide. */
24514 bool
24515 deduction_guide_p (tree fn)
24517 if (tree name = DECL_NAME (fn))
24518 return dguide_name_p (name);
24519 return false;
24522 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24523 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24524 template parameter types. Note that the handling of template template
24525 parameters relies on current_template_parms being set appropriately for the
24526 new template. */
24528 static tree
24529 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24530 tree tsubst_args, tsubst_flags_t complain)
24532 tree oldidx = get_template_parm_index (olddecl);
24534 tree newtype;
24535 if (TREE_CODE (olddecl) == TYPE_DECL
24536 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24538 newtype = cxx_make_type (TREE_CODE (TREE_TYPE (olddecl)));
24539 TYPE_MAIN_VARIANT (newtype) = newtype;
24541 else
24542 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24543 complain, NULL_TREE);
24545 tree newdecl
24546 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24547 DECL_NAME (olddecl), newtype);
24548 SET_DECL_TEMPLATE_PARM_P (newdecl);
24550 tree newidx;
24551 if (TREE_CODE (olddecl) == TYPE_DECL
24552 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24554 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24555 = build_template_parm_index (index, level, level,
24556 newdecl, newtype);
24557 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24558 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24560 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24562 DECL_TEMPLATE_RESULT (newdecl)
24563 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24564 DECL_NAME (olddecl), newtype);
24565 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24566 // First create a copy (ttargs) of tsubst_args with an
24567 // additional level for the template template parameter's own
24568 // template parameters (ttparms).
24569 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24570 (DECL_TEMPLATE_PARMS (olddecl)));
24571 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24572 tree ttargs = make_tree_vec (depth + 1);
24573 for (int i = 0; i < depth; ++i)
24574 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24575 TREE_VEC_ELT (ttargs, depth)
24576 = template_parms_level_to_args (ttparms);
24577 // Substitute ttargs into ttparms to fix references to
24578 // other template parameters.
24579 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24580 complain);
24581 // Now substitute again with args based on tparms, to reduce
24582 // the level of the ttparms.
24583 ttargs = current_template_args ();
24584 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24585 complain);
24586 // Finally, tack the adjusted parms onto tparms.
24587 ttparms = tree_cons (size_int (depth), ttparms,
24588 current_template_parms);
24589 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24592 else
24594 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24595 tree newconst
24596 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24597 TREE_CODE (oldconst),
24598 DECL_NAME (oldconst), newtype);
24599 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
24600 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
24601 SET_DECL_TEMPLATE_PARM_P (newconst);
24602 newidx = build_template_parm_index (index, level, level,
24603 newconst, newtype);
24604 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
24607 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24608 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24609 return newdecl;
24612 /* Returns a C++17 class deduction guide template based on the constructor
24613 CTOR. */
24615 static tree
24616 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
24618 if (outer_args)
24619 ctor = tsubst (ctor, outer_args, complain, ctor);
24620 tree type = DECL_CONTEXT (ctor);
24621 tree fn_tmpl;
24622 if (TREE_CODE (ctor) == TEMPLATE_DECL)
24624 fn_tmpl = ctor;
24625 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
24627 else
24628 fn_tmpl = DECL_TI_TEMPLATE (ctor);
24630 tree tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
24631 /* If type is a member class template, DECL_TI_ARGS (ctor) will have fully
24632 specialized args for the enclosing class. Strip those off, as the
24633 deduction guide won't have those template parameters. */
24634 tree targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
24635 TMPL_PARMS_DEPTH (tparms));
24636 /* Discard the 'this' parameter. */
24637 tree fparms = FUNCTION_ARG_CHAIN (ctor);
24638 tree fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
24639 tree ci = get_constraints (ctor);
24641 if (PRIMARY_TEMPLATE_P (fn_tmpl))
24643 /* For a member template constructor, we need to flatten the two template
24644 parameter lists into one, and then adjust the function signature
24645 accordingly. This gets...complicated. */
24646 ++processing_template_decl;
24647 tree save_parms = current_template_parms;
24649 /* For a member template we should have two levels of parms/args, one for
24650 the class and one for the constructor. We stripped specialized args
24651 for further enclosing classes above. */
24652 const int depth = 2;
24653 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
24655 /* Template args for translating references to the two-level template
24656 parameters into references to the one-level template parameters we are
24657 creating. */
24658 tree tsubst_args = copy_node (targs);
24659 TMPL_ARGS_LEVEL (tsubst_args, depth)
24660 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
24662 /* Template parms for the constructor template. */
24663 tree ftparms = TREE_VALUE (tparms);
24664 unsigned flen = TREE_VEC_LENGTH (ftparms);
24665 /* Template parms for the class template. */
24666 tparms = TREE_CHAIN (tparms);
24667 tree ctparms = TREE_VALUE (tparms);
24668 unsigned clen = TREE_VEC_LENGTH (ctparms);
24669 /* Template parms for the deduction guide start as a copy of the template
24670 parms for the class. We set current_template_parms for
24671 lookup_template_class_1. */
24672 current_template_parms = tparms = copy_node (tparms);
24673 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
24674 for (unsigned i = 0; i < clen; ++i)
24675 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
24677 /* Now we need to rewrite the constructor parms to append them to the
24678 class parms. */
24679 for (unsigned i = 0; i < flen; ++i)
24681 unsigned index = i + clen;
24682 unsigned level = 1;
24683 tree oldelt = TREE_VEC_ELT (ftparms, i);
24684 tree olddecl = TREE_VALUE (oldelt);
24685 tree newdecl = rewrite_template_parm (olddecl, index, level,
24686 tsubst_args, complain);
24687 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
24688 tsubst_args, complain, ctor);
24689 tree list = build_tree_list (newdef, newdecl);
24690 TEMPLATE_PARM_CONSTRAINTS (list)
24691 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
24692 tsubst_args, complain, ctor);
24693 TREE_VEC_ELT (new_vec, index) = list;
24694 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
24697 /* Now we have a final set of template parms to substitute into the
24698 function signature. */
24699 targs = template_parms_to_args (tparms);
24700 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
24701 complain, ctor);
24702 fargs = tsubst (fargs, tsubst_args, complain, ctor);
24703 if (ci)
24704 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
24706 current_template_parms = save_parms;
24707 --processing_template_decl;
24710 tree fntype = build_function_type (type, fparms);
24711 tree ded_fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (ctor),
24712 FUNCTION_DECL,
24713 dguide_name (type), fntype);
24714 DECL_ARGUMENTS (ded_fn) = fargs;
24715 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
24716 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
24717 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
24718 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
24719 if (ci)
24720 set_constraints (ded_tmpl, ci);
24722 return ded_tmpl;
24725 /* Deduce template arguments for the class template placeholder PTYPE for
24726 template TMPL based on the initializer INIT, and return the resulting
24727 type. */
24729 tree
24730 do_class_deduction (tree ptype, tree tmpl, tree init, tsubst_flags_t complain)
24732 if (!DECL_CLASS_TEMPLATE_P (tmpl))
24734 /* We should have handled this in the caller. */
24735 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
24736 return ptype;
24737 if (complain & tf_error)
24738 error ("non-class template %qT used without template arguments", tmpl);
24739 return error_mark_node;
24742 tree type = TREE_TYPE (tmpl);
24744 vec<tree,va_gc> *args;
24745 if (TREE_CODE (init) == TREE_LIST)
24746 args = make_tree_vector_from_list (init);
24747 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
24748 args = make_tree_vector_from_ctor (init);
24749 else
24750 args = make_tree_vector_single (init);
24752 if (args->length() == 1)
24754 /* First try to deduce directly, since we don't have implicitly-declared
24755 constructors yet. */
24756 tree parms = build_tree_list (NULL_TREE, type);
24757 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
24758 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
24759 int err = type_unification_real (tparms, targs, parms, &(*args)[0],
24760 1, /*subr*/false, DEDUCE_CALL,
24761 LOOKUP_NORMAL, NULL, /*explain*/false);
24762 if (err == 0)
24763 return tsubst (type, targs, complain, tmpl);
24766 tree dname = dguide_name (tmpl);
24767 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
24768 /*type*/false, /*complain*/false,
24769 /*hidden*/false);
24770 if (cands == error_mark_node)
24771 cands = NULL_TREE;
24773 tree outer_args = NULL_TREE;
24774 if (DECL_CLASS_SCOPE_P (tmpl)
24775 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
24777 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
24778 type = TREE_TYPE (most_general_template (tmpl));
24781 if (CLASSTYPE_METHOD_VEC (type))
24782 // FIXME cache artificial deduction guides
24783 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
24785 tree fn = OVL_CURRENT (fns);
24786 tree guide = build_deduction_guide (fn, outer_args, complain);
24787 cands = ovl_cons (guide, cands);
24790 if (cands == NULL_TREE)
24792 error ("cannot deduce template arguments for %qT, as it has "
24793 "no deduction guides or user-declared constructors", type);
24794 return error_mark_node;
24797 ++cp_unevaluated_operand;
24798 tree t = build_new_function_call (cands, &args, /*koenig*/false,
24799 complain|tf_decltype);
24800 --cp_unevaluated_operand;
24801 release_tree_vector (args);
24803 return TREE_TYPE (t);
24806 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24807 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
24809 tree
24810 do_auto_deduction (tree type, tree init, tree auto_node)
24812 return do_auto_deduction (type, init, auto_node,
24813 tf_warning_or_error,
24814 adc_unspecified);
24817 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24818 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
24819 The CONTEXT determines the context in which auto deduction is performed
24820 and is used to control error diagnostics.
24822 For partial-concept-ids, extra args may be appended to the list of deduced
24823 template arguments prior to determining constraint satisfaction. */
24825 tree
24826 do_auto_deduction (tree type, tree init, tree auto_node,
24827 tsubst_flags_t complain, auto_deduction_context context,
24828 tree outer_targs)
24830 tree targs;
24832 if (init == error_mark_node)
24833 return error_mark_node;
24835 if (type_dependent_expression_p (init)
24836 && context != adc_unify)
24837 /* Defining a subset of type-dependent expressions that we can deduce
24838 from ahead of time isn't worth the trouble. */
24839 return type;
24841 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24842 /* C++17 class template argument deduction. */
24843 return do_class_deduction (type, tmpl, init, complain);
24845 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
24846 with either a new invented type template parameter U or, if the
24847 initializer is a braced-init-list (8.5.4), with
24848 std::initializer_list<U>. */
24849 if (BRACE_ENCLOSED_INITIALIZER_P (init))
24851 if (!DIRECT_LIST_INIT_P (init))
24852 type = listify_autos (type, auto_node);
24853 else if (CONSTRUCTOR_NELTS (init) == 1)
24854 init = CONSTRUCTOR_ELT (init, 0)->value;
24855 else
24857 if (complain & tf_warning_or_error)
24859 if (permerror (input_location, "direct-list-initialization of "
24860 "%<auto%> requires exactly one element"))
24861 inform (input_location,
24862 "for deduction to %<std::initializer_list%>, use copy-"
24863 "list-initialization (i.e. add %<=%> before the %<{%>)");
24865 type = listify_autos (type, auto_node);
24869 if (type == error_mark_node)
24870 return error_mark_node;
24872 init = resolve_nondeduced_context (init, complain);
24874 if (context == adc_decomp_type
24875 && auto_node == type
24876 && init != error_mark_node
24877 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
24878 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
24879 and initializer has array type, deduce cv-qualified array type. */
24880 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
24881 complain);
24882 else if (AUTO_IS_DECLTYPE (auto_node))
24884 bool id = (DECL_P (init)
24885 || ((TREE_CODE (init) == COMPONENT_REF
24886 || TREE_CODE (init) == SCOPE_REF)
24887 && !REF_PARENTHESIZED_P (init)));
24888 targs = make_tree_vec (1);
24889 TREE_VEC_ELT (targs, 0)
24890 = finish_decltype_type (init, id, tf_warning_or_error);
24891 if (type != auto_node)
24893 if (complain & tf_error)
24894 error ("%qT as type rather than plain %<decltype(auto)%>", type);
24895 return error_mark_node;
24898 else
24900 tree parms = build_tree_list (NULL_TREE, type);
24901 tree tparms;
24903 if (flag_concepts)
24904 tparms = extract_autos (type);
24905 else
24907 tparms = make_tree_vec (1);
24908 TREE_VEC_ELT (tparms, 0)
24909 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
24912 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
24913 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
24914 DEDUCE_CALL, LOOKUP_NORMAL,
24915 NULL, /*explain_p=*/false);
24916 if (val > 0)
24918 if (processing_template_decl)
24919 /* Try again at instantiation time. */
24920 return type;
24921 if (type && type != error_mark_node
24922 && (complain & tf_error))
24923 /* If type is error_mark_node a diagnostic must have been
24924 emitted by now. Also, having a mention to '<type error>'
24925 in the diagnostic is not really useful to the user. */
24927 if (cfun && auto_node == current_function_auto_return_pattern
24928 && LAMBDA_FUNCTION_P (current_function_decl))
24929 error ("unable to deduce lambda return type from %qE", init);
24930 else
24931 error ("unable to deduce %qT from %qE", type, init);
24932 type_unification_real (tparms, targs, parms, &init, 1, 0,
24933 DEDUCE_CALL, LOOKUP_NORMAL,
24934 NULL, /*explain_p=*/true);
24936 return error_mark_node;
24940 /* Check any placeholder constraints against the deduced type. */
24941 if (flag_concepts && !processing_template_decl)
24942 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
24944 /* Use the deduced type to check the associated constraints. If we
24945 have a partial-concept-id, rebuild the argument list so that
24946 we check using the extra arguments. */
24947 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
24948 tree cargs = CHECK_CONSTR_ARGS (constr);
24949 if (TREE_VEC_LENGTH (cargs) > 1)
24951 cargs = copy_node (cargs);
24952 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
24954 else
24955 cargs = targs;
24956 if (!constraints_satisfied_p (constr, cargs))
24958 if (complain & tf_warning_or_error)
24960 switch (context)
24962 case adc_unspecified:
24963 case adc_unify:
24964 error("placeholder constraints not satisfied");
24965 break;
24966 case adc_variable_type:
24967 case adc_decomp_type:
24968 error ("deduced initializer does not satisfy "
24969 "placeholder constraints");
24970 break;
24971 case adc_return_type:
24972 error ("deduced return type does not satisfy "
24973 "placeholder constraints");
24974 break;
24975 case adc_requirement:
24976 error ("deduced expression type does not satisfy "
24977 "placeholder constraints");
24978 break;
24980 diagnose_constraints (input_location, constr, targs);
24982 return error_mark_node;
24986 if (processing_template_decl && context != adc_unify)
24987 outer_targs = current_template_args ();
24988 targs = add_to_template_args (outer_targs, targs);
24989 return tsubst (type, targs, complain, NULL_TREE);
24992 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
24993 result. */
24995 tree
24996 splice_late_return_type (tree type, tree late_return_type)
24998 if (is_auto (type))
25000 if (late_return_type)
25001 return late_return_type;
25003 tree idx = get_template_parm_index (type);
25004 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25005 /* In an abbreviated function template we didn't know we were dealing
25006 with a function template when we saw the auto return type, so update
25007 it to have the correct level. */
25008 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25010 return type;
25013 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25014 'decltype(auto)' or a deduced class template. */
25016 bool
25017 is_auto (const_tree type)
25019 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25020 && (TYPE_IDENTIFIER (type) == auto_identifier
25021 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25022 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25023 return true;
25024 else
25025 return false;
25028 /* for_each_template_parm callback for type_uses_auto. */
25031 is_auto_r (tree tp, void */*data*/)
25033 return is_auto_or_concept (tp);
25036 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25037 a use of `auto'. Returns NULL_TREE otherwise. */
25039 tree
25040 type_uses_auto (tree type)
25042 if (type == NULL_TREE)
25043 return NULL_TREE;
25044 else if (flag_concepts)
25046 /* The Concepts TS allows multiple autos in one type-specifier; just
25047 return the first one we find, do_auto_deduction will collect all of
25048 them. */
25049 if (uses_template_parms (type))
25050 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25051 /*visited*/NULL, /*nondeduced*/true);
25052 else
25053 return NULL_TREE;
25055 else
25056 return find_type_usage (type, is_auto);
25059 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25060 'decltype(auto)' or a concept. */
25062 bool
25063 is_auto_or_concept (const_tree type)
25065 return is_auto (type); // or concept
25068 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25069 a concept identifier) iff TYPE contains a use of a generic type. Returns
25070 NULL_TREE otherwise. */
25072 tree
25073 type_uses_auto_or_concept (tree type)
25075 return find_type_usage (type, is_auto_or_concept);
25079 /* For a given template T, return the vector of typedefs referenced
25080 in T for which access check is needed at T instantiation time.
25081 T is either a FUNCTION_DECL or a RECORD_TYPE.
25082 Those typedefs were added to T by the function
25083 append_type_to_template_for_access_check. */
25085 vec<qualified_typedef_usage_t, va_gc> *
25086 get_types_needing_access_check (tree t)
25088 tree ti;
25089 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25091 if (!t || t == error_mark_node)
25092 return NULL;
25094 if (!(ti = get_template_info (t)))
25095 return NULL;
25097 if (CLASS_TYPE_P (t)
25098 || TREE_CODE (t) == FUNCTION_DECL)
25100 if (!TI_TEMPLATE (ti))
25101 return NULL;
25103 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25106 return result;
25109 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25110 tied to T. That list of typedefs will be access checked at
25111 T instantiation time.
25112 T is either a FUNCTION_DECL or a RECORD_TYPE.
25113 TYPE_DECL is a TYPE_DECL node representing a typedef.
25114 SCOPE is the scope through which TYPE_DECL is accessed.
25115 LOCATION is the location of the usage point of TYPE_DECL.
25117 This function is a subroutine of
25118 append_type_to_template_for_access_check. */
25120 static void
25121 append_type_to_template_for_access_check_1 (tree t,
25122 tree type_decl,
25123 tree scope,
25124 location_t location)
25126 qualified_typedef_usage_t typedef_usage;
25127 tree ti;
25129 if (!t || t == error_mark_node)
25130 return;
25132 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25133 || CLASS_TYPE_P (t))
25134 && type_decl
25135 && TREE_CODE (type_decl) == TYPE_DECL
25136 && scope);
25138 if (!(ti = get_template_info (t)))
25139 return;
25141 gcc_assert (TI_TEMPLATE (ti));
25143 typedef_usage.typedef_decl = type_decl;
25144 typedef_usage.context = scope;
25145 typedef_usage.locus = location;
25147 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25150 /* Append TYPE_DECL to the template TEMPL.
25151 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25152 At TEMPL instanciation time, TYPE_DECL will be checked to see
25153 if it can be accessed through SCOPE.
25154 LOCATION is the location of the usage point of TYPE_DECL.
25156 e.g. consider the following code snippet:
25158 class C
25160 typedef int myint;
25163 template<class U> struct S
25165 C::myint mi; // <-- usage point of the typedef C::myint
25168 S<char> s;
25170 At S<char> instantiation time, we need to check the access of C::myint
25171 In other words, we need to check the access of the myint typedef through
25172 the C scope. For that purpose, this function will add the myint typedef
25173 and the scope C through which its being accessed to a list of typedefs
25174 tied to the template S. That list will be walked at template instantiation
25175 time and access check performed on each typedefs it contains.
25176 Note that this particular code snippet should yield an error because
25177 myint is private to C. */
25179 void
25180 append_type_to_template_for_access_check (tree templ,
25181 tree type_decl,
25182 tree scope,
25183 location_t location)
25185 qualified_typedef_usage_t *iter;
25186 unsigned i;
25188 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25190 /* Make sure we don't append the type to the template twice. */
25191 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25192 if (iter->typedef_decl == type_decl && scope == iter->context)
25193 return;
25195 append_type_to_template_for_access_check_1 (templ, type_decl,
25196 scope, location);
25199 /* Convert the generic type parameters in PARM that match the types given in the
25200 range [START_IDX, END_IDX) from the current_template_parms into generic type
25201 packs. */
25203 tree
25204 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25206 tree current = current_template_parms;
25207 int depth = TMPL_PARMS_DEPTH (current);
25208 current = INNERMOST_TEMPLATE_PARMS (current);
25209 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25211 for (int i = 0; i < start_idx; ++i)
25212 TREE_VEC_ELT (replacement, i)
25213 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25215 for (int i = start_idx; i < end_idx; ++i)
25217 /* Create a distinct parameter pack type from the current parm and add it
25218 to the replacement args to tsubst below into the generic function
25219 parameter. */
25221 tree o = TREE_TYPE (TREE_VALUE
25222 (TREE_VEC_ELT (current, i)));
25223 tree t = copy_type (o);
25224 TEMPLATE_TYPE_PARM_INDEX (t)
25225 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25226 o, 0, 0, tf_none);
25227 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25228 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25229 TYPE_MAIN_VARIANT (t) = t;
25230 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25231 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25232 TREE_VEC_ELT (replacement, i) = t;
25233 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25236 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25237 TREE_VEC_ELT (replacement, i)
25238 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25240 /* If there are more levels then build up the replacement with the outer
25241 template parms. */
25242 if (depth > 1)
25243 replacement = add_to_template_args (template_parms_to_args
25244 (TREE_CHAIN (current_template_parms)),
25245 replacement);
25247 return tsubst (parm, replacement, tf_none, NULL_TREE);
25250 /* Entries in the decl_constraint hash table. */
25251 struct GTY((for_user)) constr_entry
25253 tree decl;
25254 tree ci;
25257 /* Hashing function and equality for constraint entries. */
25258 struct constr_hasher : ggc_ptr_hash<constr_entry>
25260 static hashval_t hash (constr_entry *e)
25262 return (hashval_t)DECL_UID (e->decl);
25265 static bool equal (constr_entry *e1, constr_entry *e2)
25267 return e1->decl == e2->decl;
25271 /* A mapping from declarations to constraint information. Note that
25272 both templates and their underlying declarations are mapped to the
25273 same constraint information.
25275 FIXME: This is defined in pt.c because garbage collection
25276 code is not being generated for constraint.cc. */
25278 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25280 /* Returns the template constraints of declaration T. If T is not
25281 constrained, return NULL_TREE. Note that T must be non-null. */
25283 tree
25284 get_constraints (tree t)
25286 if (!flag_concepts)
25287 return NULL_TREE;
25289 gcc_assert (DECL_P (t));
25290 if (TREE_CODE (t) == TEMPLATE_DECL)
25291 t = DECL_TEMPLATE_RESULT (t);
25292 constr_entry elt = { t, NULL_TREE };
25293 constr_entry* found = decl_constraints->find (&elt);
25294 if (found)
25295 return found->ci;
25296 else
25297 return NULL_TREE;
25300 /* Associate the given constraint information CI with the declaration
25301 T. If T is a template, then the constraints are associated with
25302 its underlying declaration. Don't build associations if CI is
25303 NULL_TREE. */
25305 void
25306 set_constraints (tree t, tree ci)
25308 if (!ci)
25309 return;
25310 gcc_assert (t && flag_concepts);
25311 if (TREE_CODE (t) == TEMPLATE_DECL)
25312 t = DECL_TEMPLATE_RESULT (t);
25313 gcc_assert (!get_constraints (t));
25314 constr_entry elt = {t, ci};
25315 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25316 constr_entry* entry = ggc_alloc<constr_entry> ();
25317 *entry = elt;
25318 *slot = entry;
25321 /* Remove the associated constraints of the declaration T. */
25323 void
25324 remove_constraints (tree t)
25326 gcc_assert (DECL_P (t));
25327 if (TREE_CODE (t) == TEMPLATE_DECL)
25328 t = DECL_TEMPLATE_RESULT (t);
25330 constr_entry elt = {t, NULL_TREE};
25331 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25332 if (slot)
25333 decl_constraints->clear_slot (slot);
25336 /* Memoized satisfaction results for declarations. This
25337 maps the pair (constraint_info, arguments) to the result computed
25338 by constraints_satisfied_p. */
25340 struct GTY((for_user)) constraint_sat_entry
25342 tree ci;
25343 tree args;
25344 tree result;
25347 /* Hashing function and equality for constraint entries. */
25349 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25351 static hashval_t hash (constraint_sat_entry *e)
25353 hashval_t val = iterative_hash_object(e->ci, 0);
25354 return iterative_hash_template_arg (e->args, val);
25357 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25359 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25363 /* Memoized satisfaction results for concept checks. */
25365 struct GTY((for_user)) concept_spec_entry
25367 tree tmpl;
25368 tree args;
25369 tree result;
25372 /* Hashing function and equality for constraint entries. */
25374 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25376 static hashval_t hash (concept_spec_entry *e)
25378 return hash_tmpl_and_args (e->tmpl, e->args);
25381 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25383 ++comparing_specializations;
25384 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25385 --comparing_specializations;
25386 return eq;
25390 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25391 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25393 /* Search for a memoized satisfaction result. Returns one of the
25394 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25396 tree
25397 lookup_constraint_satisfaction (tree ci, tree args)
25399 constraint_sat_entry elt = { ci, args, NULL_TREE };
25400 constraint_sat_entry* found = constraint_memos->find (&elt);
25401 if (found)
25402 return found->result;
25403 else
25404 return NULL_TREE;
25407 /* Memoize the result of a satisfication test. Returns the saved result. */
25409 tree
25410 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25412 constraint_sat_entry elt = {ci, args, result};
25413 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25414 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25415 *entry = elt;
25416 *slot = entry;
25417 return result;
25420 /* Search for a memoized satisfaction result for a concept. */
25422 tree
25423 lookup_concept_satisfaction (tree tmpl, tree args)
25425 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25426 concept_spec_entry* found = concept_memos->find (&elt);
25427 if (found)
25428 return found->result;
25429 else
25430 return NULL_TREE;
25433 /* Memoize the result of a concept check. Returns the saved result. */
25435 tree
25436 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25438 concept_spec_entry elt = {tmpl, args, result};
25439 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25440 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25441 *entry = elt;
25442 *slot = entry;
25443 return result;
25446 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25448 /* Returns a prior concept specialization. This returns the substituted
25449 and normalized constraints defined by the concept. */
25451 tree
25452 get_concept_expansion (tree tmpl, tree args)
25454 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25455 concept_spec_entry* found = concept_expansions->find (&elt);
25456 if (found)
25457 return found->result;
25458 else
25459 return NULL_TREE;
25462 /* Save a concept expansion for later. */
25464 tree
25465 save_concept_expansion (tree tmpl, tree args, tree def)
25467 concept_spec_entry elt = {tmpl, args, def};
25468 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25469 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25470 *entry = elt;
25471 *slot = entry;
25472 return def;
25475 static hashval_t
25476 hash_subsumption_args (tree t1, tree t2)
25478 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25479 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25480 int val = 0;
25481 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25482 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25483 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25484 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25485 return val;
25488 /* Compare the constraints of two subsumption entries. The LEFT1 and
25489 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25490 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25492 static bool
25493 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25495 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25496 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25497 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25498 CHECK_CONSTR_ARGS (right1)))
25499 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25500 CHECK_CONSTR_ARGS (right2));
25501 return false;
25504 /* Key/value pair for learning and memoizing subsumption results. This
25505 associates a pair of check constraints (including arguments) with
25506 a boolean value indicating the result. */
25508 struct GTY((for_user)) subsumption_entry
25510 tree t1;
25511 tree t2;
25512 bool result;
25515 /* Hashing function and equality for constraint entries. */
25517 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
25519 static hashval_t hash (subsumption_entry *e)
25521 return hash_subsumption_args (e->t1, e->t2);
25524 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
25526 ++comparing_specializations;
25527 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
25528 --comparing_specializations;
25529 return eq;
25533 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
25535 /* Search for a previously cached subsumption result. */
25537 bool*
25538 lookup_subsumption_result (tree t1, tree t2)
25540 subsumption_entry elt = { t1, t2, false };
25541 subsumption_entry* found = subsumption_table->find (&elt);
25542 if (found)
25543 return &found->result;
25544 else
25545 return 0;
25548 /* Save a subsumption result. */
25550 bool
25551 save_subsumption_result (tree t1, tree t2, bool result)
25553 subsumption_entry elt = {t1, t2, result};
25554 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
25555 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
25556 *entry = elt;
25557 *slot = entry;
25558 return result;
25561 /* Set up the hash table for constraint association. */
25563 void
25564 init_constraint_processing (void)
25566 if (!flag_concepts)
25567 return;
25569 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
25570 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
25571 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
25572 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
25573 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
25576 /* Set up the hash tables for template instantiations. */
25578 void
25579 init_template_processing (void)
25581 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
25582 type_specializations = hash_table<spec_hasher>::create_ggc (37);
25585 /* Print stats about the template hash tables for -fstats. */
25587 void
25588 print_template_statistics (void)
25590 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
25591 "%f collisions\n", (long) decl_specializations->size (),
25592 (long) decl_specializations->elements (),
25593 decl_specializations->collisions ());
25594 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
25595 "%f collisions\n", (long) type_specializations->size (),
25596 (long) type_specializations->elements (),
25597 type_specializations->collisions ());
25600 #include "gt-cp-pt.h"