PR c++/68309
[official-gcc.git] / gcc / cp / pt.c
bloba45e6df9e8b0b1bb2927b41a1c03e954e2704daf
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 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, 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);
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 tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
182 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
184 static void regenerate_decl_from_template (tree, tree);
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 int template_args_equal (tree, tree);
199 static void tsubst_default_arguments (tree, tsubst_flags_t);
200 static tree for_each_template_parm_r (tree *, int *, void *);
201 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
202 static void copy_default_args_to_explicit_spec (tree);
203 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
204 static bool dependent_template_arg_p (tree);
205 static bool any_template_arguments_need_structural_equality_p (tree);
206 static bool dependent_type_p_r (tree);
207 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
208 static tree tsubst_decl (tree, tree, tsubst_flags_t);
209 static void perform_typedefs_access_check (tree tmpl, tree targs);
210 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
211 location_t);
212 static tree listify (tree);
213 static tree listify_autos (tree, tree);
214 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
215 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
216 static bool complex_alias_template_p (const_tree tmpl);
218 /* Make the current scope suitable for access checking when we are
219 processing T. T can be FUNCTION_DECL for instantiated function
220 template, VAR_DECL for static member variable, or TYPE_DECL for
221 alias template (needed by instantiate_decl). */
223 static void
224 push_access_scope (tree t)
226 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
227 || TREE_CODE (t) == TYPE_DECL);
229 if (DECL_FRIEND_CONTEXT (t))
230 push_nested_class (DECL_FRIEND_CONTEXT (t));
231 else if (DECL_CLASS_SCOPE_P (t))
232 push_nested_class (DECL_CONTEXT (t));
233 else
234 push_to_top_level ();
236 if (TREE_CODE (t) == FUNCTION_DECL)
238 saved_access_scope = tree_cons
239 (NULL_TREE, current_function_decl, saved_access_scope);
240 current_function_decl = t;
244 /* Restore the scope set up by push_access_scope. T is the node we
245 are processing. */
247 static void
248 pop_access_scope (tree t)
250 if (TREE_CODE (t) == FUNCTION_DECL)
252 current_function_decl = TREE_VALUE (saved_access_scope);
253 saved_access_scope = TREE_CHAIN (saved_access_scope);
256 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
257 pop_nested_class ();
258 else
259 pop_from_top_level ();
262 /* Do any processing required when DECL (a member template
263 declaration) is finished. Returns the TEMPLATE_DECL corresponding
264 to DECL, unless it is a specialization, in which case the DECL
265 itself is returned. */
267 tree
268 finish_member_template_decl (tree decl)
270 if (decl == error_mark_node)
271 return error_mark_node;
273 gcc_assert (DECL_P (decl));
275 if (TREE_CODE (decl) == TYPE_DECL)
277 tree type;
279 type = TREE_TYPE (decl);
280 if (type == error_mark_node)
281 return error_mark_node;
282 if (MAYBE_CLASS_TYPE_P (type)
283 && CLASSTYPE_TEMPLATE_INFO (type)
284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
286 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
287 check_member_template (tmpl);
288 return tmpl;
290 return NULL_TREE;
292 else if (TREE_CODE (decl) == FIELD_DECL)
293 error ("data member %qD cannot be a member template", decl);
294 else if (DECL_TEMPLATE_INFO (decl))
296 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
298 check_member_template (DECL_TI_TEMPLATE (decl));
299 return DECL_TI_TEMPLATE (decl);
301 else
302 return decl;
304 else
305 error ("invalid member template declaration %qD", decl);
307 return error_mark_node;
310 /* Create a template info node. */
312 tree
313 build_template_info (tree template_decl, tree template_args)
315 tree result = make_node (TEMPLATE_INFO);
316 TI_TEMPLATE (result) = template_decl;
317 TI_ARGS (result) = template_args;
318 return result;
321 /* Return the template info node corresponding to T, whatever T is. */
323 tree
324 get_template_info (const_tree t)
326 tree tinfo = NULL_TREE;
328 if (!t || t == error_mark_node)
329 return NULL;
331 if (TREE_CODE (t) == NAMESPACE_DECL)
332 return NULL;
334 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
335 tinfo = DECL_TEMPLATE_INFO (t);
337 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
338 t = TREE_TYPE (t);
340 if (OVERLOAD_TYPE_P (t))
341 tinfo = TYPE_TEMPLATE_INFO (t);
342 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
343 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
345 return tinfo;
348 /* Returns the template nesting level of the indicated class TYPE.
350 For example, in:
351 template <class T>
352 struct A
354 template <class U>
355 struct B {};
358 A<T>::B<U> has depth two, while A<T> has depth one.
359 Both A<T>::B<int> and A<int>::B<U> have depth one, if
360 they are instantiations, not specializations.
362 This function is guaranteed to return 0 if passed NULL_TREE so
363 that, for example, `template_class_depth (current_class_type)' is
364 always safe. */
367 template_class_depth (tree type)
369 int depth;
371 for (depth = 0;
372 type && TREE_CODE (type) != NAMESPACE_DECL;
373 type = (TREE_CODE (type) == FUNCTION_DECL)
374 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
376 tree tinfo = get_template_info (type);
378 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
379 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
380 ++depth;
383 return depth;
386 /* Subroutine of maybe_begin_member_template_processing.
387 Returns true if processing DECL needs us to push template parms. */
389 static bool
390 inline_needs_template_parms (tree decl, bool nsdmi)
392 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
393 return false;
395 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
396 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
399 /* Subroutine of maybe_begin_member_template_processing.
400 Push the template parms in PARMS, starting from LEVELS steps into the
401 chain, and ending at the beginning, since template parms are listed
402 innermost first. */
404 static void
405 push_inline_template_parms_recursive (tree parmlist, int levels)
407 tree parms = TREE_VALUE (parmlist);
408 int i;
410 if (levels > 1)
411 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
413 ++processing_template_decl;
414 current_template_parms
415 = tree_cons (size_int (processing_template_decl),
416 parms, current_template_parms);
417 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
419 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
420 NULL);
421 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
423 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
425 if (error_operand_p (parm))
426 continue;
428 gcc_assert (DECL_P (parm));
430 switch (TREE_CODE (parm))
432 case TYPE_DECL:
433 case TEMPLATE_DECL:
434 pushdecl (parm);
435 break;
437 case PARM_DECL:
438 /* Push the CONST_DECL. */
439 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
440 break;
442 default:
443 gcc_unreachable ();
448 /* Restore the template parameter context for a member template, a
449 friend template defined in a class definition, or a non-template
450 member of template class. */
452 void
453 maybe_begin_member_template_processing (tree decl)
455 tree parms;
456 int levels = 0;
457 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
459 if (nsdmi)
461 tree ctx = DECL_CONTEXT (decl);
462 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
463 /* Disregard full specializations (c++/60999). */
464 && uses_template_parms (ctx)
465 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
468 if (inline_needs_template_parms (decl, nsdmi))
470 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
471 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
473 if (DECL_TEMPLATE_SPECIALIZATION (decl))
475 --levels;
476 parms = TREE_CHAIN (parms);
479 push_inline_template_parms_recursive (parms, levels);
482 /* Remember how many levels of template parameters we pushed so that
483 we can pop them later. */
484 inline_parm_levels.safe_push (levels);
487 /* Undo the effects of maybe_begin_member_template_processing. */
489 void
490 maybe_end_member_template_processing (void)
492 int i;
493 int last;
495 if (inline_parm_levels.length () == 0)
496 return;
498 last = inline_parm_levels.pop ();
499 for (i = 0; i < last; ++i)
501 --processing_template_decl;
502 current_template_parms = TREE_CHAIN (current_template_parms);
503 poplevel (0, 0, 0);
507 /* Return a new template argument vector which contains all of ARGS,
508 but has as its innermost set of arguments the EXTRA_ARGS. */
510 static tree
511 add_to_template_args (tree args, tree extra_args)
513 tree new_args;
514 int extra_depth;
515 int i;
516 int j;
518 if (args == NULL_TREE || extra_args == error_mark_node)
519 return extra_args;
521 extra_depth = TMPL_ARGS_DEPTH (extra_args);
522 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
524 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
525 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
527 for (j = 1; j <= extra_depth; ++j, ++i)
528 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
530 return new_args;
533 /* Like add_to_template_args, but only the outermost ARGS are added to
534 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
535 (EXTRA_ARGS) levels are added. This function is used to combine
536 the template arguments from a partial instantiation with the
537 template arguments used to attain the full instantiation from the
538 partial instantiation. */
540 static tree
541 add_outermost_template_args (tree args, tree extra_args)
543 tree new_args;
545 /* If there are more levels of EXTRA_ARGS than there are ARGS,
546 something very fishy is going on. */
547 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
549 /* If *all* the new arguments will be the EXTRA_ARGS, just return
550 them. */
551 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
552 return extra_args;
554 /* For the moment, we make ARGS look like it contains fewer levels. */
555 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
557 new_args = add_to_template_args (args, extra_args);
559 /* Now, we restore ARGS to its full dimensions. */
560 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
562 return new_args;
565 /* Return the N levels of innermost template arguments from the ARGS. */
567 tree
568 get_innermost_template_args (tree args, int n)
570 tree new_args;
571 int extra_levels;
572 int i;
574 gcc_assert (n >= 0);
576 /* If N is 1, just return the innermost set of template arguments. */
577 if (n == 1)
578 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
580 /* If we're not removing anything, just return the arguments we were
581 given. */
582 extra_levels = TMPL_ARGS_DEPTH (args) - n;
583 gcc_assert (extra_levels >= 0);
584 if (extra_levels == 0)
585 return args;
587 /* Make a new set of arguments, not containing the outer arguments. */
588 new_args = make_tree_vec (n);
589 for (i = 1; i <= n; ++i)
590 SET_TMPL_ARGS_LEVEL (new_args, i,
591 TMPL_ARGS_LEVEL (args, i + extra_levels));
593 return new_args;
596 /* The inverse of get_innermost_template_args: Return all but the innermost
597 EXTRA_LEVELS levels of template arguments from the ARGS. */
599 static tree
600 strip_innermost_template_args (tree args, int extra_levels)
602 tree new_args;
603 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
604 int i;
606 gcc_assert (n >= 0);
608 /* If N is 1, just return the outermost set of template arguments. */
609 if (n == 1)
610 return TMPL_ARGS_LEVEL (args, 1);
612 /* If we're not removing anything, just return the arguments we were
613 given. */
614 gcc_assert (extra_levels >= 0);
615 if (extra_levels == 0)
616 return args;
618 /* Make a new set of arguments, not containing the inner arguments. */
619 new_args = make_tree_vec (n);
620 for (i = 1; i <= n; ++i)
621 SET_TMPL_ARGS_LEVEL (new_args, i,
622 TMPL_ARGS_LEVEL (args, i));
624 return new_args;
627 /* We've got a template header coming up; push to a new level for storing
628 the parms. */
630 void
631 begin_template_parm_list (void)
633 /* We use a non-tag-transparent scope here, which causes pushtag to
634 put tags in this scope, rather than in the enclosing class or
635 namespace scope. This is the right thing, since we want
636 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
637 global template class, push_template_decl handles putting the
638 TEMPLATE_DECL into top-level scope. For a nested template class,
639 e.g.:
641 template <class T> struct S1 {
642 template <class T> struct S2 {};
645 pushtag contains special code to call pushdecl_with_scope on the
646 TEMPLATE_DECL for S2. */
647 begin_scope (sk_template_parms, NULL);
648 ++processing_template_decl;
649 ++processing_template_parmlist;
650 note_template_header (0);
652 /* Add a dummy parameter level while we process the parameter list. */
653 current_template_parms
654 = tree_cons (size_int (processing_template_decl),
655 make_tree_vec (0),
656 current_template_parms);
659 /* This routine is called when a specialization is declared. If it is
660 invalid to declare a specialization here, an error is reported and
661 false is returned, otherwise this routine will return true. */
663 static bool
664 check_specialization_scope (void)
666 tree scope = current_scope ();
668 /* [temp.expl.spec]
670 An explicit specialization shall be declared in the namespace of
671 which the template is a member, or, for member templates, in the
672 namespace of which the enclosing class or enclosing class
673 template is a member. An explicit specialization of a member
674 function, member class or static data member of a class template
675 shall be declared in the namespace of which the class template
676 is a member. */
677 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
679 error ("explicit specialization in non-namespace scope %qD", scope);
680 return false;
683 /* [temp.expl.spec]
685 In an explicit specialization declaration for a member of a class
686 template or a member template that appears in namespace scope,
687 the member template and some of its enclosing class templates may
688 remain unspecialized, except that the declaration shall not
689 explicitly specialize a class member template if its enclosing
690 class templates are not explicitly specialized as well. */
691 if (current_template_parms)
693 error ("enclosing class templates are not explicitly specialized");
694 return false;
697 return true;
700 /* We've just seen template <>. */
702 bool
703 begin_specialization (void)
705 begin_scope (sk_template_spec, NULL);
706 note_template_header (1);
707 return check_specialization_scope ();
710 /* Called at then end of processing a declaration preceded by
711 template<>. */
713 void
714 end_specialization (void)
716 finish_scope ();
717 reset_specialization ();
720 /* Any template <>'s that we have seen thus far are not referring to a
721 function specialization. */
723 void
724 reset_specialization (void)
726 processing_specialization = 0;
727 template_header_count = 0;
730 /* We've just seen a template header. If SPECIALIZATION is nonzero,
731 it was of the form template <>. */
733 static void
734 note_template_header (int specialization)
736 processing_specialization = specialization;
737 template_header_count++;
740 /* We're beginning an explicit instantiation. */
742 void
743 begin_explicit_instantiation (void)
745 gcc_assert (!processing_explicit_instantiation);
746 processing_explicit_instantiation = true;
750 void
751 end_explicit_instantiation (void)
753 gcc_assert (processing_explicit_instantiation);
754 processing_explicit_instantiation = false;
757 /* An explicit specialization or partial specialization of TMPL is being
758 declared. Check that the namespace in which the specialization is
759 occurring is permissible. Returns false iff it is invalid to
760 specialize TMPL in the current namespace. */
762 static bool
763 check_specialization_namespace (tree tmpl)
765 tree tpl_ns = decl_namespace_context (tmpl);
767 /* [tmpl.expl.spec]
769 An explicit specialization shall be declared in the namespace of
770 which the template is a member, or, for member templates, in the
771 namespace of which the enclosing class or enclosing class
772 template is a member. An explicit specialization of a member
773 function, member class or static data member of a class template
774 shall be declared in the namespace of which the class template is
775 a member. */
776 if (current_scope() != DECL_CONTEXT (tmpl)
777 && !at_namespace_scope_p ())
779 error ("specialization of %qD must appear at namespace scope", tmpl);
780 return false;
782 if (is_associated_namespace (current_namespace, tpl_ns))
783 /* Same or super-using namespace. */
784 return true;
785 else
787 permerror (input_location,
788 "specialization of %qD in different namespace", tmpl);
789 permerror (DECL_SOURCE_LOCATION (tmpl),
790 " from definition of %q#D", tmpl);
791 return false;
795 /* SPEC is an explicit instantiation. Check that it is valid to
796 perform this explicit instantiation in the current namespace. */
798 static void
799 check_explicit_instantiation_namespace (tree spec)
801 tree ns;
803 /* DR 275: An explicit instantiation shall appear in an enclosing
804 namespace of its template. */
805 ns = decl_namespace_context (spec);
806 if (!is_ancestor (current_namespace, ns))
807 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
808 "(which does not enclose namespace %qD)",
809 spec, current_namespace, ns);
812 // Returns the type of a template specialization only if that
813 // specialization needs to be defined. Otherwise (e.g., if the type has
814 // already been defined), the function returns NULL_TREE.
815 static tree
816 maybe_new_partial_specialization (tree type)
818 // An implicit instantiation of an incomplete type implies
819 // the definition of a new class template.
821 // template<typename T>
822 // struct S;
824 // template<typename T>
825 // struct S<T*>;
827 // Here, S<T*> is an implicit instantiation of S whose type
828 // is incomplete.
829 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
830 return type;
832 // It can also be the case that TYPE is a completed specialization.
833 // Continuing the previous example, suppose we also declare:
835 // template<typename T>
836 // requires Integral<T>
837 // struct S<T*>;
839 // Here, S<T*> refers to the specialization S<T*> defined
840 // above. However, we need to differentiate definitions because
841 // we intend to define a new partial specialization. In this case,
842 // we rely on the fact that the constraints are different for
843 // this declaration than that above.
845 // Note that we also get here for injected class names and
846 // late-parsed template definitions. We must ensure that we
847 // do not create new type declarations for those cases.
848 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
850 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
851 tree args = CLASSTYPE_TI_ARGS (type);
853 // If there are no template parameters, this cannot be a new
854 // partial template specializtion?
855 if (!current_template_parms)
856 return NULL_TREE;
858 // The injected-class-name is not a new partial specialization.
859 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
860 return NULL_TREE;
862 // If the constraints are not the same as those of the primary
863 // then, we can probably create a new specialization.
864 tree type_constr = current_template_constraints ();
866 if (type == TREE_TYPE (tmpl))
868 tree main_constr = get_constraints (tmpl);
869 if (equivalent_constraints (type_constr, main_constr))
870 return NULL_TREE;
873 // Also, if there's a pre-existing specialization with matching
874 // constraints, then this also isn't new.
875 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
876 while (specs)
878 tree spec_tmpl = TREE_VALUE (specs);
879 tree spec_args = TREE_PURPOSE (specs);
880 tree spec_constr = get_constraints (spec_tmpl);
881 if (comp_template_args (args, spec_args)
882 && equivalent_constraints (type_constr, spec_constr))
883 return NULL_TREE;
884 specs = TREE_CHAIN (specs);
887 // Create a new type node (and corresponding type decl)
888 // for the newly declared specialization.
889 tree t = make_class_type (TREE_CODE (type));
890 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
891 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
892 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
894 /* We only need a separate type node for storing the definition of this
895 partial specialization; uses of S<T*> are unconstrained, so all are
896 equivalent. So keep TYPE_CANONICAL the same. */
897 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
899 // Build the corresponding type decl.
900 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
901 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
902 DECL_SOURCE_LOCATION (d) = input_location;
904 return t;
907 return NULL_TREE;
910 /* The TYPE is being declared. If it is a template type, that means it
911 is a partial specialization. Do appropriate error-checking. */
913 tree
914 maybe_process_partial_specialization (tree type)
916 tree context;
918 if (type == error_mark_node)
919 return error_mark_node;
921 /* A lambda that appears in specialization context is not itself a
922 specialization. */
923 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
924 return type;
926 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
928 error ("name of class shadows template template parameter %qD",
929 TYPE_NAME (type));
930 return error_mark_node;
933 context = TYPE_CONTEXT (type);
935 if (TYPE_ALIAS_P (type))
937 if (TYPE_TEMPLATE_INFO (type)
938 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
939 error ("specialization of alias template %qD",
940 TYPE_TI_TEMPLATE (type));
941 else
942 error ("explicit specialization of non-template %qT", type);
943 return error_mark_node;
945 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
947 /* This is for ordinary explicit specialization and partial
948 specialization of a template class such as:
950 template <> class C<int>;
954 template <class T> class C<T*>;
956 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
958 if (tree t = maybe_new_partial_specialization (type))
960 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
961 && !at_namespace_scope_p ())
962 return error_mark_node;
963 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
964 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
965 if (processing_template_decl)
967 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
968 if (decl == error_mark_node)
969 return error_mark_node;
970 return TREE_TYPE (decl);
973 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
974 error ("specialization of %qT after instantiation", type);
975 else if (errorcount && !processing_specialization
976 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
977 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
978 /* Trying to define a specialization either without a template<> header
979 or in an inappropriate place. We've already given an error, so just
980 bail now so we don't actually define the specialization. */
981 return error_mark_node;
983 else if (CLASS_TYPE_P (type)
984 && !CLASSTYPE_USE_TEMPLATE (type)
985 && CLASSTYPE_TEMPLATE_INFO (type)
986 && context && CLASS_TYPE_P (context)
987 && CLASSTYPE_TEMPLATE_INFO (context))
989 /* This is for an explicit specialization of member class
990 template according to [temp.expl.spec/18]:
992 template <> template <class U> class C<int>::D;
994 The context `C<int>' must be an implicit instantiation.
995 Otherwise this is just a member class template declared
996 earlier like:
998 template <> class C<int> { template <class U> class D; };
999 template <> template <class U> class C<int>::D;
1001 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1002 while in the second case, `C<int>::D' is a primary template
1003 and `C<T>::D' may not exist. */
1005 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1006 && !COMPLETE_TYPE_P (type))
1008 tree t;
1009 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1011 if (current_namespace
1012 != decl_namespace_context (tmpl))
1014 permerror (input_location,
1015 "specializing %q#T in different namespace", type);
1016 permerror (DECL_SOURCE_LOCATION (tmpl),
1017 " from definition of %q#D", tmpl);
1020 /* Check for invalid specialization after instantiation:
1022 template <> template <> class C<int>::D<int>;
1023 template <> template <class U> class C<int>::D; */
1025 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1026 t; t = TREE_CHAIN (t))
1028 tree inst = TREE_VALUE (t);
1029 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1030 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1032 /* We already have a full specialization of this partial
1033 instantiation, or a full specialization has been
1034 looked up but not instantiated. Reassign it to the
1035 new member specialization template. */
1036 spec_entry elt;
1037 spec_entry *entry;
1039 elt.tmpl = most_general_template (tmpl);
1040 elt.args = CLASSTYPE_TI_ARGS (inst);
1041 elt.spec = inst;
1043 type_specializations->remove_elt (&elt);
1045 elt.tmpl = tmpl;
1046 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1048 spec_entry **slot
1049 = type_specializations->find_slot (&elt, INSERT);
1050 entry = ggc_alloc<spec_entry> ();
1051 *entry = elt;
1052 *slot = entry;
1054 else
1055 /* But if we've had an implicit instantiation, that's a
1056 problem ([temp.expl.spec]/6). */
1057 error ("specialization %qT after instantiation %qT",
1058 type, inst);
1061 /* Mark TYPE as a specialization. And as a result, we only
1062 have one level of template argument for the innermost
1063 class template. */
1064 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1065 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1066 CLASSTYPE_TI_ARGS (type)
1067 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1070 else if (processing_specialization)
1072 /* Someday C++0x may allow for enum template specialization. */
1073 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1074 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1075 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1076 "of %qD not allowed by ISO C++", type);
1077 else
1079 error ("explicit specialization of non-template %qT", type);
1080 return error_mark_node;
1084 return type;
1087 /* Returns nonzero if we can optimize the retrieval of specializations
1088 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1089 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1091 static inline bool
1092 optimize_specialization_lookup_p (tree tmpl)
1094 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1095 && DECL_CLASS_SCOPE_P (tmpl)
1096 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1097 parameter. */
1098 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1099 /* The optimized lookup depends on the fact that the
1100 template arguments for the member function template apply
1101 purely to the containing class, which is not true if the
1102 containing class is an explicit or partial
1103 specialization. */
1104 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1105 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1106 && !DECL_CONV_FN_P (tmpl)
1107 /* It is possible to have a template that is not a member
1108 template and is not a member of a template class:
1110 template <typename T>
1111 struct S { friend A::f(); };
1113 Here, the friend function is a template, but the context does
1114 not have template information. The optimized lookup relies
1115 on having ARGS be the template arguments for both the class
1116 and the function template. */
1117 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1120 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1121 gone through coerce_template_parms by now. */
1123 static void
1124 verify_unstripped_args (tree args)
1126 ++processing_template_decl;
1127 if (!any_dependent_template_arguments_p (args))
1129 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1130 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1132 tree arg = TREE_VEC_ELT (inner, i);
1133 if (TREE_CODE (arg) == TEMPLATE_DECL)
1134 /* OK */;
1135 else if (TYPE_P (arg))
1136 gcc_assert (strip_typedefs (arg, NULL) == arg);
1137 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1138 /* Allow typedefs on the type of a non-type argument, since a
1139 parameter can have them. */;
1140 else
1141 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1144 --processing_template_decl;
1147 /* Retrieve the specialization (in the sense of [temp.spec] - a
1148 specialization is either an instantiation or an explicit
1149 specialization) of TMPL for the given template ARGS. If there is
1150 no such specialization, return NULL_TREE. The ARGS are a vector of
1151 arguments, or a vector of vectors of arguments, in the case of
1152 templates with more than one level of parameters.
1154 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1155 then we search for a partial specialization matching ARGS. This
1156 parameter is ignored if TMPL is not a class template.
1158 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1159 result is a NONTYPE_ARGUMENT_PACK. */
1161 static tree
1162 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1164 if (tmpl == NULL_TREE)
1165 return NULL_TREE;
1167 if (args == error_mark_node)
1168 return NULL_TREE;
1170 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1171 || TREE_CODE (tmpl) == FIELD_DECL);
1173 /* There should be as many levels of arguments as there are
1174 levels of parameters. */
1175 gcc_assert (TMPL_ARGS_DEPTH (args)
1176 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1177 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1178 : template_class_depth (DECL_CONTEXT (tmpl))));
1180 if (flag_checking)
1181 verify_unstripped_args (args);
1183 if (optimize_specialization_lookup_p (tmpl))
1185 tree class_template;
1186 tree class_specialization;
1187 vec<tree, va_gc> *methods;
1188 tree fns;
1189 int idx;
1191 /* The template arguments actually apply to the containing
1192 class. Find the class specialization with those
1193 arguments. */
1194 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1195 class_specialization
1196 = retrieve_specialization (class_template, args, 0);
1197 if (!class_specialization)
1198 return NULL_TREE;
1199 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1200 for the specialization. */
1201 idx = class_method_index_for_fn (class_specialization, tmpl);
1202 if (idx == -1)
1203 return NULL_TREE;
1204 /* Iterate through the methods with the indicated name, looking
1205 for the one that has an instance of TMPL. */
1206 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1207 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1209 tree fn = OVL_CURRENT (fns);
1210 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1211 /* using-declarations can add base methods to the method vec,
1212 and we don't want those here. */
1213 && DECL_CONTEXT (fn) == class_specialization)
1214 return fn;
1216 return NULL_TREE;
1218 else
1220 spec_entry *found;
1221 spec_entry elt;
1222 hash_table<spec_hasher> *specializations;
1224 elt.tmpl = tmpl;
1225 elt.args = args;
1226 elt.spec = NULL_TREE;
1228 if (DECL_CLASS_TEMPLATE_P (tmpl))
1229 specializations = type_specializations;
1230 else
1231 specializations = decl_specializations;
1233 if (hash == 0)
1234 hash = spec_hasher::hash (&elt);
1235 found = specializations->find_with_hash (&elt, hash);
1236 if (found)
1237 return found->spec;
1240 return NULL_TREE;
1243 /* Like retrieve_specialization, but for local declarations. */
1245 tree
1246 retrieve_local_specialization (tree tmpl)
1248 if (local_specializations == NULL)
1249 return NULL_TREE;
1251 tree *slot = local_specializations->get (tmpl);
1252 return slot ? *slot : NULL_TREE;
1255 /* Returns nonzero iff DECL is a specialization of TMPL. */
1258 is_specialization_of (tree decl, tree tmpl)
1260 tree t;
1262 if (TREE_CODE (decl) == FUNCTION_DECL)
1264 for (t = decl;
1265 t != NULL_TREE;
1266 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1267 if (t == tmpl)
1268 return 1;
1270 else
1272 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1274 for (t = TREE_TYPE (decl);
1275 t != NULL_TREE;
1276 t = CLASSTYPE_USE_TEMPLATE (t)
1277 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1278 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1279 return 1;
1282 return 0;
1285 /* Returns nonzero iff DECL is a specialization of friend declaration
1286 FRIEND_DECL according to [temp.friend]. */
1288 bool
1289 is_specialization_of_friend (tree decl, tree friend_decl)
1291 bool need_template = true;
1292 int template_depth;
1294 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1295 || TREE_CODE (decl) == TYPE_DECL);
1297 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1298 of a template class, we want to check if DECL is a specialization
1299 if this. */
1300 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1301 && DECL_TEMPLATE_INFO (friend_decl)
1302 && !DECL_USE_TEMPLATE (friend_decl))
1304 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1305 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1306 need_template = false;
1308 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1309 && !PRIMARY_TEMPLATE_P (friend_decl))
1310 need_template = false;
1312 /* There is nothing to do if this is not a template friend. */
1313 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1314 return false;
1316 if (is_specialization_of (decl, friend_decl))
1317 return true;
1319 /* [temp.friend/6]
1320 A member of a class template may be declared to be a friend of a
1321 non-template class. In this case, the corresponding member of
1322 every specialization of the class template is a friend of the
1323 class granting friendship.
1325 For example, given a template friend declaration
1327 template <class T> friend void A<T>::f();
1329 the member function below is considered a friend
1331 template <> struct A<int> {
1332 void f();
1335 For this type of template friend, TEMPLATE_DEPTH below will be
1336 nonzero. To determine if DECL is a friend of FRIEND, we first
1337 check if the enclosing class is a specialization of another. */
1339 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1340 if (template_depth
1341 && DECL_CLASS_SCOPE_P (decl)
1342 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1343 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1345 /* Next, we check the members themselves. In order to handle
1346 a few tricky cases, such as when FRIEND_DECL's are
1348 template <class T> friend void A<T>::g(T t);
1349 template <class T> template <T t> friend void A<T>::h();
1351 and DECL's are
1353 void A<int>::g(int);
1354 template <int> void A<int>::h();
1356 we need to figure out ARGS, the template arguments from
1357 the context of DECL. This is required for template substitution
1358 of `T' in the function parameter of `g' and template parameter
1359 of `h' in the above examples. Here ARGS corresponds to `int'. */
1361 tree context = DECL_CONTEXT (decl);
1362 tree args = NULL_TREE;
1363 int current_depth = 0;
1365 while (current_depth < template_depth)
1367 if (CLASSTYPE_TEMPLATE_INFO (context))
1369 if (current_depth == 0)
1370 args = TYPE_TI_ARGS (context);
1371 else
1372 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1373 current_depth++;
1375 context = TYPE_CONTEXT (context);
1378 if (TREE_CODE (decl) == FUNCTION_DECL)
1380 bool is_template;
1381 tree friend_type;
1382 tree decl_type;
1383 tree friend_args_type;
1384 tree decl_args_type;
1386 /* Make sure that both DECL and FRIEND_DECL are templates or
1387 non-templates. */
1388 is_template = DECL_TEMPLATE_INFO (decl)
1389 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1390 if (need_template ^ is_template)
1391 return false;
1392 else if (is_template)
1394 /* If both are templates, check template parameter list. */
1395 tree friend_parms
1396 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1397 args, tf_none);
1398 if (!comp_template_parms
1399 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1400 friend_parms))
1401 return false;
1403 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1405 else
1406 decl_type = TREE_TYPE (decl);
1408 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1409 tf_none, NULL_TREE);
1410 if (friend_type == error_mark_node)
1411 return false;
1413 /* Check if return types match. */
1414 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1415 return false;
1417 /* Check if function parameter types match, ignoring the
1418 `this' parameter. */
1419 friend_args_type = TYPE_ARG_TYPES (friend_type);
1420 decl_args_type = TYPE_ARG_TYPES (decl_type);
1421 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1422 friend_args_type = TREE_CHAIN (friend_args_type);
1423 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1424 decl_args_type = TREE_CHAIN (decl_args_type);
1426 return compparms (decl_args_type, friend_args_type);
1428 else
1430 /* DECL is a TYPE_DECL */
1431 bool is_template;
1432 tree decl_type = TREE_TYPE (decl);
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1435 non-templates. */
1436 is_template
1437 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1440 if (need_template ^ is_template)
1441 return false;
1442 else if (is_template)
1444 tree friend_parms;
1445 /* If both are templates, check the name of the two
1446 TEMPLATE_DECL's first because is_friend didn't. */
1447 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1448 != DECL_NAME (friend_decl))
1449 return false;
1451 /* Now check template parameter list. */
1452 friend_parms
1453 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1454 args, tf_none);
1455 return comp_template_parms
1456 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1457 friend_parms);
1459 else
1460 return (DECL_NAME (decl)
1461 == DECL_NAME (friend_decl));
1464 return false;
1467 /* Register the specialization SPEC as a specialization of TMPL with
1468 the indicated ARGS. IS_FRIEND indicates whether the specialization
1469 is actually just a friend declaration. Returns SPEC, or an
1470 equivalent prior declaration, if available.
1472 We also store instantiations of field packs in the hash table, even
1473 though they are not themselves templates, to make lookup easier. */
1475 static tree
1476 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1477 hashval_t hash)
1479 tree fn;
1480 spec_entry **slot = NULL;
1481 spec_entry elt;
1483 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1484 || (TREE_CODE (tmpl) == FIELD_DECL
1485 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1487 if (TREE_CODE (spec) == FUNCTION_DECL
1488 && uses_template_parms (DECL_TI_ARGS (spec)))
1489 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1490 register it; we want the corresponding TEMPLATE_DECL instead.
1491 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1492 the more obvious `uses_template_parms (spec)' to avoid problems
1493 with default function arguments. In particular, given
1494 something like this:
1496 template <class T> void f(T t1, T t = T())
1498 the default argument expression is not substituted for in an
1499 instantiation unless and until it is actually needed. */
1500 return spec;
1502 if (optimize_specialization_lookup_p (tmpl))
1503 /* We don't put these specializations in the hash table, but we might
1504 want to give an error about a mismatch. */
1505 fn = retrieve_specialization (tmpl, args, 0);
1506 else
1508 elt.tmpl = tmpl;
1509 elt.args = args;
1510 elt.spec = spec;
1512 if (hash == 0)
1513 hash = spec_hasher::hash (&elt);
1515 slot =
1516 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1517 if (*slot)
1518 fn = ((spec_entry *) *slot)->spec;
1519 else
1520 fn = NULL_TREE;
1523 /* We can sometimes try to re-register a specialization that we've
1524 already got. In particular, regenerate_decl_from_template calls
1525 duplicate_decls which will update the specialization list. But,
1526 we'll still get called again here anyhow. It's more convenient
1527 to simply allow this than to try to prevent it. */
1528 if (fn == spec)
1529 return spec;
1530 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1532 if (DECL_TEMPLATE_INSTANTIATION (fn))
1534 if (DECL_ODR_USED (fn)
1535 || DECL_EXPLICIT_INSTANTIATION (fn))
1537 error ("specialization of %qD after instantiation",
1538 fn);
1539 return error_mark_node;
1541 else
1543 tree clone;
1544 /* This situation should occur only if the first
1545 specialization is an implicit instantiation, the
1546 second is an explicit specialization, and the
1547 implicit instantiation has not yet been used. That
1548 situation can occur if we have implicitly
1549 instantiated a member function and then specialized
1550 it later.
1552 We can also wind up here if a friend declaration that
1553 looked like an instantiation turns out to be a
1554 specialization:
1556 template <class T> void foo(T);
1557 class S { friend void foo<>(int) };
1558 template <> void foo(int);
1560 We transform the existing DECL in place so that any
1561 pointers to it become pointers to the updated
1562 declaration.
1564 If there was a definition for the template, but not
1565 for the specialization, we want this to look as if
1566 there were no definition, and vice versa. */
1567 DECL_INITIAL (fn) = NULL_TREE;
1568 duplicate_decls (spec, fn, is_friend);
1569 /* The call to duplicate_decls will have applied
1570 [temp.expl.spec]:
1572 An explicit specialization of a function template
1573 is inline only if it is explicitly declared to be,
1574 and independently of whether its function template
1577 to the primary function; now copy the inline bits to
1578 the various clones. */
1579 FOR_EACH_CLONE (clone, fn)
1581 DECL_DECLARED_INLINE_P (clone)
1582 = DECL_DECLARED_INLINE_P (fn);
1583 DECL_SOURCE_LOCATION (clone)
1584 = DECL_SOURCE_LOCATION (fn);
1585 DECL_DELETED_FN (clone)
1586 = DECL_DELETED_FN (fn);
1588 check_specialization_namespace (tmpl);
1590 return fn;
1593 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1595 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1596 /* Dup decl failed, but this is a new definition. Set the
1597 line number so any errors match this new
1598 definition. */
1599 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1601 return fn;
1604 else if (fn)
1605 return duplicate_decls (spec, fn, is_friend);
1607 /* A specialization must be declared in the same namespace as the
1608 template it is specializing. */
1609 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1610 && !check_specialization_namespace (tmpl))
1611 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1613 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1615 spec_entry *entry = ggc_alloc<spec_entry> ();
1616 gcc_assert (tmpl && args && spec);
1617 *entry = elt;
1618 *slot = entry;
1619 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1620 && PRIMARY_TEMPLATE_P (tmpl)
1621 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1622 || variable_template_p (tmpl))
1623 /* If TMPL is a forward declaration of a template function, keep a list
1624 of all specializations in case we need to reassign them to a friend
1625 template later in tsubst_friend_function.
1627 Also keep a list of all variable template instantiations so that
1628 process_partial_specialization can check whether a later partial
1629 specialization would have used it. */
1630 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1631 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1634 return spec;
1637 /* Returns true iff two spec_entry nodes are equivalent. */
1639 int comparing_specializations;
1641 bool
1642 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1644 int equal;
1646 ++comparing_specializations;
1647 equal = (e1->tmpl == e2->tmpl
1648 && comp_template_args (e1->args, e2->args));
1649 if (equal && flag_concepts
1650 /* tmpl could be a FIELD_DECL for a capture pack. */
1651 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1652 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1653 && uses_template_parms (e1->args))
1655 /* Partial specializations of a variable template can be distinguished by
1656 constraints. */
1657 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1658 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1659 equal = equivalent_constraints (c1, c2);
1661 --comparing_specializations;
1663 return equal;
1666 /* Returns a hash for a template TMPL and template arguments ARGS. */
1668 static hashval_t
1669 hash_tmpl_and_args (tree tmpl, tree args)
1671 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1672 return iterative_hash_template_arg (args, val);
1675 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1676 ignoring SPEC. */
1678 hashval_t
1679 spec_hasher::hash (spec_entry *e)
1681 return hash_tmpl_and_args (e->tmpl, e->args);
1684 /* Recursively calculate a hash value for a template argument ARG, for use
1685 in the hash tables of template specializations. */
1687 hashval_t
1688 iterative_hash_template_arg (tree arg, hashval_t val)
1690 unsigned HOST_WIDE_INT i;
1691 enum tree_code code;
1692 char tclass;
1694 if (arg == NULL_TREE)
1695 return iterative_hash_object (arg, val);
1697 if (!TYPE_P (arg))
1698 STRIP_NOPS (arg);
1700 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1701 /* We can get one of these when re-hashing a previous entry in the middle
1702 of substituting into a pack expansion. Just look through it. */
1703 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1705 code = TREE_CODE (arg);
1706 tclass = TREE_CODE_CLASS (code);
1708 val = iterative_hash_object (code, val);
1710 switch (code)
1712 case ERROR_MARK:
1713 return val;
1715 case IDENTIFIER_NODE:
1716 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1718 case TREE_VEC:
1720 int i, len = TREE_VEC_LENGTH (arg);
1721 for (i = 0; i < len; ++i)
1722 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1723 return val;
1726 case TYPE_PACK_EXPANSION:
1727 case EXPR_PACK_EXPANSION:
1728 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1729 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1731 case TYPE_ARGUMENT_PACK:
1732 case NONTYPE_ARGUMENT_PACK:
1733 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1735 case TREE_LIST:
1736 for (; arg; arg = TREE_CHAIN (arg))
1737 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1738 return val;
1740 case OVERLOAD:
1741 for (; arg; arg = OVL_NEXT (arg))
1742 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1743 return val;
1745 case CONSTRUCTOR:
1747 tree field, value;
1748 iterative_hash_template_arg (TREE_TYPE (arg), val);
1749 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1751 val = iterative_hash_template_arg (field, val);
1752 val = iterative_hash_template_arg (value, val);
1754 return val;
1757 case PARM_DECL:
1758 if (!DECL_ARTIFICIAL (arg))
1760 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1761 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1763 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1765 case TARGET_EXPR:
1766 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1768 case PTRMEM_CST:
1769 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1770 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1772 case TEMPLATE_PARM_INDEX:
1773 val = iterative_hash_template_arg
1774 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1775 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1776 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1778 case TRAIT_EXPR:
1779 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1780 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1781 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1783 case BASELINK:
1784 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1785 val);
1786 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1787 val);
1789 case MODOP_EXPR:
1790 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1791 code = TREE_CODE (TREE_OPERAND (arg, 1));
1792 val = iterative_hash_object (code, val);
1793 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1795 case LAMBDA_EXPR:
1796 /* A lambda can't appear in a template arg, but don't crash on
1797 erroneous input. */
1798 gcc_assert (seen_error ());
1799 return val;
1801 case CAST_EXPR:
1802 case IMPLICIT_CONV_EXPR:
1803 case STATIC_CAST_EXPR:
1804 case REINTERPRET_CAST_EXPR:
1805 case CONST_CAST_EXPR:
1806 case DYNAMIC_CAST_EXPR:
1807 case NEW_EXPR:
1808 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1809 /* Now hash operands as usual. */
1810 break;
1812 default:
1813 break;
1816 switch (tclass)
1818 case tcc_type:
1819 if (alias_template_specialization_p (arg))
1821 // We want an alias specialization that survived strip_typedefs
1822 // to hash differently from its TYPE_CANONICAL, to avoid hash
1823 // collisions that compare as different in template_args_equal.
1824 // These could be dependent specializations that strip_typedefs
1825 // left alone, or untouched specializations because
1826 // coerce_template_parms returns the unconverted template
1827 // arguments if it sees incomplete argument packs.
1828 tree ti = TYPE_TEMPLATE_INFO (arg);
1829 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1831 if (TYPE_CANONICAL (arg))
1832 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1833 val);
1834 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1835 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1836 /* Otherwise just compare the types during lookup. */
1837 return val;
1839 case tcc_declaration:
1840 case tcc_constant:
1841 return iterative_hash_expr (arg, val);
1843 default:
1844 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1846 unsigned n = cp_tree_operand_length (arg);
1847 for (i = 0; i < n; ++i)
1848 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1849 return val;
1852 gcc_unreachable ();
1853 return 0;
1856 /* Unregister the specialization SPEC as a specialization of TMPL.
1857 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1858 if the SPEC was listed as a specialization of TMPL.
1860 Note that SPEC has been ggc_freed, so we can't look inside it. */
1862 bool
1863 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1865 spec_entry *entry;
1866 spec_entry elt;
1868 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1869 elt.args = TI_ARGS (tinfo);
1870 elt.spec = NULL_TREE;
1872 entry = decl_specializations->find (&elt);
1873 if (entry != NULL)
1875 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1876 gcc_assert (new_spec != NULL_TREE);
1877 entry->spec = new_spec;
1878 return 1;
1881 return 0;
1884 /* Like register_specialization, but for local declarations. We are
1885 registering SPEC, an instantiation of TMPL. */
1887 void
1888 register_local_specialization (tree spec, tree tmpl)
1890 local_specializations->put (tmpl, spec);
1893 /* TYPE is a class type. Returns true if TYPE is an explicitly
1894 specialized class. */
1896 bool
1897 explicit_class_specialization_p (tree type)
1899 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1900 return false;
1901 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1904 /* Print the list of functions at FNS, going through all the overloads
1905 for each element of the list. Alternatively, FNS can not be a
1906 TREE_LIST, in which case it will be printed together with all the
1907 overloads.
1909 MORE and *STR should respectively be FALSE and NULL when the function
1910 is called from the outside. They are used internally on recursive
1911 calls. print_candidates manages the two parameters and leaves NULL
1912 in *STR when it ends. */
1914 static void
1915 print_candidates_1 (tree fns, bool more, const char **str)
1917 tree fn, fn2;
1918 char *spaces = NULL;
1920 for (fn = fns; fn; fn = OVL_NEXT (fn))
1921 if (TREE_CODE (fn) == TREE_LIST)
1923 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1924 print_candidates_1 (TREE_VALUE (fn2),
1925 TREE_CHAIN (fn2) || more, str);
1927 else
1929 tree cand = OVL_CURRENT (fn);
1930 if (!*str)
1932 /* Pick the prefix string. */
1933 if (!more && !OVL_NEXT (fns))
1935 inform (DECL_SOURCE_LOCATION (cand),
1936 "candidate is: %#D", cand);
1937 continue;
1940 *str = _("candidates are:");
1941 spaces = get_spaces (*str);
1943 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1944 *str = spaces ? spaces : *str;
1947 if (!more)
1949 free (spaces);
1950 *str = NULL;
1954 /* Print the list of candidate FNS in an error message. FNS can also
1955 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1957 void
1958 print_candidates (tree fns)
1960 const char *str = NULL;
1961 print_candidates_1 (fns, false, &str);
1962 gcc_assert (str == NULL);
1965 /* Get a (possibly) constrained template declaration for the
1966 purpose of ordering candidates. */
1967 static tree
1968 get_template_for_ordering (tree list)
1970 gcc_assert (TREE_CODE (list) == TREE_LIST);
1971 tree f = TREE_VALUE (list);
1972 if (tree ti = DECL_TEMPLATE_INFO (f))
1973 return TI_TEMPLATE (ti);
1974 return f;
1977 /* Among candidates having the same signature, return the
1978 most constrained or NULL_TREE if there is no best candidate.
1979 If the signatures of candidates vary (e.g., template
1980 specialization vs. member function), then there can be no
1981 most constrained.
1983 Note that we don't compare constraints on the functions
1984 themselves, but rather those of their templates. */
1985 static tree
1986 most_constrained_function (tree candidates)
1988 // Try to find the best candidate in a first pass.
1989 tree champ = candidates;
1990 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1992 int winner = more_constrained (get_template_for_ordering (champ),
1993 get_template_for_ordering (c));
1994 if (winner == -1)
1995 champ = c; // The candidate is more constrained
1996 else if (winner == 0)
1997 return NULL_TREE; // Neither is more constrained
2000 // Verify that the champ is better than previous candidates.
2001 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2002 if (!more_constrained (get_template_for_ordering (champ),
2003 get_template_for_ordering (c)))
2004 return NULL_TREE;
2007 return champ;
2011 /* Returns the template (one of the functions given by TEMPLATE_ID)
2012 which can be specialized to match the indicated DECL with the
2013 explicit template args given in TEMPLATE_ID. The DECL may be
2014 NULL_TREE if none is available. In that case, the functions in
2015 TEMPLATE_ID are non-members.
2017 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2018 specialization of a member template.
2020 The TEMPLATE_COUNT is the number of references to qualifying
2021 template classes that appeared in the name of the function. See
2022 check_explicit_specialization for a more accurate description.
2024 TSK indicates what kind of template declaration (if any) is being
2025 declared. TSK_TEMPLATE indicates that the declaration given by
2026 DECL, though a FUNCTION_DECL, has template parameters, and is
2027 therefore a template function.
2029 The template args (those explicitly specified and those deduced)
2030 are output in a newly created vector *TARGS_OUT.
2032 If it is impossible to determine the result, an error message is
2033 issued. The error_mark_node is returned to indicate failure. */
2035 static tree
2036 determine_specialization (tree template_id,
2037 tree decl,
2038 tree* targs_out,
2039 int need_member_template,
2040 int template_count,
2041 tmpl_spec_kind tsk)
2043 tree fns;
2044 tree targs;
2045 tree explicit_targs;
2046 tree candidates = NULL_TREE;
2048 /* A TREE_LIST of templates of which DECL may be a specialization.
2049 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2050 corresponding TREE_PURPOSE is the set of template arguments that,
2051 when used to instantiate the template, would produce a function
2052 with the signature of DECL. */
2053 tree templates = NULL_TREE;
2054 int header_count;
2055 cp_binding_level *b;
2057 *targs_out = NULL_TREE;
2059 if (template_id == error_mark_node || decl == error_mark_node)
2060 return error_mark_node;
2062 /* We shouldn't be specializing a member template of an
2063 unspecialized class template; we already gave an error in
2064 check_specialization_scope, now avoid crashing. */
2065 if (template_count && DECL_CLASS_SCOPE_P (decl)
2066 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2068 gcc_assert (errorcount);
2069 return error_mark_node;
2072 fns = TREE_OPERAND (template_id, 0);
2073 explicit_targs = TREE_OPERAND (template_id, 1);
2075 if (fns == error_mark_node)
2076 return error_mark_node;
2078 /* Check for baselinks. */
2079 if (BASELINK_P (fns))
2080 fns = BASELINK_FUNCTIONS (fns);
2082 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2084 error ("%qD is not a function template", fns);
2085 return error_mark_node;
2087 else if (VAR_P (decl) && !variable_template_p (fns))
2089 error ("%qD is not a variable template", fns);
2090 return error_mark_node;
2093 /* Count the number of template headers specified for this
2094 specialization. */
2095 header_count = 0;
2096 for (b = current_binding_level;
2097 b->kind == sk_template_parms;
2098 b = b->level_chain)
2099 ++header_count;
2101 tree orig_fns = fns;
2103 if (variable_template_p (fns))
2105 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2106 targs = coerce_template_parms (parms, explicit_targs, fns,
2107 tf_warning_or_error,
2108 /*req_all*/true, /*use_defarg*/true);
2109 if (targs != error_mark_node)
2110 templates = tree_cons (targs, fns, templates);
2112 else for (; fns; fns = OVL_NEXT (fns))
2114 tree fn = OVL_CURRENT (fns);
2116 if (TREE_CODE (fn) == TEMPLATE_DECL)
2118 tree decl_arg_types;
2119 tree fn_arg_types;
2120 tree insttype;
2122 /* In case of explicit specialization, we need to check if
2123 the number of template headers appearing in the specialization
2124 is correct. This is usually done in check_explicit_specialization,
2125 but the check done there cannot be exhaustive when specializing
2126 member functions. Consider the following code:
2128 template <> void A<int>::f(int);
2129 template <> template <> void A<int>::f(int);
2131 Assuming that A<int> is not itself an explicit specialization
2132 already, the first line specializes "f" which is a non-template
2133 member function, whilst the second line specializes "f" which
2134 is a template member function. So both lines are syntactically
2135 correct, and check_explicit_specialization does not reject
2136 them.
2138 Here, we can do better, as we are matching the specialization
2139 against the declarations. We count the number of template
2140 headers, and we check if they match TEMPLATE_COUNT + 1
2141 (TEMPLATE_COUNT is the number of qualifying template classes,
2142 plus there must be another header for the member template
2143 itself).
2145 Notice that if header_count is zero, this is not a
2146 specialization but rather a template instantiation, so there
2147 is no check we can perform here. */
2148 if (header_count && header_count != template_count + 1)
2149 continue;
2151 /* Check that the number of template arguments at the
2152 innermost level for DECL is the same as for FN. */
2153 if (current_binding_level->kind == sk_template_parms
2154 && !current_binding_level->explicit_spec_p
2155 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2156 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2157 (current_template_parms))))
2158 continue;
2160 /* DECL might be a specialization of FN. */
2161 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2162 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2164 /* For a non-static member function, we need to make sure
2165 that the const qualification is the same. Since
2166 get_bindings does not try to merge the "this" parameter,
2167 we must do the comparison explicitly. */
2168 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2169 && !same_type_p (TREE_VALUE (fn_arg_types),
2170 TREE_VALUE (decl_arg_types)))
2171 continue;
2173 /* Skip the "this" parameter and, for constructors of
2174 classes with virtual bases, the VTT parameter. A
2175 full specialization of a constructor will have a VTT
2176 parameter, but a template never will. */
2177 decl_arg_types
2178 = skip_artificial_parms_for (decl, decl_arg_types);
2179 fn_arg_types
2180 = skip_artificial_parms_for (fn, fn_arg_types);
2182 /* Function templates cannot be specializations; there are
2183 no partial specializations of functions. Therefore, if
2184 the type of DECL does not match FN, there is no
2185 match.
2187 Note that it should never be the case that we have both
2188 candidates added here, and for regular member functions
2189 below. */
2190 if (tsk == tsk_template)
2192 if (compparms (fn_arg_types, decl_arg_types))
2193 candidates = tree_cons (NULL_TREE, fn, candidates);
2194 continue;
2197 /* See whether this function might be a specialization of this
2198 template. Suppress access control because we might be trying
2199 to make this specialization a friend, and we have already done
2200 access control for the declaration of the specialization. */
2201 push_deferring_access_checks (dk_no_check);
2202 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2203 pop_deferring_access_checks ();
2205 if (!targs)
2206 /* We cannot deduce template arguments that when used to
2207 specialize TMPL will produce DECL. */
2208 continue;
2210 /* Remove, from the set of candidates, all those functions
2211 whose constraints are not satisfied. */
2212 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2213 continue;
2215 // Then, try to form the new function type.
2216 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2217 if (insttype == error_mark_node)
2218 continue;
2219 fn_arg_types
2220 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2221 if (!compparms (fn_arg_types, decl_arg_types))
2222 continue;
2224 /* Save this template, and the arguments deduced. */
2225 templates = tree_cons (targs, fn, templates);
2227 else if (need_member_template)
2228 /* FN is an ordinary member function, and we need a
2229 specialization of a member template. */
2231 else if (TREE_CODE (fn) != FUNCTION_DECL)
2232 /* We can get IDENTIFIER_NODEs here in certain erroneous
2233 cases. */
2235 else if (!DECL_FUNCTION_MEMBER_P (fn))
2236 /* This is just an ordinary non-member function. Nothing can
2237 be a specialization of that. */
2239 else if (DECL_ARTIFICIAL (fn))
2240 /* Cannot specialize functions that are created implicitly. */
2242 else
2244 tree decl_arg_types;
2246 /* This is an ordinary member function. However, since
2247 we're here, we can assume its enclosing class is a
2248 template class. For example,
2250 template <typename T> struct S { void f(); };
2251 template <> void S<int>::f() {}
2253 Here, S<int>::f is a non-template, but S<int> is a
2254 template class. If FN has the same type as DECL, we
2255 might be in business. */
2257 if (!DECL_TEMPLATE_INFO (fn))
2258 /* Its enclosing class is an explicit specialization
2259 of a template class. This is not a candidate. */
2260 continue;
2262 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2263 TREE_TYPE (TREE_TYPE (fn))))
2264 /* The return types differ. */
2265 continue;
2267 /* Adjust the type of DECL in case FN is a static member. */
2268 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2269 if (DECL_STATIC_FUNCTION_P (fn)
2270 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2271 decl_arg_types = TREE_CHAIN (decl_arg_types);
2273 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2274 decl_arg_types))
2275 continue;
2277 // If the deduced arguments do not satisfy the constraints,
2278 // this is not a candidate.
2279 if (flag_concepts && !constraints_satisfied_p (fn))
2280 continue;
2282 // Add the candidate.
2283 candidates = tree_cons (NULL_TREE, fn, candidates);
2287 if (templates && TREE_CHAIN (templates))
2289 /* We have:
2291 [temp.expl.spec]
2293 It is possible for a specialization with a given function
2294 signature to be instantiated from more than one function
2295 template. In such cases, explicit specification of the
2296 template arguments must be used to uniquely identify the
2297 function template specialization being specialized.
2299 Note that here, there's no suggestion that we're supposed to
2300 determine which of the candidate templates is most
2301 specialized. However, we, also have:
2303 [temp.func.order]
2305 Partial ordering of overloaded function template
2306 declarations is used in the following contexts to select
2307 the function template to which a function template
2308 specialization refers:
2310 -- when an explicit specialization refers to a function
2311 template.
2313 So, we do use the partial ordering rules, at least for now.
2314 This extension can only serve to make invalid programs valid,
2315 so it's safe. And, there is strong anecdotal evidence that
2316 the committee intended the partial ordering rules to apply;
2317 the EDG front end has that behavior, and John Spicer claims
2318 that the committee simply forgot to delete the wording in
2319 [temp.expl.spec]. */
2320 tree tmpl = most_specialized_instantiation (templates);
2321 if (tmpl != error_mark_node)
2323 templates = tmpl;
2324 TREE_CHAIN (templates) = NULL_TREE;
2328 // Concepts allows multiple declarations of member functions
2329 // with the same signature. Like above, we need to rely on
2330 // on the partial ordering of those candidates to determine which
2331 // is the best.
2332 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2334 if (tree cand = most_constrained_function (candidates))
2336 candidates = cand;
2337 TREE_CHAIN (cand) = NULL_TREE;
2341 if (templates == NULL_TREE && candidates == NULL_TREE)
2343 error ("template-id %qD for %q+D does not match any template "
2344 "declaration", template_id, decl);
2345 if (header_count && header_count != template_count + 1)
2346 inform (input_location, "saw %d %<template<>%>, need %d for "
2347 "specializing a member function template",
2348 header_count, template_count + 1);
2349 else
2350 print_candidates (orig_fns);
2351 return error_mark_node;
2353 else if ((templates && TREE_CHAIN (templates))
2354 || (candidates && TREE_CHAIN (candidates))
2355 || (templates && candidates))
2357 error ("ambiguous template specialization %qD for %q+D",
2358 template_id, decl);
2359 candidates = chainon (candidates, templates);
2360 print_candidates (candidates);
2361 return error_mark_node;
2364 /* We have one, and exactly one, match. */
2365 if (candidates)
2367 tree fn = TREE_VALUE (candidates);
2368 *targs_out = copy_node (DECL_TI_ARGS (fn));
2370 // Propagate the candidate's constraints to the declaration.
2371 set_constraints (decl, get_constraints (fn));
2373 /* DECL is a re-declaration or partial instantiation of a template
2374 function. */
2375 if (TREE_CODE (fn) == TEMPLATE_DECL)
2376 return fn;
2377 /* It was a specialization of an ordinary member function in a
2378 template class. */
2379 return DECL_TI_TEMPLATE (fn);
2382 /* It was a specialization of a template. */
2383 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2384 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2386 *targs_out = copy_node (targs);
2387 SET_TMPL_ARGS_LEVEL (*targs_out,
2388 TMPL_ARGS_DEPTH (*targs_out),
2389 TREE_PURPOSE (templates));
2391 else
2392 *targs_out = TREE_PURPOSE (templates);
2393 return TREE_VALUE (templates);
2396 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2397 but with the default argument values filled in from those in the
2398 TMPL_TYPES. */
2400 static tree
2401 copy_default_args_to_explicit_spec_1 (tree spec_types,
2402 tree tmpl_types)
2404 tree new_spec_types;
2406 if (!spec_types)
2407 return NULL_TREE;
2409 if (spec_types == void_list_node)
2410 return void_list_node;
2412 /* Substitute into the rest of the list. */
2413 new_spec_types =
2414 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2415 TREE_CHAIN (tmpl_types));
2417 /* Add the default argument for this parameter. */
2418 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2419 TREE_VALUE (spec_types),
2420 new_spec_types);
2423 /* DECL is an explicit specialization. Replicate default arguments
2424 from the template it specializes. (That way, code like:
2426 template <class T> void f(T = 3);
2427 template <> void f(double);
2428 void g () { f (); }
2430 works, as required.) An alternative approach would be to look up
2431 the correct default arguments at the call-site, but this approach
2432 is consistent with how implicit instantiations are handled. */
2434 static void
2435 copy_default_args_to_explicit_spec (tree decl)
2437 tree tmpl;
2438 tree spec_types;
2439 tree tmpl_types;
2440 tree new_spec_types;
2441 tree old_type;
2442 tree new_type;
2443 tree t;
2444 tree object_type = NULL_TREE;
2445 tree in_charge = NULL_TREE;
2446 tree vtt = NULL_TREE;
2448 /* See if there's anything we need to do. */
2449 tmpl = DECL_TI_TEMPLATE (decl);
2450 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2451 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2452 if (TREE_PURPOSE (t))
2453 break;
2454 if (!t)
2455 return;
2457 old_type = TREE_TYPE (decl);
2458 spec_types = TYPE_ARG_TYPES (old_type);
2460 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2462 /* Remove the this pointer, but remember the object's type for
2463 CV quals. */
2464 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2465 spec_types = TREE_CHAIN (spec_types);
2466 tmpl_types = TREE_CHAIN (tmpl_types);
2468 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2470 /* DECL may contain more parameters than TMPL due to the extra
2471 in-charge parameter in constructors and destructors. */
2472 in_charge = spec_types;
2473 spec_types = TREE_CHAIN (spec_types);
2475 if (DECL_HAS_VTT_PARM_P (decl))
2477 vtt = spec_types;
2478 spec_types = TREE_CHAIN (spec_types);
2482 /* Compute the merged default arguments. */
2483 new_spec_types =
2484 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2486 /* Compute the new FUNCTION_TYPE. */
2487 if (object_type)
2489 if (vtt)
2490 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2491 TREE_VALUE (vtt),
2492 new_spec_types);
2494 if (in_charge)
2495 /* Put the in-charge parameter back. */
2496 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2497 TREE_VALUE (in_charge),
2498 new_spec_types);
2500 new_type = build_method_type_directly (object_type,
2501 TREE_TYPE (old_type),
2502 new_spec_types);
2504 else
2505 new_type = build_function_type (TREE_TYPE (old_type),
2506 new_spec_types);
2507 new_type = cp_build_type_attribute_variant (new_type,
2508 TYPE_ATTRIBUTES (old_type));
2509 new_type = build_exception_variant (new_type,
2510 TYPE_RAISES_EXCEPTIONS (old_type));
2512 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2513 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2515 TREE_TYPE (decl) = new_type;
2518 /* Return the number of template headers we expect to see for a definition
2519 or specialization of CTYPE or one of its non-template members. */
2522 num_template_headers_for_class (tree ctype)
2524 int num_templates = 0;
2526 while (ctype && CLASS_TYPE_P (ctype))
2528 /* You're supposed to have one `template <...>' for every
2529 template class, but you don't need one for a full
2530 specialization. For example:
2532 template <class T> struct S{};
2533 template <> struct S<int> { void f(); };
2534 void S<int>::f () {}
2536 is correct; there shouldn't be a `template <>' for the
2537 definition of `S<int>::f'. */
2538 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2539 /* If CTYPE does not have template information of any
2540 kind, then it is not a template, nor is it nested
2541 within a template. */
2542 break;
2543 if (explicit_class_specialization_p (ctype))
2544 break;
2545 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2546 ++num_templates;
2548 ctype = TYPE_CONTEXT (ctype);
2551 return num_templates;
2554 /* Do a simple sanity check on the template headers that precede the
2555 variable declaration DECL. */
2557 void
2558 check_template_variable (tree decl)
2560 tree ctx = CP_DECL_CONTEXT (decl);
2561 int wanted = num_template_headers_for_class (ctx);
2562 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2563 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2565 if (cxx_dialect < cxx14)
2566 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2567 "variable templates only available with "
2568 "-std=c++14 or -std=gnu++14");
2570 // Namespace-scope variable templates should have a template header.
2571 ++wanted;
2573 if (template_header_count > wanted)
2575 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2576 "too many template headers for %D (should be %d)",
2577 decl, wanted);
2578 if (warned && CLASS_TYPE_P (ctx)
2579 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2580 inform (DECL_SOURCE_LOCATION (decl),
2581 "members of an explicitly specialized class are defined "
2582 "without a template header");
2586 /* Check to see if the function just declared, as indicated in
2587 DECLARATOR, and in DECL, is a specialization of a function
2588 template. We may also discover that the declaration is an explicit
2589 instantiation at this point.
2591 Returns DECL, or an equivalent declaration that should be used
2592 instead if all goes well. Issues an error message if something is
2593 amiss. Returns error_mark_node if the error is not easily
2594 recoverable.
2596 FLAGS is a bitmask consisting of the following flags:
2598 2: The function has a definition.
2599 4: The function is a friend.
2601 The TEMPLATE_COUNT is the number of references to qualifying
2602 template classes that appeared in the name of the function. For
2603 example, in
2605 template <class T> struct S { void f(); };
2606 void S<int>::f();
2608 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2609 classes are not counted in the TEMPLATE_COUNT, so that in
2611 template <class T> struct S {};
2612 template <> struct S<int> { void f(); }
2613 template <> void S<int>::f();
2615 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2616 invalid; there should be no template <>.)
2618 If the function is a specialization, it is marked as such via
2619 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2620 is set up correctly, and it is added to the list of specializations
2621 for that template. */
2623 tree
2624 check_explicit_specialization (tree declarator,
2625 tree decl,
2626 int template_count,
2627 int flags)
2629 int have_def = flags & 2;
2630 int is_friend = flags & 4;
2631 bool is_concept = flags & 8;
2632 int specialization = 0;
2633 int explicit_instantiation = 0;
2634 int member_specialization = 0;
2635 tree ctype = DECL_CLASS_CONTEXT (decl);
2636 tree dname = DECL_NAME (decl);
2637 tmpl_spec_kind tsk;
2639 if (is_friend)
2641 if (!processing_specialization)
2642 tsk = tsk_none;
2643 else
2644 tsk = tsk_excessive_parms;
2646 else
2647 tsk = current_tmpl_spec_kind (template_count);
2649 switch (tsk)
2651 case tsk_none:
2652 if (processing_specialization && !VAR_P (decl))
2654 specialization = 1;
2655 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2657 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2659 if (is_friend)
2660 /* This could be something like:
2662 template <class T> void f(T);
2663 class S { friend void f<>(int); } */
2664 specialization = 1;
2665 else
2667 /* This case handles bogus declarations like template <>
2668 template <class T> void f<int>(); */
2670 error ("template-id %qD in declaration of primary template",
2671 declarator);
2672 return decl;
2675 break;
2677 case tsk_invalid_member_spec:
2678 /* The error has already been reported in
2679 check_specialization_scope. */
2680 return error_mark_node;
2682 case tsk_invalid_expl_inst:
2683 error ("template parameter list used in explicit instantiation");
2685 /* Fall through. */
2687 case tsk_expl_inst:
2688 if (have_def)
2689 error ("definition provided for explicit instantiation");
2691 explicit_instantiation = 1;
2692 break;
2694 case tsk_excessive_parms:
2695 case tsk_insufficient_parms:
2696 if (tsk == tsk_excessive_parms)
2697 error ("too many template parameter lists in declaration of %qD",
2698 decl);
2699 else if (template_header_count)
2700 error("too few template parameter lists in declaration of %qD", decl);
2701 else
2702 error("explicit specialization of %qD must be introduced by "
2703 "%<template <>%>", decl);
2705 /* Fall through. */
2706 case tsk_expl_spec:
2707 if (is_concept)
2708 error ("explicit specialization declared %<concept%>");
2710 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2711 /* In cases like template<> constexpr bool v = true;
2712 We'll give an error in check_template_variable. */
2713 break;
2715 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2716 if (ctype)
2717 member_specialization = 1;
2718 else
2719 specialization = 1;
2720 break;
2722 case tsk_template:
2723 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2725 /* This case handles bogus declarations like template <>
2726 template <class T> void f<int>(); */
2728 if (!uses_template_parms (declarator))
2729 error ("template-id %qD in declaration of primary template",
2730 declarator);
2731 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2733 /* Partial specialization of variable template. */
2734 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2735 specialization = 1;
2736 goto ok;
2738 else if (cxx_dialect < cxx14)
2739 error ("non-type partial specialization %qD "
2740 "is not allowed", declarator);
2741 else
2742 error ("non-class, non-variable partial specialization %qD "
2743 "is not allowed", declarator);
2744 return decl;
2745 ok:;
2748 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2749 /* This is a specialization of a member template, without
2750 specialization the containing class. Something like:
2752 template <class T> struct S {
2753 template <class U> void f (U);
2755 template <> template <class U> void S<int>::f(U) {}
2757 That's a specialization -- but of the entire template. */
2758 specialization = 1;
2759 break;
2761 default:
2762 gcc_unreachable ();
2765 if ((specialization || member_specialization)
2766 /* This doesn't apply to variable templates. */
2767 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2768 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2770 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2771 for (; t; t = TREE_CHAIN (t))
2772 if (TREE_PURPOSE (t))
2774 permerror (input_location,
2775 "default argument specified in explicit specialization");
2776 break;
2780 if (specialization || member_specialization || explicit_instantiation)
2782 tree tmpl = NULL_TREE;
2783 tree targs = NULL_TREE;
2784 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2786 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2787 if (!was_template_id)
2789 tree fns;
2791 gcc_assert (identifier_p (declarator));
2792 if (ctype)
2793 fns = dname;
2794 else
2796 /* If there is no class context, the explicit instantiation
2797 must be at namespace scope. */
2798 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2800 /* Find the namespace binding, using the declaration
2801 context. */
2802 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2803 false, true);
2804 if (fns == error_mark_node || !is_overloaded_fn (fns))
2806 error ("%qD is not a template function", dname);
2807 fns = error_mark_node;
2811 declarator = lookup_template_function (fns, NULL_TREE);
2814 if (declarator == error_mark_node)
2815 return error_mark_node;
2817 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2819 if (!explicit_instantiation)
2820 /* A specialization in class scope. This is invalid,
2821 but the error will already have been flagged by
2822 check_specialization_scope. */
2823 return error_mark_node;
2824 else
2826 /* It's not valid to write an explicit instantiation in
2827 class scope, e.g.:
2829 class C { template void f(); }
2831 This case is caught by the parser. However, on
2832 something like:
2834 template class C { void f(); };
2836 (which is invalid) we can get here. The error will be
2837 issued later. */
2841 return decl;
2843 else if (ctype != NULL_TREE
2844 && (identifier_p (TREE_OPERAND (declarator, 0))))
2846 // We'll match variable templates in start_decl.
2847 if (VAR_P (decl))
2848 return decl;
2850 /* Find the list of functions in ctype that have the same
2851 name as the declared function. */
2852 tree name = TREE_OPERAND (declarator, 0);
2853 tree fns = NULL_TREE;
2854 int idx;
2856 if (constructor_name_p (name, ctype))
2858 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2860 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2861 : !CLASSTYPE_DESTRUCTORS (ctype))
2863 /* From [temp.expl.spec]:
2865 If such an explicit specialization for the member
2866 of a class template names an implicitly-declared
2867 special member function (clause _special_), the
2868 program is ill-formed.
2870 Similar language is found in [temp.explicit]. */
2871 error ("specialization of implicitly-declared special member function");
2872 return error_mark_node;
2875 name = is_constructor ? ctor_identifier : dtor_identifier;
2878 if (!DECL_CONV_FN_P (decl))
2880 idx = lookup_fnfields_1 (ctype, name);
2881 if (idx >= 0)
2882 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2884 else
2886 vec<tree, va_gc> *methods;
2887 tree ovl;
2889 /* For a type-conversion operator, we cannot do a
2890 name-based lookup. We might be looking for `operator
2891 int' which will be a specialization of `operator T'.
2892 So, we find *all* the conversion operators, and then
2893 select from them. */
2894 fns = NULL_TREE;
2896 methods = CLASSTYPE_METHOD_VEC (ctype);
2897 if (methods)
2898 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2899 methods->iterate (idx, &ovl);
2900 ++idx)
2902 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2903 /* There are no more conversion functions. */
2904 break;
2906 /* Glue all these conversion functions together
2907 with those we already have. */
2908 for (; ovl; ovl = OVL_NEXT (ovl))
2909 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2913 if (fns == NULL_TREE)
2915 error ("no member function %qD declared in %qT", name, ctype);
2916 return error_mark_node;
2918 else
2919 TREE_OPERAND (declarator, 0) = fns;
2922 /* Figure out what exactly is being specialized at this point.
2923 Note that for an explicit instantiation, even one for a
2924 member function, we cannot tell apriori whether the
2925 instantiation is for a member template, or just a member
2926 function of a template class. Even if a member template is
2927 being instantiated, the member template arguments may be
2928 elided if they can be deduced from the rest of the
2929 declaration. */
2930 tmpl = determine_specialization (declarator, decl,
2931 &targs,
2932 member_specialization,
2933 template_count,
2934 tsk);
2936 if (!tmpl || tmpl == error_mark_node)
2937 /* We couldn't figure out what this declaration was
2938 specializing. */
2939 return error_mark_node;
2940 else
2942 if (!ctype && !was_template_id
2943 && (specialization || member_specialization
2944 || explicit_instantiation)
2945 && !is_associated_namespace (CP_DECL_CONTEXT (decl),
2946 CP_DECL_CONTEXT (tmpl)))
2947 error ("%qD is not declared in %qD",
2948 tmpl, current_namespace);
2950 tree gen_tmpl = most_general_template (tmpl);
2952 if (explicit_instantiation)
2954 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2955 is done by do_decl_instantiation later. */
2957 int arg_depth = TMPL_ARGS_DEPTH (targs);
2958 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2960 if (arg_depth > parm_depth)
2962 /* If TMPL is not the most general template (for
2963 example, if TMPL is a friend template that is
2964 injected into namespace scope), then there will
2965 be too many levels of TARGS. Remove some of them
2966 here. */
2967 int i;
2968 tree new_targs;
2970 new_targs = make_tree_vec (parm_depth);
2971 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2972 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2973 = TREE_VEC_ELT (targs, i);
2974 targs = new_targs;
2977 return instantiate_template (tmpl, targs, tf_error);
2980 /* If we thought that the DECL was a member function, but it
2981 turns out to be specializing a static member function,
2982 make DECL a static member function as well. */
2983 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2984 && DECL_STATIC_FUNCTION_P (tmpl)
2985 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2986 revert_static_member_fn (decl);
2988 /* If this is a specialization of a member template of a
2989 template class, we want to return the TEMPLATE_DECL, not
2990 the specialization of it. */
2991 if (tsk == tsk_template && !was_template_id)
2993 tree result = DECL_TEMPLATE_RESULT (tmpl);
2994 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2995 DECL_INITIAL (result) = NULL_TREE;
2996 if (have_def)
2998 tree parm;
2999 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3000 DECL_SOURCE_LOCATION (result)
3001 = DECL_SOURCE_LOCATION (decl);
3002 /* We want to use the argument list specified in the
3003 definition, not in the original declaration. */
3004 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3005 for (parm = DECL_ARGUMENTS (result); parm;
3006 parm = DECL_CHAIN (parm))
3007 DECL_CONTEXT (parm) = result;
3009 return register_specialization (tmpl, gen_tmpl, targs,
3010 is_friend, 0);
3013 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3014 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3016 if (was_template_id)
3017 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3019 /* Inherit default function arguments from the template
3020 DECL is specializing. */
3021 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3022 copy_default_args_to_explicit_spec (decl);
3024 /* This specialization has the same protection as the
3025 template it specializes. */
3026 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3027 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3029 /* 7.1.1-1 [dcl.stc]
3031 A storage-class-specifier shall not be specified in an
3032 explicit specialization...
3034 The parser rejects these, so unless action is taken here,
3035 explicit function specializations will always appear with
3036 global linkage.
3038 The action recommended by the C++ CWG in response to C++
3039 defect report 605 is to make the storage class and linkage
3040 of the explicit specialization match the templated function:
3042 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3044 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3046 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3047 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3049 /* A concept cannot be specialized. */
3050 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3052 error ("explicit specialization of function concept %qD",
3053 gen_tmpl);
3054 return error_mark_node;
3057 /* This specialization has the same linkage and visibility as
3058 the function template it specializes. */
3059 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3060 if (! TREE_PUBLIC (decl))
3062 DECL_INTERFACE_KNOWN (decl) = 1;
3063 DECL_NOT_REALLY_EXTERN (decl) = 1;
3065 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3066 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3068 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3069 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3073 /* If DECL is a friend declaration, declared using an
3074 unqualified name, the namespace associated with DECL may
3075 have been set incorrectly. For example, in:
3077 template <typename T> void f(T);
3078 namespace N {
3079 struct S { friend void f<int>(int); }
3082 we will have set the DECL_CONTEXT for the friend
3083 declaration to N, rather than to the global namespace. */
3084 if (DECL_NAMESPACE_SCOPE_P (decl))
3085 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3087 if (is_friend && !have_def)
3088 /* This is not really a declaration of a specialization.
3089 It's just the name of an instantiation. But, it's not
3090 a request for an instantiation, either. */
3091 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3092 else if (TREE_CODE (decl) == FUNCTION_DECL)
3093 /* A specialization is not necessarily COMDAT. */
3094 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3095 && DECL_DECLARED_INLINE_P (decl));
3096 else if (VAR_P (decl))
3097 DECL_COMDAT (decl) = false;
3099 /* If this is a full specialization, register it so that we can find
3100 it again. Partial specializations will be registered in
3101 process_partial_specialization. */
3102 if (!processing_template_decl)
3103 decl = register_specialization (decl, gen_tmpl, targs,
3104 is_friend, 0);
3106 /* A 'structor should already have clones. */
3107 gcc_assert (decl == error_mark_node
3108 || variable_template_p (tmpl)
3109 || !(DECL_CONSTRUCTOR_P (decl)
3110 || DECL_DESTRUCTOR_P (decl))
3111 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3115 return decl;
3118 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3119 parameters. These are represented in the same format used for
3120 DECL_TEMPLATE_PARMS. */
3123 comp_template_parms (const_tree parms1, const_tree parms2)
3125 const_tree p1;
3126 const_tree p2;
3128 if (parms1 == parms2)
3129 return 1;
3131 for (p1 = parms1, p2 = parms2;
3132 p1 != NULL_TREE && p2 != NULL_TREE;
3133 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3135 tree t1 = TREE_VALUE (p1);
3136 tree t2 = TREE_VALUE (p2);
3137 int i;
3139 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3140 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3142 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3143 return 0;
3145 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3147 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3148 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3150 /* If either of the template parameters are invalid, assume
3151 they match for the sake of error recovery. */
3152 if (error_operand_p (parm1) || error_operand_p (parm2))
3153 return 1;
3155 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3156 return 0;
3158 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3159 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3160 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3161 continue;
3162 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3163 return 0;
3167 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3168 /* One set of parameters has more parameters lists than the
3169 other. */
3170 return 0;
3172 return 1;
3175 /* Determine whether PARM is a parameter pack. */
3177 bool
3178 template_parameter_pack_p (const_tree parm)
3180 /* Determine if we have a non-type template parameter pack. */
3181 if (TREE_CODE (parm) == PARM_DECL)
3182 return (DECL_TEMPLATE_PARM_P (parm)
3183 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3184 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3185 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3187 /* If this is a list of template parameters, we could get a
3188 TYPE_DECL or a TEMPLATE_DECL. */
3189 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3190 parm = TREE_TYPE (parm);
3192 /* Otherwise it must be a type template parameter. */
3193 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3194 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3195 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3198 /* Determine if T is a function parameter pack. */
3200 bool
3201 function_parameter_pack_p (const_tree t)
3203 if (t && TREE_CODE (t) == PARM_DECL)
3204 return DECL_PACK_P (t);
3205 return false;
3208 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3209 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3211 tree
3212 get_function_template_decl (const_tree primary_func_tmpl_inst)
3214 if (! primary_func_tmpl_inst
3215 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3216 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3217 return NULL;
3219 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3222 /* Return true iff the function parameter PARAM_DECL was expanded
3223 from the function parameter pack PACK. */
3225 bool
3226 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3228 if (DECL_ARTIFICIAL (param_decl)
3229 || !function_parameter_pack_p (pack))
3230 return false;
3232 /* The parameter pack and its pack arguments have the same
3233 DECL_PARM_INDEX. */
3234 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3237 /* Determine whether ARGS describes a variadic template args list,
3238 i.e., one that is terminated by a template argument pack. */
3240 static bool
3241 template_args_variadic_p (tree args)
3243 int nargs;
3244 tree last_parm;
3246 if (args == NULL_TREE)
3247 return false;
3249 args = INNERMOST_TEMPLATE_ARGS (args);
3250 nargs = TREE_VEC_LENGTH (args);
3252 if (nargs == 0)
3253 return false;
3255 last_parm = TREE_VEC_ELT (args, nargs - 1);
3257 return ARGUMENT_PACK_P (last_parm);
3260 /* Generate a new name for the parameter pack name NAME (an
3261 IDENTIFIER_NODE) that incorporates its */
3263 static tree
3264 make_ith_pack_parameter_name (tree name, int i)
3266 /* Munge the name to include the parameter index. */
3267 #define NUMBUF_LEN 128
3268 char numbuf[NUMBUF_LEN];
3269 char* newname;
3270 int newname_len;
3272 if (name == NULL_TREE)
3273 return name;
3274 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3275 newname_len = IDENTIFIER_LENGTH (name)
3276 + strlen (numbuf) + 2;
3277 newname = (char*)alloca (newname_len);
3278 snprintf (newname, newname_len,
3279 "%s#%i", IDENTIFIER_POINTER (name), i);
3280 return get_identifier (newname);
3283 /* Return true if T is a primary function, class or alias template
3284 instantiation. */
3286 bool
3287 primary_template_instantiation_p (const_tree t)
3289 if (!t)
3290 return false;
3292 if (TREE_CODE (t) == FUNCTION_DECL)
3293 return DECL_LANG_SPECIFIC (t)
3294 && DECL_TEMPLATE_INSTANTIATION (t)
3295 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3296 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3297 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3299 else if (alias_template_specialization_p (t))
3300 return true;
3301 return false;
3304 /* Return true if PARM is a template template parameter. */
3306 bool
3307 template_template_parameter_p (const_tree parm)
3309 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3312 /* Return true iff PARM is a DECL representing a type template
3313 parameter. */
3315 bool
3316 template_type_parameter_p (const_tree parm)
3318 return (parm
3319 && (TREE_CODE (parm) == TYPE_DECL
3320 || TREE_CODE (parm) == TEMPLATE_DECL)
3321 && DECL_TEMPLATE_PARM_P (parm));
3324 /* Return the template parameters of T if T is a
3325 primary template instantiation, NULL otherwise. */
3327 tree
3328 get_primary_template_innermost_parameters (const_tree t)
3330 tree parms = NULL, template_info = NULL;
3332 if ((template_info = get_template_info (t))
3333 && primary_template_instantiation_p (t))
3334 parms = INNERMOST_TEMPLATE_PARMS
3335 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3337 return parms;
3340 /* Return the template parameters of the LEVELth level from the full list
3341 of template parameters PARMS. */
3343 tree
3344 get_template_parms_at_level (tree parms, int level)
3346 tree p;
3347 if (!parms
3348 || TREE_CODE (parms) != TREE_LIST
3349 || level > TMPL_PARMS_DEPTH (parms))
3350 return NULL_TREE;
3352 for (p = parms; p; p = TREE_CHAIN (p))
3353 if (TMPL_PARMS_DEPTH (p) == level)
3354 return p;
3356 return NULL_TREE;
3359 /* Returns the template arguments of T if T is a template instantiation,
3360 NULL otherwise. */
3362 tree
3363 get_template_innermost_arguments (const_tree t)
3365 tree args = NULL, template_info = NULL;
3367 if ((template_info = get_template_info (t))
3368 && TI_ARGS (template_info))
3369 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3371 return args;
3374 /* Return the argument pack elements of T if T is a template argument pack,
3375 NULL otherwise. */
3377 tree
3378 get_template_argument_pack_elems (const_tree t)
3380 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3381 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3382 return NULL;
3384 return ARGUMENT_PACK_ARGS (t);
3387 /* Structure used to track the progress of find_parameter_packs_r. */
3388 struct find_parameter_pack_data
3390 /* TREE_LIST that will contain all of the parameter packs found by
3391 the traversal. */
3392 tree* parameter_packs;
3394 /* Set of AST nodes that have been visited by the traversal. */
3395 hash_set<tree> *visited;
3397 /* True iff we're making a type pack expansion. */
3398 bool type_pack_expansion_p;
3401 /* Identifies all of the argument packs that occur in a template
3402 argument and appends them to the TREE_LIST inside DATA, which is a
3403 find_parameter_pack_data structure. This is a subroutine of
3404 make_pack_expansion and uses_parameter_packs. */
3405 static tree
3406 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3408 tree t = *tp;
3409 struct find_parameter_pack_data* ppd =
3410 (struct find_parameter_pack_data*)data;
3411 bool parameter_pack_p = false;
3413 /* Handle type aliases/typedefs. */
3414 if (TYPE_ALIAS_P (t))
3416 if (TYPE_TEMPLATE_INFO (t))
3417 cp_walk_tree (&TYPE_TI_ARGS (t),
3418 &find_parameter_packs_r,
3419 ppd, ppd->visited);
3420 *walk_subtrees = 0;
3421 return NULL_TREE;
3424 /* Identify whether this is a parameter pack or not. */
3425 switch (TREE_CODE (t))
3427 case TEMPLATE_PARM_INDEX:
3428 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3429 parameter_pack_p = true;
3430 break;
3432 case TEMPLATE_TYPE_PARM:
3433 t = TYPE_MAIN_VARIANT (t);
3434 case TEMPLATE_TEMPLATE_PARM:
3435 /* If the placeholder appears in the decl-specifier-seq of a function
3436 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3437 is a pack expansion, the invented template parameter is a template
3438 parameter pack. */
3439 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3440 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3441 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3442 parameter_pack_p = true;
3443 break;
3445 case FIELD_DECL:
3446 case PARM_DECL:
3447 if (DECL_PACK_P (t))
3449 /* We don't want to walk into the type of a PARM_DECL,
3450 because we don't want to see the type parameter pack. */
3451 *walk_subtrees = 0;
3452 parameter_pack_p = true;
3454 break;
3456 /* Look through a lambda capture proxy to the field pack. */
3457 case VAR_DECL:
3458 if (DECL_HAS_VALUE_EXPR_P (t))
3460 tree v = DECL_VALUE_EXPR (t);
3461 cp_walk_tree (&v,
3462 &find_parameter_packs_r,
3463 ppd, ppd->visited);
3464 *walk_subtrees = 0;
3466 else if (variable_template_specialization_p (t))
3468 cp_walk_tree (&DECL_TI_ARGS (t),
3469 find_parameter_packs_r,
3470 ppd, ppd->visited);
3471 *walk_subtrees = 0;
3473 break;
3475 case BASES:
3476 parameter_pack_p = true;
3477 break;
3478 default:
3479 /* Not a parameter pack. */
3480 break;
3483 if (parameter_pack_p)
3485 /* Add this parameter pack to the list. */
3486 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3489 if (TYPE_P (t))
3490 cp_walk_tree (&TYPE_CONTEXT (t),
3491 &find_parameter_packs_r, ppd, ppd->visited);
3493 /* This switch statement will return immediately if we don't find a
3494 parameter pack. */
3495 switch (TREE_CODE (t))
3497 case TEMPLATE_PARM_INDEX:
3498 return NULL_TREE;
3500 case BOUND_TEMPLATE_TEMPLATE_PARM:
3501 /* Check the template itself. */
3502 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3503 &find_parameter_packs_r, ppd, ppd->visited);
3504 /* Check the template arguments. */
3505 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3506 ppd->visited);
3507 *walk_subtrees = 0;
3508 return NULL_TREE;
3510 case TEMPLATE_TYPE_PARM:
3511 case TEMPLATE_TEMPLATE_PARM:
3512 return NULL_TREE;
3514 case PARM_DECL:
3515 return NULL_TREE;
3517 case RECORD_TYPE:
3518 if (TYPE_PTRMEMFUNC_P (t))
3519 return NULL_TREE;
3520 /* Fall through. */
3522 case UNION_TYPE:
3523 case ENUMERAL_TYPE:
3524 if (TYPE_TEMPLATE_INFO (t))
3525 cp_walk_tree (&TYPE_TI_ARGS (t),
3526 &find_parameter_packs_r, ppd, ppd->visited);
3528 *walk_subtrees = 0;
3529 return NULL_TREE;
3531 case CONSTRUCTOR:
3532 case TEMPLATE_DECL:
3533 cp_walk_tree (&TREE_TYPE (t),
3534 &find_parameter_packs_r, ppd, ppd->visited);
3535 return NULL_TREE;
3537 case TYPENAME_TYPE:
3538 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3539 ppd, ppd->visited);
3540 *walk_subtrees = 0;
3541 return NULL_TREE;
3543 case TYPE_PACK_EXPANSION:
3544 case EXPR_PACK_EXPANSION:
3545 *walk_subtrees = 0;
3546 return NULL_TREE;
3548 case INTEGER_TYPE:
3549 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3550 ppd, ppd->visited);
3551 *walk_subtrees = 0;
3552 return NULL_TREE;
3554 case IDENTIFIER_NODE:
3555 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3556 ppd->visited);
3557 *walk_subtrees = 0;
3558 return NULL_TREE;
3560 case DECLTYPE_TYPE:
3562 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3563 type_pack_expansion_p to false so that any placeholders
3564 within the expression don't get marked as parameter packs. */
3565 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3566 ppd->type_pack_expansion_p = false;
3567 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3568 ppd, ppd->visited);
3569 ppd->type_pack_expansion_p = type_pack_expansion_p;
3570 *walk_subtrees = 0;
3571 return NULL_TREE;
3574 default:
3575 return NULL_TREE;
3578 return NULL_TREE;
3581 /* Determines if the expression or type T uses any parameter packs. */
3582 bool
3583 uses_parameter_packs (tree t)
3585 tree parameter_packs = NULL_TREE;
3586 struct find_parameter_pack_data ppd;
3587 ppd.parameter_packs = &parameter_packs;
3588 ppd.visited = new hash_set<tree>;
3589 ppd.type_pack_expansion_p = false;
3590 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3591 delete ppd.visited;
3592 return parameter_packs != NULL_TREE;
3595 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3596 representation a base-class initializer into a parameter pack
3597 expansion. If all goes well, the resulting node will be an
3598 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3599 respectively. */
3600 tree
3601 make_pack_expansion (tree arg)
3603 tree result;
3604 tree parameter_packs = NULL_TREE;
3605 bool for_types = false;
3606 struct find_parameter_pack_data ppd;
3608 if (!arg || arg == error_mark_node)
3609 return arg;
3611 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3613 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3614 class initializer. In this case, the TREE_PURPOSE will be a
3615 _TYPE node (representing the base class expansion we're
3616 initializing) and the TREE_VALUE will be a TREE_LIST
3617 containing the initialization arguments.
3619 The resulting expansion looks somewhat different from most
3620 expansions. Rather than returning just one _EXPANSION, we
3621 return a TREE_LIST whose TREE_PURPOSE is a
3622 TYPE_PACK_EXPANSION containing the bases that will be
3623 initialized. The TREE_VALUE will be identical to the
3624 original TREE_VALUE, which is a list of arguments that will
3625 be passed to each base. We do not introduce any new pack
3626 expansion nodes into the TREE_VALUE (although it is possible
3627 that some already exist), because the TREE_PURPOSE and
3628 TREE_VALUE all need to be expanded together with the same
3629 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3630 resulting TREE_PURPOSE will mention the parameter packs in
3631 both the bases and the arguments to the bases. */
3632 tree purpose;
3633 tree value;
3634 tree parameter_packs = NULL_TREE;
3636 /* Determine which parameter packs will be used by the base
3637 class expansion. */
3638 ppd.visited = new hash_set<tree>;
3639 ppd.parameter_packs = &parameter_packs;
3640 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3641 &ppd, ppd.visited);
3643 if (parameter_packs == NULL_TREE)
3645 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3646 delete ppd.visited;
3647 return error_mark_node;
3650 if (TREE_VALUE (arg) != void_type_node)
3652 /* Collect the sets of parameter packs used in each of the
3653 initialization arguments. */
3654 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3656 /* Determine which parameter packs will be expanded in this
3657 argument. */
3658 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3659 &ppd, ppd.visited);
3663 delete ppd.visited;
3665 /* Create the pack expansion type for the base type. */
3666 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3667 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3668 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3670 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3671 they will rarely be compared to anything. */
3672 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3674 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3677 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3678 for_types = true;
3680 /* Build the PACK_EXPANSION_* node. */
3681 result = for_types
3682 ? cxx_make_type (TYPE_PACK_EXPANSION)
3683 : make_node (EXPR_PACK_EXPANSION);
3684 SET_PACK_EXPANSION_PATTERN (result, arg);
3685 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3687 /* Propagate type and const-expression information. */
3688 TREE_TYPE (result) = TREE_TYPE (arg);
3689 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3691 else
3692 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3693 they will rarely be compared to anything. */
3694 SET_TYPE_STRUCTURAL_EQUALITY (result);
3696 /* Determine which parameter packs will be expanded. */
3697 ppd.parameter_packs = &parameter_packs;
3698 ppd.visited = new hash_set<tree>;
3699 ppd.type_pack_expansion_p = TYPE_P (arg);
3700 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3701 delete ppd.visited;
3703 /* Make sure we found some parameter packs. */
3704 if (parameter_packs == NULL_TREE)
3706 if (TYPE_P (arg))
3707 error ("expansion pattern %<%T%> contains no argument packs", arg);
3708 else
3709 error ("expansion pattern %<%E%> contains no argument packs", arg);
3710 return error_mark_node;
3712 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3714 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3716 return result;
3719 /* Checks T for any "bare" parameter packs, which have not yet been
3720 expanded, and issues an error if any are found. This operation can
3721 only be done on full expressions or types (e.g., an expression
3722 statement, "if" condition, etc.), because we could have expressions like:
3724 foo(f(g(h(args)))...)
3726 where "args" is a parameter pack. check_for_bare_parameter_packs
3727 should not be called for the subexpressions args, h(args),
3728 g(h(args)), or f(g(h(args))), because we would produce erroneous
3729 error messages.
3731 Returns TRUE and emits an error if there were bare parameter packs,
3732 returns FALSE otherwise. */
3733 bool
3734 check_for_bare_parameter_packs (tree t)
3736 tree parameter_packs = NULL_TREE;
3737 struct find_parameter_pack_data ppd;
3739 if (!processing_template_decl || !t || t == error_mark_node)
3740 return false;
3742 if (TREE_CODE (t) == TYPE_DECL)
3743 t = TREE_TYPE (t);
3745 ppd.parameter_packs = &parameter_packs;
3746 ppd.visited = new hash_set<tree>;
3747 ppd.type_pack_expansion_p = false;
3748 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3749 delete ppd.visited;
3751 if (parameter_packs)
3753 error ("parameter packs not expanded with %<...%>:");
3754 while (parameter_packs)
3756 tree pack = TREE_VALUE (parameter_packs);
3757 tree name = NULL_TREE;
3759 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3760 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3761 name = TYPE_NAME (pack);
3762 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3763 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3764 else
3765 name = DECL_NAME (pack);
3767 if (name)
3768 inform (input_location, " %qD", name);
3769 else
3770 inform (input_location, " <anonymous>");
3772 parameter_packs = TREE_CHAIN (parameter_packs);
3775 return true;
3778 return false;
3781 /* Expand any parameter packs that occur in the template arguments in
3782 ARGS. */
3783 tree
3784 expand_template_argument_pack (tree args)
3786 tree result_args = NULL_TREE;
3787 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3788 int num_result_args = -1;
3789 int non_default_args_count = -1;
3791 /* First, determine if we need to expand anything, and the number of
3792 slots we'll need. */
3793 for (in_arg = 0; in_arg < nargs; ++in_arg)
3795 tree arg = TREE_VEC_ELT (args, in_arg);
3796 if (arg == NULL_TREE)
3797 return args;
3798 if (ARGUMENT_PACK_P (arg))
3800 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3801 if (num_result_args < 0)
3802 num_result_args = in_arg + num_packed;
3803 else
3804 num_result_args += num_packed;
3806 else
3808 if (num_result_args >= 0)
3809 num_result_args++;
3813 /* If no expansion is necessary, we're done. */
3814 if (num_result_args < 0)
3815 return args;
3817 /* Expand arguments. */
3818 result_args = make_tree_vec (num_result_args);
3819 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3820 non_default_args_count =
3821 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3822 for (in_arg = 0; in_arg < nargs; ++in_arg)
3824 tree arg = TREE_VEC_ELT (args, in_arg);
3825 if (ARGUMENT_PACK_P (arg))
3827 tree packed = ARGUMENT_PACK_ARGS (arg);
3828 int i, num_packed = TREE_VEC_LENGTH (packed);
3829 for (i = 0; i < num_packed; ++i, ++out_arg)
3830 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3831 if (non_default_args_count > 0)
3832 non_default_args_count += num_packed - 1;
3834 else
3836 TREE_VEC_ELT (result_args, out_arg) = arg;
3837 ++out_arg;
3840 if (non_default_args_count >= 0)
3841 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3842 return result_args;
3845 /* Checks if DECL shadows a template parameter.
3847 [temp.local]: A template-parameter shall not be redeclared within its
3848 scope (including nested scopes).
3850 Emits an error and returns TRUE if the DECL shadows a parameter,
3851 returns FALSE otherwise. */
3853 bool
3854 check_template_shadow (tree decl)
3856 tree olddecl;
3858 /* If we're not in a template, we can't possibly shadow a template
3859 parameter. */
3860 if (!current_template_parms)
3861 return true;
3863 /* Figure out what we're shadowing. */
3864 if (TREE_CODE (decl) == OVERLOAD)
3865 decl = OVL_CURRENT (decl);
3866 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3868 /* If there's no previous binding for this name, we're not shadowing
3869 anything, let alone a template parameter. */
3870 if (!olddecl)
3871 return true;
3873 /* If we're not shadowing a template parameter, we're done. Note
3874 that OLDDECL might be an OVERLOAD (or perhaps even an
3875 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3876 node. */
3877 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3878 return true;
3880 /* We check for decl != olddecl to avoid bogus errors for using a
3881 name inside a class. We check TPFI to avoid duplicate errors for
3882 inline member templates. */
3883 if (decl == olddecl
3884 || (DECL_TEMPLATE_PARM_P (decl)
3885 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3886 return true;
3888 /* Don't complain about the injected class name, as we've already
3889 complained about the class itself. */
3890 if (DECL_SELF_REFERENCE_P (decl))
3891 return false;
3893 if (DECL_TEMPLATE_PARM_P (decl))
3894 error ("declaration of template parameter %q+D shadows "
3895 "template parameter", decl);
3896 else
3897 error ("declaration of %q+#D shadows template parameter", decl);
3898 inform (DECL_SOURCE_LOCATION (olddecl),
3899 "template parameter %qD declared here", olddecl);
3900 return false;
3903 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3904 ORIG_LEVEL, DECL, and TYPE. */
3906 static tree
3907 build_template_parm_index (int index,
3908 int level,
3909 int orig_level,
3910 tree decl,
3911 tree type)
3913 tree t = make_node (TEMPLATE_PARM_INDEX);
3914 TEMPLATE_PARM_IDX (t) = index;
3915 TEMPLATE_PARM_LEVEL (t) = level;
3916 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3917 TEMPLATE_PARM_DECL (t) = decl;
3918 TREE_TYPE (t) = type;
3919 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3920 TREE_READONLY (t) = TREE_READONLY (decl);
3922 return t;
3925 /* Find the canonical type parameter for the given template type
3926 parameter. Returns the canonical type parameter, which may be TYPE
3927 if no such parameter existed. */
3929 static tree
3930 canonical_type_parameter (tree type)
3932 tree list;
3933 int idx = TEMPLATE_TYPE_IDX (type);
3934 if (!canonical_template_parms)
3935 vec_alloc (canonical_template_parms, idx+1);
3937 while (canonical_template_parms->length () <= (unsigned)idx)
3938 vec_safe_push (canonical_template_parms, NULL_TREE);
3940 list = (*canonical_template_parms)[idx];
3941 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3942 list = TREE_CHAIN (list);
3944 if (list)
3945 return TREE_VALUE (list);
3946 else
3948 (*canonical_template_parms)[idx]
3949 = tree_cons (NULL_TREE, type,
3950 (*canonical_template_parms)[idx]);
3951 return type;
3955 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3956 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3957 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3958 new one is created. */
3960 static tree
3961 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3962 tsubst_flags_t complain)
3964 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3965 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3966 != TEMPLATE_PARM_LEVEL (index) - levels)
3967 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3969 tree orig_decl = TEMPLATE_PARM_DECL (index);
3970 tree decl, t;
3972 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3973 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3974 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3975 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3976 DECL_ARTIFICIAL (decl) = 1;
3977 SET_DECL_TEMPLATE_PARM_P (decl);
3979 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3980 TEMPLATE_PARM_LEVEL (index) - levels,
3981 TEMPLATE_PARM_ORIG_LEVEL (index),
3982 decl, type);
3983 TEMPLATE_PARM_DESCENDANTS (index) = t;
3984 TEMPLATE_PARM_PARAMETER_PACK (t)
3985 = TEMPLATE_PARM_PARAMETER_PACK (index);
3987 /* Template template parameters need this. */
3988 if (TREE_CODE (decl) == TEMPLATE_DECL)
3990 DECL_TEMPLATE_RESULT (decl)
3991 = build_decl (DECL_SOURCE_LOCATION (decl),
3992 TYPE_DECL, DECL_NAME (decl), type);
3993 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
3994 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3995 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
3999 return TEMPLATE_PARM_DESCENDANTS (index);
4002 /* Process information from new template parameter PARM and append it
4003 to the LIST being built. This new parameter is a non-type
4004 parameter iff IS_NON_TYPE is true. This new parameter is a
4005 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4006 is in PARM_LOC. */
4008 tree
4009 process_template_parm (tree list, location_t parm_loc, tree parm,
4010 bool is_non_type, bool is_parameter_pack)
4012 tree decl = 0;
4013 int idx = 0;
4015 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4016 tree defval = TREE_PURPOSE (parm);
4017 tree constr = TREE_TYPE (parm);
4019 if (list)
4021 tree p = tree_last (list);
4023 if (p && TREE_VALUE (p) != error_mark_node)
4025 p = TREE_VALUE (p);
4026 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4027 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4028 else
4029 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4032 ++idx;
4035 if (is_non_type)
4037 parm = TREE_VALUE (parm);
4039 SET_DECL_TEMPLATE_PARM_P (parm);
4041 if (TREE_TYPE (parm) != error_mark_node)
4043 /* [temp.param]
4045 The top-level cv-qualifiers on the template-parameter are
4046 ignored when determining its type. */
4047 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4048 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4049 TREE_TYPE (parm) = error_mark_node;
4050 else if (uses_parameter_packs (TREE_TYPE (parm))
4051 && !is_parameter_pack
4052 /* If we're in a nested template parameter list, the template
4053 template parameter could be a parameter pack. */
4054 && processing_template_parmlist == 1)
4056 /* This template parameter is not a parameter pack, but it
4057 should be. Complain about "bare" parameter packs. */
4058 check_for_bare_parameter_packs (TREE_TYPE (parm));
4060 /* Recover by calling this a parameter pack. */
4061 is_parameter_pack = true;
4065 /* A template parameter is not modifiable. */
4066 TREE_CONSTANT (parm) = 1;
4067 TREE_READONLY (parm) = 1;
4068 decl = build_decl (parm_loc,
4069 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4070 TREE_CONSTANT (decl) = 1;
4071 TREE_READONLY (decl) = 1;
4072 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4073 = build_template_parm_index (idx, processing_template_decl,
4074 processing_template_decl,
4075 decl, TREE_TYPE (parm));
4077 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4078 = is_parameter_pack;
4080 else
4082 tree t;
4083 parm = TREE_VALUE (TREE_VALUE (parm));
4085 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4087 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4088 /* This is for distinguishing between real templates and template
4089 template parameters */
4090 TREE_TYPE (parm) = t;
4091 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4092 decl = parm;
4094 else
4096 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4097 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4098 decl = build_decl (parm_loc,
4099 TYPE_DECL, parm, t);
4102 TYPE_NAME (t) = decl;
4103 TYPE_STUB_DECL (t) = decl;
4104 parm = decl;
4105 TEMPLATE_TYPE_PARM_INDEX (t)
4106 = build_template_parm_index (idx, processing_template_decl,
4107 processing_template_decl,
4108 decl, TREE_TYPE (parm));
4109 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4110 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4112 DECL_ARTIFICIAL (decl) = 1;
4113 SET_DECL_TEMPLATE_PARM_P (decl);
4115 /* Build requirements for the type/template parameter.
4116 This must be done after SET_DECL_TEMPLATE_PARM_P or
4117 process_template_parm could fail. */
4118 tree reqs = finish_shorthand_constraint (parm, constr);
4120 pushdecl (decl);
4122 /* Build the parameter node linking the parameter declaration,
4123 its default argument (if any), and its constraints (if any). */
4124 parm = build_tree_list (defval, parm);
4125 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4127 return chainon (list, parm);
4130 /* The end of a template parameter list has been reached. Process the
4131 tree list into a parameter vector, converting each parameter into a more
4132 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4133 as PARM_DECLs. */
4135 tree
4136 end_template_parm_list (tree parms)
4138 int nparms;
4139 tree parm, next;
4140 tree saved_parmlist = make_tree_vec (list_length (parms));
4142 /* Pop the dummy parameter level and add the real one. */
4143 current_template_parms = TREE_CHAIN (current_template_parms);
4145 current_template_parms
4146 = tree_cons (size_int (processing_template_decl),
4147 saved_parmlist, current_template_parms);
4149 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4151 next = TREE_CHAIN (parm);
4152 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4153 TREE_CHAIN (parm) = NULL_TREE;
4156 --processing_template_parmlist;
4158 return saved_parmlist;
4161 // Explicitly indicate the end of the template parameter list. We assume
4162 // that the current template parameters have been constructed and/or
4163 // managed explicitly, as when creating new template template parameters
4164 // from a shorthand constraint.
4165 void
4166 end_template_parm_list ()
4168 --processing_template_parmlist;
4171 /* end_template_decl is called after a template declaration is seen. */
4173 void
4174 end_template_decl (void)
4176 reset_specialization ();
4178 if (! processing_template_decl)
4179 return;
4181 /* This matches the pushlevel in begin_template_parm_list. */
4182 finish_scope ();
4184 --processing_template_decl;
4185 current_template_parms = TREE_CHAIN (current_template_parms);
4188 /* Takes a TREE_LIST representing a template parameter and convert it
4189 into an argument suitable to be passed to the type substitution
4190 functions. Note that If the TREE_LIST contains an error_mark
4191 node, the returned argument is error_mark_node. */
4193 tree
4194 template_parm_to_arg (tree t)
4197 if (t == NULL_TREE
4198 || TREE_CODE (t) != TREE_LIST)
4199 return t;
4201 if (error_operand_p (TREE_VALUE (t)))
4202 return error_mark_node;
4204 t = TREE_VALUE (t);
4206 if (TREE_CODE (t) == TYPE_DECL
4207 || TREE_CODE (t) == TEMPLATE_DECL)
4209 t = TREE_TYPE (t);
4211 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4213 /* Turn this argument into a TYPE_ARGUMENT_PACK
4214 with a single element, which expands T. */
4215 tree vec = make_tree_vec (1);
4216 if (CHECKING_P)
4217 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4219 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4221 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4222 SET_ARGUMENT_PACK_ARGS (t, vec);
4225 else
4227 t = DECL_INITIAL (t);
4229 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4231 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4232 with a single element, which expands T. */
4233 tree vec = make_tree_vec (1);
4234 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4235 if (CHECKING_P)
4236 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4238 t = convert_from_reference (t);
4239 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4241 t = make_node (NONTYPE_ARGUMENT_PACK);
4242 SET_ARGUMENT_PACK_ARGS (t, vec);
4243 TREE_TYPE (t) = type;
4245 else
4246 t = convert_from_reference (t);
4248 return t;
4251 /* Given a set of template parameters, return them as a set of template
4252 arguments. The template parameters are represented as a TREE_VEC, in
4253 the form documented in cp-tree.h for template arguments. */
4255 static tree
4256 template_parms_to_args (tree parms)
4258 tree header;
4259 tree args = NULL_TREE;
4260 int length = TMPL_PARMS_DEPTH (parms);
4261 int l = length;
4263 /* If there is only one level of template parameters, we do not
4264 create a TREE_VEC of TREE_VECs. Instead, we return a single
4265 TREE_VEC containing the arguments. */
4266 if (length > 1)
4267 args = make_tree_vec (length);
4269 for (header = parms; header; header = TREE_CHAIN (header))
4271 tree a = copy_node (TREE_VALUE (header));
4272 int i;
4274 TREE_TYPE (a) = NULL_TREE;
4275 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4276 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4278 if (CHECKING_P)
4279 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4281 if (length > 1)
4282 TREE_VEC_ELT (args, --l) = a;
4283 else
4284 args = a;
4287 return args;
4290 /* Within the declaration of a template, return the currently active
4291 template parameters as an argument TREE_VEC. */
4293 static tree
4294 current_template_args (void)
4296 return template_parms_to_args (current_template_parms);
4299 /* Update the declared TYPE by doing any lookups which were thought to be
4300 dependent, but are not now that we know the SCOPE of the declarator. */
4302 tree
4303 maybe_update_decl_type (tree orig_type, tree scope)
4305 tree type = orig_type;
4307 if (type == NULL_TREE)
4308 return type;
4310 if (TREE_CODE (orig_type) == TYPE_DECL)
4311 type = TREE_TYPE (type);
4313 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4314 && dependent_type_p (type)
4315 /* Don't bother building up the args in this case. */
4316 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4318 /* tsubst in the args corresponding to the template parameters,
4319 including auto if present. Most things will be unchanged, but
4320 make_typename_type and tsubst_qualified_id will resolve
4321 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4322 tree args = current_template_args ();
4323 tree auto_node = type_uses_auto (type);
4324 tree pushed;
4325 if (auto_node)
4327 tree auto_vec = make_tree_vec (1);
4328 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4329 args = add_to_template_args (args, auto_vec);
4331 pushed = push_scope (scope);
4332 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4333 if (pushed)
4334 pop_scope (scope);
4337 if (type == error_mark_node)
4338 return orig_type;
4340 if (TREE_CODE (orig_type) == TYPE_DECL)
4342 if (same_type_p (type, TREE_TYPE (orig_type)))
4343 type = orig_type;
4344 else
4345 type = TYPE_NAME (type);
4347 return type;
4350 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4351 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4352 the new template is a member template. */
4354 tree
4355 build_template_decl (tree decl, tree parms, bool member_template_p)
4357 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4358 DECL_TEMPLATE_PARMS (tmpl) = parms;
4359 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4360 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4361 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4363 return tmpl;
4366 struct template_parm_data
4368 /* The level of the template parameters we are currently
4369 processing. */
4370 int level;
4372 /* The index of the specialization argument we are currently
4373 processing. */
4374 int current_arg;
4376 /* An array whose size is the number of template parameters. The
4377 elements are nonzero if the parameter has been used in any one
4378 of the arguments processed so far. */
4379 int* parms;
4381 /* An array whose size is the number of template arguments. The
4382 elements are nonzero if the argument makes use of template
4383 parameters of this level. */
4384 int* arg_uses_template_parms;
4387 /* Subroutine of push_template_decl used to see if each template
4388 parameter in a partial specialization is used in the explicit
4389 argument list. If T is of the LEVEL given in DATA (which is
4390 treated as a template_parm_data*), then DATA->PARMS is marked
4391 appropriately. */
4393 static int
4394 mark_template_parm (tree t, void* data)
4396 int level;
4397 int idx;
4398 struct template_parm_data* tpd = (struct template_parm_data*) data;
4400 template_parm_level_and_index (t, &level, &idx);
4402 if (level == tpd->level)
4404 tpd->parms[idx] = 1;
4405 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4408 /* Return zero so that for_each_template_parm will continue the
4409 traversal of the tree; we want to mark *every* template parm. */
4410 return 0;
4413 /* Process the partial specialization DECL. */
4415 static tree
4416 process_partial_specialization (tree decl)
4418 tree type = TREE_TYPE (decl);
4419 tree tinfo = get_template_info (decl);
4420 tree maintmpl = TI_TEMPLATE (tinfo);
4421 tree specargs = TI_ARGS (tinfo);
4422 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4423 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4424 tree inner_parms;
4425 tree inst;
4426 int nargs = TREE_VEC_LENGTH (inner_args);
4427 int ntparms;
4428 int i;
4429 bool did_error_intro = false;
4430 struct template_parm_data tpd;
4431 struct template_parm_data tpd2;
4433 gcc_assert (current_template_parms);
4435 /* A concept cannot be specialized. */
4436 if (flag_concepts && variable_concept_p (maintmpl))
4438 error ("specialization of variable concept %q#D", maintmpl);
4439 return error_mark_node;
4442 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4443 ntparms = TREE_VEC_LENGTH (inner_parms);
4445 /* We check that each of the template parameters given in the
4446 partial specialization is used in the argument list to the
4447 specialization. For example:
4449 template <class T> struct S;
4450 template <class T> struct S<T*>;
4452 The second declaration is OK because `T*' uses the template
4453 parameter T, whereas
4455 template <class T> struct S<int>;
4457 is no good. Even trickier is:
4459 template <class T>
4460 struct S1
4462 template <class U>
4463 struct S2;
4464 template <class U>
4465 struct S2<T>;
4468 The S2<T> declaration is actually invalid; it is a
4469 full-specialization. Of course,
4471 template <class U>
4472 struct S2<T (*)(U)>;
4474 or some such would have been OK. */
4475 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4476 tpd.parms = XALLOCAVEC (int, ntparms);
4477 memset (tpd.parms, 0, sizeof (int) * ntparms);
4479 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4480 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4481 for (i = 0; i < nargs; ++i)
4483 tpd.current_arg = i;
4484 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4485 &mark_template_parm,
4486 &tpd,
4487 NULL,
4488 /*include_nondeduced_p=*/false);
4490 for (i = 0; i < ntparms; ++i)
4491 if (tpd.parms[i] == 0)
4493 /* One of the template parms was not used in a deduced context in the
4494 specialization. */
4495 if (!did_error_intro)
4497 error ("template parameters not deducible in "
4498 "partial specialization:");
4499 did_error_intro = true;
4502 inform (input_location, " %qD",
4503 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4506 if (did_error_intro)
4507 return error_mark_node;
4509 /* [temp.class.spec]
4511 The argument list of the specialization shall not be identical to
4512 the implicit argument list of the primary template. */
4513 tree main_args
4514 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4515 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4516 && (!flag_concepts
4517 || !strictly_subsumes (current_template_constraints (),
4518 get_constraints (maintmpl))))
4520 if (!flag_concepts)
4521 error ("partial specialization %q+D does not specialize "
4522 "any template arguments", decl);
4523 else
4524 error ("partial specialization %q+D does not specialize any "
4525 "template arguments and is not more constrained than", decl);
4526 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4529 /* A partial specialization that replaces multiple parameters of the
4530 primary template with a pack expansion is less specialized for those
4531 parameters. */
4532 if (nargs < DECL_NTPARMS (maintmpl))
4534 error ("partial specialization is not more specialized than the "
4535 "primary template because it replaces multiple parameters "
4536 "with a pack expansion");
4537 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4538 return decl;
4541 /* [temp.class.spec]
4543 A partially specialized non-type argument expression shall not
4544 involve template parameters of the partial specialization except
4545 when the argument expression is a simple identifier.
4547 The type of a template parameter corresponding to a specialized
4548 non-type argument shall not be dependent on a parameter of the
4549 specialization.
4551 Also, we verify that pack expansions only occur at the
4552 end of the argument list. */
4553 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4554 tpd2.parms = 0;
4555 for (i = 0; i < nargs; ++i)
4557 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4558 tree arg = TREE_VEC_ELT (inner_args, i);
4559 tree packed_args = NULL_TREE;
4560 int j, len = 1;
4562 if (ARGUMENT_PACK_P (arg))
4564 /* Extract the arguments from the argument pack. We'll be
4565 iterating over these in the following loop. */
4566 packed_args = ARGUMENT_PACK_ARGS (arg);
4567 len = TREE_VEC_LENGTH (packed_args);
4570 for (j = 0; j < len; j++)
4572 if (packed_args)
4573 /* Get the Jth argument in the parameter pack. */
4574 arg = TREE_VEC_ELT (packed_args, j);
4576 if (PACK_EXPANSION_P (arg))
4578 /* Pack expansions must come at the end of the
4579 argument list. */
4580 if ((packed_args && j < len - 1)
4581 || (!packed_args && i < nargs - 1))
4583 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4584 error ("parameter pack argument %qE must be at the "
4585 "end of the template argument list", arg);
4586 else
4587 error ("parameter pack argument %qT must be at the "
4588 "end of the template argument list", arg);
4592 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4593 /* We only care about the pattern. */
4594 arg = PACK_EXPANSION_PATTERN (arg);
4596 if (/* These first two lines are the `non-type' bit. */
4597 !TYPE_P (arg)
4598 && TREE_CODE (arg) != TEMPLATE_DECL
4599 /* This next two lines are the `argument expression is not just a
4600 simple identifier' condition and also the `specialized
4601 non-type argument' bit. */
4602 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4603 && !(REFERENCE_REF_P (arg)
4604 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4606 if ((!packed_args && tpd.arg_uses_template_parms[i])
4607 || (packed_args && uses_template_parms (arg)))
4608 error ("template argument %qE involves template parameter(s)",
4609 arg);
4610 else
4612 /* Look at the corresponding template parameter,
4613 marking which template parameters its type depends
4614 upon. */
4615 tree type = TREE_TYPE (parm);
4617 if (!tpd2.parms)
4619 /* We haven't yet initialized TPD2. Do so now. */
4620 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4621 /* The number of parameters here is the number in the
4622 main template, which, as checked in the assertion
4623 above, is NARGS. */
4624 tpd2.parms = XALLOCAVEC (int, nargs);
4625 tpd2.level =
4626 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4629 /* Mark the template parameters. But this time, we're
4630 looking for the template parameters of the main
4631 template, not in the specialization. */
4632 tpd2.current_arg = i;
4633 tpd2.arg_uses_template_parms[i] = 0;
4634 memset (tpd2.parms, 0, sizeof (int) * nargs);
4635 for_each_template_parm (type,
4636 &mark_template_parm,
4637 &tpd2,
4638 NULL,
4639 /*include_nondeduced_p=*/false);
4641 if (tpd2.arg_uses_template_parms [i])
4643 /* The type depended on some template parameters.
4644 If they are fully specialized in the
4645 specialization, that's OK. */
4646 int j;
4647 int count = 0;
4648 for (j = 0; j < nargs; ++j)
4649 if (tpd2.parms[j] != 0
4650 && tpd.arg_uses_template_parms [j])
4651 ++count;
4652 if (count != 0)
4653 error_n (input_location, count,
4654 "type %qT of template argument %qE depends "
4655 "on a template parameter",
4656 "type %qT of template argument %qE depends "
4657 "on template parameters",
4658 type,
4659 arg);
4666 /* We should only get here once. */
4667 if (TREE_CODE (decl) == TYPE_DECL)
4668 gcc_assert (!COMPLETE_TYPE_P (type));
4670 // Build the template decl.
4671 tree tmpl = build_template_decl (decl, current_template_parms,
4672 DECL_MEMBER_TEMPLATE_P (maintmpl));
4673 TREE_TYPE (tmpl) = type;
4674 DECL_TEMPLATE_RESULT (tmpl) = decl;
4675 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4676 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4677 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4679 if (VAR_P (decl))
4680 /* We didn't register this in check_explicit_specialization so we could
4681 wait until the constraints were set. */
4682 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4683 else
4684 associate_classtype_constraints (type);
4686 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4687 = tree_cons (specargs, tmpl,
4688 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4689 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4691 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4692 inst = TREE_CHAIN (inst))
4694 tree instance = TREE_VALUE (inst);
4695 if (TYPE_P (instance)
4696 ? (COMPLETE_TYPE_P (instance)
4697 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4698 : DECL_TEMPLATE_INSTANTIATION (instance))
4700 tree spec = most_specialized_partial_spec (instance, tf_none);
4701 tree inst_decl = (DECL_P (instance)
4702 ? instance : TYPE_NAME (instance));
4703 if (!spec)
4704 /* OK */;
4705 else if (spec == error_mark_node)
4706 permerror (input_location,
4707 "declaration of %qD ambiguates earlier template "
4708 "instantiation for %qD", decl, inst_decl);
4709 else if (TREE_VALUE (spec) == tmpl)
4710 permerror (input_location,
4711 "partial specialization of %qD after instantiation "
4712 "of %qD", decl, inst_decl);
4716 return decl;
4719 /* PARM is a template parameter of some form; return the corresponding
4720 TEMPLATE_PARM_INDEX. */
4722 static tree
4723 get_template_parm_index (tree parm)
4725 if (TREE_CODE (parm) == PARM_DECL
4726 || TREE_CODE (parm) == CONST_DECL)
4727 parm = DECL_INITIAL (parm);
4728 else if (TREE_CODE (parm) == TYPE_DECL
4729 || TREE_CODE (parm) == TEMPLATE_DECL)
4730 parm = TREE_TYPE (parm);
4731 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4732 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4733 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4734 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4735 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4736 return parm;
4739 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4740 parameter packs used by the template parameter PARM. */
4742 static void
4743 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4745 /* A type parm can't refer to another parm. */
4746 if (TREE_CODE (parm) == TYPE_DECL)
4747 return;
4748 else if (TREE_CODE (parm) == PARM_DECL)
4750 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4751 ppd, ppd->visited);
4752 return;
4755 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4757 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4758 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4759 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4762 /* PARM is a template parameter pack. Return any parameter packs used in
4763 its type or the type of any of its template parameters. If there are
4764 any such packs, it will be instantiated into a fixed template parameter
4765 list by partial instantiation rather than be fully deduced. */
4767 tree
4768 fixed_parameter_pack_p (tree parm)
4770 /* This can only be true in a member template. */
4771 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4772 return NULL_TREE;
4773 /* This can only be true for a parameter pack. */
4774 if (!template_parameter_pack_p (parm))
4775 return NULL_TREE;
4776 /* A type parm can't refer to another parm. */
4777 if (TREE_CODE (parm) == TYPE_DECL)
4778 return NULL_TREE;
4780 tree parameter_packs = NULL_TREE;
4781 struct find_parameter_pack_data ppd;
4782 ppd.parameter_packs = &parameter_packs;
4783 ppd.visited = new hash_set<tree>;
4784 ppd.type_pack_expansion_p = false;
4786 fixed_parameter_pack_p_1 (parm, &ppd);
4788 delete ppd.visited;
4789 return parameter_packs;
4792 /* Check that a template declaration's use of default arguments and
4793 parameter packs is not invalid. Here, PARMS are the template
4794 parameters. IS_PRIMARY is true if DECL is the thing declared by
4795 a primary template. IS_PARTIAL is true if DECL is a partial
4796 specialization.
4798 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4799 declaration (but not a definition); 1 indicates a declaration, 2
4800 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4801 emitted for extraneous default arguments.
4803 Returns TRUE if there were no errors found, FALSE otherwise. */
4805 bool
4806 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4807 bool is_partial, int is_friend_decl)
4809 const char *msg;
4810 int last_level_to_check;
4811 tree parm_level;
4812 bool no_errors = true;
4814 /* [temp.param]
4816 A default template-argument shall not be specified in a
4817 function template declaration or a function template definition, nor
4818 in the template-parameter-list of the definition of a member of a
4819 class template. */
4821 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4822 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4823 /* You can't have a function template declaration in a local
4824 scope, nor you can you define a member of a class template in a
4825 local scope. */
4826 return true;
4828 if ((TREE_CODE (decl) == TYPE_DECL
4829 && TREE_TYPE (decl)
4830 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4831 || (TREE_CODE (decl) == FUNCTION_DECL
4832 && LAMBDA_FUNCTION_P (decl)))
4833 /* A lambda doesn't have an explicit declaration; don't complain
4834 about the parms of the enclosing class. */
4835 return true;
4837 if (current_class_type
4838 && !TYPE_BEING_DEFINED (current_class_type)
4839 && DECL_LANG_SPECIFIC (decl)
4840 && DECL_DECLARES_FUNCTION_P (decl)
4841 /* If this is either a friend defined in the scope of the class
4842 or a member function. */
4843 && (DECL_FUNCTION_MEMBER_P (decl)
4844 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4845 : DECL_FRIEND_CONTEXT (decl)
4846 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4847 : false)
4848 /* And, if it was a member function, it really was defined in
4849 the scope of the class. */
4850 && (!DECL_FUNCTION_MEMBER_P (decl)
4851 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4852 /* We already checked these parameters when the template was
4853 declared, so there's no need to do it again now. This function
4854 was defined in class scope, but we're processing its body now
4855 that the class is complete. */
4856 return true;
4858 /* Core issue 226 (C++0x only): the following only applies to class
4859 templates. */
4860 if (is_primary
4861 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4863 /* [temp.param]
4865 If a template-parameter has a default template-argument, all
4866 subsequent template-parameters shall have a default
4867 template-argument supplied. */
4868 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4870 tree inner_parms = TREE_VALUE (parm_level);
4871 int ntparms = TREE_VEC_LENGTH (inner_parms);
4872 int seen_def_arg_p = 0;
4873 int i;
4875 for (i = 0; i < ntparms; ++i)
4877 tree parm = TREE_VEC_ELT (inner_parms, i);
4879 if (parm == error_mark_node)
4880 continue;
4882 if (TREE_PURPOSE (parm))
4883 seen_def_arg_p = 1;
4884 else if (seen_def_arg_p
4885 && !template_parameter_pack_p (TREE_VALUE (parm)))
4887 error ("no default argument for %qD", TREE_VALUE (parm));
4888 /* For better subsequent error-recovery, we indicate that
4889 there should have been a default argument. */
4890 TREE_PURPOSE (parm) = error_mark_node;
4891 no_errors = false;
4893 else if (!is_partial
4894 && !is_friend_decl
4895 /* Don't complain about an enclosing partial
4896 specialization. */
4897 && parm_level == parms
4898 && TREE_CODE (decl) == TYPE_DECL
4899 && i < ntparms - 1
4900 && template_parameter_pack_p (TREE_VALUE (parm))
4901 /* A fixed parameter pack will be partially
4902 instantiated into a fixed length list. */
4903 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4905 /* A primary class template can only have one
4906 parameter pack, at the end of the template
4907 parameter list. */
4909 error ("parameter pack %q+D must be at the end of the"
4910 " template parameter list", TREE_VALUE (parm));
4912 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4913 = error_mark_node;
4914 no_errors = false;
4920 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4921 || is_partial
4922 || !is_primary
4923 || is_friend_decl)
4924 /* For an ordinary class template, default template arguments are
4925 allowed at the innermost level, e.g.:
4926 template <class T = int>
4927 struct S {};
4928 but, in a partial specialization, they're not allowed even
4929 there, as we have in [temp.class.spec]:
4931 The template parameter list of a specialization shall not
4932 contain default template argument values.
4934 So, for a partial specialization, or for a function template
4935 (in C++98/C++03), we look at all of them. */
4937 else
4938 /* But, for a primary class template that is not a partial
4939 specialization we look at all template parameters except the
4940 innermost ones. */
4941 parms = TREE_CHAIN (parms);
4943 /* Figure out what error message to issue. */
4944 if (is_friend_decl == 2)
4945 msg = G_("default template arguments may not be used in function template "
4946 "friend re-declaration");
4947 else if (is_friend_decl)
4948 msg = G_("default template arguments may not be used in function template "
4949 "friend declarations");
4950 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4951 msg = G_("default template arguments may not be used in function templates "
4952 "without -std=c++11 or -std=gnu++11");
4953 else if (is_partial)
4954 msg = G_("default template arguments may not be used in "
4955 "partial specializations");
4956 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
4957 msg = G_("default argument for template parameter for class enclosing %qD");
4958 else
4959 /* Per [temp.param]/9, "A default template-argument shall not be
4960 specified in the template-parameter-lists of the definition of
4961 a member of a class template that appears outside of the member's
4962 class.", thus if we aren't handling a member of a class template
4963 there is no need to examine the parameters. */
4964 return true;
4966 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4967 /* If we're inside a class definition, there's no need to
4968 examine the parameters to the class itself. On the one
4969 hand, they will be checked when the class is defined, and,
4970 on the other, default arguments are valid in things like:
4971 template <class T = double>
4972 struct S { template <class U> void f(U); };
4973 Here the default argument for `S' has no bearing on the
4974 declaration of `f'. */
4975 last_level_to_check = template_class_depth (current_class_type) + 1;
4976 else
4977 /* Check everything. */
4978 last_level_to_check = 0;
4980 for (parm_level = parms;
4981 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4982 parm_level = TREE_CHAIN (parm_level))
4984 tree inner_parms = TREE_VALUE (parm_level);
4985 int i;
4986 int ntparms;
4988 ntparms = TREE_VEC_LENGTH (inner_parms);
4989 for (i = 0; i < ntparms; ++i)
4991 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4992 continue;
4994 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4996 if (msg)
4998 no_errors = false;
4999 if (is_friend_decl == 2)
5000 return no_errors;
5002 error (msg, decl);
5003 msg = 0;
5006 /* Clear out the default argument so that we are not
5007 confused later. */
5008 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5012 /* At this point, if we're still interested in issuing messages,
5013 they must apply to classes surrounding the object declared. */
5014 if (msg)
5015 msg = G_("default argument for template parameter for class "
5016 "enclosing %qD");
5019 return no_errors;
5022 /* Worker for push_template_decl_real, called via
5023 for_each_template_parm. DATA is really an int, indicating the
5024 level of the parameters we are interested in. If T is a template
5025 parameter of that level, return nonzero. */
5027 static int
5028 template_parm_this_level_p (tree t, void* data)
5030 int this_level = *(int *)data;
5031 int level;
5033 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5034 level = TEMPLATE_PARM_LEVEL (t);
5035 else
5036 level = TEMPLATE_TYPE_LEVEL (t);
5037 return level == this_level;
5040 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5041 parameters given by current_template_args, or reuses a
5042 previously existing one, if appropriate. Returns the DECL, or an
5043 equivalent one, if it is replaced via a call to duplicate_decls.
5045 If IS_FRIEND is true, DECL is a friend declaration. */
5047 tree
5048 push_template_decl_real (tree decl, bool is_friend)
5050 tree tmpl;
5051 tree args;
5052 tree info;
5053 tree ctx;
5054 bool is_primary;
5055 bool is_partial;
5056 int new_template_p = 0;
5057 /* True if the template is a member template, in the sense of
5058 [temp.mem]. */
5059 bool member_template_p = false;
5061 if (decl == error_mark_node || !current_template_parms)
5062 return error_mark_node;
5064 /* See if this is a partial specialization. */
5065 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5066 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5067 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5068 || (VAR_P (decl)
5069 && DECL_LANG_SPECIFIC (decl)
5070 && DECL_TEMPLATE_SPECIALIZATION (decl)
5071 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5073 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5074 is_friend = true;
5076 if (is_friend)
5077 /* For a friend, we want the context of the friend function, not
5078 the type of which it is a friend. */
5079 ctx = CP_DECL_CONTEXT (decl);
5080 else if (CP_DECL_CONTEXT (decl)
5081 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5082 /* In the case of a virtual function, we want the class in which
5083 it is defined. */
5084 ctx = CP_DECL_CONTEXT (decl);
5085 else
5086 /* Otherwise, if we're currently defining some class, the DECL
5087 is assumed to be a member of the class. */
5088 ctx = current_scope ();
5090 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5091 ctx = NULL_TREE;
5093 if (!DECL_CONTEXT (decl))
5094 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5096 /* See if this is a primary template. */
5097 if (is_friend && ctx
5098 && uses_template_parms_level (ctx, processing_template_decl))
5099 /* A friend template that specifies a class context, i.e.
5100 template <typename T> friend void A<T>::f();
5101 is not primary. */
5102 is_primary = false;
5103 else if (TREE_CODE (decl) == TYPE_DECL
5104 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5105 is_primary = false;
5106 else
5107 is_primary = template_parm_scope_p ();
5109 if (is_primary)
5111 warning (OPT_Wtemplates, "template %qD declared", decl);
5113 if (DECL_CLASS_SCOPE_P (decl))
5114 member_template_p = true;
5115 if (TREE_CODE (decl) == TYPE_DECL
5116 && anon_aggrname_p (DECL_NAME (decl)))
5118 error ("template class without a name");
5119 return error_mark_node;
5121 else if (TREE_CODE (decl) == FUNCTION_DECL)
5123 if (member_template_p)
5125 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5126 error ("member template %qD may not have virt-specifiers", decl);
5128 if (DECL_DESTRUCTOR_P (decl))
5130 /* [temp.mem]
5132 A destructor shall not be a member template. */
5133 error ("destructor %qD declared as member template", decl);
5134 return error_mark_node;
5136 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5137 && (!prototype_p (TREE_TYPE (decl))
5138 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5139 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5140 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5141 == void_list_node)))
5143 /* [basic.stc.dynamic.allocation]
5145 An allocation function can be a function
5146 template. ... Template allocation functions shall
5147 have two or more parameters. */
5148 error ("invalid template declaration of %qD", decl);
5149 return error_mark_node;
5152 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5153 && CLASS_TYPE_P (TREE_TYPE (decl)))
5154 /* OK */;
5155 else if (TREE_CODE (decl) == TYPE_DECL
5156 && TYPE_DECL_ALIAS_P (decl))
5157 /* alias-declaration */
5158 gcc_assert (!DECL_ARTIFICIAL (decl));
5159 else if (VAR_P (decl))
5160 /* C++14 variable template. */;
5161 else
5163 error ("template declaration of %q#D", decl);
5164 return error_mark_node;
5168 /* Check to see that the rules regarding the use of default
5169 arguments are not being violated. */
5170 check_default_tmpl_args (decl, current_template_parms,
5171 is_primary, is_partial, /*is_friend_decl=*/0);
5173 /* Ensure that there are no parameter packs in the type of this
5174 declaration that have not been expanded. */
5175 if (TREE_CODE (decl) == FUNCTION_DECL)
5177 /* Check each of the arguments individually to see if there are
5178 any bare parameter packs. */
5179 tree type = TREE_TYPE (decl);
5180 tree arg = DECL_ARGUMENTS (decl);
5181 tree argtype = TYPE_ARG_TYPES (type);
5183 while (arg && argtype)
5185 if (!DECL_PACK_P (arg)
5186 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5188 /* This is a PARM_DECL that contains unexpanded parameter
5189 packs. We have already complained about this in the
5190 check_for_bare_parameter_packs call, so just replace
5191 these types with ERROR_MARK_NODE. */
5192 TREE_TYPE (arg) = error_mark_node;
5193 TREE_VALUE (argtype) = error_mark_node;
5196 arg = DECL_CHAIN (arg);
5197 argtype = TREE_CHAIN (argtype);
5200 /* Check for bare parameter packs in the return type and the
5201 exception specifiers. */
5202 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5203 /* Errors were already issued, set return type to int
5204 as the frontend doesn't expect error_mark_node as
5205 the return type. */
5206 TREE_TYPE (type) = integer_type_node;
5207 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5208 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5210 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5211 && TYPE_DECL_ALIAS_P (decl))
5212 ? DECL_ORIGINAL_TYPE (decl)
5213 : TREE_TYPE (decl)))
5215 TREE_TYPE (decl) = error_mark_node;
5216 return error_mark_node;
5219 if (is_partial)
5220 return process_partial_specialization (decl);
5222 args = current_template_args ();
5224 if (!ctx
5225 || TREE_CODE (ctx) == FUNCTION_DECL
5226 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5227 || (TREE_CODE (decl) == TYPE_DECL
5228 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5229 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5231 if (DECL_LANG_SPECIFIC (decl)
5232 && DECL_TEMPLATE_INFO (decl)
5233 && DECL_TI_TEMPLATE (decl))
5234 tmpl = DECL_TI_TEMPLATE (decl);
5235 /* If DECL is a TYPE_DECL for a class-template, then there won't
5236 be DECL_LANG_SPECIFIC. The information equivalent to
5237 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5238 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5239 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5240 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5242 /* Since a template declaration already existed for this
5243 class-type, we must be redeclaring it here. Make sure
5244 that the redeclaration is valid. */
5245 redeclare_class_template (TREE_TYPE (decl),
5246 current_template_parms,
5247 current_template_constraints ());
5248 /* We don't need to create a new TEMPLATE_DECL; just use the
5249 one we already had. */
5250 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5252 else
5254 tmpl = build_template_decl (decl, current_template_parms,
5255 member_template_p);
5256 new_template_p = 1;
5258 if (DECL_LANG_SPECIFIC (decl)
5259 && DECL_TEMPLATE_SPECIALIZATION (decl))
5261 /* A specialization of a member template of a template
5262 class. */
5263 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5264 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5265 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5269 else
5271 tree a, t, current, parms;
5272 int i;
5273 tree tinfo = get_template_info (decl);
5275 if (!tinfo)
5277 error ("template definition of non-template %q#D", decl);
5278 return error_mark_node;
5281 tmpl = TI_TEMPLATE (tinfo);
5283 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5284 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5285 && DECL_TEMPLATE_SPECIALIZATION (decl)
5286 && DECL_MEMBER_TEMPLATE_P (tmpl))
5288 tree new_tmpl;
5290 /* The declaration is a specialization of a member
5291 template, declared outside the class. Therefore, the
5292 innermost template arguments will be NULL, so we
5293 replace them with the arguments determined by the
5294 earlier call to check_explicit_specialization. */
5295 args = DECL_TI_ARGS (decl);
5297 new_tmpl
5298 = build_template_decl (decl, current_template_parms,
5299 member_template_p);
5300 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5301 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5302 DECL_TI_TEMPLATE (decl) = new_tmpl;
5303 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5304 DECL_TEMPLATE_INFO (new_tmpl)
5305 = build_template_info (tmpl, args);
5307 register_specialization (new_tmpl,
5308 most_general_template (tmpl),
5309 args,
5310 is_friend, 0);
5311 return decl;
5314 /* Make sure the template headers we got make sense. */
5316 parms = DECL_TEMPLATE_PARMS (tmpl);
5317 i = TMPL_PARMS_DEPTH (parms);
5318 if (TMPL_ARGS_DEPTH (args) != i)
5320 error ("expected %d levels of template parms for %q#D, got %d",
5321 i, decl, TMPL_ARGS_DEPTH (args));
5322 DECL_INTERFACE_KNOWN (decl) = 1;
5323 return error_mark_node;
5325 else
5326 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5328 a = TMPL_ARGS_LEVEL (args, i);
5329 t = INNERMOST_TEMPLATE_PARMS (parms);
5331 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5333 if (current == decl)
5334 error ("got %d template parameters for %q#D",
5335 TREE_VEC_LENGTH (a), decl);
5336 else
5337 error ("got %d template parameters for %q#T",
5338 TREE_VEC_LENGTH (a), current);
5339 error (" but %d required", TREE_VEC_LENGTH (t));
5340 /* Avoid crash in import_export_decl. */
5341 DECL_INTERFACE_KNOWN (decl) = 1;
5342 return error_mark_node;
5345 if (current == decl)
5346 current = ctx;
5347 else if (current == NULL_TREE)
5348 /* Can happen in erroneous input. */
5349 break;
5350 else
5351 current = get_containing_scope (current);
5354 /* Check that the parms are used in the appropriate qualifying scopes
5355 in the declarator. */
5356 if (!comp_template_args
5357 (TI_ARGS (tinfo),
5358 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5360 error ("\
5361 template arguments to %qD do not match original template %qD",
5362 decl, DECL_TEMPLATE_RESULT (tmpl));
5363 if (!uses_template_parms (TI_ARGS (tinfo)))
5364 inform (input_location, "use template<> for an explicit specialization");
5365 /* Avoid crash in import_export_decl. */
5366 DECL_INTERFACE_KNOWN (decl) = 1;
5367 return error_mark_node;
5371 DECL_TEMPLATE_RESULT (tmpl) = decl;
5372 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5374 /* Push template declarations for global functions and types. Note
5375 that we do not try to push a global template friend declared in a
5376 template class; such a thing may well depend on the template
5377 parameters of the class. */
5378 if (new_template_p && !ctx
5379 && !(is_friend && template_class_depth (current_class_type) > 0))
5381 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5382 if (tmpl == error_mark_node)
5383 return error_mark_node;
5385 /* Hide template friend classes that haven't been declared yet. */
5386 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5388 DECL_ANTICIPATED (tmpl) = 1;
5389 DECL_FRIEND_P (tmpl) = 1;
5393 if (is_primary)
5395 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5396 int i;
5398 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5399 if (DECL_CONV_FN_P (tmpl))
5401 int depth = TMPL_PARMS_DEPTH (parms);
5403 /* It is a conversion operator. See if the type converted to
5404 depends on innermost template operands. */
5406 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5407 depth))
5408 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5411 /* Give template template parms a DECL_CONTEXT of the template
5412 for which they are a parameter. */
5413 parms = INNERMOST_TEMPLATE_PARMS (parms);
5414 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5416 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5417 if (TREE_CODE (parm) == TEMPLATE_DECL)
5418 DECL_CONTEXT (parm) = tmpl;
5421 if (TREE_CODE (decl) == TYPE_DECL
5422 && TYPE_DECL_ALIAS_P (decl)
5423 && complex_alias_template_p (tmpl))
5424 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5427 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5428 back to its most general template. If TMPL is a specialization,
5429 ARGS may only have the innermost set of arguments. Add the missing
5430 argument levels if necessary. */
5431 if (DECL_TEMPLATE_INFO (tmpl))
5432 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5434 info = build_template_info (tmpl, args);
5436 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5437 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5438 else
5440 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5441 retrofit_lang_decl (decl);
5442 if (DECL_LANG_SPECIFIC (decl))
5443 DECL_TEMPLATE_INFO (decl) = info;
5446 if (flag_implicit_templates
5447 && !is_friend
5448 && TREE_PUBLIC (decl)
5449 && VAR_OR_FUNCTION_DECL_P (decl))
5450 /* Set DECL_COMDAT on template instantiations; if we force
5451 them to be emitted by explicit instantiation or -frepo,
5452 mark_needed will tell cgraph to do the right thing. */
5453 DECL_COMDAT (decl) = true;
5455 return DECL_TEMPLATE_RESULT (tmpl);
5458 tree
5459 push_template_decl (tree decl)
5461 return push_template_decl_real (decl, false);
5464 /* FN is an inheriting constructor that inherits from the constructor
5465 template INHERITED; turn FN into a constructor template with a matching
5466 template header. */
5468 tree
5469 add_inherited_template_parms (tree fn, tree inherited)
5471 tree inner_parms
5472 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5473 inner_parms = copy_node (inner_parms);
5474 tree parms
5475 = tree_cons (size_int (processing_template_decl + 1),
5476 inner_parms, current_template_parms);
5477 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5478 tree args = template_parms_to_args (parms);
5479 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5480 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5481 DECL_TEMPLATE_RESULT (tmpl) = fn;
5482 DECL_ARTIFICIAL (tmpl) = true;
5483 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5484 return tmpl;
5487 /* Called when a class template TYPE is redeclared with the indicated
5488 template PARMS, e.g.:
5490 template <class T> struct S;
5491 template <class T> struct S {}; */
5493 bool
5494 redeclare_class_template (tree type, tree parms, tree cons)
5496 tree tmpl;
5497 tree tmpl_parms;
5498 int i;
5500 if (!TYPE_TEMPLATE_INFO (type))
5502 error ("%qT is not a template type", type);
5503 return false;
5506 tmpl = TYPE_TI_TEMPLATE (type);
5507 if (!PRIMARY_TEMPLATE_P (tmpl))
5508 /* The type is nested in some template class. Nothing to worry
5509 about here; there are no new template parameters for the nested
5510 type. */
5511 return true;
5513 if (!parms)
5515 error ("template specifiers not specified in declaration of %qD",
5516 tmpl);
5517 return false;
5520 parms = INNERMOST_TEMPLATE_PARMS (parms);
5521 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5523 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5525 error_n (input_location, TREE_VEC_LENGTH (parms),
5526 "redeclared with %d template parameter",
5527 "redeclared with %d template parameters",
5528 TREE_VEC_LENGTH (parms));
5529 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5530 "previous declaration %qD used %d template parameter",
5531 "previous declaration %qD used %d template parameters",
5532 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5533 return false;
5536 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5538 tree tmpl_parm;
5539 tree parm;
5540 tree tmpl_default;
5541 tree parm_default;
5543 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5544 || TREE_VEC_ELT (parms, i) == error_mark_node)
5545 continue;
5547 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5548 if (error_operand_p (tmpl_parm))
5549 return false;
5551 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5552 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5553 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5555 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5556 TEMPLATE_DECL. */
5557 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5558 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5559 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5560 || (TREE_CODE (tmpl_parm) != PARM_DECL
5561 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5562 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5563 || (TREE_CODE (tmpl_parm) == PARM_DECL
5564 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5565 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5567 error ("template parameter %q+#D", tmpl_parm);
5568 error ("redeclared here as %q#D", parm);
5569 return false;
5572 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5574 /* We have in [temp.param]:
5576 A template-parameter may not be given default arguments
5577 by two different declarations in the same scope. */
5578 error_at (input_location, "redefinition of default argument for %q#D", parm);
5579 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5580 "original definition appeared here");
5581 return false;
5584 if (parm_default != NULL_TREE)
5585 /* Update the previous template parameters (which are the ones
5586 that will really count) with the new default value. */
5587 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5588 else if (tmpl_default != NULL_TREE)
5589 /* Update the new parameters, too; they'll be used as the
5590 parameters for any members. */
5591 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5593 /* Give each template template parm in this redeclaration a
5594 DECL_CONTEXT of the template for which they are a parameter. */
5595 if (TREE_CODE (parm) == TEMPLATE_DECL)
5597 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5598 DECL_CONTEXT (parm) = tmpl;
5602 // Cannot redeclare a class template with a different set of constraints.
5603 if (!equivalent_constraints (get_constraints (tmpl), cons))
5605 error_at (input_location, "redeclaration %q#D with different "
5606 "constraints", tmpl);
5607 inform (DECL_SOURCE_LOCATION (tmpl),
5608 "original declaration appeared here");
5611 return true;
5614 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5615 to be used when the caller has already checked
5616 (processing_template_decl
5617 && !instantiation_dependent_expression_p (expr)
5618 && potential_constant_expression (expr))
5619 and cleared processing_template_decl. */
5621 tree
5622 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5624 return tsubst_copy_and_build (expr,
5625 /*args=*/NULL_TREE,
5626 complain,
5627 /*in_decl=*/NULL_TREE,
5628 /*function_p=*/false,
5629 /*integral_constant_expression_p=*/true);
5632 /* Simplify EXPR if it is a non-dependent expression. Returns the
5633 (possibly simplified) expression. */
5635 tree
5636 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5638 if (expr == NULL_TREE)
5639 return NULL_TREE;
5641 /* If we're in a template, but EXPR isn't value dependent, simplify
5642 it. We're supposed to treat:
5644 template <typename T> void f(T[1 + 1]);
5645 template <typename T> void f(T[2]);
5647 as two declarations of the same function, for example. */
5648 if (processing_template_decl
5649 && !instantiation_dependent_expression_p (expr)
5650 && potential_constant_expression (expr))
5652 processing_template_decl_sentinel s;
5653 expr = instantiate_non_dependent_expr_internal (expr, complain);
5655 return expr;
5658 tree
5659 instantiate_non_dependent_expr (tree expr)
5661 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5664 /* True iff T is a specialization of a variable template. */
5666 bool
5667 variable_template_specialization_p (tree t)
5669 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5670 return false;
5671 tree tmpl = DECL_TI_TEMPLATE (t);
5672 return variable_template_p (tmpl);
5675 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5676 template declaration, or a TYPE_DECL for an alias declaration. */
5678 bool
5679 alias_type_or_template_p (tree t)
5681 if (t == NULL_TREE)
5682 return false;
5683 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5684 || (TYPE_P (t)
5685 && TYPE_NAME (t)
5686 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5687 || DECL_ALIAS_TEMPLATE_P (t));
5690 /* Return TRUE iff T is a specialization of an alias template. */
5692 bool
5693 alias_template_specialization_p (const_tree t)
5695 /* It's an alias template specialization if it's an alias and its
5696 TYPE_NAME is a specialization of a primary template. */
5697 if (TYPE_ALIAS_P (t))
5699 tree name = TYPE_NAME (t);
5700 if (DECL_LANG_SPECIFIC (name))
5701 if (tree ti = DECL_TEMPLATE_INFO (name))
5703 tree tmpl = TI_TEMPLATE (ti);
5704 return PRIMARY_TEMPLATE_P (tmpl);
5707 return false;
5710 /* An alias template is complex from a SFINAE perspective if a template-id
5711 using that alias can be ill-formed when the expansion is not, as with
5712 the void_t template. We determine this by checking whether the
5713 expansion for the alias template uses all its template parameters. */
5715 struct uses_all_template_parms_data
5717 int level;
5718 bool *seen;
5721 static int
5722 uses_all_template_parms_r (tree t, void *data_)
5724 struct uses_all_template_parms_data &data
5725 = *(struct uses_all_template_parms_data*)data_;
5726 tree idx = get_template_parm_index (t);
5728 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5729 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5730 return 0;
5733 static bool
5734 complex_alias_template_p (const_tree tmpl)
5736 struct uses_all_template_parms_data data;
5737 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5738 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5739 data.level = TMPL_PARMS_DEPTH (parms);
5740 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5741 data.seen = XALLOCAVEC (bool, len);
5742 for (int i = 0; i < len; ++i)
5743 data.seen[i] = false;
5745 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5746 for (int i = 0; i < len; ++i)
5747 if (!data.seen[i])
5748 return true;
5749 return false;
5752 /* Return TRUE iff T is a specialization of a complex alias template with
5753 dependent template-arguments. */
5755 bool
5756 dependent_alias_template_spec_p (const_tree t)
5758 return (alias_template_specialization_p (t)
5759 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5760 && (any_dependent_template_arguments_p
5761 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5764 /* Return the number of innermost template parameters in TMPL. */
5766 static int
5767 num_innermost_template_parms (tree tmpl)
5769 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5770 return TREE_VEC_LENGTH (parms);
5773 /* Return either TMPL or another template that it is equivalent to under DR
5774 1286: An alias that just changes the name of a template is equivalent to
5775 the other template. */
5777 static tree
5778 get_underlying_template (tree tmpl)
5780 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5781 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5783 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5784 if (TYPE_TEMPLATE_INFO (result))
5786 tree sub = TYPE_TI_TEMPLATE (result);
5787 if (PRIMARY_TEMPLATE_P (sub)
5788 && (num_innermost_template_parms (tmpl)
5789 == num_innermost_template_parms (sub)))
5791 tree alias_args = INNERMOST_TEMPLATE_ARGS
5792 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5793 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5794 break;
5795 /* The alias type is equivalent to the pattern of the
5796 underlying template, so strip the alias. */
5797 tmpl = sub;
5798 continue;
5801 break;
5803 return tmpl;
5806 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5807 must be a function or a pointer-to-function type, as specified
5808 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5809 and check that the resulting function has external linkage. */
5811 static tree
5812 convert_nontype_argument_function (tree type, tree expr,
5813 tsubst_flags_t complain)
5815 tree fns = expr;
5816 tree fn, fn_no_ptr;
5817 linkage_kind linkage;
5819 fn = instantiate_type (type, fns, tf_none);
5820 if (fn == error_mark_node)
5821 return error_mark_node;
5823 fn_no_ptr = fn;
5824 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5825 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5826 if (BASELINK_P (fn_no_ptr))
5827 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5829 /* [temp.arg.nontype]/1
5831 A template-argument for a non-type, non-template template-parameter
5832 shall be one of:
5833 [...]
5834 -- the address of an object or function with external [C++11: or
5835 internal] linkage. */
5837 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5839 if (complain & tf_error)
5841 error ("%qE is not a valid template argument for type %qT",
5842 expr, type);
5843 if (TYPE_PTR_P (type))
5844 error ("it must be the address of a function with "
5845 "external linkage");
5846 else
5847 error ("it must be the name of a function with "
5848 "external linkage");
5850 return NULL_TREE;
5853 linkage = decl_linkage (fn_no_ptr);
5854 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5856 if (complain & tf_error)
5858 if (cxx_dialect >= cxx11)
5859 error ("%qE is not a valid template argument for type %qT "
5860 "because %qD has no linkage",
5861 expr, type, fn_no_ptr);
5862 else
5863 error ("%qE is not a valid template argument for type %qT "
5864 "because %qD does not have external linkage",
5865 expr, type, fn_no_ptr);
5867 return NULL_TREE;
5870 return fn;
5873 /* Subroutine of convert_nontype_argument.
5874 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5875 Emit an error otherwise. */
5877 static bool
5878 check_valid_ptrmem_cst_expr (tree type, tree expr,
5879 tsubst_flags_t complain)
5881 STRIP_NOPS (expr);
5882 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5883 return true;
5884 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5885 return true;
5886 if (processing_template_decl
5887 && TREE_CODE (expr) == ADDR_EXPR
5888 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5889 return true;
5890 if (complain & tf_error)
5892 error ("%qE is not a valid template argument for type %qT",
5893 expr, type);
5894 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5896 return false;
5899 /* Returns TRUE iff the address of OP is value-dependent.
5901 14.6.2.4 [temp.dep.temp]:
5902 A non-integral non-type template-argument is dependent if its type is
5903 dependent or it has either of the following forms
5904 qualified-id
5905 & qualified-id
5906 and contains a nested-name-specifier which specifies a class-name that
5907 names a dependent type.
5909 We generalize this to just say that the address of a member of a
5910 dependent class is value-dependent; the above doesn't cover the
5911 address of a static data member named with an unqualified-id. */
5913 static bool
5914 has_value_dependent_address (tree op)
5916 /* We could use get_inner_reference here, but there's no need;
5917 this is only relevant for template non-type arguments, which
5918 can only be expressed as &id-expression. */
5919 if (DECL_P (op))
5921 tree ctx = CP_DECL_CONTEXT (op);
5922 if (TYPE_P (ctx) && dependent_type_p (ctx))
5923 return true;
5926 return false;
5929 /* The next set of functions are used for providing helpful explanatory
5930 diagnostics for failed overload resolution. Their messages should be
5931 indented by two spaces for consistency with the messages in
5932 call.c */
5934 static int
5935 unify_success (bool /*explain_p*/)
5937 return 0;
5940 static int
5941 unify_parameter_deduction_failure (bool explain_p, tree parm)
5943 if (explain_p)
5944 inform (input_location,
5945 " couldn't deduce template parameter %qD", parm);
5946 return 1;
5949 static int
5950 unify_invalid (bool /*explain_p*/)
5952 return 1;
5955 static int
5956 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5958 if (explain_p)
5959 inform (input_location,
5960 " types %qT and %qT have incompatible cv-qualifiers",
5961 parm, arg);
5962 return 1;
5965 static int
5966 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5968 if (explain_p)
5969 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5970 return 1;
5973 static int
5974 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5976 if (explain_p)
5977 inform (input_location,
5978 " template parameter %qD is not a parameter pack, but "
5979 "argument %qD is",
5980 parm, arg);
5981 return 1;
5984 static int
5985 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5987 if (explain_p)
5988 inform (input_location,
5989 " template argument %qE does not match "
5990 "pointer-to-member constant %qE",
5991 arg, parm);
5992 return 1;
5995 static int
5996 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5998 if (explain_p)
5999 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6000 return 1;
6003 static int
6004 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6006 if (explain_p)
6007 inform (input_location,
6008 " inconsistent parameter pack deduction with %qT and %qT",
6009 old_arg, new_arg);
6010 return 1;
6013 static int
6014 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6016 if (explain_p)
6018 if (TYPE_P (parm))
6019 inform (input_location,
6020 " deduced conflicting types for parameter %qT (%qT and %qT)",
6021 parm, first, second);
6022 else
6023 inform (input_location,
6024 " deduced conflicting values for non-type parameter "
6025 "%qE (%qE and %qE)", parm, first, second);
6027 return 1;
6030 static int
6031 unify_vla_arg (bool explain_p, tree arg)
6033 if (explain_p)
6034 inform (input_location,
6035 " variable-sized array type %qT is not "
6036 "a valid template argument",
6037 arg);
6038 return 1;
6041 static int
6042 unify_method_type_error (bool explain_p, tree arg)
6044 if (explain_p)
6045 inform (input_location,
6046 " member function type %qT is not a valid template argument",
6047 arg);
6048 return 1;
6051 static int
6052 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6054 if (explain_p)
6056 if (least_p)
6057 inform_n (input_location, wanted,
6058 " candidate expects at least %d argument, %d provided",
6059 " candidate expects at least %d arguments, %d provided",
6060 wanted, have);
6061 else
6062 inform_n (input_location, wanted,
6063 " candidate expects %d argument, %d provided",
6064 " candidate expects %d arguments, %d provided",
6065 wanted, have);
6067 return 1;
6070 static int
6071 unify_too_many_arguments (bool explain_p, int have, int wanted)
6073 return unify_arity (explain_p, have, wanted);
6076 static int
6077 unify_too_few_arguments (bool explain_p, int have, int wanted,
6078 bool least_p = false)
6080 return unify_arity (explain_p, have, wanted, least_p);
6083 static int
6084 unify_arg_conversion (bool explain_p, tree to_type,
6085 tree from_type, tree arg)
6087 if (explain_p)
6088 inform (EXPR_LOC_OR_LOC (arg, input_location),
6089 " cannot convert %qE (type %qT) to type %qT",
6090 arg, from_type, to_type);
6091 return 1;
6094 static int
6095 unify_no_common_base (bool explain_p, enum template_base_result r,
6096 tree parm, tree arg)
6098 if (explain_p)
6099 switch (r)
6101 case tbr_ambiguous_baseclass:
6102 inform (input_location, " %qT is an ambiguous base class of %qT",
6103 parm, arg);
6104 break;
6105 default:
6106 inform (input_location, " %qT is not derived from %qT", arg, parm);
6107 break;
6109 return 1;
6112 static int
6113 unify_inconsistent_template_template_parameters (bool explain_p)
6115 if (explain_p)
6116 inform (input_location,
6117 " template parameters of a template template argument are "
6118 "inconsistent with other deduced template arguments");
6119 return 1;
6122 static int
6123 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6125 if (explain_p)
6126 inform (input_location,
6127 " can't deduce a template for %qT from non-template type %qT",
6128 parm, arg);
6129 return 1;
6132 static int
6133 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6135 if (explain_p)
6136 inform (input_location,
6137 " template argument %qE does not match %qD", arg, parm);
6138 return 1;
6141 static int
6142 unify_overload_resolution_failure (bool explain_p, tree arg)
6144 if (explain_p)
6145 inform (input_location,
6146 " could not resolve address from overloaded function %qE",
6147 arg);
6148 return 1;
6151 /* Attempt to convert the non-type template parameter EXPR to the
6152 indicated TYPE. If the conversion is successful, return the
6153 converted value. If the conversion is unsuccessful, return
6154 NULL_TREE if we issued an error message, or error_mark_node if we
6155 did not. We issue error messages for out-and-out bad template
6156 parameters, but not simply because the conversion failed, since we
6157 might be just trying to do argument deduction. Both TYPE and EXPR
6158 must be non-dependent.
6160 The conversion follows the special rules described in
6161 [temp.arg.nontype], and it is much more strict than an implicit
6162 conversion.
6164 This function is called twice for each template argument (see
6165 lookup_template_class for a more accurate description of this
6166 problem). This means that we need to handle expressions which
6167 are not valid in a C++ source, but can be created from the
6168 first call (for instance, casts to perform conversions). These
6169 hacks can go away after we fix the double coercion problem. */
6171 static tree
6172 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6174 tree expr_type;
6176 /* Detect immediately string literals as invalid non-type argument.
6177 This special-case is not needed for correctness (we would easily
6178 catch this later), but only to provide better diagnostic for this
6179 common user mistake. As suggested by DR 100, we do not mention
6180 linkage issues in the diagnostic as this is not the point. */
6181 /* FIXME we're making this OK. */
6182 if (TREE_CODE (expr) == STRING_CST)
6184 if (complain & tf_error)
6185 error ("%qE is not a valid template argument for type %qT "
6186 "because string literals can never be used in this context",
6187 expr, type);
6188 return NULL_TREE;
6191 /* Add the ADDR_EXPR now for the benefit of
6192 value_dependent_expression_p. */
6193 if (TYPE_PTROBV_P (type)
6194 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6196 expr = decay_conversion (expr, complain);
6197 if (expr == error_mark_node)
6198 return error_mark_node;
6201 /* If we are in a template, EXPR may be non-dependent, but still
6202 have a syntactic, rather than semantic, form. For example, EXPR
6203 might be a SCOPE_REF, rather than the VAR_DECL to which the
6204 SCOPE_REF refers. Preserving the qualifying scope is necessary
6205 so that access checking can be performed when the template is
6206 instantiated -- but here we need the resolved form so that we can
6207 convert the argument. */
6208 bool non_dep = false;
6209 if (TYPE_REF_OBJ_P (type)
6210 && has_value_dependent_address (expr))
6211 /* If we want the address and it's value-dependent, don't fold. */;
6212 else if (!type_unknown_p (expr)
6213 && processing_template_decl
6214 && !instantiation_dependent_expression_p (expr)
6215 && potential_constant_expression (expr))
6216 non_dep = true;
6217 if (error_operand_p (expr))
6218 return error_mark_node;
6219 expr_type = TREE_TYPE (expr);
6220 if (TREE_CODE (type) == REFERENCE_TYPE)
6221 expr = mark_lvalue_use (expr);
6222 else
6223 expr = mark_rvalue_use (expr);
6225 /* If the argument is non-dependent, perform any conversions in
6226 non-dependent context as well. */
6227 processing_template_decl_sentinel s (non_dep);
6228 if (non_dep)
6229 expr = instantiate_non_dependent_expr_internal (expr, complain);
6231 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6232 to a non-type argument of "nullptr". */
6233 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6234 expr = fold_simple (convert (type, expr));
6236 /* In C++11, integral or enumeration non-type template arguments can be
6237 arbitrary constant expressions. Pointer and pointer to
6238 member arguments can be general constant expressions that evaluate
6239 to a null value, but otherwise still need to be of a specific form. */
6240 if (cxx_dialect >= cxx11)
6242 if (TREE_CODE (expr) == PTRMEM_CST)
6243 /* A PTRMEM_CST is already constant, and a valid template
6244 argument for a parameter of pointer to member type, we just want
6245 to leave it in that form rather than lower it to a
6246 CONSTRUCTOR. */;
6247 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6248 expr = maybe_constant_value (expr);
6249 else if (cxx_dialect >= cxx1z)
6251 if (TREE_CODE (type) != REFERENCE_TYPE)
6252 expr = maybe_constant_value (expr);
6253 else if (REFERENCE_REF_P (expr))
6255 expr = TREE_OPERAND (expr, 0);
6256 expr = maybe_constant_value (expr);
6257 expr = convert_from_reference (expr);
6260 else if (TYPE_PTR_OR_PTRMEM_P (type))
6262 tree folded = maybe_constant_value (expr);
6263 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6264 : null_member_pointer_value_p (folded))
6265 expr = folded;
6269 /* HACK: Due to double coercion, we can get a
6270 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6271 which is the tree that we built on the first call (see
6272 below when coercing to reference to object or to reference to
6273 function). We just strip everything and get to the arg.
6274 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6275 for examples. */
6276 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6278 tree probe_type, probe = expr;
6279 if (REFERENCE_REF_P (probe))
6280 probe = TREE_OPERAND (probe, 0);
6281 probe_type = TREE_TYPE (probe);
6282 if (TREE_CODE (probe) == NOP_EXPR)
6284 /* ??? Maybe we could use convert_from_reference here, but we
6285 would need to relax its constraints because the NOP_EXPR
6286 could actually change the type to something more cv-qualified,
6287 and this is not folded by convert_from_reference. */
6288 tree addr = TREE_OPERAND (probe, 0);
6289 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6290 && TREE_CODE (addr) == ADDR_EXPR
6291 && TYPE_PTR_P (TREE_TYPE (addr))
6292 && (same_type_ignoring_top_level_qualifiers_p
6293 (TREE_TYPE (probe_type),
6294 TREE_TYPE (TREE_TYPE (addr)))))
6296 expr = TREE_OPERAND (addr, 0);
6297 expr_type = TREE_TYPE (probe_type);
6302 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6303 parameter is a pointer to object, through decay and
6304 qualification conversion. Let's strip everything. */
6305 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6307 tree probe = expr;
6308 STRIP_NOPS (probe);
6309 if (TREE_CODE (probe) == ADDR_EXPR
6310 && TYPE_PTR_P (TREE_TYPE (probe)))
6312 /* Skip the ADDR_EXPR only if it is part of the decay for
6313 an array. Otherwise, it is part of the original argument
6314 in the source code. */
6315 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6316 probe = TREE_OPERAND (probe, 0);
6317 expr = probe;
6318 expr_type = TREE_TYPE (expr);
6322 /* [temp.arg.nontype]/5, bullet 1
6324 For a non-type template-parameter of integral or enumeration type,
6325 integral promotions (_conv.prom_) and integral conversions
6326 (_conv.integral_) are applied. */
6327 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6329 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6330 t = maybe_constant_value (t);
6331 if (t != error_mark_node)
6332 expr = t;
6334 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6335 return error_mark_node;
6337 /* Notice that there are constant expressions like '4 % 0' which
6338 do not fold into integer constants. */
6339 if (TREE_CODE (expr) != INTEGER_CST)
6341 if (complain & tf_error)
6343 int errs = errorcount, warns = warningcount + werrorcount;
6344 if (processing_template_decl
6345 && !require_potential_constant_expression (expr))
6346 return NULL_TREE;
6347 expr = cxx_constant_value (expr);
6348 if (errorcount > errs || warningcount + werrorcount > warns)
6349 inform (EXPR_LOC_OR_LOC (expr, input_location),
6350 "in template argument for type %qT ", type);
6351 if (expr == error_mark_node)
6352 return NULL_TREE;
6353 /* else cxx_constant_value complained but gave us
6354 a real constant, so go ahead. */
6355 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6357 else
6358 return NULL_TREE;
6361 /* Avoid typedef problems. */
6362 if (TREE_TYPE (expr) != type)
6363 expr = fold_convert (type, expr);
6365 /* [temp.arg.nontype]/5, bullet 2
6367 For a non-type template-parameter of type pointer to object,
6368 qualification conversions (_conv.qual_) and the array-to-pointer
6369 conversion (_conv.array_) are applied. */
6370 else if (TYPE_PTROBV_P (type))
6372 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6374 A template-argument for a non-type, non-template template-parameter
6375 shall be one of: [...]
6377 -- the name of a non-type template-parameter;
6378 -- the address of an object or function with external linkage, [...]
6379 expressed as "& id-expression" where the & is optional if the name
6380 refers to a function or array, or if the corresponding
6381 template-parameter is a reference.
6383 Here, we do not care about functions, as they are invalid anyway
6384 for a parameter of type pointer-to-object. */
6386 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6387 /* Non-type template parameters are OK. */
6389 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6390 /* Null pointer values are OK in C++11. */;
6391 else if (TREE_CODE (expr) != ADDR_EXPR
6392 && TREE_CODE (expr_type) != ARRAY_TYPE)
6394 if (VAR_P (expr))
6396 if (complain & tf_error)
6397 error ("%qD is not a valid template argument "
6398 "because %qD is a variable, not the address of "
6399 "a variable", expr, expr);
6400 return NULL_TREE;
6402 if (POINTER_TYPE_P (expr_type))
6404 if (complain & tf_error)
6405 error ("%qE is not a valid template argument for %qT "
6406 "because it is not the address of a variable",
6407 expr, type);
6408 return NULL_TREE;
6410 /* Other values, like integer constants, might be valid
6411 non-type arguments of some other type. */
6412 return error_mark_node;
6414 else
6416 tree decl;
6418 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6419 ? TREE_OPERAND (expr, 0) : expr);
6420 if (!VAR_P (decl))
6422 if (complain & tf_error)
6423 error ("%qE is not a valid template argument of type %qT "
6424 "because %qE is not a variable", expr, type, decl);
6425 return NULL_TREE;
6427 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6429 if (complain & tf_error)
6430 error ("%qE is not a valid template argument of type %qT "
6431 "because %qD does not have external linkage",
6432 expr, type, decl);
6433 return NULL_TREE;
6435 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6437 if (complain & tf_error)
6438 error ("%qE is not a valid template argument of type %qT "
6439 "because %qD has no linkage", expr, type, decl);
6440 return NULL_TREE;
6444 expr = decay_conversion (expr, complain);
6445 if (expr == error_mark_node)
6446 return error_mark_node;
6448 expr = perform_qualification_conversions (type, expr);
6449 if (expr == error_mark_node)
6450 return error_mark_node;
6452 /* [temp.arg.nontype]/5, bullet 3
6454 For a non-type template-parameter of type reference to object, no
6455 conversions apply. The type referred to by the reference may be more
6456 cv-qualified than the (otherwise identical) type of the
6457 template-argument. The template-parameter is bound directly to the
6458 template-argument, which must be an lvalue. */
6459 else if (TYPE_REF_OBJ_P (type))
6461 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6462 expr_type))
6463 return error_mark_node;
6465 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6467 if (complain & tf_error)
6468 error ("%qE is not a valid template argument for type %qT "
6469 "because of conflicts in cv-qualification", expr, type);
6470 return NULL_TREE;
6473 if (!real_lvalue_p (expr))
6475 if (complain & tf_error)
6476 error ("%qE is not a valid template argument for type %qT "
6477 "because it is not an lvalue", expr, type);
6478 return NULL_TREE;
6481 /* [temp.arg.nontype]/1
6483 A template-argument for a non-type, non-template template-parameter
6484 shall be one of: [...]
6486 -- the address of an object or function with external linkage. */
6487 if (INDIRECT_REF_P (expr)
6488 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6490 expr = TREE_OPERAND (expr, 0);
6491 if (DECL_P (expr))
6493 if (complain & tf_error)
6494 error ("%q#D is not a valid template argument for type %qT "
6495 "because a reference variable does not have a constant "
6496 "address", expr, type);
6497 return NULL_TREE;
6501 if (!DECL_P (expr))
6503 if (complain & tf_error)
6504 error ("%qE is not a valid template argument for type %qT "
6505 "because it is not an object with linkage",
6506 expr, type);
6507 return NULL_TREE;
6510 /* DR 1155 allows internal linkage in C++11 and up. */
6511 linkage_kind linkage = decl_linkage (expr);
6512 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6514 if (complain & tf_error)
6515 error ("%qE is not a valid template argument for type %qT "
6516 "because object %qD does not have linkage",
6517 expr, type, expr);
6518 return NULL_TREE;
6521 expr = build_nop (type, build_address (expr));
6523 /* [temp.arg.nontype]/5, bullet 4
6525 For a non-type template-parameter of type pointer to function, only
6526 the function-to-pointer conversion (_conv.func_) is applied. If the
6527 template-argument represents a set of overloaded functions (or a
6528 pointer to such), the matching function is selected from the set
6529 (_over.over_). */
6530 else if (TYPE_PTRFN_P (type))
6532 /* If the argument is a template-id, we might not have enough
6533 context information to decay the pointer. */
6534 if (!type_unknown_p (expr_type))
6536 expr = decay_conversion (expr, complain);
6537 if (expr == error_mark_node)
6538 return error_mark_node;
6541 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6542 /* Null pointer values are OK in C++11. */
6543 return perform_qualification_conversions (type, expr);
6545 expr = convert_nontype_argument_function (type, expr, complain);
6546 if (!expr || expr == error_mark_node)
6547 return expr;
6549 /* [temp.arg.nontype]/5, bullet 5
6551 For a non-type template-parameter of type reference to function, no
6552 conversions apply. If the template-argument represents a set of
6553 overloaded functions, the matching function is selected from the set
6554 (_over.over_). */
6555 else if (TYPE_REFFN_P (type))
6557 if (TREE_CODE (expr) == ADDR_EXPR)
6559 if (complain & tf_error)
6561 error ("%qE is not a valid template argument for type %qT "
6562 "because it is a pointer", expr, type);
6563 inform (input_location, "try using %qE instead",
6564 TREE_OPERAND (expr, 0));
6566 return NULL_TREE;
6569 expr = convert_nontype_argument_function (type, expr, complain);
6570 if (!expr || expr == error_mark_node)
6571 return expr;
6573 expr = build_nop (type, build_address (expr));
6575 /* [temp.arg.nontype]/5, bullet 6
6577 For a non-type template-parameter of type pointer to member function,
6578 no conversions apply. If the template-argument represents a set of
6579 overloaded member functions, the matching member function is selected
6580 from the set (_over.over_). */
6581 else if (TYPE_PTRMEMFUNC_P (type))
6583 expr = instantiate_type (type, expr, tf_none);
6584 if (expr == error_mark_node)
6585 return error_mark_node;
6587 /* [temp.arg.nontype] bullet 1 says the pointer to member
6588 expression must be a pointer-to-member constant. */
6589 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6590 return error_mark_node;
6592 /* There is no way to disable standard conversions in
6593 resolve_address_of_overloaded_function (called by
6594 instantiate_type). It is possible that the call succeeded by
6595 converting &B::I to &D::I (where B is a base of D), so we need
6596 to reject this conversion here.
6598 Actually, even if there was a way to disable standard conversions,
6599 it would still be better to reject them here so that we can
6600 provide a superior diagnostic. */
6601 if (!same_type_p (TREE_TYPE (expr), type))
6603 if (complain & tf_error)
6605 error ("%qE is not a valid template argument for type %qT "
6606 "because it is of type %qT", expr, type,
6607 TREE_TYPE (expr));
6608 /* If we are just one standard conversion off, explain. */
6609 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6610 inform (input_location,
6611 "standard conversions are not allowed in this context");
6613 return NULL_TREE;
6616 /* [temp.arg.nontype]/5, bullet 7
6618 For a non-type template-parameter of type pointer to data member,
6619 qualification conversions (_conv.qual_) are applied. */
6620 else if (TYPE_PTRDATAMEM_P (type))
6622 /* [temp.arg.nontype] bullet 1 says the pointer to member
6623 expression must be a pointer-to-member constant. */
6624 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6625 return error_mark_node;
6627 expr = perform_qualification_conversions (type, expr);
6628 if (expr == error_mark_node)
6629 return expr;
6631 else if (NULLPTR_TYPE_P (type))
6633 if (expr != nullptr_node)
6635 if (complain & tf_error)
6636 error ("%qE is not a valid template argument for type %qT "
6637 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6638 return NULL_TREE;
6640 return expr;
6642 /* A template non-type parameter must be one of the above. */
6643 else
6644 gcc_unreachable ();
6646 /* Sanity check: did we actually convert the argument to the
6647 right type? */
6648 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6649 (type, TREE_TYPE (expr)));
6650 return convert_from_reference (expr);
6653 /* Subroutine of coerce_template_template_parms, which returns 1 if
6654 PARM_PARM and ARG_PARM match using the rule for the template
6655 parameters of template template parameters. Both PARM and ARG are
6656 template parameters; the rest of the arguments are the same as for
6657 coerce_template_template_parms.
6659 static int
6660 coerce_template_template_parm (tree parm,
6661 tree arg,
6662 tsubst_flags_t complain,
6663 tree in_decl,
6664 tree outer_args)
6666 if (arg == NULL_TREE || error_operand_p (arg)
6667 || parm == NULL_TREE || error_operand_p (parm))
6668 return 0;
6670 if (TREE_CODE (arg) != TREE_CODE (parm))
6671 return 0;
6673 switch (TREE_CODE (parm))
6675 case TEMPLATE_DECL:
6676 /* We encounter instantiations of templates like
6677 template <template <template <class> class> class TT>
6678 class C; */
6680 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6681 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6683 if (!coerce_template_template_parms
6684 (parmparm, argparm, complain, in_decl, outer_args))
6685 return 0;
6687 /* Fall through. */
6689 case TYPE_DECL:
6690 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6691 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6692 /* Argument is a parameter pack but parameter is not. */
6693 return 0;
6694 break;
6696 case PARM_DECL:
6697 /* The tsubst call is used to handle cases such as
6699 template <int> class C {};
6700 template <class T, template <T> class TT> class D {};
6701 D<int, C> d;
6703 i.e. the parameter list of TT depends on earlier parameters. */
6704 if (!uses_template_parms (TREE_TYPE (arg)))
6706 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6707 if (!uses_template_parms (t)
6708 && !same_type_p (t, TREE_TYPE (arg)))
6709 return 0;
6712 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6713 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6714 /* Argument is a parameter pack but parameter is not. */
6715 return 0;
6717 break;
6719 default:
6720 gcc_unreachable ();
6723 return 1;
6727 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6728 template template parameters. Both PARM_PARMS and ARG_PARMS are
6729 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6730 or PARM_DECL.
6732 Consider the example:
6733 template <class T> class A;
6734 template<template <class U> class TT> class B;
6736 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6737 the parameters to A, and OUTER_ARGS contains A. */
6739 static int
6740 coerce_template_template_parms (tree parm_parms,
6741 tree arg_parms,
6742 tsubst_flags_t complain,
6743 tree in_decl,
6744 tree outer_args)
6746 int nparms, nargs, i;
6747 tree parm, arg;
6748 int variadic_p = 0;
6750 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6751 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6753 nparms = TREE_VEC_LENGTH (parm_parms);
6754 nargs = TREE_VEC_LENGTH (arg_parms);
6756 /* Determine whether we have a parameter pack at the end of the
6757 template template parameter's template parameter list. */
6758 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6760 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6762 if (error_operand_p (parm))
6763 return 0;
6765 switch (TREE_CODE (parm))
6767 case TEMPLATE_DECL:
6768 case TYPE_DECL:
6769 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6770 variadic_p = 1;
6771 break;
6773 case PARM_DECL:
6774 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6775 variadic_p = 1;
6776 break;
6778 default:
6779 gcc_unreachable ();
6783 if (nargs != nparms
6784 && !(variadic_p && nargs >= nparms - 1))
6785 return 0;
6787 /* Check all of the template parameters except the parameter pack at
6788 the end (if any). */
6789 for (i = 0; i < nparms - variadic_p; ++i)
6791 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6792 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6793 continue;
6795 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6796 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6798 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6799 outer_args))
6800 return 0;
6804 if (variadic_p)
6806 /* Check each of the template parameters in the template
6807 argument against the template parameter pack at the end of
6808 the template template parameter. */
6809 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6810 return 0;
6812 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6814 for (; i < nargs; ++i)
6816 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6817 continue;
6819 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6821 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6822 outer_args))
6823 return 0;
6827 return 1;
6830 /* Verifies that the deduced template arguments (in TARGS) for the
6831 template template parameters (in TPARMS) represent valid bindings,
6832 by comparing the template parameter list of each template argument
6833 to the template parameter list of its corresponding template
6834 template parameter, in accordance with DR150. This
6835 routine can only be called after all template arguments have been
6836 deduced. It will return TRUE if all of the template template
6837 parameter bindings are okay, FALSE otherwise. */
6838 bool
6839 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6841 int i, ntparms = TREE_VEC_LENGTH (tparms);
6842 bool ret = true;
6844 /* We're dealing with template parms in this process. */
6845 ++processing_template_decl;
6847 targs = INNERMOST_TEMPLATE_ARGS (targs);
6849 for (i = 0; i < ntparms; ++i)
6851 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6852 tree targ = TREE_VEC_ELT (targs, i);
6854 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6856 tree packed_args = NULL_TREE;
6857 int idx, len = 1;
6859 if (ARGUMENT_PACK_P (targ))
6861 /* Look inside the argument pack. */
6862 packed_args = ARGUMENT_PACK_ARGS (targ);
6863 len = TREE_VEC_LENGTH (packed_args);
6866 for (idx = 0; idx < len; ++idx)
6868 tree targ_parms = NULL_TREE;
6870 if (packed_args)
6871 /* Extract the next argument from the argument
6872 pack. */
6873 targ = TREE_VEC_ELT (packed_args, idx);
6875 if (PACK_EXPANSION_P (targ))
6876 /* Look at the pattern of the pack expansion. */
6877 targ = PACK_EXPANSION_PATTERN (targ);
6879 /* Extract the template parameters from the template
6880 argument. */
6881 if (TREE_CODE (targ) == TEMPLATE_DECL)
6882 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6883 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6884 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6886 /* Verify that we can coerce the template template
6887 parameters from the template argument to the template
6888 parameter. This requires an exact match. */
6889 if (targ_parms
6890 && !coerce_template_template_parms
6891 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6892 targ_parms,
6893 tf_none,
6894 tparm,
6895 targs))
6897 ret = false;
6898 goto out;
6904 out:
6906 --processing_template_decl;
6907 return ret;
6910 /* Since type attributes aren't mangled, we need to strip them from
6911 template type arguments. */
6913 static tree
6914 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6916 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6917 return arg;
6918 bool removed_attributes = false;
6919 tree canon = strip_typedefs (arg, &removed_attributes);
6920 if (removed_attributes
6921 && (complain & tf_warning))
6922 warning (0, "ignoring attributes on template argument %qT", arg);
6923 return canon;
6926 // A template declaration can be substituted for a constrained
6927 // template template parameter only when the argument is more
6928 // constrained than the parameter.
6929 static bool
6930 is_compatible_template_arg (tree parm, tree arg)
6932 tree parm_cons = get_constraints (parm);
6934 /* For now, allow constrained template template arguments
6935 and unconstrained template template parameters. */
6936 if (parm_cons == NULL_TREE)
6937 return true;
6939 tree arg_cons = get_constraints (arg);
6941 // If the template parameter is constrained, we need to rewrite its
6942 // constraints in terms of the ARG's template parameters. This ensures
6943 // that all of the template parameter types will have the same depth.
6945 // Note that this is only valid when coerce_template_template_parm is
6946 // true for the innermost template parameters of PARM and ARG. In other
6947 // words, because coercion is successful, this conversion will be valid.
6948 if (parm_cons)
6950 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
6951 parm_cons = tsubst_constraint_info (parm_cons,
6952 INNERMOST_TEMPLATE_ARGS (args),
6953 tf_none, NULL_TREE);
6954 if (parm_cons == error_mark_node)
6955 return false;
6958 return subsumes (parm_cons, arg_cons);
6961 // Convert a placeholder argument into a binding to the original
6962 // parameter. The original parameter is saved as the TREE_TYPE of
6963 // ARG.
6964 static inline tree
6965 convert_wildcard_argument (tree parm, tree arg)
6967 TREE_TYPE (arg) = parm;
6968 return arg;
6971 /* Convert the indicated template ARG as necessary to match the
6972 indicated template PARM. Returns the converted ARG, or
6973 error_mark_node if the conversion was unsuccessful. Error and
6974 warning messages are issued under control of COMPLAIN. This
6975 conversion is for the Ith parameter in the parameter list. ARGS is
6976 the full set of template arguments deduced so far. */
6978 static tree
6979 convert_template_argument (tree parm,
6980 tree arg,
6981 tree args,
6982 tsubst_flags_t complain,
6983 int i,
6984 tree in_decl)
6986 tree orig_arg;
6987 tree val;
6988 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6990 if (parm == error_mark_node)
6991 return error_mark_node;
6993 /* Trivially convert placeholders. */
6994 if (TREE_CODE (arg) == WILDCARD_DECL)
6995 return convert_wildcard_argument (parm, arg);
6997 if (TREE_CODE (arg) == TREE_LIST
6998 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7000 /* The template argument was the name of some
7001 member function. That's usually
7002 invalid, but static members are OK. In any
7003 case, grab the underlying fields/functions
7004 and issue an error later if required. */
7005 orig_arg = TREE_VALUE (arg);
7006 TREE_TYPE (arg) = unknown_type_node;
7009 orig_arg = arg;
7011 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7012 requires_type = (TREE_CODE (parm) == TYPE_DECL
7013 || requires_tmpl_type);
7015 /* When determining whether an argument pack expansion is a template,
7016 look at the pattern. */
7017 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7018 arg = PACK_EXPANSION_PATTERN (arg);
7020 /* Deal with an injected-class-name used as a template template arg. */
7021 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7023 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7024 if (TREE_CODE (t) == TEMPLATE_DECL)
7026 if (cxx_dialect >= cxx11)
7027 /* OK under DR 1004. */;
7028 else if (complain & tf_warning_or_error)
7029 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7030 " used as template template argument", TYPE_NAME (arg));
7031 else if (flag_pedantic_errors)
7032 t = arg;
7034 arg = t;
7038 is_tmpl_type =
7039 ((TREE_CODE (arg) == TEMPLATE_DECL
7040 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7041 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7042 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7043 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7045 if (is_tmpl_type
7046 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7047 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7048 arg = TYPE_STUB_DECL (arg);
7050 is_type = TYPE_P (arg) || is_tmpl_type;
7052 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7053 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7055 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7057 if (complain & tf_error)
7058 error ("invalid use of destructor %qE as a type", orig_arg);
7059 return error_mark_node;
7062 permerror (input_location,
7063 "to refer to a type member of a template parameter, "
7064 "use %<typename %E%>", orig_arg);
7066 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7067 TREE_OPERAND (arg, 1),
7068 typename_type,
7069 complain);
7070 arg = orig_arg;
7071 is_type = 1;
7073 if (is_type != requires_type)
7075 if (in_decl)
7077 if (complain & tf_error)
7079 error ("type/value mismatch at argument %d in template "
7080 "parameter list for %qD",
7081 i + 1, in_decl);
7082 if (is_type)
7083 inform (input_location,
7084 " expected a constant of type %qT, got %qT",
7085 TREE_TYPE (parm),
7086 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7087 else if (requires_tmpl_type)
7088 inform (input_location,
7089 " expected a class template, got %qE", orig_arg);
7090 else
7091 inform (input_location,
7092 " expected a type, got %qE", orig_arg);
7095 return error_mark_node;
7097 if (is_tmpl_type ^ requires_tmpl_type)
7099 if (in_decl && (complain & tf_error))
7101 error ("type/value mismatch at argument %d in template "
7102 "parameter list for %qD",
7103 i + 1, in_decl);
7104 if (is_tmpl_type)
7105 inform (input_location,
7106 " expected a type, got %qT", DECL_NAME (arg));
7107 else
7108 inform (input_location,
7109 " expected a class template, got %qT", orig_arg);
7111 return error_mark_node;
7114 if (is_type)
7116 if (requires_tmpl_type)
7118 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7119 val = orig_arg;
7120 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7121 /* The number of argument required is not known yet.
7122 Just accept it for now. */
7123 val = TREE_TYPE (arg);
7124 else
7126 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7127 tree argparm;
7129 /* Strip alias templates that are equivalent to another
7130 template. */
7131 arg = get_underlying_template (arg);
7132 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7134 if (coerce_template_template_parms (parmparm, argparm,
7135 complain, in_decl,
7136 args))
7138 val = arg;
7140 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7141 TEMPLATE_DECL. */
7142 if (val != error_mark_node)
7144 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7145 val = TREE_TYPE (val);
7146 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7147 val = make_pack_expansion (val);
7150 else
7152 if (in_decl && (complain & tf_error))
7154 error ("type/value mismatch at argument %d in "
7155 "template parameter list for %qD",
7156 i + 1, in_decl);
7157 inform (input_location,
7158 " expected a template of type %qD, got %qT",
7159 parm, orig_arg);
7162 val = error_mark_node;
7165 // Check that the constraints are compatible before allowing the
7166 // substitution.
7167 if (val != error_mark_node)
7168 if (!is_compatible_template_arg (parm, arg))
7170 if (in_decl && (complain & tf_error))
7172 error ("constraint mismatch at argument %d in "
7173 "template parameter list for %qD",
7174 i + 1, in_decl);
7175 inform (input_location, " expected %qD but got %qD",
7176 parm, arg);
7178 val = error_mark_node;
7182 else
7183 val = orig_arg;
7184 /* We only form one instance of each template specialization.
7185 Therefore, if we use a non-canonical variant (i.e., a
7186 typedef), any future messages referring to the type will use
7187 the typedef, which is confusing if those future uses do not
7188 themselves also use the typedef. */
7189 if (TYPE_P (val))
7190 val = canonicalize_type_argument (val, complain);
7192 else
7194 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7196 if (invalid_nontype_parm_type_p (t, complain))
7197 return error_mark_node;
7199 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7201 if (same_type_p (t, TREE_TYPE (orig_arg)))
7202 val = orig_arg;
7203 else
7205 /* Not sure if this is reachable, but it doesn't hurt
7206 to be robust. */
7207 error ("type mismatch in nontype parameter pack");
7208 val = error_mark_node;
7211 else if (!dependent_template_arg_p (orig_arg)
7212 && !uses_template_parms (t))
7213 /* We used to call digest_init here. However, digest_init
7214 will report errors, which we don't want when complain
7215 is zero. More importantly, digest_init will try too
7216 hard to convert things: for example, `0' should not be
7217 converted to pointer type at this point according to
7218 the standard. Accepting this is not merely an
7219 extension, since deciding whether or not these
7220 conversions can occur is part of determining which
7221 function template to call, or whether a given explicit
7222 argument specification is valid. */
7223 val = convert_nontype_argument (t, orig_arg, complain);
7224 else
7226 bool removed_attr = false;
7227 val = strip_typedefs_expr (orig_arg, &removed_attr);
7230 if (val == NULL_TREE)
7231 val = error_mark_node;
7232 else if (val == error_mark_node && (complain & tf_error))
7233 error ("could not convert template argument %qE to %qT", orig_arg, t);
7235 if (INDIRECT_REF_P (val))
7237 /* Reject template arguments that are references to built-in
7238 functions with no library fallbacks. */
7239 const_tree inner = TREE_OPERAND (val, 0);
7240 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7241 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner))) == FUNCTION_TYPE
7242 && TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7243 && 0 < TREE_OPERAND_LENGTH (inner)
7244 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7245 return error_mark_node;
7248 if (TREE_CODE (val) == SCOPE_REF)
7250 /* Strip typedefs from the SCOPE_REF. */
7251 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7252 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7253 complain);
7254 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7255 QUALIFIED_NAME_IS_TEMPLATE (val));
7259 return val;
7262 /* Coerces the remaining template arguments in INNER_ARGS (from
7263 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7264 Returns the coerced argument pack. PARM_IDX is the position of this
7265 parameter in the template parameter list. ARGS is the original
7266 template argument list. */
7267 static tree
7268 coerce_template_parameter_pack (tree parms,
7269 int parm_idx,
7270 tree args,
7271 tree inner_args,
7272 int arg_idx,
7273 tree new_args,
7274 int* lost,
7275 tree in_decl,
7276 tsubst_flags_t complain)
7278 tree parm = TREE_VEC_ELT (parms, parm_idx);
7279 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7280 tree packed_args;
7281 tree argument_pack;
7282 tree packed_parms = NULL_TREE;
7284 if (arg_idx > nargs)
7285 arg_idx = nargs;
7287 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7289 /* When the template parameter is a non-type template parameter pack
7290 or template template parameter pack whose type or template
7291 parameters use parameter packs, we know exactly how many arguments
7292 we are looking for. Build a vector of the instantiated decls for
7293 these template parameters in PACKED_PARMS. */
7294 /* We can't use make_pack_expansion here because it would interpret a
7295 _DECL as a use rather than a declaration. */
7296 tree decl = TREE_VALUE (parm);
7297 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7298 SET_PACK_EXPANSION_PATTERN (exp, decl);
7299 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7300 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7302 TREE_VEC_LENGTH (args)--;
7303 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7304 TREE_VEC_LENGTH (args)++;
7306 if (packed_parms == error_mark_node)
7307 return error_mark_node;
7309 /* If we're doing a partial instantiation of a member template,
7310 verify that all of the types used for the non-type
7311 template parameter pack are, in fact, valid for non-type
7312 template parameters. */
7313 if (arg_idx < nargs
7314 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7316 int j, len = TREE_VEC_LENGTH (packed_parms);
7317 for (j = 0; j < len; ++j)
7319 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7320 if (invalid_nontype_parm_type_p (t, complain))
7321 return error_mark_node;
7323 /* We don't know how many args we have yet, just
7324 use the unconverted ones for now. */
7325 return NULL_TREE;
7328 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7330 /* Check if we have a placeholder pack, which indicates we're
7331 in the context of a introduction list. In that case we want
7332 to match this pack to the single placeholder. */
7333 else if (arg_idx < nargs
7334 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7335 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7337 nargs = arg_idx + 1;
7338 packed_args = make_tree_vec (1);
7340 else
7341 packed_args = make_tree_vec (nargs - arg_idx);
7343 /* Convert the remaining arguments, which will be a part of the
7344 parameter pack "parm". */
7345 for (; arg_idx < nargs; ++arg_idx)
7347 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7348 tree actual_parm = TREE_VALUE (parm);
7349 int pack_idx = arg_idx - parm_idx;
7351 if (packed_parms)
7353 /* Once we've packed as many args as we have types, stop. */
7354 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7355 break;
7356 else if (PACK_EXPANSION_P (arg))
7357 /* We don't know how many args we have yet, just
7358 use the unconverted ones for now. */
7359 return NULL_TREE;
7360 else
7361 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7364 if (arg == error_mark_node)
7366 if (complain & tf_error)
7367 error ("template argument %d is invalid", arg_idx + 1);
7369 else
7370 arg = convert_template_argument (actual_parm,
7371 arg, new_args, complain, parm_idx,
7372 in_decl);
7373 if (arg == error_mark_node)
7374 (*lost)++;
7375 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7378 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
7379 && TREE_VEC_LENGTH (packed_args) > 0)
7381 if (complain & tf_error)
7382 error ("wrong number of template arguments (%d, should be %d)",
7383 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
7384 return error_mark_node;
7387 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7388 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7389 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7390 else
7392 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7393 TREE_TYPE (argument_pack)
7394 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7395 TREE_CONSTANT (argument_pack) = 1;
7398 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7399 if (CHECKING_P)
7400 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7401 TREE_VEC_LENGTH (packed_args));
7402 return argument_pack;
7405 /* Returns the number of pack expansions in the template argument vector
7406 ARGS. */
7408 static int
7409 pack_expansion_args_count (tree args)
7411 int i;
7412 int count = 0;
7413 if (args)
7414 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7416 tree elt = TREE_VEC_ELT (args, i);
7417 if (elt && PACK_EXPANSION_P (elt))
7418 ++count;
7420 return count;
7423 /* Convert all template arguments to their appropriate types, and
7424 return a vector containing the innermost resulting template
7425 arguments. If any error occurs, return error_mark_node. Error and
7426 warning messages are issued under control of COMPLAIN.
7428 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7429 for arguments not specified in ARGS. Otherwise, if
7430 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7431 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7432 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7433 ARGS. */
7435 static tree
7436 coerce_template_parms (tree parms,
7437 tree args,
7438 tree in_decl,
7439 tsubst_flags_t complain,
7440 bool require_all_args,
7441 bool use_default_args)
7443 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7444 tree orig_inner_args;
7445 tree inner_args;
7446 tree new_args;
7447 tree new_inner_args;
7448 int saved_unevaluated_operand;
7449 int saved_inhibit_evaluation_warnings;
7451 /* When used as a boolean value, indicates whether this is a
7452 variadic template parameter list. Since it's an int, we can also
7453 subtract it from nparms to get the number of non-variadic
7454 parameters. */
7455 int variadic_p = 0;
7456 int variadic_args_p = 0;
7457 int post_variadic_parms = 0;
7459 /* Likewise for parameters with default arguments. */
7460 int default_p = 0;
7462 if (args == error_mark_node)
7463 return error_mark_node;
7465 nparms = TREE_VEC_LENGTH (parms);
7467 /* Determine if there are any parameter packs or default arguments. */
7468 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7470 tree parm = TREE_VEC_ELT (parms, parm_idx);
7471 if (variadic_p)
7472 ++post_variadic_parms;
7473 if (template_parameter_pack_p (TREE_VALUE (parm)))
7474 ++variadic_p;
7475 if (TREE_PURPOSE (parm))
7476 ++default_p;
7479 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7480 /* If there are no parameters that follow a parameter pack, we need to
7481 expand any argument packs so that we can deduce a parameter pack from
7482 some non-packed args followed by an argument pack, as in variadic85.C.
7483 If there are such parameters, we need to leave argument packs intact
7484 so the arguments are assigned properly. This can happen when dealing
7485 with a nested class inside a partial specialization of a class
7486 template, as in variadic92.C, or when deducing a template parameter pack
7487 from a sub-declarator, as in variadic114.C. */
7488 if (!post_variadic_parms)
7489 inner_args = expand_template_argument_pack (inner_args);
7491 /* Count any pack expansion args. */
7492 variadic_args_p = pack_expansion_args_count (inner_args);
7494 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7495 if ((nargs > nparms && !variadic_p)
7496 || (nargs < nparms - variadic_p
7497 && require_all_args
7498 && !variadic_args_p
7499 && (!use_default_args
7500 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7501 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7503 if (complain & tf_error)
7505 if (variadic_p || default_p)
7507 nparms -= variadic_p + default_p;
7508 error ("wrong number of template arguments "
7509 "(%d, should be at least %d)", nargs, nparms);
7511 else
7512 error ("wrong number of template arguments "
7513 "(%d, should be %d)", nargs, nparms);
7515 if (in_decl)
7516 inform (DECL_SOURCE_LOCATION (in_decl),
7517 "provided for %qD", in_decl);
7520 return error_mark_node;
7522 /* We can't pass a pack expansion to a non-pack parameter of an alias
7523 template (DR 1430). */
7524 else if (in_decl
7525 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7526 || concept_template_p (in_decl))
7527 && variadic_args_p
7528 && nargs - variadic_args_p < nparms - variadic_p)
7530 if (complain & tf_error)
7532 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7534 tree arg = TREE_VEC_ELT (inner_args, i);
7535 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7537 if (PACK_EXPANSION_P (arg)
7538 && !template_parameter_pack_p (parm))
7540 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7541 error_at (location_of (arg),
7542 "pack expansion argument for non-pack parameter "
7543 "%qD of alias template %qD", parm, in_decl);
7544 else
7545 error_at (location_of (arg),
7546 "pack expansion argument for non-pack parameter "
7547 "%qD of concept %qD", parm, in_decl);
7548 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7549 goto found;
7552 gcc_unreachable ();
7553 found:;
7555 return error_mark_node;
7558 /* We need to evaluate the template arguments, even though this
7559 template-id may be nested within a "sizeof". */
7560 saved_unevaluated_operand = cp_unevaluated_operand;
7561 cp_unevaluated_operand = 0;
7562 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7563 c_inhibit_evaluation_warnings = 0;
7564 new_inner_args = make_tree_vec (nparms);
7565 new_args = add_outermost_template_args (args, new_inner_args);
7566 int pack_adjust = 0;
7567 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7569 tree arg;
7570 tree parm;
7572 /* Get the Ith template parameter. */
7573 parm = TREE_VEC_ELT (parms, parm_idx);
7575 if (parm == error_mark_node)
7577 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7578 continue;
7581 /* Calculate the next argument. */
7582 if (arg_idx < nargs)
7583 arg = TREE_VEC_ELT (inner_args, arg_idx);
7584 else
7585 arg = NULL_TREE;
7587 if (template_parameter_pack_p (TREE_VALUE (parm))
7588 && !(arg && ARGUMENT_PACK_P (arg)))
7590 /* Some arguments will be placed in the
7591 template parameter pack PARM. */
7592 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7593 inner_args, arg_idx,
7594 new_args, &lost,
7595 in_decl, complain);
7597 if (arg == NULL_TREE)
7599 /* We don't know how many args we have yet, just use the
7600 unconverted (and still packed) ones for now. */
7601 new_inner_args = orig_inner_args;
7602 arg_idx = nargs;
7603 break;
7606 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7608 /* Store this argument. */
7609 if (arg == error_mark_node)
7611 lost++;
7612 /* We are done with all of the arguments. */
7613 arg_idx = nargs;
7615 else
7617 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7618 arg_idx += pack_adjust;
7621 continue;
7623 else if (arg)
7625 if (PACK_EXPANSION_P (arg))
7627 /* "If every valid specialization of a variadic template
7628 requires an empty template parameter pack, the template is
7629 ill-formed, no diagnostic required." So check that the
7630 pattern works with this parameter. */
7631 tree pattern = PACK_EXPANSION_PATTERN (arg);
7632 tree conv = convert_template_argument (TREE_VALUE (parm),
7633 pattern, new_args,
7634 complain, parm_idx,
7635 in_decl);
7636 if (conv == error_mark_node)
7638 inform (input_location, "so any instantiation with a "
7639 "non-empty parameter pack would be ill-formed");
7640 ++lost;
7642 else if (TYPE_P (conv) && !TYPE_P (pattern))
7643 /* Recover from missing typename. */
7644 TREE_VEC_ELT (inner_args, arg_idx)
7645 = make_pack_expansion (conv);
7647 /* We don't know how many args we have yet, just
7648 use the unconverted ones for now. */
7649 new_inner_args = inner_args;
7650 arg_idx = nargs;
7651 break;
7654 else if (require_all_args)
7656 /* There must be a default arg in this case. */
7657 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7658 complain, in_decl);
7659 /* The position of the first default template argument,
7660 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7661 Record that. */
7662 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7663 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7664 arg_idx - pack_adjust);
7666 else
7667 break;
7669 if (arg == error_mark_node)
7671 if (complain & tf_error)
7672 error ("template argument %d is invalid", arg_idx + 1);
7674 else if (!arg)
7675 /* This only occurs if there was an error in the template
7676 parameter list itself (which we would already have
7677 reported) that we are trying to recover from, e.g., a class
7678 template with a parameter list such as
7679 template<typename..., typename>. */
7680 ++lost;
7681 else
7682 arg = convert_template_argument (TREE_VALUE (parm),
7683 arg, new_args, complain,
7684 parm_idx, in_decl);
7686 if (arg == error_mark_node)
7687 lost++;
7688 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7690 cp_unevaluated_operand = saved_unevaluated_operand;
7691 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7693 if (variadic_p && arg_idx < nargs)
7695 if (complain & tf_error)
7697 error ("wrong number of template arguments "
7698 "(%d, should be %d)", nargs, arg_idx);
7699 if (in_decl)
7700 error ("provided for %q+D", in_decl);
7702 return error_mark_node;
7705 if (lost)
7706 return error_mark_node;
7708 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7709 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7710 TREE_VEC_LENGTH (new_inner_args));
7712 return new_inner_args;
7715 /* Convert all template arguments to their appropriate types, and
7716 return a vector containing the innermost resulting template
7717 arguments. If any error occurs, return error_mark_node. Error and
7718 warning messages are not issued.
7720 Note that no function argument deduction is performed, and default
7721 arguments are used to fill in unspecified arguments. */
7722 tree
7723 coerce_template_parms (tree parms, tree args, tree in_decl)
7725 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7728 /* Convert all template arguments to their appropriate type, and
7729 instantiate default arguments as needed. This returns a vector
7730 containing the innermost resulting template arguments, or
7731 error_mark_node if unsuccessful. */
7732 tree
7733 coerce_template_parms (tree parms, tree args, tree in_decl,
7734 tsubst_flags_t complain)
7736 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7739 /* Like coerce_template_parms. If PARMS represents all template
7740 parameters levels, this function returns a vector of vectors
7741 representing all the resulting argument levels. Note that in this
7742 case, only the innermost arguments are coerced because the
7743 outermost ones are supposed to have been coerced already.
7745 Otherwise, if PARMS represents only (the innermost) vector of
7746 parameters, this function returns a vector containing just the
7747 innermost resulting arguments. */
7749 static tree
7750 coerce_innermost_template_parms (tree parms,
7751 tree args,
7752 tree in_decl,
7753 tsubst_flags_t complain,
7754 bool require_all_args,
7755 bool use_default_args)
7757 int parms_depth = TMPL_PARMS_DEPTH (parms);
7758 int args_depth = TMPL_ARGS_DEPTH (args);
7759 tree coerced_args;
7761 if (parms_depth > 1)
7763 coerced_args = make_tree_vec (parms_depth);
7764 tree level;
7765 int cur_depth;
7767 for (level = parms, cur_depth = parms_depth;
7768 parms_depth > 0 && level != NULL_TREE;
7769 level = TREE_CHAIN (level), --cur_depth)
7771 tree l;
7772 if (cur_depth == args_depth)
7773 l = coerce_template_parms (TREE_VALUE (level),
7774 args, in_decl, complain,
7775 require_all_args,
7776 use_default_args);
7777 else
7778 l = TMPL_ARGS_LEVEL (args, cur_depth);
7780 if (l == error_mark_node)
7781 return error_mark_node;
7783 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7786 else
7787 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7788 args, in_decl, complain,
7789 require_all_args,
7790 use_default_args);
7791 return coerced_args;
7794 /* Returns 1 if template args OT and NT are equivalent. */
7796 static int
7797 template_args_equal (tree ot, tree nt)
7799 if (nt == ot)
7800 return 1;
7801 if (nt == NULL_TREE || ot == NULL_TREE)
7802 return false;
7804 if (TREE_CODE (nt) == TREE_VEC)
7805 /* For member templates */
7806 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7807 else if (PACK_EXPANSION_P (ot))
7808 return (PACK_EXPANSION_P (nt)
7809 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7810 PACK_EXPANSION_PATTERN (nt))
7811 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7812 PACK_EXPANSION_EXTRA_ARGS (nt)));
7813 else if (ARGUMENT_PACK_P (ot))
7815 int i, len;
7816 tree opack, npack;
7818 if (!ARGUMENT_PACK_P (nt))
7819 return 0;
7821 opack = ARGUMENT_PACK_ARGS (ot);
7822 npack = ARGUMENT_PACK_ARGS (nt);
7823 len = TREE_VEC_LENGTH (opack);
7824 if (TREE_VEC_LENGTH (npack) != len)
7825 return 0;
7826 for (i = 0; i < len; ++i)
7827 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7828 TREE_VEC_ELT (npack, i)))
7829 return 0;
7830 return 1;
7832 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7834 /* We get here probably because we are in the middle of substituting
7835 into the pattern of a pack expansion. In that case the
7836 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7837 interested in. So we want to use the initial pack argument for
7838 the comparison. */
7839 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7840 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7841 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7842 return template_args_equal (ot, nt);
7844 else if (TYPE_P (nt))
7846 if (!TYPE_P (ot))
7847 return false;
7848 /* Don't treat an alias template specialization with dependent
7849 arguments as equivalent to its underlying type when used as a
7850 template argument; we need them to be distinct so that we
7851 substitute into the specialization arguments at instantiation
7852 time. And aliases can't be equivalent without being ==, so
7853 we don't need to look any deeper. */
7854 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7855 return false;
7856 else
7857 return same_type_p (ot, nt);
7859 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7860 return 0;
7861 else
7863 /* Try to treat a template non-type argument that has been converted
7864 to the parameter type as equivalent to one that hasn't yet. */
7865 for (enum tree_code code1 = TREE_CODE (ot);
7866 CONVERT_EXPR_CODE_P (code1)
7867 || code1 == NON_LVALUE_EXPR;
7868 code1 = TREE_CODE (ot))
7869 ot = TREE_OPERAND (ot, 0);
7870 for (enum tree_code code2 = TREE_CODE (nt);
7871 CONVERT_EXPR_CODE_P (code2)
7872 || code2 == NON_LVALUE_EXPR;
7873 code2 = TREE_CODE (nt))
7874 nt = TREE_OPERAND (nt, 0);
7876 return cp_tree_equal (ot, nt);
7880 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7881 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7882 NEWARG_PTR with the offending arguments if they are non-NULL. */
7884 static int
7885 comp_template_args_with_info (tree oldargs, tree newargs,
7886 tree *oldarg_ptr, tree *newarg_ptr)
7888 int i;
7890 if (oldargs == newargs)
7891 return 1;
7893 if (!oldargs || !newargs)
7894 return 0;
7896 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7897 return 0;
7899 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7901 tree nt = TREE_VEC_ELT (newargs, i);
7902 tree ot = TREE_VEC_ELT (oldargs, i);
7904 if (! template_args_equal (ot, nt))
7906 if (oldarg_ptr != NULL)
7907 *oldarg_ptr = ot;
7908 if (newarg_ptr != NULL)
7909 *newarg_ptr = nt;
7910 return 0;
7913 return 1;
7916 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7917 of template arguments. Returns 0 otherwise. */
7920 comp_template_args (tree oldargs, tree newargs)
7922 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7925 static void
7926 add_pending_template (tree d)
7928 tree ti = (TYPE_P (d)
7929 ? CLASSTYPE_TEMPLATE_INFO (d)
7930 : DECL_TEMPLATE_INFO (d));
7931 struct pending_template *pt;
7932 int level;
7934 if (TI_PENDING_TEMPLATE_FLAG (ti))
7935 return;
7937 /* We are called both from instantiate_decl, where we've already had a
7938 tinst_level pushed, and instantiate_template, where we haven't.
7939 Compensate. */
7940 level = !current_tinst_level || current_tinst_level->decl != d;
7942 if (level)
7943 push_tinst_level (d);
7945 pt = ggc_alloc<pending_template> ();
7946 pt->next = NULL;
7947 pt->tinst = current_tinst_level;
7948 if (last_pending_template)
7949 last_pending_template->next = pt;
7950 else
7951 pending_templates = pt;
7953 last_pending_template = pt;
7955 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7957 if (level)
7958 pop_tinst_level ();
7962 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7963 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7964 documentation for TEMPLATE_ID_EXPR. */
7966 tree
7967 lookup_template_function (tree fns, tree arglist)
7969 tree type;
7971 if (fns == error_mark_node || arglist == error_mark_node)
7972 return error_mark_node;
7974 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7976 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7978 error ("%q#D is not a function template", fns);
7979 return error_mark_node;
7982 if (BASELINK_P (fns))
7984 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7985 unknown_type_node,
7986 BASELINK_FUNCTIONS (fns),
7987 arglist);
7988 return fns;
7991 type = TREE_TYPE (fns);
7992 if (TREE_CODE (fns) == OVERLOAD || !type)
7993 type = unknown_type_node;
7995 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7998 /* Within the scope of a template class S<T>, the name S gets bound
7999 (in build_self_reference) to a TYPE_DECL for the class, not a
8000 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8001 or one of its enclosing classes, and that type is a template,
8002 return the associated TEMPLATE_DECL. Otherwise, the original
8003 DECL is returned.
8005 Also handle the case when DECL is a TREE_LIST of ambiguous
8006 injected-class-names from different bases. */
8008 tree
8009 maybe_get_template_decl_from_type_decl (tree decl)
8011 if (decl == NULL_TREE)
8012 return decl;
8014 /* DR 176: A lookup that finds an injected-class-name (10.2
8015 [class.member.lookup]) can result in an ambiguity in certain cases
8016 (for example, if it is found in more than one base class). If all of
8017 the injected-class-names that are found refer to specializations of
8018 the same class template, and if the name is followed by a
8019 template-argument-list, the reference refers to the class template
8020 itself and not a specialization thereof, and is not ambiguous. */
8021 if (TREE_CODE (decl) == TREE_LIST)
8023 tree t, tmpl = NULL_TREE;
8024 for (t = decl; t; t = TREE_CHAIN (t))
8026 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8027 if (!tmpl)
8028 tmpl = elt;
8029 else if (tmpl != elt)
8030 break;
8032 if (tmpl && t == NULL_TREE)
8033 return tmpl;
8034 else
8035 return decl;
8038 return (decl != NULL_TREE
8039 && DECL_SELF_REFERENCE_P (decl)
8040 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8041 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8044 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8045 parameters, find the desired type.
8047 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8049 IN_DECL, if non-NULL, is the template declaration we are trying to
8050 instantiate.
8052 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8053 the class we are looking up.
8055 Issue error and warning messages under control of COMPLAIN.
8057 If the template class is really a local class in a template
8058 function, then the FUNCTION_CONTEXT is the function in which it is
8059 being instantiated.
8061 ??? Note that this function is currently called *twice* for each
8062 template-id: the first time from the parser, while creating the
8063 incomplete type (finish_template_type), and the second type during the
8064 real instantiation (instantiate_template_class). This is surely something
8065 that we want to avoid. It also causes some problems with argument
8066 coercion (see convert_nontype_argument for more information on this). */
8068 static tree
8069 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8070 int entering_scope, tsubst_flags_t complain)
8072 tree templ = NULL_TREE, parmlist;
8073 tree t;
8074 spec_entry **slot;
8075 spec_entry *entry;
8076 spec_entry elt;
8077 hashval_t hash;
8079 if (identifier_p (d1))
8081 tree value = innermost_non_namespace_value (d1);
8082 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8083 templ = value;
8084 else
8086 if (context)
8087 push_decl_namespace (context);
8088 templ = lookup_name (d1);
8089 templ = maybe_get_template_decl_from_type_decl (templ);
8090 if (context)
8091 pop_decl_namespace ();
8093 if (templ)
8094 context = DECL_CONTEXT (templ);
8096 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8098 tree type = TREE_TYPE (d1);
8100 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8101 an implicit typename for the second A. Deal with it. */
8102 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8103 type = TREE_TYPE (type);
8105 if (CLASSTYPE_TEMPLATE_INFO (type))
8107 templ = CLASSTYPE_TI_TEMPLATE (type);
8108 d1 = DECL_NAME (templ);
8111 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8112 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8114 templ = TYPE_TI_TEMPLATE (d1);
8115 d1 = DECL_NAME (templ);
8117 else if (DECL_TYPE_TEMPLATE_P (d1))
8119 templ = d1;
8120 d1 = DECL_NAME (templ);
8121 context = DECL_CONTEXT (templ);
8123 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8125 templ = d1;
8126 d1 = DECL_NAME (templ);
8129 /* Issue an error message if we didn't find a template. */
8130 if (! templ)
8132 if (complain & tf_error)
8133 error ("%qT is not a template", d1);
8134 return error_mark_node;
8137 if (TREE_CODE (templ) != TEMPLATE_DECL
8138 /* Make sure it's a user visible template, if it was named by
8139 the user. */
8140 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8141 && !PRIMARY_TEMPLATE_P (templ)))
8143 if (complain & tf_error)
8145 error ("non-template type %qT used as a template", d1);
8146 if (in_decl)
8147 error ("for template declaration %q+D", in_decl);
8149 return error_mark_node;
8152 complain &= ~tf_user;
8154 /* An alias that just changes the name of a template is equivalent to the
8155 other template, so if any of the arguments are pack expansions, strip
8156 the alias to avoid problems with a pack expansion passed to a non-pack
8157 alias template parameter (DR 1430). */
8158 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8159 templ = get_underlying_template (templ);
8161 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8163 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8164 template arguments */
8166 tree parm;
8167 tree arglist2;
8168 tree outer;
8170 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8172 /* Consider an example where a template template parameter declared as
8174 template <class T, class U = std::allocator<T> > class TT
8176 The template parameter level of T and U are one level larger than
8177 of TT. To proper process the default argument of U, say when an
8178 instantiation `TT<int>' is seen, we need to build the full
8179 arguments containing {int} as the innermost level. Outer levels,
8180 available when not appearing as default template argument, can be
8181 obtained from the arguments of the enclosing template.
8183 Suppose that TT is later substituted with std::vector. The above
8184 instantiation is `TT<int, std::allocator<T> >' with TT at
8185 level 1, and T at level 2, while the template arguments at level 1
8186 becomes {std::vector} and the inner level 2 is {int}. */
8188 outer = DECL_CONTEXT (templ);
8189 if (outer)
8190 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8191 else if (current_template_parms)
8193 /* This is an argument of the current template, so we haven't set
8194 DECL_CONTEXT yet. */
8195 tree relevant_template_parms;
8197 /* Parameter levels that are greater than the level of the given
8198 template template parm are irrelevant. */
8199 relevant_template_parms = current_template_parms;
8200 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8201 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8202 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8204 outer = template_parms_to_args (relevant_template_parms);
8207 if (outer)
8208 arglist = add_to_template_args (outer, arglist);
8210 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8211 complain,
8212 /*require_all_args=*/true,
8213 /*use_default_args=*/true);
8214 if (arglist2 == error_mark_node
8215 || (!uses_template_parms (arglist2)
8216 && check_instantiated_args (templ, arglist2, complain)))
8217 return error_mark_node;
8219 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8220 return parm;
8222 else
8224 tree template_type = TREE_TYPE (templ);
8225 tree gen_tmpl;
8226 tree type_decl;
8227 tree found = NULL_TREE;
8228 int arg_depth;
8229 int parm_depth;
8230 int is_dependent_type;
8231 int use_partial_inst_tmpl = false;
8233 if (template_type == error_mark_node)
8234 /* An error occurred while building the template TEMPL, and a
8235 diagnostic has most certainly been emitted for that
8236 already. Let's propagate that error. */
8237 return error_mark_node;
8239 gen_tmpl = most_general_template (templ);
8240 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8241 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8242 arg_depth = TMPL_ARGS_DEPTH (arglist);
8244 if (arg_depth == 1 && parm_depth > 1)
8246 /* We've been given an incomplete set of template arguments.
8247 For example, given:
8249 template <class T> struct S1 {
8250 template <class U> struct S2 {};
8251 template <class U> struct S2<U*> {};
8254 we will be called with an ARGLIST of `U*', but the
8255 TEMPLATE will be `template <class T> template
8256 <class U> struct S1<T>::S2'. We must fill in the missing
8257 arguments. */
8258 arglist
8259 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8260 arglist);
8261 arg_depth = TMPL_ARGS_DEPTH (arglist);
8264 /* Now we should have enough arguments. */
8265 gcc_assert (parm_depth == arg_depth);
8267 /* From here on, we're only interested in the most general
8268 template. */
8270 /* Calculate the BOUND_ARGS. These will be the args that are
8271 actually tsubst'd into the definition to create the
8272 instantiation. */
8273 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8274 complain,
8275 /*require_all_args=*/true,
8276 /*use_default_args=*/true);
8278 if (arglist == error_mark_node)
8279 /* We were unable to bind the arguments. */
8280 return error_mark_node;
8282 /* In the scope of a template class, explicit references to the
8283 template class refer to the type of the template, not any
8284 instantiation of it. For example, in:
8286 template <class T> class C { void f(C<T>); }
8288 the `C<T>' is just the same as `C'. Outside of the
8289 class, however, such a reference is an instantiation. */
8290 if ((entering_scope
8291 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8292 || currently_open_class (template_type))
8293 /* comp_template_args is expensive, check it last. */
8294 && comp_template_args (TYPE_TI_ARGS (template_type),
8295 arglist))
8296 return template_type;
8298 /* If we already have this specialization, return it. */
8299 elt.tmpl = gen_tmpl;
8300 elt.args = arglist;
8301 elt.spec = NULL_TREE;
8302 hash = spec_hasher::hash (&elt);
8303 entry = type_specializations->find_with_hash (&elt, hash);
8305 if (entry)
8306 return entry->spec;
8308 /* If the the template's constraints are not satisfied,
8309 then we cannot form a valid type.
8311 Note that the check is deferred until after the hash
8312 lookup. This prevents redundant checks on previously
8313 instantiated specializations. */
8314 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8316 if (complain & tf_error)
8318 error ("template constraint failure");
8319 diagnose_constraints (input_location, gen_tmpl, arglist);
8321 return error_mark_node;
8324 is_dependent_type = uses_template_parms (arglist);
8326 /* If the deduced arguments are invalid, then the binding
8327 failed. */
8328 if (!is_dependent_type
8329 && check_instantiated_args (gen_tmpl,
8330 INNERMOST_TEMPLATE_ARGS (arglist),
8331 complain))
8332 return error_mark_node;
8334 if (!is_dependent_type
8335 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8336 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8337 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8339 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8340 DECL_NAME (gen_tmpl),
8341 /*tag_scope=*/ts_global);
8342 return found;
8345 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8346 complain, in_decl);
8347 if (context == error_mark_node)
8348 return error_mark_node;
8350 if (!context)
8351 context = global_namespace;
8353 /* Create the type. */
8354 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8356 /* The user referred to a specialization of an alias
8357 template represented by GEN_TMPL.
8359 [temp.alias]/2 says:
8361 When a template-id refers to the specialization of an
8362 alias template, it is equivalent to the associated
8363 type obtained by substitution of its
8364 template-arguments for the template-parameters in the
8365 type-id of the alias template. */
8367 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8368 /* Note that the call above (by indirectly calling
8369 register_specialization in tsubst_decl) registers the
8370 TYPE_DECL representing the specialization of the alias
8371 template. So next time someone substitutes ARGLIST for
8372 the template parms into the alias template (GEN_TMPL),
8373 she'll get that TYPE_DECL back. */
8375 if (t == error_mark_node)
8376 return t;
8378 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8380 if (!is_dependent_type)
8382 set_current_access_from_decl (TYPE_NAME (template_type));
8383 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8384 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8385 arglist, complain, in_decl),
8386 SCOPED_ENUM_P (template_type), NULL);
8388 if (t == error_mark_node)
8389 return t;
8391 else
8393 /* We don't want to call start_enum for this type, since
8394 the values for the enumeration constants may involve
8395 template parameters. And, no one should be interested
8396 in the enumeration constants for such a type. */
8397 t = cxx_make_type (ENUMERAL_TYPE);
8398 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8400 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8401 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8402 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8404 else if (CLASS_TYPE_P (template_type))
8406 t = make_class_type (TREE_CODE (template_type));
8407 CLASSTYPE_DECLARED_CLASS (t)
8408 = CLASSTYPE_DECLARED_CLASS (template_type);
8409 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8410 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
8412 /* A local class. Make sure the decl gets registered properly. */
8413 if (context == current_function_decl)
8414 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8416 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8417 /* This instantiation is another name for the primary
8418 template type. Set the TYPE_CANONICAL field
8419 appropriately. */
8420 TYPE_CANONICAL (t) = template_type;
8421 else if (any_template_arguments_need_structural_equality_p (arglist))
8422 /* Some of the template arguments require structural
8423 equality testing, so this template class requires
8424 structural equality testing. */
8425 SET_TYPE_STRUCTURAL_EQUALITY (t);
8427 else
8428 gcc_unreachable ();
8430 /* If we called start_enum or pushtag above, this information
8431 will already be set up. */
8432 if (!TYPE_NAME (t))
8434 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8436 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8437 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8438 DECL_SOURCE_LOCATION (type_decl)
8439 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8441 else
8442 type_decl = TYPE_NAME (t);
8444 if (CLASS_TYPE_P (template_type))
8446 TREE_PRIVATE (type_decl)
8447 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8448 TREE_PROTECTED (type_decl)
8449 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8450 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8452 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8453 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8457 if (OVERLOAD_TYPE_P (t)
8458 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8460 static const char *tags[] = {"abi_tag", "may_alias"};
8462 for (unsigned ix = 0; ix != 2; ix++)
8464 tree attributes
8465 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8467 if (!attributes)
8469 else if (!TREE_CHAIN (attributes) && !TYPE_ATTRIBUTES (t))
8470 TYPE_ATTRIBUTES (t) = attributes;
8471 else
8472 TYPE_ATTRIBUTES (t)
8473 = tree_cons (TREE_PURPOSE (attributes),
8474 TREE_VALUE (attributes),
8475 TYPE_ATTRIBUTES (t));
8479 /* Let's consider the explicit specialization of a member
8480 of a class template specialization that is implicitly instantiated,
8481 e.g.:
8482 template<class T>
8483 struct S
8485 template<class U> struct M {}; //#0
8488 template<>
8489 template<>
8490 struct S<int>::M<char> //#1
8492 int i;
8494 [temp.expl.spec]/4 says this is valid.
8496 In this case, when we write:
8497 S<int>::M<char> m;
8499 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8500 the one of #0.
8502 When we encounter #1, we want to store the partial instantiation
8503 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8505 For all cases other than this "explicit specialization of member of a
8506 class template", we just want to store the most general template into
8507 the CLASSTYPE_TI_TEMPLATE of M.
8509 This case of "explicit specialization of member of a class template"
8510 only happens when:
8511 1/ the enclosing class is an instantiation of, and therefore not
8512 the same as, the context of the most general template, and
8513 2/ we aren't looking at the partial instantiation itself, i.e.
8514 the innermost arguments are not the same as the innermost parms of
8515 the most general template.
8517 So it's only when 1/ and 2/ happens that we want to use the partial
8518 instantiation of the member template in lieu of its most general
8519 template. */
8521 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8522 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8523 /* the enclosing class must be an instantiation... */
8524 && CLASS_TYPE_P (context)
8525 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8527 tree partial_inst_args;
8528 TREE_VEC_LENGTH (arglist)--;
8529 ++processing_template_decl;
8530 partial_inst_args =
8531 tsubst (INNERMOST_TEMPLATE_ARGS
8532 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8533 arglist, complain, NULL_TREE);
8534 --processing_template_decl;
8535 TREE_VEC_LENGTH (arglist)++;
8536 use_partial_inst_tmpl =
8537 /*...and we must not be looking at the partial instantiation
8538 itself. */
8539 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8540 partial_inst_args);
8543 if (!use_partial_inst_tmpl)
8544 /* This case is easy; there are no member templates involved. */
8545 found = gen_tmpl;
8546 else
8548 /* This is a full instantiation of a member template. Find
8549 the partial instantiation of which this is an instance. */
8551 /* Temporarily reduce by one the number of levels in the ARGLIST
8552 so as to avoid comparing the last set of arguments. */
8553 TREE_VEC_LENGTH (arglist)--;
8554 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8555 TREE_VEC_LENGTH (arglist)++;
8556 /* FOUND is either a proper class type, or an alias
8557 template specialization. In the later case, it's a
8558 TYPE_DECL, resulting from the substituting of arguments
8559 for parameters in the TYPE_DECL of the alias template
8560 done earlier. So be careful while getting the template
8561 of FOUND. */
8562 found = TREE_CODE (found) == TYPE_DECL
8563 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8564 : CLASSTYPE_TI_TEMPLATE (found);
8567 // Build template info for the new specialization.
8568 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8570 elt.spec = t;
8571 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8572 entry = ggc_alloc<spec_entry> ();
8573 *entry = elt;
8574 *slot = entry;
8576 /* Note this use of the partial instantiation so we can check it
8577 later in maybe_process_partial_specialization. */
8578 DECL_TEMPLATE_INSTANTIATIONS (found)
8579 = tree_cons (arglist, t,
8580 DECL_TEMPLATE_INSTANTIATIONS (found));
8582 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8583 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8584 /* Now that the type has been registered on the instantiations
8585 list, we set up the enumerators. Because the enumeration
8586 constants may involve the enumeration type itself, we make
8587 sure to register the type first, and then create the
8588 constants. That way, doing tsubst_expr for the enumeration
8589 constants won't result in recursive calls here; we'll find
8590 the instantiation and exit above. */
8591 tsubst_enum (template_type, t, arglist);
8593 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8594 /* If the type makes use of template parameters, the
8595 code that generates debugging information will crash. */
8596 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8598 /* Possibly limit visibility based on template args. */
8599 TREE_PUBLIC (type_decl) = 1;
8600 determine_visibility (type_decl);
8602 inherit_targ_abi_tags (t);
8604 return t;
8608 /* Wrapper for lookup_template_class_1. */
8610 tree
8611 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8612 int entering_scope, tsubst_flags_t complain)
8614 tree ret;
8615 timevar_push (TV_TEMPLATE_INST);
8616 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8617 entering_scope, complain);
8618 timevar_pop (TV_TEMPLATE_INST);
8619 return ret;
8622 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8624 tree
8625 lookup_template_variable (tree templ, tree arglist)
8627 /* The type of the expression is NULL_TREE since the template-id could refer
8628 to an explicit or partial specialization. */
8629 tree type = NULL_TREE;
8630 if (flag_concepts && variable_concept_p (templ))
8631 /* Except that concepts are always bool. */
8632 type = boolean_type_node;
8633 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8636 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8638 tree
8639 finish_template_variable (tree var, tsubst_flags_t complain)
8641 tree templ = TREE_OPERAND (var, 0);
8642 tree arglist = TREE_OPERAND (var, 1);
8644 /* We never want to return a VAR_DECL for a variable concept, since they
8645 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8646 bool concept_p = flag_concepts && variable_concept_p (templ);
8647 if (concept_p && processing_template_decl)
8648 return var;
8650 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8651 arglist = add_outermost_template_args (tmpl_args, arglist);
8653 tree parms = DECL_TEMPLATE_PARMS (templ);
8654 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8655 /*req_all*/true,
8656 /*use_default*/true);
8658 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
8660 if (complain & tf_error)
8662 error ("constraints for %qD not satisfied", templ);
8663 diagnose_constraints (location_of (var), templ, arglist);
8665 return error_mark_node;
8668 /* If a template-id refers to a specialization of a variable
8669 concept, then the expression is true if and only if the
8670 concept's constraints are satisfied by the given template
8671 arguments.
8673 NOTE: This is an extension of Concepts Lite TS that
8674 allows constraints to be used in expressions. */
8675 if (concept_p)
8677 tree decl = DECL_TEMPLATE_RESULT (templ);
8678 return evaluate_variable_concept (decl, arglist);
8681 return instantiate_template (templ, arglist, complain);
8684 struct pair_fn_data
8686 tree_fn_t fn;
8687 void *data;
8688 /* True when we should also visit template parameters that occur in
8689 non-deduced contexts. */
8690 bool include_nondeduced_p;
8691 hash_set<tree> *visited;
8694 /* Called from for_each_template_parm via walk_tree. */
8696 static tree
8697 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8699 tree t = *tp;
8700 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8701 tree_fn_t fn = pfd->fn;
8702 void *data = pfd->data;
8703 tree result = NULL_TREE;
8705 #define WALK_SUBTREE(NODE) \
8706 do \
8708 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8709 pfd->include_nondeduced_p); \
8710 if (result) goto out; \
8712 while (0)
8714 if (TYPE_P (t)
8715 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
8716 WALK_SUBTREE (TYPE_CONTEXT (t));
8718 switch (TREE_CODE (t))
8720 case RECORD_TYPE:
8721 if (TYPE_PTRMEMFUNC_P (t))
8722 break;
8723 /* Fall through. */
8725 case UNION_TYPE:
8726 case ENUMERAL_TYPE:
8727 if (!TYPE_TEMPLATE_INFO (t))
8728 *walk_subtrees = 0;
8729 else
8730 WALK_SUBTREE (TYPE_TI_ARGS (t));
8731 break;
8733 case INTEGER_TYPE:
8734 WALK_SUBTREE (TYPE_MIN_VALUE (t));
8735 WALK_SUBTREE (TYPE_MAX_VALUE (t));
8736 break;
8738 case METHOD_TYPE:
8739 /* Since we're not going to walk subtrees, we have to do this
8740 explicitly here. */
8741 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
8742 /* Fall through. */
8744 case FUNCTION_TYPE:
8745 /* Check the return type. */
8746 WALK_SUBTREE (TREE_TYPE (t));
8748 /* Check the parameter types. Since default arguments are not
8749 instantiated until they are needed, the TYPE_ARG_TYPES may
8750 contain expressions that involve template parameters. But,
8751 no-one should be looking at them yet. And, once they're
8752 instantiated, they don't contain template parameters, so
8753 there's no point in looking at them then, either. */
8755 tree parm;
8757 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8758 WALK_SUBTREE (TREE_VALUE (parm));
8760 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8761 want walk_tree walking into them itself. */
8762 *walk_subtrees = 0;
8764 break;
8766 case TYPEOF_TYPE:
8767 case UNDERLYING_TYPE:
8768 if (pfd->include_nondeduced_p
8769 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8770 pfd->visited,
8771 pfd->include_nondeduced_p))
8772 return error_mark_node;
8773 break;
8775 case FUNCTION_DECL:
8776 case VAR_DECL:
8777 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8778 WALK_SUBTREE (DECL_TI_ARGS (t));
8779 /* Fall through. */
8781 case PARM_DECL:
8782 case CONST_DECL:
8783 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
8784 WALK_SUBTREE (DECL_INITIAL (t));
8785 if (DECL_CONTEXT (t)
8786 && pfd->include_nondeduced_p)
8787 WALK_SUBTREE (DECL_CONTEXT (t));
8788 break;
8790 case BOUND_TEMPLATE_TEMPLATE_PARM:
8791 /* Record template parameters such as `T' inside `TT<T>'. */
8792 WALK_SUBTREE (TYPE_TI_ARGS (t));
8793 /* Fall through. */
8795 case TEMPLATE_TEMPLATE_PARM:
8796 case TEMPLATE_TYPE_PARM:
8797 case TEMPLATE_PARM_INDEX:
8798 if (fn && (*fn)(t, data))
8799 return t;
8800 else if (!fn)
8801 return t;
8802 break;
8804 case TEMPLATE_DECL:
8805 /* A template template parameter is encountered. */
8806 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8807 WALK_SUBTREE (TREE_TYPE (t));
8809 /* Already substituted template template parameter */
8810 *walk_subtrees = 0;
8811 break;
8813 case TYPENAME_TYPE:
8814 if (!fn)
8815 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
8816 break;
8818 case CONSTRUCTOR:
8819 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8820 && pfd->include_nondeduced_p)
8821 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
8822 break;
8824 case INDIRECT_REF:
8825 case COMPONENT_REF:
8826 /* If there's no type, then this thing must be some expression
8827 involving template parameters. */
8828 if (!fn && !TREE_TYPE (t))
8829 return error_mark_node;
8830 break;
8832 case MODOP_EXPR:
8833 case CAST_EXPR:
8834 case IMPLICIT_CONV_EXPR:
8835 case REINTERPRET_CAST_EXPR:
8836 case CONST_CAST_EXPR:
8837 case STATIC_CAST_EXPR:
8838 case DYNAMIC_CAST_EXPR:
8839 case ARROW_EXPR:
8840 case DOTSTAR_EXPR:
8841 case TYPEID_EXPR:
8842 case PSEUDO_DTOR_EXPR:
8843 if (!fn)
8844 return error_mark_node;
8845 break;
8847 default:
8848 break;
8851 #undef WALK_SUBTREE
8853 /* We didn't find any template parameters we liked. */
8854 out:
8855 return result;
8858 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8859 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8860 call FN with the parameter and the DATA.
8861 If FN returns nonzero, the iteration is terminated, and
8862 for_each_template_parm returns 1. Otherwise, the iteration
8863 continues. If FN never returns a nonzero value, the value
8864 returned by for_each_template_parm is 0. If FN is NULL, it is
8865 considered to be the function which always returns 1.
8867 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8868 parameters that occur in non-deduced contexts. When false, only
8869 visits those template parameters that can be deduced. */
8871 static tree
8872 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8873 hash_set<tree> *visited,
8874 bool include_nondeduced_p)
8876 struct pair_fn_data pfd;
8877 tree result;
8879 /* Set up. */
8880 pfd.fn = fn;
8881 pfd.data = data;
8882 pfd.include_nondeduced_p = include_nondeduced_p;
8884 /* Walk the tree. (Conceptually, we would like to walk without
8885 duplicates, but for_each_template_parm_r recursively calls
8886 for_each_template_parm, so we would need to reorganize a fair
8887 bit to use walk_tree_without_duplicates, so we keep our own
8888 visited list.) */
8889 if (visited)
8890 pfd.visited = visited;
8891 else
8892 pfd.visited = new hash_set<tree>;
8893 result = cp_walk_tree (&t,
8894 for_each_template_parm_r,
8895 &pfd,
8896 pfd.visited);
8898 /* Clean up. */
8899 if (!visited)
8901 delete pfd.visited;
8902 pfd.visited = 0;
8905 return result;
8908 /* Returns true if T depends on any template parameter. */
8911 uses_template_parms (tree t)
8913 if (t == NULL_TREE)
8914 return false;
8916 bool dependent_p;
8917 int saved_processing_template_decl;
8919 saved_processing_template_decl = processing_template_decl;
8920 if (!saved_processing_template_decl)
8921 processing_template_decl = 1;
8922 if (TYPE_P (t))
8923 dependent_p = dependent_type_p (t);
8924 else if (TREE_CODE (t) == TREE_VEC)
8925 dependent_p = any_dependent_template_arguments_p (t);
8926 else if (TREE_CODE (t) == TREE_LIST)
8927 dependent_p = (uses_template_parms (TREE_VALUE (t))
8928 || uses_template_parms (TREE_CHAIN (t)));
8929 else if (TREE_CODE (t) == TYPE_DECL)
8930 dependent_p = dependent_type_p (TREE_TYPE (t));
8931 else if (DECL_P (t)
8932 || EXPR_P (t)
8933 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8934 || TREE_CODE (t) == OVERLOAD
8935 || BASELINK_P (t)
8936 || identifier_p (t)
8937 || TREE_CODE (t) == TRAIT_EXPR
8938 || TREE_CODE (t) == CONSTRUCTOR
8939 || CONSTANT_CLASS_P (t))
8940 dependent_p = (type_dependent_expression_p (t)
8941 || value_dependent_expression_p (t));
8942 else
8944 gcc_assert (t == error_mark_node);
8945 dependent_p = false;
8948 processing_template_decl = saved_processing_template_decl;
8950 return dependent_p;
8953 /* Returns true iff current_function_decl is an incompletely instantiated
8954 template. Useful instead of processing_template_decl because the latter
8955 is set to 0 during instantiate_non_dependent_expr. */
8957 bool
8958 in_template_function (void)
8960 tree fn = current_function_decl;
8961 bool ret;
8962 ++processing_template_decl;
8963 ret = (fn && DECL_LANG_SPECIFIC (fn)
8964 && DECL_TEMPLATE_INFO (fn)
8965 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8966 --processing_template_decl;
8967 return ret;
8970 /* Returns true if T depends on any template parameter with level LEVEL. */
8972 bool
8973 uses_template_parms_level (tree t, int level)
8975 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8976 /*include_nondeduced_p=*/true);
8979 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8980 ill-formed translation unit, i.e. a variable or function that isn't
8981 usable in a constant expression. */
8983 static inline bool
8984 neglectable_inst_p (tree d)
8986 return (DECL_P (d)
8987 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8988 : decl_maybe_constant_var_p (d)));
8991 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8992 neglectable and instantiated from within an erroneous instantiation. */
8994 static bool
8995 limit_bad_template_recursion (tree decl)
8997 struct tinst_level *lev = current_tinst_level;
8998 int errs = errorcount + sorrycount;
8999 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9000 return false;
9002 for (; lev; lev = lev->next)
9003 if (neglectable_inst_p (lev->decl))
9004 break;
9006 return (lev && errs > lev->errors);
9009 static int tinst_depth;
9010 extern int max_tinst_depth;
9011 int depth_reached;
9013 static GTY(()) struct tinst_level *last_error_tinst_level;
9015 /* We're starting to instantiate D; record the template instantiation context
9016 for diagnostics and to restore it later. */
9018 bool
9019 push_tinst_level (tree d)
9021 return push_tinst_level_loc (d, input_location);
9024 /* We're starting to instantiate D; record the template instantiation context
9025 at LOC for diagnostics and to restore it later. */
9027 bool
9028 push_tinst_level_loc (tree d, location_t loc)
9030 struct tinst_level *new_level;
9032 if (tinst_depth >= max_tinst_depth)
9034 fatal_error (input_location,
9035 "template instantiation depth exceeds maximum of %d"
9036 " (use -ftemplate-depth= to increase the maximum)",
9037 max_tinst_depth);
9038 return false;
9041 /* If the current instantiation caused problems, don't let it instantiate
9042 anything else. Do allow deduction substitution and decls usable in
9043 constant expressions. */
9044 if (limit_bad_template_recursion (d))
9045 return false;
9047 new_level = ggc_alloc<tinst_level> ();
9048 new_level->decl = d;
9049 new_level->locus = loc;
9050 new_level->errors = errorcount+sorrycount;
9051 new_level->in_system_header_p = in_system_header_at (input_location);
9052 new_level->next = current_tinst_level;
9053 current_tinst_level = new_level;
9055 ++tinst_depth;
9056 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9057 depth_reached = tinst_depth;
9059 return true;
9062 /* We're done instantiating this template; return to the instantiation
9063 context. */
9065 void
9066 pop_tinst_level (void)
9068 /* Restore the filename and line number stashed away when we started
9069 this instantiation. */
9070 input_location = current_tinst_level->locus;
9071 current_tinst_level = current_tinst_level->next;
9072 --tinst_depth;
9075 /* We're instantiating a deferred template; restore the template
9076 instantiation context in which the instantiation was requested, which
9077 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9079 static tree
9080 reopen_tinst_level (struct tinst_level *level)
9082 struct tinst_level *t;
9084 tinst_depth = 0;
9085 for (t = level; t; t = t->next)
9086 ++tinst_depth;
9088 current_tinst_level = level;
9089 pop_tinst_level ();
9090 if (current_tinst_level)
9091 current_tinst_level->errors = errorcount+sorrycount;
9092 return level->decl;
9095 /* Returns the TINST_LEVEL which gives the original instantiation
9096 context. */
9098 struct tinst_level *
9099 outermost_tinst_level (void)
9101 struct tinst_level *level = current_tinst_level;
9102 if (level)
9103 while (level->next)
9104 level = level->next;
9105 return level;
9108 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9109 vector of template arguments, as for tsubst.
9111 Returns an appropriate tsubst'd friend declaration. */
9113 static tree
9114 tsubst_friend_function (tree decl, tree args)
9116 tree new_friend;
9118 if (TREE_CODE (decl) == FUNCTION_DECL
9119 && DECL_TEMPLATE_INSTANTIATION (decl)
9120 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9121 /* This was a friend declared with an explicit template
9122 argument list, e.g.:
9124 friend void f<>(T);
9126 to indicate that f was a template instantiation, not a new
9127 function declaration. Now, we have to figure out what
9128 instantiation of what template. */
9130 tree template_id, arglist, fns;
9131 tree new_args;
9132 tree tmpl;
9133 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9135 /* Friend functions are looked up in the containing namespace scope.
9136 We must enter that scope, to avoid finding member functions of the
9137 current class with same name. */
9138 push_nested_namespace (ns);
9139 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9140 tf_warning_or_error, NULL_TREE,
9141 /*integral_constant_expression_p=*/false);
9142 pop_nested_namespace (ns);
9143 arglist = tsubst (DECL_TI_ARGS (decl), args,
9144 tf_warning_or_error, NULL_TREE);
9145 template_id = lookup_template_function (fns, arglist);
9147 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9148 tmpl = determine_specialization (template_id, new_friend,
9149 &new_args,
9150 /*need_member_template=*/0,
9151 TREE_VEC_LENGTH (args),
9152 tsk_none);
9153 return instantiate_template (tmpl, new_args, tf_error);
9156 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9158 /* The NEW_FRIEND will look like an instantiation, to the
9159 compiler, but is not an instantiation from the point of view of
9160 the language. For example, we might have had:
9162 template <class T> struct S {
9163 template <class U> friend void f(T, U);
9166 Then, in S<int>, template <class U> void f(int, U) is not an
9167 instantiation of anything. */
9168 if (new_friend == error_mark_node)
9169 return error_mark_node;
9171 DECL_USE_TEMPLATE (new_friend) = 0;
9172 if (TREE_CODE (decl) == TEMPLATE_DECL)
9174 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9175 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9176 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9179 /* The mangled name for the NEW_FRIEND is incorrect. The function
9180 is not a template instantiation and should not be mangled like
9181 one. Therefore, we forget the mangling here; we'll recompute it
9182 later if we need it. */
9183 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9185 SET_DECL_RTL (new_friend, NULL);
9186 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9189 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9191 tree old_decl;
9192 tree new_friend_template_info;
9193 tree new_friend_result_template_info;
9194 tree ns;
9195 int new_friend_is_defn;
9197 /* We must save some information from NEW_FRIEND before calling
9198 duplicate decls since that function will free NEW_FRIEND if
9199 possible. */
9200 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9201 new_friend_is_defn =
9202 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9203 (template_for_substitution (new_friend)))
9204 != NULL_TREE);
9205 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9207 /* This declaration is a `primary' template. */
9208 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9210 new_friend_result_template_info
9211 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9213 else
9214 new_friend_result_template_info = NULL_TREE;
9216 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9217 if (new_friend_is_defn)
9218 DECL_INITIAL (new_friend) = error_mark_node;
9220 /* Inside pushdecl_namespace_level, we will push into the
9221 current namespace. However, the friend function should go
9222 into the namespace of the template. */
9223 ns = decl_namespace_context (new_friend);
9224 push_nested_namespace (ns);
9225 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9226 pop_nested_namespace (ns);
9228 if (old_decl == error_mark_node)
9229 return error_mark_node;
9231 if (old_decl != new_friend)
9233 /* This new friend declaration matched an existing
9234 declaration. For example, given:
9236 template <class T> void f(T);
9237 template <class U> class C {
9238 template <class T> friend void f(T) {}
9241 the friend declaration actually provides the definition
9242 of `f', once C has been instantiated for some type. So,
9243 old_decl will be the out-of-class template declaration,
9244 while new_friend is the in-class definition.
9246 But, if `f' was called before this point, the
9247 instantiation of `f' will have DECL_TI_ARGS corresponding
9248 to `T' but not to `U', references to which might appear
9249 in the definition of `f'. Previously, the most general
9250 template for an instantiation of `f' was the out-of-class
9251 version; now it is the in-class version. Therefore, we
9252 run through all specialization of `f', adding to their
9253 DECL_TI_ARGS appropriately. In particular, they need a
9254 new set of outer arguments, corresponding to the
9255 arguments for this class instantiation.
9257 The same situation can arise with something like this:
9259 friend void f(int);
9260 template <class T> class C {
9261 friend void f(T) {}
9264 when `C<int>' is instantiated. Now, `f(int)' is defined
9265 in the class. */
9267 if (!new_friend_is_defn)
9268 /* On the other hand, if the in-class declaration does
9269 *not* provide a definition, then we don't want to alter
9270 existing definitions. We can just leave everything
9271 alone. */
9273 else
9275 tree new_template = TI_TEMPLATE (new_friend_template_info);
9276 tree new_args = TI_ARGS (new_friend_template_info);
9278 /* Overwrite whatever template info was there before, if
9279 any, with the new template information pertaining to
9280 the declaration. */
9281 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9283 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9285 /* We should have called reregister_specialization in
9286 duplicate_decls. */
9287 gcc_assert (retrieve_specialization (new_template,
9288 new_args, 0)
9289 == old_decl);
9291 /* Instantiate it if the global has already been used. */
9292 if (DECL_ODR_USED (old_decl))
9293 instantiate_decl (old_decl, /*defer_ok=*/true,
9294 /*expl_inst_class_mem_p=*/false);
9296 else
9298 tree t;
9300 /* Indicate that the old function template is a partial
9301 instantiation. */
9302 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9303 = new_friend_result_template_info;
9305 gcc_assert (new_template
9306 == most_general_template (new_template));
9307 gcc_assert (new_template != old_decl);
9309 /* Reassign any specializations already in the hash table
9310 to the new more general template, and add the
9311 additional template args. */
9312 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9313 t != NULL_TREE;
9314 t = TREE_CHAIN (t))
9316 tree spec = TREE_VALUE (t);
9317 spec_entry elt;
9319 elt.tmpl = old_decl;
9320 elt.args = DECL_TI_ARGS (spec);
9321 elt.spec = NULL_TREE;
9323 decl_specializations->remove_elt (&elt);
9325 DECL_TI_ARGS (spec)
9326 = add_outermost_template_args (new_args,
9327 DECL_TI_ARGS (spec));
9329 register_specialization
9330 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9333 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9337 /* The information from NEW_FRIEND has been merged into OLD_DECL
9338 by duplicate_decls. */
9339 new_friend = old_decl;
9342 else
9344 tree context = DECL_CONTEXT (new_friend);
9345 bool dependent_p;
9347 /* In the code
9348 template <class T> class C {
9349 template <class U> friend void C1<U>::f (); // case 1
9350 friend void C2<T>::f (); // case 2
9352 we only need to make sure CONTEXT is a complete type for
9353 case 2. To distinguish between the two cases, we note that
9354 CONTEXT of case 1 remains dependent type after tsubst while
9355 this isn't true for case 2. */
9356 ++processing_template_decl;
9357 dependent_p = dependent_type_p (context);
9358 --processing_template_decl;
9360 if (!dependent_p
9361 && !complete_type_or_else (context, NULL_TREE))
9362 return error_mark_node;
9364 if (COMPLETE_TYPE_P (context))
9366 tree fn = new_friend;
9367 /* do_friend adds the TEMPLATE_DECL for any member friend
9368 template even if it isn't a member template, i.e.
9369 template <class T> friend A<T>::f();
9370 Look through it in that case. */
9371 if (TREE_CODE (fn) == TEMPLATE_DECL
9372 && !PRIMARY_TEMPLATE_P (fn))
9373 fn = DECL_TEMPLATE_RESULT (fn);
9374 /* Check to see that the declaration is really present, and,
9375 possibly obtain an improved declaration. */
9376 fn = check_classfn (context, fn, NULL_TREE);
9378 if (fn)
9379 new_friend = fn;
9383 return new_friend;
9386 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9387 template arguments, as for tsubst.
9389 Returns an appropriate tsubst'd friend type or error_mark_node on
9390 failure. */
9392 static tree
9393 tsubst_friend_class (tree friend_tmpl, tree args)
9395 tree friend_type;
9396 tree tmpl;
9397 tree context;
9399 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9401 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9402 return TREE_TYPE (t);
9405 context = CP_DECL_CONTEXT (friend_tmpl);
9407 if (context != global_namespace)
9409 if (TREE_CODE (context) == NAMESPACE_DECL)
9410 push_nested_namespace (context);
9411 else
9412 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9415 /* Look for a class template declaration. We look for hidden names
9416 because two friend declarations of the same template are the
9417 same. For example, in:
9419 struct A {
9420 template <typename> friend class F;
9422 template <typename> struct B {
9423 template <typename> friend class F;
9426 both F templates are the same. */
9427 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9428 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9430 /* But, if we don't find one, it might be because we're in a
9431 situation like this:
9433 template <class T>
9434 struct S {
9435 template <class U>
9436 friend struct S;
9439 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9440 for `S<int>', not the TEMPLATE_DECL. */
9441 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9443 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9444 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9447 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9449 /* The friend template has already been declared. Just
9450 check to see that the declarations match, and install any new
9451 default parameters. We must tsubst the default parameters,
9452 of course. We only need the innermost template parameters
9453 because that is all that redeclare_class_template will look
9454 at. */
9455 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9456 > TMPL_ARGS_DEPTH (args))
9458 tree parms;
9459 location_t saved_input_location;
9460 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9461 args, tf_warning_or_error);
9463 saved_input_location = input_location;
9464 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9465 tree cons = get_constraints (tmpl);
9466 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9467 input_location = saved_input_location;
9471 friend_type = TREE_TYPE (tmpl);
9473 else
9475 /* The friend template has not already been declared. In this
9476 case, the instantiation of the template class will cause the
9477 injection of this template into the global scope. */
9478 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9479 if (tmpl == error_mark_node)
9480 return error_mark_node;
9482 /* The new TMPL is not an instantiation of anything, so we
9483 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9484 the new type because that is supposed to be the corresponding
9485 template decl, i.e., TMPL. */
9486 DECL_USE_TEMPLATE (tmpl) = 0;
9487 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9488 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9489 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9490 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9492 /* Inject this template into the global scope. */
9493 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9496 if (context != global_namespace)
9498 if (TREE_CODE (context) == NAMESPACE_DECL)
9499 pop_nested_namespace (context);
9500 else
9501 pop_nested_class ();
9504 return friend_type;
9507 /* Returns zero if TYPE cannot be completed later due to circularity.
9508 Otherwise returns one. */
9510 static int
9511 can_complete_type_without_circularity (tree type)
9513 if (type == NULL_TREE || type == error_mark_node)
9514 return 0;
9515 else if (COMPLETE_TYPE_P (type))
9516 return 1;
9517 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
9518 return can_complete_type_without_circularity (TREE_TYPE (type));
9519 else if (CLASS_TYPE_P (type)
9520 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9521 return 0;
9522 else
9523 return 1;
9526 static tree tsubst_omp_clauses (tree, bool, bool, tree, tsubst_flags_t, tree);
9528 /* Apply any attributes which had to be deferred until instantiation
9529 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9530 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9532 static void
9533 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9534 tree args, tsubst_flags_t complain, tree in_decl)
9536 tree last_dep = NULL_TREE;
9537 tree t;
9538 tree *p;
9540 for (t = attributes; t; t = TREE_CHAIN (t))
9541 if (ATTR_IS_DEPENDENT (t))
9543 last_dep = t;
9544 attributes = copy_list (attributes);
9545 break;
9548 if (DECL_P (*decl_p))
9550 if (TREE_TYPE (*decl_p) == error_mark_node)
9551 return;
9552 p = &DECL_ATTRIBUTES (*decl_p);
9554 else
9555 p = &TYPE_ATTRIBUTES (*decl_p);
9557 if (last_dep)
9559 tree late_attrs = NULL_TREE;
9560 tree *q = &late_attrs;
9562 for (*p = attributes; *p; )
9564 t = *p;
9565 if (ATTR_IS_DEPENDENT (t))
9567 *p = TREE_CHAIN (t);
9568 TREE_CHAIN (t) = NULL_TREE;
9569 if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9570 && is_attribute_p ("omp declare simd",
9571 get_attribute_name (t))
9572 && TREE_VALUE (t))
9574 tree clauses = TREE_VALUE (TREE_VALUE (t));
9575 clauses = tsubst_omp_clauses (clauses, true, false, args,
9576 complain, in_decl);
9577 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9578 clauses = finish_omp_clauses (clauses, false, true);
9579 tree parms = DECL_ARGUMENTS (*decl_p);
9580 clauses
9581 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9582 if (clauses)
9583 TREE_VALUE (TREE_VALUE (t)) = clauses;
9584 else
9585 TREE_VALUE (t) = NULL_TREE;
9587 /* If the first attribute argument is an identifier, don't
9588 pass it through tsubst. Attributes like mode, format,
9589 cleanup and several target specific attributes expect it
9590 unmodified. */
9591 else if (attribute_takes_identifier_p (get_attribute_name (t))
9592 && TREE_VALUE (t))
9594 tree chain
9595 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9596 in_decl,
9597 /*integral_constant_expression_p=*/false);
9598 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9599 TREE_VALUE (t)
9600 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9601 chain);
9603 else if (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))
9605 /* An attribute pack expansion. */
9606 tree purp = TREE_PURPOSE (t);
9607 tree pack = (tsubst_pack_expansion
9608 (TREE_VALUE (t), args, complain, in_decl));
9609 int len = TREE_VEC_LENGTH (pack);
9610 for (int i = 0; i < len; ++i)
9612 tree elt = TREE_VEC_ELT (pack, i);
9613 *q = build_tree_list (purp, elt);
9614 q = &TREE_CHAIN (*q);
9616 continue;
9618 else
9619 TREE_VALUE (t)
9620 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9621 /*integral_constant_expression_p=*/false);
9622 *q = t;
9623 q = &TREE_CHAIN (t);
9625 else
9626 p = &TREE_CHAIN (t);
9629 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9633 /* Perform (or defer) access check for typedefs that were referenced
9634 from within the template TMPL code.
9635 This is a subroutine of instantiate_decl and instantiate_class_template.
9636 TMPL is the template to consider and TARGS is the list of arguments of
9637 that template. */
9639 static void
9640 perform_typedefs_access_check (tree tmpl, tree targs)
9642 location_t saved_location;
9643 unsigned i;
9644 qualified_typedef_usage_t *iter;
9646 if (!tmpl
9647 || (!CLASS_TYPE_P (tmpl)
9648 && TREE_CODE (tmpl) != FUNCTION_DECL))
9649 return;
9651 saved_location = input_location;
9652 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9654 tree type_decl = iter->typedef_decl;
9655 tree type_scope = iter->context;
9657 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9658 continue;
9660 if (uses_template_parms (type_decl))
9661 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9662 if (uses_template_parms (type_scope))
9663 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9665 /* Make access check error messages point to the location
9666 of the use of the typedef. */
9667 input_location = iter->locus;
9668 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9669 type_decl, type_decl,
9670 tf_warning_or_error);
9672 input_location = saved_location;
9675 static tree
9676 instantiate_class_template_1 (tree type)
9678 tree templ, args, pattern, t, member;
9679 tree typedecl;
9680 tree pbinfo;
9681 tree base_list;
9682 unsigned int saved_maximum_field_alignment;
9683 tree fn_context;
9685 if (type == error_mark_node)
9686 return error_mark_node;
9688 if (COMPLETE_OR_OPEN_TYPE_P (type)
9689 || uses_template_parms (type))
9690 return type;
9692 /* Figure out which template is being instantiated. */
9693 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9694 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9696 /* Determine what specialization of the original template to
9697 instantiate. */
9698 t = most_specialized_partial_spec (type, tf_warning_or_error);
9699 if (t == error_mark_node)
9701 TYPE_BEING_DEFINED (type) = 1;
9702 return error_mark_node;
9704 else if (t)
9706 /* This TYPE is actually an instantiation of a partial
9707 specialization. We replace the innermost set of ARGS with
9708 the arguments appropriate for substitution. For example,
9709 given:
9711 template <class T> struct S {};
9712 template <class T> struct S<T*> {};
9714 and supposing that we are instantiating S<int*>, ARGS will
9715 presently be {int*} -- but we need {int}. */
9716 pattern = TREE_TYPE (t);
9717 args = TREE_PURPOSE (t);
9719 else
9721 pattern = TREE_TYPE (templ);
9722 args = CLASSTYPE_TI_ARGS (type);
9725 /* If the template we're instantiating is incomplete, then clearly
9726 there's nothing we can do. */
9727 if (!COMPLETE_TYPE_P (pattern))
9728 return type;
9730 /* If we've recursively instantiated too many templates, stop. */
9731 if (! push_tinst_level (type))
9732 return type;
9734 /* Now we're really doing the instantiation. Mark the type as in
9735 the process of being defined. */
9736 TYPE_BEING_DEFINED (type) = 1;
9738 /* We may be in the middle of deferred access check. Disable
9739 it now. */
9740 push_deferring_access_checks (dk_no_deferred);
9742 int saved_unevaluated_operand = cp_unevaluated_operand;
9743 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9745 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9746 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9747 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9748 fn_context = error_mark_node;
9749 if (!fn_context)
9750 push_to_top_level ();
9751 else
9753 cp_unevaluated_operand = 0;
9754 c_inhibit_evaluation_warnings = 0;
9756 /* Use #pragma pack from the template context. */
9757 saved_maximum_field_alignment = maximum_field_alignment;
9758 maximum_field_alignment = TYPE_PRECISION (pattern);
9760 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9762 /* Set the input location to the most specialized template definition.
9763 This is needed if tsubsting causes an error. */
9764 typedecl = TYPE_MAIN_DECL (pattern);
9765 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9766 DECL_SOURCE_LOCATION (typedecl);
9768 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9769 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9770 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9771 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9772 if (ANON_AGGR_TYPE_P (pattern))
9773 SET_ANON_AGGR_TYPE_P (type);
9774 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9776 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9777 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9778 /* Adjust visibility for template arguments. */
9779 determine_visibility (TYPE_MAIN_DECL (type));
9781 if (CLASS_TYPE_P (type))
9782 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9784 pbinfo = TYPE_BINFO (pattern);
9786 /* We should never instantiate a nested class before its enclosing
9787 class; we need to look up the nested class by name before we can
9788 instantiate it, and that lookup should instantiate the enclosing
9789 class. */
9790 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9791 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9793 base_list = NULL_TREE;
9794 if (BINFO_N_BASE_BINFOS (pbinfo))
9796 tree pbase_binfo;
9797 tree pushed_scope;
9798 int i;
9800 /* We must enter the scope containing the type, as that is where
9801 the accessibility of types named in dependent bases are
9802 looked up from. */
9803 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9805 /* Substitute into each of the bases to determine the actual
9806 basetypes. */
9807 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9809 tree base;
9810 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9811 tree expanded_bases = NULL_TREE;
9812 int idx, len = 1;
9814 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9816 expanded_bases =
9817 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9818 args, tf_error, NULL_TREE);
9819 if (expanded_bases == error_mark_node)
9820 continue;
9822 len = TREE_VEC_LENGTH (expanded_bases);
9825 for (idx = 0; idx < len; idx++)
9827 if (expanded_bases)
9828 /* Extract the already-expanded base class. */
9829 base = TREE_VEC_ELT (expanded_bases, idx);
9830 else
9831 /* Substitute to figure out the base class. */
9832 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9833 NULL_TREE);
9835 if (base == error_mark_node)
9836 continue;
9838 base_list = tree_cons (access, base, base_list);
9839 if (BINFO_VIRTUAL_P (pbase_binfo))
9840 TREE_TYPE (base_list) = integer_type_node;
9844 /* The list is now in reverse order; correct that. */
9845 base_list = nreverse (base_list);
9847 if (pushed_scope)
9848 pop_scope (pushed_scope);
9850 /* Now call xref_basetypes to set up all the base-class
9851 information. */
9852 xref_basetypes (type, base_list);
9854 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9855 (int) ATTR_FLAG_TYPE_IN_PLACE,
9856 args, tf_error, NULL_TREE);
9857 fixup_attribute_variants (type);
9859 /* Now that our base classes are set up, enter the scope of the
9860 class, so that name lookups into base classes, etc. will work
9861 correctly. This is precisely analogous to what we do in
9862 begin_class_definition when defining an ordinary non-template
9863 class, except we also need to push the enclosing classes. */
9864 push_nested_class (type);
9866 /* Now members are processed in the order of declaration. */
9867 for (member = CLASSTYPE_DECL_LIST (pattern);
9868 member; member = TREE_CHAIN (member))
9870 tree t = TREE_VALUE (member);
9872 if (TREE_PURPOSE (member))
9874 if (TYPE_P (t))
9876 /* Build new CLASSTYPE_NESTED_UTDS. */
9878 tree newtag;
9879 bool class_template_p;
9881 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9882 && TYPE_LANG_SPECIFIC (t)
9883 && CLASSTYPE_IS_TEMPLATE (t));
9884 /* If the member is a class template, then -- even after
9885 substitution -- there may be dependent types in the
9886 template argument list for the class. We increment
9887 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9888 that function will assume that no types are dependent
9889 when outside of a template. */
9890 if (class_template_p)
9891 ++processing_template_decl;
9892 newtag = tsubst (t, args, tf_error, NULL_TREE);
9893 if (class_template_p)
9894 --processing_template_decl;
9895 if (newtag == error_mark_node)
9896 continue;
9898 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9900 tree name = TYPE_IDENTIFIER (t);
9902 if (class_template_p)
9903 /* Unfortunately, lookup_template_class sets
9904 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9905 instantiation (i.e., for the type of a member
9906 template class nested within a template class.)
9907 This behavior is required for
9908 maybe_process_partial_specialization to work
9909 correctly, but is not accurate in this case;
9910 the TAG is not an instantiation of anything.
9911 (The corresponding TEMPLATE_DECL is an
9912 instantiation, but the TYPE is not.) */
9913 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9915 /* Now, we call pushtag to put this NEWTAG into the scope of
9916 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9917 pushtag calling push_template_decl. We don't have to do
9918 this for enums because it will already have been done in
9919 tsubst_enum. */
9920 if (name)
9921 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9922 pushtag (name, newtag, /*tag_scope=*/ts_current);
9925 else if (DECL_DECLARES_FUNCTION_P (t))
9927 /* Build new TYPE_METHODS. */
9928 tree r;
9930 if (TREE_CODE (t) == TEMPLATE_DECL)
9931 ++processing_template_decl;
9932 r = tsubst (t, args, tf_error, NULL_TREE);
9933 if (TREE_CODE (t) == TEMPLATE_DECL)
9934 --processing_template_decl;
9935 set_current_access_from_decl (r);
9936 finish_member_declaration (r);
9937 /* Instantiate members marked with attribute used. */
9938 if (r != error_mark_node && DECL_PRESERVE_P (r))
9939 mark_used (r);
9940 if (TREE_CODE (r) == FUNCTION_DECL
9941 && DECL_OMP_DECLARE_REDUCTION_P (r))
9942 cp_check_omp_declare_reduction (r);
9944 else if (DECL_CLASS_TEMPLATE_P (t)
9945 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9946 /* A closure type for a lambda in a default argument for a
9947 member template. Ignore it; it will be instantiated with
9948 the default argument. */;
9949 else
9951 /* Build new TYPE_FIELDS. */
9952 if (TREE_CODE (t) == STATIC_ASSERT)
9954 tree condition;
9956 ++c_inhibit_evaluation_warnings;
9957 condition =
9958 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9959 tf_warning_or_error, NULL_TREE,
9960 /*integral_constant_expression_p=*/true);
9961 --c_inhibit_evaluation_warnings;
9963 finish_static_assert (condition,
9964 STATIC_ASSERT_MESSAGE (t),
9965 STATIC_ASSERT_SOURCE_LOCATION (t),
9966 /*member_p=*/true);
9968 else if (TREE_CODE (t) != CONST_DECL)
9970 tree r;
9971 tree vec = NULL_TREE;
9972 int len = 1;
9974 /* The file and line for this declaration, to
9975 assist in error message reporting. Since we
9976 called push_tinst_level above, we don't need to
9977 restore these. */
9978 input_location = DECL_SOURCE_LOCATION (t);
9980 if (TREE_CODE (t) == TEMPLATE_DECL)
9981 ++processing_template_decl;
9982 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9983 if (TREE_CODE (t) == TEMPLATE_DECL)
9984 --processing_template_decl;
9986 if (TREE_CODE (r) == TREE_VEC)
9988 /* A capture pack became multiple fields. */
9989 vec = r;
9990 len = TREE_VEC_LENGTH (vec);
9993 for (int i = 0; i < len; ++i)
9995 if (vec)
9996 r = TREE_VEC_ELT (vec, i);
9997 if (VAR_P (r))
9999 /* In [temp.inst]:
10001 [t]he initialization (and any associated
10002 side-effects) of a static data member does
10003 not occur unless the static data member is
10004 itself used in a way that requires the
10005 definition of the static data member to
10006 exist.
10008 Therefore, we do not substitute into the
10009 initialized for the static data member here. */
10010 finish_static_data_member_decl
10012 /*init=*/NULL_TREE,
10013 /*init_const_expr_p=*/false,
10014 /*asmspec_tree=*/NULL_TREE,
10015 /*flags=*/0);
10016 /* Instantiate members marked with attribute used. */
10017 if (r != error_mark_node && DECL_PRESERVE_P (r))
10018 mark_used (r);
10020 else if (TREE_CODE (r) == FIELD_DECL)
10022 /* Determine whether R has a valid type and can be
10023 completed later. If R is invalid, then its type
10024 is replaced by error_mark_node. */
10025 tree rtype = TREE_TYPE (r);
10026 if (can_complete_type_without_circularity (rtype))
10027 complete_type (rtype);
10029 if (!COMPLETE_TYPE_P (rtype))
10031 cxx_incomplete_type_error (r, rtype);
10032 TREE_TYPE (r) = error_mark_node;
10036 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10037 such a thing will already have been added to the field
10038 list by tsubst_enum in finish_member_declaration in the
10039 CLASSTYPE_NESTED_UTDS case above. */
10040 if (!(TREE_CODE (r) == TYPE_DECL
10041 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10042 && DECL_ARTIFICIAL (r)))
10044 set_current_access_from_decl (r);
10045 finish_member_declaration (r);
10051 else
10053 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10054 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10056 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10058 tree friend_type = t;
10059 bool adjust_processing_template_decl = false;
10061 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10063 /* template <class T> friend class C; */
10064 friend_type = tsubst_friend_class (friend_type, args);
10065 adjust_processing_template_decl = true;
10067 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10069 /* template <class T> friend class C::D; */
10070 friend_type = tsubst (friend_type, args,
10071 tf_warning_or_error, NULL_TREE);
10072 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10073 friend_type = TREE_TYPE (friend_type);
10074 adjust_processing_template_decl = true;
10076 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10077 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10079 /* This could be either
10081 friend class T::C;
10083 when dependent_type_p is false or
10085 template <class U> friend class T::C;
10087 otherwise. */
10088 friend_type = tsubst (friend_type, args,
10089 tf_warning_or_error, NULL_TREE);
10090 /* Bump processing_template_decl for correct
10091 dependent_type_p calculation. */
10092 ++processing_template_decl;
10093 if (dependent_type_p (friend_type))
10094 adjust_processing_template_decl = true;
10095 --processing_template_decl;
10097 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10098 && hidden_name_p (TYPE_NAME (friend_type)))
10100 /* friend class C;
10102 where C hasn't been declared yet. Let's lookup name
10103 from namespace scope directly, bypassing any name that
10104 come from dependent base class. */
10105 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10107 /* The call to xref_tag_from_type does injection for friend
10108 classes. */
10109 push_nested_namespace (ns);
10110 friend_type =
10111 xref_tag_from_type (friend_type, NULL_TREE,
10112 /*tag_scope=*/ts_current);
10113 pop_nested_namespace (ns);
10115 else if (uses_template_parms (friend_type))
10116 /* friend class C<T>; */
10117 friend_type = tsubst (friend_type, args,
10118 tf_warning_or_error, NULL_TREE);
10119 /* Otherwise it's
10121 friend class C;
10123 where C is already declared or
10125 friend class C<int>;
10127 We don't have to do anything in these cases. */
10129 if (adjust_processing_template_decl)
10130 /* Trick make_friend_class into realizing that the friend
10131 we're adding is a template, not an ordinary class. It's
10132 important that we use make_friend_class since it will
10133 perform some error-checking and output cross-reference
10134 information. */
10135 ++processing_template_decl;
10137 if (friend_type != error_mark_node)
10138 make_friend_class (type, friend_type, /*complain=*/false);
10140 if (adjust_processing_template_decl)
10141 --processing_template_decl;
10143 else
10145 /* Build new DECL_FRIENDLIST. */
10146 tree r;
10148 /* The file and line for this declaration, to
10149 assist in error message reporting. Since we
10150 called push_tinst_level above, we don't need to
10151 restore these. */
10152 input_location = DECL_SOURCE_LOCATION (t);
10154 if (TREE_CODE (t) == TEMPLATE_DECL)
10156 ++processing_template_decl;
10157 push_deferring_access_checks (dk_no_check);
10160 r = tsubst_friend_function (t, args);
10161 add_friend (type, r, /*complain=*/false);
10162 if (TREE_CODE (t) == TEMPLATE_DECL)
10164 pop_deferring_access_checks ();
10165 --processing_template_decl;
10171 if (fn_context)
10173 /* Restore these before substituting into the lambda capture
10174 initializers. */
10175 cp_unevaluated_operand = saved_unevaluated_operand;
10176 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10179 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10181 tree decl = lambda_function (type);
10182 if (decl)
10184 if (!DECL_TEMPLATE_INFO (decl)
10185 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10187 /* Set function_depth to avoid garbage collection. */
10188 ++function_depth;
10189 instantiate_decl (decl, false, false);
10190 --function_depth;
10193 /* We need to instantiate the capture list from the template
10194 after we've instantiated the closure members, but before we
10195 consider adding the conversion op. Also keep any captures
10196 that may have been added during instantiation of the op(). */
10197 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10198 tree tmpl_cap
10199 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10200 args, tf_warning_or_error, NULL_TREE,
10201 false, false);
10203 LAMBDA_EXPR_CAPTURE_LIST (expr)
10204 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10206 maybe_add_lambda_conv_op (type);
10208 else
10209 gcc_assert (errorcount);
10212 /* Set the file and line number information to whatever is given for
10213 the class itself. This puts error messages involving generated
10214 implicit functions at a predictable point, and the same point
10215 that would be used for non-template classes. */
10216 input_location = DECL_SOURCE_LOCATION (typedecl);
10218 unreverse_member_declarations (type);
10219 finish_struct_1 (type);
10220 TYPE_BEING_DEFINED (type) = 0;
10222 /* We don't instantiate default arguments for member functions. 14.7.1:
10224 The implicit instantiation of a class template specialization causes
10225 the implicit instantiation of the declarations, but not of the
10226 definitions or default arguments, of the class member functions,
10227 member classes, static data members and member templates.... */
10229 /* Some typedefs referenced from within the template code need to be access
10230 checked at template instantiation time, i.e now. These types were
10231 added to the template at parsing time. Let's get those and perform
10232 the access checks then. */
10233 perform_typedefs_access_check (pattern, args);
10234 perform_deferred_access_checks (tf_warning_or_error);
10235 pop_nested_class ();
10236 maximum_field_alignment = saved_maximum_field_alignment;
10237 if (!fn_context)
10238 pop_from_top_level ();
10239 pop_deferring_access_checks ();
10240 pop_tinst_level ();
10242 /* The vtable for a template class can be emitted in any translation
10243 unit in which the class is instantiated. When there is no key
10244 method, however, finish_struct_1 will already have added TYPE to
10245 the keyed_classes list. */
10246 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10247 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10249 return type;
10252 /* Wrapper for instantiate_class_template_1. */
10254 tree
10255 instantiate_class_template (tree type)
10257 tree ret;
10258 timevar_push (TV_TEMPLATE_INST);
10259 ret = instantiate_class_template_1 (type);
10260 timevar_pop (TV_TEMPLATE_INST);
10261 return ret;
10264 static tree
10265 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10267 tree r;
10269 if (!t)
10270 r = t;
10271 else if (TYPE_P (t))
10272 r = tsubst (t, args, complain, in_decl);
10273 else
10275 if (!(complain & tf_warning))
10276 ++c_inhibit_evaluation_warnings;
10277 r = tsubst_expr (t, args, complain, in_decl,
10278 /*integral_constant_expression_p=*/true);
10279 if (!(complain & tf_warning))
10280 --c_inhibit_evaluation_warnings;
10282 return r;
10285 /* Given a function parameter pack TMPL_PARM and some function parameters
10286 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10287 and set *SPEC_P to point at the next point in the list. */
10289 tree
10290 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10292 /* Collect all of the extra "packed" parameters into an
10293 argument pack. */
10294 tree parmvec;
10295 tree parmtypevec;
10296 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10297 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10298 tree spec_parm = *spec_p;
10299 int i, len;
10301 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10302 if (tmpl_parm
10303 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10304 break;
10306 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10307 parmvec = make_tree_vec (len);
10308 parmtypevec = make_tree_vec (len);
10309 spec_parm = *spec_p;
10310 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10312 TREE_VEC_ELT (parmvec, i) = spec_parm;
10313 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10316 /* Build the argument packs. */
10317 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10318 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10319 TREE_TYPE (argpack) = argtypepack;
10320 *spec_p = spec_parm;
10322 return argpack;
10325 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10326 NONTYPE_ARGUMENT_PACK. */
10328 static tree
10329 make_fnparm_pack (tree spec_parm)
10331 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10334 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10335 pack expansion with no extra args, 2 if it has extra args, or 0
10336 if it is not a pack expansion. */
10338 static int
10339 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10341 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10342 if (i >= TREE_VEC_LENGTH (vec))
10343 return 0;
10344 tree elt = TREE_VEC_ELT (vec, i);
10345 if (DECL_P (elt))
10346 /* A decl pack is itself an expansion. */
10347 elt = TREE_TYPE (elt);
10348 if (!PACK_EXPANSION_P (elt))
10349 return 0;
10350 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10351 return 2;
10352 return 1;
10356 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10358 static tree
10359 make_argument_pack_select (tree arg_pack, unsigned index)
10361 tree aps = make_node (ARGUMENT_PACK_SELECT);
10363 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10364 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10366 return aps;
10369 /* This is a subroutine of tsubst_pack_expansion.
10371 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10372 mechanism to store the (non complete list of) arguments of the
10373 substitution and return a non substituted pack expansion, in order
10374 to wait for when we have enough arguments to really perform the
10375 substitution. */
10377 static bool
10378 use_pack_expansion_extra_args_p (tree parm_packs,
10379 int arg_pack_len,
10380 bool has_empty_arg)
10382 /* If one pack has an expansion and another pack has a normal
10383 argument or if one pack has an empty argument and an another
10384 one hasn't then tsubst_pack_expansion cannot perform the
10385 substitution and need to fall back on the
10386 PACK_EXPANSION_EXTRA mechanism. */
10387 if (parm_packs == NULL_TREE)
10388 return false;
10389 else if (has_empty_arg)
10390 return true;
10392 bool has_expansion_arg = false;
10393 for (int i = 0 ; i < arg_pack_len; ++i)
10395 bool has_non_expansion_arg = false;
10396 for (tree parm_pack = parm_packs;
10397 parm_pack;
10398 parm_pack = TREE_CHAIN (parm_pack))
10400 tree arg = TREE_VALUE (parm_pack);
10402 int exp = argument_pack_element_is_expansion_p (arg, i);
10403 if (exp == 2)
10404 /* We can't substitute a pack expansion with extra args into
10405 our pattern. */
10406 return true;
10407 else if (exp)
10408 has_expansion_arg = true;
10409 else
10410 has_non_expansion_arg = true;
10413 if (has_expansion_arg && has_non_expansion_arg)
10414 return true;
10416 return false;
10419 /* [temp.variadic]/6 says that:
10421 The instantiation of a pack expansion [...]
10422 produces a list E1,E2, ..., En, where N is the number of elements
10423 in the pack expansion parameters.
10425 This subroutine of tsubst_pack_expansion produces one of these Ei.
10427 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10428 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10429 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10430 INDEX is the index 'i' of the element Ei to produce. ARGS,
10431 COMPLAIN, and IN_DECL are the same parameters as for the
10432 tsubst_pack_expansion function.
10434 The function returns the resulting Ei upon successful completion,
10435 or error_mark_node.
10437 Note that this function possibly modifies the ARGS parameter, so
10438 it's the responsibility of the caller to restore it. */
10440 static tree
10441 gen_elem_of_pack_expansion_instantiation (tree pattern,
10442 tree parm_packs,
10443 unsigned index,
10444 tree args /* This parm gets
10445 modified. */,
10446 tsubst_flags_t complain,
10447 tree in_decl)
10449 tree t;
10450 bool ith_elem_is_expansion = false;
10452 /* For each parameter pack, change the substitution of the parameter
10453 pack to the ith argument in its argument pack, then expand the
10454 pattern. */
10455 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10457 tree parm = TREE_PURPOSE (pack);
10458 tree arg_pack = TREE_VALUE (pack);
10459 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10461 ith_elem_is_expansion |=
10462 argument_pack_element_is_expansion_p (arg_pack, index);
10464 /* Select the Ith argument from the pack. */
10465 if (TREE_CODE (parm) == PARM_DECL
10466 || TREE_CODE (parm) == FIELD_DECL)
10468 if (index == 0)
10470 aps = make_argument_pack_select (arg_pack, index);
10471 if (!mark_used (parm, complain) && !(complain & tf_error))
10472 return error_mark_node;
10473 register_local_specialization (aps, parm);
10475 else
10476 aps = retrieve_local_specialization (parm);
10478 else
10480 int idx, level;
10481 template_parm_level_and_index (parm, &level, &idx);
10483 if (index == 0)
10485 aps = make_argument_pack_select (arg_pack, index);
10486 /* Update the corresponding argument. */
10487 TMPL_ARG (args, level, idx) = aps;
10489 else
10490 /* Re-use the ARGUMENT_PACK_SELECT. */
10491 aps = TMPL_ARG (args, level, idx);
10493 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10496 /* Substitute into the PATTERN with the (possibly altered)
10497 arguments. */
10498 if (pattern == in_decl)
10499 /* Expanding a fixed parameter pack from
10500 coerce_template_parameter_pack. */
10501 t = tsubst_decl (pattern, args, complain);
10502 else if (pattern == error_mark_node)
10503 t = error_mark_node;
10504 else if (constraint_p (pattern))
10506 if (processing_template_decl)
10507 t = tsubst_constraint (pattern, args, complain, in_decl);
10508 else
10509 t = (constraints_satisfied_p (pattern, args)
10510 ? boolean_true_node : boolean_false_node);
10512 else if (!TYPE_P (pattern))
10513 t = tsubst_expr (pattern, args, complain, in_decl,
10514 /*integral_constant_expression_p=*/false);
10515 else
10516 t = tsubst (pattern, args, complain, in_decl);
10518 /* If the Ith argument pack element is a pack expansion, then
10519 the Ith element resulting from the substituting is going to
10520 be a pack expansion as well. */
10521 if (ith_elem_is_expansion)
10522 t = make_pack_expansion (t);
10524 return t;
10527 /* When the unexpanded parameter pack in a fold expression expands to an empty
10528 sequence, the value of the expression is as follows; the program is
10529 ill-formed if the operator is not listed in this table.
10533 & -1
10535 && true
10536 || false
10537 , void() */
10539 tree
10540 expand_empty_fold (tree t, tsubst_flags_t complain)
10542 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
10543 if (!FOLD_EXPR_MODIFY_P (t))
10544 switch (code)
10546 case MULT_EXPR:
10547 return integer_one_node;
10548 case PLUS_EXPR:
10549 return integer_zero_node;
10550 case BIT_AND_EXPR:
10551 return integer_minus_one_node;
10552 case BIT_IOR_EXPR:
10553 return integer_zero_node;
10554 case TRUTH_ANDIF_EXPR:
10555 return boolean_true_node;
10556 case TRUTH_ORIF_EXPR:
10557 return boolean_false_node;
10558 case COMPOUND_EXPR:
10559 return void_node;
10560 default:
10561 break;
10564 if (complain & tf_error)
10565 error_at (location_of (t),
10566 "fold of empty expansion over %O", code);
10567 return error_mark_node;
10570 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10571 form an expression that combines the two terms using the
10572 operator of T. */
10574 static tree
10575 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
10577 tree op = FOLD_EXPR_OP (t);
10578 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
10580 // Handle compound assignment operators.
10581 if (FOLD_EXPR_MODIFY_P (t))
10582 return build_x_modify_expr (input_location, left, code, right, complain);
10584 switch (code)
10586 case COMPOUND_EXPR:
10587 return build_x_compound_expr (input_location, left, right, complain);
10588 case DOTSTAR_EXPR:
10589 return build_m_component_ref (left, right, complain);
10590 default:
10591 return build_x_binary_op (input_location, code,
10592 left, TREE_CODE (left),
10593 right, TREE_CODE (right),
10594 /*overload=*/NULL,
10595 complain);
10599 /* Substitute ARGS into the pack of a fold expression T. */
10601 static inline tree
10602 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10604 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
10607 /* Substitute ARGS into the pack of a fold expression T. */
10609 static inline tree
10610 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10612 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
10615 /* Expand a PACK of arguments into a grouped as left fold.
10616 Given a pack containing elements A0, A1, ..., An and an
10617 operator @, this builds the expression:
10619 ((A0 @ A1) @ A2) ... @ An
10621 Note that PACK must not be empty.
10623 The operator is defined by the original fold expression T. */
10625 static tree
10626 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
10628 tree left = TREE_VEC_ELT (pack, 0);
10629 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
10631 tree right = TREE_VEC_ELT (pack, i);
10632 left = fold_expression (t, left, right, complain);
10634 return left;
10637 /* Substitute into a unary left fold expression. */
10639 static tree
10640 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
10641 tree in_decl)
10643 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10644 if (pack == error_mark_node)
10645 return error_mark_node;
10646 if (TREE_VEC_LENGTH (pack) == 0)
10647 return expand_empty_fold (t, complain);
10648 else
10649 return expand_left_fold (t, pack, complain);
10652 /* Substitute into a binary left fold expression.
10654 Do ths by building a single (non-empty) vector of argumnts and
10655 building the expression from those elements. */
10657 static tree
10658 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
10659 tree in_decl)
10661 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10662 if (pack == error_mark_node)
10663 return error_mark_node;
10664 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10665 if (init == error_mark_node)
10666 return error_mark_node;
10668 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
10669 TREE_VEC_ELT (vec, 0) = init;
10670 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
10671 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
10673 return expand_left_fold (t, vec, complain);
10676 /* Expand a PACK of arguments into a grouped as right fold.
10677 Given a pack containing elementns A0, A1, ..., and an
10678 operator @, this builds the expression:
10680 A0@ ... (An-2 @ (An-1 @ An))
10682 Note that PACK must not be empty.
10684 The operator is defined by the original fold expression T. */
10686 tree
10687 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
10689 // Build the expression.
10690 int n = TREE_VEC_LENGTH (pack);
10691 tree right = TREE_VEC_ELT (pack, n - 1);
10692 for (--n; n != 0; --n)
10694 tree left = TREE_VEC_ELT (pack, n - 1);
10695 right = fold_expression (t, left, right, complain);
10697 return right;
10700 /* Substitute into a unary right fold expression. */
10702 static tree
10703 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
10704 tree in_decl)
10706 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10707 if (pack == error_mark_node)
10708 return error_mark_node;
10709 if (TREE_VEC_LENGTH (pack) == 0)
10710 return expand_empty_fold (t, complain);
10711 else
10712 return expand_right_fold (t, pack, complain);
10715 /* Substitute into a binary right fold expression.
10717 Do ths by building a single (non-empty) vector of arguments and
10718 building the expression from those elements. */
10720 static tree
10721 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
10722 tree in_decl)
10724 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10725 if (pack == error_mark_node)
10726 return error_mark_node;
10727 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10728 if (init == error_mark_node)
10729 return error_mark_node;
10731 int n = TREE_VEC_LENGTH (pack);
10732 tree vec = make_tree_vec (n + 1);
10733 for (int i = 0; i < n; ++i)
10734 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
10735 TREE_VEC_ELT (vec, n) = init;
10737 return expand_right_fold (t, vec, complain);
10741 /* Substitute ARGS into T, which is an pack expansion
10742 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10743 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10744 (if only a partial substitution could be performed) or
10745 ERROR_MARK_NODE if there was an error. */
10746 tree
10747 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10748 tree in_decl)
10750 tree pattern;
10751 tree pack, packs = NULL_TREE;
10752 bool unsubstituted_packs = false;
10753 int i, len = -1;
10754 tree result;
10755 hash_map<tree, tree> *saved_local_specializations = NULL;
10756 bool need_local_specializations = false;
10757 int levels;
10759 gcc_assert (PACK_EXPANSION_P (t));
10760 pattern = PACK_EXPANSION_PATTERN (t);
10762 /* Add in any args remembered from an earlier partial instantiation. */
10763 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10765 levels = TMPL_ARGS_DEPTH (args);
10767 /* Determine the argument packs that will instantiate the parameter
10768 packs used in the expansion expression. While we're at it,
10769 compute the number of arguments to be expanded and make sure it
10770 is consistent. */
10771 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10772 pack = TREE_CHAIN (pack))
10774 tree parm_pack = TREE_VALUE (pack);
10775 tree arg_pack = NULL_TREE;
10776 tree orig_arg = NULL_TREE;
10777 int level = 0;
10779 if (TREE_CODE (parm_pack) == BASES)
10781 if (BASES_DIRECT (parm_pack))
10782 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10783 args, complain, in_decl, false));
10784 else
10785 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10786 args, complain, in_decl, false));
10788 if (TREE_CODE (parm_pack) == PARM_DECL)
10790 /* We know we have correct local_specializations if this
10791 expansion is at function scope, or if we're dealing with a
10792 local parameter in a requires expression; for the latter,
10793 tsubst_requires_expr set it up appropriately. */
10794 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
10795 arg_pack = retrieve_local_specialization (parm_pack);
10796 else
10798 /* We can't rely on local_specializations for a parameter
10799 name used later in a function declaration (such as in a
10800 late-specified return type). Even if it exists, it might
10801 have the wrong value for a recursive call. Just make a
10802 dummy decl, since it's only used for its type. */
10803 arg_pack = tsubst_decl (parm_pack, args, complain);
10804 if (arg_pack && DECL_PACK_P (arg_pack))
10805 /* Partial instantiation of the parm_pack, we can't build
10806 up an argument pack yet. */
10807 arg_pack = NULL_TREE;
10808 else
10809 arg_pack = make_fnparm_pack (arg_pack);
10810 need_local_specializations = true;
10813 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10814 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10815 else
10817 int idx;
10818 template_parm_level_and_index (parm_pack, &level, &idx);
10820 if (level <= levels)
10821 arg_pack = TMPL_ARG (args, level, idx);
10824 orig_arg = arg_pack;
10825 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
10826 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
10828 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
10829 /* This can only happen if we forget to expand an argument
10830 pack somewhere else. Just return an error, silently. */
10832 result = make_tree_vec (1);
10833 TREE_VEC_ELT (result, 0) = error_mark_node;
10834 return result;
10837 if (arg_pack)
10839 int my_len =
10840 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10842 /* Don't bother trying to do a partial substitution with
10843 incomplete packs; we'll try again after deduction. */
10844 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10845 return t;
10847 if (len < 0)
10848 len = my_len;
10849 else if (len != my_len)
10851 if (!(complain & tf_error))
10852 /* Fail quietly. */;
10853 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10854 error ("mismatched argument pack lengths while expanding "
10855 "%<%T%>",
10856 pattern);
10857 else
10858 error ("mismatched argument pack lengths while expanding "
10859 "%<%E%>",
10860 pattern);
10861 return error_mark_node;
10864 /* Keep track of the parameter packs and their corresponding
10865 argument packs. */
10866 packs = tree_cons (parm_pack, arg_pack, packs);
10867 TREE_TYPE (packs) = orig_arg;
10869 else
10871 /* We can't substitute for this parameter pack. We use a flag as
10872 well as the missing_level counter because function parameter
10873 packs don't have a level. */
10874 unsubstituted_packs = true;
10878 /* If the expansion is just T..., return the matching argument pack, unless
10879 we need to call convert_from_reference on all the elements. This is an
10880 important optimization; see c++/68422. */
10881 if (!unsubstituted_packs
10882 && TREE_PURPOSE (packs) == pattern)
10884 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10885 /* Types need no adjustment, nor does sizeof..., and if we still have
10886 some pack expansion args we won't do anything yet. */
10887 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10888 || PACK_EXPANSION_SIZEOF_P (t)
10889 || pack_expansion_args_count (args))
10890 return args;
10891 /* Also optimize expression pack expansions if we can tell that the
10892 elements won't have reference type. */
10893 tree type = TREE_TYPE (pattern);
10894 if (type && TREE_CODE (type) != REFERENCE_TYPE
10895 && !PACK_EXPANSION_P (type)
10896 && !WILDCARD_TYPE_P (type))
10897 return args;
10898 /* Otherwise use the normal path so we get convert_from_reference. */
10901 /* We cannot expand this expansion expression, because we don't have
10902 all of the argument packs we need. */
10903 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10905 /* We got some full packs, but we can't substitute them in until we
10906 have values for all the packs. So remember these until then. */
10908 t = make_pack_expansion (pattern);
10909 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10910 return t;
10912 else if (unsubstituted_packs)
10914 /* There were no real arguments, we're just replacing a parameter
10915 pack with another version of itself. Substitute into the
10916 pattern and return a PACK_EXPANSION_*. The caller will need to
10917 deal with that. */
10918 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10919 t = tsubst_expr (pattern, args, complain, in_decl,
10920 /*integral_constant_expression_p=*/false);
10921 else
10922 t = tsubst (pattern, args, complain, in_decl);
10923 t = make_pack_expansion (t);
10924 return t;
10927 gcc_assert (len >= 0);
10929 if (need_local_specializations)
10931 /* We're in a late-specified return type, so create our own local
10932 specializations map; the current map is either NULL or (in the
10933 case of recursive unification) might have bindings that we don't
10934 want to use or alter. */
10935 saved_local_specializations = local_specializations;
10936 local_specializations = new hash_map<tree, tree>;
10939 /* For each argument in each argument pack, substitute into the
10940 pattern. */
10941 result = make_tree_vec (len);
10942 for (i = 0; i < len; ++i)
10944 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10946 args, complain,
10947 in_decl);
10948 TREE_VEC_ELT (result, i) = t;
10949 if (t == error_mark_node)
10951 result = error_mark_node;
10952 break;
10956 /* Update ARGS to restore the substitution from parameter packs to
10957 their argument packs. */
10958 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10960 tree parm = TREE_PURPOSE (pack);
10962 if (TREE_CODE (parm) == PARM_DECL
10963 || TREE_CODE (parm) == FIELD_DECL)
10964 register_local_specialization (TREE_TYPE (pack), parm);
10965 else
10967 int idx, level;
10969 if (TREE_VALUE (pack) == NULL_TREE)
10970 continue;
10972 template_parm_level_and_index (parm, &level, &idx);
10974 /* Update the corresponding argument. */
10975 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10976 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10977 TREE_TYPE (pack);
10978 else
10979 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10983 if (need_local_specializations)
10985 delete local_specializations;
10986 local_specializations = saved_local_specializations;
10989 return result;
10992 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10993 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10994 parameter packs; all parms generated from a function parameter pack will
10995 have the same DECL_PARM_INDEX. */
10997 tree
10998 get_pattern_parm (tree parm, tree tmpl)
11000 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11001 tree patparm;
11003 if (DECL_ARTIFICIAL (parm))
11005 for (patparm = DECL_ARGUMENTS (pattern);
11006 patparm; patparm = DECL_CHAIN (patparm))
11007 if (DECL_ARTIFICIAL (patparm)
11008 && DECL_NAME (parm) == DECL_NAME (patparm))
11009 break;
11011 else
11013 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11014 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11015 gcc_assert (DECL_PARM_INDEX (patparm)
11016 == DECL_PARM_INDEX (parm));
11019 return patparm;
11022 /* Substitute ARGS into the vector or list of template arguments T. */
11024 static tree
11025 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11027 tree orig_t = t;
11028 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11029 tree *elts;
11031 if (t == error_mark_node)
11032 return error_mark_node;
11034 len = TREE_VEC_LENGTH (t);
11035 elts = XALLOCAVEC (tree, len);
11037 for (i = 0; i < len; i++)
11039 tree orig_arg = TREE_VEC_ELT (t, i);
11040 tree new_arg;
11042 if (TREE_CODE (orig_arg) == TREE_VEC)
11043 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11044 else if (PACK_EXPANSION_P (orig_arg))
11046 /* Substitute into an expansion expression. */
11047 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11049 if (TREE_CODE (new_arg) == TREE_VEC)
11050 /* Add to the expanded length adjustment the number of
11051 expanded arguments. We subtract one from this
11052 measurement, because the argument pack expression
11053 itself is already counted as 1 in
11054 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11055 the argument pack is empty. */
11056 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11058 else if (ARGUMENT_PACK_P (orig_arg))
11060 /* Substitute into each of the arguments. */
11061 new_arg = TYPE_P (orig_arg)
11062 ? cxx_make_type (TREE_CODE (orig_arg))
11063 : make_node (TREE_CODE (orig_arg));
11065 SET_ARGUMENT_PACK_ARGS (
11066 new_arg,
11067 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11068 args, complain, in_decl));
11070 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11071 new_arg = error_mark_node;
11073 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11074 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11075 complain, in_decl);
11076 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11078 if (TREE_TYPE (new_arg) == error_mark_node)
11079 new_arg = error_mark_node;
11082 else
11083 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11085 if (new_arg == error_mark_node)
11086 return error_mark_node;
11088 elts[i] = new_arg;
11089 if (new_arg != orig_arg)
11090 need_new = 1;
11093 if (!need_new)
11094 return t;
11096 /* Make space for the expanded arguments coming from template
11097 argument packs. */
11098 t = make_tree_vec (len + expanded_len_adjust);
11099 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11100 arguments for a member template.
11101 In that case each TREE_VEC in ORIG_T represents a level of template
11102 arguments, and ORIG_T won't carry any non defaulted argument count.
11103 It will rather be the nested TREE_VECs that will carry one.
11104 In other words, ORIG_T carries a non defaulted argument count only
11105 if it doesn't contain any nested TREE_VEC. */
11106 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11108 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11109 count += expanded_len_adjust;
11110 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11112 for (i = 0, out = 0; i < len; i++)
11114 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11115 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11116 && TREE_CODE (elts[i]) == TREE_VEC)
11118 int idx;
11120 /* Now expand the template argument pack "in place". */
11121 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11122 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11124 else
11126 TREE_VEC_ELT (t, out) = elts[i];
11127 out++;
11131 return t;
11134 /* Return the result of substituting ARGS into the template parameters
11135 given by PARMS. If there are m levels of ARGS and m + n levels of
11136 PARMS, then the result will contain n levels of PARMS. For
11137 example, if PARMS is `template <class T> template <class U>
11138 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11139 result will be `template <int*, double, class V>'. */
11141 static tree
11142 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11144 tree r = NULL_TREE;
11145 tree* new_parms;
11147 /* When substituting into a template, we must set
11148 PROCESSING_TEMPLATE_DECL as the template parameters may be
11149 dependent if they are based on one-another, and the dependency
11150 predicates are short-circuit outside of templates. */
11151 ++processing_template_decl;
11153 for (new_parms = &r;
11154 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11155 new_parms = &(TREE_CHAIN (*new_parms)),
11156 parms = TREE_CHAIN (parms))
11158 tree new_vec =
11159 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
11160 int i;
11162 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11164 tree tuple;
11166 if (parms == error_mark_node)
11167 continue;
11169 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
11171 if (tuple == error_mark_node)
11172 continue;
11174 TREE_VEC_ELT (new_vec, i) =
11175 tsubst_template_parm (tuple, args, complain);
11178 *new_parms =
11179 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11180 - TMPL_ARGS_DEPTH (args)),
11181 new_vec, NULL_TREE);
11184 --processing_template_decl;
11186 return r;
11189 /* Return the result of substituting ARGS into one template parameter
11190 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11191 parameter and which TREE_PURPOSE is the default argument of the
11192 template parameter. */
11194 static tree
11195 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11197 tree default_value, parm_decl;
11199 if (args == NULL_TREE
11200 || t == NULL_TREE
11201 || t == error_mark_node)
11202 return t;
11204 gcc_assert (TREE_CODE (t) == TREE_LIST);
11206 default_value = TREE_PURPOSE (t);
11207 parm_decl = TREE_VALUE (t);
11209 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11210 if (TREE_CODE (parm_decl) == PARM_DECL
11211 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11212 parm_decl = error_mark_node;
11213 default_value = tsubst_template_arg (default_value, args,
11214 complain, NULL_TREE);
11216 return build_tree_list (default_value, parm_decl);
11219 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11220 type T. If T is not an aggregate or enumeration type, it is
11221 handled as if by tsubst. IN_DECL is as for tsubst. If
11222 ENTERING_SCOPE is nonzero, T is the context for a template which
11223 we are presently tsubst'ing. Return the substituted value. */
11225 static tree
11226 tsubst_aggr_type (tree t,
11227 tree args,
11228 tsubst_flags_t complain,
11229 tree in_decl,
11230 int entering_scope)
11232 if (t == NULL_TREE)
11233 return NULL_TREE;
11235 switch (TREE_CODE (t))
11237 case RECORD_TYPE:
11238 if (TYPE_PTRMEMFUNC_P (t))
11239 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11241 /* Else fall through. */
11242 case ENUMERAL_TYPE:
11243 case UNION_TYPE:
11244 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11246 tree argvec;
11247 tree context;
11248 tree r;
11249 int saved_unevaluated_operand;
11250 int saved_inhibit_evaluation_warnings;
11252 /* In "sizeof(X<I>)" we need to evaluate "I". */
11253 saved_unevaluated_operand = cp_unevaluated_operand;
11254 cp_unevaluated_operand = 0;
11255 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11256 c_inhibit_evaluation_warnings = 0;
11258 /* First, determine the context for the type we are looking
11259 up. */
11260 context = TYPE_CONTEXT (t);
11261 if (context && TYPE_P (context))
11263 context = tsubst_aggr_type (context, args, complain,
11264 in_decl, /*entering_scope=*/1);
11265 /* If context is a nested class inside a class template,
11266 it may still need to be instantiated (c++/33959). */
11267 context = complete_type (context);
11270 /* Then, figure out what arguments are appropriate for the
11271 type we are trying to find. For example, given:
11273 template <class T> struct S;
11274 template <class T, class U> void f(T, U) { S<U> su; }
11276 and supposing that we are instantiating f<int, double>,
11277 then our ARGS will be {int, double}, but, when looking up
11278 S we only want {double}. */
11279 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11280 complain, in_decl);
11281 if (argvec == error_mark_node)
11282 r = error_mark_node;
11283 else
11285 r = lookup_template_class (t, argvec, in_decl, context,
11286 entering_scope, complain);
11287 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11290 cp_unevaluated_operand = saved_unevaluated_operand;
11291 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11293 return r;
11295 else
11296 /* This is not a template type, so there's nothing to do. */
11297 return t;
11299 default:
11300 return tsubst (t, args, complain, in_decl);
11304 /* Substitute into the default argument ARG (a default argument for
11305 FN), which has the indicated TYPE. */
11307 tree
11308 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11310 tree saved_class_ptr = NULL_TREE;
11311 tree saved_class_ref = NULL_TREE;
11312 int errs = errorcount + sorrycount;
11314 /* This can happen in invalid code. */
11315 if (TREE_CODE (arg) == DEFAULT_ARG)
11316 return arg;
11318 /* This default argument came from a template. Instantiate the
11319 default argument here, not in tsubst. In the case of
11320 something like:
11322 template <class T>
11323 struct S {
11324 static T t();
11325 void f(T = t());
11328 we must be careful to do name lookup in the scope of S<T>,
11329 rather than in the current class. */
11330 push_access_scope (fn);
11331 /* The "this" pointer is not valid in a default argument. */
11332 if (cfun)
11334 saved_class_ptr = current_class_ptr;
11335 cp_function_chain->x_current_class_ptr = NULL_TREE;
11336 saved_class_ref = current_class_ref;
11337 cp_function_chain->x_current_class_ref = NULL_TREE;
11340 push_deferring_access_checks(dk_no_deferred);
11341 /* The default argument expression may cause implicitly defined
11342 member functions to be synthesized, which will result in garbage
11343 collection. We must treat this situation as if we were within
11344 the body of function so as to avoid collecting live data on the
11345 stack. */
11346 ++function_depth;
11347 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11348 complain, NULL_TREE,
11349 /*integral_constant_expression_p=*/false);
11350 --function_depth;
11351 pop_deferring_access_checks();
11353 /* Restore the "this" pointer. */
11354 if (cfun)
11356 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11357 cp_function_chain->x_current_class_ref = saved_class_ref;
11360 if (errorcount+sorrycount > errs
11361 && (complain & tf_warning_or_error))
11362 inform (input_location,
11363 " when instantiating default argument for call to %D", fn);
11365 /* Make sure the default argument is reasonable. */
11366 arg = check_default_argument (type, arg, complain);
11368 pop_access_scope (fn);
11370 return arg;
11373 /* Substitute into all the default arguments for FN. */
11375 static void
11376 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11378 tree arg;
11379 tree tmpl_args;
11381 tmpl_args = DECL_TI_ARGS (fn);
11383 /* If this function is not yet instantiated, we certainly don't need
11384 its default arguments. */
11385 if (uses_template_parms (tmpl_args))
11386 return;
11387 /* Don't do this again for clones. */
11388 if (DECL_CLONED_FUNCTION_P (fn))
11389 return;
11391 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11392 arg;
11393 arg = TREE_CHAIN (arg))
11394 if (TREE_PURPOSE (arg))
11395 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11396 TREE_VALUE (arg),
11397 TREE_PURPOSE (arg),
11398 complain);
11401 /* Substitute the ARGS into the T, which is a _DECL. Return the
11402 result of the substitution. Issue error and warning messages under
11403 control of COMPLAIN. */
11405 static tree
11406 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11408 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11409 location_t saved_loc;
11410 tree r = NULL_TREE;
11411 tree in_decl = t;
11412 hashval_t hash = 0;
11414 /* Set the filename and linenumber to improve error-reporting. */
11415 saved_loc = input_location;
11416 input_location = DECL_SOURCE_LOCATION (t);
11418 switch (TREE_CODE (t))
11420 case TEMPLATE_DECL:
11422 /* We can get here when processing a member function template,
11423 member class template, or template template parameter. */
11424 tree decl = DECL_TEMPLATE_RESULT (t);
11425 tree spec;
11426 tree tmpl_args;
11427 tree full_args;
11429 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11431 /* Template template parameter is treated here. */
11432 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11433 if (new_type == error_mark_node)
11434 r = error_mark_node;
11435 /* If we get a real template back, return it. This can happen in
11436 the context of most_specialized_partial_spec. */
11437 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11438 r = new_type;
11439 else
11440 /* The new TEMPLATE_DECL was built in
11441 reduce_template_parm_level. */
11442 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
11443 break;
11446 /* We might already have an instance of this template.
11447 The ARGS are for the surrounding class type, so the
11448 full args contain the tsubst'd args for the context,
11449 plus the innermost args from the template decl. */
11450 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11451 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11452 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11453 /* Because this is a template, the arguments will still be
11454 dependent, even after substitution. If
11455 PROCESSING_TEMPLATE_DECL is not set, the dependency
11456 predicates will short-circuit. */
11457 ++processing_template_decl;
11458 full_args = tsubst_template_args (tmpl_args, args,
11459 complain, in_decl);
11460 --processing_template_decl;
11461 if (full_args == error_mark_node)
11462 RETURN (error_mark_node);
11464 /* If this is a default template template argument,
11465 tsubst might not have changed anything. */
11466 if (full_args == tmpl_args)
11467 RETURN (t);
11469 hash = hash_tmpl_and_args (t, full_args);
11470 spec = retrieve_specialization (t, full_args, hash);
11471 if (spec != NULL_TREE)
11473 r = spec;
11474 break;
11477 /* Make a new template decl. It will be similar to the
11478 original, but will record the current template arguments.
11479 We also create a new function declaration, which is just
11480 like the old one, but points to this new template, rather
11481 than the old one. */
11482 r = copy_decl (t);
11483 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11484 DECL_CHAIN (r) = NULL_TREE;
11486 // Build new template info linking to the original template decl.
11487 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11489 if (TREE_CODE (decl) == TYPE_DECL
11490 && !TYPE_DECL_ALIAS_P (decl))
11492 tree new_type;
11493 ++processing_template_decl;
11494 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11495 --processing_template_decl;
11496 if (new_type == error_mark_node)
11497 RETURN (error_mark_node);
11499 TREE_TYPE (r) = new_type;
11500 /* For a partial specialization, we need to keep pointing to
11501 the primary template. */
11502 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11503 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11504 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11505 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11506 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11508 else
11510 tree new_decl;
11511 ++processing_template_decl;
11512 new_decl = tsubst (decl, args, complain, in_decl);
11513 --processing_template_decl;
11514 if (new_decl == error_mark_node)
11515 RETURN (error_mark_node);
11517 DECL_TEMPLATE_RESULT (r) = new_decl;
11518 DECL_TI_TEMPLATE (new_decl) = r;
11519 TREE_TYPE (r) = TREE_TYPE (new_decl);
11520 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11521 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11524 SET_DECL_IMPLICIT_INSTANTIATION (r);
11525 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11526 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11528 /* The template parameters for this new template are all the
11529 template parameters for the old template, except the
11530 outermost level of parameters. */
11531 DECL_TEMPLATE_PARMS (r)
11532 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11533 complain);
11535 if (PRIMARY_TEMPLATE_P (t))
11536 DECL_PRIMARY_TEMPLATE (r) = r;
11538 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
11539 /* Record this non-type partial instantiation. */
11540 register_specialization (r, t,
11541 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11542 false, hash);
11544 break;
11546 case FUNCTION_DECL:
11548 tree ctx;
11549 tree argvec = NULL_TREE;
11550 tree *friends;
11551 tree gen_tmpl;
11552 tree type;
11553 int member;
11554 int args_depth;
11555 int parms_depth;
11557 /* Nobody should be tsubst'ing into non-template functions. */
11558 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11560 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11562 tree spec;
11563 bool dependent_p;
11565 /* If T is not dependent, just return it. We have to
11566 increment PROCESSING_TEMPLATE_DECL because
11567 value_dependent_expression_p assumes that nothing is
11568 dependent when PROCESSING_TEMPLATE_DECL is zero. */
11569 ++processing_template_decl;
11570 dependent_p = value_dependent_expression_p (t);
11571 --processing_template_decl;
11572 if (!dependent_p)
11573 RETURN (t);
11575 /* Calculate the most general template of which R is a
11576 specialization, and the complete set of arguments used to
11577 specialize R. */
11578 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11579 argvec = tsubst_template_args (DECL_TI_ARGS
11580 (DECL_TEMPLATE_RESULT
11581 (DECL_TI_TEMPLATE (t))),
11582 args, complain, in_decl);
11583 if (argvec == error_mark_node)
11584 RETURN (error_mark_node);
11586 /* Check to see if we already have this specialization. */
11587 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11588 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11590 if (spec)
11592 r = spec;
11593 break;
11596 /* We can see more levels of arguments than parameters if
11597 there was a specialization of a member template, like
11598 this:
11600 template <class T> struct S { template <class U> void f(); }
11601 template <> template <class U> void S<int>::f(U);
11603 Here, we'll be substituting into the specialization,
11604 because that's where we can find the code we actually
11605 want to generate, but we'll have enough arguments for
11606 the most general template.
11608 We also deal with the peculiar case:
11610 template <class T> struct S {
11611 template <class U> friend void f();
11613 template <class U> void f() {}
11614 template S<int>;
11615 template void f<double>();
11617 Here, the ARGS for the instantiation of will be {int,
11618 double}. But, we only need as many ARGS as there are
11619 levels of template parameters in CODE_PATTERN. We are
11620 careful not to get fooled into reducing the ARGS in
11621 situations like:
11623 template <class T> struct S { template <class U> void f(U); }
11624 template <class T> template <> void S<T>::f(int) {}
11626 which we can spot because the pattern will be a
11627 specialization in this case. */
11628 args_depth = TMPL_ARGS_DEPTH (args);
11629 parms_depth =
11630 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11631 if (args_depth > parms_depth
11632 && !DECL_TEMPLATE_SPECIALIZATION (t))
11633 args = get_innermost_template_args (args, parms_depth);
11635 else
11637 /* This special case arises when we have something like this:
11639 template <class T> struct S {
11640 friend void f<int>(int, double);
11643 Here, the DECL_TI_TEMPLATE for the friend declaration
11644 will be an IDENTIFIER_NODE. We are being called from
11645 tsubst_friend_function, and we want only to create a
11646 new decl (R) with appropriate types so that we can call
11647 determine_specialization. */
11648 gen_tmpl = NULL_TREE;
11651 if (DECL_CLASS_SCOPE_P (t))
11653 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11654 member = 2;
11655 else
11656 member = 1;
11657 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11658 complain, t, /*entering_scope=*/1);
11660 else
11662 member = 0;
11663 ctx = DECL_CONTEXT (t);
11665 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11666 if (type == error_mark_node)
11667 RETURN (error_mark_node);
11669 /* If we hit excessive deduction depth, the type is bogus even if
11670 it isn't error_mark_node, so don't build a decl. */
11671 if (excessive_deduction_depth)
11672 RETURN (error_mark_node);
11674 /* We do NOT check for matching decls pushed separately at this
11675 point, as they may not represent instantiations of this
11676 template, and in any case are considered separate under the
11677 discrete model. */
11678 r = copy_decl (t);
11679 DECL_USE_TEMPLATE (r) = 0;
11680 TREE_TYPE (r) = type;
11681 /* Clear out the mangled name and RTL for the instantiation. */
11682 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11683 SET_DECL_RTL (r, NULL);
11684 /* Leave DECL_INITIAL set on deleted instantiations. */
11685 if (!DECL_DELETED_FN (r))
11686 DECL_INITIAL (r) = NULL_TREE;
11687 DECL_CONTEXT (r) = ctx;
11689 /* OpenMP UDRs have the only argument a reference to the declared
11690 type. We want to diagnose if the declared type is a reference,
11691 which is invalid, but as references to references are usually
11692 quietly merged, diagnose it here. */
11693 if (DECL_OMP_DECLARE_REDUCTION_P (t))
11695 tree argtype
11696 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11697 argtype = tsubst (argtype, args, complain, in_decl);
11698 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11699 error_at (DECL_SOURCE_LOCATION (t),
11700 "reference type %qT in "
11701 "%<#pragma omp declare reduction%>", argtype);
11702 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11703 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11704 argtype);
11707 if (member && DECL_CONV_FN_P (r))
11708 /* Type-conversion operator. Reconstruct the name, in
11709 case it's the name of one of the template's parameters. */
11710 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11712 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11713 complain, t);
11714 DECL_RESULT (r) = NULL_TREE;
11716 TREE_STATIC (r) = 0;
11717 TREE_PUBLIC (r) = TREE_PUBLIC (t);
11718 DECL_EXTERNAL (r) = 1;
11719 /* If this is an instantiation of a function with internal
11720 linkage, we already know what object file linkage will be
11721 assigned to the instantiation. */
11722 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
11723 DECL_DEFER_OUTPUT (r) = 0;
11724 DECL_CHAIN (r) = NULL_TREE;
11725 DECL_PENDING_INLINE_INFO (r) = 0;
11726 DECL_PENDING_INLINE_P (r) = 0;
11727 DECL_SAVED_TREE (r) = NULL_TREE;
11728 DECL_STRUCT_FUNCTION (r) = NULL;
11729 TREE_USED (r) = 0;
11730 /* We'll re-clone as appropriate in instantiate_template. */
11731 DECL_CLONED_FUNCTION (r) = NULL_TREE;
11733 /* If we aren't complaining now, return on error before we register
11734 the specialization so that we'll complain eventually. */
11735 if ((complain & tf_error) == 0
11736 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11737 && !grok_op_properties (r, /*complain=*/false))
11738 RETURN (error_mark_node);
11740 /* When instantiating a constrained member, substitute
11741 into the constraints to create a new constraint. */
11742 if (tree ci = get_constraints (t))
11743 if (member)
11745 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
11746 set_constraints (r, ci);
11749 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11750 this in the special friend case mentioned above where
11751 GEN_TMPL is NULL. */
11752 if (gen_tmpl)
11754 DECL_TEMPLATE_INFO (r)
11755 = build_template_info (gen_tmpl, argvec);
11756 SET_DECL_IMPLICIT_INSTANTIATION (r);
11758 tree new_r
11759 = register_specialization (r, gen_tmpl, argvec, false, hash);
11760 if (new_r != r)
11761 /* We instantiated this while substituting into
11762 the type earlier (template/friend54.C). */
11763 RETURN (new_r);
11765 /* We're not supposed to instantiate default arguments
11766 until they are called, for a template. But, for a
11767 declaration like:
11769 template <class T> void f ()
11770 { extern void g(int i = T()); }
11772 we should do the substitution when the template is
11773 instantiated. We handle the member function case in
11774 instantiate_class_template since the default arguments
11775 might refer to other members of the class. */
11776 if (!member
11777 && !PRIMARY_TEMPLATE_P (gen_tmpl)
11778 && !uses_template_parms (argvec))
11779 tsubst_default_arguments (r, complain);
11781 else
11782 DECL_TEMPLATE_INFO (r) = NULL_TREE;
11784 /* Copy the list of befriending classes. */
11785 for (friends = &DECL_BEFRIENDING_CLASSES (r);
11786 *friends;
11787 friends = &TREE_CHAIN (*friends))
11789 *friends = copy_node (*friends);
11790 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
11791 args, complain,
11792 in_decl);
11795 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
11797 maybe_retrofit_in_chrg (r);
11798 if (DECL_CONSTRUCTOR_P (r))
11799 grok_ctor_properties (ctx, r);
11800 if (DECL_INHERITED_CTOR_BASE (r))
11801 deduce_inheriting_ctor (r);
11802 /* If this is an instantiation of a member template, clone it.
11803 If it isn't, that'll be handled by
11804 clone_constructors_and_destructors. */
11805 if (PRIMARY_TEMPLATE_P (gen_tmpl))
11806 clone_function_decl (r, /*update_method_vec_p=*/0);
11808 else if ((complain & tf_error) != 0
11809 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11810 && !grok_op_properties (r, /*complain=*/true))
11811 RETURN (error_mark_node);
11813 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
11814 SET_DECL_FRIEND_CONTEXT (r,
11815 tsubst (DECL_FRIEND_CONTEXT (t),
11816 args, complain, in_decl));
11818 /* Possibly limit visibility based on template args. */
11819 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11820 if (DECL_VISIBILITY_SPECIFIED (t))
11822 DECL_VISIBILITY_SPECIFIED (r) = 0;
11823 DECL_ATTRIBUTES (r)
11824 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11826 determine_visibility (r);
11827 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
11828 && !processing_template_decl)
11829 defaulted_late_check (r);
11831 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11832 args, complain, in_decl);
11834 break;
11836 case PARM_DECL:
11838 tree type = NULL_TREE;
11839 int i, len = 1;
11840 tree expanded_types = NULL_TREE;
11841 tree prev_r = NULL_TREE;
11842 tree first_r = NULL_TREE;
11844 if (DECL_PACK_P (t))
11846 /* If there is a local specialization that isn't a
11847 parameter pack, it means that we're doing a "simple"
11848 substitution from inside tsubst_pack_expansion. Just
11849 return the local specialization (which will be a single
11850 parm). */
11851 tree spec = retrieve_local_specialization (t);
11852 if (spec
11853 && TREE_CODE (spec) == PARM_DECL
11854 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11855 RETURN (spec);
11857 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11858 the parameters in this function parameter pack. */
11859 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11860 complain, in_decl);
11861 if (TREE_CODE (expanded_types) == TREE_VEC)
11863 len = TREE_VEC_LENGTH (expanded_types);
11865 /* Zero-length parameter packs are boring. Just substitute
11866 into the chain. */
11867 if (len == 0)
11868 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11869 TREE_CHAIN (t)));
11871 else
11873 /* All we did was update the type. Make a note of that. */
11874 type = expanded_types;
11875 expanded_types = NULL_TREE;
11879 /* Loop through all of the parameters we'll build. When T is
11880 a function parameter pack, LEN is the number of expanded
11881 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11882 r = NULL_TREE;
11883 for (i = 0; i < len; ++i)
11885 prev_r = r;
11886 r = copy_node (t);
11887 if (DECL_TEMPLATE_PARM_P (t))
11888 SET_DECL_TEMPLATE_PARM_P (r);
11890 if (expanded_types)
11891 /* We're on the Ith parameter of the function parameter
11892 pack. */
11894 /* Get the Ith type. */
11895 type = TREE_VEC_ELT (expanded_types, i);
11897 /* Rename the parameter to include the index. */
11898 DECL_NAME (r)
11899 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11901 else if (!type)
11902 /* We're dealing with a normal parameter. */
11903 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11905 type = type_decays_to (type);
11906 TREE_TYPE (r) = type;
11907 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11909 if (DECL_INITIAL (r))
11911 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11912 DECL_INITIAL (r) = TREE_TYPE (r);
11913 else
11914 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11915 complain, in_decl);
11918 DECL_CONTEXT (r) = NULL_TREE;
11920 if (!DECL_TEMPLATE_PARM_P (r))
11921 DECL_ARG_TYPE (r) = type_passed_as (type);
11923 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11924 args, complain, in_decl);
11926 /* Keep track of the first new parameter we
11927 generate. That's what will be returned to the
11928 caller. */
11929 if (!first_r)
11930 first_r = r;
11932 /* Build a proper chain of parameters when substituting
11933 into a function parameter pack. */
11934 if (prev_r)
11935 DECL_CHAIN (prev_r) = r;
11938 /* If cp_unevaluated_operand is set, we're just looking for a
11939 single dummy parameter, so don't keep going. */
11940 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11941 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11942 complain, DECL_CHAIN (t));
11944 /* FIRST_R contains the start of the chain we've built. */
11945 r = first_r;
11947 break;
11949 case FIELD_DECL:
11951 tree type = NULL_TREE;
11952 tree vec = NULL_TREE;
11953 tree expanded_types = NULL_TREE;
11954 int len = 1;
11956 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11958 /* This field is a lambda capture pack. Return a TREE_VEC of
11959 the expanded fields to instantiate_class_template_1 and
11960 store them in the specializations hash table as a
11961 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11962 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11963 complain, in_decl);
11964 if (TREE_CODE (expanded_types) == TREE_VEC)
11966 len = TREE_VEC_LENGTH (expanded_types);
11967 vec = make_tree_vec (len);
11969 else
11971 /* All we did was update the type. Make a note of that. */
11972 type = expanded_types;
11973 expanded_types = NULL_TREE;
11977 for (int i = 0; i < len; ++i)
11979 r = copy_decl (t);
11980 if (expanded_types)
11982 type = TREE_VEC_ELT (expanded_types, i);
11983 DECL_NAME (r)
11984 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11986 else if (!type)
11987 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11989 if (type == error_mark_node)
11990 RETURN (error_mark_node);
11991 TREE_TYPE (r) = type;
11992 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11994 if (DECL_C_BIT_FIELD (r))
11995 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11996 non-bit-fields DECL_INITIAL is a non-static data member
11997 initializer, which gets deferred instantiation. */
11998 DECL_INITIAL (r)
11999 = tsubst_expr (DECL_INITIAL (t), args,
12000 complain, in_decl,
12001 /*integral_constant_expression_p=*/true);
12002 else if (DECL_INITIAL (t))
12004 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12005 NSDMI in perform_member_init. Still set DECL_INITIAL
12006 so that we know there is one. */
12007 DECL_INITIAL (r) = void_node;
12008 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12009 retrofit_lang_decl (r);
12010 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12012 /* We don't have to set DECL_CONTEXT here; it is set by
12013 finish_member_declaration. */
12014 DECL_CHAIN (r) = NULL_TREE;
12016 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12017 args, complain, in_decl);
12019 if (vec)
12020 TREE_VEC_ELT (vec, i) = r;
12023 if (vec)
12025 r = vec;
12026 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12027 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12028 SET_ARGUMENT_PACK_ARGS (pack, vec);
12029 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12030 TREE_TYPE (pack) = tpack;
12031 register_specialization (pack, t, args, false, 0);
12034 break;
12036 case USING_DECL:
12037 /* We reach here only for member using decls. We also need to check
12038 uses_template_parms because DECL_DEPENDENT_P is not set for a
12039 using-declaration that designates a member of the current
12040 instantiation (c++/53549). */
12041 if (DECL_DEPENDENT_P (t)
12042 || uses_template_parms (USING_DECL_SCOPE (t)))
12044 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12045 complain, in_decl);
12046 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12047 r = do_class_using_decl (inst_scope, name);
12048 if (!r)
12049 r = error_mark_node;
12050 else
12052 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12053 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12056 else
12058 r = copy_node (t);
12059 DECL_CHAIN (r) = NULL_TREE;
12061 break;
12063 case TYPE_DECL:
12064 case VAR_DECL:
12066 tree argvec = NULL_TREE;
12067 tree gen_tmpl = NULL_TREE;
12068 tree spec;
12069 tree tmpl = NULL_TREE;
12070 tree ctx;
12071 tree type = NULL_TREE;
12072 bool local_p;
12074 if (TREE_TYPE (t) == error_mark_node)
12075 RETURN (error_mark_node);
12077 if (TREE_CODE (t) == TYPE_DECL
12078 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12080 /* If this is the canonical decl, we don't have to
12081 mess with instantiations, and often we can't (for
12082 typename, template type parms and such). Note that
12083 TYPE_NAME is not correct for the above test if
12084 we've copied the type for a typedef. */
12085 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12086 if (type == error_mark_node)
12087 RETURN (error_mark_node);
12088 r = TYPE_NAME (type);
12089 break;
12092 /* Check to see if we already have the specialization we
12093 need. */
12094 spec = NULL_TREE;
12095 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12097 /* T is a static data member or namespace-scope entity.
12098 We have to substitute into namespace-scope variables
12099 (not just variable templates) because of cases like:
12101 template <class T> void f() { extern T t; }
12103 where the entity referenced is not known until
12104 instantiation time. */
12105 local_p = false;
12106 ctx = DECL_CONTEXT (t);
12107 if (DECL_CLASS_SCOPE_P (t))
12109 ctx = tsubst_aggr_type (ctx, args,
12110 complain,
12111 in_decl, /*entering_scope=*/1);
12112 /* If CTX is unchanged, then T is in fact the
12113 specialization we want. That situation occurs when
12114 referencing a static data member within in its own
12115 class. We can use pointer equality, rather than
12116 same_type_p, because DECL_CONTEXT is always
12117 canonical... */
12118 if (ctx == DECL_CONTEXT (t)
12119 /* ... unless T is a member template; in which
12120 case our caller can be willing to create a
12121 specialization of that template represented
12122 by T. */
12123 && !(DECL_TI_TEMPLATE (t)
12124 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12125 spec = t;
12128 if (!spec)
12130 tmpl = DECL_TI_TEMPLATE (t);
12131 gen_tmpl = most_general_template (tmpl);
12132 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12133 if (argvec != error_mark_node)
12134 argvec = (coerce_innermost_template_parms
12135 (DECL_TEMPLATE_PARMS (gen_tmpl),
12136 argvec, t, complain,
12137 /*all*/true, /*defarg*/true));
12138 if (argvec == error_mark_node)
12139 RETURN (error_mark_node);
12140 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12141 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12144 else
12146 /* A local variable. */
12147 local_p = true;
12148 /* Subsequent calls to pushdecl will fill this in. */
12149 ctx = NULL_TREE;
12150 spec = retrieve_local_specialization (t);
12152 /* If we already have the specialization we need, there is
12153 nothing more to do. */
12154 if (spec)
12156 r = spec;
12157 break;
12160 /* Create a new node for the specialization we need. */
12161 r = copy_decl (t);
12162 if (type == NULL_TREE)
12164 if (is_typedef_decl (t))
12165 type = DECL_ORIGINAL_TYPE (t);
12166 else
12167 type = TREE_TYPE (t);
12168 if (VAR_P (t)
12169 && VAR_HAD_UNKNOWN_BOUND (t)
12170 && type != error_mark_node)
12171 type = strip_array_domain (type);
12172 type = tsubst (type, args, complain, in_decl);
12174 if (VAR_P (r))
12176 /* Even if the original location is out of scope, the
12177 newly substituted one is not. */
12178 DECL_DEAD_FOR_LOCAL (r) = 0;
12179 DECL_INITIALIZED_P (r) = 0;
12180 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12181 if (type == error_mark_node)
12182 RETURN (error_mark_node);
12183 if (TREE_CODE (type) == FUNCTION_TYPE)
12185 /* It may seem that this case cannot occur, since:
12187 typedef void f();
12188 void g() { f x; }
12190 declares a function, not a variable. However:
12192 typedef void f();
12193 template <typename T> void g() { T t; }
12194 template void g<f>();
12196 is an attempt to declare a variable with function
12197 type. */
12198 error ("variable %qD has function type",
12199 /* R is not yet sufficiently initialized, so we
12200 just use its name. */
12201 DECL_NAME (r));
12202 RETURN (error_mark_node);
12204 type = complete_type (type);
12205 /* Wait until cp_finish_decl to set this again, to handle
12206 circular dependency (template/instantiate6.C). */
12207 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12208 type = check_var_type (DECL_NAME (r), type);
12210 if (DECL_HAS_VALUE_EXPR_P (t))
12212 tree ve = DECL_VALUE_EXPR (t);
12213 ve = tsubst_expr (ve, args, complain, in_decl,
12214 /*constant_expression_p=*/false);
12215 if (REFERENCE_REF_P (ve))
12217 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12218 ve = TREE_OPERAND (ve, 0);
12220 SET_DECL_VALUE_EXPR (r, ve);
12222 if (CP_DECL_THREAD_LOCAL_P (r)
12223 && !processing_template_decl)
12224 set_decl_tls_model (r, decl_default_tls_model (r));
12226 else if (DECL_SELF_REFERENCE_P (t))
12227 SET_DECL_SELF_REFERENCE_P (r);
12228 TREE_TYPE (r) = type;
12229 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12230 DECL_CONTEXT (r) = ctx;
12231 /* Clear out the mangled name and RTL for the instantiation. */
12232 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12233 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12234 SET_DECL_RTL (r, NULL);
12235 /* The initializer must not be expanded until it is required;
12236 see [temp.inst]. */
12237 DECL_INITIAL (r) = NULL_TREE;
12238 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12239 SET_DECL_RTL (r, NULL);
12240 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12241 if (VAR_P (r))
12243 /* Possibly limit visibility based on template args. */
12244 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12245 if (DECL_VISIBILITY_SPECIFIED (t))
12247 DECL_VISIBILITY_SPECIFIED (r) = 0;
12248 DECL_ATTRIBUTES (r)
12249 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12251 determine_visibility (r);
12254 if (!local_p)
12256 /* A static data member declaration is always marked
12257 external when it is declared in-class, even if an
12258 initializer is present. We mimic the non-template
12259 processing here. */
12260 DECL_EXTERNAL (r) = 1;
12261 if (DECL_NAMESPACE_SCOPE_P (t))
12262 DECL_NOT_REALLY_EXTERN (r) = 1;
12264 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12265 SET_DECL_IMPLICIT_INSTANTIATION (r);
12266 register_specialization (r, gen_tmpl, argvec, false, hash);
12268 else if (!cp_unevaluated_operand)
12269 register_local_specialization (r, t);
12271 DECL_CHAIN (r) = NULL_TREE;
12273 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12274 /*flags=*/0,
12275 args, complain, in_decl);
12277 /* Preserve a typedef that names a type. */
12278 if (is_typedef_decl (r))
12280 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12281 set_underlying_type (r);
12282 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12283 /* An alias template specialization can be dependent
12284 even if its underlying type is not. */
12285 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12288 layout_decl (r, 0);
12290 break;
12292 default:
12293 gcc_unreachable ();
12295 #undef RETURN
12297 out:
12298 /* Restore the file and line information. */
12299 input_location = saved_loc;
12301 return r;
12304 /* Substitute into the ARG_TYPES of a function type.
12305 If END is a TREE_CHAIN, leave it and any following types
12306 un-substituted. */
12308 static tree
12309 tsubst_arg_types (tree arg_types,
12310 tree args,
12311 tree end,
12312 tsubst_flags_t complain,
12313 tree in_decl)
12315 tree remaining_arg_types;
12316 tree type = NULL_TREE;
12317 int i = 1;
12318 tree expanded_args = NULL_TREE;
12319 tree default_arg;
12321 if (!arg_types || arg_types == void_list_node || arg_types == end)
12322 return arg_types;
12324 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12325 args, end, complain, in_decl);
12326 if (remaining_arg_types == error_mark_node)
12327 return error_mark_node;
12329 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12331 /* For a pack expansion, perform substitution on the
12332 entire expression. Later on, we'll handle the arguments
12333 one-by-one. */
12334 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12335 args, complain, in_decl);
12337 if (TREE_CODE (expanded_args) == TREE_VEC)
12338 /* So that we'll spin through the parameters, one by one. */
12339 i = TREE_VEC_LENGTH (expanded_args);
12340 else
12342 /* We only partially substituted into the parameter
12343 pack. Our type is TYPE_PACK_EXPANSION. */
12344 type = expanded_args;
12345 expanded_args = NULL_TREE;
12349 while (i > 0) {
12350 --i;
12352 if (expanded_args)
12353 type = TREE_VEC_ELT (expanded_args, i);
12354 else if (!type)
12355 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12357 if (type == error_mark_node)
12358 return error_mark_node;
12359 if (VOID_TYPE_P (type))
12361 if (complain & tf_error)
12363 error ("invalid parameter type %qT", type);
12364 if (in_decl)
12365 error ("in declaration %q+D", in_decl);
12367 return error_mark_node;
12369 /* DR 657. */
12370 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12371 return error_mark_node;
12373 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12374 top-level qualifiers as required. */
12375 type = cv_unqualified (type_decays_to (type));
12377 /* We do not substitute into default arguments here. The standard
12378 mandates that they be instantiated only when needed, which is
12379 done in build_over_call. */
12380 default_arg = TREE_PURPOSE (arg_types);
12382 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12384 /* We've instantiated a template before its default arguments
12385 have been parsed. This can happen for a nested template
12386 class, and is not an error unless we require the default
12387 argument in a call of this function. */
12388 remaining_arg_types =
12389 tree_cons (default_arg, type, remaining_arg_types);
12390 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12392 else
12393 remaining_arg_types =
12394 hash_tree_cons (default_arg, type, remaining_arg_types);
12397 return remaining_arg_types;
12400 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12401 *not* handle the exception-specification for FNTYPE, because the
12402 initial substitution of explicitly provided template parameters
12403 during argument deduction forbids substitution into the
12404 exception-specification:
12406 [temp.deduct]
12408 All references in the function type of the function template to the
12409 corresponding template parameters are replaced by the specified tem-
12410 plate argument values. If a substitution in a template parameter or
12411 in the function type of the function template results in an invalid
12412 type, type deduction fails. [Note: The equivalent substitution in
12413 exception specifications is done only when the function is instanti-
12414 ated, at which point a program is ill-formed if the substitution
12415 results in an invalid type.] */
12417 static tree
12418 tsubst_function_type (tree t,
12419 tree args,
12420 tsubst_flags_t complain,
12421 tree in_decl)
12423 tree return_type;
12424 tree arg_types = NULL_TREE;
12425 tree fntype;
12427 /* The TYPE_CONTEXT is not used for function/method types. */
12428 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12430 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12431 failure. */
12432 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12434 if (late_return_type_p)
12436 /* Substitute the argument types. */
12437 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12438 complain, in_decl);
12439 if (arg_types == error_mark_node)
12440 return error_mark_node;
12442 tree save_ccp = current_class_ptr;
12443 tree save_ccr = current_class_ref;
12444 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12445 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12446 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12447 if (do_inject)
12449 /* DR 1207: 'this' is in scope in the trailing return type. */
12450 inject_this_parameter (this_type, cp_type_quals (this_type));
12453 /* Substitute the return type. */
12454 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12456 if (do_inject)
12458 current_class_ptr = save_ccp;
12459 current_class_ref = save_ccr;
12462 else
12463 /* Substitute the return type. */
12464 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12466 if (return_type == error_mark_node)
12467 return error_mark_node;
12468 /* DR 486 clarifies that creation of a function type with an
12469 invalid return type is a deduction failure. */
12470 if (TREE_CODE (return_type) == ARRAY_TYPE
12471 || TREE_CODE (return_type) == FUNCTION_TYPE)
12473 if (complain & tf_error)
12475 if (TREE_CODE (return_type) == ARRAY_TYPE)
12476 error ("function returning an array");
12477 else
12478 error ("function returning a function");
12480 return error_mark_node;
12482 /* And DR 657. */
12483 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12484 return error_mark_node;
12486 if (!late_return_type_p)
12488 /* Substitute the argument types. */
12489 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12490 complain, in_decl);
12491 if (arg_types == error_mark_node)
12492 return error_mark_node;
12495 /* Construct a new type node and return it. */
12496 if (TREE_CODE (t) == FUNCTION_TYPE)
12498 fntype = build_function_type (return_type, arg_types);
12499 fntype = apply_memfn_quals (fntype,
12500 type_memfn_quals (t),
12501 type_memfn_rqual (t));
12503 else
12505 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12506 /* Don't pick up extra function qualifiers from the basetype. */
12507 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12508 if (! MAYBE_CLASS_TYPE_P (r))
12510 /* [temp.deduct]
12512 Type deduction may fail for any of the following
12513 reasons:
12515 -- Attempting to create "pointer to member of T" when T
12516 is not a class type. */
12517 if (complain & tf_error)
12518 error ("creating pointer to member function of non-class type %qT",
12520 return error_mark_node;
12523 fntype = build_method_type_directly (r, return_type,
12524 TREE_CHAIN (arg_types));
12525 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12527 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12529 if (late_return_type_p)
12530 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12532 return fntype;
12535 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12536 ARGS into that specification, and return the substituted
12537 specification. If there is no specification, return NULL_TREE. */
12539 static tree
12540 tsubst_exception_specification (tree fntype,
12541 tree args,
12542 tsubst_flags_t complain,
12543 tree in_decl,
12544 bool defer_ok)
12546 tree specs;
12547 tree new_specs;
12549 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12550 new_specs = NULL_TREE;
12551 if (specs && TREE_PURPOSE (specs))
12553 /* A noexcept-specifier. */
12554 tree expr = TREE_PURPOSE (specs);
12555 if (TREE_CODE (expr) == INTEGER_CST)
12556 new_specs = expr;
12557 else if (defer_ok)
12559 /* Defer instantiation of noexcept-specifiers to avoid
12560 excessive instantiations (c++/49107). */
12561 new_specs = make_node (DEFERRED_NOEXCEPT);
12562 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12564 /* We already partially instantiated this member template,
12565 so combine the new args with the old. */
12566 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12567 = DEFERRED_NOEXCEPT_PATTERN (expr);
12568 DEFERRED_NOEXCEPT_ARGS (new_specs)
12569 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12571 else
12573 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12574 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12577 else
12578 new_specs = tsubst_copy_and_build
12579 (expr, args, complain, in_decl, /*function_p=*/false,
12580 /*integral_constant_expression_p=*/true);
12581 new_specs = build_noexcept_spec (new_specs, complain);
12583 else if (specs)
12585 if (! TREE_VALUE (specs))
12586 new_specs = specs;
12587 else
12588 while (specs)
12590 tree spec;
12591 int i, len = 1;
12592 tree expanded_specs = NULL_TREE;
12594 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12596 /* Expand the pack expansion type. */
12597 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12598 args, complain,
12599 in_decl);
12601 if (expanded_specs == error_mark_node)
12602 return error_mark_node;
12603 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12604 len = TREE_VEC_LENGTH (expanded_specs);
12605 else
12607 /* We're substituting into a member template, so
12608 we got a TYPE_PACK_EXPANSION back. Add that
12609 expansion and move on. */
12610 gcc_assert (TREE_CODE (expanded_specs)
12611 == TYPE_PACK_EXPANSION);
12612 new_specs = add_exception_specifier (new_specs,
12613 expanded_specs,
12614 complain);
12615 specs = TREE_CHAIN (specs);
12616 continue;
12620 for (i = 0; i < len; ++i)
12622 if (expanded_specs)
12623 spec = TREE_VEC_ELT (expanded_specs, i);
12624 else
12625 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12626 if (spec == error_mark_node)
12627 return spec;
12628 new_specs = add_exception_specifier (new_specs, spec,
12629 complain);
12632 specs = TREE_CHAIN (specs);
12635 return new_specs;
12638 /* Take the tree structure T and replace template parameters used
12639 therein with the argument vector ARGS. IN_DECL is an associated
12640 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12641 Issue error and warning messages under control of COMPLAIN. Note
12642 that we must be relatively non-tolerant of extensions here, in
12643 order to preserve conformance; if we allow substitutions that
12644 should not be allowed, we may allow argument deductions that should
12645 not succeed, and therefore report ambiguous overload situations
12646 where there are none. In theory, we could allow the substitution,
12647 but indicate that it should have failed, and allow our caller to
12648 make sure that the right thing happens, but we don't try to do this
12649 yet.
12651 This function is used for dealing with types, decls and the like;
12652 for expressions, use tsubst_expr or tsubst_copy. */
12654 tree
12655 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12657 enum tree_code code;
12658 tree type, r = NULL_TREE;
12660 if (t == NULL_TREE || t == error_mark_node
12661 || t == integer_type_node
12662 || t == void_type_node
12663 || t == char_type_node
12664 || t == unknown_type_node
12665 || TREE_CODE (t) == NAMESPACE_DECL
12666 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12667 return t;
12669 if (DECL_P (t))
12670 return tsubst_decl (t, args, complain);
12672 if (args == NULL_TREE)
12673 return t;
12675 code = TREE_CODE (t);
12677 if (code == IDENTIFIER_NODE)
12678 type = IDENTIFIER_TYPE_VALUE (t);
12679 else
12680 type = TREE_TYPE (t);
12682 gcc_assert (type != unknown_type_node);
12684 /* Reuse typedefs. We need to do this to handle dependent attributes,
12685 such as attribute aligned. */
12686 if (TYPE_P (t)
12687 && typedef_variant_p (t))
12689 tree decl = TYPE_NAME (t);
12691 if (alias_template_specialization_p (t))
12693 /* DECL represents an alias template and we want to
12694 instantiate it. */
12695 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12696 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12697 r = instantiate_alias_template (tmpl, gen_args, complain);
12699 else if (DECL_CLASS_SCOPE_P (decl)
12700 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12701 && uses_template_parms (DECL_CONTEXT (decl)))
12703 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12704 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12705 r = retrieve_specialization (tmpl, gen_args, 0);
12707 else if (DECL_FUNCTION_SCOPE_P (decl)
12708 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12709 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12710 r = retrieve_local_specialization (decl);
12711 else
12712 /* The typedef is from a non-template context. */
12713 return t;
12715 if (r)
12717 r = TREE_TYPE (r);
12718 r = cp_build_qualified_type_real
12719 (r, cp_type_quals (t) | cp_type_quals (r),
12720 complain | tf_ignore_bad_quals);
12721 return r;
12723 else
12725 /* We don't have an instantiation yet, so drop the typedef. */
12726 int quals = cp_type_quals (t);
12727 t = DECL_ORIGINAL_TYPE (decl);
12728 t = cp_build_qualified_type_real (t, quals,
12729 complain | tf_ignore_bad_quals);
12733 if (type
12734 && code != TYPENAME_TYPE
12735 && code != TEMPLATE_TYPE_PARM
12736 && code != IDENTIFIER_NODE
12737 && code != FUNCTION_TYPE
12738 && code != METHOD_TYPE)
12739 type = tsubst (type, args, complain, in_decl);
12740 if (type == error_mark_node)
12741 return error_mark_node;
12743 switch (code)
12745 case RECORD_TYPE:
12746 case UNION_TYPE:
12747 case ENUMERAL_TYPE:
12748 return tsubst_aggr_type (t, args, complain, in_decl,
12749 /*entering_scope=*/0);
12751 case ERROR_MARK:
12752 case IDENTIFIER_NODE:
12753 case VOID_TYPE:
12754 case REAL_TYPE:
12755 case COMPLEX_TYPE:
12756 case VECTOR_TYPE:
12757 case BOOLEAN_TYPE:
12758 case NULLPTR_TYPE:
12759 case LANG_TYPE:
12760 return t;
12762 case INTEGER_TYPE:
12763 if (t == integer_type_node)
12764 return t;
12766 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
12767 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
12768 return t;
12771 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
12773 max = tsubst_expr (omax, args, complain, in_decl,
12774 /*integral_constant_expression_p=*/false);
12776 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12777 needed. */
12778 if (TREE_CODE (max) == NOP_EXPR
12779 && TREE_SIDE_EFFECTS (omax)
12780 && !TREE_TYPE (max))
12781 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
12783 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12784 with TREE_SIDE_EFFECTS that indicates this is not an integral
12785 constant expression. */
12786 if (processing_template_decl
12787 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
12789 gcc_assert (TREE_CODE (max) == NOP_EXPR);
12790 TREE_SIDE_EFFECTS (max) = 1;
12793 return compute_array_index_type (NULL_TREE, max, complain);
12796 case TEMPLATE_TYPE_PARM:
12797 case TEMPLATE_TEMPLATE_PARM:
12798 case BOUND_TEMPLATE_TEMPLATE_PARM:
12799 case TEMPLATE_PARM_INDEX:
12801 int idx;
12802 int level;
12803 int levels;
12804 tree arg = NULL_TREE;
12806 /* Early in template argument deduction substitution, we don't
12807 want to reduce the level of 'auto', or it will be confused
12808 with a normal template parm in subsequent deduction. */
12809 if (is_auto (t) && (complain & tf_partial))
12810 return t;
12812 r = NULL_TREE;
12814 gcc_assert (TREE_VEC_LENGTH (args) > 0);
12815 template_parm_level_and_index (t, &level, &idx);
12817 levels = TMPL_ARGS_DEPTH (args);
12818 if (level <= levels
12819 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
12821 arg = TMPL_ARG (args, level, idx);
12823 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
12825 /* See through ARGUMENT_PACK_SELECT arguments. */
12826 arg = ARGUMENT_PACK_SELECT_ARG (arg);
12827 /* If the selected argument is an expansion E, that most
12828 likely means we were called from
12829 gen_elem_of_pack_expansion_instantiation during the
12830 substituting of pack an argument pack (which Ith
12831 element is a pack expansion, where I is
12832 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12833 In this case, the Ith element resulting from this
12834 substituting is going to be a pack expansion, which
12835 pattern is the pattern of E. Let's return the
12836 pattern of E, and
12837 gen_elem_of_pack_expansion_instantiation will
12838 build the resulting pack expansion from it. */
12839 if (PACK_EXPANSION_P (arg))
12841 /* Make sure we aren't throwing away arg info. */
12842 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
12843 arg = PACK_EXPANSION_PATTERN (arg);
12848 if (arg == error_mark_node)
12849 return error_mark_node;
12850 else if (arg != NULL_TREE)
12852 if (ARGUMENT_PACK_P (arg))
12853 /* If ARG is an argument pack, we don't actually want to
12854 perform a substitution here, because substitutions
12855 for argument packs are only done
12856 element-by-element. We can get to this point when
12857 substituting the type of a non-type template
12858 parameter pack, when that type actually contains
12859 template parameter packs from an outer template, e.g.,
12861 template<typename... Types> struct A {
12862 template<Types... Values> struct B { };
12863 }; */
12864 return t;
12866 if (code == TEMPLATE_TYPE_PARM)
12868 int quals;
12869 gcc_assert (TYPE_P (arg));
12871 quals = cp_type_quals (arg) | cp_type_quals (t);
12873 return cp_build_qualified_type_real
12874 (arg, quals, complain | tf_ignore_bad_quals);
12876 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12878 /* We are processing a type constructed from a
12879 template template parameter. */
12880 tree argvec = tsubst (TYPE_TI_ARGS (t),
12881 args, complain, in_decl);
12882 if (argvec == error_mark_node)
12883 return error_mark_node;
12885 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12886 || TREE_CODE (arg) == TEMPLATE_DECL
12887 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12889 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12890 /* Consider this code:
12892 template <template <class> class Template>
12893 struct Internal {
12894 template <class Arg> using Bind = Template<Arg>;
12897 template <template <class> class Template, class Arg>
12898 using Instantiate = Template<Arg>; //#0
12900 template <template <class> class Template,
12901 class Argument>
12902 using Bind =
12903 Instantiate<Internal<Template>::template Bind,
12904 Argument>; //#1
12906 When #1 is parsed, the
12907 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12908 parameter `Template' in #0 matches the
12909 UNBOUND_CLASS_TEMPLATE representing the argument
12910 `Internal<Template>::template Bind'; We then want
12911 to assemble the type `Bind<Argument>' that can't
12912 be fully created right now, because
12913 `Internal<Template>' not being complete, the Bind
12914 template cannot be looked up in that context. So
12915 we need to "store" `Bind<Argument>' for later
12916 when the context of Bind becomes complete. Let's
12917 store that in a TYPENAME_TYPE. */
12918 return make_typename_type (TYPE_CONTEXT (arg),
12919 build_nt (TEMPLATE_ID_EXPR,
12920 TYPE_IDENTIFIER (arg),
12921 argvec),
12922 typename_type,
12923 complain);
12925 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12926 are resolving nested-types in the signature of a
12927 member function templates. Otherwise ARG is a
12928 TEMPLATE_DECL and is the real template to be
12929 instantiated. */
12930 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12931 arg = TYPE_NAME (arg);
12933 r = lookup_template_class (arg,
12934 argvec, in_decl,
12935 DECL_CONTEXT (arg),
12936 /*entering_scope=*/0,
12937 complain);
12938 return cp_build_qualified_type_real
12939 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12941 else
12942 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12943 return convert_from_reference (unshare_expr (arg));
12946 if (level == 1)
12947 /* This can happen during the attempted tsubst'ing in
12948 unify. This means that we don't yet have any information
12949 about the template parameter in question. */
12950 return t;
12952 /* If we get here, we must have been looking at a parm for a
12953 more deeply nested template. Make a new version of this
12954 template parameter, but with a lower level. */
12955 switch (code)
12957 case TEMPLATE_TYPE_PARM:
12958 case TEMPLATE_TEMPLATE_PARM:
12959 case BOUND_TEMPLATE_TEMPLATE_PARM:
12960 if (cp_type_quals (t))
12962 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12963 r = cp_build_qualified_type_real
12964 (r, cp_type_quals (t),
12965 complain | (code == TEMPLATE_TYPE_PARM
12966 ? tf_ignore_bad_quals : 0));
12968 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
12969 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
12970 && (r = (TEMPLATE_PARM_DESCENDANTS
12971 (TEMPLATE_TYPE_PARM_INDEX (t))))
12972 && (r = TREE_TYPE (r))
12973 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
12974 /* Break infinite recursion when substituting the constraints
12975 of a constrained placeholder. */;
12976 else
12978 r = copy_type (t);
12979 TEMPLATE_TYPE_PARM_INDEX (r)
12980 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12981 r, levels, args, complain);
12982 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12983 TYPE_MAIN_VARIANT (r) = r;
12984 TYPE_POINTER_TO (r) = NULL_TREE;
12985 TYPE_REFERENCE_TO (r) = NULL_TREE;
12987 /* Propagate constraints on placeholders. */
12988 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12989 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
12990 PLACEHOLDER_TYPE_CONSTRAINTS (r)
12991 = tsubst_constraint (constr, args, complain, in_decl);
12993 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12994 /* We have reduced the level of the template
12995 template parameter, but not the levels of its
12996 template parameters, so canonical_type_parameter
12997 will not be able to find the canonical template
12998 template parameter for this level. Thus, we
12999 require structural equality checking to compare
13000 TEMPLATE_TEMPLATE_PARMs. */
13001 SET_TYPE_STRUCTURAL_EQUALITY (r);
13002 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13003 SET_TYPE_STRUCTURAL_EQUALITY (r);
13004 else
13005 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13007 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13009 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
13010 complain, in_decl);
13011 if (argvec == error_mark_node)
13012 return error_mark_node;
13014 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13015 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
13018 break;
13020 case TEMPLATE_PARM_INDEX:
13021 r = reduce_template_parm_level (t, type, levels, args, complain);
13022 break;
13024 default:
13025 gcc_unreachable ();
13028 return r;
13031 case TREE_LIST:
13033 tree purpose, value, chain;
13035 if (t == void_list_node)
13036 return t;
13038 purpose = TREE_PURPOSE (t);
13039 if (purpose)
13041 purpose = tsubst (purpose, args, complain, in_decl);
13042 if (purpose == error_mark_node)
13043 return error_mark_node;
13045 value = TREE_VALUE (t);
13046 if (value)
13048 value = tsubst (value, args, complain, in_decl);
13049 if (value == error_mark_node)
13050 return error_mark_node;
13052 chain = TREE_CHAIN (t);
13053 if (chain && chain != void_type_node)
13055 chain = tsubst (chain, args, complain, in_decl);
13056 if (chain == error_mark_node)
13057 return error_mark_node;
13059 if (purpose == TREE_PURPOSE (t)
13060 && value == TREE_VALUE (t)
13061 && chain == TREE_CHAIN (t))
13062 return t;
13063 return hash_tree_cons (purpose, value, chain);
13066 case TREE_BINFO:
13067 /* We should never be tsubsting a binfo. */
13068 gcc_unreachable ();
13070 case TREE_VEC:
13071 /* A vector of template arguments. */
13072 gcc_assert (!type);
13073 return tsubst_template_args (t, args, complain, in_decl);
13075 case POINTER_TYPE:
13076 case REFERENCE_TYPE:
13078 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13079 return t;
13081 /* [temp.deduct]
13083 Type deduction may fail for any of the following
13084 reasons:
13086 -- Attempting to create a pointer to reference type.
13087 -- Attempting to create a reference to a reference type or
13088 a reference to void.
13090 Core issue 106 says that creating a reference to a reference
13091 during instantiation is no longer a cause for failure. We
13092 only enforce this check in strict C++98 mode. */
13093 if ((TREE_CODE (type) == REFERENCE_TYPE
13094 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13095 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13097 static location_t last_loc;
13099 /* We keep track of the last time we issued this error
13100 message to avoid spewing a ton of messages during a
13101 single bad template instantiation. */
13102 if (complain & tf_error
13103 && last_loc != input_location)
13105 if (VOID_TYPE_P (type))
13106 error ("forming reference to void");
13107 else if (code == POINTER_TYPE)
13108 error ("forming pointer to reference type %qT", type);
13109 else
13110 error ("forming reference to reference type %qT", type);
13111 last_loc = input_location;
13114 return error_mark_node;
13116 else if (TREE_CODE (type) == FUNCTION_TYPE
13117 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13118 || type_memfn_rqual (type) != REF_QUAL_NONE))
13120 if (complain & tf_error)
13122 if (code == POINTER_TYPE)
13123 error ("forming pointer to qualified function type %qT",
13124 type);
13125 else
13126 error ("forming reference to qualified function type %qT",
13127 type);
13129 return error_mark_node;
13131 else if (code == POINTER_TYPE)
13133 r = build_pointer_type (type);
13134 if (TREE_CODE (type) == METHOD_TYPE)
13135 r = build_ptrmemfunc_type (r);
13137 else if (TREE_CODE (type) == REFERENCE_TYPE)
13138 /* In C++0x, during template argument substitution, when there is an
13139 attempt to create a reference to a reference type, reference
13140 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13142 "If a template-argument for a template-parameter T names a type
13143 that is a reference to a type A, an attempt to create the type
13144 'lvalue reference to cv T' creates the type 'lvalue reference to
13145 A,' while an attempt to create the type type rvalue reference to
13146 cv T' creates the type T"
13148 r = cp_build_reference_type
13149 (TREE_TYPE (type),
13150 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13151 else
13152 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13153 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13155 if (r != error_mark_node)
13156 /* Will this ever be needed for TYPE_..._TO values? */
13157 layout_type (r);
13159 return r;
13161 case OFFSET_TYPE:
13163 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13164 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13166 /* [temp.deduct]
13168 Type deduction may fail for any of the following
13169 reasons:
13171 -- Attempting to create "pointer to member of T" when T
13172 is not a class type. */
13173 if (complain & tf_error)
13174 error ("creating pointer to member of non-class type %qT", r);
13175 return error_mark_node;
13177 if (TREE_CODE (type) == REFERENCE_TYPE)
13179 if (complain & tf_error)
13180 error ("creating pointer to member reference type %qT", type);
13181 return error_mark_node;
13183 if (VOID_TYPE_P (type))
13185 if (complain & tf_error)
13186 error ("creating pointer to member of type void");
13187 return error_mark_node;
13189 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13190 if (TREE_CODE (type) == FUNCTION_TYPE)
13192 /* The type of the implicit object parameter gets its
13193 cv-qualifiers from the FUNCTION_TYPE. */
13194 tree memptr;
13195 tree method_type
13196 = build_memfn_type (type, r, type_memfn_quals (type),
13197 type_memfn_rqual (type));
13198 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13199 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13200 complain);
13202 else
13203 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13204 cp_type_quals (t),
13205 complain);
13207 case FUNCTION_TYPE:
13208 case METHOD_TYPE:
13210 tree fntype;
13211 tree specs;
13212 fntype = tsubst_function_type (t, args, complain, in_decl);
13213 if (fntype == error_mark_node)
13214 return error_mark_node;
13216 /* Substitute the exception specification. */
13217 specs = tsubst_exception_specification (t, args, complain,
13218 in_decl, /*defer_ok*/true);
13219 if (specs == error_mark_node)
13220 return error_mark_node;
13221 if (specs)
13222 fntype = build_exception_variant (fntype, specs);
13223 return fntype;
13225 case ARRAY_TYPE:
13227 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13228 if (domain == error_mark_node)
13229 return error_mark_node;
13231 /* As an optimization, we avoid regenerating the array type if
13232 it will obviously be the same as T. */
13233 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13234 return t;
13236 /* These checks should match the ones in create_array_type_for_decl.
13238 [temp.deduct]
13240 The deduction may fail for any of the following reasons:
13242 -- Attempting to create an array with an element type that
13243 is void, a function type, or a reference type, or [DR337]
13244 an abstract class type. */
13245 if (VOID_TYPE_P (type)
13246 || TREE_CODE (type) == FUNCTION_TYPE
13247 || (TREE_CODE (type) == ARRAY_TYPE
13248 && TYPE_DOMAIN (type) == NULL_TREE)
13249 || TREE_CODE (type) == REFERENCE_TYPE)
13251 if (complain & tf_error)
13252 error ("creating array of %qT", type);
13253 return error_mark_node;
13256 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13257 return error_mark_node;
13259 r = build_cplus_array_type (type, domain);
13261 if (TYPE_USER_ALIGN (t))
13263 TYPE_ALIGN (r) = TYPE_ALIGN (t);
13264 TYPE_USER_ALIGN (r) = 1;
13267 return r;
13270 case TYPENAME_TYPE:
13272 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13273 in_decl, /*entering_scope=*/1);
13274 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13275 complain, in_decl);
13277 if (ctx == error_mark_node || f == error_mark_node)
13278 return error_mark_node;
13280 if (!MAYBE_CLASS_TYPE_P (ctx))
13282 if (complain & tf_error)
13283 error ("%qT is not a class, struct, or union type", ctx);
13284 return error_mark_node;
13286 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13288 /* Normally, make_typename_type does not require that the CTX
13289 have complete type in order to allow things like:
13291 template <class T> struct S { typename S<T>::X Y; };
13293 But, such constructs have already been resolved by this
13294 point, so here CTX really should have complete type, unless
13295 it's a partial instantiation. */
13296 ctx = complete_type (ctx);
13297 if (!COMPLETE_TYPE_P (ctx))
13299 if (complain & tf_error)
13300 cxx_incomplete_type_error (NULL_TREE, ctx);
13301 return error_mark_node;
13305 f = make_typename_type (ctx, f, typename_type,
13306 complain | tf_keep_type_decl);
13307 if (f == error_mark_node)
13308 return f;
13309 if (TREE_CODE (f) == TYPE_DECL)
13311 complain |= tf_ignore_bad_quals;
13312 f = TREE_TYPE (f);
13315 if (TREE_CODE (f) != TYPENAME_TYPE)
13317 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13319 if (complain & tf_error)
13320 error ("%qT resolves to %qT, which is not an enumeration type",
13321 t, f);
13322 else
13323 return error_mark_node;
13325 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13327 if (complain & tf_error)
13328 error ("%qT resolves to %qT, which is is not a class type",
13329 t, f);
13330 else
13331 return error_mark_node;
13335 return cp_build_qualified_type_real
13336 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13339 case UNBOUND_CLASS_TEMPLATE:
13341 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13342 in_decl, /*entering_scope=*/1);
13343 tree name = TYPE_IDENTIFIER (t);
13344 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13346 if (ctx == error_mark_node || name == error_mark_node)
13347 return error_mark_node;
13349 if (parm_list)
13350 parm_list = tsubst_template_parms (parm_list, args, complain);
13351 return make_unbound_class_template (ctx, name, parm_list, complain);
13354 case TYPEOF_TYPE:
13356 tree type;
13358 ++cp_unevaluated_operand;
13359 ++c_inhibit_evaluation_warnings;
13361 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13362 complain, in_decl,
13363 /*integral_constant_expression_p=*/false);
13365 --cp_unevaluated_operand;
13366 --c_inhibit_evaluation_warnings;
13368 type = finish_typeof (type);
13369 return cp_build_qualified_type_real (type,
13370 cp_type_quals (t)
13371 | cp_type_quals (type),
13372 complain);
13375 case DECLTYPE_TYPE:
13377 tree type;
13379 ++cp_unevaluated_operand;
13380 ++c_inhibit_evaluation_warnings;
13382 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13383 complain|tf_decltype, in_decl,
13384 /*function_p*/false,
13385 /*integral_constant_expression*/false);
13387 --cp_unevaluated_operand;
13388 --c_inhibit_evaluation_warnings;
13390 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13391 type = lambda_capture_field_type (type,
13392 DECLTYPE_FOR_INIT_CAPTURE (t));
13393 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13394 type = lambda_proxy_type (type);
13395 else
13397 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13398 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13399 && EXPR_P (type))
13400 /* In a template ~id could be either a complement expression
13401 or an unqualified-id naming a destructor; if instantiating
13402 it produces an expression, it's not an id-expression or
13403 member access. */
13404 id = false;
13405 type = finish_decltype_type (type, id, complain);
13407 return cp_build_qualified_type_real (type,
13408 cp_type_quals (t)
13409 | cp_type_quals (type),
13410 complain | tf_ignore_bad_quals);
13413 case UNDERLYING_TYPE:
13415 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13416 complain, in_decl);
13417 return finish_underlying_type (type);
13420 case TYPE_ARGUMENT_PACK:
13421 case NONTYPE_ARGUMENT_PACK:
13423 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13424 tree packed_out =
13425 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13426 args,
13427 complain,
13428 in_decl);
13429 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13431 /* For template nontype argument packs, also substitute into
13432 the type. */
13433 if (code == NONTYPE_ARGUMENT_PACK)
13434 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13436 return r;
13438 break;
13440 case VOID_CST:
13441 case INTEGER_CST:
13442 case REAL_CST:
13443 case STRING_CST:
13444 case PLUS_EXPR:
13445 case MINUS_EXPR:
13446 case NEGATE_EXPR:
13447 case NOP_EXPR:
13448 case INDIRECT_REF:
13449 case ADDR_EXPR:
13450 case CALL_EXPR:
13451 case ARRAY_REF:
13452 case SCOPE_REF:
13453 /* We should use one of the expression tsubsts for these codes. */
13454 gcc_unreachable ();
13456 default:
13457 sorry ("use of %qs in template", get_tree_code_name (code));
13458 return error_mark_node;
13462 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13463 type of the expression on the left-hand side of the "." or "->"
13464 operator. */
13466 static tree
13467 tsubst_baselink (tree baselink, tree object_type,
13468 tree args, tsubst_flags_t complain, tree in_decl)
13470 tree name;
13471 tree qualifying_scope;
13472 tree fns;
13473 tree optype;
13474 tree template_args = 0;
13475 bool template_id_p = false;
13476 bool qualified = BASELINK_QUALIFIED_P (baselink);
13478 /* A baselink indicates a function from a base class. Both the
13479 BASELINK_ACCESS_BINFO and the base class referenced may
13480 indicate bases of the template class, rather than the
13481 instantiated class. In addition, lookups that were not
13482 ambiguous before may be ambiguous now. Therefore, we perform
13483 the lookup again. */
13484 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13485 qualifying_scope = tsubst (qualifying_scope, args,
13486 complain, in_decl);
13487 fns = BASELINK_FUNCTIONS (baselink);
13488 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13489 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13491 template_id_p = true;
13492 template_args = TREE_OPERAND (fns, 1);
13493 fns = TREE_OPERAND (fns, 0);
13494 if (template_args)
13495 template_args = tsubst_template_args (template_args, args,
13496 complain, in_decl);
13498 name = DECL_NAME (get_first_fn (fns));
13499 if (IDENTIFIER_TYPENAME_P (name))
13500 name = mangle_conv_op_name_for_type (optype);
13501 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13502 if (!baselink)
13503 return error_mark_node;
13505 /* If lookup found a single function, mark it as used at this
13506 point. (If it lookup found multiple functions the one selected
13507 later by overload resolution will be marked as used at that
13508 point.) */
13509 if (BASELINK_P (baselink))
13510 fns = BASELINK_FUNCTIONS (baselink);
13511 if (!template_id_p && !really_overloaded_fn (fns)
13512 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13513 return error_mark_node;
13515 /* Add back the template arguments, if present. */
13516 if (BASELINK_P (baselink) && template_id_p)
13517 BASELINK_FUNCTIONS (baselink)
13518 = build_nt (TEMPLATE_ID_EXPR,
13519 BASELINK_FUNCTIONS (baselink),
13520 template_args);
13521 /* Update the conversion operator type. */
13522 BASELINK_OPTYPE (baselink) = optype;
13524 if (!object_type)
13525 object_type = current_class_type;
13527 if (qualified)
13528 baselink = adjust_result_of_qualified_name_lookup (baselink,
13529 qualifying_scope,
13530 object_type);
13531 return baselink;
13534 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13535 true if the qualified-id will be a postfix-expression in-and-of
13536 itself; false if more of the postfix-expression follows the
13537 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13538 of "&". */
13540 static tree
13541 tsubst_qualified_id (tree qualified_id, tree args,
13542 tsubst_flags_t complain, tree in_decl,
13543 bool done, bool address_p)
13545 tree expr;
13546 tree scope;
13547 tree name;
13548 bool is_template;
13549 tree template_args;
13550 location_t loc = UNKNOWN_LOCATION;
13552 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13554 /* Figure out what name to look up. */
13555 name = TREE_OPERAND (qualified_id, 1);
13556 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13558 is_template = true;
13559 loc = EXPR_LOCATION (name);
13560 template_args = TREE_OPERAND (name, 1);
13561 if (template_args)
13562 template_args = tsubst_template_args (template_args, args,
13563 complain, in_decl);
13564 name = TREE_OPERAND (name, 0);
13566 else
13568 is_template = false;
13569 template_args = NULL_TREE;
13572 /* Substitute into the qualifying scope. When there are no ARGS, we
13573 are just trying to simplify a non-dependent expression. In that
13574 case the qualifying scope may be dependent, and, in any case,
13575 substituting will not help. */
13576 scope = TREE_OPERAND (qualified_id, 0);
13577 if (args)
13579 scope = tsubst (scope, args, complain, in_decl);
13580 expr = tsubst_copy (name, args, complain, in_decl);
13582 else
13583 expr = name;
13585 if (dependent_scope_p (scope))
13587 if (is_template)
13588 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13589 return build_qualified_name (NULL_TREE, scope, expr,
13590 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13593 if (!BASELINK_P (name) && !DECL_P (expr))
13595 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13597 /* A BIT_NOT_EXPR is used to represent a destructor. */
13598 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13600 error ("qualifying type %qT does not match destructor name ~%qT",
13601 scope, TREE_OPERAND (expr, 0));
13602 expr = error_mark_node;
13604 else
13605 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13606 /*is_type_p=*/0, false);
13608 else
13609 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13610 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13611 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13613 if (complain & tf_error)
13615 error ("dependent-name %qE is parsed as a non-type, but "
13616 "instantiation yields a type", qualified_id);
13617 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13619 return error_mark_node;
13623 if (DECL_P (expr))
13625 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13626 scope);
13627 /* Remember that there was a reference to this entity. */
13628 if (!mark_used (expr, complain) && !(complain & tf_error))
13629 return error_mark_node;
13632 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13634 if (complain & tf_error)
13635 qualified_name_lookup_error (scope,
13636 TREE_OPERAND (qualified_id, 1),
13637 expr, input_location);
13638 return error_mark_node;
13641 if (is_template)
13642 expr = lookup_template_function (expr, template_args);
13644 if (expr == error_mark_node && complain & tf_error)
13645 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
13646 expr, input_location);
13647 else if (TYPE_P (scope))
13649 expr = (adjust_result_of_qualified_name_lookup
13650 (expr, scope, current_nonlambda_class_type ()));
13651 expr = (finish_qualified_id_expr
13652 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
13653 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
13654 /*template_arg_p=*/false, complain));
13657 /* Expressions do not generally have reference type. */
13658 if (TREE_CODE (expr) != SCOPE_REF
13659 /* However, if we're about to form a pointer-to-member, we just
13660 want the referenced member referenced. */
13661 && TREE_CODE (expr) != OFFSET_REF)
13662 expr = convert_from_reference (expr);
13664 return expr;
13667 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13668 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13669 for tsubst. */
13671 static tree
13672 tsubst_init (tree init, tree decl, tree args,
13673 tsubst_flags_t complain, tree in_decl)
13675 if (!init)
13676 return NULL_TREE;
13678 init = tsubst_expr (init, args, complain, in_decl, false);
13680 if (!init)
13682 /* If we had an initializer but it
13683 instantiated to nothing,
13684 value-initialize the object. This will
13685 only occur when the initializer was a
13686 pack expansion where the parameter packs
13687 used in that expansion were of length
13688 zero. */
13689 init = build_value_init (TREE_TYPE (decl),
13690 complain);
13691 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13692 init = get_target_expr_sfinae (init, complain);
13695 return init;
13698 /* Like tsubst, but deals with expressions. This function just replaces
13699 template parms; to finish processing the resultant expression, use
13700 tsubst_copy_and_build or tsubst_expr. */
13702 static tree
13703 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13705 enum tree_code code;
13706 tree r;
13708 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
13709 return t;
13711 code = TREE_CODE (t);
13713 switch (code)
13715 case PARM_DECL:
13716 r = retrieve_local_specialization (t);
13718 if (r == NULL_TREE)
13720 /* We get here for a use of 'this' in an NSDMI. */
13721 if (DECL_NAME (t) == this_identifier
13722 && current_function_decl
13723 && DECL_CONSTRUCTOR_P (current_function_decl))
13724 return current_class_ptr;
13726 /* This can happen for a parameter name used later in a function
13727 declaration (such as in a late-specified return type). Just
13728 make a dummy decl, since it's only used for its type. */
13729 gcc_assert (cp_unevaluated_operand != 0);
13730 r = tsubst_decl (t, args, complain);
13731 /* Give it the template pattern as its context; its true context
13732 hasn't been instantiated yet and this is good enough for
13733 mangling. */
13734 DECL_CONTEXT (r) = DECL_CONTEXT (t);
13737 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13738 r = ARGUMENT_PACK_SELECT_ARG (r);
13739 if (!mark_used (r, complain) && !(complain & tf_error))
13740 return error_mark_node;
13741 return r;
13743 case CONST_DECL:
13745 tree enum_type;
13746 tree v;
13748 if (DECL_TEMPLATE_PARM_P (t))
13749 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
13750 /* There is no need to substitute into namespace-scope
13751 enumerators. */
13752 if (DECL_NAMESPACE_SCOPE_P (t))
13753 return t;
13754 /* If ARGS is NULL, then T is known to be non-dependent. */
13755 if (args == NULL_TREE)
13756 return scalar_constant_value (t);
13758 /* Unfortunately, we cannot just call lookup_name here.
13759 Consider:
13761 template <int I> int f() {
13762 enum E { a = I };
13763 struct S { void g() { E e = a; } };
13766 When we instantiate f<7>::S::g(), say, lookup_name is not
13767 clever enough to find f<7>::a. */
13768 enum_type
13769 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13770 /*entering_scope=*/0);
13772 for (v = TYPE_VALUES (enum_type);
13773 v != NULL_TREE;
13774 v = TREE_CHAIN (v))
13775 if (TREE_PURPOSE (v) == DECL_NAME (t))
13776 return TREE_VALUE (v);
13778 /* We didn't find the name. That should never happen; if
13779 name-lookup found it during preliminary parsing, we
13780 should find it again here during instantiation. */
13781 gcc_unreachable ();
13783 return t;
13785 case FIELD_DECL:
13786 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13788 /* Check for a local specialization set up by
13789 tsubst_pack_expansion. */
13790 if (tree r = retrieve_local_specialization (t))
13792 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13793 r = ARGUMENT_PACK_SELECT_ARG (r);
13794 return r;
13797 /* When retrieving a capture pack from a generic lambda, remove the
13798 lambda call op's own template argument list from ARGS. Only the
13799 template arguments active for the closure type should be used to
13800 retrieve the pack specialization. */
13801 if (LAMBDA_FUNCTION_P (current_function_decl)
13802 && (template_class_depth (DECL_CONTEXT (t))
13803 != TMPL_ARGS_DEPTH (args)))
13804 args = strip_innermost_template_args (args, 1);
13806 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13807 tsubst_decl put in the hash table. */
13808 return retrieve_specialization (t, args, 0);
13811 if (DECL_CONTEXT (t))
13813 tree ctx;
13815 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13816 /*entering_scope=*/1);
13817 if (ctx != DECL_CONTEXT (t))
13819 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
13820 if (!r)
13822 if (complain & tf_error)
13823 error ("using invalid field %qD", t);
13824 return error_mark_node;
13826 return r;
13830 return t;
13832 case VAR_DECL:
13833 case FUNCTION_DECL:
13834 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
13835 r = tsubst (t, args, complain, in_decl);
13836 else if (local_variable_p (t))
13838 r = retrieve_local_specialization (t);
13839 if (r == NULL_TREE)
13841 /* First try name lookup to find the instantiation. */
13842 r = lookup_name (DECL_NAME (t));
13843 if (r)
13845 /* Make sure that the one we found is the one we want. */
13846 tree ctx = DECL_CONTEXT (t);
13847 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
13848 ctx = tsubst (ctx, args, complain, in_decl);
13849 if (ctx != DECL_CONTEXT (r))
13850 r = NULL_TREE;
13853 if (r)
13854 /* OK */;
13855 else
13857 /* This can happen for a variable used in a
13858 late-specified return type of a local lambda, or for a
13859 local static or constant. Building a new VAR_DECL
13860 should be OK in all those cases. */
13861 r = tsubst_decl (t, args, complain);
13862 if (decl_maybe_constant_var_p (r))
13864 /* We can't call cp_finish_decl, so handle the
13865 initializer by hand. */
13866 tree init = tsubst_init (DECL_INITIAL (t), r, args,
13867 complain, in_decl);
13868 if (!processing_template_decl)
13869 init = maybe_constant_init (init);
13870 if (processing_template_decl
13871 ? potential_constant_expression (init)
13872 : reduced_constant_expression_p (init))
13873 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13874 = TREE_CONSTANT (r) = true;
13875 DECL_INITIAL (r) = init;
13877 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13878 || decl_constant_var_p (r)
13879 || errorcount || sorrycount);
13880 if (!processing_template_decl)
13882 if (TREE_STATIC (r))
13883 rest_of_decl_compilation (r, toplevel_bindings_p (),
13884 at_eof);
13885 else
13886 r = process_outer_var_ref (r, complain);
13889 /* Remember this for subsequent uses. */
13890 if (local_specializations)
13891 register_local_specialization (r, t);
13894 else
13895 r = t;
13896 if (!mark_used (r, complain) && !(complain & tf_error))
13897 return error_mark_node;
13898 return r;
13900 case NAMESPACE_DECL:
13901 return t;
13903 case OVERLOAD:
13904 /* An OVERLOAD will always be a non-dependent overload set; an
13905 overload set from function scope will just be represented with an
13906 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13907 gcc_assert (!uses_template_parms (t));
13908 return t;
13910 case BASELINK:
13911 return tsubst_baselink (t, current_nonlambda_class_type (),
13912 args, complain, in_decl);
13914 case TEMPLATE_DECL:
13915 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13916 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13917 args, complain, in_decl);
13918 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13919 return tsubst (t, args, complain, in_decl);
13920 else if (DECL_CLASS_SCOPE_P (t)
13921 && uses_template_parms (DECL_CONTEXT (t)))
13923 /* Template template argument like the following example need
13924 special treatment:
13926 template <template <class> class TT> struct C {};
13927 template <class T> struct D {
13928 template <class U> struct E {};
13929 C<E> c; // #1
13931 D<int> d; // #2
13933 We are processing the template argument `E' in #1 for
13934 the template instantiation #2. Originally, `E' is a
13935 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13936 have to substitute this with one having context `D<int>'. */
13938 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13939 return lookup_field (context, DECL_NAME(t), 0, false);
13941 else
13942 /* Ordinary template template argument. */
13943 return t;
13945 case CAST_EXPR:
13946 case REINTERPRET_CAST_EXPR:
13947 case CONST_CAST_EXPR:
13948 case STATIC_CAST_EXPR:
13949 case DYNAMIC_CAST_EXPR:
13950 case IMPLICIT_CONV_EXPR:
13951 case CONVERT_EXPR:
13952 case NOP_EXPR:
13954 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13955 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13956 return build1 (code, type, op0);
13959 case SIZEOF_EXPR:
13960 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13962 tree expanded, op = TREE_OPERAND (t, 0);
13963 int len = 0;
13965 if (SIZEOF_EXPR_TYPE_P (t))
13966 op = TREE_TYPE (op);
13968 ++cp_unevaluated_operand;
13969 ++c_inhibit_evaluation_warnings;
13970 /* We only want to compute the number of arguments. */
13971 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13972 --cp_unevaluated_operand;
13973 --c_inhibit_evaluation_warnings;
13975 if (TREE_CODE (expanded) == TREE_VEC)
13976 len = TREE_VEC_LENGTH (expanded);
13978 if (expanded == error_mark_node)
13979 return error_mark_node;
13980 else if (PACK_EXPANSION_P (expanded)
13981 || (TREE_CODE (expanded) == TREE_VEC
13982 && len > 0
13983 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13985 if (TREE_CODE (expanded) == TREE_VEC)
13986 expanded = TREE_VEC_ELT (expanded, len - 1);
13987 else
13988 PACK_EXPANSION_SIZEOF_P (expanded) = true;
13990 if (TYPE_P (expanded))
13991 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13992 complain & tf_error);
13993 else
13994 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13995 complain & tf_error);
13997 else
13998 return build_int_cst (size_type_node, len);
14000 if (SIZEOF_EXPR_TYPE_P (t))
14002 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14003 args, complain, in_decl);
14004 r = build1 (NOP_EXPR, r, error_mark_node);
14005 r = build1 (SIZEOF_EXPR,
14006 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14007 SIZEOF_EXPR_TYPE_P (r) = 1;
14008 return r;
14010 /* Fall through */
14012 case INDIRECT_REF:
14013 case NEGATE_EXPR:
14014 case TRUTH_NOT_EXPR:
14015 case BIT_NOT_EXPR:
14016 case ADDR_EXPR:
14017 case UNARY_PLUS_EXPR: /* Unary + */
14018 case ALIGNOF_EXPR:
14019 case AT_ENCODE_EXPR:
14020 case ARROW_EXPR:
14021 case THROW_EXPR:
14022 case TYPEID_EXPR:
14023 case REALPART_EXPR:
14024 case IMAGPART_EXPR:
14025 case PAREN_EXPR:
14027 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14028 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14029 return build1 (code, type, op0);
14032 case COMPONENT_REF:
14034 tree object;
14035 tree name;
14037 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14038 name = TREE_OPERAND (t, 1);
14039 if (TREE_CODE (name) == BIT_NOT_EXPR)
14041 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14042 complain, in_decl);
14043 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14045 else if (TREE_CODE (name) == SCOPE_REF
14046 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14048 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14049 complain, in_decl);
14050 name = TREE_OPERAND (name, 1);
14051 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14052 complain, in_decl);
14053 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14054 name = build_qualified_name (/*type=*/NULL_TREE,
14055 base, name,
14056 /*template_p=*/false);
14058 else if (BASELINK_P (name))
14059 name = tsubst_baselink (name,
14060 non_reference (TREE_TYPE (object)),
14061 args, complain,
14062 in_decl);
14063 else
14064 name = tsubst_copy (name, args, complain, in_decl);
14065 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14068 case PLUS_EXPR:
14069 case MINUS_EXPR:
14070 case MULT_EXPR:
14071 case TRUNC_DIV_EXPR:
14072 case CEIL_DIV_EXPR:
14073 case FLOOR_DIV_EXPR:
14074 case ROUND_DIV_EXPR:
14075 case EXACT_DIV_EXPR:
14076 case BIT_AND_EXPR:
14077 case BIT_IOR_EXPR:
14078 case BIT_XOR_EXPR:
14079 case TRUNC_MOD_EXPR:
14080 case FLOOR_MOD_EXPR:
14081 case TRUTH_ANDIF_EXPR:
14082 case TRUTH_ORIF_EXPR:
14083 case TRUTH_AND_EXPR:
14084 case TRUTH_OR_EXPR:
14085 case RSHIFT_EXPR:
14086 case LSHIFT_EXPR:
14087 case RROTATE_EXPR:
14088 case LROTATE_EXPR:
14089 case EQ_EXPR:
14090 case NE_EXPR:
14091 case MAX_EXPR:
14092 case MIN_EXPR:
14093 case LE_EXPR:
14094 case GE_EXPR:
14095 case LT_EXPR:
14096 case GT_EXPR:
14097 case COMPOUND_EXPR:
14098 case DOTSTAR_EXPR:
14099 case MEMBER_REF:
14100 case PREDECREMENT_EXPR:
14101 case PREINCREMENT_EXPR:
14102 case POSTDECREMENT_EXPR:
14103 case POSTINCREMENT_EXPR:
14105 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14106 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14107 return build_nt (code, op0, op1);
14110 case SCOPE_REF:
14112 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14113 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14114 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14115 QUALIFIED_NAME_IS_TEMPLATE (t));
14118 case ARRAY_REF:
14120 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14121 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14122 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14125 case CALL_EXPR:
14127 int n = VL_EXP_OPERAND_LENGTH (t);
14128 tree result = build_vl_exp (CALL_EXPR, n);
14129 int i;
14130 for (i = 0; i < n; i++)
14131 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14132 complain, in_decl);
14133 return result;
14136 case COND_EXPR:
14137 case MODOP_EXPR:
14138 case PSEUDO_DTOR_EXPR:
14139 case VEC_PERM_EXPR:
14141 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14142 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14143 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14144 r = build_nt (code, op0, op1, op2);
14145 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14146 return r;
14149 case NEW_EXPR:
14151 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14152 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14153 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14154 r = build_nt (code, op0, op1, op2);
14155 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14156 return r;
14159 case DELETE_EXPR:
14161 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14162 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14163 r = build_nt (code, op0, op1);
14164 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14165 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14166 return r;
14169 case TEMPLATE_ID_EXPR:
14171 /* Substituted template arguments */
14172 tree fn = TREE_OPERAND (t, 0);
14173 tree targs = TREE_OPERAND (t, 1);
14175 fn = tsubst_copy (fn, args, complain, in_decl);
14176 if (targs)
14177 targs = tsubst_template_args (targs, args, complain, in_decl);
14179 return lookup_template_function (fn, targs);
14182 case TREE_LIST:
14184 tree purpose, value, chain;
14186 if (t == void_list_node)
14187 return t;
14189 purpose = TREE_PURPOSE (t);
14190 if (purpose)
14191 purpose = tsubst_copy (purpose, args, complain, in_decl);
14192 value = TREE_VALUE (t);
14193 if (value)
14194 value = tsubst_copy (value, args, complain, in_decl);
14195 chain = TREE_CHAIN (t);
14196 if (chain && chain != void_type_node)
14197 chain = tsubst_copy (chain, args, complain, in_decl);
14198 if (purpose == TREE_PURPOSE (t)
14199 && value == TREE_VALUE (t)
14200 && chain == TREE_CHAIN (t))
14201 return t;
14202 return tree_cons (purpose, value, chain);
14205 case RECORD_TYPE:
14206 case UNION_TYPE:
14207 case ENUMERAL_TYPE:
14208 case INTEGER_TYPE:
14209 case TEMPLATE_TYPE_PARM:
14210 case TEMPLATE_TEMPLATE_PARM:
14211 case BOUND_TEMPLATE_TEMPLATE_PARM:
14212 case TEMPLATE_PARM_INDEX:
14213 case POINTER_TYPE:
14214 case REFERENCE_TYPE:
14215 case OFFSET_TYPE:
14216 case FUNCTION_TYPE:
14217 case METHOD_TYPE:
14218 case ARRAY_TYPE:
14219 case TYPENAME_TYPE:
14220 case UNBOUND_CLASS_TEMPLATE:
14221 case TYPEOF_TYPE:
14222 case DECLTYPE_TYPE:
14223 case TYPE_DECL:
14224 return tsubst (t, args, complain, in_decl);
14226 case USING_DECL:
14227 t = DECL_NAME (t);
14228 /* Fall through. */
14229 case IDENTIFIER_NODE:
14230 if (IDENTIFIER_TYPENAME_P (t))
14232 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14233 return mangle_conv_op_name_for_type (new_type);
14235 else
14236 return t;
14238 case CONSTRUCTOR:
14239 /* This is handled by tsubst_copy_and_build. */
14240 gcc_unreachable ();
14242 case VA_ARG_EXPR:
14244 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14245 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14246 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14249 case CLEANUP_POINT_EXPR:
14250 /* We shouldn't have built any of these during initial template
14251 generation. Instead, they should be built during instantiation
14252 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14253 gcc_unreachable ();
14255 case OFFSET_REF:
14257 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14258 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14259 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14260 r = build2 (code, type, op0, op1);
14261 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14262 if (!mark_used (TREE_OPERAND (r, 1), complain)
14263 && !(complain & tf_error))
14264 return error_mark_node;
14265 return r;
14268 case EXPR_PACK_EXPANSION:
14269 error ("invalid use of pack expansion expression");
14270 return error_mark_node;
14272 case NONTYPE_ARGUMENT_PACK:
14273 error ("use %<...%> to expand argument pack");
14274 return error_mark_node;
14276 case VOID_CST:
14277 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14278 return t;
14280 case INTEGER_CST:
14281 case REAL_CST:
14282 case STRING_CST:
14283 case COMPLEX_CST:
14285 /* Instantiate any typedefs in the type. */
14286 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14287 r = fold_convert (type, t);
14288 gcc_assert (TREE_CODE (r) == code);
14289 return r;
14292 case PTRMEM_CST:
14293 /* These can sometimes show up in a partial instantiation, but never
14294 involve template parms. */
14295 gcc_assert (!uses_template_parms (t));
14296 return t;
14298 case UNARY_LEFT_FOLD_EXPR:
14299 return tsubst_unary_left_fold (t, args, complain, in_decl);
14300 case UNARY_RIGHT_FOLD_EXPR:
14301 return tsubst_unary_right_fold (t, args, complain, in_decl);
14302 case BINARY_LEFT_FOLD_EXPR:
14303 return tsubst_binary_left_fold (t, args, complain, in_decl);
14304 case BINARY_RIGHT_FOLD_EXPR:
14305 return tsubst_binary_right_fold (t, args, complain, in_decl);
14307 default:
14308 /* We shouldn't get here, but keep going if !flag_checking. */
14309 if (flag_checking)
14310 gcc_unreachable ();
14311 return t;
14315 /* Helper function for tsubst_omp_clauses, used for instantiation of
14316 OMP_CLAUSE_DECL of clauses. */
14318 static tree
14319 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14320 tree in_decl)
14322 if (decl == NULL_TREE)
14323 return NULL_TREE;
14325 /* Handle an OpenMP array section represented as a TREE_LIST (or
14326 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14327 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14328 TREE_LIST. We can handle it exactly the same as an array section
14329 (purpose, value, and a chain), even though the nomenclature
14330 (low_bound, length, etc) is different. */
14331 if (TREE_CODE (decl) == TREE_LIST)
14333 tree low_bound
14334 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14335 /*integral_constant_expression_p=*/false);
14336 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14337 /*integral_constant_expression_p=*/false);
14338 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14339 in_decl);
14340 if (TREE_PURPOSE (decl) == low_bound
14341 && TREE_VALUE (decl) == length
14342 && TREE_CHAIN (decl) == chain)
14343 return decl;
14344 tree ret = tree_cons (low_bound, length, chain);
14345 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
14346 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
14347 return ret;
14349 tree ret = tsubst_expr (decl, args, complain, in_decl,
14350 /*integral_constant_expression_p=*/false);
14351 /* Undo convert_from_reference tsubst_expr could have called. */
14352 if (decl
14353 && REFERENCE_REF_P (ret)
14354 && !REFERENCE_REF_P (decl))
14355 ret = TREE_OPERAND (ret, 0);
14356 return ret;
14359 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14361 static tree
14362 tsubst_omp_clauses (tree clauses, bool declare_simd, bool allow_fields,
14363 tree args, tsubst_flags_t complain, tree in_decl)
14365 tree new_clauses = NULL_TREE, nc, oc;
14366 tree linear_no_step = NULL_TREE;
14368 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14370 nc = copy_node (oc);
14371 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14372 new_clauses = nc;
14374 switch (OMP_CLAUSE_CODE (nc))
14376 case OMP_CLAUSE_LASTPRIVATE:
14377 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14379 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14380 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14381 in_decl, /*integral_constant_expression_p=*/false);
14382 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14383 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14385 /* FALLTHRU */
14386 case OMP_CLAUSE_PRIVATE:
14387 case OMP_CLAUSE_SHARED:
14388 case OMP_CLAUSE_FIRSTPRIVATE:
14389 case OMP_CLAUSE_COPYIN:
14390 case OMP_CLAUSE_COPYPRIVATE:
14391 case OMP_CLAUSE_UNIFORM:
14392 case OMP_CLAUSE_DEPEND:
14393 case OMP_CLAUSE_FROM:
14394 case OMP_CLAUSE_TO:
14395 case OMP_CLAUSE_MAP:
14396 case OMP_CLAUSE_USE_DEVICE:
14397 case OMP_CLAUSE_USE_DEVICE_PTR:
14398 case OMP_CLAUSE_IS_DEVICE_PTR:
14399 OMP_CLAUSE_DECL (nc)
14400 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14401 in_decl);
14402 break;
14403 case OMP_CLAUSE_IF:
14404 case OMP_CLAUSE_NUM_THREADS:
14405 case OMP_CLAUSE_SCHEDULE:
14406 case OMP_CLAUSE_COLLAPSE:
14407 case OMP_CLAUSE_FINAL:
14408 case OMP_CLAUSE_DEVICE:
14409 case OMP_CLAUSE_DIST_SCHEDULE:
14410 case OMP_CLAUSE_NUM_TEAMS:
14411 case OMP_CLAUSE_THREAD_LIMIT:
14412 case OMP_CLAUSE_SAFELEN:
14413 case OMP_CLAUSE_SIMDLEN:
14414 case OMP_CLAUSE_NUM_TASKS:
14415 case OMP_CLAUSE_GRAINSIZE:
14416 case OMP_CLAUSE_PRIORITY:
14417 case OMP_CLAUSE_ORDERED:
14418 case OMP_CLAUSE_HINT:
14419 case OMP_CLAUSE_NUM_GANGS:
14420 case OMP_CLAUSE_NUM_WORKERS:
14421 case OMP_CLAUSE_VECTOR_LENGTH:
14422 case OMP_CLAUSE_WORKER:
14423 case OMP_CLAUSE_VECTOR:
14424 case OMP_CLAUSE_ASYNC:
14425 case OMP_CLAUSE_WAIT:
14426 OMP_CLAUSE_OPERAND (nc, 0)
14427 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14428 in_decl, /*integral_constant_expression_p=*/false);
14429 break;
14430 case OMP_CLAUSE_REDUCTION:
14431 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14433 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14434 if (TREE_CODE (placeholder) == SCOPE_REF)
14436 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14437 complain, in_decl);
14438 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14439 = build_qualified_name (NULL_TREE, scope,
14440 TREE_OPERAND (placeholder, 1),
14441 false);
14443 else
14444 gcc_assert (identifier_p (placeholder));
14446 OMP_CLAUSE_DECL (nc)
14447 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14448 in_decl);
14449 break;
14450 case OMP_CLAUSE_GANG:
14451 case OMP_CLAUSE_ALIGNED:
14452 OMP_CLAUSE_DECL (nc)
14453 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14454 in_decl);
14455 OMP_CLAUSE_OPERAND (nc, 1)
14456 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14457 in_decl, /*integral_constant_expression_p=*/false);
14458 break;
14459 case OMP_CLAUSE_LINEAR:
14460 OMP_CLAUSE_DECL (nc)
14461 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14462 in_decl);
14463 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
14465 gcc_assert (!linear_no_step);
14466 linear_no_step = nc;
14468 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
14469 OMP_CLAUSE_LINEAR_STEP (nc)
14470 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
14471 complain, in_decl);
14472 else
14473 OMP_CLAUSE_LINEAR_STEP (nc)
14474 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
14475 in_decl,
14476 /*integral_constant_expression_p=*/false);
14477 break;
14478 case OMP_CLAUSE_NOWAIT:
14479 case OMP_CLAUSE_DEFAULT:
14480 case OMP_CLAUSE_UNTIED:
14481 case OMP_CLAUSE_MERGEABLE:
14482 case OMP_CLAUSE_INBRANCH:
14483 case OMP_CLAUSE_NOTINBRANCH:
14484 case OMP_CLAUSE_PROC_BIND:
14485 case OMP_CLAUSE_FOR:
14486 case OMP_CLAUSE_PARALLEL:
14487 case OMP_CLAUSE_SECTIONS:
14488 case OMP_CLAUSE_TASKGROUP:
14489 case OMP_CLAUSE_NOGROUP:
14490 case OMP_CLAUSE_THREADS:
14491 case OMP_CLAUSE_SIMD:
14492 case OMP_CLAUSE_DEFAULTMAP:
14493 case OMP_CLAUSE_INDEPENDENT:
14494 case OMP_CLAUSE_AUTO:
14495 case OMP_CLAUSE_SEQ:
14496 break;
14497 case OMP_CLAUSE_TILE:
14499 tree lnc, loc;
14500 for (lnc = OMP_CLAUSE_TILE_LIST (nc),
14501 loc = OMP_CLAUSE_TILE_LIST (oc);
14502 loc;
14503 loc = TREE_CHAIN (loc), lnc = TREE_CHAIN (lnc))
14505 TREE_VALUE (lnc) = tsubst_expr (TREE_VALUE (loc), args,
14506 complain, in_decl, false);
14509 break;
14510 default:
14511 gcc_unreachable ();
14513 if (allow_fields)
14514 switch (OMP_CLAUSE_CODE (nc))
14516 case OMP_CLAUSE_SHARED:
14517 case OMP_CLAUSE_PRIVATE:
14518 case OMP_CLAUSE_FIRSTPRIVATE:
14519 case OMP_CLAUSE_LASTPRIVATE:
14520 case OMP_CLAUSE_COPYPRIVATE:
14521 case OMP_CLAUSE_LINEAR:
14522 case OMP_CLAUSE_REDUCTION:
14523 case OMP_CLAUSE_USE_DEVICE:
14524 case OMP_CLAUSE_USE_DEVICE_PTR:
14525 case OMP_CLAUSE_IS_DEVICE_PTR:
14526 /* tsubst_expr on SCOPE_REF results in returning
14527 finish_non_static_data_member result. Undo that here. */
14528 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
14529 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
14530 == IDENTIFIER_NODE))
14532 tree t = OMP_CLAUSE_DECL (nc);
14533 tree v = t;
14534 while (v)
14535 switch (TREE_CODE (v))
14537 case COMPONENT_REF:
14538 case MEM_REF:
14539 case INDIRECT_REF:
14540 CASE_CONVERT:
14541 case POINTER_PLUS_EXPR:
14542 v = TREE_OPERAND (v, 0);
14543 continue;
14544 case PARM_DECL:
14545 if (DECL_CONTEXT (v) == current_function_decl
14546 && DECL_ARTIFICIAL (v)
14547 && DECL_NAME (v) == this_identifier)
14548 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
14549 /* FALLTHRU */
14550 default:
14551 v = NULL_TREE;
14552 break;
14555 else if (VAR_P (OMP_CLAUSE_DECL (oc))
14556 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
14557 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
14558 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
14559 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
14561 tree decl = OMP_CLAUSE_DECL (nc);
14562 if (VAR_P (decl))
14564 if (!DECL_LANG_SPECIFIC (decl))
14565 retrofit_lang_decl (decl);
14566 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
14569 break;
14570 default:
14571 break;
14575 new_clauses = nreverse (new_clauses);
14576 if (!declare_simd)
14578 new_clauses = finish_omp_clauses (new_clauses, allow_fields);
14579 if (linear_no_step)
14580 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
14581 if (nc == linear_no_step)
14583 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
14584 break;
14587 return new_clauses;
14590 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14592 static tree
14593 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14594 tree in_decl)
14596 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14598 tree purpose, value, chain;
14600 if (t == NULL)
14601 return t;
14603 if (TREE_CODE (t) != TREE_LIST)
14604 return tsubst_copy_and_build (t, args, complain, in_decl,
14605 /*function_p=*/false,
14606 /*integral_constant_expression_p=*/false);
14608 if (t == void_list_node)
14609 return t;
14611 purpose = TREE_PURPOSE (t);
14612 if (purpose)
14613 purpose = RECUR (purpose);
14614 value = TREE_VALUE (t);
14615 if (value)
14617 if (TREE_CODE (value) != LABEL_DECL)
14618 value = RECUR (value);
14619 else
14621 value = lookup_label (DECL_NAME (value));
14622 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14623 TREE_USED (value) = 1;
14626 chain = TREE_CHAIN (t);
14627 if (chain && chain != void_type_node)
14628 chain = RECUR (chain);
14629 return tree_cons (purpose, value, chain);
14630 #undef RECUR
14633 /* Used to temporarily communicate the list of #pragma omp parallel
14634 clauses to #pragma omp for instantiation if they are combined
14635 together. */
14637 static tree *omp_parallel_combined_clauses;
14639 /* Substitute one OMP_FOR iterator. */
14641 static void
14642 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
14643 tree initv, tree condv, tree incrv, tree *clauses,
14644 tree args, tsubst_flags_t complain, tree in_decl,
14645 bool integral_constant_expression_p)
14647 #define RECUR(NODE) \
14648 tsubst_expr ((NODE), args, complain, in_decl, \
14649 integral_constant_expression_p)
14650 tree decl, init, cond, incr;
14652 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
14653 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
14655 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
14657 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
14658 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
14661 decl = TREE_OPERAND (init, 0);
14662 init = TREE_OPERAND (init, 1);
14663 tree decl_expr = NULL_TREE;
14664 if (init && TREE_CODE (init) == DECL_EXPR)
14666 /* We need to jump through some hoops to handle declarations in the
14667 for-init-statement, since we might need to handle auto deduction,
14668 but we need to keep control of initialization. */
14669 decl_expr = init;
14670 init = DECL_INITIAL (DECL_EXPR_DECL (init));
14671 decl = tsubst_decl (decl, args, complain);
14673 else
14675 if (TREE_CODE (decl) == SCOPE_REF)
14677 decl = RECUR (decl);
14678 if (TREE_CODE (decl) == COMPONENT_REF)
14680 tree v = decl;
14681 while (v)
14682 switch (TREE_CODE (v))
14684 case COMPONENT_REF:
14685 case MEM_REF:
14686 case INDIRECT_REF:
14687 CASE_CONVERT:
14688 case POINTER_PLUS_EXPR:
14689 v = TREE_OPERAND (v, 0);
14690 continue;
14691 case PARM_DECL:
14692 if (DECL_CONTEXT (v) == current_function_decl
14693 && DECL_ARTIFICIAL (v)
14694 && DECL_NAME (v) == this_identifier)
14696 decl = TREE_OPERAND (decl, 1);
14697 decl = omp_privatize_field (decl, false);
14699 /* FALLTHRU */
14700 default:
14701 v = NULL_TREE;
14702 break;
14706 else
14707 decl = RECUR (decl);
14709 init = RECUR (init);
14711 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14712 if (auto_node && init)
14713 TREE_TYPE (decl)
14714 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
14716 gcc_assert (!type_dependent_expression_p (decl));
14718 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14720 if (decl_expr)
14722 /* Declare the variable, but don't let that initialize it. */
14723 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
14724 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
14725 RECUR (decl_expr);
14726 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
14729 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
14730 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14731 if (TREE_CODE (incr) == MODIFY_EXPR)
14733 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14734 tree rhs = RECUR (TREE_OPERAND (incr, 1));
14735 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
14736 NOP_EXPR, rhs, complain);
14738 else
14739 incr = RECUR (incr);
14740 TREE_VEC_ELT (declv, i) = decl;
14741 TREE_VEC_ELT (initv, i) = init;
14742 TREE_VEC_ELT (condv, i) = cond;
14743 TREE_VEC_ELT (incrv, i) = incr;
14744 return;
14747 if (decl_expr)
14749 /* Declare and initialize the variable. */
14750 RECUR (decl_expr);
14751 init = NULL_TREE;
14753 else if (init)
14755 tree *pc;
14756 int j;
14757 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
14759 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
14761 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
14762 && OMP_CLAUSE_DECL (*pc) == decl)
14763 break;
14764 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
14765 && OMP_CLAUSE_DECL (*pc) == decl)
14767 if (j)
14768 break;
14769 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14770 tree c = *pc;
14771 *pc = OMP_CLAUSE_CHAIN (c);
14772 OMP_CLAUSE_CHAIN (c) = *clauses;
14773 *clauses = c;
14775 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
14776 && OMP_CLAUSE_DECL (*pc) == decl)
14778 error ("iteration variable %qD should not be firstprivate",
14779 decl);
14780 *pc = OMP_CLAUSE_CHAIN (*pc);
14782 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
14783 && OMP_CLAUSE_DECL (*pc) == decl)
14785 error ("iteration variable %qD should not be reduction",
14786 decl);
14787 *pc = OMP_CLAUSE_CHAIN (*pc);
14789 else
14790 pc = &OMP_CLAUSE_CHAIN (*pc);
14792 if (*pc)
14793 break;
14795 if (*pc == NULL_TREE)
14797 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14798 OMP_CLAUSE_DECL (c) = decl;
14799 c = finish_omp_clauses (c, true);
14800 if (c)
14802 OMP_CLAUSE_CHAIN (c) = *clauses;
14803 *clauses = c;
14807 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
14808 if (COMPARISON_CLASS_P (cond))
14810 tree op0 = RECUR (TREE_OPERAND (cond, 0));
14811 tree op1 = RECUR (TREE_OPERAND (cond, 1));
14812 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
14814 else
14815 cond = RECUR (cond);
14816 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14817 switch (TREE_CODE (incr))
14819 case PREINCREMENT_EXPR:
14820 case PREDECREMENT_EXPR:
14821 case POSTINCREMENT_EXPR:
14822 case POSTDECREMENT_EXPR:
14823 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
14824 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
14825 break;
14826 case MODIFY_EXPR:
14827 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14828 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14830 tree rhs = TREE_OPERAND (incr, 1);
14831 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14832 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14833 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14834 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14835 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14836 rhs0, rhs1));
14838 else
14839 incr = RECUR (incr);
14840 break;
14841 case MODOP_EXPR:
14842 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14843 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14845 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14846 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14847 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
14848 TREE_TYPE (decl), lhs,
14849 RECUR (TREE_OPERAND (incr, 2))));
14851 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
14852 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
14853 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
14855 tree rhs = TREE_OPERAND (incr, 2);
14856 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14857 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14858 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14859 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14860 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14861 rhs0, rhs1));
14863 else
14864 incr = RECUR (incr);
14865 break;
14866 default:
14867 incr = RECUR (incr);
14868 break;
14871 TREE_VEC_ELT (declv, i) = decl;
14872 TREE_VEC_ELT (initv, i) = init;
14873 TREE_VEC_ELT (condv, i) = cond;
14874 TREE_VEC_ELT (incrv, i) = incr;
14875 #undef RECUR
14878 /* Helper function of tsubst_expr, find OMP_TEAMS inside
14879 of OMP_TARGET's body. */
14881 static tree
14882 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
14884 *walk_subtrees = 0;
14885 switch (TREE_CODE (*tp))
14887 case OMP_TEAMS:
14888 return *tp;
14889 case BIND_EXPR:
14890 case STATEMENT_LIST:
14891 *walk_subtrees = 1;
14892 break;
14893 default:
14894 break;
14896 return NULL_TREE;
14899 /* Like tsubst_copy for expressions, etc. but also does semantic
14900 processing. */
14902 tree
14903 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
14904 bool integral_constant_expression_p)
14906 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14907 #define RECUR(NODE) \
14908 tsubst_expr ((NODE), args, complain, in_decl, \
14909 integral_constant_expression_p)
14911 tree stmt, tmp;
14912 tree r;
14913 location_t loc;
14915 if (t == NULL_TREE || t == error_mark_node)
14916 return t;
14918 loc = input_location;
14919 if (EXPR_HAS_LOCATION (t))
14920 input_location = EXPR_LOCATION (t);
14921 if (STATEMENT_CODE_P (TREE_CODE (t)))
14922 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
14924 switch (TREE_CODE (t))
14926 case STATEMENT_LIST:
14928 tree_stmt_iterator i;
14929 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
14930 RECUR (tsi_stmt (i));
14931 break;
14934 case CTOR_INITIALIZER:
14935 finish_mem_initializers (tsubst_initializer_list
14936 (TREE_OPERAND (t, 0), args));
14937 break;
14939 case RETURN_EXPR:
14940 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
14941 break;
14943 case EXPR_STMT:
14944 tmp = RECUR (EXPR_STMT_EXPR (t));
14945 if (EXPR_STMT_STMT_EXPR_RESULT (t))
14946 finish_stmt_expr_expr (tmp, cur_stmt_expr);
14947 else
14948 finish_expr_stmt (tmp);
14949 break;
14951 case USING_STMT:
14952 do_using_directive (USING_STMT_NAMESPACE (t));
14953 break;
14955 case DECL_EXPR:
14957 tree decl, pattern_decl;
14958 tree init;
14960 pattern_decl = decl = DECL_EXPR_DECL (t);
14961 if (TREE_CODE (decl) == LABEL_DECL)
14962 finish_label_decl (DECL_NAME (decl));
14963 else if (TREE_CODE (decl) == USING_DECL)
14965 tree scope = USING_DECL_SCOPE (decl);
14966 tree name = DECL_NAME (decl);
14967 tree decl;
14969 scope = tsubst (scope, args, complain, in_decl);
14970 decl = lookup_qualified_name (scope, name,
14971 /*is_type_p=*/false,
14972 /*complain=*/false);
14973 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
14974 qualified_name_lookup_error (scope, name, decl, input_location);
14975 else
14976 do_local_using_decl (decl, scope, name);
14978 else if (DECL_PACK_P (decl))
14980 /* Don't build up decls for a variadic capture proxy, we'll
14981 instantiate the elements directly as needed. */
14982 break;
14984 else
14986 init = DECL_INITIAL (decl);
14987 decl = tsubst (decl, args, complain, in_decl);
14988 if (decl != error_mark_node)
14990 /* By marking the declaration as instantiated, we avoid
14991 trying to instantiate it. Since instantiate_decl can't
14992 handle local variables, and since we've already done
14993 all that needs to be done, that's the right thing to
14994 do. */
14995 if (VAR_P (decl))
14996 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14997 if (VAR_P (decl)
14998 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
14999 /* Anonymous aggregates are a special case. */
15000 finish_anon_union (decl);
15001 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15003 DECL_CONTEXT (decl) = current_function_decl;
15004 if (DECL_NAME (decl) == this_identifier)
15006 tree lam = DECL_CONTEXT (current_function_decl);
15007 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15008 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15010 insert_capture_proxy (decl);
15012 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15013 /* We already did a pushtag. */;
15014 else if (TREE_CODE (decl) == FUNCTION_DECL
15015 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15016 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15018 DECL_CONTEXT (decl) = NULL_TREE;
15019 pushdecl (decl);
15020 DECL_CONTEXT (decl) = current_function_decl;
15021 cp_check_omp_declare_reduction (decl);
15023 else
15025 int const_init = false;
15026 maybe_push_decl (decl);
15027 if (VAR_P (decl)
15028 && DECL_PRETTY_FUNCTION_P (decl))
15030 /* For __PRETTY_FUNCTION__ we have to adjust the
15031 initializer. */
15032 const char *const name
15033 = cxx_printable_name (current_function_decl, 2);
15034 init = cp_fname_init (name, &TREE_TYPE (decl));
15036 else
15037 init = tsubst_init (init, decl, args, complain, in_decl);
15039 if (VAR_P (decl))
15040 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15041 (pattern_decl));
15042 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15047 break;
15050 case FOR_STMT:
15051 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15052 RECUR (FOR_INIT_STMT (t));
15053 finish_for_init_stmt (stmt);
15054 tmp = RECUR (FOR_COND (t));
15055 finish_for_cond (tmp, stmt, false);
15056 tmp = RECUR (FOR_EXPR (t));
15057 finish_for_expr (tmp, stmt);
15058 RECUR (FOR_BODY (t));
15059 finish_for_stmt (stmt);
15060 break;
15062 case RANGE_FOR_STMT:
15064 tree decl, expr;
15065 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15066 decl = RANGE_FOR_DECL (t);
15067 decl = tsubst (decl, args, complain, in_decl);
15068 maybe_push_decl (decl);
15069 expr = RECUR (RANGE_FOR_EXPR (t));
15070 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
15071 RECUR (RANGE_FOR_BODY (t));
15072 finish_for_stmt (stmt);
15074 break;
15076 case WHILE_STMT:
15077 stmt = begin_while_stmt ();
15078 tmp = RECUR (WHILE_COND (t));
15079 finish_while_stmt_cond (tmp, stmt, false);
15080 RECUR (WHILE_BODY (t));
15081 finish_while_stmt (stmt);
15082 break;
15084 case DO_STMT:
15085 stmt = begin_do_stmt ();
15086 RECUR (DO_BODY (t));
15087 finish_do_body (stmt);
15088 tmp = RECUR (DO_COND (t));
15089 finish_do_stmt (tmp, stmt, false);
15090 break;
15092 case IF_STMT:
15093 stmt = begin_if_stmt ();
15094 tmp = RECUR (IF_COND (t));
15095 finish_if_stmt_cond (tmp, stmt);
15096 RECUR (THEN_CLAUSE (t));
15097 finish_then_clause (stmt);
15099 if (ELSE_CLAUSE (t))
15101 begin_else_clause (stmt);
15102 RECUR (ELSE_CLAUSE (t));
15103 finish_else_clause (stmt);
15106 finish_if_stmt (stmt);
15107 break;
15109 case BIND_EXPR:
15110 if (BIND_EXPR_BODY_BLOCK (t))
15111 stmt = begin_function_body ();
15112 else
15113 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15114 ? BCS_TRY_BLOCK : 0);
15116 RECUR (BIND_EXPR_BODY (t));
15118 if (BIND_EXPR_BODY_BLOCK (t))
15119 finish_function_body (stmt);
15120 else
15121 finish_compound_stmt (stmt);
15122 break;
15124 case BREAK_STMT:
15125 finish_break_stmt ();
15126 break;
15128 case CONTINUE_STMT:
15129 finish_continue_stmt ();
15130 break;
15132 case SWITCH_STMT:
15133 stmt = begin_switch_stmt ();
15134 tmp = RECUR (SWITCH_STMT_COND (t));
15135 finish_switch_cond (tmp, stmt);
15136 RECUR (SWITCH_STMT_BODY (t));
15137 finish_switch_stmt (stmt);
15138 break;
15140 case CASE_LABEL_EXPR:
15142 tree low = RECUR (CASE_LOW (t));
15143 tree high = RECUR (CASE_HIGH (t));
15144 finish_case_label (EXPR_LOCATION (t), low, high);
15146 break;
15148 case LABEL_EXPR:
15150 tree decl = LABEL_EXPR_LABEL (t);
15151 tree label;
15153 label = finish_label_stmt (DECL_NAME (decl));
15154 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15155 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15157 break;
15159 case GOTO_EXPR:
15160 tmp = GOTO_DESTINATION (t);
15161 if (TREE_CODE (tmp) != LABEL_DECL)
15162 /* Computed goto's must be tsubst'd into. On the other hand,
15163 non-computed gotos must not be; the identifier in question
15164 will have no binding. */
15165 tmp = RECUR (tmp);
15166 else
15167 tmp = DECL_NAME (tmp);
15168 finish_goto_stmt (tmp);
15169 break;
15171 case ASM_EXPR:
15173 tree string = RECUR (ASM_STRING (t));
15174 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15175 complain, in_decl);
15176 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15177 complain, in_decl);
15178 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15179 complain, in_decl);
15180 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15181 complain, in_decl);
15182 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15183 clobbers, labels);
15184 tree asm_expr = tmp;
15185 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15186 asm_expr = TREE_OPERAND (asm_expr, 0);
15187 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15189 break;
15191 case TRY_BLOCK:
15192 if (CLEANUP_P (t))
15194 stmt = begin_try_block ();
15195 RECUR (TRY_STMTS (t));
15196 finish_cleanup_try_block (stmt);
15197 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15199 else
15201 tree compound_stmt = NULL_TREE;
15203 if (FN_TRY_BLOCK_P (t))
15204 stmt = begin_function_try_block (&compound_stmt);
15205 else
15206 stmt = begin_try_block ();
15208 RECUR (TRY_STMTS (t));
15210 if (FN_TRY_BLOCK_P (t))
15211 finish_function_try_block (stmt);
15212 else
15213 finish_try_block (stmt);
15215 RECUR (TRY_HANDLERS (t));
15216 if (FN_TRY_BLOCK_P (t))
15217 finish_function_handler_sequence (stmt, compound_stmt);
15218 else
15219 finish_handler_sequence (stmt);
15221 break;
15223 case HANDLER:
15225 tree decl = HANDLER_PARMS (t);
15227 if (decl)
15229 decl = tsubst (decl, args, complain, in_decl);
15230 /* Prevent instantiate_decl from trying to instantiate
15231 this variable. We've already done all that needs to be
15232 done. */
15233 if (decl != error_mark_node)
15234 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15236 stmt = begin_handler ();
15237 finish_handler_parms (decl, stmt);
15238 RECUR (HANDLER_BODY (t));
15239 finish_handler (stmt);
15241 break;
15243 case TAG_DEFN:
15244 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
15245 if (CLASS_TYPE_P (tmp))
15247 /* Local classes are not independent templates; they are
15248 instantiated along with their containing function. And this
15249 way we don't have to deal with pushing out of one local class
15250 to instantiate a member of another local class. */
15251 tree fn;
15252 /* Closures are handled by the LAMBDA_EXPR. */
15253 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
15254 complete_type (tmp);
15255 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
15256 if (!DECL_ARTIFICIAL (fn))
15257 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
15259 break;
15261 case STATIC_ASSERT:
15263 tree condition;
15265 ++c_inhibit_evaluation_warnings;
15266 condition =
15267 tsubst_expr (STATIC_ASSERT_CONDITION (t),
15268 args,
15269 complain, in_decl,
15270 /*integral_constant_expression_p=*/true);
15271 --c_inhibit_evaluation_warnings;
15273 finish_static_assert (condition,
15274 STATIC_ASSERT_MESSAGE (t),
15275 STATIC_ASSERT_SOURCE_LOCATION (t),
15276 /*member_p=*/false);
15278 break;
15280 case OACC_KERNELS:
15281 case OACC_PARALLEL:
15282 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, false, args, complain,
15283 in_decl);
15284 stmt = begin_omp_parallel ();
15285 RECUR (OMP_BODY (t));
15286 finish_omp_construct (TREE_CODE (t), stmt, tmp);
15287 break;
15289 case OMP_PARALLEL:
15290 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
15291 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false, true,
15292 args, complain, in_decl);
15293 if (OMP_PARALLEL_COMBINED (t))
15294 omp_parallel_combined_clauses = &tmp;
15295 stmt = begin_omp_parallel ();
15296 RECUR (OMP_PARALLEL_BODY (t));
15297 gcc_assert (omp_parallel_combined_clauses == NULL);
15298 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15299 = OMP_PARALLEL_COMBINED (t);
15300 pop_omp_privatization_clauses (r);
15301 break;
15303 case OMP_TASK:
15304 r = push_omp_privatization_clauses (false);
15305 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false, true,
15306 args, complain, in_decl);
15307 stmt = begin_omp_task ();
15308 RECUR (OMP_TASK_BODY (t));
15309 finish_omp_task (tmp, stmt);
15310 pop_omp_privatization_clauses (r);
15311 break;
15313 case OMP_FOR:
15314 case OMP_SIMD:
15315 case CILK_SIMD:
15316 case CILK_FOR:
15317 case OMP_DISTRIBUTE:
15318 case OMP_TASKLOOP:
15319 case OACC_LOOP:
15321 tree clauses, body, pre_body;
15322 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15323 tree orig_declv = NULL_TREE;
15324 tree incrv = NULL_TREE;
15325 int i;
15327 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
15328 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
15329 TREE_CODE (t) != OACC_LOOP,
15330 args, complain, in_decl);
15331 if (OMP_FOR_INIT (t) != NULL_TREE)
15333 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15334 if (OMP_FOR_ORIG_DECLS (t))
15335 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15336 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15337 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15338 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15341 stmt = begin_omp_structured_block ();
15343 pre_body = push_stmt_list ();
15344 RECUR (OMP_FOR_PRE_BODY (t));
15345 pre_body = pop_stmt_list (pre_body);
15347 if (OMP_FOR_INIT (t) != NULL_TREE)
15348 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15349 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
15350 incrv, &clauses, args, complain, in_decl,
15351 integral_constant_expression_p);
15352 omp_parallel_combined_clauses = NULL;
15354 body = push_stmt_list ();
15355 RECUR (OMP_FOR_BODY (t));
15356 body = pop_stmt_list (body);
15358 if (OMP_FOR_INIT (t) != NULL_TREE)
15359 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
15360 orig_declv, initv, condv, incrv, body, pre_body,
15361 NULL, clauses);
15362 else
15364 t = make_node (TREE_CODE (t));
15365 TREE_TYPE (t) = void_type_node;
15366 OMP_FOR_BODY (t) = body;
15367 OMP_FOR_PRE_BODY (t) = pre_body;
15368 OMP_FOR_CLAUSES (t) = clauses;
15369 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15370 add_stmt (t);
15373 add_stmt (finish_omp_structured_block (stmt));
15374 pop_omp_privatization_clauses (r);
15376 break;
15378 case OMP_SECTIONS:
15379 omp_parallel_combined_clauses = NULL;
15380 /* FALLTHRU */
15381 case OMP_SINGLE:
15382 case OMP_TEAMS:
15383 case OMP_CRITICAL:
15384 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
15385 && OMP_TEAMS_COMBINED (t));
15386 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, true,
15387 args, complain, in_decl);
15388 stmt = push_stmt_list ();
15389 RECUR (OMP_BODY (t));
15390 stmt = pop_stmt_list (stmt);
15392 t = copy_node (t);
15393 OMP_BODY (t) = stmt;
15394 OMP_CLAUSES (t) = tmp;
15395 add_stmt (t);
15396 pop_omp_privatization_clauses (r);
15397 break;
15399 case OACC_DATA:
15400 case OMP_TARGET_DATA:
15401 case OMP_TARGET:
15402 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
15403 TREE_CODE (t) != OACC_DATA,
15404 args, complain, in_decl);
15405 keep_next_level (true);
15406 stmt = begin_omp_structured_block ();
15408 RECUR (OMP_BODY (t));
15409 stmt = finish_omp_structured_block (stmt);
15411 t = copy_node (t);
15412 OMP_BODY (t) = stmt;
15413 OMP_CLAUSES (t) = tmp;
15414 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
15416 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
15417 if (teams)
15419 /* For combined target teams, ensure the num_teams and
15420 thread_limit clause expressions are evaluated on the host,
15421 before entering the target construct. */
15422 tree c;
15423 for (c = OMP_TEAMS_CLAUSES (teams);
15424 c; c = OMP_CLAUSE_CHAIN (c))
15425 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15426 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15427 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15429 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15430 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
15431 if (expr == error_mark_node)
15432 continue;
15433 tmp = TARGET_EXPR_SLOT (expr);
15434 add_stmt (expr);
15435 OMP_CLAUSE_OPERAND (c, 0) = expr;
15436 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15437 OMP_CLAUSE_FIRSTPRIVATE);
15438 OMP_CLAUSE_DECL (tc) = tmp;
15439 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
15440 OMP_TARGET_CLAUSES (t) = tc;
15444 add_stmt (t);
15445 break;
15447 case OACC_DECLARE:
15448 t = copy_node (t);
15449 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), false, false,
15450 args, complain, in_decl);
15451 OACC_DECLARE_CLAUSES (t) = tmp;
15452 add_stmt (t);
15453 break;
15455 case OMP_TARGET_UPDATE:
15456 case OMP_TARGET_ENTER_DATA:
15457 case OMP_TARGET_EXIT_DATA:
15458 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, true,
15459 args, complain, in_decl);
15460 t = copy_node (t);
15461 OMP_STANDALONE_CLAUSES (t) = tmp;
15462 add_stmt (t);
15463 break;
15465 case OACC_ENTER_DATA:
15466 case OACC_EXIT_DATA:
15467 case OACC_UPDATE:
15468 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, false,
15469 args, complain, in_decl);
15470 t = copy_node (t);
15471 OMP_STANDALONE_CLAUSES (t) = tmp;
15472 add_stmt (t);
15473 break;
15475 case OMP_ORDERED:
15476 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), false, true,
15477 args, complain, in_decl);
15478 stmt = push_stmt_list ();
15479 RECUR (OMP_BODY (t));
15480 stmt = pop_stmt_list (stmt);
15482 t = copy_node (t);
15483 OMP_BODY (t) = stmt;
15484 OMP_ORDERED_CLAUSES (t) = tmp;
15485 add_stmt (t);
15486 break;
15488 case OMP_SECTION:
15489 case OMP_MASTER:
15490 case OMP_TASKGROUP:
15491 stmt = push_stmt_list ();
15492 RECUR (OMP_BODY (t));
15493 stmt = pop_stmt_list (stmt);
15495 t = copy_node (t);
15496 OMP_BODY (t) = stmt;
15497 add_stmt (t);
15498 break;
15500 case OMP_ATOMIC:
15501 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
15502 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
15504 tree op1 = TREE_OPERAND (t, 1);
15505 tree rhs1 = NULL_TREE;
15506 tree lhs, rhs;
15507 if (TREE_CODE (op1) == COMPOUND_EXPR)
15509 rhs1 = RECUR (TREE_OPERAND (op1, 0));
15510 op1 = TREE_OPERAND (op1, 1);
15512 lhs = RECUR (TREE_OPERAND (op1, 0));
15513 rhs = RECUR (TREE_OPERAND (op1, 1));
15514 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
15515 NULL_TREE, NULL_TREE, rhs1,
15516 OMP_ATOMIC_SEQ_CST (t));
15518 else
15520 tree op1 = TREE_OPERAND (t, 1);
15521 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
15522 tree rhs1 = NULL_TREE;
15523 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
15524 enum tree_code opcode = NOP_EXPR;
15525 if (code == OMP_ATOMIC_READ)
15527 v = RECUR (TREE_OPERAND (op1, 0));
15528 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15530 else if (code == OMP_ATOMIC_CAPTURE_OLD
15531 || code == OMP_ATOMIC_CAPTURE_NEW)
15533 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
15534 v = RECUR (TREE_OPERAND (op1, 0));
15535 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15536 if (TREE_CODE (op11) == COMPOUND_EXPR)
15538 rhs1 = RECUR (TREE_OPERAND (op11, 0));
15539 op11 = TREE_OPERAND (op11, 1);
15541 lhs = RECUR (TREE_OPERAND (op11, 0));
15542 rhs = RECUR (TREE_OPERAND (op11, 1));
15543 opcode = TREE_CODE (op11);
15544 if (opcode == MODIFY_EXPR)
15545 opcode = NOP_EXPR;
15547 else
15549 code = OMP_ATOMIC;
15550 lhs = RECUR (TREE_OPERAND (op1, 0));
15551 rhs = RECUR (TREE_OPERAND (op1, 1));
15553 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
15554 OMP_ATOMIC_SEQ_CST (t));
15556 break;
15558 case TRANSACTION_EXPR:
15560 int flags = 0;
15561 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
15562 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
15564 if (TRANSACTION_EXPR_IS_STMT (t))
15566 tree body = TRANSACTION_EXPR_BODY (t);
15567 tree noex = NULL_TREE;
15568 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
15570 noex = MUST_NOT_THROW_COND (body);
15571 if (noex == NULL_TREE)
15572 noex = boolean_true_node;
15573 body = TREE_OPERAND (body, 0);
15575 stmt = begin_transaction_stmt (input_location, NULL, flags);
15576 RECUR (body);
15577 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
15579 else
15581 stmt = build_transaction_expr (EXPR_LOCATION (t),
15582 RECUR (TRANSACTION_EXPR_BODY (t)),
15583 flags, NULL_TREE);
15584 RETURN (stmt);
15587 break;
15589 case MUST_NOT_THROW_EXPR:
15591 tree op0 = RECUR (TREE_OPERAND (t, 0));
15592 tree cond = RECUR (MUST_NOT_THROW_COND (t));
15593 RETURN (build_must_not_throw_expr (op0, cond));
15596 case EXPR_PACK_EXPANSION:
15597 error ("invalid use of pack expansion expression");
15598 RETURN (error_mark_node);
15600 case NONTYPE_ARGUMENT_PACK:
15601 error ("use %<...%> to expand argument pack");
15602 RETURN (error_mark_node);
15604 case CILK_SPAWN_STMT:
15605 cfun->calls_cilk_spawn = 1;
15606 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
15608 case CILK_SYNC_STMT:
15609 RETURN (build_cilk_sync ());
15611 case COMPOUND_EXPR:
15612 tmp = RECUR (TREE_OPERAND (t, 0));
15613 if (tmp == NULL_TREE)
15614 /* If the first operand was a statement, we're done with it. */
15615 RETURN (RECUR (TREE_OPERAND (t, 1)));
15616 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
15617 RECUR (TREE_OPERAND (t, 1)),
15618 complain));
15620 case ANNOTATE_EXPR:
15621 tmp = RECUR (TREE_OPERAND (t, 0));
15622 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
15623 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
15625 default:
15626 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
15628 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
15629 /*function_p=*/false,
15630 integral_constant_expression_p));
15633 RETURN (NULL_TREE);
15634 out:
15635 input_location = loc;
15636 return r;
15637 #undef RECUR
15638 #undef RETURN
15641 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15642 function. For description of the body see comment above
15643 cp_parser_omp_declare_reduction_exprs. */
15645 static void
15646 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15648 if (t == NULL_TREE || t == error_mark_node)
15649 return;
15651 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
15653 tree_stmt_iterator tsi;
15654 int i;
15655 tree stmts[7];
15656 memset (stmts, 0, sizeof stmts);
15657 for (i = 0, tsi = tsi_start (t);
15658 i < 7 && !tsi_end_p (tsi);
15659 i++, tsi_next (&tsi))
15660 stmts[i] = tsi_stmt (tsi);
15661 gcc_assert (tsi_end_p (tsi));
15663 if (i >= 3)
15665 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
15666 && TREE_CODE (stmts[1]) == DECL_EXPR);
15667 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
15668 args, complain, in_decl);
15669 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
15670 args, complain, in_decl);
15671 DECL_CONTEXT (omp_out) = current_function_decl;
15672 DECL_CONTEXT (omp_in) = current_function_decl;
15673 keep_next_level (true);
15674 tree block = begin_omp_structured_block ();
15675 tsubst_expr (stmts[2], args, complain, in_decl, false);
15676 block = finish_omp_structured_block (block);
15677 block = maybe_cleanup_point_expr_void (block);
15678 add_decl_expr (omp_out);
15679 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
15680 TREE_NO_WARNING (omp_out) = 1;
15681 add_decl_expr (omp_in);
15682 finish_expr_stmt (block);
15684 if (i >= 6)
15686 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
15687 && TREE_CODE (stmts[4]) == DECL_EXPR);
15688 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
15689 args, complain, in_decl);
15690 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
15691 args, complain, in_decl);
15692 DECL_CONTEXT (omp_priv) = current_function_decl;
15693 DECL_CONTEXT (omp_orig) = current_function_decl;
15694 keep_next_level (true);
15695 tree block = begin_omp_structured_block ();
15696 tsubst_expr (stmts[5], args, complain, in_decl, false);
15697 block = finish_omp_structured_block (block);
15698 block = maybe_cleanup_point_expr_void (block);
15699 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
15700 add_decl_expr (omp_priv);
15701 add_decl_expr (omp_orig);
15702 finish_expr_stmt (block);
15703 if (i == 7)
15704 add_decl_expr (omp_orig);
15708 /* T is a postfix-expression that is not being used in a function
15709 call. Return the substituted version of T. */
15711 static tree
15712 tsubst_non_call_postfix_expression (tree t, tree args,
15713 tsubst_flags_t complain,
15714 tree in_decl)
15716 if (TREE_CODE (t) == SCOPE_REF)
15717 t = tsubst_qualified_id (t, args, complain, in_decl,
15718 /*done=*/false, /*address_p=*/false);
15719 else
15720 t = tsubst_copy_and_build (t, args, complain, in_decl,
15721 /*function_p=*/false,
15722 /*integral_constant_expression_p=*/false);
15724 return t;
15727 /* Like tsubst but deals with expressions and performs semantic
15728 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15730 tree
15731 tsubst_copy_and_build (tree t,
15732 tree args,
15733 tsubst_flags_t complain,
15734 tree in_decl,
15735 bool function_p,
15736 bool integral_constant_expression_p)
15738 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15739 #define RECUR(NODE) \
15740 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15741 /*function_p=*/false, \
15742 integral_constant_expression_p)
15744 tree retval, op1;
15745 location_t loc;
15747 if (t == NULL_TREE || t == error_mark_node)
15748 return t;
15750 loc = input_location;
15751 if (EXPR_HAS_LOCATION (t))
15752 input_location = EXPR_LOCATION (t);
15754 /* N3276 decltype magic only applies to calls at the top level or on the
15755 right side of a comma. */
15756 tsubst_flags_t decltype_flag = (complain & tf_decltype);
15757 complain &= ~tf_decltype;
15759 switch (TREE_CODE (t))
15761 case USING_DECL:
15762 t = DECL_NAME (t);
15763 /* Fall through. */
15764 case IDENTIFIER_NODE:
15766 tree decl;
15767 cp_id_kind idk;
15768 bool non_integral_constant_expression_p;
15769 const char *error_msg;
15771 if (IDENTIFIER_TYPENAME_P (t))
15773 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15774 t = mangle_conv_op_name_for_type (new_type);
15777 /* Look up the name. */
15778 decl = lookup_name (t);
15780 /* By convention, expressions use ERROR_MARK_NODE to indicate
15781 failure, not NULL_TREE. */
15782 if (decl == NULL_TREE)
15783 decl = error_mark_node;
15785 decl = finish_id_expression (t, decl, NULL_TREE,
15786 &idk,
15787 integral_constant_expression_p,
15788 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
15789 &non_integral_constant_expression_p,
15790 /*template_p=*/false,
15791 /*done=*/true,
15792 /*address_p=*/false,
15793 /*template_arg_p=*/false,
15794 &error_msg,
15795 input_location);
15796 if (error_msg)
15797 error (error_msg);
15798 if (!function_p && identifier_p (decl))
15800 if (complain & tf_error)
15801 unqualified_name_lookup_error (decl);
15802 decl = error_mark_node;
15804 RETURN (decl);
15807 case TEMPLATE_ID_EXPR:
15809 tree object;
15810 tree templ = RECUR (TREE_OPERAND (t, 0));
15811 tree targs = TREE_OPERAND (t, 1);
15813 if (targs)
15814 targs = tsubst_template_args (targs, args, complain, in_decl);
15815 if (targs == error_mark_node)
15816 return error_mark_node;
15818 if (variable_template_p (templ))
15820 templ = lookup_template_variable (templ, targs);
15821 if (!any_dependent_template_arguments_p (targs))
15823 templ = finish_template_variable (templ, complain);
15824 mark_used (templ);
15826 RETURN (convert_from_reference (templ));
15829 if (TREE_CODE (templ) == COMPONENT_REF)
15831 object = TREE_OPERAND (templ, 0);
15832 templ = TREE_OPERAND (templ, 1);
15834 else
15835 object = NULL_TREE;
15836 templ = lookup_template_function (templ, targs);
15838 if (object)
15839 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
15840 object, templ, NULL_TREE));
15841 else
15842 RETURN (baselink_for_fns (templ));
15845 case INDIRECT_REF:
15847 tree r = RECUR (TREE_OPERAND (t, 0));
15849 if (REFERENCE_REF_P (t))
15851 /* A type conversion to reference type will be enclosed in
15852 such an indirect ref, but the substitution of the cast
15853 will have also added such an indirect ref. */
15854 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
15855 r = convert_from_reference (r);
15857 else
15858 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
15859 complain|decltype_flag);
15860 RETURN (r);
15863 case NOP_EXPR:
15865 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15866 tree op0 = RECUR (TREE_OPERAND (t, 0));
15867 RETURN (build_nop (type, op0));
15870 case IMPLICIT_CONV_EXPR:
15872 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15873 tree expr = RECUR (TREE_OPERAND (t, 0));
15874 int flags = LOOKUP_IMPLICIT;
15875 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
15876 flags = LOOKUP_NORMAL;
15877 RETURN (perform_implicit_conversion_flags (type, expr, complain,
15878 flags));
15881 case CONVERT_EXPR:
15883 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15884 tree op0 = RECUR (TREE_OPERAND (t, 0));
15885 RETURN (build1 (CONVERT_EXPR, type, op0));
15888 case CAST_EXPR:
15889 case REINTERPRET_CAST_EXPR:
15890 case CONST_CAST_EXPR:
15891 case DYNAMIC_CAST_EXPR:
15892 case STATIC_CAST_EXPR:
15894 tree type;
15895 tree op, r = NULL_TREE;
15897 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15898 if (integral_constant_expression_p
15899 && !cast_valid_in_integral_constant_expression_p (type))
15901 if (complain & tf_error)
15902 error ("a cast to a type other than an integral or "
15903 "enumeration type cannot appear in a constant-expression");
15904 RETURN (error_mark_node);
15907 op = RECUR (TREE_OPERAND (t, 0));
15909 warning_sentinel s(warn_useless_cast);
15910 switch (TREE_CODE (t))
15912 case CAST_EXPR:
15913 r = build_functional_cast (type, op, complain);
15914 break;
15915 case REINTERPRET_CAST_EXPR:
15916 r = build_reinterpret_cast (type, op, complain);
15917 break;
15918 case CONST_CAST_EXPR:
15919 r = build_const_cast (type, op, complain);
15920 break;
15921 case DYNAMIC_CAST_EXPR:
15922 r = build_dynamic_cast (type, op, complain);
15923 break;
15924 case STATIC_CAST_EXPR:
15925 r = build_static_cast (type, op, complain);
15926 break;
15927 default:
15928 gcc_unreachable ();
15931 RETURN (r);
15934 case POSTDECREMENT_EXPR:
15935 case POSTINCREMENT_EXPR:
15936 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15937 args, complain, in_decl);
15938 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
15939 complain|decltype_flag));
15941 case PREDECREMENT_EXPR:
15942 case PREINCREMENT_EXPR:
15943 case NEGATE_EXPR:
15944 case BIT_NOT_EXPR:
15945 case ABS_EXPR:
15946 case TRUTH_NOT_EXPR:
15947 case UNARY_PLUS_EXPR: /* Unary + */
15948 case REALPART_EXPR:
15949 case IMAGPART_EXPR:
15950 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
15951 RECUR (TREE_OPERAND (t, 0)),
15952 complain|decltype_flag));
15954 case FIX_TRUNC_EXPR:
15955 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
15956 0, complain));
15958 case ADDR_EXPR:
15959 op1 = TREE_OPERAND (t, 0);
15960 if (TREE_CODE (op1) == LABEL_DECL)
15961 RETURN (finish_label_address_expr (DECL_NAME (op1),
15962 EXPR_LOCATION (op1)));
15963 if (TREE_CODE (op1) == SCOPE_REF)
15964 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
15965 /*done=*/true, /*address_p=*/true);
15966 else
15967 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
15968 in_decl);
15969 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
15970 complain|decltype_flag));
15972 case PLUS_EXPR:
15973 case MINUS_EXPR:
15974 case MULT_EXPR:
15975 case TRUNC_DIV_EXPR:
15976 case CEIL_DIV_EXPR:
15977 case FLOOR_DIV_EXPR:
15978 case ROUND_DIV_EXPR:
15979 case EXACT_DIV_EXPR:
15980 case BIT_AND_EXPR:
15981 case BIT_IOR_EXPR:
15982 case BIT_XOR_EXPR:
15983 case TRUNC_MOD_EXPR:
15984 case FLOOR_MOD_EXPR:
15985 case TRUTH_ANDIF_EXPR:
15986 case TRUTH_ORIF_EXPR:
15987 case TRUTH_AND_EXPR:
15988 case TRUTH_OR_EXPR:
15989 case RSHIFT_EXPR:
15990 case LSHIFT_EXPR:
15991 case RROTATE_EXPR:
15992 case LROTATE_EXPR:
15993 case EQ_EXPR:
15994 case NE_EXPR:
15995 case MAX_EXPR:
15996 case MIN_EXPR:
15997 case LE_EXPR:
15998 case GE_EXPR:
15999 case LT_EXPR:
16000 case GT_EXPR:
16001 case MEMBER_REF:
16002 case DOTSTAR_EXPR:
16004 warning_sentinel s1(warn_type_limits);
16005 warning_sentinel s2(warn_div_by_zero);
16006 warning_sentinel s3(warn_logical_op);
16007 warning_sentinel s4(warn_tautological_compare);
16008 tree op0 = RECUR (TREE_OPERAND (t, 0));
16009 tree op1 = RECUR (TREE_OPERAND (t, 1));
16010 tree r = build_x_binary_op
16011 (input_location, TREE_CODE (t),
16012 op0,
16013 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16014 ? ERROR_MARK
16015 : TREE_CODE (TREE_OPERAND (t, 0))),
16016 op1,
16017 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16018 ? ERROR_MARK
16019 : TREE_CODE (TREE_OPERAND (t, 1))),
16020 /*overload=*/NULL,
16021 complain|decltype_flag);
16022 if (EXPR_P (r) && TREE_NO_WARNING (t))
16023 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16025 RETURN (r);
16028 case POINTER_PLUS_EXPR:
16030 tree op0 = RECUR (TREE_OPERAND (t, 0));
16031 tree op1 = RECUR (TREE_OPERAND (t, 1));
16032 return fold_build_pointer_plus (op0, op1);
16035 case SCOPE_REF:
16036 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16037 /*address_p=*/false));
16038 case ARRAY_REF:
16039 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16040 args, complain, in_decl);
16041 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16042 RECUR (TREE_OPERAND (t, 1)),
16043 complain|decltype_flag));
16045 case ARRAY_NOTATION_REF:
16047 tree start_index, length, stride;
16048 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16049 args, complain, in_decl);
16050 start_index = RECUR (ARRAY_NOTATION_START (t));
16051 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16052 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16053 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16054 length, stride, TREE_TYPE (op1)));
16056 case SIZEOF_EXPR:
16057 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
16058 RETURN (tsubst_copy (t, args, complain, in_decl));
16059 /* Fall through */
16061 case ALIGNOF_EXPR:
16063 tree r;
16065 op1 = TREE_OPERAND (t, 0);
16066 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16067 op1 = TREE_TYPE (op1);
16068 if (!args)
16070 /* When there are no ARGS, we are trying to evaluate a
16071 non-dependent expression from the parser. Trying to do
16072 the substitutions may not work. */
16073 if (!TYPE_P (op1))
16074 op1 = TREE_TYPE (op1);
16076 else
16078 ++cp_unevaluated_operand;
16079 ++c_inhibit_evaluation_warnings;
16080 if (TYPE_P (op1))
16081 op1 = tsubst (op1, args, complain, in_decl);
16082 else
16083 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16084 /*function_p=*/false,
16085 /*integral_constant_expression_p=*/
16086 false);
16087 --cp_unevaluated_operand;
16088 --c_inhibit_evaluation_warnings;
16090 if (TYPE_P (op1))
16091 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16092 complain & tf_error);
16093 else
16094 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16095 complain & tf_error);
16096 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16098 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16100 if (!processing_template_decl && TYPE_P (op1))
16102 r = build_min (SIZEOF_EXPR, size_type_node,
16103 build1 (NOP_EXPR, op1, error_mark_node));
16104 SIZEOF_EXPR_TYPE_P (r) = 1;
16106 else
16107 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16108 TREE_SIDE_EFFECTS (r) = 0;
16109 TREE_READONLY (r) = 1;
16111 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16113 RETURN (r);
16116 case AT_ENCODE_EXPR:
16118 op1 = TREE_OPERAND (t, 0);
16119 ++cp_unevaluated_operand;
16120 ++c_inhibit_evaluation_warnings;
16121 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16122 /*function_p=*/false,
16123 /*integral_constant_expression_p=*/false);
16124 --cp_unevaluated_operand;
16125 --c_inhibit_evaluation_warnings;
16126 RETURN (objc_build_encode_expr (op1));
16129 case NOEXCEPT_EXPR:
16130 op1 = TREE_OPERAND (t, 0);
16131 ++cp_unevaluated_operand;
16132 ++c_inhibit_evaluation_warnings;
16133 ++cp_noexcept_operand;
16134 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16135 /*function_p=*/false,
16136 /*integral_constant_expression_p=*/false);
16137 --cp_unevaluated_operand;
16138 --c_inhibit_evaluation_warnings;
16139 --cp_noexcept_operand;
16140 RETURN (finish_noexcept_expr (op1, complain));
16142 case MODOP_EXPR:
16144 warning_sentinel s(warn_div_by_zero);
16145 tree lhs = RECUR (TREE_OPERAND (t, 0));
16146 tree rhs = RECUR (TREE_OPERAND (t, 2));
16147 tree r = build_x_modify_expr
16148 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16149 complain|decltype_flag);
16150 /* TREE_NO_WARNING must be set if either the expression was
16151 parenthesized or it uses an operator such as >>= rather
16152 than plain assignment. In the former case, it was already
16153 set and must be copied. In the latter case,
16154 build_x_modify_expr sets it and it must not be reset
16155 here. */
16156 if (TREE_NO_WARNING (t))
16157 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16159 RETURN (r);
16162 case ARROW_EXPR:
16163 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16164 args, complain, in_decl);
16165 /* Remember that there was a reference to this entity. */
16166 if (DECL_P (op1)
16167 && !mark_used (op1, complain) && !(complain & tf_error))
16168 RETURN (error_mark_node);
16169 RETURN (build_x_arrow (input_location, op1, complain));
16171 case NEW_EXPR:
16173 tree placement = RECUR (TREE_OPERAND (t, 0));
16174 tree init = RECUR (TREE_OPERAND (t, 3));
16175 vec<tree, va_gc> *placement_vec;
16176 vec<tree, va_gc> *init_vec;
16177 tree ret;
16179 if (placement == NULL_TREE)
16180 placement_vec = NULL;
16181 else
16183 placement_vec = make_tree_vector ();
16184 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16185 vec_safe_push (placement_vec, TREE_VALUE (placement));
16188 /* If there was an initializer in the original tree, but it
16189 instantiated to an empty list, then we should pass a
16190 non-NULL empty vector to tell build_new that it was an
16191 empty initializer() rather than no initializer. This can
16192 only happen when the initializer is a pack expansion whose
16193 parameter packs are of length zero. */
16194 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16195 init_vec = NULL;
16196 else
16198 init_vec = make_tree_vector ();
16199 if (init == void_node)
16200 gcc_assert (init_vec != NULL);
16201 else
16203 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16204 vec_safe_push (init_vec, TREE_VALUE (init));
16208 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16209 tree op2 = RECUR (TREE_OPERAND (t, 2));
16210 ret = build_new (&placement_vec, op1, op2, &init_vec,
16211 NEW_EXPR_USE_GLOBAL (t),
16212 complain);
16214 if (placement_vec != NULL)
16215 release_tree_vector (placement_vec);
16216 if (init_vec != NULL)
16217 release_tree_vector (init_vec);
16219 RETURN (ret);
16222 case DELETE_EXPR:
16224 tree op0 = RECUR (TREE_OPERAND (t, 0));
16225 tree op1 = RECUR (TREE_OPERAND (t, 1));
16226 RETURN (delete_sanity (op0, op1,
16227 DELETE_EXPR_USE_VEC (t),
16228 DELETE_EXPR_USE_GLOBAL (t),
16229 complain));
16232 case COMPOUND_EXPR:
16234 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
16235 complain & ~tf_decltype, in_decl,
16236 /*function_p=*/false,
16237 integral_constant_expression_p);
16238 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
16239 op0,
16240 RECUR (TREE_OPERAND (t, 1)),
16241 complain|decltype_flag));
16244 case CALL_EXPR:
16246 tree function;
16247 vec<tree, va_gc> *call_args;
16248 unsigned int nargs, i;
16249 bool qualified_p;
16250 bool koenig_p;
16251 tree ret;
16253 function = CALL_EXPR_FN (t);
16254 /* When we parsed the expression, we determined whether or
16255 not Koenig lookup should be performed. */
16256 koenig_p = KOENIG_LOOKUP_P (t);
16257 if (TREE_CODE (function) == SCOPE_REF)
16259 qualified_p = true;
16260 function = tsubst_qualified_id (function, args, complain, in_decl,
16261 /*done=*/false,
16262 /*address_p=*/false);
16264 else if (koenig_p && identifier_p (function))
16266 /* Do nothing; calling tsubst_copy_and_build on an identifier
16267 would incorrectly perform unqualified lookup again.
16269 Note that we can also have an IDENTIFIER_NODE if the earlier
16270 unqualified lookup found a member function; in that case
16271 koenig_p will be false and we do want to do the lookup
16272 again to find the instantiated member function.
16274 FIXME but doing that causes c++/15272, so we need to stop
16275 using IDENTIFIER_NODE in that situation. */
16276 qualified_p = false;
16278 else
16280 if (TREE_CODE (function) == COMPONENT_REF)
16282 tree op = TREE_OPERAND (function, 1);
16284 qualified_p = (TREE_CODE (op) == SCOPE_REF
16285 || (BASELINK_P (op)
16286 && BASELINK_QUALIFIED_P (op)));
16288 else
16289 qualified_p = false;
16291 if (TREE_CODE (function) == ADDR_EXPR
16292 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
16293 /* Avoid error about taking the address of a constructor. */
16294 function = TREE_OPERAND (function, 0);
16296 function = tsubst_copy_and_build (function, args, complain,
16297 in_decl,
16298 !qualified_p,
16299 integral_constant_expression_p);
16301 if (BASELINK_P (function))
16302 qualified_p = true;
16305 nargs = call_expr_nargs (t);
16306 call_args = make_tree_vector ();
16307 for (i = 0; i < nargs; ++i)
16309 tree arg = CALL_EXPR_ARG (t, i);
16311 if (!PACK_EXPANSION_P (arg))
16312 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
16313 else
16315 /* Expand the pack expansion and push each entry onto
16316 CALL_ARGS. */
16317 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
16318 if (TREE_CODE (arg) == TREE_VEC)
16320 unsigned int len, j;
16322 len = TREE_VEC_LENGTH (arg);
16323 for (j = 0; j < len; ++j)
16325 tree value = TREE_VEC_ELT (arg, j);
16326 if (value != NULL_TREE)
16327 value = convert_from_reference (value);
16328 vec_safe_push (call_args, value);
16331 else
16333 /* A partial substitution. Add one entry. */
16334 vec_safe_push (call_args, arg);
16339 /* We do not perform argument-dependent lookup if normal
16340 lookup finds a non-function, in accordance with the
16341 expected resolution of DR 218. */
16342 if (koenig_p
16343 && ((is_overloaded_fn (function)
16344 /* If lookup found a member function, the Koenig lookup is
16345 not appropriate, even if an unqualified-name was used
16346 to denote the function. */
16347 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
16348 || identifier_p (function))
16349 /* Only do this when substitution turns a dependent call
16350 into a non-dependent call. */
16351 && type_dependent_expression_p_push (t)
16352 && !any_type_dependent_arguments_p (call_args))
16353 function = perform_koenig_lookup (function, call_args, tf_none);
16355 if (identifier_p (function)
16356 && !any_type_dependent_arguments_p (call_args))
16358 if (koenig_p && (complain & tf_warning_or_error))
16360 /* For backwards compatibility and good diagnostics, try
16361 the unqualified lookup again if we aren't in SFINAE
16362 context. */
16363 tree unq = (tsubst_copy_and_build
16364 (function, args, complain, in_decl, true,
16365 integral_constant_expression_p));
16366 if (unq == error_mark_node)
16367 RETURN (error_mark_node);
16369 if (unq != function)
16371 tree fn = unq;
16372 if (INDIRECT_REF_P (fn))
16373 fn = TREE_OPERAND (fn, 0);
16374 if (TREE_CODE (fn) == COMPONENT_REF)
16375 fn = TREE_OPERAND (fn, 1);
16376 if (is_overloaded_fn (fn))
16377 fn = get_first_fn (fn);
16378 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16379 "%qD was not declared in this scope, "
16380 "and no declarations were found by "
16381 "argument-dependent lookup at the point "
16382 "of instantiation", function))
16384 if (!DECL_P (fn))
16385 /* Can't say anything more. */;
16386 else if (DECL_CLASS_SCOPE_P (fn))
16388 location_t loc = EXPR_LOC_OR_LOC (t,
16389 input_location);
16390 inform (loc,
16391 "declarations in dependent base %qT are "
16392 "not found by unqualified lookup",
16393 DECL_CLASS_CONTEXT (fn));
16394 if (current_class_ptr)
16395 inform (loc,
16396 "use %<this->%D%> instead", function);
16397 else
16398 inform (loc,
16399 "use %<%T::%D%> instead",
16400 current_class_name, function);
16402 else
16403 inform (DECL_SOURCE_LOCATION (fn),
16404 "%qD declared here, later in the "
16405 "translation unit", fn);
16407 function = unq;
16410 if (identifier_p (function))
16412 if (complain & tf_error)
16413 unqualified_name_lookup_error (function);
16414 release_tree_vector (call_args);
16415 RETURN (error_mark_node);
16419 /* Remember that there was a reference to this entity. */
16420 if (DECL_P (function)
16421 && !mark_used (function, complain) && !(complain & tf_error))
16422 RETURN (error_mark_node);
16424 /* Put back tf_decltype for the actual call. */
16425 complain |= decltype_flag;
16427 if (TREE_CODE (function) == OFFSET_REF)
16428 ret = build_offset_ref_call_from_tree (function, &call_args,
16429 complain);
16430 else if (TREE_CODE (function) == COMPONENT_REF)
16432 tree instance = TREE_OPERAND (function, 0);
16433 tree fn = TREE_OPERAND (function, 1);
16435 if (processing_template_decl
16436 && (type_dependent_expression_p (instance)
16437 || (!BASELINK_P (fn)
16438 && TREE_CODE (fn) != FIELD_DECL)
16439 || type_dependent_expression_p (fn)
16440 || any_type_dependent_arguments_p (call_args)))
16441 ret = build_nt_call_vec (function, call_args);
16442 else if (!BASELINK_P (fn))
16443 ret = finish_call_expr (function, &call_args,
16444 /*disallow_virtual=*/false,
16445 /*koenig_p=*/false,
16446 complain);
16447 else
16448 ret = (build_new_method_call
16449 (instance, fn,
16450 &call_args, NULL_TREE,
16451 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
16452 /*fn_p=*/NULL,
16453 complain));
16455 else
16456 ret = finish_call_expr (function, &call_args,
16457 /*disallow_virtual=*/qualified_p,
16458 koenig_p,
16459 complain);
16461 release_tree_vector (call_args);
16463 RETURN (ret);
16466 case COND_EXPR:
16468 tree cond = RECUR (TREE_OPERAND (t, 0));
16469 tree folded_cond = fold_non_dependent_expr (cond);
16470 tree exp1, exp2;
16472 if (TREE_CODE (folded_cond) == INTEGER_CST)
16474 if (integer_zerop (folded_cond))
16476 ++c_inhibit_evaluation_warnings;
16477 exp1 = RECUR (TREE_OPERAND (t, 1));
16478 --c_inhibit_evaluation_warnings;
16479 exp2 = RECUR (TREE_OPERAND (t, 2));
16481 else
16483 exp1 = RECUR (TREE_OPERAND (t, 1));
16484 ++c_inhibit_evaluation_warnings;
16485 exp2 = RECUR (TREE_OPERAND (t, 2));
16486 --c_inhibit_evaluation_warnings;
16488 cond = folded_cond;
16490 else
16492 exp1 = RECUR (TREE_OPERAND (t, 1));
16493 exp2 = RECUR (TREE_OPERAND (t, 2));
16496 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
16497 cond, exp1, exp2, complain));
16500 case PSEUDO_DTOR_EXPR:
16502 tree op0 = RECUR (TREE_OPERAND (t, 0));
16503 tree op1 = RECUR (TREE_OPERAND (t, 1));
16504 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
16505 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
16506 input_location));
16509 case TREE_LIST:
16511 tree purpose, value, chain;
16513 if (t == void_list_node)
16514 RETURN (t);
16516 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
16517 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
16519 /* We have pack expansions, so expand those and
16520 create a new list out of it. */
16521 tree purposevec = NULL_TREE;
16522 tree valuevec = NULL_TREE;
16523 tree chain;
16524 int i, len = -1;
16526 /* Expand the argument expressions. */
16527 if (TREE_PURPOSE (t))
16528 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
16529 complain, in_decl);
16530 if (TREE_VALUE (t))
16531 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
16532 complain, in_decl);
16534 /* Build the rest of the list. */
16535 chain = TREE_CHAIN (t);
16536 if (chain && chain != void_type_node)
16537 chain = RECUR (chain);
16539 /* Determine the number of arguments. */
16540 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
16542 len = TREE_VEC_LENGTH (purposevec);
16543 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16545 else if (TREE_CODE (valuevec) == TREE_VEC)
16546 len = TREE_VEC_LENGTH (valuevec);
16547 else
16549 /* Since we only performed a partial substitution into
16550 the argument pack, we only RETURN (a single list
16551 node. */
16552 if (purposevec == TREE_PURPOSE (t)
16553 && valuevec == TREE_VALUE (t)
16554 && chain == TREE_CHAIN (t))
16555 RETURN (t);
16557 RETURN (tree_cons (purposevec, valuevec, chain));
16560 /* Convert the argument vectors into a TREE_LIST */
16561 i = len;
16562 while (i > 0)
16564 /* Grab the Ith values. */
16565 i--;
16566 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
16567 : NULL_TREE;
16568 value
16569 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
16570 : NULL_TREE;
16572 /* Build the list (backwards). */
16573 chain = tree_cons (purpose, value, chain);
16576 RETURN (chain);
16579 purpose = TREE_PURPOSE (t);
16580 if (purpose)
16581 purpose = RECUR (purpose);
16582 value = TREE_VALUE (t);
16583 if (value)
16584 value = RECUR (value);
16585 chain = TREE_CHAIN (t);
16586 if (chain && chain != void_type_node)
16587 chain = RECUR (chain);
16588 if (purpose == TREE_PURPOSE (t)
16589 && value == TREE_VALUE (t)
16590 && chain == TREE_CHAIN (t))
16591 RETURN (t);
16592 RETURN (tree_cons (purpose, value, chain));
16595 case COMPONENT_REF:
16597 tree object;
16598 tree object_type;
16599 tree member;
16600 tree r;
16602 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16603 args, complain, in_decl);
16604 /* Remember that there was a reference to this entity. */
16605 if (DECL_P (object)
16606 && !mark_used (object, complain) && !(complain & tf_error))
16607 RETURN (error_mark_node);
16608 object_type = TREE_TYPE (object);
16610 member = TREE_OPERAND (t, 1);
16611 if (BASELINK_P (member))
16612 member = tsubst_baselink (member,
16613 non_reference (TREE_TYPE (object)),
16614 args, complain, in_decl);
16615 else
16616 member = tsubst_copy (member, args, complain, in_decl);
16617 if (member == error_mark_node)
16618 RETURN (error_mark_node);
16620 if (type_dependent_expression_p (object))
16621 /* We can't do much here. */;
16622 else if (!CLASS_TYPE_P (object_type))
16624 if (scalarish_type_p (object_type))
16626 tree s = NULL_TREE;
16627 tree dtor = member;
16629 if (TREE_CODE (dtor) == SCOPE_REF)
16631 s = TREE_OPERAND (dtor, 0);
16632 dtor = TREE_OPERAND (dtor, 1);
16634 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
16636 dtor = TREE_OPERAND (dtor, 0);
16637 if (TYPE_P (dtor))
16638 RETURN (finish_pseudo_destructor_expr
16639 (object, s, dtor, input_location));
16643 else if (TREE_CODE (member) == SCOPE_REF
16644 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
16646 /* Lookup the template functions now that we know what the
16647 scope is. */
16648 tree scope = TREE_OPERAND (member, 0);
16649 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
16650 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
16651 member = lookup_qualified_name (scope, tmpl,
16652 /*is_type_p=*/false,
16653 /*complain=*/false);
16654 if (BASELINK_P (member))
16656 BASELINK_FUNCTIONS (member)
16657 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
16658 args);
16659 member = (adjust_result_of_qualified_name_lookup
16660 (member, BINFO_TYPE (BASELINK_BINFO (member)),
16661 object_type));
16663 else
16665 qualified_name_lookup_error (scope, tmpl, member,
16666 input_location);
16667 RETURN (error_mark_node);
16670 else if (TREE_CODE (member) == SCOPE_REF
16671 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
16672 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
16674 if (complain & tf_error)
16676 if (TYPE_P (TREE_OPERAND (member, 0)))
16677 error ("%qT is not a class or namespace",
16678 TREE_OPERAND (member, 0));
16679 else
16680 error ("%qD is not a class or namespace",
16681 TREE_OPERAND (member, 0));
16683 RETURN (error_mark_node);
16685 else if (TREE_CODE (member) == FIELD_DECL)
16687 r = finish_non_static_data_member (member, object, NULL_TREE);
16688 if (TREE_CODE (r) == COMPONENT_REF)
16689 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16690 RETURN (r);
16693 r = finish_class_member_access_expr (object, member,
16694 /*template_p=*/false,
16695 complain);
16696 if (TREE_CODE (r) == COMPONENT_REF)
16697 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16698 RETURN (r);
16701 case THROW_EXPR:
16702 RETURN (build_throw
16703 (RECUR (TREE_OPERAND (t, 0))));
16705 case CONSTRUCTOR:
16707 vec<constructor_elt, va_gc> *n;
16708 constructor_elt *ce;
16709 unsigned HOST_WIDE_INT idx;
16710 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16711 bool process_index_p;
16712 int newlen;
16713 bool need_copy_p = false;
16714 tree r;
16716 if (type == error_mark_node)
16717 RETURN (error_mark_node);
16719 /* digest_init will do the wrong thing if we let it. */
16720 if (type && TYPE_PTRMEMFUNC_P (type))
16721 RETURN (t);
16723 /* We do not want to process the index of aggregate
16724 initializers as they are identifier nodes which will be
16725 looked up by digest_init. */
16726 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
16728 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
16729 newlen = vec_safe_length (n);
16730 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
16732 if (ce->index && process_index_p
16733 /* An identifier index is looked up in the type
16734 being initialized, not the current scope. */
16735 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
16736 ce->index = RECUR (ce->index);
16738 if (PACK_EXPANSION_P (ce->value))
16740 /* Substitute into the pack expansion. */
16741 ce->value = tsubst_pack_expansion (ce->value, args, complain,
16742 in_decl);
16744 if (ce->value == error_mark_node
16745 || PACK_EXPANSION_P (ce->value))
16747 else if (TREE_VEC_LENGTH (ce->value) == 1)
16748 /* Just move the argument into place. */
16749 ce->value = TREE_VEC_ELT (ce->value, 0);
16750 else
16752 /* Update the length of the final CONSTRUCTOR
16753 arguments vector, and note that we will need to
16754 copy.*/
16755 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
16756 need_copy_p = true;
16759 else
16760 ce->value = RECUR (ce->value);
16763 if (need_copy_p)
16765 vec<constructor_elt, va_gc> *old_n = n;
16767 vec_alloc (n, newlen);
16768 FOR_EACH_VEC_ELT (*old_n, idx, ce)
16770 if (TREE_CODE (ce->value) == TREE_VEC)
16772 int i, len = TREE_VEC_LENGTH (ce->value);
16773 for (i = 0; i < len; ++i)
16774 CONSTRUCTOR_APPEND_ELT (n, 0,
16775 TREE_VEC_ELT (ce->value, i));
16777 else
16778 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
16782 r = build_constructor (init_list_type_node, n);
16783 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
16785 if (TREE_HAS_CONSTRUCTOR (t))
16786 RETURN (finish_compound_literal (type, r, complain));
16788 TREE_TYPE (r) = type;
16789 RETURN (r);
16792 case TYPEID_EXPR:
16794 tree operand_0 = TREE_OPERAND (t, 0);
16795 if (TYPE_P (operand_0))
16797 operand_0 = tsubst (operand_0, args, complain, in_decl);
16798 RETURN (get_typeid (operand_0, complain));
16800 else
16802 operand_0 = RECUR (operand_0);
16803 RETURN (build_typeid (operand_0, complain));
16807 case VAR_DECL:
16808 if (!args)
16809 RETURN (t);
16810 else if (DECL_PACK_P (t))
16812 /* We don't build decls for an instantiation of a
16813 variadic capture proxy, we instantiate the elements
16814 when needed. */
16815 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
16816 return RECUR (DECL_VALUE_EXPR (t));
16818 /* Fall through */
16820 case PARM_DECL:
16822 tree r = tsubst_copy (t, args, complain, in_decl);
16823 /* ??? We're doing a subset of finish_id_expression here. */
16824 if (VAR_P (r)
16825 && !processing_template_decl
16826 && !cp_unevaluated_operand
16827 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
16828 && CP_DECL_THREAD_LOCAL_P (r))
16830 if (tree wrap = get_tls_wrapper_fn (r))
16831 /* Replace an evaluated use of the thread_local variable with
16832 a call to its wrapper. */
16833 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
16835 else if (outer_automatic_var_p (r))
16837 r = process_outer_var_ref (r, complain);
16838 if (is_capture_proxy (r))
16839 register_local_specialization (r, t);
16842 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
16843 /* If the original type was a reference, we'll be wrapped in
16844 the appropriate INDIRECT_REF. */
16845 r = convert_from_reference (r);
16846 RETURN (r);
16849 case VA_ARG_EXPR:
16851 tree op0 = RECUR (TREE_OPERAND (t, 0));
16852 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16853 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
16856 case OFFSETOF_EXPR:
16857 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
16858 EXPR_LOCATION (t)));
16860 case TRAIT_EXPR:
16862 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
16863 complain, in_decl);
16865 tree type2 = TRAIT_EXPR_TYPE2 (t);
16866 if (type2 && TREE_CODE (type2) == TREE_LIST)
16867 type2 = RECUR (type2);
16868 else if (type2)
16869 type2 = tsubst (type2, args, complain, in_decl);
16871 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
16874 case STMT_EXPR:
16876 tree old_stmt_expr = cur_stmt_expr;
16877 tree stmt_expr = begin_stmt_expr ();
16879 cur_stmt_expr = stmt_expr;
16880 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
16881 integral_constant_expression_p);
16882 stmt_expr = finish_stmt_expr (stmt_expr, false);
16883 cur_stmt_expr = old_stmt_expr;
16885 /* If the resulting list of expression statement is empty,
16886 fold it further into void_node. */
16887 if (empty_expr_stmt_p (stmt_expr))
16888 stmt_expr = void_node;
16890 RETURN (stmt_expr);
16893 case LAMBDA_EXPR:
16895 tree r = build_lambda_expr ();
16897 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
16898 LAMBDA_EXPR_CLOSURE (r) = type;
16899 CLASSTYPE_LAMBDA_EXPR (type) = r;
16901 LAMBDA_EXPR_LOCATION (r)
16902 = LAMBDA_EXPR_LOCATION (t);
16903 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16904 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16905 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16906 LAMBDA_EXPR_DISCRIMINATOR (r)
16907 = (LAMBDA_EXPR_DISCRIMINATOR (t));
16908 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
16909 if (!scope)
16910 /* No substitution needed. */;
16911 else if (VAR_OR_FUNCTION_DECL_P (scope))
16912 /* For a function or variable scope, we want to use tsubst so that we
16913 don't complain about referring to an auto before deduction. */
16914 scope = tsubst (scope, args, complain, in_decl);
16915 else if (TREE_CODE (scope) == PARM_DECL)
16917 /* Look up the parameter we want directly, as tsubst_copy
16918 doesn't do what we need. */
16919 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
16920 tree parm = FUNCTION_FIRST_USER_PARM (fn);
16921 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
16922 parm = DECL_CHAIN (parm);
16923 scope = parm;
16924 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16925 if (DECL_CONTEXT (scope) == NULL_TREE)
16926 DECL_CONTEXT (scope) = fn;
16928 else if (TREE_CODE (scope) == FIELD_DECL)
16929 /* For a field, use tsubst_copy so that we look up the existing field
16930 rather than build a new one. */
16931 scope = RECUR (scope);
16932 else
16933 gcc_unreachable ();
16934 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
16935 LAMBDA_EXPR_RETURN_TYPE (r)
16936 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
16938 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16939 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16941 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16942 determine_visibility (TYPE_NAME (type));
16943 /* Now that we know visibility, instantiate the type so we have a
16944 declaration of the op() for later calls to lambda_function. */
16945 complete_type (type);
16947 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16949 insert_pending_capture_proxies ();
16951 RETURN (build_lambda_object (r));
16954 case TARGET_EXPR:
16955 /* We can get here for a constant initializer of non-dependent type.
16956 FIXME stop folding in cp_parser_initializer_clause. */
16958 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
16959 complain);
16960 RETURN (r);
16963 case TRANSACTION_EXPR:
16964 RETURN (tsubst_expr(t, args, complain, in_decl,
16965 integral_constant_expression_p));
16967 case PAREN_EXPR:
16968 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
16970 case VEC_PERM_EXPR:
16972 tree op0 = RECUR (TREE_OPERAND (t, 0));
16973 tree op1 = RECUR (TREE_OPERAND (t, 1));
16974 tree op2 = RECUR (TREE_OPERAND (t, 2));
16975 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
16976 complain));
16979 case REQUIRES_EXPR:
16980 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
16982 default:
16983 /* Handle Objective-C++ constructs, if appropriate. */
16985 tree subst
16986 = objcp_tsubst_copy_and_build (t, args, complain,
16987 in_decl, /*function_p=*/false);
16988 if (subst)
16989 RETURN (subst);
16991 RETURN (tsubst_copy (t, args, complain, in_decl));
16994 #undef RECUR
16995 #undef RETURN
16996 out:
16997 input_location = loc;
16998 return retval;
17001 /* Verify that the instantiated ARGS are valid. For type arguments,
17002 make sure that the type's linkage is ok. For non-type arguments,
17003 make sure they are constants if they are integral or enumerations.
17004 Emit an error under control of COMPLAIN, and return TRUE on error. */
17006 static bool
17007 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17009 if (dependent_template_arg_p (t))
17010 return false;
17011 if (ARGUMENT_PACK_P (t))
17013 tree vec = ARGUMENT_PACK_ARGS (t);
17014 int len = TREE_VEC_LENGTH (vec);
17015 bool result = false;
17016 int i;
17018 for (i = 0; i < len; ++i)
17019 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17020 result = true;
17021 return result;
17023 else if (TYPE_P (t))
17025 /* [basic.link]: A name with no linkage (notably, the name
17026 of a class or enumeration declared in a local scope)
17027 shall not be used to declare an entity with linkage.
17028 This implies that names with no linkage cannot be used as
17029 template arguments
17031 DR 757 relaxes this restriction for C++0x. */
17032 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17033 : no_linkage_check (t, /*relaxed_p=*/false));
17035 if (nt)
17037 /* DR 488 makes use of a type with no linkage cause
17038 type deduction to fail. */
17039 if (complain & tf_error)
17041 if (TYPE_ANONYMOUS_P (nt))
17042 error ("%qT is/uses anonymous type", t);
17043 else
17044 error ("template argument for %qD uses local type %qT",
17045 tmpl, t);
17047 return true;
17049 /* In order to avoid all sorts of complications, we do not
17050 allow variably-modified types as template arguments. */
17051 else if (variably_modified_type_p (t, NULL_TREE))
17053 if (complain & tf_error)
17054 error ("%qT is a variably modified type", t);
17055 return true;
17058 /* Class template and alias template arguments should be OK. */
17059 else if (DECL_TYPE_TEMPLATE_P (t))
17061 /* A non-type argument of integral or enumerated type must be a
17062 constant. */
17063 else if (TREE_TYPE (t)
17064 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17065 && !REFERENCE_REF_P (t)
17066 && !TREE_CONSTANT (t))
17068 if (complain & tf_error)
17069 error ("integral expression %qE is not constant", t);
17070 return true;
17072 return false;
17075 static bool
17076 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17078 int ix, len = DECL_NTPARMS (tmpl);
17079 bool result = false;
17081 for (ix = 0; ix != len; ix++)
17083 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17084 result = true;
17086 if (result && (complain & tf_error))
17087 error (" trying to instantiate %qD", tmpl);
17088 return result;
17091 /* We're out of SFINAE context now, so generate diagnostics for the access
17092 errors we saw earlier when instantiating D from TMPL and ARGS. */
17094 static void
17095 recheck_decl_substitution (tree d, tree tmpl, tree args)
17097 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17098 tree type = TREE_TYPE (pattern);
17099 location_t loc = input_location;
17101 push_access_scope (d);
17102 push_deferring_access_checks (dk_no_deferred);
17103 input_location = DECL_SOURCE_LOCATION (pattern);
17104 tsubst (type, args, tf_warning_or_error, d);
17105 input_location = loc;
17106 pop_deferring_access_checks ();
17107 pop_access_scope (d);
17110 /* Instantiate the indicated variable, function, or alias template TMPL with
17111 the template arguments in TARG_PTR. */
17113 static tree
17114 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17116 tree targ_ptr = orig_args;
17117 tree fndecl;
17118 tree gen_tmpl;
17119 tree spec;
17120 bool access_ok = true;
17122 if (tmpl == error_mark_node)
17123 return error_mark_node;
17125 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
17127 /* If this function is a clone, handle it specially. */
17128 if (DECL_CLONED_FUNCTION_P (tmpl))
17130 tree spec;
17131 tree clone;
17133 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17134 DECL_CLONED_FUNCTION. */
17135 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
17136 targ_ptr, complain);
17137 if (spec == error_mark_node)
17138 return error_mark_node;
17140 /* Look for the clone. */
17141 FOR_EACH_CLONE (clone, spec)
17142 if (DECL_NAME (clone) == DECL_NAME (tmpl))
17143 return clone;
17144 /* We should always have found the clone by now. */
17145 gcc_unreachable ();
17146 return NULL_TREE;
17149 if (targ_ptr == error_mark_node)
17150 return error_mark_node;
17152 /* Check to see if we already have this specialization. */
17153 gen_tmpl = most_general_template (tmpl);
17154 if (tmpl != gen_tmpl)
17155 /* The TMPL is a partial instantiation. To get a full set of
17156 arguments we must add the arguments used to perform the
17157 partial instantiation. */
17158 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
17159 targ_ptr);
17161 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17162 but it doesn't seem to be on the hot path. */
17163 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
17165 gcc_assert (tmpl == gen_tmpl
17166 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
17167 == spec)
17168 || fndecl == NULL_TREE);
17170 if (spec != NULL_TREE)
17172 if (FNDECL_HAS_ACCESS_ERRORS (spec))
17174 if (complain & tf_error)
17175 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
17176 return error_mark_node;
17178 return spec;
17181 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
17182 complain))
17183 return error_mark_node;
17185 /* We are building a FUNCTION_DECL, during which the access of its
17186 parameters and return types have to be checked. However this
17187 FUNCTION_DECL which is the desired context for access checking
17188 is not built yet. We solve this chicken-and-egg problem by
17189 deferring all checks until we have the FUNCTION_DECL. */
17190 push_deferring_access_checks (dk_deferred);
17192 /* Instantiation of the function happens in the context of the function
17193 template, not the context of the overload resolution we're doing. */
17194 push_to_top_level ();
17195 /* If there are dependent arguments, e.g. because we're doing partial
17196 ordering, make sure processing_template_decl stays set. */
17197 if (uses_template_parms (targ_ptr))
17198 ++processing_template_decl;
17199 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17201 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
17202 complain, gen_tmpl, true);
17203 push_nested_class (ctx);
17206 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
17208 if (VAR_P (pattern))
17210 /* We need to determine if we're using a partial or explicit
17211 specialization now, because the type of the variable could be
17212 different. */
17213 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
17214 tree elt = most_specialized_partial_spec (tid, complain);
17215 if (elt == error_mark_node)
17216 pattern = error_mark_node;
17217 else if (elt)
17219 tmpl = TREE_VALUE (elt);
17220 pattern = DECL_TEMPLATE_RESULT (tmpl);
17221 targ_ptr = TREE_PURPOSE (elt);
17225 /* Substitute template parameters to obtain the specialization. */
17226 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
17227 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17228 pop_nested_class ();
17229 pop_from_top_level ();
17231 if (fndecl == error_mark_node)
17233 pop_deferring_access_checks ();
17234 return error_mark_node;
17237 /* The DECL_TI_TEMPLATE should always be the immediate parent
17238 template, not the most general template. */
17239 DECL_TI_TEMPLATE (fndecl) = tmpl;
17240 DECL_TI_ARGS (fndecl) = targ_ptr;
17242 /* Now we know the specialization, compute access previously
17243 deferred. */
17244 push_access_scope (fndecl);
17245 if (!perform_deferred_access_checks (complain))
17246 access_ok = false;
17247 pop_access_scope (fndecl);
17248 pop_deferring_access_checks ();
17250 /* If we've just instantiated the main entry point for a function,
17251 instantiate all the alternate entry points as well. We do this
17252 by cloning the instantiation of the main entry point, not by
17253 instantiating the template clones. */
17254 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
17255 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
17257 if (!access_ok)
17259 if (!(complain & tf_error))
17261 /* Remember to reinstantiate when we're out of SFINAE so the user
17262 can see the errors. */
17263 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
17265 return error_mark_node;
17267 return fndecl;
17270 /* Wrapper for instantiate_template_1. */
17272 tree
17273 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
17275 tree ret;
17276 timevar_push (TV_TEMPLATE_INST);
17277 ret = instantiate_template_1 (tmpl, orig_args, complain);
17278 timevar_pop (TV_TEMPLATE_INST);
17279 return ret;
17282 /* Instantiate the alias template TMPL with ARGS. Also push a template
17283 instantiation level, which instantiate_template doesn't do because
17284 functions and variables have sufficient context established by the
17285 callers. */
17287 static tree
17288 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
17290 struct pending_template *old_last_pend = last_pending_template;
17291 struct tinst_level *old_error_tinst = last_error_tinst_level;
17292 if (tmpl == error_mark_node || args == error_mark_node)
17293 return error_mark_node;
17294 tree tinst = build_tree_list (tmpl, args);
17295 if (!push_tinst_level (tinst))
17297 ggc_free (tinst);
17298 return error_mark_node;
17301 args =
17302 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
17303 args, tmpl, complain,
17304 /*require_all_args=*/true,
17305 /*use_default_args=*/true);
17307 tree r = instantiate_template (tmpl, args, complain);
17308 pop_tinst_level ();
17309 /* We can't free this if a pending_template entry or last_error_tinst_level
17310 is pointing at it. */
17311 if (last_pending_template == old_last_pend
17312 && last_error_tinst_level == old_error_tinst)
17313 ggc_free (tinst);
17315 return r;
17318 /* PARM is a template parameter pack for FN. Returns true iff
17319 PARM is used in a deducible way in the argument list of FN. */
17321 static bool
17322 pack_deducible_p (tree parm, tree fn)
17324 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
17325 for (; t; t = TREE_CHAIN (t))
17327 tree type = TREE_VALUE (t);
17328 tree packs;
17329 if (!PACK_EXPANSION_P (type))
17330 continue;
17331 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
17332 packs; packs = TREE_CHAIN (packs))
17333 if (template_args_equal (TREE_VALUE (packs), parm))
17335 /* The template parameter pack is used in a function parameter
17336 pack. If this is the end of the parameter list, the
17337 template parameter pack is deducible. */
17338 if (TREE_CHAIN (t) == void_list_node)
17339 return true;
17340 else
17341 /* Otherwise, not. Well, it could be deduced from
17342 a non-pack parameter, but doing so would end up with
17343 a deduction mismatch, so don't bother. */
17344 return false;
17347 /* The template parameter pack isn't used in any function parameter
17348 packs, but it might be used deeper, e.g. tuple<Args...>. */
17349 return true;
17352 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17353 NARGS elements of the arguments that are being used when calling
17354 it. TARGS is a vector into which the deduced template arguments
17355 are placed.
17357 Returns either a FUNCTION_DECL for the matching specialization of FN or
17358 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17359 true, diagnostics will be printed to explain why it failed.
17361 If FN is a conversion operator, or we are trying to produce a specific
17362 specialization, RETURN_TYPE is the return type desired.
17364 The EXPLICIT_TARGS are explicit template arguments provided via a
17365 template-id.
17367 The parameter STRICT is one of:
17369 DEDUCE_CALL:
17370 We are deducing arguments for a function call, as in
17371 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17372 deducing arguments for a call to the result of a conversion
17373 function template, as in [over.call.object].
17375 DEDUCE_CONV:
17376 We are deducing arguments for a conversion function, as in
17377 [temp.deduct.conv].
17379 DEDUCE_EXACT:
17380 We are deducing arguments when doing an explicit instantiation
17381 as in [temp.explicit], when determining an explicit specialization
17382 as in [temp.expl.spec], or when taking the address of a function
17383 template, as in [temp.deduct.funcaddr]. */
17385 tree
17386 fn_type_unification (tree fn,
17387 tree explicit_targs,
17388 tree targs,
17389 const tree *args,
17390 unsigned int nargs,
17391 tree return_type,
17392 unification_kind_t strict,
17393 int flags,
17394 bool explain_p,
17395 bool decltype_p)
17397 tree parms;
17398 tree fntype;
17399 tree decl = NULL_TREE;
17400 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17401 bool ok;
17402 static int deduction_depth;
17403 struct pending_template *old_last_pend = last_pending_template;
17404 struct tinst_level *old_error_tinst = last_error_tinst_level;
17405 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17406 tree tinst;
17407 tree r = error_mark_node;
17409 if (decltype_p)
17410 complain |= tf_decltype;
17412 /* In C++0x, it's possible to have a function template whose type depends
17413 on itself recursively. This is most obvious with decltype, but can also
17414 occur with enumeration scope (c++/48969). So we need to catch infinite
17415 recursion and reject the substitution at deduction time; this function
17416 will return error_mark_node for any repeated substitution.
17418 This also catches excessive recursion such as when f<N> depends on
17419 f<N-1> across all integers, and returns error_mark_node for all the
17420 substitutions back up to the initial one.
17422 This is, of course, not reentrant. */
17423 if (excessive_deduction_depth)
17424 return error_mark_node;
17425 tinst = build_tree_list (fn, NULL_TREE);
17426 ++deduction_depth;
17428 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
17430 fntype = TREE_TYPE (fn);
17431 if (explicit_targs)
17433 /* [temp.deduct]
17435 The specified template arguments must match the template
17436 parameters in kind (i.e., type, nontype, template), and there
17437 must not be more arguments than there are parameters;
17438 otherwise type deduction fails.
17440 Nontype arguments must match the types of the corresponding
17441 nontype template parameters, or must be convertible to the
17442 types of the corresponding nontype parameters as specified in
17443 _temp.arg.nontype_, otherwise type deduction fails.
17445 All references in the function type of the function template
17446 to the corresponding template parameters are replaced by the
17447 specified template argument values. If a substitution in a
17448 template parameter or in the function type of the function
17449 template results in an invalid type, type deduction fails. */
17450 int i, len = TREE_VEC_LENGTH (tparms);
17451 location_t loc = input_location;
17452 bool incomplete = false;
17454 /* Adjust any explicit template arguments before entering the
17455 substitution context. */
17456 explicit_targs
17457 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
17458 complain,
17459 /*require_all_args=*/false,
17460 /*use_default_args=*/false));
17461 if (explicit_targs == error_mark_node)
17462 goto fail;
17464 /* Substitute the explicit args into the function type. This is
17465 necessary so that, for instance, explicitly declared function
17466 arguments can match null pointed constants. If we were given
17467 an incomplete set of explicit args, we must not do semantic
17468 processing during substitution as we could create partial
17469 instantiations. */
17470 for (i = 0; i < len; i++)
17472 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17473 bool parameter_pack = false;
17474 tree targ = TREE_VEC_ELT (explicit_targs, i);
17476 /* Dig out the actual parm. */
17477 if (TREE_CODE (parm) == TYPE_DECL
17478 || TREE_CODE (parm) == TEMPLATE_DECL)
17480 parm = TREE_TYPE (parm);
17481 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
17483 else if (TREE_CODE (parm) == PARM_DECL)
17485 parm = DECL_INITIAL (parm);
17486 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
17489 if (!parameter_pack && targ == NULL_TREE)
17490 /* No explicit argument for this template parameter. */
17491 incomplete = true;
17493 if (parameter_pack && pack_deducible_p (parm, fn))
17495 /* Mark the argument pack as "incomplete". We could
17496 still deduce more arguments during unification.
17497 We remove this mark in type_unification_real. */
17498 if (targ)
17500 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
17501 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
17502 = ARGUMENT_PACK_ARGS (targ);
17505 /* We have some incomplete argument packs. */
17506 incomplete = true;
17510 TREE_VALUE (tinst) = explicit_targs;
17511 if (!push_tinst_level (tinst))
17513 excessive_deduction_depth = true;
17514 goto fail;
17516 processing_template_decl += incomplete;
17517 input_location = DECL_SOURCE_LOCATION (fn);
17518 /* Ignore any access checks; we'll see them again in
17519 instantiate_template and they might have the wrong
17520 access path at this point. */
17521 push_deferring_access_checks (dk_deferred);
17522 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
17523 complain | tf_partial, NULL_TREE);
17524 pop_deferring_access_checks ();
17525 input_location = loc;
17526 processing_template_decl -= incomplete;
17527 pop_tinst_level ();
17529 if (fntype == error_mark_node)
17530 goto fail;
17532 /* Place the explicitly specified arguments in TARGS. */
17533 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
17534 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
17537 /* Never do unification on the 'this' parameter. */
17538 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
17540 if (return_type && strict == DEDUCE_CALL)
17542 /* We're deducing for a call to the result of a template conversion
17543 function. The parms we really want are in return_type. */
17544 if (POINTER_TYPE_P (return_type))
17545 return_type = TREE_TYPE (return_type);
17546 parms = TYPE_ARG_TYPES (return_type);
17548 else if (return_type)
17550 tree *new_args;
17552 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
17553 new_args = XALLOCAVEC (tree, nargs + 1);
17554 new_args[0] = return_type;
17555 memcpy (new_args + 1, args, nargs * sizeof (tree));
17556 args = new_args;
17557 ++nargs;
17560 /* We allow incomplete unification without an error message here
17561 because the standard doesn't seem to explicitly prohibit it. Our
17562 callers must be ready to deal with unification failures in any
17563 event. */
17565 TREE_VALUE (tinst) = targs;
17566 /* If we aren't explaining yet, push tinst context so we can see where
17567 any errors (e.g. from class instantiations triggered by instantiation
17568 of default template arguments) come from. If we are explaining, this
17569 context is redundant. */
17570 if (!explain_p && !push_tinst_level (tinst))
17572 excessive_deduction_depth = true;
17573 goto fail;
17576 /* type_unification_real will pass back any access checks from default
17577 template argument substitution. */
17578 vec<deferred_access_check, va_gc> *checks;
17579 checks = NULL;
17581 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17582 targs, parms, args, nargs, /*subr=*/0,
17583 strict, flags, &checks, explain_p);
17584 if (!explain_p)
17585 pop_tinst_level ();
17586 if (!ok)
17587 goto fail;
17589 /* Now that we have bindings for all of the template arguments,
17590 ensure that the arguments deduced for the template template
17591 parameters have compatible template parameter lists. We cannot
17592 check this property before we have deduced all template
17593 arguments, because the template parameter types of a template
17594 template parameter might depend on prior template parameters
17595 deduced after the template template parameter. The following
17596 ill-formed example illustrates this issue:
17598 template<typename T, template<T> class C> void f(C<5>, T);
17600 template<int N> struct X {};
17602 void g() {
17603 f(X<5>(), 5l); // error: template argument deduction fails
17606 The template parameter list of 'C' depends on the template type
17607 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17608 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17609 time that we deduce 'C'. */
17610 if (!template_template_parm_bindings_ok_p
17611 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
17613 unify_inconsistent_template_template_parameters (explain_p);
17614 goto fail;
17617 /* All is well so far. Now, check:
17619 [temp.deduct]
17621 When all template arguments have been deduced, all uses of
17622 template parameters in nondeduced contexts are replaced with
17623 the corresponding deduced argument values. If the
17624 substitution results in an invalid type, as described above,
17625 type deduction fails. */
17626 TREE_VALUE (tinst) = targs;
17627 if (!push_tinst_level (tinst))
17629 excessive_deduction_depth = true;
17630 goto fail;
17633 /* Also collect access checks from the instantiation. */
17634 reopen_deferring_access_checks (checks);
17636 decl = instantiate_template (fn, targs, complain);
17638 checks = get_deferred_access_checks ();
17639 pop_deferring_access_checks ();
17641 pop_tinst_level ();
17643 if (decl == error_mark_node)
17644 goto fail;
17646 /* Now perform any access checks encountered during substitution. */
17647 push_access_scope (decl);
17648 ok = perform_access_checks (checks, complain);
17649 pop_access_scope (decl);
17650 if (!ok)
17651 goto fail;
17653 /* If we're looking for an exact match, check that what we got
17654 is indeed an exact match. It might not be if some template
17655 parameters are used in non-deduced contexts. But don't check
17656 for an exact match if we have dependent template arguments;
17657 in that case we're doing partial ordering, and we already know
17658 that we have two candidates that will provide the actual type. */
17659 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
17661 tree substed = TREE_TYPE (decl);
17662 unsigned int i;
17664 tree sarg
17665 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
17666 if (return_type)
17667 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
17668 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
17669 if (!same_type_p (args[i], TREE_VALUE (sarg)))
17671 unify_type_mismatch (explain_p, args[i],
17672 TREE_VALUE (sarg));
17673 goto fail;
17677 r = decl;
17679 fail:
17680 --deduction_depth;
17681 if (excessive_deduction_depth)
17683 if (deduction_depth == 0)
17684 /* Reset once we're all the way out. */
17685 excessive_deduction_depth = false;
17688 /* We can't free this if a pending_template entry or last_error_tinst_level
17689 is pointing at it. */
17690 if (last_pending_template == old_last_pend
17691 && last_error_tinst_level == old_error_tinst)
17692 ggc_free (tinst);
17694 return r;
17697 /* Adjust types before performing type deduction, as described in
17698 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17699 sections are symmetric. PARM is the type of a function parameter
17700 or the return type of the conversion function. ARG is the type of
17701 the argument passed to the call, or the type of the value
17702 initialized with the result of the conversion function.
17703 ARG_EXPR is the original argument expression, which may be null. */
17705 static int
17706 maybe_adjust_types_for_deduction (unification_kind_t strict,
17707 tree* parm,
17708 tree* arg,
17709 tree arg_expr)
17711 int result = 0;
17713 switch (strict)
17715 case DEDUCE_CALL:
17716 break;
17718 case DEDUCE_CONV:
17719 /* Swap PARM and ARG throughout the remainder of this
17720 function; the handling is precisely symmetric since PARM
17721 will initialize ARG rather than vice versa. */
17722 std::swap (parm, arg);
17723 break;
17725 case DEDUCE_EXACT:
17726 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17727 too, but here handle it by stripping the reference from PARM
17728 rather than by adding it to ARG. */
17729 if (TREE_CODE (*parm) == REFERENCE_TYPE
17730 && TYPE_REF_IS_RVALUE (*parm)
17731 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17732 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17733 && TREE_CODE (*arg) == REFERENCE_TYPE
17734 && !TYPE_REF_IS_RVALUE (*arg))
17735 *parm = TREE_TYPE (*parm);
17736 /* Nothing else to do in this case. */
17737 return 0;
17739 default:
17740 gcc_unreachable ();
17743 if (TREE_CODE (*parm) != REFERENCE_TYPE)
17745 /* [temp.deduct.call]
17747 If P is not a reference type:
17749 --If A is an array type, the pointer type produced by the
17750 array-to-pointer standard conversion (_conv.array_) is
17751 used in place of A for type deduction; otherwise,
17753 --If A is a function type, the pointer type produced by
17754 the function-to-pointer standard conversion
17755 (_conv.func_) is used in place of A for type deduction;
17756 otherwise,
17758 --If A is a cv-qualified type, the top level
17759 cv-qualifiers of A's type are ignored for type
17760 deduction. */
17761 if (TREE_CODE (*arg) == ARRAY_TYPE)
17762 *arg = build_pointer_type (TREE_TYPE (*arg));
17763 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
17764 *arg = build_pointer_type (*arg);
17765 else
17766 *arg = TYPE_MAIN_VARIANT (*arg);
17769 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17770 of the form T&&, where T is a template parameter, and the argument
17771 is an lvalue, T is deduced as A& */
17772 if (TREE_CODE (*parm) == REFERENCE_TYPE
17773 && TYPE_REF_IS_RVALUE (*parm)
17774 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17775 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17776 && (arg_expr ? real_lvalue_p (arg_expr)
17777 /* try_one_overload doesn't provide an arg_expr, but
17778 functions are always lvalues. */
17779 : TREE_CODE (*arg) == FUNCTION_TYPE))
17780 *arg = build_reference_type (*arg);
17782 /* [temp.deduct.call]
17784 If P is a cv-qualified type, the top level cv-qualifiers
17785 of P's type are ignored for type deduction. If P is a
17786 reference type, the type referred to by P is used for
17787 type deduction. */
17788 *parm = TYPE_MAIN_VARIANT (*parm);
17789 if (TREE_CODE (*parm) == REFERENCE_TYPE)
17791 *parm = TREE_TYPE (*parm);
17792 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17795 /* DR 322. For conversion deduction, remove a reference type on parm
17796 too (which has been swapped into ARG). */
17797 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
17798 *arg = TREE_TYPE (*arg);
17800 return result;
17803 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17804 template which does contain any deducible template parameters; check if
17805 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17806 unify_one_argument. */
17808 static int
17809 check_non_deducible_conversion (tree parm, tree arg, int strict,
17810 int flags, bool explain_p)
17812 tree type;
17814 if (!TYPE_P (arg))
17815 type = TREE_TYPE (arg);
17816 else
17817 type = arg;
17819 if (same_type_p (parm, type))
17820 return unify_success (explain_p);
17822 if (strict == DEDUCE_CONV)
17824 if (can_convert_arg (type, parm, NULL_TREE, flags,
17825 explain_p ? tf_warning_or_error : tf_none))
17826 return unify_success (explain_p);
17828 else if (strict != DEDUCE_EXACT)
17830 if (can_convert_arg (parm, type,
17831 TYPE_P (arg) ? NULL_TREE : arg,
17832 flags, explain_p ? tf_warning_or_error : tf_none))
17833 return unify_success (explain_p);
17836 if (strict == DEDUCE_EXACT)
17837 return unify_type_mismatch (explain_p, parm, arg);
17838 else
17839 return unify_arg_conversion (explain_p, parm, type, arg);
17842 static bool uses_deducible_template_parms (tree type);
17844 /* Returns true iff the expression EXPR is one from which a template
17845 argument can be deduced. In other words, if it's an undecorated
17846 use of a template non-type parameter. */
17848 static bool
17849 deducible_expression (tree expr)
17851 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
17854 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17855 deducible way; that is, if it has a max value of <PARM> - 1. */
17857 static bool
17858 deducible_array_bound (tree domain)
17860 if (domain == NULL_TREE)
17861 return false;
17863 tree max = TYPE_MAX_VALUE (domain);
17864 if (TREE_CODE (max) != MINUS_EXPR)
17865 return false;
17867 return deducible_expression (TREE_OPERAND (max, 0));
17870 /* Returns true iff the template arguments ARGS use a template parameter
17871 in a deducible way. */
17873 static bool
17874 deducible_template_args (tree args)
17876 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
17878 bool deducible;
17879 tree elt = TREE_VEC_ELT (args, i);
17880 if (ARGUMENT_PACK_P (elt))
17881 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
17882 else
17884 if (PACK_EXPANSION_P (elt))
17885 elt = PACK_EXPANSION_PATTERN (elt);
17886 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
17887 deducible = true;
17888 else if (TYPE_P (elt))
17889 deducible = uses_deducible_template_parms (elt);
17890 else
17891 deducible = deducible_expression (elt);
17893 if (deducible)
17894 return true;
17896 return false;
17899 /* Returns true iff TYPE contains any deducible references to template
17900 parameters, as per 14.8.2.5. */
17902 static bool
17903 uses_deducible_template_parms (tree type)
17905 if (PACK_EXPANSION_P (type))
17906 type = PACK_EXPANSION_PATTERN (type);
17908 /* T
17909 cv-list T
17910 TT<T>
17911 TT<i>
17912 TT<> */
17913 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17914 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17915 return true;
17917 /* T*
17919 T&& */
17920 if (POINTER_TYPE_P (type))
17921 return uses_deducible_template_parms (TREE_TYPE (type));
17923 /* T[integer-constant ]
17924 type [i] */
17925 if (TREE_CODE (type) == ARRAY_TYPE)
17926 return (uses_deducible_template_parms (TREE_TYPE (type))
17927 || deducible_array_bound (TYPE_DOMAIN (type)));
17929 /* T type ::*
17930 type T::*
17931 T T::*
17932 T (type ::*)()
17933 type (T::*)()
17934 type (type ::*)(T)
17935 type (T::*)(T)
17936 T (type ::*)(T)
17937 T (T::*)()
17938 T (T::*)(T) */
17939 if (TYPE_PTRMEM_P (type))
17940 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
17941 || (uses_deducible_template_parms
17942 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
17944 /* template-name <T> (where template-name refers to a class template)
17945 template-name <i> (where template-name refers to a class template) */
17946 if (CLASS_TYPE_P (type)
17947 && CLASSTYPE_TEMPLATE_INFO (type)
17948 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
17949 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
17950 (CLASSTYPE_TI_ARGS (type)));
17952 /* type (T)
17954 T(T) */
17955 if (TREE_CODE (type) == FUNCTION_TYPE
17956 || TREE_CODE (type) == METHOD_TYPE)
17958 if (uses_deducible_template_parms (TREE_TYPE (type)))
17959 return true;
17960 tree parm = TYPE_ARG_TYPES (type);
17961 if (TREE_CODE (type) == METHOD_TYPE)
17962 parm = TREE_CHAIN (parm);
17963 for (; parm; parm = TREE_CHAIN (parm))
17964 if (uses_deducible_template_parms (TREE_VALUE (parm)))
17965 return true;
17968 return false;
17971 /* Subroutine of type_unification_real and unify_pack_expansion to
17972 handle unification of a single P/A pair. Parameters are as
17973 for those functions. */
17975 static int
17976 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
17977 int subr, unification_kind_t strict,
17978 bool explain_p)
17980 tree arg_expr = NULL_TREE;
17981 int arg_strict;
17983 if (arg == error_mark_node || parm == error_mark_node)
17984 return unify_invalid (explain_p);
17985 if (arg == unknown_type_node)
17986 /* We can't deduce anything from this, but we might get all the
17987 template args from other function args. */
17988 return unify_success (explain_p);
17990 /* Implicit conversions (Clause 4) will be performed on a function
17991 argument to convert it to the type of the corresponding function
17992 parameter if the parameter type contains no template-parameters that
17993 participate in template argument deduction. */
17994 if (strict != DEDUCE_EXACT
17995 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
17996 /* For function parameters with no deducible template parameters,
17997 just return. We'll check non-dependent conversions later. */
17998 return unify_success (explain_p);
18000 switch (strict)
18002 case DEDUCE_CALL:
18003 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18004 | UNIFY_ALLOW_MORE_CV_QUAL
18005 | UNIFY_ALLOW_DERIVED);
18006 break;
18008 case DEDUCE_CONV:
18009 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18010 break;
18012 case DEDUCE_EXACT:
18013 arg_strict = UNIFY_ALLOW_NONE;
18014 break;
18016 default:
18017 gcc_unreachable ();
18020 /* We only do these transformations if this is the top-level
18021 parameter_type_list in a call or declaration matching; in other
18022 situations (nested function declarators, template argument lists) we
18023 won't be comparing a type to an expression, and we don't do any type
18024 adjustments. */
18025 if (!subr)
18027 if (!TYPE_P (arg))
18029 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18030 if (type_unknown_p (arg))
18032 /* [temp.deduct.type] A template-argument can be
18033 deduced from a pointer to function or pointer
18034 to member function argument if the set of
18035 overloaded functions does not contain function
18036 templates and at most one of a set of
18037 overloaded functions provides a unique
18038 match. */
18040 if (resolve_overloaded_unification
18041 (tparms, targs, parm, arg, strict,
18042 arg_strict, explain_p))
18043 return unify_success (explain_p);
18044 return unify_overload_resolution_failure (explain_p, arg);
18047 arg_expr = arg;
18048 arg = unlowered_expr_type (arg);
18049 if (arg == error_mark_node)
18050 return unify_invalid (explain_p);
18053 arg_strict |=
18054 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18056 else
18057 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18058 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18059 return unify_template_argument_mismatch (explain_p, parm, arg);
18061 /* For deduction from an init-list we need the actual list. */
18062 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18063 arg = arg_expr;
18064 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18067 /* Most parms like fn_type_unification.
18069 If SUBR is 1, we're being called recursively (to unify the
18070 arguments of a function or method parameter of a function
18071 template).
18073 CHECKS is a pointer to a vector of access checks encountered while
18074 substituting default template arguments. */
18076 static int
18077 type_unification_real (tree tparms,
18078 tree targs,
18079 tree xparms,
18080 const tree *xargs,
18081 unsigned int xnargs,
18082 int subr,
18083 unification_kind_t strict,
18084 int flags,
18085 vec<deferred_access_check, va_gc> **checks,
18086 bool explain_p)
18088 tree parm, arg;
18089 int i;
18090 int ntparms = TREE_VEC_LENGTH (tparms);
18091 int saw_undeduced = 0;
18092 tree parms;
18093 const tree *args;
18094 unsigned int nargs;
18095 unsigned int ia;
18097 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
18098 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
18099 gcc_assert (ntparms > 0);
18101 /* Reset the number of non-defaulted template arguments contained
18102 in TARGS. */
18103 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
18105 again:
18106 parms = xparms;
18107 args = xargs;
18108 nargs = xnargs;
18110 ia = 0;
18111 while (parms && parms != void_list_node
18112 && ia < nargs)
18114 parm = TREE_VALUE (parms);
18116 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18117 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
18118 /* For a function parameter pack that occurs at the end of the
18119 parameter-declaration-list, the type A of each remaining
18120 argument of the call is compared with the type P of the
18121 declarator-id of the function parameter pack. */
18122 break;
18124 parms = TREE_CHAIN (parms);
18126 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18127 /* For a function parameter pack that does not occur at the
18128 end of the parameter-declaration-list, the type of the
18129 parameter pack is a non-deduced context. */
18130 continue;
18132 arg = args[ia];
18133 ++ia;
18135 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
18136 explain_p))
18137 return 1;
18140 if (parms
18141 && parms != void_list_node
18142 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
18144 /* Unify the remaining arguments with the pack expansion type. */
18145 tree argvec;
18146 tree parmvec = make_tree_vec (1);
18148 /* Allocate a TREE_VEC and copy in all of the arguments */
18149 argvec = make_tree_vec (nargs - ia);
18150 for (i = 0; ia < nargs; ++ia, ++i)
18151 TREE_VEC_ELT (argvec, i) = args[ia];
18153 /* Copy the parameter into parmvec. */
18154 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
18155 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
18156 /*subr=*/subr, explain_p))
18157 return 1;
18159 /* Advance to the end of the list of parameters. */
18160 parms = TREE_CHAIN (parms);
18163 /* Fail if we've reached the end of the parm list, and more args
18164 are present, and the parm list isn't variadic. */
18165 if (ia < nargs && parms == void_list_node)
18166 return unify_too_many_arguments (explain_p, nargs, ia);
18167 /* Fail if parms are left and they don't have default values and
18168 they aren't all deduced as empty packs (c++/57397). This is
18169 consistent with sufficient_parms_p. */
18170 if (parms && parms != void_list_node
18171 && TREE_PURPOSE (parms) == NULL_TREE)
18173 unsigned int count = nargs;
18174 tree p = parms;
18175 bool type_pack_p;
18178 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
18179 if (!type_pack_p)
18180 count++;
18181 p = TREE_CHAIN (p);
18183 while (p && p != void_list_node);
18184 if (count != nargs)
18185 return unify_too_few_arguments (explain_p, ia, count,
18186 type_pack_p);
18189 if (!subr)
18191 tsubst_flags_t complain = (explain_p
18192 ? tf_warning_or_error
18193 : tf_none);
18195 for (i = 0; i < ntparms; i++)
18197 tree targ = TREE_VEC_ELT (targs, i);
18198 tree tparm = TREE_VEC_ELT (tparms, i);
18200 /* Clear the "incomplete" flags on all argument packs now so that
18201 substituting them into later default arguments works. */
18202 if (targ && ARGUMENT_PACK_P (targ))
18204 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
18205 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
18208 if (targ || tparm == error_mark_node)
18209 continue;
18210 tparm = TREE_VALUE (tparm);
18212 /* If this is an undeduced nontype parameter that depends on
18213 a type parameter, try another pass; its type may have been
18214 deduced from a later argument than the one from which
18215 this parameter can be deduced. */
18216 if (TREE_CODE (tparm) == PARM_DECL
18217 && uses_template_parms (TREE_TYPE (tparm))
18218 && saw_undeduced < 2)
18220 saw_undeduced = 1;
18221 continue;
18224 /* Core issue #226 (C++0x) [temp.deduct]:
18226 If a template argument has not been deduced, its
18227 default template argument, if any, is used.
18229 When we are in C++98 mode, TREE_PURPOSE will either
18230 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18231 to explicitly check cxx_dialect here. */
18232 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
18233 /* OK, there is a default argument. Wait until after the
18234 conversion check to do substitution. */
18235 continue;
18237 /* If the type parameter is a parameter pack, then it will
18238 be deduced to an empty parameter pack. */
18239 if (template_parameter_pack_p (tparm))
18241 tree arg;
18243 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
18245 arg = make_node (NONTYPE_ARGUMENT_PACK);
18246 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
18247 TREE_CONSTANT (arg) = 1;
18249 else
18250 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
18252 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
18254 TREE_VEC_ELT (targs, i) = arg;
18255 continue;
18258 return unify_parameter_deduction_failure (explain_p, tparm);
18261 /* DR 1391: All parameters have args, now check non-dependent parms for
18262 convertibility. */
18263 if (saw_undeduced < 2)
18264 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
18265 parms && parms != void_list_node && ia < nargs; )
18267 parm = TREE_VALUE (parms);
18269 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18270 && (!TREE_CHAIN (parms)
18271 || TREE_CHAIN (parms) == void_list_node))
18272 /* For a function parameter pack that occurs at the end of the
18273 parameter-declaration-list, the type A of each remaining
18274 argument of the call is compared with the type P of the
18275 declarator-id of the function parameter pack. */
18276 break;
18278 parms = TREE_CHAIN (parms);
18280 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18281 /* For a function parameter pack that does not occur at the
18282 end of the parameter-declaration-list, the type of the
18283 parameter pack is a non-deduced context. */
18284 continue;
18286 arg = args[ia];
18287 ++ia;
18289 if (uses_template_parms (parm))
18290 continue;
18291 if (check_non_deducible_conversion (parm, arg, strict, flags,
18292 explain_p))
18293 return 1;
18296 /* Now substitute into the default template arguments. */
18297 for (i = 0; i < ntparms; i++)
18299 tree targ = TREE_VEC_ELT (targs, i);
18300 tree tparm = TREE_VEC_ELT (tparms, i);
18302 if (targ || tparm == error_mark_node)
18303 continue;
18304 tree parm = TREE_VALUE (tparm);
18306 if (TREE_CODE (parm) == PARM_DECL
18307 && uses_template_parms (TREE_TYPE (parm))
18308 && saw_undeduced < 2)
18309 continue;
18311 tree arg = TREE_PURPOSE (tparm);
18312 reopen_deferring_access_checks (*checks);
18313 location_t save_loc = input_location;
18314 if (DECL_P (parm))
18315 input_location = DECL_SOURCE_LOCATION (parm);
18316 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
18317 arg = convert_template_argument (parm, arg, targs, complain,
18318 i, NULL_TREE);
18319 input_location = save_loc;
18320 *checks = get_deferred_access_checks ();
18321 pop_deferring_access_checks ();
18322 if (arg == error_mark_node)
18323 return 1;
18324 else
18326 TREE_VEC_ELT (targs, i) = arg;
18327 /* The position of the first default template argument,
18328 is also the number of non-defaulted arguments in TARGS.
18329 Record that. */
18330 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18331 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
18332 continue;
18336 if (saw_undeduced++ == 1)
18337 goto again;
18340 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18341 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
18343 return unify_success (explain_p);
18346 /* Subroutine of type_unification_real. Args are like the variables
18347 at the call site. ARG is an overloaded function (or template-id);
18348 we try deducing template args from each of the overloads, and if
18349 only one succeeds, we go with that. Modifies TARGS and returns
18350 true on success. */
18352 static bool
18353 resolve_overloaded_unification (tree tparms,
18354 tree targs,
18355 tree parm,
18356 tree arg,
18357 unification_kind_t strict,
18358 int sub_strict,
18359 bool explain_p)
18361 tree tempargs = copy_node (targs);
18362 int good = 0;
18363 tree goodfn = NULL_TREE;
18364 bool addr_p;
18366 if (TREE_CODE (arg) == ADDR_EXPR)
18368 arg = TREE_OPERAND (arg, 0);
18369 addr_p = true;
18371 else
18372 addr_p = false;
18374 if (TREE_CODE (arg) == COMPONENT_REF)
18375 /* Handle `&x' where `x' is some static or non-static member
18376 function name. */
18377 arg = TREE_OPERAND (arg, 1);
18379 if (TREE_CODE (arg) == OFFSET_REF)
18380 arg = TREE_OPERAND (arg, 1);
18382 /* Strip baselink information. */
18383 if (BASELINK_P (arg))
18384 arg = BASELINK_FUNCTIONS (arg);
18386 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
18388 /* If we got some explicit template args, we need to plug them into
18389 the affected templates before we try to unify, in case the
18390 explicit args will completely resolve the templates in question. */
18392 int ok = 0;
18393 tree expl_subargs = TREE_OPERAND (arg, 1);
18394 arg = TREE_OPERAND (arg, 0);
18396 for (; arg; arg = OVL_NEXT (arg))
18398 tree fn = OVL_CURRENT (arg);
18399 tree subargs, elem;
18401 if (TREE_CODE (fn) != TEMPLATE_DECL)
18402 continue;
18404 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18405 expl_subargs, NULL_TREE, tf_none,
18406 /*require_all_args=*/true,
18407 /*use_default_args=*/true);
18408 if (subargs != error_mark_node
18409 && !any_dependent_template_arguments_p (subargs))
18411 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
18412 if (try_one_overload (tparms, targs, tempargs, parm,
18413 elem, strict, sub_strict, addr_p, explain_p)
18414 && (!goodfn || !same_type_p (goodfn, elem)))
18416 goodfn = elem;
18417 ++good;
18420 else if (subargs)
18421 ++ok;
18423 /* If no templates (or more than one) are fully resolved by the
18424 explicit arguments, this template-id is a non-deduced context; it
18425 could still be OK if we deduce all template arguments for the
18426 enclosing call through other arguments. */
18427 if (good != 1)
18428 good = ok;
18430 else if (TREE_CODE (arg) != OVERLOAD
18431 && TREE_CODE (arg) != FUNCTION_DECL)
18432 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18433 -- but the deduction does not succeed because the expression is
18434 not just the function on its own. */
18435 return false;
18436 else
18437 for (; arg; arg = OVL_NEXT (arg))
18438 if (try_one_overload (tparms, targs, tempargs, parm,
18439 TREE_TYPE (OVL_CURRENT (arg)),
18440 strict, sub_strict, addr_p, explain_p)
18441 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
18443 goodfn = OVL_CURRENT (arg);
18444 ++good;
18447 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18448 to function or pointer to member function argument if the set of
18449 overloaded functions does not contain function templates and at most
18450 one of a set of overloaded functions provides a unique match.
18452 So if we found multiple possibilities, we return success but don't
18453 deduce anything. */
18455 if (good == 1)
18457 int i = TREE_VEC_LENGTH (targs);
18458 for (; i--; )
18459 if (TREE_VEC_ELT (tempargs, i))
18461 tree old = TREE_VEC_ELT (targs, i);
18462 tree new_ = TREE_VEC_ELT (tempargs, i);
18463 if (new_ && old && ARGUMENT_PACK_P (old)
18464 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
18465 /* Don't forget explicit template arguments in a pack. */
18466 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
18467 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
18468 TREE_VEC_ELT (targs, i) = new_;
18471 if (good)
18472 return true;
18474 return false;
18477 /* Core DR 115: In contexts where deduction is done and fails, or in
18478 contexts where deduction is not done, if a template argument list is
18479 specified and it, along with any default template arguments, identifies
18480 a single function template specialization, then the template-id is an
18481 lvalue for the function template specialization. */
18483 tree
18484 resolve_nondeduced_context (tree orig_expr)
18486 tree expr, offset, baselink;
18487 bool addr;
18489 if (!type_unknown_p (orig_expr))
18490 return orig_expr;
18492 expr = orig_expr;
18493 addr = false;
18494 offset = NULL_TREE;
18495 baselink = NULL_TREE;
18497 if (TREE_CODE (expr) == ADDR_EXPR)
18499 expr = TREE_OPERAND (expr, 0);
18500 addr = true;
18502 if (TREE_CODE (expr) == OFFSET_REF)
18504 offset = expr;
18505 expr = TREE_OPERAND (expr, 1);
18507 if (BASELINK_P (expr))
18509 baselink = expr;
18510 expr = BASELINK_FUNCTIONS (expr);
18513 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
18515 int good = 0;
18516 tree goodfn = NULL_TREE;
18518 /* If we got some explicit template args, we need to plug them into
18519 the affected templates before we try to unify, in case the
18520 explicit args will completely resolve the templates in question. */
18522 tree expl_subargs = TREE_OPERAND (expr, 1);
18523 tree arg = TREE_OPERAND (expr, 0);
18524 tree badfn = NULL_TREE;
18525 tree badargs = NULL_TREE;
18527 for (; arg; arg = OVL_NEXT (arg))
18529 tree fn = OVL_CURRENT (arg);
18530 tree subargs, elem;
18532 if (TREE_CODE (fn) != TEMPLATE_DECL)
18533 continue;
18535 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18536 expl_subargs, NULL_TREE, tf_none,
18537 /*require_all_args=*/true,
18538 /*use_default_args=*/true);
18539 if (subargs != error_mark_node
18540 && !any_dependent_template_arguments_p (subargs))
18542 elem = instantiate_template (fn, subargs, tf_none);
18543 if (elem == error_mark_node)
18545 badfn = fn;
18546 badargs = subargs;
18548 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
18550 goodfn = elem;
18551 ++good;
18555 if (good == 1)
18557 mark_used (goodfn);
18558 expr = goodfn;
18559 if (baselink)
18560 expr = build_baselink (BASELINK_BINFO (baselink),
18561 BASELINK_ACCESS_BINFO (baselink),
18562 expr, BASELINK_OPTYPE (baselink));
18563 if (offset)
18565 tree base
18566 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
18567 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
18569 if (addr)
18570 expr = cp_build_addr_expr (expr, tf_warning_or_error);
18571 return expr;
18573 else if (good == 0 && badargs)
18574 /* There were no good options and at least one bad one, so let the
18575 user know what the problem is. */
18576 instantiate_template (badfn, badargs, tf_warning_or_error);
18578 return orig_expr;
18581 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18582 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18583 different overloads deduce different arguments for a given parm.
18584 ADDR_P is true if the expression for which deduction is being
18585 performed was of the form "& fn" rather than simply "fn".
18587 Returns 1 on success. */
18589 static int
18590 try_one_overload (tree tparms,
18591 tree orig_targs,
18592 tree targs,
18593 tree parm,
18594 tree arg,
18595 unification_kind_t strict,
18596 int sub_strict,
18597 bool addr_p,
18598 bool explain_p)
18600 int nargs;
18601 tree tempargs;
18602 int i;
18604 if (arg == error_mark_node)
18605 return 0;
18607 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18608 to function or pointer to member function argument if the set of
18609 overloaded functions does not contain function templates and at most
18610 one of a set of overloaded functions provides a unique match.
18612 So if this is a template, just return success. */
18614 if (uses_template_parms (arg))
18615 return 1;
18617 if (TREE_CODE (arg) == METHOD_TYPE)
18618 arg = build_ptrmemfunc_type (build_pointer_type (arg));
18619 else if (addr_p)
18620 arg = build_pointer_type (arg);
18622 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
18624 /* We don't copy orig_targs for this because if we have already deduced
18625 some template args from previous args, unify would complain when we
18626 try to deduce a template parameter for the same argument, even though
18627 there isn't really a conflict. */
18628 nargs = TREE_VEC_LENGTH (targs);
18629 tempargs = make_tree_vec (nargs);
18631 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
18632 return 0;
18634 /* First make sure we didn't deduce anything that conflicts with
18635 explicitly specified args. */
18636 for (i = nargs; i--; )
18638 tree elt = TREE_VEC_ELT (tempargs, i);
18639 tree oldelt = TREE_VEC_ELT (orig_targs, i);
18641 if (!elt)
18642 /*NOP*/;
18643 else if (uses_template_parms (elt))
18644 /* Since we're unifying against ourselves, we will fill in
18645 template args used in the function parm list with our own
18646 template parms. Discard them. */
18647 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
18648 else if (oldelt && !template_args_equal (oldelt, elt))
18649 return 0;
18652 for (i = nargs; i--; )
18654 tree elt = TREE_VEC_ELT (tempargs, i);
18656 if (elt)
18657 TREE_VEC_ELT (targs, i) = elt;
18660 return 1;
18663 /* PARM is a template class (perhaps with unbound template
18664 parameters). ARG is a fully instantiated type. If ARG can be
18665 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
18666 TARGS are as for unify. */
18668 static tree
18669 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
18670 bool explain_p)
18672 tree copy_of_targs;
18674 if (!CLASSTYPE_TEMPLATE_INFO (arg)
18675 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
18676 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
18677 return NULL_TREE;
18679 /* We need to make a new template argument vector for the call to
18680 unify. If we used TARGS, we'd clutter it up with the result of
18681 the attempted unification, even if this class didn't work out.
18682 We also don't want to commit ourselves to all the unifications
18683 we've already done, since unification is supposed to be done on
18684 an argument-by-argument basis. In other words, consider the
18685 following pathological case:
18687 template <int I, int J, int K>
18688 struct S {};
18690 template <int I, int J>
18691 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
18693 template <int I, int J, int K>
18694 void f(S<I, J, K>, S<I, I, I>);
18696 void g() {
18697 S<0, 0, 0> s0;
18698 S<0, 1, 2> s2;
18700 f(s0, s2);
18703 Now, by the time we consider the unification involving `s2', we
18704 already know that we must have `f<0, 0, 0>'. But, even though
18705 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
18706 because there are two ways to unify base classes of S<0, 1, 2>
18707 with S<I, I, I>. If we kept the already deduced knowledge, we
18708 would reject the possibility I=1. */
18709 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
18711 /* If unification failed, we're done. */
18712 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
18713 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
18714 return NULL_TREE;
18716 return arg;
18719 /* Given a template type PARM and a class type ARG, find the unique
18720 base type in ARG that is an instance of PARM. We do not examine
18721 ARG itself; only its base-classes. If there is not exactly one
18722 appropriate base class, return NULL_TREE. PARM may be the type of
18723 a partial specialization, as well as a plain template type. Used
18724 by unify. */
18726 static enum template_base_result
18727 get_template_base (tree tparms, tree targs, tree parm, tree arg,
18728 bool explain_p, tree *result)
18730 tree rval = NULL_TREE;
18731 tree binfo;
18733 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
18735 binfo = TYPE_BINFO (complete_type (arg));
18736 if (!binfo)
18738 /* The type could not be completed. */
18739 *result = NULL_TREE;
18740 return tbr_incomplete_type;
18743 /* Walk in inheritance graph order. The search order is not
18744 important, and this avoids multiple walks of virtual bases. */
18745 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
18747 tree r = try_class_unification (tparms, targs, parm,
18748 BINFO_TYPE (binfo), explain_p);
18750 if (r)
18752 /* If there is more than one satisfactory baseclass, then:
18754 [temp.deduct.call]
18756 If they yield more than one possible deduced A, the type
18757 deduction fails.
18759 applies. */
18760 if (rval && !same_type_p (r, rval))
18762 *result = NULL_TREE;
18763 return tbr_ambiguous_baseclass;
18766 rval = r;
18770 *result = rval;
18771 return tbr_success;
18774 /* Returns the level of DECL, which declares a template parameter. */
18776 static int
18777 template_decl_level (tree decl)
18779 switch (TREE_CODE (decl))
18781 case TYPE_DECL:
18782 case TEMPLATE_DECL:
18783 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
18785 case PARM_DECL:
18786 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
18788 default:
18789 gcc_unreachable ();
18791 return 0;
18794 /* Decide whether ARG can be unified with PARM, considering only the
18795 cv-qualifiers of each type, given STRICT as documented for unify.
18796 Returns nonzero iff the unification is OK on that basis. */
18798 static int
18799 check_cv_quals_for_unify (int strict, tree arg, tree parm)
18801 int arg_quals = cp_type_quals (arg);
18802 int parm_quals = cp_type_quals (parm);
18804 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18805 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18807 /* Although a CVR qualifier is ignored when being applied to a
18808 substituted template parameter ([8.3.2]/1 for example), that
18809 does not allow us to unify "const T" with "int&" because both
18810 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18811 It is ok when we're allowing additional CV qualifiers
18812 at the outer level [14.8.2.1]/3,1st bullet. */
18813 if ((TREE_CODE (arg) == REFERENCE_TYPE
18814 || TREE_CODE (arg) == FUNCTION_TYPE
18815 || TREE_CODE (arg) == METHOD_TYPE)
18816 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
18817 return 0;
18819 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
18820 && (parm_quals & TYPE_QUAL_RESTRICT))
18821 return 0;
18824 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18825 && (arg_quals & parm_quals) != parm_quals)
18826 return 0;
18828 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
18829 && (parm_quals & arg_quals) != arg_quals)
18830 return 0;
18832 return 1;
18835 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18836 void
18837 template_parm_level_and_index (tree parm, int* level, int* index)
18839 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18840 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18841 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18843 *index = TEMPLATE_TYPE_IDX (parm);
18844 *level = TEMPLATE_TYPE_LEVEL (parm);
18846 else
18848 *index = TEMPLATE_PARM_IDX (parm);
18849 *level = TEMPLATE_PARM_LEVEL (parm);
18853 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18854 do { \
18855 if (unify (TP, TA, P, A, S, EP)) \
18856 return 1; \
18857 } while (0);
18859 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18860 expansion at the end of PACKED_PARMS. Returns 0 if the type
18861 deduction succeeds, 1 otherwise. STRICT is the same as in
18862 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18863 call argument list. We'll need to adjust the arguments to make them
18864 types. SUBR tells us if this is from a recursive call to
18865 type_unification_real, or for comparing two template argument
18866 lists. */
18868 static int
18869 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
18870 tree packed_args, unification_kind_t strict,
18871 bool subr, bool explain_p)
18873 tree parm
18874 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
18875 tree pattern = PACK_EXPANSION_PATTERN (parm);
18876 tree pack, packs = NULL_TREE;
18877 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
18879 packed_args = expand_template_argument_pack (packed_args);
18881 int len = TREE_VEC_LENGTH (packed_args);
18883 /* Determine the parameter packs we will be deducing from the
18884 pattern, and record their current deductions. */
18885 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
18886 pack; pack = TREE_CHAIN (pack))
18888 tree parm_pack = TREE_VALUE (pack);
18889 int idx, level;
18891 /* Determine the index and level of this parameter pack. */
18892 template_parm_level_and_index (parm_pack, &level, &idx);
18894 /* Keep track of the parameter packs and their corresponding
18895 argument packs. */
18896 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
18897 TREE_TYPE (packs) = make_tree_vec (len - start);
18900 /* Loop through all of the arguments that have not yet been
18901 unified and unify each with the pattern. */
18902 for (i = start; i < len; i++)
18904 tree parm;
18905 bool any_explicit = false;
18906 tree arg = TREE_VEC_ELT (packed_args, i);
18908 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18909 or the element of its argument pack at the current index if
18910 this argument was explicitly specified. */
18911 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18913 int idx, level;
18914 tree arg, pargs;
18915 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18917 arg = NULL_TREE;
18918 if (TREE_VALUE (pack)
18919 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
18920 && (i - start < TREE_VEC_LENGTH (pargs)))
18922 any_explicit = true;
18923 arg = TREE_VEC_ELT (pargs, i - start);
18925 TMPL_ARG (targs, level, idx) = arg;
18928 /* If we had explicit template arguments, substitute them into the
18929 pattern before deduction. */
18930 if (any_explicit)
18932 /* Some arguments might still be unspecified or dependent. */
18933 bool dependent;
18934 ++processing_template_decl;
18935 dependent = any_dependent_template_arguments_p (targs);
18936 if (!dependent)
18937 --processing_template_decl;
18938 parm = tsubst (pattern, targs,
18939 explain_p ? tf_warning_or_error : tf_none,
18940 NULL_TREE);
18941 if (dependent)
18942 --processing_template_decl;
18943 if (parm == error_mark_node)
18944 return 1;
18946 else
18947 parm = pattern;
18949 /* Unify the pattern with the current argument. */
18950 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
18951 explain_p))
18952 return 1;
18954 /* For each parameter pack, collect the deduced value. */
18955 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18957 int idx, level;
18958 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18960 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
18961 TMPL_ARG (targs, level, idx);
18965 /* Verify that the results of unification with the parameter packs
18966 produce results consistent with what we've seen before, and make
18967 the deduced argument packs available. */
18968 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18970 tree old_pack = TREE_VALUE (pack);
18971 tree new_args = TREE_TYPE (pack);
18972 int i, len = TREE_VEC_LENGTH (new_args);
18973 int idx, level;
18974 bool nondeduced_p = false;
18976 /* By default keep the original deduced argument pack.
18977 If necessary, more specific code is going to update the
18978 resulting deduced argument later down in this function. */
18979 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18980 TMPL_ARG (targs, level, idx) = old_pack;
18982 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
18983 actually deduce anything. */
18984 for (i = 0; i < len && !nondeduced_p; ++i)
18985 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
18986 nondeduced_p = true;
18987 if (nondeduced_p)
18988 continue;
18990 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
18992 /* If we had fewer function args than explicit template args,
18993 just use the explicits. */
18994 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
18995 int explicit_len = TREE_VEC_LENGTH (explicit_args);
18996 if (len < explicit_len)
18997 new_args = explicit_args;
19000 if (!old_pack)
19002 tree result;
19003 /* Build the deduced *_ARGUMENT_PACK. */
19004 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
19006 result = make_node (NONTYPE_ARGUMENT_PACK);
19007 TREE_TYPE (result) =
19008 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
19009 TREE_CONSTANT (result) = 1;
19011 else
19012 result = cxx_make_type (TYPE_ARGUMENT_PACK);
19014 SET_ARGUMENT_PACK_ARGS (result, new_args);
19016 /* Note the deduced argument packs for this parameter
19017 pack. */
19018 TMPL_ARG (targs, level, idx) = result;
19020 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
19021 && (ARGUMENT_PACK_ARGS (old_pack)
19022 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
19024 /* We only had the explicitly-provided arguments before, but
19025 now we have a complete set of arguments. */
19026 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19028 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
19029 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
19030 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
19032 else
19034 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
19035 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
19037 if (!comp_template_args_with_info (old_args, new_args,
19038 &bad_old_arg, &bad_new_arg))
19039 /* Inconsistent unification of this parameter pack. */
19040 return unify_parameter_pack_inconsistent (explain_p,
19041 bad_old_arg,
19042 bad_new_arg);
19046 return unify_success (explain_p);
19049 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19050 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19051 parameters and return value are as for unify. */
19053 static int
19054 unify_array_domain (tree tparms, tree targs,
19055 tree parm_dom, tree arg_dom,
19056 bool explain_p)
19058 tree parm_max;
19059 tree arg_max;
19060 bool parm_cst;
19061 bool arg_cst;
19063 /* Our representation of array types uses "N - 1" as the
19064 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19065 not an integer constant. We cannot unify arbitrarily
19066 complex expressions, so we eliminate the MINUS_EXPRs
19067 here. */
19068 parm_max = TYPE_MAX_VALUE (parm_dom);
19069 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
19070 if (!parm_cst)
19072 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
19073 parm_max = TREE_OPERAND (parm_max, 0);
19075 arg_max = TYPE_MAX_VALUE (arg_dom);
19076 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
19077 if (!arg_cst)
19079 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19080 trying to unify the type of a variable with the type
19081 of a template parameter. For example:
19083 template <unsigned int N>
19084 void f (char (&) [N]);
19085 int g();
19086 void h(int i) {
19087 char a[g(i)];
19088 f(a);
19091 Here, the type of the ARG will be "int [g(i)]", and
19092 may be a SAVE_EXPR, etc. */
19093 if (TREE_CODE (arg_max) != MINUS_EXPR)
19094 return unify_vla_arg (explain_p, arg_dom);
19095 arg_max = TREE_OPERAND (arg_max, 0);
19098 /* If only one of the bounds used a MINUS_EXPR, compensate
19099 by adding one to the other bound. */
19100 if (parm_cst && !arg_cst)
19101 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
19102 integer_type_node,
19103 parm_max,
19104 integer_one_node);
19105 else if (arg_cst && !parm_cst)
19106 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
19107 integer_type_node,
19108 arg_max,
19109 integer_one_node);
19111 return unify (tparms, targs, parm_max, arg_max,
19112 UNIFY_ALLOW_INTEGER, explain_p);
19115 /* Deduce the value of template parameters. TPARMS is the (innermost)
19116 set of template parameters to a template. TARGS is the bindings
19117 for those template parameters, as determined thus far; TARGS may
19118 include template arguments for outer levels of template parameters
19119 as well. PARM is a parameter to a template function, or a
19120 subcomponent of that parameter; ARG is the corresponding argument.
19121 This function attempts to match PARM with ARG in a manner
19122 consistent with the existing assignments in TARGS. If more values
19123 are deduced, then TARGS is updated.
19125 Returns 0 if the type deduction succeeds, 1 otherwise. The
19126 parameter STRICT is a bitwise or of the following flags:
19128 UNIFY_ALLOW_NONE:
19129 Require an exact match between PARM and ARG.
19130 UNIFY_ALLOW_MORE_CV_QUAL:
19131 Allow the deduced ARG to be more cv-qualified (by qualification
19132 conversion) than ARG.
19133 UNIFY_ALLOW_LESS_CV_QUAL:
19134 Allow the deduced ARG to be less cv-qualified than ARG.
19135 UNIFY_ALLOW_DERIVED:
19136 Allow the deduced ARG to be a template base class of ARG,
19137 or a pointer to a template base class of the type pointed to by
19138 ARG.
19139 UNIFY_ALLOW_INTEGER:
19140 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19141 case for more information.
19142 UNIFY_ALLOW_OUTER_LEVEL:
19143 This is the outermost level of a deduction. Used to determine validity
19144 of qualification conversions. A valid qualification conversion must
19145 have const qualified pointers leading up to the inner type which
19146 requires additional CV quals, except at the outer level, where const
19147 is not required [conv.qual]. It would be normal to set this flag in
19148 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19149 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19150 This is the outermost level of a deduction, and PARM can be more CV
19151 qualified at this point.
19152 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19153 This is the outermost level of a deduction, and PARM can be less CV
19154 qualified at this point. */
19156 static int
19157 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
19158 bool explain_p)
19160 int idx;
19161 tree targ;
19162 tree tparm;
19163 int strict_in = strict;
19165 /* I don't think this will do the right thing with respect to types.
19166 But the only case I've seen it in so far has been array bounds, where
19167 signedness is the only information lost, and I think that will be
19168 okay. */
19169 while (TREE_CODE (parm) == NOP_EXPR)
19170 parm = TREE_OPERAND (parm, 0);
19172 if (arg == error_mark_node)
19173 return unify_invalid (explain_p);
19174 if (arg == unknown_type_node
19175 || arg == init_list_type_node)
19176 /* We can't deduce anything from this, but we might get all the
19177 template args from other function args. */
19178 return unify_success (explain_p);
19180 /* If PARM uses template parameters, then we can't bail out here,
19181 even if ARG == PARM, since we won't record unifications for the
19182 template parameters. We might need them if we're trying to
19183 figure out which of two things is more specialized. */
19184 if (arg == parm && !uses_template_parms (parm))
19185 return unify_success (explain_p);
19187 /* Handle init lists early, so the rest of the function can assume
19188 we're dealing with a type. */
19189 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
19191 tree elt, elttype;
19192 unsigned i;
19193 tree orig_parm = parm;
19195 /* Replace T with std::initializer_list<T> for deduction. */
19196 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19197 && flag_deduce_init_list)
19198 parm = listify (parm);
19200 if (!is_std_init_list (parm)
19201 && TREE_CODE (parm) != ARRAY_TYPE)
19202 /* We can only deduce from an initializer list argument if the
19203 parameter is std::initializer_list or an array; otherwise this
19204 is a non-deduced context. */
19205 return unify_success (explain_p);
19207 if (TREE_CODE (parm) == ARRAY_TYPE)
19208 elttype = TREE_TYPE (parm);
19209 else
19211 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
19212 /* Deduction is defined in terms of a single type, so just punt
19213 on the (bizarre) std::initializer_list<T...>. */
19214 if (PACK_EXPANSION_P (elttype))
19215 return unify_success (explain_p);
19218 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
19220 int elt_strict = strict;
19222 if (elt == error_mark_node)
19223 return unify_invalid (explain_p);
19225 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
19227 tree type = TREE_TYPE (elt);
19228 if (type == error_mark_node)
19229 return unify_invalid (explain_p);
19230 /* It should only be possible to get here for a call. */
19231 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
19232 elt_strict |= maybe_adjust_types_for_deduction
19233 (DEDUCE_CALL, &elttype, &type, elt);
19234 elt = type;
19237 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
19238 explain_p);
19241 if (TREE_CODE (parm) == ARRAY_TYPE
19242 && deducible_array_bound (TYPE_DOMAIN (parm)))
19244 /* Also deduce from the length of the initializer list. */
19245 tree max = size_int (CONSTRUCTOR_NELTS (arg));
19246 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
19247 if (idx == error_mark_node)
19248 return unify_invalid (explain_p);
19249 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19250 idx, explain_p);
19253 /* If the std::initializer_list<T> deduction worked, replace the
19254 deduced A with std::initializer_list<A>. */
19255 if (orig_parm != parm)
19257 idx = TEMPLATE_TYPE_IDX (orig_parm);
19258 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19259 targ = listify (targ);
19260 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
19262 return unify_success (explain_p);
19265 /* Immediately reject some pairs that won't unify because of
19266 cv-qualification mismatches. */
19267 if (TREE_CODE (arg) == TREE_CODE (parm)
19268 && TYPE_P (arg)
19269 /* It is the elements of the array which hold the cv quals of an array
19270 type, and the elements might be template type parms. We'll check
19271 when we recurse. */
19272 && TREE_CODE (arg) != ARRAY_TYPE
19273 /* We check the cv-qualifiers when unifying with template type
19274 parameters below. We want to allow ARG `const T' to unify with
19275 PARM `T' for example, when computing which of two templates
19276 is more specialized, for example. */
19277 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
19278 && !check_cv_quals_for_unify (strict_in, arg, parm))
19279 return unify_cv_qual_mismatch (explain_p, parm, arg);
19281 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
19282 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
19283 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
19284 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
19285 strict &= ~UNIFY_ALLOW_DERIVED;
19286 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19287 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
19289 switch (TREE_CODE (parm))
19291 case TYPENAME_TYPE:
19292 case SCOPE_REF:
19293 case UNBOUND_CLASS_TEMPLATE:
19294 /* In a type which contains a nested-name-specifier, template
19295 argument values cannot be deduced for template parameters used
19296 within the nested-name-specifier. */
19297 return unify_success (explain_p);
19299 case TEMPLATE_TYPE_PARM:
19300 case TEMPLATE_TEMPLATE_PARM:
19301 case BOUND_TEMPLATE_TEMPLATE_PARM:
19302 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19303 if (error_operand_p (tparm))
19304 return unify_invalid (explain_p);
19306 if (TEMPLATE_TYPE_LEVEL (parm)
19307 != template_decl_level (tparm))
19308 /* The PARM is not one we're trying to unify. Just check
19309 to see if it matches ARG. */
19311 if (TREE_CODE (arg) == TREE_CODE (parm)
19312 && (is_auto (parm) ? is_auto (arg)
19313 : same_type_p (parm, arg)))
19314 return unify_success (explain_p);
19315 else
19316 return unify_type_mismatch (explain_p, parm, arg);
19318 idx = TEMPLATE_TYPE_IDX (parm);
19319 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19320 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
19321 if (error_operand_p (tparm))
19322 return unify_invalid (explain_p);
19324 /* Check for mixed types and values. */
19325 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19326 && TREE_CODE (tparm) != TYPE_DECL)
19327 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19328 && TREE_CODE (tparm) != TEMPLATE_DECL))
19329 gcc_unreachable ();
19331 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19333 /* ARG must be constructed from a template class or a template
19334 template parameter. */
19335 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
19336 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19337 return unify_template_deduction_failure (explain_p, parm, arg);
19339 tree parmvec = TYPE_TI_ARGS (parm);
19340 /* An alias template name is never deduced. */
19341 if (TYPE_ALIAS_P (arg))
19342 arg = strip_typedefs (arg);
19343 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
19344 tree full_argvec = add_to_template_args (targs, argvec);
19345 tree parm_parms
19346 = DECL_INNERMOST_TEMPLATE_PARMS
19347 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
19348 int i, len;
19349 int parm_variadic_p = 0;
19351 /* The resolution to DR150 makes clear that default
19352 arguments for an N-argument may not be used to bind T
19353 to a template template parameter with fewer than N
19354 parameters. It is not safe to permit the binding of
19355 default arguments as an extension, as that may change
19356 the meaning of a conforming program. Consider:
19358 struct Dense { static const unsigned int dim = 1; };
19360 template <template <typename> class View,
19361 typename Block>
19362 void operator+(float, View<Block> const&);
19364 template <typename Block,
19365 unsigned int Dim = Block::dim>
19366 struct Lvalue_proxy { operator float() const; };
19368 void
19369 test_1d (void) {
19370 Lvalue_proxy<Dense> p;
19371 float b;
19372 b + p;
19375 Here, if Lvalue_proxy is permitted to bind to View, then
19376 the global operator+ will be used; if they are not, the
19377 Lvalue_proxy will be converted to float. */
19378 if (coerce_template_parms (parm_parms,
19379 full_argvec,
19380 TYPE_TI_TEMPLATE (parm),
19381 (explain_p
19382 ? tf_warning_or_error
19383 : tf_none),
19384 /*require_all_args=*/true,
19385 /*use_default_args=*/false)
19386 == error_mark_node)
19387 return 1;
19389 /* Deduce arguments T, i from TT<T> or TT<i>.
19390 We check each element of PARMVEC and ARGVEC individually
19391 rather than the whole TREE_VEC since they can have
19392 different number of elements. */
19394 parmvec = expand_template_argument_pack (parmvec);
19395 argvec = expand_template_argument_pack (argvec);
19397 len = TREE_VEC_LENGTH (parmvec);
19399 /* Check if the parameters end in a pack, making them
19400 variadic. */
19401 if (len > 0
19402 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
19403 parm_variadic_p = 1;
19405 for (i = 0; i < len - parm_variadic_p; ++i)
19406 /* If the template argument list of P contains a pack
19407 expansion that is not the last template argument, the
19408 entire template argument list is a non-deduced
19409 context. */
19410 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
19411 return unify_success (explain_p);
19413 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
19414 return unify_too_few_arguments (explain_p,
19415 TREE_VEC_LENGTH (argvec), len);
19417 for (i = 0; i < len - parm_variadic_p; ++i)
19419 RECUR_AND_CHECK_FAILURE (tparms, targs,
19420 TREE_VEC_ELT (parmvec, i),
19421 TREE_VEC_ELT (argvec, i),
19422 UNIFY_ALLOW_NONE, explain_p);
19425 if (parm_variadic_p
19426 && unify_pack_expansion (tparms, targs,
19427 parmvec, argvec,
19428 DEDUCE_EXACT,
19429 /*subr=*/true, explain_p))
19430 return 1;
19432 arg = TYPE_TI_TEMPLATE (arg);
19434 /* Fall through to deduce template name. */
19437 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19438 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19440 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19442 /* Simple cases: Value already set, does match or doesn't. */
19443 if (targ != NULL_TREE && template_args_equal (targ, arg))
19444 return unify_success (explain_p);
19445 else if (targ)
19446 return unify_inconsistency (explain_p, parm, targ, arg);
19448 else
19450 /* If PARM is `const T' and ARG is only `int', we don't have
19451 a match unless we are allowing additional qualification.
19452 If ARG is `const int' and PARM is just `T' that's OK;
19453 that binds `const int' to `T'. */
19454 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
19455 arg, parm))
19456 return unify_cv_qual_mismatch (explain_p, parm, arg);
19458 /* Consider the case where ARG is `const volatile int' and
19459 PARM is `const T'. Then, T should be `volatile int'. */
19460 arg = cp_build_qualified_type_real
19461 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
19462 if (arg == error_mark_node)
19463 return unify_invalid (explain_p);
19465 /* Simple cases: Value already set, does match or doesn't. */
19466 if (targ != NULL_TREE && same_type_p (targ, arg))
19467 return unify_success (explain_p);
19468 else if (targ)
19469 return unify_inconsistency (explain_p, parm, targ, arg);
19471 /* Make sure that ARG is not a variable-sized array. (Note
19472 that were talking about variable-sized arrays (like
19473 `int[n]'), rather than arrays of unknown size (like
19474 `int[]').) We'll get very confused by such a type since
19475 the bound of the array is not constant, and therefore
19476 not mangleable. Besides, such types are not allowed in
19477 ISO C++, so we can do as we please here. We do allow
19478 them for 'auto' deduction, since that isn't ABI-exposed. */
19479 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
19480 return unify_vla_arg (explain_p, arg);
19482 /* Strip typedefs as in convert_template_argument. */
19483 arg = canonicalize_type_argument (arg, tf_none);
19486 /* If ARG is a parameter pack or an expansion, we cannot unify
19487 against it unless PARM is also a parameter pack. */
19488 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19489 && !template_parameter_pack_p (parm))
19490 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19492 /* If the argument deduction results is a METHOD_TYPE,
19493 then there is a problem.
19494 METHOD_TYPE doesn't map to any real C++ type the result of
19495 the deduction can not be of that type. */
19496 if (TREE_CODE (arg) == METHOD_TYPE)
19497 return unify_method_type_error (explain_p, arg);
19499 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19500 return unify_success (explain_p);
19502 case TEMPLATE_PARM_INDEX:
19503 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19504 if (error_operand_p (tparm))
19505 return unify_invalid (explain_p);
19507 if (TEMPLATE_PARM_LEVEL (parm)
19508 != template_decl_level (tparm))
19510 /* The PARM is not one we're trying to unify. Just check
19511 to see if it matches ARG. */
19512 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
19513 && cp_tree_equal (parm, arg));
19514 if (result)
19515 unify_expression_unequal (explain_p, parm, arg);
19516 return result;
19519 idx = TEMPLATE_PARM_IDX (parm);
19520 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19522 if (targ)
19524 int x = !cp_tree_equal (targ, arg);
19525 if (x)
19526 unify_inconsistency (explain_p, parm, targ, arg);
19527 return x;
19530 /* [temp.deduct.type] If, in the declaration of a function template
19531 with a non-type template-parameter, the non-type
19532 template-parameter is used in an expression in the function
19533 parameter-list and, if the corresponding template-argument is
19534 deduced, the template-argument type shall match the type of the
19535 template-parameter exactly, except that a template-argument
19536 deduced from an array bound may be of any integral type.
19537 The non-type parameter might use already deduced type parameters. */
19538 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
19539 if (!TREE_TYPE (arg))
19540 /* Template-parameter dependent expression. Just accept it for now.
19541 It will later be processed in convert_template_argument. */
19543 else if (same_type_p (TREE_TYPE (arg), tparm))
19544 /* OK */;
19545 else if ((strict & UNIFY_ALLOW_INTEGER)
19546 && CP_INTEGRAL_TYPE_P (tparm))
19547 /* Convert the ARG to the type of PARM; the deduced non-type
19548 template argument must exactly match the types of the
19549 corresponding parameter. */
19550 arg = fold (build_nop (tparm, arg));
19551 else if (uses_template_parms (tparm))
19552 /* We haven't deduced the type of this parameter yet. Try again
19553 later. */
19554 return unify_success (explain_p);
19555 else
19556 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
19558 /* If ARG is a parameter pack or an expansion, we cannot unify
19559 against it unless PARM is also a parameter pack. */
19560 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19561 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
19562 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19565 bool removed_attr = false;
19566 arg = strip_typedefs_expr (arg, &removed_attr);
19568 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19569 return unify_success (explain_p);
19571 case PTRMEM_CST:
19573 /* A pointer-to-member constant can be unified only with
19574 another constant. */
19575 if (TREE_CODE (arg) != PTRMEM_CST)
19576 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
19578 /* Just unify the class member. It would be useless (and possibly
19579 wrong, depending on the strict flags) to unify also
19580 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19581 arg refer to the same variable, even if through different
19582 classes. For instance:
19584 struct A { int x; };
19585 struct B : A { };
19587 Unification of &A::x and &B::x must succeed. */
19588 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
19589 PTRMEM_CST_MEMBER (arg), strict, explain_p);
19592 case POINTER_TYPE:
19594 if (!TYPE_PTR_P (arg))
19595 return unify_type_mismatch (explain_p, parm, arg);
19597 /* [temp.deduct.call]
19599 A can be another pointer or pointer to member type that can
19600 be converted to the deduced A via a qualification
19601 conversion (_conv.qual_).
19603 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19604 This will allow for additional cv-qualification of the
19605 pointed-to types if appropriate. */
19607 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
19608 /* The derived-to-base conversion only persists through one
19609 level of pointers. */
19610 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
19612 return unify (tparms, targs, TREE_TYPE (parm),
19613 TREE_TYPE (arg), strict, explain_p);
19616 case REFERENCE_TYPE:
19617 if (TREE_CODE (arg) != REFERENCE_TYPE)
19618 return unify_type_mismatch (explain_p, parm, arg);
19619 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19620 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19622 case ARRAY_TYPE:
19623 if (TREE_CODE (arg) != ARRAY_TYPE)
19624 return unify_type_mismatch (explain_p, parm, arg);
19625 if ((TYPE_DOMAIN (parm) == NULL_TREE)
19626 != (TYPE_DOMAIN (arg) == NULL_TREE))
19627 return unify_type_mismatch (explain_p, parm, arg);
19628 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19629 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19630 if (TYPE_DOMAIN (parm) != NULL_TREE)
19631 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19632 TYPE_DOMAIN (arg), explain_p);
19633 return unify_success (explain_p);
19635 case REAL_TYPE:
19636 case COMPLEX_TYPE:
19637 case VECTOR_TYPE:
19638 case INTEGER_TYPE:
19639 case BOOLEAN_TYPE:
19640 case ENUMERAL_TYPE:
19641 case VOID_TYPE:
19642 case NULLPTR_TYPE:
19643 if (TREE_CODE (arg) != TREE_CODE (parm))
19644 return unify_type_mismatch (explain_p, parm, arg);
19646 /* We have already checked cv-qualification at the top of the
19647 function. */
19648 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
19649 return unify_type_mismatch (explain_p, parm, arg);
19651 /* As far as unification is concerned, this wins. Later checks
19652 will invalidate it if necessary. */
19653 return unify_success (explain_p);
19655 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
19656 /* Type INTEGER_CST can come from ordinary constant template args. */
19657 case INTEGER_CST:
19658 while (TREE_CODE (arg) == NOP_EXPR)
19659 arg = TREE_OPERAND (arg, 0);
19661 if (TREE_CODE (arg) != INTEGER_CST)
19662 return unify_template_argument_mismatch (explain_p, parm, arg);
19663 return (tree_int_cst_equal (parm, arg)
19664 ? unify_success (explain_p)
19665 : unify_template_argument_mismatch (explain_p, parm, arg));
19667 case TREE_VEC:
19669 int i, len, argslen;
19670 int parm_variadic_p = 0;
19672 if (TREE_CODE (arg) != TREE_VEC)
19673 return unify_template_argument_mismatch (explain_p, parm, arg);
19675 len = TREE_VEC_LENGTH (parm);
19676 argslen = TREE_VEC_LENGTH (arg);
19678 /* Check for pack expansions in the parameters. */
19679 for (i = 0; i < len; ++i)
19681 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
19683 if (i == len - 1)
19684 /* We can unify against something with a trailing
19685 parameter pack. */
19686 parm_variadic_p = 1;
19687 else
19688 /* [temp.deduct.type]/9: If the template argument list of
19689 P contains a pack expansion that is not the last
19690 template argument, the entire template argument list
19691 is a non-deduced context. */
19692 return unify_success (explain_p);
19696 /* If we don't have enough arguments to satisfy the parameters
19697 (not counting the pack expression at the end), or we have
19698 too many arguments for a parameter list that doesn't end in
19699 a pack expression, we can't unify. */
19700 if (parm_variadic_p
19701 ? argslen < len - parm_variadic_p
19702 : argslen != len)
19703 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
19705 /* Unify all of the parameters that precede the (optional)
19706 pack expression. */
19707 for (i = 0; i < len - parm_variadic_p; ++i)
19709 RECUR_AND_CHECK_FAILURE (tparms, targs,
19710 TREE_VEC_ELT (parm, i),
19711 TREE_VEC_ELT (arg, i),
19712 UNIFY_ALLOW_NONE, explain_p);
19714 if (parm_variadic_p)
19715 return unify_pack_expansion (tparms, targs, parm, arg,
19716 DEDUCE_EXACT,
19717 /*subr=*/true, explain_p);
19718 return unify_success (explain_p);
19721 case RECORD_TYPE:
19722 case UNION_TYPE:
19723 if (TREE_CODE (arg) != TREE_CODE (parm))
19724 return unify_type_mismatch (explain_p, parm, arg);
19726 if (TYPE_PTRMEMFUNC_P (parm))
19728 if (!TYPE_PTRMEMFUNC_P (arg))
19729 return unify_type_mismatch (explain_p, parm, arg);
19731 return unify (tparms, targs,
19732 TYPE_PTRMEMFUNC_FN_TYPE (parm),
19733 TYPE_PTRMEMFUNC_FN_TYPE (arg),
19734 strict, explain_p);
19736 else if (TYPE_PTRMEMFUNC_P (arg))
19737 return unify_type_mismatch (explain_p, parm, arg);
19739 if (CLASSTYPE_TEMPLATE_INFO (parm))
19741 tree t = NULL_TREE;
19743 if (strict_in & UNIFY_ALLOW_DERIVED)
19745 /* First, we try to unify the PARM and ARG directly. */
19746 t = try_class_unification (tparms, targs,
19747 parm, arg, explain_p);
19749 if (!t)
19751 /* Fallback to the special case allowed in
19752 [temp.deduct.call]:
19754 If P is a class, and P has the form
19755 template-id, then A can be a derived class of
19756 the deduced A. Likewise, if P is a pointer to
19757 a class of the form template-id, A can be a
19758 pointer to a derived class pointed to by the
19759 deduced A. */
19760 enum template_base_result r;
19761 r = get_template_base (tparms, targs, parm, arg,
19762 explain_p, &t);
19764 if (!t)
19766 /* Don't give the derived diagnostic if we're
19767 already dealing with the same template. */
19768 bool same_template
19769 = (CLASSTYPE_TEMPLATE_INFO (arg)
19770 && (CLASSTYPE_TI_TEMPLATE (parm)
19771 == CLASSTYPE_TI_TEMPLATE (arg)));
19772 return unify_no_common_base (explain_p && !same_template,
19773 r, parm, arg);
19777 else if (CLASSTYPE_TEMPLATE_INFO (arg)
19778 && (CLASSTYPE_TI_TEMPLATE (parm)
19779 == CLASSTYPE_TI_TEMPLATE (arg)))
19780 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19781 Then, we should unify `int' and `U'. */
19782 t = arg;
19783 else
19784 /* There's no chance of unification succeeding. */
19785 return unify_type_mismatch (explain_p, parm, arg);
19787 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
19788 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
19790 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
19791 return unify_type_mismatch (explain_p, parm, arg);
19792 return unify_success (explain_p);
19794 case METHOD_TYPE:
19795 case FUNCTION_TYPE:
19797 unsigned int nargs;
19798 tree *args;
19799 tree a;
19800 unsigned int i;
19802 if (TREE_CODE (arg) != TREE_CODE (parm))
19803 return unify_type_mismatch (explain_p, parm, arg);
19805 /* CV qualifications for methods can never be deduced, they must
19806 match exactly. We need to check them explicitly here,
19807 because type_unification_real treats them as any other
19808 cv-qualified parameter. */
19809 if (TREE_CODE (parm) == METHOD_TYPE
19810 && (!check_cv_quals_for_unify
19811 (UNIFY_ALLOW_NONE,
19812 class_of_this_parm (arg),
19813 class_of_this_parm (parm))))
19814 return unify_cv_qual_mismatch (explain_p, parm, arg);
19816 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
19817 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
19819 nargs = list_length (TYPE_ARG_TYPES (arg));
19820 args = XALLOCAVEC (tree, nargs);
19821 for (a = TYPE_ARG_TYPES (arg), i = 0;
19822 a != NULL_TREE && a != void_list_node;
19823 a = TREE_CHAIN (a), ++i)
19824 args[i] = TREE_VALUE (a);
19825 nargs = i;
19827 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
19828 args, nargs, 1, DEDUCE_EXACT,
19829 LOOKUP_NORMAL, NULL, explain_p);
19832 case OFFSET_TYPE:
19833 /* Unify a pointer to member with a pointer to member function, which
19834 deduces the type of the member as a function type. */
19835 if (TYPE_PTRMEMFUNC_P (arg))
19837 /* Check top-level cv qualifiers */
19838 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
19839 return unify_cv_qual_mismatch (explain_p, parm, arg);
19841 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19842 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
19843 UNIFY_ALLOW_NONE, explain_p);
19845 /* Determine the type of the function we are unifying against. */
19846 tree fntype = static_fn_type (arg);
19848 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
19851 if (TREE_CODE (arg) != OFFSET_TYPE)
19852 return unify_type_mismatch (explain_p, parm, arg);
19853 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19854 TYPE_OFFSET_BASETYPE (arg),
19855 UNIFY_ALLOW_NONE, explain_p);
19856 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19857 strict, explain_p);
19859 case CONST_DECL:
19860 if (DECL_TEMPLATE_PARM_P (parm))
19861 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
19862 if (arg != scalar_constant_value (parm))
19863 return unify_template_argument_mismatch (explain_p, parm, arg);
19864 return unify_success (explain_p);
19866 case FIELD_DECL:
19867 case TEMPLATE_DECL:
19868 /* Matched cases are handled by the ARG == PARM test above. */
19869 return unify_template_argument_mismatch (explain_p, parm, arg);
19871 case VAR_DECL:
19872 /* A non-type template parameter that is a variable should be a
19873 an integral constant, in which case, it whould have been
19874 folded into its (constant) value. So we should not be getting
19875 a variable here. */
19876 gcc_unreachable ();
19878 case TYPE_ARGUMENT_PACK:
19879 case NONTYPE_ARGUMENT_PACK:
19880 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
19881 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
19883 case TYPEOF_TYPE:
19884 case DECLTYPE_TYPE:
19885 case UNDERLYING_TYPE:
19886 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19887 or UNDERLYING_TYPE nodes. */
19888 return unify_success (explain_p);
19890 case ERROR_MARK:
19891 /* Unification fails if we hit an error node. */
19892 return unify_invalid (explain_p);
19894 case INDIRECT_REF:
19895 if (REFERENCE_REF_P (parm))
19897 if (REFERENCE_REF_P (arg))
19898 arg = TREE_OPERAND (arg, 0);
19899 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
19900 strict, explain_p);
19902 /* FALLTHRU */
19904 default:
19905 /* An unresolved overload is a nondeduced context. */
19906 if (is_overloaded_fn (parm) || type_unknown_p (parm))
19907 return unify_success (explain_p);
19908 gcc_assert (EXPR_P (parm));
19910 /* We must be looking at an expression. This can happen with
19911 something like:
19913 template <int I>
19914 void foo(S<I>, S<I + 2>);
19916 This is a "nondeduced context":
19918 [deduct.type]
19920 The nondeduced contexts are:
19922 --A type that is a template-id in which one or more of
19923 the template-arguments is an expression that references
19924 a template-parameter.
19926 In these cases, we assume deduction succeeded, but don't
19927 actually infer any unifications. */
19929 if (!uses_template_parms (parm)
19930 && !template_args_equal (parm, arg))
19931 return unify_expression_unequal (explain_p, parm, arg);
19932 else
19933 return unify_success (explain_p);
19936 #undef RECUR_AND_CHECK_FAILURE
19938 /* Note that DECL can be defined in this translation unit, if
19939 required. */
19941 static void
19942 mark_definable (tree decl)
19944 tree clone;
19945 DECL_NOT_REALLY_EXTERN (decl) = 1;
19946 FOR_EACH_CLONE (clone, decl)
19947 DECL_NOT_REALLY_EXTERN (clone) = 1;
19950 /* Called if RESULT is explicitly instantiated, or is a member of an
19951 explicitly instantiated class. */
19953 void
19954 mark_decl_instantiated (tree result, int extern_p)
19956 SET_DECL_EXPLICIT_INSTANTIATION (result);
19958 /* If this entity has already been written out, it's too late to
19959 make any modifications. */
19960 if (TREE_ASM_WRITTEN (result))
19961 return;
19963 /* For anonymous namespace we don't need to do anything. */
19964 if (decl_anon_ns_mem_p (result))
19966 gcc_assert (!TREE_PUBLIC (result));
19967 return;
19970 if (TREE_CODE (result) != FUNCTION_DECL)
19971 /* The TREE_PUBLIC flag for function declarations will have been
19972 set correctly by tsubst. */
19973 TREE_PUBLIC (result) = 1;
19975 /* This might have been set by an earlier implicit instantiation. */
19976 DECL_COMDAT (result) = 0;
19978 if (extern_p)
19979 DECL_NOT_REALLY_EXTERN (result) = 0;
19980 else
19982 mark_definable (result);
19983 mark_needed (result);
19984 /* Always make artificials weak. */
19985 if (DECL_ARTIFICIAL (result) && flag_weak)
19986 comdat_linkage (result);
19987 /* For WIN32 we also want to put explicit instantiations in
19988 linkonce sections. */
19989 else if (TREE_PUBLIC (result))
19990 maybe_make_one_only (result);
19993 /* If EXTERN_P, then this function will not be emitted -- unless
19994 followed by an explicit instantiation, at which point its linkage
19995 will be adjusted. If !EXTERN_P, then this function will be
19996 emitted here. In neither circumstance do we want
19997 import_export_decl to adjust the linkage. */
19998 DECL_INTERFACE_KNOWN (result) = 1;
20001 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20002 important template arguments. If any are missing, we check whether
20003 they're important by using error_mark_node for substituting into any
20004 args that were used for partial ordering (the ones between ARGS and END)
20005 and seeing if it bubbles up. */
20007 static bool
20008 check_undeduced_parms (tree targs, tree args, tree end)
20010 bool found = false;
20011 int i;
20012 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
20013 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
20015 found = true;
20016 TREE_VEC_ELT (targs, i) = error_mark_node;
20018 if (found)
20020 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
20021 if (substed == error_mark_node)
20022 return true;
20024 return false;
20027 /* Given two function templates PAT1 and PAT2, return:
20029 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20030 -1 if PAT2 is more specialized than PAT1.
20031 0 if neither is more specialized.
20033 LEN indicates the number of parameters we should consider
20034 (defaulted parameters should not be considered).
20036 The 1998 std underspecified function template partial ordering, and
20037 DR214 addresses the issue. We take pairs of arguments, one from
20038 each of the templates, and deduce them against each other. One of
20039 the templates will be more specialized if all the *other*
20040 template's arguments deduce against its arguments and at least one
20041 of its arguments *does* *not* deduce against the other template's
20042 corresponding argument. Deduction is done as for class templates.
20043 The arguments used in deduction have reference and top level cv
20044 qualifiers removed. Iff both arguments were originally reference
20045 types *and* deduction succeeds in both directions, an lvalue reference
20046 wins against an rvalue reference and otherwise the template
20047 with the more cv-qualified argument wins for that pairing (if
20048 neither is more cv-qualified, they both are equal). Unlike regular
20049 deduction, after all the arguments have been deduced in this way,
20050 we do *not* verify the deduced template argument values can be
20051 substituted into non-deduced contexts.
20053 The logic can be a bit confusing here, because we look at deduce1 and
20054 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20055 can find template arguments for pat1 to make arg1 look like arg2, that
20056 means that arg2 is at least as specialized as arg1. */
20059 more_specialized_fn (tree pat1, tree pat2, int len)
20061 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
20062 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
20063 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
20064 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
20065 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
20066 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
20067 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
20068 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
20069 tree origs1, origs2;
20070 bool lose1 = false;
20071 bool lose2 = false;
20073 /* Remove the this parameter from non-static member functions. If
20074 one is a non-static member function and the other is not a static
20075 member function, remove the first parameter from that function
20076 also. This situation occurs for operator functions where we
20077 locate both a member function (with this pointer) and non-member
20078 operator (with explicit first operand). */
20079 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
20081 len--; /* LEN is the number of significant arguments for DECL1 */
20082 args1 = TREE_CHAIN (args1);
20083 if (!DECL_STATIC_FUNCTION_P (decl2))
20084 args2 = TREE_CHAIN (args2);
20086 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
20088 args2 = TREE_CHAIN (args2);
20089 if (!DECL_STATIC_FUNCTION_P (decl1))
20091 len--;
20092 args1 = TREE_CHAIN (args1);
20096 /* If only one is a conversion operator, they are unordered. */
20097 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
20098 return 0;
20100 /* Consider the return type for a conversion function */
20101 if (DECL_CONV_FN_P (decl1))
20103 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
20104 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
20105 len++;
20108 processing_template_decl++;
20110 origs1 = args1;
20111 origs2 = args2;
20113 while (len--
20114 /* Stop when an ellipsis is seen. */
20115 && args1 != NULL_TREE && args2 != NULL_TREE)
20117 tree arg1 = TREE_VALUE (args1);
20118 tree arg2 = TREE_VALUE (args2);
20119 int deduce1, deduce2;
20120 int quals1 = -1;
20121 int quals2 = -1;
20122 int ref1 = 0;
20123 int ref2 = 0;
20125 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20126 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20128 /* When both arguments are pack expansions, we need only
20129 unify the patterns themselves. */
20130 arg1 = PACK_EXPANSION_PATTERN (arg1);
20131 arg2 = PACK_EXPANSION_PATTERN (arg2);
20133 /* This is the last comparison we need to do. */
20134 len = 0;
20137 if (TREE_CODE (arg1) == REFERENCE_TYPE)
20139 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
20140 arg1 = TREE_TYPE (arg1);
20141 quals1 = cp_type_quals (arg1);
20144 if (TREE_CODE (arg2) == REFERENCE_TYPE)
20146 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
20147 arg2 = TREE_TYPE (arg2);
20148 quals2 = cp_type_quals (arg2);
20151 arg1 = TYPE_MAIN_VARIANT (arg1);
20152 arg2 = TYPE_MAIN_VARIANT (arg2);
20154 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
20156 int i, len2 = list_length (args2);
20157 tree parmvec = make_tree_vec (1);
20158 tree argvec = make_tree_vec (len2);
20159 tree ta = args2;
20161 /* Setup the parameter vector, which contains only ARG1. */
20162 TREE_VEC_ELT (parmvec, 0) = arg1;
20164 /* Setup the argument vector, which contains the remaining
20165 arguments. */
20166 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
20167 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20169 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
20170 argvec, DEDUCE_EXACT,
20171 /*subr=*/true, /*explain_p=*/false)
20172 == 0);
20174 /* We cannot deduce in the other direction, because ARG1 is
20175 a pack expansion but ARG2 is not. */
20176 deduce2 = 0;
20178 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20180 int i, len1 = list_length (args1);
20181 tree parmvec = make_tree_vec (1);
20182 tree argvec = make_tree_vec (len1);
20183 tree ta = args1;
20185 /* Setup the parameter vector, which contains only ARG1. */
20186 TREE_VEC_ELT (parmvec, 0) = arg2;
20188 /* Setup the argument vector, which contains the remaining
20189 arguments. */
20190 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
20191 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20193 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
20194 argvec, DEDUCE_EXACT,
20195 /*subr=*/true, /*explain_p=*/false)
20196 == 0);
20198 /* We cannot deduce in the other direction, because ARG2 is
20199 a pack expansion but ARG1 is not.*/
20200 deduce1 = 0;
20203 else
20205 /* The normal case, where neither argument is a pack
20206 expansion. */
20207 deduce1 = (unify (tparms1, targs1, arg1, arg2,
20208 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20209 == 0);
20210 deduce2 = (unify (tparms2, targs2, arg2, arg1,
20211 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20212 == 0);
20215 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20216 arg2, then arg2 is not as specialized as arg1. */
20217 if (!deduce1)
20218 lose2 = true;
20219 if (!deduce2)
20220 lose1 = true;
20222 /* "If, for a given type, deduction succeeds in both directions
20223 (i.e., the types are identical after the transformations above)
20224 and both P and A were reference types (before being replaced with
20225 the type referred to above):
20226 - if the type from the argument template was an lvalue reference and
20227 the type from the parameter template was not, the argument type is
20228 considered to be more specialized than the other; otherwise,
20229 - if the type from the argument template is more cv-qualified
20230 than the type from the parameter template (as described above),
20231 the argument type is considered to be more specialized than the other;
20232 otherwise,
20233 - neither type is more specialized than the other." */
20235 if (deduce1 && deduce2)
20237 if (ref1 && ref2 && ref1 != ref2)
20239 if (ref1 > ref2)
20240 lose1 = true;
20241 else
20242 lose2 = true;
20244 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
20246 if ((quals1 & quals2) == quals2)
20247 lose2 = true;
20248 if ((quals1 & quals2) == quals1)
20249 lose1 = true;
20253 if (lose1 && lose2)
20254 /* We've failed to deduce something in either direction.
20255 These must be unordered. */
20256 break;
20258 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20259 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20260 /* We have already processed all of the arguments in our
20261 handing of the pack expansion type. */
20262 len = 0;
20264 args1 = TREE_CHAIN (args1);
20265 args2 = TREE_CHAIN (args2);
20268 /* "In most cases, all template parameters must have values in order for
20269 deduction to succeed, but for partial ordering purposes a template
20270 parameter may remain without a value provided it is not used in the
20271 types being used for partial ordering."
20273 Thus, if we are missing any of the targs1 we need to substitute into
20274 origs1, then pat2 is not as specialized as pat1. This can happen when
20275 there is a nondeduced context. */
20276 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
20277 lose2 = true;
20278 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
20279 lose1 = true;
20281 processing_template_decl--;
20283 /* If both deductions succeed, the partial ordering selects the more
20284 constrained template. */
20285 if (!lose1 && !lose2)
20287 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
20288 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
20289 lose1 = !subsumes_constraints (c1, c2);
20290 lose2 = !subsumes_constraints (c2, c1);
20293 /* All things being equal, if the next argument is a pack expansion
20294 for one function but not for the other, prefer the
20295 non-variadic function. FIXME this is bogus; see c++/41958. */
20296 if (lose1 == lose2
20297 && args1 && TREE_VALUE (args1)
20298 && args2 && TREE_VALUE (args2))
20300 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
20301 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
20304 if (lose1 == lose2)
20305 return 0;
20306 else if (!lose1)
20307 return 1;
20308 else
20309 return -1;
20312 /* Determine which of two partial specializations of TMPL is more
20313 specialized.
20315 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20316 to the first partial specialization. The TREE_PURPOSE is the
20317 innermost set of template parameters for the partial
20318 specialization. PAT2 is similar, but for the second template.
20320 Return 1 if the first partial specialization is more specialized;
20321 -1 if the second is more specialized; 0 if neither is more
20322 specialized.
20324 See [temp.class.order] for information about determining which of
20325 two templates is more specialized. */
20327 static int
20328 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
20330 tree targs;
20331 int winner = 0;
20332 bool any_deductions = false;
20334 tree tmpl1 = TREE_VALUE (pat1);
20335 tree tmpl2 = TREE_VALUE (pat2);
20336 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
20337 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
20338 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
20339 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
20341 /* Just like what happens for functions, if we are ordering between
20342 different template specializations, we may encounter dependent
20343 types in the arguments, and we need our dependency check functions
20344 to behave correctly. */
20345 ++processing_template_decl;
20346 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
20347 if (targs)
20349 --winner;
20350 any_deductions = true;
20353 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
20354 if (targs)
20356 ++winner;
20357 any_deductions = true;
20359 --processing_template_decl;
20361 /* If both deductions succeed, the partial ordering selects the more
20362 constrained template. */
20363 if (!winner && any_deductions)
20364 return more_constrained (tmpl1, tmpl2);
20366 /* In the case of a tie where at least one of the templates
20367 has a parameter pack at the end, the template with the most
20368 non-packed parameters wins. */
20369 if (winner == 0
20370 && any_deductions
20371 && (template_args_variadic_p (TREE_PURPOSE (pat1))
20372 || template_args_variadic_p (TREE_PURPOSE (pat2))))
20374 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
20375 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
20376 int len1 = TREE_VEC_LENGTH (args1);
20377 int len2 = TREE_VEC_LENGTH (args2);
20379 /* We don't count the pack expansion at the end. */
20380 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
20381 --len1;
20382 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
20383 --len2;
20385 if (len1 > len2)
20386 return 1;
20387 else if (len1 < len2)
20388 return -1;
20391 return winner;
20394 /* Return the template arguments that will produce the function signature
20395 DECL from the function template FN, with the explicit template
20396 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20397 also match. Return NULL_TREE if no satisfactory arguments could be
20398 found. */
20400 static tree
20401 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
20403 int ntparms = DECL_NTPARMS (fn);
20404 tree targs = make_tree_vec (ntparms);
20405 tree decl_type = TREE_TYPE (decl);
20406 tree decl_arg_types;
20407 tree *args;
20408 unsigned int nargs, ix;
20409 tree arg;
20411 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
20413 /* Never do unification on the 'this' parameter. */
20414 decl_arg_types = skip_artificial_parms_for (decl,
20415 TYPE_ARG_TYPES (decl_type));
20417 nargs = list_length (decl_arg_types);
20418 args = XALLOCAVEC (tree, nargs);
20419 for (arg = decl_arg_types, ix = 0;
20420 arg != NULL_TREE && arg != void_list_node;
20421 arg = TREE_CHAIN (arg), ++ix)
20422 args[ix] = TREE_VALUE (arg);
20424 if (fn_type_unification (fn, explicit_args, targs,
20425 args, ix,
20426 (check_rettype || DECL_CONV_FN_P (fn)
20427 ? TREE_TYPE (decl_type) : NULL_TREE),
20428 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
20429 /*decltype*/false)
20430 == error_mark_node)
20431 return NULL_TREE;
20433 return targs;
20436 /* Return the innermost template arguments that, when applied to a partial
20437 specialization of TMPL whose innermost template parameters are
20438 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
20439 ARGS.
20441 For example, suppose we have:
20443 template <class T, class U> struct S {};
20444 template <class T> struct S<T*, int> {};
20446 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
20447 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
20448 int}. The resulting vector will be {double}, indicating that `T'
20449 is bound to `double'. */
20451 static tree
20452 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
20454 int i, ntparms = TREE_VEC_LENGTH (tparms);
20455 tree deduced_args;
20456 tree innermost_deduced_args;
20458 innermost_deduced_args = make_tree_vec (ntparms);
20459 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20461 deduced_args = copy_node (args);
20462 SET_TMPL_ARGS_LEVEL (deduced_args,
20463 TMPL_ARGS_DEPTH (deduced_args),
20464 innermost_deduced_args);
20466 else
20467 deduced_args = innermost_deduced_args;
20469 if (unify (tparms, deduced_args,
20470 INNERMOST_TEMPLATE_ARGS (spec_args),
20471 INNERMOST_TEMPLATE_ARGS (args),
20472 UNIFY_ALLOW_NONE, /*explain_p=*/false))
20473 return NULL_TREE;
20475 for (i = 0; i < ntparms; ++i)
20476 if (! TREE_VEC_ELT (innermost_deduced_args, i))
20477 return NULL_TREE;
20479 /* Verify that nondeduced template arguments agree with the type
20480 obtained from argument deduction.
20482 For example:
20484 struct A { typedef int X; };
20485 template <class T, class U> struct C {};
20486 template <class T> struct C<T, typename T::X> {};
20488 Then with the instantiation `C<A, int>', we can deduce that
20489 `T' is `A' but unify () does not check whether `typename T::X'
20490 is `int'. */
20491 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
20492 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20493 spec_args, tmpl,
20494 tf_none, false, false);
20495 if (spec_args == error_mark_node
20496 /* We only need to check the innermost arguments; the other
20497 arguments will always agree. */
20498 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
20499 INNERMOST_TEMPLATE_ARGS (args)))
20500 return NULL_TREE;
20502 /* Now that we have bindings for all of the template arguments,
20503 ensure that the arguments deduced for the template template
20504 parameters have compatible template parameter lists. See the use
20505 of template_template_parm_bindings_ok_p in fn_type_unification
20506 for more information. */
20507 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
20508 return NULL_TREE;
20510 return deduced_args;
20513 // Compare two function templates T1 and T2 by deducing bindings
20514 // from one against the other. If both deductions succeed, compare
20515 // constraints to see which is more constrained.
20516 static int
20517 more_specialized_inst (tree t1, tree t2)
20519 int fate = 0;
20520 int count = 0;
20522 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
20524 --fate;
20525 ++count;
20528 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
20530 ++fate;
20531 ++count;
20534 // If both deductions succeed, then one may be more constrained.
20535 if (count == 2 && fate == 0)
20536 fate = more_constrained (t1, t2);
20538 return fate;
20541 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20542 Return the TREE_LIST node with the most specialized template, if
20543 any. If there is no most specialized template, the error_mark_node
20544 is returned.
20546 Note that this function does not look at, or modify, the
20547 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20548 returned is one of the elements of INSTANTIATIONS, callers may
20549 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20550 and retrieve it from the value returned. */
20552 tree
20553 most_specialized_instantiation (tree templates)
20555 tree fn, champ;
20557 ++processing_template_decl;
20559 champ = templates;
20560 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
20562 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
20563 if (fate == -1)
20564 champ = fn;
20565 else if (!fate)
20567 /* Equally specialized, move to next function. If there
20568 is no next function, nothing's most specialized. */
20569 fn = TREE_CHAIN (fn);
20570 champ = fn;
20571 if (!fn)
20572 break;
20576 if (champ)
20577 /* Now verify that champ is better than everything earlier in the
20578 instantiation list. */
20579 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
20580 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
20582 champ = NULL_TREE;
20583 break;
20587 processing_template_decl--;
20589 if (!champ)
20590 return error_mark_node;
20592 return champ;
20595 /* If DECL is a specialization of some template, return the most
20596 general such template. Otherwise, returns NULL_TREE.
20598 For example, given:
20600 template <class T> struct S { template <class U> void f(U); };
20602 if TMPL is `template <class U> void S<int>::f(U)' this will return
20603 the full template. This function will not trace past partial
20604 specializations, however. For example, given in addition:
20606 template <class T> struct S<T*> { template <class U> void f(U); };
20608 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20609 `template <class T> template <class U> S<T*>::f(U)'. */
20611 tree
20612 most_general_template (tree decl)
20614 if (TREE_CODE (decl) != TEMPLATE_DECL)
20616 if (tree tinfo = get_template_info (decl))
20617 decl = TI_TEMPLATE (tinfo);
20618 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20619 template friend, or a FIELD_DECL for a capture pack. */
20620 if (TREE_CODE (decl) != TEMPLATE_DECL)
20621 return NULL_TREE;
20624 /* Look for more and more general templates. */
20625 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
20627 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20628 (See cp-tree.h for details.) */
20629 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
20630 break;
20632 if (CLASS_TYPE_P (TREE_TYPE (decl))
20633 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
20634 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
20635 break;
20637 /* Stop if we run into an explicitly specialized class template. */
20638 if (!DECL_NAMESPACE_SCOPE_P (decl)
20639 && DECL_CONTEXT (decl)
20640 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
20641 break;
20643 decl = DECL_TI_TEMPLATE (decl);
20646 return decl;
20649 /* Return the most specialized of the template partial specializations
20650 which can produce TARGET, a specialization of some class or variable
20651 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
20652 a TEMPLATE_DECL node corresponding to the partial specialization, while
20653 the TREE_PURPOSE is the set of template arguments that must be
20654 substituted into the template pattern in order to generate TARGET.
20656 If the choice of partial specialization is ambiguous, a diagnostic
20657 is issued, and the error_mark_node is returned. If there are no
20658 partial specializations matching TARGET, then NULL_TREE is
20659 returned, indicating that the primary template should be used. */
20661 static tree
20662 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
20664 tree list = NULL_TREE;
20665 tree t;
20666 tree champ;
20667 int fate;
20668 bool ambiguous_p;
20669 tree outer_args = NULL_TREE;
20670 tree tmpl, args;
20672 if (TYPE_P (target))
20674 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
20675 tmpl = TI_TEMPLATE (tinfo);
20676 args = TI_ARGS (tinfo);
20678 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
20680 tmpl = TREE_OPERAND (target, 0);
20681 args = TREE_OPERAND (target, 1);
20683 else if (VAR_P (target))
20685 tree tinfo = DECL_TEMPLATE_INFO (target);
20686 tmpl = TI_TEMPLATE (tinfo);
20687 args = TI_ARGS (tinfo);
20689 else
20690 gcc_unreachable ();
20692 tree main_tmpl = most_general_template (tmpl);
20694 /* For determining which partial specialization to use, only the
20695 innermost args are interesting. */
20696 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20698 outer_args = strip_innermost_template_args (args, 1);
20699 args = INNERMOST_TEMPLATE_ARGS (args);
20702 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
20704 tree partial_spec_args;
20705 tree spec_args;
20706 tree spec_tmpl = TREE_VALUE (t);
20708 partial_spec_args = TREE_PURPOSE (t);
20710 ++processing_template_decl;
20712 if (outer_args)
20714 /* Discard the outer levels of args, and then substitute in the
20715 template args from the enclosing class. */
20716 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
20717 partial_spec_args = tsubst_template_args
20718 (partial_spec_args, outer_args, tf_none, NULL_TREE);
20720 /* And the same for the partial specialization TEMPLATE_DECL. */
20721 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
20724 partial_spec_args =
20725 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20726 partial_spec_args,
20727 tmpl, tf_none,
20728 /*require_all_args=*/true,
20729 /*use_default_args=*/true);
20731 --processing_template_decl;
20733 if (partial_spec_args == error_mark_node)
20734 return error_mark_node;
20735 if (spec_tmpl == error_mark_node)
20736 return error_mark_node;
20738 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
20739 spec_args = get_partial_spec_bindings (tmpl, parms,
20740 partial_spec_args,
20741 args);
20742 if (spec_args)
20744 if (outer_args)
20745 spec_args = add_to_template_args (outer_args, spec_args);
20747 /* Keep the candidate only if the constraints are satisfied,
20748 or if we're not compiling with concepts. */
20749 if (!flag_concepts
20750 || constraints_satisfied_p (spec_tmpl, spec_args))
20752 list = tree_cons (spec_args, TREE_VALUE (t), list);
20753 TREE_TYPE (list) = TREE_TYPE (t);
20758 if (! list)
20759 return NULL_TREE;
20761 ambiguous_p = false;
20762 t = list;
20763 champ = t;
20764 t = TREE_CHAIN (t);
20765 for (; t; t = TREE_CHAIN (t))
20767 fate = more_specialized_partial_spec (tmpl, champ, t);
20768 if (fate == 1)
20770 else
20772 if (fate == 0)
20774 t = TREE_CHAIN (t);
20775 if (! t)
20777 ambiguous_p = true;
20778 break;
20781 champ = t;
20785 if (!ambiguous_p)
20786 for (t = list; t && t != champ; t = TREE_CHAIN (t))
20788 fate = more_specialized_partial_spec (tmpl, champ, t);
20789 if (fate != 1)
20791 ambiguous_p = true;
20792 break;
20796 if (ambiguous_p)
20798 const char *str;
20799 char *spaces = NULL;
20800 if (!(complain & tf_error))
20801 return error_mark_node;
20802 if (TYPE_P (target))
20803 error ("ambiguous template instantiation for %q#T", target);
20804 else
20805 error ("ambiguous template instantiation for %q#D", target);
20806 str = ngettext ("candidate is:", "candidates are:", list_length (list));
20807 for (t = list; t; t = TREE_CHAIN (t))
20809 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
20810 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
20811 "%s %#S", spaces ? spaces : str, subst);
20812 spaces = spaces ? spaces : get_spaces (str);
20814 free (spaces);
20815 return error_mark_node;
20818 return champ;
20821 /* Explicitly instantiate DECL. */
20823 void
20824 do_decl_instantiation (tree decl, tree storage)
20826 tree result = NULL_TREE;
20827 int extern_p = 0;
20829 if (!decl || decl == error_mark_node)
20830 /* An error occurred, for which grokdeclarator has already issued
20831 an appropriate message. */
20832 return;
20833 else if (! DECL_LANG_SPECIFIC (decl))
20835 error ("explicit instantiation of non-template %q#D", decl);
20836 return;
20839 bool var_templ = (DECL_TEMPLATE_INFO (decl)
20840 && variable_template_p (DECL_TI_TEMPLATE (decl)));
20842 if (VAR_P (decl) && !var_templ)
20844 /* There is an asymmetry here in the way VAR_DECLs and
20845 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20846 the latter, the DECL we get back will be marked as a
20847 template instantiation, and the appropriate
20848 DECL_TEMPLATE_INFO will be set up. This does not happen for
20849 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20850 should handle VAR_DECLs as it currently handles
20851 FUNCTION_DECLs. */
20852 if (!DECL_CLASS_SCOPE_P (decl))
20854 error ("%qD is not a static data member of a class template", decl);
20855 return;
20857 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
20858 if (!result || !VAR_P (result))
20860 error ("no matching template for %qD found", decl);
20861 return;
20863 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
20865 error ("type %qT for explicit instantiation %qD does not match "
20866 "declared type %qT", TREE_TYPE (result), decl,
20867 TREE_TYPE (decl));
20868 return;
20871 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
20873 error ("explicit instantiation of %q#D", decl);
20874 return;
20876 else
20877 result = decl;
20879 /* Check for various error cases. Note that if the explicit
20880 instantiation is valid the RESULT will currently be marked as an
20881 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20882 until we get here. */
20884 if (DECL_TEMPLATE_SPECIALIZATION (result))
20886 /* DR 259 [temp.spec].
20888 Both an explicit instantiation and a declaration of an explicit
20889 specialization shall not appear in a program unless the explicit
20890 instantiation follows a declaration of the explicit specialization.
20892 For a given set of template parameters, if an explicit
20893 instantiation of a template appears after a declaration of an
20894 explicit specialization for that template, the explicit
20895 instantiation has no effect. */
20896 return;
20898 else if (DECL_EXPLICIT_INSTANTIATION (result))
20900 /* [temp.spec]
20902 No program shall explicitly instantiate any template more
20903 than once.
20905 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20906 the first instantiation was `extern' and the second is not,
20907 and EXTERN_P for the opposite case. */
20908 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
20909 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
20910 /* If an "extern" explicit instantiation follows an ordinary
20911 explicit instantiation, the template is instantiated. */
20912 if (extern_p)
20913 return;
20915 else if (!DECL_IMPLICIT_INSTANTIATION (result))
20917 error ("no matching template for %qD found", result);
20918 return;
20920 else if (!DECL_TEMPLATE_INFO (result))
20922 permerror (input_location, "explicit instantiation of non-template %q#D", result);
20923 return;
20926 if (storage == NULL_TREE)
20928 else if (storage == ridpointers[(int) RID_EXTERN])
20930 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
20931 pedwarn (input_location, OPT_Wpedantic,
20932 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20933 "instantiations");
20934 extern_p = 1;
20936 else
20937 error ("storage class %qD applied to template instantiation", storage);
20939 check_explicit_instantiation_namespace (result);
20940 mark_decl_instantiated (result, extern_p);
20941 if (! extern_p)
20942 instantiate_decl (result, /*defer_ok=*/1,
20943 /*expl_inst_class_mem_p=*/false);
20946 static void
20947 mark_class_instantiated (tree t, int extern_p)
20949 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
20950 SET_CLASSTYPE_INTERFACE_KNOWN (t);
20951 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
20952 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
20953 if (! extern_p)
20955 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
20956 rest_of_type_compilation (t, 1);
20960 /* Called from do_type_instantiation through binding_table_foreach to
20961 do recursive instantiation for the type bound in ENTRY. */
20962 static void
20963 bt_instantiate_type_proc (binding_entry entry, void *data)
20965 tree storage = *(tree *) data;
20967 if (MAYBE_CLASS_TYPE_P (entry->type)
20968 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
20969 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
20972 /* Called from do_type_instantiation to instantiate a member
20973 (a member function or a static member variable) of an
20974 explicitly instantiated class template. */
20975 static void
20976 instantiate_class_member (tree decl, int extern_p)
20978 mark_decl_instantiated (decl, extern_p);
20979 if (! extern_p)
20980 instantiate_decl (decl, /*defer_ok=*/1,
20981 /*expl_inst_class_mem_p=*/true);
20984 /* Perform an explicit instantiation of template class T. STORAGE, if
20985 non-null, is the RID for extern, inline or static. COMPLAIN is
20986 nonzero if this is called from the parser, zero if called recursively,
20987 since the standard is unclear (as detailed below). */
20989 void
20990 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
20992 int extern_p = 0;
20993 int nomem_p = 0;
20994 int static_p = 0;
20995 int previous_instantiation_extern_p = 0;
20997 if (TREE_CODE (t) == TYPE_DECL)
20998 t = TREE_TYPE (t);
21000 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
21002 tree tmpl =
21003 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
21004 if (tmpl)
21005 error ("explicit instantiation of non-class template %qD", tmpl);
21006 else
21007 error ("explicit instantiation of non-template type %qT", t);
21008 return;
21011 complete_type (t);
21013 if (!COMPLETE_TYPE_P (t))
21015 if (complain & tf_error)
21016 error ("explicit instantiation of %q#T before definition of template",
21018 return;
21021 if (storage != NULL_TREE)
21023 if (!in_system_header_at (input_location))
21025 if (storage == ridpointers[(int) RID_EXTERN])
21027 if (cxx_dialect == cxx98)
21028 pedwarn (input_location, OPT_Wpedantic,
21029 "ISO C++ 1998 forbids the use of %<extern%> on "
21030 "explicit instantiations");
21032 else
21033 pedwarn (input_location, OPT_Wpedantic,
21034 "ISO C++ forbids the use of %qE"
21035 " on explicit instantiations", storage);
21038 if (storage == ridpointers[(int) RID_INLINE])
21039 nomem_p = 1;
21040 else if (storage == ridpointers[(int) RID_EXTERN])
21041 extern_p = 1;
21042 else if (storage == ridpointers[(int) RID_STATIC])
21043 static_p = 1;
21044 else
21046 error ("storage class %qD applied to template instantiation",
21047 storage);
21048 extern_p = 0;
21052 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
21054 /* DR 259 [temp.spec].
21056 Both an explicit instantiation and a declaration of an explicit
21057 specialization shall not appear in a program unless the explicit
21058 instantiation follows a declaration of the explicit specialization.
21060 For a given set of template parameters, if an explicit
21061 instantiation of a template appears after a declaration of an
21062 explicit specialization for that template, the explicit
21063 instantiation has no effect. */
21064 return;
21066 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
21068 /* [temp.spec]
21070 No program shall explicitly instantiate any template more
21071 than once.
21073 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21074 instantiation was `extern'. If EXTERN_P then the second is.
21075 These cases are OK. */
21076 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
21078 if (!previous_instantiation_extern_p && !extern_p
21079 && (complain & tf_error))
21080 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
21082 /* If we've already instantiated the template, just return now. */
21083 if (!CLASSTYPE_INTERFACE_ONLY (t))
21084 return;
21087 check_explicit_instantiation_namespace (TYPE_NAME (t));
21088 mark_class_instantiated (t, extern_p);
21090 if (nomem_p)
21091 return;
21094 tree tmp;
21096 /* In contrast to implicit instantiation, where only the
21097 declarations, and not the definitions, of members are
21098 instantiated, we have here:
21100 [temp.explicit]
21102 The explicit instantiation of a class template specialization
21103 implies the instantiation of all of its members not
21104 previously explicitly specialized in the translation unit
21105 containing the explicit instantiation.
21107 Of course, we can't instantiate member template classes, since
21108 we don't have any arguments for them. Note that the standard
21109 is unclear on whether the instantiation of the members are
21110 *explicit* instantiations or not. However, the most natural
21111 interpretation is that it should be an explicit instantiation. */
21113 if (! static_p)
21114 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
21115 if (TREE_CODE (tmp) == FUNCTION_DECL
21116 && DECL_TEMPLATE_INSTANTIATION (tmp))
21117 instantiate_class_member (tmp, extern_p);
21119 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
21120 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
21121 instantiate_class_member (tmp, extern_p);
21123 if (CLASSTYPE_NESTED_UTDS (t))
21124 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
21125 bt_instantiate_type_proc, &storage);
21129 /* Given a function DECL, which is a specialization of TMPL, modify
21130 DECL to be a re-instantiation of TMPL with the same template
21131 arguments. TMPL should be the template into which tsubst'ing
21132 should occur for DECL, not the most general template.
21134 One reason for doing this is a scenario like this:
21136 template <class T>
21137 void f(const T&, int i);
21139 void g() { f(3, 7); }
21141 template <class T>
21142 void f(const T& t, const int i) { }
21144 Note that when the template is first instantiated, with
21145 instantiate_template, the resulting DECL will have no name for the
21146 first parameter, and the wrong type for the second. So, when we go
21147 to instantiate the DECL, we regenerate it. */
21149 static void
21150 regenerate_decl_from_template (tree decl, tree tmpl)
21152 /* The arguments used to instantiate DECL, from the most general
21153 template. */
21154 tree args;
21155 tree code_pattern;
21157 args = DECL_TI_ARGS (decl);
21158 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
21160 /* Make sure that we can see identifiers, and compute access
21161 correctly. */
21162 push_access_scope (decl);
21164 if (TREE_CODE (decl) == FUNCTION_DECL)
21166 tree decl_parm;
21167 tree pattern_parm;
21168 tree specs;
21169 int args_depth;
21170 int parms_depth;
21172 args_depth = TMPL_ARGS_DEPTH (args);
21173 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
21174 if (args_depth > parms_depth)
21175 args = get_innermost_template_args (args, parms_depth);
21177 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
21178 args, tf_error, NULL_TREE,
21179 /*defer_ok*/false);
21180 if (specs && specs != error_mark_node)
21181 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
21182 specs);
21184 /* Merge parameter declarations. */
21185 decl_parm = skip_artificial_parms_for (decl,
21186 DECL_ARGUMENTS (decl));
21187 pattern_parm
21188 = skip_artificial_parms_for (code_pattern,
21189 DECL_ARGUMENTS (code_pattern));
21190 while (decl_parm && !DECL_PACK_P (pattern_parm))
21192 tree parm_type;
21193 tree attributes;
21195 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21196 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
21197 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
21198 NULL_TREE);
21199 parm_type = type_decays_to (parm_type);
21200 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21201 TREE_TYPE (decl_parm) = parm_type;
21202 attributes = DECL_ATTRIBUTES (pattern_parm);
21203 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21205 DECL_ATTRIBUTES (decl_parm) = attributes;
21206 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21208 decl_parm = DECL_CHAIN (decl_parm);
21209 pattern_parm = DECL_CHAIN (pattern_parm);
21211 /* Merge any parameters that match with the function parameter
21212 pack. */
21213 if (pattern_parm && DECL_PACK_P (pattern_parm))
21215 int i, len;
21216 tree expanded_types;
21217 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21218 the parameters in this function parameter pack. */
21219 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
21220 args, tf_error, NULL_TREE);
21221 len = TREE_VEC_LENGTH (expanded_types);
21222 for (i = 0; i < len; i++)
21224 tree parm_type;
21225 tree attributes;
21227 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21228 /* Rename the parameter to include the index. */
21229 DECL_NAME (decl_parm) =
21230 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
21231 parm_type = TREE_VEC_ELT (expanded_types, i);
21232 parm_type = type_decays_to (parm_type);
21233 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21234 TREE_TYPE (decl_parm) = parm_type;
21235 attributes = DECL_ATTRIBUTES (pattern_parm);
21236 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21238 DECL_ATTRIBUTES (decl_parm) = attributes;
21239 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21241 decl_parm = DECL_CHAIN (decl_parm);
21244 /* Merge additional specifiers from the CODE_PATTERN. */
21245 if (DECL_DECLARED_INLINE_P (code_pattern)
21246 && !DECL_DECLARED_INLINE_P (decl))
21247 DECL_DECLARED_INLINE_P (decl) = 1;
21249 else if (VAR_P (decl))
21251 DECL_INITIAL (decl) =
21252 tsubst_expr (DECL_INITIAL (code_pattern), args,
21253 tf_error, DECL_TI_TEMPLATE (decl),
21254 /*integral_constant_expression_p=*/false);
21255 if (VAR_HAD_UNKNOWN_BOUND (decl))
21256 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
21257 tf_error, DECL_TI_TEMPLATE (decl));
21259 else
21260 gcc_unreachable ();
21262 pop_access_scope (decl);
21265 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21266 substituted to get DECL. */
21268 tree
21269 template_for_substitution (tree decl)
21271 tree tmpl = DECL_TI_TEMPLATE (decl);
21273 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21274 for the instantiation. This is not always the most general
21275 template. Consider, for example:
21277 template <class T>
21278 struct S { template <class U> void f();
21279 template <> void f<int>(); };
21281 and an instantiation of S<double>::f<int>. We want TD to be the
21282 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21283 while (/* An instantiation cannot have a definition, so we need a
21284 more general template. */
21285 DECL_TEMPLATE_INSTANTIATION (tmpl)
21286 /* We must also deal with friend templates. Given:
21288 template <class T> struct S {
21289 template <class U> friend void f() {};
21292 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21293 so far as the language is concerned, but that's still
21294 where we get the pattern for the instantiation from. On
21295 other hand, if the definition comes outside the class, say:
21297 template <class T> struct S {
21298 template <class U> friend void f();
21300 template <class U> friend void f() {}
21302 we don't need to look any further. That's what the check for
21303 DECL_INITIAL is for. */
21304 || (TREE_CODE (decl) == FUNCTION_DECL
21305 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
21306 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
21308 /* The present template, TD, should not be a definition. If it
21309 were a definition, we should be using it! Note that we
21310 cannot restructure the loop to just keep going until we find
21311 a template with a definition, since that might go too far if
21312 a specialization was declared, but not defined. */
21314 /* Fetch the more general template. */
21315 tmpl = DECL_TI_TEMPLATE (tmpl);
21318 return tmpl;
21321 /* Returns true if we need to instantiate this template instance even if we
21322 know we aren't going to emit it. */
21324 bool
21325 always_instantiate_p (tree decl)
21327 /* We always instantiate inline functions so that we can inline them. An
21328 explicit instantiation declaration prohibits implicit instantiation of
21329 non-inline functions. With high levels of optimization, we would
21330 normally inline non-inline functions -- but we're not allowed to do
21331 that for "extern template" functions. Therefore, we check
21332 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21333 return ((TREE_CODE (decl) == FUNCTION_DECL
21334 && (DECL_DECLARED_INLINE_P (decl)
21335 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
21336 /* And we need to instantiate static data members so that
21337 their initializers are available in integral constant
21338 expressions. */
21339 || (VAR_P (decl)
21340 && decl_maybe_constant_var_p (decl)));
21343 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21344 instantiate it now, modifying TREE_TYPE (fn). */
21346 void
21347 maybe_instantiate_noexcept (tree fn)
21349 tree fntype, spec, noex, clone;
21351 /* Don't instantiate a noexcept-specification from template context. */
21352 if (processing_template_decl)
21353 return;
21355 if (DECL_CLONED_FUNCTION_P (fn))
21356 fn = DECL_CLONED_FUNCTION (fn);
21357 fntype = TREE_TYPE (fn);
21358 spec = TYPE_RAISES_EXCEPTIONS (fntype);
21360 if (!spec || !TREE_PURPOSE (spec))
21361 return;
21363 noex = TREE_PURPOSE (spec);
21365 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
21367 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
21368 spec = get_defaulted_eh_spec (fn);
21369 else if (push_tinst_level (fn))
21371 push_access_scope (fn);
21372 push_deferring_access_checks (dk_no_deferred);
21373 input_location = DECL_SOURCE_LOCATION (fn);
21374 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
21375 DEFERRED_NOEXCEPT_ARGS (noex),
21376 tf_warning_or_error, fn,
21377 /*function_p=*/false,
21378 /*integral_constant_expression_p=*/true);
21379 pop_deferring_access_checks ();
21380 pop_access_scope (fn);
21381 pop_tinst_level ();
21382 spec = build_noexcept_spec (noex, tf_warning_or_error);
21383 if (spec == error_mark_node)
21384 spec = noexcept_false_spec;
21386 else
21387 spec = noexcept_false_spec;
21389 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
21392 FOR_EACH_CLONE (clone, fn)
21394 if (TREE_TYPE (clone) == fntype)
21395 TREE_TYPE (clone) = TREE_TYPE (fn);
21396 else
21397 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
21401 /* Produce the definition of D, a _DECL generated from a template. If
21402 DEFER_OK is nonzero, then we don't have to actually do the
21403 instantiation now; we just have to do it sometime. Normally it is
21404 an error if this is an explicit instantiation but D is undefined.
21405 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21406 explicitly instantiated class template. */
21408 tree
21409 instantiate_decl (tree d, int defer_ok,
21410 bool expl_inst_class_mem_p)
21412 tree tmpl = DECL_TI_TEMPLATE (d);
21413 tree gen_args;
21414 tree args;
21415 tree td;
21416 tree code_pattern;
21417 tree spec;
21418 tree gen_tmpl;
21419 bool pattern_defined;
21420 location_t saved_loc = input_location;
21421 int saved_unevaluated_operand = cp_unevaluated_operand;
21422 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21423 bool external_p;
21424 bool deleted_p;
21425 tree fn_context;
21426 bool nested = false;
21428 /* This function should only be used to instantiate templates for
21429 functions and static member variables. */
21430 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
21432 /* A concept is never instantiated. */
21433 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
21435 /* Variables are never deferred; if instantiation is required, they
21436 are instantiated right away. That allows for better code in the
21437 case that an expression refers to the value of the variable --
21438 if the variable has a constant value the referring expression can
21439 take advantage of that fact. */
21440 if (VAR_P (d)
21441 || DECL_DECLARED_CONSTEXPR_P (d))
21442 defer_ok = 0;
21444 /* Don't instantiate cloned functions. Instead, instantiate the
21445 functions they cloned. */
21446 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
21447 d = DECL_CLONED_FUNCTION (d);
21449 if (DECL_TEMPLATE_INSTANTIATED (d)
21450 || (TREE_CODE (d) == FUNCTION_DECL
21451 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
21452 || DECL_TEMPLATE_SPECIALIZATION (d))
21453 /* D has already been instantiated or explicitly specialized, so
21454 there's nothing for us to do here.
21456 It might seem reasonable to check whether or not D is an explicit
21457 instantiation, and, if so, stop here. But when an explicit
21458 instantiation is deferred until the end of the compilation,
21459 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21460 the instantiation. */
21461 return d;
21463 /* Check to see whether we know that this template will be
21464 instantiated in some other file, as with "extern template"
21465 extension. */
21466 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
21468 /* In general, we do not instantiate such templates. */
21469 if (external_p && !always_instantiate_p (d))
21470 return d;
21472 gen_tmpl = most_general_template (tmpl);
21473 gen_args = DECL_TI_ARGS (d);
21475 if (tmpl != gen_tmpl)
21476 /* We should already have the extra args. */
21477 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
21478 == TMPL_ARGS_DEPTH (gen_args));
21479 /* And what's in the hash table should match D. */
21480 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
21481 || spec == NULL_TREE);
21483 /* This needs to happen before any tsubsting. */
21484 if (! push_tinst_level (d))
21485 return d;
21487 timevar_push (TV_TEMPLATE_INST);
21489 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21490 for the instantiation. */
21491 td = template_for_substitution (d);
21492 code_pattern = DECL_TEMPLATE_RESULT (td);
21494 /* We should never be trying to instantiate a member of a class
21495 template or partial specialization. */
21496 gcc_assert (d != code_pattern);
21498 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
21499 || DECL_TEMPLATE_SPECIALIZATION (td))
21500 /* In the case of a friend template whose definition is provided
21501 outside the class, we may have too many arguments. Drop the
21502 ones we don't need. The same is true for specializations. */
21503 args = get_innermost_template_args
21504 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
21505 else
21506 args = gen_args;
21508 if (TREE_CODE (d) == FUNCTION_DECL)
21510 deleted_p = DECL_DELETED_FN (code_pattern);
21511 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
21512 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
21513 || deleted_p);
21515 else
21517 deleted_p = false;
21518 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
21521 /* We may be in the middle of deferred access check. Disable it now. */
21522 push_deferring_access_checks (dk_no_deferred);
21524 /* Unless an explicit instantiation directive has already determined
21525 the linkage of D, remember that a definition is available for
21526 this entity. */
21527 if (pattern_defined
21528 && !DECL_INTERFACE_KNOWN (d)
21529 && !DECL_NOT_REALLY_EXTERN (d))
21530 mark_definable (d);
21532 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
21533 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
21534 input_location = DECL_SOURCE_LOCATION (d);
21536 /* If D is a member of an explicitly instantiated class template,
21537 and no definition is available, treat it like an implicit
21538 instantiation. */
21539 if (!pattern_defined && expl_inst_class_mem_p
21540 && DECL_EXPLICIT_INSTANTIATION (d))
21542 /* Leave linkage flags alone on instantiations with anonymous
21543 visibility. */
21544 if (TREE_PUBLIC (d))
21546 DECL_NOT_REALLY_EXTERN (d) = 0;
21547 DECL_INTERFACE_KNOWN (d) = 0;
21549 SET_DECL_IMPLICIT_INSTANTIATION (d);
21552 /* Defer all other templates, unless we have been explicitly
21553 forbidden from doing so. */
21554 if (/* If there is no definition, we cannot instantiate the
21555 template. */
21556 ! pattern_defined
21557 /* If it's OK to postpone instantiation, do so. */
21558 || defer_ok
21559 /* If this is a static data member that will be defined
21560 elsewhere, we don't want to instantiate the entire data
21561 member, but we do want to instantiate the initializer so that
21562 we can substitute that elsewhere. */
21563 || (external_p && VAR_P (d))
21564 /* Handle here a deleted function too, avoid generating
21565 its body (c++/61080). */
21566 || deleted_p)
21568 /* The definition of the static data member is now required so
21569 we must substitute the initializer. */
21570 if (VAR_P (d)
21571 && !DECL_INITIAL (d)
21572 && DECL_INITIAL (code_pattern))
21574 tree ns;
21575 tree init;
21576 bool const_init = false;
21577 bool enter_context = DECL_CLASS_SCOPE_P (d);
21579 ns = decl_namespace_context (d);
21580 push_nested_namespace (ns);
21581 if (enter_context)
21582 push_nested_class (DECL_CONTEXT (d));
21583 init = tsubst_expr (DECL_INITIAL (code_pattern),
21584 args,
21585 tf_warning_or_error, NULL_TREE,
21586 /*integral_constant_expression_p=*/false);
21587 /* If instantiating the initializer involved instantiating this
21588 again, don't call cp_finish_decl twice. */
21589 if (!DECL_INITIAL (d))
21591 /* Make sure the initializer is still constant, in case of
21592 circular dependency (template/instantiate6.C). */
21593 const_init
21594 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21595 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
21596 /*asmspec_tree=*/NULL_TREE,
21597 LOOKUP_ONLYCONVERTING);
21599 if (enter_context)
21600 pop_nested_class ();
21601 pop_nested_namespace (ns);
21604 /* We restore the source position here because it's used by
21605 add_pending_template. */
21606 input_location = saved_loc;
21608 if (at_eof && !pattern_defined
21609 && DECL_EXPLICIT_INSTANTIATION (d)
21610 && DECL_NOT_REALLY_EXTERN (d))
21611 /* [temp.explicit]
21613 The definition of a non-exported function template, a
21614 non-exported member function template, or a non-exported
21615 member function or static data member of a class template
21616 shall be present in every translation unit in which it is
21617 explicitly instantiated. */
21618 permerror (input_location, "explicit instantiation of %qD "
21619 "but no definition available", d);
21621 /* If we're in unevaluated context, we just wanted to get the
21622 constant value; this isn't an odr use, so don't queue
21623 a full instantiation. */
21624 if (cp_unevaluated_operand != 0)
21625 goto out;
21626 /* ??? Historically, we have instantiated inline functions, even
21627 when marked as "extern template". */
21628 if (!(external_p && VAR_P (d)))
21629 add_pending_template (d);
21630 goto out;
21632 /* Tell the repository that D is available in this translation unit
21633 -- and see if it is supposed to be instantiated here. */
21634 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
21636 /* In a PCH file, despite the fact that the repository hasn't
21637 requested instantiation in the PCH it is still possible that
21638 an instantiation will be required in a file that includes the
21639 PCH. */
21640 if (pch_file)
21641 add_pending_template (d);
21642 /* Instantiate inline functions so that the inliner can do its
21643 job, even though we'll not be emitting a copy of this
21644 function. */
21645 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
21646 goto out;
21649 fn_context = decl_function_context (d);
21650 nested = (current_function_decl != NULL_TREE);
21651 vec<tree> omp_privatization_save;
21652 if (nested)
21653 save_omp_privatization_clauses (omp_privatization_save);
21655 if (!fn_context)
21656 push_to_top_level ();
21657 else
21659 if (nested)
21660 push_function_context ();
21661 cp_unevaluated_operand = 0;
21662 c_inhibit_evaluation_warnings = 0;
21665 /* Mark D as instantiated so that recursive calls to
21666 instantiate_decl do not try to instantiate it again. */
21667 DECL_TEMPLATE_INSTANTIATED (d) = 1;
21669 /* Regenerate the declaration in case the template has been modified
21670 by a subsequent redeclaration. */
21671 regenerate_decl_from_template (d, td);
21673 /* We already set the file and line above. Reset them now in case
21674 they changed as a result of calling regenerate_decl_from_template. */
21675 input_location = DECL_SOURCE_LOCATION (d);
21677 if (VAR_P (d))
21679 tree init;
21680 bool const_init = false;
21682 /* Clear out DECL_RTL; whatever was there before may not be right
21683 since we've reset the type of the declaration. */
21684 SET_DECL_RTL (d, NULL);
21685 DECL_IN_AGGR_P (d) = 0;
21687 /* The initializer is placed in DECL_INITIAL by
21688 regenerate_decl_from_template so we don't need to
21689 push/pop_access_scope again here. Pull it out so that
21690 cp_finish_decl can process it. */
21691 init = DECL_INITIAL (d);
21692 DECL_INITIAL (d) = NULL_TREE;
21693 DECL_INITIALIZED_P (d) = 0;
21695 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
21696 initializer. That function will defer actual emission until
21697 we have a chance to determine linkage. */
21698 DECL_EXTERNAL (d) = 0;
21700 /* Enter the scope of D so that access-checking works correctly. */
21701 bool enter_context = DECL_CLASS_SCOPE_P (d);
21702 if (enter_context)
21703 push_nested_class (DECL_CONTEXT (d));
21705 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21706 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
21708 if (enter_context)
21709 pop_nested_class ();
21711 if (variable_template_p (td))
21712 note_variable_template_instantiation (d);
21714 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
21715 synthesize_method (d);
21716 else if (TREE_CODE (d) == FUNCTION_DECL)
21718 hash_map<tree, tree> *saved_local_specializations;
21719 tree subst_decl;
21720 tree tmpl_parm;
21721 tree spec_parm;
21722 tree block = NULL_TREE;
21724 /* Save away the current list, in case we are instantiating one
21725 template from within the body of another. */
21726 saved_local_specializations = local_specializations;
21728 /* Set up the list of local specializations, copying the current
21729 list if there is one. */
21730 if (local_specializations)
21731 local_specializations
21732 = new hash_map<tree, tree> (*local_specializations);
21733 else
21734 local_specializations = new hash_map<tree, tree>;
21736 /* Set up context. */
21737 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21738 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21739 block = push_stmt_list ();
21740 else
21741 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
21743 /* Some typedefs referenced from within the template code need to be
21744 access checked at template instantiation time, i.e now. These
21745 types were added to the template at parsing time. Let's get those
21746 and perform the access checks then. */
21747 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
21748 gen_args);
21750 /* Create substitution entries for the parameters. */
21751 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
21752 tmpl_parm = DECL_ARGUMENTS (subst_decl);
21753 spec_parm = DECL_ARGUMENTS (d);
21754 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
21756 register_local_specialization (spec_parm, tmpl_parm);
21757 spec_parm = skip_artificial_parms_for (d, spec_parm);
21758 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
21760 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
21762 if (!DECL_PACK_P (tmpl_parm))
21764 register_local_specialization (spec_parm, tmpl_parm);
21765 spec_parm = DECL_CHAIN (spec_parm);
21767 else
21769 /* Register the (value) argument pack as a specialization of
21770 TMPL_PARM, then move on. */
21771 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
21772 register_local_specialization (argpack, tmpl_parm);
21775 gcc_assert (!spec_parm);
21777 /* Substitute into the body of the function. */
21778 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21779 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
21780 tf_warning_or_error, tmpl);
21781 else
21783 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
21784 tf_warning_or_error, tmpl,
21785 /*integral_constant_expression_p=*/false);
21787 /* Set the current input_location to the end of the function
21788 so that finish_function knows where we are. */
21789 input_location
21790 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
21792 /* Remember if we saw an infinite loop in the template. */
21793 current_function_infinite_loop
21794 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
21797 /* We don't need the local specializations any more. */
21798 delete local_specializations;
21799 local_specializations = saved_local_specializations;
21801 /* Finish the function. */
21802 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21803 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21804 DECL_SAVED_TREE (d) = pop_stmt_list (block);
21805 else
21807 d = finish_function (0);
21808 expand_or_defer_fn (d);
21811 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21812 cp_check_omp_declare_reduction (d);
21815 /* We're not deferring instantiation any more. */
21816 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
21818 if (!fn_context)
21819 pop_from_top_level ();
21820 else if (nested)
21821 pop_function_context ();
21823 out:
21824 input_location = saved_loc;
21825 cp_unevaluated_operand = saved_unevaluated_operand;
21826 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21827 pop_deferring_access_checks ();
21828 pop_tinst_level ();
21829 if (nested)
21830 restore_omp_privatization_clauses (omp_privatization_save);
21832 timevar_pop (TV_TEMPLATE_INST);
21834 return d;
21837 /* Run through the list of templates that we wish we could
21838 instantiate, and instantiate any we can. RETRIES is the
21839 number of times we retry pending template instantiation. */
21841 void
21842 instantiate_pending_templates (int retries)
21844 int reconsider;
21845 location_t saved_loc = input_location;
21847 /* Instantiating templates may trigger vtable generation. This in turn
21848 may require further template instantiations. We place a limit here
21849 to avoid infinite loop. */
21850 if (pending_templates && retries >= max_tinst_depth)
21852 tree decl = pending_templates->tinst->decl;
21854 fatal_error (input_location,
21855 "template instantiation depth exceeds maximum of %d"
21856 " instantiating %q+D, possibly from virtual table generation"
21857 " (use -ftemplate-depth= to increase the maximum)",
21858 max_tinst_depth, decl);
21859 if (TREE_CODE (decl) == FUNCTION_DECL)
21860 /* Pretend that we defined it. */
21861 DECL_INITIAL (decl) = error_mark_node;
21862 return;
21867 struct pending_template **t = &pending_templates;
21868 struct pending_template *last = NULL;
21869 reconsider = 0;
21870 while (*t)
21872 tree instantiation = reopen_tinst_level ((*t)->tinst);
21873 bool complete = false;
21875 if (TYPE_P (instantiation))
21877 tree fn;
21879 if (!COMPLETE_TYPE_P (instantiation))
21881 instantiate_class_template (instantiation);
21882 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
21883 for (fn = TYPE_METHODS (instantiation);
21885 fn = TREE_CHAIN (fn))
21886 if (! DECL_ARTIFICIAL (fn))
21887 instantiate_decl (fn,
21888 /*defer_ok=*/0,
21889 /*expl_inst_class_mem_p=*/false);
21890 if (COMPLETE_TYPE_P (instantiation))
21891 reconsider = 1;
21894 complete = COMPLETE_TYPE_P (instantiation);
21896 else
21898 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
21899 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
21901 instantiation
21902 = instantiate_decl (instantiation,
21903 /*defer_ok=*/0,
21904 /*expl_inst_class_mem_p=*/false);
21905 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
21906 reconsider = 1;
21909 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
21910 || DECL_TEMPLATE_INSTANTIATED (instantiation));
21913 if (complete)
21914 /* If INSTANTIATION has been instantiated, then we don't
21915 need to consider it again in the future. */
21916 *t = (*t)->next;
21917 else
21919 last = *t;
21920 t = &(*t)->next;
21922 tinst_depth = 0;
21923 current_tinst_level = NULL;
21925 last_pending_template = last;
21927 while (reconsider);
21929 input_location = saved_loc;
21932 /* Substitute ARGVEC into T, which is a list of initializers for
21933 either base class or a non-static data member. The TREE_PURPOSEs
21934 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21935 instantiate_decl. */
21937 static tree
21938 tsubst_initializer_list (tree t, tree argvec)
21940 tree inits = NULL_TREE;
21942 for (; t; t = TREE_CHAIN (t))
21944 tree decl;
21945 tree init;
21946 tree expanded_bases = NULL_TREE;
21947 tree expanded_arguments = NULL_TREE;
21948 int i, len = 1;
21950 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
21952 tree expr;
21953 tree arg;
21955 /* Expand the base class expansion type into separate base
21956 classes. */
21957 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
21958 tf_warning_or_error,
21959 NULL_TREE);
21960 if (expanded_bases == error_mark_node)
21961 continue;
21963 /* We'll be building separate TREE_LISTs of arguments for
21964 each base. */
21965 len = TREE_VEC_LENGTH (expanded_bases);
21966 expanded_arguments = make_tree_vec (len);
21967 for (i = 0; i < len; i++)
21968 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
21970 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
21971 expand each argument in the TREE_VALUE of t. */
21972 expr = make_node (EXPR_PACK_EXPANSION);
21973 PACK_EXPANSION_LOCAL_P (expr) = true;
21974 PACK_EXPANSION_PARAMETER_PACKS (expr) =
21975 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
21977 if (TREE_VALUE (t) == void_type_node)
21978 /* VOID_TYPE_NODE is used to indicate
21979 value-initialization. */
21981 for (i = 0; i < len; i++)
21982 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
21984 else
21986 /* Substitute parameter packs into each argument in the
21987 TREE_LIST. */
21988 in_base_initializer = 1;
21989 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
21991 tree expanded_exprs;
21993 /* Expand the argument. */
21994 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
21995 expanded_exprs
21996 = tsubst_pack_expansion (expr, argvec,
21997 tf_warning_or_error,
21998 NULL_TREE);
21999 if (expanded_exprs == error_mark_node)
22000 continue;
22002 /* Prepend each of the expanded expressions to the
22003 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22004 for (i = 0; i < len; i++)
22006 TREE_VEC_ELT (expanded_arguments, i) =
22007 tree_cons (NULL_TREE,
22008 TREE_VEC_ELT (expanded_exprs, i),
22009 TREE_VEC_ELT (expanded_arguments, i));
22012 in_base_initializer = 0;
22014 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22015 since we built them backwards. */
22016 for (i = 0; i < len; i++)
22018 TREE_VEC_ELT (expanded_arguments, i) =
22019 nreverse (TREE_VEC_ELT (expanded_arguments, i));
22024 for (i = 0; i < len; ++i)
22026 if (expanded_bases)
22028 decl = TREE_VEC_ELT (expanded_bases, i);
22029 decl = expand_member_init (decl);
22030 init = TREE_VEC_ELT (expanded_arguments, i);
22032 else
22034 tree tmp;
22035 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
22036 tf_warning_or_error, NULL_TREE);
22038 decl = expand_member_init (decl);
22039 if (decl && !DECL_P (decl))
22040 in_base_initializer = 1;
22042 init = TREE_VALUE (t);
22043 tmp = init;
22044 if (init != void_type_node)
22045 init = tsubst_expr (init, argvec,
22046 tf_warning_or_error, NULL_TREE,
22047 /*integral_constant_expression_p=*/false);
22048 if (init == NULL_TREE && tmp != NULL_TREE)
22049 /* If we had an initializer but it instantiated to nothing,
22050 value-initialize the object. This will only occur when
22051 the initializer was a pack expansion where the parameter
22052 packs used in that expansion were of length zero. */
22053 init = void_type_node;
22054 in_base_initializer = 0;
22057 if (decl)
22059 init = build_tree_list (decl, init);
22060 TREE_CHAIN (init) = inits;
22061 inits = init;
22065 return inits;
22068 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22070 static void
22071 set_current_access_from_decl (tree decl)
22073 if (TREE_PRIVATE (decl))
22074 current_access_specifier = access_private_node;
22075 else if (TREE_PROTECTED (decl))
22076 current_access_specifier = access_protected_node;
22077 else
22078 current_access_specifier = access_public_node;
22081 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22082 is the instantiation (which should have been created with
22083 start_enum) and ARGS are the template arguments to use. */
22085 static void
22086 tsubst_enum (tree tag, tree newtag, tree args)
22088 tree e;
22090 if (SCOPED_ENUM_P (newtag))
22091 begin_scope (sk_scoped_enum, newtag);
22093 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
22095 tree value;
22096 tree decl;
22098 decl = TREE_VALUE (e);
22099 /* Note that in a template enum, the TREE_VALUE is the
22100 CONST_DECL, not the corresponding INTEGER_CST. */
22101 value = tsubst_expr (DECL_INITIAL (decl),
22102 args, tf_warning_or_error, NULL_TREE,
22103 /*integral_constant_expression_p=*/true);
22105 /* Give this enumeration constant the correct access. */
22106 set_current_access_from_decl (decl);
22108 /* Actually build the enumerator itself. Here we're assuming that
22109 enumerators can't have dependent attributes. */
22110 build_enumerator (DECL_NAME (decl), value, newtag,
22111 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
22114 if (SCOPED_ENUM_P (newtag))
22115 finish_scope ();
22117 finish_enum_value_list (newtag);
22118 finish_enum (newtag);
22120 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
22121 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
22124 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22125 its type -- but without substituting the innermost set of template
22126 arguments. So, innermost set of template parameters will appear in
22127 the type. */
22129 tree
22130 get_mostly_instantiated_function_type (tree decl)
22132 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22133 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
22136 /* Return truthvalue if we're processing a template different from
22137 the last one involved in diagnostics. */
22138 bool
22139 problematic_instantiation_changed (void)
22141 return current_tinst_level != last_error_tinst_level;
22144 /* Remember current template involved in diagnostics. */
22145 void
22146 record_last_problematic_instantiation (void)
22148 last_error_tinst_level = current_tinst_level;
22151 struct tinst_level *
22152 current_instantiation (void)
22154 return current_tinst_level;
22157 /* Return TRUE if current_function_decl is being instantiated, false
22158 otherwise. */
22160 bool
22161 instantiating_current_function_p (void)
22163 return (current_instantiation ()
22164 && current_instantiation ()->decl == current_function_decl);
22167 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22168 type. Return zero for ok, nonzero for disallowed. Issue error and
22169 warning messages under control of COMPLAIN. */
22171 static int
22172 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
22174 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
22175 return 0;
22176 else if (POINTER_TYPE_P (type))
22177 return 0;
22178 else if (TYPE_PTRMEM_P (type))
22179 return 0;
22180 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
22181 return 0;
22182 else if (TREE_CODE (type) == TYPENAME_TYPE)
22183 return 0;
22184 else if (TREE_CODE (type) == DECLTYPE_TYPE)
22185 return 0;
22186 else if (TREE_CODE (type) == NULLPTR_TYPE)
22187 return 0;
22188 /* A bound template template parm could later be instantiated to have a valid
22189 nontype parm type via an alias template. */
22190 else if (cxx_dialect >= cxx11
22191 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22192 return 0;
22194 if (complain & tf_error)
22196 if (type == error_mark_node)
22197 inform (input_location, "invalid template non-type parameter");
22198 else
22199 error ("%q#T is not a valid type for a template non-type parameter",
22200 type);
22202 return 1;
22205 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22206 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22208 static bool
22209 dependent_type_p_r (tree type)
22211 tree scope;
22213 /* [temp.dep.type]
22215 A type is dependent if it is:
22217 -- a template parameter. Template template parameters are types
22218 for us (since TYPE_P holds true for them) so we handle
22219 them here. */
22220 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22221 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
22222 return true;
22223 /* -- a qualified-id with a nested-name-specifier which contains a
22224 class-name that names a dependent type or whose unqualified-id
22225 names a dependent type. */
22226 if (TREE_CODE (type) == TYPENAME_TYPE)
22227 return true;
22229 /* An alias template specialization can be dependent even if the
22230 resulting type is not. */
22231 if (dependent_alias_template_spec_p (type))
22232 return true;
22234 /* -- a cv-qualified type where the cv-unqualified type is
22235 dependent.
22236 No code is necessary for this bullet; the code below handles
22237 cv-qualified types, and we don't want to strip aliases with
22238 TYPE_MAIN_VARIANT because of DR 1558. */
22239 /* -- a compound type constructed from any dependent type. */
22240 if (TYPE_PTRMEM_P (type))
22241 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
22242 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22243 (type)));
22244 else if (TYPE_PTR_P (type)
22245 || TREE_CODE (type) == REFERENCE_TYPE)
22246 return dependent_type_p (TREE_TYPE (type));
22247 else if (TREE_CODE (type) == FUNCTION_TYPE
22248 || TREE_CODE (type) == METHOD_TYPE)
22250 tree arg_type;
22252 if (dependent_type_p (TREE_TYPE (type)))
22253 return true;
22254 for (arg_type = TYPE_ARG_TYPES (type);
22255 arg_type;
22256 arg_type = TREE_CHAIN (arg_type))
22257 if (dependent_type_p (TREE_VALUE (arg_type)))
22258 return true;
22259 return false;
22261 /* -- an array type constructed from any dependent type or whose
22262 size is specified by a constant expression that is
22263 value-dependent.
22265 We checked for type- and value-dependence of the bounds in
22266 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22267 if (TREE_CODE (type) == ARRAY_TYPE)
22269 if (TYPE_DOMAIN (type)
22270 && dependent_type_p (TYPE_DOMAIN (type)))
22271 return true;
22272 return dependent_type_p (TREE_TYPE (type));
22275 /* -- a template-id in which either the template name is a template
22276 parameter ... */
22277 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22278 return true;
22279 /* ... or any of the template arguments is a dependent type or
22280 an expression that is type-dependent or value-dependent. */
22281 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
22282 && (any_dependent_template_arguments_p
22283 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
22284 return true;
22286 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22287 dependent; if the argument of the `typeof' expression is not
22288 type-dependent, then it should already been have resolved. */
22289 if (TREE_CODE (type) == TYPEOF_TYPE
22290 || TREE_CODE (type) == DECLTYPE_TYPE
22291 || TREE_CODE (type) == UNDERLYING_TYPE)
22292 return true;
22294 /* A template argument pack is dependent if any of its packed
22295 arguments are. */
22296 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
22298 tree args = ARGUMENT_PACK_ARGS (type);
22299 int i, len = TREE_VEC_LENGTH (args);
22300 for (i = 0; i < len; ++i)
22301 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22302 return true;
22305 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22306 be template parameters. */
22307 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
22308 return true;
22310 /* The standard does not specifically mention types that are local
22311 to template functions or local classes, but they should be
22312 considered dependent too. For example:
22314 template <int I> void f() {
22315 enum E { a = I };
22316 S<sizeof (E)> s;
22319 The size of `E' cannot be known until the value of `I' has been
22320 determined. Therefore, `E' must be considered dependent. */
22321 scope = TYPE_CONTEXT (type);
22322 if (scope && TYPE_P (scope))
22323 return dependent_type_p (scope);
22324 /* Don't use type_dependent_expression_p here, as it can lead
22325 to infinite recursion trying to determine whether a lambda
22326 nested in a lambda is dependent (c++/47687). */
22327 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
22328 && DECL_LANG_SPECIFIC (scope)
22329 && DECL_TEMPLATE_INFO (scope)
22330 && (any_dependent_template_arguments_p
22331 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
22332 return true;
22334 /* Other types are non-dependent. */
22335 return false;
22338 /* Returns TRUE if TYPE is dependent, in the sense of
22339 [temp.dep.type]. Note that a NULL type is considered dependent. */
22341 bool
22342 dependent_type_p (tree type)
22344 /* If there are no template parameters in scope, then there can't be
22345 any dependent types. */
22346 if (!processing_template_decl)
22348 /* If we are not processing a template, then nobody should be
22349 providing us with a dependent type. */
22350 gcc_assert (type);
22351 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
22352 return false;
22355 /* If the type is NULL, we have not computed a type for the entity
22356 in question; in that case, the type is dependent. */
22357 if (!type)
22358 return true;
22360 /* Erroneous types can be considered non-dependent. */
22361 if (type == error_mark_node)
22362 return false;
22364 /* If we have not already computed the appropriate value for TYPE,
22365 do so now. */
22366 if (!TYPE_DEPENDENT_P_VALID (type))
22368 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
22369 TYPE_DEPENDENT_P_VALID (type) = 1;
22372 return TYPE_DEPENDENT_P (type);
22375 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22376 lookup. In other words, a dependent type that is not the current
22377 instantiation. */
22379 bool
22380 dependent_scope_p (tree scope)
22382 return (scope && TYPE_P (scope) && dependent_type_p (scope)
22383 && !currently_open_class (scope));
22386 /* T is a SCOPE_REF; return whether we need to consider it
22387 instantiation-dependent so that we can check access at instantiation
22388 time even though we know which member it resolves to. */
22390 static bool
22391 instantiation_dependent_scope_ref_p (tree t)
22393 if (DECL_P (TREE_OPERAND (t, 1))
22394 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
22395 && accessible_in_template_p (TREE_OPERAND (t, 0),
22396 TREE_OPERAND (t, 1)))
22397 return false;
22398 else
22399 return true;
22402 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22403 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22404 expression. */
22406 /* Note that this predicate is not appropriate for general expressions;
22407 only constant expressions (that satisfy potential_constant_expression)
22408 can be tested for value dependence. */
22410 bool
22411 value_dependent_expression_p (tree expression)
22413 if (!processing_template_decl)
22414 return false;
22416 /* A name declared with a dependent type. */
22417 if (DECL_P (expression) && type_dependent_expression_p (expression))
22418 return true;
22420 switch (TREE_CODE (expression))
22422 case IDENTIFIER_NODE:
22423 /* A name that has not been looked up -- must be dependent. */
22424 return true;
22426 case TEMPLATE_PARM_INDEX:
22427 /* A non-type template parm. */
22428 return true;
22430 case CONST_DECL:
22431 /* A non-type template parm. */
22432 if (DECL_TEMPLATE_PARM_P (expression))
22433 return true;
22434 return value_dependent_expression_p (DECL_INITIAL (expression));
22436 case VAR_DECL:
22437 /* A constant with literal type and is initialized
22438 with an expression that is value-dependent.
22440 Note that a non-dependent parenthesized initializer will have
22441 already been replaced with its constant value, so if we see
22442 a TREE_LIST it must be dependent. */
22443 if (DECL_INITIAL (expression)
22444 && decl_constant_var_p (expression)
22445 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
22446 /* cp_finish_decl doesn't fold reference initializers. */
22447 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
22448 || value_dependent_expression_p (DECL_INITIAL (expression))))
22449 return true;
22450 return false;
22452 case DYNAMIC_CAST_EXPR:
22453 case STATIC_CAST_EXPR:
22454 case CONST_CAST_EXPR:
22455 case REINTERPRET_CAST_EXPR:
22456 case CAST_EXPR:
22457 /* These expressions are value-dependent if the type to which
22458 the cast occurs is dependent or the expression being casted
22459 is value-dependent. */
22461 tree type = TREE_TYPE (expression);
22463 if (dependent_type_p (type))
22464 return true;
22466 /* A functional cast has a list of operands. */
22467 expression = TREE_OPERAND (expression, 0);
22468 if (!expression)
22470 /* If there are no operands, it must be an expression such
22471 as "int()". This should not happen for aggregate types
22472 because it would form non-constant expressions. */
22473 gcc_assert (cxx_dialect >= cxx11
22474 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
22476 return false;
22479 if (TREE_CODE (expression) == TREE_LIST)
22480 return any_value_dependent_elements_p (expression);
22482 return value_dependent_expression_p (expression);
22485 case SIZEOF_EXPR:
22486 if (SIZEOF_EXPR_TYPE_P (expression))
22487 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
22488 /* FALLTHRU */
22489 case ALIGNOF_EXPR:
22490 case TYPEID_EXPR:
22491 /* A `sizeof' expression is value-dependent if the operand is
22492 type-dependent or is a pack expansion. */
22493 expression = TREE_OPERAND (expression, 0);
22494 if (PACK_EXPANSION_P (expression))
22495 return true;
22496 else if (TYPE_P (expression))
22497 return dependent_type_p (expression);
22498 return instantiation_dependent_expression_p (expression);
22500 case AT_ENCODE_EXPR:
22501 /* An 'encode' expression is value-dependent if the operand is
22502 type-dependent. */
22503 expression = TREE_OPERAND (expression, 0);
22504 return dependent_type_p (expression);
22506 case NOEXCEPT_EXPR:
22507 expression = TREE_OPERAND (expression, 0);
22508 return instantiation_dependent_expression_p (expression);
22510 case SCOPE_REF:
22511 /* All instantiation-dependent expressions should also be considered
22512 value-dependent. */
22513 return instantiation_dependent_scope_ref_p (expression);
22515 case COMPONENT_REF:
22516 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
22517 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
22519 case NONTYPE_ARGUMENT_PACK:
22520 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22521 is value-dependent. */
22523 tree values = ARGUMENT_PACK_ARGS (expression);
22524 int i, len = TREE_VEC_LENGTH (values);
22526 for (i = 0; i < len; ++i)
22527 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
22528 return true;
22530 return false;
22533 case TRAIT_EXPR:
22535 tree type2 = TRAIT_EXPR_TYPE2 (expression);
22536 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
22537 || (type2 ? dependent_type_p (type2) : false));
22540 case MODOP_EXPR:
22541 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22542 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
22544 case ARRAY_REF:
22545 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22546 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
22548 case ADDR_EXPR:
22550 tree op = TREE_OPERAND (expression, 0);
22551 return (value_dependent_expression_p (op)
22552 || has_value_dependent_address (op));
22555 case REQUIRES_EXPR:
22556 /* Treat all requires-expressions as value-dependent so
22557 we don't try to fold them. */
22558 return true;
22560 case TYPE_REQ:
22561 return dependent_type_p (TREE_OPERAND (expression, 0));
22563 case CALL_EXPR:
22565 tree fn = get_callee_fndecl (expression);
22566 int i, nargs;
22567 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
22568 return true;
22569 nargs = call_expr_nargs (expression);
22570 for (i = 0; i < nargs; ++i)
22572 tree op = CALL_EXPR_ARG (expression, i);
22573 /* In a call to a constexpr member function, look through the
22574 implicit ADDR_EXPR on the object argument so that it doesn't
22575 cause the call to be considered value-dependent. We also
22576 look through it in potential_constant_expression. */
22577 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
22578 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
22579 && TREE_CODE (op) == ADDR_EXPR)
22580 op = TREE_OPERAND (op, 0);
22581 if (value_dependent_expression_p (op))
22582 return true;
22584 return false;
22587 case TEMPLATE_ID_EXPR:
22588 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22589 type-dependent. */
22590 return type_dependent_expression_p (expression)
22591 || variable_concept_p (TREE_OPERAND (expression, 0));
22593 case CONSTRUCTOR:
22595 unsigned ix;
22596 tree val;
22597 if (dependent_type_p (TREE_TYPE (expression)))
22598 return true;
22599 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
22600 if (value_dependent_expression_p (val))
22601 return true;
22602 return false;
22605 case STMT_EXPR:
22606 /* Treat a GNU statement expression as dependent to avoid crashing
22607 under instantiate_non_dependent_expr; it can't be constant. */
22608 return true;
22610 default:
22611 /* A constant expression is value-dependent if any subexpression is
22612 value-dependent. */
22613 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
22615 case tcc_reference:
22616 case tcc_unary:
22617 case tcc_comparison:
22618 case tcc_binary:
22619 case tcc_expression:
22620 case tcc_vl_exp:
22622 int i, len = cp_tree_operand_length (expression);
22624 for (i = 0; i < len; i++)
22626 tree t = TREE_OPERAND (expression, i);
22628 /* In some cases, some of the operands may be missing.l
22629 (For example, in the case of PREDECREMENT_EXPR, the
22630 amount to increment by may be missing.) That doesn't
22631 make the expression dependent. */
22632 if (t && value_dependent_expression_p (t))
22633 return true;
22636 break;
22637 default:
22638 break;
22640 break;
22643 /* The expression is not value-dependent. */
22644 return false;
22647 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
22648 [temp.dep.expr]. Note that an expression with no type is
22649 considered dependent. Other parts of the compiler arrange for an
22650 expression with type-dependent subexpressions to have no type, so
22651 this function doesn't have to be fully recursive. */
22653 bool
22654 type_dependent_expression_p (tree expression)
22656 if (!processing_template_decl)
22657 return false;
22659 if (expression == NULL_TREE || expression == error_mark_node)
22660 return false;
22662 /* An unresolved name is always dependent. */
22663 if (identifier_p (expression)
22664 || TREE_CODE (expression) == USING_DECL
22665 || TREE_CODE (expression) == WILDCARD_DECL)
22666 return true;
22668 /* A fold expression is type-dependent. */
22669 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
22670 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
22671 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
22672 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
22673 return true;
22675 /* Some expression forms are never type-dependent. */
22676 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
22677 || TREE_CODE (expression) == SIZEOF_EXPR
22678 || TREE_CODE (expression) == ALIGNOF_EXPR
22679 || TREE_CODE (expression) == AT_ENCODE_EXPR
22680 || TREE_CODE (expression) == NOEXCEPT_EXPR
22681 || TREE_CODE (expression) == TRAIT_EXPR
22682 || TREE_CODE (expression) == TYPEID_EXPR
22683 || TREE_CODE (expression) == DELETE_EXPR
22684 || TREE_CODE (expression) == VEC_DELETE_EXPR
22685 || TREE_CODE (expression) == THROW_EXPR
22686 || TREE_CODE (expression) == REQUIRES_EXPR)
22687 return false;
22689 /* The types of these expressions depends only on the type to which
22690 the cast occurs. */
22691 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
22692 || TREE_CODE (expression) == STATIC_CAST_EXPR
22693 || TREE_CODE (expression) == CONST_CAST_EXPR
22694 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
22695 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
22696 || TREE_CODE (expression) == CAST_EXPR)
22697 return dependent_type_p (TREE_TYPE (expression));
22699 /* The types of these expressions depends only on the type created
22700 by the expression. */
22701 if (TREE_CODE (expression) == NEW_EXPR
22702 || TREE_CODE (expression) == VEC_NEW_EXPR)
22704 /* For NEW_EXPR tree nodes created inside a template, either
22705 the object type itself or a TREE_LIST may appear as the
22706 operand 1. */
22707 tree type = TREE_OPERAND (expression, 1);
22708 if (TREE_CODE (type) == TREE_LIST)
22709 /* This is an array type. We need to check array dimensions
22710 as well. */
22711 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
22712 || value_dependent_expression_p
22713 (TREE_OPERAND (TREE_VALUE (type), 1));
22714 else
22715 return dependent_type_p (type);
22718 if (TREE_CODE (expression) == SCOPE_REF)
22720 tree scope = TREE_OPERAND (expression, 0);
22721 tree name = TREE_OPERAND (expression, 1);
22723 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
22724 contains an identifier associated by name lookup with one or more
22725 declarations declared with a dependent type, or...a
22726 nested-name-specifier or qualified-id that names a member of an
22727 unknown specialization. */
22728 return (type_dependent_expression_p (name)
22729 || dependent_scope_p (scope));
22732 if (TREE_CODE (expression) == FUNCTION_DECL
22733 && DECL_LANG_SPECIFIC (expression)
22734 && DECL_TEMPLATE_INFO (expression)
22735 && (any_dependent_template_arguments_p
22736 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
22737 return true;
22739 if (TREE_CODE (expression) == TEMPLATE_DECL
22740 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
22741 return false;
22743 if (TREE_CODE (expression) == STMT_EXPR)
22744 expression = stmt_expr_value_expr (expression);
22746 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
22748 tree elt;
22749 unsigned i;
22751 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
22753 if (type_dependent_expression_p (elt))
22754 return true;
22756 return false;
22759 /* A static data member of the current instantiation with incomplete
22760 array type is type-dependent, as the definition and specializations
22761 can have different bounds. */
22762 if (VAR_P (expression)
22763 && DECL_CLASS_SCOPE_P (expression)
22764 && dependent_type_p (DECL_CONTEXT (expression))
22765 && VAR_HAD_UNKNOWN_BOUND (expression))
22766 return true;
22768 /* An array of unknown bound depending on a variadic parameter, eg:
22770 template<typename... Args>
22771 void foo (Args... args)
22773 int arr[] = { args... };
22776 template<int... vals>
22777 void bar ()
22779 int arr[] = { vals... };
22782 If the array has no length and has an initializer, it must be that
22783 we couldn't determine its length in cp_complete_array_type because
22784 it is dependent. */
22785 if (VAR_P (expression)
22786 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
22787 && !TYPE_DOMAIN (TREE_TYPE (expression))
22788 && DECL_INITIAL (expression))
22789 return true;
22791 /* A variable template specialization is type-dependent if it has any
22792 dependent template arguments. */
22793 if (VAR_P (expression)
22794 && DECL_LANG_SPECIFIC (expression)
22795 && DECL_TEMPLATE_INFO (expression)
22796 && variable_template_p (DECL_TI_TEMPLATE (expression)))
22797 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
22799 /* Always dependent, on the number of arguments if nothing else. */
22800 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
22801 return true;
22803 if (TREE_TYPE (expression) == unknown_type_node)
22805 if (TREE_CODE (expression) == ADDR_EXPR)
22806 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
22807 if (TREE_CODE (expression) == COMPONENT_REF
22808 || TREE_CODE (expression) == OFFSET_REF)
22810 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
22811 return true;
22812 expression = TREE_OPERAND (expression, 1);
22813 if (identifier_p (expression))
22814 return false;
22816 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22817 if (TREE_CODE (expression) == SCOPE_REF)
22818 return false;
22820 if (BASELINK_P (expression))
22822 if (BASELINK_OPTYPE (expression)
22823 && dependent_type_p (BASELINK_OPTYPE (expression)))
22824 return true;
22825 expression = BASELINK_FUNCTIONS (expression);
22828 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
22830 if (any_dependent_template_arguments_p
22831 (TREE_OPERAND (expression, 1)))
22832 return true;
22833 expression = TREE_OPERAND (expression, 0);
22834 if (identifier_p (expression))
22835 return true;
22838 gcc_assert (TREE_CODE (expression) == OVERLOAD
22839 || TREE_CODE (expression) == FUNCTION_DECL);
22841 while (expression)
22843 if (type_dependent_expression_p (OVL_CURRENT (expression)))
22844 return true;
22845 expression = OVL_NEXT (expression);
22847 return false;
22850 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
22852 return (dependent_type_p (TREE_TYPE (expression)));
22855 /* walk_tree callback function for instantiation_dependent_expression_p,
22856 below. Returns non-zero if a dependent subexpression is found. */
22858 static tree
22859 instantiation_dependent_r (tree *tp, int *walk_subtrees,
22860 void * /*data*/)
22862 if (TYPE_P (*tp))
22864 /* We don't have to worry about decltype currently because decltype
22865 of an instantiation-dependent expr is a dependent type. This
22866 might change depending on the resolution of DR 1172. */
22867 *walk_subtrees = false;
22868 return NULL_TREE;
22870 enum tree_code code = TREE_CODE (*tp);
22871 switch (code)
22873 /* Don't treat an argument list as dependent just because it has no
22874 TREE_TYPE. */
22875 case TREE_LIST:
22876 case TREE_VEC:
22877 return NULL_TREE;
22879 case VAR_DECL:
22880 case CONST_DECL:
22881 /* A constant with a dependent initializer is dependent. */
22882 if (value_dependent_expression_p (*tp))
22883 return *tp;
22884 break;
22886 case TEMPLATE_PARM_INDEX:
22887 return *tp;
22889 /* Handle expressions with type operands. */
22890 case SIZEOF_EXPR:
22891 case ALIGNOF_EXPR:
22892 case TYPEID_EXPR:
22893 case AT_ENCODE_EXPR:
22895 tree op = TREE_OPERAND (*tp, 0);
22896 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
22897 op = TREE_TYPE (op);
22898 if (TYPE_P (op))
22900 if (dependent_type_p (op))
22901 return *tp;
22902 else
22904 *walk_subtrees = false;
22905 return NULL_TREE;
22908 break;
22911 case TRAIT_EXPR:
22912 if (value_dependent_expression_p (*tp))
22913 return *tp;
22914 *walk_subtrees = false;
22915 return NULL_TREE;
22917 case COMPONENT_REF:
22918 if (identifier_p (TREE_OPERAND (*tp, 1)))
22919 /* In a template, finish_class_member_access_expr creates a
22920 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22921 type-dependent, so that we can check access control at
22922 instantiation time (PR 42277). See also Core issue 1273. */
22923 return *tp;
22924 break;
22926 case SCOPE_REF:
22927 if (instantiation_dependent_scope_ref_p (*tp))
22928 return *tp;
22929 else
22930 break;
22932 /* Treat statement-expressions as dependent. */
22933 case BIND_EXPR:
22934 return *tp;
22936 /* Treat requires-expressions as dependent. */
22937 case REQUIRES_EXPR:
22938 return *tp;
22940 case CALL_EXPR:
22941 /* Treat calls to function concepts as dependent. */
22942 if (function_concept_check_p (*tp))
22943 return *tp;
22944 break;
22946 case TEMPLATE_ID_EXPR:
22947 /* And variable concepts. */
22948 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
22949 return *tp;
22950 break;
22952 default:
22953 break;
22956 if (type_dependent_expression_p (*tp))
22957 return *tp;
22958 else
22959 return NULL_TREE;
22962 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
22963 sense defined by the ABI:
22965 "An expression is instantiation-dependent if it is type-dependent
22966 or value-dependent, or it has a subexpression that is type-dependent
22967 or value-dependent." */
22969 bool
22970 instantiation_dependent_expression_p (tree expression)
22972 tree result;
22974 if (!processing_template_decl)
22975 return false;
22977 if (expression == error_mark_node)
22978 return false;
22980 result = cp_walk_tree_without_duplicates (&expression,
22981 instantiation_dependent_r, NULL);
22982 return result != NULL_TREE;
22985 /* Like type_dependent_expression_p, but it also works while not processing
22986 a template definition, i.e. during substitution or mangling. */
22988 bool
22989 type_dependent_expression_p_push (tree expr)
22991 bool b;
22992 ++processing_template_decl;
22993 b = type_dependent_expression_p (expr);
22994 --processing_template_decl;
22995 return b;
22998 /* Returns TRUE if ARGS contains a type-dependent expression. */
23000 bool
23001 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
23003 unsigned int i;
23004 tree arg;
23006 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
23008 if (type_dependent_expression_p (arg))
23009 return true;
23011 return false;
23014 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23015 expressions) contains any type-dependent expressions. */
23017 bool
23018 any_type_dependent_elements_p (const_tree list)
23020 for (; list; list = TREE_CHAIN (list))
23021 if (type_dependent_expression_p (TREE_VALUE (list)))
23022 return true;
23024 return false;
23027 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23028 expressions) contains any value-dependent expressions. */
23030 bool
23031 any_value_dependent_elements_p (const_tree list)
23033 for (; list; list = TREE_CHAIN (list))
23034 if (value_dependent_expression_p (TREE_VALUE (list)))
23035 return true;
23037 return false;
23040 /* Returns TRUE if the ARG (a template argument) is dependent. */
23042 bool
23043 dependent_template_arg_p (tree arg)
23045 if (!processing_template_decl)
23046 return false;
23048 /* Assume a template argument that was wrongly written by the user
23049 is dependent. This is consistent with what
23050 any_dependent_template_arguments_p [that calls this function]
23051 does. */
23052 if (!arg || arg == error_mark_node)
23053 return true;
23055 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
23056 arg = ARGUMENT_PACK_SELECT_ARG (arg);
23058 if (TREE_CODE (arg) == TEMPLATE_DECL
23059 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
23060 return dependent_template_p (arg);
23061 else if (ARGUMENT_PACK_P (arg))
23063 tree args = ARGUMENT_PACK_ARGS (arg);
23064 int i, len = TREE_VEC_LENGTH (args);
23065 for (i = 0; i < len; ++i)
23067 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23068 return true;
23071 return false;
23073 else if (TYPE_P (arg))
23074 return dependent_type_p (arg);
23075 else
23076 return (type_dependent_expression_p (arg)
23077 || value_dependent_expression_p (arg));
23080 /* Returns true if ARGS (a collection of template arguments) contains
23081 any types that require structural equality testing. */
23083 bool
23084 any_template_arguments_need_structural_equality_p (tree args)
23086 int i;
23087 int j;
23089 if (!args)
23090 return false;
23091 if (args == error_mark_node)
23092 return true;
23094 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23096 tree level = TMPL_ARGS_LEVEL (args, i + 1);
23097 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23099 tree arg = TREE_VEC_ELT (level, j);
23100 tree packed_args = NULL_TREE;
23101 int k, len = 1;
23103 if (ARGUMENT_PACK_P (arg))
23105 /* Look inside the argument pack. */
23106 packed_args = ARGUMENT_PACK_ARGS (arg);
23107 len = TREE_VEC_LENGTH (packed_args);
23110 for (k = 0; k < len; ++k)
23112 if (packed_args)
23113 arg = TREE_VEC_ELT (packed_args, k);
23115 if (error_operand_p (arg))
23116 return true;
23117 else if (TREE_CODE (arg) == TEMPLATE_DECL)
23118 continue;
23119 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
23120 return true;
23121 else if (!TYPE_P (arg) && TREE_TYPE (arg)
23122 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
23123 return true;
23128 return false;
23131 /* Returns true if ARGS (a collection of template arguments) contains
23132 any dependent arguments. */
23134 bool
23135 any_dependent_template_arguments_p (const_tree args)
23137 int i;
23138 int j;
23140 if (!args)
23141 return false;
23142 if (args == error_mark_node)
23143 return true;
23145 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23147 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
23148 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23149 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
23150 return true;
23153 return false;
23156 /* Returns TRUE if the template TMPL is dependent. */
23158 bool
23159 dependent_template_p (tree tmpl)
23161 if (TREE_CODE (tmpl) == OVERLOAD)
23163 while (tmpl)
23165 if (dependent_template_p (OVL_CURRENT (tmpl)))
23166 return true;
23167 tmpl = OVL_NEXT (tmpl);
23169 return false;
23172 /* Template template parameters are dependent. */
23173 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
23174 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
23175 return true;
23176 /* So are names that have not been looked up. */
23177 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
23178 return true;
23179 /* So are member templates of dependent classes. */
23180 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
23181 return dependent_type_p (DECL_CONTEXT (tmpl));
23182 return false;
23185 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23187 bool
23188 dependent_template_id_p (tree tmpl, tree args)
23190 return (dependent_template_p (tmpl)
23191 || any_dependent_template_arguments_p (args));
23194 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23195 are dependent. */
23197 bool
23198 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
23200 int i;
23202 if (!processing_template_decl)
23203 return false;
23205 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
23207 tree decl = TREE_VEC_ELT (declv, i);
23208 tree init = TREE_VEC_ELT (initv, i);
23209 tree cond = TREE_VEC_ELT (condv, i);
23210 tree incr = TREE_VEC_ELT (incrv, i);
23212 if (type_dependent_expression_p (decl)
23213 || TREE_CODE (decl) == SCOPE_REF)
23214 return true;
23216 if (init && type_dependent_expression_p (init))
23217 return true;
23219 if (type_dependent_expression_p (cond))
23220 return true;
23222 if (COMPARISON_CLASS_P (cond)
23223 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
23224 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
23225 return true;
23227 if (TREE_CODE (incr) == MODOP_EXPR)
23229 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
23230 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
23231 return true;
23233 else if (type_dependent_expression_p (incr))
23234 return true;
23235 else if (TREE_CODE (incr) == MODIFY_EXPR)
23237 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
23238 return true;
23239 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
23241 tree t = TREE_OPERAND (incr, 1);
23242 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
23243 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
23244 return true;
23249 return false;
23252 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23253 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23254 no such TYPE can be found. Note that this function peers inside
23255 uninstantiated templates and therefore should be used only in
23256 extremely limited situations. ONLY_CURRENT_P restricts this
23257 peering to the currently open classes hierarchy (which is required
23258 when comparing types). */
23260 tree
23261 resolve_typename_type (tree type, bool only_current_p)
23263 tree scope;
23264 tree name;
23265 tree decl;
23266 int quals;
23267 tree pushed_scope;
23268 tree result;
23270 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
23272 scope = TYPE_CONTEXT (type);
23273 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23274 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23275 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23276 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23277 identifier of the TYPENAME_TYPE anymore.
23278 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23279 TYPENAME_TYPE instead, we avoid messing up with a possible
23280 typedef variant case. */
23281 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
23283 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23284 it first before we can figure out what NAME refers to. */
23285 if (TREE_CODE (scope) == TYPENAME_TYPE)
23287 if (TYPENAME_IS_RESOLVING_P (scope))
23288 /* Given a class template A with a dependent base with nested type C,
23289 typedef typename A::C::C C will land us here, as trying to resolve
23290 the initial A::C leads to the local C typedef, which leads back to
23291 A::C::C. So we break the recursion now. */
23292 return type;
23293 else
23294 scope = resolve_typename_type (scope, only_current_p);
23296 /* If we don't know what SCOPE refers to, then we cannot resolve the
23297 TYPENAME_TYPE. */
23298 if (TREE_CODE (scope) == TYPENAME_TYPE)
23299 return type;
23300 /* If the SCOPE is a template type parameter, we have no way of
23301 resolving the name. */
23302 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
23303 return type;
23304 /* If the SCOPE is not the current instantiation, there's no reason
23305 to look inside it. */
23306 if (only_current_p && !currently_open_class (scope))
23307 return type;
23308 /* If this is a typedef, we don't want to look inside (c++/11987). */
23309 if (typedef_variant_p (type))
23310 return type;
23311 /* If SCOPE isn't the template itself, it will not have a valid
23312 TYPE_FIELDS list. */
23313 if (CLASS_TYPE_P (scope)
23314 && same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
23315 /* scope is either the template itself or a compatible instantiation
23316 like X<T>, so look up the name in the original template. */
23317 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
23318 else
23319 /* scope is a partial instantiation, so we can't do the lookup or we
23320 will lose the template arguments. */
23321 return type;
23322 /* Enter the SCOPE so that name lookup will be resolved as if we
23323 were in the class definition. In particular, SCOPE will no
23324 longer be considered a dependent type. */
23325 pushed_scope = push_scope (scope);
23326 /* Look up the declaration. */
23327 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
23328 tf_warning_or_error);
23330 result = NULL_TREE;
23332 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23333 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23334 if (!decl)
23335 /*nop*/;
23336 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
23337 && TREE_CODE (decl) == TYPE_DECL)
23339 result = TREE_TYPE (decl);
23340 if (result == error_mark_node)
23341 result = NULL_TREE;
23343 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
23344 && DECL_CLASS_TEMPLATE_P (decl))
23346 tree tmpl;
23347 tree args;
23348 /* Obtain the template and the arguments. */
23349 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
23350 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
23351 /* Instantiate the template. */
23352 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
23353 /*entering_scope=*/0,
23354 tf_error | tf_user);
23355 if (result == error_mark_node)
23356 result = NULL_TREE;
23359 /* Leave the SCOPE. */
23360 if (pushed_scope)
23361 pop_scope (pushed_scope);
23363 /* If we failed to resolve it, return the original typename. */
23364 if (!result)
23365 return type;
23367 /* If lookup found a typename type, resolve that too. */
23368 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
23370 /* Ill-formed programs can cause infinite recursion here, so we
23371 must catch that. */
23372 TYPENAME_IS_RESOLVING_P (type) = 1;
23373 result = resolve_typename_type (result, only_current_p);
23374 TYPENAME_IS_RESOLVING_P (type) = 0;
23377 /* Qualify the resulting type. */
23378 quals = cp_type_quals (type);
23379 if (quals)
23380 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
23382 return result;
23385 /* EXPR is an expression which is not type-dependent. Return a proxy
23386 for EXPR that can be used to compute the types of larger
23387 expressions containing EXPR. */
23389 tree
23390 build_non_dependent_expr (tree expr)
23392 tree inner_expr;
23394 /* Try to get a constant value for all non-dependent expressions in
23395 order to expose bugs in *_dependent_expression_p and constexpr. */
23396 if (flag_checking && cxx_dialect >= cxx11)
23397 fold_non_dependent_expr (expr);
23399 /* Preserve OVERLOADs; the functions must be available to resolve
23400 types. */
23401 inner_expr = expr;
23402 if (TREE_CODE (inner_expr) == STMT_EXPR)
23403 inner_expr = stmt_expr_value_expr (inner_expr);
23404 if (TREE_CODE (inner_expr) == ADDR_EXPR)
23405 inner_expr = TREE_OPERAND (inner_expr, 0);
23406 if (TREE_CODE (inner_expr) == COMPONENT_REF)
23407 inner_expr = TREE_OPERAND (inner_expr, 1);
23408 if (is_overloaded_fn (inner_expr)
23409 || TREE_CODE (inner_expr) == OFFSET_REF)
23410 return expr;
23411 /* There is no need to return a proxy for a variable. */
23412 if (VAR_P (expr))
23413 return expr;
23414 /* Preserve string constants; conversions from string constants to
23415 "char *" are allowed, even though normally a "const char *"
23416 cannot be used to initialize a "char *". */
23417 if (TREE_CODE (expr) == STRING_CST)
23418 return expr;
23419 /* Preserve void and arithmetic constants, as an optimization -- there is no
23420 reason to create a new node. */
23421 if (TREE_CODE (expr) == VOID_CST
23422 || TREE_CODE (expr) == INTEGER_CST
23423 || TREE_CODE (expr) == REAL_CST)
23424 return expr;
23425 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23426 There is at least one place where we want to know that a
23427 particular expression is a throw-expression: when checking a ?:
23428 expression, there are special rules if the second or third
23429 argument is a throw-expression. */
23430 if (TREE_CODE (expr) == THROW_EXPR)
23431 return expr;
23433 /* Don't wrap an initializer list, we need to be able to look inside. */
23434 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
23435 return expr;
23437 /* Don't wrap a dummy object, we need to be able to test for it. */
23438 if (is_dummy_object (expr))
23439 return expr;
23441 if (TREE_CODE (expr) == COND_EXPR)
23442 return build3 (COND_EXPR,
23443 TREE_TYPE (expr),
23444 TREE_OPERAND (expr, 0),
23445 (TREE_OPERAND (expr, 1)
23446 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
23447 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
23448 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
23449 if (TREE_CODE (expr) == COMPOUND_EXPR
23450 && !COMPOUND_EXPR_OVERLOADED (expr))
23451 return build2 (COMPOUND_EXPR,
23452 TREE_TYPE (expr),
23453 TREE_OPERAND (expr, 0),
23454 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
23456 /* If the type is unknown, it can't really be non-dependent */
23457 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
23459 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23460 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
23463 /* ARGS is a vector of expressions as arguments to a function call.
23464 Replace the arguments with equivalent non-dependent expressions.
23465 This modifies ARGS in place. */
23467 void
23468 make_args_non_dependent (vec<tree, va_gc> *args)
23470 unsigned int ix;
23471 tree arg;
23473 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
23475 tree newarg = build_non_dependent_expr (arg);
23476 if (newarg != arg)
23477 (*args)[ix] = newarg;
23481 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23482 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23483 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23485 static tree
23486 make_auto_1 (tree name, bool set_canonical)
23488 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
23489 TYPE_NAME (au) = build_decl (input_location,
23490 TYPE_DECL, name, au);
23491 TYPE_STUB_DECL (au) = TYPE_NAME (au);
23492 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
23493 (0, processing_template_decl + 1, processing_template_decl + 1,
23494 TYPE_NAME (au), NULL_TREE);
23495 if (set_canonical)
23496 TYPE_CANONICAL (au) = canonical_type_parameter (au);
23497 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
23498 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
23500 return au;
23503 tree
23504 make_decltype_auto (void)
23506 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23509 tree
23510 make_auto (void)
23512 return make_auto_1 (get_identifier ("auto"), true);
23515 /* Make a "constrained auto" type-specifier. This is an
23516 auto type with constraints that must be associated after
23517 deduction. The constraint is formed from the given
23518 CONC and its optional sequence of arguments, which are
23519 non-null if written as partial-concept-id. */
23521 tree
23522 make_constrained_auto (tree con, tree args)
23524 tree type = make_auto_1 (get_identifier ("auto"), false);
23526 /* Build the constraint. */
23527 tree tmpl = DECL_TI_TEMPLATE (con);
23528 tree expr;
23529 if (VAR_P (con))
23530 expr = build_concept_check (tmpl, type, args);
23531 else
23532 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
23534 tree constr = make_predicate_constraint (expr);
23535 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
23537 /* Our canonical type depends on the constraint. */
23538 TYPE_CANONICAL (type) = canonical_type_parameter (type);
23540 /* Attach the constraint to the type declaration. */
23541 tree decl = TYPE_NAME (type);
23542 return decl;
23545 /* Given type ARG, return std::initializer_list<ARG>. */
23547 static tree
23548 listify (tree arg)
23550 tree std_init_list = namespace_binding
23551 (get_identifier ("initializer_list"), std_node);
23552 tree argvec;
23553 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
23555 error ("deducing from brace-enclosed initializer list requires "
23556 "#include <initializer_list>");
23557 return error_mark_node;
23559 argvec = make_tree_vec (1);
23560 TREE_VEC_ELT (argvec, 0) = arg;
23561 return lookup_template_class (std_init_list, argvec, NULL_TREE,
23562 NULL_TREE, 0, tf_warning_or_error);
23565 /* Replace auto in TYPE with std::initializer_list<auto>. */
23567 static tree
23568 listify_autos (tree type, tree auto_node)
23570 tree init_auto = listify (auto_node);
23571 tree argvec = make_tree_vec (1);
23572 TREE_VEC_ELT (argvec, 0) = init_auto;
23573 if (processing_template_decl)
23574 argvec = add_to_template_args (current_template_args (), argvec);
23575 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
23578 /* Hash traits for hashing possibly constrained 'auto'
23579 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23581 struct auto_hash : default_hash_traits<tree>
23583 static inline hashval_t hash (tree);
23584 static inline bool equal (tree, tree);
23587 /* Hash the 'auto' T. */
23589 inline hashval_t
23590 auto_hash::hash (tree t)
23592 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
23593 /* Matching constrained-type-specifiers denote the same template
23594 parameter, so hash the constraint. */
23595 return hash_placeholder_constraint (c);
23596 else
23597 /* But unconstrained autos are all separate, so just hash the pointer. */
23598 return iterative_hash_object (t, 0);
23601 /* Compare two 'auto's. */
23603 inline bool
23604 auto_hash::equal (tree t1, tree t2)
23606 if (t1 == t2)
23607 return true;
23609 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
23610 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
23612 /* Two unconstrained autos are distinct. */
23613 if (!c1 || !c2)
23614 return false;
23616 return equivalent_placeholder_constraints (c1, c2);
23619 /* for_each_template_parm callback for extract_autos: if t is a (possibly
23620 constrained) auto, add it to the vector. */
23622 static int
23623 extract_autos_r (tree t, void *data)
23625 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
23626 if (is_auto_or_concept (t))
23628 /* All the autos were built with index 0; fix that up now. */
23629 tree *p = hash.find_slot (t, INSERT);
23630 unsigned idx;
23631 if (*p)
23632 /* If this is a repeated constrained-type-specifier, use the index we
23633 chose before. */
23634 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
23635 else
23637 /* Otherwise this is new, so use the current count. */
23638 *p = t;
23639 idx = hash.elements () - 1;
23641 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
23644 /* Always keep walking. */
23645 return 0;
23648 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
23649 says they can appear anywhere in the type. */
23651 static tree
23652 extract_autos (tree type)
23654 hash_set<tree> visited;
23655 hash_table<auto_hash> hash (2);
23657 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
23659 tree tree_vec = make_tree_vec (hash.elements());
23660 for (hash_table<auto_hash>::iterator iter = hash.begin();
23661 iter != hash.end(); ++iter)
23663 tree elt = *iter;
23664 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
23665 TREE_VEC_ELT (tree_vec, i)
23666 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
23669 return tree_vec;
23672 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23673 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
23675 tree
23676 do_auto_deduction (tree type, tree init, tree auto_node)
23678 return do_auto_deduction (type, init, auto_node,
23679 tf_warning_or_error,
23680 adc_unspecified);
23683 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23684 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
23685 The CONTEXT determines the context in which auto deduction is performed
23686 and is used to control error diagnostics. */
23688 tree
23689 do_auto_deduction (tree type, tree init, tree auto_node,
23690 tsubst_flags_t complain, auto_deduction_context context)
23692 tree targs;
23694 if (init == error_mark_node)
23695 return error_mark_node;
23697 if (type_dependent_expression_p (init))
23698 /* Defining a subset of type-dependent expressions that we can deduce
23699 from ahead of time isn't worth the trouble. */
23700 return type;
23702 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
23703 with either a new invented type template parameter U or, if the
23704 initializer is a braced-init-list (8.5.4), with
23705 std::initializer_list<U>. */
23706 if (BRACE_ENCLOSED_INITIALIZER_P (init))
23708 if (!DIRECT_LIST_INIT_P (init))
23709 type = listify_autos (type, auto_node);
23710 else if (CONSTRUCTOR_NELTS (init) == 1)
23711 init = CONSTRUCTOR_ELT (init, 0)->value;
23712 else
23714 if (complain & tf_warning_or_error)
23716 if (permerror (input_location, "direct-list-initialization of "
23717 "%<auto%> requires exactly one element"))
23718 inform (input_location,
23719 "for deduction to %<std::initializer_list%>, use copy-"
23720 "list-initialization (i.e. add %<=%> before the %<{%>)");
23722 type = listify_autos (type, auto_node);
23726 if (type == error_mark_node)
23727 return error_mark_node;
23729 init = resolve_nondeduced_context (init);
23731 if (AUTO_IS_DECLTYPE (auto_node))
23733 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
23734 && !REF_PARENTHESIZED_P (init)));
23735 targs = make_tree_vec (1);
23736 TREE_VEC_ELT (targs, 0)
23737 = finish_decltype_type (init, id, tf_warning_or_error);
23738 if (type != auto_node)
23740 if (complain & tf_error)
23741 error ("%qT as type rather than plain %<decltype(auto)%>", type);
23742 return error_mark_node;
23745 else
23747 tree parms = build_tree_list (NULL_TREE, type);
23748 tree tparms;
23750 if (flag_concepts)
23751 tparms = extract_autos (type);
23752 else
23754 tparms = make_tree_vec (1);
23755 TREE_VEC_ELT (tparms, 0)
23756 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
23759 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
23760 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
23761 DEDUCE_CALL, LOOKUP_NORMAL,
23762 NULL, /*explain_p=*/false);
23763 if (val > 0)
23765 if (processing_template_decl)
23766 /* Try again at instantiation time. */
23767 return type;
23768 if (type && type != error_mark_node
23769 && (complain & tf_error))
23770 /* If type is error_mark_node a diagnostic must have been
23771 emitted by now. Also, having a mention to '<type error>'
23772 in the diagnostic is not really useful to the user. */
23774 if (cfun && auto_node == current_function_auto_return_pattern
23775 && LAMBDA_FUNCTION_P (current_function_decl))
23776 error ("unable to deduce lambda return type from %qE", init);
23777 else
23778 error ("unable to deduce %qT from %qE", type, init);
23779 type_unification_real (tparms, targs, parms, &init, 1, 0,
23780 DEDUCE_CALL, LOOKUP_NORMAL,
23781 NULL, /*explain_p=*/true);
23783 return error_mark_node;
23787 /* Check any placeholder constraints against the deduced type. */
23788 if (flag_concepts && !processing_template_decl)
23789 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
23791 /* Use the deduced type to check the associated constraints. */
23792 if (!constraints_satisfied_p (constr, targs))
23794 if (complain & tf_warning_or_error)
23796 switch (context)
23798 case adc_unspecified:
23799 error("placeholder constraints not satisfied");
23800 break;
23801 case adc_variable_type:
23802 error ("deduced initializer does not satisfy "
23803 "placeholder constraints");
23804 break;
23805 case adc_return_type:
23806 error ("deduced return type does not satisfy "
23807 "placeholder constraints");
23808 break;
23809 case adc_requirement:
23810 error ("deduced expression type does not saatisy "
23811 "placeholder constraints");
23812 break;
23814 diagnose_constraints (input_location, constr, targs);
23816 return error_mark_node;
23820 if (processing_template_decl)
23821 targs = add_to_template_args (current_template_args (), targs);
23822 return tsubst (type, targs, complain, NULL_TREE);
23825 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
23826 result. */
23828 tree
23829 splice_late_return_type (tree type, tree late_return_type)
23831 if (is_auto (type))
23833 if (late_return_type)
23834 return late_return_type;
23836 tree idx = get_template_parm_index (type);
23837 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
23838 /* In an abbreviated function template we didn't know we were dealing
23839 with a function template when we saw the auto return type, so update
23840 it to have the correct level. */
23841 return make_auto_1 (TYPE_IDENTIFIER (type), true);
23843 return type;
23846 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
23847 'decltype(auto)'. */
23849 bool
23850 is_auto (const_tree type)
23852 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23853 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
23854 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
23855 return true;
23856 else
23857 return false;
23860 /* for_each_template_parm callback for type_uses_auto. */
23863 is_auto_r (tree tp, void */*data*/)
23865 return is_auto_or_concept (tp);
23868 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
23869 a use of `auto'. Returns NULL_TREE otherwise. */
23871 tree
23872 type_uses_auto (tree type)
23874 if (type == NULL_TREE)
23875 return NULL_TREE;
23876 else if (flag_concepts)
23878 /* The Concepts TS allows multiple autos in one type-specifier; just
23879 return the first one we find, do_auto_deduction will collect all of
23880 them. */
23881 if (uses_template_parms (type))
23882 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
23883 /*visited*/NULL, /*nondeduced*/true);
23884 else
23885 return NULL_TREE;
23887 else
23888 return find_type_usage (type, is_auto);
23891 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
23892 'decltype(auto)' or a concept. */
23894 bool
23895 is_auto_or_concept (const_tree type)
23897 return is_auto (type); // or concept
23900 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
23901 a concept identifier) iff TYPE contains a use of a generic type. Returns
23902 NULL_TREE otherwise. */
23904 tree
23905 type_uses_auto_or_concept (tree type)
23907 return find_type_usage (type, is_auto_or_concept);
23911 /* For a given template T, return the vector of typedefs referenced
23912 in T for which access check is needed at T instantiation time.
23913 T is either a FUNCTION_DECL or a RECORD_TYPE.
23914 Those typedefs were added to T by the function
23915 append_type_to_template_for_access_check. */
23917 vec<qualified_typedef_usage_t, va_gc> *
23918 get_types_needing_access_check (tree t)
23920 tree ti;
23921 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
23923 if (!t || t == error_mark_node)
23924 return NULL;
23926 if (!(ti = get_template_info (t)))
23927 return NULL;
23929 if (CLASS_TYPE_P (t)
23930 || TREE_CODE (t) == FUNCTION_DECL)
23932 if (!TI_TEMPLATE (ti))
23933 return NULL;
23935 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
23938 return result;
23941 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
23942 tied to T. That list of typedefs will be access checked at
23943 T instantiation time.
23944 T is either a FUNCTION_DECL or a RECORD_TYPE.
23945 TYPE_DECL is a TYPE_DECL node representing a typedef.
23946 SCOPE is the scope through which TYPE_DECL is accessed.
23947 LOCATION is the location of the usage point of TYPE_DECL.
23949 This function is a subroutine of
23950 append_type_to_template_for_access_check. */
23952 static void
23953 append_type_to_template_for_access_check_1 (tree t,
23954 tree type_decl,
23955 tree scope,
23956 location_t location)
23958 qualified_typedef_usage_t typedef_usage;
23959 tree ti;
23961 if (!t || t == error_mark_node)
23962 return;
23964 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
23965 || CLASS_TYPE_P (t))
23966 && type_decl
23967 && TREE_CODE (type_decl) == TYPE_DECL
23968 && scope);
23970 if (!(ti = get_template_info (t)))
23971 return;
23973 gcc_assert (TI_TEMPLATE (ti));
23975 typedef_usage.typedef_decl = type_decl;
23976 typedef_usage.context = scope;
23977 typedef_usage.locus = location;
23979 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
23982 /* Append TYPE_DECL to the template TEMPL.
23983 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
23984 At TEMPL instanciation time, TYPE_DECL will be checked to see
23985 if it can be accessed through SCOPE.
23986 LOCATION is the location of the usage point of TYPE_DECL.
23988 e.g. consider the following code snippet:
23990 class C
23992 typedef int myint;
23995 template<class U> struct S
23997 C::myint mi; // <-- usage point of the typedef C::myint
24000 S<char> s;
24002 At S<char> instantiation time, we need to check the access of C::myint
24003 In other words, we need to check the access of the myint typedef through
24004 the C scope. For that purpose, this function will add the myint typedef
24005 and the scope C through which its being accessed to a list of typedefs
24006 tied to the template S. That list will be walked at template instantiation
24007 time and access check performed on each typedefs it contains.
24008 Note that this particular code snippet should yield an error because
24009 myint is private to C. */
24011 void
24012 append_type_to_template_for_access_check (tree templ,
24013 tree type_decl,
24014 tree scope,
24015 location_t location)
24017 qualified_typedef_usage_t *iter;
24018 unsigned i;
24020 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
24022 /* Make sure we don't append the type to the template twice. */
24023 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
24024 if (iter->typedef_decl == type_decl && scope == iter->context)
24025 return;
24027 append_type_to_template_for_access_check_1 (templ, type_decl,
24028 scope, location);
24031 /* Convert the generic type parameters in PARM that match the types given in the
24032 range [START_IDX, END_IDX) from the current_template_parms into generic type
24033 packs. */
24035 tree
24036 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
24038 tree current = current_template_parms;
24039 int depth = TMPL_PARMS_DEPTH (current);
24040 current = INNERMOST_TEMPLATE_PARMS (current);
24041 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
24043 for (int i = 0; i < start_idx; ++i)
24044 TREE_VEC_ELT (replacement, i)
24045 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24047 for (int i = start_idx; i < end_idx; ++i)
24049 /* Create a distinct parameter pack type from the current parm and add it
24050 to the replacement args to tsubst below into the generic function
24051 parameter. */
24053 tree o = TREE_TYPE (TREE_VALUE
24054 (TREE_VEC_ELT (current, i)));
24055 tree t = copy_type (o);
24056 TEMPLATE_TYPE_PARM_INDEX (t)
24057 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
24058 o, 0, 0, tf_none);
24059 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
24060 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
24061 TYPE_MAIN_VARIANT (t) = t;
24062 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
24063 TYPE_CANONICAL (t) = canonical_type_parameter (t);
24064 TREE_VEC_ELT (replacement, i) = t;
24065 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
24068 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
24069 TREE_VEC_ELT (replacement, i)
24070 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24072 /* If there are more levels then build up the replacement with the outer
24073 template parms. */
24074 if (depth > 1)
24075 replacement = add_to_template_args (template_parms_to_args
24076 (TREE_CHAIN (current_template_parms)),
24077 replacement);
24079 return tsubst (parm, replacement, tf_none, NULL_TREE);
24082 /* Entries in the decl_constraint hash table. */
24083 struct GTY((for_user)) constr_entry
24085 tree decl;
24086 tree ci;
24089 /* Hashing function and equality for constraint entries. */
24090 struct constr_hasher : ggc_ptr_hash<constr_entry>
24092 static hashval_t hash (constr_entry *e)
24094 return (hashval_t)DECL_UID (e->decl);
24097 static bool equal (constr_entry *e1, constr_entry *e2)
24099 return e1->decl == e2->decl;
24103 /* A mapping from declarations to constraint information. Note that
24104 both templates and their underlying declarations are mapped to the
24105 same constraint information.
24107 FIXME: This is defined in pt.c because garbage collection
24108 code is not being generated for constraint.cc. */
24110 static GTY (()) hash_table<constr_hasher> *decl_constraints;
24112 /* Returns true iff cinfo contains a valid set of constraints.
24113 This is the case when the associated requirements have been
24114 successfully decomposed into lists of atomic constraints.
24115 That is, when the saved assumptions are not error_mark_node. */
24117 bool
24118 valid_constraints_p (tree cinfo)
24120 gcc_assert (cinfo);
24121 return CI_ASSUMPTIONS (cinfo) != error_mark_node;
24124 /* Returns the template constraints of declaration T. If T is not
24125 constrained, return NULL_TREE. Note that T must be non-null. */
24127 tree
24128 get_constraints (tree t)
24130 gcc_assert (DECL_P (t));
24131 if (TREE_CODE (t) == TEMPLATE_DECL)
24132 t = DECL_TEMPLATE_RESULT (t);
24133 constr_entry elt = { t, NULL_TREE };
24134 constr_entry* found = decl_constraints->find (&elt);
24135 if (found)
24136 return found->ci;
24137 else
24138 return NULL_TREE;
24141 /* Associate the given constraint information CI with the declaration
24142 T. If T is a template, then the constraints are associated with
24143 its underlying declaration. Don't build associations if CI is
24144 NULL_TREE. */
24146 void
24147 set_constraints (tree t, tree ci)
24149 if (!ci)
24150 return;
24151 gcc_assert (t);
24152 if (TREE_CODE (t) == TEMPLATE_DECL)
24153 t = DECL_TEMPLATE_RESULT (t);
24154 gcc_assert (!get_constraints (t));
24155 constr_entry elt = {t, ci};
24156 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
24157 constr_entry* entry = ggc_alloc<constr_entry> ();
24158 *entry = elt;
24159 *slot = entry;
24162 /* Remove the associated constraints of the declaration T. */
24164 void
24165 remove_constraints (tree t)
24167 gcc_assert (DECL_P (t));
24168 if (TREE_CODE (t) == TEMPLATE_DECL)
24169 t = DECL_TEMPLATE_RESULT (t);
24171 constr_entry elt = {t, NULL_TREE};
24172 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
24173 if (slot)
24174 decl_constraints->clear_slot (slot);
24177 /* Set up the hash table for constraint association. */
24179 void
24180 init_constraint_processing (void)
24182 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
24185 /* Set up the hash tables for template instantiations. */
24187 void
24188 init_template_processing (void)
24190 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
24191 type_specializations = hash_table<spec_hasher>::create_ggc (37);
24194 /* Print stats about the template hash tables for -fstats. */
24196 void
24197 print_template_statistics (void)
24199 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
24200 "%f collisions\n", (long) decl_specializations->size (),
24201 (long) decl_specializations->elements (),
24202 decl_specializations->collisions ());
24203 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
24204 "%f collisions\n", (long) type_specializations->size (),
24205 (long) type_specializations->elements (),
24206 type_specializations->collisions ());
24209 #include "gt-cp-pt.h"