Reverting merge from trunk
[official-gcc.git] / gcc / cp / pt.c
blob43e6e706f20a2eee424cdbd1e3a3a9fff081ef8c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "intl.h"
33 #include "pointer-set.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "toplev.h"
42 #include "timevar.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
45 #include "gimple.h"
46 #include "gimplify.h"
48 /* The type of functions taking a tree, and some additional data, and
49 returning an int. */
50 typedef int (*tree_fn_t) (tree, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY ((chain_next ("%h.next"))) pending_template {
56 struct pending_template *next;
57 struct tinst_level *tinst;
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static vec<int> inline_parm_levels;
69 static GTY(()) struct tinst_level *current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static struct pointer_map_t *local_specializations;
83 // -------------------------------------------------------------------------- //
84 // Local Specialization Stack
86 // Implementation of the RAII helper for creating new local
87 // specializations.
88 local_specialization_stack::local_specialization_stack ()
89 : saved (local_specializations)
91 local_specializations = pointer_map_create ();
94 local_specialization_stack::~local_specialization_stack ()
96 pointer_map_destroy (local_specializations);
97 local_specializations = saved;
100 /* True if we've recursed into fn_type_unification too many times. */
101 static bool excessive_deduction_depth;
103 typedef struct GTY(()) spec_entry
105 tree tmpl;
106 tree args;
107 tree spec;
108 } spec_entry;
110 static GTY ((param_is (spec_entry)))
111 htab_t decl_specializations;
113 static GTY ((param_is (spec_entry)))
114 htab_t type_specializations;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result {
132 tbr_incomplete_type,
133 tbr_ambiguous_baseclass,
134 tbr_success
137 static void push_access_scope (tree);
138 static void pop_access_scope (tree);
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_class_bindings (tree, tree, tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static tree add_outermost_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t, int,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static int for_each_template_parm (tree, tree_fn_t, void*,
169 struct pointer_set_t*, bool);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static hashval_t hash_specialization (const void *p);
175 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
176 static int mark_template_parm (tree, void *);
177 static int template_parm_this_level_p (tree, void *);
178 static tree tsubst_friend_function (tree, tree);
179 static tree tsubst_friend_class (tree, tree);
180 static int can_complete_type_without_circularity (tree);
181 static tree get_bindings (tree, tree, tree, bool);
182 static int template_decl_level (tree);
183 static int check_cv_quals_for_unify (int, tree, tree);
184 static void template_parm_level_and_index (tree, int*, int*);
185 static int unify_pack_expansion (tree, tree, tree,
186 tree, unification_kind_t, bool, bool);
187 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
190 static void regenerate_decl_from_template (tree, tree);
191 static tree most_specialized_class (tree, tree, tsubst_flags_t);
192 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
193 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
194 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
195 static bool check_specialization_scope (void);
196 static tree process_partial_specialization (tree);
197 static void set_current_access_from_decl (tree);
198 static enum template_base_result get_template_base (tree, tree, tree, tree,
199 bool , tree *);
200 static tree try_class_unification (tree, tree, tree, tree, bool);
201 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
202 tree, tree);
203 static bool template_template_parm_bindings_ok_p (tree, tree);
204 static int template_args_equal (tree, tree);
205 static void tsubst_default_arguments (tree, tsubst_flags_t);
206 static tree for_each_template_parm_r (tree *, int *, void *);
207 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
208 static void copy_default_args_to_explicit_spec (tree);
209 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
210 static bool dependent_template_arg_p (tree);
211 static bool any_template_arguments_need_structural_equality_p (tree);
212 static bool dependent_type_p_r (tree);
213 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
214 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
224 /* Make the current scope suitable for access checking when we are
225 processing T. T can be FUNCTION_DECL for instantiated function
226 template, VAR_DECL for static member variable, or TYPE_DECL for
227 alias template (needed by instantiate_decl). */
229 static void
230 push_access_scope (tree t)
232 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
233 || TREE_CODE (t) == TYPE_DECL);
235 if (DECL_FRIEND_CONTEXT (t))
236 push_nested_class (DECL_FRIEND_CONTEXT (t));
237 else if (DECL_CLASS_SCOPE_P (t))
238 push_nested_class (DECL_CONTEXT (t));
239 else
240 push_to_top_level ();
242 if (TREE_CODE (t) == FUNCTION_DECL)
244 saved_access_scope = tree_cons
245 (NULL_TREE, current_function_decl, saved_access_scope);
246 current_function_decl = t;
250 /* Restore the scope set up by push_access_scope. T is the node we
251 are processing. */
253 static void
254 pop_access_scope (tree t)
256 if (TREE_CODE (t) == FUNCTION_DECL)
258 current_function_decl = TREE_VALUE (saved_access_scope);
259 saved_access_scope = TREE_CHAIN (saved_access_scope);
262 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
263 pop_nested_class ();
264 else
265 pop_from_top_level ();
268 /* Do any processing required when DECL (a member template
269 declaration) is finished. Returns the TEMPLATE_DECL corresponding
270 to DECL, unless it is a specialization, in which case the DECL
271 itself is returned. */
273 tree
274 finish_member_template_decl (tree decl)
276 if (decl == error_mark_node)
277 return error_mark_node;
279 gcc_assert (DECL_P (decl));
281 if (TREE_CODE (decl) == TYPE_DECL)
283 tree type;
285 type = TREE_TYPE (decl);
286 if (type == error_mark_node)
287 return error_mark_node;
288 if (MAYBE_CLASS_TYPE_P (type)
289 && CLASSTYPE_TEMPLATE_INFO (type)
290 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
292 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
293 check_member_template (tmpl);
294 return tmpl;
296 return NULL_TREE;
298 else if (TREE_CODE (decl) == FIELD_DECL)
299 error ("data member %qD cannot be a member template", decl);
300 else if (DECL_TEMPLATE_INFO (decl))
302 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
304 check_member_template (DECL_TI_TEMPLATE (decl));
305 return DECL_TI_TEMPLATE (decl);
307 else
308 return decl;
310 else
311 error ("invalid member template declaration %qD", decl);
313 return error_mark_node;
316 /* Create a template info node. */
318 tree
319 build_template_info (tree template_decl, tree template_args)
321 tree result = make_node (TEMPLATE_INFO);
322 TI_TEMPLATE (result) = template_decl;
323 TI_ARGS (result) = template_args;
324 return result;
327 /* Return the template info node corresponding to T, whatever T is. */
329 tree
330 get_template_info (const_tree t)
332 tree tinfo = NULL_TREE;
334 if (!t || t == error_mark_node)
335 return NULL;
337 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
338 tinfo = DECL_TEMPLATE_INFO (t);
340 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
341 t = TREE_TYPE (t);
343 if (OVERLOAD_TYPE_P (t))
344 tinfo = TYPE_TEMPLATE_INFO (t);
345 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
346 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
348 return tinfo;
351 /* Returns the template nesting level of the indicated class TYPE.
353 For example, in:
354 template <class T>
355 struct A
357 template <class U>
358 struct B {};
361 A<T>::B<U> has depth two, while A<T> has depth one.
362 Both A<T>::B<int> and A<int>::B<U> have depth one, if
363 they are instantiations, not specializations.
365 This function is guaranteed to return 0 if passed NULL_TREE so
366 that, for example, `template_class_depth (current_class_type)' is
367 always safe. */
370 template_class_depth (tree type)
372 int depth;
374 for (depth = 0;
375 type && TREE_CODE (type) != NAMESPACE_DECL;
376 type = (TREE_CODE (type) == FUNCTION_DECL)
377 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
379 tree tinfo = get_template_info (type);
381 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
382 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
383 ++depth;
386 return depth;
389 /* Subroutine of maybe_begin_member_template_processing.
390 Returns true if processing DECL needs us to push template parms. */
392 static bool
393 inline_needs_template_parms (tree decl, bool nsdmi)
395 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
396 return false;
398 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
399 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
402 /* Subroutine of maybe_begin_member_template_processing.
403 Push the template parms in PARMS, starting from LEVELS steps into the
404 chain, and ending at the beginning, since template parms are listed
405 innermost first. */
407 static void
408 push_inline_template_parms_recursive (tree parmlist, int levels)
410 tree parms = TREE_VALUE (parmlist);
411 int i;
413 if (levels > 1)
414 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
416 ++processing_template_decl;
417 current_template_parms
418 = tree_cons (size_int (processing_template_decl),
419 parms, current_template_parms);
420 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
422 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
423 NULL);
424 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
426 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
428 if (parm == error_mark_node)
429 continue;
431 gcc_assert (DECL_P (parm));
433 switch (TREE_CODE (parm))
435 case TYPE_DECL:
436 case TEMPLATE_DECL:
437 pushdecl (parm);
438 break;
440 case PARM_DECL:
442 /* Make a CONST_DECL as is done in process_template_parm.
443 It is ugly that we recreate this here; the original
444 version built in process_template_parm is no longer
445 available. */
446 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
447 CONST_DECL, DECL_NAME (parm),
448 TREE_TYPE (parm));
449 DECL_ARTIFICIAL (decl) = 1;
450 TREE_CONSTANT (decl) = 1;
451 TREE_READONLY (decl) = 1;
452 DECL_INITIAL (decl) = DECL_INITIAL (parm);
453 SET_DECL_TEMPLATE_PARM_P (decl);
454 pushdecl (decl);
456 break;
458 default:
459 gcc_unreachable ();
464 /* Restore the template parameter context for a member template, a
465 friend template defined in a class definition, or a non-template
466 member of template class. */
468 void
469 maybe_begin_member_template_processing (tree decl)
471 tree parms;
472 int levels = 0;
473 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
475 if (nsdmi)
476 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
477 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
478 : NULL_TREE);
480 if (inline_needs_template_parms (decl, nsdmi))
482 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
483 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
485 if (DECL_TEMPLATE_SPECIALIZATION (decl))
487 --levels;
488 parms = TREE_CHAIN (parms);
491 push_inline_template_parms_recursive (parms, levels);
494 /* Remember how many levels of template parameters we pushed so that
495 we can pop them later. */
496 inline_parm_levels.safe_push (levels);
499 /* Undo the effects of maybe_begin_member_template_processing. */
501 void
502 maybe_end_member_template_processing (void)
504 int i;
505 int last;
507 if (inline_parm_levels.length () == 0)
508 return;
510 last = inline_parm_levels.pop ();
511 for (i = 0; i < last; ++i)
513 --processing_template_decl;
514 current_template_parms = TREE_CHAIN (current_template_parms);
515 poplevel (0, 0, 0);
519 /* Return a new template argument vector which contains all of ARGS,
520 but has as its innermost set of arguments the EXTRA_ARGS. */
522 static tree
523 add_to_template_args (tree args, tree extra_args)
525 tree new_args;
526 int extra_depth;
527 int i;
528 int j;
530 if (args == NULL_TREE || extra_args == error_mark_node)
531 return extra_args;
533 extra_depth = TMPL_ARGS_DEPTH (extra_args);
534 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
536 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
537 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
539 for (j = 1; j <= extra_depth; ++j, ++i)
540 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
542 return new_args;
545 /* Like add_to_template_args, but only the outermost ARGS are added to
546 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
547 (EXTRA_ARGS) levels are added. This function is used to combine
548 the template arguments from a partial instantiation with the
549 template arguments used to attain the full instantiation from the
550 partial instantiation. */
552 static tree
553 add_outermost_template_args (tree args, tree extra_args)
555 tree new_args;
557 /* If there are more levels of EXTRA_ARGS than there are ARGS,
558 something very fishy is going on. */
559 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
561 /* If *all* the new arguments will be the EXTRA_ARGS, just return
562 them. */
563 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
564 return extra_args;
566 /* For the moment, we make ARGS look like it contains fewer levels. */
567 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
569 new_args = add_to_template_args (args, extra_args);
571 /* Now, we restore ARGS to its full dimensions. */
572 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
574 return new_args;
577 /* Return the N levels of innermost template arguments from the ARGS. */
579 tree
580 get_innermost_template_args (tree args, int n)
582 tree new_args;
583 int extra_levels;
584 int i;
586 gcc_assert (n >= 0);
588 /* If N is 1, just return the innermost set of template arguments. */
589 if (n == 1)
590 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
592 /* If we're not removing anything, just return the arguments we were
593 given. */
594 extra_levels = TMPL_ARGS_DEPTH (args) - n;
595 gcc_assert (extra_levels >= 0);
596 if (extra_levels == 0)
597 return args;
599 /* Make a new set of arguments, not containing the outer arguments. */
600 new_args = make_tree_vec (n);
601 for (i = 1; i <= n; ++i)
602 SET_TMPL_ARGS_LEVEL (new_args, i,
603 TMPL_ARGS_LEVEL (args, i + extra_levels));
605 return new_args;
608 /* The inverse of get_innermost_template_args: Return all but the innermost
609 EXTRA_LEVELS levels of template arguments from the ARGS. */
611 static tree
612 strip_innermost_template_args (tree args, int extra_levels)
614 tree new_args;
615 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
616 int i;
618 gcc_assert (n >= 0);
620 /* If N is 1, just return the outermost set of template arguments. */
621 if (n == 1)
622 return TMPL_ARGS_LEVEL (args, 1);
624 /* If we're not removing anything, just return the arguments we were
625 given. */
626 gcc_assert (extra_levels >= 0);
627 if (extra_levels == 0)
628 return args;
630 /* Make a new set of arguments, not containing the inner arguments. */
631 new_args = make_tree_vec (n);
632 for (i = 1; i <= n; ++i)
633 SET_TMPL_ARGS_LEVEL (new_args, i,
634 TMPL_ARGS_LEVEL (args, i));
636 return new_args;
639 /* We've got a template header coming up; push to a new level for storing
640 the parms. */
642 void
643 begin_template_parm_list (void)
645 /* We use a non-tag-transparent scope here, which causes pushtag to
646 put tags in this scope, rather than in the enclosing class or
647 namespace scope. This is the right thing, since we want
648 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
649 global template class, push_template_decl handles putting the
650 TEMPLATE_DECL into top-level scope. For a nested template class,
651 e.g.:
653 template <class T> struct S1 {
654 template <class T> struct S2 {};
657 pushtag contains special code to call pushdecl_with_scope on the
658 TEMPLATE_DECL for S2. */
659 begin_scope (sk_template_parms, NULL);
660 ++processing_template_decl;
661 ++processing_template_parmlist;
662 note_template_header (0);
665 /* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
669 static bool
670 check_specialization_scope (void)
672 tree scope = current_scope ();
674 /* [temp.expl.spec]
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
682 is a member. */
683 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
685 error ("explicit specialization in non-namespace scope %qD", scope);
686 return false;
689 /* [temp.expl.spec]
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms)
699 error ("enclosing class templates are not explicitly specialized");
700 return false;
703 return true;
706 /* We've just seen template <>. */
708 bool
709 begin_specialization (void)
711 begin_scope (sk_template_spec, NULL);
712 note_template_header (1);
713 return check_specialization_scope ();
716 /* Called at then end of processing a declaration preceded by
717 template<>. */
719 void
720 end_specialization (void)
722 finish_scope ();
723 reset_specialization ();
726 /* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
729 void
730 reset_specialization (void)
732 processing_specialization = 0;
733 template_header_count = 0;
736 /* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
739 static void
740 note_template_header (int specialization)
742 processing_specialization = specialization;
743 template_header_count++;
746 /* We're beginning an explicit instantiation. */
748 void
749 begin_explicit_instantiation (void)
751 gcc_assert (!processing_explicit_instantiation);
752 processing_explicit_instantiation = true;
756 void
757 end_explicit_instantiation (void)
759 gcc_assert (processing_explicit_instantiation);
760 processing_explicit_instantiation = false;
763 /* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
768 static bool
769 check_specialization_namespace (tree tmpl)
771 tree tpl_ns = decl_namespace_context (tmpl);
773 /* [tmpl.expl.spec]
775 An explicit specialization shall be declared in the namespace of
776 which the template is a member, or, for member templates, in the
777 namespace of which the enclosing class or enclosing class
778 template is a member. An explicit specialization of a member
779 function, member class or static data member of a class template
780 shall be declared in the namespace of which the class template is
781 a member. */
782 if (current_scope() != DECL_CONTEXT (tmpl)
783 && !at_namespace_scope_p ())
785 error ("specialization of %qD must appear at namespace scope", tmpl);
786 return false;
788 if (is_associated_namespace (current_namespace, tpl_ns))
789 /* Same or super-using namespace. */
790 return true;
791 else
793 permerror (input_location, "specialization of %qD in different namespace", tmpl);
794 permerror (input_location, " from definition of %q+#D", tmpl);
795 return false;
799 /* SPEC is an explicit instantiation. Check that it is valid to
800 perform this explicit instantiation in the current namespace. */
802 static void
803 check_explicit_instantiation_namespace (tree spec)
805 tree ns;
807 /* DR 275: An explicit instantiation shall appear in an enclosing
808 namespace of its template. */
809 ns = decl_namespace_context (spec);
810 if (!is_ancestor (current_namespace, ns))
811 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
812 "(which does not enclose namespace %qD)",
813 spec, current_namespace, ns);
816 // Returns the TEMPLATE_DECL that defines the partial template specialization
817 // TYPE. If TYPE is not found in the list of partial specializations, then
818 // it must be an explicit specialization.
819 static tree
820 get_specializing_template_decl (tree type)
822 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
823 tree decl = TYPE_MAIN_DECL (type);
824 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
825 while (specs)
827 tree spec = TREE_VALUE (specs);
828 if (DECL_TEMPLATE_RESULT (spec) == decl)
829 return spec;
830 specs = TREE_CHAIN (specs);
832 return NULL_TREE;
835 // Return the template constraints for the template specialization TYPE.
836 static inline tree
837 get_specialization_constraints (tree type)
839 // If TYPE is a partial specialization, return the constraints for
840 // that type. If it is an explicit specialization, return null since
841 // non-templates cannot be constrained.
842 if (tree d = get_specializing_template_decl (type))
843 return DECL_CONSTRAINTS (d);
844 else
845 return NULL_TREE;
848 // Returns the type of a template specialization only if that
849 // specialization needs to defined. Otherwise (e.g., if the type has
850 // already been defined), the function returns NULL_TREE.
851 static tree
852 maybe_new_partial_specialization (tree type)
854 // An implicit instantiation of an incomplete type implies
855 // the definition of a new class template.
857 // template<typename T>
858 // struct S;
860 // template<typename T>
861 // struct S<T*>;
863 // Here, S<T*> is an implicit instantiation of S whose type
864 // is incomplete.
865 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
866 return type;
868 // It can also be the case that is a completed specialization.
869 // Continuing the previous example, suppose we also declare:
871 // template<typename T>
872 // requires Integral<T>
873 // struct S<T*>;
875 // Here, S<T*> refers to the specialization S<T*> defined
876 // above. However, we need to differentiate definitions because
877 // we intend to define a new partial specialization. In this case,
878 // we rely on the fact that the constraints are different for
879 // this declaration than that above.
881 // Note that we also get here for injected class names and
882 // late-parsed template definitions. We must ensure that we
883 // do not create new type declarations for those cases.
884 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
886 tree constr = get_specialization_constraints (type);
887 if (!equivalent_constraints (current_template_reqs, constr))
889 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
890 tree args = CLASSTYPE_TI_ARGS (type);
892 // Create a new type node (and corresponding type decl) that
893 // for the newly declared specialization.
894 tree t = make_class_type (TREE_CODE (type));
895 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
896 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
897 TYPE_CANONICAL (t) = t;
898 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
899 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
901 // Build the corresponding type decl.
902 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
903 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
904 DECL_SOURCE_LOCATION (d) = input_location;
906 return t;
909 return NULL;
912 /* The TYPE is being declared. If it is a template type, that means it
913 is a partial specialization. Do appropriate error-checking. */
915 tree
916 maybe_process_partial_specialization (tree type)
918 tree context;
920 if (type == error_mark_node)
921 return error_mark_node;
923 /* A lambda that appears in specialization context is not itself a
924 specialization. */
925 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
926 return type;
928 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
930 error ("name of class shadows template template parameter %qD",
931 TYPE_NAME (type));
932 return error_mark_node;
935 context = TYPE_CONTEXT (type);
937 if (TYPE_ALIAS_P (type))
939 if (TYPE_TEMPLATE_INFO (type)
940 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
941 error ("specialization of alias template %qD",
942 TYPE_TI_TEMPLATE (type));
943 else
944 error ("explicit specialization of non-template %qT", type);
945 return error_mark_node;
947 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
949 /* This is for ordinary explicit specialization and partial
950 specialization of a template class such as:
952 template <> class C<int>;
956 template <class T> class C<T*>;
958 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
960 if (tree t = maybe_new_partial_specialization (type))
962 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t));
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, "specializing %q#T in different namespace", type);
1015 permerror (input_location, " from definition of %q+#D", tmpl);
1018 /* Check for invalid specialization after instantiation:
1020 template <> template <> class C<int>::D<int>;
1021 template <> template <class U> class C<int>::D; */
1023 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1024 t; t = TREE_CHAIN (t))
1026 tree inst = TREE_VALUE (t);
1027 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
1029 /* We already have a full specialization of this partial
1030 instantiation. Reassign it to the new member
1031 specialization template. */
1032 spec_entry elt;
1033 spec_entry *entry;
1034 void **slot;
1036 elt.tmpl = most_general_template (tmpl);
1037 elt.args = CLASSTYPE_TI_ARGS (inst);
1038 elt.spec = inst;
1040 htab_remove_elt (type_specializations, &elt);
1042 elt.tmpl = tmpl;
1043 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1045 slot = htab_find_slot (type_specializations, &elt, INSERT);
1046 entry = ggc_alloc_spec_entry ();
1047 *entry = elt;
1048 *slot = entry;
1050 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
1051 /* But if we've had an implicit instantiation, that's a
1052 problem ([temp.expl.spec]/6). */
1053 error ("specialization %qT after instantiation %qT",
1054 type, inst);
1057 /* Mark TYPE as a specialization. And as a result, we only
1058 have one level of template argument for the innermost
1059 class template. */
1060 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1061 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1062 CLASSTYPE_TI_ARGS (type)
1063 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1066 else if (processing_specialization)
1068 /* Someday C++0x may allow for enum template specialization. */
1069 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1070 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1071 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1072 "of %qD not allowed by ISO C++", type);
1073 else
1075 error ("explicit specialization of non-template %qT", type);
1076 return error_mark_node;
1080 return type;
1083 /* Returns nonzero if we can optimize the retrieval of specializations
1084 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1085 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1087 static inline bool
1088 optimize_specialization_lookup_p (tree tmpl)
1090 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1091 && DECL_CLASS_SCOPE_P (tmpl)
1092 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1093 parameter. */
1094 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1095 /* The optimized lookup depends on the fact that the
1096 template arguments for the member function template apply
1097 purely to the containing class, which is not true if the
1098 containing class is an explicit or partial
1099 specialization. */
1100 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1101 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1102 && !DECL_CONV_FN_P (tmpl)
1103 /* It is possible to have a template that is not a member
1104 template and is not a member of a template class:
1106 template <typename T>
1107 struct S { friend A::f(); };
1109 Here, the friend function is a template, but the context does
1110 not have template information. The optimized lookup relies
1111 on having ARGS be the template arguments for both the class
1112 and the function template. */
1113 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1116 /* Retrieve the specialization (in the sense of [temp.spec] - a
1117 specialization is either an instantiation or an explicit
1118 specialization) of TMPL for the given template ARGS. If there is
1119 no such specialization, return NULL_TREE. The ARGS are a vector of
1120 arguments, or a vector of vectors of arguments, in the case of
1121 templates with more than one level of parameters.
1123 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1124 then we search for a partial specialization matching ARGS. This
1125 parameter is ignored if TMPL is not a class template.
1127 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1128 result is a NONTYPE_ARGUMENT_PACK. */
1130 static tree
1131 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1133 if (tmpl == NULL_TREE)
1134 return NULL_TREE;
1136 if (args == error_mark_node)
1137 return NULL_TREE;
1139 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1140 || TREE_CODE (tmpl) == FIELD_DECL);
1142 /* There should be as many levels of arguments as there are
1143 levels of parameters. */
1144 gcc_assert (TMPL_ARGS_DEPTH (args)
1145 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1146 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1147 : template_class_depth (DECL_CONTEXT (tmpl))));
1149 if (optimize_specialization_lookup_p (tmpl))
1151 tree class_template;
1152 tree class_specialization;
1153 vec<tree, va_gc> *methods;
1154 tree fns;
1155 int idx;
1157 /* The template arguments actually apply to the containing
1158 class. Find the class specialization with those
1159 arguments. */
1160 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1161 class_specialization
1162 = retrieve_specialization (class_template, args, 0);
1163 if (!class_specialization)
1164 return NULL_TREE;
1165 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1166 for the specialization. */
1167 idx = class_method_index_for_fn (class_specialization, tmpl);
1168 if (idx == -1)
1169 return NULL_TREE;
1170 /* Iterate through the methods with the indicated name, looking
1171 for the one that has an instance of TMPL. */
1172 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1173 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1175 tree fn = OVL_CURRENT (fns);
1176 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1177 /* using-declarations can add base methods to the method vec,
1178 and we don't want those here. */
1179 && DECL_CONTEXT (fn) == class_specialization)
1180 return fn;
1182 return NULL_TREE;
1184 else
1186 spec_entry *found;
1187 spec_entry elt;
1188 htab_t specializations;
1190 elt.tmpl = tmpl;
1191 elt.args = args;
1192 elt.spec = NULL_TREE;
1194 if (DECL_CLASS_TEMPLATE_P (tmpl))
1195 specializations = type_specializations;
1196 else
1197 specializations = decl_specializations;
1199 if (hash == 0)
1200 hash = hash_specialization (&elt);
1201 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1202 if (found)
1203 return found->spec;
1206 return NULL_TREE;
1209 /* Like retrieve_specialization, but for local declarations. */
1211 tree
1212 retrieve_local_specialization (tree tmpl)
1214 void **slot;
1216 if (local_specializations == NULL)
1217 return NULL_TREE;
1219 slot = pointer_map_contains (local_specializations, tmpl);
1220 return slot ? (tree) *slot : NULL_TREE;
1223 /* Returns nonzero iff DECL is a specialization of TMPL. */
1226 is_specialization_of (tree decl, tree tmpl)
1228 tree t;
1230 if (TREE_CODE (decl) == FUNCTION_DECL)
1232 for (t = decl;
1233 t != NULL_TREE;
1234 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1235 if (t == tmpl)
1236 return 1;
1238 else
1240 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1242 for (t = TREE_TYPE (decl);
1243 t != NULL_TREE;
1244 t = CLASSTYPE_USE_TEMPLATE (t)
1245 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1246 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1247 return 1;
1250 return 0;
1253 /* Returns nonzero iff DECL is a specialization of friend declaration
1254 FRIEND_DECL according to [temp.friend]. */
1256 bool
1257 is_specialization_of_friend (tree decl, tree friend_decl)
1259 bool need_template = true;
1260 int template_depth;
1262 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1263 || TREE_CODE (decl) == TYPE_DECL);
1265 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1266 of a template class, we want to check if DECL is a specialization
1267 if this. */
1268 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1269 && DECL_TEMPLATE_INFO (friend_decl)
1270 && !DECL_USE_TEMPLATE (friend_decl))
1272 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1273 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1274 need_template = false;
1276 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1277 && !PRIMARY_TEMPLATE_P (friend_decl))
1278 need_template = false;
1280 /* There is nothing to do if this is not a template friend. */
1281 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1282 return false;
1284 if (is_specialization_of (decl, friend_decl))
1285 return true;
1287 /* [temp.friend/6]
1288 A member of a class template may be declared to be a friend of a
1289 non-template class. In this case, the corresponding member of
1290 every specialization of the class template is a friend of the
1291 class granting friendship.
1293 For example, given a template friend declaration
1295 template <class T> friend void A<T>::f();
1297 the member function below is considered a friend
1299 template <> struct A<int> {
1300 void f();
1303 For this type of template friend, TEMPLATE_DEPTH below will be
1304 nonzero. To determine if DECL is a friend of FRIEND, we first
1305 check if the enclosing class is a specialization of another. */
1307 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1308 if (template_depth
1309 && DECL_CLASS_SCOPE_P (decl)
1310 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1311 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1313 /* Next, we check the members themselves. In order to handle
1314 a few tricky cases, such as when FRIEND_DECL's are
1316 template <class T> friend void A<T>::g(T t);
1317 template <class T> template <T t> friend void A<T>::h();
1319 and DECL's are
1321 void A<int>::g(int);
1322 template <int> void A<int>::h();
1324 we need to figure out ARGS, the template arguments from
1325 the context of DECL. This is required for template substitution
1326 of `T' in the function parameter of `g' and template parameter
1327 of `h' in the above examples. Here ARGS corresponds to `int'. */
1329 tree context = DECL_CONTEXT (decl);
1330 tree args = NULL_TREE;
1331 int current_depth = 0;
1333 while (current_depth < template_depth)
1335 if (CLASSTYPE_TEMPLATE_INFO (context))
1337 if (current_depth == 0)
1338 args = TYPE_TI_ARGS (context);
1339 else
1340 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1341 current_depth++;
1343 context = TYPE_CONTEXT (context);
1346 if (TREE_CODE (decl) == FUNCTION_DECL)
1348 bool is_template;
1349 tree friend_type;
1350 tree decl_type;
1351 tree friend_args_type;
1352 tree decl_args_type;
1354 /* Make sure that both DECL and FRIEND_DECL are templates or
1355 non-templates. */
1356 is_template = DECL_TEMPLATE_INFO (decl)
1357 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1358 if (need_template ^ is_template)
1359 return false;
1360 else if (is_template)
1362 /* If both are templates, check template parameter list. */
1363 tree friend_parms
1364 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1365 args, tf_none);
1366 if (!comp_template_parms
1367 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1368 friend_parms))
1369 return false;
1371 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1373 else
1374 decl_type = TREE_TYPE (decl);
1376 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1377 tf_none, NULL_TREE);
1378 if (friend_type == error_mark_node)
1379 return false;
1381 /* Check if return types match. */
1382 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1383 return false;
1385 /* Check if function parameter types match, ignoring the
1386 `this' parameter. */
1387 friend_args_type = TYPE_ARG_TYPES (friend_type);
1388 decl_args_type = TYPE_ARG_TYPES (decl_type);
1389 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1390 friend_args_type = TREE_CHAIN (friend_args_type);
1391 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1392 decl_args_type = TREE_CHAIN (decl_args_type);
1394 return compparms (decl_args_type, friend_args_type);
1396 else
1398 /* DECL is a TYPE_DECL */
1399 bool is_template;
1400 tree decl_type = TREE_TYPE (decl);
1402 /* Make sure that both DECL and FRIEND_DECL are templates or
1403 non-templates. */
1404 is_template
1405 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1406 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1408 if (need_template ^ is_template)
1409 return false;
1410 else if (is_template)
1412 tree friend_parms;
1413 /* If both are templates, check the name of the two
1414 TEMPLATE_DECL's first because is_friend didn't. */
1415 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1416 != DECL_NAME (friend_decl))
1417 return false;
1419 /* Now check template parameter list. */
1420 friend_parms
1421 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1422 args, tf_none);
1423 return comp_template_parms
1424 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1425 friend_parms);
1427 else
1428 return (DECL_NAME (decl)
1429 == DECL_NAME (friend_decl));
1432 return false;
1435 /* Register the specialization SPEC as a specialization of TMPL with
1436 the indicated ARGS. IS_FRIEND indicates whether the specialization
1437 is actually just a friend declaration. Returns SPEC, or an
1438 equivalent prior declaration, if available.
1440 We also store instantiations of field packs in the hash table, even
1441 though they are not themselves templates, to make lookup easier. */
1443 static tree
1444 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1445 hashval_t hash)
1447 tree fn;
1448 void **slot = NULL;
1449 spec_entry elt;
1451 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1452 || (TREE_CODE (tmpl) == FIELD_DECL
1453 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1455 if (TREE_CODE (spec) == FUNCTION_DECL
1456 && uses_template_parms (DECL_TI_ARGS (spec)))
1457 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1458 register it; we want the corresponding TEMPLATE_DECL instead.
1459 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1460 the more obvious `uses_template_parms (spec)' to avoid problems
1461 with default function arguments. In particular, given
1462 something like this:
1464 template <class T> void f(T t1, T t = T())
1466 the default argument expression is not substituted for in an
1467 instantiation unless and until it is actually needed. */
1468 return spec;
1470 if (optimize_specialization_lookup_p (tmpl))
1471 /* We don't put these specializations in the hash table, but we might
1472 want to give an error about a mismatch. */
1473 fn = retrieve_specialization (tmpl, args, 0);
1474 else
1476 elt.tmpl = tmpl;
1477 elt.args = args;
1478 elt.spec = spec;
1480 if (hash == 0)
1481 hash = hash_specialization (&elt);
1483 slot =
1484 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1485 if (*slot)
1486 fn = ((spec_entry *) *slot)->spec;
1487 else
1488 fn = NULL_TREE;
1491 /* We can sometimes try to re-register a specialization that we've
1492 already got. In particular, regenerate_decl_from_template calls
1493 duplicate_decls which will update the specialization list. But,
1494 we'll still get called again here anyhow. It's more convenient
1495 to simply allow this than to try to prevent it. */
1496 if (fn == spec)
1497 return spec;
1498 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1500 if (DECL_TEMPLATE_INSTANTIATION (fn))
1502 if (DECL_ODR_USED (fn)
1503 || DECL_EXPLICIT_INSTANTIATION (fn))
1505 error ("specialization of %qD after instantiation",
1506 fn);
1507 return error_mark_node;
1509 else
1511 tree clone;
1512 /* This situation should occur only if the first
1513 specialization is an implicit instantiation, the
1514 second is an explicit specialization, and the
1515 implicit instantiation has not yet been used. That
1516 situation can occur if we have implicitly
1517 instantiated a member function and then specialized
1518 it later.
1520 We can also wind up here if a friend declaration that
1521 looked like an instantiation turns out to be a
1522 specialization:
1524 template <class T> void foo(T);
1525 class S { friend void foo<>(int) };
1526 template <> void foo(int);
1528 We transform the existing DECL in place so that any
1529 pointers to it become pointers to the updated
1530 declaration.
1532 If there was a definition for the template, but not
1533 for the specialization, we want this to look as if
1534 there were no definition, and vice versa. */
1535 DECL_INITIAL (fn) = NULL_TREE;
1536 duplicate_decls (spec, fn, is_friend);
1537 /* The call to duplicate_decls will have applied
1538 [temp.expl.spec]:
1540 An explicit specialization of a function template
1541 is inline only if it is explicitly declared to be,
1542 and independently of whether its function template
1545 to the primary function; now copy the inline bits to
1546 the various clones. */
1547 FOR_EACH_CLONE (clone, fn)
1549 DECL_DECLARED_INLINE_P (clone)
1550 = DECL_DECLARED_INLINE_P (fn);
1551 DECL_SOURCE_LOCATION (clone)
1552 = DECL_SOURCE_LOCATION (fn);
1554 check_specialization_namespace (tmpl);
1556 return fn;
1559 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1561 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1562 /* Dup decl failed, but this is a new definition. Set the
1563 line number so any errors match this new
1564 definition. */
1565 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1567 return fn;
1570 else if (fn)
1571 return duplicate_decls (spec, fn, is_friend);
1573 /* A specialization must be declared in the same namespace as the
1574 template it is specializing. */
1575 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1576 && !check_specialization_namespace (tmpl))
1577 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1579 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1581 spec_entry *entry = ggc_alloc_spec_entry ();
1582 gcc_assert (tmpl && args && spec);
1583 *entry = elt;
1584 *slot = entry;
1585 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1586 && PRIMARY_TEMPLATE_P (tmpl)
1587 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1588 /* TMPL is a forward declaration of a template function; keep a list
1589 of all specializations in case we need to reassign them to a friend
1590 template later in tsubst_friend_function. */
1591 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1592 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1595 return spec;
1598 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1599 TMPL and ARGS members, ignores SPEC. */
1601 int comparing_specializations;
1603 static int
1604 eq_specializations (const void *p1, const void *p2)
1606 const spec_entry *e1 = (const spec_entry *)p1;
1607 const spec_entry *e2 = (const spec_entry *)p2;
1608 int equal;
1610 ++comparing_specializations;
1611 equal = (e1->tmpl == e2->tmpl
1612 && comp_template_args (e1->args, e2->args));
1613 --comparing_specializations;
1615 return equal;
1618 /* Returns a hash for a template TMPL and template arguments ARGS. */
1620 static hashval_t
1621 hash_tmpl_and_args (tree tmpl, tree args)
1623 hashval_t val = DECL_UID (tmpl);
1624 return iterative_hash_template_arg (args, val);
1627 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1628 ignoring SPEC. */
1630 static hashval_t
1631 hash_specialization (const void *p)
1633 const spec_entry *e = (const spec_entry *)p;
1634 return hash_tmpl_and_args (e->tmpl, e->args);
1637 /* Recursively calculate a hash value for a template argument ARG, for use
1638 in the hash tables of template specializations. */
1640 hashval_t
1641 iterative_hash_template_arg (tree arg, hashval_t val)
1643 unsigned HOST_WIDE_INT i;
1644 enum tree_code code;
1645 char tclass;
1647 if (arg == NULL_TREE)
1648 return iterative_hash_object (arg, val);
1650 if (!TYPE_P (arg))
1651 STRIP_NOPS (arg);
1653 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1654 /* We can get one of these when re-hashing a previous entry in the middle
1655 of substituting into a pack expansion. Just look through it. */
1656 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1658 code = TREE_CODE (arg);
1659 tclass = TREE_CODE_CLASS (code);
1661 val = iterative_hash_object (code, val);
1663 switch (code)
1665 case ERROR_MARK:
1666 return val;
1668 case IDENTIFIER_NODE:
1669 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1671 case TREE_VEC:
1673 int i, len = TREE_VEC_LENGTH (arg);
1674 for (i = 0; i < len; ++i)
1675 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1676 return val;
1679 case TYPE_PACK_EXPANSION:
1680 case EXPR_PACK_EXPANSION:
1681 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1682 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1684 case TYPE_ARGUMENT_PACK:
1685 case NONTYPE_ARGUMENT_PACK:
1686 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1688 case TREE_LIST:
1689 for (; arg; arg = TREE_CHAIN (arg))
1690 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1691 return val;
1693 case OVERLOAD:
1694 for (; arg; arg = OVL_NEXT (arg))
1695 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1696 return val;
1698 case CONSTRUCTOR:
1700 tree field, value;
1701 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1703 val = iterative_hash_template_arg (field, val);
1704 val = iterative_hash_template_arg (value, val);
1706 return val;
1709 case PARM_DECL:
1710 if (!DECL_ARTIFICIAL (arg))
1712 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1713 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1715 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1717 case TARGET_EXPR:
1718 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1720 case PTRMEM_CST:
1721 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1722 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1724 case TEMPLATE_PARM_INDEX:
1725 val = iterative_hash_template_arg
1726 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1727 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1728 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1730 case TRAIT_EXPR:
1731 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1732 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1733 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1735 case BASELINK:
1736 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1737 val);
1738 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1739 val);
1741 case MODOP_EXPR:
1742 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1743 code = TREE_CODE (TREE_OPERAND (arg, 1));
1744 val = iterative_hash_object (code, val);
1745 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1747 case LAMBDA_EXPR:
1748 /* A lambda can't appear in a template arg, but don't crash on
1749 erroneous input. */
1750 gcc_assert (seen_error ());
1751 return val;
1753 case CAST_EXPR:
1754 case IMPLICIT_CONV_EXPR:
1755 case STATIC_CAST_EXPR:
1756 case REINTERPRET_CAST_EXPR:
1757 case CONST_CAST_EXPR:
1758 case DYNAMIC_CAST_EXPR:
1759 case NEW_EXPR:
1760 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1761 /* Now hash operands as usual. */
1762 break;
1764 default:
1765 break;
1768 switch (tclass)
1770 case tcc_type:
1771 if (TYPE_CANONICAL (arg))
1772 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1773 val);
1774 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1775 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1776 /* Otherwise just compare the types during lookup. */
1777 return val;
1779 case tcc_declaration:
1780 case tcc_constant:
1781 return iterative_hash_expr (arg, val);
1783 default:
1784 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1786 unsigned n = cp_tree_operand_length (arg);
1787 for (i = 0; i < n; ++i)
1788 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1789 return val;
1792 gcc_unreachable ();
1793 return 0;
1796 /* Unregister the specialization SPEC as a specialization of TMPL.
1797 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1798 if the SPEC was listed as a specialization of TMPL.
1800 Note that SPEC has been ggc_freed, so we can't look inside it. */
1802 bool
1803 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1805 spec_entry *entry;
1806 spec_entry elt;
1808 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1809 elt.args = TI_ARGS (tinfo);
1810 elt.spec = NULL_TREE;
1812 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1813 if (entry != NULL)
1815 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1816 gcc_assert (new_spec != NULL_TREE);
1817 entry->spec = new_spec;
1818 return 1;
1821 return 0;
1824 /* Like register_specialization, but for local declarations. We are
1825 registering SPEC, an instantiation of TMPL. */
1827 void
1828 register_local_specialization (tree spec, tree tmpl)
1830 void **slot;
1832 slot = pointer_map_insert (local_specializations, tmpl);
1833 *slot = spec;
1836 /* TYPE is a class type. Returns true if TYPE is an explicitly
1837 specialized class. */
1839 bool
1840 explicit_class_specialization_p (tree type)
1842 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1843 return false;
1844 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1847 /* Print the list of functions at FNS, going through all the overloads
1848 for each element of the list. Alternatively, FNS can not be a
1849 TREE_LIST, in which case it will be printed together with all the
1850 overloads.
1852 MORE and *STR should respectively be FALSE and NULL when the function
1853 is called from the outside. They are used internally on recursive
1854 calls. print_candidates manages the two parameters and leaves NULL
1855 in *STR when it ends. */
1857 static void
1858 print_candidates_1 (tree fns, bool more, const char **str)
1860 tree fn, fn2;
1861 char *spaces = NULL;
1863 for (fn = fns; fn; fn = OVL_NEXT (fn))
1864 if (TREE_CODE (fn) == TREE_LIST)
1866 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1867 print_candidates_1 (TREE_VALUE (fn2),
1868 TREE_CHAIN (fn2) || more, str);
1870 else
1872 tree cand = OVL_CURRENT (fn);
1873 if (!*str)
1875 /* Pick the prefix string. */
1876 if (!more && !OVL_NEXT (fns))
1878 inform (DECL_SOURCE_LOCATION (cand),
1879 "candidate is: %#D", cand);
1880 continue;
1883 *str = _("candidates are:");
1884 spaces = get_spaces (*str);
1886 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1887 *str = spaces ? spaces : *str;
1890 if (!more)
1892 free (spaces);
1893 *str = NULL;
1897 /* Print the list of candidate FNS in an error message. FNS can also
1898 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1900 void
1901 print_candidates (tree fns)
1903 const char *str = NULL;
1904 print_candidates_1 (fns, false, &str);
1905 gcc_assert (str == NULL);
1908 /* Returns the template (one of the functions given by TEMPLATE_ID)
1909 which can be specialized to match the indicated DECL with the
1910 explicit template args given in TEMPLATE_ID. The DECL may be
1911 NULL_TREE if none is available. In that case, the functions in
1912 TEMPLATE_ID are non-members.
1914 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1915 specialization of a member template.
1917 The TEMPLATE_COUNT is the number of references to qualifying
1918 template classes that appeared in the name of the function. See
1919 check_explicit_specialization for a more accurate description.
1921 TSK indicates what kind of template declaration (if any) is being
1922 declared. TSK_TEMPLATE indicates that the declaration given by
1923 DECL, though a FUNCTION_DECL, has template parameters, and is
1924 therefore a template function.
1926 The template args (those explicitly specified and those deduced)
1927 are output in a newly created vector *TARGS_OUT.
1929 If it is impossible to determine the result, an error message is
1930 issued. The error_mark_node is returned to indicate failure. */
1932 static tree
1933 determine_specialization (tree template_id,
1934 tree decl,
1935 tree* targs_out,
1936 int need_member_template,
1937 int template_count,
1938 tmpl_spec_kind tsk)
1940 tree fns;
1941 tree targs;
1942 tree explicit_targs;
1943 tree candidates = NULL_TREE;
1944 /* A TREE_LIST of templates of which DECL may be a specialization.
1945 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1946 corresponding TREE_PURPOSE is the set of template arguments that,
1947 when used to instantiate the template, would produce a function
1948 with the signature of DECL. */
1949 tree templates = NULL_TREE;
1950 int header_count;
1951 cp_binding_level *b;
1953 *targs_out = NULL_TREE;
1955 if (template_id == error_mark_node || decl == error_mark_node)
1956 return error_mark_node;
1958 /* We shouldn't be specializing a member template of an
1959 unspecialized class template; we already gave an error in
1960 check_specialization_scope, now avoid crashing. */
1961 if (template_count && DECL_CLASS_SCOPE_P (decl)
1962 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1964 gcc_assert (errorcount);
1965 return error_mark_node;
1968 fns = TREE_OPERAND (template_id, 0);
1969 explicit_targs = TREE_OPERAND (template_id, 1);
1971 if (fns == error_mark_node)
1972 return error_mark_node;
1974 /* Check for baselinks. */
1975 if (BASELINK_P (fns))
1976 fns = BASELINK_FUNCTIONS (fns);
1978 if (!is_overloaded_fn (fns))
1980 error ("%qD is not a function template", fns);
1981 return error_mark_node;
1984 /* Count the number of template headers specified for this
1985 specialization. */
1986 header_count = 0;
1987 for (b = current_binding_level;
1988 b->kind == sk_template_parms;
1989 b = b->level_chain)
1990 ++header_count;
1992 for (; fns; fns = OVL_NEXT (fns))
1994 tree fn = OVL_CURRENT (fns);
1996 if (TREE_CODE (fn) == TEMPLATE_DECL)
1998 tree decl_arg_types;
1999 tree fn_arg_types;
2000 tree insttype;
2002 /* In case of explicit specialization, we need to check if
2003 the number of template headers appearing in the specialization
2004 is correct. This is usually done in check_explicit_specialization,
2005 but the check done there cannot be exhaustive when specializing
2006 member functions. Consider the following code:
2008 template <> void A<int>::f(int);
2009 template <> template <> void A<int>::f(int);
2011 Assuming that A<int> is not itself an explicit specialization
2012 already, the first line specializes "f" which is a non-template
2013 member function, whilst the second line specializes "f" which
2014 is a template member function. So both lines are syntactically
2015 correct, and check_explicit_specialization does not reject
2016 them.
2018 Here, we can do better, as we are matching the specialization
2019 against the declarations. We count the number of template
2020 headers, and we check if they match TEMPLATE_COUNT + 1
2021 (TEMPLATE_COUNT is the number of qualifying template classes,
2022 plus there must be another header for the member template
2023 itself).
2025 Notice that if header_count is zero, this is not a
2026 specialization but rather a template instantiation, so there
2027 is no check we can perform here. */
2028 if (header_count && header_count != template_count + 1)
2029 continue;
2031 /* Check that the number of template arguments at the
2032 innermost level for DECL is the same as for FN. */
2033 if (current_binding_level->kind == sk_template_parms
2034 && !current_binding_level->explicit_spec_p
2035 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2036 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2037 (current_template_parms))))
2038 continue;
2040 /* DECL might be a specialization of FN. */
2041 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2042 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2044 /* For a non-static member function, we need to make sure
2045 that the const qualification is the same. Since
2046 get_bindings does not try to merge the "this" parameter,
2047 we must do the comparison explicitly. */
2048 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2049 && !same_type_p (TREE_VALUE (fn_arg_types),
2050 TREE_VALUE (decl_arg_types)))
2051 continue;
2053 /* Skip the "this" parameter and, for constructors of
2054 classes with virtual bases, the VTT parameter. A
2055 full specialization of a constructor will have a VTT
2056 parameter, but a template never will. */
2057 decl_arg_types
2058 = skip_artificial_parms_for (decl, decl_arg_types);
2059 fn_arg_types
2060 = skip_artificial_parms_for (fn, fn_arg_types);
2062 /* Function templates cannot be specializations; there are
2063 no partial specializations of functions. Therefore, if
2064 the type of DECL does not match FN, there is no
2065 match. */
2066 if (tsk == tsk_template)
2068 if (compparms (fn_arg_types, decl_arg_types))
2069 candidates = tree_cons (NULL_TREE, fn, candidates);
2070 continue;
2073 /* See whether this function might be a specialization of this
2074 template. Suppress access control because we might be trying
2075 to make this specialization a friend, and we have already done
2076 access control for the declaration of the specialization. */
2077 push_deferring_access_checks (dk_no_check);
2078 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2079 pop_deferring_access_checks ();
2081 if (!targs)
2082 /* We cannot deduce template arguments that when used to
2083 specialize TMPL will produce DECL. */
2084 continue;
2086 // Make sure that the deduced arguments actually work. First,
2087 // check that any template constraints are satisfied.
2089 // TODO: Make sure that we get reasonable diagnostics for these
2090 // kinds of failures.
2091 if (!check_template_constraints (fn, targs))
2092 continue;
2094 // Then, try to form the new function type.
2095 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2096 if (insttype == error_mark_node)
2097 continue;
2098 fn_arg_types
2099 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2100 if (!compparms (fn_arg_types, decl_arg_types))
2101 continue;
2103 /* Save this template, and the arguments deduced. */
2104 templates = tree_cons (targs, fn, templates);
2106 else if (need_member_template)
2107 /* FN is an ordinary member function, and we need a
2108 specialization of a member template. */
2110 else if (TREE_CODE (fn) != FUNCTION_DECL)
2111 /* We can get IDENTIFIER_NODEs here in certain erroneous
2112 cases. */
2114 else if (!DECL_FUNCTION_MEMBER_P (fn))
2115 /* This is just an ordinary non-member function. Nothing can
2116 be a specialization of that. */
2118 else if (DECL_ARTIFICIAL (fn))
2119 /* Cannot specialize functions that are created implicitly. */
2121 else
2123 tree decl_arg_types;
2125 /* This is an ordinary member function. However, since
2126 we're here, we can assume its enclosing class is a
2127 template class. For example,
2129 template <typename T> struct S { void f(); };
2130 template <> void S<int>::f() {}
2132 Here, S<int>::f is a non-template, but S<int> is a
2133 template class. If FN has the same type as DECL, we
2134 might be in business. */
2136 if (!DECL_TEMPLATE_INFO (fn))
2137 /* Its enclosing class is an explicit specialization
2138 of a template class. This is not a candidate. */
2139 continue;
2141 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2142 TREE_TYPE (TREE_TYPE (fn))))
2143 /* The return types differ. */
2144 continue;
2146 /* Adjust the type of DECL in case FN is a static member. */
2147 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2148 if (DECL_STATIC_FUNCTION_P (fn)
2149 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2150 decl_arg_types = TREE_CHAIN (decl_arg_types);
2152 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2153 decl_arg_types))
2154 /* They match! */
2155 candidates = tree_cons (NULL_TREE, fn, candidates);
2159 if (templates && TREE_CHAIN (templates))
2161 /* We have:
2163 [temp.expl.spec]
2165 It is possible for a specialization with a given function
2166 signature to be instantiated from more than one function
2167 template. In such cases, explicit specification of the
2168 template arguments must be used to uniquely identify the
2169 function template specialization being specialized.
2171 Note that here, there's no suggestion that we're supposed to
2172 determine which of the candidate templates is most
2173 specialized. However, we, also have:
2175 [temp.func.order]
2177 Partial ordering of overloaded function template
2178 declarations is used in the following contexts to select
2179 the function template to which a function template
2180 specialization refers:
2182 -- when an explicit specialization refers to a function
2183 template.
2185 So, we do use the partial ordering rules, at least for now.
2186 This extension can only serve to make invalid programs valid,
2187 so it's safe. And, there is strong anecdotal evidence that
2188 the committee intended the partial ordering rules to apply;
2189 the EDG front end has that behavior, and John Spicer claims
2190 that the committee simply forgot to delete the wording in
2191 [temp.expl.spec]. */
2192 tree tmpl = most_specialized_instantiation (templates);
2193 if (tmpl != error_mark_node)
2195 templates = tmpl;
2196 TREE_CHAIN (templates) = NULL_TREE;
2200 if (templates == NULL_TREE && candidates == NULL_TREE)
2202 error ("template-id %qD for %q+D does not match any template "
2203 "declaration", template_id, decl);
2204 if (header_count && header_count != template_count + 1)
2205 inform (input_location, "saw %d %<template<>%>, need %d for "
2206 "specializing a member function template",
2207 header_count, template_count + 1);
2208 return error_mark_node;
2210 else if ((templates && TREE_CHAIN (templates))
2211 || (candidates && TREE_CHAIN (candidates))
2212 || (templates && candidates))
2214 error ("ambiguous template specialization %qD for %q+D",
2215 template_id, decl);
2216 candidates = chainon (candidates, templates);
2217 print_candidates (candidates);
2218 return error_mark_node;
2221 /* We have one, and exactly one, match. */
2222 if (candidates)
2224 tree fn = TREE_VALUE (candidates);
2225 *targs_out = copy_node (DECL_TI_ARGS (fn));
2226 /* DECL is a re-declaration or partial instantiation of a template
2227 function. */
2228 if (TREE_CODE (fn) == TEMPLATE_DECL)
2229 return fn;
2230 /* It was a specialization of an ordinary member function in a
2231 template class. */
2232 return DECL_TI_TEMPLATE (fn);
2235 /* It was a specialization of a template. */
2236 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2237 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2239 *targs_out = copy_node (targs);
2240 SET_TMPL_ARGS_LEVEL (*targs_out,
2241 TMPL_ARGS_DEPTH (*targs_out),
2242 TREE_PURPOSE (templates));
2244 else
2245 *targs_out = TREE_PURPOSE (templates);
2246 return TREE_VALUE (templates);
2249 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2250 but with the default argument values filled in from those in the
2251 TMPL_TYPES. */
2253 static tree
2254 copy_default_args_to_explicit_spec_1 (tree spec_types,
2255 tree tmpl_types)
2257 tree new_spec_types;
2259 if (!spec_types)
2260 return NULL_TREE;
2262 if (spec_types == void_list_node)
2263 return void_list_node;
2265 /* Substitute into the rest of the list. */
2266 new_spec_types =
2267 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2268 TREE_CHAIN (tmpl_types));
2270 /* Add the default argument for this parameter. */
2271 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2272 TREE_VALUE (spec_types),
2273 new_spec_types);
2276 /* DECL is an explicit specialization. Replicate default arguments
2277 from the template it specializes. (That way, code like:
2279 template <class T> void f(T = 3);
2280 template <> void f(double);
2281 void g () { f (); }
2283 works, as required.) An alternative approach would be to look up
2284 the correct default arguments at the call-site, but this approach
2285 is consistent with how implicit instantiations are handled. */
2287 static void
2288 copy_default_args_to_explicit_spec (tree decl)
2290 tree tmpl;
2291 tree spec_types;
2292 tree tmpl_types;
2293 tree new_spec_types;
2294 tree old_type;
2295 tree new_type;
2296 tree t;
2297 tree object_type = NULL_TREE;
2298 tree in_charge = NULL_TREE;
2299 tree vtt = NULL_TREE;
2301 /* See if there's anything we need to do. */
2302 tmpl = DECL_TI_TEMPLATE (decl);
2303 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2304 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2305 if (TREE_PURPOSE (t))
2306 break;
2307 if (!t)
2308 return;
2310 old_type = TREE_TYPE (decl);
2311 spec_types = TYPE_ARG_TYPES (old_type);
2313 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2315 /* Remove the this pointer, but remember the object's type for
2316 CV quals. */
2317 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2318 spec_types = TREE_CHAIN (spec_types);
2319 tmpl_types = TREE_CHAIN (tmpl_types);
2321 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2323 /* DECL may contain more parameters than TMPL due to the extra
2324 in-charge parameter in constructors and destructors. */
2325 in_charge = spec_types;
2326 spec_types = TREE_CHAIN (spec_types);
2328 if (DECL_HAS_VTT_PARM_P (decl))
2330 vtt = spec_types;
2331 spec_types = TREE_CHAIN (spec_types);
2335 /* Compute the merged default arguments. */
2336 new_spec_types =
2337 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2339 /* Compute the new FUNCTION_TYPE. */
2340 if (object_type)
2342 if (vtt)
2343 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2344 TREE_VALUE (vtt),
2345 new_spec_types);
2347 if (in_charge)
2348 /* Put the in-charge parameter back. */
2349 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2350 TREE_VALUE (in_charge),
2351 new_spec_types);
2353 new_type = build_method_type_directly (object_type,
2354 TREE_TYPE (old_type),
2355 new_spec_types);
2357 else
2358 new_type = build_function_type (TREE_TYPE (old_type),
2359 new_spec_types);
2360 new_type = cp_build_type_attribute_variant (new_type,
2361 TYPE_ATTRIBUTES (old_type));
2362 new_type = build_exception_variant (new_type,
2363 TYPE_RAISES_EXCEPTIONS (old_type));
2364 TREE_TYPE (decl) = new_type;
2367 /* Return the number of template headers we expect to see for a definition
2368 or specialization of CTYPE or one of its non-template members. */
2371 num_template_headers_for_class (tree ctype)
2373 int num_templates = 0;
2375 while (ctype && CLASS_TYPE_P (ctype))
2377 /* You're supposed to have one `template <...>' for every
2378 template class, but you don't need one for a full
2379 specialization. For example:
2381 template <class T> struct S{};
2382 template <> struct S<int> { void f(); };
2383 void S<int>::f () {}
2385 is correct; there shouldn't be a `template <>' for the
2386 definition of `S<int>::f'. */
2387 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2388 /* If CTYPE does not have template information of any
2389 kind, then it is not a template, nor is it nested
2390 within a template. */
2391 break;
2392 if (explicit_class_specialization_p (ctype))
2393 break;
2394 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2395 ++num_templates;
2397 ctype = TYPE_CONTEXT (ctype);
2400 return num_templates;
2403 /* Do a simple sanity check on the template headers that precede the
2404 variable declaration DECL. */
2406 void
2407 check_template_variable (tree decl)
2409 tree ctx = CP_DECL_CONTEXT (decl);
2410 int wanted = num_template_headers_for_class (ctx);
2411 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2412 permerror (DECL_SOURCE_LOCATION (decl),
2413 "%qD is not a static data member of a class template", decl);
2414 else if (template_header_count > wanted)
2416 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2417 "too many template headers for %D (should be %d)",
2418 decl, wanted);
2419 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2420 inform (DECL_SOURCE_LOCATION (decl),
2421 "members of an explicitly specialized class are defined "
2422 "without a template header");
2426 /* Check to see if the function just declared, as indicated in
2427 DECLARATOR, and in DECL, is a specialization of a function
2428 template. We may also discover that the declaration is an explicit
2429 instantiation at this point.
2431 Returns DECL, or an equivalent declaration that should be used
2432 instead if all goes well. Issues an error message if something is
2433 amiss. Returns error_mark_node if the error is not easily
2434 recoverable.
2436 FLAGS is a bitmask consisting of the following flags:
2438 2: The function has a definition.
2439 4: The function is a friend.
2441 The TEMPLATE_COUNT is the number of references to qualifying
2442 template classes that appeared in the name of the function. For
2443 example, in
2445 template <class T> struct S { void f(); };
2446 void S<int>::f();
2448 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2449 classes are not counted in the TEMPLATE_COUNT, so that in
2451 template <class T> struct S {};
2452 template <> struct S<int> { void f(); }
2453 template <> void S<int>::f();
2455 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2456 invalid; there should be no template <>.)
2458 If the function is a specialization, it is marked as such via
2459 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2460 is set up correctly, and it is added to the list of specializations
2461 for that template. */
2463 tree
2464 check_explicit_specialization (tree declarator,
2465 tree decl,
2466 int template_count,
2467 int flags)
2469 int have_def = flags & 2;
2470 int is_friend = flags & 4;
2471 bool is_concept = flags & 8;
2472 int specialization = 0;
2473 int explicit_instantiation = 0;
2474 int member_specialization = 0;
2475 tree ctype = DECL_CLASS_CONTEXT (decl);
2476 tree dname = DECL_NAME (decl);
2477 tmpl_spec_kind tsk;
2479 if (is_friend)
2481 if (!processing_specialization)
2482 tsk = tsk_none;
2483 else
2484 tsk = tsk_excessive_parms;
2486 else
2487 tsk = current_tmpl_spec_kind (template_count);
2489 switch (tsk)
2491 case tsk_none:
2492 if (processing_specialization)
2494 specialization = 1;
2495 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2497 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2499 if (is_friend)
2500 /* This could be something like:
2502 template <class T> void f(T);
2503 class S { friend void f<>(int); } */
2504 specialization = 1;
2505 else
2507 /* This case handles bogus declarations like template <>
2508 template <class T> void f<int>(); */
2510 error ("template-id %qD in declaration of primary template",
2511 declarator);
2512 return decl;
2515 break;
2517 case tsk_invalid_member_spec:
2518 /* The error has already been reported in
2519 check_specialization_scope. */
2520 return error_mark_node;
2522 case tsk_invalid_expl_inst:
2523 error ("template parameter list used in explicit instantiation");
2525 /* Fall through. */
2527 case tsk_expl_inst:
2528 if (have_def)
2529 error ("definition provided for explicit instantiation");
2531 explicit_instantiation = 1;
2532 break;
2534 case tsk_excessive_parms:
2535 case tsk_insufficient_parms:
2536 if (tsk == tsk_excessive_parms)
2537 error ("too many template parameter lists in declaration of %qD",
2538 decl);
2539 else if (template_header_count)
2540 error("too few template parameter lists in declaration of %qD", decl);
2541 else
2542 error("explicit specialization of %qD must be introduced by "
2543 "%<template <>%>", decl);
2545 /* Fall through. */
2546 case tsk_expl_spec:
2547 if (is_concept)
2548 error ("explicit specialization declared %<concept%>");
2549 // if (DECL_DECLARED_CONCEPT_P (decl))
2550 // error ("explicit specialization of concept %qD", decl);
2552 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2553 if (ctype)
2554 member_specialization = 1;
2555 else
2556 specialization = 1;
2557 break;
2559 case tsk_template:
2560 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2562 /* This case handles bogus declarations like template <>
2563 template <class T> void f<int>(); */
2565 if (uses_template_parms (declarator))
2566 error ("function template partial specialization %qD "
2567 "is not allowed", declarator);
2568 else
2569 error ("template-id %qD in declaration of primary template",
2570 declarator);
2571 return decl;
2574 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2575 /* This is a specialization of a member template, without
2576 specialization the containing class. Something like:
2578 template <class T> struct S {
2579 template <class U> void f (U);
2581 template <> template <class U> void S<int>::f(U) {}
2583 That's a specialization -- but of the entire template. */
2584 specialization = 1;
2585 break;
2587 default:
2588 gcc_unreachable ();
2591 if (specialization || member_specialization)
2593 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2594 for (; t; t = TREE_CHAIN (t))
2595 if (TREE_PURPOSE (t))
2597 permerror (input_location,
2598 "default argument specified in explicit specialization");
2599 break;
2603 if (specialization || member_specialization || explicit_instantiation)
2605 tree tmpl = NULL_TREE;
2606 tree targs = NULL_TREE;
2608 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2609 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2611 tree fns;
2613 gcc_assert (identifier_p (declarator));
2614 if (ctype)
2615 fns = dname;
2616 else
2618 /* If there is no class context, the explicit instantiation
2619 must be at namespace scope. */
2620 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2622 /* Find the namespace binding, using the declaration
2623 context. */
2624 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2625 false, true);
2626 if (fns == error_mark_node || !is_overloaded_fn (fns))
2628 error ("%qD is not a template function", dname);
2629 fns = error_mark_node;
2631 else
2633 tree fn = OVL_CURRENT (fns);
2634 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2635 CP_DECL_CONTEXT (fn)))
2636 error ("%qD is not declared in %qD",
2637 decl, current_namespace);
2641 declarator = lookup_template_function (fns, NULL_TREE);
2644 if (declarator == error_mark_node)
2645 return error_mark_node;
2647 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2649 if (!explicit_instantiation)
2650 /* A specialization in class scope. This is invalid,
2651 but the error will already have been flagged by
2652 check_specialization_scope. */
2653 return error_mark_node;
2654 else
2656 /* It's not valid to write an explicit instantiation in
2657 class scope, e.g.:
2659 class C { template void f(); }
2661 This case is caught by the parser. However, on
2662 something like:
2664 template class C { void f(); };
2666 (which is invalid) we can get here. The error will be
2667 issued later. */
2671 return decl;
2673 else if (ctype != NULL_TREE
2674 && (identifier_p (TREE_OPERAND (declarator, 0))))
2676 /* Find the list of functions in ctype that have the same
2677 name as the declared function. */
2678 tree name = TREE_OPERAND (declarator, 0);
2679 tree fns = NULL_TREE;
2680 int idx;
2682 if (constructor_name_p (name, ctype))
2684 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2686 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2687 : !CLASSTYPE_DESTRUCTORS (ctype))
2689 /* From [temp.expl.spec]:
2691 If such an explicit specialization for the member
2692 of a class template names an implicitly-declared
2693 special member function (clause _special_), the
2694 program is ill-formed.
2696 Similar language is found in [temp.explicit]. */
2697 error ("specialization of implicitly-declared special member function");
2698 return error_mark_node;
2701 name = is_constructor ? ctor_identifier : dtor_identifier;
2704 if (!DECL_CONV_FN_P (decl))
2706 idx = lookup_fnfields_1 (ctype, name);
2707 if (idx >= 0)
2708 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2710 else
2712 vec<tree, va_gc> *methods;
2713 tree ovl;
2715 /* For a type-conversion operator, we cannot do a
2716 name-based lookup. We might be looking for `operator
2717 int' which will be a specialization of `operator T'.
2718 So, we find *all* the conversion operators, and then
2719 select from them. */
2720 fns = NULL_TREE;
2722 methods = CLASSTYPE_METHOD_VEC (ctype);
2723 if (methods)
2724 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2725 methods->iterate (idx, &ovl);
2726 ++idx)
2728 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2729 /* There are no more conversion functions. */
2730 break;
2732 /* Glue all these conversion functions together
2733 with those we already have. */
2734 for (; ovl; ovl = OVL_NEXT (ovl))
2735 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2739 if (fns == NULL_TREE)
2741 error ("no member function %qD declared in %qT", name, ctype);
2742 return error_mark_node;
2744 else
2745 TREE_OPERAND (declarator, 0) = fns;
2748 /* Figure out what exactly is being specialized at this point.
2749 Note that for an explicit instantiation, even one for a
2750 member function, we cannot tell apriori whether the
2751 instantiation is for a member template, or just a member
2752 function of a template class. Even if a member template is
2753 being instantiated, the member template arguments may be
2754 elided if they can be deduced from the rest of the
2755 declaration. */
2756 tmpl = determine_specialization (declarator, decl,
2757 &targs,
2758 member_specialization,
2759 template_count,
2760 tsk);
2762 if (!tmpl || tmpl == error_mark_node)
2763 /* We couldn't figure out what this declaration was
2764 specializing. */
2765 return error_mark_node;
2766 else
2768 tree gen_tmpl = most_general_template (tmpl);
2770 if (explicit_instantiation)
2772 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2773 is done by do_decl_instantiation later. */
2775 int arg_depth = TMPL_ARGS_DEPTH (targs);
2776 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2778 if (arg_depth > parm_depth)
2780 /* If TMPL is not the most general template (for
2781 example, if TMPL is a friend template that is
2782 injected into namespace scope), then there will
2783 be too many levels of TARGS. Remove some of them
2784 here. */
2785 int i;
2786 tree new_targs;
2788 new_targs = make_tree_vec (parm_depth);
2789 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2790 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2791 = TREE_VEC_ELT (targs, i);
2792 targs = new_targs;
2795 return instantiate_template (tmpl, targs, tf_error);
2798 /* If we thought that the DECL was a member function, but it
2799 turns out to be specializing a static member function,
2800 make DECL a static member function as well. */
2801 if (DECL_STATIC_FUNCTION_P (tmpl)
2802 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2803 revert_static_member_fn (decl);
2805 /* If this is a specialization of a member template of a
2806 template class, we want to return the TEMPLATE_DECL, not
2807 the specialization of it. */
2808 if (tsk == tsk_template)
2810 tree result = DECL_TEMPLATE_RESULT (tmpl);
2811 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2812 DECL_INITIAL (result) = NULL_TREE;
2813 if (have_def)
2815 tree parm;
2816 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2817 DECL_SOURCE_LOCATION (result)
2818 = DECL_SOURCE_LOCATION (decl);
2819 /* We want to use the argument list specified in the
2820 definition, not in the original declaration. */
2821 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2822 for (parm = DECL_ARGUMENTS (result); parm;
2823 parm = DECL_CHAIN (parm))
2824 DECL_CONTEXT (parm) = result;
2826 return register_specialization (tmpl, gen_tmpl, targs,
2827 is_friend, 0);
2830 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2831 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2833 /* Inherit default function arguments from the template
2834 DECL is specializing. */
2835 copy_default_args_to_explicit_spec (decl);
2837 /* This specialization has the same protection as the
2838 template it specializes. */
2839 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2840 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2842 /* 7.1.1-1 [dcl.stc]
2844 A storage-class-specifier shall not be specified in an
2845 explicit specialization...
2847 The parser rejects these, so unless action is taken here,
2848 explicit function specializations will always appear with
2849 global linkage.
2851 The action recommended by the C++ CWG in response to C++
2852 defect report 605 is to make the storage class and linkage
2853 of the explicit specialization match the templated function:
2855 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2857 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2859 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2860 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2862 // A concept cannot be specialized.
2863 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
2865 error ("explicit specialization of concept %qD", gen_tmpl);
2866 return error_mark_node;
2869 /* This specialization has the same linkage and visibility as
2870 the function template it specializes. */
2871 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2872 if (! TREE_PUBLIC (decl))
2874 DECL_INTERFACE_KNOWN (decl) = 1;
2875 DECL_NOT_REALLY_EXTERN (decl) = 1;
2877 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2878 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2880 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2881 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2885 /* If DECL is a friend declaration, declared using an
2886 unqualified name, the namespace associated with DECL may
2887 have been set incorrectly. For example, in:
2889 template <typename T> void f(T);
2890 namespace N {
2891 struct S { friend void f<int>(int); }
2894 we will have set the DECL_CONTEXT for the friend
2895 declaration to N, rather than to the global namespace. */
2896 if (DECL_NAMESPACE_SCOPE_P (decl))
2897 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2899 if (is_friend && !have_def)
2900 /* This is not really a declaration of a specialization.
2901 It's just the name of an instantiation. But, it's not
2902 a request for an instantiation, either. */
2903 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2904 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2905 /* This is indeed a specialization. In case of constructors
2906 and destructors, we need in-charge and not-in-charge
2907 versions in V3 ABI. */
2908 clone_function_decl (decl, /*update_method_vec_p=*/0);
2910 /* Register this specialization so that we can find it
2911 again. */
2912 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2916 return decl;
2919 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2920 parameters. These are represented in the same format used for
2921 DECL_TEMPLATE_PARMS. */
2924 comp_template_parms (const_tree parms1, const_tree parms2)
2926 const_tree p1;
2927 const_tree p2;
2929 if (parms1 == parms2)
2930 return 1;
2932 for (p1 = parms1, p2 = parms2;
2933 p1 != NULL_TREE && p2 != NULL_TREE;
2934 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2936 tree t1 = TREE_VALUE (p1);
2937 tree t2 = TREE_VALUE (p2);
2938 int i;
2940 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2941 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2943 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2944 return 0;
2946 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2948 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2949 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2951 /* If either of the template parameters are invalid, assume
2952 they match for the sake of error recovery. */
2953 if (parm1 == error_mark_node || parm2 == error_mark_node)
2954 return 1;
2956 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2957 return 0;
2959 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2960 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2961 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2962 continue;
2963 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2964 return 0;
2968 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2969 /* One set of parameters has more parameters lists than the
2970 other. */
2971 return 0;
2973 return 1;
2976 /* Determine whether PARM is a parameter pack. */
2978 bool
2979 template_parameter_pack_p (const_tree parm)
2981 /* Determine if we have a non-type template parameter pack. */
2982 if (TREE_CODE (parm) == PARM_DECL)
2983 return (DECL_TEMPLATE_PARM_P (parm)
2984 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2985 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2986 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2988 /* If this is a list of template parameters, we could get a
2989 TYPE_DECL or a TEMPLATE_DECL. */
2990 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2991 parm = TREE_TYPE (parm);
2993 /* Otherwise it must be a type template parameter. */
2994 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2995 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2996 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2999 /* Determine if T is a function parameter pack. */
3001 bool
3002 function_parameter_pack_p (const_tree t)
3004 if (t && TREE_CODE (t) == PARM_DECL)
3005 return DECL_PACK_P (t);
3006 return false;
3009 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3010 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3012 tree
3013 get_function_template_decl (const_tree primary_func_tmpl_inst)
3015 if (! primary_func_tmpl_inst
3016 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3017 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3018 return NULL;
3020 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3023 /* Return true iff the function parameter PARAM_DECL was expanded
3024 from the function parameter pack PACK. */
3026 bool
3027 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3029 if (DECL_ARTIFICIAL (param_decl)
3030 || !function_parameter_pack_p (pack))
3031 return false;
3033 /* The parameter pack and its pack arguments have the same
3034 DECL_PARM_INDEX. */
3035 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3038 /* Determine whether ARGS describes a variadic template args list,
3039 i.e., one that is terminated by a template argument pack. */
3041 static bool
3042 template_args_variadic_p (tree args)
3044 int nargs;
3045 tree last_parm;
3047 if (args == NULL_TREE)
3048 return false;
3050 args = INNERMOST_TEMPLATE_ARGS (args);
3051 nargs = TREE_VEC_LENGTH (args);
3053 if (nargs == 0)
3054 return false;
3056 last_parm = TREE_VEC_ELT (args, nargs - 1);
3058 return ARGUMENT_PACK_P (last_parm);
3061 /* Generate a new name for the parameter pack name NAME (an
3062 IDENTIFIER_NODE) that incorporates its */
3064 static tree
3065 make_ith_pack_parameter_name (tree name, int i)
3067 /* Munge the name to include the parameter index. */
3068 #define NUMBUF_LEN 128
3069 char numbuf[NUMBUF_LEN];
3070 char* newname;
3071 int newname_len;
3073 if (name == NULL_TREE)
3074 return name;
3075 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3076 newname_len = IDENTIFIER_LENGTH (name)
3077 + strlen (numbuf) + 2;
3078 newname = (char*)alloca (newname_len);
3079 snprintf (newname, newname_len,
3080 "%s#%i", IDENTIFIER_POINTER (name), i);
3081 return get_identifier (newname);
3084 /* Return true if T is a primary function, class or alias template
3085 instantiation. */
3087 bool
3088 primary_template_instantiation_p (const_tree t)
3090 if (!t)
3091 return false;
3093 if (TREE_CODE (t) == FUNCTION_DECL)
3094 return DECL_LANG_SPECIFIC (t)
3095 && DECL_TEMPLATE_INSTANTIATION (t)
3096 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3097 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3098 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3099 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3100 else if (alias_template_specialization_p (t))
3101 return true;
3102 return false;
3105 /* Return true if PARM is a template template parameter. */
3107 bool
3108 template_template_parameter_p (const_tree parm)
3110 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3113 /* Return true iff PARM is a DECL representing a type template
3114 parameter. */
3116 bool
3117 template_type_parameter_p (const_tree parm)
3119 return (parm
3120 && (TREE_CODE (parm) == TYPE_DECL
3121 || TREE_CODE (parm) == TEMPLATE_DECL)
3122 && DECL_TEMPLATE_PARM_P (parm));
3125 /* Return the template parameters of T if T is a
3126 primary template instantiation, NULL otherwise. */
3128 tree
3129 get_primary_template_innermost_parameters (const_tree t)
3131 tree parms = NULL, template_info = NULL;
3133 if ((template_info = get_template_info (t))
3134 && primary_template_instantiation_p (t))
3135 parms = INNERMOST_TEMPLATE_PARMS
3136 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3138 return parms;
3141 /* Return the template parameters of the LEVELth level from the full list
3142 of template parameters PARMS. */
3144 tree
3145 get_template_parms_at_level (tree parms, int level)
3147 tree p;
3148 if (!parms
3149 || TREE_CODE (parms) != TREE_LIST
3150 || level > TMPL_PARMS_DEPTH (parms))
3151 return NULL_TREE;
3153 for (p = parms; p; p = TREE_CHAIN (p))
3154 if (TMPL_PARMS_DEPTH (p) == level)
3155 return p;
3157 return NULL_TREE;
3160 /* Returns the template arguments of T if T is a template instantiation,
3161 NULL otherwise. */
3163 tree
3164 get_template_innermost_arguments (const_tree t)
3166 tree args = NULL, template_info = NULL;
3168 if ((template_info = get_template_info (t))
3169 && TI_ARGS (template_info))
3170 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3172 return args;
3175 /* Return the argument pack elements of T if T is a template argument pack,
3176 NULL otherwise. */
3178 tree
3179 get_template_argument_pack_elems (const_tree t)
3181 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3182 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3183 return NULL;
3185 return ARGUMENT_PACK_ARGS (t);
3188 /* Structure used to track the progress of find_parameter_packs_r. */
3189 struct find_parameter_pack_data
3191 /* TREE_LIST that will contain all of the parameter packs found by
3192 the traversal. */
3193 tree* parameter_packs;
3195 /* Set of AST nodes that have been visited by the traversal. */
3196 struct pointer_set_t *visited;
3199 /* Identifies all of the argument packs that occur in a template
3200 argument and appends them to the TREE_LIST inside DATA, which is a
3201 find_parameter_pack_data structure. This is a subroutine of
3202 make_pack_expansion and uses_parameter_packs. */
3203 static tree
3204 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3206 tree t = *tp;
3207 struct find_parameter_pack_data* ppd =
3208 (struct find_parameter_pack_data*)data;
3209 bool parameter_pack_p = false;
3211 /* Handle type aliases/typedefs. */
3212 if (TYPE_ALIAS_P (t))
3214 if (TYPE_TEMPLATE_INFO (t))
3215 cp_walk_tree (&TYPE_TI_ARGS (t),
3216 &find_parameter_packs_r,
3217 ppd, ppd->visited);
3218 *walk_subtrees = 0;
3219 return NULL_TREE;
3222 /* Identify whether this is a parameter pack or not. */
3223 switch (TREE_CODE (t))
3225 case TEMPLATE_PARM_INDEX:
3226 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3227 parameter_pack_p = true;
3228 break;
3230 case TEMPLATE_TYPE_PARM:
3231 t = TYPE_MAIN_VARIANT (t);
3232 case TEMPLATE_TEMPLATE_PARM:
3233 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3234 parameter_pack_p = true;
3235 break;
3237 case FIELD_DECL:
3238 case PARM_DECL:
3239 if (DECL_PACK_P (t))
3241 /* We don't want to walk into the type of a PARM_DECL,
3242 because we don't want to see the type parameter pack. */
3243 *walk_subtrees = 0;
3244 parameter_pack_p = true;
3246 break;
3248 /* Look through a lambda capture proxy to the field pack. */
3249 case VAR_DECL:
3250 if (DECL_HAS_VALUE_EXPR_P (t))
3252 tree v = DECL_VALUE_EXPR (t);
3253 cp_walk_tree (&v,
3254 &find_parameter_packs_r,
3255 ppd, ppd->visited);
3256 *walk_subtrees = 0;
3258 break;
3260 case BASES:
3261 parameter_pack_p = true;
3262 break;
3263 default:
3264 /* Not a parameter pack. */
3265 break;
3268 if (parameter_pack_p)
3270 /* Add this parameter pack to the list. */
3271 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3274 if (TYPE_P (t))
3275 cp_walk_tree (&TYPE_CONTEXT (t),
3276 &find_parameter_packs_r, ppd, ppd->visited);
3278 /* This switch statement will return immediately if we don't find a
3279 parameter pack. */
3280 switch (TREE_CODE (t))
3282 case TEMPLATE_PARM_INDEX:
3283 return NULL_TREE;
3285 case BOUND_TEMPLATE_TEMPLATE_PARM:
3286 /* Check the template itself. */
3287 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3288 &find_parameter_packs_r, ppd, ppd->visited);
3289 /* Check the template arguments. */
3290 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3291 ppd->visited);
3292 *walk_subtrees = 0;
3293 return NULL_TREE;
3295 case TEMPLATE_TYPE_PARM:
3296 case TEMPLATE_TEMPLATE_PARM:
3297 return NULL_TREE;
3299 case PARM_DECL:
3300 return NULL_TREE;
3302 case RECORD_TYPE:
3303 if (TYPE_PTRMEMFUNC_P (t))
3304 return NULL_TREE;
3305 /* Fall through. */
3307 case UNION_TYPE:
3308 case ENUMERAL_TYPE:
3309 if (TYPE_TEMPLATE_INFO (t))
3310 cp_walk_tree (&TYPE_TI_ARGS (t),
3311 &find_parameter_packs_r, ppd, ppd->visited);
3313 *walk_subtrees = 0;
3314 return NULL_TREE;
3316 case CONSTRUCTOR:
3317 case TEMPLATE_DECL:
3318 cp_walk_tree (&TREE_TYPE (t),
3319 &find_parameter_packs_r, ppd, ppd->visited);
3320 return NULL_TREE;
3322 case TYPENAME_TYPE:
3323 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3324 ppd, ppd->visited);
3325 *walk_subtrees = 0;
3326 return NULL_TREE;
3328 case TYPE_PACK_EXPANSION:
3329 case EXPR_PACK_EXPANSION:
3330 *walk_subtrees = 0;
3331 return NULL_TREE;
3333 case INTEGER_TYPE:
3334 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3335 ppd, ppd->visited);
3336 *walk_subtrees = 0;
3337 return NULL_TREE;
3339 case IDENTIFIER_NODE:
3340 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3341 ppd->visited);
3342 *walk_subtrees = 0;
3343 return NULL_TREE;
3345 default:
3346 return NULL_TREE;
3349 return NULL_TREE;
3352 /* Determines if the expression or type T uses any parameter packs. */
3353 bool
3354 uses_parameter_packs (tree t)
3356 tree parameter_packs = NULL_TREE;
3357 struct find_parameter_pack_data ppd;
3358 ppd.parameter_packs = &parameter_packs;
3359 ppd.visited = pointer_set_create ();
3360 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3361 pointer_set_destroy (ppd.visited);
3362 return parameter_packs != NULL_TREE;
3365 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3366 representation a base-class initializer into a parameter pack
3367 expansion. If all goes well, the resulting node will be an
3368 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3369 respectively. */
3370 tree
3371 make_pack_expansion (tree arg)
3373 tree result;
3374 tree parameter_packs = NULL_TREE;
3375 bool for_types = false;
3376 struct find_parameter_pack_data ppd;
3378 if (!arg || arg == error_mark_node)
3379 return arg;
3381 if (TREE_CODE (arg) == TREE_LIST)
3383 /* The only time we will see a TREE_LIST here is for a base
3384 class initializer. In this case, the TREE_PURPOSE will be a
3385 _TYPE node (representing the base class expansion we're
3386 initializing) and the TREE_VALUE will be a TREE_LIST
3387 containing the initialization arguments.
3389 The resulting expansion looks somewhat different from most
3390 expansions. Rather than returning just one _EXPANSION, we
3391 return a TREE_LIST whose TREE_PURPOSE is a
3392 TYPE_PACK_EXPANSION containing the bases that will be
3393 initialized. The TREE_VALUE will be identical to the
3394 original TREE_VALUE, which is a list of arguments that will
3395 be passed to each base. We do not introduce any new pack
3396 expansion nodes into the TREE_VALUE (although it is possible
3397 that some already exist), because the TREE_PURPOSE and
3398 TREE_VALUE all need to be expanded together with the same
3399 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3400 resulting TREE_PURPOSE will mention the parameter packs in
3401 both the bases and the arguments to the bases. */
3402 tree purpose;
3403 tree value;
3404 tree parameter_packs = NULL_TREE;
3406 /* Determine which parameter packs will be used by the base
3407 class expansion. */
3408 ppd.visited = pointer_set_create ();
3409 ppd.parameter_packs = &parameter_packs;
3410 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3411 &ppd, ppd.visited);
3413 if (parameter_packs == NULL_TREE)
3415 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3416 pointer_set_destroy (ppd.visited);
3417 return error_mark_node;
3420 if (TREE_VALUE (arg) != void_type_node)
3422 /* Collect the sets of parameter packs used in each of the
3423 initialization arguments. */
3424 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3426 /* Determine which parameter packs will be expanded in this
3427 argument. */
3428 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3429 &ppd, ppd.visited);
3433 pointer_set_destroy (ppd.visited);
3435 /* Create the pack expansion type for the base type. */
3436 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3437 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3438 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3440 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3441 they will rarely be compared to anything. */
3442 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3444 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3447 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3448 for_types = true;
3450 /* Build the PACK_EXPANSION_* node. */
3451 result = for_types
3452 ? cxx_make_type (TYPE_PACK_EXPANSION)
3453 : make_node (EXPR_PACK_EXPANSION);
3454 SET_PACK_EXPANSION_PATTERN (result, arg);
3455 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3457 /* Propagate type and const-expression information. */
3458 TREE_TYPE (result) = TREE_TYPE (arg);
3459 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3461 else
3462 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3463 they will rarely be compared to anything. */
3464 SET_TYPE_STRUCTURAL_EQUALITY (result);
3466 /* Determine which parameter packs will be expanded. */
3467 ppd.parameter_packs = &parameter_packs;
3468 ppd.visited = pointer_set_create ();
3469 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3470 pointer_set_destroy (ppd.visited);
3472 /* Make sure we found some parameter packs. */
3473 if (parameter_packs == NULL_TREE)
3475 if (TYPE_P (arg))
3476 error ("expansion pattern %<%T%> contains no argument packs", arg);
3477 else
3478 error ("expansion pattern %<%E%> contains no argument packs", arg);
3479 return error_mark_node;
3481 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3483 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3485 return result;
3488 /* Checks T for any "bare" parameter packs, which have not yet been
3489 expanded, and issues an error if any are found. This operation can
3490 only be done on full expressions or types (e.g., an expression
3491 statement, "if" condition, etc.), because we could have expressions like:
3493 foo(f(g(h(args)))...)
3495 where "args" is a parameter pack. check_for_bare_parameter_packs
3496 should not be called for the subexpressions args, h(args),
3497 g(h(args)), or f(g(h(args))), because we would produce erroneous
3498 error messages.
3500 Returns TRUE and emits an error if there were bare parameter packs,
3501 returns FALSE otherwise. */
3502 bool
3503 check_for_bare_parameter_packs (tree t)
3505 tree parameter_packs = NULL_TREE;
3506 struct find_parameter_pack_data ppd;
3508 if (!processing_template_decl || !t || t == error_mark_node)
3509 return false;
3511 if (TREE_CODE (t) == TYPE_DECL)
3512 t = TREE_TYPE (t);
3514 ppd.parameter_packs = &parameter_packs;
3515 ppd.visited = pointer_set_create ();
3516 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3517 pointer_set_destroy (ppd.visited);
3519 if (parameter_packs)
3521 error ("parameter packs not expanded with %<...%>:");
3522 while (parameter_packs)
3524 tree pack = TREE_VALUE (parameter_packs);
3525 tree name = NULL_TREE;
3527 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3528 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3529 name = TYPE_NAME (pack);
3530 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3531 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3532 else
3533 name = DECL_NAME (pack);
3535 if (name)
3536 inform (input_location, " %qD", name);
3537 else
3538 inform (input_location, " <anonymous>");
3540 parameter_packs = TREE_CHAIN (parameter_packs);
3543 return true;
3546 return false;
3549 /* Expand any parameter packs that occur in the template arguments in
3550 ARGS. */
3551 tree
3552 expand_template_argument_pack (tree args)
3554 tree result_args = NULL_TREE;
3555 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3556 int num_result_args = -1;
3557 int non_default_args_count = -1;
3559 /* First, determine if we need to expand anything, and the number of
3560 slots we'll need. */
3561 for (in_arg = 0; in_arg < nargs; ++in_arg)
3563 tree arg = TREE_VEC_ELT (args, in_arg);
3564 if (arg == NULL_TREE)
3565 return args;
3566 if (ARGUMENT_PACK_P (arg))
3568 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3569 if (num_result_args < 0)
3570 num_result_args = in_arg + num_packed;
3571 else
3572 num_result_args += num_packed;
3574 else
3576 if (num_result_args >= 0)
3577 num_result_args++;
3581 /* If no expansion is necessary, we're done. */
3582 if (num_result_args < 0)
3583 return args;
3585 /* Expand arguments. */
3586 result_args = make_tree_vec (num_result_args);
3587 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3588 non_default_args_count =
3589 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3590 for (in_arg = 0; in_arg < nargs; ++in_arg)
3592 tree arg = TREE_VEC_ELT (args, in_arg);
3593 if (ARGUMENT_PACK_P (arg))
3595 tree packed = ARGUMENT_PACK_ARGS (arg);
3596 int i, num_packed = TREE_VEC_LENGTH (packed);
3597 for (i = 0; i < num_packed; ++i, ++out_arg)
3598 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3599 if (non_default_args_count > 0)
3600 non_default_args_count += num_packed;
3602 else
3604 TREE_VEC_ELT (result_args, out_arg) = arg;
3605 ++out_arg;
3608 if (non_default_args_count >= 0)
3609 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3610 return result_args;
3613 /* Checks if DECL shadows a template parameter.
3615 [temp.local]: A template-parameter shall not be redeclared within its
3616 scope (including nested scopes).
3618 Emits an error and returns TRUE if the DECL shadows a parameter,
3619 returns FALSE otherwise. */
3621 bool
3622 check_template_shadow (tree decl)
3624 tree olddecl;
3626 /* If we're not in a template, we can't possibly shadow a template
3627 parameter. */
3628 if (!current_template_parms)
3629 return true;
3631 /* Figure out what we're shadowing. */
3632 if (TREE_CODE (decl) == OVERLOAD)
3633 decl = OVL_CURRENT (decl);
3634 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3636 /* If there's no previous binding for this name, we're not shadowing
3637 anything, let alone a template parameter. */
3638 if (!olddecl)
3639 return true;
3641 /* If we're not shadowing a template parameter, we're done. Note
3642 that OLDDECL might be an OVERLOAD (or perhaps even an
3643 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3644 node. */
3645 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3646 return true;
3648 /* We check for decl != olddecl to avoid bogus errors for using a
3649 name inside a class. We check TPFI to avoid duplicate errors for
3650 inline member templates. */
3651 if (decl == olddecl
3652 || (DECL_TEMPLATE_PARM_P (decl)
3653 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3654 return true;
3656 error ("declaration of %q+#D", decl);
3657 error (" shadows template parm %q+#D", olddecl);
3658 return false;
3661 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3662 ORIG_LEVEL, DECL, and TYPE. */
3664 static tree
3665 build_template_parm_index (int index,
3666 int level,
3667 int orig_level,
3668 tree decl,
3669 tree type)
3671 tree t = make_node (TEMPLATE_PARM_INDEX);
3672 TEMPLATE_PARM_IDX (t) = index;
3673 TEMPLATE_PARM_LEVEL (t) = level;
3674 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3675 TEMPLATE_PARM_DECL (t) = decl;
3676 TREE_TYPE (t) = type;
3677 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3678 TREE_READONLY (t) = TREE_READONLY (decl);
3680 return t;
3683 /* Find the canonical type parameter for the given template type
3684 parameter. Returns the canonical type parameter, which may be TYPE
3685 if no such parameter existed. */
3687 static tree
3688 canonical_type_parameter (tree type)
3690 tree list;
3691 int idx = TEMPLATE_TYPE_IDX (type);
3692 if (!canonical_template_parms)
3693 vec_alloc (canonical_template_parms, idx+1);
3695 while (canonical_template_parms->length () <= (unsigned)idx)
3696 vec_safe_push (canonical_template_parms, NULL_TREE);
3698 list = (*canonical_template_parms)[idx];
3699 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3700 list = TREE_CHAIN (list);
3702 if (list)
3703 return TREE_VALUE (list);
3704 else
3706 (*canonical_template_parms)[idx]
3707 = tree_cons (NULL_TREE, type,
3708 (*canonical_template_parms)[idx]);
3709 return type;
3713 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3714 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3715 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3716 new one is created. */
3718 static tree
3719 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3720 tsubst_flags_t complain)
3722 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3723 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3724 != TEMPLATE_PARM_LEVEL (index) - levels)
3725 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3727 tree orig_decl = TEMPLATE_PARM_DECL (index);
3728 tree decl, t;
3730 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3731 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3732 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3733 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3734 DECL_ARTIFICIAL (decl) = 1;
3735 SET_DECL_TEMPLATE_PARM_P (decl);
3737 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3738 TEMPLATE_PARM_LEVEL (index) - levels,
3739 TEMPLATE_PARM_ORIG_LEVEL (index),
3740 decl, type);
3741 TEMPLATE_PARM_DESCENDANTS (index) = t;
3742 TEMPLATE_PARM_PARAMETER_PACK (t)
3743 = TEMPLATE_PARM_PARAMETER_PACK (index);
3745 /* Template template parameters need this. */
3746 if (TREE_CODE (decl) == TEMPLATE_DECL)
3747 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3748 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3749 args, complain);
3752 return TEMPLATE_PARM_DESCENDANTS (index);
3755 /* Process information from new template parameter PARM and append it
3756 to the LIST being built. This new parameter is a non-type
3757 parameter iff IS_NON_TYPE is true. This new parameter is a
3758 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3759 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3760 parameter list PARM belongs to. This is used used to create a
3761 proper canonical type for the type of PARM that is to be created,
3762 iff PARM is a type. If the size is not known, this parameter shall
3763 be set to 0. */
3765 tree
3766 process_template_parm (tree list, location_t parm_loc, tree parm,
3767 bool is_non_type, bool is_parameter_pack)
3769 tree decl = 0;
3770 tree err_parm_list;
3771 int idx = 0;
3773 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3774 tree defval = TREE_PURPOSE (parm);
3775 tree constr = TREE_TYPE (parm);
3776 tree reqs = NULL_TREE;
3778 if (list)
3780 tree p = tree_last (list);
3782 if (p && TREE_VALUE (p) != error_mark_node)
3784 p = TREE_VALUE (p);
3785 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3786 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3787 else
3788 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3791 ++idx;
3793 else
3794 idx = 0;
3796 if (is_non_type)
3798 parm = TREE_VALUE (parm);
3800 SET_DECL_TEMPLATE_PARM_P (parm);
3802 if (TREE_TYPE (parm) == error_mark_node)
3804 err_parm_list = build_tree_list (defval, parm);
3805 TREE_VALUE (err_parm_list) = error_mark_node;
3806 return chainon (list, err_parm_list);
3808 else
3810 /* [temp.param]
3812 The top-level cv-qualifiers on the template-parameter are
3813 ignored when determining its type. */
3814 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3815 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3817 err_parm_list = build_tree_list (defval, parm);
3818 TREE_VALUE (err_parm_list) = error_mark_node;
3819 return chainon (list, err_parm_list);
3822 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3824 /* This template parameter is not a parameter pack, but it
3825 should be. Complain about "bare" parameter packs. */
3826 check_for_bare_parameter_packs (TREE_TYPE (parm));
3828 /* Recover by calling this a parameter pack. */
3829 is_parameter_pack = true;
3833 /* A template parameter is not modifiable. */
3834 TREE_CONSTANT (parm) = 1;
3835 TREE_READONLY (parm) = 1;
3836 decl = build_decl (parm_loc,
3837 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3838 TREE_CONSTANT (decl) = 1;
3839 TREE_READONLY (decl) = 1;
3840 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3841 = build_template_parm_index (idx, processing_template_decl,
3842 processing_template_decl,
3843 decl, TREE_TYPE (parm));
3845 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3846 = is_parameter_pack;
3848 // Build requirements for the parameter.
3849 reqs = finish_shorthand_requirement (parm, constr);
3851 else
3853 tree t;
3854 parm = TREE_VALUE (TREE_VALUE (parm));
3856 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3858 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3859 /* This is for distinguishing between real templates and template
3860 template parameters */
3861 TREE_TYPE (parm) = t;
3862 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3863 decl = parm;
3865 else
3867 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3868 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3869 decl = build_decl (parm_loc,
3870 TYPE_DECL, parm, t);
3873 TYPE_NAME (t) = decl;
3874 TYPE_STUB_DECL (t) = decl;
3875 parm = decl;
3876 TEMPLATE_TYPE_PARM_INDEX (t)
3877 = build_template_parm_index (idx, processing_template_decl,
3878 processing_template_decl,
3879 decl, TREE_TYPE (parm));
3880 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3881 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3883 // Build requirements for the type/template parameter.
3884 reqs = finish_shorthand_requirement (parm, constr);
3886 DECL_ARTIFICIAL (decl) = 1;
3887 SET_DECL_TEMPLATE_PARM_P (decl);
3888 pushdecl (decl);
3890 // Build the parameter node linking the parameter declaration, its
3891 // default argument (if any), and its constraints (if any).
3892 parm = build_tree_list (defval, parm);
3893 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
3895 return chainon (list, parm);
3898 /* The end of a template parameter list has been reached. Process the
3899 tree list into a parameter vector, converting each parameter into a more
3900 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3901 as PARM_DECLs. */
3903 tree
3904 end_template_parm_list (tree parms)
3906 int nparms;
3907 tree parm, next;
3908 tree saved_parmlist = make_tree_vec (list_length (parms));
3910 current_template_parms
3911 = tree_cons (size_int (processing_template_decl),
3912 saved_parmlist, current_template_parms);
3914 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3916 next = TREE_CHAIN (parm);
3917 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3918 TREE_CHAIN (parm) = NULL_TREE;
3921 --processing_template_parmlist;
3923 return saved_parmlist;
3926 // Explicitly indicate the end of the template parameter list. We assume
3927 // that the current template parameters have been constructed and/or
3928 // managed explicitly, as when creating new template template parameters
3929 // from a shorthand constraint.
3930 void
3931 end_template_parm_list ()
3933 --processing_template_parmlist;
3936 /* end_template_decl is called after a template declaration is seen. */
3938 void
3939 end_template_decl (void)
3941 reset_specialization ();
3943 if (! processing_template_decl)
3944 return;
3946 /* This matches the pushlevel in begin_template_parm_list. */
3947 finish_scope ();
3949 --processing_template_decl;
3950 current_template_parms = TREE_CHAIN (current_template_parms);
3953 /* Takes a TREE_LIST representing a template parameter and convert it
3954 into an argument suitable to be passed to the type substitution
3955 functions. Note that If the TREE_LIST contains an error_mark
3956 node, the returned argument is error_mark_node. */
3958 tree
3959 template_parm_to_arg (tree t)
3962 if (t == NULL_TREE
3963 || TREE_CODE (t) != TREE_LIST)
3964 return t;
3966 if (error_operand_p (TREE_VALUE (t)))
3967 return error_mark_node;
3969 t = TREE_VALUE (t);
3971 if (TREE_CODE (t) == TYPE_DECL
3972 || TREE_CODE (t) == TEMPLATE_DECL)
3974 t = TREE_TYPE (t);
3976 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3978 /* Turn this argument into a TYPE_ARGUMENT_PACK
3979 with a single element, which expands T. */
3980 tree vec = make_tree_vec (1);
3981 #ifdef ENABLE_CHECKING
3982 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3983 (vec, TREE_VEC_LENGTH (vec));
3984 #endif
3985 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3987 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3988 SET_ARGUMENT_PACK_ARGS (t, vec);
3991 else
3993 t = DECL_INITIAL (t);
3995 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3997 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3998 with a single element, which expands T. */
3999 tree vec = make_tree_vec (1);
4000 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4001 #ifdef ENABLE_CHECKING
4002 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4003 (vec, TREE_VEC_LENGTH (vec));
4004 #endif
4005 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4007 t = make_node (NONTYPE_ARGUMENT_PACK);
4008 SET_ARGUMENT_PACK_ARGS (t, vec);
4009 TREE_TYPE (t) = type;
4012 return t;
4015 /* Given a set of template parameters, return them as a set of template
4016 arguments. The template parameters are represented as a TREE_VEC, in
4017 the form documented in cp-tree.h for template arguments. */
4019 static tree
4020 template_parms_to_args (tree parms)
4022 tree header;
4023 tree args = NULL_TREE;
4024 int length = TMPL_PARMS_DEPTH (parms);
4025 int l = length;
4027 /* If there is only one level of template parameters, we do not
4028 create a TREE_VEC of TREE_VECs. Instead, we return a single
4029 TREE_VEC containing the arguments. */
4030 if (length > 1)
4031 args = make_tree_vec (length);
4033 for (header = parms; header; header = TREE_CHAIN (header))
4035 tree a = copy_node (TREE_VALUE (header));
4036 int i;
4038 TREE_TYPE (a) = NULL_TREE;
4039 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4040 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4042 #ifdef ENABLE_CHECKING
4043 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4044 #endif
4046 if (length > 1)
4047 TREE_VEC_ELT (args, --l) = a;
4048 else
4049 args = a;
4052 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
4053 /* This can happen for template parms of a template template
4054 parameter, e.g:
4056 template<template<class T, class U> class TT> struct S;
4058 Consider the level of the parms of TT; T and U both have
4059 level 2; TT has no template parm of level 1. So in this case
4060 the first element of full_template_args is NULL_TREE. If we
4061 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4062 of 2. This will make tsubst wrongly consider that T and U
4063 have level 1. Instead, let's create a dummy vector as the
4064 first element of full_template_args so that TMPL_ARGS_DEPTH
4065 returns the correct depth for args. */
4066 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4067 return args;
4070 /* Within the declaration of a template, return the currently active
4071 template parameters as an argument TREE_VEC. */
4073 static tree
4074 current_template_args (void)
4076 return template_parms_to_args (current_template_parms);
4079 /* Update the declared TYPE by doing any lookups which were thought to be
4080 dependent, but are not now that we know the SCOPE of the declarator. */
4082 tree
4083 maybe_update_decl_type (tree orig_type, tree scope)
4085 tree type = orig_type;
4087 if (type == NULL_TREE)
4088 return type;
4090 if (TREE_CODE (orig_type) == TYPE_DECL)
4091 type = TREE_TYPE (type);
4093 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4094 && dependent_type_p (type)
4095 /* Don't bother building up the args in this case. */
4096 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4098 /* tsubst in the args corresponding to the template parameters,
4099 including auto if present. Most things will be unchanged, but
4100 make_typename_type and tsubst_qualified_id will resolve
4101 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4102 tree args = current_template_args ();
4103 tree auto_node = type_uses_auto (type);
4104 tree pushed;
4105 if (auto_node)
4107 tree auto_vec = make_tree_vec (1);
4108 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4109 args = add_to_template_args (args, auto_vec);
4111 pushed = push_scope (scope);
4112 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4113 if (pushed)
4114 pop_scope (scope);
4117 if (type == error_mark_node)
4118 return orig_type;
4120 if (TREE_CODE (orig_type) == TYPE_DECL)
4122 if (same_type_p (type, TREE_TYPE (orig_type)))
4123 type = orig_type;
4124 else
4125 type = TYPE_NAME (type);
4127 return type;
4130 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4131 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4132 the new template is a member template. */
4134 tree
4135 build_template_decl (tree decl, tree parms, tree constr, bool member_template_p)
4137 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4138 DECL_TEMPLATE_PARMS (tmpl) = parms;
4139 DECL_CONSTRAINTS (tmpl) = constr;
4140 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4141 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4142 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4144 return tmpl;
4147 struct template_parm_data
4149 /* The level of the template parameters we are currently
4150 processing. */
4151 int level;
4153 /* The index of the specialization argument we are currently
4154 processing. */
4155 int current_arg;
4157 /* An array whose size is the number of template parameters. The
4158 elements are nonzero if the parameter has been used in any one
4159 of the arguments processed so far. */
4160 int* parms;
4162 /* An array whose size is the number of template arguments. The
4163 elements are nonzero if the argument makes use of template
4164 parameters of this level. */
4165 int* arg_uses_template_parms;
4168 /* Subroutine of push_template_decl used to see if each template
4169 parameter in a partial specialization is used in the explicit
4170 argument list. If T is of the LEVEL given in DATA (which is
4171 treated as a template_parm_data*), then DATA->PARMS is marked
4172 appropriately. */
4174 static int
4175 mark_template_parm (tree t, void* data)
4177 int level;
4178 int idx;
4179 struct template_parm_data* tpd = (struct template_parm_data*) data;
4181 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4183 level = TEMPLATE_PARM_LEVEL (t);
4184 idx = TEMPLATE_PARM_IDX (t);
4186 else
4188 level = TEMPLATE_TYPE_LEVEL (t);
4189 idx = TEMPLATE_TYPE_IDX (t);
4192 if (level == tpd->level)
4194 tpd->parms[idx] = 1;
4195 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4198 /* Return zero so that for_each_template_parm will continue the
4199 traversal of the tree; we want to mark *every* template parm. */
4200 return 0;
4203 /* Process the partial specialization DECL. */
4205 static tree
4206 process_partial_specialization (tree decl)
4208 tree type = TREE_TYPE (decl);
4209 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4210 tree specargs = CLASSTYPE_TI_ARGS (type);
4211 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4212 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4213 tree inner_parms;
4214 tree inst;
4215 int nargs = TREE_VEC_LENGTH (inner_args);
4216 int ntparms;
4217 int i;
4218 bool did_error_intro = false;
4219 struct template_parm_data tpd;
4220 struct template_parm_data tpd2;
4222 gcc_assert (current_template_parms);
4224 // When defining a constrained partial specialization, DECL's type
4225 // will have been assigned to the canonical type of the primary.
4226 // That is:
4228 // template<typename T>
4229 // struct S; // Has canonical type S<T>
4231 // template<typename T>
4232 // requires C<T>
4233 // struct S<T>; // binds to the primary template.
4235 // However, the the constraints differ, so we should be constructing
4236 // a new type. We do this by making the DECL's type its own
4237 // canonical type.
4239 // TODO: Do we need to compare the current requirements to make
4240 // sure this isn't a redeclaration?
4241 if (current_template_reqs)
4242 TYPE_CANONICAL (type) = type;
4244 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4245 ntparms = TREE_VEC_LENGTH (inner_parms);
4247 /* We check that each of the template parameters given in the
4248 partial specialization is used in the argument list to the
4249 specialization. For example:
4251 template <class T> struct S;
4252 template <class T> struct S<T*>;
4254 The second declaration is OK because `T*' uses the template
4255 parameter T, whereas
4257 template <class T> struct S<int>;
4259 is no good. Even trickier is:
4261 template <class T>
4262 struct S1
4264 template <class U>
4265 struct S2;
4266 template <class U>
4267 struct S2<T>;
4270 The S2<T> declaration is actually invalid; it is a
4271 full-specialization. Of course,
4273 template <class U>
4274 struct S2<T (*)(U)>;
4276 or some such would have been OK. */
4277 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4278 tpd.parms = XALLOCAVEC (int, ntparms);
4279 memset (tpd.parms, 0, sizeof (int) * ntparms);
4281 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4282 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4283 for (i = 0; i < nargs; ++i)
4285 tpd.current_arg = i;
4286 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4287 &mark_template_parm,
4288 &tpd,
4289 NULL,
4290 /*include_nondeduced_p=*/false);
4292 for (i = 0; i < ntparms; ++i)
4293 if (tpd.parms[i] == 0)
4295 /* One of the template parms was not used in the
4296 specialization. */
4297 if (!did_error_intro)
4299 error ("template parameters not used in partial specialization:");
4300 did_error_intro = true;
4303 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4306 if (did_error_intro)
4307 return error_mark_node;
4309 /* [temp.class.spec]
4311 The argument list of the specialization shall not be identical to
4312 the implicit argument list of the primary template.
4314 Note that concepts allow partial specializations with the same list of
4315 arguments but different constraints. */
4316 tree main_type = TREE_TYPE (maintmpl);
4317 tree main_args = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (main_type));
4318 tree main_constr = DECL_CONSTRAINTS (maintmpl);
4319 if (comp_template_args (inner_args, main_args)
4320 && equivalent_constraints (current_template_reqs, main_constr))
4321 error ("partial specialization %qT does not specialize any "
4322 "template arguments", type);
4324 /* A partial specialization that replaces multiple parameters of the
4325 primary template with a pack expansion is less specialized for those
4326 parameters. */
4327 if (nargs < DECL_NTPARMS (maintmpl))
4329 error ("partial specialization is not more specialized than the "
4330 "primary template because it replaces multiple parameters "
4331 "with a pack expansion");
4332 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4333 return decl;
4336 /* [temp.class.spec]
4338 A partially specialized non-type argument expression shall not
4339 involve template parameters of the partial specialization except
4340 when the argument expression is a simple identifier.
4342 The type of a template parameter corresponding to a specialized
4343 non-type argument shall not be dependent on a parameter of the
4344 specialization.
4346 Also, we verify that pack expansions only occur at the
4347 end of the argument list. */
4348 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4349 tpd2.parms = 0;
4350 for (i = 0; i < nargs; ++i)
4352 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4353 tree arg = TREE_VEC_ELT (inner_args, i);
4354 tree packed_args = NULL_TREE;
4355 int j, len = 1;
4357 if (ARGUMENT_PACK_P (arg))
4359 /* Extract the arguments from the argument pack. We'll be
4360 iterating over these in the following loop. */
4361 packed_args = ARGUMENT_PACK_ARGS (arg);
4362 len = TREE_VEC_LENGTH (packed_args);
4365 for (j = 0; j < len; j++)
4367 if (packed_args)
4368 /* Get the Jth argument in the parameter pack. */
4369 arg = TREE_VEC_ELT (packed_args, j);
4371 if (PACK_EXPANSION_P (arg))
4373 /* Pack expansions must come at the end of the
4374 argument list. */
4375 if ((packed_args && j < len - 1)
4376 || (!packed_args && i < nargs - 1))
4378 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4379 error ("parameter pack argument %qE must be at the "
4380 "end of the template argument list", arg);
4381 else
4382 error ("parameter pack argument %qT must be at the "
4383 "end of the template argument list", arg);
4387 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4388 /* We only care about the pattern. */
4389 arg = PACK_EXPANSION_PATTERN (arg);
4391 if (/* These first two lines are the `non-type' bit. */
4392 !TYPE_P (arg)
4393 && TREE_CODE (arg) != TEMPLATE_DECL
4394 /* This next line is the `argument expression is not just a
4395 simple identifier' condition and also the `specialized
4396 non-type argument' bit. */
4397 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4399 if ((!packed_args && tpd.arg_uses_template_parms[i])
4400 || (packed_args && uses_template_parms (arg)))
4401 error ("template argument %qE involves template parameter(s)",
4402 arg);
4403 else
4405 /* Look at the corresponding template parameter,
4406 marking which template parameters its type depends
4407 upon. */
4408 tree type = TREE_TYPE (parm);
4410 if (!tpd2.parms)
4412 /* We haven't yet initialized TPD2. Do so now. */
4413 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4414 /* The number of parameters here is the number in the
4415 main template, which, as checked in the assertion
4416 above, is NARGS. */
4417 tpd2.parms = XALLOCAVEC (int, nargs);
4418 tpd2.level =
4419 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4422 /* Mark the template parameters. But this time, we're
4423 looking for the template parameters of the main
4424 template, not in the specialization. */
4425 tpd2.current_arg = i;
4426 tpd2.arg_uses_template_parms[i] = 0;
4427 memset (tpd2.parms, 0, sizeof (int) * nargs);
4428 for_each_template_parm (type,
4429 &mark_template_parm,
4430 &tpd2,
4431 NULL,
4432 /*include_nondeduced_p=*/false);
4434 if (tpd2.arg_uses_template_parms [i])
4436 /* The type depended on some template parameters.
4437 If they are fully specialized in the
4438 specialization, that's OK. */
4439 int j;
4440 int count = 0;
4441 for (j = 0; j < nargs; ++j)
4442 if (tpd2.parms[j] != 0
4443 && tpd.arg_uses_template_parms [j])
4444 ++count;
4445 if (count != 0)
4446 error_n (input_location, count,
4447 "type %qT of template argument %qE depends "
4448 "on a template parameter",
4449 "type %qT of template argument %qE depends "
4450 "on template parameters",
4451 type,
4452 arg);
4459 /* We should only get here once. */
4460 gcc_assert (!COMPLETE_TYPE_P (type));
4462 // Build the template decl.
4463 tree tmpl = build_template_decl (decl,
4464 current_template_parms,
4465 current_template_reqs,
4466 DECL_MEMBER_TEMPLATE_P (maintmpl));
4467 TREE_TYPE (tmpl) = type;
4468 DECL_TEMPLATE_RESULT (tmpl) = decl;
4469 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4470 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4471 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4473 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4474 = tree_cons (specargs, tmpl,
4475 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4476 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4478 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4479 inst = TREE_CHAIN (inst))
4481 tree inst_type = TREE_VALUE (inst);
4482 if (COMPLETE_TYPE_P (inst_type)
4483 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4485 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4486 if (spec && TREE_TYPE (spec) == type)
4487 permerror (input_location,
4488 "partial specialization of %qT after instantiation "
4489 "of %qT", type, inst_type);
4493 return decl;
4496 /* Check that a template declaration's use of default arguments and
4497 parameter packs is not invalid. Here, PARMS are the template
4498 parameters. IS_PRIMARY is true if DECL is the thing declared by
4499 a primary template. IS_PARTIAL is true if DECL is a partial
4500 specialization.
4502 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4503 declaration (but not a definition); 1 indicates a declaration, 2
4504 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4505 emitted for extraneous default arguments.
4507 Returns TRUE if there were no errors found, FALSE otherwise. */
4509 bool
4510 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4511 bool is_partial, int is_friend_decl)
4513 const char *msg;
4514 int last_level_to_check;
4515 tree parm_level;
4516 bool no_errors = true;
4518 /* [temp.param]
4520 A default template-argument shall not be specified in a
4521 function template declaration or a function template definition, nor
4522 in the template-parameter-list of the definition of a member of a
4523 class template. */
4525 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4526 /* You can't have a function template declaration in a local
4527 scope, nor you can you define a member of a class template in a
4528 local scope. */
4529 return true;
4531 if (TREE_CODE (decl) == TYPE_DECL
4532 && TREE_TYPE (decl)
4533 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4534 /* A lambda doesn't have an explicit declaration; don't complain
4535 about the parms of the enclosing class. */
4536 return true;
4538 if (current_class_type
4539 && !TYPE_BEING_DEFINED (current_class_type)
4540 && DECL_LANG_SPECIFIC (decl)
4541 && DECL_DECLARES_FUNCTION_P (decl)
4542 /* If this is either a friend defined in the scope of the class
4543 or a member function. */
4544 && (DECL_FUNCTION_MEMBER_P (decl)
4545 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4546 : DECL_FRIEND_CONTEXT (decl)
4547 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4548 : false)
4549 /* And, if it was a member function, it really was defined in
4550 the scope of the class. */
4551 && (!DECL_FUNCTION_MEMBER_P (decl)
4552 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4553 /* We already checked these parameters when the template was
4554 declared, so there's no need to do it again now. This function
4555 was defined in class scope, but we're processing its body now
4556 that the class is complete. */
4557 return true;
4559 /* Core issue 226 (C++0x only): the following only applies to class
4560 templates. */
4561 if (is_primary
4562 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4564 /* [temp.param]
4566 If a template-parameter has a default template-argument, all
4567 subsequent template-parameters shall have a default
4568 template-argument supplied. */
4569 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4571 tree inner_parms = TREE_VALUE (parm_level);
4572 int ntparms = TREE_VEC_LENGTH (inner_parms);
4573 int seen_def_arg_p = 0;
4574 int i;
4576 for (i = 0; i < ntparms; ++i)
4578 tree parm = TREE_VEC_ELT (inner_parms, i);
4580 if (parm == error_mark_node)
4581 continue;
4583 if (TREE_PURPOSE (parm))
4584 seen_def_arg_p = 1;
4585 else if (seen_def_arg_p
4586 && !template_parameter_pack_p (TREE_VALUE (parm)))
4588 error ("no default argument for %qD", TREE_VALUE (parm));
4589 /* For better subsequent error-recovery, we indicate that
4590 there should have been a default argument. */
4591 TREE_PURPOSE (parm) = error_mark_node;
4592 no_errors = false;
4594 else if (!is_partial
4595 && !is_friend_decl
4596 /* Don't complain about an enclosing partial
4597 specialization. */
4598 && parm_level == parms
4599 && TREE_CODE (decl) == TYPE_DECL
4600 && i < ntparms - 1
4601 && template_parameter_pack_p (TREE_VALUE (parm)))
4603 /* A primary class template can only have one
4604 parameter pack, at the end of the template
4605 parameter list. */
4607 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4608 error ("parameter pack %qE must be at the end of the"
4609 " template parameter list", TREE_VALUE (parm));
4610 else
4611 error ("parameter pack %qT must be at the end of the"
4612 " template parameter list",
4613 TREE_TYPE (TREE_VALUE (parm)));
4615 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4616 = error_mark_node;
4617 no_errors = false;
4623 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4624 || is_partial
4625 || !is_primary
4626 || is_friend_decl)
4627 /* For an ordinary class template, default template arguments are
4628 allowed at the innermost level, e.g.:
4629 template <class T = int>
4630 struct S {};
4631 but, in a partial specialization, they're not allowed even
4632 there, as we have in [temp.class.spec]:
4634 The template parameter list of a specialization shall not
4635 contain default template argument values.
4637 So, for a partial specialization, or for a function template
4638 (in C++98/C++03), we look at all of them. */
4640 else
4641 /* But, for a primary class template that is not a partial
4642 specialization we look at all template parameters except the
4643 innermost ones. */
4644 parms = TREE_CHAIN (parms);
4646 /* Figure out what error message to issue. */
4647 if (is_friend_decl == 2)
4648 msg = G_("default template arguments may not be used in function template "
4649 "friend re-declaration");
4650 else if (is_friend_decl)
4651 msg = G_("default template arguments may not be used in function template "
4652 "friend declarations");
4653 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4654 msg = G_("default template arguments may not be used in function templates "
4655 "without -std=c++11 or -std=gnu++11");
4656 else if (is_partial)
4657 msg = G_("default template arguments may not be used in "
4658 "partial specializations");
4659 else
4660 msg = G_("default argument for template parameter for class enclosing %qD");
4662 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4663 /* If we're inside a class definition, there's no need to
4664 examine the parameters to the class itself. On the one
4665 hand, they will be checked when the class is defined, and,
4666 on the other, default arguments are valid in things like:
4667 template <class T = double>
4668 struct S { template <class U> void f(U); };
4669 Here the default argument for `S' has no bearing on the
4670 declaration of `f'. */
4671 last_level_to_check = template_class_depth (current_class_type) + 1;
4672 else
4673 /* Check everything. */
4674 last_level_to_check = 0;
4676 for (parm_level = parms;
4677 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4678 parm_level = TREE_CHAIN (parm_level))
4680 tree inner_parms = TREE_VALUE (parm_level);
4681 int i;
4682 int ntparms;
4684 ntparms = TREE_VEC_LENGTH (inner_parms);
4685 for (i = 0; i < ntparms; ++i)
4687 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4688 continue;
4690 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4692 if (msg)
4694 no_errors = false;
4695 if (is_friend_decl == 2)
4696 return no_errors;
4698 error (msg, decl);
4699 msg = 0;
4702 /* Clear out the default argument so that we are not
4703 confused later. */
4704 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4708 /* At this point, if we're still interested in issuing messages,
4709 they must apply to classes surrounding the object declared. */
4710 if (msg)
4711 msg = G_("default argument for template parameter for class "
4712 "enclosing %qD");
4715 return no_errors;
4718 /* Worker for push_template_decl_real, called via
4719 for_each_template_parm. DATA is really an int, indicating the
4720 level of the parameters we are interested in. If T is a template
4721 parameter of that level, return nonzero. */
4723 static int
4724 template_parm_this_level_p (tree t, void* data)
4726 int this_level = *(int *)data;
4727 int level;
4729 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4730 level = TEMPLATE_PARM_LEVEL (t);
4731 else
4732 level = TEMPLATE_TYPE_LEVEL (t);
4733 return level == this_level;
4736 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4737 parameters given by current_template_args, or reuses a
4738 previously existing one, if appropriate. Returns the DECL, or an
4739 equivalent one, if it is replaced via a call to duplicate_decls.
4741 If IS_FRIEND is true, DECL is a friend declaration. */
4743 tree
4744 push_template_decl_real (tree decl, bool is_friend)
4746 tree tmpl;
4747 tree args;
4748 tree info;
4749 tree ctx;
4750 bool is_primary;
4751 bool is_partial;
4752 int new_template_p = 0;
4753 /* True if the template is a member template, in the sense of
4754 [temp.mem]. */
4755 bool member_template_p = false;
4757 if (decl == error_mark_node || !current_template_parms)
4758 return error_mark_node;
4760 /* See if this is a partial specialization. */
4761 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4762 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4763 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4765 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4766 is_friend = true;
4768 if (is_friend)
4769 /* For a friend, we want the context of the friend function, not
4770 the type of which it is a friend. */
4771 ctx = CP_DECL_CONTEXT (decl);
4772 else if (CP_DECL_CONTEXT (decl)
4773 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4774 /* In the case of a virtual function, we want the class in which
4775 it is defined. */
4776 ctx = CP_DECL_CONTEXT (decl);
4777 else
4778 /* Otherwise, if we're currently defining some class, the DECL
4779 is assumed to be a member of the class. */
4780 ctx = current_scope ();
4782 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4783 ctx = NULL_TREE;
4785 if (!DECL_CONTEXT (decl))
4786 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4788 /* See if this is a primary template. */
4789 if (is_friend && ctx)
4790 /* A friend template that specifies a class context, i.e.
4791 template <typename T> friend void A<T>::f();
4792 is not primary. */
4793 is_primary = false;
4794 else
4795 is_primary = template_parm_scope_p ();
4797 if (is_primary)
4799 if (DECL_CLASS_SCOPE_P (decl))
4800 member_template_p = true;
4801 if (TREE_CODE (decl) == TYPE_DECL
4802 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4804 error ("template class without a name");
4805 return error_mark_node;
4807 else if (TREE_CODE (decl) == FUNCTION_DECL)
4809 if (DECL_DESTRUCTOR_P (decl))
4811 /* [temp.mem]
4813 A destructor shall not be a member template. */
4814 error ("destructor %qD declared as member template", decl);
4815 return error_mark_node;
4817 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4818 && (!prototype_p (TREE_TYPE (decl))
4819 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4820 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4821 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4822 == void_list_node)))
4824 /* [basic.stc.dynamic.allocation]
4826 An allocation function can be a function
4827 template. ... Template allocation functions shall
4828 have two or more parameters. */
4829 error ("invalid template declaration of %qD", decl);
4830 return error_mark_node;
4833 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4834 && CLASS_TYPE_P (TREE_TYPE (decl)))
4835 /* OK */;
4836 else if (TREE_CODE (decl) == TYPE_DECL
4837 && TYPE_DECL_ALIAS_P (decl))
4838 /* alias-declaration */
4839 gcc_assert (!DECL_ARTIFICIAL (decl));
4840 else
4842 error ("template declaration of %q#D", decl);
4843 return error_mark_node;
4847 /* Check to see that the rules regarding the use of default
4848 arguments are not being violated. */
4849 check_default_tmpl_args (decl, current_template_parms,
4850 is_primary, is_partial, /*is_friend_decl=*/0);
4852 /* Ensure that there are no parameter packs in the type of this
4853 declaration that have not been expanded. */
4854 if (TREE_CODE (decl) == FUNCTION_DECL)
4856 /* Check each of the arguments individually to see if there are
4857 any bare parameter packs. */
4858 tree type = TREE_TYPE (decl);
4859 tree arg = DECL_ARGUMENTS (decl);
4860 tree argtype = TYPE_ARG_TYPES (type);
4862 while (arg && argtype)
4864 if (!DECL_PACK_P (arg)
4865 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4867 /* This is a PARM_DECL that contains unexpanded parameter
4868 packs. We have already complained about this in the
4869 check_for_bare_parameter_packs call, so just replace
4870 these types with ERROR_MARK_NODE. */
4871 TREE_TYPE (arg) = error_mark_node;
4872 TREE_VALUE (argtype) = error_mark_node;
4875 arg = DECL_CHAIN (arg);
4876 argtype = TREE_CHAIN (argtype);
4879 /* Check for bare parameter packs in the return type and the
4880 exception specifiers. */
4881 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4882 /* Errors were already issued, set return type to int
4883 as the frontend doesn't expect error_mark_node as
4884 the return type. */
4885 TREE_TYPE (type) = integer_type_node;
4886 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4887 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4889 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4890 && TYPE_DECL_ALIAS_P (decl))
4891 ? DECL_ORIGINAL_TYPE (decl)
4892 : TREE_TYPE (decl)))
4894 TREE_TYPE (decl) = error_mark_node;
4895 return error_mark_node;
4898 if (is_partial)
4899 return process_partial_specialization (decl);
4901 args = current_template_args ();
4903 if (!ctx
4904 || TREE_CODE (ctx) == FUNCTION_DECL
4905 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4906 || (TREE_CODE (decl) == TYPE_DECL
4907 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4908 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4910 if (DECL_LANG_SPECIFIC (decl)
4911 && DECL_TEMPLATE_INFO (decl)
4912 && DECL_TI_TEMPLATE (decl))
4913 tmpl = DECL_TI_TEMPLATE (decl);
4914 /* If DECL is a TYPE_DECL for a class-template, then there won't
4915 be DECL_LANG_SPECIFIC. The information equivalent to
4916 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4917 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4918 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4919 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4921 /* Since a template declaration already existed for this
4922 class-type, we must be redeclaring it here. Make sure
4923 that the redeclaration is valid. */
4924 redeclare_class_template (TREE_TYPE (decl),
4925 current_template_parms,
4926 current_template_reqs);
4927 /* We don't need to create a new TEMPLATE_DECL; just use the
4928 one we already had. */
4929 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4931 else
4933 tmpl = build_template_decl (decl,
4934 current_template_parms,
4935 current_template_reqs,
4936 member_template_p);
4937 new_template_p = 1;
4939 if (DECL_LANG_SPECIFIC (decl)
4940 && DECL_TEMPLATE_SPECIALIZATION (decl))
4942 /* A specialization of a member template of a template
4943 class. */
4944 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4945 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4946 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4950 else
4952 tree a, t, current, parms;
4953 int i;
4954 tree tinfo = get_template_info (decl);
4956 if (!tinfo)
4958 error ("template definition of non-template %q#D", decl);
4959 return error_mark_node;
4962 tmpl = TI_TEMPLATE (tinfo);
4964 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4965 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4966 && DECL_TEMPLATE_SPECIALIZATION (decl)
4967 && DECL_MEMBER_TEMPLATE_P (tmpl))
4969 tree new_tmpl;
4971 /* The declaration is a specialization of a member
4972 template, declared outside the class. Therefore, the
4973 innermost template arguments will be NULL, so we
4974 replace them with the arguments determined by the
4975 earlier call to check_explicit_specialization. */
4976 args = DECL_TI_ARGS (decl);
4978 new_tmpl = build_template_decl (decl,
4979 current_template_parms,
4980 current_template_reqs,
4981 member_template_p);
4982 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4983 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4984 DECL_TI_TEMPLATE (decl) = new_tmpl;
4985 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4986 DECL_TEMPLATE_INFO (new_tmpl) = build_template_info (tmpl, args);
4988 register_specialization (new_tmpl,
4989 most_general_template (tmpl),
4990 args,
4991 is_friend, 0);
4992 return decl;
4995 /* Make sure the template headers we got make sense. */
4997 parms = DECL_TEMPLATE_PARMS (tmpl);
4998 i = TMPL_PARMS_DEPTH (parms);
4999 if (TMPL_ARGS_DEPTH (args) != i)
5001 error ("expected %d levels of template parms for %q#D, got %d",
5002 i, decl, TMPL_ARGS_DEPTH (args));
5004 else
5005 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5007 a = TMPL_ARGS_LEVEL (args, i);
5008 t = INNERMOST_TEMPLATE_PARMS (parms);
5010 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5012 if (current == decl)
5013 error ("got %d template parameters for %q#D",
5014 TREE_VEC_LENGTH (a), decl);
5015 else
5016 error ("got %d template parameters for %q#T",
5017 TREE_VEC_LENGTH (a), current);
5018 error (" but %d required", TREE_VEC_LENGTH (t));
5019 /* Avoid crash in import_export_decl. */
5020 DECL_INTERFACE_KNOWN (decl) = 1;
5021 return error_mark_node;
5024 if (current == decl)
5025 current = ctx;
5026 else if (current == NULL_TREE)
5027 /* Can happen in erroneous input. */
5028 break;
5029 else
5030 current = get_containing_scope (current);
5033 /* Check that the parms are used in the appropriate qualifying scopes
5034 in the declarator. */
5035 if (!comp_template_args
5036 (TI_ARGS (tinfo),
5037 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5039 error ("\
5040 template arguments to %qD do not match original template %qD",
5041 decl, DECL_TEMPLATE_RESULT (tmpl));
5042 if (!uses_template_parms (TI_ARGS (tinfo)))
5043 inform (input_location, "use template<> for an explicit specialization");
5044 /* Avoid crash in import_export_decl. */
5045 DECL_INTERFACE_KNOWN (decl) = 1;
5046 return error_mark_node;
5050 DECL_TEMPLATE_RESULT (tmpl) = decl;
5051 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5053 /* Push template declarations for global functions and types. Note
5054 that we do not try to push a global template friend declared in a
5055 template class; such a thing may well depend on the template
5056 parameters of the class. */
5057 if (new_template_p && !ctx
5058 && !(is_friend && template_class_depth (current_class_type) > 0))
5060 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5061 if (tmpl == error_mark_node)
5062 return error_mark_node;
5064 /* Hide template friend classes that haven't been declared yet. */
5065 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5067 DECL_ANTICIPATED (tmpl) = 1;
5068 DECL_FRIEND_P (tmpl) = 1;
5072 if (is_primary)
5074 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5075 int i;
5077 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5078 if (DECL_CONV_FN_P (tmpl))
5080 int depth = TMPL_PARMS_DEPTH (parms);
5082 /* It is a conversion operator. See if the type converted to
5083 depends on innermost template operands. */
5085 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5086 depth))
5087 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5090 /* Give template template parms a DECL_CONTEXT of the template
5091 for which they are a parameter. */
5092 parms = INNERMOST_TEMPLATE_PARMS (parms);
5093 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5095 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5096 if (TREE_CODE (parm) == TEMPLATE_DECL)
5097 DECL_CONTEXT (parm) = tmpl;
5101 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5102 back to its most general template. If TMPL is a specialization,
5103 ARGS may only have the innermost set of arguments. Add the missing
5104 argument levels if necessary. */
5105 if (DECL_TEMPLATE_INFO (tmpl))
5106 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5108 info = build_template_info (tmpl, args);
5110 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5111 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5112 else
5114 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5115 retrofit_lang_decl (decl);
5116 if (DECL_LANG_SPECIFIC (decl))
5117 DECL_TEMPLATE_INFO (decl) = info;
5120 return DECL_TEMPLATE_RESULT (tmpl);
5123 tree
5124 push_template_decl (tree decl)
5126 return push_template_decl_real (decl, false);
5129 /* FN is an inheriting constructor that inherits from the constructor
5130 template INHERITED; turn FN into a constructor template with a matching
5131 template header. */
5133 tree
5134 add_inherited_template_parms (tree fn, tree inherited)
5136 tree inner_parms
5137 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5138 inner_parms = copy_node (inner_parms);
5139 tree parms
5140 = tree_cons (size_int (processing_template_decl + 1),
5141 inner_parms, current_template_parms);
5142 tree tmpl = build_template_decl (fn, parms, NULL_TREE, /*member*/true);
5143 tree args = template_parms_to_args (parms);
5145 // If the inherited constructor was constrained, then also
5146 // propagate the constraints to the new declaration by
5147 // rewriting them in terms of the local template parameters.
5148 tree cons = DECL_CONSTRAINTS (inherited);
5149 if (cons)
5151 ++processing_template_decl;
5152 tree reqs = instantiate_requirements (CI_REQUIREMENTS (cons), args);
5153 --processing_template_decl;
5154 DECL_CONSTRAINTS (tmpl) = make_constraints (reqs);
5157 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5158 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5159 DECL_TEMPLATE_RESULT (tmpl) = fn;
5160 DECL_ARTIFICIAL (tmpl) = true;
5161 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5162 return tmpl;
5165 /* Called when a class template TYPE is redeclared with the indicated
5166 template PARMS, e.g.:
5168 template <class T> struct S;
5169 template <class T> struct S {}; */
5171 bool
5172 redeclare_class_template (tree type, tree parms, tree cons)
5174 tree tmpl;
5175 tree tmpl_parms;
5176 int i;
5178 if (!TYPE_TEMPLATE_INFO (type))
5180 error ("%qT is not a template type", type);
5181 return false;
5184 tmpl = TYPE_TI_TEMPLATE (type);
5185 if (!PRIMARY_TEMPLATE_P (tmpl))
5186 /* The type is nested in some template class. Nothing to worry
5187 about here; there are no new template parameters for the nested
5188 type. */
5189 return true;
5191 if (!parms)
5193 error ("template specifiers not specified in declaration of %qD",
5194 tmpl);
5195 return false;
5198 parms = INNERMOST_TEMPLATE_PARMS (parms);
5199 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5201 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5203 error_n (input_location, TREE_VEC_LENGTH (parms),
5204 "redeclared with %d template parameter",
5205 "redeclared with %d template parameters",
5206 TREE_VEC_LENGTH (parms));
5207 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5208 "previous declaration %q+D used %d template parameter",
5209 "previous declaration %q+D used %d template parameters",
5210 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5211 return false;
5214 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5216 tree tmpl_parm;
5217 tree parm;
5218 tree tmpl_default;
5219 tree parm_default;
5221 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5222 || TREE_VEC_ELT (parms, i) == error_mark_node)
5223 continue;
5225 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5226 if (tmpl_parm == error_mark_node)
5227 return false;
5229 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5230 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5231 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5233 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5234 TEMPLATE_DECL. */
5235 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5236 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5237 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5238 || (TREE_CODE (tmpl_parm) != PARM_DECL
5239 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5240 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5241 || (TREE_CODE (tmpl_parm) == PARM_DECL
5242 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5243 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5245 error ("template parameter %q+#D", tmpl_parm);
5246 error ("redeclared here as %q#D", parm);
5247 return false;
5250 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5252 /* We have in [temp.param]:
5254 A template-parameter may not be given default arguments
5255 by two different declarations in the same scope. */
5256 error_at (input_location, "redefinition of default argument for %q#D", parm);
5257 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5258 "original definition appeared here");
5259 return false;
5262 if (parm_default != NULL_TREE)
5263 /* Update the previous template parameters (which are the ones
5264 that will really count) with the new default value. */
5265 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5266 else if (tmpl_default != NULL_TREE)
5267 /* Update the new parameters, too; they'll be used as the
5268 parameters for any members. */
5269 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5272 // Cannot redeclare a class template with a different set of constraints.
5273 if (!equivalent_constraints (DECL_CONSTRAINTS (tmpl), cons))
5275 error_at (input_location, "redeclaration %q#D with different "
5276 "constraints", tmpl);
5277 inform (DECL_SOURCE_LOCATION (tmpl),
5278 "original declaration appeared here");
5281 return true;
5284 /* Simplify EXPR if it is a non-dependent expression. Returns the
5285 (possibly simplified) expression. */
5287 tree
5288 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5290 if (expr == NULL_TREE)
5291 return NULL_TREE;
5293 /* If we're in a template, but EXPR isn't value dependent, simplify
5294 it. We're supposed to treat:
5296 template <typename T> void f(T[1 + 1]);
5297 template <typename T> void f(T[2]);
5299 as two declarations of the same function, for example. */
5300 if (processing_template_decl
5301 && !instantiation_dependent_expression_p (expr)
5302 && potential_constant_expression (expr))
5304 HOST_WIDE_INT saved_processing_template_decl;
5306 saved_processing_template_decl = processing_template_decl;
5307 processing_template_decl = 0;
5308 expr = tsubst_copy_and_build (expr,
5309 /*args=*/NULL_TREE,
5310 complain,
5311 /*in_decl=*/NULL_TREE,
5312 /*function_p=*/false,
5313 /*integral_constant_expression_p=*/true);
5314 processing_template_decl = saved_processing_template_decl;
5316 return expr;
5319 tree
5320 fold_non_dependent_expr (tree expr)
5322 return fold_non_dependent_expr_sfinae (expr, tf_error);
5325 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5326 template declaration, or a TYPE_DECL for an alias declaration. */
5328 bool
5329 alias_type_or_template_p (tree t)
5331 if (t == NULL_TREE)
5332 return false;
5333 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5334 || (TYPE_P (t)
5335 && TYPE_NAME (t)
5336 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5337 || DECL_ALIAS_TEMPLATE_P (t));
5340 /* Return TRUE iff is a specialization of an alias template. */
5342 bool
5343 alias_template_specialization_p (const_tree t)
5345 if (t == NULL_TREE)
5346 return false;
5348 return (TYPE_P (t)
5349 && TYPE_TEMPLATE_INFO (t)
5350 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5351 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5354 /* Return either TMPL or another template that it is equivalent to under DR
5355 1286: An alias that just changes the name of a template is equivalent to
5356 the other template. */
5358 static tree
5359 get_underlying_template (tree tmpl)
5361 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5362 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5364 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5365 if (TYPE_TEMPLATE_INFO (result))
5367 tree sub = TYPE_TI_TEMPLATE (result);
5368 if (PRIMARY_TEMPLATE_P (sub)
5369 && same_type_p (result, TREE_TYPE (sub)))
5371 /* The alias type is equivalent to the pattern of the
5372 underlying template, so strip the alias. */
5373 tmpl = sub;
5374 continue;
5377 break;
5379 return tmpl;
5382 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5383 must be a function or a pointer-to-function type, as specified
5384 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5385 and check that the resulting function has external linkage. */
5387 static tree
5388 convert_nontype_argument_function (tree type, tree expr)
5390 tree fns = expr;
5391 tree fn, fn_no_ptr;
5392 linkage_kind linkage;
5394 fn = instantiate_type (type, fns, tf_none);
5395 if (fn == error_mark_node)
5396 return error_mark_node;
5398 fn_no_ptr = fn;
5399 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5400 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5401 if (BASELINK_P (fn_no_ptr))
5402 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5404 /* [temp.arg.nontype]/1
5406 A template-argument for a non-type, non-template template-parameter
5407 shall be one of:
5408 [...]
5409 -- the address of an object or function with external [C++11: or
5410 internal] linkage. */
5412 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5414 error ("%qE is not a valid template argument for type %qT", expr, type);
5415 if (TYPE_PTR_P (type))
5416 error ("it must be the address of a function with external linkage");
5417 else
5418 error ("it must be the name of a function with external linkage");
5419 return NULL_TREE;
5422 linkage = decl_linkage (fn_no_ptr);
5423 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5425 if (cxx_dialect >= cxx11)
5426 error ("%qE is not a valid template argument for type %qT "
5427 "because %qD has no linkage",
5428 expr, type, fn_no_ptr);
5429 else
5430 error ("%qE is not a valid template argument for type %qT "
5431 "because %qD does not have external linkage",
5432 expr, type, fn_no_ptr);
5433 return NULL_TREE;
5436 return fn;
5439 /* Subroutine of convert_nontype_argument.
5440 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5441 Emit an error otherwise. */
5443 static bool
5444 check_valid_ptrmem_cst_expr (tree type, tree expr,
5445 tsubst_flags_t complain)
5447 STRIP_NOPS (expr);
5448 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5449 return true;
5450 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5451 return true;
5452 if (complain & tf_error)
5454 error ("%qE is not a valid template argument for type %qT",
5455 expr, type);
5456 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5458 return false;
5461 /* Returns TRUE iff the address of OP is value-dependent.
5463 14.6.2.4 [temp.dep.temp]:
5464 A non-integral non-type template-argument is dependent if its type is
5465 dependent or it has either of the following forms
5466 qualified-id
5467 & qualified-id
5468 and contains a nested-name-specifier which specifies a class-name that
5469 names a dependent type.
5471 We generalize this to just say that the address of a member of a
5472 dependent class is value-dependent; the above doesn't cover the
5473 address of a static data member named with an unqualified-id. */
5475 static bool
5476 has_value_dependent_address (tree op)
5478 /* We could use get_inner_reference here, but there's no need;
5479 this is only relevant for template non-type arguments, which
5480 can only be expressed as &id-expression. */
5481 if (DECL_P (op))
5483 tree ctx = CP_DECL_CONTEXT (op);
5484 if (TYPE_P (ctx) && dependent_type_p (ctx))
5485 return true;
5488 return false;
5491 /* The next set of functions are used for providing helpful explanatory
5492 diagnostics for failed overload resolution. Their messages should be
5493 indented by two spaces for consistency with the messages in
5494 call.c */
5496 static int
5497 unify_success (bool /*explain_p*/)
5499 return 0;
5502 static int
5503 unify_parameter_deduction_failure (bool explain_p, tree parm)
5505 if (explain_p)
5506 inform (input_location,
5507 " couldn't deduce template parameter %qD", parm);
5508 return 1;
5511 static int
5512 unify_invalid (bool /*explain_p*/)
5514 return 1;
5517 static int
5518 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5520 if (explain_p)
5521 inform (input_location,
5522 " types %qT and %qT have incompatible cv-qualifiers",
5523 parm, arg);
5524 return 1;
5527 static int
5528 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5530 if (explain_p)
5531 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5532 return 1;
5535 static int
5536 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5538 if (explain_p)
5539 inform (input_location,
5540 " template parameter %qD is not a parameter pack, but "
5541 "argument %qD is",
5542 parm, arg);
5543 return 1;
5546 static int
5547 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5549 if (explain_p)
5550 inform (input_location,
5551 " template argument %qE does not match "
5552 "pointer-to-member constant %qE",
5553 arg, parm);
5554 return 1;
5557 static int
5558 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5560 if (explain_p)
5561 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5562 return 1;
5565 static int
5566 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5568 if (explain_p)
5569 inform (input_location,
5570 " inconsistent parameter pack deduction with %qT and %qT",
5571 old_arg, new_arg);
5572 return 1;
5575 static int
5576 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5578 if (explain_p)
5580 if (TYPE_P (parm))
5581 inform (input_location,
5582 " deduced conflicting types for parameter %qT (%qT and %qT)",
5583 parm, first, second);
5584 else
5585 inform (input_location,
5586 " deduced conflicting values for non-type parameter "
5587 "%qE (%qE and %qE)", parm, first, second);
5589 return 1;
5592 static int
5593 unify_vla_arg (bool explain_p, tree arg)
5595 if (explain_p)
5596 inform (input_location,
5597 " variable-sized array type %qT is not "
5598 "a valid template argument",
5599 arg);
5600 return 1;
5603 static int
5604 unify_method_type_error (bool explain_p, tree arg)
5606 if (explain_p)
5607 inform (input_location,
5608 " member function type %qT is not a valid template argument",
5609 arg);
5610 return 1;
5613 static int
5614 unify_arity (bool explain_p, int have, int wanted)
5616 if (explain_p)
5617 inform_n (input_location, wanted,
5618 " candidate expects %d argument, %d provided",
5619 " candidate expects %d arguments, %d provided",
5620 wanted, have);
5621 return 1;
5624 static int
5625 unify_too_many_arguments (bool explain_p, int have, int wanted)
5627 return unify_arity (explain_p, have, wanted);
5630 static int
5631 unify_too_few_arguments (bool explain_p, int have, int wanted)
5633 return unify_arity (explain_p, have, wanted);
5636 static int
5637 unify_arg_conversion (bool explain_p, tree to_type,
5638 tree from_type, tree arg)
5640 if (explain_p)
5641 inform (EXPR_LOC_OR_HERE (arg),
5642 " cannot convert %qE (type %qT) to type %qT",
5643 arg, from_type, to_type);
5644 return 1;
5647 static int
5648 unify_no_common_base (bool explain_p, enum template_base_result r,
5649 tree parm, tree arg)
5651 if (explain_p)
5652 switch (r)
5654 case tbr_ambiguous_baseclass:
5655 inform (input_location, " %qT is an ambiguous base class of %qT",
5656 parm, arg);
5657 break;
5658 default:
5659 inform (input_location, " %qT is not derived from %qT", arg, parm);
5660 break;
5662 return 1;
5665 static int
5666 unify_inconsistent_template_template_parameters (bool explain_p)
5668 if (explain_p)
5669 inform (input_location,
5670 " template parameters of a template template argument are "
5671 "inconsistent with other deduced template arguments");
5672 return 1;
5675 static int
5676 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5678 if (explain_p)
5679 inform (input_location,
5680 " can't deduce a template for %qT from non-template type %qT",
5681 parm, arg);
5682 return 1;
5685 static int
5686 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5688 if (explain_p)
5689 inform (input_location,
5690 " template argument %qE does not match %qD", arg, parm);
5691 return 1;
5694 static int
5695 unify_overload_resolution_failure (bool explain_p, tree arg)
5697 if (explain_p)
5698 inform (input_location,
5699 " could not resolve address from overloaded function %qE",
5700 arg);
5701 return 1;
5704 /* Attempt to convert the non-type template parameter EXPR to the
5705 indicated TYPE. If the conversion is successful, return the
5706 converted value. If the conversion is unsuccessful, return
5707 NULL_TREE if we issued an error message, or error_mark_node if we
5708 did not. We issue error messages for out-and-out bad template
5709 parameters, but not simply because the conversion failed, since we
5710 might be just trying to do argument deduction. Both TYPE and EXPR
5711 must be non-dependent.
5713 The conversion follows the special rules described in
5714 [temp.arg.nontype], and it is much more strict than an implicit
5715 conversion.
5717 This function is called twice for each template argument (see
5718 lookup_template_class for a more accurate description of this
5719 problem). This means that we need to handle expressions which
5720 are not valid in a C++ source, but can be created from the
5721 first call (for instance, casts to perform conversions). These
5722 hacks can go away after we fix the double coercion problem. */
5724 static tree
5725 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5727 tree expr_type;
5729 /* Detect immediately string literals as invalid non-type argument.
5730 This special-case is not needed for correctness (we would easily
5731 catch this later), but only to provide better diagnostic for this
5732 common user mistake. As suggested by DR 100, we do not mention
5733 linkage issues in the diagnostic as this is not the point. */
5734 /* FIXME we're making this OK. */
5735 if (TREE_CODE (expr) == STRING_CST)
5737 if (complain & tf_error)
5738 error ("%qE is not a valid template argument for type %qT "
5739 "because string literals can never be used in this context",
5740 expr, type);
5741 return NULL_TREE;
5744 /* Add the ADDR_EXPR now for the benefit of
5745 value_dependent_expression_p. */
5746 if (TYPE_PTROBV_P (type)
5747 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5749 expr = decay_conversion (expr, complain);
5750 if (expr == error_mark_node)
5751 return error_mark_node;
5754 /* If we are in a template, EXPR may be non-dependent, but still
5755 have a syntactic, rather than semantic, form. For example, EXPR
5756 might be a SCOPE_REF, rather than the VAR_DECL to which the
5757 SCOPE_REF refers. Preserving the qualifying scope is necessary
5758 so that access checking can be performed when the template is
5759 instantiated -- but here we need the resolved form so that we can
5760 convert the argument. */
5761 if (TYPE_REF_OBJ_P (type)
5762 && has_value_dependent_address (expr))
5763 /* If we want the address and it's value-dependent, don't fold. */;
5764 else if (!type_unknown_p (expr))
5765 expr = fold_non_dependent_expr_sfinae (expr, complain);
5766 if (error_operand_p (expr))
5767 return error_mark_node;
5768 expr_type = TREE_TYPE (expr);
5769 if (TREE_CODE (type) == REFERENCE_TYPE)
5770 expr = mark_lvalue_use (expr);
5771 else
5772 expr = mark_rvalue_use (expr);
5774 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5775 to a non-type argument of "nullptr". */
5776 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5777 expr = convert (type, expr);
5779 /* In C++11, integral or enumeration non-type template arguments can be
5780 arbitrary constant expressions. Pointer and pointer to
5781 member arguments can be general constant expressions that evaluate
5782 to a null value, but otherwise still need to be of a specific form. */
5783 if (cxx_dialect >= cxx11)
5785 if (TREE_CODE (expr) == PTRMEM_CST)
5786 /* A PTRMEM_CST is already constant, and a valid template
5787 argument for a parameter of pointer to member type, we just want
5788 to leave it in that form rather than lower it to a
5789 CONSTRUCTOR. */;
5790 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5791 expr = maybe_constant_value (expr);
5792 else if (TYPE_PTR_OR_PTRMEM_P (type))
5794 tree folded = maybe_constant_value (expr);
5795 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5796 : null_member_pointer_value_p (folded))
5797 expr = folded;
5801 /* HACK: Due to double coercion, we can get a
5802 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5803 which is the tree that we built on the first call (see
5804 below when coercing to reference to object or to reference to
5805 function). We just strip everything and get to the arg.
5806 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5807 for examples. */
5808 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5810 tree probe_type, probe = expr;
5811 if (REFERENCE_REF_P (probe))
5812 probe = TREE_OPERAND (probe, 0);
5813 probe_type = TREE_TYPE (probe);
5814 if (TREE_CODE (probe) == NOP_EXPR)
5816 /* ??? Maybe we could use convert_from_reference here, but we
5817 would need to relax its constraints because the NOP_EXPR
5818 could actually change the type to something more cv-qualified,
5819 and this is not folded by convert_from_reference. */
5820 tree addr = TREE_OPERAND (probe, 0);
5821 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5822 && TREE_CODE (addr) == ADDR_EXPR
5823 && TYPE_PTR_P (TREE_TYPE (addr))
5824 && (same_type_ignoring_top_level_qualifiers_p
5825 (TREE_TYPE (probe_type),
5826 TREE_TYPE (TREE_TYPE (addr)))))
5828 expr = TREE_OPERAND (addr, 0);
5829 expr_type = TREE_TYPE (probe_type);
5834 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5835 parameter is a pointer to object, through decay and
5836 qualification conversion. Let's strip everything. */
5837 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5839 tree probe = expr;
5840 STRIP_NOPS (probe);
5841 if (TREE_CODE (probe) == ADDR_EXPR
5842 && TYPE_PTR_P (TREE_TYPE (probe)))
5844 /* Skip the ADDR_EXPR only if it is part of the decay for
5845 an array. Otherwise, it is part of the original argument
5846 in the source code. */
5847 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5848 probe = TREE_OPERAND (probe, 0);
5849 expr = probe;
5850 expr_type = TREE_TYPE (expr);
5854 /* [temp.arg.nontype]/5, bullet 1
5856 For a non-type template-parameter of integral or enumeration type,
5857 integral promotions (_conv.prom_) and integral conversions
5858 (_conv.integral_) are applied. */
5859 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5861 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5862 t = maybe_constant_value (t);
5863 if (t != error_mark_node)
5864 expr = t;
5866 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5867 return error_mark_node;
5869 /* Notice that there are constant expressions like '4 % 0' which
5870 do not fold into integer constants. */
5871 if (TREE_CODE (expr) != INTEGER_CST)
5873 if (complain & tf_error)
5875 int errs = errorcount, warns = warningcount + werrorcount;
5876 if (processing_template_decl
5877 && !require_potential_constant_expression (expr))
5878 return NULL_TREE;
5879 expr = cxx_constant_value (expr);
5880 if (errorcount > errs || warningcount + werrorcount > warns)
5881 inform (EXPR_LOC_OR_HERE (expr),
5882 "in template argument for type %qT ", type);
5883 if (expr == error_mark_node)
5884 return NULL_TREE;
5885 /* else cxx_constant_value complained but gave us
5886 a real constant, so go ahead. */
5887 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5889 else
5890 return NULL_TREE;
5893 /* Avoid typedef problems. */
5894 if (TREE_TYPE (expr) != type)
5895 expr = fold_convert (type, expr);
5897 /* [temp.arg.nontype]/5, bullet 2
5899 For a non-type template-parameter of type pointer to object,
5900 qualification conversions (_conv.qual_) and the array-to-pointer
5901 conversion (_conv.array_) are applied. */
5902 else if (TYPE_PTROBV_P (type))
5904 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5906 A template-argument for a non-type, non-template template-parameter
5907 shall be one of: [...]
5909 -- the name of a non-type template-parameter;
5910 -- the address of an object or function with external linkage, [...]
5911 expressed as "& id-expression" where the & is optional if the name
5912 refers to a function or array, or if the corresponding
5913 template-parameter is a reference.
5915 Here, we do not care about functions, as they are invalid anyway
5916 for a parameter of type pointer-to-object. */
5918 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5919 /* Non-type template parameters are OK. */
5921 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5922 /* Null pointer values are OK in C++11. */;
5923 else if (TREE_CODE (expr) != ADDR_EXPR
5924 && TREE_CODE (expr_type) != ARRAY_TYPE)
5926 if (VAR_P (expr))
5928 error ("%qD is not a valid template argument "
5929 "because %qD is a variable, not the address of "
5930 "a variable",
5931 expr, expr);
5932 return NULL_TREE;
5934 if (POINTER_TYPE_P (expr_type))
5936 error ("%qE is not a valid template argument for %qT "
5937 "because it is not the address of a variable",
5938 expr, type);
5939 return NULL_TREE;
5941 /* Other values, like integer constants, might be valid
5942 non-type arguments of some other type. */
5943 return error_mark_node;
5945 else
5947 tree decl;
5949 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5950 ? TREE_OPERAND (expr, 0) : expr);
5951 if (!VAR_P (decl))
5953 error ("%qE is not a valid template argument of type %qT "
5954 "because %qE is not a variable",
5955 expr, type, decl);
5956 return NULL_TREE;
5958 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5960 error ("%qE is not a valid template argument of type %qT "
5961 "because %qD does not have external linkage",
5962 expr, type, decl);
5963 return NULL_TREE;
5965 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5967 error ("%qE is not a valid template argument of type %qT "
5968 "because %qD has no linkage",
5969 expr, type, decl);
5970 return NULL_TREE;
5974 expr = decay_conversion (expr, complain);
5975 if (expr == error_mark_node)
5976 return error_mark_node;
5978 expr = perform_qualification_conversions (type, expr);
5979 if (expr == error_mark_node)
5980 return error_mark_node;
5982 /* [temp.arg.nontype]/5, bullet 3
5984 For a non-type template-parameter of type reference to object, no
5985 conversions apply. The type referred to by the reference may be more
5986 cv-qualified than the (otherwise identical) type of the
5987 template-argument. The template-parameter is bound directly to the
5988 template-argument, which must be an lvalue. */
5989 else if (TYPE_REF_OBJ_P (type))
5991 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5992 expr_type))
5993 return error_mark_node;
5995 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5997 error ("%qE is not a valid template argument for type %qT "
5998 "because of conflicts in cv-qualification", expr, type);
5999 return NULL_TREE;
6002 if (!real_lvalue_p (expr))
6004 error ("%qE is not a valid template argument for type %qT "
6005 "because it is not an lvalue", expr, type);
6006 return NULL_TREE;
6009 /* [temp.arg.nontype]/1
6011 A template-argument for a non-type, non-template template-parameter
6012 shall be one of: [...]
6014 -- the address of an object or function with external linkage. */
6015 if (INDIRECT_REF_P (expr)
6016 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6018 expr = TREE_OPERAND (expr, 0);
6019 if (DECL_P (expr))
6021 error ("%q#D is not a valid template argument for type %qT "
6022 "because a reference variable does not have a constant "
6023 "address", expr, type);
6024 return NULL_TREE;
6028 if (!DECL_P (expr))
6030 error ("%qE is not a valid template argument for type %qT "
6031 "because it is not an object with external linkage",
6032 expr, type);
6033 return NULL_TREE;
6036 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6038 error ("%qE is not a valid template argument for type %qT "
6039 "because object %qD has not external linkage",
6040 expr, type, expr);
6041 return NULL_TREE;
6044 expr = build_nop (type, build_address (expr));
6046 /* [temp.arg.nontype]/5, bullet 4
6048 For a non-type template-parameter of type pointer to function, only
6049 the function-to-pointer conversion (_conv.func_) is applied. If the
6050 template-argument represents a set of overloaded functions (or a
6051 pointer to such), the matching function is selected from the set
6052 (_over.over_). */
6053 else if (TYPE_PTRFN_P (type))
6055 /* If the argument is a template-id, we might not have enough
6056 context information to decay the pointer. */
6057 if (!type_unknown_p (expr_type))
6059 expr = decay_conversion (expr, complain);
6060 if (expr == error_mark_node)
6061 return error_mark_node;
6064 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6065 /* Null pointer values are OK in C++11. */
6066 return perform_qualification_conversions (type, expr);
6068 expr = convert_nontype_argument_function (type, expr);
6069 if (!expr || expr == error_mark_node)
6070 return expr;
6072 /* [temp.arg.nontype]/5, bullet 5
6074 For a non-type template-parameter of type reference to function, no
6075 conversions apply. If the template-argument represents a set of
6076 overloaded functions, the matching function is selected from the set
6077 (_over.over_). */
6078 else if (TYPE_REFFN_P (type))
6080 if (TREE_CODE (expr) == ADDR_EXPR)
6082 error ("%qE is not a valid template argument for type %qT "
6083 "because it is a pointer", expr, type);
6084 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
6085 return NULL_TREE;
6088 expr = convert_nontype_argument_function (type, expr);
6089 if (!expr || expr == error_mark_node)
6090 return expr;
6092 expr = build_nop (type, build_address (expr));
6094 /* [temp.arg.nontype]/5, bullet 6
6096 For a non-type template-parameter of type pointer to member function,
6097 no conversions apply. If the template-argument represents a set of
6098 overloaded member functions, the matching member function is selected
6099 from the set (_over.over_). */
6100 else if (TYPE_PTRMEMFUNC_P (type))
6102 expr = instantiate_type (type, expr, tf_none);
6103 if (expr == error_mark_node)
6104 return error_mark_node;
6106 /* [temp.arg.nontype] bullet 1 says the pointer to member
6107 expression must be a pointer-to-member constant. */
6108 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6109 return error_mark_node;
6111 /* There is no way to disable standard conversions in
6112 resolve_address_of_overloaded_function (called by
6113 instantiate_type). It is possible that the call succeeded by
6114 converting &B::I to &D::I (where B is a base of D), so we need
6115 to reject this conversion here.
6117 Actually, even if there was a way to disable standard conversions,
6118 it would still be better to reject them here so that we can
6119 provide a superior diagnostic. */
6120 if (!same_type_p (TREE_TYPE (expr), type))
6122 error ("%qE is not a valid template argument for type %qT "
6123 "because it is of type %qT", expr, type,
6124 TREE_TYPE (expr));
6125 /* If we are just one standard conversion off, explain. */
6126 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6127 inform (input_location,
6128 "standard conversions are not allowed in this context");
6129 return NULL_TREE;
6132 /* [temp.arg.nontype]/5, bullet 7
6134 For a non-type template-parameter of type pointer to data member,
6135 qualification conversions (_conv.qual_) are applied. */
6136 else if (TYPE_PTRDATAMEM_P (type))
6138 /* [temp.arg.nontype] bullet 1 says the pointer to member
6139 expression must be a pointer-to-member constant. */
6140 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6141 return error_mark_node;
6143 expr = perform_qualification_conversions (type, expr);
6144 if (expr == error_mark_node)
6145 return expr;
6147 else if (NULLPTR_TYPE_P (type))
6149 if (expr != nullptr_node)
6151 error ("%qE is not a valid template argument for type %qT "
6152 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6153 return NULL_TREE;
6155 return expr;
6157 /* A template non-type parameter must be one of the above. */
6158 else
6159 gcc_unreachable ();
6161 /* Sanity check: did we actually convert the argument to the
6162 right type? */
6163 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6164 (type, TREE_TYPE (expr)));
6165 return expr;
6168 /* Subroutine of coerce_template_template_parms, which returns 1 if
6169 PARM_PARM and ARG_PARM match using the rule for the template
6170 parameters of template template parameters. Both PARM and ARG are
6171 template parameters; the rest of the arguments are the same as for
6172 coerce_template_template_parms.
6174 static int
6175 coerce_template_template_parm (tree parm,
6176 tree arg,
6177 tsubst_flags_t complain,
6178 tree in_decl,
6179 tree outer_args)
6181 if (arg == NULL_TREE || arg == error_mark_node
6182 || parm == NULL_TREE || parm == error_mark_node)
6183 return 0;
6185 if (TREE_CODE (arg) != TREE_CODE (parm))
6186 return 0;
6188 switch (TREE_CODE (parm))
6190 case TEMPLATE_DECL:
6191 /* We encounter instantiations of templates like
6192 template <template <template <class> class> class TT>
6193 class C; */
6195 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6196 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6198 if (!coerce_template_template_parms
6199 (parmparm, argparm, complain, in_decl, outer_args))
6200 return 0;
6202 /* Fall through. */
6204 case TYPE_DECL:
6205 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6206 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6207 /* Argument is a parameter pack but parameter is not. */
6208 return 0;
6209 break;
6211 case PARM_DECL:
6212 /* The tsubst call is used to handle cases such as
6214 template <int> class C {};
6215 template <class T, template <T> class TT> class D {};
6216 D<int, C> d;
6218 i.e. the parameter list of TT depends on earlier parameters. */
6219 if (!uses_template_parms (TREE_TYPE (arg))
6220 && !same_type_p
6221 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6222 TREE_TYPE (arg)))
6223 return 0;
6225 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6226 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6227 /* Argument is a parameter pack but parameter is not. */
6228 return 0;
6230 break;
6232 default:
6233 gcc_unreachable ();
6236 return 1;
6240 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6241 template template parameters. Both PARM_PARMS and ARG_PARMS are
6242 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6243 or PARM_DECL.
6245 Consider the example:
6246 template <class T> class A;
6247 template<template <class U> class TT> class B;
6249 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6250 the parameters to A, and OUTER_ARGS contains A. */
6252 static int
6253 coerce_template_template_parms (tree parm_parms,
6254 tree arg_parms,
6255 tsubst_flags_t complain,
6256 tree in_decl,
6257 tree outer_args)
6259 int nparms, nargs, i;
6260 tree parm, arg;
6261 int variadic_p = 0;
6263 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6264 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6266 nparms = TREE_VEC_LENGTH (parm_parms);
6267 nargs = TREE_VEC_LENGTH (arg_parms);
6269 /* Determine whether we have a parameter pack at the end of the
6270 template template parameter's template parameter list. */
6271 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6273 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6275 if (parm == error_mark_node)
6276 return 0;
6278 switch (TREE_CODE (parm))
6280 case TEMPLATE_DECL:
6281 case TYPE_DECL:
6282 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6283 variadic_p = 1;
6284 break;
6286 case PARM_DECL:
6287 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6288 variadic_p = 1;
6289 break;
6291 default:
6292 gcc_unreachable ();
6296 if (nargs != nparms
6297 && !(variadic_p && nargs >= nparms - 1))
6298 return 0;
6300 /* Check all of the template parameters except the parameter pack at
6301 the end (if any). */
6302 for (i = 0; i < nparms - variadic_p; ++i)
6304 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6305 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6306 continue;
6308 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6309 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6311 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6312 outer_args))
6313 return 0;
6317 if (variadic_p)
6319 /* Check each of the template parameters in the template
6320 argument against the template parameter pack at the end of
6321 the template template parameter. */
6322 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6323 return 0;
6325 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6327 for (; i < nargs; ++i)
6329 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6330 continue;
6332 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6334 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6335 outer_args))
6336 return 0;
6340 return 1;
6343 /* Verifies that the deduced template arguments (in TARGS) for the
6344 template template parameters (in TPARMS) represent valid bindings,
6345 by comparing the template parameter list of each template argument
6346 to the template parameter list of its corresponding template
6347 template parameter, in accordance with DR150. This
6348 routine can only be called after all template arguments have been
6349 deduced. It will return TRUE if all of the template template
6350 parameter bindings are okay, FALSE otherwise. */
6351 bool
6352 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6354 int i, ntparms = TREE_VEC_LENGTH (tparms);
6355 bool ret = true;
6357 /* We're dealing with template parms in this process. */
6358 ++processing_template_decl;
6360 targs = INNERMOST_TEMPLATE_ARGS (targs);
6362 for (i = 0; i < ntparms; ++i)
6364 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6365 tree targ = TREE_VEC_ELT (targs, i);
6367 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6369 tree packed_args = NULL_TREE;
6370 int idx, len = 1;
6372 if (ARGUMENT_PACK_P (targ))
6374 /* Look inside the argument pack. */
6375 packed_args = ARGUMENT_PACK_ARGS (targ);
6376 len = TREE_VEC_LENGTH (packed_args);
6379 for (idx = 0; idx < len; ++idx)
6381 tree targ_parms = NULL_TREE;
6383 if (packed_args)
6384 /* Extract the next argument from the argument
6385 pack. */
6386 targ = TREE_VEC_ELT (packed_args, idx);
6388 if (PACK_EXPANSION_P (targ))
6389 /* Look at the pattern of the pack expansion. */
6390 targ = PACK_EXPANSION_PATTERN (targ);
6392 /* Extract the template parameters from the template
6393 argument. */
6394 if (TREE_CODE (targ) == TEMPLATE_DECL)
6395 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6396 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6397 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6399 /* Verify that we can coerce the template template
6400 parameters from the template argument to the template
6401 parameter. This requires an exact match. */
6402 if (targ_parms
6403 && !coerce_template_template_parms
6404 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6405 targ_parms,
6406 tf_none,
6407 tparm,
6408 targs))
6410 ret = false;
6411 goto out;
6417 out:
6419 --processing_template_decl;
6420 return ret;
6423 /* Since type attributes aren't mangled, we need to strip them from
6424 template type arguments. */
6426 static tree
6427 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6429 tree mv;
6430 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6431 return arg;
6432 mv = TYPE_MAIN_VARIANT (arg);
6433 arg = strip_typedefs (arg);
6434 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6435 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6437 if (complain & tf_warning)
6438 warning (0, "ignoring attributes on template argument %qT", arg);
6439 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6440 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6442 return arg;
6445 // A template declaration can be substituted for a constrained
6446 // template template parameter only when the argument is more
6447 // constrained than the parameter.
6448 static bool
6449 is_compatible_template_arg (tree parm, tree arg)
6451 // TODO: The argument may not have a decl result. This seems to
6452 // happen in some classes with nested template template
6453 // parameters (e.g., a rebind struct in a class taking a template
6454 // template parameter. If this is the case, just return true and
6455 // allow things to happen as they always did.
6456 if (TREE_CODE (arg) == TEMPLATE_DECL)
6458 if (!DECL_TEMPLATE_RESULT (arg))
6459 return true;
6462 tree parmcons = DECL_CONSTRAINTS (parm);
6463 tree argcons = DECL_CONSTRAINTS (arg);
6465 // If the template parameter is constrained, we need to rewrite its
6466 // constraints in terms of the ARG's template parameters. This ensures
6467 // that all of the template parameter types will have the same depth.
6469 // Note that this is only valid when coerce_template_template_parm is
6470 // true for the innermost template parameters of PARM and ARG. In other
6471 // words, because coercion is successful, this conversion will be valid.
6472 if (parmcons)
6474 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
6475 ++processing_template_decl;
6476 tree reqs = instantiate_requirements (CI_REQUIREMENTS (parmcons), args);
6477 --processing_template_decl;
6478 if (reqs == error_mark_node)
6479 return false;
6480 parmcons = make_constraints (reqs);
6483 return more_constraints (argcons, parmcons);
6486 // Convert a placeholder argument into a binding to the original
6487 // parameter. The original parameter is saved as the TREE_TYPE of
6488 // ARG.
6489 static inline tree
6490 convert_placeholder_argument (tree parm, tree arg)
6492 TREE_TYPE (arg) = parm;
6493 return arg;
6496 /* Convert the indicated template ARG as necessary to match the
6497 indicated template PARM. Returns the converted ARG, or
6498 error_mark_node if the conversion was unsuccessful. Error and
6499 warning messages are issued under control of COMPLAIN. This
6500 conversion is for the Ith parameter in the parameter list. ARGS is
6501 the full set of template arguments deduced so far. */
6503 static tree
6504 convert_template_argument (tree parm,
6505 tree arg,
6506 tree args,
6507 tsubst_flags_t complain,
6508 int i,
6509 tree in_decl)
6511 tree orig_arg;
6512 tree val;
6513 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6515 // Trivially convert placeholders.
6516 if (TREE_CODE (arg) == PLACEHOLDER_EXPR)
6517 return convert_placeholder_argument (parm, arg);
6519 if (TREE_CODE (arg) == TREE_LIST
6520 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6522 /* The template argument was the name of some
6523 member function. That's usually
6524 invalid, but static members are OK. In any
6525 case, grab the underlying fields/functions
6526 and issue an error later if required. */
6527 orig_arg = TREE_VALUE (arg);
6528 TREE_TYPE (arg) = unknown_type_node;
6531 orig_arg = arg;
6533 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6534 requires_type = (TREE_CODE (parm) == TYPE_DECL
6535 || requires_tmpl_type);
6537 /* When determining whether an argument pack expansion is a template,
6538 look at the pattern. */
6539 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6540 arg = PACK_EXPANSION_PATTERN (arg);
6542 /* Deal with an injected-class-name used as a template template arg. */
6543 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6545 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6546 if (TREE_CODE (t) == TEMPLATE_DECL)
6548 if (cxx_dialect >= cxx11)
6549 /* OK under DR 1004. */;
6550 else if (complain & tf_warning_or_error)
6551 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6552 " used as template template argument", TYPE_NAME (arg));
6553 else if (flag_pedantic_errors)
6554 t = arg;
6556 arg = t;
6560 is_tmpl_type =
6561 ((TREE_CODE (arg) == TEMPLATE_DECL
6562 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6563 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6564 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6565 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6567 if (is_tmpl_type
6568 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6569 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6570 arg = TYPE_STUB_DECL (arg);
6572 is_type = TYPE_P (arg) || is_tmpl_type;
6574 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6575 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6577 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6579 if (complain & tf_error)
6580 error ("invalid use of destructor %qE as a type", orig_arg);
6581 return error_mark_node;
6584 permerror (input_location,
6585 "to refer to a type member of a template parameter, "
6586 "use %<typename %E%>", orig_arg);
6588 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6589 TREE_OPERAND (arg, 1),
6590 typename_type,
6591 complain);
6592 arg = orig_arg;
6593 is_type = 1;
6595 if (is_type != requires_type)
6597 if (in_decl)
6599 if (complain & tf_error)
6601 error ("type/value mismatch at argument %d in template "
6602 "parameter list for %qD",
6603 i + 1, in_decl);
6604 if (is_type)
6605 error (" expected a constant of type %qT, got %qT",
6606 TREE_TYPE (parm),
6607 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6608 else if (requires_tmpl_type)
6609 error (" expected a class template, got %qE", orig_arg);
6610 else
6611 error (" expected a type, got %qE", orig_arg);
6614 return error_mark_node;
6616 if (is_tmpl_type ^ requires_tmpl_type)
6618 if (in_decl && (complain & tf_error))
6620 error ("type/value mismatch at argument %d in template "
6621 "parameter list for %qD",
6622 i + 1, in_decl);
6623 if (is_tmpl_type)
6624 error (" expected a type, got %qT", DECL_NAME (arg));
6625 else
6626 error (" expected a class template, got %qT", orig_arg);
6628 return error_mark_node;
6631 if (is_type)
6633 if (requires_tmpl_type)
6635 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6636 val = orig_arg;
6637 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6638 /* The number of argument required is not known yet.
6639 Just accept it for now. */
6640 val = TREE_TYPE (arg);
6641 else
6643 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6644 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6646 /* Strip alias templates that are equivalent to another
6647 template. */
6648 arg = get_underlying_template (arg);
6649 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6651 if (coerce_template_template_parms (parmparm, argparm,
6652 complain, in_decl,
6653 args))
6655 val = arg;
6657 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6658 TEMPLATE_DECL. */
6659 if (val != error_mark_node)
6661 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6662 val = TREE_TYPE (val);
6663 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6664 val = make_pack_expansion (val);
6667 else
6669 if (in_decl && (complain & tf_error))
6671 error ("type/value mismatch at argument %d in "
6672 "template parameter list for %qD",
6673 i + 1, in_decl);
6674 error (" expected a template of type %qD, got %qT",
6675 parm, orig_arg);
6678 val = error_mark_node;
6681 // Check that the constraints are compatible before allowing the
6682 // substitution.
6683 if (val != error_mark_node)
6684 if (!is_compatible_template_arg (parm, arg))
6686 if (in_decl && (complain & tf_error))
6688 error ("constraint mismatch at argument %d in "
6689 "template parameter list for %qD",
6690 i + 1, in_decl);
6691 inform (input_location, " expected %qD but got %qD",
6692 parm, arg);
6694 val = error_mark_node;
6698 else
6699 val = orig_arg;
6700 /* We only form one instance of each template specialization.
6701 Therefore, if we use a non-canonical variant (i.e., a
6702 typedef), any future messages referring to the type will use
6703 the typedef, which is confusing if those future uses do not
6704 themselves also use the typedef. */
6705 if (TYPE_P (val))
6706 val = canonicalize_type_argument (val, complain);
6708 else
6710 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6712 if (invalid_nontype_parm_type_p (t, complain))
6713 return error_mark_node;
6715 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6717 if (same_type_p (t, TREE_TYPE (orig_arg)))
6718 val = orig_arg;
6719 else
6721 /* Not sure if this is reachable, but it doesn't hurt
6722 to be robust. */
6723 error ("type mismatch in nontype parameter pack");
6724 val = error_mark_node;
6727 else if (!dependent_template_arg_p (orig_arg)
6728 && !uses_template_parms (t))
6729 /* We used to call digest_init here. However, digest_init
6730 will report errors, which we don't want when complain
6731 is zero. More importantly, digest_init will try too
6732 hard to convert things: for example, `0' should not be
6733 converted to pointer type at this point according to
6734 the standard. Accepting this is not merely an
6735 extension, since deciding whether or not these
6736 conversions can occur is part of determining which
6737 function template to call, or whether a given explicit
6738 argument specification is valid. */
6739 val = convert_nontype_argument (t, orig_arg, complain);
6740 else
6741 val = strip_typedefs_expr (orig_arg);
6743 if (val == NULL_TREE)
6744 val = error_mark_node;
6745 else if (val == error_mark_node && (complain & tf_error))
6746 error ("could not convert template argument %qE to %qT", orig_arg, t);
6748 if (TREE_CODE (val) == SCOPE_REF)
6750 /* Strip typedefs from the SCOPE_REF. */
6751 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6752 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6753 complain);
6754 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6755 QUALIFIED_NAME_IS_TEMPLATE (val));
6759 return val;
6762 /* Coerces the remaining template arguments in INNER_ARGS (from
6763 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6764 Returns the coerced argument pack. PARM_IDX is the position of this
6765 parameter in the template parameter list. ARGS is the original
6766 template argument list. */
6767 static tree
6768 coerce_template_parameter_pack (tree parms,
6769 int parm_idx,
6770 tree args,
6771 tree inner_args,
6772 int arg_idx,
6773 tree new_args,
6774 int* lost,
6775 tree in_decl,
6776 tsubst_flags_t complain)
6778 tree parm = TREE_VEC_ELT (parms, parm_idx);
6779 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6780 tree packed_args;
6781 tree argument_pack;
6782 tree packed_types = NULL_TREE;
6784 if (arg_idx > nargs)
6785 arg_idx = nargs;
6787 packed_args = make_tree_vec (nargs - arg_idx);
6789 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6790 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6792 /* When the template parameter is a non-type template
6793 parameter pack whose type uses parameter packs, we need
6794 to look at each of the template arguments
6795 separately. Build a vector of the types for these
6796 non-type template parameters in PACKED_TYPES. */
6797 tree expansion
6798 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6799 packed_types = tsubst_pack_expansion (expansion, args,
6800 complain, in_decl);
6802 if (packed_types == error_mark_node)
6803 return error_mark_node;
6805 /* Check that we have the right number of arguments. */
6806 if (arg_idx < nargs
6807 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6808 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6810 int needed_parms
6811 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6812 error ("wrong number of template arguments (%d, should be %d)",
6813 nargs, needed_parms);
6814 return error_mark_node;
6817 /* If we aren't able to check the actual arguments now
6818 (because they haven't been expanded yet), we can at least
6819 verify that all of the types used for the non-type
6820 template parameter pack are, in fact, valid for non-type
6821 template parameters. */
6822 if (arg_idx < nargs
6823 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6825 int j, len = TREE_VEC_LENGTH (packed_types);
6826 for (j = 0; j < len; ++j)
6828 tree t = TREE_VEC_ELT (packed_types, j);
6829 if (invalid_nontype_parm_type_p (t, complain))
6830 return error_mark_node;
6835 /* Convert the remaining arguments, which will be a part of the
6836 parameter pack "parm". */
6837 for (; arg_idx < nargs; ++arg_idx)
6839 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6840 tree actual_parm = TREE_VALUE (parm);
6842 if (packed_types && !PACK_EXPANSION_P (arg))
6844 /* When we have a vector of types (corresponding to the
6845 non-type template parameter pack that uses parameter
6846 packs in its type, as mention above), and the
6847 argument is not an expansion (which expands to a
6848 currently unknown number of arguments), clone the
6849 parm and give it the next type in PACKED_TYPES. */
6850 actual_parm = copy_node (actual_parm);
6851 TREE_TYPE (actual_parm) =
6852 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6855 if (arg == error_mark_node)
6857 if (complain & tf_error)
6858 error ("template argument %d is invalid", arg_idx + 1);
6860 else
6861 arg = convert_template_argument (actual_parm,
6862 arg, new_args, complain, parm_idx,
6863 in_decl);
6864 if (arg == error_mark_node)
6865 (*lost)++;
6866 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6869 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6870 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6871 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6872 else
6874 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6875 TREE_TYPE (argument_pack)
6876 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6877 TREE_CONSTANT (argument_pack) = 1;
6880 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6881 #ifdef ENABLE_CHECKING
6882 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6883 TREE_VEC_LENGTH (packed_args));
6884 #endif
6885 return argument_pack;
6888 /* Returns the number of pack expansions in the template argument vector
6889 ARGS. */
6891 static int
6892 pack_expansion_args_count (tree args)
6894 int i;
6895 int count = 0;
6896 if (args)
6897 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6899 tree elt = TREE_VEC_ELT (args, i);
6900 if (elt && PACK_EXPANSION_P (elt))
6901 ++count;
6903 return count;
6906 /* Convert all template arguments to their appropriate types, and
6907 return a vector containing the innermost resulting template
6908 arguments. If any error occurs, return error_mark_node. Error and
6909 warning messages are issued under control of COMPLAIN.
6911 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6912 for arguments not specified in ARGS. Otherwise, if
6913 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6914 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6915 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6916 ARGS. */
6918 static tree
6919 coerce_template_parms (tree parms,
6920 tree args,
6921 tree in_decl,
6922 tsubst_flags_t complain,
6923 bool require_all_args,
6924 bool use_default_args)
6926 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6927 tree inner_args;
6928 tree new_args;
6929 tree new_inner_args;
6930 int saved_unevaluated_operand;
6931 int saved_inhibit_evaluation_warnings;
6933 /* When used as a boolean value, indicates whether this is a
6934 variadic template parameter list. Since it's an int, we can also
6935 subtract it from nparms to get the number of non-variadic
6936 parameters. */
6937 int variadic_p = 0;
6938 int variadic_args_p = 0;
6939 int post_variadic_parms = 0;
6941 if (args == error_mark_node)
6942 return error_mark_node;
6944 nparms = TREE_VEC_LENGTH (parms);
6946 /* Determine if there are any parameter packs. */
6947 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6949 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6950 if (variadic_p)
6951 ++post_variadic_parms;
6952 if (template_parameter_pack_p (tparm))
6953 ++variadic_p;
6956 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6957 /* If there are no parameters that follow a parameter pack, we need to
6958 expand any argument packs so that we can deduce a parameter pack from
6959 some non-packed args followed by an argument pack, as in variadic85.C.
6960 If there are such parameters, we need to leave argument packs intact
6961 so the arguments are assigned properly. This can happen when dealing
6962 with a nested class inside a partial specialization of a class
6963 template, as in variadic92.C, or when deducing a template parameter pack
6964 from a sub-declarator, as in variadic114.C. */
6965 if (!post_variadic_parms)
6966 inner_args = expand_template_argument_pack (inner_args);
6968 /* Count any pack expansion args. */
6969 variadic_args_p = pack_expansion_args_count (inner_args);
6971 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6972 if ((nargs > nparms && !variadic_p)
6973 || (nargs < nparms - variadic_p
6974 && require_all_args
6975 && !variadic_args_p
6976 && (!use_default_args
6977 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6978 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6980 if (complain & tf_error)
6982 if (variadic_p)
6984 nparms -= variadic_p;
6985 error ("wrong number of template arguments "
6986 "(%d, should be %d or more)", nargs, nparms);
6988 else
6989 error ("wrong number of template arguments "
6990 "(%d, should be %d)", nargs, nparms);
6992 if (in_decl)
6993 error ("provided for %q+D", in_decl);
6996 return error_mark_node;
6998 /* We can't pass a pack expansion to a non-pack parameter of an alias
6999 template (DR 1430). */
7000 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7001 && variadic_args_p
7002 && nargs - variadic_args_p < nparms - variadic_p)
7004 if (complain & tf_error)
7006 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7008 tree arg = TREE_VEC_ELT (inner_args, i);
7009 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7011 if (PACK_EXPANSION_P (arg)
7012 && !template_parameter_pack_p (parm))
7014 error ("pack expansion argument for non-pack parameter "
7015 "%qD of alias template %qD", parm, in_decl);
7016 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7017 goto found;
7020 gcc_unreachable ();
7021 found:;
7023 return error_mark_node;
7026 /* We need to evaluate the template arguments, even though this
7027 template-id may be nested within a "sizeof". */
7028 saved_unevaluated_operand = cp_unevaluated_operand;
7029 cp_unevaluated_operand = 0;
7030 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7031 c_inhibit_evaluation_warnings = 0;
7032 new_inner_args = make_tree_vec (nparms);
7033 new_args = add_outermost_template_args (args, new_inner_args);
7034 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7036 tree arg;
7037 tree parm;
7039 /* Get the Ith template parameter. */
7040 parm = TREE_VEC_ELT (parms, parm_idx);
7042 if (parm == error_mark_node)
7044 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7045 continue;
7048 /* Calculate the next argument. */
7049 if (arg_idx < nargs)
7050 arg = TREE_VEC_ELT (inner_args, arg_idx);
7051 else
7052 arg = NULL_TREE;
7054 if (template_parameter_pack_p (TREE_VALUE (parm))
7055 && !(arg && ARGUMENT_PACK_P (arg)))
7057 /* All remaining arguments will be placed in the
7058 template parameter pack PARM. */
7059 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7060 inner_args, arg_idx,
7061 new_args, &lost,
7062 in_decl, complain);
7064 /* Store this argument. */
7065 if (arg == error_mark_node)
7066 lost++;
7067 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7069 /* We are done with all of the arguments. */
7070 arg_idx = nargs;
7072 continue;
7074 else if (arg)
7076 if (PACK_EXPANSION_P (arg))
7078 /* We don't know how many args we have yet, just
7079 use the unconverted ones for now. */
7080 new_inner_args = inner_args;
7081 break;
7084 else if (require_all_args)
7086 /* There must be a default arg in this case. */
7087 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7088 complain, in_decl);
7089 /* The position of the first default template argument,
7090 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7091 Record that. */
7092 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7093 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
7095 else
7096 break;
7098 if (arg == error_mark_node)
7100 if (complain & tf_error)
7101 error ("template argument %d is invalid", arg_idx + 1);
7103 else if (!arg)
7104 /* This only occurs if there was an error in the template
7105 parameter list itself (which we would already have
7106 reported) that we are trying to recover from, e.g., a class
7107 template with a parameter list such as
7108 template<typename..., typename>. */
7109 ++lost;
7110 else
7111 arg = convert_template_argument (TREE_VALUE (parm),
7112 arg, new_args, complain,
7113 parm_idx, in_decl);
7115 if (arg == error_mark_node)
7116 lost++;
7117 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
7119 cp_unevaluated_operand = saved_unevaluated_operand;
7120 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7122 if (lost)
7123 return error_mark_node;
7125 #ifdef ENABLE_CHECKING
7126 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7127 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7128 TREE_VEC_LENGTH (new_inner_args));
7129 #endif
7131 return new_inner_args;
7134 // Convert all template arguments to their appropriate types, and
7135 // return a vector containing the innermost resulting template
7136 // arguments. If any error occurs, return error_mark_node. Error and
7137 // warning messages are not issued.
7139 // Note that no function argument deduction is performed, and default
7140 // arguments are used to fill in unspecified arguments.
7141 tree
7142 coerce_template_parms (tree parms, tree args, tree in_decl)
7144 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7147 /* Like coerce_template_parms. If PARMS represents all template
7148 parameters levels, this function returns a vector of vectors
7149 representing all the resulting argument levels. Note that in this
7150 case, only the innermost arguments are coerced because the
7151 outermost ones are supposed to have been coerced already.
7153 Otherwise, if PARMS represents only (the innermost) vector of
7154 parameters, this function returns a vector containing just the
7155 innermost resulting arguments. */
7157 static tree
7158 coerce_innermost_template_parms (tree parms,
7159 tree args,
7160 tree in_decl,
7161 tsubst_flags_t complain,
7162 bool require_all_args,
7163 bool use_default_args)
7165 int parms_depth = TMPL_PARMS_DEPTH (parms);
7166 int args_depth = TMPL_ARGS_DEPTH (args);
7167 tree coerced_args;
7169 if (parms_depth > 1)
7171 coerced_args = make_tree_vec (parms_depth);
7172 tree level;
7173 int cur_depth;
7175 for (level = parms, cur_depth = parms_depth;
7176 parms_depth > 0 && level != NULL_TREE;
7177 level = TREE_CHAIN (level), --cur_depth)
7179 tree l;
7180 if (cur_depth == args_depth)
7181 l = coerce_template_parms (TREE_VALUE (level),
7182 args, in_decl, complain,
7183 require_all_args,
7184 use_default_args);
7185 else
7186 l = TMPL_ARGS_LEVEL (args, cur_depth);
7188 if (l == error_mark_node)
7189 return error_mark_node;
7191 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7194 else
7195 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7196 args, in_decl, complain,
7197 require_all_args,
7198 use_default_args);
7199 return coerced_args;
7202 /* Returns 1 if template args OT and NT are equivalent. */
7204 static int
7205 template_args_equal (tree ot, tree nt)
7207 if (nt == ot)
7208 return 1;
7209 if (nt == NULL_TREE || ot == NULL_TREE)
7210 return false;
7212 if (TREE_CODE (nt) == TREE_VEC)
7213 /* For member templates */
7214 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7215 else if (PACK_EXPANSION_P (ot))
7216 return (PACK_EXPANSION_P (nt)
7217 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7218 PACK_EXPANSION_PATTERN (nt))
7219 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7220 PACK_EXPANSION_EXTRA_ARGS (nt)));
7221 else if (ARGUMENT_PACK_P (ot))
7223 int i, len;
7224 tree opack, npack;
7226 if (!ARGUMENT_PACK_P (nt))
7227 return 0;
7229 opack = ARGUMENT_PACK_ARGS (ot);
7230 npack = ARGUMENT_PACK_ARGS (nt);
7231 len = TREE_VEC_LENGTH (opack);
7232 if (TREE_VEC_LENGTH (npack) != len)
7233 return 0;
7234 for (i = 0; i < len; ++i)
7235 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7236 TREE_VEC_ELT (npack, i)))
7237 return 0;
7238 return 1;
7240 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7242 /* We get here probably because we are in the middle of substituting
7243 into the pattern of a pack expansion. In that case the
7244 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7245 interested in. So we want to use the initial pack argument for
7246 the comparison. */
7247 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7248 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7249 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7250 return template_args_equal (ot, nt);
7252 else if (TYPE_P (nt))
7253 return TYPE_P (ot) && same_type_p (ot, nt);
7254 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7255 return 0;
7256 else
7257 return cp_tree_equal (ot, nt);
7260 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7261 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7262 NEWARG_PTR with the offending arguments if they are non-NULL. */
7264 static int
7265 comp_template_args_with_info (tree oldargs, tree newargs,
7266 tree *oldarg_ptr, tree *newarg_ptr)
7268 int i;
7270 if (oldargs == newargs)
7271 return 1;
7273 if (!oldargs || !newargs)
7274 return 0;
7276 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7277 return 0;
7279 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7281 tree nt = TREE_VEC_ELT (newargs, i);
7282 tree ot = TREE_VEC_ELT (oldargs, i);
7284 if (! template_args_equal (ot, nt))
7286 if (oldarg_ptr != NULL)
7287 *oldarg_ptr = ot;
7288 if (newarg_ptr != NULL)
7289 *newarg_ptr = nt;
7290 return 0;
7293 return 1;
7296 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7297 of template arguments. Returns 0 otherwise. */
7300 comp_template_args (tree oldargs, tree newargs)
7302 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7305 static void
7306 add_pending_template (tree d)
7308 tree ti = (TYPE_P (d)
7309 ? CLASSTYPE_TEMPLATE_INFO (d)
7310 : DECL_TEMPLATE_INFO (d));
7311 struct pending_template *pt;
7312 int level;
7314 if (TI_PENDING_TEMPLATE_FLAG (ti))
7315 return;
7317 /* We are called both from instantiate_decl, where we've already had a
7318 tinst_level pushed, and instantiate_template, where we haven't.
7319 Compensate. */
7320 level = !current_tinst_level || current_tinst_level->decl != d;
7322 if (level)
7323 push_tinst_level (d);
7325 pt = ggc_alloc_pending_template ();
7326 pt->next = NULL;
7327 pt->tinst = current_tinst_level;
7328 if (last_pending_template)
7329 last_pending_template->next = pt;
7330 else
7331 pending_templates = pt;
7333 last_pending_template = pt;
7335 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7337 if (level)
7338 pop_tinst_level ();
7342 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7343 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7344 documentation for TEMPLATE_ID_EXPR. */
7346 tree
7347 lookup_template_function (tree fns, tree arglist)
7349 tree type;
7351 if (fns == error_mark_node || arglist == error_mark_node)
7352 return error_mark_node;
7354 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7356 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7358 error ("%q#D is not a function template", fns);
7359 return error_mark_node;
7362 if (BASELINK_P (fns))
7364 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7365 unknown_type_node,
7366 BASELINK_FUNCTIONS (fns),
7367 arglist);
7368 return fns;
7371 type = TREE_TYPE (fns);
7372 if (TREE_CODE (fns) == OVERLOAD || !type)
7373 type = unknown_type_node;
7375 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7378 /* Within the scope of a template class S<T>, the name S gets bound
7379 (in build_self_reference) to a TYPE_DECL for the class, not a
7380 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7381 or one of its enclosing classes, and that type is a template,
7382 return the associated TEMPLATE_DECL. Otherwise, the original
7383 DECL is returned.
7385 Also handle the case when DECL is a TREE_LIST of ambiguous
7386 injected-class-names from different bases. */
7388 tree
7389 maybe_get_template_decl_from_type_decl (tree decl)
7391 if (decl == NULL_TREE)
7392 return decl;
7394 /* DR 176: A lookup that finds an injected-class-name (10.2
7395 [class.member.lookup]) can result in an ambiguity in certain cases
7396 (for example, if it is found in more than one base class). If all of
7397 the injected-class-names that are found refer to specializations of
7398 the same class template, and if the name is followed by a
7399 template-argument-list, the reference refers to the class template
7400 itself and not a specialization thereof, and is not ambiguous. */
7401 if (TREE_CODE (decl) == TREE_LIST)
7403 tree t, tmpl = NULL_TREE;
7404 for (t = decl; t; t = TREE_CHAIN (t))
7406 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7407 if (!tmpl)
7408 tmpl = elt;
7409 else if (tmpl != elt)
7410 break;
7412 if (tmpl && t == NULL_TREE)
7413 return tmpl;
7414 else
7415 return decl;
7418 return (decl != NULL_TREE
7419 && DECL_SELF_REFERENCE_P (decl)
7420 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7421 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7424 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7425 parameters, find the desired type.
7427 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7429 IN_DECL, if non-NULL, is the template declaration we are trying to
7430 instantiate.
7432 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7433 the class we are looking up.
7435 Issue error and warning messages under control of COMPLAIN.
7437 If the template class is really a local class in a template
7438 function, then the FUNCTION_CONTEXT is the function in which it is
7439 being instantiated.
7441 ??? Note that this function is currently called *twice* for each
7442 template-id: the first time from the parser, while creating the
7443 incomplete type (finish_template_type), and the second type during the
7444 real instantiation (instantiate_template_class). This is surely something
7445 that we want to avoid. It also causes some problems with argument
7446 coercion (see convert_nontype_argument for more information on this). */
7448 static tree
7449 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7450 int entering_scope, tsubst_flags_t complain)
7452 tree templ = NULL_TREE, parmlist;
7453 tree t;
7454 void **slot;
7455 spec_entry *entry;
7456 spec_entry elt;
7457 hashval_t hash;
7459 if (identifier_p (d1))
7461 tree value = innermost_non_namespace_value (d1);
7462 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7463 templ = value;
7464 else
7466 if (context)
7467 push_decl_namespace (context);
7468 templ = lookup_name (d1);
7469 templ = maybe_get_template_decl_from_type_decl (templ);
7470 if (context)
7471 pop_decl_namespace ();
7473 if (templ)
7474 context = DECL_CONTEXT (templ);
7476 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7478 tree type = TREE_TYPE (d1);
7480 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7481 an implicit typename for the second A. Deal with it. */
7482 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7483 type = TREE_TYPE (type);
7485 if (CLASSTYPE_TEMPLATE_INFO (type))
7487 templ = CLASSTYPE_TI_TEMPLATE (type);
7488 d1 = DECL_NAME (templ);
7491 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7492 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7494 templ = TYPE_TI_TEMPLATE (d1);
7495 d1 = DECL_NAME (templ);
7497 else if (TREE_CODE (d1) == TEMPLATE_DECL
7498 && DECL_TEMPLATE_RESULT (d1)
7499 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7501 templ = d1;
7502 d1 = DECL_NAME (templ);
7503 context = DECL_CONTEXT (templ);
7505 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7507 templ = d1;
7508 d1 = DECL_NAME (templ);
7511 /* Issue an error message if we didn't find a template. */
7512 if (! templ)
7514 if (complain & tf_error)
7515 error ("%qT is not a template", d1);
7516 return error_mark_node;
7519 if (TREE_CODE (templ) != TEMPLATE_DECL
7520 /* Make sure it's a user visible template, if it was named by
7521 the user. */
7522 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7523 && !PRIMARY_TEMPLATE_P (templ)))
7525 if (complain & tf_error)
7527 error ("non-template type %qT used as a template", d1);
7528 if (in_decl)
7529 error ("for template declaration %q+D", in_decl);
7531 return error_mark_node;
7534 complain &= ~tf_user;
7536 /* An alias that just changes the name of a template is equivalent to the
7537 other template, so if any of the arguments are pack expansions, strip
7538 the alias to avoid problems with a pack expansion passed to a non-pack
7539 alias template parameter (DR 1430). */
7540 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7541 templ = get_underlying_template (templ);
7543 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7545 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7546 template arguments */
7548 tree parm;
7549 tree arglist2;
7550 tree outer;
7552 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7554 /* Consider an example where a template template parameter declared as
7556 template <class T, class U = std::allocator<T> > class TT
7558 The template parameter level of T and U are one level larger than
7559 of TT. To proper process the default argument of U, say when an
7560 instantiation `TT<int>' is seen, we need to build the full
7561 arguments containing {int} as the innermost level. Outer levels,
7562 available when not appearing as default template argument, can be
7563 obtained from the arguments of the enclosing template.
7565 Suppose that TT is later substituted with std::vector. The above
7566 instantiation is `TT<int, std::allocator<T> >' with TT at
7567 level 1, and T at level 2, while the template arguments at level 1
7568 becomes {std::vector} and the inner level 2 is {int}. */
7570 outer = DECL_CONTEXT (templ);
7571 if (outer)
7572 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7573 else if (current_template_parms)
7574 /* This is an argument of the current template, so we haven't set
7575 DECL_CONTEXT yet. */
7576 outer = current_template_args ();
7578 if (outer)
7579 arglist = add_to_template_args (outer, arglist);
7581 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7582 complain,
7583 /*require_all_args=*/true,
7584 /*use_default_args=*/true);
7585 if (arglist2 == error_mark_node
7586 || (!uses_template_parms (arglist2)
7587 && check_instantiated_args (templ, arglist2, complain)))
7588 return error_mark_node;
7590 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7591 return parm;
7593 else
7595 tree template_type = TREE_TYPE (templ);
7596 tree gen_tmpl;
7597 tree type_decl;
7598 tree found = NULL_TREE;
7599 int arg_depth;
7600 int parm_depth;
7601 int is_dependent_type;
7602 int use_partial_inst_tmpl = false;
7604 if (template_type == error_mark_node)
7605 /* An error occurred while building the template TEMPL, and a
7606 diagnostic has most certainly been emitted for that
7607 already. Let's propagate that error. */
7608 return error_mark_node;
7610 gen_tmpl = most_general_template (templ);
7611 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7612 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7613 arg_depth = TMPL_ARGS_DEPTH (arglist);
7615 if (arg_depth == 1 && parm_depth > 1)
7617 /* We've been given an incomplete set of template arguments.
7618 For example, given:
7620 template <class T> struct S1 {
7621 template <class U> struct S2 {};
7622 template <class U> struct S2<U*> {};
7625 we will be called with an ARGLIST of `U*', but the
7626 TEMPLATE will be `template <class T> template
7627 <class U> struct S1<T>::S2'. We must fill in the missing
7628 arguments. */
7629 arglist
7630 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7631 arglist);
7632 arg_depth = TMPL_ARGS_DEPTH (arglist);
7635 /* Now we should have enough arguments. */
7636 gcc_assert (parm_depth == arg_depth);
7638 /* From here on, we're only interested in the most general
7639 template. */
7641 /* Calculate the BOUND_ARGS. These will be the args that are
7642 actually tsubst'd into the definition to create the
7643 instantiation. */
7644 if (parm_depth > 1)
7646 /* We have multiple levels of arguments to coerce, at once. */
7647 int i;
7648 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7650 tree bound_args = make_tree_vec (parm_depth);
7652 for (i = saved_depth,
7653 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7654 i > 0 && t != NULL_TREE;
7655 --i, t = TREE_CHAIN (t))
7657 tree a;
7658 if (i == saved_depth)
7659 a = coerce_template_parms (TREE_VALUE (t),
7660 arglist, gen_tmpl,
7661 complain,
7662 /*require_all_args=*/true,
7663 /*use_default_args=*/true);
7664 else
7665 /* Outer levels should have already been coerced. */
7666 a = TMPL_ARGS_LEVEL (arglist, i);
7668 /* Don't process further if one of the levels fails. */
7669 if (a == error_mark_node)
7671 /* Restore the ARGLIST to its full size. */
7672 TREE_VEC_LENGTH (arglist) = saved_depth;
7673 return error_mark_node;
7676 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7678 /* We temporarily reduce the length of the ARGLIST so
7679 that coerce_template_parms will see only the arguments
7680 corresponding to the template parameters it is
7681 examining. */
7682 TREE_VEC_LENGTH (arglist)--;
7685 /* Restore the ARGLIST to its full size. */
7686 TREE_VEC_LENGTH (arglist) = saved_depth;
7688 arglist = bound_args;
7690 else
7691 arglist
7692 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7693 INNERMOST_TEMPLATE_ARGS (arglist),
7694 gen_tmpl,
7695 complain,
7696 /*require_all_args=*/true,
7697 /*use_default_args=*/true);
7699 if (arglist == error_mark_node)
7700 /* We were unable to bind the arguments. */
7701 return error_mark_node;
7703 /* In the scope of a template class, explicit references to the
7704 template class refer to the type of the template, not any
7705 instantiation of it. For example, in:
7707 template <class T> class C { void f(C<T>); }
7709 the `C<T>' is just the same as `C'. Outside of the
7710 class, however, such a reference is an instantiation. */
7711 if ((entering_scope
7712 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7713 || currently_open_class (template_type))
7714 /* comp_template_args is expensive, check it last. */
7715 && comp_template_args (TYPE_TI_ARGS (template_type),
7716 arglist))
7717 return template_type;
7719 /* If we already have this specialization, return it. */
7720 elt.tmpl = gen_tmpl;
7721 elt.args = arglist;
7722 hash = hash_specialization (&elt);
7723 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7724 &elt, hash);
7726 if (entry)
7727 return entry->spec;
7729 // If the the template's constraints are not satisfied, then we
7730 // cannot form a valid type.
7732 // Note that the check is deferred until after the hash-lookup.
7733 // This prevents redundant checks on specializations.
7734 if (!check_template_constraints (gen_tmpl, arglist))
7736 // Diagnose constraints here since they are not diagnosed
7737 // anywhere else.
7738 if (complain & tf_error)
7740 error ("template argument deduction failure");
7741 diagnose_constraints (input_location, gen_tmpl, arglist);
7743 return error_mark_node;
7746 is_dependent_type = uses_template_parms (arglist);
7748 /* If the deduced arguments are invalid, then the binding
7749 failed. */
7750 if (!is_dependent_type
7751 && check_instantiated_args (gen_tmpl,
7752 INNERMOST_TEMPLATE_ARGS (arglist),
7753 complain))
7754 return error_mark_node;
7756 if (!is_dependent_type
7757 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7758 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7759 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7761 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7762 DECL_NAME (gen_tmpl),
7763 /*tag_scope=*/ts_global);
7764 return found;
7767 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7768 complain, in_decl);
7769 if (context == error_mark_node)
7770 return error_mark_node;
7772 if (!context)
7773 context = global_namespace;
7775 /* Create the type. */
7776 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7778 /* The user referred to a specialization of an alias
7779 template represented by GEN_TMPL.
7781 [temp.alias]/2 says:
7783 When a template-id refers to the specialization of an
7784 alias template, it is equivalent to the associated
7785 type obtained by substitution of its
7786 template-arguments for the template-parameters in the
7787 type-id of the alias template. */
7789 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7790 /* Note that the call above (by indirectly calling
7791 register_specialization in tsubst_decl) registers the
7792 TYPE_DECL representing the specialization of the alias
7793 template. So next time someone substitutes ARGLIST for
7794 the template parms into the alias template (GEN_TMPL),
7795 she'll get that TYPE_DECL back. */
7797 if (t == error_mark_node)
7798 return t;
7800 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7802 if (!is_dependent_type)
7804 set_current_access_from_decl (TYPE_NAME (template_type));
7805 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7806 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7807 arglist, complain, in_decl),
7808 SCOPED_ENUM_P (template_type), NULL);
7810 else
7812 /* We don't want to call start_enum for this type, since
7813 the values for the enumeration constants may involve
7814 template parameters. And, no one should be interested
7815 in the enumeration constants for such a type. */
7816 t = cxx_make_type (ENUMERAL_TYPE);
7817 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7819 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7820 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7821 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7823 else if (CLASS_TYPE_P (template_type))
7825 t = make_class_type (TREE_CODE (template_type));
7826 CLASSTYPE_DECLARED_CLASS (t)
7827 = CLASSTYPE_DECLARED_CLASS (template_type);
7828 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7829 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7831 /* A local class. Make sure the decl gets registered properly. */
7832 if (context == current_function_decl)
7833 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7835 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7836 /* This instantiation is another name for the primary
7837 template type. Set the TYPE_CANONICAL field
7838 appropriately. */
7839 TYPE_CANONICAL (t) = template_type;
7840 else if (any_template_arguments_need_structural_equality_p (arglist))
7841 /* Some of the template arguments require structural
7842 equality testing, so this template class requires
7843 structural equality testing. */
7844 SET_TYPE_STRUCTURAL_EQUALITY (t);
7846 else
7847 gcc_unreachable ();
7849 /* If we called start_enum or pushtag above, this information
7850 will already be set up. */
7851 if (!TYPE_NAME (t))
7853 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7855 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7856 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7857 DECL_SOURCE_LOCATION (type_decl)
7858 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7860 else
7861 type_decl = TYPE_NAME (t);
7863 if (CLASS_TYPE_P (template_type))
7865 TREE_PRIVATE (type_decl)
7866 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7867 TREE_PROTECTED (type_decl)
7868 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7869 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7871 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7872 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7876 /* Let's consider the explicit specialization of a member
7877 of a class template specialization that is implicitely instantiated,
7878 e.g.:
7879 template<class T>
7880 struct S
7882 template<class U> struct M {}; //#0
7885 template<>
7886 template<>
7887 struct S<int>::M<char> //#1
7889 int i;
7891 [temp.expl.spec]/4 says this is valid.
7893 In this case, when we write:
7894 S<int>::M<char> m;
7896 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7897 the one of #0.
7899 When we encounter #1, we want to store the partial instantiation
7900 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7902 For all cases other than this "explicit specialization of member of a
7903 class template", we just want to store the most general template into
7904 the CLASSTYPE_TI_TEMPLATE of M.
7906 This case of "explicit specialization of member of a class template"
7907 only happens when:
7908 1/ the enclosing class is an instantiation of, and therefore not
7909 the same as, the context of the most general template, and
7910 2/ we aren't looking at the partial instantiation itself, i.e.
7911 the innermost arguments are not the same as the innermost parms of
7912 the most general template.
7914 So it's only when 1/ and 2/ happens that we want to use the partial
7915 instantiation of the member template in lieu of its most general
7916 template. */
7918 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7919 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7920 /* the enclosing class must be an instantiation... */
7921 && CLASS_TYPE_P (context)
7922 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7924 tree partial_inst_args;
7925 TREE_VEC_LENGTH (arglist)--;
7926 ++processing_template_decl;
7927 partial_inst_args =
7928 tsubst (INNERMOST_TEMPLATE_ARGS
7929 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7930 arglist, complain, NULL_TREE);
7931 --processing_template_decl;
7932 TREE_VEC_LENGTH (arglist)++;
7933 use_partial_inst_tmpl =
7934 /*...and we must not be looking at the partial instantiation
7935 itself. */
7936 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7937 partial_inst_args);
7940 if (!use_partial_inst_tmpl)
7941 /* This case is easy; there are no member templates involved. */
7942 found = gen_tmpl;
7943 else
7945 /* This is a full instantiation of a member template. Find
7946 the partial instantiation of which this is an instance. */
7948 /* Temporarily reduce by one the number of levels in the ARGLIST
7949 so as to avoid comparing the last set of arguments. */
7950 TREE_VEC_LENGTH (arglist)--;
7951 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7952 TREE_VEC_LENGTH (arglist)++;
7953 /* FOUND is either a proper class type, or an alias
7954 template specialization. In the later case, it's a
7955 TYPE_DECL, resulting from the substituting of arguments
7956 for parameters in the TYPE_DECL of the alias template
7957 done earlier. So be careful while getting the template
7958 of FOUND. */
7959 found = TREE_CODE (found) == TYPE_DECL
7960 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7961 : CLASSTYPE_TI_TEMPLATE (found);
7964 // Build template info for the new specialization.
7965 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7967 elt.spec = t;
7968 slot = htab_find_slot_with_hash (type_specializations,
7969 &elt, hash, INSERT);
7970 entry = ggc_alloc_spec_entry ();
7971 *entry = elt;
7972 *slot = entry;
7974 /* Note this use of the partial instantiation so we can check it
7975 later in maybe_process_partial_specialization. */
7976 DECL_TEMPLATE_INSTANTIATIONS (templ)
7977 = tree_cons (arglist, t,
7978 DECL_TEMPLATE_INSTANTIATIONS (templ));
7980 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7981 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7982 /* Now that the type has been registered on the instantiations
7983 list, we set up the enumerators. Because the enumeration
7984 constants may involve the enumeration type itself, we make
7985 sure to register the type first, and then create the
7986 constants. That way, doing tsubst_expr for the enumeration
7987 constants won't result in recursive calls here; we'll find
7988 the instantiation and exit above. */
7989 tsubst_enum (template_type, t, arglist);
7991 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7992 /* If the type makes use of template parameters, the
7993 code that generates debugging information will crash. */
7994 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7996 /* Possibly limit visibility based on template args. */
7997 TREE_PUBLIC (type_decl) = 1;
7998 determine_visibility (type_decl);
8000 return t;
8004 /* Wrapper for lookup_template_class_1. */
8006 tree
8007 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8008 int entering_scope, tsubst_flags_t complain)
8010 tree ret;
8011 timevar_push (TV_TEMPLATE_INST);
8012 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8013 entering_scope, complain);
8014 timevar_pop (TV_TEMPLATE_INST);
8015 return ret;
8018 struct pair_fn_data
8020 tree_fn_t fn;
8021 void *data;
8022 /* True when we should also visit template parameters that occur in
8023 non-deduced contexts. */
8024 bool include_nondeduced_p;
8025 struct pointer_set_t *visited;
8028 /* Called from for_each_template_parm via walk_tree. */
8030 static tree
8031 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8033 tree t = *tp;
8034 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8035 tree_fn_t fn = pfd->fn;
8036 void *data = pfd->data;
8038 if (TYPE_P (t)
8039 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8040 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8041 pfd->include_nondeduced_p))
8042 return error_mark_node;
8044 switch (TREE_CODE (t))
8046 case RECORD_TYPE:
8047 if (TYPE_PTRMEMFUNC_P (t))
8048 break;
8049 /* Fall through. */
8051 case UNION_TYPE:
8052 case ENUMERAL_TYPE:
8053 if (!TYPE_TEMPLATE_INFO (t))
8054 *walk_subtrees = 0;
8055 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8056 fn, data, pfd->visited,
8057 pfd->include_nondeduced_p))
8058 return error_mark_node;
8059 break;
8061 case INTEGER_TYPE:
8062 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8063 fn, data, pfd->visited,
8064 pfd->include_nondeduced_p)
8065 || for_each_template_parm (TYPE_MAX_VALUE (t),
8066 fn, data, pfd->visited,
8067 pfd->include_nondeduced_p))
8068 return error_mark_node;
8069 break;
8071 case METHOD_TYPE:
8072 /* Since we're not going to walk subtrees, we have to do this
8073 explicitly here. */
8074 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8075 pfd->visited, pfd->include_nondeduced_p))
8076 return error_mark_node;
8077 /* Fall through. */
8079 case FUNCTION_TYPE:
8080 /* Check the return type. */
8081 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8082 pfd->include_nondeduced_p))
8083 return error_mark_node;
8085 /* Check the parameter types. Since default arguments are not
8086 instantiated until they are needed, the TYPE_ARG_TYPES may
8087 contain expressions that involve template parameters. But,
8088 no-one should be looking at them yet. And, once they're
8089 instantiated, they don't contain template parameters, so
8090 there's no point in looking at them then, either. */
8092 tree parm;
8094 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8095 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8096 pfd->visited, pfd->include_nondeduced_p))
8097 return error_mark_node;
8099 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8100 want walk_tree walking into them itself. */
8101 *walk_subtrees = 0;
8103 break;
8105 case TYPEOF_TYPE:
8106 case UNDERLYING_TYPE:
8107 if (pfd->include_nondeduced_p
8108 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
8109 pfd->visited,
8110 pfd->include_nondeduced_p))
8111 return error_mark_node;
8112 break;
8114 case FUNCTION_DECL:
8115 case VAR_DECL:
8116 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8117 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8118 pfd->visited, pfd->include_nondeduced_p))
8119 return error_mark_node;
8120 /* Fall through. */
8122 case PARM_DECL:
8123 case CONST_DECL:
8124 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8125 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8126 pfd->visited, pfd->include_nondeduced_p))
8127 return error_mark_node;
8128 if (DECL_CONTEXT (t)
8129 && pfd->include_nondeduced_p
8130 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8131 pfd->visited, pfd->include_nondeduced_p))
8132 return error_mark_node;
8133 break;
8135 case BOUND_TEMPLATE_TEMPLATE_PARM:
8136 /* Record template parameters such as `T' inside `TT<T>'. */
8137 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8138 pfd->include_nondeduced_p))
8139 return error_mark_node;
8140 /* Fall through. */
8142 case TEMPLATE_TEMPLATE_PARM:
8143 case TEMPLATE_TYPE_PARM:
8144 case TEMPLATE_PARM_INDEX:
8145 if (fn && (*fn)(t, data))
8146 return error_mark_node;
8147 else if (!fn)
8148 return error_mark_node;
8149 break;
8151 case TEMPLATE_DECL:
8152 /* A template template parameter is encountered. */
8153 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8154 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8155 pfd->include_nondeduced_p))
8156 return error_mark_node;
8158 /* Already substituted template template parameter */
8159 *walk_subtrees = 0;
8160 break;
8162 case TYPENAME_TYPE:
8163 if (!fn
8164 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8165 data, pfd->visited,
8166 pfd->include_nondeduced_p))
8167 return error_mark_node;
8168 break;
8170 case CONSTRUCTOR:
8171 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8172 && pfd->include_nondeduced_p
8173 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8174 (TREE_TYPE (t)), fn, data,
8175 pfd->visited, pfd->include_nondeduced_p))
8176 return error_mark_node;
8177 break;
8179 case INDIRECT_REF:
8180 case COMPONENT_REF:
8181 /* If there's no type, then this thing must be some expression
8182 involving template parameters. */
8183 if (!fn && !TREE_TYPE (t))
8184 return error_mark_node;
8185 break;
8187 case MODOP_EXPR:
8188 case CAST_EXPR:
8189 case IMPLICIT_CONV_EXPR:
8190 case REINTERPRET_CAST_EXPR:
8191 case CONST_CAST_EXPR:
8192 case STATIC_CAST_EXPR:
8193 case DYNAMIC_CAST_EXPR:
8194 case ARROW_EXPR:
8195 case DOTSTAR_EXPR:
8196 case TYPEID_EXPR:
8197 case PSEUDO_DTOR_EXPR:
8198 if (!fn)
8199 return error_mark_node;
8200 break;
8202 default:
8203 break;
8206 /* We didn't find any template parameters we liked. */
8207 return NULL_TREE;
8210 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8211 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8212 call FN with the parameter and the DATA.
8213 If FN returns nonzero, the iteration is terminated, and
8214 for_each_template_parm returns 1. Otherwise, the iteration
8215 continues. If FN never returns a nonzero value, the value
8216 returned by for_each_template_parm is 0. If FN is NULL, it is
8217 considered to be the function which always returns 1.
8219 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8220 parameters that occur in non-deduced contexts. When false, only
8221 visits those template parameters that can be deduced. */
8223 static int
8224 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8225 struct pointer_set_t *visited,
8226 bool include_nondeduced_p)
8228 struct pair_fn_data pfd;
8229 int result;
8231 /* Set up. */
8232 pfd.fn = fn;
8233 pfd.data = data;
8234 pfd.include_nondeduced_p = include_nondeduced_p;
8236 /* Walk the tree. (Conceptually, we would like to walk without
8237 duplicates, but for_each_template_parm_r recursively calls
8238 for_each_template_parm, so we would need to reorganize a fair
8239 bit to use walk_tree_without_duplicates, so we keep our own
8240 visited list.) */
8241 if (visited)
8242 pfd.visited = visited;
8243 else
8244 pfd.visited = pointer_set_create ();
8245 result = cp_walk_tree (&t,
8246 for_each_template_parm_r,
8247 &pfd,
8248 pfd.visited) != NULL_TREE;
8250 /* Clean up. */
8251 if (!visited)
8253 pointer_set_destroy (pfd.visited);
8254 pfd.visited = 0;
8257 return result;
8260 /* Returns true if T depends on any template parameter. */
8263 uses_template_parms (tree t)
8265 bool dependent_p;
8266 int saved_processing_template_decl;
8268 saved_processing_template_decl = processing_template_decl;
8269 if (!saved_processing_template_decl)
8270 processing_template_decl = 1;
8271 if (TYPE_P (t))
8272 dependent_p = dependent_type_p (t);
8273 else if (TREE_CODE (t) == TREE_VEC)
8274 dependent_p = any_dependent_template_arguments_p (t);
8275 else if (TREE_CODE (t) == TREE_LIST)
8276 dependent_p = (uses_template_parms (TREE_VALUE (t))
8277 || uses_template_parms (TREE_CHAIN (t)));
8278 else if (TREE_CODE (t) == TYPE_DECL)
8279 dependent_p = dependent_type_p (TREE_TYPE (t));
8280 else if (DECL_P (t)
8281 || EXPR_P (t)
8282 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8283 || TREE_CODE (t) == OVERLOAD
8284 || BASELINK_P (t)
8285 || identifier_p (t)
8286 || TREE_CODE (t) == TRAIT_EXPR
8287 || TREE_CODE (t) == CONSTRUCTOR
8288 || CONSTANT_CLASS_P (t))
8289 dependent_p = (type_dependent_expression_p (t)
8290 || value_dependent_expression_p (t));
8291 else
8293 gcc_assert (t == error_mark_node);
8294 dependent_p = false;
8297 processing_template_decl = saved_processing_template_decl;
8299 return dependent_p;
8302 /* Returns true iff current_function_decl is an incompletely instantiated
8303 template. Useful instead of processing_template_decl because the latter
8304 is set to 0 during fold_non_dependent_expr. */
8306 bool
8307 in_template_function (void)
8309 tree fn = current_function_decl;
8310 bool ret;
8311 ++processing_template_decl;
8312 ret = (fn && DECL_LANG_SPECIFIC (fn)
8313 && DECL_TEMPLATE_INFO (fn)
8314 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8315 --processing_template_decl;
8316 return ret;
8319 /* Returns true if T depends on any template parameter with level LEVEL. */
8322 uses_template_parms_level (tree t, int level)
8324 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8325 /*include_nondeduced_p=*/true);
8328 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8329 ill-formed translation unit, i.e. a variable or function that isn't
8330 usable in a constant expression. */
8332 static inline bool
8333 neglectable_inst_p (tree d)
8335 return (DECL_P (d)
8336 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8337 : decl_maybe_constant_var_p (d)));
8340 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8341 neglectable and instantiated from within an erroneous instantiation. */
8343 static bool
8344 limit_bad_template_recursion (tree decl)
8346 struct tinst_level *lev = current_tinst_level;
8347 int errs = errorcount + sorrycount;
8348 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8349 return false;
8351 for (; lev; lev = lev->next)
8352 if (neglectable_inst_p (lev->decl))
8353 break;
8355 return (lev && errs > lev->errors);
8358 static int tinst_depth;
8359 extern int max_tinst_depth;
8360 int depth_reached;
8362 static GTY(()) struct tinst_level *last_error_tinst_level;
8364 /* We're starting to instantiate D; record the template instantiation context
8365 for diagnostics and to restore it later. */
8368 push_tinst_level (tree d)
8370 struct tinst_level *new_level;
8372 if (tinst_depth >= max_tinst_depth)
8374 last_error_tinst_level = current_tinst_level;
8375 if (TREE_CODE (d) == TREE_LIST)
8376 error ("template instantiation depth exceeds maximum of %d (use "
8377 "-ftemplate-depth= to increase the maximum) substituting %qS",
8378 max_tinst_depth, d);
8379 else
8380 error ("template instantiation depth exceeds maximum of %d (use "
8381 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8382 max_tinst_depth, d);
8384 print_instantiation_context ();
8386 return 0;
8389 /* If the current instantiation caused problems, don't let it instantiate
8390 anything else. Do allow deduction substitution and decls usable in
8391 constant expressions. */
8392 if (limit_bad_template_recursion (d))
8393 return 0;
8395 new_level = ggc_alloc_tinst_level ();
8396 new_level->decl = d;
8397 new_level->locus = input_location;
8398 new_level->errors = errorcount+sorrycount;
8399 new_level->in_system_header_p = in_system_header;
8400 new_level->next = current_tinst_level;
8401 current_tinst_level = new_level;
8403 ++tinst_depth;
8404 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8405 depth_reached = tinst_depth;
8407 return 1;
8410 /* We're done instantiating this template; return to the instantiation
8411 context. */
8413 void
8414 pop_tinst_level (void)
8416 /* Restore the filename and line number stashed away when we started
8417 this instantiation. */
8418 input_location = current_tinst_level->locus;
8419 current_tinst_level = current_tinst_level->next;
8420 --tinst_depth;
8423 /* We're instantiating a deferred template; restore the template
8424 instantiation context in which the instantiation was requested, which
8425 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8427 static tree
8428 reopen_tinst_level (struct tinst_level *level)
8430 struct tinst_level *t;
8432 tinst_depth = 0;
8433 for (t = level; t; t = t->next)
8434 ++tinst_depth;
8436 current_tinst_level = level;
8437 pop_tinst_level ();
8438 if (current_tinst_level)
8439 current_tinst_level->errors = errorcount+sorrycount;
8440 return level->decl;
8443 /* Returns the TINST_LEVEL which gives the original instantiation
8444 context. */
8446 struct tinst_level *
8447 outermost_tinst_level (void)
8449 struct tinst_level *level = current_tinst_level;
8450 if (level)
8451 while (level->next)
8452 level = level->next;
8453 return level;
8456 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8457 vector of template arguments, as for tsubst.
8459 Returns an appropriate tsubst'd friend declaration. */
8461 static tree
8462 tsubst_friend_function (tree decl, tree args)
8464 tree new_friend;
8466 if (TREE_CODE (decl) == FUNCTION_DECL
8467 && DECL_TEMPLATE_INSTANTIATION (decl)
8468 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8469 /* This was a friend declared with an explicit template
8470 argument list, e.g.:
8472 friend void f<>(T);
8474 to indicate that f was a template instantiation, not a new
8475 function declaration. Now, we have to figure out what
8476 instantiation of what template. */
8478 tree template_id, arglist, fns;
8479 tree new_args;
8480 tree tmpl;
8481 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8483 /* Friend functions are looked up in the containing namespace scope.
8484 We must enter that scope, to avoid finding member functions of the
8485 current class with same name. */
8486 push_nested_namespace (ns);
8487 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8488 tf_warning_or_error, NULL_TREE,
8489 /*integral_constant_expression_p=*/false);
8490 pop_nested_namespace (ns);
8491 arglist = tsubst (DECL_TI_ARGS (decl), args,
8492 tf_warning_or_error, NULL_TREE);
8493 template_id = lookup_template_function (fns, arglist);
8495 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8496 tmpl = determine_specialization (template_id, new_friend,
8497 &new_args,
8498 /*need_member_template=*/0,
8499 TREE_VEC_LENGTH (args),
8500 tsk_none);
8501 return instantiate_template (tmpl, new_args, tf_error);
8504 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8506 /* The NEW_FRIEND will look like an instantiation, to the
8507 compiler, but is not an instantiation from the point of view of
8508 the language. For example, we might have had:
8510 template <class T> struct S {
8511 template <class U> friend void f(T, U);
8514 Then, in S<int>, template <class U> void f(int, U) is not an
8515 instantiation of anything. */
8516 if (new_friend == error_mark_node)
8517 return error_mark_node;
8519 DECL_USE_TEMPLATE (new_friend) = 0;
8520 if (TREE_CODE (decl) == TEMPLATE_DECL)
8522 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8523 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8524 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8527 /* The mangled name for the NEW_FRIEND is incorrect. The function
8528 is not a template instantiation and should not be mangled like
8529 one. Therefore, we forget the mangling here; we'll recompute it
8530 later if we need it. */
8531 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8533 SET_DECL_RTL (new_friend, NULL);
8534 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8537 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8539 tree old_decl;
8540 tree new_friend_template_info;
8541 tree new_friend_result_template_info;
8542 tree ns;
8543 int new_friend_is_defn;
8545 /* We must save some information from NEW_FRIEND before calling
8546 duplicate decls since that function will free NEW_FRIEND if
8547 possible. */
8548 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8549 new_friend_is_defn =
8550 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8551 (template_for_substitution (new_friend)))
8552 != NULL_TREE);
8553 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8555 /* This declaration is a `primary' template. */
8556 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8558 new_friend_result_template_info
8559 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8561 else
8562 new_friend_result_template_info = NULL_TREE;
8564 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8565 if (new_friend_is_defn)
8566 DECL_INITIAL (new_friend) = error_mark_node;
8568 /* Inside pushdecl_namespace_level, we will push into the
8569 current namespace. However, the friend function should go
8570 into the namespace of the template. */
8571 ns = decl_namespace_context (new_friend);
8572 push_nested_namespace (ns);
8573 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8574 pop_nested_namespace (ns);
8576 if (old_decl == error_mark_node)
8577 return error_mark_node;
8579 if (old_decl != new_friend)
8581 /* This new friend declaration matched an existing
8582 declaration. For example, given:
8584 template <class T> void f(T);
8585 template <class U> class C {
8586 template <class T> friend void f(T) {}
8589 the friend declaration actually provides the definition
8590 of `f', once C has been instantiated for some type. So,
8591 old_decl will be the out-of-class template declaration,
8592 while new_friend is the in-class definition.
8594 But, if `f' was called before this point, the
8595 instantiation of `f' will have DECL_TI_ARGS corresponding
8596 to `T' but not to `U', references to which might appear
8597 in the definition of `f'. Previously, the most general
8598 template for an instantiation of `f' was the out-of-class
8599 version; now it is the in-class version. Therefore, we
8600 run through all specialization of `f', adding to their
8601 DECL_TI_ARGS appropriately. In particular, they need a
8602 new set of outer arguments, corresponding to the
8603 arguments for this class instantiation.
8605 The same situation can arise with something like this:
8607 friend void f(int);
8608 template <class T> class C {
8609 friend void f(T) {}
8612 when `C<int>' is instantiated. Now, `f(int)' is defined
8613 in the class. */
8615 if (!new_friend_is_defn)
8616 /* On the other hand, if the in-class declaration does
8617 *not* provide a definition, then we don't want to alter
8618 existing definitions. We can just leave everything
8619 alone. */
8621 else
8623 tree new_template = TI_TEMPLATE (new_friend_template_info);
8624 tree new_args = TI_ARGS (new_friend_template_info);
8626 /* Overwrite whatever template info was there before, if
8627 any, with the new template information pertaining to
8628 the declaration. */
8629 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8631 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8633 /* We should have called reregister_specialization in
8634 duplicate_decls. */
8635 gcc_assert (retrieve_specialization (new_template,
8636 new_args, 0)
8637 == old_decl);
8639 /* Instantiate it if the global has already been used. */
8640 if (DECL_ODR_USED (old_decl))
8641 instantiate_decl (old_decl, /*defer_ok=*/true,
8642 /*expl_inst_class_mem_p=*/false);
8644 else
8646 tree t;
8648 /* Indicate that the old function template is a partial
8649 instantiation. */
8650 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8651 = new_friend_result_template_info;
8653 gcc_assert (new_template
8654 == most_general_template (new_template));
8655 gcc_assert (new_template != old_decl);
8657 /* Reassign any specializations already in the hash table
8658 to the new more general template, and add the
8659 additional template args. */
8660 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8661 t != NULL_TREE;
8662 t = TREE_CHAIN (t))
8664 tree spec = TREE_VALUE (t);
8665 spec_entry elt;
8667 elt.tmpl = old_decl;
8668 elt.args = DECL_TI_ARGS (spec);
8669 elt.spec = NULL_TREE;
8671 htab_remove_elt (decl_specializations, &elt);
8673 DECL_TI_ARGS (spec)
8674 = add_outermost_template_args (new_args,
8675 DECL_TI_ARGS (spec));
8677 register_specialization
8678 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8681 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8685 /* The information from NEW_FRIEND has been merged into OLD_DECL
8686 by duplicate_decls. */
8687 new_friend = old_decl;
8690 else
8692 tree context = DECL_CONTEXT (new_friend);
8693 bool dependent_p;
8695 /* In the code
8696 template <class T> class C {
8697 template <class U> friend void C1<U>::f (); // case 1
8698 friend void C2<T>::f (); // case 2
8700 we only need to make sure CONTEXT is a complete type for
8701 case 2. To distinguish between the two cases, we note that
8702 CONTEXT of case 1 remains dependent type after tsubst while
8703 this isn't true for case 2. */
8704 ++processing_template_decl;
8705 dependent_p = dependent_type_p (context);
8706 --processing_template_decl;
8708 if (!dependent_p
8709 && !complete_type_or_else (context, NULL_TREE))
8710 return error_mark_node;
8712 if (COMPLETE_TYPE_P (context))
8714 /* Check to see that the declaration is really present, and,
8715 possibly obtain an improved declaration. */
8716 tree fn = check_classfn (context,
8717 new_friend, NULL_TREE);
8719 if (fn)
8720 new_friend = fn;
8724 return new_friend;
8727 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8728 template arguments, as for tsubst.
8730 Returns an appropriate tsubst'd friend type or error_mark_node on
8731 failure. */
8733 static tree
8734 tsubst_friend_class (tree friend_tmpl, tree args)
8736 tree friend_type;
8737 tree tmpl;
8738 tree context;
8740 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8742 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8743 return TREE_TYPE (t);
8746 context = CP_DECL_CONTEXT (friend_tmpl);
8748 if (context != global_namespace)
8750 if (TREE_CODE (context) == NAMESPACE_DECL)
8751 push_nested_namespace (context);
8752 else
8753 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8756 /* Look for a class template declaration. We look for hidden names
8757 because two friend declarations of the same template are the
8758 same. For example, in:
8760 struct A {
8761 template <typename> friend class F;
8763 template <typename> struct B {
8764 template <typename> friend class F;
8767 both F templates are the same. */
8768 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8769 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8771 /* But, if we don't find one, it might be because we're in a
8772 situation like this:
8774 template <class T>
8775 struct S {
8776 template <class U>
8777 friend struct S;
8780 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8781 for `S<int>', not the TEMPLATE_DECL. */
8782 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8784 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8785 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8788 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8790 /* The friend template has already been declared. Just
8791 check to see that the declarations match, and install any new
8792 default parameters. We must tsubst the default parameters,
8793 of course. We only need the innermost template parameters
8794 because that is all that redeclare_class_template will look
8795 at. */
8796 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8797 > TMPL_ARGS_DEPTH (args))
8799 tree parms;
8800 location_t saved_input_location;
8801 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8802 args, tf_warning_or_error);
8804 saved_input_location = input_location;
8805 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8806 tree cons = DECL_CONSTRAINTS (tmpl);
8807 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
8808 input_location = saved_input_location;
8812 friend_type = TREE_TYPE (tmpl);
8814 else
8816 /* The friend template has not already been declared. In this
8817 case, the instantiation of the template class will cause the
8818 injection of this template into the global scope. */
8819 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8820 if (tmpl == error_mark_node)
8821 return error_mark_node;
8823 /* The new TMPL is not an instantiation of anything, so we
8824 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8825 the new type because that is supposed to be the corresponding
8826 template decl, i.e., TMPL. */
8827 DECL_USE_TEMPLATE (tmpl) = 0;
8828 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8829 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8830 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8831 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8833 /* Inject this template into the global scope. */
8834 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8837 if (context != global_namespace)
8839 if (TREE_CODE (context) == NAMESPACE_DECL)
8840 pop_nested_namespace (context);
8841 else
8842 pop_nested_class ();
8845 return friend_type;
8848 /* Returns zero if TYPE cannot be completed later due to circularity.
8849 Otherwise returns one. */
8851 static int
8852 can_complete_type_without_circularity (tree type)
8854 if (type == NULL_TREE || type == error_mark_node)
8855 return 0;
8856 else if (COMPLETE_TYPE_P (type))
8857 return 1;
8858 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8859 return can_complete_type_without_circularity (TREE_TYPE (type));
8860 else if (CLASS_TYPE_P (type)
8861 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8862 return 0;
8863 else
8864 return 1;
8867 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8869 /* Apply any attributes which had to be deferred until instantiation
8870 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8871 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8873 static void
8874 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8875 tree args, tsubst_flags_t complain, tree in_decl)
8877 tree last_dep = NULL_TREE;
8878 tree t;
8879 tree *p;
8881 for (t = attributes; t; t = TREE_CHAIN (t))
8882 if (ATTR_IS_DEPENDENT (t))
8884 last_dep = t;
8885 attributes = copy_list (attributes);
8886 break;
8889 if (DECL_P (*decl_p))
8891 if (TREE_TYPE (*decl_p) == error_mark_node)
8892 return;
8893 p = &DECL_ATTRIBUTES (*decl_p);
8895 else
8896 p = &TYPE_ATTRIBUTES (*decl_p);
8898 if (last_dep)
8900 tree late_attrs = NULL_TREE;
8901 tree *q = &late_attrs;
8903 for (*p = attributes; *p; )
8905 t = *p;
8906 if (ATTR_IS_DEPENDENT (t))
8908 *p = TREE_CHAIN (t);
8909 TREE_CHAIN (t) = NULL_TREE;
8910 if (flag_openmp
8911 && is_attribute_p ("omp declare simd",
8912 get_attribute_name (t))
8913 && TREE_VALUE (t))
8915 tree clauses = TREE_VALUE (TREE_VALUE (t));
8916 clauses = tsubst_omp_clauses (clauses, true, args,
8917 complain, in_decl);
8918 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8919 clauses = finish_omp_clauses (clauses);
8920 tree parms = DECL_ARGUMENTS (*decl_p);
8921 clauses
8922 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8923 if (clauses)
8924 TREE_VALUE (TREE_VALUE (t)) = clauses;
8925 else
8926 TREE_VALUE (t) = NULL_TREE;
8928 /* If the first attribute argument is an identifier, don't
8929 pass it through tsubst. Attributes like mode, format,
8930 cleanup and several target specific attributes expect it
8931 unmodified. */
8932 else if (attribute_takes_identifier_p (get_attribute_name (t)))
8934 tree chain
8935 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8936 in_decl,
8937 /*integral_constant_expression_p=*/false);
8938 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8939 TREE_VALUE (t)
8940 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8941 chain);
8943 else
8944 TREE_VALUE (t)
8945 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8946 /*integral_constant_expression_p=*/false);
8947 *q = t;
8948 q = &TREE_CHAIN (t);
8950 else
8951 p = &TREE_CHAIN (t);
8954 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8958 /* Perform (or defer) access check for typedefs that were referenced
8959 from within the template TMPL code.
8960 This is a subroutine of instantiate_decl and instantiate_class_template.
8961 TMPL is the template to consider and TARGS is the list of arguments of
8962 that template. */
8964 static void
8965 perform_typedefs_access_check (tree tmpl, tree targs)
8967 location_t saved_location;
8968 unsigned i;
8969 qualified_typedef_usage_t *iter;
8971 if (!tmpl
8972 || (!CLASS_TYPE_P (tmpl)
8973 && TREE_CODE (tmpl) != FUNCTION_DECL))
8974 return;
8976 saved_location = input_location;
8977 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8979 tree type_decl = iter->typedef_decl;
8980 tree type_scope = iter->context;
8982 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8983 continue;
8985 if (uses_template_parms (type_decl))
8986 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8987 if (uses_template_parms (type_scope))
8988 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8990 /* Make access check error messages point to the location
8991 of the use of the typedef. */
8992 input_location = iter->locus;
8993 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8994 type_decl, type_decl,
8995 tf_warning_or_error);
8997 input_location = saved_location;
9000 static tree
9001 instantiate_class_template_1 (tree type)
9003 tree templ, args, pattern, t, member;
9004 tree typedecl;
9005 tree pbinfo;
9006 tree base_list;
9007 unsigned int saved_maximum_field_alignment;
9008 tree fn_context;
9010 if (type == error_mark_node)
9011 return error_mark_node;
9013 if (COMPLETE_OR_OPEN_TYPE_P (type)
9014 || uses_template_parms (type))
9015 return type;
9017 /* Figure out which template is being instantiated. */
9018 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9019 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9021 /* Determine what specialization of the original template to
9022 instantiate. */
9023 t = most_specialized_class (type, templ, tf_warning_or_error);
9024 if (t == error_mark_node)
9026 TYPE_BEING_DEFINED (type) = 1;
9027 return error_mark_node;
9029 else if (t)
9031 /* This TYPE is actually an instantiation of a partial
9032 specialization. We replace the innermost set of ARGS with
9033 the arguments appropriate for substitution. For example,
9034 given:
9036 template <class T> struct S {};
9037 template <class T> struct S<T*> {};
9039 and supposing that we are instantiating S<int*>, ARGS will
9040 presently be {int*} -- but we need {int}. */
9041 pattern = TREE_TYPE (t);
9042 args = TREE_PURPOSE (t);
9044 else
9046 pattern = TREE_TYPE (templ);
9047 args = CLASSTYPE_TI_ARGS (type);
9050 /* If the template we're instantiating is incomplete, then clearly
9051 there's nothing we can do. */
9052 if (!COMPLETE_TYPE_P (pattern))
9053 return type;
9055 /* If we've recursively instantiated too many templates, stop. */
9056 if (! push_tinst_level (type))
9057 return type;
9059 /* Now we're really doing the instantiation. Mark the type as in
9060 the process of being defined. */
9061 TYPE_BEING_DEFINED (type) = 1;
9063 /* We may be in the middle of deferred access check. Disable
9064 it now. */
9065 push_deferring_access_checks (dk_no_deferred);
9067 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9068 if (!fn_context)
9069 push_to_top_level ();
9070 /* Use #pragma pack from the template context. */
9071 saved_maximum_field_alignment = maximum_field_alignment;
9072 maximum_field_alignment = TYPE_PRECISION (pattern);
9074 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9076 /* Set the input location to the most specialized template definition.
9077 This is needed if tsubsting causes an error. */
9078 typedecl = TYPE_MAIN_DECL (pattern);
9079 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9080 DECL_SOURCE_LOCATION (typedecl);
9082 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9083 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9084 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9085 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9086 if (ANON_AGGR_TYPE_P (pattern))
9087 SET_ANON_AGGR_TYPE_P (type);
9088 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9090 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9091 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9092 /* Adjust visibility for template arguments. */
9093 determine_visibility (TYPE_MAIN_DECL (type));
9095 if (CLASS_TYPE_P (type))
9096 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9098 pbinfo = TYPE_BINFO (pattern);
9100 /* We should never instantiate a nested class before its enclosing
9101 class; we need to look up the nested class by name before we can
9102 instantiate it, and that lookup should instantiate the enclosing
9103 class. */
9104 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9105 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9107 base_list = NULL_TREE;
9108 if (BINFO_N_BASE_BINFOS (pbinfo))
9110 tree pbase_binfo;
9111 tree pushed_scope;
9112 int i;
9114 /* We must enter the scope containing the type, as that is where
9115 the accessibility of types named in dependent bases are
9116 looked up from. */
9117 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9119 /* Substitute into each of the bases to determine the actual
9120 basetypes. */
9121 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9123 tree base;
9124 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9125 tree expanded_bases = NULL_TREE;
9126 int idx, len = 1;
9128 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9130 expanded_bases =
9131 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9132 args, tf_error, NULL_TREE);
9133 if (expanded_bases == error_mark_node)
9134 continue;
9136 len = TREE_VEC_LENGTH (expanded_bases);
9139 for (idx = 0; idx < len; idx++)
9141 if (expanded_bases)
9142 /* Extract the already-expanded base class. */
9143 base = TREE_VEC_ELT (expanded_bases, idx);
9144 else
9145 /* Substitute to figure out the base class. */
9146 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9147 NULL_TREE);
9149 if (base == error_mark_node)
9150 continue;
9152 base_list = tree_cons (access, base, base_list);
9153 if (BINFO_VIRTUAL_P (pbase_binfo))
9154 TREE_TYPE (base_list) = integer_type_node;
9158 /* The list is now in reverse order; correct that. */
9159 base_list = nreverse (base_list);
9161 if (pushed_scope)
9162 pop_scope (pushed_scope);
9164 /* Now call xref_basetypes to set up all the base-class
9165 information. */
9166 xref_basetypes (type, base_list);
9168 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9169 (int) ATTR_FLAG_TYPE_IN_PLACE,
9170 args, tf_error, NULL_TREE);
9171 fixup_attribute_variants (type);
9173 /* Now that our base classes are set up, enter the scope of the
9174 class, so that name lookups into base classes, etc. will work
9175 correctly. This is precisely analogous to what we do in
9176 begin_class_definition when defining an ordinary non-template
9177 class, except we also need to push the enclosing classes. */
9178 push_nested_class (type);
9180 /* Now members are processed in the order of declaration. */
9181 for (member = CLASSTYPE_DECL_LIST (pattern);
9182 member; member = TREE_CHAIN (member))
9184 tree t = TREE_VALUE (member);
9186 if (TREE_PURPOSE (member))
9188 if (TYPE_P (t))
9190 /* Build new CLASSTYPE_NESTED_UTDS. */
9192 tree newtag;
9193 bool class_template_p;
9195 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9196 && TYPE_LANG_SPECIFIC (t)
9197 && CLASSTYPE_IS_TEMPLATE (t));
9198 /* If the member is a class template, then -- even after
9199 substitution -- there may be dependent types in the
9200 template argument list for the class. We increment
9201 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9202 that function will assume that no types are dependent
9203 when outside of a template. */
9204 if (class_template_p)
9205 ++processing_template_decl;
9206 newtag = tsubst (t, args, tf_error, NULL_TREE);
9207 if (class_template_p)
9208 --processing_template_decl;
9209 if (newtag == error_mark_node)
9210 continue;
9212 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9214 tree name = TYPE_IDENTIFIER (t);
9216 if (class_template_p)
9217 /* Unfortunately, lookup_template_class sets
9218 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9219 instantiation (i.e., for the type of a member
9220 template class nested within a template class.)
9221 This behavior is required for
9222 maybe_process_partial_specialization to work
9223 correctly, but is not accurate in this case;
9224 the TAG is not an instantiation of anything.
9225 (The corresponding TEMPLATE_DECL is an
9226 instantiation, but the TYPE is not.) */
9227 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9229 /* Now, we call pushtag to put this NEWTAG into the scope of
9230 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9231 pushtag calling push_template_decl. We don't have to do
9232 this for enums because it will already have been done in
9233 tsubst_enum. */
9234 if (name)
9235 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9236 pushtag (name, newtag, /*tag_scope=*/ts_current);
9239 else if (DECL_DECLARES_FUNCTION_P (t))
9241 /* Build new TYPE_METHODS. */
9242 tree r;
9244 if (TREE_CODE (t) == TEMPLATE_DECL)
9245 ++processing_template_decl;
9246 r = tsubst (t, args, tf_error, NULL_TREE);
9247 if (TREE_CODE (t) == TEMPLATE_DECL)
9248 --processing_template_decl;
9249 set_current_access_from_decl (r);
9250 finish_member_declaration (r);
9251 /* Instantiate members marked with attribute used. */
9252 if (r != error_mark_node && DECL_PRESERVE_P (r))
9253 mark_used (r);
9254 if (TREE_CODE (r) == FUNCTION_DECL
9255 && DECL_OMP_DECLARE_REDUCTION_P (r))
9256 cp_check_omp_declare_reduction (r);
9258 else
9260 /* Build new TYPE_FIELDS. */
9261 if (TREE_CODE (t) == STATIC_ASSERT)
9263 tree condition;
9265 ++c_inhibit_evaluation_warnings;
9266 condition =
9267 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9268 tf_warning_or_error, NULL_TREE,
9269 /*integral_constant_expression_p=*/true);
9270 --c_inhibit_evaluation_warnings;
9272 finish_static_assert (condition,
9273 STATIC_ASSERT_MESSAGE (t),
9274 STATIC_ASSERT_SOURCE_LOCATION (t),
9275 /*member_p=*/true);
9277 else if (TREE_CODE (t) != CONST_DECL)
9279 tree r;
9280 tree vec = NULL_TREE;
9281 int len = 1;
9283 /* The file and line for this declaration, to
9284 assist in error message reporting. Since we
9285 called push_tinst_level above, we don't need to
9286 restore these. */
9287 input_location = DECL_SOURCE_LOCATION (t);
9289 if (TREE_CODE (t) == TEMPLATE_DECL)
9290 ++processing_template_decl;
9291 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9292 if (TREE_CODE (t) == TEMPLATE_DECL)
9293 --processing_template_decl;
9295 if (TREE_CODE (r) == TREE_VEC)
9297 /* A capture pack became multiple fields. */
9298 vec = r;
9299 len = TREE_VEC_LENGTH (vec);
9302 for (int i = 0; i < len; ++i)
9304 if (vec)
9305 r = TREE_VEC_ELT (vec, i);
9306 if (VAR_P (r))
9308 /* In [temp.inst]:
9310 [t]he initialization (and any associated
9311 side-effects) of a static data member does
9312 not occur unless the static data member is
9313 itself used in a way that requires the
9314 definition of the static data member to
9315 exist.
9317 Therefore, we do not substitute into the
9318 initialized for the static data member here. */
9319 finish_static_data_member_decl
9321 /*init=*/NULL_TREE,
9322 /*init_const_expr_p=*/false,
9323 /*asmspec_tree=*/NULL_TREE,
9324 /*flags=*/0);
9325 /* Instantiate members marked with attribute used. */
9326 if (r != error_mark_node && DECL_PRESERVE_P (r))
9327 mark_used (r);
9329 else if (TREE_CODE (r) == FIELD_DECL)
9331 /* Determine whether R has a valid type and can be
9332 completed later. If R is invalid, then its type
9333 is replaced by error_mark_node. */
9334 tree rtype = TREE_TYPE (r);
9335 if (can_complete_type_without_circularity (rtype))
9336 complete_type (rtype);
9338 if (!COMPLETE_TYPE_P (rtype))
9340 cxx_incomplete_type_error (r, rtype);
9341 TREE_TYPE (r) = error_mark_node;
9345 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9346 such a thing will already have been added to the field
9347 list by tsubst_enum in finish_member_declaration in the
9348 CLASSTYPE_NESTED_UTDS case above. */
9349 if (!(TREE_CODE (r) == TYPE_DECL
9350 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9351 && DECL_ARTIFICIAL (r)))
9353 set_current_access_from_decl (r);
9354 finish_member_declaration (r);
9360 else
9362 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9363 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9365 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9367 tree friend_type = t;
9368 bool adjust_processing_template_decl = false;
9370 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9372 /* template <class T> friend class C; */
9373 friend_type = tsubst_friend_class (friend_type, args);
9374 adjust_processing_template_decl = true;
9376 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9378 /* template <class T> friend class C::D; */
9379 friend_type = tsubst (friend_type, args,
9380 tf_warning_or_error, NULL_TREE);
9381 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9382 friend_type = TREE_TYPE (friend_type);
9383 adjust_processing_template_decl = true;
9385 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9386 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9388 /* This could be either
9390 friend class T::C;
9392 when dependent_type_p is false or
9394 template <class U> friend class T::C;
9396 otherwise. */
9397 friend_type = tsubst (friend_type, args,
9398 tf_warning_or_error, NULL_TREE);
9399 /* Bump processing_template_decl for correct
9400 dependent_type_p calculation. */
9401 ++processing_template_decl;
9402 if (dependent_type_p (friend_type))
9403 adjust_processing_template_decl = true;
9404 --processing_template_decl;
9406 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9407 && hidden_name_p (TYPE_NAME (friend_type)))
9409 /* friend class C;
9411 where C hasn't been declared yet. Let's lookup name
9412 from namespace scope directly, bypassing any name that
9413 come from dependent base class. */
9414 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9416 /* The call to xref_tag_from_type does injection for friend
9417 classes. */
9418 push_nested_namespace (ns);
9419 friend_type =
9420 xref_tag_from_type (friend_type, NULL_TREE,
9421 /*tag_scope=*/ts_current);
9422 pop_nested_namespace (ns);
9424 else if (uses_template_parms (friend_type))
9425 /* friend class C<T>; */
9426 friend_type = tsubst (friend_type, args,
9427 tf_warning_or_error, NULL_TREE);
9428 /* Otherwise it's
9430 friend class C;
9432 where C is already declared or
9434 friend class C<int>;
9436 We don't have to do anything in these cases. */
9438 if (adjust_processing_template_decl)
9439 /* Trick make_friend_class into realizing that the friend
9440 we're adding is a template, not an ordinary class. It's
9441 important that we use make_friend_class since it will
9442 perform some error-checking and output cross-reference
9443 information. */
9444 ++processing_template_decl;
9446 if (friend_type != error_mark_node)
9447 make_friend_class (type, friend_type, /*complain=*/false);
9449 if (adjust_processing_template_decl)
9450 --processing_template_decl;
9452 else
9454 /* Build new DECL_FRIENDLIST. */
9455 tree r;
9457 /* The file and line for this declaration, to
9458 assist in error message reporting. Since we
9459 called push_tinst_level above, we don't need to
9460 restore these. */
9461 input_location = DECL_SOURCE_LOCATION (t);
9463 if (TREE_CODE (t) == TEMPLATE_DECL)
9465 ++processing_template_decl;
9466 push_deferring_access_checks (dk_no_check);
9469 r = tsubst_friend_function (t, args);
9470 add_friend (type, r, /*complain=*/false);
9471 if (TREE_CODE (t) == TEMPLATE_DECL)
9473 pop_deferring_access_checks ();
9474 --processing_template_decl;
9480 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9482 tree decl = lambda_function (type);
9483 if (decl)
9485 if (!DECL_TEMPLATE_INFO (decl)
9486 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9487 instantiate_decl (decl, false, false);
9489 /* We need to instantiate the capture list from the template
9490 after we've instantiated the closure members, but before we
9491 consider adding the conversion op. Also keep any captures
9492 that may have been added during instantiation of the op(). */
9493 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9494 tree tmpl_cap
9495 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9496 args, tf_warning_or_error, NULL_TREE,
9497 false, false);
9499 LAMBDA_EXPR_CAPTURE_LIST (expr)
9500 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9502 maybe_add_lambda_conv_op (type);
9504 else
9505 gcc_assert (errorcount);
9508 /* Set the file and line number information to whatever is given for
9509 the class itself. This puts error messages involving generated
9510 implicit functions at a predictable point, and the same point
9511 that would be used for non-template classes. */
9512 input_location = DECL_SOURCE_LOCATION (typedecl);
9514 unreverse_member_declarations (type);
9515 finish_struct_1 (type);
9516 TYPE_BEING_DEFINED (type) = 0;
9518 /* We don't instantiate default arguments for member functions. 14.7.1:
9520 The implicit instantiation of a class template specialization causes
9521 the implicit instantiation of the declarations, but not of the
9522 definitions or default arguments, of the class member functions,
9523 member classes, static data members and member templates.... */
9525 /* Some typedefs referenced from within the template code need to be access
9526 checked at template instantiation time, i.e now. These types were
9527 added to the template at parsing time. Let's get those and perform
9528 the access checks then. */
9529 perform_typedefs_access_check (pattern, args);
9530 perform_deferred_access_checks (tf_warning_or_error);
9531 pop_nested_class ();
9532 maximum_field_alignment = saved_maximum_field_alignment;
9533 if (!fn_context)
9534 pop_from_top_level ();
9535 pop_deferring_access_checks ();
9536 pop_tinst_level ();
9538 /* The vtable for a template class can be emitted in any translation
9539 unit in which the class is instantiated. When there is no key
9540 method, however, finish_struct_1 will already have added TYPE to
9541 the keyed_classes list. */
9542 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9543 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9545 return type;
9548 /* Wrapper for instantiate_class_template_1. */
9550 tree
9551 instantiate_class_template (tree type)
9553 tree ret;
9554 timevar_push (TV_TEMPLATE_INST);
9555 ret = instantiate_class_template_1 (type);
9556 timevar_pop (TV_TEMPLATE_INST);
9557 return ret;
9560 static tree
9561 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9563 tree r;
9565 if (!t)
9566 r = t;
9567 else if (TYPE_P (t))
9568 r = tsubst (t, args, complain, in_decl);
9569 else
9571 if (!(complain & tf_warning))
9572 ++c_inhibit_evaluation_warnings;
9573 r = tsubst_expr (t, args, complain, in_decl,
9574 /*integral_constant_expression_p=*/true);
9575 if (!(complain & tf_warning))
9576 --c_inhibit_evaluation_warnings;
9577 /* Preserve the raw-reference nature of T. */
9578 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9579 && REFERENCE_REF_P (r))
9580 r = TREE_OPERAND (r, 0);
9582 return r;
9585 /* Given a function parameter pack TMPL_PARM and some function parameters
9586 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9587 and set *SPEC_P to point at the next point in the list. */
9589 tree
9590 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9592 /* Collect all of the extra "packed" parameters into an
9593 argument pack. */
9594 tree parmvec;
9595 tree parmtypevec;
9596 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9597 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9598 tree spec_parm = *spec_p;
9599 int i, len;
9601 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9602 if (tmpl_parm
9603 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9604 break;
9606 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9607 parmvec = make_tree_vec (len);
9608 parmtypevec = make_tree_vec (len);
9609 spec_parm = *spec_p;
9610 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9612 TREE_VEC_ELT (parmvec, i) = spec_parm;
9613 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9616 /* Build the argument packs. */
9617 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9618 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9619 TREE_TYPE (argpack) = argtypepack;
9620 *spec_p = spec_parm;
9622 return argpack;
9625 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9626 NONTYPE_ARGUMENT_PACK. */
9628 static tree
9629 make_fnparm_pack (tree spec_parm)
9631 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9634 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9635 pack expansion. */
9637 static bool
9638 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9640 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9641 if (i >= TREE_VEC_LENGTH (vec))
9642 return false;
9643 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9647 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9649 static tree
9650 make_argument_pack_select (tree arg_pack, unsigned index)
9652 tree aps = make_node (ARGUMENT_PACK_SELECT);
9654 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9655 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9657 return aps;
9660 /* This is a subroutine of tsubst_pack_expansion.
9662 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9663 mechanism to store the (non complete list of) arguments of the
9664 substitution and return a non substituted pack expansion, in order
9665 to wait for when we have enough arguments to really perform the
9666 substitution. */
9668 static bool
9669 use_pack_expansion_extra_args_p (tree parm_packs,
9670 int arg_pack_len,
9671 bool has_empty_arg)
9673 /* If one pack has an expansion and another pack has a normal
9674 argument or if one pack has an empty argument and an another
9675 one hasn't then tsubst_pack_expansion cannot perform the
9676 substitution and need to fall back on the
9677 PACK_EXPANSION_EXTRA mechanism. */
9678 if (parm_packs == NULL_TREE)
9679 return false;
9680 else if (has_empty_arg)
9681 return true;
9683 bool has_expansion_arg = false;
9684 for (int i = 0 ; i < arg_pack_len; ++i)
9686 bool has_non_expansion_arg = false;
9687 for (tree parm_pack = parm_packs;
9688 parm_pack;
9689 parm_pack = TREE_CHAIN (parm_pack))
9691 tree arg = TREE_VALUE (parm_pack);
9693 if (argument_pack_element_is_expansion_p (arg, i))
9694 has_expansion_arg = true;
9695 else
9696 has_non_expansion_arg = true;
9699 if (has_expansion_arg && has_non_expansion_arg)
9700 return true;
9702 return false;
9705 /* [temp.variadic]/6 says that:
9707 The instantiation of a pack expansion [...]
9708 produces a list E1,E2, ..., En, where N is the number of elements
9709 in the pack expansion parameters.
9711 This subroutine of tsubst_pack_expansion produces one of these Ei.
9713 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9714 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9715 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9716 INDEX is the index 'i' of the element Ei to produce. ARGS,
9717 COMPLAIN, and IN_DECL are the same parameters as for the
9718 tsubst_pack_expansion function.
9720 The function returns the resulting Ei upon successful completion,
9721 or error_mark_node.
9723 Note that this function possibly modifies the ARGS parameter, so
9724 it's the responsibility of the caller to restore it. */
9726 static tree
9727 gen_elem_of_pack_expansion_instantiation (tree pattern,
9728 tree parm_packs,
9729 unsigned index,
9730 tree args /* This parm gets
9731 modified. */,
9732 tsubst_flags_t complain,
9733 tree in_decl)
9735 tree t;
9736 bool ith_elem_is_expansion = false;
9738 /* For each parameter pack, change the substitution of the parameter
9739 pack to the ith argument in its argument pack, then expand the
9740 pattern. */
9741 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9743 tree parm = TREE_PURPOSE (pack);
9744 tree arg_pack = TREE_VALUE (pack);
9745 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9747 ith_elem_is_expansion |=
9748 argument_pack_element_is_expansion_p (arg_pack, index);
9750 /* Select the Ith argument from the pack. */
9751 if (TREE_CODE (parm) == PARM_DECL
9752 || TREE_CODE (parm) == FIELD_DECL)
9754 if (index == 0)
9756 aps = make_argument_pack_select (arg_pack, index);
9757 mark_used (parm);
9758 register_local_specialization (aps, parm);
9760 else
9761 aps = retrieve_local_specialization (parm);
9763 else
9765 int idx, level;
9766 template_parm_level_and_index (parm, &level, &idx);
9768 if (index == 0)
9770 aps = make_argument_pack_select (arg_pack, index);
9771 /* Update the corresponding argument. */
9772 TMPL_ARG (args, level, idx) = aps;
9774 else
9775 /* Re-use the ARGUMENT_PACK_SELECT. */
9776 aps = TMPL_ARG (args, level, idx);
9778 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9781 /* Substitute into the PATTERN with the (possibly altered)
9782 arguments. */
9783 if (!TYPE_P (pattern))
9784 t = tsubst_expr (pattern, args, complain, in_decl,
9785 /*integral_constant_expression_p=*/false);
9786 else
9787 t = tsubst (pattern, args, complain, in_decl);
9789 /* If the Ith argument pack element is a pack expansion, then
9790 the Ith element resulting from the substituting is going to
9791 be a pack expansion as well. */
9792 if (ith_elem_is_expansion)
9793 t = make_pack_expansion (t);
9795 return t;
9798 // Substitute args into the pack expansion T, and rewrite the resulting
9799 // list as a conjuntion of the specified terms. If the result is an empty
9800 // expression, return boolean_true_node.
9801 tree
9802 tsubst_pack_conjunction (tree t, tree args, tsubst_flags_t complain,
9803 tree in_decl)
9805 tree terms = tsubst_pack_expansion (t, args, complain, in_decl);
9807 // If the resulting expression is type- or value-dependent, then
9808 // return it after setting the result type to bool (so it can be
9809 // expanded as a conjunction).
9811 // This happens when instantiating constrained variadic
9812 // member function templates.
9813 if (instantiation_dependent_expression_p (terms))
9815 TREE_TYPE (terms) = boolean_type_node;
9816 return terms;
9819 // Conjoin requirements. An empty conjunction is equivalent to ture.
9820 if (tree reqs = conjoin_requirements (terms))
9821 return reqs;
9822 else
9823 return boolean_true_node;
9827 /* Substitute ARGS into T, which is an pack expansion
9828 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9829 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9830 (if only a partial substitution could be performed) or
9831 ERROR_MARK_NODE if there was an error. */
9832 tree
9833 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9834 tree in_decl)
9836 tree pattern;
9837 tree pack, packs = NULL_TREE;
9838 bool unsubstituted_packs = false;
9839 int i, len = -1;
9840 tree result;
9841 struct pointer_map_t *saved_local_specializations = NULL;
9842 bool need_local_specializations = false;
9843 int levels;
9845 gcc_assert (PACK_EXPANSION_P (t));
9846 pattern = PACK_EXPANSION_PATTERN (t);
9848 /* Add in any args remembered from an earlier partial instantiation. */
9849 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9851 levels = TMPL_ARGS_DEPTH (args);
9853 /* Determine the argument packs that will instantiate the parameter
9854 packs used in the expansion expression. While we're at it,
9855 compute the number of arguments to be expanded and make sure it
9856 is consistent. */
9857 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9858 pack = TREE_CHAIN (pack))
9860 tree parm_pack = TREE_VALUE (pack);
9861 tree arg_pack = NULL_TREE;
9862 tree orig_arg = NULL_TREE;
9863 int level = 0;
9865 if (TREE_CODE (parm_pack) == BASES)
9867 if (BASES_DIRECT (parm_pack))
9868 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9869 args, complain, in_decl, false));
9870 else
9871 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9872 args, complain, in_decl, false));
9874 if (TREE_CODE (parm_pack) == PARM_DECL)
9876 if (PACK_EXPANSION_LOCAL_P (t))
9877 arg_pack = retrieve_local_specialization (parm_pack);
9878 else
9880 /* We can't rely on local_specializations for a parameter
9881 name used later in a function declaration (such as in a
9882 late-specified return type). Even if it exists, it might
9883 have the wrong value for a recursive call. Just make a
9884 dummy decl, since it's only used for its type. */
9885 arg_pack = tsubst_decl (parm_pack, args, complain);
9886 if (arg_pack && DECL_PACK_P (arg_pack))
9887 /* Partial instantiation of the parm_pack, we can't build
9888 up an argument pack yet. */
9889 arg_pack = NULL_TREE;
9890 else
9891 arg_pack = make_fnparm_pack (arg_pack);
9892 need_local_specializations = true;
9895 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9896 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9897 else
9899 int idx;
9900 template_parm_level_and_index (parm_pack, &level, &idx);
9902 if (level <= levels)
9903 arg_pack = TMPL_ARG (args, level, idx);
9906 orig_arg = arg_pack;
9907 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9908 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9910 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9911 /* This can only happen if we forget to expand an argument
9912 pack somewhere else. Just return an error, silently. */
9914 result = make_tree_vec (1);
9915 TREE_VEC_ELT (result, 0) = error_mark_node;
9916 return result;
9919 if (arg_pack)
9921 int my_len =
9922 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9924 /* Don't bother trying to do a partial substitution with
9925 incomplete packs; we'll try again after deduction. */
9926 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9927 return t;
9929 if (len < 0)
9930 len = my_len;
9931 else if (len != my_len)
9933 if (!(complain & tf_error))
9934 /* Fail quietly. */;
9935 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9936 error ("mismatched argument pack lengths while expanding "
9937 "%<%T%>",
9938 pattern);
9939 else
9940 error ("mismatched argument pack lengths while expanding "
9941 "%<%E%>",
9942 pattern);
9943 return error_mark_node;
9946 /* Keep track of the parameter packs and their corresponding
9947 argument packs. */
9948 packs = tree_cons (parm_pack, arg_pack, packs);
9949 TREE_TYPE (packs) = orig_arg;
9951 else
9953 /* We can't substitute for this parameter pack. We use a flag as
9954 well as the missing_level counter because function parameter
9955 packs don't have a level. */
9956 unsubstituted_packs = true;
9960 /* We cannot expand this expansion expression, because we don't have
9961 all of the argument packs we need. */
9962 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9964 /* We got some full packs, but we can't substitute them in until we
9965 have values for all the packs. So remember these until then. */
9967 t = make_pack_expansion (pattern);
9968 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9969 return t;
9971 else if (unsubstituted_packs)
9973 /* There were no real arguments, we're just replacing a parameter
9974 pack with another version of itself. Substitute into the
9975 pattern and return a PACK_EXPANSION_*. The caller will need to
9976 deal with that. */
9977 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9978 t = tsubst_expr (pattern, args, complain, in_decl,
9979 /*integral_constant_expression_p=*/false);
9980 else
9981 t = tsubst (pattern, args, complain, in_decl);
9982 t = make_pack_expansion (t);
9983 return t;
9986 gcc_assert (len >= 0);
9988 if (need_local_specializations)
9990 /* We're in a late-specified return type, so create our own local
9991 specializations map; the current map is either NULL or (in the
9992 case of recursive unification) might have bindings that we don't
9993 want to use or alter. */
9994 saved_local_specializations = local_specializations;
9995 local_specializations = pointer_map_create ();
9998 /* For each argument in each argument pack, substitute into the
9999 pattern. */
10000 result = make_tree_vec (len);
10001 for (i = 0; i < len; ++i)
10003 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10005 args, complain,
10006 in_decl);
10007 TREE_VEC_ELT (result, i) = t;
10008 if (t == error_mark_node)
10010 result = error_mark_node;
10011 break;
10015 /* Update ARGS to restore the substitution from parameter packs to
10016 their argument packs. */
10017 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10019 tree parm = TREE_PURPOSE (pack);
10021 if (TREE_CODE (parm) == PARM_DECL
10022 || TREE_CODE (parm) == FIELD_DECL)
10023 register_local_specialization (TREE_TYPE (pack), parm);
10024 else
10026 int idx, level;
10028 if (TREE_VALUE (pack) == NULL_TREE)
10029 continue;
10031 template_parm_level_and_index (parm, &level, &idx);
10033 /* Update the corresponding argument. */
10034 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10035 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10036 TREE_TYPE (pack);
10037 else
10038 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10042 if (need_local_specializations)
10044 pointer_map_destroy (local_specializations);
10045 local_specializations = saved_local_specializations;
10048 return result;
10051 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10052 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10053 parameter packs; all parms generated from a function parameter pack will
10054 have the same DECL_PARM_INDEX. */
10056 tree
10057 get_pattern_parm (tree parm, tree tmpl)
10059 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10060 tree patparm;
10062 if (DECL_ARTIFICIAL (parm))
10064 for (patparm = DECL_ARGUMENTS (pattern);
10065 patparm; patparm = DECL_CHAIN (patparm))
10066 if (DECL_ARTIFICIAL (patparm)
10067 && DECL_NAME (parm) == DECL_NAME (patparm))
10068 break;
10070 else
10072 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10073 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10074 gcc_assert (DECL_PARM_INDEX (patparm)
10075 == DECL_PARM_INDEX (parm));
10078 return patparm;
10081 /* Substitute ARGS into the vector or list of template arguments T. */
10083 static tree
10084 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10086 tree orig_t = t;
10087 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10088 tree *elts;
10090 if (t == error_mark_node)
10091 return error_mark_node;
10093 len = TREE_VEC_LENGTH (t);
10094 elts = XALLOCAVEC (tree, len);
10096 for (i = 0; i < len; i++)
10098 tree orig_arg = TREE_VEC_ELT (t, i);
10099 tree new_arg;
10101 if (TREE_CODE (orig_arg) == TREE_VEC)
10102 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10103 else if (PACK_EXPANSION_P (orig_arg))
10105 /* Substitute into an expansion expression. */
10106 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10108 if (TREE_CODE (new_arg) == TREE_VEC)
10109 /* Add to the expanded length adjustment the number of
10110 expanded arguments. We subtract one from this
10111 measurement, because the argument pack expression
10112 itself is already counted as 1 in
10113 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10114 the argument pack is empty. */
10115 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10117 else if (ARGUMENT_PACK_P (orig_arg))
10119 /* Substitute into each of the arguments. */
10120 new_arg = TYPE_P (orig_arg)
10121 ? cxx_make_type (TREE_CODE (orig_arg))
10122 : make_node (TREE_CODE (orig_arg));
10124 SET_ARGUMENT_PACK_ARGS (
10125 new_arg,
10126 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10127 args, complain, in_decl));
10129 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10130 new_arg = error_mark_node;
10132 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10133 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10134 complain, in_decl);
10135 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10137 if (TREE_TYPE (new_arg) == error_mark_node)
10138 new_arg = error_mark_node;
10141 else
10142 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10144 if (new_arg == error_mark_node)
10145 return error_mark_node;
10147 elts[i] = new_arg;
10148 if (new_arg != orig_arg)
10149 need_new = 1;
10152 if (!need_new)
10153 return t;
10155 /* Make space for the expanded arguments coming from template
10156 argument packs. */
10157 t = make_tree_vec (len + expanded_len_adjust);
10158 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10159 arguments for a member template.
10160 In that case each TREE_VEC in ORIG_T represents a level of template
10161 arguments, and ORIG_T won't carry any non defaulted argument count.
10162 It will rather be the nested TREE_VECs that will carry one.
10163 In other words, ORIG_T carries a non defaulted argument count only
10164 if it doesn't contain any nested TREE_VEC. */
10165 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10167 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10168 count += expanded_len_adjust;
10169 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10171 for (i = 0, out = 0; i < len; i++)
10173 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10174 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10175 && TREE_CODE (elts[i]) == TREE_VEC)
10177 int idx;
10179 /* Now expand the template argument pack "in place". */
10180 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10181 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10183 else
10185 TREE_VEC_ELT (t, out) = elts[i];
10186 out++;
10190 return t;
10193 /* Return the result of substituting ARGS into the template parameters
10194 given by PARMS. If there are m levels of ARGS and m + n levels of
10195 PARMS, then the result will contain n levels of PARMS. For
10196 example, if PARMS is `template <class T> template <class U>
10197 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10198 result will be `template <int*, double, class V>'. */
10200 static tree
10201 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10203 tree r = NULL_TREE;
10204 tree* new_parms;
10206 /* When substituting into a template, we must set
10207 PROCESSING_TEMPLATE_DECL as the template parameters may be
10208 dependent if they are based on one-another, and the dependency
10209 predicates are short-circuit outside of templates. */
10210 ++processing_template_decl;
10212 for (new_parms = &r;
10213 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10214 new_parms = &(TREE_CHAIN (*new_parms)),
10215 parms = TREE_CHAIN (parms))
10217 tree new_vec =
10218 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10219 int i;
10221 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10223 tree tuple;
10225 if (parms == error_mark_node)
10226 continue;
10228 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10230 if (tuple == error_mark_node)
10231 continue;
10233 TREE_VEC_ELT (new_vec, i) =
10234 tsubst_template_parm (tuple, args, complain);
10237 *new_parms =
10238 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10239 - TMPL_ARGS_DEPTH (args)),
10240 new_vec, NULL_TREE);
10243 --processing_template_decl;
10245 return r;
10248 /* Return the result of substituting ARGS into one template parameter
10249 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10250 parameter and which TREE_PURPOSE is the default argument of the
10251 template parameter. */
10253 static tree
10254 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10256 tree default_value, parm_decl;
10258 if (args == NULL_TREE
10259 || t == NULL_TREE
10260 || t == error_mark_node)
10261 return t;
10263 gcc_assert (TREE_CODE (t) == TREE_LIST);
10265 default_value = TREE_PURPOSE (t);
10266 parm_decl = TREE_VALUE (t);
10268 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10269 if (TREE_CODE (parm_decl) == PARM_DECL
10270 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10271 parm_decl = error_mark_node;
10272 default_value = tsubst_template_arg (default_value, args,
10273 complain, NULL_TREE);
10275 return build_tree_list (default_value, parm_decl);
10278 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10279 type T. If T is not an aggregate or enumeration type, it is
10280 handled as if by tsubst. IN_DECL is as for tsubst. If
10281 ENTERING_SCOPE is nonzero, T is the context for a template which
10282 we are presently tsubst'ing. Return the substituted value. */
10284 static tree
10285 tsubst_aggr_type (tree t,
10286 tree args,
10287 tsubst_flags_t complain,
10288 tree in_decl,
10289 int entering_scope)
10291 if (t == NULL_TREE)
10292 return NULL_TREE;
10294 switch (TREE_CODE (t))
10296 case RECORD_TYPE:
10297 if (TYPE_PTRMEMFUNC_P (t))
10298 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10300 /* Else fall through. */
10301 case ENUMERAL_TYPE:
10302 case UNION_TYPE:
10303 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10305 tree argvec;
10306 tree context;
10307 tree r;
10308 int saved_unevaluated_operand;
10309 int saved_inhibit_evaluation_warnings;
10311 /* In "sizeof(X<I>)" we need to evaluate "I". */
10312 saved_unevaluated_operand = cp_unevaluated_operand;
10313 cp_unevaluated_operand = 0;
10314 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10315 c_inhibit_evaluation_warnings = 0;
10317 /* First, determine the context for the type we are looking
10318 up. */
10319 context = TYPE_CONTEXT (t);
10320 if (context && TYPE_P (context))
10322 context = tsubst_aggr_type (context, args, complain,
10323 in_decl, /*entering_scope=*/1);
10324 /* If context is a nested class inside a class template,
10325 it may still need to be instantiated (c++/33959). */
10326 context = complete_type (context);
10329 /* Then, figure out what arguments are appropriate for the
10330 type we are trying to find. For example, given:
10332 template <class T> struct S;
10333 template <class T, class U> void f(T, U) { S<U> su; }
10335 and supposing that we are instantiating f<int, double>,
10336 then our ARGS will be {int, double}, but, when looking up
10337 S we only want {double}. */
10338 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10339 complain, in_decl);
10340 if (argvec == error_mark_node)
10341 r = error_mark_node;
10342 else
10344 r = lookup_template_class (t, argvec, in_decl, context,
10345 entering_scope, complain);
10346 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10349 cp_unevaluated_operand = saved_unevaluated_operand;
10350 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10352 return r;
10354 else
10355 /* This is not a template type, so there's nothing to do. */
10356 return t;
10358 default:
10359 return tsubst (t, args, complain, in_decl);
10363 /* Substitute into the default argument ARG (a default argument for
10364 FN), which has the indicated TYPE. */
10366 tree
10367 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10369 tree saved_class_ptr = NULL_TREE;
10370 tree saved_class_ref = NULL_TREE;
10371 int errs = errorcount + sorrycount;
10373 /* This can happen in invalid code. */
10374 if (TREE_CODE (arg) == DEFAULT_ARG)
10375 return arg;
10377 /* This default argument came from a template. Instantiate the
10378 default argument here, not in tsubst. In the case of
10379 something like:
10381 template <class T>
10382 struct S {
10383 static T t();
10384 void f(T = t());
10387 we must be careful to do name lookup in the scope of S<T>,
10388 rather than in the current class. */
10389 push_access_scope (fn);
10390 /* The "this" pointer is not valid in a default argument. */
10391 if (cfun)
10393 saved_class_ptr = current_class_ptr;
10394 cp_function_chain->x_current_class_ptr = NULL_TREE;
10395 saved_class_ref = current_class_ref;
10396 cp_function_chain->x_current_class_ref = NULL_TREE;
10399 push_deferring_access_checks(dk_no_deferred);
10400 /* The default argument expression may cause implicitly defined
10401 member functions to be synthesized, which will result in garbage
10402 collection. We must treat this situation as if we were within
10403 the body of function so as to avoid collecting live data on the
10404 stack. */
10405 ++function_depth;
10406 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10407 complain, NULL_TREE,
10408 /*integral_constant_expression_p=*/false);
10409 --function_depth;
10410 pop_deferring_access_checks();
10412 /* Restore the "this" pointer. */
10413 if (cfun)
10415 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10416 cp_function_chain->x_current_class_ref = saved_class_ref;
10419 if (errorcount+sorrycount > errs
10420 && (complain & tf_warning_or_error))
10421 inform (input_location,
10422 " when instantiating default argument for call to %D", fn);
10424 /* Make sure the default argument is reasonable. */
10425 arg = check_default_argument (type, arg, complain);
10427 pop_access_scope (fn);
10429 return arg;
10432 /* Substitute into all the default arguments for FN. */
10434 static void
10435 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10437 tree arg;
10438 tree tmpl_args;
10440 tmpl_args = DECL_TI_ARGS (fn);
10442 /* If this function is not yet instantiated, we certainly don't need
10443 its default arguments. */
10444 if (uses_template_parms (tmpl_args))
10445 return;
10446 /* Don't do this again for clones. */
10447 if (DECL_CLONED_FUNCTION_P (fn))
10448 return;
10450 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10451 arg;
10452 arg = TREE_CHAIN (arg))
10453 if (TREE_PURPOSE (arg))
10454 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10455 TREE_VALUE (arg),
10456 TREE_PURPOSE (arg),
10457 complain);
10460 /* Substitute the ARGS into the T, which is a _DECL. Return the
10461 result of the substitution. Issue error and warning messages under
10462 control of COMPLAIN. */
10464 static tree
10465 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10467 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10468 location_t saved_loc;
10469 tree r = NULL_TREE;
10470 tree in_decl = t;
10471 hashval_t hash = 0;
10473 /* Set the filename and linenumber to improve error-reporting. */
10474 saved_loc = input_location;
10475 input_location = DECL_SOURCE_LOCATION (t);
10477 switch (TREE_CODE (t))
10479 case TEMPLATE_DECL:
10481 /* We can get here when processing a member function template,
10482 member class template, or template template parameter. */
10483 tree decl = DECL_TEMPLATE_RESULT (t);
10484 tree type = TREE_TYPE (t);
10485 tree spec;
10486 tree tmpl_args;
10487 tree full_args;
10489 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10491 /* Template template parameter is treated here. */
10492 tree new_type = tsubst (type, args, complain, in_decl);
10493 if (new_type == error_mark_node)
10494 RETURN (error_mark_node);
10495 /* If we get a real template back, return it. This can happen in
10496 the context of most_specialized_class. */
10497 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10498 return new_type;
10500 r = copy_decl (t);
10501 DECL_CHAIN (r) = NULL_TREE;
10502 TREE_TYPE (r) = new_type;
10503 DECL_TEMPLATE_RESULT (r)
10504 = build_decl (DECL_SOURCE_LOCATION (decl),
10505 TYPE_DECL, DECL_NAME (decl), new_type);
10506 DECL_TEMPLATE_PARMS (r)
10507 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10508 complain);
10509 TYPE_NAME (new_type) = r;
10510 break;
10513 /* We might already have an instance of this template.
10514 The ARGS are for the surrounding class type, so the
10515 full args contain the tsubst'd args for the context,
10516 plus the innermost args from the template decl. */
10517 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10518 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10519 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10520 /* Because this is a template, the arguments will still be
10521 dependent, even after substitution. If
10522 PROCESSING_TEMPLATE_DECL is not set, the dependency
10523 predicates will short-circuit. */
10524 ++processing_template_decl;
10525 full_args = tsubst_template_args (tmpl_args, args,
10526 complain, in_decl);
10527 --processing_template_decl;
10528 if (full_args == error_mark_node)
10529 RETURN (error_mark_node);
10531 /* If this is a default template template argument,
10532 tsubst might not have changed anything. */
10533 if (full_args == tmpl_args)
10534 RETURN (t);
10536 hash = hash_tmpl_and_args (t, full_args);
10537 spec = retrieve_specialization (t, full_args, hash);
10538 if (spec != NULL_TREE)
10540 r = spec;
10541 break;
10544 /* Make a new template decl. It will be similar to the
10545 original, but will record the current template arguments.
10546 We also create a new function declaration, which is just
10547 like the old one, but points to this new template, rather
10548 than the old one. */
10549 r = copy_decl (t);
10550 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10551 DECL_CHAIN (r) = NULL_TREE;
10553 // Build new template info linking to the original template decl.
10554 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10556 if (TREE_CODE (decl) == TYPE_DECL
10557 && !TYPE_DECL_ALIAS_P (decl))
10559 tree new_type;
10560 ++processing_template_decl;
10561 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10562 --processing_template_decl;
10563 if (new_type == error_mark_node)
10564 RETURN (error_mark_node);
10566 TREE_TYPE (r) = new_type;
10567 /* For a partial specialization, we need to keep pointing to
10568 the primary template. */
10569 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10570 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10571 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10572 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10573 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10575 else
10577 tree new_decl;
10578 ++processing_template_decl;
10579 new_decl = tsubst (decl, args, complain, in_decl);
10580 --processing_template_decl;
10581 if (new_decl == error_mark_node)
10582 RETURN (error_mark_node);
10584 DECL_TEMPLATE_RESULT (r) = new_decl;
10585 DECL_TI_TEMPLATE (new_decl) = r;
10586 TREE_TYPE (r) = TREE_TYPE (new_decl);
10587 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10588 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10591 SET_DECL_IMPLICIT_INSTANTIATION (r);
10592 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10593 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10595 /* The template parameters for this new template are all the
10596 template parameters for the old template, except the
10597 outermost level of parameters. */
10598 DECL_TEMPLATE_PARMS (r)
10599 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10600 complain);
10602 // If constrained, also instantiate requirements.
10603 // TODO: Instantiate shorthand constraints on parameters also.
10604 // See tsubst_template_parms for that.
10605 if (tree ci = DECL_CONSTRAINTS (t))
10607 tree reqs = instantiate_requirements (CI_SPELLING (ci), args);
10608 DECL_CONSTRAINTS (r) = finish_template_requirements (reqs);
10611 if (PRIMARY_TEMPLATE_P (t))
10612 DECL_PRIMARY_TEMPLATE (r) = r;
10614 if (TREE_CODE (decl) != TYPE_DECL)
10615 /* Record this non-type partial instantiation. */
10616 register_specialization (r, t,
10617 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10618 false, hash);
10620 break;
10622 case FUNCTION_DECL:
10624 tree ctx;
10625 tree argvec = NULL_TREE;
10626 tree *friends;
10627 tree gen_tmpl;
10628 tree type;
10629 int member;
10630 int args_depth;
10631 int parms_depth;
10633 /* Nobody should be tsubst'ing into non-template functions. */
10634 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10636 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10638 tree spec;
10639 bool dependent_p;
10641 /* If T is not dependent, just return it. We have to
10642 increment PROCESSING_TEMPLATE_DECL because
10643 value_dependent_expression_p assumes that nothing is
10644 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10645 ++processing_template_decl;
10646 dependent_p = value_dependent_expression_p (t);
10647 --processing_template_decl;
10648 if (!dependent_p)
10649 RETURN (t);
10651 /* Calculate the most general template of which R is a
10652 specialization, and the complete set of arguments used to
10653 specialize R. */
10654 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10655 argvec = tsubst_template_args (DECL_TI_ARGS
10656 (DECL_TEMPLATE_RESULT
10657 (DECL_TI_TEMPLATE (t))),
10658 args, complain, in_decl);
10659 if (argvec == error_mark_node)
10660 RETURN (error_mark_node);
10662 /* Check to see if we already have this specialization. */
10663 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10664 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10666 if (spec)
10668 r = spec;
10669 break;
10672 /* We can see more levels of arguments than parameters if
10673 there was a specialization of a member template, like
10674 this:
10676 template <class T> struct S { template <class U> void f(); }
10677 template <> template <class U> void S<int>::f(U);
10679 Here, we'll be substituting into the specialization,
10680 because that's where we can find the code we actually
10681 want to generate, but we'll have enough arguments for
10682 the most general template.
10684 We also deal with the peculiar case:
10686 template <class T> struct S {
10687 template <class U> friend void f();
10689 template <class U> void f() {}
10690 template S<int>;
10691 template void f<double>();
10693 Here, the ARGS for the instantiation of will be {int,
10694 double}. But, we only need as many ARGS as there are
10695 levels of template parameters in CODE_PATTERN. We are
10696 careful not to get fooled into reducing the ARGS in
10697 situations like:
10699 template <class T> struct S { template <class U> void f(U); }
10700 template <class T> template <> void S<T>::f(int) {}
10702 which we can spot because the pattern will be a
10703 specialization in this case. */
10704 args_depth = TMPL_ARGS_DEPTH (args);
10705 parms_depth =
10706 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10707 if (args_depth > parms_depth
10708 && !DECL_TEMPLATE_SPECIALIZATION (t))
10709 args = get_innermost_template_args (args, parms_depth);
10711 else
10713 /* This special case arises when we have something like this:
10715 template <class T> struct S {
10716 friend void f<int>(int, double);
10719 Here, the DECL_TI_TEMPLATE for the friend declaration
10720 will be an IDENTIFIER_NODE. We are being called from
10721 tsubst_friend_function, and we want only to create a
10722 new decl (R) with appropriate types so that we can call
10723 determine_specialization. */
10724 gen_tmpl = NULL_TREE;
10727 if (DECL_CLASS_SCOPE_P (t))
10729 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10730 member = 2;
10731 else
10732 member = 1;
10733 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10734 complain, t, /*entering_scope=*/1);
10736 else
10738 member = 0;
10739 ctx = DECL_CONTEXT (t);
10741 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10742 if (type == error_mark_node)
10743 RETURN (error_mark_node);
10745 /* If we hit excessive deduction depth, the type is bogus even if
10746 it isn't error_mark_node, so don't build a decl. */
10747 if (excessive_deduction_depth)
10748 RETURN (error_mark_node);
10750 /* We do NOT check for matching decls pushed separately at this
10751 point, as they may not represent instantiations of this
10752 template, and in any case are considered separate under the
10753 discrete model. */
10754 r = copy_decl (t);
10755 DECL_USE_TEMPLATE (r) = 0;
10756 TREE_TYPE (r) = type;
10757 /* Clear out the mangled name and RTL for the instantiation. */
10758 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10759 SET_DECL_RTL (r, NULL);
10760 /* Leave DECL_INITIAL set on deleted instantiations. */
10761 if (!DECL_DELETED_FN (r))
10762 DECL_INITIAL (r) = NULL_TREE;
10763 DECL_CONTEXT (r) = ctx;
10765 /* OpenMP UDRs have the only argument a reference to the declared
10766 type. We want to diagnose if the declared type is a reference,
10767 which is invalid, but as references to references are usually
10768 quietly merged, diagnose it here. */
10769 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10771 tree argtype
10772 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10773 argtype = tsubst (argtype, args, complain, in_decl);
10774 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10775 error_at (DECL_SOURCE_LOCATION (t),
10776 "reference type %qT in "
10777 "%<#pragma omp declare reduction%>", argtype);
10778 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10779 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10780 argtype);
10783 if (member && DECL_CONV_FN_P (r))
10784 /* Type-conversion operator. Reconstruct the name, in
10785 case it's the name of one of the template's parameters. */
10786 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10788 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10789 complain, t);
10790 DECL_RESULT (r) = NULL_TREE;
10792 TREE_STATIC (r) = 0;
10793 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10794 DECL_EXTERNAL (r) = 1;
10795 /* If this is an instantiation of a function with internal
10796 linkage, we already know what object file linkage will be
10797 assigned to the instantiation. */
10798 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10799 DECL_DEFER_OUTPUT (r) = 0;
10800 DECL_CHAIN (r) = NULL_TREE;
10801 DECL_PENDING_INLINE_INFO (r) = 0;
10802 DECL_PENDING_INLINE_P (r) = 0;
10803 DECL_SAVED_TREE (r) = NULL_TREE;
10804 DECL_STRUCT_FUNCTION (r) = NULL;
10805 TREE_USED (r) = 0;
10806 /* We'll re-clone as appropriate in instantiate_template. */
10807 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10809 /* If we aren't complaining now, return on error before we register
10810 the specialization so that we'll complain eventually. */
10811 if ((complain & tf_error) == 0
10812 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10813 && !grok_op_properties (r, /*complain=*/false))
10814 RETURN (error_mark_node);
10816 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10817 this in the special friend case mentioned above where
10818 GEN_TMPL is NULL. */
10819 if (gen_tmpl)
10821 DECL_TEMPLATE_INFO (r) = build_template_info (gen_tmpl, argvec);
10822 SET_DECL_IMPLICIT_INSTANTIATION (r);
10824 tree new_r
10825 = register_specialization (r, gen_tmpl, argvec, false, hash);
10826 if (new_r != r)
10827 /* We instantiated this while substituting into
10828 the type earlier (template/friend54.C). */
10829 RETURN (new_r);
10831 /* We're not supposed to instantiate default arguments
10832 until they are called, for a template. But, for a
10833 declaration like:
10835 template <class T> void f ()
10836 { extern void g(int i = T()); }
10838 we should do the substitution when the template is
10839 instantiated. We handle the member function case in
10840 instantiate_class_template since the default arguments
10841 might refer to other members of the class. */
10842 if (!member
10843 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10844 && !uses_template_parms (argvec))
10845 tsubst_default_arguments (r, complain);
10847 else
10848 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10850 /* Copy the list of befriending classes. */
10851 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10852 *friends;
10853 friends = &TREE_CHAIN (*friends))
10855 *friends = copy_node (*friends);
10856 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10857 args, complain,
10858 in_decl);
10861 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10863 maybe_retrofit_in_chrg (r);
10864 if (DECL_CONSTRUCTOR_P (r))
10865 grok_ctor_properties (ctx, r);
10866 if (DECL_INHERITED_CTOR_BASE (r))
10867 deduce_inheriting_ctor (r);
10868 /* If this is an instantiation of a member template, clone it.
10869 If it isn't, that'll be handled by
10870 clone_constructors_and_destructors. */
10871 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10872 clone_function_decl (r, /*update_method_vec_p=*/0);
10874 else if ((complain & tf_error) != 0
10875 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10876 && !grok_op_properties (r, /*complain=*/true))
10877 RETURN (error_mark_node);
10879 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10880 SET_DECL_FRIEND_CONTEXT (r,
10881 tsubst (DECL_FRIEND_CONTEXT (t),
10882 args, complain, in_decl));
10884 /* Possibly limit visibility based on template args. */
10885 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10886 if (DECL_VISIBILITY_SPECIFIED (t))
10888 DECL_VISIBILITY_SPECIFIED (r) = 0;
10889 DECL_ATTRIBUTES (r)
10890 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10892 determine_visibility (r);
10893 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10894 && !processing_template_decl)
10895 defaulted_late_check (r);
10897 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10898 args, complain, in_decl);
10900 break;
10902 case PARM_DECL:
10904 tree type = NULL_TREE;
10905 int i, len = 1;
10906 tree expanded_types = NULL_TREE;
10907 tree prev_r = NULL_TREE;
10908 tree first_r = NULL_TREE;
10910 if (DECL_PACK_P (t))
10912 /* If there is a local specialization that isn't a
10913 parameter pack, it means that we're doing a "simple"
10914 substitution from inside tsubst_pack_expansion. Just
10915 return the local specialization (which will be a single
10916 parm). */
10917 tree spec = retrieve_local_specialization (t);
10918 if (spec
10919 && TREE_CODE (spec) == PARM_DECL
10920 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10921 RETURN (spec);
10923 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10924 the parameters in this function parameter pack. */
10925 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10926 complain, in_decl);
10927 if (TREE_CODE (expanded_types) == TREE_VEC)
10929 len = TREE_VEC_LENGTH (expanded_types);
10931 /* Zero-length parameter packs are boring. Just substitute
10932 into the chain. */
10933 if (len == 0)
10934 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10935 TREE_CHAIN (t)));
10937 else
10939 /* All we did was update the type. Make a note of that. */
10940 type = expanded_types;
10941 expanded_types = NULL_TREE;
10945 /* Loop through all of the parameters we'll build. When T is
10946 a function parameter pack, LEN is the number of expanded
10947 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10948 r = NULL_TREE;
10949 for (i = 0; i < len; ++i)
10951 prev_r = r;
10952 r = copy_node (t);
10953 if (DECL_TEMPLATE_PARM_P (t))
10954 SET_DECL_TEMPLATE_PARM_P (r);
10956 if (expanded_types)
10957 /* We're on the Ith parameter of the function parameter
10958 pack. */
10960 /* Get the Ith type. */
10961 type = TREE_VEC_ELT (expanded_types, i);
10963 // An argument of a function parameter pack is a function
10964 // parameter pack if its type is also a pack. This can
10965 // happen when instantiating templates with other template
10966 // parameters.
10967 // DECL_PACK_P (r) = PACK_EXPANSION_P (type);
10969 /* Rename the parameter to include the index. */
10970 DECL_NAME (r)
10971 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10973 else if (!type)
10974 /* We're dealing with a normal parameter. */
10975 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10977 type = type_decays_to (type);
10978 TREE_TYPE (r) = type;
10979 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10981 if (DECL_INITIAL (r))
10983 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10984 DECL_INITIAL (r) = TREE_TYPE (r);
10985 else
10986 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10987 complain, in_decl);
10990 DECL_CONTEXT (r) = NULL_TREE;
10992 if (!DECL_TEMPLATE_PARM_P (r))
10993 DECL_ARG_TYPE (r) = type_passed_as (type);
10995 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10996 args, complain, in_decl);
10998 /* Keep track of the first new parameter we
10999 generate. That's what will be returned to the
11000 caller. */
11001 if (!first_r)
11002 first_r = r;
11004 /* Build a proper chain of parameters when substituting
11005 into a function parameter pack. */
11006 if (prev_r)
11007 DECL_CHAIN (prev_r) = r;
11010 /* If cp_unevaluated_operand is set, we're just looking for a
11011 single dummy parameter, so don't keep going. */
11012 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11013 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11014 complain, DECL_CHAIN (t));
11016 /* FIRST_R contains the start of the chain we've built. */
11017 r = first_r;
11019 break;
11021 case FIELD_DECL:
11023 tree type = NULL_TREE;
11024 tree vec = NULL_TREE;
11025 tree expanded_types = NULL_TREE;
11026 int len = 1;
11028 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11030 /* This field is a lambda capture pack. Return a TREE_VEC of
11031 the expanded fields to instantiate_class_template_1 and
11032 store them in the specializations hash table as a
11033 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11034 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11035 complain, in_decl);
11036 if (TREE_CODE (expanded_types) == TREE_VEC)
11038 len = TREE_VEC_LENGTH (expanded_types);
11039 vec = make_tree_vec (len);
11041 else
11043 /* All we did was update the type. Make a note of that. */
11044 type = expanded_types;
11045 expanded_types = NULL_TREE;
11049 for (int i = 0; i < len; ++i)
11051 r = copy_decl (t);
11052 if (expanded_types)
11054 type = TREE_VEC_ELT (expanded_types, i);
11055 DECL_NAME (r)
11056 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11058 else if (!type)
11059 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11061 if (type == error_mark_node)
11062 RETURN (error_mark_node);
11063 TREE_TYPE (r) = type;
11064 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11066 if (DECL_C_BIT_FIELD (r))
11067 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11068 non-bit-fields DECL_INITIAL is a non-static data member
11069 initializer, which gets deferred instantiation. */
11070 DECL_INITIAL (r)
11071 = tsubst_expr (DECL_INITIAL (t), args,
11072 complain, in_decl,
11073 /*integral_constant_expression_p=*/true);
11074 else if (DECL_INITIAL (t))
11076 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11077 NSDMI in perform_member_init. Still set DECL_INITIAL
11078 so that we know there is one. */
11079 DECL_INITIAL (r) = void_zero_node;
11080 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11081 retrofit_lang_decl (r);
11082 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11084 /* We don't have to set DECL_CONTEXT here; it is set by
11085 finish_member_declaration. */
11086 DECL_CHAIN (r) = NULL_TREE;
11088 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11089 args, complain, in_decl);
11091 if (vec)
11092 TREE_VEC_ELT (vec, i) = r;
11095 if (vec)
11097 r = vec;
11098 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11099 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11100 SET_ARGUMENT_PACK_ARGS (pack, vec);
11101 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11102 TREE_TYPE (pack) = tpack;
11103 register_specialization (pack, t, args, false, 0);
11106 break;
11108 case USING_DECL:
11109 /* We reach here only for member using decls. We also need to check
11110 uses_template_parms because DECL_DEPENDENT_P is not set for a
11111 using-declaration that designates a member of the current
11112 instantiation (c++/53549). */
11113 if (DECL_DEPENDENT_P (t)
11114 || uses_template_parms (USING_DECL_SCOPE (t)))
11116 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11117 complain, in_decl);
11118 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11119 r = do_class_using_decl (inst_scope, name);
11120 if (!r)
11121 r = error_mark_node;
11122 else
11124 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11125 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11128 else
11130 r = copy_node (t);
11131 DECL_CHAIN (r) = NULL_TREE;
11133 break;
11135 case TYPE_DECL:
11136 case VAR_DECL:
11138 tree argvec = NULL_TREE;
11139 tree gen_tmpl = NULL_TREE;
11140 tree spec;
11141 tree tmpl = NULL_TREE;
11142 tree ctx;
11143 tree type = NULL_TREE;
11144 bool local_p;
11146 if (TREE_CODE (t) == TYPE_DECL
11147 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11149 /* If this is the canonical decl, we don't have to
11150 mess with instantiations, and often we can't (for
11151 typename, template type parms and such). Note that
11152 TYPE_NAME is not correct for the above test if
11153 we've copied the type for a typedef. */
11154 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11155 if (type == error_mark_node)
11156 RETURN (error_mark_node);
11157 r = TYPE_NAME (type);
11158 break;
11161 /* Check to see if we already have the specialization we
11162 need. */
11163 spec = NULL_TREE;
11164 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11166 /* T is a static data member or namespace-scope entity.
11167 We have to substitute into namespace-scope variables
11168 (even though such entities are never templates) because
11169 of cases like:
11171 template <class T> void f() { extern T t; }
11173 where the entity referenced is not known until
11174 instantiation time. */
11175 local_p = false;
11176 ctx = DECL_CONTEXT (t);
11177 if (DECL_CLASS_SCOPE_P (t))
11179 ctx = tsubst_aggr_type (ctx, args,
11180 complain,
11181 in_decl, /*entering_scope=*/1);
11182 /* If CTX is unchanged, then T is in fact the
11183 specialization we want. That situation occurs when
11184 referencing a static data member within in its own
11185 class. We can use pointer equality, rather than
11186 same_type_p, because DECL_CONTEXT is always
11187 canonical... */
11188 if (ctx == DECL_CONTEXT (t)
11189 && (TREE_CODE (t) != TYPE_DECL
11190 /* ... unless T is a member template; in which
11191 case our caller can be willing to create a
11192 specialization of that template represented
11193 by T. */
11194 || !(DECL_TI_TEMPLATE (t)
11195 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
11196 spec = t;
11199 if (!spec)
11201 tmpl = DECL_TI_TEMPLATE (t);
11202 gen_tmpl = most_general_template (tmpl);
11203 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11204 if (argvec == error_mark_node)
11205 RETURN (error_mark_node);
11206 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11207 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11210 else
11212 /* A local variable. */
11213 local_p = true;
11214 /* Subsequent calls to pushdecl will fill this in. */
11215 ctx = NULL_TREE;
11216 spec = retrieve_local_specialization (t);
11218 /* If we already have the specialization we need, there is
11219 nothing more to do. */
11220 if (spec)
11222 r = spec;
11223 break;
11226 /* Create a new node for the specialization we need. */
11227 r = copy_decl (t);
11228 if (type == NULL_TREE)
11230 if (is_typedef_decl (t))
11231 type = DECL_ORIGINAL_TYPE (t);
11232 else
11233 type = TREE_TYPE (t);
11234 if (VAR_P (t)
11235 && VAR_HAD_UNKNOWN_BOUND (t)
11236 && type != error_mark_node)
11237 type = strip_array_domain (type);
11238 type = tsubst (type, args, complain, in_decl);
11240 if (VAR_P (r))
11242 /* Even if the original location is out of scope, the
11243 newly substituted one is not. */
11244 DECL_DEAD_FOR_LOCAL (r) = 0;
11245 DECL_INITIALIZED_P (r) = 0;
11246 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11247 if (type == error_mark_node)
11248 RETURN (error_mark_node);
11249 if (TREE_CODE (type) == FUNCTION_TYPE)
11251 /* It may seem that this case cannot occur, since:
11253 typedef void f();
11254 void g() { f x; }
11256 declares a function, not a variable. However:
11258 typedef void f();
11259 template <typename T> void g() { T t; }
11260 template void g<f>();
11262 is an attempt to declare a variable with function
11263 type. */
11264 error ("variable %qD has function type",
11265 /* R is not yet sufficiently initialized, so we
11266 just use its name. */
11267 DECL_NAME (r));
11268 RETURN (error_mark_node);
11270 type = complete_type (type);
11271 /* Wait until cp_finish_decl to set this again, to handle
11272 circular dependency (template/instantiate6.C). */
11273 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11274 type = check_var_type (DECL_NAME (r), type);
11276 if (DECL_HAS_VALUE_EXPR_P (t))
11278 tree ve = DECL_VALUE_EXPR (t);
11279 ve = tsubst_expr (ve, args, complain, in_decl,
11280 /*constant_expression_p=*/false);
11281 if (REFERENCE_REF_P (ve))
11283 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11284 ve = TREE_OPERAND (ve, 0);
11286 SET_DECL_VALUE_EXPR (r, ve);
11289 else if (DECL_SELF_REFERENCE_P (t))
11290 SET_DECL_SELF_REFERENCE_P (r);
11291 TREE_TYPE (r) = type;
11292 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11293 DECL_CONTEXT (r) = ctx;
11294 /* Clear out the mangled name and RTL for the instantiation. */
11295 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11296 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11297 SET_DECL_RTL (r, NULL);
11298 /* The initializer must not be expanded until it is required;
11299 see [temp.inst]. */
11300 DECL_INITIAL (r) = NULL_TREE;
11301 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11302 SET_DECL_RTL (r, NULL);
11303 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11304 if (VAR_P (r))
11306 /* Possibly limit visibility based on template args. */
11307 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11308 if (DECL_VISIBILITY_SPECIFIED (t))
11310 DECL_VISIBILITY_SPECIFIED (r) = 0;
11311 DECL_ATTRIBUTES (r)
11312 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11314 determine_visibility (r);
11317 if (!local_p)
11319 /* A static data member declaration is always marked
11320 external when it is declared in-class, even if an
11321 initializer is present. We mimic the non-template
11322 processing here. */
11323 DECL_EXTERNAL (r) = 1;
11325 register_specialization (r, gen_tmpl, argvec, false, hash);
11327 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11328 SET_DECL_IMPLICIT_INSTANTIATION (r);
11330 else if (cp_unevaluated_operand)
11331 gcc_unreachable ();
11332 else
11333 register_local_specialization (r, t);
11335 DECL_CHAIN (r) = NULL_TREE;
11337 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11338 /*flags=*/0,
11339 args, complain, in_decl);
11341 /* Preserve a typedef that names a type. */
11342 if (is_typedef_decl (r))
11344 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11345 set_underlying_type (r);
11348 layout_decl (r, 0);
11350 break;
11352 default:
11353 gcc_unreachable ();
11355 #undef RETURN
11357 out:
11358 /* Restore the file and line information. */
11359 input_location = saved_loc;
11361 return r;
11364 /* Substitute into the ARG_TYPES of a function type.
11365 If END is a TREE_CHAIN, leave it and any following types
11366 un-substituted. */
11368 static tree
11369 tsubst_arg_types (tree arg_types,
11370 tree args,
11371 tree end,
11372 tsubst_flags_t complain,
11373 tree in_decl)
11375 tree remaining_arg_types;
11376 tree type = NULL_TREE;
11377 int i = 1;
11378 tree expanded_args = NULL_TREE;
11379 tree default_arg;
11381 if (!arg_types || arg_types == void_list_node || arg_types == end)
11382 return arg_types;
11384 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11385 args, end, complain, in_decl);
11386 if (remaining_arg_types == error_mark_node)
11387 return error_mark_node;
11389 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11391 /* For a pack expansion, perform substitution on the
11392 entire expression. Later on, we'll handle the arguments
11393 one-by-one. */
11394 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11395 args, complain, in_decl);
11397 if (TREE_CODE (expanded_args) == TREE_VEC)
11398 /* So that we'll spin through the parameters, one by one. */
11399 i = TREE_VEC_LENGTH (expanded_args);
11400 else
11402 /* We only partially substituted into the parameter
11403 pack. Our type is TYPE_PACK_EXPANSION. */
11404 type = expanded_args;
11405 expanded_args = NULL_TREE;
11409 while (i > 0) {
11410 --i;
11412 if (expanded_args)
11413 type = TREE_VEC_ELT (expanded_args, i);
11414 else if (!type)
11415 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11417 if (type == error_mark_node)
11418 return error_mark_node;
11419 if (VOID_TYPE_P (type))
11421 if (complain & tf_error)
11423 error ("invalid parameter type %qT", type);
11424 if (in_decl)
11425 error ("in declaration %q+D", in_decl);
11427 return error_mark_node;
11429 /* DR 657. */
11430 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11431 return error_mark_node;
11433 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11434 top-level qualifiers as required. */
11435 type = cv_unqualified (type_decays_to (type));
11437 /* We do not substitute into default arguments here. The standard
11438 mandates that they be instantiated only when needed, which is
11439 done in build_over_call. */
11440 default_arg = TREE_PURPOSE (arg_types);
11442 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11444 /* We've instantiated a template before its default arguments
11445 have been parsed. This can happen for a nested template
11446 class, and is not an error unless we require the default
11447 argument in a call of this function. */
11448 remaining_arg_types =
11449 tree_cons (default_arg, type, remaining_arg_types);
11450 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11452 else
11453 remaining_arg_types =
11454 hash_tree_cons (default_arg, type, remaining_arg_types);
11457 return remaining_arg_types;
11460 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11461 *not* handle the exception-specification for FNTYPE, because the
11462 initial substitution of explicitly provided template parameters
11463 during argument deduction forbids substitution into the
11464 exception-specification:
11466 [temp.deduct]
11468 All references in the function type of the function template to the
11469 corresponding template parameters are replaced by the specified tem-
11470 plate argument values. If a substitution in a template parameter or
11471 in the function type of the function template results in an invalid
11472 type, type deduction fails. [Note: The equivalent substitution in
11473 exception specifications is done only when the function is instanti-
11474 ated, at which point a program is ill-formed if the substitution
11475 results in an invalid type.] */
11477 static tree
11478 tsubst_function_type (tree t,
11479 tree args,
11480 tsubst_flags_t complain,
11481 tree in_decl)
11483 tree return_type;
11484 tree arg_types;
11485 tree fntype;
11487 /* The TYPE_CONTEXT is not used for function/method types. */
11488 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11490 /* Substitute the return type. */
11491 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11492 if (return_type == error_mark_node)
11493 return error_mark_node;
11494 /* DR 486 clarifies that creation of a function type with an
11495 invalid return type is a deduction failure. */
11496 if (TREE_CODE (return_type) == ARRAY_TYPE
11497 || TREE_CODE (return_type) == FUNCTION_TYPE)
11499 if (complain & tf_error)
11501 if (TREE_CODE (return_type) == ARRAY_TYPE)
11502 error ("function returning an array");
11503 else
11504 error ("function returning a function");
11506 return error_mark_node;
11508 /* And DR 657. */
11509 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11510 return error_mark_node;
11512 /* Substitute the argument types. */
11513 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11514 complain, in_decl);
11515 if (arg_types == error_mark_node)
11516 return error_mark_node;
11518 /* Construct a new type node and return it. */
11519 if (TREE_CODE (t) == FUNCTION_TYPE)
11521 fntype = build_function_type (return_type, arg_types);
11522 fntype = apply_memfn_quals (fntype,
11523 type_memfn_quals (t),
11524 type_memfn_rqual (t));
11526 else
11528 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11529 if (! MAYBE_CLASS_TYPE_P (r))
11531 /* [temp.deduct]
11533 Type deduction may fail for any of the following
11534 reasons:
11536 -- Attempting to create "pointer to member of T" when T
11537 is not a class type. */
11538 if (complain & tf_error)
11539 error ("creating pointer to member function of non-class type %qT",
11541 return error_mark_node;
11544 fntype = build_method_type_directly (r, return_type,
11545 TREE_CHAIN (arg_types));
11546 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11548 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11550 return fntype;
11553 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11554 ARGS into that specification, and return the substituted
11555 specification. If there is no specification, return NULL_TREE. */
11557 static tree
11558 tsubst_exception_specification (tree fntype,
11559 tree args,
11560 tsubst_flags_t complain,
11561 tree in_decl,
11562 bool defer_ok)
11564 tree specs;
11565 tree new_specs;
11567 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11568 new_specs = NULL_TREE;
11569 if (specs && TREE_PURPOSE (specs))
11571 /* A noexcept-specifier. */
11572 tree expr = TREE_PURPOSE (specs);
11573 if (TREE_CODE (expr) == INTEGER_CST)
11574 new_specs = expr;
11575 else if (defer_ok)
11577 /* Defer instantiation of noexcept-specifiers to avoid
11578 excessive instantiations (c++/49107). */
11579 new_specs = make_node (DEFERRED_NOEXCEPT);
11580 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11582 /* We already partially instantiated this member template,
11583 so combine the new args with the old. */
11584 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11585 = DEFERRED_NOEXCEPT_PATTERN (expr);
11586 DEFERRED_NOEXCEPT_ARGS (new_specs)
11587 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11589 else
11591 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11592 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11595 else
11596 new_specs = tsubst_copy_and_build
11597 (expr, args, complain, in_decl, /*function_p=*/false,
11598 /*integral_constant_expression_p=*/true);
11599 new_specs = build_noexcept_spec (new_specs, complain);
11601 else if (specs)
11603 if (! TREE_VALUE (specs))
11604 new_specs = specs;
11605 else
11606 while (specs)
11608 tree spec;
11609 int i, len = 1;
11610 tree expanded_specs = NULL_TREE;
11612 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11614 /* Expand the pack expansion type. */
11615 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11616 args, complain,
11617 in_decl);
11619 if (expanded_specs == error_mark_node)
11620 return error_mark_node;
11621 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11622 len = TREE_VEC_LENGTH (expanded_specs);
11623 else
11625 /* We're substituting into a member template, so
11626 we got a TYPE_PACK_EXPANSION back. Add that
11627 expansion and move on. */
11628 gcc_assert (TREE_CODE (expanded_specs)
11629 == TYPE_PACK_EXPANSION);
11630 new_specs = add_exception_specifier (new_specs,
11631 expanded_specs,
11632 complain);
11633 specs = TREE_CHAIN (specs);
11634 continue;
11638 for (i = 0; i < len; ++i)
11640 if (expanded_specs)
11641 spec = TREE_VEC_ELT (expanded_specs, i);
11642 else
11643 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11644 if (spec == error_mark_node)
11645 return spec;
11646 new_specs = add_exception_specifier (new_specs, spec,
11647 complain);
11650 specs = TREE_CHAIN (specs);
11653 return new_specs;
11656 /* Take the tree structure T and replace template parameters used
11657 therein with the argument vector ARGS. IN_DECL is an associated
11658 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11659 Issue error and warning messages under control of COMPLAIN. Note
11660 that we must be relatively non-tolerant of extensions here, in
11661 order to preserve conformance; if we allow substitutions that
11662 should not be allowed, we may allow argument deductions that should
11663 not succeed, and therefore report ambiguous overload situations
11664 where there are none. In theory, we could allow the substitution,
11665 but indicate that it should have failed, and allow our caller to
11666 make sure that the right thing happens, but we don't try to do this
11667 yet.
11669 This function is used for dealing with types, decls and the like;
11670 for expressions, use tsubst_expr or tsubst_copy. */
11672 tree
11673 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11675 enum tree_code code;
11676 tree type, r = NULL_TREE;
11678 if (t == NULL_TREE || t == error_mark_node
11679 || t == integer_type_node
11680 || t == void_type_node
11681 || t == char_type_node
11682 || t == unknown_type_node
11683 || TREE_CODE (t) == NAMESPACE_DECL
11684 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11685 return t;
11687 if (DECL_P (t))
11688 return tsubst_decl (t, args, complain);
11690 if (args == NULL_TREE)
11691 return t;
11693 code = TREE_CODE (t);
11695 if (code == IDENTIFIER_NODE)
11696 type = IDENTIFIER_TYPE_VALUE (t);
11697 else
11698 type = TREE_TYPE (t);
11700 gcc_assert (type != unknown_type_node);
11702 /* Reuse typedefs. We need to do this to handle dependent attributes,
11703 such as attribute aligned. */
11704 if (TYPE_P (t)
11705 && typedef_variant_p (t))
11707 tree decl = TYPE_NAME (t);
11709 if (alias_template_specialization_p (t))
11711 /* DECL represents an alias template and we want to
11712 instantiate it. */
11713 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11714 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11715 r = instantiate_alias_template (tmpl, gen_args, complain);
11717 else if (DECL_CLASS_SCOPE_P (decl)
11718 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11719 && uses_template_parms (DECL_CONTEXT (decl)))
11721 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11722 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11723 r = retrieve_specialization (tmpl, gen_args, 0);
11725 else if (DECL_FUNCTION_SCOPE_P (decl)
11726 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11727 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11728 r = retrieve_local_specialization (decl);
11729 else
11730 /* The typedef is from a non-template context. */
11731 return t;
11733 if (r)
11735 r = TREE_TYPE (r);
11736 r = cp_build_qualified_type_real
11737 (r, cp_type_quals (t) | cp_type_quals (r),
11738 complain | tf_ignore_bad_quals);
11739 return r;
11741 else
11743 /* We don't have an instantiation yet, so drop the typedef. */
11744 int quals = cp_type_quals (t);
11745 t = DECL_ORIGINAL_TYPE (decl);
11746 t = cp_build_qualified_type_real (t, quals,
11747 complain | tf_ignore_bad_quals);
11751 if (type
11752 && code != TYPENAME_TYPE
11753 && code != TEMPLATE_TYPE_PARM
11754 && code != IDENTIFIER_NODE
11755 && code != FUNCTION_TYPE
11756 && code != METHOD_TYPE)
11757 type = tsubst (type, args, complain, in_decl);
11758 if (type == error_mark_node)
11759 return error_mark_node;
11761 switch (code)
11763 case RECORD_TYPE:
11764 case UNION_TYPE:
11765 case ENUMERAL_TYPE:
11766 return tsubst_aggr_type (t, args, complain, in_decl,
11767 /*entering_scope=*/0);
11769 case ERROR_MARK:
11770 case IDENTIFIER_NODE:
11771 case VOID_TYPE:
11772 case REAL_TYPE:
11773 case COMPLEX_TYPE:
11774 case VECTOR_TYPE:
11775 case BOOLEAN_TYPE:
11776 case NULLPTR_TYPE:
11777 case LANG_TYPE:
11778 return t;
11780 case INTEGER_TYPE:
11781 if (t == integer_type_node)
11782 return t;
11784 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11785 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11786 return t;
11789 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11791 max = tsubst_expr (omax, args, complain, in_decl,
11792 /*integral_constant_expression_p=*/false);
11794 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11795 needed. */
11796 if (TREE_CODE (max) == NOP_EXPR
11797 && TREE_SIDE_EFFECTS (omax)
11798 && !TREE_TYPE (max))
11799 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11801 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11802 with TREE_SIDE_EFFECTS that indicates this is not an integral
11803 constant expression. */
11804 if (processing_template_decl
11805 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11807 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11808 TREE_SIDE_EFFECTS (max) = 1;
11811 return compute_array_index_type (NULL_TREE, max, complain);
11814 case TEMPLATE_TYPE_PARM:
11815 case TEMPLATE_TEMPLATE_PARM:
11816 case BOUND_TEMPLATE_TEMPLATE_PARM:
11817 case TEMPLATE_PARM_INDEX:
11819 int idx;
11820 int level;
11821 int levels;
11822 tree arg = NULL_TREE;
11824 r = NULL_TREE;
11826 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11827 template_parm_level_and_index (t, &level, &idx);
11829 levels = TMPL_ARGS_DEPTH (args);
11830 if (level <= levels)
11832 arg = TMPL_ARG (args, level, idx);
11834 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11836 /* See through ARGUMENT_PACK_SELECT arguments. */
11837 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11838 /* If the selected argument is an expansion E, that most
11839 likely means we were called from
11840 gen_elem_of_pack_expansion_instantiation during the
11841 substituting of pack an argument pack (which Ith
11842 element is a pack expansion, where I is
11843 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11844 In this case, the Ith element resulting from this
11845 substituting is going to be a pack expansion, which
11846 pattern is the pattern of E. Let's return the
11847 pattern of E, and
11848 gen_elem_of_pack_expansion_instantiation will
11849 build the resulting pack expansion from it. */
11850 if (PACK_EXPANSION_P (arg))
11851 arg = PACK_EXPANSION_PATTERN (arg);
11855 if (arg == error_mark_node)
11856 return error_mark_node;
11857 else if (arg != NULL_TREE)
11859 if (ARGUMENT_PACK_P (arg))
11860 /* If ARG is an argument pack, we don't actually want to
11861 perform a substitution here, because substitutions
11862 for argument packs are only done
11863 element-by-element. We can get to this point when
11864 substituting the type of a non-type template
11865 parameter pack, when that type actually contains
11866 template parameter packs from an outer template, e.g.,
11868 template<typename... Types> struct A {
11869 template<Types... Values> struct B { };
11870 }; */
11871 return t;
11873 if (code == TEMPLATE_TYPE_PARM)
11875 int quals;
11876 gcc_assert (TYPE_P (arg));
11878 quals = cp_type_quals (arg) | cp_type_quals (t);
11880 return cp_build_qualified_type_real
11881 (arg, quals, complain | tf_ignore_bad_quals);
11883 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11885 /* We are processing a type constructed from a
11886 template template parameter. */
11887 tree argvec = tsubst (TYPE_TI_ARGS (t),
11888 args, complain, in_decl);
11889 if (argvec == error_mark_node)
11890 return error_mark_node;
11892 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11893 || TREE_CODE (arg) == TEMPLATE_DECL
11894 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11896 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11897 /* Consider this code:
11899 template <template <class> class Template>
11900 struct Internal {
11901 template <class Arg> using Bind = Template<Arg>;
11904 template <template <class> class Template, class Arg>
11905 using Instantiate = Template<Arg>; //#0
11907 template <template <class> class Template,
11908 class Argument>
11909 using Bind =
11910 Instantiate<Internal<Template>::template Bind,
11911 Argument>; //#1
11913 When #1 is parsed, the
11914 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11915 parameter `Template' in #0 matches the
11916 UNBOUND_CLASS_TEMPLATE representing the argument
11917 `Internal<Template>::template Bind'; We then want
11918 to assemble the type `Bind<Argument>' that can't
11919 be fully created right now, because
11920 `Internal<Template>' not being complete, the Bind
11921 template cannot be looked up in that context. So
11922 we need to "store" `Bind<Argument>' for later
11923 when the context of Bind becomes complete. Let's
11924 store that in a TYPENAME_TYPE. */
11925 return make_typename_type (TYPE_CONTEXT (arg),
11926 build_nt (TEMPLATE_ID_EXPR,
11927 TYPE_IDENTIFIER (arg),
11928 argvec),
11929 typename_type,
11930 complain);
11932 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11933 are resolving nested-types in the signature of a
11934 member function templates. Otherwise ARG is a
11935 TEMPLATE_DECL and is the real template to be
11936 instantiated. */
11937 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11938 arg = TYPE_NAME (arg);
11940 r = lookup_template_class (arg,
11941 argvec, in_decl,
11942 DECL_CONTEXT (arg),
11943 /*entering_scope=*/0,
11944 complain);
11945 return cp_build_qualified_type_real
11946 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11948 else
11949 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11950 return convert_from_reference (unshare_expr (arg));
11953 if (level == 1)
11954 /* This can happen during the attempted tsubst'ing in
11955 unify. This means that we don't yet have any information
11956 about the template parameter in question. */
11957 return t;
11959 /* Early in template argument deduction substitution, we don't
11960 want to reduce the level of 'auto', or it will be confused
11961 with a normal template parm in subsequent deduction. */
11962 if (is_auto (t) && (complain & tf_partial))
11963 return t;
11965 /* If we get here, we must have been looking at a parm for a
11966 more deeply nested template. Make a new version of this
11967 template parameter, but with a lower level. */
11968 switch (code)
11970 case TEMPLATE_TYPE_PARM:
11971 case TEMPLATE_TEMPLATE_PARM:
11972 case BOUND_TEMPLATE_TEMPLATE_PARM:
11973 if (cp_type_quals (t))
11975 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11976 r = cp_build_qualified_type_real
11977 (r, cp_type_quals (t),
11978 complain | (code == TEMPLATE_TYPE_PARM
11979 ? tf_ignore_bad_quals : 0));
11981 else
11983 r = copy_type (t);
11984 TEMPLATE_TYPE_PARM_INDEX (r)
11985 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11986 r, levels, args, complain);
11987 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11988 TYPE_MAIN_VARIANT (r) = r;
11989 TYPE_POINTER_TO (r) = NULL_TREE;
11990 TYPE_REFERENCE_TO (r) = NULL_TREE;
11992 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11993 /* We have reduced the level of the template
11994 template parameter, but not the levels of its
11995 template parameters, so canonical_type_parameter
11996 will not be able to find the canonical template
11997 template parameter for this level. Thus, we
11998 require structural equality checking to compare
11999 TEMPLATE_TEMPLATE_PARMs. */
12000 SET_TYPE_STRUCTURAL_EQUALITY (r);
12001 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12002 SET_TYPE_STRUCTURAL_EQUALITY (r);
12003 else
12004 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12006 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12008 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12009 complain, in_decl);
12010 if (argvec == error_mark_node)
12011 return error_mark_node;
12013 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12014 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12017 break;
12019 case TEMPLATE_PARM_INDEX:
12020 r = reduce_template_parm_level (t, type, levels, args, complain);
12021 break;
12023 default:
12024 gcc_unreachable ();
12027 return r;
12030 case TREE_LIST:
12032 tree purpose, value, chain;
12034 if (t == void_list_node)
12035 return t;
12037 purpose = TREE_PURPOSE (t);
12038 if (purpose)
12040 purpose = tsubst (purpose, args, complain, in_decl);
12041 if (purpose == error_mark_node)
12042 return error_mark_node;
12044 value = TREE_VALUE (t);
12045 if (value)
12047 value = tsubst (value, args, complain, in_decl);
12048 if (value == error_mark_node)
12049 return error_mark_node;
12051 chain = TREE_CHAIN (t);
12052 if (chain && chain != void_type_node)
12054 chain = tsubst (chain, args, complain, in_decl);
12055 if (chain == error_mark_node)
12056 return error_mark_node;
12058 if (purpose == TREE_PURPOSE (t)
12059 && value == TREE_VALUE (t)
12060 && chain == TREE_CHAIN (t))
12061 return t;
12062 return hash_tree_cons (purpose, value, chain);
12065 case TREE_BINFO:
12066 /* We should never be tsubsting a binfo. */
12067 gcc_unreachable ();
12069 case TREE_VEC:
12070 /* A vector of template arguments. */
12071 gcc_assert (!type);
12072 return tsubst_template_args (t, args, complain, in_decl);
12074 case POINTER_TYPE:
12075 case REFERENCE_TYPE:
12077 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12078 return t;
12080 /* [temp.deduct]
12082 Type deduction may fail for any of the following
12083 reasons:
12085 -- Attempting to create a pointer to reference type.
12086 -- Attempting to create a reference to a reference type or
12087 a reference to void.
12089 Core issue 106 says that creating a reference to a reference
12090 during instantiation is no longer a cause for failure. We
12091 only enforce this check in strict C++98 mode. */
12092 if ((TREE_CODE (type) == REFERENCE_TYPE
12093 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12094 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12096 static location_t last_loc;
12098 /* We keep track of the last time we issued this error
12099 message to avoid spewing a ton of messages during a
12100 single bad template instantiation. */
12101 if (complain & tf_error
12102 && last_loc != input_location)
12104 if (VOID_TYPE_P (type))
12105 error ("forming reference to void");
12106 else if (code == POINTER_TYPE)
12107 error ("forming pointer to reference type %qT", type);
12108 else
12109 error ("forming reference to reference type %qT", type);
12110 last_loc = input_location;
12113 return error_mark_node;
12115 else if (TREE_CODE (type) == FUNCTION_TYPE
12116 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12117 || type_memfn_rqual (type) != REF_QUAL_NONE))
12119 if (complain & tf_error)
12121 if (code == POINTER_TYPE)
12122 error ("forming pointer to qualified function type %qT",
12123 type);
12124 else
12125 error ("forming reference to qualified function type %qT",
12126 type);
12128 return error_mark_node;
12130 else if (code == POINTER_TYPE)
12132 r = build_pointer_type (type);
12133 if (TREE_CODE (type) == METHOD_TYPE)
12134 r = build_ptrmemfunc_type (r);
12136 else if (TREE_CODE (type) == REFERENCE_TYPE)
12137 /* In C++0x, during template argument substitution, when there is an
12138 attempt to create a reference to a reference type, reference
12139 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12141 "If a template-argument for a template-parameter T names a type
12142 that is a reference to a type A, an attempt to create the type
12143 'lvalue reference to cv T' creates the type 'lvalue reference to
12144 A,' while an attempt to create the type type rvalue reference to
12145 cv T' creates the type T"
12147 r = cp_build_reference_type
12148 (TREE_TYPE (type),
12149 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12150 else
12151 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12152 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12154 if (cxx_dialect >= cxx1y
12155 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
12156 && array_of_runtime_bound_p (type))
12158 if (complain & tf_warning_or_error)
12159 pedwarn
12160 (input_location, OPT_Wvla,
12161 code == REFERENCE_TYPE
12162 ? G_("cannot declare reference to array of runtime bound")
12163 : G_("cannot declare pointer to array of runtime bound"));
12164 else
12165 r = error_mark_node;
12168 if (r != error_mark_node)
12169 /* Will this ever be needed for TYPE_..._TO values? */
12170 layout_type (r);
12172 return r;
12174 case OFFSET_TYPE:
12176 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12177 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12179 /* [temp.deduct]
12181 Type deduction may fail for any of the following
12182 reasons:
12184 -- Attempting to create "pointer to member of T" when T
12185 is not a class type. */
12186 if (complain & tf_error)
12187 error ("creating pointer to member of non-class type %qT", r);
12188 return error_mark_node;
12190 if (TREE_CODE (type) == REFERENCE_TYPE)
12192 if (complain & tf_error)
12193 error ("creating pointer to member reference type %qT", type);
12194 return error_mark_node;
12196 if (VOID_TYPE_P (type))
12198 if (complain & tf_error)
12199 error ("creating pointer to member of type void");
12200 return error_mark_node;
12202 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12203 if (TREE_CODE (type) == FUNCTION_TYPE)
12205 /* The type of the implicit object parameter gets its
12206 cv-qualifiers from the FUNCTION_TYPE. */
12207 tree memptr;
12208 tree method_type
12209 = build_memfn_type (type, r, type_memfn_quals (type),
12210 type_memfn_rqual (type));
12211 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12212 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12213 complain);
12215 else
12216 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12217 cp_type_quals (t),
12218 complain);
12220 case FUNCTION_TYPE:
12221 case METHOD_TYPE:
12223 tree fntype;
12224 tree specs;
12225 fntype = tsubst_function_type (t, args, complain, in_decl);
12226 if (fntype == error_mark_node)
12227 return error_mark_node;
12229 /* Substitute the exception specification. */
12230 specs = tsubst_exception_specification (t, args, complain,
12231 in_decl, /*defer_ok*/true);
12232 if (specs == error_mark_node)
12233 return error_mark_node;
12234 if (specs)
12235 fntype = build_exception_variant (fntype, specs);
12236 return fntype;
12238 case ARRAY_TYPE:
12240 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12241 if (domain == error_mark_node)
12242 return error_mark_node;
12244 /* As an optimization, we avoid regenerating the array type if
12245 it will obviously be the same as T. */
12246 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12247 return t;
12249 /* These checks should match the ones in grokdeclarator.
12251 [temp.deduct]
12253 The deduction may fail for any of the following reasons:
12255 -- Attempting to create an array with an element type that
12256 is void, a function type, or a reference type, or [DR337]
12257 an abstract class type. */
12258 if (VOID_TYPE_P (type)
12259 || TREE_CODE (type) == FUNCTION_TYPE
12260 || TREE_CODE (type) == REFERENCE_TYPE)
12262 if (complain & tf_error)
12263 error ("creating array of %qT", type);
12264 return error_mark_node;
12267 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12268 return error_mark_node;
12270 r = build_cplus_array_type (type, domain);
12272 if (TYPE_USER_ALIGN (t))
12274 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12275 TYPE_USER_ALIGN (r) = 1;
12278 return r;
12281 case TYPENAME_TYPE:
12283 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12284 in_decl, /*entering_scope=*/1);
12285 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12286 complain, in_decl);
12288 if (ctx == error_mark_node || f == error_mark_node)
12289 return error_mark_node;
12291 if (!MAYBE_CLASS_TYPE_P (ctx))
12293 if (complain & tf_error)
12294 error ("%qT is not a class, struct, or union type", ctx);
12295 return error_mark_node;
12297 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12299 /* Normally, make_typename_type does not require that the CTX
12300 have complete type in order to allow things like:
12302 template <class T> struct S { typename S<T>::X Y; };
12304 But, such constructs have already been resolved by this
12305 point, so here CTX really should have complete type, unless
12306 it's a partial instantiation. */
12307 ctx = complete_type (ctx);
12308 if (!COMPLETE_TYPE_P (ctx))
12310 if (complain & tf_error)
12311 cxx_incomplete_type_error (NULL_TREE, ctx);
12312 return error_mark_node;
12316 f = make_typename_type (ctx, f, typename_type,
12317 complain | tf_keep_type_decl);
12318 if (f == error_mark_node)
12319 return f;
12320 if (TREE_CODE (f) == TYPE_DECL)
12322 complain |= tf_ignore_bad_quals;
12323 f = TREE_TYPE (f);
12326 if (TREE_CODE (f) != TYPENAME_TYPE)
12328 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12330 if (complain & tf_error)
12331 error ("%qT resolves to %qT, which is not an enumeration type",
12332 t, f);
12333 else
12334 return error_mark_node;
12336 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12338 if (complain & tf_error)
12339 error ("%qT resolves to %qT, which is is not a class type",
12340 t, f);
12341 else
12342 return error_mark_node;
12346 return cp_build_qualified_type_real
12347 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12350 case UNBOUND_CLASS_TEMPLATE:
12352 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12353 in_decl, /*entering_scope=*/1);
12354 tree name = TYPE_IDENTIFIER (t);
12355 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12357 if (ctx == error_mark_node || name == error_mark_node)
12358 return error_mark_node;
12360 if (parm_list)
12361 parm_list = tsubst_template_parms (parm_list, args, complain);
12362 return make_unbound_class_template (ctx, name, parm_list, complain);
12365 case TYPEOF_TYPE:
12367 tree type;
12369 ++cp_unevaluated_operand;
12370 ++c_inhibit_evaluation_warnings;
12372 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12373 complain, in_decl,
12374 /*integral_constant_expression_p=*/false);
12376 --cp_unevaluated_operand;
12377 --c_inhibit_evaluation_warnings;
12379 type = finish_typeof (type);
12380 return cp_build_qualified_type_real (type,
12381 cp_type_quals (t)
12382 | cp_type_quals (type),
12383 complain);
12386 case DECLTYPE_TYPE:
12388 tree type;
12390 ++cp_unevaluated_operand;
12391 ++c_inhibit_evaluation_warnings;
12393 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12394 complain|tf_decltype, in_decl,
12395 /*function_p*/false,
12396 /*integral_constant_expression*/false);
12398 --cp_unevaluated_operand;
12399 --c_inhibit_evaluation_warnings;
12401 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12402 type = lambda_capture_field_type (type,
12403 DECLTYPE_FOR_INIT_CAPTURE (t));
12404 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12405 type = lambda_proxy_type (type);
12406 else
12408 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12409 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12410 && EXPR_P (type))
12411 /* In a template ~id could be either a complement expression
12412 or an unqualified-id naming a destructor; if instantiating
12413 it produces an expression, it's not an id-expression or
12414 member access. */
12415 id = false;
12416 type = finish_decltype_type (type, id, complain);
12418 return cp_build_qualified_type_real (type,
12419 cp_type_quals (t)
12420 | cp_type_quals (type),
12421 complain);
12424 case UNDERLYING_TYPE:
12426 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12427 complain, in_decl);
12428 return finish_underlying_type (type);
12431 case TYPE_ARGUMENT_PACK:
12432 case NONTYPE_ARGUMENT_PACK:
12434 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12435 tree packed_out =
12436 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12437 args,
12438 complain,
12439 in_decl);
12440 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12442 /* For template nontype argument packs, also substitute into
12443 the type. */
12444 if (code == NONTYPE_ARGUMENT_PACK)
12445 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12447 return r;
12449 break;
12451 case INTEGER_CST:
12452 case REAL_CST:
12453 case STRING_CST:
12454 case PLUS_EXPR:
12455 case MINUS_EXPR:
12456 case NEGATE_EXPR:
12457 case NOP_EXPR:
12458 case INDIRECT_REF:
12459 case ADDR_EXPR:
12460 case CALL_EXPR:
12461 case ARRAY_REF:
12462 case SCOPE_REF:
12463 /* We should use one of the expression tsubsts for these codes. */
12464 gcc_unreachable ();
12466 default:
12467 sorry ("use of %qs in template", get_tree_code_name (code));
12468 return error_mark_node;
12472 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12473 type of the expression on the left-hand side of the "." or "->"
12474 operator. */
12476 static tree
12477 tsubst_baselink (tree baselink, tree object_type,
12478 tree args, tsubst_flags_t complain, tree in_decl)
12480 tree name;
12481 tree qualifying_scope;
12482 tree fns;
12483 tree optype;
12484 tree template_args = 0;
12485 bool template_id_p = false;
12486 bool qualified = BASELINK_QUALIFIED_P (baselink);
12488 /* A baselink indicates a function from a base class. Both the
12489 BASELINK_ACCESS_BINFO and the base class referenced may
12490 indicate bases of the template class, rather than the
12491 instantiated class. In addition, lookups that were not
12492 ambiguous before may be ambiguous now. Therefore, we perform
12493 the lookup again. */
12494 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12495 qualifying_scope = tsubst (qualifying_scope, args,
12496 complain, in_decl);
12497 fns = BASELINK_FUNCTIONS (baselink);
12498 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12499 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12501 template_id_p = true;
12502 template_args = TREE_OPERAND (fns, 1);
12503 fns = TREE_OPERAND (fns, 0);
12504 if (template_args)
12505 template_args = tsubst_template_args (template_args, args,
12506 complain, in_decl);
12508 name = DECL_NAME (get_first_fn (fns));
12509 if (IDENTIFIER_TYPENAME_P (name))
12510 name = mangle_conv_op_name_for_type (optype);
12511 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12512 if (!baselink)
12513 return error_mark_node;
12515 /* If lookup found a single function, mark it as used at this
12516 point. (If it lookup found multiple functions the one selected
12517 later by overload resolution will be marked as used at that
12518 point.) */
12519 if (BASELINK_P (baselink))
12520 fns = BASELINK_FUNCTIONS (baselink);
12521 if (!template_id_p && !really_overloaded_fn (fns))
12522 mark_used (OVL_CURRENT (fns));
12524 /* Add back the template arguments, if present. */
12525 if (BASELINK_P (baselink) && template_id_p)
12526 BASELINK_FUNCTIONS (baselink)
12527 = build_nt (TEMPLATE_ID_EXPR,
12528 BASELINK_FUNCTIONS (baselink),
12529 template_args);
12530 /* Update the conversion operator type. */
12531 BASELINK_OPTYPE (baselink) = optype;
12533 if (!object_type)
12534 object_type = current_class_type;
12536 if (qualified)
12537 baselink = adjust_result_of_qualified_name_lookup (baselink,
12538 qualifying_scope,
12539 object_type);
12540 return baselink;
12543 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12544 true if the qualified-id will be a postfix-expression in-and-of
12545 itself; false if more of the postfix-expression follows the
12546 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12547 of "&". */
12549 static tree
12550 tsubst_qualified_id (tree qualified_id, tree args,
12551 tsubst_flags_t complain, tree in_decl,
12552 bool done, bool address_p)
12554 tree expr;
12555 tree scope;
12556 tree name;
12557 bool is_template;
12558 tree template_args;
12559 location_t loc = UNKNOWN_LOCATION;
12561 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12563 /* Figure out what name to look up. */
12564 name = TREE_OPERAND (qualified_id, 1);
12565 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12567 is_template = true;
12568 loc = EXPR_LOCATION (name);
12569 template_args = TREE_OPERAND (name, 1);
12570 if (template_args)
12571 template_args = tsubst_template_args (template_args, args,
12572 complain, in_decl);
12573 name = TREE_OPERAND (name, 0);
12575 else
12577 is_template = false;
12578 template_args = NULL_TREE;
12581 /* Substitute into the qualifying scope. When there are no ARGS, we
12582 are just trying to simplify a non-dependent expression. In that
12583 case the qualifying scope may be dependent, and, in any case,
12584 substituting will not help. */
12585 scope = TREE_OPERAND (qualified_id, 0);
12586 if (args)
12588 scope = tsubst (scope, args, complain, in_decl);
12589 expr = tsubst_copy (name, args, complain, in_decl);
12591 else
12592 expr = name;
12594 if (dependent_scope_p (scope))
12596 if (is_template)
12597 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12598 return build_qualified_name (NULL_TREE, scope, expr,
12599 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12602 if (!BASELINK_P (name) && !DECL_P (expr))
12604 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12606 /* A BIT_NOT_EXPR is used to represent a destructor. */
12607 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12609 error ("qualifying type %qT does not match destructor name ~%qT",
12610 scope, TREE_OPERAND (expr, 0));
12611 expr = error_mark_node;
12613 else
12614 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12615 /*is_type_p=*/0, false);
12617 else
12618 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12619 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12620 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12622 if (complain & tf_error)
12624 error ("dependent-name %qE is parsed as a non-type, but "
12625 "instantiation yields a type", qualified_id);
12626 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12628 return error_mark_node;
12632 if (DECL_P (expr))
12634 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12635 scope);
12636 /* Remember that there was a reference to this entity. */
12637 mark_used (expr);
12640 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12642 if (complain & tf_error)
12643 qualified_name_lookup_error (scope,
12644 TREE_OPERAND (qualified_id, 1),
12645 expr, input_location);
12646 return error_mark_node;
12649 if (is_template)
12650 expr = lookup_template_function (expr, template_args);
12652 if (expr == error_mark_node && complain & tf_error)
12653 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12654 expr, input_location);
12655 else if (TYPE_P (scope))
12657 expr = (adjust_result_of_qualified_name_lookup
12658 (expr, scope, current_nonlambda_class_type ()));
12659 expr = (finish_qualified_id_expr
12660 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12661 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12662 /*template_arg_p=*/false, complain));
12665 /* Expressions do not generally have reference type. */
12666 if (TREE_CODE (expr) != SCOPE_REF
12667 /* However, if we're about to form a pointer-to-member, we just
12668 want the referenced member referenced. */
12669 && TREE_CODE (expr) != OFFSET_REF)
12670 expr = convert_from_reference (expr);
12672 return expr;
12675 /* Like tsubst, but deals with expressions. This function just replaces
12676 template parms; to finish processing the resultant expression, use
12677 tsubst_copy_and_build or tsubst_expr. */
12679 static tree
12680 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12682 enum tree_code code;
12683 tree r;
12685 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12686 return t;
12688 code = TREE_CODE (t);
12690 switch (code)
12692 case PARM_DECL:
12693 r = retrieve_local_specialization (t);
12695 if (r == NULL_TREE)
12697 /* We get here for a use of 'this' in an NSDMI. */
12698 if (DECL_NAME (t) == this_identifier
12699 && at_function_scope_p ()
12700 && DECL_CONSTRUCTOR_P (current_function_decl))
12701 return current_class_ptr;
12703 /* This can happen for a parameter name used later in a function
12704 declaration (such as in a late-specified return type). Just
12705 make a dummy decl, since it's only used for its type. */
12706 gcc_assert (cp_unevaluated_operand != 0);
12707 r = tsubst_decl (t, args, complain);
12708 /* Give it the template pattern as its context; its true context
12709 hasn't been instantiated yet and this is good enough for
12710 mangling. */
12711 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12714 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12715 r = ARGUMENT_PACK_SELECT_ARG (r);
12716 mark_used (r);
12717 return r;
12719 case CONST_DECL:
12721 tree enum_type;
12722 tree v;
12724 if (DECL_TEMPLATE_PARM_P (t))
12725 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12726 /* There is no need to substitute into namespace-scope
12727 enumerators. */
12728 if (DECL_NAMESPACE_SCOPE_P (t))
12729 return t;
12730 /* If ARGS is NULL, then T is known to be non-dependent. */
12731 if (args == NULL_TREE)
12732 return integral_constant_value (t);
12734 /* Unfortunately, we cannot just call lookup_name here.
12735 Consider:
12737 template <int I> int f() {
12738 enum E { a = I };
12739 struct S { void g() { E e = a; } };
12742 When we instantiate f<7>::S::g(), say, lookup_name is not
12743 clever enough to find f<7>::a. */
12744 enum_type
12745 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12746 /*entering_scope=*/0);
12748 for (v = TYPE_VALUES (enum_type);
12749 v != NULL_TREE;
12750 v = TREE_CHAIN (v))
12751 if (TREE_PURPOSE (v) == DECL_NAME (t))
12752 return TREE_VALUE (v);
12754 /* We didn't find the name. That should never happen; if
12755 name-lookup found it during preliminary parsing, we
12756 should find it again here during instantiation. */
12757 gcc_unreachable ();
12759 return t;
12761 case FIELD_DECL:
12762 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12764 /* Check for a local specialization set up by
12765 tsubst_pack_expansion. */
12766 tree r = retrieve_local_specialization (t);
12767 if (r)
12769 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12770 r = ARGUMENT_PACK_SELECT_ARG (r);
12771 return r;
12774 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12775 tsubst_decl put in the hash table. */
12776 return retrieve_specialization (t, args, 0);
12779 if (DECL_CONTEXT (t))
12781 tree ctx;
12783 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12784 /*entering_scope=*/1);
12785 if (ctx != DECL_CONTEXT (t))
12787 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12788 if (!r)
12790 if (complain & tf_error)
12791 error ("using invalid field %qD", t);
12792 return error_mark_node;
12794 return r;
12798 return t;
12800 case VAR_DECL:
12801 case FUNCTION_DECL:
12802 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12803 r = tsubst (t, args, complain, in_decl);
12804 else if (local_variable_p (t))
12806 r = retrieve_local_specialization (t);
12807 if (r == NULL_TREE)
12809 if (DECL_ANON_UNION_VAR_P (t))
12811 /* Just use name lookup to find a member alias for an
12812 anonymous union, but then add it to the hash table. */
12813 r = lookup_name (DECL_NAME (t));
12814 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12815 register_local_specialization (r, t);
12817 else
12819 gcc_assert (errorcount || sorrycount);
12820 return error_mark_node;
12824 else
12825 r = t;
12826 mark_used (r);
12827 return r;
12829 case NAMESPACE_DECL:
12830 return t;
12832 case OVERLOAD:
12833 /* An OVERLOAD will always be a non-dependent overload set; an
12834 overload set from function scope will just be represented with an
12835 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12836 gcc_assert (!uses_template_parms (t));
12837 return t;
12839 case BASELINK:
12840 return tsubst_baselink (t, current_nonlambda_class_type (),
12841 args, complain, in_decl);
12843 case TEMPLATE_DECL:
12844 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12845 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12846 args, complain, in_decl);
12847 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12848 return tsubst (t, args, complain, in_decl);
12849 else if (DECL_CLASS_SCOPE_P (t)
12850 && uses_template_parms (DECL_CONTEXT (t)))
12852 /* Template template argument like the following example need
12853 special treatment:
12855 template <template <class> class TT> struct C {};
12856 template <class T> struct D {
12857 template <class U> struct E {};
12858 C<E> c; // #1
12860 D<int> d; // #2
12862 We are processing the template argument `E' in #1 for
12863 the template instantiation #2. Originally, `E' is a
12864 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12865 have to substitute this with one having context `D<int>'. */
12867 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12868 return lookup_field (context, DECL_NAME(t), 0, false);
12870 else
12871 /* Ordinary template template argument. */
12872 return t;
12874 case CAST_EXPR:
12875 case REINTERPRET_CAST_EXPR:
12876 case CONST_CAST_EXPR:
12877 case STATIC_CAST_EXPR:
12878 case DYNAMIC_CAST_EXPR:
12879 case IMPLICIT_CONV_EXPR:
12880 case CONVERT_EXPR:
12881 case NOP_EXPR:
12882 return build1
12883 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12884 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12886 case SIZEOF_EXPR:
12887 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12890 tree expanded, op = TREE_OPERAND (t, 0);
12891 int len = 0;
12893 if (SIZEOF_EXPR_TYPE_P (t))
12894 op = TREE_TYPE (op);
12896 ++cp_unevaluated_operand;
12897 ++c_inhibit_evaluation_warnings;
12898 /* We only want to compute the number of arguments. */
12899 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12900 --cp_unevaluated_operand;
12901 --c_inhibit_evaluation_warnings;
12903 if (TREE_CODE (expanded) == TREE_VEC)
12904 len = TREE_VEC_LENGTH (expanded);
12906 if (expanded == error_mark_node)
12907 return error_mark_node;
12908 else if (PACK_EXPANSION_P (expanded)
12909 || (TREE_CODE (expanded) == TREE_VEC
12910 && len > 0
12911 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12913 if (TREE_CODE (expanded) == TREE_VEC)
12914 expanded = TREE_VEC_ELT (expanded, len - 1);
12916 if (TYPE_P (expanded))
12917 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12918 complain & tf_error);
12919 else
12920 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12921 complain & tf_error);
12923 else
12924 return build_int_cst (size_type_node, len);
12926 if (SIZEOF_EXPR_TYPE_P (t))
12928 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12929 args, complain, in_decl);
12930 r = build1 (NOP_EXPR, r, error_mark_node);
12931 r = build1 (SIZEOF_EXPR,
12932 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12933 SIZEOF_EXPR_TYPE_P (r) = 1;
12934 return r;
12936 /* Fall through */
12938 case INDIRECT_REF:
12939 case NEGATE_EXPR:
12940 case TRUTH_NOT_EXPR:
12941 case BIT_NOT_EXPR:
12942 case ADDR_EXPR:
12943 case UNARY_PLUS_EXPR: /* Unary + */
12944 case ALIGNOF_EXPR:
12945 case AT_ENCODE_EXPR:
12946 case ARROW_EXPR:
12947 case THROW_EXPR:
12948 case TYPEID_EXPR:
12949 case REALPART_EXPR:
12950 case IMAGPART_EXPR:
12951 case PAREN_EXPR:
12952 return build1
12953 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12954 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12956 case COMPONENT_REF:
12958 tree object;
12959 tree name;
12961 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12962 name = TREE_OPERAND (t, 1);
12963 if (TREE_CODE (name) == BIT_NOT_EXPR)
12965 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12966 complain, in_decl);
12967 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12969 else if (TREE_CODE (name) == SCOPE_REF
12970 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12972 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12973 complain, in_decl);
12974 name = TREE_OPERAND (name, 1);
12975 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12976 complain, in_decl);
12977 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12978 name = build_qualified_name (/*type=*/NULL_TREE,
12979 base, name,
12980 /*template_p=*/false);
12982 else if (BASELINK_P (name))
12983 name = tsubst_baselink (name,
12984 non_reference (TREE_TYPE (object)),
12985 args, complain,
12986 in_decl);
12987 else
12988 name = tsubst_copy (name, args, complain, in_decl);
12989 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12992 case PLUS_EXPR:
12993 case MINUS_EXPR:
12994 case MULT_EXPR:
12995 case TRUNC_DIV_EXPR:
12996 case CEIL_DIV_EXPR:
12997 case FLOOR_DIV_EXPR:
12998 case ROUND_DIV_EXPR:
12999 case EXACT_DIV_EXPR:
13000 case BIT_AND_EXPR:
13001 case BIT_IOR_EXPR:
13002 case BIT_XOR_EXPR:
13003 case TRUNC_MOD_EXPR:
13004 case FLOOR_MOD_EXPR:
13005 case TRUTH_ANDIF_EXPR:
13006 case TRUTH_ORIF_EXPR:
13007 case TRUTH_AND_EXPR:
13008 case TRUTH_OR_EXPR:
13009 case RSHIFT_EXPR:
13010 case LSHIFT_EXPR:
13011 case RROTATE_EXPR:
13012 case LROTATE_EXPR:
13013 case EQ_EXPR:
13014 case NE_EXPR:
13015 case MAX_EXPR:
13016 case MIN_EXPR:
13017 case LE_EXPR:
13018 case GE_EXPR:
13019 case LT_EXPR:
13020 case GT_EXPR:
13021 case COMPOUND_EXPR:
13022 case DOTSTAR_EXPR:
13023 case MEMBER_REF:
13024 case PREDECREMENT_EXPR:
13025 case PREINCREMENT_EXPR:
13026 case POSTDECREMENT_EXPR:
13027 case POSTINCREMENT_EXPR:
13028 return build_nt
13029 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13030 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13032 case SCOPE_REF:
13033 return build_qualified_name (/*type=*/NULL_TREE,
13034 tsubst_copy (TREE_OPERAND (t, 0),
13035 args, complain, in_decl),
13036 tsubst_copy (TREE_OPERAND (t, 1),
13037 args, complain, in_decl),
13038 QUALIFIED_NAME_IS_TEMPLATE (t));
13040 case ARRAY_REF:
13041 return build_nt
13042 (ARRAY_REF,
13043 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13044 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
13045 NULL_TREE, NULL_TREE);
13047 case CALL_EXPR:
13049 int n = VL_EXP_OPERAND_LENGTH (t);
13050 tree result = build_vl_exp (CALL_EXPR, n);
13051 int i;
13052 for (i = 0; i < n; i++)
13053 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13054 complain, in_decl);
13055 return result;
13058 case COND_EXPR:
13059 case MODOP_EXPR:
13060 case PSEUDO_DTOR_EXPR:
13061 case VEC_PERM_EXPR:
13063 r = build_nt
13064 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13065 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
13066 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
13067 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13068 return r;
13071 case NEW_EXPR:
13073 r = build_nt
13074 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13075 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
13076 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
13077 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13078 return r;
13081 case DELETE_EXPR:
13083 r = build_nt
13084 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13085 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13086 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13087 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13088 return r;
13091 case TEMPLATE_ID_EXPR:
13093 /* Substituted template arguments */
13094 tree fn = TREE_OPERAND (t, 0);
13095 tree targs = TREE_OPERAND (t, 1);
13097 fn = tsubst_copy (fn, args, complain, in_decl);
13098 if (targs)
13099 targs = tsubst_template_args (targs, args, complain, in_decl);
13101 return lookup_template_function (fn, targs);
13104 case TREE_LIST:
13106 tree purpose, value, chain;
13108 if (t == void_list_node)
13109 return t;
13111 purpose = TREE_PURPOSE (t);
13112 if (purpose)
13113 purpose = tsubst_copy (purpose, args, complain, in_decl);
13114 value = TREE_VALUE (t);
13115 if (value)
13116 value = tsubst_copy (value, args, complain, in_decl);
13117 chain = TREE_CHAIN (t);
13118 if (chain && chain != void_type_node)
13119 chain = tsubst_copy (chain, args, complain, in_decl);
13120 if (purpose == TREE_PURPOSE (t)
13121 && value == TREE_VALUE (t)
13122 && chain == TREE_CHAIN (t))
13123 return t;
13124 return tree_cons (purpose, value, chain);
13127 case RECORD_TYPE:
13128 case UNION_TYPE:
13129 case ENUMERAL_TYPE:
13130 case INTEGER_TYPE:
13131 case REAL_TYPE:
13132 case BOOLEAN_TYPE:
13133 case TEMPLATE_TYPE_PARM:
13134 case TEMPLATE_TEMPLATE_PARM:
13135 case BOUND_TEMPLATE_TEMPLATE_PARM:
13136 case TEMPLATE_PARM_INDEX:
13137 case POINTER_TYPE:
13138 case REFERENCE_TYPE:
13139 case OFFSET_TYPE:
13140 case FUNCTION_TYPE:
13141 case METHOD_TYPE:
13142 case ARRAY_TYPE:
13143 case TYPENAME_TYPE:
13144 case UNBOUND_CLASS_TEMPLATE:
13145 case TYPEOF_TYPE:
13146 case DECLTYPE_TYPE:
13147 case TYPE_DECL:
13148 return tsubst (t, args, complain, in_decl);
13150 case USING_DECL:
13151 t = DECL_NAME (t);
13152 /* Fall through. */
13153 case IDENTIFIER_NODE:
13154 if (IDENTIFIER_TYPENAME_P (t))
13156 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13157 return mangle_conv_op_name_for_type (new_type);
13159 else
13160 return t;
13162 case CONSTRUCTOR:
13163 /* This is handled by tsubst_copy_and_build. */
13164 gcc_unreachable ();
13166 case VA_ARG_EXPR:
13167 return build_x_va_arg (EXPR_LOCATION (t),
13168 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
13169 in_decl),
13170 tsubst (TREE_TYPE (t), args, complain, in_decl));
13172 case CLEANUP_POINT_EXPR:
13173 /* We shouldn't have built any of these during initial template
13174 generation. Instead, they should be built during instantiation
13175 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13176 gcc_unreachable ();
13178 case OFFSET_REF:
13179 r = build2
13180 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
13181 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13182 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13183 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13184 mark_used (TREE_OPERAND (r, 1));
13185 return r;
13187 case EXPR_PACK_EXPANSION:
13188 // Expansions of variadic constraints are viable, expanding
13189 // to a conunction of the expanded terms.
13190 if (TREE_TYPE (t) == boolean_type_node)
13191 return tsubst_pack_conjunction (t, args, complain, in_decl);
13193 error ("invalid use of pack expansion expression");
13194 return error_mark_node;
13196 case NONTYPE_ARGUMENT_PACK:
13197 error ("use %<...%> to expand argument pack");
13198 return error_mark_node;
13200 case INTEGER_CST:
13201 case REAL_CST:
13202 case STRING_CST:
13203 case COMPLEX_CST:
13205 /* Instantiate any typedefs in the type. */
13206 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13207 r = fold_convert (type, t);
13208 gcc_assert (TREE_CODE (r) == code);
13209 return r;
13212 case PTRMEM_CST:
13213 /* These can sometimes show up in a partial instantiation, but never
13214 involve template parms. */
13215 gcc_assert (!uses_template_parms (t));
13216 return t;
13218 default:
13219 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13220 gcc_checking_assert (false);
13221 return t;
13225 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13227 static tree
13228 tsubst_omp_clauses (tree clauses, bool declare_simd,
13229 tree args, tsubst_flags_t complain, tree in_decl)
13231 tree new_clauses = NULL, nc, oc;
13233 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13235 nc = copy_node (oc);
13236 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13237 new_clauses = nc;
13239 switch (OMP_CLAUSE_CODE (nc))
13241 case OMP_CLAUSE_LASTPRIVATE:
13242 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13244 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13245 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13246 in_decl, /*integral_constant_expression_p=*/false);
13247 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13248 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13250 /* FALLTHRU */
13251 case OMP_CLAUSE_PRIVATE:
13252 case OMP_CLAUSE_SHARED:
13253 case OMP_CLAUSE_FIRSTPRIVATE:
13254 case OMP_CLAUSE_COPYIN:
13255 case OMP_CLAUSE_COPYPRIVATE:
13256 case OMP_CLAUSE_IF:
13257 case OMP_CLAUSE_NUM_THREADS:
13258 case OMP_CLAUSE_SCHEDULE:
13259 case OMP_CLAUSE_COLLAPSE:
13260 case OMP_CLAUSE_FINAL:
13261 case OMP_CLAUSE_DEPEND:
13262 case OMP_CLAUSE_FROM:
13263 case OMP_CLAUSE_TO:
13264 case OMP_CLAUSE_UNIFORM:
13265 case OMP_CLAUSE_MAP:
13266 case OMP_CLAUSE_DEVICE:
13267 case OMP_CLAUSE_DIST_SCHEDULE:
13268 case OMP_CLAUSE_NUM_TEAMS:
13269 case OMP_CLAUSE_THREAD_LIMIT:
13270 case OMP_CLAUSE_SAFELEN:
13271 case OMP_CLAUSE_SIMDLEN:
13272 OMP_CLAUSE_OPERAND (nc, 0)
13273 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13274 in_decl, /*integral_constant_expression_p=*/false);
13275 break;
13276 case OMP_CLAUSE_REDUCTION:
13277 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13279 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13280 if (TREE_CODE (placeholder) == SCOPE_REF)
13282 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13283 complain, in_decl);
13284 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13285 = build_qualified_name (NULL_TREE, scope,
13286 TREE_OPERAND (placeholder, 1),
13287 false);
13289 else
13290 gcc_assert (identifier_p (placeholder));
13292 OMP_CLAUSE_OPERAND (nc, 0)
13293 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13294 in_decl, /*integral_constant_expression_p=*/false);
13295 break;
13296 case OMP_CLAUSE_LINEAR:
13297 case OMP_CLAUSE_ALIGNED:
13298 OMP_CLAUSE_OPERAND (nc, 0)
13299 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13300 in_decl, /*integral_constant_expression_p=*/false);
13301 OMP_CLAUSE_OPERAND (nc, 1)
13302 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13303 in_decl, /*integral_constant_expression_p=*/false);
13304 break;
13306 case OMP_CLAUSE_NOWAIT:
13307 case OMP_CLAUSE_ORDERED:
13308 case OMP_CLAUSE_DEFAULT:
13309 case OMP_CLAUSE_UNTIED:
13310 case OMP_CLAUSE_MERGEABLE:
13311 case OMP_CLAUSE_INBRANCH:
13312 case OMP_CLAUSE_NOTINBRANCH:
13313 case OMP_CLAUSE_PROC_BIND:
13314 case OMP_CLAUSE_FOR:
13315 case OMP_CLAUSE_PARALLEL:
13316 case OMP_CLAUSE_SECTIONS:
13317 case OMP_CLAUSE_TASKGROUP:
13318 break;
13319 default:
13320 gcc_unreachable ();
13324 new_clauses = nreverse (new_clauses);
13325 if (!declare_simd)
13326 new_clauses = finish_omp_clauses (new_clauses);
13327 return new_clauses;
13330 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13332 static tree
13333 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13334 tree in_decl)
13336 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13338 tree purpose, value, chain;
13340 if (t == NULL)
13341 return t;
13343 if (TREE_CODE (t) != TREE_LIST)
13344 return tsubst_copy_and_build (t, args, complain, in_decl,
13345 /*function_p=*/false,
13346 /*integral_constant_expression_p=*/false);
13348 if (t == void_list_node)
13349 return t;
13351 purpose = TREE_PURPOSE (t);
13352 if (purpose)
13353 purpose = RECUR (purpose);
13354 value = TREE_VALUE (t);
13355 if (value)
13357 if (TREE_CODE (value) != LABEL_DECL)
13358 value = RECUR (value);
13359 else
13361 value = lookup_label (DECL_NAME (value));
13362 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13363 TREE_USED (value) = 1;
13366 chain = TREE_CHAIN (t);
13367 if (chain && chain != void_type_node)
13368 chain = RECUR (chain);
13369 return tree_cons (purpose, value, chain);
13370 #undef RECUR
13373 /* Substitute one OMP_FOR iterator. */
13375 static void
13376 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13377 tree condv, tree incrv, tree *clauses,
13378 tree args, tsubst_flags_t complain, tree in_decl,
13379 bool integral_constant_expression_p)
13381 #define RECUR(NODE) \
13382 tsubst_expr ((NODE), args, complain, in_decl, \
13383 integral_constant_expression_p)
13384 tree decl, init, cond, incr;
13385 bool init_decl;
13387 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13388 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13389 decl = TREE_OPERAND (init, 0);
13390 init = TREE_OPERAND (init, 1);
13391 /* Do this before substituting into decl to handle 'auto'. */
13392 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13393 init = RECUR (init);
13394 decl = RECUR (decl);
13395 if (init_decl)
13397 gcc_assert (!processing_template_decl);
13398 init = DECL_INITIAL (decl);
13399 DECL_INITIAL (decl) = NULL_TREE;
13402 gcc_assert (!type_dependent_expression_p (decl));
13404 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13406 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13407 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13408 if (TREE_CODE (incr) == MODIFY_EXPR)
13409 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13410 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13411 RECUR (TREE_OPERAND (incr, 1)),
13412 complain);
13413 else
13414 incr = RECUR (incr);
13415 TREE_VEC_ELT (declv, i) = decl;
13416 TREE_VEC_ELT (initv, i) = init;
13417 TREE_VEC_ELT (condv, i) = cond;
13418 TREE_VEC_ELT (incrv, i) = incr;
13419 return;
13422 if (init && !init_decl)
13424 tree c;
13425 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13427 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13428 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13429 && OMP_CLAUSE_DECL (c) == decl)
13430 break;
13431 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13432 && OMP_CLAUSE_DECL (c) == decl)
13433 error ("iteration variable %qD should not be firstprivate", decl);
13434 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13435 && OMP_CLAUSE_DECL (c) == decl)
13436 error ("iteration variable %qD should not be reduction", decl);
13438 if (c == NULL)
13440 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13441 OMP_CLAUSE_DECL (c) = decl;
13442 c = finish_omp_clauses (c);
13443 if (c)
13445 OMP_CLAUSE_CHAIN (c) = *clauses;
13446 *clauses = c;
13450 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13451 if (COMPARISON_CLASS_P (cond))
13452 cond = build2 (TREE_CODE (cond), boolean_type_node,
13453 RECUR (TREE_OPERAND (cond, 0)),
13454 RECUR (TREE_OPERAND (cond, 1)));
13455 else
13456 cond = RECUR (cond);
13457 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13458 switch (TREE_CODE (incr))
13460 case PREINCREMENT_EXPR:
13461 case PREDECREMENT_EXPR:
13462 case POSTINCREMENT_EXPR:
13463 case POSTDECREMENT_EXPR:
13464 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13465 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13466 break;
13467 case MODIFY_EXPR:
13468 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13469 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13471 tree rhs = TREE_OPERAND (incr, 1);
13472 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13473 RECUR (TREE_OPERAND (incr, 0)),
13474 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13475 RECUR (TREE_OPERAND (rhs, 0)),
13476 RECUR (TREE_OPERAND (rhs, 1))));
13478 else
13479 incr = RECUR (incr);
13480 break;
13481 case MODOP_EXPR:
13482 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13483 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13485 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13486 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13487 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13488 TREE_TYPE (decl), lhs,
13489 RECUR (TREE_OPERAND (incr, 2))));
13491 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13492 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13493 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13495 tree rhs = TREE_OPERAND (incr, 2);
13496 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13497 RECUR (TREE_OPERAND (incr, 0)),
13498 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13499 RECUR (TREE_OPERAND (rhs, 0)),
13500 RECUR (TREE_OPERAND (rhs, 1))));
13502 else
13503 incr = RECUR (incr);
13504 break;
13505 default:
13506 incr = RECUR (incr);
13507 break;
13510 TREE_VEC_ELT (declv, i) = decl;
13511 TREE_VEC_ELT (initv, i) = init;
13512 TREE_VEC_ELT (condv, i) = cond;
13513 TREE_VEC_ELT (incrv, i) = incr;
13514 #undef RECUR
13517 /* Like tsubst_copy for expressions, etc. but also does semantic
13518 processing. */
13520 tree
13521 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13522 bool integral_constant_expression_p)
13524 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13525 #define RECUR(NODE) \
13526 tsubst_expr ((NODE), args, complain, in_decl, \
13527 integral_constant_expression_p)
13529 tree stmt, tmp;
13530 tree r;
13531 location_t loc;
13533 if (t == NULL_TREE || t == error_mark_node)
13534 return t;
13536 loc = input_location;
13537 if (EXPR_HAS_LOCATION (t))
13538 input_location = EXPR_LOCATION (t);
13539 if (STATEMENT_CODE_P (TREE_CODE (t)))
13540 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13542 switch (TREE_CODE (t))
13544 case STATEMENT_LIST:
13546 tree_stmt_iterator i;
13547 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13548 RECUR (tsi_stmt (i));
13549 break;
13552 case CTOR_INITIALIZER:
13553 finish_mem_initializers (tsubst_initializer_list
13554 (TREE_OPERAND (t, 0), args));
13555 break;
13557 case RETURN_EXPR:
13558 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13559 break;
13561 case EXPR_STMT:
13562 tmp = RECUR (EXPR_STMT_EXPR (t));
13563 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13564 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13565 else
13566 finish_expr_stmt (tmp);
13567 break;
13569 case USING_STMT:
13570 do_using_directive (USING_STMT_NAMESPACE (t));
13571 break;
13573 case DECL_EXPR:
13575 tree decl, pattern_decl;
13576 tree init;
13578 pattern_decl = decl = DECL_EXPR_DECL (t);
13579 if (TREE_CODE (decl) == LABEL_DECL)
13580 finish_label_decl (DECL_NAME (decl));
13581 else if (TREE_CODE (decl) == USING_DECL)
13583 tree scope = USING_DECL_SCOPE (decl);
13584 tree name = DECL_NAME (decl);
13585 tree decl;
13587 scope = tsubst (scope, args, complain, in_decl);
13588 decl = lookup_qualified_name (scope, name,
13589 /*is_type_p=*/false,
13590 /*complain=*/false);
13591 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13592 qualified_name_lookup_error (scope, name, decl, input_location);
13593 else
13594 do_local_using_decl (decl, scope, name);
13596 else if (DECL_PACK_P (decl))
13598 /* Don't build up decls for a variadic capture proxy, we'll
13599 instantiate the elements directly as needed. */
13600 break;
13602 else
13604 init = DECL_INITIAL (decl);
13605 decl = tsubst (decl, args, complain, in_decl);
13606 if (decl != error_mark_node)
13608 /* By marking the declaration as instantiated, we avoid
13609 trying to instantiate it. Since instantiate_decl can't
13610 handle local variables, and since we've already done
13611 all that needs to be done, that's the right thing to
13612 do. */
13613 if (VAR_P (decl))
13614 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13615 if (VAR_P (decl)
13616 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13617 /* Anonymous aggregates are a special case. */
13618 finish_anon_union (decl);
13619 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13621 DECL_CONTEXT (decl) = current_function_decl;
13622 if (DECL_NAME (decl) == this_identifier)
13624 tree lam = DECL_CONTEXT (current_function_decl);
13625 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13626 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13628 insert_capture_proxy (decl);
13630 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13631 /* We already did a pushtag. */;
13632 else if (TREE_CODE (decl) == FUNCTION_DECL
13633 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13634 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13636 DECL_CONTEXT (decl) = NULL_TREE;
13637 pushdecl (decl);
13638 DECL_CONTEXT (decl) = current_function_decl;
13639 cp_check_omp_declare_reduction (decl);
13641 else
13643 int const_init = false;
13644 maybe_push_decl (decl);
13645 if (VAR_P (decl)
13646 && DECL_PRETTY_FUNCTION_P (decl))
13648 /* For __PRETTY_FUNCTION__ we have to adjust the
13649 initializer. */
13650 const char *const name
13651 = cxx_printable_name (current_function_decl, 2);
13652 init = cp_fname_init (name, &TREE_TYPE (decl));
13654 else
13656 tree t = RECUR (init);
13658 if (init && !t)
13660 /* If we had an initializer but it
13661 instantiated to nothing,
13662 value-initialize the object. This will
13663 only occur when the initializer was a
13664 pack expansion where the parameter packs
13665 used in that expansion were of length
13666 zero. */
13667 init = build_value_init (TREE_TYPE (decl),
13668 complain);
13669 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13670 init = get_target_expr_sfinae (init, complain);
13672 else
13673 init = t;
13676 if (VAR_P (decl))
13677 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13678 (pattern_decl));
13679 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13684 break;
13687 case FOR_STMT:
13688 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13689 RECUR (FOR_INIT_STMT (t));
13690 finish_for_init_stmt (stmt);
13691 tmp = RECUR (FOR_COND (t));
13692 finish_for_cond (tmp, stmt, false);
13693 tmp = RECUR (FOR_EXPR (t));
13694 finish_for_expr (tmp, stmt);
13695 RECUR (FOR_BODY (t));
13696 finish_for_stmt (stmt);
13697 break;
13699 case RANGE_FOR_STMT:
13701 tree decl, expr;
13702 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13703 decl = RANGE_FOR_DECL (t);
13704 decl = tsubst (decl, args, complain, in_decl);
13705 maybe_push_decl (decl);
13706 expr = RECUR (RANGE_FOR_EXPR (t));
13707 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13708 RECUR (RANGE_FOR_BODY (t));
13709 finish_for_stmt (stmt);
13711 break;
13713 case WHILE_STMT:
13714 stmt = begin_while_stmt ();
13715 tmp = RECUR (WHILE_COND (t));
13716 finish_while_stmt_cond (tmp, stmt, false);
13717 RECUR (WHILE_BODY (t));
13718 finish_while_stmt (stmt);
13719 break;
13721 case DO_STMT:
13722 stmt = begin_do_stmt ();
13723 RECUR (DO_BODY (t));
13724 finish_do_body (stmt);
13725 tmp = RECUR (DO_COND (t));
13726 finish_do_stmt (tmp, stmt, false);
13727 break;
13729 case IF_STMT:
13730 stmt = begin_if_stmt ();
13731 tmp = RECUR (IF_COND (t));
13732 finish_if_stmt_cond (tmp, stmt);
13733 RECUR (THEN_CLAUSE (t));
13734 finish_then_clause (stmt);
13736 if (ELSE_CLAUSE (t))
13738 begin_else_clause (stmt);
13739 RECUR (ELSE_CLAUSE (t));
13740 finish_else_clause (stmt);
13743 finish_if_stmt (stmt);
13744 break;
13746 case BIND_EXPR:
13747 if (BIND_EXPR_BODY_BLOCK (t))
13748 stmt = begin_function_body ();
13749 else
13750 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13751 ? BCS_TRY_BLOCK : 0);
13753 RECUR (BIND_EXPR_BODY (t));
13755 if (BIND_EXPR_BODY_BLOCK (t))
13756 finish_function_body (stmt);
13757 else
13758 finish_compound_stmt (stmt);
13759 break;
13761 case BREAK_STMT:
13762 finish_break_stmt ();
13763 break;
13765 case CONTINUE_STMT:
13766 finish_continue_stmt ();
13767 break;
13769 case SWITCH_STMT:
13770 stmt = begin_switch_stmt ();
13771 tmp = RECUR (SWITCH_STMT_COND (t));
13772 finish_switch_cond (tmp, stmt);
13773 RECUR (SWITCH_STMT_BODY (t));
13774 finish_switch_stmt (stmt);
13775 break;
13777 case CASE_LABEL_EXPR:
13778 finish_case_label (EXPR_LOCATION (t),
13779 RECUR (CASE_LOW (t)),
13780 RECUR (CASE_HIGH (t)));
13781 break;
13783 case LABEL_EXPR:
13785 tree decl = LABEL_EXPR_LABEL (t);
13786 tree label;
13788 label = finish_label_stmt (DECL_NAME (decl));
13789 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13790 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13792 break;
13794 case GOTO_EXPR:
13795 tmp = GOTO_DESTINATION (t);
13796 if (TREE_CODE (tmp) != LABEL_DECL)
13797 /* Computed goto's must be tsubst'd into. On the other hand,
13798 non-computed gotos must not be; the identifier in question
13799 will have no binding. */
13800 tmp = RECUR (tmp);
13801 else
13802 tmp = DECL_NAME (tmp);
13803 finish_goto_stmt (tmp);
13804 break;
13806 case ASM_EXPR:
13807 tmp = finish_asm_stmt
13808 (ASM_VOLATILE_P (t),
13809 RECUR (ASM_STRING (t)),
13810 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13811 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13812 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13813 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13815 tree asm_expr = tmp;
13816 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13817 asm_expr = TREE_OPERAND (asm_expr, 0);
13818 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13820 break;
13822 case TRY_BLOCK:
13823 if (CLEANUP_P (t))
13825 stmt = begin_try_block ();
13826 RECUR (TRY_STMTS (t));
13827 finish_cleanup_try_block (stmt);
13828 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13830 else
13832 tree compound_stmt = NULL_TREE;
13834 if (FN_TRY_BLOCK_P (t))
13835 stmt = begin_function_try_block (&compound_stmt);
13836 else
13837 stmt = begin_try_block ();
13839 RECUR (TRY_STMTS (t));
13841 if (FN_TRY_BLOCK_P (t))
13842 finish_function_try_block (stmt);
13843 else
13844 finish_try_block (stmt);
13846 RECUR (TRY_HANDLERS (t));
13847 if (FN_TRY_BLOCK_P (t))
13848 finish_function_handler_sequence (stmt, compound_stmt);
13849 else
13850 finish_handler_sequence (stmt);
13852 break;
13854 case HANDLER:
13856 tree decl = HANDLER_PARMS (t);
13858 if (decl)
13860 decl = tsubst (decl, args, complain, in_decl);
13861 /* Prevent instantiate_decl from trying to instantiate
13862 this variable. We've already done all that needs to be
13863 done. */
13864 if (decl != error_mark_node)
13865 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13867 stmt = begin_handler ();
13868 finish_handler_parms (decl, stmt);
13869 RECUR (HANDLER_BODY (t));
13870 finish_handler (stmt);
13872 break;
13874 case TAG_DEFN:
13875 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13876 if (CLASS_TYPE_P (tmp))
13878 /* Local classes are not independent templates; they are
13879 instantiated along with their containing function. And this
13880 way we don't have to deal with pushing out of one local class
13881 to instantiate a member of another local class. */
13882 tree fn;
13883 /* Closures are handled by the LAMBDA_EXPR. */
13884 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13885 complete_type (tmp);
13886 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13887 if (!DECL_ARTIFICIAL (fn))
13888 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13890 break;
13892 case STATIC_ASSERT:
13894 tree condition;
13896 ++c_inhibit_evaluation_warnings;
13897 condition =
13898 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13899 args,
13900 complain, in_decl,
13901 /*integral_constant_expression_p=*/true);
13902 --c_inhibit_evaluation_warnings;
13904 finish_static_assert (condition,
13905 STATIC_ASSERT_MESSAGE (t),
13906 STATIC_ASSERT_SOURCE_LOCATION (t),
13907 /*member_p=*/false);
13909 break;
13911 case OMP_PARALLEL:
13912 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13913 args, complain, in_decl);
13914 stmt = begin_omp_parallel ();
13915 RECUR (OMP_PARALLEL_BODY (t));
13916 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13917 = OMP_PARALLEL_COMBINED (t);
13918 break;
13920 case OMP_TASK:
13921 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13922 args, complain, in_decl);
13923 stmt = begin_omp_task ();
13924 RECUR (OMP_TASK_BODY (t));
13925 finish_omp_task (tmp, stmt);
13926 break;
13928 case OMP_FOR:
13929 case OMP_SIMD:
13930 case CILK_SIMD:
13931 case OMP_DISTRIBUTE:
13933 tree clauses, body, pre_body;
13934 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13935 tree incrv = NULL_TREE;
13936 int i;
13938 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13939 args, complain, in_decl);
13940 if (OMP_FOR_INIT (t) != NULL_TREE)
13942 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13943 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13944 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13945 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13948 stmt = begin_omp_structured_block ();
13950 pre_body = push_stmt_list ();
13951 RECUR (OMP_FOR_PRE_BODY (t));
13952 pre_body = pop_stmt_list (pre_body);
13954 if (OMP_FOR_INIT (t) != NULL_TREE)
13955 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13956 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13957 &clauses, args, complain, in_decl,
13958 integral_constant_expression_p);
13960 body = push_stmt_list ();
13961 RECUR (OMP_FOR_BODY (t));
13962 body = pop_stmt_list (body);
13964 if (OMP_FOR_INIT (t) != NULL_TREE)
13965 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13966 condv, incrv, body, pre_body, clauses);
13967 else
13969 t = make_node (TREE_CODE (t));
13970 TREE_TYPE (t) = void_type_node;
13971 OMP_FOR_BODY (t) = body;
13972 OMP_FOR_PRE_BODY (t) = pre_body;
13973 OMP_FOR_CLAUSES (t) = clauses;
13974 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13975 add_stmt (t);
13978 add_stmt (finish_omp_structured_block (stmt));
13980 break;
13982 case OMP_SECTIONS:
13983 case OMP_SINGLE:
13984 case OMP_TEAMS:
13985 case OMP_TARGET_DATA:
13986 case OMP_TARGET:
13987 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13988 args, complain, in_decl);
13989 stmt = push_stmt_list ();
13990 RECUR (OMP_BODY (t));
13991 stmt = pop_stmt_list (stmt);
13993 t = copy_node (t);
13994 OMP_BODY (t) = stmt;
13995 OMP_CLAUSES (t) = tmp;
13996 add_stmt (t);
13997 break;
13999 case OMP_TARGET_UPDATE:
14000 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14001 args, complain, in_decl);
14002 t = copy_node (t);
14003 OMP_CLAUSES (t) = tmp;
14004 add_stmt (t);
14005 break;
14007 case OMP_SECTION:
14008 case OMP_CRITICAL:
14009 case OMP_MASTER:
14010 case OMP_TASKGROUP:
14011 case OMP_ORDERED:
14012 stmt = push_stmt_list ();
14013 RECUR (OMP_BODY (t));
14014 stmt = pop_stmt_list (stmt);
14016 t = copy_node (t);
14017 OMP_BODY (t) = stmt;
14018 add_stmt (t);
14019 break;
14021 case OMP_ATOMIC:
14022 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14023 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14025 tree op1 = TREE_OPERAND (t, 1);
14026 tree rhs1 = NULL_TREE;
14027 tree lhs, rhs;
14028 if (TREE_CODE (op1) == COMPOUND_EXPR)
14030 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14031 op1 = TREE_OPERAND (op1, 1);
14033 lhs = RECUR (TREE_OPERAND (op1, 0));
14034 rhs = RECUR (TREE_OPERAND (op1, 1));
14035 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14036 NULL_TREE, NULL_TREE, rhs1,
14037 OMP_ATOMIC_SEQ_CST (t));
14039 else
14041 tree op1 = TREE_OPERAND (t, 1);
14042 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14043 tree rhs1 = NULL_TREE;
14044 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14045 enum tree_code opcode = NOP_EXPR;
14046 if (code == OMP_ATOMIC_READ)
14048 v = RECUR (TREE_OPERAND (op1, 0));
14049 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14051 else if (code == OMP_ATOMIC_CAPTURE_OLD
14052 || code == OMP_ATOMIC_CAPTURE_NEW)
14054 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14055 v = RECUR (TREE_OPERAND (op1, 0));
14056 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14057 if (TREE_CODE (op11) == COMPOUND_EXPR)
14059 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14060 op11 = TREE_OPERAND (op11, 1);
14062 lhs = RECUR (TREE_OPERAND (op11, 0));
14063 rhs = RECUR (TREE_OPERAND (op11, 1));
14064 opcode = TREE_CODE (op11);
14065 if (opcode == MODIFY_EXPR)
14066 opcode = NOP_EXPR;
14068 else
14070 code = OMP_ATOMIC;
14071 lhs = RECUR (TREE_OPERAND (op1, 0));
14072 rhs = RECUR (TREE_OPERAND (op1, 1));
14074 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14075 OMP_ATOMIC_SEQ_CST (t));
14077 break;
14079 case TRANSACTION_EXPR:
14081 int flags = 0;
14082 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14083 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14085 if (TRANSACTION_EXPR_IS_STMT (t))
14087 tree body = TRANSACTION_EXPR_BODY (t);
14088 tree noex = NULL_TREE;
14089 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14091 noex = MUST_NOT_THROW_COND (body);
14092 if (noex == NULL_TREE)
14093 noex = boolean_true_node;
14094 body = TREE_OPERAND (body, 0);
14096 stmt = begin_transaction_stmt (input_location, NULL, flags);
14097 RECUR (body);
14098 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14100 else
14102 stmt = build_transaction_expr (EXPR_LOCATION (t),
14103 RECUR (TRANSACTION_EXPR_BODY (t)),
14104 flags, NULL_TREE);
14105 RETURN (stmt);
14108 break;
14110 case MUST_NOT_THROW_EXPR:
14111 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
14112 RECUR (MUST_NOT_THROW_COND (t))));
14114 case EXPR_PACK_EXPANSION:
14115 // Expansions of variadic constraints are viable, expanding
14116 // to a conunction of the expanded terms.
14117 if (TREE_TYPE (t) == boolean_type_node)
14118 return tsubst_pack_conjunction (t, args, complain, in_decl);
14120 error ("invalid use of pack expansion expression");
14121 RETURN (error_mark_node);
14123 case NONTYPE_ARGUMENT_PACK:
14124 error ("use %<...%> to expand argument pack");
14125 RETURN (error_mark_node);
14127 case COMPOUND_EXPR:
14128 tmp = RECUR (TREE_OPERAND (t, 0));
14129 if (tmp == NULL_TREE)
14130 /* If the first operand was a statement, we're done with it. */
14131 RETURN (RECUR (TREE_OPERAND (t, 1)));
14132 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14133 RECUR (TREE_OPERAND (t, 1)),
14134 complain));
14136 default:
14137 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14139 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14140 /*function_p=*/false,
14141 integral_constant_expression_p));
14144 RETURN (NULL_TREE);
14145 out:
14146 input_location = loc;
14147 return r;
14148 #undef RECUR
14149 #undef RETURN
14152 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14153 function. For description of the body see comment above
14154 cp_parser_omp_declare_reduction_exprs. */
14156 static void
14157 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14159 if (t == NULL_TREE || t == error_mark_node)
14160 return;
14162 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14164 tree_stmt_iterator tsi;
14165 int i;
14166 tree stmts[7];
14167 memset (stmts, 0, sizeof stmts);
14168 for (i = 0, tsi = tsi_start (t);
14169 i < 7 && !tsi_end_p (tsi);
14170 i++, tsi_next (&tsi))
14171 stmts[i] = tsi_stmt (tsi);
14172 gcc_assert (tsi_end_p (tsi));
14174 if (i >= 3)
14176 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14177 && TREE_CODE (stmts[1]) == DECL_EXPR);
14178 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14179 args, complain, in_decl);
14180 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14181 args, complain, in_decl);
14182 DECL_CONTEXT (omp_out) = current_function_decl;
14183 DECL_CONTEXT (omp_in) = current_function_decl;
14184 keep_next_level (true);
14185 tree block = begin_omp_structured_block ();
14186 tsubst_expr (stmts[2], args, complain, in_decl, false);
14187 block = finish_omp_structured_block (block);
14188 block = maybe_cleanup_point_expr_void (block);
14189 add_decl_expr (omp_out);
14190 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14191 TREE_NO_WARNING (omp_out) = 1;
14192 add_decl_expr (omp_in);
14193 finish_expr_stmt (block);
14195 if (i >= 6)
14197 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14198 && TREE_CODE (stmts[4]) == DECL_EXPR);
14199 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14200 args, complain, in_decl);
14201 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14202 args, complain, in_decl);
14203 DECL_CONTEXT (omp_priv) = current_function_decl;
14204 DECL_CONTEXT (omp_orig) = current_function_decl;
14205 keep_next_level (true);
14206 tree block = begin_omp_structured_block ();
14207 tsubst_expr (stmts[5], args, complain, in_decl, false);
14208 block = finish_omp_structured_block (block);
14209 block = maybe_cleanup_point_expr_void (block);
14210 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14211 add_decl_expr (omp_priv);
14212 add_decl_expr (omp_orig);
14213 finish_expr_stmt (block);
14214 if (i == 7)
14215 add_decl_expr (omp_orig);
14219 /* T is a postfix-expression that is not being used in a function
14220 call. Return the substituted version of T. */
14222 static tree
14223 tsubst_non_call_postfix_expression (tree t, tree args,
14224 tsubst_flags_t complain,
14225 tree in_decl)
14227 if (TREE_CODE (t) == SCOPE_REF)
14228 t = tsubst_qualified_id (t, args, complain, in_decl,
14229 /*done=*/false, /*address_p=*/false);
14230 else
14231 t = tsubst_copy_and_build (t, args, complain, in_decl,
14232 /*function_p=*/false,
14233 /*integral_constant_expression_p=*/false);
14235 return t;
14238 /* Like tsubst but deals with expressions and performs semantic
14239 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14241 tree
14242 tsubst_copy_and_build (tree t,
14243 tree args,
14244 tsubst_flags_t complain,
14245 tree in_decl,
14246 bool function_p,
14247 bool integral_constant_expression_p)
14249 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14250 #define RECUR(NODE) \
14251 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14252 /*function_p=*/false, \
14253 integral_constant_expression_p)
14255 tree retval, op1;
14256 location_t loc;
14258 if (t == NULL_TREE || t == error_mark_node)
14259 return t;
14261 loc = input_location;
14262 if (EXPR_HAS_LOCATION (t))
14263 input_location = EXPR_LOCATION (t);
14265 /* N3276 decltype magic only applies to calls at the top level or on the
14266 right side of a comma. */
14267 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14268 complain &= ~tf_decltype;
14270 switch (TREE_CODE (t))
14272 case USING_DECL:
14273 t = DECL_NAME (t);
14274 /* Fall through. */
14275 case IDENTIFIER_NODE:
14277 tree decl;
14278 cp_id_kind idk;
14279 bool non_integral_constant_expression_p;
14280 const char *error_msg;
14282 if (IDENTIFIER_TYPENAME_P (t))
14284 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14285 t = mangle_conv_op_name_for_type (new_type);
14288 /* Look up the name. */
14289 decl = lookup_name (t);
14291 /* By convention, expressions use ERROR_MARK_NODE to indicate
14292 failure, not NULL_TREE. */
14293 if (decl == NULL_TREE)
14294 decl = error_mark_node;
14296 decl = finish_id_expression (t, decl, NULL_TREE,
14297 &idk,
14298 integral_constant_expression_p,
14299 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14300 &non_integral_constant_expression_p,
14301 /*template_p=*/false,
14302 /*done=*/true,
14303 /*address_p=*/false,
14304 /*template_arg_p=*/false,
14305 &error_msg,
14306 input_location);
14307 if (error_msg)
14308 error (error_msg);
14309 if (!function_p && identifier_p (decl))
14311 if (complain & tf_error)
14312 unqualified_name_lookup_error (decl);
14313 decl = error_mark_node;
14315 RETURN (decl);
14318 case TEMPLATE_ID_EXPR:
14320 tree object;
14321 tree templ = RECUR (TREE_OPERAND (t, 0));
14322 tree targs = TREE_OPERAND (t, 1);
14324 if (targs)
14325 targs = tsubst_template_args (targs, args, complain, in_decl);
14327 if (TREE_CODE (templ) == COMPONENT_REF)
14329 object = TREE_OPERAND (templ, 0);
14330 templ = TREE_OPERAND (templ, 1);
14332 else
14333 object = NULL_TREE;
14334 templ = lookup_template_function (templ, targs);
14336 if (object)
14337 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14338 object, templ, NULL_TREE));
14339 else
14340 RETURN (baselink_for_fns (templ));
14343 case INDIRECT_REF:
14345 tree r = RECUR (TREE_OPERAND (t, 0));
14347 if (REFERENCE_REF_P (t))
14349 /* A type conversion to reference type will be enclosed in
14350 such an indirect ref, but the substitution of the cast
14351 will have also added such an indirect ref. */
14352 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14353 r = convert_from_reference (r);
14355 else
14356 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14357 complain|decltype_flag);
14358 RETURN (r);
14361 case NOP_EXPR:
14362 RETURN (build_nop
14363 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14364 RECUR (TREE_OPERAND (t, 0))));
14366 case IMPLICIT_CONV_EXPR:
14368 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14369 tree expr = RECUR (TREE_OPERAND (t, 0));
14370 int flags = LOOKUP_IMPLICIT;
14371 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14372 flags = LOOKUP_NORMAL;
14373 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14374 flags));
14377 case CONVERT_EXPR:
14378 RETURN (build1
14379 (CONVERT_EXPR,
14380 tsubst (TREE_TYPE (t), args, complain, in_decl),
14381 RECUR (TREE_OPERAND (t, 0))));
14383 case CAST_EXPR:
14384 case REINTERPRET_CAST_EXPR:
14385 case CONST_CAST_EXPR:
14386 case DYNAMIC_CAST_EXPR:
14387 case STATIC_CAST_EXPR:
14389 tree type;
14390 tree op, r = NULL_TREE;
14392 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14393 if (integral_constant_expression_p
14394 && !cast_valid_in_integral_constant_expression_p (type))
14396 if (complain & tf_error)
14397 error ("a cast to a type other than an integral or "
14398 "enumeration type cannot appear in a constant-expression");
14399 RETURN (error_mark_node);
14402 op = RECUR (TREE_OPERAND (t, 0));
14404 ++c_inhibit_evaluation_warnings;
14405 switch (TREE_CODE (t))
14407 case CAST_EXPR:
14408 r = build_functional_cast (type, op, complain);
14409 break;
14410 case REINTERPRET_CAST_EXPR:
14411 r = build_reinterpret_cast (type, op, complain);
14412 break;
14413 case CONST_CAST_EXPR:
14414 r = build_const_cast (type, op, complain);
14415 break;
14416 case DYNAMIC_CAST_EXPR:
14417 r = build_dynamic_cast (type, op, complain);
14418 break;
14419 case STATIC_CAST_EXPR:
14420 r = build_static_cast (type, op, complain);
14421 break;
14422 default:
14423 gcc_unreachable ();
14425 --c_inhibit_evaluation_warnings;
14427 RETURN (r);
14430 case POSTDECREMENT_EXPR:
14431 case POSTINCREMENT_EXPR:
14432 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14433 args, complain, in_decl);
14434 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14435 complain|decltype_flag));
14437 case PREDECREMENT_EXPR:
14438 case PREINCREMENT_EXPR:
14439 case NEGATE_EXPR:
14440 case BIT_NOT_EXPR:
14441 case ABS_EXPR:
14442 case TRUTH_NOT_EXPR:
14443 case UNARY_PLUS_EXPR: /* Unary + */
14444 case REALPART_EXPR:
14445 case IMAGPART_EXPR:
14446 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14447 RECUR (TREE_OPERAND (t, 0)),
14448 complain|decltype_flag));
14450 case FIX_TRUNC_EXPR:
14451 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14452 0, complain));
14454 case ADDR_EXPR:
14455 op1 = TREE_OPERAND (t, 0);
14456 if (TREE_CODE (op1) == LABEL_DECL)
14457 RETURN (finish_label_address_expr (DECL_NAME (op1),
14458 EXPR_LOCATION (op1)));
14459 if (TREE_CODE (op1) == SCOPE_REF)
14460 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14461 /*done=*/true, /*address_p=*/true);
14462 else
14463 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14464 in_decl);
14465 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14466 complain|decltype_flag));
14468 case PLUS_EXPR:
14469 case MINUS_EXPR:
14470 case MULT_EXPR:
14471 case TRUNC_DIV_EXPR:
14472 case CEIL_DIV_EXPR:
14473 case FLOOR_DIV_EXPR:
14474 case ROUND_DIV_EXPR:
14475 case EXACT_DIV_EXPR:
14476 case BIT_AND_EXPR:
14477 case BIT_IOR_EXPR:
14478 case BIT_XOR_EXPR:
14479 case TRUNC_MOD_EXPR:
14480 case FLOOR_MOD_EXPR:
14481 case TRUTH_ANDIF_EXPR:
14482 case TRUTH_ORIF_EXPR:
14483 case TRUTH_AND_EXPR:
14484 case TRUTH_OR_EXPR:
14485 case RSHIFT_EXPR:
14486 case LSHIFT_EXPR:
14487 case RROTATE_EXPR:
14488 case LROTATE_EXPR:
14489 case EQ_EXPR:
14490 case NE_EXPR:
14491 case MAX_EXPR:
14492 case MIN_EXPR:
14493 case LE_EXPR:
14494 case GE_EXPR:
14495 case LT_EXPR:
14496 case GT_EXPR:
14497 case MEMBER_REF:
14498 case DOTSTAR_EXPR:
14500 tree r;
14502 ++c_inhibit_evaluation_warnings;
14504 r = build_x_binary_op
14505 (input_location, TREE_CODE (t),
14506 RECUR (TREE_OPERAND (t, 0)),
14507 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14508 ? ERROR_MARK
14509 : TREE_CODE (TREE_OPERAND (t, 0))),
14510 RECUR (TREE_OPERAND (t, 1)),
14511 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14512 ? ERROR_MARK
14513 : TREE_CODE (TREE_OPERAND (t, 1))),
14514 /*overload=*/NULL,
14515 complain|decltype_flag);
14516 if (EXPR_P (r) && TREE_NO_WARNING (t))
14517 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14519 --c_inhibit_evaluation_warnings;
14521 RETURN (r);
14524 case SCOPE_REF:
14525 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14526 /*address_p=*/false));
14527 case ARRAY_REF:
14528 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14529 args, complain, in_decl);
14530 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14531 RECUR (TREE_OPERAND (t, 1)),
14532 complain|decltype_flag));
14534 case ARRAY_NOTATION_REF:
14536 tree start_index, length, stride;
14537 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14538 args, complain, in_decl);
14539 start_index = RECUR (ARRAY_NOTATION_START (t));
14540 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14541 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14542 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14543 length, stride, TREE_TYPE (op1)));
14545 case SIZEOF_EXPR:
14546 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14547 RETURN (tsubst_copy (t, args, complain, in_decl));
14548 /* Fall through */
14550 case ALIGNOF_EXPR:
14552 tree r;
14554 op1 = TREE_OPERAND (t, 0);
14555 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14556 op1 = TREE_TYPE (op1);
14557 if (!args)
14559 /* When there are no ARGS, we are trying to evaluate a
14560 non-dependent expression from the parser. Trying to do
14561 the substitutions may not work. */
14562 if (!TYPE_P (op1))
14563 op1 = TREE_TYPE (op1);
14565 else
14567 ++cp_unevaluated_operand;
14568 ++c_inhibit_evaluation_warnings;
14569 if (TYPE_P (op1))
14570 op1 = tsubst (op1, args, complain, in_decl);
14571 else
14572 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14573 /*function_p=*/false,
14574 /*integral_constant_expression_p=*/
14575 false);
14576 --cp_unevaluated_operand;
14577 --c_inhibit_evaluation_warnings;
14579 if (TYPE_P (op1))
14580 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14581 complain & tf_error);
14582 else
14583 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14584 complain & tf_error);
14585 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14587 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14589 if (!processing_template_decl && TYPE_P (op1))
14591 r = build_min (SIZEOF_EXPR, size_type_node,
14592 build1 (NOP_EXPR, op1, error_mark_node));
14593 SIZEOF_EXPR_TYPE_P (r) = 1;
14595 else
14596 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14597 TREE_SIDE_EFFECTS (r) = 0;
14598 TREE_READONLY (r) = 1;
14600 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14602 RETURN (r);
14605 case AT_ENCODE_EXPR:
14607 op1 = TREE_OPERAND (t, 0);
14608 ++cp_unevaluated_operand;
14609 ++c_inhibit_evaluation_warnings;
14610 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14611 /*function_p=*/false,
14612 /*integral_constant_expression_p=*/false);
14613 --cp_unevaluated_operand;
14614 --c_inhibit_evaluation_warnings;
14615 RETURN (objc_build_encode_expr (op1));
14618 case NOEXCEPT_EXPR:
14619 op1 = TREE_OPERAND (t, 0);
14620 ++cp_unevaluated_operand;
14621 ++c_inhibit_evaluation_warnings;
14622 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14623 /*function_p=*/false,
14624 /*integral_constant_expression_p=*/false);
14625 --cp_unevaluated_operand;
14626 --c_inhibit_evaluation_warnings;
14627 RETURN (finish_noexcept_expr (op1, complain));
14629 case MODOP_EXPR:
14631 tree r;
14633 ++c_inhibit_evaluation_warnings;
14635 r = build_x_modify_expr
14636 (EXPR_LOCATION (t),
14637 RECUR (TREE_OPERAND (t, 0)),
14638 TREE_CODE (TREE_OPERAND (t, 1)),
14639 RECUR (TREE_OPERAND (t, 2)),
14640 complain|decltype_flag);
14641 /* TREE_NO_WARNING must be set if either the expression was
14642 parenthesized or it uses an operator such as >>= rather
14643 than plain assignment. In the former case, it was already
14644 set and must be copied. In the latter case,
14645 build_x_modify_expr sets it and it must not be reset
14646 here. */
14647 if (TREE_NO_WARNING (t))
14648 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14650 --c_inhibit_evaluation_warnings;
14652 RETURN (r);
14655 case ARROW_EXPR:
14656 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14657 args, complain, in_decl);
14658 /* Remember that there was a reference to this entity. */
14659 if (DECL_P (op1))
14660 mark_used (op1);
14661 RETURN (build_x_arrow (input_location, op1, complain));
14663 case NEW_EXPR:
14665 tree placement = RECUR (TREE_OPERAND (t, 0));
14666 tree init = RECUR (TREE_OPERAND (t, 3));
14667 vec<tree, va_gc> *placement_vec;
14668 vec<tree, va_gc> *init_vec;
14669 tree ret;
14671 if (placement == NULL_TREE)
14672 placement_vec = NULL;
14673 else
14675 placement_vec = make_tree_vector ();
14676 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14677 vec_safe_push (placement_vec, TREE_VALUE (placement));
14680 /* If there was an initializer in the original tree, but it
14681 instantiated to an empty list, then we should pass a
14682 non-NULL empty vector to tell build_new that it was an
14683 empty initializer() rather than no initializer. This can
14684 only happen when the initializer is a pack expansion whose
14685 parameter packs are of length zero. */
14686 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14687 init_vec = NULL;
14688 else
14690 init_vec = make_tree_vector ();
14691 if (init == void_zero_node)
14692 gcc_assert (init_vec != NULL);
14693 else
14695 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14696 vec_safe_push (init_vec, TREE_VALUE (init));
14700 ret = build_new (&placement_vec,
14701 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14702 RECUR (TREE_OPERAND (t, 2)),
14703 &init_vec,
14704 NEW_EXPR_USE_GLOBAL (t),
14705 complain);
14707 if (placement_vec != NULL)
14708 release_tree_vector (placement_vec);
14709 if (init_vec != NULL)
14710 release_tree_vector (init_vec);
14712 RETURN (ret);
14715 case DELETE_EXPR:
14716 RETURN (delete_sanity
14717 (RECUR (TREE_OPERAND (t, 0)),
14718 RECUR (TREE_OPERAND (t, 1)),
14719 DELETE_EXPR_USE_VEC (t),
14720 DELETE_EXPR_USE_GLOBAL (t),
14721 complain));
14723 case COMPOUND_EXPR:
14725 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14726 complain & ~tf_decltype, in_decl,
14727 /*function_p=*/false,
14728 integral_constant_expression_p);
14729 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14730 op0,
14731 RECUR (TREE_OPERAND (t, 1)),
14732 complain|decltype_flag));
14735 case CALL_EXPR:
14737 tree function;
14738 vec<tree, va_gc> *call_args;
14739 unsigned int nargs, i;
14740 bool qualified_p;
14741 bool koenig_p;
14742 tree ret;
14744 function = CALL_EXPR_FN (t);
14745 /* When we parsed the expression, we determined whether or
14746 not Koenig lookup should be performed. */
14747 koenig_p = KOENIG_LOOKUP_P (t);
14748 if (TREE_CODE (function) == SCOPE_REF)
14750 qualified_p = true;
14751 function = tsubst_qualified_id (function, args, complain, in_decl,
14752 /*done=*/false,
14753 /*address_p=*/false);
14755 else if (koenig_p && identifier_p (function))
14757 /* Do nothing; calling tsubst_copy_and_build on an identifier
14758 would incorrectly perform unqualified lookup again.
14760 Note that we can also have an IDENTIFIER_NODE if the earlier
14761 unqualified lookup found a member function; in that case
14762 koenig_p will be false and we do want to do the lookup
14763 again to find the instantiated member function.
14765 FIXME but doing that causes c++/15272, so we need to stop
14766 using IDENTIFIER_NODE in that situation. */
14767 qualified_p = false;
14769 else
14771 if (TREE_CODE (function) == COMPONENT_REF)
14773 tree op = TREE_OPERAND (function, 1);
14775 qualified_p = (TREE_CODE (op) == SCOPE_REF
14776 || (BASELINK_P (op)
14777 && BASELINK_QUALIFIED_P (op)));
14779 else
14780 qualified_p = false;
14782 if (TREE_CODE (function) == ADDR_EXPR
14783 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14784 /* Avoid error about taking the address of a constructor. */
14785 function = TREE_OPERAND (function, 0);
14787 function = tsubst_copy_and_build (function, args, complain,
14788 in_decl,
14789 !qualified_p,
14790 integral_constant_expression_p);
14792 if (BASELINK_P (function))
14793 qualified_p = true;
14796 nargs = call_expr_nargs (t);
14797 call_args = make_tree_vector ();
14798 for (i = 0; i < nargs; ++i)
14800 tree arg = CALL_EXPR_ARG (t, i);
14802 if (!PACK_EXPANSION_P (arg))
14803 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14804 else
14806 /* Expand the pack expansion and push each entry onto
14807 CALL_ARGS. */
14808 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14809 if (TREE_CODE (arg) == TREE_VEC)
14811 unsigned int len, j;
14813 len = TREE_VEC_LENGTH (arg);
14814 for (j = 0; j < len; ++j)
14816 tree value = TREE_VEC_ELT (arg, j);
14817 if (value != NULL_TREE)
14818 value = convert_from_reference (value);
14819 vec_safe_push (call_args, value);
14822 else
14824 /* A partial substitution. Add one entry. */
14825 vec_safe_push (call_args, arg);
14830 /* We do not perform argument-dependent lookup if normal
14831 lookup finds a non-function, in accordance with the
14832 expected resolution of DR 218. */
14833 if (koenig_p
14834 && ((is_overloaded_fn (function)
14835 /* If lookup found a member function, the Koenig lookup is
14836 not appropriate, even if an unqualified-name was used
14837 to denote the function. */
14838 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14839 || identifier_p (function))
14840 /* Only do this when substitution turns a dependent call
14841 into a non-dependent call. */
14842 && type_dependent_expression_p_push (t)
14843 && !any_type_dependent_arguments_p (call_args))
14844 function = perform_koenig_lookup (function, call_args, false,
14845 tf_none);
14847 if (identifier_p (function)
14848 && !any_type_dependent_arguments_p (call_args))
14850 if (koenig_p && (complain & tf_warning_or_error))
14852 /* For backwards compatibility and good diagnostics, try
14853 the unqualified lookup again if we aren't in SFINAE
14854 context. */
14855 tree unq = (tsubst_copy_and_build
14856 (function, args, complain, in_decl, true,
14857 integral_constant_expression_p));
14858 if (unq == error_mark_node)
14859 RETURN (error_mark_node);
14861 if (unq != function)
14863 tree fn = unq;
14864 if (INDIRECT_REF_P (fn))
14865 fn = TREE_OPERAND (fn, 0);
14866 if (TREE_CODE (fn) == COMPONENT_REF)
14867 fn = TREE_OPERAND (fn, 1);
14868 if (is_overloaded_fn (fn))
14869 fn = get_first_fn (fn);
14870 if (permerror (EXPR_LOC_OR_HERE (t),
14871 "%qD was not declared in this scope, "
14872 "and no declarations were found by "
14873 "argument-dependent lookup at the point "
14874 "of instantiation", function))
14876 if (!DECL_P (fn))
14877 /* Can't say anything more. */;
14878 else if (DECL_CLASS_SCOPE_P (fn))
14880 inform (EXPR_LOC_OR_HERE (t),
14881 "declarations in dependent base %qT are "
14882 "not found by unqualified lookup",
14883 DECL_CLASS_CONTEXT (fn));
14884 if (current_class_ptr)
14885 inform (EXPR_LOC_OR_HERE (t),
14886 "use %<this->%D%> instead", function);
14887 else
14888 inform (EXPR_LOC_OR_HERE (t),
14889 "use %<%T::%D%> instead",
14890 current_class_name, function);
14892 else
14893 inform (0, "%q+D declared here, later in the "
14894 "translation unit", fn);
14896 function = unq;
14899 if (identifier_p (function))
14901 if (complain & tf_error)
14902 unqualified_name_lookup_error (function);
14903 release_tree_vector (call_args);
14904 RETURN (error_mark_node);
14908 /* Remember that there was a reference to this entity. */
14909 if (DECL_P (function))
14910 mark_used (function);
14912 /* Put back tf_decltype for the actual call. */
14913 complain |= decltype_flag;
14915 if (TREE_CODE (function) == OFFSET_REF)
14916 ret = build_offset_ref_call_from_tree (function, &call_args,
14917 complain);
14918 else if (TREE_CODE (function) == COMPONENT_REF)
14920 tree instance = TREE_OPERAND (function, 0);
14921 tree fn = TREE_OPERAND (function, 1);
14923 if (processing_template_decl
14924 && (type_dependent_expression_p (instance)
14925 || (!BASELINK_P (fn)
14926 && TREE_CODE (fn) != FIELD_DECL)
14927 || type_dependent_expression_p (fn)
14928 || any_type_dependent_arguments_p (call_args)))
14929 ret = build_nt_call_vec (function, call_args);
14930 else if (!BASELINK_P (fn))
14931 ret = finish_call_expr (function, &call_args,
14932 /*disallow_virtual=*/false,
14933 /*koenig_p=*/false,
14934 complain);
14935 else
14936 ret = (build_new_method_call
14937 (instance, fn,
14938 &call_args, NULL_TREE,
14939 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14940 /*fn_p=*/NULL,
14941 complain));
14943 else
14944 ret = finish_call_expr (function, &call_args,
14945 /*disallow_virtual=*/qualified_p,
14946 koenig_p,
14947 complain);
14949 release_tree_vector (call_args);
14951 RETURN (ret);
14954 case COND_EXPR:
14956 tree cond = RECUR (TREE_OPERAND (t, 0));
14957 tree exp1, exp2;
14959 if (TREE_CODE (cond) == INTEGER_CST)
14961 if (integer_zerop (cond))
14963 ++c_inhibit_evaluation_warnings;
14964 exp1 = RECUR (TREE_OPERAND (t, 1));
14965 --c_inhibit_evaluation_warnings;
14966 exp2 = RECUR (TREE_OPERAND (t, 2));
14968 else
14970 exp1 = RECUR (TREE_OPERAND (t, 1));
14971 ++c_inhibit_evaluation_warnings;
14972 exp2 = RECUR (TREE_OPERAND (t, 2));
14973 --c_inhibit_evaluation_warnings;
14976 else
14978 exp1 = RECUR (TREE_OPERAND (t, 1));
14979 exp2 = RECUR (TREE_OPERAND (t, 2));
14982 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14983 cond, exp1, exp2, complain));
14986 case PSEUDO_DTOR_EXPR:
14987 RETURN (finish_pseudo_destructor_expr
14988 (RECUR (TREE_OPERAND (t, 0)),
14989 RECUR (TREE_OPERAND (t, 1)),
14990 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14991 input_location));
14993 case TREE_LIST:
14995 tree purpose, value, chain;
14997 if (t == void_list_node)
14998 RETURN (t);
15000 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15001 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15003 /* We have pack expansions, so expand those and
15004 create a new list out of it. */
15005 tree purposevec = NULL_TREE;
15006 tree valuevec = NULL_TREE;
15007 tree chain;
15008 int i, len = -1;
15010 /* Expand the argument expressions. */
15011 if (TREE_PURPOSE (t))
15012 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15013 complain, in_decl);
15014 if (TREE_VALUE (t))
15015 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15016 complain, in_decl);
15018 /* Build the rest of the list. */
15019 chain = TREE_CHAIN (t);
15020 if (chain && chain != void_type_node)
15021 chain = RECUR (chain);
15023 /* Determine the number of arguments. */
15024 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15026 len = TREE_VEC_LENGTH (purposevec);
15027 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15029 else if (TREE_CODE (valuevec) == TREE_VEC)
15030 len = TREE_VEC_LENGTH (valuevec);
15031 else
15033 /* Since we only performed a partial substitution into
15034 the argument pack, we only RETURN (a single list
15035 node. */
15036 if (purposevec == TREE_PURPOSE (t)
15037 && valuevec == TREE_VALUE (t)
15038 && chain == TREE_CHAIN (t))
15039 RETURN (t);
15041 RETURN (tree_cons (purposevec, valuevec, chain));
15044 /* Convert the argument vectors into a TREE_LIST */
15045 i = len;
15046 while (i > 0)
15048 /* Grab the Ith values. */
15049 i--;
15050 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15051 : NULL_TREE;
15052 value
15053 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15054 : NULL_TREE;
15056 /* Build the list (backwards). */
15057 chain = tree_cons (purpose, value, chain);
15060 RETURN (chain);
15063 purpose = TREE_PURPOSE (t);
15064 if (purpose)
15065 purpose = RECUR (purpose);
15066 value = TREE_VALUE (t);
15067 if (value)
15068 value = RECUR (value);
15069 chain = TREE_CHAIN (t);
15070 if (chain && chain != void_type_node)
15071 chain = RECUR (chain);
15072 if (purpose == TREE_PURPOSE (t)
15073 && value == TREE_VALUE (t)
15074 && chain == TREE_CHAIN (t))
15075 RETURN (t);
15076 RETURN (tree_cons (purpose, value, chain));
15079 case COMPONENT_REF:
15081 tree object;
15082 tree object_type;
15083 tree member;
15085 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15086 args, complain, in_decl);
15087 /* Remember that there was a reference to this entity. */
15088 if (DECL_P (object))
15089 mark_used (object);
15090 object_type = TREE_TYPE (object);
15092 member = TREE_OPERAND (t, 1);
15093 if (BASELINK_P (member))
15094 member = tsubst_baselink (member,
15095 non_reference (TREE_TYPE (object)),
15096 args, complain, in_decl);
15097 else
15098 member = tsubst_copy (member, args, complain, in_decl);
15099 if (member == error_mark_node)
15100 RETURN (error_mark_node);
15102 if (type_dependent_expression_p (object))
15103 /* We can't do much here. */;
15104 else if (!CLASS_TYPE_P (object_type))
15106 if (scalarish_type_p (object_type))
15108 tree s = NULL_TREE;
15109 tree dtor = member;
15111 if (TREE_CODE (dtor) == SCOPE_REF)
15113 s = TREE_OPERAND (dtor, 0);
15114 dtor = TREE_OPERAND (dtor, 1);
15116 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15118 dtor = TREE_OPERAND (dtor, 0);
15119 if (TYPE_P (dtor))
15120 RETURN (finish_pseudo_destructor_expr
15121 (object, s, dtor, input_location));
15125 else if (TREE_CODE (member) == SCOPE_REF
15126 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15128 /* Lookup the template functions now that we know what the
15129 scope is. */
15130 tree scope = TREE_OPERAND (member, 0);
15131 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15132 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15133 member = lookup_qualified_name (scope, tmpl,
15134 /*is_type_p=*/false,
15135 /*complain=*/false);
15136 if (BASELINK_P (member))
15138 BASELINK_FUNCTIONS (member)
15139 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15140 args);
15141 member = (adjust_result_of_qualified_name_lookup
15142 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15143 object_type));
15145 else
15147 qualified_name_lookup_error (scope, tmpl, member,
15148 input_location);
15149 RETURN (error_mark_node);
15152 else if (TREE_CODE (member) == SCOPE_REF
15153 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15154 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15156 if (complain & tf_error)
15158 if (TYPE_P (TREE_OPERAND (member, 0)))
15159 error ("%qT is not a class or namespace",
15160 TREE_OPERAND (member, 0));
15161 else
15162 error ("%qD is not a class or namespace",
15163 TREE_OPERAND (member, 0));
15165 RETURN (error_mark_node);
15167 else if (TREE_CODE (member) == FIELD_DECL)
15168 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
15170 RETURN (finish_class_member_access_expr (object, member,
15171 /*template_p=*/false,
15172 complain));
15175 case THROW_EXPR:
15176 RETURN (build_throw
15177 (RECUR (TREE_OPERAND (t, 0))));
15179 case CONSTRUCTOR:
15181 vec<constructor_elt, va_gc> *n;
15182 constructor_elt *ce;
15183 unsigned HOST_WIDE_INT idx;
15184 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15185 bool process_index_p;
15186 int newlen;
15187 bool need_copy_p = false;
15188 tree r;
15190 if (type == error_mark_node)
15191 RETURN (error_mark_node);
15193 /* digest_init will do the wrong thing if we let it. */
15194 if (type && TYPE_PTRMEMFUNC_P (type))
15195 RETURN (t);
15197 /* We do not want to process the index of aggregate
15198 initializers as they are identifier nodes which will be
15199 looked up by digest_init. */
15200 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15202 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15203 newlen = vec_safe_length (n);
15204 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15206 if (ce->index && process_index_p
15207 /* An identifier index is looked up in the type
15208 being initialized, not the current scope. */
15209 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15210 ce->index = RECUR (ce->index);
15212 if (PACK_EXPANSION_P (ce->value))
15214 /* Substitute into the pack expansion. */
15215 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15216 in_decl);
15218 if (ce->value == error_mark_node
15219 || PACK_EXPANSION_P (ce->value))
15221 else if (TREE_VEC_LENGTH (ce->value) == 1)
15222 /* Just move the argument into place. */
15223 ce->value = TREE_VEC_ELT (ce->value, 0);
15224 else
15226 /* Update the length of the final CONSTRUCTOR
15227 arguments vector, and note that we will need to
15228 copy.*/
15229 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15230 need_copy_p = true;
15233 else
15234 ce->value = RECUR (ce->value);
15237 if (need_copy_p)
15239 vec<constructor_elt, va_gc> *old_n = n;
15241 vec_alloc (n, newlen);
15242 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15244 if (TREE_CODE (ce->value) == TREE_VEC)
15246 int i, len = TREE_VEC_LENGTH (ce->value);
15247 for (i = 0; i < len; ++i)
15248 CONSTRUCTOR_APPEND_ELT (n, 0,
15249 TREE_VEC_ELT (ce->value, i));
15251 else
15252 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15256 r = build_constructor (init_list_type_node, n);
15257 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15259 if (TREE_HAS_CONSTRUCTOR (t))
15260 RETURN (finish_compound_literal (type, r, complain));
15262 TREE_TYPE (r) = type;
15263 RETURN (r);
15266 case TYPEID_EXPR:
15268 tree operand_0 = TREE_OPERAND (t, 0);
15269 if (TYPE_P (operand_0))
15271 operand_0 = tsubst (operand_0, args, complain, in_decl);
15272 RETURN (get_typeid (operand_0, complain));
15274 else
15276 operand_0 = RECUR (operand_0);
15277 RETURN (build_typeid (operand_0, complain));
15281 case VAR_DECL:
15282 if (!args)
15283 RETURN (t);
15284 else if (DECL_PACK_P (t))
15286 /* We don't build decls for an instantiation of a
15287 variadic capture proxy, we instantiate the elements
15288 when needed. */
15289 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15290 return RECUR (DECL_VALUE_EXPR (t));
15292 /* Fall through */
15294 case PARM_DECL:
15296 tree r = tsubst_copy (t, args, complain, in_decl);
15298 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15299 /* If the original type was a reference, we'll be wrapped in
15300 the appropriate INDIRECT_REF. */
15301 r = convert_from_reference (r);
15303 // If the type of the argument is a pack expansion, then
15304 // the parameter must also be expanded.
15305 if (PACK_EXPANSION_P (TREE_TYPE (r)))
15306 r = make_pack_expansion (r);
15308 RETURN (r);
15311 case VA_ARG_EXPR:
15312 RETURN (build_x_va_arg (EXPR_LOCATION (t),
15313 RECUR (TREE_OPERAND (t, 0)),
15314 tsubst (TREE_TYPE (t), args, complain, in_decl)));
15316 case OFFSETOF_EXPR:
15317 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
15319 case TRAIT_EXPR:
15321 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
15322 complain, in_decl);
15324 tree type2 = TRAIT_EXPR_TYPE2 (t);
15325 if (type2)
15326 type2 = tsubst_copy (type2, args, complain, in_decl);
15328 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15331 case STMT_EXPR:
15333 tree old_stmt_expr = cur_stmt_expr;
15334 tree stmt_expr = begin_stmt_expr ();
15336 cur_stmt_expr = stmt_expr;
15337 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15338 integral_constant_expression_p);
15339 stmt_expr = finish_stmt_expr (stmt_expr, false);
15340 cur_stmt_expr = old_stmt_expr;
15342 /* If the resulting list of expression statement is empty,
15343 fold it further into void_zero_node. */
15344 if (empty_expr_stmt_p (stmt_expr))
15345 stmt_expr = void_zero_node;
15347 RETURN (stmt_expr);
15350 case LAMBDA_EXPR:
15352 tree r = build_lambda_expr ();
15354 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15355 LAMBDA_EXPR_CLOSURE (r) = type;
15356 CLASSTYPE_LAMBDA_EXPR (type) = r;
15358 LAMBDA_EXPR_LOCATION (r)
15359 = LAMBDA_EXPR_LOCATION (t);
15360 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15361 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15362 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15363 LAMBDA_EXPR_DISCRIMINATOR (r)
15364 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15365 /* For a function scope, we want to use tsubst so that we don't
15366 complain about referring to an auto function before its return
15367 type has been deduced. Otherwise, we want to use tsubst_copy so
15368 that we look up the existing field/parameter/variable rather
15369 than build a new one. */
15370 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15371 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15372 scope = tsubst (scope, args, complain, in_decl);
15373 else if (scope && TREE_CODE (scope) == PARM_DECL)
15375 /* Look up the parameter we want directly, as tsubst_copy
15376 doesn't do what we need. */
15377 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15378 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15379 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15380 parm = DECL_CHAIN (parm);
15381 scope = parm;
15382 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15383 if (DECL_CONTEXT (scope) == NULL_TREE)
15384 DECL_CONTEXT (scope) = fn;
15386 else
15387 scope = RECUR (scope);
15388 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15389 LAMBDA_EXPR_RETURN_TYPE (r)
15390 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15392 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15393 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15395 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15396 determine_visibility (TYPE_NAME (type));
15397 /* Now that we know visibility, instantiate the type so we have a
15398 declaration of the op() for later calls to lambda_function. */
15399 complete_type (type);
15401 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15403 RETURN (build_lambda_object (r));
15406 case TARGET_EXPR:
15407 /* We can get here for a constant initializer of non-dependent type.
15408 FIXME stop folding in cp_parser_initializer_clause. */
15410 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15411 complain);
15412 RETURN (r);
15415 case TRANSACTION_EXPR:
15416 RETURN (tsubst_expr(t, args, complain, in_decl,
15417 integral_constant_expression_p));
15419 case PAREN_EXPR:
15420 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15422 case VEC_PERM_EXPR:
15423 RETURN (build_x_vec_perm_expr (input_location,
15424 RECUR (TREE_OPERAND (t, 0)),
15425 RECUR (TREE_OPERAND (t, 1)),
15426 RECUR (TREE_OPERAND (t, 2)),
15427 complain));
15429 case REQUIRES_EXPR:
15430 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
15432 case VALIDEXPR_EXPR:
15433 RETURN (tsubst_validexpr_expr (t, args, in_decl));
15435 case VALIDTYPE_EXPR:
15436 RETURN (tsubst_validtype_expr (t, args, in_decl));
15438 case CONSTEXPR_EXPR:
15439 RETURN (tsubst_constexpr_expr (t, args, in_decl));
15441 // Normally, *_REQ are reduced out of requiremetns when used
15442 // as constraints. If a concept is checked directly via e.g.,
15443 // a static_assert, however, these appear in the input tree.
15445 case EXPR_REQ:
15446 RETURN (tsubst_expr_req (t, args, in_decl));
15448 case TYPE_REQ:
15449 RETURN (tsubst_type_req (t, args, in_decl));
15451 case NESTED_REQ:
15452 RETURN (tsubst_nested_req (t, args, in_decl));
15454 default:
15455 /* Handle Objective-C++ constructs, if appropriate. */
15457 tree subst
15458 = objcp_tsubst_copy_and_build (t, args, complain,
15459 in_decl, /*function_p=*/false);
15460 if (subst)
15461 RETURN (subst);
15463 RETURN (tsubst_copy (t, args, complain, in_decl));
15466 #undef RECUR
15467 #undef RETURN
15468 out:
15469 input_location = loc;
15470 return retval;
15473 /* Verify that the instantiated ARGS are valid. For type arguments,
15474 make sure that the type's linkage is ok. For non-type arguments,
15475 make sure they are constants if they are integral or enumerations.
15476 Emit an error under control of COMPLAIN, and return TRUE on error. */
15478 static bool
15479 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15481 if (dependent_template_arg_p (t))
15482 return false;
15483 if (ARGUMENT_PACK_P (t))
15485 tree vec = ARGUMENT_PACK_ARGS (t);
15486 int len = TREE_VEC_LENGTH (vec);
15487 bool result = false;
15488 int i;
15490 for (i = 0; i < len; ++i)
15491 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15492 result = true;
15493 return result;
15495 else if (TYPE_P (t))
15497 /* [basic.link]: A name with no linkage (notably, the name
15498 of a class or enumeration declared in a local scope)
15499 shall not be used to declare an entity with linkage.
15500 This implies that names with no linkage cannot be used as
15501 template arguments
15503 DR 757 relaxes this restriction for C++0x. */
15504 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15505 : no_linkage_check (t, /*relaxed_p=*/false));
15507 if (nt)
15509 /* DR 488 makes use of a type with no linkage cause
15510 type deduction to fail. */
15511 if (complain & tf_error)
15513 if (TYPE_ANONYMOUS_P (nt))
15514 error ("%qT is/uses anonymous type", t);
15515 else
15516 error ("template argument for %qD uses local type %qT",
15517 tmpl, t);
15519 return true;
15521 /* In order to avoid all sorts of complications, we do not
15522 allow variably-modified types as template arguments. */
15523 else if (variably_modified_type_p (t, NULL_TREE))
15525 if (complain & tf_error)
15526 error ("%qT is a variably modified type", t);
15527 return true;
15530 /* Class template and alias template arguments should be OK. */
15531 else if (DECL_TYPE_TEMPLATE_P (t))
15533 /* A non-type argument of integral or enumerated type must be a
15534 constant. */
15535 else if (TREE_TYPE (t)
15536 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15537 && !TREE_CONSTANT (t))
15539 if (complain & tf_error)
15540 error ("integral expression %qE is not constant", t);
15541 return true;
15543 return false;
15546 static bool
15547 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15549 int ix, len = DECL_NTPARMS (tmpl);
15550 bool result = false;
15552 for (ix = 0; ix != len; ix++)
15554 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15555 result = true;
15557 if (result && (complain & tf_error))
15558 error (" trying to instantiate %qD", tmpl);
15559 return result;
15562 /* We're out of SFINAE context now, so generate diagnostics for the access
15563 errors we saw earlier when instantiating D from TMPL and ARGS. */
15565 static void
15566 recheck_decl_substitution (tree d, tree tmpl, tree args)
15568 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15569 tree type = TREE_TYPE (pattern);
15570 location_t loc = input_location;
15572 push_access_scope (d);
15573 push_deferring_access_checks (dk_no_deferred);
15574 input_location = DECL_SOURCE_LOCATION (pattern);
15575 tsubst (type, args, tf_warning_or_error, d);
15576 input_location = loc;
15577 pop_deferring_access_checks ();
15578 pop_access_scope (d);
15581 /* Instantiate the indicated variable, function, or alias template TMPL with
15582 the template arguments in TARG_PTR. */
15584 static tree
15585 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15587 tree targ_ptr = orig_args;
15588 tree fndecl;
15589 tree gen_tmpl;
15590 tree spec;
15591 bool access_ok = true;
15593 if (tmpl == error_mark_node)
15594 return error_mark_node;
15596 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15598 /* If this function is a clone, handle it specially. */
15599 if (DECL_CLONED_FUNCTION_P (tmpl))
15601 tree spec;
15602 tree clone;
15604 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15605 DECL_CLONED_FUNCTION. */
15606 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15607 targ_ptr, complain);
15608 if (spec == error_mark_node)
15609 return error_mark_node;
15611 /* Look for the clone. */
15612 FOR_EACH_CLONE (clone, spec)
15613 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15614 return clone;
15615 /* We should always have found the clone by now. */
15616 gcc_unreachable ();
15617 return NULL_TREE;
15620 /* Check to see if we already have this specialization. */
15621 gen_tmpl = most_general_template (tmpl);
15622 if (tmpl != gen_tmpl)
15623 /* The TMPL is a partial instantiation. To get a full set of
15624 arguments we must add the arguments used to perform the
15625 partial instantiation. */
15626 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15627 targ_ptr);
15629 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15630 but it doesn't seem to be on the hot path. */
15631 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15633 gcc_assert (tmpl == gen_tmpl
15634 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15635 == spec)
15636 || fndecl == NULL_TREE);
15638 if (spec != NULL_TREE)
15640 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15642 if (complain & tf_error)
15643 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15644 return error_mark_node;
15646 return spec;
15649 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15650 complain))
15651 return error_mark_node;
15653 /* We are building a FUNCTION_DECL, during which the access of its
15654 parameters and return types have to be checked. However this
15655 FUNCTION_DECL which is the desired context for access checking
15656 is not built yet. We solve this chicken-and-egg problem by
15657 deferring all checks until we have the FUNCTION_DECL. */
15658 push_deferring_access_checks (dk_deferred);
15660 /* Instantiation of the function happens in the context of the function
15661 template, not the context of the overload resolution we're doing. */
15662 push_to_top_level ();
15663 /* If there are dependent arguments, e.g. because we're doing partial
15664 ordering, make sure processing_template_decl stays set. */
15665 if (uses_template_parms (targ_ptr))
15666 ++processing_template_decl;
15667 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15669 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15670 complain, gen_tmpl);
15671 push_nested_class (ctx);
15673 /* Substitute template parameters to obtain the specialization. */
15674 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15675 targ_ptr, complain, gen_tmpl);
15676 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15677 pop_nested_class ();
15678 pop_from_top_level ();
15680 if (fndecl == error_mark_node)
15682 pop_deferring_access_checks ();
15683 return error_mark_node;
15686 /* The DECL_TI_TEMPLATE should always be the immediate parent
15687 template, not the most general template. */
15688 DECL_TI_TEMPLATE (fndecl) = tmpl;
15690 /* Now we know the specialization, compute access previously
15691 deferred. */
15692 push_access_scope (fndecl);
15693 if (!perform_deferred_access_checks (complain))
15694 access_ok = false;
15695 pop_access_scope (fndecl);
15696 pop_deferring_access_checks ();
15698 /* If we've just instantiated the main entry point for a function,
15699 instantiate all the alternate entry points as well. We do this
15700 by cloning the instantiation of the main entry point, not by
15701 instantiating the template clones. */
15702 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15703 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15705 if (!access_ok)
15707 if (!(complain & tf_error))
15709 /* Remember to reinstantiate when we're out of SFINAE so the user
15710 can see the errors. */
15711 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15713 return error_mark_node;
15715 return fndecl;
15718 /* Wrapper for instantiate_template_1. */
15720 tree
15721 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15723 tree ret;
15724 timevar_push (TV_TEMPLATE_INST);
15725 ret = instantiate_template_1 (tmpl, orig_args, complain);
15726 timevar_pop (TV_TEMPLATE_INST);
15727 return ret;
15730 /* Instantiate the alias template TMPL with ARGS. Also push a template
15731 instantiation level, which instantiate_template doesn't do because
15732 functions and variables have sufficient context established by the
15733 callers. */
15735 static tree
15736 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15738 struct pending_template *old_last_pend = last_pending_template;
15739 struct tinst_level *old_error_tinst = last_error_tinst_level;
15740 if (tmpl == error_mark_node || args == error_mark_node)
15741 return error_mark_node;
15742 tree tinst = build_tree_list (tmpl, args);
15743 if (!push_tinst_level (tinst))
15745 ggc_free (tinst);
15746 return error_mark_node;
15749 args =
15750 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15751 args, tmpl, complain,
15752 /*require_all_args=*/true,
15753 /*use_default_args=*/true);
15755 tree r = instantiate_template (tmpl, args, complain);
15756 pop_tinst_level ();
15757 /* We can't free this if a pending_template entry or last_error_tinst_level
15758 is pointing at it. */
15759 if (last_pending_template == old_last_pend
15760 && last_error_tinst_level == old_error_tinst)
15761 ggc_free (tinst);
15763 return r;
15766 /* PARM is a template parameter pack for FN. Returns true iff
15767 PARM is used in a deducible way in the argument list of FN. */
15769 static bool
15770 pack_deducible_p (tree parm, tree fn)
15772 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15773 for (; t; t = TREE_CHAIN (t))
15775 tree type = TREE_VALUE (t);
15776 tree packs;
15777 if (!PACK_EXPANSION_P (type))
15778 continue;
15779 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15780 packs; packs = TREE_CHAIN (packs))
15781 if (TREE_VALUE (packs) == parm)
15783 /* The template parameter pack is used in a function parameter
15784 pack. If this is the end of the parameter list, the
15785 template parameter pack is deducible. */
15786 if (TREE_CHAIN (t) == void_list_node)
15787 return true;
15788 else
15789 /* Otherwise, not. Well, it could be deduced from
15790 a non-pack parameter, but doing so would end up with
15791 a deduction mismatch, so don't bother. */
15792 return false;
15795 /* The template parameter pack isn't used in any function parameter
15796 packs, but it might be used deeper, e.g. tuple<Args...>. */
15797 return true;
15800 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15801 NARGS elements of the arguments that are being used when calling
15802 it. TARGS is a vector into which the deduced template arguments
15803 are placed.
15805 Return zero for success, 2 for an incomplete match that doesn't resolve
15806 all the types, and 1 for complete failure. An error message will be
15807 printed only for an incomplete match.
15809 If FN is a conversion operator, or we are trying to produce a specific
15810 specialization, RETURN_TYPE is the return type desired.
15812 The EXPLICIT_TARGS are explicit template arguments provided via a
15813 template-id.
15815 The parameter STRICT is one of:
15817 DEDUCE_CALL:
15818 We are deducing arguments for a function call, as in
15819 [temp.deduct.call].
15821 DEDUCE_CONV:
15822 We are deducing arguments for a conversion function, as in
15823 [temp.deduct.conv].
15825 DEDUCE_EXACT:
15826 We are deducing arguments when doing an explicit instantiation
15827 as in [temp.explicit], when determining an explicit specialization
15828 as in [temp.expl.spec], or when taking the address of a function
15829 template, as in [temp.deduct.funcaddr]. */
15831 tree
15832 fn_type_unification (tree fn,
15833 tree explicit_targs,
15834 tree targs,
15835 const tree *args,
15836 unsigned int nargs,
15837 tree return_type,
15838 unification_kind_t strict,
15839 int flags,
15840 bool explain_p,
15841 bool decltype_p)
15843 tree parms;
15844 tree fntype;
15845 tree decl = NULL_TREE;
15846 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15847 bool ok;
15848 static int deduction_depth;
15849 struct pending_template *old_last_pend = last_pending_template;
15850 struct tinst_level *old_error_tinst = last_error_tinst_level;
15851 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15852 tree tinst;
15853 tree r = error_mark_node;
15855 if (decltype_p)
15856 complain |= tf_decltype;
15858 /* In C++0x, it's possible to have a function template whose type depends
15859 on itself recursively. This is most obvious with decltype, but can also
15860 occur with enumeration scope (c++/48969). So we need to catch infinite
15861 recursion and reject the substitution at deduction time; this function
15862 will return error_mark_node for any repeated substitution.
15864 This also catches excessive recursion such as when f<N> depends on
15865 f<N-1> across all integers, and returns error_mark_node for all the
15866 substitutions back up to the initial one.
15868 This is, of course, not reentrant. */
15869 if (excessive_deduction_depth)
15870 return error_mark_node;
15871 tinst = build_tree_list (fn, NULL_TREE);
15872 ++deduction_depth;
15874 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15876 fntype = TREE_TYPE (fn);
15877 if (explicit_targs)
15879 /* [temp.deduct]
15881 The specified template arguments must match the template
15882 parameters in kind (i.e., type, nontype, template), and there
15883 must not be more arguments than there are parameters;
15884 otherwise type deduction fails.
15886 Nontype arguments must match the types of the corresponding
15887 nontype template parameters, or must be convertible to the
15888 types of the corresponding nontype parameters as specified in
15889 _temp.arg.nontype_, otherwise type deduction fails.
15891 All references in the function type of the function template
15892 to the corresponding template parameters are replaced by the
15893 specified template argument values. If a substitution in a
15894 template parameter or in the function type of the function
15895 template results in an invalid type, type deduction fails. */
15896 int i, len = TREE_VEC_LENGTH (tparms);
15897 location_t loc = input_location;
15898 bool incomplete = false;
15900 /* Adjust any explicit template arguments before entering the
15901 substitution context. */
15902 explicit_targs
15903 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15904 complain,
15905 /*require_all_args=*/false,
15906 /*use_default_args=*/false));
15907 if (explicit_targs == error_mark_node)
15908 goto fail;
15910 /* Substitute the explicit args into the function type. This is
15911 necessary so that, for instance, explicitly declared function
15912 arguments can match null pointed constants. If we were given
15913 an incomplete set of explicit args, we must not do semantic
15914 processing during substitution as we could create partial
15915 instantiations. */
15916 for (i = 0; i < len; i++)
15918 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15919 bool parameter_pack = false;
15920 tree targ = TREE_VEC_ELT (explicit_targs, i);
15922 /* Dig out the actual parm. */
15923 if (TREE_CODE (parm) == TYPE_DECL
15924 || TREE_CODE (parm) == TEMPLATE_DECL)
15926 parm = TREE_TYPE (parm);
15927 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15929 else if (TREE_CODE (parm) == PARM_DECL)
15931 parm = DECL_INITIAL (parm);
15932 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15935 if (!parameter_pack && targ == NULL_TREE)
15936 /* No explicit argument for this template parameter. */
15937 incomplete = true;
15939 if (parameter_pack && pack_deducible_p (parm, fn))
15941 /* Mark the argument pack as "incomplete". We could
15942 still deduce more arguments during unification.
15943 We remove this mark in type_unification_real. */
15944 if (targ)
15946 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15947 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15948 = ARGUMENT_PACK_ARGS (targ);
15951 /* We have some incomplete argument packs. */
15952 incomplete = true;
15956 TREE_VALUE (tinst) = explicit_targs;
15957 if (!push_tinst_level (tinst))
15959 excessive_deduction_depth = true;
15960 goto fail;
15962 processing_template_decl += incomplete;
15963 input_location = DECL_SOURCE_LOCATION (fn);
15964 /* Ignore any access checks; we'll see them again in
15965 instantiate_template and they might have the wrong
15966 access path at this point. */
15967 push_deferring_access_checks (dk_deferred);
15968 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15969 complain | tf_partial, NULL_TREE);
15970 pop_deferring_access_checks ();
15971 input_location = loc;
15972 processing_template_decl -= incomplete;
15973 pop_tinst_level ();
15975 if (fntype == error_mark_node)
15976 goto fail;
15978 /* Place the explicitly specified arguments in TARGS. */
15979 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15980 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15983 /* Never do unification on the 'this' parameter. */
15984 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15986 if (return_type)
15988 tree *new_args;
15990 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15991 new_args = XALLOCAVEC (tree, nargs + 1);
15992 new_args[0] = return_type;
15993 memcpy (new_args + 1, args, nargs * sizeof (tree));
15994 args = new_args;
15995 ++nargs;
15998 /* We allow incomplete unification without an error message here
15999 because the standard doesn't seem to explicitly prohibit it. Our
16000 callers must be ready to deal with unification failures in any
16001 event. */
16003 TREE_VALUE (tinst) = targs;
16004 /* If we aren't explaining yet, push tinst context so we can see where
16005 any errors (e.g. from class instantiations triggered by instantiation
16006 of default template arguments) come from. If we are explaining, this
16007 context is redundant. */
16008 if (!explain_p && !push_tinst_level (tinst))
16010 excessive_deduction_depth = true;
16011 goto fail;
16014 /* type_unification_real will pass back any access checks from default
16015 template argument substitution. */
16016 vec<deferred_access_check, va_gc> *checks;
16017 checks = NULL;
16019 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16020 targs, parms, args, nargs, /*subr=*/0,
16021 strict, flags, &checks, explain_p);
16022 if (!explain_p)
16023 pop_tinst_level ();
16024 if (!ok)
16025 goto fail;
16027 /* Now that we have bindings for all of the template arguments,
16028 ensure that the arguments deduced for the template template
16029 parameters have compatible template parameter lists. We cannot
16030 check this property before we have deduced all template
16031 arguments, because the template parameter types of a template
16032 template parameter might depend on prior template parameters
16033 deduced after the template template parameter. The following
16034 ill-formed example illustrates this issue:
16036 template<typename T, template<T> class C> void f(C<5>, T);
16038 template<int N> struct X {};
16040 void g() {
16041 f(X<5>(), 5l); // error: template argument deduction fails
16044 The template parameter list of 'C' depends on the template type
16045 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16046 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16047 time that we deduce 'C'. */
16048 if (!template_template_parm_bindings_ok_p
16049 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16051 unify_inconsistent_template_template_parameters (explain_p);
16052 goto fail;
16055 // All is well so far. Now, check that the template constraints
16056 // are satisfied.
16057 if (!check_template_constraints (fn, targs))
16059 if (explain_p)
16060 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
16061 return error_mark_node;
16064 /* All is well so far. Now, check:
16066 [temp.deduct]
16068 When all template arguments have been deduced, all uses of
16069 template parameters in nondeduced contexts are replaced with
16070 the corresponding deduced argument values. If the
16071 substitution results in an invalid type, as described above,
16072 type deduction fails. */
16073 TREE_VALUE (tinst) = targs;
16074 if (!push_tinst_level (tinst))
16076 excessive_deduction_depth = true;
16077 goto fail;
16080 /* Also collect access checks from the instantiation. */
16081 reopen_deferring_access_checks (checks);
16083 decl = instantiate_template (fn, targs, complain);
16085 checks = get_deferred_access_checks ();
16086 pop_deferring_access_checks ();
16088 pop_tinst_level ();
16090 if (decl == error_mark_node)
16091 goto fail;
16093 /* Now perform any access checks encountered during substitution. */
16094 push_access_scope (decl);
16095 ok = perform_access_checks (checks, complain);
16096 pop_access_scope (decl);
16097 if (!ok)
16098 goto fail;
16100 /* If we're looking for an exact match, check that what we got
16101 is indeed an exact match. It might not be if some template
16102 parameters are used in non-deduced contexts. */
16103 if (strict == DEDUCE_EXACT)
16105 tree substed = TREE_TYPE (decl);
16106 unsigned int i;
16108 tree sarg
16109 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16110 if (return_type)
16111 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16112 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16113 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16115 unify_type_mismatch (explain_p, args[i],
16116 TREE_VALUE (sarg));
16117 goto fail;
16121 r = decl;
16123 fail:
16124 --deduction_depth;
16125 if (excessive_deduction_depth)
16127 if (deduction_depth == 0)
16128 /* Reset once we're all the way out. */
16129 excessive_deduction_depth = false;
16132 /* We can't free this if a pending_template entry or last_error_tinst_level
16133 is pointing at it. */
16134 if (last_pending_template == old_last_pend
16135 && last_error_tinst_level == old_error_tinst)
16136 ggc_free (tinst);
16138 return r;
16141 /* Adjust types before performing type deduction, as described in
16142 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16143 sections are symmetric. PARM is the type of a function parameter
16144 or the return type of the conversion function. ARG is the type of
16145 the argument passed to the call, or the type of the value
16146 initialized with the result of the conversion function.
16147 ARG_EXPR is the original argument expression, which may be null. */
16149 static int
16150 maybe_adjust_types_for_deduction (unification_kind_t strict,
16151 tree* parm,
16152 tree* arg,
16153 tree arg_expr)
16155 int result = 0;
16157 switch (strict)
16159 case DEDUCE_CALL:
16160 break;
16162 case DEDUCE_CONV:
16164 /* Swap PARM and ARG throughout the remainder of this
16165 function; the handling is precisely symmetric since PARM
16166 will initialize ARG rather than vice versa. */
16167 tree* temp = parm;
16168 parm = arg;
16169 arg = temp;
16170 break;
16173 case DEDUCE_EXACT:
16174 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16175 too, but here handle it by stripping the reference from PARM
16176 rather than by adding it to ARG. */
16177 if (TREE_CODE (*parm) == REFERENCE_TYPE
16178 && TYPE_REF_IS_RVALUE (*parm)
16179 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16180 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16181 && TREE_CODE (*arg) == REFERENCE_TYPE
16182 && !TYPE_REF_IS_RVALUE (*arg))
16183 *parm = TREE_TYPE (*parm);
16184 /* Nothing else to do in this case. */
16185 return 0;
16187 default:
16188 gcc_unreachable ();
16191 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16193 /* [temp.deduct.call]
16195 If P is not a reference type:
16197 --If A is an array type, the pointer type produced by the
16198 array-to-pointer standard conversion (_conv.array_) is
16199 used in place of A for type deduction; otherwise,
16201 --If A is a function type, the pointer type produced by
16202 the function-to-pointer standard conversion
16203 (_conv.func_) is used in place of A for type deduction;
16204 otherwise,
16206 --If A is a cv-qualified type, the top level
16207 cv-qualifiers of A's type are ignored for type
16208 deduction. */
16209 if (TREE_CODE (*arg) == ARRAY_TYPE)
16210 *arg = build_pointer_type (TREE_TYPE (*arg));
16211 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16212 *arg = build_pointer_type (*arg);
16213 else
16214 *arg = TYPE_MAIN_VARIANT (*arg);
16217 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16218 of the form T&&, where T is a template parameter, and the argument
16219 is an lvalue, T is deduced as A& */
16220 if (TREE_CODE (*parm) == REFERENCE_TYPE
16221 && TYPE_REF_IS_RVALUE (*parm)
16222 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16223 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16224 && (arg_expr ? real_lvalue_p (arg_expr)
16225 /* try_one_overload doesn't provide an arg_expr, but
16226 functions are always lvalues. */
16227 : TREE_CODE (*arg) == FUNCTION_TYPE))
16228 *arg = build_reference_type (*arg);
16230 /* [temp.deduct.call]
16232 If P is a cv-qualified type, the top level cv-qualifiers
16233 of P's type are ignored for type deduction. If P is a
16234 reference type, the type referred to by P is used for
16235 type deduction. */
16236 *parm = TYPE_MAIN_VARIANT (*parm);
16237 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16239 *parm = TREE_TYPE (*parm);
16240 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16243 /* DR 322. For conversion deduction, remove a reference type on parm
16244 too (which has been swapped into ARG). */
16245 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16246 *arg = TREE_TYPE (*arg);
16248 return result;
16251 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16252 template which does contain any deducible template parameters; check if
16253 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16254 unify_one_argument. */
16256 static int
16257 check_non_deducible_conversion (tree parm, tree arg, int strict,
16258 int flags, bool explain_p)
16260 tree type;
16262 if (!TYPE_P (arg))
16263 type = TREE_TYPE (arg);
16264 else
16265 type = arg;
16267 if (same_type_p (parm, type))
16268 return unify_success (explain_p);
16270 if (strict == DEDUCE_CONV)
16272 if (can_convert_arg (type, parm, NULL_TREE, flags,
16273 explain_p ? tf_warning_or_error : tf_none))
16274 return unify_success (explain_p);
16276 else if (strict != DEDUCE_EXACT)
16278 if (can_convert_arg (parm, type,
16279 TYPE_P (arg) ? NULL_TREE : arg,
16280 flags, explain_p ? tf_warning_or_error : tf_none))
16281 return unify_success (explain_p);
16284 if (strict == DEDUCE_EXACT)
16285 return unify_type_mismatch (explain_p, parm, arg);
16286 else
16287 return unify_arg_conversion (explain_p, parm, type, arg);
16290 static bool uses_deducible_template_parms (tree type);
16292 /* Returns true iff the expression EXPR is one from which a template
16293 argument can be deduced. In other words, if it's an undecorated
16294 use of a template non-type parameter. */
16296 static bool
16297 deducible_expression (tree expr)
16299 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16302 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16303 deducible way; that is, if it has a max value of <PARM> - 1. */
16305 static bool
16306 deducible_array_bound (tree domain)
16308 if (domain == NULL_TREE)
16309 return false;
16311 tree max = TYPE_MAX_VALUE (domain);
16312 if (TREE_CODE (max) != MINUS_EXPR)
16313 return false;
16315 return deducible_expression (TREE_OPERAND (max, 0));
16318 /* Returns true iff the template arguments ARGS use a template parameter
16319 in a deducible way. */
16321 static bool
16322 deducible_template_args (tree args)
16324 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16326 bool deducible;
16327 tree elt = TREE_VEC_ELT (args, i);
16328 if (ARGUMENT_PACK_P (elt))
16329 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16330 else
16332 if (PACK_EXPANSION_P (elt))
16333 elt = PACK_EXPANSION_PATTERN (elt);
16334 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16335 deducible = true;
16336 else if (TYPE_P (elt))
16337 deducible = uses_deducible_template_parms (elt);
16338 else
16339 deducible = deducible_expression (elt);
16341 if (deducible)
16342 return true;
16344 return false;
16347 /* Returns true iff TYPE contains any deducible references to template
16348 parameters, as per 14.8.2.5. */
16350 static bool
16351 uses_deducible_template_parms (tree type)
16353 if (PACK_EXPANSION_P (type))
16354 type = PACK_EXPANSION_PATTERN (type);
16356 /* T
16357 cv-list T
16358 TT<T>
16359 TT<i>
16360 TT<> */
16361 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16362 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16363 return true;
16365 /* T*
16367 T&& */
16368 if (POINTER_TYPE_P (type))
16369 return uses_deducible_template_parms (TREE_TYPE (type));
16371 /* T[integer-constant ]
16372 type [i] */
16373 if (TREE_CODE (type) == ARRAY_TYPE)
16374 return (uses_deducible_template_parms (TREE_TYPE (type))
16375 || deducible_array_bound (TYPE_DOMAIN (type)));
16377 /* T type ::*
16378 type T::*
16379 T T::*
16380 T (type ::*)()
16381 type (T::*)()
16382 type (type ::*)(T)
16383 type (T::*)(T)
16384 T (type ::*)(T)
16385 T (T::*)()
16386 T (T::*)(T) */
16387 if (TYPE_PTRMEM_P (type))
16388 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16389 || (uses_deducible_template_parms
16390 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16392 /* template-name <T> (where template-name refers to a class template)
16393 template-name <i> (where template-name refers to a class template) */
16394 if (CLASS_TYPE_P (type)
16395 && CLASSTYPE_TEMPLATE_INFO (type)
16396 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16397 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16398 (CLASSTYPE_TI_ARGS (type)));
16400 /* type (T)
16402 T(T) */
16403 if (TREE_CODE (type) == FUNCTION_TYPE
16404 || TREE_CODE (type) == METHOD_TYPE)
16406 if (uses_deducible_template_parms (TREE_TYPE (type)))
16407 return true;
16408 tree parm = TYPE_ARG_TYPES (type);
16409 if (TREE_CODE (type) == METHOD_TYPE)
16410 parm = TREE_CHAIN (parm);
16411 for (; parm; parm = TREE_CHAIN (parm))
16412 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16413 return true;
16416 return false;
16419 /* Subroutine of type_unification_real and unify_pack_expansion to
16420 handle unification of a single P/A pair. Parameters are as
16421 for those functions. */
16423 static int
16424 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16425 int subr, unification_kind_t strict, int flags,
16426 bool explain_p)
16428 tree arg_expr = NULL_TREE;
16429 int arg_strict;
16431 if (arg == error_mark_node || parm == error_mark_node)
16432 return unify_invalid (explain_p);
16433 if (arg == unknown_type_node)
16434 /* We can't deduce anything from this, but we might get all the
16435 template args from other function args. */
16436 return unify_success (explain_p);
16438 /* Implicit conversions (Clause 4) will be performed on a function
16439 argument to convert it to the type of the corresponding function
16440 parameter if the parameter type contains no template-parameters that
16441 participate in template argument deduction. */
16442 if (TYPE_P (parm) && !uses_template_parms (parm))
16443 /* For function parameters that contain no template-parameters at all,
16444 we have historically checked for convertibility in order to shortcut
16445 consideration of this candidate. */
16446 return check_non_deducible_conversion (parm, arg, strict, flags,
16447 explain_p);
16448 else if (strict == DEDUCE_CALL
16449 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16450 /* For function parameters with only non-deducible template parameters,
16451 just return. */
16452 return unify_success (explain_p);
16454 switch (strict)
16456 case DEDUCE_CALL:
16457 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16458 | UNIFY_ALLOW_MORE_CV_QUAL
16459 | UNIFY_ALLOW_DERIVED);
16460 break;
16462 case DEDUCE_CONV:
16463 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16464 break;
16466 case DEDUCE_EXACT:
16467 arg_strict = UNIFY_ALLOW_NONE;
16468 break;
16470 default:
16471 gcc_unreachable ();
16474 /* We only do these transformations if this is the top-level
16475 parameter_type_list in a call or declaration matching; in other
16476 situations (nested function declarators, template argument lists) we
16477 won't be comparing a type to an expression, and we don't do any type
16478 adjustments. */
16479 if (!subr)
16481 if (!TYPE_P (arg))
16483 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16484 if (type_unknown_p (arg))
16486 /* [temp.deduct.type] A template-argument can be
16487 deduced from a pointer to function or pointer
16488 to member function argument if the set of
16489 overloaded functions does not contain function
16490 templates and at most one of a set of
16491 overloaded functions provides a unique
16492 match. */
16494 if (resolve_overloaded_unification
16495 (tparms, targs, parm, arg, strict,
16496 arg_strict, explain_p))
16497 return unify_success (explain_p);
16498 return unify_overload_resolution_failure (explain_p, arg);
16501 arg_expr = arg;
16502 arg = unlowered_expr_type (arg);
16503 if (arg == error_mark_node)
16504 return unify_invalid (explain_p);
16507 arg_strict |=
16508 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16510 else
16511 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16512 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16514 /* For deduction from an init-list we need the actual list. */
16515 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16516 arg = arg_expr;
16517 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16520 /* Most parms like fn_type_unification.
16522 If SUBR is 1, we're being called recursively (to unify the
16523 arguments of a function or method parameter of a function
16524 template).
16526 CHECKS is a pointer to a vector of access checks encountered while
16527 substituting default template arguments. */
16529 static int
16530 type_unification_real (tree tparms,
16531 tree targs,
16532 tree xparms,
16533 const tree *xargs,
16534 unsigned int xnargs,
16535 int subr,
16536 unification_kind_t strict,
16537 int flags,
16538 vec<deferred_access_check, va_gc> **checks,
16539 bool explain_p)
16541 tree parm, arg;
16542 int i;
16543 int ntparms = TREE_VEC_LENGTH (tparms);
16544 int saw_undeduced = 0;
16545 tree parms;
16546 const tree *args;
16547 unsigned int nargs;
16548 unsigned int ia;
16550 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16551 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16552 gcc_assert (ntparms > 0);
16554 /* Reset the number of non-defaulted template arguments contained
16555 in TARGS. */
16556 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16558 again:
16559 parms = xparms;
16560 args = xargs;
16561 nargs = xnargs;
16563 ia = 0;
16564 while (parms && parms != void_list_node
16565 && ia < nargs)
16567 parm = TREE_VALUE (parms);
16569 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16570 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16571 /* For a function parameter pack that occurs at the end of the
16572 parameter-declaration-list, the type A of each remaining
16573 argument of the call is compared with the type P of the
16574 declarator-id of the function parameter pack. */
16575 break;
16577 parms = TREE_CHAIN (parms);
16579 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16580 /* For a function parameter pack that does not occur at the
16581 end of the parameter-declaration-list, the type of the
16582 parameter pack is a non-deduced context. */
16583 continue;
16585 arg = args[ia];
16586 ++ia;
16588 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16589 flags, explain_p))
16590 return 1;
16593 if (parms
16594 && parms != void_list_node
16595 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16597 /* Unify the remaining arguments with the pack expansion type. */
16598 tree argvec;
16599 tree parmvec = make_tree_vec (1);
16601 /* Allocate a TREE_VEC and copy in all of the arguments */
16602 argvec = make_tree_vec (nargs - ia);
16603 for (i = 0; ia < nargs; ++ia, ++i)
16604 TREE_VEC_ELT (argvec, i) = args[ia];
16606 /* Copy the parameter into parmvec. */
16607 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16608 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16609 /*subr=*/subr, explain_p))
16610 return 1;
16612 /* Advance to the end of the list of parameters. */
16613 parms = TREE_CHAIN (parms);
16616 /* Fail if we've reached the end of the parm list, and more args
16617 are present, and the parm list isn't variadic. */
16618 if (ia < nargs && parms == void_list_node)
16619 return unify_too_many_arguments (explain_p, nargs, ia);
16620 /* Fail if parms are left and they don't have default values. */
16621 if (parms && parms != void_list_node
16622 && TREE_PURPOSE (parms) == NULL_TREE)
16624 unsigned int count = nargs;
16625 tree p = parms;
16626 while (p && p != void_list_node)
16628 count++;
16629 p = TREE_CHAIN (p);
16631 return unify_too_few_arguments (explain_p, ia, count);
16634 if (!subr)
16636 tsubst_flags_t complain = (explain_p
16637 ? tf_warning_or_error
16638 : tf_none);
16640 for (i = 0; i < ntparms; i++)
16642 tree targ = TREE_VEC_ELT (targs, i);
16643 tree tparm = TREE_VEC_ELT (tparms, i);
16645 /* Clear the "incomplete" flags on all argument packs now so that
16646 substituting them into later default arguments works. */
16647 if (targ && ARGUMENT_PACK_P (targ))
16649 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16650 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16653 if (targ || tparm == error_mark_node)
16654 continue;
16655 tparm = TREE_VALUE (tparm);
16657 /* If this is an undeduced nontype parameter that depends on
16658 a type parameter, try another pass; its type may have been
16659 deduced from a later argument than the one from which
16660 this parameter can be deduced. */
16661 if (TREE_CODE (tparm) == PARM_DECL
16662 && uses_template_parms (TREE_TYPE (tparm))
16663 && !saw_undeduced++)
16664 goto again;
16666 /* Core issue #226 (C++0x) [temp.deduct]:
16668 If a template argument has not been deduced, its
16669 default template argument, if any, is used.
16671 When we are in C++98 mode, TREE_PURPOSE will either
16672 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16673 to explicitly check cxx_dialect here. */
16674 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16676 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16677 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16678 reopen_deferring_access_checks (*checks);
16679 location_t save_loc = input_location;
16680 if (DECL_P (parm))
16681 input_location = DECL_SOURCE_LOCATION (parm);
16682 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16683 arg = convert_template_argument (parm, arg, targs, complain,
16684 i, NULL_TREE);
16685 input_location = save_loc;
16686 *checks = get_deferred_access_checks ();
16687 pop_deferring_access_checks ();
16688 if (arg == error_mark_node)
16689 return 1;
16690 else
16692 TREE_VEC_ELT (targs, i) = arg;
16693 /* The position of the first default template argument,
16694 is also the number of non-defaulted arguments in TARGS.
16695 Record that. */
16696 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16697 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16698 continue;
16702 /* If the type parameter is a parameter pack, then it will
16703 be deduced to an empty parameter pack. */
16704 if (template_parameter_pack_p (tparm))
16706 tree arg;
16708 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16710 arg = make_node (NONTYPE_ARGUMENT_PACK);
16711 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16712 TREE_CONSTANT (arg) = 1;
16714 else
16715 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16717 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16719 TREE_VEC_ELT (targs, i) = arg;
16720 continue;
16723 return unify_parameter_deduction_failure (explain_p, tparm);
16726 #ifdef ENABLE_CHECKING
16727 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16728 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16729 #endif
16731 return unify_success (explain_p);
16734 /* Subroutine of type_unification_real. Args are like the variables
16735 at the call site. ARG is an overloaded function (or template-id);
16736 we try deducing template args from each of the overloads, and if
16737 only one succeeds, we go with that. Modifies TARGS and returns
16738 true on success. */
16740 static bool
16741 resolve_overloaded_unification (tree tparms,
16742 tree targs,
16743 tree parm,
16744 tree arg,
16745 unification_kind_t strict,
16746 int sub_strict,
16747 bool explain_p)
16749 tree tempargs = copy_node (targs);
16750 int good = 0;
16751 tree goodfn = NULL_TREE;
16752 bool addr_p;
16754 if (TREE_CODE (arg) == ADDR_EXPR)
16756 arg = TREE_OPERAND (arg, 0);
16757 addr_p = true;
16759 else
16760 addr_p = false;
16762 if (TREE_CODE (arg) == COMPONENT_REF)
16763 /* Handle `&x' where `x' is some static or non-static member
16764 function name. */
16765 arg = TREE_OPERAND (arg, 1);
16767 if (TREE_CODE (arg) == OFFSET_REF)
16768 arg = TREE_OPERAND (arg, 1);
16770 /* Strip baselink information. */
16771 if (BASELINK_P (arg))
16772 arg = BASELINK_FUNCTIONS (arg);
16774 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16776 /* If we got some explicit template args, we need to plug them into
16777 the affected templates before we try to unify, in case the
16778 explicit args will completely resolve the templates in question. */
16780 int ok = 0;
16781 tree expl_subargs = TREE_OPERAND (arg, 1);
16782 arg = TREE_OPERAND (arg, 0);
16784 for (; arg; arg = OVL_NEXT (arg))
16786 tree fn = OVL_CURRENT (arg);
16787 tree subargs, elem;
16789 if (TREE_CODE (fn) != TEMPLATE_DECL)
16790 continue;
16792 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16793 expl_subargs, NULL_TREE, tf_none,
16794 /*require_all_args=*/true,
16795 /*use_default_args=*/true);
16796 if (subargs != error_mark_node
16797 && !any_dependent_template_arguments_p (subargs))
16799 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16800 if (try_one_overload (tparms, targs, tempargs, parm,
16801 elem, strict, sub_strict, addr_p, explain_p)
16802 && (!goodfn || !same_type_p (goodfn, elem)))
16804 goodfn = elem;
16805 ++good;
16808 else if (subargs)
16809 ++ok;
16811 /* If no templates (or more than one) are fully resolved by the
16812 explicit arguments, this template-id is a non-deduced context; it
16813 could still be OK if we deduce all template arguments for the
16814 enclosing call through other arguments. */
16815 if (good != 1)
16816 good = ok;
16818 else if (TREE_CODE (arg) != OVERLOAD
16819 && TREE_CODE (arg) != FUNCTION_DECL)
16820 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16821 -- but the deduction does not succeed because the expression is
16822 not just the function on its own. */
16823 return false;
16824 else
16825 for (; arg; arg = OVL_NEXT (arg))
16826 if (try_one_overload (tparms, targs, tempargs, parm,
16827 TREE_TYPE (OVL_CURRENT (arg)),
16828 strict, sub_strict, addr_p, explain_p)
16829 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16831 goodfn = OVL_CURRENT (arg);
16832 ++good;
16835 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16836 to function or pointer to member function argument if the set of
16837 overloaded functions does not contain function templates and at most
16838 one of a set of overloaded functions provides a unique match.
16840 So if we found multiple possibilities, we return success but don't
16841 deduce anything. */
16843 if (good == 1)
16845 int i = TREE_VEC_LENGTH (targs);
16846 for (; i--; )
16847 if (TREE_VEC_ELT (tempargs, i))
16848 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16850 if (good)
16851 return true;
16853 return false;
16856 /* Core DR 115: In contexts where deduction is done and fails, or in
16857 contexts where deduction is not done, if a template argument list is
16858 specified and it, along with any default template arguments, identifies
16859 a single function template specialization, then the template-id is an
16860 lvalue for the function template specialization. */
16862 tree
16863 resolve_nondeduced_context (tree orig_expr)
16865 tree expr, offset, baselink;
16866 bool addr;
16868 if (!type_unknown_p (orig_expr))
16869 return orig_expr;
16871 expr = orig_expr;
16872 addr = false;
16873 offset = NULL_TREE;
16874 baselink = NULL_TREE;
16876 if (TREE_CODE (expr) == ADDR_EXPR)
16878 expr = TREE_OPERAND (expr, 0);
16879 addr = true;
16881 if (TREE_CODE (expr) == OFFSET_REF)
16883 offset = expr;
16884 expr = TREE_OPERAND (expr, 1);
16886 if (BASELINK_P (expr))
16888 baselink = expr;
16889 expr = BASELINK_FUNCTIONS (expr);
16892 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16894 int good = 0;
16895 tree goodfn = NULL_TREE;
16897 /* If we got some explicit template args, we need to plug them into
16898 the affected templates before we try to unify, in case the
16899 explicit args will completely resolve the templates in question. */
16901 tree expl_subargs = TREE_OPERAND (expr, 1);
16902 tree arg = TREE_OPERAND (expr, 0);
16903 tree badfn = NULL_TREE;
16904 tree badargs = NULL_TREE;
16906 for (; arg; arg = OVL_NEXT (arg))
16908 tree fn = OVL_CURRENT (arg);
16909 tree subargs, elem;
16911 if (TREE_CODE (fn) != TEMPLATE_DECL)
16912 continue;
16914 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16915 expl_subargs, NULL_TREE, tf_none,
16916 /*require_all_args=*/true,
16917 /*use_default_args=*/true);
16918 if (subargs != error_mark_node
16919 && !any_dependent_template_arguments_p (subargs))
16921 elem = instantiate_template (fn, subargs, tf_none);
16922 if (elem == error_mark_node)
16924 badfn = fn;
16925 badargs = subargs;
16927 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16929 goodfn = elem;
16930 ++good;
16934 if (good == 1)
16936 mark_used (goodfn);
16937 expr = goodfn;
16938 if (baselink)
16939 expr = build_baselink (BASELINK_BINFO (baselink),
16940 BASELINK_ACCESS_BINFO (baselink),
16941 expr, BASELINK_OPTYPE (baselink));
16942 if (offset)
16944 tree base
16945 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16946 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16948 if (addr)
16949 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16950 return expr;
16952 else if (good == 0 && badargs)
16953 /* There were no good options and at least one bad one, so let the
16954 user know what the problem is. */
16955 instantiate_template (badfn, badargs, tf_warning_or_error);
16957 return orig_expr;
16960 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16961 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16962 different overloads deduce different arguments for a given parm.
16963 ADDR_P is true if the expression for which deduction is being
16964 performed was of the form "& fn" rather than simply "fn".
16966 Returns 1 on success. */
16968 static int
16969 try_one_overload (tree tparms,
16970 tree orig_targs,
16971 tree targs,
16972 tree parm,
16973 tree arg,
16974 unification_kind_t strict,
16975 int sub_strict,
16976 bool addr_p,
16977 bool explain_p)
16979 int nargs;
16980 tree tempargs;
16981 int i;
16983 if (arg == error_mark_node)
16984 return 0;
16986 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16987 to function or pointer to member function argument if the set of
16988 overloaded functions does not contain function templates and at most
16989 one of a set of overloaded functions provides a unique match.
16991 So if this is a template, just return success. */
16993 if (uses_template_parms (arg))
16994 return 1;
16996 if (TREE_CODE (arg) == METHOD_TYPE)
16997 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16998 else if (addr_p)
16999 arg = build_pointer_type (arg);
17001 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17003 /* We don't copy orig_targs for this because if we have already deduced
17004 some template args from previous args, unify would complain when we
17005 try to deduce a template parameter for the same argument, even though
17006 there isn't really a conflict. */
17007 nargs = TREE_VEC_LENGTH (targs);
17008 tempargs = make_tree_vec (nargs);
17010 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17011 return 0;
17013 /* First make sure we didn't deduce anything that conflicts with
17014 explicitly specified args. */
17015 for (i = nargs; i--; )
17017 tree elt = TREE_VEC_ELT (tempargs, i);
17018 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17020 if (!elt)
17021 /*NOP*/;
17022 else if (uses_template_parms (elt))
17023 /* Since we're unifying against ourselves, we will fill in
17024 template args used in the function parm list with our own
17025 template parms. Discard them. */
17026 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17027 else if (oldelt && !template_args_equal (oldelt, elt))
17028 return 0;
17031 for (i = nargs; i--; )
17033 tree elt = TREE_VEC_ELT (tempargs, i);
17035 if (elt)
17036 TREE_VEC_ELT (targs, i) = elt;
17039 return 1;
17042 /* PARM is a template class (perhaps with unbound template
17043 parameters). ARG is a fully instantiated type. If ARG can be
17044 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17045 TARGS are as for unify. */
17047 static tree
17048 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17049 bool explain_p)
17051 tree copy_of_targs;
17053 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17054 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17055 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17056 return NULL_TREE;
17058 /* We need to make a new template argument vector for the call to
17059 unify. If we used TARGS, we'd clutter it up with the result of
17060 the attempted unification, even if this class didn't work out.
17061 We also don't want to commit ourselves to all the unifications
17062 we've already done, since unification is supposed to be done on
17063 an argument-by-argument basis. In other words, consider the
17064 following pathological case:
17066 template <int I, int J, int K>
17067 struct S {};
17069 template <int I, int J>
17070 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17072 template <int I, int J, int K>
17073 void f(S<I, J, K>, S<I, I, I>);
17075 void g() {
17076 S<0, 0, 0> s0;
17077 S<0, 1, 2> s2;
17079 f(s0, s2);
17082 Now, by the time we consider the unification involving `s2', we
17083 already know that we must have `f<0, 0, 0>'. But, even though
17084 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17085 because there are two ways to unify base classes of S<0, 1, 2>
17086 with S<I, I, I>. If we kept the already deduced knowledge, we
17087 would reject the possibility I=1. */
17088 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17090 /* If unification failed, we're done. */
17091 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17092 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17093 return NULL_TREE;
17095 return arg;
17098 /* Given a template type PARM and a class type ARG, find the unique
17099 base type in ARG that is an instance of PARM. We do not examine
17100 ARG itself; only its base-classes. If there is not exactly one
17101 appropriate base class, return NULL_TREE. PARM may be the type of
17102 a partial specialization, as well as a plain template type. Used
17103 by unify. */
17105 static enum template_base_result
17106 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17107 bool explain_p, tree *result)
17109 tree rval = NULL_TREE;
17110 tree binfo;
17112 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17114 binfo = TYPE_BINFO (complete_type (arg));
17115 if (!binfo)
17117 /* The type could not be completed. */
17118 *result = NULL_TREE;
17119 return tbr_incomplete_type;
17122 /* Walk in inheritance graph order. The search order is not
17123 important, and this avoids multiple walks of virtual bases. */
17124 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17126 tree r = try_class_unification (tparms, targs, parm,
17127 BINFO_TYPE (binfo), explain_p);
17129 if (r)
17131 /* If there is more than one satisfactory baseclass, then:
17133 [temp.deduct.call]
17135 If they yield more than one possible deduced A, the type
17136 deduction fails.
17138 applies. */
17139 if (rval && !same_type_p (r, rval))
17141 *result = NULL_TREE;
17142 return tbr_ambiguous_baseclass;
17145 rval = r;
17149 *result = rval;
17150 return tbr_success;
17153 /* Returns the level of DECL, which declares a template parameter. */
17155 static int
17156 template_decl_level (tree decl)
17158 switch (TREE_CODE (decl))
17160 case TYPE_DECL:
17161 case TEMPLATE_DECL:
17162 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17164 case PARM_DECL:
17165 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17167 default:
17168 gcc_unreachable ();
17170 return 0;
17173 /* Decide whether ARG can be unified with PARM, considering only the
17174 cv-qualifiers of each type, given STRICT as documented for unify.
17175 Returns nonzero iff the unification is OK on that basis. */
17177 static int
17178 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17180 int arg_quals = cp_type_quals (arg);
17181 int parm_quals = cp_type_quals (parm);
17183 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17184 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17186 /* Although a CVR qualifier is ignored when being applied to a
17187 substituted template parameter ([8.3.2]/1 for example), that
17188 does not allow us to unify "const T" with "int&" because both
17189 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17190 It is ok when we're allowing additional CV qualifiers
17191 at the outer level [14.8.2.1]/3,1st bullet. */
17192 if ((TREE_CODE (arg) == REFERENCE_TYPE
17193 || TREE_CODE (arg) == FUNCTION_TYPE
17194 || TREE_CODE (arg) == METHOD_TYPE)
17195 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17196 return 0;
17198 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17199 && (parm_quals & TYPE_QUAL_RESTRICT))
17200 return 0;
17203 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17204 && (arg_quals & parm_quals) != parm_quals)
17205 return 0;
17207 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17208 && (parm_quals & arg_quals) != arg_quals)
17209 return 0;
17211 return 1;
17214 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17215 void
17216 template_parm_level_and_index (tree parm, int* level, int* index)
17218 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17219 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17220 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17222 *index = TEMPLATE_TYPE_IDX (parm);
17223 *level = TEMPLATE_TYPE_LEVEL (parm);
17225 else
17227 *index = TEMPLATE_PARM_IDX (parm);
17228 *level = TEMPLATE_PARM_LEVEL (parm);
17232 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17233 do { \
17234 if (unify (TP, TA, P, A, S, EP)) \
17235 return 1; \
17236 } while (0);
17238 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17239 expansion at the end of PACKED_PARMS. Returns 0 if the type
17240 deduction succeeds, 1 otherwise. STRICT is the same as in
17241 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17242 call argument list. We'll need to adjust the arguments to make them
17243 types. SUBR tells us if this is from a recursive call to
17244 type_unification_real, or for comparing two template argument
17245 lists. */
17247 static int
17248 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17249 tree packed_args, unification_kind_t strict,
17250 bool subr, bool explain_p)
17252 tree parm
17253 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17254 tree pattern = PACK_EXPANSION_PATTERN (parm);
17255 tree pack, packs = NULL_TREE;
17256 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17257 int len = TREE_VEC_LENGTH (packed_args);
17259 /* Determine the parameter packs we will be deducing from the
17260 pattern, and record their current deductions. */
17261 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17262 pack; pack = TREE_CHAIN (pack))
17264 tree parm_pack = TREE_VALUE (pack);
17265 int idx, level;
17267 /* Determine the index and level of this parameter pack. */
17268 template_parm_level_and_index (parm_pack, &level, &idx);
17270 /* Keep track of the parameter packs and their corresponding
17271 argument packs. */
17272 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17273 TREE_TYPE (packs) = make_tree_vec (len - start);
17276 /* Loop through all of the arguments that have not yet been
17277 unified and unify each with the pattern. */
17278 for (i = start; i < len; i++)
17280 tree parm;
17281 bool any_explicit = false;
17282 tree arg = TREE_VEC_ELT (packed_args, i);
17284 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17285 or the element of its argument pack at the current index if
17286 this argument was explicitly specified. */
17287 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17289 int idx, level;
17290 tree arg, pargs;
17291 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17293 arg = NULL_TREE;
17294 if (TREE_VALUE (pack)
17295 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17296 && (i - start < TREE_VEC_LENGTH (pargs)))
17298 any_explicit = true;
17299 arg = TREE_VEC_ELT (pargs, i - start);
17301 TMPL_ARG (targs, level, idx) = arg;
17304 /* If we had explicit template arguments, substitute them into the
17305 pattern before deduction. */
17306 if (any_explicit)
17308 /* Some arguments might still be unspecified or dependent. */
17309 bool dependent;
17310 ++processing_template_decl;
17311 dependent = any_dependent_template_arguments_p (targs);
17312 if (!dependent)
17313 --processing_template_decl;
17314 parm = tsubst (pattern, targs,
17315 explain_p ? tf_warning_or_error : tf_none,
17316 NULL_TREE);
17317 if (dependent)
17318 --processing_template_decl;
17319 if (parm == error_mark_node)
17320 return 1;
17322 else
17323 parm = pattern;
17325 /* Unify the pattern with the current argument. */
17326 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17327 LOOKUP_IMPLICIT, explain_p))
17328 return 1;
17330 /* For each parameter pack, collect the deduced value. */
17331 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17333 int idx, level;
17334 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17336 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17337 TMPL_ARG (targs, level, idx);
17341 /* Verify that the results of unification with the parameter packs
17342 produce results consistent with what we've seen before, and make
17343 the deduced argument packs available. */
17344 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17346 tree old_pack = TREE_VALUE (pack);
17347 tree new_args = TREE_TYPE (pack);
17348 int i, len = TREE_VEC_LENGTH (new_args);
17349 int idx, level;
17350 bool nondeduced_p = false;
17352 /* By default keep the original deduced argument pack.
17353 If necessary, more specific code is going to update the
17354 resulting deduced argument later down in this function. */
17355 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17356 TMPL_ARG (targs, level, idx) = old_pack;
17358 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17359 actually deduce anything. */
17360 for (i = 0; i < len && !nondeduced_p; ++i)
17361 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17362 nondeduced_p = true;
17363 if (nondeduced_p)
17364 continue;
17366 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17368 /* If we had fewer function args than explicit template args,
17369 just use the explicits. */
17370 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17371 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17372 if (len < explicit_len)
17373 new_args = explicit_args;
17376 if (!old_pack)
17378 tree result;
17379 /* Build the deduced *_ARGUMENT_PACK. */
17380 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17382 result = make_node (NONTYPE_ARGUMENT_PACK);
17383 TREE_TYPE (result) =
17384 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17385 TREE_CONSTANT (result) = 1;
17387 else
17388 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17390 SET_ARGUMENT_PACK_ARGS (result, new_args);
17392 /* Note the deduced argument packs for this parameter
17393 pack. */
17394 TMPL_ARG (targs, level, idx) = result;
17396 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17397 && (ARGUMENT_PACK_ARGS (old_pack)
17398 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17400 /* We only had the explicitly-provided arguments before, but
17401 now we have a complete set of arguments. */
17402 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17404 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17405 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17406 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17408 else
17410 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17411 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17413 if (!comp_template_args_with_info (old_args, new_args,
17414 &bad_old_arg, &bad_new_arg))
17415 /* Inconsistent unification of this parameter pack. */
17416 return unify_parameter_pack_inconsistent (explain_p,
17417 bad_old_arg,
17418 bad_new_arg);
17422 return unify_success (explain_p);
17425 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17426 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17427 parameters and return value are as for unify. */
17429 static int
17430 unify_array_domain (tree tparms, tree targs,
17431 tree parm_dom, tree arg_dom,
17432 bool explain_p)
17434 tree parm_max;
17435 tree arg_max;
17436 bool parm_cst;
17437 bool arg_cst;
17439 /* Our representation of array types uses "N - 1" as the
17440 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17441 not an integer constant. We cannot unify arbitrarily
17442 complex expressions, so we eliminate the MINUS_EXPRs
17443 here. */
17444 parm_max = TYPE_MAX_VALUE (parm_dom);
17445 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17446 if (!parm_cst)
17448 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17449 parm_max = TREE_OPERAND (parm_max, 0);
17451 arg_max = TYPE_MAX_VALUE (arg_dom);
17452 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17453 if (!arg_cst)
17455 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17456 trying to unify the type of a variable with the type
17457 of a template parameter. For example:
17459 template <unsigned int N>
17460 void f (char (&) [N]);
17461 int g();
17462 void h(int i) {
17463 char a[g(i)];
17464 f(a);
17467 Here, the type of the ARG will be "int [g(i)]", and
17468 may be a SAVE_EXPR, etc. */
17469 if (TREE_CODE (arg_max) != MINUS_EXPR)
17470 return unify_vla_arg (explain_p, arg_dom);
17471 arg_max = TREE_OPERAND (arg_max, 0);
17474 /* If only one of the bounds used a MINUS_EXPR, compensate
17475 by adding one to the other bound. */
17476 if (parm_cst && !arg_cst)
17477 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17478 integer_type_node,
17479 parm_max,
17480 integer_one_node);
17481 else if (arg_cst && !parm_cst)
17482 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17483 integer_type_node,
17484 arg_max,
17485 integer_one_node);
17487 return unify (tparms, targs, parm_max, arg_max,
17488 UNIFY_ALLOW_INTEGER, explain_p);
17491 /* Deduce the value of template parameters. TPARMS is the (innermost)
17492 set of template parameters to a template. TARGS is the bindings
17493 for those template parameters, as determined thus far; TARGS may
17494 include template arguments for outer levels of template parameters
17495 as well. PARM is a parameter to a template function, or a
17496 subcomponent of that parameter; ARG is the corresponding argument.
17497 This function attempts to match PARM with ARG in a manner
17498 consistent with the existing assignments in TARGS. If more values
17499 are deduced, then TARGS is updated.
17501 Returns 0 if the type deduction succeeds, 1 otherwise. The
17502 parameter STRICT is a bitwise or of the following flags:
17504 UNIFY_ALLOW_NONE:
17505 Require an exact match between PARM and ARG.
17506 UNIFY_ALLOW_MORE_CV_QUAL:
17507 Allow the deduced ARG to be more cv-qualified (by qualification
17508 conversion) than ARG.
17509 UNIFY_ALLOW_LESS_CV_QUAL:
17510 Allow the deduced ARG to be less cv-qualified than ARG.
17511 UNIFY_ALLOW_DERIVED:
17512 Allow the deduced ARG to be a template base class of ARG,
17513 or a pointer to a template base class of the type pointed to by
17514 ARG.
17515 UNIFY_ALLOW_INTEGER:
17516 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17517 case for more information.
17518 UNIFY_ALLOW_OUTER_LEVEL:
17519 This is the outermost level of a deduction. Used to determine validity
17520 of qualification conversions. A valid qualification conversion must
17521 have const qualified pointers leading up to the inner type which
17522 requires additional CV quals, except at the outer level, where const
17523 is not required [conv.qual]. It would be normal to set this flag in
17524 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17525 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17526 This is the outermost level of a deduction, and PARM can be more CV
17527 qualified at this point.
17528 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17529 This is the outermost level of a deduction, and PARM can be less CV
17530 qualified at this point. */
17532 static int
17533 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17534 bool explain_p)
17536 int idx;
17537 tree targ;
17538 tree tparm;
17539 int strict_in = strict;
17541 /* I don't think this will do the right thing with respect to types.
17542 But the only case I've seen it in so far has been array bounds, where
17543 signedness is the only information lost, and I think that will be
17544 okay. */
17545 while (TREE_CODE (parm) == NOP_EXPR)
17546 parm = TREE_OPERAND (parm, 0);
17548 if (arg == error_mark_node)
17549 return unify_invalid (explain_p);
17550 if (arg == unknown_type_node
17551 || arg == init_list_type_node)
17552 /* We can't deduce anything from this, but we might get all the
17553 template args from other function args. */
17554 return unify_success (explain_p);
17556 /* If PARM uses template parameters, then we can't bail out here,
17557 even if ARG == PARM, since we won't record unifications for the
17558 template parameters. We might need them if we're trying to
17559 figure out which of two things is more specialized. */
17560 if (arg == parm && !uses_template_parms (parm))
17561 return unify_success (explain_p);
17563 /* Handle init lists early, so the rest of the function can assume
17564 we're dealing with a type. */
17565 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17567 tree elt, elttype;
17568 unsigned i;
17569 tree orig_parm = parm;
17571 /* Replace T with std::initializer_list<T> for deduction. */
17572 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17573 && flag_deduce_init_list)
17574 parm = listify (parm);
17576 if (!is_std_init_list (parm)
17577 && TREE_CODE (parm) != ARRAY_TYPE)
17578 /* We can only deduce from an initializer list argument if the
17579 parameter is std::initializer_list or an array; otherwise this
17580 is a non-deduced context. */
17581 return unify_success (explain_p);
17583 if (TREE_CODE (parm) == ARRAY_TYPE)
17584 elttype = TREE_TYPE (parm);
17585 else
17586 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17588 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17590 int elt_strict = strict;
17592 if (elt == error_mark_node)
17593 return unify_invalid (explain_p);
17595 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17597 tree type = TREE_TYPE (elt);
17598 /* It should only be possible to get here for a call. */
17599 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17600 elt_strict |= maybe_adjust_types_for_deduction
17601 (DEDUCE_CALL, &elttype, &type, elt);
17602 elt = type;
17605 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17606 explain_p);
17609 if (TREE_CODE (parm) == ARRAY_TYPE)
17611 /* Also deduce from the length of the initializer list. */
17612 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17613 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17614 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17615 idx, explain_p);
17618 /* If the std::initializer_list<T> deduction worked, replace the
17619 deduced A with std::initializer_list<A>. */
17620 if (orig_parm != parm)
17622 idx = TEMPLATE_TYPE_IDX (orig_parm);
17623 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17624 targ = listify (targ);
17625 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17627 return unify_success (explain_p);
17630 /* Immediately reject some pairs that won't unify because of
17631 cv-qualification mismatches. */
17632 if (TREE_CODE (arg) == TREE_CODE (parm)
17633 && TYPE_P (arg)
17634 /* It is the elements of the array which hold the cv quals of an array
17635 type, and the elements might be template type parms. We'll check
17636 when we recurse. */
17637 && TREE_CODE (arg) != ARRAY_TYPE
17638 /* We check the cv-qualifiers when unifying with template type
17639 parameters below. We want to allow ARG `const T' to unify with
17640 PARM `T' for example, when computing which of two templates
17641 is more specialized, for example. */
17642 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17643 && !check_cv_quals_for_unify (strict_in, arg, parm))
17644 return unify_cv_qual_mismatch (explain_p, parm, arg);
17646 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17647 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17648 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17649 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17650 strict &= ~UNIFY_ALLOW_DERIVED;
17651 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17652 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17654 switch (TREE_CODE (parm))
17656 case TYPENAME_TYPE:
17657 case SCOPE_REF:
17658 case UNBOUND_CLASS_TEMPLATE:
17659 /* In a type which contains a nested-name-specifier, template
17660 argument values cannot be deduced for template parameters used
17661 within the nested-name-specifier. */
17662 return unify_success (explain_p);
17664 case TEMPLATE_TYPE_PARM:
17665 case TEMPLATE_TEMPLATE_PARM:
17666 case BOUND_TEMPLATE_TEMPLATE_PARM:
17667 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17668 if (tparm == error_mark_node)
17669 return unify_invalid (explain_p);
17671 if (TEMPLATE_TYPE_LEVEL (parm)
17672 != template_decl_level (tparm))
17673 /* The PARM is not one we're trying to unify. Just check
17674 to see if it matches ARG. */
17676 if (TREE_CODE (arg) == TREE_CODE (parm)
17677 && (is_auto (parm) ? is_auto (arg)
17678 : same_type_p (parm, arg)))
17679 return unify_success (explain_p);
17680 else
17681 return unify_type_mismatch (explain_p, parm, arg);
17683 idx = TEMPLATE_TYPE_IDX (parm);
17684 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17685 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17686 if (tparm == error_mark_node)
17687 return unify_invalid (explain_p);
17689 /* Check for mixed types and values. */
17690 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17691 && TREE_CODE (tparm) != TYPE_DECL)
17692 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17693 && TREE_CODE (tparm) != TEMPLATE_DECL))
17694 gcc_unreachable ();
17696 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17698 /* ARG must be constructed from a template class or a template
17699 template parameter. */
17700 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17701 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17702 return unify_template_deduction_failure (explain_p, parm, arg);
17705 tree parmvec = TYPE_TI_ARGS (parm);
17706 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17707 tree full_argvec = add_to_template_args (targs, argvec);
17708 tree parm_parms
17709 = DECL_INNERMOST_TEMPLATE_PARMS
17710 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17711 int i, len;
17712 int parm_variadic_p = 0;
17714 /* The resolution to DR150 makes clear that default
17715 arguments for an N-argument may not be used to bind T
17716 to a template template parameter with fewer than N
17717 parameters. It is not safe to permit the binding of
17718 default arguments as an extension, as that may change
17719 the meaning of a conforming program. Consider:
17721 struct Dense { static const unsigned int dim = 1; };
17723 template <template <typename> class View,
17724 typename Block>
17725 void operator+(float, View<Block> const&);
17727 template <typename Block,
17728 unsigned int Dim = Block::dim>
17729 struct Lvalue_proxy { operator float() const; };
17731 void
17732 test_1d (void) {
17733 Lvalue_proxy<Dense> p;
17734 float b;
17735 b + p;
17738 Here, if Lvalue_proxy is permitted to bind to View, then
17739 the global operator+ will be used; if they are not, the
17740 Lvalue_proxy will be converted to float. */
17741 if (coerce_template_parms (parm_parms,
17742 full_argvec,
17743 TYPE_TI_TEMPLATE (parm),
17744 (explain_p
17745 ? tf_warning_or_error
17746 : tf_none),
17747 /*require_all_args=*/true,
17748 /*use_default_args=*/false)
17749 == error_mark_node)
17750 return 1;
17752 /* Deduce arguments T, i from TT<T> or TT<i>.
17753 We check each element of PARMVEC and ARGVEC individually
17754 rather than the whole TREE_VEC since they can have
17755 different number of elements. */
17757 parmvec = expand_template_argument_pack (parmvec);
17758 argvec = expand_template_argument_pack (argvec);
17760 len = TREE_VEC_LENGTH (parmvec);
17762 /* Check if the parameters end in a pack, making them
17763 variadic. */
17764 if (len > 0
17765 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17766 parm_variadic_p = 1;
17768 for (i = 0; i < len - parm_variadic_p; ++i)
17769 /* If the template argument list of P contains a pack
17770 expansion that is not the last template argument, the
17771 entire template argument list is a non-deduced
17772 context. */
17773 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17774 return unify_success (explain_p);
17776 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17777 return unify_too_few_arguments (explain_p,
17778 TREE_VEC_LENGTH (argvec), len);
17780 for (i = 0; i < len - parm_variadic_p; ++i)
17782 RECUR_AND_CHECK_FAILURE (tparms, targs,
17783 TREE_VEC_ELT (parmvec, i),
17784 TREE_VEC_ELT (argvec, i),
17785 UNIFY_ALLOW_NONE, explain_p);
17788 if (parm_variadic_p
17789 && unify_pack_expansion (tparms, targs,
17790 parmvec, argvec,
17791 DEDUCE_EXACT,
17792 /*subr=*/true, explain_p))
17793 return 1;
17795 arg = TYPE_TI_TEMPLATE (arg);
17797 /* Fall through to deduce template name. */
17800 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17801 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17803 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17805 /* Simple cases: Value already set, does match or doesn't. */
17806 if (targ != NULL_TREE && template_args_equal (targ, arg))
17807 return unify_success (explain_p);
17808 else if (targ)
17809 return unify_inconsistency (explain_p, parm, targ, arg);
17811 else
17813 /* If PARM is `const T' and ARG is only `int', we don't have
17814 a match unless we are allowing additional qualification.
17815 If ARG is `const int' and PARM is just `T' that's OK;
17816 that binds `const int' to `T'. */
17817 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17818 arg, parm))
17819 return unify_cv_qual_mismatch (explain_p, parm, arg);
17821 /* Consider the case where ARG is `const volatile int' and
17822 PARM is `const T'. Then, T should be `volatile int'. */
17823 arg = cp_build_qualified_type_real
17824 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17825 if (arg == error_mark_node)
17826 return unify_invalid (explain_p);
17828 /* Simple cases: Value already set, does match or doesn't. */
17829 if (targ != NULL_TREE && same_type_p (targ, arg))
17830 return unify_success (explain_p);
17831 else if (targ)
17832 return unify_inconsistency (explain_p, parm, targ, arg);
17834 /* Make sure that ARG is not a variable-sized array. (Note
17835 that were talking about variable-sized arrays (like
17836 `int[n]'), rather than arrays of unknown size (like
17837 `int[]').) We'll get very confused by such a type since
17838 the bound of the array is not constant, and therefore
17839 not mangleable. Besides, such types are not allowed in
17840 ISO C++, so we can do as we please here. We do allow
17841 them for 'auto' deduction, since that isn't ABI-exposed. */
17842 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17843 return unify_vla_arg (explain_p, arg);
17845 /* Strip typedefs as in convert_template_argument. */
17846 arg = canonicalize_type_argument (arg, tf_none);
17849 /* If ARG is a parameter pack or an expansion, we cannot unify
17850 against it unless PARM is also a parameter pack. */
17851 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17852 && !template_parameter_pack_p (parm))
17853 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17855 /* If the argument deduction results is a METHOD_TYPE,
17856 then there is a problem.
17857 METHOD_TYPE doesn't map to any real C++ type the result of
17858 the deduction can not be of that type. */
17859 if (TREE_CODE (arg) == METHOD_TYPE)
17860 return unify_method_type_error (explain_p, arg);
17862 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17863 return unify_success (explain_p);
17865 case TEMPLATE_PARM_INDEX:
17866 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17867 if (tparm == error_mark_node)
17868 return unify_invalid (explain_p);
17870 if (TEMPLATE_PARM_LEVEL (parm)
17871 != template_decl_level (tparm))
17873 /* The PARM is not one we're trying to unify. Just check
17874 to see if it matches ARG. */
17875 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17876 && cp_tree_equal (parm, arg));
17877 if (result)
17878 unify_expression_unequal (explain_p, parm, arg);
17879 return result;
17882 idx = TEMPLATE_PARM_IDX (parm);
17883 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17885 if (targ)
17887 int x = !cp_tree_equal (targ, arg);
17888 if (x)
17889 unify_inconsistency (explain_p, parm, targ, arg);
17890 return x;
17893 /* [temp.deduct.type] If, in the declaration of a function template
17894 with a non-type template-parameter, the non-type
17895 template-parameter is used in an expression in the function
17896 parameter-list and, if the corresponding template-argument is
17897 deduced, the template-argument type shall match the type of the
17898 template-parameter exactly, except that a template-argument
17899 deduced from an array bound may be of any integral type.
17900 The non-type parameter might use already deduced type parameters. */
17901 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17902 if (!TREE_TYPE (arg))
17903 /* Template-parameter dependent expression. Just accept it for now.
17904 It will later be processed in convert_template_argument. */
17906 else if (same_type_p (TREE_TYPE (arg), tparm))
17907 /* OK */;
17908 else if ((strict & UNIFY_ALLOW_INTEGER)
17909 && CP_INTEGRAL_TYPE_P (tparm))
17910 /* Convert the ARG to the type of PARM; the deduced non-type
17911 template argument must exactly match the types of the
17912 corresponding parameter. */
17913 arg = fold (build_nop (tparm, arg));
17914 else if (uses_template_parms (tparm))
17915 /* We haven't deduced the type of this parameter yet. Try again
17916 later. */
17917 return unify_success (explain_p);
17918 else
17919 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17921 /* If ARG is a parameter pack or an expansion, we cannot unify
17922 against it unless PARM is also a parameter pack. */
17923 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17924 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17925 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17927 arg = strip_typedefs_expr (arg);
17928 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17929 return unify_success (explain_p);
17931 case PTRMEM_CST:
17933 /* A pointer-to-member constant can be unified only with
17934 another constant. */
17935 if (TREE_CODE (arg) != PTRMEM_CST)
17936 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17938 /* Just unify the class member. It would be useless (and possibly
17939 wrong, depending on the strict flags) to unify also
17940 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17941 arg refer to the same variable, even if through different
17942 classes. For instance:
17944 struct A { int x; };
17945 struct B : A { };
17947 Unification of &A::x and &B::x must succeed. */
17948 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17949 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17952 case POINTER_TYPE:
17954 if (!TYPE_PTR_P (arg))
17955 return unify_type_mismatch (explain_p, parm, arg);
17957 /* [temp.deduct.call]
17959 A can be another pointer or pointer to member type that can
17960 be converted to the deduced A via a qualification
17961 conversion (_conv.qual_).
17963 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17964 This will allow for additional cv-qualification of the
17965 pointed-to types if appropriate. */
17967 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17968 /* The derived-to-base conversion only persists through one
17969 level of pointers. */
17970 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17972 return unify (tparms, targs, TREE_TYPE (parm),
17973 TREE_TYPE (arg), strict, explain_p);
17976 case REFERENCE_TYPE:
17977 if (TREE_CODE (arg) != REFERENCE_TYPE)
17978 return unify_type_mismatch (explain_p, parm, arg);
17979 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17980 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17982 case ARRAY_TYPE:
17983 if (TREE_CODE (arg) != ARRAY_TYPE)
17984 return unify_type_mismatch (explain_p, parm, arg);
17985 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17986 != (TYPE_DOMAIN (arg) == NULL_TREE))
17987 return unify_type_mismatch (explain_p, parm, arg);
17988 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17989 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17990 if (TYPE_DOMAIN (parm) != NULL_TREE)
17991 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17992 TYPE_DOMAIN (arg), explain_p);
17993 return unify_success (explain_p);
17995 case REAL_TYPE:
17996 case COMPLEX_TYPE:
17997 case VECTOR_TYPE:
17998 case INTEGER_TYPE:
17999 case BOOLEAN_TYPE:
18000 case ENUMERAL_TYPE:
18001 case VOID_TYPE:
18002 case NULLPTR_TYPE:
18003 if (TREE_CODE (arg) != TREE_CODE (parm))
18004 return unify_type_mismatch (explain_p, parm, arg);
18006 /* We have already checked cv-qualification at the top of the
18007 function. */
18008 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18009 return unify_type_mismatch (explain_p, parm, arg);
18011 /* As far as unification is concerned, this wins. Later checks
18012 will invalidate it if necessary. */
18013 return unify_success (explain_p);
18015 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18016 /* Type INTEGER_CST can come from ordinary constant template args. */
18017 case INTEGER_CST:
18018 while (TREE_CODE (arg) == NOP_EXPR)
18019 arg = TREE_OPERAND (arg, 0);
18021 if (TREE_CODE (arg) != INTEGER_CST)
18022 return unify_template_argument_mismatch (explain_p, parm, arg);
18023 return (tree_int_cst_equal (parm, arg)
18024 ? unify_success (explain_p)
18025 : unify_template_argument_mismatch (explain_p, parm, arg));
18027 case TREE_VEC:
18029 int i, len, argslen;
18030 int parm_variadic_p = 0;
18032 if (TREE_CODE (arg) != TREE_VEC)
18033 return unify_template_argument_mismatch (explain_p, parm, arg);
18035 len = TREE_VEC_LENGTH (parm);
18036 argslen = TREE_VEC_LENGTH (arg);
18038 /* Check for pack expansions in the parameters. */
18039 for (i = 0; i < len; ++i)
18041 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18043 if (i == len - 1)
18044 /* We can unify against something with a trailing
18045 parameter pack. */
18046 parm_variadic_p = 1;
18047 else
18048 /* [temp.deduct.type]/9: If the template argument list of
18049 P contains a pack expansion that is not the last
18050 template argument, the entire template argument list
18051 is a non-deduced context. */
18052 return unify_success (explain_p);
18056 /* If we don't have enough arguments to satisfy the parameters
18057 (not counting the pack expression at the end), or we have
18058 too many arguments for a parameter list that doesn't end in
18059 a pack expression, we can't unify. */
18060 if (parm_variadic_p
18061 ? argslen < len - parm_variadic_p
18062 : argslen != len)
18063 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18065 /* Unify all of the parameters that precede the (optional)
18066 pack expression. */
18067 for (i = 0; i < len - parm_variadic_p; ++i)
18069 RECUR_AND_CHECK_FAILURE (tparms, targs,
18070 TREE_VEC_ELT (parm, i),
18071 TREE_VEC_ELT (arg, i),
18072 UNIFY_ALLOW_NONE, explain_p);
18074 if (parm_variadic_p)
18075 return unify_pack_expansion (tparms, targs, parm, arg,
18076 DEDUCE_EXACT,
18077 /*subr=*/true, explain_p);
18078 return unify_success (explain_p);
18081 case RECORD_TYPE:
18082 case UNION_TYPE:
18083 if (TREE_CODE (arg) != TREE_CODE (parm))
18084 return unify_type_mismatch (explain_p, parm, arg);
18086 if (TYPE_PTRMEMFUNC_P (parm))
18088 if (!TYPE_PTRMEMFUNC_P (arg))
18089 return unify_type_mismatch (explain_p, parm, arg);
18091 return unify (tparms, targs,
18092 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18093 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18094 strict, explain_p);
18097 if (CLASSTYPE_TEMPLATE_INFO (parm))
18099 tree t = NULL_TREE;
18101 if (strict_in & UNIFY_ALLOW_DERIVED)
18103 /* First, we try to unify the PARM and ARG directly. */
18104 t = try_class_unification (tparms, targs,
18105 parm, arg, explain_p);
18107 if (!t)
18109 /* Fallback to the special case allowed in
18110 [temp.deduct.call]:
18112 If P is a class, and P has the form
18113 template-id, then A can be a derived class of
18114 the deduced A. Likewise, if P is a pointer to
18115 a class of the form template-id, A can be a
18116 pointer to a derived class pointed to by the
18117 deduced A. */
18118 enum template_base_result r;
18119 r = get_template_base (tparms, targs, parm, arg,
18120 explain_p, &t);
18122 if (!t)
18123 return unify_no_common_base (explain_p, r, parm, arg);
18126 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18127 && (CLASSTYPE_TI_TEMPLATE (parm)
18128 == CLASSTYPE_TI_TEMPLATE (arg)))
18129 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18130 Then, we should unify `int' and `U'. */
18131 t = arg;
18132 else
18133 /* There's no chance of unification succeeding. */
18134 return unify_type_mismatch (explain_p, parm, arg);
18136 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18137 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18139 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18140 return unify_type_mismatch (explain_p, parm, arg);
18141 return unify_success (explain_p);
18143 case METHOD_TYPE:
18144 case FUNCTION_TYPE:
18146 unsigned int nargs;
18147 tree *args;
18148 tree a;
18149 unsigned int i;
18151 if (TREE_CODE (arg) != TREE_CODE (parm))
18152 return unify_type_mismatch (explain_p, parm, arg);
18154 /* CV qualifications for methods can never be deduced, they must
18155 match exactly. We need to check them explicitly here,
18156 because type_unification_real treats them as any other
18157 cv-qualified parameter. */
18158 if (TREE_CODE (parm) == METHOD_TYPE
18159 && (!check_cv_quals_for_unify
18160 (UNIFY_ALLOW_NONE,
18161 class_of_this_parm (arg),
18162 class_of_this_parm (parm))))
18163 return unify_cv_qual_mismatch (explain_p, parm, arg);
18165 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18166 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18168 nargs = list_length (TYPE_ARG_TYPES (arg));
18169 args = XALLOCAVEC (tree, nargs);
18170 for (a = TYPE_ARG_TYPES (arg), i = 0;
18171 a != NULL_TREE && a != void_list_node;
18172 a = TREE_CHAIN (a), ++i)
18173 args[i] = TREE_VALUE (a);
18174 nargs = i;
18176 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18177 args, nargs, 1, DEDUCE_EXACT,
18178 LOOKUP_NORMAL, NULL, explain_p);
18181 case OFFSET_TYPE:
18182 /* Unify a pointer to member with a pointer to member function, which
18183 deduces the type of the member as a function type. */
18184 if (TYPE_PTRMEMFUNC_P (arg))
18186 /* Check top-level cv qualifiers */
18187 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18188 return unify_cv_qual_mismatch (explain_p, parm, arg);
18190 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18191 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18192 UNIFY_ALLOW_NONE, explain_p);
18194 /* Determine the type of the function we are unifying against. */
18195 tree fntype = static_fn_type (arg);
18197 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18200 if (TREE_CODE (arg) != OFFSET_TYPE)
18201 return unify_type_mismatch (explain_p, parm, arg);
18202 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18203 TYPE_OFFSET_BASETYPE (arg),
18204 UNIFY_ALLOW_NONE, explain_p);
18205 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18206 strict, explain_p);
18208 case CONST_DECL:
18209 if (DECL_TEMPLATE_PARM_P (parm))
18210 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18211 if (arg != integral_constant_value (parm))
18212 return unify_template_argument_mismatch (explain_p, parm, arg);
18213 return unify_success (explain_p);
18215 case FIELD_DECL:
18216 case TEMPLATE_DECL:
18217 /* Matched cases are handled by the ARG == PARM test above. */
18218 return unify_template_argument_mismatch (explain_p, parm, arg);
18220 case VAR_DECL:
18221 /* A non-type template parameter that is a variable should be a
18222 an integral constant, in which case, it whould have been
18223 folded into its (constant) value. So we should not be getting
18224 a variable here. */
18225 gcc_unreachable ();
18227 case TYPE_ARGUMENT_PACK:
18228 case NONTYPE_ARGUMENT_PACK:
18229 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18230 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18232 case TYPEOF_TYPE:
18233 case DECLTYPE_TYPE:
18234 case UNDERLYING_TYPE:
18235 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18236 or UNDERLYING_TYPE nodes. */
18237 return unify_success (explain_p);
18239 case ERROR_MARK:
18240 /* Unification fails if we hit an error node. */
18241 return unify_invalid (explain_p);
18243 default:
18244 /* An unresolved overload is a nondeduced context. */
18245 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18246 return unify_success (explain_p);
18247 gcc_assert (EXPR_P (parm));
18249 /* We must be looking at an expression. This can happen with
18250 something like:
18252 template <int I>
18253 void foo(S<I>, S<I + 2>);
18255 This is a "nondeduced context":
18257 [deduct.type]
18259 The nondeduced contexts are:
18261 --A type that is a template-id in which one or more of
18262 the template-arguments is an expression that references
18263 a template-parameter.
18265 In these cases, we assume deduction succeeded, but don't
18266 actually infer any unifications. */
18268 if (!uses_template_parms (parm)
18269 && !template_args_equal (parm, arg))
18270 return unify_expression_unequal (explain_p, parm, arg);
18271 else
18272 return unify_success (explain_p);
18275 #undef RECUR_AND_CHECK_FAILURE
18277 /* Note that DECL can be defined in this translation unit, if
18278 required. */
18280 static void
18281 mark_definable (tree decl)
18283 tree clone;
18284 DECL_NOT_REALLY_EXTERN (decl) = 1;
18285 FOR_EACH_CLONE (clone, decl)
18286 DECL_NOT_REALLY_EXTERN (clone) = 1;
18289 /* Called if RESULT is explicitly instantiated, or is a member of an
18290 explicitly instantiated class. */
18292 void
18293 mark_decl_instantiated (tree result, int extern_p)
18295 SET_DECL_EXPLICIT_INSTANTIATION (result);
18297 /* If this entity has already been written out, it's too late to
18298 make any modifications. */
18299 if (TREE_ASM_WRITTEN (result))
18300 return;
18302 /* For anonymous namespace we don't need to do anything. */
18303 if (decl_anon_ns_mem_p (result))
18305 gcc_assert (!TREE_PUBLIC (result));
18306 return;
18309 if (TREE_CODE (result) != FUNCTION_DECL)
18310 /* The TREE_PUBLIC flag for function declarations will have been
18311 set correctly by tsubst. */
18312 TREE_PUBLIC (result) = 1;
18314 /* This might have been set by an earlier implicit instantiation. */
18315 DECL_COMDAT (result) = 0;
18317 if (extern_p)
18318 DECL_NOT_REALLY_EXTERN (result) = 0;
18319 else
18321 mark_definable (result);
18322 /* Always make artificials weak. */
18323 if (DECL_ARTIFICIAL (result) && flag_weak)
18324 comdat_linkage (result);
18325 /* For WIN32 we also want to put explicit instantiations in
18326 linkonce sections. */
18327 else if (TREE_PUBLIC (result))
18328 maybe_make_one_only (result);
18331 /* If EXTERN_P, then this function will not be emitted -- unless
18332 followed by an explicit instantiation, at which point its linkage
18333 will be adjusted. If !EXTERN_P, then this function will be
18334 emitted here. In neither circumstance do we want
18335 import_export_decl to adjust the linkage. */
18336 DECL_INTERFACE_KNOWN (result) = 1;
18339 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18340 important template arguments. If any are missing, we check whether
18341 they're important by using error_mark_node for substituting into any
18342 args that were used for partial ordering (the ones between ARGS and END)
18343 and seeing if it bubbles up. */
18345 static bool
18346 check_undeduced_parms (tree targs, tree args, tree end)
18348 bool found = false;
18349 int i;
18350 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18351 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18353 found = true;
18354 TREE_VEC_ELT (targs, i) = error_mark_node;
18356 if (found)
18358 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18359 if (substed == error_mark_node)
18360 return true;
18362 return false;
18365 /* Given two function templates PAT1 and PAT2, return:
18367 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18368 -1 if PAT2 is more specialized than PAT1.
18369 0 if neither is more specialized.
18371 LEN indicates the number of parameters we should consider
18372 (defaulted parameters should not be considered).
18374 The 1998 std underspecified function template partial ordering, and
18375 DR214 addresses the issue. We take pairs of arguments, one from
18376 each of the templates, and deduce them against each other. One of
18377 the templates will be more specialized if all the *other*
18378 template's arguments deduce against its arguments and at least one
18379 of its arguments *does* *not* deduce against the other template's
18380 corresponding argument. Deduction is done as for class templates.
18381 The arguments used in deduction have reference and top level cv
18382 qualifiers removed. Iff both arguments were originally reference
18383 types *and* deduction succeeds in both directions, an lvalue reference
18384 wins against an rvalue reference and otherwise the template
18385 with the more cv-qualified argument wins for that pairing (if
18386 neither is more cv-qualified, they both are equal). Unlike regular
18387 deduction, after all the arguments have been deduced in this way,
18388 we do *not* verify the deduced template argument values can be
18389 substituted into non-deduced contexts.
18391 The logic can be a bit confusing here, because we look at deduce1 and
18392 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18393 can find template arguments for pat1 to make arg1 look like arg2, that
18394 means that arg2 is at least as specialized as arg1. */
18397 more_specialized_fn (tree pat1, tree pat2, int len)
18399 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18400 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18401 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18402 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18403 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18404 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18405 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18406 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18407 tree origs1, origs2;
18408 bool lose1 = false;
18409 bool lose2 = false;
18411 /* Remove the this parameter from non-static member functions. If
18412 one is a non-static member function and the other is not a static
18413 member function, remove the first parameter from that function
18414 also. This situation occurs for operator functions where we
18415 locate both a member function (with this pointer) and non-member
18416 operator (with explicit first operand). */
18417 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18419 len--; /* LEN is the number of significant arguments for DECL1 */
18420 args1 = TREE_CHAIN (args1);
18421 if (!DECL_STATIC_FUNCTION_P (decl2))
18422 args2 = TREE_CHAIN (args2);
18424 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18426 args2 = TREE_CHAIN (args2);
18427 if (!DECL_STATIC_FUNCTION_P (decl1))
18429 len--;
18430 args1 = TREE_CHAIN (args1);
18434 /* If only one is a conversion operator, they are unordered. */
18435 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18436 return 0;
18438 /* Consider the return type for a conversion function */
18439 if (DECL_CONV_FN_P (decl1))
18441 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18442 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18443 len++;
18446 processing_template_decl++;
18448 origs1 = args1;
18449 origs2 = args2;
18451 while (len--
18452 /* Stop when an ellipsis is seen. */
18453 && args1 != NULL_TREE && args2 != NULL_TREE)
18455 tree arg1 = TREE_VALUE (args1);
18456 tree arg2 = TREE_VALUE (args2);
18457 int deduce1, deduce2;
18458 int quals1 = -1;
18459 int quals2 = -1;
18460 int ref1 = 0;
18461 int ref2 = 0;
18463 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18464 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18466 /* When both arguments are pack expansions, we need only
18467 unify the patterns themselves. */
18468 arg1 = PACK_EXPANSION_PATTERN (arg1);
18469 arg2 = PACK_EXPANSION_PATTERN (arg2);
18471 /* This is the last comparison we need to do. */
18472 len = 0;
18475 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18477 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18478 arg1 = TREE_TYPE (arg1);
18479 quals1 = cp_type_quals (arg1);
18482 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18484 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18485 arg2 = TREE_TYPE (arg2);
18486 quals2 = cp_type_quals (arg2);
18489 arg1 = TYPE_MAIN_VARIANT (arg1);
18490 arg2 = TYPE_MAIN_VARIANT (arg2);
18492 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18494 int i, len2 = list_length (args2);
18495 tree parmvec = make_tree_vec (1);
18496 tree argvec = make_tree_vec (len2);
18497 tree ta = args2;
18499 /* Setup the parameter vector, which contains only ARG1. */
18500 TREE_VEC_ELT (parmvec, 0) = arg1;
18502 /* Setup the argument vector, which contains the remaining
18503 arguments. */
18504 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18505 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18507 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18508 argvec, DEDUCE_EXACT,
18509 /*subr=*/true, /*explain_p=*/false)
18510 == 0);
18512 /* We cannot deduce in the other direction, because ARG1 is
18513 a pack expansion but ARG2 is not. */
18514 deduce2 = 0;
18516 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18518 int i, len1 = list_length (args1);
18519 tree parmvec = make_tree_vec (1);
18520 tree argvec = make_tree_vec (len1);
18521 tree ta = args1;
18523 /* Setup the parameter vector, which contains only ARG1. */
18524 TREE_VEC_ELT (parmvec, 0) = arg2;
18526 /* Setup the argument vector, which contains the remaining
18527 arguments. */
18528 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18529 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18531 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18532 argvec, DEDUCE_EXACT,
18533 /*subr=*/true, /*explain_p=*/false)
18534 == 0);
18536 /* We cannot deduce in the other direction, because ARG2 is
18537 a pack expansion but ARG1 is not.*/
18538 deduce1 = 0;
18541 else
18543 /* The normal case, where neither argument is a pack
18544 expansion. */
18545 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18546 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18547 == 0);
18548 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18549 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18550 == 0);
18553 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18554 arg2, then arg2 is not as specialized as arg1. */
18555 if (!deduce1)
18556 lose2 = true;
18557 if (!deduce2)
18558 lose1 = true;
18560 /* "If, for a given type, deduction succeeds in both directions
18561 (i.e., the types are identical after the transformations above)
18562 and both P and A were reference types (before being replaced with
18563 the type referred to above):
18564 - if the type from the argument template was an lvalue reference and
18565 the type from the parameter template was not, the argument type is
18566 considered to be more specialized than the other; otherwise,
18567 - if the type from the argument template is more cv-qualified
18568 than the type from the parameter template (as described above),
18569 the argument type is considered to be more specialized than the other;
18570 otherwise,
18571 - neither type is more specialized than the other." */
18573 if (deduce1 && deduce2)
18575 if (ref1 && ref2 && ref1 != ref2)
18577 if (ref1 > ref2)
18578 lose1 = true;
18579 else
18580 lose2 = true;
18582 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18584 if ((quals1 & quals2) == quals2)
18585 lose2 = true;
18586 if ((quals1 & quals2) == quals1)
18587 lose1 = true;
18591 if (lose1 && lose2)
18592 /* We've failed to deduce something in either direction.
18593 These must be unordered. */
18594 break;
18596 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18597 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18598 /* We have already processed all of the arguments in our
18599 handing of the pack expansion type. */
18600 len = 0;
18602 args1 = TREE_CHAIN (args1);
18603 args2 = TREE_CHAIN (args2);
18606 /* "In most cases, all template parameters must have values in order for
18607 deduction to succeed, but for partial ordering purposes a template
18608 parameter may remain without a value provided it is not used in the
18609 types being used for partial ordering."
18611 Thus, if we are missing any of the targs1 we need to substitute into
18612 origs1, then pat2 is not as specialized as pat1. This can happen when
18613 there is a nondeduced context. */
18614 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18615 lose2 = true;
18616 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18617 lose1 = true;
18619 processing_template_decl--;
18621 /* All things being equal, if the next argument is a pack expansion
18622 for one function but not for the other, prefer the
18623 non-variadic function. FIXME this is bogus; see c++/41958. */
18624 if (lose1 == lose2
18625 && args1 && TREE_VALUE (args1)
18626 && args2 && TREE_VALUE (args2))
18628 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18629 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18632 // All things still being equal, determine if one is more constrained.
18633 if (lose1 == lose2)
18635 lose1 = !more_constrained (pat1, pat2);
18636 lose2 = !more_constrained (pat2, pat1);
18639 if (lose1 == lose2)
18640 return 0;
18641 else if (!lose1)
18642 return 1;
18643 else
18644 return -1;
18647 /* Determine which of two partial specializations of MAIN_TMPL is more
18648 specialized.
18650 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18651 to the first partial specialization. The TREE_VALUE is the
18652 innermost set of template parameters for the partial
18653 specialization. PAT2 is similar, but for the second template.
18655 Return 1 if the first partial specialization is more specialized;
18656 -1 if the second is more specialized; 0 if neither is more
18657 specialized.
18659 See [temp.class.order] for information about determining which of
18660 two templates is more specialized. */
18662 static int
18663 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18665 tree targs;
18666 int winner = 0;
18667 bool any_deductions = false;
18669 tree type1 = TREE_TYPE (pat1);
18670 tree type2 = TREE_TYPE (pat2);
18671 tree tmpl1 = get_specializing_template_decl (type1);
18672 tree tmpl2 = get_specializing_template_decl (type2);
18674 /* Just like what happens for functions, if we are ordering between
18675 different class template specializations, we may encounter dependent
18676 types in the arguments, and we need our dependency check functions
18677 to behave correctly. */
18678 ++processing_template_decl;
18679 targs = get_class_bindings (main_tmpl, tmpl1, TREE_VALUE (pat1),
18680 CLASSTYPE_TI_ARGS (type1),
18681 CLASSTYPE_TI_ARGS (type2));
18682 if (targs)
18684 --winner;
18685 any_deductions = true;
18688 targs = get_class_bindings (main_tmpl, tmpl2, TREE_VALUE (pat2),
18689 CLASSTYPE_TI_ARGS (type2),
18690 CLASSTYPE_TI_ARGS (type1));
18691 if (targs)
18693 ++winner;
18694 any_deductions = true;
18696 --processing_template_decl;
18698 /* In the case of a tie where at least one of the class templates
18699 has a parameter pack at the end, the template with the most
18700 non-packed parameters wins. */
18701 if (winner == 0
18702 && any_deductions
18703 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18704 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18706 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18707 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18708 int len1 = TREE_VEC_LENGTH (args1);
18709 int len2 = TREE_VEC_LENGTH (args2);
18711 /* We don't count the pack expansion at the end. */
18712 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18713 --len1;
18714 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18715 --len2;
18717 if (len1 > len2)
18718 return 1;
18719 else if (len1 < len2)
18720 return -1;
18723 if (more_constrained (tmpl1, tmpl2))
18724 ++winner;
18725 if (more_constrained (tmpl2, tmpl1))
18726 --winner;
18728 return winner;
18731 /* Return the template arguments that will produce the function signature
18732 DECL from the function template FN, with the explicit template
18733 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18734 also match. Return NULL_TREE if no satisfactory arguments could be
18735 found. */
18737 static tree
18738 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18740 int ntparms = DECL_NTPARMS (fn);
18741 tree targs = make_tree_vec (ntparms);
18742 tree decl_type = TREE_TYPE (decl);
18743 tree decl_arg_types;
18744 tree *args;
18745 unsigned int nargs, ix;
18746 tree arg;
18748 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18750 /* Never do unification on the 'this' parameter. */
18751 decl_arg_types = skip_artificial_parms_for (decl,
18752 TYPE_ARG_TYPES (decl_type));
18754 nargs = list_length (decl_arg_types);
18755 args = XALLOCAVEC (tree, nargs);
18756 for (arg = decl_arg_types, ix = 0;
18757 arg != NULL_TREE && arg != void_list_node;
18758 arg = TREE_CHAIN (arg), ++ix)
18759 args[ix] = TREE_VALUE (arg);
18761 if (fn_type_unification (fn, explicit_args, targs,
18762 args, ix,
18763 (check_rettype || DECL_CONV_FN_P (fn)
18764 ? TREE_TYPE (decl_type) : NULL_TREE),
18765 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18766 /*decltype*/false)
18767 == error_mark_node)
18768 return NULL_TREE;
18770 return targs;
18773 /* Return the innermost template arguments that, when applied to a partial
18774 specialization of MAIN_TMPL whose innermost template parameters are
18775 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18776 ARGS.
18778 For example, suppose we have:
18780 template <class T, class U> struct S {};
18781 template <class T> struct S<T*, int> {};
18783 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18784 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18785 int}. The resulting vector will be {double}, indicating that `T'
18786 is bound to `double'. */
18788 static tree
18789 get_class_bindings (tree main_tmpl, tree spec_tmpl, tree tparms,
18790 tree spec_args, tree args)
18792 int i, ntparms = TREE_VEC_LENGTH (tparms);
18793 tree deduced_args;
18794 tree innermost_deduced_args;
18796 innermost_deduced_args = make_tree_vec (ntparms);
18797 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18799 deduced_args = copy_node (args);
18800 SET_TMPL_ARGS_LEVEL (deduced_args,
18801 TMPL_ARGS_DEPTH (deduced_args),
18802 innermost_deduced_args);
18804 else
18805 deduced_args = innermost_deduced_args;
18807 if (unify (tparms, deduced_args,
18808 INNERMOST_TEMPLATE_ARGS (spec_args),
18809 INNERMOST_TEMPLATE_ARGS (args),
18810 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18811 return NULL_TREE;
18813 for (i = 0; i < ntparms; ++i)
18814 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18815 return NULL_TREE;
18817 /* Verify that nondeduced template arguments agree with the type
18818 obtained from argument deduction.
18820 For example:
18822 struct A { typedef int X; };
18823 template <class T, class U> struct C {};
18824 template <class T> struct C<T, typename T::X> {};
18826 Then with the instantiation `C<A, int>', we can deduce that
18827 `T' is `A' but unify () does not check whether `typename T::X'
18828 is `int'. */
18829 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18830 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18831 spec_args, main_tmpl,
18832 tf_none, false, false);
18833 if (spec_args == error_mark_node
18834 /* We only need to check the innermost arguments; the other
18835 arguments will always agree. */
18836 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18837 INNERMOST_TEMPLATE_ARGS (args)))
18838 return NULL_TREE;
18840 /* Now that we have bindings for all of the template arguments,
18841 ensure that the arguments deduced for the template template
18842 parameters have compatible template parameter lists. See the use
18843 of template_template_parm_bindings_ok_p in fn_type_unification
18844 for more information. */
18845 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18846 return NULL_TREE;
18848 // Having computed and checked the binding, make sure that the
18849 // deduced arguments actually satisfy the template constraints.
18850 // If not, it is equivalent to having failed to compute the binding.
18851 if (!check_template_constraints (spec_tmpl, deduced_args))
18852 return NULL_TREE;
18854 return deduced_args;
18857 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18858 Return the TREE_LIST node with the most specialized template, if
18859 any. If there is no most specialized template, the error_mark_node
18860 is returned.
18862 Note that this function does not look at, or modify, the
18863 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18864 returned is one of the elements of INSTANTIATIONS, callers may
18865 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18866 and retrieve it from the value returned. */
18868 tree
18869 most_specialized_instantiation (tree templates)
18871 tree fn, champ;
18873 ++processing_template_decl;
18875 champ = templates;
18876 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18878 int fate = 0;
18880 if (get_bindings (TREE_VALUE (champ),
18881 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18882 NULL_TREE, /*check_ret=*/true))
18883 fate--;
18885 if (get_bindings (TREE_VALUE (fn),
18886 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18887 NULL_TREE, /*check_ret=*/true))
18888 fate++;
18890 if (fate == -1)
18891 champ = fn;
18892 else if (!fate)
18894 /* Equally specialized, move to next function. If there
18895 is no next function, nothing's most specialized. */
18896 fn = TREE_CHAIN (fn);
18897 champ = fn;
18898 if (!fn)
18899 break;
18903 if (champ)
18904 /* Now verify that champ is better than everything earlier in the
18905 instantiation list. */
18906 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18907 if (get_bindings (TREE_VALUE (champ),
18908 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18909 NULL_TREE, /*check_ret=*/true)
18910 || !get_bindings (TREE_VALUE (fn),
18911 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18912 NULL_TREE, /*check_ret=*/true))
18914 champ = NULL_TREE;
18915 break;
18918 processing_template_decl--;
18920 if (!champ)
18921 return error_mark_node;
18923 return champ;
18926 /* If DECL is a specialization of some template, return the most
18927 general such template. Otherwise, returns NULL_TREE.
18929 For example, given:
18931 template <class T> struct S { template <class U> void f(U); };
18933 if TMPL is `template <class U> void S<int>::f(U)' this will return
18934 the full template. This function will not trace past partial
18935 specializations, however. For example, given in addition:
18937 template <class T> struct S<T*> { template <class U> void f(U); };
18939 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18940 `template <class T> template <class U> S<T*>::f(U)'. */
18942 tree
18943 most_general_template (tree decl)
18945 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18946 an immediate specialization. */
18947 if (TREE_CODE (decl) == FUNCTION_DECL)
18949 if (DECL_TEMPLATE_INFO (decl)) {
18950 decl = DECL_TI_TEMPLATE (decl);
18952 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18953 template friend. */
18954 if (TREE_CODE (decl) != TEMPLATE_DECL)
18955 return NULL_TREE;
18956 } else
18957 return NULL_TREE;
18960 /* Look for more and more general templates. */
18961 while (DECL_TEMPLATE_INFO (decl))
18963 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18964 (See cp-tree.h for details.) */
18965 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18966 break;
18968 if (CLASS_TYPE_P (TREE_TYPE (decl))
18969 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18970 break;
18972 /* Stop if we run into an explicitly specialized class template. */
18973 if (!DECL_NAMESPACE_SCOPE_P (decl)
18974 && DECL_CONTEXT (decl)
18975 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18976 break;
18978 decl = DECL_TI_TEMPLATE (decl);
18981 return decl;
18984 /* Return the most specialized of the class template partial
18985 specializations of TMPL which can produce TYPE, a specialization of
18986 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18987 a _TYPE node corresponding to the partial specialization, while the
18988 TREE_PURPOSE is the set of template arguments that must be
18989 substituted into the TREE_TYPE in order to generate TYPE.
18991 If the choice of partial specialization is ambiguous, a diagnostic
18992 is issued, and the error_mark_node is returned. If there are no
18993 partial specializations of TMPL matching TYPE, then NULL_TREE is
18994 returned. */
18996 static tree
18997 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18999 tree list = NULL_TREE;
19000 tree t;
19001 tree champ;
19002 int fate;
19003 bool ambiguous_p;
19004 tree args;
19005 tree outer_args = NULL_TREE;
19007 tmpl = most_general_template (tmpl);
19008 args = CLASSTYPE_TI_ARGS (type);
19010 /* For determining which partial specialization to use, only the
19011 innermost args are interesting. */
19012 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19014 outer_args = strip_innermost_template_args (args, 1);
19015 args = INNERMOST_TEMPLATE_ARGS (args);
19018 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
19020 tree partial_spec_args;
19021 tree spec_args;
19022 tree spec_tmpl = TREE_VALUE (t);
19023 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19025 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
19027 ++processing_template_decl;
19029 if (outer_args)
19031 /* Discard the outer levels of args, and then substitute in the
19032 template args from the enclosing class. */
19033 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19034 partial_spec_args = tsubst_template_args
19035 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19037 /* And the same for the partial specialization TEMPLATE_DECL. */
19038 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19041 partial_spec_args =
19042 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19043 add_to_template_args (outer_args,
19044 partial_spec_args),
19045 tmpl, tf_none,
19046 /*require_all_args=*/true,
19047 /*use_default_args=*/true);
19049 --processing_template_decl;
19051 if (partial_spec_args == error_mark_node)
19052 return error_mark_node;
19053 if (spec_tmpl == error_mark_node)
19054 return error_mark_node;
19056 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19057 spec_args = get_class_bindings (tmpl, spec_tmpl, parms,
19058 partial_spec_args,
19059 args);
19060 if (spec_args)
19062 if (outer_args)
19063 spec_args = add_to_template_args (outer_args, spec_args);
19064 list = tree_cons (spec_args, orig_parms, list);
19065 TREE_TYPE (list) = TREE_TYPE (t);
19069 if (! list)
19070 return NULL_TREE;
19072 ambiguous_p = false;
19073 t = list;
19074 champ = t;
19075 t = TREE_CHAIN (t);
19076 for (; t; t = TREE_CHAIN (t))
19078 fate = more_specialized_class (tmpl, champ, t);
19079 if (fate == 1)
19081 else
19083 if (fate == 0)
19085 t = TREE_CHAIN (t);
19086 if (! t)
19088 ambiguous_p = true;
19089 break;
19092 champ = t;
19096 if (!ambiguous_p)
19097 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19099 fate = more_specialized_class (tmpl, champ, t);
19100 if (fate != 1)
19102 ambiguous_p = true;
19103 break;
19107 if (ambiguous_p)
19109 const char *str;
19110 char *spaces = NULL;
19111 if (!(complain & tf_error))
19112 return error_mark_node;
19113 error ("ambiguous class template instantiation for %q#T", type);
19114 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19115 for (t = list; t; t = TREE_CHAIN (t))
19117 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
19118 spaces = spaces ? spaces : get_spaces (str);
19120 free (spaces);
19121 return error_mark_node;
19124 return champ;
19127 /* Explicitly instantiate DECL. */
19129 void
19130 do_decl_instantiation (tree decl, tree storage)
19132 tree result = NULL_TREE;
19133 int extern_p = 0;
19135 if (!decl || decl == error_mark_node)
19136 /* An error occurred, for which grokdeclarator has already issued
19137 an appropriate message. */
19138 return;
19139 else if (! DECL_LANG_SPECIFIC (decl))
19141 error ("explicit instantiation of non-template %q#D", decl);
19142 return;
19144 else if (VAR_P (decl))
19146 /* There is an asymmetry here in the way VAR_DECLs and
19147 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19148 the latter, the DECL we get back will be marked as a
19149 template instantiation, and the appropriate
19150 DECL_TEMPLATE_INFO will be set up. This does not happen for
19151 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19152 should handle VAR_DECLs as it currently handles
19153 FUNCTION_DECLs. */
19154 if (!DECL_CLASS_SCOPE_P (decl))
19156 error ("%qD is not a static data member of a class template", decl);
19157 return;
19159 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19160 if (!result || !VAR_P (result))
19162 error ("no matching template for %qD found", decl);
19163 return;
19165 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19167 error ("type %qT for explicit instantiation %qD does not match "
19168 "declared type %qT", TREE_TYPE (result), decl,
19169 TREE_TYPE (decl));
19170 return;
19173 else if (TREE_CODE (decl) != FUNCTION_DECL)
19175 error ("explicit instantiation of %q#D", decl);
19176 return;
19178 else
19179 result = decl;
19181 /* Check for various error cases. Note that if the explicit
19182 instantiation is valid the RESULT will currently be marked as an
19183 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19184 until we get here. */
19186 if (DECL_TEMPLATE_SPECIALIZATION (result))
19188 /* DR 259 [temp.spec].
19190 Both an explicit instantiation and a declaration of an explicit
19191 specialization shall not appear in a program unless the explicit
19192 instantiation follows a declaration of the explicit specialization.
19194 For a given set of template parameters, if an explicit
19195 instantiation of a template appears after a declaration of an
19196 explicit specialization for that template, the explicit
19197 instantiation has no effect. */
19198 return;
19200 else if (DECL_EXPLICIT_INSTANTIATION (result))
19202 /* [temp.spec]
19204 No program shall explicitly instantiate any template more
19205 than once.
19207 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19208 the first instantiation was `extern' and the second is not,
19209 and EXTERN_P for the opposite case. */
19210 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19211 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19212 /* If an "extern" explicit instantiation follows an ordinary
19213 explicit instantiation, the template is instantiated. */
19214 if (extern_p)
19215 return;
19217 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19219 error ("no matching template for %qD found", result);
19220 return;
19222 else if (!DECL_TEMPLATE_INFO (result))
19224 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19225 return;
19228 if (storage == NULL_TREE)
19230 else if (storage == ridpointers[(int) RID_EXTERN])
19232 if (!in_system_header && (cxx_dialect == cxx98))
19233 pedwarn (input_location, OPT_Wpedantic,
19234 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19235 "instantiations");
19236 extern_p = 1;
19238 else
19239 error ("storage class %qD applied to template instantiation", storage);
19241 check_explicit_instantiation_namespace (result);
19242 mark_decl_instantiated (result, extern_p);
19243 if (! extern_p)
19244 instantiate_decl (result, /*defer_ok=*/1,
19245 /*expl_inst_class_mem_p=*/false);
19248 static void
19249 mark_class_instantiated (tree t, int extern_p)
19251 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19252 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19253 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19254 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19255 if (! extern_p)
19257 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19258 rest_of_type_compilation (t, 1);
19262 /* Called from do_type_instantiation through binding_table_foreach to
19263 do recursive instantiation for the type bound in ENTRY. */
19264 static void
19265 bt_instantiate_type_proc (binding_entry entry, void *data)
19267 tree storage = *(tree *) data;
19269 if (MAYBE_CLASS_TYPE_P (entry->type)
19270 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19271 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19274 /* Called from do_type_instantiation to instantiate a member
19275 (a member function or a static member variable) of an
19276 explicitly instantiated class template. */
19277 static void
19278 instantiate_class_member (tree decl, int extern_p)
19280 mark_decl_instantiated (decl, extern_p);
19281 if (! extern_p)
19282 instantiate_decl (decl, /*defer_ok=*/1,
19283 /*expl_inst_class_mem_p=*/true);
19286 /* Perform an explicit instantiation of template class T. STORAGE, if
19287 non-null, is the RID for extern, inline or static. COMPLAIN is
19288 nonzero if this is called from the parser, zero if called recursively,
19289 since the standard is unclear (as detailed below). */
19291 void
19292 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19294 int extern_p = 0;
19295 int nomem_p = 0;
19296 int static_p = 0;
19297 int previous_instantiation_extern_p = 0;
19299 if (TREE_CODE (t) == TYPE_DECL)
19300 t = TREE_TYPE (t);
19302 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19304 tree tmpl =
19305 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19306 if (tmpl)
19307 error ("explicit instantiation of non-class template %qD", tmpl);
19308 else
19309 error ("explicit instantiation of non-template type %qT", t);
19310 return;
19313 complete_type (t);
19315 if (!COMPLETE_TYPE_P (t))
19317 if (complain & tf_error)
19318 error ("explicit instantiation of %q#T before definition of template",
19320 return;
19323 if (storage != NULL_TREE)
19325 if (!in_system_header)
19327 if (storage == ridpointers[(int) RID_EXTERN])
19329 if (cxx_dialect == cxx98)
19330 pedwarn (input_location, OPT_Wpedantic,
19331 "ISO C++ 1998 forbids the use of %<extern%> on "
19332 "explicit instantiations");
19334 else
19335 pedwarn (input_location, OPT_Wpedantic,
19336 "ISO C++ forbids the use of %qE"
19337 " on explicit instantiations", storage);
19340 if (storage == ridpointers[(int) RID_INLINE])
19341 nomem_p = 1;
19342 else if (storage == ridpointers[(int) RID_EXTERN])
19343 extern_p = 1;
19344 else if (storage == ridpointers[(int) RID_STATIC])
19345 static_p = 1;
19346 else
19348 error ("storage class %qD applied to template instantiation",
19349 storage);
19350 extern_p = 0;
19354 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19356 /* DR 259 [temp.spec].
19358 Both an explicit instantiation and a declaration of an explicit
19359 specialization shall not appear in a program unless the explicit
19360 instantiation follows a declaration of the explicit specialization.
19362 For a given set of template parameters, if an explicit
19363 instantiation of a template appears after a declaration of an
19364 explicit specialization for that template, the explicit
19365 instantiation has no effect. */
19366 return;
19368 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19370 /* [temp.spec]
19372 No program shall explicitly instantiate any template more
19373 than once.
19375 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19376 instantiation was `extern'. If EXTERN_P then the second is.
19377 These cases are OK. */
19378 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19380 if (!previous_instantiation_extern_p && !extern_p
19381 && (complain & tf_error))
19382 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19384 /* If we've already instantiated the template, just return now. */
19385 if (!CLASSTYPE_INTERFACE_ONLY (t))
19386 return;
19389 check_explicit_instantiation_namespace (TYPE_NAME (t));
19390 mark_class_instantiated (t, extern_p);
19392 if (nomem_p)
19393 return;
19396 tree tmp;
19398 /* In contrast to implicit instantiation, where only the
19399 declarations, and not the definitions, of members are
19400 instantiated, we have here:
19402 [temp.explicit]
19404 The explicit instantiation of a class template specialization
19405 implies the instantiation of all of its members not
19406 previously explicitly specialized in the translation unit
19407 containing the explicit instantiation.
19409 Of course, we can't instantiate member template classes, since
19410 we don't have any arguments for them. Note that the standard
19411 is unclear on whether the instantiation of the members are
19412 *explicit* instantiations or not. However, the most natural
19413 interpretation is that it should be an explicit instantiation. */
19415 if (! static_p)
19416 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19417 if (TREE_CODE (tmp) == FUNCTION_DECL
19418 && DECL_TEMPLATE_INSTANTIATION (tmp))
19419 instantiate_class_member (tmp, extern_p);
19421 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19422 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19423 instantiate_class_member (tmp, extern_p);
19425 if (CLASSTYPE_NESTED_UTDS (t))
19426 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19427 bt_instantiate_type_proc, &storage);
19431 /* Given a function DECL, which is a specialization of TMPL, modify
19432 DECL to be a re-instantiation of TMPL with the same template
19433 arguments. TMPL should be the template into which tsubst'ing
19434 should occur for DECL, not the most general template.
19436 One reason for doing this is a scenario like this:
19438 template <class T>
19439 void f(const T&, int i);
19441 void g() { f(3, 7); }
19443 template <class T>
19444 void f(const T& t, const int i) { }
19446 Note that when the template is first instantiated, with
19447 instantiate_template, the resulting DECL will have no name for the
19448 first parameter, and the wrong type for the second. So, when we go
19449 to instantiate the DECL, we regenerate it. */
19451 static void
19452 regenerate_decl_from_template (tree decl, tree tmpl)
19454 /* The arguments used to instantiate DECL, from the most general
19455 template. */
19456 tree args;
19457 tree code_pattern;
19459 args = DECL_TI_ARGS (decl);
19460 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19462 /* Make sure that we can see identifiers, and compute access
19463 correctly. */
19464 push_access_scope (decl);
19466 if (TREE_CODE (decl) == FUNCTION_DECL)
19468 tree decl_parm;
19469 tree pattern_parm;
19470 tree specs;
19471 int args_depth;
19472 int parms_depth;
19474 args_depth = TMPL_ARGS_DEPTH (args);
19475 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19476 if (args_depth > parms_depth)
19477 args = get_innermost_template_args (args, parms_depth);
19479 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19480 args, tf_error, NULL_TREE,
19481 /*defer_ok*/false);
19482 if (specs && specs != error_mark_node)
19483 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19484 specs);
19486 /* Merge parameter declarations. */
19487 decl_parm = skip_artificial_parms_for (decl,
19488 DECL_ARGUMENTS (decl));
19489 pattern_parm
19490 = skip_artificial_parms_for (code_pattern,
19491 DECL_ARGUMENTS (code_pattern));
19492 while (decl_parm && !DECL_PACK_P (pattern_parm))
19494 tree parm_type;
19495 tree attributes;
19497 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19498 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19499 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19500 NULL_TREE);
19501 parm_type = type_decays_to (parm_type);
19502 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19503 TREE_TYPE (decl_parm) = parm_type;
19504 attributes = DECL_ATTRIBUTES (pattern_parm);
19505 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19507 DECL_ATTRIBUTES (decl_parm) = attributes;
19508 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19510 decl_parm = DECL_CHAIN (decl_parm);
19511 pattern_parm = DECL_CHAIN (pattern_parm);
19513 /* Merge any parameters that match with the function parameter
19514 pack. */
19515 if (pattern_parm && DECL_PACK_P (pattern_parm))
19517 int i, len;
19518 tree expanded_types;
19519 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19520 the parameters in this function parameter pack. */
19521 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19522 args, tf_error, NULL_TREE);
19523 len = TREE_VEC_LENGTH (expanded_types);
19524 for (i = 0; i < len; i++)
19526 tree parm_type;
19527 tree attributes;
19529 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19530 /* Rename the parameter to include the index. */
19531 DECL_NAME (decl_parm) =
19532 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19533 parm_type = TREE_VEC_ELT (expanded_types, i);
19534 parm_type = type_decays_to (parm_type);
19535 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19536 TREE_TYPE (decl_parm) = parm_type;
19537 attributes = DECL_ATTRIBUTES (pattern_parm);
19538 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19540 DECL_ATTRIBUTES (decl_parm) = attributes;
19541 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19543 decl_parm = DECL_CHAIN (decl_parm);
19546 /* Merge additional specifiers from the CODE_PATTERN. */
19547 if (DECL_DECLARED_INLINE_P (code_pattern)
19548 && !DECL_DECLARED_INLINE_P (decl))
19549 DECL_DECLARED_INLINE_P (decl) = 1;
19551 else if (VAR_P (decl))
19553 DECL_INITIAL (decl) =
19554 tsubst_expr (DECL_INITIAL (code_pattern), args,
19555 tf_error, DECL_TI_TEMPLATE (decl),
19556 /*integral_constant_expression_p=*/false);
19557 if (VAR_HAD_UNKNOWN_BOUND (decl))
19558 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19559 tf_error, DECL_TI_TEMPLATE (decl));
19561 else
19562 gcc_unreachable ();
19564 pop_access_scope (decl);
19567 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19568 substituted to get DECL. */
19570 tree
19571 template_for_substitution (tree decl)
19573 tree tmpl = DECL_TI_TEMPLATE (decl);
19575 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19576 for the instantiation. This is not always the most general
19577 template. Consider, for example:
19579 template <class T>
19580 struct S { template <class U> void f();
19581 template <> void f<int>(); };
19583 and an instantiation of S<double>::f<int>. We want TD to be the
19584 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19585 while (/* An instantiation cannot have a definition, so we need a
19586 more general template. */
19587 DECL_TEMPLATE_INSTANTIATION (tmpl)
19588 /* We must also deal with friend templates. Given:
19590 template <class T> struct S {
19591 template <class U> friend void f() {};
19594 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19595 so far as the language is concerned, but that's still
19596 where we get the pattern for the instantiation from. On
19597 other hand, if the definition comes outside the class, say:
19599 template <class T> struct S {
19600 template <class U> friend void f();
19602 template <class U> friend void f() {}
19604 we don't need to look any further. That's what the check for
19605 DECL_INITIAL is for. */
19606 || (TREE_CODE (decl) == FUNCTION_DECL
19607 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19608 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19610 /* The present template, TD, should not be a definition. If it
19611 were a definition, we should be using it! Note that we
19612 cannot restructure the loop to just keep going until we find
19613 a template with a definition, since that might go too far if
19614 a specialization was declared, but not defined. */
19615 gcc_assert (!VAR_P (decl)
19616 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19618 /* Fetch the more general template. */
19619 tmpl = DECL_TI_TEMPLATE (tmpl);
19622 return tmpl;
19625 /* Returns true if we need to instantiate this template instance even if we
19626 know we aren't going to emit it.. */
19628 bool
19629 always_instantiate_p (tree decl)
19631 /* We always instantiate inline functions so that we can inline them. An
19632 explicit instantiation declaration prohibits implicit instantiation of
19633 non-inline functions. With high levels of optimization, we would
19634 normally inline non-inline functions -- but we're not allowed to do
19635 that for "extern template" functions. Therefore, we check
19636 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19637 return ((TREE_CODE (decl) == FUNCTION_DECL
19638 && (DECL_DECLARED_INLINE_P (decl)
19639 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19640 /* And we need to instantiate static data members so that
19641 their initializers are available in integral constant
19642 expressions. */
19643 || (VAR_P (decl)
19644 && decl_maybe_constant_var_p (decl)));
19647 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19648 instantiate it now, modifying TREE_TYPE (fn). */
19650 void
19651 maybe_instantiate_noexcept (tree fn)
19653 tree fntype, spec, noex, clone;
19655 if (DECL_CLONED_FUNCTION_P (fn))
19656 fn = DECL_CLONED_FUNCTION (fn);
19657 fntype = TREE_TYPE (fn);
19658 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19660 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19661 return;
19663 noex = TREE_PURPOSE (spec);
19665 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19667 if (push_tinst_level (fn))
19669 push_access_scope (fn);
19670 push_deferring_access_checks (dk_no_deferred);
19671 input_location = DECL_SOURCE_LOCATION (fn);
19672 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19673 DEFERRED_NOEXCEPT_ARGS (noex),
19674 tf_warning_or_error, fn,
19675 /*function_p=*/false,
19676 /*integral_constant_expression_p=*/true);
19677 pop_deferring_access_checks ();
19678 pop_access_scope (fn);
19679 pop_tinst_level ();
19680 spec = build_noexcept_spec (noex, tf_warning_or_error);
19681 if (spec == error_mark_node)
19682 spec = noexcept_false_spec;
19684 else
19685 spec = noexcept_false_spec;
19687 else
19689 /* This is an implicitly declared function, so NOEX is a list of
19690 other functions to evaluate and merge. */
19691 tree elt;
19692 spec = noexcept_true_spec;
19693 for (elt = noex; elt; elt = OVL_NEXT (elt))
19695 tree fn = OVL_CURRENT (elt);
19696 tree subspec;
19697 maybe_instantiate_noexcept (fn);
19698 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19699 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19703 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19705 FOR_EACH_CLONE (clone, fn)
19707 if (TREE_TYPE (clone) == fntype)
19708 TREE_TYPE (clone) = TREE_TYPE (fn);
19709 else
19710 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19714 /* Produce the definition of D, a _DECL generated from a template. If
19715 DEFER_OK is nonzero, then we don't have to actually do the
19716 instantiation now; we just have to do it sometime. Normally it is
19717 an error if this is an explicit instantiation but D is undefined.
19718 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19719 explicitly instantiated class template. */
19721 tree
19722 instantiate_decl (tree d, int defer_ok,
19723 bool expl_inst_class_mem_p)
19725 tree tmpl = DECL_TI_TEMPLATE (d);
19726 tree gen_args;
19727 tree args;
19728 tree td;
19729 tree code_pattern;
19730 tree spec;
19731 tree gen_tmpl;
19732 bool pattern_defined;
19733 location_t saved_loc = input_location;
19734 int saved_unevaluated_operand = cp_unevaluated_operand;
19735 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19736 bool external_p;
19737 tree fn_context;
19738 bool nested;
19740 /* This function should only be used to instantiate templates for
19741 functions and static member variables. */
19742 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19744 /* Variables are never deferred; if instantiation is required, they
19745 are instantiated right away. That allows for better code in the
19746 case that an expression refers to the value of the variable --
19747 if the variable has a constant value the referring expression can
19748 take advantage of that fact. */
19749 if (VAR_P (d)
19750 || DECL_DECLARED_CONSTEXPR_P (d))
19751 defer_ok = 0;
19753 /* Don't instantiate cloned functions. Instead, instantiate the
19754 functions they cloned. */
19755 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19756 d = DECL_CLONED_FUNCTION (d);
19758 if (DECL_TEMPLATE_INSTANTIATED (d)
19759 || (TREE_CODE (d) == FUNCTION_DECL
19760 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19761 || DECL_TEMPLATE_SPECIALIZATION (d))
19762 /* D has already been instantiated or explicitly specialized, so
19763 there's nothing for us to do here.
19765 It might seem reasonable to check whether or not D is an explicit
19766 instantiation, and, if so, stop here. But when an explicit
19767 instantiation is deferred until the end of the compilation,
19768 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19769 the instantiation. */
19770 return d;
19772 /* Check to see whether we know that this template will be
19773 instantiated in some other file, as with "extern template"
19774 extension. */
19775 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19777 /* In general, we do not instantiate such templates. */
19778 if (external_p && !always_instantiate_p (d))
19779 return d;
19781 gen_tmpl = most_general_template (tmpl);
19782 gen_args = DECL_TI_ARGS (d);
19784 if (tmpl != gen_tmpl)
19785 /* We should already have the extra args. */
19786 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19787 == TMPL_ARGS_DEPTH (gen_args));
19788 /* And what's in the hash table should match D. */
19789 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19790 || spec == NULL_TREE);
19792 /* This needs to happen before any tsubsting. */
19793 if (! push_tinst_level (d))
19794 return d;
19796 timevar_push (TV_TEMPLATE_INST);
19798 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19799 for the instantiation. */
19800 td = template_for_substitution (d);
19801 code_pattern = DECL_TEMPLATE_RESULT (td);
19803 /* We should never be trying to instantiate a member of a class
19804 template or partial specialization. */
19805 gcc_assert (d != code_pattern);
19807 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19808 || DECL_TEMPLATE_SPECIALIZATION (td))
19809 /* In the case of a friend template whose definition is provided
19810 outside the class, we may have too many arguments. Drop the
19811 ones we don't need. The same is true for specializations. */
19812 args = get_innermost_template_args
19813 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19814 else
19815 args = gen_args;
19817 if (TREE_CODE (d) == FUNCTION_DECL)
19818 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19819 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19820 else
19821 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19823 /* We may be in the middle of deferred access check. Disable it now. */
19824 push_deferring_access_checks (dk_no_deferred);
19826 /* Unless an explicit instantiation directive has already determined
19827 the linkage of D, remember that a definition is available for
19828 this entity. */
19829 if (pattern_defined
19830 && !DECL_INTERFACE_KNOWN (d)
19831 && !DECL_NOT_REALLY_EXTERN (d))
19832 mark_definable (d);
19834 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19835 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19836 input_location = DECL_SOURCE_LOCATION (d);
19838 /* If D is a member of an explicitly instantiated class template,
19839 and no definition is available, treat it like an implicit
19840 instantiation. */
19841 if (!pattern_defined && expl_inst_class_mem_p
19842 && DECL_EXPLICIT_INSTANTIATION (d))
19844 /* Leave linkage flags alone on instantiations with anonymous
19845 visibility. */
19846 if (TREE_PUBLIC (d))
19848 DECL_NOT_REALLY_EXTERN (d) = 0;
19849 DECL_INTERFACE_KNOWN (d) = 0;
19851 SET_DECL_IMPLICIT_INSTANTIATION (d);
19854 if (TREE_CODE (d) == FUNCTION_DECL)
19855 maybe_instantiate_noexcept (d);
19857 /* Defer all other templates, unless we have been explicitly
19858 forbidden from doing so. */
19859 if (/* If there is no definition, we cannot instantiate the
19860 template. */
19861 ! pattern_defined
19862 /* If it's OK to postpone instantiation, do so. */
19863 || defer_ok
19864 /* If this is a static data member that will be defined
19865 elsewhere, we don't want to instantiate the entire data
19866 member, but we do want to instantiate the initializer so that
19867 we can substitute that elsewhere. */
19868 || (external_p && VAR_P (d)))
19870 /* The definition of the static data member is now required so
19871 we must substitute the initializer. */
19872 if (VAR_P (d)
19873 && !DECL_INITIAL (d)
19874 && DECL_INITIAL (code_pattern))
19876 tree ns;
19877 tree init;
19878 bool const_init = false;
19880 ns = decl_namespace_context (d);
19881 push_nested_namespace (ns);
19882 push_nested_class (DECL_CONTEXT (d));
19883 init = tsubst_expr (DECL_INITIAL (code_pattern),
19884 args,
19885 tf_warning_or_error, NULL_TREE,
19886 /*integral_constant_expression_p=*/false);
19887 /* Make sure the initializer is still constant, in case of
19888 circular dependency (template/instantiate6.C). */
19889 const_init
19890 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19891 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19892 /*asmspec_tree=*/NULL_TREE,
19893 LOOKUP_ONLYCONVERTING);
19894 pop_nested_class ();
19895 pop_nested_namespace (ns);
19898 /* We restore the source position here because it's used by
19899 add_pending_template. */
19900 input_location = saved_loc;
19902 if (at_eof && !pattern_defined
19903 && DECL_EXPLICIT_INSTANTIATION (d)
19904 && DECL_NOT_REALLY_EXTERN (d))
19905 /* [temp.explicit]
19907 The definition of a non-exported function template, a
19908 non-exported member function template, or a non-exported
19909 member function or static data member of a class template
19910 shall be present in every translation unit in which it is
19911 explicitly instantiated. */
19912 permerror (input_location, "explicit instantiation of %qD "
19913 "but no definition available", d);
19915 /* If we're in unevaluated context, we just wanted to get the
19916 constant value; this isn't an odr use, so don't queue
19917 a full instantiation. */
19918 if (cp_unevaluated_operand != 0)
19919 goto out;
19920 /* ??? Historically, we have instantiated inline functions, even
19921 when marked as "extern template". */
19922 if (!(external_p && VAR_P (d)))
19923 add_pending_template (d);
19924 goto out;
19926 /* Tell the repository that D is available in this translation unit
19927 -- and see if it is supposed to be instantiated here. */
19928 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19930 /* In a PCH file, despite the fact that the repository hasn't
19931 requested instantiation in the PCH it is still possible that
19932 an instantiation will be required in a file that includes the
19933 PCH. */
19934 if (pch_file)
19935 add_pending_template (d);
19936 /* Instantiate inline functions so that the inliner can do its
19937 job, even though we'll not be emitting a copy of this
19938 function. */
19939 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19940 goto out;
19943 fn_context = decl_function_context (d);
19944 nested = (current_function_decl != NULL_TREE);
19945 if (!fn_context)
19946 push_to_top_level ();
19947 else
19949 if (nested)
19950 push_function_context ();
19951 cp_unevaluated_operand = 0;
19952 c_inhibit_evaluation_warnings = 0;
19955 /* Mark D as instantiated so that recursive calls to
19956 instantiate_decl do not try to instantiate it again. */
19957 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19959 /* Regenerate the declaration in case the template has been modified
19960 by a subsequent redeclaration. */
19961 regenerate_decl_from_template (d, td);
19963 /* We already set the file and line above. Reset them now in case
19964 they changed as a result of calling regenerate_decl_from_template. */
19965 input_location = DECL_SOURCE_LOCATION (d);
19967 if (VAR_P (d))
19969 tree init;
19970 bool const_init = false;
19972 /* Clear out DECL_RTL; whatever was there before may not be right
19973 since we've reset the type of the declaration. */
19974 SET_DECL_RTL (d, NULL);
19975 DECL_IN_AGGR_P (d) = 0;
19977 /* The initializer is placed in DECL_INITIAL by
19978 regenerate_decl_from_template so we don't need to
19979 push/pop_access_scope again here. Pull it out so that
19980 cp_finish_decl can process it. */
19981 init = DECL_INITIAL (d);
19982 DECL_INITIAL (d) = NULL_TREE;
19983 DECL_INITIALIZED_P (d) = 0;
19985 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19986 initializer. That function will defer actual emission until
19987 we have a chance to determine linkage. */
19988 DECL_EXTERNAL (d) = 0;
19990 /* Enter the scope of D so that access-checking works correctly. */
19991 push_nested_class (DECL_CONTEXT (d));
19992 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19993 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19994 pop_nested_class ();
19996 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19997 synthesize_method (d);
19998 else if (TREE_CODE (d) == FUNCTION_DECL)
20000 struct pointer_map_t *saved_local_specializations;
20001 tree subst_decl;
20002 tree tmpl_parm;
20003 tree spec_parm;
20004 tree block = NULL_TREE;
20006 /* Save away the current list, in case we are instantiating one
20007 template from within the body of another. */
20008 saved_local_specializations = local_specializations;
20010 /* Set up the list of local specializations. */
20011 local_specializations = pointer_map_create ();
20013 /* Set up context. */
20014 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20015 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20016 block = push_stmt_list ();
20017 else
20018 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
20020 /* Some typedefs referenced from within the template code need to be
20021 access checked at template instantiation time, i.e now. These
20022 types were added to the template at parsing time. Let's get those
20023 and perform the access checks then. */
20024 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
20025 gen_args);
20027 /* Create substitution entries for the parameters. */
20028 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
20029 tmpl_parm = DECL_ARGUMENTS (subst_decl);
20030 spec_parm = DECL_ARGUMENTS (d);
20031 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
20033 register_local_specialization (spec_parm, tmpl_parm);
20034 spec_parm = skip_artificial_parms_for (d, spec_parm);
20035 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
20037 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
20039 if (!DECL_PACK_P (tmpl_parm))
20041 register_local_specialization (spec_parm, tmpl_parm);
20042 spec_parm = DECL_CHAIN (spec_parm);
20044 else
20046 /* Register the (value) argument pack as a specialization of
20047 TMPL_PARM, then move on. */
20048 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
20049 register_local_specialization (argpack, tmpl_parm);
20052 gcc_assert (!spec_parm);
20054 /* Substitute into the body of the function. */
20055 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20056 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
20057 tf_warning_or_error, tmpl);
20058 else
20060 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
20061 tf_warning_or_error, tmpl,
20062 /*integral_constant_expression_p=*/false);
20064 /* Set the current input_location to the end of the function
20065 so that finish_function knows where we are. */
20066 input_location
20067 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
20070 /* We don't need the local specializations any more. */
20071 pointer_map_destroy (local_specializations);
20072 local_specializations = saved_local_specializations;
20074 /* Finish the function. */
20075 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20076 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20077 DECL_SAVED_TREE (d) = pop_stmt_list (block);
20078 else
20080 d = finish_function (0);
20081 expand_or_defer_fn (d);
20084 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20085 cp_check_omp_declare_reduction (d);
20088 /* We're not deferring instantiation any more. */
20089 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20091 if (!fn_context)
20092 pop_from_top_level ();
20093 else if (nested)
20094 pop_function_context ();
20096 out:
20097 input_location = saved_loc;
20098 cp_unevaluated_operand = saved_unevaluated_operand;
20099 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20100 pop_deferring_access_checks ();
20101 pop_tinst_level ();
20103 timevar_pop (TV_TEMPLATE_INST);
20105 return d;
20108 /* Run through the list of templates that we wish we could
20109 instantiate, and instantiate any we can. RETRIES is the
20110 number of times we retry pending template instantiation. */
20112 void
20113 instantiate_pending_templates (int retries)
20115 int reconsider;
20116 location_t saved_loc = input_location;
20118 /* Instantiating templates may trigger vtable generation. This in turn
20119 may require further template instantiations. We place a limit here
20120 to avoid infinite loop. */
20121 if (pending_templates && retries >= max_tinst_depth)
20123 tree decl = pending_templates->tinst->decl;
20125 error ("template instantiation depth exceeds maximum of %d"
20126 " instantiating %q+D, possibly from virtual table generation"
20127 " (use -ftemplate-depth= to increase the maximum)",
20128 max_tinst_depth, decl);
20129 if (TREE_CODE (decl) == FUNCTION_DECL)
20130 /* Pretend that we defined it. */
20131 DECL_INITIAL (decl) = error_mark_node;
20132 return;
20137 struct pending_template **t = &pending_templates;
20138 struct pending_template *last = NULL;
20139 reconsider = 0;
20140 while (*t)
20142 tree instantiation = reopen_tinst_level ((*t)->tinst);
20143 bool complete = false;
20145 if (TYPE_P (instantiation))
20147 tree fn;
20149 if (!COMPLETE_TYPE_P (instantiation))
20151 instantiate_class_template (instantiation);
20152 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20153 for (fn = TYPE_METHODS (instantiation);
20155 fn = TREE_CHAIN (fn))
20156 if (! DECL_ARTIFICIAL (fn))
20157 instantiate_decl (fn,
20158 /*defer_ok=*/0,
20159 /*expl_inst_class_mem_p=*/false);
20160 if (COMPLETE_TYPE_P (instantiation))
20161 reconsider = 1;
20164 complete = COMPLETE_TYPE_P (instantiation);
20166 else
20168 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20169 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20171 instantiation
20172 = instantiate_decl (instantiation,
20173 /*defer_ok=*/0,
20174 /*expl_inst_class_mem_p=*/false);
20175 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20176 reconsider = 1;
20179 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20180 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20183 if (complete)
20184 /* If INSTANTIATION has been instantiated, then we don't
20185 need to consider it again in the future. */
20186 *t = (*t)->next;
20187 else
20189 last = *t;
20190 t = &(*t)->next;
20192 tinst_depth = 0;
20193 current_tinst_level = NULL;
20195 last_pending_template = last;
20197 while (reconsider);
20199 input_location = saved_loc;
20202 /* Substitute ARGVEC into T, which is a list of initializers for
20203 either base class or a non-static data member. The TREE_PURPOSEs
20204 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20205 instantiate_decl. */
20207 static tree
20208 tsubst_initializer_list (tree t, tree argvec)
20210 tree inits = NULL_TREE;
20212 for (; t; t = TREE_CHAIN (t))
20214 tree decl;
20215 tree init;
20216 tree expanded_bases = NULL_TREE;
20217 tree expanded_arguments = NULL_TREE;
20218 int i, len = 1;
20220 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20222 tree expr;
20223 tree arg;
20225 /* Expand the base class expansion type into separate base
20226 classes. */
20227 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20228 tf_warning_or_error,
20229 NULL_TREE);
20230 if (expanded_bases == error_mark_node)
20231 continue;
20233 /* We'll be building separate TREE_LISTs of arguments for
20234 each base. */
20235 len = TREE_VEC_LENGTH (expanded_bases);
20236 expanded_arguments = make_tree_vec (len);
20237 for (i = 0; i < len; i++)
20238 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20240 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20241 expand each argument in the TREE_VALUE of t. */
20242 expr = make_node (EXPR_PACK_EXPANSION);
20243 PACK_EXPANSION_LOCAL_P (expr) = true;
20244 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20245 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20247 if (TREE_VALUE (t) == void_type_node)
20248 /* VOID_TYPE_NODE is used to indicate
20249 value-initialization. */
20251 for (i = 0; i < len; i++)
20252 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20254 else
20256 /* Substitute parameter packs into each argument in the
20257 TREE_LIST. */
20258 in_base_initializer = 1;
20259 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20261 tree expanded_exprs;
20263 /* Expand the argument. */
20264 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20265 expanded_exprs
20266 = tsubst_pack_expansion (expr, argvec,
20267 tf_warning_or_error,
20268 NULL_TREE);
20269 if (expanded_exprs == error_mark_node)
20270 continue;
20272 /* Prepend each of the expanded expressions to the
20273 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20274 for (i = 0; i < len; i++)
20276 TREE_VEC_ELT (expanded_arguments, i) =
20277 tree_cons (NULL_TREE,
20278 TREE_VEC_ELT (expanded_exprs, i),
20279 TREE_VEC_ELT (expanded_arguments, i));
20282 in_base_initializer = 0;
20284 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20285 since we built them backwards. */
20286 for (i = 0; i < len; i++)
20288 TREE_VEC_ELT (expanded_arguments, i) =
20289 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20294 for (i = 0; i < len; ++i)
20296 if (expanded_bases)
20298 decl = TREE_VEC_ELT (expanded_bases, i);
20299 decl = expand_member_init (decl);
20300 init = TREE_VEC_ELT (expanded_arguments, i);
20302 else
20304 tree tmp;
20305 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20306 tf_warning_or_error, NULL_TREE);
20308 decl = expand_member_init (decl);
20309 if (decl && !DECL_P (decl))
20310 in_base_initializer = 1;
20312 init = TREE_VALUE (t);
20313 tmp = init;
20314 if (init != void_type_node)
20315 init = tsubst_expr (init, argvec,
20316 tf_warning_or_error, NULL_TREE,
20317 /*integral_constant_expression_p=*/false);
20318 if (init == NULL_TREE && tmp != NULL_TREE)
20319 /* If we had an initializer but it instantiated to nothing,
20320 value-initialize the object. This will only occur when
20321 the initializer was a pack expansion where the parameter
20322 packs used in that expansion were of length zero. */
20323 init = void_type_node;
20324 in_base_initializer = 0;
20327 if (decl)
20329 init = build_tree_list (decl, init);
20330 TREE_CHAIN (init) = inits;
20331 inits = init;
20335 return inits;
20338 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20340 static void
20341 set_current_access_from_decl (tree decl)
20343 if (TREE_PRIVATE (decl))
20344 current_access_specifier = access_private_node;
20345 else if (TREE_PROTECTED (decl))
20346 current_access_specifier = access_protected_node;
20347 else
20348 current_access_specifier = access_public_node;
20351 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20352 is the instantiation (which should have been created with
20353 start_enum) and ARGS are the template arguments to use. */
20355 static void
20356 tsubst_enum (tree tag, tree newtag, tree args)
20358 tree e;
20360 if (SCOPED_ENUM_P (newtag))
20361 begin_scope (sk_scoped_enum, newtag);
20363 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20365 tree value;
20366 tree decl;
20368 decl = TREE_VALUE (e);
20369 /* Note that in a template enum, the TREE_VALUE is the
20370 CONST_DECL, not the corresponding INTEGER_CST. */
20371 value = tsubst_expr (DECL_INITIAL (decl),
20372 args, tf_warning_or_error, NULL_TREE,
20373 /*integral_constant_expression_p=*/true);
20375 /* Give this enumeration constant the correct access. */
20376 set_current_access_from_decl (decl);
20378 /* Actually build the enumerator itself. */
20379 build_enumerator
20380 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20383 if (SCOPED_ENUM_P (newtag))
20384 finish_scope ();
20386 finish_enum_value_list (newtag);
20387 finish_enum (newtag);
20389 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20390 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20393 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20394 its type -- but without substituting the innermost set of template
20395 arguments. So, innermost set of template parameters will appear in
20396 the type. */
20398 tree
20399 get_mostly_instantiated_function_type (tree decl)
20401 tree fn_type;
20402 tree tmpl;
20403 tree targs;
20404 tree tparms;
20405 int parm_depth;
20407 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20408 targs = DECL_TI_ARGS (decl);
20409 tparms = DECL_TEMPLATE_PARMS (tmpl);
20410 parm_depth = TMPL_PARMS_DEPTH (tparms);
20412 /* There should be as many levels of arguments as there are levels
20413 of parameters. */
20414 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20416 fn_type = TREE_TYPE (tmpl);
20418 if (parm_depth == 1)
20419 /* No substitution is necessary. */
20421 else
20423 int i;
20424 tree partial_args;
20426 /* Replace the innermost level of the TARGS with NULL_TREEs to
20427 let tsubst know not to substitute for those parameters. */
20428 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20429 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20430 SET_TMPL_ARGS_LEVEL (partial_args, i,
20431 TMPL_ARGS_LEVEL (targs, i));
20432 SET_TMPL_ARGS_LEVEL (partial_args,
20433 TMPL_ARGS_DEPTH (targs),
20434 make_tree_vec (DECL_NTPARMS (tmpl)));
20436 /* Make sure that we can see identifiers, and compute access
20437 correctly. */
20438 push_access_scope (decl);
20440 ++processing_template_decl;
20441 /* Now, do the (partial) substitution to figure out the
20442 appropriate function type. */
20443 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20444 --processing_template_decl;
20446 /* Substitute into the template parameters to obtain the real
20447 innermost set of parameters. This step is important if the
20448 innermost set of template parameters contains value
20449 parameters whose types depend on outer template parameters. */
20450 TREE_VEC_LENGTH (partial_args)--;
20451 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20453 pop_access_scope (decl);
20456 return fn_type;
20459 /* Return truthvalue if we're processing a template different from
20460 the last one involved in diagnostics. */
20462 problematic_instantiation_changed (void)
20464 return current_tinst_level != last_error_tinst_level;
20467 /* Remember current template involved in diagnostics. */
20468 void
20469 record_last_problematic_instantiation (void)
20471 last_error_tinst_level = current_tinst_level;
20474 struct tinst_level *
20475 current_instantiation (void)
20477 return current_tinst_level;
20480 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20481 type. Return zero for ok, nonzero for disallowed. Issue error and
20482 warning messages under control of COMPLAIN. */
20484 static int
20485 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20487 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20488 return 0;
20489 else if (POINTER_TYPE_P (type))
20490 return 0;
20491 else if (TYPE_PTRMEM_P (type))
20492 return 0;
20493 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20494 return 0;
20495 else if (TREE_CODE (type) == TYPENAME_TYPE)
20496 return 0;
20497 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20498 return 0;
20499 else if (TREE_CODE (type) == NULLPTR_TYPE)
20500 return 0;
20502 if (complain & tf_error)
20504 if (type == error_mark_node)
20505 inform (input_location, "invalid template non-type parameter");
20506 else
20507 error ("%q#T is not a valid type for a template non-type parameter",
20508 type);
20510 return 1;
20513 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20514 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20516 static bool
20517 dependent_type_p_r (tree type)
20519 tree scope;
20521 /* [temp.dep.type]
20523 A type is dependent if it is:
20525 -- a template parameter. Template template parameters are types
20526 for us (since TYPE_P holds true for them) so we handle
20527 them here. */
20528 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20529 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20530 return true;
20531 /* -- a qualified-id with a nested-name-specifier which contains a
20532 class-name that names a dependent type or whose unqualified-id
20533 names a dependent type. */
20534 if (TREE_CODE (type) == TYPENAME_TYPE)
20535 return true;
20536 /* -- a cv-qualified type where the cv-unqualified type is
20537 dependent. */
20538 type = TYPE_MAIN_VARIANT (type);
20539 /* -- a compound type constructed from any dependent type. */
20540 if (TYPE_PTRMEM_P (type))
20541 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20542 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20543 (type)));
20544 else if (TYPE_PTR_P (type)
20545 || TREE_CODE (type) == REFERENCE_TYPE)
20546 return dependent_type_p (TREE_TYPE (type));
20547 else if (TREE_CODE (type) == FUNCTION_TYPE
20548 || TREE_CODE (type) == METHOD_TYPE)
20550 tree arg_type;
20552 if (dependent_type_p (TREE_TYPE (type)))
20553 return true;
20554 for (arg_type = TYPE_ARG_TYPES (type);
20555 arg_type;
20556 arg_type = TREE_CHAIN (arg_type))
20557 if (dependent_type_p (TREE_VALUE (arg_type)))
20558 return true;
20559 return false;
20561 /* -- an array type constructed from any dependent type or whose
20562 size is specified by a constant expression that is
20563 value-dependent.
20565 We checked for type- and value-dependence of the bounds in
20566 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20567 if (TREE_CODE (type) == ARRAY_TYPE)
20569 if (TYPE_DOMAIN (type)
20570 && dependent_type_p (TYPE_DOMAIN (type)))
20571 return true;
20572 return dependent_type_p (TREE_TYPE (type));
20575 /* -- a template-id in which either the template name is a template
20576 parameter ... */
20577 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20578 return true;
20579 /* ... or any of the template arguments is a dependent type or
20580 an expression that is type-dependent or value-dependent. */
20581 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20582 && (any_dependent_template_arguments_p
20583 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20584 return true;
20586 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20587 dependent; if the argument of the `typeof' expression is not
20588 type-dependent, then it should already been have resolved. */
20589 if (TREE_CODE (type) == TYPEOF_TYPE
20590 || TREE_CODE (type) == DECLTYPE_TYPE
20591 || TREE_CODE (type) == UNDERLYING_TYPE)
20592 return true;
20594 /* A template argument pack is dependent if any of its packed
20595 arguments are. */
20596 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20598 tree args = ARGUMENT_PACK_ARGS (type);
20599 int i, len = TREE_VEC_LENGTH (args);
20600 for (i = 0; i < len; ++i)
20601 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20602 return true;
20605 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20606 be template parameters. */
20607 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20608 return true;
20610 /* The standard does not specifically mention types that are local
20611 to template functions or local classes, but they should be
20612 considered dependent too. For example:
20614 template <int I> void f() {
20615 enum E { a = I };
20616 S<sizeof (E)> s;
20619 The size of `E' cannot be known until the value of `I' has been
20620 determined. Therefore, `E' must be considered dependent. */
20621 scope = TYPE_CONTEXT (type);
20622 if (scope && TYPE_P (scope))
20623 return dependent_type_p (scope);
20624 /* Don't use type_dependent_expression_p here, as it can lead
20625 to infinite recursion trying to determine whether a lambda
20626 nested in a lambda is dependent (c++/47687). */
20627 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20628 && DECL_LANG_SPECIFIC (scope)
20629 && DECL_TEMPLATE_INFO (scope)
20630 && (any_dependent_template_arguments_p
20631 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20632 return true;
20634 /* Other types are non-dependent. */
20635 return false;
20638 /* Returns TRUE if TYPE is dependent, in the sense of
20639 [temp.dep.type]. Note that a NULL type is considered dependent. */
20641 bool
20642 dependent_type_p (tree type)
20644 /* If there are no template parameters in scope, then there can't be
20645 any dependent types. */
20646 if (!processing_template_decl)
20648 /* If we are not processing a template, then nobody should be
20649 providing us with a dependent type. */
20650 gcc_assert (type);
20651 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20652 return false;
20655 /* If the type is NULL, we have not computed a type for the entity
20656 in question; in that case, the type is dependent. */
20657 if (!type)
20658 return true;
20660 /* Erroneous types can be considered non-dependent. */
20661 if (type == error_mark_node)
20662 return false;
20664 /* If we have not already computed the appropriate value for TYPE,
20665 do so now. */
20666 if (!TYPE_DEPENDENT_P_VALID (type))
20668 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20669 TYPE_DEPENDENT_P_VALID (type) = 1;
20672 return TYPE_DEPENDENT_P (type);
20675 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20676 lookup. In other words, a dependent type that is not the current
20677 instantiation. */
20679 bool
20680 dependent_scope_p (tree scope)
20682 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20683 && !currently_open_class (scope));
20686 /* T is a SCOPE_REF; return whether we need to consider it
20687 instantiation-dependent so that we can check access at instantiation
20688 time even though we know which member it resolves to. */
20690 static bool
20691 instantiation_dependent_scope_ref_p (tree t)
20693 if (DECL_P (TREE_OPERAND (t, 1))
20694 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20695 && accessible_in_template_p (TREE_OPERAND (t, 0),
20696 TREE_OPERAND (t, 1)))
20697 return false;
20698 else
20699 return true;
20702 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20703 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20704 expression. */
20706 /* Note that this predicate is not appropriate for general expressions;
20707 only constant expressions (that satisfy potential_constant_expression)
20708 can be tested for value dependence. */
20710 bool
20711 value_dependent_expression_p (tree expression)
20713 if (!processing_template_decl)
20714 return false;
20716 /* A name declared with a dependent type. */
20717 if (DECL_P (expression) && type_dependent_expression_p (expression))
20718 return true;
20720 switch (TREE_CODE (expression))
20722 case IDENTIFIER_NODE:
20723 /* A name that has not been looked up -- must be dependent. */
20724 return true;
20726 case TEMPLATE_PARM_INDEX:
20727 /* A non-type template parm. */
20728 return true;
20730 case CONST_DECL:
20731 /* A non-type template parm. */
20732 if (DECL_TEMPLATE_PARM_P (expression))
20733 return true;
20734 return value_dependent_expression_p (DECL_INITIAL (expression));
20736 case VAR_DECL:
20737 /* A constant with literal type and is initialized
20738 with an expression that is value-dependent.
20740 Note that a non-dependent parenthesized initializer will have
20741 already been replaced with its constant value, so if we see
20742 a TREE_LIST it must be dependent. */
20743 if (DECL_INITIAL (expression)
20744 && decl_constant_var_p (expression)
20745 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20746 || value_dependent_expression_p (DECL_INITIAL (expression))))
20747 return true;
20748 return false;
20750 case DYNAMIC_CAST_EXPR:
20751 case STATIC_CAST_EXPR:
20752 case CONST_CAST_EXPR:
20753 case REINTERPRET_CAST_EXPR:
20754 case CAST_EXPR:
20755 /* These expressions are value-dependent if the type to which
20756 the cast occurs is dependent or the expression being casted
20757 is value-dependent. */
20759 tree type = TREE_TYPE (expression);
20761 if (dependent_type_p (type))
20762 return true;
20764 /* A functional cast has a list of operands. */
20765 expression = TREE_OPERAND (expression, 0);
20766 if (!expression)
20768 /* If there are no operands, it must be an expression such
20769 as "int()". This should not happen for aggregate types
20770 because it would form non-constant expressions. */
20771 gcc_assert (cxx_dialect >= cxx11
20772 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20774 return false;
20777 if (TREE_CODE (expression) == TREE_LIST)
20778 return any_value_dependent_elements_p (expression);
20780 return value_dependent_expression_p (expression);
20783 case SIZEOF_EXPR:
20784 if (SIZEOF_EXPR_TYPE_P (expression))
20785 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20786 /* FALLTHRU */
20787 case ALIGNOF_EXPR:
20788 case TYPEID_EXPR:
20789 /* A `sizeof' expression is value-dependent if the operand is
20790 type-dependent or is a pack expansion. */
20791 expression = TREE_OPERAND (expression, 0);
20792 if (PACK_EXPANSION_P (expression))
20793 return true;
20794 else if (TYPE_P (expression))
20795 return dependent_type_p (expression);
20796 return instantiation_dependent_expression_p (expression);
20798 case AT_ENCODE_EXPR:
20799 /* An 'encode' expression is value-dependent if the operand is
20800 type-dependent. */
20801 expression = TREE_OPERAND (expression, 0);
20802 return dependent_type_p (expression);
20804 case NOEXCEPT_EXPR:
20805 expression = TREE_OPERAND (expression, 0);
20806 return instantiation_dependent_expression_p (expression);
20808 case SCOPE_REF:
20809 /* All instantiation-dependent expressions should also be considered
20810 value-dependent. */
20811 return instantiation_dependent_scope_ref_p (expression);
20813 case COMPONENT_REF:
20814 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20815 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20817 case NONTYPE_ARGUMENT_PACK:
20818 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20819 is value-dependent. */
20821 tree values = ARGUMENT_PACK_ARGS (expression);
20822 int i, len = TREE_VEC_LENGTH (values);
20824 for (i = 0; i < len; ++i)
20825 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20826 return true;
20828 return false;
20831 case TRAIT_EXPR:
20833 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20834 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20835 || (type2 ? dependent_type_p (type2) : false));
20838 case MODOP_EXPR:
20839 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20840 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20842 case ARRAY_REF:
20843 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20844 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20846 case ADDR_EXPR:
20848 tree op = TREE_OPERAND (expression, 0);
20849 return (value_dependent_expression_p (op)
20850 || has_value_dependent_address (op));
20853 case CALL_EXPR:
20855 tree fn = get_callee_fndecl (expression);
20856 int i, nargs;
20857 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20858 return true;
20859 nargs = call_expr_nargs (expression);
20860 for (i = 0; i < nargs; ++i)
20862 tree op = CALL_EXPR_ARG (expression, i);
20863 /* In a call to a constexpr member function, look through the
20864 implicit ADDR_EXPR on the object argument so that it doesn't
20865 cause the call to be considered value-dependent. We also
20866 look through it in potential_constant_expression. */
20867 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20868 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20869 && TREE_CODE (op) == ADDR_EXPR)
20870 op = TREE_OPERAND (op, 0);
20871 if (value_dependent_expression_p (op))
20872 return true;
20874 return false;
20877 case TEMPLATE_ID_EXPR:
20878 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20879 type-dependent. */
20880 return type_dependent_expression_p (expression);
20882 case CONSTRUCTOR:
20884 unsigned ix;
20885 tree val;
20886 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20887 if (value_dependent_expression_p (val))
20888 return true;
20889 return false;
20892 case STMT_EXPR:
20893 /* Treat a GNU statement expression as dependent to avoid crashing
20894 under fold_non_dependent_expr; it can't be constant. */
20895 return true;
20897 default:
20898 /* A constant expression is value-dependent if any subexpression is
20899 value-dependent. */
20900 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20902 case tcc_reference:
20903 case tcc_unary:
20904 case tcc_comparison:
20905 case tcc_binary:
20906 case tcc_expression:
20907 case tcc_vl_exp:
20909 int i, len = cp_tree_operand_length (expression);
20911 for (i = 0; i < len; i++)
20913 tree t = TREE_OPERAND (expression, i);
20915 /* In some cases, some of the operands may be missing.l
20916 (For example, in the case of PREDECREMENT_EXPR, the
20917 amount to increment by may be missing.) That doesn't
20918 make the expression dependent. */
20919 if (t && value_dependent_expression_p (t))
20920 return true;
20923 break;
20924 default:
20925 break;
20927 break;
20930 /* The expression is not value-dependent. */
20931 return false;
20934 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20935 [temp.dep.expr]. Note that an expression with no type is
20936 considered dependent. Other parts of the compiler arrange for an
20937 expression with type-dependent subexpressions to have no type, so
20938 this function doesn't have to be fully recursive. */
20940 bool
20941 type_dependent_expression_p (tree expression)
20943 if (!processing_template_decl)
20944 return false;
20946 if (expression == NULL_TREE || expression == error_mark_node)
20947 return false;
20949 /* An unresolved name is always dependent. */
20950 if (identifier_p (expression)
20951 || TREE_CODE (expression) == USING_DECL
20952 || TREE_CODE (expression) == PLACEHOLDER_EXPR)
20953 return true;
20955 /* Some expression forms are never type-dependent. */
20956 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20957 || TREE_CODE (expression) == SIZEOF_EXPR
20958 || TREE_CODE (expression) == ALIGNOF_EXPR
20959 || TREE_CODE (expression) == AT_ENCODE_EXPR
20960 || TREE_CODE (expression) == NOEXCEPT_EXPR
20961 || TREE_CODE (expression) == TRAIT_EXPR
20962 || TREE_CODE (expression) == TYPEID_EXPR
20963 || TREE_CODE (expression) == DELETE_EXPR
20964 || TREE_CODE (expression) == VEC_DELETE_EXPR
20965 || TREE_CODE (expression) == THROW_EXPR)
20966 return false;
20968 /* The types of these expressions depends only on the type to which
20969 the cast occurs. */
20970 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20971 || TREE_CODE (expression) == STATIC_CAST_EXPR
20972 || TREE_CODE (expression) == CONST_CAST_EXPR
20973 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20974 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20975 || TREE_CODE (expression) == CAST_EXPR)
20976 return dependent_type_p (TREE_TYPE (expression));
20978 /* The types of these expressions depends only on the type created
20979 by the expression. */
20980 if (TREE_CODE (expression) == NEW_EXPR
20981 || TREE_CODE (expression) == VEC_NEW_EXPR)
20983 /* For NEW_EXPR tree nodes created inside a template, either
20984 the object type itself or a TREE_LIST may appear as the
20985 operand 1. */
20986 tree type = TREE_OPERAND (expression, 1);
20987 if (TREE_CODE (type) == TREE_LIST)
20988 /* This is an array type. We need to check array dimensions
20989 as well. */
20990 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20991 || value_dependent_expression_p
20992 (TREE_OPERAND (TREE_VALUE (type), 1));
20993 else
20994 return dependent_type_p (type);
20997 if (TREE_CODE (expression) == SCOPE_REF)
20999 tree scope = TREE_OPERAND (expression, 0);
21000 tree name = TREE_OPERAND (expression, 1);
21002 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21003 contains an identifier associated by name lookup with one or more
21004 declarations declared with a dependent type, or...a
21005 nested-name-specifier or qualified-id that names a member of an
21006 unknown specialization. */
21007 return (type_dependent_expression_p (name)
21008 || dependent_scope_p (scope));
21011 if (TREE_CODE (expression) == FUNCTION_DECL
21012 && DECL_LANG_SPECIFIC (expression)
21013 && DECL_TEMPLATE_INFO (expression)
21014 && (any_dependent_template_arguments_p
21015 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21016 return true;
21018 if (TREE_CODE (expression) == TEMPLATE_DECL
21019 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21020 return false;
21022 if (TREE_CODE (expression) == STMT_EXPR)
21023 expression = stmt_expr_value_expr (expression);
21025 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21027 tree elt;
21028 unsigned i;
21030 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
21032 if (type_dependent_expression_p (elt))
21033 return true;
21035 return false;
21038 /* A static data member of the current instantiation with incomplete
21039 array type is type-dependent, as the definition and specializations
21040 can have different bounds. */
21041 if (VAR_P (expression)
21042 && DECL_CLASS_SCOPE_P (expression)
21043 && dependent_type_p (DECL_CONTEXT (expression))
21044 && VAR_HAD_UNKNOWN_BOUND (expression))
21045 return true;
21047 /* An array of unknown bound depending on a variadic parameter, eg:
21049 template<typename... Args>
21050 void foo (Args... args)
21052 int arr[] = { args... };
21055 template<int... vals>
21056 void bar ()
21058 int arr[] = { vals... };
21061 If the array has no length and has an initializer, it must be that
21062 we couldn't determine its length in cp_complete_array_type because
21063 it is dependent. */
21064 if (VAR_P (expression)
21065 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
21066 && !TYPE_DOMAIN (TREE_TYPE (expression))
21067 && DECL_INITIAL (expression))
21068 return true;
21070 if (TREE_TYPE (expression) == unknown_type_node)
21072 if (TREE_CODE (expression) == ADDR_EXPR)
21073 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21074 if (TREE_CODE (expression) == COMPONENT_REF
21075 || TREE_CODE (expression) == OFFSET_REF)
21077 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
21078 return true;
21079 expression = TREE_OPERAND (expression, 1);
21080 if (identifier_p (expression))
21081 return false;
21083 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21084 if (TREE_CODE (expression) == SCOPE_REF)
21085 return false;
21087 /* Always dependent, on the number of arguments if nothing else. */
21088 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21089 return true;
21091 if (BASELINK_P (expression))
21092 expression = BASELINK_FUNCTIONS (expression);
21094 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21096 if (any_dependent_template_arguments_p
21097 (TREE_OPERAND (expression, 1)))
21098 return true;
21099 expression = TREE_OPERAND (expression, 0);
21101 gcc_assert (TREE_CODE (expression) == OVERLOAD
21102 || TREE_CODE (expression) == FUNCTION_DECL);
21104 while (expression)
21106 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21107 return true;
21108 expression = OVL_NEXT (expression);
21110 return false;
21113 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21115 return (dependent_type_p (TREE_TYPE (expression)));
21118 /* walk_tree callback function for instantiation_dependent_expression_p,
21119 below. Returns non-zero if a dependent subexpression is found. */
21121 static tree
21122 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21123 void * /*data*/)
21125 if (TYPE_P (*tp))
21127 /* We don't have to worry about decltype currently because decltype
21128 of an instantiation-dependent expr is a dependent type. This
21129 might change depending on the resolution of DR 1172. */
21130 *walk_subtrees = false;
21131 return NULL_TREE;
21133 enum tree_code code = TREE_CODE (*tp);
21134 switch (code)
21136 /* Don't treat an argument list as dependent just because it has no
21137 TREE_TYPE. */
21138 case TREE_LIST:
21139 case TREE_VEC:
21140 return NULL_TREE;
21142 case VAR_DECL:
21143 case CONST_DECL:
21144 /* A constant with a dependent initializer is dependent. */
21145 if (value_dependent_expression_p (*tp))
21146 return *tp;
21147 break;
21149 case TEMPLATE_PARM_INDEX:
21150 return *tp;
21152 /* Handle expressions with type operands. */
21153 case SIZEOF_EXPR:
21154 case ALIGNOF_EXPR:
21155 case TYPEID_EXPR:
21156 case AT_ENCODE_EXPR:
21158 tree op = TREE_OPERAND (*tp, 0);
21159 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21160 op = TREE_TYPE (op);
21161 if (TYPE_P (op))
21163 if (dependent_type_p (op))
21164 return *tp;
21165 else
21167 *walk_subtrees = false;
21168 return NULL_TREE;
21171 break;
21174 case TRAIT_EXPR:
21176 bool l = dependent_type_p (TRAIT_EXPR_TYPE1 (*tp));
21177 bool r = is_binary_trait (TRAIT_EXPR_KIND (*tp))
21178 ? dependent_type_p (TRAIT_EXPR_TYPE1 (*tp)) : false;
21179 if (l || r)
21180 return *tp;
21181 *walk_subtrees = false;
21182 return NULL_TREE;
21185 case COMPONENT_REF:
21186 if (identifier_p (TREE_OPERAND (*tp, 1)))
21187 /* In a template, finish_class_member_access_expr creates a
21188 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21189 type-dependent, so that we can check access control at
21190 instantiation time (PR 42277). See also Core issue 1273. */
21191 return *tp;
21192 break;
21194 case SCOPE_REF:
21195 if (instantiation_dependent_scope_ref_p (*tp))
21196 return *tp;
21197 else
21198 break;
21200 /* Treat statement-expressions as dependent. */
21201 case BIND_EXPR:
21202 return *tp;
21204 default:
21205 break;
21208 if (type_dependent_expression_p (*tp))
21209 return *tp;
21210 else
21211 return NULL_TREE;
21214 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21215 sense defined by the ABI:
21217 "An expression is instantiation-dependent if it is type-dependent
21218 or value-dependent, or it has a subexpression that is type-dependent
21219 or value-dependent." */
21221 bool
21222 instantiation_dependent_expression_p (tree expression)
21224 tree result;
21226 if (!processing_template_decl)
21227 return false;
21229 if (expression == error_mark_node)
21230 return false;
21232 result = cp_walk_tree_without_duplicates (&expression,
21233 instantiation_dependent_r, NULL);
21234 return result != NULL_TREE;
21237 /* Like type_dependent_expression_p, but it also works while not processing
21238 a template definition, i.e. during substitution or mangling. */
21240 bool
21241 type_dependent_expression_p_push (tree expr)
21243 bool b;
21244 ++processing_template_decl;
21245 b = type_dependent_expression_p (expr);
21246 --processing_template_decl;
21247 return b;
21250 /* Returns TRUE if ARGS contains a type-dependent expression. */
21252 bool
21253 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21255 unsigned int i;
21256 tree arg;
21258 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21260 if (type_dependent_expression_p (arg))
21261 return true;
21263 return false;
21266 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21267 expressions) contains any type-dependent expressions. */
21269 bool
21270 any_type_dependent_elements_p (const_tree list)
21272 for (; list; list = TREE_CHAIN (list))
21273 if (type_dependent_expression_p (TREE_VALUE (list)))
21274 return true;
21276 return false;
21279 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21280 expressions) contains any value-dependent expressions. */
21282 bool
21283 any_value_dependent_elements_p (const_tree list)
21285 for (; list; list = TREE_CHAIN (list))
21286 if (value_dependent_expression_p (TREE_VALUE (list)))
21287 return true;
21289 return false;
21292 /* Returns TRUE if the ARG (a template argument) is dependent. */
21294 bool
21295 dependent_template_arg_p (tree arg)
21297 if (!processing_template_decl)
21298 return false;
21300 /* Assume a template argument that was wrongly written by the user
21301 is dependent. This is consistent with what
21302 any_dependent_template_arguments_p [that calls this function]
21303 does. */
21304 if (!arg || arg == error_mark_node)
21305 return true;
21307 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21308 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21310 if (TREE_CODE (arg) == TEMPLATE_DECL
21311 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21312 return dependent_template_p (arg);
21313 else if (ARGUMENT_PACK_P (arg))
21315 tree args = ARGUMENT_PACK_ARGS (arg);
21316 int i, len = TREE_VEC_LENGTH (args);
21317 for (i = 0; i < len; ++i)
21319 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21320 return true;
21323 return false;
21325 else if (TYPE_P (arg))
21326 return dependent_type_p (arg);
21327 else
21328 return (type_dependent_expression_p (arg)
21329 || value_dependent_expression_p (arg));
21332 /* Returns true if ARGS (a collection of template arguments) contains
21333 any types that require structural equality testing. */
21335 bool
21336 any_template_arguments_need_structural_equality_p (tree args)
21338 int i;
21339 int j;
21341 if (!args)
21342 return false;
21343 if (args == error_mark_node)
21344 return true;
21346 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21348 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21349 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21351 tree arg = TREE_VEC_ELT (level, j);
21352 tree packed_args = NULL_TREE;
21353 int k, len = 1;
21355 if (ARGUMENT_PACK_P (arg))
21357 /* Look inside the argument pack. */
21358 packed_args = ARGUMENT_PACK_ARGS (arg);
21359 len = TREE_VEC_LENGTH (packed_args);
21362 for (k = 0; k < len; ++k)
21364 if (packed_args)
21365 arg = TREE_VEC_ELT (packed_args, k);
21367 if (error_operand_p (arg))
21368 return true;
21369 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21370 continue;
21371 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21372 return true;
21373 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21374 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21375 return true;
21380 return false;
21383 /* Returns true if ARGS (a collection of template arguments) contains
21384 any dependent arguments. */
21386 bool
21387 any_dependent_template_arguments_p (const_tree args)
21389 int i;
21390 int j;
21392 if (!args)
21393 return false;
21394 if (args == error_mark_node)
21395 return true;
21397 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21399 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21400 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21401 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21402 return true;
21405 return false;
21408 /* Returns TRUE if the template TMPL is dependent. */
21410 bool
21411 dependent_template_p (tree tmpl)
21413 if (TREE_CODE (tmpl) == OVERLOAD)
21415 while (tmpl)
21417 if (dependent_template_p (OVL_CURRENT (tmpl)))
21418 return true;
21419 tmpl = OVL_NEXT (tmpl);
21421 return false;
21424 /* Template template parameters are dependent. */
21425 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21426 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21427 return true;
21428 /* So are names that have not been looked up. */
21429 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21430 return true;
21431 /* So are member templates of dependent classes. */
21432 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21433 return dependent_type_p (DECL_CONTEXT (tmpl));
21434 return false;
21437 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21439 bool
21440 dependent_template_id_p (tree tmpl, tree args)
21442 return (dependent_template_p (tmpl)
21443 || any_dependent_template_arguments_p (args));
21446 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21447 is dependent. */
21449 bool
21450 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21452 int i;
21454 if (!processing_template_decl)
21455 return false;
21457 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21459 tree decl = TREE_VEC_ELT (declv, i);
21460 tree init = TREE_VEC_ELT (initv, i);
21461 tree cond = TREE_VEC_ELT (condv, i);
21462 tree incr = TREE_VEC_ELT (incrv, i);
21464 if (type_dependent_expression_p (decl))
21465 return true;
21467 if (init && type_dependent_expression_p (init))
21468 return true;
21470 if (type_dependent_expression_p (cond))
21471 return true;
21473 if (COMPARISON_CLASS_P (cond)
21474 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21475 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21476 return true;
21478 if (TREE_CODE (incr) == MODOP_EXPR)
21480 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21481 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21482 return true;
21484 else if (type_dependent_expression_p (incr))
21485 return true;
21486 else if (TREE_CODE (incr) == MODIFY_EXPR)
21488 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21489 return true;
21490 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21492 tree t = TREE_OPERAND (incr, 1);
21493 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21494 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21495 return true;
21500 return false;
21503 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21504 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21505 no such TYPE can be found. Note that this function peers inside
21506 uninstantiated templates and therefore should be used only in
21507 extremely limited situations. ONLY_CURRENT_P restricts this
21508 peering to the currently open classes hierarchy (which is required
21509 when comparing types). */
21511 tree
21512 resolve_typename_type (tree type, bool only_current_p)
21514 tree scope;
21515 tree name;
21516 tree decl;
21517 int quals;
21518 tree pushed_scope;
21519 tree result;
21521 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21523 scope = TYPE_CONTEXT (type);
21524 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21525 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21526 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21527 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21528 identifier of the TYPENAME_TYPE anymore.
21529 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21530 TYPENAME_TYPE instead, we avoid messing up with a possible
21531 typedef variant case. */
21532 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21534 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21535 it first before we can figure out what NAME refers to. */
21536 if (TREE_CODE (scope) == TYPENAME_TYPE)
21538 if (TYPENAME_IS_RESOLVING_P (scope))
21539 /* Given a class template A with a dependent base with nested type C,
21540 typedef typename A::C::C C will land us here, as trying to resolve
21541 the initial A::C leads to the local C typedef, which leads back to
21542 A::C::C. So we break the recursion now. */
21543 return type;
21544 else
21545 scope = resolve_typename_type (scope, only_current_p);
21547 /* If we don't know what SCOPE refers to, then we cannot resolve the
21548 TYPENAME_TYPE. */
21549 if (TREE_CODE (scope) == TYPENAME_TYPE)
21550 return type;
21551 /* If the SCOPE is a template type parameter, we have no way of
21552 resolving the name. */
21553 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21554 return type;
21555 /* If the SCOPE is not the current instantiation, there's no reason
21556 to look inside it. */
21557 if (only_current_p && !currently_open_class (scope))
21558 return type;
21559 /* If this is a typedef, we don't want to look inside (c++/11987). */
21560 if (typedef_variant_p (type))
21561 return type;
21562 /* If SCOPE isn't the template itself, it will not have a valid
21563 TYPE_FIELDS list. */
21564 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21565 /* scope is either the template itself or a compatible instantiation
21566 like X<T>, so look up the name in the original template. */
21567 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21568 else
21569 /* scope is a partial instantiation, so we can't do the lookup or we
21570 will lose the template arguments. */
21571 return type;
21572 /* Enter the SCOPE so that name lookup will be resolved as if we
21573 were in the class definition. In particular, SCOPE will no
21574 longer be considered a dependent type. */
21575 pushed_scope = push_scope (scope);
21576 /* Look up the declaration. */
21577 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21578 tf_warning_or_error);
21580 result = NULL_TREE;
21582 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21583 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21584 if (!decl)
21585 /*nop*/;
21586 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21587 && TREE_CODE (decl) == TYPE_DECL)
21589 result = TREE_TYPE (decl);
21590 if (result == error_mark_node)
21591 result = NULL_TREE;
21593 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21594 && DECL_CLASS_TEMPLATE_P (decl))
21596 tree tmpl;
21597 tree args;
21598 /* Obtain the template and the arguments. */
21599 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21600 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21601 /* Instantiate the template. */
21602 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21603 /*entering_scope=*/0,
21604 tf_error | tf_user);
21605 if (result == error_mark_node)
21606 result = NULL_TREE;
21609 /* Leave the SCOPE. */
21610 if (pushed_scope)
21611 pop_scope (pushed_scope);
21613 /* If we failed to resolve it, return the original typename. */
21614 if (!result)
21615 return type;
21617 /* If lookup found a typename type, resolve that too. */
21618 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21620 /* Ill-formed programs can cause infinite recursion here, so we
21621 must catch that. */
21622 TYPENAME_IS_RESOLVING_P (type) = 1;
21623 result = resolve_typename_type (result, only_current_p);
21624 TYPENAME_IS_RESOLVING_P (type) = 0;
21627 /* Qualify the resulting type. */
21628 quals = cp_type_quals (type);
21629 if (quals)
21630 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21632 return result;
21635 /* EXPR is an expression which is not type-dependent. Return a proxy
21636 for EXPR that can be used to compute the types of larger
21637 expressions containing EXPR. */
21639 tree
21640 build_non_dependent_expr (tree expr)
21642 tree inner_expr;
21644 #ifdef ENABLE_CHECKING
21645 /* Try to get a constant value for all non-dependent expressions in
21646 order to expose bugs in *_dependent_expression_p and constexpr. */
21647 if (cxx_dialect >= cxx11)
21648 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21649 #endif
21651 /* Preserve OVERLOADs; the functions must be available to resolve
21652 types. */
21653 inner_expr = expr;
21654 if (TREE_CODE (inner_expr) == STMT_EXPR)
21655 inner_expr = stmt_expr_value_expr (inner_expr);
21656 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21657 inner_expr = TREE_OPERAND (inner_expr, 0);
21658 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21659 inner_expr = TREE_OPERAND (inner_expr, 1);
21660 if (is_overloaded_fn (inner_expr)
21661 || TREE_CODE (inner_expr) == OFFSET_REF)
21662 return expr;
21663 /* There is no need to return a proxy for a variable. */
21664 if (VAR_P (expr))
21665 return expr;
21666 /* Preserve string constants; conversions from string constants to
21667 "char *" are allowed, even though normally a "const char *"
21668 cannot be used to initialize a "char *". */
21669 if (TREE_CODE (expr) == STRING_CST)
21670 return expr;
21671 /* Preserve arithmetic constants, as an optimization -- there is no
21672 reason to create a new node. */
21673 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21674 return expr;
21675 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21676 There is at least one place where we want to know that a
21677 particular expression is a throw-expression: when checking a ?:
21678 expression, there are special rules if the second or third
21679 argument is a throw-expression. */
21680 if (TREE_CODE (expr) == THROW_EXPR)
21681 return expr;
21683 /* Don't wrap an initializer list, we need to be able to look inside. */
21684 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21685 return expr;
21687 /* Don't wrap a dummy object, we need to be able to test for it. */
21688 if (is_dummy_object (expr))
21689 return expr;
21691 if (TREE_CODE (expr) == COND_EXPR)
21692 return build3 (COND_EXPR,
21693 TREE_TYPE (expr),
21694 TREE_OPERAND (expr, 0),
21695 (TREE_OPERAND (expr, 1)
21696 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21697 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21698 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21699 if (TREE_CODE (expr) == COMPOUND_EXPR
21700 && !COMPOUND_EXPR_OVERLOADED (expr))
21701 return build2 (COMPOUND_EXPR,
21702 TREE_TYPE (expr),
21703 TREE_OPERAND (expr, 0),
21704 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21706 /* If the type is unknown, it can't really be non-dependent */
21707 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21709 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21710 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21713 /* ARGS is a vector of expressions as arguments to a function call.
21714 Replace the arguments with equivalent non-dependent expressions.
21715 This modifies ARGS in place. */
21717 void
21718 make_args_non_dependent (vec<tree, va_gc> *args)
21720 unsigned int ix;
21721 tree arg;
21723 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21725 tree newarg = build_non_dependent_expr (arg);
21726 if (newarg != arg)
21727 (*args)[ix] = newarg;
21731 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21732 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21733 parms. */
21735 static tree
21736 make_auto_1 (tree name)
21738 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21739 TYPE_NAME (au) = build_decl (input_location,
21740 TYPE_DECL, name, au);
21741 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21742 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21743 (0, processing_template_decl + 1, processing_template_decl + 1,
21744 TYPE_NAME (au), NULL_TREE);
21745 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21746 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21747 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21749 return au;
21752 tree
21753 make_decltype_auto (void)
21755 return make_auto_1 (get_identifier ("decltype(auto)"));
21758 tree
21759 make_auto (void)
21761 return make_auto_1 (get_identifier ("auto"));
21764 /* Given type ARG, return std::initializer_list<ARG>. */
21766 static tree
21767 listify (tree arg)
21769 tree std_init_list = namespace_binding
21770 (get_identifier ("initializer_list"), std_node);
21771 tree argvec;
21772 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21774 error ("deducing from brace-enclosed initializer list requires "
21775 "#include <initializer_list>");
21776 return error_mark_node;
21778 argvec = make_tree_vec (1);
21779 TREE_VEC_ELT (argvec, 0) = arg;
21780 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21781 NULL_TREE, 0, tf_warning_or_error);
21784 /* Replace auto in TYPE with std::initializer_list<auto>. */
21786 static tree
21787 listify_autos (tree type, tree auto_node)
21789 tree init_auto = listify (auto_node);
21790 tree argvec = make_tree_vec (1);
21791 TREE_VEC_ELT (argvec, 0) = init_auto;
21792 if (processing_template_decl)
21793 argvec = add_to_template_args (current_template_args (), argvec);
21794 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21797 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21798 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21800 tree
21801 do_auto_deduction (tree type, tree init, tree auto_node)
21803 tree targs;
21805 if (init == error_mark_node)
21806 return error_mark_node;
21808 if (type_dependent_expression_p (init))
21809 /* Defining a subset of type-dependent expressions that we can deduce
21810 from ahead of time isn't worth the trouble. */
21811 return type;
21813 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21814 with either a new invented type template parameter U or, if the
21815 initializer is a braced-init-list (8.5.4), with
21816 std::initializer_list<U>. */
21817 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21818 type = listify_autos (type, auto_node);
21820 init = resolve_nondeduced_context (init);
21822 targs = make_tree_vec (1);
21823 if (AUTO_IS_DECLTYPE (auto_node))
21825 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21826 TREE_VEC_ELT (targs, 0)
21827 = finish_decltype_type (init, id, tf_warning_or_error);
21828 if (type != auto_node)
21830 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21831 return error_mark_node;
21834 else
21836 tree parms = build_tree_list (NULL_TREE, type);
21837 tree tparms = make_tree_vec (1);
21838 int val;
21840 TREE_VEC_ELT (tparms, 0)
21841 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21842 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21843 DEDUCE_CALL, LOOKUP_NORMAL,
21844 NULL, /*explain_p=*/false);
21845 if (val > 0)
21847 if (processing_template_decl)
21848 /* Try again at instantiation time. */
21849 return type;
21850 if (type && type != error_mark_node)
21851 /* If type is error_mark_node a diagnostic must have been
21852 emitted by now. Also, having a mention to '<type error>'
21853 in the diagnostic is not really useful to the user. */
21855 if (cfun && auto_node == current_function_auto_return_pattern
21856 && LAMBDA_FUNCTION_P (current_function_decl))
21857 error ("unable to deduce lambda return type from %qE", init);
21858 else
21859 error ("unable to deduce %qT from %qE", type, init);
21861 return error_mark_node;
21865 /* If the list of declarators contains more than one declarator, the type
21866 of each declared variable is determined as described above. If the
21867 type deduced for the template parameter U is not the same in each
21868 deduction, the program is ill-formed. */
21869 if (TREE_TYPE (auto_node)
21870 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21872 if (cfun && auto_node == current_function_auto_return_pattern
21873 && LAMBDA_FUNCTION_P (current_function_decl))
21874 error ("inconsistent types %qT and %qT deduced for "
21875 "lambda return type", TREE_TYPE (auto_node),
21876 TREE_VEC_ELT (targs, 0));
21877 else
21878 error ("inconsistent deduction for %qT: %qT and then %qT",
21879 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21880 return error_mark_node;
21882 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21884 if (processing_template_decl)
21885 targs = add_to_template_args (current_template_args (), targs);
21886 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21889 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21890 result. */
21892 tree
21893 splice_late_return_type (tree type, tree late_return_type)
21895 tree argvec;
21897 if (late_return_type == NULL_TREE)
21898 return type;
21899 argvec = make_tree_vec (1);
21900 TREE_VEC_ELT (argvec, 0) = late_return_type;
21901 if (processing_template_parmlist)
21902 /* For a late-specified return type in a template type-parameter, we
21903 need to add a dummy argument level for its parmlist. */
21904 argvec = add_to_template_args
21905 (make_tree_vec (processing_template_parmlist), argvec);
21906 if (current_template_parms)
21907 argvec = add_to_template_args (current_template_args (), argvec);
21908 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21911 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21912 'decltype(auto)'. */
21914 bool
21915 is_auto (const_tree type)
21917 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21918 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21919 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21920 return true;
21921 else
21922 return false;
21925 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21926 a use of `auto'. Returns NULL_TREE otherwise. */
21928 tree
21929 type_uses_auto (tree type)
21931 return find_type_usage (type, is_auto);
21934 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21935 'decltype(auto)' or a concept. */
21937 bool
21938 is_auto_or_concept (const_tree type)
21940 return is_auto (type); // or concept
21943 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21944 a concept identifier) iff TYPE contains a use of a generic type. Returns
21945 NULL_TREE otherwise. */
21947 tree
21948 type_uses_auto_or_concept (tree type)
21950 return find_type_usage (type, is_auto_or_concept);
21954 /* For a given template T, return the vector of typedefs referenced
21955 in T for which access check is needed at T instantiation time.
21956 T is either a FUNCTION_DECL or a RECORD_TYPE.
21957 Those typedefs were added to T by the function
21958 append_type_to_template_for_access_check. */
21960 vec<qualified_typedef_usage_t, va_gc> *
21961 get_types_needing_access_check (tree t)
21963 tree ti;
21964 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21966 if (!t || t == error_mark_node)
21967 return NULL;
21969 if (!(ti = get_template_info (t)))
21970 return NULL;
21972 if (CLASS_TYPE_P (t)
21973 || TREE_CODE (t) == FUNCTION_DECL)
21975 if (!TI_TEMPLATE (ti))
21976 return NULL;
21978 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21981 return result;
21984 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21985 tied to T. That list of typedefs will be access checked at
21986 T instantiation time.
21987 T is either a FUNCTION_DECL or a RECORD_TYPE.
21988 TYPE_DECL is a TYPE_DECL node representing a typedef.
21989 SCOPE is the scope through which TYPE_DECL is accessed.
21990 LOCATION is the location of the usage point of TYPE_DECL.
21992 This function is a subroutine of
21993 append_type_to_template_for_access_check. */
21995 static void
21996 append_type_to_template_for_access_check_1 (tree t,
21997 tree type_decl,
21998 tree scope,
21999 location_t location)
22001 qualified_typedef_usage_t typedef_usage;
22002 tree ti;
22004 if (!t || t == error_mark_node)
22005 return;
22007 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
22008 || CLASS_TYPE_P (t))
22009 && type_decl
22010 && TREE_CODE (type_decl) == TYPE_DECL
22011 && scope);
22013 if (!(ti = get_template_info (t)))
22014 return;
22016 gcc_assert (TI_TEMPLATE (ti));
22018 typedef_usage.typedef_decl = type_decl;
22019 typedef_usage.context = scope;
22020 typedef_usage.locus = location;
22022 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
22025 /* Append TYPE_DECL to the template TEMPL.
22026 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22027 At TEMPL instanciation time, TYPE_DECL will be checked to see
22028 if it can be accessed through SCOPE.
22029 LOCATION is the location of the usage point of TYPE_DECL.
22031 e.g. consider the following code snippet:
22033 class C
22035 typedef int myint;
22038 template<class U> struct S
22040 C::myint mi; // <-- usage point of the typedef C::myint
22043 S<char> s;
22045 At S<char> instantiation time, we need to check the access of C::myint
22046 In other words, we need to check the access of the myint typedef through
22047 the C scope. For that purpose, this function will add the myint typedef
22048 and the scope C through which its being accessed to a list of typedefs
22049 tied to the template S. That list will be walked at template instantiation
22050 time and access check performed on each typedefs it contains.
22051 Note that this particular code snippet should yield an error because
22052 myint is private to C. */
22054 void
22055 append_type_to_template_for_access_check (tree templ,
22056 tree type_decl,
22057 tree scope,
22058 location_t location)
22060 qualified_typedef_usage_t *iter;
22061 unsigned i;
22063 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
22065 /* Make sure we don't append the type to the template twice. */
22066 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
22067 if (iter->typedef_decl == type_decl && scope == iter->context)
22068 return;
22070 append_type_to_template_for_access_check_1 (templ, type_decl,
22071 scope, location);
22074 /* Convert the generic type parameters in PARM that match the types given in the
22075 range [START_IDX, END_IDX) from the current_template_parms into generic type
22076 packs. */
22078 tree
22079 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22081 tree current = current_template_parms;
22082 int depth = TMPL_PARMS_DEPTH (current);
22083 current = INNERMOST_TEMPLATE_PARMS (current);
22084 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22086 for (int i = 0; i < start_idx; ++i)
22087 TREE_VEC_ELT (replacement, i)
22088 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22090 for (int i = start_idx; i < end_idx; ++i)
22092 /* Create a distinct parameter pack type from the current parm and add it
22093 to the replacement args to tsubst below into the generic function
22094 parameter. */
22096 tree o = TREE_TYPE (TREE_VALUE
22097 (TREE_VEC_ELT (current, i)));
22098 tree t = copy_type (o);
22099 TEMPLATE_TYPE_PARM_INDEX (t)
22100 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22101 o, 0, 0, tf_none);
22102 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22103 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22104 TYPE_MAIN_VARIANT (t) = t;
22105 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22106 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22107 TREE_VEC_ELT (replacement, i) = t;
22108 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22111 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22112 TREE_VEC_ELT (replacement, i)
22113 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22115 /* If there are more levels then build up the replacement with the outer
22116 template parms. */
22117 if (depth > 1)
22118 replacement = add_to_template_args (template_parms_to_args
22119 (TREE_CHAIN (current_template_parms)),
22120 replacement);
22122 return tsubst (parm, replacement, tf_none, NULL_TREE);
22126 /* Set up the hash tables for template instantiations. */
22128 void
22129 init_template_processing (void)
22131 decl_specializations = htab_create_ggc (37,
22132 hash_specialization,
22133 eq_specializations,
22134 ggc_free);
22135 type_specializations = htab_create_ggc (37,
22136 hash_specialization,
22137 eq_specializations,
22138 ggc_free);
22141 /* Print stats about the template hash tables for -fstats. */
22143 void
22144 print_template_statistics (void)
22146 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22147 "%f collisions\n", (long) htab_size (decl_specializations),
22148 (long) htab_elements (decl_specializations),
22149 htab_collisions (decl_specializations));
22150 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22151 "%f collisions\n", (long) htab_size (type_specializations),
22152 (long) htab_elements (type_specializations),
22153 htab_collisions (type_specializations));
22156 #include "gt-cp-pt.h"