Merge branches/gcc-4_9-branch rev 225109.
[official-gcc.git] / gcc-4_9-branch / gcc / cp / pt.c
blob60e96710eb638327258baade207f58a849633de7
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "pointer-set.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimplify.h"
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
66 int processing_template_parmlist;
67 static int template_header_count;
69 static GTY(()) tree saved_trees;
70 static vec<int> inline_parm_levels;
72 static GTY(()) struct tinst_level *current_tinst_level;
74 static GTY(()) tree saved_access_scope;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth;
84 typedef struct GTY(()) spec_entry
86 tree tmpl;
87 tree args;
88 tree spec;
89 } spec_entry;
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result {
113 tbr_incomplete_type,
114 tbr_ambiguous_baseclass,
115 tbr_success
118 static void push_access_scope (tree);
119 static void pop_access_scope (tree);
120 static bool resolve_overloaded_unification (tree, tree, tree, tree,
121 unification_kind_t, int,
122 bool);
123 static int try_one_overload (tree, tree, tree, tree, tree,
124 unification_kind_t, int, bool, bool);
125 static int unify (tree, tree, tree, tree, int, bool);
126 static void add_pending_template (tree);
127 static tree reopen_tinst_level (struct tinst_level *);
128 static tree tsubst_initializer_list (tree, tree);
129 static tree get_class_bindings (tree, tree, tree, tree);
130 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
131 bool, bool);
132 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
133 bool, bool);
134 static void tsubst_enum (tree, tree, tree);
135 static tree add_to_template_args (tree, tree);
136 static tree add_outermost_template_args (tree, tree);
137 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
138 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
139 tree);
140 static int type_unification_real (tree, tree, tree, const tree *,
141 unsigned int, int, unification_kind_t, int,
142 vec<deferred_access_check, va_gc> **,
143 bool);
144 static void note_template_header (int);
145 static tree convert_nontype_argument_function (tree, tree);
146 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
147 static tree convert_template_argument (tree, tree, tree,
148 tsubst_flags_t, int, tree);
149 static int for_each_template_parm (tree, tree_fn_t, void*,
150 struct pointer_set_t*, bool);
151 static tree expand_template_argument_pack (tree);
152 static tree build_template_parm_index (int, int, int, tree, tree);
153 static bool inline_needs_template_parms (tree, bool);
154 static void push_inline_template_parms_recursive (tree, int);
155 static tree retrieve_local_specialization (tree);
156 static void register_local_specialization (tree, tree);
157 static hashval_t hash_specialization (const void *p);
158 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
159 static int mark_template_parm (tree, void *);
160 static int template_parm_this_level_p (tree, void *);
161 static tree tsubst_friend_function (tree, tree);
162 static tree tsubst_friend_class (tree, tree);
163 static int can_complete_type_without_circularity (tree);
164 static tree get_bindings (tree, tree, tree, bool);
165 static int template_decl_level (tree);
166 static int check_cv_quals_for_unify (int, tree, tree);
167 static void template_parm_level_and_index (tree, int*, int*);
168 static int unify_pack_expansion (tree, tree, tree,
169 tree, unification_kind_t, bool, bool);
170 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
171 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
173 static void regenerate_decl_from_template (tree, tree);
174 static tree most_specialized_class (tree, tsubst_flags_t);
175 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
176 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
177 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
178 static bool check_specialization_scope (void);
179 static tree process_partial_specialization (tree);
180 static void set_current_access_from_decl (tree);
181 static enum template_base_result get_template_base (tree, tree, tree, tree,
182 bool , tree *);
183 static tree try_class_unification (tree, tree, tree, tree, bool);
184 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
185 tree, tree);
186 static bool template_template_parm_bindings_ok_p (tree, tree);
187 static int template_args_equal (tree, tree);
188 static void tsubst_default_arguments (tree, tsubst_flags_t);
189 static tree for_each_template_parm_r (tree *, int *, void *);
190 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
191 static void copy_default_args_to_explicit_spec (tree);
192 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
193 static bool dependent_template_arg_p (tree);
194 static bool any_template_arguments_need_structural_equality_p (tree);
195 static bool dependent_type_p_r (tree);
196 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
197 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
198 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
199 static tree tsubst_decl (tree, tree, tsubst_flags_t);
200 static void perform_typedefs_access_check (tree tmpl, tree targs);
201 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
202 location_t);
203 static tree listify (tree);
204 static tree listify_autos (tree, tree);
205 static tree template_parm_to_arg (tree t);
206 static tree current_template_args (void);
207 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
208 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
215 static void
216 push_access_scope (tree t)
218 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
219 || TREE_CODE (t) == TYPE_DECL);
221 if (DECL_FRIEND_CONTEXT (t))
222 push_nested_class (DECL_FRIEND_CONTEXT (t));
223 else if (DECL_CLASS_SCOPE_P (t))
224 push_nested_class (DECL_CONTEXT (t));
225 else
226 push_to_top_level ();
228 if (TREE_CODE (t) == FUNCTION_DECL)
230 saved_access_scope = tree_cons
231 (NULL_TREE, current_function_decl, saved_access_scope);
232 current_function_decl = t;
236 /* Restore the scope set up by push_access_scope. T is the node we
237 are processing. */
239 static void
240 pop_access_scope (tree t)
242 if (TREE_CODE (t) == FUNCTION_DECL)
244 current_function_decl = TREE_VALUE (saved_access_scope);
245 saved_access_scope = TREE_CHAIN (saved_access_scope);
248 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
249 pop_nested_class ();
250 else
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
259 tree
260 finish_member_template_decl (tree decl)
262 if (decl == error_mark_node)
263 return error_mark_node;
265 gcc_assert (DECL_P (decl));
267 if (TREE_CODE (decl) == TYPE_DECL)
269 tree type;
271 type = TREE_TYPE (decl);
272 if (type == error_mark_node)
273 return error_mark_node;
274 if (MAYBE_CLASS_TYPE_P (type)
275 && CLASSTYPE_TEMPLATE_INFO (type)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
278 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
279 check_member_template (tmpl);
280 return tmpl;
282 return NULL_TREE;
284 else if (TREE_CODE (decl) == FIELD_DECL)
285 error ("data member %qD cannot be a member template", decl);
286 else if (DECL_TEMPLATE_INFO (decl))
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
290 check_member_template (DECL_TI_TEMPLATE (decl));
291 return DECL_TI_TEMPLATE (decl);
293 else
294 return decl;
296 else
297 error ("invalid member template declaration %qD", decl);
299 return error_mark_node;
302 /* Create a template info node. */
304 tree
305 build_template_info (tree template_decl, tree template_args)
307 tree result = make_node (TEMPLATE_INFO);
308 TI_TEMPLATE (result) = template_decl;
309 TI_ARGS (result) = template_args;
310 return result;
313 /* Return the template info node corresponding to T, whatever T is. */
315 tree
316 get_template_info (const_tree t)
318 tree tinfo = NULL_TREE;
320 if (!t || t == error_mark_node)
321 return NULL;
323 if (TREE_CODE (t) == NAMESPACE_DECL)
324 return NULL;
326 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
327 tinfo = DECL_TEMPLATE_INFO (t);
329 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
330 t = TREE_TYPE (t);
332 if (OVERLOAD_TYPE_P (t))
333 tinfo = TYPE_TEMPLATE_INFO (t);
334 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
335 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
337 return tinfo;
340 /* Returns the template nesting level of the indicated class TYPE.
342 For example, in:
343 template <class T>
344 struct A
346 template <class U>
347 struct B {};
350 A<T>::B<U> has depth two, while A<T> has depth one.
351 Both A<T>::B<int> and A<int>::B<U> have depth one, if
352 they are instantiations, not specializations.
354 This function is guaranteed to return 0 if passed NULL_TREE so
355 that, for example, `template_class_depth (current_class_type)' is
356 always safe. */
359 template_class_depth (tree type)
361 int depth;
363 for (depth = 0;
364 type && TREE_CODE (type) != NAMESPACE_DECL;
365 type = (TREE_CODE (type) == FUNCTION_DECL)
366 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
368 tree tinfo = get_template_info (type);
370 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
371 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
372 ++depth;
375 return depth;
378 /* Subroutine of maybe_begin_member_template_processing.
379 Returns true if processing DECL needs us to push template parms. */
381 static bool
382 inline_needs_template_parms (tree decl, bool nsdmi)
384 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
385 return false;
387 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
388 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
391 /* Subroutine of maybe_begin_member_template_processing.
392 Push the template parms in PARMS, starting from LEVELS steps into the
393 chain, and ending at the beginning, since template parms are listed
394 innermost first. */
396 static void
397 push_inline_template_parms_recursive (tree parmlist, int levels)
399 tree parms = TREE_VALUE (parmlist);
400 int i;
402 if (levels > 1)
403 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
405 ++processing_template_decl;
406 current_template_parms
407 = tree_cons (size_int (processing_template_decl),
408 parms, current_template_parms);
409 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
411 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
412 NULL);
413 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
415 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
417 if (error_operand_p (parm))
418 continue;
420 gcc_assert (DECL_P (parm));
422 switch (TREE_CODE (parm))
424 case TYPE_DECL:
425 case TEMPLATE_DECL:
426 pushdecl (parm);
427 break;
429 case PARM_DECL:
431 /* Make a CONST_DECL as is done in process_template_parm.
432 It is ugly that we recreate this here; the original
433 version built in process_template_parm is no longer
434 available. */
435 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
436 CONST_DECL, DECL_NAME (parm),
437 TREE_TYPE (parm));
438 DECL_ARTIFICIAL (decl) = 1;
439 TREE_CONSTANT (decl) = 1;
440 TREE_READONLY (decl) = 1;
441 DECL_INITIAL (decl) = DECL_INITIAL (parm);
442 SET_DECL_TEMPLATE_PARM_P (decl);
443 pushdecl (decl);
445 break;
447 default:
448 gcc_unreachable ();
453 /* Restore the template parameter context for a member template, a
454 friend template defined in a class definition, or a non-template
455 member of template class. */
457 void
458 maybe_begin_member_template_processing (tree decl)
460 tree parms;
461 int levels = 0;
462 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
464 if (nsdmi)
466 tree ctx = DECL_CONTEXT (decl);
467 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
468 /* Disregard full specializations (c++/60999). */
469 && uses_template_parms (ctx)
470 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
473 if (inline_needs_template_parms (decl, nsdmi))
475 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
476 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
478 if (DECL_TEMPLATE_SPECIALIZATION (decl))
480 --levels;
481 parms = TREE_CHAIN (parms);
484 push_inline_template_parms_recursive (parms, levels);
487 /* Remember how many levels of template parameters we pushed so that
488 we can pop them later. */
489 inline_parm_levels.safe_push (levels);
492 /* Undo the effects of maybe_begin_member_template_processing. */
494 void
495 maybe_end_member_template_processing (void)
497 int i;
498 int last;
500 if (inline_parm_levels.length () == 0)
501 return;
503 last = inline_parm_levels.pop ();
504 for (i = 0; i < last; ++i)
506 --processing_template_decl;
507 current_template_parms = TREE_CHAIN (current_template_parms);
508 poplevel (0, 0, 0);
512 /* Return a new template argument vector which contains all of ARGS,
513 but has as its innermost set of arguments the EXTRA_ARGS. */
515 static tree
516 add_to_template_args (tree args, tree extra_args)
518 tree new_args;
519 int extra_depth;
520 int i;
521 int j;
523 if (args == NULL_TREE || extra_args == error_mark_node)
524 return extra_args;
526 extra_depth = TMPL_ARGS_DEPTH (extra_args);
527 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
529 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
530 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
532 for (j = 1; j <= extra_depth; ++j, ++i)
533 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
535 return new_args;
538 /* Like add_to_template_args, but only the outermost ARGS are added to
539 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
540 (EXTRA_ARGS) levels are added. This function is used to combine
541 the template arguments from a partial instantiation with the
542 template arguments used to attain the full instantiation from the
543 partial instantiation. */
545 static tree
546 add_outermost_template_args (tree args, tree extra_args)
548 tree new_args;
550 /* If there are more levels of EXTRA_ARGS than there are ARGS,
551 something very fishy is going on. */
552 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
554 /* If *all* the new arguments will be the EXTRA_ARGS, just return
555 them. */
556 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
557 return extra_args;
559 /* For the moment, we make ARGS look like it contains fewer levels. */
560 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
562 new_args = add_to_template_args (args, extra_args);
564 /* Now, we restore ARGS to its full dimensions. */
565 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
567 return new_args;
570 /* Return the N levels of innermost template arguments from the ARGS. */
572 tree
573 get_innermost_template_args (tree args, int n)
575 tree new_args;
576 int extra_levels;
577 int i;
579 gcc_assert (n >= 0);
581 /* If N is 1, just return the innermost set of template arguments. */
582 if (n == 1)
583 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
585 /* If we're not removing anything, just return the arguments we were
586 given. */
587 extra_levels = TMPL_ARGS_DEPTH (args) - n;
588 gcc_assert (extra_levels >= 0);
589 if (extra_levels == 0)
590 return args;
592 /* Make a new set of arguments, not containing the outer arguments. */
593 new_args = make_tree_vec (n);
594 for (i = 1; i <= n; ++i)
595 SET_TMPL_ARGS_LEVEL (new_args, i,
596 TMPL_ARGS_LEVEL (args, i + extra_levels));
598 return new_args;
601 /* The inverse of get_innermost_template_args: Return all but the innermost
602 EXTRA_LEVELS levels of template arguments from the ARGS. */
604 static tree
605 strip_innermost_template_args (tree args, int extra_levels)
607 tree new_args;
608 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
609 int i;
611 gcc_assert (n >= 0);
613 /* If N is 1, just return the outermost set of template arguments. */
614 if (n == 1)
615 return TMPL_ARGS_LEVEL (args, 1);
617 /* If we're not removing anything, just return the arguments we were
618 given. */
619 gcc_assert (extra_levels >= 0);
620 if (extra_levels == 0)
621 return args;
623 /* Make a new set of arguments, not containing the inner arguments. */
624 new_args = make_tree_vec (n);
625 for (i = 1; i <= n; ++i)
626 SET_TMPL_ARGS_LEVEL (new_args, i,
627 TMPL_ARGS_LEVEL (args, i));
629 return new_args;
632 /* We've got a template header coming up; push to a new level for storing
633 the parms. */
635 void
636 begin_template_parm_list (void)
638 /* We use a non-tag-transparent scope here, which causes pushtag to
639 put tags in this scope, rather than in the enclosing class or
640 namespace scope. This is the right thing, since we want
641 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
642 global template class, push_template_decl handles putting the
643 TEMPLATE_DECL into top-level scope. For a nested template class,
644 e.g.:
646 template <class T> struct S1 {
647 template <class T> struct S2 {};
650 pushtag contains special code to call pushdecl_with_scope on the
651 TEMPLATE_DECL for S2. */
652 begin_scope (sk_template_parms, NULL);
653 ++processing_template_decl;
654 ++processing_template_parmlist;
655 note_template_header (0);
658 /* This routine is called when a specialization is declared. If it is
659 invalid to declare a specialization here, an error is reported and
660 false is returned, otherwise this routine will return true. */
662 static bool
663 check_specialization_scope (void)
665 tree scope = current_scope ();
667 /* [temp.expl.spec]
669 An explicit specialization shall be declared in the namespace of
670 which the template is a member, or, for member templates, in the
671 namespace of which the enclosing class or enclosing class
672 template is a member. An explicit specialization of a member
673 function, member class or static data member of a class template
674 shall be declared in the namespace of which the class template
675 is a member. */
676 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
678 error ("explicit specialization in non-namespace scope %qD", scope);
679 return false;
682 /* [temp.expl.spec]
684 In an explicit specialization declaration for a member of a class
685 template or a member template that appears in namespace scope,
686 the member template and some of its enclosing class templates may
687 remain unspecialized, except that the declaration shall not
688 explicitly specialize a class member template if its enclosing
689 class templates are not explicitly specialized as well. */
690 if (current_template_parms)
692 error ("enclosing class templates are not explicitly specialized");
693 return false;
696 return true;
699 /* We've just seen template <>. */
701 bool
702 begin_specialization (void)
704 begin_scope (sk_template_spec, NULL);
705 note_template_header (1);
706 return check_specialization_scope ();
709 /* Called at then end of processing a declaration preceded by
710 template<>. */
712 void
713 end_specialization (void)
715 finish_scope ();
716 reset_specialization ();
719 /* Any template <>'s that we have seen thus far are not referring to a
720 function specialization. */
722 void
723 reset_specialization (void)
725 processing_specialization = 0;
726 template_header_count = 0;
729 /* We've just seen a template header. If SPECIALIZATION is nonzero,
730 it was of the form template <>. */
732 static void
733 note_template_header (int specialization)
735 processing_specialization = specialization;
736 template_header_count++;
739 /* We're beginning an explicit instantiation. */
741 void
742 begin_explicit_instantiation (void)
744 gcc_assert (!processing_explicit_instantiation);
745 processing_explicit_instantiation = true;
749 void
750 end_explicit_instantiation (void)
752 gcc_assert (processing_explicit_instantiation);
753 processing_explicit_instantiation = false;
756 /* An explicit specialization or partial specialization of TMPL is being
757 declared. Check that the namespace in which the specialization is
758 occurring is permissible. Returns false iff it is invalid to
759 specialize TMPL in the current namespace. */
761 static bool
762 check_specialization_namespace (tree tmpl)
764 tree tpl_ns = decl_namespace_context (tmpl);
766 /* [tmpl.expl.spec]
768 An explicit specialization shall be declared in the namespace of
769 which the template is a member, or, for member templates, in the
770 namespace of which the enclosing class or enclosing class
771 template is a member. An explicit specialization of a member
772 function, member class or static data member of a class template
773 shall be declared in the namespace of which the class template is
774 a member. */
775 if (current_scope() != DECL_CONTEXT (tmpl)
776 && !at_namespace_scope_p ())
778 error ("specialization of %qD must appear at namespace scope", tmpl);
779 return false;
781 if (is_associated_namespace (current_namespace, tpl_ns))
782 /* Same or super-using namespace. */
783 return true;
784 else
786 permerror (input_location, "specialization of %qD in different namespace", tmpl);
787 permerror (input_location, " from definition of %q+#D", tmpl);
788 return false;
792 /* SPEC is an explicit instantiation. Check that it is valid to
793 perform this explicit instantiation in the current namespace. */
795 static void
796 check_explicit_instantiation_namespace (tree spec)
798 tree ns;
800 /* DR 275: An explicit instantiation shall appear in an enclosing
801 namespace of its template. */
802 ns = decl_namespace_context (spec);
803 if (!is_ancestor (current_namespace, ns))
804 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
805 "(which does not enclose namespace %qD)",
806 spec, current_namespace, ns);
809 /* The TYPE is being declared. If it is a template type, that means it
810 is a partial specialization. Do appropriate error-checking. */
812 tree
813 maybe_process_partial_specialization (tree type)
815 tree context;
817 if (type == error_mark_node)
818 return error_mark_node;
820 /* A lambda that appears in specialization context is not itself a
821 specialization. */
822 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
823 return type;
825 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
827 error ("name of class shadows template template parameter %qD",
828 TYPE_NAME (type));
829 return error_mark_node;
832 context = TYPE_CONTEXT (type);
834 if (TYPE_ALIAS_P (type))
836 if (TYPE_TEMPLATE_INFO (type)
837 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
838 error ("specialization of alias template %qD",
839 TYPE_TI_TEMPLATE (type));
840 else
841 error ("explicit specialization of non-template %qT", type);
842 return error_mark_node;
844 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
846 /* This is for ordinary explicit specialization and partial
847 specialization of a template class such as:
849 template <> class C<int>;
853 template <class T> class C<T*>;
855 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
857 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
858 && !COMPLETE_TYPE_P (type))
860 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
861 && !at_namespace_scope_p ())
862 return error_mark_node;
863 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
864 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
865 if (processing_template_decl)
867 if (push_template_decl (TYPE_MAIN_DECL (type))
868 == error_mark_node)
869 return error_mark_node;
872 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
873 error ("specialization of %qT after instantiation", type);
874 else if (errorcount && !processing_specialization
875 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
876 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
877 /* Trying to define a specialization either without a template<> header
878 or in an inappropriate place. We've already given an error, so just
879 bail now so we don't actually define the specialization. */
880 return error_mark_node;
882 else if (CLASS_TYPE_P (type)
883 && !CLASSTYPE_USE_TEMPLATE (type)
884 && CLASSTYPE_TEMPLATE_INFO (type)
885 && context && CLASS_TYPE_P (context)
886 && CLASSTYPE_TEMPLATE_INFO (context))
888 /* This is for an explicit specialization of member class
889 template according to [temp.expl.spec/18]:
891 template <> template <class U> class C<int>::D;
893 The context `C<int>' must be an implicit instantiation.
894 Otherwise this is just a member class template declared
895 earlier like:
897 template <> class C<int> { template <class U> class D; };
898 template <> template <class U> class C<int>::D;
900 In the first case, `C<int>::D' is a specialization of `C<T>::D'
901 while in the second case, `C<int>::D' is a primary template
902 and `C<T>::D' may not exist. */
904 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
905 && !COMPLETE_TYPE_P (type))
907 tree t;
908 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
910 if (current_namespace
911 != decl_namespace_context (tmpl))
913 permerror (input_location, "specializing %q#T in different namespace", type);
914 permerror (input_location, " from definition of %q+#D", tmpl);
917 /* Check for invalid specialization after instantiation:
919 template <> template <> class C<int>::D<int>;
920 template <> template <class U> class C<int>::D; */
922 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
923 t; t = TREE_CHAIN (t))
925 tree inst = TREE_VALUE (t);
926 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
927 || !COMPLETE_OR_OPEN_TYPE_P (inst))
929 /* We already have a full specialization of this partial
930 instantiation, or a full specialization has been
931 looked up but not instantiated. Reassign it to the
932 new member specialization template. */
933 spec_entry elt;
934 spec_entry *entry;
935 void **slot;
937 elt.tmpl = most_general_template (tmpl);
938 elt.args = CLASSTYPE_TI_ARGS (inst);
939 elt.spec = inst;
941 htab_remove_elt (type_specializations, &elt);
943 elt.tmpl = tmpl;
944 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
946 slot = htab_find_slot (type_specializations, &elt, INSERT);
947 entry = ggc_alloc_spec_entry ();
948 *entry = elt;
949 *slot = entry;
951 else
952 /* But if we've had an implicit instantiation, that's a
953 problem ([temp.expl.spec]/6). */
954 error ("specialization %qT after instantiation %qT",
955 type, inst);
958 /* Mark TYPE as a specialization. And as a result, we only
959 have one level of template argument for the innermost
960 class template. */
961 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
962 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
963 CLASSTYPE_TI_ARGS (type)
964 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
967 else if (processing_specialization)
969 /* Someday C++0x may allow for enum template specialization. */
970 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
971 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
972 pedwarn (input_location, OPT_Wpedantic, "template specialization "
973 "of %qD not allowed by ISO C++", type);
974 else
976 error ("explicit specialization of non-template %qT", type);
977 return error_mark_node;
981 return type;
984 /* Returns nonzero if we can optimize the retrieval of specializations
985 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
986 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
988 static inline bool
989 optimize_specialization_lookup_p (tree tmpl)
991 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
992 && DECL_CLASS_SCOPE_P (tmpl)
993 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
994 parameter. */
995 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
996 /* The optimized lookup depends on the fact that the
997 template arguments for the member function template apply
998 purely to the containing class, which is not true if the
999 containing class is an explicit or partial
1000 specialization. */
1001 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1002 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1003 && !DECL_CONV_FN_P (tmpl)
1004 /* It is possible to have a template that is not a member
1005 template and is not a member of a template class:
1007 template <typename T>
1008 struct S { friend A::f(); };
1010 Here, the friend function is a template, but the context does
1011 not have template information. The optimized lookup relies
1012 on having ARGS be the template arguments for both the class
1013 and the function template. */
1014 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1017 /* Retrieve the specialization (in the sense of [temp.spec] - a
1018 specialization is either an instantiation or an explicit
1019 specialization) of TMPL for the given template ARGS. If there is
1020 no such specialization, return NULL_TREE. The ARGS are a vector of
1021 arguments, or a vector of vectors of arguments, in the case of
1022 templates with more than one level of parameters.
1024 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1025 then we search for a partial specialization matching ARGS. This
1026 parameter is ignored if TMPL is not a class template.
1028 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1029 result is a NONTYPE_ARGUMENT_PACK. */
1031 static tree
1032 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1034 if (tmpl == NULL_TREE)
1035 return NULL_TREE;
1037 if (args == error_mark_node)
1038 return NULL_TREE;
1040 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1041 || TREE_CODE (tmpl) == FIELD_DECL);
1043 /* There should be as many levels of arguments as there are
1044 levels of parameters. */
1045 gcc_assert (TMPL_ARGS_DEPTH (args)
1046 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1047 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1048 : template_class_depth (DECL_CONTEXT (tmpl))));
1050 if (optimize_specialization_lookup_p (tmpl))
1052 tree class_template;
1053 tree class_specialization;
1054 vec<tree, va_gc> *methods;
1055 tree fns;
1056 int idx;
1058 /* The template arguments actually apply to the containing
1059 class. Find the class specialization with those
1060 arguments. */
1061 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1062 class_specialization
1063 = retrieve_specialization (class_template, args, 0);
1064 if (!class_specialization)
1065 return NULL_TREE;
1066 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1067 for the specialization. */
1068 idx = class_method_index_for_fn (class_specialization, tmpl);
1069 if (idx == -1)
1070 return NULL_TREE;
1071 /* Iterate through the methods with the indicated name, looking
1072 for the one that has an instance of TMPL. */
1073 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1074 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1076 tree fn = OVL_CURRENT (fns);
1077 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1078 /* using-declarations can add base methods to the method vec,
1079 and we don't want those here. */
1080 && DECL_CONTEXT (fn) == class_specialization)
1081 return fn;
1083 return NULL_TREE;
1085 else
1087 spec_entry *found;
1088 spec_entry elt;
1089 htab_t specializations;
1091 elt.tmpl = tmpl;
1092 elt.args = args;
1093 elt.spec = NULL_TREE;
1095 if (DECL_CLASS_TEMPLATE_P (tmpl))
1096 specializations = type_specializations;
1097 else
1098 specializations = decl_specializations;
1100 if (hash == 0)
1101 hash = hash_specialization (&elt);
1102 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1103 if (found)
1104 return found->spec;
1107 return NULL_TREE;
1110 /* Like retrieve_specialization, but for local declarations. */
1112 static tree
1113 retrieve_local_specialization (tree tmpl)
1115 void **slot;
1117 if (local_specializations == NULL)
1118 return NULL_TREE;
1120 slot = pointer_map_contains (local_specializations, tmpl);
1121 return slot ? (tree) *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 void **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 = hash_specialization (&elt);
1384 slot =
1385 htab_find_slot_with_hash (decl_specializations, &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 /* TMPL is a forward declaration of a template function; keep a list
1492 of all specializations in case we need to reassign them to a friend
1493 template later in tsubst_friend_function. */
1494 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1495 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1498 return spec;
1501 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1502 TMPL and ARGS members, ignores SPEC. */
1504 int comparing_specializations;
1506 static int
1507 eq_specializations (const void *p1, const void *p2)
1509 const spec_entry *e1 = (const spec_entry *)p1;
1510 const spec_entry *e2 = (const spec_entry *)p2;
1511 int equal;
1513 ++comparing_specializations;
1514 equal = (e1->tmpl == e2->tmpl
1515 && comp_template_args (e1->args, e2->args));
1516 --comparing_specializations;
1518 return equal;
1521 /* Returns a hash for a template TMPL and template arguments ARGS. */
1523 static hashval_t
1524 hash_tmpl_and_args (tree tmpl, tree args)
1526 hashval_t val = DECL_UID (tmpl);
1527 return iterative_hash_template_arg (args, val);
1530 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1531 ignoring SPEC. */
1533 static hashval_t
1534 hash_specialization (const void *p)
1536 const spec_entry *e = (const spec_entry *)p;
1537 return hash_tmpl_and_args (e->tmpl, e->args);
1540 /* Recursively calculate a hash value for a template argument ARG, for use
1541 in the hash tables of template specializations. */
1543 hashval_t
1544 iterative_hash_template_arg (tree arg, hashval_t val)
1546 unsigned HOST_WIDE_INT i;
1547 enum tree_code code;
1548 char tclass;
1550 if (arg == NULL_TREE)
1551 return iterative_hash_object (arg, val);
1553 if (!TYPE_P (arg))
1554 STRIP_NOPS (arg);
1556 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1557 /* We can get one of these when re-hashing a previous entry in the middle
1558 of substituting into a pack expansion. Just look through it. */
1559 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1561 code = TREE_CODE (arg);
1562 tclass = TREE_CODE_CLASS (code);
1564 val = iterative_hash_object (code, val);
1566 switch (code)
1568 case ERROR_MARK:
1569 return val;
1571 case IDENTIFIER_NODE:
1572 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1574 case TREE_VEC:
1576 int i, len = TREE_VEC_LENGTH (arg);
1577 for (i = 0; i < len; ++i)
1578 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1579 return val;
1582 case TYPE_PACK_EXPANSION:
1583 case EXPR_PACK_EXPANSION:
1584 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1585 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1587 case TYPE_ARGUMENT_PACK:
1588 case NONTYPE_ARGUMENT_PACK:
1589 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1591 case TREE_LIST:
1592 for (; arg; arg = TREE_CHAIN (arg))
1593 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1594 return val;
1596 case OVERLOAD:
1597 for (; arg; arg = OVL_NEXT (arg))
1598 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1599 return val;
1601 case CONSTRUCTOR:
1603 tree field, value;
1604 iterative_hash_template_arg (TREE_TYPE (arg), val);
1605 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1607 val = iterative_hash_template_arg (field, val);
1608 val = iterative_hash_template_arg (value, val);
1610 return val;
1613 case PARM_DECL:
1614 if (!DECL_ARTIFICIAL (arg))
1616 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1617 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1619 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1621 case TARGET_EXPR:
1622 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1624 case PTRMEM_CST:
1625 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1626 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1628 case TEMPLATE_PARM_INDEX:
1629 val = iterative_hash_template_arg
1630 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1631 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1632 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1634 case TRAIT_EXPR:
1635 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1636 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1637 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1639 case BASELINK:
1640 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1641 val);
1642 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1643 val);
1645 case MODOP_EXPR:
1646 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1647 code = TREE_CODE (TREE_OPERAND (arg, 1));
1648 val = iterative_hash_object (code, val);
1649 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1651 case LAMBDA_EXPR:
1652 /* A lambda can't appear in a template arg, but don't crash on
1653 erroneous input. */
1654 gcc_assert (seen_error ());
1655 return val;
1657 case CAST_EXPR:
1658 case IMPLICIT_CONV_EXPR:
1659 case STATIC_CAST_EXPR:
1660 case REINTERPRET_CAST_EXPR:
1661 case CONST_CAST_EXPR:
1662 case DYNAMIC_CAST_EXPR:
1663 case NEW_EXPR:
1664 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1665 /* Now hash operands as usual. */
1666 break;
1668 default:
1669 break;
1672 switch (tclass)
1674 case tcc_type:
1675 if (TYPE_CANONICAL (arg))
1676 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1677 val);
1678 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1679 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1680 /* Otherwise just compare the types during lookup. */
1681 return val;
1683 case tcc_declaration:
1684 case tcc_constant:
1685 return iterative_hash_expr (arg, val);
1687 default:
1688 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1690 unsigned n = cp_tree_operand_length (arg);
1691 for (i = 0; i < n; ++i)
1692 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1693 return val;
1696 gcc_unreachable ();
1697 return 0;
1700 /* Unregister the specialization SPEC as a specialization of TMPL.
1701 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1702 if the SPEC was listed as a specialization of TMPL.
1704 Note that SPEC has been ggc_freed, so we can't look inside it. */
1706 bool
1707 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1709 spec_entry *entry;
1710 spec_entry elt;
1712 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1713 elt.args = TI_ARGS (tinfo);
1714 elt.spec = NULL_TREE;
1716 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1717 if (entry != NULL)
1719 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1720 gcc_assert (new_spec != NULL_TREE);
1721 entry->spec = new_spec;
1722 return 1;
1725 return 0;
1728 /* Like register_specialization, but for local declarations. We are
1729 registering SPEC, an instantiation of TMPL. */
1731 static void
1732 register_local_specialization (tree spec, tree tmpl)
1734 void **slot;
1736 slot = pointer_map_insert (local_specializations, tmpl);
1737 *slot = spec;
1740 /* TYPE is a class type. Returns true if TYPE is an explicitly
1741 specialized class. */
1743 bool
1744 explicit_class_specialization_p (tree type)
1746 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1747 return false;
1748 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1751 /* Print the list of functions at FNS, going through all the overloads
1752 for each element of the list. Alternatively, FNS can not be a
1753 TREE_LIST, in which case it will be printed together with all the
1754 overloads.
1756 MORE and *STR should respectively be FALSE and NULL when the function
1757 is called from the outside. They are used internally on recursive
1758 calls. print_candidates manages the two parameters and leaves NULL
1759 in *STR when it ends. */
1761 static void
1762 print_candidates_1 (tree fns, bool more, const char **str)
1764 tree fn, fn2;
1765 char *spaces = NULL;
1767 for (fn = fns; fn; fn = OVL_NEXT (fn))
1768 if (TREE_CODE (fn) == TREE_LIST)
1770 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1771 print_candidates_1 (TREE_VALUE (fn2),
1772 TREE_CHAIN (fn2) || more, str);
1774 else
1776 tree cand = OVL_CURRENT (fn);
1777 if (!*str)
1779 /* Pick the prefix string. */
1780 if (!more && !OVL_NEXT (fns))
1782 inform (DECL_SOURCE_LOCATION (cand),
1783 "candidate is: %#D", cand);
1784 continue;
1787 *str = _("candidates are:");
1788 spaces = get_spaces (*str);
1790 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1791 *str = spaces ? spaces : *str;
1794 if (!more)
1796 free (spaces);
1797 *str = NULL;
1801 /* Print the list of candidate FNS in an error message. FNS can also
1802 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1804 void
1805 print_candidates (tree fns)
1807 const char *str = NULL;
1808 print_candidates_1 (fns, false, &str);
1809 gcc_assert (str == NULL);
1812 /* Returns the template (one of the functions given by TEMPLATE_ID)
1813 which can be specialized to match the indicated DECL with the
1814 explicit template args given in TEMPLATE_ID. The DECL may be
1815 NULL_TREE if none is available. In that case, the functions in
1816 TEMPLATE_ID are non-members.
1818 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1819 specialization of a member template.
1821 The TEMPLATE_COUNT is the number of references to qualifying
1822 template classes that appeared in the name of the function. See
1823 check_explicit_specialization for a more accurate description.
1825 TSK indicates what kind of template declaration (if any) is being
1826 declared. TSK_TEMPLATE indicates that the declaration given by
1827 DECL, though a FUNCTION_DECL, has template parameters, and is
1828 therefore a template function.
1830 The template args (those explicitly specified and those deduced)
1831 are output in a newly created vector *TARGS_OUT.
1833 If it is impossible to determine the result, an error message is
1834 issued. The error_mark_node is returned to indicate failure. */
1836 static tree
1837 determine_specialization (tree template_id,
1838 tree decl,
1839 tree* targs_out,
1840 int need_member_template,
1841 int template_count,
1842 tmpl_spec_kind tsk)
1844 tree fns;
1845 tree targs;
1846 tree explicit_targs;
1847 tree candidates = NULL_TREE;
1848 /* A TREE_LIST of templates of which DECL may be a specialization.
1849 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1850 corresponding TREE_PURPOSE is the set of template arguments that,
1851 when used to instantiate the template, would produce a function
1852 with the signature of DECL. */
1853 tree templates = NULL_TREE;
1854 int header_count;
1855 cp_binding_level *b;
1857 *targs_out = NULL_TREE;
1859 if (template_id == error_mark_node || decl == error_mark_node)
1860 return error_mark_node;
1862 /* We shouldn't be specializing a member template of an
1863 unspecialized class template; we already gave an error in
1864 check_specialization_scope, now avoid crashing. */
1865 if (template_count && DECL_CLASS_SCOPE_P (decl)
1866 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1868 gcc_assert (errorcount);
1869 return error_mark_node;
1872 fns = TREE_OPERAND (template_id, 0);
1873 explicit_targs = TREE_OPERAND (template_id, 1);
1875 if (fns == error_mark_node)
1876 return error_mark_node;
1878 /* Check for baselinks. */
1879 if (BASELINK_P (fns))
1880 fns = BASELINK_FUNCTIONS (fns);
1882 if (!is_overloaded_fn (fns))
1884 error ("%qD is not a function template", fns);
1885 return error_mark_node;
1888 /* Count the number of template headers specified for this
1889 specialization. */
1890 header_count = 0;
1891 for (b = current_binding_level;
1892 b->kind == sk_template_parms;
1893 b = b->level_chain)
1894 ++header_count;
1896 for (; fns; fns = OVL_NEXT (fns))
1898 tree fn = OVL_CURRENT (fns);
1900 if (TREE_CODE (fn) == TEMPLATE_DECL)
1902 tree decl_arg_types;
1903 tree fn_arg_types;
1904 tree insttype;
1906 /* In case of explicit specialization, we need to check if
1907 the number of template headers appearing in the specialization
1908 is correct. This is usually done in check_explicit_specialization,
1909 but the check done there cannot be exhaustive when specializing
1910 member functions. Consider the following code:
1912 template <> void A<int>::f(int);
1913 template <> template <> void A<int>::f(int);
1915 Assuming that A<int> is not itself an explicit specialization
1916 already, the first line specializes "f" which is a non-template
1917 member function, whilst the second line specializes "f" which
1918 is a template member function. So both lines are syntactically
1919 correct, and check_explicit_specialization does not reject
1920 them.
1922 Here, we can do better, as we are matching the specialization
1923 against the declarations. We count the number of template
1924 headers, and we check if they match TEMPLATE_COUNT + 1
1925 (TEMPLATE_COUNT is the number of qualifying template classes,
1926 plus there must be another header for the member template
1927 itself).
1929 Notice that if header_count is zero, this is not a
1930 specialization but rather a template instantiation, so there
1931 is no check we can perform here. */
1932 if (header_count && header_count != template_count + 1)
1933 continue;
1935 /* Check that the number of template arguments at the
1936 innermost level for DECL is the same as for FN. */
1937 if (current_binding_level->kind == sk_template_parms
1938 && !current_binding_level->explicit_spec_p
1939 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1940 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1941 (current_template_parms))))
1942 continue;
1944 /* DECL might be a specialization of FN. */
1945 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1946 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1948 /* For a non-static member function, we need to make sure
1949 that the const qualification is the same. Since
1950 get_bindings does not try to merge the "this" parameter,
1951 we must do the comparison explicitly. */
1952 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1953 && !same_type_p (TREE_VALUE (fn_arg_types),
1954 TREE_VALUE (decl_arg_types)))
1955 continue;
1957 /* Skip the "this" parameter and, for constructors of
1958 classes with virtual bases, the VTT parameter. A
1959 full specialization of a constructor will have a VTT
1960 parameter, but a template never will. */
1961 decl_arg_types
1962 = skip_artificial_parms_for (decl, decl_arg_types);
1963 fn_arg_types
1964 = skip_artificial_parms_for (fn, fn_arg_types);
1966 /* Function templates cannot be specializations; there are
1967 no partial specializations of functions. Therefore, if
1968 the type of DECL does not match FN, there is no
1969 match. */
1970 if (tsk == tsk_template)
1972 if (compparms (fn_arg_types, decl_arg_types))
1973 candidates = tree_cons (NULL_TREE, fn, candidates);
1974 continue;
1977 /* See whether this function might be a specialization of this
1978 template. Suppress access control because we might be trying
1979 to make this specialization a friend, and we have already done
1980 access control for the declaration of the specialization. */
1981 push_deferring_access_checks (dk_no_check);
1982 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1983 pop_deferring_access_checks ();
1985 if (!targs)
1986 /* We cannot deduce template arguments that when used to
1987 specialize TMPL will produce DECL. */
1988 continue;
1990 /* Make sure that the deduced arguments actually work. */
1991 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1992 if (insttype == error_mark_node)
1993 continue;
1994 fn_arg_types
1995 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1996 if (!compparms (fn_arg_types, decl_arg_types))
1997 continue;
1999 /* Save this template, and the arguments deduced. */
2000 templates = tree_cons (targs, fn, templates);
2002 else if (need_member_template)
2003 /* FN is an ordinary member function, and we need a
2004 specialization of a member template. */
2006 else if (TREE_CODE (fn) != FUNCTION_DECL)
2007 /* We can get IDENTIFIER_NODEs here in certain erroneous
2008 cases. */
2010 else if (!DECL_FUNCTION_MEMBER_P (fn))
2011 /* This is just an ordinary non-member function. Nothing can
2012 be a specialization of that. */
2014 else if (DECL_ARTIFICIAL (fn))
2015 /* Cannot specialize functions that are created implicitly. */
2017 else
2019 tree decl_arg_types;
2021 /* This is an ordinary member function. However, since
2022 we're here, we can assume its enclosing class is a
2023 template class. For example,
2025 template <typename T> struct S { void f(); };
2026 template <> void S<int>::f() {}
2028 Here, S<int>::f is a non-template, but S<int> is a
2029 template class. If FN has the same type as DECL, we
2030 might be in business. */
2032 if (!DECL_TEMPLATE_INFO (fn))
2033 /* Its enclosing class is an explicit specialization
2034 of a template class. This is not a candidate. */
2035 continue;
2037 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2038 TREE_TYPE (TREE_TYPE (fn))))
2039 /* The return types differ. */
2040 continue;
2042 /* Adjust the type of DECL in case FN is a static member. */
2043 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2044 if (DECL_STATIC_FUNCTION_P (fn)
2045 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2046 decl_arg_types = TREE_CHAIN (decl_arg_types);
2048 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2049 decl_arg_types))
2050 /* They match! */
2051 candidates = tree_cons (NULL_TREE, fn, candidates);
2055 if (templates && TREE_CHAIN (templates))
2057 /* We have:
2059 [temp.expl.spec]
2061 It is possible for a specialization with a given function
2062 signature to be instantiated from more than one function
2063 template. In such cases, explicit specification of the
2064 template arguments must be used to uniquely identify the
2065 function template specialization being specialized.
2067 Note that here, there's no suggestion that we're supposed to
2068 determine which of the candidate templates is most
2069 specialized. However, we, also have:
2071 [temp.func.order]
2073 Partial ordering of overloaded function template
2074 declarations is used in the following contexts to select
2075 the function template to which a function template
2076 specialization refers:
2078 -- when an explicit specialization refers to a function
2079 template.
2081 So, we do use the partial ordering rules, at least for now.
2082 This extension can only serve to make invalid programs valid,
2083 so it's safe. And, there is strong anecdotal evidence that
2084 the committee intended the partial ordering rules to apply;
2085 the EDG front end has that behavior, and John Spicer claims
2086 that the committee simply forgot to delete the wording in
2087 [temp.expl.spec]. */
2088 tree tmpl = most_specialized_instantiation (templates);
2089 if (tmpl != error_mark_node)
2091 templates = tmpl;
2092 TREE_CHAIN (templates) = NULL_TREE;
2096 if (templates == NULL_TREE && candidates == NULL_TREE)
2098 error ("template-id %qD for %q+D does not match any template "
2099 "declaration", template_id, decl);
2100 if (header_count && header_count != template_count + 1)
2101 inform (input_location, "saw %d %<template<>%>, need %d for "
2102 "specializing a member function template",
2103 header_count, template_count + 1);
2104 return error_mark_node;
2106 else if ((templates && TREE_CHAIN (templates))
2107 || (candidates && TREE_CHAIN (candidates))
2108 || (templates && candidates))
2110 error ("ambiguous template specialization %qD for %q+D",
2111 template_id, decl);
2112 candidates = chainon (candidates, templates);
2113 print_candidates (candidates);
2114 return error_mark_node;
2117 /* We have one, and exactly one, match. */
2118 if (candidates)
2120 tree fn = TREE_VALUE (candidates);
2121 *targs_out = copy_node (DECL_TI_ARGS (fn));
2122 /* DECL is a re-declaration or partial instantiation of a template
2123 function. */
2124 if (TREE_CODE (fn) == TEMPLATE_DECL)
2125 return fn;
2126 /* It was a specialization of an ordinary member function in a
2127 template class. */
2128 return DECL_TI_TEMPLATE (fn);
2131 /* It was a specialization of a template. */
2132 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2133 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2135 *targs_out = copy_node (targs);
2136 SET_TMPL_ARGS_LEVEL (*targs_out,
2137 TMPL_ARGS_DEPTH (*targs_out),
2138 TREE_PURPOSE (templates));
2140 else
2141 *targs_out = TREE_PURPOSE (templates);
2142 return TREE_VALUE (templates);
2145 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2146 but with the default argument values filled in from those in the
2147 TMPL_TYPES. */
2149 static tree
2150 copy_default_args_to_explicit_spec_1 (tree spec_types,
2151 tree tmpl_types)
2153 tree new_spec_types;
2155 if (!spec_types)
2156 return NULL_TREE;
2158 if (spec_types == void_list_node)
2159 return void_list_node;
2161 /* Substitute into the rest of the list. */
2162 new_spec_types =
2163 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2164 TREE_CHAIN (tmpl_types));
2166 /* Add the default argument for this parameter. */
2167 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2168 TREE_VALUE (spec_types),
2169 new_spec_types);
2172 /* DECL is an explicit specialization. Replicate default arguments
2173 from the template it specializes. (That way, code like:
2175 template <class T> void f(T = 3);
2176 template <> void f(double);
2177 void g () { f (); }
2179 works, as required.) An alternative approach would be to look up
2180 the correct default arguments at the call-site, but this approach
2181 is consistent with how implicit instantiations are handled. */
2183 static void
2184 copy_default_args_to_explicit_spec (tree decl)
2186 tree tmpl;
2187 tree spec_types;
2188 tree tmpl_types;
2189 tree new_spec_types;
2190 tree old_type;
2191 tree new_type;
2192 tree t;
2193 tree object_type = NULL_TREE;
2194 tree in_charge = NULL_TREE;
2195 tree vtt = NULL_TREE;
2197 /* See if there's anything we need to do. */
2198 tmpl = DECL_TI_TEMPLATE (decl);
2199 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2200 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2201 if (TREE_PURPOSE (t))
2202 break;
2203 if (!t)
2204 return;
2206 old_type = TREE_TYPE (decl);
2207 spec_types = TYPE_ARG_TYPES (old_type);
2209 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2211 /* Remove the this pointer, but remember the object's type for
2212 CV quals. */
2213 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2214 spec_types = TREE_CHAIN (spec_types);
2215 tmpl_types = TREE_CHAIN (tmpl_types);
2217 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2219 /* DECL may contain more parameters than TMPL due to the extra
2220 in-charge parameter in constructors and destructors. */
2221 in_charge = spec_types;
2222 spec_types = TREE_CHAIN (spec_types);
2224 if (DECL_HAS_VTT_PARM_P (decl))
2226 vtt = spec_types;
2227 spec_types = TREE_CHAIN (spec_types);
2231 /* Compute the merged default arguments. */
2232 new_spec_types =
2233 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2235 /* Compute the new FUNCTION_TYPE. */
2236 if (object_type)
2238 if (vtt)
2239 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2240 TREE_VALUE (vtt),
2241 new_spec_types);
2243 if (in_charge)
2244 /* Put the in-charge parameter back. */
2245 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2246 TREE_VALUE (in_charge),
2247 new_spec_types);
2249 new_type = build_method_type_directly (object_type,
2250 TREE_TYPE (old_type),
2251 new_spec_types);
2253 else
2254 new_type = build_function_type (TREE_TYPE (old_type),
2255 new_spec_types);
2256 new_type = cp_build_type_attribute_variant (new_type,
2257 TYPE_ATTRIBUTES (old_type));
2258 new_type = build_exception_variant (new_type,
2259 TYPE_RAISES_EXCEPTIONS (old_type));
2260 TREE_TYPE (decl) = new_type;
2263 /* Return the number of template headers we expect to see for a definition
2264 or specialization of CTYPE or one of its non-template members. */
2267 num_template_headers_for_class (tree ctype)
2269 int num_templates = 0;
2271 while (ctype && CLASS_TYPE_P (ctype))
2273 /* You're supposed to have one `template <...>' for every
2274 template class, but you don't need one for a full
2275 specialization. For example:
2277 template <class T> struct S{};
2278 template <> struct S<int> { void f(); };
2279 void S<int>::f () {}
2281 is correct; there shouldn't be a `template <>' for the
2282 definition of `S<int>::f'. */
2283 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2284 /* If CTYPE does not have template information of any
2285 kind, then it is not a template, nor is it nested
2286 within a template. */
2287 break;
2288 if (explicit_class_specialization_p (ctype))
2289 break;
2290 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2291 ++num_templates;
2293 ctype = TYPE_CONTEXT (ctype);
2296 return num_templates;
2299 /* Do a simple sanity check on the template headers that precede the
2300 variable declaration DECL. */
2302 void
2303 check_template_variable (tree decl)
2305 tree ctx = CP_DECL_CONTEXT (decl);
2306 int wanted = num_template_headers_for_class (ctx);
2307 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2308 permerror (DECL_SOURCE_LOCATION (decl),
2309 "%qD is not a static data member of a class template", decl);
2310 else if (template_header_count > wanted)
2312 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2313 "too many template headers for %D (should be %d)",
2314 decl, wanted);
2315 if (warned && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2316 inform (DECL_SOURCE_LOCATION (decl),
2317 "members of an explicitly specialized class are defined "
2318 "without a template header");
2322 /* Check to see if the function just declared, as indicated in
2323 DECLARATOR, and in DECL, is a specialization of a function
2324 template. We may also discover that the declaration is an explicit
2325 instantiation at this point.
2327 Returns DECL, or an equivalent declaration that should be used
2328 instead if all goes well. Issues an error message if something is
2329 amiss. Returns error_mark_node if the error is not easily
2330 recoverable.
2332 FLAGS is a bitmask consisting of the following flags:
2334 2: The function has a definition.
2335 4: The function is a friend.
2337 The TEMPLATE_COUNT is the number of references to qualifying
2338 template classes that appeared in the name of the function. For
2339 example, in
2341 template <class T> struct S { void f(); };
2342 void S<int>::f();
2344 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2345 classes are not counted in the TEMPLATE_COUNT, so that in
2347 template <class T> struct S {};
2348 template <> struct S<int> { void f(); }
2349 template <> void S<int>::f();
2351 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2352 invalid; there should be no template <>.)
2354 If the function is a specialization, it is marked as such via
2355 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2356 is set up correctly, and it is added to the list of specializations
2357 for that template. */
2359 tree
2360 check_explicit_specialization (tree declarator,
2361 tree decl,
2362 int template_count,
2363 int flags)
2365 int have_def = flags & 2;
2366 int is_friend = flags & 4;
2367 int specialization = 0;
2368 int explicit_instantiation = 0;
2369 int member_specialization = 0;
2370 tree ctype = DECL_CLASS_CONTEXT (decl);
2371 tree dname = DECL_NAME (decl);
2372 tmpl_spec_kind tsk;
2374 if (is_friend)
2376 if (!processing_specialization)
2377 tsk = tsk_none;
2378 else
2379 tsk = tsk_excessive_parms;
2381 else
2382 tsk = current_tmpl_spec_kind (template_count);
2384 switch (tsk)
2386 case tsk_none:
2387 if (processing_specialization)
2389 specialization = 1;
2390 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2392 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2394 if (is_friend)
2395 /* This could be something like:
2397 template <class T> void f(T);
2398 class S { friend void f<>(int); } */
2399 specialization = 1;
2400 else
2402 /* This case handles bogus declarations like template <>
2403 template <class T> void f<int>(); */
2405 error ("template-id %qD in declaration of primary template",
2406 declarator);
2407 return decl;
2410 break;
2412 case tsk_invalid_member_spec:
2413 /* The error has already been reported in
2414 check_specialization_scope. */
2415 return error_mark_node;
2417 case tsk_invalid_expl_inst:
2418 error ("template parameter list used in explicit instantiation");
2420 /* Fall through. */
2422 case tsk_expl_inst:
2423 if (have_def)
2424 error ("definition provided for explicit instantiation");
2426 explicit_instantiation = 1;
2427 break;
2429 case tsk_excessive_parms:
2430 case tsk_insufficient_parms:
2431 if (tsk == tsk_excessive_parms)
2432 error ("too many template parameter lists in declaration of %qD",
2433 decl);
2434 else if (template_header_count)
2435 error("too few template parameter lists in declaration of %qD", decl);
2436 else
2437 error("explicit specialization of %qD must be introduced by "
2438 "%<template <>%>", decl);
2440 /* Fall through. */
2441 case tsk_expl_spec:
2442 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2443 if (ctype)
2444 member_specialization = 1;
2445 else
2446 specialization = 1;
2447 break;
2449 case tsk_template:
2450 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2452 /* This case handles bogus declarations like template <>
2453 template <class T> void f<int>(); */
2455 if (uses_template_parms (declarator))
2456 error ("function template partial specialization %qD "
2457 "is not allowed", declarator);
2458 else
2459 error ("template-id %qD in declaration of primary template",
2460 declarator);
2461 return decl;
2464 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2465 /* This is a specialization of a member template, without
2466 specialization the containing class. Something like:
2468 template <class T> struct S {
2469 template <class U> void f (U);
2471 template <> template <class U> void S<int>::f(U) {}
2473 That's a specialization -- but of the entire template. */
2474 specialization = 1;
2475 break;
2477 default:
2478 gcc_unreachable ();
2481 if (specialization || member_specialization)
2483 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2484 for (; t; t = TREE_CHAIN (t))
2485 if (TREE_PURPOSE (t))
2487 permerror (input_location,
2488 "default argument specified in explicit specialization");
2489 break;
2493 if (specialization || member_specialization || explicit_instantiation)
2495 tree tmpl = NULL_TREE;
2496 tree targs = NULL_TREE;
2498 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2499 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2501 tree fns;
2503 gcc_assert (identifier_p (declarator));
2504 if (ctype)
2505 fns = dname;
2506 else
2508 /* If there is no class context, the explicit instantiation
2509 must be at namespace scope. */
2510 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2512 /* Find the namespace binding, using the declaration
2513 context. */
2514 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2515 false, true);
2516 if (fns == error_mark_node || !is_overloaded_fn (fns))
2518 error ("%qD is not a template function", dname);
2519 fns = error_mark_node;
2521 else
2523 tree fn = OVL_CURRENT (fns);
2524 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2525 CP_DECL_CONTEXT (fn)))
2526 error ("%qD is not declared in %qD",
2527 decl, current_namespace);
2531 declarator = lookup_template_function (fns, NULL_TREE);
2534 if (declarator == error_mark_node)
2535 return error_mark_node;
2537 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2539 if (!explicit_instantiation)
2540 /* A specialization in class scope. This is invalid,
2541 but the error will already have been flagged by
2542 check_specialization_scope. */
2543 return error_mark_node;
2544 else
2546 /* It's not valid to write an explicit instantiation in
2547 class scope, e.g.:
2549 class C { template void f(); }
2551 This case is caught by the parser. However, on
2552 something like:
2554 template class C { void f(); };
2556 (which is invalid) we can get here. The error will be
2557 issued later. */
2561 return decl;
2563 else if (ctype != NULL_TREE
2564 && (identifier_p (TREE_OPERAND (declarator, 0))))
2566 /* Find the list of functions in ctype that have the same
2567 name as the declared function. */
2568 tree name = TREE_OPERAND (declarator, 0);
2569 tree fns = NULL_TREE;
2570 int idx;
2572 if (constructor_name_p (name, ctype))
2574 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2576 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2577 : !CLASSTYPE_DESTRUCTORS (ctype))
2579 /* From [temp.expl.spec]:
2581 If such an explicit specialization for the member
2582 of a class template names an implicitly-declared
2583 special member function (clause _special_), the
2584 program is ill-formed.
2586 Similar language is found in [temp.explicit]. */
2587 error ("specialization of implicitly-declared special member function");
2588 return error_mark_node;
2591 name = is_constructor ? ctor_identifier : dtor_identifier;
2594 if (!DECL_CONV_FN_P (decl))
2596 idx = lookup_fnfields_1 (ctype, name);
2597 if (idx >= 0)
2598 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2600 else
2602 vec<tree, va_gc> *methods;
2603 tree ovl;
2605 /* For a type-conversion operator, we cannot do a
2606 name-based lookup. We might be looking for `operator
2607 int' which will be a specialization of `operator T'.
2608 So, we find *all* the conversion operators, and then
2609 select from them. */
2610 fns = NULL_TREE;
2612 methods = CLASSTYPE_METHOD_VEC (ctype);
2613 if (methods)
2614 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2615 methods->iterate (idx, &ovl);
2616 ++idx)
2618 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2619 /* There are no more conversion functions. */
2620 break;
2622 /* Glue all these conversion functions together
2623 with those we already have. */
2624 for (; ovl; ovl = OVL_NEXT (ovl))
2625 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2629 if (fns == NULL_TREE)
2631 error ("no member function %qD declared in %qT", name, ctype);
2632 return error_mark_node;
2634 else
2635 TREE_OPERAND (declarator, 0) = fns;
2638 /* Figure out what exactly is being specialized at this point.
2639 Note that for an explicit instantiation, even one for a
2640 member function, we cannot tell apriori whether the
2641 instantiation is for a member template, or just a member
2642 function of a template class. Even if a member template is
2643 being instantiated, the member template arguments may be
2644 elided if they can be deduced from the rest of the
2645 declaration. */
2646 tmpl = determine_specialization (declarator, decl,
2647 &targs,
2648 member_specialization,
2649 template_count,
2650 tsk);
2652 if (!tmpl || tmpl == error_mark_node)
2653 /* We couldn't figure out what this declaration was
2654 specializing. */
2655 return error_mark_node;
2656 else
2658 tree gen_tmpl = most_general_template (tmpl);
2660 if (explicit_instantiation)
2662 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2663 is done by do_decl_instantiation later. */
2665 int arg_depth = TMPL_ARGS_DEPTH (targs);
2666 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2668 if (arg_depth > parm_depth)
2670 /* If TMPL is not the most general template (for
2671 example, if TMPL is a friend template that is
2672 injected into namespace scope), then there will
2673 be too many levels of TARGS. Remove some of them
2674 here. */
2675 int i;
2676 tree new_targs;
2678 new_targs = make_tree_vec (parm_depth);
2679 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2680 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2681 = TREE_VEC_ELT (targs, i);
2682 targs = new_targs;
2685 return instantiate_template (tmpl, targs, tf_error);
2688 /* If we thought that the DECL was a member function, but it
2689 turns out to be specializing a static member function,
2690 make DECL a static member function as well. */
2691 if (DECL_STATIC_FUNCTION_P (tmpl)
2692 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2693 revert_static_member_fn (decl);
2695 /* If this is a specialization of a member template of a
2696 template class, we want to return the TEMPLATE_DECL, not
2697 the specialization of it. */
2698 if (tsk == tsk_template)
2700 tree result = DECL_TEMPLATE_RESULT (tmpl);
2701 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2702 DECL_INITIAL (result) = NULL_TREE;
2703 if (have_def)
2705 tree parm;
2706 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2707 DECL_SOURCE_LOCATION (result)
2708 = DECL_SOURCE_LOCATION (decl);
2709 /* We want to use the argument list specified in the
2710 definition, not in the original declaration. */
2711 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2712 for (parm = DECL_ARGUMENTS (result); parm;
2713 parm = DECL_CHAIN (parm))
2714 DECL_CONTEXT (parm) = result;
2716 return register_specialization (tmpl, gen_tmpl, targs,
2717 is_friend, 0);
2720 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2721 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2723 /* Inherit default function arguments from the template
2724 DECL is specializing. */
2725 copy_default_args_to_explicit_spec (decl);
2727 /* This specialization has the same protection as the
2728 template it specializes. */
2729 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2730 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2732 /* 7.1.1-1 [dcl.stc]
2734 A storage-class-specifier shall not be specified in an
2735 explicit specialization...
2737 The parser rejects these, so unless action is taken here,
2738 explicit function specializations will always appear with
2739 global linkage.
2741 The action recommended by the C++ CWG in response to C++
2742 defect report 605 is to make the storage class and linkage
2743 of the explicit specialization match the templated function:
2745 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2747 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2749 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2750 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2752 /* This specialization has the same linkage and visibility as
2753 the function template it specializes. */
2754 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2755 if (! TREE_PUBLIC (decl))
2757 DECL_INTERFACE_KNOWN (decl) = 1;
2758 DECL_NOT_REALLY_EXTERN (decl) = 1;
2760 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2761 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2763 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2764 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2768 /* If DECL is a friend declaration, declared using an
2769 unqualified name, the namespace associated with DECL may
2770 have been set incorrectly. For example, in:
2772 template <typename T> void f(T);
2773 namespace N {
2774 struct S { friend void f<int>(int); }
2777 we will have set the DECL_CONTEXT for the friend
2778 declaration to N, rather than to the global namespace. */
2779 if (DECL_NAMESPACE_SCOPE_P (decl))
2780 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2782 if (is_friend && !have_def)
2783 /* This is not really a declaration of a specialization.
2784 It's just the name of an instantiation. But, it's not
2785 a request for an instantiation, either. */
2786 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2788 /* Register this specialization so that we can find it
2789 again. */
2790 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2792 /* A 'structor should already have clones. */
2793 gcc_assert (decl == error_mark_node
2794 || !(DECL_CONSTRUCTOR_P (decl)
2795 || DECL_DESTRUCTOR_P (decl))
2796 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2800 return decl;
2803 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2804 parameters. These are represented in the same format used for
2805 DECL_TEMPLATE_PARMS. */
2808 comp_template_parms (const_tree parms1, const_tree parms2)
2810 const_tree p1;
2811 const_tree p2;
2813 if (parms1 == parms2)
2814 return 1;
2816 for (p1 = parms1, p2 = parms2;
2817 p1 != NULL_TREE && p2 != NULL_TREE;
2818 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2820 tree t1 = TREE_VALUE (p1);
2821 tree t2 = TREE_VALUE (p2);
2822 int i;
2824 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2825 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2827 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2828 return 0;
2830 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2832 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2833 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2835 /* If either of the template parameters are invalid, assume
2836 they match for the sake of error recovery. */
2837 if (error_operand_p (parm1) || error_operand_p (parm2))
2838 return 1;
2840 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2841 return 0;
2843 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2844 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2845 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2846 continue;
2847 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2848 return 0;
2852 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2853 /* One set of parameters has more parameters lists than the
2854 other. */
2855 return 0;
2857 return 1;
2860 /* Determine whether PARM is a parameter pack. */
2862 bool
2863 template_parameter_pack_p (const_tree parm)
2865 /* Determine if we have a non-type template parameter pack. */
2866 if (TREE_CODE (parm) == PARM_DECL)
2867 return (DECL_TEMPLATE_PARM_P (parm)
2868 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2869 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2870 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2872 /* If this is a list of template parameters, we could get a
2873 TYPE_DECL or a TEMPLATE_DECL. */
2874 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2875 parm = TREE_TYPE (parm);
2877 /* Otherwise it must be a type template parameter. */
2878 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2879 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2880 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2883 /* Determine if T is a function parameter pack. */
2885 bool
2886 function_parameter_pack_p (const_tree t)
2888 if (t && TREE_CODE (t) == PARM_DECL)
2889 return DECL_PACK_P (t);
2890 return false;
2893 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2894 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2896 tree
2897 get_function_template_decl (const_tree primary_func_tmpl_inst)
2899 if (! primary_func_tmpl_inst
2900 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2901 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2902 return NULL;
2904 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2907 /* Return true iff the function parameter PARAM_DECL was expanded
2908 from the function parameter pack PACK. */
2910 bool
2911 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2913 if (DECL_ARTIFICIAL (param_decl)
2914 || !function_parameter_pack_p (pack))
2915 return false;
2917 /* The parameter pack and its pack arguments have the same
2918 DECL_PARM_INDEX. */
2919 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2922 /* Determine whether ARGS describes a variadic template args list,
2923 i.e., one that is terminated by a template argument pack. */
2925 static bool
2926 template_args_variadic_p (tree args)
2928 int nargs;
2929 tree last_parm;
2931 if (args == NULL_TREE)
2932 return false;
2934 args = INNERMOST_TEMPLATE_ARGS (args);
2935 nargs = TREE_VEC_LENGTH (args);
2937 if (nargs == 0)
2938 return false;
2940 last_parm = TREE_VEC_ELT (args, nargs - 1);
2942 return ARGUMENT_PACK_P (last_parm);
2945 /* Generate a new name for the parameter pack name NAME (an
2946 IDENTIFIER_NODE) that incorporates its */
2948 static tree
2949 make_ith_pack_parameter_name (tree name, int i)
2951 /* Munge the name to include the parameter index. */
2952 #define NUMBUF_LEN 128
2953 char numbuf[NUMBUF_LEN];
2954 char* newname;
2955 int newname_len;
2957 if (name == NULL_TREE)
2958 return name;
2959 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2960 newname_len = IDENTIFIER_LENGTH (name)
2961 + strlen (numbuf) + 2;
2962 newname = (char*)alloca (newname_len);
2963 snprintf (newname, newname_len,
2964 "%s#%i", IDENTIFIER_POINTER (name), i);
2965 return get_identifier (newname);
2968 /* Return true if T is a primary function, class or alias template
2969 instantiation. */
2971 bool
2972 primary_template_instantiation_p (const_tree t)
2974 if (!t)
2975 return false;
2977 if (TREE_CODE (t) == FUNCTION_DECL)
2978 return DECL_LANG_SPECIFIC (t)
2979 && DECL_TEMPLATE_INSTANTIATION (t)
2980 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2981 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2982 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2983 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2984 else if (alias_template_specialization_p (t))
2985 return true;
2986 return false;
2989 /* Return true if PARM is a template template parameter. */
2991 bool
2992 template_template_parameter_p (const_tree parm)
2994 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2997 /* Return true iff PARM is a DECL representing a type template
2998 parameter. */
3000 bool
3001 template_type_parameter_p (const_tree parm)
3003 return (parm
3004 && (TREE_CODE (parm) == TYPE_DECL
3005 || TREE_CODE (parm) == TEMPLATE_DECL)
3006 && DECL_TEMPLATE_PARM_P (parm));
3009 /* Return the template parameters of T if T is a
3010 primary template instantiation, NULL otherwise. */
3012 tree
3013 get_primary_template_innermost_parameters (const_tree t)
3015 tree parms = NULL, template_info = NULL;
3017 if ((template_info = get_template_info (t))
3018 && primary_template_instantiation_p (t))
3019 parms = INNERMOST_TEMPLATE_PARMS
3020 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3022 return parms;
3025 /* Return the template parameters of the LEVELth level from the full list
3026 of template parameters PARMS. */
3028 tree
3029 get_template_parms_at_level (tree parms, int level)
3031 tree p;
3032 if (!parms
3033 || TREE_CODE (parms) != TREE_LIST
3034 || level > TMPL_PARMS_DEPTH (parms))
3035 return NULL_TREE;
3037 for (p = parms; p; p = TREE_CHAIN (p))
3038 if (TMPL_PARMS_DEPTH (p) == level)
3039 return p;
3041 return NULL_TREE;
3044 /* Returns the template arguments of T if T is a template instantiation,
3045 NULL otherwise. */
3047 tree
3048 get_template_innermost_arguments (const_tree t)
3050 tree args = NULL, template_info = NULL;
3052 if ((template_info = get_template_info (t))
3053 && TI_ARGS (template_info))
3054 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3056 return args;
3059 /* Return the argument pack elements of T if T is a template argument pack,
3060 NULL otherwise. */
3062 tree
3063 get_template_argument_pack_elems (const_tree t)
3065 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3066 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3067 return NULL;
3069 return ARGUMENT_PACK_ARGS (t);
3072 /* Structure used to track the progress of find_parameter_packs_r. */
3073 struct find_parameter_pack_data
3075 /* TREE_LIST that will contain all of the parameter packs found by
3076 the traversal. */
3077 tree* parameter_packs;
3079 /* Set of AST nodes that have been visited by the traversal. */
3080 struct pointer_set_t *visited;
3083 /* Identifies all of the argument packs that occur in a template
3084 argument and appends them to the TREE_LIST inside DATA, which is a
3085 find_parameter_pack_data structure. This is a subroutine of
3086 make_pack_expansion and uses_parameter_packs. */
3087 static tree
3088 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3090 tree t = *tp;
3091 struct find_parameter_pack_data* ppd =
3092 (struct find_parameter_pack_data*)data;
3093 bool parameter_pack_p = false;
3095 /* Handle type aliases/typedefs. */
3096 if (TYPE_ALIAS_P (t))
3098 if (TYPE_TEMPLATE_INFO (t))
3099 cp_walk_tree (&TYPE_TI_ARGS (t),
3100 &find_parameter_packs_r,
3101 ppd, ppd->visited);
3102 *walk_subtrees = 0;
3103 return NULL_TREE;
3106 /* Identify whether this is a parameter pack or not. */
3107 switch (TREE_CODE (t))
3109 case TEMPLATE_PARM_INDEX:
3110 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3111 parameter_pack_p = true;
3112 break;
3114 case TEMPLATE_TYPE_PARM:
3115 t = TYPE_MAIN_VARIANT (t);
3116 case TEMPLATE_TEMPLATE_PARM:
3117 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3118 parameter_pack_p = true;
3119 break;
3121 case FIELD_DECL:
3122 case PARM_DECL:
3123 if (DECL_PACK_P (t))
3125 /* We don't want to walk into the type of a PARM_DECL,
3126 because we don't want to see the type parameter pack. */
3127 *walk_subtrees = 0;
3128 parameter_pack_p = true;
3130 break;
3132 /* Look through a lambda capture proxy to the field pack. */
3133 case VAR_DECL:
3134 if (DECL_HAS_VALUE_EXPR_P (t))
3136 tree v = DECL_VALUE_EXPR (t);
3137 cp_walk_tree (&v,
3138 &find_parameter_packs_r,
3139 ppd, ppd->visited);
3140 *walk_subtrees = 0;
3142 break;
3144 case BASES:
3145 parameter_pack_p = true;
3146 break;
3147 default:
3148 /* Not a parameter pack. */
3149 break;
3152 if (parameter_pack_p)
3154 /* Add this parameter pack to the list. */
3155 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3158 if (TYPE_P (t))
3159 cp_walk_tree (&TYPE_CONTEXT (t),
3160 &find_parameter_packs_r, ppd, ppd->visited);
3162 /* This switch statement will return immediately if we don't find a
3163 parameter pack. */
3164 switch (TREE_CODE (t))
3166 case TEMPLATE_PARM_INDEX:
3167 return NULL_TREE;
3169 case BOUND_TEMPLATE_TEMPLATE_PARM:
3170 /* Check the template itself. */
3171 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3172 &find_parameter_packs_r, ppd, ppd->visited);
3173 /* Check the template arguments. */
3174 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3175 ppd->visited);
3176 *walk_subtrees = 0;
3177 return NULL_TREE;
3179 case TEMPLATE_TYPE_PARM:
3180 case TEMPLATE_TEMPLATE_PARM:
3181 return NULL_TREE;
3183 case PARM_DECL:
3184 return NULL_TREE;
3186 case RECORD_TYPE:
3187 if (TYPE_PTRMEMFUNC_P (t))
3188 return NULL_TREE;
3189 /* Fall through. */
3191 case UNION_TYPE:
3192 case ENUMERAL_TYPE:
3193 if (TYPE_TEMPLATE_INFO (t))
3194 cp_walk_tree (&TYPE_TI_ARGS (t),
3195 &find_parameter_packs_r, ppd, ppd->visited);
3197 *walk_subtrees = 0;
3198 return NULL_TREE;
3200 case CONSTRUCTOR:
3201 case TEMPLATE_DECL:
3202 cp_walk_tree (&TREE_TYPE (t),
3203 &find_parameter_packs_r, ppd, ppd->visited);
3204 return NULL_TREE;
3206 case TYPENAME_TYPE:
3207 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3208 ppd, ppd->visited);
3209 *walk_subtrees = 0;
3210 return NULL_TREE;
3212 case TYPE_PACK_EXPANSION:
3213 case EXPR_PACK_EXPANSION:
3214 *walk_subtrees = 0;
3215 return NULL_TREE;
3217 case INTEGER_TYPE:
3218 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3219 ppd, ppd->visited);
3220 *walk_subtrees = 0;
3221 return NULL_TREE;
3223 case IDENTIFIER_NODE:
3224 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3225 ppd->visited);
3226 *walk_subtrees = 0;
3227 return NULL_TREE;
3229 default:
3230 return NULL_TREE;
3233 return NULL_TREE;
3236 /* Determines if the expression or type T uses any parameter packs. */
3237 bool
3238 uses_parameter_packs (tree t)
3240 tree parameter_packs = NULL_TREE;
3241 struct find_parameter_pack_data ppd;
3242 ppd.parameter_packs = &parameter_packs;
3243 ppd.visited = pointer_set_create ();
3244 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3245 pointer_set_destroy (ppd.visited);
3246 return parameter_packs != NULL_TREE;
3249 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3250 representation a base-class initializer into a parameter pack
3251 expansion. If all goes well, the resulting node will be an
3252 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3253 respectively. */
3254 tree
3255 make_pack_expansion (tree arg)
3257 tree result;
3258 tree parameter_packs = NULL_TREE;
3259 bool for_types = false;
3260 struct find_parameter_pack_data ppd;
3262 if (!arg || arg == error_mark_node)
3263 return arg;
3265 if (TREE_CODE (arg) == TREE_LIST)
3267 /* The only time we will see a TREE_LIST here is for a base
3268 class initializer. In this case, the TREE_PURPOSE will be a
3269 _TYPE node (representing the base class expansion we're
3270 initializing) and the TREE_VALUE will be a TREE_LIST
3271 containing the initialization arguments.
3273 The resulting expansion looks somewhat different from most
3274 expansions. Rather than returning just one _EXPANSION, we
3275 return a TREE_LIST whose TREE_PURPOSE is a
3276 TYPE_PACK_EXPANSION containing the bases that will be
3277 initialized. The TREE_VALUE will be identical to the
3278 original TREE_VALUE, which is a list of arguments that will
3279 be passed to each base. We do not introduce any new pack
3280 expansion nodes into the TREE_VALUE (although it is possible
3281 that some already exist), because the TREE_PURPOSE and
3282 TREE_VALUE all need to be expanded together with the same
3283 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3284 resulting TREE_PURPOSE will mention the parameter packs in
3285 both the bases and the arguments to the bases. */
3286 tree purpose;
3287 tree value;
3288 tree parameter_packs = NULL_TREE;
3290 /* Determine which parameter packs will be used by the base
3291 class expansion. */
3292 ppd.visited = pointer_set_create ();
3293 ppd.parameter_packs = &parameter_packs;
3294 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3295 &ppd, ppd.visited);
3297 if (parameter_packs == NULL_TREE)
3299 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3300 pointer_set_destroy (ppd.visited);
3301 return error_mark_node;
3304 if (TREE_VALUE (arg) != void_type_node)
3306 /* Collect the sets of parameter packs used in each of the
3307 initialization arguments. */
3308 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3310 /* Determine which parameter packs will be expanded in this
3311 argument. */
3312 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3313 &ppd, ppd.visited);
3317 pointer_set_destroy (ppd.visited);
3319 /* Create the pack expansion type for the base type. */
3320 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3321 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3322 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3324 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3325 they will rarely be compared to anything. */
3326 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3328 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3331 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3332 for_types = true;
3334 /* Build the PACK_EXPANSION_* node. */
3335 result = for_types
3336 ? cxx_make_type (TYPE_PACK_EXPANSION)
3337 : make_node (EXPR_PACK_EXPANSION);
3338 SET_PACK_EXPANSION_PATTERN (result, arg);
3339 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3341 /* Propagate type and const-expression information. */
3342 TREE_TYPE (result) = TREE_TYPE (arg);
3343 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3345 else
3346 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3347 they will rarely be compared to anything. */
3348 SET_TYPE_STRUCTURAL_EQUALITY (result);
3350 /* Determine which parameter packs will be expanded. */
3351 ppd.parameter_packs = &parameter_packs;
3352 ppd.visited = pointer_set_create ();
3353 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3354 pointer_set_destroy (ppd.visited);
3356 /* Make sure we found some parameter packs. */
3357 if (parameter_packs == NULL_TREE)
3359 if (TYPE_P (arg))
3360 error ("expansion pattern %<%T%> contains no argument packs", arg);
3361 else
3362 error ("expansion pattern %<%E%> contains no argument packs", arg);
3363 return error_mark_node;
3365 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3367 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3369 return result;
3372 /* Checks T for any "bare" parameter packs, which have not yet been
3373 expanded, and issues an error if any are found. This operation can
3374 only be done on full expressions or types (e.g., an expression
3375 statement, "if" condition, etc.), because we could have expressions like:
3377 foo(f(g(h(args)))...)
3379 where "args" is a parameter pack. check_for_bare_parameter_packs
3380 should not be called for the subexpressions args, h(args),
3381 g(h(args)), or f(g(h(args))), because we would produce erroneous
3382 error messages.
3384 Returns TRUE and emits an error if there were bare parameter packs,
3385 returns FALSE otherwise. */
3386 bool
3387 check_for_bare_parameter_packs (tree t)
3389 tree parameter_packs = NULL_TREE;
3390 struct find_parameter_pack_data ppd;
3392 if (!processing_template_decl || !t || t == error_mark_node)
3393 return false;
3395 if (TREE_CODE (t) == TYPE_DECL)
3396 t = TREE_TYPE (t);
3398 ppd.parameter_packs = &parameter_packs;
3399 ppd.visited = pointer_set_create ();
3400 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3401 pointer_set_destroy (ppd.visited);
3403 if (parameter_packs)
3405 error ("parameter packs not expanded with %<...%>:");
3406 while (parameter_packs)
3408 tree pack = TREE_VALUE (parameter_packs);
3409 tree name = NULL_TREE;
3411 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3412 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3413 name = TYPE_NAME (pack);
3414 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3415 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3416 else
3417 name = DECL_NAME (pack);
3419 if (name)
3420 inform (input_location, " %qD", name);
3421 else
3422 inform (input_location, " <anonymous>");
3424 parameter_packs = TREE_CHAIN (parameter_packs);
3427 return true;
3430 return false;
3433 /* Expand any parameter packs that occur in the template arguments in
3434 ARGS. */
3435 tree
3436 expand_template_argument_pack (tree args)
3438 tree result_args = NULL_TREE;
3439 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3440 int num_result_args = -1;
3441 int non_default_args_count = -1;
3443 /* First, determine if we need to expand anything, and the number of
3444 slots we'll need. */
3445 for (in_arg = 0; in_arg < nargs; ++in_arg)
3447 tree arg = TREE_VEC_ELT (args, in_arg);
3448 if (arg == NULL_TREE)
3449 return args;
3450 if (ARGUMENT_PACK_P (arg))
3452 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3453 if (num_result_args < 0)
3454 num_result_args = in_arg + num_packed;
3455 else
3456 num_result_args += num_packed;
3458 else
3460 if (num_result_args >= 0)
3461 num_result_args++;
3465 /* If no expansion is necessary, we're done. */
3466 if (num_result_args < 0)
3467 return args;
3469 /* Expand arguments. */
3470 result_args = make_tree_vec (num_result_args);
3471 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3472 non_default_args_count =
3473 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3474 for (in_arg = 0; in_arg < nargs; ++in_arg)
3476 tree arg = TREE_VEC_ELT (args, in_arg);
3477 if (ARGUMENT_PACK_P (arg))
3479 tree packed = ARGUMENT_PACK_ARGS (arg);
3480 int i, num_packed = TREE_VEC_LENGTH (packed);
3481 for (i = 0; i < num_packed; ++i, ++out_arg)
3482 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3483 if (non_default_args_count > 0)
3484 non_default_args_count += num_packed - 1;
3486 else
3488 TREE_VEC_ELT (result_args, out_arg) = arg;
3489 ++out_arg;
3492 if (non_default_args_count >= 0)
3493 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3494 return result_args;
3497 /* Checks if DECL shadows a template parameter.
3499 [temp.local]: A template-parameter shall not be redeclared within its
3500 scope (including nested scopes).
3502 Emits an error and returns TRUE if the DECL shadows a parameter,
3503 returns FALSE otherwise. */
3505 bool
3506 check_template_shadow (tree decl)
3508 tree olddecl;
3510 /* If we're not in a template, we can't possibly shadow a template
3511 parameter. */
3512 if (!current_template_parms)
3513 return true;
3515 /* Figure out what we're shadowing. */
3516 if (TREE_CODE (decl) == OVERLOAD)
3517 decl = OVL_CURRENT (decl);
3518 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3520 /* If there's no previous binding for this name, we're not shadowing
3521 anything, let alone a template parameter. */
3522 if (!olddecl)
3523 return true;
3525 /* If we're not shadowing a template parameter, we're done. Note
3526 that OLDDECL might be an OVERLOAD (or perhaps even an
3527 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3528 node. */
3529 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3530 return true;
3532 /* We check for decl != olddecl to avoid bogus errors for using a
3533 name inside a class. We check TPFI to avoid duplicate errors for
3534 inline member templates. */
3535 if (decl == olddecl
3536 || (DECL_TEMPLATE_PARM_P (decl)
3537 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3538 return true;
3540 /* Don't complain about the injected class name, as we've already
3541 complained about the class itself. */
3542 if (DECL_SELF_REFERENCE_P (decl))
3543 return false;
3545 error ("declaration of %q+#D", decl);
3546 error (" shadows template parm %q+#D", olddecl);
3547 return false;
3550 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3551 ORIG_LEVEL, DECL, and TYPE. */
3553 static tree
3554 build_template_parm_index (int index,
3555 int level,
3556 int orig_level,
3557 tree decl,
3558 tree type)
3560 tree t = make_node (TEMPLATE_PARM_INDEX);
3561 TEMPLATE_PARM_IDX (t) = index;
3562 TEMPLATE_PARM_LEVEL (t) = level;
3563 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3564 TEMPLATE_PARM_DECL (t) = decl;
3565 TREE_TYPE (t) = type;
3566 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3567 TREE_READONLY (t) = TREE_READONLY (decl);
3569 return t;
3572 /* Find the canonical type parameter for the given template type
3573 parameter. Returns the canonical type parameter, which may be TYPE
3574 if no such parameter existed. */
3576 static tree
3577 canonical_type_parameter (tree type)
3579 tree list;
3580 int idx = TEMPLATE_TYPE_IDX (type);
3581 if (!canonical_template_parms)
3582 vec_alloc (canonical_template_parms, idx+1);
3584 while (canonical_template_parms->length () <= (unsigned)idx)
3585 vec_safe_push (canonical_template_parms, NULL_TREE);
3587 list = (*canonical_template_parms)[idx];
3588 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3589 list = TREE_CHAIN (list);
3591 if (list)
3592 return TREE_VALUE (list);
3593 else
3595 (*canonical_template_parms)[idx]
3596 = tree_cons (NULL_TREE, type,
3597 (*canonical_template_parms)[idx]);
3598 return type;
3602 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3603 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3604 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3605 new one is created. */
3607 static tree
3608 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3609 tsubst_flags_t complain)
3611 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3612 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3613 != TEMPLATE_PARM_LEVEL (index) - levels)
3614 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3616 tree orig_decl = TEMPLATE_PARM_DECL (index);
3617 tree decl, t;
3619 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3620 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3621 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3622 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3623 DECL_ARTIFICIAL (decl) = 1;
3624 SET_DECL_TEMPLATE_PARM_P (decl);
3626 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3627 TEMPLATE_PARM_LEVEL (index) - levels,
3628 TEMPLATE_PARM_ORIG_LEVEL (index),
3629 decl, type);
3630 TEMPLATE_PARM_DESCENDANTS (index) = t;
3631 TEMPLATE_PARM_PARAMETER_PACK (t)
3632 = TEMPLATE_PARM_PARAMETER_PACK (index);
3634 /* Template template parameters need this. */
3635 if (TREE_CODE (decl) == TEMPLATE_DECL)
3636 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3637 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3638 args, complain);
3641 return TEMPLATE_PARM_DESCENDANTS (index);
3644 /* Process information from new template parameter PARM and append it
3645 to the LIST being built. This new parameter is a non-type
3646 parameter iff IS_NON_TYPE is true. This new parameter is a
3647 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3648 is in PARM_LOC. */
3650 tree
3651 process_template_parm (tree list, location_t parm_loc, tree parm,
3652 bool is_non_type, bool is_parameter_pack)
3654 tree decl = 0;
3655 tree defval;
3656 int idx = 0;
3658 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3659 defval = TREE_PURPOSE (parm);
3661 if (list)
3663 tree p = tree_last (list);
3665 if (p && TREE_VALUE (p) != error_mark_node)
3667 p = TREE_VALUE (p);
3668 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3669 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3670 else
3671 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3674 ++idx;
3677 if (is_non_type)
3679 parm = TREE_VALUE (parm);
3681 SET_DECL_TEMPLATE_PARM_P (parm);
3683 if (TREE_TYPE (parm) != error_mark_node)
3685 /* [temp.param]
3687 The top-level cv-qualifiers on the template-parameter are
3688 ignored when determining its type. */
3689 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3690 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3691 TREE_TYPE (parm) = error_mark_node;
3692 else if (uses_parameter_packs (TREE_TYPE (parm))
3693 && !is_parameter_pack
3694 /* If we're in a nested template parameter list, the template
3695 template parameter could be a parameter pack. */
3696 && processing_template_parmlist == 1)
3698 /* This template parameter is not a parameter pack, but it
3699 should be. Complain about "bare" parameter packs. */
3700 check_for_bare_parameter_packs (TREE_TYPE (parm));
3702 /* Recover by calling this a parameter pack. */
3703 is_parameter_pack = true;
3707 /* A template parameter is not modifiable. */
3708 TREE_CONSTANT (parm) = 1;
3709 TREE_READONLY (parm) = 1;
3710 decl = build_decl (parm_loc,
3711 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3712 TREE_CONSTANT (decl) = 1;
3713 TREE_READONLY (decl) = 1;
3714 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3715 = build_template_parm_index (idx, processing_template_decl,
3716 processing_template_decl,
3717 decl, TREE_TYPE (parm));
3719 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3720 = is_parameter_pack;
3722 else
3724 tree t;
3725 parm = TREE_VALUE (TREE_VALUE (parm));
3727 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3729 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3730 /* This is for distinguishing between real templates and template
3731 template parameters */
3732 TREE_TYPE (parm) = t;
3733 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3734 decl = parm;
3736 else
3738 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3739 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3740 decl = build_decl (parm_loc,
3741 TYPE_DECL, parm, t);
3744 TYPE_NAME (t) = decl;
3745 TYPE_STUB_DECL (t) = decl;
3746 parm = decl;
3747 TEMPLATE_TYPE_PARM_INDEX (t)
3748 = build_template_parm_index (idx, processing_template_decl,
3749 processing_template_decl,
3750 decl, TREE_TYPE (parm));
3751 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3752 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3754 DECL_ARTIFICIAL (decl) = 1;
3755 SET_DECL_TEMPLATE_PARM_P (decl);
3756 pushdecl (decl);
3757 parm = build_tree_list (defval, parm);
3758 return chainon (list, parm);
3761 /* The end of a template parameter list has been reached. Process the
3762 tree list into a parameter vector, converting each parameter into a more
3763 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3764 as PARM_DECLs. */
3766 tree
3767 end_template_parm_list (tree parms)
3769 int nparms;
3770 tree parm, next;
3771 tree saved_parmlist = make_tree_vec (list_length (parms));
3773 current_template_parms
3774 = tree_cons (size_int (processing_template_decl),
3775 saved_parmlist, current_template_parms);
3777 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3779 next = TREE_CHAIN (parm);
3780 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3781 TREE_CHAIN (parm) = NULL_TREE;
3784 --processing_template_parmlist;
3786 return saved_parmlist;
3789 /* end_template_decl is called after a template declaration is seen. */
3791 void
3792 end_template_decl (void)
3794 reset_specialization ();
3796 if (! processing_template_decl)
3797 return;
3799 /* This matches the pushlevel in begin_template_parm_list. */
3800 finish_scope ();
3802 --processing_template_decl;
3803 current_template_parms = TREE_CHAIN (current_template_parms);
3806 /* Takes a TREE_LIST representing a template parameter and convert it
3807 into an argument suitable to be passed to the type substitution
3808 functions. Note that If the TREE_LIST contains an error_mark
3809 node, the returned argument is error_mark_node. */
3811 static tree
3812 template_parm_to_arg (tree t)
3815 if (t == NULL_TREE
3816 || TREE_CODE (t) != TREE_LIST)
3817 return t;
3819 if (error_operand_p (TREE_VALUE (t)))
3820 return error_mark_node;
3822 t = TREE_VALUE (t);
3824 if (TREE_CODE (t) == TYPE_DECL
3825 || TREE_CODE (t) == TEMPLATE_DECL)
3827 t = TREE_TYPE (t);
3829 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3831 /* Turn this argument into a TYPE_ARGUMENT_PACK
3832 with a single element, which expands T. */
3833 tree vec = make_tree_vec (1);
3834 #ifdef ENABLE_CHECKING
3835 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3836 (vec, TREE_VEC_LENGTH (vec));
3837 #endif
3838 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3840 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3841 SET_ARGUMENT_PACK_ARGS (t, vec);
3844 else
3846 t = DECL_INITIAL (t);
3848 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3850 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3851 with a single element, which expands T. */
3852 tree vec = make_tree_vec (1);
3853 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3854 #ifdef ENABLE_CHECKING
3855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3856 (vec, TREE_VEC_LENGTH (vec));
3857 #endif
3858 t = convert_from_reference (t);
3859 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3861 t = make_node (NONTYPE_ARGUMENT_PACK);
3862 SET_ARGUMENT_PACK_ARGS (t, vec);
3863 TREE_TYPE (t) = type;
3865 else
3866 t = convert_from_reference (t);
3868 return t;
3871 /* Given a set of template parameters, return them as a set of template
3872 arguments. The template parameters are represented as a TREE_VEC, in
3873 the form documented in cp-tree.h for template arguments. */
3875 static tree
3876 template_parms_to_args (tree parms)
3878 tree header;
3879 tree args = NULL_TREE;
3880 int length = TMPL_PARMS_DEPTH (parms);
3881 int l = length;
3883 /* If there is only one level of template parameters, we do not
3884 create a TREE_VEC of TREE_VECs. Instead, we return a single
3885 TREE_VEC containing the arguments. */
3886 if (length > 1)
3887 args = make_tree_vec (length);
3889 for (header = parms; header; header = TREE_CHAIN (header))
3891 tree a = copy_node (TREE_VALUE (header));
3892 int i;
3894 TREE_TYPE (a) = NULL_TREE;
3895 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3896 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3898 #ifdef ENABLE_CHECKING
3899 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3900 #endif
3902 if (length > 1)
3903 TREE_VEC_ELT (args, --l) = a;
3904 else
3905 args = a;
3908 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3909 /* This can happen for template parms of a template template
3910 parameter, e.g:
3912 template<template<class T, class U> class TT> struct S;
3914 Consider the level of the parms of TT; T and U both have
3915 level 2; TT has no template parm of level 1. So in this case
3916 the first element of full_template_args is NULL_TREE. If we
3917 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3918 of 2. This will make tsubst wrongly consider that T and U
3919 have level 1. Instead, let's create a dummy vector as the
3920 first element of full_template_args so that TMPL_ARGS_DEPTH
3921 returns the correct depth for args. */
3922 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3923 return args;
3926 /* Within the declaration of a template, return the currently active
3927 template parameters as an argument TREE_VEC. */
3929 static tree
3930 current_template_args (void)
3932 return template_parms_to_args (current_template_parms);
3935 /* Update the declared TYPE by doing any lookups which were thought to be
3936 dependent, but are not now that we know the SCOPE of the declarator. */
3938 tree
3939 maybe_update_decl_type (tree orig_type, tree scope)
3941 tree type = orig_type;
3943 if (type == NULL_TREE)
3944 return type;
3946 if (TREE_CODE (orig_type) == TYPE_DECL)
3947 type = TREE_TYPE (type);
3949 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3950 && dependent_type_p (type)
3951 /* Don't bother building up the args in this case. */
3952 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3954 /* tsubst in the args corresponding to the template parameters,
3955 including auto if present. Most things will be unchanged, but
3956 make_typename_type and tsubst_qualified_id will resolve
3957 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3958 tree args = current_template_args ();
3959 tree auto_node = type_uses_auto (type);
3960 tree pushed;
3961 if (auto_node)
3963 tree auto_vec = make_tree_vec (1);
3964 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3965 args = add_to_template_args (args, auto_vec);
3967 pushed = push_scope (scope);
3968 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3969 if (pushed)
3970 pop_scope (scope);
3973 if (type == error_mark_node)
3974 return orig_type;
3976 if (TREE_CODE (orig_type) == TYPE_DECL)
3978 if (same_type_p (type, TREE_TYPE (orig_type)))
3979 type = orig_type;
3980 else
3981 type = TYPE_NAME (type);
3983 return type;
3986 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3987 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3988 a member template. Used by push_template_decl below. */
3990 static tree
3991 build_template_decl (tree decl, tree parms, bool member_template_p)
3993 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3994 DECL_TEMPLATE_PARMS (tmpl) = parms;
3995 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3996 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3997 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3999 return tmpl;
4002 struct template_parm_data
4004 /* The level of the template parameters we are currently
4005 processing. */
4006 int level;
4008 /* The index of the specialization argument we are currently
4009 processing. */
4010 int current_arg;
4012 /* An array whose size is the number of template parameters. The
4013 elements are nonzero if the parameter has been used in any one
4014 of the arguments processed so far. */
4015 int* parms;
4017 /* An array whose size is the number of template arguments. The
4018 elements are nonzero if the argument makes use of template
4019 parameters of this level. */
4020 int* arg_uses_template_parms;
4023 /* Subroutine of push_template_decl used to see if each template
4024 parameter in a partial specialization is used in the explicit
4025 argument list. If T is of the LEVEL given in DATA (which is
4026 treated as a template_parm_data*), then DATA->PARMS is marked
4027 appropriately. */
4029 static int
4030 mark_template_parm (tree t, void* data)
4032 int level;
4033 int idx;
4034 struct template_parm_data* tpd = (struct template_parm_data*) data;
4036 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4038 level = TEMPLATE_PARM_LEVEL (t);
4039 idx = TEMPLATE_PARM_IDX (t);
4041 else
4043 level = TEMPLATE_TYPE_LEVEL (t);
4044 idx = TEMPLATE_TYPE_IDX (t);
4047 if (level == tpd->level)
4049 tpd->parms[idx] = 1;
4050 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4053 /* Return zero so that for_each_template_parm will continue the
4054 traversal of the tree; we want to mark *every* template parm. */
4055 return 0;
4058 /* Process the partial specialization DECL. */
4060 static tree
4061 process_partial_specialization (tree decl)
4063 tree type = TREE_TYPE (decl);
4064 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4065 tree specargs = CLASSTYPE_TI_ARGS (type);
4066 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4067 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4068 tree inner_parms;
4069 tree inst;
4070 int nargs = TREE_VEC_LENGTH (inner_args);
4071 int ntparms;
4072 int i;
4073 bool did_error_intro = false;
4074 struct template_parm_data tpd;
4075 struct template_parm_data tpd2;
4077 gcc_assert (current_template_parms);
4079 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4080 ntparms = TREE_VEC_LENGTH (inner_parms);
4082 /* We check that each of the template parameters given in the
4083 partial specialization is used in the argument list to the
4084 specialization. For example:
4086 template <class T> struct S;
4087 template <class T> struct S<T*>;
4089 The second declaration is OK because `T*' uses the template
4090 parameter T, whereas
4092 template <class T> struct S<int>;
4094 is no good. Even trickier is:
4096 template <class T>
4097 struct S1
4099 template <class U>
4100 struct S2;
4101 template <class U>
4102 struct S2<T>;
4105 The S2<T> declaration is actually invalid; it is a
4106 full-specialization. Of course,
4108 template <class U>
4109 struct S2<T (*)(U)>;
4111 or some such would have been OK. */
4112 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4113 tpd.parms = XALLOCAVEC (int, ntparms);
4114 memset (tpd.parms, 0, sizeof (int) * ntparms);
4116 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4117 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4118 for (i = 0; i < nargs; ++i)
4120 tpd.current_arg = i;
4121 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4122 &mark_template_parm,
4123 &tpd,
4124 NULL,
4125 /*include_nondeduced_p=*/false);
4127 for (i = 0; i < ntparms; ++i)
4128 if (tpd.parms[i] == 0)
4130 /* One of the template parms was not used in a deduced context in the
4131 specialization. */
4132 if (!did_error_intro)
4134 error ("template parameters not deducible in "
4135 "partial specialization:");
4136 did_error_intro = true;
4139 inform (input_location, " %qD",
4140 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4143 if (did_error_intro)
4144 return error_mark_node;
4146 /* [temp.class.spec]
4148 The argument list of the specialization shall not be identical to
4149 the implicit argument list of the primary template. */
4150 if (comp_template_args
4151 (inner_args,
4152 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4153 (maintmpl)))))
4154 error ("partial specialization %qT does not specialize any template arguments", type);
4156 /* A partial specialization that replaces multiple parameters of the
4157 primary template with a pack expansion is less specialized for those
4158 parameters. */
4159 if (nargs < DECL_NTPARMS (maintmpl))
4161 error ("partial specialization is not more specialized than the "
4162 "primary template because it replaces multiple parameters "
4163 "with a pack expansion");
4164 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4165 return decl;
4168 /* [temp.class.spec]
4170 A partially specialized non-type argument expression shall not
4171 involve template parameters of the partial specialization except
4172 when the argument expression is a simple identifier.
4174 The type of a template parameter corresponding to a specialized
4175 non-type argument shall not be dependent on a parameter of the
4176 specialization.
4178 Also, we verify that pack expansions only occur at the
4179 end of the argument list. */
4180 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4181 tpd2.parms = 0;
4182 for (i = 0; i < nargs; ++i)
4184 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4185 tree arg = TREE_VEC_ELT (inner_args, i);
4186 tree packed_args = NULL_TREE;
4187 int j, len = 1;
4189 if (ARGUMENT_PACK_P (arg))
4191 /* Extract the arguments from the argument pack. We'll be
4192 iterating over these in the following loop. */
4193 packed_args = ARGUMENT_PACK_ARGS (arg);
4194 len = TREE_VEC_LENGTH (packed_args);
4197 for (j = 0; j < len; j++)
4199 if (packed_args)
4200 /* Get the Jth argument in the parameter pack. */
4201 arg = TREE_VEC_ELT (packed_args, j);
4203 if (PACK_EXPANSION_P (arg))
4205 /* Pack expansions must come at the end of the
4206 argument list. */
4207 if ((packed_args && j < len - 1)
4208 || (!packed_args && i < nargs - 1))
4210 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4211 error ("parameter pack argument %qE must be at the "
4212 "end of the template argument list", arg);
4213 else
4214 error ("parameter pack argument %qT must be at the "
4215 "end of the template argument list", arg);
4219 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4220 /* We only care about the pattern. */
4221 arg = PACK_EXPANSION_PATTERN (arg);
4223 if (/* These first two lines are the `non-type' bit. */
4224 !TYPE_P (arg)
4225 && TREE_CODE (arg) != TEMPLATE_DECL
4226 /* This next two lines are the `argument expression is not just a
4227 simple identifier' condition and also the `specialized
4228 non-type argument' bit. */
4229 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4230 && !(REFERENCE_REF_P (arg)
4231 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4233 if ((!packed_args && tpd.arg_uses_template_parms[i])
4234 || (packed_args && uses_template_parms (arg)))
4235 error ("template argument %qE involves template parameter(s)",
4236 arg);
4237 else
4239 /* Look at the corresponding template parameter,
4240 marking which template parameters its type depends
4241 upon. */
4242 tree type = TREE_TYPE (parm);
4244 if (!tpd2.parms)
4246 /* We haven't yet initialized TPD2. Do so now. */
4247 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4248 /* The number of parameters here is the number in the
4249 main template, which, as checked in the assertion
4250 above, is NARGS. */
4251 tpd2.parms = XALLOCAVEC (int, nargs);
4252 tpd2.level =
4253 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4256 /* Mark the template parameters. But this time, we're
4257 looking for the template parameters of the main
4258 template, not in the specialization. */
4259 tpd2.current_arg = i;
4260 tpd2.arg_uses_template_parms[i] = 0;
4261 memset (tpd2.parms, 0, sizeof (int) * nargs);
4262 for_each_template_parm (type,
4263 &mark_template_parm,
4264 &tpd2,
4265 NULL,
4266 /*include_nondeduced_p=*/false);
4268 if (tpd2.arg_uses_template_parms [i])
4270 /* The type depended on some template parameters.
4271 If they are fully specialized in the
4272 specialization, that's OK. */
4273 int j;
4274 int count = 0;
4275 for (j = 0; j < nargs; ++j)
4276 if (tpd2.parms[j] != 0
4277 && tpd.arg_uses_template_parms [j])
4278 ++count;
4279 if (count != 0)
4280 error_n (input_location, count,
4281 "type %qT of template argument %qE depends "
4282 "on a template parameter",
4283 "type %qT of template argument %qE depends "
4284 "on template parameters",
4285 type,
4286 arg);
4293 /* We should only get here once. */
4294 gcc_assert (!COMPLETE_TYPE_P (type));
4296 tree tmpl = build_template_decl (decl, current_template_parms,
4297 DECL_MEMBER_TEMPLATE_P (maintmpl));
4298 TREE_TYPE (tmpl) = type;
4299 DECL_TEMPLATE_RESULT (tmpl) = decl;
4300 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4301 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4302 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4304 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4305 = tree_cons (specargs, tmpl,
4306 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4307 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4309 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4310 inst = TREE_CHAIN (inst))
4312 tree inst_type = TREE_VALUE (inst);
4313 if (COMPLETE_TYPE_P (inst_type)
4314 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4316 tree spec = most_specialized_class (inst_type, tf_none);
4317 if (spec && TREE_TYPE (spec) == type)
4318 permerror (input_location,
4319 "partial specialization of %qT after instantiation "
4320 "of %qT", type, inst_type);
4324 return decl;
4327 /* PARM is a template parameter of some form; return the corresponding
4328 TEMPLATE_PARM_INDEX. */
4330 static tree
4331 get_template_parm_index (tree parm)
4333 if (TREE_CODE (parm) == PARM_DECL
4334 || TREE_CODE (parm) == CONST_DECL)
4335 parm = DECL_INITIAL (parm);
4336 else if (TREE_CODE (parm) == TYPE_DECL
4337 || TREE_CODE (parm) == TEMPLATE_DECL)
4338 parm = TREE_TYPE (parm);
4339 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4340 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4341 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4342 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4343 return parm;
4346 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4347 parameter packs used by the template parameter PARM. */
4349 static void
4350 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4352 /* A type parm can't refer to another parm. */
4353 if (TREE_CODE (parm) == TYPE_DECL)
4354 return;
4355 else if (TREE_CODE (parm) == PARM_DECL)
4357 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4358 ppd, ppd->visited);
4359 return;
4362 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4364 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4365 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4366 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4369 /* PARM is a template parameter pack. Return any parameter packs used in
4370 its type or the type of any of its template parameters. If there are
4371 any such packs, it will be instantiated into a fixed template parameter
4372 list by partial instantiation rather than be fully deduced. */
4374 tree
4375 fixed_parameter_pack_p (tree parm)
4377 /* This can only be true in a member template. */
4378 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4379 return NULL_TREE;
4380 /* This can only be true for a parameter pack. */
4381 if (!template_parameter_pack_p (parm))
4382 return NULL_TREE;
4383 /* A type parm can't refer to another parm. */
4384 if (TREE_CODE (parm) == TYPE_DECL)
4385 return NULL_TREE;
4387 tree parameter_packs = NULL_TREE;
4388 struct find_parameter_pack_data ppd;
4389 ppd.parameter_packs = &parameter_packs;
4390 ppd.visited = pointer_set_create ();
4392 fixed_parameter_pack_p_1 (parm, &ppd);
4394 pointer_set_destroy (ppd.visited);
4395 return parameter_packs;
4398 /* Check that a template declaration's use of default arguments and
4399 parameter packs is not invalid. Here, PARMS are the template
4400 parameters. IS_PRIMARY is true if DECL is the thing declared by
4401 a primary template. IS_PARTIAL is true if DECL is a partial
4402 specialization.
4404 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4405 declaration (but not a definition); 1 indicates a declaration, 2
4406 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4407 emitted for extraneous default arguments.
4409 Returns TRUE if there were no errors found, FALSE otherwise. */
4411 bool
4412 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4413 bool is_partial, int is_friend_decl)
4415 const char *msg;
4416 int last_level_to_check;
4417 tree parm_level;
4418 bool no_errors = true;
4420 /* [temp.param]
4422 A default template-argument shall not be specified in a
4423 function template declaration or a function template definition, nor
4424 in the template-parameter-list of the definition of a member of a
4425 class template. */
4427 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4428 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4429 /* You can't have a function template declaration in a local
4430 scope, nor you can you define a member of a class template in a
4431 local scope. */
4432 return true;
4434 if ((TREE_CODE (decl) == TYPE_DECL
4435 && TREE_TYPE (decl)
4436 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4437 || (TREE_CODE (decl) == FUNCTION_DECL
4438 && LAMBDA_FUNCTION_P (decl)))
4439 /* A lambda doesn't have an explicit declaration; don't complain
4440 about the parms of the enclosing class. */
4441 return true;
4443 if (current_class_type
4444 && !TYPE_BEING_DEFINED (current_class_type)
4445 && DECL_LANG_SPECIFIC (decl)
4446 && DECL_DECLARES_FUNCTION_P (decl)
4447 /* If this is either a friend defined in the scope of the class
4448 or a member function. */
4449 && (DECL_FUNCTION_MEMBER_P (decl)
4450 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4451 : DECL_FRIEND_CONTEXT (decl)
4452 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4453 : false)
4454 /* And, if it was a member function, it really was defined in
4455 the scope of the class. */
4456 && (!DECL_FUNCTION_MEMBER_P (decl)
4457 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4458 /* We already checked these parameters when the template was
4459 declared, so there's no need to do it again now. This function
4460 was defined in class scope, but we're processing its body now
4461 that the class is complete. */
4462 return true;
4464 /* Core issue 226 (C++0x only): the following only applies to class
4465 templates. */
4466 if (is_primary
4467 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4469 /* [temp.param]
4471 If a template-parameter has a default template-argument, all
4472 subsequent template-parameters shall have a default
4473 template-argument supplied. */
4474 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4476 tree inner_parms = TREE_VALUE (parm_level);
4477 int ntparms = TREE_VEC_LENGTH (inner_parms);
4478 int seen_def_arg_p = 0;
4479 int i;
4481 for (i = 0; i < ntparms; ++i)
4483 tree parm = TREE_VEC_ELT (inner_parms, i);
4485 if (parm == error_mark_node)
4486 continue;
4488 if (TREE_PURPOSE (parm))
4489 seen_def_arg_p = 1;
4490 else if (seen_def_arg_p
4491 && !template_parameter_pack_p (TREE_VALUE (parm)))
4493 error ("no default argument for %qD", TREE_VALUE (parm));
4494 /* For better subsequent error-recovery, we indicate that
4495 there should have been a default argument. */
4496 TREE_PURPOSE (parm) = error_mark_node;
4497 no_errors = false;
4499 else if (!is_partial
4500 && !is_friend_decl
4501 /* Don't complain about an enclosing partial
4502 specialization. */
4503 && parm_level == parms
4504 && TREE_CODE (decl) == TYPE_DECL
4505 && i < ntparms - 1
4506 && template_parameter_pack_p (TREE_VALUE (parm))
4507 /* A fixed parameter pack will be partially
4508 instantiated into a fixed length list. */
4509 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4511 /* A primary class template can only have one
4512 parameter pack, at the end of the template
4513 parameter list. */
4515 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4516 error ("parameter pack %qE must be at the end of the"
4517 " template parameter list", TREE_VALUE (parm));
4518 else
4519 error ("parameter pack %qT must be at the end of the"
4520 " template parameter list",
4521 TREE_TYPE (TREE_VALUE (parm)));
4523 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4524 = error_mark_node;
4525 no_errors = false;
4531 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4532 || is_partial
4533 || !is_primary
4534 || is_friend_decl)
4535 /* For an ordinary class template, default template arguments are
4536 allowed at the innermost level, e.g.:
4537 template <class T = int>
4538 struct S {};
4539 but, in a partial specialization, they're not allowed even
4540 there, as we have in [temp.class.spec]:
4542 The template parameter list of a specialization shall not
4543 contain default template argument values.
4545 So, for a partial specialization, or for a function template
4546 (in C++98/C++03), we look at all of them. */
4548 else
4549 /* But, for a primary class template that is not a partial
4550 specialization we look at all template parameters except the
4551 innermost ones. */
4552 parms = TREE_CHAIN (parms);
4554 /* Figure out what error message to issue. */
4555 if (is_friend_decl == 2)
4556 msg = G_("default template arguments may not be used in function template "
4557 "friend re-declaration");
4558 else if (is_friend_decl)
4559 msg = G_("default template arguments may not be used in function template "
4560 "friend declarations");
4561 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4562 msg = G_("default template arguments may not be used in function templates "
4563 "without -std=c++11 or -std=gnu++11");
4564 else if (is_partial)
4565 msg = G_("default template arguments may not be used in "
4566 "partial specializations");
4567 else
4568 msg = G_("default argument for template parameter for class enclosing %qD");
4570 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4571 /* If we're inside a class definition, there's no need to
4572 examine the parameters to the class itself. On the one
4573 hand, they will be checked when the class is defined, and,
4574 on the other, default arguments are valid in things like:
4575 template <class T = double>
4576 struct S { template <class U> void f(U); };
4577 Here the default argument for `S' has no bearing on the
4578 declaration of `f'. */
4579 last_level_to_check = template_class_depth (current_class_type) + 1;
4580 else
4581 /* Check everything. */
4582 last_level_to_check = 0;
4584 for (parm_level = parms;
4585 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4586 parm_level = TREE_CHAIN (parm_level))
4588 tree inner_parms = TREE_VALUE (parm_level);
4589 int i;
4590 int ntparms;
4592 ntparms = TREE_VEC_LENGTH (inner_parms);
4593 for (i = 0; i < ntparms; ++i)
4595 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4596 continue;
4598 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4600 if (msg)
4602 no_errors = false;
4603 if (is_friend_decl == 2)
4604 return no_errors;
4606 error (msg, decl);
4607 msg = 0;
4610 /* Clear out the default argument so that we are not
4611 confused later. */
4612 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4616 /* At this point, if we're still interested in issuing messages,
4617 they must apply to classes surrounding the object declared. */
4618 if (msg)
4619 msg = G_("default argument for template parameter for class "
4620 "enclosing %qD");
4623 return no_errors;
4626 /* Worker for push_template_decl_real, called via
4627 for_each_template_parm. DATA is really an int, indicating the
4628 level of the parameters we are interested in. If T is a template
4629 parameter of that level, return nonzero. */
4631 static int
4632 template_parm_this_level_p (tree t, void* data)
4634 int this_level = *(int *)data;
4635 int level;
4637 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4638 level = TEMPLATE_PARM_LEVEL (t);
4639 else
4640 level = TEMPLATE_TYPE_LEVEL (t);
4641 return level == this_level;
4644 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4645 parameters given by current_template_args, or reuses a
4646 previously existing one, if appropriate. Returns the DECL, or an
4647 equivalent one, if it is replaced via a call to duplicate_decls.
4649 If IS_FRIEND is true, DECL is a friend declaration. */
4651 tree
4652 push_template_decl_real (tree decl, bool is_friend)
4654 tree tmpl;
4655 tree args;
4656 tree info;
4657 tree ctx;
4658 bool is_primary;
4659 bool is_partial;
4660 int new_template_p = 0;
4661 /* True if the template is a member template, in the sense of
4662 [temp.mem]. */
4663 bool member_template_p = false;
4665 if (decl == error_mark_node || !current_template_parms)
4666 return error_mark_node;
4668 /* See if this is a partial specialization. */
4669 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4670 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4671 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4673 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4674 is_friend = true;
4676 if (is_friend)
4677 /* For a friend, we want the context of the friend function, not
4678 the type of which it is a friend. */
4679 ctx = CP_DECL_CONTEXT (decl);
4680 else if (CP_DECL_CONTEXT (decl)
4681 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4682 /* In the case of a virtual function, we want the class in which
4683 it is defined. */
4684 ctx = CP_DECL_CONTEXT (decl);
4685 else
4686 /* Otherwise, if we're currently defining some class, the DECL
4687 is assumed to be a member of the class. */
4688 ctx = current_scope ();
4690 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4691 ctx = NULL_TREE;
4693 if (!DECL_CONTEXT (decl))
4694 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4696 /* See if this is a primary template. */
4697 if (is_friend && ctx
4698 && uses_template_parms_level (ctx, processing_template_decl))
4699 /* A friend template that specifies a class context, i.e.
4700 template <typename T> friend void A<T>::f();
4701 is not primary. */
4702 is_primary = false;
4703 else if (TREE_CODE (decl) == TYPE_DECL
4704 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4705 is_primary = false;
4706 else
4707 is_primary = template_parm_scope_p ();
4709 if (is_primary)
4711 if (DECL_CLASS_SCOPE_P (decl))
4712 member_template_p = true;
4713 if (TREE_CODE (decl) == TYPE_DECL
4714 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4716 error ("template class without a name");
4717 return error_mark_node;
4719 else if (TREE_CODE (decl) == FUNCTION_DECL)
4721 if (DECL_DESTRUCTOR_P (decl))
4723 /* [temp.mem]
4725 A destructor shall not be a member template. */
4726 error ("destructor %qD declared as member template", decl);
4727 return error_mark_node;
4729 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4730 && (!prototype_p (TREE_TYPE (decl))
4731 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4732 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4733 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4734 == void_list_node)))
4736 /* [basic.stc.dynamic.allocation]
4738 An allocation function can be a function
4739 template. ... Template allocation functions shall
4740 have two or more parameters. */
4741 error ("invalid template declaration of %qD", decl);
4742 return error_mark_node;
4745 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4746 && CLASS_TYPE_P (TREE_TYPE (decl)))
4747 /* OK */;
4748 else if (TREE_CODE (decl) == TYPE_DECL
4749 && TYPE_DECL_ALIAS_P (decl))
4750 /* alias-declaration */
4751 gcc_assert (!DECL_ARTIFICIAL (decl));
4752 else
4754 error ("template declaration of %q#D", decl);
4755 return error_mark_node;
4759 /* Check to see that the rules regarding the use of default
4760 arguments are not being violated. */
4761 check_default_tmpl_args (decl, current_template_parms,
4762 is_primary, is_partial, /*is_friend_decl=*/0);
4764 /* Ensure that there are no parameter packs in the type of this
4765 declaration that have not been expanded. */
4766 if (TREE_CODE (decl) == FUNCTION_DECL)
4768 /* Check each of the arguments individually to see if there are
4769 any bare parameter packs. */
4770 tree type = TREE_TYPE (decl);
4771 tree arg = DECL_ARGUMENTS (decl);
4772 tree argtype = TYPE_ARG_TYPES (type);
4774 while (arg && argtype)
4776 if (!DECL_PACK_P (arg)
4777 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4779 /* This is a PARM_DECL that contains unexpanded parameter
4780 packs. We have already complained about this in the
4781 check_for_bare_parameter_packs call, so just replace
4782 these types with ERROR_MARK_NODE. */
4783 TREE_TYPE (arg) = error_mark_node;
4784 TREE_VALUE (argtype) = error_mark_node;
4787 arg = DECL_CHAIN (arg);
4788 argtype = TREE_CHAIN (argtype);
4791 /* Check for bare parameter packs in the return type and the
4792 exception specifiers. */
4793 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4794 /* Errors were already issued, set return type to int
4795 as the frontend doesn't expect error_mark_node as
4796 the return type. */
4797 TREE_TYPE (type) = integer_type_node;
4798 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4799 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4801 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4802 && TYPE_DECL_ALIAS_P (decl))
4803 ? DECL_ORIGINAL_TYPE (decl)
4804 : TREE_TYPE (decl)))
4806 TREE_TYPE (decl) = error_mark_node;
4807 return error_mark_node;
4810 if (is_partial)
4811 return process_partial_specialization (decl);
4813 args = current_template_args ();
4815 if (!ctx
4816 || TREE_CODE (ctx) == FUNCTION_DECL
4817 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4818 || (TREE_CODE (decl) == TYPE_DECL
4819 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4820 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4822 if (DECL_LANG_SPECIFIC (decl)
4823 && DECL_TEMPLATE_INFO (decl)
4824 && DECL_TI_TEMPLATE (decl))
4825 tmpl = DECL_TI_TEMPLATE (decl);
4826 /* If DECL is a TYPE_DECL for a class-template, then there won't
4827 be DECL_LANG_SPECIFIC. The information equivalent to
4828 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4829 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4830 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4831 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4833 /* Since a template declaration already existed for this
4834 class-type, we must be redeclaring it here. Make sure
4835 that the redeclaration is valid. */
4836 redeclare_class_template (TREE_TYPE (decl),
4837 current_template_parms);
4838 /* We don't need to create a new TEMPLATE_DECL; just use the
4839 one we already had. */
4840 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4842 else
4844 tmpl = build_template_decl (decl, current_template_parms,
4845 member_template_p);
4846 new_template_p = 1;
4848 if (DECL_LANG_SPECIFIC (decl)
4849 && DECL_TEMPLATE_SPECIALIZATION (decl))
4851 /* A specialization of a member template of a template
4852 class. */
4853 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4854 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4855 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4859 else
4861 tree a, t, current, parms;
4862 int i;
4863 tree tinfo = get_template_info (decl);
4865 if (!tinfo)
4867 error ("template definition of non-template %q#D", decl);
4868 return error_mark_node;
4871 tmpl = TI_TEMPLATE (tinfo);
4873 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4874 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4875 && DECL_TEMPLATE_SPECIALIZATION (decl)
4876 && DECL_MEMBER_TEMPLATE_P (tmpl))
4878 tree new_tmpl;
4880 /* The declaration is a specialization of a member
4881 template, declared outside the class. Therefore, the
4882 innermost template arguments will be NULL, so we
4883 replace them with the arguments determined by the
4884 earlier call to check_explicit_specialization. */
4885 args = DECL_TI_ARGS (decl);
4887 new_tmpl
4888 = build_template_decl (decl, current_template_parms,
4889 member_template_p);
4890 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4891 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4892 DECL_TI_TEMPLATE (decl) = new_tmpl;
4893 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4894 DECL_TEMPLATE_INFO (new_tmpl)
4895 = build_template_info (tmpl, args);
4897 register_specialization (new_tmpl,
4898 most_general_template (tmpl),
4899 args,
4900 is_friend, 0);
4901 return decl;
4904 /* Make sure the template headers we got make sense. */
4906 parms = DECL_TEMPLATE_PARMS (tmpl);
4907 i = TMPL_PARMS_DEPTH (parms);
4908 if (TMPL_ARGS_DEPTH (args) != i)
4910 error ("expected %d levels of template parms for %q#D, got %d",
4911 i, decl, TMPL_ARGS_DEPTH (args));
4912 DECL_INTERFACE_KNOWN (decl) = 1;
4913 return error_mark_node;
4915 else
4916 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4918 a = TMPL_ARGS_LEVEL (args, i);
4919 t = INNERMOST_TEMPLATE_PARMS (parms);
4921 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4923 if (current == decl)
4924 error ("got %d template parameters for %q#D",
4925 TREE_VEC_LENGTH (a), decl);
4926 else
4927 error ("got %d template parameters for %q#T",
4928 TREE_VEC_LENGTH (a), current);
4929 error (" but %d required", TREE_VEC_LENGTH (t));
4930 /* Avoid crash in import_export_decl. */
4931 DECL_INTERFACE_KNOWN (decl) = 1;
4932 return error_mark_node;
4935 if (current == decl)
4936 current = ctx;
4937 else if (current == NULL_TREE)
4938 /* Can happen in erroneous input. */
4939 break;
4940 else
4941 current = get_containing_scope (current);
4944 /* Check that the parms are used in the appropriate qualifying scopes
4945 in the declarator. */
4946 if (!comp_template_args
4947 (TI_ARGS (tinfo),
4948 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4950 error ("\
4951 template arguments to %qD do not match original template %qD",
4952 decl, DECL_TEMPLATE_RESULT (tmpl));
4953 if (!uses_template_parms (TI_ARGS (tinfo)))
4954 inform (input_location, "use template<> for an explicit specialization");
4955 /* Avoid crash in import_export_decl. */
4956 DECL_INTERFACE_KNOWN (decl) = 1;
4957 return error_mark_node;
4961 DECL_TEMPLATE_RESULT (tmpl) = decl;
4962 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4964 /* Push template declarations for global functions and types. Note
4965 that we do not try to push a global template friend declared in a
4966 template class; such a thing may well depend on the template
4967 parameters of the class. */
4968 if (new_template_p && !ctx
4969 && !(is_friend && template_class_depth (current_class_type) > 0))
4971 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4972 if (tmpl == error_mark_node)
4973 return error_mark_node;
4975 /* Hide template friend classes that haven't been declared yet. */
4976 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4978 DECL_ANTICIPATED (tmpl) = 1;
4979 DECL_FRIEND_P (tmpl) = 1;
4983 if (is_primary)
4985 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4986 int i;
4988 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4989 if (DECL_CONV_FN_P (tmpl))
4991 int depth = TMPL_PARMS_DEPTH (parms);
4993 /* It is a conversion operator. See if the type converted to
4994 depends on innermost template operands. */
4996 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4997 depth))
4998 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5001 /* Give template template parms a DECL_CONTEXT of the template
5002 for which they are a parameter. */
5003 parms = INNERMOST_TEMPLATE_PARMS (parms);
5004 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5006 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5007 if (TREE_CODE (parm) == TEMPLATE_DECL)
5008 DECL_CONTEXT (parm) = tmpl;
5012 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5013 back to its most general template. If TMPL is a specialization,
5014 ARGS may only have the innermost set of arguments. Add the missing
5015 argument levels if necessary. */
5016 if (DECL_TEMPLATE_INFO (tmpl))
5017 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5019 info = build_template_info (tmpl, args);
5021 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5022 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5023 else
5025 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5026 retrofit_lang_decl (decl);
5027 if (DECL_LANG_SPECIFIC (decl))
5028 DECL_TEMPLATE_INFO (decl) = info;
5031 return DECL_TEMPLATE_RESULT (tmpl);
5034 tree
5035 push_template_decl (tree decl)
5037 return push_template_decl_real (decl, false);
5040 /* FN is an inheriting constructor that inherits from the constructor
5041 template INHERITED; turn FN into a constructor template with a matching
5042 template header. */
5044 tree
5045 add_inherited_template_parms (tree fn, tree inherited)
5047 tree inner_parms
5048 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5049 inner_parms = copy_node (inner_parms);
5050 tree parms
5051 = tree_cons (size_int (processing_template_decl + 1),
5052 inner_parms, current_template_parms);
5053 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5054 tree args = template_parms_to_args (parms);
5055 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5056 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5057 DECL_TEMPLATE_RESULT (tmpl) = fn;
5058 DECL_ARTIFICIAL (tmpl) = true;
5059 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5060 return tmpl;
5063 /* Called when a class template TYPE is redeclared with the indicated
5064 template PARMS, e.g.:
5066 template <class T> struct S;
5067 template <class T> struct S {}; */
5069 bool
5070 redeclare_class_template (tree type, tree parms)
5072 tree tmpl;
5073 tree tmpl_parms;
5074 int i;
5076 if (!TYPE_TEMPLATE_INFO (type))
5078 error ("%qT is not a template type", type);
5079 return false;
5082 tmpl = TYPE_TI_TEMPLATE (type);
5083 if (!PRIMARY_TEMPLATE_P (tmpl))
5084 /* The type is nested in some template class. Nothing to worry
5085 about here; there are no new template parameters for the nested
5086 type. */
5087 return true;
5089 if (!parms)
5091 error ("template specifiers not specified in declaration of %qD",
5092 tmpl);
5093 return false;
5096 parms = INNERMOST_TEMPLATE_PARMS (parms);
5097 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5099 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5101 error_n (input_location, TREE_VEC_LENGTH (parms),
5102 "redeclared with %d template parameter",
5103 "redeclared with %d template parameters",
5104 TREE_VEC_LENGTH (parms));
5105 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5106 "previous declaration %q+D used %d template parameter",
5107 "previous declaration %q+D used %d template parameters",
5108 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5109 return false;
5112 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5114 tree tmpl_parm;
5115 tree parm;
5116 tree tmpl_default;
5117 tree parm_default;
5119 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5120 || TREE_VEC_ELT (parms, i) == error_mark_node)
5121 continue;
5123 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5124 if (error_operand_p (tmpl_parm))
5125 return false;
5127 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5128 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5129 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5131 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5132 TEMPLATE_DECL. */
5133 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5134 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5135 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5136 || (TREE_CODE (tmpl_parm) != PARM_DECL
5137 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5138 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5139 || (TREE_CODE (tmpl_parm) == PARM_DECL
5140 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5141 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5143 error ("template parameter %q+#D", tmpl_parm);
5144 error ("redeclared here as %q#D", parm);
5145 return false;
5148 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5150 /* We have in [temp.param]:
5152 A template-parameter may not be given default arguments
5153 by two different declarations in the same scope. */
5154 error_at (input_location, "redefinition of default argument for %q#D", parm);
5155 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5156 "original definition appeared here");
5157 return false;
5160 if (parm_default != NULL_TREE)
5161 /* Update the previous template parameters (which are the ones
5162 that will really count) with the new default value. */
5163 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5164 else if (tmpl_default != NULL_TREE)
5165 /* Update the new parameters, too; they'll be used as the
5166 parameters for any members. */
5167 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5170 return true;
5173 /* Simplify EXPR if it is a non-dependent expression. Returns the
5174 (possibly simplified) expression. */
5176 tree
5177 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5179 if (expr == NULL_TREE)
5180 return NULL_TREE;
5182 /* If we're in a template, but EXPR isn't value dependent, simplify
5183 it. We're supposed to treat:
5185 template <typename T> void f(T[1 + 1]);
5186 template <typename T> void f(T[2]);
5188 as two declarations of the same function, for example. */
5189 if (processing_template_decl
5190 && !instantiation_dependent_expression_p (expr)
5191 && potential_constant_expression (expr))
5193 HOST_WIDE_INT saved_processing_template_decl;
5195 saved_processing_template_decl = processing_template_decl;
5196 processing_template_decl = 0;
5197 expr = tsubst_copy_and_build (expr,
5198 /*args=*/NULL_TREE,
5199 complain,
5200 /*in_decl=*/NULL_TREE,
5201 /*function_p=*/false,
5202 /*integral_constant_expression_p=*/true);
5203 processing_template_decl = saved_processing_template_decl;
5205 return expr;
5208 tree
5209 fold_non_dependent_expr (tree expr)
5211 return fold_non_dependent_expr_sfinae (expr, tf_error);
5214 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5215 template declaration, or a TYPE_DECL for an alias declaration. */
5217 bool
5218 alias_type_or_template_p (tree t)
5220 if (t == NULL_TREE)
5221 return false;
5222 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5223 || (TYPE_P (t)
5224 && TYPE_NAME (t)
5225 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5226 || DECL_ALIAS_TEMPLATE_P (t));
5229 /* Return TRUE iff is a specialization of an alias template. */
5231 bool
5232 alias_template_specialization_p (const_tree t)
5234 if (t == NULL_TREE)
5235 return false;
5237 return (TYPE_P (t)
5238 && TYPE_TEMPLATE_INFO (t)
5239 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5240 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5243 /* Return the number of innermost template parameters in TMPL. */
5245 static int
5246 num_innermost_template_parms (tree tmpl)
5248 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5249 return TREE_VEC_LENGTH (parms);
5252 /* Return either TMPL or another template that it is equivalent to under DR
5253 1286: An alias that just changes the name of a template is equivalent to
5254 the other template. */
5256 static tree
5257 get_underlying_template (tree tmpl)
5259 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5260 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5262 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5263 if (TYPE_TEMPLATE_INFO (result))
5265 tree sub = TYPE_TI_TEMPLATE (result);
5266 if (PRIMARY_TEMPLATE_P (sub)
5267 && (num_innermost_template_parms (tmpl)
5268 == num_innermost_template_parms (sub)))
5270 tree alias_args = INNERMOST_TEMPLATE_ARGS
5271 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5272 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5273 break;
5274 /* The alias type is equivalent to the pattern of the
5275 underlying template, so strip the alias. */
5276 tmpl = sub;
5277 continue;
5280 break;
5282 return tmpl;
5285 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5286 must be a function or a pointer-to-function type, as specified
5287 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5288 and check that the resulting function has external linkage. */
5290 static tree
5291 convert_nontype_argument_function (tree type, tree expr)
5293 tree fns = expr;
5294 tree fn, fn_no_ptr;
5295 linkage_kind linkage;
5297 fn = instantiate_type (type, fns, tf_none);
5298 if (fn == error_mark_node)
5299 return error_mark_node;
5301 fn_no_ptr = fn;
5302 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5303 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5304 if (BASELINK_P (fn_no_ptr))
5305 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5307 /* [temp.arg.nontype]/1
5309 A template-argument for a non-type, non-template template-parameter
5310 shall be one of:
5311 [...]
5312 -- the address of an object or function with external [C++11: or
5313 internal] linkage. */
5315 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5317 error ("%qE is not a valid template argument for type %qT", expr, type);
5318 if (TYPE_PTR_P (type))
5319 error ("it must be the address of a function with external linkage");
5320 else
5321 error ("it must be the name of a function with external linkage");
5322 return NULL_TREE;
5325 linkage = decl_linkage (fn_no_ptr);
5326 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5328 if (cxx_dialect >= cxx11)
5329 error ("%qE is not a valid template argument for type %qT "
5330 "because %qD has no linkage",
5331 expr, type, fn_no_ptr);
5332 else
5333 error ("%qE is not a valid template argument for type %qT "
5334 "because %qD does not have external linkage",
5335 expr, type, fn_no_ptr);
5336 return NULL_TREE;
5339 return fn;
5342 /* Subroutine of convert_nontype_argument.
5343 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5344 Emit an error otherwise. */
5346 static bool
5347 check_valid_ptrmem_cst_expr (tree type, tree expr,
5348 tsubst_flags_t complain)
5350 STRIP_NOPS (expr);
5351 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5352 return true;
5353 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5354 return true;
5355 if (processing_template_decl
5356 && TREE_CODE (expr) == ADDR_EXPR
5357 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5358 return true;
5359 if (complain & tf_error)
5361 error ("%qE is not a valid template argument for type %qT",
5362 expr, type);
5363 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5365 return false;
5368 /* Returns TRUE iff the address of OP is value-dependent.
5370 14.6.2.4 [temp.dep.temp]:
5371 A non-integral non-type template-argument is dependent if its type is
5372 dependent or it has either of the following forms
5373 qualified-id
5374 & qualified-id
5375 and contains a nested-name-specifier which specifies a class-name that
5376 names a dependent type.
5378 We generalize this to just say that the address of a member of a
5379 dependent class is value-dependent; the above doesn't cover the
5380 address of a static data member named with an unqualified-id. */
5382 static bool
5383 has_value_dependent_address (tree op)
5385 /* We could use get_inner_reference here, but there's no need;
5386 this is only relevant for template non-type arguments, which
5387 can only be expressed as &id-expression. */
5388 if (DECL_P (op))
5390 tree ctx = CP_DECL_CONTEXT (op);
5391 if (TYPE_P (ctx) && dependent_type_p (ctx))
5392 return true;
5395 return false;
5398 /* The next set of functions are used for providing helpful explanatory
5399 diagnostics for failed overload resolution. Their messages should be
5400 indented by two spaces for consistency with the messages in
5401 call.c */
5403 static int
5404 unify_success (bool /*explain_p*/)
5406 return 0;
5409 static int
5410 unify_parameter_deduction_failure (bool explain_p, tree parm)
5412 if (explain_p)
5413 inform (input_location,
5414 " couldn't deduce template parameter %qD", parm);
5415 return 1;
5418 static int
5419 unify_invalid (bool /*explain_p*/)
5421 return 1;
5424 static int
5425 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5427 if (explain_p)
5428 inform (input_location,
5429 " types %qT and %qT have incompatible cv-qualifiers",
5430 parm, arg);
5431 return 1;
5434 static int
5435 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5437 if (explain_p)
5438 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5439 return 1;
5442 static int
5443 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5445 if (explain_p)
5446 inform (input_location,
5447 " template parameter %qD is not a parameter pack, but "
5448 "argument %qD is",
5449 parm, arg);
5450 return 1;
5453 static int
5454 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5456 if (explain_p)
5457 inform (input_location,
5458 " template argument %qE does not match "
5459 "pointer-to-member constant %qE",
5460 arg, parm);
5461 return 1;
5464 static int
5465 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5467 if (explain_p)
5468 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5469 return 1;
5472 static int
5473 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5475 if (explain_p)
5476 inform (input_location,
5477 " inconsistent parameter pack deduction with %qT and %qT",
5478 old_arg, new_arg);
5479 return 1;
5482 static int
5483 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5485 if (explain_p)
5487 if (TYPE_P (parm))
5488 inform (input_location,
5489 " deduced conflicting types for parameter %qT (%qT and %qT)",
5490 parm, first, second);
5491 else
5492 inform (input_location,
5493 " deduced conflicting values for non-type parameter "
5494 "%qE (%qE and %qE)", parm, first, second);
5496 return 1;
5499 static int
5500 unify_vla_arg (bool explain_p, tree arg)
5502 if (explain_p)
5503 inform (input_location,
5504 " variable-sized array type %qT is not "
5505 "a valid template argument",
5506 arg);
5507 return 1;
5510 static int
5511 unify_method_type_error (bool explain_p, tree arg)
5513 if (explain_p)
5514 inform (input_location,
5515 " member function type %qT is not a valid template argument",
5516 arg);
5517 return 1;
5520 static int
5521 unify_arity (bool explain_p, int have, int wanted)
5523 if (explain_p)
5524 inform_n (input_location, wanted,
5525 " candidate expects %d argument, %d provided",
5526 " candidate expects %d arguments, %d provided",
5527 wanted, have);
5528 return 1;
5531 static int
5532 unify_too_many_arguments (bool explain_p, int have, int wanted)
5534 return unify_arity (explain_p, have, wanted);
5537 static int
5538 unify_too_few_arguments (bool explain_p, int have, int wanted)
5540 return unify_arity (explain_p, have, wanted);
5543 static int
5544 unify_arg_conversion (bool explain_p, tree to_type,
5545 tree from_type, tree arg)
5547 if (explain_p)
5548 inform (EXPR_LOC_OR_LOC (arg, input_location),
5549 " cannot convert %qE (type %qT) to type %qT",
5550 arg, from_type, to_type);
5551 return 1;
5554 static int
5555 unify_no_common_base (bool explain_p, enum template_base_result r,
5556 tree parm, tree arg)
5558 if (explain_p)
5559 switch (r)
5561 case tbr_ambiguous_baseclass:
5562 inform (input_location, " %qT is an ambiguous base class of %qT",
5563 parm, arg);
5564 break;
5565 default:
5566 inform (input_location, " %qT is not derived from %qT", arg, parm);
5567 break;
5569 return 1;
5572 static int
5573 unify_inconsistent_template_template_parameters (bool explain_p)
5575 if (explain_p)
5576 inform (input_location,
5577 " template parameters of a template template argument are "
5578 "inconsistent with other deduced template arguments");
5579 return 1;
5582 static int
5583 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5585 if (explain_p)
5586 inform (input_location,
5587 " can't deduce a template for %qT from non-template type %qT",
5588 parm, arg);
5589 return 1;
5592 static int
5593 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5595 if (explain_p)
5596 inform (input_location,
5597 " template argument %qE does not match %qD", arg, parm);
5598 return 1;
5601 static int
5602 unify_overload_resolution_failure (bool explain_p, tree arg)
5604 if (explain_p)
5605 inform (input_location,
5606 " could not resolve address from overloaded function %qE",
5607 arg);
5608 return 1;
5611 /* Attempt to convert the non-type template parameter EXPR to the
5612 indicated TYPE. If the conversion is successful, return the
5613 converted value. If the conversion is unsuccessful, return
5614 NULL_TREE if we issued an error message, or error_mark_node if we
5615 did not. We issue error messages for out-and-out bad template
5616 parameters, but not simply because the conversion failed, since we
5617 might be just trying to do argument deduction. Both TYPE and EXPR
5618 must be non-dependent.
5620 The conversion follows the special rules described in
5621 [temp.arg.nontype], and it is much more strict than an implicit
5622 conversion.
5624 This function is called twice for each template argument (see
5625 lookup_template_class for a more accurate description of this
5626 problem). This means that we need to handle expressions which
5627 are not valid in a C++ source, but can be created from the
5628 first call (for instance, casts to perform conversions). These
5629 hacks can go away after we fix the double coercion problem. */
5631 static tree
5632 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5634 tree expr_type;
5636 /* Detect immediately string literals as invalid non-type argument.
5637 This special-case is not needed for correctness (we would easily
5638 catch this later), but only to provide better diagnostic for this
5639 common user mistake. As suggested by DR 100, we do not mention
5640 linkage issues in the diagnostic as this is not the point. */
5641 /* FIXME we're making this OK. */
5642 if (TREE_CODE (expr) == STRING_CST)
5644 if (complain & tf_error)
5645 error ("%qE is not a valid template argument for type %qT "
5646 "because string literals can never be used in this context",
5647 expr, type);
5648 return NULL_TREE;
5651 /* Add the ADDR_EXPR now for the benefit of
5652 value_dependent_expression_p. */
5653 if (TYPE_PTROBV_P (type)
5654 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5656 expr = decay_conversion (expr, complain);
5657 if (expr == error_mark_node)
5658 return error_mark_node;
5661 /* If we are in a template, EXPR may be non-dependent, but still
5662 have a syntactic, rather than semantic, form. For example, EXPR
5663 might be a SCOPE_REF, rather than the VAR_DECL to which the
5664 SCOPE_REF refers. Preserving the qualifying scope is necessary
5665 so that access checking can be performed when the template is
5666 instantiated -- but here we need the resolved form so that we can
5667 convert the argument. */
5668 if (TYPE_REF_OBJ_P (type)
5669 && has_value_dependent_address (expr))
5670 /* If we want the address and it's value-dependent, don't fold. */;
5671 else if (!type_unknown_p (expr))
5672 expr = fold_non_dependent_expr_sfinae (expr, complain);
5673 if (error_operand_p (expr))
5674 return error_mark_node;
5675 expr_type = TREE_TYPE (expr);
5676 if (TREE_CODE (type) == REFERENCE_TYPE)
5677 expr = mark_lvalue_use (expr);
5678 else
5679 expr = mark_rvalue_use (expr);
5681 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5682 to a non-type argument of "nullptr". */
5683 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5684 expr = convert (type, expr);
5686 /* In C++11, integral or enumeration non-type template arguments can be
5687 arbitrary constant expressions. Pointer and pointer to
5688 member arguments can be general constant expressions that evaluate
5689 to a null value, but otherwise still need to be of a specific form. */
5690 if (cxx_dialect >= cxx11)
5692 if (TREE_CODE (expr) == PTRMEM_CST)
5693 /* A PTRMEM_CST is already constant, and a valid template
5694 argument for a parameter of pointer to member type, we just want
5695 to leave it in that form rather than lower it to a
5696 CONSTRUCTOR. */;
5697 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5698 expr = maybe_constant_value (expr);
5699 else if (TYPE_PTR_OR_PTRMEM_P (type))
5701 tree folded = maybe_constant_value (expr);
5702 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5703 : null_member_pointer_value_p (folded))
5704 expr = folded;
5708 /* HACK: Due to double coercion, we can get a
5709 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5710 which is the tree that we built on the first call (see
5711 below when coercing to reference to object or to reference to
5712 function). We just strip everything and get to the arg.
5713 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5714 for examples. */
5715 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5717 tree probe_type, probe = expr;
5718 if (REFERENCE_REF_P (probe))
5719 probe = TREE_OPERAND (probe, 0);
5720 probe_type = TREE_TYPE (probe);
5721 if (TREE_CODE (probe) == NOP_EXPR)
5723 /* ??? Maybe we could use convert_from_reference here, but we
5724 would need to relax its constraints because the NOP_EXPR
5725 could actually change the type to something more cv-qualified,
5726 and this is not folded by convert_from_reference. */
5727 tree addr = TREE_OPERAND (probe, 0);
5728 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5729 && TREE_CODE (addr) == ADDR_EXPR
5730 && TYPE_PTR_P (TREE_TYPE (addr))
5731 && (same_type_ignoring_top_level_qualifiers_p
5732 (TREE_TYPE (probe_type),
5733 TREE_TYPE (TREE_TYPE (addr)))))
5735 expr = TREE_OPERAND (addr, 0);
5736 expr_type = TREE_TYPE (probe_type);
5741 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5742 parameter is a pointer to object, through decay and
5743 qualification conversion. Let's strip everything. */
5744 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5746 tree probe = expr;
5747 STRIP_NOPS (probe);
5748 if (TREE_CODE (probe) == ADDR_EXPR
5749 && TYPE_PTR_P (TREE_TYPE (probe)))
5751 /* Skip the ADDR_EXPR only if it is part of the decay for
5752 an array. Otherwise, it is part of the original argument
5753 in the source code. */
5754 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5755 probe = TREE_OPERAND (probe, 0);
5756 expr = probe;
5757 expr_type = TREE_TYPE (expr);
5761 /* [temp.arg.nontype]/5, bullet 1
5763 For a non-type template-parameter of integral or enumeration type,
5764 integral promotions (_conv.prom_) and integral conversions
5765 (_conv.integral_) are applied. */
5766 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5768 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5769 t = maybe_constant_value (t);
5770 if (t != error_mark_node)
5771 expr = t;
5773 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5774 return error_mark_node;
5776 /* Notice that there are constant expressions like '4 % 0' which
5777 do not fold into integer constants. */
5778 if (TREE_CODE (expr) != INTEGER_CST)
5780 if (complain & tf_error)
5782 int errs = errorcount, warns = warningcount + werrorcount;
5783 if (processing_template_decl
5784 && !require_potential_constant_expression (expr))
5785 return NULL_TREE;
5786 expr = cxx_constant_value (expr);
5787 if (errorcount > errs || warningcount + werrorcount > warns)
5788 inform (EXPR_LOC_OR_LOC (expr, input_location),
5789 "in template argument for type %qT ", type);
5790 if (expr == error_mark_node)
5791 return NULL_TREE;
5792 /* else cxx_constant_value complained but gave us
5793 a real constant, so go ahead. */
5794 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5796 else
5797 return NULL_TREE;
5800 /* Avoid typedef problems. */
5801 if (TREE_TYPE (expr) != type)
5802 expr = fold_convert (type, expr);
5804 /* [temp.arg.nontype]/5, bullet 2
5806 For a non-type template-parameter of type pointer to object,
5807 qualification conversions (_conv.qual_) and the array-to-pointer
5808 conversion (_conv.array_) are applied. */
5809 else if (TYPE_PTROBV_P (type))
5811 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5813 A template-argument for a non-type, non-template template-parameter
5814 shall be one of: [...]
5816 -- the name of a non-type template-parameter;
5817 -- the address of an object or function with external linkage, [...]
5818 expressed as "& id-expression" where the & is optional if the name
5819 refers to a function or array, or if the corresponding
5820 template-parameter is a reference.
5822 Here, we do not care about functions, as they are invalid anyway
5823 for a parameter of type pointer-to-object. */
5825 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5826 /* Non-type template parameters are OK. */
5828 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5829 /* Null pointer values are OK in C++11. */;
5830 else if (TREE_CODE (expr) != ADDR_EXPR
5831 && TREE_CODE (expr_type) != ARRAY_TYPE)
5833 if (VAR_P (expr))
5835 if (complain & tf_error)
5836 error ("%qD is not a valid template argument "
5837 "because %qD is a variable, not the address of "
5838 "a variable", expr, expr);
5839 return NULL_TREE;
5841 if (POINTER_TYPE_P (expr_type))
5843 if (complain & tf_error)
5844 error ("%qE is not a valid template argument for %qT "
5845 "because it is not the address of a variable",
5846 expr, type);
5847 return NULL_TREE;
5849 /* Other values, like integer constants, might be valid
5850 non-type arguments of some other type. */
5851 return error_mark_node;
5853 else
5855 tree decl;
5857 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5858 ? TREE_OPERAND (expr, 0) : expr);
5859 if (!VAR_P (decl))
5861 if (complain & tf_error)
5862 error ("%qE is not a valid template argument of type %qT "
5863 "because %qE is not a variable", expr, type, decl);
5864 return NULL_TREE;
5866 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5868 if (complain & tf_error)
5869 error ("%qE is not a valid template argument of type %qT "
5870 "because %qD does not have external linkage",
5871 expr, type, decl);
5872 return NULL_TREE;
5874 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5876 if (complain & tf_error)
5877 error ("%qE is not a valid template argument of type %qT "
5878 "because %qD has no linkage", expr, type, decl);
5879 return NULL_TREE;
5883 expr = decay_conversion (expr, complain);
5884 if (expr == error_mark_node)
5885 return error_mark_node;
5887 expr = perform_qualification_conversions (type, expr);
5888 if (expr == error_mark_node)
5889 return error_mark_node;
5891 /* [temp.arg.nontype]/5, bullet 3
5893 For a non-type template-parameter of type reference to object, no
5894 conversions apply. The type referred to by the reference may be more
5895 cv-qualified than the (otherwise identical) type of the
5896 template-argument. The template-parameter is bound directly to the
5897 template-argument, which must be an lvalue. */
5898 else if (TYPE_REF_OBJ_P (type))
5900 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5901 expr_type))
5902 return error_mark_node;
5904 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5906 if (complain & tf_error)
5907 error ("%qE is not a valid template argument for type %qT "
5908 "because of conflicts in cv-qualification", expr, type);
5909 return NULL_TREE;
5912 if (!real_lvalue_p (expr))
5914 if (complain & tf_error)
5915 error ("%qE is not a valid template argument for type %qT "
5916 "because it is not an lvalue", expr, type);
5917 return NULL_TREE;
5920 /* [temp.arg.nontype]/1
5922 A template-argument for a non-type, non-template template-parameter
5923 shall be one of: [...]
5925 -- the address of an object or function with external linkage. */
5926 if (INDIRECT_REF_P (expr)
5927 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5929 expr = TREE_OPERAND (expr, 0);
5930 if (DECL_P (expr))
5932 if (complain & tf_error)
5933 error ("%q#D is not a valid template argument for type %qT "
5934 "because a reference variable does not have a constant "
5935 "address", expr, type);
5936 return NULL_TREE;
5940 if (!DECL_P (expr))
5942 if (complain & tf_error)
5943 error ("%qE is not a valid template argument for type %qT "
5944 "because it is not an object with external linkage",
5945 expr, type);
5946 return NULL_TREE;
5949 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5951 if (complain & tf_error)
5952 error ("%qE is not a valid template argument for type %qT "
5953 "because object %qD has not external linkage",
5954 expr, type, expr);
5955 return NULL_TREE;
5958 expr = build_nop (type, build_address (expr));
5960 /* [temp.arg.nontype]/5, bullet 4
5962 For a non-type template-parameter of type pointer to function, only
5963 the function-to-pointer conversion (_conv.func_) is applied. If the
5964 template-argument represents a set of overloaded functions (or a
5965 pointer to such), the matching function is selected from the set
5966 (_over.over_). */
5967 else if (TYPE_PTRFN_P (type))
5969 /* If the argument is a template-id, we might not have enough
5970 context information to decay the pointer. */
5971 if (!type_unknown_p (expr_type))
5973 expr = decay_conversion (expr, complain);
5974 if (expr == error_mark_node)
5975 return error_mark_node;
5978 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5979 /* Null pointer values are OK in C++11. */
5980 return perform_qualification_conversions (type, expr);
5982 expr = convert_nontype_argument_function (type, expr);
5983 if (!expr || expr == error_mark_node)
5984 return expr;
5986 /* [temp.arg.nontype]/5, bullet 5
5988 For a non-type template-parameter of type reference to function, no
5989 conversions apply. If the template-argument represents a set of
5990 overloaded functions, the matching function is selected from the set
5991 (_over.over_). */
5992 else if (TYPE_REFFN_P (type))
5994 if (TREE_CODE (expr) == ADDR_EXPR)
5996 if (complain & tf_error)
5998 error ("%qE is not a valid template argument for type %qT "
5999 "because it is a pointer", expr, type);
6000 inform (input_location, "try using %qE instead",
6001 TREE_OPERAND (expr, 0));
6003 return NULL_TREE;
6006 expr = convert_nontype_argument_function (type, expr);
6007 if (!expr || expr == error_mark_node)
6008 return expr;
6010 expr = build_nop (type, build_address (expr));
6012 /* [temp.arg.nontype]/5, bullet 6
6014 For a non-type template-parameter of type pointer to member function,
6015 no conversions apply. If the template-argument represents a set of
6016 overloaded member functions, the matching member function is selected
6017 from the set (_over.over_). */
6018 else if (TYPE_PTRMEMFUNC_P (type))
6020 expr = instantiate_type (type, expr, tf_none);
6021 if (expr == error_mark_node)
6022 return error_mark_node;
6024 /* [temp.arg.nontype] bullet 1 says the pointer to member
6025 expression must be a pointer-to-member constant. */
6026 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6027 return error_mark_node;
6029 /* There is no way to disable standard conversions in
6030 resolve_address_of_overloaded_function (called by
6031 instantiate_type). It is possible that the call succeeded by
6032 converting &B::I to &D::I (where B is a base of D), so we need
6033 to reject this conversion here.
6035 Actually, even if there was a way to disable standard conversions,
6036 it would still be better to reject them here so that we can
6037 provide a superior diagnostic. */
6038 if (!same_type_p (TREE_TYPE (expr), type))
6040 if (complain & tf_error)
6042 error ("%qE is not a valid template argument for type %qT "
6043 "because it is of type %qT", expr, type,
6044 TREE_TYPE (expr));
6045 /* If we are just one standard conversion off, explain. */
6046 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6047 inform (input_location,
6048 "standard conversions are not allowed in this context");
6050 return NULL_TREE;
6053 /* [temp.arg.nontype]/5, bullet 7
6055 For a non-type template-parameter of type pointer to data member,
6056 qualification conversions (_conv.qual_) are applied. */
6057 else if (TYPE_PTRDATAMEM_P (type))
6059 /* [temp.arg.nontype] bullet 1 says the pointer to member
6060 expression must be a pointer-to-member constant. */
6061 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6062 return error_mark_node;
6064 expr = perform_qualification_conversions (type, expr);
6065 if (expr == error_mark_node)
6066 return expr;
6068 else if (NULLPTR_TYPE_P (type))
6070 if (expr != nullptr_node)
6072 if (complain & tf_error)
6073 error ("%qE is not a valid template argument for type %qT "
6074 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6075 return NULL_TREE;
6077 return expr;
6079 /* A template non-type parameter must be one of the above. */
6080 else
6081 gcc_unreachable ();
6083 /* Sanity check: did we actually convert the argument to the
6084 right type? */
6085 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6086 (type, TREE_TYPE (expr)));
6087 return convert_from_reference (expr);
6090 /* Subroutine of coerce_template_template_parms, which returns 1 if
6091 PARM_PARM and ARG_PARM match using the rule for the template
6092 parameters of template template parameters. Both PARM and ARG are
6093 template parameters; the rest of the arguments are the same as for
6094 coerce_template_template_parms.
6096 static int
6097 coerce_template_template_parm (tree parm,
6098 tree arg,
6099 tsubst_flags_t complain,
6100 tree in_decl,
6101 tree outer_args)
6103 if (arg == NULL_TREE || error_operand_p (arg)
6104 || parm == NULL_TREE || error_operand_p (parm))
6105 return 0;
6107 if (TREE_CODE (arg) != TREE_CODE (parm))
6108 return 0;
6110 switch (TREE_CODE (parm))
6112 case TEMPLATE_DECL:
6113 /* We encounter instantiations of templates like
6114 template <template <template <class> class> class TT>
6115 class C; */
6117 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6118 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6120 if (!coerce_template_template_parms
6121 (parmparm, argparm, complain, in_decl, outer_args))
6122 return 0;
6124 /* Fall through. */
6126 case TYPE_DECL:
6127 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6128 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6129 /* Argument is a parameter pack but parameter is not. */
6130 return 0;
6131 break;
6133 case PARM_DECL:
6134 /* The tsubst call is used to handle cases such as
6136 template <int> class C {};
6137 template <class T, template <T> class TT> class D {};
6138 D<int, C> d;
6140 i.e. the parameter list of TT depends on earlier parameters. */
6141 if (!uses_template_parms (TREE_TYPE (arg))
6142 && !same_type_p
6143 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6144 TREE_TYPE (arg)))
6145 return 0;
6147 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6148 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6149 /* Argument is a parameter pack but parameter is not. */
6150 return 0;
6152 break;
6154 default:
6155 gcc_unreachable ();
6158 return 1;
6162 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6163 template template parameters. Both PARM_PARMS and ARG_PARMS are
6164 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6165 or PARM_DECL.
6167 Consider the example:
6168 template <class T> class A;
6169 template<template <class U> class TT> class B;
6171 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6172 the parameters to A, and OUTER_ARGS contains A. */
6174 static int
6175 coerce_template_template_parms (tree parm_parms,
6176 tree arg_parms,
6177 tsubst_flags_t complain,
6178 tree in_decl,
6179 tree outer_args)
6181 int nparms, nargs, i;
6182 tree parm, arg;
6183 int variadic_p = 0;
6185 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6186 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6188 nparms = TREE_VEC_LENGTH (parm_parms);
6189 nargs = TREE_VEC_LENGTH (arg_parms);
6191 /* Determine whether we have a parameter pack at the end of the
6192 template template parameter's template parameter list. */
6193 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6195 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6197 if (error_operand_p (parm))
6198 return 0;
6200 switch (TREE_CODE (parm))
6202 case TEMPLATE_DECL:
6203 case TYPE_DECL:
6204 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6205 variadic_p = 1;
6206 break;
6208 case PARM_DECL:
6209 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6210 variadic_p = 1;
6211 break;
6213 default:
6214 gcc_unreachable ();
6218 if (nargs != nparms
6219 && !(variadic_p && nargs >= nparms - 1))
6220 return 0;
6222 /* Check all of the template parameters except the parameter pack at
6223 the end (if any). */
6224 for (i = 0; i < nparms - variadic_p; ++i)
6226 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6227 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6228 continue;
6230 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6231 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6233 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6234 outer_args))
6235 return 0;
6239 if (variadic_p)
6241 /* Check each of the template parameters in the template
6242 argument against the template parameter pack at the end of
6243 the template template parameter. */
6244 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6245 return 0;
6247 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6249 for (; i < nargs; ++i)
6251 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6252 continue;
6254 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6256 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6257 outer_args))
6258 return 0;
6262 return 1;
6265 /* Verifies that the deduced template arguments (in TARGS) for the
6266 template template parameters (in TPARMS) represent valid bindings,
6267 by comparing the template parameter list of each template argument
6268 to the template parameter list of its corresponding template
6269 template parameter, in accordance with DR150. This
6270 routine can only be called after all template arguments have been
6271 deduced. It will return TRUE if all of the template template
6272 parameter bindings are okay, FALSE otherwise. */
6273 bool
6274 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6276 int i, ntparms = TREE_VEC_LENGTH (tparms);
6277 bool ret = true;
6279 /* We're dealing with template parms in this process. */
6280 ++processing_template_decl;
6282 targs = INNERMOST_TEMPLATE_ARGS (targs);
6284 for (i = 0; i < ntparms; ++i)
6286 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6287 tree targ = TREE_VEC_ELT (targs, i);
6289 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6291 tree packed_args = NULL_TREE;
6292 int idx, len = 1;
6294 if (ARGUMENT_PACK_P (targ))
6296 /* Look inside the argument pack. */
6297 packed_args = ARGUMENT_PACK_ARGS (targ);
6298 len = TREE_VEC_LENGTH (packed_args);
6301 for (idx = 0; idx < len; ++idx)
6303 tree targ_parms = NULL_TREE;
6305 if (packed_args)
6306 /* Extract the next argument from the argument
6307 pack. */
6308 targ = TREE_VEC_ELT (packed_args, idx);
6310 if (PACK_EXPANSION_P (targ))
6311 /* Look at the pattern of the pack expansion. */
6312 targ = PACK_EXPANSION_PATTERN (targ);
6314 /* Extract the template parameters from the template
6315 argument. */
6316 if (TREE_CODE (targ) == TEMPLATE_DECL)
6317 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6318 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6319 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6321 /* Verify that we can coerce the template template
6322 parameters from the template argument to the template
6323 parameter. This requires an exact match. */
6324 if (targ_parms
6325 && !coerce_template_template_parms
6326 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6327 targ_parms,
6328 tf_none,
6329 tparm,
6330 targs))
6332 ret = false;
6333 goto out;
6339 out:
6341 --processing_template_decl;
6342 return ret;
6345 /* Since type attributes aren't mangled, we need to strip them from
6346 template type arguments. */
6348 static tree
6349 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6351 tree mv;
6352 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6353 return arg;
6354 mv = TYPE_MAIN_VARIANT (arg);
6355 arg = strip_typedefs (arg);
6356 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6357 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6359 if (complain & tf_warning)
6360 warning (0, "ignoring attributes on template argument %qT", arg);
6361 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6362 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6364 return arg;
6367 /* Convert the indicated template ARG as necessary to match the
6368 indicated template PARM. Returns the converted ARG, or
6369 error_mark_node if the conversion was unsuccessful. Error and
6370 warning messages are issued under control of COMPLAIN. This
6371 conversion is for the Ith parameter in the parameter list. ARGS is
6372 the full set of template arguments deduced so far. */
6374 static tree
6375 convert_template_argument (tree parm,
6376 tree arg,
6377 tree args,
6378 tsubst_flags_t complain,
6379 int i,
6380 tree in_decl)
6382 tree orig_arg;
6383 tree val;
6384 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6386 if (TREE_CODE (arg) == TREE_LIST
6387 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6389 /* The template argument was the name of some
6390 member function. That's usually
6391 invalid, but static members are OK. In any
6392 case, grab the underlying fields/functions
6393 and issue an error later if required. */
6394 orig_arg = TREE_VALUE (arg);
6395 TREE_TYPE (arg) = unknown_type_node;
6398 orig_arg = arg;
6400 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6401 requires_type = (TREE_CODE (parm) == TYPE_DECL
6402 || requires_tmpl_type);
6404 /* When determining whether an argument pack expansion is a template,
6405 look at the pattern. */
6406 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6407 arg = PACK_EXPANSION_PATTERN (arg);
6409 /* Deal with an injected-class-name used as a template template arg. */
6410 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6412 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6413 if (TREE_CODE (t) == TEMPLATE_DECL)
6415 if (cxx_dialect >= cxx11)
6416 /* OK under DR 1004. */;
6417 else if (complain & tf_warning_or_error)
6418 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6419 " used as template template argument", TYPE_NAME (arg));
6420 else if (flag_pedantic_errors)
6421 t = arg;
6423 arg = t;
6427 is_tmpl_type =
6428 ((TREE_CODE (arg) == TEMPLATE_DECL
6429 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6430 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6431 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6432 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6434 if (is_tmpl_type
6435 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6436 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6437 arg = TYPE_STUB_DECL (arg);
6439 is_type = TYPE_P (arg) || is_tmpl_type;
6441 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6442 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6444 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6446 if (complain & tf_error)
6447 error ("invalid use of destructor %qE as a type", orig_arg);
6448 return error_mark_node;
6451 permerror (input_location,
6452 "to refer to a type member of a template parameter, "
6453 "use %<typename %E%>", orig_arg);
6455 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6456 TREE_OPERAND (arg, 1),
6457 typename_type,
6458 complain);
6459 arg = orig_arg;
6460 is_type = 1;
6462 if (is_type != requires_type)
6464 if (in_decl)
6466 if (complain & tf_error)
6468 error ("type/value mismatch at argument %d in template "
6469 "parameter list for %qD",
6470 i + 1, in_decl);
6471 if (is_type)
6472 error (" expected a constant of type %qT, got %qT",
6473 TREE_TYPE (parm),
6474 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6475 else if (requires_tmpl_type)
6476 error (" expected a class template, got %qE", orig_arg);
6477 else
6478 error (" expected a type, got %qE", orig_arg);
6481 return error_mark_node;
6483 if (is_tmpl_type ^ requires_tmpl_type)
6485 if (in_decl && (complain & tf_error))
6487 error ("type/value mismatch at argument %d in template "
6488 "parameter list for %qD",
6489 i + 1, in_decl);
6490 if (is_tmpl_type)
6491 error (" expected a type, got %qT", DECL_NAME (arg));
6492 else
6493 error (" expected a class template, got %qT", orig_arg);
6495 return error_mark_node;
6498 if (is_type)
6500 if (requires_tmpl_type)
6502 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6503 val = orig_arg;
6504 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6505 /* The number of argument required is not known yet.
6506 Just accept it for now. */
6507 val = TREE_TYPE (arg);
6508 else
6510 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6511 tree argparm;
6513 /* Strip alias templates that are equivalent to another
6514 template. */
6515 arg = get_underlying_template (arg);
6516 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6518 if (coerce_template_template_parms (parmparm, argparm,
6519 complain, in_decl,
6520 args))
6522 val = arg;
6524 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6525 TEMPLATE_DECL. */
6526 if (val != error_mark_node)
6528 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6529 val = TREE_TYPE (val);
6530 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6531 val = make_pack_expansion (val);
6534 else
6536 if (in_decl && (complain & tf_error))
6538 error ("type/value mismatch at argument %d in "
6539 "template parameter list for %qD",
6540 i + 1, in_decl);
6541 error (" expected a template of type %qD, got %qT",
6542 parm, orig_arg);
6545 val = error_mark_node;
6549 else
6550 val = orig_arg;
6551 /* We only form one instance of each template specialization.
6552 Therefore, if we use a non-canonical variant (i.e., a
6553 typedef), any future messages referring to the type will use
6554 the typedef, which is confusing if those future uses do not
6555 themselves also use the typedef. */
6556 if (TYPE_P (val))
6557 val = canonicalize_type_argument (val, complain);
6559 else
6561 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6563 if (invalid_nontype_parm_type_p (t, complain))
6564 return error_mark_node;
6566 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6568 if (same_type_p (t, TREE_TYPE (orig_arg)))
6569 val = orig_arg;
6570 else
6572 /* Not sure if this is reachable, but it doesn't hurt
6573 to be robust. */
6574 error ("type mismatch in nontype parameter pack");
6575 val = error_mark_node;
6578 else if (!dependent_template_arg_p (orig_arg)
6579 && !uses_template_parms (t))
6580 /* We used to call digest_init here. However, digest_init
6581 will report errors, which we don't want when complain
6582 is zero. More importantly, digest_init will try too
6583 hard to convert things: for example, `0' should not be
6584 converted to pointer type at this point according to
6585 the standard. Accepting this is not merely an
6586 extension, since deciding whether or not these
6587 conversions can occur is part of determining which
6588 function template to call, or whether a given explicit
6589 argument specification is valid. */
6590 val = convert_nontype_argument (t, orig_arg, complain);
6591 else
6592 val = strip_typedefs_expr (orig_arg);
6594 if (val == NULL_TREE)
6595 val = error_mark_node;
6596 else if (val == error_mark_node && (complain & tf_error))
6597 error ("could not convert template argument %qE to %qT", orig_arg, t);
6599 if (TREE_CODE (val) == SCOPE_REF)
6601 /* Strip typedefs from the SCOPE_REF. */
6602 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6603 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6604 complain);
6605 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6606 QUALIFIED_NAME_IS_TEMPLATE (val));
6610 return val;
6613 /* Coerces the remaining template arguments in INNER_ARGS (from
6614 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6615 Returns the coerced argument pack. PARM_IDX is the position of this
6616 parameter in the template parameter list. ARGS is the original
6617 template argument list. */
6618 static tree
6619 coerce_template_parameter_pack (tree parms,
6620 int parm_idx,
6621 tree args,
6622 tree inner_args,
6623 int arg_idx,
6624 tree new_args,
6625 int* lost,
6626 tree in_decl,
6627 tsubst_flags_t complain)
6629 tree parm = TREE_VEC_ELT (parms, parm_idx);
6630 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6631 tree packed_args;
6632 tree argument_pack;
6633 tree packed_parms = NULL_TREE;
6635 if (arg_idx > nargs)
6636 arg_idx = nargs;
6638 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6640 /* When the template parameter is a non-type template parameter pack
6641 or template template parameter pack whose type or template
6642 parameters use parameter packs, we know exactly how many arguments
6643 we are looking for. Build a vector of the instantiated decls for
6644 these template parameters in PACKED_PARMS. */
6645 /* We can't use make_pack_expansion here because it would interpret a
6646 _DECL as a use rather than a declaration. */
6647 tree decl = TREE_VALUE (parm);
6648 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6649 SET_PACK_EXPANSION_PATTERN (exp, decl);
6650 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6651 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6653 TREE_VEC_LENGTH (args)--;
6654 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6655 TREE_VEC_LENGTH (args)++;
6657 if (packed_parms == error_mark_node)
6658 return error_mark_node;
6660 /* If we're doing a partial instantiation of a member template,
6661 verify that all of the types used for the non-type
6662 template parameter pack are, in fact, valid for non-type
6663 template parameters. */
6664 if (arg_idx < nargs
6665 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6667 int j, len = TREE_VEC_LENGTH (packed_parms);
6668 for (j = 0; j < len; ++j)
6670 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6671 if (invalid_nontype_parm_type_p (t, complain))
6672 return error_mark_node;
6674 /* We don't know how many args we have yet, just
6675 use the unconverted ones for now. */
6676 return NULL_TREE;
6679 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6681 else
6682 packed_args = make_tree_vec (nargs - arg_idx);
6684 /* Convert the remaining arguments, which will be a part of the
6685 parameter pack "parm". */
6686 for (; arg_idx < nargs; ++arg_idx)
6688 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6689 tree actual_parm = TREE_VALUE (parm);
6690 int pack_idx = arg_idx - parm_idx;
6692 if (packed_parms)
6694 /* Once we've packed as many args as we have types, stop. */
6695 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6696 break;
6697 else if (PACK_EXPANSION_P (arg))
6698 /* We don't know how many args we have yet, just
6699 use the unconverted ones for now. */
6700 return NULL_TREE;
6701 else
6702 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6705 if (arg == error_mark_node)
6707 if (complain & tf_error)
6708 error ("template argument %d is invalid", arg_idx + 1);
6710 else
6711 arg = convert_template_argument (actual_parm,
6712 arg, new_args, complain, parm_idx,
6713 in_decl);
6714 if (arg == error_mark_node)
6715 (*lost)++;
6716 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6719 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6720 && TREE_VEC_LENGTH (packed_args) > 0)
6722 error ("wrong number of template arguments (%d, should be %d)",
6723 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6724 return error_mark_node;
6727 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6728 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6729 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6730 else
6732 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6733 TREE_TYPE (argument_pack)
6734 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6735 TREE_CONSTANT (argument_pack) = 1;
6738 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6739 #ifdef ENABLE_CHECKING
6740 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6741 TREE_VEC_LENGTH (packed_args));
6742 #endif
6743 return argument_pack;
6746 /* Returns the number of pack expansions in the template argument vector
6747 ARGS. */
6749 static int
6750 pack_expansion_args_count (tree args)
6752 int i;
6753 int count = 0;
6754 if (args)
6755 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6757 tree elt = TREE_VEC_ELT (args, i);
6758 if (elt && PACK_EXPANSION_P (elt))
6759 ++count;
6761 return count;
6764 /* Convert all template arguments to their appropriate types, and
6765 return a vector containing the innermost resulting template
6766 arguments. If any error occurs, return error_mark_node. Error and
6767 warning messages are issued under control of COMPLAIN.
6769 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6770 for arguments not specified in ARGS. Otherwise, if
6771 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6772 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6773 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6774 ARGS. */
6776 static tree
6777 coerce_template_parms (tree parms,
6778 tree args,
6779 tree in_decl,
6780 tsubst_flags_t complain,
6781 bool require_all_args,
6782 bool use_default_args)
6784 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6785 tree orig_inner_args;
6786 tree inner_args;
6787 tree new_args;
6788 tree new_inner_args;
6789 int saved_unevaluated_operand;
6790 int saved_inhibit_evaluation_warnings;
6792 /* When used as a boolean value, indicates whether this is a
6793 variadic template parameter list. Since it's an int, we can also
6794 subtract it from nparms to get the number of non-variadic
6795 parameters. */
6796 int variadic_p = 0;
6797 int variadic_args_p = 0;
6798 int post_variadic_parms = 0;
6800 if (args == error_mark_node)
6801 return error_mark_node;
6803 nparms = TREE_VEC_LENGTH (parms);
6805 /* Determine if there are any parameter packs. */
6806 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6808 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6809 if (variadic_p)
6810 ++post_variadic_parms;
6811 if (template_parameter_pack_p (tparm))
6812 ++variadic_p;
6815 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6816 /* If there are no parameters that follow a parameter pack, we need to
6817 expand any argument packs so that we can deduce a parameter pack from
6818 some non-packed args followed by an argument pack, as in variadic85.C.
6819 If there are such parameters, we need to leave argument packs intact
6820 so the arguments are assigned properly. This can happen when dealing
6821 with a nested class inside a partial specialization of a class
6822 template, as in variadic92.C, or when deducing a template parameter pack
6823 from a sub-declarator, as in variadic114.C. */
6824 if (!post_variadic_parms)
6825 inner_args = expand_template_argument_pack (inner_args);
6827 /* Count any pack expansion args. */
6828 variadic_args_p = pack_expansion_args_count (inner_args);
6830 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6831 if ((nargs > nparms && !variadic_p)
6832 || (nargs < nparms - variadic_p
6833 && require_all_args
6834 && !variadic_args_p
6835 && (!use_default_args
6836 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6837 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6839 if (complain & tf_error)
6841 if (variadic_p)
6843 nparms -= variadic_p;
6844 error ("wrong number of template arguments "
6845 "(%d, should be %d or more)", nargs, nparms);
6847 else
6848 error ("wrong number of template arguments "
6849 "(%d, should be %d)", nargs, nparms);
6851 if (in_decl)
6852 error ("provided for %q+D", in_decl);
6855 return error_mark_node;
6857 /* We can't pass a pack expansion to a non-pack parameter of an alias
6858 template (DR 1430). */
6859 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6860 && variadic_args_p
6861 && nargs - variadic_args_p < nparms - variadic_p)
6863 if (complain & tf_error)
6865 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6867 tree arg = TREE_VEC_ELT (inner_args, i);
6868 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6870 if (PACK_EXPANSION_P (arg)
6871 && !template_parameter_pack_p (parm))
6873 error ("pack expansion argument for non-pack parameter "
6874 "%qD of alias template %qD", parm, in_decl);
6875 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6876 goto found;
6879 gcc_unreachable ();
6880 found:;
6882 return error_mark_node;
6885 /* We need to evaluate the template arguments, even though this
6886 template-id may be nested within a "sizeof". */
6887 saved_unevaluated_operand = cp_unevaluated_operand;
6888 cp_unevaluated_operand = 0;
6889 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6890 c_inhibit_evaluation_warnings = 0;
6891 new_inner_args = make_tree_vec (nparms);
6892 new_args = add_outermost_template_args (args, new_inner_args);
6893 int pack_adjust = 0;
6894 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6896 tree arg;
6897 tree parm;
6899 /* Get the Ith template parameter. */
6900 parm = TREE_VEC_ELT (parms, parm_idx);
6902 if (parm == error_mark_node)
6904 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6905 continue;
6908 /* Calculate the next argument. */
6909 if (arg_idx < nargs)
6910 arg = TREE_VEC_ELT (inner_args, arg_idx);
6911 else
6912 arg = NULL_TREE;
6914 if (template_parameter_pack_p (TREE_VALUE (parm))
6915 && !(arg && ARGUMENT_PACK_P (arg)))
6917 /* Some arguments will be placed in the
6918 template parameter pack PARM. */
6919 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6920 inner_args, arg_idx,
6921 new_args, &lost,
6922 in_decl, complain);
6924 if (arg == NULL_TREE)
6926 /* We don't know how many args we have yet, just use the
6927 unconverted (and still packed) ones for now. */
6928 new_inner_args = orig_inner_args;
6929 arg_idx = nargs;
6930 break;
6933 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6935 /* Store this argument. */
6936 if (arg == error_mark_node)
6938 lost++;
6939 /* We are done with all of the arguments. */
6940 arg_idx = nargs;
6942 else
6944 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
6945 arg_idx += pack_adjust;
6948 continue;
6950 else if (arg)
6952 if (PACK_EXPANSION_P (arg))
6954 /* "If every valid specialization of a variadic template
6955 requires an empty template parameter pack, the template is
6956 ill-formed, no diagnostic required." So check that the
6957 pattern works with this parameter. */
6958 tree pattern = PACK_EXPANSION_PATTERN (arg);
6959 tree conv = convert_template_argument (TREE_VALUE (parm),
6960 pattern, new_args,
6961 complain, parm_idx,
6962 in_decl);
6963 if (conv == error_mark_node)
6965 inform (input_location, "so any instantiation with a "
6966 "non-empty parameter pack would be ill-formed");
6967 ++lost;
6969 else if (TYPE_P (conv) && !TYPE_P (pattern))
6970 /* Recover from missing typename. */
6971 TREE_VEC_ELT (inner_args, arg_idx)
6972 = make_pack_expansion (conv);
6974 /* We don't know how many args we have yet, just
6975 use the unconverted ones for now. */
6976 new_inner_args = inner_args;
6977 arg_idx = nargs;
6978 break;
6981 else if (require_all_args)
6983 /* There must be a default arg in this case. */
6984 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6985 complain, in_decl);
6986 /* The position of the first default template argument,
6987 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6988 Record that. */
6989 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6990 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6991 arg_idx - pack_adjust);
6993 else
6994 break;
6996 if (arg == error_mark_node)
6998 if (complain & tf_error)
6999 error ("template argument %d is invalid", arg_idx + 1);
7001 else if (!arg)
7002 /* This only occurs if there was an error in the template
7003 parameter list itself (which we would already have
7004 reported) that we are trying to recover from, e.g., a class
7005 template with a parameter list such as
7006 template<typename..., typename>. */
7007 ++lost;
7008 else
7009 arg = convert_template_argument (TREE_VALUE (parm),
7010 arg, new_args, complain,
7011 parm_idx, in_decl);
7013 if (arg == error_mark_node)
7014 lost++;
7015 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7017 cp_unevaluated_operand = saved_unevaluated_operand;
7018 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7020 if (variadic_p && arg_idx < nargs)
7022 if (complain & tf_error)
7024 error ("wrong number of template arguments "
7025 "(%d, should be %d)", nargs, arg_idx);
7026 if (in_decl)
7027 error ("provided for %q+D", in_decl);
7029 return error_mark_node;
7032 if (lost)
7033 return error_mark_node;
7035 #ifdef ENABLE_CHECKING
7036 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7037 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7038 TREE_VEC_LENGTH (new_inner_args));
7039 #endif
7041 return new_inner_args;
7044 /* Like coerce_template_parms. If PARMS represents all template
7045 parameters levels, this function returns a vector of vectors
7046 representing all the resulting argument levels. Note that in this
7047 case, only the innermost arguments are coerced because the
7048 outermost ones are supposed to have been coerced already.
7050 Otherwise, if PARMS represents only (the innermost) vector of
7051 parameters, this function returns a vector containing just the
7052 innermost resulting arguments. */
7054 static tree
7055 coerce_innermost_template_parms (tree parms,
7056 tree args,
7057 tree in_decl,
7058 tsubst_flags_t complain,
7059 bool require_all_args,
7060 bool use_default_args)
7062 int parms_depth = TMPL_PARMS_DEPTH (parms);
7063 int args_depth = TMPL_ARGS_DEPTH (args);
7064 tree coerced_args;
7066 if (parms_depth > 1)
7068 coerced_args = make_tree_vec (parms_depth);
7069 tree level;
7070 int cur_depth;
7072 for (level = parms, cur_depth = parms_depth;
7073 parms_depth > 0 && level != NULL_TREE;
7074 level = TREE_CHAIN (level), --cur_depth)
7076 tree l;
7077 if (cur_depth == args_depth)
7078 l = coerce_template_parms (TREE_VALUE (level),
7079 args, in_decl, complain,
7080 require_all_args,
7081 use_default_args);
7082 else
7083 l = TMPL_ARGS_LEVEL (args, cur_depth);
7085 if (l == error_mark_node)
7086 return error_mark_node;
7088 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7091 else
7092 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7093 args, in_decl, complain,
7094 require_all_args,
7095 use_default_args);
7096 return coerced_args;
7099 /* Returns 1 if template args OT and NT are equivalent. */
7101 static int
7102 template_args_equal (tree ot, tree nt)
7104 if (nt == ot)
7105 return 1;
7106 if (nt == NULL_TREE || ot == NULL_TREE)
7107 return false;
7109 if (TREE_CODE (nt) == TREE_VEC)
7110 /* For member templates */
7111 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7112 else if (PACK_EXPANSION_P (ot))
7113 return (PACK_EXPANSION_P (nt)
7114 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7115 PACK_EXPANSION_PATTERN (nt))
7116 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7117 PACK_EXPANSION_EXTRA_ARGS (nt)));
7118 else if (ARGUMENT_PACK_P (ot))
7120 int i, len;
7121 tree opack, npack;
7123 if (!ARGUMENT_PACK_P (nt))
7124 return 0;
7126 opack = ARGUMENT_PACK_ARGS (ot);
7127 npack = ARGUMENT_PACK_ARGS (nt);
7128 len = TREE_VEC_LENGTH (opack);
7129 if (TREE_VEC_LENGTH (npack) != len)
7130 return 0;
7131 for (i = 0; i < len; ++i)
7132 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7133 TREE_VEC_ELT (npack, i)))
7134 return 0;
7135 return 1;
7137 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7139 /* We get here probably because we are in the middle of substituting
7140 into the pattern of a pack expansion. In that case the
7141 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7142 interested in. So we want to use the initial pack argument for
7143 the comparison. */
7144 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7145 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7146 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7147 return template_args_equal (ot, nt);
7149 else if (TYPE_P (nt))
7150 return TYPE_P (ot) && same_type_p (ot, nt);
7151 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7152 return 0;
7153 else
7154 return cp_tree_equal (ot, nt);
7157 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7158 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7159 NEWARG_PTR with the offending arguments if they are non-NULL. */
7161 static int
7162 comp_template_args_with_info (tree oldargs, tree newargs,
7163 tree *oldarg_ptr, tree *newarg_ptr)
7165 int i;
7167 if (oldargs == newargs)
7168 return 1;
7170 if (!oldargs || !newargs)
7171 return 0;
7173 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7174 return 0;
7176 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7178 tree nt = TREE_VEC_ELT (newargs, i);
7179 tree ot = TREE_VEC_ELT (oldargs, i);
7181 if (! template_args_equal (ot, nt))
7183 if (oldarg_ptr != NULL)
7184 *oldarg_ptr = ot;
7185 if (newarg_ptr != NULL)
7186 *newarg_ptr = nt;
7187 return 0;
7190 return 1;
7193 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7194 of template arguments. Returns 0 otherwise. */
7197 comp_template_args (tree oldargs, tree newargs)
7199 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7202 static void
7203 add_pending_template (tree d)
7205 tree ti = (TYPE_P (d)
7206 ? CLASSTYPE_TEMPLATE_INFO (d)
7207 : DECL_TEMPLATE_INFO (d));
7208 struct pending_template *pt;
7209 int level;
7211 if (TI_PENDING_TEMPLATE_FLAG (ti))
7212 return;
7214 /* We are called both from instantiate_decl, where we've already had a
7215 tinst_level pushed, and instantiate_template, where we haven't.
7216 Compensate. */
7217 level = !current_tinst_level || current_tinst_level->decl != d;
7219 if (level)
7220 push_tinst_level (d);
7222 pt = ggc_alloc_pending_template ();
7223 pt->next = NULL;
7224 pt->tinst = current_tinst_level;
7225 if (last_pending_template)
7226 last_pending_template->next = pt;
7227 else
7228 pending_templates = pt;
7230 last_pending_template = pt;
7232 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7234 if (level)
7235 pop_tinst_level ();
7239 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7240 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7241 documentation for TEMPLATE_ID_EXPR. */
7243 tree
7244 lookup_template_function (tree fns, tree arglist)
7246 tree type;
7248 if (fns == error_mark_node || arglist == error_mark_node)
7249 return error_mark_node;
7251 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7253 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7255 error ("%q#D is not a function template", fns);
7256 return error_mark_node;
7259 if (BASELINK_P (fns))
7261 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7262 unknown_type_node,
7263 BASELINK_FUNCTIONS (fns),
7264 arglist);
7265 return fns;
7268 type = TREE_TYPE (fns);
7269 if (TREE_CODE (fns) == OVERLOAD || !type)
7270 type = unknown_type_node;
7272 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7275 /* Within the scope of a template class S<T>, the name S gets bound
7276 (in build_self_reference) to a TYPE_DECL for the class, not a
7277 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7278 or one of its enclosing classes, and that type is a template,
7279 return the associated TEMPLATE_DECL. Otherwise, the original
7280 DECL is returned.
7282 Also handle the case when DECL is a TREE_LIST of ambiguous
7283 injected-class-names from different bases. */
7285 tree
7286 maybe_get_template_decl_from_type_decl (tree decl)
7288 if (decl == NULL_TREE)
7289 return decl;
7291 /* DR 176: A lookup that finds an injected-class-name (10.2
7292 [class.member.lookup]) can result in an ambiguity in certain cases
7293 (for example, if it is found in more than one base class). If all of
7294 the injected-class-names that are found refer to specializations of
7295 the same class template, and if the name is followed by a
7296 template-argument-list, the reference refers to the class template
7297 itself and not a specialization thereof, and is not ambiguous. */
7298 if (TREE_CODE (decl) == TREE_LIST)
7300 tree t, tmpl = NULL_TREE;
7301 for (t = decl; t; t = TREE_CHAIN (t))
7303 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7304 if (!tmpl)
7305 tmpl = elt;
7306 else if (tmpl != elt)
7307 break;
7309 if (tmpl && t == NULL_TREE)
7310 return tmpl;
7311 else
7312 return decl;
7315 return (decl != NULL_TREE
7316 && DECL_SELF_REFERENCE_P (decl)
7317 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7318 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7321 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7322 parameters, find the desired type.
7324 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7326 IN_DECL, if non-NULL, is the template declaration we are trying to
7327 instantiate.
7329 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7330 the class we are looking up.
7332 Issue error and warning messages under control of COMPLAIN.
7334 If the template class is really a local class in a template
7335 function, then the FUNCTION_CONTEXT is the function in which it is
7336 being instantiated.
7338 ??? Note that this function is currently called *twice* for each
7339 template-id: the first time from the parser, while creating the
7340 incomplete type (finish_template_type), and the second type during the
7341 real instantiation (instantiate_template_class). This is surely something
7342 that we want to avoid. It also causes some problems with argument
7343 coercion (see convert_nontype_argument for more information on this). */
7345 static tree
7346 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7347 int entering_scope, tsubst_flags_t complain)
7349 tree templ = NULL_TREE, parmlist;
7350 tree t;
7351 void **slot;
7352 spec_entry *entry;
7353 spec_entry elt;
7354 hashval_t hash;
7356 if (identifier_p (d1))
7358 tree value = innermost_non_namespace_value (d1);
7359 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7360 templ = value;
7361 else
7363 if (context)
7364 push_decl_namespace (context);
7365 templ = lookup_name (d1);
7366 templ = maybe_get_template_decl_from_type_decl (templ);
7367 if (context)
7368 pop_decl_namespace ();
7370 if (templ)
7371 context = DECL_CONTEXT (templ);
7373 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7375 tree type = TREE_TYPE (d1);
7377 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7378 an implicit typename for the second A. Deal with it. */
7379 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7380 type = TREE_TYPE (type);
7382 if (CLASSTYPE_TEMPLATE_INFO (type))
7384 templ = CLASSTYPE_TI_TEMPLATE (type);
7385 d1 = DECL_NAME (templ);
7388 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7389 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7391 templ = TYPE_TI_TEMPLATE (d1);
7392 d1 = DECL_NAME (templ);
7394 else if (TREE_CODE (d1) == TEMPLATE_DECL
7395 && DECL_TEMPLATE_RESULT (d1)
7396 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7398 templ = d1;
7399 d1 = DECL_NAME (templ);
7400 context = DECL_CONTEXT (templ);
7402 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7404 templ = d1;
7405 d1 = DECL_NAME (templ);
7408 /* Issue an error message if we didn't find a template. */
7409 if (! templ)
7411 if (complain & tf_error)
7412 error ("%qT is not a template", d1);
7413 return error_mark_node;
7416 if (TREE_CODE (templ) != TEMPLATE_DECL
7417 /* Make sure it's a user visible template, if it was named by
7418 the user. */
7419 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7420 && !PRIMARY_TEMPLATE_P (templ)))
7422 if (complain & tf_error)
7424 error ("non-template type %qT used as a template", d1);
7425 if (in_decl)
7426 error ("for template declaration %q+D", in_decl);
7428 return error_mark_node;
7431 complain &= ~tf_user;
7433 /* An alias that just changes the name of a template is equivalent to the
7434 other template, so if any of the arguments are pack expansions, strip
7435 the alias to avoid problems with a pack expansion passed to a non-pack
7436 alias template parameter (DR 1430). */
7437 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7438 templ = get_underlying_template (templ);
7440 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7442 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7443 template arguments */
7445 tree parm;
7446 tree arglist2;
7447 tree outer;
7449 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7451 /* Consider an example where a template template parameter declared as
7453 template <class T, class U = std::allocator<T> > class TT
7455 The template parameter level of T and U are one level larger than
7456 of TT. To proper process the default argument of U, say when an
7457 instantiation `TT<int>' is seen, we need to build the full
7458 arguments containing {int} as the innermost level. Outer levels,
7459 available when not appearing as default template argument, can be
7460 obtained from the arguments of the enclosing template.
7462 Suppose that TT is later substituted with std::vector. The above
7463 instantiation is `TT<int, std::allocator<T> >' with TT at
7464 level 1, and T at level 2, while the template arguments at level 1
7465 becomes {std::vector} and the inner level 2 is {int}. */
7467 outer = DECL_CONTEXT (templ);
7468 if (outer)
7469 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7470 else if (current_template_parms)
7471 /* This is an argument of the current template, so we haven't set
7472 DECL_CONTEXT yet. */
7473 outer = current_template_args ();
7475 if (outer)
7476 arglist = add_to_template_args (outer, arglist);
7478 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7479 complain,
7480 /*require_all_args=*/true,
7481 /*use_default_args=*/true);
7482 if (arglist2 == error_mark_node
7483 || (!uses_template_parms (arglist2)
7484 && check_instantiated_args (templ, arglist2, complain)))
7485 return error_mark_node;
7487 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7488 return parm;
7490 else
7492 tree template_type = TREE_TYPE (templ);
7493 tree gen_tmpl;
7494 tree type_decl;
7495 tree found = NULL_TREE;
7496 int arg_depth;
7497 int parm_depth;
7498 int is_dependent_type;
7499 int use_partial_inst_tmpl = false;
7501 if (template_type == error_mark_node)
7502 /* An error occurred while building the template TEMPL, and a
7503 diagnostic has most certainly been emitted for that
7504 already. Let's propagate that error. */
7505 return error_mark_node;
7507 gen_tmpl = most_general_template (templ);
7508 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7509 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7510 arg_depth = TMPL_ARGS_DEPTH (arglist);
7512 if (arg_depth == 1 && parm_depth > 1)
7514 /* We've been given an incomplete set of template arguments.
7515 For example, given:
7517 template <class T> struct S1 {
7518 template <class U> struct S2 {};
7519 template <class U> struct S2<U*> {};
7522 we will be called with an ARGLIST of `U*', but the
7523 TEMPLATE will be `template <class T> template
7524 <class U> struct S1<T>::S2'. We must fill in the missing
7525 arguments. */
7526 arglist
7527 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7528 arglist);
7529 arg_depth = TMPL_ARGS_DEPTH (arglist);
7532 /* Now we should have enough arguments. */
7533 gcc_assert (parm_depth == arg_depth);
7535 /* From here on, we're only interested in the most general
7536 template. */
7538 /* Calculate the BOUND_ARGS. These will be the args that are
7539 actually tsubst'd into the definition to create the
7540 instantiation. */
7541 if (parm_depth > 1)
7543 /* We have multiple levels of arguments to coerce, at once. */
7544 int i;
7545 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7547 tree bound_args = make_tree_vec (parm_depth);
7549 for (i = saved_depth,
7550 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7551 i > 0 && t != NULL_TREE;
7552 --i, t = TREE_CHAIN (t))
7554 tree a;
7555 if (i == saved_depth)
7556 a = coerce_template_parms (TREE_VALUE (t),
7557 arglist, gen_tmpl,
7558 complain,
7559 /*require_all_args=*/true,
7560 /*use_default_args=*/true);
7561 else
7562 /* Outer levels should have already been coerced. */
7563 a = TMPL_ARGS_LEVEL (arglist, i);
7565 /* Don't process further if one of the levels fails. */
7566 if (a == error_mark_node)
7568 /* Restore the ARGLIST to its full size. */
7569 TREE_VEC_LENGTH (arglist) = saved_depth;
7570 return error_mark_node;
7573 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7575 /* We temporarily reduce the length of the ARGLIST so
7576 that coerce_template_parms will see only the arguments
7577 corresponding to the template parameters it is
7578 examining. */
7579 TREE_VEC_LENGTH (arglist)--;
7582 /* Restore the ARGLIST to its full size. */
7583 TREE_VEC_LENGTH (arglist) = saved_depth;
7585 arglist = bound_args;
7587 else
7588 arglist
7589 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7590 INNERMOST_TEMPLATE_ARGS (arglist),
7591 gen_tmpl,
7592 complain,
7593 /*require_all_args=*/true,
7594 /*use_default_args=*/true);
7596 if (arglist == error_mark_node)
7597 /* We were unable to bind the arguments. */
7598 return error_mark_node;
7600 /* In the scope of a template class, explicit references to the
7601 template class refer to the type of the template, not any
7602 instantiation of it. For example, in:
7604 template <class T> class C { void f(C<T>); }
7606 the `C<T>' is just the same as `C'. Outside of the
7607 class, however, such a reference is an instantiation. */
7608 if ((entering_scope
7609 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7610 || currently_open_class (template_type))
7611 /* comp_template_args is expensive, check it last. */
7612 && comp_template_args (TYPE_TI_ARGS (template_type),
7613 arglist))
7614 return template_type;
7616 /* If we already have this specialization, return it. */
7617 elt.tmpl = gen_tmpl;
7618 elt.args = arglist;
7619 hash = hash_specialization (&elt);
7620 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7621 &elt, hash);
7623 if (entry)
7624 return entry->spec;
7626 is_dependent_type = uses_template_parms (arglist);
7628 /* If the deduced arguments are invalid, then the binding
7629 failed. */
7630 if (!is_dependent_type
7631 && check_instantiated_args (gen_tmpl,
7632 INNERMOST_TEMPLATE_ARGS (arglist),
7633 complain))
7634 return error_mark_node;
7636 if (!is_dependent_type
7637 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7638 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7639 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7641 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7642 DECL_NAME (gen_tmpl),
7643 /*tag_scope=*/ts_global);
7644 return found;
7647 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7648 complain, in_decl);
7649 if (context == error_mark_node)
7650 return error_mark_node;
7652 if (!context)
7653 context = global_namespace;
7655 /* Create the type. */
7656 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7658 /* The user referred to a specialization of an alias
7659 template represented by GEN_TMPL.
7661 [temp.alias]/2 says:
7663 When a template-id refers to the specialization of an
7664 alias template, it is equivalent to the associated
7665 type obtained by substitution of its
7666 template-arguments for the template-parameters in the
7667 type-id of the alias template. */
7669 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7670 /* Note that the call above (by indirectly calling
7671 register_specialization in tsubst_decl) registers the
7672 TYPE_DECL representing the specialization of the alias
7673 template. So next time someone substitutes ARGLIST for
7674 the template parms into the alias template (GEN_TMPL),
7675 she'll get that TYPE_DECL back. */
7677 if (t == error_mark_node)
7678 return t;
7680 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7682 if (!is_dependent_type)
7684 set_current_access_from_decl (TYPE_NAME (template_type));
7685 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7686 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7687 arglist, complain, in_decl),
7688 SCOPED_ENUM_P (template_type), NULL);
7690 if (t == error_mark_node)
7691 return t;
7693 else
7695 /* We don't want to call start_enum for this type, since
7696 the values for the enumeration constants may involve
7697 template parameters. And, no one should be interested
7698 in the enumeration constants for such a type. */
7699 t = cxx_make_type (ENUMERAL_TYPE);
7700 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7702 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7703 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7704 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7706 else if (CLASS_TYPE_P (template_type))
7708 t = make_class_type (TREE_CODE (template_type));
7709 CLASSTYPE_DECLARED_CLASS (t)
7710 = CLASSTYPE_DECLARED_CLASS (template_type);
7711 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7712 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7714 /* A local class. Make sure the decl gets registered properly. */
7715 if (context == current_function_decl)
7716 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7718 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7719 /* This instantiation is another name for the primary
7720 template type. Set the TYPE_CANONICAL field
7721 appropriately. */
7722 TYPE_CANONICAL (t) = template_type;
7723 else if (any_template_arguments_need_structural_equality_p (arglist))
7724 /* Some of the template arguments require structural
7725 equality testing, so this template class requires
7726 structural equality testing. */
7727 SET_TYPE_STRUCTURAL_EQUALITY (t);
7729 else
7730 gcc_unreachable ();
7732 /* If we called start_enum or pushtag above, this information
7733 will already be set up. */
7734 if (!TYPE_NAME (t))
7736 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7738 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7739 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7740 DECL_SOURCE_LOCATION (type_decl)
7741 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7743 else
7744 type_decl = TYPE_NAME (t);
7746 if (CLASS_TYPE_P (template_type))
7748 TREE_PRIVATE (type_decl)
7749 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7750 TREE_PROTECTED (type_decl)
7751 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7752 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7754 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7755 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7759 /* Let's consider the explicit specialization of a member
7760 of a class template specialization that is implicitly instantiated,
7761 e.g.:
7762 template<class T>
7763 struct S
7765 template<class U> struct M {}; //#0
7768 template<>
7769 template<>
7770 struct S<int>::M<char> //#1
7772 int i;
7774 [temp.expl.spec]/4 says this is valid.
7776 In this case, when we write:
7777 S<int>::M<char> m;
7779 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7780 the one of #0.
7782 When we encounter #1, we want to store the partial instantiation
7783 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7785 For all cases other than this "explicit specialization of member of a
7786 class template", we just want to store the most general template into
7787 the CLASSTYPE_TI_TEMPLATE of M.
7789 This case of "explicit specialization of member of a class template"
7790 only happens when:
7791 1/ the enclosing class is an instantiation of, and therefore not
7792 the same as, the context of the most general template, and
7793 2/ we aren't looking at the partial instantiation itself, i.e.
7794 the innermost arguments are not the same as the innermost parms of
7795 the most general template.
7797 So it's only when 1/ and 2/ happens that we want to use the partial
7798 instantiation of the member template in lieu of its most general
7799 template. */
7801 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7802 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7803 /* the enclosing class must be an instantiation... */
7804 && CLASS_TYPE_P (context)
7805 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7807 tree partial_inst_args;
7808 TREE_VEC_LENGTH (arglist)--;
7809 ++processing_template_decl;
7810 partial_inst_args =
7811 tsubst (INNERMOST_TEMPLATE_ARGS
7812 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7813 arglist, complain, NULL_TREE);
7814 --processing_template_decl;
7815 TREE_VEC_LENGTH (arglist)++;
7816 use_partial_inst_tmpl =
7817 /*...and we must not be looking at the partial instantiation
7818 itself. */
7819 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7820 partial_inst_args);
7823 if (!use_partial_inst_tmpl)
7824 /* This case is easy; there are no member templates involved. */
7825 found = gen_tmpl;
7826 else
7828 /* This is a full instantiation of a member template. Find
7829 the partial instantiation of which this is an instance. */
7831 /* Temporarily reduce by one the number of levels in the ARGLIST
7832 so as to avoid comparing the last set of arguments. */
7833 TREE_VEC_LENGTH (arglist)--;
7834 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7835 TREE_VEC_LENGTH (arglist)++;
7836 /* FOUND is either a proper class type, or an alias
7837 template specialization. In the later case, it's a
7838 TYPE_DECL, resulting from the substituting of arguments
7839 for parameters in the TYPE_DECL of the alias template
7840 done earlier. So be careful while getting the template
7841 of FOUND. */
7842 found = TREE_CODE (found) == TYPE_DECL
7843 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7844 : CLASSTYPE_TI_TEMPLATE (found);
7847 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7849 elt.spec = t;
7850 slot = htab_find_slot_with_hash (type_specializations,
7851 &elt, hash, INSERT);
7852 entry = ggc_alloc_spec_entry ();
7853 *entry = elt;
7854 *slot = entry;
7856 /* Note this use of the partial instantiation so we can check it
7857 later in maybe_process_partial_specialization. */
7858 DECL_TEMPLATE_INSTANTIATIONS (found)
7859 = tree_cons (arglist, t,
7860 DECL_TEMPLATE_INSTANTIATIONS (found));
7862 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7863 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7864 /* Now that the type has been registered on the instantiations
7865 list, we set up the enumerators. Because the enumeration
7866 constants may involve the enumeration type itself, we make
7867 sure to register the type first, and then create the
7868 constants. That way, doing tsubst_expr for the enumeration
7869 constants won't result in recursive calls here; we'll find
7870 the instantiation and exit above. */
7871 tsubst_enum (template_type, t, arglist);
7873 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7874 /* If the type makes use of template parameters, the
7875 code that generates debugging information will crash. */
7876 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7878 /* Possibly limit visibility based on template args. */
7879 TREE_PUBLIC (type_decl) = 1;
7880 determine_visibility (type_decl);
7882 return t;
7886 /* Wrapper for lookup_template_class_1. */
7888 tree
7889 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7890 int entering_scope, tsubst_flags_t complain)
7892 tree ret;
7893 timevar_push (TV_TEMPLATE_INST);
7894 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7895 entering_scope, complain);
7896 timevar_pop (TV_TEMPLATE_INST);
7897 return ret;
7900 struct pair_fn_data
7902 tree_fn_t fn;
7903 void *data;
7904 /* True when we should also visit template parameters that occur in
7905 non-deduced contexts. */
7906 bool include_nondeduced_p;
7907 struct pointer_set_t *visited;
7910 /* Called from for_each_template_parm via walk_tree. */
7912 static tree
7913 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7915 tree t = *tp;
7916 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7917 tree_fn_t fn = pfd->fn;
7918 void *data = pfd->data;
7920 if (TYPE_P (t)
7921 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7922 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7923 pfd->include_nondeduced_p))
7924 return error_mark_node;
7926 switch (TREE_CODE (t))
7928 case RECORD_TYPE:
7929 if (TYPE_PTRMEMFUNC_P (t))
7930 break;
7931 /* Fall through. */
7933 case UNION_TYPE:
7934 case ENUMERAL_TYPE:
7935 if (!TYPE_TEMPLATE_INFO (t))
7936 *walk_subtrees = 0;
7937 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7938 fn, data, pfd->visited,
7939 pfd->include_nondeduced_p))
7940 return error_mark_node;
7941 break;
7943 case INTEGER_TYPE:
7944 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7945 fn, data, pfd->visited,
7946 pfd->include_nondeduced_p)
7947 || for_each_template_parm (TYPE_MAX_VALUE (t),
7948 fn, data, pfd->visited,
7949 pfd->include_nondeduced_p))
7950 return error_mark_node;
7951 break;
7953 case METHOD_TYPE:
7954 /* Since we're not going to walk subtrees, we have to do this
7955 explicitly here. */
7956 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7957 pfd->visited, pfd->include_nondeduced_p))
7958 return error_mark_node;
7959 /* Fall through. */
7961 case FUNCTION_TYPE:
7962 /* Check the return type. */
7963 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7964 pfd->include_nondeduced_p))
7965 return error_mark_node;
7967 /* Check the parameter types. Since default arguments are not
7968 instantiated until they are needed, the TYPE_ARG_TYPES may
7969 contain expressions that involve template parameters. But,
7970 no-one should be looking at them yet. And, once they're
7971 instantiated, they don't contain template parameters, so
7972 there's no point in looking at them then, either. */
7974 tree parm;
7976 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7977 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7978 pfd->visited, pfd->include_nondeduced_p))
7979 return error_mark_node;
7981 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7982 want walk_tree walking into them itself. */
7983 *walk_subtrees = 0;
7985 break;
7987 case TYPEOF_TYPE:
7988 case UNDERLYING_TYPE:
7989 if (pfd->include_nondeduced_p
7990 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7991 pfd->visited,
7992 pfd->include_nondeduced_p))
7993 return error_mark_node;
7994 break;
7996 case FUNCTION_DECL:
7997 case VAR_DECL:
7998 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7999 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8000 pfd->visited, pfd->include_nondeduced_p))
8001 return error_mark_node;
8002 /* Fall through. */
8004 case PARM_DECL:
8005 case CONST_DECL:
8006 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8007 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8008 pfd->visited, pfd->include_nondeduced_p))
8009 return error_mark_node;
8010 if (DECL_CONTEXT (t)
8011 && pfd->include_nondeduced_p
8012 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8013 pfd->visited, pfd->include_nondeduced_p))
8014 return error_mark_node;
8015 break;
8017 case BOUND_TEMPLATE_TEMPLATE_PARM:
8018 /* Record template parameters such as `T' inside `TT<T>'. */
8019 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8020 pfd->include_nondeduced_p))
8021 return error_mark_node;
8022 /* Fall through. */
8024 case TEMPLATE_TEMPLATE_PARM:
8025 case TEMPLATE_TYPE_PARM:
8026 case TEMPLATE_PARM_INDEX:
8027 if (fn && (*fn)(t, data))
8028 return error_mark_node;
8029 else if (!fn)
8030 return error_mark_node;
8031 break;
8033 case TEMPLATE_DECL:
8034 /* A template template parameter is encountered. */
8035 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8036 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8037 pfd->include_nondeduced_p))
8038 return error_mark_node;
8040 /* Already substituted template template parameter */
8041 *walk_subtrees = 0;
8042 break;
8044 case TYPENAME_TYPE:
8045 if (!fn
8046 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8047 data, pfd->visited,
8048 pfd->include_nondeduced_p))
8049 return error_mark_node;
8050 break;
8052 case CONSTRUCTOR:
8053 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8054 && pfd->include_nondeduced_p
8055 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8056 (TREE_TYPE (t)), fn, data,
8057 pfd->visited, pfd->include_nondeduced_p))
8058 return error_mark_node;
8059 break;
8061 case INDIRECT_REF:
8062 case COMPONENT_REF:
8063 /* If there's no type, then this thing must be some expression
8064 involving template parameters. */
8065 if (!fn && !TREE_TYPE (t))
8066 return error_mark_node;
8067 break;
8069 case MODOP_EXPR:
8070 case CAST_EXPR:
8071 case IMPLICIT_CONV_EXPR:
8072 case REINTERPRET_CAST_EXPR:
8073 case CONST_CAST_EXPR:
8074 case STATIC_CAST_EXPR:
8075 case DYNAMIC_CAST_EXPR:
8076 case ARROW_EXPR:
8077 case DOTSTAR_EXPR:
8078 case TYPEID_EXPR:
8079 case PSEUDO_DTOR_EXPR:
8080 if (!fn)
8081 return error_mark_node;
8082 break;
8084 default:
8085 break;
8088 /* We didn't find any template parameters we liked. */
8089 return NULL_TREE;
8092 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8093 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8094 call FN with the parameter and the DATA.
8095 If FN returns nonzero, the iteration is terminated, and
8096 for_each_template_parm returns 1. Otherwise, the iteration
8097 continues. If FN never returns a nonzero value, the value
8098 returned by for_each_template_parm is 0. If FN is NULL, it is
8099 considered to be the function which always returns 1.
8101 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8102 parameters that occur in non-deduced contexts. When false, only
8103 visits those template parameters that can be deduced. */
8105 static int
8106 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8107 struct pointer_set_t *visited,
8108 bool include_nondeduced_p)
8110 struct pair_fn_data pfd;
8111 int result;
8113 /* Set up. */
8114 pfd.fn = fn;
8115 pfd.data = data;
8116 pfd.include_nondeduced_p = include_nondeduced_p;
8118 /* Walk the tree. (Conceptually, we would like to walk without
8119 duplicates, but for_each_template_parm_r recursively calls
8120 for_each_template_parm, so we would need to reorganize a fair
8121 bit to use walk_tree_without_duplicates, so we keep our own
8122 visited list.) */
8123 if (visited)
8124 pfd.visited = visited;
8125 else
8126 pfd.visited = pointer_set_create ();
8127 result = cp_walk_tree (&t,
8128 for_each_template_parm_r,
8129 &pfd,
8130 pfd.visited) != NULL_TREE;
8132 /* Clean up. */
8133 if (!visited)
8135 pointer_set_destroy (pfd.visited);
8136 pfd.visited = 0;
8139 return result;
8142 /* Returns true if T depends on any template parameter. */
8145 uses_template_parms (tree t)
8147 bool dependent_p;
8148 int saved_processing_template_decl;
8150 saved_processing_template_decl = processing_template_decl;
8151 if (!saved_processing_template_decl)
8152 processing_template_decl = 1;
8153 if (TYPE_P (t))
8154 dependent_p = dependent_type_p (t);
8155 else if (TREE_CODE (t) == TREE_VEC)
8156 dependent_p = any_dependent_template_arguments_p (t);
8157 else if (TREE_CODE (t) == TREE_LIST)
8158 dependent_p = (uses_template_parms (TREE_VALUE (t))
8159 || uses_template_parms (TREE_CHAIN (t)));
8160 else if (TREE_CODE (t) == TYPE_DECL)
8161 dependent_p = dependent_type_p (TREE_TYPE (t));
8162 else if (DECL_P (t)
8163 || EXPR_P (t)
8164 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8165 || TREE_CODE (t) == OVERLOAD
8166 || BASELINK_P (t)
8167 || identifier_p (t)
8168 || TREE_CODE (t) == TRAIT_EXPR
8169 || TREE_CODE (t) == CONSTRUCTOR
8170 || CONSTANT_CLASS_P (t))
8171 dependent_p = (type_dependent_expression_p (t)
8172 || value_dependent_expression_p (t));
8173 else
8175 gcc_assert (t == error_mark_node);
8176 dependent_p = false;
8179 processing_template_decl = saved_processing_template_decl;
8181 return dependent_p;
8184 /* Returns true iff current_function_decl is an incompletely instantiated
8185 template. Useful instead of processing_template_decl because the latter
8186 is set to 0 during fold_non_dependent_expr. */
8188 bool
8189 in_template_function (void)
8191 tree fn = current_function_decl;
8192 bool ret;
8193 ++processing_template_decl;
8194 ret = (fn && DECL_LANG_SPECIFIC (fn)
8195 && DECL_TEMPLATE_INFO (fn)
8196 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8197 --processing_template_decl;
8198 return ret;
8201 /* Returns true if T depends on any template parameter with level LEVEL. */
8204 uses_template_parms_level (tree t, int level)
8206 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8207 /*include_nondeduced_p=*/true);
8210 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8211 ill-formed translation unit, i.e. a variable or function that isn't
8212 usable in a constant expression. */
8214 static inline bool
8215 neglectable_inst_p (tree d)
8217 return (DECL_P (d)
8218 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8219 : decl_maybe_constant_var_p (d)));
8222 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8223 neglectable and instantiated from within an erroneous instantiation. */
8225 static bool
8226 limit_bad_template_recursion (tree decl)
8228 struct tinst_level *lev = current_tinst_level;
8229 int errs = errorcount + sorrycount;
8230 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8231 return false;
8233 for (; lev; lev = lev->next)
8234 if (neglectable_inst_p (lev->decl))
8235 break;
8237 return (lev && errs > lev->errors);
8240 static int tinst_depth;
8241 extern int max_tinst_depth;
8242 int depth_reached;
8244 static GTY(()) struct tinst_level *last_error_tinst_level;
8246 /* We're starting to instantiate D; record the template instantiation context
8247 for diagnostics and to restore it later. */
8250 push_tinst_level (tree d)
8252 struct tinst_level *new_level;
8254 if (tinst_depth >= max_tinst_depth)
8256 last_error_tinst_level = current_tinst_level;
8257 if (TREE_CODE (d) == TREE_LIST)
8258 error ("template instantiation depth exceeds maximum of %d (use "
8259 "-ftemplate-depth= to increase the maximum) substituting %qS",
8260 max_tinst_depth, d);
8261 else
8262 error ("template instantiation depth exceeds maximum of %d (use "
8263 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8264 max_tinst_depth, d);
8266 print_instantiation_context ();
8268 return 0;
8271 /* If the current instantiation caused problems, don't let it instantiate
8272 anything else. Do allow deduction substitution and decls usable in
8273 constant expressions. */
8274 if (limit_bad_template_recursion (d))
8275 return 0;
8277 new_level = ggc_alloc_tinst_level ();
8278 new_level->decl = d;
8279 new_level->locus = input_location;
8280 new_level->errors = errorcount+sorrycount;
8281 new_level->in_system_header_p = in_system_header_at (input_location);
8282 new_level->next = current_tinst_level;
8283 current_tinst_level = new_level;
8285 ++tinst_depth;
8286 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8287 depth_reached = tinst_depth;
8289 return 1;
8292 /* We're done instantiating this template; return to the instantiation
8293 context. */
8295 void
8296 pop_tinst_level (void)
8298 /* Restore the filename and line number stashed away when we started
8299 this instantiation. */
8300 input_location = current_tinst_level->locus;
8301 current_tinst_level = current_tinst_level->next;
8302 --tinst_depth;
8305 /* We're instantiating a deferred template; restore the template
8306 instantiation context in which the instantiation was requested, which
8307 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8309 static tree
8310 reopen_tinst_level (struct tinst_level *level)
8312 struct tinst_level *t;
8314 tinst_depth = 0;
8315 for (t = level; t; t = t->next)
8316 ++tinst_depth;
8318 current_tinst_level = level;
8319 pop_tinst_level ();
8320 if (current_tinst_level)
8321 current_tinst_level->errors = errorcount+sorrycount;
8322 return level->decl;
8325 /* Returns the TINST_LEVEL which gives the original instantiation
8326 context. */
8328 struct tinst_level *
8329 outermost_tinst_level (void)
8331 struct tinst_level *level = current_tinst_level;
8332 if (level)
8333 while (level->next)
8334 level = level->next;
8335 return level;
8338 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8339 vector of template arguments, as for tsubst.
8341 Returns an appropriate tsubst'd friend declaration. */
8343 static tree
8344 tsubst_friend_function (tree decl, tree args)
8346 tree new_friend;
8348 if (TREE_CODE (decl) == FUNCTION_DECL
8349 && DECL_TEMPLATE_INSTANTIATION (decl)
8350 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8351 /* This was a friend declared with an explicit template
8352 argument list, e.g.:
8354 friend void f<>(T);
8356 to indicate that f was a template instantiation, not a new
8357 function declaration. Now, we have to figure out what
8358 instantiation of what template. */
8360 tree template_id, arglist, fns;
8361 tree new_args;
8362 tree tmpl;
8363 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8365 /* Friend functions are looked up in the containing namespace scope.
8366 We must enter that scope, to avoid finding member functions of the
8367 current class with same name. */
8368 push_nested_namespace (ns);
8369 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8370 tf_warning_or_error, NULL_TREE,
8371 /*integral_constant_expression_p=*/false);
8372 pop_nested_namespace (ns);
8373 arglist = tsubst (DECL_TI_ARGS (decl), args,
8374 tf_warning_or_error, NULL_TREE);
8375 template_id = lookup_template_function (fns, arglist);
8377 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8378 tmpl = determine_specialization (template_id, new_friend,
8379 &new_args,
8380 /*need_member_template=*/0,
8381 TREE_VEC_LENGTH (args),
8382 tsk_none);
8383 return instantiate_template (tmpl, new_args, tf_error);
8386 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8388 /* The NEW_FRIEND will look like an instantiation, to the
8389 compiler, but is not an instantiation from the point of view of
8390 the language. For example, we might have had:
8392 template <class T> struct S {
8393 template <class U> friend void f(T, U);
8396 Then, in S<int>, template <class U> void f(int, U) is not an
8397 instantiation of anything. */
8398 if (new_friend == error_mark_node)
8399 return error_mark_node;
8401 DECL_USE_TEMPLATE (new_friend) = 0;
8402 if (TREE_CODE (decl) == TEMPLATE_DECL)
8404 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8405 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8406 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8409 /* The mangled name for the NEW_FRIEND is incorrect. The function
8410 is not a template instantiation and should not be mangled like
8411 one. Therefore, we forget the mangling here; we'll recompute it
8412 later if we need it. */
8413 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8415 SET_DECL_RTL (new_friend, NULL);
8416 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8419 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8421 tree old_decl;
8422 tree new_friend_template_info;
8423 tree new_friend_result_template_info;
8424 tree ns;
8425 int new_friend_is_defn;
8427 /* We must save some information from NEW_FRIEND before calling
8428 duplicate decls since that function will free NEW_FRIEND if
8429 possible. */
8430 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8431 new_friend_is_defn =
8432 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8433 (template_for_substitution (new_friend)))
8434 != NULL_TREE);
8435 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8437 /* This declaration is a `primary' template. */
8438 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8440 new_friend_result_template_info
8441 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8443 else
8444 new_friend_result_template_info = NULL_TREE;
8446 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8447 if (new_friend_is_defn)
8448 DECL_INITIAL (new_friend) = error_mark_node;
8450 /* Inside pushdecl_namespace_level, we will push into the
8451 current namespace. However, the friend function should go
8452 into the namespace of the template. */
8453 ns = decl_namespace_context (new_friend);
8454 push_nested_namespace (ns);
8455 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8456 pop_nested_namespace (ns);
8458 if (old_decl == error_mark_node)
8459 return error_mark_node;
8461 if (old_decl != new_friend)
8463 /* This new friend declaration matched an existing
8464 declaration. For example, given:
8466 template <class T> void f(T);
8467 template <class U> class C {
8468 template <class T> friend void f(T) {}
8471 the friend declaration actually provides the definition
8472 of `f', once C has been instantiated for some type. So,
8473 old_decl will be the out-of-class template declaration,
8474 while new_friend is the in-class definition.
8476 But, if `f' was called before this point, the
8477 instantiation of `f' will have DECL_TI_ARGS corresponding
8478 to `T' but not to `U', references to which might appear
8479 in the definition of `f'. Previously, the most general
8480 template for an instantiation of `f' was the out-of-class
8481 version; now it is the in-class version. Therefore, we
8482 run through all specialization of `f', adding to their
8483 DECL_TI_ARGS appropriately. In particular, they need a
8484 new set of outer arguments, corresponding to the
8485 arguments for this class instantiation.
8487 The same situation can arise with something like this:
8489 friend void f(int);
8490 template <class T> class C {
8491 friend void f(T) {}
8494 when `C<int>' is instantiated. Now, `f(int)' is defined
8495 in the class. */
8497 if (!new_friend_is_defn)
8498 /* On the other hand, if the in-class declaration does
8499 *not* provide a definition, then we don't want to alter
8500 existing definitions. We can just leave everything
8501 alone. */
8503 else
8505 tree new_template = TI_TEMPLATE (new_friend_template_info);
8506 tree new_args = TI_ARGS (new_friend_template_info);
8508 /* Overwrite whatever template info was there before, if
8509 any, with the new template information pertaining to
8510 the declaration. */
8511 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8513 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8515 /* We should have called reregister_specialization in
8516 duplicate_decls. */
8517 gcc_assert (retrieve_specialization (new_template,
8518 new_args, 0)
8519 == old_decl);
8521 /* Instantiate it if the global has already been used. */
8522 if (DECL_ODR_USED (old_decl))
8523 instantiate_decl (old_decl, /*defer_ok=*/true,
8524 /*expl_inst_class_mem_p=*/false);
8526 else
8528 tree t;
8530 /* Indicate that the old function template is a partial
8531 instantiation. */
8532 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8533 = new_friend_result_template_info;
8535 gcc_assert (new_template
8536 == most_general_template (new_template));
8537 gcc_assert (new_template != old_decl);
8539 /* Reassign any specializations already in the hash table
8540 to the new more general template, and add the
8541 additional template args. */
8542 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8543 t != NULL_TREE;
8544 t = TREE_CHAIN (t))
8546 tree spec = TREE_VALUE (t);
8547 spec_entry elt;
8549 elt.tmpl = old_decl;
8550 elt.args = DECL_TI_ARGS (spec);
8551 elt.spec = NULL_TREE;
8553 htab_remove_elt (decl_specializations, &elt);
8555 DECL_TI_ARGS (spec)
8556 = add_outermost_template_args (new_args,
8557 DECL_TI_ARGS (spec));
8559 register_specialization
8560 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8563 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8567 /* The information from NEW_FRIEND has been merged into OLD_DECL
8568 by duplicate_decls. */
8569 new_friend = old_decl;
8572 else
8574 tree context = DECL_CONTEXT (new_friend);
8575 bool dependent_p;
8577 /* In the code
8578 template <class T> class C {
8579 template <class U> friend void C1<U>::f (); // case 1
8580 friend void C2<T>::f (); // case 2
8582 we only need to make sure CONTEXT is a complete type for
8583 case 2. To distinguish between the two cases, we note that
8584 CONTEXT of case 1 remains dependent type after tsubst while
8585 this isn't true for case 2. */
8586 ++processing_template_decl;
8587 dependent_p = dependent_type_p (context);
8588 --processing_template_decl;
8590 if (!dependent_p
8591 && !complete_type_or_else (context, NULL_TREE))
8592 return error_mark_node;
8594 if (COMPLETE_TYPE_P (context))
8596 tree fn = new_friend;
8597 /* do_friend adds the TEMPLATE_DECL for any member friend
8598 template even if it isn't a member template, i.e.
8599 template <class T> friend A<T>::f();
8600 Look through it in that case. */
8601 if (TREE_CODE (fn) == TEMPLATE_DECL
8602 && !PRIMARY_TEMPLATE_P (fn))
8603 fn = DECL_TEMPLATE_RESULT (fn);
8604 /* Check to see that the declaration is really present, and,
8605 possibly obtain an improved declaration. */
8606 fn = check_classfn (context, fn, NULL_TREE);
8608 if (fn)
8609 new_friend = fn;
8613 return new_friend;
8616 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8617 template arguments, as for tsubst.
8619 Returns an appropriate tsubst'd friend type or error_mark_node on
8620 failure. */
8622 static tree
8623 tsubst_friend_class (tree friend_tmpl, tree args)
8625 tree friend_type;
8626 tree tmpl;
8627 tree context;
8629 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8631 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8632 return TREE_TYPE (t);
8635 context = CP_DECL_CONTEXT (friend_tmpl);
8637 if (context != global_namespace)
8639 if (TREE_CODE (context) == NAMESPACE_DECL)
8640 push_nested_namespace (context);
8641 else
8642 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8645 /* Look for a class template declaration. We look for hidden names
8646 because two friend declarations of the same template are the
8647 same. For example, in:
8649 struct A {
8650 template <typename> friend class F;
8652 template <typename> struct B {
8653 template <typename> friend class F;
8656 both F templates are the same. */
8657 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8658 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8660 /* But, if we don't find one, it might be because we're in a
8661 situation like this:
8663 template <class T>
8664 struct S {
8665 template <class U>
8666 friend struct S;
8669 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8670 for `S<int>', not the TEMPLATE_DECL. */
8671 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8673 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8674 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8677 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8679 /* The friend template has already been declared. Just
8680 check to see that the declarations match, and install any new
8681 default parameters. We must tsubst the default parameters,
8682 of course. We only need the innermost template parameters
8683 because that is all that redeclare_class_template will look
8684 at. */
8685 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8686 > TMPL_ARGS_DEPTH (args))
8688 tree parms;
8689 location_t saved_input_location;
8690 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8691 args, tf_warning_or_error);
8693 saved_input_location = input_location;
8694 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8695 redeclare_class_template (TREE_TYPE (tmpl), parms);
8696 input_location = saved_input_location;
8700 friend_type = TREE_TYPE (tmpl);
8702 else
8704 /* The friend template has not already been declared. In this
8705 case, the instantiation of the template class will cause the
8706 injection of this template into the global scope. */
8707 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8708 if (tmpl == error_mark_node)
8709 return error_mark_node;
8711 /* The new TMPL is not an instantiation of anything, so we
8712 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8713 the new type because that is supposed to be the corresponding
8714 template decl, i.e., TMPL. */
8715 DECL_USE_TEMPLATE (tmpl) = 0;
8716 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8717 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8718 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8719 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8721 /* Inject this template into the global scope. */
8722 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8725 if (context != global_namespace)
8727 if (TREE_CODE (context) == NAMESPACE_DECL)
8728 pop_nested_namespace (context);
8729 else
8730 pop_nested_class ();
8733 return friend_type;
8736 /* Returns zero if TYPE cannot be completed later due to circularity.
8737 Otherwise returns one. */
8739 static int
8740 can_complete_type_without_circularity (tree type)
8742 if (type == NULL_TREE || type == error_mark_node)
8743 return 0;
8744 else if (COMPLETE_TYPE_P (type))
8745 return 1;
8746 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8747 return can_complete_type_without_circularity (TREE_TYPE (type));
8748 else if (CLASS_TYPE_P (type)
8749 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8750 return 0;
8751 else
8752 return 1;
8755 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8757 /* Apply any attributes which had to be deferred until instantiation
8758 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8759 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8761 static void
8762 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8763 tree args, tsubst_flags_t complain, tree in_decl)
8765 tree last_dep = NULL_TREE;
8766 tree t;
8767 tree *p;
8769 for (t = attributes; t; t = TREE_CHAIN (t))
8770 if (ATTR_IS_DEPENDENT (t))
8772 last_dep = t;
8773 attributes = copy_list (attributes);
8774 break;
8777 if (DECL_P (*decl_p))
8779 if (TREE_TYPE (*decl_p) == error_mark_node)
8780 return;
8781 p = &DECL_ATTRIBUTES (*decl_p);
8783 else
8784 p = &TYPE_ATTRIBUTES (*decl_p);
8786 if (last_dep)
8788 tree late_attrs = NULL_TREE;
8789 tree *q = &late_attrs;
8791 for (*p = attributes; *p; )
8793 t = *p;
8794 if (ATTR_IS_DEPENDENT (t))
8796 *p = TREE_CHAIN (t);
8797 TREE_CHAIN (t) = NULL_TREE;
8798 if ((flag_openmp || flag_cilkplus)
8799 && is_attribute_p ("omp declare simd",
8800 get_attribute_name (t))
8801 && TREE_VALUE (t))
8803 tree clauses = TREE_VALUE (TREE_VALUE (t));
8804 clauses = tsubst_omp_clauses (clauses, true, args,
8805 complain, in_decl);
8806 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8807 clauses = finish_omp_clauses (clauses);
8808 tree parms = DECL_ARGUMENTS (*decl_p);
8809 clauses
8810 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8811 if (clauses)
8812 TREE_VALUE (TREE_VALUE (t)) = clauses;
8813 else
8814 TREE_VALUE (t) = NULL_TREE;
8816 /* If the first attribute argument is an identifier, don't
8817 pass it through tsubst. Attributes like mode, format,
8818 cleanup and several target specific attributes expect it
8819 unmodified. */
8820 else if (attribute_takes_identifier_p (get_attribute_name (t))
8821 && TREE_VALUE (t))
8823 tree chain
8824 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8825 in_decl,
8826 /*integral_constant_expression_p=*/false);
8827 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8828 TREE_VALUE (t)
8829 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8830 chain);
8832 else
8833 TREE_VALUE (t)
8834 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8835 /*integral_constant_expression_p=*/false);
8836 *q = t;
8837 q = &TREE_CHAIN (t);
8839 else
8840 p = &TREE_CHAIN (t);
8843 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8847 /* Perform (or defer) access check for typedefs that were referenced
8848 from within the template TMPL code.
8849 This is a subroutine of instantiate_decl and instantiate_class_template.
8850 TMPL is the template to consider and TARGS is the list of arguments of
8851 that template. */
8853 static void
8854 perform_typedefs_access_check (tree tmpl, tree targs)
8856 location_t saved_location;
8857 unsigned i;
8858 qualified_typedef_usage_t *iter;
8860 if (!tmpl
8861 || (!CLASS_TYPE_P (tmpl)
8862 && TREE_CODE (tmpl) != FUNCTION_DECL))
8863 return;
8865 saved_location = input_location;
8866 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8868 tree type_decl = iter->typedef_decl;
8869 tree type_scope = iter->context;
8871 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8872 continue;
8874 if (uses_template_parms (type_decl))
8875 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8876 if (uses_template_parms (type_scope))
8877 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8879 /* Make access check error messages point to the location
8880 of the use of the typedef. */
8881 input_location = iter->locus;
8882 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8883 type_decl, type_decl,
8884 tf_warning_or_error);
8886 input_location = saved_location;
8889 static tree
8890 instantiate_class_template_1 (tree type)
8892 tree templ, args, pattern, t, member;
8893 tree typedecl;
8894 tree pbinfo;
8895 tree base_list;
8896 unsigned int saved_maximum_field_alignment;
8897 tree fn_context;
8899 if (type == error_mark_node)
8900 return error_mark_node;
8902 if (COMPLETE_OR_OPEN_TYPE_P (type)
8903 || uses_template_parms (type))
8904 return type;
8906 /* Figure out which template is being instantiated. */
8907 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8908 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8910 /* Determine what specialization of the original template to
8911 instantiate. */
8912 t = most_specialized_class (type, tf_warning_or_error);
8913 if (t == error_mark_node)
8915 TYPE_BEING_DEFINED (type) = 1;
8916 return error_mark_node;
8918 else if (t)
8920 /* This TYPE is actually an instantiation of a partial
8921 specialization. We replace the innermost set of ARGS with
8922 the arguments appropriate for substitution. For example,
8923 given:
8925 template <class T> struct S {};
8926 template <class T> struct S<T*> {};
8928 and supposing that we are instantiating S<int*>, ARGS will
8929 presently be {int*} -- but we need {int}. */
8930 pattern = TREE_TYPE (t);
8931 args = TREE_PURPOSE (t);
8933 else
8935 pattern = TREE_TYPE (templ);
8936 args = CLASSTYPE_TI_ARGS (type);
8939 /* If the template we're instantiating is incomplete, then clearly
8940 there's nothing we can do. */
8941 if (!COMPLETE_TYPE_P (pattern))
8942 return type;
8944 /* If we've recursively instantiated too many templates, stop. */
8945 if (! push_tinst_level (type))
8946 return type;
8948 /* Now we're really doing the instantiation. Mark the type as in
8949 the process of being defined. */
8950 TYPE_BEING_DEFINED (type) = 1;
8952 /* We may be in the middle of deferred access check. Disable
8953 it now. */
8954 push_deferring_access_checks (dk_no_deferred);
8956 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8957 if (!fn_context)
8958 push_to_top_level ();
8959 /* Use #pragma pack from the template context. */
8960 saved_maximum_field_alignment = maximum_field_alignment;
8961 maximum_field_alignment = TYPE_PRECISION (pattern);
8963 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8965 /* Set the input location to the most specialized template definition.
8966 This is needed if tsubsting causes an error. */
8967 typedecl = TYPE_MAIN_DECL (pattern);
8968 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8969 DECL_SOURCE_LOCATION (typedecl);
8971 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8972 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8973 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8974 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8975 if (ANON_AGGR_TYPE_P (pattern))
8976 SET_ANON_AGGR_TYPE_P (type);
8977 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8979 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8980 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8981 /* Adjust visibility for template arguments. */
8982 determine_visibility (TYPE_MAIN_DECL (type));
8984 if (CLASS_TYPE_P (type))
8985 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8987 pbinfo = TYPE_BINFO (pattern);
8989 /* We should never instantiate a nested class before its enclosing
8990 class; we need to look up the nested class by name before we can
8991 instantiate it, and that lookup should instantiate the enclosing
8992 class. */
8993 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8994 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8996 base_list = NULL_TREE;
8997 if (BINFO_N_BASE_BINFOS (pbinfo))
8999 tree pbase_binfo;
9000 tree pushed_scope;
9001 int i;
9003 /* We must enter the scope containing the type, as that is where
9004 the accessibility of types named in dependent bases are
9005 looked up from. */
9006 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9008 /* Substitute into each of the bases to determine the actual
9009 basetypes. */
9010 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9012 tree base;
9013 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9014 tree expanded_bases = NULL_TREE;
9015 int idx, len = 1;
9017 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9019 expanded_bases =
9020 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9021 args, tf_error, NULL_TREE);
9022 if (expanded_bases == error_mark_node)
9023 continue;
9025 len = TREE_VEC_LENGTH (expanded_bases);
9028 for (idx = 0; idx < len; idx++)
9030 if (expanded_bases)
9031 /* Extract the already-expanded base class. */
9032 base = TREE_VEC_ELT (expanded_bases, idx);
9033 else
9034 /* Substitute to figure out the base class. */
9035 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9036 NULL_TREE);
9038 if (base == error_mark_node)
9039 continue;
9041 base_list = tree_cons (access, base, base_list);
9042 if (BINFO_VIRTUAL_P (pbase_binfo))
9043 TREE_TYPE (base_list) = integer_type_node;
9047 /* The list is now in reverse order; correct that. */
9048 base_list = nreverse (base_list);
9050 if (pushed_scope)
9051 pop_scope (pushed_scope);
9053 /* Now call xref_basetypes to set up all the base-class
9054 information. */
9055 xref_basetypes (type, base_list);
9057 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9058 (int) ATTR_FLAG_TYPE_IN_PLACE,
9059 args, tf_error, NULL_TREE);
9060 fixup_attribute_variants (type);
9062 /* Now that our base classes are set up, enter the scope of the
9063 class, so that name lookups into base classes, etc. will work
9064 correctly. This is precisely analogous to what we do in
9065 begin_class_definition when defining an ordinary non-template
9066 class, except we also need to push the enclosing classes. */
9067 push_nested_class (type);
9069 /* Now members are processed in the order of declaration. */
9070 for (member = CLASSTYPE_DECL_LIST (pattern);
9071 member; member = TREE_CHAIN (member))
9073 tree t = TREE_VALUE (member);
9075 if (TREE_PURPOSE (member))
9077 if (TYPE_P (t))
9079 /* Build new CLASSTYPE_NESTED_UTDS. */
9081 tree newtag;
9082 bool class_template_p;
9084 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9085 && TYPE_LANG_SPECIFIC (t)
9086 && CLASSTYPE_IS_TEMPLATE (t));
9087 /* If the member is a class template, then -- even after
9088 substitution -- there may be dependent types in the
9089 template argument list for the class. We increment
9090 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9091 that function will assume that no types are dependent
9092 when outside of a template. */
9093 if (class_template_p)
9094 ++processing_template_decl;
9095 newtag = tsubst (t, args, tf_error, NULL_TREE);
9096 if (class_template_p)
9097 --processing_template_decl;
9098 if (newtag == error_mark_node)
9099 continue;
9101 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9103 tree name = TYPE_IDENTIFIER (t);
9105 if (class_template_p)
9106 /* Unfortunately, lookup_template_class sets
9107 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9108 instantiation (i.e., for the type of a member
9109 template class nested within a template class.)
9110 This behavior is required for
9111 maybe_process_partial_specialization to work
9112 correctly, but is not accurate in this case;
9113 the TAG is not an instantiation of anything.
9114 (The corresponding TEMPLATE_DECL is an
9115 instantiation, but the TYPE is not.) */
9116 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9118 /* Now, we call pushtag to put this NEWTAG into the scope of
9119 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9120 pushtag calling push_template_decl. We don't have to do
9121 this for enums because it will already have been done in
9122 tsubst_enum. */
9123 if (name)
9124 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9125 pushtag (name, newtag, /*tag_scope=*/ts_current);
9128 else if (DECL_DECLARES_FUNCTION_P (t))
9130 /* Build new TYPE_METHODS. */
9131 tree r;
9133 if (TREE_CODE (t) == TEMPLATE_DECL)
9134 ++processing_template_decl;
9135 r = tsubst (t, args, tf_error, NULL_TREE);
9136 if (TREE_CODE (t) == TEMPLATE_DECL)
9137 --processing_template_decl;
9138 set_current_access_from_decl (r);
9139 finish_member_declaration (r);
9140 /* Instantiate members marked with attribute used. */
9141 if (r != error_mark_node && DECL_PRESERVE_P (r))
9142 mark_used (r);
9143 if (TREE_CODE (r) == FUNCTION_DECL
9144 && DECL_OMP_DECLARE_REDUCTION_P (r))
9145 cp_check_omp_declare_reduction (r);
9147 else if (DECL_CLASS_TEMPLATE_P (t)
9148 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9149 /* A closure type for a lambda in a default argument for a
9150 member template. Ignore it; it will be instantiated with
9151 the default argument. */;
9152 else
9154 /* Build new TYPE_FIELDS. */
9155 if (TREE_CODE (t) == STATIC_ASSERT)
9157 tree condition;
9159 ++c_inhibit_evaluation_warnings;
9160 condition =
9161 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9162 tf_warning_or_error, NULL_TREE,
9163 /*integral_constant_expression_p=*/true);
9164 --c_inhibit_evaluation_warnings;
9166 finish_static_assert (condition,
9167 STATIC_ASSERT_MESSAGE (t),
9168 STATIC_ASSERT_SOURCE_LOCATION (t),
9169 /*member_p=*/true);
9171 else if (TREE_CODE (t) != CONST_DECL)
9173 tree r;
9174 tree vec = NULL_TREE;
9175 int len = 1;
9177 /* The file and line for this declaration, to
9178 assist in error message reporting. Since we
9179 called push_tinst_level above, we don't need to
9180 restore these. */
9181 input_location = DECL_SOURCE_LOCATION (t);
9183 if (TREE_CODE (t) == TEMPLATE_DECL)
9184 ++processing_template_decl;
9185 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9186 if (TREE_CODE (t) == TEMPLATE_DECL)
9187 --processing_template_decl;
9189 if (TREE_CODE (r) == TREE_VEC)
9191 /* A capture pack became multiple fields. */
9192 vec = r;
9193 len = TREE_VEC_LENGTH (vec);
9196 for (int i = 0; i < len; ++i)
9198 if (vec)
9199 r = TREE_VEC_ELT (vec, i);
9200 if (VAR_P (r))
9202 /* In [temp.inst]:
9204 [t]he initialization (and any associated
9205 side-effects) of a static data member does
9206 not occur unless the static data member is
9207 itself used in a way that requires the
9208 definition of the static data member to
9209 exist.
9211 Therefore, we do not substitute into the
9212 initialized for the static data member here. */
9213 finish_static_data_member_decl
9215 /*init=*/NULL_TREE,
9216 /*init_const_expr_p=*/false,
9217 /*asmspec_tree=*/NULL_TREE,
9218 /*flags=*/0);
9219 /* Instantiate members marked with attribute used. */
9220 if (r != error_mark_node && DECL_PRESERVE_P (r))
9221 mark_used (r);
9223 else if (TREE_CODE (r) == FIELD_DECL)
9225 /* Determine whether R has a valid type and can be
9226 completed later. If R is invalid, then its type
9227 is replaced by error_mark_node. */
9228 tree rtype = TREE_TYPE (r);
9229 if (can_complete_type_without_circularity (rtype))
9230 complete_type (rtype);
9232 if (!COMPLETE_TYPE_P (rtype))
9234 cxx_incomplete_type_error (r, rtype);
9235 TREE_TYPE (r) = error_mark_node;
9239 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9240 such a thing will already have been added to the field
9241 list by tsubst_enum in finish_member_declaration in the
9242 CLASSTYPE_NESTED_UTDS case above. */
9243 if (!(TREE_CODE (r) == TYPE_DECL
9244 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9245 && DECL_ARTIFICIAL (r)))
9247 set_current_access_from_decl (r);
9248 finish_member_declaration (r);
9254 else
9256 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9257 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9259 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9261 tree friend_type = t;
9262 bool adjust_processing_template_decl = false;
9264 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9266 /* template <class T> friend class C; */
9267 friend_type = tsubst_friend_class (friend_type, args);
9268 adjust_processing_template_decl = true;
9270 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9272 /* template <class T> friend class C::D; */
9273 friend_type = tsubst (friend_type, args,
9274 tf_warning_or_error, NULL_TREE);
9275 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9276 friend_type = TREE_TYPE (friend_type);
9277 adjust_processing_template_decl = true;
9279 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9280 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9282 /* This could be either
9284 friend class T::C;
9286 when dependent_type_p is false or
9288 template <class U> friend class T::C;
9290 otherwise. */
9291 friend_type = tsubst (friend_type, args,
9292 tf_warning_or_error, NULL_TREE);
9293 /* Bump processing_template_decl for correct
9294 dependent_type_p calculation. */
9295 ++processing_template_decl;
9296 if (dependent_type_p (friend_type))
9297 adjust_processing_template_decl = true;
9298 --processing_template_decl;
9300 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9301 && hidden_name_p (TYPE_NAME (friend_type)))
9303 /* friend class C;
9305 where C hasn't been declared yet. Let's lookup name
9306 from namespace scope directly, bypassing any name that
9307 come from dependent base class. */
9308 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9310 /* The call to xref_tag_from_type does injection for friend
9311 classes. */
9312 push_nested_namespace (ns);
9313 friend_type =
9314 xref_tag_from_type (friend_type, NULL_TREE,
9315 /*tag_scope=*/ts_current);
9316 pop_nested_namespace (ns);
9318 else if (uses_template_parms (friend_type))
9319 /* friend class C<T>; */
9320 friend_type = tsubst (friend_type, args,
9321 tf_warning_or_error, NULL_TREE);
9322 /* Otherwise it's
9324 friend class C;
9326 where C is already declared or
9328 friend class C<int>;
9330 We don't have to do anything in these cases. */
9332 if (adjust_processing_template_decl)
9333 /* Trick make_friend_class into realizing that the friend
9334 we're adding is a template, not an ordinary class. It's
9335 important that we use make_friend_class since it will
9336 perform some error-checking and output cross-reference
9337 information. */
9338 ++processing_template_decl;
9340 if (friend_type != error_mark_node)
9341 make_friend_class (type, friend_type, /*complain=*/false);
9343 if (adjust_processing_template_decl)
9344 --processing_template_decl;
9346 else
9348 /* Build new DECL_FRIENDLIST. */
9349 tree r;
9351 /* The file and line for this declaration, to
9352 assist in error message reporting. Since we
9353 called push_tinst_level above, we don't need to
9354 restore these. */
9355 input_location = DECL_SOURCE_LOCATION (t);
9357 if (TREE_CODE (t) == TEMPLATE_DECL)
9359 ++processing_template_decl;
9360 push_deferring_access_checks (dk_no_check);
9363 r = tsubst_friend_function (t, args);
9364 add_friend (type, r, /*complain=*/false);
9365 if (TREE_CODE (t) == TEMPLATE_DECL)
9367 pop_deferring_access_checks ();
9368 --processing_template_decl;
9374 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9376 tree decl = lambda_function (type);
9377 if (decl)
9379 if (!DECL_TEMPLATE_INFO (decl)
9380 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9381 instantiate_decl (decl, false, false);
9383 /* We need to instantiate the capture list from the template
9384 after we've instantiated the closure members, but before we
9385 consider adding the conversion op. Also keep any captures
9386 that may have been added during instantiation of the op(). */
9387 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9388 tree tmpl_cap
9389 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9390 args, tf_warning_or_error, NULL_TREE,
9391 false, false);
9393 LAMBDA_EXPR_CAPTURE_LIST (expr)
9394 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9396 maybe_add_lambda_conv_op (type);
9398 else
9399 gcc_assert (errorcount);
9402 /* Set the file and line number information to whatever is given for
9403 the class itself. This puts error messages involving generated
9404 implicit functions at a predictable point, and the same point
9405 that would be used for non-template classes. */
9406 input_location = DECL_SOURCE_LOCATION (typedecl);
9408 unreverse_member_declarations (type);
9409 finish_struct_1 (type);
9410 TYPE_BEING_DEFINED (type) = 0;
9412 /* We don't instantiate default arguments for member functions. 14.7.1:
9414 The implicit instantiation of a class template specialization causes
9415 the implicit instantiation of the declarations, but not of the
9416 definitions or default arguments, of the class member functions,
9417 member classes, static data members and member templates.... */
9419 /* Some typedefs referenced from within the template code need to be access
9420 checked at template instantiation time, i.e now. These types were
9421 added to the template at parsing time. Let's get those and perform
9422 the access checks then. */
9423 perform_typedefs_access_check (pattern, args);
9424 perform_deferred_access_checks (tf_warning_or_error);
9425 pop_nested_class ();
9426 maximum_field_alignment = saved_maximum_field_alignment;
9427 if (!fn_context)
9428 pop_from_top_level ();
9429 pop_deferring_access_checks ();
9430 pop_tinst_level ();
9432 /* The vtable for a template class can be emitted in any translation
9433 unit in which the class is instantiated. When there is no key
9434 method, however, finish_struct_1 will already have added TYPE to
9435 the keyed_classes list. */
9436 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9437 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9439 return type;
9442 /* Wrapper for instantiate_class_template_1. */
9444 tree
9445 instantiate_class_template (tree type)
9447 tree ret;
9448 timevar_push (TV_TEMPLATE_INST);
9449 ret = instantiate_class_template_1 (type);
9450 timevar_pop (TV_TEMPLATE_INST);
9451 return ret;
9454 static tree
9455 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9457 tree r;
9459 if (!t)
9460 r = t;
9461 else if (TYPE_P (t))
9462 r = tsubst (t, args, complain, in_decl);
9463 else
9465 if (!(complain & tf_warning))
9466 ++c_inhibit_evaluation_warnings;
9467 r = tsubst_expr (t, args, complain, in_decl,
9468 /*integral_constant_expression_p=*/true);
9469 if (!(complain & tf_warning))
9470 --c_inhibit_evaluation_warnings;
9472 return r;
9475 /* Given a function parameter pack TMPL_PARM and some function parameters
9476 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9477 and set *SPEC_P to point at the next point in the list. */
9479 static tree
9480 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9482 /* Collect all of the extra "packed" parameters into an
9483 argument pack. */
9484 tree parmvec;
9485 tree parmtypevec;
9486 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9487 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9488 tree spec_parm = *spec_p;
9489 int i, len;
9491 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9492 if (tmpl_parm
9493 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9494 break;
9496 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9497 parmvec = make_tree_vec (len);
9498 parmtypevec = make_tree_vec (len);
9499 spec_parm = *spec_p;
9500 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9502 TREE_VEC_ELT (parmvec, i) = spec_parm;
9503 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9506 /* Build the argument packs. */
9507 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9508 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9509 TREE_TYPE (argpack) = argtypepack;
9510 *spec_p = spec_parm;
9512 return argpack;
9515 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9516 NONTYPE_ARGUMENT_PACK. */
9518 static tree
9519 make_fnparm_pack (tree spec_parm)
9521 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9524 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9525 pack expansion. */
9527 static bool
9528 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9530 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9531 if (i >= TREE_VEC_LENGTH (vec))
9532 return false;
9533 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9537 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9539 static tree
9540 make_argument_pack_select (tree arg_pack, unsigned index)
9542 tree aps = make_node (ARGUMENT_PACK_SELECT);
9544 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9545 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9547 return aps;
9550 /* This is a subroutine of tsubst_pack_expansion.
9552 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9553 mechanism to store the (non complete list of) arguments of the
9554 substitution and return a non substituted pack expansion, in order
9555 to wait for when we have enough arguments to really perform the
9556 substitution. */
9558 static bool
9559 use_pack_expansion_extra_args_p (tree parm_packs,
9560 int arg_pack_len,
9561 bool has_empty_arg)
9563 /* If one pack has an expansion and another pack has a normal
9564 argument or if one pack has an empty argument and an another
9565 one hasn't then tsubst_pack_expansion cannot perform the
9566 substitution and need to fall back on the
9567 PACK_EXPANSION_EXTRA mechanism. */
9568 if (parm_packs == NULL_TREE)
9569 return false;
9570 else if (has_empty_arg)
9571 return true;
9573 bool has_expansion_arg = false;
9574 for (int i = 0 ; i < arg_pack_len; ++i)
9576 bool has_non_expansion_arg = false;
9577 for (tree parm_pack = parm_packs;
9578 parm_pack;
9579 parm_pack = TREE_CHAIN (parm_pack))
9581 tree arg = TREE_VALUE (parm_pack);
9583 if (argument_pack_element_is_expansion_p (arg, i))
9584 has_expansion_arg = true;
9585 else
9586 has_non_expansion_arg = true;
9589 if (has_expansion_arg && has_non_expansion_arg)
9590 return true;
9592 return false;
9595 /* [temp.variadic]/6 says that:
9597 The instantiation of a pack expansion [...]
9598 produces a list E1,E2, ..., En, where N is the number of elements
9599 in the pack expansion parameters.
9601 This subroutine of tsubst_pack_expansion produces one of these Ei.
9603 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9604 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9605 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9606 INDEX is the index 'i' of the element Ei to produce. ARGS,
9607 COMPLAIN, and IN_DECL are the same parameters as for the
9608 tsubst_pack_expansion function.
9610 The function returns the resulting Ei upon successful completion,
9611 or error_mark_node.
9613 Note that this function possibly modifies the ARGS parameter, so
9614 it's the responsibility of the caller to restore it. */
9616 static tree
9617 gen_elem_of_pack_expansion_instantiation (tree pattern,
9618 tree parm_packs,
9619 unsigned index,
9620 tree args /* This parm gets
9621 modified. */,
9622 tsubst_flags_t complain,
9623 tree in_decl)
9625 tree t;
9626 bool ith_elem_is_expansion = false;
9628 /* For each parameter pack, change the substitution of the parameter
9629 pack to the ith argument in its argument pack, then expand the
9630 pattern. */
9631 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9633 tree parm = TREE_PURPOSE (pack);
9634 tree arg_pack = TREE_VALUE (pack);
9635 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9637 ith_elem_is_expansion |=
9638 argument_pack_element_is_expansion_p (arg_pack, index);
9640 /* Select the Ith argument from the pack. */
9641 if (TREE_CODE (parm) == PARM_DECL
9642 || TREE_CODE (parm) == FIELD_DECL)
9644 if (index == 0)
9646 aps = make_argument_pack_select (arg_pack, index);
9647 mark_used (parm);
9648 register_local_specialization (aps, parm);
9650 else
9651 aps = retrieve_local_specialization (parm);
9653 else
9655 int idx, level;
9656 template_parm_level_and_index (parm, &level, &idx);
9658 if (index == 0)
9660 aps = make_argument_pack_select (arg_pack, index);
9661 /* Update the corresponding argument. */
9662 TMPL_ARG (args, level, idx) = aps;
9664 else
9665 /* Re-use the ARGUMENT_PACK_SELECT. */
9666 aps = TMPL_ARG (args, level, idx);
9668 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9671 /* Substitute into the PATTERN with the (possibly altered)
9672 arguments. */
9673 if (pattern == in_decl)
9674 /* Expanding a fixed parameter pack from
9675 coerce_template_parameter_pack. */
9676 t = tsubst_decl (pattern, args, complain);
9677 else if (!TYPE_P (pattern))
9678 t = tsubst_expr (pattern, args, complain, in_decl,
9679 /*integral_constant_expression_p=*/false);
9680 else
9681 t = tsubst (pattern, args, complain, in_decl);
9683 /* If the Ith argument pack element is a pack expansion, then
9684 the Ith element resulting from the substituting is going to
9685 be a pack expansion as well. */
9686 if (ith_elem_is_expansion)
9687 t = make_pack_expansion (t);
9689 return t;
9692 /* Substitute ARGS into T, which is an pack expansion
9693 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9694 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9695 (if only a partial substitution could be performed) or
9696 ERROR_MARK_NODE if there was an error. */
9697 tree
9698 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9699 tree in_decl)
9701 tree pattern;
9702 tree pack, packs = NULL_TREE;
9703 bool unsubstituted_packs = false;
9704 int i, len = -1;
9705 tree result;
9706 struct pointer_map_t *saved_local_specializations = NULL;
9707 bool need_local_specializations = false;
9708 int levels;
9710 gcc_assert (PACK_EXPANSION_P (t));
9711 pattern = PACK_EXPANSION_PATTERN (t);
9713 /* Add in any args remembered from an earlier partial instantiation. */
9714 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9716 levels = TMPL_ARGS_DEPTH (args);
9718 /* Determine the argument packs that will instantiate the parameter
9719 packs used in the expansion expression. While we're at it,
9720 compute the number of arguments to be expanded and make sure it
9721 is consistent. */
9722 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9723 pack = TREE_CHAIN (pack))
9725 tree parm_pack = TREE_VALUE (pack);
9726 tree arg_pack = NULL_TREE;
9727 tree orig_arg = NULL_TREE;
9728 int level = 0;
9730 if (TREE_CODE (parm_pack) == BASES)
9732 if (BASES_DIRECT (parm_pack))
9733 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9734 args, complain, in_decl, false));
9735 else
9736 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9737 args, complain, in_decl, false));
9739 if (TREE_CODE (parm_pack) == PARM_DECL)
9741 if (PACK_EXPANSION_LOCAL_P (t))
9742 arg_pack = retrieve_local_specialization (parm_pack);
9743 else
9745 /* We can't rely on local_specializations for a parameter
9746 name used later in a function declaration (such as in a
9747 late-specified return type). Even if it exists, it might
9748 have the wrong value for a recursive call. Just make a
9749 dummy decl, since it's only used for its type. */
9750 arg_pack = tsubst_decl (parm_pack, args, complain);
9751 if (arg_pack && DECL_PACK_P (arg_pack))
9752 /* Partial instantiation of the parm_pack, we can't build
9753 up an argument pack yet. */
9754 arg_pack = NULL_TREE;
9755 else
9756 arg_pack = make_fnparm_pack (arg_pack);
9757 need_local_specializations = true;
9760 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9761 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9762 else
9764 int idx;
9765 template_parm_level_and_index (parm_pack, &level, &idx);
9767 if (level <= levels)
9768 arg_pack = TMPL_ARG (args, level, idx);
9771 orig_arg = arg_pack;
9772 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9773 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9775 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9776 /* This can only happen if we forget to expand an argument
9777 pack somewhere else. Just return an error, silently. */
9779 result = make_tree_vec (1);
9780 TREE_VEC_ELT (result, 0) = error_mark_node;
9781 return result;
9784 if (arg_pack)
9786 int my_len =
9787 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9789 /* Don't bother trying to do a partial substitution with
9790 incomplete packs; we'll try again after deduction. */
9791 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9792 return t;
9794 if (len < 0)
9795 len = my_len;
9796 else if (len != my_len)
9798 if (!(complain & tf_error))
9799 /* Fail quietly. */;
9800 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9801 error ("mismatched argument pack lengths while expanding "
9802 "%<%T%>",
9803 pattern);
9804 else
9805 error ("mismatched argument pack lengths while expanding "
9806 "%<%E%>",
9807 pattern);
9808 return error_mark_node;
9811 /* Keep track of the parameter packs and their corresponding
9812 argument packs. */
9813 packs = tree_cons (parm_pack, arg_pack, packs);
9814 TREE_TYPE (packs) = orig_arg;
9816 else
9818 /* We can't substitute for this parameter pack. We use a flag as
9819 well as the missing_level counter because function parameter
9820 packs don't have a level. */
9821 unsubstituted_packs = true;
9825 /* If the expansion is just T..., return the matching argument pack. */
9826 if (!unsubstituted_packs
9827 && TREE_PURPOSE (packs) == pattern)
9829 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
9830 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
9831 || pack_expansion_args_count (args))
9832 return args;
9833 /* Otherwise use the normal path so we get convert_from_reference. */
9836 /* We cannot expand this expansion expression, because we don't have
9837 all of the argument packs we need. */
9838 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9840 /* We got some full packs, but we can't substitute them in until we
9841 have values for all the packs. So remember these until then. */
9843 t = make_pack_expansion (pattern);
9844 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9845 return t;
9847 else if (unsubstituted_packs)
9849 /* There were no real arguments, we're just replacing a parameter
9850 pack with another version of itself. Substitute into the
9851 pattern and return a PACK_EXPANSION_*. The caller will need to
9852 deal with that. */
9853 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9854 t = tsubst_expr (pattern, args, complain, in_decl,
9855 /*integral_constant_expression_p=*/false);
9856 else
9857 t = tsubst (pattern, args, complain, in_decl);
9858 t = make_pack_expansion (t);
9859 return t;
9862 gcc_assert (len >= 0);
9864 if (need_local_specializations)
9866 /* We're in a late-specified return type, so create our own local
9867 specializations map; the current map is either NULL or (in the
9868 case of recursive unification) might have bindings that we don't
9869 want to use or alter. */
9870 saved_local_specializations = local_specializations;
9871 local_specializations = pointer_map_create ();
9874 /* For each argument in each argument pack, substitute into the
9875 pattern. */
9876 result = make_tree_vec (len);
9877 for (i = 0; i < len; ++i)
9879 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9881 args, complain,
9882 in_decl);
9883 TREE_VEC_ELT (result, i) = t;
9884 if (t == error_mark_node)
9886 result = error_mark_node;
9887 break;
9891 /* Update ARGS to restore the substitution from parameter packs to
9892 their argument packs. */
9893 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9895 tree parm = TREE_PURPOSE (pack);
9897 if (TREE_CODE (parm) == PARM_DECL
9898 || TREE_CODE (parm) == FIELD_DECL)
9899 register_local_specialization (TREE_TYPE (pack), parm);
9900 else
9902 int idx, level;
9904 if (TREE_VALUE (pack) == NULL_TREE)
9905 continue;
9907 template_parm_level_and_index (parm, &level, &idx);
9909 /* Update the corresponding argument. */
9910 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9911 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9912 TREE_TYPE (pack);
9913 else
9914 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9918 if (need_local_specializations)
9920 pointer_map_destroy (local_specializations);
9921 local_specializations = saved_local_specializations;
9924 return result;
9927 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9928 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9929 parameter packs; all parms generated from a function parameter pack will
9930 have the same DECL_PARM_INDEX. */
9932 tree
9933 get_pattern_parm (tree parm, tree tmpl)
9935 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9936 tree patparm;
9938 if (DECL_ARTIFICIAL (parm))
9940 for (patparm = DECL_ARGUMENTS (pattern);
9941 patparm; patparm = DECL_CHAIN (patparm))
9942 if (DECL_ARTIFICIAL (patparm)
9943 && DECL_NAME (parm) == DECL_NAME (patparm))
9944 break;
9946 else
9948 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9949 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9950 gcc_assert (DECL_PARM_INDEX (patparm)
9951 == DECL_PARM_INDEX (parm));
9954 return patparm;
9957 /* Substitute ARGS into the vector or list of template arguments T. */
9959 static tree
9960 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9962 tree orig_t = t;
9963 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9964 tree *elts;
9966 if (t == error_mark_node)
9967 return error_mark_node;
9969 len = TREE_VEC_LENGTH (t);
9970 elts = XALLOCAVEC (tree, len);
9972 for (i = 0; i < len; i++)
9974 tree orig_arg = TREE_VEC_ELT (t, i);
9975 tree new_arg;
9977 if (TREE_CODE (orig_arg) == TREE_VEC)
9978 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9979 else if (PACK_EXPANSION_P (orig_arg))
9981 /* Substitute into an expansion expression. */
9982 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9984 if (TREE_CODE (new_arg) == TREE_VEC)
9985 /* Add to the expanded length adjustment the number of
9986 expanded arguments. We subtract one from this
9987 measurement, because the argument pack expression
9988 itself is already counted as 1 in
9989 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9990 the argument pack is empty. */
9991 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9993 else if (ARGUMENT_PACK_P (orig_arg))
9995 /* Substitute into each of the arguments. */
9996 new_arg = TYPE_P (orig_arg)
9997 ? cxx_make_type (TREE_CODE (orig_arg))
9998 : make_node (TREE_CODE (orig_arg));
10000 SET_ARGUMENT_PACK_ARGS (
10001 new_arg,
10002 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10003 args, complain, in_decl));
10005 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10006 new_arg = error_mark_node;
10008 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10009 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10010 complain, in_decl);
10011 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10013 if (TREE_TYPE (new_arg) == error_mark_node)
10014 new_arg = error_mark_node;
10017 else
10018 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10020 if (new_arg == error_mark_node)
10021 return error_mark_node;
10023 elts[i] = new_arg;
10024 if (new_arg != orig_arg)
10025 need_new = 1;
10028 if (!need_new)
10029 return t;
10031 /* Make space for the expanded arguments coming from template
10032 argument packs. */
10033 t = make_tree_vec (len + expanded_len_adjust);
10034 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10035 arguments for a member template.
10036 In that case each TREE_VEC in ORIG_T represents a level of template
10037 arguments, and ORIG_T won't carry any non defaulted argument count.
10038 It will rather be the nested TREE_VECs that will carry one.
10039 In other words, ORIG_T carries a non defaulted argument count only
10040 if it doesn't contain any nested TREE_VEC. */
10041 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10043 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10044 count += expanded_len_adjust;
10045 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10047 for (i = 0, out = 0; i < len; i++)
10049 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10050 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10051 && TREE_CODE (elts[i]) == TREE_VEC)
10053 int idx;
10055 /* Now expand the template argument pack "in place". */
10056 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10057 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10059 else
10061 TREE_VEC_ELT (t, out) = elts[i];
10062 out++;
10066 return t;
10069 /* Return the result of substituting ARGS into the template parameters
10070 given by PARMS. If there are m levels of ARGS and m + n levels of
10071 PARMS, then the result will contain n levels of PARMS. For
10072 example, if PARMS is `template <class T> template <class U>
10073 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10074 result will be `template <int*, double, class V>'. */
10076 static tree
10077 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10079 tree r = NULL_TREE;
10080 tree* new_parms;
10082 /* When substituting into a template, we must set
10083 PROCESSING_TEMPLATE_DECL as the template parameters may be
10084 dependent if they are based on one-another, and the dependency
10085 predicates are short-circuit outside of templates. */
10086 ++processing_template_decl;
10088 for (new_parms = &r;
10089 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10090 new_parms = &(TREE_CHAIN (*new_parms)),
10091 parms = TREE_CHAIN (parms))
10093 tree new_vec =
10094 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10095 int i;
10097 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10099 tree tuple;
10101 if (parms == error_mark_node)
10102 continue;
10104 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10106 if (tuple == error_mark_node)
10107 continue;
10109 TREE_VEC_ELT (new_vec, i) =
10110 tsubst_template_parm (tuple, args, complain);
10113 *new_parms =
10114 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10115 - TMPL_ARGS_DEPTH (args)),
10116 new_vec, NULL_TREE);
10119 --processing_template_decl;
10121 return r;
10124 /* Return the result of substituting ARGS into one template parameter
10125 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10126 parameter and which TREE_PURPOSE is the default argument of the
10127 template parameter. */
10129 static tree
10130 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10132 tree default_value, parm_decl;
10134 if (args == NULL_TREE
10135 || t == NULL_TREE
10136 || t == error_mark_node)
10137 return t;
10139 gcc_assert (TREE_CODE (t) == TREE_LIST);
10141 default_value = TREE_PURPOSE (t);
10142 parm_decl = TREE_VALUE (t);
10144 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10145 if (TREE_CODE (parm_decl) == PARM_DECL
10146 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10147 parm_decl = error_mark_node;
10148 default_value = tsubst_template_arg (default_value, args,
10149 complain, NULL_TREE);
10151 return build_tree_list (default_value, parm_decl);
10154 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10155 type T. If T is not an aggregate or enumeration type, it is
10156 handled as if by tsubst. IN_DECL is as for tsubst. If
10157 ENTERING_SCOPE is nonzero, T is the context for a template which
10158 we are presently tsubst'ing. Return the substituted value. */
10160 static tree
10161 tsubst_aggr_type (tree t,
10162 tree args,
10163 tsubst_flags_t complain,
10164 tree in_decl,
10165 int entering_scope)
10167 if (t == NULL_TREE)
10168 return NULL_TREE;
10170 switch (TREE_CODE (t))
10172 case RECORD_TYPE:
10173 if (TYPE_PTRMEMFUNC_P (t))
10174 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10176 /* Else fall through. */
10177 case ENUMERAL_TYPE:
10178 case UNION_TYPE:
10179 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10181 tree argvec;
10182 tree context;
10183 tree r;
10184 int saved_unevaluated_operand;
10185 int saved_inhibit_evaluation_warnings;
10187 /* In "sizeof(X<I>)" we need to evaluate "I". */
10188 saved_unevaluated_operand = cp_unevaluated_operand;
10189 cp_unevaluated_operand = 0;
10190 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10191 c_inhibit_evaluation_warnings = 0;
10193 /* First, determine the context for the type we are looking
10194 up. */
10195 context = TYPE_CONTEXT (t);
10196 if (context && TYPE_P (context))
10198 context = tsubst_aggr_type (context, args, complain,
10199 in_decl, /*entering_scope=*/1);
10200 /* If context is a nested class inside a class template,
10201 it may still need to be instantiated (c++/33959). */
10202 context = complete_type (context);
10205 /* Then, figure out what arguments are appropriate for the
10206 type we are trying to find. For example, given:
10208 template <class T> struct S;
10209 template <class T, class U> void f(T, U) { S<U> su; }
10211 and supposing that we are instantiating f<int, double>,
10212 then our ARGS will be {int, double}, but, when looking up
10213 S we only want {double}. */
10214 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10215 complain, in_decl);
10216 if (argvec == error_mark_node)
10217 r = error_mark_node;
10218 else
10220 r = lookup_template_class (t, argvec, in_decl, context,
10221 entering_scope, complain);
10222 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10225 cp_unevaluated_operand = saved_unevaluated_operand;
10226 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10228 return r;
10230 else
10231 /* This is not a template type, so there's nothing to do. */
10232 return t;
10234 default:
10235 return tsubst (t, args, complain, in_decl);
10239 /* Substitute into the default argument ARG (a default argument for
10240 FN), which has the indicated TYPE. */
10242 tree
10243 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10245 tree saved_class_ptr = NULL_TREE;
10246 tree saved_class_ref = NULL_TREE;
10247 int errs = errorcount + sorrycount;
10249 /* This can happen in invalid code. */
10250 if (TREE_CODE (arg) == DEFAULT_ARG)
10251 return arg;
10253 /* This default argument came from a template. Instantiate the
10254 default argument here, not in tsubst. In the case of
10255 something like:
10257 template <class T>
10258 struct S {
10259 static T t();
10260 void f(T = t());
10263 we must be careful to do name lookup in the scope of S<T>,
10264 rather than in the current class. */
10265 push_access_scope (fn);
10266 /* The "this" pointer is not valid in a default argument. */
10267 if (cfun)
10269 saved_class_ptr = current_class_ptr;
10270 cp_function_chain->x_current_class_ptr = NULL_TREE;
10271 saved_class_ref = current_class_ref;
10272 cp_function_chain->x_current_class_ref = NULL_TREE;
10275 push_deferring_access_checks(dk_no_deferred);
10276 /* The default argument expression may cause implicitly defined
10277 member functions to be synthesized, which will result in garbage
10278 collection. We must treat this situation as if we were within
10279 the body of function so as to avoid collecting live data on the
10280 stack. */
10281 ++function_depth;
10282 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10283 complain, NULL_TREE,
10284 /*integral_constant_expression_p=*/false);
10285 --function_depth;
10286 pop_deferring_access_checks();
10288 /* Restore the "this" pointer. */
10289 if (cfun)
10291 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10292 cp_function_chain->x_current_class_ref = saved_class_ref;
10295 if (errorcount+sorrycount > errs
10296 && (complain & tf_warning_or_error))
10297 inform (input_location,
10298 " when instantiating default argument for call to %D", fn);
10300 /* Make sure the default argument is reasonable. */
10301 arg = check_default_argument (type, arg, complain);
10303 pop_access_scope (fn);
10305 return arg;
10308 /* Substitute into all the default arguments for FN. */
10310 static void
10311 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10313 tree arg;
10314 tree tmpl_args;
10316 tmpl_args = DECL_TI_ARGS (fn);
10318 /* If this function is not yet instantiated, we certainly don't need
10319 its default arguments. */
10320 if (uses_template_parms (tmpl_args))
10321 return;
10322 /* Don't do this again for clones. */
10323 if (DECL_CLONED_FUNCTION_P (fn))
10324 return;
10326 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10327 arg;
10328 arg = TREE_CHAIN (arg))
10329 if (TREE_PURPOSE (arg))
10330 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10331 TREE_VALUE (arg),
10332 TREE_PURPOSE (arg),
10333 complain);
10336 /* Substitute the ARGS into the T, which is a _DECL. Return the
10337 result of the substitution. Issue error and warning messages under
10338 control of COMPLAIN. */
10340 static tree
10341 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10343 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10344 location_t saved_loc;
10345 tree r = NULL_TREE;
10346 tree in_decl = t;
10347 hashval_t hash = 0;
10349 /* Set the filename and linenumber to improve error-reporting. */
10350 saved_loc = input_location;
10351 input_location = DECL_SOURCE_LOCATION (t);
10353 switch (TREE_CODE (t))
10355 case TEMPLATE_DECL:
10357 /* We can get here when processing a member function template,
10358 member class template, or template template parameter. */
10359 tree decl = DECL_TEMPLATE_RESULT (t);
10360 tree spec;
10361 tree tmpl_args;
10362 tree full_args;
10364 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10366 /* Template template parameter is treated here. */
10367 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10368 if (new_type == error_mark_node)
10369 RETURN (error_mark_node);
10370 /* If we get a real template back, return it. This can happen in
10371 the context of most_specialized_class. */
10372 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10373 return new_type;
10375 r = copy_decl (t);
10376 DECL_CHAIN (r) = NULL_TREE;
10377 TREE_TYPE (r) = new_type;
10378 DECL_TEMPLATE_RESULT (r)
10379 = build_decl (DECL_SOURCE_LOCATION (decl),
10380 TYPE_DECL, DECL_NAME (decl), new_type);
10381 DECL_TEMPLATE_PARMS (r)
10382 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10383 complain);
10384 TYPE_NAME (new_type) = r;
10385 break;
10388 /* We might already have an instance of this template.
10389 The ARGS are for the surrounding class type, so the
10390 full args contain the tsubst'd args for the context,
10391 plus the innermost args from the template decl. */
10392 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10393 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10394 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10395 /* Because this is a template, the arguments will still be
10396 dependent, even after substitution. If
10397 PROCESSING_TEMPLATE_DECL is not set, the dependency
10398 predicates will short-circuit. */
10399 ++processing_template_decl;
10400 full_args = tsubst_template_args (tmpl_args, args,
10401 complain, in_decl);
10402 --processing_template_decl;
10403 if (full_args == error_mark_node)
10404 RETURN (error_mark_node);
10406 /* If this is a default template template argument,
10407 tsubst might not have changed anything. */
10408 if (full_args == tmpl_args)
10409 RETURN (t);
10411 hash = hash_tmpl_and_args (t, full_args);
10412 spec = retrieve_specialization (t, full_args, hash);
10413 if (spec != NULL_TREE)
10415 r = spec;
10416 break;
10419 /* Make a new template decl. It will be similar to the
10420 original, but will record the current template arguments.
10421 We also create a new function declaration, which is just
10422 like the old one, but points to this new template, rather
10423 than the old one. */
10424 r = copy_decl (t);
10425 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10426 DECL_CHAIN (r) = NULL_TREE;
10428 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10430 if (TREE_CODE (decl) == TYPE_DECL
10431 && !TYPE_DECL_ALIAS_P (decl))
10433 tree new_type;
10434 ++processing_template_decl;
10435 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10436 --processing_template_decl;
10437 if (new_type == error_mark_node)
10438 RETURN (error_mark_node);
10440 TREE_TYPE (r) = new_type;
10441 /* For a partial specialization, we need to keep pointing to
10442 the primary template. */
10443 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10444 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10445 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10446 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10447 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10449 else
10451 tree new_decl;
10452 ++processing_template_decl;
10453 new_decl = tsubst (decl, args, complain, in_decl);
10454 --processing_template_decl;
10455 if (new_decl == error_mark_node)
10456 RETURN (error_mark_node);
10458 DECL_TEMPLATE_RESULT (r) = new_decl;
10459 DECL_TI_TEMPLATE (new_decl) = r;
10460 TREE_TYPE (r) = TREE_TYPE (new_decl);
10461 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10462 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10465 SET_DECL_IMPLICIT_INSTANTIATION (r);
10466 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10467 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10469 /* The template parameters for this new template are all the
10470 template parameters for the old template, except the
10471 outermost level of parameters. */
10472 DECL_TEMPLATE_PARMS (r)
10473 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10474 complain);
10476 if (PRIMARY_TEMPLATE_P (t))
10477 DECL_PRIMARY_TEMPLATE (r) = r;
10479 if (TREE_CODE (decl) != TYPE_DECL)
10480 /* Record this non-type partial instantiation. */
10481 register_specialization (r, t,
10482 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10483 false, hash);
10485 break;
10487 case FUNCTION_DECL:
10489 tree ctx;
10490 tree argvec = NULL_TREE;
10491 tree *friends;
10492 tree gen_tmpl;
10493 tree type;
10494 int member;
10495 int args_depth;
10496 int parms_depth;
10498 /* Nobody should be tsubst'ing into non-template functions. */
10499 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10501 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10503 tree spec;
10504 bool dependent_p;
10506 /* If T is not dependent, just return it. We have to
10507 increment PROCESSING_TEMPLATE_DECL because
10508 value_dependent_expression_p assumes that nothing is
10509 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10510 ++processing_template_decl;
10511 dependent_p = value_dependent_expression_p (t);
10512 --processing_template_decl;
10513 if (!dependent_p)
10514 RETURN (t);
10516 /* Calculate the most general template of which R is a
10517 specialization, and the complete set of arguments used to
10518 specialize R. */
10519 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10520 argvec = tsubst_template_args (DECL_TI_ARGS
10521 (DECL_TEMPLATE_RESULT
10522 (DECL_TI_TEMPLATE (t))),
10523 args, complain, in_decl);
10524 if (argvec == error_mark_node)
10525 RETURN (error_mark_node);
10527 /* Check to see if we already have this specialization. */
10528 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10529 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10531 if (spec)
10533 r = spec;
10534 break;
10537 /* We can see more levels of arguments than parameters if
10538 there was a specialization of a member template, like
10539 this:
10541 template <class T> struct S { template <class U> void f(); }
10542 template <> template <class U> void S<int>::f(U);
10544 Here, we'll be substituting into the specialization,
10545 because that's where we can find the code we actually
10546 want to generate, but we'll have enough arguments for
10547 the most general template.
10549 We also deal with the peculiar case:
10551 template <class T> struct S {
10552 template <class U> friend void f();
10554 template <class U> void f() {}
10555 template S<int>;
10556 template void f<double>();
10558 Here, the ARGS for the instantiation of will be {int,
10559 double}. But, we only need as many ARGS as there are
10560 levels of template parameters in CODE_PATTERN. We are
10561 careful not to get fooled into reducing the ARGS in
10562 situations like:
10564 template <class T> struct S { template <class U> void f(U); }
10565 template <class T> template <> void S<T>::f(int) {}
10567 which we can spot because the pattern will be a
10568 specialization in this case. */
10569 args_depth = TMPL_ARGS_DEPTH (args);
10570 parms_depth =
10571 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10572 if (args_depth > parms_depth
10573 && !DECL_TEMPLATE_SPECIALIZATION (t))
10574 args = get_innermost_template_args (args, parms_depth);
10576 else
10578 /* This special case arises when we have something like this:
10580 template <class T> struct S {
10581 friend void f<int>(int, double);
10584 Here, the DECL_TI_TEMPLATE for the friend declaration
10585 will be an IDENTIFIER_NODE. We are being called from
10586 tsubst_friend_function, and we want only to create a
10587 new decl (R) with appropriate types so that we can call
10588 determine_specialization. */
10589 gen_tmpl = NULL_TREE;
10592 if (DECL_CLASS_SCOPE_P (t))
10594 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10595 member = 2;
10596 else
10597 member = 1;
10598 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10599 complain, t, /*entering_scope=*/1);
10601 else
10603 member = 0;
10604 ctx = DECL_CONTEXT (t);
10606 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10607 if (type == error_mark_node)
10608 RETURN (error_mark_node);
10610 /* If we hit excessive deduction depth, the type is bogus even if
10611 it isn't error_mark_node, so don't build a decl. */
10612 if (excessive_deduction_depth)
10613 RETURN (error_mark_node);
10615 /* We do NOT check for matching decls pushed separately at this
10616 point, as they may not represent instantiations of this
10617 template, and in any case are considered separate under the
10618 discrete model. */
10619 r = copy_decl (t);
10620 DECL_USE_TEMPLATE (r) = 0;
10621 TREE_TYPE (r) = type;
10622 /* Clear out the mangled name and RTL for the instantiation. */
10623 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10624 SET_DECL_RTL (r, NULL);
10625 /* Leave DECL_INITIAL set on deleted instantiations. */
10626 if (!DECL_DELETED_FN (r))
10627 DECL_INITIAL (r) = NULL_TREE;
10628 DECL_CONTEXT (r) = ctx;
10630 /* OpenMP UDRs have the only argument a reference to the declared
10631 type. We want to diagnose if the declared type is a reference,
10632 which is invalid, but as references to references are usually
10633 quietly merged, diagnose it here. */
10634 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10636 tree argtype
10637 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10638 argtype = tsubst (argtype, args, complain, in_decl);
10639 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10640 error_at (DECL_SOURCE_LOCATION (t),
10641 "reference type %qT in "
10642 "%<#pragma omp declare reduction%>", argtype);
10643 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10644 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10645 argtype);
10648 if (member && DECL_CONV_FN_P (r))
10649 /* Type-conversion operator. Reconstruct the name, in
10650 case it's the name of one of the template's parameters. */
10651 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10653 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10654 complain, t);
10655 DECL_RESULT (r) = NULL_TREE;
10657 TREE_STATIC (r) = 0;
10658 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10659 DECL_EXTERNAL (r) = 1;
10660 /* If this is an instantiation of a function with internal
10661 linkage, we already know what object file linkage will be
10662 assigned to the instantiation. */
10663 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10664 DECL_DEFER_OUTPUT (r) = 0;
10665 DECL_CHAIN (r) = NULL_TREE;
10666 DECL_PENDING_INLINE_INFO (r) = 0;
10667 DECL_PENDING_INLINE_P (r) = 0;
10668 DECL_SAVED_TREE (r) = NULL_TREE;
10669 DECL_STRUCT_FUNCTION (r) = NULL;
10670 TREE_USED (r) = 0;
10671 /* We'll re-clone as appropriate in instantiate_template. */
10672 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10674 /* If we aren't complaining now, return on error before we register
10675 the specialization so that we'll complain eventually. */
10676 if ((complain & tf_error) == 0
10677 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10678 && !grok_op_properties (r, /*complain=*/false))
10679 RETURN (error_mark_node);
10681 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10682 this in the special friend case mentioned above where
10683 GEN_TMPL is NULL. */
10684 if (gen_tmpl)
10686 DECL_TEMPLATE_INFO (r)
10687 = build_template_info (gen_tmpl, argvec);
10688 SET_DECL_IMPLICIT_INSTANTIATION (r);
10690 tree new_r
10691 = register_specialization (r, gen_tmpl, argvec, false, hash);
10692 if (new_r != r)
10693 /* We instantiated this while substituting into
10694 the type earlier (template/friend54.C). */
10695 RETURN (new_r);
10697 if (!DECL_FRIEND_P (r))
10698 note_comdat_fn (r);
10700 /* We're not supposed to instantiate default arguments
10701 until they are called, for a template. But, for a
10702 declaration like:
10704 template <class T> void f ()
10705 { extern void g(int i = T()); }
10707 we should do the substitution when the template is
10708 instantiated. We handle the member function case in
10709 instantiate_class_template since the default arguments
10710 might refer to other members of the class. */
10711 if (!member
10712 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10713 && !uses_template_parms (argvec))
10714 tsubst_default_arguments (r, complain);
10716 else
10717 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10719 /* Copy the list of befriending classes. */
10720 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10721 *friends;
10722 friends = &TREE_CHAIN (*friends))
10724 *friends = copy_node (*friends);
10725 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10726 args, complain,
10727 in_decl);
10730 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10732 maybe_retrofit_in_chrg (r);
10733 if (DECL_CONSTRUCTOR_P (r))
10734 grok_ctor_properties (ctx, r);
10735 if (DECL_INHERITED_CTOR_BASE (r))
10736 deduce_inheriting_ctor (r);
10737 /* If this is an instantiation of a member template, clone it.
10738 If it isn't, that'll be handled by
10739 clone_constructors_and_destructors. */
10740 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10741 clone_function_decl (r, /*update_method_vec_p=*/0);
10743 else if ((complain & tf_error) != 0
10744 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10745 && !grok_op_properties (r, /*complain=*/true))
10746 RETURN (error_mark_node);
10748 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10749 SET_DECL_FRIEND_CONTEXT (r,
10750 tsubst (DECL_FRIEND_CONTEXT (t),
10751 args, complain, in_decl));
10753 /* Possibly limit visibility based on template args. */
10754 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10755 if (DECL_VISIBILITY_SPECIFIED (t))
10757 DECL_VISIBILITY_SPECIFIED (r) = 0;
10758 DECL_ATTRIBUTES (r)
10759 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10761 determine_visibility (r);
10762 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10763 && !processing_template_decl)
10764 defaulted_late_check (r);
10766 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10767 args, complain, in_decl);
10769 break;
10771 case PARM_DECL:
10773 tree type = NULL_TREE;
10774 int i, len = 1;
10775 tree expanded_types = NULL_TREE;
10776 tree prev_r = NULL_TREE;
10777 tree first_r = NULL_TREE;
10779 if (DECL_PACK_P (t))
10781 /* If there is a local specialization that isn't a
10782 parameter pack, it means that we're doing a "simple"
10783 substitution from inside tsubst_pack_expansion. Just
10784 return the local specialization (which will be a single
10785 parm). */
10786 tree spec = retrieve_local_specialization (t);
10787 if (spec
10788 && TREE_CODE (spec) == PARM_DECL
10789 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10790 RETURN (spec);
10792 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10793 the parameters in this function parameter pack. */
10794 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10795 complain, in_decl);
10796 if (TREE_CODE (expanded_types) == TREE_VEC)
10798 len = TREE_VEC_LENGTH (expanded_types);
10800 /* Zero-length parameter packs are boring. Just substitute
10801 into the chain. */
10802 if (len == 0)
10803 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10804 TREE_CHAIN (t)));
10806 else
10808 /* All we did was update the type. Make a note of that. */
10809 type = expanded_types;
10810 expanded_types = NULL_TREE;
10814 /* Loop through all of the parameters we'll build. When T is
10815 a function parameter pack, LEN is the number of expanded
10816 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10817 r = NULL_TREE;
10818 for (i = 0; i < len; ++i)
10820 prev_r = r;
10821 r = copy_node (t);
10822 if (DECL_TEMPLATE_PARM_P (t))
10823 SET_DECL_TEMPLATE_PARM_P (r);
10825 if (expanded_types)
10826 /* We're on the Ith parameter of the function parameter
10827 pack. */
10829 /* Get the Ith type. */
10830 type = TREE_VEC_ELT (expanded_types, i);
10832 /* Rename the parameter to include the index. */
10833 DECL_NAME (r)
10834 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10836 else if (!type)
10837 /* We're dealing with a normal parameter. */
10838 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10840 type = type_decays_to (type);
10841 TREE_TYPE (r) = type;
10842 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10844 if (DECL_INITIAL (r))
10846 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10847 DECL_INITIAL (r) = TREE_TYPE (r);
10848 else
10849 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10850 complain, in_decl);
10853 DECL_CONTEXT (r) = NULL_TREE;
10855 if (!DECL_TEMPLATE_PARM_P (r))
10856 DECL_ARG_TYPE (r) = type_passed_as (type);
10858 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10859 args, complain, in_decl);
10861 /* Keep track of the first new parameter we
10862 generate. That's what will be returned to the
10863 caller. */
10864 if (!first_r)
10865 first_r = r;
10867 /* Build a proper chain of parameters when substituting
10868 into a function parameter pack. */
10869 if (prev_r)
10870 DECL_CHAIN (prev_r) = r;
10873 /* If cp_unevaluated_operand is set, we're just looking for a
10874 single dummy parameter, so don't keep going. */
10875 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10876 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10877 complain, DECL_CHAIN (t));
10879 /* FIRST_R contains the start of the chain we've built. */
10880 r = first_r;
10882 break;
10884 case FIELD_DECL:
10886 tree type = NULL_TREE;
10887 tree vec = NULL_TREE;
10888 tree expanded_types = NULL_TREE;
10889 int len = 1;
10891 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10893 /* This field is a lambda capture pack. Return a TREE_VEC of
10894 the expanded fields to instantiate_class_template_1 and
10895 store them in the specializations hash table as a
10896 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10897 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10898 complain, in_decl);
10899 if (TREE_CODE (expanded_types) == TREE_VEC)
10901 len = TREE_VEC_LENGTH (expanded_types);
10902 vec = make_tree_vec (len);
10904 else
10906 /* All we did was update the type. Make a note of that. */
10907 type = expanded_types;
10908 expanded_types = NULL_TREE;
10912 for (int i = 0; i < len; ++i)
10914 r = copy_decl (t);
10915 if (expanded_types)
10917 type = TREE_VEC_ELT (expanded_types, i);
10918 DECL_NAME (r)
10919 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10921 else if (!type)
10922 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10924 if (type == error_mark_node)
10925 RETURN (error_mark_node);
10926 TREE_TYPE (r) = type;
10927 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10929 if (DECL_C_BIT_FIELD (r))
10930 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10931 non-bit-fields DECL_INITIAL is a non-static data member
10932 initializer, which gets deferred instantiation. */
10933 DECL_INITIAL (r)
10934 = tsubst_expr (DECL_INITIAL (t), args,
10935 complain, in_decl,
10936 /*integral_constant_expression_p=*/true);
10937 else if (DECL_INITIAL (t))
10939 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10940 NSDMI in perform_member_init. Still set DECL_INITIAL
10941 so that we know there is one. */
10942 DECL_INITIAL (r) = void_zero_node;
10943 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10944 retrofit_lang_decl (r);
10945 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10947 /* We don't have to set DECL_CONTEXT here; it is set by
10948 finish_member_declaration. */
10949 DECL_CHAIN (r) = NULL_TREE;
10951 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10952 args, complain, in_decl);
10954 if (vec)
10955 TREE_VEC_ELT (vec, i) = r;
10958 if (vec)
10960 r = vec;
10961 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10962 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10963 SET_ARGUMENT_PACK_ARGS (pack, vec);
10964 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10965 TREE_TYPE (pack) = tpack;
10966 register_specialization (pack, t, args, false, 0);
10969 break;
10971 case USING_DECL:
10972 /* We reach here only for member using decls. We also need to check
10973 uses_template_parms because DECL_DEPENDENT_P is not set for a
10974 using-declaration that designates a member of the current
10975 instantiation (c++/53549). */
10976 if (DECL_DEPENDENT_P (t)
10977 || uses_template_parms (USING_DECL_SCOPE (t)))
10979 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10980 complain, in_decl);
10981 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10982 r = do_class_using_decl (inst_scope, name);
10983 if (!r)
10984 r = error_mark_node;
10985 else
10987 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10988 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10991 else
10993 r = copy_node (t);
10994 DECL_CHAIN (r) = NULL_TREE;
10996 break;
10998 case TYPE_DECL:
10999 case VAR_DECL:
11001 tree argvec = NULL_TREE;
11002 tree gen_tmpl = NULL_TREE;
11003 tree spec;
11004 tree tmpl = NULL_TREE;
11005 tree ctx;
11006 tree type = NULL_TREE;
11007 bool local_p;
11009 if (TREE_TYPE (t) == error_mark_node)
11010 RETURN (error_mark_node);
11012 if (TREE_CODE (t) == TYPE_DECL
11013 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11015 /* If this is the canonical decl, we don't have to
11016 mess with instantiations, and often we can't (for
11017 typename, template type parms and such). Note that
11018 TYPE_NAME is not correct for the above test if
11019 we've copied the type for a typedef. */
11020 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11021 if (type == error_mark_node)
11022 RETURN (error_mark_node);
11023 r = TYPE_NAME (type);
11024 break;
11027 /* Check to see if we already have the specialization we
11028 need. */
11029 spec = NULL_TREE;
11030 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11032 /* T is a static data member or namespace-scope entity.
11033 We have to substitute into namespace-scope variables
11034 (even though such entities are never templates) because
11035 of cases like:
11037 template <class T> void f() { extern T t; }
11039 where the entity referenced is not known until
11040 instantiation time. */
11041 local_p = false;
11042 ctx = DECL_CONTEXT (t);
11043 if (DECL_CLASS_SCOPE_P (t))
11045 ctx = tsubst_aggr_type (ctx, args,
11046 complain,
11047 in_decl, /*entering_scope=*/1);
11048 /* If CTX is unchanged, then T is in fact the
11049 specialization we want. That situation occurs when
11050 referencing a static data member within in its own
11051 class. We can use pointer equality, rather than
11052 same_type_p, because DECL_CONTEXT is always
11053 canonical... */
11054 if (ctx == DECL_CONTEXT (t)
11055 && (TREE_CODE (t) != TYPE_DECL
11056 /* ... unless T is a member template; in which
11057 case our caller can be willing to create a
11058 specialization of that template represented
11059 by T. */
11060 || !(DECL_TI_TEMPLATE (t)
11061 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
11062 spec = t;
11065 if (!spec)
11067 tmpl = DECL_TI_TEMPLATE (t);
11068 gen_tmpl = most_general_template (tmpl);
11069 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11070 if (argvec == error_mark_node)
11071 RETURN (error_mark_node);
11072 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11073 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11076 else
11078 /* A local variable. */
11079 local_p = true;
11080 /* Subsequent calls to pushdecl will fill this in. */
11081 ctx = NULL_TREE;
11082 spec = retrieve_local_specialization (t);
11084 /* If we already have the specialization we need, there is
11085 nothing more to do. */
11086 if (spec)
11088 r = spec;
11089 break;
11092 /* Create a new node for the specialization we need. */
11093 r = copy_decl (t);
11094 if (type == NULL_TREE)
11096 if (is_typedef_decl (t))
11097 type = DECL_ORIGINAL_TYPE (t);
11098 else
11099 type = TREE_TYPE (t);
11100 if (VAR_P (t)
11101 && VAR_HAD_UNKNOWN_BOUND (t)
11102 && type != error_mark_node)
11103 type = strip_array_domain (type);
11104 type = tsubst (type, args, complain, in_decl);
11106 if (VAR_P (r))
11108 /* Even if the original location is out of scope, the
11109 newly substituted one is not. */
11110 DECL_DEAD_FOR_LOCAL (r) = 0;
11111 DECL_INITIALIZED_P (r) = 0;
11112 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11113 if (type == error_mark_node)
11114 RETURN (error_mark_node);
11115 if (TREE_CODE (type) == FUNCTION_TYPE)
11117 /* It may seem that this case cannot occur, since:
11119 typedef void f();
11120 void g() { f x; }
11122 declares a function, not a variable. However:
11124 typedef void f();
11125 template <typename T> void g() { T t; }
11126 template void g<f>();
11128 is an attempt to declare a variable with function
11129 type. */
11130 error ("variable %qD has function type",
11131 /* R is not yet sufficiently initialized, so we
11132 just use its name. */
11133 DECL_NAME (r));
11134 RETURN (error_mark_node);
11136 type = complete_type (type);
11137 /* Wait until cp_finish_decl to set this again, to handle
11138 circular dependency (template/instantiate6.C). */
11139 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11140 type = check_var_type (DECL_NAME (r), type);
11142 if (DECL_HAS_VALUE_EXPR_P (t))
11144 tree ve = DECL_VALUE_EXPR (t);
11145 ve = tsubst_expr (ve, args, complain, in_decl,
11146 /*constant_expression_p=*/false);
11147 if (REFERENCE_REF_P (ve))
11149 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11150 ve = TREE_OPERAND (ve, 0);
11152 SET_DECL_VALUE_EXPR (r, ve);
11155 else if (DECL_SELF_REFERENCE_P (t))
11156 SET_DECL_SELF_REFERENCE_P (r);
11157 TREE_TYPE (r) = type;
11158 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11159 DECL_CONTEXT (r) = ctx;
11160 /* Clear out the mangled name and RTL for the instantiation. */
11161 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11162 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11163 SET_DECL_RTL (r, NULL);
11164 /* The initializer must not be expanded until it is required;
11165 see [temp.inst]. */
11166 DECL_INITIAL (r) = NULL_TREE;
11167 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11168 SET_DECL_RTL (r, NULL);
11169 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11170 if (VAR_P (r))
11172 /* Possibly limit visibility based on template args. */
11173 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11174 if (DECL_VISIBILITY_SPECIFIED (t))
11176 DECL_VISIBILITY_SPECIFIED (r) = 0;
11177 DECL_ATTRIBUTES (r)
11178 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11180 determine_visibility (r);
11183 if (!local_p)
11185 /* A static data member declaration is always marked
11186 external when it is declared in-class, even if an
11187 initializer is present. We mimic the non-template
11188 processing here. */
11189 DECL_EXTERNAL (r) = 1;
11191 register_specialization (r, gen_tmpl, argvec, false, hash);
11192 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11193 SET_DECL_IMPLICIT_INSTANTIATION (r);
11195 else if (!cp_unevaluated_operand)
11196 register_local_specialization (r, t);
11198 DECL_CHAIN (r) = NULL_TREE;
11200 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11201 /*flags=*/0,
11202 args, complain, in_decl);
11204 /* Preserve a typedef that names a type. */
11205 if (is_typedef_decl (r))
11207 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11208 set_underlying_type (r);
11211 layout_decl (r, 0);
11213 break;
11215 default:
11216 gcc_unreachable ();
11218 #undef RETURN
11220 out:
11221 /* Restore the file and line information. */
11222 input_location = saved_loc;
11224 return r;
11227 /* Substitute into the ARG_TYPES of a function type.
11228 If END is a TREE_CHAIN, leave it and any following types
11229 un-substituted. */
11231 static tree
11232 tsubst_arg_types (tree arg_types,
11233 tree args,
11234 tree end,
11235 tsubst_flags_t complain,
11236 tree in_decl)
11238 tree remaining_arg_types;
11239 tree type = NULL_TREE;
11240 int i = 1;
11241 tree expanded_args = NULL_TREE;
11242 tree default_arg;
11244 if (!arg_types || arg_types == void_list_node || arg_types == end)
11245 return arg_types;
11247 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11248 args, end, complain, in_decl);
11249 if (remaining_arg_types == error_mark_node)
11250 return error_mark_node;
11252 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11254 /* For a pack expansion, perform substitution on the
11255 entire expression. Later on, we'll handle the arguments
11256 one-by-one. */
11257 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11258 args, complain, in_decl);
11260 if (TREE_CODE (expanded_args) == TREE_VEC)
11261 /* So that we'll spin through the parameters, one by one. */
11262 i = TREE_VEC_LENGTH (expanded_args);
11263 else
11265 /* We only partially substituted into the parameter
11266 pack. Our type is TYPE_PACK_EXPANSION. */
11267 type = expanded_args;
11268 expanded_args = NULL_TREE;
11272 while (i > 0) {
11273 --i;
11275 if (expanded_args)
11276 type = TREE_VEC_ELT (expanded_args, i);
11277 else if (!type)
11278 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11280 if (type == error_mark_node)
11281 return error_mark_node;
11282 if (VOID_TYPE_P (type))
11284 if (complain & tf_error)
11286 error ("invalid parameter type %qT", type);
11287 if (in_decl)
11288 error ("in declaration %q+D", in_decl);
11290 return error_mark_node;
11292 /* DR 657. */
11293 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11294 return error_mark_node;
11296 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11297 top-level qualifiers as required. */
11298 type = cv_unqualified (type_decays_to (type));
11300 /* We do not substitute into default arguments here. The standard
11301 mandates that they be instantiated only when needed, which is
11302 done in build_over_call. */
11303 default_arg = TREE_PURPOSE (arg_types);
11305 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11307 /* We've instantiated a template before its default arguments
11308 have been parsed. This can happen for a nested template
11309 class, and is not an error unless we require the default
11310 argument in a call of this function. */
11311 remaining_arg_types =
11312 tree_cons (default_arg, type, remaining_arg_types);
11313 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11315 else
11316 remaining_arg_types =
11317 hash_tree_cons (default_arg, type, remaining_arg_types);
11320 return remaining_arg_types;
11323 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11324 *not* handle the exception-specification for FNTYPE, because the
11325 initial substitution of explicitly provided template parameters
11326 during argument deduction forbids substitution into the
11327 exception-specification:
11329 [temp.deduct]
11331 All references in the function type of the function template to the
11332 corresponding template parameters are replaced by the specified tem-
11333 plate argument values. If a substitution in a template parameter or
11334 in the function type of the function template results in an invalid
11335 type, type deduction fails. [Note: The equivalent substitution in
11336 exception specifications is done only when the function is instanti-
11337 ated, at which point a program is ill-formed if the substitution
11338 results in an invalid type.] */
11340 static tree
11341 tsubst_function_type (tree t,
11342 tree args,
11343 tsubst_flags_t complain,
11344 tree in_decl)
11346 tree return_type;
11347 tree arg_types;
11348 tree fntype;
11350 /* The TYPE_CONTEXT is not used for function/method types. */
11351 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11353 /* Substitute the return type. */
11354 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11355 if (return_type == error_mark_node)
11356 return error_mark_node;
11357 /* DR 486 clarifies that creation of a function type with an
11358 invalid return type is a deduction failure. */
11359 if (TREE_CODE (return_type) == ARRAY_TYPE
11360 || TREE_CODE (return_type) == FUNCTION_TYPE)
11362 if (complain & tf_error)
11364 if (TREE_CODE (return_type) == ARRAY_TYPE)
11365 error ("function returning an array");
11366 else
11367 error ("function returning a function");
11369 return error_mark_node;
11371 /* And DR 657. */
11372 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11373 return error_mark_node;
11375 /* Substitute the argument types. */
11376 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11377 complain, in_decl);
11378 if (arg_types == error_mark_node)
11379 return error_mark_node;
11381 /* Construct a new type node and return it. */
11382 if (TREE_CODE (t) == FUNCTION_TYPE)
11384 fntype = build_function_type (return_type, arg_types);
11385 fntype = apply_memfn_quals (fntype,
11386 type_memfn_quals (t),
11387 type_memfn_rqual (t));
11389 else
11391 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11392 /* Don't pick up extra function qualifiers from the basetype. */
11393 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11394 if (! MAYBE_CLASS_TYPE_P (r))
11396 /* [temp.deduct]
11398 Type deduction may fail for any of the following
11399 reasons:
11401 -- Attempting to create "pointer to member of T" when T
11402 is not a class type. */
11403 if (complain & tf_error)
11404 error ("creating pointer to member function of non-class type %qT",
11406 return error_mark_node;
11409 fntype = build_method_type_directly (r, return_type,
11410 TREE_CHAIN (arg_types));
11411 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11413 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11415 return fntype;
11418 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11419 ARGS into that specification, and return the substituted
11420 specification. If there is no specification, return NULL_TREE. */
11422 static tree
11423 tsubst_exception_specification (tree fntype,
11424 tree args,
11425 tsubst_flags_t complain,
11426 tree in_decl,
11427 bool defer_ok)
11429 tree specs;
11430 tree new_specs;
11432 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11433 new_specs = NULL_TREE;
11434 if (specs && TREE_PURPOSE (specs))
11436 /* A noexcept-specifier. */
11437 tree expr = TREE_PURPOSE (specs);
11438 if (TREE_CODE (expr) == INTEGER_CST)
11439 new_specs = expr;
11440 else if (defer_ok)
11442 /* Defer instantiation of noexcept-specifiers to avoid
11443 excessive instantiations (c++/49107). */
11444 new_specs = make_node (DEFERRED_NOEXCEPT);
11445 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11447 /* We already partially instantiated this member template,
11448 so combine the new args with the old. */
11449 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11450 = DEFERRED_NOEXCEPT_PATTERN (expr);
11451 DEFERRED_NOEXCEPT_ARGS (new_specs)
11452 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11454 else
11456 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11457 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11460 else
11461 new_specs = tsubst_copy_and_build
11462 (expr, args, complain, in_decl, /*function_p=*/false,
11463 /*integral_constant_expression_p=*/true);
11464 new_specs = build_noexcept_spec (new_specs, complain);
11466 else if (specs)
11468 if (! TREE_VALUE (specs))
11469 new_specs = specs;
11470 else
11471 while (specs)
11473 tree spec;
11474 int i, len = 1;
11475 tree expanded_specs = NULL_TREE;
11477 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11479 /* Expand the pack expansion type. */
11480 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11481 args, complain,
11482 in_decl);
11484 if (expanded_specs == error_mark_node)
11485 return error_mark_node;
11486 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11487 len = TREE_VEC_LENGTH (expanded_specs);
11488 else
11490 /* We're substituting into a member template, so
11491 we got a TYPE_PACK_EXPANSION back. Add that
11492 expansion and move on. */
11493 gcc_assert (TREE_CODE (expanded_specs)
11494 == TYPE_PACK_EXPANSION);
11495 new_specs = add_exception_specifier (new_specs,
11496 expanded_specs,
11497 complain);
11498 specs = TREE_CHAIN (specs);
11499 continue;
11503 for (i = 0; i < len; ++i)
11505 if (expanded_specs)
11506 spec = TREE_VEC_ELT (expanded_specs, i);
11507 else
11508 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11509 if (spec == error_mark_node)
11510 return spec;
11511 new_specs = add_exception_specifier (new_specs, spec,
11512 complain);
11515 specs = TREE_CHAIN (specs);
11518 return new_specs;
11521 /* Take the tree structure T and replace template parameters used
11522 therein with the argument vector ARGS. IN_DECL is an associated
11523 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11524 Issue error and warning messages under control of COMPLAIN. Note
11525 that we must be relatively non-tolerant of extensions here, in
11526 order to preserve conformance; if we allow substitutions that
11527 should not be allowed, we may allow argument deductions that should
11528 not succeed, and therefore report ambiguous overload situations
11529 where there are none. In theory, we could allow the substitution,
11530 but indicate that it should have failed, and allow our caller to
11531 make sure that the right thing happens, but we don't try to do this
11532 yet.
11534 This function is used for dealing with types, decls and the like;
11535 for expressions, use tsubst_expr or tsubst_copy. */
11537 tree
11538 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11540 enum tree_code code;
11541 tree type, r = NULL_TREE;
11543 if (t == NULL_TREE || t == error_mark_node
11544 || t == integer_type_node
11545 || t == void_type_node
11546 || t == char_type_node
11547 || t == unknown_type_node
11548 || TREE_CODE (t) == NAMESPACE_DECL
11549 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11550 return t;
11552 if (DECL_P (t))
11553 return tsubst_decl (t, args, complain);
11555 if (args == NULL_TREE)
11556 return t;
11558 code = TREE_CODE (t);
11560 if (code == IDENTIFIER_NODE)
11561 type = IDENTIFIER_TYPE_VALUE (t);
11562 else
11563 type = TREE_TYPE (t);
11565 gcc_assert (type != unknown_type_node);
11567 /* Reuse typedefs. We need to do this to handle dependent attributes,
11568 such as attribute aligned. */
11569 if (TYPE_P (t)
11570 && typedef_variant_p (t))
11572 tree decl = TYPE_NAME (t);
11574 if (alias_template_specialization_p (t))
11576 /* DECL represents an alias template and we want to
11577 instantiate it. */
11578 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11579 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11580 r = instantiate_alias_template (tmpl, gen_args, complain);
11582 else if (DECL_CLASS_SCOPE_P (decl)
11583 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11584 && uses_template_parms (DECL_CONTEXT (decl)))
11586 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11587 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11588 r = retrieve_specialization (tmpl, gen_args, 0);
11590 else if (DECL_FUNCTION_SCOPE_P (decl)
11591 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11592 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11593 r = retrieve_local_specialization (decl);
11594 else
11595 /* The typedef is from a non-template context. */
11596 return t;
11598 if (r)
11600 r = TREE_TYPE (r);
11601 r = cp_build_qualified_type_real
11602 (r, cp_type_quals (t) | cp_type_quals (r),
11603 complain | tf_ignore_bad_quals);
11604 return r;
11606 else
11608 /* We don't have an instantiation yet, so drop the typedef. */
11609 int quals = cp_type_quals (t);
11610 t = DECL_ORIGINAL_TYPE (decl);
11611 t = cp_build_qualified_type_real (t, quals,
11612 complain | tf_ignore_bad_quals);
11616 if (type
11617 && code != TYPENAME_TYPE
11618 && code != TEMPLATE_TYPE_PARM
11619 && code != IDENTIFIER_NODE
11620 && code != FUNCTION_TYPE
11621 && code != METHOD_TYPE)
11622 type = tsubst (type, args, complain, in_decl);
11623 if (type == error_mark_node)
11624 return error_mark_node;
11626 switch (code)
11628 case RECORD_TYPE:
11629 case UNION_TYPE:
11630 case ENUMERAL_TYPE:
11631 return tsubst_aggr_type (t, args, complain, in_decl,
11632 /*entering_scope=*/0);
11634 case ERROR_MARK:
11635 case IDENTIFIER_NODE:
11636 case VOID_TYPE:
11637 case REAL_TYPE:
11638 case COMPLEX_TYPE:
11639 case VECTOR_TYPE:
11640 case BOOLEAN_TYPE:
11641 case NULLPTR_TYPE:
11642 case LANG_TYPE:
11643 return t;
11645 case INTEGER_TYPE:
11646 if (t == integer_type_node)
11647 return t;
11649 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11650 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11651 return t;
11654 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11656 max = tsubst_expr (omax, args, complain, in_decl,
11657 /*integral_constant_expression_p=*/false);
11659 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11660 needed. */
11661 if (TREE_CODE (max) == NOP_EXPR
11662 && TREE_SIDE_EFFECTS (omax)
11663 && !TREE_TYPE (max))
11664 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11666 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11667 with TREE_SIDE_EFFECTS that indicates this is not an integral
11668 constant expression. */
11669 if (processing_template_decl
11670 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11672 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11673 TREE_SIDE_EFFECTS (max) = 1;
11676 return compute_array_index_type (NULL_TREE, max, complain);
11679 case TEMPLATE_TYPE_PARM:
11680 case TEMPLATE_TEMPLATE_PARM:
11681 case BOUND_TEMPLATE_TEMPLATE_PARM:
11682 case TEMPLATE_PARM_INDEX:
11684 int idx;
11685 int level;
11686 int levels;
11687 tree arg = NULL_TREE;
11689 r = NULL_TREE;
11691 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11692 template_parm_level_and_index (t, &level, &idx);
11694 levels = TMPL_ARGS_DEPTH (args);
11695 if (level <= levels)
11697 arg = TMPL_ARG (args, level, idx);
11699 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11701 /* See through ARGUMENT_PACK_SELECT arguments. */
11702 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11703 /* If the selected argument is an expansion E, that most
11704 likely means we were called from
11705 gen_elem_of_pack_expansion_instantiation during the
11706 substituting of pack an argument pack (which Ith
11707 element is a pack expansion, where I is
11708 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11709 In this case, the Ith element resulting from this
11710 substituting is going to be a pack expansion, which
11711 pattern is the pattern of E. Let's return the
11712 pattern of E, and
11713 gen_elem_of_pack_expansion_instantiation will
11714 build the resulting pack expansion from it. */
11715 if (PACK_EXPANSION_P (arg))
11716 arg = PACK_EXPANSION_PATTERN (arg);
11720 if (arg == error_mark_node)
11721 return error_mark_node;
11722 else if (arg != NULL_TREE)
11724 if (ARGUMENT_PACK_P (arg))
11725 /* If ARG is an argument pack, we don't actually want to
11726 perform a substitution here, because substitutions
11727 for argument packs are only done
11728 element-by-element. We can get to this point when
11729 substituting the type of a non-type template
11730 parameter pack, when that type actually contains
11731 template parameter packs from an outer template, e.g.,
11733 template<typename... Types> struct A {
11734 template<Types... Values> struct B { };
11735 }; */
11736 return t;
11738 if (code == TEMPLATE_TYPE_PARM)
11740 int quals;
11741 gcc_assert (TYPE_P (arg));
11743 quals = cp_type_quals (arg) | cp_type_quals (t);
11745 return cp_build_qualified_type_real
11746 (arg, quals, complain | tf_ignore_bad_quals);
11748 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11750 /* We are processing a type constructed from a
11751 template template parameter. */
11752 tree argvec = tsubst (TYPE_TI_ARGS (t),
11753 args, complain, in_decl);
11754 if (argvec == error_mark_node)
11755 return error_mark_node;
11757 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11758 || TREE_CODE (arg) == TEMPLATE_DECL
11759 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11761 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11762 /* Consider this code:
11764 template <template <class> class Template>
11765 struct Internal {
11766 template <class Arg> using Bind = Template<Arg>;
11769 template <template <class> class Template, class Arg>
11770 using Instantiate = Template<Arg>; //#0
11772 template <template <class> class Template,
11773 class Argument>
11774 using Bind =
11775 Instantiate<Internal<Template>::template Bind,
11776 Argument>; //#1
11778 When #1 is parsed, the
11779 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11780 parameter `Template' in #0 matches the
11781 UNBOUND_CLASS_TEMPLATE representing the argument
11782 `Internal<Template>::template Bind'; We then want
11783 to assemble the type `Bind<Argument>' that can't
11784 be fully created right now, because
11785 `Internal<Template>' not being complete, the Bind
11786 template cannot be looked up in that context. So
11787 we need to "store" `Bind<Argument>' for later
11788 when the context of Bind becomes complete. Let's
11789 store that in a TYPENAME_TYPE. */
11790 return make_typename_type (TYPE_CONTEXT (arg),
11791 build_nt (TEMPLATE_ID_EXPR,
11792 TYPE_IDENTIFIER (arg),
11793 argvec),
11794 typename_type,
11795 complain);
11797 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11798 are resolving nested-types in the signature of a
11799 member function templates. Otherwise ARG is a
11800 TEMPLATE_DECL and is the real template to be
11801 instantiated. */
11802 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11803 arg = TYPE_NAME (arg);
11805 r = lookup_template_class (arg,
11806 argvec, in_decl,
11807 DECL_CONTEXT (arg),
11808 /*entering_scope=*/0,
11809 complain);
11810 return cp_build_qualified_type_real
11811 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11813 else
11814 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11815 return convert_from_reference (unshare_expr (arg));
11818 if (level == 1)
11819 /* This can happen during the attempted tsubst'ing in
11820 unify. This means that we don't yet have any information
11821 about the template parameter in question. */
11822 return t;
11824 /* Early in template argument deduction substitution, we don't
11825 want to reduce the level of 'auto', or it will be confused
11826 with a normal template parm in subsequent deduction. */
11827 if (is_auto (t) && (complain & tf_partial))
11828 return t;
11830 /* If we get here, we must have been looking at a parm for a
11831 more deeply nested template. Make a new version of this
11832 template parameter, but with a lower level. */
11833 switch (code)
11835 case TEMPLATE_TYPE_PARM:
11836 case TEMPLATE_TEMPLATE_PARM:
11837 case BOUND_TEMPLATE_TEMPLATE_PARM:
11838 if (cp_type_quals (t))
11840 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11841 r = cp_build_qualified_type_real
11842 (r, cp_type_quals (t),
11843 complain | (code == TEMPLATE_TYPE_PARM
11844 ? tf_ignore_bad_quals : 0));
11846 else
11848 r = copy_type (t);
11849 TEMPLATE_TYPE_PARM_INDEX (r)
11850 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11851 r, levels, args, complain);
11852 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11853 TYPE_MAIN_VARIANT (r) = r;
11854 TYPE_POINTER_TO (r) = NULL_TREE;
11855 TYPE_REFERENCE_TO (r) = NULL_TREE;
11857 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11858 /* We have reduced the level of the template
11859 template parameter, but not the levels of its
11860 template parameters, so canonical_type_parameter
11861 will not be able to find the canonical template
11862 template parameter for this level. Thus, we
11863 require structural equality checking to compare
11864 TEMPLATE_TEMPLATE_PARMs. */
11865 SET_TYPE_STRUCTURAL_EQUALITY (r);
11866 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11867 SET_TYPE_STRUCTURAL_EQUALITY (r);
11868 else
11869 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11871 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11873 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11874 complain, in_decl);
11875 if (argvec == error_mark_node)
11876 return error_mark_node;
11878 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11879 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11882 break;
11884 case TEMPLATE_PARM_INDEX:
11885 r = reduce_template_parm_level (t, type, levels, args, complain);
11886 break;
11888 default:
11889 gcc_unreachable ();
11892 return r;
11895 case TREE_LIST:
11897 tree purpose, value, chain;
11899 if (t == void_list_node)
11900 return t;
11902 purpose = TREE_PURPOSE (t);
11903 if (purpose)
11905 purpose = tsubst (purpose, args, complain, in_decl);
11906 if (purpose == error_mark_node)
11907 return error_mark_node;
11909 value = TREE_VALUE (t);
11910 if (value)
11912 value = tsubst (value, args, complain, in_decl);
11913 if (value == error_mark_node)
11914 return error_mark_node;
11916 chain = TREE_CHAIN (t);
11917 if (chain && chain != void_type_node)
11919 chain = tsubst (chain, args, complain, in_decl);
11920 if (chain == error_mark_node)
11921 return error_mark_node;
11923 if (purpose == TREE_PURPOSE (t)
11924 && value == TREE_VALUE (t)
11925 && chain == TREE_CHAIN (t))
11926 return t;
11927 return hash_tree_cons (purpose, value, chain);
11930 case TREE_BINFO:
11931 /* We should never be tsubsting a binfo. */
11932 gcc_unreachable ();
11934 case TREE_VEC:
11935 /* A vector of template arguments. */
11936 gcc_assert (!type);
11937 return tsubst_template_args (t, args, complain, in_decl);
11939 case POINTER_TYPE:
11940 case REFERENCE_TYPE:
11942 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11943 return t;
11945 /* [temp.deduct]
11947 Type deduction may fail for any of the following
11948 reasons:
11950 -- Attempting to create a pointer to reference type.
11951 -- Attempting to create a reference to a reference type or
11952 a reference to void.
11954 Core issue 106 says that creating a reference to a reference
11955 during instantiation is no longer a cause for failure. We
11956 only enforce this check in strict C++98 mode. */
11957 if ((TREE_CODE (type) == REFERENCE_TYPE
11958 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11959 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11961 static location_t last_loc;
11963 /* We keep track of the last time we issued this error
11964 message to avoid spewing a ton of messages during a
11965 single bad template instantiation. */
11966 if (complain & tf_error
11967 && last_loc != input_location)
11969 if (VOID_TYPE_P (type))
11970 error ("forming reference to void");
11971 else if (code == POINTER_TYPE)
11972 error ("forming pointer to reference type %qT", type);
11973 else
11974 error ("forming reference to reference type %qT", type);
11975 last_loc = input_location;
11978 return error_mark_node;
11980 else if (TREE_CODE (type) == FUNCTION_TYPE
11981 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11982 || type_memfn_rqual (type) != REF_QUAL_NONE))
11984 if (complain & tf_error)
11986 if (code == POINTER_TYPE)
11987 error ("forming pointer to qualified function type %qT",
11988 type);
11989 else
11990 error ("forming reference to qualified function type %qT",
11991 type);
11993 return error_mark_node;
11995 else if (code == POINTER_TYPE)
11997 r = build_pointer_type (type);
11998 if (TREE_CODE (type) == METHOD_TYPE)
11999 r = build_ptrmemfunc_type (r);
12001 else if (TREE_CODE (type) == REFERENCE_TYPE)
12002 /* In C++0x, during template argument substitution, when there is an
12003 attempt to create a reference to a reference type, reference
12004 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12006 "If a template-argument for a template-parameter T names a type
12007 that is a reference to a type A, an attempt to create the type
12008 'lvalue reference to cv T' creates the type 'lvalue reference to
12009 A,' while an attempt to create the type type rvalue reference to
12010 cv T' creates the type T"
12012 r = cp_build_reference_type
12013 (TREE_TYPE (type),
12014 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12015 else
12016 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12017 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12019 if (cxx_dialect >= cxx1y
12020 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
12021 && array_of_runtime_bound_p (type)
12022 && (flag_iso || warn_vla > 0))
12024 if (complain & tf_warning_or_error)
12025 pedwarn
12026 (input_location, OPT_Wvla,
12027 code == REFERENCE_TYPE
12028 ? G_("cannot declare reference to array of runtime bound")
12029 : G_("cannot declare pointer to array of runtime bound"));
12030 else
12031 r = error_mark_node;
12034 if (r != error_mark_node)
12035 /* Will this ever be needed for TYPE_..._TO values? */
12036 layout_type (r);
12038 return r;
12040 case OFFSET_TYPE:
12042 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12043 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12045 /* [temp.deduct]
12047 Type deduction may fail for any of the following
12048 reasons:
12050 -- Attempting to create "pointer to member of T" when T
12051 is not a class type. */
12052 if (complain & tf_error)
12053 error ("creating pointer to member of non-class type %qT", r);
12054 return error_mark_node;
12056 if (TREE_CODE (type) == REFERENCE_TYPE)
12058 if (complain & tf_error)
12059 error ("creating pointer to member reference type %qT", type);
12060 return error_mark_node;
12062 if (VOID_TYPE_P (type))
12064 if (complain & tf_error)
12065 error ("creating pointer to member of type void");
12066 return error_mark_node;
12068 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12069 if (TREE_CODE (type) == FUNCTION_TYPE)
12071 /* The type of the implicit object parameter gets its
12072 cv-qualifiers from the FUNCTION_TYPE. */
12073 tree memptr;
12074 tree method_type
12075 = build_memfn_type (type, r, type_memfn_quals (type),
12076 type_memfn_rqual (type));
12077 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12078 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12079 complain);
12081 else
12082 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12083 cp_type_quals (t),
12084 complain);
12086 case FUNCTION_TYPE:
12087 case METHOD_TYPE:
12089 tree fntype;
12090 tree specs;
12091 fntype = tsubst_function_type (t, args, complain, in_decl);
12092 if (fntype == error_mark_node)
12093 return error_mark_node;
12095 /* Substitute the exception specification. */
12096 specs = tsubst_exception_specification (t, args, complain,
12097 in_decl, /*defer_ok*/true);
12098 if (specs == error_mark_node)
12099 return error_mark_node;
12100 if (specs)
12101 fntype = build_exception_variant (fntype, specs);
12102 return fntype;
12104 case ARRAY_TYPE:
12106 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12107 if (domain == error_mark_node)
12108 return error_mark_node;
12110 /* As an optimization, we avoid regenerating the array type if
12111 it will obviously be the same as T. */
12112 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12113 return t;
12115 /* These checks should match the ones in grokdeclarator.
12117 [temp.deduct]
12119 The deduction may fail for any of the following reasons:
12121 -- Attempting to create an array with an element type that
12122 is void, a function type, or a reference type, or [DR337]
12123 an abstract class type. */
12124 if (VOID_TYPE_P (type)
12125 || TREE_CODE (type) == FUNCTION_TYPE
12126 || TREE_CODE (type) == REFERENCE_TYPE)
12128 if (complain & tf_error)
12129 error ("creating array of %qT", type);
12130 return error_mark_node;
12133 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12134 return error_mark_node;
12136 r = build_cplus_array_type (type, domain);
12138 if (TYPE_USER_ALIGN (t))
12140 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12141 TYPE_USER_ALIGN (r) = 1;
12144 return r;
12147 case TYPENAME_TYPE:
12149 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12150 in_decl, /*entering_scope=*/1);
12151 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12152 complain, in_decl);
12154 if (ctx == error_mark_node || f == error_mark_node)
12155 return error_mark_node;
12157 if (!MAYBE_CLASS_TYPE_P (ctx))
12159 if (complain & tf_error)
12160 error ("%qT is not a class, struct, or union type", ctx);
12161 return error_mark_node;
12163 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12165 /* Normally, make_typename_type does not require that the CTX
12166 have complete type in order to allow things like:
12168 template <class T> struct S { typename S<T>::X Y; };
12170 But, such constructs have already been resolved by this
12171 point, so here CTX really should have complete type, unless
12172 it's a partial instantiation. */
12173 ctx = complete_type (ctx);
12174 if (!COMPLETE_TYPE_P (ctx))
12176 if (complain & tf_error)
12177 cxx_incomplete_type_error (NULL_TREE, ctx);
12178 return error_mark_node;
12182 f = make_typename_type (ctx, f, typename_type,
12183 complain | tf_keep_type_decl);
12184 if (f == error_mark_node)
12185 return f;
12186 if (TREE_CODE (f) == TYPE_DECL)
12188 complain |= tf_ignore_bad_quals;
12189 f = TREE_TYPE (f);
12192 if (TREE_CODE (f) != TYPENAME_TYPE)
12194 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12196 if (complain & tf_error)
12197 error ("%qT resolves to %qT, which is not an enumeration type",
12198 t, f);
12199 else
12200 return error_mark_node;
12202 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12204 if (complain & tf_error)
12205 error ("%qT resolves to %qT, which is is not a class type",
12206 t, f);
12207 else
12208 return error_mark_node;
12212 return cp_build_qualified_type_real
12213 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12216 case UNBOUND_CLASS_TEMPLATE:
12218 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12219 in_decl, /*entering_scope=*/1);
12220 tree name = TYPE_IDENTIFIER (t);
12221 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12223 if (ctx == error_mark_node || name == error_mark_node)
12224 return error_mark_node;
12226 if (parm_list)
12227 parm_list = tsubst_template_parms (parm_list, args, complain);
12228 return make_unbound_class_template (ctx, name, parm_list, complain);
12231 case TYPEOF_TYPE:
12233 tree type;
12235 ++cp_unevaluated_operand;
12236 ++c_inhibit_evaluation_warnings;
12238 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12239 complain, in_decl,
12240 /*integral_constant_expression_p=*/false);
12242 --cp_unevaluated_operand;
12243 --c_inhibit_evaluation_warnings;
12245 type = finish_typeof (type);
12246 return cp_build_qualified_type_real (type,
12247 cp_type_quals (t)
12248 | cp_type_quals (type),
12249 complain);
12252 case DECLTYPE_TYPE:
12254 tree type;
12256 ++cp_unevaluated_operand;
12257 ++c_inhibit_evaluation_warnings;
12259 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12260 complain|tf_decltype, in_decl,
12261 /*function_p*/false,
12262 /*integral_constant_expression*/false);
12264 --cp_unevaluated_operand;
12265 --c_inhibit_evaluation_warnings;
12267 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12268 type = lambda_capture_field_type (type,
12269 DECLTYPE_FOR_INIT_CAPTURE (t));
12270 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12271 type = lambda_proxy_type (type);
12272 else
12274 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12275 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12276 && EXPR_P (type))
12277 /* In a template ~id could be either a complement expression
12278 or an unqualified-id naming a destructor; if instantiating
12279 it produces an expression, it's not an id-expression or
12280 member access. */
12281 id = false;
12282 type = finish_decltype_type (type, id, complain);
12284 return cp_build_qualified_type_real (type,
12285 cp_type_quals (t)
12286 | cp_type_quals (type),
12287 complain);
12290 case UNDERLYING_TYPE:
12292 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12293 complain, in_decl);
12294 return finish_underlying_type (type);
12297 case TYPE_ARGUMENT_PACK:
12298 case NONTYPE_ARGUMENT_PACK:
12300 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12301 tree packed_out =
12302 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12303 args,
12304 complain,
12305 in_decl);
12306 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12308 /* For template nontype argument packs, also substitute into
12309 the type. */
12310 if (code == NONTYPE_ARGUMENT_PACK)
12311 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12313 return r;
12315 break;
12317 case INTEGER_CST:
12318 case REAL_CST:
12319 case STRING_CST:
12320 case PLUS_EXPR:
12321 case MINUS_EXPR:
12322 case NEGATE_EXPR:
12323 case NOP_EXPR:
12324 case INDIRECT_REF:
12325 case ADDR_EXPR:
12326 case CALL_EXPR:
12327 case ARRAY_REF:
12328 case SCOPE_REF:
12329 /* We should use one of the expression tsubsts for these codes. */
12330 gcc_unreachable ();
12332 default:
12333 sorry ("use of %qs in template", get_tree_code_name (code));
12334 return error_mark_node;
12338 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12339 type of the expression on the left-hand side of the "." or "->"
12340 operator. */
12342 static tree
12343 tsubst_baselink (tree baselink, tree object_type,
12344 tree args, tsubst_flags_t complain, tree in_decl)
12346 tree name;
12347 tree qualifying_scope;
12348 tree fns;
12349 tree optype;
12350 tree template_args = 0;
12351 bool template_id_p = false;
12352 bool qualified = BASELINK_QUALIFIED_P (baselink);
12354 /* A baselink indicates a function from a base class. Both the
12355 BASELINK_ACCESS_BINFO and the base class referenced may
12356 indicate bases of the template class, rather than the
12357 instantiated class. In addition, lookups that were not
12358 ambiguous before may be ambiguous now. Therefore, we perform
12359 the lookup again. */
12360 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12361 qualifying_scope = tsubst (qualifying_scope, args,
12362 complain, in_decl);
12363 fns = BASELINK_FUNCTIONS (baselink);
12364 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12365 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12367 template_id_p = true;
12368 template_args = TREE_OPERAND (fns, 1);
12369 fns = TREE_OPERAND (fns, 0);
12370 if (template_args)
12371 template_args = tsubst_template_args (template_args, args,
12372 complain, in_decl);
12374 name = DECL_NAME (get_first_fn (fns));
12375 if (IDENTIFIER_TYPENAME_P (name))
12376 name = mangle_conv_op_name_for_type (optype);
12377 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12378 if (!baselink)
12379 return error_mark_node;
12381 /* If lookup found a single function, mark it as used at this
12382 point. (If it lookup found multiple functions the one selected
12383 later by overload resolution will be marked as used at that
12384 point.) */
12385 if (BASELINK_P (baselink))
12386 fns = BASELINK_FUNCTIONS (baselink);
12387 if (!template_id_p && !really_overloaded_fn (fns))
12388 mark_used (OVL_CURRENT (fns));
12390 /* Add back the template arguments, if present. */
12391 if (BASELINK_P (baselink) && template_id_p)
12392 BASELINK_FUNCTIONS (baselink)
12393 = build_nt (TEMPLATE_ID_EXPR,
12394 BASELINK_FUNCTIONS (baselink),
12395 template_args);
12396 /* Update the conversion operator type. */
12397 BASELINK_OPTYPE (baselink) = optype;
12399 if (!object_type)
12400 object_type = current_class_type;
12402 if (qualified)
12403 baselink = adjust_result_of_qualified_name_lookup (baselink,
12404 qualifying_scope,
12405 object_type);
12406 return baselink;
12409 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12410 true if the qualified-id will be a postfix-expression in-and-of
12411 itself; false if more of the postfix-expression follows the
12412 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12413 of "&". */
12415 static tree
12416 tsubst_qualified_id (tree qualified_id, tree args,
12417 tsubst_flags_t complain, tree in_decl,
12418 bool done, bool address_p)
12420 tree expr;
12421 tree scope;
12422 tree name;
12423 bool is_template;
12424 tree template_args;
12425 location_t loc = UNKNOWN_LOCATION;
12427 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12429 /* Figure out what name to look up. */
12430 name = TREE_OPERAND (qualified_id, 1);
12431 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12433 is_template = true;
12434 loc = EXPR_LOCATION (name);
12435 template_args = TREE_OPERAND (name, 1);
12436 if (template_args)
12437 template_args = tsubst_template_args (template_args, args,
12438 complain, in_decl);
12439 name = TREE_OPERAND (name, 0);
12441 else
12443 is_template = false;
12444 template_args = NULL_TREE;
12447 /* Substitute into the qualifying scope. When there are no ARGS, we
12448 are just trying to simplify a non-dependent expression. In that
12449 case the qualifying scope may be dependent, and, in any case,
12450 substituting will not help. */
12451 scope = TREE_OPERAND (qualified_id, 0);
12452 if (args)
12454 scope = tsubst (scope, args, complain, in_decl);
12455 expr = tsubst_copy (name, args, complain, in_decl);
12457 else
12458 expr = name;
12460 if (dependent_scope_p (scope))
12462 if (is_template)
12463 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12464 return build_qualified_name (NULL_TREE, scope, expr,
12465 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12468 if (!BASELINK_P (name) && !DECL_P (expr))
12470 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12472 /* A BIT_NOT_EXPR is used to represent a destructor. */
12473 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12475 error ("qualifying type %qT does not match destructor name ~%qT",
12476 scope, TREE_OPERAND (expr, 0));
12477 expr = error_mark_node;
12479 else
12480 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12481 /*is_type_p=*/0, false);
12483 else
12484 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12485 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12486 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12488 if (complain & tf_error)
12490 error ("dependent-name %qE is parsed as a non-type, but "
12491 "instantiation yields a type", qualified_id);
12492 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12494 return error_mark_node;
12498 if (DECL_P (expr))
12500 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12501 scope);
12502 /* Remember that there was a reference to this entity. */
12503 mark_used (expr);
12506 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12508 if (complain & tf_error)
12509 qualified_name_lookup_error (scope,
12510 TREE_OPERAND (qualified_id, 1),
12511 expr, input_location);
12512 return error_mark_node;
12515 if (is_template)
12516 expr = lookup_template_function (expr, template_args);
12518 if (expr == error_mark_node && complain & tf_error)
12519 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12520 expr, input_location);
12521 else if (TYPE_P (scope))
12523 expr = (adjust_result_of_qualified_name_lookup
12524 (expr, scope, current_nonlambda_class_type ()));
12525 expr = (finish_qualified_id_expr
12526 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12527 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12528 /*template_arg_p=*/false, complain));
12531 /* Expressions do not generally have reference type. */
12532 if (TREE_CODE (expr) != SCOPE_REF
12533 /* However, if we're about to form a pointer-to-member, we just
12534 want the referenced member referenced. */
12535 && TREE_CODE (expr) != OFFSET_REF)
12536 expr = convert_from_reference (expr);
12538 return expr;
12541 /* Like tsubst, but deals with expressions. This function just replaces
12542 template parms; to finish processing the resultant expression, use
12543 tsubst_copy_and_build or tsubst_expr. */
12545 static tree
12546 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12548 enum tree_code code;
12549 tree r;
12551 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12552 return t;
12554 code = TREE_CODE (t);
12556 switch (code)
12558 case PARM_DECL:
12559 r = retrieve_local_specialization (t);
12561 if (r == NULL_TREE)
12563 /* We get here for a use of 'this' in an NSDMI. */
12564 if (DECL_NAME (t) == this_identifier
12565 && at_function_scope_p ()
12566 && DECL_CONSTRUCTOR_P (current_function_decl))
12567 return current_class_ptr;
12569 /* This can happen for a parameter name used later in a function
12570 declaration (such as in a late-specified return type). Just
12571 make a dummy decl, since it's only used for its type. */
12572 gcc_assert (cp_unevaluated_operand != 0);
12573 r = tsubst_decl (t, args, complain);
12574 /* Give it the template pattern as its context; its true context
12575 hasn't been instantiated yet and this is good enough for
12576 mangling. */
12577 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12580 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12581 r = ARGUMENT_PACK_SELECT_ARG (r);
12582 mark_used (r);
12583 return r;
12585 case CONST_DECL:
12587 tree enum_type;
12588 tree v;
12590 if (DECL_TEMPLATE_PARM_P (t))
12591 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12592 /* There is no need to substitute into namespace-scope
12593 enumerators. */
12594 if (DECL_NAMESPACE_SCOPE_P (t))
12595 return t;
12596 /* If ARGS is NULL, then T is known to be non-dependent. */
12597 if (args == NULL_TREE)
12598 return integral_constant_value (t);
12600 /* Unfortunately, we cannot just call lookup_name here.
12601 Consider:
12603 template <int I> int f() {
12604 enum E { a = I };
12605 struct S { void g() { E e = a; } };
12608 When we instantiate f<7>::S::g(), say, lookup_name is not
12609 clever enough to find f<7>::a. */
12610 enum_type
12611 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12612 /*entering_scope=*/0);
12614 for (v = TYPE_VALUES (enum_type);
12615 v != NULL_TREE;
12616 v = TREE_CHAIN (v))
12617 if (TREE_PURPOSE (v) == DECL_NAME (t))
12618 return TREE_VALUE (v);
12620 /* We didn't find the name. That should never happen; if
12621 name-lookup found it during preliminary parsing, we
12622 should find it again here during instantiation. */
12623 gcc_unreachable ();
12625 return t;
12627 case FIELD_DECL:
12628 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12630 /* Check for a local specialization set up by
12631 tsubst_pack_expansion. */
12632 if (tree r = retrieve_local_specialization (t))
12634 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12635 r = ARGUMENT_PACK_SELECT_ARG (r);
12636 return r;
12639 /* When retrieving a capture pack from a generic lambda, remove the
12640 lambda call op's own template argument list from ARGS. Only the
12641 template arguments active for the closure type should be used to
12642 retrieve the pack specialization. */
12643 if (LAMBDA_FUNCTION_P (current_function_decl)
12644 && (template_class_depth (DECL_CONTEXT (t))
12645 != TMPL_ARGS_DEPTH (args)))
12646 args = strip_innermost_template_args (args, 1);
12648 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12649 tsubst_decl put in the hash table. */
12650 return retrieve_specialization (t, args, 0);
12653 if (DECL_CONTEXT (t))
12655 tree ctx;
12657 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12658 /*entering_scope=*/1);
12659 if (ctx != DECL_CONTEXT (t))
12661 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12662 if (!r)
12664 if (complain & tf_error)
12665 error ("using invalid field %qD", t);
12666 return error_mark_node;
12668 return r;
12672 return t;
12674 case VAR_DECL:
12675 case FUNCTION_DECL:
12676 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12677 r = tsubst (t, args, complain, in_decl);
12678 else if (local_variable_p (t))
12680 r = retrieve_local_specialization (t);
12681 if (r == NULL_TREE)
12683 if (DECL_ANON_UNION_VAR_P (t))
12685 /* Just use name lookup to find a member alias for an
12686 anonymous union, but then add it to the hash table. */
12687 r = lookup_name (DECL_NAME (t));
12688 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12689 register_local_specialization (r, t);
12691 else
12693 /* This can happen for a variable used in a
12694 late-specified return type of a local lambda, or for a
12695 local static or constant. Building a new VAR_DECL
12696 should be OK in all those cases. */
12697 r = tsubst_decl (t, args, complain);
12698 if (decl_constant_var_p (r))
12699 /* A use of a local constant must decay to its value. */
12700 return integral_constant_value (r);
12701 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
12702 || errorcount || sorrycount);
12703 return r;
12707 else
12708 r = t;
12709 mark_used (r);
12710 return r;
12712 case NAMESPACE_DECL:
12713 return t;
12715 case OVERLOAD:
12716 /* An OVERLOAD will always be a non-dependent overload set; an
12717 overload set from function scope will just be represented with an
12718 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12719 gcc_assert (!uses_template_parms (t));
12720 return t;
12722 case BASELINK:
12723 return tsubst_baselink (t, current_nonlambda_class_type (),
12724 args, complain, in_decl);
12726 case TEMPLATE_DECL:
12727 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12728 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12729 args, complain, in_decl);
12730 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12731 return tsubst (t, args, complain, in_decl);
12732 else if (DECL_CLASS_SCOPE_P (t)
12733 && uses_template_parms (DECL_CONTEXT (t)))
12735 /* Template template argument like the following example need
12736 special treatment:
12738 template <template <class> class TT> struct C {};
12739 template <class T> struct D {
12740 template <class U> struct E {};
12741 C<E> c; // #1
12743 D<int> d; // #2
12745 We are processing the template argument `E' in #1 for
12746 the template instantiation #2. Originally, `E' is a
12747 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12748 have to substitute this with one having context `D<int>'. */
12750 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12751 return lookup_field (context, DECL_NAME(t), 0, false);
12753 else
12754 /* Ordinary template template argument. */
12755 return t;
12757 case CAST_EXPR:
12758 case REINTERPRET_CAST_EXPR:
12759 case CONST_CAST_EXPR:
12760 case STATIC_CAST_EXPR:
12761 case DYNAMIC_CAST_EXPR:
12762 case IMPLICIT_CONV_EXPR:
12763 case CONVERT_EXPR:
12764 case NOP_EXPR:
12765 return build1
12766 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12767 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12769 case SIZEOF_EXPR:
12770 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12773 tree expanded, op = TREE_OPERAND (t, 0);
12774 int len = 0;
12776 if (SIZEOF_EXPR_TYPE_P (t))
12777 op = TREE_TYPE (op);
12779 ++cp_unevaluated_operand;
12780 ++c_inhibit_evaluation_warnings;
12781 /* We only want to compute the number of arguments. */
12782 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12783 --cp_unevaluated_operand;
12784 --c_inhibit_evaluation_warnings;
12786 if (TREE_CODE (expanded) == TREE_VEC)
12787 len = TREE_VEC_LENGTH (expanded);
12789 if (expanded == error_mark_node)
12790 return error_mark_node;
12791 else if (PACK_EXPANSION_P (expanded)
12792 || (TREE_CODE (expanded) == TREE_VEC
12793 && len > 0
12794 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12796 if (TREE_CODE (expanded) == TREE_VEC)
12797 expanded = TREE_VEC_ELT (expanded, len - 1);
12799 if (TYPE_P (expanded))
12800 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12801 complain & tf_error);
12802 else
12803 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12804 complain & tf_error);
12806 else
12807 return build_int_cst (size_type_node, len);
12809 if (SIZEOF_EXPR_TYPE_P (t))
12811 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12812 args, complain, in_decl);
12813 r = build1 (NOP_EXPR, r, error_mark_node);
12814 r = build1 (SIZEOF_EXPR,
12815 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12816 SIZEOF_EXPR_TYPE_P (r) = 1;
12817 return r;
12819 /* Fall through */
12821 case INDIRECT_REF:
12822 case NEGATE_EXPR:
12823 case TRUTH_NOT_EXPR:
12824 case BIT_NOT_EXPR:
12825 case ADDR_EXPR:
12826 case UNARY_PLUS_EXPR: /* Unary + */
12827 case ALIGNOF_EXPR:
12828 case AT_ENCODE_EXPR:
12829 case ARROW_EXPR:
12830 case THROW_EXPR:
12831 case TYPEID_EXPR:
12832 case REALPART_EXPR:
12833 case IMAGPART_EXPR:
12834 case PAREN_EXPR:
12835 return build1
12836 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12837 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12839 case COMPONENT_REF:
12841 tree object;
12842 tree name;
12844 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12845 name = TREE_OPERAND (t, 1);
12846 if (TREE_CODE (name) == BIT_NOT_EXPR)
12848 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12849 complain, in_decl);
12850 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12852 else if (TREE_CODE (name) == SCOPE_REF
12853 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12855 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12856 complain, in_decl);
12857 name = TREE_OPERAND (name, 1);
12858 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12859 complain, in_decl);
12860 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12861 name = build_qualified_name (/*type=*/NULL_TREE,
12862 base, name,
12863 /*template_p=*/false);
12865 else if (BASELINK_P (name))
12866 name = tsubst_baselink (name,
12867 non_reference (TREE_TYPE (object)),
12868 args, complain,
12869 in_decl);
12870 else
12871 name = tsubst_copy (name, args, complain, in_decl);
12872 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12875 case PLUS_EXPR:
12876 case MINUS_EXPR:
12877 case MULT_EXPR:
12878 case TRUNC_DIV_EXPR:
12879 case CEIL_DIV_EXPR:
12880 case FLOOR_DIV_EXPR:
12881 case ROUND_DIV_EXPR:
12882 case EXACT_DIV_EXPR:
12883 case BIT_AND_EXPR:
12884 case BIT_IOR_EXPR:
12885 case BIT_XOR_EXPR:
12886 case TRUNC_MOD_EXPR:
12887 case FLOOR_MOD_EXPR:
12888 case TRUTH_ANDIF_EXPR:
12889 case TRUTH_ORIF_EXPR:
12890 case TRUTH_AND_EXPR:
12891 case TRUTH_OR_EXPR:
12892 case RSHIFT_EXPR:
12893 case LSHIFT_EXPR:
12894 case RROTATE_EXPR:
12895 case LROTATE_EXPR:
12896 case EQ_EXPR:
12897 case NE_EXPR:
12898 case MAX_EXPR:
12899 case MIN_EXPR:
12900 case LE_EXPR:
12901 case GE_EXPR:
12902 case LT_EXPR:
12903 case GT_EXPR:
12904 case COMPOUND_EXPR:
12905 case DOTSTAR_EXPR:
12906 case MEMBER_REF:
12907 case PREDECREMENT_EXPR:
12908 case PREINCREMENT_EXPR:
12909 case POSTDECREMENT_EXPR:
12910 case POSTINCREMENT_EXPR:
12911 return build_nt
12912 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12913 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12915 case SCOPE_REF:
12916 return build_qualified_name (/*type=*/NULL_TREE,
12917 tsubst_copy (TREE_OPERAND (t, 0),
12918 args, complain, in_decl),
12919 tsubst_copy (TREE_OPERAND (t, 1),
12920 args, complain, in_decl),
12921 QUALIFIED_NAME_IS_TEMPLATE (t));
12923 case ARRAY_REF:
12924 return build_nt
12925 (ARRAY_REF,
12926 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12927 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12928 NULL_TREE, NULL_TREE);
12930 case CALL_EXPR:
12932 int n = VL_EXP_OPERAND_LENGTH (t);
12933 tree result = build_vl_exp (CALL_EXPR, n);
12934 int i;
12935 for (i = 0; i < n; i++)
12936 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12937 complain, in_decl);
12938 return result;
12941 case COND_EXPR:
12942 case MODOP_EXPR:
12943 case PSEUDO_DTOR_EXPR:
12944 case VEC_PERM_EXPR:
12946 r = build_nt
12947 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12948 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12949 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12950 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12951 return r;
12954 case NEW_EXPR:
12956 r = build_nt
12957 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12958 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12959 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12960 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12961 return r;
12964 case DELETE_EXPR:
12966 r = build_nt
12967 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12968 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12969 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12970 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12971 return r;
12974 case TEMPLATE_ID_EXPR:
12976 /* Substituted template arguments */
12977 tree fn = TREE_OPERAND (t, 0);
12978 tree targs = TREE_OPERAND (t, 1);
12980 fn = tsubst_copy (fn, args, complain, in_decl);
12981 if (targs)
12982 targs = tsubst_template_args (targs, args, complain, in_decl);
12984 return lookup_template_function (fn, targs);
12987 case TREE_LIST:
12989 tree purpose, value, chain;
12991 if (t == void_list_node)
12992 return t;
12994 purpose = TREE_PURPOSE (t);
12995 if (purpose)
12996 purpose = tsubst_copy (purpose, args, complain, in_decl);
12997 value = TREE_VALUE (t);
12998 if (value)
12999 value = tsubst_copy (value, args, complain, in_decl);
13000 chain = TREE_CHAIN (t);
13001 if (chain && chain != void_type_node)
13002 chain = tsubst_copy (chain, args, complain, in_decl);
13003 if (purpose == TREE_PURPOSE (t)
13004 && value == TREE_VALUE (t)
13005 && chain == TREE_CHAIN (t))
13006 return t;
13007 return tree_cons (purpose, value, chain);
13010 case RECORD_TYPE:
13011 case UNION_TYPE:
13012 case ENUMERAL_TYPE:
13013 case INTEGER_TYPE:
13014 case TEMPLATE_TYPE_PARM:
13015 case TEMPLATE_TEMPLATE_PARM:
13016 case BOUND_TEMPLATE_TEMPLATE_PARM:
13017 case TEMPLATE_PARM_INDEX:
13018 case POINTER_TYPE:
13019 case REFERENCE_TYPE:
13020 case OFFSET_TYPE:
13021 case FUNCTION_TYPE:
13022 case METHOD_TYPE:
13023 case ARRAY_TYPE:
13024 case TYPENAME_TYPE:
13025 case UNBOUND_CLASS_TEMPLATE:
13026 case TYPEOF_TYPE:
13027 case DECLTYPE_TYPE:
13028 case TYPE_DECL:
13029 return tsubst (t, args, complain, in_decl);
13031 case USING_DECL:
13032 t = DECL_NAME (t);
13033 /* Fall through. */
13034 case IDENTIFIER_NODE:
13035 if (IDENTIFIER_TYPENAME_P (t))
13037 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13038 return mangle_conv_op_name_for_type (new_type);
13040 else
13041 return t;
13043 case CONSTRUCTOR:
13044 /* This is handled by tsubst_copy_and_build. */
13045 gcc_unreachable ();
13047 case VA_ARG_EXPR:
13048 return build_x_va_arg (EXPR_LOCATION (t),
13049 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
13050 in_decl),
13051 tsubst (TREE_TYPE (t), args, complain, in_decl));
13053 case CLEANUP_POINT_EXPR:
13054 /* We shouldn't have built any of these during initial template
13055 generation. Instead, they should be built during instantiation
13056 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13057 gcc_unreachable ();
13059 case OFFSET_REF:
13060 r = build2
13061 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
13062 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13063 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13064 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13065 mark_used (TREE_OPERAND (r, 1));
13066 return r;
13068 case EXPR_PACK_EXPANSION:
13069 error ("invalid use of pack expansion expression");
13070 return error_mark_node;
13072 case NONTYPE_ARGUMENT_PACK:
13073 error ("use %<...%> to expand argument pack");
13074 return error_mark_node;
13076 case INTEGER_CST:
13077 case REAL_CST:
13078 case STRING_CST:
13079 case COMPLEX_CST:
13081 /* Instantiate any typedefs in the type. */
13082 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13083 r = fold_convert (type, t);
13084 gcc_assert (TREE_CODE (r) == code);
13085 return r;
13088 case PTRMEM_CST:
13089 /* These can sometimes show up in a partial instantiation, but never
13090 involve template parms. */
13091 gcc_assert (!uses_template_parms (t));
13092 return t;
13094 default:
13095 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13096 gcc_checking_assert (false);
13097 return t;
13101 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13103 static tree
13104 tsubst_omp_clauses (tree clauses, bool declare_simd,
13105 tree args, tsubst_flags_t complain, tree in_decl)
13107 tree new_clauses = NULL, nc, oc;
13109 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13111 nc = copy_node (oc);
13112 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13113 new_clauses = nc;
13115 switch (OMP_CLAUSE_CODE (nc))
13117 case OMP_CLAUSE_LASTPRIVATE:
13118 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13120 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13121 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13122 in_decl, /*integral_constant_expression_p=*/false);
13123 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13124 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13126 /* FALLTHRU */
13127 case OMP_CLAUSE_PRIVATE:
13128 case OMP_CLAUSE_SHARED:
13129 case OMP_CLAUSE_FIRSTPRIVATE:
13130 case OMP_CLAUSE_COPYIN:
13131 case OMP_CLAUSE_COPYPRIVATE:
13132 case OMP_CLAUSE_IF:
13133 case OMP_CLAUSE_NUM_THREADS:
13134 case OMP_CLAUSE_SCHEDULE:
13135 case OMP_CLAUSE_COLLAPSE:
13136 case OMP_CLAUSE_FINAL:
13137 case OMP_CLAUSE_DEPEND:
13138 case OMP_CLAUSE_FROM:
13139 case OMP_CLAUSE_TO:
13140 case OMP_CLAUSE_UNIFORM:
13141 case OMP_CLAUSE_MAP:
13142 case OMP_CLAUSE_DEVICE:
13143 case OMP_CLAUSE_DIST_SCHEDULE:
13144 case OMP_CLAUSE_NUM_TEAMS:
13145 case OMP_CLAUSE_THREAD_LIMIT:
13146 case OMP_CLAUSE_SAFELEN:
13147 case OMP_CLAUSE_SIMDLEN:
13148 OMP_CLAUSE_OPERAND (nc, 0)
13149 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13150 in_decl, /*integral_constant_expression_p=*/false);
13151 break;
13152 case OMP_CLAUSE_REDUCTION:
13153 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13155 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13156 if (TREE_CODE (placeholder) == SCOPE_REF)
13158 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13159 complain, in_decl);
13160 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13161 = build_qualified_name (NULL_TREE, scope,
13162 TREE_OPERAND (placeholder, 1),
13163 false);
13165 else
13166 gcc_assert (identifier_p (placeholder));
13168 OMP_CLAUSE_OPERAND (nc, 0)
13169 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13170 in_decl, /*integral_constant_expression_p=*/false);
13171 break;
13172 case OMP_CLAUSE_LINEAR:
13173 case OMP_CLAUSE_ALIGNED:
13174 OMP_CLAUSE_OPERAND (nc, 0)
13175 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13176 in_decl, /*integral_constant_expression_p=*/false);
13177 OMP_CLAUSE_OPERAND (nc, 1)
13178 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13179 in_decl, /*integral_constant_expression_p=*/false);
13180 break;
13182 case OMP_CLAUSE_NOWAIT:
13183 case OMP_CLAUSE_ORDERED:
13184 case OMP_CLAUSE_DEFAULT:
13185 case OMP_CLAUSE_UNTIED:
13186 case OMP_CLAUSE_MERGEABLE:
13187 case OMP_CLAUSE_INBRANCH:
13188 case OMP_CLAUSE_NOTINBRANCH:
13189 case OMP_CLAUSE_PROC_BIND:
13190 case OMP_CLAUSE_FOR:
13191 case OMP_CLAUSE_PARALLEL:
13192 case OMP_CLAUSE_SECTIONS:
13193 case OMP_CLAUSE_TASKGROUP:
13194 break;
13195 default:
13196 gcc_unreachable ();
13200 new_clauses = nreverse (new_clauses);
13201 if (!declare_simd)
13202 new_clauses = finish_omp_clauses (new_clauses);
13203 return new_clauses;
13206 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13208 static tree
13209 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13210 tree in_decl)
13212 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13214 tree purpose, value, chain;
13216 if (t == NULL)
13217 return t;
13219 if (TREE_CODE (t) != TREE_LIST)
13220 return tsubst_copy_and_build (t, args, complain, in_decl,
13221 /*function_p=*/false,
13222 /*integral_constant_expression_p=*/false);
13224 if (t == void_list_node)
13225 return t;
13227 purpose = TREE_PURPOSE (t);
13228 if (purpose)
13229 purpose = RECUR (purpose);
13230 value = TREE_VALUE (t);
13231 if (value)
13233 if (TREE_CODE (value) != LABEL_DECL)
13234 value = RECUR (value);
13235 else
13237 value = lookup_label (DECL_NAME (value));
13238 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13239 TREE_USED (value) = 1;
13242 chain = TREE_CHAIN (t);
13243 if (chain && chain != void_type_node)
13244 chain = RECUR (chain);
13245 return tree_cons (purpose, value, chain);
13246 #undef RECUR
13249 /* Substitute one OMP_FOR iterator. */
13251 static void
13252 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13253 tree condv, tree incrv, tree *clauses,
13254 tree args, tsubst_flags_t complain, tree in_decl,
13255 bool integral_constant_expression_p)
13257 #define RECUR(NODE) \
13258 tsubst_expr ((NODE), args, complain, in_decl, \
13259 integral_constant_expression_p)
13260 tree decl, init, cond, incr;
13262 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13263 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13264 decl = TREE_OPERAND (init, 0);
13265 init = TREE_OPERAND (init, 1);
13266 tree decl_expr = NULL_TREE;
13267 if (init && TREE_CODE (init) == DECL_EXPR)
13269 /* We need to jump through some hoops to handle declarations in the
13270 for-init-statement, since we might need to handle auto deduction,
13271 but we need to keep control of initialization. */
13272 decl_expr = init;
13273 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13274 decl = tsubst_decl (decl, args, complain);
13276 else
13277 decl = RECUR (decl);
13278 init = RECUR (init);
13280 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13281 if (auto_node && init)
13282 TREE_TYPE (decl)
13283 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13285 gcc_assert (!type_dependent_expression_p (decl));
13287 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13289 if (decl_expr)
13291 /* Declare the variable, but don't let that initialize it. */
13292 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13293 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13294 RECUR (decl_expr);
13295 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13298 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13299 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13300 if (TREE_CODE (incr) == MODIFY_EXPR)
13301 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13302 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13303 RECUR (TREE_OPERAND (incr, 1)),
13304 complain);
13305 else
13306 incr = RECUR (incr);
13307 TREE_VEC_ELT (declv, i) = decl;
13308 TREE_VEC_ELT (initv, i) = init;
13309 TREE_VEC_ELT (condv, i) = cond;
13310 TREE_VEC_ELT (incrv, i) = incr;
13311 return;
13314 if (decl_expr)
13316 /* Declare and initialize the variable. */
13317 RECUR (decl_expr);
13318 init = NULL_TREE;
13320 else if (init)
13322 tree c;
13323 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13325 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13326 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13327 && OMP_CLAUSE_DECL (c) == decl)
13328 break;
13329 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13330 && OMP_CLAUSE_DECL (c) == decl)
13331 error ("iteration variable %qD should not be firstprivate", decl);
13332 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13333 && OMP_CLAUSE_DECL (c) == decl)
13334 error ("iteration variable %qD should not be reduction", decl);
13336 if (c == NULL)
13338 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13339 OMP_CLAUSE_DECL (c) = decl;
13340 c = finish_omp_clauses (c);
13341 if (c)
13343 OMP_CLAUSE_CHAIN (c) = *clauses;
13344 *clauses = c;
13348 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13349 if (COMPARISON_CLASS_P (cond))
13350 cond = build2 (TREE_CODE (cond), boolean_type_node,
13351 RECUR (TREE_OPERAND (cond, 0)),
13352 RECUR (TREE_OPERAND (cond, 1)));
13353 else
13354 cond = RECUR (cond);
13355 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13356 switch (TREE_CODE (incr))
13358 case PREINCREMENT_EXPR:
13359 case PREDECREMENT_EXPR:
13360 case POSTINCREMENT_EXPR:
13361 case POSTDECREMENT_EXPR:
13362 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13363 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13364 break;
13365 case MODIFY_EXPR:
13366 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13367 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13369 tree rhs = TREE_OPERAND (incr, 1);
13370 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13371 RECUR (TREE_OPERAND (incr, 0)),
13372 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13373 RECUR (TREE_OPERAND (rhs, 0)),
13374 RECUR (TREE_OPERAND (rhs, 1))));
13376 else
13377 incr = RECUR (incr);
13378 break;
13379 case MODOP_EXPR:
13380 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13381 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13383 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13384 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13385 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13386 TREE_TYPE (decl), lhs,
13387 RECUR (TREE_OPERAND (incr, 2))));
13389 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13390 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13391 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13393 tree rhs = TREE_OPERAND (incr, 2);
13394 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13395 RECUR (TREE_OPERAND (incr, 0)),
13396 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13397 RECUR (TREE_OPERAND (rhs, 0)),
13398 RECUR (TREE_OPERAND (rhs, 1))));
13400 else
13401 incr = RECUR (incr);
13402 break;
13403 default:
13404 incr = RECUR (incr);
13405 break;
13408 TREE_VEC_ELT (declv, i) = decl;
13409 TREE_VEC_ELT (initv, i) = init;
13410 TREE_VEC_ELT (condv, i) = cond;
13411 TREE_VEC_ELT (incrv, i) = incr;
13412 #undef RECUR
13415 /* Like tsubst_copy for expressions, etc. but also does semantic
13416 processing. */
13418 static tree
13419 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13420 bool integral_constant_expression_p)
13422 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13423 #define RECUR(NODE) \
13424 tsubst_expr ((NODE), args, complain, in_decl, \
13425 integral_constant_expression_p)
13427 tree stmt, tmp;
13428 tree r;
13429 location_t loc;
13431 if (t == NULL_TREE || t == error_mark_node)
13432 return t;
13434 loc = input_location;
13435 if (EXPR_HAS_LOCATION (t))
13436 input_location = EXPR_LOCATION (t);
13437 if (STATEMENT_CODE_P (TREE_CODE (t)))
13438 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13440 switch (TREE_CODE (t))
13442 case STATEMENT_LIST:
13444 tree_stmt_iterator i;
13445 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13446 RECUR (tsi_stmt (i));
13447 break;
13450 case CTOR_INITIALIZER:
13451 finish_mem_initializers (tsubst_initializer_list
13452 (TREE_OPERAND (t, 0), args));
13453 break;
13455 case RETURN_EXPR:
13456 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13457 break;
13459 case EXPR_STMT:
13460 tmp = RECUR (EXPR_STMT_EXPR (t));
13461 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13462 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13463 else
13464 finish_expr_stmt (tmp);
13465 break;
13467 case USING_STMT:
13468 do_using_directive (USING_STMT_NAMESPACE (t));
13469 break;
13471 case DECL_EXPR:
13473 tree decl, pattern_decl;
13474 tree init;
13476 pattern_decl = decl = DECL_EXPR_DECL (t);
13477 if (TREE_CODE (decl) == LABEL_DECL)
13478 finish_label_decl (DECL_NAME (decl));
13479 else if (TREE_CODE (decl) == USING_DECL)
13481 tree scope = USING_DECL_SCOPE (decl);
13482 tree name = DECL_NAME (decl);
13483 tree decl;
13485 scope = tsubst (scope, args, complain, in_decl);
13486 decl = lookup_qualified_name (scope, name,
13487 /*is_type_p=*/false,
13488 /*complain=*/false);
13489 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13490 qualified_name_lookup_error (scope, name, decl, input_location);
13491 else
13492 do_local_using_decl (decl, scope, name);
13494 else if (DECL_PACK_P (decl))
13496 /* Don't build up decls for a variadic capture proxy, we'll
13497 instantiate the elements directly as needed. */
13498 break;
13500 else
13502 init = DECL_INITIAL (decl);
13503 decl = tsubst (decl, args, complain, in_decl);
13504 if (decl != error_mark_node)
13506 /* By marking the declaration as instantiated, we avoid
13507 trying to instantiate it. Since instantiate_decl can't
13508 handle local variables, and since we've already done
13509 all that needs to be done, that's the right thing to
13510 do. */
13511 if (VAR_P (decl))
13512 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13513 if (VAR_P (decl)
13514 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13515 /* Anonymous aggregates are a special case. */
13516 finish_anon_union (decl);
13517 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13519 DECL_CONTEXT (decl) = current_function_decl;
13520 if (DECL_NAME (decl) == this_identifier)
13522 tree lam = DECL_CONTEXT (current_function_decl);
13523 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13524 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13526 insert_capture_proxy (decl);
13528 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13529 /* We already did a pushtag. */;
13530 else if (TREE_CODE (decl) == FUNCTION_DECL
13531 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13532 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13534 DECL_CONTEXT (decl) = NULL_TREE;
13535 pushdecl (decl);
13536 DECL_CONTEXT (decl) = current_function_decl;
13537 cp_check_omp_declare_reduction (decl);
13539 else
13541 int const_init = false;
13542 maybe_push_decl (decl);
13543 if (VAR_P (decl)
13544 && DECL_PRETTY_FUNCTION_P (decl))
13546 /* For __PRETTY_FUNCTION__ we have to adjust the
13547 initializer. */
13548 const char *const name
13549 = cxx_printable_name (current_function_decl, 2);
13550 init = cp_fname_init (name, &TREE_TYPE (decl));
13552 else
13554 tree t = RECUR (init);
13556 if (init && !t)
13558 /* If we had an initializer but it
13559 instantiated to nothing,
13560 value-initialize the object. This will
13561 only occur when the initializer was a
13562 pack expansion where the parameter packs
13563 used in that expansion were of length
13564 zero. */
13565 init = build_value_init (TREE_TYPE (decl),
13566 complain);
13567 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13568 init = get_target_expr_sfinae (init, complain);
13570 else
13571 init = t;
13574 if (VAR_P (decl))
13575 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13576 (pattern_decl));
13577 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13582 break;
13585 case FOR_STMT:
13586 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13587 RECUR (FOR_INIT_STMT (t));
13588 finish_for_init_stmt (stmt);
13589 tmp = RECUR (FOR_COND (t));
13590 finish_for_cond (tmp, stmt, false);
13591 tmp = RECUR (FOR_EXPR (t));
13592 finish_for_expr (tmp, stmt);
13593 RECUR (FOR_BODY (t));
13594 finish_for_stmt (stmt);
13595 break;
13597 case RANGE_FOR_STMT:
13599 tree decl, expr;
13600 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13601 decl = RANGE_FOR_DECL (t);
13602 decl = tsubst (decl, args, complain, in_decl);
13603 maybe_push_decl (decl);
13604 expr = RECUR (RANGE_FOR_EXPR (t));
13605 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13606 RECUR (RANGE_FOR_BODY (t));
13607 finish_for_stmt (stmt);
13609 break;
13611 case WHILE_STMT:
13612 stmt = begin_while_stmt ();
13613 tmp = RECUR (WHILE_COND (t));
13614 finish_while_stmt_cond (tmp, stmt, false);
13615 RECUR (WHILE_BODY (t));
13616 finish_while_stmt (stmt);
13617 break;
13619 case DO_STMT:
13620 stmt = begin_do_stmt ();
13621 RECUR (DO_BODY (t));
13622 finish_do_body (stmt);
13623 tmp = RECUR (DO_COND (t));
13624 finish_do_stmt (tmp, stmt, false);
13625 break;
13627 case IF_STMT:
13628 stmt = begin_if_stmt ();
13629 tmp = RECUR (IF_COND (t));
13630 finish_if_stmt_cond (tmp, stmt);
13631 RECUR (THEN_CLAUSE (t));
13632 finish_then_clause (stmt);
13634 if (ELSE_CLAUSE (t))
13636 begin_else_clause (stmt);
13637 RECUR (ELSE_CLAUSE (t));
13638 finish_else_clause (stmt);
13641 finish_if_stmt (stmt);
13642 break;
13644 case BIND_EXPR:
13645 if (BIND_EXPR_BODY_BLOCK (t))
13646 stmt = begin_function_body ();
13647 else
13648 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13649 ? BCS_TRY_BLOCK : 0);
13651 RECUR (BIND_EXPR_BODY (t));
13653 if (BIND_EXPR_BODY_BLOCK (t))
13654 finish_function_body (stmt);
13655 else
13656 finish_compound_stmt (stmt);
13657 break;
13659 case BREAK_STMT:
13660 finish_break_stmt ();
13661 break;
13663 case CONTINUE_STMT:
13664 finish_continue_stmt ();
13665 break;
13667 case SWITCH_STMT:
13668 stmt = begin_switch_stmt ();
13669 tmp = RECUR (SWITCH_STMT_COND (t));
13670 finish_switch_cond (tmp, stmt);
13671 RECUR (SWITCH_STMT_BODY (t));
13672 finish_switch_stmt (stmt);
13673 break;
13675 case CASE_LABEL_EXPR:
13676 finish_case_label (EXPR_LOCATION (t),
13677 RECUR (CASE_LOW (t)),
13678 RECUR (CASE_HIGH (t)));
13679 break;
13681 case LABEL_EXPR:
13683 tree decl = LABEL_EXPR_LABEL (t);
13684 tree label;
13686 label = finish_label_stmt (DECL_NAME (decl));
13687 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13688 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13690 break;
13692 case GOTO_EXPR:
13693 tmp = GOTO_DESTINATION (t);
13694 if (TREE_CODE (tmp) != LABEL_DECL)
13695 /* Computed goto's must be tsubst'd into. On the other hand,
13696 non-computed gotos must not be; the identifier in question
13697 will have no binding. */
13698 tmp = RECUR (tmp);
13699 else
13700 tmp = DECL_NAME (tmp);
13701 finish_goto_stmt (tmp);
13702 break;
13704 case ASM_EXPR:
13705 tmp = finish_asm_stmt
13706 (ASM_VOLATILE_P (t),
13707 RECUR (ASM_STRING (t)),
13708 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13709 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13710 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13711 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13713 tree asm_expr = tmp;
13714 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13715 asm_expr = TREE_OPERAND (asm_expr, 0);
13716 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13718 break;
13720 case TRY_BLOCK:
13721 if (CLEANUP_P (t))
13723 stmt = begin_try_block ();
13724 RECUR (TRY_STMTS (t));
13725 finish_cleanup_try_block (stmt);
13726 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13728 else
13730 tree compound_stmt = NULL_TREE;
13732 if (FN_TRY_BLOCK_P (t))
13733 stmt = begin_function_try_block (&compound_stmt);
13734 else
13735 stmt = begin_try_block ();
13737 RECUR (TRY_STMTS (t));
13739 if (FN_TRY_BLOCK_P (t))
13740 finish_function_try_block (stmt);
13741 else
13742 finish_try_block (stmt);
13744 RECUR (TRY_HANDLERS (t));
13745 if (FN_TRY_BLOCK_P (t))
13746 finish_function_handler_sequence (stmt, compound_stmt);
13747 else
13748 finish_handler_sequence (stmt);
13750 break;
13752 case HANDLER:
13754 tree decl = HANDLER_PARMS (t);
13756 if (decl)
13758 decl = tsubst (decl, args, complain, in_decl);
13759 /* Prevent instantiate_decl from trying to instantiate
13760 this variable. We've already done all that needs to be
13761 done. */
13762 if (decl != error_mark_node)
13763 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13765 stmt = begin_handler ();
13766 finish_handler_parms (decl, stmt);
13767 RECUR (HANDLER_BODY (t));
13768 finish_handler (stmt);
13770 break;
13772 case TAG_DEFN:
13773 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13774 if (CLASS_TYPE_P (tmp))
13776 /* Local classes are not independent templates; they are
13777 instantiated along with their containing function. And this
13778 way we don't have to deal with pushing out of one local class
13779 to instantiate a member of another local class. */
13780 tree fn;
13781 /* Closures are handled by the LAMBDA_EXPR. */
13782 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13783 complete_type (tmp);
13784 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13785 if (!DECL_ARTIFICIAL (fn))
13786 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13788 break;
13790 case STATIC_ASSERT:
13792 tree condition;
13794 ++c_inhibit_evaluation_warnings;
13795 condition =
13796 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13797 args,
13798 complain, in_decl,
13799 /*integral_constant_expression_p=*/true);
13800 --c_inhibit_evaluation_warnings;
13802 finish_static_assert (condition,
13803 STATIC_ASSERT_MESSAGE (t),
13804 STATIC_ASSERT_SOURCE_LOCATION (t),
13805 /*member_p=*/false);
13807 break;
13809 case OMP_PARALLEL:
13810 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13811 args, complain, in_decl);
13812 stmt = begin_omp_parallel ();
13813 RECUR (OMP_PARALLEL_BODY (t));
13814 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13815 = OMP_PARALLEL_COMBINED (t);
13816 break;
13818 case OMP_TASK:
13819 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13820 args, complain, in_decl);
13821 stmt = begin_omp_task ();
13822 RECUR (OMP_TASK_BODY (t));
13823 finish_omp_task (tmp, stmt);
13824 break;
13826 case OMP_FOR:
13827 case OMP_SIMD:
13828 case CILK_SIMD:
13829 case OMP_DISTRIBUTE:
13831 tree clauses, body, pre_body;
13832 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13833 tree incrv = NULL_TREE;
13834 int i;
13836 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13837 args, complain, in_decl);
13838 if (OMP_FOR_INIT (t) != NULL_TREE)
13840 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13841 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13842 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13843 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13846 stmt = begin_omp_structured_block ();
13848 pre_body = push_stmt_list ();
13849 RECUR (OMP_FOR_PRE_BODY (t));
13850 pre_body = pop_stmt_list (pre_body);
13852 if (OMP_FOR_INIT (t) != NULL_TREE)
13853 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13854 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13855 &clauses, args, complain, in_decl,
13856 integral_constant_expression_p);
13858 body = push_stmt_list ();
13859 RECUR (OMP_FOR_BODY (t));
13860 body = pop_stmt_list (body);
13862 if (OMP_FOR_INIT (t) != NULL_TREE)
13863 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13864 condv, incrv, body, pre_body, clauses);
13865 else
13867 t = make_node (TREE_CODE (t));
13868 TREE_TYPE (t) = void_type_node;
13869 OMP_FOR_BODY (t) = body;
13870 OMP_FOR_PRE_BODY (t) = pre_body;
13871 OMP_FOR_CLAUSES (t) = clauses;
13872 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13873 add_stmt (t);
13876 add_stmt (finish_omp_structured_block (stmt));
13878 break;
13880 case OMP_SECTIONS:
13881 case OMP_SINGLE:
13882 case OMP_TEAMS:
13883 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13884 args, complain, in_decl);
13885 stmt = push_stmt_list ();
13886 RECUR (OMP_BODY (t));
13887 stmt = pop_stmt_list (stmt);
13889 t = copy_node (t);
13890 OMP_BODY (t) = stmt;
13891 OMP_CLAUSES (t) = tmp;
13892 add_stmt (t);
13893 break;
13895 case OMP_TARGET_DATA:
13896 case OMP_TARGET:
13897 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13898 args, complain, in_decl);
13899 keep_next_level (true);
13900 stmt = begin_omp_structured_block ();
13902 RECUR (OMP_BODY (t));
13903 stmt = finish_omp_structured_block (stmt);
13905 t = copy_node (t);
13906 OMP_BODY (t) = stmt;
13907 OMP_CLAUSES (t) = tmp;
13908 add_stmt (t);
13909 break;
13911 case OMP_TARGET_UPDATE:
13912 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13913 args, complain, in_decl);
13914 t = copy_node (t);
13915 OMP_TARGET_UPDATE_CLAUSES (t) = tmp;
13916 add_stmt (t);
13917 break;
13919 case OMP_SECTION:
13920 case OMP_CRITICAL:
13921 case OMP_MASTER:
13922 case OMP_TASKGROUP:
13923 case OMP_ORDERED:
13924 stmt = push_stmt_list ();
13925 RECUR (OMP_BODY (t));
13926 stmt = pop_stmt_list (stmt);
13928 t = copy_node (t);
13929 OMP_BODY (t) = stmt;
13930 add_stmt (t);
13931 break;
13933 case OMP_ATOMIC:
13934 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13935 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13937 tree op1 = TREE_OPERAND (t, 1);
13938 tree rhs1 = NULL_TREE;
13939 tree lhs, rhs;
13940 if (TREE_CODE (op1) == COMPOUND_EXPR)
13942 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13943 op1 = TREE_OPERAND (op1, 1);
13945 lhs = RECUR (TREE_OPERAND (op1, 0));
13946 rhs = RECUR (TREE_OPERAND (op1, 1));
13947 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13948 NULL_TREE, NULL_TREE, rhs1,
13949 OMP_ATOMIC_SEQ_CST (t));
13951 else
13953 tree op1 = TREE_OPERAND (t, 1);
13954 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13955 tree rhs1 = NULL_TREE;
13956 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13957 enum tree_code opcode = NOP_EXPR;
13958 if (code == OMP_ATOMIC_READ)
13960 v = RECUR (TREE_OPERAND (op1, 0));
13961 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13963 else if (code == OMP_ATOMIC_CAPTURE_OLD
13964 || code == OMP_ATOMIC_CAPTURE_NEW)
13966 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13967 v = RECUR (TREE_OPERAND (op1, 0));
13968 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13969 if (TREE_CODE (op11) == COMPOUND_EXPR)
13971 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13972 op11 = TREE_OPERAND (op11, 1);
13974 lhs = RECUR (TREE_OPERAND (op11, 0));
13975 rhs = RECUR (TREE_OPERAND (op11, 1));
13976 opcode = TREE_CODE (op11);
13977 if (opcode == MODIFY_EXPR)
13978 opcode = NOP_EXPR;
13980 else
13982 code = OMP_ATOMIC;
13983 lhs = RECUR (TREE_OPERAND (op1, 0));
13984 rhs = RECUR (TREE_OPERAND (op1, 1));
13986 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13987 OMP_ATOMIC_SEQ_CST (t));
13989 break;
13991 case TRANSACTION_EXPR:
13993 int flags = 0;
13994 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13995 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13997 if (TRANSACTION_EXPR_IS_STMT (t))
13999 tree body = TRANSACTION_EXPR_BODY (t);
14000 tree noex = NULL_TREE;
14001 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14003 noex = MUST_NOT_THROW_COND (body);
14004 if (noex == NULL_TREE)
14005 noex = boolean_true_node;
14006 body = TREE_OPERAND (body, 0);
14008 stmt = begin_transaction_stmt (input_location, NULL, flags);
14009 RECUR (body);
14010 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14012 else
14014 stmt = build_transaction_expr (EXPR_LOCATION (t),
14015 RECUR (TRANSACTION_EXPR_BODY (t)),
14016 flags, NULL_TREE);
14017 RETURN (stmt);
14020 break;
14022 case MUST_NOT_THROW_EXPR:
14023 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
14024 RECUR (MUST_NOT_THROW_COND (t))));
14026 case EXPR_PACK_EXPANSION:
14027 error ("invalid use of pack expansion expression");
14028 RETURN (error_mark_node);
14030 case NONTYPE_ARGUMENT_PACK:
14031 error ("use %<...%> to expand argument pack");
14032 RETURN (error_mark_node);
14034 case CILK_SPAWN_STMT:
14035 cfun->calls_cilk_spawn = 1;
14036 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14038 case CILK_SYNC_STMT:
14039 RETURN (build_cilk_sync ());
14041 case COMPOUND_EXPR:
14042 tmp = RECUR (TREE_OPERAND (t, 0));
14043 if (tmp == NULL_TREE)
14044 /* If the first operand was a statement, we're done with it. */
14045 RETURN (RECUR (TREE_OPERAND (t, 1)));
14046 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14047 RECUR (TREE_OPERAND (t, 1)),
14048 complain));
14050 case ANNOTATE_EXPR:
14051 tmp = RECUR (TREE_OPERAND (t, 0));
14052 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14053 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14055 default:
14056 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14058 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14059 /*function_p=*/false,
14060 integral_constant_expression_p));
14063 RETURN (NULL_TREE);
14064 out:
14065 input_location = loc;
14066 return r;
14067 #undef RECUR
14068 #undef RETURN
14071 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14072 function. For description of the body see comment above
14073 cp_parser_omp_declare_reduction_exprs. */
14075 static void
14076 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14078 if (t == NULL_TREE || t == error_mark_node)
14079 return;
14081 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14083 tree_stmt_iterator tsi;
14084 int i;
14085 tree stmts[7];
14086 memset (stmts, 0, sizeof stmts);
14087 for (i = 0, tsi = tsi_start (t);
14088 i < 7 && !tsi_end_p (tsi);
14089 i++, tsi_next (&tsi))
14090 stmts[i] = tsi_stmt (tsi);
14091 gcc_assert (tsi_end_p (tsi));
14093 if (i >= 3)
14095 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14096 && TREE_CODE (stmts[1]) == DECL_EXPR);
14097 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14098 args, complain, in_decl);
14099 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14100 args, complain, in_decl);
14101 DECL_CONTEXT (omp_out) = current_function_decl;
14102 DECL_CONTEXT (omp_in) = current_function_decl;
14103 keep_next_level (true);
14104 tree block = begin_omp_structured_block ();
14105 tsubst_expr (stmts[2], args, complain, in_decl, false);
14106 block = finish_omp_structured_block (block);
14107 block = maybe_cleanup_point_expr_void (block);
14108 add_decl_expr (omp_out);
14109 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14110 TREE_NO_WARNING (omp_out) = 1;
14111 add_decl_expr (omp_in);
14112 finish_expr_stmt (block);
14114 if (i >= 6)
14116 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14117 && TREE_CODE (stmts[4]) == DECL_EXPR);
14118 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14119 args, complain, in_decl);
14120 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14121 args, complain, in_decl);
14122 DECL_CONTEXT (omp_priv) = current_function_decl;
14123 DECL_CONTEXT (omp_orig) = current_function_decl;
14124 keep_next_level (true);
14125 tree block = begin_omp_structured_block ();
14126 tsubst_expr (stmts[5], args, complain, in_decl, false);
14127 block = finish_omp_structured_block (block);
14128 block = maybe_cleanup_point_expr_void (block);
14129 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14130 add_decl_expr (omp_priv);
14131 add_decl_expr (omp_orig);
14132 finish_expr_stmt (block);
14133 if (i == 7)
14134 add_decl_expr (omp_orig);
14138 /* T is a postfix-expression that is not being used in a function
14139 call. Return the substituted version of T. */
14141 static tree
14142 tsubst_non_call_postfix_expression (tree t, tree args,
14143 tsubst_flags_t complain,
14144 tree in_decl)
14146 if (TREE_CODE (t) == SCOPE_REF)
14147 t = tsubst_qualified_id (t, args, complain, in_decl,
14148 /*done=*/false, /*address_p=*/false);
14149 else
14150 t = tsubst_copy_and_build (t, args, complain, in_decl,
14151 /*function_p=*/false,
14152 /*integral_constant_expression_p=*/false);
14154 return t;
14157 /* Like tsubst but deals with expressions and performs semantic
14158 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14160 tree
14161 tsubst_copy_and_build (tree t,
14162 tree args,
14163 tsubst_flags_t complain,
14164 tree in_decl,
14165 bool function_p,
14166 bool integral_constant_expression_p)
14168 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14169 #define RECUR(NODE) \
14170 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14171 /*function_p=*/false, \
14172 integral_constant_expression_p)
14174 tree retval, op1;
14175 location_t loc;
14177 if (t == NULL_TREE || t == error_mark_node)
14178 return t;
14180 loc = input_location;
14181 if (EXPR_HAS_LOCATION (t))
14182 input_location = EXPR_LOCATION (t);
14184 /* N3276 decltype magic only applies to calls at the top level or on the
14185 right side of a comma. */
14186 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14187 complain &= ~tf_decltype;
14189 switch (TREE_CODE (t))
14191 case USING_DECL:
14192 t = DECL_NAME (t);
14193 /* Fall through. */
14194 case IDENTIFIER_NODE:
14196 tree decl;
14197 cp_id_kind idk;
14198 bool non_integral_constant_expression_p;
14199 const char *error_msg;
14201 if (IDENTIFIER_TYPENAME_P (t))
14203 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14204 t = mangle_conv_op_name_for_type (new_type);
14207 /* Look up the name. */
14208 decl = lookup_name (t);
14210 /* By convention, expressions use ERROR_MARK_NODE to indicate
14211 failure, not NULL_TREE. */
14212 if (decl == NULL_TREE)
14213 decl = error_mark_node;
14215 decl = finish_id_expression (t, decl, NULL_TREE,
14216 &idk,
14217 integral_constant_expression_p,
14218 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14219 &non_integral_constant_expression_p,
14220 /*template_p=*/false,
14221 /*done=*/true,
14222 /*address_p=*/false,
14223 /*template_arg_p=*/false,
14224 &error_msg,
14225 input_location);
14226 if (error_msg)
14227 error (error_msg);
14228 if (!function_p && identifier_p (decl))
14230 if (complain & tf_error)
14231 unqualified_name_lookup_error (decl);
14232 decl = error_mark_node;
14234 RETURN (decl);
14237 case TEMPLATE_ID_EXPR:
14239 tree object;
14240 tree templ = RECUR (TREE_OPERAND (t, 0));
14241 tree targs = TREE_OPERAND (t, 1);
14243 if (targs)
14244 targs = tsubst_template_args (targs, args, complain, in_decl);
14246 if (TREE_CODE (templ) == COMPONENT_REF)
14248 object = TREE_OPERAND (templ, 0);
14249 templ = TREE_OPERAND (templ, 1);
14251 else
14252 object = NULL_TREE;
14253 templ = lookup_template_function (templ, targs);
14255 if (object)
14256 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14257 object, templ, NULL_TREE));
14258 else
14259 RETURN (baselink_for_fns (templ));
14262 case INDIRECT_REF:
14264 tree r = RECUR (TREE_OPERAND (t, 0));
14266 if (REFERENCE_REF_P (t))
14268 /* A type conversion to reference type will be enclosed in
14269 such an indirect ref, but the substitution of the cast
14270 will have also added such an indirect ref. */
14271 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14272 r = convert_from_reference (r);
14274 else
14275 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14276 complain|decltype_flag);
14277 RETURN (r);
14280 case NOP_EXPR:
14281 RETURN (build_nop
14282 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14283 RECUR (TREE_OPERAND (t, 0))));
14285 case IMPLICIT_CONV_EXPR:
14287 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14288 tree expr = RECUR (TREE_OPERAND (t, 0));
14289 int flags = LOOKUP_IMPLICIT;
14290 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14291 flags = LOOKUP_NORMAL;
14292 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14293 flags));
14296 case CONVERT_EXPR:
14297 RETURN (build1
14298 (CONVERT_EXPR,
14299 tsubst (TREE_TYPE (t), args, complain, in_decl),
14300 RECUR (TREE_OPERAND (t, 0))));
14302 case CAST_EXPR:
14303 case REINTERPRET_CAST_EXPR:
14304 case CONST_CAST_EXPR:
14305 case DYNAMIC_CAST_EXPR:
14306 case STATIC_CAST_EXPR:
14308 tree type;
14309 tree op, r = NULL_TREE;
14311 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14312 if (integral_constant_expression_p
14313 && !cast_valid_in_integral_constant_expression_p (type))
14315 if (complain & tf_error)
14316 error ("a cast to a type other than an integral or "
14317 "enumeration type cannot appear in a constant-expression");
14318 RETURN (error_mark_node);
14321 op = RECUR (TREE_OPERAND (t, 0));
14323 warning_sentinel s(warn_useless_cast);
14324 switch (TREE_CODE (t))
14326 case CAST_EXPR:
14327 r = build_functional_cast (type, op, complain);
14328 break;
14329 case REINTERPRET_CAST_EXPR:
14330 r = build_reinterpret_cast (type, op, complain);
14331 break;
14332 case CONST_CAST_EXPR:
14333 r = build_const_cast (type, op, complain);
14334 break;
14335 case DYNAMIC_CAST_EXPR:
14336 r = build_dynamic_cast (type, op, complain);
14337 break;
14338 case STATIC_CAST_EXPR:
14339 r = build_static_cast (type, op, complain);
14340 break;
14341 default:
14342 gcc_unreachable ();
14345 RETURN (r);
14348 case POSTDECREMENT_EXPR:
14349 case POSTINCREMENT_EXPR:
14350 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14351 args, complain, in_decl);
14352 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14353 complain|decltype_flag));
14355 case PREDECREMENT_EXPR:
14356 case PREINCREMENT_EXPR:
14357 case NEGATE_EXPR:
14358 case BIT_NOT_EXPR:
14359 case ABS_EXPR:
14360 case TRUTH_NOT_EXPR:
14361 case UNARY_PLUS_EXPR: /* Unary + */
14362 case REALPART_EXPR:
14363 case IMAGPART_EXPR:
14364 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14365 RECUR (TREE_OPERAND (t, 0)),
14366 complain|decltype_flag));
14368 case FIX_TRUNC_EXPR:
14369 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14370 0, complain));
14372 case ADDR_EXPR:
14373 op1 = TREE_OPERAND (t, 0);
14374 if (TREE_CODE (op1) == LABEL_DECL)
14375 RETURN (finish_label_address_expr (DECL_NAME (op1),
14376 EXPR_LOCATION (op1)));
14377 if (TREE_CODE (op1) == SCOPE_REF)
14378 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14379 /*done=*/true, /*address_p=*/true);
14380 else
14381 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14382 in_decl);
14383 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14384 complain|decltype_flag));
14386 case PLUS_EXPR:
14387 case MINUS_EXPR:
14388 case MULT_EXPR:
14389 case TRUNC_DIV_EXPR:
14390 case CEIL_DIV_EXPR:
14391 case FLOOR_DIV_EXPR:
14392 case ROUND_DIV_EXPR:
14393 case EXACT_DIV_EXPR:
14394 case BIT_AND_EXPR:
14395 case BIT_IOR_EXPR:
14396 case BIT_XOR_EXPR:
14397 case TRUNC_MOD_EXPR:
14398 case FLOOR_MOD_EXPR:
14399 case TRUTH_ANDIF_EXPR:
14400 case TRUTH_ORIF_EXPR:
14401 case TRUTH_AND_EXPR:
14402 case TRUTH_OR_EXPR:
14403 case RSHIFT_EXPR:
14404 case LSHIFT_EXPR:
14405 case RROTATE_EXPR:
14406 case LROTATE_EXPR:
14407 case EQ_EXPR:
14408 case NE_EXPR:
14409 case MAX_EXPR:
14410 case MIN_EXPR:
14411 case LE_EXPR:
14412 case GE_EXPR:
14413 case LT_EXPR:
14414 case GT_EXPR:
14415 case MEMBER_REF:
14416 case DOTSTAR_EXPR:
14418 warning_sentinel s1(warn_type_limits);
14419 warning_sentinel s2(warn_div_by_zero);
14420 tree r = build_x_binary_op
14421 (input_location, TREE_CODE (t),
14422 RECUR (TREE_OPERAND (t, 0)),
14423 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14424 ? ERROR_MARK
14425 : TREE_CODE (TREE_OPERAND (t, 0))),
14426 RECUR (TREE_OPERAND (t, 1)),
14427 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14428 ? ERROR_MARK
14429 : TREE_CODE (TREE_OPERAND (t, 1))),
14430 /*overload=*/NULL,
14431 complain|decltype_flag);
14432 if (EXPR_P (r) && TREE_NO_WARNING (t))
14433 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14435 RETURN (r);
14438 case POINTER_PLUS_EXPR:
14439 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14440 RECUR (TREE_OPERAND (t, 1)));
14442 case SCOPE_REF:
14443 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14444 /*address_p=*/false));
14445 case ARRAY_REF:
14446 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14447 args, complain, in_decl);
14448 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14449 RECUR (TREE_OPERAND (t, 1)),
14450 complain|decltype_flag));
14452 case ARRAY_NOTATION_REF:
14454 tree start_index, length, stride;
14455 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14456 args, complain, in_decl);
14457 start_index = RECUR (ARRAY_NOTATION_START (t));
14458 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14459 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14460 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14461 length, stride, TREE_TYPE (op1)));
14463 case SIZEOF_EXPR:
14464 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14465 RETURN (tsubst_copy (t, args, complain, in_decl));
14466 /* Fall through */
14468 case ALIGNOF_EXPR:
14470 tree r;
14472 op1 = TREE_OPERAND (t, 0);
14473 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14474 op1 = TREE_TYPE (op1);
14475 if (!args)
14477 /* When there are no ARGS, we are trying to evaluate a
14478 non-dependent expression from the parser. Trying to do
14479 the substitutions may not work. */
14480 if (!TYPE_P (op1))
14481 op1 = TREE_TYPE (op1);
14483 else
14485 ++cp_unevaluated_operand;
14486 ++c_inhibit_evaluation_warnings;
14487 if (TYPE_P (op1))
14488 op1 = tsubst (op1, args, complain, in_decl);
14489 else
14490 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14491 /*function_p=*/false,
14492 /*integral_constant_expression_p=*/
14493 false);
14494 --cp_unevaluated_operand;
14495 --c_inhibit_evaluation_warnings;
14497 if (TYPE_P (op1))
14498 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14499 complain & tf_error);
14500 else
14501 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14502 complain & tf_error);
14503 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14505 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14507 if (!processing_template_decl && TYPE_P (op1))
14509 r = build_min (SIZEOF_EXPR, size_type_node,
14510 build1 (NOP_EXPR, op1, error_mark_node));
14511 SIZEOF_EXPR_TYPE_P (r) = 1;
14513 else
14514 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14515 TREE_SIDE_EFFECTS (r) = 0;
14516 TREE_READONLY (r) = 1;
14518 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14520 RETURN (r);
14523 case AT_ENCODE_EXPR:
14525 op1 = TREE_OPERAND (t, 0);
14526 ++cp_unevaluated_operand;
14527 ++c_inhibit_evaluation_warnings;
14528 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14529 /*function_p=*/false,
14530 /*integral_constant_expression_p=*/false);
14531 --cp_unevaluated_operand;
14532 --c_inhibit_evaluation_warnings;
14533 RETURN (objc_build_encode_expr (op1));
14536 case NOEXCEPT_EXPR:
14537 op1 = TREE_OPERAND (t, 0);
14538 ++cp_unevaluated_operand;
14539 ++c_inhibit_evaluation_warnings;
14540 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14541 /*function_p=*/false,
14542 /*integral_constant_expression_p=*/false);
14543 --cp_unevaluated_operand;
14544 --c_inhibit_evaluation_warnings;
14545 RETURN (finish_noexcept_expr (op1, complain));
14547 case MODOP_EXPR:
14549 warning_sentinel s(warn_div_by_zero);
14550 tree r = build_x_modify_expr
14551 (EXPR_LOCATION (t),
14552 RECUR (TREE_OPERAND (t, 0)),
14553 TREE_CODE (TREE_OPERAND (t, 1)),
14554 RECUR (TREE_OPERAND (t, 2)),
14555 complain|decltype_flag);
14556 /* TREE_NO_WARNING must be set if either the expression was
14557 parenthesized or it uses an operator such as >>= rather
14558 than plain assignment. In the former case, it was already
14559 set and must be copied. In the latter case,
14560 build_x_modify_expr sets it and it must not be reset
14561 here. */
14562 if (TREE_NO_WARNING (t))
14563 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14565 RETURN (r);
14568 case ARROW_EXPR:
14569 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14570 args, complain, in_decl);
14571 /* Remember that there was a reference to this entity. */
14572 if (DECL_P (op1))
14573 mark_used (op1);
14574 RETURN (build_x_arrow (input_location, op1, complain));
14576 case NEW_EXPR:
14578 tree placement = RECUR (TREE_OPERAND (t, 0));
14579 tree init = RECUR (TREE_OPERAND (t, 3));
14580 vec<tree, va_gc> *placement_vec;
14581 vec<tree, va_gc> *init_vec;
14582 tree ret;
14584 if (placement == NULL_TREE)
14585 placement_vec = NULL;
14586 else
14588 placement_vec = make_tree_vector ();
14589 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14590 vec_safe_push (placement_vec, TREE_VALUE (placement));
14593 /* If there was an initializer in the original tree, but it
14594 instantiated to an empty list, then we should pass a
14595 non-NULL empty vector to tell build_new that it was an
14596 empty initializer() rather than no initializer. This can
14597 only happen when the initializer is a pack expansion whose
14598 parameter packs are of length zero. */
14599 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14600 init_vec = NULL;
14601 else
14603 init_vec = make_tree_vector ();
14604 if (init == void_zero_node)
14605 gcc_assert (init_vec != NULL);
14606 else
14608 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14609 vec_safe_push (init_vec, TREE_VALUE (init));
14613 ret = build_new (&placement_vec,
14614 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14615 RECUR (TREE_OPERAND (t, 2)),
14616 &init_vec,
14617 NEW_EXPR_USE_GLOBAL (t),
14618 complain);
14620 if (placement_vec != NULL)
14621 release_tree_vector (placement_vec);
14622 if (init_vec != NULL)
14623 release_tree_vector (init_vec);
14625 RETURN (ret);
14628 case DELETE_EXPR:
14629 RETURN (delete_sanity
14630 (RECUR (TREE_OPERAND (t, 0)),
14631 RECUR (TREE_OPERAND (t, 1)),
14632 DELETE_EXPR_USE_VEC (t),
14633 DELETE_EXPR_USE_GLOBAL (t),
14634 complain));
14636 case COMPOUND_EXPR:
14638 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14639 complain & ~tf_decltype, in_decl,
14640 /*function_p=*/false,
14641 integral_constant_expression_p);
14642 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14643 op0,
14644 RECUR (TREE_OPERAND (t, 1)),
14645 complain|decltype_flag));
14648 case CALL_EXPR:
14650 tree function;
14651 vec<tree, va_gc> *call_args;
14652 unsigned int nargs, i;
14653 bool qualified_p;
14654 bool koenig_p;
14655 tree ret;
14657 function = CALL_EXPR_FN (t);
14658 /* When we parsed the expression, we determined whether or
14659 not Koenig lookup should be performed. */
14660 koenig_p = KOENIG_LOOKUP_P (t);
14661 if (TREE_CODE (function) == SCOPE_REF)
14663 qualified_p = true;
14664 function = tsubst_qualified_id (function, args, complain, in_decl,
14665 /*done=*/false,
14666 /*address_p=*/false);
14668 else if (koenig_p && identifier_p (function))
14670 /* Do nothing; calling tsubst_copy_and_build on an identifier
14671 would incorrectly perform unqualified lookup again.
14673 Note that we can also have an IDENTIFIER_NODE if the earlier
14674 unqualified lookup found a member function; in that case
14675 koenig_p will be false and we do want to do the lookup
14676 again to find the instantiated member function.
14678 FIXME but doing that causes c++/15272, so we need to stop
14679 using IDENTIFIER_NODE in that situation. */
14680 qualified_p = false;
14682 else
14684 if (TREE_CODE (function) == COMPONENT_REF)
14686 tree op = TREE_OPERAND (function, 1);
14688 qualified_p = (TREE_CODE (op) == SCOPE_REF
14689 || (BASELINK_P (op)
14690 && BASELINK_QUALIFIED_P (op)));
14692 else
14693 qualified_p = false;
14695 if (TREE_CODE (function) == ADDR_EXPR
14696 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14697 /* Avoid error about taking the address of a constructor. */
14698 function = TREE_OPERAND (function, 0);
14700 function = tsubst_copy_and_build (function, args, complain,
14701 in_decl,
14702 !qualified_p,
14703 integral_constant_expression_p);
14705 if (BASELINK_P (function))
14706 qualified_p = true;
14709 nargs = call_expr_nargs (t);
14710 call_args = make_tree_vector ();
14711 for (i = 0; i < nargs; ++i)
14713 tree arg = CALL_EXPR_ARG (t, i);
14715 if (!PACK_EXPANSION_P (arg))
14716 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14717 else
14719 /* Expand the pack expansion and push each entry onto
14720 CALL_ARGS. */
14721 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14722 if (TREE_CODE (arg) == TREE_VEC)
14724 unsigned int len, j;
14726 len = TREE_VEC_LENGTH (arg);
14727 for (j = 0; j < len; ++j)
14729 tree value = TREE_VEC_ELT (arg, j);
14730 if (value != NULL_TREE)
14731 value = convert_from_reference (value);
14732 vec_safe_push (call_args, value);
14735 else
14737 /* A partial substitution. Add one entry. */
14738 vec_safe_push (call_args, arg);
14743 /* We do not perform argument-dependent lookup if normal
14744 lookup finds a non-function, in accordance with the
14745 expected resolution of DR 218. */
14746 if (koenig_p
14747 && ((is_overloaded_fn (function)
14748 /* If lookup found a member function, the Koenig lookup is
14749 not appropriate, even if an unqualified-name was used
14750 to denote the function. */
14751 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14752 || identifier_p (function))
14753 /* Only do this when substitution turns a dependent call
14754 into a non-dependent call. */
14755 && type_dependent_expression_p_push (t)
14756 && !any_type_dependent_arguments_p (call_args))
14757 function = perform_koenig_lookup (function, call_args, tf_none);
14759 if (identifier_p (function)
14760 && !any_type_dependent_arguments_p (call_args))
14762 if (koenig_p && (complain & tf_warning_or_error))
14764 /* For backwards compatibility and good diagnostics, try
14765 the unqualified lookup again if we aren't in SFINAE
14766 context. */
14767 tree unq = (tsubst_copy_and_build
14768 (function, args, complain, in_decl, true,
14769 integral_constant_expression_p));
14770 if (unq == error_mark_node)
14771 RETURN (error_mark_node);
14773 if (unq != function)
14775 tree fn = unq;
14776 if (INDIRECT_REF_P (fn))
14777 fn = TREE_OPERAND (fn, 0);
14778 if (TREE_CODE (fn) == COMPONENT_REF)
14779 fn = TREE_OPERAND (fn, 1);
14780 if (is_overloaded_fn (fn))
14781 fn = get_first_fn (fn);
14782 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
14783 "%qD was not declared in this scope, "
14784 "and no declarations were found by "
14785 "argument-dependent lookup at the point "
14786 "of instantiation", function))
14788 if (!DECL_P (fn))
14789 /* Can't say anything more. */;
14790 else if (DECL_CLASS_SCOPE_P (fn))
14792 location_t loc = EXPR_LOC_OR_LOC (t,
14793 input_location);
14794 inform (loc,
14795 "declarations in dependent base %qT are "
14796 "not found by unqualified lookup",
14797 DECL_CLASS_CONTEXT (fn));
14798 if (current_class_ptr)
14799 inform (loc,
14800 "use %<this->%D%> instead", function);
14801 else
14802 inform (loc,
14803 "use %<%T::%D%> instead",
14804 current_class_name, function);
14806 else
14807 inform (0, "%q+D declared here, later in the "
14808 "translation unit", fn);
14810 function = unq;
14813 if (identifier_p (function))
14815 if (complain & tf_error)
14816 unqualified_name_lookup_error (function);
14817 release_tree_vector (call_args);
14818 RETURN (error_mark_node);
14822 /* Remember that there was a reference to this entity. */
14823 if (DECL_P (function))
14824 mark_used (function, complain);
14826 /* Put back tf_decltype for the actual call. */
14827 complain |= decltype_flag;
14829 if (TREE_CODE (function) == OFFSET_REF)
14830 ret = build_offset_ref_call_from_tree (function, &call_args,
14831 complain);
14832 else if (TREE_CODE (function) == COMPONENT_REF)
14834 tree instance = TREE_OPERAND (function, 0);
14835 tree fn = TREE_OPERAND (function, 1);
14837 if (processing_template_decl
14838 && (type_dependent_expression_p (instance)
14839 || (!BASELINK_P (fn)
14840 && TREE_CODE (fn) != FIELD_DECL)
14841 || type_dependent_expression_p (fn)
14842 || any_type_dependent_arguments_p (call_args)))
14843 ret = build_nt_call_vec (function, call_args);
14844 else if (!BASELINK_P (fn))
14845 ret = finish_call_expr (function, &call_args,
14846 /*disallow_virtual=*/false,
14847 /*koenig_p=*/false,
14848 complain);
14849 else
14850 ret = (build_new_method_call
14851 (instance, fn,
14852 &call_args, NULL_TREE,
14853 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14854 /*fn_p=*/NULL,
14855 complain));
14857 else
14858 ret = finish_call_expr (function, &call_args,
14859 /*disallow_virtual=*/qualified_p,
14860 koenig_p,
14861 complain);
14863 release_tree_vector (call_args);
14865 RETURN (ret);
14868 case COND_EXPR:
14870 tree cond = RECUR (TREE_OPERAND (t, 0));
14871 tree folded_cond = (maybe_constant_value
14872 (fold_non_dependent_expr_sfinae (cond, tf_none)));
14873 tree exp1, exp2;
14875 if (TREE_CODE (folded_cond) == INTEGER_CST)
14877 if (integer_zerop (folded_cond))
14879 ++c_inhibit_evaluation_warnings;
14880 exp1 = RECUR (TREE_OPERAND (t, 1));
14881 --c_inhibit_evaluation_warnings;
14882 exp2 = RECUR (TREE_OPERAND (t, 2));
14884 else
14886 exp1 = RECUR (TREE_OPERAND (t, 1));
14887 ++c_inhibit_evaluation_warnings;
14888 exp2 = RECUR (TREE_OPERAND (t, 2));
14889 --c_inhibit_evaluation_warnings;
14891 cond = folded_cond;
14893 else
14895 exp1 = RECUR (TREE_OPERAND (t, 1));
14896 exp2 = RECUR (TREE_OPERAND (t, 2));
14899 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14900 cond, exp1, exp2, complain));
14903 case PSEUDO_DTOR_EXPR:
14904 RETURN (finish_pseudo_destructor_expr
14905 (RECUR (TREE_OPERAND (t, 0)),
14906 RECUR (TREE_OPERAND (t, 1)),
14907 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14908 input_location));
14910 case TREE_LIST:
14912 tree purpose, value, chain;
14914 if (t == void_list_node)
14915 RETURN (t);
14917 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14918 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14920 /* We have pack expansions, so expand those and
14921 create a new list out of it. */
14922 tree purposevec = NULL_TREE;
14923 tree valuevec = NULL_TREE;
14924 tree chain;
14925 int i, len = -1;
14927 /* Expand the argument expressions. */
14928 if (TREE_PURPOSE (t))
14929 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14930 complain, in_decl);
14931 if (TREE_VALUE (t))
14932 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14933 complain, in_decl);
14935 /* Build the rest of the list. */
14936 chain = TREE_CHAIN (t);
14937 if (chain && chain != void_type_node)
14938 chain = RECUR (chain);
14940 /* Determine the number of arguments. */
14941 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14943 len = TREE_VEC_LENGTH (purposevec);
14944 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14946 else if (TREE_CODE (valuevec) == TREE_VEC)
14947 len = TREE_VEC_LENGTH (valuevec);
14948 else
14950 /* Since we only performed a partial substitution into
14951 the argument pack, we only RETURN (a single list
14952 node. */
14953 if (purposevec == TREE_PURPOSE (t)
14954 && valuevec == TREE_VALUE (t)
14955 && chain == TREE_CHAIN (t))
14956 RETURN (t);
14958 RETURN (tree_cons (purposevec, valuevec, chain));
14961 /* Convert the argument vectors into a TREE_LIST */
14962 i = len;
14963 while (i > 0)
14965 /* Grab the Ith values. */
14966 i--;
14967 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14968 : NULL_TREE;
14969 value
14970 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14971 : NULL_TREE;
14973 /* Build the list (backwards). */
14974 chain = tree_cons (purpose, value, chain);
14977 RETURN (chain);
14980 purpose = TREE_PURPOSE (t);
14981 if (purpose)
14982 purpose = RECUR (purpose);
14983 value = TREE_VALUE (t);
14984 if (value)
14985 value = RECUR (value);
14986 chain = TREE_CHAIN (t);
14987 if (chain && chain != void_type_node)
14988 chain = RECUR (chain);
14989 if (purpose == TREE_PURPOSE (t)
14990 && value == TREE_VALUE (t)
14991 && chain == TREE_CHAIN (t))
14992 RETURN (t);
14993 RETURN (tree_cons (purpose, value, chain));
14996 case COMPONENT_REF:
14998 tree object;
14999 tree object_type;
15000 tree member;
15001 tree r;
15003 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15004 args, complain, in_decl);
15005 /* Remember that there was a reference to this entity. */
15006 if (DECL_P (object))
15007 mark_used (object);
15008 object_type = TREE_TYPE (object);
15010 member = TREE_OPERAND (t, 1);
15011 if (BASELINK_P (member))
15012 member = tsubst_baselink (member,
15013 non_reference (TREE_TYPE (object)),
15014 args, complain, in_decl);
15015 else
15016 member = tsubst_copy (member, args, complain, in_decl);
15017 if (member == error_mark_node)
15018 RETURN (error_mark_node);
15020 if (type_dependent_expression_p (object))
15021 /* We can't do much here. */;
15022 else if (!CLASS_TYPE_P (object_type))
15024 if (scalarish_type_p (object_type))
15026 tree s = NULL_TREE;
15027 tree dtor = member;
15029 if (TREE_CODE (dtor) == SCOPE_REF)
15031 s = TREE_OPERAND (dtor, 0);
15032 dtor = TREE_OPERAND (dtor, 1);
15034 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15036 dtor = TREE_OPERAND (dtor, 0);
15037 if (TYPE_P (dtor))
15038 RETURN (finish_pseudo_destructor_expr
15039 (object, s, dtor, input_location));
15043 else if (TREE_CODE (member) == SCOPE_REF
15044 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15046 /* Lookup the template functions now that we know what the
15047 scope is. */
15048 tree scope = TREE_OPERAND (member, 0);
15049 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15050 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15051 member = lookup_qualified_name (scope, tmpl,
15052 /*is_type_p=*/false,
15053 /*complain=*/false);
15054 if (BASELINK_P (member))
15056 BASELINK_FUNCTIONS (member)
15057 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15058 args);
15059 member = (adjust_result_of_qualified_name_lookup
15060 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15061 object_type));
15063 else
15065 qualified_name_lookup_error (scope, tmpl, member,
15066 input_location);
15067 RETURN (error_mark_node);
15070 else if (TREE_CODE (member) == SCOPE_REF
15071 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15072 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15074 if (complain & tf_error)
15076 if (TYPE_P (TREE_OPERAND (member, 0)))
15077 error ("%qT is not a class or namespace",
15078 TREE_OPERAND (member, 0));
15079 else
15080 error ("%qD is not a class or namespace",
15081 TREE_OPERAND (member, 0));
15083 RETURN (error_mark_node);
15085 else if (TREE_CODE (member) == FIELD_DECL)
15087 r = finish_non_static_data_member (member, object, NULL_TREE);
15088 if (TREE_CODE (r) == COMPONENT_REF)
15089 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15090 RETURN (r);
15093 r = finish_class_member_access_expr (object, member,
15094 /*template_p=*/false,
15095 complain);
15096 if (TREE_CODE (r) == COMPONENT_REF)
15097 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15098 RETURN (r);
15101 case THROW_EXPR:
15102 RETURN (build_throw
15103 (RECUR (TREE_OPERAND (t, 0))));
15105 case CONSTRUCTOR:
15107 vec<constructor_elt, va_gc> *n;
15108 constructor_elt *ce;
15109 unsigned HOST_WIDE_INT idx;
15110 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15111 bool process_index_p;
15112 int newlen;
15113 bool need_copy_p = false;
15114 tree r;
15116 if (type == error_mark_node)
15117 RETURN (error_mark_node);
15119 /* digest_init will do the wrong thing if we let it. */
15120 if (type && TYPE_PTRMEMFUNC_P (type))
15121 RETURN (t);
15123 /* We do not want to process the index of aggregate
15124 initializers as they are identifier nodes which will be
15125 looked up by digest_init. */
15126 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15128 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15129 newlen = vec_safe_length (n);
15130 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15132 if (ce->index && process_index_p
15133 /* An identifier index is looked up in the type
15134 being initialized, not the current scope. */
15135 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15136 ce->index = RECUR (ce->index);
15138 if (PACK_EXPANSION_P (ce->value))
15140 /* Substitute into the pack expansion. */
15141 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15142 in_decl);
15144 if (ce->value == error_mark_node
15145 || PACK_EXPANSION_P (ce->value))
15147 else if (TREE_VEC_LENGTH (ce->value) == 1)
15148 /* Just move the argument into place. */
15149 ce->value = TREE_VEC_ELT (ce->value, 0);
15150 else
15152 /* Update the length of the final CONSTRUCTOR
15153 arguments vector, and note that we will need to
15154 copy.*/
15155 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15156 need_copy_p = true;
15159 else
15160 ce->value = RECUR (ce->value);
15163 if (need_copy_p)
15165 vec<constructor_elt, va_gc> *old_n = n;
15167 vec_alloc (n, newlen);
15168 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15170 if (TREE_CODE (ce->value) == TREE_VEC)
15172 int i, len = TREE_VEC_LENGTH (ce->value);
15173 for (i = 0; i < len; ++i)
15174 CONSTRUCTOR_APPEND_ELT (n, 0,
15175 TREE_VEC_ELT (ce->value, i));
15177 else
15178 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15182 r = build_constructor (init_list_type_node, n);
15183 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15185 if (TREE_HAS_CONSTRUCTOR (t))
15186 RETURN (finish_compound_literal (type, r, complain));
15188 TREE_TYPE (r) = type;
15189 RETURN (r);
15192 case TYPEID_EXPR:
15194 tree operand_0 = TREE_OPERAND (t, 0);
15195 if (TYPE_P (operand_0))
15197 operand_0 = tsubst (operand_0, args, complain, in_decl);
15198 RETURN (get_typeid (operand_0, complain));
15200 else
15202 operand_0 = RECUR (operand_0);
15203 RETURN (build_typeid (operand_0, complain));
15207 case VAR_DECL:
15208 if (!args)
15209 RETURN (t);
15210 else if (DECL_PACK_P (t))
15212 /* We don't build decls for an instantiation of a
15213 variadic capture proxy, we instantiate the elements
15214 when needed. */
15215 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15216 return RECUR (DECL_VALUE_EXPR (t));
15218 /* Fall through */
15220 case PARM_DECL:
15222 tree r = tsubst_copy (t, args, complain, in_decl);
15223 if (TREE_CODE (r) == VAR_DECL
15224 && !processing_template_decl
15225 && !cp_unevaluated_operand
15226 && DECL_THREAD_LOCAL_P (r))
15228 if (tree wrap = get_tls_wrapper_fn (r))
15229 /* Replace an evaluated use of the thread_local variable with
15230 a call to its wrapper. */
15231 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
15234 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15235 /* If the original type was a reference, we'll be wrapped in
15236 the appropriate INDIRECT_REF. */
15237 r = convert_from_reference (r);
15238 RETURN (r);
15241 case VA_ARG_EXPR:
15242 RETURN (build_x_va_arg (EXPR_LOCATION (t),
15243 RECUR (TREE_OPERAND (t, 0)),
15244 tsubst (TREE_TYPE (t), args, complain, in_decl)));
15246 case OFFSETOF_EXPR:
15247 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
15249 case TRAIT_EXPR:
15251 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15252 complain, in_decl);
15254 tree type2 = TRAIT_EXPR_TYPE2 (t);
15255 if (type2)
15256 type2 = tsubst (type2, args, complain, in_decl);
15258 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15261 case STMT_EXPR:
15263 tree old_stmt_expr = cur_stmt_expr;
15264 tree stmt_expr = begin_stmt_expr ();
15266 cur_stmt_expr = stmt_expr;
15267 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15268 integral_constant_expression_p);
15269 stmt_expr = finish_stmt_expr (stmt_expr, false);
15270 cur_stmt_expr = old_stmt_expr;
15272 /* If the resulting list of expression statement is empty,
15273 fold it further into void_zero_node. */
15274 if (empty_expr_stmt_p (stmt_expr))
15275 stmt_expr = void_zero_node;
15277 RETURN (stmt_expr);
15280 case LAMBDA_EXPR:
15282 tree r = build_lambda_expr ();
15284 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15285 LAMBDA_EXPR_CLOSURE (r) = type;
15286 CLASSTYPE_LAMBDA_EXPR (type) = r;
15288 LAMBDA_EXPR_LOCATION (r)
15289 = LAMBDA_EXPR_LOCATION (t);
15290 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15291 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15292 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15293 LAMBDA_EXPR_DISCRIMINATOR (r)
15294 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15295 /* For a function scope, we want to use tsubst so that we don't
15296 complain about referring to an auto function before its return
15297 type has been deduced. Otherwise, we want to use tsubst_copy so
15298 that we look up the existing field/parameter/variable rather
15299 than build a new one. */
15300 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15301 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15302 scope = tsubst (scope, args, complain, in_decl);
15303 else if (scope && TREE_CODE (scope) == PARM_DECL)
15305 /* Look up the parameter we want directly, as tsubst_copy
15306 doesn't do what we need. */
15307 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15308 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15309 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15310 parm = DECL_CHAIN (parm);
15311 scope = parm;
15312 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15313 if (DECL_CONTEXT (scope) == NULL_TREE)
15314 DECL_CONTEXT (scope) = fn;
15316 else
15317 scope = RECUR (scope);
15318 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15319 LAMBDA_EXPR_RETURN_TYPE (r)
15320 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15322 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15323 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15325 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15326 determine_visibility (TYPE_NAME (type));
15327 /* Now that we know visibility, instantiate the type so we have a
15328 declaration of the op() for later calls to lambda_function. */
15329 complete_type (type);
15331 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15333 RETURN (build_lambda_object (r));
15336 case TARGET_EXPR:
15337 /* We can get here for a constant initializer of non-dependent type.
15338 FIXME stop folding in cp_parser_initializer_clause. */
15340 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15341 complain);
15342 RETURN (r);
15345 case TRANSACTION_EXPR:
15346 RETURN (tsubst_expr(t, args, complain, in_decl,
15347 integral_constant_expression_p));
15349 case PAREN_EXPR:
15350 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15352 case VEC_PERM_EXPR:
15353 RETURN (build_x_vec_perm_expr (input_location,
15354 RECUR (TREE_OPERAND (t, 0)),
15355 RECUR (TREE_OPERAND (t, 1)),
15356 RECUR (TREE_OPERAND (t, 2)),
15357 complain));
15359 default:
15360 /* Handle Objective-C++ constructs, if appropriate. */
15362 tree subst
15363 = objcp_tsubst_copy_and_build (t, args, complain,
15364 in_decl, /*function_p=*/false);
15365 if (subst)
15366 RETURN (subst);
15368 RETURN (tsubst_copy (t, args, complain, in_decl));
15371 #undef RECUR
15372 #undef RETURN
15373 out:
15374 input_location = loc;
15375 return retval;
15378 /* Verify that the instantiated ARGS are valid. For type arguments,
15379 make sure that the type's linkage is ok. For non-type arguments,
15380 make sure they are constants if they are integral or enumerations.
15381 Emit an error under control of COMPLAIN, and return TRUE on error. */
15383 static bool
15384 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15386 if (dependent_template_arg_p (t))
15387 return false;
15388 if (ARGUMENT_PACK_P (t))
15390 tree vec = ARGUMENT_PACK_ARGS (t);
15391 int len = TREE_VEC_LENGTH (vec);
15392 bool result = false;
15393 int i;
15395 for (i = 0; i < len; ++i)
15396 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15397 result = true;
15398 return result;
15400 else if (TYPE_P (t))
15402 /* [basic.link]: A name with no linkage (notably, the name
15403 of a class or enumeration declared in a local scope)
15404 shall not be used to declare an entity with linkage.
15405 This implies that names with no linkage cannot be used as
15406 template arguments
15408 DR 757 relaxes this restriction for C++0x. */
15409 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15410 : no_linkage_check (t, /*relaxed_p=*/false));
15412 if (nt)
15414 /* DR 488 makes use of a type with no linkage cause
15415 type deduction to fail. */
15416 if (complain & tf_error)
15418 if (TYPE_ANONYMOUS_P (nt))
15419 error ("%qT is/uses anonymous type", t);
15420 else
15421 error ("template argument for %qD uses local type %qT",
15422 tmpl, t);
15424 return true;
15426 /* In order to avoid all sorts of complications, we do not
15427 allow variably-modified types as template arguments. */
15428 else if (variably_modified_type_p (t, NULL_TREE))
15430 if (complain & tf_error)
15431 error ("%qT is a variably modified type", t);
15432 return true;
15435 /* Class template and alias template arguments should be OK. */
15436 else if (DECL_TYPE_TEMPLATE_P (t))
15438 /* A non-type argument of integral or enumerated type must be a
15439 constant. */
15440 else if (TREE_TYPE (t)
15441 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15442 && !REFERENCE_REF_P (t)
15443 && !TREE_CONSTANT (t))
15445 if (complain & tf_error)
15446 error ("integral expression %qE is not constant", t);
15447 return true;
15449 return false;
15452 static bool
15453 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15455 int ix, len = DECL_NTPARMS (tmpl);
15456 bool result = false;
15458 for (ix = 0; ix != len; ix++)
15460 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15461 result = true;
15463 if (result && (complain & tf_error))
15464 error (" trying to instantiate %qD", tmpl);
15465 return result;
15468 /* We're out of SFINAE context now, so generate diagnostics for the access
15469 errors we saw earlier when instantiating D from TMPL and ARGS. */
15471 static void
15472 recheck_decl_substitution (tree d, tree tmpl, tree args)
15474 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15475 tree type = TREE_TYPE (pattern);
15476 location_t loc = input_location;
15478 push_access_scope (d);
15479 push_deferring_access_checks (dk_no_deferred);
15480 input_location = DECL_SOURCE_LOCATION (pattern);
15481 tsubst (type, args, tf_warning_or_error, d);
15482 input_location = loc;
15483 pop_deferring_access_checks ();
15484 pop_access_scope (d);
15487 /* Instantiate the indicated variable, function, or alias template TMPL with
15488 the template arguments in TARG_PTR. */
15490 static tree
15491 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15493 tree targ_ptr = orig_args;
15494 tree fndecl;
15495 tree gen_tmpl;
15496 tree spec;
15497 bool access_ok = true;
15499 if (tmpl == error_mark_node)
15500 return error_mark_node;
15502 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15504 /* If this function is a clone, handle it specially. */
15505 if (DECL_CLONED_FUNCTION_P (tmpl))
15507 tree spec;
15508 tree clone;
15510 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15511 DECL_CLONED_FUNCTION. */
15512 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15513 targ_ptr, complain);
15514 if (spec == error_mark_node)
15515 return error_mark_node;
15517 /* Look for the clone. */
15518 FOR_EACH_CLONE (clone, spec)
15519 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15520 return clone;
15521 /* We should always have found the clone by now. */
15522 gcc_unreachable ();
15523 return NULL_TREE;
15526 if (targ_ptr == error_mark_node)
15527 return error_mark_node;
15529 /* Check to see if we already have this specialization. */
15530 gen_tmpl = most_general_template (tmpl);
15531 if (tmpl != gen_tmpl)
15532 /* The TMPL is a partial instantiation. To get a full set of
15533 arguments we must add the arguments used to perform the
15534 partial instantiation. */
15535 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15536 targ_ptr);
15538 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15539 but it doesn't seem to be on the hot path. */
15540 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15542 gcc_assert (tmpl == gen_tmpl
15543 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15544 == spec)
15545 || fndecl == NULL_TREE);
15547 if (spec != NULL_TREE)
15549 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15551 if (complain & tf_error)
15552 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15553 return error_mark_node;
15555 return spec;
15558 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15559 complain))
15560 return error_mark_node;
15562 /* We are building a FUNCTION_DECL, during which the access of its
15563 parameters and return types have to be checked. However this
15564 FUNCTION_DECL which is the desired context for access checking
15565 is not built yet. We solve this chicken-and-egg problem by
15566 deferring all checks until we have the FUNCTION_DECL. */
15567 push_deferring_access_checks (dk_deferred);
15569 /* Instantiation of the function happens in the context of the function
15570 template, not the context of the overload resolution we're doing. */
15571 push_to_top_level ();
15572 /* If there are dependent arguments, e.g. because we're doing partial
15573 ordering, make sure processing_template_decl stays set. */
15574 if (uses_template_parms (targ_ptr))
15575 ++processing_template_decl;
15576 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15578 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15579 complain, gen_tmpl);
15580 push_nested_class (ctx);
15582 /* Substitute template parameters to obtain the specialization. */
15583 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15584 targ_ptr, complain, gen_tmpl);
15585 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15586 pop_nested_class ();
15587 pop_from_top_level ();
15589 if (fndecl == error_mark_node)
15591 pop_deferring_access_checks ();
15592 return error_mark_node;
15595 /* The DECL_TI_TEMPLATE should always be the immediate parent
15596 template, not the most general template. */
15597 DECL_TI_TEMPLATE (fndecl) = tmpl;
15599 /* Now we know the specialization, compute access previously
15600 deferred. */
15601 push_access_scope (fndecl);
15602 if (!perform_deferred_access_checks (complain))
15603 access_ok = false;
15604 pop_access_scope (fndecl);
15605 pop_deferring_access_checks ();
15607 /* If we've just instantiated the main entry point for a function,
15608 instantiate all the alternate entry points as well. We do this
15609 by cloning the instantiation of the main entry point, not by
15610 instantiating the template clones. */
15611 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15612 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15614 if (!access_ok)
15616 if (!(complain & tf_error))
15618 /* Remember to reinstantiate when we're out of SFINAE so the user
15619 can see the errors. */
15620 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15622 return error_mark_node;
15624 return fndecl;
15627 /* Wrapper for instantiate_template_1. */
15629 tree
15630 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15632 tree ret;
15633 timevar_push (TV_TEMPLATE_INST);
15634 ret = instantiate_template_1 (tmpl, orig_args, complain);
15635 timevar_pop (TV_TEMPLATE_INST);
15636 return ret;
15639 /* Instantiate the alias template TMPL with ARGS. Also push a template
15640 instantiation level, which instantiate_template doesn't do because
15641 functions and variables have sufficient context established by the
15642 callers. */
15644 static tree
15645 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15647 struct pending_template *old_last_pend = last_pending_template;
15648 struct tinst_level *old_error_tinst = last_error_tinst_level;
15649 if (tmpl == error_mark_node || args == error_mark_node)
15650 return error_mark_node;
15651 tree tinst = build_tree_list (tmpl, args);
15652 if (!push_tinst_level (tinst))
15654 ggc_free (tinst);
15655 return error_mark_node;
15658 args =
15659 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15660 args, tmpl, complain,
15661 /*require_all_args=*/true,
15662 /*use_default_args=*/true);
15664 tree r = instantiate_template (tmpl, args, complain);
15665 pop_tinst_level ();
15666 /* We can't free this if a pending_template entry or last_error_tinst_level
15667 is pointing at it. */
15668 if (last_pending_template == old_last_pend
15669 && last_error_tinst_level == old_error_tinst)
15670 ggc_free (tinst);
15672 return r;
15675 /* PARM is a template parameter pack for FN. Returns true iff
15676 PARM is used in a deducible way in the argument list of FN. */
15678 static bool
15679 pack_deducible_p (tree parm, tree fn)
15681 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15682 for (; t; t = TREE_CHAIN (t))
15684 tree type = TREE_VALUE (t);
15685 tree packs;
15686 if (!PACK_EXPANSION_P (type))
15687 continue;
15688 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15689 packs; packs = TREE_CHAIN (packs))
15690 if (template_args_equal (TREE_VALUE (packs), parm))
15692 /* The template parameter pack is used in a function parameter
15693 pack. If this is the end of the parameter list, the
15694 template parameter pack is deducible. */
15695 if (TREE_CHAIN (t) == void_list_node)
15696 return true;
15697 else
15698 /* Otherwise, not. Well, it could be deduced from
15699 a non-pack parameter, but doing so would end up with
15700 a deduction mismatch, so don't bother. */
15701 return false;
15704 /* The template parameter pack isn't used in any function parameter
15705 packs, but it might be used deeper, e.g. tuple<Args...>. */
15706 return true;
15709 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15710 NARGS elements of the arguments that are being used when calling
15711 it. TARGS is a vector into which the deduced template arguments
15712 are placed.
15714 Returns either a FUNCTION_DECL for the matching specialization of FN or
15715 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15716 true, diagnostics will be printed to explain why it failed.
15718 If FN is a conversion operator, or we are trying to produce a specific
15719 specialization, RETURN_TYPE is the return type desired.
15721 The EXPLICIT_TARGS are explicit template arguments provided via a
15722 template-id.
15724 The parameter STRICT is one of:
15726 DEDUCE_CALL:
15727 We are deducing arguments for a function call, as in
15728 [temp.deduct.call].
15730 DEDUCE_CONV:
15731 We are deducing arguments for a conversion function, as in
15732 [temp.deduct.conv].
15734 DEDUCE_EXACT:
15735 We are deducing arguments when doing an explicit instantiation
15736 as in [temp.explicit], when determining an explicit specialization
15737 as in [temp.expl.spec], or when taking the address of a function
15738 template, as in [temp.deduct.funcaddr]. */
15740 tree
15741 fn_type_unification (tree fn,
15742 tree explicit_targs,
15743 tree targs,
15744 const tree *args,
15745 unsigned int nargs,
15746 tree return_type,
15747 unification_kind_t strict,
15748 int flags,
15749 bool explain_p,
15750 bool decltype_p)
15752 tree parms;
15753 tree fntype;
15754 tree decl = NULL_TREE;
15755 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15756 bool ok;
15757 static int deduction_depth;
15758 struct pending_template *old_last_pend = last_pending_template;
15759 struct tinst_level *old_error_tinst = last_error_tinst_level;
15760 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15761 tree tinst;
15762 tree r = error_mark_node;
15764 if (decltype_p)
15765 complain |= tf_decltype;
15767 /* In C++0x, it's possible to have a function template whose type depends
15768 on itself recursively. This is most obvious with decltype, but can also
15769 occur with enumeration scope (c++/48969). So we need to catch infinite
15770 recursion and reject the substitution at deduction time; this function
15771 will return error_mark_node for any repeated substitution.
15773 This also catches excessive recursion such as when f<N> depends on
15774 f<N-1> across all integers, and returns error_mark_node for all the
15775 substitutions back up to the initial one.
15777 This is, of course, not reentrant. */
15778 if (excessive_deduction_depth)
15779 return error_mark_node;
15780 tinst = build_tree_list (fn, NULL_TREE);
15781 ++deduction_depth;
15783 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15785 fntype = TREE_TYPE (fn);
15786 if (explicit_targs)
15788 /* [temp.deduct]
15790 The specified template arguments must match the template
15791 parameters in kind (i.e., type, nontype, template), and there
15792 must not be more arguments than there are parameters;
15793 otherwise type deduction fails.
15795 Nontype arguments must match the types of the corresponding
15796 nontype template parameters, or must be convertible to the
15797 types of the corresponding nontype parameters as specified in
15798 _temp.arg.nontype_, otherwise type deduction fails.
15800 All references in the function type of the function template
15801 to the corresponding template parameters are replaced by the
15802 specified template argument values. If a substitution in a
15803 template parameter or in the function type of the function
15804 template results in an invalid type, type deduction fails. */
15805 int i, len = TREE_VEC_LENGTH (tparms);
15806 location_t loc = input_location;
15807 bool incomplete = false;
15809 /* Adjust any explicit template arguments before entering the
15810 substitution context. */
15811 explicit_targs
15812 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15813 complain,
15814 /*require_all_args=*/false,
15815 /*use_default_args=*/false));
15816 if (explicit_targs == error_mark_node)
15817 goto fail;
15819 /* Substitute the explicit args into the function type. This is
15820 necessary so that, for instance, explicitly declared function
15821 arguments can match null pointed constants. If we were given
15822 an incomplete set of explicit args, we must not do semantic
15823 processing during substitution as we could create partial
15824 instantiations. */
15825 for (i = 0; i < len; i++)
15827 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15828 bool parameter_pack = false;
15829 tree targ = TREE_VEC_ELT (explicit_targs, i);
15831 /* Dig out the actual parm. */
15832 if (TREE_CODE (parm) == TYPE_DECL
15833 || TREE_CODE (parm) == TEMPLATE_DECL)
15835 parm = TREE_TYPE (parm);
15836 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15838 else if (TREE_CODE (parm) == PARM_DECL)
15840 parm = DECL_INITIAL (parm);
15841 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15844 if (!parameter_pack && targ == NULL_TREE)
15845 /* No explicit argument for this template parameter. */
15846 incomplete = true;
15848 if (parameter_pack && pack_deducible_p (parm, fn))
15850 /* Mark the argument pack as "incomplete". We could
15851 still deduce more arguments during unification.
15852 We remove this mark in type_unification_real. */
15853 if (targ)
15855 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15856 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15857 = ARGUMENT_PACK_ARGS (targ);
15860 /* We have some incomplete argument packs. */
15861 incomplete = true;
15865 TREE_VALUE (tinst) = explicit_targs;
15866 if (!push_tinst_level (tinst))
15868 excessive_deduction_depth = true;
15869 goto fail;
15871 processing_template_decl += incomplete;
15872 input_location = DECL_SOURCE_LOCATION (fn);
15873 /* Ignore any access checks; we'll see them again in
15874 instantiate_template and they might have the wrong
15875 access path at this point. */
15876 push_deferring_access_checks (dk_deferred);
15877 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15878 complain | tf_partial, NULL_TREE);
15879 pop_deferring_access_checks ();
15880 input_location = loc;
15881 processing_template_decl -= incomplete;
15882 pop_tinst_level ();
15884 if (fntype == error_mark_node)
15885 goto fail;
15887 /* Place the explicitly specified arguments in TARGS. */
15888 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15889 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15892 /* Never do unification on the 'this' parameter. */
15893 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15895 if (return_type)
15897 tree *new_args;
15899 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15900 new_args = XALLOCAVEC (tree, nargs + 1);
15901 new_args[0] = return_type;
15902 memcpy (new_args + 1, args, nargs * sizeof (tree));
15903 args = new_args;
15904 ++nargs;
15907 /* We allow incomplete unification without an error message here
15908 because the standard doesn't seem to explicitly prohibit it. Our
15909 callers must be ready to deal with unification failures in any
15910 event. */
15912 TREE_VALUE (tinst) = targs;
15913 /* If we aren't explaining yet, push tinst context so we can see where
15914 any errors (e.g. from class instantiations triggered by instantiation
15915 of default template arguments) come from. If we are explaining, this
15916 context is redundant. */
15917 if (!explain_p && !push_tinst_level (tinst))
15919 excessive_deduction_depth = true;
15920 goto fail;
15923 /* type_unification_real will pass back any access checks from default
15924 template argument substitution. */
15925 vec<deferred_access_check, va_gc> *checks;
15926 checks = NULL;
15928 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15929 targs, parms, args, nargs, /*subr=*/0,
15930 strict, flags, &checks, explain_p);
15931 if (!explain_p)
15932 pop_tinst_level ();
15933 if (!ok)
15934 goto fail;
15936 /* Now that we have bindings for all of the template arguments,
15937 ensure that the arguments deduced for the template template
15938 parameters have compatible template parameter lists. We cannot
15939 check this property before we have deduced all template
15940 arguments, because the template parameter types of a template
15941 template parameter might depend on prior template parameters
15942 deduced after the template template parameter. The following
15943 ill-formed example illustrates this issue:
15945 template<typename T, template<T> class C> void f(C<5>, T);
15947 template<int N> struct X {};
15949 void g() {
15950 f(X<5>(), 5l); // error: template argument deduction fails
15953 The template parameter list of 'C' depends on the template type
15954 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15955 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15956 time that we deduce 'C'. */
15957 if (!template_template_parm_bindings_ok_p
15958 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15960 unify_inconsistent_template_template_parameters (explain_p);
15961 goto fail;
15964 /* All is well so far. Now, check:
15966 [temp.deduct]
15968 When all template arguments have been deduced, all uses of
15969 template parameters in nondeduced contexts are replaced with
15970 the corresponding deduced argument values. If the
15971 substitution results in an invalid type, as described above,
15972 type deduction fails. */
15973 TREE_VALUE (tinst) = targs;
15974 if (!push_tinst_level (tinst))
15976 excessive_deduction_depth = true;
15977 goto fail;
15980 /* Also collect access checks from the instantiation. */
15981 reopen_deferring_access_checks (checks);
15983 decl = instantiate_template (fn, targs, complain);
15985 checks = get_deferred_access_checks ();
15986 pop_deferring_access_checks ();
15988 pop_tinst_level ();
15990 if (decl == error_mark_node)
15991 goto fail;
15993 /* Now perform any access checks encountered during substitution. */
15994 push_access_scope (decl);
15995 ok = perform_access_checks (checks, complain);
15996 pop_access_scope (decl);
15997 if (!ok)
15998 goto fail;
16000 /* If we're looking for an exact match, check that what we got
16001 is indeed an exact match. It might not be if some template
16002 parameters are used in non-deduced contexts. But don't check
16003 for an exact match if we have dependent template arguments;
16004 in that case we're doing partial ordering, and we already know
16005 that we have two candidates that will provide the actual type. */
16006 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16008 tree substed = TREE_TYPE (decl);
16009 unsigned int i;
16011 tree sarg
16012 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16013 if (return_type)
16014 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16015 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16016 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16018 unify_type_mismatch (explain_p, args[i],
16019 TREE_VALUE (sarg));
16020 goto fail;
16024 r = decl;
16026 fail:
16027 --deduction_depth;
16028 if (excessive_deduction_depth)
16030 if (deduction_depth == 0)
16031 /* Reset once we're all the way out. */
16032 excessive_deduction_depth = false;
16035 /* We can't free this if a pending_template entry or last_error_tinst_level
16036 is pointing at it. */
16037 if (last_pending_template == old_last_pend
16038 && last_error_tinst_level == old_error_tinst)
16039 ggc_free (tinst);
16041 return r;
16044 /* Adjust types before performing type deduction, as described in
16045 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16046 sections are symmetric. PARM is the type of a function parameter
16047 or the return type of the conversion function. ARG is the type of
16048 the argument passed to the call, or the type of the value
16049 initialized with the result of the conversion function.
16050 ARG_EXPR is the original argument expression, which may be null. */
16052 static int
16053 maybe_adjust_types_for_deduction (unification_kind_t strict,
16054 tree* parm,
16055 tree* arg,
16056 tree arg_expr)
16058 int result = 0;
16060 switch (strict)
16062 case DEDUCE_CALL:
16063 break;
16065 case DEDUCE_CONV:
16067 /* Swap PARM and ARG throughout the remainder of this
16068 function; the handling is precisely symmetric since PARM
16069 will initialize ARG rather than vice versa. */
16070 tree* temp = parm;
16071 parm = arg;
16072 arg = temp;
16073 break;
16076 case DEDUCE_EXACT:
16077 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16078 too, but here handle it by stripping the reference from PARM
16079 rather than by adding it to ARG. */
16080 if (TREE_CODE (*parm) == REFERENCE_TYPE
16081 && TYPE_REF_IS_RVALUE (*parm)
16082 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16083 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16084 && TREE_CODE (*arg) == REFERENCE_TYPE
16085 && !TYPE_REF_IS_RVALUE (*arg))
16086 *parm = TREE_TYPE (*parm);
16087 /* Nothing else to do in this case. */
16088 return 0;
16090 default:
16091 gcc_unreachable ();
16094 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16096 /* [temp.deduct.call]
16098 If P is not a reference type:
16100 --If A is an array type, the pointer type produced by the
16101 array-to-pointer standard conversion (_conv.array_) is
16102 used in place of A for type deduction; otherwise,
16104 --If A is a function type, the pointer type produced by
16105 the function-to-pointer standard conversion
16106 (_conv.func_) is used in place of A for type deduction;
16107 otherwise,
16109 --If A is a cv-qualified type, the top level
16110 cv-qualifiers of A's type are ignored for type
16111 deduction. */
16112 if (TREE_CODE (*arg) == ARRAY_TYPE)
16113 *arg = build_pointer_type (TREE_TYPE (*arg));
16114 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16115 *arg = build_pointer_type (*arg);
16116 else
16117 *arg = TYPE_MAIN_VARIANT (*arg);
16120 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16121 of the form T&&, where T is a template parameter, and the argument
16122 is an lvalue, T is deduced as A& */
16123 if (TREE_CODE (*parm) == REFERENCE_TYPE
16124 && TYPE_REF_IS_RVALUE (*parm)
16125 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16126 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16127 && (arg_expr ? real_lvalue_p (arg_expr)
16128 /* try_one_overload doesn't provide an arg_expr, but
16129 functions are always lvalues. */
16130 : TREE_CODE (*arg) == FUNCTION_TYPE))
16131 *arg = build_reference_type (*arg);
16133 /* [temp.deduct.call]
16135 If P is a cv-qualified type, the top level cv-qualifiers
16136 of P's type are ignored for type deduction. If P is a
16137 reference type, the type referred to by P is used for
16138 type deduction. */
16139 *parm = TYPE_MAIN_VARIANT (*parm);
16140 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16142 *parm = TREE_TYPE (*parm);
16143 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16146 /* DR 322. For conversion deduction, remove a reference type on parm
16147 too (which has been swapped into ARG). */
16148 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16149 *arg = TREE_TYPE (*arg);
16151 return result;
16154 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16155 template which does contain any deducible template parameters; check if
16156 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16157 unify_one_argument. */
16159 static int
16160 check_non_deducible_conversion (tree parm, tree arg, int strict,
16161 int flags, bool explain_p)
16163 tree type;
16165 if (!TYPE_P (arg))
16166 type = TREE_TYPE (arg);
16167 else
16168 type = arg;
16170 if (same_type_p (parm, type))
16171 return unify_success (explain_p);
16173 if (strict == DEDUCE_CONV)
16175 if (can_convert_arg (type, parm, NULL_TREE, flags,
16176 explain_p ? tf_warning_or_error : tf_none))
16177 return unify_success (explain_p);
16179 else if (strict != DEDUCE_EXACT)
16181 if (can_convert_arg (parm, type,
16182 TYPE_P (arg) ? NULL_TREE : arg,
16183 flags, explain_p ? tf_warning_or_error : tf_none))
16184 return unify_success (explain_p);
16187 if (strict == DEDUCE_EXACT)
16188 return unify_type_mismatch (explain_p, parm, arg);
16189 else
16190 return unify_arg_conversion (explain_p, parm, type, arg);
16193 static bool uses_deducible_template_parms (tree type);
16195 /* Returns true iff the expression EXPR is one from which a template
16196 argument can be deduced. In other words, if it's an undecorated
16197 use of a template non-type parameter. */
16199 static bool
16200 deducible_expression (tree expr)
16202 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16205 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16206 deducible way; that is, if it has a max value of <PARM> - 1. */
16208 static bool
16209 deducible_array_bound (tree domain)
16211 if (domain == NULL_TREE)
16212 return false;
16214 tree max = TYPE_MAX_VALUE (domain);
16215 if (TREE_CODE (max) != MINUS_EXPR)
16216 return false;
16218 return deducible_expression (TREE_OPERAND (max, 0));
16221 /* Returns true iff the template arguments ARGS use a template parameter
16222 in a deducible way. */
16224 static bool
16225 deducible_template_args (tree args)
16227 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16229 bool deducible;
16230 tree elt = TREE_VEC_ELT (args, i);
16231 if (ARGUMENT_PACK_P (elt))
16232 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16233 else
16235 if (PACK_EXPANSION_P (elt))
16236 elt = PACK_EXPANSION_PATTERN (elt);
16237 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16238 deducible = true;
16239 else if (TYPE_P (elt))
16240 deducible = uses_deducible_template_parms (elt);
16241 else
16242 deducible = deducible_expression (elt);
16244 if (deducible)
16245 return true;
16247 return false;
16250 /* Returns true iff TYPE contains any deducible references to template
16251 parameters, as per 14.8.2.5. */
16253 static bool
16254 uses_deducible_template_parms (tree type)
16256 if (PACK_EXPANSION_P (type))
16257 type = PACK_EXPANSION_PATTERN (type);
16259 /* T
16260 cv-list T
16261 TT<T>
16262 TT<i>
16263 TT<> */
16264 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16265 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16266 return true;
16268 /* T*
16270 T&& */
16271 if (POINTER_TYPE_P (type))
16272 return uses_deducible_template_parms (TREE_TYPE (type));
16274 /* T[integer-constant ]
16275 type [i] */
16276 if (TREE_CODE (type) == ARRAY_TYPE)
16277 return (uses_deducible_template_parms (TREE_TYPE (type))
16278 || deducible_array_bound (TYPE_DOMAIN (type)));
16280 /* T type ::*
16281 type T::*
16282 T T::*
16283 T (type ::*)()
16284 type (T::*)()
16285 type (type ::*)(T)
16286 type (T::*)(T)
16287 T (type ::*)(T)
16288 T (T::*)()
16289 T (T::*)(T) */
16290 if (TYPE_PTRMEM_P (type))
16291 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16292 || (uses_deducible_template_parms
16293 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16295 /* template-name <T> (where template-name refers to a class template)
16296 template-name <i> (where template-name refers to a class template) */
16297 if (CLASS_TYPE_P (type)
16298 && CLASSTYPE_TEMPLATE_INFO (type)
16299 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16300 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16301 (CLASSTYPE_TI_ARGS (type)));
16303 /* type (T)
16305 T(T) */
16306 if (TREE_CODE (type) == FUNCTION_TYPE
16307 || TREE_CODE (type) == METHOD_TYPE)
16309 if (uses_deducible_template_parms (TREE_TYPE (type)))
16310 return true;
16311 tree parm = TYPE_ARG_TYPES (type);
16312 if (TREE_CODE (type) == METHOD_TYPE)
16313 parm = TREE_CHAIN (parm);
16314 for (; parm; parm = TREE_CHAIN (parm))
16315 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16316 return true;
16319 return false;
16322 /* Subroutine of type_unification_real and unify_pack_expansion to
16323 handle unification of a single P/A pair. Parameters are as
16324 for those functions. */
16326 static int
16327 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16328 int subr, unification_kind_t strict, int flags,
16329 bool explain_p)
16331 tree arg_expr = NULL_TREE;
16332 int arg_strict;
16334 if (arg == error_mark_node || parm == error_mark_node)
16335 return unify_invalid (explain_p);
16336 if (arg == unknown_type_node)
16337 /* We can't deduce anything from this, but we might get all the
16338 template args from other function args. */
16339 return unify_success (explain_p);
16341 /* Implicit conversions (Clause 4) will be performed on a function
16342 argument to convert it to the type of the corresponding function
16343 parameter if the parameter type contains no template-parameters that
16344 participate in template argument deduction. */
16345 if (TYPE_P (parm) && !uses_template_parms (parm))
16346 /* For function parameters that contain no template-parameters at all,
16347 we have historically checked for convertibility in order to shortcut
16348 consideration of this candidate. */
16349 return check_non_deducible_conversion (parm, arg, strict, flags,
16350 explain_p);
16351 else if (strict == DEDUCE_CALL
16352 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16353 /* For function parameters with only non-deducible template parameters,
16354 just return. */
16355 return unify_success (explain_p);
16357 switch (strict)
16359 case DEDUCE_CALL:
16360 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16361 | UNIFY_ALLOW_MORE_CV_QUAL
16362 | UNIFY_ALLOW_DERIVED);
16363 break;
16365 case DEDUCE_CONV:
16366 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16367 break;
16369 case DEDUCE_EXACT:
16370 arg_strict = UNIFY_ALLOW_NONE;
16371 break;
16373 default:
16374 gcc_unreachable ();
16377 /* We only do these transformations if this is the top-level
16378 parameter_type_list in a call or declaration matching; in other
16379 situations (nested function declarators, template argument lists) we
16380 won't be comparing a type to an expression, and we don't do any type
16381 adjustments. */
16382 if (!subr)
16384 if (!TYPE_P (arg))
16386 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16387 if (type_unknown_p (arg))
16389 /* [temp.deduct.type] A template-argument can be
16390 deduced from a pointer to function or pointer
16391 to member function argument if the set of
16392 overloaded functions does not contain function
16393 templates and at most one of a set of
16394 overloaded functions provides a unique
16395 match. */
16397 if (resolve_overloaded_unification
16398 (tparms, targs, parm, arg, strict,
16399 arg_strict, explain_p))
16400 return unify_success (explain_p);
16401 return unify_overload_resolution_failure (explain_p, arg);
16404 arg_expr = arg;
16405 arg = unlowered_expr_type (arg);
16406 if (arg == error_mark_node)
16407 return unify_invalid (explain_p);
16410 arg_strict |=
16411 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16413 else
16414 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16415 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
16416 return unify_template_argument_mismatch (explain_p, parm, arg);
16418 /* For deduction from an init-list we need the actual list. */
16419 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16420 arg = arg_expr;
16421 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16424 /* Most parms like fn_type_unification.
16426 If SUBR is 1, we're being called recursively (to unify the
16427 arguments of a function or method parameter of a function
16428 template).
16430 CHECKS is a pointer to a vector of access checks encountered while
16431 substituting default template arguments. */
16433 static int
16434 type_unification_real (tree tparms,
16435 tree targs,
16436 tree xparms,
16437 const tree *xargs,
16438 unsigned int xnargs,
16439 int subr,
16440 unification_kind_t strict,
16441 int flags,
16442 vec<deferred_access_check, va_gc> **checks,
16443 bool explain_p)
16445 tree parm, arg;
16446 int i;
16447 int ntparms = TREE_VEC_LENGTH (tparms);
16448 int saw_undeduced = 0;
16449 tree parms;
16450 const tree *args;
16451 unsigned int nargs;
16452 unsigned int ia;
16454 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16455 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16456 gcc_assert (ntparms > 0);
16458 /* Reset the number of non-defaulted template arguments contained
16459 in TARGS. */
16460 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16462 again:
16463 parms = xparms;
16464 args = xargs;
16465 nargs = xnargs;
16467 ia = 0;
16468 while (parms && parms != void_list_node
16469 && ia < nargs)
16471 parm = TREE_VALUE (parms);
16473 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16474 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16475 /* For a function parameter pack that occurs at the end of the
16476 parameter-declaration-list, the type A of each remaining
16477 argument of the call is compared with the type P of the
16478 declarator-id of the function parameter pack. */
16479 break;
16481 parms = TREE_CHAIN (parms);
16483 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16484 /* For a function parameter pack that does not occur at the
16485 end of the parameter-declaration-list, the type of the
16486 parameter pack is a non-deduced context. */
16487 continue;
16489 arg = args[ia];
16490 ++ia;
16492 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16493 flags, explain_p))
16494 return 1;
16497 if (parms
16498 && parms != void_list_node
16499 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16501 /* Unify the remaining arguments with the pack expansion type. */
16502 tree argvec;
16503 tree parmvec = make_tree_vec (1);
16505 /* Allocate a TREE_VEC and copy in all of the arguments */
16506 argvec = make_tree_vec (nargs - ia);
16507 for (i = 0; ia < nargs; ++ia, ++i)
16508 TREE_VEC_ELT (argvec, i) = args[ia];
16510 /* Copy the parameter into parmvec. */
16511 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16512 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16513 /*subr=*/subr, explain_p))
16514 return 1;
16516 /* Advance to the end of the list of parameters. */
16517 parms = TREE_CHAIN (parms);
16520 /* Fail if we've reached the end of the parm list, and more args
16521 are present, and the parm list isn't variadic. */
16522 if (ia < nargs && parms == void_list_node)
16523 return unify_too_many_arguments (explain_p, nargs, ia);
16524 /* Fail if parms are left and they don't have default values. */
16525 if (parms && parms != void_list_node
16526 && TREE_PURPOSE (parms) == NULL_TREE)
16528 unsigned int count = nargs;
16529 tree p = parms;
16530 while (p && p != void_list_node)
16532 count++;
16533 p = TREE_CHAIN (p);
16535 return unify_too_few_arguments (explain_p, ia, count);
16538 if (!subr)
16540 tsubst_flags_t complain = (explain_p
16541 ? tf_warning_or_error
16542 : tf_none);
16544 for (i = 0; i < ntparms; i++)
16546 tree targ = TREE_VEC_ELT (targs, i);
16547 tree tparm = TREE_VEC_ELT (tparms, i);
16549 /* Clear the "incomplete" flags on all argument packs now so that
16550 substituting them into later default arguments works. */
16551 if (targ && ARGUMENT_PACK_P (targ))
16553 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16554 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16557 if (targ || tparm == error_mark_node)
16558 continue;
16559 tparm = TREE_VALUE (tparm);
16561 /* If this is an undeduced nontype parameter that depends on
16562 a type parameter, try another pass; its type may have been
16563 deduced from a later argument than the one from which
16564 this parameter can be deduced. */
16565 if (TREE_CODE (tparm) == PARM_DECL
16566 && uses_template_parms (TREE_TYPE (tparm))
16567 && !saw_undeduced++)
16568 goto again;
16570 /* Core issue #226 (C++0x) [temp.deduct]:
16572 If a template argument has not been deduced, its
16573 default template argument, if any, is used.
16575 When we are in C++98 mode, TREE_PURPOSE will either
16576 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16577 to explicitly check cxx_dialect here. */
16578 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16580 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16581 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16582 reopen_deferring_access_checks (*checks);
16583 location_t save_loc = input_location;
16584 if (DECL_P (parm))
16585 input_location = DECL_SOURCE_LOCATION (parm);
16586 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16587 arg = convert_template_argument (parm, arg, targs, complain,
16588 i, NULL_TREE);
16589 input_location = save_loc;
16590 *checks = get_deferred_access_checks ();
16591 pop_deferring_access_checks ();
16592 if (arg == error_mark_node)
16593 return 1;
16594 else
16596 TREE_VEC_ELT (targs, i) = arg;
16597 /* The position of the first default template argument,
16598 is also the number of non-defaulted arguments in TARGS.
16599 Record that. */
16600 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16601 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16602 continue;
16606 /* If the type parameter is a parameter pack, then it will
16607 be deduced to an empty parameter pack. */
16608 if (template_parameter_pack_p (tparm))
16610 tree arg;
16612 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16614 arg = make_node (NONTYPE_ARGUMENT_PACK);
16615 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16616 TREE_CONSTANT (arg) = 1;
16618 else
16619 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16621 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16623 TREE_VEC_ELT (targs, i) = arg;
16624 continue;
16627 return unify_parameter_deduction_failure (explain_p, tparm);
16630 #ifdef ENABLE_CHECKING
16631 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16632 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16633 #endif
16635 return unify_success (explain_p);
16638 /* Subroutine of type_unification_real. Args are like the variables
16639 at the call site. ARG is an overloaded function (or template-id);
16640 we try deducing template args from each of the overloads, and if
16641 only one succeeds, we go with that. Modifies TARGS and returns
16642 true on success. */
16644 static bool
16645 resolve_overloaded_unification (tree tparms,
16646 tree targs,
16647 tree parm,
16648 tree arg,
16649 unification_kind_t strict,
16650 int sub_strict,
16651 bool explain_p)
16653 tree tempargs = copy_node (targs);
16654 int good = 0;
16655 tree goodfn = NULL_TREE;
16656 bool addr_p;
16658 if (TREE_CODE (arg) == ADDR_EXPR)
16660 arg = TREE_OPERAND (arg, 0);
16661 addr_p = true;
16663 else
16664 addr_p = false;
16666 if (TREE_CODE (arg) == COMPONENT_REF)
16667 /* Handle `&x' where `x' is some static or non-static member
16668 function name. */
16669 arg = TREE_OPERAND (arg, 1);
16671 if (TREE_CODE (arg) == OFFSET_REF)
16672 arg = TREE_OPERAND (arg, 1);
16674 /* Strip baselink information. */
16675 if (BASELINK_P (arg))
16676 arg = BASELINK_FUNCTIONS (arg);
16678 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16680 /* If we got some explicit template args, we need to plug them into
16681 the affected templates before we try to unify, in case the
16682 explicit args will completely resolve the templates in question. */
16684 int ok = 0;
16685 tree expl_subargs = TREE_OPERAND (arg, 1);
16686 arg = TREE_OPERAND (arg, 0);
16688 for (; arg; arg = OVL_NEXT (arg))
16690 tree fn = OVL_CURRENT (arg);
16691 tree subargs, elem;
16693 if (TREE_CODE (fn) != TEMPLATE_DECL)
16694 continue;
16696 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16697 expl_subargs, NULL_TREE, tf_none,
16698 /*require_all_args=*/true,
16699 /*use_default_args=*/true);
16700 if (subargs != error_mark_node
16701 && !any_dependent_template_arguments_p (subargs))
16703 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
16704 if (try_one_overload (tparms, targs, tempargs, parm,
16705 elem, strict, sub_strict, addr_p, explain_p)
16706 && (!goodfn || !same_type_p (goodfn, elem)))
16708 goodfn = elem;
16709 ++good;
16712 else if (subargs)
16713 ++ok;
16715 /* If no templates (or more than one) are fully resolved by the
16716 explicit arguments, this template-id is a non-deduced context; it
16717 could still be OK if we deduce all template arguments for the
16718 enclosing call through other arguments. */
16719 if (good != 1)
16720 good = ok;
16722 else if (TREE_CODE (arg) != OVERLOAD
16723 && TREE_CODE (arg) != FUNCTION_DECL)
16724 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16725 -- but the deduction does not succeed because the expression is
16726 not just the function on its own. */
16727 return false;
16728 else
16729 for (; arg; arg = OVL_NEXT (arg))
16730 if (try_one_overload (tparms, targs, tempargs, parm,
16731 TREE_TYPE (OVL_CURRENT (arg)),
16732 strict, sub_strict, addr_p, explain_p)
16733 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16735 goodfn = OVL_CURRENT (arg);
16736 ++good;
16739 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16740 to function or pointer to member function argument if the set of
16741 overloaded functions does not contain function templates and at most
16742 one of a set of overloaded functions provides a unique match.
16744 So if we found multiple possibilities, we return success but don't
16745 deduce anything. */
16747 if (good == 1)
16749 int i = TREE_VEC_LENGTH (targs);
16750 for (; i--; )
16751 if (TREE_VEC_ELT (tempargs, i))
16753 tree old = TREE_VEC_ELT (targs, i);
16754 tree new_ = TREE_VEC_ELT (tempargs, i);
16755 if (new_ && old && ARGUMENT_PACK_P (old)
16756 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
16757 /* Don't forget explicit template arguments in a pack. */
16758 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
16759 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
16760 TREE_VEC_ELT (targs, i) = new_;
16763 if (good)
16764 return true;
16766 return false;
16769 /* Core DR 115: In contexts where deduction is done and fails, or in
16770 contexts where deduction is not done, if a template argument list is
16771 specified and it, along with any default template arguments, identifies
16772 a single function template specialization, then the template-id is an
16773 lvalue for the function template specialization. */
16775 tree
16776 resolve_nondeduced_context (tree orig_expr)
16778 tree expr, offset, baselink;
16779 bool addr;
16781 if (!type_unknown_p (orig_expr))
16782 return orig_expr;
16784 expr = orig_expr;
16785 addr = false;
16786 offset = NULL_TREE;
16787 baselink = NULL_TREE;
16789 if (TREE_CODE (expr) == ADDR_EXPR)
16791 expr = TREE_OPERAND (expr, 0);
16792 addr = true;
16794 if (TREE_CODE (expr) == OFFSET_REF)
16796 offset = expr;
16797 expr = TREE_OPERAND (expr, 1);
16799 if (BASELINK_P (expr))
16801 baselink = expr;
16802 expr = BASELINK_FUNCTIONS (expr);
16805 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16807 int good = 0;
16808 tree goodfn = NULL_TREE;
16810 /* If we got some explicit template args, we need to plug them into
16811 the affected templates before we try to unify, in case the
16812 explicit args will completely resolve the templates in question. */
16814 tree expl_subargs = TREE_OPERAND (expr, 1);
16815 tree arg = TREE_OPERAND (expr, 0);
16816 tree badfn = NULL_TREE;
16817 tree badargs = NULL_TREE;
16819 for (; arg; arg = OVL_NEXT (arg))
16821 tree fn = OVL_CURRENT (arg);
16822 tree subargs, elem;
16824 if (TREE_CODE (fn) != TEMPLATE_DECL)
16825 continue;
16827 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16828 expl_subargs, NULL_TREE, tf_none,
16829 /*require_all_args=*/true,
16830 /*use_default_args=*/true);
16831 if (subargs != error_mark_node
16832 && !any_dependent_template_arguments_p (subargs))
16834 elem = instantiate_template (fn, subargs, tf_none);
16835 if (elem == error_mark_node)
16837 badfn = fn;
16838 badargs = subargs;
16840 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16842 goodfn = elem;
16843 ++good;
16847 if (good == 1)
16849 mark_used (goodfn);
16850 expr = goodfn;
16851 if (baselink)
16852 expr = build_baselink (BASELINK_BINFO (baselink),
16853 BASELINK_ACCESS_BINFO (baselink),
16854 expr, BASELINK_OPTYPE (baselink));
16855 if (offset)
16857 tree base
16858 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16859 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16861 if (addr)
16862 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16863 return expr;
16865 else if (good == 0 && badargs)
16866 /* There were no good options and at least one bad one, so let the
16867 user know what the problem is. */
16868 instantiate_template (badfn, badargs, tf_warning_or_error);
16870 return orig_expr;
16873 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16874 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16875 different overloads deduce different arguments for a given parm.
16876 ADDR_P is true if the expression for which deduction is being
16877 performed was of the form "& fn" rather than simply "fn".
16879 Returns 1 on success. */
16881 static int
16882 try_one_overload (tree tparms,
16883 tree orig_targs,
16884 tree targs,
16885 tree parm,
16886 tree arg,
16887 unification_kind_t strict,
16888 int sub_strict,
16889 bool addr_p,
16890 bool explain_p)
16892 int nargs;
16893 tree tempargs;
16894 int i;
16896 if (arg == error_mark_node)
16897 return 0;
16899 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16900 to function or pointer to member function argument if the set of
16901 overloaded functions does not contain function templates and at most
16902 one of a set of overloaded functions provides a unique match.
16904 So if this is a template, just return success. */
16906 if (uses_template_parms (arg))
16907 return 1;
16909 if (TREE_CODE (arg) == METHOD_TYPE)
16910 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16911 else if (addr_p)
16912 arg = build_pointer_type (arg);
16914 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16916 /* We don't copy orig_targs for this because if we have already deduced
16917 some template args from previous args, unify would complain when we
16918 try to deduce a template parameter for the same argument, even though
16919 there isn't really a conflict. */
16920 nargs = TREE_VEC_LENGTH (targs);
16921 tempargs = make_tree_vec (nargs);
16923 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16924 return 0;
16926 /* First make sure we didn't deduce anything that conflicts with
16927 explicitly specified args. */
16928 for (i = nargs; i--; )
16930 tree elt = TREE_VEC_ELT (tempargs, i);
16931 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16933 if (!elt)
16934 /*NOP*/;
16935 else if (uses_template_parms (elt))
16936 /* Since we're unifying against ourselves, we will fill in
16937 template args used in the function parm list with our own
16938 template parms. Discard them. */
16939 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16940 else if (oldelt && !template_args_equal (oldelt, elt))
16941 return 0;
16944 for (i = nargs; i--; )
16946 tree elt = TREE_VEC_ELT (tempargs, i);
16948 if (elt)
16949 TREE_VEC_ELT (targs, i) = elt;
16952 return 1;
16955 /* PARM is a template class (perhaps with unbound template
16956 parameters). ARG is a fully instantiated type. If ARG can be
16957 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16958 TARGS are as for unify. */
16960 static tree
16961 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16962 bool explain_p)
16964 tree copy_of_targs;
16966 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16967 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16968 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16969 return NULL_TREE;
16971 /* We need to make a new template argument vector for the call to
16972 unify. If we used TARGS, we'd clutter it up with the result of
16973 the attempted unification, even if this class didn't work out.
16974 We also don't want to commit ourselves to all the unifications
16975 we've already done, since unification is supposed to be done on
16976 an argument-by-argument basis. In other words, consider the
16977 following pathological case:
16979 template <int I, int J, int K>
16980 struct S {};
16982 template <int I, int J>
16983 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16985 template <int I, int J, int K>
16986 void f(S<I, J, K>, S<I, I, I>);
16988 void g() {
16989 S<0, 0, 0> s0;
16990 S<0, 1, 2> s2;
16992 f(s0, s2);
16995 Now, by the time we consider the unification involving `s2', we
16996 already know that we must have `f<0, 0, 0>'. But, even though
16997 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16998 because there are two ways to unify base classes of S<0, 1, 2>
16999 with S<I, I, I>. If we kept the already deduced knowledge, we
17000 would reject the possibility I=1. */
17001 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17003 /* If unification failed, we're done. */
17004 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17005 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17006 return NULL_TREE;
17008 return arg;
17011 /* Given a template type PARM and a class type ARG, find the unique
17012 base type in ARG that is an instance of PARM. We do not examine
17013 ARG itself; only its base-classes. If there is not exactly one
17014 appropriate base class, return NULL_TREE. PARM may be the type of
17015 a partial specialization, as well as a plain template type. Used
17016 by unify. */
17018 static enum template_base_result
17019 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17020 bool explain_p, tree *result)
17022 tree rval = NULL_TREE;
17023 tree binfo;
17025 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17027 binfo = TYPE_BINFO (complete_type (arg));
17028 if (!binfo)
17030 /* The type could not be completed. */
17031 *result = NULL_TREE;
17032 return tbr_incomplete_type;
17035 /* Walk in inheritance graph order. The search order is not
17036 important, and this avoids multiple walks of virtual bases. */
17037 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17039 tree r = try_class_unification (tparms, targs, parm,
17040 BINFO_TYPE (binfo), explain_p);
17042 if (r)
17044 /* If there is more than one satisfactory baseclass, then:
17046 [temp.deduct.call]
17048 If they yield more than one possible deduced A, the type
17049 deduction fails.
17051 applies. */
17052 if (rval && !same_type_p (r, rval))
17054 *result = NULL_TREE;
17055 return tbr_ambiguous_baseclass;
17058 rval = r;
17062 *result = rval;
17063 return tbr_success;
17066 /* Returns the level of DECL, which declares a template parameter. */
17068 static int
17069 template_decl_level (tree decl)
17071 switch (TREE_CODE (decl))
17073 case TYPE_DECL:
17074 case TEMPLATE_DECL:
17075 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17077 case PARM_DECL:
17078 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17080 default:
17081 gcc_unreachable ();
17083 return 0;
17086 /* Decide whether ARG can be unified with PARM, considering only the
17087 cv-qualifiers of each type, given STRICT as documented for unify.
17088 Returns nonzero iff the unification is OK on that basis. */
17090 static int
17091 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17093 int arg_quals = cp_type_quals (arg);
17094 int parm_quals = cp_type_quals (parm);
17096 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17097 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17099 /* Although a CVR qualifier is ignored when being applied to a
17100 substituted template parameter ([8.3.2]/1 for example), that
17101 does not allow us to unify "const T" with "int&" because both
17102 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17103 It is ok when we're allowing additional CV qualifiers
17104 at the outer level [14.8.2.1]/3,1st bullet. */
17105 if ((TREE_CODE (arg) == REFERENCE_TYPE
17106 || TREE_CODE (arg) == FUNCTION_TYPE
17107 || TREE_CODE (arg) == METHOD_TYPE)
17108 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17109 return 0;
17111 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17112 && (parm_quals & TYPE_QUAL_RESTRICT))
17113 return 0;
17116 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17117 && (arg_quals & parm_quals) != parm_quals)
17118 return 0;
17120 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17121 && (parm_quals & arg_quals) != arg_quals)
17122 return 0;
17124 return 1;
17127 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17128 void
17129 template_parm_level_and_index (tree parm, int* level, int* index)
17131 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17132 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17133 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17135 *index = TEMPLATE_TYPE_IDX (parm);
17136 *level = TEMPLATE_TYPE_LEVEL (parm);
17138 else
17140 *index = TEMPLATE_PARM_IDX (parm);
17141 *level = TEMPLATE_PARM_LEVEL (parm);
17145 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17146 do { \
17147 if (unify (TP, TA, P, A, S, EP)) \
17148 return 1; \
17149 } while (0);
17151 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17152 expansion at the end of PACKED_PARMS. Returns 0 if the type
17153 deduction succeeds, 1 otherwise. STRICT is the same as in
17154 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17155 call argument list. We'll need to adjust the arguments to make them
17156 types. SUBR tells us if this is from a recursive call to
17157 type_unification_real, or for comparing two template argument
17158 lists. */
17160 static int
17161 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17162 tree packed_args, unification_kind_t strict,
17163 bool subr, bool explain_p)
17165 tree parm
17166 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17167 tree pattern = PACK_EXPANSION_PATTERN (parm);
17168 tree pack, packs = NULL_TREE;
17169 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17171 packed_args = expand_template_argument_pack (packed_args);
17173 int len = TREE_VEC_LENGTH (packed_args);
17175 /* Determine the parameter packs we will be deducing from the
17176 pattern, and record their current deductions. */
17177 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17178 pack; pack = TREE_CHAIN (pack))
17180 tree parm_pack = TREE_VALUE (pack);
17181 int idx, level;
17183 /* Determine the index and level of this parameter pack. */
17184 template_parm_level_and_index (parm_pack, &level, &idx);
17186 /* Keep track of the parameter packs and their corresponding
17187 argument packs. */
17188 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17189 TREE_TYPE (packs) = make_tree_vec (len - start);
17192 /* Loop through all of the arguments that have not yet been
17193 unified and unify each with the pattern. */
17194 for (i = start; i < len; i++)
17196 tree parm;
17197 bool any_explicit = false;
17198 tree arg = TREE_VEC_ELT (packed_args, i);
17200 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17201 or the element of its argument pack at the current index if
17202 this argument was explicitly specified. */
17203 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17205 int idx, level;
17206 tree arg, pargs;
17207 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17209 arg = NULL_TREE;
17210 if (TREE_VALUE (pack)
17211 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17212 && (i - start < TREE_VEC_LENGTH (pargs)))
17214 any_explicit = true;
17215 arg = TREE_VEC_ELT (pargs, i - start);
17217 TMPL_ARG (targs, level, idx) = arg;
17220 /* If we had explicit template arguments, substitute them into the
17221 pattern before deduction. */
17222 if (any_explicit)
17224 /* Some arguments might still be unspecified or dependent. */
17225 bool dependent;
17226 ++processing_template_decl;
17227 dependent = any_dependent_template_arguments_p (targs);
17228 if (!dependent)
17229 --processing_template_decl;
17230 parm = tsubst (pattern, targs,
17231 explain_p ? tf_warning_or_error : tf_none,
17232 NULL_TREE);
17233 if (dependent)
17234 --processing_template_decl;
17235 if (parm == error_mark_node)
17236 return 1;
17238 else
17239 parm = pattern;
17241 /* Unify the pattern with the current argument. */
17242 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17243 LOOKUP_IMPLICIT, explain_p))
17244 return 1;
17246 /* For each parameter pack, collect the deduced value. */
17247 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17249 int idx, level;
17250 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17252 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17253 TMPL_ARG (targs, level, idx);
17257 /* Verify that the results of unification with the parameter packs
17258 produce results consistent with what we've seen before, and make
17259 the deduced argument packs available. */
17260 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17262 tree old_pack = TREE_VALUE (pack);
17263 tree new_args = TREE_TYPE (pack);
17264 int i, len = TREE_VEC_LENGTH (new_args);
17265 int idx, level;
17266 bool nondeduced_p = false;
17268 /* By default keep the original deduced argument pack.
17269 If necessary, more specific code is going to update the
17270 resulting deduced argument later down in this function. */
17271 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17272 TMPL_ARG (targs, level, idx) = old_pack;
17274 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17275 actually deduce anything. */
17276 for (i = 0; i < len && !nondeduced_p; ++i)
17277 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17278 nondeduced_p = true;
17279 if (nondeduced_p)
17280 continue;
17282 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17284 /* If we had fewer function args than explicit template args,
17285 just use the explicits. */
17286 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17287 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17288 if (len < explicit_len)
17289 new_args = explicit_args;
17292 if (!old_pack)
17294 tree result;
17295 /* Build the deduced *_ARGUMENT_PACK. */
17296 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17298 result = make_node (NONTYPE_ARGUMENT_PACK);
17299 TREE_TYPE (result) =
17300 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17301 TREE_CONSTANT (result) = 1;
17303 else
17304 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17306 SET_ARGUMENT_PACK_ARGS (result, new_args);
17308 /* Note the deduced argument packs for this parameter
17309 pack. */
17310 TMPL_ARG (targs, level, idx) = result;
17312 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17313 && (ARGUMENT_PACK_ARGS (old_pack)
17314 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17316 /* We only had the explicitly-provided arguments before, but
17317 now we have a complete set of arguments. */
17318 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17320 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17321 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17322 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17324 else
17326 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17327 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17329 if (!comp_template_args_with_info (old_args, new_args,
17330 &bad_old_arg, &bad_new_arg))
17331 /* Inconsistent unification of this parameter pack. */
17332 return unify_parameter_pack_inconsistent (explain_p,
17333 bad_old_arg,
17334 bad_new_arg);
17338 return unify_success (explain_p);
17341 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17342 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17343 parameters and return value are as for unify. */
17345 static int
17346 unify_array_domain (tree tparms, tree targs,
17347 tree parm_dom, tree arg_dom,
17348 bool explain_p)
17350 tree parm_max;
17351 tree arg_max;
17352 bool parm_cst;
17353 bool arg_cst;
17355 /* Our representation of array types uses "N - 1" as the
17356 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17357 not an integer constant. We cannot unify arbitrarily
17358 complex expressions, so we eliminate the MINUS_EXPRs
17359 here. */
17360 parm_max = TYPE_MAX_VALUE (parm_dom);
17361 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17362 if (!parm_cst)
17364 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17365 parm_max = TREE_OPERAND (parm_max, 0);
17367 arg_max = TYPE_MAX_VALUE (arg_dom);
17368 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17369 if (!arg_cst)
17371 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17372 trying to unify the type of a variable with the type
17373 of a template parameter. For example:
17375 template <unsigned int N>
17376 void f (char (&) [N]);
17377 int g();
17378 void h(int i) {
17379 char a[g(i)];
17380 f(a);
17383 Here, the type of the ARG will be "int [g(i)]", and
17384 may be a SAVE_EXPR, etc. */
17385 if (TREE_CODE (arg_max) != MINUS_EXPR)
17386 return unify_vla_arg (explain_p, arg_dom);
17387 arg_max = TREE_OPERAND (arg_max, 0);
17390 /* If only one of the bounds used a MINUS_EXPR, compensate
17391 by adding one to the other bound. */
17392 if (parm_cst && !arg_cst)
17393 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17394 integer_type_node,
17395 parm_max,
17396 integer_one_node);
17397 else if (arg_cst && !parm_cst)
17398 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17399 integer_type_node,
17400 arg_max,
17401 integer_one_node);
17403 return unify (tparms, targs, parm_max, arg_max,
17404 UNIFY_ALLOW_INTEGER, explain_p);
17407 /* Deduce the value of template parameters. TPARMS is the (innermost)
17408 set of template parameters to a template. TARGS is the bindings
17409 for those template parameters, as determined thus far; TARGS may
17410 include template arguments for outer levels of template parameters
17411 as well. PARM is a parameter to a template function, or a
17412 subcomponent of that parameter; ARG is the corresponding argument.
17413 This function attempts to match PARM with ARG in a manner
17414 consistent with the existing assignments in TARGS. If more values
17415 are deduced, then TARGS is updated.
17417 Returns 0 if the type deduction succeeds, 1 otherwise. The
17418 parameter STRICT is a bitwise or of the following flags:
17420 UNIFY_ALLOW_NONE:
17421 Require an exact match between PARM and ARG.
17422 UNIFY_ALLOW_MORE_CV_QUAL:
17423 Allow the deduced ARG to be more cv-qualified (by qualification
17424 conversion) than ARG.
17425 UNIFY_ALLOW_LESS_CV_QUAL:
17426 Allow the deduced ARG to be less cv-qualified than ARG.
17427 UNIFY_ALLOW_DERIVED:
17428 Allow the deduced ARG to be a template base class of ARG,
17429 or a pointer to a template base class of the type pointed to by
17430 ARG.
17431 UNIFY_ALLOW_INTEGER:
17432 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17433 case for more information.
17434 UNIFY_ALLOW_OUTER_LEVEL:
17435 This is the outermost level of a deduction. Used to determine validity
17436 of qualification conversions. A valid qualification conversion must
17437 have const qualified pointers leading up to the inner type which
17438 requires additional CV quals, except at the outer level, where const
17439 is not required [conv.qual]. It would be normal to set this flag in
17440 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17441 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17442 This is the outermost level of a deduction, and PARM can be more CV
17443 qualified at this point.
17444 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17445 This is the outermost level of a deduction, and PARM can be less CV
17446 qualified at this point. */
17448 static int
17449 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17450 bool explain_p)
17452 int idx;
17453 tree targ;
17454 tree tparm;
17455 int strict_in = strict;
17457 /* I don't think this will do the right thing with respect to types.
17458 But the only case I've seen it in so far has been array bounds, where
17459 signedness is the only information lost, and I think that will be
17460 okay. */
17461 while (TREE_CODE (parm) == NOP_EXPR)
17462 parm = TREE_OPERAND (parm, 0);
17464 if (arg == error_mark_node)
17465 return unify_invalid (explain_p);
17466 if (arg == unknown_type_node
17467 || arg == init_list_type_node)
17468 /* We can't deduce anything from this, but we might get all the
17469 template args from other function args. */
17470 return unify_success (explain_p);
17472 /* If PARM uses template parameters, then we can't bail out here,
17473 even if ARG == PARM, since we won't record unifications for the
17474 template parameters. We might need them if we're trying to
17475 figure out which of two things is more specialized. */
17476 if (arg == parm && !uses_template_parms (parm))
17477 return unify_success (explain_p);
17479 /* Handle init lists early, so the rest of the function can assume
17480 we're dealing with a type. */
17481 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17483 tree elt, elttype;
17484 unsigned i;
17485 tree orig_parm = parm;
17487 /* Replace T with std::initializer_list<T> for deduction. */
17488 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17489 && flag_deduce_init_list)
17490 parm = listify (parm);
17492 if (!is_std_init_list (parm)
17493 && TREE_CODE (parm) != ARRAY_TYPE)
17494 /* We can only deduce from an initializer list argument if the
17495 parameter is std::initializer_list or an array; otherwise this
17496 is a non-deduced context. */
17497 return unify_success (explain_p);
17499 if (TREE_CODE (parm) == ARRAY_TYPE)
17500 elttype = TREE_TYPE (parm);
17501 else
17502 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17504 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17506 int elt_strict = strict;
17508 if (elt == error_mark_node)
17509 return unify_invalid (explain_p);
17511 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17513 tree type = TREE_TYPE (elt);
17514 /* It should only be possible to get here for a call. */
17515 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17516 elt_strict |= maybe_adjust_types_for_deduction
17517 (DEDUCE_CALL, &elttype, &type, elt);
17518 elt = type;
17521 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17522 explain_p);
17525 if (TREE_CODE (parm) == ARRAY_TYPE
17526 && deducible_array_bound (TYPE_DOMAIN (parm)))
17528 /* Also deduce from the length of the initializer list. */
17529 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17530 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17531 if (idx == error_mark_node)
17532 return unify_invalid (explain_p);
17533 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17534 idx, explain_p);
17537 /* If the std::initializer_list<T> deduction worked, replace the
17538 deduced A with std::initializer_list<A>. */
17539 if (orig_parm != parm)
17541 idx = TEMPLATE_TYPE_IDX (orig_parm);
17542 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17543 targ = listify (targ);
17544 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17546 return unify_success (explain_p);
17549 /* Immediately reject some pairs that won't unify because of
17550 cv-qualification mismatches. */
17551 if (TREE_CODE (arg) == TREE_CODE (parm)
17552 && TYPE_P (arg)
17553 /* It is the elements of the array which hold the cv quals of an array
17554 type, and the elements might be template type parms. We'll check
17555 when we recurse. */
17556 && TREE_CODE (arg) != ARRAY_TYPE
17557 /* We check the cv-qualifiers when unifying with template type
17558 parameters below. We want to allow ARG `const T' to unify with
17559 PARM `T' for example, when computing which of two templates
17560 is more specialized, for example. */
17561 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17562 && !check_cv_quals_for_unify (strict_in, arg, parm))
17563 return unify_cv_qual_mismatch (explain_p, parm, arg);
17565 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17566 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17567 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17568 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17569 strict &= ~UNIFY_ALLOW_DERIVED;
17570 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17571 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17573 switch (TREE_CODE (parm))
17575 case TYPENAME_TYPE:
17576 case SCOPE_REF:
17577 case UNBOUND_CLASS_TEMPLATE:
17578 /* In a type which contains a nested-name-specifier, template
17579 argument values cannot be deduced for template parameters used
17580 within the nested-name-specifier. */
17581 return unify_success (explain_p);
17583 case TEMPLATE_TYPE_PARM:
17584 case TEMPLATE_TEMPLATE_PARM:
17585 case BOUND_TEMPLATE_TEMPLATE_PARM:
17586 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17587 if (error_operand_p (tparm))
17588 return unify_invalid (explain_p);
17590 if (TEMPLATE_TYPE_LEVEL (parm)
17591 != template_decl_level (tparm))
17592 /* The PARM is not one we're trying to unify. Just check
17593 to see if it matches ARG. */
17595 if (TREE_CODE (arg) == TREE_CODE (parm)
17596 && (is_auto (parm) ? is_auto (arg)
17597 : same_type_p (parm, arg)))
17598 return unify_success (explain_p);
17599 else
17600 return unify_type_mismatch (explain_p, parm, arg);
17602 idx = TEMPLATE_TYPE_IDX (parm);
17603 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17604 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17605 if (error_operand_p (tparm))
17606 return unify_invalid (explain_p);
17608 /* Check for mixed types and values. */
17609 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17610 && TREE_CODE (tparm) != TYPE_DECL)
17611 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17612 && TREE_CODE (tparm) != TEMPLATE_DECL))
17613 gcc_unreachable ();
17615 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17617 /* ARG must be constructed from a template class or a template
17618 template parameter. */
17619 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17620 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17621 return unify_template_deduction_failure (explain_p, parm, arg);
17623 tree parmvec = TYPE_TI_ARGS (parm);
17624 /* An alias template name is never deduced. */
17625 if (TYPE_ALIAS_P (arg))
17626 arg = strip_typedefs (arg);
17627 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17628 tree full_argvec = add_to_template_args (targs, argvec);
17629 tree parm_parms
17630 = DECL_INNERMOST_TEMPLATE_PARMS
17631 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17632 int i, len;
17633 int parm_variadic_p = 0;
17635 /* The resolution to DR150 makes clear that default
17636 arguments for an N-argument may not be used to bind T
17637 to a template template parameter with fewer than N
17638 parameters. It is not safe to permit the binding of
17639 default arguments as an extension, as that may change
17640 the meaning of a conforming program. Consider:
17642 struct Dense { static const unsigned int dim = 1; };
17644 template <template <typename> class View,
17645 typename Block>
17646 void operator+(float, View<Block> const&);
17648 template <typename Block,
17649 unsigned int Dim = Block::dim>
17650 struct Lvalue_proxy { operator float() const; };
17652 void
17653 test_1d (void) {
17654 Lvalue_proxy<Dense> p;
17655 float b;
17656 b + p;
17659 Here, if Lvalue_proxy is permitted to bind to View, then
17660 the global operator+ will be used; if they are not, the
17661 Lvalue_proxy will be converted to float. */
17662 if (coerce_template_parms (parm_parms,
17663 full_argvec,
17664 TYPE_TI_TEMPLATE (parm),
17665 (explain_p
17666 ? tf_warning_or_error
17667 : tf_none),
17668 /*require_all_args=*/true,
17669 /*use_default_args=*/false)
17670 == error_mark_node)
17671 return 1;
17673 /* Deduce arguments T, i from TT<T> or TT<i>.
17674 We check each element of PARMVEC and ARGVEC individually
17675 rather than the whole TREE_VEC since they can have
17676 different number of elements. */
17678 parmvec = expand_template_argument_pack (parmvec);
17679 argvec = expand_template_argument_pack (argvec);
17681 len = TREE_VEC_LENGTH (parmvec);
17683 /* Check if the parameters end in a pack, making them
17684 variadic. */
17685 if (len > 0
17686 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17687 parm_variadic_p = 1;
17689 for (i = 0; i < len - parm_variadic_p; ++i)
17690 /* If the template argument list of P contains a pack
17691 expansion that is not the last template argument, the
17692 entire template argument list is a non-deduced
17693 context. */
17694 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17695 return unify_success (explain_p);
17697 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17698 return unify_too_few_arguments (explain_p,
17699 TREE_VEC_LENGTH (argvec), len);
17701 for (i = 0; i < len - parm_variadic_p; ++i)
17703 RECUR_AND_CHECK_FAILURE (tparms, targs,
17704 TREE_VEC_ELT (parmvec, i),
17705 TREE_VEC_ELT (argvec, i),
17706 UNIFY_ALLOW_NONE, explain_p);
17709 if (parm_variadic_p
17710 && unify_pack_expansion (tparms, targs,
17711 parmvec, argvec,
17712 DEDUCE_EXACT,
17713 /*subr=*/true, explain_p))
17714 return 1;
17716 arg = TYPE_TI_TEMPLATE (arg);
17718 /* Fall through to deduce template name. */
17721 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17722 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17724 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17726 /* Simple cases: Value already set, does match or doesn't. */
17727 if (targ != NULL_TREE && template_args_equal (targ, arg))
17728 return unify_success (explain_p);
17729 else if (targ)
17730 return unify_inconsistency (explain_p, parm, targ, arg);
17732 else
17734 /* If PARM is `const T' and ARG is only `int', we don't have
17735 a match unless we are allowing additional qualification.
17736 If ARG is `const int' and PARM is just `T' that's OK;
17737 that binds `const int' to `T'. */
17738 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17739 arg, parm))
17740 return unify_cv_qual_mismatch (explain_p, parm, arg);
17742 /* Consider the case where ARG is `const volatile int' and
17743 PARM is `const T'. Then, T should be `volatile int'. */
17744 arg = cp_build_qualified_type_real
17745 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17746 if (arg == error_mark_node)
17747 return unify_invalid (explain_p);
17749 /* Simple cases: Value already set, does match or doesn't. */
17750 if (targ != NULL_TREE && same_type_p (targ, arg))
17751 return unify_success (explain_p);
17752 else if (targ)
17753 return unify_inconsistency (explain_p, parm, targ, arg);
17755 /* Make sure that ARG is not a variable-sized array. (Note
17756 that were talking about variable-sized arrays (like
17757 `int[n]'), rather than arrays of unknown size (like
17758 `int[]').) We'll get very confused by such a type since
17759 the bound of the array is not constant, and therefore
17760 not mangleable. Besides, such types are not allowed in
17761 ISO C++, so we can do as we please here. We do allow
17762 them for 'auto' deduction, since that isn't ABI-exposed. */
17763 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17764 return unify_vla_arg (explain_p, arg);
17766 /* Strip typedefs as in convert_template_argument. */
17767 arg = canonicalize_type_argument (arg, tf_none);
17770 /* If ARG is a parameter pack or an expansion, we cannot unify
17771 against it unless PARM is also a parameter pack. */
17772 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17773 && !template_parameter_pack_p (parm))
17774 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17776 /* If the argument deduction results is a METHOD_TYPE,
17777 then there is a problem.
17778 METHOD_TYPE doesn't map to any real C++ type the result of
17779 the deduction can not be of that type. */
17780 if (TREE_CODE (arg) == METHOD_TYPE)
17781 return unify_method_type_error (explain_p, arg);
17783 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17784 return unify_success (explain_p);
17786 case TEMPLATE_PARM_INDEX:
17787 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17788 if (error_operand_p (tparm))
17789 return unify_invalid (explain_p);
17791 if (TEMPLATE_PARM_LEVEL (parm)
17792 != template_decl_level (tparm))
17794 /* The PARM is not one we're trying to unify. Just check
17795 to see if it matches ARG. */
17796 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17797 && cp_tree_equal (parm, arg));
17798 if (result)
17799 unify_expression_unequal (explain_p, parm, arg);
17800 return result;
17803 idx = TEMPLATE_PARM_IDX (parm);
17804 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17806 if (targ)
17808 int x = !cp_tree_equal (targ, arg);
17809 if (x)
17810 unify_inconsistency (explain_p, parm, targ, arg);
17811 return x;
17814 /* [temp.deduct.type] If, in the declaration of a function template
17815 with a non-type template-parameter, the non-type
17816 template-parameter is used in an expression in the function
17817 parameter-list and, if the corresponding template-argument is
17818 deduced, the template-argument type shall match the type of the
17819 template-parameter exactly, except that a template-argument
17820 deduced from an array bound may be of any integral type.
17821 The non-type parameter might use already deduced type parameters. */
17822 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17823 if (!TREE_TYPE (arg))
17824 /* Template-parameter dependent expression. Just accept it for now.
17825 It will later be processed in convert_template_argument. */
17827 else if (same_type_p (TREE_TYPE (arg), tparm))
17828 /* OK */;
17829 else if ((strict & UNIFY_ALLOW_INTEGER)
17830 && CP_INTEGRAL_TYPE_P (tparm))
17831 /* Convert the ARG to the type of PARM; the deduced non-type
17832 template argument must exactly match the types of the
17833 corresponding parameter. */
17834 arg = fold (build_nop (tparm, arg));
17835 else if (uses_template_parms (tparm))
17836 /* We haven't deduced the type of this parameter yet. Try again
17837 later. */
17838 return unify_success (explain_p);
17839 else
17840 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17842 /* If ARG is a parameter pack or an expansion, we cannot unify
17843 against it unless PARM is also a parameter pack. */
17844 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17845 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17846 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17848 arg = strip_typedefs_expr (arg);
17849 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17850 return unify_success (explain_p);
17852 case PTRMEM_CST:
17854 /* A pointer-to-member constant can be unified only with
17855 another constant. */
17856 if (TREE_CODE (arg) != PTRMEM_CST)
17857 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17859 /* Just unify the class member. It would be useless (and possibly
17860 wrong, depending on the strict flags) to unify also
17861 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17862 arg refer to the same variable, even if through different
17863 classes. For instance:
17865 struct A { int x; };
17866 struct B : A { };
17868 Unification of &A::x and &B::x must succeed. */
17869 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17870 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17873 case POINTER_TYPE:
17875 if (!TYPE_PTR_P (arg))
17876 return unify_type_mismatch (explain_p, parm, arg);
17878 /* [temp.deduct.call]
17880 A can be another pointer or pointer to member type that can
17881 be converted to the deduced A via a qualification
17882 conversion (_conv.qual_).
17884 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17885 This will allow for additional cv-qualification of the
17886 pointed-to types if appropriate. */
17888 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17889 /* The derived-to-base conversion only persists through one
17890 level of pointers. */
17891 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17893 return unify (tparms, targs, TREE_TYPE (parm),
17894 TREE_TYPE (arg), strict, explain_p);
17897 case REFERENCE_TYPE:
17898 if (TREE_CODE (arg) != REFERENCE_TYPE)
17899 return unify_type_mismatch (explain_p, parm, arg);
17900 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17901 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17903 case ARRAY_TYPE:
17904 if (TREE_CODE (arg) != ARRAY_TYPE)
17905 return unify_type_mismatch (explain_p, parm, arg);
17906 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17907 != (TYPE_DOMAIN (arg) == NULL_TREE))
17908 return unify_type_mismatch (explain_p, parm, arg);
17909 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17910 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17911 if (TYPE_DOMAIN (parm) != NULL_TREE)
17912 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17913 TYPE_DOMAIN (arg), explain_p);
17914 return unify_success (explain_p);
17916 case REAL_TYPE:
17917 case COMPLEX_TYPE:
17918 case VECTOR_TYPE:
17919 case INTEGER_TYPE:
17920 case BOOLEAN_TYPE:
17921 case ENUMERAL_TYPE:
17922 case VOID_TYPE:
17923 case NULLPTR_TYPE:
17924 if (TREE_CODE (arg) != TREE_CODE (parm))
17925 return unify_type_mismatch (explain_p, parm, arg);
17927 /* We have already checked cv-qualification at the top of the
17928 function. */
17929 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17930 return unify_type_mismatch (explain_p, parm, arg);
17932 /* As far as unification is concerned, this wins. Later checks
17933 will invalidate it if necessary. */
17934 return unify_success (explain_p);
17936 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17937 /* Type INTEGER_CST can come from ordinary constant template args. */
17938 case INTEGER_CST:
17939 while (TREE_CODE (arg) == NOP_EXPR)
17940 arg = TREE_OPERAND (arg, 0);
17942 if (TREE_CODE (arg) != INTEGER_CST)
17943 return unify_template_argument_mismatch (explain_p, parm, arg);
17944 return (tree_int_cst_equal (parm, arg)
17945 ? unify_success (explain_p)
17946 : unify_template_argument_mismatch (explain_p, parm, arg));
17948 case TREE_VEC:
17950 int i, len, argslen;
17951 int parm_variadic_p = 0;
17953 if (TREE_CODE (arg) != TREE_VEC)
17954 return unify_template_argument_mismatch (explain_p, parm, arg);
17956 len = TREE_VEC_LENGTH (parm);
17957 argslen = TREE_VEC_LENGTH (arg);
17959 /* Check for pack expansions in the parameters. */
17960 for (i = 0; i < len; ++i)
17962 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17964 if (i == len - 1)
17965 /* We can unify against something with a trailing
17966 parameter pack. */
17967 parm_variadic_p = 1;
17968 else
17969 /* [temp.deduct.type]/9: If the template argument list of
17970 P contains a pack expansion that is not the last
17971 template argument, the entire template argument list
17972 is a non-deduced context. */
17973 return unify_success (explain_p);
17977 /* If we don't have enough arguments to satisfy the parameters
17978 (not counting the pack expression at the end), or we have
17979 too many arguments for a parameter list that doesn't end in
17980 a pack expression, we can't unify. */
17981 if (parm_variadic_p
17982 ? argslen < len - parm_variadic_p
17983 : argslen != len)
17984 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17986 /* Unify all of the parameters that precede the (optional)
17987 pack expression. */
17988 for (i = 0; i < len - parm_variadic_p; ++i)
17990 RECUR_AND_CHECK_FAILURE (tparms, targs,
17991 TREE_VEC_ELT (parm, i),
17992 TREE_VEC_ELT (arg, i),
17993 UNIFY_ALLOW_NONE, explain_p);
17995 if (parm_variadic_p)
17996 return unify_pack_expansion (tparms, targs, parm, arg,
17997 DEDUCE_EXACT,
17998 /*subr=*/true, explain_p);
17999 return unify_success (explain_p);
18002 case RECORD_TYPE:
18003 case UNION_TYPE:
18004 if (TREE_CODE (arg) != TREE_CODE (parm))
18005 return unify_type_mismatch (explain_p, parm, arg);
18007 if (TYPE_PTRMEMFUNC_P (parm))
18009 if (!TYPE_PTRMEMFUNC_P (arg))
18010 return unify_type_mismatch (explain_p, parm, arg);
18012 return unify (tparms, targs,
18013 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18014 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18015 strict, explain_p);
18018 if (CLASSTYPE_TEMPLATE_INFO (parm))
18020 tree t = NULL_TREE;
18022 if (strict_in & UNIFY_ALLOW_DERIVED)
18024 /* First, we try to unify the PARM and ARG directly. */
18025 t = try_class_unification (tparms, targs,
18026 parm, arg, explain_p);
18028 if (!t)
18030 /* Fallback to the special case allowed in
18031 [temp.deduct.call]:
18033 If P is a class, and P has the form
18034 template-id, then A can be a derived class of
18035 the deduced A. Likewise, if P is a pointer to
18036 a class of the form template-id, A can be a
18037 pointer to a derived class pointed to by the
18038 deduced A. */
18039 enum template_base_result r;
18040 r = get_template_base (tparms, targs, parm, arg,
18041 explain_p, &t);
18043 if (!t)
18044 return unify_no_common_base (explain_p, r, parm, arg);
18047 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18048 && (CLASSTYPE_TI_TEMPLATE (parm)
18049 == CLASSTYPE_TI_TEMPLATE (arg)))
18050 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18051 Then, we should unify `int' and `U'. */
18052 t = arg;
18053 else
18054 /* There's no chance of unification succeeding. */
18055 return unify_type_mismatch (explain_p, parm, arg);
18057 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18058 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18060 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18061 return unify_type_mismatch (explain_p, parm, arg);
18062 return unify_success (explain_p);
18064 case METHOD_TYPE:
18065 case FUNCTION_TYPE:
18067 unsigned int nargs;
18068 tree *args;
18069 tree a;
18070 unsigned int i;
18072 if (TREE_CODE (arg) != TREE_CODE (parm))
18073 return unify_type_mismatch (explain_p, parm, arg);
18075 /* CV qualifications for methods can never be deduced, they must
18076 match exactly. We need to check them explicitly here,
18077 because type_unification_real treats them as any other
18078 cv-qualified parameter. */
18079 if (TREE_CODE (parm) == METHOD_TYPE
18080 && (!check_cv_quals_for_unify
18081 (UNIFY_ALLOW_NONE,
18082 class_of_this_parm (arg),
18083 class_of_this_parm (parm))))
18084 return unify_cv_qual_mismatch (explain_p, parm, arg);
18086 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18087 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18089 nargs = list_length (TYPE_ARG_TYPES (arg));
18090 args = XALLOCAVEC (tree, nargs);
18091 for (a = TYPE_ARG_TYPES (arg), i = 0;
18092 a != NULL_TREE && a != void_list_node;
18093 a = TREE_CHAIN (a), ++i)
18094 args[i] = TREE_VALUE (a);
18095 nargs = i;
18097 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18098 args, nargs, 1, DEDUCE_EXACT,
18099 LOOKUP_NORMAL, NULL, explain_p);
18102 case OFFSET_TYPE:
18103 /* Unify a pointer to member with a pointer to member function, which
18104 deduces the type of the member as a function type. */
18105 if (TYPE_PTRMEMFUNC_P (arg))
18107 /* Check top-level cv qualifiers */
18108 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18109 return unify_cv_qual_mismatch (explain_p, parm, arg);
18111 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18112 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18113 UNIFY_ALLOW_NONE, explain_p);
18115 /* Determine the type of the function we are unifying against. */
18116 tree fntype = static_fn_type (arg);
18118 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18121 if (TREE_CODE (arg) != OFFSET_TYPE)
18122 return unify_type_mismatch (explain_p, parm, arg);
18123 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18124 TYPE_OFFSET_BASETYPE (arg),
18125 UNIFY_ALLOW_NONE, explain_p);
18126 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18127 strict, explain_p);
18129 case CONST_DECL:
18130 if (DECL_TEMPLATE_PARM_P (parm))
18131 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18132 if (arg != integral_constant_value (parm))
18133 return unify_template_argument_mismatch (explain_p, parm, arg);
18134 return unify_success (explain_p);
18136 case FIELD_DECL:
18137 case TEMPLATE_DECL:
18138 /* Matched cases are handled by the ARG == PARM test above. */
18139 return unify_template_argument_mismatch (explain_p, parm, arg);
18141 case VAR_DECL:
18142 /* A non-type template parameter that is a variable should be a
18143 an integral constant, in which case, it whould have been
18144 folded into its (constant) value. So we should not be getting
18145 a variable here. */
18146 gcc_unreachable ();
18148 case TYPE_ARGUMENT_PACK:
18149 case NONTYPE_ARGUMENT_PACK:
18150 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18151 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18153 case TYPEOF_TYPE:
18154 case DECLTYPE_TYPE:
18155 case UNDERLYING_TYPE:
18156 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18157 or UNDERLYING_TYPE nodes. */
18158 return unify_success (explain_p);
18160 case ERROR_MARK:
18161 /* Unification fails if we hit an error node. */
18162 return unify_invalid (explain_p);
18164 case INDIRECT_REF:
18165 if (REFERENCE_REF_P (parm))
18167 if (REFERENCE_REF_P (arg))
18168 arg = TREE_OPERAND (arg, 0);
18169 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18170 strict, explain_p);
18172 /* FALLTHRU */
18174 default:
18175 /* An unresolved overload is a nondeduced context. */
18176 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18177 return unify_success (explain_p);
18178 gcc_assert (EXPR_P (parm));
18180 /* We must be looking at an expression. This can happen with
18181 something like:
18183 template <int I>
18184 void foo(S<I>, S<I + 2>);
18186 This is a "nondeduced context":
18188 [deduct.type]
18190 The nondeduced contexts are:
18192 --A type that is a template-id in which one or more of
18193 the template-arguments is an expression that references
18194 a template-parameter.
18196 In these cases, we assume deduction succeeded, but don't
18197 actually infer any unifications. */
18199 if (!uses_template_parms (parm)
18200 && !template_args_equal (parm, arg))
18201 return unify_expression_unequal (explain_p, parm, arg);
18202 else
18203 return unify_success (explain_p);
18206 #undef RECUR_AND_CHECK_FAILURE
18208 /* Note that DECL can be defined in this translation unit, if
18209 required. */
18211 static void
18212 mark_definable (tree decl)
18214 tree clone;
18215 DECL_NOT_REALLY_EXTERN (decl) = 1;
18216 FOR_EACH_CLONE (clone, decl)
18217 DECL_NOT_REALLY_EXTERN (clone) = 1;
18220 /* Called if RESULT is explicitly instantiated, or is a member of an
18221 explicitly instantiated class. */
18223 void
18224 mark_decl_instantiated (tree result, int extern_p)
18226 SET_DECL_EXPLICIT_INSTANTIATION (result);
18228 /* If this entity has already been written out, it's too late to
18229 make any modifications. */
18230 if (TREE_ASM_WRITTEN (result))
18231 return;
18233 /* For anonymous namespace we don't need to do anything. */
18234 if (decl_anon_ns_mem_p (result))
18236 gcc_assert (!TREE_PUBLIC (result));
18237 return;
18240 if (TREE_CODE (result) != FUNCTION_DECL)
18241 /* The TREE_PUBLIC flag for function declarations will have been
18242 set correctly by tsubst. */
18243 TREE_PUBLIC (result) = 1;
18245 /* This might have been set by an earlier implicit instantiation. */
18246 DECL_COMDAT (result) = 0;
18248 if (extern_p)
18249 DECL_NOT_REALLY_EXTERN (result) = 0;
18250 else
18252 mark_definable (result);
18253 mark_needed (result);
18254 /* Always make artificials weak. */
18255 if (DECL_ARTIFICIAL (result) && flag_weak)
18256 comdat_linkage (result);
18257 /* For WIN32 we also want to put explicit instantiations in
18258 linkonce sections. */
18259 else if (TREE_PUBLIC (result))
18260 maybe_make_one_only (result);
18263 /* If EXTERN_P, then this function will not be emitted -- unless
18264 followed by an explicit instantiation, at which point its linkage
18265 will be adjusted. If !EXTERN_P, then this function will be
18266 emitted here. In neither circumstance do we want
18267 import_export_decl to adjust the linkage. */
18268 DECL_INTERFACE_KNOWN (result) = 1;
18271 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18272 important template arguments. If any are missing, we check whether
18273 they're important by using error_mark_node for substituting into any
18274 args that were used for partial ordering (the ones between ARGS and END)
18275 and seeing if it bubbles up. */
18277 static bool
18278 check_undeduced_parms (tree targs, tree args, tree end)
18280 bool found = false;
18281 int i;
18282 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18283 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18285 found = true;
18286 TREE_VEC_ELT (targs, i) = error_mark_node;
18288 if (found)
18290 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18291 if (substed == error_mark_node)
18292 return true;
18294 return false;
18297 /* Given two function templates PAT1 and PAT2, return:
18299 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18300 -1 if PAT2 is more specialized than PAT1.
18301 0 if neither is more specialized.
18303 LEN indicates the number of parameters we should consider
18304 (defaulted parameters should not be considered).
18306 The 1998 std underspecified function template partial ordering, and
18307 DR214 addresses the issue. We take pairs of arguments, one from
18308 each of the templates, and deduce them against each other. One of
18309 the templates will be more specialized if all the *other*
18310 template's arguments deduce against its arguments and at least one
18311 of its arguments *does* *not* deduce against the other template's
18312 corresponding argument. Deduction is done as for class templates.
18313 The arguments used in deduction have reference and top level cv
18314 qualifiers removed. Iff both arguments were originally reference
18315 types *and* deduction succeeds in both directions, an lvalue reference
18316 wins against an rvalue reference and otherwise the template
18317 with the more cv-qualified argument wins for that pairing (if
18318 neither is more cv-qualified, they both are equal). Unlike regular
18319 deduction, after all the arguments have been deduced in this way,
18320 we do *not* verify the deduced template argument values can be
18321 substituted into non-deduced contexts.
18323 The logic can be a bit confusing here, because we look at deduce1 and
18324 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18325 can find template arguments for pat1 to make arg1 look like arg2, that
18326 means that arg2 is at least as specialized as arg1. */
18329 more_specialized_fn (tree pat1, tree pat2, int len)
18331 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18332 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18333 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18334 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18335 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18336 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18337 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18338 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18339 tree origs1, origs2;
18340 bool lose1 = false;
18341 bool lose2 = false;
18343 /* Remove the this parameter from non-static member functions. If
18344 one is a non-static member function and the other is not a static
18345 member function, remove the first parameter from that function
18346 also. This situation occurs for operator functions where we
18347 locate both a member function (with this pointer) and non-member
18348 operator (with explicit first operand). */
18349 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18351 len--; /* LEN is the number of significant arguments for DECL1 */
18352 args1 = TREE_CHAIN (args1);
18353 if (!DECL_STATIC_FUNCTION_P (decl2))
18354 args2 = TREE_CHAIN (args2);
18356 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18358 args2 = TREE_CHAIN (args2);
18359 if (!DECL_STATIC_FUNCTION_P (decl1))
18361 len--;
18362 args1 = TREE_CHAIN (args1);
18366 /* If only one is a conversion operator, they are unordered. */
18367 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18368 return 0;
18370 /* Consider the return type for a conversion function */
18371 if (DECL_CONV_FN_P (decl1))
18373 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18374 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18375 len++;
18378 processing_template_decl++;
18380 origs1 = args1;
18381 origs2 = args2;
18383 while (len--
18384 /* Stop when an ellipsis is seen. */
18385 && args1 != NULL_TREE && args2 != NULL_TREE)
18387 tree arg1 = TREE_VALUE (args1);
18388 tree arg2 = TREE_VALUE (args2);
18389 int deduce1, deduce2;
18390 int quals1 = -1;
18391 int quals2 = -1;
18392 int ref1 = 0;
18393 int ref2 = 0;
18395 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18396 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18398 /* When both arguments are pack expansions, we need only
18399 unify the patterns themselves. */
18400 arg1 = PACK_EXPANSION_PATTERN (arg1);
18401 arg2 = PACK_EXPANSION_PATTERN (arg2);
18403 /* This is the last comparison we need to do. */
18404 len = 0;
18407 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18409 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18410 arg1 = TREE_TYPE (arg1);
18411 quals1 = cp_type_quals (arg1);
18414 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18416 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18417 arg2 = TREE_TYPE (arg2);
18418 quals2 = cp_type_quals (arg2);
18421 arg1 = TYPE_MAIN_VARIANT (arg1);
18422 arg2 = TYPE_MAIN_VARIANT (arg2);
18424 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18426 int i, len2 = list_length (args2);
18427 tree parmvec = make_tree_vec (1);
18428 tree argvec = make_tree_vec (len2);
18429 tree ta = args2;
18431 /* Setup the parameter vector, which contains only ARG1. */
18432 TREE_VEC_ELT (parmvec, 0) = arg1;
18434 /* Setup the argument vector, which contains the remaining
18435 arguments. */
18436 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18437 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18439 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18440 argvec, DEDUCE_EXACT,
18441 /*subr=*/true, /*explain_p=*/false)
18442 == 0);
18444 /* We cannot deduce in the other direction, because ARG1 is
18445 a pack expansion but ARG2 is not. */
18446 deduce2 = 0;
18448 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18450 int i, len1 = list_length (args1);
18451 tree parmvec = make_tree_vec (1);
18452 tree argvec = make_tree_vec (len1);
18453 tree ta = args1;
18455 /* Setup the parameter vector, which contains only ARG1. */
18456 TREE_VEC_ELT (parmvec, 0) = arg2;
18458 /* Setup the argument vector, which contains the remaining
18459 arguments. */
18460 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18461 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18463 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18464 argvec, DEDUCE_EXACT,
18465 /*subr=*/true, /*explain_p=*/false)
18466 == 0);
18468 /* We cannot deduce in the other direction, because ARG2 is
18469 a pack expansion but ARG1 is not.*/
18470 deduce1 = 0;
18473 else
18475 /* The normal case, where neither argument is a pack
18476 expansion. */
18477 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18478 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18479 == 0);
18480 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18481 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18482 == 0);
18485 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18486 arg2, then arg2 is not as specialized as arg1. */
18487 if (!deduce1)
18488 lose2 = true;
18489 if (!deduce2)
18490 lose1 = true;
18492 /* "If, for a given type, deduction succeeds in both directions
18493 (i.e., the types are identical after the transformations above)
18494 and both P and A were reference types (before being replaced with
18495 the type referred to above):
18496 - if the type from the argument template was an lvalue reference and
18497 the type from the parameter template was not, the argument type is
18498 considered to be more specialized than the other; otherwise,
18499 - if the type from the argument template is more cv-qualified
18500 than the type from the parameter template (as described above),
18501 the argument type is considered to be more specialized than the other;
18502 otherwise,
18503 - neither type is more specialized than the other." */
18505 if (deduce1 && deduce2)
18507 if (ref1 && ref2 && ref1 != ref2)
18509 if (ref1 > ref2)
18510 lose1 = true;
18511 else
18512 lose2 = true;
18514 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18516 if ((quals1 & quals2) == quals2)
18517 lose2 = true;
18518 if ((quals1 & quals2) == quals1)
18519 lose1 = true;
18523 if (lose1 && lose2)
18524 /* We've failed to deduce something in either direction.
18525 These must be unordered. */
18526 break;
18528 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18529 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18530 /* We have already processed all of the arguments in our
18531 handing of the pack expansion type. */
18532 len = 0;
18534 args1 = TREE_CHAIN (args1);
18535 args2 = TREE_CHAIN (args2);
18538 /* "In most cases, all template parameters must have values in order for
18539 deduction to succeed, but for partial ordering purposes a template
18540 parameter may remain without a value provided it is not used in the
18541 types being used for partial ordering."
18543 Thus, if we are missing any of the targs1 we need to substitute into
18544 origs1, then pat2 is not as specialized as pat1. This can happen when
18545 there is a nondeduced context. */
18546 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18547 lose2 = true;
18548 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18549 lose1 = true;
18551 processing_template_decl--;
18553 /* All things being equal, if the next argument is a pack expansion
18554 for one function but not for the other, prefer the
18555 non-variadic function. FIXME this is bogus; see c++/41958. */
18556 if (lose1 == lose2
18557 && args1 && TREE_VALUE (args1)
18558 && args2 && TREE_VALUE (args2))
18560 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18561 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18564 if (lose1 == lose2)
18565 return 0;
18566 else if (!lose1)
18567 return 1;
18568 else
18569 return -1;
18572 /* Determine which of two partial specializations of TMPL is more
18573 specialized.
18575 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18576 to the first partial specialization. The TREE_VALUE is the
18577 innermost set of template parameters for the partial
18578 specialization. PAT2 is similar, but for the second template.
18580 Return 1 if the first partial specialization is more specialized;
18581 -1 if the second is more specialized; 0 if neither is more
18582 specialized.
18584 See [temp.class.order] for information about determining which of
18585 two templates is more specialized. */
18587 static int
18588 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18590 tree targs;
18591 tree tmpl1, tmpl2;
18592 int winner = 0;
18593 bool any_deductions = false;
18595 tmpl1 = TREE_TYPE (pat1);
18596 tmpl2 = TREE_TYPE (pat2);
18598 /* Just like what happens for functions, if we are ordering between
18599 different class template specializations, we may encounter dependent
18600 types in the arguments, and we need our dependency check functions
18601 to behave correctly. */
18602 ++processing_template_decl;
18603 targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
18604 CLASSTYPE_TI_ARGS (tmpl1),
18605 CLASSTYPE_TI_ARGS (tmpl2));
18606 if (targs)
18608 --winner;
18609 any_deductions = true;
18612 targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
18613 CLASSTYPE_TI_ARGS (tmpl2),
18614 CLASSTYPE_TI_ARGS (tmpl1));
18615 if (targs)
18617 ++winner;
18618 any_deductions = true;
18620 --processing_template_decl;
18622 /* In the case of a tie where at least one of the class templates
18623 has a parameter pack at the end, the template with the most
18624 non-packed parameters wins. */
18625 if (winner == 0
18626 && any_deductions
18627 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18628 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18630 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18631 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18632 int len1 = TREE_VEC_LENGTH (args1);
18633 int len2 = TREE_VEC_LENGTH (args2);
18635 /* We don't count the pack expansion at the end. */
18636 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18637 --len1;
18638 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18639 --len2;
18641 if (len1 > len2)
18642 return 1;
18643 else if (len1 < len2)
18644 return -1;
18647 return winner;
18650 /* Return the template arguments that will produce the function signature
18651 DECL from the function template FN, with the explicit template
18652 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18653 also match. Return NULL_TREE if no satisfactory arguments could be
18654 found. */
18656 static tree
18657 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18659 int ntparms = DECL_NTPARMS (fn);
18660 tree targs = make_tree_vec (ntparms);
18661 tree decl_type = TREE_TYPE (decl);
18662 tree decl_arg_types;
18663 tree *args;
18664 unsigned int nargs, ix;
18665 tree arg;
18667 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18669 /* Never do unification on the 'this' parameter. */
18670 decl_arg_types = skip_artificial_parms_for (decl,
18671 TYPE_ARG_TYPES (decl_type));
18673 nargs = list_length (decl_arg_types);
18674 args = XALLOCAVEC (tree, nargs);
18675 for (arg = decl_arg_types, ix = 0;
18676 arg != NULL_TREE && arg != void_list_node;
18677 arg = TREE_CHAIN (arg), ++ix)
18678 args[ix] = TREE_VALUE (arg);
18680 if (fn_type_unification (fn, explicit_args, targs,
18681 args, ix,
18682 (check_rettype || DECL_CONV_FN_P (fn)
18683 ? TREE_TYPE (decl_type) : NULL_TREE),
18684 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18685 /*decltype*/false)
18686 == error_mark_node)
18687 return NULL_TREE;
18689 return targs;
18692 /* Return the innermost template arguments that, when applied to a partial
18693 specialization of TMPL whose innermost template parameters are
18694 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18695 ARGS.
18697 For example, suppose we have:
18699 template <class T, class U> struct S {};
18700 template <class T> struct S<T*, int> {};
18702 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18703 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18704 int}. The resulting vector will be {double}, indicating that `T'
18705 is bound to `double'. */
18707 static tree
18708 get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
18710 int i, ntparms = TREE_VEC_LENGTH (tparms);
18711 tree deduced_args;
18712 tree innermost_deduced_args;
18714 innermost_deduced_args = make_tree_vec (ntparms);
18715 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18717 deduced_args = copy_node (args);
18718 SET_TMPL_ARGS_LEVEL (deduced_args,
18719 TMPL_ARGS_DEPTH (deduced_args),
18720 innermost_deduced_args);
18722 else
18723 deduced_args = innermost_deduced_args;
18725 if (unify (tparms, deduced_args,
18726 INNERMOST_TEMPLATE_ARGS (spec_args),
18727 INNERMOST_TEMPLATE_ARGS (args),
18728 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18729 return NULL_TREE;
18731 for (i = 0; i < ntparms; ++i)
18732 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18733 return NULL_TREE;
18735 /* Verify that nondeduced template arguments agree with the type
18736 obtained from argument deduction.
18738 For example:
18740 struct A { typedef int X; };
18741 template <class T, class U> struct C {};
18742 template <class T> struct C<T, typename T::X> {};
18744 Then with the instantiation `C<A, int>', we can deduce that
18745 `T' is `A' but unify () does not check whether `typename T::X'
18746 is `int'. */
18747 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18748 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18749 spec_args, tmpl,
18750 tf_none, false, false);
18751 if (spec_args == error_mark_node
18752 /* We only need to check the innermost arguments; the other
18753 arguments will always agree. */
18754 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18755 INNERMOST_TEMPLATE_ARGS (args)))
18756 return NULL_TREE;
18758 /* Now that we have bindings for all of the template arguments,
18759 ensure that the arguments deduced for the template template
18760 parameters have compatible template parameter lists. See the use
18761 of template_template_parm_bindings_ok_p in fn_type_unification
18762 for more information. */
18763 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18764 return NULL_TREE;
18766 return deduced_args;
18769 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18770 Return the TREE_LIST node with the most specialized template, if
18771 any. If there is no most specialized template, the error_mark_node
18772 is returned.
18774 Note that this function does not look at, or modify, the
18775 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18776 returned is one of the elements of INSTANTIATIONS, callers may
18777 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18778 and retrieve it from the value returned. */
18780 tree
18781 most_specialized_instantiation (tree templates)
18783 tree fn, champ;
18785 ++processing_template_decl;
18787 champ = templates;
18788 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18790 int fate = 0;
18792 if (get_bindings (TREE_VALUE (champ),
18793 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18794 NULL_TREE, /*check_ret=*/true))
18795 fate--;
18797 if (get_bindings (TREE_VALUE (fn),
18798 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18799 NULL_TREE, /*check_ret=*/true))
18800 fate++;
18802 if (fate == -1)
18803 champ = fn;
18804 else if (!fate)
18806 /* Equally specialized, move to next function. If there
18807 is no next function, nothing's most specialized. */
18808 fn = TREE_CHAIN (fn);
18809 champ = fn;
18810 if (!fn)
18811 break;
18815 if (champ)
18816 /* Now verify that champ is better than everything earlier in the
18817 instantiation list. */
18818 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18819 if (get_bindings (TREE_VALUE (champ),
18820 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18821 NULL_TREE, /*check_ret=*/true)
18822 || !get_bindings (TREE_VALUE (fn),
18823 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18824 NULL_TREE, /*check_ret=*/true))
18826 champ = NULL_TREE;
18827 break;
18830 processing_template_decl--;
18832 if (!champ)
18833 return error_mark_node;
18835 return champ;
18838 /* If DECL is a specialization of some template, return the most
18839 general such template. Otherwise, returns NULL_TREE.
18841 For example, given:
18843 template <class T> struct S { template <class U> void f(U); };
18845 if TMPL is `template <class U> void S<int>::f(U)' this will return
18846 the full template. This function will not trace past partial
18847 specializations, however. For example, given in addition:
18849 template <class T> struct S<T*> { template <class U> void f(U); };
18851 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18852 `template <class T> template <class U> S<T*>::f(U)'. */
18854 tree
18855 most_general_template (tree decl)
18857 if (TREE_CODE (decl) != TEMPLATE_DECL)
18859 if (tree tinfo = get_template_info (decl))
18860 decl = TI_TEMPLATE (tinfo);
18861 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
18862 template friend, or a FIELD_DECL for a capture pack. */
18863 if (TREE_CODE (decl) != TEMPLATE_DECL)
18864 return NULL_TREE;
18867 /* Look for more and more general templates. */
18868 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
18870 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18871 (See cp-tree.h for details.) */
18872 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18873 break;
18875 if (CLASS_TYPE_P (TREE_TYPE (decl))
18876 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18877 break;
18879 /* Stop if we run into an explicitly specialized class template. */
18880 if (!DECL_NAMESPACE_SCOPE_P (decl)
18881 && DECL_CONTEXT (decl)
18882 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18883 break;
18885 decl = DECL_TI_TEMPLATE (decl);
18888 return decl;
18891 /* Return the most specialized of the class template partial
18892 specializations which can produce TYPE, a specialization of some class
18893 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18894 a _TYPE node corresponding to the partial specialization, while the
18895 TREE_PURPOSE is the set of template arguments that must be
18896 substituted into the TREE_TYPE in order to generate TYPE.
18898 If the choice of partial specialization is ambiguous, a diagnostic
18899 is issued, and the error_mark_node is returned. If there are no
18900 partial specializations matching TYPE, then NULL_TREE is
18901 returned, indicating that the primary template should be used. */
18903 static tree
18904 most_specialized_class (tree type, tsubst_flags_t complain)
18906 tree list = NULL_TREE;
18907 tree t;
18908 tree champ;
18909 int fate;
18910 bool ambiguous_p;
18911 tree outer_args = NULL_TREE;
18913 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
18914 tree main_tmpl = most_general_template (tmpl);
18915 tree args = CLASSTYPE_TI_ARGS (type);
18917 /* For determining which partial specialization to use, only the
18918 innermost args are interesting. */
18919 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18921 outer_args = strip_innermost_template_args (args, 1);
18922 args = INNERMOST_TEMPLATE_ARGS (args);
18925 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
18927 tree partial_spec_args;
18928 tree spec_args;
18929 tree spec_tmpl = TREE_VALUE (t);
18930 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18932 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18934 ++processing_template_decl;
18936 if (outer_args)
18938 /* Discard the outer levels of args, and then substitute in the
18939 template args from the enclosing class. */
18940 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18941 partial_spec_args = tsubst_template_args
18942 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18944 /* And the same for the partial specialization TEMPLATE_DECL. */
18945 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18948 partial_spec_args =
18949 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18950 partial_spec_args,
18951 tmpl, tf_none,
18952 /*require_all_args=*/true,
18953 /*use_default_args=*/true);
18955 --processing_template_decl;
18957 if (partial_spec_args == error_mark_node)
18958 return error_mark_node;
18959 if (spec_tmpl == error_mark_node)
18960 return error_mark_node;
18962 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18963 spec_args = get_class_bindings (tmpl, parms,
18964 partial_spec_args,
18965 args);
18966 if (spec_args)
18968 if (outer_args)
18969 spec_args = add_to_template_args (outer_args, spec_args);
18970 list = tree_cons (spec_args, orig_parms, list);
18971 TREE_TYPE (list) = TREE_TYPE (t);
18975 if (! list)
18976 return NULL_TREE;
18978 ambiguous_p = false;
18979 t = list;
18980 champ = t;
18981 t = TREE_CHAIN (t);
18982 for (; t; t = TREE_CHAIN (t))
18984 fate = more_specialized_class (tmpl, champ, t);
18985 if (fate == 1)
18987 else
18989 if (fate == 0)
18991 t = TREE_CHAIN (t);
18992 if (! t)
18994 ambiguous_p = true;
18995 break;
18998 champ = t;
19002 if (!ambiguous_p)
19003 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19005 fate = more_specialized_class (tmpl, champ, t);
19006 if (fate != 1)
19008 ambiguous_p = true;
19009 break;
19013 if (ambiguous_p)
19015 const char *str;
19016 char *spaces = NULL;
19017 if (!(complain & tf_error))
19018 return error_mark_node;
19019 error ("ambiguous class template instantiation for %q#T", type);
19020 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19021 for (t = list; t; t = TREE_CHAIN (t))
19023 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
19024 spaces = spaces ? spaces : get_spaces (str);
19026 free (spaces);
19027 return error_mark_node;
19030 return champ;
19033 /* Explicitly instantiate DECL. */
19035 void
19036 do_decl_instantiation (tree decl, tree storage)
19038 tree result = NULL_TREE;
19039 int extern_p = 0;
19041 if (!decl || decl == error_mark_node)
19042 /* An error occurred, for which grokdeclarator has already issued
19043 an appropriate message. */
19044 return;
19045 else if (! DECL_LANG_SPECIFIC (decl))
19047 error ("explicit instantiation of non-template %q#D", decl);
19048 return;
19050 else if (VAR_P (decl))
19052 /* There is an asymmetry here in the way VAR_DECLs and
19053 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19054 the latter, the DECL we get back will be marked as a
19055 template instantiation, and the appropriate
19056 DECL_TEMPLATE_INFO will be set up. This does not happen for
19057 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19058 should handle VAR_DECLs as it currently handles
19059 FUNCTION_DECLs. */
19060 if (!DECL_CLASS_SCOPE_P (decl))
19062 error ("%qD is not a static data member of a class template", decl);
19063 return;
19065 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19066 if (!result || !VAR_P (result))
19068 error ("no matching template for %qD found", decl);
19069 return;
19071 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19073 error ("type %qT for explicit instantiation %qD does not match "
19074 "declared type %qT", TREE_TYPE (result), decl,
19075 TREE_TYPE (decl));
19076 return;
19079 else if (TREE_CODE (decl) != FUNCTION_DECL)
19081 error ("explicit instantiation of %q#D", decl);
19082 return;
19084 else
19085 result = decl;
19087 /* Check for various error cases. Note that if the explicit
19088 instantiation is valid the RESULT will currently be marked as an
19089 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19090 until we get here. */
19092 if (DECL_TEMPLATE_SPECIALIZATION (result))
19094 /* DR 259 [temp.spec].
19096 Both an explicit instantiation and a declaration of an explicit
19097 specialization shall not appear in a program unless the explicit
19098 instantiation follows a declaration of the explicit specialization.
19100 For a given set of template parameters, if an explicit
19101 instantiation of a template appears after a declaration of an
19102 explicit specialization for that template, the explicit
19103 instantiation has no effect. */
19104 return;
19106 else if (DECL_EXPLICIT_INSTANTIATION (result))
19108 /* [temp.spec]
19110 No program shall explicitly instantiate any template more
19111 than once.
19113 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19114 the first instantiation was `extern' and the second is not,
19115 and EXTERN_P for the opposite case. */
19116 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19117 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19118 /* If an "extern" explicit instantiation follows an ordinary
19119 explicit instantiation, the template is instantiated. */
19120 if (extern_p)
19121 return;
19123 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19125 error ("no matching template for %qD found", result);
19126 return;
19128 else if (!DECL_TEMPLATE_INFO (result))
19130 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19131 return;
19134 if (storage == NULL_TREE)
19136 else if (storage == ridpointers[(int) RID_EXTERN])
19138 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19139 pedwarn (input_location, OPT_Wpedantic,
19140 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19141 "instantiations");
19142 extern_p = 1;
19144 else
19145 error ("storage class %qD applied to template instantiation", storage);
19147 check_explicit_instantiation_namespace (result);
19148 mark_decl_instantiated (result, extern_p);
19149 if (! extern_p)
19150 instantiate_decl (result, /*defer_ok=*/1,
19151 /*expl_inst_class_mem_p=*/false);
19154 static void
19155 mark_class_instantiated (tree t, int extern_p)
19157 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19158 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19159 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19160 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19161 if (! extern_p)
19163 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19164 rest_of_type_compilation (t, 1);
19168 /* Called from do_type_instantiation through binding_table_foreach to
19169 do recursive instantiation for the type bound in ENTRY. */
19170 static void
19171 bt_instantiate_type_proc (binding_entry entry, void *data)
19173 tree storage = *(tree *) data;
19175 if (MAYBE_CLASS_TYPE_P (entry->type)
19176 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19177 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19180 /* Called from do_type_instantiation to instantiate a member
19181 (a member function or a static member variable) of an
19182 explicitly instantiated class template. */
19183 static void
19184 instantiate_class_member (tree decl, int extern_p)
19186 mark_decl_instantiated (decl, extern_p);
19187 if (! extern_p)
19188 instantiate_decl (decl, /*defer_ok=*/1,
19189 /*expl_inst_class_mem_p=*/true);
19192 /* Perform an explicit instantiation of template class T. STORAGE, if
19193 non-null, is the RID for extern, inline or static. COMPLAIN is
19194 nonzero if this is called from the parser, zero if called recursively,
19195 since the standard is unclear (as detailed below). */
19197 void
19198 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19200 int extern_p = 0;
19201 int nomem_p = 0;
19202 int static_p = 0;
19203 int previous_instantiation_extern_p = 0;
19205 if (TREE_CODE (t) == TYPE_DECL)
19206 t = TREE_TYPE (t);
19208 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19210 tree tmpl =
19211 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19212 if (tmpl)
19213 error ("explicit instantiation of non-class template %qD", tmpl);
19214 else
19215 error ("explicit instantiation of non-template type %qT", t);
19216 return;
19219 complete_type (t);
19221 if (!COMPLETE_TYPE_P (t))
19223 if (complain & tf_error)
19224 error ("explicit instantiation of %q#T before definition of template",
19226 return;
19229 if (storage != NULL_TREE)
19231 if (!in_system_header_at (input_location))
19233 if (storage == ridpointers[(int) RID_EXTERN])
19235 if (cxx_dialect == cxx98)
19236 pedwarn (input_location, OPT_Wpedantic,
19237 "ISO C++ 1998 forbids the use of %<extern%> on "
19238 "explicit instantiations");
19240 else
19241 pedwarn (input_location, OPT_Wpedantic,
19242 "ISO C++ forbids the use of %qE"
19243 " on explicit instantiations", storage);
19246 if (storage == ridpointers[(int) RID_INLINE])
19247 nomem_p = 1;
19248 else if (storage == ridpointers[(int) RID_EXTERN])
19249 extern_p = 1;
19250 else if (storage == ridpointers[(int) RID_STATIC])
19251 static_p = 1;
19252 else
19254 error ("storage class %qD applied to template instantiation",
19255 storage);
19256 extern_p = 0;
19260 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19262 /* DR 259 [temp.spec].
19264 Both an explicit instantiation and a declaration of an explicit
19265 specialization shall not appear in a program unless the explicit
19266 instantiation follows a declaration of the explicit specialization.
19268 For a given set of template parameters, if an explicit
19269 instantiation of a template appears after a declaration of an
19270 explicit specialization for that template, the explicit
19271 instantiation has no effect. */
19272 return;
19274 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19276 /* [temp.spec]
19278 No program shall explicitly instantiate any template more
19279 than once.
19281 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19282 instantiation was `extern'. If EXTERN_P then the second is.
19283 These cases are OK. */
19284 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19286 if (!previous_instantiation_extern_p && !extern_p
19287 && (complain & tf_error))
19288 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19290 /* If we've already instantiated the template, just return now. */
19291 if (!CLASSTYPE_INTERFACE_ONLY (t))
19292 return;
19295 check_explicit_instantiation_namespace (TYPE_NAME (t));
19296 mark_class_instantiated (t, extern_p);
19298 if (nomem_p)
19299 return;
19302 tree tmp;
19304 /* In contrast to implicit instantiation, where only the
19305 declarations, and not the definitions, of members are
19306 instantiated, we have here:
19308 [temp.explicit]
19310 The explicit instantiation of a class template specialization
19311 implies the instantiation of all of its members not
19312 previously explicitly specialized in the translation unit
19313 containing the explicit instantiation.
19315 Of course, we can't instantiate member template classes, since
19316 we don't have any arguments for them. Note that the standard
19317 is unclear on whether the instantiation of the members are
19318 *explicit* instantiations or not. However, the most natural
19319 interpretation is that it should be an explicit instantiation. */
19321 if (! static_p)
19322 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19323 if (TREE_CODE (tmp) == FUNCTION_DECL
19324 && DECL_TEMPLATE_INSTANTIATION (tmp))
19325 instantiate_class_member (tmp, extern_p);
19327 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19328 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19329 instantiate_class_member (tmp, extern_p);
19331 if (CLASSTYPE_NESTED_UTDS (t))
19332 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19333 bt_instantiate_type_proc, &storage);
19337 /* Given a function DECL, which is a specialization of TMPL, modify
19338 DECL to be a re-instantiation of TMPL with the same template
19339 arguments. TMPL should be the template into which tsubst'ing
19340 should occur for DECL, not the most general template.
19342 One reason for doing this is a scenario like this:
19344 template <class T>
19345 void f(const T&, int i);
19347 void g() { f(3, 7); }
19349 template <class T>
19350 void f(const T& t, const int i) { }
19352 Note that when the template is first instantiated, with
19353 instantiate_template, the resulting DECL will have no name for the
19354 first parameter, and the wrong type for the second. So, when we go
19355 to instantiate the DECL, we regenerate it. */
19357 static void
19358 regenerate_decl_from_template (tree decl, tree tmpl)
19360 /* The arguments used to instantiate DECL, from the most general
19361 template. */
19362 tree args;
19363 tree code_pattern;
19365 args = DECL_TI_ARGS (decl);
19366 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19368 /* Make sure that we can see identifiers, and compute access
19369 correctly. */
19370 push_access_scope (decl);
19372 if (TREE_CODE (decl) == FUNCTION_DECL)
19374 tree decl_parm;
19375 tree pattern_parm;
19376 tree specs;
19377 int args_depth;
19378 int parms_depth;
19380 args_depth = TMPL_ARGS_DEPTH (args);
19381 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19382 if (args_depth > parms_depth)
19383 args = get_innermost_template_args (args, parms_depth);
19385 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19386 args, tf_error, NULL_TREE,
19387 /*defer_ok*/false);
19388 if (specs && specs != error_mark_node)
19389 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19390 specs);
19392 /* Merge parameter declarations. */
19393 decl_parm = skip_artificial_parms_for (decl,
19394 DECL_ARGUMENTS (decl));
19395 pattern_parm
19396 = skip_artificial_parms_for (code_pattern,
19397 DECL_ARGUMENTS (code_pattern));
19398 while (decl_parm && !DECL_PACK_P (pattern_parm))
19400 tree parm_type;
19401 tree attributes;
19403 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19404 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19405 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19406 NULL_TREE);
19407 parm_type = type_decays_to (parm_type);
19408 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19409 TREE_TYPE (decl_parm) = parm_type;
19410 attributes = DECL_ATTRIBUTES (pattern_parm);
19411 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19413 DECL_ATTRIBUTES (decl_parm) = attributes;
19414 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19416 decl_parm = DECL_CHAIN (decl_parm);
19417 pattern_parm = DECL_CHAIN (pattern_parm);
19419 /* Merge any parameters that match with the function parameter
19420 pack. */
19421 if (pattern_parm && DECL_PACK_P (pattern_parm))
19423 int i, len;
19424 tree expanded_types;
19425 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19426 the parameters in this function parameter pack. */
19427 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19428 args, tf_error, NULL_TREE);
19429 len = TREE_VEC_LENGTH (expanded_types);
19430 for (i = 0; i < len; i++)
19432 tree parm_type;
19433 tree attributes;
19435 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19436 /* Rename the parameter to include the index. */
19437 DECL_NAME (decl_parm) =
19438 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19439 parm_type = TREE_VEC_ELT (expanded_types, i);
19440 parm_type = type_decays_to (parm_type);
19441 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19442 TREE_TYPE (decl_parm) = parm_type;
19443 attributes = DECL_ATTRIBUTES (pattern_parm);
19444 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19446 DECL_ATTRIBUTES (decl_parm) = attributes;
19447 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19449 decl_parm = DECL_CHAIN (decl_parm);
19452 /* Merge additional specifiers from the CODE_PATTERN. */
19453 if (DECL_DECLARED_INLINE_P (code_pattern)
19454 && !DECL_DECLARED_INLINE_P (decl))
19455 DECL_DECLARED_INLINE_P (decl) = 1;
19457 else if (VAR_P (decl))
19459 DECL_INITIAL (decl) =
19460 tsubst_expr (DECL_INITIAL (code_pattern), args,
19461 tf_error, DECL_TI_TEMPLATE (decl),
19462 /*integral_constant_expression_p=*/false);
19463 if (VAR_HAD_UNKNOWN_BOUND (decl))
19464 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19465 tf_error, DECL_TI_TEMPLATE (decl));
19467 else
19468 gcc_unreachable ();
19470 pop_access_scope (decl);
19473 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19474 substituted to get DECL. */
19476 tree
19477 template_for_substitution (tree decl)
19479 tree tmpl = DECL_TI_TEMPLATE (decl);
19481 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19482 for the instantiation. This is not always the most general
19483 template. Consider, for example:
19485 template <class T>
19486 struct S { template <class U> void f();
19487 template <> void f<int>(); };
19489 and an instantiation of S<double>::f<int>. We want TD to be the
19490 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19491 while (/* An instantiation cannot have a definition, so we need a
19492 more general template. */
19493 DECL_TEMPLATE_INSTANTIATION (tmpl)
19494 /* We must also deal with friend templates. Given:
19496 template <class T> struct S {
19497 template <class U> friend void f() {};
19500 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19501 so far as the language is concerned, but that's still
19502 where we get the pattern for the instantiation from. On
19503 other hand, if the definition comes outside the class, say:
19505 template <class T> struct S {
19506 template <class U> friend void f();
19508 template <class U> friend void f() {}
19510 we don't need to look any further. That's what the check for
19511 DECL_INITIAL is for. */
19512 || (TREE_CODE (decl) == FUNCTION_DECL
19513 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19514 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19516 /* The present template, TD, should not be a definition. If it
19517 were a definition, we should be using it! Note that we
19518 cannot restructure the loop to just keep going until we find
19519 a template with a definition, since that might go too far if
19520 a specialization was declared, but not defined. */
19521 gcc_assert (!VAR_P (decl)
19522 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19524 /* Fetch the more general template. */
19525 tmpl = DECL_TI_TEMPLATE (tmpl);
19528 return tmpl;
19531 /* Returns true if we need to instantiate this template instance even if we
19532 know we aren't going to emit it.. */
19534 bool
19535 always_instantiate_p (tree decl)
19537 /* We always instantiate inline functions so that we can inline them. An
19538 explicit instantiation declaration prohibits implicit instantiation of
19539 non-inline functions. With high levels of optimization, we would
19540 normally inline non-inline functions -- but we're not allowed to do
19541 that for "extern template" functions. Therefore, we check
19542 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19543 return ((TREE_CODE (decl) == FUNCTION_DECL
19544 && (DECL_DECLARED_INLINE_P (decl)
19545 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19546 /* And we need to instantiate static data members so that
19547 their initializers are available in integral constant
19548 expressions. */
19549 || (VAR_P (decl)
19550 && decl_maybe_constant_var_p (decl)));
19553 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19554 instantiate it now, modifying TREE_TYPE (fn). */
19556 void
19557 maybe_instantiate_noexcept (tree fn)
19559 tree fntype, spec, noex, clone;
19561 /* Don't instantiate a noexcept-specification from template context. */
19562 if (processing_template_decl)
19563 return;
19565 if (DECL_CLONED_FUNCTION_P (fn))
19566 fn = DECL_CLONED_FUNCTION (fn);
19567 fntype = TREE_TYPE (fn);
19568 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19570 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19571 return;
19573 noex = TREE_PURPOSE (spec);
19575 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19577 if (push_tinst_level (fn))
19579 push_access_scope (fn);
19580 push_deferring_access_checks (dk_no_deferred);
19581 input_location = DECL_SOURCE_LOCATION (fn);
19582 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19583 DEFERRED_NOEXCEPT_ARGS (noex),
19584 tf_warning_or_error, fn,
19585 /*function_p=*/false,
19586 /*integral_constant_expression_p=*/true);
19587 pop_deferring_access_checks ();
19588 pop_access_scope (fn);
19589 pop_tinst_level ();
19590 spec = build_noexcept_spec (noex, tf_warning_or_error);
19591 if (spec == error_mark_node)
19592 spec = noexcept_false_spec;
19594 else
19595 spec = noexcept_false_spec;
19597 else
19599 /* This is an implicitly declared function, so NOEX is a list of
19600 other functions to evaluate and merge. */
19601 tree elt;
19602 spec = noexcept_true_spec;
19603 for (elt = noex; elt; elt = OVL_NEXT (elt))
19605 tree fn = OVL_CURRENT (elt);
19606 tree subspec;
19607 maybe_instantiate_noexcept (fn);
19608 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19609 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19613 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19615 FOR_EACH_CLONE (clone, fn)
19617 if (TREE_TYPE (clone) == fntype)
19618 TREE_TYPE (clone) = TREE_TYPE (fn);
19619 else
19620 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19624 /* Produce the definition of D, a _DECL generated from a template. If
19625 DEFER_OK is nonzero, then we don't have to actually do the
19626 instantiation now; we just have to do it sometime. Normally it is
19627 an error if this is an explicit instantiation but D is undefined.
19628 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19629 explicitly instantiated class template. */
19631 tree
19632 instantiate_decl (tree d, int defer_ok,
19633 bool expl_inst_class_mem_p)
19635 tree tmpl = DECL_TI_TEMPLATE (d);
19636 tree gen_args;
19637 tree args;
19638 tree td;
19639 tree code_pattern;
19640 tree spec;
19641 tree gen_tmpl;
19642 bool pattern_defined;
19643 location_t saved_loc = input_location;
19644 int saved_unevaluated_operand = cp_unevaluated_operand;
19645 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19646 bool external_p;
19647 tree fn_context;
19648 bool nested;
19650 /* This function should only be used to instantiate templates for
19651 functions and static member variables. */
19652 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19654 /* Variables are never deferred; if instantiation is required, they
19655 are instantiated right away. That allows for better code in the
19656 case that an expression refers to the value of the variable --
19657 if the variable has a constant value the referring expression can
19658 take advantage of that fact. */
19659 if (VAR_P (d)
19660 || DECL_DECLARED_CONSTEXPR_P (d))
19661 defer_ok = 0;
19663 /* Don't instantiate cloned functions. Instead, instantiate the
19664 functions they cloned. */
19665 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19666 d = DECL_CLONED_FUNCTION (d);
19668 if (DECL_TEMPLATE_INSTANTIATED (d)
19669 || (TREE_CODE (d) == FUNCTION_DECL
19670 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19671 || DECL_TEMPLATE_SPECIALIZATION (d))
19672 /* D has already been instantiated or explicitly specialized, so
19673 there's nothing for us to do here.
19675 It might seem reasonable to check whether or not D is an explicit
19676 instantiation, and, if so, stop here. But when an explicit
19677 instantiation is deferred until the end of the compilation,
19678 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19679 the instantiation. */
19680 return d;
19682 /* Check to see whether we know that this template will be
19683 instantiated in some other file, as with "extern template"
19684 extension. */
19685 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19687 /* In general, we do not instantiate such templates. */
19688 if (external_p && !always_instantiate_p (d))
19689 return d;
19691 gen_tmpl = most_general_template (tmpl);
19692 gen_args = DECL_TI_ARGS (d);
19694 if (tmpl != gen_tmpl)
19695 /* We should already have the extra args. */
19696 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19697 == TMPL_ARGS_DEPTH (gen_args));
19698 /* And what's in the hash table should match D. */
19699 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19700 || spec == NULL_TREE);
19702 /* This needs to happen before any tsubsting. */
19703 if (! push_tinst_level (d))
19704 return d;
19706 timevar_push (TV_TEMPLATE_INST);
19708 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19709 for the instantiation. */
19710 td = template_for_substitution (d);
19711 code_pattern = DECL_TEMPLATE_RESULT (td);
19713 /* We should never be trying to instantiate a member of a class
19714 template or partial specialization. */
19715 gcc_assert (d != code_pattern);
19717 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19718 || DECL_TEMPLATE_SPECIALIZATION (td))
19719 /* In the case of a friend template whose definition is provided
19720 outside the class, we may have too many arguments. Drop the
19721 ones we don't need. The same is true for specializations. */
19722 args = get_innermost_template_args
19723 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19724 else
19725 args = gen_args;
19727 if (TREE_CODE (d) == FUNCTION_DECL)
19728 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19729 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19730 else
19731 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19733 /* We may be in the middle of deferred access check. Disable it now. */
19734 push_deferring_access_checks (dk_no_deferred);
19736 /* Unless an explicit instantiation directive has already determined
19737 the linkage of D, remember that a definition is available for
19738 this entity. */
19739 if (pattern_defined
19740 && !DECL_INTERFACE_KNOWN (d)
19741 && !DECL_NOT_REALLY_EXTERN (d))
19742 mark_definable (d);
19744 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19745 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19746 input_location = DECL_SOURCE_LOCATION (d);
19748 /* If D is a member of an explicitly instantiated class template,
19749 and no definition is available, treat it like an implicit
19750 instantiation. */
19751 if (!pattern_defined && expl_inst_class_mem_p
19752 && DECL_EXPLICIT_INSTANTIATION (d))
19754 /* Leave linkage flags alone on instantiations with anonymous
19755 visibility. */
19756 if (TREE_PUBLIC (d))
19758 DECL_NOT_REALLY_EXTERN (d) = 0;
19759 DECL_INTERFACE_KNOWN (d) = 0;
19761 SET_DECL_IMPLICIT_INSTANTIATION (d);
19764 if (TREE_CODE (d) == FUNCTION_DECL)
19765 maybe_instantiate_noexcept (d);
19767 /* Defer all other templates, unless we have been explicitly
19768 forbidden from doing so. */
19769 if (/* If there is no definition, we cannot instantiate the
19770 template. */
19771 ! pattern_defined
19772 /* If it's OK to postpone instantiation, do so. */
19773 || defer_ok
19774 /* If this is a static data member that will be defined
19775 elsewhere, we don't want to instantiate the entire data
19776 member, but we do want to instantiate the initializer so that
19777 we can substitute that elsewhere. */
19778 || (external_p && VAR_P (d)))
19780 /* The definition of the static data member is now required so
19781 we must substitute the initializer. */
19782 if (VAR_P (d)
19783 && !DECL_INITIAL (d)
19784 && DECL_INITIAL (code_pattern))
19786 tree ns;
19787 tree init;
19788 bool const_init = false;
19790 ns = decl_namespace_context (d);
19791 push_nested_namespace (ns);
19792 push_nested_class (DECL_CONTEXT (d));
19793 init = tsubst_expr (DECL_INITIAL (code_pattern),
19794 args,
19795 tf_warning_or_error, NULL_TREE,
19796 /*integral_constant_expression_p=*/false);
19797 /* If instantiating the initializer involved instantiating this
19798 again, don't call cp_finish_decl twice. */
19799 if (!DECL_INITIAL (d))
19801 /* Make sure the initializer is still constant, in case of
19802 circular dependency (template/instantiate6.C). */
19803 const_init
19804 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19805 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19806 /*asmspec_tree=*/NULL_TREE,
19807 LOOKUP_ONLYCONVERTING);
19809 pop_nested_class ();
19810 pop_nested_namespace (ns);
19813 /* We restore the source position here because it's used by
19814 add_pending_template. */
19815 input_location = saved_loc;
19817 if (at_eof && !pattern_defined
19818 && DECL_EXPLICIT_INSTANTIATION (d)
19819 && DECL_NOT_REALLY_EXTERN (d))
19820 /* [temp.explicit]
19822 The definition of a non-exported function template, a
19823 non-exported member function template, or a non-exported
19824 member function or static data member of a class template
19825 shall be present in every translation unit in which it is
19826 explicitly instantiated. */
19827 permerror (input_location, "explicit instantiation of %qD "
19828 "but no definition available", d);
19830 /* If we're in unevaluated context, we just wanted to get the
19831 constant value; this isn't an odr use, so don't queue
19832 a full instantiation. */
19833 if (cp_unevaluated_operand != 0)
19834 goto out;
19835 /* ??? Historically, we have instantiated inline functions, even
19836 when marked as "extern template". */
19837 if (!(external_p && VAR_P (d)))
19838 add_pending_template (d);
19839 goto out;
19841 /* Tell the repository that D is available in this translation unit
19842 -- and see if it is supposed to be instantiated here. */
19843 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19845 /* In a PCH file, despite the fact that the repository hasn't
19846 requested instantiation in the PCH it is still possible that
19847 an instantiation will be required in a file that includes the
19848 PCH. */
19849 if (pch_file)
19850 add_pending_template (d);
19851 /* Instantiate inline functions so that the inliner can do its
19852 job, even though we'll not be emitting a copy of this
19853 function. */
19854 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19855 goto out;
19858 fn_context = decl_function_context (d);
19859 nested = (current_function_decl != NULL_TREE);
19860 if (!fn_context)
19861 push_to_top_level ();
19862 else
19864 if (nested)
19865 push_function_context ();
19866 cp_unevaluated_operand = 0;
19867 c_inhibit_evaluation_warnings = 0;
19870 /* Mark D as instantiated so that recursive calls to
19871 instantiate_decl do not try to instantiate it again. */
19872 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19874 /* Regenerate the declaration in case the template has been modified
19875 by a subsequent redeclaration. */
19876 regenerate_decl_from_template (d, td);
19878 /* We already set the file and line above. Reset them now in case
19879 they changed as a result of calling regenerate_decl_from_template. */
19880 input_location = DECL_SOURCE_LOCATION (d);
19882 if (VAR_P (d))
19884 tree init;
19885 bool const_init = false;
19887 /* Clear out DECL_RTL; whatever was there before may not be right
19888 since we've reset the type of the declaration. */
19889 SET_DECL_RTL (d, NULL);
19890 DECL_IN_AGGR_P (d) = 0;
19892 /* The initializer is placed in DECL_INITIAL by
19893 regenerate_decl_from_template so we don't need to
19894 push/pop_access_scope again here. Pull it out so that
19895 cp_finish_decl can process it. */
19896 init = DECL_INITIAL (d);
19897 DECL_INITIAL (d) = NULL_TREE;
19898 DECL_INITIALIZED_P (d) = 0;
19900 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19901 initializer. That function will defer actual emission until
19902 we have a chance to determine linkage. */
19903 DECL_EXTERNAL (d) = 0;
19905 /* Enter the scope of D so that access-checking works correctly. */
19906 push_nested_class (DECL_CONTEXT (d));
19907 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19908 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19909 pop_nested_class ();
19911 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19912 synthesize_method (d);
19913 else if (TREE_CODE (d) == FUNCTION_DECL)
19915 struct pointer_map_t *saved_local_specializations;
19916 tree subst_decl;
19917 tree tmpl_parm;
19918 tree spec_parm;
19919 tree block = NULL_TREE;
19921 /* Save away the current list, in case we are instantiating one
19922 template from within the body of another. */
19923 saved_local_specializations = local_specializations;
19925 /* Set up the list of local specializations. */
19926 local_specializations = pointer_map_create ();
19928 /* Set up context. */
19929 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19930 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19931 block = push_stmt_list ();
19932 else
19933 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19935 /* Some typedefs referenced from within the template code need to be
19936 access checked at template instantiation time, i.e now. These
19937 types were added to the template at parsing time. Let's get those
19938 and perform the access checks then. */
19939 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19940 gen_args);
19942 /* Create substitution entries for the parameters. */
19943 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19944 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19945 spec_parm = DECL_ARGUMENTS (d);
19946 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19948 register_local_specialization (spec_parm, tmpl_parm);
19949 spec_parm = skip_artificial_parms_for (d, spec_parm);
19950 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19952 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19954 if (!DECL_PACK_P (tmpl_parm))
19956 register_local_specialization (spec_parm, tmpl_parm);
19957 spec_parm = DECL_CHAIN (spec_parm);
19959 else
19961 /* Register the (value) argument pack as a specialization of
19962 TMPL_PARM, then move on. */
19963 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19964 register_local_specialization (argpack, tmpl_parm);
19967 gcc_assert (!spec_parm);
19969 /* Substitute into the body of the function. */
19970 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19971 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19972 tf_warning_or_error, tmpl);
19973 else
19975 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19976 tf_warning_or_error, tmpl,
19977 /*integral_constant_expression_p=*/false);
19979 /* Set the current input_location to the end of the function
19980 so that finish_function knows where we are. */
19981 input_location
19982 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19984 /* Remember if we saw an infinite loop in the template. */
19985 current_function_infinite_loop
19986 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
19989 /* We don't need the local specializations any more. */
19990 pointer_map_destroy (local_specializations);
19991 local_specializations = saved_local_specializations;
19993 /* Finish the function. */
19994 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19995 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19996 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19997 else
19999 d = finish_function (0);
20000 expand_or_defer_fn (d);
20003 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20004 cp_check_omp_declare_reduction (d);
20007 /* We're not deferring instantiation any more. */
20008 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20010 if (!fn_context)
20011 pop_from_top_level ();
20012 else if (nested)
20013 pop_function_context ();
20015 out:
20016 input_location = saved_loc;
20017 cp_unevaluated_operand = saved_unevaluated_operand;
20018 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20019 pop_deferring_access_checks ();
20020 pop_tinst_level ();
20022 timevar_pop (TV_TEMPLATE_INST);
20024 return d;
20027 /* Run through the list of templates that we wish we could
20028 instantiate, and instantiate any we can. RETRIES is the
20029 number of times we retry pending template instantiation. */
20031 void
20032 instantiate_pending_templates (int retries)
20034 int reconsider;
20035 location_t saved_loc = input_location;
20037 /* Instantiating templates may trigger vtable generation. This in turn
20038 may require further template instantiations. We place a limit here
20039 to avoid infinite loop. */
20040 if (pending_templates && retries >= max_tinst_depth)
20042 tree decl = pending_templates->tinst->decl;
20044 error ("template instantiation depth exceeds maximum of %d"
20045 " instantiating %q+D, possibly from virtual table generation"
20046 " (use -ftemplate-depth= to increase the maximum)",
20047 max_tinst_depth, decl);
20048 if (TREE_CODE (decl) == FUNCTION_DECL)
20049 /* Pretend that we defined it. */
20050 DECL_INITIAL (decl) = error_mark_node;
20051 return;
20056 struct pending_template **t = &pending_templates;
20057 struct pending_template *last = NULL;
20058 reconsider = 0;
20059 while (*t)
20061 tree instantiation = reopen_tinst_level ((*t)->tinst);
20062 bool complete = false;
20064 if (TYPE_P (instantiation))
20066 tree fn;
20068 if (!COMPLETE_TYPE_P (instantiation))
20070 instantiate_class_template (instantiation);
20071 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20072 for (fn = TYPE_METHODS (instantiation);
20074 fn = TREE_CHAIN (fn))
20075 if (! DECL_ARTIFICIAL (fn))
20076 instantiate_decl (fn,
20077 /*defer_ok=*/0,
20078 /*expl_inst_class_mem_p=*/false);
20079 if (COMPLETE_TYPE_P (instantiation))
20080 reconsider = 1;
20083 complete = COMPLETE_TYPE_P (instantiation);
20085 else
20087 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20088 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20090 instantiation
20091 = instantiate_decl (instantiation,
20092 /*defer_ok=*/0,
20093 /*expl_inst_class_mem_p=*/false);
20094 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20095 reconsider = 1;
20098 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20099 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20102 if (complete)
20103 /* If INSTANTIATION has been instantiated, then we don't
20104 need to consider it again in the future. */
20105 *t = (*t)->next;
20106 else
20108 last = *t;
20109 t = &(*t)->next;
20111 tinst_depth = 0;
20112 current_tinst_level = NULL;
20114 last_pending_template = last;
20116 while (reconsider);
20118 input_location = saved_loc;
20121 /* Substitute ARGVEC into T, which is a list of initializers for
20122 either base class or a non-static data member. The TREE_PURPOSEs
20123 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20124 instantiate_decl. */
20126 static tree
20127 tsubst_initializer_list (tree t, tree argvec)
20129 tree inits = NULL_TREE;
20131 for (; t; t = TREE_CHAIN (t))
20133 tree decl;
20134 tree init;
20135 tree expanded_bases = NULL_TREE;
20136 tree expanded_arguments = NULL_TREE;
20137 int i, len = 1;
20139 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20141 tree expr;
20142 tree arg;
20144 /* Expand the base class expansion type into separate base
20145 classes. */
20146 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20147 tf_warning_or_error,
20148 NULL_TREE);
20149 if (expanded_bases == error_mark_node)
20150 continue;
20152 /* We'll be building separate TREE_LISTs of arguments for
20153 each base. */
20154 len = TREE_VEC_LENGTH (expanded_bases);
20155 expanded_arguments = make_tree_vec (len);
20156 for (i = 0; i < len; i++)
20157 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20159 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20160 expand each argument in the TREE_VALUE of t. */
20161 expr = make_node (EXPR_PACK_EXPANSION);
20162 PACK_EXPANSION_LOCAL_P (expr) = true;
20163 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20164 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20166 if (TREE_VALUE (t) == void_type_node)
20167 /* VOID_TYPE_NODE is used to indicate
20168 value-initialization. */
20170 for (i = 0; i < len; i++)
20171 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20173 else
20175 /* Substitute parameter packs into each argument in the
20176 TREE_LIST. */
20177 in_base_initializer = 1;
20178 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20180 tree expanded_exprs;
20182 /* Expand the argument. */
20183 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20184 expanded_exprs
20185 = tsubst_pack_expansion (expr, argvec,
20186 tf_warning_or_error,
20187 NULL_TREE);
20188 if (expanded_exprs == error_mark_node)
20189 continue;
20191 /* Prepend each of the expanded expressions to the
20192 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20193 for (i = 0; i < len; i++)
20195 TREE_VEC_ELT (expanded_arguments, i) =
20196 tree_cons (NULL_TREE,
20197 TREE_VEC_ELT (expanded_exprs, i),
20198 TREE_VEC_ELT (expanded_arguments, i));
20201 in_base_initializer = 0;
20203 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20204 since we built them backwards. */
20205 for (i = 0; i < len; i++)
20207 TREE_VEC_ELT (expanded_arguments, i) =
20208 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20213 for (i = 0; i < len; ++i)
20215 if (expanded_bases)
20217 decl = TREE_VEC_ELT (expanded_bases, i);
20218 decl = expand_member_init (decl);
20219 init = TREE_VEC_ELT (expanded_arguments, i);
20221 else
20223 tree tmp;
20224 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20225 tf_warning_or_error, NULL_TREE);
20227 decl = expand_member_init (decl);
20228 if (decl && !DECL_P (decl))
20229 in_base_initializer = 1;
20231 init = TREE_VALUE (t);
20232 tmp = init;
20233 if (init != void_type_node)
20234 init = tsubst_expr (init, argvec,
20235 tf_warning_or_error, NULL_TREE,
20236 /*integral_constant_expression_p=*/false);
20237 if (init == NULL_TREE && tmp != NULL_TREE)
20238 /* If we had an initializer but it instantiated to nothing,
20239 value-initialize the object. This will only occur when
20240 the initializer was a pack expansion where the parameter
20241 packs used in that expansion were of length zero. */
20242 init = void_type_node;
20243 in_base_initializer = 0;
20246 if (decl)
20248 init = build_tree_list (decl, init);
20249 TREE_CHAIN (init) = inits;
20250 inits = init;
20254 return inits;
20257 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20259 static void
20260 set_current_access_from_decl (tree decl)
20262 if (TREE_PRIVATE (decl))
20263 current_access_specifier = access_private_node;
20264 else if (TREE_PROTECTED (decl))
20265 current_access_specifier = access_protected_node;
20266 else
20267 current_access_specifier = access_public_node;
20270 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20271 is the instantiation (which should have been created with
20272 start_enum) and ARGS are the template arguments to use. */
20274 static void
20275 tsubst_enum (tree tag, tree newtag, tree args)
20277 tree e;
20279 if (SCOPED_ENUM_P (newtag))
20280 begin_scope (sk_scoped_enum, newtag);
20282 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20284 tree value;
20285 tree decl;
20287 decl = TREE_VALUE (e);
20288 /* Note that in a template enum, the TREE_VALUE is the
20289 CONST_DECL, not the corresponding INTEGER_CST. */
20290 value = tsubst_expr (DECL_INITIAL (decl),
20291 args, tf_warning_or_error, NULL_TREE,
20292 /*integral_constant_expression_p=*/true);
20294 /* Give this enumeration constant the correct access. */
20295 set_current_access_from_decl (decl);
20297 /* Actually build the enumerator itself. */
20298 build_enumerator
20299 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20302 if (SCOPED_ENUM_P (newtag))
20303 finish_scope ();
20305 finish_enum_value_list (newtag);
20306 finish_enum (newtag);
20308 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20309 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20312 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20313 its type -- but without substituting the innermost set of template
20314 arguments. So, innermost set of template parameters will appear in
20315 the type. */
20317 tree
20318 get_mostly_instantiated_function_type (tree decl)
20320 tree fn_type;
20321 tree tmpl;
20322 tree targs;
20323 tree tparms;
20324 int parm_depth;
20326 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20327 targs = DECL_TI_ARGS (decl);
20328 tparms = DECL_TEMPLATE_PARMS (tmpl);
20329 parm_depth = TMPL_PARMS_DEPTH (tparms);
20331 /* There should be as many levels of arguments as there are levels
20332 of parameters. */
20333 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20335 fn_type = TREE_TYPE (tmpl);
20337 if (parm_depth == 1)
20338 /* No substitution is necessary. */
20340 else
20342 int i;
20343 tree partial_args;
20345 /* Replace the innermost level of the TARGS with NULL_TREEs to
20346 let tsubst know not to substitute for those parameters. */
20347 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20348 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20349 SET_TMPL_ARGS_LEVEL (partial_args, i,
20350 TMPL_ARGS_LEVEL (targs, i));
20351 SET_TMPL_ARGS_LEVEL (partial_args,
20352 TMPL_ARGS_DEPTH (targs),
20353 make_tree_vec (DECL_NTPARMS (tmpl)));
20355 /* Make sure that we can see identifiers, and compute access
20356 correctly. */
20357 push_access_scope (decl);
20359 ++processing_template_decl;
20360 /* Now, do the (partial) substitution to figure out the
20361 appropriate function type. */
20362 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20363 --processing_template_decl;
20365 /* Substitute into the template parameters to obtain the real
20366 innermost set of parameters. This step is important if the
20367 innermost set of template parameters contains value
20368 parameters whose types depend on outer template parameters. */
20369 TREE_VEC_LENGTH (partial_args)--;
20370 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20372 pop_access_scope (decl);
20375 return fn_type;
20378 /* Return truthvalue if we're processing a template different from
20379 the last one involved in diagnostics. */
20381 problematic_instantiation_changed (void)
20383 return current_tinst_level != last_error_tinst_level;
20386 /* Remember current template involved in diagnostics. */
20387 void
20388 record_last_problematic_instantiation (void)
20390 last_error_tinst_level = current_tinst_level;
20393 struct tinst_level *
20394 current_instantiation (void)
20396 return current_tinst_level;
20399 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20400 type. Return zero for ok, nonzero for disallowed. Issue error and
20401 warning messages under control of COMPLAIN. */
20403 static int
20404 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20406 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20407 return 0;
20408 else if (POINTER_TYPE_P (type))
20409 return 0;
20410 else if (TYPE_PTRMEM_P (type))
20411 return 0;
20412 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20413 return 0;
20414 else if (TREE_CODE (type) == TYPENAME_TYPE)
20415 return 0;
20416 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20417 return 0;
20418 else if (TREE_CODE (type) == NULLPTR_TYPE)
20419 return 0;
20421 if (complain & tf_error)
20423 if (type == error_mark_node)
20424 inform (input_location, "invalid template non-type parameter");
20425 else
20426 error ("%q#T is not a valid type for a template non-type parameter",
20427 type);
20429 return 1;
20432 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20433 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20435 static bool
20436 dependent_type_p_r (tree type)
20438 tree scope;
20440 /* [temp.dep.type]
20442 A type is dependent if it is:
20444 -- a template parameter. Template template parameters are types
20445 for us (since TYPE_P holds true for them) so we handle
20446 them here. */
20447 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20448 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20449 return true;
20450 /* -- a qualified-id with a nested-name-specifier which contains a
20451 class-name that names a dependent type or whose unqualified-id
20452 names a dependent type. */
20453 if (TREE_CODE (type) == TYPENAME_TYPE)
20454 return true;
20455 /* -- a cv-qualified type where the cv-unqualified type is
20456 dependent. */
20457 type = TYPE_MAIN_VARIANT (type);
20458 /* -- a compound type constructed from any dependent type. */
20459 if (TYPE_PTRMEM_P (type))
20460 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20461 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20462 (type)));
20463 else if (TYPE_PTR_P (type)
20464 || TREE_CODE (type) == REFERENCE_TYPE)
20465 return dependent_type_p (TREE_TYPE (type));
20466 else if (TREE_CODE (type) == FUNCTION_TYPE
20467 || TREE_CODE (type) == METHOD_TYPE)
20469 tree arg_type;
20471 if (dependent_type_p (TREE_TYPE (type)))
20472 return true;
20473 for (arg_type = TYPE_ARG_TYPES (type);
20474 arg_type;
20475 arg_type = TREE_CHAIN (arg_type))
20476 if (dependent_type_p (TREE_VALUE (arg_type)))
20477 return true;
20478 return false;
20480 /* -- an array type constructed from any dependent type or whose
20481 size is specified by a constant expression that is
20482 value-dependent.
20484 We checked for type- and value-dependence of the bounds in
20485 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20486 if (TREE_CODE (type) == ARRAY_TYPE)
20488 if (TYPE_DOMAIN (type)
20489 && dependent_type_p (TYPE_DOMAIN (type)))
20490 return true;
20491 return dependent_type_p (TREE_TYPE (type));
20494 /* -- a template-id in which either the template name is a template
20495 parameter ... */
20496 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20497 return true;
20498 /* ... or any of the template arguments is a dependent type or
20499 an expression that is type-dependent or value-dependent. */
20500 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20501 && (any_dependent_template_arguments_p
20502 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20503 return true;
20505 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20506 dependent; if the argument of the `typeof' expression is not
20507 type-dependent, then it should already been have resolved. */
20508 if (TREE_CODE (type) == TYPEOF_TYPE
20509 || TREE_CODE (type) == DECLTYPE_TYPE
20510 || TREE_CODE (type) == UNDERLYING_TYPE)
20511 return true;
20513 /* A template argument pack is dependent if any of its packed
20514 arguments are. */
20515 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20517 tree args = ARGUMENT_PACK_ARGS (type);
20518 int i, len = TREE_VEC_LENGTH (args);
20519 for (i = 0; i < len; ++i)
20520 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20521 return true;
20524 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20525 be template parameters. */
20526 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20527 return true;
20529 /* The standard does not specifically mention types that are local
20530 to template functions or local classes, but they should be
20531 considered dependent too. For example:
20533 template <int I> void f() {
20534 enum E { a = I };
20535 S<sizeof (E)> s;
20538 The size of `E' cannot be known until the value of `I' has been
20539 determined. Therefore, `E' must be considered dependent. */
20540 scope = TYPE_CONTEXT (type);
20541 if (scope && TYPE_P (scope))
20542 return dependent_type_p (scope);
20543 /* Don't use type_dependent_expression_p here, as it can lead
20544 to infinite recursion trying to determine whether a lambda
20545 nested in a lambda is dependent (c++/47687). */
20546 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20547 && DECL_LANG_SPECIFIC (scope)
20548 && DECL_TEMPLATE_INFO (scope)
20549 && (any_dependent_template_arguments_p
20550 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20551 return true;
20553 /* Other types are non-dependent. */
20554 return false;
20557 /* Returns TRUE if TYPE is dependent, in the sense of
20558 [temp.dep.type]. Note that a NULL type is considered dependent. */
20560 bool
20561 dependent_type_p (tree type)
20563 /* If there are no template parameters in scope, then there can't be
20564 any dependent types. */
20565 if (!processing_template_decl)
20567 /* If we are not processing a template, then nobody should be
20568 providing us with a dependent type. */
20569 gcc_assert (type);
20570 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20571 return false;
20574 /* If the type is NULL, we have not computed a type for the entity
20575 in question; in that case, the type is dependent. */
20576 if (!type)
20577 return true;
20579 /* Erroneous types can be considered non-dependent. */
20580 if (type == error_mark_node)
20581 return false;
20583 /* If we have not already computed the appropriate value for TYPE,
20584 do so now. */
20585 if (!TYPE_DEPENDENT_P_VALID (type))
20587 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20588 TYPE_DEPENDENT_P_VALID (type) = 1;
20591 return TYPE_DEPENDENT_P (type);
20594 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20595 lookup. In other words, a dependent type that is not the current
20596 instantiation. */
20598 bool
20599 dependent_scope_p (tree scope)
20601 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20602 && !currently_open_class (scope));
20605 /* T is a SCOPE_REF; return whether we need to consider it
20606 instantiation-dependent so that we can check access at instantiation
20607 time even though we know which member it resolves to. */
20609 static bool
20610 instantiation_dependent_scope_ref_p (tree t)
20612 if (DECL_P (TREE_OPERAND (t, 1))
20613 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20614 && accessible_in_template_p (TREE_OPERAND (t, 0),
20615 TREE_OPERAND (t, 1)))
20616 return false;
20617 else
20618 return true;
20621 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20622 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20623 expression. */
20625 /* Note that this predicate is not appropriate for general expressions;
20626 only constant expressions (that satisfy potential_constant_expression)
20627 can be tested for value dependence. */
20629 bool
20630 value_dependent_expression_p (tree expression)
20632 if (!processing_template_decl)
20633 return false;
20635 /* A name declared with a dependent type. */
20636 if (DECL_P (expression) && type_dependent_expression_p (expression))
20637 return true;
20639 switch (TREE_CODE (expression))
20641 case IDENTIFIER_NODE:
20642 /* A name that has not been looked up -- must be dependent. */
20643 return true;
20645 case TEMPLATE_PARM_INDEX:
20646 /* A non-type template parm. */
20647 return true;
20649 case CONST_DECL:
20650 /* A non-type template parm. */
20651 if (DECL_TEMPLATE_PARM_P (expression))
20652 return true;
20653 return value_dependent_expression_p (DECL_INITIAL (expression));
20655 case VAR_DECL:
20656 /* A constant with literal type and is initialized
20657 with an expression that is value-dependent.
20659 Note that a non-dependent parenthesized initializer will have
20660 already been replaced with its constant value, so if we see
20661 a TREE_LIST it must be dependent. */
20662 if (DECL_INITIAL (expression)
20663 && decl_constant_var_p (expression)
20664 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20665 || value_dependent_expression_p (DECL_INITIAL (expression))))
20666 return true;
20667 return false;
20669 case DYNAMIC_CAST_EXPR:
20670 case STATIC_CAST_EXPR:
20671 case CONST_CAST_EXPR:
20672 case REINTERPRET_CAST_EXPR:
20673 case CAST_EXPR:
20674 /* These expressions are value-dependent if the type to which
20675 the cast occurs is dependent or the expression being casted
20676 is value-dependent. */
20678 tree type = TREE_TYPE (expression);
20680 if (dependent_type_p (type))
20681 return true;
20683 /* A functional cast has a list of operands. */
20684 expression = TREE_OPERAND (expression, 0);
20685 if (!expression)
20687 /* If there are no operands, it must be an expression such
20688 as "int()". This should not happen for aggregate types
20689 because it would form non-constant expressions. */
20690 gcc_assert (cxx_dialect >= cxx11
20691 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20693 return false;
20696 if (TREE_CODE (expression) == TREE_LIST)
20697 return any_value_dependent_elements_p (expression);
20699 return value_dependent_expression_p (expression);
20702 case SIZEOF_EXPR:
20703 if (SIZEOF_EXPR_TYPE_P (expression))
20704 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20705 /* FALLTHRU */
20706 case ALIGNOF_EXPR:
20707 case TYPEID_EXPR:
20708 /* A `sizeof' expression is value-dependent if the operand is
20709 type-dependent or is a pack expansion. */
20710 expression = TREE_OPERAND (expression, 0);
20711 if (PACK_EXPANSION_P (expression))
20712 return true;
20713 else if (TYPE_P (expression))
20714 return dependent_type_p (expression);
20715 return instantiation_dependent_expression_p (expression);
20717 case AT_ENCODE_EXPR:
20718 /* An 'encode' expression is value-dependent if the operand is
20719 type-dependent. */
20720 expression = TREE_OPERAND (expression, 0);
20721 return dependent_type_p (expression);
20723 case NOEXCEPT_EXPR:
20724 expression = TREE_OPERAND (expression, 0);
20725 return instantiation_dependent_expression_p (expression);
20727 case SCOPE_REF:
20728 /* All instantiation-dependent expressions should also be considered
20729 value-dependent. */
20730 return instantiation_dependent_scope_ref_p (expression);
20732 case COMPONENT_REF:
20733 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20734 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20736 case NONTYPE_ARGUMENT_PACK:
20737 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20738 is value-dependent. */
20740 tree values = ARGUMENT_PACK_ARGS (expression);
20741 int i, len = TREE_VEC_LENGTH (values);
20743 for (i = 0; i < len; ++i)
20744 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20745 return true;
20747 return false;
20750 case TRAIT_EXPR:
20752 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20753 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20754 || (type2 ? dependent_type_p (type2) : false));
20757 case MODOP_EXPR:
20758 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20759 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20761 case ARRAY_REF:
20762 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20763 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20765 case ADDR_EXPR:
20767 tree op = TREE_OPERAND (expression, 0);
20768 return (value_dependent_expression_p (op)
20769 || has_value_dependent_address (op));
20772 case CALL_EXPR:
20774 tree fn = get_callee_fndecl (expression);
20775 int i, nargs;
20776 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20777 return true;
20778 nargs = call_expr_nargs (expression);
20779 for (i = 0; i < nargs; ++i)
20781 tree op = CALL_EXPR_ARG (expression, i);
20782 /* In a call to a constexpr member function, look through the
20783 implicit ADDR_EXPR on the object argument so that it doesn't
20784 cause the call to be considered value-dependent. We also
20785 look through it in potential_constant_expression. */
20786 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20787 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20788 && TREE_CODE (op) == ADDR_EXPR)
20789 op = TREE_OPERAND (op, 0);
20790 if (value_dependent_expression_p (op))
20791 return true;
20793 return false;
20796 case TEMPLATE_ID_EXPR:
20797 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20798 type-dependent. */
20799 return type_dependent_expression_p (expression);
20801 case CONSTRUCTOR:
20803 unsigned ix;
20804 tree val;
20805 if (dependent_type_p (TREE_TYPE (expression)))
20806 return true;
20807 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20808 if (value_dependent_expression_p (val))
20809 return true;
20810 return false;
20813 case STMT_EXPR:
20814 /* Treat a GNU statement expression as dependent to avoid crashing
20815 under fold_non_dependent_expr; it can't be constant. */
20816 return true;
20818 default:
20819 /* A constant expression is value-dependent if any subexpression is
20820 value-dependent. */
20821 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20823 case tcc_reference:
20824 case tcc_unary:
20825 case tcc_comparison:
20826 case tcc_binary:
20827 case tcc_expression:
20828 case tcc_vl_exp:
20830 int i, len = cp_tree_operand_length (expression);
20832 for (i = 0; i < len; i++)
20834 tree t = TREE_OPERAND (expression, i);
20836 /* In some cases, some of the operands may be missing.l
20837 (For example, in the case of PREDECREMENT_EXPR, the
20838 amount to increment by may be missing.) That doesn't
20839 make the expression dependent. */
20840 if (t && value_dependent_expression_p (t))
20841 return true;
20844 break;
20845 default:
20846 break;
20848 break;
20851 /* The expression is not value-dependent. */
20852 return false;
20855 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20856 [temp.dep.expr]. Note that an expression with no type is
20857 considered dependent. Other parts of the compiler arrange for an
20858 expression with type-dependent subexpressions to have no type, so
20859 this function doesn't have to be fully recursive. */
20861 bool
20862 type_dependent_expression_p (tree expression)
20864 if (!processing_template_decl)
20865 return false;
20867 if (expression == NULL_TREE || expression == error_mark_node)
20868 return false;
20870 /* An unresolved name is always dependent. */
20871 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20872 return true;
20874 /* Some expression forms are never type-dependent. */
20875 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20876 || TREE_CODE (expression) == SIZEOF_EXPR
20877 || TREE_CODE (expression) == ALIGNOF_EXPR
20878 || TREE_CODE (expression) == AT_ENCODE_EXPR
20879 || TREE_CODE (expression) == NOEXCEPT_EXPR
20880 || TREE_CODE (expression) == TRAIT_EXPR
20881 || TREE_CODE (expression) == TYPEID_EXPR
20882 || TREE_CODE (expression) == DELETE_EXPR
20883 || TREE_CODE (expression) == VEC_DELETE_EXPR
20884 || TREE_CODE (expression) == THROW_EXPR)
20885 return false;
20887 /* The types of these expressions depends only on the type to which
20888 the cast occurs. */
20889 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20890 || TREE_CODE (expression) == STATIC_CAST_EXPR
20891 || TREE_CODE (expression) == CONST_CAST_EXPR
20892 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20893 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20894 || TREE_CODE (expression) == CAST_EXPR)
20895 return dependent_type_p (TREE_TYPE (expression));
20897 /* The types of these expressions depends only on the type created
20898 by the expression. */
20899 if (TREE_CODE (expression) == NEW_EXPR
20900 || TREE_CODE (expression) == VEC_NEW_EXPR)
20902 /* For NEW_EXPR tree nodes created inside a template, either
20903 the object type itself or a TREE_LIST may appear as the
20904 operand 1. */
20905 tree type = TREE_OPERAND (expression, 1);
20906 if (TREE_CODE (type) == TREE_LIST)
20907 /* This is an array type. We need to check array dimensions
20908 as well. */
20909 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20910 || value_dependent_expression_p
20911 (TREE_OPERAND (TREE_VALUE (type), 1));
20912 else
20913 return dependent_type_p (type);
20916 if (TREE_CODE (expression) == SCOPE_REF)
20918 tree scope = TREE_OPERAND (expression, 0);
20919 tree name = TREE_OPERAND (expression, 1);
20921 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20922 contains an identifier associated by name lookup with one or more
20923 declarations declared with a dependent type, or...a
20924 nested-name-specifier or qualified-id that names a member of an
20925 unknown specialization. */
20926 return (type_dependent_expression_p (name)
20927 || dependent_scope_p (scope));
20930 if (TREE_CODE (expression) == FUNCTION_DECL
20931 && DECL_LANG_SPECIFIC (expression)
20932 && DECL_TEMPLATE_INFO (expression)
20933 && (any_dependent_template_arguments_p
20934 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20935 return true;
20937 if (TREE_CODE (expression) == TEMPLATE_DECL
20938 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20939 return false;
20941 if (TREE_CODE (expression) == STMT_EXPR)
20942 expression = stmt_expr_value_expr (expression);
20944 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20946 tree elt;
20947 unsigned i;
20949 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20951 if (type_dependent_expression_p (elt))
20952 return true;
20954 return false;
20957 /* A static data member of the current instantiation with incomplete
20958 array type is type-dependent, as the definition and specializations
20959 can have different bounds. */
20960 if (VAR_P (expression)
20961 && DECL_CLASS_SCOPE_P (expression)
20962 && dependent_type_p (DECL_CONTEXT (expression))
20963 && VAR_HAD_UNKNOWN_BOUND (expression))
20964 return true;
20966 /* An array of unknown bound depending on a variadic parameter, eg:
20968 template<typename... Args>
20969 void foo (Args... args)
20971 int arr[] = { args... };
20974 template<int... vals>
20975 void bar ()
20977 int arr[] = { vals... };
20980 If the array has no length and has an initializer, it must be that
20981 we couldn't determine its length in cp_complete_array_type because
20982 it is dependent. */
20983 if (VAR_P (expression)
20984 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20985 && !TYPE_DOMAIN (TREE_TYPE (expression))
20986 && DECL_INITIAL (expression))
20987 return true;
20989 if (TREE_TYPE (expression) == unknown_type_node)
20991 if (TREE_CODE (expression) == ADDR_EXPR)
20992 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20993 if (TREE_CODE (expression) == COMPONENT_REF
20994 || TREE_CODE (expression) == OFFSET_REF)
20996 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20997 return true;
20998 expression = TREE_OPERAND (expression, 1);
20999 if (identifier_p (expression))
21000 return false;
21002 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21003 if (TREE_CODE (expression) == SCOPE_REF)
21004 return false;
21006 /* Always dependent, on the number of arguments if nothing else. */
21007 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21008 return true;
21010 if (BASELINK_P (expression))
21012 if (BASELINK_OPTYPE (expression)
21013 && dependent_type_p (BASELINK_OPTYPE (expression)))
21014 return true;
21015 expression = BASELINK_FUNCTIONS (expression);
21018 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21020 if (any_dependent_template_arguments_p
21021 (TREE_OPERAND (expression, 1)))
21022 return true;
21023 expression = TREE_OPERAND (expression, 0);
21025 gcc_assert (TREE_CODE (expression) == OVERLOAD
21026 || TREE_CODE (expression) == FUNCTION_DECL);
21028 while (expression)
21030 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21031 return true;
21032 expression = OVL_NEXT (expression);
21034 return false;
21037 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21039 return (dependent_type_p (TREE_TYPE (expression)));
21042 /* walk_tree callback function for instantiation_dependent_expression_p,
21043 below. Returns non-zero if a dependent subexpression is found. */
21045 static tree
21046 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21047 void * /*data*/)
21049 if (TYPE_P (*tp))
21051 /* We don't have to worry about decltype currently because decltype
21052 of an instantiation-dependent expr is a dependent type. This
21053 might change depending on the resolution of DR 1172. */
21054 *walk_subtrees = false;
21055 return NULL_TREE;
21057 enum tree_code code = TREE_CODE (*tp);
21058 switch (code)
21060 /* Don't treat an argument list as dependent just because it has no
21061 TREE_TYPE. */
21062 case TREE_LIST:
21063 case TREE_VEC:
21064 return NULL_TREE;
21066 case VAR_DECL:
21067 case CONST_DECL:
21068 /* A constant with a dependent initializer is dependent. */
21069 if (value_dependent_expression_p (*tp))
21070 return *tp;
21071 break;
21073 case TEMPLATE_PARM_INDEX:
21074 return *tp;
21076 /* Handle expressions with type operands. */
21077 case SIZEOF_EXPR:
21078 case ALIGNOF_EXPR:
21079 case TYPEID_EXPR:
21080 case AT_ENCODE_EXPR:
21082 tree op = TREE_OPERAND (*tp, 0);
21083 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21084 op = TREE_TYPE (op);
21085 if (TYPE_P (op))
21087 if (dependent_type_p (op))
21088 return *tp;
21089 else
21091 *walk_subtrees = false;
21092 return NULL_TREE;
21095 break;
21098 case TRAIT_EXPR:
21099 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
21100 || (TRAIT_EXPR_TYPE2 (*tp)
21101 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
21102 return *tp;
21103 *walk_subtrees = false;
21104 return NULL_TREE;
21106 case COMPONENT_REF:
21107 if (identifier_p (TREE_OPERAND (*tp, 1)))
21108 /* In a template, finish_class_member_access_expr creates a
21109 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21110 type-dependent, so that we can check access control at
21111 instantiation time (PR 42277). See also Core issue 1273. */
21112 return *tp;
21113 break;
21115 case SCOPE_REF:
21116 if (instantiation_dependent_scope_ref_p (*tp))
21117 return *tp;
21118 else
21119 break;
21121 /* Treat statement-expressions as dependent. */
21122 case BIND_EXPR:
21123 return *tp;
21125 default:
21126 break;
21129 if (type_dependent_expression_p (*tp))
21130 return *tp;
21131 else
21132 return NULL_TREE;
21135 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21136 sense defined by the ABI:
21138 "An expression is instantiation-dependent if it is type-dependent
21139 or value-dependent, or it has a subexpression that is type-dependent
21140 or value-dependent." */
21142 bool
21143 instantiation_dependent_expression_p (tree expression)
21145 tree result;
21147 if (!processing_template_decl)
21148 return false;
21150 if (expression == error_mark_node)
21151 return false;
21153 result = cp_walk_tree_without_duplicates (&expression,
21154 instantiation_dependent_r, NULL);
21155 return result != NULL_TREE;
21158 /* Like type_dependent_expression_p, but it also works while not processing
21159 a template definition, i.e. during substitution or mangling. */
21161 bool
21162 type_dependent_expression_p_push (tree expr)
21164 bool b;
21165 ++processing_template_decl;
21166 b = type_dependent_expression_p (expr);
21167 --processing_template_decl;
21168 return b;
21171 /* Returns TRUE if ARGS contains a type-dependent expression. */
21173 bool
21174 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21176 unsigned int i;
21177 tree arg;
21179 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21181 if (type_dependent_expression_p (arg))
21182 return true;
21184 return false;
21187 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21188 expressions) contains any type-dependent expressions. */
21190 bool
21191 any_type_dependent_elements_p (const_tree list)
21193 for (; list; list = TREE_CHAIN (list))
21194 if (type_dependent_expression_p (TREE_VALUE (list)))
21195 return true;
21197 return false;
21200 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21201 expressions) contains any value-dependent expressions. */
21203 bool
21204 any_value_dependent_elements_p (const_tree list)
21206 for (; list; list = TREE_CHAIN (list))
21207 if (value_dependent_expression_p (TREE_VALUE (list)))
21208 return true;
21210 return false;
21213 /* Returns TRUE if the ARG (a template argument) is dependent. */
21215 bool
21216 dependent_template_arg_p (tree arg)
21218 if (!processing_template_decl)
21219 return false;
21221 /* Assume a template argument that was wrongly written by the user
21222 is dependent. This is consistent with what
21223 any_dependent_template_arguments_p [that calls this function]
21224 does. */
21225 if (!arg || arg == error_mark_node)
21226 return true;
21228 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21229 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21231 if (TREE_CODE (arg) == TEMPLATE_DECL
21232 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21233 return dependent_template_p (arg);
21234 else if (ARGUMENT_PACK_P (arg))
21236 tree args = ARGUMENT_PACK_ARGS (arg);
21237 int i, len = TREE_VEC_LENGTH (args);
21238 for (i = 0; i < len; ++i)
21240 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21241 return true;
21244 return false;
21246 else if (TYPE_P (arg))
21247 return dependent_type_p (arg);
21248 else
21249 return (type_dependent_expression_p (arg)
21250 || value_dependent_expression_p (arg));
21253 /* Returns true if ARGS (a collection of template arguments) contains
21254 any types that require structural equality testing. */
21256 bool
21257 any_template_arguments_need_structural_equality_p (tree args)
21259 int i;
21260 int j;
21262 if (!args)
21263 return false;
21264 if (args == error_mark_node)
21265 return true;
21267 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21269 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21270 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21272 tree arg = TREE_VEC_ELT (level, j);
21273 tree packed_args = NULL_TREE;
21274 int k, len = 1;
21276 if (ARGUMENT_PACK_P (arg))
21278 /* Look inside the argument pack. */
21279 packed_args = ARGUMENT_PACK_ARGS (arg);
21280 len = TREE_VEC_LENGTH (packed_args);
21283 for (k = 0; k < len; ++k)
21285 if (packed_args)
21286 arg = TREE_VEC_ELT (packed_args, k);
21288 if (error_operand_p (arg))
21289 return true;
21290 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21291 continue;
21292 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21293 return true;
21294 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21295 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21296 return true;
21301 return false;
21304 /* Returns true if ARGS (a collection of template arguments) contains
21305 any dependent arguments. */
21307 bool
21308 any_dependent_template_arguments_p (const_tree args)
21310 int i;
21311 int j;
21313 if (!args)
21314 return false;
21315 if (args == error_mark_node)
21316 return true;
21318 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21320 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21321 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21322 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21323 return true;
21326 return false;
21329 /* Returns TRUE if the template TMPL is dependent. */
21331 bool
21332 dependent_template_p (tree tmpl)
21334 if (TREE_CODE (tmpl) == OVERLOAD)
21336 while (tmpl)
21338 if (dependent_template_p (OVL_CURRENT (tmpl)))
21339 return true;
21340 tmpl = OVL_NEXT (tmpl);
21342 return false;
21345 /* Template template parameters are dependent. */
21346 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21347 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21348 return true;
21349 /* So are names that have not been looked up. */
21350 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21351 return true;
21352 /* So are member templates of dependent classes. */
21353 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21354 return dependent_type_p (DECL_CONTEXT (tmpl));
21355 return false;
21358 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21360 bool
21361 dependent_template_id_p (tree tmpl, tree args)
21363 return (dependent_template_p (tmpl)
21364 || any_dependent_template_arguments_p (args));
21367 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21368 is dependent. */
21370 bool
21371 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21373 int i;
21375 if (!processing_template_decl)
21376 return false;
21378 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21380 tree decl = TREE_VEC_ELT (declv, i);
21381 tree init = TREE_VEC_ELT (initv, i);
21382 tree cond = TREE_VEC_ELT (condv, i);
21383 tree incr = TREE_VEC_ELT (incrv, i);
21385 if (type_dependent_expression_p (decl))
21386 return true;
21388 if (init && type_dependent_expression_p (init))
21389 return true;
21391 if (type_dependent_expression_p (cond))
21392 return true;
21394 if (COMPARISON_CLASS_P (cond)
21395 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21396 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21397 return true;
21399 if (TREE_CODE (incr) == MODOP_EXPR)
21401 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21402 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21403 return true;
21405 else if (type_dependent_expression_p (incr))
21406 return true;
21407 else if (TREE_CODE (incr) == MODIFY_EXPR)
21409 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21410 return true;
21411 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21413 tree t = TREE_OPERAND (incr, 1);
21414 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21415 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21416 return true;
21421 return false;
21424 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21425 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21426 no such TYPE can be found. Note that this function peers inside
21427 uninstantiated templates and therefore should be used only in
21428 extremely limited situations. ONLY_CURRENT_P restricts this
21429 peering to the currently open classes hierarchy (which is required
21430 when comparing types). */
21432 tree
21433 resolve_typename_type (tree type, bool only_current_p)
21435 tree scope;
21436 tree name;
21437 tree decl;
21438 int quals;
21439 tree pushed_scope;
21440 tree result;
21442 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21444 scope = TYPE_CONTEXT (type);
21445 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21446 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21447 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21448 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21449 identifier of the TYPENAME_TYPE anymore.
21450 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21451 TYPENAME_TYPE instead, we avoid messing up with a possible
21452 typedef variant case. */
21453 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21455 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21456 it first before we can figure out what NAME refers to. */
21457 if (TREE_CODE (scope) == TYPENAME_TYPE)
21459 if (TYPENAME_IS_RESOLVING_P (scope))
21460 /* Given a class template A with a dependent base with nested type C,
21461 typedef typename A::C::C C will land us here, as trying to resolve
21462 the initial A::C leads to the local C typedef, which leads back to
21463 A::C::C. So we break the recursion now. */
21464 return type;
21465 else
21466 scope = resolve_typename_type (scope, only_current_p);
21468 /* If we don't know what SCOPE refers to, then we cannot resolve the
21469 TYPENAME_TYPE. */
21470 if (TREE_CODE (scope) == TYPENAME_TYPE)
21471 return type;
21472 /* If the SCOPE is a template type parameter, we have no way of
21473 resolving the name. */
21474 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21475 return type;
21476 /* If the SCOPE is not the current instantiation, there's no reason
21477 to look inside it. */
21478 if (only_current_p && !currently_open_class (scope))
21479 return type;
21480 /* If this is a typedef, we don't want to look inside (c++/11987). */
21481 if (typedef_variant_p (type))
21482 return type;
21483 /* If SCOPE isn't the template itself, it will not have a valid
21484 TYPE_FIELDS list. */
21485 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21486 /* scope is either the template itself or a compatible instantiation
21487 like X<T>, so look up the name in the original template. */
21488 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21489 else
21490 /* scope is a partial instantiation, so we can't do the lookup or we
21491 will lose the template arguments. */
21492 return type;
21493 /* Enter the SCOPE so that name lookup will be resolved as if we
21494 were in the class definition. In particular, SCOPE will no
21495 longer be considered a dependent type. */
21496 pushed_scope = push_scope (scope);
21497 /* Look up the declaration. */
21498 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21499 tf_warning_or_error);
21501 result = NULL_TREE;
21503 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21504 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21505 if (!decl)
21506 /*nop*/;
21507 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21508 && TREE_CODE (decl) == TYPE_DECL)
21510 result = TREE_TYPE (decl);
21511 if (result == error_mark_node)
21512 result = NULL_TREE;
21514 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21515 && DECL_CLASS_TEMPLATE_P (decl))
21517 tree tmpl;
21518 tree args;
21519 /* Obtain the template and the arguments. */
21520 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21521 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21522 /* Instantiate the template. */
21523 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21524 /*entering_scope=*/0,
21525 tf_error | tf_user);
21526 if (result == error_mark_node)
21527 result = NULL_TREE;
21530 /* Leave the SCOPE. */
21531 if (pushed_scope)
21532 pop_scope (pushed_scope);
21534 /* If we failed to resolve it, return the original typename. */
21535 if (!result)
21536 return type;
21538 /* If lookup found a typename type, resolve that too. */
21539 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21541 /* Ill-formed programs can cause infinite recursion here, so we
21542 must catch that. */
21543 TYPENAME_IS_RESOLVING_P (type) = 1;
21544 result = resolve_typename_type (result, only_current_p);
21545 TYPENAME_IS_RESOLVING_P (type) = 0;
21548 /* Qualify the resulting type. */
21549 quals = cp_type_quals (type);
21550 if (quals)
21551 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21553 return result;
21556 /* EXPR is an expression which is not type-dependent. Return a proxy
21557 for EXPR that can be used to compute the types of larger
21558 expressions containing EXPR. */
21560 tree
21561 build_non_dependent_expr (tree expr)
21563 tree inner_expr;
21565 #ifdef ENABLE_CHECKING
21566 /* Try to get a constant value for all non-dependent expressions in
21567 order to expose bugs in *_dependent_expression_p and constexpr. */
21568 if (cxx_dialect >= cxx11)
21569 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21570 #endif
21572 /* Preserve OVERLOADs; the functions must be available to resolve
21573 types. */
21574 inner_expr = expr;
21575 if (TREE_CODE (inner_expr) == STMT_EXPR)
21576 inner_expr = stmt_expr_value_expr (inner_expr);
21577 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21578 inner_expr = TREE_OPERAND (inner_expr, 0);
21579 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21580 inner_expr = TREE_OPERAND (inner_expr, 1);
21581 if (is_overloaded_fn (inner_expr)
21582 || TREE_CODE (inner_expr) == OFFSET_REF)
21583 return expr;
21584 /* There is no need to return a proxy for a variable. */
21585 if (VAR_P (expr))
21586 return expr;
21587 /* Preserve string constants; conversions from string constants to
21588 "char *" are allowed, even though normally a "const char *"
21589 cannot be used to initialize a "char *". */
21590 if (TREE_CODE (expr) == STRING_CST)
21591 return expr;
21592 /* Preserve arithmetic constants, as an optimization -- there is no
21593 reason to create a new node. */
21594 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21595 return expr;
21596 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21597 There is at least one place where we want to know that a
21598 particular expression is a throw-expression: when checking a ?:
21599 expression, there are special rules if the second or third
21600 argument is a throw-expression. */
21601 if (TREE_CODE (expr) == THROW_EXPR)
21602 return expr;
21604 /* Don't wrap an initializer list, we need to be able to look inside. */
21605 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21606 return expr;
21608 /* Don't wrap a dummy object, we need to be able to test for it. */
21609 if (is_dummy_object (expr))
21610 return expr;
21612 if (TREE_CODE (expr) == COND_EXPR)
21613 return build3 (COND_EXPR,
21614 TREE_TYPE (expr),
21615 TREE_OPERAND (expr, 0),
21616 (TREE_OPERAND (expr, 1)
21617 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21618 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21619 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21620 if (TREE_CODE (expr) == COMPOUND_EXPR
21621 && !COMPOUND_EXPR_OVERLOADED (expr))
21622 return build2 (COMPOUND_EXPR,
21623 TREE_TYPE (expr),
21624 TREE_OPERAND (expr, 0),
21625 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21627 /* If the type is unknown, it can't really be non-dependent */
21628 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21630 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21631 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21634 /* ARGS is a vector of expressions as arguments to a function call.
21635 Replace the arguments with equivalent non-dependent expressions.
21636 This modifies ARGS in place. */
21638 void
21639 make_args_non_dependent (vec<tree, va_gc> *args)
21641 unsigned int ix;
21642 tree arg;
21644 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21646 tree newarg = build_non_dependent_expr (arg);
21647 if (newarg != arg)
21648 (*args)[ix] = newarg;
21652 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21653 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21654 parms. */
21656 static tree
21657 make_auto_1 (tree name)
21659 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21660 TYPE_NAME (au) = build_decl (input_location,
21661 TYPE_DECL, name, au);
21662 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21663 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21664 (0, processing_template_decl + 1, processing_template_decl + 1,
21665 TYPE_NAME (au), NULL_TREE);
21666 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21667 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21668 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21670 return au;
21673 tree
21674 make_decltype_auto (void)
21676 return make_auto_1 (get_identifier ("decltype(auto)"));
21679 tree
21680 make_auto (void)
21682 return make_auto_1 (get_identifier ("auto"));
21685 /* Given type ARG, return std::initializer_list<ARG>. */
21687 static tree
21688 listify (tree arg)
21690 tree std_init_list = namespace_binding
21691 (get_identifier ("initializer_list"), std_node);
21692 tree argvec;
21693 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21695 error ("deducing from brace-enclosed initializer list requires "
21696 "#include <initializer_list>");
21697 return error_mark_node;
21699 argvec = make_tree_vec (1);
21700 TREE_VEC_ELT (argvec, 0) = arg;
21701 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21702 NULL_TREE, 0, tf_warning_or_error);
21705 /* Replace auto in TYPE with std::initializer_list<auto>. */
21707 static tree
21708 listify_autos (tree type, tree auto_node)
21710 tree init_auto = listify (auto_node);
21711 tree argvec = make_tree_vec (1);
21712 TREE_VEC_ELT (argvec, 0) = init_auto;
21713 if (processing_template_decl)
21714 argvec = add_to_template_args (current_template_args (), argvec);
21715 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21718 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21719 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21721 tree
21722 do_auto_deduction (tree type, tree init, tree auto_node)
21724 tree targs;
21726 if (init == error_mark_node)
21727 return error_mark_node;
21729 if (type_dependent_expression_p (init))
21730 /* Defining a subset of type-dependent expressions that we can deduce
21731 from ahead of time isn't worth the trouble. */
21732 return type;
21734 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21735 with either a new invented type template parameter U or, if the
21736 initializer is a braced-init-list (8.5.4), with
21737 std::initializer_list<U>. */
21738 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21739 type = listify_autos (type, auto_node);
21741 init = resolve_nondeduced_context (init);
21743 targs = make_tree_vec (1);
21744 if (AUTO_IS_DECLTYPE (auto_node))
21746 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
21747 && !REF_PARENTHESIZED_P (init)));
21748 TREE_VEC_ELT (targs, 0)
21749 = finish_decltype_type (init, id, tf_warning_or_error);
21750 if (type != auto_node)
21752 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21753 return error_mark_node;
21756 else
21758 tree parms = build_tree_list (NULL_TREE, type);
21759 tree tparms = make_tree_vec (1);
21760 int val;
21762 TREE_VEC_ELT (tparms, 0)
21763 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21764 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21765 DEDUCE_CALL, LOOKUP_NORMAL,
21766 NULL, /*explain_p=*/false);
21767 if (val > 0)
21769 if (processing_template_decl)
21770 /* Try again at instantiation time. */
21771 return type;
21772 if (type && type != error_mark_node)
21773 /* If type is error_mark_node a diagnostic must have been
21774 emitted by now. Also, having a mention to '<type error>'
21775 in the diagnostic is not really useful to the user. */
21777 if (cfun && auto_node == current_function_auto_return_pattern
21778 && LAMBDA_FUNCTION_P (current_function_decl))
21779 error ("unable to deduce lambda return type from %qE", init);
21780 else
21781 error ("unable to deduce %qT from %qE", type, init);
21783 return error_mark_node;
21787 /* If the list of declarators contains more than one declarator, the type
21788 of each declared variable is determined as described above. If the
21789 type deduced for the template parameter U is not the same in each
21790 deduction, the program is ill-formed. */
21791 if (TREE_TYPE (auto_node)
21792 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21794 if (cfun && auto_node == current_function_auto_return_pattern
21795 && LAMBDA_FUNCTION_P (current_function_decl))
21796 error ("inconsistent types %qT and %qT deduced for "
21797 "lambda return type", TREE_TYPE (auto_node),
21798 TREE_VEC_ELT (targs, 0));
21799 else
21800 error ("inconsistent deduction for %qT: %qT and then %qT",
21801 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21802 return error_mark_node;
21804 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21806 if (processing_template_decl)
21807 targs = add_to_template_args (current_template_args (), targs);
21808 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21811 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21812 result. */
21814 tree
21815 splice_late_return_type (tree type, tree late_return_type)
21817 tree argvec;
21819 if (late_return_type == NULL_TREE)
21820 return type;
21821 argvec = make_tree_vec (1);
21822 TREE_VEC_ELT (argvec, 0) = late_return_type;
21823 if (processing_template_parmlist)
21824 /* For a late-specified return type in a template type-parameter, we
21825 need to add a dummy argument level for its parmlist. */
21826 argvec = add_to_template_args
21827 (make_tree_vec (processing_template_parmlist), argvec);
21828 if (current_template_parms)
21829 argvec = add_to_template_args (current_template_args (), argvec);
21830 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21833 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21834 'decltype(auto)'. */
21836 bool
21837 is_auto (const_tree type)
21839 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21840 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21841 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21842 return true;
21843 else
21844 return false;
21847 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21848 a use of `auto'. Returns NULL_TREE otherwise. */
21850 tree
21851 type_uses_auto (tree type)
21853 return find_type_usage (type, is_auto);
21856 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21857 'decltype(auto)' or a concept. */
21859 bool
21860 is_auto_or_concept (const_tree type)
21862 return is_auto (type); // or concept
21865 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21866 a concept identifier) iff TYPE contains a use of a generic type. Returns
21867 NULL_TREE otherwise. */
21869 tree
21870 type_uses_auto_or_concept (tree type)
21872 return find_type_usage (type, is_auto_or_concept);
21876 /* For a given template T, return the vector of typedefs referenced
21877 in T for which access check is needed at T instantiation time.
21878 T is either a FUNCTION_DECL or a RECORD_TYPE.
21879 Those typedefs were added to T by the function
21880 append_type_to_template_for_access_check. */
21882 vec<qualified_typedef_usage_t, va_gc> *
21883 get_types_needing_access_check (tree t)
21885 tree ti;
21886 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21888 if (!t || t == error_mark_node)
21889 return NULL;
21891 if (!(ti = get_template_info (t)))
21892 return NULL;
21894 if (CLASS_TYPE_P (t)
21895 || TREE_CODE (t) == FUNCTION_DECL)
21897 if (!TI_TEMPLATE (ti))
21898 return NULL;
21900 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21903 return result;
21906 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21907 tied to T. That list of typedefs will be access checked at
21908 T instantiation time.
21909 T is either a FUNCTION_DECL or a RECORD_TYPE.
21910 TYPE_DECL is a TYPE_DECL node representing a typedef.
21911 SCOPE is the scope through which TYPE_DECL is accessed.
21912 LOCATION is the location of the usage point of TYPE_DECL.
21914 This function is a subroutine of
21915 append_type_to_template_for_access_check. */
21917 static void
21918 append_type_to_template_for_access_check_1 (tree t,
21919 tree type_decl,
21920 tree scope,
21921 location_t location)
21923 qualified_typedef_usage_t typedef_usage;
21924 tree ti;
21926 if (!t || t == error_mark_node)
21927 return;
21929 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21930 || CLASS_TYPE_P (t))
21931 && type_decl
21932 && TREE_CODE (type_decl) == TYPE_DECL
21933 && scope);
21935 if (!(ti = get_template_info (t)))
21936 return;
21938 gcc_assert (TI_TEMPLATE (ti));
21940 typedef_usage.typedef_decl = type_decl;
21941 typedef_usage.context = scope;
21942 typedef_usage.locus = location;
21944 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21947 /* Append TYPE_DECL to the template TEMPL.
21948 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21949 At TEMPL instanciation time, TYPE_DECL will be checked to see
21950 if it can be accessed through SCOPE.
21951 LOCATION is the location of the usage point of TYPE_DECL.
21953 e.g. consider the following code snippet:
21955 class C
21957 typedef int myint;
21960 template<class U> struct S
21962 C::myint mi; // <-- usage point of the typedef C::myint
21965 S<char> s;
21967 At S<char> instantiation time, we need to check the access of C::myint
21968 In other words, we need to check the access of the myint typedef through
21969 the C scope. For that purpose, this function will add the myint typedef
21970 and the scope C through which its being accessed to a list of typedefs
21971 tied to the template S. That list will be walked at template instantiation
21972 time and access check performed on each typedefs it contains.
21973 Note that this particular code snippet should yield an error because
21974 myint is private to C. */
21976 void
21977 append_type_to_template_for_access_check (tree templ,
21978 tree type_decl,
21979 tree scope,
21980 location_t location)
21982 qualified_typedef_usage_t *iter;
21983 unsigned i;
21985 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21987 /* Make sure we don't append the type to the template twice. */
21988 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21989 if (iter->typedef_decl == type_decl && scope == iter->context)
21990 return;
21992 append_type_to_template_for_access_check_1 (templ, type_decl,
21993 scope, location);
21996 /* Convert the generic type parameters in PARM that match the types given in the
21997 range [START_IDX, END_IDX) from the current_template_parms into generic type
21998 packs. */
22000 tree
22001 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22003 tree current = current_template_parms;
22004 int depth = TMPL_PARMS_DEPTH (current);
22005 current = INNERMOST_TEMPLATE_PARMS (current);
22006 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22008 for (int i = 0; i < start_idx; ++i)
22009 TREE_VEC_ELT (replacement, i)
22010 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22012 for (int i = start_idx; i < end_idx; ++i)
22014 /* Create a distinct parameter pack type from the current parm and add it
22015 to the replacement args to tsubst below into the generic function
22016 parameter. */
22018 tree o = TREE_TYPE (TREE_VALUE
22019 (TREE_VEC_ELT (current, i)));
22020 tree t = copy_type (o);
22021 TEMPLATE_TYPE_PARM_INDEX (t)
22022 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22023 o, 0, 0, tf_none);
22024 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22025 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22026 TYPE_MAIN_VARIANT (t) = t;
22027 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22028 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22029 TREE_VEC_ELT (replacement, i) = t;
22030 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22033 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22034 TREE_VEC_ELT (replacement, i)
22035 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22037 /* If there are more levels then build up the replacement with the outer
22038 template parms. */
22039 if (depth > 1)
22040 replacement = add_to_template_args (template_parms_to_args
22041 (TREE_CHAIN (current_template_parms)),
22042 replacement);
22044 return tsubst (parm, replacement, tf_none, NULL_TREE);
22048 /* Set up the hash tables for template instantiations. */
22050 void
22051 init_template_processing (void)
22053 decl_specializations = htab_create_ggc (37,
22054 hash_specialization,
22055 eq_specializations,
22056 ggc_free);
22057 type_specializations = htab_create_ggc (37,
22058 hash_specialization,
22059 eq_specializations,
22060 ggc_free);
22063 /* Print stats about the template hash tables for -fstats. */
22065 void
22066 print_template_statistics (void)
22068 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22069 "%f collisions\n", (long) htab_size (decl_specializations),
22070 (long) htab_elements (decl_specializations),
22071 htab_collisions (decl_specializations));
22072 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22073 "%f collisions\n", (long) htab_size (type_specializations),
22074 (long) htab_elements (type_specializations),
22075 htab_collisions (type_specializations));
22078 #include "gt-cp-pt.h"