PR c++/64359
[official-gcc.git] / gcc / cp / pt.c
blobf5bd842df0eb4f78dfdcc105cd42ae463c605de7
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "flags.h"
38 #include "cp-tree.h"
39 #include "c-family/c-common.h"
40 #include "c-family/c-objc.h"
41 #include "cp-objcp-common.h"
42 #include "tree-inline.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "timevar.h"
46 #include "tree-iterator.h"
47 #include "type-utils.h"
48 #include "gimplify.h"
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct GTY ((chain_next ("%h.next"))) pending_template {
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static GTY(()) tree saved_trees;
69 static vec<int> inline_parm_levels;
71 static GTY(()) struct tinst_level *current_tinst_level;
73 static GTY(()) tree saved_access_scope;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
80 /* True if we've recursed into fn_type_unification too many times. */
81 static bool excessive_deduction_depth;
83 struct GTY((for_user)) spec_entry
85 tree tmpl;
86 tree args;
87 tree spec;
90 struct spec_hasher : ggc_hasher<spec_entry *>
92 static hashval_t hash (spec_entry *);
93 static bool equal (spec_entry *, spec_entry *);
96 static GTY (()) hash_table<spec_hasher> *decl_specializations;
98 static GTY (()) hash_table<spec_hasher> *type_specializations;
100 /* Contains canonical template parameter types. The vector is indexed by
101 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
102 TREE_LIST, whose TREE_VALUEs contain the canonical template
103 parameters of various types and levels. */
104 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
106 #define UNIFY_ALLOW_NONE 0
107 #define UNIFY_ALLOW_MORE_CV_QUAL 1
108 #define UNIFY_ALLOW_LESS_CV_QUAL 2
109 #define UNIFY_ALLOW_DERIVED 4
110 #define UNIFY_ALLOW_INTEGER 8
111 #define UNIFY_ALLOW_OUTER_LEVEL 16
112 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
113 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
115 enum template_base_result {
116 tbr_incomplete_type,
117 tbr_ambiguous_baseclass,
118 tbr_success
121 static void push_access_scope (tree);
122 static void pop_access_scope (tree);
123 static bool resolve_overloaded_unification (tree, tree, tree, tree,
124 unification_kind_t, int,
125 bool);
126 static int try_one_overload (tree, tree, tree, tree, tree,
127 unification_kind_t, int, bool, bool);
128 static int unify (tree, tree, tree, tree, int, bool);
129 static void add_pending_template (tree);
130 static tree reopen_tinst_level (struct tinst_level *);
131 static tree tsubst_initializer_list (tree, tree);
132 static tree get_partial_spec_bindings (tree, tree, tree, tree);
133 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
134 bool, bool);
135 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
136 bool, bool);
137 static void tsubst_enum (tree, tree, tree);
138 static tree add_to_template_args (tree, tree);
139 static tree add_outermost_template_args (tree, tree);
140 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
141 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
142 tree);
143 static int type_unification_real (tree, tree, tree, const tree *,
144 unsigned int, int, unification_kind_t, int,
145 vec<deferred_access_check, va_gc> **,
146 bool);
147 static void note_template_header (int);
148 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
149 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
150 static tree convert_template_argument (tree, tree, tree,
151 tsubst_flags_t, int, tree);
152 static int for_each_template_parm (tree, tree_fn_t, void*,
153 hash_set<tree> *, bool);
154 static tree expand_template_argument_pack (tree);
155 static tree build_template_parm_index (int, int, int, tree, tree);
156 static bool inline_needs_template_parms (tree, bool);
157 static void push_inline_template_parms_recursive (tree, int);
158 static tree retrieve_local_specialization (tree);
159 static void register_local_specialization (tree, tree);
160 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
161 static int mark_template_parm (tree, void *);
162 static int template_parm_this_level_p (tree, void *);
163 static tree tsubst_friend_function (tree, tree);
164 static tree tsubst_friend_class (tree, tree);
165 static int can_complete_type_without_circularity (tree);
166 static tree get_bindings (tree, tree, tree, bool);
167 static int template_decl_level (tree);
168 static int check_cv_quals_for_unify (int, tree, tree);
169 static void template_parm_level_and_index (tree, int*, int*);
170 static int unify_pack_expansion (tree, tree, tree,
171 tree, unification_kind_t, bool, bool);
172 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
173 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
175 static void regenerate_decl_from_template (tree, tree);
176 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
177 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
178 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
179 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
180 static bool check_specialization_scope (void);
181 static tree process_partial_specialization (tree);
182 static void set_current_access_from_decl (tree);
183 static enum template_base_result get_template_base (tree, tree, tree, tree,
184 bool , tree *);
185 static tree try_class_unification (tree, tree, tree, tree, bool);
186 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
187 tree, tree);
188 static bool template_template_parm_bindings_ok_p (tree, tree);
189 static int template_args_equal (tree, tree);
190 static void tsubst_default_arguments (tree, tsubst_flags_t);
191 static tree for_each_template_parm_r (tree *, int *, void *);
192 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
193 static void copy_default_args_to_explicit_spec (tree);
194 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
195 static bool dependent_template_arg_p (tree);
196 static bool any_template_arguments_need_structural_equality_p (tree);
197 static bool dependent_type_p_r (tree);
198 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
199 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
200 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
201 static tree tsubst_decl (tree, tree, tsubst_flags_t);
202 static void perform_typedefs_access_check (tree tmpl, tree targs);
203 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
204 location_t);
205 static tree listify (tree);
206 static tree listify_autos (tree, tree);
207 static tree template_parm_to_arg (tree t);
208 static tree current_template_args (void);
209 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
210 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
212 /* Make the current scope suitable for access checking when we are
213 processing T. T can be FUNCTION_DECL for instantiated function
214 template, VAR_DECL for static member variable, or TYPE_DECL for
215 alias template (needed by instantiate_decl). */
217 static void
218 push_access_scope (tree t)
220 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
221 || TREE_CODE (t) == TYPE_DECL);
223 if (DECL_FRIEND_CONTEXT (t))
224 push_nested_class (DECL_FRIEND_CONTEXT (t));
225 else if (DECL_CLASS_SCOPE_P (t))
226 push_nested_class (DECL_CONTEXT (t));
227 else
228 push_to_top_level ();
230 if (TREE_CODE (t) == FUNCTION_DECL)
232 saved_access_scope = tree_cons
233 (NULL_TREE, current_function_decl, saved_access_scope);
234 current_function_decl = t;
238 /* Restore the scope set up by push_access_scope. T is the node we
239 are processing. */
241 static void
242 pop_access_scope (tree t)
244 if (TREE_CODE (t) == FUNCTION_DECL)
246 current_function_decl = TREE_VALUE (saved_access_scope);
247 saved_access_scope = TREE_CHAIN (saved_access_scope);
250 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
251 pop_nested_class ();
252 else
253 pop_from_top_level ();
256 /* Do any processing required when DECL (a member template
257 declaration) is finished. Returns the TEMPLATE_DECL corresponding
258 to DECL, unless it is a specialization, in which case the DECL
259 itself is returned. */
261 tree
262 finish_member_template_decl (tree decl)
264 if (decl == error_mark_node)
265 return error_mark_node;
267 gcc_assert (DECL_P (decl));
269 if (TREE_CODE (decl) == TYPE_DECL)
271 tree type;
273 type = TREE_TYPE (decl);
274 if (type == error_mark_node)
275 return error_mark_node;
276 if (MAYBE_CLASS_TYPE_P (type)
277 && CLASSTYPE_TEMPLATE_INFO (type)
278 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
280 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
281 check_member_template (tmpl);
282 return tmpl;
284 return NULL_TREE;
286 else if (TREE_CODE (decl) == FIELD_DECL)
287 error ("data member %qD cannot be a member template", decl);
288 else if (DECL_TEMPLATE_INFO (decl))
290 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
292 check_member_template (DECL_TI_TEMPLATE (decl));
293 return DECL_TI_TEMPLATE (decl);
295 else
296 return decl;
298 else
299 error ("invalid member template declaration %qD", decl);
301 return error_mark_node;
304 /* Create a template info node. */
306 tree
307 build_template_info (tree template_decl, tree template_args)
309 tree result = make_node (TEMPLATE_INFO);
310 TI_TEMPLATE (result) = template_decl;
311 TI_ARGS (result) = template_args;
312 return result;
315 /* Return the template info node corresponding to T, whatever T is. */
317 tree
318 get_template_info (const_tree t)
320 tree tinfo = NULL_TREE;
322 if (!t || t == error_mark_node)
323 return NULL;
325 if (TREE_CODE (t) == NAMESPACE_DECL)
326 return NULL;
328 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
329 tinfo = DECL_TEMPLATE_INFO (t);
331 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
332 t = TREE_TYPE (t);
334 if (OVERLOAD_TYPE_P (t))
335 tinfo = TYPE_TEMPLATE_INFO (t);
336 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
337 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
339 return tinfo;
342 /* Returns the template nesting level of the indicated class TYPE.
344 For example, in:
345 template <class T>
346 struct A
348 template <class U>
349 struct B {};
352 A<T>::B<U> has depth two, while A<T> has depth one.
353 Both A<T>::B<int> and A<int>::B<U> have depth one, if
354 they are instantiations, not specializations.
356 This function is guaranteed to return 0 if passed NULL_TREE so
357 that, for example, `template_class_depth (current_class_type)' is
358 always safe. */
361 template_class_depth (tree type)
363 int depth;
365 for (depth = 0;
366 type && TREE_CODE (type) != NAMESPACE_DECL;
367 type = (TREE_CODE (type) == FUNCTION_DECL)
368 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
370 tree tinfo = get_template_info (type);
372 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
373 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
374 ++depth;
377 return depth;
380 /* Subroutine of maybe_begin_member_template_processing.
381 Returns true if processing DECL needs us to push template parms. */
383 static bool
384 inline_needs_template_parms (tree decl, bool nsdmi)
386 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
387 return false;
389 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
390 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
393 /* Subroutine of maybe_begin_member_template_processing.
394 Push the template parms in PARMS, starting from LEVELS steps into the
395 chain, and ending at the beginning, since template parms are listed
396 innermost first. */
398 static void
399 push_inline_template_parms_recursive (tree parmlist, int levels)
401 tree parms = TREE_VALUE (parmlist);
402 int i;
404 if (levels > 1)
405 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
407 ++processing_template_decl;
408 current_template_parms
409 = tree_cons (size_int (processing_template_decl),
410 parms, current_template_parms);
411 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
413 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
414 NULL);
415 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
417 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
419 if (error_operand_p (parm))
420 continue;
422 gcc_assert (DECL_P (parm));
424 switch (TREE_CODE (parm))
426 case TYPE_DECL:
427 case TEMPLATE_DECL:
428 pushdecl (parm);
429 break;
431 case PARM_DECL:
433 /* Make a CONST_DECL as is done in process_template_parm.
434 It is ugly that we recreate this here; the original
435 version built in process_template_parm is no longer
436 available. */
437 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
438 CONST_DECL, DECL_NAME (parm),
439 TREE_TYPE (parm));
440 DECL_ARTIFICIAL (decl) = 1;
441 TREE_CONSTANT (decl) = 1;
442 TREE_READONLY (decl) = 1;
443 DECL_INITIAL (decl) = DECL_INITIAL (parm);
444 SET_DECL_TEMPLATE_PARM_P (decl);
445 pushdecl (decl);
447 break;
449 default:
450 gcc_unreachable ();
455 /* Restore the template parameter context for a member template, a
456 friend template defined in a class definition, or a non-template
457 member of template class. */
459 void
460 maybe_begin_member_template_processing (tree decl)
462 tree parms;
463 int levels = 0;
464 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
466 if (nsdmi)
468 tree ctx = DECL_CONTEXT (decl);
469 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
470 /* Disregard full specializations (c++/60999). */
471 && uses_template_parms (ctx)
472 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
475 if (inline_needs_template_parms (decl, nsdmi))
477 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
478 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
480 if (DECL_TEMPLATE_SPECIALIZATION (decl))
482 --levels;
483 parms = TREE_CHAIN (parms);
486 push_inline_template_parms_recursive (parms, levels);
489 /* Remember how many levels of template parameters we pushed so that
490 we can pop them later. */
491 inline_parm_levels.safe_push (levels);
494 /* Undo the effects of maybe_begin_member_template_processing. */
496 void
497 maybe_end_member_template_processing (void)
499 int i;
500 int last;
502 if (inline_parm_levels.length () == 0)
503 return;
505 last = inline_parm_levels.pop ();
506 for (i = 0; i < last; ++i)
508 --processing_template_decl;
509 current_template_parms = TREE_CHAIN (current_template_parms);
510 poplevel (0, 0, 0);
514 /* Return a new template argument vector which contains all of ARGS,
515 but has as its innermost set of arguments the EXTRA_ARGS. */
517 static tree
518 add_to_template_args (tree args, tree extra_args)
520 tree new_args;
521 int extra_depth;
522 int i;
523 int j;
525 if (args == NULL_TREE || extra_args == error_mark_node)
526 return extra_args;
528 extra_depth = TMPL_ARGS_DEPTH (extra_args);
529 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
531 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
532 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
534 for (j = 1; j <= extra_depth; ++j, ++i)
535 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
537 return new_args;
540 /* Like add_to_template_args, but only the outermost ARGS are added to
541 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
542 (EXTRA_ARGS) levels are added. This function is used to combine
543 the template arguments from a partial instantiation with the
544 template arguments used to attain the full instantiation from the
545 partial instantiation. */
547 static tree
548 add_outermost_template_args (tree args, tree extra_args)
550 tree new_args;
552 /* If there are more levels of EXTRA_ARGS than there are ARGS,
553 something very fishy is going on. */
554 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
556 /* If *all* the new arguments will be the EXTRA_ARGS, just return
557 them. */
558 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
559 return extra_args;
561 /* For the moment, we make ARGS look like it contains fewer levels. */
562 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
564 new_args = add_to_template_args (args, extra_args);
566 /* Now, we restore ARGS to its full dimensions. */
567 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
569 return new_args;
572 /* Return the N levels of innermost template arguments from the ARGS. */
574 tree
575 get_innermost_template_args (tree args, int n)
577 tree new_args;
578 int extra_levels;
579 int i;
581 gcc_assert (n >= 0);
583 /* If N is 1, just return the innermost set of template arguments. */
584 if (n == 1)
585 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
587 /* If we're not removing anything, just return the arguments we were
588 given. */
589 extra_levels = TMPL_ARGS_DEPTH (args) - n;
590 gcc_assert (extra_levels >= 0);
591 if (extra_levels == 0)
592 return args;
594 /* Make a new set of arguments, not containing the outer arguments. */
595 new_args = make_tree_vec (n);
596 for (i = 1; i <= n; ++i)
597 SET_TMPL_ARGS_LEVEL (new_args, i,
598 TMPL_ARGS_LEVEL (args, i + extra_levels));
600 return new_args;
603 /* The inverse of get_innermost_template_args: Return all but the innermost
604 EXTRA_LEVELS levels of template arguments from the ARGS. */
606 static tree
607 strip_innermost_template_args (tree args, int extra_levels)
609 tree new_args;
610 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
611 int i;
613 gcc_assert (n >= 0);
615 /* If N is 1, just return the outermost set of template arguments. */
616 if (n == 1)
617 return TMPL_ARGS_LEVEL (args, 1);
619 /* If we're not removing anything, just return the arguments we were
620 given. */
621 gcc_assert (extra_levels >= 0);
622 if (extra_levels == 0)
623 return args;
625 /* Make a new set of arguments, not containing the inner arguments. */
626 new_args = make_tree_vec (n);
627 for (i = 1; i <= n; ++i)
628 SET_TMPL_ARGS_LEVEL (new_args, i,
629 TMPL_ARGS_LEVEL (args, i));
631 return new_args;
634 /* We've got a template header coming up; push to a new level for storing
635 the parms. */
637 void
638 begin_template_parm_list (void)
640 /* We use a non-tag-transparent scope here, which causes pushtag to
641 put tags in this scope, rather than in the enclosing class or
642 namespace scope. This is the right thing, since we want
643 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
644 global template class, push_template_decl handles putting the
645 TEMPLATE_DECL into top-level scope. For a nested template class,
646 e.g.:
648 template <class T> struct S1 {
649 template <class T> struct S2 {};
652 pushtag contains special code to call pushdecl_with_scope on the
653 TEMPLATE_DECL for S2. */
654 begin_scope (sk_template_parms, NULL);
655 ++processing_template_decl;
656 ++processing_template_parmlist;
657 note_template_header (0);
660 /* This routine is called when a specialization is declared. If it is
661 invalid to declare a specialization here, an error is reported and
662 false is returned, otherwise this routine will return true. */
664 static bool
665 check_specialization_scope (void)
667 tree scope = current_scope ();
669 /* [temp.expl.spec]
671 An explicit specialization shall be declared in the namespace of
672 which the template is a member, or, for member templates, in the
673 namespace of which the enclosing class or enclosing class
674 template is a member. An explicit specialization of a member
675 function, member class or static data member of a class template
676 shall be declared in the namespace of which the class template
677 is a member. */
678 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
680 error ("explicit specialization in non-namespace scope %qD", scope);
681 return false;
684 /* [temp.expl.spec]
686 In an explicit specialization declaration for a member of a class
687 template or a member template that appears in namespace scope,
688 the member template and some of its enclosing class templates may
689 remain unspecialized, except that the declaration shall not
690 explicitly specialize a class member template if its enclosing
691 class templates are not explicitly specialized as well. */
692 if (current_template_parms)
694 error ("enclosing class templates are not explicitly specialized");
695 return false;
698 return true;
701 /* We've just seen template <>. */
703 bool
704 begin_specialization (void)
706 begin_scope (sk_template_spec, NULL);
707 note_template_header (1);
708 return check_specialization_scope ();
711 /* Called at then end of processing a declaration preceded by
712 template<>. */
714 void
715 end_specialization (void)
717 finish_scope ();
718 reset_specialization ();
721 /* Any template <>'s that we have seen thus far are not referring to a
722 function specialization. */
724 void
725 reset_specialization (void)
727 processing_specialization = 0;
728 template_header_count = 0;
731 /* We've just seen a template header. If SPECIALIZATION is nonzero,
732 it was of the form template <>. */
734 static void
735 note_template_header (int specialization)
737 processing_specialization = specialization;
738 template_header_count++;
741 /* We're beginning an explicit instantiation. */
743 void
744 begin_explicit_instantiation (void)
746 gcc_assert (!processing_explicit_instantiation);
747 processing_explicit_instantiation = true;
751 void
752 end_explicit_instantiation (void)
754 gcc_assert (processing_explicit_instantiation);
755 processing_explicit_instantiation = false;
758 /* An explicit specialization or partial specialization of TMPL is being
759 declared. Check that the namespace in which the specialization is
760 occurring is permissible. Returns false iff it is invalid to
761 specialize TMPL in the current namespace. */
763 static bool
764 check_specialization_namespace (tree tmpl)
766 tree tpl_ns = decl_namespace_context (tmpl);
768 /* [tmpl.expl.spec]
770 An explicit specialization shall be declared in the namespace of
771 which the template is a member, or, for member templates, in the
772 namespace of which the enclosing class or enclosing class
773 template is a member. An explicit specialization of a member
774 function, member class or static data member of a class template
775 shall be declared in the namespace of which the class template is
776 a member. */
777 if (current_scope() != DECL_CONTEXT (tmpl)
778 && !at_namespace_scope_p ())
780 error ("specialization of %qD must appear at namespace scope", tmpl);
781 return false;
783 if (is_associated_namespace (current_namespace, tpl_ns))
784 /* Same or super-using namespace. */
785 return true;
786 else
788 permerror (input_location, "specialization of %qD in different namespace", tmpl);
789 permerror (input_location, " from definition of %q+#D", tmpl);
790 return false;
794 /* SPEC is an explicit instantiation. Check that it is valid to
795 perform this explicit instantiation in the current namespace. */
797 static void
798 check_explicit_instantiation_namespace (tree spec)
800 tree ns;
802 /* DR 275: An explicit instantiation shall appear in an enclosing
803 namespace of its template. */
804 ns = decl_namespace_context (spec);
805 if (!is_ancestor (current_namespace, ns))
806 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
807 "(which does not enclose namespace %qD)",
808 spec, current_namespace, ns);
811 /* The TYPE is being declared. If it is a template type, that means it
812 is a partial specialization. Do appropriate error-checking. */
814 tree
815 maybe_process_partial_specialization (tree type)
817 tree context;
819 if (type == error_mark_node)
820 return error_mark_node;
822 /* A lambda that appears in specialization context is not itself a
823 specialization. */
824 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
825 return type;
827 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
829 error ("name of class shadows template template parameter %qD",
830 TYPE_NAME (type));
831 return error_mark_node;
834 context = TYPE_CONTEXT (type);
836 if (TYPE_ALIAS_P (type))
838 if (TYPE_TEMPLATE_INFO (type)
839 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
840 error ("specialization of alias template %qD",
841 TYPE_TI_TEMPLATE (type));
842 else
843 error ("explicit specialization of non-template %qT", type);
844 return error_mark_node;
846 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
848 /* This is for ordinary explicit specialization and partial
849 specialization of a template class such as:
851 template <> class C<int>;
855 template <class T> class C<T*>;
857 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
859 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
860 && !COMPLETE_TYPE_P (type))
862 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
863 && !at_namespace_scope_p ())
864 return error_mark_node;
865 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
866 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
867 if (processing_template_decl)
869 if (push_template_decl (TYPE_MAIN_DECL (type))
870 == error_mark_node)
871 return error_mark_node;
874 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
875 error ("specialization of %qT after instantiation", type);
876 else if (errorcount && !processing_specialization
877 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
878 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
879 /* Trying to define a specialization either without a template<> header
880 or in an inappropriate place. We've already given an error, so just
881 bail now so we don't actually define the specialization. */
882 return error_mark_node;
884 else if (CLASS_TYPE_P (type)
885 && !CLASSTYPE_USE_TEMPLATE (type)
886 && CLASSTYPE_TEMPLATE_INFO (type)
887 && context && CLASS_TYPE_P (context)
888 && CLASSTYPE_TEMPLATE_INFO (context))
890 /* This is for an explicit specialization of member class
891 template according to [temp.expl.spec/18]:
893 template <> template <class U> class C<int>::D;
895 The context `C<int>' must be an implicit instantiation.
896 Otherwise this is just a member class template declared
897 earlier like:
899 template <> class C<int> { template <class U> class D; };
900 template <> template <class U> class C<int>::D;
902 In the first case, `C<int>::D' is a specialization of `C<T>::D'
903 while in the second case, `C<int>::D' is a primary template
904 and `C<T>::D' may not exist. */
906 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
907 && !COMPLETE_TYPE_P (type))
909 tree t;
910 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
912 if (current_namespace
913 != decl_namespace_context (tmpl))
915 permerror (input_location, "specializing %q#T in different namespace", type);
916 permerror (input_location, " from definition of %q+#D", tmpl);
919 /* Check for invalid specialization after instantiation:
921 template <> template <> class C<int>::D<int>;
922 template <> template <class U> class C<int>::D; */
924 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
925 t; t = TREE_CHAIN (t))
927 tree inst = TREE_VALUE (t);
928 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
929 || !COMPLETE_OR_OPEN_TYPE_P (inst))
931 /* We already have a full specialization of this partial
932 instantiation, or a full specialization has been
933 looked up but not instantiated. Reassign it to the
934 new member specialization template. */
935 spec_entry elt;
936 spec_entry *entry;
938 elt.tmpl = most_general_template (tmpl);
939 elt.args = CLASSTYPE_TI_ARGS (inst);
940 elt.spec = inst;
942 type_specializations->remove_elt (&elt);
944 elt.tmpl = tmpl;
945 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
947 spec_entry **slot
948 = type_specializations->find_slot (&elt, INSERT);
949 entry = ggc_alloc<spec_entry> ();
950 *entry = elt;
951 *slot = entry;
953 else
954 /* But if we've had an implicit instantiation, that's a
955 problem ([temp.expl.spec]/6). */
956 error ("specialization %qT after instantiation %qT",
957 type, inst);
960 /* Mark TYPE as a specialization. And as a result, we only
961 have one level of template argument for the innermost
962 class template. */
963 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
964 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
965 CLASSTYPE_TI_ARGS (type)
966 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
969 else if (processing_specialization)
971 /* Someday C++0x may allow for enum template specialization. */
972 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
973 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
974 pedwarn (input_location, OPT_Wpedantic, "template specialization "
975 "of %qD not allowed by ISO C++", type);
976 else
978 error ("explicit specialization of non-template %qT", type);
979 return error_mark_node;
983 return type;
986 /* Returns nonzero if we can optimize the retrieval of specializations
987 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
988 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
990 static inline bool
991 optimize_specialization_lookup_p (tree tmpl)
993 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
994 && DECL_CLASS_SCOPE_P (tmpl)
995 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
996 parameter. */
997 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
998 /* The optimized lookup depends on the fact that the
999 template arguments for the member function template apply
1000 purely to the containing class, which is not true if the
1001 containing class is an explicit or partial
1002 specialization. */
1003 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1004 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1005 && !DECL_CONV_FN_P (tmpl)
1006 /* It is possible to have a template that is not a member
1007 template and is not a member of a template class:
1009 template <typename T>
1010 struct S { friend A::f(); };
1012 Here, the friend function is a template, but the context does
1013 not have template information. The optimized lookup relies
1014 on having ARGS be the template arguments for both the class
1015 and the function template. */
1016 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1019 /* Retrieve the specialization (in the sense of [temp.spec] - a
1020 specialization is either an instantiation or an explicit
1021 specialization) of TMPL for the given template ARGS. If there is
1022 no such specialization, return NULL_TREE. The ARGS are a vector of
1023 arguments, or a vector of vectors of arguments, in the case of
1024 templates with more than one level of parameters.
1026 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1027 then we search for a partial specialization matching ARGS. This
1028 parameter is ignored if TMPL is not a class template.
1030 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1031 result is a NONTYPE_ARGUMENT_PACK. */
1033 static tree
1034 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1036 if (tmpl == NULL_TREE)
1037 return NULL_TREE;
1039 if (args == error_mark_node)
1040 return NULL_TREE;
1042 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1043 || TREE_CODE (tmpl) == FIELD_DECL);
1045 /* There should be as many levels of arguments as there are
1046 levels of parameters. */
1047 gcc_assert (TMPL_ARGS_DEPTH (args)
1048 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1049 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1050 : template_class_depth (DECL_CONTEXT (tmpl))));
1052 if (optimize_specialization_lookup_p (tmpl))
1054 tree class_template;
1055 tree class_specialization;
1056 vec<tree, va_gc> *methods;
1057 tree fns;
1058 int idx;
1060 /* The template arguments actually apply to the containing
1061 class. Find the class specialization with those
1062 arguments. */
1063 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1064 class_specialization
1065 = retrieve_specialization (class_template, args, 0);
1066 if (!class_specialization)
1067 return NULL_TREE;
1068 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1069 for the specialization. */
1070 idx = class_method_index_for_fn (class_specialization, tmpl);
1071 if (idx == -1)
1072 return NULL_TREE;
1073 /* Iterate through the methods with the indicated name, looking
1074 for the one that has an instance of TMPL. */
1075 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1076 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1078 tree fn = OVL_CURRENT (fns);
1079 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1080 /* using-declarations can add base methods to the method vec,
1081 and we don't want those here. */
1082 && DECL_CONTEXT (fn) == class_specialization)
1083 return fn;
1085 return NULL_TREE;
1087 else
1089 spec_entry *found;
1090 spec_entry elt;
1091 hash_table<spec_hasher> *specializations;
1093 elt.tmpl = tmpl;
1094 elt.args = args;
1095 elt.spec = NULL_TREE;
1097 if (DECL_CLASS_TEMPLATE_P (tmpl))
1098 specializations = type_specializations;
1099 else
1100 specializations = decl_specializations;
1102 if (hash == 0)
1103 hash = spec_hasher::hash (&elt);
1104 found = specializations->find_with_hash (&elt, hash);
1105 if (found)
1106 return found->spec;
1109 return NULL_TREE;
1112 /* Like retrieve_specialization, but for local declarations. */
1114 static tree
1115 retrieve_local_specialization (tree tmpl)
1117 if (local_specializations == NULL)
1118 return NULL_TREE;
1120 tree *slot = local_specializations->get (tmpl);
1121 return slot ? *slot : NULL_TREE;
1124 /* Returns nonzero iff DECL is a specialization of TMPL. */
1127 is_specialization_of (tree decl, tree tmpl)
1129 tree t;
1131 if (TREE_CODE (decl) == FUNCTION_DECL)
1133 for (t = decl;
1134 t != NULL_TREE;
1135 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1136 if (t == tmpl)
1137 return 1;
1139 else
1141 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1143 for (t = TREE_TYPE (decl);
1144 t != NULL_TREE;
1145 t = CLASSTYPE_USE_TEMPLATE (t)
1146 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1147 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1148 return 1;
1151 return 0;
1154 /* Returns nonzero iff DECL is a specialization of friend declaration
1155 FRIEND_DECL according to [temp.friend]. */
1157 bool
1158 is_specialization_of_friend (tree decl, tree friend_decl)
1160 bool need_template = true;
1161 int template_depth;
1163 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1164 || TREE_CODE (decl) == TYPE_DECL);
1166 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1167 of a template class, we want to check if DECL is a specialization
1168 if this. */
1169 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1170 && DECL_TEMPLATE_INFO (friend_decl)
1171 && !DECL_USE_TEMPLATE (friend_decl))
1173 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1174 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1175 need_template = false;
1177 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1178 && !PRIMARY_TEMPLATE_P (friend_decl))
1179 need_template = false;
1181 /* There is nothing to do if this is not a template friend. */
1182 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1183 return false;
1185 if (is_specialization_of (decl, friend_decl))
1186 return true;
1188 /* [temp.friend/6]
1189 A member of a class template may be declared to be a friend of a
1190 non-template class. In this case, the corresponding member of
1191 every specialization of the class template is a friend of the
1192 class granting friendship.
1194 For example, given a template friend declaration
1196 template <class T> friend void A<T>::f();
1198 the member function below is considered a friend
1200 template <> struct A<int> {
1201 void f();
1204 For this type of template friend, TEMPLATE_DEPTH below will be
1205 nonzero. To determine if DECL is a friend of FRIEND, we first
1206 check if the enclosing class is a specialization of another. */
1208 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1209 if (template_depth
1210 && DECL_CLASS_SCOPE_P (decl)
1211 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1212 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1214 /* Next, we check the members themselves. In order to handle
1215 a few tricky cases, such as when FRIEND_DECL's are
1217 template <class T> friend void A<T>::g(T t);
1218 template <class T> template <T t> friend void A<T>::h();
1220 and DECL's are
1222 void A<int>::g(int);
1223 template <int> void A<int>::h();
1225 we need to figure out ARGS, the template arguments from
1226 the context of DECL. This is required for template substitution
1227 of `T' in the function parameter of `g' and template parameter
1228 of `h' in the above examples. Here ARGS corresponds to `int'. */
1230 tree context = DECL_CONTEXT (decl);
1231 tree args = NULL_TREE;
1232 int current_depth = 0;
1234 while (current_depth < template_depth)
1236 if (CLASSTYPE_TEMPLATE_INFO (context))
1238 if (current_depth == 0)
1239 args = TYPE_TI_ARGS (context);
1240 else
1241 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1242 current_depth++;
1244 context = TYPE_CONTEXT (context);
1247 if (TREE_CODE (decl) == FUNCTION_DECL)
1249 bool is_template;
1250 tree friend_type;
1251 tree decl_type;
1252 tree friend_args_type;
1253 tree decl_args_type;
1255 /* Make sure that both DECL and FRIEND_DECL are templates or
1256 non-templates. */
1257 is_template = DECL_TEMPLATE_INFO (decl)
1258 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1259 if (need_template ^ is_template)
1260 return false;
1261 else if (is_template)
1263 /* If both are templates, check template parameter list. */
1264 tree friend_parms
1265 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1266 args, tf_none);
1267 if (!comp_template_parms
1268 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1269 friend_parms))
1270 return false;
1272 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1274 else
1275 decl_type = TREE_TYPE (decl);
1277 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1278 tf_none, NULL_TREE);
1279 if (friend_type == error_mark_node)
1280 return false;
1282 /* Check if return types match. */
1283 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1284 return false;
1286 /* Check if function parameter types match, ignoring the
1287 `this' parameter. */
1288 friend_args_type = TYPE_ARG_TYPES (friend_type);
1289 decl_args_type = TYPE_ARG_TYPES (decl_type);
1290 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1291 friend_args_type = TREE_CHAIN (friend_args_type);
1292 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1293 decl_args_type = TREE_CHAIN (decl_args_type);
1295 return compparms (decl_args_type, friend_args_type);
1297 else
1299 /* DECL is a TYPE_DECL */
1300 bool is_template;
1301 tree decl_type = TREE_TYPE (decl);
1303 /* Make sure that both DECL and FRIEND_DECL are templates or
1304 non-templates. */
1305 is_template
1306 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1309 if (need_template ^ is_template)
1310 return false;
1311 else if (is_template)
1313 tree friend_parms;
1314 /* If both are templates, check the name of the two
1315 TEMPLATE_DECL's first because is_friend didn't. */
1316 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1317 != DECL_NAME (friend_decl))
1318 return false;
1320 /* Now check template parameter list. */
1321 friend_parms
1322 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1323 args, tf_none);
1324 return comp_template_parms
1325 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1326 friend_parms);
1328 else
1329 return (DECL_NAME (decl)
1330 == DECL_NAME (friend_decl));
1333 return false;
1336 /* Register the specialization SPEC as a specialization of TMPL with
1337 the indicated ARGS. IS_FRIEND indicates whether the specialization
1338 is actually just a friend declaration. Returns SPEC, or an
1339 equivalent prior declaration, if available.
1341 We also store instantiations of field packs in the hash table, even
1342 though they are not themselves templates, to make lookup easier. */
1344 static tree
1345 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1346 hashval_t hash)
1348 tree fn;
1349 spec_entry **slot = NULL;
1350 spec_entry elt;
1352 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1353 || (TREE_CODE (tmpl) == FIELD_DECL
1354 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1356 if (TREE_CODE (spec) == FUNCTION_DECL
1357 && uses_template_parms (DECL_TI_ARGS (spec)))
1358 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1359 register it; we want the corresponding TEMPLATE_DECL instead.
1360 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1361 the more obvious `uses_template_parms (spec)' to avoid problems
1362 with default function arguments. In particular, given
1363 something like this:
1365 template <class T> void f(T t1, T t = T())
1367 the default argument expression is not substituted for in an
1368 instantiation unless and until it is actually needed. */
1369 return spec;
1371 if (optimize_specialization_lookup_p (tmpl))
1372 /* We don't put these specializations in the hash table, but we might
1373 want to give an error about a mismatch. */
1374 fn = retrieve_specialization (tmpl, args, 0);
1375 else
1377 elt.tmpl = tmpl;
1378 elt.args = args;
1379 elt.spec = spec;
1381 if (hash == 0)
1382 hash = spec_hasher::hash (&elt);
1384 slot =
1385 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1386 if (*slot)
1387 fn = ((spec_entry *) *slot)->spec;
1388 else
1389 fn = NULL_TREE;
1392 /* We can sometimes try to re-register a specialization that we've
1393 already got. In particular, regenerate_decl_from_template calls
1394 duplicate_decls which will update the specialization list. But,
1395 we'll still get called again here anyhow. It's more convenient
1396 to simply allow this than to try to prevent it. */
1397 if (fn == spec)
1398 return spec;
1399 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1401 if (DECL_TEMPLATE_INSTANTIATION (fn))
1403 if (DECL_ODR_USED (fn)
1404 || DECL_EXPLICIT_INSTANTIATION (fn))
1406 error ("specialization of %qD after instantiation",
1407 fn);
1408 return error_mark_node;
1410 else
1412 tree clone;
1413 /* This situation should occur only if the first
1414 specialization is an implicit instantiation, the
1415 second is an explicit specialization, and the
1416 implicit instantiation has not yet been used. That
1417 situation can occur if we have implicitly
1418 instantiated a member function and then specialized
1419 it later.
1421 We can also wind up here if a friend declaration that
1422 looked like an instantiation turns out to be a
1423 specialization:
1425 template <class T> void foo(T);
1426 class S { friend void foo<>(int) };
1427 template <> void foo(int);
1429 We transform the existing DECL in place so that any
1430 pointers to it become pointers to the updated
1431 declaration.
1433 If there was a definition for the template, but not
1434 for the specialization, we want this to look as if
1435 there were no definition, and vice versa. */
1436 DECL_INITIAL (fn) = NULL_TREE;
1437 duplicate_decls (spec, fn, is_friend);
1438 /* The call to duplicate_decls will have applied
1439 [temp.expl.spec]:
1441 An explicit specialization of a function template
1442 is inline only if it is explicitly declared to be,
1443 and independently of whether its function template
1446 to the primary function; now copy the inline bits to
1447 the various clones. */
1448 FOR_EACH_CLONE (clone, fn)
1450 DECL_DECLARED_INLINE_P (clone)
1451 = DECL_DECLARED_INLINE_P (fn);
1452 DECL_SOURCE_LOCATION (clone)
1453 = DECL_SOURCE_LOCATION (fn);
1454 DECL_DELETED_FN (clone)
1455 = DECL_DELETED_FN (fn);
1457 check_specialization_namespace (tmpl);
1459 return fn;
1462 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1464 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1465 /* Dup decl failed, but this is a new definition. Set the
1466 line number so any errors match this new
1467 definition. */
1468 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1470 return fn;
1473 else if (fn)
1474 return duplicate_decls (spec, fn, is_friend);
1476 /* A specialization must be declared in the same namespace as the
1477 template it is specializing. */
1478 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1479 && !check_specialization_namespace (tmpl))
1480 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1482 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1484 spec_entry *entry = ggc_alloc<spec_entry> ();
1485 gcc_assert (tmpl && args && spec);
1486 *entry = elt;
1487 *slot = entry;
1488 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1489 && PRIMARY_TEMPLATE_P (tmpl)
1490 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1491 || variable_template_p (tmpl))
1492 /* If TMPL is a forward declaration of a template function, keep a list
1493 of all specializations in case we need to reassign them to a friend
1494 template later in tsubst_friend_function.
1496 Also keep a list of all variable template instantiations so that
1497 process_partial_specialization can check whether a later partial
1498 specialization would have used it. */
1499 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1500 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1503 return spec;
1506 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1507 TMPL and ARGS members, ignores SPEC. */
1509 int comparing_specializations;
1511 bool
1512 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1514 int equal;
1516 ++comparing_specializations;
1517 equal = (e1->tmpl == e2->tmpl
1518 && comp_template_args (e1->args, e2->args));
1519 --comparing_specializations;
1521 return equal;
1524 /* Returns a hash for a template TMPL and template arguments ARGS. */
1526 static hashval_t
1527 hash_tmpl_and_args (tree tmpl, tree args)
1529 hashval_t val = DECL_UID (tmpl);
1530 return iterative_hash_template_arg (args, val);
1533 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1534 ignoring SPEC. */
1536 hashval_t
1537 spec_hasher::hash (spec_entry *e)
1539 return hash_tmpl_and_args (e->tmpl, e->args);
1542 /* Recursively calculate a hash value for a template argument ARG, for use
1543 in the hash tables of template specializations. */
1545 hashval_t
1546 iterative_hash_template_arg (tree arg, hashval_t val)
1548 unsigned HOST_WIDE_INT i;
1549 enum tree_code code;
1550 char tclass;
1552 if (arg == NULL_TREE)
1553 return iterative_hash_object (arg, val);
1555 if (!TYPE_P (arg))
1556 STRIP_NOPS (arg);
1558 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1559 /* We can get one of these when re-hashing a previous entry in the middle
1560 of substituting into a pack expansion. Just look through it. */
1561 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1563 code = TREE_CODE (arg);
1564 tclass = TREE_CODE_CLASS (code);
1566 val = iterative_hash_object (code, val);
1568 switch (code)
1570 case ERROR_MARK:
1571 return val;
1573 case IDENTIFIER_NODE:
1574 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1576 case TREE_VEC:
1578 int i, len = TREE_VEC_LENGTH (arg);
1579 for (i = 0; i < len; ++i)
1580 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1581 return val;
1584 case TYPE_PACK_EXPANSION:
1585 case EXPR_PACK_EXPANSION:
1586 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1587 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1589 case TYPE_ARGUMENT_PACK:
1590 case NONTYPE_ARGUMENT_PACK:
1591 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1593 case TREE_LIST:
1594 for (; arg; arg = TREE_CHAIN (arg))
1595 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1596 return val;
1598 case OVERLOAD:
1599 for (; arg; arg = OVL_NEXT (arg))
1600 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1601 return val;
1603 case CONSTRUCTOR:
1605 tree field, value;
1606 iterative_hash_template_arg (TREE_TYPE (arg), val);
1607 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1609 val = iterative_hash_template_arg (field, val);
1610 val = iterative_hash_template_arg (value, val);
1612 return val;
1615 case PARM_DECL:
1616 if (!DECL_ARTIFICIAL (arg))
1618 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1619 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1621 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1623 case TARGET_EXPR:
1624 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1626 case PTRMEM_CST:
1627 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1628 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1630 case TEMPLATE_PARM_INDEX:
1631 val = iterative_hash_template_arg
1632 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1633 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1634 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1636 case TRAIT_EXPR:
1637 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1638 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1639 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1641 case BASELINK:
1642 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1643 val);
1644 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1645 val);
1647 case MODOP_EXPR:
1648 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1649 code = TREE_CODE (TREE_OPERAND (arg, 1));
1650 val = iterative_hash_object (code, val);
1651 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1653 case LAMBDA_EXPR:
1654 /* A lambda can't appear in a template arg, but don't crash on
1655 erroneous input. */
1656 gcc_assert (seen_error ());
1657 return val;
1659 case CAST_EXPR:
1660 case IMPLICIT_CONV_EXPR:
1661 case STATIC_CAST_EXPR:
1662 case REINTERPRET_CAST_EXPR:
1663 case CONST_CAST_EXPR:
1664 case DYNAMIC_CAST_EXPR:
1665 case NEW_EXPR:
1666 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1667 /* Now hash operands as usual. */
1668 break;
1670 default:
1671 break;
1674 switch (tclass)
1676 case tcc_type:
1677 if (alias_template_specialization_p (arg))
1679 // We want an alias specialization that survived strip_typedefs
1680 // to hash differently from its TYPE_CANONICAL, to avoid hash
1681 // collisions that compare as different in template_args_equal.
1682 // These could be dependent specializations that strip_typedefs
1683 // left alone, or untouched specializations because
1684 // coerce_template_parms returns the unconverted template
1685 // arguments if it sees incomplete argument packs.
1686 tree ti = TYPE_TEMPLATE_INFO (arg);
1687 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1689 if (TYPE_CANONICAL (arg))
1690 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1691 val);
1692 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1693 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1694 /* Otherwise just compare the types during lookup. */
1695 return val;
1697 case tcc_declaration:
1698 case tcc_constant:
1699 return iterative_hash_expr (arg, val);
1701 default:
1702 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1704 unsigned n = cp_tree_operand_length (arg);
1705 for (i = 0; i < n; ++i)
1706 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1707 return val;
1710 gcc_unreachable ();
1711 return 0;
1714 /* Unregister the specialization SPEC as a specialization of TMPL.
1715 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1716 if the SPEC was listed as a specialization of TMPL.
1718 Note that SPEC has been ggc_freed, so we can't look inside it. */
1720 bool
1721 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1723 spec_entry *entry;
1724 spec_entry elt;
1726 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1727 elt.args = TI_ARGS (tinfo);
1728 elt.spec = NULL_TREE;
1730 entry = decl_specializations->find (&elt);
1731 if (entry != NULL)
1733 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1734 gcc_assert (new_spec != NULL_TREE);
1735 entry->spec = new_spec;
1736 return 1;
1739 return 0;
1742 /* Like register_specialization, but for local declarations. We are
1743 registering SPEC, an instantiation of TMPL. */
1745 static void
1746 register_local_specialization (tree spec, tree tmpl)
1748 local_specializations->put (tmpl, spec);
1751 /* TYPE is a class type. Returns true if TYPE is an explicitly
1752 specialized class. */
1754 bool
1755 explicit_class_specialization_p (tree type)
1757 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1758 return false;
1759 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1762 /* Print the list of functions at FNS, going through all the overloads
1763 for each element of the list. Alternatively, FNS can not be a
1764 TREE_LIST, in which case it will be printed together with all the
1765 overloads.
1767 MORE and *STR should respectively be FALSE and NULL when the function
1768 is called from the outside. They are used internally on recursive
1769 calls. print_candidates manages the two parameters and leaves NULL
1770 in *STR when it ends. */
1772 static void
1773 print_candidates_1 (tree fns, bool more, const char **str)
1775 tree fn, fn2;
1776 char *spaces = NULL;
1778 for (fn = fns; fn; fn = OVL_NEXT (fn))
1779 if (TREE_CODE (fn) == TREE_LIST)
1781 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1782 print_candidates_1 (TREE_VALUE (fn2),
1783 TREE_CHAIN (fn2) || more, str);
1785 else
1787 tree cand = OVL_CURRENT (fn);
1788 if (!*str)
1790 /* Pick the prefix string. */
1791 if (!more && !OVL_NEXT (fns))
1793 inform (DECL_SOURCE_LOCATION (cand),
1794 "candidate is: %#D", cand);
1795 continue;
1798 *str = _("candidates are:");
1799 spaces = get_spaces (*str);
1801 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1802 *str = spaces ? spaces : *str;
1805 if (!more)
1807 free (spaces);
1808 *str = NULL;
1812 /* Print the list of candidate FNS in an error message. FNS can also
1813 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1815 void
1816 print_candidates (tree fns)
1818 const char *str = NULL;
1819 print_candidates_1 (fns, false, &str);
1820 gcc_assert (str == NULL);
1823 /* Returns the template (one of the functions given by TEMPLATE_ID)
1824 which can be specialized to match the indicated DECL with the
1825 explicit template args given in TEMPLATE_ID. The DECL may be
1826 NULL_TREE if none is available. In that case, the functions in
1827 TEMPLATE_ID are non-members.
1829 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1830 specialization of a member template.
1832 The TEMPLATE_COUNT is the number of references to qualifying
1833 template classes that appeared in the name of the function. See
1834 check_explicit_specialization for a more accurate description.
1836 TSK indicates what kind of template declaration (if any) is being
1837 declared. TSK_TEMPLATE indicates that the declaration given by
1838 DECL, though a FUNCTION_DECL, has template parameters, and is
1839 therefore a template function.
1841 The template args (those explicitly specified and those deduced)
1842 are output in a newly created vector *TARGS_OUT.
1844 If it is impossible to determine the result, an error message is
1845 issued. The error_mark_node is returned to indicate failure. */
1847 static tree
1848 determine_specialization (tree template_id,
1849 tree decl,
1850 tree* targs_out,
1851 int need_member_template,
1852 int template_count,
1853 tmpl_spec_kind tsk)
1855 tree fns;
1856 tree targs;
1857 tree explicit_targs;
1858 tree candidates = NULL_TREE;
1859 /* A TREE_LIST of templates of which DECL may be a specialization.
1860 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1861 corresponding TREE_PURPOSE is the set of template arguments that,
1862 when used to instantiate the template, would produce a function
1863 with the signature of DECL. */
1864 tree templates = NULL_TREE;
1865 int header_count;
1866 cp_binding_level *b;
1868 *targs_out = NULL_TREE;
1870 if (template_id == error_mark_node || decl == error_mark_node)
1871 return error_mark_node;
1873 /* We shouldn't be specializing a member template of an
1874 unspecialized class template; we already gave an error in
1875 check_specialization_scope, now avoid crashing. */
1876 if (template_count && DECL_CLASS_SCOPE_P (decl)
1877 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1879 gcc_assert (errorcount);
1880 return error_mark_node;
1883 fns = TREE_OPERAND (template_id, 0);
1884 explicit_targs = TREE_OPERAND (template_id, 1);
1886 if (fns == error_mark_node)
1887 return error_mark_node;
1889 /* Check for baselinks. */
1890 if (BASELINK_P (fns))
1891 fns = BASELINK_FUNCTIONS (fns);
1893 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
1895 error ("%qD is not a function template", fns);
1896 return error_mark_node;
1898 else if (VAR_P (decl) && !variable_template_p (fns))
1900 error ("%qD is not a variable template", fns);
1901 return error_mark_node;
1904 /* Count the number of template headers specified for this
1905 specialization. */
1906 header_count = 0;
1907 for (b = current_binding_level;
1908 b->kind == sk_template_parms;
1909 b = b->level_chain)
1910 ++header_count;
1912 if (variable_template_p (fns))
1913 templates = tree_cons (explicit_targs, fns, templates);
1914 else for (; fns; fns = OVL_NEXT (fns))
1916 tree fn = OVL_CURRENT (fns);
1918 if (TREE_CODE (fn) == TEMPLATE_DECL)
1920 tree decl_arg_types;
1921 tree fn_arg_types;
1922 tree insttype;
1924 /* In case of explicit specialization, we need to check if
1925 the number of template headers appearing in the specialization
1926 is correct. This is usually done in check_explicit_specialization,
1927 but the check done there cannot be exhaustive when specializing
1928 member functions. Consider the following code:
1930 template <> void A<int>::f(int);
1931 template <> template <> void A<int>::f(int);
1933 Assuming that A<int> is not itself an explicit specialization
1934 already, the first line specializes "f" which is a non-template
1935 member function, whilst the second line specializes "f" which
1936 is a template member function. So both lines are syntactically
1937 correct, and check_explicit_specialization does not reject
1938 them.
1940 Here, we can do better, as we are matching the specialization
1941 against the declarations. We count the number of template
1942 headers, and we check if they match TEMPLATE_COUNT + 1
1943 (TEMPLATE_COUNT is the number of qualifying template classes,
1944 plus there must be another header for the member template
1945 itself).
1947 Notice that if header_count is zero, this is not a
1948 specialization but rather a template instantiation, so there
1949 is no check we can perform here. */
1950 if (header_count && header_count != template_count + 1)
1951 continue;
1953 /* Check that the number of template arguments at the
1954 innermost level for DECL is the same as for FN. */
1955 if (current_binding_level->kind == sk_template_parms
1956 && !current_binding_level->explicit_spec_p
1957 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1958 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1959 (current_template_parms))))
1960 continue;
1962 /* DECL might be a specialization of FN. */
1963 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1964 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1966 /* For a non-static member function, we need to make sure
1967 that the const qualification is the same. Since
1968 get_bindings does not try to merge the "this" parameter,
1969 we must do the comparison explicitly. */
1970 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1971 && !same_type_p (TREE_VALUE (fn_arg_types),
1972 TREE_VALUE (decl_arg_types)))
1973 continue;
1975 /* Skip the "this" parameter and, for constructors of
1976 classes with virtual bases, the VTT parameter. A
1977 full specialization of a constructor will have a VTT
1978 parameter, but a template never will. */
1979 decl_arg_types
1980 = skip_artificial_parms_for (decl, decl_arg_types);
1981 fn_arg_types
1982 = skip_artificial_parms_for (fn, fn_arg_types);
1984 /* Function templates cannot be specializations; there are
1985 no partial specializations of functions. Therefore, if
1986 the type of DECL does not match FN, there is no
1987 match. */
1988 if (tsk == tsk_template)
1990 if (compparms (fn_arg_types, decl_arg_types))
1991 candidates = tree_cons (NULL_TREE, fn, candidates);
1992 continue;
1995 /* See whether this function might be a specialization of this
1996 template. Suppress access control because we might be trying
1997 to make this specialization a friend, and we have already done
1998 access control for the declaration of the specialization. */
1999 push_deferring_access_checks (dk_no_check);
2000 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2001 pop_deferring_access_checks ();
2003 if (!targs)
2004 /* We cannot deduce template arguments that when used to
2005 specialize TMPL will produce DECL. */
2006 continue;
2008 /* Make sure that the deduced arguments actually work. */
2009 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2010 if (insttype == error_mark_node)
2011 continue;
2012 fn_arg_types
2013 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2014 if (!compparms (fn_arg_types, decl_arg_types))
2015 continue;
2017 /* Save this template, and the arguments deduced. */
2018 templates = tree_cons (targs, fn, templates);
2020 else if (need_member_template)
2021 /* FN is an ordinary member function, and we need a
2022 specialization of a member template. */
2024 else if (TREE_CODE (fn) != FUNCTION_DECL)
2025 /* We can get IDENTIFIER_NODEs here in certain erroneous
2026 cases. */
2028 else if (!DECL_FUNCTION_MEMBER_P (fn))
2029 /* This is just an ordinary non-member function. Nothing can
2030 be a specialization of that. */
2032 else if (DECL_ARTIFICIAL (fn))
2033 /* Cannot specialize functions that are created implicitly. */
2035 else
2037 tree decl_arg_types;
2039 /* This is an ordinary member function. However, since
2040 we're here, we can assume its enclosing class is a
2041 template class. For example,
2043 template <typename T> struct S { void f(); };
2044 template <> void S<int>::f() {}
2046 Here, S<int>::f is a non-template, but S<int> is a
2047 template class. If FN has the same type as DECL, we
2048 might be in business. */
2050 if (!DECL_TEMPLATE_INFO (fn))
2051 /* Its enclosing class is an explicit specialization
2052 of a template class. This is not a candidate. */
2053 continue;
2055 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2056 TREE_TYPE (TREE_TYPE (fn))))
2057 /* The return types differ. */
2058 continue;
2060 /* Adjust the type of DECL in case FN is a static member. */
2061 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2062 if (DECL_STATIC_FUNCTION_P (fn)
2063 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2064 decl_arg_types = TREE_CHAIN (decl_arg_types);
2066 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2067 decl_arg_types))
2068 /* They match! */
2069 candidates = tree_cons (NULL_TREE, fn, candidates);
2073 if (templates && TREE_CHAIN (templates))
2075 /* We have:
2077 [temp.expl.spec]
2079 It is possible for a specialization with a given function
2080 signature to be instantiated from more than one function
2081 template. In such cases, explicit specification of the
2082 template arguments must be used to uniquely identify the
2083 function template specialization being specialized.
2085 Note that here, there's no suggestion that we're supposed to
2086 determine which of the candidate templates is most
2087 specialized. However, we, also have:
2089 [temp.func.order]
2091 Partial ordering of overloaded function template
2092 declarations is used in the following contexts to select
2093 the function template to which a function template
2094 specialization refers:
2096 -- when an explicit specialization refers to a function
2097 template.
2099 So, we do use the partial ordering rules, at least for now.
2100 This extension can only serve to make invalid programs valid,
2101 so it's safe. And, there is strong anecdotal evidence that
2102 the committee intended the partial ordering rules to apply;
2103 the EDG front end has that behavior, and John Spicer claims
2104 that the committee simply forgot to delete the wording in
2105 [temp.expl.spec]. */
2106 tree tmpl = most_specialized_instantiation (templates);
2107 if (tmpl != error_mark_node)
2109 templates = tmpl;
2110 TREE_CHAIN (templates) = NULL_TREE;
2114 if (templates == NULL_TREE && candidates == NULL_TREE)
2116 error ("template-id %qD for %q+D does not match any template "
2117 "declaration", template_id, decl);
2118 if (header_count && header_count != template_count + 1)
2119 inform (input_location, "saw %d %<template<>%>, need %d for "
2120 "specializing a member function template",
2121 header_count, template_count + 1);
2122 return error_mark_node;
2124 else if ((templates && TREE_CHAIN (templates))
2125 || (candidates && TREE_CHAIN (candidates))
2126 || (templates && candidates))
2128 error ("ambiguous template specialization %qD for %q+D",
2129 template_id, decl);
2130 candidates = chainon (candidates, templates);
2131 print_candidates (candidates);
2132 return error_mark_node;
2135 /* We have one, and exactly one, match. */
2136 if (candidates)
2138 tree fn = TREE_VALUE (candidates);
2139 *targs_out = copy_node (DECL_TI_ARGS (fn));
2140 /* DECL is a re-declaration or partial instantiation of a template
2141 function. */
2142 if (TREE_CODE (fn) == TEMPLATE_DECL)
2143 return fn;
2144 /* It was a specialization of an ordinary member function in a
2145 template class. */
2146 return DECL_TI_TEMPLATE (fn);
2149 /* It was a specialization of a template. */
2150 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2151 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2153 *targs_out = copy_node (targs);
2154 SET_TMPL_ARGS_LEVEL (*targs_out,
2155 TMPL_ARGS_DEPTH (*targs_out),
2156 TREE_PURPOSE (templates));
2158 else
2159 *targs_out = TREE_PURPOSE (templates);
2160 return TREE_VALUE (templates);
2163 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2164 but with the default argument values filled in from those in the
2165 TMPL_TYPES. */
2167 static tree
2168 copy_default_args_to_explicit_spec_1 (tree spec_types,
2169 tree tmpl_types)
2171 tree new_spec_types;
2173 if (!spec_types)
2174 return NULL_TREE;
2176 if (spec_types == void_list_node)
2177 return void_list_node;
2179 /* Substitute into the rest of the list. */
2180 new_spec_types =
2181 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2182 TREE_CHAIN (tmpl_types));
2184 /* Add the default argument for this parameter. */
2185 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2186 TREE_VALUE (spec_types),
2187 new_spec_types);
2190 /* DECL is an explicit specialization. Replicate default arguments
2191 from the template it specializes. (That way, code like:
2193 template <class T> void f(T = 3);
2194 template <> void f(double);
2195 void g () { f (); }
2197 works, as required.) An alternative approach would be to look up
2198 the correct default arguments at the call-site, but this approach
2199 is consistent with how implicit instantiations are handled. */
2201 static void
2202 copy_default_args_to_explicit_spec (tree decl)
2204 tree tmpl;
2205 tree spec_types;
2206 tree tmpl_types;
2207 tree new_spec_types;
2208 tree old_type;
2209 tree new_type;
2210 tree t;
2211 tree object_type = NULL_TREE;
2212 tree in_charge = NULL_TREE;
2213 tree vtt = NULL_TREE;
2215 /* See if there's anything we need to do. */
2216 tmpl = DECL_TI_TEMPLATE (decl);
2217 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2218 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2219 if (TREE_PURPOSE (t))
2220 break;
2221 if (!t)
2222 return;
2224 old_type = TREE_TYPE (decl);
2225 spec_types = TYPE_ARG_TYPES (old_type);
2227 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2229 /* Remove the this pointer, but remember the object's type for
2230 CV quals. */
2231 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2232 spec_types = TREE_CHAIN (spec_types);
2233 tmpl_types = TREE_CHAIN (tmpl_types);
2235 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2237 /* DECL may contain more parameters than TMPL due to the extra
2238 in-charge parameter in constructors and destructors. */
2239 in_charge = spec_types;
2240 spec_types = TREE_CHAIN (spec_types);
2242 if (DECL_HAS_VTT_PARM_P (decl))
2244 vtt = spec_types;
2245 spec_types = TREE_CHAIN (spec_types);
2249 /* Compute the merged default arguments. */
2250 new_spec_types =
2251 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2253 /* Compute the new FUNCTION_TYPE. */
2254 if (object_type)
2256 if (vtt)
2257 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2258 TREE_VALUE (vtt),
2259 new_spec_types);
2261 if (in_charge)
2262 /* Put the in-charge parameter back. */
2263 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2264 TREE_VALUE (in_charge),
2265 new_spec_types);
2267 new_type = build_method_type_directly (object_type,
2268 TREE_TYPE (old_type),
2269 new_spec_types);
2271 else
2272 new_type = build_function_type (TREE_TYPE (old_type),
2273 new_spec_types);
2274 new_type = cp_build_type_attribute_variant (new_type,
2275 TYPE_ATTRIBUTES (old_type));
2276 new_type = build_exception_variant (new_type,
2277 TYPE_RAISES_EXCEPTIONS (old_type));
2279 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2280 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2282 TREE_TYPE (decl) = new_type;
2285 /* Return the number of template headers we expect to see for a definition
2286 or specialization of CTYPE or one of its non-template members. */
2289 num_template_headers_for_class (tree ctype)
2291 int num_templates = 0;
2293 while (ctype && CLASS_TYPE_P (ctype))
2295 /* You're supposed to have one `template <...>' for every
2296 template class, but you don't need one for a full
2297 specialization. For example:
2299 template <class T> struct S{};
2300 template <> struct S<int> { void f(); };
2301 void S<int>::f () {}
2303 is correct; there shouldn't be a `template <>' for the
2304 definition of `S<int>::f'. */
2305 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2306 /* If CTYPE does not have template information of any
2307 kind, then it is not a template, nor is it nested
2308 within a template. */
2309 break;
2310 if (explicit_class_specialization_p (ctype))
2311 break;
2312 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2313 ++num_templates;
2315 ctype = TYPE_CONTEXT (ctype);
2318 return num_templates;
2321 /* Do a simple sanity check on the template headers that precede the
2322 variable declaration DECL. */
2324 void
2325 check_template_variable (tree decl)
2327 tree ctx = CP_DECL_CONTEXT (decl);
2328 int wanted = num_template_headers_for_class (ctx);
2329 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2330 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2332 if (cxx_dialect < cxx14)
2333 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2334 "variable templates only available with "
2335 "-std=c++14 or -std=gnu++14");
2337 // Namespace-scope variable templates should have a template header.
2338 ++wanted;
2340 if (template_header_count > wanted)
2342 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2343 "too many template headers for %D (should be %d)",
2344 decl, wanted);
2345 if (warned && CLASS_TYPE_P (ctx)
2346 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2347 inform (DECL_SOURCE_LOCATION (decl),
2348 "members of an explicitly specialized class are defined "
2349 "without a template header");
2353 /* Check to see if the function just declared, as indicated in
2354 DECLARATOR, and in DECL, is a specialization of a function
2355 template. We may also discover that the declaration is an explicit
2356 instantiation at this point.
2358 Returns DECL, or an equivalent declaration that should be used
2359 instead if all goes well. Issues an error message if something is
2360 amiss. Returns error_mark_node if the error is not easily
2361 recoverable.
2363 FLAGS is a bitmask consisting of the following flags:
2365 2: The function has a definition.
2366 4: The function is a friend.
2368 The TEMPLATE_COUNT is the number of references to qualifying
2369 template classes that appeared in the name of the function. For
2370 example, in
2372 template <class T> struct S { void f(); };
2373 void S<int>::f();
2375 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2376 classes are not counted in the TEMPLATE_COUNT, so that in
2378 template <class T> struct S {};
2379 template <> struct S<int> { void f(); }
2380 template <> void S<int>::f();
2382 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2383 invalid; there should be no template <>.)
2385 If the function is a specialization, it is marked as such via
2386 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2387 is set up correctly, and it is added to the list of specializations
2388 for that template. */
2390 tree
2391 check_explicit_specialization (tree declarator,
2392 tree decl,
2393 int template_count,
2394 int flags)
2396 int have_def = flags & 2;
2397 int is_friend = flags & 4;
2398 int specialization = 0;
2399 int explicit_instantiation = 0;
2400 int member_specialization = 0;
2401 tree ctype = DECL_CLASS_CONTEXT (decl);
2402 tree dname = DECL_NAME (decl);
2403 tmpl_spec_kind tsk;
2405 if (is_friend)
2407 if (!processing_specialization)
2408 tsk = tsk_none;
2409 else
2410 tsk = tsk_excessive_parms;
2412 else
2413 tsk = current_tmpl_spec_kind (template_count);
2415 switch (tsk)
2417 case tsk_none:
2418 if (processing_specialization)
2420 specialization = 1;
2421 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2423 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2425 if (is_friend)
2426 /* This could be something like:
2428 template <class T> void f(T);
2429 class S { friend void f<>(int); } */
2430 specialization = 1;
2431 else
2433 /* This case handles bogus declarations like template <>
2434 template <class T> void f<int>(); */
2436 error ("template-id %qD in declaration of primary template",
2437 declarator);
2438 return decl;
2441 break;
2443 case tsk_invalid_member_spec:
2444 /* The error has already been reported in
2445 check_specialization_scope. */
2446 return error_mark_node;
2448 case tsk_invalid_expl_inst:
2449 error ("template parameter list used in explicit instantiation");
2451 /* Fall through. */
2453 case tsk_expl_inst:
2454 if (have_def)
2455 error ("definition provided for explicit instantiation");
2457 explicit_instantiation = 1;
2458 break;
2460 case tsk_excessive_parms:
2461 case tsk_insufficient_parms:
2462 if (tsk == tsk_excessive_parms)
2463 error ("too many template parameter lists in declaration of %qD",
2464 decl);
2465 else if (template_header_count)
2466 error("too few template parameter lists in declaration of %qD", decl);
2467 else
2468 error("explicit specialization of %qD must be introduced by "
2469 "%<template <>%>", decl);
2471 /* Fall through. */
2472 case tsk_expl_spec:
2473 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2474 /* In cases like template<> constexpr bool v = true;
2475 We'll give an error in check_template_variable. */
2476 break;
2478 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2479 if (ctype)
2480 member_specialization = 1;
2481 else
2482 specialization = 1;
2483 break;
2485 case tsk_template:
2486 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2488 /* This case handles bogus declarations like template <>
2489 template <class T> void f<int>(); */
2491 if (!uses_template_parms (declarator))
2492 error ("template-id %qD in declaration of primary template",
2493 declarator);
2494 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2496 /* Partial specialization of variable template. */
2497 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2498 specialization = 1;
2499 goto ok;
2501 else if (cxx_dialect < cxx14)
2502 error ("non-type partial specialization %qD "
2503 "is not allowed", declarator);
2504 else
2505 error ("non-class, non-variable partial specialization %qD "
2506 "is not allowed", declarator);
2507 return decl;
2508 ok:;
2511 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2512 /* This is a specialization of a member template, without
2513 specialization the containing class. Something like:
2515 template <class T> struct S {
2516 template <class U> void f (U);
2518 template <> template <class U> void S<int>::f(U) {}
2520 That's a specialization -- but of the entire template. */
2521 specialization = 1;
2522 break;
2524 default:
2525 gcc_unreachable ();
2528 if ((specialization || member_specialization)
2529 /* This doesn't apply to variable templates. */
2530 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2531 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2533 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2534 for (; t; t = TREE_CHAIN (t))
2535 if (TREE_PURPOSE (t))
2537 permerror (input_location,
2538 "default argument specified in explicit specialization");
2539 break;
2543 if (specialization || member_specialization || explicit_instantiation)
2545 tree tmpl = NULL_TREE;
2546 tree targs = NULL_TREE;
2547 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2549 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2550 if (!was_template_id)
2552 tree fns;
2554 gcc_assert (identifier_p (declarator));
2555 if (ctype)
2556 fns = dname;
2557 else
2559 /* If there is no class context, the explicit instantiation
2560 must be at namespace scope. */
2561 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2563 /* Find the namespace binding, using the declaration
2564 context. */
2565 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2566 false, true);
2567 if (fns == error_mark_node || !is_overloaded_fn (fns))
2569 error ("%qD is not a template function", dname);
2570 fns = error_mark_node;
2572 else
2574 tree fn = OVL_CURRENT (fns);
2575 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2576 CP_DECL_CONTEXT (fn)))
2577 error ("%qD is not declared in %qD",
2578 decl, current_namespace);
2582 declarator = lookup_template_function (fns, NULL_TREE);
2585 if (declarator == error_mark_node)
2586 return error_mark_node;
2588 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2590 if (!explicit_instantiation)
2591 /* A specialization in class scope. This is invalid,
2592 but the error will already have been flagged by
2593 check_specialization_scope. */
2594 return error_mark_node;
2595 else
2597 /* It's not valid to write an explicit instantiation in
2598 class scope, e.g.:
2600 class C { template void f(); }
2602 This case is caught by the parser. However, on
2603 something like:
2605 template class C { void f(); };
2607 (which is invalid) we can get here. The error will be
2608 issued later. */
2612 return decl;
2614 else if (ctype != NULL_TREE
2615 && (identifier_p (TREE_OPERAND (declarator, 0))))
2617 // We'll match variable templates in start_decl.
2618 if (VAR_P (decl))
2619 return decl;
2621 /* Find the list of functions in ctype that have the same
2622 name as the declared function. */
2623 tree name = TREE_OPERAND (declarator, 0);
2624 tree fns = NULL_TREE;
2625 int idx;
2627 if (constructor_name_p (name, ctype))
2629 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2631 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2632 : !CLASSTYPE_DESTRUCTORS (ctype))
2634 /* From [temp.expl.spec]:
2636 If such an explicit specialization for the member
2637 of a class template names an implicitly-declared
2638 special member function (clause _special_), the
2639 program is ill-formed.
2641 Similar language is found in [temp.explicit]. */
2642 error ("specialization of implicitly-declared special member function");
2643 return error_mark_node;
2646 name = is_constructor ? ctor_identifier : dtor_identifier;
2649 if (!DECL_CONV_FN_P (decl))
2651 idx = lookup_fnfields_1 (ctype, name);
2652 if (idx >= 0)
2653 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2655 else
2657 vec<tree, va_gc> *methods;
2658 tree ovl;
2660 /* For a type-conversion operator, we cannot do a
2661 name-based lookup. We might be looking for `operator
2662 int' which will be a specialization of `operator T'.
2663 So, we find *all* the conversion operators, and then
2664 select from them. */
2665 fns = NULL_TREE;
2667 methods = CLASSTYPE_METHOD_VEC (ctype);
2668 if (methods)
2669 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2670 methods->iterate (idx, &ovl);
2671 ++idx)
2673 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2674 /* There are no more conversion functions. */
2675 break;
2677 /* Glue all these conversion functions together
2678 with those we already have. */
2679 for (; ovl; ovl = OVL_NEXT (ovl))
2680 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2684 if (fns == NULL_TREE)
2686 error ("no member function %qD declared in %qT", name, ctype);
2687 return error_mark_node;
2689 else
2690 TREE_OPERAND (declarator, 0) = fns;
2693 /* Figure out what exactly is being specialized at this point.
2694 Note that for an explicit instantiation, even one for a
2695 member function, we cannot tell apriori whether the
2696 instantiation is for a member template, or just a member
2697 function of a template class. Even if a member template is
2698 being instantiated, the member template arguments may be
2699 elided if they can be deduced from the rest of the
2700 declaration. */
2701 tmpl = determine_specialization (declarator, decl,
2702 &targs,
2703 member_specialization,
2704 template_count,
2705 tsk);
2707 if (!tmpl || tmpl == error_mark_node)
2708 /* We couldn't figure out what this declaration was
2709 specializing. */
2710 return error_mark_node;
2711 else
2713 tree gen_tmpl = most_general_template (tmpl);
2715 if (explicit_instantiation)
2717 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2718 is done by do_decl_instantiation later. */
2720 int arg_depth = TMPL_ARGS_DEPTH (targs);
2721 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2723 if (arg_depth > parm_depth)
2725 /* If TMPL is not the most general template (for
2726 example, if TMPL is a friend template that is
2727 injected into namespace scope), then there will
2728 be too many levels of TARGS. Remove some of them
2729 here. */
2730 int i;
2731 tree new_targs;
2733 new_targs = make_tree_vec (parm_depth);
2734 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2735 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2736 = TREE_VEC_ELT (targs, i);
2737 targs = new_targs;
2740 return instantiate_template (tmpl, targs, tf_error);
2743 /* If we thought that the DECL was a member function, but it
2744 turns out to be specializing a static member function,
2745 make DECL a static member function as well. */
2746 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2747 && DECL_STATIC_FUNCTION_P (tmpl)
2748 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2749 revert_static_member_fn (decl);
2751 /* If this is a specialization of a member template of a
2752 template class, we want to return the TEMPLATE_DECL, not
2753 the specialization of it. */
2754 if (tsk == tsk_template && !was_template_id)
2756 tree result = DECL_TEMPLATE_RESULT (tmpl);
2757 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2758 DECL_INITIAL (result) = NULL_TREE;
2759 if (have_def)
2761 tree parm;
2762 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2763 DECL_SOURCE_LOCATION (result)
2764 = DECL_SOURCE_LOCATION (decl);
2765 /* We want to use the argument list specified in the
2766 definition, not in the original declaration. */
2767 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2768 for (parm = DECL_ARGUMENTS (result); parm;
2769 parm = DECL_CHAIN (parm))
2770 DECL_CONTEXT (parm) = result;
2772 return register_specialization (tmpl, gen_tmpl, targs,
2773 is_friend, 0);
2776 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2777 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2779 if (was_template_id)
2780 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
2782 /* Inherit default function arguments from the template
2783 DECL is specializing. */
2784 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2785 copy_default_args_to_explicit_spec (decl);
2787 /* This specialization has the same protection as the
2788 template it specializes. */
2789 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2790 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2792 /* 7.1.1-1 [dcl.stc]
2794 A storage-class-specifier shall not be specified in an
2795 explicit specialization...
2797 The parser rejects these, so unless action is taken here,
2798 explicit function specializations will always appear with
2799 global linkage.
2801 The action recommended by the C++ CWG in response to C++
2802 defect report 605 is to make the storage class and linkage
2803 of the explicit specialization match the templated function:
2805 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2807 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2809 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2810 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2812 /* This specialization has the same linkage and visibility as
2813 the function template it specializes. */
2814 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2815 if (! TREE_PUBLIC (decl))
2817 DECL_INTERFACE_KNOWN (decl) = 1;
2818 DECL_NOT_REALLY_EXTERN (decl) = 1;
2820 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2821 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2823 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2824 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2828 /* If DECL is a friend declaration, declared using an
2829 unqualified name, the namespace associated with DECL may
2830 have been set incorrectly. For example, in:
2832 template <typename T> void f(T);
2833 namespace N {
2834 struct S { friend void f<int>(int); }
2837 we will have set the DECL_CONTEXT for the friend
2838 declaration to N, rather than to the global namespace. */
2839 if (DECL_NAMESPACE_SCOPE_P (decl))
2840 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2842 if (is_friend && !have_def)
2843 /* This is not really a declaration of a specialization.
2844 It's just the name of an instantiation. But, it's not
2845 a request for an instantiation, either. */
2846 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2847 else if (TREE_CODE (decl) == FUNCTION_DECL)
2848 /* A specialization is not necessarily COMDAT. */
2849 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
2850 && DECL_DECLARED_INLINE_P (decl));
2851 else if (TREE_CODE (decl) == VAR_DECL)
2852 DECL_COMDAT (decl) = false;
2854 /* Register this specialization so that we can find it
2855 again. */
2856 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2858 /* A 'structor should already have clones. */
2859 gcc_assert (decl == error_mark_node
2860 || variable_template_p (tmpl)
2861 || !(DECL_CONSTRUCTOR_P (decl)
2862 || DECL_DESTRUCTOR_P (decl))
2863 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2867 return decl;
2870 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2871 parameters. These are represented in the same format used for
2872 DECL_TEMPLATE_PARMS. */
2875 comp_template_parms (const_tree parms1, const_tree parms2)
2877 const_tree p1;
2878 const_tree p2;
2880 if (parms1 == parms2)
2881 return 1;
2883 for (p1 = parms1, p2 = parms2;
2884 p1 != NULL_TREE && p2 != NULL_TREE;
2885 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2887 tree t1 = TREE_VALUE (p1);
2888 tree t2 = TREE_VALUE (p2);
2889 int i;
2891 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2892 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2894 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2895 return 0;
2897 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2899 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2900 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2902 /* If either of the template parameters are invalid, assume
2903 they match for the sake of error recovery. */
2904 if (error_operand_p (parm1) || error_operand_p (parm2))
2905 return 1;
2907 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2908 return 0;
2910 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2911 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2912 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2913 continue;
2914 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2915 return 0;
2919 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2920 /* One set of parameters has more parameters lists than the
2921 other. */
2922 return 0;
2924 return 1;
2927 /* Determine whether PARM is a parameter pack. */
2929 bool
2930 template_parameter_pack_p (const_tree parm)
2932 /* Determine if we have a non-type template parameter pack. */
2933 if (TREE_CODE (parm) == PARM_DECL)
2934 return (DECL_TEMPLATE_PARM_P (parm)
2935 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2936 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2937 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2939 /* If this is a list of template parameters, we could get a
2940 TYPE_DECL or a TEMPLATE_DECL. */
2941 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2942 parm = TREE_TYPE (parm);
2944 /* Otherwise it must be a type template parameter. */
2945 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2946 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2947 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2950 /* Determine if T is a function parameter pack. */
2952 bool
2953 function_parameter_pack_p (const_tree t)
2955 if (t && TREE_CODE (t) == PARM_DECL)
2956 return DECL_PACK_P (t);
2957 return false;
2960 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2961 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2963 tree
2964 get_function_template_decl (const_tree primary_func_tmpl_inst)
2966 if (! primary_func_tmpl_inst
2967 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2968 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2969 return NULL;
2971 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2974 /* Return true iff the function parameter PARAM_DECL was expanded
2975 from the function parameter pack PACK. */
2977 bool
2978 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2980 if (DECL_ARTIFICIAL (param_decl)
2981 || !function_parameter_pack_p (pack))
2982 return false;
2984 /* The parameter pack and its pack arguments have the same
2985 DECL_PARM_INDEX. */
2986 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2989 /* Determine whether ARGS describes a variadic template args list,
2990 i.e., one that is terminated by a template argument pack. */
2992 static bool
2993 template_args_variadic_p (tree args)
2995 int nargs;
2996 tree last_parm;
2998 if (args == NULL_TREE)
2999 return false;
3001 args = INNERMOST_TEMPLATE_ARGS (args);
3002 nargs = TREE_VEC_LENGTH (args);
3004 if (nargs == 0)
3005 return false;
3007 last_parm = TREE_VEC_ELT (args, nargs - 1);
3009 return ARGUMENT_PACK_P (last_parm);
3012 /* Generate a new name for the parameter pack name NAME (an
3013 IDENTIFIER_NODE) that incorporates its */
3015 static tree
3016 make_ith_pack_parameter_name (tree name, int i)
3018 /* Munge the name to include the parameter index. */
3019 #define NUMBUF_LEN 128
3020 char numbuf[NUMBUF_LEN];
3021 char* newname;
3022 int newname_len;
3024 if (name == NULL_TREE)
3025 return name;
3026 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3027 newname_len = IDENTIFIER_LENGTH (name)
3028 + strlen (numbuf) + 2;
3029 newname = (char*)alloca (newname_len);
3030 snprintf (newname, newname_len,
3031 "%s#%i", IDENTIFIER_POINTER (name), i);
3032 return get_identifier (newname);
3035 /* Return true if T is a primary function, class or alias template
3036 instantiation. */
3038 bool
3039 primary_template_instantiation_p (const_tree t)
3041 if (!t)
3042 return false;
3044 if (TREE_CODE (t) == FUNCTION_DECL)
3045 return DECL_LANG_SPECIFIC (t)
3046 && DECL_TEMPLATE_INSTANTIATION (t)
3047 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3048 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3049 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3050 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3051 else if (alias_template_specialization_p (t))
3052 return true;
3053 return false;
3056 /* Return true if PARM is a template template parameter. */
3058 bool
3059 template_template_parameter_p (const_tree parm)
3061 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3064 /* Return true iff PARM is a DECL representing a type template
3065 parameter. */
3067 bool
3068 template_type_parameter_p (const_tree parm)
3070 return (parm
3071 && (TREE_CODE (parm) == TYPE_DECL
3072 || TREE_CODE (parm) == TEMPLATE_DECL)
3073 && DECL_TEMPLATE_PARM_P (parm));
3076 /* Return the template parameters of T if T is a
3077 primary template instantiation, NULL otherwise. */
3079 tree
3080 get_primary_template_innermost_parameters (const_tree t)
3082 tree parms = NULL, template_info = NULL;
3084 if ((template_info = get_template_info (t))
3085 && primary_template_instantiation_p (t))
3086 parms = INNERMOST_TEMPLATE_PARMS
3087 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3089 return parms;
3092 /* Return the template parameters of the LEVELth level from the full list
3093 of template parameters PARMS. */
3095 tree
3096 get_template_parms_at_level (tree parms, int level)
3098 tree p;
3099 if (!parms
3100 || TREE_CODE (parms) != TREE_LIST
3101 || level > TMPL_PARMS_DEPTH (parms))
3102 return NULL_TREE;
3104 for (p = parms; p; p = TREE_CHAIN (p))
3105 if (TMPL_PARMS_DEPTH (p) == level)
3106 return p;
3108 return NULL_TREE;
3111 /* Returns the template arguments of T if T is a template instantiation,
3112 NULL otherwise. */
3114 tree
3115 get_template_innermost_arguments (const_tree t)
3117 tree args = NULL, template_info = NULL;
3119 if ((template_info = get_template_info (t))
3120 && TI_ARGS (template_info))
3121 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3123 return args;
3126 /* Return the argument pack elements of T if T is a template argument pack,
3127 NULL otherwise. */
3129 tree
3130 get_template_argument_pack_elems (const_tree t)
3132 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3133 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3134 return NULL;
3136 return ARGUMENT_PACK_ARGS (t);
3139 /* Structure used to track the progress of find_parameter_packs_r. */
3140 struct find_parameter_pack_data
3142 /* TREE_LIST that will contain all of the parameter packs found by
3143 the traversal. */
3144 tree* parameter_packs;
3146 /* Set of AST nodes that have been visited by the traversal. */
3147 hash_set<tree> *visited;
3150 /* Identifies all of the argument packs that occur in a template
3151 argument and appends them to the TREE_LIST inside DATA, which is a
3152 find_parameter_pack_data structure. This is a subroutine of
3153 make_pack_expansion and uses_parameter_packs. */
3154 static tree
3155 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3157 tree t = *tp;
3158 struct find_parameter_pack_data* ppd =
3159 (struct find_parameter_pack_data*)data;
3160 bool parameter_pack_p = false;
3162 /* Handle type aliases/typedefs. */
3163 if (TYPE_ALIAS_P (t))
3165 if (TYPE_TEMPLATE_INFO (t))
3166 cp_walk_tree (&TYPE_TI_ARGS (t),
3167 &find_parameter_packs_r,
3168 ppd, ppd->visited);
3169 *walk_subtrees = 0;
3170 return NULL_TREE;
3173 /* Identify whether this is a parameter pack or not. */
3174 switch (TREE_CODE (t))
3176 case TEMPLATE_PARM_INDEX:
3177 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3178 parameter_pack_p = true;
3179 break;
3181 case TEMPLATE_TYPE_PARM:
3182 t = TYPE_MAIN_VARIANT (t);
3183 case TEMPLATE_TEMPLATE_PARM:
3184 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3185 parameter_pack_p = true;
3186 break;
3188 case FIELD_DECL:
3189 case PARM_DECL:
3190 if (DECL_PACK_P (t))
3192 /* We don't want to walk into the type of a PARM_DECL,
3193 because we don't want to see the type parameter pack. */
3194 *walk_subtrees = 0;
3195 parameter_pack_p = true;
3197 break;
3199 /* Look through a lambda capture proxy to the field pack. */
3200 case VAR_DECL:
3201 if (DECL_HAS_VALUE_EXPR_P (t))
3203 tree v = DECL_VALUE_EXPR (t);
3204 cp_walk_tree (&v,
3205 &find_parameter_packs_r,
3206 ppd, ppd->visited);
3207 *walk_subtrees = 0;
3209 break;
3211 case BASES:
3212 parameter_pack_p = true;
3213 break;
3214 default:
3215 /* Not a parameter pack. */
3216 break;
3219 if (parameter_pack_p)
3221 /* Add this parameter pack to the list. */
3222 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3225 if (TYPE_P (t))
3226 cp_walk_tree (&TYPE_CONTEXT (t),
3227 &find_parameter_packs_r, ppd, ppd->visited);
3229 /* This switch statement will return immediately if we don't find a
3230 parameter pack. */
3231 switch (TREE_CODE (t))
3233 case TEMPLATE_PARM_INDEX:
3234 return NULL_TREE;
3236 case BOUND_TEMPLATE_TEMPLATE_PARM:
3237 /* Check the template itself. */
3238 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3239 &find_parameter_packs_r, ppd, ppd->visited);
3240 /* Check the template arguments. */
3241 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3242 ppd->visited);
3243 *walk_subtrees = 0;
3244 return NULL_TREE;
3246 case TEMPLATE_TYPE_PARM:
3247 case TEMPLATE_TEMPLATE_PARM:
3248 return NULL_TREE;
3250 case PARM_DECL:
3251 return NULL_TREE;
3253 case RECORD_TYPE:
3254 if (TYPE_PTRMEMFUNC_P (t))
3255 return NULL_TREE;
3256 /* Fall through. */
3258 case UNION_TYPE:
3259 case ENUMERAL_TYPE:
3260 if (TYPE_TEMPLATE_INFO (t))
3261 cp_walk_tree (&TYPE_TI_ARGS (t),
3262 &find_parameter_packs_r, ppd, ppd->visited);
3264 *walk_subtrees = 0;
3265 return NULL_TREE;
3267 case CONSTRUCTOR:
3268 case TEMPLATE_DECL:
3269 cp_walk_tree (&TREE_TYPE (t),
3270 &find_parameter_packs_r, ppd, ppd->visited);
3271 return NULL_TREE;
3273 case TYPENAME_TYPE:
3274 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3275 ppd, ppd->visited);
3276 *walk_subtrees = 0;
3277 return NULL_TREE;
3279 case TYPE_PACK_EXPANSION:
3280 case EXPR_PACK_EXPANSION:
3281 *walk_subtrees = 0;
3282 return NULL_TREE;
3284 case INTEGER_TYPE:
3285 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3286 ppd, ppd->visited);
3287 *walk_subtrees = 0;
3288 return NULL_TREE;
3290 case IDENTIFIER_NODE:
3291 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3292 ppd->visited);
3293 *walk_subtrees = 0;
3294 return NULL_TREE;
3296 default:
3297 return NULL_TREE;
3300 return NULL_TREE;
3303 /* Determines if the expression or type T uses any parameter packs. */
3304 bool
3305 uses_parameter_packs (tree t)
3307 tree parameter_packs = NULL_TREE;
3308 struct find_parameter_pack_data ppd;
3309 ppd.parameter_packs = &parameter_packs;
3310 ppd.visited = new hash_set<tree>;
3311 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3312 delete ppd.visited;
3313 return parameter_packs != NULL_TREE;
3316 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3317 representation a base-class initializer into a parameter pack
3318 expansion. If all goes well, the resulting node will be an
3319 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3320 respectively. */
3321 tree
3322 make_pack_expansion (tree arg)
3324 tree result;
3325 tree parameter_packs = NULL_TREE;
3326 bool for_types = false;
3327 struct find_parameter_pack_data ppd;
3329 if (!arg || arg == error_mark_node)
3330 return arg;
3332 if (TREE_CODE (arg) == TREE_LIST)
3334 /* The only time we will see a TREE_LIST here is for a base
3335 class initializer. In this case, the TREE_PURPOSE will be a
3336 _TYPE node (representing the base class expansion we're
3337 initializing) and the TREE_VALUE will be a TREE_LIST
3338 containing the initialization arguments.
3340 The resulting expansion looks somewhat different from most
3341 expansions. Rather than returning just one _EXPANSION, we
3342 return a TREE_LIST whose TREE_PURPOSE is a
3343 TYPE_PACK_EXPANSION containing the bases that will be
3344 initialized. The TREE_VALUE will be identical to the
3345 original TREE_VALUE, which is a list of arguments that will
3346 be passed to each base. We do not introduce any new pack
3347 expansion nodes into the TREE_VALUE (although it is possible
3348 that some already exist), because the TREE_PURPOSE and
3349 TREE_VALUE all need to be expanded together with the same
3350 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3351 resulting TREE_PURPOSE will mention the parameter packs in
3352 both the bases and the arguments to the bases. */
3353 tree purpose;
3354 tree value;
3355 tree parameter_packs = NULL_TREE;
3357 /* Determine which parameter packs will be used by the base
3358 class expansion. */
3359 ppd.visited = new hash_set<tree>;
3360 ppd.parameter_packs = &parameter_packs;
3361 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3362 &ppd, ppd.visited);
3364 if (parameter_packs == NULL_TREE)
3366 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3367 delete ppd.visited;
3368 return error_mark_node;
3371 if (TREE_VALUE (arg) != void_type_node)
3373 /* Collect the sets of parameter packs used in each of the
3374 initialization arguments. */
3375 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3377 /* Determine which parameter packs will be expanded in this
3378 argument. */
3379 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3380 &ppd, ppd.visited);
3384 delete ppd.visited;
3386 /* Create the pack expansion type for the base type. */
3387 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3388 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3389 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3391 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3392 they will rarely be compared to anything. */
3393 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3395 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3398 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3399 for_types = true;
3401 /* Build the PACK_EXPANSION_* node. */
3402 result = for_types
3403 ? cxx_make_type (TYPE_PACK_EXPANSION)
3404 : make_node (EXPR_PACK_EXPANSION);
3405 SET_PACK_EXPANSION_PATTERN (result, arg);
3406 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3408 /* Propagate type and const-expression information. */
3409 TREE_TYPE (result) = TREE_TYPE (arg);
3410 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3412 else
3413 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3414 they will rarely be compared to anything. */
3415 SET_TYPE_STRUCTURAL_EQUALITY (result);
3417 /* Determine which parameter packs will be expanded. */
3418 ppd.parameter_packs = &parameter_packs;
3419 ppd.visited = new hash_set<tree>;
3420 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3421 delete ppd.visited;
3423 /* Make sure we found some parameter packs. */
3424 if (parameter_packs == NULL_TREE)
3426 if (TYPE_P (arg))
3427 error ("expansion pattern %<%T%> contains no argument packs", arg);
3428 else
3429 error ("expansion pattern %<%E%> contains no argument packs", arg);
3430 return error_mark_node;
3432 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3434 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3436 return result;
3439 /* Checks T for any "bare" parameter packs, which have not yet been
3440 expanded, and issues an error if any are found. This operation can
3441 only be done on full expressions or types (e.g., an expression
3442 statement, "if" condition, etc.), because we could have expressions like:
3444 foo(f(g(h(args)))...)
3446 where "args" is a parameter pack. check_for_bare_parameter_packs
3447 should not be called for the subexpressions args, h(args),
3448 g(h(args)), or f(g(h(args))), because we would produce erroneous
3449 error messages.
3451 Returns TRUE and emits an error if there were bare parameter packs,
3452 returns FALSE otherwise. */
3453 bool
3454 check_for_bare_parameter_packs (tree t)
3456 tree parameter_packs = NULL_TREE;
3457 struct find_parameter_pack_data ppd;
3459 if (!processing_template_decl || !t || t == error_mark_node)
3460 return false;
3462 if (TREE_CODE (t) == TYPE_DECL)
3463 t = TREE_TYPE (t);
3465 ppd.parameter_packs = &parameter_packs;
3466 ppd.visited = new hash_set<tree>;
3467 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3468 delete ppd.visited;
3470 if (parameter_packs)
3472 error ("parameter packs not expanded with %<...%>:");
3473 while (parameter_packs)
3475 tree pack = TREE_VALUE (parameter_packs);
3476 tree name = NULL_TREE;
3478 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3479 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3480 name = TYPE_NAME (pack);
3481 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3482 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3483 else
3484 name = DECL_NAME (pack);
3486 if (name)
3487 inform (input_location, " %qD", name);
3488 else
3489 inform (input_location, " <anonymous>");
3491 parameter_packs = TREE_CHAIN (parameter_packs);
3494 return true;
3497 return false;
3500 /* Expand any parameter packs that occur in the template arguments in
3501 ARGS. */
3502 tree
3503 expand_template_argument_pack (tree args)
3505 tree result_args = NULL_TREE;
3506 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3507 int num_result_args = -1;
3508 int non_default_args_count = -1;
3510 /* First, determine if we need to expand anything, and the number of
3511 slots we'll need. */
3512 for (in_arg = 0; in_arg < nargs; ++in_arg)
3514 tree arg = TREE_VEC_ELT (args, in_arg);
3515 if (arg == NULL_TREE)
3516 return args;
3517 if (ARGUMENT_PACK_P (arg))
3519 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3520 if (num_result_args < 0)
3521 num_result_args = in_arg + num_packed;
3522 else
3523 num_result_args += num_packed;
3525 else
3527 if (num_result_args >= 0)
3528 num_result_args++;
3532 /* If no expansion is necessary, we're done. */
3533 if (num_result_args < 0)
3534 return args;
3536 /* Expand arguments. */
3537 result_args = make_tree_vec (num_result_args);
3538 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3539 non_default_args_count =
3540 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3541 for (in_arg = 0; in_arg < nargs; ++in_arg)
3543 tree arg = TREE_VEC_ELT (args, in_arg);
3544 if (ARGUMENT_PACK_P (arg))
3546 tree packed = ARGUMENT_PACK_ARGS (arg);
3547 int i, num_packed = TREE_VEC_LENGTH (packed);
3548 for (i = 0; i < num_packed; ++i, ++out_arg)
3549 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3550 if (non_default_args_count > 0)
3551 non_default_args_count += num_packed - 1;
3553 else
3555 TREE_VEC_ELT (result_args, out_arg) = arg;
3556 ++out_arg;
3559 if (non_default_args_count >= 0)
3560 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3561 return result_args;
3564 /* Checks if DECL shadows a template parameter.
3566 [temp.local]: A template-parameter shall not be redeclared within its
3567 scope (including nested scopes).
3569 Emits an error and returns TRUE if the DECL shadows a parameter,
3570 returns FALSE otherwise. */
3572 bool
3573 check_template_shadow (tree decl)
3575 tree olddecl;
3577 /* If we're not in a template, we can't possibly shadow a template
3578 parameter. */
3579 if (!current_template_parms)
3580 return true;
3582 /* Figure out what we're shadowing. */
3583 if (TREE_CODE (decl) == OVERLOAD)
3584 decl = OVL_CURRENT (decl);
3585 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3587 /* If there's no previous binding for this name, we're not shadowing
3588 anything, let alone a template parameter. */
3589 if (!olddecl)
3590 return true;
3592 /* If we're not shadowing a template parameter, we're done. Note
3593 that OLDDECL might be an OVERLOAD (or perhaps even an
3594 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3595 node. */
3596 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3597 return true;
3599 /* We check for decl != olddecl to avoid bogus errors for using a
3600 name inside a class. We check TPFI to avoid duplicate errors for
3601 inline member templates. */
3602 if (decl == olddecl
3603 || (DECL_TEMPLATE_PARM_P (decl)
3604 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3605 return true;
3607 /* Don't complain about the injected class name, as we've already
3608 complained about the class itself. */
3609 if (DECL_SELF_REFERENCE_P (decl))
3610 return false;
3612 error ("declaration of %q+#D", decl);
3613 error (" shadows template parm %q+#D", olddecl);
3614 return false;
3617 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3618 ORIG_LEVEL, DECL, and TYPE. */
3620 static tree
3621 build_template_parm_index (int index,
3622 int level,
3623 int orig_level,
3624 tree decl,
3625 tree type)
3627 tree t = make_node (TEMPLATE_PARM_INDEX);
3628 TEMPLATE_PARM_IDX (t) = index;
3629 TEMPLATE_PARM_LEVEL (t) = level;
3630 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3631 TEMPLATE_PARM_DECL (t) = decl;
3632 TREE_TYPE (t) = type;
3633 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3634 TREE_READONLY (t) = TREE_READONLY (decl);
3636 return t;
3639 /* Find the canonical type parameter for the given template type
3640 parameter. Returns the canonical type parameter, which may be TYPE
3641 if no such parameter existed. */
3643 static tree
3644 canonical_type_parameter (tree type)
3646 tree list;
3647 int idx = TEMPLATE_TYPE_IDX (type);
3648 if (!canonical_template_parms)
3649 vec_alloc (canonical_template_parms, idx+1);
3651 while (canonical_template_parms->length () <= (unsigned)idx)
3652 vec_safe_push (canonical_template_parms, NULL_TREE);
3654 list = (*canonical_template_parms)[idx];
3655 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3656 list = TREE_CHAIN (list);
3658 if (list)
3659 return TREE_VALUE (list);
3660 else
3662 (*canonical_template_parms)[idx]
3663 = tree_cons (NULL_TREE, type,
3664 (*canonical_template_parms)[idx]);
3665 return type;
3669 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3670 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3671 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3672 new one is created. */
3674 static tree
3675 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3676 tsubst_flags_t complain)
3678 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3679 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3680 != TEMPLATE_PARM_LEVEL (index) - levels)
3681 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3683 tree orig_decl = TEMPLATE_PARM_DECL (index);
3684 tree decl, t;
3686 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3687 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3688 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3689 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3690 DECL_ARTIFICIAL (decl) = 1;
3691 SET_DECL_TEMPLATE_PARM_P (decl);
3693 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3694 TEMPLATE_PARM_LEVEL (index) - levels,
3695 TEMPLATE_PARM_ORIG_LEVEL (index),
3696 decl, type);
3697 TEMPLATE_PARM_DESCENDANTS (index) = t;
3698 TEMPLATE_PARM_PARAMETER_PACK (t)
3699 = TEMPLATE_PARM_PARAMETER_PACK (index);
3701 /* Template template parameters need this. */
3702 if (TREE_CODE (decl) == TEMPLATE_DECL)
3703 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3704 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3705 args, complain);
3708 return TEMPLATE_PARM_DESCENDANTS (index);
3711 /* Process information from new template parameter PARM and append it
3712 to the LIST being built. This new parameter is a non-type
3713 parameter iff IS_NON_TYPE is true. This new parameter is a
3714 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3715 is in PARM_LOC. */
3717 tree
3718 process_template_parm (tree list, location_t parm_loc, tree parm,
3719 bool is_non_type, bool is_parameter_pack)
3721 tree decl = 0;
3722 tree defval;
3723 int idx = 0;
3725 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3726 defval = TREE_PURPOSE (parm);
3728 if (list)
3730 tree p = tree_last (list);
3732 if (p && TREE_VALUE (p) != error_mark_node)
3734 p = TREE_VALUE (p);
3735 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3736 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3737 else
3738 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3741 ++idx;
3744 if (is_non_type)
3746 parm = TREE_VALUE (parm);
3748 SET_DECL_TEMPLATE_PARM_P (parm);
3750 if (TREE_TYPE (parm) != error_mark_node)
3752 /* [temp.param]
3754 The top-level cv-qualifiers on the template-parameter are
3755 ignored when determining its type. */
3756 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3757 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3758 TREE_TYPE (parm) = error_mark_node;
3759 else if (uses_parameter_packs (TREE_TYPE (parm))
3760 && !is_parameter_pack
3761 /* If we're in a nested template parameter list, the template
3762 template parameter could be a parameter pack. */
3763 && processing_template_parmlist == 1)
3765 /* This template parameter is not a parameter pack, but it
3766 should be. Complain about "bare" parameter packs. */
3767 check_for_bare_parameter_packs (TREE_TYPE (parm));
3769 /* Recover by calling this a parameter pack. */
3770 is_parameter_pack = true;
3774 /* A template parameter is not modifiable. */
3775 TREE_CONSTANT (parm) = 1;
3776 TREE_READONLY (parm) = 1;
3777 decl = build_decl (parm_loc,
3778 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3779 TREE_CONSTANT (decl) = 1;
3780 TREE_READONLY (decl) = 1;
3781 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3782 = build_template_parm_index (idx, processing_template_decl,
3783 processing_template_decl,
3784 decl, TREE_TYPE (parm));
3786 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3787 = is_parameter_pack;
3789 else
3791 tree t;
3792 parm = TREE_VALUE (TREE_VALUE (parm));
3794 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3796 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3797 /* This is for distinguishing between real templates and template
3798 template parameters */
3799 TREE_TYPE (parm) = t;
3800 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3801 decl = parm;
3803 else
3805 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3806 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3807 decl = build_decl (parm_loc,
3808 TYPE_DECL, parm, t);
3811 TYPE_NAME (t) = decl;
3812 TYPE_STUB_DECL (t) = decl;
3813 parm = decl;
3814 TEMPLATE_TYPE_PARM_INDEX (t)
3815 = build_template_parm_index (idx, processing_template_decl,
3816 processing_template_decl,
3817 decl, TREE_TYPE (parm));
3818 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3819 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3821 DECL_ARTIFICIAL (decl) = 1;
3822 SET_DECL_TEMPLATE_PARM_P (decl);
3823 pushdecl (decl);
3824 parm = build_tree_list (defval, parm);
3825 return chainon (list, parm);
3828 /* The end of a template parameter list has been reached. Process the
3829 tree list into a parameter vector, converting each parameter into a more
3830 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3831 as PARM_DECLs. */
3833 tree
3834 end_template_parm_list (tree parms)
3836 int nparms;
3837 tree parm, next;
3838 tree saved_parmlist = make_tree_vec (list_length (parms));
3840 current_template_parms
3841 = tree_cons (size_int (processing_template_decl),
3842 saved_parmlist, current_template_parms);
3844 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3846 next = TREE_CHAIN (parm);
3847 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3848 TREE_CHAIN (parm) = NULL_TREE;
3851 --processing_template_parmlist;
3853 return saved_parmlist;
3856 /* end_template_decl is called after a template declaration is seen. */
3858 void
3859 end_template_decl (void)
3861 reset_specialization ();
3863 if (! processing_template_decl)
3864 return;
3866 /* This matches the pushlevel in begin_template_parm_list. */
3867 finish_scope ();
3869 --processing_template_decl;
3870 current_template_parms = TREE_CHAIN (current_template_parms);
3873 /* Takes a TREE_LIST representing a template parameter and convert it
3874 into an argument suitable to be passed to the type substitution
3875 functions. Note that If the TREE_LIST contains an error_mark
3876 node, the returned argument is error_mark_node. */
3878 static tree
3879 template_parm_to_arg (tree t)
3882 if (t == NULL_TREE
3883 || TREE_CODE (t) != TREE_LIST)
3884 return t;
3886 if (error_operand_p (TREE_VALUE (t)))
3887 return error_mark_node;
3889 t = TREE_VALUE (t);
3891 if (TREE_CODE (t) == TYPE_DECL
3892 || TREE_CODE (t) == TEMPLATE_DECL)
3894 t = TREE_TYPE (t);
3896 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3898 /* Turn this argument into a TYPE_ARGUMENT_PACK
3899 with a single element, which expands T. */
3900 tree vec = make_tree_vec (1);
3901 #ifdef ENABLE_CHECKING
3902 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3903 (vec, TREE_VEC_LENGTH (vec));
3904 #endif
3905 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3907 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3908 SET_ARGUMENT_PACK_ARGS (t, vec);
3911 else
3913 t = DECL_INITIAL (t);
3915 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3917 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3918 with a single element, which expands T. */
3919 tree vec = make_tree_vec (1);
3920 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3921 #ifdef ENABLE_CHECKING
3922 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3923 (vec, TREE_VEC_LENGTH (vec));
3924 #endif
3925 t = convert_from_reference (t);
3926 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3928 t = make_node (NONTYPE_ARGUMENT_PACK);
3929 SET_ARGUMENT_PACK_ARGS (t, vec);
3930 TREE_TYPE (t) = type;
3932 else
3933 t = convert_from_reference (t);
3935 return t;
3938 /* Given a set of template parameters, return them as a set of template
3939 arguments. The template parameters are represented as a TREE_VEC, in
3940 the form documented in cp-tree.h for template arguments. */
3942 static tree
3943 template_parms_to_args (tree parms)
3945 tree header;
3946 tree args = NULL_TREE;
3947 int length = TMPL_PARMS_DEPTH (parms);
3948 int l = length;
3950 /* If there is only one level of template parameters, we do not
3951 create a TREE_VEC of TREE_VECs. Instead, we return a single
3952 TREE_VEC containing the arguments. */
3953 if (length > 1)
3954 args = make_tree_vec (length);
3956 for (header = parms; header; header = TREE_CHAIN (header))
3958 tree a = copy_node (TREE_VALUE (header));
3959 int i;
3961 TREE_TYPE (a) = NULL_TREE;
3962 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3963 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3965 #ifdef ENABLE_CHECKING
3966 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3967 #endif
3969 if (length > 1)
3970 TREE_VEC_ELT (args, --l) = a;
3971 else
3972 args = a;
3975 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3976 /* This can happen for template parms of a template template
3977 parameter, e.g:
3979 template<template<class T, class U> class TT> struct S;
3981 Consider the level of the parms of TT; T and U both have
3982 level 2; TT has no template parm of level 1. So in this case
3983 the first element of full_template_args is NULL_TREE. If we
3984 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3985 of 2. This will make tsubst wrongly consider that T and U
3986 have level 1. Instead, let's create a dummy vector as the
3987 first element of full_template_args so that TMPL_ARGS_DEPTH
3988 returns the correct depth for args. */
3989 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3990 return args;
3993 /* Within the declaration of a template, return the currently active
3994 template parameters as an argument TREE_VEC. */
3996 static tree
3997 current_template_args (void)
3999 return template_parms_to_args (current_template_parms);
4002 /* Update the declared TYPE by doing any lookups which were thought to be
4003 dependent, but are not now that we know the SCOPE of the declarator. */
4005 tree
4006 maybe_update_decl_type (tree orig_type, tree scope)
4008 tree type = orig_type;
4010 if (type == NULL_TREE)
4011 return type;
4013 if (TREE_CODE (orig_type) == TYPE_DECL)
4014 type = TREE_TYPE (type);
4016 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4017 && dependent_type_p (type)
4018 /* Don't bother building up the args in this case. */
4019 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4021 /* tsubst in the args corresponding to the template parameters,
4022 including auto if present. Most things will be unchanged, but
4023 make_typename_type and tsubst_qualified_id will resolve
4024 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4025 tree args = current_template_args ();
4026 tree auto_node = type_uses_auto (type);
4027 tree pushed;
4028 if (auto_node)
4030 tree auto_vec = make_tree_vec (1);
4031 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4032 args = add_to_template_args (args, auto_vec);
4034 pushed = push_scope (scope);
4035 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4036 if (pushed)
4037 pop_scope (scope);
4040 if (type == error_mark_node)
4041 return orig_type;
4043 if (TREE_CODE (orig_type) == TYPE_DECL)
4045 if (same_type_p (type, TREE_TYPE (orig_type)))
4046 type = orig_type;
4047 else
4048 type = TYPE_NAME (type);
4050 return type;
4053 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4054 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4055 a member template. Used by push_template_decl below. */
4057 static tree
4058 build_template_decl (tree decl, tree parms, bool member_template_p)
4060 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4061 DECL_TEMPLATE_PARMS (tmpl) = parms;
4062 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4063 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4064 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4066 return tmpl;
4069 struct template_parm_data
4071 /* The level of the template parameters we are currently
4072 processing. */
4073 int level;
4075 /* The index of the specialization argument we are currently
4076 processing. */
4077 int current_arg;
4079 /* An array whose size is the number of template parameters. The
4080 elements are nonzero if the parameter has been used in any one
4081 of the arguments processed so far. */
4082 int* parms;
4084 /* An array whose size is the number of template arguments. The
4085 elements are nonzero if the argument makes use of template
4086 parameters of this level. */
4087 int* arg_uses_template_parms;
4090 /* Subroutine of push_template_decl used to see if each template
4091 parameter in a partial specialization is used in the explicit
4092 argument list. If T is of the LEVEL given in DATA (which is
4093 treated as a template_parm_data*), then DATA->PARMS is marked
4094 appropriately. */
4096 static int
4097 mark_template_parm (tree t, void* data)
4099 int level;
4100 int idx;
4101 struct template_parm_data* tpd = (struct template_parm_data*) data;
4103 template_parm_level_and_index (t, &level, &idx);
4105 if (level == tpd->level)
4107 tpd->parms[idx] = 1;
4108 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4111 /* Return zero so that for_each_template_parm will continue the
4112 traversal of the tree; we want to mark *every* template parm. */
4113 return 0;
4116 /* Process the partial specialization DECL. */
4118 static tree
4119 process_partial_specialization (tree decl)
4121 tree type = TREE_TYPE (decl);
4122 tree tinfo = get_template_info (decl);
4123 tree maintmpl = TI_TEMPLATE (tinfo);
4124 tree specargs = TI_ARGS (tinfo);
4125 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4126 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4127 tree inner_parms;
4128 tree inst;
4129 int nargs = TREE_VEC_LENGTH (inner_args);
4130 int ntparms;
4131 int i;
4132 bool did_error_intro = false;
4133 struct template_parm_data tpd;
4134 struct template_parm_data tpd2;
4136 gcc_assert (current_template_parms);
4138 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4139 ntparms = TREE_VEC_LENGTH (inner_parms);
4141 /* We check that each of the template parameters given in the
4142 partial specialization is used in the argument list to the
4143 specialization. For example:
4145 template <class T> struct S;
4146 template <class T> struct S<T*>;
4148 The second declaration is OK because `T*' uses the template
4149 parameter T, whereas
4151 template <class T> struct S<int>;
4153 is no good. Even trickier is:
4155 template <class T>
4156 struct S1
4158 template <class U>
4159 struct S2;
4160 template <class U>
4161 struct S2<T>;
4164 The S2<T> declaration is actually invalid; it is a
4165 full-specialization. Of course,
4167 template <class U>
4168 struct S2<T (*)(U)>;
4170 or some such would have been OK. */
4171 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4172 tpd.parms = XALLOCAVEC (int, ntparms);
4173 memset (tpd.parms, 0, sizeof (int) * ntparms);
4175 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4176 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4177 for (i = 0; i < nargs; ++i)
4179 tpd.current_arg = i;
4180 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4181 &mark_template_parm,
4182 &tpd,
4183 NULL,
4184 /*include_nondeduced_p=*/false);
4186 for (i = 0; i < ntparms; ++i)
4187 if (tpd.parms[i] == 0)
4189 /* One of the template parms was not used in a deduced context in the
4190 specialization. */
4191 if (!did_error_intro)
4193 error ("template parameters not deducible in "
4194 "partial specialization:");
4195 did_error_intro = true;
4198 inform (input_location, " %qD",
4199 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4202 if (did_error_intro)
4203 return error_mark_node;
4205 /* [temp.class.spec]
4207 The argument list of the specialization shall not be identical to
4208 the implicit argument list of the primary template. */
4209 tree main_args
4210 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4211 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)))
4212 error ("partial specialization %qD does not specialize "
4213 "any template arguments", decl);
4215 /* A partial specialization that replaces multiple parameters of the
4216 primary template with a pack expansion is less specialized for those
4217 parameters. */
4218 if (nargs < DECL_NTPARMS (maintmpl))
4220 error ("partial specialization is not more specialized than the "
4221 "primary template because it replaces multiple parameters "
4222 "with a pack expansion");
4223 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4224 return decl;
4227 /* [temp.class.spec]
4229 A partially specialized non-type argument expression shall not
4230 involve template parameters of the partial specialization except
4231 when the argument expression is a simple identifier.
4233 The type of a template parameter corresponding to a specialized
4234 non-type argument shall not be dependent on a parameter of the
4235 specialization.
4237 Also, we verify that pack expansions only occur at the
4238 end of the argument list. */
4239 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4240 tpd2.parms = 0;
4241 for (i = 0; i < nargs; ++i)
4243 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4244 tree arg = TREE_VEC_ELT (inner_args, i);
4245 tree packed_args = NULL_TREE;
4246 int j, len = 1;
4248 if (ARGUMENT_PACK_P (arg))
4250 /* Extract the arguments from the argument pack. We'll be
4251 iterating over these in the following loop. */
4252 packed_args = ARGUMENT_PACK_ARGS (arg);
4253 len = TREE_VEC_LENGTH (packed_args);
4256 for (j = 0; j < len; j++)
4258 if (packed_args)
4259 /* Get the Jth argument in the parameter pack. */
4260 arg = TREE_VEC_ELT (packed_args, j);
4262 if (PACK_EXPANSION_P (arg))
4264 /* Pack expansions must come at the end of the
4265 argument list. */
4266 if ((packed_args && j < len - 1)
4267 || (!packed_args && i < nargs - 1))
4269 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4270 error ("parameter pack argument %qE must be at the "
4271 "end of the template argument list", arg);
4272 else
4273 error ("parameter pack argument %qT must be at the "
4274 "end of the template argument list", arg);
4278 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4279 /* We only care about the pattern. */
4280 arg = PACK_EXPANSION_PATTERN (arg);
4282 if (/* These first two lines are the `non-type' bit. */
4283 !TYPE_P (arg)
4284 && TREE_CODE (arg) != TEMPLATE_DECL
4285 /* This next two lines are the `argument expression is not just a
4286 simple identifier' condition and also the `specialized
4287 non-type argument' bit. */
4288 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4289 && !(REFERENCE_REF_P (arg)
4290 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4292 if ((!packed_args && tpd.arg_uses_template_parms[i])
4293 || (packed_args && uses_template_parms (arg)))
4294 error ("template argument %qE involves template parameter(s)",
4295 arg);
4296 else
4298 /* Look at the corresponding template parameter,
4299 marking which template parameters its type depends
4300 upon. */
4301 tree type = TREE_TYPE (parm);
4303 if (!tpd2.parms)
4305 /* We haven't yet initialized TPD2. Do so now. */
4306 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4307 /* The number of parameters here is the number in the
4308 main template, which, as checked in the assertion
4309 above, is NARGS. */
4310 tpd2.parms = XALLOCAVEC (int, nargs);
4311 tpd2.level =
4312 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4315 /* Mark the template parameters. But this time, we're
4316 looking for the template parameters of the main
4317 template, not in the specialization. */
4318 tpd2.current_arg = i;
4319 tpd2.arg_uses_template_parms[i] = 0;
4320 memset (tpd2.parms, 0, sizeof (int) * nargs);
4321 for_each_template_parm (type,
4322 &mark_template_parm,
4323 &tpd2,
4324 NULL,
4325 /*include_nondeduced_p=*/false);
4327 if (tpd2.arg_uses_template_parms [i])
4329 /* The type depended on some template parameters.
4330 If they are fully specialized in the
4331 specialization, that's OK. */
4332 int j;
4333 int count = 0;
4334 for (j = 0; j < nargs; ++j)
4335 if (tpd2.parms[j] != 0
4336 && tpd.arg_uses_template_parms [j])
4337 ++count;
4338 if (count != 0)
4339 error_n (input_location, count,
4340 "type %qT of template argument %qE depends "
4341 "on a template parameter",
4342 "type %qT of template argument %qE depends "
4343 "on template parameters",
4344 type,
4345 arg);
4352 /* We should only get here once. */
4353 if (TREE_CODE (decl) == TYPE_DECL)
4354 gcc_assert (!COMPLETE_TYPE_P (type));
4356 tree tmpl = build_template_decl (decl, current_template_parms,
4357 DECL_MEMBER_TEMPLATE_P (maintmpl));
4358 TREE_TYPE (tmpl) = type;
4359 DECL_TEMPLATE_RESULT (tmpl) = decl;
4360 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4361 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4362 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4364 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4365 = tree_cons (specargs, tmpl,
4366 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4367 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4369 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4370 inst = TREE_CHAIN (inst))
4372 tree instance = TREE_VALUE (inst);
4373 if (TYPE_P (instance)
4374 ? (COMPLETE_TYPE_P (instance)
4375 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4376 : DECL_TEMPLATE_INSTANTIATION (instance))
4378 tree spec = most_specialized_partial_spec (instance, tf_none);
4379 if (spec && TREE_VALUE (spec) == tmpl)
4381 tree inst_decl = (DECL_P (instance)
4382 ? instance : TYPE_NAME (instance));
4383 permerror (input_location,
4384 "partial specialization of %qD after instantiation "
4385 "of %qD", decl, inst_decl);
4390 return decl;
4393 /* PARM is a template parameter of some form; return the corresponding
4394 TEMPLATE_PARM_INDEX. */
4396 static tree
4397 get_template_parm_index (tree parm)
4399 if (TREE_CODE (parm) == PARM_DECL
4400 || TREE_CODE (parm) == CONST_DECL)
4401 parm = DECL_INITIAL (parm);
4402 else if (TREE_CODE (parm) == TYPE_DECL
4403 || TREE_CODE (parm) == TEMPLATE_DECL)
4404 parm = TREE_TYPE (parm);
4405 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4406 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4407 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4408 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4409 return parm;
4412 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4413 parameter packs used by the template parameter PARM. */
4415 static void
4416 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4418 /* A type parm can't refer to another parm. */
4419 if (TREE_CODE (parm) == TYPE_DECL)
4420 return;
4421 else if (TREE_CODE (parm) == PARM_DECL)
4423 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4424 ppd, ppd->visited);
4425 return;
4428 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4430 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4431 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4432 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4435 /* PARM is a template parameter pack. Return any parameter packs used in
4436 its type or the type of any of its template parameters. If there are
4437 any such packs, it will be instantiated into a fixed template parameter
4438 list by partial instantiation rather than be fully deduced. */
4440 tree
4441 fixed_parameter_pack_p (tree parm)
4443 /* This can only be true in a member template. */
4444 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4445 return NULL_TREE;
4446 /* This can only be true for a parameter pack. */
4447 if (!template_parameter_pack_p (parm))
4448 return NULL_TREE;
4449 /* A type parm can't refer to another parm. */
4450 if (TREE_CODE (parm) == TYPE_DECL)
4451 return NULL_TREE;
4453 tree parameter_packs = NULL_TREE;
4454 struct find_parameter_pack_data ppd;
4455 ppd.parameter_packs = &parameter_packs;
4456 ppd.visited = new hash_set<tree>;
4458 fixed_parameter_pack_p_1 (parm, &ppd);
4460 delete ppd.visited;
4461 return parameter_packs;
4464 /* Check that a template declaration's use of default arguments and
4465 parameter packs is not invalid. Here, PARMS are the template
4466 parameters. IS_PRIMARY is true if DECL is the thing declared by
4467 a primary template. IS_PARTIAL is true if DECL is a partial
4468 specialization.
4470 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4471 declaration (but not a definition); 1 indicates a declaration, 2
4472 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4473 emitted for extraneous default arguments.
4475 Returns TRUE if there were no errors found, FALSE otherwise. */
4477 bool
4478 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4479 bool is_partial, int is_friend_decl)
4481 const char *msg;
4482 int last_level_to_check;
4483 tree parm_level;
4484 bool no_errors = true;
4486 /* [temp.param]
4488 A default template-argument shall not be specified in a
4489 function template declaration or a function template definition, nor
4490 in the template-parameter-list of the definition of a member of a
4491 class template. */
4493 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4494 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4495 /* You can't have a function template declaration in a local
4496 scope, nor you can you define a member of a class template in a
4497 local scope. */
4498 return true;
4500 if ((TREE_CODE (decl) == TYPE_DECL
4501 && TREE_TYPE (decl)
4502 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4503 || (TREE_CODE (decl) == FUNCTION_DECL
4504 && LAMBDA_FUNCTION_P (decl)))
4505 /* A lambda doesn't have an explicit declaration; don't complain
4506 about the parms of the enclosing class. */
4507 return true;
4509 if (current_class_type
4510 && !TYPE_BEING_DEFINED (current_class_type)
4511 && DECL_LANG_SPECIFIC (decl)
4512 && DECL_DECLARES_FUNCTION_P (decl)
4513 /* If this is either a friend defined in the scope of the class
4514 or a member function. */
4515 && (DECL_FUNCTION_MEMBER_P (decl)
4516 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4517 : DECL_FRIEND_CONTEXT (decl)
4518 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4519 : false)
4520 /* And, if it was a member function, it really was defined in
4521 the scope of the class. */
4522 && (!DECL_FUNCTION_MEMBER_P (decl)
4523 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4524 /* We already checked these parameters when the template was
4525 declared, so there's no need to do it again now. This function
4526 was defined in class scope, but we're processing its body now
4527 that the class is complete. */
4528 return true;
4530 /* Core issue 226 (C++0x only): the following only applies to class
4531 templates. */
4532 if (is_primary
4533 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4535 /* [temp.param]
4537 If a template-parameter has a default template-argument, all
4538 subsequent template-parameters shall have a default
4539 template-argument supplied. */
4540 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4542 tree inner_parms = TREE_VALUE (parm_level);
4543 int ntparms = TREE_VEC_LENGTH (inner_parms);
4544 int seen_def_arg_p = 0;
4545 int i;
4547 for (i = 0; i < ntparms; ++i)
4549 tree parm = TREE_VEC_ELT (inner_parms, i);
4551 if (parm == error_mark_node)
4552 continue;
4554 if (TREE_PURPOSE (parm))
4555 seen_def_arg_p = 1;
4556 else if (seen_def_arg_p
4557 && !template_parameter_pack_p (TREE_VALUE (parm)))
4559 error ("no default argument for %qD", TREE_VALUE (parm));
4560 /* For better subsequent error-recovery, we indicate that
4561 there should have been a default argument. */
4562 TREE_PURPOSE (parm) = error_mark_node;
4563 no_errors = false;
4565 else if (!is_partial
4566 && !is_friend_decl
4567 /* Don't complain about an enclosing partial
4568 specialization. */
4569 && parm_level == parms
4570 && TREE_CODE (decl) == TYPE_DECL
4571 && i < ntparms - 1
4572 && template_parameter_pack_p (TREE_VALUE (parm))
4573 /* A fixed parameter pack will be partially
4574 instantiated into a fixed length list. */
4575 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4577 /* A primary class template can only have one
4578 parameter pack, at the end of the template
4579 parameter list. */
4581 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4582 error ("parameter pack %qE must be at the end of the"
4583 " template parameter list", TREE_VALUE (parm));
4584 else
4585 error ("parameter pack %qT must be at the end of the"
4586 " template parameter list",
4587 TREE_TYPE (TREE_VALUE (parm)));
4589 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4590 = error_mark_node;
4591 no_errors = false;
4597 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4598 || is_partial
4599 || !is_primary
4600 || is_friend_decl)
4601 /* For an ordinary class template, default template arguments are
4602 allowed at the innermost level, e.g.:
4603 template <class T = int>
4604 struct S {};
4605 but, in a partial specialization, they're not allowed even
4606 there, as we have in [temp.class.spec]:
4608 The template parameter list of a specialization shall not
4609 contain default template argument values.
4611 So, for a partial specialization, or for a function template
4612 (in C++98/C++03), we look at all of them. */
4614 else
4615 /* But, for a primary class template that is not a partial
4616 specialization we look at all template parameters except the
4617 innermost ones. */
4618 parms = TREE_CHAIN (parms);
4620 /* Figure out what error message to issue. */
4621 if (is_friend_decl == 2)
4622 msg = G_("default template arguments may not be used in function template "
4623 "friend re-declaration");
4624 else if (is_friend_decl)
4625 msg = G_("default template arguments may not be used in function template "
4626 "friend declarations");
4627 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4628 msg = G_("default template arguments may not be used in function templates "
4629 "without -std=c++11 or -std=gnu++11");
4630 else if (is_partial)
4631 msg = G_("default template arguments may not be used in "
4632 "partial specializations");
4633 else
4634 msg = G_("default argument for template parameter for class enclosing %qD");
4636 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4637 /* If we're inside a class definition, there's no need to
4638 examine the parameters to the class itself. On the one
4639 hand, they will be checked when the class is defined, and,
4640 on the other, default arguments are valid in things like:
4641 template <class T = double>
4642 struct S { template <class U> void f(U); };
4643 Here the default argument for `S' has no bearing on the
4644 declaration of `f'. */
4645 last_level_to_check = template_class_depth (current_class_type) + 1;
4646 else
4647 /* Check everything. */
4648 last_level_to_check = 0;
4650 for (parm_level = parms;
4651 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4652 parm_level = TREE_CHAIN (parm_level))
4654 tree inner_parms = TREE_VALUE (parm_level);
4655 int i;
4656 int ntparms;
4658 ntparms = TREE_VEC_LENGTH (inner_parms);
4659 for (i = 0; i < ntparms; ++i)
4661 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4662 continue;
4664 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4666 if (msg)
4668 no_errors = false;
4669 if (is_friend_decl == 2)
4670 return no_errors;
4672 error (msg, decl);
4673 msg = 0;
4676 /* Clear out the default argument so that we are not
4677 confused later. */
4678 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4682 /* At this point, if we're still interested in issuing messages,
4683 they must apply to classes surrounding the object declared. */
4684 if (msg)
4685 msg = G_("default argument for template parameter for class "
4686 "enclosing %qD");
4689 return no_errors;
4692 /* Worker for push_template_decl_real, called via
4693 for_each_template_parm. DATA is really an int, indicating the
4694 level of the parameters we are interested in. If T is a template
4695 parameter of that level, return nonzero. */
4697 static int
4698 template_parm_this_level_p (tree t, void* data)
4700 int this_level = *(int *)data;
4701 int level;
4703 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4704 level = TEMPLATE_PARM_LEVEL (t);
4705 else
4706 level = TEMPLATE_TYPE_LEVEL (t);
4707 return level == this_level;
4710 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4711 parameters given by current_template_args, or reuses a
4712 previously existing one, if appropriate. Returns the DECL, or an
4713 equivalent one, if it is replaced via a call to duplicate_decls.
4715 If IS_FRIEND is true, DECL is a friend declaration. */
4717 tree
4718 push_template_decl_real (tree decl, bool is_friend)
4720 tree tmpl;
4721 tree args;
4722 tree info;
4723 tree ctx;
4724 bool is_primary;
4725 bool is_partial;
4726 int new_template_p = 0;
4727 /* True if the template is a member template, in the sense of
4728 [temp.mem]. */
4729 bool member_template_p = false;
4731 if (decl == error_mark_node || !current_template_parms)
4732 return error_mark_node;
4734 /* See if this is a partial specialization. */
4735 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
4736 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4737 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4738 || (TREE_CODE (decl) == VAR_DECL
4739 && DECL_LANG_SPECIFIC (decl)
4740 && DECL_TEMPLATE_SPECIALIZATION (decl)
4741 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
4743 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4744 is_friend = true;
4746 if (is_friend)
4747 /* For a friend, we want the context of the friend function, not
4748 the type of which it is a friend. */
4749 ctx = CP_DECL_CONTEXT (decl);
4750 else if (CP_DECL_CONTEXT (decl)
4751 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4752 /* In the case of a virtual function, we want the class in which
4753 it is defined. */
4754 ctx = CP_DECL_CONTEXT (decl);
4755 else
4756 /* Otherwise, if we're currently defining some class, the DECL
4757 is assumed to be a member of the class. */
4758 ctx = current_scope ();
4760 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4761 ctx = NULL_TREE;
4763 if (!DECL_CONTEXT (decl))
4764 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4766 /* See if this is a primary template. */
4767 if (is_friend && ctx
4768 && uses_template_parms_level (ctx, processing_template_decl))
4769 /* A friend template that specifies a class context, i.e.
4770 template <typename T> friend void A<T>::f();
4771 is not primary. */
4772 is_primary = false;
4773 else if (TREE_CODE (decl) == TYPE_DECL
4774 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4775 is_primary = false;
4776 else
4777 is_primary = template_parm_scope_p ();
4779 if (is_primary)
4781 if (DECL_CLASS_SCOPE_P (decl))
4782 member_template_p = true;
4783 if (TREE_CODE (decl) == TYPE_DECL
4784 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4786 error ("template class without a name");
4787 return error_mark_node;
4789 else if (TREE_CODE (decl) == FUNCTION_DECL)
4791 if (member_template_p)
4793 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
4794 error ("member template %qD may not have virt-specifiers", decl);
4796 if (DECL_DESTRUCTOR_P (decl))
4798 /* [temp.mem]
4800 A destructor shall not be a member template. */
4801 error ("destructor %qD declared as member template", decl);
4802 return error_mark_node;
4804 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4805 && (!prototype_p (TREE_TYPE (decl))
4806 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4807 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4808 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4809 == void_list_node)))
4811 /* [basic.stc.dynamic.allocation]
4813 An allocation function can be a function
4814 template. ... Template allocation functions shall
4815 have two or more parameters. */
4816 error ("invalid template declaration of %qD", decl);
4817 return error_mark_node;
4820 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4821 && CLASS_TYPE_P (TREE_TYPE (decl)))
4822 /* OK */;
4823 else if (TREE_CODE (decl) == TYPE_DECL
4824 && TYPE_DECL_ALIAS_P (decl))
4825 /* alias-declaration */
4826 gcc_assert (!DECL_ARTIFICIAL (decl));
4827 else if (VAR_P (decl))
4828 /* C++14 variable template. */;
4829 else
4831 error ("template declaration of %q#D", decl);
4832 return error_mark_node;
4836 /* Check to see that the rules regarding the use of default
4837 arguments are not being violated. */
4838 check_default_tmpl_args (decl, current_template_parms,
4839 is_primary, is_partial, /*is_friend_decl=*/0);
4841 /* Ensure that there are no parameter packs in the type of this
4842 declaration that have not been expanded. */
4843 if (TREE_CODE (decl) == FUNCTION_DECL)
4845 /* Check each of the arguments individually to see if there are
4846 any bare parameter packs. */
4847 tree type = TREE_TYPE (decl);
4848 tree arg = DECL_ARGUMENTS (decl);
4849 tree argtype = TYPE_ARG_TYPES (type);
4851 while (arg && argtype)
4853 if (!DECL_PACK_P (arg)
4854 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4856 /* This is a PARM_DECL that contains unexpanded parameter
4857 packs. We have already complained about this in the
4858 check_for_bare_parameter_packs call, so just replace
4859 these types with ERROR_MARK_NODE. */
4860 TREE_TYPE (arg) = error_mark_node;
4861 TREE_VALUE (argtype) = error_mark_node;
4864 arg = DECL_CHAIN (arg);
4865 argtype = TREE_CHAIN (argtype);
4868 /* Check for bare parameter packs in the return type and the
4869 exception specifiers. */
4870 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4871 /* Errors were already issued, set return type to int
4872 as the frontend doesn't expect error_mark_node as
4873 the return type. */
4874 TREE_TYPE (type) = integer_type_node;
4875 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4876 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4878 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4879 && TYPE_DECL_ALIAS_P (decl))
4880 ? DECL_ORIGINAL_TYPE (decl)
4881 : TREE_TYPE (decl)))
4883 TREE_TYPE (decl) = error_mark_node;
4884 return error_mark_node;
4887 if (is_partial)
4888 return process_partial_specialization (decl);
4890 args = current_template_args ();
4892 if (!ctx
4893 || TREE_CODE (ctx) == FUNCTION_DECL
4894 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4895 || (TREE_CODE (decl) == TYPE_DECL
4896 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4897 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4899 if (DECL_LANG_SPECIFIC (decl)
4900 && DECL_TEMPLATE_INFO (decl)
4901 && DECL_TI_TEMPLATE (decl))
4902 tmpl = DECL_TI_TEMPLATE (decl);
4903 /* If DECL is a TYPE_DECL for a class-template, then there won't
4904 be DECL_LANG_SPECIFIC. The information equivalent to
4905 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4906 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4907 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4908 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4910 /* Since a template declaration already existed for this
4911 class-type, we must be redeclaring it here. Make sure
4912 that the redeclaration is valid. */
4913 redeclare_class_template (TREE_TYPE (decl),
4914 current_template_parms);
4915 /* We don't need to create a new TEMPLATE_DECL; just use the
4916 one we already had. */
4917 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4919 else
4921 tmpl = build_template_decl (decl, current_template_parms,
4922 member_template_p);
4923 new_template_p = 1;
4925 if (DECL_LANG_SPECIFIC (decl)
4926 && DECL_TEMPLATE_SPECIALIZATION (decl))
4928 /* A specialization of a member template of a template
4929 class. */
4930 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4931 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4932 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4936 else
4938 tree a, t, current, parms;
4939 int i;
4940 tree tinfo = get_template_info (decl);
4942 if (!tinfo)
4944 error ("template definition of non-template %q#D", decl);
4945 return error_mark_node;
4948 tmpl = TI_TEMPLATE (tinfo);
4950 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4951 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4952 && DECL_TEMPLATE_SPECIALIZATION (decl)
4953 && DECL_MEMBER_TEMPLATE_P (tmpl))
4955 tree new_tmpl;
4957 /* The declaration is a specialization of a member
4958 template, declared outside the class. Therefore, the
4959 innermost template arguments will be NULL, so we
4960 replace them with the arguments determined by the
4961 earlier call to check_explicit_specialization. */
4962 args = DECL_TI_ARGS (decl);
4964 new_tmpl
4965 = build_template_decl (decl, current_template_parms,
4966 member_template_p);
4967 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4968 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4969 DECL_TI_TEMPLATE (decl) = new_tmpl;
4970 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4971 DECL_TEMPLATE_INFO (new_tmpl)
4972 = build_template_info (tmpl, args);
4974 register_specialization (new_tmpl,
4975 most_general_template (tmpl),
4976 args,
4977 is_friend, 0);
4978 return decl;
4981 /* Make sure the template headers we got make sense. */
4983 parms = DECL_TEMPLATE_PARMS (tmpl);
4984 i = TMPL_PARMS_DEPTH (parms);
4985 if (TMPL_ARGS_DEPTH (args) != i)
4987 error ("expected %d levels of template parms for %q#D, got %d",
4988 i, decl, TMPL_ARGS_DEPTH (args));
4989 DECL_INTERFACE_KNOWN (decl) = 1;
4990 return error_mark_node;
4992 else
4993 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4995 a = TMPL_ARGS_LEVEL (args, i);
4996 t = INNERMOST_TEMPLATE_PARMS (parms);
4998 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5000 if (current == decl)
5001 error ("got %d template parameters for %q#D",
5002 TREE_VEC_LENGTH (a), decl);
5003 else
5004 error ("got %d template parameters for %q#T",
5005 TREE_VEC_LENGTH (a), current);
5006 error (" but %d required", TREE_VEC_LENGTH (t));
5007 /* Avoid crash in import_export_decl. */
5008 DECL_INTERFACE_KNOWN (decl) = 1;
5009 return error_mark_node;
5012 if (current == decl)
5013 current = ctx;
5014 else if (current == NULL_TREE)
5015 /* Can happen in erroneous input. */
5016 break;
5017 else
5018 current = get_containing_scope (current);
5021 /* Check that the parms are used in the appropriate qualifying scopes
5022 in the declarator. */
5023 if (!comp_template_args
5024 (TI_ARGS (tinfo),
5025 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5027 error ("\
5028 template arguments to %qD do not match original template %qD",
5029 decl, DECL_TEMPLATE_RESULT (tmpl));
5030 if (!uses_template_parms (TI_ARGS (tinfo)))
5031 inform (input_location, "use template<> for an explicit specialization");
5032 /* Avoid crash in import_export_decl. */
5033 DECL_INTERFACE_KNOWN (decl) = 1;
5034 return error_mark_node;
5038 DECL_TEMPLATE_RESULT (tmpl) = decl;
5039 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5041 /* Push template declarations for global functions and types. Note
5042 that we do not try to push a global template friend declared in a
5043 template class; such a thing may well depend on the template
5044 parameters of the class. */
5045 if (new_template_p && !ctx
5046 && !(is_friend && template_class_depth (current_class_type) > 0))
5048 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5049 if (tmpl == error_mark_node)
5050 return error_mark_node;
5052 /* Hide template friend classes that haven't been declared yet. */
5053 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5055 DECL_ANTICIPATED (tmpl) = 1;
5056 DECL_FRIEND_P (tmpl) = 1;
5060 if (is_primary)
5062 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5063 int i;
5065 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5066 if (DECL_CONV_FN_P (tmpl))
5068 int depth = TMPL_PARMS_DEPTH (parms);
5070 /* It is a conversion operator. See if the type converted to
5071 depends on innermost template operands. */
5073 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5074 depth))
5075 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5078 /* Give template template parms a DECL_CONTEXT of the template
5079 for which they are a parameter. */
5080 parms = INNERMOST_TEMPLATE_PARMS (parms);
5081 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5083 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5084 if (TREE_CODE (parm) == TEMPLATE_DECL)
5085 DECL_CONTEXT (parm) = tmpl;
5089 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5090 back to its most general template. If TMPL is a specialization,
5091 ARGS may only have the innermost set of arguments. Add the missing
5092 argument levels if necessary. */
5093 if (DECL_TEMPLATE_INFO (tmpl))
5094 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5096 info = build_template_info (tmpl, args);
5098 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5099 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5100 else
5102 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5103 retrofit_lang_decl (decl);
5104 if (DECL_LANG_SPECIFIC (decl))
5105 DECL_TEMPLATE_INFO (decl) = info;
5108 if (flag_implicit_templates
5109 && !is_friend
5110 && TREE_PUBLIC (decl)
5111 && VAR_OR_FUNCTION_DECL_P (decl))
5112 /* Set DECL_COMDAT on template instantiations; if we force
5113 them to be emitted by explicit instantiation or -frepo,
5114 mark_needed will tell cgraph to do the right thing. */
5115 DECL_COMDAT (decl) = true;
5117 return DECL_TEMPLATE_RESULT (tmpl);
5120 tree
5121 push_template_decl (tree decl)
5123 return push_template_decl_real (decl, false);
5126 /* FN is an inheriting constructor that inherits from the constructor
5127 template INHERITED; turn FN into a constructor template with a matching
5128 template header. */
5130 tree
5131 add_inherited_template_parms (tree fn, tree inherited)
5133 tree inner_parms
5134 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5135 inner_parms = copy_node (inner_parms);
5136 tree parms
5137 = tree_cons (size_int (processing_template_decl + 1),
5138 inner_parms, current_template_parms);
5139 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5140 tree args = template_parms_to_args (parms);
5141 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5142 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5143 DECL_TEMPLATE_RESULT (tmpl) = fn;
5144 DECL_ARTIFICIAL (tmpl) = true;
5145 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5146 return tmpl;
5149 /* Called when a class template TYPE is redeclared with the indicated
5150 template PARMS, e.g.:
5152 template <class T> struct S;
5153 template <class T> struct S {}; */
5155 bool
5156 redeclare_class_template (tree type, tree parms)
5158 tree tmpl;
5159 tree tmpl_parms;
5160 int i;
5162 if (!TYPE_TEMPLATE_INFO (type))
5164 error ("%qT is not a template type", type);
5165 return false;
5168 tmpl = TYPE_TI_TEMPLATE (type);
5169 if (!PRIMARY_TEMPLATE_P (tmpl))
5170 /* The type is nested in some template class. Nothing to worry
5171 about here; there are no new template parameters for the nested
5172 type. */
5173 return true;
5175 if (!parms)
5177 error ("template specifiers not specified in declaration of %qD",
5178 tmpl);
5179 return false;
5182 parms = INNERMOST_TEMPLATE_PARMS (parms);
5183 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5185 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5187 error_n (input_location, TREE_VEC_LENGTH (parms),
5188 "redeclared with %d template parameter",
5189 "redeclared with %d template parameters",
5190 TREE_VEC_LENGTH (parms));
5191 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5192 "previous declaration %q+D used %d template parameter",
5193 "previous declaration %q+D used %d template parameters",
5194 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5195 return false;
5198 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5200 tree tmpl_parm;
5201 tree parm;
5202 tree tmpl_default;
5203 tree parm_default;
5205 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5206 || TREE_VEC_ELT (parms, i) == error_mark_node)
5207 continue;
5209 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5210 if (error_operand_p (tmpl_parm))
5211 return false;
5213 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5214 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5215 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5217 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5218 TEMPLATE_DECL. */
5219 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5220 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5221 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5222 || (TREE_CODE (tmpl_parm) != PARM_DECL
5223 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5224 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5225 || (TREE_CODE (tmpl_parm) == PARM_DECL
5226 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5227 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5229 error ("template parameter %q+#D", tmpl_parm);
5230 error ("redeclared here as %q#D", parm);
5231 return false;
5234 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5236 /* We have in [temp.param]:
5238 A template-parameter may not be given default arguments
5239 by two different declarations in the same scope. */
5240 error_at (input_location, "redefinition of default argument for %q#D", parm);
5241 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5242 "original definition appeared here");
5243 return false;
5246 if (parm_default != NULL_TREE)
5247 /* Update the previous template parameters (which are the ones
5248 that will really count) with the new default value. */
5249 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5250 else if (tmpl_default != NULL_TREE)
5251 /* Update the new parameters, too; they'll be used as the
5252 parameters for any members. */
5253 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5256 return true;
5259 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5260 to be used when the caller has already checked
5261 (processing_template_decl
5262 && !instantiation_dependent_expression_p (expr)
5263 && potential_constant_expression (expr))
5264 and cleared processing_template_decl. */
5266 tree
5267 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5269 return tsubst_copy_and_build (expr,
5270 /*args=*/NULL_TREE,
5271 complain,
5272 /*in_decl=*/NULL_TREE,
5273 /*function_p=*/false,
5274 /*integral_constant_expression_p=*/true);
5277 /* Simplify EXPR if it is a non-dependent expression. Returns the
5278 (possibly simplified) expression. */
5280 tree
5281 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5283 if (expr == NULL_TREE)
5284 return NULL_TREE;
5286 /* If we're in a template, but EXPR isn't value dependent, simplify
5287 it. We're supposed to treat:
5289 template <typename T> void f(T[1 + 1]);
5290 template <typename T> void f(T[2]);
5292 as two declarations of the same function, for example. */
5293 if (processing_template_decl
5294 && !instantiation_dependent_expression_p (expr)
5295 && potential_constant_expression (expr))
5297 processing_template_decl_sentinel s;
5298 expr = instantiate_non_dependent_expr_internal (expr, complain);
5300 return expr;
5303 tree
5304 instantiate_non_dependent_expr (tree expr)
5306 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5309 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5310 template declaration, or a TYPE_DECL for an alias declaration. */
5312 bool
5313 alias_type_or_template_p (tree t)
5315 if (t == NULL_TREE)
5316 return false;
5317 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5318 || (TYPE_P (t)
5319 && TYPE_NAME (t)
5320 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5321 || DECL_ALIAS_TEMPLATE_P (t));
5324 /* Return TRUE iff T is a specialization of an alias template. */
5326 bool
5327 alias_template_specialization_p (const_tree t)
5329 /* It's an alias template specialization if it's an alias and its
5330 TYPE_NAME is a specialization of a primary template. */
5331 if (TYPE_ALIAS_P (t))
5333 tree name = TYPE_NAME (t);
5334 if (DECL_LANG_SPECIFIC (name))
5335 if (tree ti = DECL_TEMPLATE_INFO (name))
5337 tree tmpl = TI_TEMPLATE (ti);
5338 return PRIMARY_TEMPLATE_P (tmpl);
5341 return false;
5344 /* Return TRUE iff T is a specialization of an alias template with
5345 dependent template-arguments. */
5347 bool
5348 dependent_alias_template_spec_p (const_tree t)
5350 return (alias_template_specialization_p (t)
5351 && (any_dependent_template_arguments_p
5352 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5355 /* Return the number of innermost template parameters in TMPL. */
5357 static int
5358 num_innermost_template_parms (tree tmpl)
5360 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5361 return TREE_VEC_LENGTH (parms);
5364 /* Return either TMPL or another template that it is equivalent to under DR
5365 1286: An alias that just changes the name of a template is equivalent to
5366 the other template. */
5368 static tree
5369 get_underlying_template (tree tmpl)
5371 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5372 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5374 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5375 if (TYPE_TEMPLATE_INFO (result))
5377 tree sub = TYPE_TI_TEMPLATE (result);
5378 if (PRIMARY_TEMPLATE_P (sub)
5379 && (num_innermost_template_parms (tmpl)
5380 == num_innermost_template_parms (sub)))
5382 tree alias_args = INNERMOST_TEMPLATE_ARGS
5383 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5384 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5385 break;
5386 /* The alias type is equivalent to the pattern of the
5387 underlying template, so strip the alias. */
5388 tmpl = sub;
5389 continue;
5392 break;
5394 return tmpl;
5397 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5398 must be a function or a pointer-to-function type, as specified
5399 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5400 and check that the resulting function has external linkage. */
5402 static tree
5403 convert_nontype_argument_function (tree type, tree expr,
5404 tsubst_flags_t complain)
5406 tree fns = expr;
5407 tree fn, fn_no_ptr;
5408 linkage_kind linkage;
5410 fn = instantiate_type (type, fns, tf_none);
5411 if (fn == error_mark_node)
5412 return error_mark_node;
5414 fn_no_ptr = fn;
5415 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5416 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5417 if (BASELINK_P (fn_no_ptr))
5418 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5420 /* [temp.arg.nontype]/1
5422 A template-argument for a non-type, non-template template-parameter
5423 shall be one of:
5424 [...]
5425 -- the address of an object or function with external [C++11: or
5426 internal] linkage. */
5428 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5430 if (complain & tf_error)
5432 error ("%qE is not a valid template argument for type %qT",
5433 expr, type);
5434 if (TYPE_PTR_P (type))
5435 error ("it must be the address of a function with "
5436 "external linkage");
5437 else
5438 error ("it must be the name of a function with "
5439 "external linkage");
5441 return NULL_TREE;
5444 linkage = decl_linkage (fn_no_ptr);
5445 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5447 if (complain & tf_error)
5449 if (cxx_dialect >= cxx11)
5450 error ("%qE is not a valid template argument for type %qT "
5451 "because %qD has no linkage",
5452 expr, type, fn_no_ptr);
5453 else
5454 error ("%qE is not a valid template argument for type %qT "
5455 "because %qD does not have external linkage",
5456 expr, type, fn_no_ptr);
5458 return NULL_TREE;
5461 return fn;
5464 /* Subroutine of convert_nontype_argument.
5465 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5466 Emit an error otherwise. */
5468 static bool
5469 check_valid_ptrmem_cst_expr (tree type, tree expr,
5470 tsubst_flags_t complain)
5472 STRIP_NOPS (expr);
5473 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5474 return true;
5475 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5476 return true;
5477 if (processing_template_decl
5478 && TREE_CODE (expr) == ADDR_EXPR
5479 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5480 return true;
5481 if (complain & tf_error)
5483 error ("%qE is not a valid template argument for type %qT",
5484 expr, type);
5485 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5487 return false;
5490 /* Returns TRUE iff the address of OP is value-dependent.
5492 14.6.2.4 [temp.dep.temp]:
5493 A non-integral non-type template-argument is dependent if its type is
5494 dependent or it has either of the following forms
5495 qualified-id
5496 & qualified-id
5497 and contains a nested-name-specifier which specifies a class-name that
5498 names a dependent type.
5500 We generalize this to just say that the address of a member of a
5501 dependent class is value-dependent; the above doesn't cover the
5502 address of a static data member named with an unqualified-id. */
5504 static bool
5505 has_value_dependent_address (tree op)
5507 /* We could use get_inner_reference here, but there's no need;
5508 this is only relevant for template non-type arguments, which
5509 can only be expressed as &id-expression. */
5510 if (DECL_P (op))
5512 tree ctx = CP_DECL_CONTEXT (op);
5513 if (TYPE_P (ctx) && dependent_type_p (ctx))
5514 return true;
5517 return false;
5520 /* The next set of functions are used for providing helpful explanatory
5521 diagnostics for failed overload resolution. Their messages should be
5522 indented by two spaces for consistency with the messages in
5523 call.c */
5525 static int
5526 unify_success (bool /*explain_p*/)
5528 return 0;
5531 static int
5532 unify_parameter_deduction_failure (bool explain_p, tree parm)
5534 if (explain_p)
5535 inform (input_location,
5536 " couldn't deduce template parameter %qD", parm);
5537 return 1;
5540 static int
5541 unify_invalid (bool /*explain_p*/)
5543 return 1;
5546 static int
5547 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5549 if (explain_p)
5550 inform (input_location,
5551 " types %qT and %qT have incompatible cv-qualifiers",
5552 parm, arg);
5553 return 1;
5556 static int
5557 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5559 if (explain_p)
5560 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5561 return 1;
5564 static int
5565 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5567 if (explain_p)
5568 inform (input_location,
5569 " template parameter %qD is not a parameter pack, but "
5570 "argument %qD is",
5571 parm, arg);
5572 return 1;
5575 static int
5576 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5578 if (explain_p)
5579 inform (input_location,
5580 " template argument %qE does not match "
5581 "pointer-to-member constant %qE",
5582 arg, parm);
5583 return 1;
5586 static int
5587 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5589 if (explain_p)
5590 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5591 return 1;
5594 static int
5595 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5597 if (explain_p)
5598 inform (input_location,
5599 " inconsistent parameter pack deduction with %qT and %qT",
5600 old_arg, new_arg);
5601 return 1;
5604 static int
5605 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5607 if (explain_p)
5609 if (TYPE_P (parm))
5610 inform (input_location,
5611 " deduced conflicting types for parameter %qT (%qT and %qT)",
5612 parm, first, second);
5613 else
5614 inform (input_location,
5615 " deduced conflicting values for non-type parameter "
5616 "%qE (%qE and %qE)", parm, first, second);
5618 return 1;
5621 static int
5622 unify_vla_arg (bool explain_p, tree arg)
5624 if (explain_p)
5625 inform (input_location,
5626 " variable-sized array type %qT is not "
5627 "a valid template argument",
5628 arg);
5629 return 1;
5632 static int
5633 unify_method_type_error (bool explain_p, tree arg)
5635 if (explain_p)
5636 inform (input_location,
5637 " member function type %qT is not a valid template argument",
5638 arg);
5639 return 1;
5642 static int
5643 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
5645 if (explain_p)
5647 if (least_p)
5648 inform_n (input_location, wanted,
5649 " candidate expects at least %d argument, %d provided",
5650 " candidate expects at least %d arguments, %d provided",
5651 wanted, have);
5652 else
5653 inform_n (input_location, wanted,
5654 " candidate expects %d argument, %d provided",
5655 " candidate expects %d arguments, %d provided",
5656 wanted, have);
5658 return 1;
5661 static int
5662 unify_too_many_arguments (bool explain_p, int have, int wanted)
5664 return unify_arity (explain_p, have, wanted);
5667 static int
5668 unify_too_few_arguments (bool explain_p, int have, int wanted,
5669 bool least_p = false)
5671 return unify_arity (explain_p, have, wanted, least_p);
5674 static int
5675 unify_arg_conversion (bool explain_p, tree to_type,
5676 tree from_type, tree arg)
5678 if (explain_p)
5679 inform (EXPR_LOC_OR_LOC (arg, input_location),
5680 " cannot convert %qE (type %qT) to type %qT",
5681 arg, from_type, to_type);
5682 return 1;
5685 static int
5686 unify_no_common_base (bool explain_p, enum template_base_result r,
5687 tree parm, tree arg)
5689 if (explain_p)
5690 switch (r)
5692 case tbr_ambiguous_baseclass:
5693 inform (input_location, " %qT is an ambiguous base class of %qT",
5694 parm, arg);
5695 break;
5696 default:
5697 inform (input_location, " %qT is not derived from %qT", arg, parm);
5698 break;
5700 return 1;
5703 static int
5704 unify_inconsistent_template_template_parameters (bool explain_p)
5706 if (explain_p)
5707 inform (input_location,
5708 " template parameters of a template template argument are "
5709 "inconsistent with other deduced template arguments");
5710 return 1;
5713 static int
5714 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5716 if (explain_p)
5717 inform (input_location,
5718 " can't deduce a template for %qT from non-template type %qT",
5719 parm, arg);
5720 return 1;
5723 static int
5724 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5726 if (explain_p)
5727 inform (input_location,
5728 " template argument %qE does not match %qD", arg, parm);
5729 return 1;
5732 static int
5733 unify_overload_resolution_failure (bool explain_p, tree arg)
5735 if (explain_p)
5736 inform (input_location,
5737 " could not resolve address from overloaded function %qE",
5738 arg);
5739 return 1;
5742 /* Attempt to convert the non-type template parameter EXPR to the
5743 indicated TYPE. If the conversion is successful, return the
5744 converted value. If the conversion is unsuccessful, return
5745 NULL_TREE if we issued an error message, or error_mark_node if we
5746 did not. We issue error messages for out-and-out bad template
5747 parameters, but not simply because the conversion failed, since we
5748 might be just trying to do argument deduction. Both TYPE and EXPR
5749 must be non-dependent.
5751 The conversion follows the special rules described in
5752 [temp.arg.nontype], and it is much more strict than an implicit
5753 conversion.
5755 This function is called twice for each template argument (see
5756 lookup_template_class for a more accurate description of this
5757 problem). This means that we need to handle expressions which
5758 are not valid in a C++ source, but can be created from the
5759 first call (for instance, casts to perform conversions). These
5760 hacks can go away after we fix the double coercion problem. */
5762 static tree
5763 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5765 tree expr_type;
5767 /* Detect immediately string literals as invalid non-type argument.
5768 This special-case is not needed for correctness (we would easily
5769 catch this later), but only to provide better diagnostic for this
5770 common user mistake. As suggested by DR 100, we do not mention
5771 linkage issues in the diagnostic as this is not the point. */
5772 /* FIXME we're making this OK. */
5773 if (TREE_CODE (expr) == STRING_CST)
5775 if (complain & tf_error)
5776 error ("%qE is not a valid template argument for type %qT "
5777 "because string literals can never be used in this context",
5778 expr, type);
5779 return NULL_TREE;
5782 /* Add the ADDR_EXPR now for the benefit of
5783 value_dependent_expression_p. */
5784 if (TYPE_PTROBV_P (type)
5785 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5787 expr = decay_conversion (expr, complain);
5788 if (expr == error_mark_node)
5789 return error_mark_node;
5792 /* If we are in a template, EXPR may be non-dependent, but still
5793 have a syntactic, rather than semantic, form. For example, EXPR
5794 might be a SCOPE_REF, rather than the VAR_DECL to which the
5795 SCOPE_REF refers. Preserving the qualifying scope is necessary
5796 so that access checking can be performed when the template is
5797 instantiated -- but here we need the resolved form so that we can
5798 convert the argument. */
5799 bool non_dep = false;
5800 if (TYPE_REF_OBJ_P (type)
5801 && has_value_dependent_address (expr))
5802 /* If we want the address and it's value-dependent, don't fold. */;
5803 else if (!type_unknown_p (expr)
5804 && processing_template_decl
5805 && !instantiation_dependent_expression_p (expr)
5806 && potential_constant_expression (expr))
5807 non_dep = true;
5808 if (error_operand_p (expr))
5809 return error_mark_node;
5810 expr_type = TREE_TYPE (expr);
5811 if (TREE_CODE (type) == REFERENCE_TYPE)
5812 expr = mark_lvalue_use (expr);
5813 else
5814 expr = mark_rvalue_use (expr);
5816 /* If the argument is non-dependent, perform any conversions in
5817 non-dependent context as well. */
5818 processing_template_decl_sentinel s (non_dep);
5819 if (non_dep)
5820 expr = instantiate_non_dependent_expr_internal (expr, complain);
5822 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5823 to a non-type argument of "nullptr". */
5824 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5825 expr = convert (type, expr);
5827 /* In C++11, integral or enumeration non-type template arguments can be
5828 arbitrary constant expressions. Pointer and pointer to
5829 member arguments can be general constant expressions that evaluate
5830 to a null value, but otherwise still need to be of a specific form. */
5831 if (cxx_dialect >= cxx11)
5833 if (TREE_CODE (expr) == PTRMEM_CST)
5834 /* A PTRMEM_CST is already constant, and a valid template
5835 argument for a parameter of pointer to member type, we just want
5836 to leave it in that form rather than lower it to a
5837 CONSTRUCTOR. */;
5838 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5839 expr = maybe_constant_value (expr);
5840 else if (TYPE_PTR_OR_PTRMEM_P (type))
5842 tree folded = maybe_constant_value (expr);
5843 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5844 : null_member_pointer_value_p (folded))
5845 expr = folded;
5849 /* HACK: Due to double coercion, we can get a
5850 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5851 which is the tree that we built on the first call (see
5852 below when coercing to reference to object or to reference to
5853 function). We just strip everything and get to the arg.
5854 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5855 for examples. */
5856 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5858 tree probe_type, probe = expr;
5859 if (REFERENCE_REF_P (probe))
5860 probe = TREE_OPERAND (probe, 0);
5861 probe_type = TREE_TYPE (probe);
5862 if (TREE_CODE (probe) == NOP_EXPR)
5864 /* ??? Maybe we could use convert_from_reference here, but we
5865 would need to relax its constraints because the NOP_EXPR
5866 could actually change the type to something more cv-qualified,
5867 and this is not folded by convert_from_reference. */
5868 tree addr = TREE_OPERAND (probe, 0);
5869 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5870 && TREE_CODE (addr) == ADDR_EXPR
5871 && TYPE_PTR_P (TREE_TYPE (addr))
5872 && (same_type_ignoring_top_level_qualifiers_p
5873 (TREE_TYPE (probe_type),
5874 TREE_TYPE (TREE_TYPE (addr)))))
5876 expr = TREE_OPERAND (addr, 0);
5877 expr_type = TREE_TYPE (probe_type);
5882 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5883 parameter is a pointer to object, through decay and
5884 qualification conversion. Let's strip everything. */
5885 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5887 tree probe = expr;
5888 STRIP_NOPS (probe);
5889 if (TREE_CODE (probe) == ADDR_EXPR
5890 && TYPE_PTR_P (TREE_TYPE (probe)))
5892 /* Skip the ADDR_EXPR only if it is part of the decay for
5893 an array. Otherwise, it is part of the original argument
5894 in the source code. */
5895 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5896 probe = TREE_OPERAND (probe, 0);
5897 expr = probe;
5898 expr_type = TREE_TYPE (expr);
5902 /* [temp.arg.nontype]/5, bullet 1
5904 For a non-type template-parameter of integral or enumeration type,
5905 integral promotions (_conv.prom_) and integral conversions
5906 (_conv.integral_) are applied. */
5907 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5909 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5910 t = maybe_constant_value (t);
5911 if (t != error_mark_node)
5912 expr = t;
5914 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5915 return error_mark_node;
5917 /* Notice that there are constant expressions like '4 % 0' which
5918 do not fold into integer constants. */
5919 if (TREE_CODE (expr) != INTEGER_CST)
5921 if (complain & tf_error)
5923 int errs = errorcount, warns = warningcount + werrorcount;
5924 if (processing_template_decl
5925 && !require_potential_constant_expression (expr))
5926 return NULL_TREE;
5927 expr = cxx_constant_value (expr);
5928 if (errorcount > errs || warningcount + werrorcount > warns)
5929 inform (EXPR_LOC_OR_LOC (expr, input_location),
5930 "in template argument for type %qT ", type);
5931 if (expr == error_mark_node)
5932 return NULL_TREE;
5933 /* else cxx_constant_value complained but gave us
5934 a real constant, so go ahead. */
5935 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5937 else
5938 return NULL_TREE;
5941 /* Avoid typedef problems. */
5942 if (TREE_TYPE (expr) != type)
5943 expr = fold_convert (type, expr);
5945 /* [temp.arg.nontype]/5, bullet 2
5947 For a non-type template-parameter of type pointer to object,
5948 qualification conversions (_conv.qual_) and the array-to-pointer
5949 conversion (_conv.array_) are applied. */
5950 else if (TYPE_PTROBV_P (type))
5952 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5954 A template-argument for a non-type, non-template template-parameter
5955 shall be one of: [...]
5957 -- the name of a non-type template-parameter;
5958 -- the address of an object or function with external linkage, [...]
5959 expressed as "& id-expression" where the & is optional if the name
5960 refers to a function or array, or if the corresponding
5961 template-parameter is a reference.
5963 Here, we do not care about functions, as they are invalid anyway
5964 for a parameter of type pointer-to-object. */
5966 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5967 /* Non-type template parameters are OK. */
5969 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5970 /* Null pointer values are OK in C++11. */;
5971 else if (TREE_CODE (expr) != ADDR_EXPR
5972 && TREE_CODE (expr_type) != ARRAY_TYPE)
5974 if (VAR_P (expr))
5976 if (complain & tf_error)
5977 error ("%qD is not a valid template argument "
5978 "because %qD is a variable, not the address of "
5979 "a variable", expr, expr);
5980 return NULL_TREE;
5982 if (POINTER_TYPE_P (expr_type))
5984 if (complain & tf_error)
5985 error ("%qE is not a valid template argument for %qT "
5986 "because it is not the address of a variable",
5987 expr, type);
5988 return NULL_TREE;
5990 /* Other values, like integer constants, might be valid
5991 non-type arguments of some other type. */
5992 return error_mark_node;
5994 else
5996 tree decl;
5998 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5999 ? TREE_OPERAND (expr, 0) : expr);
6000 if (!VAR_P (decl))
6002 if (complain & tf_error)
6003 error ("%qE is not a valid template argument of type %qT "
6004 "because %qE is not a variable", expr, type, decl);
6005 return NULL_TREE;
6007 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6009 if (complain & tf_error)
6010 error ("%qE is not a valid template argument of type %qT "
6011 "because %qD does not have external linkage",
6012 expr, type, decl);
6013 return NULL_TREE;
6015 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6017 if (complain & tf_error)
6018 error ("%qE is not a valid template argument of type %qT "
6019 "because %qD has no linkage", expr, type, decl);
6020 return NULL_TREE;
6024 expr = decay_conversion (expr, complain);
6025 if (expr == error_mark_node)
6026 return error_mark_node;
6028 expr = perform_qualification_conversions (type, expr);
6029 if (expr == error_mark_node)
6030 return error_mark_node;
6032 /* [temp.arg.nontype]/5, bullet 3
6034 For a non-type template-parameter of type reference to object, no
6035 conversions apply. The type referred to by the reference may be more
6036 cv-qualified than the (otherwise identical) type of the
6037 template-argument. The template-parameter is bound directly to the
6038 template-argument, which must be an lvalue. */
6039 else if (TYPE_REF_OBJ_P (type))
6041 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6042 expr_type))
6043 return error_mark_node;
6045 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6047 if (complain & tf_error)
6048 error ("%qE is not a valid template argument for type %qT "
6049 "because of conflicts in cv-qualification", expr, type);
6050 return NULL_TREE;
6053 if (!real_lvalue_p (expr))
6055 if (complain & tf_error)
6056 error ("%qE is not a valid template argument for type %qT "
6057 "because it is not an lvalue", expr, type);
6058 return NULL_TREE;
6061 /* [temp.arg.nontype]/1
6063 A template-argument for a non-type, non-template template-parameter
6064 shall be one of: [...]
6066 -- the address of an object or function with external linkage. */
6067 if (INDIRECT_REF_P (expr)
6068 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6070 expr = TREE_OPERAND (expr, 0);
6071 if (DECL_P (expr))
6073 if (complain & tf_error)
6074 error ("%q#D is not a valid template argument for type %qT "
6075 "because a reference variable does not have a constant "
6076 "address", expr, type);
6077 return NULL_TREE;
6081 if (!DECL_P (expr))
6083 if (complain & tf_error)
6084 error ("%qE is not a valid template argument for type %qT "
6085 "because it is not an object with external linkage",
6086 expr, type);
6087 return NULL_TREE;
6090 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6092 if (complain & tf_error)
6093 error ("%qE is not a valid template argument for type %qT "
6094 "because object %qD has not external linkage",
6095 expr, type, expr);
6096 return NULL_TREE;
6099 expr = build_nop (type, build_address (expr));
6101 /* [temp.arg.nontype]/5, bullet 4
6103 For a non-type template-parameter of type pointer to function, only
6104 the function-to-pointer conversion (_conv.func_) is applied. If the
6105 template-argument represents a set of overloaded functions (or a
6106 pointer to such), the matching function is selected from the set
6107 (_over.over_). */
6108 else if (TYPE_PTRFN_P (type))
6110 /* If the argument is a template-id, we might not have enough
6111 context information to decay the pointer. */
6112 if (!type_unknown_p (expr_type))
6114 expr = decay_conversion (expr, complain);
6115 if (expr == error_mark_node)
6116 return error_mark_node;
6119 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6120 /* Null pointer values are OK in C++11. */
6121 return perform_qualification_conversions (type, expr);
6123 expr = convert_nontype_argument_function (type, expr, complain);
6124 if (!expr || expr == error_mark_node)
6125 return expr;
6127 /* [temp.arg.nontype]/5, bullet 5
6129 For a non-type template-parameter of type reference to function, no
6130 conversions apply. If the template-argument represents a set of
6131 overloaded functions, the matching function is selected from the set
6132 (_over.over_). */
6133 else if (TYPE_REFFN_P (type))
6135 if (TREE_CODE (expr) == ADDR_EXPR)
6137 if (complain & tf_error)
6139 error ("%qE is not a valid template argument for type %qT "
6140 "because it is a pointer", expr, type);
6141 inform (input_location, "try using %qE instead",
6142 TREE_OPERAND (expr, 0));
6144 return NULL_TREE;
6147 expr = convert_nontype_argument_function (type, expr, complain);
6148 if (!expr || expr == error_mark_node)
6149 return expr;
6151 expr = build_nop (type, build_address (expr));
6153 /* [temp.arg.nontype]/5, bullet 6
6155 For a non-type template-parameter of type pointer to member function,
6156 no conversions apply. If the template-argument represents a set of
6157 overloaded member functions, the matching member function is selected
6158 from the set (_over.over_). */
6159 else if (TYPE_PTRMEMFUNC_P (type))
6161 expr = instantiate_type (type, expr, tf_none);
6162 if (expr == error_mark_node)
6163 return error_mark_node;
6165 /* [temp.arg.nontype] bullet 1 says the pointer to member
6166 expression must be a pointer-to-member constant. */
6167 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6168 return error_mark_node;
6170 /* There is no way to disable standard conversions in
6171 resolve_address_of_overloaded_function (called by
6172 instantiate_type). It is possible that the call succeeded by
6173 converting &B::I to &D::I (where B is a base of D), so we need
6174 to reject this conversion here.
6176 Actually, even if there was a way to disable standard conversions,
6177 it would still be better to reject them here so that we can
6178 provide a superior diagnostic. */
6179 if (!same_type_p (TREE_TYPE (expr), type))
6181 if (complain & tf_error)
6183 error ("%qE is not a valid template argument for type %qT "
6184 "because it is of type %qT", expr, type,
6185 TREE_TYPE (expr));
6186 /* If we are just one standard conversion off, explain. */
6187 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6188 inform (input_location,
6189 "standard conversions are not allowed in this context");
6191 return NULL_TREE;
6194 /* [temp.arg.nontype]/5, bullet 7
6196 For a non-type template-parameter of type pointer to data member,
6197 qualification conversions (_conv.qual_) are applied. */
6198 else if (TYPE_PTRDATAMEM_P (type))
6200 /* [temp.arg.nontype] bullet 1 says the pointer to member
6201 expression must be a pointer-to-member constant. */
6202 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6203 return error_mark_node;
6205 expr = perform_qualification_conversions (type, expr);
6206 if (expr == error_mark_node)
6207 return expr;
6209 else if (NULLPTR_TYPE_P (type))
6211 if (expr != nullptr_node)
6213 if (complain & tf_error)
6214 error ("%qE is not a valid template argument for type %qT "
6215 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6216 return NULL_TREE;
6218 return expr;
6220 /* A template non-type parameter must be one of the above. */
6221 else
6222 gcc_unreachable ();
6224 /* Sanity check: did we actually convert the argument to the
6225 right type? */
6226 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6227 (type, TREE_TYPE (expr)));
6228 return convert_from_reference (expr);
6231 /* Subroutine of coerce_template_template_parms, which returns 1 if
6232 PARM_PARM and ARG_PARM match using the rule for the template
6233 parameters of template template parameters. Both PARM and ARG are
6234 template parameters; the rest of the arguments are the same as for
6235 coerce_template_template_parms.
6237 static int
6238 coerce_template_template_parm (tree parm,
6239 tree arg,
6240 tsubst_flags_t complain,
6241 tree in_decl,
6242 tree outer_args)
6244 if (arg == NULL_TREE || error_operand_p (arg)
6245 || parm == NULL_TREE || error_operand_p (parm))
6246 return 0;
6248 if (TREE_CODE (arg) != TREE_CODE (parm))
6249 return 0;
6251 switch (TREE_CODE (parm))
6253 case TEMPLATE_DECL:
6254 /* We encounter instantiations of templates like
6255 template <template <template <class> class> class TT>
6256 class C; */
6258 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6259 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6261 if (!coerce_template_template_parms
6262 (parmparm, argparm, complain, in_decl, outer_args))
6263 return 0;
6265 /* Fall through. */
6267 case TYPE_DECL:
6268 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6269 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6270 /* Argument is a parameter pack but parameter is not. */
6271 return 0;
6272 break;
6274 case PARM_DECL:
6275 /* The tsubst call is used to handle cases such as
6277 template <int> class C {};
6278 template <class T, template <T> class TT> class D {};
6279 D<int, C> d;
6281 i.e. the parameter list of TT depends on earlier parameters. */
6282 if (!uses_template_parms (TREE_TYPE (arg))
6283 && !same_type_p
6284 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6285 TREE_TYPE (arg)))
6286 return 0;
6288 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6289 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6290 /* Argument is a parameter pack but parameter is not. */
6291 return 0;
6293 break;
6295 default:
6296 gcc_unreachable ();
6299 return 1;
6303 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6304 template template parameters. Both PARM_PARMS and ARG_PARMS are
6305 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6306 or PARM_DECL.
6308 Consider the example:
6309 template <class T> class A;
6310 template<template <class U> class TT> class B;
6312 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6313 the parameters to A, and OUTER_ARGS contains A. */
6315 static int
6316 coerce_template_template_parms (tree parm_parms,
6317 tree arg_parms,
6318 tsubst_flags_t complain,
6319 tree in_decl,
6320 tree outer_args)
6322 int nparms, nargs, i;
6323 tree parm, arg;
6324 int variadic_p = 0;
6326 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6327 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6329 nparms = TREE_VEC_LENGTH (parm_parms);
6330 nargs = TREE_VEC_LENGTH (arg_parms);
6332 /* Determine whether we have a parameter pack at the end of the
6333 template template parameter's template parameter list. */
6334 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6336 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6338 if (error_operand_p (parm))
6339 return 0;
6341 switch (TREE_CODE (parm))
6343 case TEMPLATE_DECL:
6344 case TYPE_DECL:
6345 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6346 variadic_p = 1;
6347 break;
6349 case PARM_DECL:
6350 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6351 variadic_p = 1;
6352 break;
6354 default:
6355 gcc_unreachable ();
6359 if (nargs != nparms
6360 && !(variadic_p && nargs >= nparms - 1))
6361 return 0;
6363 /* Check all of the template parameters except the parameter pack at
6364 the end (if any). */
6365 for (i = 0; i < nparms - variadic_p; ++i)
6367 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6368 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6369 continue;
6371 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6372 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6374 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6375 outer_args))
6376 return 0;
6380 if (variadic_p)
6382 /* Check each of the template parameters in the template
6383 argument against the template parameter pack at the end of
6384 the template template parameter. */
6385 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6386 return 0;
6388 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6390 for (; i < nargs; ++i)
6392 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6393 continue;
6395 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6397 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6398 outer_args))
6399 return 0;
6403 return 1;
6406 /* Verifies that the deduced template arguments (in TARGS) for the
6407 template template parameters (in TPARMS) represent valid bindings,
6408 by comparing the template parameter list of each template argument
6409 to the template parameter list of its corresponding template
6410 template parameter, in accordance with DR150. This
6411 routine can only be called after all template arguments have been
6412 deduced. It will return TRUE if all of the template template
6413 parameter bindings are okay, FALSE otherwise. */
6414 bool
6415 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6417 int i, ntparms = TREE_VEC_LENGTH (tparms);
6418 bool ret = true;
6420 /* We're dealing with template parms in this process. */
6421 ++processing_template_decl;
6423 targs = INNERMOST_TEMPLATE_ARGS (targs);
6425 for (i = 0; i < ntparms; ++i)
6427 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6428 tree targ = TREE_VEC_ELT (targs, i);
6430 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6432 tree packed_args = NULL_TREE;
6433 int idx, len = 1;
6435 if (ARGUMENT_PACK_P (targ))
6437 /* Look inside the argument pack. */
6438 packed_args = ARGUMENT_PACK_ARGS (targ);
6439 len = TREE_VEC_LENGTH (packed_args);
6442 for (idx = 0; idx < len; ++idx)
6444 tree targ_parms = NULL_TREE;
6446 if (packed_args)
6447 /* Extract the next argument from the argument
6448 pack. */
6449 targ = TREE_VEC_ELT (packed_args, idx);
6451 if (PACK_EXPANSION_P (targ))
6452 /* Look at the pattern of the pack expansion. */
6453 targ = PACK_EXPANSION_PATTERN (targ);
6455 /* Extract the template parameters from the template
6456 argument. */
6457 if (TREE_CODE (targ) == TEMPLATE_DECL)
6458 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6459 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6460 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6462 /* Verify that we can coerce the template template
6463 parameters from the template argument to the template
6464 parameter. This requires an exact match. */
6465 if (targ_parms
6466 && !coerce_template_template_parms
6467 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6468 targ_parms,
6469 tf_none,
6470 tparm,
6471 targs))
6473 ret = false;
6474 goto out;
6480 out:
6482 --processing_template_decl;
6483 return ret;
6486 /* Since type attributes aren't mangled, we need to strip them from
6487 template type arguments. */
6489 static tree
6490 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6492 tree mv;
6493 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6494 return arg;
6495 mv = TYPE_MAIN_VARIANT (arg);
6496 arg = strip_typedefs (arg);
6497 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6498 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6500 if (complain & tf_warning)
6501 warning (0, "ignoring attributes on template argument %qT", arg);
6502 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6503 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6505 return arg;
6508 /* Convert the indicated template ARG as necessary to match the
6509 indicated template PARM. Returns the converted ARG, or
6510 error_mark_node if the conversion was unsuccessful. Error and
6511 warning messages are issued under control of COMPLAIN. This
6512 conversion is for the Ith parameter in the parameter list. ARGS is
6513 the full set of template arguments deduced so far. */
6515 static tree
6516 convert_template_argument (tree parm,
6517 tree arg,
6518 tree args,
6519 tsubst_flags_t complain,
6520 int i,
6521 tree in_decl)
6523 tree orig_arg;
6524 tree val;
6525 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6527 if (TREE_CODE (arg) == TREE_LIST
6528 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6530 /* The template argument was the name of some
6531 member function. That's usually
6532 invalid, but static members are OK. In any
6533 case, grab the underlying fields/functions
6534 and issue an error later if required. */
6535 orig_arg = TREE_VALUE (arg);
6536 TREE_TYPE (arg) = unknown_type_node;
6539 orig_arg = arg;
6541 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6542 requires_type = (TREE_CODE (parm) == TYPE_DECL
6543 || requires_tmpl_type);
6545 /* When determining whether an argument pack expansion is a template,
6546 look at the pattern. */
6547 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6548 arg = PACK_EXPANSION_PATTERN (arg);
6550 /* Deal with an injected-class-name used as a template template arg. */
6551 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6553 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6554 if (TREE_CODE (t) == TEMPLATE_DECL)
6556 if (cxx_dialect >= cxx11)
6557 /* OK under DR 1004. */;
6558 else if (complain & tf_warning_or_error)
6559 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6560 " used as template template argument", TYPE_NAME (arg));
6561 else if (flag_pedantic_errors)
6562 t = arg;
6564 arg = t;
6568 is_tmpl_type =
6569 ((TREE_CODE (arg) == TEMPLATE_DECL
6570 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6571 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6572 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6573 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6575 if (is_tmpl_type
6576 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6577 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6578 arg = TYPE_STUB_DECL (arg);
6580 is_type = TYPE_P (arg) || is_tmpl_type;
6582 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6583 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6585 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6587 if (complain & tf_error)
6588 error ("invalid use of destructor %qE as a type", orig_arg);
6589 return error_mark_node;
6592 permerror (input_location,
6593 "to refer to a type member of a template parameter, "
6594 "use %<typename %E%>", orig_arg);
6596 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6597 TREE_OPERAND (arg, 1),
6598 typename_type,
6599 complain);
6600 arg = orig_arg;
6601 is_type = 1;
6603 if (is_type != requires_type)
6605 if (in_decl)
6607 if (complain & tf_error)
6609 error ("type/value mismatch at argument %d in template "
6610 "parameter list for %qD",
6611 i + 1, in_decl);
6612 if (is_type)
6613 inform (input_location,
6614 " expected a constant of type %qT, got %qT",
6615 TREE_TYPE (parm),
6616 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6617 else if (requires_tmpl_type)
6618 inform (input_location,
6619 " expected a class template, got %qE", orig_arg);
6620 else
6621 inform (input_location,
6622 " expected a type, got %qE", orig_arg);
6625 return error_mark_node;
6627 if (is_tmpl_type ^ requires_tmpl_type)
6629 if (in_decl && (complain & tf_error))
6631 error ("type/value mismatch at argument %d in template "
6632 "parameter list for %qD",
6633 i + 1, in_decl);
6634 if (is_tmpl_type)
6635 inform (input_location,
6636 " expected a type, got %qT", DECL_NAME (arg));
6637 else
6638 inform (input_location,
6639 " expected a class template, got %qT", orig_arg);
6641 return error_mark_node;
6644 if (is_type)
6646 if (requires_tmpl_type)
6648 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6649 val = orig_arg;
6650 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6651 /* The number of argument required is not known yet.
6652 Just accept it for now. */
6653 val = TREE_TYPE (arg);
6654 else
6656 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6657 tree argparm;
6659 /* Strip alias templates that are equivalent to another
6660 template. */
6661 arg = get_underlying_template (arg);
6662 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6664 if (coerce_template_template_parms (parmparm, argparm,
6665 complain, in_decl,
6666 args))
6668 val = arg;
6670 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6671 TEMPLATE_DECL. */
6672 if (val != error_mark_node)
6674 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6675 val = TREE_TYPE (val);
6676 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6677 val = make_pack_expansion (val);
6680 else
6682 if (in_decl && (complain & tf_error))
6684 error ("type/value mismatch at argument %d in "
6685 "template parameter list for %qD",
6686 i + 1, in_decl);
6687 inform (input_location,
6688 " expected a template of type %qD, got %qT",
6689 parm, orig_arg);
6692 val = error_mark_node;
6696 else
6697 val = orig_arg;
6698 /* We only form one instance of each template specialization.
6699 Therefore, if we use a non-canonical variant (i.e., a
6700 typedef), any future messages referring to the type will use
6701 the typedef, which is confusing if those future uses do not
6702 themselves also use the typedef. */
6703 if (TYPE_P (val))
6704 val = canonicalize_type_argument (val, complain);
6706 else
6708 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6710 if (invalid_nontype_parm_type_p (t, complain))
6711 return error_mark_node;
6713 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6715 if (same_type_p (t, TREE_TYPE (orig_arg)))
6716 val = orig_arg;
6717 else
6719 /* Not sure if this is reachable, but it doesn't hurt
6720 to be robust. */
6721 error ("type mismatch in nontype parameter pack");
6722 val = error_mark_node;
6725 else if (!dependent_template_arg_p (orig_arg)
6726 && !uses_template_parms (t))
6727 /* We used to call digest_init here. However, digest_init
6728 will report errors, which we don't want when complain
6729 is zero. More importantly, digest_init will try too
6730 hard to convert things: for example, `0' should not be
6731 converted to pointer type at this point according to
6732 the standard. Accepting this is not merely an
6733 extension, since deciding whether or not these
6734 conversions can occur is part of determining which
6735 function template to call, or whether a given explicit
6736 argument specification is valid. */
6737 val = convert_nontype_argument (t, orig_arg, complain);
6738 else
6739 val = strip_typedefs_expr (orig_arg);
6741 if (val == NULL_TREE)
6742 val = error_mark_node;
6743 else if (val == error_mark_node && (complain & tf_error))
6744 error ("could not convert template argument %qE to %qT", orig_arg, t);
6746 if (TREE_CODE (val) == SCOPE_REF)
6748 /* Strip typedefs from the SCOPE_REF. */
6749 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6750 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6751 complain);
6752 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6753 QUALIFIED_NAME_IS_TEMPLATE (val));
6757 return val;
6760 /* Coerces the remaining template arguments in INNER_ARGS (from
6761 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6762 Returns the coerced argument pack. PARM_IDX is the position of this
6763 parameter in the template parameter list. ARGS is the original
6764 template argument list. */
6765 static tree
6766 coerce_template_parameter_pack (tree parms,
6767 int parm_idx,
6768 tree args,
6769 tree inner_args,
6770 int arg_idx,
6771 tree new_args,
6772 int* lost,
6773 tree in_decl,
6774 tsubst_flags_t complain)
6776 tree parm = TREE_VEC_ELT (parms, parm_idx);
6777 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6778 tree packed_args;
6779 tree argument_pack;
6780 tree packed_parms = NULL_TREE;
6782 if (arg_idx > nargs)
6783 arg_idx = nargs;
6785 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6787 /* When the template parameter is a non-type template parameter pack
6788 or template template parameter pack whose type or template
6789 parameters use parameter packs, we know exactly how many arguments
6790 we are looking for. Build a vector of the instantiated decls for
6791 these template parameters in PACKED_PARMS. */
6792 /* We can't use make_pack_expansion here because it would interpret a
6793 _DECL as a use rather than a declaration. */
6794 tree decl = TREE_VALUE (parm);
6795 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6796 SET_PACK_EXPANSION_PATTERN (exp, decl);
6797 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6798 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6800 TREE_VEC_LENGTH (args)--;
6801 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6802 TREE_VEC_LENGTH (args)++;
6804 if (packed_parms == error_mark_node)
6805 return error_mark_node;
6807 /* If we're doing a partial instantiation of a member template,
6808 verify that all of the types used for the non-type
6809 template parameter pack are, in fact, valid for non-type
6810 template parameters. */
6811 if (arg_idx < nargs
6812 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6814 int j, len = TREE_VEC_LENGTH (packed_parms);
6815 for (j = 0; j < len; ++j)
6817 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6818 if (invalid_nontype_parm_type_p (t, complain))
6819 return error_mark_node;
6823 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6825 else
6826 packed_args = make_tree_vec (nargs - arg_idx);
6828 /* Convert the remaining arguments, which will be a part of the
6829 parameter pack "parm". */
6830 for (; arg_idx < nargs; ++arg_idx)
6832 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6833 tree actual_parm = TREE_VALUE (parm);
6834 int pack_idx = arg_idx - parm_idx;
6836 if (packed_parms)
6838 /* Once we've packed as many args as we have types, stop. */
6839 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6840 break;
6841 else if (PACK_EXPANSION_P (arg))
6842 /* We don't know how many args we have yet, just
6843 use the unconverted ones for now. */
6844 return NULL_TREE;
6845 else
6846 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6849 if (arg == error_mark_node)
6851 if (complain & tf_error)
6852 error ("template argument %d is invalid", arg_idx + 1);
6854 else
6855 arg = convert_template_argument (actual_parm,
6856 arg, new_args, complain, parm_idx,
6857 in_decl);
6858 if (arg == error_mark_node)
6859 (*lost)++;
6860 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6863 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6864 && TREE_VEC_LENGTH (packed_args) > 0)
6866 if (complain & tf_error)
6867 error ("wrong number of template arguments (%d, should be %d)",
6868 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6869 return error_mark_node;
6872 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6873 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6874 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6875 else
6877 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6878 TREE_TYPE (argument_pack)
6879 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6880 TREE_CONSTANT (argument_pack) = 1;
6883 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6884 #ifdef ENABLE_CHECKING
6885 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6886 TREE_VEC_LENGTH (packed_args));
6887 #endif
6888 return argument_pack;
6891 /* Returns the number of pack expansions in the template argument vector
6892 ARGS. */
6894 static int
6895 pack_expansion_args_count (tree args)
6897 int i;
6898 int count = 0;
6899 if (args)
6900 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6902 tree elt = TREE_VEC_ELT (args, i);
6903 if (elt && PACK_EXPANSION_P (elt))
6904 ++count;
6906 return count;
6909 /* Convert all template arguments to their appropriate types, and
6910 return a vector containing the innermost resulting template
6911 arguments. If any error occurs, return error_mark_node. Error and
6912 warning messages are issued under control of COMPLAIN.
6914 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6915 for arguments not specified in ARGS. Otherwise, if
6916 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6917 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6918 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6919 ARGS. */
6921 static tree
6922 coerce_template_parms (tree parms,
6923 tree args,
6924 tree in_decl,
6925 tsubst_flags_t complain,
6926 bool require_all_args,
6927 bool use_default_args)
6929 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6930 tree orig_inner_args;
6931 tree inner_args;
6932 tree new_args;
6933 tree new_inner_args;
6934 int saved_unevaluated_operand;
6935 int saved_inhibit_evaluation_warnings;
6937 /* When used as a boolean value, indicates whether this is a
6938 variadic template parameter list. Since it's an int, we can also
6939 subtract it from nparms to get the number of non-variadic
6940 parameters. */
6941 int variadic_p = 0;
6942 int variadic_args_p = 0;
6943 int post_variadic_parms = 0;
6945 /* Likewise for parameters with default arguments. */
6946 int default_p = 0;
6948 if (args == error_mark_node)
6949 return error_mark_node;
6951 nparms = TREE_VEC_LENGTH (parms);
6953 /* Determine if there are any parameter packs or default arguments. */
6954 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6956 tree parm = TREE_VEC_ELT (parms, parm_idx);
6957 if (variadic_p)
6958 ++post_variadic_parms;
6959 if (template_parameter_pack_p (TREE_VALUE (parm)))
6960 ++variadic_p;
6961 if (TREE_PURPOSE (parm))
6962 ++default_p;
6965 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6966 /* If there are no parameters that follow a parameter pack, we need to
6967 expand any argument packs so that we can deduce a parameter pack from
6968 some non-packed args followed by an argument pack, as in variadic85.C.
6969 If there are such parameters, we need to leave argument packs intact
6970 so the arguments are assigned properly. This can happen when dealing
6971 with a nested class inside a partial specialization of a class
6972 template, as in variadic92.C, or when deducing a template parameter pack
6973 from a sub-declarator, as in variadic114.C. */
6974 if (!post_variadic_parms)
6975 inner_args = expand_template_argument_pack (inner_args);
6977 /* Count any pack expansion args. */
6978 variadic_args_p = pack_expansion_args_count (inner_args);
6980 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6981 if ((nargs > nparms && !variadic_p)
6982 || (nargs < nparms - variadic_p
6983 && require_all_args
6984 && !variadic_args_p
6985 && (!use_default_args
6986 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6987 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6989 if (complain & tf_error)
6991 if (variadic_p || default_p)
6993 nparms -= variadic_p + default_p;
6994 error ("wrong number of template arguments "
6995 "(%d, should be at least %d)", nargs, nparms);
6997 else
6998 error ("wrong number of template arguments "
6999 "(%d, should be %d)", nargs, nparms);
7001 if (in_decl)
7002 inform (input_location, "provided for %q+D", in_decl);
7005 return error_mark_node;
7007 /* We can't pass a pack expansion to a non-pack parameter of an alias
7008 template (DR 1430). */
7009 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7010 && variadic_args_p
7011 && nargs - variadic_args_p < nparms - variadic_p)
7013 if (complain & tf_error)
7015 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7017 tree arg = TREE_VEC_ELT (inner_args, i);
7018 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7020 if (PACK_EXPANSION_P (arg)
7021 && !template_parameter_pack_p (parm))
7023 error ("pack expansion argument for non-pack parameter "
7024 "%qD of alias template %qD", parm, in_decl);
7025 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7026 goto found;
7029 gcc_unreachable ();
7030 found:;
7032 return error_mark_node;
7035 /* We need to evaluate the template arguments, even though this
7036 template-id may be nested within a "sizeof". */
7037 saved_unevaluated_operand = cp_unevaluated_operand;
7038 cp_unevaluated_operand = 0;
7039 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7040 c_inhibit_evaluation_warnings = 0;
7041 new_inner_args = make_tree_vec (nparms);
7042 new_args = add_outermost_template_args (args, new_inner_args);
7043 int pack_adjust = 0;
7044 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7046 tree arg;
7047 tree parm;
7049 /* Get the Ith template parameter. */
7050 parm = TREE_VEC_ELT (parms, parm_idx);
7052 if (parm == error_mark_node)
7054 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7055 continue;
7058 /* Calculate the next argument. */
7059 if (arg_idx < nargs)
7060 arg = TREE_VEC_ELT (inner_args, arg_idx);
7061 else
7062 arg = NULL_TREE;
7064 if (template_parameter_pack_p (TREE_VALUE (parm))
7065 && !(arg && ARGUMENT_PACK_P (arg)))
7067 /* Some arguments will be placed in the
7068 template parameter pack PARM. */
7069 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7070 inner_args, arg_idx,
7071 new_args, &lost,
7072 in_decl, complain);
7074 if (arg == NULL_TREE)
7076 /* We don't know how many args we have yet, just use the
7077 unconverted (and still packed) ones for now. */
7078 new_inner_args = orig_inner_args;
7079 arg_idx = nargs;
7080 break;
7083 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7085 /* Store this argument. */
7086 if (arg == error_mark_node)
7088 lost++;
7089 /* We are done with all of the arguments. */
7090 arg_idx = nargs;
7092 else
7094 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7095 arg_idx += pack_adjust;
7098 continue;
7100 else if (arg)
7102 if (PACK_EXPANSION_P (arg))
7104 /* "If every valid specialization of a variadic template
7105 requires an empty template parameter pack, the template is
7106 ill-formed, no diagnostic required." So check that the
7107 pattern works with this parameter. */
7108 tree pattern = PACK_EXPANSION_PATTERN (arg);
7109 tree conv = convert_template_argument (TREE_VALUE (parm),
7110 pattern, new_args,
7111 complain, parm_idx,
7112 in_decl);
7113 if (conv == error_mark_node)
7115 inform (input_location, "so any instantiation with a "
7116 "non-empty parameter pack would be ill-formed");
7117 ++lost;
7119 else if (TYPE_P (conv) && !TYPE_P (pattern))
7120 /* Recover from missing typename. */
7121 TREE_VEC_ELT (inner_args, arg_idx)
7122 = make_pack_expansion (conv);
7124 /* We don't know how many args we have yet, just
7125 use the unconverted ones for now. */
7126 new_inner_args = inner_args;
7127 arg_idx = nargs;
7128 break;
7131 else if (require_all_args)
7133 /* There must be a default arg in this case. */
7134 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7135 complain, in_decl);
7136 /* The position of the first default template argument,
7137 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7138 Record that. */
7139 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7140 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7141 arg_idx - pack_adjust);
7143 else
7144 break;
7146 if (arg == error_mark_node)
7148 if (complain & tf_error)
7149 error ("template argument %d is invalid", arg_idx + 1);
7151 else if (!arg)
7152 /* This only occurs if there was an error in the template
7153 parameter list itself (which we would already have
7154 reported) that we are trying to recover from, e.g., a class
7155 template with a parameter list such as
7156 template<typename..., typename>. */
7157 ++lost;
7158 else
7159 arg = convert_template_argument (TREE_VALUE (parm),
7160 arg, new_args, complain,
7161 parm_idx, in_decl);
7163 if (arg == error_mark_node)
7164 lost++;
7165 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7167 cp_unevaluated_operand = saved_unevaluated_operand;
7168 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7170 if (variadic_p && arg_idx < nargs)
7172 if (complain & tf_error)
7174 error ("wrong number of template arguments "
7175 "(%d, should be %d)", nargs, arg_idx);
7176 if (in_decl)
7177 error ("provided for %q+D", in_decl);
7179 return error_mark_node;
7182 if (lost)
7183 return error_mark_node;
7185 #ifdef ENABLE_CHECKING
7186 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7187 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7188 TREE_VEC_LENGTH (new_inner_args));
7189 #endif
7191 return new_inner_args;
7194 /* Like coerce_template_parms. If PARMS represents all template
7195 parameters levels, this function returns a vector of vectors
7196 representing all the resulting argument levels. Note that in this
7197 case, only the innermost arguments are coerced because the
7198 outermost ones are supposed to have been coerced already.
7200 Otherwise, if PARMS represents only (the innermost) vector of
7201 parameters, this function returns a vector containing just the
7202 innermost resulting arguments. */
7204 static tree
7205 coerce_innermost_template_parms (tree parms,
7206 tree args,
7207 tree in_decl,
7208 tsubst_flags_t complain,
7209 bool require_all_args,
7210 bool use_default_args)
7212 int parms_depth = TMPL_PARMS_DEPTH (parms);
7213 int args_depth = TMPL_ARGS_DEPTH (args);
7214 tree coerced_args;
7216 if (parms_depth > 1)
7218 coerced_args = make_tree_vec (parms_depth);
7219 tree level;
7220 int cur_depth;
7222 for (level = parms, cur_depth = parms_depth;
7223 parms_depth > 0 && level != NULL_TREE;
7224 level = TREE_CHAIN (level), --cur_depth)
7226 tree l;
7227 if (cur_depth == args_depth)
7228 l = coerce_template_parms (TREE_VALUE (level),
7229 args, in_decl, complain,
7230 require_all_args,
7231 use_default_args);
7232 else
7233 l = TMPL_ARGS_LEVEL (args, cur_depth);
7235 if (l == error_mark_node)
7236 return error_mark_node;
7238 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7241 else
7242 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7243 args, in_decl, complain,
7244 require_all_args,
7245 use_default_args);
7246 return coerced_args;
7249 /* Returns 1 if template args OT and NT are equivalent. */
7251 static int
7252 template_args_equal (tree ot, tree nt)
7254 if (nt == ot)
7255 return 1;
7256 if (nt == NULL_TREE || ot == NULL_TREE)
7257 return false;
7259 if (TREE_CODE (nt) == TREE_VEC)
7260 /* For member templates */
7261 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7262 else if (PACK_EXPANSION_P (ot))
7263 return (PACK_EXPANSION_P (nt)
7264 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7265 PACK_EXPANSION_PATTERN (nt))
7266 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7267 PACK_EXPANSION_EXTRA_ARGS (nt)));
7268 else if (ARGUMENT_PACK_P (ot))
7270 int i, len;
7271 tree opack, npack;
7273 if (!ARGUMENT_PACK_P (nt))
7274 return 0;
7276 opack = ARGUMENT_PACK_ARGS (ot);
7277 npack = ARGUMENT_PACK_ARGS (nt);
7278 len = TREE_VEC_LENGTH (opack);
7279 if (TREE_VEC_LENGTH (npack) != len)
7280 return 0;
7281 for (i = 0; i < len; ++i)
7282 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7283 TREE_VEC_ELT (npack, i)))
7284 return 0;
7285 return 1;
7287 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7289 /* We get here probably because we are in the middle of substituting
7290 into the pattern of a pack expansion. In that case the
7291 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7292 interested in. So we want to use the initial pack argument for
7293 the comparison. */
7294 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7295 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7296 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7297 return template_args_equal (ot, nt);
7299 else if (TYPE_P (nt))
7301 if (!TYPE_P (ot))
7302 return false;
7303 /* Don't treat an alias template specialization with dependent
7304 arguments as equivalent to its underlying type when used as a
7305 template argument; we need them to be distinct so that we
7306 substitute into the specialization arguments at instantiation
7307 time. And aliases can't be equivalent without being ==, so
7308 we don't need to look any deeper. */
7309 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7310 return false;
7311 else
7312 return same_type_p (ot, nt);
7314 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7315 return 0;
7316 else
7317 return cp_tree_equal (ot, nt);
7320 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7321 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7322 NEWARG_PTR with the offending arguments if they are non-NULL. */
7324 static int
7325 comp_template_args_with_info (tree oldargs, tree newargs,
7326 tree *oldarg_ptr, tree *newarg_ptr)
7328 int i;
7330 if (oldargs == newargs)
7331 return 1;
7333 if (!oldargs || !newargs)
7334 return 0;
7336 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7337 return 0;
7339 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7341 tree nt = TREE_VEC_ELT (newargs, i);
7342 tree ot = TREE_VEC_ELT (oldargs, i);
7344 if (! template_args_equal (ot, nt))
7346 if (oldarg_ptr != NULL)
7347 *oldarg_ptr = ot;
7348 if (newarg_ptr != NULL)
7349 *newarg_ptr = nt;
7350 return 0;
7353 return 1;
7356 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7357 of template arguments. Returns 0 otherwise. */
7360 comp_template_args (tree oldargs, tree newargs)
7362 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7365 static void
7366 add_pending_template (tree d)
7368 tree ti = (TYPE_P (d)
7369 ? CLASSTYPE_TEMPLATE_INFO (d)
7370 : DECL_TEMPLATE_INFO (d));
7371 struct pending_template *pt;
7372 int level;
7374 if (TI_PENDING_TEMPLATE_FLAG (ti))
7375 return;
7377 /* We are called both from instantiate_decl, where we've already had a
7378 tinst_level pushed, and instantiate_template, where we haven't.
7379 Compensate. */
7380 level = !current_tinst_level || current_tinst_level->decl != d;
7382 if (level)
7383 push_tinst_level (d);
7385 pt = ggc_alloc<pending_template> ();
7386 pt->next = NULL;
7387 pt->tinst = current_tinst_level;
7388 if (last_pending_template)
7389 last_pending_template->next = pt;
7390 else
7391 pending_templates = pt;
7393 last_pending_template = pt;
7395 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7397 if (level)
7398 pop_tinst_level ();
7402 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7403 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7404 documentation for TEMPLATE_ID_EXPR. */
7406 tree
7407 lookup_template_function (tree fns, tree arglist)
7409 tree type;
7411 if (fns == error_mark_node || arglist == error_mark_node)
7412 return error_mark_node;
7414 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7416 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7418 error ("%q#D is not a function template", fns);
7419 return error_mark_node;
7422 if (BASELINK_P (fns))
7424 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7425 unknown_type_node,
7426 BASELINK_FUNCTIONS (fns),
7427 arglist);
7428 return fns;
7431 type = TREE_TYPE (fns);
7432 if (TREE_CODE (fns) == OVERLOAD || !type)
7433 type = unknown_type_node;
7435 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7438 /* Within the scope of a template class S<T>, the name S gets bound
7439 (in build_self_reference) to a TYPE_DECL for the class, not a
7440 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7441 or one of its enclosing classes, and that type is a template,
7442 return the associated TEMPLATE_DECL. Otherwise, the original
7443 DECL is returned.
7445 Also handle the case when DECL is a TREE_LIST of ambiguous
7446 injected-class-names from different bases. */
7448 tree
7449 maybe_get_template_decl_from_type_decl (tree decl)
7451 if (decl == NULL_TREE)
7452 return decl;
7454 /* DR 176: A lookup that finds an injected-class-name (10.2
7455 [class.member.lookup]) can result in an ambiguity in certain cases
7456 (for example, if it is found in more than one base class). If all of
7457 the injected-class-names that are found refer to specializations of
7458 the same class template, and if the name is followed by a
7459 template-argument-list, the reference refers to the class template
7460 itself and not a specialization thereof, and is not ambiguous. */
7461 if (TREE_CODE (decl) == TREE_LIST)
7463 tree t, tmpl = NULL_TREE;
7464 for (t = decl; t; t = TREE_CHAIN (t))
7466 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7467 if (!tmpl)
7468 tmpl = elt;
7469 else if (tmpl != elt)
7470 break;
7472 if (tmpl && t == NULL_TREE)
7473 return tmpl;
7474 else
7475 return decl;
7478 return (decl != NULL_TREE
7479 && DECL_SELF_REFERENCE_P (decl)
7480 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7481 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7484 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7485 parameters, find the desired type.
7487 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7489 IN_DECL, if non-NULL, is the template declaration we are trying to
7490 instantiate.
7492 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7493 the class we are looking up.
7495 Issue error and warning messages under control of COMPLAIN.
7497 If the template class is really a local class in a template
7498 function, then the FUNCTION_CONTEXT is the function in which it is
7499 being instantiated.
7501 ??? Note that this function is currently called *twice* for each
7502 template-id: the first time from the parser, while creating the
7503 incomplete type (finish_template_type), and the second type during the
7504 real instantiation (instantiate_template_class). This is surely something
7505 that we want to avoid. It also causes some problems with argument
7506 coercion (see convert_nontype_argument for more information on this). */
7508 static tree
7509 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7510 int entering_scope, tsubst_flags_t complain)
7512 tree templ = NULL_TREE, parmlist;
7513 tree t;
7514 spec_entry **slot;
7515 spec_entry *entry;
7516 spec_entry elt;
7517 hashval_t hash;
7519 if (identifier_p (d1))
7521 tree value = innermost_non_namespace_value (d1);
7522 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7523 templ = value;
7524 else
7526 if (context)
7527 push_decl_namespace (context);
7528 templ = lookup_name (d1);
7529 templ = maybe_get_template_decl_from_type_decl (templ);
7530 if (context)
7531 pop_decl_namespace ();
7533 if (templ)
7534 context = DECL_CONTEXT (templ);
7536 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7538 tree type = TREE_TYPE (d1);
7540 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7541 an implicit typename for the second A. Deal with it. */
7542 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7543 type = TREE_TYPE (type);
7545 if (CLASSTYPE_TEMPLATE_INFO (type))
7547 templ = CLASSTYPE_TI_TEMPLATE (type);
7548 d1 = DECL_NAME (templ);
7551 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7552 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7554 templ = TYPE_TI_TEMPLATE (d1);
7555 d1 = DECL_NAME (templ);
7557 else if (DECL_TYPE_TEMPLATE_P (d1))
7559 templ = d1;
7560 d1 = DECL_NAME (templ);
7561 context = DECL_CONTEXT (templ);
7563 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7565 templ = d1;
7566 d1 = DECL_NAME (templ);
7569 /* Issue an error message if we didn't find a template. */
7570 if (! templ)
7572 if (complain & tf_error)
7573 error ("%qT is not a template", d1);
7574 return error_mark_node;
7577 if (TREE_CODE (templ) != TEMPLATE_DECL
7578 /* Make sure it's a user visible template, if it was named by
7579 the user. */
7580 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7581 && !PRIMARY_TEMPLATE_P (templ)))
7583 if (complain & tf_error)
7585 error ("non-template type %qT used as a template", d1);
7586 if (in_decl)
7587 error ("for template declaration %q+D", in_decl);
7589 return error_mark_node;
7592 complain &= ~tf_user;
7594 /* An alias that just changes the name of a template is equivalent to the
7595 other template, so if any of the arguments are pack expansions, strip
7596 the alias to avoid problems with a pack expansion passed to a non-pack
7597 alias template parameter (DR 1430). */
7598 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7599 templ = get_underlying_template (templ);
7601 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7603 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7604 template arguments */
7606 tree parm;
7607 tree arglist2;
7608 tree outer;
7610 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7612 /* Consider an example where a template template parameter declared as
7614 template <class T, class U = std::allocator<T> > class TT
7616 The template parameter level of T and U are one level larger than
7617 of TT. To proper process the default argument of U, say when an
7618 instantiation `TT<int>' is seen, we need to build the full
7619 arguments containing {int} as the innermost level. Outer levels,
7620 available when not appearing as default template argument, can be
7621 obtained from the arguments of the enclosing template.
7623 Suppose that TT is later substituted with std::vector. The above
7624 instantiation is `TT<int, std::allocator<T> >' with TT at
7625 level 1, and T at level 2, while the template arguments at level 1
7626 becomes {std::vector} and the inner level 2 is {int}. */
7628 outer = DECL_CONTEXT (templ);
7629 if (outer)
7630 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7631 else if (current_template_parms)
7632 /* This is an argument of the current template, so we haven't set
7633 DECL_CONTEXT yet. */
7634 outer = current_template_args ();
7636 if (outer)
7637 arglist = add_to_template_args (outer, arglist);
7639 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7640 complain,
7641 /*require_all_args=*/true,
7642 /*use_default_args=*/true);
7643 if (arglist2 == error_mark_node
7644 || (!uses_template_parms (arglist2)
7645 && check_instantiated_args (templ, arglist2, complain)))
7646 return error_mark_node;
7648 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7649 return parm;
7651 else
7653 tree template_type = TREE_TYPE (templ);
7654 tree gen_tmpl;
7655 tree type_decl;
7656 tree found = NULL_TREE;
7657 int arg_depth;
7658 int parm_depth;
7659 int is_dependent_type;
7660 int use_partial_inst_tmpl = false;
7662 if (template_type == error_mark_node)
7663 /* An error occurred while building the template TEMPL, and a
7664 diagnostic has most certainly been emitted for that
7665 already. Let's propagate that error. */
7666 return error_mark_node;
7668 gen_tmpl = most_general_template (templ);
7669 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7670 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7671 arg_depth = TMPL_ARGS_DEPTH (arglist);
7673 if (arg_depth == 1 && parm_depth > 1)
7675 /* We've been given an incomplete set of template arguments.
7676 For example, given:
7678 template <class T> struct S1 {
7679 template <class U> struct S2 {};
7680 template <class U> struct S2<U*> {};
7683 we will be called with an ARGLIST of `U*', but the
7684 TEMPLATE will be `template <class T> template
7685 <class U> struct S1<T>::S2'. We must fill in the missing
7686 arguments. */
7687 arglist
7688 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7689 arglist);
7690 arg_depth = TMPL_ARGS_DEPTH (arglist);
7693 /* Now we should have enough arguments. */
7694 gcc_assert (parm_depth == arg_depth);
7696 /* From here on, we're only interested in the most general
7697 template. */
7699 /* Calculate the BOUND_ARGS. These will be the args that are
7700 actually tsubst'd into the definition to create the
7701 instantiation. */
7702 if (parm_depth > 1)
7704 /* We have multiple levels of arguments to coerce, at once. */
7705 int i;
7706 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7708 tree bound_args = make_tree_vec (parm_depth);
7710 for (i = saved_depth,
7711 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7712 i > 0 && t != NULL_TREE;
7713 --i, t = TREE_CHAIN (t))
7715 tree a;
7716 if (i == saved_depth)
7717 a = coerce_template_parms (TREE_VALUE (t),
7718 arglist, gen_tmpl,
7719 complain,
7720 /*require_all_args=*/true,
7721 /*use_default_args=*/true);
7722 else
7723 /* Outer levels should have already been coerced. */
7724 a = TMPL_ARGS_LEVEL (arglist, i);
7726 /* Don't process further if one of the levels fails. */
7727 if (a == error_mark_node)
7729 /* Restore the ARGLIST to its full size. */
7730 TREE_VEC_LENGTH (arglist) = saved_depth;
7731 return error_mark_node;
7734 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7736 /* We temporarily reduce the length of the ARGLIST so
7737 that coerce_template_parms will see only the arguments
7738 corresponding to the template parameters it is
7739 examining. */
7740 TREE_VEC_LENGTH (arglist)--;
7743 /* Restore the ARGLIST to its full size. */
7744 TREE_VEC_LENGTH (arglist) = saved_depth;
7746 arglist = bound_args;
7748 else
7749 arglist
7750 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7751 INNERMOST_TEMPLATE_ARGS (arglist),
7752 gen_tmpl,
7753 complain,
7754 /*require_all_args=*/true,
7755 /*use_default_args=*/true);
7757 if (arglist == error_mark_node)
7758 /* We were unable to bind the arguments. */
7759 return error_mark_node;
7761 /* In the scope of a template class, explicit references to the
7762 template class refer to the type of the template, not any
7763 instantiation of it. For example, in:
7765 template <class T> class C { void f(C<T>); }
7767 the `C<T>' is just the same as `C'. Outside of the
7768 class, however, such a reference is an instantiation. */
7769 if ((entering_scope
7770 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7771 || currently_open_class (template_type))
7772 /* comp_template_args is expensive, check it last. */
7773 && comp_template_args (TYPE_TI_ARGS (template_type),
7774 arglist))
7775 return template_type;
7777 /* If we already have this specialization, return it. */
7778 elt.tmpl = gen_tmpl;
7779 elt.args = arglist;
7780 hash = spec_hasher::hash (&elt);
7781 entry = type_specializations->find_with_hash (&elt, hash);
7783 if (entry)
7784 return entry->spec;
7786 is_dependent_type = uses_template_parms (arglist);
7788 /* If the deduced arguments are invalid, then the binding
7789 failed. */
7790 if (!is_dependent_type
7791 && check_instantiated_args (gen_tmpl,
7792 INNERMOST_TEMPLATE_ARGS (arglist),
7793 complain))
7794 return error_mark_node;
7796 if (!is_dependent_type
7797 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7798 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7799 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7801 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7802 DECL_NAME (gen_tmpl),
7803 /*tag_scope=*/ts_global);
7804 return found;
7807 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7808 complain, in_decl);
7809 if (context == error_mark_node)
7810 return error_mark_node;
7812 if (!context)
7813 context = global_namespace;
7815 /* Create the type. */
7816 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7818 /* The user referred to a specialization of an alias
7819 template represented by GEN_TMPL.
7821 [temp.alias]/2 says:
7823 When a template-id refers to the specialization of an
7824 alias template, it is equivalent to the associated
7825 type obtained by substitution of its
7826 template-arguments for the template-parameters in the
7827 type-id of the alias template. */
7829 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7830 /* Note that the call above (by indirectly calling
7831 register_specialization in tsubst_decl) registers the
7832 TYPE_DECL representing the specialization of the alias
7833 template. So next time someone substitutes ARGLIST for
7834 the template parms into the alias template (GEN_TMPL),
7835 she'll get that TYPE_DECL back. */
7837 if (t == error_mark_node)
7838 return t;
7840 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7842 if (!is_dependent_type)
7844 set_current_access_from_decl (TYPE_NAME (template_type));
7845 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7846 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7847 arglist, complain, in_decl),
7848 SCOPED_ENUM_P (template_type), NULL);
7850 if (t == error_mark_node)
7851 return t;
7853 else
7855 /* We don't want to call start_enum for this type, since
7856 the values for the enumeration constants may involve
7857 template parameters. And, no one should be interested
7858 in the enumeration constants for such a type. */
7859 t = cxx_make_type (ENUMERAL_TYPE);
7860 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7862 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7863 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7864 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7866 else if (CLASS_TYPE_P (template_type))
7868 t = make_class_type (TREE_CODE (template_type));
7869 CLASSTYPE_DECLARED_CLASS (t)
7870 = CLASSTYPE_DECLARED_CLASS (template_type);
7871 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7872 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7874 /* A local class. Make sure the decl gets registered properly. */
7875 if (context == current_function_decl)
7876 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7878 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7879 /* This instantiation is another name for the primary
7880 template type. Set the TYPE_CANONICAL field
7881 appropriately. */
7882 TYPE_CANONICAL (t) = template_type;
7883 else if (any_template_arguments_need_structural_equality_p (arglist))
7884 /* Some of the template arguments require structural
7885 equality testing, so this template class requires
7886 structural equality testing. */
7887 SET_TYPE_STRUCTURAL_EQUALITY (t);
7889 else
7890 gcc_unreachable ();
7892 /* If we called start_enum or pushtag above, this information
7893 will already be set up. */
7894 if (!TYPE_NAME (t))
7896 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7898 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7899 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7900 DECL_SOURCE_LOCATION (type_decl)
7901 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7903 else
7904 type_decl = TYPE_NAME (t);
7906 if (CLASS_TYPE_P (template_type))
7908 TREE_PRIVATE (type_decl)
7909 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7910 TREE_PROTECTED (type_decl)
7911 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7912 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7914 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7915 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7919 if (OVERLOAD_TYPE_P (t)
7920 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7922 if (tree attributes
7923 = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (template_type)))
7925 if (!TREE_CHAIN (attributes))
7926 TYPE_ATTRIBUTES (t) = attributes;
7927 else
7928 TYPE_ATTRIBUTES (t)
7929 = build_tree_list (TREE_PURPOSE (attributes),
7930 TREE_VALUE (attributes));
7934 /* Let's consider the explicit specialization of a member
7935 of a class template specialization that is implicitly instantiated,
7936 e.g.:
7937 template<class T>
7938 struct S
7940 template<class U> struct M {}; //#0
7943 template<>
7944 template<>
7945 struct S<int>::M<char> //#1
7947 int i;
7949 [temp.expl.spec]/4 says this is valid.
7951 In this case, when we write:
7952 S<int>::M<char> m;
7954 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7955 the one of #0.
7957 When we encounter #1, we want to store the partial instantiation
7958 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7960 For all cases other than this "explicit specialization of member of a
7961 class template", we just want to store the most general template into
7962 the CLASSTYPE_TI_TEMPLATE of M.
7964 This case of "explicit specialization of member of a class template"
7965 only happens when:
7966 1/ the enclosing class is an instantiation of, and therefore not
7967 the same as, the context of the most general template, and
7968 2/ we aren't looking at the partial instantiation itself, i.e.
7969 the innermost arguments are not the same as the innermost parms of
7970 the most general template.
7972 So it's only when 1/ and 2/ happens that we want to use the partial
7973 instantiation of the member template in lieu of its most general
7974 template. */
7976 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7977 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7978 /* the enclosing class must be an instantiation... */
7979 && CLASS_TYPE_P (context)
7980 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7982 tree partial_inst_args;
7983 TREE_VEC_LENGTH (arglist)--;
7984 ++processing_template_decl;
7985 partial_inst_args =
7986 tsubst (INNERMOST_TEMPLATE_ARGS
7987 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7988 arglist, complain, NULL_TREE);
7989 --processing_template_decl;
7990 TREE_VEC_LENGTH (arglist)++;
7991 use_partial_inst_tmpl =
7992 /*...and we must not be looking at the partial instantiation
7993 itself. */
7994 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7995 partial_inst_args);
7998 if (!use_partial_inst_tmpl)
7999 /* This case is easy; there are no member templates involved. */
8000 found = gen_tmpl;
8001 else
8003 /* This is a full instantiation of a member template. Find
8004 the partial instantiation of which this is an instance. */
8006 /* Temporarily reduce by one the number of levels in the ARGLIST
8007 so as to avoid comparing the last set of arguments. */
8008 TREE_VEC_LENGTH (arglist)--;
8009 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8010 TREE_VEC_LENGTH (arglist)++;
8011 /* FOUND is either a proper class type, or an alias
8012 template specialization. In the later case, it's a
8013 TYPE_DECL, resulting from the substituting of arguments
8014 for parameters in the TYPE_DECL of the alias template
8015 done earlier. So be careful while getting the template
8016 of FOUND. */
8017 found = TREE_CODE (found) == TYPE_DECL
8018 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8019 : CLASSTYPE_TI_TEMPLATE (found);
8022 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8024 elt.spec = t;
8025 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8026 entry = ggc_alloc<spec_entry> ();
8027 *entry = elt;
8028 *slot = entry;
8030 /* Note this use of the partial instantiation so we can check it
8031 later in maybe_process_partial_specialization. */
8032 DECL_TEMPLATE_INSTANTIATIONS (found)
8033 = tree_cons (arglist, t,
8034 DECL_TEMPLATE_INSTANTIATIONS (found));
8036 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8037 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8038 /* Now that the type has been registered on the instantiations
8039 list, we set up the enumerators. Because the enumeration
8040 constants may involve the enumeration type itself, we make
8041 sure to register the type first, and then create the
8042 constants. That way, doing tsubst_expr for the enumeration
8043 constants won't result in recursive calls here; we'll find
8044 the instantiation and exit above. */
8045 tsubst_enum (template_type, t, arglist);
8047 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8048 /* If the type makes use of template parameters, the
8049 code that generates debugging information will crash. */
8050 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8052 /* Possibly limit visibility based on template args. */
8053 TREE_PUBLIC (type_decl) = 1;
8054 determine_visibility (type_decl);
8056 inherit_targ_abi_tags (t);
8058 return t;
8062 /* Wrapper for lookup_template_class_1. */
8064 tree
8065 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8066 int entering_scope, tsubst_flags_t complain)
8068 tree ret;
8069 timevar_push (TV_TEMPLATE_INST);
8070 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8071 entering_scope, complain);
8072 timevar_pop (TV_TEMPLATE_INST);
8073 return ret;
8076 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8077 The type of the expression is the unknown_type_node since the
8078 template-id could refer to an explicit or partial specialization. */
8080 tree
8081 lookup_template_variable (tree templ, tree arglist)
8083 tree type = unknown_type_node;
8084 tsubst_flags_t complain = tf_warning_or_error;
8085 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (templ));
8086 arglist = coerce_template_parms (parms, arglist, templ, complain,
8087 /*req_all*/true, /*use_default*/true);
8088 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8092 struct pair_fn_data
8094 tree_fn_t fn;
8095 void *data;
8096 /* True when we should also visit template parameters that occur in
8097 non-deduced contexts. */
8098 bool include_nondeduced_p;
8099 hash_set<tree> *visited;
8102 /* Called from for_each_template_parm via walk_tree. */
8104 static tree
8105 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8107 tree t = *tp;
8108 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8109 tree_fn_t fn = pfd->fn;
8110 void *data = pfd->data;
8112 if (TYPE_P (t)
8113 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8114 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8115 pfd->include_nondeduced_p))
8116 return error_mark_node;
8118 switch (TREE_CODE (t))
8120 case RECORD_TYPE:
8121 if (TYPE_PTRMEMFUNC_P (t))
8122 break;
8123 /* Fall through. */
8125 case UNION_TYPE:
8126 case ENUMERAL_TYPE:
8127 if (!TYPE_TEMPLATE_INFO (t))
8128 *walk_subtrees = 0;
8129 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8130 fn, data, pfd->visited,
8131 pfd->include_nondeduced_p))
8132 return error_mark_node;
8133 break;
8135 case INTEGER_TYPE:
8136 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8137 fn, data, pfd->visited,
8138 pfd->include_nondeduced_p)
8139 || for_each_template_parm (TYPE_MAX_VALUE (t),
8140 fn, data, pfd->visited,
8141 pfd->include_nondeduced_p))
8142 return error_mark_node;
8143 break;
8145 case METHOD_TYPE:
8146 /* Since we're not going to walk subtrees, we have to do this
8147 explicitly here. */
8148 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8149 pfd->visited, pfd->include_nondeduced_p))
8150 return error_mark_node;
8151 /* Fall through. */
8153 case FUNCTION_TYPE:
8154 /* Check the return type. */
8155 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8156 pfd->include_nondeduced_p))
8157 return error_mark_node;
8159 /* Check the parameter types. Since default arguments are not
8160 instantiated until they are needed, the TYPE_ARG_TYPES may
8161 contain expressions that involve template parameters. But,
8162 no-one should be looking at them yet. And, once they're
8163 instantiated, they don't contain template parameters, so
8164 there's no point in looking at them then, either. */
8166 tree parm;
8168 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8169 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8170 pfd->visited, pfd->include_nondeduced_p))
8171 return error_mark_node;
8173 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8174 want walk_tree walking into them itself. */
8175 *walk_subtrees = 0;
8177 break;
8179 case TYPEOF_TYPE:
8180 case UNDERLYING_TYPE:
8181 if (pfd->include_nondeduced_p
8182 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
8183 pfd->visited,
8184 pfd->include_nondeduced_p))
8185 return error_mark_node;
8186 break;
8188 case FUNCTION_DECL:
8189 case VAR_DECL:
8190 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8191 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8192 pfd->visited, pfd->include_nondeduced_p))
8193 return error_mark_node;
8194 /* Fall through. */
8196 case PARM_DECL:
8197 case CONST_DECL:
8198 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8199 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8200 pfd->visited, pfd->include_nondeduced_p))
8201 return error_mark_node;
8202 if (DECL_CONTEXT (t)
8203 && pfd->include_nondeduced_p
8204 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8205 pfd->visited, pfd->include_nondeduced_p))
8206 return error_mark_node;
8207 break;
8209 case BOUND_TEMPLATE_TEMPLATE_PARM:
8210 /* Record template parameters such as `T' inside `TT<T>'. */
8211 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8212 pfd->include_nondeduced_p))
8213 return error_mark_node;
8214 /* Fall through. */
8216 case TEMPLATE_TEMPLATE_PARM:
8217 case TEMPLATE_TYPE_PARM:
8218 case TEMPLATE_PARM_INDEX:
8219 if (fn && (*fn)(t, data))
8220 return error_mark_node;
8221 else if (!fn)
8222 return error_mark_node;
8223 break;
8225 case TEMPLATE_DECL:
8226 /* A template template parameter is encountered. */
8227 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8228 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8229 pfd->include_nondeduced_p))
8230 return error_mark_node;
8232 /* Already substituted template template parameter */
8233 *walk_subtrees = 0;
8234 break;
8236 case TYPENAME_TYPE:
8237 if (!fn
8238 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8239 data, pfd->visited,
8240 pfd->include_nondeduced_p))
8241 return error_mark_node;
8242 break;
8244 case CONSTRUCTOR:
8245 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8246 && pfd->include_nondeduced_p
8247 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8248 (TREE_TYPE (t)), fn, data,
8249 pfd->visited, pfd->include_nondeduced_p))
8250 return error_mark_node;
8251 break;
8253 case INDIRECT_REF:
8254 case COMPONENT_REF:
8255 /* If there's no type, then this thing must be some expression
8256 involving template parameters. */
8257 if (!fn && !TREE_TYPE (t))
8258 return error_mark_node;
8259 break;
8261 case MODOP_EXPR:
8262 case CAST_EXPR:
8263 case IMPLICIT_CONV_EXPR:
8264 case REINTERPRET_CAST_EXPR:
8265 case CONST_CAST_EXPR:
8266 case STATIC_CAST_EXPR:
8267 case DYNAMIC_CAST_EXPR:
8268 case ARROW_EXPR:
8269 case DOTSTAR_EXPR:
8270 case TYPEID_EXPR:
8271 case PSEUDO_DTOR_EXPR:
8272 if (!fn)
8273 return error_mark_node;
8274 break;
8276 default:
8277 break;
8280 /* We didn't find any template parameters we liked. */
8281 return NULL_TREE;
8284 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8285 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8286 call FN with the parameter and the DATA.
8287 If FN returns nonzero, the iteration is terminated, and
8288 for_each_template_parm returns 1. Otherwise, the iteration
8289 continues. If FN never returns a nonzero value, the value
8290 returned by for_each_template_parm is 0. If FN is NULL, it is
8291 considered to be the function which always returns 1.
8293 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8294 parameters that occur in non-deduced contexts. When false, only
8295 visits those template parameters that can be deduced. */
8297 static int
8298 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8299 hash_set<tree> *visited,
8300 bool include_nondeduced_p)
8302 struct pair_fn_data pfd;
8303 int result;
8305 /* Set up. */
8306 pfd.fn = fn;
8307 pfd.data = data;
8308 pfd.include_nondeduced_p = include_nondeduced_p;
8310 /* Walk the tree. (Conceptually, we would like to walk without
8311 duplicates, but for_each_template_parm_r recursively calls
8312 for_each_template_parm, so we would need to reorganize a fair
8313 bit to use walk_tree_without_duplicates, so we keep our own
8314 visited list.) */
8315 if (visited)
8316 pfd.visited = visited;
8317 else
8318 pfd.visited = new hash_set<tree>;
8319 result = cp_walk_tree (&t,
8320 for_each_template_parm_r,
8321 &pfd,
8322 pfd.visited) != NULL_TREE;
8324 /* Clean up. */
8325 if (!visited)
8327 delete pfd.visited;
8328 pfd.visited = 0;
8331 return result;
8334 /* Returns true if T depends on any template parameter. */
8337 uses_template_parms (tree t)
8339 if (t == NULL_TREE)
8340 return false;
8342 bool dependent_p;
8343 int saved_processing_template_decl;
8345 saved_processing_template_decl = processing_template_decl;
8346 if (!saved_processing_template_decl)
8347 processing_template_decl = 1;
8348 if (TYPE_P (t))
8349 dependent_p = dependent_type_p (t);
8350 else if (TREE_CODE (t) == TREE_VEC)
8351 dependent_p = any_dependent_template_arguments_p (t);
8352 else if (TREE_CODE (t) == TREE_LIST)
8353 dependent_p = (uses_template_parms (TREE_VALUE (t))
8354 || uses_template_parms (TREE_CHAIN (t)));
8355 else if (TREE_CODE (t) == TYPE_DECL)
8356 dependent_p = dependent_type_p (TREE_TYPE (t));
8357 else if (DECL_P (t)
8358 || EXPR_P (t)
8359 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8360 || TREE_CODE (t) == OVERLOAD
8361 || BASELINK_P (t)
8362 || identifier_p (t)
8363 || TREE_CODE (t) == TRAIT_EXPR
8364 || TREE_CODE (t) == CONSTRUCTOR
8365 || CONSTANT_CLASS_P (t))
8366 dependent_p = (type_dependent_expression_p (t)
8367 || value_dependent_expression_p (t));
8368 else
8370 gcc_assert (t == error_mark_node);
8371 dependent_p = false;
8374 processing_template_decl = saved_processing_template_decl;
8376 return dependent_p;
8379 /* Returns true iff current_function_decl is an incompletely instantiated
8380 template. Useful instead of processing_template_decl because the latter
8381 is set to 0 during instantiate_non_dependent_expr. */
8383 bool
8384 in_template_function (void)
8386 tree fn = current_function_decl;
8387 bool ret;
8388 ++processing_template_decl;
8389 ret = (fn && DECL_LANG_SPECIFIC (fn)
8390 && DECL_TEMPLATE_INFO (fn)
8391 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8392 --processing_template_decl;
8393 return ret;
8396 /* Returns true if T depends on any template parameter with level LEVEL. */
8399 uses_template_parms_level (tree t, int level)
8401 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8402 /*include_nondeduced_p=*/true);
8405 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8406 ill-formed translation unit, i.e. a variable or function that isn't
8407 usable in a constant expression. */
8409 static inline bool
8410 neglectable_inst_p (tree d)
8412 return (DECL_P (d)
8413 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8414 : decl_maybe_constant_var_p (d)));
8417 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8418 neglectable and instantiated from within an erroneous instantiation. */
8420 static bool
8421 limit_bad_template_recursion (tree decl)
8423 struct tinst_level *lev = current_tinst_level;
8424 int errs = errorcount + sorrycount;
8425 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8426 return false;
8428 for (; lev; lev = lev->next)
8429 if (neglectable_inst_p (lev->decl))
8430 break;
8432 return (lev && errs > lev->errors);
8435 static int tinst_depth;
8436 extern int max_tinst_depth;
8437 int depth_reached;
8439 static GTY(()) struct tinst_level *last_error_tinst_level;
8441 /* We're starting to instantiate D; record the template instantiation context
8442 for diagnostics and to restore it later. */
8444 bool
8445 push_tinst_level (tree d)
8447 return push_tinst_level_loc (d, input_location);
8450 /* We're starting to instantiate D; record the template instantiation context
8451 at LOC for diagnostics and to restore it later. */
8453 bool
8454 push_tinst_level_loc (tree d, location_t loc)
8456 struct tinst_level *new_level;
8458 if (tinst_depth >= max_tinst_depth)
8460 fatal_error ("template instantiation depth exceeds maximum of %d"
8461 " (use -ftemplate-depth= to increase the maximum)",
8462 max_tinst_depth);
8463 return false;
8466 /* If the current instantiation caused problems, don't let it instantiate
8467 anything else. Do allow deduction substitution and decls usable in
8468 constant expressions. */
8469 if (limit_bad_template_recursion (d))
8470 return false;
8472 new_level = ggc_alloc<tinst_level> ();
8473 new_level->decl = d;
8474 new_level->locus = loc;
8475 new_level->errors = errorcount+sorrycount;
8476 new_level->in_system_header_p = in_system_header_at (input_location);
8477 new_level->next = current_tinst_level;
8478 current_tinst_level = new_level;
8480 ++tinst_depth;
8481 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8482 depth_reached = tinst_depth;
8484 return true;
8487 /* We're done instantiating this template; return to the instantiation
8488 context. */
8490 void
8491 pop_tinst_level (void)
8493 /* Restore the filename and line number stashed away when we started
8494 this instantiation. */
8495 input_location = current_tinst_level->locus;
8496 current_tinst_level = current_tinst_level->next;
8497 --tinst_depth;
8500 /* We're instantiating a deferred template; restore the template
8501 instantiation context in which the instantiation was requested, which
8502 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8504 static tree
8505 reopen_tinst_level (struct tinst_level *level)
8507 struct tinst_level *t;
8509 tinst_depth = 0;
8510 for (t = level; t; t = t->next)
8511 ++tinst_depth;
8513 current_tinst_level = level;
8514 pop_tinst_level ();
8515 if (current_tinst_level)
8516 current_tinst_level->errors = errorcount+sorrycount;
8517 return level->decl;
8520 /* Returns the TINST_LEVEL which gives the original instantiation
8521 context. */
8523 struct tinst_level *
8524 outermost_tinst_level (void)
8526 struct tinst_level *level = current_tinst_level;
8527 if (level)
8528 while (level->next)
8529 level = level->next;
8530 return level;
8533 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8534 vector of template arguments, as for tsubst.
8536 Returns an appropriate tsubst'd friend declaration. */
8538 static tree
8539 tsubst_friend_function (tree decl, tree args)
8541 tree new_friend;
8543 if (TREE_CODE (decl) == FUNCTION_DECL
8544 && DECL_TEMPLATE_INSTANTIATION (decl)
8545 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8546 /* This was a friend declared with an explicit template
8547 argument list, e.g.:
8549 friend void f<>(T);
8551 to indicate that f was a template instantiation, not a new
8552 function declaration. Now, we have to figure out what
8553 instantiation of what template. */
8555 tree template_id, arglist, fns;
8556 tree new_args;
8557 tree tmpl;
8558 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8560 /* Friend functions are looked up in the containing namespace scope.
8561 We must enter that scope, to avoid finding member functions of the
8562 current class with same name. */
8563 push_nested_namespace (ns);
8564 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8565 tf_warning_or_error, NULL_TREE,
8566 /*integral_constant_expression_p=*/false);
8567 pop_nested_namespace (ns);
8568 arglist = tsubst (DECL_TI_ARGS (decl), args,
8569 tf_warning_or_error, NULL_TREE);
8570 template_id = lookup_template_function (fns, arglist);
8572 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8573 tmpl = determine_specialization (template_id, new_friend,
8574 &new_args,
8575 /*need_member_template=*/0,
8576 TREE_VEC_LENGTH (args),
8577 tsk_none);
8578 return instantiate_template (tmpl, new_args, tf_error);
8581 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8583 /* The NEW_FRIEND will look like an instantiation, to the
8584 compiler, but is not an instantiation from the point of view of
8585 the language. For example, we might have had:
8587 template <class T> struct S {
8588 template <class U> friend void f(T, U);
8591 Then, in S<int>, template <class U> void f(int, U) is not an
8592 instantiation of anything. */
8593 if (new_friend == error_mark_node)
8594 return error_mark_node;
8596 DECL_USE_TEMPLATE (new_friend) = 0;
8597 if (TREE_CODE (decl) == TEMPLATE_DECL)
8599 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8600 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8601 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8604 /* The mangled name for the NEW_FRIEND is incorrect. The function
8605 is not a template instantiation and should not be mangled like
8606 one. Therefore, we forget the mangling here; we'll recompute it
8607 later if we need it. */
8608 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8610 SET_DECL_RTL (new_friend, NULL);
8611 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8614 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8616 tree old_decl;
8617 tree new_friend_template_info;
8618 tree new_friend_result_template_info;
8619 tree ns;
8620 int new_friend_is_defn;
8622 /* We must save some information from NEW_FRIEND before calling
8623 duplicate decls since that function will free NEW_FRIEND if
8624 possible. */
8625 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8626 new_friend_is_defn =
8627 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8628 (template_for_substitution (new_friend)))
8629 != NULL_TREE);
8630 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8632 /* This declaration is a `primary' template. */
8633 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8635 new_friend_result_template_info
8636 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8638 else
8639 new_friend_result_template_info = NULL_TREE;
8641 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8642 if (new_friend_is_defn)
8643 DECL_INITIAL (new_friend) = error_mark_node;
8645 /* Inside pushdecl_namespace_level, we will push into the
8646 current namespace. However, the friend function should go
8647 into the namespace of the template. */
8648 ns = decl_namespace_context (new_friend);
8649 push_nested_namespace (ns);
8650 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8651 pop_nested_namespace (ns);
8653 if (old_decl == error_mark_node)
8654 return error_mark_node;
8656 if (old_decl != new_friend)
8658 /* This new friend declaration matched an existing
8659 declaration. For example, given:
8661 template <class T> void f(T);
8662 template <class U> class C {
8663 template <class T> friend void f(T) {}
8666 the friend declaration actually provides the definition
8667 of `f', once C has been instantiated for some type. So,
8668 old_decl will be the out-of-class template declaration,
8669 while new_friend is the in-class definition.
8671 But, if `f' was called before this point, the
8672 instantiation of `f' will have DECL_TI_ARGS corresponding
8673 to `T' but not to `U', references to which might appear
8674 in the definition of `f'. Previously, the most general
8675 template for an instantiation of `f' was the out-of-class
8676 version; now it is the in-class version. Therefore, we
8677 run through all specialization of `f', adding to their
8678 DECL_TI_ARGS appropriately. In particular, they need a
8679 new set of outer arguments, corresponding to the
8680 arguments for this class instantiation.
8682 The same situation can arise with something like this:
8684 friend void f(int);
8685 template <class T> class C {
8686 friend void f(T) {}
8689 when `C<int>' is instantiated. Now, `f(int)' is defined
8690 in the class. */
8692 if (!new_friend_is_defn)
8693 /* On the other hand, if the in-class declaration does
8694 *not* provide a definition, then we don't want to alter
8695 existing definitions. We can just leave everything
8696 alone. */
8698 else
8700 tree new_template = TI_TEMPLATE (new_friend_template_info);
8701 tree new_args = TI_ARGS (new_friend_template_info);
8703 /* Overwrite whatever template info was there before, if
8704 any, with the new template information pertaining to
8705 the declaration. */
8706 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8708 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8710 /* We should have called reregister_specialization in
8711 duplicate_decls. */
8712 gcc_assert (retrieve_specialization (new_template,
8713 new_args, 0)
8714 == old_decl);
8716 /* Instantiate it if the global has already been used. */
8717 if (DECL_ODR_USED (old_decl))
8718 instantiate_decl (old_decl, /*defer_ok=*/true,
8719 /*expl_inst_class_mem_p=*/false);
8721 else
8723 tree t;
8725 /* Indicate that the old function template is a partial
8726 instantiation. */
8727 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8728 = new_friend_result_template_info;
8730 gcc_assert (new_template
8731 == most_general_template (new_template));
8732 gcc_assert (new_template != old_decl);
8734 /* Reassign any specializations already in the hash table
8735 to the new more general template, and add the
8736 additional template args. */
8737 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8738 t != NULL_TREE;
8739 t = TREE_CHAIN (t))
8741 tree spec = TREE_VALUE (t);
8742 spec_entry elt;
8744 elt.tmpl = old_decl;
8745 elt.args = DECL_TI_ARGS (spec);
8746 elt.spec = NULL_TREE;
8748 decl_specializations->remove_elt (&elt);
8750 DECL_TI_ARGS (spec)
8751 = add_outermost_template_args (new_args,
8752 DECL_TI_ARGS (spec));
8754 register_specialization
8755 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8758 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8762 /* The information from NEW_FRIEND has been merged into OLD_DECL
8763 by duplicate_decls. */
8764 new_friend = old_decl;
8767 else
8769 tree context = DECL_CONTEXT (new_friend);
8770 bool dependent_p;
8772 /* In the code
8773 template <class T> class C {
8774 template <class U> friend void C1<U>::f (); // case 1
8775 friend void C2<T>::f (); // case 2
8777 we only need to make sure CONTEXT is a complete type for
8778 case 2. To distinguish between the two cases, we note that
8779 CONTEXT of case 1 remains dependent type after tsubst while
8780 this isn't true for case 2. */
8781 ++processing_template_decl;
8782 dependent_p = dependent_type_p (context);
8783 --processing_template_decl;
8785 if (!dependent_p
8786 && !complete_type_or_else (context, NULL_TREE))
8787 return error_mark_node;
8789 if (COMPLETE_TYPE_P (context))
8791 tree fn = new_friend;
8792 /* do_friend adds the TEMPLATE_DECL for any member friend
8793 template even if it isn't a member template, i.e.
8794 template <class T> friend A<T>::f();
8795 Look through it in that case. */
8796 if (TREE_CODE (fn) == TEMPLATE_DECL
8797 && !PRIMARY_TEMPLATE_P (fn))
8798 fn = DECL_TEMPLATE_RESULT (fn);
8799 /* Check to see that the declaration is really present, and,
8800 possibly obtain an improved declaration. */
8801 fn = check_classfn (context, fn, NULL_TREE);
8803 if (fn)
8804 new_friend = fn;
8808 return new_friend;
8811 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8812 template arguments, as for tsubst.
8814 Returns an appropriate tsubst'd friend type or error_mark_node on
8815 failure. */
8817 static tree
8818 tsubst_friend_class (tree friend_tmpl, tree args)
8820 tree friend_type;
8821 tree tmpl;
8822 tree context;
8824 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8826 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8827 return TREE_TYPE (t);
8830 context = CP_DECL_CONTEXT (friend_tmpl);
8832 if (context != global_namespace)
8834 if (TREE_CODE (context) == NAMESPACE_DECL)
8835 push_nested_namespace (context);
8836 else
8837 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8840 /* Look for a class template declaration. We look for hidden names
8841 because two friend declarations of the same template are the
8842 same. For example, in:
8844 struct A {
8845 template <typename> friend class F;
8847 template <typename> struct B {
8848 template <typename> friend class F;
8851 both F templates are the same. */
8852 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8853 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8855 /* But, if we don't find one, it might be because we're in a
8856 situation like this:
8858 template <class T>
8859 struct S {
8860 template <class U>
8861 friend struct S;
8864 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8865 for `S<int>', not the TEMPLATE_DECL. */
8866 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8868 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8869 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8872 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8874 /* The friend template has already been declared. Just
8875 check to see that the declarations match, and install any new
8876 default parameters. We must tsubst the default parameters,
8877 of course. We only need the innermost template parameters
8878 because that is all that redeclare_class_template will look
8879 at. */
8880 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8881 > TMPL_ARGS_DEPTH (args))
8883 tree parms;
8884 location_t saved_input_location;
8885 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8886 args, tf_warning_or_error);
8888 saved_input_location = input_location;
8889 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8890 redeclare_class_template (TREE_TYPE (tmpl), parms);
8891 input_location = saved_input_location;
8895 friend_type = TREE_TYPE (tmpl);
8897 else
8899 /* The friend template has not already been declared. In this
8900 case, the instantiation of the template class will cause the
8901 injection of this template into the global scope. */
8902 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8903 if (tmpl == error_mark_node)
8904 return error_mark_node;
8906 /* The new TMPL is not an instantiation of anything, so we
8907 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8908 the new type because that is supposed to be the corresponding
8909 template decl, i.e., TMPL. */
8910 DECL_USE_TEMPLATE (tmpl) = 0;
8911 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8912 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8913 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8914 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8916 /* Inject this template into the global scope. */
8917 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8920 if (context != global_namespace)
8922 if (TREE_CODE (context) == NAMESPACE_DECL)
8923 pop_nested_namespace (context);
8924 else
8925 pop_nested_class ();
8928 return friend_type;
8931 /* Returns zero if TYPE cannot be completed later due to circularity.
8932 Otherwise returns one. */
8934 static int
8935 can_complete_type_without_circularity (tree type)
8937 if (type == NULL_TREE || type == error_mark_node)
8938 return 0;
8939 else if (COMPLETE_TYPE_P (type))
8940 return 1;
8941 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8942 return can_complete_type_without_circularity (TREE_TYPE (type));
8943 else if (CLASS_TYPE_P (type)
8944 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8945 return 0;
8946 else
8947 return 1;
8950 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8952 /* Apply any attributes which had to be deferred until instantiation
8953 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8954 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8956 static void
8957 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8958 tree args, tsubst_flags_t complain, tree in_decl)
8960 tree last_dep = NULL_TREE;
8961 tree t;
8962 tree *p;
8964 for (t = attributes; t; t = TREE_CHAIN (t))
8965 if (ATTR_IS_DEPENDENT (t))
8967 last_dep = t;
8968 attributes = copy_list (attributes);
8969 break;
8972 if (DECL_P (*decl_p))
8974 if (TREE_TYPE (*decl_p) == error_mark_node)
8975 return;
8976 p = &DECL_ATTRIBUTES (*decl_p);
8978 else
8979 p = &TYPE_ATTRIBUTES (*decl_p);
8981 if (last_dep)
8983 tree late_attrs = NULL_TREE;
8984 tree *q = &late_attrs;
8986 for (*p = attributes; *p; )
8988 t = *p;
8989 if (ATTR_IS_DEPENDENT (t))
8991 *p = TREE_CHAIN (t);
8992 TREE_CHAIN (t) = NULL_TREE;
8993 if ((flag_openmp || flag_cilkplus)
8994 && is_attribute_p ("omp declare simd",
8995 get_attribute_name (t))
8996 && TREE_VALUE (t))
8998 tree clauses = TREE_VALUE (TREE_VALUE (t));
8999 clauses = tsubst_omp_clauses (clauses, true, args,
9000 complain, in_decl);
9001 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9002 clauses = finish_omp_clauses (clauses);
9003 tree parms = DECL_ARGUMENTS (*decl_p);
9004 clauses
9005 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9006 if (clauses)
9007 TREE_VALUE (TREE_VALUE (t)) = clauses;
9008 else
9009 TREE_VALUE (t) = NULL_TREE;
9011 /* If the first attribute argument is an identifier, don't
9012 pass it through tsubst. Attributes like mode, format,
9013 cleanup and several target specific attributes expect it
9014 unmodified. */
9015 else if (attribute_takes_identifier_p (get_attribute_name (t))
9016 && TREE_VALUE (t))
9018 tree chain
9019 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9020 in_decl,
9021 /*integral_constant_expression_p=*/false);
9022 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9023 TREE_VALUE (t)
9024 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9025 chain);
9027 else
9028 TREE_VALUE (t)
9029 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9030 /*integral_constant_expression_p=*/false);
9031 *q = t;
9032 q = &TREE_CHAIN (t);
9034 else
9035 p = &TREE_CHAIN (t);
9038 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9042 /* Perform (or defer) access check for typedefs that were referenced
9043 from within the template TMPL code.
9044 This is a subroutine of instantiate_decl and instantiate_class_template.
9045 TMPL is the template to consider and TARGS is the list of arguments of
9046 that template. */
9048 static void
9049 perform_typedefs_access_check (tree tmpl, tree targs)
9051 location_t saved_location;
9052 unsigned i;
9053 qualified_typedef_usage_t *iter;
9055 if (!tmpl
9056 || (!CLASS_TYPE_P (tmpl)
9057 && TREE_CODE (tmpl) != FUNCTION_DECL))
9058 return;
9060 saved_location = input_location;
9061 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9063 tree type_decl = iter->typedef_decl;
9064 tree type_scope = iter->context;
9066 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9067 continue;
9069 if (uses_template_parms (type_decl))
9070 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9071 if (uses_template_parms (type_scope))
9072 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9074 /* Make access check error messages point to the location
9075 of the use of the typedef. */
9076 input_location = iter->locus;
9077 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9078 type_decl, type_decl,
9079 tf_warning_or_error);
9081 input_location = saved_location;
9084 static tree
9085 instantiate_class_template_1 (tree type)
9087 tree templ, args, pattern, t, member;
9088 tree typedecl;
9089 tree pbinfo;
9090 tree base_list;
9091 unsigned int saved_maximum_field_alignment;
9092 tree fn_context;
9094 if (type == error_mark_node)
9095 return error_mark_node;
9097 if (COMPLETE_OR_OPEN_TYPE_P (type)
9098 || uses_template_parms (type))
9099 return type;
9101 /* Figure out which template is being instantiated. */
9102 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9103 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9105 /* Determine what specialization of the original template to
9106 instantiate. */
9107 t = most_specialized_partial_spec (type, tf_warning_or_error);
9108 if (t == error_mark_node)
9110 TYPE_BEING_DEFINED (type) = 1;
9111 return error_mark_node;
9113 else if (t)
9115 /* This TYPE is actually an instantiation of a partial
9116 specialization. We replace the innermost set of ARGS with
9117 the arguments appropriate for substitution. For example,
9118 given:
9120 template <class T> struct S {};
9121 template <class T> struct S<T*> {};
9123 and supposing that we are instantiating S<int*>, ARGS will
9124 presently be {int*} -- but we need {int}. */
9125 pattern = TREE_TYPE (t);
9126 args = TREE_PURPOSE (t);
9128 else
9130 pattern = TREE_TYPE (templ);
9131 args = CLASSTYPE_TI_ARGS (type);
9134 /* If the template we're instantiating is incomplete, then clearly
9135 there's nothing we can do. */
9136 if (!COMPLETE_TYPE_P (pattern))
9137 return type;
9139 /* If we've recursively instantiated too many templates, stop. */
9140 if (! push_tinst_level (type))
9141 return type;
9143 /* Now we're really doing the instantiation. Mark the type as in
9144 the process of being defined. */
9145 TYPE_BEING_DEFINED (type) = 1;
9147 /* We may be in the middle of deferred access check. Disable
9148 it now. */
9149 push_deferring_access_checks (dk_no_deferred);
9151 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9152 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9153 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9154 fn_context = error_mark_node;
9155 if (!fn_context)
9156 push_to_top_level ();
9157 /* Use #pragma pack from the template context. */
9158 saved_maximum_field_alignment = maximum_field_alignment;
9159 maximum_field_alignment = TYPE_PRECISION (pattern);
9161 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9163 /* Set the input location to the most specialized template definition.
9164 This is needed if tsubsting causes an error. */
9165 typedecl = TYPE_MAIN_DECL (pattern);
9166 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9167 DECL_SOURCE_LOCATION (typedecl);
9169 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9170 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9171 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9172 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9173 if (ANON_AGGR_TYPE_P (pattern))
9174 SET_ANON_AGGR_TYPE_P (type);
9175 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9177 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9178 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9179 /* Adjust visibility for template arguments. */
9180 determine_visibility (TYPE_MAIN_DECL (type));
9182 if (CLASS_TYPE_P (type))
9183 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9185 pbinfo = TYPE_BINFO (pattern);
9187 /* We should never instantiate a nested class before its enclosing
9188 class; we need to look up the nested class by name before we can
9189 instantiate it, and that lookup should instantiate the enclosing
9190 class. */
9191 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9192 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9194 base_list = NULL_TREE;
9195 if (BINFO_N_BASE_BINFOS (pbinfo))
9197 tree pbase_binfo;
9198 tree pushed_scope;
9199 int i;
9201 /* We must enter the scope containing the type, as that is where
9202 the accessibility of types named in dependent bases are
9203 looked up from. */
9204 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9206 /* Substitute into each of the bases to determine the actual
9207 basetypes. */
9208 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9210 tree base;
9211 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9212 tree expanded_bases = NULL_TREE;
9213 int idx, len = 1;
9215 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9217 expanded_bases =
9218 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9219 args, tf_error, NULL_TREE);
9220 if (expanded_bases == error_mark_node)
9221 continue;
9223 len = TREE_VEC_LENGTH (expanded_bases);
9226 for (idx = 0; idx < len; idx++)
9228 if (expanded_bases)
9229 /* Extract the already-expanded base class. */
9230 base = TREE_VEC_ELT (expanded_bases, idx);
9231 else
9232 /* Substitute to figure out the base class. */
9233 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9234 NULL_TREE);
9236 if (base == error_mark_node)
9237 continue;
9239 base_list = tree_cons (access, base, base_list);
9240 if (BINFO_VIRTUAL_P (pbase_binfo))
9241 TREE_TYPE (base_list) = integer_type_node;
9245 /* The list is now in reverse order; correct that. */
9246 base_list = nreverse (base_list);
9248 if (pushed_scope)
9249 pop_scope (pushed_scope);
9251 /* Now call xref_basetypes to set up all the base-class
9252 information. */
9253 xref_basetypes (type, base_list);
9255 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9256 (int) ATTR_FLAG_TYPE_IN_PLACE,
9257 args, tf_error, NULL_TREE);
9258 fixup_attribute_variants (type);
9260 /* Now that our base classes are set up, enter the scope of the
9261 class, so that name lookups into base classes, etc. will work
9262 correctly. This is precisely analogous to what we do in
9263 begin_class_definition when defining an ordinary non-template
9264 class, except we also need to push the enclosing classes. */
9265 push_nested_class (type);
9267 /* Now members are processed in the order of declaration. */
9268 for (member = CLASSTYPE_DECL_LIST (pattern);
9269 member; member = TREE_CHAIN (member))
9271 tree t = TREE_VALUE (member);
9273 if (TREE_PURPOSE (member))
9275 if (TYPE_P (t))
9277 /* Build new CLASSTYPE_NESTED_UTDS. */
9279 tree newtag;
9280 bool class_template_p;
9282 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9283 && TYPE_LANG_SPECIFIC (t)
9284 && CLASSTYPE_IS_TEMPLATE (t));
9285 /* If the member is a class template, then -- even after
9286 substitution -- there may be dependent types in the
9287 template argument list for the class. We increment
9288 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9289 that function will assume that no types are dependent
9290 when outside of a template. */
9291 if (class_template_p)
9292 ++processing_template_decl;
9293 newtag = tsubst (t, args, tf_error, NULL_TREE);
9294 if (class_template_p)
9295 --processing_template_decl;
9296 if (newtag == error_mark_node)
9297 continue;
9299 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9301 tree name = TYPE_IDENTIFIER (t);
9303 if (class_template_p)
9304 /* Unfortunately, lookup_template_class sets
9305 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9306 instantiation (i.e., for the type of a member
9307 template class nested within a template class.)
9308 This behavior is required for
9309 maybe_process_partial_specialization to work
9310 correctly, but is not accurate in this case;
9311 the TAG is not an instantiation of anything.
9312 (The corresponding TEMPLATE_DECL is an
9313 instantiation, but the TYPE is not.) */
9314 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9316 /* Now, we call pushtag to put this NEWTAG into the scope of
9317 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9318 pushtag calling push_template_decl. We don't have to do
9319 this for enums because it will already have been done in
9320 tsubst_enum. */
9321 if (name)
9322 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9323 pushtag (name, newtag, /*tag_scope=*/ts_current);
9326 else if (DECL_DECLARES_FUNCTION_P (t))
9328 /* Build new TYPE_METHODS. */
9329 tree r;
9331 if (TREE_CODE (t) == TEMPLATE_DECL)
9332 ++processing_template_decl;
9333 r = tsubst (t, args, tf_error, NULL_TREE);
9334 if (TREE_CODE (t) == TEMPLATE_DECL)
9335 --processing_template_decl;
9336 set_current_access_from_decl (r);
9337 finish_member_declaration (r);
9338 /* Instantiate members marked with attribute used. */
9339 if (r != error_mark_node && DECL_PRESERVE_P (r))
9340 mark_used (r);
9341 if (TREE_CODE (r) == FUNCTION_DECL
9342 && DECL_OMP_DECLARE_REDUCTION_P (r))
9343 cp_check_omp_declare_reduction (r);
9345 else if (DECL_CLASS_TEMPLATE_P (t)
9346 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9347 /* A closure type for a lambda in a default argument for a
9348 member template. Ignore it; it will be instantiated with
9349 the default argument. */;
9350 else
9352 /* Build new TYPE_FIELDS. */
9353 if (TREE_CODE (t) == STATIC_ASSERT)
9355 tree condition;
9357 ++c_inhibit_evaluation_warnings;
9358 condition =
9359 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9360 tf_warning_or_error, NULL_TREE,
9361 /*integral_constant_expression_p=*/true);
9362 --c_inhibit_evaluation_warnings;
9364 finish_static_assert (condition,
9365 STATIC_ASSERT_MESSAGE (t),
9366 STATIC_ASSERT_SOURCE_LOCATION (t),
9367 /*member_p=*/true);
9369 else if (TREE_CODE (t) != CONST_DECL)
9371 tree r;
9372 tree vec = NULL_TREE;
9373 int len = 1;
9375 /* The file and line for this declaration, to
9376 assist in error message reporting. Since we
9377 called push_tinst_level above, we don't need to
9378 restore these. */
9379 input_location = DECL_SOURCE_LOCATION (t);
9381 if (TREE_CODE (t) == TEMPLATE_DECL)
9382 ++processing_template_decl;
9383 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9384 if (TREE_CODE (t) == TEMPLATE_DECL)
9385 --processing_template_decl;
9387 if (TREE_CODE (r) == TREE_VEC)
9389 /* A capture pack became multiple fields. */
9390 vec = r;
9391 len = TREE_VEC_LENGTH (vec);
9394 for (int i = 0; i < len; ++i)
9396 if (vec)
9397 r = TREE_VEC_ELT (vec, i);
9398 if (VAR_P (r))
9400 /* In [temp.inst]:
9402 [t]he initialization (and any associated
9403 side-effects) of a static data member does
9404 not occur unless the static data member is
9405 itself used in a way that requires the
9406 definition of the static data member to
9407 exist.
9409 Therefore, we do not substitute into the
9410 initialized for the static data member here. */
9411 finish_static_data_member_decl
9413 /*init=*/NULL_TREE,
9414 /*init_const_expr_p=*/false,
9415 /*asmspec_tree=*/NULL_TREE,
9416 /*flags=*/0);
9417 /* Instantiate members marked with attribute used. */
9418 if (r != error_mark_node && DECL_PRESERVE_P (r))
9419 mark_used (r);
9421 else if (TREE_CODE (r) == FIELD_DECL)
9423 /* Determine whether R has a valid type and can be
9424 completed later. If R is invalid, then its type
9425 is replaced by error_mark_node. */
9426 tree rtype = TREE_TYPE (r);
9427 if (can_complete_type_without_circularity (rtype))
9428 complete_type (rtype);
9430 if (!COMPLETE_TYPE_P (rtype))
9432 cxx_incomplete_type_error (r, rtype);
9433 TREE_TYPE (r) = error_mark_node;
9437 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9438 such a thing will already have been added to the field
9439 list by tsubst_enum in finish_member_declaration in the
9440 CLASSTYPE_NESTED_UTDS case above. */
9441 if (!(TREE_CODE (r) == TYPE_DECL
9442 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9443 && DECL_ARTIFICIAL (r)))
9445 set_current_access_from_decl (r);
9446 finish_member_declaration (r);
9452 else
9454 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9455 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9457 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9459 tree friend_type = t;
9460 bool adjust_processing_template_decl = false;
9462 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9464 /* template <class T> friend class C; */
9465 friend_type = tsubst_friend_class (friend_type, args);
9466 adjust_processing_template_decl = true;
9468 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9470 /* template <class T> friend class C::D; */
9471 friend_type = tsubst (friend_type, args,
9472 tf_warning_or_error, NULL_TREE);
9473 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9474 friend_type = TREE_TYPE (friend_type);
9475 adjust_processing_template_decl = true;
9477 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9478 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9480 /* This could be either
9482 friend class T::C;
9484 when dependent_type_p is false or
9486 template <class U> friend class T::C;
9488 otherwise. */
9489 friend_type = tsubst (friend_type, args,
9490 tf_warning_or_error, NULL_TREE);
9491 /* Bump processing_template_decl for correct
9492 dependent_type_p calculation. */
9493 ++processing_template_decl;
9494 if (dependent_type_p (friend_type))
9495 adjust_processing_template_decl = true;
9496 --processing_template_decl;
9498 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9499 && hidden_name_p (TYPE_NAME (friend_type)))
9501 /* friend class C;
9503 where C hasn't been declared yet. Let's lookup name
9504 from namespace scope directly, bypassing any name that
9505 come from dependent base class. */
9506 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9508 /* The call to xref_tag_from_type does injection for friend
9509 classes. */
9510 push_nested_namespace (ns);
9511 friend_type =
9512 xref_tag_from_type (friend_type, NULL_TREE,
9513 /*tag_scope=*/ts_current);
9514 pop_nested_namespace (ns);
9516 else if (uses_template_parms (friend_type))
9517 /* friend class C<T>; */
9518 friend_type = tsubst (friend_type, args,
9519 tf_warning_or_error, NULL_TREE);
9520 /* Otherwise it's
9522 friend class C;
9524 where C is already declared or
9526 friend class C<int>;
9528 We don't have to do anything in these cases. */
9530 if (adjust_processing_template_decl)
9531 /* Trick make_friend_class into realizing that the friend
9532 we're adding is a template, not an ordinary class. It's
9533 important that we use make_friend_class since it will
9534 perform some error-checking and output cross-reference
9535 information. */
9536 ++processing_template_decl;
9538 if (friend_type != error_mark_node)
9539 make_friend_class (type, friend_type, /*complain=*/false);
9541 if (adjust_processing_template_decl)
9542 --processing_template_decl;
9544 else
9546 /* Build new DECL_FRIENDLIST. */
9547 tree r;
9549 /* The file and line for this declaration, to
9550 assist in error message reporting. Since we
9551 called push_tinst_level above, we don't need to
9552 restore these. */
9553 input_location = DECL_SOURCE_LOCATION (t);
9555 if (TREE_CODE (t) == TEMPLATE_DECL)
9557 ++processing_template_decl;
9558 push_deferring_access_checks (dk_no_check);
9561 r = tsubst_friend_function (t, args);
9562 add_friend (type, r, /*complain=*/false);
9563 if (TREE_CODE (t) == TEMPLATE_DECL)
9565 pop_deferring_access_checks ();
9566 --processing_template_decl;
9572 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9574 tree decl = lambda_function (type);
9575 if (decl)
9577 if (!DECL_TEMPLATE_INFO (decl)
9578 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9579 instantiate_decl (decl, false, false);
9581 /* We need to instantiate the capture list from the template
9582 after we've instantiated the closure members, but before we
9583 consider adding the conversion op. Also keep any captures
9584 that may have been added during instantiation of the op(). */
9585 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9586 tree tmpl_cap
9587 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9588 args, tf_warning_or_error, NULL_TREE,
9589 false, false);
9591 LAMBDA_EXPR_CAPTURE_LIST (expr)
9592 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9594 maybe_add_lambda_conv_op (type);
9596 else
9597 gcc_assert (errorcount);
9600 /* Set the file and line number information to whatever is given for
9601 the class itself. This puts error messages involving generated
9602 implicit functions at a predictable point, and the same point
9603 that would be used for non-template classes. */
9604 input_location = DECL_SOURCE_LOCATION (typedecl);
9606 unreverse_member_declarations (type);
9607 finish_struct_1 (type);
9608 TYPE_BEING_DEFINED (type) = 0;
9610 /* We don't instantiate default arguments for member functions. 14.7.1:
9612 The implicit instantiation of a class template specialization causes
9613 the implicit instantiation of the declarations, but not of the
9614 definitions or default arguments, of the class member functions,
9615 member classes, static data members and member templates.... */
9617 /* Some typedefs referenced from within the template code need to be access
9618 checked at template instantiation time, i.e now. These types were
9619 added to the template at parsing time. Let's get those and perform
9620 the access checks then. */
9621 perform_typedefs_access_check (pattern, args);
9622 perform_deferred_access_checks (tf_warning_or_error);
9623 pop_nested_class ();
9624 maximum_field_alignment = saved_maximum_field_alignment;
9625 if (!fn_context)
9626 pop_from_top_level ();
9627 pop_deferring_access_checks ();
9628 pop_tinst_level ();
9630 /* The vtable for a template class can be emitted in any translation
9631 unit in which the class is instantiated. When there is no key
9632 method, however, finish_struct_1 will already have added TYPE to
9633 the keyed_classes list. */
9634 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9635 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9637 return type;
9640 /* Wrapper for instantiate_class_template_1. */
9642 tree
9643 instantiate_class_template (tree type)
9645 tree ret;
9646 timevar_push (TV_TEMPLATE_INST);
9647 ret = instantiate_class_template_1 (type);
9648 timevar_pop (TV_TEMPLATE_INST);
9649 return ret;
9652 static tree
9653 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9655 tree r;
9657 if (!t)
9658 r = t;
9659 else if (TYPE_P (t))
9660 r = tsubst (t, args, complain, in_decl);
9661 else
9663 if (!(complain & tf_warning))
9664 ++c_inhibit_evaluation_warnings;
9665 r = tsubst_expr (t, args, complain, in_decl,
9666 /*integral_constant_expression_p=*/true);
9667 if (!(complain & tf_warning))
9668 --c_inhibit_evaluation_warnings;
9670 return r;
9673 /* Given a function parameter pack TMPL_PARM and some function parameters
9674 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9675 and set *SPEC_P to point at the next point in the list. */
9677 static tree
9678 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9680 /* Collect all of the extra "packed" parameters into an
9681 argument pack. */
9682 tree parmvec;
9683 tree parmtypevec;
9684 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9685 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9686 tree spec_parm = *spec_p;
9687 int i, len;
9689 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9690 if (tmpl_parm
9691 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9692 break;
9694 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9695 parmvec = make_tree_vec (len);
9696 parmtypevec = make_tree_vec (len);
9697 spec_parm = *spec_p;
9698 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9700 TREE_VEC_ELT (parmvec, i) = spec_parm;
9701 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9704 /* Build the argument packs. */
9705 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9706 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9707 TREE_TYPE (argpack) = argtypepack;
9708 *spec_p = spec_parm;
9710 return argpack;
9713 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9714 NONTYPE_ARGUMENT_PACK. */
9716 static tree
9717 make_fnparm_pack (tree spec_parm)
9719 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9722 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9723 pack expansion. */
9725 static bool
9726 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9728 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9729 if (i >= TREE_VEC_LENGTH (vec))
9730 return false;
9731 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9735 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9737 static tree
9738 make_argument_pack_select (tree arg_pack, unsigned index)
9740 tree aps = make_node (ARGUMENT_PACK_SELECT);
9742 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9743 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9745 return aps;
9748 /* This is a subroutine of tsubst_pack_expansion.
9750 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9751 mechanism to store the (non complete list of) arguments of the
9752 substitution and return a non substituted pack expansion, in order
9753 to wait for when we have enough arguments to really perform the
9754 substitution. */
9756 static bool
9757 use_pack_expansion_extra_args_p (tree parm_packs,
9758 int arg_pack_len,
9759 bool has_empty_arg)
9761 /* If one pack has an expansion and another pack has a normal
9762 argument or if one pack has an empty argument and an another
9763 one hasn't then tsubst_pack_expansion cannot perform the
9764 substitution and need to fall back on the
9765 PACK_EXPANSION_EXTRA mechanism. */
9766 if (parm_packs == NULL_TREE)
9767 return false;
9768 else if (has_empty_arg)
9769 return true;
9771 bool has_expansion_arg = false;
9772 for (int i = 0 ; i < arg_pack_len; ++i)
9774 bool has_non_expansion_arg = false;
9775 for (tree parm_pack = parm_packs;
9776 parm_pack;
9777 parm_pack = TREE_CHAIN (parm_pack))
9779 tree arg = TREE_VALUE (parm_pack);
9781 if (argument_pack_element_is_expansion_p (arg, i))
9782 has_expansion_arg = true;
9783 else
9784 has_non_expansion_arg = true;
9787 if (has_expansion_arg && has_non_expansion_arg)
9788 return true;
9790 return false;
9793 /* [temp.variadic]/6 says that:
9795 The instantiation of a pack expansion [...]
9796 produces a list E1,E2, ..., En, where N is the number of elements
9797 in the pack expansion parameters.
9799 This subroutine of tsubst_pack_expansion produces one of these Ei.
9801 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9802 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9803 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9804 INDEX is the index 'i' of the element Ei to produce. ARGS,
9805 COMPLAIN, and IN_DECL are the same parameters as for the
9806 tsubst_pack_expansion function.
9808 The function returns the resulting Ei upon successful completion,
9809 or error_mark_node.
9811 Note that this function possibly modifies the ARGS parameter, so
9812 it's the responsibility of the caller to restore it. */
9814 static tree
9815 gen_elem_of_pack_expansion_instantiation (tree pattern,
9816 tree parm_packs,
9817 unsigned index,
9818 tree args /* This parm gets
9819 modified. */,
9820 tsubst_flags_t complain,
9821 tree in_decl)
9823 tree t;
9824 bool ith_elem_is_expansion = false;
9826 /* For each parameter pack, change the substitution of the parameter
9827 pack to the ith argument in its argument pack, then expand the
9828 pattern. */
9829 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9831 tree parm = TREE_PURPOSE (pack);
9832 tree arg_pack = TREE_VALUE (pack);
9833 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9835 ith_elem_is_expansion |=
9836 argument_pack_element_is_expansion_p (arg_pack, index);
9838 /* Select the Ith argument from the pack. */
9839 if (TREE_CODE (parm) == PARM_DECL
9840 || TREE_CODE (parm) == FIELD_DECL)
9842 if (index == 0)
9844 aps = make_argument_pack_select (arg_pack, index);
9845 mark_used (parm);
9846 register_local_specialization (aps, parm);
9848 else
9849 aps = retrieve_local_specialization (parm);
9851 else
9853 int idx, level;
9854 template_parm_level_and_index (parm, &level, &idx);
9856 if (index == 0)
9858 aps = make_argument_pack_select (arg_pack, index);
9859 /* Update the corresponding argument. */
9860 TMPL_ARG (args, level, idx) = aps;
9862 else
9863 /* Re-use the ARGUMENT_PACK_SELECT. */
9864 aps = TMPL_ARG (args, level, idx);
9866 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9869 /* Substitute into the PATTERN with the (possibly altered)
9870 arguments. */
9871 if (pattern == in_decl)
9872 /* Expanding a fixed parameter pack from
9873 coerce_template_parameter_pack. */
9874 t = tsubst_decl (pattern, args, complain);
9875 else if (!TYPE_P (pattern))
9876 t = tsubst_expr (pattern, args, complain, in_decl,
9877 /*integral_constant_expression_p=*/false);
9878 else
9879 t = tsubst (pattern, args, complain, in_decl);
9881 /* If the Ith argument pack element is a pack expansion, then
9882 the Ith element resulting from the substituting is going to
9883 be a pack expansion as well. */
9884 if (ith_elem_is_expansion)
9885 t = make_pack_expansion (t);
9887 return t;
9890 /* Substitute ARGS into T, which is an pack expansion
9891 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9892 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9893 (if only a partial substitution could be performed) or
9894 ERROR_MARK_NODE if there was an error. */
9895 tree
9896 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9897 tree in_decl)
9899 tree pattern;
9900 tree pack, packs = NULL_TREE;
9901 bool unsubstituted_packs = false;
9902 int i, len = -1;
9903 tree result;
9904 hash_map<tree, tree> *saved_local_specializations = NULL;
9905 bool need_local_specializations = false;
9906 int levels;
9908 gcc_assert (PACK_EXPANSION_P (t));
9909 pattern = PACK_EXPANSION_PATTERN (t);
9911 /* Add in any args remembered from an earlier partial instantiation. */
9912 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9914 levels = TMPL_ARGS_DEPTH (args);
9916 /* Determine the argument packs that will instantiate the parameter
9917 packs used in the expansion expression. While we're at it,
9918 compute the number of arguments to be expanded and make sure it
9919 is consistent. */
9920 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9921 pack = TREE_CHAIN (pack))
9923 tree parm_pack = TREE_VALUE (pack);
9924 tree arg_pack = NULL_TREE;
9925 tree orig_arg = NULL_TREE;
9926 int level = 0;
9928 if (TREE_CODE (parm_pack) == BASES)
9930 if (BASES_DIRECT (parm_pack))
9931 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9932 args, complain, in_decl, false));
9933 else
9934 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9935 args, complain, in_decl, false));
9937 if (TREE_CODE (parm_pack) == PARM_DECL)
9939 if (PACK_EXPANSION_LOCAL_P (t))
9940 arg_pack = retrieve_local_specialization (parm_pack);
9941 else
9943 /* We can't rely on local_specializations for a parameter
9944 name used later in a function declaration (such as in a
9945 late-specified return type). Even if it exists, it might
9946 have the wrong value for a recursive call. Just make a
9947 dummy decl, since it's only used for its type. */
9948 arg_pack = tsubst_decl (parm_pack, args, complain);
9949 if (arg_pack && DECL_PACK_P (arg_pack))
9950 /* Partial instantiation of the parm_pack, we can't build
9951 up an argument pack yet. */
9952 arg_pack = NULL_TREE;
9953 else
9954 arg_pack = make_fnparm_pack (arg_pack);
9955 need_local_specializations = true;
9958 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9959 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9960 else
9962 int idx;
9963 template_parm_level_and_index (parm_pack, &level, &idx);
9965 if (level <= levels)
9966 arg_pack = TMPL_ARG (args, level, idx);
9969 orig_arg = arg_pack;
9970 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9971 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9973 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9974 /* This can only happen if we forget to expand an argument
9975 pack somewhere else. Just return an error, silently. */
9977 result = make_tree_vec (1);
9978 TREE_VEC_ELT (result, 0) = error_mark_node;
9979 return result;
9982 if (arg_pack)
9984 int my_len =
9985 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9987 /* Don't bother trying to do a partial substitution with
9988 incomplete packs; we'll try again after deduction. */
9989 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9990 return t;
9992 if (len < 0)
9993 len = my_len;
9994 else if (len != my_len)
9996 if (!(complain & tf_error))
9997 /* Fail quietly. */;
9998 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9999 error ("mismatched argument pack lengths while expanding "
10000 "%<%T%>",
10001 pattern);
10002 else
10003 error ("mismatched argument pack lengths while expanding "
10004 "%<%E%>",
10005 pattern);
10006 return error_mark_node;
10009 /* Keep track of the parameter packs and their corresponding
10010 argument packs. */
10011 packs = tree_cons (parm_pack, arg_pack, packs);
10012 TREE_TYPE (packs) = orig_arg;
10014 else
10016 /* We can't substitute for this parameter pack. We use a flag as
10017 well as the missing_level counter because function parameter
10018 packs don't have a level. */
10019 unsubstituted_packs = true;
10023 /* If the expansion is just T..., return the matching argument pack. */
10024 if (!unsubstituted_packs
10025 && TREE_PURPOSE (packs) == pattern)
10027 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10028 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10029 || pack_expansion_args_count (args))
10030 return args;
10031 /* Otherwise use the normal path so we get convert_from_reference. */
10034 /* We cannot expand this expansion expression, because we don't have
10035 all of the argument packs we need. */
10036 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10038 /* We got some full packs, but we can't substitute them in until we
10039 have values for all the packs. So remember these until then. */
10041 t = make_pack_expansion (pattern);
10042 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10043 return t;
10045 else if (unsubstituted_packs)
10047 /* There were no real arguments, we're just replacing a parameter
10048 pack with another version of itself. Substitute into the
10049 pattern and return a PACK_EXPANSION_*. The caller will need to
10050 deal with that. */
10051 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10052 t = tsubst_expr (pattern, args, complain, in_decl,
10053 /*integral_constant_expression_p=*/false);
10054 else
10055 t = tsubst (pattern, args, complain, in_decl);
10056 t = make_pack_expansion (t);
10057 return t;
10060 gcc_assert (len >= 0);
10062 if (need_local_specializations)
10064 /* We're in a late-specified return type, so create our own local
10065 specializations map; the current map is either NULL or (in the
10066 case of recursive unification) might have bindings that we don't
10067 want to use or alter. */
10068 saved_local_specializations = local_specializations;
10069 local_specializations = new hash_map<tree, tree>;
10072 /* For each argument in each argument pack, substitute into the
10073 pattern. */
10074 result = make_tree_vec (len);
10075 for (i = 0; i < len; ++i)
10077 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10079 args, complain,
10080 in_decl);
10081 TREE_VEC_ELT (result, i) = t;
10082 if (t == error_mark_node)
10084 result = error_mark_node;
10085 break;
10089 /* Update ARGS to restore the substitution from parameter packs to
10090 their argument packs. */
10091 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10093 tree parm = TREE_PURPOSE (pack);
10095 if (TREE_CODE (parm) == PARM_DECL
10096 || TREE_CODE (parm) == FIELD_DECL)
10097 register_local_specialization (TREE_TYPE (pack), parm);
10098 else
10100 int idx, level;
10102 if (TREE_VALUE (pack) == NULL_TREE)
10103 continue;
10105 template_parm_level_and_index (parm, &level, &idx);
10107 /* Update the corresponding argument. */
10108 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10109 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10110 TREE_TYPE (pack);
10111 else
10112 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10116 if (need_local_specializations)
10118 delete local_specializations;
10119 local_specializations = saved_local_specializations;
10122 return result;
10125 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10126 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10127 parameter packs; all parms generated from a function parameter pack will
10128 have the same DECL_PARM_INDEX. */
10130 tree
10131 get_pattern_parm (tree parm, tree tmpl)
10133 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10134 tree patparm;
10136 if (DECL_ARTIFICIAL (parm))
10138 for (patparm = DECL_ARGUMENTS (pattern);
10139 patparm; patparm = DECL_CHAIN (patparm))
10140 if (DECL_ARTIFICIAL (patparm)
10141 && DECL_NAME (parm) == DECL_NAME (patparm))
10142 break;
10144 else
10146 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10147 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10148 gcc_assert (DECL_PARM_INDEX (patparm)
10149 == DECL_PARM_INDEX (parm));
10152 return patparm;
10155 /* Substitute ARGS into the vector or list of template arguments T. */
10157 static tree
10158 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10160 tree orig_t = t;
10161 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10162 tree *elts;
10164 if (t == error_mark_node)
10165 return error_mark_node;
10167 len = TREE_VEC_LENGTH (t);
10168 elts = XALLOCAVEC (tree, len);
10170 for (i = 0; i < len; i++)
10172 tree orig_arg = TREE_VEC_ELT (t, i);
10173 tree new_arg;
10175 if (TREE_CODE (orig_arg) == TREE_VEC)
10176 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10177 else if (PACK_EXPANSION_P (orig_arg))
10179 /* Substitute into an expansion expression. */
10180 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10182 if (TREE_CODE (new_arg) == TREE_VEC)
10183 /* Add to the expanded length adjustment the number of
10184 expanded arguments. We subtract one from this
10185 measurement, because the argument pack expression
10186 itself is already counted as 1 in
10187 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10188 the argument pack is empty. */
10189 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10191 else if (ARGUMENT_PACK_P (orig_arg))
10193 /* Substitute into each of the arguments. */
10194 new_arg = TYPE_P (orig_arg)
10195 ? cxx_make_type (TREE_CODE (orig_arg))
10196 : make_node (TREE_CODE (orig_arg));
10198 SET_ARGUMENT_PACK_ARGS (
10199 new_arg,
10200 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10201 args, complain, in_decl));
10203 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10204 new_arg = error_mark_node;
10206 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10207 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10208 complain, in_decl);
10209 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10211 if (TREE_TYPE (new_arg) == error_mark_node)
10212 new_arg = error_mark_node;
10215 else
10216 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10218 if (new_arg == error_mark_node)
10219 return error_mark_node;
10221 elts[i] = new_arg;
10222 if (new_arg != orig_arg)
10223 need_new = 1;
10226 if (!need_new)
10227 return t;
10229 /* Make space for the expanded arguments coming from template
10230 argument packs. */
10231 t = make_tree_vec (len + expanded_len_adjust);
10232 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10233 arguments for a member template.
10234 In that case each TREE_VEC in ORIG_T represents a level of template
10235 arguments, and ORIG_T won't carry any non defaulted argument count.
10236 It will rather be the nested TREE_VECs that will carry one.
10237 In other words, ORIG_T carries a non defaulted argument count only
10238 if it doesn't contain any nested TREE_VEC. */
10239 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10241 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10242 count += expanded_len_adjust;
10243 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10245 for (i = 0, out = 0; i < len; i++)
10247 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10248 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10249 && TREE_CODE (elts[i]) == TREE_VEC)
10251 int idx;
10253 /* Now expand the template argument pack "in place". */
10254 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10255 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10257 else
10259 TREE_VEC_ELT (t, out) = elts[i];
10260 out++;
10264 return t;
10267 /* Return the result of substituting ARGS into the template parameters
10268 given by PARMS. If there are m levels of ARGS and m + n levels of
10269 PARMS, then the result will contain n levels of PARMS. For
10270 example, if PARMS is `template <class T> template <class U>
10271 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10272 result will be `template <int*, double, class V>'. */
10274 static tree
10275 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10277 tree r = NULL_TREE;
10278 tree* new_parms;
10280 /* When substituting into a template, we must set
10281 PROCESSING_TEMPLATE_DECL as the template parameters may be
10282 dependent if they are based on one-another, and the dependency
10283 predicates are short-circuit outside of templates. */
10284 ++processing_template_decl;
10286 for (new_parms = &r;
10287 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10288 new_parms = &(TREE_CHAIN (*new_parms)),
10289 parms = TREE_CHAIN (parms))
10291 tree new_vec =
10292 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10293 int i;
10295 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10297 tree tuple;
10299 if (parms == error_mark_node)
10300 continue;
10302 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10304 if (tuple == error_mark_node)
10305 continue;
10307 TREE_VEC_ELT (new_vec, i) =
10308 tsubst_template_parm (tuple, args, complain);
10311 *new_parms =
10312 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10313 - TMPL_ARGS_DEPTH (args)),
10314 new_vec, NULL_TREE);
10317 --processing_template_decl;
10319 return r;
10322 /* Return the result of substituting ARGS into one template parameter
10323 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10324 parameter and which TREE_PURPOSE is the default argument of the
10325 template parameter. */
10327 static tree
10328 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10330 tree default_value, parm_decl;
10332 if (args == NULL_TREE
10333 || t == NULL_TREE
10334 || t == error_mark_node)
10335 return t;
10337 gcc_assert (TREE_CODE (t) == TREE_LIST);
10339 default_value = TREE_PURPOSE (t);
10340 parm_decl = TREE_VALUE (t);
10342 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10343 if (TREE_CODE (parm_decl) == PARM_DECL
10344 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10345 parm_decl = error_mark_node;
10346 default_value = tsubst_template_arg (default_value, args,
10347 complain, NULL_TREE);
10349 return build_tree_list (default_value, parm_decl);
10352 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10353 type T. If T is not an aggregate or enumeration type, it is
10354 handled as if by tsubst. IN_DECL is as for tsubst. If
10355 ENTERING_SCOPE is nonzero, T is the context for a template which
10356 we are presently tsubst'ing. Return the substituted value. */
10358 static tree
10359 tsubst_aggr_type (tree t,
10360 tree args,
10361 tsubst_flags_t complain,
10362 tree in_decl,
10363 int entering_scope)
10365 if (t == NULL_TREE)
10366 return NULL_TREE;
10368 switch (TREE_CODE (t))
10370 case RECORD_TYPE:
10371 if (TYPE_PTRMEMFUNC_P (t))
10372 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10374 /* Else fall through. */
10375 case ENUMERAL_TYPE:
10376 case UNION_TYPE:
10377 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10379 tree argvec;
10380 tree context;
10381 tree r;
10382 int saved_unevaluated_operand;
10383 int saved_inhibit_evaluation_warnings;
10385 /* In "sizeof(X<I>)" we need to evaluate "I". */
10386 saved_unevaluated_operand = cp_unevaluated_operand;
10387 cp_unevaluated_operand = 0;
10388 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10389 c_inhibit_evaluation_warnings = 0;
10391 /* First, determine the context for the type we are looking
10392 up. */
10393 context = TYPE_CONTEXT (t);
10394 if (context && TYPE_P (context))
10396 context = tsubst_aggr_type (context, args, complain,
10397 in_decl, /*entering_scope=*/1);
10398 /* If context is a nested class inside a class template,
10399 it may still need to be instantiated (c++/33959). */
10400 context = complete_type (context);
10403 /* Then, figure out what arguments are appropriate for the
10404 type we are trying to find. For example, given:
10406 template <class T> struct S;
10407 template <class T, class U> void f(T, U) { S<U> su; }
10409 and supposing that we are instantiating f<int, double>,
10410 then our ARGS will be {int, double}, but, when looking up
10411 S we only want {double}. */
10412 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10413 complain, in_decl);
10414 if (argvec == error_mark_node)
10415 r = error_mark_node;
10416 else
10418 r = lookup_template_class (t, argvec, in_decl, context,
10419 entering_scope, complain);
10420 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10423 cp_unevaluated_operand = saved_unevaluated_operand;
10424 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10426 return r;
10428 else
10429 /* This is not a template type, so there's nothing to do. */
10430 return t;
10432 default:
10433 return tsubst (t, args, complain, in_decl);
10437 /* Substitute into the default argument ARG (a default argument for
10438 FN), which has the indicated TYPE. */
10440 tree
10441 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10443 tree saved_class_ptr = NULL_TREE;
10444 tree saved_class_ref = NULL_TREE;
10445 int errs = errorcount + sorrycount;
10447 /* This can happen in invalid code. */
10448 if (TREE_CODE (arg) == DEFAULT_ARG)
10449 return arg;
10451 /* This default argument came from a template. Instantiate the
10452 default argument here, not in tsubst. In the case of
10453 something like:
10455 template <class T>
10456 struct S {
10457 static T t();
10458 void f(T = t());
10461 we must be careful to do name lookup in the scope of S<T>,
10462 rather than in the current class. */
10463 push_access_scope (fn);
10464 /* The "this" pointer is not valid in a default argument. */
10465 if (cfun)
10467 saved_class_ptr = current_class_ptr;
10468 cp_function_chain->x_current_class_ptr = NULL_TREE;
10469 saved_class_ref = current_class_ref;
10470 cp_function_chain->x_current_class_ref = NULL_TREE;
10473 push_deferring_access_checks(dk_no_deferred);
10474 /* The default argument expression may cause implicitly defined
10475 member functions to be synthesized, which will result in garbage
10476 collection. We must treat this situation as if we were within
10477 the body of function so as to avoid collecting live data on the
10478 stack. */
10479 ++function_depth;
10480 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10481 complain, NULL_TREE,
10482 /*integral_constant_expression_p=*/false);
10483 --function_depth;
10484 pop_deferring_access_checks();
10486 /* Restore the "this" pointer. */
10487 if (cfun)
10489 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10490 cp_function_chain->x_current_class_ref = saved_class_ref;
10493 if (errorcount+sorrycount > errs
10494 && (complain & tf_warning_or_error))
10495 inform (input_location,
10496 " when instantiating default argument for call to %D", fn);
10498 /* Make sure the default argument is reasonable. */
10499 arg = check_default_argument (type, arg, complain);
10501 pop_access_scope (fn);
10503 return arg;
10506 /* Substitute into all the default arguments for FN. */
10508 static void
10509 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10511 tree arg;
10512 tree tmpl_args;
10514 tmpl_args = DECL_TI_ARGS (fn);
10516 /* If this function is not yet instantiated, we certainly don't need
10517 its default arguments. */
10518 if (uses_template_parms (tmpl_args))
10519 return;
10520 /* Don't do this again for clones. */
10521 if (DECL_CLONED_FUNCTION_P (fn))
10522 return;
10524 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10525 arg;
10526 arg = TREE_CHAIN (arg))
10527 if (TREE_PURPOSE (arg))
10528 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10529 TREE_VALUE (arg),
10530 TREE_PURPOSE (arg),
10531 complain);
10534 /* Substitute the ARGS into the T, which is a _DECL. Return the
10535 result of the substitution. Issue error and warning messages under
10536 control of COMPLAIN. */
10538 static tree
10539 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10541 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10542 location_t saved_loc;
10543 tree r = NULL_TREE;
10544 tree in_decl = t;
10545 hashval_t hash = 0;
10547 /* Set the filename and linenumber to improve error-reporting. */
10548 saved_loc = input_location;
10549 input_location = DECL_SOURCE_LOCATION (t);
10551 switch (TREE_CODE (t))
10553 case TEMPLATE_DECL:
10555 /* We can get here when processing a member function template,
10556 member class template, or template template parameter. */
10557 tree decl = DECL_TEMPLATE_RESULT (t);
10558 tree spec;
10559 tree tmpl_args;
10560 tree full_args;
10562 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10564 /* Template template parameter is treated here. */
10565 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10566 if (new_type == error_mark_node)
10567 RETURN (error_mark_node);
10568 /* If we get a real template back, return it. This can happen in
10569 the context of most_specialized_partial_spec. */
10570 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10571 return new_type;
10573 r = copy_decl (t);
10574 DECL_CHAIN (r) = NULL_TREE;
10575 TREE_TYPE (r) = new_type;
10576 DECL_TEMPLATE_RESULT (r)
10577 = build_decl (DECL_SOURCE_LOCATION (decl),
10578 TYPE_DECL, DECL_NAME (decl), new_type);
10579 DECL_TEMPLATE_PARMS (r)
10580 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10581 complain);
10582 TYPE_NAME (new_type) = r;
10583 break;
10586 /* We might already have an instance of this template.
10587 The ARGS are for the surrounding class type, so the
10588 full args contain the tsubst'd args for the context,
10589 plus the innermost args from the template decl. */
10590 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10591 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10592 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10593 /* Because this is a template, the arguments will still be
10594 dependent, even after substitution. If
10595 PROCESSING_TEMPLATE_DECL is not set, the dependency
10596 predicates will short-circuit. */
10597 ++processing_template_decl;
10598 full_args = tsubst_template_args (tmpl_args, args,
10599 complain, in_decl);
10600 --processing_template_decl;
10601 if (full_args == error_mark_node)
10602 RETURN (error_mark_node);
10604 /* If this is a default template template argument,
10605 tsubst might not have changed anything. */
10606 if (full_args == tmpl_args)
10607 RETURN (t);
10609 hash = hash_tmpl_and_args (t, full_args);
10610 spec = retrieve_specialization (t, full_args, hash);
10611 if (spec != NULL_TREE)
10613 r = spec;
10614 break;
10617 /* Make a new template decl. It will be similar to the
10618 original, but will record the current template arguments.
10619 We also create a new function declaration, which is just
10620 like the old one, but points to this new template, rather
10621 than the old one. */
10622 r = copy_decl (t);
10623 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10624 DECL_CHAIN (r) = NULL_TREE;
10626 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10628 if (TREE_CODE (decl) == TYPE_DECL
10629 && !TYPE_DECL_ALIAS_P (decl))
10631 tree new_type;
10632 ++processing_template_decl;
10633 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10634 --processing_template_decl;
10635 if (new_type == error_mark_node)
10636 RETURN (error_mark_node);
10638 TREE_TYPE (r) = new_type;
10639 /* For a partial specialization, we need to keep pointing to
10640 the primary template. */
10641 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10642 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10643 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10644 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10645 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10647 else
10649 tree new_decl;
10650 ++processing_template_decl;
10651 new_decl = tsubst (decl, args, complain, in_decl);
10652 --processing_template_decl;
10653 if (new_decl == error_mark_node)
10654 RETURN (error_mark_node);
10656 DECL_TEMPLATE_RESULT (r) = new_decl;
10657 DECL_TI_TEMPLATE (new_decl) = r;
10658 TREE_TYPE (r) = TREE_TYPE (new_decl);
10659 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10660 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10663 SET_DECL_IMPLICIT_INSTANTIATION (r);
10664 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10665 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10667 /* The template parameters for this new template are all the
10668 template parameters for the old template, except the
10669 outermost level of parameters. */
10670 DECL_TEMPLATE_PARMS (r)
10671 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10672 complain);
10674 if (PRIMARY_TEMPLATE_P (t))
10675 DECL_PRIMARY_TEMPLATE (r) = r;
10677 if (TREE_CODE (decl) != TYPE_DECL && TREE_CODE (decl) != VAR_DECL)
10678 /* Record this non-type partial instantiation. */
10679 register_specialization (r, t,
10680 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10681 false, hash);
10683 break;
10685 case FUNCTION_DECL:
10687 tree ctx;
10688 tree argvec = NULL_TREE;
10689 tree *friends;
10690 tree gen_tmpl;
10691 tree type;
10692 int member;
10693 int args_depth;
10694 int parms_depth;
10696 /* Nobody should be tsubst'ing into non-template functions. */
10697 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10699 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10701 tree spec;
10702 bool dependent_p;
10704 /* If T is not dependent, just return it. We have to
10705 increment PROCESSING_TEMPLATE_DECL because
10706 value_dependent_expression_p assumes that nothing is
10707 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10708 ++processing_template_decl;
10709 dependent_p = value_dependent_expression_p (t);
10710 --processing_template_decl;
10711 if (!dependent_p)
10712 RETURN (t);
10714 /* Calculate the most general template of which R is a
10715 specialization, and the complete set of arguments used to
10716 specialize R. */
10717 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10718 argvec = tsubst_template_args (DECL_TI_ARGS
10719 (DECL_TEMPLATE_RESULT
10720 (DECL_TI_TEMPLATE (t))),
10721 args, complain, in_decl);
10722 if (argvec == error_mark_node)
10723 RETURN (error_mark_node);
10725 /* Check to see if we already have this specialization. */
10726 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10727 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10729 if (spec)
10731 r = spec;
10732 break;
10735 /* We can see more levels of arguments than parameters if
10736 there was a specialization of a member template, like
10737 this:
10739 template <class T> struct S { template <class U> void f(); }
10740 template <> template <class U> void S<int>::f(U);
10742 Here, we'll be substituting into the specialization,
10743 because that's where we can find the code we actually
10744 want to generate, but we'll have enough arguments for
10745 the most general template.
10747 We also deal with the peculiar case:
10749 template <class T> struct S {
10750 template <class U> friend void f();
10752 template <class U> void f() {}
10753 template S<int>;
10754 template void f<double>();
10756 Here, the ARGS for the instantiation of will be {int,
10757 double}. But, we only need as many ARGS as there are
10758 levels of template parameters in CODE_PATTERN. We are
10759 careful not to get fooled into reducing the ARGS in
10760 situations like:
10762 template <class T> struct S { template <class U> void f(U); }
10763 template <class T> template <> void S<T>::f(int) {}
10765 which we can spot because the pattern will be a
10766 specialization in this case. */
10767 args_depth = TMPL_ARGS_DEPTH (args);
10768 parms_depth =
10769 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10770 if (args_depth > parms_depth
10771 && !DECL_TEMPLATE_SPECIALIZATION (t))
10772 args = get_innermost_template_args (args, parms_depth);
10774 else
10776 /* This special case arises when we have something like this:
10778 template <class T> struct S {
10779 friend void f<int>(int, double);
10782 Here, the DECL_TI_TEMPLATE for the friend declaration
10783 will be an IDENTIFIER_NODE. We are being called from
10784 tsubst_friend_function, and we want only to create a
10785 new decl (R) with appropriate types so that we can call
10786 determine_specialization. */
10787 gen_tmpl = NULL_TREE;
10790 if (DECL_CLASS_SCOPE_P (t))
10792 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10793 member = 2;
10794 else
10795 member = 1;
10796 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10797 complain, t, /*entering_scope=*/1);
10799 else
10801 member = 0;
10802 ctx = DECL_CONTEXT (t);
10804 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10805 if (type == error_mark_node)
10806 RETURN (error_mark_node);
10808 /* If we hit excessive deduction depth, the type is bogus even if
10809 it isn't error_mark_node, so don't build a decl. */
10810 if (excessive_deduction_depth)
10811 RETURN (error_mark_node);
10813 /* We do NOT check for matching decls pushed separately at this
10814 point, as they may not represent instantiations of this
10815 template, and in any case are considered separate under the
10816 discrete model. */
10817 r = copy_decl (t);
10818 DECL_USE_TEMPLATE (r) = 0;
10819 TREE_TYPE (r) = type;
10820 /* Clear out the mangled name and RTL for the instantiation. */
10821 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10822 SET_DECL_RTL (r, NULL);
10823 /* Leave DECL_INITIAL set on deleted instantiations. */
10824 if (!DECL_DELETED_FN (r))
10825 DECL_INITIAL (r) = NULL_TREE;
10826 DECL_CONTEXT (r) = ctx;
10828 /* OpenMP UDRs have the only argument a reference to the declared
10829 type. We want to diagnose if the declared type is a reference,
10830 which is invalid, but as references to references are usually
10831 quietly merged, diagnose it here. */
10832 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10834 tree argtype
10835 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10836 argtype = tsubst (argtype, args, complain, in_decl);
10837 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10838 error_at (DECL_SOURCE_LOCATION (t),
10839 "reference type %qT in "
10840 "%<#pragma omp declare reduction%>", argtype);
10841 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10842 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10843 argtype);
10846 if (member && DECL_CONV_FN_P (r))
10847 /* Type-conversion operator. Reconstruct the name, in
10848 case it's the name of one of the template's parameters. */
10849 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10851 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10852 complain, t);
10853 DECL_RESULT (r) = NULL_TREE;
10855 TREE_STATIC (r) = 0;
10856 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10857 DECL_EXTERNAL (r) = 1;
10858 /* If this is an instantiation of a function with internal
10859 linkage, we already know what object file linkage will be
10860 assigned to the instantiation. */
10861 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10862 DECL_DEFER_OUTPUT (r) = 0;
10863 DECL_CHAIN (r) = NULL_TREE;
10864 DECL_PENDING_INLINE_INFO (r) = 0;
10865 DECL_PENDING_INLINE_P (r) = 0;
10866 DECL_SAVED_TREE (r) = NULL_TREE;
10867 DECL_STRUCT_FUNCTION (r) = NULL;
10868 TREE_USED (r) = 0;
10869 /* We'll re-clone as appropriate in instantiate_template. */
10870 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10872 /* If we aren't complaining now, return on error before we register
10873 the specialization so that we'll complain eventually. */
10874 if ((complain & tf_error) == 0
10875 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10876 && !grok_op_properties (r, /*complain=*/false))
10877 RETURN (error_mark_node);
10879 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10880 this in the special friend case mentioned above where
10881 GEN_TMPL is NULL. */
10882 if (gen_tmpl)
10884 DECL_TEMPLATE_INFO (r)
10885 = build_template_info (gen_tmpl, argvec);
10886 SET_DECL_IMPLICIT_INSTANTIATION (r);
10888 tree new_r
10889 = register_specialization (r, gen_tmpl, argvec, false, hash);
10890 if (new_r != r)
10891 /* We instantiated this while substituting into
10892 the type earlier (template/friend54.C). */
10893 RETURN (new_r);
10895 /* We're not supposed to instantiate default arguments
10896 until they are called, for a template. But, for a
10897 declaration like:
10899 template <class T> void f ()
10900 { extern void g(int i = T()); }
10902 we should do the substitution when the template is
10903 instantiated. We handle the member function case in
10904 instantiate_class_template since the default arguments
10905 might refer to other members of the class. */
10906 if (!member
10907 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10908 && !uses_template_parms (argvec))
10909 tsubst_default_arguments (r, complain);
10911 else
10912 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10914 /* Copy the list of befriending classes. */
10915 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10916 *friends;
10917 friends = &TREE_CHAIN (*friends))
10919 *friends = copy_node (*friends);
10920 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10921 args, complain,
10922 in_decl);
10925 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10927 maybe_retrofit_in_chrg (r);
10928 if (DECL_CONSTRUCTOR_P (r))
10929 grok_ctor_properties (ctx, r);
10930 if (DECL_INHERITED_CTOR_BASE (r))
10931 deduce_inheriting_ctor (r);
10932 /* If this is an instantiation of a member template, clone it.
10933 If it isn't, that'll be handled by
10934 clone_constructors_and_destructors. */
10935 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10936 clone_function_decl (r, /*update_method_vec_p=*/0);
10938 else if ((complain & tf_error) != 0
10939 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10940 && !grok_op_properties (r, /*complain=*/true))
10941 RETURN (error_mark_node);
10943 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10944 SET_DECL_FRIEND_CONTEXT (r,
10945 tsubst (DECL_FRIEND_CONTEXT (t),
10946 args, complain, in_decl));
10948 /* Possibly limit visibility based on template args. */
10949 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10950 if (DECL_VISIBILITY_SPECIFIED (t))
10952 DECL_VISIBILITY_SPECIFIED (r) = 0;
10953 DECL_ATTRIBUTES (r)
10954 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10956 determine_visibility (r);
10957 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10958 && !processing_template_decl)
10959 defaulted_late_check (r);
10961 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10962 args, complain, in_decl);
10964 break;
10966 case PARM_DECL:
10968 tree type = NULL_TREE;
10969 int i, len = 1;
10970 tree expanded_types = NULL_TREE;
10971 tree prev_r = NULL_TREE;
10972 tree first_r = NULL_TREE;
10974 if (DECL_PACK_P (t))
10976 /* If there is a local specialization that isn't a
10977 parameter pack, it means that we're doing a "simple"
10978 substitution from inside tsubst_pack_expansion. Just
10979 return the local specialization (which will be a single
10980 parm). */
10981 tree spec = retrieve_local_specialization (t);
10982 if (spec
10983 && TREE_CODE (spec) == PARM_DECL
10984 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10985 RETURN (spec);
10987 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10988 the parameters in this function parameter pack. */
10989 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10990 complain, in_decl);
10991 if (TREE_CODE (expanded_types) == TREE_VEC)
10993 len = TREE_VEC_LENGTH (expanded_types);
10995 /* Zero-length parameter packs are boring. Just substitute
10996 into the chain. */
10997 if (len == 0)
10998 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10999 TREE_CHAIN (t)));
11001 else
11003 /* All we did was update the type. Make a note of that. */
11004 type = expanded_types;
11005 expanded_types = NULL_TREE;
11009 /* Loop through all of the parameters we'll build. When T is
11010 a function parameter pack, LEN is the number of expanded
11011 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11012 r = NULL_TREE;
11013 for (i = 0; i < len; ++i)
11015 prev_r = r;
11016 r = copy_node (t);
11017 if (DECL_TEMPLATE_PARM_P (t))
11018 SET_DECL_TEMPLATE_PARM_P (r);
11020 if (expanded_types)
11021 /* We're on the Ith parameter of the function parameter
11022 pack. */
11024 /* Get the Ith type. */
11025 type = TREE_VEC_ELT (expanded_types, i);
11027 /* Rename the parameter to include the index. */
11028 DECL_NAME (r)
11029 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11031 else if (!type)
11032 /* We're dealing with a normal parameter. */
11033 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11035 type = type_decays_to (type);
11036 TREE_TYPE (r) = type;
11037 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11039 if (DECL_INITIAL (r))
11041 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11042 DECL_INITIAL (r) = TREE_TYPE (r);
11043 else
11044 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11045 complain, in_decl);
11048 DECL_CONTEXT (r) = NULL_TREE;
11050 if (!DECL_TEMPLATE_PARM_P (r))
11051 DECL_ARG_TYPE (r) = type_passed_as (type);
11053 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11054 args, complain, in_decl);
11056 /* Keep track of the first new parameter we
11057 generate. That's what will be returned to the
11058 caller. */
11059 if (!first_r)
11060 first_r = r;
11062 /* Build a proper chain of parameters when substituting
11063 into a function parameter pack. */
11064 if (prev_r)
11065 DECL_CHAIN (prev_r) = r;
11068 /* If cp_unevaluated_operand is set, we're just looking for a
11069 single dummy parameter, so don't keep going. */
11070 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11071 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11072 complain, DECL_CHAIN (t));
11074 /* FIRST_R contains the start of the chain we've built. */
11075 r = first_r;
11077 break;
11079 case FIELD_DECL:
11081 tree type = NULL_TREE;
11082 tree vec = NULL_TREE;
11083 tree expanded_types = NULL_TREE;
11084 int len = 1;
11086 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11088 /* This field is a lambda capture pack. Return a TREE_VEC of
11089 the expanded fields to instantiate_class_template_1 and
11090 store them in the specializations hash table as a
11091 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11092 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11093 complain, in_decl);
11094 if (TREE_CODE (expanded_types) == TREE_VEC)
11096 len = TREE_VEC_LENGTH (expanded_types);
11097 vec = make_tree_vec (len);
11099 else
11101 /* All we did was update the type. Make a note of that. */
11102 type = expanded_types;
11103 expanded_types = NULL_TREE;
11107 for (int i = 0; i < len; ++i)
11109 r = copy_decl (t);
11110 if (expanded_types)
11112 type = TREE_VEC_ELT (expanded_types, i);
11113 DECL_NAME (r)
11114 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11116 else if (!type)
11117 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11119 if (type == error_mark_node)
11120 RETURN (error_mark_node);
11121 TREE_TYPE (r) = type;
11122 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11124 if (DECL_C_BIT_FIELD (r))
11125 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11126 non-bit-fields DECL_INITIAL is a non-static data member
11127 initializer, which gets deferred instantiation. */
11128 DECL_INITIAL (r)
11129 = tsubst_expr (DECL_INITIAL (t), args,
11130 complain, in_decl,
11131 /*integral_constant_expression_p=*/true);
11132 else if (DECL_INITIAL (t))
11134 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11135 NSDMI in perform_member_init. Still set DECL_INITIAL
11136 so that we know there is one. */
11137 DECL_INITIAL (r) = void_node;
11138 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11139 retrofit_lang_decl (r);
11140 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11142 /* We don't have to set DECL_CONTEXT here; it is set by
11143 finish_member_declaration. */
11144 DECL_CHAIN (r) = NULL_TREE;
11146 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11147 args, complain, in_decl);
11149 if (vec)
11150 TREE_VEC_ELT (vec, i) = r;
11153 if (vec)
11155 r = vec;
11156 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11157 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11158 SET_ARGUMENT_PACK_ARGS (pack, vec);
11159 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11160 TREE_TYPE (pack) = tpack;
11161 register_specialization (pack, t, args, false, 0);
11164 break;
11166 case USING_DECL:
11167 /* We reach here only for member using decls. We also need to check
11168 uses_template_parms because DECL_DEPENDENT_P is not set for a
11169 using-declaration that designates a member of the current
11170 instantiation (c++/53549). */
11171 if (DECL_DEPENDENT_P (t)
11172 || uses_template_parms (USING_DECL_SCOPE (t)))
11174 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11175 complain, in_decl);
11176 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11177 r = do_class_using_decl (inst_scope, name);
11178 if (!r)
11179 r = error_mark_node;
11180 else
11182 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11183 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11186 else
11188 r = copy_node (t);
11189 DECL_CHAIN (r) = NULL_TREE;
11191 break;
11193 case TYPE_DECL:
11194 case VAR_DECL:
11196 tree argvec = NULL_TREE;
11197 tree gen_tmpl = NULL_TREE;
11198 tree spec;
11199 tree tmpl = NULL_TREE;
11200 tree ctx;
11201 tree type = NULL_TREE;
11202 bool local_p;
11204 if (TREE_TYPE (t) == error_mark_node)
11205 RETURN (error_mark_node);
11207 if (TREE_CODE (t) == TYPE_DECL
11208 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11210 /* If this is the canonical decl, we don't have to
11211 mess with instantiations, and often we can't (for
11212 typename, template type parms and such). Note that
11213 TYPE_NAME is not correct for the above test if
11214 we've copied the type for a typedef. */
11215 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11216 if (type == error_mark_node)
11217 RETURN (error_mark_node);
11218 r = TYPE_NAME (type);
11219 break;
11222 /* Check to see if we already have the specialization we
11223 need. */
11224 spec = NULL_TREE;
11225 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11227 /* T is a static data member or namespace-scope entity.
11228 We have to substitute into namespace-scope variables
11229 (even though such entities are never templates) because
11230 of cases like:
11232 template <class T> void f() { extern T t; }
11234 where the entity referenced is not known until
11235 instantiation time. */
11236 local_p = false;
11237 ctx = DECL_CONTEXT (t);
11238 if (DECL_CLASS_SCOPE_P (t))
11240 ctx = tsubst_aggr_type (ctx, args,
11241 complain,
11242 in_decl, /*entering_scope=*/1);
11243 /* If CTX is unchanged, then T is in fact the
11244 specialization we want. That situation occurs when
11245 referencing a static data member within in its own
11246 class. We can use pointer equality, rather than
11247 same_type_p, because DECL_CONTEXT is always
11248 canonical... */
11249 if (ctx == DECL_CONTEXT (t)
11250 /* ... unless T is a member template; in which
11251 case our caller can be willing to create a
11252 specialization of that template represented
11253 by T. */
11254 && !(DECL_TI_TEMPLATE (t)
11255 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
11256 spec = t;
11259 if (!spec)
11261 tmpl = DECL_TI_TEMPLATE (t);
11262 gen_tmpl = most_general_template (tmpl);
11263 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11264 if (argvec == error_mark_node)
11265 RETURN (error_mark_node);
11266 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11267 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11270 else
11272 /* A local variable. */
11273 local_p = true;
11274 /* Subsequent calls to pushdecl will fill this in. */
11275 ctx = NULL_TREE;
11276 spec = retrieve_local_specialization (t);
11278 /* If we already have the specialization we need, there is
11279 nothing more to do. */
11280 if (spec)
11282 r = spec;
11283 break;
11286 /* Create a new node for the specialization we need. */
11287 r = copy_decl (t);
11288 if (type == NULL_TREE)
11290 if (is_typedef_decl (t))
11291 type = DECL_ORIGINAL_TYPE (t);
11292 else
11293 type = TREE_TYPE (t);
11294 if (VAR_P (t)
11295 && VAR_HAD_UNKNOWN_BOUND (t)
11296 && type != error_mark_node)
11297 type = strip_array_domain (type);
11298 type = tsubst (type, args, complain, in_decl);
11300 if (VAR_P (r))
11302 /* Even if the original location is out of scope, the
11303 newly substituted one is not. */
11304 DECL_DEAD_FOR_LOCAL (r) = 0;
11305 DECL_INITIALIZED_P (r) = 0;
11306 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11307 if (type == error_mark_node)
11308 RETURN (error_mark_node);
11309 if (TREE_CODE (type) == FUNCTION_TYPE)
11311 /* It may seem that this case cannot occur, since:
11313 typedef void f();
11314 void g() { f x; }
11316 declares a function, not a variable. However:
11318 typedef void f();
11319 template <typename T> void g() { T t; }
11320 template void g<f>();
11322 is an attempt to declare a variable with function
11323 type. */
11324 error ("variable %qD has function type",
11325 /* R is not yet sufficiently initialized, so we
11326 just use its name. */
11327 DECL_NAME (r));
11328 RETURN (error_mark_node);
11330 type = complete_type (type);
11331 /* Wait until cp_finish_decl to set this again, to handle
11332 circular dependency (template/instantiate6.C). */
11333 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11334 type = check_var_type (DECL_NAME (r), type);
11336 if (DECL_HAS_VALUE_EXPR_P (t))
11338 tree ve = DECL_VALUE_EXPR (t);
11339 ve = tsubst_expr (ve, args, complain, in_decl,
11340 /*constant_expression_p=*/false);
11341 if (REFERENCE_REF_P (ve))
11343 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11344 ve = TREE_OPERAND (ve, 0);
11346 SET_DECL_VALUE_EXPR (r, ve);
11348 if (TREE_STATIC (r) || DECL_EXTERNAL (r))
11349 set_decl_tls_model (r, decl_tls_model (t));
11351 else if (DECL_SELF_REFERENCE_P (t))
11352 SET_DECL_SELF_REFERENCE_P (r);
11353 TREE_TYPE (r) = type;
11354 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11355 DECL_CONTEXT (r) = ctx;
11356 /* Clear out the mangled name and RTL for the instantiation. */
11357 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11358 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11359 SET_DECL_RTL (r, NULL);
11360 /* The initializer must not be expanded until it is required;
11361 see [temp.inst]. */
11362 DECL_INITIAL (r) = NULL_TREE;
11363 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11364 SET_DECL_RTL (r, NULL);
11365 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11366 if (VAR_P (r))
11368 /* Possibly limit visibility based on template args. */
11369 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11370 if (DECL_VISIBILITY_SPECIFIED (t))
11372 DECL_VISIBILITY_SPECIFIED (r) = 0;
11373 DECL_ATTRIBUTES (r)
11374 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11376 determine_visibility (r);
11379 if (!local_p)
11381 /* A static data member declaration is always marked
11382 external when it is declared in-class, even if an
11383 initializer is present. We mimic the non-template
11384 processing here. */
11385 DECL_EXTERNAL (r) = 1;
11387 register_specialization (r, gen_tmpl, argvec, false, hash);
11388 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11389 SET_DECL_IMPLICIT_INSTANTIATION (r);
11391 else if (!cp_unevaluated_operand)
11392 register_local_specialization (r, t);
11394 DECL_CHAIN (r) = NULL_TREE;
11396 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11397 /*flags=*/0,
11398 args, complain, in_decl);
11400 /* Preserve a typedef that names a type. */
11401 if (is_typedef_decl (r))
11403 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11404 set_underlying_type (r);
11407 layout_decl (r, 0);
11409 break;
11411 default:
11412 gcc_unreachable ();
11414 #undef RETURN
11416 out:
11417 /* Restore the file and line information. */
11418 input_location = saved_loc;
11420 return r;
11423 /* Substitute into the ARG_TYPES of a function type.
11424 If END is a TREE_CHAIN, leave it and any following types
11425 un-substituted. */
11427 static tree
11428 tsubst_arg_types (tree arg_types,
11429 tree args,
11430 tree end,
11431 tsubst_flags_t complain,
11432 tree in_decl)
11434 tree remaining_arg_types;
11435 tree type = NULL_TREE;
11436 int i = 1;
11437 tree expanded_args = NULL_TREE;
11438 tree default_arg;
11440 if (!arg_types || arg_types == void_list_node || arg_types == end)
11441 return arg_types;
11443 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11444 args, end, complain, in_decl);
11445 if (remaining_arg_types == error_mark_node)
11446 return error_mark_node;
11448 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11450 /* For a pack expansion, perform substitution on the
11451 entire expression. Later on, we'll handle the arguments
11452 one-by-one. */
11453 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11454 args, complain, in_decl);
11456 if (TREE_CODE (expanded_args) == TREE_VEC)
11457 /* So that we'll spin through the parameters, one by one. */
11458 i = TREE_VEC_LENGTH (expanded_args);
11459 else
11461 /* We only partially substituted into the parameter
11462 pack. Our type is TYPE_PACK_EXPANSION. */
11463 type = expanded_args;
11464 expanded_args = NULL_TREE;
11468 while (i > 0) {
11469 --i;
11471 if (expanded_args)
11472 type = TREE_VEC_ELT (expanded_args, i);
11473 else if (!type)
11474 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11476 if (type == error_mark_node)
11477 return error_mark_node;
11478 if (VOID_TYPE_P (type))
11480 if (complain & tf_error)
11482 error ("invalid parameter type %qT", type);
11483 if (in_decl)
11484 error ("in declaration %q+D", in_decl);
11486 return error_mark_node;
11488 /* DR 657. */
11489 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11490 return error_mark_node;
11492 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11493 top-level qualifiers as required. */
11494 type = cv_unqualified (type_decays_to (type));
11496 /* We do not substitute into default arguments here. The standard
11497 mandates that they be instantiated only when needed, which is
11498 done in build_over_call. */
11499 default_arg = TREE_PURPOSE (arg_types);
11501 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11503 /* We've instantiated a template before its default arguments
11504 have been parsed. This can happen for a nested template
11505 class, and is not an error unless we require the default
11506 argument in a call of this function. */
11507 remaining_arg_types =
11508 tree_cons (default_arg, type, remaining_arg_types);
11509 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11511 else
11512 remaining_arg_types =
11513 hash_tree_cons (default_arg, type, remaining_arg_types);
11516 return remaining_arg_types;
11519 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11520 *not* handle the exception-specification for FNTYPE, because the
11521 initial substitution of explicitly provided template parameters
11522 during argument deduction forbids substitution into the
11523 exception-specification:
11525 [temp.deduct]
11527 All references in the function type of the function template to the
11528 corresponding template parameters are replaced by the specified tem-
11529 plate argument values. If a substitution in a template parameter or
11530 in the function type of the function template results in an invalid
11531 type, type deduction fails. [Note: The equivalent substitution in
11532 exception specifications is done only when the function is instanti-
11533 ated, at which point a program is ill-formed if the substitution
11534 results in an invalid type.] */
11536 static tree
11537 tsubst_function_type (tree t,
11538 tree args,
11539 tsubst_flags_t complain,
11540 tree in_decl)
11542 tree return_type;
11543 tree arg_types = NULL_TREE;
11544 tree fntype;
11546 /* The TYPE_CONTEXT is not used for function/method types. */
11547 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11549 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11550 failure. */
11551 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
11553 if (late_return_type_p)
11555 /* Substitute the argument types. */
11556 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11557 complain, in_decl);
11558 if (arg_types == error_mark_node)
11559 return error_mark_node;
11561 tree save_ccp = current_class_ptr;
11562 tree save_ccr = current_class_ref;
11563 tree this_type = (TREE_CODE (t) == METHOD_TYPE
11564 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
11565 bool do_inject = this_type && CLASS_TYPE_P (this_type);
11566 if (do_inject)
11568 /* DR 1207: 'this' is in scope in the trailing return type. */
11569 inject_this_parameter (this_type, cp_type_quals (this_type));
11572 /* Substitute the return type. */
11573 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11575 if (do_inject)
11577 current_class_ptr = save_ccp;
11578 current_class_ref = save_ccr;
11581 else
11582 /* Substitute the return type. */
11583 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11585 if (return_type == error_mark_node)
11586 return error_mark_node;
11587 /* DR 486 clarifies that creation of a function type with an
11588 invalid return type is a deduction failure. */
11589 if (TREE_CODE (return_type) == ARRAY_TYPE
11590 || TREE_CODE (return_type) == FUNCTION_TYPE)
11592 if (complain & tf_error)
11594 if (TREE_CODE (return_type) == ARRAY_TYPE)
11595 error ("function returning an array");
11596 else
11597 error ("function returning a function");
11599 return error_mark_node;
11601 /* And DR 657. */
11602 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11603 return error_mark_node;
11605 if (!late_return_type_p)
11607 /* Substitute the argument types. */
11608 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11609 complain, in_decl);
11610 if (arg_types == error_mark_node)
11611 return error_mark_node;
11614 /* Construct a new type node and return it. */
11615 if (TREE_CODE (t) == FUNCTION_TYPE)
11617 fntype = build_function_type (return_type, arg_types);
11618 fntype = apply_memfn_quals (fntype,
11619 type_memfn_quals (t),
11620 type_memfn_rqual (t));
11622 else
11624 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11625 /* Don't pick up extra function qualifiers from the basetype. */
11626 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11627 if (! MAYBE_CLASS_TYPE_P (r))
11629 /* [temp.deduct]
11631 Type deduction may fail for any of the following
11632 reasons:
11634 -- Attempting to create "pointer to member of T" when T
11635 is not a class type. */
11636 if (complain & tf_error)
11637 error ("creating pointer to member function of non-class type %qT",
11639 return error_mark_node;
11642 fntype = build_method_type_directly (r, return_type,
11643 TREE_CHAIN (arg_types));
11644 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11646 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11648 if (late_return_type_p)
11649 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
11651 return fntype;
11654 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11655 ARGS into that specification, and return the substituted
11656 specification. If there is no specification, return NULL_TREE. */
11658 static tree
11659 tsubst_exception_specification (tree fntype,
11660 tree args,
11661 tsubst_flags_t complain,
11662 tree in_decl,
11663 bool defer_ok)
11665 tree specs;
11666 tree new_specs;
11668 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11669 new_specs = NULL_TREE;
11670 if (specs && TREE_PURPOSE (specs))
11672 /* A noexcept-specifier. */
11673 tree expr = TREE_PURPOSE (specs);
11674 if (TREE_CODE (expr) == INTEGER_CST)
11675 new_specs = expr;
11676 else if (defer_ok)
11678 /* Defer instantiation of noexcept-specifiers to avoid
11679 excessive instantiations (c++/49107). */
11680 new_specs = make_node (DEFERRED_NOEXCEPT);
11681 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11683 /* We already partially instantiated this member template,
11684 so combine the new args with the old. */
11685 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11686 = DEFERRED_NOEXCEPT_PATTERN (expr);
11687 DEFERRED_NOEXCEPT_ARGS (new_specs)
11688 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11690 else
11692 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11693 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11696 else
11697 new_specs = tsubst_copy_and_build
11698 (expr, args, complain, in_decl, /*function_p=*/false,
11699 /*integral_constant_expression_p=*/true);
11700 new_specs = build_noexcept_spec (new_specs, complain);
11702 else if (specs)
11704 if (! TREE_VALUE (specs))
11705 new_specs = specs;
11706 else
11707 while (specs)
11709 tree spec;
11710 int i, len = 1;
11711 tree expanded_specs = NULL_TREE;
11713 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11715 /* Expand the pack expansion type. */
11716 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11717 args, complain,
11718 in_decl);
11720 if (expanded_specs == error_mark_node)
11721 return error_mark_node;
11722 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11723 len = TREE_VEC_LENGTH (expanded_specs);
11724 else
11726 /* We're substituting into a member template, so
11727 we got a TYPE_PACK_EXPANSION back. Add that
11728 expansion and move on. */
11729 gcc_assert (TREE_CODE (expanded_specs)
11730 == TYPE_PACK_EXPANSION);
11731 new_specs = add_exception_specifier (new_specs,
11732 expanded_specs,
11733 complain);
11734 specs = TREE_CHAIN (specs);
11735 continue;
11739 for (i = 0; i < len; ++i)
11741 if (expanded_specs)
11742 spec = TREE_VEC_ELT (expanded_specs, i);
11743 else
11744 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11745 if (spec == error_mark_node)
11746 return spec;
11747 new_specs = add_exception_specifier (new_specs, spec,
11748 complain);
11751 specs = TREE_CHAIN (specs);
11754 return new_specs;
11757 /* Take the tree structure T and replace template parameters used
11758 therein with the argument vector ARGS. IN_DECL is an associated
11759 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11760 Issue error and warning messages under control of COMPLAIN. Note
11761 that we must be relatively non-tolerant of extensions here, in
11762 order to preserve conformance; if we allow substitutions that
11763 should not be allowed, we may allow argument deductions that should
11764 not succeed, and therefore report ambiguous overload situations
11765 where there are none. In theory, we could allow the substitution,
11766 but indicate that it should have failed, and allow our caller to
11767 make sure that the right thing happens, but we don't try to do this
11768 yet.
11770 This function is used for dealing with types, decls and the like;
11771 for expressions, use tsubst_expr or tsubst_copy. */
11773 tree
11774 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11776 enum tree_code code;
11777 tree type, r = NULL_TREE;
11779 if (t == NULL_TREE || t == error_mark_node
11780 || t == integer_type_node
11781 || t == void_type_node
11782 || t == char_type_node
11783 || t == unknown_type_node
11784 || TREE_CODE (t) == NAMESPACE_DECL
11785 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11786 return t;
11788 if (DECL_P (t))
11789 return tsubst_decl (t, args, complain);
11791 if (args == NULL_TREE)
11792 return t;
11794 code = TREE_CODE (t);
11796 if (code == IDENTIFIER_NODE)
11797 type = IDENTIFIER_TYPE_VALUE (t);
11798 else
11799 type = TREE_TYPE (t);
11801 gcc_assert (type != unknown_type_node);
11803 /* Reuse typedefs. We need to do this to handle dependent attributes,
11804 such as attribute aligned. */
11805 if (TYPE_P (t)
11806 && typedef_variant_p (t))
11808 tree decl = TYPE_NAME (t);
11810 if (alias_template_specialization_p (t))
11812 /* DECL represents an alias template and we want to
11813 instantiate it. */
11814 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11815 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11816 r = instantiate_alias_template (tmpl, gen_args, complain);
11818 else if (DECL_CLASS_SCOPE_P (decl)
11819 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11820 && uses_template_parms (DECL_CONTEXT (decl)))
11822 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11823 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11824 r = retrieve_specialization (tmpl, gen_args, 0);
11826 else if (DECL_FUNCTION_SCOPE_P (decl)
11827 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11828 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11829 r = retrieve_local_specialization (decl);
11830 else
11831 /* The typedef is from a non-template context. */
11832 return t;
11834 if (r)
11836 r = TREE_TYPE (r);
11837 r = cp_build_qualified_type_real
11838 (r, cp_type_quals (t) | cp_type_quals (r),
11839 complain | tf_ignore_bad_quals);
11840 return r;
11842 else
11844 /* We don't have an instantiation yet, so drop the typedef. */
11845 int quals = cp_type_quals (t);
11846 t = DECL_ORIGINAL_TYPE (decl);
11847 t = cp_build_qualified_type_real (t, quals,
11848 complain | tf_ignore_bad_quals);
11852 if (type
11853 && code != TYPENAME_TYPE
11854 && code != TEMPLATE_TYPE_PARM
11855 && code != IDENTIFIER_NODE
11856 && code != FUNCTION_TYPE
11857 && code != METHOD_TYPE)
11858 type = tsubst (type, args, complain, in_decl);
11859 if (type == error_mark_node)
11860 return error_mark_node;
11862 switch (code)
11864 case RECORD_TYPE:
11865 case UNION_TYPE:
11866 case ENUMERAL_TYPE:
11867 return tsubst_aggr_type (t, args, complain, in_decl,
11868 /*entering_scope=*/0);
11870 case ERROR_MARK:
11871 case IDENTIFIER_NODE:
11872 case VOID_TYPE:
11873 case REAL_TYPE:
11874 case COMPLEX_TYPE:
11875 case VECTOR_TYPE:
11876 case BOOLEAN_TYPE:
11877 case NULLPTR_TYPE:
11878 case LANG_TYPE:
11879 return t;
11881 case INTEGER_TYPE:
11882 if (t == integer_type_node)
11883 return t;
11885 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11886 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11887 return t;
11890 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11892 max = tsubst_expr (omax, args, complain, in_decl,
11893 /*integral_constant_expression_p=*/false);
11895 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11896 needed. */
11897 if (TREE_CODE (max) == NOP_EXPR
11898 && TREE_SIDE_EFFECTS (omax)
11899 && !TREE_TYPE (max))
11900 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11902 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11903 with TREE_SIDE_EFFECTS that indicates this is not an integral
11904 constant expression. */
11905 if (processing_template_decl
11906 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11908 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11909 TREE_SIDE_EFFECTS (max) = 1;
11912 return compute_array_index_type (NULL_TREE, max, complain);
11915 case TEMPLATE_TYPE_PARM:
11916 case TEMPLATE_TEMPLATE_PARM:
11917 case BOUND_TEMPLATE_TEMPLATE_PARM:
11918 case TEMPLATE_PARM_INDEX:
11920 int idx;
11921 int level;
11922 int levels;
11923 tree arg = NULL_TREE;
11925 r = NULL_TREE;
11927 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11928 template_parm_level_and_index (t, &level, &idx);
11930 levels = TMPL_ARGS_DEPTH (args);
11931 if (level <= levels)
11933 arg = TMPL_ARG (args, level, idx);
11935 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11937 /* See through ARGUMENT_PACK_SELECT arguments. */
11938 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11939 /* If the selected argument is an expansion E, that most
11940 likely means we were called from
11941 gen_elem_of_pack_expansion_instantiation during the
11942 substituting of pack an argument pack (which Ith
11943 element is a pack expansion, where I is
11944 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11945 In this case, the Ith element resulting from this
11946 substituting is going to be a pack expansion, which
11947 pattern is the pattern of E. Let's return the
11948 pattern of E, and
11949 gen_elem_of_pack_expansion_instantiation will
11950 build the resulting pack expansion from it. */
11951 if (PACK_EXPANSION_P (arg))
11953 /* Make sure we aren't throwing away arg info. */
11954 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
11955 arg = PACK_EXPANSION_PATTERN (arg);
11960 if (arg == error_mark_node)
11961 return error_mark_node;
11962 else if (arg != NULL_TREE)
11964 if (ARGUMENT_PACK_P (arg))
11965 /* If ARG is an argument pack, we don't actually want to
11966 perform a substitution here, because substitutions
11967 for argument packs are only done
11968 element-by-element. We can get to this point when
11969 substituting the type of a non-type template
11970 parameter pack, when that type actually contains
11971 template parameter packs from an outer template, e.g.,
11973 template<typename... Types> struct A {
11974 template<Types... Values> struct B { };
11975 }; */
11976 return t;
11978 if (code == TEMPLATE_TYPE_PARM)
11980 int quals;
11981 gcc_assert (TYPE_P (arg));
11983 quals = cp_type_quals (arg) | cp_type_quals (t);
11985 return cp_build_qualified_type_real
11986 (arg, quals, complain | tf_ignore_bad_quals);
11988 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11990 /* We are processing a type constructed from a
11991 template template parameter. */
11992 tree argvec = tsubst (TYPE_TI_ARGS (t),
11993 args, complain, in_decl);
11994 if (argvec == error_mark_node)
11995 return error_mark_node;
11997 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11998 || TREE_CODE (arg) == TEMPLATE_DECL
11999 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12001 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12002 /* Consider this code:
12004 template <template <class> class Template>
12005 struct Internal {
12006 template <class Arg> using Bind = Template<Arg>;
12009 template <template <class> class Template, class Arg>
12010 using Instantiate = Template<Arg>; //#0
12012 template <template <class> class Template,
12013 class Argument>
12014 using Bind =
12015 Instantiate<Internal<Template>::template Bind,
12016 Argument>; //#1
12018 When #1 is parsed, the
12019 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12020 parameter `Template' in #0 matches the
12021 UNBOUND_CLASS_TEMPLATE representing the argument
12022 `Internal<Template>::template Bind'; We then want
12023 to assemble the type `Bind<Argument>' that can't
12024 be fully created right now, because
12025 `Internal<Template>' not being complete, the Bind
12026 template cannot be looked up in that context. So
12027 we need to "store" `Bind<Argument>' for later
12028 when the context of Bind becomes complete. Let's
12029 store that in a TYPENAME_TYPE. */
12030 return make_typename_type (TYPE_CONTEXT (arg),
12031 build_nt (TEMPLATE_ID_EXPR,
12032 TYPE_IDENTIFIER (arg),
12033 argvec),
12034 typename_type,
12035 complain);
12037 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12038 are resolving nested-types in the signature of a
12039 member function templates. Otherwise ARG is a
12040 TEMPLATE_DECL and is the real template to be
12041 instantiated. */
12042 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12043 arg = TYPE_NAME (arg);
12045 r = lookup_template_class (arg,
12046 argvec, in_decl,
12047 DECL_CONTEXT (arg),
12048 /*entering_scope=*/0,
12049 complain);
12050 return cp_build_qualified_type_real
12051 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12053 else
12054 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12055 return convert_from_reference (unshare_expr (arg));
12058 if (level == 1)
12059 /* This can happen during the attempted tsubst'ing in
12060 unify. This means that we don't yet have any information
12061 about the template parameter in question. */
12062 return t;
12064 /* Early in template argument deduction substitution, we don't
12065 want to reduce the level of 'auto', or it will be confused
12066 with a normal template parm in subsequent deduction. */
12067 if (is_auto (t) && (complain & tf_partial))
12068 return t;
12070 /* If we get here, we must have been looking at a parm for a
12071 more deeply nested template. Make a new version of this
12072 template parameter, but with a lower level. */
12073 switch (code)
12075 case TEMPLATE_TYPE_PARM:
12076 case TEMPLATE_TEMPLATE_PARM:
12077 case BOUND_TEMPLATE_TEMPLATE_PARM:
12078 if (cp_type_quals (t))
12080 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12081 r = cp_build_qualified_type_real
12082 (r, cp_type_quals (t),
12083 complain | (code == TEMPLATE_TYPE_PARM
12084 ? tf_ignore_bad_quals : 0));
12086 else
12088 r = copy_type (t);
12089 TEMPLATE_TYPE_PARM_INDEX (r)
12090 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12091 r, levels, args, complain);
12092 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12093 TYPE_MAIN_VARIANT (r) = r;
12094 TYPE_POINTER_TO (r) = NULL_TREE;
12095 TYPE_REFERENCE_TO (r) = NULL_TREE;
12097 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12098 /* We have reduced the level of the template
12099 template parameter, but not the levels of its
12100 template parameters, so canonical_type_parameter
12101 will not be able to find the canonical template
12102 template parameter for this level. Thus, we
12103 require structural equality checking to compare
12104 TEMPLATE_TEMPLATE_PARMs. */
12105 SET_TYPE_STRUCTURAL_EQUALITY (r);
12106 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12107 SET_TYPE_STRUCTURAL_EQUALITY (r);
12108 else
12109 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12111 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12113 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12114 complain, in_decl);
12115 if (argvec == error_mark_node)
12116 return error_mark_node;
12118 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12119 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12122 break;
12124 case TEMPLATE_PARM_INDEX:
12125 r = reduce_template_parm_level (t, type, levels, args, complain);
12126 break;
12128 default:
12129 gcc_unreachable ();
12132 return r;
12135 case TREE_LIST:
12137 tree purpose, value, chain;
12139 if (t == void_list_node)
12140 return t;
12142 purpose = TREE_PURPOSE (t);
12143 if (purpose)
12145 purpose = tsubst (purpose, args, complain, in_decl);
12146 if (purpose == error_mark_node)
12147 return error_mark_node;
12149 value = TREE_VALUE (t);
12150 if (value)
12152 value = tsubst (value, args, complain, in_decl);
12153 if (value == error_mark_node)
12154 return error_mark_node;
12156 chain = TREE_CHAIN (t);
12157 if (chain && chain != void_type_node)
12159 chain = tsubst (chain, args, complain, in_decl);
12160 if (chain == error_mark_node)
12161 return error_mark_node;
12163 if (purpose == TREE_PURPOSE (t)
12164 && value == TREE_VALUE (t)
12165 && chain == TREE_CHAIN (t))
12166 return t;
12167 return hash_tree_cons (purpose, value, chain);
12170 case TREE_BINFO:
12171 /* We should never be tsubsting a binfo. */
12172 gcc_unreachable ();
12174 case TREE_VEC:
12175 /* A vector of template arguments. */
12176 gcc_assert (!type);
12177 return tsubst_template_args (t, args, complain, in_decl);
12179 case POINTER_TYPE:
12180 case REFERENCE_TYPE:
12182 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12183 return t;
12185 /* [temp.deduct]
12187 Type deduction may fail for any of the following
12188 reasons:
12190 -- Attempting to create a pointer to reference type.
12191 -- Attempting to create a reference to a reference type or
12192 a reference to void.
12194 Core issue 106 says that creating a reference to a reference
12195 during instantiation is no longer a cause for failure. We
12196 only enforce this check in strict C++98 mode. */
12197 if ((TREE_CODE (type) == REFERENCE_TYPE
12198 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12199 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12201 static location_t last_loc;
12203 /* We keep track of the last time we issued this error
12204 message to avoid spewing a ton of messages during a
12205 single bad template instantiation. */
12206 if (complain & tf_error
12207 && last_loc != input_location)
12209 if (VOID_TYPE_P (type))
12210 error ("forming reference to void");
12211 else if (code == POINTER_TYPE)
12212 error ("forming pointer to reference type %qT", type);
12213 else
12214 error ("forming reference to reference type %qT", type);
12215 last_loc = input_location;
12218 return error_mark_node;
12220 else if (TREE_CODE (type) == FUNCTION_TYPE
12221 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12222 || type_memfn_rqual (type) != REF_QUAL_NONE))
12224 if (complain & tf_error)
12226 if (code == POINTER_TYPE)
12227 error ("forming pointer to qualified function type %qT",
12228 type);
12229 else
12230 error ("forming reference to qualified function type %qT",
12231 type);
12233 return error_mark_node;
12235 else if (code == POINTER_TYPE)
12237 r = build_pointer_type (type);
12238 if (TREE_CODE (type) == METHOD_TYPE)
12239 r = build_ptrmemfunc_type (r);
12241 else if (TREE_CODE (type) == REFERENCE_TYPE)
12242 /* In C++0x, during template argument substitution, when there is an
12243 attempt to create a reference to a reference type, reference
12244 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12246 "If a template-argument for a template-parameter T names a type
12247 that is a reference to a type A, an attempt to create the type
12248 'lvalue reference to cv T' creates the type 'lvalue reference to
12249 A,' while an attempt to create the type type rvalue reference to
12250 cv T' creates the type T"
12252 r = cp_build_reference_type
12253 (TREE_TYPE (type),
12254 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12255 else
12256 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12257 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12259 if (r != error_mark_node)
12260 /* Will this ever be needed for TYPE_..._TO values? */
12261 layout_type (r);
12263 return r;
12265 case OFFSET_TYPE:
12267 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12268 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12270 /* [temp.deduct]
12272 Type deduction may fail for any of the following
12273 reasons:
12275 -- Attempting to create "pointer to member of T" when T
12276 is not a class type. */
12277 if (complain & tf_error)
12278 error ("creating pointer to member of non-class type %qT", r);
12279 return error_mark_node;
12281 if (TREE_CODE (type) == REFERENCE_TYPE)
12283 if (complain & tf_error)
12284 error ("creating pointer to member reference type %qT", type);
12285 return error_mark_node;
12287 if (VOID_TYPE_P (type))
12289 if (complain & tf_error)
12290 error ("creating pointer to member of type void");
12291 return error_mark_node;
12293 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12294 if (TREE_CODE (type) == FUNCTION_TYPE)
12296 /* The type of the implicit object parameter gets its
12297 cv-qualifiers from the FUNCTION_TYPE. */
12298 tree memptr;
12299 tree method_type
12300 = build_memfn_type (type, r, type_memfn_quals (type),
12301 type_memfn_rqual (type));
12302 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12303 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12304 complain);
12306 else
12307 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12308 cp_type_quals (t),
12309 complain);
12311 case FUNCTION_TYPE:
12312 case METHOD_TYPE:
12314 tree fntype;
12315 tree specs;
12316 fntype = tsubst_function_type (t, args, complain, in_decl);
12317 if (fntype == error_mark_node)
12318 return error_mark_node;
12320 /* Substitute the exception specification. */
12321 specs = tsubst_exception_specification (t, args, complain,
12322 in_decl, /*defer_ok*/true);
12323 if (specs == error_mark_node)
12324 return error_mark_node;
12325 if (specs)
12326 fntype = build_exception_variant (fntype, specs);
12327 return fntype;
12329 case ARRAY_TYPE:
12331 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12332 if (domain == error_mark_node)
12333 return error_mark_node;
12335 /* As an optimization, we avoid regenerating the array type if
12336 it will obviously be the same as T. */
12337 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12338 return t;
12340 /* These checks should match the ones in create_array_type_for_decl.
12342 [temp.deduct]
12344 The deduction may fail for any of the following reasons:
12346 -- Attempting to create an array with an element type that
12347 is void, a function type, or a reference type, or [DR337]
12348 an abstract class type. */
12349 if (VOID_TYPE_P (type)
12350 || TREE_CODE (type) == FUNCTION_TYPE
12351 || (TREE_CODE (type) == ARRAY_TYPE
12352 && TYPE_DOMAIN (type) == NULL_TREE)
12353 || TREE_CODE (type) == REFERENCE_TYPE)
12355 if (complain & tf_error)
12356 error ("creating array of %qT", type);
12357 return error_mark_node;
12360 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12361 return error_mark_node;
12363 r = build_cplus_array_type (type, domain);
12365 if (TYPE_USER_ALIGN (t))
12367 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12368 TYPE_USER_ALIGN (r) = 1;
12371 return r;
12374 case TYPENAME_TYPE:
12376 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12377 in_decl, /*entering_scope=*/1);
12378 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12379 complain, in_decl);
12381 if (ctx == error_mark_node || f == error_mark_node)
12382 return error_mark_node;
12384 if (!MAYBE_CLASS_TYPE_P (ctx))
12386 if (complain & tf_error)
12387 error ("%qT is not a class, struct, or union type", ctx);
12388 return error_mark_node;
12390 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12392 /* Normally, make_typename_type does not require that the CTX
12393 have complete type in order to allow things like:
12395 template <class T> struct S { typename S<T>::X Y; };
12397 But, such constructs have already been resolved by this
12398 point, so here CTX really should have complete type, unless
12399 it's a partial instantiation. */
12400 ctx = complete_type (ctx);
12401 if (!COMPLETE_TYPE_P (ctx))
12403 if (complain & tf_error)
12404 cxx_incomplete_type_error (NULL_TREE, ctx);
12405 return error_mark_node;
12409 f = make_typename_type (ctx, f, typename_type,
12410 complain | tf_keep_type_decl);
12411 if (f == error_mark_node)
12412 return f;
12413 if (TREE_CODE (f) == TYPE_DECL)
12415 complain |= tf_ignore_bad_quals;
12416 f = TREE_TYPE (f);
12419 if (TREE_CODE (f) != TYPENAME_TYPE)
12421 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12423 if (complain & tf_error)
12424 error ("%qT resolves to %qT, which is not an enumeration type",
12425 t, f);
12426 else
12427 return error_mark_node;
12429 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12431 if (complain & tf_error)
12432 error ("%qT resolves to %qT, which is is not a class type",
12433 t, f);
12434 else
12435 return error_mark_node;
12439 return cp_build_qualified_type_real
12440 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12443 case UNBOUND_CLASS_TEMPLATE:
12445 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12446 in_decl, /*entering_scope=*/1);
12447 tree name = TYPE_IDENTIFIER (t);
12448 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12450 if (ctx == error_mark_node || name == error_mark_node)
12451 return error_mark_node;
12453 if (parm_list)
12454 parm_list = tsubst_template_parms (parm_list, args, complain);
12455 return make_unbound_class_template (ctx, name, parm_list, complain);
12458 case TYPEOF_TYPE:
12460 tree type;
12462 ++cp_unevaluated_operand;
12463 ++c_inhibit_evaluation_warnings;
12465 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12466 complain, in_decl,
12467 /*integral_constant_expression_p=*/false);
12469 --cp_unevaluated_operand;
12470 --c_inhibit_evaluation_warnings;
12472 type = finish_typeof (type);
12473 return cp_build_qualified_type_real (type,
12474 cp_type_quals (t)
12475 | cp_type_quals (type),
12476 complain);
12479 case DECLTYPE_TYPE:
12481 tree type;
12483 ++cp_unevaluated_operand;
12484 ++c_inhibit_evaluation_warnings;
12486 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12487 complain|tf_decltype, in_decl,
12488 /*function_p*/false,
12489 /*integral_constant_expression*/false);
12491 --cp_unevaluated_operand;
12492 --c_inhibit_evaluation_warnings;
12494 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12495 type = lambda_capture_field_type (type,
12496 DECLTYPE_FOR_INIT_CAPTURE (t));
12497 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12498 type = lambda_proxy_type (type);
12499 else
12501 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12502 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12503 && EXPR_P (type))
12504 /* In a template ~id could be either a complement expression
12505 or an unqualified-id naming a destructor; if instantiating
12506 it produces an expression, it's not an id-expression or
12507 member access. */
12508 id = false;
12509 type = finish_decltype_type (type, id, complain);
12511 return cp_build_qualified_type_real (type,
12512 cp_type_quals (t)
12513 | cp_type_quals (type),
12514 complain | tf_ignore_bad_quals);
12517 case UNDERLYING_TYPE:
12519 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12520 complain, in_decl);
12521 return finish_underlying_type (type);
12524 case TYPE_ARGUMENT_PACK:
12525 case NONTYPE_ARGUMENT_PACK:
12527 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12528 tree packed_out =
12529 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12530 args,
12531 complain,
12532 in_decl);
12533 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12535 /* For template nontype argument packs, also substitute into
12536 the type. */
12537 if (code == NONTYPE_ARGUMENT_PACK)
12538 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12540 return r;
12542 break;
12544 case VOID_CST:
12545 case INTEGER_CST:
12546 case REAL_CST:
12547 case STRING_CST:
12548 case PLUS_EXPR:
12549 case MINUS_EXPR:
12550 case NEGATE_EXPR:
12551 case NOP_EXPR:
12552 case INDIRECT_REF:
12553 case ADDR_EXPR:
12554 case CALL_EXPR:
12555 case ARRAY_REF:
12556 case SCOPE_REF:
12557 /* We should use one of the expression tsubsts for these codes. */
12558 gcc_unreachable ();
12560 default:
12561 sorry ("use of %qs in template", get_tree_code_name (code));
12562 return error_mark_node;
12566 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12567 type of the expression on the left-hand side of the "." or "->"
12568 operator. */
12570 static tree
12571 tsubst_baselink (tree baselink, tree object_type,
12572 tree args, tsubst_flags_t complain, tree in_decl)
12574 tree name;
12575 tree qualifying_scope;
12576 tree fns;
12577 tree optype;
12578 tree template_args = 0;
12579 bool template_id_p = false;
12580 bool qualified = BASELINK_QUALIFIED_P (baselink);
12582 /* A baselink indicates a function from a base class. Both the
12583 BASELINK_ACCESS_BINFO and the base class referenced may
12584 indicate bases of the template class, rather than the
12585 instantiated class. In addition, lookups that were not
12586 ambiguous before may be ambiguous now. Therefore, we perform
12587 the lookup again. */
12588 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12589 qualifying_scope = tsubst (qualifying_scope, args,
12590 complain, in_decl);
12591 fns = BASELINK_FUNCTIONS (baselink);
12592 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12593 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12595 template_id_p = true;
12596 template_args = TREE_OPERAND (fns, 1);
12597 fns = TREE_OPERAND (fns, 0);
12598 if (template_args)
12599 template_args = tsubst_template_args (template_args, args,
12600 complain, in_decl);
12602 name = DECL_NAME (get_first_fn (fns));
12603 if (IDENTIFIER_TYPENAME_P (name))
12604 name = mangle_conv_op_name_for_type (optype);
12605 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12606 if (!baselink)
12607 return error_mark_node;
12609 /* If lookup found a single function, mark it as used at this
12610 point. (If it lookup found multiple functions the one selected
12611 later by overload resolution will be marked as used at that
12612 point.) */
12613 if (BASELINK_P (baselink))
12614 fns = BASELINK_FUNCTIONS (baselink);
12615 if (!template_id_p && !really_overloaded_fn (fns))
12616 mark_used (OVL_CURRENT (fns));
12618 /* Add back the template arguments, if present. */
12619 if (BASELINK_P (baselink) && template_id_p)
12620 BASELINK_FUNCTIONS (baselink)
12621 = build_nt (TEMPLATE_ID_EXPR,
12622 BASELINK_FUNCTIONS (baselink),
12623 template_args);
12624 /* Update the conversion operator type. */
12625 BASELINK_OPTYPE (baselink) = optype;
12627 if (!object_type)
12628 object_type = current_class_type;
12630 if (qualified)
12631 baselink = adjust_result_of_qualified_name_lookup (baselink,
12632 qualifying_scope,
12633 object_type);
12634 return baselink;
12637 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12638 true if the qualified-id will be a postfix-expression in-and-of
12639 itself; false if more of the postfix-expression follows the
12640 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12641 of "&". */
12643 static tree
12644 tsubst_qualified_id (tree qualified_id, tree args,
12645 tsubst_flags_t complain, tree in_decl,
12646 bool done, bool address_p)
12648 tree expr;
12649 tree scope;
12650 tree name;
12651 bool is_template;
12652 tree template_args;
12653 location_t loc = UNKNOWN_LOCATION;
12655 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12657 /* Figure out what name to look up. */
12658 name = TREE_OPERAND (qualified_id, 1);
12659 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12661 is_template = true;
12662 loc = EXPR_LOCATION (name);
12663 template_args = TREE_OPERAND (name, 1);
12664 if (template_args)
12665 template_args = tsubst_template_args (template_args, args,
12666 complain, in_decl);
12667 name = TREE_OPERAND (name, 0);
12669 else
12671 is_template = false;
12672 template_args = NULL_TREE;
12675 /* Substitute into the qualifying scope. When there are no ARGS, we
12676 are just trying to simplify a non-dependent expression. In that
12677 case the qualifying scope may be dependent, and, in any case,
12678 substituting will not help. */
12679 scope = TREE_OPERAND (qualified_id, 0);
12680 if (args)
12682 scope = tsubst (scope, args, complain, in_decl);
12683 expr = tsubst_copy (name, args, complain, in_decl);
12685 else
12686 expr = name;
12688 if (dependent_scope_p (scope))
12690 if (is_template)
12691 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12692 return build_qualified_name (NULL_TREE, scope, expr,
12693 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12696 if (!BASELINK_P (name) && !DECL_P (expr))
12698 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12700 /* A BIT_NOT_EXPR is used to represent a destructor. */
12701 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12703 error ("qualifying type %qT does not match destructor name ~%qT",
12704 scope, TREE_OPERAND (expr, 0));
12705 expr = error_mark_node;
12707 else
12708 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12709 /*is_type_p=*/0, false);
12711 else
12712 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12713 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12714 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12716 if (complain & tf_error)
12718 error ("dependent-name %qE is parsed as a non-type, but "
12719 "instantiation yields a type", qualified_id);
12720 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12722 return error_mark_node;
12726 if (DECL_P (expr))
12728 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12729 scope);
12730 /* Remember that there was a reference to this entity. */
12731 mark_used (expr);
12734 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12736 if (complain & tf_error)
12737 qualified_name_lookup_error (scope,
12738 TREE_OPERAND (qualified_id, 1),
12739 expr, input_location);
12740 return error_mark_node;
12743 if (is_template)
12744 expr = lookup_template_function (expr, template_args);
12746 if (expr == error_mark_node && complain & tf_error)
12747 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12748 expr, input_location);
12749 else if (TYPE_P (scope))
12751 expr = (adjust_result_of_qualified_name_lookup
12752 (expr, scope, current_nonlambda_class_type ()));
12753 expr = (finish_qualified_id_expr
12754 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12755 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12756 /*template_arg_p=*/false, complain));
12759 /* Expressions do not generally have reference type. */
12760 if (TREE_CODE (expr) != SCOPE_REF
12761 /* However, if we're about to form a pointer-to-member, we just
12762 want the referenced member referenced. */
12763 && TREE_CODE (expr) != OFFSET_REF)
12764 expr = convert_from_reference (expr);
12766 return expr;
12769 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12770 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12771 for tsubst. */
12773 static tree
12774 tsubst_init (tree init, tree decl, tree args,
12775 tsubst_flags_t complain, tree in_decl)
12777 if (!init)
12778 return NULL_TREE;
12780 init = tsubst_expr (init, args, complain, in_decl, false);
12782 if (!init)
12784 /* If we had an initializer but it
12785 instantiated to nothing,
12786 value-initialize the object. This will
12787 only occur when the initializer was a
12788 pack expansion where the parameter packs
12789 used in that expansion were of length
12790 zero. */
12791 init = build_value_init (TREE_TYPE (decl),
12792 complain);
12793 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12794 init = get_target_expr_sfinae (init, complain);
12797 return init;
12800 /* Like tsubst, but deals with expressions. This function just replaces
12801 template parms; to finish processing the resultant expression, use
12802 tsubst_copy_and_build or tsubst_expr. */
12804 static tree
12805 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12807 enum tree_code code;
12808 tree r;
12810 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12811 return t;
12813 code = TREE_CODE (t);
12815 switch (code)
12817 case PARM_DECL:
12818 r = retrieve_local_specialization (t);
12820 if (r == NULL_TREE)
12822 /* We get here for a use of 'this' in an NSDMI. */
12823 if (DECL_NAME (t) == this_identifier
12824 && current_function_decl
12825 && DECL_CONSTRUCTOR_P (current_function_decl))
12826 return current_class_ptr;
12828 /* This can happen for a parameter name used later in a function
12829 declaration (such as in a late-specified return type). Just
12830 make a dummy decl, since it's only used for its type. */
12831 gcc_assert (cp_unevaluated_operand != 0);
12832 r = tsubst_decl (t, args, complain);
12833 /* Give it the template pattern as its context; its true context
12834 hasn't been instantiated yet and this is good enough for
12835 mangling. */
12836 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12839 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12840 r = ARGUMENT_PACK_SELECT_ARG (r);
12841 mark_used (r);
12842 return r;
12844 case CONST_DECL:
12846 tree enum_type;
12847 tree v;
12849 if (DECL_TEMPLATE_PARM_P (t))
12850 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12851 /* There is no need to substitute into namespace-scope
12852 enumerators. */
12853 if (DECL_NAMESPACE_SCOPE_P (t))
12854 return t;
12855 /* If ARGS is NULL, then T is known to be non-dependent. */
12856 if (args == NULL_TREE)
12857 return scalar_constant_value (t);
12859 /* Unfortunately, we cannot just call lookup_name here.
12860 Consider:
12862 template <int I> int f() {
12863 enum E { a = I };
12864 struct S { void g() { E e = a; } };
12867 When we instantiate f<7>::S::g(), say, lookup_name is not
12868 clever enough to find f<7>::a. */
12869 enum_type
12870 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12871 /*entering_scope=*/0);
12873 for (v = TYPE_VALUES (enum_type);
12874 v != NULL_TREE;
12875 v = TREE_CHAIN (v))
12876 if (TREE_PURPOSE (v) == DECL_NAME (t))
12877 return TREE_VALUE (v);
12879 /* We didn't find the name. That should never happen; if
12880 name-lookup found it during preliminary parsing, we
12881 should find it again here during instantiation. */
12882 gcc_unreachable ();
12884 return t;
12886 case FIELD_DECL:
12887 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12889 /* Check for a local specialization set up by
12890 tsubst_pack_expansion. */
12891 if (tree r = retrieve_local_specialization (t))
12893 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12894 r = ARGUMENT_PACK_SELECT_ARG (r);
12895 return r;
12898 /* When retrieving a capture pack from a generic lambda, remove the
12899 lambda call op's own template argument list from ARGS. Only the
12900 template arguments active for the closure type should be used to
12901 retrieve the pack specialization. */
12902 if (LAMBDA_FUNCTION_P (current_function_decl)
12903 && (template_class_depth (DECL_CONTEXT (t))
12904 != TMPL_ARGS_DEPTH (args)))
12905 args = strip_innermost_template_args (args, 1);
12907 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12908 tsubst_decl put in the hash table. */
12909 return retrieve_specialization (t, args, 0);
12912 if (DECL_CONTEXT (t))
12914 tree ctx;
12916 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12917 /*entering_scope=*/1);
12918 if (ctx != DECL_CONTEXT (t))
12920 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12921 if (!r)
12923 if (complain & tf_error)
12924 error ("using invalid field %qD", t);
12925 return error_mark_node;
12927 return r;
12931 return t;
12933 case VAR_DECL:
12934 case FUNCTION_DECL:
12935 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12936 r = tsubst (t, args, complain, in_decl);
12937 else if (local_variable_p (t))
12939 r = retrieve_local_specialization (t);
12940 if (r == NULL_TREE)
12942 /* First try name lookup to find the instantiation. */
12943 r = lookup_name (DECL_NAME (t));
12944 if (r)
12946 /* Make sure that the one we found is the one we want. */
12947 tree ctx = tsubst (DECL_CONTEXT (t), args,
12948 complain, in_decl);
12949 if (ctx != DECL_CONTEXT (r))
12950 r = NULL_TREE;
12953 if (r)
12954 /* OK */;
12955 else
12957 /* This can happen for a variable used in a
12958 late-specified return type of a local lambda, or for a
12959 local static or constant. Building a new VAR_DECL
12960 should be OK in all those cases. */
12961 r = tsubst_decl (t, args, complain);
12962 if (decl_maybe_constant_var_p (r))
12964 /* We can't call cp_finish_decl, so handle the
12965 initializer by hand. */
12966 tree init = tsubst_init (DECL_INITIAL (t), r, args,
12967 complain, in_decl);
12968 if (!processing_template_decl)
12969 init = maybe_constant_init (init);
12970 if (processing_template_decl
12971 ? potential_constant_expression (init)
12972 : reduced_constant_expression_p (init))
12973 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
12974 = TREE_CONSTANT (r) = true;
12975 DECL_INITIAL (r) = init;
12977 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
12978 || decl_constant_var_p (r)
12979 || errorcount || sorrycount);
12980 if (!processing_template_decl)
12982 if (TREE_STATIC (r))
12983 rest_of_decl_compilation (r, toplevel_bindings_p (),
12984 at_eof);
12985 else if (decl_constant_var_p (r))
12986 /* A use of a local constant decays to its value.
12987 FIXME update for core DR 696. */
12988 r = scalar_constant_value (r);
12991 /* Remember this for subsequent uses. */
12992 if (local_specializations)
12993 register_local_specialization (r, t);
12996 else
12997 r = t;
12998 mark_used (r);
12999 return r;
13001 case NAMESPACE_DECL:
13002 return t;
13004 case OVERLOAD:
13005 /* An OVERLOAD will always be a non-dependent overload set; an
13006 overload set from function scope will just be represented with an
13007 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13008 gcc_assert (!uses_template_parms (t));
13009 return t;
13011 case BASELINK:
13012 return tsubst_baselink (t, current_nonlambda_class_type (),
13013 args, complain, in_decl);
13015 case TEMPLATE_DECL:
13016 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13017 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13018 args, complain, in_decl);
13019 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13020 return tsubst (t, args, complain, in_decl);
13021 else if (DECL_CLASS_SCOPE_P (t)
13022 && uses_template_parms (DECL_CONTEXT (t)))
13024 /* Template template argument like the following example need
13025 special treatment:
13027 template <template <class> class TT> struct C {};
13028 template <class T> struct D {
13029 template <class U> struct E {};
13030 C<E> c; // #1
13032 D<int> d; // #2
13034 We are processing the template argument `E' in #1 for
13035 the template instantiation #2. Originally, `E' is a
13036 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13037 have to substitute this with one having context `D<int>'. */
13039 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13040 return lookup_field (context, DECL_NAME(t), 0, false);
13042 else
13043 /* Ordinary template template argument. */
13044 return t;
13046 case CAST_EXPR:
13047 case REINTERPRET_CAST_EXPR:
13048 case CONST_CAST_EXPR:
13049 case STATIC_CAST_EXPR:
13050 case DYNAMIC_CAST_EXPR:
13051 case IMPLICIT_CONV_EXPR:
13052 case CONVERT_EXPR:
13053 case NOP_EXPR:
13055 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13056 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13057 return build1 (code, type, op0);
13060 case SIZEOF_EXPR:
13061 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13064 tree expanded, op = TREE_OPERAND (t, 0);
13065 int len = 0;
13067 if (SIZEOF_EXPR_TYPE_P (t))
13068 op = TREE_TYPE (op);
13070 ++cp_unevaluated_operand;
13071 ++c_inhibit_evaluation_warnings;
13072 /* We only want to compute the number of arguments. */
13073 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13074 --cp_unevaluated_operand;
13075 --c_inhibit_evaluation_warnings;
13077 if (TREE_CODE (expanded) == TREE_VEC)
13078 len = TREE_VEC_LENGTH (expanded);
13080 if (expanded == error_mark_node)
13081 return error_mark_node;
13082 else if (PACK_EXPANSION_P (expanded)
13083 || (TREE_CODE (expanded) == TREE_VEC
13084 && len > 0
13085 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13087 if (TREE_CODE (expanded) == TREE_VEC)
13088 expanded = TREE_VEC_ELT (expanded, len - 1);
13090 if (TYPE_P (expanded))
13091 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13092 complain & tf_error);
13093 else
13094 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13095 complain & tf_error);
13097 else
13098 return build_int_cst (size_type_node, len);
13100 if (SIZEOF_EXPR_TYPE_P (t))
13102 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13103 args, complain, in_decl);
13104 r = build1 (NOP_EXPR, r, error_mark_node);
13105 r = build1 (SIZEOF_EXPR,
13106 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13107 SIZEOF_EXPR_TYPE_P (r) = 1;
13108 return r;
13110 /* Fall through */
13112 case INDIRECT_REF:
13113 case NEGATE_EXPR:
13114 case TRUTH_NOT_EXPR:
13115 case BIT_NOT_EXPR:
13116 case ADDR_EXPR:
13117 case UNARY_PLUS_EXPR: /* Unary + */
13118 case ALIGNOF_EXPR:
13119 case AT_ENCODE_EXPR:
13120 case ARROW_EXPR:
13121 case THROW_EXPR:
13122 case TYPEID_EXPR:
13123 case REALPART_EXPR:
13124 case IMAGPART_EXPR:
13125 case PAREN_EXPR:
13127 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13128 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13129 return build1 (code, type, op0);
13132 case COMPONENT_REF:
13134 tree object;
13135 tree name;
13137 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13138 name = TREE_OPERAND (t, 1);
13139 if (TREE_CODE (name) == BIT_NOT_EXPR)
13141 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13142 complain, in_decl);
13143 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13145 else if (TREE_CODE (name) == SCOPE_REF
13146 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
13148 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
13149 complain, in_decl);
13150 name = TREE_OPERAND (name, 1);
13151 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13152 complain, in_decl);
13153 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13154 name = build_qualified_name (/*type=*/NULL_TREE,
13155 base, name,
13156 /*template_p=*/false);
13158 else if (BASELINK_P (name))
13159 name = tsubst_baselink (name,
13160 non_reference (TREE_TYPE (object)),
13161 args, complain,
13162 in_decl);
13163 else
13164 name = tsubst_copy (name, args, complain, in_decl);
13165 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
13168 case PLUS_EXPR:
13169 case MINUS_EXPR:
13170 case MULT_EXPR:
13171 case TRUNC_DIV_EXPR:
13172 case CEIL_DIV_EXPR:
13173 case FLOOR_DIV_EXPR:
13174 case ROUND_DIV_EXPR:
13175 case EXACT_DIV_EXPR:
13176 case BIT_AND_EXPR:
13177 case BIT_IOR_EXPR:
13178 case BIT_XOR_EXPR:
13179 case TRUNC_MOD_EXPR:
13180 case FLOOR_MOD_EXPR:
13181 case TRUTH_ANDIF_EXPR:
13182 case TRUTH_ORIF_EXPR:
13183 case TRUTH_AND_EXPR:
13184 case TRUTH_OR_EXPR:
13185 case RSHIFT_EXPR:
13186 case LSHIFT_EXPR:
13187 case RROTATE_EXPR:
13188 case LROTATE_EXPR:
13189 case EQ_EXPR:
13190 case NE_EXPR:
13191 case MAX_EXPR:
13192 case MIN_EXPR:
13193 case LE_EXPR:
13194 case GE_EXPR:
13195 case LT_EXPR:
13196 case GT_EXPR:
13197 case COMPOUND_EXPR:
13198 case DOTSTAR_EXPR:
13199 case MEMBER_REF:
13200 case PREDECREMENT_EXPR:
13201 case PREINCREMENT_EXPR:
13202 case POSTDECREMENT_EXPR:
13203 case POSTINCREMENT_EXPR:
13205 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13206 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13207 return build_nt (code, op0, op1);
13210 case SCOPE_REF:
13212 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13213 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13214 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
13215 QUALIFIED_NAME_IS_TEMPLATE (t));
13218 case ARRAY_REF:
13220 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13221 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13222 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
13225 case CALL_EXPR:
13227 int n = VL_EXP_OPERAND_LENGTH (t);
13228 tree result = build_vl_exp (CALL_EXPR, n);
13229 int i;
13230 for (i = 0; i < n; i++)
13231 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13232 complain, in_decl);
13233 return result;
13236 case COND_EXPR:
13237 case MODOP_EXPR:
13238 case PSEUDO_DTOR_EXPR:
13239 case VEC_PERM_EXPR:
13241 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13242 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13243 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13244 r = build_nt (code, op0, op1, op2);
13245 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13246 return r;
13249 case NEW_EXPR:
13251 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13252 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13253 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13254 r = build_nt (code, op0, op1, op2);
13255 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13256 return r;
13259 case DELETE_EXPR:
13261 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13262 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13263 r = build_nt (code, op0, op1);
13264 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13265 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13266 return r;
13269 case TEMPLATE_ID_EXPR:
13271 /* Substituted template arguments */
13272 tree fn = TREE_OPERAND (t, 0);
13273 tree targs = TREE_OPERAND (t, 1);
13275 fn = tsubst_copy (fn, args, complain, in_decl);
13276 if (targs)
13277 targs = tsubst_template_args (targs, args, complain, in_decl);
13279 return lookup_template_function (fn, targs);
13282 case TREE_LIST:
13284 tree purpose, value, chain;
13286 if (t == void_list_node)
13287 return t;
13289 purpose = TREE_PURPOSE (t);
13290 if (purpose)
13291 purpose = tsubst_copy (purpose, args, complain, in_decl);
13292 value = TREE_VALUE (t);
13293 if (value)
13294 value = tsubst_copy (value, args, complain, in_decl);
13295 chain = TREE_CHAIN (t);
13296 if (chain && chain != void_type_node)
13297 chain = tsubst_copy (chain, args, complain, in_decl);
13298 if (purpose == TREE_PURPOSE (t)
13299 && value == TREE_VALUE (t)
13300 && chain == TREE_CHAIN (t))
13301 return t;
13302 return tree_cons (purpose, value, chain);
13305 case RECORD_TYPE:
13306 case UNION_TYPE:
13307 case ENUMERAL_TYPE:
13308 case INTEGER_TYPE:
13309 case TEMPLATE_TYPE_PARM:
13310 case TEMPLATE_TEMPLATE_PARM:
13311 case BOUND_TEMPLATE_TEMPLATE_PARM:
13312 case TEMPLATE_PARM_INDEX:
13313 case POINTER_TYPE:
13314 case REFERENCE_TYPE:
13315 case OFFSET_TYPE:
13316 case FUNCTION_TYPE:
13317 case METHOD_TYPE:
13318 case ARRAY_TYPE:
13319 case TYPENAME_TYPE:
13320 case UNBOUND_CLASS_TEMPLATE:
13321 case TYPEOF_TYPE:
13322 case DECLTYPE_TYPE:
13323 case TYPE_DECL:
13324 return tsubst (t, args, complain, in_decl);
13326 case USING_DECL:
13327 t = DECL_NAME (t);
13328 /* Fall through. */
13329 case IDENTIFIER_NODE:
13330 if (IDENTIFIER_TYPENAME_P (t))
13332 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13333 return mangle_conv_op_name_for_type (new_type);
13335 else
13336 return t;
13338 case CONSTRUCTOR:
13339 /* This is handled by tsubst_copy_and_build. */
13340 gcc_unreachable ();
13342 case VA_ARG_EXPR:
13344 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13345 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13346 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
13349 case CLEANUP_POINT_EXPR:
13350 /* We shouldn't have built any of these during initial template
13351 generation. Instead, they should be built during instantiation
13352 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13353 gcc_unreachable ();
13355 case OFFSET_REF:
13357 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13358 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13359 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13360 r = build2 (code, type, op0, op1);
13361 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13362 mark_used (TREE_OPERAND (r, 1));
13363 return r;
13366 case EXPR_PACK_EXPANSION:
13367 error ("invalid use of pack expansion expression");
13368 return error_mark_node;
13370 case NONTYPE_ARGUMENT_PACK:
13371 error ("use %<...%> to expand argument pack");
13372 return error_mark_node;
13374 case VOID_CST:
13375 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
13376 return t;
13378 case INTEGER_CST:
13379 case REAL_CST:
13380 case STRING_CST:
13381 case COMPLEX_CST:
13383 /* Instantiate any typedefs in the type. */
13384 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13385 r = fold_convert (type, t);
13386 gcc_assert (TREE_CODE (r) == code);
13387 return r;
13390 case PTRMEM_CST:
13391 /* These can sometimes show up in a partial instantiation, but never
13392 involve template parms. */
13393 gcc_assert (!uses_template_parms (t));
13394 return t;
13396 default:
13397 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13398 gcc_checking_assert (false);
13399 return t;
13403 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13405 static tree
13406 tsubst_omp_clauses (tree clauses, bool declare_simd,
13407 tree args, tsubst_flags_t complain, tree in_decl)
13409 tree new_clauses = NULL, nc, oc;
13411 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13413 nc = copy_node (oc);
13414 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13415 new_clauses = nc;
13417 switch (OMP_CLAUSE_CODE (nc))
13419 case OMP_CLAUSE_LASTPRIVATE:
13420 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13422 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13423 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13424 in_decl, /*integral_constant_expression_p=*/false);
13425 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13426 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13428 /* FALLTHRU */
13429 case OMP_CLAUSE_PRIVATE:
13430 case OMP_CLAUSE_SHARED:
13431 case OMP_CLAUSE_FIRSTPRIVATE:
13432 case OMP_CLAUSE_COPYIN:
13433 case OMP_CLAUSE_COPYPRIVATE:
13434 case OMP_CLAUSE_IF:
13435 case OMP_CLAUSE_NUM_THREADS:
13436 case OMP_CLAUSE_SCHEDULE:
13437 case OMP_CLAUSE_COLLAPSE:
13438 case OMP_CLAUSE_FINAL:
13439 case OMP_CLAUSE_DEPEND:
13440 case OMP_CLAUSE_FROM:
13441 case OMP_CLAUSE_TO:
13442 case OMP_CLAUSE_UNIFORM:
13443 case OMP_CLAUSE_MAP:
13444 case OMP_CLAUSE_DEVICE:
13445 case OMP_CLAUSE_DIST_SCHEDULE:
13446 case OMP_CLAUSE_NUM_TEAMS:
13447 case OMP_CLAUSE_THREAD_LIMIT:
13448 case OMP_CLAUSE_SAFELEN:
13449 case OMP_CLAUSE_SIMDLEN:
13450 OMP_CLAUSE_OPERAND (nc, 0)
13451 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13452 in_decl, /*integral_constant_expression_p=*/false);
13453 break;
13454 case OMP_CLAUSE_REDUCTION:
13455 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13457 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13458 if (TREE_CODE (placeholder) == SCOPE_REF)
13460 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13461 complain, in_decl);
13462 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13463 = build_qualified_name (NULL_TREE, scope,
13464 TREE_OPERAND (placeholder, 1),
13465 false);
13467 else
13468 gcc_assert (identifier_p (placeholder));
13470 OMP_CLAUSE_OPERAND (nc, 0)
13471 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13472 in_decl, /*integral_constant_expression_p=*/false);
13473 break;
13474 case OMP_CLAUSE_LINEAR:
13475 case OMP_CLAUSE_ALIGNED:
13476 OMP_CLAUSE_OPERAND (nc, 0)
13477 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13478 in_decl, /*integral_constant_expression_p=*/false);
13479 OMP_CLAUSE_OPERAND (nc, 1)
13480 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13481 in_decl, /*integral_constant_expression_p=*/false);
13482 break;
13484 case OMP_CLAUSE_NOWAIT:
13485 case OMP_CLAUSE_ORDERED:
13486 case OMP_CLAUSE_DEFAULT:
13487 case OMP_CLAUSE_UNTIED:
13488 case OMP_CLAUSE_MERGEABLE:
13489 case OMP_CLAUSE_INBRANCH:
13490 case OMP_CLAUSE_NOTINBRANCH:
13491 case OMP_CLAUSE_PROC_BIND:
13492 case OMP_CLAUSE_FOR:
13493 case OMP_CLAUSE_PARALLEL:
13494 case OMP_CLAUSE_SECTIONS:
13495 case OMP_CLAUSE_TASKGROUP:
13496 break;
13497 default:
13498 gcc_unreachable ();
13502 new_clauses = nreverse (new_clauses);
13503 if (!declare_simd)
13504 new_clauses = finish_omp_clauses (new_clauses);
13505 return new_clauses;
13508 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13510 static tree
13511 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13512 tree in_decl)
13514 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13516 tree purpose, value, chain;
13518 if (t == NULL)
13519 return t;
13521 if (TREE_CODE (t) != TREE_LIST)
13522 return tsubst_copy_and_build (t, args, complain, in_decl,
13523 /*function_p=*/false,
13524 /*integral_constant_expression_p=*/false);
13526 if (t == void_list_node)
13527 return t;
13529 purpose = TREE_PURPOSE (t);
13530 if (purpose)
13531 purpose = RECUR (purpose);
13532 value = TREE_VALUE (t);
13533 if (value)
13535 if (TREE_CODE (value) != LABEL_DECL)
13536 value = RECUR (value);
13537 else
13539 value = lookup_label (DECL_NAME (value));
13540 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13541 TREE_USED (value) = 1;
13544 chain = TREE_CHAIN (t);
13545 if (chain && chain != void_type_node)
13546 chain = RECUR (chain);
13547 return tree_cons (purpose, value, chain);
13548 #undef RECUR
13551 /* Substitute one OMP_FOR iterator. */
13553 static void
13554 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13555 tree condv, tree incrv, tree *clauses,
13556 tree args, tsubst_flags_t complain, tree in_decl,
13557 bool integral_constant_expression_p)
13559 #define RECUR(NODE) \
13560 tsubst_expr ((NODE), args, complain, in_decl, \
13561 integral_constant_expression_p)
13562 tree decl, init, cond, incr;
13564 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13565 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13566 decl = TREE_OPERAND (init, 0);
13567 init = TREE_OPERAND (init, 1);
13568 tree decl_expr = NULL_TREE;
13569 if (init && TREE_CODE (init) == DECL_EXPR)
13571 /* We need to jump through some hoops to handle declarations in the
13572 for-init-statement, since we might need to handle auto deduction,
13573 but we need to keep control of initialization. */
13574 decl_expr = init;
13575 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13576 decl = tsubst_decl (decl, args, complain);
13578 else
13579 decl = RECUR (decl);
13580 init = RECUR (init);
13582 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13583 if (auto_node && init)
13584 TREE_TYPE (decl)
13585 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13587 gcc_assert (!type_dependent_expression_p (decl));
13589 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13591 if (decl_expr)
13593 /* Declare the variable, but don't let that initialize it. */
13594 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13595 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13596 RECUR (decl_expr);
13597 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13600 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13601 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13602 if (TREE_CODE (incr) == MODIFY_EXPR)
13604 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13605 tree rhs = RECUR (TREE_OPERAND (incr, 1));
13606 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
13607 NOP_EXPR, rhs, complain);
13609 else
13610 incr = RECUR (incr);
13611 TREE_VEC_ELT (declv, i) = decl;
13612 TREE_VEC_ELT (initv, i) = init;
13613 TREE_VEC_ELT (condv, i) = cond;
13614 TREE_VEC_ELT (incrv, i) = incr;
13615 return;
13618 if (decl_expr)
13620 /* Declare and initialize the variable. */
13621 RECUR (decl_expr);
13622 init = NULL_TREE;
13624 else if (init)
13626 tree c;
13627 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13629 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13630 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13631 && OMP_CLAUSE_DECL (c) == decl)
13632 break;
13633 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13634 && OMP_CLAUSE_DECL (c) == decl)
13635 error ("iteration variable %qD should not be firstprivate", decl);
13636 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13637 && OMP_CLAUSE_DECL (c) == decl)
13638 error ("iteration variable %qD should not be reduction", decl);
13640 if (c == NULL)
13642 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13643 OMP_CLAUSE_DECL (c) = decl;
13644 c = finish_omp_clauses (c);
13645 if (c)
13647 OMP_CLAUSE_CHAIN (c) = *clauses;
13648 *clauses = c;
13652 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13653 if (COMPARISON_CLASS_P (cond))
13655 tree op0 = RECUR (TREE_OPERAND (cond, 0));
13656 tree op1 = RECUR (TREE_OPERAND (cond, 1));
13657 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
13659 else
13660 cond = RECUR (cond);
13661 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13662 switch (TREE_CODE (incr))
13664 case PREINCREMENT_EXPR:
13665 case PREDECREMENT_EXPR:
13666 case POSTINCREMENT_EXPR:
13667 case POSTDECREMENT_EXPR:
13668 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13669 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13670 break;
13671 case MODIFY_EXPR:
13672 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13673 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13675 tree rhs = TREE_OPERAND (incr, 1);
13676 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13677 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13678 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13679 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13680 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13681 rhs0, rhs1));
13683 else
13684 incr = RECUR (incr);
13685 break;
13686 case MODOP_EXPR:
13687 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13688 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13690 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13691 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13692 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13693 TREE_TYPE (decl), lhs,
13694 RECUR (TREE_OPERAND (incr, 2))));
13696 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13697 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13698 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13700 tree rhs = TREE_OPERAND (incr, 2);
13701 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13702 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13703 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13704 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13705 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13706 rhs0, rhs1));
13708 else
13709 incr = RECUR (incr);
13710 break;
13711 default:
13712 incr = RECUR (incr);
13713 break;
13716 TREE_VEC_ELT (declv, i) = decl;
13717 TREE_VEC_ELT (initv, i) = init;
13718 TREE_VEC_ELT (condv, i) = cond;
13719 TREE_VEC_ELT (incrv, i) = incr;
13720 #undef RECUR
13723 /* Like tsubst_copy for expressions, etc. but also does semantic
13724 processing. */
13726 static tree
13727 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13728 bool integral_constant_expression_p)
13730 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13731 #define RECUR(NODE) \
13732 tsubst_expr ((NODE), args, complain, in_decl, \
13733 integral_constant_expression_p)
13735 tree stmt, tmp;
13736 tree r;
13737 location_t loc;
13739 if (t == NULL_TREE || t == error_mark_node)
13740 return t;
13742 loc = input_location;
13743 if (EXPR_HAS_LOCATION (t))
13744 input_location = EXPR_LOCATION (t);
13745 if (STATEMENT_CODE_P (TREE_CODE (t)))
13746 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13748 switch (TREE_CODE (t))
13750 case STATEMENT_LIST:
13752 tree_stmt_iterator i;
13753 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13754 RECUR (tsi_stmt (i));
13755 break;
13758 case CTOR_INITIALIZER:
13759 finish_mem_initializers (tsubst_initializer_list
13760 (TREE_OPERAND (t, 0), args));
13761 break;
13763 case RETURN_EXPR:
13764 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13765 break;
13767 case EXPR_STMT:
13768 tmp = RECUR (EXPR_STMT_EXPR (t));
13769 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13770 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13771 else
13772 finish_expr_stmt (tmp);
13773 break;
13775 case USING_STMT:
13776 do_using_directive (USING_STMT_NAMESPACE (t));
13777 break;
13779 case DECL_EXPR:
13781 tree decl, pattern_decl;
13782 tree init;
13784 pattern_decl = decl = DECL_EXPR_DECL (t);
13785 if (TREE_CODE (decl) == LABEL_DECL)
13786 finish_label_decl (DECL_NAME (decl));
13787 else if (TREE_CODE (decl) == USING_DECL)
13789 tree scope = USING_DECL_SCOPE (decl);
13790 tree name = DECL_NAME (decl);
13791 tree decl;
13793 scope = tsubst (scope, args, complain, in_decl);
13794 decl = lookup_qualified_name (scope, name,
13795 /*is_type_p=*/false,
13796 /*complain=*/false);
13797 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13798 qualified_name_lookup_error (scope, name, decl, input_location);
13799 else
13800 do_local_using_decl (decl, scope, name);
13802 else if (DECL_PACK_P (decl))
13804 /* Don't build up decls for a variadic capture proxy, we'll
13805 instantiate the elements directly as needed. */
13806 break;
13808 else
13810 init = DECL_INITIAL (decl);
13811 decl = tsubst (decl, args, complain, in_decl);
13812 if (decl != error_mark_node)
13814 /* By marking the declaration as instantiated, we avoid
13815 trying to instantiate it. Since instantiate_decl can't
13816 handle local variables, and since we've already done
13817 all that needs to be done, that's the right thing to
13818 do. */
13819 if (VAR_P (decl))
13820 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13821 if (VAR_P (decl)
13822 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13823 /* Anonymous aggregates are a special case. */
13824 finish_anon_union (decl);
13825 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13827 DECL_CONTEXT (decl) = current_function_decl;
13828 if (DECL_NAME (decl) == this_identifier)
13830 tree lam = DECL_CONTEXT (current_function_decl);
13831 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13832 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13834 insert_capture_proxy (decl);
13836 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13837 /* We already did a pushtag. */;
13838 else if (TREE_CODE (decl) == FUNCTION_DECL
13839 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13840 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13842 DECL_CONTEXT (decl) = NULL_TREE;
13843 pushdecl (decl);
13844 DECL_CONTEXT (decl) = current_function_decl;
13845 cp_check_omp_declare_reduction (decl);
13847 else
13849 int const_init = false;
13850 maybe_push_decl (decl);
13851 if (VAR_P (decl)
13852 && DECL_PRETTY_FUNCTION_P (decl))
13854 /* For __PRETTY_FUNCTION__ we have to adjust the
13855 initializer. */
13856 const char *const name
13857 = cxx_printable_name (current_function_decl, 2);
13858 init = cp_fname_init (name, &TREE_TYPE (decl));
13860 else
13861 init = tsubst_init (init, decl, args, complain, in_decl);
13863 if (VAR_P (decl))
13864 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13865 (pattern_decl));
13866 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13871 break;
13874 case FOR_STMT:
13875 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13876 RECUR (FOR_INIT_STMT (t));
13877 finish_for_init_stmt (stmt);
13878 tmp = RECUR (FOR_COND (t));
13879 finish_for_cond (tmp, stmt, false);
13880 tmp = RECUR (FOR_EXPR (t));
13881 finish_for_expr (tmp, stmt);
13882 RECUR (FOR_BODY (t));
13883 finish_for_stmt (stmt);
13884 break;
13886 case RANGE_FOR_STMT:
13888 tree decl, expr;
13889 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13890 decl = RANGE_FOR_DECL (t);
13891 decl = tsubst (decl, args, complain, in_decl);
13892 maybe_push_decl (decl);
13893 expr = RECUR (RANGE_FOR_EXPR (t));
13894 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13895 RECUR (RANGE_FOR_BODY (t));
13896 finish_for_stmt (stmt);
13898 break;
13900 case WHILE_STMT:
13901 stmt = begin_while_stmt ();
13902 tmp = RECUR (WHILE_COND (t));
13903 finish_while_stmt_cond (tmp, stmt, false);
13904 RECUR (WHILE_BODY (t));
13905 finish_while_stmt (stmt);
13906 break;
13908 case DO_STMT:
13909 stmt = begin_do_stmt ();
13910 RECUR (DO_BODY (t));
13911 finish_do_body (stmt);
13912 tmp = RECUR (DO_COND (t));
13913 finish_do_stmt (tmp, stmt, false);
13914 break;
13916 case IF_STMT:
13917 stmt = begin_if_stmt ();
13918 tmp = RECUR (IF_COND (t));
13919 finish_if_stmt_cond (tmp, stmt);
13920 RECUR (THEN_CLAUSE (t));
13921 finish_then_clause (stmt);
13923 if (ELSE_CLAUSE (t))
13925 begin_else_clause (stmt);
13926 RECUR (ELSE_CLAUSE (t));
13927 finish_else_clause (stmt);
13930 finish_if_stmt (stmt);
13931 break;
13933 case BIND_EXPR:
13934 if (BIND_EXPR_BODY_BLOCK (t))
13935 stmt = begin_function_body ();
13936 else
13937 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13938 ? BCS_TRY_BLOCK : 0);
13940 RECUR (BIND_EXPR_BODY (t));
13942 if (BIND_EXPR_BODY_BLOCK (t))
13943 finish_function_body (stmt);
13944 else
13945 finish_compound_stmt (stmt);
13946 break;
13948 case BREAK_STMT:
13949 finish_break_stmt ();
13950 break;
13952 case CONTINUE_STMT:
13953 finish_continue_stmt ();
13954 break;
13956 case SWITCH_STMT:
13957 stmt = begin_switch_stmt ();
13958 tmp = RECUR (SWITCH_STMT_COND (t));
13959 finish_switch_cond (tmp, stmt);
13960 RECUR (SWITCH_STMT_BODY (t));
13961 finish_switch_stmt (stmt);
13962 break;
13964 case CASE_LABEL_EXPR:
13966 tree low = RECUR (CASE_LOW (t));
13967 tree high = RECUR (CASE_HIGH (t));
13968 finish_case_label (EXPR_LOCATION (t), low, high);
13970 break;
13972 case LABEL_EXPR:
13974 tree decl = LABEL_EXPR_LABEL (t);
13975 tree label;
13977 label = finish_label_stmt (DECL_NAME (decl));
13978 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13979 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13981 break;
13983 case GOTO_EXPR:
13984 tmp = GOTO_DESTINATION (t);
13985 if (TREE_CODE (tmp) != LABEL_DECL)
13986 /* Computed goto's must be tsubst'd into. On the other hand,
13987 non-computed gotos must not be; the identifier in question
13988 will have no binding. */
13989 tmp = RECUR (tmp);
13990 else
13991 tmp = DECL_NAME (tmp);
13992 finish_goto_stmt (tmp);
13993 break;
13995 case ASM_EXPR:
13997 tree string = RECUR (ASM_STRING (t));
13998 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
13999 complain, in_decl);
14000 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
14001 complain, in_decl);
14002 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
14003 complain, in_decl);
14004 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
14005 complain, in_decl);
14006 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
14007 clobbers, labels);
14008 tree asm_expr = tmp;
14009 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14010 asm_expr = TREE_OPERAND (asm_expr, 0);
14011 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14013 break;
14015 case TRY_BLOCK:
14016 if (CLEANUP_P (t))
14018 stmt = begin_try_block ();
14019 RECUR (TRY_STMTS (t));
14020 finish_cleanup_try_block (stmt);
14021 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14023 else
14025 tree compound_stmt = NULL_TREE;
14027 if (FN_TRY_BLOCK_P (t))
14028 stmt = begin_function_try_block (&compound_stmt);
14029 else
14030 stmt = begin_try_block ();
14032 RECUR (TRY_STMTS (t));
14034 if (FN_TRY_BLOCK_P (t))
14035 finish_function_try_block (stmt);
14036 else
14037 finish_try_block (stmt);
14039 RECUR (TRY_HANDLERS (t));
14040 if (FN_TRY_BLOCK_P (t))
14041 finish_function_handler_sequence (stmt, compound_stmt);
14042 else
14043 finish_handler_sequence (stmt);
14045 break;
14047 case HANDLER:
14049 tree decl = HANDLER_PARMS (t);
14051 if (decl)
14053 decl = tsubst (decl, args, complain, in_decl);
14054 /* Prevent instantiate_decl from trying to instantiate
14055 this variable. We've already done all that needs to be
14056 done. */
14057 if (decl != error_mark_node)
14058 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14060 stmt = begin_handler ();
14061 finish_handler_parms (decl, stmt);
14062 RECUR (HANDLER_BODY (t));
14063 finish_handler (stmt);
14065 break;
14067 case TAG_DEFN:
14068 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14069 if (CLASS_TYPE_P (tmp))
14071 /* Local classes are not independent templates; they are
14072 instantiated along with their containing function. And this
14073 way we don't have to deal with pushing out of one local class
14074 to instantiate a member of another local class. */
14075 tree fn;
14076 /* Closures are handled by the LAMBDA_EXPR. */
14077 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14078 complete_type (tmp);
14079 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14080 if (!DECL_ARTIFICIAL (fn))
14081 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14083 break;
14085 case STATIC_ASSERT:
14087 tree condition;
14089 ++c_inhibit_evaluation_warnings;
14090 condition =
14091 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14092 args,
14093 complain, in_decl,
14094 /*integral_constant_expression_p=*/true);
14095 --c_inhibit_evaluation_warnings;
14097 finish_static_assert (condition,
14098 STATIC_ASSERT_MESSAGE (t),
14099 STATIC_ASSERT_SOURCE_LOCATION (t),
14100 /*member_p=*/false);
14102 break;
14104 case OMP_PARALLEL:
14105 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
14106 args, complain, in_decl);
14107 stmt = begin_omp_parallel ();
14108 RECUR (OMP_PARALLEL_BODY (t));
14109 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
14110 = OMP_PARALLEL_COMBINED (t);
14111 break;
14113 case OMP_TASK:
14114 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
14115 args, complain, in_decl);
14116 stmt = begin_omp_task ();
14117 RECUR (OMP_TASK_BODY (t));
14118 finish_omp_task (tmp, stmt);
14119 break;
14121 case OMP_FOR:
14122 case OMP_SIMD:
14123 case CILK_SIMD:
14124 case CILK_FOR:
14125 case OMP_DISTRIBUTE:
14127 tree clauses, body, pre_body;
14128 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
14129 tree incrv = NULL_TREE;
14130 int i;
14132 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
14133 args, complain, in_decl);
14134 if (OMP_FOR_INIT (t) != NULL_TREE)
14136 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14137 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14138 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14139 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14142 stmt = begin_omp_structured_block ();
14144 pre_body = push_stmt_list ();
14145 RECUR (OMP_FOR_PRE_BODY (t));
14146 pre_body = pop_stmt_list (pre_body);
14148 if (OMP_FOR_INIT (t) != NULL_TREE)
14149 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
14150 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
14151 &clauses, args, complain, in_decl,
14152 integral_constant_expression_p);
14154 body = push_stmt_list ();
14155 RECUR (OMP_FOR_BODY (t));
14156 body = pop_stmt_list (body);
14158 if (OMP_FOR_INIT (t) != NULL_TREE)
14159 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
14160 condv, incrv, body, pre_body, clauses);
14161 else
14163 t = make_node (TREE_CODE (t));
14164 TREE_TYPE (t) = void_type_node;
14165 OMP_FOR_BODY (t) = body;
14166 OMP_FOR_PRE_BODY (t) = pre_body;
14167 OMP_FOR_CLAUSES (t) = clauses;
14168 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
14169 add_stmt (t);
14172 add_stmt (finish_omp_structured_block (stmt));
14174 break;
14176 case OMP_SECTIONS:
14177 case OMP_SINGLE:
14178 case OMP_TEAMS:
14179 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14180 args, complain, in_decl);
14181 stmt = push_stmt_list ();
14182 RECUR (OMP_BODY (t));
14183 stmt = pop_stmt_list (stmt);
14185 t = copy_node (t);
14186 OMP_BODY (t) = stmt;
14187 OMP_CLAUSES (t) = tmp;
14188 add_stmt (t);
14189 break;
14191 case OMP_TARGET_DATA:
14192 case OMP_TARGET:
14193 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14194 args, complain, in_decl);
14195 keep_next_level (true);
14196 stmt = begin_omp_structured_block ();
14198 RECUR (OMP_BODY (t));
14199 stmt = finish_omp_structured_block (stmt);
14201 t = copy_node (t);
14202 OMP_BODY (t) = stmt;
14203 OMP_CLAUSES (t) = tmp;
14204 add_stmt (t);
14205 break;
14207 case OMP_TARGET_UPDATE:
14208 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14209 args, complain, in_decl);
14210 t = copy_node (t);
14211 OMP_CLAUSES (t) = tmp;
14212 add_stmt (t);
14213 break;
14215 case OMP_SECTION:
14216 case OMP_CRITICAL:
14217 case OMP_MASTER:
14218 case OMP_TASKGROUP:
14219 case OMP_ORDERED:
14220 stmt = push_stmt_list ();
14221 RECUR (OMP_BODY (t));
14222 stmt = pop_stmt_list (stmt);
14224 t = copy_node (t);
14225 OMP_BODY (t) = stmt;
14226 add_stmt (t);
14227 break;
14229 case OMP_ATOMIC:
14230 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14231 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14233 tree op1 = TREE_OPERAND (t, 1);
14234 tree rhs1 = NULL_TREE;
14235 tree lhs, rhs;
14236 if (TREE_CODE (op1) == COMPOUND_EXPR)
14238 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14239 op1 = TREE_OPERAND (op1, 1);
14241 lhs = RECUR (TREE_OPERAND (op1, 0));
14242 rhs = RECUR (TREE_OPERAND (op1, 1));
14243 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14244 NULL_TREE, NULL_TREE, rhs1,
14245 OMP_ATOMIC_SEQ_CST (t));
14247 else
14249 tree op1 = TREE_OPERAND (t, 1);
14250 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14251 tree rhs1 = NULL_TREE;
14252 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14253 enum tree_code opcode = NOP_EXPR;
14254 if (code == OMP_ATOMIC_READ)
14256 v = RECUR (TREE_OPERAND (op1, 0));
14257 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14259 else if (code == OMP_ATOMIC_CAPTURE_OLD
14260 || code == OMP_ATOMIC_CAPTURE_NEW)
14262 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14263 v = RECUR (TREE_OPERAND (op1, 0));
14264 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14265 if (TREE_CODE (op11) == COMPOUND_EXPR)
14267 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14268 op11 = TREE_OPERAND (op11, 1);
14270 lhs = RECUR (TREE_OPERAND (op11, 0));
14271 rhs = RECUR (TREE_OPERAND (op11, 1));
14272 opcode = TREE_CODE (op11);
14273 if (opcode == MODIFY_EXPR)
14274 opcode = NOP_EXPR;
14276 else
14278 code = OMP_ATOMIC;
14279 lhs = RECUR (TREE_OPERAND (op1, 0));
14280 rhs = RECUR (TREE_OPERAND (op1, 1));
14282 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14283 OMP_ATOMIC_SEQ_CST (t));
14285 break;
14287 case TRANSACTION_EXPR:
14289 int flags = 0;
14290 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14291 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14293 if (TRANSACTION_EXPR_IS_STMT (t))
14295 tree body = TRANSACTION_EXPR_BODY (t);
14296 tree noex = NULL_TREE;
14297 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14299 noex = MUST_NOT_THROW_COND (body);
14300 if (noex == NULL_TREE)
14301 noex = boolean_true_node;
14302 body = TREE_OPERAND (body, 0);
14304 stmt = begin_transaction_stmt (input_location, NULL, flags);
14305 RECUR (body);
14306 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14308 else
14310 stmt = build_transaction_expr (EXPR_LOCATION (t),
14311 RECUR (TRANSACTION_EXPR_BODY (t)),
14312 flags, NULL_TREE);
14313 RETURN (stmt);
14316 break;
14318 case MUST_NOT_THROW_EXPR:
14320 tree op0 = RECUR (TREE_OPERAND (t, 0));
14321 tree cond = RECUR (MUST_NOT_THROW_COND (t));
14322 RETURN (build_must_not_throw_expr (op0, cond));
14325 case EXPR_PACK_EXPANSION:
14326 error ("invalid use of pack expansion expression");
14327 RETURN (error_mark_node);
14329 case NONTYPE_ARGUMENT_PACK:
14330 error ("use %<...%> to expand argument pack");
14331 RETURN (error_mark_node);
14333 case CILK_SPAWN_STMT:
14334 cfun->calls_cilk_spawn = 1;
14335 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14337 case CILK_SYNC_STMT:
14338 RETURN (build_cilk_sync ());
14340 case COMPOUND_EXPR:
14341 tmp = RECUR (TREE_OPERAND (t, 0));
14342 if (tmp == NULL_TREE)
14343 /* If the first operand was a statement, we're done with it. */
14344 RETURN (RECUR (TREE_OPERAND (t, 1)));
14345 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14346 RECUR (TREE_OPERAND (t, 1)),
14347 complain));
14349 case ANNOTATE_EXPR:
14350 tmp = RECUR (TREE_OPERAND (t, 0));
14351 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14352 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14354 default:
14355 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14357 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14358 /*function_p=*/false,
14359 integral_constant_expression_p));
14362 RETURN (NULL_TREE);
14363 out:
14364 input_location = loc;
14365 return r;
14366 #undef RECUR
14367 #undef RETURN
14370 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14371 function. For description of the body see comment above
14372 cp_parser_omp_declare_reduction_exprs. */
14374 static void
14375 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14377 if (t == NULL_TREE || t == error_mark_node)
14378 return;
14380 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14382 tree_stmt_iterator tsi;
14383 int i;
14384 tree stmts[7];
14385 memset (stmts, 0, sizeof stmts);
14386 for (i = 0, tsi = tsi_start (t);
14387 i < 7 && !tsi_end_p (tsi);
14388 i++, tsi_next (&tsi))
14389 stmts[i] = tsi_stmt (tsi);
14390 gcc_assert (tsi_end_p (tsi));
14392 if (i >= 3)
14394 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14395 && TREE_CODE (stmts[1]) == DECL_EXPR);
14396 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14397 args, complain, in_decl);
14398 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14399 args, complain, in_decl);
14400 DECL_CONTEXT (omp_out) = current_function_decl;
14401 DECL_CONTEXT (omp_in) = current_function_decl;
14402 keep_next_level (true);
14403 tree block = begin_omp_structured_block ();
14404 tsubst_expr (stmts[2], args, complain, in_decl, false);
14405 block = finish_omp_structured_block (block);
14406 block = maybe_cleanup_point_expr_void (block);
14407 add_decl_expr (omp_out);
14408 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14409 TREE_NO_WARNING (omp_out) = 1;
14410 add_decl_expr (omp_in);
14411 finish_expr_stmt (block);
14413 if (i >= 6)
14415 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14416 && TREE_CODE (stmts[4]) == DECL_EXPR);
14417 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14418 args, complain, in_decl);
14419 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14420 args, complain, in_decl);
14421 DECL_CONTEXT (omp_priv) = current_function_decl;
14422 DECL_CONTEXT (omp_orig) = current_function_decl;
14423 keep_next_level (true);
14424 tree block = begin_omp_structured_block ();
14425 tsubst_expr (stmts[5], args, complain, in_decl, false);
14426 block = finish_omp_structured_block (block);
14427 block = maybe_cleanup_point_expr_void (block);
14428 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14429 add_decl_expr (omp_priv);
14430 add_decl_expr (omp_orig);
14431 finish_expr_stmt (block);
14432 if (i == 7)
14433 add_decl_expr (omp_orig);
14437 /* T is a postfix-expression that is not being used in a function
14438 call. Return the substituted version of T. */
14440 static tree
14441 tsubst_non_call_postfix_expression (tree t, tree args,
14442 tsubst_flags_t complain,
14443 tree in_decl)
14445 if (TREE_CODE (t) == SCOPE_REF)
14446 t = tsubst_qualified_id (t, args, complain, in_decl,
14447 /*done=*/false, /*address_p=*/false);
14448 else
14449 t = tsubst_copy_and_build (t, args, complain, in_decl,
14450 /*function_p=*/false,
14451 /*integral_constant_expression_p=*/false);
14453 return t;
14456 /* Like tsubst but deals with expressions and performs semantic
14457 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14459 tree
14460 tsubst_copy_and_build (tree t,
14461 tree args,
14462 tsubst_flags_t complain,
14463 tree in_decl,
14464 bool function_p,
14465 bool integral_constant_expression_p)
14467 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14468 #define RECUR(NODE) \
14469 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14470 /*function_p=*/false, \
14471 integral_constant_expression_p)
14473 tree retval, op1;
14474 location_t loc;
14476 if (t == NULL_TREE || t == error_mark_node)
14477 return t;
14479 loc = input_location;
14480 if (EXPR_HAS_LOCATION (t))
14481 input_location = EXPR_LOCATION (t);
14483 /* N3276 decltype magic only applies to calls at the top level or on the
14484 right side of a comma. */
14485 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14486 complain &= ~tf_decltype;
14488 switch (TREE_CODE (t))
14490 case USING_DECL:
14491 t = DECL_NAME (t);
14492 /* Fall through. */
14493 case IDENTIFIER_NODE:
14495 tree decl;
14496 cp_id_kind idk;
14497 bool non_integral_constant_expression_p;
14498 const char *error_msg;
14500 if (IDENTIFIER_TYPENAME_P (t))
14502 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14503 t = mangle_conv_op_name_for_type (new_type);
14506 /* Look up the name. */
14507 decl = lookup_name (t);
14509 /* By convention, expressions use ERROR_MARK_NODE to indicate
14510 failure, not NULL_TREE. */
14511 if (decl == NULL_TREE)
14512 decl = error_mark_node;
14514 decl = finish_id_expression (t, decl, NULL_TREE,
14515 &idk,
14516 integral_constant_expression_p,
14517 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14518 &non_integral_constant_expression_p,
14519 /*template_p=*/false,
14520 /*done=*/true,
14521 /*address_p=*/false,
14522 /*template_arg_p=*/false,
14523 &error_msg,
14524 input_location);
14525 if (error_msg)
14526 error (error_msg);
14527 if (!function_p && identifier_p (decl))
14529 if (complain & tf_error)
14530 unqualified_name_lookup_error (decl);
14531 decl = error_mark_node;
14533 RETURN (decl);
14536 case TEMPLATE_ID_EXPR:
14538 tree object;
14539 tree templ = RECUR (TREE_OPERAND (t, 0));
14540 tree targs = TREE_OPERAND (t, 1);
14542 if (targs)
14543 targs = tsubst_template_args (targs, args, complain, in_decl);
14545 if (TREE_CODE (templ) == COMPONENT_REF)
14547 object = TREE_OPERAND (templ, 0);
14548 templ = TREE_OPERAND (templ, 1);
14550 else
14551 object = NULL_TREE;
14552 templ = lookup_template_function (templ, targs);
14554 if (object)
14555 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14556 object, templ, NULL_TREE));
14557 else
14558 RETURN (baselink_for_fns (templ));
14561 case INDIRECT_REF:
14563 tree r = RECUR (TREE_OPERAND (t, 0));
14565 if (REFERENCE_REF_P (t))
14567 /* A type conversion to reference type will be enclosed in
14568 such an indirect ref, but the substitution of the cast
14569 will have also added such an indirect ref. */
14570 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14571 r = convert_from_reference (r);
14573 else
14574 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14575 complain|decltype_flag);
14576 RETURN (r);
14579 case NOP_EXPR:
14581 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14582 tree op0 = RECUR (TREE_OPERAND (t, 0));
14583 RETURN (build_nop (type, op0));
14586 case IMPLICIT_CONV_EXPR:
14588 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14589 tree expr = RECUR (TREE_OPERAND (t, 0));
14590 int flags = LOOKUP_IMPLICIT;
14591 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14592 flags = LOOKUP_NORMAL;
14593 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14594 flags));
14597 case CONVERT_EXPR:
14599 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14600 tree op0 = RECUR (TREE_OPERAND (t, 0));
14601 RETURN (build1 (CONVERT_EXPR, type, op0));
14604 case CAST_EXPR:
14605 case REINTERPRET_CAST_EXPR:
14606 case CONST_CAST_EXPR:
14607 case DYNAMIC_CAST_EXPR:
14608 case STATIC_CAST_EXPR:
14610 tree type;
14611 tree op, r = NULL_TREE;
14613 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14614 if (integral_constant_expression_p
14615 && !cast_valid_in_integral_constant_expression_p (type))
14617 if (complain & tf_error)
14618 error ("a cast to a type other than an integral or "
14619 "enumeration type cannot appear in a constant-expression");
14620 RETURN (error_mark_node);
14623 op = RECUR (TREE_OPERAND (t, 0));
14625 warning_sentinel s(warn_useless_cast);
14626 switch (TREE_CODE (t))
14628 case CAST_EXPR:
14629 r = build_functional_cast (type, op, complain);
14630 break;
14631 case REINTERPRET_CAST_EXPR:
14632 r = build_reinterpret_cast (type, op, complain);
14633 break;
14634 case CONST_CAST_EXPR:
14635 r = build_const_cast (type, op, complain);
14636 break;
14637 case DYNAMIC_CAST_EXPR:
14638 r = build_dynamic_cast (type, op, complain);
14639 break;
14640 case STATIC_CAST_EXPR:
14641 r = build_static_cast (type, op, complain);
14642 break;
14643 default:
14644 gcc_unreachable ();
14647 RETURN (r);
14650 case POSTDECREMENT_EXPR:
14651 case POSTINCREMENT_EXPR:
14652 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14653 args, complain, in_decl);
14654 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14655 complain|decltype_flag));
14657 case PREDECREMENT_EXPR:
14658 case PREINCREMENT_EXPR:
14659 case NEGATE_EXPR:
14660 case BIT_NOT_EXPR:
14661 case ABS_EXPR:
14662 case TRUTH_NOT_EXPR:
14663 case UNARY_PLUS_EXPR: /* Unary + */
14664 case REALPART_EXPR:
14665 case IMAGPART_EXPR:
14666 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14667 RECUR (TREE_OPERAND (t, 0)),
14668 complain|decltype_flag));
14670 case FIX_TRUNC_EXPR:
14671 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14672 0, complain));
14674 case ADDR_EXPR:
14675 op1 = TREE_OPERAND (t, 0);
14676 if (TREE_CODE (op1) == LABEL_DECL)
14677 RETURN (finish_label_address_expr (DECL_NAME (op1),
14678 EXPR_LOCATION (op1)));
14679 if (TREE_CODE (op1) == SCOPE_REF)
14680 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14681 /*done=*/true, /*address_p=*/true);
14682 else
14683 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14684 in_decl);
14685 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14686 complain|decltype_flag));
14688 case PLUS_EXPR:
14689 case MINUS_EXPR:
14690 case MULT_EXPR:
14691 case TRUNC_DIV_EXPR:
14692 case CEIL_DIV_EXPR:
14693 case FLOOR_DIV_EXPR:
14694 case ROUND_DIV_EXPR:
14695 case EXACT_DIV_EXPR:
14696 case BIT_AND_EXPR:
14697 case BIT_IOR_EXPR:
14698 case BIT_XOR_EXPR:
14699 case TRUNC_MOD_EXPR:
14700 case FLOOR_MOD_EXPR:
14701 case TRUTH_ANDIF_EXPR:
14702 case TRUTH_ORIF_EXPR:
14703 case TRUTH_AND_EXPR:
14704 case TRUTH_OR_EXPR:
14705 case RSHIFT_EXPR:
14706 case LSHIFT_EXPR:
14707 case RROTATE_EXPR:
14708 case LROTATE_EXPR:
14709 case EQ_EXPR:
14710 case NE_EXPR:
14711 case MAX_EXPR:
14712 case MIN_EXPR:
14713 case LE_EXPR:
14714 case GE_EXPR:
14715 case LT_EXPR:
14716 case GT_EXPR:
14717 case MEMBER_REF:
14718 case DOTSTAR_EXPR:
14720 warning_sentinel s1(warn_type_limits);
14721 warning_sentinel s2(warn_div_by_zero);
14722 tree op0 = RECUR (TREE_OPERAND (t, 0));
14723 tree op1 = RECUR (TREE_OPERAND (t, 1));
14724 tree r = build_x_binary_op
14725 (input_location, TREE_CODE (t),
14726 op0,
14727 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14728 ? ERROR_MARK
14729 : TREE_CODE (TREE_OPERAND (t, 0))),
14730 op1,
14731 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14732 ? ERROR_MARK
14733 : TREE_CODE (TREE_OPERAND (t, 1))),
14734 /*overload=*/NULL,
14735 complain|decltype_flag);
14736 if (EXPR_P (r) && TREE_NO_WARNING (t))
14737 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14739 RETURN (r);
14742 case POINTER_PLUS_EXPR:
14744 tree op0 = RECUR (TREE_OPERAND (t, 0));
14745 tree op1 = RECUR (TREE_OPERAND (t, 1));
14746 return fold_build_pointer_plus (op0, op1);
14749 case SCOPE_REF:
14750 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14751 /*address_p=*/false));
14752 case ARRAY_REF:
14753 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14754 args, complain, in_decl);
14755 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14756 RECUR (TREE_OPERAND (t, 1)),
14757 complain|decltype_flag));
14759 case ARRAY_NOTATION_REF:
14761 tree start_index, length, stride;
14762 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14763 args, complain, in_decl);
14764 start_index = RECUR (ARRAY_NOTATION_START (t));
14765 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14766 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14767 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14768 length, stride, TREE_TYPE (op1)));
14770 case SIZEOF_EXPR:
14771 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14772 RETURN (tsubst_copy (t, args, complain, in_decl));
14773 /* Fall through */
14775 case ALIGNOF_EXPR:
14777 tree r;
14779 op1 = TREE_OPERAND (t, 0);
14780 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14781 op1 = TREE_TYPE (op1);
14782 if (!args)
14784 /* When there are no ARGS, we are trying to evaluate a
14785 non-dependent expression from the parser. Trying to do
14786 the substitutions may not work. */
14787 if (!TYPE_P (op1))
14788 op1 = TREE_TYPE (op1);
14790 else
14792 ++cp_unevaluated_operand;
14793 ++c_inhibit_evaluation_warnings;
14794 if (TYPE_P (op1))
14795 op1 = tsubst (op1, args, complain, in_decl);
14796 else
14797 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14798 /*function_p=*/false,
14799 /*integral_constant_expression_p=*/
14800 false);
14801 --cp_unevaluated_operand;
14802 --c_inhibit_evaluation_warnings;
14804 if (TYPE_P (op1))
14805 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14806 complain & tf_error);
14807 else
14808 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14809 complain & tf_error);
14810 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14812 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14814 if (!processing_template_decl && TYPE_P (op1))
14816 r = build_min (SIZEOF_EXPR, size_type_node,
14817 build1 (NOP_EXPR, op1, error_mark_node));
14818 SIZEOF_EXPR_TYPE_P (r) = 1;
14820 else
14821 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14822 TREE_SIDE_EFFECTS (r) = 0;
14823 TREE_READONLY (r) = 1;
14825 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14827 RETURN (r);
14830 case AT_ENCODE_EXPR:
14832 op1 = TREE_OPERAND (t, 0);
14833 ++cp_unevaluated_operand;
14834 ++c_inhibit_evaluation_warnings;
14835 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14836 /*function_p=*/false,
14837 /*integral_constant_expression_p=*/false);
14838 --cp_unevaluated_operand;
14839 --c_inhibit_evaluation_warnings;
14840 RETURN (objc_build_encode_expr (op1));
14843 case NOEXCEPT_EXPR:
14844 op1 = TREE_OPERAND (t, 0);
14845 ++cp_unevaluated_operand;
14846 ++c_inhibit_evaluation_warnings;
14847 ++cp_noexcept_operand;
14848 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14849 /*function_p=*/false,
14850 /*integral_constant_expression_p=*/false);
14851 --cp_unevaluated_operand;
14852 --c_inhibit_evaluation_warnings;
14853 --cp_noexcept_operand;
14854 RETURN (finish_noexcept_expr (op1, complain));
14856 case MODOP_EXPR:
14858 warning_sentinel s(warn_div_by_zero);
14859 tree lhs = RECUR (TREE_OPERAND (t, 0));
14860 tree rhs = RECUR (TREE_OPERAND (t, 2));
14861 tree r = build_x_modify_expr
14862 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
14863 complain|decltype_flag);
14864 /* TREE_NO_WARNING must be set if either the expression was
14865 parenthesized or it uses an operator such as >>= rather
14866 than plain assignment. In the former case, it was already
14867 set and must be copied. In the latter case,
14868 build_x_modify_expr sets it and it must not be reset
14869 here. */
14870 if (TREE_NO_WARNING (t))
14871 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14873 RETURN (r);
14876 case ARROW_EXPR:
14877 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14878 args, complain, in_decl);
14879 /* Remember that there was a reference to this entity. */
14880 if (DECL_P (op1))
14881 mark_used (op1);
14882 RETURN (build_x_arrow (input_location, op1, complain));
14884 case NEW_EXPR:
14886 tree placement = RECUR (TREE_OPERAND (t, 0));
14887 tree init = RECUR (TREE_OPERAND (t, 3));
14888 vec<tree, va_gc> *placement_vec;
14889 vec<tree, va_gc> *init_vec;
14890 tree ret;
14892 if (placement == NULL_TREE)
14893 placement_vec = NULL;
14894 else
14896 placement_vec = make_tree_vector ();
14897 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14898 vec_safe_push (placement_vec, TREE_VALUE (placement));
14901 /* If there was an initializer in the original tree, but it
14902 instantiated to an empty list, then we should pass a
14903 non-NULL empty vector to tell build_new that it was an
14904 empty initializer() rather than no initializer. This can
14905 only happen when the initializer is a pack expansion whose
14906 parameter packs are of length zero. */
14907 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14908 init_vec = NULL;
14909 else
14911 init_vec = make_tree_vector ();
14912 if (init == void_node)
14913 gcc_assert (init_vec != NULL);
14914 else
14916 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14917 vec_safe_push (init_vec, TREE_VALUE (init));
14921 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
14922 tree op2 = RECUR (TREE_OPERAND (t, 2));
14923 ret = build_new (&placement_vec, op1, op2, &init_vec,
14924 NEW_EXPR_USE_GLOBAL (t),
14925 complain);
14927 if (placement_vec != NULL)
14928 release_tree_vector (placement_vec);
14929 if (init_vec != NULL)
14930 release_tree_vector (init_vec);
14932 RETURN (ret);
14935 case DELETE_EXPR:
14937 tree op0 = RECUR (TREE_OPERAND (t, 0));
14938 tree op1 = RECUR (TREE_OPERAND (t, 1));
14939 RETURN (delete_sanity (op0, op1,
14940 DELETE_EXPR_USE_VEC (t),
14941 DELETE_EXPR_USE_GLOBAL (t),
14942 complain));
14945 case COMPOUND_EXPR:
14947 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14948 complain & ~tf_decltype, in_decl,
14949 /*function_p=*/false,
14950 integral_constant_expression_p);
14951 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14952 op0,
14953 RECUR (TREE_OPERAND (t, 1)),
14954 complain|decltype_flag));
14957 case CALL_EXPR:
14959 tree function;
14960 vec<tree, va_gc> *call_args;
14961 unsigned int nargs, i;
14962 bool qualified_p;
14963 bool koenig_p;
14964 tree ret;
14966 function = CALL_EXPR_FN (t);
14967 /* When we parsed the expression, we determined whether or
14968 not Koenig lookup should be performed. */
14969 koenig_p = KOENIG_LOOKUP_P (t);
14970 if (TREE_CODE (function) == SCOPE_REF)
14972 qualified_p = true;
14973 function = tsubst_qualified_id (function, args, complain, in_decl,
14974 /*done=*/false,
14975 /*address_p=*/false);
14977 else if (koenig_p && identifier_p (function))
14979 /* Do nothing; calling tsubst_copy_and_build on an identifier
14980 would incorrectly perform unqualified lookup again.
14982 Note that we can also have an IDENTIFIER_NODE if the earlier
14983 unqualified lookup found a member function; in that case
14984 koenig_p will be false and we do want to do the lookup
14985 again to find the instantiated member function.
14987 FIXME but doing that causes c++/15272, so we need to stop
14988 using IDENTIFIER_NODE in that situation. */
14989 qualified_p = false;
14991 else
14993 if (TREE_CODE (function) == COMPONENT_REF)
14995 tree op = TREE_OPERAND (function, 1);
14997 qualified_p = (TREE_CODE (op) == SCOPE_REF
14998 || (BASELINK_P (op)
14999 && BASELINK_QUALIFIED_P (op)));
15001 else
15002 qualified_p = false;
15004 if (TREE_CODE (function) == ADDR_EXPR
15005 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
15006 /* Avoid error about taking the address of a constructor. */
15007 function = TREE_OPERAND (function, 0);
15009 function = tsubst_copy_and_build (function, args, complain,
15010 in_decl,
15011 !qualified_p,
15012 integral_constant_expression_p);
15014 if (BASELINK_P (function))
15015 qualified_p = true;
15018 nargs = call_expr_nargs (t);
15019 call_args = make_tree_vector ();
15020 for (i = 0; i < nargs; ++i)
15022 tree arg = CALL_EXPR_ARG (t, i);
15024 if (!PACK_EXPANSION_P (arg))
15025 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15026 else
15028 /* Expand the pack expansion and push each entry onto
15029 CALL_ARGS. */
15030 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15031 if (TREE_CODE (arg) == TREE_VEC)
15033 unsigned int len, j;
15035 len = TREE_VEC_LENGTH (arg);
15036 for (j = 0; j < len; ++j)
15038 tree value = TREE_VEC_ELT (arg, j);
15039 if (value != NULL_TREE)
15040 value = convert_from_reference (value);
15041 vec_safe_push (call_args, value);
15044 else
15046 /* A partial substitution. Add one entry. */
15047 vec_safe_push (call_args, arg);
15052 /* We do not perform argument-dependent lookup if normal
15053 lookup finds a non-function, in accordance with the
15054 expected resolution of DR 218. */
15055 if (koenig_p
15056 && ((is_overloaded_fn (function)
15057 /* If lookup found a member function, the Koenig lookup is
15058 not appropriate, even if an unqualified-name was used
15059 to denote the function. */
15060 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15061 || identifier_p (function))
15062 /* Only do this when substitution turns a dependent call
15063 into a non-dependent call. */
15064 && type_dependent_expression_p_push (t)
15065 && !any_type_dependent_arguments_p (call_args))
15066 function = perform_koenig_lookup (function, call_args, tf_none);
15068 if (identifier_p (function)
15069 && !any_type_dependent_arguments_p (call_args))
15071 if (koenig_p && (complain & tf_warning_or_error))
15073 /* For backwards compatibility and good diagnostics, try
15074 the unqualified lookup again if we aren't in SFINAE
15075 context. */
15076 tree unq = (tsubst_copy_and_build
15077 (function, args, complain, in_decl, true,
15078 integral_constant_expression_p));
15079 if (unq == error_mark_node)
15080 RETURN (error_mark_node);
15082 if (unq != function)
15084 tree fn = unq;
15085 if (INDIRECT_REF_P (fn))
15086 fn = TREE_OPERAND (fn, 0);
15087 if (TREE_CODE (fn) == COMPONENT_REF)
15088 fn = TREE_OPERAND (fn, 1);
15089 if (is_overloaded_fn (fn))
15090 fn = get_first_fn (fn);
15091 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
15092 "%qD was not declared in this scope, "
15093 "and no declarations were found by "
15094 "argument-dependent lookup at the point "
15095 "of instantiation", function))
15097 if (!DECL_P (fn))
15098 /* Can't say anything more. */;
15099 else if (DECL_CLASS_SCOPE_P (fn))
15101 location_t loc = EXPR_LOC_OR_LOC (t,
15102 input_location);
15103 inform (loc,
15104 "declarations in dependent base %qT are "
15105 "not found by unqualified lookup",
15106 DECL_CLASS_CONTEXT (fn));
15107 if (current_class_ptr)
15108 inform (loc,
15109 "use %<this->%D%> instead", function);
15110 else
15111 inform (loc,
15112 "use %<%T::%D%> instead",
15113 current_class_name, function);
15115 else
15116 inform (0, "%q+D declared here, later in the "
15117 "translation unit", fn);
15119 function = unq;
15122 if (identifier_p (function))
15124 if (complain & tf_error)
15125 unqualified_name_lookup_error (function);
15126 release_tree_vector (call_args);
15127 RETURN (error_mark_node);
15131 /* Remember that there was a reference to this entity. */
15132 if (DECL_P (function))
15133 mark_used (function, complain);
15135 /* Put back tf_decltype for the actual call. */
15136 complain |= decltype_flag;
15138 if (TREE_CODE (function) == OFFSET_REF)
15139 ret = build_offset_ref_call_from_tree (function, &call_args,
15140 complain);
15141 else if (TREE_CODE (function) == COMPONENT_REF)
15143 tree instance = TREE_OPERAND (function, 0);
15144 tree fn = TREE_OPERAND (function, 1);
15146 if (processing_template_decl
15147 && (type_dependent_expression_p (instance)
15148 || (!BASELINK_P (fn)
15149 && TREE_CODE (fn) != FIELD_DECL)
15150 || type_dependent_expression_p (fn)
15151 || any_type_dependent_arguments_p (call_args)))
15152 ret = build_nt_call_vec (function, call_args);
15153 else if (!BASELINK_P (fn))
15154 ret = finish_call_expr (function, &call_args,
15155 /*disallow_virtual=*/false,
15156 /*koenig_p=*/false,
15157 complain);
15158 else
15159 ret = (build_new_method_call
15160 (instance, fn,
15161 &call_args, NULL_TREE,
15162 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
15163 /*fn_p=*/NULL,
15164 complain));
15166 else
15167 ret = finish_call_expr (function, &call_args,
15168 /*disallow_virtual=*/qualified_p,
15169 koenig_p,
15170 complain);
15172 release_tree_vector (call_args);
15174 RETURN (ret);
15177 case COND_EXPR:
15179 tree cond = RECUR (TREE_OPERAND (t, 0));
15180 tree folded_cond = fold_non_dependent_expr (cond);
15181 tree exp1, exp2;
15183 if (TREE_CODE (folded_cond) == INTEGER_CST)
15185 if (integer_zerop (folded_cond))
15187 ++c_inhibit_evaluation_warnings;
15188 exp1 = RECUR (TREE_OPERAND (t, 1));
15189 --c_inhibit_evaluation_warnings;
15190 exp2 = RECUR (TREE_OPERAND (t, 2));
15192 else
15194 exp1 = RECUR (TREE_OPERAND (t, 1));
15195 ++c_inhibit_evaluation_warnings;
15196 exp2 = RECUR (TREE_OPERAND (t, 2));
15197 --c_inhibit_evaluation_warnings;
15199 cond = folded_cond;
15201 else
15203 exp1 = RECUR (TREE_OPERAND (t, 1));
15204 exp2 = RECUR (TREE_OPERAND (t, 2));
15207 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
15208 cond, exp1, exp2, complain));
15211 case PSEUDO_DTOR_EXPR:
15213 tree op0 = RECUR (TREE_OPERAND (t, 0));
15214 tree op1 = RECUR (TREE_OPERAND (t, 1));
15215 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
15216 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
15217 input_location));
15220 case TREE_LIST:
15222 tree purpose, value, chain;
15224 if (t == void_list_node)
15225 RETURN (t);
15227 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15228 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15230 /* We have pack expansions, so expand those and
15231 create a new list out of it. */
15232 tree purposevec = NULL_TREE;
15233 tree valuevec = NULL_TREE;
15234 tree chain;
15235 int i, len = -1;
15237 /* Expand the argument expressions. */
15238 if (TREE_PURPOSE (t))
15239 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15240 complain, in_decl);
15241 if (TREE_VALUE (t))
15242 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15243 complain, in_decl);
15245 /* Build the rest of the list. */
15246 chain = TREE_CHAIN (t);
15247 if (chain && chain != void_type_node)
15248 chain = RECUR (chain);
15250 /* Determine the number of arguments. */
15251 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15253 len = TREE_VEC_LENGTH (purposevec);
15254 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15256 else if (TREE_CODE (valuevec) == TREE_VEC)
15257 len = TREE_VEC_LENGTH (valuevec);
15258 else
15260 /* Since we only performed a partial substitution into
15261 the argument pack, we only RETURN (a single list
15262 node. */
15263 if (purposevec == TREE_PURPOSE (t)
15264 && valuevec == TREE_VALUE (t)
15265 && chain == TREE_CHAIN (t))
15266 RETURN (t);
15268 RETURN (tree_cons (purposevec, valuevec, chain));
15271 /* Convert the argument vectors into a TREE_LIST */
15272 i = len;
15273 while (i > 0)
15275 /* Grab the Ith values. */
15276 i--;
15277 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15278 : NULL_TREE;
15279 value
15280 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15281 : NULL_TREE;
15283 /* Build the list (backwards). */
15284 chain = tree_cons (purpose, value, chain);
15287 RETURN (chain);
15290 purpose = TREE_PURPOSE (t);
15291 if (purpose)
15292 purpose = RECUR (purpose);
15293 value = TREE_VALUE (t);
15294 if (value)
15295 value = RECUR (value);
15296 chain = TREE_CHAIN (t);
15297 if (chain && chain != void_type_node)
15298 chain = RECUR (chain);
15299 if (purpose == TREE_PURPOSE (t)
15300 && value == TREE_VALUE (t)
15301 && chain == TREE_CHAIN (t))
15302 RETURN (t);
15303 RETURN (tree_cons (purpose, value, chain));
15306 case COMPONENT_REF:
15308 tree object;
15309 tree object_type;
15310 tree member;
15311 tree r;
15313 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15314 args, complain, in_decl);
15315 /* Remember that there was a reference to this entity. */
15316 if (DECL_P (object))
15317 mark_used (object);
15318 object_type = TREE_TYPE (object);
15320 member = TREE_OPERAND (t, 1);
15321 if (BASELINK_P (member))
15322 member = tsubst_baselink (member,
15323 non_reference (TREE_TYPE (object)),
15324 args, complain, in_decl);
15325 else
15326 member = tsubst_copy (member, args, complain, in_decl);
15327 if (member == error_mark_node)
15328 RETURN (error_mark_node);
15330 if (type_dependent_expression_p (object))
15331 /* We can't do much here. */;
15332 else if (!CLASS_TYPE_P (object_type))
15334 if (scalarish_type_p (object_type))
15336 tree s = NULL_TREE;
15337 tree dtor = member;
15339 if (TREE_CODE (dtor) == SCOPE_REF)
15341 s = TREE_OPERAND (dtor, 0);
15342 dtor = TREE_OPERAND (dtor, 1);
15344 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15346 dtor = TREE_OPERAND (dtor, 0);
15347 if (TYPE_P (dtor))
15348 RETURN (finish_pseudo_destructor_expr
15349 (object, s, dtor, input_location));
15353 else if (TREE_CODE (member) == SCOPE_REF
15354 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15356 /* Lookup the template functions now that we know what the
15357 scope is. */
15358 tree scope = TREE_OPERAND (member, 0);
15359 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15360 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15361 member = lookup_qualified_name (scope, tmpl,
15362 /*is_type_p=*/false,
15363 /*complain=*/false);
15364 if (BASELINK_P (member))
15366 BASELINK_FUNCTIONS (member)
15367 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15368 args);
15369 member = (adjust_result_of_qualified_name_lookup
15370 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15371 object_type));
15373 else
15375 qualified_name_lookup_error (scope, tmpl, member,
15376 input_location);
15377 RETURN (error_mark_node);
15380 else if (TREE_CODE (member) == SCOPE_REF
15381 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15382 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15384 if (complain & tf_error)
15386 if (TYPE_P (TREE_OPERAND (member, 0)))
15387 error ("%qT is not a class or namespace",
15388 TREE_OPERAND (member, 0));
15389 else
15390 error ("%qD is not a class or namespace",
15391 TREE_OPERAND (member, 0));
15393 RETURN (error_mark_node);
15395 else if (TREE_CODE (member) == FIELD_DECL)
15397 r = finish_non_static_data_member (member, object, NULL_TREE);
15398 if (TREE_CODE (r) == COMPONENT_REF)
15399 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15400 RETURN (r);
15403 r = finish_class_member_access_expr (object, member,
15404 /*template_p=*/false,
15405 complain);
15406 if (TREE_CODE (r) == COMPONENT_REF)
15407 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15408 RETURN (r);
15411 case THROW_EXPR:
15412 RETURN (build_throw
15413 (RECUR (TREE_OPERAND (t, 0))));
15415 case CONSTRUCTOR:
15417 vec<constructor_elt, va_gc> *n;
15418 constructor_elt *ce;
15419 unsigned HOST_WIDE_INT idx;
15420 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15421 bool process_index_p;
15422 int newlen;
15423 bool need_copy_p = false;
15424 tree r;
15426 if (type == error_mark_node)
15427 RETURN (error_mark_node);
15429 /* digest_init will do the wrong thing if we let it. */
15430 if (type && TYPE_PTRMEMFUNC_P (type))
15431 RETURN (t);
15433 /* We do not want to process the index of aggregate
15434 initializers as they are identifier nodes which will be
15435 looked up by digest_init. */
15436 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15438 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15439 newlen = vec_safe_length (n);
15440 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15442 if (ce->index && process_index_p
15443 /* An identifier index is looked up in the type
15444 being initialized, not the current scope. */
15445 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15446 ce->index = RECUR (ce->index);
15448 if (PACK_EXPANSION_P (ce->value))
15450 /* Substitute into the pack expansion. */
15451 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15452 in_decl);
15454 if (ce->value == error_mark_node
15455 || PACK_EXPANSION_P (ce->value))
15457 else if (TREE_VEC_LENGTH (ce->value) == 1)
15458 /* Just move the argument into place. */
15459 ce->value = TREE_VEC_ELT (ce->value, 0);
15460 else
15462 /* Update the length of the final CONSTRUCTOR
15463 arguments vector, and note that we will need to
15464 copy.*/
15465 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15466 need_copy_p = true;
15469 else
15470 ce->value = RECUR (ce->value);
15473 if (need_copy_p)
15475 vec<constructor_elt, va_gc> *old_n = n;
15477 vec_alloc (n, newlen);
15478 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15480 if (TREE_CODE (ce->value) == TREE_VEC)
15482 int i, len = TREE_VEC_LENGTH (ce->value);
15483 for (i = 0; i < len; ++i)
15484 CONSTRUCTOR_APPEND_ELT (n, 0,
15485 TREE_VEC_ELT (ce->value, i));
15487 else
15488 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15492 r = build_constructor (init_list_type_node, n);
15493 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15495 if (TREE_HAS_CONSTRUCTOR (t))
15496 RETURN (finish_compound_literal (type, r, complain));
15498 TREE_TYPE (r) = type;
15499 RETURN (r);
15502 case TYPEID_EXPR:
15504 tree operand_0 = TREE_OPERAND (t, 0);
15505 if (TYPE_P (operand_0))
15507 operand_0 = tsubst (operand_0, args, complain, in_decl);
15508 RETURN (get_typeid (operand_0, complain));
15510 else
15512 operand_0 = RECUR (operand_0);
15513 RETURN (build_typeid (operand_0, complain));
15517 case VAR_DECL:
15518 if (!args)
15519 RETURN (t);
15520 else if (DECL_PACK_P (t))
15522 /* We don't build decls for an instantiation of a
15523 variadic capture proxy, we instantiate the elements
15524 when needed. */
15525 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15526 return RECUR (DECL_VALUE_EXPR (t));
15528 /* Fall through */
15530 case PARM_DECL:
15532 tree r = tsubst_copy (t, args, complain, in_decl);
15533 /* ??? We're doing a subset of finish_id_expression here. */
15534 if (VAR_P (r)
15535 && !processing_template_decl
15536 && !cp_unevaluated_operand
15537 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
15538 && DECL_THREAD_LOCAL_P (r))
15540 if (tree wrap = get_tls_wrapper_fn (r))
15541 /* Replace an evaluated use of the thread_local variable with
15542 a call to its wrapper. */
15543 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
15545 else if (outer_automatic_var_p (r))
15546 r = process_outer_var_ref (r, complain);
15548 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15549 /* If the original type was a reference, we'll be wrapped in
15550 the appropriate INDIRECT_REF. */
15551 r = convert_from_reference (r);
15552 RETURN (r);
15555 case VA_ARG_EXPR:
15557 tree op0 = RECUR (TREE_OPERAND (t, 0));
15558 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15559 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
15562 case OFFSETOF_EXPR:
15563 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
15564 EXPR_LOCATION (t)));
15566 case TRAIT_EXPR:
15568 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15569 complain, in_decl);
15571 tree type2 = TRAIT_EXPR_TYPE2 (t);
15572 if (type2 && TREE_CODE (type2) == TREE_LIST)
15573 type2 = RECUR (type2);
15574 else if (type2)
15575 type2 = tsubst (type2, args, complain, in_decl);
15577 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15580 case STMT_EXPR:
15582 tree old_stmt_expr = cur_stmt_expr;
15583 tree stmt_expr = begin_stmt_expr ();
15585 cur_stmt_expr = stmt_expr;
15586 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15587 integral_constant_expression_p);
15588 stmt_expr = finish_stmt_expr (stmt_expr, false);
15589 cur_stmt_expr = old_stmt_expr;
15591 /* If the resulting list of expression statement is empty,
15592 fold it further into void_node. */
15593 if (empty_expr_stmt_p (stmt_expr))
15594 stmt_expr = void_node;
15596 RETURN (stmt_expr);
15599 case LAMBDA_EXPR:
15601 tree r = build_lambda_expr ();
15603 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15604 LAMBDA_EXPR_CLOSURE (r) = type;
15605 CLASSTYPE_LAMBDA_EXPR (type) = r;
15607 LAMBDA_EXPR_LOCATION (r)
15608 = LAMBDA_EXPR_LOCATION (t);
15609 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15610 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15611 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15612 LAMBDA_EXPR_DISCRIMINATOR (r)
15613 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15614 /* For a function scope, we want to use tsubst so that we don't
15615 complain about referring to an auto function before its return
15616 type has been deduced. Otherwise, we want to use tsubst_copy so
15617 that we look up the existing field/parameter/variable rather
15618 than build a new one. */
15619 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15620 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15621 scope = tsubst (scope, args, complain, in_decl);
15622 else if (scope && TREE_CODE (scope) == PARM_DECL)
15624 /* Look up the parameter we want directly, as tsubst_copy
15625 doesn't do what we need. */
15626 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15627 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15628 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15629 parm = DECL_CHAIN (parm);
15630 scope = parm;
15631 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15632 if (DECL_CONTEXT (scope) == NULL_TREE)
15633 DECL_CONTEXT (scope) = fn;
15635 else
15636 scope = RECUR (scope);
15637 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15638 LAMBDA_EXPR_RETURN_TYPE (r)
15639 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15641 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15642 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15644 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15645 determine_visibility (TYPE_NAME (type));
15646 /* Now that we know visibility, instantiate the type so we have a
15647 declaration of the op() for later calls to lambda_function. */
15648 complete_type (type);
15650 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15652 RETURN (build_lambda_object (r));
15655 case TARGET_EXPR:
15656 /* We can get here for a constant initializer of non-dependent type.
15657 FIXME stop folding in cp_parser_initializer_clause. */
15659 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15660 complain);
15661 RETURN (r);
15664 case TRANSACTION_EXPR:
15665 RETURN (tsubst_expr(t, args, complain, in_decl,
15666 integral_constant_expression_p));
15668 case PAREN_EXPR:
15669 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15671 case VEC_PERM_EXPR:
15673 tree op0 = RECUR (TREE_OPERAND (t, 0));
15674 tree op1 = RECUR (TREE_OPERAND (t, 1));
15675 tree op2 = RECUR (TREE_OPERAND (t, 2));
15676 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
15677 complain));
15680 default:
15681 /* Handle Objective-C++ constructs, if appropriate. */
15683 tree subst
15684 = objcp_tsubst_copy_and_build (t, args, complain,
15685 in_decl, /*function_p=*/false);
15686 if (subst)
15687 RETURN (subst);
15689 RETURN (tsubst_copy (t, args, complain, in_decl));
15692 #undef RECUR
15693 #undef RETURN
15694 out:
15695 input_location = loc;
15696 return retval;
15699 /* Verify that the instantiated ARGS are valid. For type arguments,
15700 make sure that the type's linkage is ok. For non-type arguments,
15701 make sure they are constants if they are integral or enumerations.
15702 Emit an error under control of COMPLAIN, and return TRUE on error. */
15704 static bool
15705 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15707 if (dependent_template_arg_p (t))
15708 return false;
15709 if (ARGUMENT_PACK_P (t))
15711 tree vec = ARGUMENT_PACK_ARGS (t);
15712 int len = TREE_VEC_LENGTH (vec);
15713 bool result = false;
15714 int i;
15716 for (i = 0; i < len; ++i)
15717 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15718 result = true;
15719 return result;
15721 else if (TYPE_P (t))
15723 /* [basic.link]: A name with no linkage (notably, the name
15724 of a class or enumeration declared in a local scope)
15725 shall not be used to declare an entity with linkage.
15726 This implies that names with no linkage cannot be used as
15727 template arguments
15729 DR 757 relaxes this restriction for C++0x. */
15730 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15731 : no_linkage_check (t, /*relaxed_p=*/false));
15733 if (nt)
15735 /* DR 488 makes use of a type with no linkage cause
15736 type deduction to fail. */
15737 if (complain & tf_error)
15739 if (TYPE_ANONYMOUS_P (nt))
15740 error ("%qT is/uses anonymous type", t);
15741 else
15742 error ("template argument for %qD uses local type %qT",
15743 tmpl, t);
15745 return true;
15747 /* In order to avoid all sorts of complications, we do not
15748 allow variably-modified types as template arguments. */
15749 else if (variably_modified_type_p (t, NULL_TREE))
15751 if (complain & tf_error)
15752 error ("%qT is a variably modified type", t);
15753 return true;
15756 /* Class template and alias template arguments should be OK. */
15757 else if (DECL_TYPE_TEMPLATE_P (t))
15759 /* A non-type argument of integral or enumerated type must be a
15760 constant. */
15761 else if (TREE_TYPE (t)
15762 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15763 && !REFERENCE_REF_P (t)
15764 && !TREE_CONSTANT (t))
15766 if (complain & tf_error)
15767 error ("integral expression %qE is not constant", t);
15768 return true;
15770 return false;
15773 static bool
15774 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15776 int ix, len = DECL_NTPARMS (tmpl);
15777 bool result = false;
15779 for (ix = 0; ix != len; ix++)
15781 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15782 result = true;
15784 if (result && (complain & tf_error))
15785 error (" trying to instantiate %qD", tmpl);
15786 return result;
15789 /* We're out of SFINAE context now, so generate diagnostics for the access
15790 errors we saw earlier when instantiating D from TMPL and ARGS. */
15792 static void
15793 recheck_decl_substitution (tree d, tree tmpl, tree args)
15795 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15796 tree type = TREE_TYPE (pattern);
15797 location_t loc = input_location;
15799 push_access_scope (d);
15800 push_deferring_access_checks (dk_no_deferred);
15801 input_location = DECL_SOURCE_LOCATION (pattern);
15802 tsubst (type, args, tf_warning_or_error, d);
15803 input_location = loc;
15804 pop_deferring_access_checks ();
15805 pop_access_scope (d);
15808 /* Instantiate the indicated variable, function, or alias template TMPL with
15809 the template arguments in TARG_PTR. */
15811 static tree
15812 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15814 tree targ_ptr = orig_args;
15815 tree fndecl;
15816 tree gen_tmpl;
15817 tree spec;
15818 bool access_ok = true;
15820 if (tmpl == error_mark_node)
15821 return error_mark_node;
15823 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15825 /* If this function is a clone, handle it specially. */
15826 if (DECL_CLONED_FUNCTION_P (tmpl))
15828 tree spec;
15829 tree clone;
15831 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15832 DECL_CLONED_FUNCTION. */
15833 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15834 targ_ptr, complain);
15835 if (spec == error_mark_node)
15836 return error_mark_node;
15838 /* Look for the clone. */
15839 FOR_EACH_CLONE (clone, spec)
15840 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15841 return clone;
15842 /* We should always have found the clone by now. */
15843 gcc_unreachable ();
15844 return NULL_TREE;
15847 if (targ_ptr == error_mark_node)
15848 return error_mark_node;
15850 /* Check to see if we already have this specialization. */
15851 gen_tmpl = most_general_template (tmpl);
15852 if (tmpl != gen_tmpl)
15853 /* The TMPL is a partial instantiation. To get a full set of
15854 arguments we must add the arguments used to perform the
15855 partial instantiation. */
15856 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15857 targ_ptr);
15859 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15860 but it doesn't seem to be on the hot path. */
15861 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15863 gcc_assert (tmpl == gen_tmpl
15864 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15865 == spec)
15866 || fndecl == NULL_TREE);
15868 if (spec != NULL_TREE)
15870 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15872 if (complain & tf_error)
15873 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15874 return error_mark_node;
15876 return spec;
15879 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15880 complain))
15881 return error_mark_node;
15883 /* We are building a FUNCTION_DECL, during which the access of its
15884 parameters and return types have to be checked. However this
15885 FUNCTION_DECL which is the desired context for access checking
15886 is not built yet. We solve this chicken-and-egg problem by
15887 deferring all checks until we have the FUNCTION_DECL. */
15888 push_deferring_access_checks (dk_deferred);
15890 /* Instantiation of the function happens in the context of the function
15891 template, not the context of the overload resolution we're doing. */
15892 push_to_top_level ();
15893 /* If there are dependent arguments, e.g. because we're doing partial
15894 ordering, make sure processing_template_decl stays set. */
15895 if (uses_template_parms (targ_ptr))
15896 ++processing_template_decl;
15897 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15899 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
15900 complain, gen_tmpl, true);
15901 push_nested_class (ctx);
15904 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
15906 if (VAR_P (pattern))
15908 /* We need to determine if we're using a partial or explicit
15909 specialization now, because the type of the variable could be
15910 different. */
15911 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
15912 tree elt = most_specialized_partial_spec (tid, complain);
15913 if (elt == error_mark_node)
15914 pattern = error_mark_node;
15915 else if (elt)
15917 tmpl = TREE_VALUE (elt);
15918 pattern = DECL_TEMPLATE_RESULT (tmpl);
15919 targ_ptr = TREE_PURPOSE (elt);
15923 /* Substitute template parameters to obtain the specialization. */
15924 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
15925 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15926 pop_nested_class ();
15927 pop_from_top_level ();
15929 if (fndecl == error_mark_node)
15931 pop_deferring_access_checks ();
15932 return error_mark_node;
15935 /* The DECL_TI_TEMPLATE should always be the immediate parent
15936 template, not the most general template. */
15937 DECL_TI_TEMPLATE (fndecl) = tmpl;
15939 /* Now we know the specialization, compute access previously
15940 deferred. */
15941 push_access_scope (fndecl);
15942 if (!perform_deferred_access_checks (complain))
15943 access_ok = false;
15944 pop_access_scope (fndecl);
15945 pop_deferring_access_checks ();
15947 /* If we've just instantiated the main entry point for a function,
15948 instantiate all the alternate entry points as well. We do this
15949 by cloning the instantiation of the main entry point, not by
15950 instantiating the template clones. */
15951 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15952 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15954 if (!access_ok)
15956 if (!(complain & tf_error))
15958 /* Remember to reinstantiate when we're out of SFINAE so the user
15959 can see the errors. */
15960 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15962 return error_mark_node;
15964 return fndecl;
15967 /* Wrapper for instantiate_template_1. */
15969 tree
15970 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15972 tree ret;
15973 timevar_push (TV_TEMPLATE_INST);
15974 ret = instantiate_template_1 (tmpl, orig_args, complain);
15975 timevar_pop (TV_TEMPLATE_INST);
15976 return ret;
15979 /* Instantiate the alias template TMPL with ARGS. Also push a template
15980 instantiation level, which instantiate_template doesn't do because
15981 functions and variables have sufficient context established by the
15982 callers. */
15984 static tree
15985 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15987 struct pending_template *old_last_pend = last_pending_template;
15988 struct tinst_level *old_error_tinst = last_error_tinst_level;
15989 if (tmpl == error_mark_node || args == error_mark_node)
15990 return error_mark_node;
15991 tree tinst = build_tree_list (tmpl, args);
15992 if (!push_tinst_level (tinst))
15994 ggc_free (tinst);
15995 return error_mark_node;
15998 args =
15999 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
16000 args, tmpl, complain,
16001 /*require_all_args=*/true,
16002 /*use_default_args=*/true);
16004 tree r = instantiate_template (tmpl, args, complain);
16005 pop_tinst_level ();
16006 /* We can't free this if a pending_template entry or last_error_tinst_level
16007 is pointing at it. */
16008 if (last_pending_template == old_last_pend
16009 && last_error_tinst_level == old_error_tinst)
16010 ggc_free (tinst);
16012 return r;
16015 /* PARM is a template parameter pack for FN. Returns true iff
16016 PARM is used in a deducible way in the argument list of FN. */
16018 static bool
16019 pack_deducible_p (tree parm, tree fn)
16021 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
16022 for (; t; t = TREE_CHAIN (t))
16024 tree type = TREE_VALUE (t);
16025 tree packs;
16026 if (!PACK_EXPANSION_P (type))
16027 continue;
16028 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16029 packs; packs = TREE_CHAIN (packs))
16030 if (template_args_equal (TREE_VALUE (packs), parm))
16032 /* The template parameter pack is used in a function parameter
16033 pack. If this is the end of the parameter list, the
16034 template parameter pack is deducible. */
16035 if (TREE_CHAIN (t) == void_list_node)
16036 return true;
16037 else
16038 /* Otherwise, not. Well, it could be deduced from
16039 a non-pack parameter, but doing so would end up with
16040 a deduction mismatch, so don't bother. */
16041 return false;
16044 /* The template parameter pack isn't used in any function parameter
16045 packs, but it might be used deeper, e.g. tuple<Args...>. */
16046 return true;
16049 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16050 NARGS elements of the arguments that are being used when calling
16051 it. TARGS is a vector into which the deduced template arguments
16052 are placed.
16054 Returns either a FUNCTION_DECL for the matching specialization of FN or
16055 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16056 true, diagnostics will be printed to explain why it failed.
16058 If FN is a conversion operator, or we are trying to produce a specific
16059 specialization, RETURN_TYPE is the return type desired.
16061 The EXPLICIT_TARGS are explicit template arguments provided via a
16062 template-id.
16064 The parameter STRICT is one of:
16066 DEDUCE_CALL:
16067 We are deducing arguments for a function call, as in
16068 [temp.deduct.call].
16070 DEDUCE_CONV:
16071 We are deducing arguments for a conversion function, as in
16072 [temp.deduct.conv].
16074 DEDUCE_EXACT:
16075 We are deducing arguments when doing an explicit instantiation
16076 as in [temp.explicit], when determining an explicit specialization
16077 as in [temp.expl.spec], or when taking the address of a function
16078 template, as in [temp.deduct.funcaddr]. */
16080 tree
16081 fn_type_unification (tree fn,
16082 tree explicit_targs,
16083 tree targs,
16084 const tree *args,
16085 unsigned int nargs,
16086 tree return_type,
16087 unification_kind_t strict,
16088 int flags,
16089 bool explain_p,
16090 bool decltype_p)
16092 tree parms;
16093 tree fntype;
16094 tree decl = NULL_TREE;
16095 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
16096 bool ok;
16097 static int deduction_depth;
16098 struct pending_template *old_last_pend = last_pending_template;
16099 struct tinst_level *old_error_tinst = last_error_tinst_level;
16100 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
16101 tree tinst;
16102 tree r = error_mark_node;
16104 if (decltype_p)
16105 complain |= tf_decltype;
16107 /* In C++0x, it's possible to have a function template whose type depends
16108 on itself recursively. This is most obvious with decltype, but can also
16109 occur with enumeration scope (c++/48969). So we need to catch infinite
16110 recursion and reject the substitution at deduction time; this function
16111 will return error_mark_node for any repeated substitution.
16113 This also catches excessive recursion such as when f<N> depends on
16114 f<N-1> across all integers, and returns error_mark_node for all the
16115 substitutions back up to the initial one.
16117 This is, of course, not reentrant. */
16118 if (excessive_deduction_depth)
16119 return error_mark_node;
16120 tinst = build_tree_list (fn, NULL_TREE);
16121 ++deduction_depth;
16123 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
16125 fntype = TREE_TYPE (fn);
16126 if (explicit_targs)
16128 /* [temp.deduct]
16130 The specified template arguments must match the template
16131 parameters in kind (i.e., type, nontype, template), and there
16132 must not be more arguments than there are parameters;
16133 otherwise type deduction fails.
16135 Nontype arguments must match the types of the corresponding
16136 nontype template parameters, or must be convertible to the
16137 types of the corresponding nontype parameters as specified in
16138 _temp.arg.nontype_, otherwise type deduction fails.
16140 All references in the function type of the function template
16141 to the corresponding template parameters are replaced by the
16142 specified template argument values. If a substitution in a
16143 template parameter or in the function type of the function
16144 template results in an invalid type, type deduction fails. */
16145 int i, len = TREE_VEC_LENGTH (tparms);
16146 location_t loc = input_location;
16147 bool incomplete = false;
16149 /* Adjust any explicit template arguments before entering the
16150 substitution context. */
16151 explicit_targs
16152 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
16153 complain,
16154 /*require_all_args=*/false,
16155 /*use_default_args=*/false));
16156 if (explicit_targs == error_mark_node)
16157 goto fail;
16159 /* Substitute the explicit args into the function type. This is
16160 necessary so that, for instance, explicitly declared function
16161 arguments can match null pointed constants. If we were given
16162 an incomplete set of explicit args, we must not do semantic
16163 processing during substitution as we could create partial
16164 instantiations. */
16165 for (i = 0; i < len; i++)
16167 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16168 bool parameter_pack = false;
16169 tree targ = TREE_VEC_ELT (explicit_targs, i);
16171 /* Dig out the actual parm. */
16172 if (TREE_CODE (parm) == TYPE_DECL
16173 || TREE_CODE (parm) == TEMPLATE_DECL)
16175 parm = TREE_TYPE (parm);
16176 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
16178 else if (TREE_CODE (parm) == PARM_DECL)
16180 parm = DECL_INITIAL (parm);
16181 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
16184 if (!parameter_pack && targ == NULL_TREE)
16185 /* No explicit argument for this template parameter. */
16186 incomplete = true;
16188 if (parameter_pack && pack_deducible_p (parm, fn))
16190 /* Mark the argument pack as "incomplete". We could
16191 still deduce more arguments during unification.
16192 We remove this mark in type_unification_real. */
16193 if (targ)
16195 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
16196 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
16197 = ARGUMENT_PACK_ARGS (targ);
16200 /* We have some incomplete argument packs. */
16201 incomplete = true;
16205 TREE_VALUE (tinst) = explicit_targs;
16206 if (!push_tinst_level (tinst))
16208 excessive_deduction_depth = true;
16209 goto fail;
16211 processing_template_decl += incomplete;
16212 input_location = DECL_SOURCE_LOCATION (fn);
16213 /* Ignore any access checks; we'll see them again in
16214 instantiate_template and they might have the wrong
16215 access path at this point. */
16216 push_deferring_access_checks (dk_deferred);
16217 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
16218 complain | tf_partial, NULL_TREE);
16219 pop_deferring_access_checks ();
16220 input_location = loc;
16221 processing_template_decl -= incomplete;
16222 pop_tinst_level ();
16224 if (fntype == error_mark_node)
16225 goto fail;
16227 /* Place the explicitly specified arguments in TARGS. */
16228 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
16229 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
16232 /* Never do unification on the 'this' parameter. */
16233 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
16235 if (return_type)
16237 tree *new_args;
16239 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
16240 new_args = XALLOCAVEC (tree, nargs + 1);
16241 new_args[0] = return_type;
16242 memcpy (new_args + 1, args, nargs * sizeof (tree));
16243 args = new_args;
16244 ++nargs;
16247 /* We allow incomplete unification without an error message here
16248 because the standard doesn't seem to explicitly prohibit it. Our
16249 callers must be ready to deal with unification failures in any
16250 event. */
16252 TREE_VALUE (tinst) = targs;
16253 /* If we aren't explaining yet, push tinst context so we can see where
16254 any errors (e.g. from class instantiations triggered by instantiation
16255 of default template arguments) come from. If we are explaining, this
16256 context is redundant. */
16257 if (!explain_p && !push_tinst_level (tinst))
16259 excessive_deduction_depth = true;
16260 goto fail;
16263 /* type_unification_real will pass back any access checks from default
16264 template argument substitution. */
16265 vec<deferred_access_check, va_gc> *checks;
16266 checks = NULL;
16268 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16269 targs, parms, args, nargs, /*subr=*/0,
16270 strict, flags, &checks, explain_p);
16271 if (!explain_p)
16272 pop_tinst_level ();
16273 if (!ok)
16274 goto fail;
16276 /* Now that we have bindings for all of the template arguments,
16277 ensure that the arguments deduced for the template template
16278 parameters have compatible template parameter lists. We cannot
16279 check this property before we have deduced all template
16280 arguments, because the template parameter types of a template
16281 template parameter might depend on prior template parameters
16282 deduced after the template template parameter. The following
16283 ill-formed example illustrates this issue:
16285 template<typename T, template<T> class C> void f(C<5>, T);
16287 template<int N> struct X {};
16289 void g() {
16290 f(X<5>(), 5l); // error: template argument deduction fails
16293 The template parameter list of 'C' depends on the template type
16294 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16295 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16296 time that we deduce 'C'. */
16297 if (!template_template_parm_bindings_ok_p
16298 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16300 unify_inconsistent_template_template_parameters (explain_p);
16301 goto fail;
16304 /* All is well so far. Now, check:
16306 [temp.deduct]
16308 When all template arguments have been deduced, all uses of
16309 template parameters in nondeduced contexts are replaced with
16310 the corresponding deduced argument values. If the
16311 substitution results in an invalid type, as described above,
16312 type deduction fails. */
16313 TREE_VALUE (tinst) = targs;
16314 if (!push_tinst_level (tinst))
16316 excessive_deduction_depth = true;
16317 goto fail;
16320 /* Also collect access checks from the instantiation. */
16321 reopen_deferring_access_checks (checks);
16323 decl = instantiate_template (fn, targs, complain);
16325 checks = get_deferred_access_checks ();
16326 pop_deferring_access_checks ();
16328 pop_tinst_level ();
16330 if (decl == error_mark_node)
16331 goto fail;
16333 /* Now perform any access checks encountered during substitution. */
16334 push_access_scope (decl);
16335 ok = perform_access_checks (checks, complain);
16336 pop_access_scope (decl);
16337 if (!ok)
16338 goto fail;
16340 /* If we're looking for an exact match, check that what we got
16341 is indeed an exact match. It might not be if some template
16342 parameters are used in non-deduced contexts. But don't check
16343 for an exact match if we have dependent template arguments;
16344 in that case we're doing partial ordering, and we already know
16345 that we have two candidates that will provide the actual type. */
16346 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16348 tree substed = TREE_TYPE (decl);
16349 unsigned int i;
16351 tree sarg
16352 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16353 if (return_type)
16354 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16355 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16356 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16358 unify_type_mismatch (explain_p, args[i],
16359 TREE_VALUE (sarg));
16360 goto fail;
16364 r = decl;
16366 fail:
16367 --deduction_depth;
16368 if (excessive_deduction_depth)
16370 if (deduction_depth == 0)
16371 /* Reset once we're all the way out. */
16372 excessive_deduction_depth = false;
16375 /* We can't free this if a pending_template entry or last_error_tinst_level
16376 is pointing at it. */
16377 if (last_pending_template == old_last_pend
16378 && last_error_tinst_level == old_error_tinst)
16379 ggc_free (tinst);
16381 return r;
16384 /* Adjust types before performing type deduction, as described in
16385 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16386 sections are symmetric. PARM is the type of a function parameter
16387 or the return type of the conversion function. ARG is the type of
16388 the argument passed to the call, or the type of the value
16389 initialized with the result of the conversion function.
16390 ARG_EXPR is the original argument expression, which may be null. */
16392 static int
16393 maybe_adjust_types_for_deduction (unification_kind_t strict,
16394 tree* parm,
16395 tree* arg,
16396 tree arg_expr)
16398 int result = 0;
16400 switch (strict)
16402 case DEDUCE_CALL:
16403 break;
16405 case DEDUCE_CONV:
16407 /* Swap PARM and ARG throughout the remainder of this
16408 function; the handling is precisely symmetric since PARM
16409 will initialize ARG rather than vice versa. */
16410 tree* temp = parm;
16411 parm = arg;
16412 arg = temp;
16413 break;
16416 case DEDUCE_EXACT:
16417 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16418 too, but here handle it by stripping the reference from PARM
16419 rather than by adding it to ARG. */
16420 if (TREE_CODE (*parm) == REFERENCE_TYPE
16421 && TYPE_REF_IS_RVALUE (*parm)
16422 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16423 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16424 && TREE_CODE (*arg) == REFERENCE_TYPE
16425 && !TYPE_REF_IS_RVALUE (*arg))
16426 *parm = TREE_TYPE (*parm);
16427 /* Nothing else to do in this case. */
16428 return 0;
16430 default:
16431 gcc_unreachable ();
16434 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16436 /* [temp.deduct.call]
16438 If P is not a reference type:
16440 --If A is an array type, the pointer type produced by the
16441 array-to-pointer standard conversion (_conv.array_) is
16442 used in place of A for type deduction; otherwise,
16444 --If A is a function type, the pointer type produced by
16445 the function-to-pointer standard conversion
16446 (_conv.func_) is used in place of A for type deduction;
16447 otherwise,
16449 --If A is a cv-qualified type, the top level
16450 cv-qualifiers of A's type are ignored for type
16451 deduction. */
16452 if (TREE_CODE (*arg) == ARRAY_TYPE)
16453 *arg = build_pointer_type (TREE_TYPE (*arg));
16454 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16455 *arg = build_pointer_type (*arg);
16456 else
16457 *arg = TYPE_MAIN_VARIANT (*arg);
16460 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16461 of the form T&&, where T is a template parameter, and the argument
16462 is an lvalue, T is deduced as A& */
16463 if (TREE_CODE (*parm) == REFERENCE_TYPE
16464 && TYPE_REF_IS_RVALUE (*parm)
16465 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16466 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16467 && (arg_expr ? real_lvalue_p (arg_expr)
16468 /* try_one_overload doesn't provide an arg_expr, but
16469 functions are always lvalues. */
16470 : TREE_CODE (*arg) == FUNCTION_TYPE))
16471 *arg = build_reference_type (*arg);
16473 /* [temp.deduct.call]
16475 If P is a cv-qualified type, the top level cv-qualifiers
16476 of P's type are ignored for type deduction. If P is a
16477 reference type, the type referred to by P is used for
16478 type deduction. */
16479 *parm = TYPE_MAIN_VARIANT (*parm);
16480 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16482 *parm = TREE_TYPE (*parm);
16483 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16486 /* DR 322. For conversion deduction, remove a reference type on parm
16487 too (which has been swapped into ARG). */
16488 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16489 *arg = TREE_TYPE (*arg);
16491 return result;
16494 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16495 template which does contain any deducible template parameters; check if
16496 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16497 unify_one_argument. */
16499 static int
16500 check_non_deducible_conversion (tree parm, tree arg, int strict,
16501 int flags, bool explain_p)
16503 tree type;
16505 if (!TYPE_P (arg))
16506 type = TREE_TYPE (arg);
16507 else
16508 type = arg;
16510 if (same_type_p (parm, type))
16511 return unify_success (explain_p);
16513 if (strict == DEDUCE_CONV)
16515 if (can_convert_arg (type, parm, NULL_TREE, flags,
16516 explain_p ? tf_warning_or_error : tf_none))
16517 return unify_success (explain_p);
16519 else if (strict != DEDUCE_EXACT)
16521 if (can_convert_arg (parm, type,
16522 TYPE_P (arg) ? NULL_TREE : arg,
16523 flags, explain_p ? tf_warning_or_error : tf_none))
16524 return unify_success (explain_p);
16527 if (strict == DEDUCE_EXACT)
16528 return unify_type_mismatch (explain_p, parm, arg);
16529 else
16530 return unify_arg_conversion (explain_p, parm, type, arg);
16533 static bool uses_deducible_template_parms (tree type);
16535 /* Returns true iff the expression EXPR is one from which a template
16536 argument can be deduced. In other words, if it's an undecorated
16537 use of a template non-type parameter. */
16539 static bool
16540 deducible_expression (tree expr)
16542 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16545 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16546 deducible way; that is, if it has a max value of <PARM> - 1. */
16548 static bool
16549 deducible_array_bound (tree domain)
16551 if (domain == NULL_TREE)
16552 return false;
16554 tree max = TYPE_MAX_VALUE (domain);
16555 if (TREE_CODE (max) != MINUS_EXPR)
16556 return false;
16558 return deducible_expression (TREE_OPERAND (max, 0));
16561 /* Returns true iff the template arguments ARGS use a template parameter
16562 in a deducible way. */
16564 static bool
16565 deducible_template_args (tree args)
16567 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16569 bool deducible;
16570 tree elt = TREE_VEC_ELT (args, i);
16571 if (ARGUMENT_PACK_P (elt))
16572 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16573 else
16575 if (PACK_EXPANSION_P (elt))
16576 elt = PACK_EXPANSION_PATTERN (elt);
16577 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16578 deducible = true;
16579 else if (TYPE_P (elt))
16580 deducible = uses_deducible_template_parms (elt);
16581 else
16582 deducible = deducible_expression (elt);
16584 if (deducible)
16585 return true;
16587 return false;
16590 /* Returns true iff TYPE contains any deducible references to template
16591 parameters, as per 14.8.2.5. */
16593 static bool
16594 uses_deducible_template_parms (tree type)
16596 if (PACK_EXPANSION_P (type))
16597 type = PACK_EXPANSION_PATTERN (type);
16599 /* T
16600 cv-list T
16601 TT<T>
16602 TT<i>
16603 TT<> */
16604 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16605 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16606 return true;
16608 /* T*
16610 T&& */
16611 if (POINTER_TYPE_P (type))
16612 return uses_deducible_template_parms (TREE_TYPE (type));
16614 /* T[integer-constant ]
16615 type [i] */
16616 if (TREE_CODE (type) == ARRAY_TYPE)
16617 return (uses_deducible_template_parms (TREE_TYPE (type))
16618 || deducible_array_bound (TYPE_DOMAIN (type)));
16620 /* T type ::*
16621 type T::*
16622 T T::*
16623 T (type ::*)()
16624 type (T::*)()
16625 type (type ::*)(T)
16626 type (T::*)(T)
16627 T (type ::*)(T)
16628 T (T::*)()
16629 T (T::*)(T) */
16630 if (TYPE_PTRMEM_P (type))
16631 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16632 || (uses_deducible_template_parms
16633 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16635 /* template-name <T> (where template-name refers to a class template)
16636 template-name <i> (where template-name refers to a class template) */
16637 if (CLASS_TYPE_P (type)
16638 && CLASSTYPE_TEMPLATE_INFO (type)
16639 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16640 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16641 (CLASSTYPE_TI_ARGS (type)));
16643 /* type (T)
16645 T(T) */
16646 if (TREE_CODE (type) == FUNCTION_TYPE
16647 || TREE_CODE (type) == METHOD_TYPE)
16649 if (uses_deducible_template_parms (TREE_TYPE (type)))
16650 return true;
16651 tree parm = TYPE_ARG_TYPES (type);
16652 if (TREE_CODE (type) == METHOD_TYPE)
16653 parm = TREE_CHAIN (parm);
16654 for (; parm; parm = TREE_CHAIN (parm))
16655 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16656 return true;
16659 return false;
16662 /* Subroutine of type_unification_real and unify_pack_expansion to
16663 handle unification of a single P/A pair. Parameters are as
16664 for those functions. */
16666 static int
16667 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16668 int subr, unification_kind_t strict, int flags,
16669 bool explain_p)
16671 tree arg_expr = NULL_TREE;
16672 int arg_strict;
16674 if (arg == error_mark_node || parm == error_mark_node)
16675 return unify_invalid (explain_p);
16676 if (arg == unknown_type_node)
16677 /* We can't deduce anything from this, but we might get all the
16678 template args from other function args. */
16679 return unify_success (explain_p);
16681 /* Implicit conversions (Clause 4) will be performed on a function
16682 argument to convert it to the type of the corresponding function
16683 parameter if the parameter type contains no template-parameters that
16684 participate in template argument deduction. */
16685 if (TYPE_P (parm) && !uses_template_parms (parm))
16686 /* For function parameters that contain no template-parameters at all,
16687 we have historically checked for convertibility in order to shortcut
16688 consideration of this candidate. */
16689 return check_non_deducible_conversion (parm, arg, strict, flags,
16690 explain_p);
16691 else if (strict == DEDUCE_CALL
16692 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16693 /* For function parameters with only non-deducible template parameters,
16694 just return. */
16695 return unify_success (explain_p);
16697 switch (strict)
16699 case DEDUCE_CALL:
16700 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16701 | UNIFY_ALLOW_MORE_CV_QUAL
16702 | UNIFY_ALLOW_DERIVED);
16703 break;
16705 case DEDUCE_CONV:
16706 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16707 break;
16709 case DEDUCE_EXACT:
16710 arg_strict = UNIFY_ALLOW_NONE;
16711 break;
16713 default:
16714 gcc_unreachable ();
16717 /* We only do these transformations if this is the top-level
16718 parameter_type_list in a call or declaration matching; in other
16719 situations (nested function declarators, template argument lists) we
16720 won't be comparing a type to an expression, and we don't do any type
16721 adjustments. */
16722 if (!subr)
16724 if (!TYPE_P (arg))
16726 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16727 if (type_unknown_p (arg))
16729 /* [temp.deduct.type] A template-argument can be
16730 deduced from a pointer to function or pointer
16731 to member function argument if the set of
16732 overloaded functions does not contain function
16733 templates and at most one of a set of
16734 overloaded functions provides a unique
16735 match. */
16737 if (resolve_overloaded_unification
16738 (tparms, targs, parm, arg, strict,
16739 arg_strict, explain_p))
16740 return unify_success (explain_p);
16741 return unify_overload_resolution_failure (explain_p, arg);
16744 arg_expr = arg;
16745 arg = unlowered_expr_type (arg);
16746 if (arg == error_mark_node)
16747 return unify_invalid (explain_p);
16750 arg_strict |=
16751 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16753 else
16754 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16755 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
16756 return unify_template_argument_mismatch (explain_p, parm, arg);
16758 /* For deduction from an init-list we need the actual list. */
16759 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16760 arg = arg_expr;
16761 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16764 /* Most parms like fn_type_unification.
16766 If SUBR is 1, we're being called recursively (to unify the
16767 arguments of a function or method parameter of a function
16768 template).
16770 CHECKS is a pointer to a vector of access checks encountered while
16771 substituting default template arguments. */
16773 static int
16774 type_unification_real (tree tparms,
16775 tree targs,
16776 tree xparms,
16777 const tree *xargs,
16778 unsigned int xnargs,
16779 int subr,
16780 unification_kind_t strict,
16781 int flags,
16782 vec<deferred_access_check, va_gc> **checks,
16783 bool explain_p)
16785 tree parm, arg;
16786 int i;
16787 int ntparms = TREE_VEC_LENGTH (tparms);
16788 int saw_undeduced = 0;
16789 tree parms;
16790 const tree *args;
16791 unsigned int nargs;
16792 unsigned int ia;
16794 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16795 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16796 gcc_assert (ntparms > 0);
16798 /* Reset the number of non-defaulted template arguments contained
16799 in TARGS. */
16800 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16802 again:
16803 parms = xparms;
16804 args = xargs;
16805 nargs = xnargs;
16807 ia = 0;
16808 while (parms && parms != void_list_node
16809 && ia < nargs)
16811 parm = TREE_VALUE (parms);
16813 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16814 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16815 /* For a function parameter pack that occurs at the end of the
16816 parameter-declaration-list, the type A of each remaining
16817 argument of the call is compared with the type P of the
16818 declarator-id of the function parameter pack. */
16819 break;
16821 parms = TREE_CHAIN (parms);
16823 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16824 /* For a function parameter pack that does not occur at the
16825 end of the parameter-declaration-list, the type of the
16826 parameter pack is a non-deduced context. */
16827 continue;
16829 arg = args[ia];
16830 ++ia;
16832 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16833 flags, explain_p))
16834 return 1;
16837 if (parms
16838 && parms != void_list_node
16839 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16841 /* Unify the remaining arguments with the pack expansion type. */
16842 tree argvec;
16843 tree parmvec = make_tree_vec (1);
16845 /* Allocate a TREE_VEC and copy in all of the arguments */
16846 argvec = make_tree_vec (nargs - ia);
16847 for (i = 0; ia < nargs; ++ia, ++i)
16848 TREE_VEC_ELT (argvec, i) = args[ia];
16850 /* Copy the parameter into parmvec. */
16851 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16852 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16853 /*subr=*/subr, explain_p))
16854 return 1;
16856 /* Advance to the end of the list of parameters. */
16857 parms = TREE_CHAIN (parms);
16860 /* Fail if we've reached the end of the parm list, and more args
16861 are present, and the parm list isn't variadic. */
16862 if (ia < nargs && parms == void_list_node)
16863 return unify_too_many_arguments (explain_p, nargs, ia);
16864 /* Fail if parms are left and they don't have default values and
16865 they aren't all deduced as empty packs (c++/57397). This is
16866 consistent with sufficient_parms_p. */
16867 if (parms && parms != void_list_node
16868 && TREE_PURPOSE (parms) == NULL_TREE)
16870 unsigned int count = nargs;
16871 tree p = parms;
16872 bool type_pack_p;
16875 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
16876 if (!type_pack_p)
16877 count++;
16878 p = TREE_CHAIN (p);
16880 while (p && p != void_list_node);
16881 if (count != nargs)
16882 return unify_too_few_arguments (explain_p, ia, count,
16883 type_pack_p);
16886 if (!subr)
16888 tsubst_flags_t complain = (explain_p
16889 ? tf_warning_or_error
16890 : tf_none);
16892 for (i = 0; i < ntparms; i++)
16894 tree targ = TREE_VEC_ELT (targs, i);
16895 tree tparm = TREE_VEC_ELT (tparms, i);
16897 /* Clear the "incomplete" flags on all argument packs now so that
16898 substituting them into later default arguments works. */
16899 if (targ && ARGUMENT_PACK_P (targ))
16901 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16902 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16905 if (targ || tparm == error_mark_node)
16906 continue;
16907 tparm = TREE_VALUE (tparm);
16909 /* If this is an undeduced nontype parameter that depends on
16910 a type parameter, try another pass; its type may have been
16911 deduced from a later argument than the one from which
16912 this parameter can be deduced. */
16913 if (TREE_CODE (tparm) == PARM_DECL
16914 && uses_template_parms (TREE_TYPE (tparm))
16915 && !saw_undeduced++)
16916 goto again;
16918 /* Core issue #226 (C++0x) [temp.deduct]:
16920 If a template argument has not been deduced, its
16921 default template argument, if any, is used.
16923 When we are in C++98 mode, TREE_PURPOSE will either
16924 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16925 to explicitly check cxx_dialect here. */
16926 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16928 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16929 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16930 reopen_deferring_access_checks (*checks);
16931 location_t save_loc = input_location;
16932 if (DECL_P (parm))
16933 input_location = DECL_SOURCE_LOCATION (parm);
16934 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16935 arg = convert_template_argument (parm, arg, targs, complain,
16936 i, NULL_TREE);
16937 input_location = save_loc;
16938 *checks = get_deferred_access_checks ();
16939 pop_deferring_access_checks ();
16940 if (arg == error_mark_node)
16941 return 1;
16942 else
16944 TREE_VEC_ELT (targs, i) = arg;
16945 /* The position of the first default template argument,
16946 is also the number of non-defaulted arguments in TARGS.
16947 Record that. */
16948 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16949 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16950 continue;
16954 /* If the type parameter is a parameter pack, then it will
16955 be deduced to an empty parameter pack. */
16956 if (template_parameter_pack_p (tparm))
16958 tree arg;
16960 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16962 arg = make_node (NONTYPE_ARGUMENT_PACK);
16963 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16964 TREE_CONSTANT (arg) = 1;
16966 else
16967 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16969 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16971 TREE_VEC_ELT (targs, i) = arg;
16972 continue;
16975 return unify_parameter_deduction_failure (explain_p, tparm);
16978 #ifdef ENABLE_CHECKING
16979 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16980 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16981 #endif
16983 return unify_success (explain_p);
16986 /* Subroutine of type_unification_real. Args are like the variables
16987 at the call site. ARG is an overloaded function (or template-id);
16988 we try deducing template args from each of the overloads, and if
16989 only one succeeds, we go with that. Modifies TARGS and returns
16990 true on success. */
16992 static bool
16993 resolve_overloaded_unification (tree tparms,
16994 tree targs,
16995 tree parm,
16996 tree arg,
16997 unification_kind_t strict,
16998 int sub_strict,
16999 bool explain_p)
17001 tree tempargs = copy_node (targs);
17002 int good = 0;
17003 tree goodfn = NULL_TREE;
17004 bool addr_p;
17006 if (TREE_CODE (arg) == ADDR_EXPR)
17008 arg = TREE_OPERAND (arg, 0);
17009 addr_p = true;
17011 else
17012 addr_p = false;
17014 if (TREE_CODE (arg) == COMPONENT_REF)
17015 /* Handle `&x' where `x' is some static or non-static member
17016 function name. */
17017 arg = TREE_OPERAND (arg, 1);
17019 if (TREE_CODE (arg) == OFFSET_REF)
17020 arg = TREE_OPERAND (arg, 1);
17022 /* Strip baselink information. */
17023 if (BASELINK_P (arg))
17024 arg = BASELINK_FUNCTIONS (arg);
17026 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
17028 /* If we got some explicit template args, we need to plug them into
17029 the affected templates before we try to unify, in case the
17030 explicit args will completely resolve the templates in question. */
17032 int ok = 0;
17033 tree expl_subargs = TREE_OPERAND (arg, 1);
17034 arg = TREE_OPERAND (arg, 0);
17036 for (; arg; arg = OVL_NEXT (arg))
17038 tree fn = OVL_CURRENT (arg);
17039 tree subargs, elem;
17041 if (TREE_CODE (fn) != TEMPLATE_DECL)
17042 continue;
17044 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17045 expl_subargs, NULL_TREE, tf_none,
17046 /*require_all_args=*/true,
17047 /*use_default_args=*/true);
17048 if (subargs != error_mark_node
17049 && !any_dependent_template_arguments_p (subargs))
17051 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
17052 if (try_one_overload (tparms, targs, tempargs, parm,
17053 elem, strict, sub_strict, addr_p, explain_p)
17054 && (!goodfn || !same_type_p (goodfn, elem)))
17056 goodfn = elem;
17057 ++good;
17060 else if (subargs)
17061 ++ok;
17063 /* If no templates (or more than one) are fully resolved by the
17064 explicit arguments, this template-id is a non-deduced context; it
17065 could still be OK if we deduce all template arguments for the
17066 enclosing call through other arguments. */
17067 if (good != 1)
17068 good = ok;
17070 else if (TREE_CODE (arg) != OVERLOAD
17071 && TREE_CODE (arg) != FUNCTION_DECL)
17072 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17073 -- but the deduction does not succeed because the expression is
17074 not just the function on its own. */
17075 return false;
17076 else
17077 for (; arg; arg = OVL_NEXT (arg))
17078 if (try_one_overload (tparms, targs, tempargs, parm,
17079 TREE_TYPE (OVL_CURRENT (arg)),
17080 strict, sub_strict, addr_p, explain_p)
17081 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
17083 goodfn = OVL_CURRENT (arg);
17084 ++good;
17087 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17088 to function or pointer to member function argument if the set of
17089 overloaded functions does not contain function templates and at most
17090 one of a set of overloaded functions provides a unique match.
17092 So if we found multiple possibilities, we return success but don't
17093 deduce anything. */
17095 if (good == 1)
17097 int i = TREE_VEC_LENGTH (targs);
17098 for (; i--; )
17099 if (TREE_VEC_ELT (tempargs, i))
17101 tree old = TREE_VEC_ELT (targs, i);
17102 tree new_ = TREE_VEC_ELT (tempargs, i);
17103 if (new_ && old && ARGUMENT_PACK_P (old)
17104 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
17105 /* Don't forget explicit template arguments in a pack. */
17106 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
17107 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
17108 TREE_VEC_ELT (targs, i) = new_;
17111 if (good)
17112 return true;
17114 return false;
17117 /* Core DR 115: In contexts where deduction is done and fails, or in
17118 contexts where deduction is not done, if a template argument list is
17119 specified and it, along with any default template arguments, identifies
17120 a single function template specialization, then the template-id is an
17121 lvalue for the function template specialization. */
17123 tree
17124 resolve_nondeduced_context (tree orig_expr)
17126 tree expr, offset, baselink;
17127 bool addr;
17129 if (!type_unknown_p (orig_expr))
17130 return orig_expr;
17132 expr = orig_expr;
17133 addr = false;
17134 offset = NULL_TREE;
17135 baselink = NULL_TREE;
17137 if (TREE_CODE (expr) == ADDR_EXPR)
17139 expr = TREE_OPERAND (expr, 0);
17140 addr = true;
17142 if (TREE_CODE (expr) == OFFSET_REF)
17144 offset = expr;
17145 expr = TREE_OPERAND (expr, 1);
17147 if (BASELINK_P (expr))
17149 baselink = expr;
17150 expr = BASELINK_FUNCTIONS (expr);
17153 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
17155 int good = 0;
17156 tree goodfn = NULL_TREE;
17158 /* If we got some explicit template args, we need to plug them into
17159 the affected templates before we try to unify, in case the
17160 explicit args will completely resolve the templates in question. */
17162 tree expl_subargs = TREE_OPERAND (expr, 1);
17163 tree arg = TREE_OPERAND (expr, 0);
17164 tree badfn = NULL_TREE;
17165 tree badargs = NULL_TREE;
17167 for (; arg; arg = OVL_NEXT (arg))
17169 tree fn = OVL_CURRENT (arg);
17170 tree subargs, elem;
17172 if (TREE_CODE (fn) != TEMPLATE_DECL)
17173 continue;
17175 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17176 expl_subargs, NULL_TREE, tf_none,
17177 /*require_all_args=*/true,
17178 /*use_default_args=*/true);
17179 if (subargs != error_mark_node
17180 && !any_dependent_template_arguments_p (subargs))
17182 elem = instantiate_template (fn, subargs, tf_none);
17183 if (elem == error_mark_node)
17185 badfn = fn;
17186 badargs = subargs;
17188 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
17190 goodfn = elem;
17191 ++good;
17195 if (good == 1)
17197 mark_used (goodfn);
17198 expr = goodfn;
17199 if (baselink)
17200 expr = build_baselink (BASELINK_BINFO (baselink),
17201 BASELINK_ACCESS_BINFO (baselink),
17202 expr, BASELINK_OPTYPE (baselink));
17203 if (offset)
17205 tree base
17206 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
17207 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
17209 if (addr)
17210 expr = cp_build_addr_expr (expr, tf_warning_or_error);
17211 return expr;
17213 else if (good == 0 && badargs)
17214 /* There were no good options and at least one bad one, so let the
17215 user know what the problem is. */
17216 instantiate_template (badfn, badargs, tf_warning_or_error);
17218 return orig_expr;
17221 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17222 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17223 different overloads deduce different arguments for a given parm.
17224 ADDR_P is true if the expression for which deduction is being
17225 performed was of the form "& fn" rather than simply "fn".
17227 Returns 1 on success. */
17229 static int
17230 try_one_overload (tree tparms,
17231 tree orig_targs,
17232 tree targs,
17233 tree parm,
17234 tree arg,
17235 unification_kind_t strict,
17236 int sub_strict,
17237 bool addr_p,
17238 bool explain_p)
17240 int nargs;
17241 tree tempargs;
17242 int i;
17244 if (arg == error_mark_node)
17245 return 0;
17247 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17248 to function or pointer to member function argument if the set of
17249 overloaded functions does not contain function templates and at most
17250 one of a set of overloaded functions provides a unique match.
17252 So if this is a template, just return success. */
17254 if (uses_template_parms (arg))
17255 return 1;
17257 if (TREE_CODE (arg) == METHOD_TYPE)
17258 arg = build_ptrmemfunc_type (build_pointer_type (arg));
17259 else if (addr_p)
17260 arg = build_pointer_type (arg);
17262 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17264 /* We don't copy orig_targs for this because if we have already deduced
17265 some template args from previous args, unify would complain when we
17266 try to deduce a template parameter for the same argument, even though
17267 there isn't really a conflict. */
17268 nargs = TREE_VEC_LENGTH (targs);
17269 tempargs = make_tree_vec (nargs);
17271 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17272 return 0;
17274 /* First make sure we didn't deduce anything that conflicts with
17275 explicitly specified args. */
17276 for (i = nargs; i--; )
17278 tree elt = TREE_VEC_ELT (tempargs, i);
17279 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17281 if (!elt)
17282 /*NOP*/;
17283 else if (uses_template_parms (elt))
17284 /* Since we're unifying against ourselves, we will fill in
17285 template args used in the function parm list with our own
17286 template parms. Discard them. */
17287 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17288 else if (oldelt && !template_args_equal (oldelt, elt))
17289 return 0;
17292 for (i = nargs; i--; )
17294 tree elt = TREE_VEC_ELT (tempargs, i);
17296 if (elt)
17297 TREE_VEC_ELT (targs, i) = elt;
17300 return 1;
17303 /* PARM is a template class (perhaps with unbound template
17304 parameters). ARG is a fully instantiated type. If ARG can be
17305 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17306 TARGS are as for unify. */
17308 static tree
17309 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17310 bool explain_p)
17312 tree copy_of_targs;
17314 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17315 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17316 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17317 return NULL_TREE;
17319 /* We need to make a new template argument vector for the call to
17320 unify. If we used TARGS, we'd clutter it up with the result of
17321 the attempted unification, even if this class didn't work out.
17322 We also don't want to commit ourselves to all the unifications
17323 we've already done, since unification is supposed to be done on
17324 an argument-by-argument basis. In other words, consider the
17325 following pathological case:
17327 template <int I, int J, int K>
17328 struct S {};
17330 template <int I, int J>
17331 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17333 template <int I, int J, int K>
17334 void f(S<I, J, K>, S<I, I, I>);
17336 void g() {
17337 S<0, 0, 0> s0;
17338 S<0, 1, 2> s2;
17340 f(s0, s2);
17343 Now, by the time we consider the unification involving `s2', we
17344 already know that we must have `f<0, 0, 0>'. But, even though
17345 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17346 because there are two ways to unify base classes of S<0, 1, 2>
17347 with S<I, I, I>. If we kept the already deduced knowledge, we
17348 would reject the possibility I=1. */
17349 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17351 /* If unification failed, we're done. */
17352 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17353 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17354 return NULL_TREE;
17356 return arg;
17359 /* Given a template type PARM and a class type ARG, find the unique
17360 base type in ARG that is an instance of PARM. We do not examine
17361 ARG itself; only its base-classes. If there is not exactly one
17362 appropriate base class, return NULL_TREE. PARM may be the type of
17363 a partial specialization, as well as a plain template type. Used
17364 by unify. */
17366 static enum template_base_result
17367 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17368 bool explain_p, tree *result)
17370 tree rval = NULL_TREE;
17371 tree binfo;
17373 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17375 binfo = TYPE_BINFO (complete_type (arg));
17376 if (!binfo)
17378 /* The type could not be completed. */
17379 *result = NULL_TREE;
17380 return tbr_incomplete_type;
17383 /* Walk in inheritance graph order. The search order is not
17384 important, and this avoids multiple walks of virtual bases. */
17385 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17387 tree r = try_class_unification (tparms, targs, parm,
17388 BINFO_TYPE (binfo), explain_p);
17390 if (r)
17392 /* If there is more than one satisfactory baseclass, then:
17394 [temp.deduct.call]
17396 If they yield more than one possible deduced A, the type
17397 deduction fails.
17399 applies. */
17400 if (rval && !same_type_p (r, rval))
17402 *result = NULL_TREE;
17403 return tbr_ambiguous_baseclass;
17406 rval = r;
17410 *result = rval;
17411 return tbr_success;
17414 /* Returns the level of DECL, which declares a template parameter. */
17416 static int
17417 template_decl_level (tree decl)
17419 switch (TREE_CODE (decl))
17421 case TYPE_DECL:
17422 case TEMPLATE_DECL:
17423 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17425 case PARM_DECL:
17426 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17428 default:
17429 gcc_unreachable ();
17431 return 0;
17434 /* Decide whether ARG can be unified with PARM, considering only the
17435 cv-qualifiers of each type, given STRICT as documented for unify.
17436 Returns nonzero iff the unification is OK on that basis. */
17438 static int
17439 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17441 int arg_quals = cp_type_quals (arg);
17442 int parm_quals = cp_type_quals (parm);
17444 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17445 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17447 /* Although a CVR qualifier is ignored when being applied to a
17448 substituted template parameter ([8.3.2]/1 for example), that
17449 does not allow us to unify "const T" with "int&" because both
17450 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17451 It is ok when we're allowing additional CV qualifiers
17452 at the outer level [14.8.2.1]/3,1st bullet. */
17453 if ((TREE_CODE (arg) == REFERENCE_TYPE
17454 || TREE_CODE (arg) == FUNCTION_TYPE
17455 || TREE_CODE (arg) == METHOD_TYPE)
17456 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17457 return 0;
17459 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17460 && (parm_quals & TYPE_QUAL_RESTRICT))
17461 return 0;
17464 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17465 && (arg_quals & parm_quals) != parm_quals)
17466 return 0;
17468 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17469 && (parm_quals & arg_quals) != arg_quals)
17470 return 0;
17472 return 1;
17475 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17476 void
17477 template_parm_level_and_index (tree parm, int* level, int* index)
17479 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17480 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17481 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17483 *index = TEMPLATE_TYPE_IDX (parm);
17484 *level = TEMPLATE_TYPE_LEVEL (parm);
17486 else
17488 *index = TEMPLATE_PARM_IDX (parm);
17489 *level = TEMPLATE_PARM_LEVEL (parm);
17493 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17494 do { \
17495 if (unify (TP, TA, P, A, S, EP)) \
17496 return 1; \
17497 } while (0);
17499 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17500 expansion at the end of PACKED_PARMS. Returns 0 if the type
17501 deduction succeeds, 1 otherwise. STRICT is the same as in
17502 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17503 call argument list. We'll need to adjust the arguments to make them
17504 types. SUBR tells us if this is from a recursive call to
17505 type_unification_real, or for comparing two template argument
17506 lists. */
17508 static int
17509 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17510 tree packed_args, unification_kind_t strict,
17511 bool subr, bool explain_p)
17513 tree parm
17514 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17515 tree pattern = PACK_EXPANSION_PATTERN (parm);
17516 tree pack, packs = NULL_TREE;
17517 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17519 packed_args = expand_template_argument_pack (packed_args);
17521 int len = TREE_VEC_LENGTH (packed_args);
17523 /* Determine the parameter packs we will be deducing from the
17524 pattern, and record their current deductions. */
17525 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17526 pack; pack = TREE_CHAIN (pack))
17528 tree parm_pack = TREE_VALUE (pack);
17529 int idx, level;
17531 /* Determine the index and level of this parameter pack. */
17532 template_parm_level_and_index (parm_pack, &level, &idx);
17534 /* Keep track of the parameter packs and their corresponding
17535 argument packs. */
17536 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17537 TREE_TYPE (packs) = make_tree_vec (len - start);
17540 /* Loop through all of the arguments that have not yet been
17541 unified and unify each with the pattern. */
17542 for (i = start; i < len; i++)
17544 tree parm;
17545 bool any_explicit = false;
17546 tree arg = TREE_VEC_ELT (packed_args, i);
17548 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17549 or the element of its argument pack at the current index if
17550 this argument was explicitly specified. */
17551 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17553 int idx, level;
17554 tree arg, pargs;
17555 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17557 arg = NULL_TREE;
17558 if (TREE_VALUE (pack)
17559 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17560 && (i - start < TREE_VEC_LENGTH (pargs)))
17562 any_explicit = true;
17563 arg = TREE_VEC_ELT (pargs, i - start);
17565 TMPL_ARG (targs, level, idx) = arg;
17568 /* If we had explicit template arguments, substitute them into the
17569 pattern before deduction. */
17570 if (any_explicit)
17572 /* Some arguments might still be unspecified or dependent. */
17573 bool dependent;
17574 ++processing_template_decl;
17575 dependent = any_dependent_template_arguments_p (targs);
17576 if (!dependent)
17577 --processing_template_decl;
17578 parm = tsubst (pattern, targs,
17579 explain_p ? tf_warning_or_error : tf_none,
17580 NULL_TREE);
17581 if (dependent)
17582 --processing_template_decl;
17583 if (parm == error_mark_node)
17584 return 1;
17586 else
17587 parm = pattern;
17589 /* Unify the pattern with the current argument. */
17590 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17591 LOOKUP_IMPLICIT, explain_p))
17592 return 1;
17594 /* For each parameter pack, collect the deduced value. */
17595 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17597 int idx, level;
17598 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17600 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17601 TMPL_ARG (targs, level, idx);
17605 /* Verify that the results of unification with the parameter packs
17606 produce results consistent with what we've seen before, and make
17607 the deduced argument packs available. */
17608 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17610 tree old_pack = TREE_VALUE (pack);
17611 tree new_args = TREE_TYPE (pack);
17612 int i, len = TREE_VEC_LENGTH (new_args);
17613 int idx, level;
17614 bool nondeduced_p = false;
17616 /* By default keep the original deduced argument pack.
17617 If necessary, more specific code is going to update the
17618 resulting deduced argument later down in this function. */
17619 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17620 TMPL_ARG (targs, level, idx) = old_pack;
17622 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17623 actually deduce anything. */
17624 for (i = 0; i < len && !nondeduced_p; ++i)
17625 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17626 nondeduced_p = true;
17627 if (nondeduced_p)
17628 continue;
17630 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17632 /* If we had fewer function args than explicit template args,
17633 just use the explicits. */
17634 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17635 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17636 if (len < explicit_len)
17637 new_args = explicit_args;
17640 if (!old_pack)
17642 tree result;
17643 /* Build the deduced *_ARGUMENT_PACK. */
17644 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17646 result = make_node (NONTYPE_ARGUMENT_PACK);
17647 TREE_TYPE (result) =
17648 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17649 TREE_CONSTANT (result) = 1;
17651 else
17652 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17654 SET_ARGUMENT_PACK_ARGS (result, new_args);
17656 /* Note the deduced argument packs for this parameter
17657 pack. */
17658 TMPL_ARG (targs, level, idx) = result;
17660 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17661 && (ARGUMENT_PACK_ARGS (old_pack)
17662 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17664 /* We only had the explicitly-provided arguments before, but
17665 now we have a complete set of arguments. */
17666 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17668 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17669 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17670 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17672 else
17674 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17675 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17677 if (!comp_template_args_with_info (old_args, new_args,
17678 &bad_old_arg, &bad_new_arg))
17679 /* Inconsistent unification of this parameter pack. */
17680 return unify_parameter_pack_inconsistent (explain_p,
17681 bad_old_arg,
17682 bad_new_arg);
17686 return unify_success (explain_p);
17689 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17690 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17691 parameters and return value are as for unify. */
17693 static int
17694 unify_array_domain (tree tparms, tree targs,
17695 tree parm_dom, tree arg_dom,
17696 bool explain_p)
17698 tree parm_max;
17699 tree arg_max;
17700 bool parm_cst;
17701 bool arg_cst;
17703 /* Our representation of array types uses "N - 1" as the
17704 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17705 not an integer constant. We cannot unify arbitrarily
17706 complex expressions, so we eliminate the MINUS_EXPRs
17707 here. */
17708 parm_max = TYPE_MAX_VALUE (parm_dom);
17709 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17710 if (!parm_cst)
17712 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17713 parm_max = TREE_OPERAND (parm_max, 0);
17715 arg_max = TYPE_MAX_VALUE (arg_dom);
17716 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17717 if (!arg_cst)
17719 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17720 trying to unify the type of a variable with the type
17721 of a template parameter. For example:
17723 template <unsigned int N>
17724 void f (char (&) [N]);
17725 int g();
17726 void h(int i) {
17727 char a[g(i)];
17728 f(a);
17731 Here, the type of the ARG will be "int [g(i)]", and
17732 may be a SAVE_EXPR, etc. */
17733 if (TREE_CODE (arg_max) != MINUS_EXPR)
17734 return unify_vla_arg (explain_p, arg_dom);
17735 arg_max = TREE_OPERAND (arg_max, 0);
17738 /* If only one of the bounds used a MINUS_EXPR, compensate
17739 by adding one to the other bound. */
17740 if (parm_cst && !arg_cst)
17741 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17742 integer_type_node,
17743 parm_max,
17744 integer_one_node);
17745 else if (arg_cst && !parm_cst)
17746 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17747 integer_type_node,
17748 arg_max,
17749 integer_one_node);
17751 return unify (tparms, targs, parm_max, arg_max,
17752 UNIFY_ALLOW_INTEGER, explain_p);
17755 /* Deduce the value of template parameters. TPARMS is the (innermost)
17756 set of template parameters to a template. TARGS is the bindings
17757 for those template parameters, as determined thus far; TARGS may
17758 include template arguments for outer levels of template parameters
17759 as well. PARM is a parameter to a template function, or a
17760 subcomponent of that parameter; ARG is the corresponding argument.
17761 This function attempts to match PARM with ARG in a manner
17762 consistent with the existing assignments in TARGS. If more values
17763 are deduced, then TARGS is updated.
17765 Returns 0 if the type deduction succeeds, 1 otherwise. The
17766 parameter STRICT is a bitwise or of the following flags:
17768 UNIFY_ALLOW_NONE:
17769 Require an exact match between PARM and ARG.
17770 UNIFY_ALLOW_MORE_CV_QUAL:
17771 Allow the deduced ARG to be more cv-qualified (by qualification
17772 conversion) than ARG.
17773 UNIFY_ALLOW_LESS_CV_QUAL:
17774 Allow the deduced ARG to be less cv-qualified than ARG.
17775 UNIFY_ALLOW_DERIVED:
17776 Allow the deduced ARG to be a template base class of ARG,
17777 or a pointer to a template base class of the type pointed to by
17778 ARG.
17779 UNIFY_ALLOW_INTEGER:
17780 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17781 case for more information.
17782 UNIFY_ALLOW_OUTER_LEVEL:
17783 This is the outermost level of a deduction. Used to determine validity
17784 of qualification conversions. A valid qualification conversion must
17785 have const qualified pointers leading up to the inner type which
17786 requires additional CV quals, except at the outer level, where const
17787 is not required [conv.qual]. It would be normal to set this flag in
17788 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17789 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17790 This is the outermost level of a deduction, and PARM can be more CV
17791 qualified at this point.
17792 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17793 This is the outermost level of a deduction, and PARM can be less CV
17794 qualified at this point. */
17796 static int
17797 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17798 bool explain_p)
17800 int idx;
17801 tree targ;
17802 tree tparm;
17803 int strict_in = strict;
17805 /* I don't think this will do the right thing with respect to types.
17806 But the only case I've seen it in so far has been array bounds, where
17807 signedness is the only information lost, and I think that will be
17808 okay. */
17809 while (TREE_CODE (parm) == NOP_EXPR)
17810 parm = TREE_OPERAND (parm, 0);
17812 if (arg == error_mark_node)
17813 return unify_invalid (explain_p);
17814 if (arg == unknown_type_node
17815 || arg == init_list_type_node)
17816 /* We can't deduce anything from this, but we might get all the
17817 template args from other function args. */
17818 return unify_success (explain_p);
17820 /* If PARM uses template parameters, then we can't bail out here,
17821 even if ARG == PARM, since we won't record unifications for the
17822 template parameters. We might need them if we're trying to
17823 figure out which of two things is more specialized. */
17824 if (arg == parm && !uses_template_parms (parm))
17825 return unify_success (explain_p);
17827 /* Handle init lists early, so the rest of the function can assume
17828 we're dealing with a type. */
17829 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17831 tree elt, elttype;
17832 unsigned i;
17833 tree orig_parm = parm;
17835 /* Replace T with std::initializer_list<T> for deduction. */
17836 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17837 && flag_deduce_init_list)
17838 parm = listify (parm);
17840 if (!is_std_init_list (parm)
17841 && TREE_CODE (parm) != ARRAY_TYPE)
17842 /* We can only deduce from an initializer list argument if the
17843 parameter is std::initializer_list or an array; otherwise this
17844 is a non-deduced context. */
17845 return unify_success (explain_p);
17847 if (TREE_CODE (parm) == ARRAY_TYPE)
17848 elttype = TREE_TYPE (parm);
17849 else
17850 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17852 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17854 int elt_strict = strict;
17856 if (elt == error_mark_node)
17857 return unify_invalid (explain_p);
17859 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17861 tree type = TREE_TYPE (elt);
17862 /* It should only be possible to get here for a call. */
17863 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17864 elt_strict |= maybe_adjust_types_for_deduction
17865 (DEDUCE_CALL, &elttype, &type, elt);
17866 elt = type;
17869 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17870 explain_p);
17873 if (TREE_CODE (parm) == ARRAY_TYPE
17874 && deducible_array_bound (TYPE_DOMAIN (parm)))
17876 /* Also deduce from the length of the initializer list. */
17877 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17878 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17879 if (idx == error_mark_node)
17880 return unify_invalid (explain_p);
17881 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17882 idx, explain_p);
17885 /* If the std::initializer_list<T> deduction worked, replace the
17886 deduced A with std::initializer_list<A>. */
17887 if (orig_parm != parm)
17889 idx = TEMPLATE_TYPE_IDX (orig_parm);
17890 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17891 targ = listify (targ);
17892 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17894 return unify_success (explain_p);
17897 /* Immediately reject some pairs that won't unify because of
17898 cv-qualification mismatches. */
17899 if (TREE_CODE (arg) == TREE_CODE (parm)
17900 && TYPE_P (arg)
17901 /* It is the elements of the array which hold the cv quals of an array
17902 type, and the elements might be template type parms. We'll check
17903 when we recurse. */
17904 && TREE_CODE (arg) != ARRAY_TYPE
17905 /* We check the cv-qualifiers when unifying with template type
17906 parameters below. We want to allow ARG `const T' to unify with
17907 PARM `T' for example, when computing which of two templates
17908 is more specialized, for example. */
17909 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17910 && !check_cv_quals_for_unify (strict_in, arg, parm))
17911 return unify_cv_qual_mismatch (explain_p, parm, arg);
17913 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17914 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17915 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17916 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17917 strict &= ~UNIFY_ALLOW_DERIVED;
17918 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17919 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17921 switch (TREE_CODE (parm))
17923 case TYPENAME_TYPE:
17924 case SCOPE_REF:
17925 case UNBOUND_CLASS_TEMPLATE:
17926 /* In a type which contains a nested-name-specifier, template
17927 argument values cannot be deduced for template parameters used
17928 within the nested-name-specifier. */
17929 return unify_success (explain_p);
17931 case TEMPLATE_TYPE_PARM:
17932 case TEMPLATE_TEMPLATE_PARM:
17933 case BOUND_TEMPLATE_TEMPLATE_PARM:
17934 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17935 if (error_operand_p (tparm))
17936 return unify_invalid (explain_p);
17938 if (TEMPLATE_TYPE_LEVEL (parm)
17939 != template_decl_level (tparm))
17940 /* The PARM is not one we're trying to unify. Just check
17941 to see if it matches ARG. */
17943 if (TREE_CODE (arg) == TREE_CODE (parm)
17944 && (is_auto (parm) ? is_auto (arg)
17945 : same_type_p (parm, arg)))
17946 return unify_success (explain_p);
17947 else
17948 return unify_type_mismatch (explain_p, parm, arg);
17950 idx = TEMPLATE_TYPE_IDX (parm);
17951 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17952 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17953 if (error_operand_p (tparm))
17954 return unify_invalid (explain_p);
17956 /* Check for mixed types and values. */
17957 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17958 && TREE_CODE (tparm) != TYPE_DECL)
17959 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17960 && TREE_CODE (tparm) != TEMPLATE_DECL))
17961 gcc_unreachable ();
17963 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17965 /* ARG must be constructed from a template class or a template
17966 template parameter. */
17967 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17968 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17969 return unify_template_deduction_failure (explain_p, parm, arg);
17971 tree parmvec = TYPE_TI_ARGS (parm);
17972 /* An alias template name is never deduced. */
17973 if (TYPE_ALIAS_P (arg))
17974 arg = strip_typedefs (arg);
17975 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17976 tree full_argvec = add_to_template_args (targs, argvec);
17977 tree parm_parms
17978 = DECL_INNERMOST_TEMPLATE_PARMS
17979 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17980 int i, len;
17981 int parm_variadic_p = 0;
17983 /* The resolution to DR150 makes clear that default
17984 arguments for an N-argument may not be used to bind T
17985 to a template template parameter with fewer than N
17986 parameters. It is not safe to permit the binding of
17987 default arguments as an extension, as that may change
17988 the meaning of a conforming program. Consider:
17990 struct Dense { static const unsigned int dim = 1; };
17992 template <template <typename> class View,
17993 typename Block>
17994 void operator+(float, View<Block> const&);
17996 template <typename Block,
17997 unsigned int Dim = Block::dim>
17998 struct Lvalue_proxy { operator float() const; };
18000 void
18001 test_1d (void) {
18002 Lvalue_proxy<Dense> p;
18003 float b;
18004 b + p;
18007 Here, if Lvalue_proxy is permitted to bind to View, then
18008 the global operator+ will be used; if they are not, the
18009 Lvalue_proxy will be converted to float. */
18010 if (coerce_template_parms (parm_parms,
18011 full_argvec,
18012 TYPE_TI_TEMPLATE (parm),
18013 (explain_p
18014 ? tf_warning_or_error
18015 : tf_none),
18016 /*require_all_args=*/true,
18017 /*use_default_args=*/false)
18018 == error_mark_node)
18019 return 1;
18021 /* Deduce arguments T, i from TT<T> or TT<i>.
18022 We check each element of PARMVEC and ARGVEC individually
18023 rather than the whole TREE_VEC since they can have
18024 different number of elements. */
18026 parmvec = expand_template_argument_pack (parmvec);
18027 argvec = expand_template_argument_pack (argvec);
18029 len = TREE_VEC_LENGTH (parmvec);
18031 /* Check if the parameters end in a pack, making them
18032 variadic. */
18033 if (len > 0
18034 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
18035 parm_variadic_p = 1;
18037 for (i = 0; i < len - parm_variadic_p; ++i)
18038 /* If the template argument list of P contains a pack
18039 expansion that is not the last template argument, the
18040 entire template argument list is a non-deduced
18041 context. */
18042 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
18043 return unify_success (explain_p);
18045 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
18046 return unify_too_few_arguments (explain_p,
18047 TREE_VEC_LENGTH (argvec), len);
18049 for (i = 0; i < len - parm_variadic_p; ++i)
18051 RECUR_AND_CHECK_FAILURE (tparms, targs,
18052 TREE_VEC_ELT (parmvec, i),
18053 TREE_VEC_ELT (argvec, i),
18054 UNIFY_ALLOW_NONE, explain_p);
18057 if (parm_variadic_p
18058 && unify_pack_expansion (tparms, targs,
18059 parmvec, argvec,
18060 DEDUCE_EXACT,
18061 /*subr=*/true, explain_p))
18062 return 1;
18064 arg = TYPE_TI_TEMPLATE (arg);
18066 /* Fall through to deduce template name. */
18069 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18070 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18072 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18074 /* Simple cases: Value already set, does match or doesn't. */
18075 if (targ != NULL_TREE && template_args_equal (targ, arg))
18076 return unify_success (explain_p);
18077 else if (targ)
18078 return unify_inconsistency (explain_p, parm, targ, arg);
18080 else
18082 /* If PARM is `const T' and ARG is only `int', we don't have
18083 a match unless we are allowing additional qualification.
18084 If ARG is `const int' and PARM is just `T' that's OK;
18085 that binds `const int' to `T'. */
18086 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
18087 arg, parm))
18088 return unify_cv_qual_mismatch (explain_p, parm, arg);
18090 /* Consider the case where ARG is `const volatile int' and
18091 PARM is `const T'. Then, T should be `volatile int'. */
18092 arg = cp_build_qualified_type_real
18093 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
18094 if (arg == error_mark_node)
18095 return unify_invalid (explain_p);
18097 /* Simple cases: Value already set, does match or doesn't. */
18098 if (targ != NULL_TREE && same_type_p (targ, arg))
18099 return unify_success (explain_p);
18100 else if (targ)
18101 return unify_inconsistency (explain_p, parm, targ, arg);
18103 /* Make sure that ARG is not a variable-sized array. (Note
18104 that were talking about variable-sized arrays (like
18105 `int[n]'), rather than arrays of unknown size (like
18106 `int[]').) We'll get very confused by such a type since
18107 the bound of the array is not constant, and therefore
18108 not mangleable. Besides, such types are not allowed in
18109 ISO C++, so we can do as we please here. We do allow
18110 them for 'auto' deduction, since that isn't ABI-exposed. */
18111 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
18112 return unify_vla_arg (explain_p, arg);
18114 /* Strip typedefs as in convert_template_argument. */
18115 arg = canonicalize_type_argument (arg, tf_none);
18118 /* If ARG is a parameter pack or an expansion, we cannot unify
18119 against it unless PARM is also a parameter pack. */
18120 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18121 && !template_parameter_pack_p (parm))
18122 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18124 /* If the argument deduction results is a METHOD_TYPE,
18125 then there is a problem.
18126 METHOD_TYPE doesn't map to any real C++ type the result of
18127 the deduction can not be of that type. */
18128 if (TREE_CODE (arg) == METHOD_TYPE)
18129 return unify_method_type_error (explain_p, arg);
18131 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18132 return unify_success (explain_p);
18134 case TEMPLATE_PARM_INDEX:
18135 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18136 if (error_operand_p (tparm))
18137 return unify_invalid (explain_p);
18139 if (TEMPLATE_PARM_LEVEL (parm)
18140 != template_decl_level (tparm))
18142 /* The PARM is not one we're trying to unify. Just check
18143 to see if it matches ARG. */
18144 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
18145 && cp_tree_equal (parm, arg));
18146 if (result)
18147 unify_expression_unequal (explain_p, parm, arg);
18148 return result;
18151 idx = TEMPLATE_PARM_IDX (parm);
18152 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18154 if (targ)
18156 int x = !cp_tree_equal (targ, arg);
18157 if (x)
18158 unify_inconsistency (explain_p, parm, targ, arg);
18159 return x;
18162 /* [temp.deduct.type] If, in the declaration of a function template
18163 with a non-type template-parameter, the non-type
18164 template-parameter is used in an expression in the function
18165 parameter-list and, if the corresponding template-argument is
18166 deduced, the template-argument type shall match the type of the
18167 template-parameter exactly, except that a template-argument
18168 deduced from an array bound may be of any integral type.
18169 The non-type parameter might use already deduced type parameters. */
18170 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
18171 if (!TREE_TYPE (arg))
18172 /* Template-parameter dependent expression. Just accept it for now.
18173 It will later be processed in convert_template_argument. */
18175 else if (same_type_p (TREE_TYPE (arg), tparm))
18176 /* OK */;
18177 else if ((strict & UNIFY_ALLOW_INTEGER)
18178 && CP_INTEGRAL_TYPE_P (tparm))
18179 /* Convert the ARG to the type of PARM; the deduced non-type
18180 template argument must exactly match the types of the
18181 corresponding parameter. */
18182 arg = fold (build_nop (tparm, arg));
18183 else if (uses_template_parms (tparm))
18184 /* We haven't deduced the type of this parameter yet. Try again
18185 later. */
18186 return unify_success (explain_p);
18187 else
18188 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
18190 /* If ARG is a parameter pack or an expansion, we cannot unify
18191 against it unless PARM is also a parameter pack. */
18192 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18193 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
18194 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18196 arg = strip_typedefs_expr (arg);
18197 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18198 return unify_success (explain_p);
18200 case PTRMEM_CST:
18202 /* A pointer-to-member constant can be unified only with
18203 another constant. */
18204 if (TREE_CODE (arg) != PTRMEM_CST)
18205 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
18207 /* Just unify the class member. It would be useless (and possibly
18208 wrong, depending on the strict flags) to unify also
18209 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18210 arg refer to the same variable, even if through different
18211 classes. For instance:
18213 struct A { int x; };
18214 struct B : A { };
18216 Unification of &A::x and &B::x must succeed. */
18217 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
18218 PTRMEM_CST_MEMBER (arg), strict, explain_p);
18221 case POINTER_TYPE:
18223 if (!TYPE_PTR_P (arg))
18224 return unify_type_mismatch (explain_p, parm, arg);
18226 /* [temp.deduct.call]
18228 A can be another pointer or pointer to member type that can
18229 be converted to the deduced A via a qualification
18230 conversion (_conv.qual_).
18232 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18233 This will allow for additional cv-qualification of the
18234 pointed-to types if appropriate. */
18236 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
18237 /* The derived-to-base conversion only persists through one
18238 level of pointers. */
18239 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
18241 return unify (tparms, targs, TREE_TYPE (parm),
18242 TREE_TYPE (arg), strict, explain_p);
18245 case REFERENCE_TYPE:
18246 if (TREE_CODE (arg) != REFERENCE_TYPE)
18247 return unify_type_mismatch (explain_p, parm, arg);
18248 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18249 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18251 case ARRAY_TYPE:
18252 if (TREE_CODE (arg) != ARRAY_TYPE)
18253 return unify_type_mismatch (explain_p, parm, arg);
18254 if ((TYPE_DOMAIN (parm) == NULL_TREE)
18255 != (TYPE_DOMAIN (arg) == NULL_TREE))
18256 return unify_type_mismatch (explain_p, parm, arg);
18257 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18258 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18259 if (TYPE_DOMAIN (parm) != NULL_TREE)
18260 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18261 TYPE_DOMAIN (arg), explain_p);
18262 return unify_success (explain_p);
18264 case REAL_TYPE:
18265 case COMPLEX_TYPE:
18266 case VECTOR_TYPE:
18267 case INTEGER_TYPE:
18268 case BOOLEAN_TYPE:
18269 case ENUMERAL_TYPE:
18270 case VOID_TYPE:
18271 case NULLPTR_TYPE:
18272 if (TREE_CODE (arg) != TREE_CODE (parm))
18273 return unify_type_mismatch (explain_p, parm, arg);
18275 /* We have already checked cv-qualification at the top of the
18276 function. */
18277 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18278 return unify_type_mismatch (explain_p, parm, arg);
18280 /* As far as unification is concerned, this wins. Later checks
18281 will invalidate it if necessary. */
18282 return unify_success (explain_p);
18284 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18285 /* Type INTEGER_CST can come from ordinary constant template args. */
18286 case INTEGER_CST:
18287 while (TREE_CODE (arg) == NOP_EXPR)
18288 arg = TREE_OPERAND (arg, 0);
18290 if (TREE_CODE (arg) != INTEGER_CST)
18291 return unify_template_argument_mismatch (explain_p, parm, arg);
18292 return (tree_int_cst_equal (parm, arg)
18293 ? unify_success (explain_p)
18294 : unify_template_argument_mismatch (explain_p, parm, arg));
18296 case TREE_VEC:
18298 int i, len, argslen;
18299 int parm_variadic_p = 0;
18301 if (TREE_CODE (arg) != TREE_VEC)
18302 return unify_template_argument_mismatch (explain_p, parm, arg);
18304 len = TREE_VEC_LENGTH (parm);
18305 argslen = TREE_VEC_LENGTH (arg);
18307 /* Check for pack expansions in the parameters. */
18308 for (i = 0; i < len; ++i)
18310 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18312 if (i == len - 1)
18313 /* We can unify against something with a trailing
18314 parameter pack. */
18315 parm_variadic_p = 1;
18316 else
18317 /* [temp.deduct.type]/9: If the template argument list of
18318 P contains a pack expansion that is not the last
18319 template argument, the entire template argument list
18320 is a non-deduced context. */
18321 return unify_success (explain_p);
18325 /* If we don't have enough arguments to satisfy the parameters
18326 (not counting the pack expression at the end), or we have
18327 too many arguments for a parameter list that doesn't end in
18328 a pack expression, we can't unify. */
18329 if (parm_variadic_p
18330 ? argslen < len - parm_variadic_p
18331 : argslen != len)
18332 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18334 /* Unify all of the parameters that precede the (optional)
18335 pack expression. */
18336 for (i = 0; i < len - parm_variadic_p; ++i)
18338 RECUR_AND_CHECK_FAILURE (tparms, targs,
18339 TREE_VEC_ELT (parm, i),
18340 TREE_VEC_ELT (arg, i),
18341 UNIFY_ALLOW_NONE, explain_p);
18343 if (parm_variadic_p)
18344 return unify_pack_expansion (tparms, targs, parm, arg,
18345 DEDUCE_EXACT,
18346 /*subr=*/true, explain_p);
18347 return unify_success (explain_p);
18350 case RECORD_TYPE:
18351 case UNION_TYPE:
18352 if (TREE_CODE (arg) != TREE_CODE (parm))
18353 return unify_type_mismatch (explain_p, parm, arg);
18355 if (TYPE_PTRMEMFUNC_P (parm))
18357 if (!TYPE_PTRMEMFUNC_P (arg))
18358 return unify_type_mismatch (explain_p, parm, arg);
18360 return unify (tparms, targs,
18361 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18362 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18363 strict, explain_p);
18365 else if (TYPE_PTRMEMFUNC_P (arg))
18366 return unify_type_mismatch (explain_p, parm, arg);
18368 if (CLASSTYPE_TEMPLATE_INFO (parm))
18370 tree t = NULL_TREE;
18372 if (strict_in & UNIFY_ALLOW_DERIVED)
18374 /* First, we try to unify the PARM and ARG directly. */
18375 t = try_class_unification (tparms, targs,
18376 parm, arg, explain_p);
18378 if (!t)
18380 /* Fallback to the special case allowed in
18381 [temp.deduct.call]:
18383 If P is a class, and P has the form
18384 template-id, then A can be a derived class of
18385 the deduced A. Likewise, if P is a pointer to
18386 a class of the form template-id, A can be a
18387 pointer to a derived class pointed to by the
18388 deduced A. */
18389 enum template_base_result r;
18390 r = get_template_base (tparms, targs, parm, arg,
18391 explain_p, &t);
18393 if (!t)
18394 return unify_no_common_base (explain_p, r, parm, arg);
18397 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18398 && (CLASSTYPE_TI_TEMPLATE (parm)
18399 == CLASSTYPE_TI_TEMPLATE (arg)))
18400 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18401 Then, we should unify `int' and `U'. */
18402 t = arg;
18403 else
18404 /* There's no chance of unification succeeding. */
18405 return unify_type_mismatch (explain_p, parm, arg);
18407 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18408 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18410 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18411 return unify_type_mismatch (explain_p, parm, arg);
18412 return unify_success (explain_p);
18414 case METHOD_TYPE:
18415 case FUNCTION_TYPE:
18417 unsigned int nargs;
18418 tree *args;
18419 tree a;
18420 unsigned int i;
18422 if (TREE_CODE (arg) != TREE_CODE (parm))
18423 return unify_type_mismatch (explain_p, parm, arg);
18425 /* CV qualifications for methods can never be deduced, they must
18426 match exactly. We need to check them explicitly here,
18427 because type_unification_real treats them as any other
18428 cv-qualified parameter. */
18429 if (TREE_CODE (parm) == METHOD_TYPE
18430 && (!check_cv_quals_for_unify
18431 (UNIFY_ALLOW_NONE,
18432 class_of_this_parm (arg),
18433 class_of_this_parm (parm))))
18434 return unify_cv_qual_mismatch (explain_p, parm, arg);
18436 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18437 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18439 nargs = list_length (TYPE_ARG_TYPES (arg));
18440 args = XALLOCAVEC (tree, nargs);
18441 for (a = TYPE_ARG_TYPES (arg), i = 0;
18442 a != NULL_TREE && a != void_list_node;
18443 a = TREE_CHAIN (a), ++i)
18444 args[i] = TREE_VALUE (a);
18445 nargs = i;
18447 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18448 args, nargs, 1, DEDUCE_EXACT,
18449 LOOKUP_NORMAL, NULL, explain_p);
18452 case OFFSET_TYPE:
18453 /* Unify a pointer to member with a pointer to member function, which
18454 deduces the type of the member as a function type. */
18455 if (TYPE_PTRMEMFUNC_P (arg))
18457 /* Check top-level cv qualifiers */
18458 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18459 return unify_cv_qual_mismatch (explain_p, parm, arg);
18461 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18462 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18463 UNIFY_ALLOW_NONE, explain_p);
18465 /* Determine the type of the function we are unifying against. */
18466 tree fntype = static_fn_type (arg);
18468 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18471 if (TREE_CODE (arg) != OFFSET_TYPE)
18472 return unify_type_mismatch (explain_p, parm, arg);
18473 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18474 TYPE_OFFSET_BASETYPE (arg),
18475 UNIFY_ALLOW_NONE, explain_p);
18476 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18477 strict, explain_p);
18479 case CONST_DECL:
18480 if (DECL_TEMPLATE_PARM_P (parm))
18481 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18482 if (arg != scalar_constant_value (parm))
18483 return unify_template_argument_mismatch (explain_p, parm, arg);
18484 return unify_success (explain_p);
18486 case FIELD_DECL:
18487 case TEMPLATE_DECL:
18488 /* Matched cases are handled by the ARG == PARM test above. */
18489 return unify_template_argument_mismatch (explain_p, parm, arg);
18491 case VAR_DECL:
18492 /* A non-type template parameter that is a variable should be a
18493 an integral constant, in which case, it whould have been
18494 folded into its (constant) value. So we should not be getting
18495 a variable here. */
18496 gcc_unreachable ();
18498 case TYPE_ARGUMENT_PACK:
18499 case NONTYPE_ARGUMENT_PACK:
18500 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18501 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18503 case TYPEOF_TYPE:
18504 case DECLTYPE_TYPE:
18505 case UNDERLYING_TYPE:
18506 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18507 or UNDERLYING_TYPE nodes. */
18508 return unify_success (explain_p);
18510 case ERROR_MARK:
18511 /* Unification fails if we hit an error node. */
18512 return unify_invalid (explain_p);
18514 case INDIRECT_REF:
18515 if (REFERENCE_REF_P (parm))
18517 if (REFERENCE_REF_P (arg))
18518 arg = TREE_OPERAND (arg, 0);
18519 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18520 strict, explain_p);
18522 /* FALLTHRU */
18524 default:
18525 /* An unresolved overload is a nondeduced context. */
18526 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18527 return unify_success (explain_p);
18528 gcc_assert (EXPR_P (parm));
18530 /* We must be looking at an expression. This can happen with
18531 something like:
18533 template <int I>
18534 void foo(S<I>, S<I + 2>);
18536 This is a "nondeduced context":
18538 [deduct.type]
18540 The nondeduced contexts are:
18542 --A type that is a template-id in which one or more of
18543 the template-arguments is an expression that references
18544 a template-parameter.
18546 In these cases, we assume deduction succeeded, but don't
18547 actually infer any unifications. */
18549 if (!uses_template_parms (parm)
18550 && !template_args_equal (parm, arg))
18551 return unify_expression_unequal (explain_p, parm, arg);
18552 else
18553 return unify_success (explain_p);
18556 #undef RECUR_AND_CHECK_FAILURE
18558 /* Note that DECL can be defined in this translation unit, if
18559 required. */
18561 static void
18562 mark_definable (tree decl)
18564 tree clone;
18565 DECL_NOT_REALLY_EXTERN (decl) = 1;
18566 FOR_EACH_CLONE (clone, decl)
18567 DECL_NOT_REALLY_EXTERN (clone) = 1;
18570 /* Called if RESULT is explicitly instantiated, or is a member of an
18571 explicitly instantiated class. */
18573 void
18574 mark_decl_instantiated (tree result, int extern_p)
18576 SET_DECL_EXPLICIT_INSTANTIATION (result);
18578 /* If this entity has already been written out, it's too late to
18579 make any modifications. */
18580 if (TREE_ASM_WRITTEN (result))
18581 return;
18583 /* For anonymous namespace we don't need to do anything. */
18584 if (decl_anon_ns_mem_p (result))
18586 gcc_assert (!TREE_PUBLIC (result));
18587 return;
18590 if (TREE_CODE (result) != FUNCTION_DECL)
18591 /* The TREE_PUBLIC flag for function declarations will have been
18592 set correctly by tsubst. */
18593 TREE_PUBLIC (result) = 1;
18595 /* This might have been set by an earlier implicit instantiation. */
18596 DECL_COMDAT (result) = 0;
18598 if (extern_p)
18599 DECL_NOT_REALLY_EXTERN (result) = 0;
18600 else
18602 mark_definable (result);
18603 mark_needed (result);
18604 /* Always make artificials weak. */
18605 if (DECL_ARTIFICIAL (result) && flag_weak)
18606 comdat_linkage (result);
18607 /* For WIN32 we also want to put explicit instantiations in
18608 linkonce sections. */
18609 else if (TREE_PUBLIC (result))
18610 maybe_make_one_only (result);
18613 /* If EXTERN_P, then this function will not be emitted -- unless
18614 followed by an explicit instantiation, at which point its linkage
18615 will be adjusted. If !EXTERN_P, then this function will be
18616 emitted here. In neither circumstance do we want
18617 import_export_decl to adjust the linkage. */
18618 DECL_INTERFACE_KNOWN (result) = 1;
18621 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18622 important template arguments. If any are missing, we check whether
18623 they're important by using error_mark_node for substituting into any
18624 args that were used for partial ordering (the ones between ARGS and END)
18625 and seeing if it bubbles up. */
18627 static bool
18628 check_undeduced_parms (tree targs, tree args, tree end)
18630 bool found = false;
18631 int i;
18632 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18633 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18635 found = true;
18636 TREE_VEC_ELT (targs, i) = error_mark_node;
18638 if (found)
18640 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18641 if (substed == error_mark_node)
18642 return true;
18644 return false;
18647 /* Given two function templates PAT1 and PAT2, return:
18649 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18650 -1 if PAT2 is more specialized than PAT1.
18651 0 if neither is more specialized.
18653 LEN indicates the number of parameters we should consider
18654 (defaulted parameters should not be considered).
18656 The 1998 std underspecified function template partial ordering, and
18657 DR214 addresses the issue. We take pairs of arguments, one from
18658 each of the templates, and deduce them against each other. One of
18659 the templates will be more specialized if all the *other*
18660 template's arguments deduce against its arguments and at least one
18661 of its arguments *does* *not* deduce against the other template's
18662 corresponding argument. Deduction is done as for class templates.
18663 The arguments used in deduction have reference and top level cv
18664 qualifiers removed. Iff both arguments were originally reference
18665 types *and* deduction succeeds in both directions, an lvalue reference
18666 wins against an rvalue reference and otherwise the template
18667 with the more cv-qualified argument wins for that pairing (if
18668 neither is more cv-qualified, they both are equal). Unlike regular
18669 deduction, after all the arguments have been deduced in this way,
18670 we do *not* verify the deduced template argument values can be
18671 substituted into non-deduced contexts.
18673 The logic can be a bit confusing here, because we look at deduce1 and
18674 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18675 can find template arguments for pat1 to make arg1 look like arg2, that
18676 means that arg2 is at least as specialized as arg1. */
18679 more_specialized_fn (tree pat1, tree pat2, int len)
18681 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18682 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18683 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18684 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18685 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18686 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18687 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18688 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18689 tree origs1, origs2;
18690 bool lose1 = false;
18691 bool lose2 = false;
18693 /* Remove the this parameter from non-static member functions. If
18694 one is a non-static member function and the other is not a static
18695 member function, remove the first parameter from that function
18696 also. This situation occurs for operator functions where we
18697 locate both a member function (with this pointer) and non-member
18698 operator (with explicit first operand). */
18699 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18701 len--; /* LEN is the number of significant arguments for DECL1 */
18702 args1 = TREE_CHAIN (args1);
18703 if (!DECL_STATIC_FUNCTION_P (decl2))
18704 args2 = TREE_CHAIN (args2);
18706 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18708 args2 = TREE_CHAIN (args2);
18709 if (!DECL_STATIC_FUNCTION_P (decl1))
18711 len--;
18712 args1 = TREE_CHAIN (args1);
18716 /* If only one is a conversion operator, they are unordered. */
18717 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18718 return 0;
18720 /* Consider the return type for a conversion function */
18721 if (DECL_CONV_FN_P (decl1))
18723 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18724 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18725 len++;
18728 processing_template_decl++;
18730 origs1 = args1;
18731 origs2 = args2;
18733 while (len--
18734 /* Stop when an ellipsis is seen. */
18735 && args1 != NULL_TREE && args2 != NULL_TREE)
18737 tree arg1 = TREE_VALUE (args1);
18738 tree arg2 = TREE_VALUE (args2);
18739 int deduce1, deduce2;
18740 int quals1 = -1;
18741 int quals2 = -1;
18742 int ref1 = 0;
18743 int ref2 = 0;
18745 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18746 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18748 /* When both arguments are pack expansions, we need only
18749 unify the patterns themselves. */
18750 arg1 = PACK_EXPANSION_PATTERN (arg1);
18751 arg2 = PACK_EXPANSION_PATTERN (arg2);
18753 /* This is the last comparison we need to do. */
18754 len = 0;
18757 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18759 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18760 arg1 = TREE_TYPE (arg1);
18761 quals1 = cp_type_quals (arg1);
18764 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18766 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18767 arg2 = TREE_TYPE (arg2);
18768 quals2 = cp_type_quals (arg2);
18771 arg1 = TYPE_MAIN_VARIANT (arg1);
18772 arg2 = TYPE_MAIN_VARIANT (arg2);
18774 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18776 int i, len2 = list_length (args2);
18777 tree parmvec = make_tree_vec (1);
18778 tree argvec = make_tree_vec (len2);
18779 tree ta = args2;
18781 /* Setup the parameter vector, which contains only ARG1. */
18782 TREE_VEC_ELT (parmvec, 0) = arg1;
18784 /* Setup the argument vector, which contains the remaining
18785 arguments. */
18786 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18787 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18789 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18790 argvec, DEDUCE_EXACT,
18791 /*subr=*/true, /*explain_p=*/false)
18792 == 0);
18794 /* We cannot deduce in the other direction, because ARG1 is
18795 a pack expansion but ARG2 is not. */
18796 deduce2 = 0;
18798 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18800 int i, len1 = list_length (args1);
18801 tree parmvec = make_tree_vec (1);
18802 tree argvec = make_tree_vec (len1);
18803 tree ta = args1;
18805 /* Setup the parameter vector, which contains only ARG1. */
18806 TREE_VEC_ELT (parmvec, 0) = arg2;
18808 /* Setup the argument vector, which contains the remaining
18809 arguments. */
18810 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18811 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18813 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18814 argvec, DEDUCE_EXACT,
18815 /*subr=*/true, /*explain_p=*/false)
18816 == 0);
18818 /* We cannot deduce in the other direction, because ARG2 is
18819 a pack expansion but ARG1 is not.*/
18820 deduce1 = 0;
18823 else
18825 /* The normal case, where neither argument is a pack
18826 expansion. */
18827 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18828 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18829 == 0);
18830 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18831 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18832 == 0);
18835 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18836 arg2, then arg2 is not as specialized as arg1. */
18837 if (!deduce1)
18838 lose2 = true;
18839 if (!deduce2)
18840 lose1 = true;
18842 /* "If, for a given type, deduction succeeds in both directions
18843 (i.e., the types are identical after the transformations above)
18844 and both P and A were reference types (before being replaced with
18845 the type referred to above):
18846 - if the type from the argument template was an lvalue reference and
18847 the type from the parameter template was not, the argument type is
18848 considered to be more specialized than the other; otherwise,
18849 - if the type from the argument template is more cv-qualified
18850 than the type from the parameter template (as described above),
18851 the argument type is considered to be more specialized than the other;
18852 otherwise,
18853 - neither type is more specialized than the other." */
18855 if (deduce1 && deduce2)
18857 if (ref1 && ref2 && ref1 != ref2)
18859 if (ref1 > ref2)
18860 lose1 = true;
18861 else
18862 lose2 = true;
18864 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18866 if ((quals1 & quals2) == quals2)
18867 lose2 = true;
18868 if ((quals1 & quals2) == quals1)
18869 lose1 = true;
18873 if (lose1 && lose2)
18874 /* We've failed to deduce something in either direction.
18875 These must be unordered. */
18876 break;
18878 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18879 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18880 /* We have already processed all of the arguments in our
18881 handing of the pack expansion type. */
18882 len = 0;
18884 args1 = TREE_CHAIN (args1);
18885 args2 = TREE_CHAIN (args2);
18888 /* "In most cases, all template parameters must have values in order for
18889 deduction to succeed, but for partial ordering purposes a template
18890 parameter may remain without a value provided it is not used in the
18891 types being used for partial ordering."
18893 Thus, if we are missing any of the targs1 we need to substitute into
18894 origs1, then pat2 is not as specialized as pat1. This can happen when
18895 there is a nondeduced context. */
18896 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18897 lose2 = true;
18898 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18899 lose1 = true;
18901 processing_template_decl--;
18903 /* All things being equal, if the next argument is a pack expansion
18904 for one function but not for the other, prefer the
18905 non-variadic function. FIXME this is bogus; see c++/41958. */
18906 if (lose1 == lose2
18907 && args1 && TREE_VALUE (args1)
18908 && args2 && TREE_VALUE (args2))
18910 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18911 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18914 if (lose1 == lose2)
18915 return 0;
18916 else if (!lose1)
18917 return 1;
18918 else
18919 return -1;
18922 /* Determine which of two partial specializations of TMPL is more
18923 specialized.
18925 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
18926 to the first partial specialization. The TREE_PURPOSE is the
18927 innermost set of template parameters for the partial
18928 specialization. PAT2 is similar, but for the second template.
18930 Return 1 if the first partial specialization is more specialized;
18931 -1 if the second is more specialized; 0 if neither is more
18932 specialized.
18934 See [temp.class.order] for information about determining which of
18935 two templates is more specialized. */
18937 static int
18938 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
18940 tree targs;
18941 int winner = 0;
18942 bool any_deductions = false;
18944 tree tmpl1 = TREE_VALUE (pat1);
18945 tree tmpl2 = TREE_VALUE (pat2);
18946 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
18947 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
18948 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
18949 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
18951 /* Just like what happens for functions, if we are ordering between
18952 different template specializations, we may encounter dependent
18953 types in the arguments, and we need our dependency check functions
18954 to behave correctly. */
18955 ++processing_template_decl;
18956 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
18957 if (targs)
18959 --winner;
18960 any_deductions = true;
18963 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
18964 if (targs)
18966 ++winner;
18967 any_deductions = true;
18969 --processing_template_decl;
18971 /* In the case of a tie where at least one of the templates
18972 has a parameter pack at the end, the template with the most
18973 non-packed parameters wins. */
18974 if (winner == 0
18975 && any_deductions
18976 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18977 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18979 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18980 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18981 int len1 = TREE_VEC_LENGTH (args1);
18982 int len2 = TREE_VEC_LENGTH (args2);
18984 /* We don't count the pack expansion at the end. */
18985 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18986 --len1;
18987 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18988 --len2;
18990 if (len1 > len2)
18991 return 1;
18992 else if (len1 < len2)
18993 return -1;
18996 return winner;
18999 /* Return the template arguments that will produce the function signature
19000 DECL from the function template FN, with the explicit template
19001 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19002 also match. Return NULL_TREE if no satisfactory arguments could be
19003 found. */
19005 static tree
19006 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
19008 int ntparms = DECL_NTPARMS (fn);
19009 tree targs = make_tree_vec (ntparms);
19010 tree decl_type = TREE_TYPE (decl);
19011 tree decl_arg_types;
19012 tree *args;
19013 unsigned int nargs, ix;
19014 tree arg;
19016 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
19018 /* Never do unification on the 'this' parameter. */
19019 decl_arg_types = skip_artificial_parms_for (decl,
19020 TYPE_ARG_TYPES (decl_type));
19022 nargs = list_length (decl_arg_types);
19023 args = XALLOCAVEC (tree, nargs);
19024 for (arg = decl_arg_types, ix = 0;
19025 arg != NULL_TREE && arg != void_list_node;
19026 arg = TREE_CHAIN (arg), ++ix)
19027 args[ix] = TREE_VALUE (arg);
19029 if (fn_type_unification (fn, explicit_args, targs,
19030 args, ix,
19031 (check_rettype || DECL_CONV_FN_P (fn)
19032 ? TREE_TYPE (decl_type) : NULL_TREE),
19033 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
19034 /*decltype*/false)
19035 == error_mark_node)
19036 return NULL_TREE;
19038 return targs;
19041 /* Return the innermost template arguments that, when applied to a partial
19042 specialization of TMPL whose innermost template parameters are
19043 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19044 ARGS.
19046 For example, suppose we have:
19048 template <class T, class U> struct S {};
19049 template <class T> struct S<T*, int> {};
19051 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19052 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19053 int}. The resulting vector will be {double}, indicating that `T'
19054 is bound to `double'. */
19056 static tree
19057 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
19059 int i, ntparms = TREE_VEC_LENGTH (tparms);
19060 tree deduced_args;
19061 tree innermost_deduced_args;
19063 innermost_deduced_args = make_tree_vec (ntparms);
19064 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19066 deduced_args = copy_node (args);
19067 SET_TMPL_ARGS_LEVEL (deduced_args,
19068 TMPL_ARGS_DEPTH (deduced_args),
19069 innermost_deduced_args);
19071 else
19072 deduced_args = innermost_deduced_args;
19074 if (unify (tparms, deduced_args,
19075 INNERMOST_TEMPLATE_ARGS (spec_args),
19076 INNERMOST_TEMPLATE_ARGS (args),
19077 UNIFY_ALLOW_NONE, /*explain_p=*/false))
19078 return NULL_TREE;
19080 for (i = 0; i < ntparms; ++i)
19081 if (! TREE_VEC_ELT (innermost_deduced_args, i))
19082 return NULL_TREE;
19084 /* Verify that nondeduced template arguments agree with the type
19085 obtained from argument deduction.
19087 For example:
19089 struct A { typedef int X; };
19090 template <class T, class U> struct C {};
19091 template <class T> struct C<T, typename T::X> {};
19093 Then with the instantiation `C<A, int>', we can deduce that
19094 `T' is `A' but unify () does not check whether `typename T::X'
19095 is `int'. */
19096 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
19097 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19098 spec_args, tmpl,
19099 tf_none, false, false);
19100 if (spec_args == error_mark_node
19101 /* We only need to check the innermost arguments; the other
19102 arguments will always agree. */
19103 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
19104 INNERMOST_TEMPLATE_ARGS (args)))
19105 return NULL_TREE;
19107 /* Now that we have bindings for all of the template arguments,
19108 ensure that the arguments deduced for the template template
19109 parameters have compatible template parameter lists. See the use
19110 of template_template_parm_bindings_ok_p in fn_type_unification
19111 for more information. */
19112 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
19113 return NULL_TREE;
19115 return deduced_args;
19118 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19119 Return the TREE_LIST node with the most specialized template, if
19120 any. If there is no most specialized template, the error_mark_node
19121 is returned.
19123 Note that this function does not look at, or modify, the
19124 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19125 returned is one of the elements of INSTANTIATIONS, callers may
19126 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19127 and retrieve it from the value returned. */
19129 tree
19130 most_specialized_instantiation (tree templates)
19132 tree fn, champ;
19134 ++processing_template_decl;
19136 champ = templates;
19137 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
19139 int fate = 0;
19141 if (get_bindings (TREE_VALUE (champ),
19142 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19143 NULL_TREE, /*check_ret=*/true))
19144 fate--;
19146 if (get_bindings (TREE_VALUE (fn),
19147 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19148 NULL_TREE, /*check_ret=*/true))
19149 fate++;
19151 if (fate == -1)
19152 champ = fn;
19153 else if (!fate)
19155 /* Equally specialized, move to next function. If there
19156 is no next function, nothing's most specialized. */
19157 fn = TREE_CHAIN (fn);
19158 champ = fn;
19159 if (!fn)
19160 break;
19164 if (champ)
19165 /* Now verify that champ is better than everything earlier in the
19166 instantiation list. */
19167 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
19168 if (get_bindings (TREE_VALUE (champ),
19169 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19170 NULL_TREE, /*check_ret=*/true)
19171 || !get_bindings (TREE_VALUE (fn),
19172 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19173 NULL_TREE, /*check_ret=*/true))
19175 champ = NULL_TREE;
19176 break;
19179 processing_template_decl--;
19181 if (!champ)
19182 return error_mark_node;
19184 return champ;
19187 /* If DECL is a specialization of some template, return the most
19188 general such template. Otherwise, returns NULL_TREE.
19190 For example, given:
19192 template <class T> struct S { template <class U> void f(U); };
19194 if TMPL is `template <class U> void S<int>::f(U)' this will return
19195 the full template. This function will not trace past partial
19196 specializations, however. For example, given in addition:
19198 template <class T> struct S<T*> { template <class U> void f(U); };
19200 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19201 `template <class T> template <class U> S<T*>::f(U)'. */
19203 tree
19204 most_general_template (tree decl)
19206 if (TREE_CODE (decl) != TEMPLATE_DECL)
19208 if (tree tinfo = get_template_info (decl))
19209 decl = TI_TEMPLATE (tinfo);
19210 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19211 template friend, or a FIELD_DECL for a capture pack. */
19212 if (TREE_CODE (decl) != TEMPLATE_DECL)
19213 return NULL_TREE;
19216 /* Look for more and more general templates. */
19217 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
19219 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19220 (See cp-tree.h for details.) */
19221 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
19222 break;
19224 if (CLASS_TYPE_P (TREE_TYPE (decl))
19225 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
19226 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19227 break;
19229 /* Stop if we run into an explicitly specialized class template. */
19230 if (!DECL_NAMESPACE_SCOPE_P (decl)
19231 && DECL_CONTEXT (decl)
19232 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
19233 break;
19235 decl = DECL_TI_TEMPLATE (decl);
19238 return decl;
19241 /* Return the most specialized of the template partial specializations
19242 which can produce TARGET, a specialization of some class or variable
19243 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19244 a TEMPLATE_DECL node corresponding to the partial specialization, while
19245 the TREE_PURPOSE is the set of template arguments that must be
19246 substituted into the template pattern in order to generate TARGET.
19248 If the choice of partial specialization is ambiguous, a diagnostic
19249 is issued, and the error_mark_node is returned. If there are no
19250 partial specializations matching TARGET, then NULL_TREE is
19251 returned, indicating that the primary template should be used. */
19253 static tree
19254 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
19256 tree list = NULL_TREE;
19257 tree t;
19258 tree champ;
19259 int fate;
19260 bool ambiguous_p;
19261 tree outer_args = NULL_TREE;
19262 tree tmpl, args;
19264 if (TYPE_P (target))
19266 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
19267 tmpl = TI_TEMPLATE (tinfo);
19268 args = TI_ARGS (tinfo);
19270 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
19272 tmpl = TREE_OPERAND (target, 0);
19273 args = TREE_OPERAND (target, 1);
19275 else if (VAR_P (target))
19277 tree tinfo = DECL_TEMPLATE_INFO (target);
19278 tmpl = TI_TEMPLATE (tinfo);
19279 args = TI_ARGS (tinfo);
19281 else
19282 gcc_unreachable ();
19284 tree main_tmpl = most_general_template (tmpl);
19286 /* For determining which partial specialization to use, only the
19287 innermost args are interesting. */
19288 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19290 outer_args = strip_innermost_template_args (args, 1);
19291 args = INNERMOST_TEMPLATE_ARGS (args);
19294 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
19296 tree partial_spec_args;
19297 tree spec_args;
19298 tree spec_tmpl = TREE_VALUE (t);
19300 partial_spec_args = TREE_PURPOSE (t);
19302 ++processing_template_decl;
19304 if (outer_args)
19306 /* Discard the outer levels of args, and then substitute in the
19307 template args from the enclosing class. */
19308 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19309 partial_spec_args = tsubst_template_args
19310 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19312 /* And the same for the partial specialization TEMPLATE_DECL. */
19313 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19316 partial_spec_args =
19317 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19318 partial_spec_args,
19319 tmpl, tf_none,
19320 /*require_all_args=*/true,
19321 /*use_default_args=*/true);
19323 --processing_template_decl;
19325 if (partial_spec_args == error_mark_node)
19326 return error_mark_node;
19327 if (spec_tmpl == error_mark_node)
19328 return error_mark_node;
19330 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19331 spec_args = get_partial_spec_bindings (tmpl, parms,
19332 partial_spec_args,
19333 args);
19334 if (spec_args)
19336 if (outer_args)
19337 spec_args = add_to_template_args (outer_args, spec_args);
19338 list = tree_cons (spec_args, TREE_VALUE (t), list);
19339 TREE_TYPE (list) = TREE_TYPE (t);
19343 if (! list)
19344 return NULL_TREE;
19346 ambiguous_p = false;
19347 t = list;
19348 champ = t;
19349 t = TREE_CHAIN (t);
19350 for (; t; t = TREE_CHAIN (t))
19352 fate = more_specialized_partial_spec (tmpl, champ, t);
19353 if (fate == 1)
19355 else
19357 if (fate == 0)
19359 t = TREE_CHAIN (t);
19360 if (! t)
19362 ambiguous_p = true;
19363 break;
19366 champ = t;
19370 if (!ambiguous_p)
19371 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19373 fate = more_specialized_partial_spec (tmpl, champ, t);
19374 if (fate != 1)
19376 ambiguous_p = true;
19377 break;
19381 if (ambiguous_p)
19383 const char *str;
19384 char *spaces = NULL;
19385 if (!(complain & tf_error))
19386 return error_mark_node;
19387 if (TYPE_P (target))
19388 error ("ambiguous template instantiation for %q#T", target);
19389 else
19390 error ("ambiguous template instantiation for %q#D", target);
19391 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19392 for (t = list; t; t = TREE_CHAIN (t))
19394 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
19395 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
19396 "%s %#S", spaces ? spaces : str, subst);
19397 spaces = spaces ? spaces : get_spaces (str);
19399 free (spaces);
19400 return error_mark_node;
19403 return champ;
19406 /* Explicitly instantiate DECL. */
19408 void
19409 do_decl_instantiation (tree decl, tree storage)
19411 tree result = NULL_TREE;
19412 int extern_p = 0;
19414 if (!decl || decl == error_mark_node)
19415 /* An error occurred, for which grokdeclarator has already issued
19416 an appropriate message. */
19417 return;
19418 else if (! DECL_LANG_SPECIFIC (decl))
19420 error ("explicit instantiation of non-template %q#D", decl);
19421 return;
19424 bool var_templ = (DECL_TEMPLATE_INFO (decl)
19425 && variable_template_p (DECL_TI_TEMPLATE (decl)));
19427 if (VAR_P (decl) && !var_templ)
19429 /* There is an asymmetry here in the way VAR_DECLs and
19430 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19431 the latter, the DECL we get back will be marked as a
19432 template instantiation, and the appropriate
19433 DECL_TEMPLATE_INFO will be set up. This does not happen for
19434 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19435 should handle VAR_DECLs as it currently handles
19436 FUNCTION_DECLs. */
19437 if (!DECL_CLASS_SCOPE_P (decl))
19439 error ("%qD is not a static data member of a class template", decl);
19440 return;
19442 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19443 if (!result || !VAR_P (result))
19445 error ("no matching template for %qD found", decl);
19446 return;
19448 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19450 error ("type %qT for explicit instantiation %qD does not match "
19451 "declared type %qT", TREE_TYPE (result), decl,
19452 TREE_TYPE (decl));
19453 return;
19456 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
19458 error ("explicit instantiation of %q#D", decl);
19459 return;
19461 else
19462 result = decl;
19464 /* Check for various error cases. Note that if the explicit
19465 instantiation is valid the RESULT will currently be marked as an
19466 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19467 until we get here. */
19469 if (DECL_TEMPLATE_SPECIALIZATION (result))
19471 /* DR 259 [temp.spec].
19473 Both an explicit instantiation and a declaration of an explicit
19474 specialization shall not appear in a program unless the explicit
19475 instantiation follows a declaration of the explicit specialization.
19477 For a given set of template parameters, if an explicit
19478 instantiation of a template appears after a declaration of an
19479 explicit specialization for that template, the explicit
19480 instantiation has no effect. */
19481 return;
19483 else if (DECL_EXPLICIT_INSTANTIATION (result))
19485 /* [temp.spec]
19487 No program shall explicitly instantiate any template more
19488 than once.
19490 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19491 the first instantiation was `extern' and the second is not,
19492 and EXTERN_P for the opposite case. */
19493 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19494 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19495 /* If an "extern" explicit instantiation follows an ordinary
19496 explicit instantiation, the template is instantiated. */
19497 if (extern_p)
19498 return;
19500 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19502 error ("no matching template for %qD found", result);
19503 return;
19505 else if (!DECL_TEMPLATE_INFO (result))
19507 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19508 return;
19511 if (storage == NULL_TREE)
19513 else if (storage == ridpointers[(int) RID_EXTERN])
19515 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19516 pedwarn (input_location, OPT_Wpedantic,
19517 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19518 "instantiations");
19519 extern_p = 1;
19521 else
19522 error ("storage class %qD applied to template instantiation", storage);
19524 check_explicit_instantiation_namespace (result);
19525 mark_decl_instantiated (result, extern_p);
19526 if (! extern_p)
19527 instantiate_decl (result, /*defer_ok=*/1,
19528 /*expl_inst_class_mem_p=*/false);
19531 static void
19532 mark_class_instantiated (tree t, int extern_p)
19534 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19535 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19536 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19537 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19538 if (! extern_p)
19540 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19541 rest_of_type_compilation (t, 1);
19545 /* Called from do_type_instantiation through binding_table_foreach to
19546 do recursive instantiation for the type bound in ENTRY. */
19547 static void
19548 bt_instantiate_type_proc (binding_entry entry, void *data)
19550 tree storage = *(tree *) data;
19552 if (MAYBE_CLASS_TYPE_P (entry->type)
19553 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19554 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19557 /* Called from do_type_instantiation to instantiate a member
19558 (a member function or a static member variable) of an
19559 explicitly instantiated class template. */
19560 static void
19561 instantiate_class_member (tree decl, int extern_p)
19563 mark_decl_instantiated (decl, extern_p);
19564 if (! extern_p)
19565 instantiate_decl (decl, /*defer_ok=*/1,
19566 /*expl_inst_class_mem_p=*/true);
19569 /* Perform an explicit instantiation of template class T. STORAGE, if
19570 non-null, is the RID for extern, inline or static. COMPLAIN is
19571 nonzero if this is called from the parser, zero if called recursively,
19572 since the standard is unclear (as detailed below). */
19574 void
19575 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19577 int extern_p = 0;
19578 int nomem_p = 0;
19579 int static_p = 0;
19580 int previous_instantiation_extern_p = 0;
19582 if (TREE_CODE (t) == TYPE_DECL)
19583 t = TREE_TYPE (t);
19585 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19587 tree tmpl =
19588 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19589 if (tmpl)
19590 error ("explicit instantiation of non-class template %qD", tmpl);
19591 else
19592 error ("explicit instantiation of non-template type %qT", t);
19593 return;
19596 complete_type (t);
19598 if (!COMPLETE_TYPE_P (t))
19600 if (complain & tf_error)
19601 error ("explicit instantiation of %q#T before definition of template",
19603 return;
19606 if (storage != NULL_TREE)
19608 if (!in_system_header_at (input_location))
19610 if (storage == ridpointers[(int) RID_EXTERN])
19612 if (cxx_dialect == cxx98)
19613 pedwarn (input_location, OPT_Wpedantic,
19614 "ISO C++ 1998 forbids the use of %<extern%> on "
19615 "explicit instantiations");
19617 else
19618 pedwarn (input_location, OPT_Wpedantic,
19619 "ISO C++ forbids the use of %qE"
19620 " on explicit instantiations", storage);
19623 if (storage == ridpointers[(int) RID_INLINE])
19624 nomem_p = 1;
19625 else if (storage == ridpointers[(int) RID_EXTERN])
19626 extern_p = 1;
19627 else if (storage == ridpointers[(int) RID_STATIC])
19628 static_p = 1;
19629 else
19631 error ("storage class %qD applied to template instantiation",
19632 storage);
19633 extern_p = 0;
19637 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19639 /* DR 259 [temp.spec].
19641 Both an explicit instantiation and a declaration of an explicit
19642 specialization shall not appear in a program unless the explicit
19643 instantiation follows a declaration of the explicit specialization.
19645 For a given set of template parameters, if an explicit
19646 instantiation of a template appears after a declaration of an
19647 explicit specialization for that template, the explicit
19648 instantiation has no effect. */
19649 return;
19651 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19653 /* [temp.spec]
19655 No program shall explicitly instantiate any template more
19656 than once.
19658 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19659 instantiation was `extern'. If EXTERN_P then the second is.
19660 These cases are OK. */
19661 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19663 if (!previous_instantiation_extern_p && !extern_p
19664 && (complain & tf_error))
19665 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19667 /* If we've already instantiated the template, just return now. */
19668 if (!CLASSTYPE_INTERFACE_ONLY (t))
19669 return;
19672 check_explicit_instantiation_namespace (TYPE_NAME (t));
19673 mark_class_instantiated (t, extern_p);
19675 if (nomem_p)
19676 return;
19679 tree tmp;
19681 /* In contrast to implicit instantiation, where only the
19682 declarations, and not the definitions, of members are
19683 instantiated, we have here:
19685 [temp.explicit]
19687 The explicit instantiation of a class template specialization
19688 implies the instantiation of all of its members not
19689 previously explicitly specialized in the translation unit
19690 containing the explicit instantiation.
19692 Of course, we can't instantiate member template classes, since
19693 we don't have any arguments for them. Note that the standard
19694 is unclear on whether the instantiation of the members are
19695 *explicit* instantiations or not. However, the most natural
19696 interpretation is that it should be an explicit instantiation. */
19698 if (! static_p)
19699 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19700 if (TREE_CODE (tmp) == FUNCTION_DECL
19701 && DECL_TEMPLATE_INSTANTIATION (tmp))
19702 instantiate_class_member (tmp, extern_p);
19704 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19705 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19706 instantiate_class_member (tmp, extern_p);
19708 if (CLASSTYPE_NESTED_UTDS (t))
19709 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19710 bt_instantiate_type_proc, &storage);
19714 /* Given a function DECL, which is a specialization of TMPL, modify
19715 DECL to be a re-instantiation of TMPL with the same template
19716 arguments. TMPL should be the template into which tsubst'ing
19717 should occur for DECL, not the most general template.
19719 One reason for doing this is a scenario like this:
19721 template <class T>
19722 void f(const T&, int i);
19724 void g() { f(3, 7); }
19726 template <class T>
19727 void f(const T& t, const int i) { }
19729 Note that when the template is first instantiated, with
19730 instantiate_template, the resulting DECL will have no name for the
19731 first parameter, and the wrong type for the second. So, when we go
19732 to instantiate the DECL, we regenerate it. */
19734 static void
19735 regenerate_decl_from_template (tree decl, tree tmpl)
19737 /* The arguments used to instantiate DECL, from the most general
19738 template. */
19739 tree args;
19740 tree code_pattern;
19742 args = DECL_TI_ARGS (decl);
19743 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19745 /* Make sure that we can see identifiers, and compute access
19746 correctly. */
19747 push_access_scope (decl);
19749 if (TREE_CODE (decl) == FUNCTION_DECL)
19751 tree decl_parm;
19752 tree pattern_parm;
19753 tree specs;
19754 int args_depth;
19755 int parms_depth;
19757 args_depth = TMPL_ARGS_DEPTH (args);
19758 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19759 if (args_depth > parms_depth)
19760 args = get_innermost_template_args (args, parms_depth);
19762 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19763 args, tf_error, NULL_TREE,
19764 /*defer_ok*/false);
19765 if (specs && specs != error_mark_node)
19766 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19767 specs);
19769 /* Merge parameter declarations. */
19770 decl_parm = skip_artificial_parms_for (decl,
19771 DECL_ARGUMENTS (decl));
19772 pattern_parm
19773 = skip_artificial_parms_for (code_pattern,
19774 DECL_ARGUMENTS (code_pattern));
19775 while (decl_parm && !DECL_PACK_P (pattern_parm))
19777 tree parm_type;
19778 tree attributes;
19780 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19781 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19782 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19783 NULL_TREE);
19784 parm_type = type_decays_to (parm_type);
19785 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19786 TREE_TYPE (decl_parm) = parm_type;
19787 attributes = DECL_ATTRIBUTES (pattern_parm);
19788 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19790 DECL_ATTRIBUTES (decl_parm) = attributes;
19791 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19793 decl_parm = DECL_CHAIN (decl_parm);
19794 pattern_parm = DECL_CHAIN (pattern_parm);
19796 /* Merge any parameters that match with the function parameter
19797 pack. */
19798 if (pattern_parm && DECL_PACK_P (pattern_parm))
19800 int i, len;
19801 tree expanded_types;
19802 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19803 the parameters in this function parameter pack. */
19804 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19805 args, tf_error, NULL_TREE);
19806 len = TREE_VEC_LENGTH (expanded_types);
19807 for (i = 0; i < len; i++)
19809 tree parm_type;
19810 tree attributes;
19812 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19813 /* Rename the parameter to include the index. */
19814 DECL_NAME (decl_parm) =
19815 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19816 parm_type = TREE_VEC_ELT (expanded_types, i);
19817 parm_type = type_decays_to (parm_type);
19818 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19819 TREE_TYPE (decl_parm) = parm_type;
19820 attributes = DECL_ATTRIBUTES (pattern_parm);
19821 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19823 DECL_ATTRIBUTES (decl_parm) = attributes;
19824 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19826 decl_parm = DECL_CHAIN (decl_parm);
19829 /* Merge additional specifiers from the CODE_PATTERN. */
19830 if (DECL_DECLARED_INLINE_P (code_pattern)
19831 && !DECL_DECLARED_INLINE_P (decl))
19832 DECL_DECLARED_INLINE_P (decl) = 1;
19834 else if (VAR_P (decl))
19836 DECL_INITIAL (decl) =
19837 tsubst_expr (DECL_INITIAL (code_pattern), args,
19838 tf_error, DECL_TI_TEMPLATE (decl),
19839 /*integral_constant_expression_p=*/false);
19840 if (VAR_HAD_UNKNOWN_BOUND (decl))
19841 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19842 tf_error, DECL_TI_TEMPLATE (decl));
19844 else
19845 gcc_unreachable ();
19847 pop_access_scope (decl);
19850 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19851 substituted to get DECL. */
19853 tree
19854 template_for_substitution (tree decl)
19856 tree tmpl = DECL_TI_TEMPLATE (decl);
19858 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19859 for the instantiation. This is not always the most general
19860 template. Consider, for example:
19862 template <class T>
19863 struct S { template <class U> void f();
19864 template <> void f<int>(); };
19866 and an instantiation of S<double>::f<int>. We want TD to be the
19867 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19868 while (/* An instantiation cannot have a definition, so we need a
19869 more general template. */
19870 DECL_TEMPLATE_INSTANTIATION (tmpl)
19871 /* We must also deal with friend templates. Given:
19873 template <class T> struct S {
19874 template <class U> friend void f() {};
19877 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19878 so far as the language is concerned, but that's still
19879 where we get the pattern for the instantiation from. On
19880 other hand, if the definition comes outside the class, say:
19882 template <class T> struct S {
19883 template <class U> friend void f();
19885 template <class U> friend void f() {}
19887 we don't need to look any further. That's what the check for
19888 DECL_INITIAL is for. */
19889 || (TREE_CODE (decl) == FUNCTION_DECL
19890 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19891 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19893 /* The present template, TD, should not be a definition. If it
19894 were a definition, we should be using it! Note that we
19895 cannot restructure the loop to just keep going until we find
19896 a template with a definition, since that might go too far if
19897 a specialization was declared, but not defined. */
19899 /* Fetch the more general template. */
19900 tmpl = DECL_TI_TEMPLATE (tmpl);
19903 return tmpl;
19906 /* Returns true if we need to instantiate this template instance even if we
19907 know we aren't going to emit it.. */
19909 bool
19910 always_instantiate_p (tree decl)
19912 /* We always instantiate inline functions so that we can inline them. An
19913 explicit instantiation declaration prohibits implicit instantiation of
19914 non-inline functions. With high levels of optimization, we would
19915 normally inline non-inline functions -- but we're not allowed to do
19916 that for "extern template" functions. Therefore, we check
19917 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19918 return ((TREE_CODE (decl) == FUNCTION_DECL
19919 && (DECL_DECLARED_INLINE_P (decl)
19920 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19921 /* And we need to instantiate static data members so that
19922 their initializers are available in integral constant
19923 expressions. */
19924 || (VAR_P (decl)
19925 && decl_maybe_constant_var_p (decl)));
19928 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19929 instantiate it now, modifying TREE_TYPE (fn). */
19931 void
19932 maybe_instantiate_noexcept (tree fn)
19934 tree fntype, spec, noex, clone;
19936 /* Don't instantiate a noexcept-specification from template context. */
19937 if (processing_template_decl)
19938 return;
19940 if (DECL_CLONED_FUNCTION_P (fn))
19941 fn = DECL_CLONED_FUNCTION (fn);
19942 fntype = TREE_TYPE (fn);
19943 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19945 if (!spec || !TREE_PURPOSE (spec))
19946 return;
19948 noex = TREE_PURPOSE (spec);
19950 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19952 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
19953 spec = get_defaulted_eh_spec (fn);
19954 else if (push_tinst_level (fn))
19956 push_access_scope (fn);
19957 push_deferring_access_checks (dk_no_deferred);
19958 input_location = DECL_SOURCE_LOCATION (fn);
19959 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19960 DEFERRED_NOEXCEPT_ARGS (noex),
19961 tf_warning_or_error, fn,
19962 /*function_p=*/false,
19963 /*integral_constant_expression_p=*/true);
19964 pop_deferring_access_checks ();
19965 pop_access_scope (fn);
19966 pop_tinst_level ();
19967 spec = build_noexcept_spec (noex, tf_warning_or_error);
19968 if (spec == error_mark_node)
19969 spec = noexcept_false_spec;
19971 else
19972 spec = noexcept_false_spec;
19974 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19977 FOR_EACH_CLONE (clone, fn)
19979 if (TREE_TYPE (clone) == fntype)
19980 TREE_TYPE (clone) = TREE_TYPE (fn);
19981 else
19982 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19986 /* Produce the definition of D, a _DECL generated from a template. If
19987 DEFER_OK is nonzero, then we don't have to actually do the
19988 instantiation now; we just have to do it sometime. Normally it is
19989 an error if this is an explicit instantiation but D is undefined.
19990 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19991 explicitly instantiated class template. */
19993 tree
19994 instantiate_decl (tree d, int defer_ok,
19995 bool expl_inst_class_mem_p)
19997 tree tmpl = DECL_TI_TEMPLATE (d);
19998 tree gen_args;
19999 tree args;
20000 tree td;
20001 tree code_pattern;
20002 tree spec;
20003 tree gen_tmpl;
20004 bool pattern_defined;
20005 location_t saved_loc = input_location;
20006 int saved_unevaluated_operand = cp_unevaluated_operand;
20007 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20008 bool external_p;
20009 bool deleted_p;
20010 tree fn_context;
20011 bool nested;
20013 /* This function should only be used to instantiate templates for
20014 functions and static member variables. */
20015 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
20017 /* Variables are never deferred; if instantiation is required, they
20018 are instantiated right away. That allows for better code in the
20019 case that an expression refers to the value of the variable --
20020 if the variable has a constant value the referring expression can
20021 take advantage of that fact. */
20022 if (VAR_P (d)
20023 || DECL_DECLARED_CONSTEXPR_P (d))
20024 defer_ok = 0;
20026 /* Don't instantiate cloned functions. Instead, instantiate the
20027 functions they cloned. */
20028 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
20029 d = DECL_CLONED_FUNCTION (d);
20031 if (DECL_TEMPLATE_INSTANTIATED (d)
20032 || (TREE_CODE (d) == FUNCTION_DECL
20033 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
20034 || DECL_TEMPLATE_SPECIALIZATION (d))
20035 /* D has already been instantiated or explicitly specialized, so
20036 there's nothing for us to do here.
20038 It might seem reasonable to check whether or not D is an explicit
20039 instantiation, and, if so, stop here. But when an explicit
20040 instantiation is deferred until the end of the compilation,
20041 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20042 the instantiation. */
20043 return d;
20045 /* Check to see whether we know that this template will be
20046 instantiated in some other file, as with "extern template"
20047 extension. */
20048 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
20050 /* In general, we do not instantiate such templates. */
20051 if (external_p && !always_instantiate_p (d))
20052 return d;
20054 gen_tmpl = most_general_template (tmpl);
20055 gen_args = DECL_TI_ARGS (d);
20057 if (tmpl != gen_tmpl)
20058 /* We should already have the extra args. */
20059 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
20060 == TMPL_ARGS_DEPTH (gen_args));
20061 /* And what's in the hash table should match D. */
20062 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
20063 || spec == NULL_TREE);
20065 /* This needs to happen before any tsubsting. */
20066 if (! push_tinst_level (d))
20067 return d;
20069 timevar_push (TV_TEMPLATE_INST);
20071 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20072 for the instantiation. */
20073 td = template_for_substitution (d);
20074 code_pattern = DECL_TEMPLATE_RESULT (td);
20076 /* We should never be trying to instantiate a member of a class
20077 template or partial specialization. */
20078 gcc_assert (d != code_pattern);
20080 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
20081 || DECL_TEMPLATE_SPECIALIZATION (td))
20082 /* In the case of a friend template whose definition is provided
20083 outside the class, we may have too many arguments. Drop the
20084 ones we don't need. The same is true for specializations. */
20085 args = get_innermost_template_args
20086 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
20087 else
20088 args = gen_args;
20090 if (TREE_CODE (d) == FUNCTION_DECL)
20092 deleted_p = DECL_DELETED_FN (code_pattern);
20093 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
20094 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
20095 || deleted_p);
20097 else
20099 deleted_p = false;
20100 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
20103 /* We may be in the middle of deferred access check. Disable it now. */
20104 push_deferring_access_checks (dk_no_deferred);
20106 /* Unless an explicit instantiation directive has already determined
20107 the linkage of D, remember that a definition is available for
20108 this entity. */
20109 if (pattern_defined
20110 && !DECL_INTERFACE_KNOWN (d)
20111 && !DECL_NOT_REALLY_EXTERN (d))
20112 mark_definable (d);
20114 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
20115 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
20116 input_location = DECL_SOURCE_LOCATION (d);
20118 /* If D is a member of an explicitly instantiated class template,
20119 and no definition is available, treat it like an implicit
20120 instantiation. */
20121 if (!pattern_defined && expl_inst_class_mem_p
20122 && DECL_EXPLICIT_INSTANTIATION (d))
20124 /* Leave linkage flags alone on instantiations with anonymous
20125 visibility. */
20126 if (TREE_PUBLIC (d))
20128 DECL_NOT_REALLY_EXTERN (d) = 0;
20129 DECL_INTERFACE_KNOWN (d) = 0;
20131 SET_DECL_IMPLICIT_INSTANTIATION (d);
20134 /* Defer all other templates, unless we have been explicitly
20135 forbidden from doing so. */
20136 if (/* If there is no definition, we cannot instantiate the
20137 template. */
20138 ! pattern_defined
20139 /* If it's OK to postpone instantiation, do so. */
20140 || defer_ok
20141 /* If this is a static data member that will be defined
20142 elsewhere, we don't want to instantiate the entire data
20143 member, but we do want to instantiate the initializer so that
20144 we can substitute that elsewhere. */
20145 || (external_p && VAR_P (d))
20146 /* Handle here a deleted function too, avoid generating
20147 its body (c++/61080). */
20148 || deleted_p)
20150 /* The definition of the static data member is now required so
20151 we must substitute the initializer. */
20152 if (VAR_P (d)
20153 && !DECL_INITIAL (d)
20154 && DECL_INITIAL (code_pattern))
20156 tree ns;
20157 tree init;
20158 bool const_init = false;
20159 bool enter_context = DECL_CLASS_SCOPE_P (d);
20161 ns = decl_namespace_context (d);
20162 push_nested_namespace (ns);
20163 if (enter_context)
20164 push_nested_class (DECL_CONTEXT (d));
20165 init = tsubst_expr (DECL_INITIAL (code_pattern),
20166 args,
20167 tf_warning_or_error, NULL_TREE,
20168 /*integral_constant_expression_p=*/false);
20169 /* If instantiating the initializer involved instantiating this
20170 again, don't call cp_finish_decl twice. */
20171 if (!DECL_INITIAL (d))
20173 /* Make sure the initializer is still constant, in case of
20174 circular dependency (template/instantiate6.C). */
20175 const_init
20176 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20177 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
20178 /*asmspec_tree=*/NULL_TREE,
20179 LOOKUP_ONLYCONVERTING);
20181 if (enter_context)
20182 pop_nested_class ();
20183 pop_nested_namespace (ns);
20186 /* We restore the source position here because it's used by
20187 add_pending_template. */
20188 input_location = saved_loc;
20190 if (at_eof && !pattern_defined
20191 && DECL_EXPLICIT_INSTANTIATION (d)
20192 && DECL_NOT_REALLY_EXTERN (d))
20193 /* [temp.explicit]
20195 The definition of a non-exported function template, a
20196 non-exported member function template, or a non-exported
20197 member function or static data member of a class template
20198 shall be present in every translation unit in which it is
20199 explicitly instantiated. */
20200 permerror (input_location, "explicit instantiation of %qD "
20201 "but no definition available", d);
20203 /* If we're in unevaluated context, we just wanted to get the
20204 constant value; this isn't an odr use, so don't queue
20205 a full instantiation. */
20206 if (cp_unevaluated_operand != 0)
20207 goto out;
20208 /* ??? Historically, we have instantiated inline functions, even
20209 when marked as "extern template". */
20210 if (!(external_p && VAR_P (d)))
20211 add_pending_template (d);
20212 goto out;
20214 /* Tell the repository that D is available in this translation unit
20215 -- and see if it is supposed to be instantiated here. */
20216 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
20218 /* In a PCH file, despite the fact that the repository hasn't
20219 requested instantiation in the PCH it is still possible that
20220 an instantiation will be required in a file that includes the
20221 PCH. */
20222 if (pch_file)
20223 add_pending_template (d);
20224 /* Instantiate inline functions so that the inliner can do its
20225 job, even though we'll not be emitting a copy of this
20226 function. */
20227 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
20228 goto out;
20231 fn_context = decl_function_context (d);
20232 nested = (current_function_decl != NULL_TREE);
20233 if (!fn_context)
20234 push_to_top_level ();
20235 else
20237 if (nested)
20238 push_function_context ();
20239 cp_unevaluated_operand = 0;
20240 c_inhibit_evaluation_warnings = 0;
20243 /* Mark D as instantiated so that recursive calls to
20244 instantiate_decl do not try to instantiate it again. */
20245 DECL_TEMPLATE_INSTANTIATED (d) = 1;
20247 /* Regenerate the declaration in case the template has been modified
20248 by a subsequent redeclaration. */
20249 regenerate_decl_from_template (d, td);
20251 /* We already set the file and line above. Reset them now in case
20252 they changed as a result of calling regenerate_decl_from_template. */
20253 input_location = DECL_SOURCE_LOCATION (d);
20255 if (VAR_P (d))
20257 tree init;
20258 bool const_init = false;
20260 /* Clear out DECL_RTL; whatever was there before may not be right
20261 since we've reset the type of the declaration. */
20262 SET_DECL_RTL (d, NULL);
20263 DECL_IN_AGGR_P (d) = 0;
20265 /* The initializer is placed in DECL_INITIAL by
20266 regenerate_decl_from_template so we don't need to
20267 push/pop_access_scope again here. Pull it out so that
20268 cp_finish_decl can process it. */
20269 init = DECL_INITIAL (d);
20270 DECL_INITIAL (d) = NULL_TREE;
20271 DECL_INITIALIZED_P (d) = 0;
20273 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20274 initializer. That function will defer actual emission until
20275 we have a chance to determine linkage. */
20276 DECL_EXTERNAL (d) = 0;
20278 /* Enter the scope of D so that access-checking works correctly. */
20279 bool enter_context = DECL_CLASS_SCOPE_P (d);
20280 if (enter_context)
20281 push_nested_class (DECL_CONTEXT (d));
20283 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20284 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
20286 if (enter_context)
20287 pop_nested_class ();
20289 if (variable_template_p (td))
20290 note_variable_template_instantiation (d);
20292 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
20293 synthesize_method (d);
20294 else if (TREE_CODE (d) == FUNCTION_DECL)
20296 hash_map<tree, tree> *saved_local_specializations;
20297 tree subst_decl;
20298 tree tmpl_parm;
20299 tree spec_parm;
20300 tree block = NULL_TREE;
20302 /* Save away the current list, in case we are instantiating one
20303 template from within the body of another. */
20304 saved_local_specializations = local_specializations;
20306 /* Set up the list of local specializations. */
20307 local_specializations = new hash_map<tree, tree>;
20309 /* Set up context. */
20310 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20311 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20312 block = push_stmt_list ();
20313 else
20314 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
20316 /* Some typedefs referenced from within the template code need to be
20317 access checked at template instantiation time, i.e now. These
20318 types were added to the template at parsing time. Let's get those
20319 and perform the access checks then. */
20320 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
20321 gen_args);
20323 /* Create substitution entries for the parameters. */
20324 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
20325 tmpl_parm = DECL_ARGUMENTS (subst_decl);
20326 spec_parm = DECL_ARGUMENTS (d);
20327 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
20329 register_local_specialization (spec_parm, tmpl_parm);
20330 spec_parm = skip_artificial_parms_for (d, spec_parm);
20331 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
20333 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
20335 if (!DECL_PACK_P (tmpl_parm))
20337 register_local_specialization (spec_parm, tmpl_parm);
20338 spec_parm = DECL_CHAIN (spec_parm);
20340 else
20342 /* Register the (value) argument pack as a specialization of
20343 TMPL_PARM, then move on. */
20344 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
20345 register_local_specialization (argpack, tmpl_parm);
20348 gcc_assert (!spec_parm);
20350 /* Substitute into the body of the function. */
20351 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20352 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
20353 tf_warning_or_error, tmpl);
20354 else
20356 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
20357 tf_warning_or_error, tmpl,
20358 /*integral_constant_expression_p=*/false);
20360 /* Set the current input_location to the end of the function
20361 so that finish_function knows where we are. */
20362 input_location
20363 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
20365 /* Remember if we saw an infinite loop in the template. */
20366 current_function_infinite_loop
20367 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
20370 /* We don't need the local specializations any more. */
20371 delete local_specializations;
20372 local_specializations = saved_local_specializations;
20374 /* Finish the function. */
20375 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20376 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20377 DECL_SAVED_TREE (d) = pop_stmt_list (block);
20378 else
20380 d = finish_function (0);
20381 expand_or_defer_fn (d);
20384 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20385 cp_check_omp_declare_reduction (d);
20388 /* We're not deferring instantiation any more. */
20389 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20391 if (!fn_context)
20392 pop_from_top_level ();
20393 else if (nested)
20394 pop_function_context ();
20396 out:
20397 input_location = saved_loc;
20398 cp_unevaluated_operand = saved_unevaluated_operand;
20399 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20400 pop_deferring_access_checks ();
20401 pop_tinst_level ();
20403 timevar_pop (TV_TEMPLATE_INST);
20405 return d;
20408 /* Run through the list of templates that we wish we could
20409 instantiate, and instantiate any we can. RETRIES is the
20410 number of times we retry pending template instantiation. */
20412 void
20413 instantiate_pending_templates (int retries)
20415 int reconsider;
20416 location_t saved_loc = input_location;
20418 /* Instantiating templates may trigger vtable generation. This in turn
20419 may require further template instantiations. We place a limit here
20420 to avoid infinite loop. */
20421 if (pending_templates && retries >= max_tinst_depth)
20423 tree decl = pending_templates->tinst->decl;
20425 fatal_error ("template instantiation depth exceeds maximum of %d"
20426 " instantiating %q+D, possibly from virtual table generation"
20427 " (use -ftemplate-depth= to increase the maximum)",
20428 max_tinst_depth, decl);
20429 if (TREE_CODE (decl) == FUNCTION_DECL)
20430 /* Pretend that we defined it. */
20431 DECL_INITIAL (decl) = error_mark_node;
20432 return;
20437 struct pending_template **t = &pending_templates;
20438 struct pending_template *last = NULL;
20439 reconsider = 0;
20440 while (*t)
20442 tree instantiation = reopen_tinst_level ((*t)->tinst);
20443 bool complete = false;
20445 if (TYPE_P (instantiation))
20447 tree fn;
20449 if (!COMPLETE_TYPE_P (instantiation))
20451 instantiate_class_template (instantiation);
20452 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20453 for (fn = TYPE_METHODS (instantiation);
20455 fn = TREE_CHAIN (fn))
20456 if (! DECL_ARTIFICIAL (fn))
20457 instantiate_decl (fn,
20458 /*defer_ok=*/0,
20459 /*expl_inst_class_mem_p=*/false);
20460 if (COMPLETE_TYPE_P (instantiation))
20461 reconsider = 1;
20464 complete = COMPLETE_TYPE_P (instantiation);
20466 else
20468 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20469 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20471 instantiation
20472 = instantiate_decl (instantiation,
20473 /*defer_ok=*/0,
20474 /*expl_inst_class_mem_p=*/false);
20475 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20476 reconsider = 1;
20479 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20480 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20483 if (complete)
20484 /* If INSTANTIATION has been instantiated, then we don't
20485 need to consider it again in the future. */
20486 *t = (*t)->next;
20487 else
20489 last = *t;
20490 t = &(*t)->next;
20492 tinst_depth = 0;
20493 current_tinst_level = NULL;
20495 last_pending_template = last;
20497 while (reconsider);
20499 input_location = saved_loc;
20502 /* Substitute ARGVEC into T, which is a list of initializers for
20503 either base class or a non-static data member. The TREE_PURPOSEs
20504 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20505 instantiate_decl. */
20507 static tree
20508 tsubst_initializer_list (tree t, tree argvec)
20510 tree inits = NULL_TREE;
20512 for (; t; t = TREE_CHAIN (t))
20514 tree decl;
20515 tree init;
20516 tree expanded_bases = NULL_TREE;
20517 tree expanded_arguments = NULL_TREE;
20518 int i, len = 1;
20520 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20522 tree expr;
20523 tree arg;
20525 /* Expand the base class expansion type into separate base
20526 classes. */
20527 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20528 tf_warning_or_error,
20529 NULL_TREE);
20530 if (expanded_bases == error_mark_node)
20531 continue;
20533 /* We'll be building separate TREE_LISTs of arguments for
20534 each base. */
20535 len = TREE_VEC_LENGTH (expanded_bases);
20536 expanded_arguments = make_tree_vec (len);
20537 for (i = 0; i < len; i++)
20538 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20540 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20541 expand each argument in the TREE_VALUE of t. */
20542 expr = make_node (EXPR_PACK_EXPANSION);
20543 PACK_EXPANSION_LOCAL_P (expr) = true;
20544 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20545 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20547 if (TREE_VALUE (t) == void_type_node)
20548 /* VOID_TYPE_NODE is used to indicate
20549 value-initialization. */
20551 for (i = 0; i < len; i++)
20552 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20554 else
20556 /* Substitute parameter packs into each argument in the
20557 TREE_LIST. */
20558 in_base_initializer = 1;
20559 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20561 tree expanded_exprs;
20563 /* Expand the argument. */
20564 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20565 expanded_exprs
20566 = tsubst_pack_expansion (expr, argvec,
20567 tf_warning_or_error,
20568 NULL_TREE);
20569 if (expanded_exprs == error_mark_node)
20570 continue;
20572 /* Prepend each of the expanded expressions to the
20573 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20574 for (i = 0; i < len; i++)
20576 TREE_VEC_ELT (expanded_arguments, i) =
20577 tree_cons (NULL_TREE,
20578 TREE_VEC_ELT (expanded_exprs, i),
20579 TREE_VEC_ELT (expanded_arguments, i));
20582 in_base_initializer = 0;
20584 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20585 since we built them backwards. */
20586 for (i = 0; i < len; i++)
20588 TREE_VEC_ELT (expanded_arguments, i) =
20589 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20594 for (i = 0; i < len; ++i)
20596 if (expanded_bases)
20598 decl = TREE_VEC_ELT (expanded_bases, i);
20599 decl = expand_member_init (decl);
20600 init = TREE_VEC_ELT (expanded_arguments, i);
20602 else
20604 tree tmp;
20605 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20606 tf_warning_or_error, NULL_TREE);
20608 decl = expand_member_init (decl);
20609 if (decl && !DECL_P (decl))
20610 in_base_initializer = 1;
20612 init = TREE_VALUE (t);
20613 tmp = init;
20614 if (init != void_type_node)
20615 init = tsubst_expr (init, argvec,
20616 tf_warning_or_error, NULL_TREE,
20617 /*integral_constant_expression_p=*/false);
20618 if (init == NULL_TREE && tmp != NULL_TREE)
20619 /* If we had an initializer but it instantiated to nothing,
20620 value-initialize the object. This will only occur when
20621 the initializer was a pack expansion where the parameter
20622 packs used in that expansion were of length zero. */
20623 init = void_type_node;
20624 in_base_initializer = 0;
20627 if (decl)
20629 init = build_tree_list (decl, init);
20630 TREE_CHAIN (init) = inits;
20631 inits = init;
20635 return inits;
20638 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20640 static void
20641 set_current_access_from_decl (tree decl)
20643 if (TREE_PRIVATE (decl))
20644 current_access_specifier = access_private_node;
20645 else if (TREE_PROTECTED (decl))
20646 current_access_specifier = access_protected_node;
20647 else
20648 current_access_specifier = access_public_node;
20651 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20652 is the instantiation (which should have been created with
20653 start_enum) and ARGS are the template arguments to use. */
20655 static void
20656 tsubst_enum (tree tag, tree newtag, tree args)
20658 tree e;
20660 if (SCOPED_ENUM_P (newtag))
20661 begin_scope (sk_scoped_enum, newtag);
20663 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20665 tree value;
20666 tree decl;
20668 decl = TREE_VALUE (e);
20669 /* Note that in a template enum, the TREE_VALUE is the
20670 CONST_DECL, not the corresponding INTEGER_CST. */
20671 value = tsubst_expr (DECL_INITIAL (decl),
20672 args, tf_warning_or_error, NULL_TREE,
20673 /*integral_constant_expression_p=*/true);
20675 /* Give this enumeration constant the correct access. */
20676 set_current_access_from_decl (decl);
20678 /* Actually build the enumerator itself. */
20679 build_enumerator
20680 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20683 if (SCOPED_ENUM_P (newtag))
20684 finish_scope ();
20686 finish_enum_value_list (newtag);
20687 finish_enum (newtag);
20689 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20690 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20693 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20694 its type -- but without substituting the innermost set of template
20695 arguments. So, innermost set of template parameters will appear in
20696 the type. */
20698 tree
20699 get_mostly_instantiated_function_type (tree decl)
20701 tree fn_type;
20702 tree tmpl;
20703 tree targs;
20704 tree tparms;
20705 int parm_depth;
20707 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20708 targs = DECL_TI_ARGS (decl);
20709 tparms = DECL_TEMPLATE_PARMS (tmpl);
20710 parm_depth = TMPL_PARMS_DEPTH (tparms);
20712 /* There should be as many levels of arguments as there are levels
20713 of parameters. */
20714 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20716 fn_type = TREE_TYPE (tmpl);
20718 if (parm_depth == 1)
20719 /* No substitution is necessary. */
20721 else
20723 int i;
20724 tree partial_args;
20726 /* Replace the innermost level of the TARGS with NULL_TREEs to
20727 let tsubst know not to substitute for those parameters. */
20728 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20729 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20730 SET_TMPL_ARGS_LEVEL (partial_args, i,
20731 TMPL_ARGS_LEVEL (targs, i));
20732 SET_TMPL_ARGS_LEVEL (partial_args,
20733 TMPL_ARGS_DEPTH (targs),
20734 make_tree_vec (DECL_NTPARMS (tmpl)));
20736 /* Make sure that we can see identifiers, and compute access
20737 correctly. */
20738 push_access_scope (decl);
20740 ++processing_template_decl;
20741 /* Now, do the (partial) substitution to figure out the
20742 appropriate function type. */
20743 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20744 --processing_template_decl;
20746 /* Substitute into the template parameters to obtain the real
20747 innermost set of parameters. This step is important if the
20748 innermost set of template parameters contains value
20749 parameters whose types depend on outer template parameters. */
20750 TREE_VEC_LENGTH (partial_args)--;
20751 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20753 pop_access_scope (decl);
20756 return fn_type;
20759 /* Return truthvalue if we're processing a template different from
20760 the last one involved in diagnostics. */
20761 bool
20762 problematic_instantiation_changed (void)
20764 return current_tinst_level != last_error_tinst_level;
20767 /* Remember current template involved in diagnostics. */
20768 void
20769 record_last_problematic_instantiation (void)
20771 last_error_tinst_level = current_tinst_level;
20774 struct tinst_level *
20775 current_instantiation (void)
20777 return current_tinst_level;
20780 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20781 type. Return zero for ok, nonzero for disallowed. Issue error and
20782 warning messages under control of COMPLAIN. */
20784 static int
20785 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20787 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20788 return 0;
20789 else if (POINTER_TYPE_P (type))
20790 return 0;
20791 else if (TYPE_PTRMEM_P (type))
20792 return 0;
20793 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20794 return 0;
20795 else if (TREE_CODE (type) == TYPENAME_TYPE)
20796 return 0;
20797 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20798 return 0;
20799 else if (TREE_CODE (type) == NULLPTR_TYPE)
20800 return 0;
20802 if (complain & tf_error)
20804 if (type == error_mark_node)
20805 inform (input_location, "invalid template non-type parameter");
20806 else
20807 error ("%q#T is not a valid type for a template non-type parameter",
20808 type);
20810 return 1;
20813 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20814 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20816 static bool
20817 dependent_type_p_r (tree type)
20819 tree scope;
20821 /* [temp.dep.type]
20823 A type is dependent if it is:
20825 -- a template parameter. Template template parameters are types
20826 for us (since TYPE_P holds true for them) so we handle
20827 them here. */
20828 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20829 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20830 return true;
20831 /* -- a qualified-id with a nested-name-specifier which contains a
20832 class-name that names a dependent type or whose unqualified-id
20833 names a dependent type. */
20834 if (TREE_CODE (type) == TYPENAME_TYPE)
20835 return true;
20836 /* -- a cv-qualified type where the cv-unqualified type is
20837 dependent.
20838 No code is necessary for this bullet; the code below handles
20839 cv-qualified types, and we don't want to strip aliases with
20840 TYPE_MAIN_VARIANT because of DR 1558. */
20841 /* -- a compound type constructed from any dependent type. */
20842 if (TYPE_PTRMEM_P (type))
20843 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20844 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20845 (type)));
20846 else if (TYPE_PTR_P (type)
20847 || TREE_CODE (type) == REFERENCE_TYPE)
20848 return dependent_type_p (TREE_TYPE (type));
20849 else if (TREE_CODE (type) == FUNCTION_TYPE
20850 || TREE_CODE (type) == METHOD_TYPE)
20852 tree arg_type;
20854 if (dependent_type_p (TREE_TYPE (type)))
20855 return true;
20856 for (arg_type = TYPE_ARG_TYPES (type);
20857 arg_type;
20858 arg_type = TREE_CHAIN (arg_type))
20859 if (dependent_type_p (TREE_VALUE (arg_type)))
20860 return true;
20861 return false;
20863 /* -- an array type constructed from any dependent type or whose
20864 size is specified by a constant expression that is
20865 value-dependent.
20867 We checked for type- and value-dependence of the bounds in
20868 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20869 if (TREE_CODE (type) == ARRAY_TYPE)
20871 if (TYPE_DOMAIN (type)
20872 && dependent_type_p (TYPE_DOMAIN (type)))
20873 return true;
20874 return dependent_type_p (TREE_TYPE (type));
20877 /* -- a template-id in which either the template name is a template
20878 parameter ... */
20879 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20880 return true;
20881 /* ... or any of the template arguments is a dependent type or
20882 an expression that is type-dependent or value-dependent. */
20883 else if (TYPE_TEMPLATE_INFO (type)
20884 && (any_dependent_template_arguments_p
20885 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (type)))))
20886 return true;
20888 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20889 dependent; if the argument of the `typeof' expression is not
20890 type-dependent, then it should already been have resolved. */
20891 if (TREE_CODE (type) == TYPEOF_TYPE
20892 || TREE_CODE (type) == DECLTYPE_TYPE
20893 || TREE_CODE (type) == UNDERLYING_TYPE)
20894 return true;
20896 /* A template argument pack is dependent if any of its packed
20897 arguments are. */
20898 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20900 tree args = ARGUMENT_PACK_ARGS (type);
20901 int i, len = TREE_VEC_LENGTH (args);
20902 for (i = 0; i < len; ++i)
20903 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20904 return true;
20907 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20908 be template parameters. */
20909 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20910 return true;
20912 /* The standard does not specifically mention types that are local
20913 to template functions or local classes, but they should be
20914 considered dependent too. For example:
20916 template <int I> void f() {
20917 enum E { a = I };
20918 S<sizeof (E)> s;
20921 The size of `E' cannot be known until the value of `I' has been
20922 determined. Therefore, `E' must be considered dependent. */
20923 scope = TYPE_CONTEXT (type);
20924 if (scope && TYPE_P (scope))
20925 return dependent_type_p (scope);
20926 /* Don't use type_dependent_expression_p here, as it can lead
20927 to infinite recursion trying to determine whether a lambda
20928 nested in a lambda is dependent (c++/47687). */
20929 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20930 && DECL_LANG_SPECIFIC (scope)
20931 && DECL_TEMPLATE_INFO (scope)
20932 && (any_dependent_template_arguments_p
20933 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20934 return true;
20936 /* Other types are non-dependent. */
20937 return false;
20940 /* Returns TRUE if TYPE is dependent, in the sense of
20941 [temp.dep.type]. Note that a NULL type is considered dependent. */
20943 bool
20944 dependent_type_p (tree type)
20946 /* If there are no template parameters in scope, then there can't be
20947 any dependent types. */
20948 if (!processing_template_decl)
20950 /* If we are not processing a template, then nobody should be
20951 providing us with a dependent type. */
20952 gcc_assert (type);
20953 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20954 return false;
20957 /* If the type is NULL, we have not computed a type for the entity
20958 in question; in that case, the type is dependent. */
20959 if (!type)
20960 return true;
20962 /* Erroneous types can be considered non-dependent. */
20963 if (type == error_mark_node)
20964 return false;
20966 /* If we have not already computed the appropriate value for TYPE,
20967 do so now. */
20968 if (!TYPE_DEPENDENT_P_VALID (type))
20970 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20971 TYPE_DEPENDENT_P_VALID (type) = 1;
20974 return TYPE_DEPENDENT_P (type);
20977 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20978 lookup. In other words, a dependent type that is not the current
20979 instantiation. */
20981 bool
20982 dependent_scope_p (tree scope)
20984 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20985 && !currently_open_class (scope));
20988 /* T is a SCOPE_REF; return whether we need to consider it
20989 instantiation-dependent so that we can check access at instantiation
20990 time even though we know which member it resolves to. */
20992 static bool
20993 instantiation_dependent_scope_ref_p (tree t)
20995 if (DECL_P (TREE_OPERAND (t, 1))
20996 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20997 && accessible_in_template_p (TREE_OPERAND (t, 0),
20998 TREE_OPERAND (t, 1)))
20999 return false;
21000 else
21001 return true;
21004 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21005 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21006 expression. */
21008 /* Note that this predicate is not appropriate for general expressions;
21009 only constant expressions (that satisfy potential_constant_expression)
21010 can be tested for value dependence. */
21012 bool
21013 value_dependent_expression_p (tree expression)
21015 if (!processing_template_decl)
21016 return false;
21018 /* A name declared with a dependent type. */
21019 if (DECL_P (expression) && type_dependent_expression_p (expression))
21020 return true;
21022 switch (TREE_CODE (expression))
21024 case IDENTIFIER_NODE:
21025 /* A name that has not been looked up -- must be dependent. */
21026 return true;
21028 case TEMPLATE_PARM_INDEX:
21029 /* A non-type template parm. */
21030 return true;
21032 case CONST_DECL:
21033 /* A non-type template parm. */
21034 if (DECL_TEMPLATE_PARM_P (expression))
21035 return true;
21036 return value_dependent_expression_p (DECL_INITIAL (expression));
21038 case VAR_DECL:
21039 /* A constant with literal type and is initialized
21040 with an expression that is value-dependent.
21042 Note that a non-dependent parenthesized initializer will have
21043 already been replaced with its constant value, so if we see
21044 a TREE_LIST it must be dependent. */
21045 if (DECL_INITIAL (expression)
21046 && decl_constant_var_p (expression)
21047 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
21048 /* cp_finish_decl doesn't fold reference initializers. */
21049 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
21050 || value_dependent_expression_p (DECL_INITIAL (expression))))
21051 return true;
21052 return false;
21054 case DYNAMIC_CAST_EXPR:
21055 case STATIC_CAST_EXPR:
21056 case CONST_CAST_EXPR:
21057 case REINTERPRET_CAST_EXPR:
21058 case CAST_EXPR:
21059 /* These expressions are value-dependent if the type to which
21060 the cast occurs is dependent or the expression being casted
21061 is value-dependent. */
21063 tree type = TREE_TYPE (expression);
21065 if (dependent_type_p (type))
21066 return true;
21068 /* A functional cast has a list of operands. */
21069 expression = TREE_OPERAND (expression, 0);
21070 if (!expression)
21072 /* If there are no operands, it must be an expression such
21073 as "int()". This should not happen for aggregate types
21074 because it would form non-constant expressions. */
21075 gcc_assert (cxx_dialect >= cxx11
21076 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
21078 return false;
21081 if (TREE_CODE (expression) == TREE_LIST)
21082 return any_value_dependent_elements_p (expression);
21084 return value_dependent_expression_p (expression);
21087 case SIZEOF_EXPR:
21088 if (SIZEOF_EXPR_TYPE_P (expression))
21089 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
21090 /* FALLTHRU */
21091 case ALIGNOF_EXPR:
21092 case TYPEID_EXPR:
21093 /* A `sizeof' expression is value-dependent if the operand is
21094 type-dependent or is a pack expansion. */
21095 expression = TREE_OPERAND (expression, 0);
21096 if (PACK_EXPANSION_P (expression))
21097 return true;
21098 else if (TYPE_P (expression))
21099 return dependent_type_p (expression);
21100 return instantiation_dependent_expression_p (expression);
21102 case AT_ENCODE_EXPR:
21103 /* An 'encode' expression is value-dependent if the operand is
21104 type-dependent. */
21105 expression = TREE_OPERAND (expression, 0);
21106 return dependent_type_p (expression);
21108 case NOEXCEPT_EXPR:
21109 expression = TREE_OPERAND (expression, 0);
21110 return instantiation_dependent_expression_p (expression);
21112 case SCOPE_REF:
21113 /* All instantiation-dependent expressions should also be considered
21114 value-dependent. */
21115 return instantiation_dependent_scope_ref_p (expression);
21117 case COMPONENT_REF:
21118 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
21119 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
21121 case NONTYPE_ARGUMENT_PACK:
21122 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21123 is value-dependent. */
21125 tree values = ARGUMENT_PACK_ARGS (expression);
21126 int i, len = TREE_VEC_LENGTH (values);
21128 for (i = 0; i < len; ++i)
21129 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
21130 return true;
21132 return false;
21135 case TRAIT_EXPR:
21137 tree type2 = TRAIT_EXPR_TYPE2 (expression);
21138 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
21139 || (type2 ? dependent_type_p (type2) : false));
21142 case MODOP_EXPR:
21143 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21144 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
21146 case ARRAY_REF:
21147 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21148 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
21150 case ADDR_EXPR:
21152 tree op = TREE_OPERAND (expression, 0);
21153 return (value_dependent_expression_p (op)
21154 || has_value_dependent_address (op));
21157 case CALL_EXPR:
21159 tree fn = get_callee_fndecl (expression);
21160 int i, nargs;
21161 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
21162 return true;
21163 nargs = call_expr_nargs (expression);
21164 for (i = 0; i < nargs; ++i)
21166 tree op = CALL_EXPR_ARG (expression, i);
21167 /* In a call to a constexpr member function, look through the
21168 implicit ADDR_EXPR on the object argument so that it doesn't
21169 cause the call to be considered value-dependent. We also
21170 look through it in potential_constant_expression. */
21171 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
21172 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21173 && TREE_CODE (op) == ADDR_EXPR)
21174 op = TREE_OPERAND (op, 0);
21175 if (value_dependent_expression_p (op))
21176 return true;
21178 return false;
21181 case TEMPLATE_ID_EXPR:
21182 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21183 type-dependent. */
21184 return type_dependent_expression_p (expression);
21186 case CONSTRUCTOR:
21188 unsigned ix;
21189 tree val;
21190 if (dependent_type_p (TREE_TYPE (expression)))
21191 return true;
21192 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
21193 if (value_dependent_expression_p (val))
21194 return true;
21195 return false;
21198 case STMT_EXPR:
21199 /* Treat a GNU statement expression as dependent to avoid crashing
21200 under instantiate_non_dependent_expr; it can't be constant. */
21201 return true;
21203 default:
21204 /* A constant expression is value-dependent if any subexpression is
21205 value-dependent. */
21206 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
21208 case tcc_reference:
21209 case tcc_unary:
21210 case tcc_comparison:
21211 case tcc_binary:
21212 case tcc_expression:
21213 case tcc_vl_exp:
21215 int i, len = cp_tree_operand_length (expression);
21217 for (i = 0; i < len; i++)
21219 tree t = TREE_OPERAND (expression, i);
21221 /* In some cases, some of the operands may be missing.l
21222 (For example, in the case of PREDECREMENT_EXPR, the
21223 amount to increment by may be missing.) That doesn't
21224 make the expression dependent. */
21225 if (t && value_dependent_expression_p (t))
21226 return true;
21229 break;
21230 default:
21231 break;
21233 break;
21236 /* The expression is not value-dependent. */
21237 return false;
21240 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21241 [temp.dep.expr]. Note that an expression with no type is
21242 considered dependent. Other parts of the compiler arrange for an
21243 expression with type-dependent subexpressions to have no type, so
21244 this function doesn't have to be fully recursive. */
21246 bool
21247 type_dependent_expression_p (tree expression)
21249 if (!processing_template_decl)
21250 return false;
21252 if (expression == NULL_TREE || expression == error_mark_node)
21253 return false;
21255 /* An unresolved name is always dependent. */
21256 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
21257 return true;
21259 /* Some expression forms are never type-dependent. */
21260 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
21261 || TREE_CODE (expression) == SIZEOF_EXPR
21262 || TREE_CODE (expression) == ALIGNOF_EXPR
21263 || TREE_CODE (expression) == AT_ENCODE_EXPR
21264 || TREE_CODE (expression) == NOEXCEPT_EXPR
21265 || TREE_CODE (expression) == TRAIT_EXPR
21266 || TREE_CODE (expression) == TYPEID_EXPR
21267 || TREE_CODE (expression) == DELETE_EXPR
21268 || TREE_CODE (expression) == VEC_DELETE_EXPR
21269 || TREE_CODE (expression) == THROW_EXPR)
21270 return false;
21272 /* The types of these expressions depends only on the type to which
21273 the cast occurs. */
21274 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
21275 || TREE_CODE (expression) == STATIC_CAST_EXPR
21276 || TREE_CODE (expression) == CONST_CAST_EXPR
21277 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
21278 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
21279 || TREE_CODE (expression) == CAST_EXPR)
21280 return dependent_type_p (TREE_TYPE (expression));
21282 /* The types of these expressions depends only on the type created
21283 by the expression. */
21284 if (TREE_CODE (expression) == NEW_EXPR
21285 || TREE_CODE (expression) == VEC_NEW_EXPR)
21287 /* For NEW_EXPR tree nodes created inside a template, either
21288 the object type itself or a TREE_LIST may appear as the
21289 operand 1. */
21290 tree type = TREE_OPERAND (expression, 1);
21291 if (TREE_CODE (type) == TREE_LIST)
21292 /* This is an array type. We need to check array dimensions
21293 as well. */
21294 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
21295 || value_dependent_expression_p
21296 (TREE_OPERAND (TREE_VALUE (type), 1));
21297 else
21298 return dependent_type_p (type);
21301 if (TREE_CODE (expression) == SCOPE_REF)
21303 tree scope = TREE_OPERAND (expression, 0);
21304 tree name = TREE_OPERAND (expression, 1);
21306 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21307 contains an identifier associated by name lookup with one or more
21308 declarations declared with a dependent type, or...a
21309 nested-name-specifier or qualified-id that names a member of an
21310 unknown specialization. */
21311 return (type_dependent_expression_p (name)
21312 || dependent_scope_p (scope));
21315 if (TREE_CODE (expression) == FUNCTION_DECL
21316 && DECL_LANG_SPECIFIC (expression)
21317 && DECL_TEMPLATE_INFO (expression)
21318 && (any_dependent_template_arguments_p
21319 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21320 return true;
21322 if (TREE_CODE (expression) == TEMPLATE_DECL
21323 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21324 return false;
21326 if (TREE_CODE (expression) == STMT_EXPR)
21327 expression = stmt_expr_value_expr (expression);
21329 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21331 tree elt;
21332 unsigned i;
21334 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
21336 if (type_dependent_expression_p (elt))
21337 return true;
21339 return false;
21342 /* A static data member of the current instantiation with incomplete
21343 array type is type-dependent, as the definition and specializations
21344 can have different bounds. */
21345 if (VAR_P (expression)
21346 && DECL_CLASS_SCOPE_P (expression)
21347 && dependent_type_p (DECL_CONTEXT (expression))
21348 && VAR_HAD_UNKNOWN_BOUND (expression))
21349 return true;
21351 /* An array of unknown bound depending on a variadic parameter, eg:
21353 template<typename... Args>
21354 void foo (Args... args)
21356 int arr[] = { args... };
21359 template<int... vals>
21360 void bar ()
21362 int arr[] = { vals... };
21365 If the array has no length and has an initializer, it must be that
21366 we couldn't determine its length in cp_complete_array_type because
21367 it is dependent. */
21368 if (VAR_P (expression)
21369 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
21370 && !TYPE_DOMAIN (TREE_TYPE (expression))
21371 && DECL_INITIAL (expression))
21372 return true;
21374 if (TREE_TYPE (expression) == unknown_type_node)
21376 if (TREE_CODE (expression) == ADDR_EXPR)
21377 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21378 if (TREE_CODE (expression) == COMPONENT_REF
21379 || TREE_CODE (expression) == OFFSET_REF)
21381 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
21382 return true;
21383 expression = TREE_OPERAND (expression, 1);
21384 if (identifier_p (expression))
21385 return false;
21387 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21388 if (TREE_CODE (expression) == SCOPE_REF)
21389 return false;
21391 /* Always dependent, on the number of arguments if nothing else. */
21392 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21393 return true;
21395 if (BASELINK_P (expression))
21397 if (BASELINK_OPTYPE (expression)
21398 && dependent_type_p (BASELINK_OPTYPE (expression)))
21399 return true;
21400 expression = BASELINK_FUNCTIONS (expression);
21403 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21405 if (any_dependent_template_arguments_p
21406 (TREE_OPERAND (expression, 1)))
21407 return true;
21408 expression = TREE_OPERAND (expression, 0);
21410 gcc_assert (TREE_CODE (expression) == OVERLOAD
21411 || TREE_CODE (expression) == FUNCTION_DECL);
21413 while (expression)
21415 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21416 return true;
21417 expression = OVL_NEXT (expression);
21419 return false;
21422 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21424 return (dependent_type_p (TREE_TYPE (expression)));
21427 /* walk_tree callback function for instantiation_dependent_expression_p,
21428 below. Returns non-zero if a dependent subexpression is found. */
21430 static tree
21431 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21432 void * /*data*/)
21434 if (TYPE_P (*tp))
21436 /* We don't have to worry about decltype currently because decltype
21437 of an instantiation-dependent expr is a dependent type. This
21438 might change depending on the resolution of DR 1172. */
21439 *walk_subtrees = false;
21440 return NULL_TREE;
21442 enum tree_code code = TREE_CODE (*tp);
21443 switch (code)
21445 /* Don't treat an argument list as dependent just because it has no
21446 TREE_TYPE. */
21447 case TREE_LIST:
21448 case TREE_VEC:
21449 return NULL_TREE;
21451 case VAR_DECL:
21452 case CONST_DECL:
21453 /* A constant with a dependent initializer is dependent. */
21454 if (value_dependent_expression_p (*tp))
21455 return *tp;
21456 break;
21458 case TEMPLATE_PARM_INDEX:
21459 return *tp;
21461 /* Handle expressions with type operands. */
21462 case SIZEOF_EXPR:
21463 case ALIGNOF_EXPR:
21464 case TYPEID_EXPR:
21465 case AT_ENCODE_EXPR:
21467 tree op = TREE_OPERAND (*tp, 0);
21468 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21469 op = TREE_TYPE (op);
21470 if (TYPE_P (op))
21472 if (dependent_type_p (op))
21473 return *tp;
21474 else
21476 *walk_subtrees = false;
21477 return NULL_TREE;
21480 break;
21483 case TRAIT_EXPR:
21484 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
21485 || (TRAIT_EXPR_TYPE2 (*tp)
21486 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
21487 return *tp;
21488 *walk_subtrees = false;
21489 return NULL_TREE;
21491 case COMPONENT_REF:
21492 if (identifier_p (TREE_OPERAND (*tp, 1)))
21493 /* In a template, finish_class_member_access_expr creates a
21494 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21495 type-dependent, so that we can check access control at
21496 instantiation time (PR 42277). See also Core issue 1273. */
21497 return *tp;
21498 break;
21500 case SCOPE_REF:
21501 if (instantiation_dependent_scope_ref_p (*tp))
21502 return *tp;
21503 else
21504 break;
21506 /* Treat statement-expressions as dependent. */
21507 case BIND_EXPR:
21508 return *tp;
21510 default:
21511 break;
21514 if (type_dependent_expression_p (*tp))
21515 return *tp;
21516 else
21517 return NULL_TREE;
21520 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21521 sense defined by the ABI:
21523 "An expression is instantiation-dependent if it is type-dependent
21524 or value-dependent, or it has a subexpression that is type-dependent
21525 or value-dependent." */
21527 bool
21528 instantiation_dependent_expression_p (tree expression)
21530 tree result;
21532 if (!processing_template_decl)
21533 return false;
21535 if (expression == error_mark_node)
21536 return false;
21538 result = cp_walk_tree_without_duplicates (&expression,
21539 instantiation_dependent_r, NULL);
21540 return result != NULL_TREE;
21543 /* Like type_dependent_expression_p, but it also works while not processing
21544 a template definition, i.e. during substitution or mangling. */
21546 bool
21547 type_dependent_expression_p_push (tree expr)
21549 bool b;
21550 ++processing_template_decl;
21551 b = type_dependent_expression_p (expr);
21552 --processing_template_decl;
21553 return b;
21556 /* Returns TRUE if ARGS contains a type-dependent expression. */
21558 bool
21559 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21561 unsigned int i;
21562 tree arg;
21564 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21566 if (type_dependent_expression_p (arg))
21567 return true;
21569 return false;
21572 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21573 expressions) contains any type-dependent expressions. */
21575 bool
21576 any_type_dependent_elements_p (const_tree list)
21578 for (; list; list = TREE_CHAIN (list))
21579 if (type_dependent_expression_p (TREE_VALUE (list)))
21580 return true;
21582 return false;
21585 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21586 expressions) contains any value-dependent expressions. */
21588 bool
21589 any_value_dependent_elements_p (const_tree list)
21591 for (; list; list = TREE_CHAIN (list))
21592 if (value_dependent_expression_p (TREE_VALUE (list)))
21593 return true;
21595 return false;
21598 /* Returns TRUE if the ARG (a template argument) is dependent. */
21600 bool
21601 dependent_template_arg_p (tree arg)
21603 if (!processing_template_decl)
21604 return false;
21606 /* Assume a template argument that was wrongly written by the user
21607 is dependent. This is consistent with what
21608 any_dependent_template_arguments_p [that calls this function]
21609 does. */
21610 if (!arg || arg == error_mark_node)
21611 return true;
21613 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21614 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21616 if (TREE_CODE (arg) == TEMPLATE_DECL
21617 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21618 return dependent_template_p (arg);
21619 else if (ARGUMENT_PACK_P (arg))
21621 tree args = ARGUMENT_PACK_ARGS (arg);
21622 int i, len = TREE_VEC_LENGTH (args);
21623 for (i = 0; i < len; ++i)
21625 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21626 return true;
21629 return false;
21631 else if (TYPE_P (arg))
21632 return dependent_type_p (arg);
21633 else
21634 return (type_dependent_expression_p (arg)
21635 || value_dependent_expression_p (arg));
21638 /* Returns true if ARGS (a collection of template arguments) contains
21639 any types that require structural equality testing. */
21641 bool
21642 any_template_arguments_need_structural_equality_p (tree args)
21644 int i;
21645 int j;
21647 if (!args)
21648 return false;
21649 if (args == error_mark_node)
21650 return true;
21652 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21654 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21655 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21657 tree arg = TREE_VEC_ELT (level, j);
21658 tree packed_args = NULL_TREE;
21659 int k, len = 1;
21661 if (ARGUMENT_PACK_P (arg))
21663 /* Look inside the argument pack. */
21664 packed_args = ARGUMENT_PACK_ARGS (arg);
21665 len = TREE_VEC_LENGTH (packed_args);
21668 for (k = 0; k < len; ++k)
21670 if (packed_args)
21671 arg = TREE_VEC_ELT (packed_args, k);
21673 if (error_operand_p (arg))
21674 return true;
21675 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21676 continue;
21677 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21678 return true;
21679 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21680 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21681 return true;
21686 return false;
21689 /* Returns true if ARGS (a collection of template arguments) contains
21690 any dependent arguments. */
21692 bool
21693 any_dependent_template_arguments_p (const_tree args)
21695 int i;
21696 int j;
21698 if (!args)
21699 return false;
21700 if (args == error_mark_node)
21701 return true;
21703 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21705 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21706 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21707 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21708 return true;
21711 return false;
21714 /* Returns TRUE if the template TMPL is dependent. */
21716 bool
21717 dependent_template_p (tree tmpl)
21719 if (TREE_CODE (tmpl) == OVERLOAD)
21721 while (tmpl)
21723 if (dependent_template_p (OVL_CURRENT (tmpl)))
21724 return true;
21725 tmpl = OVL_NEXT (tmpl);
21727 return false;
21730 /* Template template parameters are dependent. */
21731 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21732 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21733 return true;
21734 /* So are names that have not been looked up. */
21735 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21736 return true;
21737 /* So are member templates of dependent classes. */
21738 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21739 return dependent_type_p (DECL_CONTEXT (tmpl));
21740 return false;
21743 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21745 bool
21746 dependent_template_id_p (tree tmpl, tree args)
21748 return (dependent_template_p (tmpl)
21749 || any_dependent_template_arguments_p (args));
21752 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21753 is dependent. */
21755 bool
21756 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21758 int i;
21760 if (!processing_template_decl)
21761 return false;
21763 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21765 tree decl = TREE_VEC_ELT (declv, i);
21766 tree init = TREE_VEC_ELT (initv, i);
21767 tree cond = TREE_VEC_ELT (condv, i);
21768 tree incr = TREE_VEC_ELT (incrv, i);
21770 if (type_dependent_expression_p (decl))
21771 return true;
21773 if (init && type_dependent_expression_p (init))
21774 return true;
21776 if (type_dependent_expression_p (cond))
21777 return true;
21779 if (COMPARISON_CLASS_P (cond)
21780 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21781 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21782 return true;
21784 if (TREE_CODE (incr) == MODOP_EXPR)
21786 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21787 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21788 return true;
21790 else if (type_dependent_expression_p (incr))
21791 return true;
21792 else if (TREE_CODE (incr) == MODIFY_EXPR)
21794 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21795 return true;
21796 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21798 tree t = TREE_OPERAND (incr, 1);
21799 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21800 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21801 return true;
21806 return false;
21809 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21810 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21811 no such TYPE can be found. Note that this function peers inside
21812 uninstantiated templates and therefore should be used only in
21813 extremely limited situations. ONLY_CURRENT_P restricts this
21814 peering to the currently open classes hierarchy (which is required
21815 when comparing types). */
21817 tree
21818 resolve_typename_type (tree type, bool only_current_p)
21820 tree scope;
21821 tree name;
21822 tree decl;
21823 int quals;
21824 tree pushed_scope;
21825 tree result;
21827 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21829 scope = TYPE_CONTEXT (type);
21830 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21831 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21832 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21833 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21834 identifier of the TYPENAME_TYPE anymore.
21835 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21836 TYPENAME_TYPE instead, we avoid messing up with a possible
21837 typedef variant case. */
21838 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21840 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21841 it first before we can figure out what NAME refers to. */
21842 if (TREE_CODE (scope) == TYPENAME_TYPE)
21844 if (TYPENAME_IS_RESOLVING_P (scope))
21845 /* Given a class template A with a dependent base with nested type C,
21846 typedef typename A::C::C C will land us here, as trying to resolve
21847 the initial A::C leads to the local C typedef, which leads back to
21848 A::C::C. So we break the recursion now. */
21849 return type;
21850 else
21851 scope = resolve_typename_type (scope, only_current_p);
21853 /* If we don't know what SCOPE refers to, then we cannot resolve the
21854 TYPENAME_TYPE. */
21855 if (TREE_CODE (scope) == TYPENAME_TYPE)
21856 return type;
21857 /* If the SCOPE is a template type parameter, we have no way of
21858 resolving the name. */
21859 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21860 return type;
21861 /* If the SCOPE is not the current instantiation, there's no reason
21862 to look inside it. */
21863 if (only_current_p && !currently_open_class (scope))
21864 return type;
21865 /* If this is a typedef, we don't want to look inside (c++/11987). */
21866 if (typedef_variant_p (type))
21867 return type;
21868 /* If SCOPE isn't the template itself, it will not have a valid
21869 TYPE_FIELDS list. */
21870 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21871 /* scope is either the template itself or a compatible instantiation
21872 like X<T>, so look up the name in the original template. */
21873 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21874 else
21875 /* scope is a partial instantiation, so we can't do the lookup or we
21876 will lose the template arguments. */
21877 return type;
21878 /* Enter the SCOPE so that name lookup will be resolved as if we
21879 were in the class definition. In particular, SCOPE will no
21880 longer be considered a dependent type. */
21881 pushed_scope = push_scope (scope);
21882 /* Look up the declaration. */
21883 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21884 tf_warning_or_error);
21886 result = NULL_TREE;
21888 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21889 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21890 if (!decl)
21891 /*nop*/;
21892 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21893 && TREE_CODE (decl) == TYPE_DECL)
21895 result = TREE_TYPE (decl);
21896 if (result == error_mark_node)
21897 result = NULL_TREE;
21899 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21900 && DECL_CLASS_TEMPLATE_P (decl))
21902 tree tmpl;
21903 tree args;
21904 /* Obtain the template and the arguments. */
21905 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21906 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21907 /* Instantiate the template. */
21908 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21909 /*entering_scope=*/0,
21910 tf_error | tf_user);
21911 if (result == error_mark_node)
21912 result = NULL_TREE;
21915 /* Leave the SCOPE. */
21916 if (pushed_scope)
21917 pop_scope (pushed_scope);
21919 /* If we failed to resolve it, return the original typename. */
21920 if (!result)
21921 return type;
21923 /* If lookup found a typename type, resolve that too. */
21924 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21926 /* Ill-formed programs can cause infinite recursion here, so we
21927 must catch that. */
21928 TYPENAME_IS_RESOLVING_P (type) = 1;
21929 result = resolve_typename_type (result, only_current_p);
21930 TYPENAME_IS_RESOLVING_P (type) = 0;
21933 /* Qualify the resulting type. */
21934 quals = cp_type_quals (type);
21935 if (quals)
21936 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21938 return result;
21941 /* EXPR is an expression which is not type-dependent. Return a proxy
21942 for EXPR that can be used to compute the types of larger
21943 expressions containing EXPR. */
21945 tree
21946 build_non_dependent_expr (tree expr)
21948 tree inner_expr;
21950 #ifdef ENABLE_CHECKING
21951 /* Try to get a constant value for all non-dependent expressions in
21952 order to expose bugs in *_dependent_expression_p and constexpr. */
21953 if (cxx_dialect >= cxx11)
21954 fold_non_dependent_expr (expr);
21955 #endif
21957 /* Preserve OVERLOADs; the functions must be available to resolve
21958 types. */
21959 inner_expr = expr;
21960 if (TREE_CODE (inner_expr) == STMT_EXPR)
21961 inner_expr = stmt_expr_value_expr (inner_expr);
21962 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21963 inner_expr = TREE_OPERAND (inner_expr, 0);
21964 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21965 inner_expr = TREE_OPERAND (inner_expr, 1);
21966 if (is_overloaded_fn (inner_expr)
21967 || TREE_CODE (inner_expr) == OFFSET_REF)
21968 return expr;
21969 /* There is no need to return a proxy for a variable. */
21970 if (VAR_P (expr))
21971 return expr;
21972 /* Preserve string constants; conversions from string constants to
21973 "char *" are allowed, even though normally a "const char *"
21974 cannot be used to initialize a "char *". */
21975 if (TREE_CODE (expr) == STRING_CST)
21976 return expr;
21977 /* Preserve void and arithmetic constants, as an optimization -- there is no
21978 reason to create a new node. */
21979 if (TREE_CODE (expr) == VOID_CST
21980 || TREE_CODE (expr) == INTEGER_CST
21981 || TREE_CODE (expr) == REAL_CST)
21982 return expr;
21983 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21984 There is at least one place where we want to know that a
21985 particular expression is a throw-expression: when checking a ?:
21986 expression, there are special rules if the second or third
21987 argument is a throw-expression. */
21988 if (TREE_CODE (expr) == THROW_EXPR)
21989 return expr;
21991 /* Don't wrap an initializer list, we need to be able to look inside. */
21992 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21993 return expr;
21995 /* Don't wrap a dummy object, we need to be able to test for it. */
21996 if (is_dummy_object (expr))
21997 return expr;
21999 if (TREE_CODE (expr) == COND_EXPR)
22000 return build3 (COND_EXPR,
22001 TREE_TYPE (expr),
22002 TREE_OPERAND (expr, 0),
22003 (TREE_OPERAND (expr, 1)
22004 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
22005 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
22006 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
22007 if (TREE_CODE (expr) == COMPOUND_EXPR
22008 && !COMPOUND_EXPR_OVERLOADED (expr))
22009 return build2 (COMPOUND_EXPR,
22010 TREE_TYPE (expr),
22011 TREE_OPERAND (expr, 0),
22012 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
22014 /* If the type is unknown, it can't really be non-dependent */
22015 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
22017 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22018 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
22021 /* ARGS is a vector of expressions as arguments to a function call.
22022 Replace the arguments with equivalent non-dependent expressions.
22023 This modifies ARGS in place. */
22025 void
22026 make_args_non_dependent (vec<tree, va_gc> *args)
22028 unsigned int ix;
22029 tree arg;
22031 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
22033 tree newarg = build_non_dependent_expr (arg);
22034 if (newarg != arg)
22035 (*args)[ix] = newarg;
22039 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22040 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22041 parms. */
22043 static tree
22044 make_auto_1 (tree name)
22046 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
22047 TYPE_NAME (au) = build_decl (input_location,
22048 TYPE_DECL, name, au);
22049 TYPE_STUB_DECL (au) = TYPE_NAME (au);
22050 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
22051 (0, processing_template_decl + 1, processing_template_decl + 1,
22052 TYPE_NAME (au), NULL_TREE);
22053 TYPE_CANONICAL (au) = canonical_type_parameter (au);
22054 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
22055 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
22057 return au;
22060 tree
22061 make_decltype_auto (void)
22063 return make_auto_1 (get_identifier ("decltype(auto)"));
22066 tree
22067 make_auto (void)
22069 return make_auto_1 (get_identifier ("auto"));
22072 /* Given type ARG, return std::initializer_list<ARG>. */
22074 static tree
22075 listify (tree arg)
22077 tree std_init_list = namespace_binding
22078 (get_identifier ("initializer_list"), std_node);
22079 tree argvec;
22080 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
22082 error ("deducing from brace-enclosed initializer list requires "
22083 "#include <initializer_list>");
22084 return error_mark_node;
22086 argvec = make_tree_vec (1);
22087 TREE_VEC_ELT (argvec, 0) = arg;
22088 return lookup_template_class (std_init_list, argvec, NULL_TREE,
22089 NULL_TREE, 0, tf_warning_or_error);
22092 /* Replace auto in TYPE with std::initializer_list<auto>. */
22094 static tree
22095 listify_autos (tree type, tree auto_node)
22097 tree init_auto = listify (auto_node);
22098 tree argvec = make_tree_vec (1);
22099 TREE_VEC_ELT (argvec, 0) = init_auto;
22100 if (processing_template_decl)
22101 argvec = add_to_template_args (current_template_args (), argvec);
22102 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22105 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22106 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22108 tree
22109 do_auto_deduction (tree type, tree init, tree auto_node)
22111 tree targs;
22113 if (init == error_mark_node)
22114 return error_mark_node;
22116 if (type_dependent_expression_p (init))
22117 /* Defining a subset of type-dependent expressions that we can deduce
22118 from ahead of time isn't worth the trouble. */
22119 return type;
22121 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22122 with either a new invented type template parameter U or, if the
22123 initializer is a braced-init-list (8.5.4), with
22124 std::initializer_list<U>. */
22125 if (BRACE_ENCLOSED_INITIALIZER_P (init))
22127 if (!DIRECT_LIST_INIT_P (init))
22128 type = listify_autos (type, auto_node);
22129 else if (CONSTRUCTOR_NELTS (init) == 1)
22130 init = CONSTRUCTOR_ELT (init, 0)->value;
22131 else
22133 if (permerror (input_location, "direct-list-initialization of "
22134 "%<auto%> requires exactly one element"))
22135 inform (input_location,
22136 "for deduction to %<std::initializer_list%>, use copy-"
22137 "list-initialization (i.e. add %<=%> before the %<{%>)");
22138 type = listify_autos (type, auto_node);
22142 init = resolve_nondeduced_context (init);
22144 targs = make_tree_vec (1);
22145 if (AUTO_IS_DECLTYPE (auto_node))
22147 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
22148 && !REF_PARENTHESIZED_P (init)));
22149 TREE_VEC_ELT (targs, 0)
22150 = finish_decltype_type (init, id, tf_warning_or_error);
22151 if (type != auto_node)
22153 error ("%qT as type rather than plain %<decltype(auto)%>", type);
22154 return error_mark_node;
22157 else
22159 tree parms = build_tree_list (NULL_TREE, type);
22160 tree tparms = make_tree_vec (1);
22161 int val;
22163 TREE_VEC_ELT (tparms, 0)
22164 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
22165 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
22166 DEDUCE_CALL, LOOKUP_NORMAL,
22167 NULL, /*explain_p=*/false);
22168 if (val > 0)
22170 if (processing_template_decl)
22171 /* Try again at instantiation time. */
22172 return type;
22173 if (type && type != error_mark_node)
22174 /* If type is error_mark_node a diagnostic must have been
22175 emitted by now. Also, having a mention to '<type error>'
22176 in the diagnostic is not really useful to the user. */
22178 if (cfun && auto_node == current_function_auto_return_pattern
22179 && LAMBDA_FUNCTION_P (current_function_decl))
22180 error ("unable to deduce lambda return type from %qE", init);
22181 else
22182 error ("unable to deduce %qT from %qE", type, init);
22184 return error_mark_node;
22188 /* If the list of declarators contains more than one declarator, the type
22189 of each declared variable is determined as described above. If the
22190 type deduced for the template parameter U is not the same in each
22191 deduction, the program is ill-formed. */
22192 if (TREE_TYPE (auto_node)
22193 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
22195 if (cfun && auto_node == current_function_auto_return_pattern
22196 && LAMBDA_FUNCTION_P (current_function_decl))
22197 error ("inconsistent types %qT and %qT deduced for "
22198 "lambda return type", TREE_TYPE (auto_node),
22199 TREE_VEC_ELT (targs, 0));
22200 else
22201 error ("inconsistent deduction for %qT: %qT and then %qT",
22202 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
22203 return error_mark_node;
22205 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
22207 if (processing_template_decl)
22208 targs = add_to_template_args (current_template_args (), targs);
22209 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
22212 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22213 result. */
22215 tree
22216 splice_late_return_type (tree type, tree late_return_type)
22218 tree argvec;
22220 if (late_return_type == NULL_TREE)
22221 return type;
22222 argvec = make_tree_vec (1);
22223 TREE_VEC_ELT (argvec, 0) = late_return_type;
22224 if (processing_template_parmlist)
22225 /* For a late-specified return type in a template type-parameter, we
22226 need to add a dummy argument level for its parmlist. */
22227 argvec = add_to_template_args
22228 (make_tree_vec (processing_template_parmlist), argvec);
22229 if (current_template_parms)
22230 argvec = add_to_template_args (current_template_args (), argvec);
22231 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22234 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22235 'decltype(auto)'. */
22237 bool
22238 is_auto (const_tree type)
22240 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22241 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
22242 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
22243 return true;
22244 else
22245 return false;
22248 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22249 a use of `auto'. Returns NULL_TREE otherwise. */
22251 tree
22252 type_uses_auto (tree type)
22254 return find_type_usage (type, is_auto);
22257 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22258 'decltype(auto)' or a concept. */
22260 bool
22261 is_auto_or_concept (const_tree type)
22263 return is_auto (type); // or concept
22266 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22267 a concept identifier) iff TYPE contains a use of a generic type. Returns
22268 NULL_TREE otherwise. */
22270 tree
22271 type_uses_auto_or_concept (tree type)
22273 return find_type_usage (type, is_auto_or_concept);
22277 /* For a given template T, return the vector of typedefs referenced
22278 in T for which access check is needed at T instantiation time.
22279 T is either a FUNCTION_DECL or a RECORD_TYPE.
22280 Those typedefs were added to T by the function
22281 append_type_to_template_for_access_check. */
22283 vec<qualified_typedef_usage_t, va_gc> *
22284 get_types_needing_access_check (tree t)
22286 tree ti;
22287 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
22289 if (!t || t == error_mark_node)
22290 return NULL;
22292 if (!(ti = get_template_info (t)))
22293 return NULL;
22295 if (CLASS_TYPE_P (t)
22296 || TREE_CODE (t) == FUNCTION_DECL)
22298 if (!TI_TEMPLATE (ti))
22299 return NULL;
22301 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
22304 return result;
22307 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22308 tied to T. That list of typedefs will be access checked at
22309 T instantiation time.
22310 T is either a FUNCTION_DECL or a RECORD_TYPE.
22311 TYPE_DECL is a TYPE_DECL node representing a typedef.
22312 SCOPE is the scope through which TYPE_DECL is accessed.
22313 LOCATION is the location of the usage point of TYPE_DECL.
22315 This function is a subroutine of
22316 append_type_to_template_for_access_check. */
22318 static void
22319 append_type_to_template_for_access_check_1 (tree t,
22320 tree type_decl,
22321 tree scope,
22322 location_t location)
22324 qualified_typedef_usage_t typedef_usage;
22325 tree ti;
22327 if (!t || t == error_mark_node)
22328 return;
22330 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
22331 || CLASS_TYPE_P (t))
22332 && type_decl
22333 && TREE_CODE (type_decl) == TYPE_DECL
22334 && scope);
22336 if (!(ti = get_template_info (t)))
22337 return;
22339 gcc_assert (TI_TEMPLATE (ti));
22341 typedef_usage.typedef_decl = type_decl;
22342 typedef_usage.context = scope;
22343 typedef_usage.locus = location;
22345 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
22348 /* Append TYPE_DECL to the template TEMPL.
22349 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22350 At TEMPL instanciation time, TYPE_DECL will be checked to see
22351 if it can be accessed through SCOPE.
22352 LOCATION is the location of the usage point of TYPE_DECL.
22354 e.g. consider the following code snippet:
22356 class C
22358 typedef int myint;
22361 template<class U> struct S
22363 C::myint mi; // <-- usage point of the typedef C::myint
22366 S<char> s;
22368 At S<char> instantiation time, we need to check the access of C::myint
22369 In other words, we need to check the access of the myint typedef through
22370 the C scope. For that purpose, this function will add the myint typedef
22371 and the scope C through which its being accessed to a list of typedefs
22372 tied to the template S. That list will be walked at template instantiation
22373 time and access check performed on each typedefs it contains.
22374 Note that this particular code snippet should yield an error because
22375 myint is private to C. */
22377 void
22378 append_type_to_template_for_access_check (tree templ,
22379 tree type_decl,
22380 tree scope,
22381 location_t location)
22383 qualified_typedef_usage_t *iter;
22384 unsigned i;
22386 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
22388 /* Make sure we don't append the type to the template twice. */
22389 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
22390 if (iter->typedef_decl == type_decl && scope == iter->context)
22391 return;
22393 append_type_to_template_for_access_check_1 (templ, type_decl,
22394 scope, location);
22397 /* Convert the generic type parameters in PARM that match the types given in the
22398 range [START_IDX, END_IDX) from the current_template_parms into generic type
22399 packs. */
22401 tree
22402 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22404 tree current = current_template_parms;
22405 int depth = TMPL_PARMS_DEPTH (current);
22406 current = INNERMOST_TEMPLATE_PARMS (current);
22407 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22409 for (int i = 0; i < start_idx; ++i)
22410 TREE_VEC_ELT (replacement, i)
22411 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22413 for (int i = start_idx; i < end_idx; ++i)
22415 /* Create a distinct parameter pack type from the current parm and add it
22416 to the replacement args to tsubst below into the generic function
22417 parameter. */
22419 tree o = TREE_TYPE (TREE_VALUE
22420 (TREE_VEC_ELT (current, i)));
22421 tree t = copy_type (o);
22422 TEMPLATE_TYPE_PARM_INDEX (t)
22423 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22424 o, 0, 0, tf_none);
22425 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22426 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22427 TYPE_MAIN_VARIANT (t) = t;
22428 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22429 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22430 TREE_VEC_ELT (replacement, i) = t;
22431 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22434 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22435 TREE_VEC_ELT (replacement, i)
22436 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22438 /* If there are more levels then build up the replacement with the outer
22439 template parms. */
22440 if (depth > 1)
22441 replacement = add_to_template_args (template_parms_to_args
22442 (TREE_CHAIN (current_template_parms)),
22443 replacement);
22445 return tsubst (parm, replacement, tf_none, NULL_TREE);
22449 /* Set up the hash tables for template instantiations. */
22451 void
22452 init_template_processing (void)
22454 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
22455 type_specializations = hash_table<spec_hasher>::create_ggc (37);
22458 /* Print stats about the template hash tables for -fstats. */
22460 void
22461 print_template_statistics (void)
22463 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22464 "%f collisions\n", (long) decl_specializations->size (),
22465 (long) decl_specializations->elements (),
22466 decl_specializations->collisions ());
22467 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22468 "%f collisions\n", (long) type_specializations->size (),
22469 (long) type_specializations->elements (),
22470 type_specializations->collisions ());
22473 #include "gt-cp-pt.h"