2013-11-01 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / cp / pt.c
blob8c1553feca20c7715c868fe45e2e2e723907d0a6
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "intl.h"
33 #include "pointer-set.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "toplev.h"
42 #include "timevar.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template {
54 struct pending_template *next;
55 struct tinst_level *tinst;
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
61 int processing_template_parmlist;
62 static int template_header_count;
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
67 static GTY(()) struct tinst_level *current_tinst_level;
69 static GTY(()) tree saved_access_scope;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
76 /* A map from local variable declarations in the body of the template
77 presently being instantiated to the corresponding instantiated
78 local variables. */
79 static struct pointer_map_t *local_specializations;
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);
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, 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 (DECL_P (t) && DECL_LANG_SPECIFIC (t))
324 tinfo = DECL_TEMPLATE_INFO (t);
326 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
327 t = TREE_TYPE (t);
329 if (OVERLOAD_TYPE_P (t))
330 tinfo = TYPE_TEMPLATE_INFO (t);
331 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
332 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
334 return tinfo;
337 /* Returns the template nesting level of the indicated class TYPE.
339 For example, in:
340 template <class T>
341 struct A
343 template <class U>
344 struct B {};
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
353 always safe. */
356 template_class_depth (tree type)
358 int depth;
360 for (depth = 0;
361 type && TREE_CODE (type) != NAMESPACE_DECL;
362 type = (TREE_CODE (type) == FUNCTION_DECL)
363 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
365 tree tinfo = get_template_info (type);
367 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
369 ++depth;
372 return depth;
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
378 static bool
379 inline_needs_template_parms (tree decl)
381 if (! DECL_TEMPLATE_INFO (decl))
382 return false;
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
385 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
391 innermost first. */
393 static void
394 push_inline_template_parms_recursive (tree parmlist, int levels)
396 tree parms = TREE_VALUE (parmlist);
397 int i;
399 if (levels > 1)
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
402 ++processing_template_decl;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl),
405 parms, current_template_parms);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
408 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
409 NULL);
410 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
412 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
414 if (parm == error_mark_node)
415 continue;
417 gcc_assert (DECL_P (parm));
419 switch (TREE_CODE (parm))
421 case TYPE_DECL:
422 case TEMPLATE_DECL:
423 pushdecl (parm);
424 break;
426 case PARM_DECL:
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
431 available. */
432 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
433 CONST_DECL, DECL_NAME (parm),
434 TREE_TYPE (parm));
435 DECL_ARTIFICIAL (decl) = 1;
436 TREE_CONSTANT (decl) = 1;
437 TREE_READONLY (decl) = 1;
438 DECL_INITIAL (decl) = DECL_INITIAL (parm);
439 SET_DECL_TEMPLATE_PARM_P (decl);
440 pushdecl (decl);
442 break;
444 default:
445 gcc_unreachable ();
450 /* Restore the template parameter context for a member template or
451 a friend template defined in a class definition. */
453 void
454 maybe_begin_member_template_processing (tree decl)
456 tree parms;
457 int levels = 0;
459 if (inline_needs_template_parms (decl))
461 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
462 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
464 if (DECL_TEMPLATE_SPECIALIZATION (decl))
466 --levels;
467 parms = TREE_CHAIN (parms);
470 push_inline_template_parms_recursive (parms, levels);
473 /* Remember how many levels of template parameters we pushed so that
474 we can pop them later. */
475 inline_parm_levels.safe_push (levels);
478 /* Undo the effects of maybe_begin_member_template_processing. */
480 void
481 maybe_end_member_template_processing (void)
483 int i;
484 int last;
486 if (inline_parm_levels.length () == 0)
487 return;
489 last = inline_parm_levels.pop ();
490 for (i = 0; i < last; ++i)
492 --processing_template_decl;
493 current_template_parms = TREE_CHAIN (current_template_parms);
494 poplevel (0, 0, 0);
498 /* Return a new template argument vector which contains all of ARGS,
499 but has as its innermost set of arguments the EXTRA_ARGS. */
501 static tree
502 add_to_template_args (tree args, tree extra_args)
504 tree new_args;
505 int extra_depth;
506 int i;
507 int j;
509 if (args == NULL_TREE || extra_args == error_mark_node)
510 return extra_args;
512 extra_depth = TMPL_ARGS_DEPTH (extra_args);
513 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
515 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
516 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
518 for (j = 1; j <= extra_depth; ++j, ++i)
519 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
521 return new_args;
524 /* Like add_to_template_args, but only the outermost ARGS are added to
525 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
526 (EXTRA_ARGS) levels are added. This function is used to combine
527 the template arguments from a partial instantiation with the
528 template arguments used to attain the full instantiation from the
529 partial instantiation. */
531 static tree
532 add_outermost_template_args (tree args, tree extra_args)
534 tree new_args;
536 /* If there are more levels of EXTRA_ARGS than there are ARGS,
537 something very fishy is going on. */
538 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
540 /* If *all* the new arguments will be the EXTRA_ARGS, just return
541 them. */
542 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
543 return extra_args;
545 /* For the moment, we make ARGS look like it contains fewer levels. */
546 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
548 new_args = add_to_template_args (args, extra_args);
550 /* Now, we restore ARGS to its full dimensions. */
551 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
553 return new_args;
556 /* Return the N levels of innermost template arguments from the ARGS. */
558 tree
559 get_innermost_template_args (tree args, int n)
561 tree new_args;
562 int extra_levels;
563 int i;
565 gcc_assert (n >= 0);
567 /* If N is 1, just return the innermost set of template arguments. */
568 if (n == 1)
569 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
571 /* If we're not removing anything, just return the arguments we were
572 given. */
573 extra_levels = TMPL_ARGS_DEPTH (args) - n;
574 gcc_assert (extra_levels >= 0);
575 if (extra_levels == 0)
576 return args;
578 /* Make a new set of arguments, not containing the outer arguments. */
579 new_args = make_tree_vec (n);
580 for (i = 1; i <= n; ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i,
582 TMPL_ARGS_LEVEL (args, i + extra_levels));
584 return new_args;
587 /* The inverse of get_innermost_template_args: Return all but the innermost
588 EXTRA_LEVELS levels of template arguments from the ARGS. */
590 static tree
591 strip_innermost_template_args (tree args, int extra_levels)
593 tree new_args;
594 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
595 int i;
597 gcc_assert (n >= 0);
599 /* If N is 1, just return the outermost set of template arguments. */
600 if (n == 1)
601 return TMPL_ARGS_LEVEL (args, 1);
603 /* If we're not removing anything, just return the arguments we were
604 given. */
605 gcc_assert (extra_levels >= 0);
606 if (extra_levels == 0)
607 return args;
609 /* Make a new set of arguments, not containing the inner arguments. */
610 new_args = make_tree_vec (n);
611 for (i = 1; i <= n; ++i)
612 SET_TMPL_ARGS_LEVEL (new_args, i,
613 TMPL_ARGS_LEVEL (args, i));
615 return new_args;
618 /* We've got a template header coming up; push to a new level for storing
619 the parms. */
621 void
622 begin_template_parm_list (void)
624 /* We use a non-tag-transparent scope here, which causes pushtag to
625 put tags in this scope, rather than in the enclosing class or
626 namespace scope. This is the right thing, since we want
627 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
628 global template class, push_template_decl handles putting the
629 TEMPLATE_DECL into top-level scope. For a nested template class,
630 e.g.:
632 template <class T> struct S1 {
633 template <class T> struct S2 {};
636 pushtag contains special code to call pushdecl_with_scope on the
637 TEMPLATE_DECL for S2. */
638 begin_scope (sk_template_parms, NULL);
639 ++processing_template_decl;
640 ++processing_template_parmlist;
641 note_template_header (0);
644 /* This routine is called when a specialization is declared. If it is
645 invalid to declare a specialization here, an error is reported and
646 false is returned, otherwise this routine will return true. */
648 static bool
649 check_specialization_scope (void)
651 tree scope = current_scope ();
653 /* [temp.expl.spec]
655 An explicit specialization shall be declared in the namespace of
656 which the template is a member, or, for member templates, in the
657 namespace of which the enclosing class or enclosing class
658 template is a member. An explicit specialization of a member
659 function, member class or static data member of a class template
660 shall be declared in the namespace of which the class template
661 is a member. */
662 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
664 error ("explicit specialization in non-namespace scope %qD", scope);
665 return false;
668 /* [temp.expl.spec]
670 In an explicit specialization declaration for a member of a class
671 template or a member template that appears in namespace scope,
672 the member template and some of its enclosing class templates may
673 remain unspecialized, except that the declaration shall not
674 explicitly specialize a class member template if its enclosing
675 class templates are not explicitly specialized as well. */
676 if (current_template_parms)
678 error ("enclosing class templates are not explicitly specialized");
679 return false;
682 return true;
685 /* We've just seen template <>. */
687 bool
688 begin_specialization (void)
690 begin_scope (sk_template_spec, NULL);
691 note_template_header (1);
692 return check_specialization_scope ();
695 /* Called at then end of processing a declaration preceded by
696 template<>. */
698 void
699 end_specialization (void)
701 finish_scope ();
702 reset_specialization ();
705 /* Any template <>'s that we have seen thus far are not referring to a
706 function specialization. */
708 void
709 reset_specialization (void)
711 processing_specialization = 0;
712 template_header_count = 0;
715 /* We've just seen a template header. If SPECIALIZATION is nonzero,
716 it was of the form template <>. */
718 static void
719 note_template_header (int specialization)
721 processing_specialization = specialization;
722 template_header_count++;
725 /* We're beginning an explicit instantiation. */
727 void
728 begin_explicit_instantiation (void)
730 gcc_assert (!processing_explicit_instantiation);
731 processing_explicit_instantiation = true;
735 void
736 end_explicit_instantiation (void)
738 gcc_assert (processing_explicit_instantiation);
739 processing_explicit_instantiation = false;
742 /* An explicit specialization or partial specialization of TMPL is being
743 declared. Check that the namespace in which the specialization is
744 occurring is permissible. Returns false iff it is invalid to
745 specialize TMPL in the current namespace. */
747 static bool
748 check_specialization_namespace (tree tmpl)
750 tree tpl_ns = decl_namespace_context (tmpl);
752 /* [tmpl.expl.spec]
754 An explicit specialization shall be declared in the namespace of
755 which the template is a member, or, for member templates, in the
756 namespace of which the enclosing class or enclosing class
757 template is a member. An explicit specialization of a member
758 function, member class or static data member of a class template
759 shall be declared in the namespace of which the class template is
760 a member. */
761 if (current_scope() != DECL_CONTEXT (tmpl)
762 && !at_namespace_scope_p ())
764 error ("specialization of %qD must appear at namespace scope", tmpl);
765 return false;
767 if (is_associated_namespace (current_namespace, tpl_ns))
768 /* Same or super-using namespace. */
769 return true;
770 else
772 permerror (input_location, "specialization of %qD in different namespace", tmpl);
773 permerror (input_location, " from definition of %q+#D", tmpl);
774 return false;
778 /* SPEC is an explicit instantiation. Check that it is valid to
779 perform this explicit instantiation in the current namespace. */
781 static void
782 check_explicit_instantiation_namespace (tree spec)
784 tree ns;
786 /* DR 275: An explicit instantiation shall appear in an enclosing
787 namespace of its template. */
788 ns = decl_namespace_context (spec);
789 if (!is_ancestor (current_namespace, ns))
790 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
791 "(which does not enclose namespace %qD)",
792 spec, current_namespace, ns);
795 /* The TYPE is being declared. If it is a template type, that means it
796 is a partial specialization. Do appropriate error-checking. */
798 tree
799 maybe_process_partial_specialization (tree type)
801 tree context;
803 if (type == error_mark_node)
804 return error_mark_node;
806 /* A lambda that appears in specialization context is not itself a
807 specialization. */
808 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
809 return type;
811 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
813 error ("name of class shadows template template parameter %qD",
814 TYPE_NAME (type));
815 return error_mark_node;
818 context = TYPE_CONTEXT (type);
820 if (TYPE_ALIAS_P (type))
822 if (TYPE_TEMPLATE_INFO (type)
823 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
824 error ("specialization of alias template %qD",
825 TYPE_TI_TEMPLATE (type));
826 else
827 error ("explicit specialization of non-template %qT", type);
828 return error_mark_node;
830 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
832 /* This is for ordinary explicit specialization and partial
833 specialization of a template class such as:
835 template <> class C<int>;
839 template <class T> class C<T*>;
841 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
843 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
844 && !COMPLETE_TYPE_P (type))
846 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
847 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
848 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
849 if (processing_template_decl)
851 if (push_template_decl (TYPE_MAIN_DECL (type))
852 == error_mark_node)
853 return error_mark_node;
856 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
857 error ("specialization of %qT after instantiation", type);
858 else if (errorcount && !processing_specialization
859 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
860 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
861 /* Trying to define a specialization either without a template<> header
862 or in an inappropriate place. We've already given an error, so just
863 bail now so we don't actually define the specialization. */
864 return error_mark_node;
866 else if (CLASS_TYPE_P (type)
867 && !CLASSTYPE_USE_TEMPLATE (type)
868 && CLASSTYPE_TEMPLATE_INFO (type)
869 && context && CLASS_TYPE_P (context)
870 && CLASSTYPE_TEMPLATE_INFO (context))
872 /* This is for an explicit specialization of member class
873 template according to [temp.expl.spec/18]:
875 template <> template <class U> class C<int>::D;
877 The context `C<int>' must be an implicit instantiation.
878 Otherwise this is just a member class template declared
879 earlier like:
881 template <> class C<int> { template <class U> class D; };
882 template <> template <class U> class C<int>::D;
884 In the first case, `C<int>::D' is a specialization of `C<T>::D'
885 while in the second case, `C<int>::D' is a primary template
886 and `C<T>::D' may not exist. */
888 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
889 && !COMPLETE_TYPE_P (type))
891 tree t;
892 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
894 if (current_namespace
895 != decl_namespace_context (tmpl))
897 permerror (input_location, "specializing %q#T in different namespace", type);
898 permerror (input_location, " from definition of %q+#D", tmpl);
901 /* Check for invalid specialization after instantiation:
903 template <> template <> class C<int>::D<int>;
904 template <> template <class U> class C<int>::D; */
906 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
907 t; t = TREE_CHAIN (t))
909 tree inst = TREE_VALUE (t);
910 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
912 /* We already have a full specialization of this partial
913 instantiation. Reassign it to the new member
914 specialization template. */
915 spec_entry elt;
916 spec_entry *entry;
917 void **slot;
919 elt.tmpl = most_general_template (tmpl);
920 elt.args = CLASSTYPE_TI_ARGS (inst);
921 elt.spec = inst;
923 htab_remove_elt (type_specializations, &elt);
925 elt.tmpl = tmpl;
926 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
928 slot = htab_find_slot (type_specializations, &elt, INSERT);
929 entry = ggc_alloc_spec_entry ();
930 *entry = elt;
931 *slot = entry;
933 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
934 /* But if we've had an implicit instantiation, that's a
935 problem ([temp.expl.spec]/6). */
936 error ("specialization %qT after instantiation %qT",
937 type, inst);
940 /* Mark TYPE as a specialization. And as a result, we only
941 have one level of template argument for the innermost
942 class template. */
943 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
944 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
945 CLASSTYPE_TI_ARGS (type)
946 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
949 else if (processing_specialization)
951 /* Someday C++0x may allow for enum template specialization. */
952 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
953 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
954 pedwarn (input_location, OPT_Wpedantic, "template specialization "
955 "of %qD not allowed by ISO C++", type);
956 else
958 error ("explicit specialization of non-template %qT", type);
959 return error_mark_node;
963 return type;
966 /* Returns nonzero if we can optimize the retrieval of specializations
967 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
968 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
970 static inline bool
971 optimize_specialization_lookup_p (tree tmpl)
973 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
974 && DECL_CLASS_SCOPE_P (tmpl)
975 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
976 parameter. */
977 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
978 /* The optimized lookup depends on the fact that the
979 template arguments for the member function template apply
980 purely to the containing class, which is not true if the
981 containing class is an explicit or partial
982 specialization. */
983 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
984 && !DECL_MEMBER_TEMPLATE_P (tmpl)
985 && !DECL_CONV_FN_P (tmpl)
986 /* It is possible to have a template that is not a member
987 template and is not a member of a template class:
989 template <typename T>
990 struct S { friend A::f(); };
992 Here, the friend function is a template, but the context does
993 not have template information. The optimized lookup relies
994 on having ARGS be the template arguments for both the class
995 and the function template. */
996 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
999 /* Retrieve the specialization (in the sense of [temp.spec] - a
1000 specialization is either an instantiation or an explicit
1001 specialization) of TMPL for the given template ARGS. If there is
1002 no such specialization, return NULL_TREE. The ARGS are a vector of
1003 arguments, or a vector of vectors of arguments, in the case of
1004 templates with more than one level of parameters.
1006 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1007 then we search for a partial specialization matching ARGS. This
1008 parameter is ignored if TMPL is not a class template.
1010 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1011 result is a NONTYPE_ARGUMENT_PACK. */
1013 static tree
1014 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1016 if (tmpl == NULL_TREE)
1017 return NULL_TREE;
1019 if (args == error_mark_node)
1020 return NULL_TREE;
1022 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1023 || TREE_CODE (tmpl) == FIELD_DECL);
1025 /* There should be as many levels of arguments as there are
1026 levels of parameters. */
1027 gcc_assert (TMPL_ARGS_DEPTH (args)
1028 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1029 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1030 : template_class_depth (DECL_CONTEXT (tmpl))));
1032 if (optimize_specialization_lookup_p (tmpl))
1034 tree class_template;
1035 tree class_specialization;
1036 vec<tree, va_gc> *methods;
1037 tree fns;
1038 int idx;
1040 /* The template arguments actually apply to the containing
1041 class. Find the class specialization with those
1042 arguments. */
1043 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1044 class_specialization
1045 = retrieve_specialization (class_template, args, 0);
1046 if (!class_specialization)
1047 return NULL_TREE;
1048 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1049 for the specialization. */
1050 idx = class_method_index_for_fn (class_specialization, tmpl);
1051 if (idx == -1)
1052 return NULL_TREE;
1053 /* Iterate through the methods with the indicated name, looking
1054 for the one that has an instance of TMPL. */
1055 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1056 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1058 tree fn = OVL_CURRENT (fns);
1059 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1060 /* using-declarations can add base methods to the method vec,
1061 and we don't want those here. */
1062 && DECL_CONTEXT (fn) == class_specialization)
1063 return fn;
1065 return NULL_TREE;
1067 else
1069 spec_entry *found;
1070 spec_entry elt;
1071 htab_t specializations;
1073 elt.tmpl = tmpl;
1074 elt.args = args;
1075 elt.spec = NULL_TREE;
1077 if (DECL_CLASS_TEMPLATE_P (tmpl))
1078 specializations = type_specializations;
1079 else
1080 specializations = decl_specializations;
1082 if (hash == 0)
1083 hash = hash_specialization (&elt);
1084 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1085 if (found)
1086 return found->spec;
1089 return NULL_TREE;
1092 /* Like retrieve_specialization, but for local declarations. */
1094 static tree
1095 retrieve_local_specialization (tree tmpl)
1097 void **slot;
1099 if (local_specializations == NULL)
1100 return NULL_TREE;
1102 slot = pointer_map_contains (local_specializations, tmpl);
1103 return slot ? (tree) *slot : NULL_TREE;
1106 /* Returns nonzero iff DECL is a specialization of TMPL. */
1109 is_specialization_of (tree decl, tree tmpl)
1111 tree t;
1113 if (TREE_CODE (decl) == FUNCTION_DECL)
1115 for (t = decl;
1116 t != NULL_TREE;
1117 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1118 if (t == tmpl)
1119 return 1;
1121 else
1123 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1125 for (t = TREE_TYPE (decl);
1126 t != NULL_TREE;
1127 t = CLASSTYPE_USE_TEMPLATE (t)
1128 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1129 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1130 return 1;
1133 return 0;
1136 /* Returns nonzero iff DECL is a specialization of friend declaration
1137 FRIEND_DECL according to [temp.friend]. */
1139 bool
1140 is_specialization_of_friend (tree decl, tree friend_decl)
1142 bool need_template = true;
1143 int template_depth;
1145 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1146 || TREE_CODE (decl) == TYPE_DECL);
1148 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1149 of a template class, we want to check if DECL is a specialization
1150 if this. */
1151 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1152 && DECL_TEMPLATE_INFO (friend_decl)
1153 && !DECL_USE_TEMPLATE (friend_decl))
1155 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1156 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1157 need_template = false;
1159 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1160 && !PRIMARY_TEMPLATE_P (friend_decl))
1161 need_template = false;
1163 /* There is nothing to do if this is not a template friend. */
1164 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1165 return false;
1167 if (is_specialization_of (decl, friend_decl))
1168 return true;
1170 /* [temp.friend/6]
1171 A member of a class template may be declared to be a friend of a
1172 non-template class. In this case, the corresponding member of
1173 every specialization of the class template is a friend of the
1174 class granting friendship.
1176 For example, given a template friend declaration
1178 template <class T> friend void A<T>::f();
1180 the member function below is considered a friend
1182 template <> struct A<int> {
1183 void f();
1186 For this type of template friend, TEMPLATE_DEPTH below will be
1187 nonzero. To determine if DECL is a friend of FRIEND, we first
1188 check if the enclosing class is a specialization of another. */
1190 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1191 if (template_depth
1192 && DECL_CLASS_SCOPE_P (decl)
1193 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1194 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1196 /* Next, we check the members themselves. In order to handle
1197 a few tricky cases, such as when FRIEND_DECL's are
1199 template <class T> friend void A<T>::g(T t);
1200 template <class T> template <T t> friend void A<T>::h();
1202 and DECL's are
1204 void A<int>::g(int);
1205 template <int> void A<int>::h();
1207 we need to figure out ARGS, the template arguments from
1208 the context of DECL. This is required for template substitution
1209 of `T' in the function parameter of `g' and template parameter
1210 of `h' in the above examples. Here ARGS corresponds to `int'. */
1212 tree context = DECL_CONTEXT (decl);
1213 tree args = NULL_TREE;
1214 int current_depth = 0;
1216 while (current_depth < template_depth)
1218 if (CLASSTYPE_TEMPLATE_INFO (context))
1220 if (current_depth == 0)
1221 args = TYPE_TI_ARGS (context);
1222 else
1223 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1224 current_depth++;
1226 context = TYPE_CONTEXT (context);
1229 if (TREE_CODE (decl) == FUNCTION_DECL)
1231 bool is_template;
1232 tree friend_type;
1233 tree decl_type;
1234 tree friend_args_type;
1235 tree decl_args_type;
1237 /* Make sure that both DECL and FRIEND_DECL are templates or
1238 non-templates. */
1239 is_template = DECL_TEMPLATE_INFO (decl)
1240 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1241 if (need_template ^ is_template)
1242 return false;
1243 else if (is_template)
1245 /* If both are templates, check template parameter list. */
1246 tree friend_parms
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1248 args, tf_none);
1249 if (!comp_template_parms
1250 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1251 friend_parms))
1252 return false;
1254 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1256 else
1257 decl_type = TREE_TYPE (decl);
1259 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1260 tf_none, NULL_TREE);
1261 if (friend_type == error_mark_node)
1262 return false;
1264 /* Check if return types match. */
1265 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1266 return false;
1268 /* Check if function parameter types match, ignoring the
1269 `this' parameter. */
1270 friend_args_type = TYPE_ARG_TYPES (friend_type);
1271 decl_args_type = TYPE_ARG_TYPES (decl_type);
1272 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1273 friend_args_type = TREE_CHAIN (friend_args_type);
1274 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1275 decl_args_type = TREE_CHAIN (decl_args_type);
1277 return compparms (decl_args_type, friend_args_type);
1279 else
1281 /* DECL is a TYPE_DECL */
1282 bool is_template;
1283 tree decl_type = TREE_TYPE (decl);
1285 /* Make sure that both DECL and FRIEND_DECL are templates or
1286 non-templates. */
1287 is_template
1288 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1289 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1291 if (need_template ^ is_template)
1292 return false;
1293 else if (is_template)
1295 tree friend_parms;
1296 /* If both are templates, check the name of the two
1297 TEMPLATE_DECL's first because is_friend didn't. */
1298 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1299 != DECL_NAME (friend_decl))
1300 return false;
1302 /* Now check template parameter list. */
1303 friend_parms
1304 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1305 args, tf_none);
1306 return comp_template_parms
1307 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1308 friend_parms);
1310 else
1311 return (DECL_NAME (decl)
1312 == DECL_NAME (friend_decl));
1315 return false;
1318 /* Register the specialization SPEC as a specialization of TMPL with
1319 the indicated ARGS. IS_FRIEND indicates whether the specialization
1320 is actually just a friend declaration. Returns SPEC, or an
1321 equivalent prior declaration, if available.
1323 We also store instantiations of field packs in the hash table, even
1324 though they are not themselves templates, to make lookup easier. */
1326 static tree
1327 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1328 hashval_t hash)
1330 tree fn;
1331 void **slot = NULL;
1332 spec_entry elt;
1334 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1335 || (TREE_CODE (tmpl) == FIELD_DECL
1336 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1338 if (TREE_CODE (spec) == FUNCTION_DECL
1339 && uses_template_parms (DECL_TI_ARGS (spec)))
1340 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1341 register it; we want the corresponding TEMPLATE_DECL instead.
1342 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1343 the more obvious `uses_template_parms (spec)' to avoid problems
1344 with default function arguments. In particular, given
1345 something like this:
1347 template <class T> void f(T t1, T t = T())
1349 the default argument expression is not substituted for in an
1350 instantiation unless and until it is actually needed. */
1351 return spec;
1353 if (optimize_specialization_lookup_p (tmpl))
1354 /* We don't put these specializations in the hash table, but we might
1355 want to give an error about a mismatch. */
1356 fn = retrieve_specialization (tmpl, args, 0);
1357 else
1359 elt.tmpl = tmpl;
1360 elt.args = args;
1361 elt.spec = spec;
1363 if (hash == 0)
1364 hash = hash_specialization (&elt);
1366 slot =
1367 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1368 if (*slot)
1369 fn = ((spec_entry *) *slot)->spec;
1370 else
1371 fn = NULL_TREE;
1374 /* We can sometimes try to re-register a specialization that we've
1375 already got. In particular, regenerate_decl_from_template calls
1376 duplicate_decls which will update the specialization list. But,
1377 we'll still get called again here anyhow. It's more convenient
1378 to simply allow this than to try to prevent it. */
1379 if (fn == spec)
1380 return spec;
1381 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1383 if (DECL_TEMPLATE_INSTANTIATION (fn))
1385 if (DECL_ODR_USED (fn)
1386 || DECL_EXPLICIT_INSTANTIATION (fn))
1388 error ("specialization of %qD after instantiation",
1389 fn);
1390 return error_mark_node;
1392 else
1394 tree clone;
1395 /* This situation should occur only if the first
1396 specialization is an implicit instantiation, the
1397 second is an explicit specialization, and the
1398 implicit instantiation has not yet been used. That
1399 situation can occur if we have implicitly
1400 instantiated a member function and then specialized
1401 it later.
1403 We can also wind up here if a friend declaration that
1404 looked like an instantiation turns out to be a
1405 specialization:
1407 template <class T> void foo(T);
1408 class S { friend void foo<>(int) };
1409 template <> void foo(int);
1411 We transform the existing DECL in place so that any
1412 pointers to it become pointers to the updated
1413 declaration.
1415 If there was a definition for the template, but not
1416 for the specialization, we want this to look as if
1417 there were no definition, and vice versa. */
1418 DECL_INITIAL (fn) = NULL_TREE;
1419 duplicate_decls (spec, fn, is_friend);
1420 /* The call to duplicate_decls will have applied
1421 [temp.expl.spec]:
1423 An explicit specialization of a function template
1424 is inline only if it is explicitly declared to be,
1425 and independently of whether its function template
1428 to the primary function; now copy the inline bits to
1429 the various clones. */
1430 FOR_EACH_CLONE (clone, fn)
1432 DECL_DECLARED_INLINE_P (clone)
1433 = DECL_DECLARED_INLINE_P (fn);
1434 DECL_SOURCE_LOCATION (clone)
1435 = DECL_SOURCE_LOCATION (fn);
1437 check_specialization_namespace (tmpl);
1439 return fn;
1442 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1444 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1445 /* Dup decl failed, but this is a new definition. Set the
1446 line number so any errors match this new
1447 definition. */
1448 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1450 return fn;
1453 else if (fn)
1454 return duplicate_decls (spec, fn, is_friend);
1456 /* A specialization must be declared in the same namespace as the
1457 template it is specializing. */
1458 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1459 && !check_specialization_namespace (tmpl))
1460 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1462 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1464 spec_entry *entry = ggc_alloc_spec_entry ();
1465 gcc_assert (tmpl && args && spec);
1466 *entry = elt;
1467 *slot = entry;
1468 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1469 && PRIMARY_TEMPLATE_P (tmpl)
1470 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1471 /* TMPL is a forward declaration of a template function; keep a list
1472 of all specializations in case we need to reassign them to a friend
1473 template later in tsubst_friend_function. */
1474 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1475 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1478 return spec;
1481 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1482 TMPL and ARGS members, ignores SPEC. */
1484 int comparing_specializations;
1486 static int
1487 eq_specializations (const void *p1, const void *p2)
1489 const spec_entry *e1 = (const spec_entry *)p1;
1490 const spec_entry *e2 = (const spec_entry *)p2;
1491 int equal;
1493 ++comparing_specializations;
1494 equal = (e1->tmpl == e2->tmpl
1495 && comp_template_args (e1->args, e2->args));
1496 --comparing_specializations;
1498 return equal;
1501 /* Returns a hash for a template TMPL and template arguments ARGS. */
1503 static hashval_t
1504 hash_tmpl_and_args (tree tmpl, tree args)
1506 hashval_t val = DECL_UID (tmpl);
1507 return iterative_hash_template_arg (args, val);
1510 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1511 ignoring SPEC. */
1513 static hashval_t
1514 hash_specialization (const void *p)
1516 const spec_entry *e = (const spec_entry *)p;
1517 return hash_tmpl_and_args (e->tmpl, e->args);
1520 /* Recursively calculate a hash value for a template argument ARG, for use
1521 in the hash tables of template specializations. */
1523 hashval_t
1524 iterative_hash_template_arg (tree arg, hashval_t val)
1526 unsigned HOST_WIDE_INT i;
1527 enum tree_code code;
1528 char tclass;
1530 if (arg == NULL_TREE)
1531 return iterative_hash_object (arg, val);
1533 if (!TYPE_P (arg))
1534 STRIP_NOPS (arg);
1536 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1537 /* We can get one of these when re-hashing a previous entry in the middle
1538 of substituting into a pack expansion. Just look through it. */
1539 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1541 code = TREE_CODE (arg);
1542 tclass = TREE_CODE_CLASS (code);
1544 val = iterative_hash_object (code, val);
1546 switch (code)
1548 case ERROR_MARK:
1549 return val;
1551 case IDENTIFIER_NODE:
1552 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1554 case TREE_VEC:
1556 int i, len = TREE_VEC_LENGTH (arg);
1557 for (i = 0; i < len; ++i)
1558 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1559 return val;
1562 case TYPE_PACK_EXPANSION:
1563 case EXPR_PACK_EXPANSION:
1564 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1565 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1567 case TYPE_ARGUMENT_PACK:
1568 case NONTYPE_ARGUMENT_PACK:
1569 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1571 case TREE_LIST:
1572 for (; arg; arg = TREE_CHAIN (arg))
1573 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1574 return val;
1576 case OVERLOAD:
1577 for (; arg; arg = OVL_NEXT (arg))
1578 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1579 return val;
1581 case CONSTRUCTOR:
1583 tree field, value;
1584 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1586 val = iterative_hash_template_arg (field, val);
1587 val = iterative_hash_template_arg (value, val);
1589 return val;
1592 case PARM_DECL:
1593 if (!DECL_ARTIFICIAL (arg))
1595 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1596 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1598 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1600 case TARGET_EXPR:
1601 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1603 case PTRMEM_CST:
1604 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1605 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1607 case TEMPLATE_PARM_INDEX:
1608 val = iterative_hash_template_arg
1609 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1610 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1611 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1613 case TRAIT_EXPR:
1614 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1615 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1616 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1618 case BASELINK:
1619 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1620 val);
1621 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1622 val);
1624 case MODOP_EXPR:
1625 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1626 code = TREE_CODE (TREE_OPERAND (arg, 1));
1627 val = iterative_hash_object (code, val);
1628 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1630 case LAMBDA_EXPR:
1631 /* A lambda can't appear in a template arg, but don't crash on
1632 erroneous input. */
1633 gcc_assert (seen_error ());
1634 return val;
1636 case CAST_EXPR:
1637 case IMPLICIT_CONV_EXPR:
1638 case STATIC_CAST_EXPR:
1639 case REINTERPRET_CAST_EXPR:
1640 case CONST_CAST_EXPR:
1641 case DYNAMIC_CAST_EXPR:
1642 case NEW_EXPR:
1643 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1644 /* Now hash operands as usual. */
1645 break;
1647 default:
1648 break;
1651 switch (tclass)
1653 case tcc_type:
1654 if (TYPE_CANONICAL (arg))
1655 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1656 val);
1657 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1658 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1659 /* Otherwise just compare the types during lookup. */
1660 return val;
1662 case tcc_declaration:
1663 case tcc_constant:
1664 return iterative_hash_expr (arg, val);
1666 default:
1667 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1669 unsigned n = cp_tree_operand_length (arg);
1670 for (i = 0; i < n; ++i)
1671 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1672 return val;
1675 gcc_unreachable ();
1676 return 0;
1679 /* Unregister the specialization SPEC as a specialization of TMPL.
1680 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1681 if the SPEC was listed as a specialization of TMPL.
1683 Note that SPEC has been ggc_freed, so we can't look inside it. */
1685 bool
1686 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1688 spec_entry *entry;
1689 spec_entry elt;
1691 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1692 elt.args = TI_ARGS (tinfo);
1693 elt.spec = NULL_TREE;
1695 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1696 if (entry != NULL)
1698 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1699 gcc_assert (new_spec != NULL_TREE);
1700 entry->spec = new_spec;
1701 return 1;
1704 return 0;
1707 /* Like register_specialization, but for local declarations. We are
1708 registering SPEC, an instantiation of TMPL. */
1710 static void
1711 register_local_specialization (tree spec, tree tmpl)
1713 void **slot;
1715 slot = pointer_map_insert (local_specializations, tmpl);
1716 *slot = spec;
1719 /* TYPE is a class type. Returns true if TYPE is an explicitly
1720 specialized class. */
1722 bool
1723 explicit_class_specialization_p (tree type)
1725 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1726 return false;
1727 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1730 /* Print the list of functions at FNS, going through all the overloads
1731 for each element of the list. Alternatively, FNS can not be a
1732 TREE_LIST, in which case it will be printed together with all the
1733 overloads.
1735 MORE and *STR should respectively be FALSE and NULL when the function
1736 is called from the outside. They are used internally on recursive
1737 calls. print_candidates manages the two parameters and leaves NULL
1738 in *STR when it ends. */
1740 static void
1741 print_candidates_1 (tree fns, bool more, const char **str)
1743 tree fn, fn2;
1744 char *spaces = NULL;
1746 for (fn = fns; fn; fn = OVL_NEXT (fn))
1747 if (TREE_CODE (fn) == TREE_LIST)
1749 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1750 print_candidates_1 (TREE_VALUE (fn2),
1751 TREE_CHAIN (fn2) || more, str);
1753 else
1755 tree cand = OVL_CURRENT (fn);
1756 if (!*str)
1758 /* Pick the prefix string. */
1759 if (!more && !OVL_NEXT (fns))
1761 inform (DECL_SOURCE_LOCATION (cand),
1762 "candidate is: %#D", cand);
1763 continue;
1766 *str = _("candidates are:");
1767 spaces = get_spaces (*str);
1769 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1770 *str = spaces ? spaces : *str;
1773 if (!more)
1775 free (spaces);
1776 *str = NULL;
1780 /* Print the list of candidate FNS in an error message. FNS can also
1781 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1783 void
1784 print_candidates (tree fns)
1786 const char *str = NULL;
1787 print_candidates_1 (fns, false, &str);
1788 gcc_assert (str == NULL);
1791 /* Returns the template (one of the functions given by TEMPLATE_ID)
1792 which can be specialized to match the indicated DECL with the
1793 explicit template args given in TEMPLATE_ID. The DECL may be
1794 NULL_TREE if none is available. In that case, the functions in
1795 TEMPLATE_ID are non-members.
1797 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1798 specialization of a member template.
1800 The TEMPLATE_COUNT is the number of references to qualifying
1801 template classes that appeared in the name of the function. See
1802 check_explicit_specialization for a more accurate description.
1804 TSK indicates what kind of template declaration (if any) is being
1805 declared. TSK_TEMPLATE indicates that the declaration given by
1806 DECL, though a FUNCTION_DECL, has template parameters, and is
1807 therefore a template function.
1809 The template args (those explicitly specified and those deduced)
1810 are output in a newly created vector *TARGS_OUT.
1812 If it is impossible to determine the result, an error message is
1813 issued. The error_mark_node is returned to indicate failure. */
1815 static tree
1816 determine_specialization (tree template_id,
1817 tree decl,
1818 tree* targs_out,
1819 int need_member_template,
1820 int template_count,
1821 tmpl_spec_kind tsk)
1823 tree fns;
1824 tree targs;
1825 tree explicit_targs;
1826 tree candidates = NULL_TREE;
1827 /* A TREE_LIST of templates of which DECL may be a specialization.
1828 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1829 corresponding TREE_PURPOSE is the set of template arguments that,
1830 when used to instantiate the template, would produce a function
1831 with the signature of DECL. */
1832 tree templates = NULL_TREE;
1833 int header_count;
1834 cp_binding_level *b;
1836 *targs_out = NULL_TREE;
1838 if (template_id == error_mark_node || decl == error_mark_node)
1839 return error_mark_node;
1841 /* We shouldn't be specializing a member template of an
1842 unspecialized class template; we already gave an error in
1843 check_specialization_scope, now avoid crashing. */
1844 if (template_count && DECL_CLASS_SCOPE_P (decl)
1845 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1847 gcc_assert (errorcount);
1848 return error_mark_node;
1851 fns = TREE_OPERAND (template_id, 0);
1852 explicit_targs = TREE_OPERAND (template_id, 1);
1854 if (fns == error_mark_node)
1855 return error_mark_node;
1857 /* Check for baselinks. */
1858 if (BASELINK_P (fns))
1859 fns = BASELINK_FUNCTIONS (fns);
1861 if (!is_overloaded_fn (fns))
1863 error ("%qD is not a function template", fns);
1864 return error_mark_node;
1867 /* Count the number of template headers specified for this
1868 specialization. */
1869 header_count = 0;
1870 for (b = current_binding_level;
1871 b->kind == sk_template_parms;
1872 b = b->level_chain)
1873 ++header_count;
1875 for (; fns; fns = OVL_NEXT (fns))
1877 tree fn = OVL_CURRENT (fns);
1879 if (TREE_CODE (fn) == TEMPLATE_DECL)
1881 tree decl_arg_types;
1882 tree fn_arg_types;
1883 tree insttype;
1885 /* In case of explicit specialization, we need to check if
1886 the number of template headers appearing in the specialization
1887 is correct. This is usually done in check_explicit_specialization,
1888 but the check done there cannot be exhaustive when specializing
1889 member functions. Consider the following code:
1891 template <> void A<int>::f(int);
1892 template <> template <> void A<int>::f(int);
1894 Assuming that A<int> is not itself an explicit specialization
1895 already, the first line specializes "f" which is a non-template
1896 member function, whilst the second line specializes "f" which
1897 is a template member function. So both lines are syntactically
1898 correct, and check_explicit_specialization does not reject
1899 them.
1901 Here, we can do better, as we are matching the specialization
1902 against the declarations. We count the number of template
1903 headers, and we check if they match TEMPLATE_COUNT + 1
1904 (TEMPLATE_COUNT is the number of qualifying template classes,
1905 plus there must be another header for the member template
1906 itself).
1908 Notice that if header_count is zero, this is not a
1909 specialization but rather a template instantiation, so there
1910 is no check we can perform here. */
1911 if (header_count && header_count != template_count + 1)
1912 continue;
1914 /* Check that the number of template arguments at the
1915 innermost level for DECL is the same as for FN. */
1916 if (current_binding_level->kind == sk_template_parms
1917 && !current_binding_level->explicit_spec_p
1918 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1919 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1920 (current_template_parms))))
1921 continue;
1923 /* DECL might be a specialization of FN. */
1924 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1925 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1927 /* For a non-static member function, we need to make sure
1928 that the const qualification is the same. Since
1929 get_bindings does not try to merge the "this" parameter,
1930 we must do the comparison explicitly. */
1931 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1932 && !same_type_p (TREE_VALUE (fn_arg_types),
1933 TREE_VALUE (decl_arg_types)))
1934 continue;
1936 /* Skip the "this" parameter and, for constructors of
1937 classes with virtual bases, the VTT parameter. A
1938 full specialization of a constructor will have a VTT
1939 parameter, but a template never will. */
1940 decl_arg_types
1941 = skip_artificial_parms_for (decl, decl_arg_types);
1942 fn_arg_types
1943 = skip_artificial_parms_for (fn, fn_arg_types);
1945 /* Function templates cannot be specializations; there are
1946 no partial specializations of functions. Therefore, if
1947 the type of DECL does not match FN, there is no
1948 match. */
1949 if (tsk == tsk_template)
1951 if (compparms (fn_arg_types, decl_arg_types))
1952 candidates = tree_cons (NULL_TREE, fn, candidates);
1953 continue;
1956 /* See whether this function might be a specialization of this
1957 template. Suppress access control because we might be trying
1958 to make this specialization a friend, and we have already done
1959 access control for the declaration of the specialization. */
1960 push_deferring_access_checks (dk_no_check);
1961 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1962 pop_deferring_access_checks ();
1964 if (!targs)
1965 /* We cannot deduce template arguments that when used to
1966 specialize TMPL will produce DECL. */
1967 continue;
1969 /* Make sure that the deduced arguments actually work. */
1970 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1971 if (insttype == error_mark_node)
1972 continue;
1973 fn_arg_types
1974 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1975 if (!compparms (fn_arg_types, decl_arg_types))
1976 continue;
1978 /* Save this template, and the arguments deduced. */
1979 templates = tree_cons (targs, fn, templates);
1981 else if (need_member_template)
1982 /* FN is an ordinary member function, and we need a
1983 specialization of a member template. */
1985 else if (TREE_CODE (fn) != FUNCTION_DECL)
1986 /* We can get IDENTIFIER_NODEs here in certain erroneous
1987 cases. */
1989 else if (!DECL_FUNCTION_MEMBER_P (fn))
1990 /* This is just an ordinary non-member function. Nothing can
1991 be a specialization of that. */
1993 else if (DECL_ARTIFICIAL (fn))
1994 /* Cannot specialize functions that are created implicitly. */
1996 else
1998 tree decl_arg_types;
2000 /* This is an ordinary member function. However, since
2001 we're here, we can assume its enclosing class is a
2002 template class. For example,
2004 template <typename T> struct S { void f(); };
2005 template <> void S<int>::f() {}
2007 Here, S<int>::f is a non-template, but S<int> is a
2008 template class. If FN has the same type as DECL, we
2009 might be in business. */
2011 if (!DECL_TEMPLATE_INFO (fn))
2012 /* Its enclosing class is an explicit specialization
2013 of a template class. This is not a candidate. */
2014 continue;
2016 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2017 TREE_TYPE (TREE_TYPE (fn))))
2018 /* The return types differ. */
2019 continue;
2021 /* Adjust the type of DECL in case FN is a static member. */
2022 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2023 if (DECL_STATIC_FUNCTION_P (fn)
2024 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2025 decl_arg_types = TREE_CHAIN (decl_arg_types);
2027 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2028 decl_arg_types))
2029 /* They match! */
2030 candidates = tree_cons (NULL_TREE, fn, candidates);
2034 if (templates && TREE_CHAIN (templates))
2036 /* We have:
2038 [temp.expl.spec]
2040 It is possible for a specialization with a given function
2041 signature to be instantiated from more than one function
2042 template. In such cases, explicit specification of the
2043 template arguments must be used to uniquely identify the
2044 function template specialization being specialized.
2046 Note that here, there's no suggestion that we're supposed to
2047 determine which of the candidate templates is most
2048 specialized. However, we, also have:
2050 [temp.func.order]
2052 Partial ordering of overloaded function template
2053 declarations is used in the following contexts to select
2054 the function template to which a function template
2055 specialization refers:
2057 -- when an explicit specialization refers to a function
2058 template.
2060 So, we do use the partial ordering rules, at least for now.
2061 This extension can only serve to make invalid programs valid,
2062 so it's safe. And, there is strong anecdotal evidence that
2063 the committee intended the partial ordering rules to apply;
2064 the EDG front end has that behavior, and John Spicer claims
2065 that the committee simply forgot to delete the wording in
2066 [temp.expl.spec]. */
2067 tree tmpl = most_specialized_instantiation (templates);
2068 if (tmpl != error_mark_node)
2070 templates = tmpl;
2071 TREE_CHAIN (templates) = NULL_TREE;
2075 if (templates == NULL_TREE && candidates == NULL_TREE)
2077 error ("template-id %qD for %q+D does not match any template "
2078 "declaration", template_id, decl);
2079 if (header_count && header_count != template_count + 1)
2080 inform (input_location, "saw %d %<template<>%>, need %d for "
2081 "specializing a member function template",
2082 header_count, template_count + 1);
2083 return error_mark_node;
2085 else if ((templates && TREE_CHAIN (templates))
2086 || (candidates && TREE_CHAIN (candidates))
2087 || (templates && candidates))
2089 error ("ambiguous template specialization %qD for %q+D",
2090 template_id, decl);
2091 candidates = chainon (candidates, templates);
2092 print_candidates (candidates);
2093 return error_mark_node;
2096 /* We have one, and exactly one, match. */
2097 if (candidates)
2099 tree fn = TREE_VALUE (candidates);
2100 *targs_out = copy_node (DECL_TI_ARGS (fn));
2101 /* DECL is a re-declaration or partial instantiation of a template
2102 function. */
2103 if (TREE_CODE (fn) == TEMPLATE_DECL)
2104 return fn;
2105 /* It was a specialization of an ordinary member function in a
2106 template class. */
2107 return DECL_TI_TEMPLATE (fn);
2110 /* It was a specialization of a template. */
2111 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2112 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2114 *targs_out = copy_node (targs);
2115 SET_TMPL_ARGS_LEVEL (*targs_out,
2116 TMPL_ARGS_DEPTH (*targs_out),
2117 TREE_PURPOSE (templates));
2119 else
2120 *targs_out = TREE_PURPOSE (templates);
2121 return TREE_VALUE (templates);
2124 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2125 but with the default argument values filled in from those in the
2126 TMPL_TYPES. */
2128 static tree
2129 copy_default_args_to_explicit_spec_1 (tree spec_types,
2130 tree tmpl_types)
2132 tree new_spec_types;
2134 if (!spec_types)
2135 return NULL_TREE;
2137 if (spec_types == void_list_node)
2138 return void_list_node;
2140 /* Substitute into the rest of the list. */
2141 new_spec_types =
2142 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2143 TREE_CHAIN (tmpl_types));
2145 /* Add the default argument for this parameter. */
2146 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2147 TREE_VALUE (spec_types),
2148 new_spec_types);
2151 /* DECL is an explicit specialization. Replicate default arguments
2152 from the template it specializes. (That way, code like:
2154 template <class T> void f(T = 3);
2155 template <> void f(double);
2156 void g () { f (); }
2158 works, as required.) An alternative approach would be to look up
2159 the correct default arguments at the call-site, but this approach
2160 is consistent with how implicit instantiations are handled. */
2162 static void
2163 copy_default_args_to_explicit_spec (tree decl)
2165 tree tmpl;
2166 tree spec_types;
2167 tree tmpl_types;
2168 tree new_spec_types;
2169 tree old_type;
2170 tree new_type;
2171 tree t;
2172 tree object_type = NULL_TREE;
2173 tree in_charge = NULL_TREE;
2174 tree vtt = NULL_TREE;
2176 /* See if there's anything we need to do. */
2177 tmpl = DECL_TI_TEMPLATE (decl);
2178 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2179 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2180 if (TREE_PURPOSE (t))
2181 break;
2182 if (!t)
2183 return;
2185 old_type = TREE_TYPE (decl);
2186 spec_types = TYPE_ARG_TYPES (old_type);
2188 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2190 /* Remove the this pointer, but remember the object's type for
2191 CV quals. */
2192 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2193 spec_types = TREE_CHAIN (spec_types);
2194 tmpl_types = TREE_CHAIN (tmpl_types);
2196 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2198 /* DECL may contain more parameters than TMPL due to the extra
2199 in-charge parameter in constructors and destructors. */
2200 in_charge = spec_types;
2201 spec_types = TREE_CHAIN (spec_types);
2203 if (DECL_HAS_VTT_PARM_P (decl))
2205 vtt = spec_types;
2206 spec_types = TREE_CHAIN (spec_types);
2210 /* Compute the merged default arguments. */
2211 new_spec_types =
2212 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2214 /* Compute the new FUNCTION_TYPE. */
2215 if (object_type)
2217 if (vtt)
2218 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2219 TREE_VALUE (vtt),
2220 new_spec_types);
2222 if (in_charge)
2223 /* Put the in-charge parameter back. */
2224 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2225 TREE_VALUE (in_charge),
2226 new_spec_types);
2228 new_type = build_method_type_directly (object_type,
2229 TREE_TYPE (old_type),
2230 new_spec_types);
2232 else
2233 new_type = build_function_type (TREE_TYPE (old_type),
2234 new_spec_types);
2235 new_type = cp_build_type_attribute_variant (new_type,
2236 TYPE_ATTRIBUTES (old_type));
2237 new_type = build_exception_variant (new_type,
2238 TYPE_RAISES_EXCEPTIONS (old_type));
2239 TREE_TYPE (decl) = new_type;
2242 /* Return the number of template headers we expect to see for a definition
2243 or specialization of CTYPE or one of its non-template members. */
2246 num_template_headers_for_class (tree ctype)
2248 int num_templates = 0;
2250 while (ctype && CLASS_TYPE_P (ctype))
2252 /* You're supposed to have one `template <...>' for every
2253 template class, but you don't need one for a full
2254 specialization. For example:
2256 template <class T> struct S{};
2257 template <> struct S<int> { void f(); };
2258 void S<int>::f () {}
2260 is correct; there shouldn't be a `template <>' for the
2261 definition of `S<int>::f'. */
2262 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2263 /* If CTYPE does not have template information of any
2264 kind, then it is not a template, nor is it nested
2265 within a template. */
2266 break;
2267 if (explicit_class_specialization_p (ctype))
2268 break;
2269 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2270 ++num_templates;
2272 ctype = TYPE_CONTEXT (ctype);
2275 return num_templates;
2278 /* Do a simple sanity check on the template headers that precede the
2279 variable declaration DECL. */
2281 void
2282 check_template_variable (tree decl)
2284 tree ctx = CP_DECL_CONTEXT (decl);
2285 int wanted = num_template_headers_for_class (ctx);
2286 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2287 permerror (DECL_SOURCE_LOCATION (decl),
2288 "%qD is not a static data member of a class template", decl);
2289 else if (template_header_count > wanted)
2291 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2292 "too many template headers for %D (should be %d)",
2293 decl, wanted);
2294 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2295 inform (DECL_SOURCE_LOCATION (decl),
2296 "members of an explicitly specialized class are defined "
2297 "without a template header");
2301 /* Check to see if the function just declared, as indicated in
2302 DECLARATOR, and in DECL, is a specialization of a function
2303 template. We may also discover that the declaration is an explicit
2304 instantiation at this point.
2306 Returns DECL, or an equivalent declaration that should be used
2307 instead if all goes well. Issues an error message if something is
2308 amiss. Returns error_mark_node if the error is not easily
2309 recoverable.
2311 FLAGS is a bitmask consisting of the following flags:
2313 2: The function has a definition.
2314 4: The function is a friend.
2316 The TEMPLATE_COUNT is the number of references to qualifying
2317 template classes that appeared in the name of the function. For
2318 example, in
2320 template <class T> struct S { void f(); };
2321 void S<int>::f();
2323 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2324 classes are not counted in the TEMPLATE_COUNT, so that in
2326 template <class T> struct S {};
2327 template <> struct S<int> { void f(); }
2328 template <> void S<int>::f();
2330 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2331 invalid; there should be no template <>.)
2333 If the function is a specialization, it is marked as such via
2334 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2335 is set up correctly, and it is added to the list of specializations
2336 for that template. */
2338 tree
2339 check_explicit_specialization (tree declarator,
2340 tree decl,
2341 int template_count,
2342 int flags)
2344 int have_def = flags & 2;
2345 int is_friend = flags & 4;
2346 int specialization = 0;
2347 int explicit_instantiation = 0;
2348 int member_specialization = 0;
2349 tree ctype = DECL_CLASS_CONTEXT (decl);
2350 tree dname = DECL_NAME (decl);
2351 tmpl_spec_kind tsk;
2353 if (is_friend)
2355 if (!processing_specialization)
2356 tsk = tsk_none;
2357 else
2358 tsk = tsk_excessive_parms;
2360 else
2361 tsk = current_tmpl_spec_kind (template_count);
2363 switch (tsk)
2365 case tsk_none:
2366 if (processing_specialization)
2368 specialization = 1;
2369 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2371 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2373 if (is_friend)
2374 /* This could be something like:
2376 template <class T> void f(T);
2377 class S { friend void f<>(int); } */
2378 specialization = 1;
2379 else
2381 /* This case handles bogus declarations like template <>
2382 template <class T> void f<int>(); */
2384 error ("template-id %qD in declaration of primary template",
2385 declarator);
2386 return decl;
2389 break;
2391 case tsk_invalid_member_spec:
2392 /* The error has already been reported in
2393 check_specialization_scope. */
2394 return error_mark_node;
2396 case tsk_invalid_expl_inst:
2397 error ("template parameter list used in explicit instantiation");
2399 /* Fall through. */
2401 case tsk_expl_inst:
2402 if (have_def)
2403 error ("definition provided for explicit instantiation");
2405 explicit_instantiation = 1;
2406 break;
2408 case tsk_excessive_parms:
2409 case tsk_insufficient_parms:
2410 if (tsk == tsk_excessive_parms)
2411 error ("too many template parameter lists in declaration of %qD",
2412 decl);
2413 else if (template_header_count)
2414 error("too few template parameter lists in declaration of %qD", decl);
2415 else
2416 error("explicit specialization of %qD must be introduced by "
2417 "%<template <>%>", decl);
2419 /* Fall through. */
2420 case tsk_expl_spec:
2421 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2422 if (ctype)
2423 member_specialization = 1;
2424 else
2425 specialization = 1;
2426 break;
2428 case tsk_template:
2429 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2431 /* This case handles bogus declarations like template <>
2432 template <class T> void f<int>(); */
2434 if (uses_template_parms (declarator))
2435 error ("function template partial specialization %qD "
2436 "is not allowed", declarator);
2437 else
2438 error ("template-id %qD in declaration of primary template",
2439 declarator);
2440 return decl;
2443 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2444 /* This is a specialization of a member template, without
2445 specialization the containing class. Something like:
2447 template <class T> struct S {
2448 template <class U> void f (U);
2450 template <> template <class U> void S<int>::f(U) {}
2452 That's a specialization -- but of the entire template. */
2453 specialization = 1;
2454 break;
2456 default:
2457 gcc_unreachable ();
2460 if (specialization || member_specialization)
2462 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2463 for (; t; t = TREE_CHAIN (t))
2464 if (TREE_PURPOSE (t))
2466 permerror (input_location,
2467 "default argument specified in explicit specialization");
2468 break;
2472 if (specialization || member_specialization || explicit_instantiation)
2474 tree tmpl = NULL_TREE;
2475 tree targs = NULL_TREE;
2477 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2478 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2480 tree fns;
2482 gcc_assert (identifier_p (declarator));
2483 if (ctype)
2484 fns = dname;
2485 else
2487 /* If there is no class context, the explicit instantiation
2488 must be at namespace scope. */
2489 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2491 /* Find the namespace binding, using the declaration
2492 context. */
2493 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2494 false, true);
2495 if (fns == error_mark_node || !is_overloaded_fn (fns))
2497 error ("%qD is not a template function", dname);
2498 fns = error_mark_node;
2500 else
2502 tree fn = OVL_CURRENT (fns);
2503 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2504 CP_DECL_CONTEXT (fn)))
2505 error ("%qD is not declared in %qD",
2506 decl, current_namespace);
2510 declarator = lookup_template_function (fns, NULL_TREE);
2513 if (declarator == error_mark_node)
2514 return error_mark_node;
2516 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2518 if (!explicit_instantiation)
2519 /* A specialization in class scope. This is invalid,
2520 but the error will already have been flagged by
2521 check_specialization_scope. */
2522 return error_mark_node;
2523 else
2525 /* It's not valid to write an explicit instantiation in
2526 class scope, e.g.:
2528 class C { template void f(); }
2530 This case is caught by the parser. However, on
2531 something like:
2533 template class C { void f(); };
2535 (which is invalid) we can get here. The error will be
2536 issued later. */
2540 return decl;
2542 else if (ctype != NULL_TREE
2543 && (identifier_p (TREE_OPERAND (declarator, 0))))
2545 /* Find the list of functions in ctype that have the same
2546 name as the declared function. */
2547 tree name = TREE_OPERAND (declarator, 0);
2548 tree fns = NULL_TREE;
2549 int idx;
2551 if (constructor_name_p (name, ctype))
2553 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2555 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2556 : !CLASSTYPE_DESTRUCTORS (ctype))
2558 /* From [temp.expl.spec]:
2560 If such an explicit specialization for the member
2561 of a class template names an implicitly-declared
2562 special member function (clause _special_), the
2563 program is ill-formed.
2565 Similar language is found in [temp.explicit]. */
2566 error ("specialization of implicitly-declared special member function");
2567 return error_mark_node;
2570 name = is_constructor ? ctor_identifier : dtor_identifier;
2573 if (!DECL_CONV_FN_P (decl))
2575 idx = lookup_fnfields_1 (ctype, name);
2576 if (idx >= 0)
2577 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2579 else
2581 vec<tree, va_gc> *methods;
2582 tree ovl;
2584 /* For a type-conversion operator, we cannot do a
2585 name-based lookup. We might be looking for `operator
2586 int' which will be a specialization of `operator T'.
2587 So, we find *all* the conversion operators, and then
2588 select from them. */
2589 fns = NULL_TREE;
2591 methods = CLASSTYPE_METHOD_VEC (ctype);
2592 if (methods)
2593 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2594 methods->iterate (idx, &ovl);
2595 ++idx)
2597 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2598 /* There are no more conversion functions. */
2599 break;
2601 /* Glue all these conversion functions together
2602 with those we already have. */
2603 for (; ovl; ovl = OVL_NEXT (ovl))
2604 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2608 if (fns == NULL_TREE)
2610 error ("no member function %qD declared in %qT", name, ctype);
2611 return error_mark_node;
2613 else
2614 TREE_OPERAND (declarator, 0) = fns;
2617 /* Figure out what exactly is being specialized at this point.
2618 Note that for an explicit instantiation, even one for a
2619 member function, we cannot tell apriori whether the
2620 instantiation is for a member template, or just a member
2621 function of a template class. Even if a member template is
2622 being instantiated, the member template arguments may be
2623 elided if they can be deduced from the rest of the
2624 declaration. */
2625 tmpl = determine_specialization (declarator, decl,
2626 &targs,
2627 member_specialization,
2628 template_count,
2629 tsk);
2631 if (!tmpl || tmpl == error_mark_node)
2632 /* We couldn't figure out what this declaration was
2633 specializing. */
2634 return error_mark_node;
2635 else
2637 tree gen_tmpl = most_general_template (tmpl);
2639 if (explicit_instantiation)
2641 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2642 is done by do_decl_instantiation later. */
2644 int arg_depth = TMPL_ARGS_DEPTH (targs);
2645 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2647 if (arg_depth > parm_depth)
2649 /* If TMPL is not the most general template (for
2650 example, if TMPL is a friend template that is
2651 injected into namespace scope), then there will
2652 be too many levels of TARGS. Remove some of them
2653 here. */
2654 int i;
2655 tree new_targs;
2657 new_targs = make_tree_vec (parm_depth);
2658 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2659 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2660 = TREE_VEC_ELT (targs, i);
2661 targs = new_targs;
2664 return instantiate_template (tmpl, targs, tf_error);
2667 /* If we thought that the DECL was a member function, but it
2668 turns out to be specializing a static member function,
2669 make DECL a static member function as well. */
2670 if (DECL_STATIC_FUNCTION_P (tmpl)
2671 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2672 revert_static_member_fn (decl);
2674 /* If this is a specialization of a member template of a
2675 template class, we want to return the TEMPLATE_DECL, not
2676 the specialization of it. */
2677 if (tsk == tsk_template)
2679 tree result = DECL_TEMPLATE_RESULT (tmpl);
2680 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2681 DECL_INITIAL (result) = NULL_TREE;
2682 if (have_def)
2684 tree parm;
2685 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2686 DECL_SOURCE_LOCATION (result)
2687 = DECL_SOURCE_LOCATION (decl);
2688 /* We want to use the argument list specified in the
2689 definition, not in the original declaration. */
2690 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2691 for (parm = DECL_ARGUMENTS (result); parm;
2692 parm = DECL_CHAIN (parm))
2693 DECL_CONTEXT (parm) = result;
2695 return register_specialization (tmpl, gen_tmpl, targs,
2696 is_friend, 0);
2699 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2700 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2702 /* Inherit default function arguments from the template
2703 DECL is specializing. */
2704 copy_default_args_to_explicit_spec (decl);
2706 /* This specialization has the same protection as the
2707 template it specializes. */
2708 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2709 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2711 /* 7.1.1-1 [dcl.stc]
2713 A storage-class-specifier shall not be specified in an
2714 explicit specialization...
2716 The parser rejects these, so unless action is taken here,
2717 explicit function specializations will always appear with
2718 global linkage.
2720 The action recommended by the C++ CWG in response to C++
2721 defect report 605 is to make the storage class and linkage
2722 of the explicit specialization match the templated function:
2724 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2726 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2728 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2729 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2731 /* This specialization has the same linkage and visibility as
2732 the function template it specializes. */
2733 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2734 if (! TREE_PUBLIC (decl))
2736 DECL_INTERFACE_KNOWN (decl) = 1;
2737 DECL_NOT_REALLY_EXTERN (decl) = 1;
2739 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2740 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2742 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2743 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2747 /* If DECL is a friend declaration, declared using an
2748 unqualified name, the namespace associated with DECL may
2749 have been set incorrectly. For example, in:
2751 template <typename T> void f(T);
2752 namespace N {
2753 struct S { friend void f<int>(int); }
2756 we will have set the DECL_CONTEXT for the friend
2757 declaration to N, rather than to the global namespace. */
2758 if (DECL_NAMESPACE_SCOPE_P (decl))
2759 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2761 if (is_friend && !have_def)
2762 /* This is not really a declaration of a specialization.
2763 It's just the name of an instantiation. But, it's not
2764 a request for an instantiation, either. */
2765 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2766 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2767 /* This is indeed a specialization. In case of constructors
2768 and destructors, we need in-charge and not-in-charge
2769 versions in V3 ABI. */
2770 clone_function_decl (decl, /*update_method_vec_p=*/0);
2772 /* Register this specialization so that we can find it
2773 again. */
2774 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2778 return decl;
2781 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2782 parameters. These are represented in the same format used for
2783 DECL_TEMPLATE_PARMS. */
2786 comp_template_parms (const_tree parms1, const_tree parms2)
2788 const_tree p1;
2789 const_tree p2;
2791 if (parms1 == parms2)
2792 return 1;
2794 for (p1 = parms1, p2 = parms2;
2795 p1 != NULL_TREE && p2 != NULL_TREE;
2796 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2798 tree t1 = TREE_VALUE (p1);
2799 tree t2 = TREE_VALUE (p2);
2800 int i;
2802 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2803 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2805 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2806 return 0;
2808 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2810 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2811 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2813 /* If either of the template parameters are invalid, assume
2814 they match for the sake of error recovery. */
2815 if (parm1 == error_mark_node || parm2 == error_mark_node)
2816 return 1;
2818 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2819 return 0;
2821 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2822 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2823 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2824 continue;
2825 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2826 return 0;
2830 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2831 /* One set of parameters has more parameters lists than the
2832 other. */
2833 return 0;
2835 return 1;
2838 /* Determine whether PARM is a parameter pack. */
2840 bool
2841 template_parameter_pack_p (const_tree parm)
2843 /* Determine if we have a non-type template parameter pack. */
2844 if (TREE_CODE (parm) == PARM_DECL)
2845 return (DECL_TEMPLATE_PARM_P (parm)
2846 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2847 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2848 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2850 /* If this is a list of template parameters, we could get a
2851 TYPE_DECL or a TEMPLATE_DECL. */
2852 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2853 parm = TREE_TYPE (parm);
2855 /* Otherwise it must be a type template parameter. */
2856 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2857 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2858 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2861 /* Determine if T is a function parameter pack. */
2863 bool
2864 function_parameter_pack_p (const_tree t)
2866 if (t && TREE_CODE (t) == PARM_DECL)
2867 return DECL_PACK_P (t);
2868 return false;
2871 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2872 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2874 tree
2875 get_function_template_decl (const_tree primary_func_tmpl_inst)
2877 if (! primary_func_tmpl_inst
2878 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2879 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2880 return NULL;
2882 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2885 /* Return true iff the function parameter PARAM_DECL was expanded
2886 from the function parameter pack PACK. */
2888 bool
2889 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2891 if (DECL_ARTIFICIAL (param_decl)
2892 || !function_parameter_pack_p (pack))
2893 return false;
2895 /* The parameter pack and its pack arguments have the same
2896 DECL_PARM_INDEX. */
2897 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2900 /* Determine whether ARGS describes a variadic template args list,
2901 i.e., one that is terminated by a template argument pack. */
2903 static bool
2904 template_args_variadic_p (tree args)
2906 int nargs;
2907 tree last_parm;
2909 if (args == NULL_TREE)
2910 return false;
2912 args = INNERMOST_TEMPLATE_ARGS (args);
2913 nargs = TREE_VEC_LENGTH (args);
2915 if (nargs == 0)
2916 return false;
2918 last_parm = TREE_VEC_ELT (args, nargs - 1);
2920 return ARGUMENT_PACK_P (last_parm);
2923 /* Generate a new name for the parameter pack name NAME (an
2924 IDENTIFIER_NODE) that incorporates its */
2926 static tree
2927 make_ith_pack_parameter_name (tree name, int i)
2929 /* Munge the name to include the parameter index. */
2930 #define NUMBUF_LEN 128
2931 char numbuf[NUMBUF_LEN];
2932 char* newname;
2933 int newname_len;
2935 if (name == NULL_TREE)
2936 return name;
2937 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2938 newname_len = IDENTIFIER_LENGTH (name)
2939 + strlen (numbuf) + 2;
2940 newname = (char*)alloca (newname_len);
2941 snprintf (newname, newname_len,
2942 "%s#%i", IDENTIFIER_POINTER (name), i);
2943 return get_identifier (newname);
2946 /* Return true if T is a primary function, class or alias template
2947 instantiation. */
2949 bool
2950 primary_template_instantiation_p (const_tree t)
2952 if (!t)
2953 return false;
2955 if (TREE_CODE (t) == FUNCTION_DECL)
2956 return DECL_LANG_SPECIFIC (t)
2957 && DECL_TEMPLATE_INSTANTIATION (t)
2958 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2959 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2960 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2961 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2962 else if (alias_template_specialization_p (t))
2963 return true;
2964 return false;
2967 /* Return true if PARM is a template template parameter. */
2969 bool
2970 template_template_parameter_p (const_tree parm)
2972 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2975 /* Return true iff PARM is a DECL representing a type template
2976 parameter. */
2978 bool
2979 template_type_parameter_p (const_tree parm)
2981 return (parm
2982 && (TREE_CODE (parm) == TYPE_DECL
2983 || TREE_CODE (parm) == TEMPLATE_DECL)
2984 && DECL_TEMPLATE_PARM_P (parm));
2987 /* Return the template parameters of T if T is a
2988 primary template instantiation, NULL otherwise. */
2990 tree
2991 get_primary_template_innermost_parameters (const_tree t)
2993 tree parms = NULL, template_info = NULL;
2995 if ((template_info = get_template_info (t))
2996 && primary_template_instantiation_p (t))
2997 parms = INNERMOST_TEMPLATE_PARMS
2998 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3000 return parms;
3003 /* Return the template parameters of the LEVELth level from the full list
3004 of template parameters PARMS. */
3006 tree
3007 get_template_parms_at_level (tree parms, int level)
3009 tree p;
3010 if (!parms
3011 || TREE_CODE (parms) != TREE_LIST
3012 || level > TMPL_PARMS_DEPTH (parms))
3013 return NULL_TREE;
3015 for (p = parms; p; p = TREE_CHAIN (p))
3016 if (TMPL_PARMS_DEPTH (p) == level)
3017 return p;
3019 return NULL_TREE;
3022 /* Returns the template arguments of T if T is a template instantiation,
3023 NULL otherwise. */
3025 tree
3026 get_template_innermost_arguments (const_tree t)
3028 tree args = NULL, template_info = NULL;
3030 if ((template_info = get_template_info (t))
3031 && TI_ARGS (template_info))
3032 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3034 return args;
3037 /* Return the argument pack elements of T if T is a template argument pack,
3038 NULL otherwise. */
3040 tree
3041 get_template_argument_pack_elems (const_tree t)
3043 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3044 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3045 return NULL;
3047 return ARGUMENT_PACK_ARGS (t);
3050 /* Structure used to track the progress of find_parameter_packs_r. */
3051 struct find_parameter_pack_data
3053 /* TREE_LIST that will contain all of the parameter packs found by
3054 the traversal. */
3055 tree* parameter_packs;
3057 /* Set of AST nodes that have been visited by the traversal. */
3058 struct pointer_set_t *visited;
3061 /* Identifies all of the argument packs that occur in a template
3062 argument and appends them to the TREE_LIST inside DATA, which is a
3063 find_parameter_pack_data structure. This is a subroutine of
3064 make_pack_expansion and uses_parameter_packs. */
3065 static tree
3066 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3068 tree t = *tp;
3069 struct find_parameter_pack_data* ppd =
3070 (struct find_parameter_pack_data*)data;
3071 bool parameter_pack_p = false;
3073 /* Handle type aliases/typedefs. */
3074 if (TYPE_ALIAS_P (t))
3076 if (TYPE_TEMPLATE_INFO (t))
3077 cp_walk_tree (&TYPE_TI_ARGS (t),
3078 &find_parameter_packs_r,
3079 ppd, ppd->visited);
3080 *walk_subtrees = 0;
3081 return NULL_TREE;
3084 /* Identify whether this is a parameter pack or not. */
3085 switch (TREE_CODE (t))
3087 case TEMPLATE_PARM_INDEX:
3088 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3089 parameter_pack_p = true;
3090 break;
3092 case TEMPLATE_TYPE_PARM:
3093 t = TYPE_MAIN_VARIANT (t);
3094 case TEMPLATE_TEMPLATE_PARM:
3095 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3096 parameter_pack_p = true;
3097 break;
3099 case FIELD_DECL:
3100 case PARM_DECL:
3101 if (DECL_PACK_P (t))
3103 /* We don't want to walk into the type of a PARM_DECL,
3104 because we don't want to see the type parameter pack. */
3105 *walk_subtrees = 0;
3106 parameter_pack_p = true;
3108 break;
3110 /* Look through a lambda capture proxy to the field pack. */
3111 case VAR_DECL:
3112 if (DECL_HAS_VALUE_EXPR_P (t))
3114 tree v = DECL_VALUE_EXPR (t);
3115 cp_walk_tree (&v,
3116 &find_parameter_packs_r,
3117 ppd, ppd->visited);
3118 *walk_subtrees = 0;
3120 break;
3122 case BASES:
3123 parameter_pack_p = true;
3124 break;
3125 default:
3126 /* Not a parameter pack. */
3127 break;
3130 if (parameter_pack_p)
3132 /* Add this parameter pack to the list. */
3133 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3136 if (TYPE_P (t))
3137 cp_walk_tree (&TYPE_CONTEXT (t),
3138 &find_parameter_packs_r, ppd, ppd->visited);
3140 /* This switch statement will return immediately if we don't find a
3141 parameter pack. */
3142 switch (TREE_CODE (t))
3144 case TEMPLATE_PARM_INDEX:
3145 return NULL_TREE;
3147 case BOUND_TEMPLATE_TEMPLATE_PARM:
3148 /* Check the template itself. */
3149 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3150 &find_parameter_packs_r, ppd, ppd->visited);
3151 /* Check the template arguments. */
3152 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3153 ppd->visited);
3154 *walk_subtrees = 0;
3155 return NULL_TREE;
3157 case TEMPLATE_TYPE_PARM:
3158 case TEMPLATE_TEMPLATE_PARM:
3159 return NULL_TREE;
3161 case PARM_DECL:
3162 return NULL_TREE;
3164 case RECORD_TYPE:
3165 if (TYPE_PTRMEMFUNC_P (t))
3166 return NULL_TREE;
3167 /* Fall through. */
3169 case UNION_TYPE:
3170 case ENUMERAL_TYPE:
3171 if (TYPE_TEMPLATE_INFO (t))
3172 cp_walk_tree (&TYPE_TI_ARGS (t),
3173 &find_parameter_packs_r, ppd, ppd->visited);
3175 *walk_subtrees = 0;
3176 return NULL_TREE;
3178 case CONSTRUCTOR:
3179 case TEMPLATE_DECL:
3180 cp_walk_tree (&TREE_TYPE (t),
3181 &find_parameter_packs_r, ppd, ppd->visited);
3182 return NULL_TREE;
3184 case TYPENAME_TYPE:
3185 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3186 ppd, ppd->visited);
3187 *walk_subtrees = 0;
3188 return NULL_TREE;
3190 case TYPE_PACK_EXPANSION:
3191 case EXPR_PACK_EXPANSION:
3192 *walk_subtrees = 0;
3193 return NULL_TREE;
3195 case INTEGER_TYPE:
3196 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3197 ppd, ppd->visited);
3198 *walk_subtrees = 0;
3199 return NULL_TREE;
3201 case IDENTIFIER_NODE:
3202 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3203 ppd->visited);
3204 *walk_subtrees = 0;
3205 return NULL_TREE;
3207 default:
3208 return NULL_TREE;
3211 return NULL_TREE;
3214 /* Determines if the expression or type T uses any parameter packs. */
3215 bool
3216 uses_parameter_packs (tree t)
3218 tree parameter_packs = NULL_TREE;
3219 struct find_parameter_pack_data ppd;
3220 ppd.parameter_packs = &parameter_packs;
3221 ppd.visited = pointer_set_create ();
3222 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3223 pointer_set_destroy (ppd.visited);
3224 return parameter_packs != NULL_TREE;
3227 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3228 representation a base-class initializer into a parameter pack
3229 expansion. If all goes well, the resulting node will be an
3230 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3231 respectively. */
3232 tree
3233 make_pack_expansion (tree arg)
3235 tree result;
3236 tree parameter_packs = NULL_TREE;
3237 bool for_types = false;
3238 struct find_parameter_pack_data ppd;
3240 if (!arg || arg == error_mark_node)
3241 return arg;
3243 if (TREE_CODE (arg) == TREE_LIST)
3245 /* The only time we will see a TREE_LIST here is for a base
3246 class initializer. In this case, the TREE_PURPOSE will be a
3247 _TYPE node (representing the base class expansion we're
3248 initializing) and the TREE_VALUE will be a TREE_LIST
3249 containing the initialization arguments.
3251 The resulting expansion looks somewhat different from most
3252 expansions. Rather than returning just one _EXPANSION, we
3253 return a TREE_LIST whose TREE_PURPOSE is a
3254 TYPE_PACK_EXPANSION containing the bases that will be
3255 initialized. The TREE_VALUE will be identical to the
3256 original TREE_VALUE, which is a list of arguments that will
3257 be passed to each base. We do not introduce any new pack
3258 expansion nodes into the TREE_VALUE (although it is possible
3259 that some already exist), because the TREE_PURPOSE and
3260 TREE_VALUE all need to be expanded together with the same
3261 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3262 resulting TREE_PURPOSE will mention the parameter packs in
3263 both the bases and the arguments to the bases. */
3264 tree purpose;
3265 tree value;
3266 tree parameter_packs = NULL_TREE;
3268 /* Determine which parameter packs will be used by the base
3269 class expansion. */
3270 ppd.visited = pointer_set_create ();
3271 ppd.parameter_packs = &parameter_packs;
3272 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3273 &ppd, ppd.visited);
3275 if (parameter_packs == NULL_TREE)
3277 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3278 pointer_set_destroy (ppd.visited);
3279 return error_mark_node;
3282 if (TREE_VALUE (arg) != void_type_node)
3284 /* Collect the sets of parameter packs used in each of the
3285 initialization arguments. */
3286 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3288 /* Determine which parameter packs will be expanded in this
3289 argument. */
3290 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3291 &ppd, ppd.visited);
3295 pointer_set_destroy (ppd.visited);
3297 /* Create the pack expansion type for the base type. */
3298 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3299 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3300 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3302 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3303 they will rarely be compared to anything. */
3304 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3306 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3309 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3310 for_types = true;
3312 /* Build the PACK_EXPANSION_* node. */
3313 result = for_types
3314 ? cxx_make_type (TYPE_PACK_EXPANSION)
3315 : make_node (EXPR_PACK_EXPANSION);
3316 SET_PACK_EXPANSION_PATTERN (result, arg);
3317 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3319 /* Propagate type and const-expression information. */
3320 TREE_TYPE (result) = TREE_TYPE (arg);
3321 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3323 else
3324 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3325 they will rarely be compared to anything. */
3326 SET_TYPE_STRUCTURAL_EQUALITY (result);
3328 /* Determine which parameter packs will be expanded. */
3329 ppd.parameter_packs = &parameter_packs;
3330 ppd.visited = pointer_set_create ();
3331 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3332 pointer_set_destroy (ppd.visited);
3334 /* Make sure we found some parameter packs. */
3335 if (parameter_packs == NULL_TREE)
3337 if (TYPE_P (arg))
3338 error ("expansion pattern %<%T%> contains no argument packs", arg);
3339 else
3340 error ("expansion pattern %<%E%> contains no argument packs", arg);
3341 return error_mark_node;
3343 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3345 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3347 return result;
3350 /* Checks T for any "bare" parameter packs, which have not yet been
3351 expanded, and issues an error if any are found. This operation can
3352 only be done on full expressions or types (e.g., an expression
3353 statement, "if" condition, etc.), because we could have expressions like:
3355 foo(f(g(h(args)))...)
3357 where "args" is a parameter pack. check_for_bare_parameter_packs
3358 should not be called for the subexpressions args, h(args),
3359 g(h(args)), or f(g(h(args))), because we would produce erroneous
3360 error messages.
3362 Returns TRUE and emits an error if there were bare parameter packs,
3363 returns FALSE otherwise. */
3364 bool
3365 check_for_bare_parameter_packs (tree t)
3367 tree parameter_packs = NULL_TREE;
3368 struct find_parameter_pack_data ppd;
3370 if (!processing_template_decl || !t || t == error_mark_node)
3371 return false;
3373 if (TREE_CODE (t) == TYPE_DECL)
3374 t = TREE_TYPE (t);
3376 ppd.parameter_packs = &parameter_packs;
3377 ppd.visited = pointer_set_create ();
3378 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3379 pointer_set_destroy (ppd.visited);
3381 if (parameter_packs)
3383 error ("parameter packs not expanded with %<...%>:");
3384 while (parameter_packs)
3386 tree pack = TREE_VALUE (parameter_packs);
3387 tree name = NULL_TREE;
3389 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3390 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3391 name = TYPE_NAME (pack);
3392 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3393 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3394 else
3395 name = DECL_NAME (pack);
3397 if (name)
3398 inform (input_location, " %qD", name);
3399 else
3400 inform (input_location, " <anonymous>");
3402 parameter_packs = TREE_CHAIN (parameter_packs);
3405 return true;
3408 return false;
3411 /* Expand any parameter packs that occur in the template arguments in
3412 ARGS. */
3413 tree
3414 expand_template_argument_pack (tree args)
3416 tree result_args = NULL_TREE;
3417 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3418 int num_result_args = -1;
3419 int non_default_args_count = -1;
3421 /* First, determine if we need to expand anything, and the number of
3422 slots we'll need. */
3423 for (in_arg = 0; in_arg < nargs; ++in_arg)
3425 tree arg = TREE_VEC_ELT (args, in_arg);
3426 if (arg == NULL_TREE)
3427 return args;
3428 if (ARGUMENT_PACK_P (arg))
3430 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3431 if (num_result_args < 0)
3432 num_result_args = in_arg + num_packed;
3433 else
3434 num_result_args += num_packed;
3436 else
3438 if (num_result_args >= 0)
3439 num_result_args++;
3443 /* If no expansion is necessary, we're done. */
3444 if (num_result_args < 0)
3445 return args;
3447 /* Expand arguments. */
3448 result_args = make_tree_vec (num_result_args);
3449 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3450 non_default_args_count =
3451 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3452 for (in_arg = 0; in_arg < nargs; ++in_arg)
3454 tree arg = TREE_VEC_ELT (args, in_arg);
3455 if (ARGUMENT_PACK_P (arg))
3457 tree packed = ARGUMENT_PACK_ARGS (arg);
3458 int i, num_packed = TREE_VEC_LENGTH (packed);
3459 for (i = 0; i < num_packed; ++i, ++out_arg)
3460 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3461 if (non_default_args_count > 0)
3462 non_default_args_count += num_packed;
3464 else
3466 TREE_VEC_ELT (result_args, out_arg) = arg;
3467 ++out_arg;
3470 if (non_default_args_count >= 0)
3471 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3472 return result_args;
3475 /* Checks if DECL shadows a template parameter.
3477 [temp.local]: A template-parameter shall not be redeclared within its
3478 scope (including nested scopes).
3480 Emits an error and returns TRUE if the DECL shadows a parameter,
3481 returns FALSE otherwise. */
3483 bool
3484 check_template_shadow (tree decl)
3486 tree olddecl;
3488 /* If we're not in a template, we can't possibly shadow a template
3489 parameter. */
3490 if (!current_template_parms)
3491 return true;
3493 /* Figure out what we're shadowing. */
3494 if (TREE_CODE (decl) == OVERLOAD)
3495 decl = OVL_CURRENT (decl);
3496 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3498 /* If there's no previous binding for this name, we're not shadowing
3499 anything, let alone a template parameter. */
3500 if (!olddecl)
3501 return true;
3503 /* If we're not shadowing a template parameter, we're done. Note
3504 that OLDDECL might be an OVERLOAD (or perhaps even an
3505 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3506 node. */
3507 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3508 return true;
3510 /* We check for decl != olddecl to avoid bogus errors for using a
3511 name inside a class. We check TPFI to avoid duplicate errors for
3512 inline member templates. */
3513 if (decl == olddecl
3514 || (DECL_TEMPLATE_PARM_P (decl)
3515 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3516 return true;
3518 error ("declaration of %q+#D", decl);
3519 error (" shadows template parm %q+#D", olddecl);
3520 return false;
3523 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3524 ORIG_LEVEL, DECL, and TYPE. */
3526 static tree
3527 build_template_parm_index (int index,
3528 int level,
3529 int orig_level,
3530 tree decl,
3531 tree type)
3533 tree t = make_node (TEMPLATE_PARM_INDEX);
3534 TEMPLATE_PARM_IDX (t) = index;
3535 TEMPLATE_PARM_LEVEL (t) = level;
3536 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3537 TEMPLATE_PARM_DECL (t) = decl;
3538 TREE_TYPE (t) = type;
3539 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3540 TREE_READONLY (t) = TREE_READONLY (decl);
3542 return t;
3545 /* Find the canonical type parameter for the given template type
3546 parameter. Returns the canonical type parameter, which may be TYPE
3547 if no such parameter existed. */
3549 static tree
3550 canonical_type_parameter (tree type)
3552 tree list;
3553 int idx = TEMPLATE_TYPE_IDX (type);
3554 if (!canonical_template_parms)
3555 vec_alloc (canonical_template_parms, idx+1);
3557 while (canonical_template_parms->length () <= (unsigned)idx)
3558 vec_safe_push (canonical_template_parms, NULL_TREE);
3560 list = (*canonical_template_parms)[idx];
3561 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3562 list = TREE_CHAIN (list);
3564 if (list)
3565 return TREE_VALUE (list);
3566 else
3568 (*canonical_template_parms)[idx]
3569 = tree_cons (NULL_TREE, type,
3570 (*canonical_template_parms)[idx]);
3571 return type;
3575 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3576 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3577 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3578 new one is created. */
3580 static tree
3581 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3582 tsubst_flags_t complain)
3584 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3585 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3586 != TEMPLATE_PARM_LEVEL (index) - levels)
3587 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3589 tree orig_decl = TEMPLATE_PARM_DECL (index);
3590 tree decl, t;
3592 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3593 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3594 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3595 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3596 DECL_ARTIFICIAL (decl) = 1;
3597 SET_DECL_TEMPLATE_PARM_P (decl);
3599 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3600 TEMPLATE_PARM_LEVEL (index) - levels,
3601 TEMPLATE_PARM_ORIG_LEVEL (index),
3602 decl, type);
3603 TEMPLATE_PARM_DESCENDANTS (index) = t;
3604 TEMPLATE_PARM_PARAMETER_PACK (t)
3605 = TEMPLATE_PARM_PARAMETER_PACK (index);
3607 /* Template template parameters need this. */
3608 if (TREE_CODE (decl) == TEMPLATE_DECL)
3609 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3610 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3611 args, complain);
3614 return TEMPLATE_PARM_DESCENDANTS (index);
3617 /* Process information from new template parameter PARM and append it
3618 to the LIST being built. This new parameter is a non-type
3619 parameter iff IS_NON_TYPE is true. This new parameter is a
3620 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3621 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3622 parameter list PARM belongs to. This is used used to create a
3623 proper canonical type for the type of PARM that is to be created,
3624 iff PARM is a type. If the size is not known, this parameter shall
3625 be set to 0. */
3627 tree
3628 process_template_parm (tree list, location_t parm_loc, tree parm,
3629 bool is_non_type, bool is_parameter_pack)
3631 tree decl = 0;
3632 tree defval;
3633 tree err_parm_list;
3634 int idx = 0;
3636 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3637 defval = TREE_PURPOSE (parm);
3639 if (list)
3641 tree p = tree_last (list);
3643 if (p && TREE_VALUE (p) != error_mark_node)
3645 p = TREE_VALUE (p);
3646 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3647 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3648 else
3649 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3652 ++idx;
3654 else
3655 idx = 0;
3657 if (is_non_type)
3659 parm = TREE_VALUE (parm);
3661 SET_DECL_TEMPLATE_PARM_P (parm);
3663 if (TREE_TYPE (parm) == error_mark_node)
3665 err_parm_list = build_tree_list (defval, parm);
3666 TREE_VALUE (err_parm_list) = error_mark_node;
3667 return chainon (list, err_parm_list);
3669 else
3671 /* [temp.param]
3673 The top-level cv-qualifiers on the template-parameter are
3674 ignored when determining its type. */
3675 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3676 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3678 err_parm_list = build_tree_list (defval, parm);
3679 TREE_VALUE (err_parm_list) = error_mark_node;
3680 return chainon (list, err_parm_list);
3683 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3685 /* This template parameter is not a parameter pack, but it
3686 should be. Complain about "bare" parameter packs. */
3687 check_for_bare_parameter_packs (TREE_TYPE (parm));
3689 /* Recover by calling this a parameter pack. */
3690 is_parameter_pack = true;
3694 /* A template parameter is not modifiable. */
3695 TREE_CONSTANT (parm) = 1;
3696 TREE_READONLY (parm) = 1;
3697 decl = build_decl (parm_loc,
3698 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3699 TREE_CONSTANT (decl) = 1;
3700 TREE_READONLY (decl) = 1;
3701 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3702 = build_template_parm_index (idx, processing_template_decl,
3703 processing_template_decl,
3704 decl, TREE_TYPE (parm));
3706 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3707 = is_parameter_pack;
3709 else
3711 tree t;
3712 parm = TREE_VALUE (TREE_VALUE (parm));
3714 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3716 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3717 /* This is for distinguishing between real templates and template
3718 template parameters */
3719 TREE_TYPE (parm) = t;
3720 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3721 decl = parm;
3723 else
3725 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3726 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3727 decl = build_decl (parm_loc,
3728 TYPE_DECL, parm, t);
3731 TYPE_NAME (t) = decl;
3732 TYPE_STUB_DECL (t) = decl;
3733 parm = decl;
3734 TEMPLATE_TYPE_PARM_INDEX (t)
3735 = build_template_parm_index (idx, processing_template_decl,
3736 processing_template_decl,
3737 decl, TREE_TYPE (parm));
3738 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3739 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3741 DECL_ARTIFICIAL (decl) = 1;
3742 SET_DECL_TEMPLATE_PARM_P (decl);
3743 pushdecl (decl);
3744 parm = build_tree_list (defval, parm);
3745 return chainon (list, parm);
3748 /* The end of a template parameter list has been reached. Process the
3749 tree list into a parameter vector, converting each parameter into a more
3750 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3751 as PARM_DECLs. */
3753 tree
3754 end_template_parm_list (tree parms)
3756 int nparms;
3757 tree parm, next;
3758 tree saved_parmlist = make_tree_vec (list_length (parms));
3760 current_template_parms
3761 = tree_cons (size_int (processing_template_decl),
3762 saved_parmlist, current_template_parms);
3764 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3766 next = TREE_CHAIN (parm);
3767 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3768 TREE_CHAIN (parm) = NULL_TREE;
3771 --processing_template_parmlist;
3773 return saved_parmlist;
3776 /* end_template_decl is called after a template declaration is seen. */
3778 void
3779 end_template_decl (void)
3781 reset_specialization ();
3783 if (! processing_template_decl)
3784 return;
3786 /* This matches the pushlevel in begin_template_parm_list. */
3787 finish_scope ();
3789 --processing_template_decl;
3790 current_template_parms = TREE_CHAIN (current_template_parms);
3793 /* Takes a TREE_LIST representing a template parameter and convert it
3794 into an argument suitable to be passed to the type substitution
3795 functions. Note that If the TREE_LIST contains an error_mark
3796 node, the returned argument is error_mark_node. */
3798 static tree
3799 template_parm_to_arg (tree t)
3802 if (t == NULL_TREE
3803 || TREE_CODE (t) != TREE_LIST)
3804 return t;
3806 if (error_operand_p (TREE_VALUE (t)))
3807 return error_mark_node;
3809 t = TREE_VALUE (t);
3811 if (TREE_CODE (t) == TYPE_DECL
3812 || TREE_CODE (t) == TEMPLATE_DECL)
3814 t = TREE_TYPE (t);
3816 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3818 /* Turn this argument into a TYPE_ARGUMENT_PACK
3819 with a single element, which expands T. */
3820 tree vec = make_tree_vec (1);
3821 #ifdef ENABLE_CHECKING
3822 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3823 (vec, TREE_VEC_LENGTH (vec));
3824 #endif
3825 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3827 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3828 SET_ARGUMENT_PACK_ARGS (t, vec);
3831 else
3833 t = DECL_INITIAL (t);
3835 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3837 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3838 with a single element, which expands T. */
3839 tree vec = make_tree_vec (1);
3840 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3841 #ifdef ENABLE_CHECKING
3842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3843 (vec, TREE_VEC_LENGTH (vec));
3844 #endif
3845 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3847 t = make_node (NONTYPE_ARGUMENT_PACK);
3848 SET_ARGUMENT_PACK_ARGS (t, vec);
3849 TREE_TYPE (t) = type;
3852 return t;
3855 /* Given a set of template parameters, return them as a set of template
3856 arguments. The template parameters are represented as a TREE_VEC, in
3857 the form documented in cp-tree.h for template arguments. */
3859 static tree
3860 template_parms_to_args (tree parms)
3862 tree header;
3863 tree args = NULL_TREE;
3864 int length = TMPL_PARMS_DEPTH (parms);
3865 int l = length;
3867 /* If there is only one level of template parameters, we do not
3868 create a TREE_VEC of TREE_VECs. Instead, we return a single
3869 TREE_VEC containing the arguments. */
3870 if (length > 1)
3871 args = make_tree_vec (length);
3873 for (header = parms; header; header = TREE_CHAIN (header))
3875 tree a = copy_node (TREE_VALUE (header));
3876 int i;
3878 TREE_TYPE (a) = NULL_TREE;
3879 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3880 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3882 #ifdef ENABLE_CHECKING
3883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3884 #endif
3886 if (length > 1)
3887 TREE_VEC_ELT (args, --l) = a;
3888 else
3889 args = a;
3892 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3893 /* This can happen for template parms of a template template
3894 parameter, e.g:
3896 template<template<class T, class U> class TT> struct S;
3898 Consider the level of the parms of TT; T and U both have
3899 level 2; TT has no template parm of level 1. So in this case
3900 the first element of full_template_args is NULL_TREE. If we
3901 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3902 of 2. This will make tsubst wrongly consider that T and U
3903 have level 1. Instead, let's create a dummy vector as the
3904 first element of full_template_args so that TMPL_ARGS_DEPTH
3905 returns the correct depth for args. */
3906 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3907 return args;
3910 /* Within the declaration of a template, return the currently active
3911 template parameters as an argument TREE_VEC. */
3913 static tree
3914 current_template_args (void)
3916 return template_parms_to_args (current_template_parms);
3919 /* Update the declared TYPE by doing any lookups which were thought to be
3920 dependent, but are not now that we know the SCOPE of the declarator. */
3922 tree
3923 maybe_update_decl_type (tree orig_type, tree scope)
3925 tree type = orig_type;
3927 if (type == NULL_TREE)
3928 return type;
3930 if (TREE_CODE (orig_type) == TYPE_DECL)
3931 type = TREE_TYPE (type);
3933 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3934 && dependent_type_p (type)
3935 /* Don't bother building up the args in this case. */
3936 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3938 /* tsubst in the args corresponding to the template parameters,
3939 including auto if present. Most things will be unchanged, but
3940 make_typename_type and tsubst_qualified_id will resolve
3941 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3942 tree args = current_template_args ();
3943 tree auto_node = type_uses_auto (type);
3944 tree pushed;
3945 if (auto_node)
3947 tree auto_vec = make_tree_vec (1);
3948 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3949 args = add_to_template_args (args, auto_vec);
3951 pushed = push_scope (scope);
3952 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3953 if (pushed)
3954 pop_scope (scope);
3957 if (type == error_mark_node)
3958 return orig_type;
3960 if (TREE_CODE (orig_type) == TYPE_DECL)
3962 if (same_type_p (type, TREE_TYPE (orig_type)))
3963 type = orig_type;
3964 else
3965 type = TYPE_NAME (type);
3967 return type;
3970 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3971 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3972 a member template. Used by push_template_decl below. */
3974 static tree
3975 build_template_decl (tree decl, tree parms, bool member_template_p)
3977 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3978 DECL_TEMPLATE_PARMS (tmpl) = parms;
3979 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3980 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3981 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3983 return tmpl;
3986 struct template_parm_data
3988 /* The level of the template parameters we are currently
3989 processing. */
3990 int level;
3992 /* The index of the specialization argument we are currently
3993 processing. */
3994 int current_arg;
3996 /* An array whose size is the number of template parameters. The
3997 elements are nonzero if the parameter has been used in any one
3998 of the arguments processed so far. */
3999 int* parms;
4001 /* An array whose size is the number of template arguments. The
4002 elements are nonzero if the argument makes use of template
4003 parameters of this level. */
4004 int* arg_uses_template_parms;
4007 /* Subroutine of push_template_decl used to see if each template
4008 parameter in a partial specialization is used in the explicit
4009 argument list. If T is of the LEVEL given in DATA (which is
4010 treated as a template_parm_data*), then DATA->PARMS is marked
4011 appropriately. */
4013 static int
4014 mark_template_parm (tree t, void* data)
4016 int level;
4017 int idx;
4018 struct template_parm_data* tpd = (struct template_parm_data*) data;
4020 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4022 level = TEMPLATE_PARM_LEVEL (t);
4023 idx = TEMPLATE_PARM_IDX (t);
4025 else
4027 level = TEMPLATE_TYPE_LEVEL (t);
4028 idx = TEMPLATE_TYPE_IDX (t);
4031 if (level == tpd->level)
4033 tpd->parms[idx] = 1;
4034 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4037 /* Return zero so that for_each_template_parm will continue the
4038 traversal of the tree; we want to mark *every* template parm. */
4039 return 0;
4042 /* Process the partial specialization DECL. */
4044 static tree
4045 process_partial_specialization (tree decl)
4047 tree type = TREE_TYPE (decl);
4048 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4049 tree specargs = CLASSTYPE_TI_ARGS (type);
4050 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4051 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4052 tree inner_parms;
4053 tree inst;
4054 int nargs = TREE_VEC_LENGTH (inner_args);
4055 int ntparms;
4056 int i;
4057 bool did_error_intro = false;
4058 struct template_parm_data tpd;
4059 struct template_parm_data tpd2;
4061 gcc_assert (current_template_parms);
4063 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4064 ntparms = TREE_VEC_LENGTH (inner_parms);
4066 /* We check that each of the template parameters given in the
4067 partial specialization is used in the argument list to the
4068 specialization. For example:
4070 template <class T> struct S;
4071 template <class T> struct S<T*>;
4073 The second declaration is OK because `T*' uses the template
4074 parameter T, whereas
4076 template <class T> struct S<int>;
4078 is no good. Even trickier is:
4080 template <class T>
4081 struct S1
4083 template <class U>
4084 struct S2;
4085 template <class U>
4086 struct S2<T>;
4089 The S2<T> declaration is actually invalid; it is a
4090 full-specialization. Of course,
4092 template <class U>
4093 struct S2<T (*)(U)>;
4095 or some such would have been OK. */
4096 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4097 tpd.parms = XALLOCAVEC (int, ntparms);
4098 memset (tpd.parms, 0, sizeof (int) * ntparms);
4100 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4101 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4102 for (i = 0; i < nargs; ++i)
4104 tpd.current_arg = i;
4105 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4106 &mark_template_parm,
4107 &tpd,
4108 NULL,
4109 /*include_nondeduced_p=*/false);
4111 for (i = 0; i < ntparms; ++i)
4112 if (tpd.parms[i] == 0)
4114 /* One of the template parms was not used in the
4115 specialization. */
4116 if (!did_error_intro)
4118 error ("template parameters not used in partial specialization:");
4119 did_error_intro = true;
4122 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4125 if (did_error_intro)
4126 return error_mark_node;
4128 /* [temp.class.spec]
4130 The argument list of the specialization shall not be identical to
4131 the implicit argument list of the primary template. */
4132 if (comp_template_args
4133 (inner_args,
4134 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4135 (maintmpl)))))
4136 error ("partial specialization %qT does not specialize any template arguments", type);
4138 /* A partial specialization that replaces multiple parameters of the
4139 primary template with a pack expansion is less specialized for those
4140 parameters. */
4141 if (nargs < DECL_NTPARMS (maintmpl))
4143 error ("partial specialization is not more specialized than the "
4144 "primary template because it replaces multiple parameters "
4145 "with a pack expansion");
4146 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4147 return decl;
4150 /* [temp.class.spec]
4152 A partially specialized non-type argument expression shall not
4153 involve template parameters of the partial specialization except
4154 when the argument expression is a simple identifier.
4156 The type of a template parameter corresponding to a specialized
4157 non-type argument shall not be dependent on a parameter of the
4158 specialization.
4160 Also, we verify that pack expansions only occur at the
4161 end of the argument list. */
4162 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4163 tpd2.parms = 0;
4164 for (i = 0; i < nargs; ++i)
4166 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4167 tree arg = TREE_VEC_ELT (inner_args, i);
4168 tree packed_args = NULL_TREE;
4169 int j, len = 1;
4171 if (ARGUMENT_PACK_P (arg))
4173 /* Extract the arguments from the argument pack. We'll be
4174 iterating over these in the following loop. */
4175 packed_args = ARGUMENT_PACK_ARGS (arg);
4176 len = TREE_VEC_LENGTH (packed_args);
4179 for (j = 0; j < len; j++)
4181 if (packed_args)
4182 /* Get the Jth argument in the parameter pack. */
4183 arg = TREE_VEC_ELT (packed_args, j);
4185 if (PACK_EXPANSION_P (arg))
4187 /* Pack expansions must come at the end of the
4188 argument list. */
4189 if ((packed_args && j < len - 1)
4190 || (!packed_args && i < nargs - 1))
4192 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4193 error ("parameter pack argument %qE must be at the "
4194 "end of the template argument list", arg);
4195 else
4196 error ("parameter pack argument %qT must be at the "
4197 "end of the template argument list", arg);
4201 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4202 /* We only care about the pattern. */
4203 arg = PACK_EXPANSION_PATTERN (arg);
4205 if (/* These first two lines are the `non-type' bit. */
4206 !TYPE_P (arg)
4207 && TREE_CODE (arg) != TEMPLATE_DECL
4208 /* This next line is the `argument expression is not just a
4209 simple identifier' condition and also the `specialized
4210 non-type argument' bit. */
4211 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4213 if ((!packed_args && tpd.arg_uses_template_parms[i])
4214 || (packed_args && uses_template_parms (arg)))
4215 error ("template argument %qE involves template parameter(s)",
4216 arg);
4217 else
4219 /* Look at the corresponding template parameter,
4220 marking which template parameters its type depends
4221 upon. */
4222 tree type = TREE_TYPE (parm);
4224 if (!tpd2.parms)
4226 /* We haven't yet initialized TPD2. Do so now. */
4227 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4228 /* The number of parameters here is the number in the
4229 main template, which, as checked in the assertion
4230 above, is NARGS. */
4231 tpd2.parms = XALLOCAVEC (int, nargs);
4232 tpd2.level =
4233 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4236 /* Mark the template parameters. But this time, we're
4237 looking for the template parameters of the main
4238 template, not in the specialization. */
4239 tpd2.current_arg = i;
4240 tpd2.arg_uses_template_parms[i] = 0;
4241 memset (tpd2.parms, 0, sizeof (int) * nargs);
4242 for_each_template_parm (type,
4243 &mark_template_parm,
4244 &tpd2,
4245 NULL,
4246 /*include_nondeduced_p=*/false);
4248 if (tpd2.arg_uses_template_parms [i])
4250 /* The type depended on some template parameters.
4251 If they are fully specialized in the
4252 specialization, that's OK. */
4253 int j;
4254 int count = 0;
4255 for (j = 0; j < nargs; ++j)
4256 if (tpd2.parms[j] != 0
4257 && tpd.arg_uses_template_parms [j])
4258 ++count;
4259 if (count != 0)
4260 error_n (input_location, count,
4261 "type %qT of template argument %qE depends "
4262 "on a template parameter",
4263 "type %qT of template argument %qE depends "
4264 "on template parameters",
4265 type,
4266 arg);
4273 /* We should only get here once. */
4274 gcc_assert (!COMPLETE_TYPE_P (type));
4276 tree tmpl = build_template_decl (decl, current_template_parms,
4277 DECL_MEMBER_TEMPLATE_P (maintmpl));
4278 TREE_TYPE (tmpl) = type;
4279 DECL_TEMPLATE_RESULT (tmpl) = decl;
4280 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4281 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4282 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4284 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4285 = tree_cons (specargs, tmpl,
4286 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4287 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4289 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4290 inst = TREE_CHAIN (inst))
4292 tree inst_type = TREE_VALUE (inst);
4293 if (COMPLETE_TYPE_P (inst_type)
4294 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4296 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4297 if (spec && TREE_TYPE (spec) == type)
4298 permerror (input_location,
4299 "partial specialization of %qT after instantiation "
4300 "of %qT", type, inst_type);
4304 return decl;
4307 /* Check that a template declaration's use of default arguments and
4308 parameter packs is not invalid. Here, PARMS are the template
4309 parameters. IS_PRIMARY is true if DECL is the thing declared by
4310 a primary template. IS_PARTIAL is true if DECL is a partial
4311 specialization.
4313 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4314 declaration (but not a definition); 1 indicates a declaration, 2
4315 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4316 emitted for extraneous default arguments.
4318 Returns TRUE if there were no errors found, FALSE otherwise. */
4320 bool
4321 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4322 bool is_partial, int is_friend_decl)
4324 const char *msg;
4325 int last_level_to_check;
4326 tree parm_level;
4327 bool no_errors = true;
4329 /* [temp.param]
4331 A default template-argument shall not be specified in a
4332 function template declaration or a function template definition, nor
4333 in the template-parameter-list of the definition of a member of a
4334 class template. */
4336 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4337 /* You can't have a function template declaration in a local
4338 scope, nor you can you define a member of a class template in a
4339 local scope. */
4340 return true;
4342 if (TREE_CODE (decl) == TYPE_DECL
4343 && TREE_TYPE (decl)
4344 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4345 /* A lambda doesn't have an explicit declaration; don't complain
4346 about the parms of the enclosing class. */
4347 return true;
4349 if (current_class_type
4350 && !TYPE_BEING_DEFINED (current_class_type)
4351 && DECL_LANG_SPECIFIC (decl)
4352 && DECL_DECLARES_FUNCTION_P (decl)
4353 /* If this is either a friend defined in the scope of the class
4354 or a member function. */
4355 && (DECL_FUNCTION_MEMBER_P (decl)
4356 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4357 : DECL_FRIEND_CONTEXT (decl)
4358 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4359 : false)
4360 /* And, if it was a member function, it really was defined in
4361 the scope of the class. */
4362 && (!DECL_FUNCTION_MEMBER_P (decl)
4363 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4364 /* We already checked these parameters when the template was
4365 declared, so there's no need to do it again now. This function
4366 was defined in class scope, but we're processing its body now
4367 that the class is complete. */
4368 return true;
4370 /* Core issue 226 (C++0x only): the following only applies to class
4371 templates. */
4372 if (is_primary
4373 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4375 /* [temp.param]
4377 If a template-parameter has a default template-argument, all
4378 subsequent template-parameters shall have a default
4379 template-argument supplied. */
4380 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4382 tree inner_parms = TREE_VALUE (parm_level);
4383 int ntparms = TREE_VEC_LENGTH (inner_parms);
4384 int seen_def_arg_p = 0;
4385 int i;
4387 for (i = 0; i < ntparms; ++i)
4389 tree parm = TREE_VEC_ELT (inner_parms, i);
4391 if (parm == error_mark_node)
4392 continue;
4394 if (TREE_PURPOSE (parm))
4395 seen_def_arg_p = 1;
4396 else if (seen_def_arg_p
4397 && !template_parameter_pack_p (TREE_VALUE (parm)))
4399 error ("no default argument for %qD", TREE_VALUE (parm));
4400 /* For better subsequent error-recovery, we indicate that
4401 there should have been a default argument. */
4402 TREE_PURPOSE (parm) = error_mark_node;
4403 no_errors = false;
4405 else if (!is_partial
4406 && !is_friend_decl
4407 /* Don't complain about an enclosing partial
4408 specialization. */
4409 && parm_level == parms
4410 && TREE_CODE (decl) == TYPE_DECL
4411 && i < ntparms - 1
4412 && template_parameter_pack_p (TREE_VALUE (parm)))
4414 /* A primary class template can only have one
4415 parameter pack, at the end of the template
4416 parameter list. */
4418 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4419 error ("parameter pack %qE must be at the end of the"
4420 " template parameter list", TREE_VALUE (parm));
4421 else
4422 error ("parameter pack %qT must be at the end of the"
4423 " template parameter list",
4424 TREE_TYPE (TREE_VALUE (parm)));
4426 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4427 = error_mark_node;
4428 no_errors = false;
4434 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4435 || is_partial
4436 || !is_primary
4437 || is_friend_decl)
4438 /* For an ordinary class template, default template arguments are
4439 allowed at the innermost level, e.g.:
4440 template <class T = int>
4441 struct S {};
4442 but, in a partial specialization, they're not allowed even
4443 there, as we have in [temp.class.spec]:
4445 The template parameter list of a specialization shall not
4446 contain default template argument values.
4448 So, for a partial specialization, or for a function template
4449 (in C++98/C++03), we look at all of them. */
4451 else
4452 /* But, for a primary class template that is not a partial
4453 specialization we look at all template parameters except the
4454 innermost ones. */
4455 parms = TREE_CHAIN (parms);
4457 /* Figure out what error message to issue. */
4458 if (is_friend_decl == 2)
4459 msg = G_("default template arguments may not be used in function template "
4460 "friend re-declaration");
4461 else if (is_friend_decl)
4462 msg = G_("default template arguments may not be used in function template "
4463 "friend declarations");
4464 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4465 msg = G_("default template arguments may not be used in function templates "
4466 "without -std=c++11 or -std=gnu++11");
4467 else if (is_partial)
4468 msg = G_("default template arguments may not be used in "
4469 "partial specializations");
4470 else
4471 msg = G_("default argument for template parameter for class enclosing %qD");
4473 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4474 /* If we're inside a class definition, there's no need to
4475 examine the parameters to the class itself. On the one
4476 hand, they will be checked when the class is defined, and,
4477 on the other, default arguments are valid in things like:
4478 template <class T = double>
4479 struct S { template <class U> void f(U); };
4480 Here the default argument for `S' has no bearing on the
4481 declaration of `f'. */
4482 last_level_to_check = template_class_depth (current_class_type) + 1;
4483 else
4484 /* Check everything. */
4485 last_level_to_check = 0;
4487 for (parm_level = parms;
4488 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4489 parm_level = TREE_CHAIN (parm_level))
4491 tree inner_parms = TREE_VALUE (parm_level);
4492 int i;
4493 int ntparms;
4495 ntparms = TREE_VEC_LENGTH (inner_parms);
4496 for (i = 0; i < ntparms; ++i)
4498 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4499 continue;
4501 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4503 if (msg)
4505 no_errors = false;
4506 if (is_friend_decl == 2)
4507 return no_errors;
4509 error (msg, decl);
4510 msg = 0;
4513 /* Clear out the default argument so that we are not
4514 confused later. */
4515 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4519 /* At this point, if we're still interested in issuing messages,
4520 they must apply to classes surrounding the object declared. */
4521 if (msg)
4522 msg = G_("default argument for template parameter for class "
4523 "enclosing %qD");
4526 return no_errors;
4529 /* Worker for push_template_decl_real, called via
4530 for_each_template_parm. DATA is really an int, indicating the
4531 level of the parameters we are interested in. If T is a template
4532 parameter of that level, return nonzero. */
4534 static int
4535 template_parm_this_level_p (tree t, void* data)
4537 int this_level = *(int *)data;
4538 int level;
4540 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4541 level = TEMPLATE_PARM_LEVEL (t);
4542 else
4543 level = TEMPLATE_TYPE_LEVEL (t);
4544 return level == this_level;
4547 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4548 parameters given by current_template_args, or reuses a
4549 previously existing one, if appropriate. Returns the DECL, or an
4550 equivalent one, if it is replaced via a call to duplicate_decls.
4552 If IS_FRIEND is true, DECL is a friend declaration. */
4554 tree
4555 push_template_decl_real (tree decl, bool is_friend)
4557 tree tmpl;
4558 tree args;
4559 tree info;
4560 tree ctx;
4561 bool is_primary;
4562 bool is_partial;
4563 int new_template_p = 0;
4564 /* True if the template is a member template, in the sense of
4565 [temp.mem]. */
4566 bool member_template_p = false;
4568 if (decl == error_mark_node || !current_template_parms)
4569 return error_mark_node;
4571 /* See if this is a partial specialization. */
4572 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4573 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4574 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4576 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4577 is_friend = true;
4579 if (is_friend)
4580 /* For a friend, we want the context of the friend function, not
4581 the type of which it is a friend. */
4582 ctx = CP_DECL_CONTEXT (decl);
4583 else if (CP_DECL_CONTEXT (decl)
4584 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4585 /* In the case of a virtual function, we want the class in which
4586 it is defined. */
4587 ctx = CP_DECL_CONTEXT (decl);
4588 else
4589 /* Otherwise, if we're currently defining some class, the DECL
4590 is assumed to be a member of the class. */
4591 ctx = current_scope ();
4593 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4594 ctx = NULL_TREE;
4596 if (!DECL_CONTEXT (decl))
4597 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4599 /* See if this is a primary template. */
4600 if (is_friend && ctx)
4601 /* A friend template that specifies a class context, i.e.
4602 template <typename T> friend void A<T>::f();
4603 is not primary. */
4604 is_primary = false;
4605 else
4606 is_primary = template_parm_scope_p ();
4608 if (is_primary)
4610 if (DECL_CLASS_SCOPE_P (decl))
4611 member_template_p = true;
4612 if (TREE_CODE (decl) == TYPE_DECL
4613 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4615 error ("template class without a name");
4616 return error_mark_node;
4618 else if (TREE_CODE (decl) == FUNCTION_DECL)
4620 if (DECL_DESTRUCTOR_P (decl))
4622 /* [temp.mem]
4624 A destructor shall not be a member template. */
4625 error ("destructor %qD declared as member template", decl);
4626 return error_mark_node;
4628 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4629 && (!prototype_p (TREE_TYPE (decl))
4630 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4631 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4632 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4633 == void_list_node)))
4635 /* [basic.stc.dynamic.allocation]
4637 An allocation function can be a function
4638 template. ... Template allocation functions shall
4639 have two or more parameters. */
4640 error ("invalid template declaration of %qD", decl);
4641 return error_mark_node;
4644 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4645 && CLASS_TYPE_P (TREE_TYPE (decl)))
4646 /* OK */;
4647 else if (TREE_CODE (decl) == TYPE_DECL
4648 && TYPE_DECL_ALIAS_P (decl))
4649 /* alias-declaration */
4650 gcc_assert (!DECL_ARTIFICIAL (decl));
4651 else
4653 error ("template declaration of %q#D", decl);
4654 return error_mark_node;
4658 /* Check to see that the rules regarding the use of default
4659 arguments are not being violated. */
4660 check_default_tmpl_args (decl, current_template_parms,
4661 is_primary, is_partial, /*is_friend_decl=*/0);
4663 /* Ensure that there are no parameter packs in the type of this
4664 declaration that have not been expanded. */
4665 if (TREE_CODE (decl) == FUNCTION_DECL)
4667 /* Check each of the arguments individually to see if there are
4668 any bare parameter packs. */
4669 tree type = TREE_TYPE (decl);
4670 tree arg = DECL_ARGUMENTS (decl);
4671 tree argtype = TYPE_ARG_TYPES (type);
4673 while (arg && argtype)
4675 if (!DECL_PACK_P (arg)
4676 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4678 /* This is a PARM_DECL that contains unexpanded parameter
4679 packs. We have already complained about this in the
4680 check_for_bare_parameter_packs call, so just replace
4681 these types with ERROR_MARK_NODE. */
4682 TREE_TYPE (arg) = error_mark_node;
4683 TREE_VALUE (argtype) = error_mark_node;
4686 arg = DECL_CHAIN (arg);
4687 argtype = TREE_CHAIN (argtype);
4690 /* Check for bare parameter packs in the return type and the
4691 exception specifiers. */
4692 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4693 /* Errors were already issued, set return type to int
4694 as the frontend doesn't expect error_mark_node as
4695 the return type. */
4696 TREE_TYPE (type) = integer_type_node;
4697 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4698 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4700 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4701 && TYPE_DECL_ALIAS_P (decl))
4702 ? DECL_ORIGINAL_TYPE (decl)
4703 : TREE_TYPE (decl)))
4705 TREE_TYPE (decl) = error_mark_node;
4706 return error_mark_node;
4709 if (is_partial)
4710 return process_partial_specialization (decl);
4712 args = current_template_args ();
4714 if (!ctx
4715 || TREE_CODE (ctx) == FUNCTION_DECL
4716 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4717 || (TREE_CODE (decl) == TYPE_DECL
4718 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4719 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4721 if (DECL_LANG_SPECIFIC (decl)
4722 && DECL_TEMPLATE_INFO (decl)
4723 && DECL_TI_TEMPLATE (decl))
4724 tmpl = DECL_TI_TEMPLATE (decl);
4725 /* If DECL is a TYPE_DECL for a class-template, then there won't
4726 be DECL_LANG_SPECIFIC. The information equivalent to
4727 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4728 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4729 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4730 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4732 /* Since a template declaration already existed for this
4733 class-type, we must be redeclaring it here. Make sure
4734 that the redeclaration is valid. */
4735 redeclare_class_template (TREE_TYPE (decl),
4736 current_template_parms);
4737 /* We don't need to create a new TEMPLATE_DECL; just use the
4738 one we already had. */
4739 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4741 else
4743 tmpl = build_template_decl (decl, current_template_parms,
4744 member_template_p);
4745 new_template_p = 1;
4747 if (DECL_LANG_SPECIFIC (decl)
4748 && DECL_TEMPLATE_SPECIALIZATION (decl))
4750 /* A specialization of a member template of a template
4751 class. */
4752 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4753 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4754 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4758 else
4760 tree a, t, current, parms;
4761 int i;
4762 tree tinfo = get_template_info (decl);
4764 if (!tinfo)
4766 error ("template definition of non-template %q#D", decl);
4767 return error_mark_node;
4770 tmpl = TI_TEMPLATE (tinfo);
4772 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4773 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4774 && DECL_TEMPLATE_SPECIALIZATION (decl)
4775 && DECL_MEMBER_TEMPLATE_P (tmpl))
4777 tree new_tmpl;
4779 /* The declaration is a specialization of a member
4780 template, declared outside the class. Therefore, the
4781 innermost template arguments will be NULL, so we
4782 replace them with the arguments determined by the
4783 earlier call to check_explicit_specialization. */
4784 args = DECL_TI_ARGS (decl);
4786 new_tmpl
4787 = build_template_decl (decl, current_template_parms,
4788 member_template_p);
4789 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4790 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4791 DECL_TI_TEMPLATE (decl) = new_tmpl;
4792 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4793 DECL_TEMPLATE_INFO (new_tmpl)
4794 = build_template_info (tmpl, args);
4796 register_specialization (new_tmpl,
4797 most_general_template (tmpl),
4798 args,
4799 is_friend, 0);
4800 return decl;
4803 /* Make sure the template headers we got make sense. */
4805 parms = DECL_TEMPLATE_PARMS (tmpl);
4806 i = TMPL_PARMS_DEPTH (parms);
4807 if (TMPL_ARGS_DEPTH (args) != i)
4809 error ("expected %d levels of template parms for %q#D, got %d",
4810 i, decl, TMPL_ARGS_DEPTH (args));
4812 else
4813 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4815 a = TMPL_ARGS_LEVEL (args, i);
4816 t = INNERMOST_TEMPLATE_PARMS (parms);
4818 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4820 if (current == decl)
4821 error ("got %d template parameters for %q#D",
4822 TREE_VEC_LENGTH (a), decl);
4823 else
4824 error ("got %d template parameters for %q#T",
4825 TREE_VEC_LENGTH (a), current);
4826 error (" but %d required", TREE_VEC_LENGTH (t));
4827 /* Avoid crash in import_export_decl. */
4828 DECL_INTERFACE_KNOWN (decl) = 1;
4829 return error_mark_node;
4832 if (current == decl)
4833 current = ctx;
4834 else if (current == NULL_TREE)
4835 /* Can happen in erroneous input. */
4836 break;
4837 else
4838 current = get_containing_scope (current);
4841 /* Check that the parms are used in the appropriate qualifying scopes
4842 in the declarator. */
4843 if (!comp_template_args
4844 (TI_ARGS (tinfo),
4845 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4847 error ("\
4848 template arguments to %qD do not match original template %qD",
4849 decl, DECL_TEMPLATE_RESULT (tmpl));
4850 if (!uses_template_parms (TI_ARGS (tinfo)))
4851 inform (input_location, "use template<> for an explicit specialization");
4852 /* Avoid crash in import_export_decl. */
4853 DECL_INTERFACE_KNOWN (decl) = 1;
4854 return error_mark_node;
4858 DECL_TEMPLATE_RESULT (tmpl) = decl;
4859 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4861 /* Push template declarations for global functions and types. Note
4862 that we do not try to push a global template friend declared in a
4863 template class; such a thing may well depend on the template
4864 parameters of the class. */
4865 if (new_template_p && !ctx
4866 && !(is_friend && template_class_depth (current_class_type) > 0))
4868 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4869 if (tmpl == error_mark_node)
4870 return error_mark_node;
4872 /* Hide template friend classes that haven't been declared yet. */
4873 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4875 DECL_ANTICIPATED (tmpl) = 1;
4876 DECL_FRIEND_P (tmpl) = 1;
4880 if (is_primary)
4882 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4883 int i;
4885 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4886 if (DECL_CONV_FN_P (tmpl))
4888 int depth = TMPL_PARMS_DEPTH (parms);
4890 /* It is a conversion operator. See if the type converted to
4891 depends on innermost template operands. */
4893 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4894 depth))
4895 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4898 /* Give template template parms a DECL_CONTEXT of the template
4899 for which they are a parameter. */
4900 parms = INNERMOST_TEMPLATE_PARMS (parms);
4901 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4903 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4904 if (TREE_CODE (parm) == TEMPLATE_DECL)
4905 DECL_CONTEXT (parm) = tmpl;
4909 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4910 back to its most general template. If TMPL is a specialization,
4911 ARGS may only have the innermost set of arguments. Add the missing
4912 argument levels if necessary. */
4913 if (DECL_TEMPLATE_INFO (tmpl))
4914 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4916 info = build_template_info (tmpl, args);
4918 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4919 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4920 else
4922 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4923 retrofit_lang_decl (decl);
4924 if (DECL_LANG_SPECIFIC (decl))
4925 DECL_TEMPLATE_INFO (decl) = info;
4928 return DECL_TEMPLATE_RESULT (tmpl);
4931 tree
4932 push_template_decl (tree decl)
4934 return push_template_decl_real (decl, false);
4937 /* FN is an inheriting constructor that inherits from the constructor
4938 template INHERITED; turn FN into a constructor template with a matching
4939 template header. */
4941 tree
4942 add_inherited_template_parms (tree fn, tree inherited)
4944 tree inner_parms
4945 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4946 inner_parms = copy_node (inner_parms);
4947 tree parms
4948 = tree_cons (size_int (processing_template_decl + 1),
4949 inner_parms, current_template_parms);
4950 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4951 tree args = template_parms_to_args (parms);
4952 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4953 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4954 DECL_TEMPLATE_RESULT (tmpl) = fn;
4955 DECL_ARTIFICIAL (tmpl) = true;
4956 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4957 return tmpl;
4960 /* Called when a class template TYPE is redeclared with the indicated
4961 template PARMS, e.g.:
4963 template <class T> struct S;
4964 template <class T> struct S {}; */
4966 bool
4967 redeclare_class_template (tree type, tree parms)
4969 tree tmpl;
4970 tree tmpl_parms;
4971 int i;
4973 if (!TYPE_TEMPLATE_INFO (type))
4975 error ("%qT is not a template type", type);
4976 return false;
4979 tmpl = TYPE_TI_TEMPLATE (type);
4980 if (!PRIMARY_TEMPLATE_P (tmpl))
4981 /* The type is nested in some template class. Nothing to worry
4982 about here; there are no new template parameters for the nested
4983 type. */
4984 return true;
4986 if (!parms)
4988 error ("template specifiers not specified in declaration of %qD",
4989 tmpl);
4990 return false;
4993 parms = INNERMOST_TEMPLATE_PARMS (parms);
4994 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4996 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4998 error_n (input_location, TREE_VEC_LENGTH (parms),
4999 "redeclared with %d template parameter",
5000 "redeclared with %d template parameters",
5001 TREE_VEC_LENGTH (parms));
5002 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5003 "previous declaration %q+D used %d template parameter",
5004 "previous declaration %q+D used %d template parameters",
5005 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5006 return false;
5009 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5011 tree tmpl_parm;
5012 tree parm;
5013 tree tmpl_default;
5014 tree parm_default;
5016 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5017 || TREE_VEC_ELT (parms, i) == error_mark_node)
5018 continue;
5020 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5021 if (tmpl_parm == error_mark_node)
5022 return false;
5024 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5025 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5026 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5028 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5029 TEMPLATE_DECL. */
5030 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5031 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5032 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5033 || (TREE_CODE (tmpl_parm) != PARM_DECL
5034 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5035 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5036 || (TREE_CODE (tmpl_parm) == PARM_DECL
5037 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5038 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5040 error ("template parameter %q+#D", tmpl_parm);
5041 error ("redeclared here as %q#D", parm);
5042 return false;
5045 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5047 /* We have in [temp.param]:
5049 A template-parameter may not be given default arguments
5050 by two different declarations in the same scope. */
5051 error_at (input_location, "redefinition of default argument for %q#D", parm);
5052 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5053 "original definition appeared here");
5054 return false;
5057 if (parm_default != NULL_TREE)
5058 /* Update the previous template parameters (which are the ones
5059 that will really count) with the new default value. */
5060 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5061 else if (tmpl_default != NULL_TREE)
5062 /* Update the new parameters, too; they'll be used as the
5063 parameters for any members. */
5064 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5067 return true;
5070 /* Simplify EXPR if it is a non-dependent expression. Returns the
5071 (possibly simplified) expression. */
5073 tree
5074 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5076 if (expr == NULL_TREE)
5077 return NULL_TREE;
5079 /* If we're in a template, but EXPR isn't value dependent, simplify
5080 it. We're supposed to treat:
5082 template <typename T> void f(T[1 + 1]);
5083 template <typename T> void f(T[2]);
5085 as two declarations of the same function, for example. */
5086 if (processing_template_decl
5087 && !instantiation_dependent_expression_p (expr)
5088 && potential_constant_expression (expr))
5090 HOST_WIDE_INT saved_processing_template_decl;
5092 saved_processing_template_decl = processing_template_decl;
5093 processing_template_decl = 0;
5094 expr = tsubst_copy_and_build (expr,
5095 /*args=*/NULL_TREE,
5096 complain,
5097 /*in_decl=*/NULL_TREE,
5098 /*function_p=*/false,
5099 /*integral_constant_expression_p=*/true);
5100 processing_template_decl = saved_processing_template_decl;
5102 return expr;
5105 tree
5106 fold_non_dependent_expr (tree expr)
5108 return fold_non_dependent_expr_sfinae (expr, tf_error);
5111 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5112 template declaration, or a TYPE_DECL for an alias declaration. */
5114 bool
5115 alias_type_or_template_p (tree t)
5117 if (t == NULL_TREE)
5118 return false;
5119 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5120 || (TYPE_P (t)
5121 && TYPE_NAME (t)
5122 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5123 || DECL_ALIAS_TEMPLATE_P (t));
5126 /* Return TRUE iff is a specialization of an alias template. */
5128 bool
5129 alias_template_specialization_p (const_tree t)
5131 if (t == NULL_TREE)
5132 return false;
5134 return (TYPE_P (t)
5135 && TYPE_TEMPLATE_INFO (t)
5136 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5137 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5140 /* Return either TMPL or another template that it is equivalent to under DR
5141 1286: An alias that just changes the name of a template is equivalent to
5142 the other template. */
5144 static tree
5145 get_underlying_template (tree tmpl)
5147 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5148 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5150 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5151 if (TYPE_TEMPLATE_INFO (result))
5153 tree sub = TYPE_TI_TEMPLATE (result);
5154 if (PRIMARY_TEMPLATE_P (sub)
5155 && same_type_p (result, TREE_TYPE (sub)))
5157 /* The alias type is equivalent to the pattern of the
5158 underlying template, so strip the alias. */
5159 tmpl = sub;
5160 continue;
5163 break;
5165 return tmpl;
5168 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5169 must be a function or a pointer-to-function type, as specified
5170 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5171 and check that the resulting function has external linkage. */
5173 static tree
5174 convert_nontype_argument_function (tree type, tree expr)
5176 tree fns = expr;
5177 tree fn, fn_no_ptr;
5178 linkage_kind linkage;
5180 fn = instantiate_type (type, fns, tf_none);
5181 if (fn == error_mark_node)
5182 return error_mark_node;
5184 fn_no_ptr = fn;
5185 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5186 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5187 if (BASELINK_P (fn_no_ptr))
5188 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5190 /* [temp.arg.nontype]/1
5192 A template-argument for a non-type, non-template template-parameter
5193 shall be one of:
5194 [...]
5195 -- the address of an object or function with external [C++11: or
5196 internal] linkage. */
5198 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5200 error ("%qE is not a valid template argument for type %qT", expr, type);
5201 if (TYPE_PTR_P (type))
5202 error ("it must be the address of a function with external linkage");
5203 else
5204 error ("it must be the name of a function with external linkage");
5205 return NULL_TREE;
5208 linkage = decl_linkage (fn_no_ptr);
5209 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5211 if (cxx_dialect >= cxx11)
5212 error ("%qE is not a valid template argument for type %qT "
5213 "because %qD has no linkage",
5214 expr, type, fn_no_ptr);
5215 else
5216 error ("%qE is not a valid template argument for type %qT "
5217 "because %qD does not have external linkage",
5218 expr, type, fn_no_ptr);
5219 return NULL_TREE;
5222 return fn;
5225 /* Subroutine of convert_nontype_argument.
5226 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5227 Emit an error otherwise. */
5229 static bool
5230 check_valid_ptrmem_cst_expr (tree type, tree expr,
5231 tsubst_flags_t complain)
5233 STRIP_NOPS (expr);
5234 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5235 return true;
5236 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5237 return true;
5238 if (complain & tf_error)
5240 error ("%qE is not a valid template argument for type %qT",
5241 expr, type);
5242 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5244 return false;
5247 /* Returns TRUE iff the address of OP is value-dependent.
5249 14.6.2.4 [temp.dep.temp]:
5250 A non-integral non-type template-argument is dependent if its type is
5251 dependent or it has either of the following forms
5252 qualified-id
5253 & qualified-id
5254 and contains a nested-name-specifier which specifies a class-name that
5255 names a dependent type.
5257 We generalize this to just say that the address of a member of a
5258 dependent class is value-dependent; the above doesn't cover the
5259 address of a static data member named with an unqualified-id. */
5261 static bool
5262 has_value_dependent_address (tree op)
5264 /* We could use get_inner_reference here, but there's no need;
5265 this is only relevant for template non-type arguments, which
5266 can only be expressed as &id-expression. */
5267 if (DECL_P (op))
5269 tree ctx = CP_DECL_CONTEXT (op);
5270 if (TYPE_P (ctx) && dependent_type_p (ctx))
5271 return true;
5274 return false;
5277 /* The next set of functions are used for providing helpful explanatory
5278 diagnostics for failed overload resolution. Their messages should be
5279 indented by two spaces for consistency with the messages in
5280 call.c */
5282 static int
5283 unify_success (bool /*explain_p*/)
5285 return 0;
5288 static int
5289 unify_parameter_deduction_failure (bool explain_p, tree parm)
5291 if (explain_p)
5292 inform (input_location,
5293 " couldn't deduce template parameter %qD", parm);
5294 return 1;
5297 static int
5298 unify_invalid (bool /*explain_p*/)
5300 return 1;
5303 static int
5304 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5306 if (explain_p)
5307 inform (input_location,
5308 " types %qT and %qT have incompatible cv-qualifiers",
5309 parm, arg);
5310 return 1;
5313 static int
5314 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5316 if (explain_p)
5317 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5318 return 1;
5321 static int
5322 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5324 if (explain_p)
5325 inform (input_location,
5326 " template parameter %qD is not a parameter pack, but "
5327 "argument %qD is",
5328 parm, arg);
5329 return 1;
5332 static int
5333 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5335 if (explain_p)
5336 inform (input_location,
5337 " template argument %qE does not match "
5338 "pointer-to-member constant %qE",
5339 arg, parm);
5340 return 1;
5343 static int
5344 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5346 if (explain_p)
5347 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5348 return 1;
5351 static int
5352 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5354 if (explain_p)
5355 inform (input_location,
5356 " inconsistent parameter pack deduction with %qT and %qT",
5357 old_arg, new_arg);
5358 return 1;
5361 static int
5362 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5364 if (explain_p)
5366 if (TYPE_P (parm))
5367 inform (input_location,
5368 " deduced conflicting types for parameter %qT (%qT and %qT)",
5369 parm, first, second);
5370 else
5371 inform (input_location,
5372 " deduced conflicting values for non-type parameter "
5373 "%qE (%qE and %qE)", parm, first, second);
5375 return 1;
5378 static int
5379 unify_vla_arg (bool explain_p, tree arg)
5381 if (explain_p)
5382 inform (input_location,
5383 " variable-sized array type %qT is not "
5384 "a valid template argument",
5385 arg);
5386 return 1;
5389 static int
5390 unify_method_type_error (bool explain_p, tree arg)
5392 if (explain_p)
5393 inform (input_location,
5394 " member function type %qT is not a valid template argument",
5395 arg);
5396 return 1;
5399 static int
5400 unify_arity (bool explain_p, int have, int wanted)
5402 if (explain_p)
5403 inform_n (input_location, wanted,
5404 " candidate expects %d argument, %d provided",
5405 " candidate expects %d arguments, %d provided",
5406 wanted, have);
5407 return 1;
5410 static int
5411 unify_too_many_arguments (bool explain_p, int have, int wanted)
5413 return unify_arity (explain_p, have, wanted);
5416 static int
5417 unify_too_few_arguments (bool explain_p, int have, int wanted)
5419 return unify_arity (explain_p, have, wanted);
5422 static int
5423 unify_arg_conversion (bool explain_p, tree to_type,
5424 tree from_type, tree arg)
5426 if (explain_p)
5427 inform (EXPR_LOC_OR_HERE (arg),
5428 " cannot convert %qE (type %qT) to type %qT",
5429 arg, from_type, to_type);
5430 return 1;
5433 static int
5434 unify_no_common_base (bool explain_p, enum template_base_result r,
5435 tree parm, tree arg)
5437 if (explain_p)
5438 switch (r)
5440 case tbr_ambiguous_baseclass:
5441 inform (input_location, " %qT is an ambiguous base class of %qT",
5442 parm, arg);
5443 break;
5444 default:
5445 inform (input_location, " %qT is not derived from %qT", arg, parm);
5446 break;
5448 return 1;
5451 static int
5452 unify_inconsistent_template_template_parameters (bool explain_p)
5454 if (explain_p)
5455 inform (input_location,
5456 " template parameters of a template template argument are "
5457 "inconsistent with other deduced template arguments");
5458 return 1;
5461 static int
5462 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5464 if (explain_p)
5465 inform (input_location,
5466 " can't deduce a template for %qT from non-template type %qT",
5467 parm, arg);
5468 return 1;
5471 static int
5472 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5474 if (explain_p)
5475 inform (input_location,
5476 " template argument %qE does not match %qD", arg, parm);
5477 return 1;
5480 static int
5481 unify_overload_resolution_failure (bool explain_p, tree arg)
5483 if (explain_p)
5484 inform (input_location,
5485 " could not resolve address from overloaded function %qE",
5486 arg);
5487 return 1;
5490 /* Attempt to convert the non-type template parameter EXPR to the
5491 indicated TYPE. If the conversion is successful, return the
5492 converted value. If the conversion is unsuccessful, return
5493 NULL_TREE if we issued an error message, or error_mark_node if we
5494 did not. We issue error messages for out-and-out bad template
5495 parameters, but not simply because the conversion failed, since we
5496 might be just trying to do argument deduction. Both TYPE and EXPR
5497 must be non-dependent.
5499 The conversion follows the special rules described in
5500 [temp.arg.nontype], and it is much more strict than an implicit
5501 conversion.
5503 This function is called twice for each template argument (see
5504 lookup_template_class for a more accurate description of this
5505 problem). This means that we need to handle expressions which
5506 are not valid in a C++ source, but can be created from the
5507 first call (for instance, casts to perform conversions). These
5508 hacks can go away after we fix the double coercion problem. */
5510 static tree
5511 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5513 tree expr_type;
5515 /* Detect immediately string literals as invalid non-type argument.
5516 This special-case is not needed for correctness (we would easily
5517 catch this later), but only to provide better diagnostic for this
5518 common user mistake. As suggested by DR 100, we do not mention
5519 linkage issues in the diagnostic as this is not the point. */
5520 /* FIXME we're making this OK. */
5521 if (TREE_CODE (expr) == STRING_CST)
5523 if (complain & tf_error)
5524 error ("%qE is not a valid template argument for type %qT "
5525 "because string literals can never be used in this context",
5526 expr, type);
5527 return NULL_TREE;
5530 /* Add the ADDR_EXPR now for the benefit of
5531 value_dependent_expression_p. */
5532 if (TYPE_PTROBV_P (type)
5533 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5535 expr = decay_conversion (expr, complain);
5536 if (expr == error_mark_node)
5537 return error_mark_node;
5540 /* If we are in a template, EXPR may be non-dependent, but still
5541 have a syntactic, rather than semantic, form. For example, EXPR
5542 might be a SCOPE_REF, rather than the VAR_DECL to which the
5543 SCOPE_REF refers. Preserving the qualifying scope is necessary
5544 so that access checking can be performed when the template is
5545 instantiated -- but here we need the resolved form so that we can
5546 convert the argument. */
5547 if (TYPE_REF_OBJ_P (type)
5548 && has_value_dependent_address (expr))
5549 /* If we want the address and it's value-dependent, don't fold. */;
5550 else if (!type_unknown_p (expr))
5551 expr = fold_non_dependent_expr_sfinae (expr, complain);
5552 if (error_operand_p (expr))
5553 return error_mark_node;
5554 expr_type = TREE_TYPE (expr);
5555 if (TREE_CODE (type) == REFERENCE_TYPE)
5556 expr = mark_lvalue_use (expr);
5557 else
5558 expr = mark_rvalue_use (expr);
5560 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5561 to a non-type argument of "nullptr". */
5562 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5563 expr = convert (type, expr);
5565 /* In C++11, integral or enumeration non-type template arguments can be
5566 arbitrary constant expressions. Pointer and pointer to
5567 member arguments can be general constant expressions that evaluate
5568 to a null value, but otherwise still need to be of a specific form. */
5569 if (cxx_dialect >= cxx11)
5571 if (TREE_CODE (expr) == PTRMEM_CST)
5572 /* A PTRMEM_CST is already constant, and a valid template
5573 argument for a parameter of pointer to member type, we just want
5574 to leave it in that form rather than lower it to a
5575 CONSTRUCTOR. */;
5576 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5577 expr = maybe_constant_value (expr);
5578 else if (TYPE_PTR_OR_PTRMEM_P (type))
5580 tree folded = maybe_constant_value (expr);
5581 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5582 : null_member_pointer_value_p (folded))
5583 expr = folded;
5587 /* HACK: Due to double coercion, we can get a
5588 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5589 which is the tree that we built on the first call (see
5590 below when coercing to reference to object or to reference to
5591 function). We just strip everything and get to the arg.
5592 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5593 for examples. */
5594 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5596 tree probe_type, probe = expr;
5597 if (REFERENCE_REF_P (probe))
5598 probe = TREE_OPERAND (probe, 0);
5599 probe_type = TREE_TYPE (probe);
5600 if (TREE_CODE (probe) == NOP_EXPR)
5602 /* ??? Maybe we could use convert_from_reference here, but we
5603 would need to relax its constraints because the NOP_EXPR
5604 could actually change the type to something more cv-qualified,
5605 and this is not folded by convert_from_reference. */
5606 tree addr = TREE_OPERAND (probe, 0);
5607 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5608 && TREE_CODE (addr) == ADDR_EXPR
5609 && TYPE_PTR_P (TREE_TYPE (addr))
5610 && (same_type_ignoring_top_level_qualifiers_p
5611 (TREE_TYPE (probe_type),
5612 TREE_TYPE (TREE_TYPE (addr)))))
5614 expr = TREE_OPERAND (addr, 0);
5615 expr_type = TREE_TYPE (probe_type);
5620 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5621 parameter is a pointer to object, through decay and
5622 qualification conversion. Let's strip everything. */
5623 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5625 tree probe = expr;
5626 STRIP_NOPS (probe);
5627 if (TREE_CODE (probe) == ADDR_EXPR
5628 && TYPE_PTR_P (TREE_TYPE (probe)))
5630 /* Skip the ADDR_EXPR only if it is part of the decay for
5631 an array. Otherwise, it is part of the original argument
5632 in the source code. */
5633 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5634 probe = TREE_OPERAND (probe, 0);
5635 expr = probe;
5636 expr_type = TREE_TYPE (expr);
5640 /* [temp.arg.nontype]/5, bullet 1
5642 For a non-type template-parameter of integral or enumeration type,
5643 integral promotions (_conv.prom_) and integral conversions
5644 (_conv.integral_) are applied. */
5645 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5647 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5648 t = maybe_constant_value (t);
5649 if (t != error_mark_node)
5650 expr = t;
5652 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5653 return error_mark_node;
5655 /* Notice that there are constant expressions like '4 % 0' which
5656 do not fold into integer constants. */
5657 if (TREE_CODE (expr) != INTEGER_CST)
5659 if (complain & tf_error)
5661 int errs = errorcount, warns = warningcount + werrorcount;
5662 if (processing_template_decl
5663 && !require_potential_constant_expression (expr))
5664 return NULL_TREE;
5665 expr = cxx_constant_value (expr);
5666 if (errorcount > errs || warningcount + werrorcount > warns)
5667 inform (EXPR_LOC_OR_HERE (expr),
5668 "in template argument for type %qT ", type);
5669 if (expr == error_mark_node)
5670 return NULL_TREE;
5671 /* else cxx_constant_value complained but gave us
5672 a real constant, so go ahead. */
5673 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5675 else
5676 return NULL_TREE;
5679 /* Avoid typedef problems. */
5680 if (TREE_TYPE (expr) != type)
5681 expr = fold_convert (type, expr);
5683 /* [temp.arg.nontype]/5, bullet 2
5685 For a non-type template-parameter of type pointer to object,
5686 qualification conversions (_conv.qual_) and the array-to-pointer
5687 conversion (_conv.array_) are applied. */
5688 else if (TYPE_PTROBV_P (type))
5690 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5692 A template-argument for a non-type, non-template template-parameter
5693 shall be one of: [...]
5695 -- the name of a non-type template-parameter;
5696 -- the address of an object or function with external linkage, [...]
5697 expressed as "& id-expression" where the & is optional if the name
5698 refers to a function or array, or if the corresponding
5699 template-parameter is a reference.
5701 Here, we do not care about functions, as they are invalid anyway
5702 for a parameter of type pointer-to-object. */
5704 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5705 /* Non-type template parameters are OK. */
5707 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5708 /* Null pointer values are OK in C++11. */;
5709 else if (TREE_CODE (expr) != ADDR_EXPR
5710 && TREE_CODE (expr_type) != ARRAY_TYPE)
5712 if (VAR_P (expr))
5714 error ("%qD is not a valid template argument "
5715 "because %qD is a variable, not the address of "
5716 "a variable",
5717 expr, expr);
5718 return NULL_TREE;
5720 if (POINTER_TYPE_P (expr_type))
5722 error ("%qE is not a valid template argument for %qT "
5723 "because it is not the address of a variable",
5724 expr, type);
5725 return NULL_TREE;
5727 /* Other values, like integer constants, might be valid
5728 non-type arguments of some other type. */
5729 return error_mark_node;
5731 else
5733 tree decl;
5735 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5736 ? TREE_OPERAND (expr, 0) : expr);
5737 if (!VAR_P (decl))
5739 error ("%qE is not a valid template argument of type %qT "
5740 "because %qE is not a variable",
5741 expr, type, decl);
5742 return NULL_TREE;
5744 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5746 error ("%qE is not a valid template argument of type %qT "
5747 "because %qD does not have external linkage",
5748 expr, type, decl);
5749 return NULL_TREE;
5751 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5753 error ("%qE is not a valid template argument of type %qT "
5754 "because %qD has no linkage",
5755 expr, type, decl);
5756 return NULL_TREE;
5760 expr = decay_conversion (expr, complain);
5761 if (expr == error_mark_node)
5762 return error_mark_node;
5764 expr = perform_qualification_conversions (type, expr);
5765 if (expr == error_mark_node)
5766 return error_mark_node;
5768 /* [temp.arg.nontype]/5, bullet 3
5770 For a non-type template-parameter of type reference to object, no
5771 conversions apply. The type referred to by the reference may be more
5772 cv-qualified than the (otherwise identical) type of the
5773 template-argument. The template-parameter is bound directly to the
5774 template-argument, which must be an lvalue. */
5775 else if (TYPE_REF_OBJ_P (type))
5777 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5778 expr_type))
5779 return error_mark_node;
5781 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5783 error ("%qE is not a valid template argument for type %qT "
5784 "because of conflicts in cv-qualification", expr, type);
5785 return NULL_TREE;
5788 if (!real_lvalue_p (expr))
5790 error ("%qE is not a valid template argument for type %qT "
5791 "because it is not an lvalue", expr, type);
5792 return NULL_TREE;
5795 /* [temp.arg.nontype]/1
5797 A template-argument for a non-type, non-template template-parameter
5798 shall be one of: [...]
5800 -- the address of an object or function with external linkage. */
5801 if (INDIRECT_REF_P (expr)
5802 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5804 expr = TREE_OPERAND (expr, 0);
5805 if (DECL_P (expr))
5807 error ("%q#D is not a valid template argument for type %qT "
5808 "because a reference variable does not have a constant "
5809 "address", expr, type);
5810 return NULL_TREE;
5814 if (!DECL_P (expr))
5816 error ("%qE is not a valid template argument for type %qT "
5817 "because it is not an object with external linkage",
5818 expr, type);
5819 return NULL_TREE;
5822 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5824 error ("%qE is not a valid template argument for type %qT "
5825 "because object %qD has not external linkage",
5826 expr, type, expr);
5827 return NULL_TREE;
5830 expr = build_nop (type, build_address (expr));
5832 /* [temp.arg.nontype]/5, bullet 4
5834 For a non-type template-parameter of type pointer to function, only
5835 the function-to-pointer conversion (_conv.func_) is applied. If the
5836 template-argument represents a set of overloaded functions (or a
5837 pointer to such), the matching function is selected from the set
5838 (_over.over_). */
5839 else if (TYPE_PTRFN_P (type))
5841 /* If the argument is a template-id, we might not have enough
5842 context information to decay the pointer. */
5843 if (!type_unknown_p (expr_type))
5845 expr = decay_conversion (expr, complain);
5846 if (expr == error_mark_node)
5847 return error_mark_node;
5850 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5851 /* Null pointer values are OK in C++11. */
5852 return perform_qualification_conversions (type, expr);
5854 expr = convert_nontype_argument_function (type, expr);
5855 if (!expr || expr == error_mark_node)
5856 return expr;
5858 /* [temp.arg.nontype]/5, bullet 5
5860 For a non-type template-parameter of type reference to function, no
5861 conversions apply. If the template-argument represents a set of
5862 overloaded functions, the matching function is selected from the set
5863 (_over.over_). */
5864 else if (TYPE_REFFN_P (type))
5866 if (TREE_CODE (expr) == ADDR_EXPR)
5868 error ("%qE is not a valid template argument for type %qT "
5869 "because it is a pointer", expr, type);
5870 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5871 return NULL_TREE;
5874 expr = convert_nontype_argument_function (type, expr);
5875 if (!expr || expr == error_mark_node)
5876 return expr;
5878 expr = build_nop (type, build_address (expr));
5880 /* [temp.arg.nontype]/5, bullet 6
5882 For a non-type template-parameter of type pointer to member function,
5883 no conversions apply. If the template-argument represents a set of
5884 overloaded member functions, the matching member function is selected
5885 from the set (_over.over_). */
5886 else if (TYPE_PTRMEMFUNC_P (type))
5888 expr = instantiate_type (type, expr, tf_none);
5889 if (expr == error_mark_node)
5890 return error_mark_node;
5892 /* [temp.arg.nontype] bullet 1 says the pointer to member
5893 expression must be a pointer-to-member constant. */
5894 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5895 return error_mark_node;
5897 /* There is no way to disable standard conversions in
5898 resolve_address_of_overloaded_function (called by
5899 instantiate_type). It is possible that the call succeeded by
5900 converting &B::I to &D::I (where B is a base of D), so we need
5901 to reject this conversion here.
5903 Actually, even if there was a way to disable standard conversions,
5904 it would still be better to reject them here so that we can
5905 provide a superior diagnostic. */
5906 if (!same_type_p (TREE_TYPE (expr), type))
5908 error ("%qE is not a valid template argument for type %qT "
5909 "because it is of type %qT", expr, type,
5910 TREE_TYPE (expr));
5911 /* If we are just one standard conversion off, explain. */
5912 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5913 inform (input_location,
5914 "standard conversions are not allowed in this context");
5915 return NULL_TREE;
5918 /* [temp.arg.nontype]/5, bullet 7
5920 For a non-type template-parameter of type pointer to data member,
5921 qualification conversions (_conv.qual_) are applied. */
5922 else if (TYPE_PTRDATAMEM_P (type))
5924 /* [temp.arg.nontype] bullet 1 says the pointer to member
5925 expression must be a pointer-to-member constant. */
5926 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5927 return error_mark_node;
5929 expr = perform_qualification_conversions (type, expr);
5930 if (expr == error_mark_node)
5931 return expr;
5933 else if (NULLPTR_TYPE_P (type))
5935 if (expr != nullptr_node)
5937 error ("%qE is not a valid template argument for type %qT "
5938 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5939 return NULL_TREE;
5941 return expr;
5943 /* A template non-type parameter must be one of the above. */
5944 else
5945 gcc_unreachable ();
5947 /* Sanity check: did we actually convert the argument to the
5948 right type? */
5949 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5950 (type, TREE_TYPE (expr)));
5951 return expr;
5954 /* Subroutine of coerce_template_template_parms, which returns 1 if
5955 PARM_PARM and ARG_PARM match using the rule for the template
5956 parameters of template template parameters. Both PARM and ARG are
5957 template parameters; the rest of the arguments are the same as for
5958 coerce_template_template_parms.
5960 static int
5961 coerce_template_template_parm (tree parm,
5962 tree arg,
5963 tsubst_flags_t complain,
5964 tree in_decl,
5965 tree outer_args)
5967 if (arg == NULL_TREE || arg == error_mark_node
5968 || parm == NULL_TREE || parm == error_mark_node)
5969 return 0;
5971 if (TREE_CODE (arg) != TREE_CODE (parm))
5972 return 0;
5974 switch (TREE_CODE (parm))
5976 case TEMPLATE_DECL:
5977 /* We encounter instantiations of templates like
5978 template <template <template <class> class> class TT>
5979 class C; */
5981 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5982 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5984 if (!coerce_template_template_parms
5985 (parmparm, argparm, complain, in_decl, outer_args))
5986 return 0;
5988 /* Fall through. */
5990 case TYPE_DECL:
5991 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5992 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5993 /* Argument is a parameter pack but parameter is not. */
5994 return 0;
5995 break;
5997 case PARM_DECL:
5998 /* The tsubst call is used to handle cases such as
6000 template <int> class C {};
6001 template <class T, template <T> class TT> class D {};
6002 D<int, C> d;
6004 i.e. the parameter list of TT depends on earlier parameters. */
6005 if (!uses_template_parms (TREE_TYPE (arg))
6006 && !same_type_p
6007 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6008 TREE_TYPE (arg)))
6009 return 0;
6011 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6012 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6013 /* Argument is a parameter pack but parameter is not. */
6014 return 0;
6016 break;
6018 default:
6019 gcc_unreachable ();
6022 return 1;
6026 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6027 template template parameters. Both PARM_PARMS and ARG_PARMS are
6028 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6029 or PARM_DECL.
6031 Consider the example:
6032 template <class T> class A;
6033 template<template <class U> class TT> class B;
6035 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6036 the parameters to A, and OUTER_ARGS contains A. */
6038 static int
6039 coerce_template_template_parms (tree parm_parms,
6040 tree arg_parms,
6041 tsubst_flags_t complain,
6042 tree in_decl,
6043 tree outer_args)
6045 int nparms, nargs, i;
6046 tree parm, arg;
6047 int variadic_p = 0;
6049 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6050 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6052 nparms = TREE_VEC_LENGTH (parm_parms);
6053 nargs = TREE_VEC_LENGTH (arg_parms);
6055 /* Determine whether we have a parameter pack at the end of the
6056 template template parameter's template parameter list. */
6057 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6059 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6061 if (parm == error_mark_node)
6062 return 0;
6064 switch (TREE_CODE (parm))
6066 case TEMPLATE_DECL:
6067 case TYPE_DECL:
6068 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6069 variadic_p = 1;
6070 break;
6072 case PARM_DECL:
6073 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6074 variadic_p = 1;
6075 break;
6077 default:
6078 gcc_unreachable ();
6082 if (nargs != nparms
6083 && !(variadic_p && nargs >= nparms - 1))
6084 return 0;
6086 /* Check all of the template parameters except the parameter pack at
6087 the end (if any). */
6088 for (i = 0; i < nparms - variadic_p; ++i)
6090 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6091 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6092 continue;
6094 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6095 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6097 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6098 outer_args))
6099 return 0;
6103 if (variadic_p)
6105 /* Check each of the template parameters in the template
6106 argument against the template parameter pack at the end of
6107 the template template parameter. */
6108 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6109 return 0;
6111 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6113 for (; i < nargs; ++i)
6115 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6116 continue;
6118 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6120 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6121 outer_args))
6122 return 0;
6126 return 1;
6129 /* Verifies that the deduced template arguments (in TARGS) for the
6130 template template parameters (in TPARMS) represent valid bindings,
6131 by comparing the template parameter list of each template argument
6132 to the template parameter list of its corresponding template
6133 template parameter, in accordance with DR150. This
6134 routine can only be called after all template arguments have been
6135 deduced. It will return TRUE if all of the template template
6136 parameter bindings are okay, FALSE otherwise. */
6137 bool
6138 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6140 int i, ntparms = TREE_VEC_LENGTH (tparms);
6141 bool ret = true;
6143 /* We're dealing with template parms in this process. */
6144 ++processing_template_decl;
6146 targs = INNERMOST_TEMPLATE_ARGS (targs);
6148 for (i = 0; i < ntparms; ++i)
6150 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6151 tree targ = TREE_VEC_ELT (targs, i);
6153 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6155 tree packed_args = NULL_TREE;
6156 int idx, len = 1;
6158 if (ARGUMENT_PACK_P (targ))
6160 /* Look inside the argument pack. */
6161 packed_args = ARGUMENT_PACK_ARGS (targ);
6162 len = TREE_VEC_LENGTH (packed_args);
6165 for (idx = 0; idx < len; ++idx)
6167 tree targ_parms = NULL_TREE;
6169 if (packed_args)
6170 /* Extract the next argument from the argument
6171 pack. */
6172 targ = TREE_VEC_ELT (packed_args, idx);
6174 if (PACK_EXPANSION_P (targ))
6175 /* Look at the pattern of the pack expansion. */
6176 targ = PACK_EXPANSION_PATTERN (targ);
6178 /* Extract the template parameters from the template
6179 argument. */
6180 if (TREE_CODE (targ) == TEMPLATE_DECL)
6181 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6182 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6183 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6185 /* Verify that we can coerce the template template
6186 parameters from the template argument to the template
6187 parameter. This requires an exact match. */
6188 if (targ_parms
6189 && !coerce_template_template_parms
6190 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6191 targ_parms,
6192 tf_none,
6193 tparm,
6194 targs))
6196 ret = false;
6197 goto out;
6203 out:
6205 --processing_template_decl;
6206 return ret;
6209 /* Since type attributes aren't mangled, we need to strip them from
6210 template type arguments. */
6212 static tree
6213 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6215 tree mv;
6216 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6217 return arg;
6218 mv = TYPE_MAIN_VARIANT (arg);
6219 arg = strip_typedefs (arg);
6220 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6221 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6223 if (complain & tf_warning)
6224 warning (0, "ignoring attributes on template argument %qT", arg);
6225 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6226 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6228 return arg;
6231 /* Convert the indicated template ARG as necessary to match the
6232 indicated template PARM. Returns the converted ARG, or
6233 error_mark_node if the conversion was unsuccessful. Error and
6234 warning messages are issued under control of COMPLAIN. This
6235 conversion is for the Ith parameter in the parameter list. ARGS is
6236 the full set of template arguments deduced so far. */
6238 static tree
6239 convert_template_argument (tree parm,
6240 tree arg,
6241 tree args,
6242 tsubst_flags_t complain,
6243 int i,
6244 tree in_decl)
6246 tree orig_arg;
6247 tree val;
6248 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6250 if (TREE_CODE (arg) == TREE_LIST
6251 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6253 /* The template argument was the name of some
6254 member function. That's usually
6255 invalid, but static members are OK. In any
6256 case, grab the underlying fields/functions
6257 and issue an error later if required. */
6258 orig_arg = TREE_VALUE (arg);
6259 TREE_TYPE (arg) = unknown_type_node;
6262 orig_arg = arg;
6264 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6265 requires_type = (TREE_CODE (parm) == TYPE_DECL
6266 || requires_tmpl_type);
6268 /* When determining whether an argument pack expansion is a template,
6269 look at the pattern. */
6270 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6271 arg = PACK_EXPANSION_PATTERN (arg);
6273 /* Deal with an injected-class-name used as a template template arg. */
6274 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6276 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6277 if (TREE_CODE (t) == TEMPLATE_DECL)
6279 if (cxx_dialect >= cxx11)
6280 /* OK under DR 1004. */;
6281 else if (complain & tf_warning_or_error)
6282 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6283 " used as template template argument", TYPE_NAME (arg));
6284 else if (flag_pedantic_errors)
6285 t = arg;
6287 arg = t;
6291 is_tmpl_type =
6292 ((TREE_CODE (arg) == TEMPLATE_DECL
6293 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6294 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6295 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6296 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6298 if (is_tmpl_type
6299 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6300 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6301 arg = TYPE_STUB_DECL (arg);
6303 is_type = TYPE_P (arg) || is_tmpl_type;
6305 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6306 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6308 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6310 if (complain & tf_error)
6311 error ("invalid use of destructor %qE as a type", orig_arg);
6312 return error_mark_node;
6315 permerror (input_location,
6316 "to refer to a type member of a template parameter, "
6317 "use %<typename %E%>", orig_arg);
6319 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6320 TREE_OPERAND (arg, 1),
6321 typename_type,
6322 complain);
6323 arg = orig_arg;
6324 is_type = 1;
6326 if (is_type != requires_type)
6328 if (in_decl)
6330 if (complain & tf_error)
6332 error ("type/value mismatch at argument %d in template "
6333 "parameter list for %qD",
6334 i + 1, in_decl);
6335 if (is_type)
6336 error (" expected a constant of type %qT, got %qT",
6337 TREE_TYPE (parm),
6338 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6339 else if (requires_tmpl_type)
6340 error (" expected a class template, got %qE", orig_arg);
6341 else
6342 error (" expected a type, got %qE", orig_arg);
6345 return error_mark_node;
6347 if (is_tmpl_type ^ requires_tmpl_type)
6349 if (in_decl && (complain & tf_error))
6351 error ("type/value mismatch at argument %d in template "
6352 "parameter list for %qD",
6353 i + 1, in_decl);
6354 if (is_tmpl_type)
6355 error (" expected a type, got %qT", DECL_NAME (arg));
6356 else
6357 error (" expected a class template, got %qT", orig_arg);
6359 return error_mark_node;
6362 if (is_type)
6364 if (requires_tmpl_type)
6366 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6367 val = orig_arg;
6368 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6369 /* The number of argument required is not known yet.
6370 Just accept it for now. */
6371 val = TREE_TYPE (arg);
6372 else
6374 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6375 tree argparm;
6377 /* Strip alias templates that are equivalent to another
6378 template. */
6379 arg = get_underlying_template (arg);
6380 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6382 if (coerce_template_template_parms (parmparm, argparm,
6383 complain, in_decl,
6384 args))
6386 val = arg;
6388 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6389 TEMPLATE_DECL. */
6390 if (val != error_mark_node)
6392 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6393 val = TREE_TYPE (val);
6394 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6395 val = make_pack_expansion (val);
6398 else
6400 if (in_decl && (complain & tf_error))
6402 error ("type/value mismatch at argument %d in "
6403 "template parameter list for %qD",
6404 i + 1, in_decl);
6405 error (" expected a template of type %qD, got %qT",
6406 parm, orig_arg);
6409 val = error_mark_node;
6413 else
6414 val = orig_arg;
6415 /* We only form one instance of each template specialization.
6416 Therefore, if we use a non-canonical variant (i.e., a
6417 typedef), any future messages referring to the type will use
6418 the typedef, which is confusing if those future uses do not
6419 themselves also use the typedef. */
6420 if (TYPE_P (val))
6421 val = canonicalize_type_argument (val, complain);
6423 else
6425 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6427 if (invalid_nontype_parm_type_p (t, complain))
6428 return error_mark_node;
6430 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6432 if (same_type_p (t, TREE_TYPE (orig_arg)))
6433 val = orig_arg;
6434 else
6436 /* Not sure if this is reachable, but it doesn't hurt
6437 to be robust. */
6438 error ("type mismatch in nontype parameter pack");
6439 val = error_mark_node;
6442 else if (!dependent_template_arg_p (orig_arg)
6443 && !uses_template_parms (t))
6444 /* We used to call digest_init here. However, digest_init
6445 will report errors, which we don't want when complain
6446 is zero. More importantly, digest_init will try too
6447 hard to convert things: for example, `0' should not be
6448 converted to pointer type at this point according to
6449 the standard. Accepting this is not merely an
6450 extension, since deciding whether or not these
6451 conversions can occur is part of determining which
6452 function template to call, or whether a given explicit
6453 argument specification is valid. */
6454 val = convert_nontype_argument (t, orig_arg, complain);
6455 else
6456 val = strip_typedefs_expr (orig_arg);
6458 if (val == NULL_TREE)
6459 val = error_mark_node;
6460 else if (val == error_mark_node && (complain & tf_error))
6461 error ("could not convert template argument %qE to %qT", orig_arg, t);
6463 if (TREE_CODE (val) == SCOPE_REF)
6465 /* Strip typedefs from the SCOPE_REF. */
6466 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6467 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6468 complain);
6469 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6470 QUALIFIED_NAME_IS_TEMPLATE (val));
6474 return val;
6477 /* Coerces the remaining template arguments in INNER_ARGS (from
6478 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6479 Returns the coerced argument pack. PARM_IDX is the position of this
6480 parameter in the template parameter list. ARGS is the original
6481 template argument list. */
6482 static tree
6483 coerce_template_parameter_pack (tree parms,
6484 int parm_idx,
6485 tree args,
6486 tree inner_args,
6487 int arg_idx,
6488 tree new_args,
6489 int* lost,
6490 tree in_decl,
6491 tsubst_flags_t complain)
6493 tree parm = TREE_VEC_ELT (parms, parm_idx);
6494 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6495 tree packed_args;
6496 tree argument_pack;
6497 tree packed_types = NULL_TREE;
6499 if (arg_idx > nargs)
6500 arg_idx = nargs;
6502 packed_args = make_tree_vec (nargs - arg_idx);
6504 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6505 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6507 /* When the template parameter is a non-type template
6508 parameter pack whose type uses parameter packs, we need
6509 to look at each of the template arguments
6510 separately. Build a vector of the types for these
6511 non-type template parameters in PACKED_TYPES. */
6512 tree expansion
6513 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6514 packed_types = tsubst_pack_expansion (expansion, args,
6515 complain, in_decl);
6517 if (packed_types == error_mark_node)
6518 return error_mark_node;
6520 /* Check that we have the right number of arguments. */
6521 if (arg_idx < nargs
6522 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6523 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6525 int needed_parms
6526 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6527 error ("wrong number of template arguments (%d, should be %d)",
6528 nargs, needed_parms);
6529 return error_mark_node;
6532 /* If we aren't able to check the actual arguments now
6533 (because they haven't been expanded yet), we can at least
6534 verify that all of the types used for the non-type
6535 template parameter pack are, in fact, valid for non-type
6536 template parameters. */
6537 if (arg_idx < nargs
6538 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6540 int j, len = TREE_VEC_LENGTH (packed_types);
6541 for (j = 0; j < len; ++j)
6543 tree t = TREE_VEC_ELT (packed_types, j);
6544 if (invalid_nontype_parm_type_p (t, complain))
6545 return error_mark_node;
6550 /* Convert the remaining arguments, which will be a part of the
6551 parameter pack "parm". */
6552 for (; arg_idx < nargs; ++arg_idx)
6554 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6555 tree actual_parm = TREE_VALUE (parm);
6557 if (packed_types && !PACK_EXPANSION_P (arg))
6559 /* When we have a vector of types (corresponding to the
6560 non-type template parameter pack that uses parameter
6561 packs in its type, as mention above), and the
6562 argument is not an expansion (which expands to a
6563 currently unknown number of arguments), clone the
6564 parm and give it the next type in PACKED_TYPES. */
6565 actual_parm = copy_node (actual_parm);
6566 TREE_TYPE (actual_parm) =
6567 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6570 if (arg == error_mark_node)
6572 if (complain & tf_error)
6573 error ("template argument %d is invalid", arg_idx + 1);
6575 else
6576 arg = convert_template_argument (actual_parm,
6577 arg, new_args, complain, parm_idx,
6578 in_decl);
6579 if (arg == error_mark_node)
6580 (*lost)++;
6581 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6584 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6585 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6586 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6587 else
6589 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6590 TREE_TYPE (argument_pack)
6591 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6592 TREE_CONSTANT (argument_pack) = 1;
6595 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6596 #ifdef ENABLE_CHECKING
6597 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6598 TREE_VEC_LENGTH (packed_args));
6599 #endif
6600 return argument_pack;
6603 /* Returns the number of pack expansions in the template argument vector
6604 ARGS. */
6606 static int
6607 pack_expansion_args_count (tree args)
6609 int i;
6610 int count = 0;
6611 if (args)
6612 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6614 tree elt = TREE_VEC_ELT (args, i);
6615 if (elt && PACK_EXPANSION_P (elt))
6616 ++count;
6618 return count;
6621 /* Convert all template arguments to their appropriate types, and
6622 return a vector containing the innermost resulting template
6623 arguments. If any error occurs, return error_mark_node. Error and
6624 warning messages are issued under control of COMPLAIN.
6626 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6627 for arguments not specified in ARGS. Otherwise, if
6628 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6629 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6630 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6631 ARGS. */
6633 static tree
6634 coerce_template_parms (tree parms,
6635 tree args,
6636 tree in_decl,
6637 tsubst_flags_t complain,
6638 bool require_all_args,
6639 bool use_default_args)
6641 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6642 tree inner_args;
6643 tree new_args;
6644 tree new_inner_args;
6645 int saved_unevaluated_operand;
6646 int saved_inhibit_evaluation_warnings;
6648 /* When used as a boolean value, indicates whether this is a
6649 variadic template parameter list. Since it's an int, we can also
6650 subtract it from nparms to get the number of non-variadic
6651 parameters. */
6652 int variadic_p = 0;
6653 int variadic_args_p = 0;
6654 int post_variadic_parms = 0;
6656 if (args == error_mark_node)
6657 return error_mark_node;
6659 nparms = TREE_VEC_LENGTH (parms);
6661 /* Determine if there are any parameter packs. */
6662 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6664 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6665 if (variadic_p)
6666 ++post_variadic_parms;
6667 if (template_parameter_pack_p (tparm))
6668 ++variadic_p;
6671 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6672 /* If there are no parameters that follow a parameter pack, we need to
6673 expand any argument packs so that we can deduce a parameter pack from
6674 some non-packed args followed by an argument pack, as in variadic85.C.
6675 If there are such parameters, we need to leave argument packs intact
6676 so the arguments are assigned properly. This can happen when dealing
6677 with a nested class inside a partial specialization of a class
6678 template, as in variadic92.C, or when deducing a template parameter pack
6679 from a sub-declarator, as in variadic114.C. */
6680 if (!post_variadic_parms)
6681 inner_args = expand_template_argument_pack (inner_args);
6683 /* Count any pack expansion args. */
6684 variadic_args_p = pack_expansion_args_count (inner_args);
6686 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6687 if ((nargs > nparms && !variadic_p)
6688 || (nargs < nparms - variadic_p
6689 && require_all_args
6690 && !variadic_args_p
6691 && (!use_default_args
6692 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6693 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6695 if (complain & tf_error)
6697 if (variadic_p)
6699 nparms -= variadic_p;
6700 error ("wrong number of template arguments "
6701 "(%d, should be %d or more)", nargs, nparms);
6703 else
6704 error ("wrong number of template arguments "
6705 "(%d, should be %d)", nargs, nparms);
6707 if (in_decl)
6708 error ("provided for %q+D", in_decl);
6711 return error_mark_node;
6713 /* We can't pass a pack expansion to a non-pack parameter of an alias
6714 template (DR 1430). */
6715 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6716 && variadic_args_p
6717 && nargs - variadic_args_p < nparms - variadic_p)
6719 if (complain & tf_error)
6721 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6723 tree arg = TREE_VEC_ELT (inner_args, i);
6724 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6726 if (PACK_EXPANSION_P (arg)
6727 && !template_parameter_pack_p (parm))
6729 error ("pack expansion argument for non-pack parameter "
6730 "%qD of alias template %qD", parm, in_decl);
6731 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6732 goto found;
6735 gcc_unreachable ();
6736 found:;
6738 return error_mark_node;
6741 /* We need to evaluate the template arguments, even though this
6742 template-id may be nested within a "sizeof". */
6743 saved_unevaluated_operand = cp_unevaluated_operand;
6744 cp_unevaluated_operand = 0;
6745 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6746 c_inhibit_evaluation_warnings = 0;
6747 new_inner_args = make_tree_vec (nparms);
6748 new_args = add_outermost_template_args (args, new_inner_args);
6749 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6751 tree arg;
6752 tree parm;
6754 /* Get the Ith template parameter. */
6755 parm = TREE_VEC_ELT (parms, parm_idx);
6757 if (parm == error_mark_node)
6759 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6760 continue;
6763 /* Calculate the next argument. */
6764 if (arg_idx < nargs)
6765 arg = TREE_VEC_ELT (inner_args, arg_idx);
6766 else
6767 arg = NULL_TREE;
6769 if (template_parameter_pack_p (TREE_VALUE (parm))
6770 && !(arg && ARGUMENT_PACK_P (arg)))
6772 /* All remaining arguments will be placed in the
6773 template parameter pack PARM. */
6774 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6775 inner_args, arg_idx,
6776 new_args, &lost,
6777 in_decl, complain);
6779 /* Store this argument. */
6780 if (arg == error_mark_node)
6781 lost++;
6782 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6784 /* We are done with all of the arguments. */
6785 arg_idx = nargs;
6787 continue;
6789 else if (arg)
6791 if (PACK_EXPANSION_P (arg))
6793 /* We don't know how many args we have yet, just
6794 use the unconverted ones for now. */
6795 new_inner_args = inner_args;
6796 break;
6799 else if (require_all_args)
6801 /* There must be a default arg in this case. */
6802 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6803 complain, in_decl);
6804 /* The position of the first default template argument,
6805 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6806 Record that. */
6807 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6808 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6810 else
6811 break;
6813 if (arg == error_mark_node)
6815 if (complain & tf_error)
6816 error ("template argument %d is invalid", arg_idx + 1);
6818 else if (!arg)
6819 /* This only occurs if there was an error in the template
6820 parameter list itself (which we would already have
6821 reported) that we are trying to recover from, e.g., a class
6822 template with a parameter list such as
6823 template<typename..., typename>. */
6824 ++lost;
6825 else
6826 arg = convert_template_argument (TREE_VALUE (parm),
6827 arg, new_args, complain,
6828 parm_idx, in_decl);
6830 if (arg == error_mark_node)
6831 lost++;
6832 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6834 cp_unevaluated_operand = saved_unevaluated_operand;
6835 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6837 if (lost)
6838 return error_mark_node;
6840 #ifdef ENABLE_CHECKING
6841 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6843 TREE_VEC_LENGTH (new_inner_args));
6844 #endif
6846 return new_inner_args;
6849 /* Like coerce_template_parms. If PARMS represents all template
6850 parameters levels, this function returns a vector of vectors
6851 representing all the resulting argument levels. Note that in this
6852 case, only the innermost arguments are coerced because the
6853 outermost ones are supposed to have been coerced already.
6855 Otherwise, if PARMS represents only (the innermost) vector of
6856 parameters, this function returns a vector containing just the
6857 innermost resulting arguments. */
6859 static tree
6860 coerce_innermost_template_parms (tree parms,
6861 tree args,
6862 tree in_decl,
6863 tsubst_flags_t complain,
6864 bool require_all_args,
6865 bool use_default_args)
6867 int parms_depth = TMPL_PARMS_DEPTH (parms);
6868 int args_depth = TMPL_ARGS_DEPTH (args);
6869 tree coerced_args;
6871 if (parms_depth > 1)
6873 coerced_args = make_tree_vec (parms_depth);
6874 tree level;
6875 int cur_depth;
6877 for (level = parms, cur_depth = parms_depth;
6878 parms_depth > 0 && level != NULL_TREE;
6879 level = TREE_CHAIN (level), --cur_depth)
6881 tree l;
6882 if (cur_depth == args_depth)
6883 l = coerce_template_parms (TREE_VALUE (level),
6884 args, in_decl, complain,
6885 require_all_args,
6886 use_default_args);
6887 else
6888 l = TMPL_ARGS_LEVEL (args, cur_depth);
6890 if (l == error_mark_node)
6891 return error_mark_node;
6893 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6896 else
6897 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6898 args, in_decl, complain,
6899 require_all_args,
6900 use_default_args);
6901 return coerced_args;
6904 /* Returns 1 if template args OT and NT are equivalent. */
6906 static int
6907 template_args_equal (tree ot, tree nt)
6909 if (nt == ot)
6910 return 1;
6911 if (nt == NULL_TREE || ot == NULL_TREE)
6912 return false;
6914 if (TREE_CODE (nt) == TREE_VEC)
6915 /* For member templates */
6916 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6917 else if (PACK_EXPANSION_P (ot))
6918 return (PACK_EXPANSION_P (nt)
6919 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6920 PACK_EXPANSION_PATTERN (nt))
6921 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6922 PACK_EXPANSION_EXTRA_ARGS (nt)));
6923 else if (ARGUMENT_PACK_P (ot))
6925 int i, len;
6926 tree opack, npack;
6928 if (!ARGUMENT_PACK_P (nt))
6929 return 0;
6931 opack = ARGUMENT_PACK_ARGS (ot);
6932 npack = ARGUMENT_PACK_ARGS (nt);
6933 len = TREE_VEC_LENGTH (opack);
6934 if (TREE_VEC_LENGTH (npack) != len)
6935 return 0;
6936 for (i = 0; i < len; ++i)
6937 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6938 TREE_VEC_ELT (npack, i)))
6939 return 0;
6940 return 1;
6942 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6944 /* We get here probably because we are in the middle of substituting
6945 into the pattern of a pack expansion. In that case the
6946 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6947 interested in. So we want to use the initial pack argument for
6948 the comparison. */
6949 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6950 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6951 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6952 return template_args_equal (ot, nt);
6954 else if (TYPE_P (nt))
6955 return TYPE_P (ot) && same_type_p (ot, nt);
6956 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6957 return 0;
6958 else
6959 return cp_tree_equal (ot, nt);
6962 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6963 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6964 NEWARG_PTR with the offending arguments if they are non-NULL. */
6966 static int
6967 comp_template_args_with_info (tree oldargs, tree newargs,
6968 tree *oldarg_ptr, tree *newarg_ptr)
6970 int i;
6972 if (oldargs == newargs)
6973 return 1;
6975 if (!oldargs || !newargs)
6976 return 0;
6978 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6979 return 0;
6981 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6983 tree nt = TREE_VEC_ELT (newargs, i);
6984 tree ot = TREE_VEC_ELT (oldargs, i);
6986 if (! template_args_equal (ot, nt))
6988 if (oldarg_ptr != NULL)
6989 *oldarg_ptr = ot;
6990 if (newarg_ptr != NULL)
6991 *newarg_ptr = nt;
6992 return 0;
6995 return 1;
6998 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6999 of template arguments. Returns 0 otherwise. */
7002 comp_template_args (tree oldargs, tree newargs)
7004 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7007 static void
7008 add_pending_template (tree d)
7010 tree ti = (TYPE_P (d)
7011 ? CLASSTYPE_TEMPLATE_INFO (d)
7012 : DECL_TEMPLATE_INFO (d));
7013 struct pending_template *pt;
7014 int level;
7016 if (TI_PENDING_TEMPLATE_FLAG (ti))
7017 return;
7019 /* We are called both from instantiate_decl, where we've already had a
7020 tinst_level pushed, and instantiate_template, where we haven't.
7021 Compensate. */
7022 level = !current_tinst_level || current_tinst_level->decl != d;
7024 if (level)
7025 push_tinst_level (d);
7027 pt = ggc_alloc_pending_template ();
7028 pt->next = NULL;
7029 pt->tinst = current_tinst_level;
7030 if (last_pending_template)
7031 last_pending_template->next = pt;
7032 else
7033 pending_templates = pt;
7035 last_pending_template = pt;
7037 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7039 if (level)
7040 pop_tinst_level ();
7044 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7045 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7046 documentation for TEMPLATE_ID_EXPR. */
7048 tree
7049 lookup_template_function (tree fns, tree arglist)
7051 tree type;
7053 if (fns == error_mark_node || arglist == error_mark_node)
7054 return error_mark_node;
7056 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7058 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7060 error ("%q#D is not a function template", fns);
7061 return error_mark_node;
7064 if (BASELINK_P (fns))
7066 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7067 unknown_type_node,
7068 BASELINK_FUNCTIONS (fns),
7069 arglist);
7070 return fns;
7073 type = TREE_TYPE (fns);
7074 if (TREE_CODE (fns) == OVERLOAD || !type)
7075 type = unknown_type_node;
7077 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7080 /* Within the scope of a template class S<T>, the name S gets bound
7081 (in build_self_reference) to a TYPE_DECL for the class, not a
7082 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7083 or one of its enclosing classes, and that type is a template,
7084 return the associated TEMPLATE_DECL. Otherwise, the original
7085 DECL is returned.
7087 Also handle the case when DECL is a TREE_LIST of ambiguous
7088 injected-class-names from different bases. */
7090 tree
7091 maybe_get_template_decl_from_type_decl (tree decl)
7093 if (decl == NULL_TREE)
7094 return decl;
7096 /* DR 176: A lookup that finds an injected-class-name (10.2
7097 [class.member.lookup]) can result in an ambiguity in certain cases
7098 (for example, if it is found in more than one base class). If all of
7099 the injected-class-names that are found refer to specializations of
7100 the same class template, and if the name is followed by a
7101 template-argument-list, the reference refers to the class template
7102 itself and not a specialization thereof, and is not ambiguous. */
7103 if (TREE_CODE (decl) == TREE_LIST)
7105 tree t, tmpl = NULL_TREE;
7106 for (t = decl; t; t = TREE_CHAIN (t))
7108 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7109 if (!tmpl)
7110 tmpl = elt;
7111 else if (tmpl != elt)
7112 break;
7114 if (tmpl && t == NULL_TREE)
7115 return tmpl;
7116 else
7117 return decl;
7120 return (decl != NULL_TREE
7121 && DECL_SELF_REFERENCE_P (decl)
7122 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7123 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7126 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7127 parameters, find the desired type.
7129 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7131 IN_DECL, if non-NULL, is the template declaration we are trying to
7132 instantiate.
7134 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7135 the class we are looking up.
7137 Issue error and warning messages under control of COMPLAIN.
7139 If the template class is really a local class in a template
7140 function, then the FUNCTION_CONTEXT is the function in which it is
7141 being instantiated.
7143 ??? Note that this function is currently called *twice* for each
7144 template-id: the first time from the parser, while creating the
7145 incomplete type (finish_template_type), and the second type during the
7146 real instantiation (instantiate_template_class). This is surely something
7147 that we want to avoid. It also causes some problems with argument
7148 coercion (see convert_nontype_argument for more information on this). */
7150 static tree
7151 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7152 int entering_scope, tsubst_flags_t complain)
7154 tree templ = NULL_TREE, parmlist;
7155 tree t;
7156 void **slot;
7157 spec_entry *entry;
7158 spec_entry elt;
7159 hashval_t hash;
7161 if (identifier_p (d1))
7163 tree value = innermost_non_namespace_value (d1);
7164 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7165 templ = value;
7166 else
7168 if (context)
7169 push_decl_namespace (context);
7170 templ = lookup_name (d1);
7171 templ = maybe_get_template_decl_from_type_decl (templ);
7172 if (context)
7173 pop_decl_namespace ();
7175 if (templ)
7176 context = DECL_CONTEXT (templ);
7178 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7180 tree type = TREE_TYPE (d1);
7182 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7183 an implicit typename for the second A. Deal with it. */
7184 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7185 type = TREE_TYPE (type);
7187 if (CLASSTYPE_TEMPLATE_INFO (type))
7189 templ = CLASSTYPE_TI_TEMPLATE (type);
7190 d1 = DECL_NAME (templ);
7193 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7194 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7196 templ = TYPE_TI_TEMPLATE (d1);
7197 d1 = DECL_NAME (templ);
7199 else if (TREE_CODE (d1) == TEMPLATE_DECL
7200 && DECL_TEMPLATE_RESULT (d1)
7201 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7203 templ = d1;
7204 d1 = DECL_NAME (templ);
7205 context = DECL_CONTEXT (templ);
7207 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7209 templ = d1;
7210 d1 = DECL_NAME (templ);
7213 /* Issue an error message if we didn't find a template. */
7214 if (! templ)
7216 if (complain & tf_error)
7217 error ("%qT is not a template", d1);
7218 return error_mark_node;
7221 if (TREE_CODE (templ) != TEMPLATE_DECL
7222 /* Make sure it's a user visible template, if it was named by
7223 the user. */
7224 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7225 && !PRIMARY_TEMPLATE_P (templ)))
7227 if (complain & tf_error)
7229 error ("non-template type %qT used as a template", d1);
7230 if (in_decl)
7231 error ("for template declaration %q+D", in_decl);
7233 return error_mark_node;
7236 complain &= ~tf_user;
7238 /* An alias that just changes the name of a template is equivalent to the
7239 other template, so if any of the arguments are pack expansions, strip
7240 the alias to avoid problems with a pack expansion passed to a non-pack
7241 alias template parameter (DR 1430). */
7242 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7243 templ = get_underlying_template (templ);
7245 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7247 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7248 template arguments */
7250 tree parm;
7251 tree arglist2;
7252 tree outer;
7254 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7256 /* Consider an example where a template template parameter declared as
7258 template <class T, class U = std::allocator<T> > class TT
7260 The template parameter level of T and U are one level larger than
7261 of TT. To proper process the default argument of U, say when an
7262 instantiation `TT<int>' is seen, we need to build the full
7263 arguments containing {int} as the innermost level. Outer levels,
7264 available when not appearing as default template argument, can be
7265 obtained from the arguments of the enclosing template.
7267 Suppose that TT is later substituted with std::vector. The above
7268 instantiation is `TT<int, std::allocator<T> >' with TT at
7269 level 1, and T at level 2, while the template arguments at level 1
7270 becomes {std::vector} and the inner level 2 is {int}. */
7272 outer = DECL_CONTEXT (templ);
7273 if (outer)
7274 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7275 else if (current_template_parms)
7276 /* This is an argument of the current template, so we haven't set
7277 DECL_CONTEXT yet. */
7278 outer = current_template_args ();
7280 if (outer)
7281 arglist = add_to_template_args (outer, arglist);
7283 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7284 complain,
7285 /*require_all_args=*/true,
7286 /*use_default_args=*/true);
7287 if (arglist2 == error_mark_node
7288 || (!uses_template_parms (arglist2)
7289 && check_instantiated_args (templ, arglist2, complain)))
7290 return error_mark_node;
7292 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7293 return parm;
7295 else
7297 tree template_type = TREE_TYPE (templ);
7298 tree gen_tmpl;
7299 tree type_decl;
7300 tree found = NULL_TREE;
7301 int arg_depth;
7302 int parm_depth;
7303 int is_dependent_type;
7304 int use_partial_inst_tmpl = false;
7306 if (template_type == error_mark_node)
7307 /* An error occurred while building the template TEMPL, and a
7308 diagnostic has most certainly been emitted for that
7309 already. Let's propagate that error. */
7310 return error_mark_node;
7312 gen_tmpl = most_general_template (templ);
7313 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7314 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7315 arg_depth = TMPL_ARGS_DEPTH (arglist);
7317 if (arg_depth == 1 && parm_depth > 1)
7319 /* We've been given an incomplete set of template arguments.
7320 For example, given:
7322 template <class T> struct S1 {
7323 template <class U> struct S2 {};
7324 template <class U> struct S2<U*> {};
7327 we will be called with an ARGLIST of `U*', but the
7328 TEMPLATE will be `template <class T> template
7329 <class U> struct S1<T>::S2'. We must fill in the missing
7330 arguments. */
7331 arglist
7332 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7333 arglist);
7334 arg_depth = TMPL_ARGS_DEPTH (arglist);
7337 /* Now we should have enough arguments. */
7338 gcc_assert (parm_depth == arg_depth);
7340 /* From here on, we're only interested in the most general
7341 template. */
7343 /* Calculate the BOUND_ARGS. These will be the args that are
7344 actually tsubst'd into the definition to create the
7345 instantiation. */
7346 if (parm_depth > 1)
7348 /* We have multiple levels of arguments to coerce, at once. */
7349 int i;
7350 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7352 tree bound_args = make_tree_vec (parm_depth);
7354 for (i = saved_depth,
7355 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7356 i > 0 && t != NULL_TREE;
7357 --i, t = TREE_CHAIN (t))
7359 tree a;
7360 if (i == saved_depth)
7361 a = coerce_template_parms (TREE_VALUE (t),
7362 arglist, gen_tmpl,
7363 complain,
7364 /*require_all_args=*/true,
7365 /*use_default_args=*/true);
7366 else
7367 /* Outer levels should have already been coerced. */
7368 a = TMPL_ARGS_LEVEL (arglist, i);
7370 /* Don't process further if one of the levels fails. */
7371 if (a == error_mark_node)
7373 /* Restore the ARGLIST to its full size. */
7374 TREE_VEC_LENGTH (arglist) = saved_depth;
7375 return error_mark_node;
7378 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7380 /* We temporarily reduce the length of the ARGLIST so
7381 that coerce_template_parms will see only the arguments
7382 corresponding to the template parameters it is
7383 examining. */
7384 TREE_VEC_LENGTH (arglist)--;
7387 /* Restore the ARGLIST to its full size. */
7388 TREE_VEC_LENGTH (arglist) = saved_depth;
7390 arglist = bound_args;
7392 else
7393 arglist
7394 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7395 INNERMOST_TEMPLATE_ARGS (arglist),
7396 gen_tmpl,
7397 complain,
7398 /*require_all_args=*/true,
7399 /*use_default_args=*/true);
7401 if (arglist == error_mark_node)
7402 /* We were unable to bind the arguments. */
7403 return error_mark_node;
7405 /* In the scope of a template class, explicit references to the
7406 template class refer to the type of the template, not any
7407 instantiation of it. For example, in:
7409 template <class T> class C { void f(C<T>); }
7411 the `C<T>' is just the same as `C'. Outside of the
7412 class, however, such a reference is an instantiation. */
7413 if ((entering_scope
7414 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7415 || currently_open_class (template_type))
7416 /* comp_template_args is expensive, check it last. */
7417 && comp_template_args (TYPE_TI_ARGS (template_type),
7418 arglist))
7419 return template_type;
7421 /* If we already have this specialization, return it. */
7422 elt.tmpl = gen_tmpl;
7423 elt.args = arglist;
7424 hash = hash_specialization (&elt);
7425 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7426 &elt, hash);
7428 if (entry)
7429 return entry->spec;
7431 is_dependent_type = uses_template_parms (arglist);
7433 /* If the deduced arguments are invalid, then the binding
7434 failed. */
7435 if (!is_dependent_type
7436 && check_instantiated_args (gen_tmpl,
7437 INNERMOST_TEMPLATE_ARGS (arglist),
7438 complain))
7439 return error_mark_node;
7441 if (!is_dependent_type
7442 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7443 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7444 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7446 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7447 DECL_NAME (gen_tmpl),
7448 /*tag_scope=*/ts_global);
7449 return found;
7452 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7453 complain, in_decl);
7454 if (context == error_mark_node)
7455 return error_mark_node;
7457 if (!context)
7458 context = global_namespace;
7460 /* Create the type. */
7461 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7463 if (!is_dependent_type)
7465 set_current_access_from_decl (TYPE_NAME (template_type));
7466 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7467 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7468 arglist, complain, in_decl),
7469 SCOPED_ENUM_P (template_type), NULL);
7471 else
7473 /* We don't want to call start_enum for this type, since
7474 the values for the enumeration constants may involve
7475 template parameters. And, no one should be interested
7476 in the enumeration constants for such a type. */
7477 t = cxx_make_type (ENUMERAL_TYPE);
7478 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7480 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7481 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7482 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7484 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7486 /* The user referred to a specialization of an alias
7487 template represented by GEN_TMPL.
7489 [temp.alias]/2 says:
7491 When a template-id refers to the specialization of an
7492 alias template, it is equivalent to the associated
7493 type obtained by substitution of its
7494 template-arguments for the template-parameters in the
7495 type-id of the alias template. */
7497 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7498 /* Note that the call above (by indirectly calling
7499 register_specialization in tsubst_decl) registers the
7500 TYPE_DECL representing the specialization of the alias
7501 template. So next time someone substitutes ARGLIST for
7502 the template parms into the alias template (GEN_TMPL),
7503 she'll get that TYPE_DECL back. */
7505 if (t == error_mark_node)
7506 return t;
7508 else if (CLASS_TYPE_P (template_type))
7510 t = make_class_type (TREE_CODE (template_type));
7511 CLASSTYPE_DECLARED_CLASS (t)
7512 = CLASSTYPE_DECLARED_CLASS (template_type);
7513 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7514 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7516 /* A local class. Make sure the decl gets registered properly. */
7517 if (context == current_function_decl)
7518 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7520 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7521 /* This instantiation is another name for the primary
7522 template type. Set the TYPE_CANONICAL field
7523 appropriately. */
7524 TYPE_CANONICAL (t) = template_type;
7525 else if (any_template_arguments_need_structural_equality_p (arglist))
7526 /* Some of the template arguments require structural
7527 equality testing, so this template class requires
7528 structural equality testing. */
7529 SET_TYPE_STRUCTURAL_EQUALITY (t);
7531 else
7532 gcc_unreachable ();
7534 /* If we called start_enum or pushtag above, this information
7535 will already be set up. */
7536 if (!TYPE_NAME (t))
7538 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7540 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7541 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7542 DECL_SOURCE_LOCATION (type_decl)
7543 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7545 else
7546 type_decl = TYPE_NAME (t);
7548 if (CLASS_TYPE_P (template_type))
7550 TREE_PRIVATE (type_decl)
7551 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7552 TREE_PROTECTED (type_decl)
7553 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7554 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7556 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7557 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7561 /* Let's consider the explicit specialization of a member
7562 of a class template specialization that is implicitely instantiated,
7563 e.g.:
7564 template<class T>
7565 struct S
7567 template<class U> struct M {}; //#0
7570 template<>
7571 template<>
7572 struct S<int>::M<char> //#1
7574 int i;
7576 [temp.expl.spec]/4 says this is valid.
7578 In this case, when we write:
7579 S<int>::M<char> m;
7581 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7582 the one of #0.
7584 When we encounter #1, we want to store the partial instantiation
7585 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7587 For all cases other than this "explicit specialization of member of a
7588 class template", we just want to store the most general template into
7589 the CLASSTYPE_TI_TEMPLATE of M.
7591 This case of "explicit specialization of member of a class template"
7592 only happens when:
7593 1/ the enclosing class is an instantiation of, and therefore not
7594 the same as, the context of the most general template, and
7595 2/ we aren't looking at the partial instantiation itself, i.e.
7596 the innermost arguments are not the same as the innermost parms of
7597 the most general template.
7599 So it's only when 1/ and 2/ happens that we want to use the partial
7600 instantiation of the member template in lieu of its most general
7601 template. */
7603 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7604 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7605 /* the enclosing class must be an instantiation... */
7606 && CLASS_TYPE_P (context)
7607 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7609 tree partial_inst_args;
7610 TREE_VEC_LENGTH (arglist)--;
7611 ++processing_template_decl;
7612 partial_inst_args =
7613 tsubst (INNERMOST_TEMPLATE_ARGS
7614 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7615 arglist, complain, NULL_TREE);
7616 --processing_template_decl;
7617 TREE_VEC_LENGTH (arglist)++;
7618 use_partial_inst_tmpl =
7619 /*...and we must not be looking at the partial instantiation
7620 itself. */
7621 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7622 partial_inst_args);
7625 if (!use_partial_inst_tmpl)
7626 /* This case is easy; there are no member templates involved. */
7627 found = gen_tmpl;
7628 else
7630 /* This is a full instantiation of a member template. Find
7631 the partial instantiation of which this is an instance. */
7633 /* Temporarily reduce by one the number of levels in the ARGLIST
7634 so as to avoid comparing the last set of arguments. */
7635 TREE_VEC_LENGTH (arglist)--;
7636 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7637 TREE_VEC_LENGTH (arglist)++;
7638 /* FOUND is either a proper class type, or an alias
7639 template specialization. In the later case, it's a
7640 TYPE_DECL, resulting from the substituting of arguments
7641 for parameters in the TYPE_DECL of the alias template
7642 done earlier. So be careful while getting the template
7643 of FOUND. */
7644 found = TREE_CODE (found) == TYPE_DECL
7645 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7646 : CLASSTYPE_TI_TEMPLATE (found);
7649 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7651 elt.spec = t;
7652 slot = htab_find_slot_with_hash (type_specializations,
7653 &elt, hash, INSERT);
7654 entry = ggc_alloc_spec_entry ();
7655 *entry = elt;
7656 *slot = entry;
7658 /* Note this use of the partial instantiation so we can check it
7659 later in maybe_process_partial_specialization. */
7660 DECL_TEMPLATE_INSTANTIATIONS (templ)
7661 = tree_cons (arglist, t,
7662 DECL_TEMPLATE_INSTANTIATIONS (templ));
7664 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type)
7665 /* Now that the type has been registered on the instantiations
7666 list, we set up the enumerators. Because the enumeration
7667 constants may involve the enumeration type itself, we make
7668 sure to register the type first, and then create the
7669 constants. That way, doing tsubst_expr for the enumeration
7670 constants won't result in recursive calls here; we'll find
7671 the instantiation and exit above. */
7672 tsubst_enum (template_type, t, arglist);
7674 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7675 /* If the type makes use of template parameters, the
7676 code that generates debugging information will crash. */
7677 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7679 /* Possibly limit visibility based on template args. */
7680 TREE_PUBLIC (type_decl) = 1;
7681 determine_visibility (type_decl);
7683 return t;
7687 /* Wrapper for lookup_template_class_1. */
7689 tree
7690 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7691 int entering_scope, tsubst_flags_t complain)
7693 tree ret;
7694 timevar_push (TV_TEMPLATE_INST);
7695 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7696 entering_scope, complain);
7697 timevar_pop (TV_TEMPLATE_INST);
7698 return ret;
7701 struct pair_fn_data
7703 tree_fn_t fn;
7704 void *data;
7705 /* True when we should also visit template parameters that occur in
7706 non-deduced contexts. */
7707 bool include_nondeduced_p;
7708 struct pointer_set_t *visited;
7711 /* Called from for_each_template_parm via walk_tree. */
7713 static tree
7714 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7716 tree t = *tp;
7717 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7718 tree_fn_t fn = pfd->fn;
7719 void *data = pfd->data;
7721 if (TYPE_P (t)
7722 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7723 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7724 pfd->include_nondeduced_p))
7725 return error_mark_node;
7727 switch (TREE_CODE (t))
7729 case RECORD_TYPE:
7730 if (TYPE_PTRMEMFUNC_P (t))
7731 break;
7732 /* Fall through. */
7734 case UNION_TYPE:
7735 case ENUMERAL_TYPE:
7736 if (!TYPE_TEMPLATE_INFO (t))
7737 *walk_subtrees = 0;
7738 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7739 fn, data, pfd->visited,
7740 pfd->include_nondeduced_p))
7741 return error_mark_node;
7742 break;
7744 case INTEGER_TYPE:
7745 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7746 fn, data, pfd->visited,
7747 pfd->include_nondeduced_p)
7748 || for_each_template_parm (TYPE_MAX_VALUE (t),
7749 fn, data, pfd->visited,
7750 pfd->include_nondeduced_p))
7751 return error_mark_node;
7752 break;
7754 case METHOD_TYPE:
7755 /* Since we're not going to walk subtrees, we have to do this
7756 explicitly here. */
7757 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7758 pfd->visited, pfd->include_nondeduced_p))
7759 return error_mark_node;
7760 /* Fall through. */
7762 case FUNCTION_TYPE:
7763 /* Check the return type. */
7764 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7765 pfd->include_nondeduced_p))
7766 return error_mark_node;
7768 /* Check the parameter types. Since default arguments are not
7769 instantiated until they are needed, the TYPE_ARG_TYPES may
7770 contain expressions that involve template parameters. But,
7771 no-one should be looking at them yet. And, once they're
7772 instantiated, they don't contain template parameters, so
7773 there's no point in looking at them then, either. */
7775 tree parm;
7777 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7778 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7779 pfd->visited, pfd->include_nondeduced_p))
7780 return error_mark_node;
7782 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7783 want walk_tree walking into them itself. */
7784 *walk_subtrees = 0;
7786 break;
7788 case TYPEOF_TYPE:
7789 case UNDERLYING_TYPE:
7790 if (pfd->include_nondeduced_p
7791 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7792 pfd->visited,
7793 pfd->include_nondeduced_p))
7794 return error_mark_node;
7795 break;
7797 case FUNCTION_DECL:
7798 case VAR_DECL:
7799 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7800 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7801 pfd->visited, pfd->include_nondeduced_p))
7802 return error_mark_node;
7803 /* Fall through. */
7805 case PARM_DECL:
7806 case CONST_DECL:
7807 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7808 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7809 pfd->visited, pfd->include_nondeduced_p))
7810 return error_mark_node;
7811 if (DECL_CONTEXT (t)
7812 && pfd->include_nondeduced_p
7813 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7814 pfd->visited, pfd->include_nondeduced_p))
7815 return error_mark_node;
7816 break;
7818 case BOUND_TEMPLATE_TEMPLATE_PARM:
7819 /* Record template parameters such as `T' inside `TT<T>'. */
7820 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7821 pfd->include_nondeduced_p))
7822 return error_mark_node;
7823 /* Fall through. */
7825 case TEMPLATE_TEMPLATE_PARM:
7826 case TEMPLATE_TYPE_PARM:
7827 case TEMPLATE_PARM_INDEX:
7828 if (fn && (*fn)(t, data))
7829 return error_mark_node;
7830 else if (!fn)
7831 return error_mark_node;
7832 break;
7834 case TEMPLATE_DECL:
7835 /* A template template parameter is encountered. */
7836 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7837 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7838 pfd->include_nondeduced_p))
7839 return error_mark_node;
7841 /* Already substituted template template parameter */
7842 *walk_subtrees = 0;
7843 break;
7845 case TYPENAME_TYPE:
7846 if (!fn
7847 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7848 data, pfd->visited,
7849 pfd->include_nondeduced_p))
7850 return error_mark_node;
7851 break;
7853 case CONSTRUCTOR:
7854 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7855 && pfd->include_nondeduced_p
7856 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7857 (TREE_TYPE (t)), fn, data,
7858 pfd->visited, pfd->include_nondeduced_p))
7859 return error_mark_node;
7860 break;
7862 case INDIRECT_REF:
7863 case COMPONENT_REF:
7864 /* If there's no type, then this thing must be some expression
7865 involving template parameters. */
7866 if (!fn && !TREE_TYPE (t))
7867 return error_mark_node;
7868 break;
7870 case MODOP_EXPR:
7871 case CAST_EXPR:
7872 case IMPLICIT_CONV_EXPR:
7873 case REINTERPRET_CAST_EXPR:
7874 case CONST_CAST_EXPR:
7875 case STATIC_CAST_EXPR:
7876 case DYNAMIC_CAST_EXPR:
7877 case ARROW_EXPR:
7878 case DOTSTAR_EXPR:
7879 case TYPEID_EXPR:
7880 case PSEUDO_DTOR_EXPR:
7881 if (!fn)
7882 return error_mark_node;
7883 break;
7885 default:
7886 break;
7889 /* We didn't find any template parameters we liked. */
7890 return NULL_TREE;
7893 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7894 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7895 call FN with the parameter and the DATA.
7896 If FN returns nonzero, the iteration is terminated, and
7897 for_each_template_parm returns 1. Otherwise, the iteration
7898 continues. If FN never returns a nonzero value, the value
7899 returned by for_each_template_parm is 0. If FN is NULL, it is
7900 considered to be the function which always returns 1.
7902 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7903 parameters that occur in non-deduced contexts. When false, only
7904 visits those template parameters that can be deduced. */
7906 static int
7907 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7908 struct pointer_set_t *visited,
7909 bool include_nondeduced_p)
7911 struct pair_fn_data pfd;
7912 int result;
7914 /* Set up. */
7915 pfd.fn = fn;
7916 pfd.data = data;
7917 pfd.include_nondeduced_p = include_nondeduced_p;
7919 /* Walk the tree. (Conceptually, we would like to walk without
7920 duplicates, but for_each_template_parm_r recursively calls
7921 for_each_template_parm, so we would need to reorganize a fair
7922 bit to use walk_tree_without_duplicates, so we keep our own
7923 visited list.) */
7924 if (visited)
7925 pfd.visited = visited;
7926 else
7927 pfd.visited = pointer_set_create ();
7928 result = cp_walk_tree (&t,
7929 for_each_template_parm_r,
7930 &pfd,
7931 pfd.visited) != NULL_TREE;
7933 /* Clean up. */
7934 if (!visited)
7936 pointer_set_destroy (pfd.visited);
7937 pfd.visited = 0;
7940 return result;
7943 /* Returns true if T depends on any template parameter. */
7946 uses_template_parms (tree t)
7948 bool dependent_p;
7949 int saved_processing_template_decl;
7951 saved_processing_template_decl = processing_template_decl;
7952 if (!saved_processing_template_decl)
7953 processing_template_decl = 1;
7954 if (TYPE_P (t))
7955 dependent_p = dependent_type_p (t);
7956 else if (TREE_CODE (t) == TREE_VEC)
7957 dependent_p = any_dependent_template_arguments_p (t);
7958 else if (TREE_CODE (t) == TREE_LIST)
7959 dependent_p = (uses_template_parms (TREE_VALUE (t))
7960 || uses_template_parms (TREE_CHAIN (t)));
7961 else if (TREE_CODE (t) == TYPE_DECL)
7962 dependent_p = dependent_type_p (TREE_TYPE (t));
7963 else if (DECL_P (t)
7964 || EXPR_P (t)
7965 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7966 || TREE_CODE (t) == OVERLOAD
7967 || BASELINK_P (t)
7968 || identifier_p (t)
7969 || TREE_CODE (t) == TRAIT_EXPR
7970 || TREE_CODE (t) == CONSTRUCTOR
7971 || CONSTANT_CLASS_P (t))
7972 dependent_p = (type_dependent_expression_p (t)
7973 || value_dependent_expression_p (t));
7974 else
7976 gcc_assert (t == error_mark_node);
7977 dependent_p = false;
7980 processing_template_decl = saved_processing_template_decl;
7982 return dependent_p;
7985 /* Returns true iff current_function_decl is an incompletely instantiated
7986 template. Useful instead of processing_template_decl because the latter
7987 is set to 0 during fold_non_dependent_expr. */
7989 bool
7990 in_template_function (void)
7992 tree fn = current_function_decl;
7993 bool ret;
7994 ++processing_template_decl;
7995 ret = (fn && DECL_LANG_SPECIFIC (fn)
7996 && DECL_TEMPLATE_INFO (fn)
7997 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7998 --processing_template_decl;
7999 return ret;
8002 /* Returns true if T depends on any template parameter with level LEVEL. */
8005 uses_template_parms_level (tree t, int level)
8007 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8008 /*include_nondeduced_p=*/true);
8011 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8012 ill-formed translation unit, i.e. a variable or function that isn't
8013 usable in a constant expression. */
8015 static inline bool
8016 neglectable_inst_p (tree d)
8018 return (DECL_P (d)
8019 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8020 : decl_maybe_constant_var_p (d)));
8023 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8024 neglectable and instantiated from within an erroneous instantiation. */
8026 static bool
8027 limit_bad_template_recursion (tree decl)
8029 struct tinst_level *lev = current_tinst_level;
8030 int errs = errorcount + sorrycount;
8031 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8032 return false;
8034 for (; lev; lev = lev->next)
8035 if (neglectable_inst_p (lev->decl))
8036 break;
8038 return (lev && errs > lev->errors);
8041 static int tinst_depth;
8042 extern int max_tinst_depth;
8043 int depth_reached;
8045 static GTY(()) struct tinst_level *last_error_tinst_level;
8047 /* We're starting to instantiate D; record the template instantiation context
8048 for diagnostics and to restore it later. */
8051 push_tinst_level (tree d)
8053 struct tinst_level *new_level;
8055 if (tinst_depth >= max_tinst_depth)
8057 last_error_tinst_level = current_tinst_level;
8058 if (TREE_CODE (d) == TREE_LIST)
8059 error ("template instantiation depth exceeds maximum of %d (use "
8060 "-ftemplate-depth= to increase the maximum) substituting %qS",
8061 max_tinst_depth, d);
8062 else
8063 error ("template instantiation depth exceeds maximum of %d (use "
8064 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8065 max_tinst_depth, d);
8067 print_instantiation_context ();
8069 return 0;
8072 /* If the current instantiation caused problems, don't let it instantiate
8073 anything else. Do allow deduction substitution and decls usable in
8074 constant expressions. */
8075 if (limit_bad_template_recursion (d))
8076 return 0;
8078 new_level = ggc_alloc_tinst_level ();
8079 new_level->decl = d;
8080 new_level->locus = input_location;
8081 new_level->errors = errorcount+sorrycount;
8082 new_level->in_system_header_p = in_system_header;
8083 new_level->next = current_tinst_level;
8084 current_tinst_level = new_level;
8086 ++tinst_depth;
8087 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8088 depth_reached = tinst_depth;
8090 return 1;
8093 /* We're done instantiating this template; return to the instantiation
8094 context. */
8096 void
8097 pop_tinst_level (void)
8099 /* Restore the filename and line number stashed away when we started
8100 this instantiation. */
8101 input_location = current_tinst_level->locus;
8102 current_tinst_level = current_tinst_level->next;
8103 --tinst_depth;
8106 /* We're instantiating a deferred template; restore the template
8107 instantiation context in which the instantiation was requested, which
8108 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8110 static tree
8111 reopen_tinst_level (struct tinst_level *level)
8113 struct tinst_level *t;
8115 tinst_depth = 0;
8116 for (t = level; t; t = t->next)
8117 ++tinst_depth;
8119 current_tinst_level = level;
8120 pop_tinst_level ();
8121 if (current_tinst_level)
8122 current_tinst_level->errors = errorcount+sorrycount;
8123 return level->decl;
8126 /* Returns the TINST_LEVEL which gives the original instantiation
8127 context. */
8129 struct tinst_level *
8130 outermost_tinst_level (void)
8132 struct tinst_level *level = current_tinst_level;
8133 if (level)
8134 while (level->next)
8135 level = level->next;
8136 return level;
8139 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8140 vector of template arguments, as for tsubst.
8142 Returns an appropriate tsubst'd friend declaration. */
8144 static tree
8145 tsubst_friend_function (tree decl, tree args)
8147 tree new_friend;
8149 if (TREE_CODE (decl) == FUNCTION_DECL
8150 && DECL_TEMPLATE_INSTANTIATION (decl)
8151 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8152 /* This was a friend declared with an explicit template
8153 argument list, e.g.:
8155 friend void f<>(T);
8157 to indicate that f was a template instantiation, not a new
8158 function declaration. Now, we have to figure out what
8159 instantiation of what template. */
8161 tree template_id, arglist, fns;
8162 tree new_args;
8163 tree tmpl;
8164 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8166 /* Friend functions are looked up in the containing namespace scope.
8167 We must enter that scope, to avoid finding member functions of the
8168 current class with same name. */
8169 push_nested_namespace (ns);
8170 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8171 tf_warning_or_error, NULL_TREE,
8172 /*integral_constant_expression_p=*/false);
8173 pop_nested_namespace (ns);
8174 arglist = tsubst (DECL_TI_ARGS (decl), args,
8175 tf_warning_or_error, NULL_TREE);
8176 template_id = lookup_template_function (fns, arglist);
8178 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8179 tmpl = determine_specialization (template_id, new_friend,
8180 &new_args,
8181 /*need_member_template=*/0,
8182 TREE_VEC_LENGTH (args),
8183 tsk_none);
8184 return instantiate_template (tmpl, new_args, tf_error);
8187 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8189 /* The NEW_FRIEND will look like an instantiation, to the
8190 compiler, but is not an instantiation from the point of view of
8191 the language. For example, we might have had:
8193 template <class T> struct S {
8194 template <class U> friend void f(T, U);
8197 Then, in S<int>, template <class U> void f(int, U) is not an
8198 instantiation of anything. */
8199 if (new_friend == error_mark_node)
8200 return error_mark_node;
8202 DECL_USE_TEMPLATE (new_friend) = 0;
8203 if (TREE_CODE (decl) == TEMPLATE_DECL)
8205 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8206 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8207 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8210 /* The mangled name for the NEW_FRIEND is incorrect. The function
8211 is not a template instantiation and should not be mangled like
8212 one. Therefore, we forget the mangling here; we'll recompute it
8213 later if we need it. */
8214 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8216 SET_DECL_RTL (new_friend, NULL);
8217 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8220 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8222 tree old_decl;
8223 tree new_friend_template_info;
8224 tree new_friend_result_template_info;
8225 tree ns;
8226 int new_friend_is_defn;
8228 /* We must save some information from NEW_FRIEND before calling
8229 duplicate decls since that function will free NEW_FRIEND if
8230 possible. */
8231 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8232 new_friend_is_defn =
8233 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8234 (template_for_substitution (new_friend)))
8235 != NULL_TREE);
8236 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8238 /* This declaration is a `primary' template. */
8239 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8241 new_friend_result_template_info
8242 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8244 else
8245 new_friend_result_template_info = NULL_TREE;
8247 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8248 if (new_friend_is_defn)
8249 DECL_INITIAL (new_friend) = error_mark_node;
8251 /* Inside pushdecl_namespace_level, we will push into the
8252 current namespace. However, the friend function should go
8253 into the namespace of the template. */
8254 ns = decl_namespace_context (new_friend);
8255 push_nested_namespace (ns);
8256 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8257 pop_nested_namespace (ns);
8259 if (old_decl == error_mark_node)
8260 return error_mark_node;
8262 if (old_decl != new_friend)
8264 /* This new friend declaration matched an existing
8265 declaration. For example, given:
8267 template <class T> void f(T);
8268 template <class U> class C {
8269 template <class T> friend void f(T) {}
8272 the friend declaration actually provides the definition
8273 of `f', once C has been instantiated for some type. So,
8274 old_decl will be the out-of-class template declaration,
8275 while new_friend is the in-class definition.
8277 But, if `f' was called before this point, the
8278 instantiation of `f' will have DECL_TI_ARGS corresponding
8279 to `T' but not to `U', references to which might appear
8280 in the definition of `f'. Previously, the most general
8281 template for an instantiation of `f' was the out-of-class
8282 version; now it is the in-class version. Therefore, we
8283 run through all specialization of `f', adding to their
8284 DECL_TI_ARGS appropriately. In particular, they need a
8285 new set of outer arguments, corresponding to the
8286 arguments for this class instantiation.
8288 The same situation can arise with something like this:
8290 friend void f(int);
8291 template <class T> class C {
8292 friend void f(T) {}
8295 when `C<int>' is instantiated. Now, `f(int)' is defined
8296 in the class. */
8298 if (!new_friend_is_defn)
8299 /* On the other hand, if the in-class declaration does
8300 *not* provide a definition, then we don't want to alter
8301 existing definitions. We can just leave everything
8302 alone. */
8304 else
8306 tree new_template = TI_TEMPLATE (new_friend_template_info);
8307 tree new_args = TI_ARGS (new_friend_template_info);
8309 /* Overwrite whatever template info was there before, if
8310 any, with the new template information pertaining to
8311 the declaration. */
8312 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8314 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8316 /* We should have called reregister_specialization in
8317 duplicate_decls. */
8318 gcc_assert (retrieve_specialization (new_template,
8319 new_args, 0)
8320 == old_decl);
8322 /* Instantiate it if the global has already been used. */
8323 if (DECL_ODR_USED (old_decl))
8324 instantiate_decl (old_decl, /*defer_ok=*/true,
8325 /*expl_inst_class_mem_p=*/false);
8327 else
8329 tree t;
8331 /* Indicate that the old function template is a partial
8332 instantiation. */
8333 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8334 = new_friend_result_template_info;
8336 gcc_assert (new_template
8337 == most_general_template (new_template));
8338 gcc_assert (new_template != old_decl);
8340 /* Reassign any specializations already in the hash table
8341 to the new more general template, and add the
8342 additional template args. */
8343 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8344 t != NULL_TREE;
8345 t = TREE_CHAIN (t))
8347 tree spec = TREE_VALUE (t);
8348 spec_entry elt;
8350 elt.tmpl = old_decl;
8351 elt.args = DECL_TI_ARGS (spec);
8352 elt.spec = NULL_TREE;
8354 htab_remove_elt (decl_specializations, &elt);
8356 DECL_TI_ARGS (spec)
8357 = add_outermost_template_args (new_args,
8358 DECL_TI_ARGS (spec));
8360 register_specialization
8361 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8364 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8368 /* The information from NEW_FRIEND has been merged into OLD_DECL
8369 by duplicate_decls. */
8370 new_friend = old_decl;
8373 else
8375 tree context = DECL_CONTEXT (new_friend);
8376 bool dependent_p;
8378 /* In the code
8379 template <class T> class C {
8380 template <class U> friend void C1<U>::f (); // case 1
8381 friend void C2<T>::f (); // case 2
8383 we only need to make sure CONTEXT is a complete type for
8384 case 2. To distinguish between the two cases, we note that
8385 CONTEXT of case 1 remains dependent type after tsubst while
8386 this isn't true for case 2. */
8387 ++processing_template_decl;
8388 dependent_p = dependent_type_p (context);
8389 --processing_template_decl;
8391 if (!dependent_p
8392 && !complete_type_or_else (context, NULL_TREE))
8393 return error_mark_node;
8395 if (COMPLETE_TYPE_P (context))
8397 /* Check to see that the declaration is really present, and,
8398 possibly obtain an improved declaration. */
8399 tree fn = check_classfn (context,
8400 new_friend, NULL_TREE);
8402 if (fn)
8403 new_friend = fn;
8407 return new_friend;
8410 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8411 template arguments, as for tsubst.
8413 Returns an appropriate tsubst'd friend type or error_mark_node on
8414 failure. */
8416 static tree
8417 tsubst_friend_class (tree friend_tmpl, tree args)
8419 tree friend_type;
8420 tree tmpl;
8421 tree context;
8423 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8425 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8426 return TREE_TYPE (t);
8429 context = CP_DECL_CONTEXT (friend_tmpl);
8431 if (context != global_namespace)
8433 if (TREE_CODE (context) == NAMESPACE_DECL)
8434 push_nested_namespace (context);
8435 else
8436 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8439 /* Look for a class template declaration. We look for hidden names
8440 because two friend declarations of the same template are the
8441 same. For example, in:
8443 struct A {
8444 template <typename> friend class F;
8446 template <typename> struct B {
8447 template <typename> friend class F;
8450 both F templates are the same. */
8451 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8452 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8454 /* But, if we don't find one, it might be because we're in a
8455 situation like this:
8457 template <class T>
8458 struct S {
8459 template <class U>
8460 friend struct S;
8463 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8464 for `S<int>', not the TEMPLATE_DECL. */
8465 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8467 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8468 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8471 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8473 /* The friend template has already been declared. Just
8474 check to see that the declarations match, and install any new
8475 default parameters. We must tsubst the default parameters,
8476 of course. We only need the innermost template parameters
8477 because that is all that redeclare_class_template will look
8478 at. */
8479 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8480 > TMPL_ARGS_DEPTH (args))
8482 tree parms;
8483 location_t saved_input_location;
8484 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8485 args, tf_warning_or_error);
8487 saved_input_location = input_location;
8488 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8489 redeclare_class_template (TREE_TYPE (tmpl), parms);
8490 input_location = saved_input_location;
8494 friend_type = TREE_TYPE (tmpl);
8496 else
8498 /* The friend template has not already been declared. In this
8499 case, the instantiation of the template class will cause the
8500 injection of this template into the global scope. */
8501 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8502 if (tmpl == error_mark_node)
8503 return error_mark_node;
8505 /* The new TMPL is not an instantiation of anything, so we
8506 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8507 the new type because that is supposed to be the corresponding
8508 template decl, i.e., TMPL. */
8509 DECL_USE_TEMPLATE (tmpl) = 0;
8510 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8511 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8512 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8513 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8515 /* Inject this template into the global scope. */
8516 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8519 if (context != global_namespace)
8521 if (TREE_CODE (context) == NAMESPACE_DECL)
8522 pop_nested_namespace (context);
8523 else
8524 pop_nested_class ();
8527 return friend_type;
8530 /* Returns zero if TYPE cannot be completed later due to circularity.
8531 Otherwise returns one. */
8533 static int
8534 can_complete_type_without_circularity (tree type)
8536 if (type == NULL_TREE || type == error_mark_node)
8537 return 0;
8538 else if (COMPLETE_TYPE_P (type))
8539 return 1;
8540 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8541 return can_complete_type_without_circularity (TREE_TYPE (type));
8542 else if (CLASS_TYPE_P (type)
8543 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8544 return 0;
8545 else
8546 return 1;
8549 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8551 /* Apply any attributes which had to be deferred until instantiation
8552 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8553 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8555 static void
8556 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8557 tree args, tsubst_flags_t complain, tree in_decl)
8559 tree last_dep = NULL_TREE;
8560 tree t;
8561 tree *p;
8563 for (t = attributes; t; t = TREE_CHAIN (t))
8564 if (ATTR_IS_DEPENDENT (t))
8566 last_dep = t;
8567 attributes = copy_list (attributes);
8568 break;
8571 if (DECL_P (*decl_p))
8573 if (TREE_TYPE (*decl_p) == error_mark_node)
8574 return;
8575 p = &DECL_ATTRIBUTES (*decl_p);
8577 else
8578 p = &TYPE_ATTRIBUTES (*decl_p);
8580 if (last_dep)
8582 tree late_attrs = NULL_TREE;
8583 tree *q = &late_attrs;
8585 for (*p = attributes; *p; )
8587 t = *p;
8588 if (ATTR_IS_DEPENDENT (t))
8590 *p = TREE_CHAIN (t);
8591 TREE_CHAIN (t) = NULL_TREE;
8592 if (flag_openmp
8593 && is_attribute_p ("omp declare simd",
8594 get_attribute_name (t))
8595 && TREE_VALUE (t))
8597 tree clauses = TREE_VALUE (TREE_VALUE (t));
8598 clauses = tsubst_omp_clauses (clauses, true, args,
8599 complain, in_decl);
8600 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8601 clauses = finish_omp_clauses (clauses);
8602 tree parms = DECL_ARGUMENTS (*decl_p);
8603 clauses
8604 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8605 if (clauses)
8606 TREE_VALUE (TREE_VALUE (t)) = clauses;
8607 else
8608 TREE_VALUE (t) = NULL_TREE;
8610 /* If the first attribute argument is an identifier, don't
8611 pass it through tsubst. Attributes like mode, format,
8612 cleanup and several target specific attributes expect it
8613 unmodified. */
8614 else if (attribute_takes_identifier_p (get_attribute_name (t)))
8616 tree chain
8617 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8618 in_decl,
8619 /*integral_constant_expression_p=*/false);
8620 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8621 TREE_VALUE (t)
8622 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8623 chain);
8625 else
8626 TREE_VALUE (t)
8627 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8628 /*integral_constant_expression_p=*/false);
8629 *q = t;
8630 q = &TREE_CHAIN (t);
8632 else
8633 p = &TREE_CHAIN (t);
8636 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8640 /* Perform (or defer) access check for typedefs that were referenced
8641 from within the template TMPL code.
8642 This is a subroutine of instantiate_decl and instantiate_class_template.
8643 TMPL is the template to consider and TARGS is the list of arguments of
8644 that template. */
8646 static void
8647 perform_typedefs_access_check (tree tmpl, tree targs)
8649 location_t saved_location;
8650 unsigned i;
8651 qualified_typedef_usage_t *iter;
8653 if (!tmpl
8654 || (!CLASS_TYPE_P (tmpl)
8655 && TREE_CODE (tmpl) != FUNCTION_DECL))
8656 return;
8658 saved_location = input_location;
8659 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8661 tree type_decl = iter->typedef_decl;
8662 tree type_scope = iter->context;
8664 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8665 continue;
8667 if (uses_template_parms (type_decl))
8668 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8669 if (uses_template_parms (type_scope))
8670 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8672 /* Make access check error messages point to the location
8673 of the use of the typedef. */
8674 input_location = iter->locus;
8675 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8676 type_decl, type_decl,
8677 tf_warning_or_error);
8679 input_location = saved_location;
8682 static tree
8683 instantiate_class_template_1 (tree type)
8685 tree templ, args, pattern, t, member;
8686 tree typedecl;
8687 tree pbinfo;
8688 tree base_list;
8689 unsigned int saved_maximum_field_alignment;
8690 tree fn_context;
8692 if (type == error_mark_node)
8693 return error_mark_node;
8695 if (COMPLETE_OR_OPEN_TYPE_P (type)
8696 || uses_template_parms (type))
8697 return type;
8699 /* Figure out which template is being instantiated. */
8700 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8701 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8703 /* Determine what specialization of the original template to
8704 instantiate. */
8705 t = most_specialized_class (type, templ, tf_warning_or_error);
8706 if (t == error_mark_node)
8708 TYPE_BEING_DEFINED (type) = 1;
8709 return error_mark_node;
8711 else if (t)
8713 /* This TYPE is actually an instantiation of a partial
8714 specialization. We replace the innermost set of ARGS with
8715 the arguments appropriate for substitution. For example,
8716 given:
8718 template <class T> struct S {};
8719 template <class T> struct S<T*> {};
8721 and supposing that we are instantiating S<int*>, ARGS will
8722 presently be {int*} -- but we need {int}. */
8723 pattern = TREE_TYPE (t);
8724 args = TREE_PURPOSE (t);
8726 else
8728 pattern = TREE_TYPE (templ);
8729 args = CLASSTYPE_TI_ARGS (type);
8732 /* If the template we're instantiating is incomplete, then clearly
8733 there's nothing we can do. */
8734 if (!COMPLETE_TYPE_P (pattern))
8735 return type;
8737 /* If we've recursively instantiated too many templates, stop. */
8738 if (! push_tinst_level (type))
8739 return type;
8741 /* Now we're really doing the instantiation. Mark the type as in
8742 the process of being defined. */
8743 TYPE_BEING_DEFINED (type) = 1;
8745 /* We may be in the middle of deferred access check. Disable
8746 it now. */
8747 push_deferring_access_checks (dk_no_deferred);
8749 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8750 if (!fn_context)
8751 push_to_top_level ();
8752 /* Use #pragma pack from the template context. */
8753 saved_maximum_field_alignment = maximum_field_alignment;
8754 maximum_field_alignment = TYPE_PRECISION (pattern);
8756 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8758 /* Set the input location to the most specialized template definition.
8759 This is needed if tsubsting causes an error. */
8760 typedecl = TYPE_MAIN_DECL (pattern);
8761 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8762 DECL_SOURCE_LOCATION (typedecl);
8764 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8765 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8766 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8767 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8768 if (ANON_AGGR_TYPE_P (pattern))
8769 SET_ANON_AGGR_TYPE_P (type);
8770 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8772 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8773 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8774 /* Adjust visibility for template arguments. */
8775 determine_visibility (TYPE_MAIN_DECL (type));
8777 if (CLASS_TYPE_P (type))
8778 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8780 pbinfo = TYPE_BINFO (pattern);
8782 /* We should never instantiate a nested class before its enclosing
8783 class; we need to look up the nested class by name before we can
8784 instantiate it, and that lookup should instantiate the enclosing
8785 class. */
8786 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8787 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8789 base_list = NULL_TREE;
8790 if (BINFO_N_BASE_BINFOS (pbinfo))
8792 tree pbase_binfo;
8793 tree pushed_scope;
8794 int i;
8796 /* We must enter the scope containing the type, as that is where
8797 the accessibility of types named in dependent bases are
8798 looked up from. */
8799 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8801 /* Substitute into each of the bases to determine the actual
8802 basetypes. */
8803 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8805 tree base;
8806 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8807 tree expanded_bases = NULL_TREE;
8808 int idx, len = 1;
8810 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8812 expanded_bases =
8813 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8814 args, tf_error, NULL_TREE);
8815 if (expanded_bases == error_mark_node)
8816 continue;
8818 len = TREE_VEC_LENGTH (expanded_bases);
8821 for (idx = 0; idx < len; idx++)
8823 if (expanded_bases)
8824 /* Extract the already-expanded base class. */
8825 base = TREE_VEC_ELT (expanded_bases, idx);
8826 else
8827 /* Substitute to figure out the base class. */
8828 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8829 NULL_TREE);
8831 if (base == error_mark_node)
8832 continue;
8834 base_list = tree_cons (access, base, base_list);
8835 if (BINFO_VIRTUAL_P (pbase_binfo))
8836 TREE_TYPE (base_list) = integer_type_node;
8840 /* The list is now in reverse order; correct that. */
8841 base_list = nreverse (base_list);
8843 if (pushed_scope)
8844 pop_scope (pushed_scope);
8846 /* Now call xref_basetypes to set up all the base-class
8847 information. */
8848 xref_basetypes (type, base_list);
8850 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8851 (int) ATTR_FLAG_TYPE_IN_PLACE,
8852 args, tf_error, NULL_TREE);
8853 fixup_attribute_variants (type);
8855 /* Now that our base classes are set up, enter the scope of the
8856 class, so that name lookups into base classes, etc. will work
8857 correctly. This is precisely analogous to what we do in
8858 begin_class_definition when defining an ordinary non-template
8859 class, except we also need to push the enclosing classes. */
8860 push_nested_class (type);
8862 /* Now members are processed in the order of declaration. */
8863 for (member = CLASSTYPE_DECL_LIST (pattern);
8864 member; member = TREE_CHAIN (member))
8866 tree t = TREE_VALUE (member);
8868 if (TREE_PURPOSE (member))
8870 if (TYPE_P (t))
8872 /* Build new CLASSTYPE_NESTED_UTDS. */
8874 tree newtag;
8875 bool class_template_p;
8877 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8878 && TYPE_LANG_SPECIFIC (t)
8879 && CLASSTYPE_IS_TEMPLATE (t));
8880 /* If the member is a class template, then -- even after
8881 substitution -- there may be dependent types in the
8882 template argument list for the class. We increment
8883 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8884 that function will assume that no types are dependent
8885 when outside of a template. */
8886 if (class_template_p)
8887 ++processing_template_decl;
8888 newtag = tsubst (t, args, tf_error, NULL_TREE);
8889 if (class_template_p)
8890 --processing_template_decl;
8891 if (newtag == error_mark_node)
8892 continue;
8894 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8896 tree name = TYPE_IDENTIFIER (t);
8898 if (class_template_p)
8899 /* Unfortunately, lookup_template_class sets
8900 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8901 instantiation (i.e., for the type of a member
8902 template class nested within a template class.)
8903 This behavior is required for
8904 maybe_process_partial_specialization to work
8905 correctly, but is not accurate in this case;
8906 the TAG is not an instantiation of anything.
8907 (The corresponding TEMPLATE_DECL is an
8908 instantiation, but the TYPE is not.) */
8909 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8911 /* Now, we call pushtag to put this NEWTAG into the scope of
8912 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8913 pushtag calling push_template_decl. We don't have to do
8914 this for enums because it will already have been done in
8915 tsubst_enum. */
8916 if (name)
8917 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8918 pushtag (name, newtag, /*tag_scope=*/ts_current);
8921 else if (DECL_DECLARES_FUNCTION_P (t))
8923 /* Build new TYPE_METHODS. */
8924 tree r;
8926 if (TREE_CODE (t) == TEMPLATE_DECL)
8927 ++processing_template_decl;
8928 r = tsubst (t, args, tf_error, NULL_TREE);
8929 if (TREE_CODE (t) == TEMPLATE_DECL)
8930 --processing_template_decl;
8931 set_current_access_from_decl (r);
8932 finish_member_declaration (r);
8933 /* Instantiate members marked with attribute used. */
8934 if (r != error_mark_node && DECL_PRESERVE_P (r))
8935 mark_used (r);
8936 if (TREE_CODE (r) == FUNCTION_DECL
8937 && DECL_OMP_DECLARE_REDUCTION_P (r))
8938 cp_check_omp_declare_reduction (r);
8940 else
8942 /* Build new TYPE_FIELDS. */
8943 if (TREE_CODE (t) == STATIC_ASSERT)
8945 tree condition;
8947 ++c_inhibit_evaluation_warnings;
8948 condition =
8949 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8950 tf_warning_or_error, NULL_TREE,
8951 /*integral_constant_expression_p=*/true);
8952 --c_inhibit_evaluation_warnings;
8954 finish_static_assert (condition,
8955 STATIC_ASSERT_MESSAGE (t),
8956 STATIC_ASSERT_SOURCE_LOCATION (t),
8957 /*member_p=*/true);
8959 else if (TREE_CODE (t) != CONST_DECL)
8961 tree r;
8962 tree vec = NULL_TREE;
8963 int len = 1;
8965 /* The file and line for this declaration, to
8966 assist in error message reporting. Since we
8967 called push_tinst_level above, we don't need to
8968 restore these. */
8969 input_location = DECL_SOURCE_LOCATION (t);
8971 if (TREE_CODE (t) == TEMPLATE_DECL)
8972 ++processing_template_decl;
8973 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8974 if (TREE_CODE (t) == TEMPLATE_DECL)
8975 --processing_template_decl;
8977 if (TREE_CODE (r) == TREE_VEC)
8979 /* A capture pack became multiple fields. */
8980 vec = r;
8981 len = TREE_VEC_LENGTH (vec);
8984 for (int i = 0; i < len; ++i)
8986 if (vec)
8987 r = TREE_VEC_ELT (vec, i);
8988 if (VAR_P (r))
8990 /* In [temp.inst]:
8992 [t]he initialization (and any associated
8993 side-effects) of a static data member does
8994 not occur unless the static data member is
8995 itself used in a way that requires the
8996 definition of the static data member to
8997 exist.
8999 Therefore, we do not substitute into the
9000 initialized for the static data member here. */
9001 finish_static_data_member_decl
9003 /*init=*/NULL_TREE,
9004 /*init_const_expr_p=*/false,
9005 /*asmspec_tree=*/NULL_TREE,
9006 /*flags=*/0);
9007 /* Instantiate members marked with attribute used. */
9008 if (r != error_mark_node && DECL_PRESERVE_P (r))
9009 mark_used (r);
9011 else if (TREE_CODE (r) == FIELD_DECL)
9013 /* Determine whether R has a valid type and can be
9014 completed later. If R is invalid, then its type
9015 is replaced by error_mark_node. */
9016 tree rtype = TREE_TYPE (r);
9017 if (can_complete_type_without_circularity (rtype))
9018 complete_type (rtype);
9020 if (!COMPLETE_TYPE_P (rtype))
9022 cxx_incomplete_type_error (r, rtype);
9023 TREE_TYPE (r) = error_mark_node;
9027 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9028 such a thing will already have been added to the field
9029 list by tsubst_enum in finish_member_declaration in the
9030 CLASSTYPE_NESTED_UTDS case above. */
9031 if (!(TREE_CODE (r) == TYPE_DECL
9032 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9033 && DECL_ARTIFICIAL (r)))
9035 set_current_access_from_decl (r);
9036 finish_member_declaration (r);
9042 else
9044 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9045 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9047 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9049 tree friend_type = t;
9050 bool adjust_processing_template_decl = false;
9052 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9054 /* template <class T> friend class C; */
9055 friend_type = tsubst_friend_class (friend_type, args);
9056 adjust_processing_template_decl = true;
9058 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9060 /* template <class T> friend class C::D; */
9061 friend_type = tsubst (friend_type, args,
9062 tf_warning_or_error, NULL_TREE);
9063 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9064 friend_type = TREE_TYPE (friend_type);
9065 adjust_processing_template_decl = true;
9067 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9068 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9070 /* This could be either
9072 friend class T::C;
9074 when dependent_type_p is false or
9076 template <class U> friend class T::C;
9078 otherwise. */
9079 friend_type = tsubst (friend_type, args,
9080 tf_warning_or_error, NULL_TREE);
9081 /* Bump processing_template_decl for correct
9082 dependent_type_p calculation. */
9083 ++processing_template_decl;
9084 if (dependent_type_p (friend_type))
9085 adjust_processing_template_decl = true;
9086 --processing_template_decl;
9088 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9089 && hidden_name_p (TYPE_NAME (friend_type)))
9091 /* friend class C;
9093 where C hasn't been declared yet. Let's lookup name
9094 from namespace scope directly, bypassing any name that
9095 come from dependent base class. */
9096 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9098 /* The call to xref_tag_from_type does injection for friend
9099 classes. */
9100 push_nested_namespace (ns);
9101 friend_type =
9102 xref_tag_from_type (friend_type, NULL_TREE,
9103 /*tag_scope=*/ts_current);
9104 pop_nested_namespace (ns);
9106 else if (uses_template_parms (friend_type))
9107 /* friend class C<T>; */
9108 friend_type = tsubst (friend_type, args,
9109 tf_warning_or_error, NULL_TREE);
9110 /* Otherwise it's
9112 friend class C;
9114 where C is already declared or
9116 friend class C<int>;
9118 We don't have to do anything in these cases. */
9120 if (adjust_processing_template_decl)
9121 /* Trick make_friend_class into realizing that the friend
9122 we're adding is a template, not an ordinary class. It's
9123 important that we use make_friend_class since it will
9124 perform some error-checking and output cross-reference
9125 information. */
9126 ++processing_template_decl;
9128 if (friend_type != error_mark_node)
9129 make_friend_class (type, friend_type, /*complain=*/false);
9131 if (adjust_processing_template_decl)
9132 --processing_template_decl;
9134 else
9136 /* Build new DECL_FRIENDLIST. */
9137 tree r;
9139 /* The file and line for this declaration, to
9140 assist in error message reporting. Since we
9141 called push_tinst_level above, we don't need to
9142 restore these. */
9143 input_location = DECL_SOURCE_LOCATION (t);
9145 if (TREE_CODE (t) == TEMPLATE_DECL)
9147 ++processing_template_decl;
9148 push_deferring_access_checks (dk_no_check);
9151 r = tsubst_friend_function (t, args);
9152 add_friend (type, r, /*complain=*/false);
9153 if (TREE_CODE (t) == TEMPLATE_DECL)
9155 pop_deferring_access_checks ();
9156 --processing_template_decl;
9162 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9164 tree decl = lambda_function (type);
9165 if (decl)
9167 if (!DECL_TEMPLATE_INFO (decl)
9168 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9169 instantiate_decl (decl, false, false);
9171 /* We need to instantiate the capture list from the template
9172 after we've instantiated the closure members, but before we
9173 consider adding the conversion op. Also keep any captures
9174 that may have been added during instantiation of the op(). */
9175 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9176 tree tmpl_cap
9177 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9178 args, tf_warning_or_error, NULL_TREE,
9179 false, false);
9181 LAMBDA_EXPR_CAPTURE_LIST (expr)
9182 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9184 maybe_add_lambda_conv_op (type);
9186 else
9187 gcc_assert (errorcount);
9190 /* Set the file and line number information to whatever is given for
9191 the class itself. This puts error messages involving generated
9192 implicit functions at a predictable point, and the same point
9193 that would be used for non-template classes. */
9194 input_location = DECL_SOURCE_LOCATION (typedecl);
9196 unreverse_member_declarations (type);
9197 finish_struct_1 (type);
9198 TYPE_BEING_DEFINED (type) = 0;
9200 /* We don't instantiate default arguments for member functions. 14.7.1:
9202 The implicit instantiation of a class template specialization causes
9203 the implicit instantiation of the declarations, but not of the
9204 definitions or default arguments, of the class member functions,
9205 member classes, static data members and member templates.... */
9207 /* Some typedefs referenced from within the template code need to be access
9208 checked at template instantiation time, i.e now. These types were
9209 added to the template at parsing time. Let's get those and perform
9210 the access checks then. */
9211 perform_typedefs_access_check (pattern, args);
9212 perform_deferred_access_checks (tf_warning_or_error);
9213 pop_nested_class ();
9214 maximum_field_alignment = saved_maximum_field_alignment;
9215 if (!fn_context)
9216 pop_from_top_level ();
9217 pop_deferring_access_checks ();
9218 pop_tinst_level ();
9220 /* The vtable for a template class can be emitted in any translation
9221 unit in which the class is instantiated. When there is no key
9222 method, however, finish_struct_1 will already have added TYPE to
9223 the keyed_classes list. */
9224 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9225 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9227 return type;
9230 /* Wrapper for instantiate_class_template_1. */
9232 tree
9233 instantiate_class_template (tree type)
9235 tree ret;
9236 timevar_push (TV_TEMPLATE_INST);
9237 ret = instantiate_class_template_1 (type);
9238 timevar_pop (TV_TEMPLATE_INST);
9239 return ret;
9242 static tree
9243 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9245 tree r;
9247 if (!t)
9248 r = t;
9249 else if (TYPE_P (t))
9250 r = tsubst (t, args, complain, in_decl);
9251 else
9253 if (!(complain & tf_warning))
9254 ++c_inhibit_evaluation_warnings;
9255 r = tsubst_expr (t, args, complain, in_decl,
9256 /*integral_constant_expression_p=*/true);
9257 if (!(complain & tf_warning))
9258 --c_inhibit_evaluation_warnings;
9259 /* Preserve the raw-reference nature of T. */
9260 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9261 && REFERENCE_REF_P (r))
9262 r = TREE_OPERAND (r, 0);
9264 return r;
9267 /* Given a function parameter pack TMPL_PARM and some function parameters
9268 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9269 and set *SPEC_P to point at the next point in the list. */
9271 static tree
9272 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9274 /* Collect all of the extra "packed" parameters into an
9275 argument pack. */
9276 tree parmvec;
9277 tree parmtypevec;
9278 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9279 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9280 tree spec_parm = *spec_p;
9281 int i, len;
9283 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9284 if (tmpl_parm
9285 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9286 break;
9288 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9289 parmvec = make_tree_vec (len);
9290 parmtypevec = make_tree_vec (len);
9291 spec_parm = *spec_p;
9292 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9294 TREE_VEC_ELT (parmvec, i) = spec_parm;
9295 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9298 /* Build the argument packs. */
9299 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9300 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9301 TREE_TYPE (argpack) = argtypepack;
9302 *spec_p = spec_parm;
9304 return argpack;
9307 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9308 NONTYPE_ARGUMENT_PACK. */
9310 static tree
9311 make_fnparm_pack (tree spec_parm)
9313 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9316 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9317 pack expansion. */
9319 static bool
9320 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9322 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9323 if (i >= TREE_VEC_LENGTH (vec))
9324 return false;
9325 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9329 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9331 static tree
9332 make_argument_pack_select (tree arg_pack, unsigned index)
9334 tree aps = make_node (ARGUMENT_PACK_SELECT);
9336 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9337 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9339 return aps;
9342 /* This is a subroutine of tsubst_pack_expansion.
9344 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9345 mechanism to store the (non complete list of) arguments of the
9346 substitution and return a non substituted pack expansion, in order
9347 to wait for when we have enough arguments to really perform the
9348 substitution. */
9350 static bool
9351 use_pack_expansion_extra_args_p (tree parm_packs,
9352 int arg_pack_len,
9353 bool has_empty_arg)
9355 /* If one pack has an expansion and another pack has a normal
9356 argument or if one pack has an empty argument and an another
9357 one hasn't then tsubst_pack_expansion cannot perform the
9358 substitution and need to fall back on the
9359 PACK_EXPANSION_EXTRA mechanism. */
9360 if (parm_packs == NULL_TREE)
9361 return false;
9362 else if (has_empty_arg)
9363 return true;
9365 bool has_expansion_arg = false;
9366 for (int i = 0 ; i < arg_pack_len; ++i)
9368 bool has_non_expansion_arg = false;
9369 for (tree parm_pack = parm_packs;
9370 parm_pack;
9371 parm_pack = TREE_CHAIN (parm_pack))
9373 tree arg = TREE_VALUE (parm_pack);
9375 if (argument_pack_element_is_expansion_p (arg, i))
9376 has_expansion_arg = true;
9377 else
9378 has_non_expansion_arg = true;
9381 if (has_expansion_arg && has_non_expansion_arg)
9382 return true;
9384 return false;
9387 /* [temp.variadic]/6 says that:
9389 The instantiation of a pack expansion [...]
9390 produces a list E1,E2, ..., En, where N is the number of elements
9391 in the pack expansion parameters.
9393 This subroutine of tsubst_pack_expansion produces one of these Ei.
9395 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9396 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9397 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9398 INDEX is the index 'i' of the element Ei to produce. ARGS,
9399 COMPLAIN, and IN_DECL are the same parameters as for the
9400 tsubst_pack_expansion function.
9402 The function returns the resulting Ei upon successful completion,
9403 or error_mark_node.
9405 Note that this function possibly modifies the ARGS parameter, so
9406 it's the responsibility of the caller to restore it. */
9408 static tree
9409 gen_elem_of_pack_expansion_instantiation (tree pattern,
9410 tree parm_packs,
9411 unsigned index,
9412 tree args /* This parm gets
9413 modified. */,
9414 tsubst_flags_t complain,
9415 tree in_decl)
9417 tree t;
9418 bool ith_elem_is_expansion = false;
9420 /* For each parameter pack, change the substitution of the parameter
9421 pack to the ith argument in its argument pack, then expand the
9422 pattern. */
9423 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9425 tree parm = TREE_PURPOSE (pack);
9426 tree arg_pack = TREE_VALUE (pack);
9427 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9429 ith_elem_is_expansion |=
9430 argument_pack_element_is_expansion_p (arg_pack, index);
9432 /* Select the Ith argument from the pack. */
9433 if (TREE_CODE (parm) == PARM_DECL
9434 || TREE_CODE (parm) == FIELD_DECL)
9436 if (index == 0)
9438 aps = make_argument_pack_select (arg_pack, index);
9439 mark_used (parm);
9440 register_local_specialization (aps, parm);
9442 else
9443 aps = retrieve_local_specialization (parm);
9445 else
9447 int idx, level;
9448 template_parm_level_and_index (parm, &level, &idx);
9450 if (index == 0)
9452 aps = make_argument_pack_select (arg_pack, index);
9453 /* Update the corresponding argument. */
9454 TMPL_ARG (args, level, idx) = aps;
9456 else
9457 /* Re-use the ARGUMENT_PACK_SELECT. */
9458 aps = TMPL_ARG (args, level, idx);
9460 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9463 /* Substitute into the PATTERN with the (possibly altered)
9464 arguments. */
9465 if (!TYPE_P (pattern))
9466 t = tsubst_expr (pattern, args, complain, in_decl,
9467 /*integral_constant_expression_p=*/false);
9468 else
9469 t = tsubst (pattern, args, complain, in_decl);
9471 /* If the Ith argument pack element is a pack expansion, then
9472 the Ith element resulting from the substituting is going to
9473 be a pack expansion as well. */
9474 if (ith_elem_is_expansion)
9475 t = make_pack_expansion (t);
9477 return t;
9480 /* Substitute ARGS into T, which is an pack expansion
9481 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9482 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9483 (if only a partial substitution could be performed) or
9484 ERROR_MARK_NODE if there was an error. */
9485 tree
9486 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9487 tree in_decl)
9489 tree pattern;
9490 tree pack, packs = NULL_TREE;
9491 bool unsubstituted_packs = false;
9492 int i, len = -1;
9493 tree result;
9494 struct pointer_map_t *saved_local_specializations = NULL;
9495 bool need_local_specializations = false;
9496 int levels;
9498 gcc_assert (PACK_EXPANSION_P (t));
9499 pattern = PACK_EXPANSION_PATTERN (t);
9501 /* Add in any args remembered from an earlier partial instantiation. */
9502 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9504 levels = TMPL_ARGS_DEPTH (args);
9506 /* Determine the argument packs that will instantiate the parameter
9507 packs used in the expansion expression. While we're at it,
9508 compute the number of arguments to be expanded and make sure it
9509 is consistent. */
9510 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9511 pack = TREE_CHAIN (pack))
9513 tree parm_pack = TREE_VALUE (pack);
9514 tree arg_pack = NULL_TREE;
9515 tree orig_arg = NULL_TREE;
9516 int level = 0;
9518 if (TREE_CODE (parm_pack) == BASES)
9520 if (BASES_DIRECT (parm_pack))
9521 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9522 args, complain, in_decl, false));
9523 else
9524 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9525 args, complain, in_decl, false));
9527 if (TREE_CODE (parm_pack) == PARM_DECL)
9529 if (PACK_EXPANSION_LOCAL_P (t))
9530 arg_pack = retrieve_local_specialization (parm_pack);
9531 else
9533 /* We can't rely on local_specializations for a parameter
9534 name used later in a function declaration (such as in a
9535 late-specified return type). Even if it exists, it might
9536 have the wrong value for a recursive call. Just make a
9537 dummy decl, since it's only used for its type. */
9538 arg_pack = tsubst_decl (parm_pack, args, complain);
9539 if (arg_pack && DECL_PACK_P (arg_pack))
9540 /* Partial instantiation of the parm_pack, we can't build
9541 up an argument pack yet. */
9542 arg_pack = NULL_TREE;
9543 else
9544 arg_pack = make_fnparm_pack (arg_pack);
9545 need_local_specializations = true;
9548 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9549 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9550 else
9552 int idx;
9553 template_parm_level_and_index (parm_pack, &level, &idx);
9555 if (level <= levels)
9556 arg_pack = TMPL_ARG (args, level, idx);
9559 orig_arg = arg_pack;
9560 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9561 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9563 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9564 /* This can only happen if we forget to expand an argument
9565 pack somewhere else. Just return an error, silently. */
9567 result = make_tree_vec (1);
9568 TREE_VEC_ELT (result, 0) = error_mark_node;
9569 return result;
9572 if (arg_pack)
9574 int my_len =
9575 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9577 /* Don't bother trying to do a partial substitution with
9578 incomplete packs; we'll try again after deduction. */
9579 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9580 return t;
9582 if (len < 0)
9583 len = my_len;
9584 else if (len != my_len)
9586 if (!(complain & tf_error))
9587 /* Fail quietly. */;
9588 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9589 error ("mismatched argument pack lengths while expanding "
9590 "%<%T%>",
9591 pattern);
9592 else
9593 error ("mismatched argument pack lengths while expanding "
9594 "%<%E%>",
9595 pattern);
9596 return error_mark_node;
9599 /* Keep track of the parameter packs and their corresponding
9600 argument packs. */
9601 packs = tree_cons (parm_pack, arg_pack, packs);
9602 TREE_TYPE (packs) = orig_arg;
9604 else
9606 /* We can't substitute for this parameter pack. We use a flag as
9607 well as the missing_level counter because function parameter
9608 packs don't have a level. */
9609 unsubstituted_packs = true;
9613 /* We cannot expand this expansion expression, because we don't have
9614 all of the argument packs we need. */
9615 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9617 /* We got some full packs, but we can't substitute them in until we
9618 have values for all the packs. So remember these until then. */
9620 t = make_pack_expansion (pattern);
9621 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9622 return t;
9624 else if (unsubstituted_packs)
9626 /* There were no real arguments, we're just replacing a parameter
9627 pack with another version of itself. Substitute into the
9628 pattern and return a PACK_EXPANSION_*. The caller will need to
9629 deal with that. */
9630 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9631 t = tsubst_expr (pattern, args, complain, in_decl,
9632 /*integral_constant_expression_p=*/false);
9633 else
9634 t = tsubst (pattern, args, complain, in_decl);
9635 t = make_pack_expansion (t);
9636 return t;
9639 gcc_assert (len >= 0);
9641 if (need_local_specializations)
9643 /* We're in a late-specified return type, so create our own local
9644 specializations map; the current map is either NULL or (in the
9645 case of recursive unification) might have bindings that we don't
9646 want to use or alter. */
9647 saved_local_specializations = local_specializations;
9648 local_specializations = pointer_map_create ();
9651 /* For each argument in each argument pack, substitute into the
9652 pattern. */
9653 result = make_tree_vec (len);
9654 for (i = 0; i < len; ++i)
9656 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9658 args, complain,
9659 in_decl);
9660 TREE_VEC_ELT (result, i) = t;
9661 if (t == error_mark_node)
9663 result = error_mark_node;
9664 break;
9668 /* Update ARGS to restore the substitution from parameter packs to
9669 their argument packs. */
9670 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9672 tree parm = TREE_PURPOSE (pack);
9674 if (TREE_CODE (parm) == PARM_DECL
9675 || TREE_CODE (parm) == FIELD_DECL)
9676 register_local_specialization (TREE_TYPE (pack), parm);
9677 else
9679 int idx, level;
9681 if (TREE_VALUE (pack) == NULL_TREE)
9682 continue;
9684 template_parm_level_and_index (parm, &level, &idx);
9686 /* Update the corresponding argument. */
9687 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9688 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9689 TREE_TYPE (pack);
9690 else
9691 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9695 if (need_local_specializations)
9697 pointer_map_destroy (local_specializations);
9698 local_specializations = saved_local_specializations;
9701 return result;
9704 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9705 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9706 parameter packs; all parms generated from a function parameter pack will
9707 have the same DECL_PARM_INDEX. */
9709 tree
9710 get_pattern_parm (tree parm, tree tmpl)
9712 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9713 tree patparm;
9715 if (DECL_ARTIFICIAL (parm))
9717 for (patparm = DECL_ARGUMENTS (pattern);
9718 patparm; patparm = DECL_CHAIN (patparm))
9719 if (DECL_ARTIFICIAL (patparm)
9720 && DECL_NAME (parm) == DECL_NAME (patparm))
9721 break;
9723 else
9725 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9726 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9727 gcc_assert (DECL_PARM_INDEX (patparm)
9728 == DECL_PARM_INDEX (parm));
9731 return patparm;
9734 /* Substitute ARGS into the vector or list of template arguments T. */
9736 static tree
9737 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9739 tree orig_t = t;
9740 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9741 tree *elts;
9743 if (t == error_mark_node)
9744 return error_mark_node;
9746 len = TREE_VEC_LENGTH (t);
9747 elts = XALLOCAVEC (tree, len);
9749 for (i = 0; i < len; i++)
9751 tree orig_arg = TREE_VEC_ELT (t, i);
9752 tree new_arg;
9754 if (TREE_CODE (orig_arg) == TREE_VEC)
9755 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9756 else if (PACK_EXPANSION_P (orig_arg))
9758 /* Substitute into an expansion expression. */
9759 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9761 if (TREE_CODE (new_arg) == TREE_VEC)
9762 /* Add to the expanded length adjustment the number of
9763 expanded arguments. We subtract one from this
9764 measurement, because the argument pack expression
9765 itself is already counted as 1 in
9766 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9767 the argument pack is empty. */
9768 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9770 else if (ARGUMENT_PACK_P (orig_arg))
9772 /* Substitute into each of the arguments. */
9773 new_arg = TYPE_P (orig_arg)
9774 ? cxx_make_type (TREE_CODE (orig_arg))
9775 : make_node (TREE_CODE (orig_arg));
9777 SET_ARGUMENT_PACK_ARGS (
9778 new_arg,
9779 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9780 args, complain, in_decl));
9782 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9783 new_arg = error_mark_node;
9785 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9786 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9787 complain, in_decl);
9788 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9790 if (TREE_TYPE (new_arg) == error_mark_node)
9791 new_arg = error_mark_node;
9794 else
9795 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9797 if (new_arg == error_mark_node)
9798 return error_mark_node;
9800 elts[i] = new_arg;
9801 if (new_arg != orig_arg)
9802 need_new = 1;
9805 if (!need_new)
9806 return t;
9808 /* Make space for the expanded arguments coming from template
9809 argument packs. */
9810 t = make_tree_vec (len + expanded_len_adjust);
9811 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9812 arguments for a member template.
9813 In that case each TREE_VEC in ORIG_T represents a level of template
9814 arguments, and ORIG_T won't carry any non defaulted argument count.
9815 It will rather be the nested TREE_VECs that will carry one.
9816 In other words, ORIG_T carries a non defaulted argument count only
9817 if it doesn't contain any nested TREE_VEC. */
9818 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9820 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9821 count += expanded_len_adjust;
9822 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9824 for (i = 0, out = 0; i < len; i++)
9826 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9827 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9828 && TREE_CODE (elts[i]) == TREE_VEC)
9830 int idx;
9832 /* Now expand the template argument pack "in place". */
9833 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9834 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9836 else
9838 TREE_VEC_ELT (t, out) = elts[i];
9839 out++;
9843 return t;
9846 /* Return the result of substituting ARGS into the template parameters
9847 given by PARMS. If there are m levels of ARGS and m + n levels of
9848 PARMS, then the result will contain n levels of PARMS. For
9849 example, if PARMS is `template <class T> template <class U>
9850 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9851 result will be `template <int*, double, class V>'. */
9853 static tree
9854 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9856 tree r = NULL_TREE;
9857 tree* new_parms;
9859 /* When substituting into a template, we must set
9860 PROCESSING_TEMPLATE_DECL as the template parameters may be
9861 dependent if they are based on one-another, and the dependency
9862 predicates are short-circuit outside of templates. */
9863 ++processing_template_decl;
9865 for (new_parms = &r;
9866 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9867 new_parms = &(TREE_CHAIN (*new_parms)),
9868 parms = TREE_CHAIN (parms))
9870 tree new_vec =
9871 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9872 int i;
9874 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9876 tree tuple;
9878 if (parms == error_mark_node)
9879 continue;
9881 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9883 if (tuple == error_mark_node)
9884 continue;
9886 TREE_VEC_ELT (new_vec, i) =
9887 tsubst_template_parm (tuple, args, complain);
9890 *new_parms =
9891 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9892 - TMPL_ARGS_DEPTH (args)),
9893 new_vec, NULL_TREE);
9896 --processing_template_decl;
9898 return r;
9901 /* Return the result of substituting ARGS into one template parameter
9902 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9903 parameter and which TREE_PURPOSE is the default argument of the
9904 template parameter. */
9906 static tree
9907 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9909 tree default_value, parm_decl;
9911 if (args == NULL_TREE
9912 || t == NULL_TREE
9913 || t == error_mark_node)
9914 return t;
9916 gcc_assert (TREE_CODE (t) == TREE_LIST);
9918 default_value = TREE_PURPOSE (t);
9919 parm_decl = TREE_VALUE (t);
9921 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9922 if (TREE_CODE (parm_decl) == PARM_DECL
9923 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9924 parm_decl = error_mark_node;
9925 default_value = tsubst_template_arg (default_value, args,
9926 complain, NULL_TREE);
9928 return build_tree_list (default_value, parm_decl);
9931 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9932 type T. If T is not an aggregate or enumeration type, it is
9933 handled as if by tsubst. IN_DECL is as for tsubst. If
9934 ENTERING_SCOPE is nonzero, T is the context for a template which
9935 we are presently tsubst'ing. Return the substituted value. */
9937 static tree
9938 tsubst_aggr_type (tree t,
9939 tree args,
9940 tsubst_flags_t complain,
9941 tree in_decl,
9942 int entering_scope)
9944 if (t == NULL_TREE)
9945 return NULL_TREE;
9947 switch (TREE_CODE (t))
9949 case RECORD_TYPE:
9950 if (TYPE_PTRMEMFUNC_P (t))
9951 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9953 /* Else fall through. */
9954 case ENUMERAL_TYPE:
9955 case UNION_TYPE:
9956 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9958 tree argvec;
9959 tree context;
9960 tree r;
9961 int saved_unevaluated_operand;
9962 int saved_inhibit_evaluation_warnings;
9964 /* In "sizeof(X<I>)" we need to evaluate "I". */
9965 saved_unevaluated_operand = cp_unevaluated_operand;
9966 cp_unevaluated_operand = 0;
9967 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9968 c_inhibit_evaluation_warnings = 0;
9970 /* First, determine the context for the type we are looking
9971 up. */
9972 context = TYPE_CONTEXT (t);
9973 if (context && TYPE_P (context))
9975 context = tsubst_aggr_type (context, args, complain,
9976 in_decl, /*entering_scope=*/1);
9977 /* If context is a nested class inside a class template,
9978 it may still need to be instantiated (c++/33959). */
9979 context = complete_type (context);
9982 /* Then, figure out what arguments are appropriate for the
9983 type we are trying to find. For example, given:
9985 template <class T> struct S;
9986 template <class T, class U> void f(T, U) { S<U> su; }
9988 and supposing that we are instantiating f<int, double>,
9989 then our ARGS will be {int, double}, but, when looking up
9990 S we only want {double}. */
9991 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9992 complain, in_decl);
9993 if (argvec == error_mark_node)
9994 r = error_mark_node;
9995 else
9997 r = lookup_template_class (t, argvec, in_decl, context,
9998 entering_scope, complain);
9999 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10002 cp_unevaluated_operand = saved_unevaluated_operand;
10003 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10005 return r;
10007 else
10008 /* This is not a template type, so there's nothing to do. */
10009 return t;
10011 default:
10012 return tsubst (t, args, complain, in_decl);
10016 /* Substitute into the default argument ARG (a default argument for
10017 FN), which has the indicated TYPE. */
10019 tree
10020 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10022 tree saved_class_ptr = NULL_TREE;
10023 tree saved_class_ref = NULL_TREE;
10024 int errs = errorcount + sorrycount;
10026 /* This can happen in invalid code. */
10027 if (TREE_CODE (arg) == DEFAULT_ARG)
10028 return arg;
10030 /* This default argument came from a template. Instantiate the
10031 default argument here, not in tsubst. In the case of
10032 something like:
10034 template <class T>
10035 struct S {
10036 static T t();
10037 void f(T = t());
10040 we must be careful to do name lookup in the scope of S<T>,
10041 rather than in the current class. */
10042 push_access_scope (fn);
10043 /* The "this" pointer is not valid in a default argument. */
10044 if (cfun)
10046 saved_class_ptr = current_class_ptr;
10047 cp_function_chain->x_current_class_ptr = NULL_TREE;
10048 saved_class_ref = current_class_ref;
10049 cp_function_chain->x_current_class_ref = NULL_TREE;
10052 push_deferring_access_checks(dk_no_deferred);
10053 /* The default argument expression may cause implicitly defined
10054 member functions to be synthesized, which will result in garbage
10055 collection. We must treat this situation as if we were within
10056 the body of function so as to avoid collecting live data on the
10057 stack. */
10058 ++function_depth;
10059 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10060 complain, NULL_TREE,
10061 /*integral_constant_expression_p=*/false);
10062 --function_depth;
10063 pop_deferring_access_checks();
10065 /* Restore the "this" pointer. */
10066 if (cfun)
10068 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10069 cp_function_chain->x_current_class_ref = saved_class_ref;
10072 if (errorcount+sorrycount > errs
10073 && (complain & tf_warning_or_error))
10074 inform (input_location,
10075 " when instantiating default argument for call to %D", fn);
10077 /* Make sure the default argument is reasonable. */
10078 arg = check_default_argument (type, arg, complain);
10080 pop_access_scope (fn);
10082 return arg;
10085 /* Substitute into all the default arguments for FN. */
10087 static void
10088 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10090 tree arg;
10091 tree tmpl_args;
10093 tmpl_args = DECL_TI_ARGS (fn);
10095 /* If this function is not yet instantiated, we certainly don't need
10096 its default arguments. */
10097 if (uses_template_parms (tmpl_args))
10098 return;
10099 /* Don't do this again for clones. */
10100 if (DECL_CLONED_FUNCTION_P (fn))
10101 return;
10103 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10104 arg;
10105 arg = TREE_CHAIN (arg))
10106 if (TREE_PURPOSE (arg))
10107 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10108 TREE_VALUE (arg),
10109 TREE_PURPOSE (arg),
10110 complain);
10113 /* Substitute the ARGS into the T, which is a _DECL. Return the
10114 result of the substitution. Issue error and warning messages under
10115 control of COMPLAIN. */
10117 static tree
10118 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10120 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10121 location_t saved_loc;
10122 tree r = NULL_TREE;
10123 tree in_decl = t;
10124 hashval_t hash = 0;
10126 /* Set the filename and linenumber to improve error-reporting. */
10127 saved_loc = input_location;
10128 input_location = DECL_SOURCE_LOCATION (t);
10130 switch (TREE_CODE (t))
10132 case TEMPLATE_DECL:
10134 /* We can get here when processing a member function template,
10135 member class template, or template template parameter. */
10136 tree decl = DECL_TEMPLATE_RESULT (t);
10137 tree spec;
10138 tree tmpl_args;
10139 tree full_args;
10141 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10143 /* Template template parameter is treated here. */
10144 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10145 if (new_type == error_mark_node)
10146 RETURN (error_mark_node);
10147 /* If we get a real template back, return it. This can happen in
10148 the context of most_specialized_class. */
10149 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10150 return new_type;
10152 r = copy_decl (t);
10153 DECL_CHAIN (r) = NULL_TREE;
10154 TREE_TYPE (r) = new_type;
10155 DECL_TEMPLATE_RESULT (r)
10156 = build_decl (DECL_SOURCE_LOCATION (decl),
10157 TYPE_DECL, DECL_NAME (decl), new_type);
10158 DECL_TEMPLATE_PARMS (r)
10159 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10160 complain);
10161 TYPE_NAME (new_type) = r;
10162 break;
10165 /* We might already have an instance of this template.
10166 The ARGS are for the surrounding class type, so the
10167 full args contain the tsubst'd args for the context,
10168 plus the innermost args from the template decl. */
10169 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10170 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10171 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10172 /* Because this is a template, the arguments will still be
10173 dependent, even after substitution. If
10174 PROCESSING_TEMPLATE_DECL is not set, the dependency
10175 predicates will short-circuit. */
10176 ++processing_template_decl;
10177 full_args = tsubst_template_args (tmpl_args, args,
10178 complain, in_decl);
10179 --processing_template_decl;
10180 if (full_args == error_mark_node)
10181 RETURN (error_mark_node);
10183 /* If this is a default template template argument,
10184 tsubst might not have changed anything. */
10185 if (full_args == tmpl_args)
10186 RETURN (t);
10188 hash = hash_tmpl_and_args (t, full_args);
10189 spec = retrieve_specialization (t, full_args, hash);
10190 if (spec != NULL_TREE)
10192 r = spec;
10193 break;
10196 /* Make a new template decl. It will be similar to the
10197 original, but will record the current template arguments.
10198 We also create a new function declaration, which is just
10199 like the old one, but points to this new template, rather
10200 than the old one. */
10201 r = copy_decl (t);
10202 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10203 DECL_CHAIN (r) = NULL_TREE;
10205 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10207 if (TREE_CODE (decl) == TYPE_DECL
10208 && !TYPE_DECL_ALIAS_P (decl))
10210 tree new_type;
10211 ++processing_template_decl;
10212 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10213 --processing_template_decl;
10214 if (new_type == error_mark_node)
10215 RETURN (error_mark_node);
10217 TREE_TYPE (r) = new_type;
10218 /* For a partial specialization, we need to keep pointing to
10219 the primary template. */
10220 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10221 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10222 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10223 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10224 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10226 else
10228 tree new_decl;
10229 ++processing_template_decl;
10230 new_decl = tsubst (decl, args, complain, in_decl);
10231 --processing_template_decl;
10232 if (new_decl == error_mark_node)
10233 RETURN (error_mark_node);
10235 DECL_TEMPLATE_RESULT (r) = new_decl;
10236 DECL_TI_TEMPLATE (new_decl) = r;
10237 TREE_TYPE (r) = TREE_TYPE (new_decl);
10238 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10239 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10242 SET_DECL_IMPLICIT_INSTANTIATION (r);
10243 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10244 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10246 /* The template parameters for this new template are all the
10247 template parameters for the old template, except the
10248 outermost level of parameters. */
10249 DECL_TEMPLATE_PARMS (r)
10250 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10251 complain);
10253 if (PRIMARY_TEMPLATE_P (t))
10254 DECL_PRIMARY_TEMPLATE (r) = r;
10256 if (TREE_CODE (decl) != TYPE_DECL)
10257 /* Record this non-type partial instantiation. */
10258 register_specialization (r, t,
10259 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10260 false, hash);
10262 break;
10264 case FUNCTION_DECL:
10266 tree ctx;
10267 tree argvec = NULL_TREE;
10268 tree *friends;
10269 tree gen_tmpl;
10270 tree type;
10271 int member;
10272 int args_depth;
10273 int parms_depth;
10275 /* Nobody should be tsubst'ing into non-template functions. */
10276 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10278 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10280 tree spec;
10281 bool dependent_p;
10283 /* If T is not dependent, just return it. We have to
10284 increment PROCESSING_TEMPLATE_DECL because
10285 value_dependent_expression_p assumes that nothing is
10286 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10287 ++processing_template_decl;
10288 dependent_p = value_dependent_expression_p (t);
10289 --processing_template_decl;
10290 if (!dependent_p)
10291 RETURN (t);
10293 /* Calculate the most general template of which R is a
10294 specialization, and the complete set of arguments used to
10295 specialize R. */
10296 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10297 argvec = tsubst_template_args (DECL_TI_ARGS
10298 (DECL_TEMPLATE_RESULT
10299 (DECL_TI_TEMPLATE (t))),
10300 args, complain, in_decl);
10301 if (argvec == error_mark_node)
10302 RETURN (error_mark_node);
10304 /* Check to see if we already have this specialization. */
10305 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10306 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10308 if (spec)
10310 r = spec;
10311 break;
10314 /* We can see more levels of arguments than parameters if
10315 there was a specialization of a member template, like
10316 this:
10318 template <class T> struct S { template <class U> void f(); }
10319 template <> template <class U> void S<int>::f(U);
10321 Here, we'll be substituting into the specialization,
10322 because that's where we can find the code we actually
10323 want to generate, but we'll have enough arguments for
10324 the most general template.
10326 We also deal with the peculiar case:
10328 template <class T> struct S {
10329 template <class U> friend void f();
10331 template <class U> void f() {}
10332 template S<int>;
10333 template void f<double>();
10335 Here, the ARGS for the instantiation of will be {int,
10336 double}. But, we only need as many ARGS as there are
10337 levels of template parameters in CODE_PATTERN. We are
10338 careful not to get fooled into reducing the ARGS in
10339 situations like:
10341 template <class T> struct S { template <class U> void f(U); }
10342 template <class T> template <> void S<T>::f(int) {}
10344 which we can spot because the pattern will be a
10345 specialization in this case. */
10346 args_depth = TMPL_ARGS_DEPTH (args);
10347 parms_depth =
10348 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10349 if (args_depth > parms_depth
10350 && !DECL_TEMPLATE_SPECIALIZATION (t))
10351 args = get_innermost_template_args (args, parms_depth);
10353 else
10355 /* This special case arises when we have something like this:
10357 template <class T> struct S {
10358 friend void f<int>(int, double);
10361 Here, the DECL_TI_TEMPLATE for the friend declaration
10362 will be an IDENTIFIER_NODE. We are being called from
10363 tsubst_friend_function, and we want only to create a
10364 new decl (R) with appropriate types so that we can call
10365 determine_specialization. */
10366 gen_tmpl = NULL_TREE;
10369 if (DECL_CLASS_SCOPE_P (t))
10371 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10372 member = 2;
10373 else
10374 member = 1;
10375 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10376 complain, t, /*entering_scope=*/1);
10378 else
10380 member = 0;
10381 ctx = DECL_CONTEXT (t);
10383 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10384 if (type == error_mark_node)
10385 RETURN (error_mark_node);
10387 /* If we hit excessive deduction depth, the type is bogus even if
10388 it isn't error_mark_node, so don't build a decl. */
10389 if (excessive_deduction_depth)
10390 RETURN (error_mark_node);
10392 /* We do NOT check for matching decls pushed separately at this
10393 point, as they may not represent instantiations of this
10394 template, and in any case are considered separate under the
10395 discrete model. */
10396 r = copy_decl (t);
10397 DECL_USE_TEMPLATE (r) = 0;
10398 TREE_TYPE (r) = type;
10399 /* Clear out the mangled name and RTL for the instantiation. */
10400 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10401 SET_DECL_RTL (r, NULL);
10402 /* Leave DECL_INITIAL set on deleted instantiations. */
10403 if (!DECL_DELETED_FN (r))
10404 DECL_INITIAL (r) = NULL_TREE;
10405 DECL_CONTEXT (r) = ctx;
10407 /* OpenMP UDRs have the only argument a reference to the declared
10408 type. We want to diagnose if the declared type is a reference,
10409 which is invalid, but as references to references are usually
10410 quietly merged, diagnose it here. */
10411 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10413 tree argtype
10414 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10415 argtype = tsubst (argtype, args, complain, in_decl);
10416 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10417 error_at (DECL_SOURCE_LOCATION (t),
10418 "reference type %qT in "
10419 "%<#pragma omp declare reduction%>", argtype);
10420 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10421 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10422 argtype);
10425 if (member && DECL_CONV_FN_P (r))
10426 /* Type-conversion operator. Reconstruct the name, in
10427 case it's the name of one of the template's parameters. */
10428 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10430 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10431 complain, t);
10432 DECL_RESULT (r) = NULL_TREE;
10434 TREE_STATIC (r) = 0;
10435 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10436 DECL_EXTERNAL (r) = 1;
10437 /* If this is an instantiation of a function with internal
10438 linkage, we already know what object file linkage will be
10439 assigned to the instantiation. */
10440 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10441 DECL_DEFER_OUTPUT (r) = 0;
10442 DECL_CHAIN (r) = NULL_TREE;
10443 DECL_PENDING_INLINE_INFO (r) = 0;
10444 DECL_PENDING_INLINE_P (r) = 0;
10445 DECL_SAVED_TREE (r) = NULL_TREE;
10446 DECL_STRUCT_FUNCTION (r) = NULL;
10447 TREE_USED (r) = 0;
10448 /* We'll re-clone as appropriate in instantiate_template. */
10449 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10451 /* If we aren't complaining now, return on error before we register
10452 the specialization so that we'll complain eventually. */
10453 if ((complain & tf_error) == 0
10454 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10455 && !grok_op_properties (r, /*complain=*/false))
10456 RETURN (error_mark_node);
10458 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10459 this in the special friend case mentioned above where
10460 GEN_TMPL is NULL. */
10461 if (gen_tmpl)
10463 DECL_TEMPLATE_INFO (r)
10464 = build_template_info (gen_tmpl, argvec);
10465 SET_DECL_IMPLICIT_INSTANTIATION (r);
10467 tree new_r
10468 = register_specialization (r, gen_tmpl, argvec, false, hash);
10469 if (new_r != r)
10470 /* We instantiated this while substituting into
10471 the type earlier (template/friend54.C). */
10472 RETURN (new_r);
10474 /* We're not supposed to instantiate default arguments
10475 until they are called, for a template. But, for a
10476 declaration like:
10478 template <class T> void f ()
10479 { extern void g(int i = T()); }
10481 we should do the substitution when the template is
10482 instantiated. We handle the member function case in
10483 instantiate_class_template since the default arguments
10484 might refer to other members of the class. */
10485 if (!member
10486 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10487 && !uses_template_parms (argvec))
10488 tsubst_default_arguments (r, complain);
10490 else
10491 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10493 /* Copy the list of befriending classes. */
10494 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10495 *friends;
10496 friends = &TREE_CHAIN (*friends))
10498 *friends = copy_node (*friends);
10499 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10500 args, complain,
10501 in_decl);
10504 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10506 maybe_retrofit_in_chrg (r);
10507 if (DECL_CONSTRUCTOR_P (r))
10508 grok_ctor_properties (ctx, r);
10509 if (DECL_INHERITED_CTOR_BASE (r))
10510 deduce_inheriting_ctor (r);
10511 /* If this is an instantiation of a member template, clone it.
10512 If it isn't, that'll be handled by
10513 clone_constructors_and_destructors. */
10514 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10515 clone_function_decl (r, /*update_method_vec_p=*/0);
10517 else if ((complain & tf_error) != 0
10518 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10519 && !grok_op_properties (r, /*complain=*/true))
10520 RETURN (error_mark_node);
10522 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10523 SET_DECL_FRIEND_CONTEXT (r,
10524 tsubst (DECL_FRIEND_CONTEXT (t),
10525 args, complain, in_decl));
10527 /* Possibly limit visibility based on template args. */
10528 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10529 if (DECL_VISIBILITY_SPECIFIED (t))
10531 DECL_VISIBILITY_SPECIFIED (r) = 0;
10532 DECL_ATTRIBUTES (r)
10533 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10535 determine_visibility (r);
10536 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10537 && !processing_template_decl)
10538 defaulted_late_check (r);
10540 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10541 args, complain, in_decl);
10543 break;
10545 case PARM_DECL:
10547 tree type = NULL_TREE;
10548 int i, len = 1;
10549 tree expanded_types = NULL_TREE;
10550 tree prev_r = NULL_TREE;
10551 tree first_r = NULL_TREE;
10553 if (DECL_PACK_P (t))
10555 /* If there is a local specialization that isn't a
10556 parameter pack, it means that we're doing a "simple"
10557 substitution from inside tsubst_pack_expansion. Just
10558 return the local specialization (which will be a single
10559 parm). */
10560 tree spec = retrieve_local_specialization (t);
10561 if (spec
10562 && TREE_CODE (spec) == PARM_DECL
10563 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10564 RETURN (spec);
10566 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10567 the parameters in this function parameter pack. */
10568 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10569 complain, in_decl);
10570 if (TREE_CODE (expanded_types) == TREE_VEC)
10572 len = TREE_VEC_LENGTH (expanded_types);
10574 /* Zero-length parameter packs are boring. Just substitute
10575 into the chain. */
10576 if (len == 0)
10577 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10578 TREE_CHAIN (t)));
10580 else
10582 /* All we did was update the type. Make a note of that. */
10583 type = expanded_types;
10584 expanded_types = NULL_TREE;
10588 /* Loop through all of the parameters we'll build. When T is
10589 a function parameter pack, LEN is the number of expanded
10590 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10591 r = NULL_TREE;
10592 for (i = 0; i < len; ++i)
10594 prev_r = r;
10595 r = copy_node (t);
10596 if (DECL_TEMPLATE_PARM_P (t))
10597 SET_DECL_TEMPLATE_PARM_P (r);
10599 if (expanded_types)
10600 /* We're on the Ith parameter of the function parameter
10601 pack. */
10603 /* Get the Ith type. */
10604 type = TREE_VEC_ELT (expanded_types, i);
10606 /* Rename the parameter to include the index. */
10607 DECL_NAME (r)
10608 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10610 else if (!type)
10611 /* We're dealing with a normal parameter. */
10612 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10614 type = type_decays_to (type);
10615 TREE_TYPE (r) = type;
10616 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10618 if (DECL_INITIAL (r))
10620 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10621 DECL_INITIAL (r) = TREE_TYPE (r);
10622 else
10623 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10624 complain, in_decl);
10627 DECL_CONTEXT (r) = NULL_TREE;
10629 if (!DECL_TEMPLATE_PARM_P (r))
10630 DECL_ARG_TYPE (r) = type_passed_as (type);
10632 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10633 args, complain, in_decl);
10635 /* Keep track of the first new parameter we
10636 generate. That's what will be returned to the
10637 caller. */
10638 if (!first_r)
10639 first_r = r;
10641 /* Build a proper chain of parameters when substituting
10642 into a function parameter pack. */
10643 if (prev_r)
10644 DECL_CHAIN (prev_r) = r;
10647 /* If cp_unevaluated_operand is set, we're just looking for a
10648 single dummy parameter, so don't keep going. */
10649 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10650 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10651 complain, DECL_CHAIN (t));
10653 /* FIRST_R contains the start of the chain we've built. */
10654 r = first_r;
10656 break;
10658 case FIELD_DECL:
10660 tree type = NULL_TREE;
10661 tree vec = NULL_TREE;
10662 tree expanded_types = NULL_TREE;
10663 int len = 1;
10665 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10667 /* This field is a lambda capture pack. Return a TREE_VEC of
10668 the expanded fields to instantiate_class_template_1 and
10669 store them in the specializations hash table as a
10670 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10671 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10672 complain, in_decl);
10673 if (TREE_CODE (expanded_types) == TREE_VEC)
10675 len = TREE_VEC_LENGTH (expanded_types);
10676 vec = make_tree_vec (len);
10678 else
10680 /* All we did was update the type. Make a note of that. */
10681 type = expanded_types;
10682 expanded_types = NULL_TREE;
10686 for (int i = 0; i < len; ++i)
10688 r = copy_decl (t);
10689 if (expanded_types)
10691 type = TREE_VEC_ELT (expanded_types, i);
10692 DECL_NAME (r)
10693 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10695 else if (!type)
10696 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10698 if (type == error_mark_node)
10699 RETURN (error_mark_node);
10700 TREE_TYPE (r) = type;
10701 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10703 if (DECL_C_BIT_FIELD (r))
10704 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10705 non-bit-fields DECL_INITIAL is a non-static data member
10706 initializer, which gets deferred instantiation. */
10707 DECL_INITIAL (r)
10708 = tsubst_expr (DECL_INITIAL (t), args,
10709 complain, in_decl,
10710 /*integral_constant_expression_p=*/true);
10711 else if (DECL_INITIAL (t))
10713 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10714 NSDMI in perform_member_init. Still set DECL_INITIAL
10715 so that we know there is one. */
10716 DECL_INITIAL (r) = void_zero_node;
10717 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10718 retrofit_lang_decl (r);
10719 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10721 /* We don't have to set DECL_CONTEXT here; it is set by
10722 finish_member_declaration. */
10723 DECL_CHAIN (r) = NULL_TREE;
10725 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10726 args, complain, in_decl);
10728 if (vec)
10729 TREE_VEC_ELT (vec, i) = r;
10732 if (vec)
10734 r = vec;
10735 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10736 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10737 SET_ARGUMENT_PACK_ARGS (pack, vec);
10738 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10739 TREE_TYPE (pack) = tpack;
10740 register_specialization (pack, t, args, false, 0);
10743 break;
10745 case USING_DECL:
10746 /* We reach here only for member using decls. We also need to check
10747 uses_template_parms because DECL_DEPENDENT_P is not set for a
10748 using-declaration that designates a member of the current
10749 instantiation (c++/53549). */
10750 if (DECL_DEPENDENT_P (t)
10751 || uses_template_parms (USING_DECL_SCOPE (t)))
10753 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10754 complain, in_decl);
10755 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10756 r = do_class_using_decl (inst_scope, name);
10757 if (!r)
10758 r = error_mark_node;
10759 else
10761 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10762 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10765 else
10767 r = copy_node (t);
10768 DECL_CHAIN (r) = NULL_TREE;
10770 break;
10772 case TYPE_DECL:
10773 case VAR_DECL:
10775 tree argvec = NULL_TREE;
10776 tree gen_tmpl = NULL_TREE;
10777 tree spec;
10778 tree tmpl = NULL_TREE;
10779 tree ctx;
10780 tree type = NULL_TREE;
10781 bool local_p;
10783 if (TREE_CODE (t) == TYPE_DECL
10784 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10786 /* If this is the canonical decl, we don't have to
10787 mess with instantiations, and often we can't (for
10788 typename, template type parms and such). Note that
10789 TYPE_NAME is not correct for the above test if
10790 we've copied the type for a typedef. */
10791 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10792 if (type == error_mark_node)
10793 RETURN (error_mark_node);
10794 r = TYPE_NAME (type);
10795 break;
10798 /* Check to see if we already have the specialization we
10799 need. */
10800 spec = NULL_TREE;
10801 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10803 /* T is a static data member or namespace-scope entity.
10804 We have to substitute into namespace-scope variables
10805 (even though such entities are never templates) because
10806 of cases like:
10808 template <class T> void f() { extern T t; }
10810 where the entity referenced is not known until
10811 instantiation time. */
10812 local_p = false;
10813 ctx = DECL_CONTEXT (t);
10814 if (DECL_CLASS_SCOPE_P (t))
10816 ctx = tsubst_aggr_type (ctx, args,
10817 complain,
10818 in_decl, /*entering_scope=*/1);
10819 /* If CTX is unchanged, then T is in fact the
10820 specialization we want. That situation occurs when
10821 referencing a static data member within in its own
10822 class. We can use pointer equality, rather than
10823 same_type_p, because DECL_CONTEXT is always
10824 canonical... */
10825 if (ctx == DECL_CONTEXT (t)
10826 && (TREE_CODE (t) != TYPE_DECL
10827 /* ... unless T is a member template; in which
10828 case our caller can be willing to create a
10829 specialization of that template represented
10830 by T. */
10831 || !(DECL_TI_TEMPLATE (t)
10832 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10833 spec = t;
10836 if (!spec)
10838 tmpl = DECL_TI_TEMPLATE (t);
10839 gen_tmpl = most_general_template (tmpl);
10840 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10841 if (argvec == error_mark_node)
10842 RETURN (error_mark_node);
10843 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10844 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10847 else
10849 /* A local variable. */
10850 local_p = true;
10851 /* Subsequent calls to pushdecl will fill this in. */
10852 ctx = NULL_TREE;
10853 spec = retrieve_local_specialization (t);
10855 /* If we already have the specialization we need, there is
10856 nothing more to do. */
10857 if (spec)
10859 r = spec;
10860 break;
10863 /* Create a new node for the specialization we need. */
10864 r = copy_decl (t);
10865 if (type == NULL_TREE)
10867 if (is_typedef_decl (t))
10868 type = DECL_ORIGINAL_TYPE (t);
10869 else
10870 type = TREE_TYPE (t);
10871 if (VAR_P (t)
10872 && VAR_HAD_UNKNOWN_BOUND (t)
10873 && type != error_mark_node)
10874 type = strip_array_domain (type);
10875 type = tsubst (type, args, complain, in_decl);
10877 if (VAR_P (r))
10879 /* Even if the original location is out of scope, the
10880 newly substituted one is not. */
10881 DECL_DEAD_FOR_LOCAL (r) = 0;
10882 DECL_INITIALIZED_P (r) = 0;
10883 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10884 if (type == error_mark_node)
10885 RETURN (error_mark_node);
10886 if (TREE_CODE (type) == FUNCTION_TYPE)
10888 /* It may seem that this case cannot occur, since:
10890 typedef void f();
10891 void g() { f x; }
10893 declares a function, not a variable. However:
10895 typedef void f();
10896 template <typename T> void g() { T t; }
10897 template void g<f>();
10899 is an attempt to declare a variable with function
10900 type. */
10901 error ("variable %qD has function type",
10902 /* R is not yet sufficiently initialized, so we
10903 just use its name. */
10904 DECL_NAME (r));
10905 RETURN (error_mark_node);
10907 type = complete_type (type);
10908 /* Wait until cp_finish_decl to set this again, to handle
10909 circular dependency (template/instantiate6.C). */
10910 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10911 type = check_var_type (DECL_NAME (r), type);
10913 if (DECL_HAS_VALUE_EXPR_P (t))
10915 tree ve = DECL_VALUE_EXPR (t);
10916 ve = tsubst_expr (ve, args, complain, in_decl,
10917 /*constant_expression_p=*/false);
10918 if (REFERENCE_REF_P (ve))
10920 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10921 ve = TREE_OPERAND (ve, 0);
10923 SET_DECL_VALUE_EXPR (r, ve);
10926 else if (DECL_SELF_REFERENCE_P (t))
10927 SET_DECL_SELF_REFERENCE_P (r);
10928 TREE_TYPE (r) = type;
10929 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10930 DECL_CONTEXT (r) = ctx;
10931 /* Clear out the mangled name and RTL for the instantiation. */
10932 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10933 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10934 SET_DECL_RTL (r, NULL);
10935 /* The initializer must not be expanded until it is required;
10936 see [temp.inst]. */
10937 DECL_INITIAL (r) = NULL_TREE;
10938 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10939 SET_DECL_RTL (r, NULL);
10940 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10941 if (VAR_P (r))
10943 /* Possibly limit visibility based on template args. */
10944 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10945 if (DECL_VISIBILITY_SPECIFIED (t))
10947 DECL_VISIBILITY_SPECIFIED (r) = 0;
10948 DECL_ATTRIBUTES (r)
10949 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10951 determine_visibility (r);
10954 if (!local_p)
10956 /* A static data member declaration is always marked
10957 external when it is declared in-class, even if an
10958 initializer is present. We mimic the non-template
10959 processing here. */
10960 DECL_EXTERNAL (r) = 1;
10962 register_specialization (r, gen_tmpl, argvec, false, hash);
10963 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10964 SET_DECL_IMPLICIT_INSTANTIATION (r);
10966 else if (cp_unevaluated_operand)
10967 gcc_unreachable ();
10968 else
10969 register_local_specialization (r, t);
10971 DECL_CHAIN (r) = NULL_TREE;
10973 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10974 /*flags=*/0,
10975 args, complain, in_decl);
10977 /* Preserve a typedef that names a type. */
10978 if (is_typedef_decl (r))
10980 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10981 set_underlying_type (r);
10984 layout_decl (r, 0);
10986 break;
10988 default:
10989 gcc_unreachable ();
10991 #undef RETURN
10993 out:
10994 /* Restore the file and line information. */
10995 input_location = saved_loc;
10997 return r;
11000 /* Substitute into the ARG_TYPES of a function type.
11001 If END is a TREE_CHAIN, leave it and any following types
11002 un-substituted. */
11004 static tree
11005 tsubst_arg_types (tree arg_types,
11006 tree args,
11007 tree end,
11008 tsubst_flags_t complain,
11009 tree in_decl)
11011 tree remaining_arg_types;
11012 tree type = NULL_TREE;
11013 int i = 1;
11014 tree expanded_args = NULL_TREE;
11015 tree default_arg;
11017 if (!arg_types || arg_types == void_list_node || arg_types == end)
11018 return arg_types;
11020 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11021 args, end, complain, in_decl);
11022 if (remaining_arg_types == error_mark_node)
11023 return error_mark_node;
11025 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11027 /* For a pack expansion, perform substitution on the
11028 entire expression. Later on, we'll handle the arguments
11029 one-by-one. */
11030 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11031 args, complain, in_decl);
11033 if (TREE_CODE (expanded_args) == TREE_VEC)
11034 /* So that we'll spin through the parameters, one by one. */
11035 i = TREE_VEC_LENGTH (expanded_args);
11036 else
11038 /* We only partially substituted into the parameter
11039 pack. Our type is TYPE_PACK_EXPANSION. */
11040 type = expanded_args;
11041 expanded_args = NULL_TREE;
11045 while (i > 0) {
11046 --i;
11048 if (expanded_args)
11049 type = TREE_VEC_ELT (expanded_args, i);
11050 else if (!type)
11051 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11053 if (type == error_mark_node)
11054 return error_mark_node;
11055 if (VOID_TYPE_P (type))
11057 if (complain & tf_error)
11059 error ("invalid parameter type %qT", type);
11060 if (in_decl)
11061 error ("in declaration %q+D", in_decl);
11063 return error_mark_node;
11065 /* DR 657. */
11066 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11067 return error_mark_node;
11069 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11070 top-level qualifiers as required. */
11071 type = cv_unqualified (type_decays_to (type));
11073 /* We do not substitute into default arguments here. The standard
11074 mandates that they be instantiated only when needed, which is
11075 done in build_over_call. */
11076 default_arg = TREE_PURPOSE (arg_types);
11078 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11080 /* We've instantiated a template before its default arguments
11081 have been parsed. This can happen for a nested template
11082 class, and is not an error unless we require the default
11083 argument in a call of this function. */
11084 remaining_arg_types =
11085 tree_cons (default_arg, type, remaining_arg_types);
11086 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11088 else
11089 remaining_arg_types =
11090 hash_tree_cons (default_arg, type, remaining_arg_types);
11093 return remaining_arg_types;
11096 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11097 *not* handle the exception-specification for FNTYPE, because the
11098 initial substitution of explicitly provided template parameters
11099 during argument deduction forbids substitution into the
11100 exception-specification:
11102 [temp.deduct]
11104 All references in the function type of the function template to the
11105 corresponding template parameters are replaced by the specified tem-
11106 plate argument values. If a substitution in a template parameter or
11107 in the function type of the function template results in an invalid
11108 type, type deduction fails. [Note: The equivalent substitution in
11109 exception specifications is done only when the function is instanti-
11110 ated, at which point a program is ill-formed if the substitution
11111 results in an invalid type.] */
11113 static tree
11114 tsubst_function_type (tree t,
11115 tree args,
11116 tsubst_flags_t complain,
11117 tree in_decl)
11119 tree return_type;
11120 tree arg_types;
11121 tree fntype;
11123 /* The TYPE_CONTEXT is not used for function/method types. */
11124 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11126 /* Substitute the return type. */
11127 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11128 if (return_type == error_mark_node)
11129 return error_mark_node;
11130 /* DR 486 clarifies that creation of a function type with an
11131 invalid return type is a deduction failure. */
11132 if (TREE_CODE (return_type) == ARRAY_TYPE
11133 || TREE_CODE (return_type) == FUNCTION_TYPE)
11135 if (complain & tf_error)
11137 if (TREE_CODE (return_type) == ARRAY_TYPE)
11138 error ("function returning an array");
11139 else
11140 error ("function returning a function");
11142 return error_mark_node;
11144 /* And DR 657. */
11145 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11146 return error_mark_node;
11148 /* Substitute the argument types. */
11149 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11150 complain, in_decl);
11151 if (arg_types == error_mark_node)
11152 return error_mark_node;
11154 /* Construct a new type node and return it. */
11155 if (TREE_CODE (t) == FUNCTION_TYPE)
11157 fntype = build_function_type (return_type, arg_types);
11158 fntype = apply_memfn_quals (fntype,
11159 type_memfn_quals (t),
11160 type_memfn_rqual (t));
11162 else
11164 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11165 if (! MAYBE_CLASS_TYPE_P (r))
11167 /* [temp.deduct]
11169 Type deduction may fail for any of the following
11170 reasons:
11172 -- Attempting to create "pointer to member of T" when T
11173 is not a class type. */
11174 if (complain & tf_error)
11175 error ("creating pointer to member function of non-class type %qT",
11177 return error_mark_node;
11180 fntype = build_method_type_directly (r, return_type,
11181 TREE_CHAIN (arg_types));
11182 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11184 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11186 return fntype;
11189 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11190 ARGS into that specification, and return the substituted
11191 specification. If there is no specification, return NULL_TREE. */
11193 static tree
11194 tsubst_exception_specification (tree fntype,
11195 tree args,
11196 tsubst_flags_t complain,
11197 tree in_decl,
11198 bool defer_ok)
11200 tree specs;
11201 tree new_specs;
11203 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11204 new_specs = NULL_TREE;
11205 if (specs && TREE_PURPOSE (specs))
11207 /* A noexcept-specifier. */
11208 tree expr = TREE_PURPOSE (specs);
11209 if (TREE_CODE (expr) == INTEGER_CST)
11210 new_specs = expr;
11211 else if (defer_ok)
11213 /* Defer instantiation of noexcept-specifiers to avoid
11214 excessive instantiations (c++/49107). */
11215 new_specs = make_node (DEFERRED_NOEXCEPT);
11216 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11218 /* We already partially instantiated this member template,
11219 so combine the new args with the old. */
11220 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11221 = DEFERRED_NOEXCEPT_PATTERN (expr);
11222 DEFERRED_NOEXCEPT_ARGS (new_specs)
11223 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11225 else
11227 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11228 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11231 else
11232 new_specs = tsubst_copy_and_build
11233 (expr, args, complain, in_decl, /*function_p=*/false,
11234 /*integral_constant_expression_p=*/true);
11235 new_specs = build_noexcept_spec (new_specs, complain);
11237 else if (specs)
11239 if (! TREE_VALUE (specs))
11240 new_specs = specs;
11241 else
11242 while (specs)
11244 tree spec;
11245 int i, len = 1;
11246 tree expanded_specs = NULL_TREE;
11248 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11250 /* Expand the pack expansion type. */
11251 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11252 args, complain,
11253 in_decl);
11255 if (expanded_specs == error_mark_node)
11256 return error_mark_node;
11257 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11258 len = TREE_VEC_LENGTH (expanded_specs);
11259 else
11261 /* We're substituting into a member template, so
11262 we got a TYPE_PACK_EXPANSION back. Add that
11263 expansion and move on. */
11264 gcc_assert (TREE_CODE (expanded_specs)
11265 == TYPE_PACK_EXPANSION);
11266 new_specs = add_exception_specifier (new_specs,
11267 expanded_specs,
11268 complain);
11269 specs = TREE_CHAIN (specs);
11270 continue;
11274 for (i = 0; i < len; ++i)
11276 if (expanded_specs)
11277 spec = TREE_VEC_ELT (expanded_specs, i);
11278 else
11279 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11280 if (spec == error_mark_node)
11281 return spec;
11282 new_specs = add_exception_specifier (new_specs, spec,
11283 complain);
11286 specs = TREE_CHAIN (specs);
11289 return new_specs;
11292 /* Take the tree structure T and replace template parameters used
11293 therein with the argument vector ARGS. IN_DECL is an associated
11294 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11295 Issue error and warning messages under control of COMPLAIN. Note
11296 that we must be relatively non-tolerant of extensions here, in
11297 order to preserve conformance; if we allow substitutions that
11298 should not be allowed, we may allow argument deductions that should
11299 not succeed, and therefore report ambiguous overload situations
11300 where there are none. In theory, we could allow the substitution,
11301 but indicate that it should have failed, and allow our caller to
11302 make sure that the right thing happens, but we don't try to do this
11303 yet.
11305 This function is used for dealing with types, decls and the like;
11306 for expressions, use tsubst_expr or tsubst_copy. */
11308 tree
11309 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11311 enum tree_code code;
11312 tree type, r = NULL_TREE;
11314 if (t == NULL_TREE || t == error_mark_node
11315 || t == integer_type_node
11316 || t == void_type_node
11317 || t == char_type_node
11318 || t == unknown_type_node
11319 || TREE_CODE (t) == NAMESPACE_DECL
11320 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11321 return t;
11323 if (DECL_P (t))
11324 return tsubst_decl (t, args, complain);
11326 if (args == NULL_TREE)
11327 return t;
11329 code = TREE_CODE (t);
11331 if (code == IDENTIFIER_NODE)
11332 type = IDENTIFIER_TYPE_VALUE (t);
11333 else
11334 type = TREE_TYPE (t);
11336 gcc_assert (type != unknown_type_node);
11338 /* Reuse typedefs. We need to do this to handle dependent attributes,
11339 such as attribute aligned. */
11340 if (TYPE_P (t)
11341 && typedef_variant_p (t))
11343 tree decl = TYPE_NAME (t);
11345 if (alias_template_specialization_p (t))
11347 /* DECL represents an alias template and we want to
11348 instantiate it. */
11349 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11350 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11351 r = instantiate_alias_template (tmpl, gen_args, complain);
11353 else if (DECL_CLASS_SCOPE_P (decl)
11354 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11355 && uses_template_parms (DECL_CONTEXT (decl)))
11357 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11358 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11359 r = retrieve_specialization (tmpl, gen_args, 0);
11361 else if (DECL_FUNCTION_SCOPE_P (decl)
11362 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11363 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11364 r = retrieve_local_specialization (decl);
11365 else
11366 /* The typedef is from a non-template context. */
11367 return t;
11369 if (r)
11371 r = TREE_TYPE (r);
11372 r = cp_build_qualified_type_real
11373 (r, cp_type_quals (t) | cp_type_quals (r),
11374 complain | tf_ignore_bad_quals);
11375 return r;
11377 else
11379 /* We don't have an instantiation yet, so drop the typedef. */
11380 int quals = cp_type_quals (t);
11381 t = DECL_ORIGINAL_TYPE (decl);
11382 t = cp_build_qualified_type_real (t, quals,
11383 complain | tf_ignore_bad_quals);
11387 if (type
11388 && code != TYPENAME_TYPE
11389 && code != TEMPLATE_TYPE_PARM
11390 && code != IDENTIFIER_NODE
11391 && code != FUNCTION_TYPE
11392 && code != METHOD_TYPE)
11393 type = tsubst (type, args, complain, in_decl);
11394 if (type == error_mark_node)
11395 return error_mark_node;
11397 switch (code)
11399 case RECORD_TYPE:
11400 case UNION_TYPE:
11401 case ENUMERAL_TYPE:
11402 return tsubst_aggr_type (t, args, complain, in_decl,
11403 /*entering_scope=*/0);
11405 case ERROR_MARK:
11406 case IDENTIFIER_NODE:
11407 case VOID_TYPE:
11408 case REAL_TYPE:
11409 case COMPLEX_TYPE:
11410 case VECTOR_TYPE:
11411 case BOOLEAN_TYPE:
11412 case NULLPTR_TYPE:
11413 case LANG_TYPE:
11414 return t;
11416 case INTEGER_TYPE:
11417 if (t == integer_type_node)
11418 return t;
11420 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11421 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11422 return t;
11425 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11427 max = tsubst_expr (omax, args, complain, in_decl,
11428 /*integral_constant_expression_p=*/false);
11430 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11431 needed. */
11432 if (TREE_CODE (max) == NOP_EXPR
11433 && TREE_SIDE_EFFECTS (omax)
11434 && !TREE_TYPE (max))
11435 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11437 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11438 with TREE_SIDE_EFFECTS that indicates this is not an integral
11439 constant expression. */
11440 if (processing_template_decl
11441 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11443 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11444 TREE_SIDE_EFFECTS (max) = 1;
11447 return compute_array_index_type (NULL_TREE, max, complain);
11450 case TEMPLATE_TYPE_PARM:
11451 case TEMPLATE_TEMPLATE_PARM:
11452 case BOUND_TEMPLATE_TEMPLATE_PARM:
11453 case TEMPLATE_PARM_INDEX:
11455 int idx;
11456 int level;
11457 int levels;
11458 tree arg = NULL_TREE;
11460 r = NULL_TREE;
11462 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11463 template_parm_level_and_index (t, &level, &idx);
11465 levels = TMPL_ARGS_DEPTH (args);
11466 if (level <= levels)
11468 arg = TMPL_ARG (args, level, idx);
11470 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11472 /* See through ARGUMENT_PACK_SELECT arguments. */
11473 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11474 /* If the selected argument is an expansion E, that most
11475 likely means we were called from
11476 gen_elem_of_pack_expansion_instantiation during the
11477 substituting of pack an argument pack (which Ith
11478 element is a pack expansion, where I is
11479 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11480 In this case, the Ith element resulting from this
11481 substituting is going to be a pack expansion, which
11482 pattern is the pattern of E. Let's return the
11483 pattern of E, and
11484 gen_elem_of_pack_expansion_instantiation will
11485 build the resulting pack expansion from it. */
11486 if (PACK_EXPANSION_P (arg))
11487 arg = PACK_EXPANSION_PATTERN (arg);
11491 if (arg == error_mark_node)
11492 return error_mark_node;
11493 else if (arg != NULL_TREE)
11495 if (ARGUMENT_PACK_P (arg))
11496 /* If ARG is an argument pack, we don't actually want to
11497 perform a substitution here, because substitutions
11498 for argument packs are only done
11499 element-by-element. We can get to this point when
11500 substituting the type of a non-type template
11501 parameter pack, when that type actually contains
11502 template parameter packs from an outer template, e.g.,
11504 template<typename... Types> struct A {
11505 template<Types... Values> struct B { };
11506 }; */
11507 return t;
11509 if (code == TEMPLATE_TYPE_PARM)
11511 int quals;
11512 gcc_assert (TYPE_P (arg));
11514 quals = cp_type_quals (arg) | cp_type_quals (t);
11516 return cp_build_qualified_type_real
11517 (arg, quals, complain | tf_ignore_bad_quals);
11519 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11521 /* We are processing a type constructed from a
11522 template template parameter. */
11523 tree argvec = tsubst (TYPE_TI_ARGS (t),
11524 args, complain, in_decl);
11525 if (argvec == error_mark_node)
11526 return error_mark_node;
11528 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11529 || TREE_CODE (arg) == TEMPLATE_DECL
11530 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11532 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11533 /* Consider this code:
11535 template <template <class> class Template>
11536 struct Internal {
11537 template <class Arg> using Bind = Template<Arg>;
11540 template <template <class> class Template, class Arg>
11541 using Instantiate = Template<Arg>; //#0
11543 template <template <class> class Template,
11544 class Argument>
11545 using Bind =
11546 Instantiate<Internal<Template>::template Bind,
11547 Argument>; //#1
11549 When #1 is parsed, the
11550 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11551 parameter `Template' in #0 matches the
11552 UNBOUND_CLASS_TEMPLATE representing the argument
11553 `Internal<Template>::template Bind'; We then want
11554 to assemble the type `Bind<Argument>' that can't
11555 be fully created right now, because
11556 `Internal<Template>' not being complete, the Bind
11557 template cannot be looked up in that context. So
11558 we need to "store" `Bind<Argument>' for later
11559 when the context of Bind becomes complete. Let's
11560 store that in a TYPENAME_TYPE. */
11561 return make_typename_type (TYPE_CONTEXT (arg),
11562 build_nt (TEMPLATE_ID_EXPR,
11563 TYPE_IDENTIFIER (arg),
11564 argvec),
11565 typename_type,
11566 complain);
11568 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11569 are resolving nested-types in the signature of a
11570 member function templates. Otherwise ARG is a
11571 TEMPLATE_DECL and is the real template to be
11572 instantiated. */
11573 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11574 arg = TYPE_NAME (arg);
11576 r = lookup_template_class (arg,
11577 argvec, in_decl,
11578 DECL_CONTEXT (arg),
11579 /*entering_scope=*/0,
11580 complain);
11581 return cp_build_qualified_type_real
11582 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11584 else
11585 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11586 return convert_from_reference (unshare_expr (arg));
11589 if (level == 1)
11590 /* This can happen during the attempted tsubst'ing in
11591 unify. This means that we don't yet have any information
11592 about the template parameter in question. */
11593 return t;
11595 /* Early in template argument deduction substitution, we don't
11596 want to reduce the level of 'auto', or it will be confused
11597 with a normal template parm in subsequent deduction. */
11598 if (is_auto (t) && (complain & tf_partial))
11599 return t;
11601 /* If we get here, we must have been looking at a parm for a
11602 more deeply nested template. Make a new version of this
11603 template parameter, but with a lower level. */
11604 switch (code)
11606 case TEMPLATE_TYPE_PARM:
11607 case TEMPLATE_TEMPLATE_PARM:
11608 case BOUND_TEMPLATE_TEMPLATE_PARM:
11609 if (cp_type_quals (t))
11611 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11612 r = cp_build_qualified_type_real
11613 (r, cp_type_quals (t),
11614 complain | (code == TEMPLATE_TYPE_PARM
11615 ? tf_ignore_bad_quals : 0));
11617 else
11619 r = copy_type (t);
11620 TEMPLATE_TYPE_PARM_INDEX (r)
11621 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11622 r, levels, args, complain);
11623 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11624 TYPE_MAIN_VARIANT (r) = r;
11625 TYPE_POINTER_TO (r) = NULL_TREE;
11626 TYPE_REFERENCE_TO (r) = NULL_TREE;
11628 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11629 /* We have reduced the level of the template
11630 template parameter, but not the levels of its
11631 template parameters, so canonical_type_parameter
11632 will not be able to find the canonical template
11633 template parameter for this level. Thus, we
11634 require structural equality checking to compare
11635 TEMPLATE_TEMPLATE_PARMs. */
11636 SET_TYPE_STRUCTURAL_EQUALITY (r);
11637 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11638 SET_TYPE_STRUCTURAL_EQUALITY (r);
11639 else
11640 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11642 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11644 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11645 complain, in_decl);
11646 if (argvec == error_mark_node)
11647 return error_mark_node;
11649 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11650 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11653 break;
11655 case TEMPLATE_PARM_INDEX:
11656 r = reduce_template_parm_level (t, type, levels, args, complain);
11657 break;
11659 default:
11660 gcc_unreachable ();
11663 return r;
11666 case TREE_LIST:
11668 tree purpose, value, chain;
11670 if (t == void_list_node)
11671 return t;
11673 purpose = TREE_PURPOSE (t);
11674 if (purpose)
11676 purpose = tsubst (purpose, args, complain, in_decl);
11677 if (purpose == error_mark_node)
11678 return error_mark_node;
11680 value = TREE_VALUE (t);
11681 if (value)
11683 value = tsubst (value, args, complain, in_decl);
11684 if (value == error_mark_node)
11685 return error_mark_node;
11687 chain = TREE_CHAIN (t);
11688 if (chain && chain != void_type_node)
11690 chain = tsubst (chain, args, complain, in_decl);
11691 if (chain == error_mark_node)
11692 return error_mark_node;
11694 if (purpose == TREE_PURPOSE (t)
11695 && value == TREE_VALUE (t)
11696 && chain == TREE_CHAIN (t))
11697 return t;
11698 return hash_tree_cons (purpose, value, chain);
11701 case TREE_BINFO:
11702 /* We should never be tsubsting a binfo. */
11703 gcc_unreachable ();
11705 case TREE_VEC:
11706 /* A vector of template arguments. */
11707 gcc_assert (!type);
11708 return tsubst_template_args (t, args, complain, in_decl);
11710 case POINTER_TYPE:
11711 case REFERENCE_TYPE:
11713 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11714 return t;
11716 /* [temp.deduct]
11718 Type deduction may fail for any of the following
11719 reasons:
11721 -- Attempting to create a pointer to reference type.
11722 -- Attempting to create a reference to a reference type or
11723 a reference to void.
11725 Core issue 106 says that creating a reference to a reference
11726 during instantiation is no longer a cause for failure. We
11727 only enforce this check in strict C++98 mode. */
11728 if ((TREE_CODE (type) == REFERENCE_TYPE
11729 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11730 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11732 static location_t last_loc;
11734 /* We keep track of the last time we issued this error
11735 message to avoid spewing a ton of messages during a
11736 single bad template instantiation. */
11737 if (complain & tf_error
11738 && last_loc != input_location)
11740 if (VOID_TYPE_P (type))
11741 error ("forming reference to void");
11742 else if (code == POINTER_TYPE)
11743 error ("forming pointer to reference type %qT", type);
11744 else
11745 error ("forming reference to reference type %qT", type);
11746 last_loc = input_location;
11749 return error_mark_node;
11751 else if (TREE_CODE (type) == FUNCTION_TYPE
11752 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11753 || type_memfn_rqual (type) != REF_QUAL_NONE))
11755 if (complain & tf_error)
11757 if (code == POINTER_TYPE)
11758 error ("forming pointer to qualified function type %qT",
11759 type);
11760 else
11761 error ("forming reference to qualified function type %qT",
11762 type);
11764 return error_mark_node;
11766 else if (code == POINTER_TYPE)
11768 r = build_pointer_type (type);
11769 if (TREE_CODE (type) == METHOD_TYPE)
11770 r = build_ptrmemfunc_type (r);
11772 else if (TREE_CODE (type) == REFERENCE_TYPE)
11773 /* In C++0x, during template argument substitution, when there is an
11774 attempt to create a reference to a reference type, reference
11775 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11777 "If a template-argument for a template-parameter T names a type
11778 that is a reference to a type A, an attempt to create the type
11779 'lvalue reference to cv T' creates the type 'lvalue reference to
11780 A,' while an attempt to create the type type rvalue reference to
11781 cv T' creates the type T"
11783 r = cp_build_reference_type
11784 (TREE_TYPE (type),
11785 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11786 else
11787 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11788 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11790 if (cxx_dialect >= cxx1y
11791 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11792 && array_of_runtime_bound_p (type))
11794 if (complain & tf_warning_or_error)
11795 pedwarn
11796 (input_location, OPT_Wvla,
11797 code == REFERENCE_TYPE
11798 ? G_("cannot declare reference to array of runtime bound")
11799 : G_("cannot declare pointer to array of runtime bound"));
11800 else
11801 r = error_mark_node;
11804 if (r != error_mark_node)
11805 /* Will this ever be needed for TYPE_..._TO values? */
11806 layout_type (r);
11808 return r;
11810 case OFFSET_TYPE:
11812 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11813 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11815 /* [temp.deduct]
11817 Type deduction may fail for any of the following
11818 reasons:
11820 -- Attempting to create "pointer to member of T" when T
11821 is not a class type. */
11822 if (complain & tf_error)
11823 error ("creating pointer to member of non-class type %qT", r);
11824 return error_mark_node;
11826 if (TREE_CODE (type) == REFERENCE_TYPE)
11828 if (complain & tf_error)
11829 error ("creating pointer to member reference type %qT", type);
11830 return error_mark_node;
11832 if (VOID_TYPE_P (type))
11834 if (complain & tf_error)
11835 error ("creating pointer to member of type void");
11836 return error_mark_node;
11838 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11839 if (TREE_CODE (type) == FUNCTION_TYPE)
11841 /* The type of the implicit object parameter gets its
11842 cv-qualifiers from the FUNCTION_TYPE. */
11843 tree memptr;
11844 tree method_type
11845 = build_memfn_type (type, r, type_memfn_quals (type),
11846 type_memfn_rqual (type));
11847 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11848 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11849 complain);
11851 else
11852 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11853 cp_type_quals (t),
11854 complain);
11856 case FUNCTION_TYPE:
11857 case METHOD_TYPE:
11859 tree fntype;
11860 tree specs;
11861 fntype = tsubst_function_type (t, args, complain, in_decl);
11862 if (fntype == error_mark_node)
11863 return error_mark_node;
11865 /* Substitute the exception specification. */
11866 specs = tsubst_exception_specification (t, args, complain,
11867 in_decl, /*defer_ok*/true);
11868 if (specs == error_mark_node)
11869 return error_mark_node;
11870 if (specs)
11871 fntype = build_exception_variant (fntype, specs);
11872 return fntype;
11874 case ARRAY_TYPE:
11876 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11877 if (domain == error_mark_node)
11878 return error_mark_node;
11880 /* As an optimization, we avoid regenerating the array type if
11881 it will obviously be the same as T. */
11882 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11883 return t;
11885 /* These checks should match the ones in grokdeclarator.
11887 [temp.deduct]
11889 The deduction may fail for any of the following reasons:
11891 -- Attempting to create an array with an element type that
11892 is void, a function type, or a reference type, or [DR337]
11893 an abstract class type. */
11894 if (VOID_TYPE_P (type)
11895 || TREE_CODE (type) == FUNCTION_TYPE
11896 || TREE_CODE (type) == REFERENCE_TYPE)
11898 if (complain & tf_error)
11899 error ("creating array of %qT", type);
11900 return error_mark_node;
11903 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11904 return error_mark_node;
11906 r = build_cplus_array_type (type, domain);
11908 if (TYPE_USER_ALIGN (t))
11910 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11911 TYPE_USER_ALIGN (r) = 1;
11914 return r;
11917 case TYPENAME_TYPE:
11919 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11920 in_decl, /*entering_scope=*/1);
11921 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11922 complain, in_decl);
11924 if (ctx == error_mark_node || f == error_mark_node)
11925 return error_mark_node;
11927 if (!MAYBE_CLASS_TYPE_P (ctx))
11929 if (complain & tf_error)
11930 error ("%qT is not a class, struct, or union type", ctx);
11931 return error_mark_node;
11933 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11935 /* Normally, make_typename_type does not require that the CTX
11936 have complete type in order to allow things like:
11938 template <class T> struct S { typename S<T>::X Y; };
11940 But, such constructs have already been resolved by this
11941 point, so here CTX really should have complete type, unless
11942 it's a partial instantiation. */
11943 ctx = complete_type (ctx);
11944 if (!COMPLETE_TYPE_P (ctx))
11946 if (complain & tf_error)
11947 cxx_incomplete_type_error (NULL_TREE, ctx);
11948 return error_mark_node;
11952 f = make_typename_type (ctx, f, typename_type,
11953 complain | tf_keep_type_decl);
11954 if (f == error_mark_node)
11955 return f;
11956 if (TREE_CODE (f) == TYPE_DECL)
11958 complain |= tf_ignore_bad_quals;
11959 f = TREE_TYPE (f);
11962 if (TREE_CODE (f) != TYPENAME_TYPE)
11964 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11966 if (complain & tf_error)
11967 error ("%qT resolves to %qT, which is not an enumeration type",
11968 t, f);
11969 else
11970 return error_mark_node;
11972 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11974 if (complain & tf_error)
11975 error ("%qT resolves to %qT, which is is not a class type",
11976 t, f);
11977 else
11978 return error_mark_node;
11982 return cp_build_qualified_type_real
11983 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11986 case UNBOUND_CLASS_TEMPLATE:
11988 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11989 in_decl, /*entering_scope=*/1);
11990 tree name = TYPE_IDENTIFIER (t);
11991 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11993 if (ctx == error_mark_node || name == error_mark_node)
11994 return error_mark_node;
11996 if (parm_list)
11997 parm_list = tsubst_template_parms (parm_list, args, complain);
11998 return make_unbound_class_template (ctx, name, parm_list, complain);
12001 case TYPEOF_TYPE:
12003 tree type;
12005 ++cp_unevaluated_operand;
12006 ++c_inhibit_evaluation_warnings;
12008 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12009 complain, in_decl,
12010 /*integral_constant_expression_p=*/false);
12012 --cp_unevaluated_operand;
12013 --c_inhibit_evaluation_warnings;
12015 type = finish_typeof (type);
12016 return cp_build_qualified_type_real (type,
12017 cp_type_quals (t)
12018 | cp_type_quals (type),
12019 complain);
12022 case DECLTYPE_TYPE:
12024 tree type;
12026 ++cp_unevaluated_operand;
12027 ++c_inhibit_evaluation_warnings;
12029 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12030 complain|tf_decltype, in_decl,
12031 /*function_p*/false,
12032 /*integral_constant_expression*/false);
12034 --cp_unevaluated_operand;
12035 --c_inhibit_evaluation_warnings;
12037 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12038 type = lambda_capture_field_type (type,
12039 DECLTYPE_FOR_INIT_CAPTURE (t));
12040 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12041 type = lambda_proxy_type (type);
12042 else
12044 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12045 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12046 && EXPR_P (type))
12047 /* In a template ~id could be either a complement expression
12048 or an unqualified-id naming a destructor; if instantiating
12049 it produces an expression, it's not an id-expression or
12050 member access. */
12051 id = false;
12052 type = finish_decltype_type (type, id, complain);
12054 return cp_build_qualified_type_real (type,
12055 cp_type_quals (t)
12056 | cp_type_quals (type),
12057 complain);
12060 case UNDERLYING_TYPE:
12062 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12063 complain, in_decl);
12064 return finish_underlying_type (type);
12067 case TYPE_ARGUMENT_PACK:
12068 case NONTYPE_ARGUMENT_PACK:
12070 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12071 tree packed_out =
12072 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12073 args,
12074 complain,
12075 in_decl);
12076 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12078 /* For template nontype argument packs, also substitute into
12079 the type. */
12080 if (code == NONTYPE_ARGUMENT_PACK)
12081 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12083 return r;
12085 break;
12087 case INTEGER_CST:
12088 case REAL_CST:
12089 case STRING_CST:
12090 case PLUS_EXPR:
12091 case MINUS_EXPR:
12092 case NEGATE_EXPR:
12093 case NOP_EXPR:
12094 case INDIRECT_REF:
12095 case ADDR_EXPR:
12096 case CALL_EXPR:
12097 case ARRAY_REF:
12098 case SCOPE_REF:
12099 /* We should use one of the expression tsubsts for these codes. */
12100 gcc_unreachable ();
12102 default:
12103 sorry ("use of %qs in template", get_tree_code_name (code));
12104 return error_mark_node;
12108 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12109 type of the expression on the left-hand side of the "." or "->"
12110 operator. */
12112 static tree
12113 tsubst_baselink (tree baselink, tree object_type,
12114 tree args, tsubst_flags_t complain, tree in_decl)
12116 tree name;
12117 tree qualifying_scope;
12118 tree fns;
12119 tree optype;
12120 tree template_args = 0;
12121 bool template_id_p = false;
12122 bool qualified = BASELINK_QUALIFIED_P (baselink);
12124 /* A baselink indicates a function from a base class. Both the
12125 BASELINK_ACCESS_BINFO and the base class referenced may
12126 indicate bases of the template class, rather than the
12127 instantiated class. In addition, lookups that were not
12128 ambiguous before may be ambiguous now. Therefore, we perform
12129 the lookup again. */
12130 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12131 qualifying_scope = tsubst (qualifying_scope, args,
12132 complain, in_decl);
12133 fns = BASELINK_FUNCTIONS (baselink);
12134 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12135 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12137 template_id_p = true;
12138 template_args = TREE_OPERAND (fns, 1);
12139 fns = TREE_OPERAND (fns, 0);
12140 if (template_args)
12141 template_args = tsubst_template_args (template_args, args,
12142 complain, in_decl);
12144 name = DECL_NAME (get_first_fn (fns));
12145 if (IDENTIFIER_TYPENAME_P (name))
12146 name = mangle_conv_op_name_for_type (optype);
12147 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12148 if (!baselink)
12149 return error_mark_node;
12151 /* If lookup found a single function, mark it as used at this
12152 point. (If it lookup found multiple functions the one selected
12153 later by overload resolution will be marked as used at that
12154 point.) */
12155 if (BASELINK_P (baselink))
12156 fns = BASELINK_FUNCTIONS (baselink);
12157 if (!template_id_p && !really_overloaded_fn (fns))
12158 mark_used (OVL_CURRENT (fns));
12160 /* Add back the template arguments, if present. */
12161 if (BASELINK_P (baselink) && template_id_p)
12162 BASELINK_FUNCTIONS (baselink)
12163 = build_nt (TEMPLATE_ID_EXPR,
12164 BASELINK_FUNCTIONS (baselink),
12165 template_args);
12166 /* Update the conversion operator type. */
12167 BASELINK_OPTYPE (baselink) = optype;
12169 if (!object_type)
12170 object_type = current_class_type;
12172 if (qualified)
12173 baselink = adjust_result_of_qualified_name_lookup (baselink,
12174 qualifying_scope,
12175 object_type);
12176 return baselink;
12179 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12180 true if the qualified-id will be a postfix-expression in-and-of
12181 itself; false if more of the postfix-expression follows the
12182 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12183 of "&". */
12185 static tree
12186 tsubst_qualified_id (tree qualified_id, tree args,
12187 tsubst_flags_t complain, tree in_decl,
12188 bool done, bool address_p)
12190 tree expr;
12191 tree scope;
12192 tree name;
12193 bool is_template;
12194 tree template_args;
12195 location_t loc = UNKNOWN_LOCATION;
12197 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12199 /* Figure out what name to look up. */
12200 name = TREE_OPERAND (qualified_id, 1);
12201 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12203 is_template = true;
12204 loc = EXPR_LOCATION (name);
12205 template_args = TREE_OPERAND (name, 1);
12206 if (template_args)
12207 template_args = tsubst_template_args (template_args, args,
12208 complain, in_decl);
12209 name = TREE_OPERAND (name, 0);
12211 else
12213 is_template = false;
12214 template_args = NULL_TREE;
12217 /* Substitute into the qualifying scope. When there are no ARGS, we
12218 are just trying to simplify a non-dependent expression. In that
12219 case the qualifying scope may be dependent, and, in any case,
12220 substituting will not help. */
12221 scope = TREE_OPERAND (qualified_id, 0);
12222 if (args)
12224 scope = tsubst (scope, args, complain, in_decl);
12225 expr = tsubst_copy (name, args, complain, in_decl);
12227 else
12228 expr = name;
12230 if (dependent_scope_p (scope))
12232 if (is_template)
12233 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12234 return build_qualified_name (NULL_TREE, scope, expr,
12235 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12238 if (!BASELINK_P (name) && !DECL_P (expr))
12240 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12242 /* A BIT_NOT_EXPR is used to represent a destructor. */
12243 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12245 error ("qualifying type %qT does not match destructor name ~%qT",
12246 scope, TREE_OPERAND (expr, 0));
12247 expr = error_mark_node;
12249 else
12250 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12251 /*is_type_p=*/0, false);
12253 else
12254 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12255 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12256 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12258 if (complain & tf_error)
12260 error ("dependent-name %qE is parsed as a non-type, but "
12261 "instantiation yields a type", qualified_id);
12262 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12264 return error_mark_node;
12268 if (DECL_P (expr))
12270 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12271 scope);
12272 /* Remember that there was a reference to this entity. */
12273 mark_used (expr);
12276 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12278 if (complain & tf_error)
12279 qualified_name_lookup_error (scope,
12280 TREE_OPERAND (qualified_id, 1),
12281 expr, input_location);
12282 return error_mark_node;
12285 if (is_template)
12286 expr = lookup_template_function (expr, template_args);
12288 if (expr == error_mark_node && complain & tf_error)
12289 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12290 expr, input_location);
12291 else if (TYPE_P (scope))
12293 expr = (adjust_result_of_qualified_name_lookup
12294 (expr, scope, current_nonlambda_class_type ()));
12295 expr = (finish_qualified_id_expr
12296 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12297 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12298 /*template_arg_p=*/false, complain));
12301 /* Expressions do not generally have reference type. */
12302 if (TREE_CODE (expr) != SCOPE_REF
12303 /* However, if we're about to form a pointer-to-member, we just
12304 want the referenced member referenced. */
12305 && TREE_CODE (expr) != OFFSET_REF)
12306 expr = convert_from_reference (expr);
12308 return expr;
12311 /* Like tsubst, but deals with expressions. This function just replaces
12312 template parms; to finish processing the resultant expression, use
12313 tsubst_copy_and_build or tsubst_expr. */
12315 static tree
12316 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12318 enum tree_code code;
12319 tree r;
12321 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12322 return t;
12324 code = TREE_CODE (t);
12326 switch (code)
12328 case PARM_DECL:
12329 r = retrieve_local_specialization (t);
12331 if (r == NULL_TREE)
12333 /* We get here for a use of 'this' in an NSDMI. */
12334 if (DECL_NAME (t) == this_identifier
12335 && at_function_scope_p ()
12336 && DECL_CONSTRUCTOR_P (current_function_decl))
12337 return current_class_ptr;
12339 /* This can happen for a parameter name used later in a function
12340 declaration (such as in a late-specified return type). Just
12341 make a dummy decl, since it's only used for its type. */
12342 gcc_assert (cp_unevaluated_operand != 0);
12343 r = tsubst_decl (t, args, complain);
12344 /* Give it the template pattern as its context; its true context
12345 hasn't been instantiated yet and this is good enough for
12346 mangling. */
12347 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12350 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12351 r = ARGUMENT_PACK_SELECT_ARG (r);
12352 mark_used (r);
12353 return r;
12355 case CONST_DECL:
12357 tree enum_type;
12358 tree v;
12360 if (DECL_TEMPLATE_PARM_P (t))
12361 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12362 /* There is no need to substitute into namespace-scope
12363 enumerators. */
12364 if (DECL_NAMESPACE_SCOPE_P (t))
12365 return t;
12366 /* If ARGS is NULL, then T is known to be non-dependent. */
12367 if (args == NULL_TREE)
12368 return integral_constant_value (t);
12370 /* Unfortunately, we cannot just call lookup_name here.
12371 Consider:
12373 template <int I> int f() {
12374 enum E { a = I };
12375 struct S { void g() { E e = a; } };
12378 When we instantiate f<7>::S::g(), say, lookup_name is not
12379 clever enough to find f<7>::a. */
12380 enum_type
12381 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12382 /*entering_scope=*/0);
12384 for (v = TYPE_VALUES (enum_type);
12385 v != NULL_TREE;
12386 v = TREE_CHAIN (v))
12387 if (TREE_PURPOSE (v) == DECL_NAME (t))
12388 return TREE_VALUE (v);
12390 /* We didn't find the name. That should never happen; if
12391 name-lookup found it during preliminary parsing, we
12392 should find it again here during instantiation. */
12393 gcc_unreachable ();
12395 return t;
12397 case FIELD_DECL:
12398 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12400 /* Check for a local specialization set up by
12401 tsubst_pack_expansion. */
12402 tree r = retrieve_local_specialization (t);
12403 if (r)
12405 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12406 r = ARGUMENT_PACK_SELECT_ARG (r);
12407 return r;
12410 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12411 tsubst_decl put in the hash table. */
12412 return retrieve_specialization (t, args, 0);
12415 if (DECL_CONTEXT (t))
12417 tree ctx;
12419 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12420 /*entering_scope=*/1);
12421 if (ctx != DECL_CONTEXT (t))
12423 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12424 if (!r)
12426 if (complain & tf_error)
12427 error ("using invalid field %qD", t);
12428 return error_mark_node;
12430 return r;
12434 return t;
12436 case VAR_DECL:
12437 case FUNCTION_DECL:
12438 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12439 r = tsubst (t, args, complain, in_decl);
12440 else if (local_variable_p (t))
12442 r = retrieve_local_specialization (t);
12443 if (r == NULL_TREE)
12445 if (DECL_ANON_UNION_VAR_P (t))
12447 /* Just use name lookup to find a member alias for an
12448 anonymous union, but then add it to the hash table. */
12449 r = lookup_name (DECL_NAME (t));
12450 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12451 register_local_specialization (r, t);
12453 else
12455 gcc_assert (errorcount || sorrycount);
12456 return error_mark_node;
12460 else
12461 r = t;
12462 mark_used (r);
12463 return r;
12465 case NAMESPACE_DECL:
12466 return t;
12468 case OVERLOAD:
12469 /* An OVERLOAD will always be a non-dependent overload set; an
12470 overload set from function scope will just be represented with an
12471 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12472 gcc_assert (!uses_template_parms (t));
12473 return t;
12475 case BASELINK:
12476 return tsubst_baselink (t, current_nonlambda_class_type (),
12477 args, complain, in_decl);
12479 case TEMPLATE_DECL:
12480 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12481 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12482 args, complain, in_decl);
12483 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12484 return tsubst (t, args, complain, in_decl);
12485 else if (DECL_CLASS_SCOPE_P (t)
12486 && uses_template_parms (DECL_CONTEXT (t)))
12488 /* Template template argument like the following example need
12489 special treatment:
12491 template <template <class> class TT> struct C {};
12492 template <class T> struct D {
12493 template <class U> struct E {};
12494 C<E> c; // #1
12496 D<int> d; // #2
12498 We are processing the template argument `E' in #1 for
12499 the template instantiation #2. Originally, `E' is a
12500 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12501 have to substitute this with one having context `D<int>'. */
12503 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12504 return lookup_field (context, DECL_NAME(t), 0, false);
12506 else
12507 /* Ordinary template template argument. */
12508 return t;
12510 case CAST_EXPR:
12511 case REINTERPRET_CAST_EXPR:
12512 case CONST_CAST_EXPR:
12513 case STATIC_CAST_EXPR:
12514 case DYNAMIC_CAST_EXPR:
12515 case IMPLICIT_CONV_EXPR:
12516 case CONVERT_EXPR:
12517 case NOP_EXPR:
12518 return build1
12519 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12520 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12522 case SIZEOF_EXPR:
12523 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12526 tree expanded, op = TREE_OPERAND (t, 0);
12527 int len = 0;
12529 if (SIZEOF_EXPR_TYPE_P (t))
12530 op = TREE_TYPE (op);
12532 ++cp_unevaluated_operand;
12533 ++c_inhibit_evaluation_warnings;
12534 /* We only want to compute the number of arguments. */
12535 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12536 --cp_unevaluated_operand;
12537 --c_inhibit_evaluation_warnings;
12539 if (TREE_CODE (expanded) == TREE_VEC)
12540 len = TREE_VEC_LENGTH (expanded);
12542 if (expanded == error_mark_node)
12543 return error_mark_node;
12544 else if (PACK_EXPANSION_P (expanded)
12545 || (TREE_CODE (expanded) == TREE_VEC
12546 && len > 0
12547 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12549 if (TREE_CODE (expanded) == TREE_VEC)
12550 expanded = TREE_VEC_ELT (expanded, len - 1);
12552 if (TYPE_P (expanded))
12553 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12554 complain & tf_error);
12555 else
12556 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12557 complain & tf_error);
12559 else
12560 return build_int_cst (size_type_node, len);
12562 if (SIZEOF_EXPR_TYPE_P (t))
12564 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12565 args, complain, in_decl);
12566 r = build1 (NOP_EXPR, r, error_mark_node);
12567 r = build1 (SIZEOF_EXPR,
12568 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12569 SIZEOF_EXPR_TYPE_P (r) = 1;
12570 return r;
12572 /* Fall through */
12574 case INDIRECT_REF:
12575 case NEGATE_EXPR:
12576 case TRUTH_NOT_EXPR:
12577 case BIT_NOT_EXPR:
12578 case ADDR_EXPR:
12579 case UNARY_PLUS_EXPR: /* Unary + */
12580 case ALIGNOF_EXPR:
12581 case AT_ENCODE_EXPR:
12582 case ARROW_EXPR:
12583 case THROW_EXPR:
12584 case TYPEID_EXPR:
12585 case REALPART_EXPR:
12586 case IMAGPART_EXPR:
12587 case PAREN_EXPR:
12588 return build1
12589 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12590 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12592 case COMPONENT_REF:
12594 tree object;
12595 tree name;
12597 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12598 name = TREE_OPERAND (t, 1);
12599 if (TREE_CODE (name) == BIT_NOT_EXPR)
12601 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12602 complain, in_decl);
12603 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12605 else if (TREE_CODE (name) == SCOPE_REF
12606 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12608 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12609 complain, in_decl);
12610 name = TREE_OPERAND (name, 1);
12611 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12612 complain, in_decl);
12613 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12614 name = build_qualified_name (/*type=*/NULL_TREE,
12615 base, name,
12616 /*template_p=*/false);
12618 else if (BASELINK_P (name))
12619 name = tsubst_baselink (name,
12620 non_reference (TREE_TYPE (object)),
12621 args, complain,
12622 in_decl);
12623 else
12624 name = tsubst_copy (name, args, complain, in_decl);
12625 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12628 case PLUS_EXPR:
12629 case MINUS_EXPR:
12630 case MULT_EXPR:
12631 case TRUNC_DIV_EXPR:
12632 case CEIL_DIV_EXPR:
12633 case FLOOR_DIV_EXPR:
12634 case ROUND_DIV_EXPR:
12635 case EXACT_DIV_EXPR:
12636 case BIT_AND_EXPR:
12637 case BIT_IOR_EXPR:
12638 case BIT_XOR_EXPR:
12639 case TRUNC_MOD_EXPR:
12640 case FLOOR_MOD_EXPR:
12641 case TRUTH_ANDIF_EXPR:
12642 case TRUTH_ORIF_EXPR:
12643 case TRUTH_AND_EXPR:
12644 case TRUTH_OR_EXPR:
12645 case RSHIFT_EXPR:
12646 case LSHIFT_EXPR:
12647 case RROTATE_EXPR:
12648 case LROTATE_EXPR:
12649 case EQ_EXPR:
12650 case NE_EXPR:
12651 case MAX_EXPR:
12652 case MIN_EXPR:
12653 case LE_EXPR:
12654 case GE_EXPR:
12655 case LT_EXPR:
12656 case GT_EXPR:
12657 case COMPOUND_EXPR:
12658 case DOTSTAR_EXPR:
12659 case MEMBER_REF:
12660 case PREDECREMENT_EXPR:
12661 case PREINCREMENT_EXPR:
12662 case POSTDECREMENT_EXPR:
12663 case POSTINCREMENT_EXPR:
12664 return build_nt
12665 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12666 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12668 case SCOPE_REF:
12669 return build_qualified_name (/*type=*/NULL_TREE,
12670 tsubst_copy (TREE_OPERAND (t, 0),
12671 args, complain, in_decl),
12672 tsubst_copy (TREE_OPERAND (t, 1),
12673 args, complain, in_decl),
12674 QUALIFIED_NAME_IS_TEMPLATE (t));
12676 case ARRAY_REF:
12677 return build_nt
12678 (ARRAY_REF,
12679 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12680 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12681 NULL_TREE, NULL_TREE);
12683 case CALL_EXPR:
12685 int n = VL_EXP_OPERAND_LENGTH (t);
12686 tree result = build_vl_exp (CALL_EXPR, n);
12687 int i;
12688 for (i = 0; i < n; i++)
12689 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12690 complain, in_decl);
12691 return result;
12694 case COND_EXPR:
12695 case MODOP_EXPR:
12696 case PSEUDO_DTOR_EXPR:
12697 case VEC_PERM_EXPR:
12699 r = build_nt
12700 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12701 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12702 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12703 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12704 return r;
12707 case NEW_EXPR:
12709 r = build_nt
12710 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12711 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12712 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12713 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12714 return r;
12717 case DELETE_EXPR:
12719 r = build_nt
12720 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12721 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12722 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12723 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12724 return r;
12727 case TEMPLATE_ID_EXPR:
12729 /* Substituted template arguments */
12730 tree fn = TREE_OPERAND (t, 0);
12731 tree targs = TREE_OPERAND (t, 1);
12733 fn = tsubst_copy (fn, args, complain, in_decl);
12734 if (targs)
12735 targs = tsubst_template_args (targs, args, complain, in_decl);
12737 return lookup_template_function (fn, targs);
12740 case TREE_LIST:
12742 tree purpose, value, chain;
12744 if (t == void_list_node)
12745 return t;
12747 purpose = TREE_PURPOSE (t);
12748 if (purpose)
12749 purpose = tsubst_copy (purpose, args, complain, in_decl);
12750 value = TREE_VALUE (t);
12751 if (value)
12752 value = tsubst_copy (value, args, complain, in_decl);
12753 chain = TREE_CHAIN (t);
12754 if (chain && chain != void_type_node)
12755 chain = tsubst_copy (chain, args, complain, in_decl);
12756 if (purpose == TREE_PURPOSE (t)
12757 && value == TREE_VALUE (t)
12758 && chain == TREE_CHAIN (t))
12759 return t;
12760 return tree_cons (purpose, value, chain);
12763 case RECORD_TYPE:
12764 case UNION_TYPE:
12765 case ENUMERAL_TYPE:
12766 case INTEGER_TYPE:
12767 case TEMPLATE_TYPE_PARM:
12768 case TEMPLATE_TEMPLATE_PARM:
12769 case BOUND_TEMPLATE_TEMPLATE_PARM:
12770 case TEMPLATE_PARM_INDEX:
12771 case POINTER_TYPE:
12772 case REFERENCE_TYPE:
12773 case OFFSET_TYPE:
12774 case FUNCTION_TYPE:
12775 case METHOD_TYPE:
12776 case ARRAY_TYPE:
12777 case TYPENAME_TYPE:
12778 case UNBOUND_CLASS_TEMPLATE:
12779 case TYPEOF_TYPE:
12780 case DECLTYPE_TYPE:
12781 case TYPE_DECL:
12782 return tsubst (t, args, complain, in_decl);
12784 case USING_DECL:
12785 t = DECL_NAME (t);
12786 /* Fall through. */
12787 case IDENTIFIER_NODE:
12788 if (IDENTIFIER_TYPENAME_P (t))
12790 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12791 return mangle_conv_op_name_for_type (new_type);
12793 else
12794 return t;
12796 case CONSTRUCTOR:
12797 /* This is handled by tsubst_copy_and_build. */
12798 gcc_unreachable ();
12800 case VA_ARG_EXPR:
12801 return build_x_va_arg (EXPR_LOCATION (t),
12802 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12803 in_decl),
12804 tsubst (TREE_TYPE (t), args, complain, in_decl));
12806 case CLEANUP_POINT_EXPR:
12807 /* We shouldn't have built any of these during initial template
12808 generation. Instead, they should be built during instantiation
12809 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12810 gcc_unreachable ();
12812 case OFFSET_REF:
12813 r = build2
12814 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12815 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12816 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12817 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12818 mark_used (TREE_OPERAND (r, 1));
12819 return r;
12821 case EXPR_PACK_EXPANSION:
12822 error ("invalid use of pack expansion expression");
12823 return error_mark_node;
12825 case NONTYPE_ARGUMENT_PACK:
12826 error ("use %<...%> to expand argument pack");
12827 return error_mark_node;
12829 case INTEGER_CST:
12830 case REAL_CST:
12831 case STRING_CST:
12832 case COMPLEX_CST:
12834 /* Instantiate any typedefs in the type. */
12835 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12836 r = fold_convert (type, t);
12837 gcc_assert (TREE_CODE (r) == code);
12838 return r;
12841 case PTRMEM_CST:
12842 /* These can sometimes show up in a partial instantiation, but never
12843 involve template parms. */
12844 gcc_assert (!uses_template_parms (t));
12845 return t;
12847 default:
12848 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12849 gcc_checking_assert (false);
12850 return t;
12854 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12856 static tree
12857 tsubst_omp_clauses (tree clauses, bool declare_simd,
12858 tree args, tsubst_flags_t complain, tree in_decl)
12860 tree new_clauses = NULL, nc, oc;
12862 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12864 nc = copy_node (oc);
12865 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12866 new_clauses = nc;
12868 switch (OMP_CLAUSE_CODE (nc))
12870 case OMP_CLAUSE_LASTPRIVATE:
12871 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12873 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12874 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12875 in_decl, /*integral_constant_expression_p=*/false);
12876 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12877 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12879 /* FALLTHRU */
12880 case OMP_CLAUSE_PRIVATE:
12881 case OMP_CLAUSE_SHARED:
12882 case OMP_CLAUSE_FIRSTPRIVATE:
12883 case OMP_CLAUSE_COPYIN:
12884 case OMP_CLAUSE_COPYPRIVATE:
12885 case OMP_CLAUSE_IF:
12886 case OMP_CLAUSE_NUM_THREADS:
12887 case OMP_CLAUSE_SCHEDULE:
12888 case OMP_CLAUSE_COLLAPSE:
12889 case OMP_CLAUSE_FINAL:
12890 case OMP_CLAUSE_DEPEND:
12891 case OMP_CLAUSE_FROM:
12892 case OMP_CLAUSE_TO:
12893 case OMP_CLAUSE_UNIFORM:
12894 case OMP_CLAUSE_MAP:
12895 case OMP_CLAUSE_DEVICE:
12896 case OMP_CLAUSE_DIST_SCHEDULE:
12897 case OMP_CLAUSE_NUM_TEAMS:
12898 case OMP_CLAUSE_THREAD_LIMIT:
12899 case OMP_CLAUSE_SAFELEN:
12900 case OMP_CLAUSE_SIMDLEN:
12901 OMP_CLAUSE_OPERAND (nc, 0)
12902 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12903 in_decl, /*integral_constant_expression_p=*/false);
12904 break;
12905 case OMP_CLAUSE_REDUCTION:
12906 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12908 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12909 if (TREE_CODE (placeholder) == SCOPE_REF)
12911 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12912 complain, in_decl);
12913 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12914 = build_qualified_name (NULL_TREE, scope,
12915 TREE_OPERAND (placeholder, 1),
12916 false);
12918 else
12919 gcc_assert (identifier_p (placeholder));
12921 OMP_CLAUSE_OPERAND (nc, 0)
12922 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12923 in_decl, /*integral_constant_expression_p=*/false);
12924 break;
12925 case OMP_CLAUSE_LINEAR:
12926 case OMP_CLAUSE_ALIGNED:
12927 OMP_CLAUSE_OPERAND (nc, 0)
12928 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12929 in_decl, /*integral_constant_expression_p=*/false);
12930 OMP_CLAUSE_OPERAND (nc, 1)
12931 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12932 in_decl, /*integral_constant_expression_p=*/false);
12933 break;
12935 case OMP_CLAUSE_NOWAIT:
12936 case OMP_CLAUSE_ORDERED:
12937 case OMP_CLAUSE_DEFAULT:
12938 case OMP_CLAUSE_UNTIED:
12939 case OMP_CLAUSE_MERGEABLE:
12940 case OMP_CLAUSE_INBRANCH:
12941 case OMP_CLAUSE_NOTINBRANCH:
12942 case OMP_CLAUSE_PROC_BIND:
12943 case OMP_CLAUSE_FOR:
12944 case OMP_CLAUSE_PARALLEL:
12945 case OMP_CLAUSE_SECTIONS:
12946 case OMP_CLAUSE_TASKGROUP:
12947 break;
12948 default:
12949 gcc_unreachable ();
12953 new_clauses = nreverse (new_clauses);
12954 if (!declare_simd)
12955 new_clauses = finish_omp_clauses (new_clauses);
12956 return new_clauses;
12959 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12961 static tree
12962 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12963 tree in_decl)
12965 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12967 tree purpose, value, chain;
12969 if (t == NULL)
12970 return t;
12972 if (TREE_CODE (t) != TREE_LIST)
12973 return tsubst_copy_and_build (t, args, complain, in_decl,
12974 /*function_p=*/false,
12975 /*integral_constant_expression_p=*/false);
12977 if (t == void_list_node)
12978 return t;
12980 purpose = TREE_PURPOSE (t);
12981 if (purpose)
12982 purpose = RECUR (purpose);
12983 value = TREE_VALUE (t);
12984 if (value)
12986 if (TREE_CODE (value) != LABEL_DECL)
12987 value = RECUR (value);
12988 else
12990 value = lookup_label (DECL_NAME (value));
12991 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12992 TREE_USED (value) = 1;
12995 chain = TREE_CHAIN (t);
12996 if (chain && chain != void_type_node)
12997 chain = RECUR (chain);
12998 return tree_cons (purpose, value, chain);
12999 #undef RECUR
13002 /* Substitute one OMP_FOR iterator. */
13004 static void
13005 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13006 tree condv, tree incrv, tree *clauses,
13007 tree args, tsubst_flags_t complain, tree in_decl,
13008 bool integral_constant_expression_p)
13010 #define RECUR(NODE) \
13011 tsubst_expr ((NODE), args, complain, in_decl, \
13012 integral_constant_expression_p)
13013 tree decl, init, cond, incr;
13014 bool init_decl;
13016 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13017 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13018 decl = TREE_OPERAND (init, 0);
13019 init = TREE_OPERAND (init, 1);
13020 /* Do this before substituting into decl to handle 'auto'. */
13021 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13022 init = RECUR (init);
13023 decl = RECUR (decl);
13024 if (init_decl)
13026 gcc_assert (!processing_template_decl);
13027 init = DECL_INITIAL (decl);
13028 DECL_INITIAL (decl) = NULL_TREE;
13031 gcc_assert (!type_dependent_expression_p (decl));
13033 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13035 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13036 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13037 if (TREE_CODE (incr) == MODIFY_EXPR)
13038 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13039 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13040 RECUR (TREE_OPERAND (incr, 1)),
13041 complain);
13042 else
13043 incr = RECUR (incr);
13044 TREE_VEC_ELT (declv, i) = decl;
13045 TREE_VEC_ELT (initv, i) = init;
13046 TREE_VEC_ELT (condv, i) = cond;
13047 TREE_VEC_ELT (incrv, i) = incr;
13048 return;
13051 if (init && !init_decl)
13053 tree c;
13054 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13056 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13057 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13058 && OMP_CLAUSE_DECL (c) == decl)
13059 break;
13060 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13061 && OMP_CLAUSE_DECL (c) == decl)
13062 error ("iteration variable %qD should not be firstprivate", decl);
13063 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13064 && OMP_CLAUSE_DECL (c) == decl)
13065 error ("iteration variable %qD should not be reduction", decl);
13067 if (c == NULL)
13069 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13070 OMP_CLAUSE_DECL (c) = decl;
13071 c = finish_omp_clauses (c);
13072 if (c)
13074 OMP_CLAUSE_CHAIN (c) = *clauses;
13075 *clauses = c;
13079 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13080 if (COMPARISON_CLASS_P (cond))
13081 cond = build2 (TREE_CODE (cond), boolean_type_node,
13082 RECUR (TREE_OPERAND (cond, 0)),
13083 RECUR (TREE_OPERAND (cond, 1)));
13084 else
13085 cond = RECUR (cond);
13086 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13087 switch (TREE_CODE (incr))
13089 case PREINCREMENT_EXPR:
13090 case PREDECREMENT_EXPR:
13091 case POSTINCREMENT_EXPR:
13092 case POSTDECREMENT_EXPR:
13093 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13094 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13095 break;
13096 case MODIFY_EXPR:
13097 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13098 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13100 tree rhs = TREE_OPERAND (incr, 1);
13101 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13102 RECUR (TREE_OPERAND (incr, 0)),
13103 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13104 RECUR (TREE_OPERAND (rhs, 0)),
13105 RECUR (TREE_OPERAND (rhs, 1))));
13107 else
13108 incr = RECUR (incr);
13109 break;
13110 case MODOP_EXPR:
13111 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13112 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13114 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13115 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13116 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13117 TREE_TYPE (decl), lhs,
13118 RECUR (TREE_OPERAND (incr, 2))));
13120 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13121 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13122 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13124 tree rhs = TREE_OPERAND (incr, 2);
13125 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13126 RECUR (TREE_OPERAND (incr, 0)),
13127 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13128 RECUR (TREE_OPERAND (rhs, 0)),
13129 RECUR (TREE_OPERAND (rhs, 1))));
13131 else
13132 incr = RECUR (incr);
13133 break;
13134 default:
13135 incr = RECUR (incr);
13136 break;
13139 TREE_VEC_ELT (declv, i) = decl;
13140 TREE_VEC_ELT (initv, i) = init;
13141 TREE_VEC_ELT (condv, i) = cond;
13142 TREE_VEC_ELT (incrv, i) = incr;
13143 #undef RECUR
13146 /* Like tsubst_copy for expressions, etc. but also does semantic
13147 processing. */
13149 static tree
13150 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13151 bool integral_constant_expression_p)
13153 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13154 #define RECUR(NODE) \
13155 tsubst_expr ((NODE), args, complain, in_decl, \
13156 integral_constant_expression_p)
13158 tree stmt, tmp;
13159 tree r;
13160 location_t loc;
13162 if (t == NULL_TREE || t == error_mark_node)
13163 return t;
13165 loc = input_location;
13166 if (EXPR_HAS_LOCATION (t))
13167 input_location = EXPR_LOCATION (t);
13168 if (STATEMENT_CODE_P (TREE_CODE (t)))
13169 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13171 switch (TREE_CODE (t))
13173 case STATEMENT_LIST:
13175 tree_stmt_iterator i;
13176 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13177 RECUR (tsi_stmt (i));
13178 break;
13181 case CTOR_INITIALIZER:
13182 finish_mem_initializers (tsubst_initializer_list
13183 (TREE_OPERAND (t, 0), args));
13184 break;
13186 case RETURN_EXPR:
13187 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13188 break;
13190 case EXPR_STMT:
13191 tmp = RECUR (EXPR_STMT_EXPR (t));
13192 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13193 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13194 else
13195 finish_expr_stmt (tmp);
13196 break;
13198 case USING_STMT:
13199 do_using_directive (USING_STMT_NAMESPACE (t));
13200 break;
13202 case DECL_EXPR:
13204 tree decl, pattern_decl;
13205 tree init;
13207 pattern_decl = decl = DECL_EXPR_DECL (t);
13208 if (TREE_CODE (decl) == LABEL_DECL)
13209 finish_label_decl (DECL_NAME (decl));
13210 else if (TREE_CODE (decl) == USING_DECL)
13212 tree scope = USING_DECL_SCOPE (decl);
13213 tree name = DECL_NAME (decl);
13214 tree decl;
13216 scope = tsubst (scope, args, complain, in_decl);
13217 decl = lookup_qualified_name (scope, name,
13218 /*is_type_p=*/false,
13219 /*complain=*/false);
13220 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13221 qualified_name_lookup_error (scope, name, decl, input_location);
13222 else
13223 do_local_using_decl (decl, scope, name);
13225 else if (DECL_PACK_P (decl))
13227 /* Don't build up decls for a variadic capture proxy, we'll
13228 instantiate the elements directly as needed. */
13229 break;
13231 else
13233 init = DECL_INITIAL (decl);
13234 decl = tsubst (decl, args, complain, in_decl);
13235 if (decl != error_mark_node)
13237 /* By marking the declaration as instantiated, we avoid
13238 trying to instantiate it. Since instantiate_decl can't
13239 handle local variables, and since we've already done
13240 all that needs to be done, that's the right thing to
13241 do. */
13242 if (VAR_P (decl))
13243 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13244 if (VAR_P (decl)
13245 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13246 /* Anonymous aggregates are a special case. */
13247 finish_anon_union (decl);
13248 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13250 DECL_CONTEXT (decl) = current_function_decl;
13251 if (DECL_NAME (decl) == this_identifier)
13253 tree lam = DECL_CONTEXT (current_function_decl);
13254 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13255 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13257 insert_capture_proxy (decl);
13259 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13260 /* We already did a pushtag. */;
13261 else if (TREE_CODE (decl) == FUNCTION_DECL
13262 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13263 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13265 DECL_CONTEXT (decl) = NULL_TREE;
13266 pushdecl (decl);
13267 DECL_CONTEXT (decl) = current_function_decl;
13268 cp_check_omp_declare_reduction (decl);
13270 else
13272 int const_init = false;
13273 maybe_push_decl (decl);
13274 if (VAR_P (decl)
13275 && DECL_PRETTY_FUNCTION_P (decl))
13277 /* For __PRETTY_FUNCTION__ we have to adjust the
13278 initializer. */
13279 const char *const name
13280 = cxx_printable_name (current_function_decl, 2);
13281 init = cp_fname_init (name, &TREE_TYPE (decl));
13283 else
13285 tree t = RECUR (init);
13287 if (init && !t)
13289 /* If we had an initializer but it
13290 instantiated to nothing,
13291 value-initialize the object. This will
13292 only occur when the initializer was a
13293 pack expansion where the parameter packs
13294 used in that expansion were of length
13295 zero. */
13296 init = build_value_init (TREE_TYPE (decl),
13297 complain);
13298 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13299 init = get_target_expr_sfinae (init, complain);
13301 else
13302 init = t;
13305 if (VAR_P (decl))
13306 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13307 (pattern_decl));
13308 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13313 break;
13316 case FOR_STMT:
13317 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13318 RECUR (FOR_INIT_STMT (t));
13319 finish_for_init_stmt (stmt);
13320 tmp = RECUR (FOR_COND (t));
13321 finish_for_cond (tmp, stmt, false);
13322 tmp = RECUR (FOR_EXPR (t));
13323 finish_for_expr (tmp, stmt);
13324 RECUR (FOR_BODY (t));
13325 finish_for_stmt (stmt);
13326 break;
13328 case RANGE_FOR_STMT:
13330 tree decl, expr;
13331 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13332 decl = RANGE_FOR_DECL (t);
13333 decl = tsubst (decl, args, complain, in_decl);
13334 maybe_push_decl (decl);
13335 expr = RECUR (RANGE_FOR_EXPR (t));
13336 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13337 RECUR (RANGE_FOR_BODY (t));
13338 finish_for_stmt (stmt);
13340 break;
13342 case WHILE_STMT:
13343 stmt = begin_while_stmt ();
13344 tmp = RECUR (WHILE_COND (t));
13345 finish_while_stmt_cond (tmp, stmt, false);
13346 RECUR (WHILE_BODY (t));
13347 finish_while_stmt (stmt);
13348 break;
13350 case DO_STMT:
13351 stmt = begin_do_stmt ();
13352 RECUR (DO_BODY (t));
13353 finish_do_body (stmt);
13354 tmp = RECUR (DO_COND (t));
13355 finish_do_stmt (tmp, stmt, false);
13356 break;
13358 case IF_STMT:
13359 stmt = begin_if_stmt ();
13360 tmp = RECUR (IF_COND (t));
13361 finish_if_stmt_cond (tmp, stmt);
13362 RECUR (THEN_CLAUSE (t));
13363 finish_then_clause (stmt);
13365 if (ELSE_CLAUSE (t))
13367 begin_else_clause (stmt);
13368 RECUR (ELSE_CLAUSE (t));
13369 finish_else_clause (stmt);
13372 finish_if_stmt (stmt);
13373 break;
13375 case BIND_EXPR:
13376 if (BIND_EXPR_BODY_BLOCK (t))
13377 stmt = begin_function_body ();
13378 else
13379 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13380 ? BCS_TRY_BLOCK : 0);
13382 RECUR (BIND_EXPR_BODY (t));
13384 if (BIND_EXPR_BODY_BLOCK (t))
13385 finish_function_body (stmt);
13386 else
13387 finish_compound_stmt (stmt);
13388 break;
13390 case BREAK_STMT:
13391 finish_break_stmt ();
13392 break;
13394 case CONTINUE_STMT:
13395 finish_continue_stmt ();
13396 break;
13398 case SWITCH_STMT:
13399 stmt = begin_switch_stmt ();
13400 tmp = RECUR (SWITCH_STMT_COND (t));
13401 finish_switch_cond (tmp, stmt);
13402 RECUR (SWITCH_STMT_BODY (t));
13403 finish_switch_stmt (stmt);
13404 break;
13406 case CASE_LABEL_EXPR:
13407 finish_case_label (EXPR_LOCATION (t),
13408 RECUR (CASE_LOW (t)),
13409 RECUR (CASE_HIGH (t)));
13410 break;
13412 case LABEL_EXPR:
13414 tree decl = LABEL_EXPR_LABEL (t);
13415 tree label;
13417 label = finish_label_stmt (DECL_NAME (decl));
13418 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13419 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13421 break;
13423 case GOTO_EXPR:
13424 tmp = GOTO_DESTINATION (t);
13425 if (TREE_CODE (tmp) != LABEL_DECL)
13426 /* Computed goto's must be tsubst'd into. On the other hand,
13427 non-computed gotos must not be; the identifier in question
13428 will have no binding. */
13429 tmp = RECUR (tmp);
13430 else
13431 tmp = DECL_NAME (tmp);
13432 finish_goto_stmt (tmp);
13433 break;
13435 case ASM_EXPR:
13436 tmp = finish_asm_stmt
13437 (ASM_VOLATILE_P (t),
13438 RECUR (ASM_STRING (t)),
13439 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13440 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13441 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13442 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13444 tree asm_expr = tmp;
13445 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13446 asm_expr = TREE_OPERAND (asm_expr, 0);
13447 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13449 break;
13451 case TRY_BLOCK:
13452 if (CLEANUP_P (t))
13454 stmt = begin_try_block ();
13455 RECUR (TRY_STMTS (t));
13456 finish_cleanup_try_block (stmt);
13457 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13459 else
13461 tree compound_stmt = NULL_TREE;
13463 if (FN_TRY_BLOCK_P (t))
13464 stmt = begin_function_try_block (&compound_stmt);
13465 else
13466 stmt = begin_try_block ();
13468 RECUR (TRY_STMTS (t));
13470 if (FN_TRY_BLOCK_P (t))
13471 finish_function_try_block (stmt);
13472 else
13473 finish_try_block (stmt);
13475 RECUR (TRY_HANDLERS (t));
13476 if (FN_TRY_BLOCK_P (t))
13477 finish_function_handler_sequence (stmt, compound_stmt);
13478 else
13479 finish_handler_sequence (stmt);
13481 break;
13483 case HANDLER:
13485 tree decl = HANDLER_PARMS (t);
13487 if (decl)
13489 decl = tsubst (decl, args, complain, in_decl);
13490 /* Prevent instantiate_decl from trying to instantiate
13491 this variable. We've already done all that needs to be
13492 done. */
13493 if (decl != error_mark_node)
13494 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13496 stmt = begin_handler ();
13497 finish_handler_parms (decl, stmt);
13498 RECUR (HANDLER_BODY (t));
13499 finish_handler (stmt);
13501 break;
13503 case TAG_DEFN:
13504 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13505 if (CLASS_TYPE_P (tmp))
13507 /* Local classes are not independent templates; they are
13508 instantiated along with their containing function. And this
13509 way we don't have to deal with pushing out of one local class
13510 to instantiate a member of another local class. */
13511 tree fn;
13512 /* Closures are handled by the LAMBDA_EXPR. */
13513 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13514 complete_type (tmp);
13515 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13516 if (!DECL_ARTIFICIAL (fn))
13517 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13519 break;
13521 case STATIC_ASSERT:
13523 tree condition;
13525 ++c_inhibit_evaluation_warnings;
13526 condition =
13527 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13528 args,
13529 complain, in_decl,
13530 /*integral_constant_expression_p=*/true);
13531 --c_inhibit_evaluation_warnings;
13533 finish_static_assert (condition,
13534 STATIC_ASSERT_MESSAGE (t),
13535 STATIC_ASSERT_SOURCE_LOCATION (t),
13536 /*member_p=*/false);
13538 break;
13540 case OMP_PARALLEL:
13541 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13542 args, complain, in_decl);
13543 stmt = begin_omp_parallel ();
13544 RECUR (OMP_PARALLEL_BODY (t));
13545 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13546 = OMP_PARALLEL_COMBINED (t);
13547 break;
13549 case OMP_TASK:
13550 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13551 args, complain, in_decl);
13552 stmt = begin_omp_task ();
13553 RECUR (OMP_TASK_BODY (t));
13554 finish_omp_task (tmp, stmt);
13555 break;
13557 case OMP_FOR:
13558 case OMP_SIMD:
13559 case OMP_DISTRIBUTE:
13561 tree clauses, body, pre_body;
13562 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13563 tree incrv = NULL_TREE;
13564 int i;
13566 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13567 args, complain, in_decl);
13568 if (OMP_FOR_INIT (t) != NULL_TREE)
13570 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13571 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13572 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13573 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13576 stmt = begin_omp_structured_block ();
13578 pre_body = push_stmt_list ();
13579 RECUR (OMP_FOR_PRE_BODY (t));
13580 pre_body = pop_stmt_list (pre_body);
13582 if (OMP_FOR_INIT (t) != NULL_TREE)
13583 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13584 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13585 &clauses, args, complain, in_decl,
13586 integral_constant_expression_p);
13588 body = push_stmt_list ();
13589 RECUR (OMP_FOR_BODY (t));
13590 body = pop_stmt_list (body);
13592 if (OMP_FOR_INIT (t) != NULL_TREE)
13593 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13594 condv, incrv, body, pre_body, clauses);
13595 else
13597 t = make_node (TREE_CODE (t));
13598 TREE_TYPE (t) = void_type_node;
13599 OMP_FOR_BODY (t) = body;
13600 OMP_FOR_PRE_BODY (t) = pre_body;
13601 OMP_FOR_CLAUSES (t) = clauses;
13602 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13603 add_stmt (t);
13606 add_stmt (finish_omp_structured_block (stmt));
13608 break;
13610 case OMP_SECTIONS:
13611 case OMP_SINGLE:
13612 case OMP_TEAMS:
13613 case OMP_TARGET_DATA:
13614 case OMP_TARGET:
13615 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13616 args, complain, in_decl);
13617 stmt = push_stmt_list ();
13618 RECUR (OMP_BODY (t));
13619 stmt = pop_stmt_list (stmt);
13621 t = copy_node (t);
13622 OMP_BODY (t) = stmt;
13623 OMP_CLAUSES (t) = tmp;
13624 add_stmt (t);
13625 break;
13627 case OMP_TARGET_UPDATE:
13628 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13629 args, complain, in_decl);
13630 t = copy_node (t);
13631 OMP_CLAUSES (t) = tmp;
13632 add_stmt (t);
13633 break;
13635 case OMP_SECTION:
13636 case OMP_CRITICAL:
13637 case OMP_MASTER:
13638 case OMP_TASKGROUP:
13639 case OMP_ORDERED:
13640 stmt = push_stmt_list ();
13641 RECUR (OMP_BODY (t));
13642 stmt = pop_stmt_list (stmt);
13644 t = copy_node (t);
13645 OMP_BODY (t) = stmt;
13646 add_stmt (t);
13647 break;
13649 case OMP_ATOMIC:
13650 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13651 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13653 tree op1 = TREE_OPERAND (t, 1);
13654 tree rhs1 = NULL_TREE;
13655 tree lhs, rhs;
13656 if (TREE_CODE (op1) == COMPOUND_EXPR)
13658 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13659 op1 = TREE_OPERAND (op1, 1);
13661 lhs = RECUR (TREE_OPERAND (op1, 0));
13662 rhs = RECUR (TREE_OPERAND (op1, 1));
13663 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13664 NULL_TREE, NULL_TREE, rhs1,
13665 OMP_ATOMIC_SEQ_CST (t));
13667 else
13669 tree op1 = TREE_OPERAND (t, 1);
13670 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13671 tree rhs1 = NULL_TREE;
13672 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13673 enum tree_code opcode = NOP_EXPR;
13674 if (code == OMP_ATOMIC_READ)
13676 v = RECUR (TREE_OPERAND (op1, 0));
13677 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13679 else if (code == OMP_ATOMIC_CAPTURE_OLD
13680 || code == OMP_ATOMIC_CAPTURE_NEW)
13682 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13683 v = RECUR (TREE_OPERAND (op1, 0));
13684 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13685 if (TREE_CODE (op11) == COMPOUND_EXPR)
13687 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13688 op11 = TREE_OPERAND (op11, 1);
13690 lhs = RECUR (TREE_OPERAND (op11, 0));
13691 rhs = RECUR (TREE_OPERAND (op11, 1));
13692 opcode = TREE_CODE (op11);
13693 if (opcode == MODIFY_EXPR)
13694 opcode = NOP_EXPR;
13696 else
13698 code = OMP_ATOMIC;
13699 lhs = RECUR (TREE_OPERAND (op1, 0));
13700 rhs = RECUR (TREE_OPERAND (op1, 1));
13702 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13703 OMP_ATOMIC_SEQ_CST (t));
13705 break;
13707 case TRANSACTION_EXPR:
13709 int flags = 0;
13710 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13711 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13713 if (TRANSACTION_EXPR_IS_STMT (t))
13715 tree body = TRANSACTION_EXPR_BODY (t);
13716 tree noex = NULL_TREE;
13717 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13719 noex = MUST_NOT_THROW_COND (body);
13720 if (noex == NULL_TREE)
13721 noex = boolean_true_node;
13722 body = TREE_OPERAND (body, 0);
13724 stmt = begin_transaction_stmt (input_location, NULL, flags);
13725 RECUR (body);
13726 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13728 else
13730 stmt = build_transaction_expr (EXPR_LOCATION (t),
13731 RECUR (TRANSACTION_EXPR_BODY (t)),
13732 flags, NULL_TREE);
13733 RETURN (stmt);
13736 break;
13738 case MUST_NOT_THROW_EXPR:
13739 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13740 RECUR (MUST_NOT_THROW_COND (t))));
13742 case EXPR_PACK_EXPANSION:
13743 error ("invalid use of pack expansion expression");
13744 RETURN (error_mark_node);
13746 case NONTYPE_ARGUMENT_PACK:
13747 error ("use %<...%> to expand argument pack");
13748 RETURN (error_mark_node);
13750 case COMPOUND_EXPR:
13751 tmp = RECUR (TREE_OPERAND (t, 0));
13752 if (tmp == NULL_TREE)
13753 /* If the first operand was a statement, we're done with it. */
13754 RETURN (RECUR (TREE_OPERAND (t, 1)));
13755 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13756 RECUR (TREE_OPERAND (t, 1)),
13757 complain));
13759 default:
13760 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13762 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13763 /*function_p=*/false,
13764 integral_constant_expression_p));
13767 RETURN (NULL_TREE);
13768 out:
13769 input_location = loc;
13770 return r;
13771 #undef RECUR
13772 #undef RETURN
13775 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13776 function. For description of the body see comment above
13777 cp_parser_omp_declare_reduction_exprs. */
13779 static void
13780 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13782 if (t == NULL_TREE || t == error_mark_node)
13783 return;
13785 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13787 tree_stmt_iterator tsi;
13788 int i;
13789 tree stmts[7];
13790 memset (stmts, 0, sizeof stmts);
13791 for (i = 0, tsi = tsi_start (t);
13792 i < 7 && !tsi_end_p (tsi);
13793 i++, tsi_next (&tsi))
13794 stmts[i] = tsi_stmt (tsi);
13795 gcc_assert (tsi_end_p (tsi));
13797 if (i >= 3)
13799 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13800 && TREE_CODE (stmts[1]) == DECL_EXPR);
13801 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13802 args, complain, in_decl);
13803 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13804 args, complain, in_decl);
13805 DECL_CONTEXT (omp_out) = current_function_decl;
13806 DECL_CONTEXT (omp_in) = current_function_decl;
13807 keep_next_level (true);
13808 tree block = begin_omp_structured_block ();
13809 tsubst_expr (stmts[2], args, complain, in_decl, false);
13810 block = finish_omp_structured_block (block);
13811 block = maybe_cleanup_point_expr_void (block);
13812 add_decl_expr (omp_out);
13813 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13814 TREE_NO_WARNING (omp_out) = 1;
13815 add_decl_expr (omp_in);
13816 finish_expr_stmt (block);
13818 if (i >= 6)
13820 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13821 && TREE_CODE (stmts[4]) == DECL_EXPR);
13822 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13823 args, complain, in_decl);
13824 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13825 args, complain, in_decl);
13826 DECL_CONTEXT (omp_priv) = current_function_decl;
13827 DECL_CONTEXT (omp_orig) = current_function_decl;
13828 keep_next_level (true);
13829 tree block = begin_omp_structured_block ();
13830 tsubst_expr (stmts[5], args, complain, in_decl, false);
13831 block = finish_omp_structured_block (block);
13832 block = maybe_cleanup_point_expr_void (block);
13833 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13834 add_decl_expr (omp_priv);
13835 add_decl_expr (omp_orig);
13836 finish_expr_stmt (block);
13837 if (i == 7)
13838 add_decl_expr (omp_orig);
13842 /* T is a postfix-expression that is not being used in a function
13843 call. Return the substituted version of T. */
13845 static tree
13846 tsubst_non_call_postfix_expression (tree t, tree args,
13847 tsubst_flags_t complain,
13848 tree in_decl)
13850 if (TREE_CODE (t) == SCOPE_REF)
13851 t = tsubst_qualified_id (t, args, complain, in_decl,
13852 /*done=*/false, /*address_p=*/false);
13853 else
13854 t = tsubst_copy_and_build (t, args, complain, in_decl,
13855 /*function_p=*/false,
13856 /*integral_constant_expression_p=*/false);
13858 return t;
13861 /* Like tsubst but deals with expressions and performs semantic
13862 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13864 tree
13865 tsubst_copy_and_build (tree t,
13866 tree args,
13867 tsubst_flags_t complain,
13868 tree in_decl,
13869 bool function_p,
13870 bool integral_constant_expression_p)
13872 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13873 #define RECUR(NODE) \
13874 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13875 /*function_p=*/false, \
13876 integral_constant_expression_p)
13878 tree retval, op1;
13879 location_t loc;
13881 if (t == NULL_TREE || t == error_mark_node)
13882 return t;
13884 loc = input_location;
13885 if (EXPR_HAS_LOCATION (t))
13886 input_location = EXPR_LOCATION (t);
13888 /* N3276 decltype magic only applies to calls at the top level or on the
13889 right side of a comma. */
13890 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13891 complain &= ~tf_decltype;
13893 switch (TREE_CODE (t))
13895 case USING_DECL:
13896 t = DECL_NAME (t);
13897 /* Fall through. */
13898 case IDENTIFIER_NODE:
13900 tree decl;
13901 cp_id_kind idk;
13902 bool non_integral_constant_expression_p;
13903 const char *error_msg;
13905 if (IDENTIFIER_TYPENAME_P (t))
13907 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13908 t = mangle_conv_op_name_for_type (new_type);
13911 /* Look up the name. */
13912 decl = lookup_name (t);
13914 /* By convention, expressions use ERROR_MARK_NODE to indicate
13915 failure, not NULL_TREE. */
13916 if (decl == NULL_TREE)
13917 decl = error_mark_node;
13919 decl = finish_id_expression (t, decl, NULL_TREE,
13920 &idk,
13921 integral_constant_expression_p,
13922 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13923 &non_integral_constant_expression_p,
13924 /*template_p=*/false,
13925 /*done=*/true,
13926 /*address_p=*/false,
13927 /*template_arg_p=*/false,
13928 &error_msg,
13929 input_location);
13930 if (error_msg)
13931 error (error_msg);
13932 if (!function_p && identifier_p (decl))
13934 if (complain & tf_error)
13935 unqualified_name_lookup_error (decl);
13936 decl = error_mark_node;
13938 RETURN (decl);
13941 case TEMPLATE_ID_EXPR:
13943 tree object;
13944 tree templ = RECUR (TREE_OPERAND (t, 0));
13945 tree targs = TREE_OPERAND (t, 1);
13947 if (targs)
13948 targs = tsubst_template_args (targs, args, complain, in_decl);
13950 if (TREE_CODE (templ) == COMPONENT_REF)
13952 object = TREE_OPERAND (templ, 0);
13953 templ = TREE_OPERAND (templ, 1);
13955 else
13956 object = NULL_TREE;
13957 templ = lookup_template_function (templ, targs);
13959 if (object)
13960 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13961 object, templ, NULL_TREE));
13962 else
13963 RETURN (baselink_for_fns (templ));
13966 case INDIRECT_REF:
13968 tree r = RECUR (TREE_OPERAND (t, 0));
13970 if (REFERENCE_REF_P (t))
13972 /* A type conversion to reference type will be enclosed in
13973 such an indirect ref, but the substitution of the cast
13974 will have also added such an indirect ref. */
13975 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13976 r = convert_from_reference (r);
13978 else
13979 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
13980 complain|decltype_flag);
13981 RETURN (r);
13984 case NOP_EXPR:
13985 RETURN (build_nop
13986 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13987 RECUR (TREE_OPERAND (t, 0))));
13989 case IMPLICIT_CONV_EXPR:
13991 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13992 tree expr = RECUR (TREE_OPERAND (t, 0));
13993 int flags = LOOKUP_IMPLICIT;
13994 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13995 flags = LOOKUP_NORMAL;
13996 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13997 flags));
14000 case CONVERT_EXPR:
14001 RETURN (build1
14002 (CONVERT_EXPR,
14003 tsubst (TREE_TYPE (t), args, complain, in_decl),
14004 RECUR (TREE_OPERAND (t, 0))));
14006 case CAST_EXPR:
14007 case REINTERPRET_CAST_EXPR:
14008 case CONST_CAST_EXPR:
14009 case DYNAMIC_CAST_EXPR:
14010 case STATIC_CAST_EXPR:
14012 tree type;
14013 tree op, r = NULL_TREE;
14015 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14016 if (integral_constant_expression_p
14017 && !cast_valid_in_integral_constant_expression_p (type))
14019 if (complain & tf_error)
14020 error ("a cast to a type other than an integral or "
14021 "enumeration type cannot appear in a constant-expression");
14022 RETURN (error_mark_node);
14025 op = RECUR (TREE_OPERAND (t, 0));
14027 ++c_inhibit_evaluation_warnings;
14028 switch (TREE_CODE (t))
14030 case CAST_EXPR:
14031 r = build_functional_cast (type, op, complain);
14032 break;
14033 case REINTERPRET_CAST_EXPR:
14034 r = build_reinterpret_cast (type, op, complain);
14035 break;
14036 case CONST_CAST_EXPR:
14037 r = build_const_cast (type, op, complain);
14038 break;
14039 case DYNAMIC_CAST_EXPR:
14040 r = build_dynamic_cast (type, op, complain);
14041 break;
14042 case STATIC_CAST_EXPR:
14043 r = build_static_cast (type, op, complain);
14044 break;
14045 default:
14046 gcc_unreachable ();
14048 --c_inhibit_evaluation_warnings;
14050 RETURN (r);
14053 case POSTDECREMENT_EXPR:
14054 case POSTINCREMENT_EXPR:
14055 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14056 args, complain, in_decl);
14057 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14058 complain|decltype_flag));
14060 case PREDECREMENT_EXPR:
14061 case PREINCREMENT_EXPR:
14062 case NEGATE_EXPR:
14063 case BIT_NOT_EXPR:
14064 case ABS_EXPR:
14065 case TRUTH_NOT_EXPR:
14066 case UNARY_PLUS_EXPR: /* Unary + */
14067 case REALPART_EXPR:
14068 case IMAGPART_EXPR:
14069 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14070 RECUR (TREE_OPERAND (t, 0)),
14071 complain|decltype_flag));
14073 case FIX_TRUNC_EXPR:
14074 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14075 0, complain));
14077 case ADDR_EXPR:
14078 op1 = TREE_OPERAND (t, 0);
14079 if (TREE_CODE (op1) == LABEL_DECL)
14080 RETURN (finish_label_address_expr (DECL_NAME (op1),
14081 EXPR_LOCATION (op1)));
14082 if (TREE_CODE (op1) == SCOPE_REF)
14083 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14084 /*done=*/true, /*address_p=*/true);
14085 else
14086 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14087 in_decl);
14088 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14089 complain|decltype_flag));
14091 case PLUS_EXPR:
14092 case MINUS_EXPR:
14093 case MULT_EXPR:
14094 case TRUNC_DIV_EXPR:
14095 case CEIL_DIV_EXPR:
14096 case FLOOR_DIV_EXPR:
14097 case ROUND_DIV_EXPR:
14098 case EXACT_DIV_EXPR:
14099 case BIT_AND_EXPR:
14100 case BIT_IOR_EXPR:
14101 case BIT_XOR_EXPR:
14102 case TRUNC_MOD_EXPR:
14103 case FLOOR_MOD_EXPR:
14104 case TRUTH_ANDIF_EXPR:
14105 case TRUTH_ORIF_EXPR:
14106 case TRUTH_AND_EXPR:
14107 case TRUTH_OR_EXPR:
14108 case RSHIFT_EXPR:
14109 case LSHIFT_EXPR:
14110 case RROTATE_EXPR:
14111 case LROTATE_EXPR:
14112 case EQ_EXPR:
14113 case NE_EXPR:
14114 case MAX_EXPR:
14115 case MIN_EXPR:
14116 case LE_EXPR:
14117 case GE_EXPR:
14118 case LT_EXPR:
14119 case GT_EXPR:
14120 case MEMBER_REF:
14121 case DOTSTAR_EXPR:
14123 tree r;
14125 ++c_inhibit_evaluation_warnings;
14127 r = build_x_binary_op
14128 (input_location, TREE_CODE (t),
14129 RECUR (TREE_OPERAND (t, 0)),
14130 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14131 ? ERROR_MARK
14132 : TREE_CODE (TREE_OPERAND (t, 0))),
14133 RECUR (TREE_OPERAND (t, 1)),
14134 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14135 ? ERROR_MARK
14136 : TREE_CODE (TREE_OPERAND (t, 1))),
14137 /*overload=*/NULL,
14138 complain|decltype_flag);
14139 if (EXPR_P (r) && TREE_NO_WARNING (t))
14140 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14142 --c_inhibit_evaluation_warnings;
14144 RETURN (r);
14147 case SCOPE_REF:
14148 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14149 /*address_p=*/false));
14150 case ARRAY_REF:
14151 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14152 args, complain, in_decl);
14153 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14154 RECUR (TREE_OPERAND (t, 1)),
14155 complain|decltype_flag));
14157 case ARRAY_NOTATION_REF:
14159 tree start_index, length, stride;
14160 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14161 args, complain, in_decl);
14162 start_index = RECUR (ARRAY_NOTATION_START (t));
14163 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14164 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14165 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14166 length, stride, TREE_TYPE (op1)));
14168 case SIZEOF_EXPR:
14169 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14170 RETURN (tsubst_copy (t, args, complain, in_decl));
14171 /* Fall through */
14173 case ALIGNOF_EXPR:
14175 tree r;
14177 op1 = TREE_OPERAND (t, 0);
14178 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14179 op1 = TREE_TYPE (op1);
14180 if (!args)
14182 /* When there are no ARGS, we are trying to evaluate a
14183 non-dependent expression from the parser. Trying to do
14184 the substitutions may not work. */
14185 if (!TYPE_P (op1))
14186 op1 = TREE_TYPE (op1);
14188 else
14190 ++cp_unevaluated_operand;
14191 ++c_inhibit_evaluation_warnings;
14192 if (TYPE_P (op1))
14193 op1 = tsubst (op1, args, complain, in_decl);
14194 else
14195 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14196 /*function_p=*/false,
14197 /*integral_constant_expression_p=*/
14198 false);
14199 --cp_unevaluated_operand;
14200 --c_inhibit_evaluation_warnings;
14202 if (TYPE_P (op1))
14203 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14204 complain & tf_error);
14205 else
14206 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14207 complain & tf_error);
14208 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14210 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14212 if (!processing_template_decl && TYPE_P (op1))
14214 r = build_min (SIZEOF_EXPR, size_type_node,
14215 build1 (NOP_EXPR, op1, error_mark_node));
14216 SIZEOF_EXPR_TYPE_P (r) = 1;
14218 else
14219 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14220 TREE_SIDE_EFFECTS (r) = 0;
14221 TREE_READONLY (r) = 1;
14223 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14225 RETURN (r);
14228 case AT_ENCODE_EXPR:
14230 op1 = TREE_OPERAND (t, 0);
14231 ++cp_unevaluated_operand;
14232 ++c_inhibit_evaluation_warnings;
14233 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14234 /*function_p=*/false,
14235 /*integral_constant_expression_p=*/false);
14236 --cp_unevaluated_operand;
14237 --c_inhibit_evaluation_warnings;
14238 RETURN (objc_build_encode_expr (op1));
14241 case NOEXCEPT_EXPR:
14242 op1 = TREE_OPERAND (t, 0);
14243 ++cp_unevaluated_operand;
14244 ++c_inhibit_evaluation_warnings;
14245 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14246 /*function_p=*/false,
14247 /*integral_constant_expression_p=*/false);
14248 --cp_unevaluated_operand;
14249 --c_inhibit_evaluation_warnings;
14250 RETURN (finish_noexcept_expr (op1, complain));
14252 case MODOP_EXPR:
14254 tree r;
14256 ++c_inhibit_evaluation_warnings;
14258 r = build_x_modify_expr
14259 (EXPR_LOCATION (t),
14260 RECUR (TREE_OPERAND (t, 0)),
14261 TREE_CODE (TREE_OPERAND (t, 1)),
14262 RECUR (TREE_OPERAND (t, 2)),
14263 complain|decltype_flag);
14264 /* TREE_NO_WARNING must be set if either the expression was
14265 parenthesized or it uses an operator such as >>= rather
14266 than plain assignment. In the former case, it was already
14267 set and must be copied. In the latter case,
14268 build_x_modify_expr sets it and it must not be reset
14269 here. */
14270 if (TREE_NO_WARNING (t))
14271 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14273 --c_inhibit_evaluation_warnings;
14275 RETURN (r);
14278 case ARROW_EXPR:
14279 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14280 args, complain, in_decl);
14281 /* Remember that there was a reference to this entity. */
14282 if (DECL_P (op1))
14283 mark_used (op1);
14284 RETURN (build_x_arrow (input_location, op1, complain));
14286 case NEW_EXPR:
14288 tree placement = RECUR (TREE_OPERAND (t, 0));
14289 tree init = RECUR (TREE_OPERAND (t, 3));
14290 vec<tree, va_gc> *placement_vec;
14291 vec<tree, va_gc> *init_vec;
14292 tree ret;
14294 if (placement == NULL_TREE)
14295 placement_vec = NULL;
14296 else
14298 placement_vec = make_tree_vector ();
14299 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14300 vec_safe_push (placement_vec, TREE_VALUE (placement));
14303 /* If there was an initializer in the original tree, but it
14304 instantiated to an empty list, then we should pass a
14305 non-NULL empty vector to tell build_new that it was an
14306 empty initializer() rather than no initializer. This can
14307 only happen when the initializer is a pack expansion whose
14308 parameter packs are of length zero. */
14309 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14310 init_vec = NULL;
14311 else
14313 init_vec = make_tree_vector ();
14314 if (init == void_zero_node)
14315 gcc_assert (init_vec != NULL);
14316 else
14318 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14319 vec_safe_push (init_vec, TREE_VALUE (init));
14323 ret = build_new (&placement_vec,
14324 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14325 RECUR (TREE_OPERAND (t, 2)),
14326 &init_vec,
14327 NEW_EXPR_USE_GLOBAL (t),
14328 complain);
14330 if (placement_vec != NULL)
14331 release_tree_vector (placement_vec);
14332 if (init_vec != NULL)
14333 release_tree_vector (init_vec);
14335 RETURN (ret);
14338 case DELETE_EXPR:
14339 RETURN (delete_sanity
14340 (RECUR (TREE_OPERAND (t, 0)),
14341 RECUR (TREE_OPERAND (t, 1)),
14342 DELETE_EXPR_USE_VEC (t),
14343 DELETE_EXPR_USE_GLOBAL (t),
14344 complain));
14346 case COMPOUND_EXPR:
14348 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14349 complain & ~tf_decltype, in_decl,
14350 /*function_p=*/false,
14351 integral_constant_expression_p);
14352 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14353 op0,
14354 RECUR (TREE_OPERAND (t, 1)),
14355 complain|decltype_flag));
14358 case CALL_EXPR:
14360 tree function;
14361 vec<tree, va_gc> *call_args;
14362 unsigned int nargs, i;
14363 bool qualified_p;
14364 bool koenig_p;
14365 tree ret;
14367 function = CALL_EXPR_FN (t);
14368 /* When we parsed the expression, we determined whether or
14369 not Koenig lookup should be performed. */
14370 koenig_p = KOENIG_LOOKUP_P (t);
14371 if (TREE_CODE (function) == SCOPE_REF)
14373 qualified_p = true;
14374 function = tsubst_qualified_id (function, args, complain, in_decl,
14375 /*done=*/false,
14376 /*address_p=*/false);
14378 else if (koenig_p && identifier_p (function))
14380 /* Do nothing; calling tsubst_copy_and_build on an identifier
14381 would incorrectly perform unqualified lookup again.
14383 Note that we can also have an IDENTIFIER_NODE if the earlier
14384 unqualified lookup found a member function; in that case
14385 koenig_p will be false and we do want to do the lookup
14386 again to find the instantiated member function.
14388 FIXME but doing that causes c++/15272, so we need to stop
14389 using IDENTIFIER_NODE in that situation. */
14390 qualified_p = false;
14392 else
14394 if (TREE_CODE (function) == COMPONENT_REF)
14396 tree op = TREE_OPERAND (function, 1);
14398 qualified_p = (TREE_CODE (op) == SCOPE_REF
14399 || (BASELINK_P (op)
14400 && BASELINK_QUALIFIED_P (op)));
14402 else
14403 qualified_p = false;
14405 if (TREE_CODE (function) == ADDR_EXPR
14406 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14407 /* Avoid error about taking the address of a constructor. */
14408 function = TREE_OPERAND (function, 0);
14410 function = tsubst_copy_and_build (function, args, complain,
14411 in_decl,
14412 !qualified_p,
14413 integral_constant_expression_p);
14415 if (BASELINK_P (function))
14416 qualified_p = true;
14419 nargs = call_expr_nargs (t);
14420 call_args = make_tree_vector ();
14421 for (i = 0; i < nargs; ++i)
14423 tree arg = CALL_EXPR_ARG (t, i);
14425 if (!PACK_EXPANSION_P (arg))
14426 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14427 else
14429 /* Expand the pack expansion and push each entry onto
14430 CALL_ARGS. */
14431 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14432 if (TREE_CODE (arg) == TREE_VEC)
14434 unsigned int len, j;
14436 len = TREE_VEC_LENGTH (arg);
14437 for (j = 0; j < len; ++j)
14439 tree value = TREE_VEC_ELT (arg, j);
14440 if (value != NULL_TREE)
14441 value = convert_from_reference (value);
14442 vec_safe_push (call_args, value);
14445 else
14447 /* A partial substitution. Add one entry. */
14448 vec_safe_push (call_args, arg);
14453 /* We do not perform argument-dependent lookup if normal
14454 lookup finds a non-function, in accordance with the
14455 expected resolution of DR 218. */
14456 if (koenig_p
14457 && ((is_overloaded_fn (function)
14458 /* If lookup found a member function, the Koenig lookup is
14459 not appropriate, even if an unqualified-name was used
14460 to denote the function. */
14461 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14462 || identifier_p (function))
14463 /* Only do this when substitution turns a dependent call
14464 into a non-dependent call. */
14465 && type_dependent_expression_p_push (t)
14466 && !any_type_dependent_arguments_p (call_args))
14467 function = perform_koenig_lookup (function, call_args, false,
14468 tf_none);
14470 if (identifier_p (function)
14471 && !any_type_dependent_arguments_p (call_args))
14473 if (koenig_p && (complain & tf_warning_or_error))
14475 /* For backwards compatibility and good diagnostics, try
14476 the unqualified lookup again if we aren't in SFINAE
14477 context. */
14478 tree unq = (tsubst_copy_and_build
14479 (function, args, complain, in_decl, true,
14480 integral_constant_expression_p));
14481 if (unq == error_mark_node)
14482 RETURN (error_mark_node);
14484 if (unq != function)
14486 tree fn = unq;
14487 if (INDIRECT_REF_P (fn))
14488 fn = TREE_OPERAND (fn, 0);
14489 if (TREE_CODE (fn) == COMPONENT_REF)
14490 fn = TREE_OPERAND (fn, 1);
14491 if (is_overloaded_fn (fn))
14492 fn = get_first_fn (fn);
14493 if (permerror (EXPR_LOC_OR_HERE (t),
14494 "%qD was not declared in this scope, "
14495 "and no declarations were found by "
14496 "argument-dependent lookup at the point "
14497 "of instantiation", function))
14499 if (!DECL_P (fn))
14500 /* Can't say anything more. */;
14501 else if (DECL_CLASS_SCOPE_P (fn))
14503 inform (EXPR_LOC_OR_HERE (t),
14504 "declarations in dependent base %qT are "
14505 "not found by unqualified lookup",
14506 DECL_CLASS_CONTEXT (fn));
14507 if (current_class_ptr)
14508 inform (EXPR_LOC_OR_HERE (t),
14509 "use %<this->%D%> instead", function);
14510 else
14511 inform (EXPR_LOC_OR_HERE (t),
14512 "use %<%T::%D%> instead",
14513 current_class_name, function);
14515 else
14516 inform (0, "%q+D declared here, later in the "
14517 "translation unit", fn);
14519 function = unq;
14522 if (identifier_p (function))
14524 if (complain & tf_error)
14525 unqualified_name_lookup_error (function);
14526 release_tree_vector (call_args);
14527 RETURN (error_mark_node);
14531 /* Remember that there was a reference to this entity. */
14532 if (DECL_P (function))
14533 mark_used (function);
14535 /* Put back tf_decltype for the actual call. */
14536 complain |= decltype_flag;
14538 if (TREE_CODE (function) == OFFSET_REF)
14539 ret = build_offset_ref_call_from_tree (function, &call_args,
14540 complain);
14541 else if (TREE_CODE (function) == COMPONENT_REF)
14543 tree instance = TREE_OPERAND (function, 0);
14544 tree fn = TREE_OPERAND (function, 1);
14546 if (processing_template_decl
14547 && (type_dependent_expression_p (instance)
14548 || (!BASELINK_P (fn)
14549 && TREE_CODE (fn) != FIELD_DECL)
14550 || type_dependent_expression_p (fn)
14551 || any_type_dependent_arguments_p (call_args)))
14552 ret = build_nt_call_vec (function, call_args);
14553 else if (!BASELINK_P (fn))
14554 ret = finish_call_expr (function, &call_args,
14555 /*disallow_virtual=*/false,
14556 /*koenig_p=*/false,
14557 complain);
14558 else
14559 ret = (build_new_method_call
14560 (instance, fn,
14561 &call_args, NULL_TREE,
14562 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14563 /*fn_p=*/NULL,
14564 complain));
14566 else
14567 ret = finish_call_expr (function, &call_args,
14568 /*disallow_virtual=*/qualified_p,
14569 koenig_p,
14570 complain);
14572 release_tree_vector (call_args);
14574 RETURN (ret);
14577 case COND_EXPR:
14579 tree cond = RECUR (TREE_OPERAND (t, 0));
14580 tree exp1, exp2;
14582 if (TREE_CODE (cond) == INTEGER_CST)
14584 if (integer_zerop (cond))
14586 ++c_inhibit_evaluation_warnings;
14587 exp1 = RECUR (TREE_OPERAND (t, 1));
14588 --c_inhibit_evaluation_warnings;
14589 exp2 = RECUR (TREE_OPERAND (t, 2));
14591 else
14593 exp1 = RECUR (TREE_OPERAND (t, 1));
14594 ++c_inhibit_evaluation_warnings;
14595 exp2 = RECUR (TREE_OPERAND (t, 2));
14596 --c_inhibit_evaluation_warnings;
14599 else
14601 exp1 = RECUR (TREE_OPERAND (t, 1));
14602 exp2 = RECUR (TREE_OPERAND (t, 2));
14605 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14606 cond, exp1, exp2, complain));
14609 case PSEUDO_DTOR_EXPR:
14610 RETURN (finish_pseudo_destructor_expr
14611 (RECUR (TREE_OPERAND (t, 0)),
14612 RECUR (TREE_OPERAND (t, 1)),
14613 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14614 input_location));
14616 case TREE_LIST:
14618 tree purpose, value, chain;
14620 if (t == void_list_node)
14621 RETURN (t);
14623 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14624 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14626 /* We have pack expansions, so expand those and
14627 create a new list out of it. */
14628 tree purposevec = NULL_TREE;
14629 tree valuevec = NULL_TREE;
14630 tree chain;
14631 int i, len = -1;
14633 /* Expand the argument expressions. */
14634 if (TREE_PURPOSE (t))
14635 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14636 complain, in_decl);
14637 if (TREE_VALUE (t))
14638 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14639 complain, in_decl);
14641 /* Build the rest of the list. */
14642 chain = TREE_CHAIN (t);
14643 if (chain && chain != void_type_node)
14644 chain = RECUR (chain);
14646 /* Determine the number of arguments. */
14647 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14649 len = TREE_VEC_LENGTH (purposevec);
14650 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14652 else if (TREE_CODE (valuevec) == TREE_VEC)
14653 len = TREE_VEC_LENGTH (valuevec);
14654 else
14656 /* Since we only performed a partial substitution into
14657 the argument pack, we only RETURN (a single list
14658 node. */
14659 if (purposevec == TREE_PURPOSE (t)
14660 && valuevec == TREE_VALUE (t)
14661 && chain == TREE_CHAIN (t))
14662 RETURN (t);
14664 RETURN (tree_cons (purposevec, valuevec, chain));
14667 /* Convert the argument vectors into a TREE_LIST */
14668 i = len;
14669 while (i > 0)
14671 /* Grab the Ith values. */
14672 i--;
14673 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14674 : NULL_TREE;
14675 value
14676 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14677 : NULL_TREE;
14679 /* Build the list (backwards). */
14680 chain = tree_cons (purpose, value, chain);
14683 RETURN (chain);
14686 purpose = TREE_PURPOSE (t);
14687 if (purpose)
14688 purpose = RECUR (purpose);
14689 value = TREE_VALUE (t);
14690 if (value)
14691 value = RECUR (value);
14692 chain = TREE_CHAIN (t);
14693 if (chain && chain != void_type_node)
14694 chain = RECUR (chain);
14695 if (purpose == TREE_PURPOSE (t)
14696 && value == TREE_VALUE (t)
14697 && chain == TREE_CHAIN (t))
14698 RETURN (t);
14699 RETURN (tree_cons (purpose, value, chain));
14702 case COMPONENT_REF:
14704 tree object;
14705 tree object_type;
14706 tree member;
14708 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14709 args, complain, in_decl);
14710 /* Remember that there was a reference to this entity. */
14711 if (DECL_P (object))
14712 mark_used (object);
14713 object_type = TREE_TYPE (object);
14715 member = TREE_OPERAND (t, 1);
14716 if (BASELINK_P (member))
14717 member = tsubst_baselink (member,
14718 non_reference (TREE_TYPE (object)),
14719 args, complain, in_decl);
14720 else
14721 member = tsubst_copy (member, args, complain, in_decl);
14722 if (member == error_mark_node)
14723 RETURN (error_mark_node);
14725 if (type_dependent_expression_p (object))
14726 /* We can't do much here. */;
14727 else if (!CLASS_TYPE_P (object_type))
14729 if (scalarish_type_p (object_type))
14731 tree s = NULL_TREE;
14732 tree dtor = member;
14734 if (TREE_CODE (dtor) == SCOPE_REF)
14736 s = TREE_OPERAND (dtor, 0);
14737 dtor = TREE_OPERAND (dtor, 1);
14739 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14741 dtor = TREE_OPERAND (dtor, 0);
14742 if (TYPE_P (dtor))
14743 RETURN (finish_pseudo_destructor_expr
14744 (object, s, dtor, input_location));
14748 else if (TREE_CODE (member) == SCOPE_REF
14749 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14751 /* Lookup the template functions now that we know what the
14752 scope is. */
14753 tree scope = TREE_OPERAND (member, 0);
14754 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14755 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14756 member = lookup_qualified_name (scope, tmpl,
14757 /*is_type_p=*/false,
14758 /*complain=*/false);
14759 if (BASELINK_P (member))
14761 BASELINK_FUNCTIONS (member)
14762 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14763 args);
14764 member = (adjust_result_of_qualified_name_lookup
14765 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14766 object_type));
14768 else
14770 qualified_name_lookup_error (scope, tmpl, member,
14771 input_location);
14772 RETURN (error_mark_node);
14775 else if (TREE_CODE (member) == SCOPE_REF
14776 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14777 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14779 if (complain & tf_error)
14781 if (TYPE_P (TREE_OPERAND (member, 0)))
14782 error ("%qT is not a class or namespace",
14783 TREE_OPERAND (member, 0));
14784 else
14785 error ("%qD is not a class or namespace",
14786 TREE_OPERAND (member, 0));
14788 RETURN (error_mark_node);
14790 else if (TREE_CODE (member) == FIELD_DECL)
14791 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14793 RETURN (finish_class_member_access_expr (object, member,
14794 /*template_p=*/false,
14795 complain));
14798 case THROW_EXPR:
14799 RETURN (build_throw
14800 (RECUR (TREE_OPERAND (t, 0))));
14802 case CONSTRUCTOR:
14804 vec<constructor_elt, va_gc> *n;
14805 constructor_elt *ce;
14806 unsigned HOST_WIDE_INT idx;
14807 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14808 bool process_index_p;
14809 int newlen;
14810 bool need_copy_p = false;
14811 tree r;
14813 if (type == error_mark_node)
14814 RETURN (error_mark_node);
14816 /* digest_init will do the wrong thing if we let it. */
14817 if (type && TYPE_PTRMEMFUNC_P (type))
14818 RETURN (t);
14820 /* We do not want to process the index of aggregate
14821 initializers as they are identifier nodes which will be
14822 looked up by digest_init. */
14823 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14825 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14826 newlen = vec_safe_length (n);
14827 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14829 if (ce->index && process_index_p
14830 /* An identifier index is looked up in the type
14831 being initialized, not the current scope. */
14832 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14833 ce->index = RECUR (ce->index);
14835 if (PACK_EXPANSION_P (ce->value))
14837 /* Substitute into the pack expansion. */
14838 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14839 in_decl);
14841 if (ce->value == error_mark_node
14842 || PACK_EXPANSION_P (ce->value))
14844 else if (TREE_VEC_LENGTH (ce->value) == 1)
14845 /* Just move the argument into place. */
14846 ce->value = TREE_VEC_ELT (ce->value, 0);
14847 else
14849 /* Update the length of the final CONSTRUCTOR
14850 arguments vector, and note that we will need to
14851 copy.*/
14852 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14853 need_copy_p = true;
14856 else
14857 ce->value = RECUR (ce->value);
14860 if (need_copy_p)
14862 vec<constructor_elt, va_gc> *old_n = n;
14864 vec_alloc (n, newlen);
14865 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14867 if (TREE_CODE (ce->value) == TREE_VEC)
14869 int i, len = TREE_VEC_LENGTH (ce->value);
14870 for (i = 0; i < len; ++i)
14871 CONSTRUCTOR_APPEND_ELT (n, 0,
14872 TREE_VEC_ELT (ce->value, i));
14874 else
14875 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14879 r = build_constructor (init_list_type_node, n);
14880 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14882 if (TREE_HAS_CONSTRUCTOR (t))
14883 RETURN (finish_compound_literal (type, r, complain));
14885 TREE_TYPE (r) = type;
14886 RETURN (r);
14889 case TYPEID_EXPR:
14891 tree operand_0 = TREE_OPERAND (t, 0);
14892 if (TYPE_P (operand_0))
14894 operand_0 = tsubst (operand_0, args, complain, in_decl);
14895 RETURN (get_typeid (operand_0, complain));
14897 else
14899 operand_0 = RECUR (operand_0);
14900 RETURN (build_typeid (operand_0, complain));
14904 case VAR_DECL:
14905 if (!args)
14906 RETURN (t);
14907 else if (DECL_PACK_P (t))
14909 /* We don't build decls for an instantiation of a
14910 variadic capture proxy, we instantiate the elements
14911 when needed. */
14912 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14913 return RECUR (DECL_VALUE_EXPR (t));
14915 /* Fall through */
14917 case PARM_DECL:
14919 tree r = tsubst_copy (t, args, complain, in_decl);
14921 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14922 /* If the original type was a reference, we'll be wrapped in
14923 the appropriate INDIRECT_REF. */
14924 r = convert_from_reference (r);
14925 RETURN (r);
14928 case VA_ARG_EXPR:
14929 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14930 RECUR (TREE_OPERAND (t, 0)),
14931 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14933 case OFFSETOF_EXPR:
14934 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14936 case TRAIT_EXPR:
14938 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14939 complain, in_decl);
14941 tree type2 = TRAIT_EXPR_TYPE2 (t);
14942 if (type2)
14943 type2 = tsubst_copy (type2, args, complain, in_decl);
14945 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14948 case STMT_EXPR:
14950 tree old_stmt_expr = cur_stmt_expr;
14951 tree stmt_expr = begin_stmt_expr ();
14953 cur_stmt_expr = stmt_expr;
14954 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14955 integral_constant_expression_p);
14956 stmt_expr = finish_stmt_expr (stmt_expr, false);
14957 cur_stmt_expr = old_stmt_expr;
14959 /* If the resulting list of expression statement is empty,
14960 fold it further into void_zero_node. */
14961 if (empty_expr_stmt_p (stmt_expr))
14962 stmt_expr = void_zero_node;
14964 RETURN (stmt_expr);
14967 case LAMBDA_EXPR:
14969 tree r = build_lambda_expr ();
14971 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14972 LAMBDA_EXPR_CLOSURE (r) = type;
14973 CLASSTYPE_LAMBDA_EXPR (type) = r;
14975 LAMBDA_EXPR_LOCATION (r)
14976 = LAMBDA_EXPR_LOCATION (t);
14977 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14978 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14979 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14980 LAMBDA_EXPR_DISCRIMINATOR (r)
14981 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14982 /* For a function scope, we want to use tsubst so that we don't
14983 complain about referring to an auto function before its return
14984 type has been deduced. Otherwise, we want to use tsubst_copy so
14985 that we look up the existing field/parameter/variable rather
14986 than build a new one. */
14987 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14988 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14989 scope = tsubst (scope, args, complain, in_decl);
14990 else if (scope && TREE_CODE (scope) == PARM_DECL)
14992 /* Look up the parameter we want directly, as tsubst_copy
14993 doesn't do what we need. */
14994 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
14995 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14996 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
14997 parm = DECL_CHAIN (parm);
14998 scope = parm;
14999 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15000 if (DECL_CONTEXT (scope) == NULL_TREE)
15001 DECL_CONTEXT (scope) = fn;
15003 else
15004 scope = RECUR (scope);
15005 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15006 LAMBDA_EXPR_RETURN_TYPE (r)
15007 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15009 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15010 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15012 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15013 determine_visibility (TYPE_NAME (type));
15014 /* Now that we know visibility, instantiate the type so we have a
15015 declaration of the op() for later calls to lambda_function. */
15016 complete_type (type);
15018 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15020 RETURN (build_lambda_object (r));
15023 case TARGET_EXPR:
15024 /* We can get here for a constant initializer of non-dependent type.
15025 FIXME stop folding in cp_parser_initializer_clause. */
15027 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15028 complain);
15029 RETURN (r);
15032 case TRANSACTION_EXPR:
15033 RETURN (tsubst_expr(t, args, complain, in_decl,
15034 integral_constant_expression_p));
15036 case PAREN_EXPR:
15037 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15039 case VEC_PERM_EXPR:
15040 RETURN (build_x_vec_perm_expr (input_location,
15041 RECUR (TREE_OPERAND (t, 0)),
15042 RECUR (TREE_OPERAND (t, 1)),
15043 RECUR (TREE_OPERAND (t, 2)),
15044 complain));
15046 default:
15047 /* Handle Objective-C++ constructs, if appropriate. */
15049 tree subst
15050 = objcp_tsubst_copy_and_build (t, args, complain,
15051 in_decl, /*function_p=*/false);
15052 if (subst)
15053 RETURN (subst);
15055 RETURN (tsubst_copy (t, args, complain, in_decl));
15058 #undef RECUR
15059 #undef RETURN
15060 out:
15061 input_location = loc;
15062 return retval;
15065 /* Verify that the instantiated ARGS are valid. For type arguments,
15066 make sure that the type's linkage is ok. For non-type arguments,
15067 make sure they are constants if they are integral or enumerations.
15068 Emit an error under control of COMPLAIN, and return TRUE on error. */
15070 static bool
15071 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15073 if (dependent_template_arg_p (t))
15074 return false;
15075 if (ARGUMENT_PACK_P (t))
15077 tree vec = ARGUMENT_PACK_ARGS (t);
15078 int len = TREE_VEC_LENGTH (vec);
15079 bool result = false;
15080 int i;
15082 for (i = 0; i < len; ++i)
15083 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15084 result = true;
15085 return result;
15087 else if (TYPE_P (t))
15089 /* [basic.link]: A name with no linkage (notably, the name
15090 of a class or enumeration declared in a local scope)
15091 shall not be used to declare an entity with linkage.
15092 This implies that names with no linkage cannot be used as
15093 template arguments
15095 DR 757 relaxes this restriction for C++0x. */
15096 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15097 : no_linkage_check (t, /*relaxed_p=*/false));
15099 if (nt)
15101 /* DR 488 makes use of a type with no linkage cause
15102 type deduction to fail. */
15103 if (complain & tf_error)
15105 if (TYPE_ANONYMOUS_P (nt))
15106 error ("%qT is/uses anonymous type", t);
15107 else
15108 error ("template argument for %qD uses local type %qT",
15109 tmpl, t);
15111 return true;
15113 /* In order to avoid all sorts of complications, we do not
15114 allow variably-modified types as template arguments. */
15115 else if (variably_modified_type_p (t, NULL_TREE))
15117 if (complain & tf_error)
15118 error ("%qT is a variably modified type", t);
15119 return true;
15122 /* Class template and alias template arguments should be OK. */
15123 else if (DECL_TYPE_TEMPLATE_P (t))
15125 /* A non-type argument of integral or enumerated type must be a
15126 constant. */
15127 else if (TREE_TYPE (t)
15128 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15129 && !TREE_CONSTANT (t))
15131 if (complain & tf_error)
15132 error ("integral expression %qE is not constant", t);
15133 return true;
15135 return false;
15138 static bool
15139 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15141 int ix, len = DECL_NTPARMS (tmpl);
15142 bool result = false;
15144 for (ix = 0; ix != len; ix++)
15146 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15147 result = true;
15149 if (result && (complain & tf_error))
15150 error (" trying to instantiate %qD", tmpl);
15151 return result;
15154 /* We're out of SFINAE context now, so generate diagnostics for the access
15155 errors we saw earlier when instantiating D from TMPL and ARGS. */
15157 static void
15158 recheck_decl_substitution (tree d, tree tmpl, tree args)
15160 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15161 tree type = TREE_TYPE (pattern);
15162 location_t loc = input_location;
15164 push_access_scope (d);
15165 push_deferring_access_checks (dk_no_deferred);
15166 input_location = DECL_SOURCE_LOCATION (pattern);
15167 tsubst (type, args, tf_warning_or_error, d);
15168 input_location = loc;
15169 pop_deferring_access_checks ();
15170 pop_access_scope (d);
15173 /* Instantiate the indicated variable, function, or alias template TMPL with
15174 the template arguments in TARG_PTR. */
15176 static tree
15177 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15179 tree targ_ptr = orig_args;
15180 tree fndecl;
15181 tree gen_tmpl;
15182 tree spec;
15183 bool access_ok = true;
15185 if (tmpl == error_mark_node)
15186 return error_mark_node;
15188 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15190 /* If this function is a clone, handle it specially. */
15191 if (DECL_CLONED_FUNCTION_P (tmpl))
15193 tree spec;
15194 tree clone;
15196 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15197 DECL_CLONED_FUNCTION. */
15198 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15199 targ_ptr, complain);
15200 if (spec == error_mark_node)
15201 return error_mark_node;
15203 /* Look for the clone. */
15204 FOR_EACH_CLONE (clone, spec)
15205 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15206 return clone;
15207 /* We should always have found the clone by now. */
15208 gcc_unreachable ();
15209 return NULL_TREE;
15212 /* Check to see if we already have this specialization. */
15213 gen_tmpl = most_general_template (tmpl);
15214 if (tmpl != gen_tmpl)
15215 /* The TMPL is a partial instantiation. To get a full set of
15216 arguments we must add the arguments used to perform the
15217 partial instantiation. */
15218 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15219 targ_ptr);
15221 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15222 but it doesn't seem to be on the hot path. */
15223 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15225 gcc_assert (tmpl == gen_tmpl
15226 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15227 == spec)
15228 || fndecl == NULL_TREE);
15230 if (spec != NULL_TREE)
15232 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15234 if (complain & tf_error)
15235 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15236 return error_mark_node;
15238 return spec;
15241 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15242 complain))
15243 return error_mark_node;
15245 /* We are building a FUNCTION_DECL, during which the access of its
15246 parameters and return types have to be checked. However this
15247 FUNCTION_DECL which is the desired context for access checking
15248 is not built yet. We solve this chicken-and-egg problem by
15249 deferring all checks until we have the FUNCTION_DECL. */
15250 push_deferring_access_checks (dk_deferred);
15252 /* Instantiation of the function happens in the context of the function
15253 template, not the context of the overload resolution we're doing. */
15254 push_to_top_level ();
15255 /* If there are dependent arguments, e.g. because we're doing partial
15256 ordering, make sure processing_template_decl stays set. */
15257 if (uses_template_parms (targ_ptr))
15258 ++processing_template_decl;
15259 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15261 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15262 complain, gen_tmpl);
15263 push_nested_class (ctx);
15265 /* Substitute template parameters to obtain the specialization. */
15266 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15267 targ_ptr, complain, gen_tmpl);
15268 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15269 pop_nested_class ();
15270 pop_from_top_level ();
15272 if (fndecl == error_mark_node)
15274 pop_deferring_access_checks ();
15275 return error_mark_node;
15278 /* The DECL_TI_TEMPLATE should always be the immediate parent
15279 template, not the most general template. */
15280 DECL_TI_TEMPLATE (fndecl) = tmpl;
15282 /* Now we know the specialization, compute access previously
15283 deferred. */
15284 push_access_scope (fndecl);
15285 if (!perform_deferred_access_checks (complain))
15286 access_ok = false;
15287 pop_access_scope (fndecl);
15288 pop_deferring_access_checks ();
15290 /* If we've just instantiated the main entry point for a function,
15291 instantiate all the alternate entry points as well. We do this
15292 by cloning the instantiation of the main entry point, not by
15293 instantiating the template clones. */
15294 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15295 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15297 if (!access_ok)
15299 if (!(complain & tf_error))
15301 /* Remember to reinstantiate when we're out of SFINAE so the user
15302 can see the errors. */
15303 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15305 return error_mark_node;
15307 return fndecl;
15310 /* Wrapper for instantiate_template_1. */
15312 tree
15313 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15315 tree ret;
15316 timevar_push (TV_TEMPLATE_INST);
15317 ret = instantiate_template_1 (tmpl, orig_args, complain);
15318 timevar_pop (TV_TEMPLATE_INST);
15319 return ret;
15322 /* Instantiate the alias template TMPL with ARGS. Also push a template
15323 instantiation level, which instantiate_template doesn't do because
15324 functions and variables have sufficient context established by the
15325 callers. */
15327 static tree
15328 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15330 struct pending_template *old_last_pend = last_pending_template;
15331 struct tinst_level *old_error_tinst = last_error_tinst_level;
15332 if (tmpl == error_mark_node || args == error_mark_node)
15333 return error_mark_node;
15334 tree tinst = build_tree_list (tmpl, args);
15335 if (!push_tinst_level (tinst))
15337 ggc_free (tinst);
15338 return error_mark_node;
15341 args =
15342 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15343 args, tmpl, complain,
15344 /*require_all_args=*/true,
15345 /*use_default_args=*/true);
15347 tree r = instantiate_template (tmpl, args, complain);
15348 pop_tinst_level ();
15349 /* We can't free this if a pending_template entry or last_error_tinst_level
15350 is pointing at it. */
15351 if (last_pending_template == old_last_pend
15352 && last_error_tinst_level == old_error_tinst)
15353 ggc_free (tinst);
15355 return r;
15358 /* PARM is a template parameter pack for FN. Returns true iff
15359 PARM is used in a deducible way in the argument list of FN. */
15361 static bool
15362 pack_deducible_p (tree parm, tree fn)
15364 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15365 for (; t; t = TREE_CHAIN (t))
15367 tree type = TREE_VALUE (t);
15368 tree packs;
15369 if (!PACK_EXPANSION_P (type))
15370 continue;
15371 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15372 packs; packs = TREE_CHAIN (packs))
15373 if (TREE_VALUE (packs) == parm)
15375 /* The template parameter pack is used in a function parameter
15376 pack. If this is the end of the parameter list, the
15377 template parameter pack is deducible. */
15378 if (TREE_CHAIN (t) == void_list_node)
15379 return true;
15380 else
15381 /* Otherwise, not. Well, it could be deduced from
15382 a non-pack parameter, but doing so would end up with
15383 a deduction mismatch, so don't bother. */
15384 return false;
15387 /* The template parameter pack isn't used in any function parameter
15388 packs, but it might be used deeper, e.g. tuple<Args...>. */
15389 return true;
15392 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15393 NARGS elements of the arguments that are being used when calling
15394 it. TARGS is a vector into which the deduced template arguments
15395 are placed.
15397 Return zero for success, 2 for an incomplete match that doesn't resolve
15398 all the types, and 1 for complete failure. An error message will be
15399 printed only for an incomplete match.
15401 If FN is a conversion operator, or we are trying to produce a specific
15402 specialization, RETURN_TYPE is the return type desired.
15404 The EXPLICIT_TARGS are explicit template arguments provided via a
15405 template-id.
15407 The parameter STRICT is one of:
15409 DEDUCE_CALL:
15410 We are deducing arguments for a function call, as in
15411 [temp.deduct.call].
15413 DEDUCE_CONV:
15414 We are deducing arguments for a conversion function, as in
15415 [temp.deduct.conv].
15417 DEDUCE_EXACT:
15418 We are deducing arguments when doing an explicit instantiation
15419 as in [temp.explicit], when determining an explicit specialization
15420 as in [temp.expl.spec], or when taking the address of a function
15421 template, as in [temp.deduct.funcaddr]. */
15423 tree
15424 fn_type_unification (tree fn,
15425 tree explicit_targs,
15426 tree targs,
15427 const tree *args,
15428 unsigned int nargs,
15429 tree return_type,
15430 unification_kind_t strict,
15431 int flags,
15432 bool explain_p,
15433 bool decltype_p)
15435 tree parms;
15436 tree fntype;
15437 tree decl = NULL_TREE;
15438 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15439 bool ok;
15440 static int deduction_depth;
15441 struct pending_template *old_last_pend = last_pending_template;
15442 struct tinst_level *old_error_tinst = last_error_tinst_level;
15443 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15444 tree tinst;
15445 tree r = error_mark_node;
15447 if (decltype_p)
15448 complain |= tf_decltype;
15450 /* In C++0x, it's possible to have a function template whose type depends
15451 on itself recursively. This is most obvious with decltype, but can also
15452 occur with enumeration scope (c++/48969). So we need to catch infinite
15453 recursion and reject the substitution at deduction time; this function
15454 will return error_mark_node for any repeated substitution.
15456 This also catches excessive recursion such as when f<N> depends on
15457 f<N-1> across all integers, and returns error_mark_node for all the
15458 substitutions back up to the initial one.
15460 This is, of course, not reentrant. */
15461 if (excessive_deduction_depth)
15462 return error_mark_node;
15463 tinst = build_tree_list (fn, NULL_TREE);
15464 ++deduction_depth;
15466 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15468 fntype = TREE_TYPE (fn);
15469 if (explicit_targs)
15471 /* [temp.deduct]
15473 The specified template arguments must match the template
15474 parameters in kind (i.e., type, nontype, template), and there
15475 must not be more arguments than there are parameters;
15476 otherwise type deduction fails.
15478 Nontype arguments must match the types of the corresponding
15479 nontype template parameters, or must be convertible to the
15480 types of the corresponding nontype parameters as specified in
15481 _temp.arg.nontype_, otherwise type deduction fails.
15483 All references in the function type of the function template
15484 to the corresponding template parameters are replaced by the
15485 specified template argument values. If a substitution in a
15486 template parameter or in the function type of the function
15487 template results in an invalid type, type deduction fails. */
15488 int i, len = TREE_VEC_LENGTH (tparms);
15489 location_t loc = input_location;
15490 bool incomplete = false;
15492 /* Adjust any explicit template arguments before entering the
15493 substitution context. */
15494 explicit_targs
15495 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15496 complain,
15497 /*require_all_args=*/false,
15498 /*use_default_args=*/false));
15499 if (explicit_targs == error_mark_node)
15500 goto fail;
15502 /* Substitute the explicit args into the function type. This is
15503 necessary so that, for instance, explicitly declared function
15504 arguments can match null pointed constants. If we were given
15505 an incomplete set of explicit args, we must not do semantic
15506 processing during substitution as we could create partial
15507 instantiations. */
15508 for (i = 0; i < len; i++)
15510 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15511 bool parameter_pack = false;
15512 tree targ = TREE_VEC_ELT (explicit_targs, i);
15514 /* Dig out the actual parm. */
15515 if (TREE_CODE (parm) == TYPE_DECL
15516 || TREE_CODE (parm) == TEMPLATE_DECL)
15518 parm = TREE_TYPE (parm);
15519 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15521 else if (TREE_CODE (parm) == PARM_DECL)
15523 parm = DECL_INITIAL (parm);
15524 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15527 if (!parameter_pack && targ == NULL_TREE)
15528 /* No explicit argument for this template parameter. */
15529 incomplete = true;
15531 if (parameter_pack && pack_deducible_p (parm, fn))
15533 /* Mark the argument pack as "incomplete". We could
15534 still deduce more arguments during unification.
15535 We remove this mark in type_unification_real. */
15536 if (targ)
15538 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15539 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15540 = ARGUMENT_PACK_ARGS (targ);
15543 /* We have some incomplete argument packs. */
15544 incomplete = true;
15548 TREE_VALUE (tinst) = explicit_targs;
15549 if (!push_tinst_level (tinst))
15551 excessive_deduction_depth = true;
15552 goto fail;
15554 processing_template_decl += incomplete;
15555 input_location = DECL_SOURCE_LOCATION (fn);
15556 /* Ignore any access checks; we'll see them again in
15557 instantiate_template and they might have the wrong
15558 access path at this point. */
15559 push_deferring_access_checks (dk_deferred);
15560 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15561 complain | tf_partial, NULL_TREE);
15562 pop_deferring_access_checks ();
15563 input_location = loc;
15564 processing_template_decl -= incomplete;
15565 pop_tinst_level ();
15567 if (fntype == error_mark_node)
15568 goto fail;
15570 /* Place the explicitly specified arguments in TARGS. */
15571 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15572 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15575 /* Never do unification on the 'this' parameter. */
15576 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15578 if (return_type)
15580 tree *new_args;
15582 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15583 new_args = XALLOCAVEC (tree, nargs + 1);
15584 new_args[0] = return_type;
15585 memcpy (new_args + 1, args, nargs * sizeof (tree));
15586 args = new_args;
15587 ++nargs;
15590 /* We allow incomplete unification without an error message here
15591 because the standard doesn't seem to explicitly prohibit it. Our
15592 callers must be ready to deal with unification failures in any
15593 event. */
15595 TREE_VALUE (tinst) = targs;
15596 /* If we aren't explaining yet, push tinst context so we can see where
15597 any errors (e.g. from class instantiations triggered by instantiation
15598 of default template arguments) come from. If we are explaining, this
15599 context is redundant. */
15600 if (!explain_p && !push_tinst_level (tinst))
15602 excessive_deduction_depth = true;
15603 goto fail;
15606 /* type_unification_real will pass back any access checks from default
15607 template argument substitution. */
15608 vec<deferred_access_check, va_gc> *checks;
15609 checks = NULL;
15611 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15612 targs, parms, args, nargs, /*subr=*/0,
15613 strict, flags, &checks, explain_p);
15614 if (!explain_p)
15615 pop_tinst_level ();
15616 if (!ok)
15617 goto fail;
15619 /* Now that we have bindings for all of the template arguments,
15620 ensure that the arguments deduced for the template template
15621 parameters have compatible template parameter lists. We cannot
15622 check this property before we have deduced all template
15623 arguments, because the template parameter types of a template
15624 template parameter might depend on prior template parameters
15625 deduced after the template template parameter. The following
15626 ill-formed example illustrates this issue:
15628 template<typename T, template<T> class C> void f(C<5>, T);
15630 template<int N> struct X {};
15632 void g() {
15633 f(X<5>(), 5l); // error: template argument deduction fails
15636 The template parameter list of 'C' depends on the template type
15637 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15638 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15639 time that we deduce 'C'. */
15640 if (!template_template_parm_bindings_ok_p
15641 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15643 unify_inconsistent_template_template_parameters (explain_p);
15644 goto fail;
15647 /* All is well so far. Now, check:
15649 [temp.deduct]
15651 When all template arguments have been deduced, all uses of
15652 template parameters in nondeduced contexts are replaced with
15653 the corresponding deduced argument values. If the
15654 substitution results in an invalid type, as described above,
15655 type deduction fails. */
15656 TREE_VALUE (tinst) = targs;
15657 if (!push_tinst_level (tinst))
15659 excessive_deduction_depth = true;
15660 goto fail;
15663 /* Also collect access checks from the instantiation. */
15664 reopen_deferring_access_checks (checks);
15666 decl = instantiate_template (fn, targs, complain);
15668 checks = get_deferred_access_checks ();
15669 pop_deferring_access_checks ();
15671 pop_tinst_level ();
15673 if (decl == error_mark_node)
15674 goto fail;
15676 /* Now perform any access checks encountered during substitution. */
15677 push_access_scope (decl);
15678 ok = perform_access_checks (checks, complain);
15679 pop_access_scope (decl);
15680 if (!ok)
15681 goto fail;
15683 /* If we're looking for an exact match, check that what we got
15684 is indeed an exact match. It might not be if some template
15685 parameters are used in non-deduced contexts. */
15686 if (strict == DEDUCE_EXACT)
15688 tree substed = TREE_TYPE (decl);
15689 unsigned int i;
15691 tree sarg
15692 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15693 if (return_type)
15694 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15695 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15696 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15698 unify_type_mismatch (explain_p, args[i],
15699 TREE_VALUE (sarg));
15700 goto fail;
15704 r = decl;
15706 fail:
15707 --deduction_depth;
15708 if (excessive_deduction_depth)
15710 if (deduction_depth == 0)
15711 /* Reset once we're all the way out. */
15712 excessive_deduction_depth = false;
15715 /* We can't free this if a pending_template entry or last_error_tinst_level
15716 is pointing at it. */
15717 if (last_pending_template == old_last_pend
15718 && last_error_tinst_level == old_error_tinst)
15719 ggc_free (tinst);
15721 return r;
15724 /* Adjust types before performing type deduction, as described in
15725 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15726 sections are symmetric. PARM is the type of a function parameter
15727 or the return type of the conversion function. ARG is the type of
15728 the argument passed to the call, or the type of the value
15729 initialized with the result of the conversion function.
15730 ARG_EXPR is the original argument expression, which may be null. */
15732 static int
15733 maybe_adjust_types_for_deduction (unification_kind_t strict,
15734 tree* parm,
15735 tree* arg,
15736 tree arg_expr)
15738 int result = 0;
15740 switch (strict)
15742 case DEDUCE_CALL:
15743 break;
15745 case DEDUCE_CONV:
15747 /* Swap PARM and ARG throughout the remainder of this
15748 function; the handling is precisely symmetric since PARM
15749 will initialize ARG rather than vice versa. */
15750 tree* temp = parm;
15751 parm = arg;
15752 arg = temp;
15753 break;
15756 case DEDUCE_EXACT:
15757 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15758 too, but here handle it by stripping the reference from PARM
15759 rather than by adding it to ARG. */
15760 if (TREE_CODE (*parm) == REFERENCE_TYPE
15761 && TYPE_REF_IS_RVALUE (*parm)
15762 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15763 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15764 && TREE_CODE (*arg) == REFERENCE_TYPE
15765 && !TYPE_REF_IS_RVALUE (*arg))
15766 *parm = TREE_TYPE (*parm);
15767 /* Nothing else to do in this case. */
15768 return 0;
15770 default:
15771 gcc_unreachable ();
15774 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15776 /* [temp.deduct.call]
15778 If P is not a reference type:
15780 --If A is an array type, the pointer type produced by the
15781 array-to-pointer standard conversion (_conv.array_) is
15782 used in place of A for type deduction; otherwise,
15784 --If A is a function type, the pointer type produced by
15785 the function-to-pointer standard conversion
15786 (_conv.func_) is used in place of A for type deduction;
15787 otherwise,
15789 --If A is a cv-qualified type, the top level
15790 cv-qualifiers of A's type are ignored for type
15791 deduction. */
15792 if (TREE_CODE (*arg) == ARRAY_TYPE)
15793 *arg = build_pointer_type (TREE_TYPE (*arg));
15794 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15795 *arg = build_pointer_type (*arg);
15796 else
15797 *arg = TYPE_MAIN_VARIANT (*arg);
15800 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15801 of the form T&&, where T is a template parameter, and the argument
15802 is an lvalue, T is deduced as A& */
15803 if (TREE_CODE (*parm) == REFERENCE_TYPE
15804 && TYPE_REF_IS_RVALUE (*parm)
15805 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15806 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15807 && (arg_expr ? real_lvalue_p (arg_expr)
15808 /* try_one_overload doesn't provide an arg_expr, but
15809 functions are always lvalues. */
15810 : TREE_CODE (*arg) == FUNCTION_TYPE))
15811 *arg = build_reference_type (*arg);
15813 /* [temp.deduct.call]
15815 If P is a cv-qualified type, the top level cv-qualifiers
15816 of P's type are ignored for type deduction. If P is a
15817 reference type, the type referred to by P is used for
15818 type deduction. */
15819 *parm = TYPE_MAIN_VARIANT (*parm);
15820 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15822 *parm = TREE_TYPE (*parm);
15823 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15826 /* DR 322. For conversion deduction, remove a reference type on parm
15827 too (which has been swapped into ARG). */
15828 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15829 *arg = TREE_TYPE (*arg);
15831 return result;
15834 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15835 template which does contain any deducible template parameters; check if
15836 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15837 unify_one_argument. */
15839 static int
15840 check_non_deducible_conversion (tree parm, tree arg, int strict,
15841 int flags, bool explain_p)
15843 tree type;
15845 if (!TYPE_P (arg))
15846 type = TREE_TYPE (arg);
15847 else
15848 type = arg;
15850 if (same_type_p (parm, type))
15851 return unify_success (explain_p);
15853 if (strict == DEDUCE_CONV)
15855 if (can_convert_arg (type, parm, NULL_TREE, flags,
15856 explain_p ? tf_warning_or_error : tf_none))
15857 return unify_success (explain_p);
15859 else if (strict != DEDUCE_EXACT)
15861 if (can_convert_arg (parm, type,
15862 TYPE_P (arg) ? NULL_TREE : arg,
15863 flags, explain_p ? tf_warning_or_error : tf_none))
15864 return unify_success (explain_p);
15867 if (strict == DEDUCE_EXACT)
15868 return unify_type_mismatch (explain_p, parm, arg);
15869 else
15870 return unify_arg_conversion (explain_p, parm, type, arg);
15873 static bool uses_deducible_template_parms (tree type);
15875 /* Returns true iff the expression EXPR is one from which a template
15876 argument can be deduced. In other words, if it's an undecorated
15877 use of a template non-type parameter. */
15879 static bool
15880 deducible_expression (tree expr)
15882 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15885 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15886 deducible way; that is, if it has a max value of <PARM> - 1. */
15888 static bool
15889 deducible_array_bound (tree domain)
15891 if (domain == NULL_TREE)
15892 return false;
15894 tree max = TYPE_MAX_VALUE (domain);
15895 if (TREE_CODE (max) != MINUS_EXPR)
15896 return false;
15898 return deducible_expression (TREE_OPERAND (max, 0));
15901 /* Returns true iff the template arguments ARGS use a template parameter
15902 in a deducible way. */
15904 static bool
15905 deducible_template_args (tree args)
15907 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15909 bool deducible;
15910 tree elt = TREE_VEC_ELT (args, i);
15911 if (ARGUMENT_PACK_P (elt))
15912 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15913 else
15915 if (PACK_EXPANSION_P (elt))
15916 elt = PACK_EXPANSION_PATTERN (elt);
15917 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15918 deducible = true;
15919 else if (TYPE_P (elt))
15920 deducible = uses_deducible_template_parms (elt);
15921 else
15922 deducible = deducible_expression (elt);
15924 if (deducible)
15925 return true;
15927 return false;
15930 /* Returns true iff TYPE contains any deducible references to template
15931 parameters, as per 14.8.2.5. */
15933 static bool
15934 uses_deducible_template_parms (tree type)
15936 if (PACK_EXPANSION_P (type))
15937 type = PACK_EXPANSION_PATTERN (type);
15939 /* T
15940 cv-list T
15941 TT<T>
15942 TT<i>
15943 TT<> */
15944 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15945 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15946 return true;
15948 /* T*
15950 T&& */
15951 if (POINTER_TYPE_P (type))
15952 return uses_deducible_template_parms (TREE_TYPE (type));
15954 /* T[integer-constant ]
15955 type [i] */
15956 if (TREE_CODE (type) == ARRAY_TYPE)
15957 return (uses_deducible_template_parms (TREE_TYPE (type))
15958 || deducible_array_bound (TYPE_DOMAIN (type)));
15960 /* T type ::*
15961 type T::*
15962 T T::*
15963 T (type ::*)()
15964 type (T::*)()
15965 type (type ::*)(T)
15966 type (T::*)(T)
15967 T (type ::*)(T)
15968 T (T::*)()
15969 T (T::*)(T) */
15970 if (TYPE_PTRMEM_P (type))
15971 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
15972 || (uses_deducible_template_parms
15973 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
15975 /* template-name <T> (where template-name refers to a class template)
15976 template-name <i> (where template-name refers to a class template) */
15977 if (CLASS_TYPE_P (type)
15978 && CLASSTYPE_TEMPLATE_INFO (type)
15979 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
15980 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15981 (CLASSTYPE_TI_ARGS (type)));
15983 /* type (T)
15985 T(T) */
15986 if (TREE_CODE (type) == FUNCTION_TYPE
15987 || TREE_CODE (type) == METHOD_TYPE)
15989 if (uses_deducible_template_parms (TREE_TYPE (type)))
15990 return true;
15991 tree parm = TYPE_ARG_TYPES (type);
15992 if (TREE_CODE (type) == METHOD_TYPE)
15993 parm = TREE_CHAIN (parm);
15994 for (; parm; parm = TREE_CHAIN (parm))
15995 if (uses_deducible_template_parms (TREE_VALUE (parm)))
15996 return true;
15999 return false;
16002 /* Subroutine of type_unification_real and unify_pack_expansion to
16003 handle unification of a single P/A pair. Parameters are as
16004 for those functions. */
16006 static int
16007 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16008 int subr, unification_kind_t strict, int flags,
16009 bool explain_p)
16011 tree arg_expr = NULL_TREE;
16012 int arg_strict;
16014 if (arg == error_mark_node || parm == error_mark_node)
16015 return unify_invalid (explain_p);
16016 if (arg == unknown_type_node)
16017 /* We can't deduce anything from this, but we might get all the
16018 template args from other function args. */
16019 return unify_success (explain_p);
16021 /* Implicit conversions (Clause 4) will be performed on a function
16022 argument to convert it to the type of the corresponding function
16023 parameter if the parameter type contains no template-parameters that
16024 participate in template argument deduction. */
16025 if (TYPE_P (parm) && !uses_template_parms (parm))
16026 /* For function parameters that contain no template-parameters at all,
16027 we have historically checked for convertibility in order to shortcut
16028 consideration of this candidate. */
16029 return check_non_deducible_conversion (parm, arg, strict, flags,
16030 explain_p);
16031 else if (strict == DEDUCE_CALL
16032 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16033 /* For function parameters with only non-deducible template parameters,
16034 just return. */
16035 return unify_success (explain_p);
16037 switch (strict)
16039 case DEDUCE_CALL:
16040 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16041 | UNIFY_ALLOW_MORE_CV_QUAL
16042 | UNIFY_ALLOW_DERIVED);
16043 break;
16045 case DEDUCE_CONV:
16046 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16047 break;
16049 case DEDUCE_EXACT:
16050 arg_strict = UNIFY_ALLOW_NONE;
16051 break;
16053 default:
16054 gcc_unreachable ();
16057 /* We only do these transformations if this is the top-level
16058 parameter_type_list in a call or declaration matching; in other
16059 situations (nested function declarators, template argument lists) we
16060 won't be comparing a type to an expression, and we don't do any type
16061 adjustments. */
16062 if (!subr)
16064 if (!TYPE_P (arg))
16066 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16067 if (type_unknown_p (arg))
16069 /* [temp.deduct.type] A template-argument can be
16070 deduced from a pointer to function or pointer
16071 to member function argument if the set of
16072 overloaded functions does not contain function
16073 templates and at most one of a set of
16074 overloaded functions provides a unique
16075 match. */
16077 if (resolve_overloaded_unification
16078 (tparms, targs, parm, arg, strict,
16079 arg_strict, explain_p))
16080 return unify_success (explain_p);
16081 return unify_overload_resolution_failure (explain_p, arg);
16084 arg_expr = arg;
16085 arg = unlowered_expr_type (arg);
16086 if (arg == error_mark_node)
16087 return unify_invalid (explain_p);
16090 arg_strict |=
16091 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16093 else
16094 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16095 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16097 /* For deduction from an init-list we need the actual list. */
16098 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16099 arg = arg_expr;
16100 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16103 /* Most parms like fn_type_unification.
16105 If SUBR is 1, we're being called recursively (to unify the
16106 arguments of a function or method parameter of a function
16107 template).
16109 CHECKS is a pointer to a vector of access checks encountered while
16110 substituting default template arguments. */
16112 static int
16113 type_unification_real (tree tparms,
16114 tree targs,
16115 tree xparms,
16116 const tree *xargs,
16117 unsigned int xnargs,
16118 int subr,
16119 unification_kind_t strict,
16120 int flags,
16121 vec<deferred_access_check, va_gc> **checks,
16122 bool explain_p)
16124 tree parm, arg;
16125 int i;
16126 int ntparms = TREE_VEC_LENGTH (tparms);
16127 int saw_undeduced = 0;
16128 tree parms;
16129 const tree *args;
16130 unsigned int nargs;
16131 unsigned int ia;
16133 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16134 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16135 gcc_assert (ntparms > 0);
16137 /* Reset the number of non-defaulted template arguments contained
16138 in TARGS. */
16139 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16141 again:
16142 parms = xparms;
16143 args = xargs;
16144 nargs = xnargs;
16146 ia = 0;
16147 while (parms && parms != void_list_node
16148 && ia < nargs)
16150 parm = TREE_VALUE (parms);
16152 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16153 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16154 /* For a function parameter pack that occurs at the end of the
16155 parameter-declaration-list, the type A of each remaining
16156 argument of the call is compared with the type P of the
16157 declarator-id of the function parameter pack. */
16158 break;
16160 parms = TREE_CHAIN (parms);
16162 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16163 /* For a function parameter pack that does not occur at the
16164 end of the parameter-declaration-list, the type of the
16165 parameter pack is a non-deduced context. */
16166 continue;
16168 arg = args[ia];
16169 ++ia;
16171 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16172 flags, explain_p))
16173 return 1;
16176 if (parms
16177 && parms != void_list_node
16178 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16180 /* Unify the remaining arguments with the pack expansion type. */
16181 tree argvec;
16182 tree parmvec = make_tree_vec (1);
16184 /* Allocate a TREE_VEC and copy in all of the arguments */
16185 argvec = make_tree_vec (nargs - ia);
16186 for (i = 0; ia < nargs; ++ia, ++i)
16187 TREE_VEC_ELT (argvec, i) = args[ia];
16189 /* Copy the parameter into parmvec. */
16190 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16191 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16192 /*subr=*/subr, explain_p))
16193 return 1;
16195 /* Advance to the end of the list of parameters. */
16196 parms = TREE_CHAIN (parms);
16199 /* Fail if we've reached the end of the parm list, and more args
16200 are present, and the parm list isn't variadic. */
16201 if (ia < nargs && parms == void_list_node)
16202 return unify_too_many_arguments (explain_p, nargs, ia);
16203 /* Fail if parms are left and they don't have default values. */
16204 if (parms && parms != void_list_node
16205 && TREE_PURPOSE (parms) == NULL_TREE)
16207 unsigned int count = nargs;
16208 tree p = parms;
16209 while (p && p != void_list_node)
16211 count++;
16212 p = TREE_CHAIN (p);
16214 return unify_too_few_arguments (explain_p, ia, count);
16217 if (!subr)
16219 tsubst_flags_t complain = (explain_p
16220 ? tf_warning_or_error
16221 : tf_none);
16223 for (i = 0; i < ntparms; i++)
16225 tree targ = TREE_VEC_ELT (targs, i);
16226 tree tparm = TREE_VEC_ELT (tparms, i);
16228 /* Clear the "incomplete" flags on all argument packs now so that
16229 substituting them into later default arguments works. */
16230 if (targ && ARGUMENT_PACK_P (targ))
16232 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16233 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16236 if (targ || tparm == error_mark_node)
16237 continue;
16238 tparm = TREE_VALUE (tparm);
16240 /* If this is an undeduced nontype parameter that depends on
16241 a type parameter, try another pass; its type may have been
16242 deduced from a later argument than the one from which
16243 this parameter can be deduced. */
16244 if (TREE_CODE (tparm) == PARM_DECL
16245 && uses_template_parms (TREE_TYPE (tparm))
16246 && !saw_undeduced++)
16247 goto again;
16249 /* Core issue #226 (C++0x) [temp.deduct]:
16251 If a template argument has not been deduced, its
16252 default template argument, if any, is used.
16254 When we are in C++98 mode, TREE_PURPOSE will either
16255 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16256 to explicitly check cxx_dialect here. */
16257 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16259 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16260 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16261 reopen_deferring_access_checks (*checks);
16262 location_t save_loc = input_location;
16263 if (DECL_P (parm))
16264 input_location = DECL_SOURCE_LOCATION (parm);
16265 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16266 arg = convert_template_argument (parm, arg, targs, complain,
16267 i, NULL_TREE);
16268 input_location = save_loc;
16269 *checks = get_deferred_access_checks ();
16270 pop_deferring_access_checks ();
16271 if (arg == error_mark_node)
16272 return 1;
16273 else
16275 TREE_VEC_ELT (targs, i) = arg;
16276 /* The position of the first default template argument,
16277 is also the number of non-defaulted arguments in TARGS.
16278 Record that. */
16279 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16280 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16281 continue;
16285 /* If the type parameter is a parameter pack, then it will
16286 be deduced to an empty parameter pack. */
16287 if (template_parameter_pack_p (tparm))
16289 tree arg;
16291 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16293 arg = make_node (NONTYPE_ARGUMENT_PACK);
16294 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16295 TREE_CONSTANT (arg) = 1;
16297 else
16298 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16300 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16302 TREE_VEC_ELT (targs, i) = arg;
16303 continue;
16306 return unify_parameter_deduction_failure (explain_p, tparm);
16309 #ifdef ENABLE_CHECKING
16310 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16311 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16312 #endif
16314 return unify_success (explain_p);
16317 /* Subroutine of type_unification_real. Args are like the variables
16318 at the call site. ARG is an overloaded function (or template-id);
16319 we try deducing template args from each of the overloads, and if
16320 only one succeeds, we go with that. Modifies TARGS and returns
16321 true on success. */
16323 static bool
16324 resolve_overloaded_unification (tree tparms,
16325 tree targs,
16326 tree parm,
16327 tree arg,
16328 unification_kind_t strict,
16329 int sub_strict,
16330 bool explain_p)
16332 tree tempargs = copy_node (targs);
16333 int good = 0;
16334 tree goodfn = NULL_TREE;
16335 bool addr_p;
16337 if (TREE_CODE (arg) == ADDR_EXPR)
16339 arg = TREE_OPERAND (arg, 0);
16340 addr_p = true;
16342 else
16343 addr_p = false;
16345 if (TREE_CODE (arg) == COMPONENT_REF)
16346 /* Handle `&x' where `x' is some static or non-static member
16347 function name. */
16348 arg = TREE_OPERAND (arg, 1);
16350 if (TREE_CODE (arg) == OFFSET_REF)
16351 arg = TREE_OPERAND (arg, 1);
16353 /* Strip baselink information. */
16354 if (BASELINK_P (arg))
16355 arg = BASELINK_FUNCTIONS (arg);
16357 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16359 /* If we got some explicit template args, we need to plug them into
16360 the affected templates before we try to unify, in case the
16361 explicit args will completely resolve the templates in question. */
16363 int ok = 0;
16364 tree expl_subargs = TREE_OPERAND (arg, 1);
16365 arg = TREE_OPERAND (arg, 0);
16367 for (; arg; arg = OVL_NEXT (arg))
16369 tree fn = OVL_CURRENT (arg);
16370 tree subargs, elem;
16372 if (TREE_CODE (fn) != TEMPLATE_DECL)
16373 continue;
16375 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16376 expl_subargs, NULL_TREE, tf_none,
16377 /*require_all_args=*/true,
16378 /*use_default_args=*/true);
16379 if (subargs != error_mark_node
16380 && !any_dependent_template_arguments_p (subargs))
16382 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16383 if (try_one_overload (tparms, targs, tempargs, parm,
16384 elem, strict, sub_strict, addr_p, explain_p)
16385 && (!goodfn || !same_type_p (goodfn, elem)))
16387 goodfn = elem;
16388 ++good;
16391 else if (subargs)
16392 ++ok;
16394 /* If no templates (or more than one) are fully resolved by the
16395 explicit arguments, this template-id is a non-deduced context; it
16396 could still be OK if we deduce all template arguments for the
16397 enclosing call through other arguments. */
16398 if (good != 1)
16399 good = ok;
16401 else if (TREE_CODE (arg) != OVERLOAD
16402 && TREE_CODE (arg) != FUNCTION_DECL)
16403 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16404 -- but the deduction does not succeed because the expression is
16405 not just the function on its own. */
16406 return false;
16407 else
16408 for (; arg; arg = OVL_NEXT (arg))
16409 if (try_one_overload (tparms, targs, tempargs, parm,
16410 TREE_TYPE (OVL_CURRENT (arg)),
16411 strict, sub_strict, addr_p, explain_p)
16412 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16414 goodfn = OVL_CURRENT (arg);
16415 ++good;
16418 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16419 to function or pointer to member function argument if the set of
16420 overloaded functions does not contain function templates and at most
16421 one of a set of overloaded functions provides a unique match.
16423 So if we found multiple possibilities, we return success but don't
16424 deduce anything. */
16426 if (good == 1)
16428 int i = TREE_VEC_LENGTH (targs);
16429 for (; i--; )
16430 if (TREE_VEC_ELT (tempargs, i))
16431 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16433 if (good)
16434 return true;
16436 return false;
16439 /* Core DR 115: In contexts where deduction is done and fails, or in
16440 contexts where deduction is not done, if a template argument list is
16441 specified and it, along with any default template arguments, identifies
16442 a single function template specialization, then the template-id is an
16443 lvalue for the function template specialization. */
16445 tree
16446 resolve_nondeduced_context (tree orig_expr)
16448 tree expr, offset, baselink;
16449 bool addr;
16451 if (!type_unknown_p (orig_expr))
16452 return orig_expr;
16454 expr = orig_expr;
16455 addr = false;
16456 offset = NULL_TREE;
16457 baselink = NULL_TREE;
16459 if (TREE_CODE (expr) == ADDR_EXPR)
16461 expr = TREE_OPERAND (expr, 0);
16462 addr = true;
16464 if (TREE_CODE (expr) == OFFSET_REF)
16466 offset = expr;
16467 expr = TREE_OPERAND (expr, 1);
16469 if (BASELINK_P (expr))
16471 baselink = expr;
16472 expr = BASELINK_FUNCTIONS (expr);
16475 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16477 int good = 0;
16478 tree goodfn = NULL_TREE;
16480 /* If we got some explicit template args, we need to plug them into
16481 the affected templates before we try to unify, in case the
16482 explicit args will completely resolve the templates in question. */
16484 tree expl_subargs = TREE_OPERAND (expr, 1);
16485 tree arg = TREE_OPERAND (expr, 0);
16486 tree badfn = NULL_TREE;
16487 tree badargs = NULL_TREE;
16489 for (; arg; arg = OVL_NEXT (arg))
16491 tree fn = OVL_CURRENT (arg);
16492 tree subargs, elem;
16494 if (TREE_CODE (fn) != TEMPLATE_DECL)
16495 continue;
16497 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16498 expl_subargs, NULL_TREE, tf_none,
16499 /*require_all_args=*/true,
16500 /*use_default_args=*/true);
16501 if (subargs != error_mark_node
16502 && !any_dependent_template_arguments_p (subargs))
16504 elem = instantiate_template (fn, subargs, tf_none);
16505 if (elem == error_mark_node)
16507 badfn = fn;
16508 badargs = subargs;
16510 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16512 goodfn = elem;
16513 ++good;
16517 if (good == 1)
16519 mark_used (goodfn);
16520 expr = goodfn;
16521 if (baselink)
16522 expr = build_baselink (BASELINK_BINFO (baselink),
16523 BASELINK_ACCESS_BINFO (baselink),
16524 expr, BASELINK_OPTYPE (baselink));
16525 if (offset)
16527 tree base
16528 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16529 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16531 if (addr)
16532 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16533 return expr;
16535 else if (good == 0 && badargs)
16536 /* There were no good options and at least one bad one, so let the
16537 user know what the problem is. */
16538 instantiate_template (badfn, badargs, tf_warning_or_error);
16540 return orig_expr;
16543 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16544 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16545 different overloads deduce different arguments for a given parm.
16546 ADDR_P is true if the expression for which deduction is being
16547 performed was of the form "& fn" rather than simply "fn".
16549 Returns 1 on success. */
16551 static int
16552 try_one_overload (tree tparms,
16553 tree orig_targs,
16554 tree targs,
16555 tree parm,
16556 tree arg,
16557 unification_kind_t strict,
16558 int sub_strict,
16559 bool addr_p,
16560 bool explain_p)
16562 int nargs;
16563 tree tempargs;
16564 int i;
16566 if (arg == error_mark_node)
16567 return 0;
16569 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16570 to function or pointer to member function argument if the set of
16571 overloaded functions does not contain function templates and at most
16572 one of a set of overloaded functions provides a unique match.
16574 So if this is a template, just return success. */
16576 if (uses_template_parms (arg))
16577 return 1;
16579 if (TREE_CODE (arg) == METHOD_TYPE)
16580 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16581 else if (addr_p)
16582 arg = build_pointer_type (arg);
16584 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16586 /* We don't copy orig_targs for this because if we have already deduced
16587 some template args from previous args, unify would complain when we
16588 try to deduce a template parameter for the same argument, even though
16589 there isn't really a conflict. */
16590 nargs = TREE_VEC_LENGTH (targs);
16591 tempargs = make_tree_vec (nargs);
16593 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16594 return 0;
16596 /* First make sure we didn't deduce anything that conflicts with
16597 explicitly specified args. */
16598 for (i = nargs; i--; )
16600 tree elt = TREE_VEC_ELT (tempargs, i);
16601 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16603 if (!elt)
16604 /*NOP*/;
16605 else if (uses_template_parms (elt))
16606 /* Since we're unifying against ourselves, we will fill in
16607 template args used in the function parm list with our own
16608 template parms. Discard them. */
16609 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16610 else if (oldelt && !template_args_equal (oldelt, elt))
16611 return 0;
16614 for (i = nargs; i--; )
16616 tree elt = TREE_VEC_ELT (tempargs, i);
16618 if (elt)
16619 TREE_VEC_ELT (targs, i) = elt;
16622 return 1;
16625 /* PARM is a template class (perhaps with unbound template
16626 parameters). ARG is a fully instantiated type. If ARG can be
16627 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16628 TARGS are as for unify. */
16630 static tree
16631 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16632 bool explain_p)
16634 tree copy_of_targs;
16636 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16637 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16638 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16639 return NULL_TREE;
16641 /* We need to make a new template argument vector for the call to
16642 unify. If we used TARGS, we'd clutter it up with the result of
16643 the attempted unification, even if this class didn't work out.
16644 We also don't want to commit ourselves to all the unifications
16645 we've already done, since unification is supposed to be done on
16646 an argument-by-argument basis. In other words, consider the
16647 following pathological case:
16649 template <int I, int J, int K>
16650 struct S {};
16652 template <int I, int J>
16653 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16655 template <int I, int J, int K>
16656 void f(S<I, J, K>, S<I, I, I>);
16658 void g() {
16659 S<0, 0, 0> s0;
16660 S<0, 1, 2> s2;
16662 f(s0, s2);
16665 Now, by the time we consider the unification involving `s2', we
16666 already know that we must have `f<0, 0, 0>'. But, even though
16667 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16668 because there are two ways to unify base classes of S<0, 1, 2>
16669 with S<I, I, I>. If we kept the already deduced knowledge, we
16670 would reject the possibility I=1. */
16671 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16673 /* If unification failed, we're done. */
16674 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16675 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16676 return NULL_TREE;
16678 return arg;
16681 /* Given a template type PARM and a class type ARG, find the unique
16682 base type in ARG that is an instance of PARM. We do not examine
16683 ARG itself; only its base-classes. If there is not exactly one
16684 appropriate base class, return NULL_TREE. PARM may be the type of
16685 a partial specialization, as well as a plain template type. Used
16686 by unify. */
16688 static enum template_base_result
16689 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16690 bool explain_p, tree *result)
16692 tree rval = NULL_TREE;
16693 tree binfo;
16695 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16697 binfo = TYPE_BINFO (complete_type (arg));
16698 if (!binfo)
16700 /* The type could not be completed. */
16701 *result = NULL_TREE;
16702 return tbr_incomplete_type;
16705 /* Walk in inheritance graph order. The search order is not
16706 important, and this avoids multiple walks of virtual bases. */
16707 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16709 tree r = try_class_unification (tparms, targs, parm,
16710 BINFO_TYPE (binfo), explain_p);
16712 if (r)
16714 /* If there is more than one satisfactory baseclass, then:
16716 [temp.deduct.call]
16718 If they yield more than one possible deduced A, the type
16719 deduction fails.
16721 applies. */
16722 if (rval && !same_type_p (r, rval))
16724 *result = NULL_TREE;
16725 return tbr_ambiguous_baseclass;
16728 rval = r;
16732 *result = rval;
16733 return tbr_success;
16736 /* Returns the level of DECL, which declares a template parameter. */
16738 static int
16739 template_decl_level (tree decl)
16741 switch (TREE_CODE (decl))
16743 case TYPE_DECL:
16744 case TEMPLATE_DECL:
16745 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16747 case PARM_DECL:
16748 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16750 default:
16751 gcc_unreachable ();
16753 return 0;
16756 /* Decide whether ARG can be unified with PARM, considering only the
16757 cv-qualifiers of each type, given STRICT as documented for unify.
16758 Returns nonzero iff the unification is OK on that basis. */
16760 static int
16761 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16763 int arg_quals = cp_type_quals (arg);
16764 int parm_quals = cp_type_quals (parm);
16766 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16767 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16769 /* Although a CVR qualifier is ignored when being applied to a
16770 substituted template parameter ([8.3.2]/1 for example), that
16771 does not allow us to unify "const T" with "int&" because both
16772 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16773 It is ok when we're allowing additional CV qualifiers
16774 at the outer level [14.8.2.1]/3,1st bullet. */
16775 if ((TREE_CODE (arg) == REFERENCE_TYPE
16776 || TREE_CODE (arg) == FUNCTION_TYPE
16777 || TREE_CODE (arg) == METHOD_TYPE)
16778 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16779 return 0;
16781 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16782 && (parm_quals & TYPE_QUAL_RESTRICT))
16783 return 0;
16786 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16787 && (arg_quals & parm_quals) != parm_quals)
16788 return 0;
16790 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16791 && (parm_quals & arg_quals) != arg_quals)
16792 return 0;
16794 return 1;
16797 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16798 void
16799 template_parm_level_and_index (tree parm, int* level, int* index)
16801 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16802 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16803 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16805 *index = TEMPLATE_TYPE_IDX (parm);
16806 *level = TEMPLATE_TYPE_LEVEL (parm);
16808 else
16810 *index = TEMPLATE_PARM_IDX (parm);
16811 *level = TEMPLATE_PARM_LEVEL (parm);
16815 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16816 do { \
16817 if (unify (TP, TA, P, A, S, EP)) \
16818 return 1; \
16819 } while (0);
16821 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16822 expansion at the end of PACKED_PARMS. Returns 0 if the type
16823 deduction succeeds, 1 otherwise. STRICT is the same as in
16824 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16825 call argument list. We'll need to adjust the arguments to make them
16826 types. SUBR tells us if this is from a recursive call to
16827 type_unification_real, or for comparing two template argument
16828 lists. */
16830 static int
16831 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16832 tree packed_args, unification_kind_t strict,
16833 bool subr, bool explain_p)
16835 tree parm
16836 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16837 tree pattern = PACK_EXPANSION_PATTERN (parm);
16838 tree pack, packs = NULL_TREE;
16839 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16840 int len = TREE_VEC_LENGTH (packed_args);
16842 /* Determine the parameter packs we will be deducing from the
16843 pattern, and record their current deductions. */
16844 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16845 pack; pack = TREE_CHAIN (pack))
16847 tree parm_pack = TREE_VALUE (pack);
16848 int idx, level;
16850 /* Determine the index and level of this parameter pack. */
16851 template_parm_level_and_index (parm_pack, &level, &idx);
16853 /* Keep track of the parameter packs and their corresponding
16854 argument packs. */
16855 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16856 TREE_TYPE (packs) = make_tree_vec (len - start);
16859 /* Loop through all of the arguments that have not yet been
16860 unified and unify each with the pattern. */
16861 for (i = start; i < len; i++)
16863 tree parm;
16864 bool any_explicit = false;
16865 tree arg = TREE_VEC_ELT (packed_args, i);
16867 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16868 or the element of its argument pack at the current index if
16869 this argument was explicitly specified. */
16870 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16872 int idx, level;
16873 tree arg, pargs;
16874 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16876 arg = NULL_TREE;
16877 if (TREE_VALUE (pack)
16878 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16879 && (i - start < TREE_VEC_LENGTH (pargs)))
16881 any_explicit = true;
16882 arg = TREE_VEC_ELT (pargs, i - start);
16884 TMPL_ARG (targs, level, idx) = arg;
16887 /* If we had explicit template arguments, substitute them into the
16888 pattern before deduction. */
16889 if (any_explicit)
16891 /* Some arguments might still be unspecified or dependent. */
16892 bool dependent;
16893 ++processing_template_decl;
16894 dependent = any_dependent_template_arguments_p (targs);
16895 if (!dependent)
16896 --processing_template_decl;
16897 parm = tsubst (pattern, targs,
16898 explain_p ? tf_warning_or_error : tf_none,
16899 NULL_TREE);
16900 if (dependent)
16901 --processing_template_decl;
16902 if (parm == error_mark_node)
16903 return 1;
16905 else
16906 parm = pattern;
16908 /* Unify the pattern with the current argument. */
16909 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16910 LOOKUP_IMPLICIT, explain_p))
16911 return 1;
16913 /* For each parameter pack, collect the deduced value. */
16914 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16916 int idx, level;
16917 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16919 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16920 TMPL_ARG (targs, level, idx);
16924 /* Verify that the results of unification with the parameter packs
16925 produce results consistent with what we've seen before, and make
16926 the deduced argument packs available. */
16927 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16929 tree old_pack = TREE_VALUE (pack);
16930 tree new_args = TREE_TYPE (pack);
16931 int i, len = TREE_VEC_LENGTH (new_args);
16932 int idx, level;
16933 bool nondeduced_p = false;
16935 /* By default keep the original deduced argument pack.
16936 If necessary, more specific code is going to update the
16937 resulting deduced argument later down in this function. */
16938 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16939 TMPL_ARG (targs, level, idx) = old_pack;
16941 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16942 actually deduce anything. */
16943 for (i = 0; i < len && !nondeduced_p; ++i)
16944 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16945 nondeduced_p = true;
16946 if (nondeduced_p)
16947 continue;
16949 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16951 /* If we had fewer function args than explicit template args,
16952 just use the explicits. */
16953 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16954 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16955 if (len < explicit_len)
16956 new_args = explicit_args;
16959 if (!old_pack)
16961 tree result;
16962 /* Build the deduced *_ARGUMENT_PACK. */
16963 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16965 result = make_node (NONTYPE_ARGUMENT_PACK);
16966 TREE_TYPE (result) =
16967 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16968 TREE_CONSTANT (result) = 1;
16970 else
16971 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16973 SET_ARGUMENT_PACK_ARGS (result, new_args);
16975 /* Note the deduced argument packs for this parameter
16976 pack. */
16977 TMPL_ARG (targs, level, idx) = result;
16979 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16980 && (ARGUMENT_PACK_ARGS (old_pack)
16981 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16983 /* We only had the explicitly-provided arguments before, but
16984 now we have a complete set of arguments. */
16985 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16987 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16988 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16989 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16991 else
16993 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16994 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16996 if (!comp_template_args_with_info (old_args, new_args,
16997 &bad_old_arg, &bad_new_arg))
16998 /* Inconsistent unification of this parameter pack. */
16999 return unify_parameter_pack_inconsistent (explain_p,
17000 bad_old_arg,
17001 bad_new_arg);
17005 return unify_success (explain_p);
17008 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17009 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17010 parameters and return value are as for unify. */
17012 static int
17013 unify_array_domain (tree tparms, tree targs,
17014 tree parm_dom, tree arg_dom,
17015 bool explain_p)
17017 tree parm_max;
17018 tree arg_max;
17019 bool parm_cst;
17020 bool arg_cst;
17022 /* Our representation of array types uses "N - 1" as the
17023 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17024 not an integer constant. We cannot unify arbitrarily
17025 complex expressions, so we eliminate the MINUS_EXPRs
17026 here. */
17027 parm_max = TYPE_MAX_VALUE (parm_dom);
17028 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17029 if (!parm_cst)
17031 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17032 parm_max = TREE_OPERAND (parm_max, 0);
17034 arg_max = TYPE_MAX_VALUE (arg_dom);
17035 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17036 if (!arg_cst)
17038 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17039 trying to unify the type of a variable with the type
17040 of a template parameter. For example:
17042 template <unsigned int N>
17043 void f (char (&) [N]);
17044 int g();
17045 void h(int i) {
17046 char a[g(i)];
17047 f(a);
17050 Here, the type of the ARG will be "int [g(i)]", and
17051 may be a SAVE_EXPR, etc. */
17052 if (TREE_CODE (arg_max) != MINUS_EXPR)
17053 return unify_vla_arg (explain_p, arg_dom);
17054 arg_max = TREE_OPERAND (arg_max, 0);
17057 /* If only one of the bounds used a MINUS_EXPR, compensate
17058 by adding one to the other bound. */
17059 if (parm_cst && !arg_cst)
17060 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17061 integer_type_node,
17062 parm_max,
17063 integer_one_node);
17064 else if (arg_cst && !parm_cst)
17065 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17066 integer_type_node,
17067 arg_max,
17068 integer_one_node);
17070 return unify (tparms, targs, parm_max, arg_max,
17071 UNIFY_ALLOW_INTEGER, explain_p);
17074 /* Deduce the value of template parameters. TPARMS is the (innermost)
17075 set of template parameters to a template. TARGS is the bindings
17076 for those template parameters, as determined thus far; TARGS may
17077 include template arguments for outer levels of template parameters
17078 as well. PARM is a parameter to a template function, or a
17079 subcomponent of that parameter; ARG is the corresponding argument.
17080 This function attempts to match PARM with ARG in a manner
17081 consistent with the existing assignments in TARGS. If more values
17082 are deduced, then TARGS is updated.
17084 Returns 0 if the type deduction succeeds, 1 otherwise. The
17085 parameter STRICT is a bitwise or of the following flags:
17087 UNIFY_ALLOW_NONE:
17088 Require an exact match between PARM and ARG.
17089 UNIFY_ALLOW_MORE_CV_QUAL:
17090 Allow the deduced ARG to be more cv-qualified (by qualification
17091 conversion) than ARG.
17092 UNIFY_ALLOW_LESS_CV_QUAL:
17093 Allow the deduced ARG to be less cv-qualified than ARG.
17094 UNIFY_ALLOW_DERIVED:
17095 Allow the deduced ARG to be a template base class of ARG,
17096 or a pointer to a template base class of the type pointed to by
17097 ARG.
17098 UNIFY_ALLOW_INTEGER:
17099 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17100 case for more information.
17101 UNIFY_ALLOW_OUTER_LEVEL:
17102 This is the outermost level of a deduction. Used to determine validity
17103 of qualification conversions. A valid qualification conversion must
17104 have const qualified pointers leading up to the inner type which
17105 requires additional CV quals, except at the outer level, where const
17106 is not required [conv.qual]. It would be normal to set this flag in
17107 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17108 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17109 This is the outermost level of a deduction, and PARM can be more CV
17110 qualified at this point.
17111 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17112 This is the outermost level of a deduction, and PARM can be less CV
17113 qualified at this point. */
17115 static int
17116 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17117 bool explain_p)
17119 int idx;
17120 tree targ;
17121 tree tparm;
17122 int strict_in = strict;
17124 /* I don't think this will do the right thing with respect to types.
17125 But the only case I've seen it in so far has been array bounds, where
17126 signedness is the only information lost, and I think that will be
17127 okay. */
17128 while (TREE_CODE (parm) == NOP_EXPR)
17129 parm = TREE_OPERAND (parm, 0);
17131 if (arg == error_mark_node)
17132 return unify_invalid (explain_p);
17133 if (arg == unknown_type_node
17134 || arg == init_list_type_node)
17135 /* We can't deduce anything from this, but we might get all the
17136 template args from other function args. */
17137 return unify_success (explain_p);
17139 /* If PARM uses template parameters, then we can't bail out here,
17140 even if ARG == PARM, since we won't record unifications for the
17141 template parameters. We might need them if we're trying to
17142 figure out which of two things is more specialized. */
17143 if (arg == parm && !uses_template_parms (parm))
17144 return unify_success (explain_p);
17146 /* Handle init lists early, so the rest of the function can assume
17147 we're dealing with a type. */
17148 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17150 tree elt, elttype;
17151 unsigned i;
17152 tree orig_parm = parm;
17154 /* Replace T with std::initializer_list<T> for deduction. */
17155 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17156 && flag_deduce_init_list)
17157 parm = listify (parm);
17159 if (!is_std_init_list (parm)
17160 && TREE_CODE (parm) != ARRAY_TYPE)
17161 /* We can only deduce from an initializer list argument if the
17162 parameter is std::initializer_list or an array; otherwise this
17163 is a non-deduced context. */
17164 return unify_success (explain_p);
17166 if (TREE_CODE (parm) == ARRAY_TYPE)
17167 elttype = TREE_TYPE (parm);
17168 else
17169 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17171 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17173 int elt_strict = strict;
17175 if (elt == error_mark_node)
17176 return unify_invalid (explain_p);
17178 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17180 tree type = TREE_TYPE (elt);
17181 /* It should only be possible to get here for a call. */
17182 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17183 elt_strict |= maybe_adjust_types_for_deduction
17184 (DEDUCE_CALL, &elttype, &type, elt);
17185 elt = type;
17188 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17189 explain_p);
17192 if (TREE_CODE (parm) == ARRAY_TYPE)
17194 /* Also deduce from the length of the initializer list. */
17195 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17196 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17197 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17198 idx, explain_p);
17201 /* If the std::initializer_list<T> deduction worked, replace the
17202 deduced A with std::initializer_list<A>. */
17203 if (orig_parm != parm)
17205 idx = TEMPLATE_TYPE_IDX (orig_parm);
17206 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17207 targ = listify (targ);
17208 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17210 return unify_success (explain_p);
17213 /* Immediately reject some pairs that won't unify because of
17214 cv-qualification mismatches. */
17215 if (TREE_CODE (arg) == TREE_CODE (parm)
17216 && TYPE_P (arg)
17217 /* It is the elements of the array which hold the cv quals of an array
17218 type, and the elements might be template type parms. We'll check
17219 when we recurse. */
17220 && TREE_CODE (arg) != ARRAY_TYPE
17221 /* We check the cv-qualifiers when unifying with template type
17222 parameters below. We want to allow ARG `const T' to unify with
17223 PARM `T' for example, when computing which of two templates
17224 is more specialized, for example. */
17225 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17226 && !check_cv_quals_for_unify (strict_in, arg, parm))
17227 return unify_cv_qual_mismatch (explain_p, parm, arg);
17229 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17230 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17231 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17232 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17233 strict &= ~UNIFY_ALLOW_DERIVED;
17234 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17235 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17237 switch (TREE_CODE (parm))
17239 case TYPENAME_TYPE:
17240 case SCOPE_REF:
17241 case UNBOUND_CLASS_TEMPLATE:
17242 /* In a type which contains a nested-name-specifier, template
17243 argument values cannot be deduced for template parameters used
17244 within the nested-name-specifier. */
17245 return unify_success (explain_p);
17247 case TEMPLATE_TYPE_PARM:
17248 case TEMPLATE_TEMPLATE_PARM:
17249 case BOUND_TEMPLATE_TEMPLATE_PARM:
17250 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17251 if (tparm == error_mark_node)
17252 return unify_invalid (explain_p);
17254 if (TEMPLATE_TYPE_LEVEL (parm)
17255 != template_decl_level (tparm))
17256 /* The PARM is not one we're trying to unify. Just check
17257 to see if it matches ARG. */
17259 if (TREE_CODE (arg) == TREE_CODE (parm)
17260 && (is_auto (parm) ? is_auto (arg)
17261 : same_type_p (parm, arg)))
17262 return unify_success (explain_p);
17263 else
17264 return unify_type_mismatch (explain_p, parm, arg);
17266 idx = TEMPLATE_TYPE_IDX (parm);
17267 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17268 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17269 if (tparm == error_mark_node)
17270 return unify_invalid (explain_p);
17272 /* Check for mixed types and values. */
17273 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17274 && TREE_CODE (tparm) != TYPE_DECL)
17275 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17276 && TREE_CODE (tparm) != TEMPLATE_DECL))
17277 gcc_unreachable ();
17279 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17281 /* ARG must be constructed from a template class or a template
17282 template parameter. */
17283 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17284 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17285 return unify_template_deduction_failure (explain_p, parm, arg);
17288 tree parmvec = TYPE_TI_ARGS (parm);
17289 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17290 tree full_argvec = add_to_template_args (targs, argvec);
17291 tree parm_parms
17292 = DECL_INNERMOST_TEMPLATE_PARMS
17293 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17294 int i, len;
17295 int parm_variadic_p = 0;
17297 /* The resolution to DR150 makes clear that default
17298 arguments for an N-argument may not be used to bind T
17299 to a template template parameter with fewer than N
17300 parameters. It is not safe to permit the binding of
17301 default arguments as an extension, as that may change
17302 the meaning of a conforming program. Consider:
17304 struct Dense { static const unsigned int dim = 1; };
17306 template <template <typename> class View,
17307 typename Block>
17308 void operator+(float, View<Block> const&);
17310 template <typename Block,
17311 unsigned int Dim = Block::dim>
17312 struct Lvalue_proxy { operator float() const; };
17314 void
17315 test_1d (void) {
17316 Lvalue_proxy<Dense> p;
17317 float b;
17318 b + p;
17321 Here, if Lvalue_proxy is permitted to bind to View, then
17322 the global operator+ will be used; if they are not, the
17323 Lvalue_proxy will be converted to float. */
17324 if (coerce_template_parms (parm_parms,
17325 full_argvec,
17326 TYPE_TI_TEMPLATE (parm),
17327 (explain_p
17328 ? tf_warning_or_error
17329 : tf_none),
17330 /*require_all_args=*/true,
17331 /*use_default_args=*/false)
17332 == error_mark_node)
17333 return 1;
17335 /* Deduce arguments T, i from TT<T> or TT<i>.
17336 We check each element of PARMVEC and ARGVEC individually
17337 rather than the whole TREE_VEC since they can have
17338 different number of elements. */
17340 parmvec = expand_template_argument_pack (parmvec);
17341 argvec = expand_template_argument_pack (argvec);
17343 len = TREE_VEC_LENGTH (parmvec);
17345 /* Check if the parameters end in a pack, making them
17346 variadic. */
17347 if (len > 0
17348 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17349 parm_variadic_p = 1;
17351 for (i = 0; i < len - parm_variadic_p; ++i)
17352 /* If the template argument list of P contains a pack
17353 expansion that is not the last template argument, the
17354 entire template argument list is a non-deduced
17355 context. */
17356 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17357 return unify_success (explain_p);
17359 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17360 return unify_too_few_arguments (explain_p,
17361 TREE_VEC_LENGTH (argvec), len);
17363 for (i = 0; i < len - parm_variadic_p; ++i)
17365 RECUR_AND_CHECK_FAILURE (tparms, targs,
17366 TREE_VEC_ELT (parmvec, i),
17367 TREE_VEC_ELT (argvec, i),
17368 UNIFY_ALLOW_NONE, explain_p);
17371 if (parm_variadic_p
17372 && unify_pack_expansion (tparms, targs,
17373 parmvec, argvec,
17374 DEDUCE_EXACT,
17375 /*subr=*/true, explain_p))
17376 return 1;
17378 arg = TYPE_TI_TEMPLATE (arg);
17380 /* Fall through to deduce template name. */
17383 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17384 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17386 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17388 /* Simple cases: Value already set, does match or doesn't. */
17389 if (targ != NULL_TREE && template_args_equal (targ, arg))
17390 return unify_success (explain_p);
17391 else if (targ)
17392 return unify_inconsistency (explain_p, parm, targ, arg);
17394 else
17396 /* If PARM is `const T' and ARG is only `int', we don't have
17397 a match unless we are allowing additional qualification.
17398 If ARG is `const int' and PARM is just `T' that's OK;
17399 that binds `const int' to `T'. */
17400 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17401 arg, parm))
17402 return unify_cv_qual_mismatch (explain_p, parm, arg);
17404 /* Consider the case where ARG is `const volatile int' and
17405 PARM is `const T'. Then, T should be `volatile int'. */
17406 arg = cp_build_qualified_type_real
17407 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17408 if (arg == error_mark_node)
17409 return unify_invalid (explain_p);
17411 /* Simple cases: Value already set, does match or doesn't. */
17412 if (targ != NULL_TREE && same_type_p (targ, arg))
17413 return unify_success (explain_p);
17414 else if (targ)
17415 return unify_inconsistency (explain_p, parm, targ, arg);
17417 /* Make sure that ARG is not a variable-sized array. (Note
17418 that were talking about variable-sized arrays (like
17419 `int[n]'), rather than arrays of unknown size (like
17420 `int[]').) We'll get very confused by such a type since
17421 the bound of the array is not constant, and therefore
17422 not mangleable. Besides, such types are not allowed in
17423 ISO C++, so we can do as we please here. We do allow
17424 them for 'auto' deduction, since that isn't ABI-exposed. */
17425 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17426 return unify_vla_arg (explain_p, arg);
17428 /* Strip typedefs as in convert_template_argument. */
17429 arg = canonicalize_type_argument (arg, tf_none);
17432 /* If ARG is a parameter pack or an expansion, we cannot unify
17433 against it unless PARM is also a parameter pack. */
17434 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17435 && !template_parameter_pack_p (parm))
17436 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17438 /* If the argument deduction results is a METHOD_TYPE,
17439 then there is a problem.
17440 METHOD_TYPE doesn't map to any real C++ type the result of
17441 the deduction can not be of that type. */
17442 if (TREE_CODE (arg) == METHOD_TYPE)
17443 return unify_method_type_error (explain_p, arg);
17445 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17446 return unify_success (explain_p);
17448 case TEMPLATE_PARM_INDEX:
17449 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17450 if (tparm == error_mark_node)
17451 return unify_invalid (explain_p);
17453 if (TEMPLATE_PARM_LEVEL (parm)
17454 != template_decl_level (tparm))
17456 /* The PARM is not one we're trying to unify. Just check
17457 to see if it matches ARG. */
17458 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17459 && cp_tree_equal (parm, arg));
17460 if (result)
17461 unify_expression_unequal (explain_p, parm, arg);
17462 return result;
17465 idx = TEMPLATE_PARM_IDX (parm);
17466 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17468 if (targ)
17470 int x = !cp_tree_equal (targ, arg);
17471 if (x)
17472 unify_inconsistency (explain_p, parm, targ, arg);
17473 return x;
17476 /* [temp.deduct.type] If, in the declaration of a function template
17477 with a non-type template-parameter, the non-type
17478 template-parameter is used in an expression in the function
17479 parameter-list and, if the corresponding template-argument is
17480 deduced, the template-argument type shall match the type of the
17481 template-parameter exactly, except that a template-argument
17482 deduced from an array bound may be of any integral type.
17483 The non-type parameter might use already deduced type parameters. */
17484 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17485 if (!TREE_TYPE (arg))
17486 /* Template-parameter dependent expression. Just accept it for now.
17487 It will later be processed in convert_template_argument. */
17489 else if (same_type_p (TREE_TYPE (arg), tparm))
17490 /* OK */;
17491 else if ((strict & UNIFY_ALLOW_INTEGER)
17492 && CP_INTEGRAL_TYPE_P (tparm))
17493 /* Convert the ARG to the type of PARM; the deduced non-type
17494 template argument must exactly match the types of the
17495 corresponding parameter. */
17496 arg = fold (build_nop (tparm, arg));
17497 else if (uses_template_parms (tparm))
17498 /* We haven't deduced the type of this parameter yet. Try again
17499 later. */
17500 return unify_success (explain_p);
17501 else
17502 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17504 /* If ARG is a parameter pack or an expansion, we cannot unify
17505 against it unless PARM is also a parameter pack. */
17506 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17507 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17508 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17510 arg = strip_typedefs_expr (arg);
17511 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17512 return unify_success (explain_p);
17514 case PTRMEM_CST:
17516 /* A pointer-to-member constant can be unified only with
17517 another constant. */
17518 if (TREE_CODE (arg) != PTRMEM_CST)
17519 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17521 /* Just unify the class member. It would be useless (and possibly
17522 wrong, depending on the strict flags) to unify also
17523 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17524 arg refer to the same variable, even if through different
17525 classes. For instance:
17527 struct A { int x; };
17528 struct B : A { };
17530 Unification of &A::x and &B::x must succeed. */
17531 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17532 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17535 case POINTER_TYPE:
17537 if (!TYPE_PTR_P (arg))
17538 return unify_type_mismatch (explain_p, parm, arg);
17540 /* [temp.deduct.call]
17542 A can be another pointer or pointer to member type that can
17543 be converted to the deduced A via a qualification
17544 conversion (_conv.qual_).
17546 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17547 This will allow for additional cv-qualification of the
17548 pointed-to types if appropriate. */
17550 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17551 /* The derived-to-base conversion only persists through one
17552 level of pointers. */
17553 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17555 return unify (tparms, targs, TREE_TYPE (parm),
17556 TREE_TYPE (arg), strict, explain_p);
17559 case REFERENCE_TYPE:
17560 if (TREE_CODE (arg) != REFERENCE_TYPE)
17561 return unify_type_mismatch (explain_p, parm, arg);
17562 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17563 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17565 case ARRAY_TYPE:
17566 if (TREE_CODE (arg) != ARRAY_TYPE)
17567 return unify_type_mismatch (explain_p, parm, arg);
17568 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17569 != (TYPE_DOMAIN (arg) == NULL_TREE))
17570 return unify_type_mismatch (explain_p, parm, arg);
17571 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17572 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17573 if (TYPE_DOMAIN (parm) != NULL_TREE)
17574 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17575 TYPE_DOMAIN (arg), explain_p);
17576 return unify_success (explain_p);
17578 case REAL_TYPE:
17579 case COMPLEX_TYPE:
17580 case VECTOR_TYPE:
17581 case INTEGER_TYPE:
17582 case BOOLEAN_TYPE:
17583 case ENUMERAL_TYPE:
17584 case VOID_TYPE:
17585 case NULLPTR_TYPE:
17586 if (TREE_CODE (arg) != TREE_CODE (parm))
17587 return unify_type_mismatch (explain_p, parm, arg);
17589 /* We have already checked cv-qualification at the top of the
17590 function. */
17591 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17592 return unify_type_mismatch (explain_p, parm, arg);
17594 /* As far as unification is concerned, this wins. Later checks
17595 will invalidate it if necessary. */
17596 return unify_success (explain_p);
17598 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17599 /* Type INTEGER_CST can come from ordinary constant template args. */
17600 case INTEGER_CST:
17601 while (TREE_CODE (arg) == NOP_EXPR)
17602 arg = TREE_OPERAND (arg, 0);
17604 if (TREE_CODE (arg) != INTEGER_CST)
17605 return unify_template_argument_mismatch (explain_p, parm, arg);
17606 return (tree_int_cst_equal (parm, arg)
17607 ? unify_success (explain_p)
17608 : unify_template_argument_mismatch (explain_p, parm, arg));
17610 case TREE_VEC:
17612 int i, len, argslen;
17613 int parm_variadic_p = 0;
17615 if (TREE_CODE (arg) != TREE_VEC)
17616 return unify_template_argument_mismatch (explain_p, parm, arg);
17618 len = TREE_VEC_LENGTH (parm);
17619 argslen = TREE_VEC_LENGTH (arg);
17621 /* Check for pack expansions in the parameters. */
17622 for (i = 0; i < len; ++i)
17624 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17626 if (i == len - 1)
17627 /* We can unify against something with a trailing
17628 parameter pack. */
17629 parm_variadic_p = 1;
17630 else
17631 /* [temp.deduct.type]/9: If the template argument list of
17632 P contains a pack expansion that is not the last
17633 template argument, the entire template argument list
17634 is a non-deduced context. */
17635 return unify_success (explain_p);
17639 /* If we don't have enough arguments to satisfy the parameters
17640 (not counting the pack expression at the end), or we have
17641 too many arguments for a parameter list that doesn't end in
17642 a pack expression, we can't unify. */
17643 if (parm_variadic_p
17644 ? argslen < len - parm_variadic_p
17645 : argslen != len)
17646 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17648 /* Unify all of the parameters that precede the (optional)
17649 pack expression. */
17650 for (i = 0; i < len - parm_variadic_p; ++i)
17652 RECUR_AND_CHECK_FAILURE (tparms, targs,
17653 TREE_VEC_ELT (parm, i),
17654 TREE_VEC_ELT (arg, i),
17655 UNIFY_ALLOW_NONE, explain_p);
17657 if (parm_variadic_p)
17658 return unify_pack_expansion (tparms, targs, parm, arg,
17659 DEDUCE_EXACT,
17660 /*subr=*/true, explain_p);
17661 return unify_success (explain_p);
17664 case RECORD_TYPE:
17665 case UNION_TYPE:
17666 if (TREE_CODE (arg) != TREE_CODE (parm))
17667 return unify_type_mismatch (explain_p, parm, arg);
17669 if (TYPE_PTRMEMFUNC_P (parm))
17671 if (!TYPE_PTRMEMFUNC_P (arg))
17672 return unify_type_mismatch (explain_p, parm, arg);
17674 return unify (tparms, targs,
17675 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17676 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17677 strict, explain_p);
17680 if (CLASSTYPE_TEMPLATE_INFO (parm))
17682 tree t = NULL_TREE;
17684 if (strict_in & UNIFY_ALLOW_DERIVED)
17686 /* First, we try to unify the PARM and ARG directly. */
17687 t = try_class_unification (tparms, targs,
17688 parm, arg, explain_p);
17690 if (!t)
17692 /* Fallback to the special case allowed in
17693 [temp.deduct.call]:
17695 If P is a class, and P has the form
17696 template-id, then A can be a derived class of
17697 the deduced A. Likewise, if P is a pointer to
17698 a class of the form template-id, A can be a
17699 pointer to a derived class pointed to by the
17700 deduced A. */
17701 enum template_base_result r;
17702 r = get_template_base (tparms, targs, parm, arg,
17703 explain_p, &t);
17705 if (!t)
17706 return unify_no_common_base (explain_p, r, parm, arg);
17709 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17710 && (CLASSTYPE_TI_TEMPLATE (parm)
17711 == CLASSTYPE_TI_TEMPLATE (arg)))
17712 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17713 Then, we should unify `int' and `U'. */
17714 t = arg;
17715 else
17716 /* There's no chance of unification succeeding. */
17717 return unify_type_mismatch (explain_p, parm, arg);
17719 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17720 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17722 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17723 return unify_type_mismatch (explain_p, parm, arg);
17724 return unify_success (explain_p);
17726 case METHOD_TYPE:
17727 case FUNCTION_TYPE:
17729 unsigned int nargs;
17730 tree *args;
17731 tree a;
17732 unsigned int i;
17734 if (TREE_CODE (arg) != TREE_CODE (parm))
17735 return unify_type_mismatch (explain_p, parm, arg);
17737 /* CV qualifications for methods can never be deduced, they must
17738 match exactly. We need to check them explicitly here,
17739 because type_unification_real treats them as any other
17740 cv-qualified parameter. */
17741 if (TREE_CODE (parm) == METHOD_TYPE
17742 && (!check_cv_quals_for_unify
17743 (UNIFY_ALLOW_NONE,
17744 class_of_this_parm (arg),
17745 class_of_this_parm (parm))))
17746 return unify_cv_qual_mismatch (explain_p, parm, arg);
17748 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17749 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17751 nargs = list_length (TYPE_ARG_TYPES (arg));
17752 args = XALLOCAVEC (tree, nargs);
17753 for (a = TYPE_ARG_TYPES (arg), i = 0;
17754 a != NULL_TREE && a != void_list_node;
17755 a = TREE_CHAIN (a), ++i)
17756 args[i] = TREE_VALUE (a);
17757 nargs = i;
17759 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17760 args, nargs, 1, DEDUCE_EXACT,
17761 LOOKUP_NORMAL, NULL, explain_p);
17764 case OFFSET_TYPE:
17765 /* Unify a pointer to member with a pointer to member function, which
17766 deduces the type of the member as a function type. */
17767 if (TYPE_PTRMEMFUNC_P (arg))
17769 /* Check top-level cv qualifiers */
17770 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17771 return unify_cv_qual_mismatch (explain_p, parm, arg);
17773 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17774 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17775 UNIFY_ALLOW_NONE, explain_p);
17777 /* Determine the type of the function we are unifying against. */
17778 tree fntype = static_fn_type (arg);
17780 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17783 if (TREE_CODE (arg) != OFFSET_TYPE)
17784 return unify_type_mismatch (explain_p, parm, arg);
17785 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17786 TYPE_OFFSET_BASETYPE (arg),
17787 UNIFY_ALLOW_NONE, explain_p);
17788 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17789 strict, explain_p);
17791 case CONST_DECL:
17792 if (DECL_TEMPLATE_PARM_P (parm))
17793 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17794 if (arg != integral_constant_value (parm))
17795 return unify_template_argument_mismatch (explain_p, parm, arg);
17796 return unify_success (explain_p);
17798 case FIELD_DECL:
17799 case TEMPLATE_DECL:
17800 /* Matched cases are handled by the ARG == PARM test above. */
17801 return unify_template_argument_mismatch (explain_p, parm, arg);
17803 case VAR_DECL:
17804 /* A non-type template parameter that is a variable should be a
17805 an integral constant, in which case, it whould have been
17806 folded into its (constant) value. So we should not be getting
17807 a variable here. */
17808 gcc_unreachable ();
17810 case TYPE_ARGUMENT_PACK:
17811 case NONTYPE_ARGUMENT_PACK:
17812 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17813 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17815 case TYPEOF_TYPE:
17816 case DECLTYPE_TYPE:
17817 case UNDERLYING_TYPE:
17818 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17819 or UNDERLYING_TYPE nodes. */
17820 return unify_success (explain_p);
17822 case ERROR_MARK:
17823 /* Unification fails if we hit an error node. */
17824 return unify_invalid (explain_p);
17826 default:
17827 /* An unresolved overload is a nondeduced context. */
17828 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17829 return unify_success (explain_p);
17830 gcc_assert (EXPR_P (parm));
17832 /* We must be looking at an expression. This can happen with
17833 something like:
17835 template <int I>
17836 void foo(S<I>, S<I + 2>);
17838 This is a "nondeduced context":
17840 [deduct.type]
17842 The nondeduced contexts are:
17844 --A type that is a template-id in which one or more of
17845 the template-arguments is an expression that references
17846 a template-parameter.
17848 In these cases, we assume deduction succeeded, but don't
17849 actually infer any unifications. */
17851 if (!uses_template_parms (parm)
17852 && !template_args_equal (parm, arg))
17853 return unify_expression_unequal (explain_p, parm, arg);
17854 else
17855 return unify_success (explain_p);
17858 #undef RECUR_AND_CHECK_FAILURE
17860 /* Note that DECL can be defined in this translation unit, if
17861 required. */
17863 static void
17864 mark_definable (tree decl)
17866 tree clone;
17867 DECL_NOT_REALLY_EXTERN (decl) = 1;
17868 FOR_EACH_CLONE (clone, decl)
17869 DECL_NOT_REALLY_EXTERN (clone) = 1;
17872 /* Called if RESULT is explicitly instantiated, or is a member of an
17873 explicitly instantiated class. */
17875 void
17876 mark_decl_instantiated (tree result, int extern_p)
17878 SET_DECL_EXPLICIT_INSTANTIATION (result);
17880 /* If this entity has already been written out, it's too late to
17881 make any modifications. */
17882 if (TREE_ASM_WRITTEN (result))
17883 return;
17885 /* For anonymous namespace we don't need to do anything. */
17886 if (decl_anon_ns_mem_p (result))
17888 gcc_assert (!TREE_PUBLIC (result));
17889 return;
17892 if (TREE_CODE (result) != FUNCTION_DECL)
17893 /* The TREE_PUBLIC flag for function declarations will have been
17894 set correctly by tsubst. */
17895 TREE_PUBLIC (result) = 1;
17897 /* This might have been set by an earlier implicit instantiation. */
17898 DECL_COMDAT (result) = 0;
17900 if (extern_p)
17901 DECL_NOT_REALLY_EXTERN (result) = 0;
17902 else
17904 mark_definable (result);
17905 /* Always make artificials weak. */
17906 if (DECL_ARTIFICIAL (result) && flag_weak)
17907 comdat_linkage (result);
17908 /* For WIN32 we also want to put explicit instantiations in
17909 linkonce sections. */
17910 else if (TREE_PUBLIC (result))
17911 maybe_make_one_only (result);
17914 /* If EXTERN_P, then this function will not be emitted -- unless
17915 followed by an explicit instantiation, at which point its linkage
17916 will be adjusted. If !EXTERN_P, then this function will be
17917 emitted here. In neither circumstance do we want
17918 import_export_decl to adjust the linkage. */
17919 DECL_INTERFACE_KNOWN (result) = 1;
17922 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17923 important template arguments. If any are missing, we check whether
17924 they're important by using error_mark_node for substituting into any
17925 args that were used for partial ordering (the ones between ARGS and END)
17926 and seeing if it bubbles up. */
17928 static bool
17929 check_undeduced_parms (tree targs, tree args, tree end)
17931 bool found = false;
17932 int i;
17933 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17934 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17936 found = true;
17937 TREE_VEC_ELT (targs, i) = error_mark_node;
17939 if (found)
17941 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17942 if (substed == error_mark_node)
17943 return true;
17945 return false;
17948 /* Given two function templates PAT1 and PAT2, return:
17950 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17951 -1 if PAT2 is more specialized than PAT1.
17952 0 if neither is more specialized.
17954 LEN indicates the number of parameters we should consider
17955 (defaulted parameters should not be considered).
17957 The 1998 std underspecified function template partial ordering, and
17958 DR214 addresses the issue. We take pairs of arguments, one from
17959 each of the templates, and deduce them against each other. One of
17960 the templates will be more specialized if all the *other*
17961 template's arguments deduce against its arguments and at least one
17962 of its arguments *does* *not* deduce against the other template's
17963 corresponding argument. Deduction is done as for class templates.
17964 The arguments used in deduction have reference and top level cv
17965 qualifiers removed. Iff both arguments were originally reference
17966 types *and* deduction succeeds in both directions, an lvalue reference
17967 wins against an rvalue reference and otherwise the template
17968 with the more cv-qualified argument wins for that pairing (if
17969 neither is more cv-qualified, they both are equal). Unlike regular
17970 deduction, after all the arguments have been deduced in this way,
17971 we do *not* verify the deduced template argument values can be
17972 substituted into non-deduced contexts.
17974 The logic can be a bit confusing here, because we look at deduce1 and
17975 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17976 can find template arguments for pat1 to make arg1 look like arg2, that
17977 means that arg2 is at least as specialized as arg1. */
17980 more_specialized_fn (tree pat1, tree pat2, int len)
17982 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17983 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17984 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17985 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17986 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17987 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17988 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17989 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17990 tree origs1, origs2;
17991 bool lose1 = false;
17992 bool lose2 = false;
17994 /* Remove the this parameter from non-static member functions. If
17995 one is a non-static member function and the other is not a static
17996 member function, remove the first parameter from that function
17997 also. This situation occurs for operator functions where we
17998 locate both a member function (with this pointer) and non-member
17999 operator (with explicit first operand). */
18000 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18002 len--; /* LEN is the number of significant arguments for DECL1 */
18003 args1 = TREE_CHAIN (args1);
18004 if (!DECL_STATIC_FUNCTION_P (decl2))
18005 args2 = TREE_CHAIN (args2);
18007 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18009 args2 = TREE_CHAIN (args2);
18010 if (!DECL_STATIC_FUNCTION_P (decl1))
18012 len--;
18013 args1 = TREE_CHAIN (args1);
18017 /* If only one is a conversion operator, they are unordered. */
18018 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18019 return 0;
18021 /* Consider the return type for a conversion function */
18022 if (DECL_CONV_FN_P (decl1))
18024 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18025 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18026 len++;
18029 processing_template_decl++;
18031 origs1 = args1;
18032 origs2 = args2;
18034 while (len--
18035 /* Stop when an ellipsis is seen. */
18036 && args1 != NULL_TREE && args2 != NULL_TREE)
18038 tree arg1 = TREE_VALUE (args1);
18039 tree arg2 = TREE_VALUE (args2);
18040 int deduce1, deduce2;
18041 int quals1 = -1;
18042 int quals2 = -1;
18043 int ref1 = 0;
18044 int ref2 = 0;
18046 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18047 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18049 /* When both arguments are pack expansions, we need only
18050 unify the patterns themselves. */
18051 arg1 = PACK_EXPANSION_PATTERN (arg1);
18052 arg2 = PACK_EXPANSION_PATTERN (arg2);
18054 /* This is the last comparison we need to do. */
18055 len = 0;
18058 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18060 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18061 arg1 = TREE_TYPE (arg1);
18062 quals1 = cp_type_quals (arg1);
18065 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18067 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18068 arg2 = TREE_TYPE (arg2);
18069 quals2 = cp_type_quals (arg2);
18072 arg1 = TYPE_MAIN_VARIANT (arg1);
18073 arg2 = TYPE_MAIN_VARIANT (arg2);
18075 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18077 int i, len2 = list_length (args2);
18078 tree parmvec = make_tree_vec (1);
18079 tree argvec = make_tree_vec (len2);
18080 tree ta = args2;
18082 /* Setup the parameter vector, which contains only ARG1. */
18083 TREE_VEC_ELT (parmvec, 0) = arg1;
18085 /* Setup the argument vector, which contains the remaining
18086 arguments. */
18087 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18088 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18090 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18091 argvec, DEDUCE_EXACT,
18092 /*subr=*/true, /*explain_p=*/false)
18093 == 0);
18095 /* We cannot deduce in the other direction, because ARG1 is
18096 a pack expansion but ARG2 is not. */
18097 deduce2 = 0;
18099 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18101 int i, len1 = list_length (args1);
18102 tree parmvec = make_tree_vec (1);
18103 tree argvec = make_tree_vec (len1);
18104 tree ta = args1;
18106 /* Setup the parameter vector, which contains only ARG1. */
18107 TREE_VEC_ELT (parmvec, 0) = arg2;
18109 /* Setup the argument vector, which contains the remaining
18110 arguments. */
18111 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18112 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18114 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18115 argvec, DEDUCE_EXACT,
18116 /*subr=*/true, /*explain_p=*/false)
18117 == 0);
18119 /* We cannot deduce in the other direction, because ARG2 is
18120 a pack expansion but ARG1 is not.*/
18121 deduce1 = 0;
18124 else
18126 /* The normal case, where neither argument is a pack
18127 expansion. */
18128 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18129 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18130 == 0);
18131 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18132 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18133 == 0);
18136 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18137 arg2, then arg2 is not as specialized as arg1. */
18138 if (!deduce1)
18139 lose2 = true;
18140 if (!deduce2)
18141 lose1 = true;
18143 /* "If, for a given type, deduction succeeds in both directions
18144 (i.e., the types are identical after the transformations above)
18145 and both P and A were reference types (before being replaced with
18146 the type referred to above):
18147 - if the type from the argument template was an lvalue reference and
18148 the type from the parameter template was not, the argument type is
18149 considered to be more specialized than the other; otherwise,
18150 - if the type from the argument template is more cv-qualified
18151 than the type from the parameter template (as described above),
18152 the argument type is considered to be more specialized than the other;
18153 otherwise,
18154 - neither type is more specialized than the other." */
18156 if (deduce1 && deduce2)
18158 if (ref1 && ref2 && ref1 != ref2)
18160 if (ref1 > ref2)
18161 lose1 = true;
18162 else
18163 lose2 = true;
18165 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18167 if ((quals1 & quals2) == quals2)
18168 lose2 = true;
18169 if ((quals1 & quals2) == quals1)
18170 lose1 = true;
18174 if (lose1 && lose2)
18175 /* We've failed to deduce something in either direction.
18176 These must be unordered. */
18177 break;
18179 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18180 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18181 /* We have already processed all of the arguments in our
18182 handing of the pack expansion type. */
18183 len = 0;
18185 args1 = TREE_CHAIN (args1);
18186 args2 = TREE_CHAIN (args2);
18189 /* "In most cases, all template parameters must have values in order for
18190 deduction to succeed, but for partial ordering purposes a template
18191 parameter may remain without a value provided it is not used in the
18192 types being used for partial ordering."
18194 Thus, if we are missing any of the targs1 we need to substitute into
18195 origs1, then pat2 is not as specialized as pat1. This can happen when
18196 there is a nondeduced context. */
18197 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18198 lose2 = true;
18199 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18200 lose1 = true;
18202 processing_template_decl--;
18204 /* All things being equal, if the next argument is a pack expansion
18205 for one function but not for the other, prefer the
18206 non-variadic function. FIXME this is bogus; see c++/41958. */
18207 if (lose1 == lose2
18208 && args1 && TREE_VALUE (args1)
18209 && args2 && TREE_VALUE (args2))
18211 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18212 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18215 if (lose1 == lose2)
18216 return 0;
18217 else if (!lose1)
18218 return 1;
18219 else
18220 return -1;
18223 /* Determine which of two partial specializations of MAIN_TMPL is more
18224 specialized.
18226 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18227 to the first partial specialization. The TREE_VALUE is the
18228 innermost set of template parameters for the partial
18229 specialization. PAT2 is similar, but for the second template.
18231 Return 1 if the first partial specialization is more specialized;
18232 -1 if the second is more specialized; 0 if neither is more
18233 specialized.
18235 See [temp.class.order] for information about determining which of
18236 two templates is more specialized. */
18238 static int
18239 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18241 tree targs;
18242 tree tmpl1, tmpl2;
18243 int winner = 0;
18244 bool any_deductions = false;
18246 tmpl1 = TREE_TYPE (pat1);
18247 tmpl2 = TREE_TYPE (pat2);
18249 /* Just like what happens for functions, if we are ordering between
18250 different class template specializations, we may encounter dependent
18251 types in the arguments, and we need our dependency check functions
18252 to behave correctly. */
18253 ++processing_template_decl;
18254 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
18255 CLASSTYPE_TI_ARGS (tmpl1),
18256 CLASSTYPE_TI_ARGS (tmpl2));
18257 if (targs)
18259 --winner;
18260 any_deductions = true;
18263 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
18264 CLASSTYPE_TI_ARGS (tmpl2),
18265 CLASSTYPE_TI_ARGS (tmpl1));
18266 if (targs)
18268 ++winner;
18269 any_deductions = true;
18271 --processing_template_decl;
18273 /* In the case of a tie where at least one of the class templates
18274 has a parameter pack at the end, the template with the most
18275 non-packed parameters wins. */
18276 if (winner == 0
18277 && any_deductions
18278 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18279 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18281 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18282 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18283 int len1 = TREE_VEC_LENGTH (args1);
18284 int len2 = TREE_VEC_LENGTH (args2);
18286 /* We don't count the pack expansion at the end. */
18287 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18288 --len1;
18289 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18290 --len2;
18292 if (len1 > len2)
18293 return 1;
18294 else if (len1 < len2)
18295 return -1;
18298 return winner;
18301 /* Return the template arguments that will produce the function signature
18302 DECL from the function template FN, with the explicit template
18303 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18304 also match. Return NULL_TREE if no satisfactory arguments could be
18305 found. */
18307 static tree
18308 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18310 int ntparms = DECL_NTPARMS (fn);
18311 tree targs = make_tree_vec (ntparms);
18312 tree decl_type = TREE_TYPE (decl);
18313 tree decl_arg_types;
18314 tree *args;
18315 unsigned int nargs, ix;
18316 tree arg;
18318 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18320 /* Never do unification on the 'this' parameter. */
18321 decl_arg_types = skip_artificial_parms_for (decl,
18322 TYPE_ARG_TYPES (decl_type));
18324 nargs = list_length (decl_arg_types);
18325 args = XALLOCAVEC (tree, nargs);
18326 for (arg = decl_arg_types, ix = 0;
18327 arg != NULL_TREE && arg != void_list_node;
18328 arg = TREE_CHAIN (arg), ++ix)
18329 args[ix] = TREE_VALUE (arg);
18331 if (fn_type_unification (fn, explicit_args, targs,
18332 args, ix,
18333 (check_rettype || DECL_CONV_FN_P (fn)
18334 ? TREE_TYPE (decl_type) : NULL_TREE),
18335 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18336 /*decltype*/false)
18337 == error_mark_node)
18338 return NULL_TREE;
18340 return targs;
18343 /* Return the innermost template arguments that, when applied to a partial
18344 specialization of MAIN_TMPL whose innermost template parameters are
18345 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18346 ARGS.
18348 For example, suppose we have:
18350 template <class T, class U> struct S {};
18351 template <class T> struct S<T*, int> {};
18353 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18354 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18355 int}. The resulting vector will be {double}, indicating that `T'
18356 is bound to `double'. */
18358 static tree
18359 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
18361 int i, ntparms = TREE_VEC_LENGTH (tparms);
18362 tree deduced_args;
18363 tree innermost_deduced_args;
18365 innermost_deduced_args = make_tree_vec (ntparms);
18366 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18368 deduced_args = copy_node (args);
18369 SET_TMPL_ARGS_LEVEL (deduced_args,
18370 TMPL_ARGS_DEPTH (deduced_args),
18371 innermost_deduced_args);
18373 else
18374 deduced_args = innermost_deduced_args;
18376 if (unify (tparms, deduced_args,
18377 INNERMOST_TEMPLATE_ARGS (spec_args),
18378 INNERMOST_TEMPLATE_ARGS (args),
18379 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18380 return NULL_TREE;
18382 for (i = 0; i < ntparms; ++i)
18383 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18384 return NULL_TREE;
18386 /* Verify that nondeduced template arguments agree with the type
18387 obtained from argument deduction.
18389 For example:
18391 struct A { typedef int X; };
18392 template <class T, class U> struct C {};
18393 template <class T> struct C<T, typename T::X> {};
18395 Then with the instantiation `C<A, int>', we can deduce that
18396 `T' is `A' but unify () does not check whether `typename T::X'
18397 is `int'. */
18398 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18399 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18400 spec_args, main_tmpl,
18401 tf_none, false, false);
18402 if (spec_args == error_mark_node
18403 /* We only need to check the innermost arguments; the other
18404 arguments will always agree. */
18405 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18406 INNERMOST_TEMPLATE_ARGS (args)))
18407 return NULL_TREE;
18409 /* Now that we have bindings for all of the template arguments,
18410 ensure that the arguments deduced for the template template
18411 parameters have compatible template parameter lists. See the use
18412 of template_template_parm_bindings_ok_p in fn_type_unification
18413 for more information. */
18414 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18415 return NULL_TREE;
18417 return deduced_args;
18420 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18421 Return the TREE_LIST node with the most specialized template, if
18422 any. If there is no most specialized template, the error_mark_node
18423 is returned.
18425 Note that this function does not look at, or modify, the
18426 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18427 returned is one of the elements of INSTANTIATIONS, callers may
18428 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18429 and retrieve it from the value returned. */
18431 tree
18432 most_specialized_instantiation (tree templates)
18434 tree fn, champ;
18436 ++processing_template_decl;
18438 champ = templates;
18439 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18441 int fate = 0;
18443 if (get_bindings (TREE_VALUE (champ),
18444 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18445 NULL_TREE, /*check_ret=*/true))
18446 fate--;
18448 if (get_bindings (TREE_VALUE (fn),
18449 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18450 NULL_TREE, /*check_ret=*/true))
18451 fate++;
18453 if (fate == -1)
18454 champ = fn;
18455 else if (!fate)
18457 /* Equally specialized, move to next function. If there
18458 is no next function, nothing's most specialized. */
18459 fn = TREE_CHAIN (fn);
18460 champ = fn;
18461 if (!fn)
18462 break;
18466 if (champ)
18467 /* Now verify that champ is better than everything earlier in the
18468 instantiation list. */
18469 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18470 if (get_bindings (TREE_VALUE (champ),
18471 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18472 NULL_TREE, /*check_ret=*/true)
18473 || !get_bindings (TREE_VALUE (fn),
18474 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18475 NULL_TREE, /*check_ret=*/true))
18477 champ = NULL_TREE;
18478 break;
18481 processing_template_decl--;
18483 if (!champ)
18484 return error_mark_node;
18486 return champ;
18489 /* If DECL is a specialization of some template, return the most
18490 general such template. Otherwise, returns NULL_TREE.
18492 For example, given:
18494 template <class T> struct S { template <class U> void f(U); };
18496 if TMPL is `template <class U> void S<int>::f(U)' this will return
18497 the full template. This function will not trace past partial
18498 specializations, however. For example, given in addition:
18500 template <class T> struct S<T*> { template <class U> void f(U); };
18502 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18503 `template <class T> template <class U> S<T*>::f(U)'. */
18505 tree
18506 most_general_template (tree decl)
18508 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18509 an immediate specialization. */
18510 if (TREE_CODE (decl) == FUNCTION_DECL)
18512 if (DECL_TEMPLATE_INFO (decl)) {
18513 decl = DECL_TI_TEMPLATE (decl);
18515 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18516 template friend. */
18517 if (TREE_CODE (decl) != TEMPLATE_DECL)
18518 return NULL_TREE;
18519 } else
18520 return NULL_TREE;
18523 /* Look for more and more general templates. */
18524 while (DECL_TEMPLATE_INFO (decl))
18526 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18527 (See cp-tree.h for details.) */
18528 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18529 break;
18531 if (CLASS_TYPE_P (TREE_TYPE (decl))
18532 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18533 break;
18535 /* Stop if we run into an explicitly specialized class template. */
18536 if (!DECL_NAMESPACE_SCOPE_P (decl)
18537 && DECL_CONTEXT (decl)
18538 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18539 break;
18541 decl = DECL_TI_TEMPLATE (decl);
18544 return decl;
18547 /* Return the most specialized of the class template partial
18548 specializations of TMPL which can produce TYPE, a specialization of
18549 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18550 a _TYPE node corresponding to the partial specialization, while the
18551 TREE_PURPOSE is the set of template arguments that must be
18552 substituted into the TREE_TYPE in order to generate TYPE.
18554 If the choice of partial specialization is ambiguous, a diagnostic
18555 is issued, and the error_mark_node is returned. If there are no
18556 partial specializations of TMPL matching TYPE, then NULL_TREE is
18557 returned. */
18559 static tree
18560 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18562 tree list = NULL_TREE;
18563 tree t;
18564 tree champ;
18565 int fate;
18566 bool ambiguous_p;
18567 tree args;
18568 tree outer_args = NULL_TREE;
18570 tmpl = most_general_template (tmpl);
18571 args = CLASSTYPE_TI_ARGS (type);
18573 /* For determining which partial specialization to use, only the
18574 innermost args are interesting. */
18575 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18577 outer_args = strip_innermost_template_args (args, 1);
18578 args = INNERMOST_TEMPLATE_ARGS (args);
18581 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
18583 tree partial_spec_args;
18584 tree spec_args;
18585 tree spec_tmpl = TREE_VALUE (t);
18586 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18588 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18590 ++processing_template_decl;
18592 if (outer_args)
18594 /* Discard the outer levels of args, and then substitute in the
18595 template args from the enclosing class. */
18596 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18597 partial_spec_args = tsubst_template_args
18598 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18600 /* And the same for the partial specialization TEMPLATE_DECL. */
18601 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18604 partial_spec_args =
18605 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18606 add_to_template_args (outer_args,
18607 partial_spec_args),
18608 tmpl, tf_none,
18609 /*require_all_args=*/true,
18610 /*use_default_args=*/true);
18612 --processing_template_decl;
18614 if (partial_spec_args == error_mark_node)
18615 return error_mark_node;
18616 if (spec_tmpl == error_mark_node)
18617 return error_mark_node;
18619 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18620 spec_args = get_class_bindings (tmpl, parms,
18621 partial_spec_args,
18622 args);
18623 if (spec_args)
18625 if (outer_args)
18626 spec_args = add_to_template_args (outer_args, spec_args);
18627 list = tree_cons (spec_args, orig_parms, list);
18628 TREE_TYPE (list) = TREE_TYPE (t);
18632 if (! list)
18633 return NULL_TREE;
18635 ambiguous_p = false;
18636 t = list;
18637 champ = t;
18638 t = TREE_CHAIN (t);
18639 for (; t; t = TREE_CHAIN (t))
18641 fate = more_specialized_class (tmpl, champ, t);
18642 if (fate == 1)
18644 else
18646 if (fate == 0)
18648 t = TREE_CHAIN (t);
18649 if (! t)
18651 ambiguous_p = true;
18652 break;
18655 champ = t;
18659 if (!ambiguous_p)
18660 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18662 fate = more_specialized_class (tmpl, champ, t);
18663 if (fate != 1)
18665 ambiguous_p = true;
18666 break;
18670 if (ambiguous_p)
18672 const char *str;
18673 char *spaces = NULL;
18674 if (!(complain & tf_error))
18675 return error_mark_node;
18676 error ("ambiguous class template instantiation for %q#T", type);
18677 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18678 for (t = list; t; t = TREE_CHAIN (t))
18680 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18681 spaces = spaces ? spaces : get_spaces (str);
18683 free (spaces);
18684 return error_mark_node;
18687 return champ;
18690 /* Explicitly instantiate DECL. */
18692 void
18693 do_decl_instantiation (tree decl, tree storage)
18695 tree result = NULL_TREE;
18696 int extern_p = 0;
18698 if (!decl || decl == error_mark_node)
18699 /* An error occurred, for which grokdeclarator has already issued
18700 an appropriate message. */
18701 return;
18702 else if (! DECL_LANG_SPECIFIC (decl))
18704 error ("explicit instantiation of non-template %q#D", decl);
18705 return;
18707 else if (VAR_P (decl))
18709 /* There is an asymmetry here in the way VAR_DECLs and
18710 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18711 the latter, the DECL we get back will be marked as a
18712 template instantiation, and the appropriate
18713 DECL_TEMPLATE_INFO will be set up. This does not happen for
18714 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18715 should handle VAR_DECLs as it currently handles
18716 FUNCTION_DECLs. */
18717 if (!DECL_CLASS_SCOPE_P (decl))
18719 error ("%qD is not a static data member of a class template", decl);
18720 return;
18722 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18723 if (!result || !VAR_P (result))
18725 error ("no matching template for %qD found", decl);
18726 return;
18728 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18730 error ("type %qT for explicit instantiation %qD does not match "
18731 "declared type %qT", TREE_TYPE (result), decl,
18732 TREE_TYPE (decl));
18733 return;
18736 else if (TREE_CODE (decl) != FUNCTION_DECL)
18738 error ("explicit instantiation of %q#D", decl);
18739 return;
18741 else
18742 result = decl;
18744 /* Check for various error cases. Note that if the explicit
18745 instantiation is valid the RESULT will currently be marked as an
18746 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18747 until we get here. */
18749 if (DECL_TEMPLATE_SPECIALIZATION (result))
18751 /* DR 259 [temp.spec].
18753 Both an explicit instantiation and a declaration of an explicit
18754 specialization shall not appear in a program unless the explicit
18755 instantiation follows a declaration of the explicit specialization.
18757 For a given set of template parameters, if an explicit
18758 instantiation of a template appears after a declaration of an
18759 explicit specialization for that template, the explicit
18760 instantiation has no effect. */
18761 return;
18763 else if (DECL_EXPLICIT_INSTANTIATION (result))
18765 /* [temp.spec]
18767 No program shall explicitly instantiate any template more
18768 than once.
18770 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18771 the first instantiation was `extern' and the second is not,
18772 and EXTERN_P for the opposite case. */
18773 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18774 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18775 /* If an "extern" explicit instantiation follows an ordinary
18776 explicit instantiation, the template is instantiated. */
18777 if (extern_p)
18778 return;
18780 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18782 error ("no matching template for %qD found", result);
18783 return;
18785 else if (!DECL_TEMPLATE_INFO (result))
18787 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18788 return;
18791 if (storage == NULL_TREE)
18793 else if (storage == ridpointers[(int) RID_EXTERN])
18795 if (!in_system_header && (cxx_dialect == cxx98))
18796 pedwarn (input_location, OPT_Wpedantic,
18797 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18798 "instantiations");
18799 extern_p = 1;
18801 else
18802 error ("storage class %qD applied to template instantiation", storage);
18804 check_explicit_instantiation_namespace (result);
18805 mark_decl_instantiated (result, extern_p);
18806 if (! extern_p)
18807 instantiate_decl (result, /*defer_ok=*/1,
18808 /*expl_inst_class_mem_p=*/false);
18811 static void
18812 mark_class_instantiated (tree t, int extern_p)
18814 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18815 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18816 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18817 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18818 if (! extern_p)
18820 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18821 rest_of_type_compilation (t, 1);
18825 /* Called from do_type_instantiation through binding_table_foreach to
18826 do recursive instantiation for the type bound in ENTRY. */
18827 static void
18828 bt_instantiate_type_proc (binding_entry entry, void *data)
18830 tree storage = *(tree *) data;
18832 if (MAYBE_CLASS_TYPE_P (entry->type)
18833 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18834 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18837 /* Called from do_type_instantiation to instantiate a member
18838 (a member function or a static member variable) of an
18839 explicitly instantiated class template. */
18840 static void
18841 instantiate_class_member (tree decl, int extern_p)
18843 mark_decl_instantiated (decl, extern_p);
18844 if (! extern_p)
18845 instantiate_decl (decl, /*defer_ok=*/1,
18846 /*expl_inst_class_mem_p=*/true);
18849 /* Perform an explicit instantiation of template class T. STORAGE, if
18850 non-null, is the RID for extern, inline or static. COMPLAIN is
18851 nonzero if this is called from the parser, zero if called recursively,
18852 since the standard is unclear (as detailed below). */
18854 void
18855 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18857 int extern_p = 0;
18858 int nomem_p = 0;
18859 int static_p = 0;
18860 int previous_instantiation_extern_p = 0;
18862 if (TREE_CODE (t) == TYPE_DECL)
18863 t = TREE_TYPE (t);
18865 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18867 tree tmpl =
18868 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18869 if (tmpl)
18870 error ("explicit instantiation of non-class template %qD", tmpl);
18871 else
18872 error ("explicit instantiation of non-template type %qT", t);
18873 return;
18876 complete_type (t);
18878 if (!COMPLETE_TYPE_P (t))
18880 if (complain & tf_error)
18881 error ("explicit instantiation of %q#T before definition of template",
18883 return;
18886 if (storage != NULL_TREE)
18888 if (!in_system_header)
18890 if (storage == ridpointers[(int) RID_EXTERN])
18892 if (cxx_dialect == cxx98)
18893 pedwarn (input_location, OPT_Wpedantic,
18894 "ISO C++ 1998 forbids the use of %<extern%> on "
18895 "explicit instantiations");
18897 else
18898 pedwarn (input_location, OPT_Wpedantic,
18899 "ISO C++ forbids the use of %qE"
18900 " on explicit instantiations", storage);
18903 if (storage == ridpointers[(int) RID_INLINE])
18904 nomem_p = 1;
18905 else if (storage == ridpointers[(int) RID_EXTERN])
18906 extern_p = 1;
18907 else if (storage == ridpointers[(int) RID_STATIC])
18908 static_p = 1;
18909 else
18911 error ("storage class %qD applied to template instantiation",
18912 storage);
18913 extern_p = 0;
18917 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18919 /* DR 259 [temp.spec].
18921 Both an explicit instantiation and a declaration of an explicit
18922 specialization shall not appear in a program unless the explicit
18923 instantiation follows a declaration of the explicit specialization.
18925 For a given set of template parameters, if an explicit
18926 instantiation of a template appears after a declaration of an
18927 explicit specialization for that template, the explicit
18928 instantiation has no effect. */
18929 return;
18931 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18933 /* [temp.spec]
18935 No program shall explicitly instantiate any template more
18936 than once.
18938 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18939 instantiation was `extern'. If EXTERN_P then the second is.
18940 These cases are OK. */
18941 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18943 if (!previous_instantiation_extern_p && !extern_p
18944 && (complain & tf_error))
18945 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18947 /* If we've already instantiated the template, just return now. */
18948 if (!CLASSTYPE_INTERFACE_ONLY (t))
18949 return;
18952 check_explicit_instantiation_namespace (TYPE_NAME (t));
18953 mark_class_instantiated (t, extern_p);
18955 if (nomem_p)
18956 return;
18959 tree tmp;
18961 /* In contrast to implicit instantiation, where only the
18962 declarations, and not the definitions, of members are
18963 instantiated, we have here:
18965 [temp.explicit]
18967 The explicit instantiation of a class template specialization
18968 implies the instantiation of all of its members not
18969 previously explicitly specialized in the translation unit
18970 containing the explicit instantiation.
18972 Of course, we can't instantiate member template classes, since
18973 we don't have any arguments for them. Note that the standard
18974 is unclear on whether the instantiation of the members are
18975 *explicit* instantiations or not. However, the most natural
18976 interpretation is that it should be an explicit instantiation. */
18978 if (! static_p)
18979 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18980 if (TREE_CODE (tmp) == FUNCTION_DECL
18981 && DECL_TEMPLATE_INSTANTIATION (tmp))
18982 instantiate_class_member (tmp, extern_p);
18984 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18985 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
18986 instantiate_class_member (tmp, extern_p);
18988 if (CLASSTYPE_NESTED_UTDS (t))
18989 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18990 bt_instantiate_type_proc, &storage);
18994 /* Given a function DECL, which is a specialization of TMPL, modify
18995 DECL to be a re-instantiation of TMPL with the same template
18996 arguments. TMPL should be the template into which tsubst'ing
18997 should occur for DECL, not the most general template.
18999 One reason for doing this is a scenario like this:
19001 template <class T>
19002 void f(const T&, int i);
19004 void g() { f(3, 7); }
19006 template <class T>
19007 void f(const T& t, const int i) { }
19009 Note that when the template is first instantiated, with
19010 instantiate_template, the resulting DECL will have no name for the
19011 first parameter, and the wrong type for the second. So, when we go
19012 to instantiate the DECL, we regenerate it. */
19014 static void
19015 regenerate_decl_from_template (tree decl, tree tmpl)
19017 /* The arguments used to instantiate DECL, from the most general
19018 template. */
19019 tree args;
19020 tree code_pattern;
19022 args = DECL_TI_ARGS (decl);
19023 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19025 /* Make sure that we can see identifiers, and compute access
19026 correctly. */
19027 push_access_scope (decl);
19029 if (TREE_CODE (decl) == FUNCTION_DECL)
19031 tree decl_parm;
19032 tree pattern_parm;
19033 tree specs;
19034 int args_depth;
19035 int parms_depth;
19037 args_depth = TMPL_ARGS_DEPTH (args);
19038 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19039 if (args_depth > parms_depth)
19040 args = get_innermost_template_args (args, parms_depth);
19042 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19043 args, tf_error, NULL_TREE,
19044 /*defer_ok*/false);
19045 if (specs && specs != error_mark_node)
19046 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19047 specs);
19049 /* Merge parameter declarations. */
19050 decl_parm = skip_artificial_parms_for (decl,
19051 DECL_ARGUMENTS (decl));
19052 pattern_parm
19053 = skip_artificial_parms_for (code_pattern,
19054 DECL_ARGUMENTS (code_pattern));
19055 while (decl_parm && !DECL_PACK_P (pattern_parm))
19057 tree parm_type;
19058 tree attributes;
19060 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19061 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19062 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19063 NULL_TREE);
19064 parm_type = type_decays_to (parm_type);
19065 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19066 TREE_TYPE (decl_parm) = parm_type;
19067 attributes = DECL_ATTRIBUTES (pattern_parm);
19068 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19070 DECL_ATTRIBUTES (decl_parm) = attributes;
19071 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19073 decl_parm = DECL_CHAIN (decl_parm);
19074 pattern_parm = DECL_CHAIN (pattern_parm);
19076 /* Merge any parameters that match with the function parameter
19077 pack. */
19078 if (pattern_parm && DECL_PACK_P (pattern_parm))
19080 int i, len;
19081 tree expanded_types;
19082 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19083 the parameters in this function parameter pack. */
19084 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19085 args, tf_error, NULL_TREE);
19086 len = TREE_VEC_LENGTH (expanded_types);
19087 for (i = 0; i < len; i++)
19089 tree parm_type;
19090 tree attributes;
19092 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19093 /* Rename the parameter to include the index. */
19094 DECL_NAME (decl_parm) =
19095 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19096 parm_type = TREE_VEC_ELT (expanded_types, i);
19097 parm_type = type_decays_to (parm_type);
19098 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19099 TREE_TYPE (decl_parm) = parm_type;
19100 attributes = DECL_ATTRIBUTES (pattern_parm);
19101 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19103 DECL_ATTRIBUTES (decl_parm) = attributes;
19104 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19106 decl_parm = DECL_CHAIN (decl_parm);
19109 /* Merge additional specifiers from the CODE_PATTERN. */
19110 if (DECL_DECLARED_INLINE_P (code_pattern)
19111 && !DECL_DECLARED_INLINE_P (decl))
19112 DECL_DECLARED_INLINE_P (decl) = 1;
19114 else if (VAR_P (decl))
19116 DECL_INITIAL (decl) =
19117 tsubst_expr (DECL_INITIAL (code_pattern), args,
19118 tf_error, DECL_TI_TEMPLATE (decl),
19119 /*integral_constant_expression_p=*/false);
19120 if (VAR_HAD_UNKNOWN_BOUND (decl))
19121 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19122 tf_error, DECL_TI_TEMPLATE (decl));
19124 else
19125 gcc_unreachable ();
19127 pop_access_scope (decl);
19130 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19131 substituted to get DECL. */
19133 tree
19134 template_for_substitution (tree decl)
19136 tree tmpl = DECL_TI_TEMPLATE (decl);
19138 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19139 for the instantiation. This is not always the most general
19140 template. Consider, for example:
19142 template <class T>
19143 struct S { template <class U> void f();
19144 template <> void f<int>(); };
19146 and an instantiation of S<double>::f<int>. We want TD to be the
19147 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19148 while (/* An instantiation cannot have a definition, so we need a
19149 more general template. */
19150 DECL_TEMPLATE_INSTANTIATION (tmpl)
19151 /* We must also deal with friend templates. Given:
19153 template <class T> struct S {
19154 template <class U> friend void f() {};
19157 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19158 so far as the language is concerned, but that's still
19159 where we get the pattern for the instantiation from. On
19160 other hand, if the definition comes outside the class, say:
19162 template <class T> struct S {
19163 template <class U> friend void f();
19165 template <class U> friend void f() {}
19167 we don't need to look any further. That's what the check for
19168 DECL_INITIAL is for. */
19169 || (TREE_CODE (decl) == FUNCTION_DECL
19170 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19171 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19173 /* The present template, TD, should not be a definition. If it
19174 were a definition, we should be using it! Note that we
19175 cannot restructure the loop to just keep going until we find
19176 a template with a definition, since that might go too far if
19177 a specialization was declared, but not defined. */
19178 gcc_assert (!VAR_P (decl)
19179 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19181 /* Fetch the more general template. */
19182 tmpl = DECL_TI_TEMPLATE (tmpl);
19185 return tmpl;
19188 /* Returns true if we need to instantiate this template instance even if we
19189 know we aren't going to emit it.. */
19191 bool
19192 always_instantiate_p (tree decl)
19194 /* We always instantiate inline functions so that we can inline them. An
19195 explicit instantiation declaration prohibits implicit instantiation of
19196 non-inline functions. With high levels of optimization, we would
19197 normally inline non-inline functions -- but we're not allowed to do
19198 that for "extern template" functions. Therefore, we check
19199 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19200 return ((TREE_CODE (decl) == FUNCTION_DECL
19201 && (DECL_DECLARED_INLINE_P (decl)
19202 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19203 /* And we need to instantiate static data members so that
19204 their initializers are available in integral constant
19205 expressions. */
19206 || (VAR_P (decl)
19207 && decl_maybe_constant_var_p (decl)));
19210 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19211 instantiate it now, modifying TREE_TYPE (fn). */
19213 void
19214 maybe_instantiate_noexcept (tree fn)
19216 tree fntype, spec, noex, clone;
19218 if (DECL_CLONED_FUNCTION_P (fn))
19219 fn = DECL_CLONED_FUNCTION (fn);
19220 fntype = TREE_TYPE (fn);
19221 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19223 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19224 return;
19226 noex = TREE_PURPOSE (spec);
19228 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19230 if (push_tinst_level (fn))
19232 push_access_scope (fn);
19233 push_deferring_access_checks (dk_no_deferred);
19234 input_location = DECL_SOURCE_LOCATION (fn);
19235 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19236 DEFERRED_NOEXCEPT_ARGS (noex),
19237 tf_warning_or_error, fn,
19238 /*function_p=*/false,
19239 /*integral_constant_expression_p=*/true);
19240 pop_deferring_access_checks ();
19241 pop_access_scope (fn);
19242 pop_tinst_level ();
19243 spec = build_noexcept_spec (noex, tf_warning_or_error);
19244 if (spec == error_mark_node)
19245 spec = noexcept_false_spec;
19247 else
19248 spec = noexcept_false_spec;
19250 else
19252 /* This is an implicitly declared function, so NOEX is a list of
19253 other functions to evaluate and merge. */
19254 tree elt;
19255 spec = noexcept_true_spec;
19256 for (elt = noex; elt; elt = OVL_NEXT (elt))
19258 tree fn = OVL_CURRENT (elt);
19259 tree subspec;
19260 maybe_instantiate_noexcept (fn);
19261 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19262 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19266 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19268 FOR_EACH_CLONE (clone, fn)
19270 if (TREE_TYPE (clone) == fntype)
19271 TREE_TYPE (clone) = TREE_TYPE (fn);
19272 else
19273 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19277 /* Produce the definition of D, a _DECL generated from a template. If
19278 DEFER_OK is nonzero, then we don't have to actually do the
19279 instantiation now; we just have to do it sometime. Normally it is
19280 an error if this is an explicit instantiation but D is undefined.
19281 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19282 explicitly instantiated class template. */
19284 tree
19285 instantiate_decl (tree d, int defer_ok,
19286 bool expl_inst_class_mem_p)
19288 tree tmpl = DECL_TI_TEMPLATE (d);
19289 tree gen_args;
19290 tree args;
19291 tree td;
19292 tree code_pattern;
19293 tree spec;
19294 tree gen_tmpl;
19295 bool pattern_defined;
19296 location_t saved_loc = input_location;
19297 int saved_unevaluated_operand = cp_unevaluated_operand;
19298 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19299 bool external_p;
19300 tree fn_context;
19301 bool nested;
19303 /* This function should only be used to instantiate templates for
19304 functions and static member variables. */
19305 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19307 /* Variables are never deferred; if instantiation is required, they
19308 are instantiated right away. That allows for better code in the
19309 case that an expression refers to the value of the variable --
19310 if the variable has a constant value the referring expression can
19311 take advantage of that fact. */
19312 if (VAR_P (d)
19313 || DECL_DECLARED_CONSTEXPR_P (d))
19314 defer_ok = 0;
19316 /* Don't instantiate cloned functions. Instead, instantiate the
19317 functions they cloned. */
19318 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19319 d = DECL_CLONED_FUNCTION (d);
19321 if (DECL_TEMPLATE_INSTANTIATED (d)
19322 || (TREE_CODE (d) == FUNCTION_DECL
19323 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19324 || DECL_TEMPLATE_SPECIALIZATION (d))
19325 /* D has already been instantiated or explicitly specialized, so
19326 there's nothing for us to do here.
19328 It might seem reasonable to check whether or not D is an explicit
19329 instantiation, and, if so, stop here. But when an explicit
19330 instantiation is deferred until the end of the compilation,
19331 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19332 the instantiation. */
19333 return d;
19335 /* Check to see whether we know that this template will be
19336 instantiated in some other file, as with "extern template"
19337 extension. */
19338 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19340 /* In general, we do not instantiate such templates. */
19341 if (external_p && !always_instantiate_p (d))
19342 return d;
19344 gen_tmpl = most_general_template (tmpl);
19345 gen_args = DECL_TI_ARGS (d);
19347 if (tmpl != gen_tmpl)
19348 /* We should already have the extra args. */
19349 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19350 == TMPL_ARGS_DEPTH (gen_args));
19351 /* And what's in the hash table should match D. */
19352 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19353 || spec == NULL_TREE);
19355 /* This needs to happen before any tsubsting. */
19356 if (! push_tinst_level (d))
19357 return d;
19359 timevar_push (TV_TEMPLATE_INST);
19361 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19362 for the instantiation. */
19363 td = template_for_substitution (d);
19364 code_pattern = DECL_TEMPLATE_RESULT (td);
19366 /* We should never be trying to instantiate a member of a class
19367 template or partial specialization. */
19368 gcc_assert (d != code_pattern);
19370 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19371 || DECL_TEMPLATE_SPECIALIZATION (td))
19372 /* In the case of a friend template whose definition is provided
19373 outside the class, we may have too many arguments. Drop the
19374 ones we don't need. The same is true for specializations. */
19375 args = get_innermost_template_args
19376 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19377 else
19378 args = gen_args;
19380 if (TREE_CODE (d) == FUNCTION_DECL)
19381 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19382 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19383 else
19384 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19386 /* We may be in the middle of deferred access check. Disable it now. */
19387 push_deferring_access_checks (dk_no_deferred);
19389 /* Unless an explicit instantiation directive has already determined
19390 the linkage of D, remember that a definition is available for
19391 this entity. */
19392 if (pattern_defined
19393 && !DECL_INTERFACE_KNOWN (d)
19394 && !DECL_NOT_REALLY_EXTERN (d))
19395 mark_definable (d);
19397 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19398 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19399 input_location = DECL_SOURCE_LOCATION (d);
19401 /* If D is a member of an explicitly instantiated class template,
19402 and no definition is available, treat it like an implicit
19403 instantiation. */
19404 if (!pattern_defined && expl_inst_class_mem_p
19405 && DECL_EXPLICIT_INSTANTIATION (d))
19407 /* Leave linkage flags alone on instantiations with anonymous
19408 visibility. */
19409 if (TREE_PUBLIC (d))
19411 DECL_NOT_REALLY_EXTERN (d) = 0;
19412 DECL_INTERFACE_KNOWN (d) = 0;
19414 SET_DECL_IMPLICIT_INSTANTIATION (d);
19417 if (TREE_CODE (d) == FUNCTION_DECL)
19418 maybe_instantiate_noexcept (d);
19420 /* Defer all other templates, unless we have been explicitly
19421 forbidden from doing so. */
19422 if (/* If there is no definition, we cannot instantiate the
19423 template. */
19424 ! pattern_defined
19425 /* If it's OK to postpone instantiation, do so. */
19426 || defer_ok
19427 /* If this is a static data member that will be defined
19428 elsewhere, we don't want to instantiate the entire data
19429 member, but we do want to instantiate the initializer so that
19430 we can substitute that elsewhere. */
19431 || (external_p && VAR_P (d)))
19433 /* The definition of the static data member is now required so
19434 we must substitute the initializer. */
19435 if (VAR_P (d)
19436 && !DECL_INITIAL (d)
19437 && DECL_INITIAL (code_pattern))
19439 tree ns;
19440 tree init;
19441 bool const_init = false;
19443 ns = decl_namespace_context (d);
19444 push_nested_namespace (ns);
19445 push_nested_class (DECL_CONTEXT (d));
19446 init = tsubst_expr (DECL_INITIAL (code_pattern),
19447 args,
19448 tf_warning_or_error, NULL_TREE,
19449 /*integral_constant_expression_p=*/false);
19450 /* Make sure the initializer is still constant, in case of
19451 circular dependency (template/instantiate6.C). */
19452 const_init
19453 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19454 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19455 /*asmspec_tree=*/NULL_TREE,
19456 LOOKUP_ONLYCONVERTING);
19457 pop_nested_class ();
19458 pop_nested_namespace (ns);
19461 /* We restore the source position here because it's used by
19462 add_pending_template. */
19463 input_location = saved_loc;
19465 if (at_eof && !pattern_defined
19466 && DECL_EXPLICIT_INSTANTIATION (d)
19467 && DECL_NOT_REALLY_EXTERN (d))
19468 /* [temp.explicit]
19470 The definition of a non-exported function template, a
19471 non-exported member function template, or a non-exported
19472 member function or static data member of a class template
19473 shall be present in every translation unit in which it is
19474 explicitly instantiated. */
19475 permerror (input_location, "explicit instantiation of %qD "
19476 "but no definition available", d);
19478 /* If we're in unevaluated context, we just wanted to get the
19479 constant value; this isn't an odr use, so don't queue
19480 a full instantiation. */
19481 if (cp_unevaluated_operand != 0)
19482 goto out;
19483 /* ??? Historically, we have instantiated inline functions, even
19484 when marked as "extern template". */
19485 if (!(external_p && VAR_P (d)))
19486 add_pending_template (d);
19487 goto out;
19489 /* Tell the repository that D is available in this translation unit
19490 -- and see if it is supposed to be instantiated here. */
19491 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19493 /* In a PCH file, despite the fact that the repository hasn't
19494 requested instantiation in the PCH it is still possible that
19495 an instantiation will be required in a file that includes the
19496 PCH. */
19497 if (pch_file)
19498 add_pending_template (d);
19499 /* Instantiate inline functions so that the inliner can do its
19500 job, even though we'll not be emitting a copy of this
19501 function. */
19502 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19503 goto out;
19506 fn_context = decl_function_context (d);
19507 nested = (current_function_decl != NULL_TREE);
19508 if (!fn_context)
19509 push_to_top_level ();
19510 else
19512 if (nested)
19513 push_function_context ();
19514 cp_unevaluated_operand = 0;
19515 c_inhibit_evaluation_warnings = 0;
19518 /* Mark D as instantiated so that recursive calls to
19519 instantiate_decl do not try to instantiate it again. */
19520 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19522 /* Regenerate the declaration in case the template has been modified
19523 by a subsequent redeclaration. */
19524 regenerate_decl_from_template (d, td);
19526 /* We already set the file and line above. Reset them now in case
19527 they changed as a result of calling regenerate_decl_from_template. */
19528 input_location = DECL_SOURCE_LOCATION (d);
19530 if (VAR_P (d))
19532 tree init;
19533 bool const_init = false;
19535 /* Clear out DECL_RTL; whatever was there before may not be right
19536 since we've reset the type of the declaration. */
19537 SET_DECL_RTL (d, NULL);
19538 DECL_IN_AGGR_P (d) = 0;
19540 /* The initializer is placed in DECL_INITIAL by
19541 regenerate_decl_from_template so we don't need to
19542 push/pop_access_scope again here. Pull it out so that
19543 cp_finish_decl can process it. */
19544 init = DECL_INITIAL (d);
19545 DECL_INITIAL (d) = NULL_TREE;
19546 DECL_INITIALIZED_P (d) = 0;
19548 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19549 initializer. That function will defer actual emission until
19550 we have a chance to determine linkage. */
19551 DECL_EXTERNAL (d) = 0;
19553 /* Enter the scope of D so that access-checking works correctly. */
19554 push_nested_class (DECL_CONTEXT (d));
19555 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19556 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19557 pop_nested_class ();
19559 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19560 synthesize_method (d);
19561 else if (TREE_CODE (d) == FUNCTION_DECL)
19563 struct pointer_map_t *saved_local_specializations;
19564 tree subst_decl;
19565 tree tmpl_parm;
19566 tree spec_parm;
19567 tree block = NULL_TREE;
19569 /* Save away the current list, in case we are instantiating one
19570 template from within the body of another. */
19571 saved_local_specializations = local_specializations;
19573 /* Set up the list of local specializations. */
19574 local_specializations = pointer_map_create ();
19576 /* Set up context. */
19577 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19578 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19579 block = push_stmt_list ();
19580 else
19581 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19583 /* Some typedefs referenced from within the template code need to be
19584 access checked at template instantiation time, i.e now. These
19585 types were added to the template at parsing time. Let's get those
19586 and perform the access checks then. */
19587 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19588 gen_args);
19590 /* Create substitution entries for the parameters. */
19591 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19592 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19593 spec_parm = DECL_ARGUMENTS (d);
19594 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19596 register_local_specialization (spec_parm, tmpl_parm);
19597 spec_parm = skip_artificial_parms_for (d, spec_parm);
19598 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19600 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19602 if (!DECL_PACK_P (tmpl_parm))
19604 register_local_specialization (spec_parm, tmpl_parm);
19605 spec_parm = DECL_CHAIN (spec_parm);
19607 else
19609 /* Register the (value) argument pack as a specialization of
19610 TMPL_PARM, then move on. */
19611 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19612 register_local_specialization (argpack, tmpl_parm);
19615 gcc_assert (!spec_parm);
19617 /* Substitute into the body of the function. */
19618 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19619 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19620 tf_warning_or_error, tmpl);
19621 else
19623 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19624 tf_warning_or_error, tmpl,
19625 /*integral_constant_expression_p=*/false);
19627 /* Set the current input_location to the end of the function
19628 so that finish_function knows where we are. */
19629 input_location
19630 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19633 /* We don't need the local specializations any more. */
19634 pointer_map_destroy (local_specializations);
19635 local_specializations = saved_local_specializations;
19637 /* Finish the function. */
19638 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19639 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19640 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19641 else
19643 d = finish_function (0);
19644 expand_or_defer_fn (d);
19647 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19648 cp_check_omp_declare_reduction (d);
19651 /* We're not deferring instantiation any more. */
19652 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19654 if (!fn_context)
19655 pop_from_top_level ();
19656 else if (nested)
19657 pop_function_context ();
19659 out:
19660 input_location = saved_loc;
19661 cp_unevaluated_operand = saved_unevaluated_operand;
19662 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19663 pop_deferring_access_checks ();
19664 pop_tinst_level ();
19666 timevar_pop (TV_TEMPLATE_INST);
19668 return d;
19671 /* Run through the list of templates that we wish we could
19672 instantiate, and instantiate any we can. RETRIES is the
19673 number of times we retry pending template instantiation. */
19675 void
19676 instantiate_pending_templates (int retries)
19678 int reconsider;
19679 location_t saved_loc = input_location;
19681 /* Instantiating templates may trigger vtable generation. This in turn
19682 may require further template instantiations. We place a limit here
19683 to avoid infinite loop. */
19684 if (pending_templates && retries >= max_tinst_depth)
19686 tree decl = pending_templates->tinst->decl;
19688 error ("template instantiation depth exceeds maximum of %d"
19689 " instantiating %q+D, possibly from virtual table generation"
19690 " (use -ftemplate-depth= to increase the maximum)",
19691 max_tinst_depth, decl);
19692 if (TREE_CODE (decl) == FUNCTION_DECL)
19693 /* Pretend that we defined it. */
19694 DECL_INITIAL (decl) = error_mark_node;
19695 return;
19700 struct pending_template **t = &pending_templates;
19701 struct pending_template *last = NULL;
19702 reconsider = 0;
19703 while (*t)
19705 tree instantiation = reopen_tinst_level ((*t)->tinst);
19706 bool complete = false;
19708 if (TYPE_P (instantiation))
19710 tree fn;
19712 if (!COMPLETE_TYPE_P (instantiation))
19714 instantiate_class_template (instantiation);
19715 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19716 for (fn = TYPE_METHODS (instantiation);
19718 fn = TREE_CHAIN (fn))
19719 if (! DECL_ARTIFICIAL (fn))
19720 instantiate_decl (fn,
19721 /*defer_ok=*/0,
19722 /*expl_inst_class_mem_p=*/false);
19723 if (COMPLETE_TYPE_P (instantiation))
19724 reconsider = 1;
19727 complete = COMPLETE_TYPE_P (instantiation);
19729 else
19731 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19732 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19734 instantiation
19735 = instantiate_decl (instantiation,
19736 /*defer_ok=*/0,
19737 /*expl_inst_class_mem_p=*/false);
19738 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19739 reconsider = 1;
19742 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19743 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19746 if (complete)
19747 /* If INSTANTIATION has been instantiated, then we don't
19748 need to consider it again in the future. */
19749 *t = (*t)->next;
19750 else
19752 last = *t;
19753 t = &(*t)->next;
19755 tinst_depth = 0;
19756 current_tinst_level = NULL;
19758 last_pending_template = last;
19760 while (reconsider);
19762 input_location = saved_loc;
19765 /* Substitute ARGVEC into T, which is a list of initializers for
19766 either base class or a non-static data member. The TREE_PURPOSEs
19767 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19768 instantiate_decl. */
19770 static tree
19771 tsubst_initializer_list (tree t, tree argvec)
19773 tree inits = NULL_TREE;
19775 for (; t; t = TREE_CHAIN (t))
19777 tree decl;
19778 tree init;
19779 tree expanded_bases = NULL_TREE;
19780 tree expanded_arguments = NULL_TREE;
19781 int i, len = 1;
19783 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19785 tree expr;
19786 tree arg;
19788 /* Expand the base class expansion type into separate base
19789 classes. */
19790 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19791 tf_warning_or_error,
19792 NULL_TREE);
19793 if (expanded_bases == error_mark_node)
19794 continue;
19796 /* We'll be building separate TREE_LISTs of arguments for
19797 each base. */
19798 len = TREE_VEC_LENGTH (expanded_bases);
19799 expanded_arguments = make_tree_vec (len);
19800 for (i = 0; i < len; i++)
19801 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19803 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19804 expand each argument in the TREE_VALUE of t. */
19805 expr = make_node (EXPR_PACK_EXPANSION);
19806 PACK_EXPANSION_LOCAL_P (expr) = true;
19807 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19808 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19810 if (TREE_VALUE (t) == void_type_node)
19811 /* VOID_TYPE_NODE is used to indicate
19812 value-initialization. */
19814 for (i = 0; i < len; i++)
19815 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19817 else
19819 /* Substitute parameter packs into each argument in the
19820 TREE_LIST. */
19821 in_base_initializer = 1;
19822 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19824 tree expanded_exprs;
19826 /* Expand the argument. */
19827 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19828 expanded_exprs
19829 = tsubst_pack_expansion (expr, argvec,
19830 tf_warning_or_error,
19831 NULL_TREE);
19832 if (expanded_exprs == error_mark_node)
19833 continue;
19835 /* Prepend each of the expanded expressions to the
19836 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19837 for (i = 0; i < len; i++)
19839 TREE_VEC_ELT (expanded_arguments, i) =
19840 tree_cons (NULL_TREE,
19841 TREE_VEC_ELT (expanded_exprs, i),
19842 TREE_VEC_ELT (expanded_arguments, i));
19845 in_base_initializer = 0;
19847 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19848 since we built them backwards. */
19849 for (i = 0; i < len; i++)
19851 TREE_VEC_ELT (expanded_arguments, i) =
19852 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19857 for (i = 0; i < len; ++i)
19859 if (expanded_bases)
19861 decl = TREE_VEC_ELT (expanded_bases, i);
19862 decl = expand_member_init (decl);
19863 init = TREE_VEC_ELT (expanded_arguments, i);
19865 else
19867 tree tmp;
19868 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19869 tf_warning_or_error, NULL_TREE);
19871 decl = expand_member_init (decl);
19872 if (decl && !DECL_P (decl))
19873 in_base_initializer = 1;
19875 init = TREE_VALUE (t);
19876 tmp = init;
19877 if (init != void_type_node)
19878 init = tsubst_expr (init, argvec,
19879 tf_warning_or_error, NULL_TREE,
19880 /*integral_constant_expression_p=*/false);
19881 if (init == NULL_TREE && tmp != NULL_TREE)
19882 /* If we had an initializer but it instantiated to nothing,
19883 value-initialize the object. This will only occur when
19884 the initializer was a pack expansion where the parameter
19885 packs used in that expansion were of length zero. */
19886 init = void_type_node;
19887 in_base_initializer = 0;
19890 if (decl)
19892 init = build_tree_list (decl, init);
19893 TREE_CHAIN (init) = inits;
19894 inits = init;
19898 return inits;
19901 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19903 static void
19904 set_current_access_from_decl (tree decl)
19906 if (TREE_PRIVATE (decl))
19907 current_access_specifier = access_private_node;
19908 else if (TREE_PROTECTED (decl))
19909 current_access_specifier = access_protected_node;
19910 else
19911 current_access_specifier = access_public_node;
19914 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19915 is the instantiation (which should have been created with
19916 start_enum) and ARGS are the template arguments to use. */
19918 static void
19919 tsubst_enum (tree tag, tree newtag, tree args)
19921 tree e;
19923 if (SCOPED_ENUM_P (newtag))
19924 begin_scope (sk_scoped_enum, newtag);
19926 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19928 tree value;
19929 tree decl;
19931 decl = TREE_VALUE (e);
19932 /* Note that in a template enum, the TREE_VALUE is the
19933 CONST_DECL, not the corresponding INTEGER_CST. */
19934 value = tsubst_expr (DECL_INITIAL (decl),
19935 args, tf_warning_or_error, NULL_TREE,
19936 /*integral_constant_expression_p=*/true);
19938 /* Give this enumeration constant the correct access. */
19939 set_current_access_from_decl (decl);
19941 /* Actually build the enumerator itself. */
19942 build_enumerator
19943 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19946 if (SCOPED_ENUM_P (newtag))
19947 finish_scope ();
19949 finish_enum_value_list (newtag);
19950 finish_enum (newtag);
19952 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19953 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19956 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19957 its type -- but without substituting the innermost set of template
19958 arguments. So, innermost set of template parameters will appear in
19959 the type. */
19961 tree
19962 get_mostly_instantiated_function_type (tree decl)
19964 tree fn_type;
19965 tree tmpl;
19966 tree targs;
19967 tree tparms;
19968 int parm_depth;
19970 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19971 targs = DECL_TI_ARGS (decl);
19972 tparms = DECL_TEMPLATE_PARMS (tmpl);
19973 parm_depth = TMPL_PARMS_DEPTH (tparms);
19975 /* There should be as many levels of arguments as there are levels
19976 of parameters. */
19977 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19979 fn_type = TREE_TYPE (tmpl);
19981 if (parm_depth == 1)
19982 /* No substitution is necessary. */
19984 else
19986 int i;
19987 tree partial_args;
19989 /* Replace the innermost level of the TARGS with NULL_TREEs to
19990 let tsubst know not to substitute for those parameters. */
19991 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19992 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19993 SET_TMPL_ARGS_LEVEL (partial_args, i,
19994 TMPL_ARGS_LEVEL (targs, i));
19995 SET_TMPL_ARGS_LEVEL (partial_args,
19996 TMPL_ARGS_DEPTH (targs),
19997 make_tree_vec (DECL_NTPARMS (tmpl)));
19999 /* Make sure that we can see identifiers, and compute access
20000 correctly. */
20001 push_access_scope (decl);
20003 ++processing_template_decl;
20004 /* Now, do the (partial) substitution to figure out the
20005 appropriate function type. */
20006 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20007 --processing_template_decl;
20009 /* Substitute into the template parameters to obtain the real
20010 innermost set of parameters. This step is important if the
20011 innermost set of template parameters contains value
20012 parameters whose types depend on outer template parameters. */
20013 TREE_VEC_LENGTH (partial_args)--;
20014 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20016 pop_access_scope (decl);
20019 return fn_type;
20022 /* Return truthvalue if we're processing a template different from
20023 the last one involved in diagnostics. */
20025 problematic_instantiation_changed (void)
20027 return current_tinst_level != last_error_tinst_level;
20030 /* Remember current template involved in diagnostics. */
20031 void
20032 record_last_problematic_instantiation (void)
20034 last_error_tinst_level = current_tinst_level;
20037 struct tinst_level *
20038 current_instantiation (void)
20040 return current_tinst_level;
20043 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20044 type. Return zero for ok, nonzero for disallowed. Issue error and
20045 warning messages under control of COMPLAIN. */
20047 static int
20048 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20050 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20051 return 0;
20052 else if (POINTER_TYPE_P (type))
20053 return 0;
20054 else if (TYPE_PTRMEM_P (type))
20055 return 0;
20056 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20057 return 0;
20058 else if (TREE_CODE (type) == TYPENAME_TYPE)
20059 return 0;
20060 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20061 return 0;
20062 else if (TREE_CODE (type) == NULLPTR_TYPE)
20063 return 0;
20065 if (complain & tf_error)
20067 if (type == error_mark_node)
20068 inform (input_location, "invalid template non-type parameter");
20069 else
20070 error ("%q#T is not a valid type for a template non-type parameter",
20071 type);
20073 return 1;
20076 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20077 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20079 static bool
20080 dependent_type_p_r (tree type)
20082 tree scope;
20084 /* [temp.dep.type]
20086 A type is dependent if it is:
20088 -- a template parameter. Template template parameters are types
20089 for us (since TYPE_P holds true for them) so we handle
20090 them here. */
20091 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20092 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20093 return true;
20094 /* -- a qualified-id with a nested-name-specifier which contains a
20095 class-name that names a dependent type or whose unqualified-id
20096 names a dependent type. */
20097 if (TREE_CODE (type) == TYPENAME_TYPE)
20098 return true;
20099 /* -- a cv-qualified type where the cv-unqualified type is
20100 dependent. */
20101 type = TYPE_MAIN_VARIANT (type);
20102 /* -- a compound type constructed from any dependent type. */
20103 if (TYPE_PTRMEM_P (type))
20104 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20105 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20106 (type)));
20107 else if (TYPE_PTR_P (type)
20108 || TREE_CODE (type) == REFERENCE_TYPE)
20109 return dependent_type_p (TREE_TYPE (type));
20110 else if (TREE_CODE (type) == FUNCTION_TYPE
20111 || TREE_CODE (type) == METHOD_TYPE)
20113 tree arg_type;
20115 if (dependent_type_p (TREE_TYPE (type)))
20116 return true;
20117 for (arg_type = TYPE_ARG_TYPES (type);
20118 arg_type;
20119 arg_type = TREE_CHAIN (arg_type))
20120 if (dependent_type_p (TREE_VALUE (arg_type)))
20121 return true;
20122 return false;
20124 /* -- an array type constructed from any dependent type or whose
20125 size is specified by a constant expression that is
20126 value-dependent.
20128 We checked for type- and value-dependence of the bounds in
20129 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20130 if (TREE_CODE (type) == ARRAY_TYPE)
20132 if (TYPE_DOMAIN (type)
20133 && dependent_type_p (TYPE_DOMAIN (type)))
20134 return true;
20135 return dependent_type_p (TREE_TYPE (type));
20138 /* -- a template-id in which either the template name is a template
20139 parameter ... */
20140 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20141 return true;
20142 /* ... or any of the template arguments is a dependent type or
20143 an expression that is type-dependent or value-dependent. */
20144 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20145 && (any_dependent_template_arguments_p
20146 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20147 return true;
20149 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20150 dependent; if the argument of the `typeof' expression is not
20151 type-dependent, then it should already been have resolved. */
20152 if (TREE_CODE (type) == TYPEOF_TYPE
20153 || TREE_CODE (type) == DECLTYPE_TYPE
20154 || TREE_CODE (type) == UNDERLYING_TYPE)
20155 return true;
20157 /* A template argument pack is dependent if any of its packed
20158 arguments are. */
20159 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20161 tree args = ARGUMENT_PACK_ARGS (type);
20162 int i, len = TREE_VEC_LENGTH (args);
20163 for (i = 0; i < len; ++i)
20164 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20165 return true;
20168 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20169 be template parameters. */
20170 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20171 return true;
20173 /* The standard does not specifically mention types that are local
20174 to template functions or local classes, but they should be
20175 considered dependent too. For example:
20177 template <int I> void f() {
20178 enum E { a = I };
20179 S<sizeof (E)> s;
20182 The size of `E' cannot be known until the value of `I' has been
20183 determined. Therefore, `E' must be considered dependent. */
20184 scope = TYPE_CONTEXT (type);
20185 if (scope && TYPE_P (scope))
20186 return dependent_type_p (scope);
20187 /* Don't use type_dependent_expression_p here, as it can lead
20188 to infinite recursion trying to determine whether a lambda
20189 nested in a lambda is dependent (c++/47687). */
20190 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20191 && DECL_LANG_SPECIFIC (scope)
20192 && DECL_TEMPLATE_INFO (scope)
20193 && (any_dependent_template_arguments_p
20194 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20195 return true;
20197 /* Other types are non-dependent. */
20198 return false;
20201 /* Returns TRUE if TYPE is dependent, in the sense of
20202 [temp.dep.type]. Note that a NULL type is considered dependent. */
20204 bool
20205 dependent_type_p (tree type)
20207 /* If there are no template parameters in scope, then there can't be
20208 any dependent types. */
20209 if (!processing_template_decl)
20211 /* If we are not processing a template, then nobody should be
20212 providing us with a dependent type. */
20213 gcc_assert (type);
20214 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20215 return false;
20218 /* If the type is NULL, we have not computed a type for the entity
20219 in question; in that case, the type is dependent. */
20220 if (!type)
20221 return true;
20223 /* Erroneous types can be considered non-dependent. */
20224 if (type == error_mark_node)
20225 return false;
20227 /* If we have not already computed the appropriate value for TYPE,
20228 do so now. */
20229 if (!TYPE_DEPENDENT_P_VALID (type))
20231 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20232 TYPE_DEPENDENT_P_VALID (type) = 1;
20235 return TYPE_DEPENDENT_P (type);
20238 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20239 lookup. In other words, a dependent type that is not the current
20240 instantiation. */
20242 bool
20243 dependent_scope_p (tree scope)
20245 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20246 && !currently_open_class (scope));
20249 /* T is a SCOPE_REF; return whether we need to consider it
20250 instantiation-dependent so that we can check access at instantiation
20251 time even though we know which member it resolves to. */
20253 static bool
20254 instantiation_dependent_scope_ref_p (tree t)
20256 if (DECL_P (TREE_OPERAND (t, 1))
20257 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20258 && accessible_in_template_p (TREE_OPERAND (t, 0),
20259 TREE_OPERAND (t, 1)))
20260 return false;
20261 else
20262 return true;
20265 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20266 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20267 expression. */
20269 /* Note that this predicate is not appropriate for general expressions;
20270 only constant expressions (that satisfy potential_constant_expression)
20271 can be tested for value dependence. */
20273 bool
20274 value_dependent_expression_p (tree expression)
20276 if (!processing_template_decl)
20277 return false;
20279 /* A name declared with a dependent type. */
20280 if (DECL_P (expression) && type_dependent_expression_p (expression))
20281 return true;
20283 switch (TREE_CODE (expression))
20285 case IDENTIFIER_NODE:
20286 /* A name that has not been looked up -- must be dependent. */
20287 return true;
20289 case TEMPLATE_PARM_INDEX:
20290 /* A non-type template parm. */
20291 return true;
20293 case CONST_DECL:
20294 /* A non-type template parm. */
20295 if (DECL_TEMPLATE_PARM_P (expression))
20296 return true;
20297 return value_dependent_expression_p (DECL_INITIAL (expression));
20299 case VAR_DECL:
20300 /* A constant with literal type and is initialized
20301 with an expression that is value-dependent.
20303 Note that a non-dependent parenthesized initializer will have
20304 already been replaced with its constant value, so if we see
20305 a TREE_LIST it must be dependent. */
20306 if (DECL_INITIAL (expression)
20307 && decl_constant_var_p (expression)
20308 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20309 || value_dependent_expression_p (DECL_INITIAL (expression))))
20310 return true;
20311 return false;
20313 case DYNAMIC_CAST_EXPR:
20314 case STATIC_CAST_EXPR:
20315 case CONST_CAST_EXPR:
20316 case REINTERPRET_CAST_EXPR:
20317 case CAST_EXPR:
20318 /* These expressions are value-dependent if the type to which
20319 the cast occurs is dependent or the expression being casted
20320 is value-dependent. */
20322 tree type = TREE_TYPE (expression);
20324 if (dependent_type_p (type))
20325 return true;
20327 /* A functional cast has a list of operands. */
20328 expression = TREE_OPERAND (expression, 0);
20329 if (!expression)
20331 /* If there are no operands, it must be an expression such
20332 as "int()". This should not happen for aggregate types
20333 because it would form non-constant expressions. */
20334 gcc_assert (cxx_dialect >= cxx11
20335 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20337 return false;
20340 if (TREE_CODE (expression) == TREE_LIST)
20341 return any_value_dependent_elements_p (expression);
20343 return value_dependent_expression_p (expression);
20346 case SIZEOF_EXPR:
20347 if (SIZEOF_EXPR_TYPE_P (expression))
20348 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20349 /* FALLTHRU */
20350 case ALIGNOF_EXPR:
20351 case TYPEID_EXPR:
20352 /* A `sizeof' expression is value-dependent if the operand is
20353 type-dependent or is a pack expansion. */
20354 expression = TREE_OPERAND (expression, 0);
20355 if (PACK_EXPANSION_P (expression))
20356 return true;
20357 else if (TYPE_P (expression))
20358 return dependent_type_p (expression);
20359 return instantiation_dependent_expression_p (expression);
20361 case AT_ENCODE_EXPR:
20362 /* An 'encode' expression is value-dependent if the operand is
20363 type-dependent. */
20364 expression = TREE_OPERAND (expression, 0);
20365 return dependent_type_p (expression);
20367 case NOEXCEPT_EXPR:
20368 expression = TREE_OPERAND (expression, 0);
20369 return instantiation_dependent_expression_p (expression);
20371 case SCOPE_REF:
20372 /* All instantiation-dependent expressions should also be considered
20373 value-dependent. */
20374 return instantiation_dependent_scope_ref_p (expression);
20376 case COMPONENT_REF:
20377 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20378 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20380 case NONTYPE_ARGUMENT_PACK:
20381 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20382 is value-dependent. */
20384 tree values = ARGUMENT_PACK_ARGS (expression);
20385 int i, len = TREE_VEC_LENGTH (values);
20387 for (i = 0; i < len; ++i)
20388 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20389 return true;
20391 return false;
20394 case TRAIT_EXPR:
20396 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20397 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20398 || (type2 ? dependent_type_p (type2) : false));
20401 case MODOP_EXPR:
20402 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20403 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20405 case ARRAY_REF:
20406 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20407 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20409 case ADDR_EXPR:
20411 tree op = TREE_OPERAND (expression, 0);
20412 return (value_dependent_expression_p (op)
20413 || has_value_dependent_address (op));
20416 case CALL_EXPR:
20418 tree fn = get_callee_fndecl (expression);
20419 int i, nargs;
20420 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20421 return true;
20422 nargs = call_expr_nargs (expression);
20423 for (i = 0; i < nargs; ++i)
20425 tree op = CALL_EXPR_ARG (expression, i);
20426 /* In a call to a constexpr member function, look through the
20427 implicit ADDR_EXPR on the object argument so that it doesn't
20428 cause the call to be considered value-dependent. We also
20429 look through it in potential_constant_expression. */
20430 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20431 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20432 && TREE_CODE (op) == ADDR_EXPR)
20433 op = TREE_OPERAND (op, 0);
20434 if (value_dependent_expression_p (op))
20435 return true;
20437 return false;
20440 case TEMPLATE_ID_EXPR:
20441 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20442 type-dependent. */
20443 return type_dependent_expression_p (expression);
20445 case CONSTRUCTOR:
20447 unsigned ix;
20448 tree val;
20449 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20450 if (value_dependent_expression_p (val))
20451 return true;
20452 return false;
20455 case STMT_EXPR:
20456 /* Treat a GNU statement expression as dependent to avoid crashing
20457 under fold_non_dependent_expr; it can't be constant. */
20458 return true;
20460 default:
20461 /* A constant expression is value-dependent if any subexpression is
20462 value-dependent. */
20463 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20465 case tcc_reference:
20466 case tcc_unary:
20467 case tcc_comparison:
20468 case tcc_binary:
20469 case tcc_expression:
20470 case tcc_vl_exp:
20472 int i, len = cp_tree_operand_length (expression);
20474 for (i = 0; i < len; i++)
20476 tree t = TREE_OPERAND (expression, i);
20478 /* In some cases, some of the operands may be missing.l
20479 (For example, in the case of PREDECREMENT_EXPR, the
20480 amount to increment by may be missing.) That doesn't
20481 make the expression dependent. */
20482 if (t && value_dependent_expression_p (t))
20483 return true;
20486 break;
20487 default:
20488 break;
20490 break;
20493 /* The expression is not value-dependent. */
20494 return false;
20497 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20498 [temp.dep.expr]. Note that an expression with no type is
20499 considered dependent. Other parts of the compiler arrange for an
20500 expression with type-dependent subexpressions to have no type, so
20501 this function doesn't have to be fully recursive. */
20503 bool
20504 type_dependent_expression_p (tree expression)
20506 if (!processing_template_decl)
20507 return false;
20509 if (expression == NULL_TREE || expression == error_mark_node)
20510 return false;
20512 /* An unresolved name is always dependent. */
20513 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20514 return true;
20516 /* Some expression forms are never type-dependent. */
20517 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20518 || TREE_CODE (expression) == SIZEOF_EXPR
20519 || TREE_CODE (expression) == ALIGNOF_EXPR
20520 || TREE_CODE (expression) == AT_ENCODE_EXPR
20521 || TREE_CODE (expression) == NOEXCEPT_EXPR
20522 || TREE_CODE (expression) == TRAIT_EXPR
20523 || TREE_CODE (expression) == TYPEID_EXPR
20524 || TREE_CODE (expression) == DELETE_EXPR
20525 || TREE_CODE (expression) == VEC_DELETE_EXPR
20526 || TREE_CODE (expression) == THROW_EXPR)
20527 return false;
20529 /* The types of these expressions depends only on the type to which
20530 the cast occurs. */
20531 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20532 || TREE_CODE (expression) == STATIC_CAST_EXPR
20533 || TREE_CODE (expression) == CONST_CAST_EXPR
20534 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20535 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20536 || TREE_CODE (expression) == CAST_EXPR)
20537 return dependent_type_p (TREE_TYPE (expression));
20539 /* The types of these expressions depends only on the type created
20540 by the expression. */
20541 if (TREE_CODE (expression) == NEW_EXPR
20542 || TREE_CODE (expression) == VEC_NEW_EXPR)
20544 /* For NEW_EXPR tree nodes created inside a template, either
20545 the object type itself or a TREE_LIST may appear as the
20546 operand 1. */
20547 tree type = TREE_OPERAND (expression, 1);
20548 if (TREE_CODE (type) == TREE_LIST)
20549 /* This is an array type. We need to check array dimensions
20550 as well. */
20551 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20552 || value_dependent_expression_p
20553 (TREE_OPERAND (TREE_VALUE (type), 1));
20554 else
20555 return dependent_type_p (type);
20558 if (TREE_CODE (expression) == SCOPE_REF)
20560 tree scope = TREE_OPERAND (expression, 0);
20561 tree name = TREE_OPERAND (expression, 1);
20563 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20564 contains an identifier associated by name lookup with one or more
20565 declarations declared with a dependent type, or...a
20566 nested-name-specifier or qualified-id that names a member of an
20567 unknown specialization. */
20568 return (type_dependent_expression_p (name)
20569 || dependent_scope_p (scope));
20572 if (TREE_CODE (expression) == FUNCTION_DECL
20573 && DECL_LANG_SPECIFIC (expression)
20574 && DECL_TEMPLATE_INFO (expression)
20575 && (any_dependent_template_arguments_p
20576 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20577 return true;
20579 if (TREE_CODE (expression) == TEMPLATE_DECL
20580 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20581 return false;
20583 if (TREE_CODE (expression) == STMT_EXPR)
20584 expression = stmt_expr_value_expr (expression);
20586 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20588 tree elt;
20589 unsigned i;
20591 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20593 if (type_dependent_expression_p (elt))
20594 return true;
20596 return false;
20599 /* A static data member of the current instantiation with incomplete
20600 array type is type-dependent, as the definition and specializations
20601 can have different bounds. */
20602 if (VAR_P (expression)
20603 && DECL_CLASS_SCOPE_P (expression)
20604 && dependent_type_p (DECL_CONTEXT (expression))
20605 && VAR_HAD_UNKNOWN_BOUND (expression))
20606 return true;
20608 /* An array of unknown bound depending on a variadic parameter, eg:
20610 template<typename... Args>
20611 void foo (Args... args)
20613 int arr[] = { args... };
20616 template<int... vals>
20617 void bar ()
20619 int arr[] = { vals... };
20622 If the array has no length and has an initializer, it must be that
20623 we couldn't determine its length in cp_complete_array_type because
20624 it is dependent. */
20625 if (VAR_P (expression)
20626 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20627 && !TYPE_DOMAIN (TREE_TYPE (expression))
20628 && DECL_INITIAL (expression))
20629 return true;
20631 if (TREE_TYPE (expression) == unknown_type_node)
20633 if (TREE_CODE (expression) == ADDR_EXPR)
20634 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20635 if (TREE_CODE (expression) == COMPONENT_REF
20636 || TREE_CODE (expression) == OFFSET_REF)
20638 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20639 return true;
20640 expression = TREE_OPERAND (expression, 1);
20641 if (identifier_p (expression))
20642 return false;
20644 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20645 if (TREE_CODE (expression) == SCOPE_REF)
20646 return false;
20648 /* Always dependent, on the number of arguments if nothing else. */
20649 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20650 return true;
20652 if (BASELINK_P (expression))
20653 expression = BASELINK_FUNCTIONS (expression);
20655 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20657 if (any_dependent_template_arguments_p
20658 (TREE_OPERAND (expression, 1)))
20659 return true;
20660 expression = TREE_OPERAND (expression, 0);
20662 gcc_assert (TREE_CODE (expression) == OVERLOAD
20663 || TREE_CODE (expression) == FUNCTION_DECL);
20665 while (expression)
20667 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20668 return true;
20669 expression = OVL_NEXT (expression);
20671 return false;
20674 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20676 return (dependent_type_p (TREE_TYPE (expression)));
20679 /* walk_tree callback function for instantiation_dependent_expression_p,
20680 below. Returns non-zero if a dependent subexpression is found. */
20682 static tree
20683 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20684 void * /*data*/)
20686 if (TYPE_P (*tp))
20688 /* We don't have to worry about decltype currently because decltype
20689 of an instantiation-dependent expr is a dependent type. This
20690 might change depending on the resolution of DR 1172. */
20691 *walk_subtrees = false;
20692 return NULL_TREE;
20694 enum tree_code code = TREE_CODE (*tp);
20695 switch (code)
20697 /* Don't treat an argument list as dependent just because it has no
20698 TREE_TYPE. */
20699 case TREE_LIST:
20700 case TREE_VEC:
20701 return NULL_TREE;
20703 case VAR_DECL:
20704 case CONST_DECL:
20705 /* A constant with a dependent initializer is dependent. */
20706 if (value_dependent_expression_p (*tp))
20707 return *tp;
20708 break;
20710 case TEMPLATE_PARM_INDEX:
20711 return *tp;
20713 /* Handle expressions with type operands. */
20714 case SIZEOF_EXPR:
20715 case ALIGNOF_EXPR:
20716 case TYPEID_EXPR:
20717 case AT_ENCODE_EXPR:
20719 tree op = TREE_OPERAND (*tp, 0);
20720 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20721 op = TREE_TYPE (op);
20722 if (TYPE_P (op))
20724 if (dependent_type_p (op))
20725 return *tp;
20726 else
20728 *walk_subtrees = false;
20729 return NULL_TREE;
20732 break;
20735 case TRAIT_EXPR:
20736 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20737 || (TRAIT_EXPR_TYPE2 (*tp)
20738 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20739 return *tp;
20740 *walk_subtrees = false;
20741 return NULL_TREE;
20743 case COMPONENT_REF:
20744 if (identifier_p (TREE_OPERAND (*tp, 1)))
20745 /* In a template, finish_class_member_access_expr creates a
20746 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20747 type-dependent, so that we can check access control at
20748 instantiation time (PR 42277). See also Core issue 1273. */
20749 return *tp;
20750 break;
20752 case SCOPE_REF:
20753 if (instantiation_dependent_scope_ref_p (*tp))
20754 return *tp;
20755 else
20756 break;
20758 /* Treat statement-expressions as dependent. */
20759 case BIND_EXPR:
20760 return *tp;
20762 default:
20763 break;
20766 if (type_dependent_expression_p (*tp))
20767 return *tp;
20768 else
20769 return NULL_TREE;
20772 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20773 sense defined by the ABI:
20775 "An expression is instantiation-dependent if it is type-dependent
20776 or value-dependent, or it has a subexpression that is type-dependent
20777 or value-dependent." */
20779 bool
20780 instantiation_dependent_expression_p (tree expression)
20782 tree result;
20784 if (!processing_template_decl)
20785 return false;
20787 if (expression == error_mark_node)
20788 return false;
20790 result = cp_walk_tree_without_duplicates (&expression,
20791 instantiation_dependent_r, NULL);
20792 return result != NULL_TREE;
20795 /* Like type_dependent_expression_p, but it also works while not processing
20796 a template definition, i.e. during substitution or mangling. */
20798 bool
20799 type_dependent_expression_p_push (tree expr)
20801 bool b;
20802 ++processing_template_decl;
20803 b = type_dependent_expression_p (expr);
20804 --processing_template_decl;
20805 return b;
20808 /* Returns TRUE if ARGS contains a type-dependent expression. */
20810 bool
20811 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20813 unsigned int i;
20814 tree arg;
20816 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20818 if (type_dependent_expression_p (arg))
20819 return true;
20821 return false;
20824 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20825 expressions) contains any type-dependent expressions. */
20827 bool
20828 any_type_dependent_elements_p (const_tree list)
20830 for (; list; list = TREE_CHAIN (list))
20831 if (type_dependent_expression_p (TREE_VALUE (list)))
20832 return true;
20834 return false;
20837 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20838 expressions) contains any value-dependent expressions. */
20840 bool
20841 any_value_dependent_elements_p (const_tree list)
20843 for (; list; list = TREE_CHAIN (list))
20844 if (value_dependent_expression_p (TREE_VALUE (list)))
20845 return true;
20847 return false;
20850 /* Returns TRUE if the ARG (a template argument) is dependent. */
20852 bool
20853 dependent_template_arg_p (tree arg)
20855 if (!processing_template_decl)
20856 return false;
20858 /* Assume a template argument that was wrongly written by the user
20859 is dependent. This is consistent with what
20860 any_dependent_template_arguments_p [that calls this function]
20861 does. */
20862 if (!arg || arg == error_mark_node)
20863 return true;
20865 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20866 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20868 if (TREE_CODE (arg) == TEMPLATE_DECL
20869 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20870 return dependent_template_p (arg);
20871 else if (ARGUMENT_PACK_P (arg))
20873 tree args = ARGUMENT_PACK_ARGS (arg);
20874 int i, len = TREE_VEC_LENGTH (args);
20875 for (i = 0; i < len; ++i)
20877 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20878 return true;
20881 return false;
20883 else if (TYPE_P (arg))
20884 return dependent_type_p (arg);
20885 else
20886 return (type_dependent_expression_p (arg)
20887 || value_dependent_expression_p (arg));
20890 /* Returns true if ARGS (a collection of template arguments) contains
20891 any types that require structural equality testing. */
20893 bool
20894 any_template_arguments_need_structural_equality_p (tree args)
20896 int i;
20897 int j;
20899 if (!args)
20900 return false;
20901 if (args == error_mark_node)
20902 return true;
20904 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20906 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20907 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20909 tree arg = TREE_VEC_ELT (level, j);
20910 tree packed_args = NULL_TREE;
20911 int k, len = 1;
20913 if (ARGUMENT_PACK_P (arg))
20915 /* Look inside the argument pack. */
20916 packed_args = ARGUMENT_PACK_ARGS (arg);
20917 len = TREE_VEC_LENGTH (packed_args);
20920 for (k = 0; k < len; ++k)
20922 if (packed_args)
20923 arg = TREE_VEC_ELT (packed_args, k);
20925 if (error_operand_p (arg))
20926 return true;
20927 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20928 continue;
20929 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20930 return true;
20931 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20932 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20933 return true;
20938 return false;
20941 /* Returns true if ARGS (a collection of template arguments) contains
20942 any dependent arguments. */
20944 bool
20945 any_dependent_template_arguments_p (const_tree args)
20947 int i;
20948 int j;
20950 if (!args)
20951 return false;
20952 if (args == error_mark_node)
20953 return true;
20955 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20957 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20958 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20959 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20960 return true;
20963 return false;
20966 /* Returns TRUE if the template TMPL is dependent. */
20968 bool
20969 dependent_template_p (tree tmpl)
20971 if (TREE_CODE (tmpl) == OVERLOAD)
20973 while (tmpl)
20975 if (dependent_template_p (OVL_CURRENT (tmpl)))
20976 return true;
20977 tmpl = OVL_NEXT (tmpl);
20979 return false;
20982 /* Template template parameters are dependent. */
20983 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20984 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20985 return true;
20986 /* So are names that have not been looked up. */
20987 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
20988 return true;
20989 /* So are member templates of dependent classes. */
20990 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
20991 return dependent_type_p (DECL_CONTEXT (tmpl));
20992 return false;
20995 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20997 bool
20998 dependent_template_id_p (tree tmpl, tree args)
21000 return (dependent_template_p (tmpl)
21001 || any_dependent_template_arguments_p (args));
21004 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21005 is dependent. */
21007 bool
21008 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21010 int i;
21012 if (!processing_template_decl)
21013 return false;
21015 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21017 tree decl = TREE_VEC_ELT (declv, i);
21018 tree init = TREE_VEC_ELT (initv, i);
21019 tree cond = TREE_VEC_ELT (condv, i);
21020 tree incr = TREE_VEC_ELT (incrv, i);
21022 if (type_dependent_expression_p (decl))
21023 return true;
21025 if (init && type_dependent_expression_p (init))
21026 return true;
21028 if (type_dependent_expression_p (cond))
21029 return true;
21031 if (COMPARISON_CLASS_P (cond)
21032 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21033 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21034 return true;
21036 if (TREE_CODE (incr) == MODOP_EXPR)
21038 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21039 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21040 return true;
21042 else if (type_dependent_expression_p (incr))
21043 return true;
21044 else if (TREE_CODE (incr) == MODIFY_EXPR)
21046 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21047 return true;
21048 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21050 tree t = TREE_OPERAND (incr, 1);
21051 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21052 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21053 return true;
21058 return false;
21061 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21062 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21063 no such TYPE can be found. Note that this function peers inside
21064 uninstantiated templates and therefore should be used only in
21065 extremely limited situations. ONLY_CURRENT_P restricts this
21066 peering to the currently open classes hierarchy (which is required
21067 when comparing types). */
21069 tree
21070 resolve_typename_type (tree type, bool only_current_p)
21072 tree scope;
21073 tree name;
21074 tree decl;
21075 int quals;
21076 tree pushed_scope;
21077 tree result;
21079 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21081 scope = TYPE_CONTEXT (type);
21082 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21083 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21084 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21085 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21086 identifier of the TYPENAME_TYPE anymore.
21087 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21088 TYPENAME_TYPE instead, we avoid messing up with a possible
21089 typedef variant case. */
21090 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21092 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21093 it first before we can figure out what NAME refers to. */
21094 if (TREE_CODE (scope) == TYPENAME_TYPE)
21096 if (TYPENAME_IS_RESOLVING_P (scope))
21097 /* Given a class template A with a dependent base with nested type C,
21098 typedef typename A::C::C C will land us here, as trying to resolve
21099 the initial A::C leads to the local C typedef, which leads back to
21100 A::C::C. So we break the recursion now. */
21101 return type;
21102 else
21103 scope = resolve_typename_type (scope, only_current_p);
21105 /* If we don't know what SCOPE refers to, then we cannot resolve the
21106 TYPENAME_TYPE. */
21107 if (TREE_CODE (scope) == TYPENAME_TYPE)
21108 return type;
21109 /* If the SCOPE is a template type parameter, we have no way of
21110 resolving the name. */
21111 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21112 return type;
21113 /* If the SCOPE is not the current instantiation, there's no reason
21114 to look inside it. */
21115 if (only_current_p && !currently_open_class (scope))
21116 return type;
21117 /* If this is a typedef, we don't want to look inside (c++/11987). */
21118 if (typedef_variant_p (type))
21119 return type;
21120 /* If SCOPE isn't the template itself, it will not have a valid
21121 TYPE_FIELDS list. */
21122 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21123 /* scope is either the template itself or a compatible instantiation
21124 like X<T>, so look up the name in the original template. */
21125 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21126 else
21127 /* scope is a partial instantiation, so we can't do the lookup or we
21128 will lose the template arguments. */
21129 return type;
21130 /* Enter the SCOPE so that name lookup will be resolved as if we
21131 were in the class definition. In particular, SCOPE will no
21132 longer be considered a dependent type. */
21133 pushed_scope = push_scope (scope);
21134 /* Look up the declaration. */
21135 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21136 tf_warning_or_error);
21138 result = NULL_TREE;
21140 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21141 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21142 if (!decl)
21143 /*nop*/;
21144 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21145 && TREE_CODE (decl) == TYPE_DECL)
21147 result = TREE_TYPE (decl);
21148 if (result == error_mark_node)
21149 result = NULL_TREE;
21151 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21152 && DECL_CLASS_TEMPLATE_P (decl))
21154 tree tmpl;
21155 tree args;
21156 /* Obtain the template and the arguments. */
21157 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21158 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21159 /* Instantiate the template. */
21160 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21161 /*entering_scope=*/0,
21162 tf_error | tf_user);
21163 if (result == error_mark_node)
21164 result = NULL_TREE;
21167 /* Leave the SCOPE. */
21168 if (pushed_scope)
21169 pop_scope (pushed_scope);
21171 /* If we failed to resolve it, return the original typename. */
21172 if (!result)
21173 return type;
21175 /* If lookup found a typename type, resolve that too. */
21176 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21178 /* Ill-formed programs can cause infinite recursion here, so we
21179 must catch that. */
21180 TYPENAME_IS_RESOLVING_P (type) = 1;
21181 result = resolve_typename_type (result, only_current_p);
21182 TYPENAME_IS_RESOLVING_P (type) = 0;
21185 /* Qualify the resulting type. */
21186 quals = cp_type_quals (type);
21187 if (quals)
21188 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21190 return result;
21193 /* EXPR is an expression which is not type-dependent. Return a proxy
21194 for EXPR that can be used to compute the types of larger
21195 expressions containing EXPR. */
21197 tree
21198 build_non_dependent_expr (tree expr)
21200 tree inner_expr;
21202 #ifdef ENABLE_CHECKING
21203 /* Try to get a constant value for all non-dependent expressions in
21204 order to expose bugs in *_dependent_expression_p and constexpr. */
21205 if (cxx_dialect >= cxx11)
21206 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21207 #endif
21209 /* Preserve OVERLOADs; the functions must be available to resolve
21210 types. */
21211 inner_expr = expr;
21212 if (TREE_CODE (inner_expr) == STMT_EXPR)
21213 inner_expr = stmt_expr_value_expr (inner_expr);
21214 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21215 inner_expr = TREE_OPERAND (inner_expr, 0);
21216 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21217 inner_expr = TREE_OPERAND (inner_expr, 1);
21218 if (is_overloaded_fn (inner_expr)
21219 || TREE_CODE (inner_expr) == OFFSET_REF)
21220 return expr;
21221 /* There is no need to return a proxy for a variable. */
21222 if (VAR_P (expr))
21223 return expr;
21224 /* Preserve string constants; conversions from string constants to
21225 "char *" are allowed, even though normally a "const char *"
21226 cannot be used to initialize a "char *". */
21227 if (TREE_CODE (expr) == STRING_CST)
21228 return expr;
21229 /* Preserve arithmetic constants, as an optimization -- there is no
21230 reason to create a new node. */
21231 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21232 return expr;
21233 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21234 There is at least one place where we want to know that a
21235 particular expression is a throw-expression: when checking a ?:
21236 expression, there are special rules if the second or third
21237 argument is a throw-expression. */
21238 if (TREE_CODE (expr) == THROW_EXPR)
21239 return expr;
21241 /* Don't wrap an initializer list, we need to be able to look inside. */
21242 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21243 return expr;
21245 /* Don't wrap a dummy object, we need to be able to test for it. */
21246 if (is_dummy_object (expr))
21247 return expr;
21249 if (TREE_CODE (expr) == COND_EXPR)
21250 return build3 (COND_EXPR,
21251 TREE_TYPE (expr),
21252 TREE_OPERAND (expr, 0),
21253 (TREE_OPERAND (expr, 1)
21254 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21255 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21256 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21257 if (TREE_CODE (expr) == COMPOUND_EXPR
21258 && !COMPOUND_EXPR_OVERLOADED (expr))
21259 return build2 (COMPOUND_EXPR,
21260 TREE_TYPE (expr),
21261 TREE_OPERAND (expr, 0),
21262 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21264 /* If the type is unknown, it can't really be non-dependent */
21265 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21267 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21268 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21271 /* ARGS is a vector of expressions as arguments to a function call.
21272 Replace the arguments with equivalent non-dependent expressions.
21273 This modifies ARGS in place. */
21275 void
21276 make_args_non_dependent (vec<tree, va_gc> *args)
21278 unsigned int ix;
21279 tree arg;
21281 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21283 tree newarg = build_non_dependent_expr (arg);
21284 if (newarg != arg)
21285 (*args)[ix] = newarg;
21289 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21290 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21291 parms. */
21293 static tree
21294 make_auto_1 (tree name)
21296 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21297 TYPE_NAME (au) = build_decl (input_location,
21298 TYPE_DECL, name, au);
21299 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21300 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21301 (0, processing_template_decl + 1, processing_template_decl + 1,
21302 TYPE_NAME (au), NULL_TREE);
21303 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21304 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21305 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21307 return au;
21310 tree
21311 make_decltype_auto (void)
21313 return make_auto_1 (get_identifier ("decltype(auto)"));
21316 tree
21317 make_auto (void)
21319 return make_auto_1 (get_identifier ("auto"));
21322 /* Given type ARG, return std::initializer_list<ARG>. */
21324 static tree
21325 listify (tree arg)
21327 tree std_init_list = namespace_binding
21328 (get_identifier ("initializer_list"), std_node);
21329 tree argvec;
21330 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21332 error ("deducing from brace-enclosed initializer list requires "
21333 "#include <initializer_list>");
21334 return error_mark_node;
21336 argvec = make_tree_vec (1);
21337 TREE_VEC_ELT (argvec, 0) = arg;
21338 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21339 NULL_TREE, 0, tf_warning_or_error);
21342 /* Replace auto in TYPE with std::initializer_list<auto>. */
21344 static tree
21345 listify_autos (tree type, tree auto_node)
21347 tree init_auto = listify (auto_node);
21348 tree argvec = make_tree_vec (1);
21349 TREE_VEC_ELT (argvec, 0) = init_auto;
21350 if (processing_template_decl)
21351 argvec = add_to_template_args (current_template_args (), argvec);
21352 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21355 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21356 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21358 tree
21359 do_auto_deduction (tree type, tree init, tree auto_node)
21361 tree targs;
21363 if (init == error_mark_node)
21364 return error_mark_node;
21366 if (type_dependent_expression_p (init))
21367 /* Defining a subset of type-dependent expressions that we can deduce
21368 from ahead of time isn't worth the trouble. */
21369 return type;
21371 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21372 with either a new invented type template parameter U or, if the
21373 initializer is a braced-init-list (8.5.4), with
21374 std::initializer_list<U>. */
21375 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21376 type = listify_autos (type, auto_node);
21378 init = resolve_nondeduced_context (init);
21380 targs = make_tree_vec (1);
21381 if (AUTO_IS_DECLTYPE (auto_node))
21383 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21384 TREE_VEC_ELT (targs, 0)
21385 = finish_decltype_type (init, id, tf_warning_or_error);
21386 if (type != auto_node)
21388 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21389 return error_mark_node;
21392 else
21394 tree parms = build_tree_list (NULL_TREE, type);
21395 tree tparms = make_tree_vec (1);
21396 int val;
21398 TREE_VEC_ELT (tparms, 0)
21399 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21400 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21401 DEDUCE_CALL, LOOKUP_NORMAL,
21402 NULL, /*explain_p=*/false);
21403 if (val > 0)
21405 if (processing_template_decl)
21406 /* Try again at instantiation time. */
21407 return type;
21408 if (type && type != error_mark_node)
21409 /* If type is error_mark_node a diagnostic must have been
21410 emitted by now. Also, having a mention to '<type error>'
21411 in the diagnostic is not really useful to the user. */
21413 if (cfun && auto_node == current_function_auto_return_pattern
21414 && LAMBDA_FUNCTION_P (current_function_decl))
21415 error ("unable to deduce lambda return type from %qE", init);
21416 else
21417 error ("unable to deduce %qT from %qE", type, init);
21419 return error_mark_node;
21423 /* If the list of declarators contains more than one declarator, the type
21424 of each declared variable is determined as described above. If the
21425 type deduced for the template parameter U is not the same in each
21426 deduction, the program is ill-formed. */
21427 if (TREE_TYPE (auto_node)
21428 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21430 if (cfun && auto_node == current_function_auto_return_pattern
21431 && LAMBDA_FUNCTION_P (current_function_decl))
21432 error ("inconsistent types %qT and %qT deduced for "
21433 "lambda return type", TREE_TYPE (auto_node),
21434 TREE_VEC_ELT (targs, 0));
21435 else
21436 error ("inconsistent deduction for %qT: %qT and then %qT",
21437 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21438 return error_mark_node;
21440 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21442 if (processing_template_decl)
21443 targs = add_to_template_args (current_template_args (), targs);
21444 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21447 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21448 result. */
21450 tree
21451 splice_late_return_type (tree type, tree late_return_type)
21453 tree argvec;
21455 if (late_return_type == NULL_TREE)
21456 return type;
21457 argvec = make_tree_vec (1);
21458 TREE_VEC_ELT (argvec, 0) = late_return_type;
21459 if (processing_template_parmlist)
21460 /* For a late-specified return type in a template type-parameter, we
21461 need to add a dummy argument level for its parmlist. */
21462 argvec = add_to_template_args
21463 (make_tree_vec (processing_template_parmlist), argvec);
21464 if (current_template_parms)
21465 argvec = add_to_template_args (current_template_args (), argvec);
21466 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21469 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21470 'decltype(auto)'. */
21472 bool
21473 is_auto (const_tree type)
21475 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21476 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21477 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21478 return true;
21479 else
21480 return false;
21483 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21484 a use of `auto'. Returns NULL_TREE otherwise. */
21486 tree
21487 type_uses_auto (tree type)
21489 return find_type_usage (type, is_auto);
21492 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21493 'decltype(auto)' or a concept. */
21495 bool
21496 is_auto_or_concept (const_tree type)
21498 return is_auto (type); // or concept
21501 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21502 a concept identifier) iff TYPE contains a use of a generic type. Returns
21503 NULL_TREE otherwise. */
21505 tree
21506 type_uses_auto_or_concept (tree type)
21508 return find_type_usage (type, is_auto_or_concept);
21512 /* For a given template T, return the vector of typedefs referenced
21513 in T for which access check is needed at T instantiation time.
21514 T is either a FUNCTION_DECL or a RECORD_TYPE.
21515 Those typedefs were added to T by the function
21516 append_type_to_template_for_access_check. */
21518 vec<qualified_typedef_usage_t, va_gc> *
21519 get_types_needing_access_check (tree t)
21521 tree ti;
21522 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21524 if (!t || t == error_mark_node)
21525 return NULL;
21527 if (!(ti = get_template_info (t)))
21528 return NULL;
21530 if (CLASS_TYPE_P (t)
21531 || TREE_CODE (t) == FUNCTION_DECL)
21533 if (!TI_TEMPLATE (ti))
21534 return NULL;
21536 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21539 return result;
21542 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21543 tied to T. That list of typedefs will be access checked at
21544 T instantiation time.
21545 T is either a FUNCTION_DECL or a RECORD_TYPE.
21546 TYPE_DECL is a TYPE_DECL node representing a typedef.
21547 SCOPE is the scope through which TYPE_DECL is accessed.
21548 LOCATION is the location of the usage point of TYPE_DECL.
21550 This function is a subroutine of
21551 append_type_to_template_for_access_check. */
21553 static void
21554 append_type_to_template_for_access_check_1 (tree t,
21555 tree type_decl,
21556 tree scope,
21557 location_t location)
21559 qualified_typedef_usage_t typedef_usage;
21560 tree ti;
21562 if (!t || t == error_mark_node)
21563 return;
21565 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21566 || CLASS_TYPE_P (t))
21567 && type_decl
21568 && TREE_CODE (type_decl) == TYPE_DECL
21569 && scope);
21571 if (!(ti = get_template_info (t)))
21572 return;
21574 gcc_assert (TI_TEMPLATE (ti));
21576 typedef_usage.typedef_decl = type_decl;
21577 typedef_usage.context = scope;
21578 typedef_usage.locus = location;
21580 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21583 /* Append TYPE_DECL to the template TEMPL.
21584 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21585 At TEMPL instanciation time, TYPE_DECL will be checked to see
21586 if it can be accessed through SCOPE.
21587 LOCATION is the location of the usage point of TYPE_DECL.
21589 e.g. consider the following code snippet:
21591 class C
21593 typedef int myint;
21596 template<class U> struct S
21598 C::myint mi; // <-- usage point of the typedef C::myint
21601 S<char> s;
21603 At S<char> instantiation time, we need to check the access of C::myint
21604 In other words, we need to check the access of the myint typedef through
21605 the C scope. For that purpose, this function will add the myint typedef
21606 and the scope C through which its being accessed to a list of typedefs
21607 tied to the template S. That list will be walked at template instantiation
21608 time and access check performed on each typedefs it contains.
21609 Note that this particular code snippet should yield an error because
21610 myint is private to C. */
21612 void
21613 append_type_to_template_for_access_check (tree templ,
21614 tree type_decl,
21615 tree scope,
21616 location_t location)
21618 qualified_typedef_usage_t *iter;
21619 unsigned i;
21621 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21623 /* Make sure we don't append the type to the template twice. */
21624 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21625 if (iter->typedef_decl == type_decl && scope == iter->context)
21626 return;
21628 append_type_to_template_for_access_check_1 (templ, type_decl,
21629 scope, location);
21632 /* Set up the hash tables for template instantiations. */
21634 void
21635 init_template_processing (void)
21637 decl_specializations = htab_create_ggc (37,
21638 hash_specialization,
21639 eq_specializations,
21640 ggc_free);
21641 type_specializations = htab_create_ggc (37,
21642 hash_specialization,
21643 eq_specializations,
21644 ggc_free);
21647 /* Print stats about the template hash tables for -fstats. */
21649 void
21650 print_template_statistics (void)
21652 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21653 "%f collisions\n", (long) htab_size (decl_specializations),
21654 (long) htab_elements (decl_specializations),
21655 htab_collisions (decl_specializations));
21656 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21657 "%f collisions\n", (long) htab_size (type_specializations),
21658 (long) htab_elements (type_specializations),
21659 htab_collisions (type_specializations));
21662 #include "gt-cp-pt.h"