2014-02-21 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / cp / pt.c
blob6477fce7aab36dd15ecb176ee95d557bef8c443c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "pointer-set.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimplify.h"
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
66 int processing_template_parmlist;
67 static int template_header_count;
69 static GTY(()) tree saved_trees;
70 static vec<int> inline_parm_levels;
72 static GTY(()) struct tinst_level *current_tinst_level;
74 static GTY(()) tree saved_access_scope;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth;
84 typedef struct GTY(()) spec_entry
86 tree tmpl;
87 tree args;
88 tree spec;
89 } spec_entry;
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result {
113 tbr_incomplete_type,
114 tbr_ambiguous_baseclass,
115 tbr_success
118 static void push_access_scope (tree);
119 static void pop_access_scope (tree);
120 static bool resolve_overloaded_unification (tree, tree, tree, tree,
121 unification_kind_t, int,
122 bool);
123 static int try_one_overload (tree, tree, tree, tree, tree,
124 unification_kind_t, int, bool, bool);
125 static int unify (tree, tree, tree, tree, int, bool);
126 static void add_pending_template (tree);
127 static tree reopen_tinst_level (struct tinst_level *);
128 static tree tsubst_initializer_list (tree, tree);
129 static tree get_class_bindings (tree, tree, tree, tree);
130 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
131 bool, bool);
132 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
133 bool, bool);
134 static void tsubst_enum (tree, tree, tree);
135 static tree add_to_template_args (tree, tree);
136 static tree add_outermost_template_args (tree, tree);
137 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
138 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
139 tree);
140 static int type_unification_real (tree, tree, tree, const tree *,
141 unsigned int, int, unification_kind_t, int,
142 vec<deferred_access_check, va_gc> **,
143 bool);
144 static void note_template_header (int);
145 static tree convert_nontype_argument_function (tree, tree);
146 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
147 static tree convert_template_argument (tree, tree, tree,
148 tsubst_flags_t, int, tree);
149 static int for_each_template_parm (tree, tree_fn_t, void*,
150 struct pointer_set_t*, bool);
151 static tree expand_template_argument_pack (tree);
152 static tree build_template_parm_index (int, int, int, tree, tree);
153 static bool inline_needs_template_parms (tree, bool);
154 static void push_inline_template_parms_recursive (tree, int);
155 static tree retrieve_local_specialization (tree);
156 static void register_local_specialization (tree, tree);
157 static hashval_t hash_specialization (const void *p);
158 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
159 static int mark_template_parm (tree, void *);
160 static int template_parm_this_level_p (tree, void *);
161 static tree tsubst_friend_function (tree, tree);
162 static tree tsubst_friend_class (tree, tree);
163 static int can_complete_type_without_circularity (tree);
164 static tree get_bindings (tree, tree, tree, bool);
165 static int template_decl_level (tree);
166 static int check_cv_quals_for_unify (int, tree, tree);
167 static void template_parm_level_and_index (tree, int*, int*);
168 static int unify_pack_expansion (tree, tree, tree,
169 tree, unification_kind_t, bool, bool);
170 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
171 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
173 static void regenerate_decl_from_template (tree, tree);
174 static tree most_specialized_class (tree, tsubst_flags_t);
175 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
176 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
177 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
178 static bool check_specialization_scope (void);
179 static tree process_partial_specialization (tree);
180 static void set_current_access_from_decl (tree);
181 static enum template_base_result get_template_base (tree, tree, tree, tree,
182 bool , tree *);
183 static tree try_class_unification (tree, tree, tree, tree, bool);
184 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
185 tree, tree);
186 static bool template_template_parm_bindings_ok_p (tree, tree);
187 static int template_args_equal (tree, tree);
188 static void tsubst_default_arguments (tree, tsubst_flags_t);
189 static tree for_each_template_parm_r (tree *, int *, void *);
190 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
191 static void copy_default_args_to_explicit_spec (tree);
192 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
193 static bool dependent_template_arg_p (tree);
194 static bool any_template_arguments_need_structural_equality_p (tree);
195 static bool dependent_type_p_r (tree);
196 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
197 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
198 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
199 static tree tsubst_decl (tree, tree, tsubst_flags_t);
200 static void perform_typedefs_access_check (tree tmpl, tree targs);
201 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
202 location_t);
203 static tree listify (tree);
204 static tree listify_autos (tree, tree);
205 static tree template_parm_to_arg (tree t);
206 static tree current_template_args (void);
207 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
208 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
215 static void
216 push_access_scope (tree t)
218 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
219 || TREE_CODE (t) == TYPE_DECL);
221 if (DECL_FRIEND_CONTEXT (t))
222 push_nested_class (DECL_FRIEND_CONTEXT (t));
223 else if (DECL_CLASS_SCOPE_P (t))
224 push_nested_class (DECL_CONTEXT (t));
225 else
226 push_to_top_level ();
228 if (TREE_CODE (t) == FUNCTION_DECL)
230 saved_access_scope = tree_cons
231 (NULL_TREE, current_function_decl, saved_access_scope);
232 current_function_decl = t;
236 /* Restore the scope set up by push_access_scope. T is the node we
237 are processing. */
239 static void
240 pop_access_scope (tree t)
242 if (TREE_CODE (t) == FUNCTION_DECL)
244 current_function_decl = TREE_VALUE (saved_access_scope);
245 saved_access_scope = TREE_CHAIN (saved_access_scope);
248 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
249 pop_nested_class ();
250 else
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
259 tree
260 finish_member_template_decl (tree decl)
262 if (decl == error_mark_node)
263 return error_mark_node;
265 gcc_assert (DECL_P (decl));
267 if (TREE_CODE (decl) == TYPE_DECL)
269 tree type;
271 type = TREE_TYPE (decl);
272 if (type == error_mark_node)
273 return error_mark_node;
274 if (MAYBE_CLASS_TYPE_P (type)
275 && CLASSTYPE_TEMPLATE_INFO (type)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
278 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
279 check_member_template (tmpl);
280 return tmpl;
282 return NULL_TREE;
284 else if (TREE_CODE (decl) == FIELD_DECL)
285 error ("data member %qD cannot be a member template", decl);
286 else if (DECL_TEMPLATE_INFO (decl))
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
290 check_member_template (DECL_TI_TEMPLATE (decl));
291 return DECL_TI_TEMPLATE (decl);
293 else
294 return decl;
296 else
297 error ("invalid member template declaration %qD", decl);
299 return error_mark_node;
302 /* Create a template info node. */
304 tree
305 build_template_info (tree template_decl, tree template_args)
307 tree result = make_node (TEMPLATE_INFO);
308 TI_TEMPLATE (result) = template_decl;
309 TI_ARGS (result) = template_args;
310 return result;
313 /* Return the template info node corresponding to T, whatever T is. */
315 tree
316 get_template_info (const_tree t)
318 tree tinfo = NULL_TREE;
320 if (!t || t == error_mark_node)
321 return NULL;
323 if (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, bool nsdmi)
381 if (!decl || (!nsdmi && ! 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, a
451 friend template defined in a class definition, or a non-template
452 member of template class. */
454 void
455 maybe_begin_member_template_processing (tree decl)
457 tree parms;
458 int levels = 0;
459 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
461 if (nsdmi)
462 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
463 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
464 : NULL_TREE);
466 if (inline_needs_template_parms (decl, nsdmi))
468 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
469 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
471 if (DECL_TEMPLATE_SPECIALIZATION (decl))
473 --levels;
474 parms = TREE_CHAIN (parms);
477 push_inline_template_parms_recursive (parms, levels);
480 /* Remember how many levels of template parameters we pushed so that
481 we can pop them later. */
482 inline_parm_levels.safe_push (levels);
485 /* Undo the effects of maybe_begin_member_template_processing. */
487 void
488 maybe_end_member_template_processing (void)
490 int i;
491 int last;
493 if (inline_parm_levels.length () == 0)
494 return;
496 last = inline_parm_levels.pop ();
497 for (i = 0; i < last; ++i)
499 --processing_template_decl;
500 current_template_parms = TREE_CHAIN (current_template_parms);
501 poplevel (0, 0, 0);
505 /* Return a new template argument vector which contains all of ARGS,
506 but has as its innermost set of arguments the EXTRA_ARGS. */
508 static tree
509 add_to_template_args (tree args, tree extra_args)
511 tree new_args;
512 int extra_depth;
513 int i;
514 int j;
516 if (args == NULL_TREE || extra_args == error_mark_node)
517 return extra_args;
519 extra_depth = TMPL_ARGS_DEPTH (extra_args);
520 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
522 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
523 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
525 for (j = 1; j <= extra_depth; ++j, ++i)
526 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
528 return new_args;
531 /* Like add_to_template_args, but only the outermost ARGS are added to
532 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
533 (EXTRA_ARGS) levels are added. This function is used to combine
534 the template arguments from a partial instantiation with the
535 template arguments used to attain the full instantiation from the
536 partial instantiation. */
538 static tree
539 add_outermost_template_args (tree args, tree extra_args)
541 tree new_args;
543 /* If there are more levels of EXTRA_ARGS than there are ARGS,
544 something very fishy is going on. */
545 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
547 /* If *all* the new arguments will be the EXTRA_ARGS, just return
548 them. */
549 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
550 return extra_args;
552 /* For the moment, we make ARGS look like it contains fewer levels. */
553 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
555 new_args = add_to_template_args (args, extra_args);
557 /* Now, we restore ARGS to its full dimensions. */
558 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
560 return new_args;
563 /* Return the N levels of innermost template arguments from the ARGS. */
565 tree
566 get_innermost_template_args (tree args, int n)
568 tree new_args;
569 int extra_levels;
570 int i;
572 gcc_assert (n >= 0);
574 /* If N is 1, just return the innermost set of template arguments. */
575 if (n == 1)
576 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
578 /* If we're not removing anything, just return the arguments we were
579 given. */
580 extra_levels = TMPL_ARGS_DEPTH (args) - n;
581 gcc_assert (extra_levels >= 0);
582 if (extra_levels == 0)
583 return args;
585 /* Make a new set of arguments, not containing the outer arguments. */
586 new_args = make_tree_vec (n);
587 for (i = 1; i <= n; ++i)
588 SET_TMPL_ARGS_LEVEL (new_args, i,
589 TMPL_ARGS_LEVEL (args, i + extra_levels));
591 return new_args;
594 /* The inverse of get_innermost_template_args: Return all but the innermost
595 EXTRA_LEVELS levels of template arguments from the ARGS. */
597 static tree
598 strip_innermost_template_args (tree args, int extra_levels)
600 tree new_args;
601 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
602 int i;
604 gcc_assert (n >= 0);
606 /* If N is 1, just return the outermost set of template arguments. */
607 if (n == 1)
608 return TMPL_ARGS_LEVEL (args, 1);
610 /* If we're not removing anything, just return the arguments we were
611 given. */
612 gcc_assert (extra_levels >= 0);
613 if (extra_levels == 0)
614 return args;
616 /* Make a new set of arguments, not containing the inner arguments. */
617 new_args = make_tree_vec (n);
618 for (i = 1; i <= n; ++i)
619 SET_TMPL_ARGS_LEVEL (new_args, i,
620 TMPL_ARGS_LEVEL (args, i));
622 return new_args;
625 /* We've got a template header coming up; push to a new level for storing
626 the parms. */
628 void
629 begin_template_parm_list (void)
631 /* We use a non-tag-transparent scope here, which causes pushtag to
632 put tags in this scope, rather than in the enclosing class or
633 namespace scope. This is the right thing, since we want
634 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
635 global template class, push_template_decl handles putting the
636 TEMPLATE_DECL into top-level scope. For a nested template class,
637 e.g.:
639 template <class T> struct S1 {
640 template <class T> struct S2 {};
643 pushtag contains special code to call pushdecl_with_scope on the
644 TEMPLATE_DECL for S2. */
645 begin_scope (sk_template_parms, NULL);
646 ++processing_template_decl;
647 ++processing_template_parmlist;
648 note_template_header (0);
651 /* This routine is called when a specialization is declared. If it is
652 invalid to declare a specialization here, an error is reported and
653 false is returned, otherwise this routine will return true. */
655 static bool
656 check_specialization_scope (void)
658 tree scope = current_scope ();
660 /* [temp.expl.spec]
662 An explicit specialization shall be declared in the namespace of
663 which the template is a member, or, for member templates, in the
664 namespace of which the enclosing class or enclosing class
665 template is a member. An explicit specialization of a member
666 function, member class or static data member of a class template
667 shall be declared in the namespace of which the class template
668 is a member. */
669 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
671 error ("explicit specialization in non-namespace scope %qD", scope);
672 return false;
675 /* [temp.expl.spec]
677 In an explicit specialization declaration for a member of a class
678 template or a member template that appears in namespace scope,
679 the member template and some of its enclosing class templates may
680 remain unspecialized, except that the declaration shall not
681 explicitly specialize a class member template if its enclosing
682 class templates are not explicitly specialized as well. */
683 if (current_template_parms)
685 error ("enclosing class templates are not explicitly specialized");
686 return false;
689 return true;
692 /* We've just seen template <>. */
694 bool
695 begin_specialization (void)
697 begin_scope (sk_template_spec, NULL);
698 note_template_header (1);
699 return check_specialization_scope ();
702 /* Called at then end of processing a declaration preceded by
703 template<>. */
705 void
706 end_specialization (void)
708 finish_scope ();
709 reset_specialization ();
712 /* Any template <>'s that we have seen thus far are not referring to a
713 function specialization. */
715 void
716 reset_specialization (void)
718 processing_specialization = 0;
719 template_header_count = 0;
722 /* We've just seen a template header. If SPECIALIZATION is nonzero,
723 it was of the form template <>. */
725 static void
726 note_template_header (int specialization)
728 processing_specialization = specialization;
729 template_header_count++;
732 /* We're beginning an explicit instantiation. */
734 void
735 begin_explicit_instantiation (void)
737 gcc_assert (!processing_explicit_instantiation);
738 processing_explicit_instantiation = true;
742 void
743 end_explicit_instantiation (void)
745 gcc_assert (processing_explicit_instantiation);
746 processing_explicit_instantiation = false;
749 /* An explicit specialization or partial specialization of TMPL is being
750 declared. Check that the namespace in which the specialization is
751 occurring is permissible. Returns false iff it is invalid to
752 specialize TMPL in the current namespace. */
754 static bool
755 check_specialization_namespace (tree tmpl)
757 tree tpl_ns = decl_namespace_context (tmpl);
759 /* [tmpl.expl.spec]
761 An explicit specialization shall be declared in the namespace of
762 which the template is a member, or, for member templates, in the
763 namespace of which the enclosing class or enclosing class
764 template is a member. An explicit specialization of a member
765 function, member class or static data member of a class template
766 shall be declared in the namespace of which the class template is
767 a member. */
768 if (current_scope() != DECL_CONTEXT (tmpl)
769 && !at_namespace_scope_p ())
771 error ("specialization of %qD must appear at namespace scope", tmpl);
772 return false;
774 if (is_associated_namespace (current_namespace, tpl_ns))
775 /* Same or super-using namespace. */
776 return true;
777 else
779 permerror (input_location, "specialization of %qD in different namespace", tmpl);
780 permerror (input_location, " from definition of %q+#D", tmpl);
781 return false;
785 /* SPEC is an explicit instantiation. Check that it is valid to
786 perform this explicit instantiation in the current namespace. */
788 static void
789 check_explicit_instantiation_namespace (tree spec)
791 tree ns;
793 /* DR 275: An explicit instantiation shall appear in an enclosing
794 namespace of its template. */
795 ns = decl_namespace_context (spec);
796 if (!is_ancestor (current_namespace, ns))
797 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
798 "(which does not enclose namespace %qD)",
799 spec, current_namespace, ns);
802 /* The TYPE is being declared. If it is a template type, that means it
803 is a partial specialization. Do appropriate error-checking. */
805 tree
806 maybe_process_partial_specialization (tree type)
808 tree context;
810 if (type == error_mark_node)
811 return error_mark_node;
813 /* A lambda that appears in specialization context is not itself a
814 specialization. */
815 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
816 return type;
818 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
820 error ("name of class shadows template template parameter %qD",
821 TYPE_NAME (type));
822 return error_mark_node;
825 context = TYPE_CONTEXT (type);
827 if (TYPE_ALIAS_P (type))
829 if (TYPE_TEMPLATE_INFO (type)
830 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
831 error ("specialization of alias template %qD",
832 TYPE_TI_TEMPLATE (type));
833 else
834 error ("explicit specialization of non-template %qT", type);
835 return error_mark_node;
837 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
839 /* This is for ordinary explicit specialization and partial
840 specialization of a template class such as:
842 template <> class C<int>;
846 template <class T> class C<T*>;
848 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
850 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
851 && !COMPLETE_TYPE_P (type))
853 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
854 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
855 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
856 if (processing_template_decl)
858 if (push_template_decl (TYPE_MAIN_DECL (type))
859 == error_mark_node)
860 return error_mark_node;
863 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
864 error ("specialization of %qT after instantiation", type);
865 else if (errorcount && !processing_specialization
866 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
867 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
868 /* Trying to define a specialization either without a template<> header
869 or in an inappropriate place. We've already given an error, so just
870 bail now so we don't actually define the specialization. */
871 return error_mark_node;
873 else if (CLASS_TYPE_P (type)
874 && !CLASSTYPE_USE_TEMPLATE (type)
875 && CLASSTYPE_TEMPLATE_INFO (type)
876 && context && CLASS_TYPE_P (context)
877 && CLASSTYPE_TEMPLATE_INFO (context))
879 /* This is for an explicit specialization of member class
880 template according to [temp.expl.spec/18]:
882 template <> template <class U> class C<int>::D;
884 The context `C<int>' must be an implicit instantiation.
885 Otherwise this is just a member class template declared
886 earlier like:
888 template <> class C<int> { template <class U> class D; };
889 template <> template <class U> class C<int>::D;
891 In the first case, `C<int>::D' is a specialization of `C<T>::D'
892 while in the second case, `C<int>::D' is a primary template
893 and `C<T>::D' may not exist. */
895 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
896 && !COMPLETE_TYPE_P (type))
898 tree t;
899 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
901 if (current_namespace
902 != decl_namespace_context (tmpl))
904 permerror (input_location, "specializing %q#T in different namespace", type);
905 permerror (input_location, " from definition of %q+#D", tmpl);
908 /* Check for invalid specialization after instantiation:
910 template <> template <> class C<int>::D<int>;
911 template <> template <class U> class C<int>::D; */
913 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
914 t; t = TREE_CHAIN (t))
916 tree inst = TREE_VALUE (t);
917 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
919 /* We already have a full specialization of this partial
920 instantiation. Reassign it to the new member
921 specialization template. */
922 spec_entry elt;
923 spec_entry *entry;
924 void **slot;
926 elt.tmpl = most_general_template (tmpl);
927 elt.args = CLASSTYPE_TI_ARGS (inst);
928 elt.spec = inst;
930 htab_remove_elt (type_specializations, &elt);
932 elt.tmpl = tmpl;
933 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
935 slot = htab_find_slot (type_specializations, &elt, INSERT);
936 entry = ggc_alloc_spec_entry ();
937 *entry = elt;
938 *slot = entry;
940 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
941 /* But if we've had an implicit instantiation, that's a
942 problem ([temp.expl.spec]/6). */
943 error ("specialization %qT after instantiation %qT",
944 type, inst);
947 /* Mark TYPE as a specialization. And as a result, we only
948 have one level of template argument for the innermost
949 class template. */
950 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
951 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
952 CLASSTYPE_TI_ARGS (type)
953 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
956 else if (processing_specialization)
958 /* Someday C++0x may allow for enum template specialization. */
959 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
960 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
961 pedwarn (input_location, OPT_Wpedantic, "template specialization "
962 "of %qD not allowed by ISO C++", type);
963 else
965 error ("explicit specialization of non-template %qT", type);
966 return error_mark_node;
970 return type;
973 /* Returns nonzero if we can optimize the retrieval of specializations
974 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
975 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
977 static inline bool
978 optimize_specialization_lookup_p (tree tmpl)
980 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
981 && DECL_CLASS_SCOPE_P (tmpl)
982 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
983 parameter. */
984 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
985 /* The optimized lookup depends on the fact that the
986 template arguments for the member function template apply
987 purely to the containing class, which is not true if the
988 containing class is an explicit or partial
989 specialization. */
990 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
991 && !DECL_MEMBER_TEMPLATE_P (tmpl)
992 && !DECL_CONV_FN_P (tmpl)
993 /* It is possible to have a template that is not a member
994 template and is not a member of a template class:
996 template <typename T>
997 struct S { friend A::f(); };
999 Here, the friend function is a template, but the context does
1000 not have template information. The optimized lookup relies
1001 on having ARGS be the template arguments for both the class
1002 and the function template. */
1003 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1006 /* Retrieve the specialization (in the sense of [temp.spec] - a
1007 specialization is either an instantiation or an explicit
1008 specialization) of TMPL for the given template ARGS. If there is
1009 no such specialization, return NULL_TREE. The ARGS are a vector of
1010 arguments, or a vector of vectors of arguments, in the case of
1011 templates with more than one level of parameters.
1013 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1014 then we search for a partial specialization matching ARGS. This
1015 parameter is ignored if TMPL is not a class template.
1017 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1018 result is a NONTYPE_ARGUMENT_PACK. */
1020 static tree
1021 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1023 if (tmpl == NULL_TREE)
1024 return NULL_TREE;
1026 if (args == error_mark_node)
1027 return NULL_TREE;
1029 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1030 || TREE_CODE (tmpl) == FIELD_DECL);
1032 /* There should be as many levels of arguments as there are
1033 levels of parameters. */
1034 gcc_assert (TMPL_ARGS_DEPTH (args)
1035 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1036 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1037 : template_class_depth (DECL_CONTEXT (tmpl))));
1039 if (optimize_specialization_lookup_p (tmpl))
1041 tree class_template;
1042 tree class_specialization;
1043 vec<tree, va_gc> *methods;
1044 tree fns;
1045 int idx;
1047 /* The template arguments actually apply to the containing
1048 class. Find the class specialization with those
1049 arguments. */
1050 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1051 class_specialization
1052 = retrieve_specialization (class_template, args, 0);
1053 if (!class_specialization)
1054 return NULL_TREE;
1055 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1056 for the specialization. */
1057 idx = class_method_index_for_fn (class_specialization, tmpl);
1058 if (idx == -1)
1059 return NULL_TREE;
1060 /* Iterate through the methods with the indicated name, looking
1061 for the one that has an instance of TMPL. */
1062 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1063 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1065 tree fn = OVL_CURRENT (fns);
1066 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1067 /* using-declarations can add base methods to the method vec,
1068 and we don't want those here. */
1069 && DECL_CONTEXT (fn) == class_specialization)
1070 return fn;
1072 return NULL_TREE;
1074 else
1076 spec_entry *found;
1077 spec_entry elt;
1078 htab_t specializations;
1080 elt.tmpl = tmpl;
1081 elt.args = args;
1082 elt.spec = NULL_TREE;
1084 if (DECL_CLASS_TEMPLATE_P (tmpl))
1085 specializations = type_specializations;
1086 else
1087 specializations = decl_specializations;
1089 if (hash == 0)
1090 hash = hash_specialization (&elt);
1091 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1092 if (found)
1093 return found->spec;
1096 return NULL_TREE;
1099 /* Like retrieve_specialization, but for local declarations. */
1101 static tree
1102 retrieve_local_specialization (tree tmpl)
1104 void **slot;
1106 if (local_specializations == NULL)
1107 return NULL_TREE;
1109 slot = pointer_map_contains (local_specializations, tmpl);
1110 return slot ? (tree) *slot : NULL_TREE;
1113 /* Returns nonzero iff DECL is a specialization of TMPL. */
1116 is_specialization_of (tree decl, tree tmpl)
1118 tree t;
1120 if (TREE_CODE (decl) == FUNCTION_DECL)
1122 for (t = decl;
1123 t != NULL_TREE;
1124 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1125 if (t == tmpl)
1126 return 1;
1128 else
1130 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1132 for (t = TREE_TYPE (decl);
1133 t != NULL_TREE;
1134 t = CLASSTYPE_USE_TEMPLATE (t)
1135 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1136 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1137 return 1;
1140 return 0;
1143 /* Returns nonzero iff DECL is a specialization of friend declaration
1144 FRIEND_DECL according to [temp.friend]. */
1146 bool
1147 is_specialization_of_friend (tree decl, tree friend_decl)
1149 bool need_template = true;
1150 int template_depth;
1152 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1153 || TREE_CODE (decl) == TYPE_DECL);
1155 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1156 of a template class, we want to check if DECL is a specialization
1157 if this. */
1158 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1159 && DECL_TEMPLATE_INFO (friend_decl)
1160 && !DECL_USE_TEMPLATE (friend_decl))
1162 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1163 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1164 need_template = false;
1166 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1167 && !PRIMARY_TEMPLATE_P (friend_decl))
1168 need_template = false;
1170 /* There is nothing to do if this is not a template friend. */
1171 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1172 return false;
1174 if (is_specialization_of (decl, friend_decl))
1175 return true;
1177 /* [temp.friend/6]
1178 A member of a class template may be declared to be a friend of a
1179 non-template class. In this case, the corresponding member of
1180 every specialization of the class template is a friend of the
1181 class granting friendship.
1183 For example, given a template friend declaration
1185 template <class T> friend void A<T>::f();
1187 the member function below is considered a friend
1189 template <> struct A<int> {
1190 void f();
1193 For this type of template friend, TEMPLATE_DEPTH below will be
1194 nonzero. To determine if DECL is a friend of FRIEND, we first
1195 check if the enclosing class is a specialization of another. */
1197 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1198 if (template_depth
1199 && DECL_CLASS_SCOPE_P (decl)
1200 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1201 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1203 /* Next, we check the members themselves. In order to handle
1204 a few tricky cases, such as when FRIEND_DECL's are
1206 template <class T> friend void A<T>::g(T t);
1207 template <class T> template <T t> friend void A<T>::h();
1209 and DECL's are
1211 void A<int>::g(int);
1212 template <int> void A<int>::h();
1214 we need to figure out ARGS, the template arguments from
1215 the context of DECL. This is required for template substitution
1216 of `T' in the function parameter of `g' and template parameter
1217 of `h' in the above examples. Here ARGS corresponds to `int'. */
1219 tree context = DECL_CONTEXT (decl);
1220 tree args = NULL_TREE;
1221 int current_depth = 0;
1223 while (current_depth < template_depth)
1225 if (CLASSTYPE_TEMPLATE_INFO (context))
1227 if (current_depth == 0)
1228 args = TYPE_TI_ARGS (context);
1229 else
1230 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1231 current_depth++;
1233 context = TYPE_CONTEXT (context);
1236 if (TREE_CODE (decl) == FUNCTION_DECL)
1238 bool is_template;
1239 tree friend_type;
1240 tree decl_type;
1241 tree friend_args_type;
1242 tree decl_args_type;
1244 /* Make sure that both DECL and FRIEND_DECL are templates or
1245 non-templates. */
1246 is_template = DECL_TEMPLATE_INFO (decl)
1247 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1248 if (need_template ^ is_template)
1249 return false;
1250 else if (is_template)
1252 /* If both are templates, check template parameter list. */
1253 tree friend_parms
1254 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1255 args, tf_none);
1256 if (!comp_template_parms
1257 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1258 friend_parms))
1259 return false;
1261 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1263 else
1264 decl_type = TREE_TYPE (decl);
1266 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1267 tf_none, NULL_TREE);
1268 if (friend_type == error_mark_node)
1269 return false;
1271 /* Check if return types match. */
1272 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1273 return false;
1275 /* Check if function parameter types match, ignoring the
1276 `this' parameter. */
1277 friend_args_type = TYPE_ARG_TYPES (friend_type);
1278 decl_args_type = TYPE_ARG_TYPES (decl_type);
1279 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1280 friend_args_type = TREE_CHAIN (friend_args_type);
1281 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1282 decl_args_type = TREE_CHAIN (decl_args_type);
1284 return compparms (decl_args_type, friend_args_type);
1286 else
1288 /* DECL is a TYPE_DECL */
1289 bool is_template;
1290 tree decl_type = TREE_TYPE (decl);
1292 /* Make sure that both DECL and FRIEND_DECL are templates or
1293 non-templates. */
1294 is_template
1295 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1296 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1298 if (need_template ^ is_template)
1299 return false;
1300 else if (is_template)
1302 tree friend_parms;
1303 /* If both are templates, check the name of the two
1304 TEMPLATE_DECL's first because is_friend didn't. */
1305 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1306 != DECL_NAME (friend_decl))
1307 return false;
1309 /* Now check template parameter list. */
1310 friend_parms
1311 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1312 args, tf_none);
1313 return comp_template_parms
1314 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1315 friend_parms);
1317 else
1318 return (DECL_NAME (decl)
1319 == DECL_NAME (friend_decl));
1322 return false;
1325 /* Register the specialization SPEC as a specialization of TMPL with
1326 the indicated ARGS. IS_FRIEND indicates whether the specialization
1327 is actually just a friend declaration. Returns SPEC, or an
1328 equivalent prior declaration, if available.
1330 We also store instantiations of field packs in the hash table, even
1331 though they are not themselves templates, to make lookup easier. */
1333 static tree
1334 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1335 hashval_t hash)
1337 tree fn;
1338 void **slot = NULL;
1339 spec_entry elt;
1341 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1342 || (TREE_CODE (tmpl) == FIELD_DECL
1343 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1345 if (TREE_CODE (spec) == FUNCTION_DECL
1346 && uses_template_parms (DECL_TI_ARGS (spec)))
1347 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1348 register it; we want the corresponding TEMPLATE_DECL instead.
1349 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1350 the more obvious `uses_template_parms (spec)' to avoid problems
1351 with default function arguments. In particular, given
1352 something like this:
1354 template <class T> void f(T t1, T t = T())
1356 the default argument expression is not substituted for in an
1357 instantiation unless and until it is actually needed. */
1358 return spec;
1360 if (optimize_specialization_lookup_p (tmpl))
1361 /* We don't put these specializations in the hash table, but we might
1362 want to give an error about a mismatch. */
1363 fn = retrieve_specialization (tmpl, args, 0);
1364 else
1366 elt.tmpl = tmpl;
1367 elt.args = args;
1368 elt.spec = spec;
1370 if (hash == 0)
1371 hash = hash_specialization (&elt);
1373 slot =
1374 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1375 if (*slot)
1376 fn = ((spec_entry *) *slot)->spec;
1377 else
1378 fn = NULL_TREE;
1381 /* We can sometimes try to re-register a specialization that we've
1382 already got. In particular, regenerate_decl_from_template calls
1383 duplicate_decls which will update the specialization list. But,
1384 we'll still get called again here anyhow. It's more convenient
1385 to simply allow this than to try to prevent it. */
1386 if (fn == spec)
1387 return spec;
1388 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1390 if (DECL_TEMPLATE_INSTANTIATION (fn))
1392 if (DECL_ODR_USED (fn)
1393 || DECL_EXPLICIT_INSTANTIATION (fn))
1395 error ("specialization of %qD after instantiation",
1396 fn);
1397 return error_mark_node;
1399 else
1401 tree clone;
1402 /* This situation should occur only if the first
1403 specialization is an implicit instantiation, the
1404 second is an explicit specialization, and the
1405 implicit instantiation has not yet been used. That
1406 situation can occur if we have implicitly
1407 instantiated a member function and then specialized
1408 it later.
1410 We can also wind up here if a friend declaration that
1411 looked like an instantiation turns out to be a
1412 specialization:
1414 template <class T> void foo(T);
1415 class S { friend void foo<>(int) };
1416 template <> void foo(int);
1418 We transform the existing DECL in place so that any
1419 pointers to it become pointers to the updated
1420 declaration.
1422 If there was a definition for the template, but not
1423 for the specialization, we want this to look as if
1424 there were no definition, and vice versa. */
1425 DECL_INITIAL (fn) = NULL_TREE;
1426 duplicate_decls (spec, fn, is_friend);
1427 /* The call to duplicate_decls will have applied
1428 [temp.expl.spec]:
1430 An explicit specialization of a function template
1431 is inline only if it is explicitly declared to be,
1432 and independently of whether its function template
1435 to the primary function; now copy the inline bits to
1436 the various clones. */
1437 FOR_EACH_CLONE (clone, fn)
1439 DECL_DECLARED_INLINE_P (clone)
1440 = DECL_DECLARED_INLINE_P (fn);
1441 DECL_SOURCE_LOCATION (clone)
1442 = DECL_SOURCE_LOCATION (fn);
1444 check_specialization_namespace (tmpl);
1446 return fn;
1449 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1451 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1452 /* Dup decl failed, but this is a new definition. Set the
1453 line number so any errors match this new
1454 definition. */
1455 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1457 return fn;
1460 else if (fn)
1461 return duplicate_decls (spec, fn, is_friend);
1463 /* A specialization must be declared in the same namespace as the
1464 template it is specializing. */
1465 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1466 && !check_specialization_namespace (tmpl))
1467 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1469 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1471 spec_entry *entry = ggc_alloc_spec_entry ();
1472 gcc_assert (tmpl && args && spec);
1473 *entry = elt;
1474 *slot = entry;
1475 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1476 && PRIMARY_TEMPLATE_P (tmpl)
1477 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1478 /* TMPL is a forward declaration of a template function; keep a list
1479 of all specializations in case we need to reassign them to a friend
1480 template later in tsubst_friend_function. */
1481 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1482 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1485 return spec;
1488 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1489 TMPL and ARGS members, ignores SPEC. */
1491 int comparing_specializations;
1493 static int
1494 eq_specializations (const void *p1, const void *p2)
1496 const spec_entry *e1 = (const spec_entry *)p1;
1497 const spec_entry *e2 = (const spec_entry *)p2;
1498 int equal;
1500 ++comparing_specializations;
1501 equal = (e1->tmpl == e2->tmpl
1502 && comp_template_args (e1->args, e2->args));
1503 --comparing_specializations;
1505 return equal;
1508 /* Returns a hash for a template TMPL and template arguments ARGS. */
1510 static hashval_t
1511 hash_tmpl_and_args (tree tmpl, tree args)
1513 hashval_t val = DECL_UID (tmpl);
1514 return iterative_hash_template_arg (args, val);
1517 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1518 ignoring SPEC. */
1520 static hashval_t
1521 hash_specialization (const void *p)
1523 const spec_entry *e = (const spec_entry *)p;
1524 return hash_tmpl_and_args (e->tmpl, e->args);
1527 /* Recursively calculate a hash value for a template argument ARG, for use
1528 in the hash tables of template specializations. */
1530 hashval_t
1531 iterative_hash_template_arg (tree arg, hashval_t val)
1533 unsigned HOST_WIDE_INT i;
1534 enum tree_code code;
1535 char tclass;
1537 if (arg == NULL_TREE)
1538 return iterative_hash_object (arg, val);
1540 if (!TYPE_P (arg))
1541 STRIP_NOPS (arg);
1543 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1544 /* We can get one of these when re-hashing a previous entry in the middle
1545 of substituting into a pack expansion. Just look through it. */
1546 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1548 code = TREE_CODE (arg);
1549 tclass = TREE_CODE_CLASS (code);
1551 val = iterative_hash_object (code, val);
1553 switch (code)
1555 case ERROR_MARK:
1556 return val;
1558 case IDENTIFIER_NODE:
1559 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1561 case TREE_VEC:
1563 int i, len = TREE_VEC_LENGTH (arg);
1564 for (i = 0; i < len; ++i)
1565 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1566 return val;
1569 case TYPE_PACK_EXPANSION:
1570 case EXPR_PACK_EXPANSION:
1571 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1572 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1574 case TYPE_ARGUMENT_PACK:
1575 case NONTYPE_ARGUMENT_PACK:
1576 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1578 case TREE_LIST:
1579 for (; arg; arg = TREE_CHAIN (arg))
1580 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1581 return val;
1583 case OVERLOAD:
1584 for (; arg; arg = OVL_NEXT (arg))
1585 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1586 return val;
1588 case CONSTRUCTOR:
1590 tree field, value;
1591 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1593 val = iterative_hash_template_arg (field, val);
1594 val = iterative_hash_template_arg (value, val);
1596 return val;
1599 case PARM_DECL:
1600 if (!DECL_ARTIFICIAL (arg))
1602 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1603 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1605 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1607 case TARGET_EXPR:
1608 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1610 case PTRMEM_CST:
1611 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1612 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1614 case TEMPLATE_PARM_INDEX:
1615 val = iterative_hash_template_arg
1616 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1617 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1618 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1620 case TRAIT_EXPR:
1621 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1622 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1623 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1625 case BASELINK:
1626 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1627 val);
1628 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1629 val);
1631 case MODOP_EXPR:
1632 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1633 code = TREE_CODE (TREE_OPERAND (arg, 1));
1634 val = iterative_hash_object (code, val);
1635 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1637 case LAMBDA_EXPR:
1638 /* A lambda can't appear in a template arg, but don't crash on
1639 erroneous input. */
1640 gcc_assert (seen_error ());
1641 return val;
1643 case CAST_EXPR:
1644 case IMPLICIT_CONV_EXPR:
1645 case STATIC_CAST_EXPR:
1646 case REINTERPRET_CAST_EXPR:
1647 case CONST_CAST_EXPR:
1648 case DYNAMIC_CAST_EXPR:
1649 case NEW_EXPR:
1650 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1651 /* Now hash operands as usual. */
1652 break;
1654 default:
1655 break;
1658 switch (tclass)
1660 case tcc_type:
1661 if (TYPE_CANONICAL (arg))
1662 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1663 val);
1664 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1665 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1666 /* Otherwise just compare the types during lookup. */
1667 return val;
1669 case tcc_declaration:
1670 case tcc_constant:
1671 return iterative_hash_expr (arg, val);
1673 default:
1674 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1676 unsigned n = cp_tree_operand_length (arg);
1677 for (i = 0; i < n; ++i)
1678 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1679 return val;
1682 gcc_unreachable ();
1683 return 0;
1686 /* Unregister the specialization SPEC as a specialization of TMPL.
1687 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1688 if the SPEC was listed as a specialization of TMPL.
1690 Note that SPEC has been ggc_freed, so we can't look inside it. */
1692 bool
1693 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1695 spec_entry *entry;
1696 spec_entry elt;
1698 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1699 elt.args = TI_ARGS (tinfo);
1700 elt.spec = NULL_TREE;
1702 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1703 if (entry != NULL)
1705 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1706 gcc_assert (new_spec != NULL_TREE);
1707 entry->spec = new_spec;
1708 return 1;
1711 return 0;
1714 /* Like register_specialization, but for local declarations. We are
1715 registering SPEC, an instantiation of TMPL. */
1717 static void
1718 register_local_specialization (tree spec, tree tmpl)
1720 void **slot;
1722 slot = pointer_map_insert (local_specializations, tmpl);
1723 *slot = spec;
1726 /* TYPE is a class type. Returns true if TYPE is an explicitly
1727 specialized class. */
1729 bool
1730 explicit_class_specialization_p (tree type)
1732 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1733 return false;
1734 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1737 /* Print the list of functions at FNS, going through all the overloads
1738 for each element of the list. Alternatively, FNS can not be a
1739 TREE_LIST, in which case it will be printed together with all the
1740 overloads.
1742 MORE and *STR should respectively be FALSE and NULL when the function
1743 is called from the outside. They are used internally on recursive
1744 calls. print_candidates manages the two parameters and leaves NULL
1745 in *STR when it ends. */
1747 static void
1748 print_candidates_1 (tree fns, bool more, const char **str)
1750 tree fn, fn2;
1751 char *spaces = NULL;
1753 for (fn = fns; fn; fn = OVL_NEXT (fn))
1754 if (TREE_CODE (fn) == TREE_LIST)
1756 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1757 print_candidates_1 (TREE_VALUE (fn2),
1758 TREE_CHAIN (fn2) || more, str);
1760 else
1762 tree cand = OVL_CURRENT (fn);
1763 if (!*str)
1765 /* Pick the prefix string. */
1766 if (!more && !OVL_NEXT (fns))
1768 inform (DECL_SOURCE_LOCATION (cand),
1769 "candidate is: %#D", cand);
1770 continue;
1773 *str = _("candidates are:");
1774 spaces = get_spaces (*str);
1776 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1777 *str = spaces ? spaces : *str;
1780 if (!more)
1782 free (spaces);
1783 *str = NULL;
1787 /* Print the list of candidate FNS in an error message. FNS can also
1788 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1790 void
1791 print_candidates (tree fns)
1793 const char *str = NULL;
1794 print_candidates_1 (fns, false, &str);
1795 gcc_assert (str == NULL);
1798 /* Returns the template (one of the functions given by TEMPLATE_ID)
1799 which can be specialized to match the indicated DECL with the
1800 explicit template args given in TEMPLATE_ID. The DECL may be
1801 NULL_TREE if none is available. In that case, the functions in
1802 TEMPLATE_ID are non-members.
1804 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1805 specialization of a member template.
1807 The TEMPLATE_COUNT is the number of references to qualifying
1808 template classes that appeared in the name of the function. See
1809 check_explicit_specialization for a more accurate description.
1811 TSK indicates what kind of template declaration (if any) is being
1812 declared. TSK_TEMPLATE indicates that the declaration given by
1813 DECL, though a FUNCTION_DECL, has template parameters, and is
1814 therefore a template function.
1816 The template args (those explicitly specified and those deduced)
1817 are output in a newly created vector *TARGS_OUT.
1819 If it is impossible to determine the result, an error message is
1820 issued. The error_mark_node is returned to indicate failure. */
1822 static tree
1823 determine_specialization (tree template_id,
1824 tree decl,
1825 tree* targs_out,
1826 int need_member_template,
1827 int template_count,
1828 tmpl_spec_kind tsk)
1830 tree fns;
1831 tree targs;
1832 tree explicit_targs;
1833 tree candidates = NULL_TREE;
1834 /* A TREE_LIST of templates of which DECL may be a specialization.
1835 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1836 corresponding TREE_PURPOSE is the set of template arguments that,
1837 when used to instantiate the template, would produce a function
1838 with the signature of DECL. */
1839 tree templates = NULL_TREE;
1840 int header_count;
1841 cp_binding_level *b;
1843 *targs_out = NULL_TREE;
1845 if (template_id == error_mark_node || decl == error_mark_node)
1846 return error_mark_node;
1848 /* We shouldn't be specializing a member template of an
1849 unspecialized class template; we already gave an error in
1850 check_specialization_scope, now avoid crashing. */
1851 if (template_count && DECL_CLASS_SCOPE_P (decl)
1852 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1854 gcc_assert (errorcount);
1855 return error_mark_node;
1858 fns = TREE_OPERAND (template_id, 0);
1859 explicit_targs = TREE_OPERAND (template_id, 1);
1861 if (fns == error_mark_node)
1862 return error_mark_node;
1864 /* Check for baselinks. */
1865 if (BASELINK_P (fns))
1866 fns = BASELINK_FUNCTIONS (fns);
1868 if (!is_overloaded_fn (fns))
1870 error ("%qD is not a function template", fns);
1871 return error_mark_node;
1874 /* Count the number of template headers specified for this
1875 specialization. */
1876 header_count = 0;
1877 for (b = current_binding_level;
1878 b->kind == sk_template_parms;
1879 b = b->level_chain)
1880 ++header_count;
1882 for (; fns; fns = OVL_NEXT (fns))
1884 tree fn = OVL_CURRENT (fns);
1886 if (TREE_CODE (fn) == TEMPLATE_DECL)
1888 tree decl_arg_types;
1889 tree fn_arg_types;
1890 tree insttype;
1892 /* In case of explicit specialization, we need to check if
1893 the number of template headers appearing in the specialization
1894 is correct. This is usually done in check_explicit_specialization,
1895 but the check done there cannot be exhaustive when specializing
1896 member functions. Consider the following code:
1898 template <> void A<int>::f(int);
1899 template <> template <> void A<int>::f(int);
1901 Assuming that A<int> is not itself an explicit specialization
1902 already, the first line specializes "f" which is a non-template
1903 member function, whilst the second line specializes "f" which
1904 is a template member function. So both lines are syntactically
1905 correct, and check_explicit_specialization does not reject
1906 them.
1908 Here, we can do better, as we are matching the specialization
1909 against the declarations. We count the number of template
1910 headers, and we check if they match TEMPLATE_COUNT + 1
1911 (TEMPLATE_COUNT is the number of qualifying template classes,
1912 plus there must be another header for the member template
1913 itself).
1915 Notice that if header_count is zero, this is not a
1916 specialization but rather a template instantiation, so there
1917 is no check we can perform here. */
1918 if (header_count && header_count != template_count + 1)
1919 continue;
1921 /* Check that the number of template arguments at the
1922 innermost level for DECL is the same as for FN. */
1923 if (current_binding_level->kind == sk_template_parms
1924 && !current_binding_level->explicit_spec_p
1925 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1926 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1927 (current_template_parms))))
1928 continue;
1930 /* DECL might be a specialization of FN. */
1931 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1932 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1934 /* For a non-static member function, we need to make sure
1935 that the const qualification is the same. Since
1936 get_bindings does not try to merge the "this" parameter,
1937 we must do the comparison explicitly. */
1938 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1939 && !same_type_p (TREE_VALUE (fn_arg_types),
1940 TREE_VALUE (decl_arg_types)))
1941 continue;
1943 /* Skip the "this" parameter and, for constructors of
1944 classes with virtual bases, the VTT parameter. A
1945 full specialization of a constructor will have a VTT
1946 parameter, but a template never will. */
1947 decl_arg_types
1948 = skip_artificial_parms_for (decl, decl_arg_types);
1949 fn_arg_types
1950 = skip_artificial_parms_for (fn, fn_arg_types);
1952 /* Function templates cannot be specializations; there are
1953 no partial specializations of functions. Therefore, if
1954 the type of DECL does not match FN, there is no
1955 match. */
1956 if (tsk == tsk_template)
1958 if (compparms (fn_arg_types, decl_arg_types))
1959 candidates = tree_cons (NULL_TREE, fn, candidates);
1960 continue;
1963 /* See whether this function might be a specialization of this
1964 template. Suppress access control because we might be trying
1965 to make this specialization a friend, and we have already done
1966 access control for the declaration of the specialization. */
1967 push_deferring_access_checks (dk_no_check);
1968 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1969 pop_deferring_access_checks ();
1971 if (!targs)
1972 /* We cannot deduce template arguments that when used to
1973 specialize TMPL will produce DECL. */
1974 continue;
1976 /* Make sure that the deduced arguments actually work. */
1977 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1978 if (insttype == error_mark_node)
1979 continue;
1980 fn_arg_types
1981 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1982 if (!compparms (fn_arg_types, decl_arg_types))
1983 continue;
1985 /* Save this template, and the arguments deduced. */
1986 templates = tree_cons (targs, fn, templates);
1988 else if (need_member_template)
1989 /* FN is an ordinary member function, and we need a
1990 specialization of a member template. */
1992 else if (TREE_CODE (fn) != FUNCTION_DECL)
1993 /* We can get IDENTIFIER_NODEs here in certain erroneous
1994 cases. */
1996 else if (!DECL_FUNCTION_MEMBER_P (fn))
1997 /* This is just an ordinary non-member function. Nothing can
1998 be a specialization of that. */
2000 else if (DECL_ARTIFICIAL (fn))
2001 /* Cannot specialize functions that are created implicitly. */
2003 else
2005 tree decl_arg_types;
2007 /* This is an ordinary member function. However, since
2008 we're here, we can assume its enclosing class is a
2009 template class. For example,
2011 template <typename T> struct S { void f(); };
2012 template <> void S<int>::f() {}
2014 Here, S<int>::f is a non-template, but S<int> is a
2015 template class. If FN has the same type as DECL, we
2016 might be in business. */
2018 if (!DECL_TEMPLATE_INFO (fn))
2019 /* Its enclosing class is an explicit specialization
2020 of a template class. This is not a candidate. */
2021 continue;
2023 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2024 TREE_TYPE (TREE_TYPE (fn))))
2025 /* The return types differ. */
2026 continue;
2028 /* Adjust the type of DECL in case FN is a static member. */
2029 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2030 if (DECL_STATIC_FUNCTION_P (fn)
2031 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2032 decl_arg_types = TREE_CHAIN (decl_arg_types);
2034 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2035 decl_arg_types))
2036 /* They match! */
2037 candidates = tree_cons (NULL_TREE, fn, candidates);
2041 if (templates && TREE_CHAIN (templates))
2043 /* We have:
2045 [temp.expl.spec]
2047 It is possible for a specialization with a given function
2048 signature to be instantiated from more than one function
2049 template. In such cases, explicit specification of the
2050 template arguments must be used to uniquely identify the
2051 function template specialization being specialized.
2053 Note that here, there's no suggestion that we're supposed to
2054 determine which of the candidate templates is most
2055 specialized. However, we, also have:
2057 [temp.func.order]
2059 Partial ordering of overloaded function template
2060 declarations is used in the following contexts to select
2061 the function template to which a function template
2062 specialization refers:
2064 -- when an explicit specialization refers to a function
2065 template.
2067 So, we do use the partial ordering rules, at least for now.
2068 This extension can only serve to make invalid programs valid,
2069 so it's safe. And, there is strong anecdotal evidence that
2070 the committee intended the partial ordering rules to apply;
2071 the EDG front end has that behavior, and John Spicer claims
2072 that the committee simply forgot to delete the wording in
2073 [temp.expl.spec]. */
2074 tree tmpl = most_specialized_instantiation (templates);
2075 if (tmpl != error_mark_node)
2077 templates = tmpl;
2078 TREE_CHAIN (templates) = NULL_TREE;
2082 if (templates == NULL_TREE && candidates == NULL_TREE)
2084 error ("template-id %qD for %q+D does not match any template "
2085 "declaration", template_id, decl);
2086 if (header_count && header_count != template_count + 1)
2087 inform (input_location, "saw %d %<template<>%>, need %d for "
2088 "specializing a member function template",
2089 header_count, template_count + 1);
2090 return error_mark_node;
2092 else if ((templates && TREE_CHAIN (templates))
2093 || (candidates && TREE_CHAIN (candidates))
2094 || (templates && candidates))
2096 error ("ambiguous template specialization %qD for %q+D",
2097 template_id, decl);
2098 candidates = chainon (candidates, templates);
2099 print_candidates (candidates);
2100 return error_mark_node;
2103 /* We have one, and exactly one, match. */
2104 if (candidates)
2106 tree fn = TREE_VALUE (candidates);
2107 *targs_out = copy_node (DECL_TI_ARGS (fn));
2108 /* DECL is a re-declaration or partial instantiation of a template
2109 function. */
2110 if (TREE_CODE (fn) == TEMPLATE_DECL)
2111 return fn;
2112 /* It was a specialization of an ordinary member function in a
2113 template class. */
2114 return DECL_TI_TEMPLATE (fn);
2117 /* It was a specialization of a template. */
2118 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2119 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2121 *targs_out = copy_node (targs);
2122 SET_TMPL_ARGS_LEVEL (*targs_out,
2123 TMPL_ARGS_DEPTH (*targs_out),
2124 TREE_PURPOSE (templates));
2126 else
2127 *targs_out = TREE_PURPOSE (templates);
2128 return TREE_VALUE (templates);
2131 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2132 but with the default argument values filled in from those in the
2133 TMPL_TYPES. */
2135 static tree
2136 copy_default_args_to_explicit_spec_1 (tree spec_types,
2137 tree tmpl_types)
2139 tree new_spec_types;
2141 if (!spec_types)
2142 return NULL_TREE;
2144 if (spec_types == void_list_node)
2145 return void_list_node;
2147 /* Substitute into the rest of the list. */
2148 new_spec_types =
2149 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2150 TREE_CHAIN (tmpl_types));
2152 /* Add the default argument for this parameter. */
2153 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2154 TREE_VALUE (spec_types),
2155 new_spec_types);
2158 /* DECL is an explicit specialization. Replicate default arguments
2159 from the template it specializes. (That way, code like:
2161 template <class T> void f(T = 3);
2162 template <> void f(double);
2163 void g () { f (); }
2165 works, as required.) An alternative approach would be to look up
2166 the correct default arguments at the call-site, but this approach
2167 is consistent with how implicit instantiations are handled. */
2169 static void
2170 copy_default_args_to_explicit_spec (tree decl)
2172 tree tmpl;
2173 tree spec_types;
2174 tree tmpl_types;
2175 tree new_spec_types;
2176 tree old_type;
2177 tree new_type;
2178 tree t;
2179 tree object_type = NULL_TREE;
2180 tree in_charge = NULL_TREE;
2181 tree vtt = NULL_TREE;
2183 /* See if there's anything we need to do. */
2184 tmpl = DECL_TI_TEMPLATE (decl);
2185 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2186 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2187 if (TREE_PURPOSE (t))
2188 break;
2189 if (!t)
2190 return;
2192 old_type = TREE_TYPE (decl);
2193 spec_types = TYPE_ARG_TYPES (old_type);
2195 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2197 /* Remove the this pointer, but remember the object's type for
2198 CV quals. */
2199 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2200 spec_types = TREE_CHAIN (spec_types);
2201 tmpl_types = TREE_CHAIN (tmpl_types);
2203 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2205 /* DECL may contain more parameters than TMPL due to the extra
2206 in-charge parameter in constructors and destructors. */
2207 in_charge = spec_types;
2208 spec_types = TREE_CHAIN (spec_types);
2210 if (DECL_HAS_VTT_PARM_P (decl))
2212 vtt = spec_types;
2213 spec_types = TREE_CHAIN (spec_types);
2217 /* Compute the merged default arguments. */
2218 new_spec_types =
2219 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2221 /* Compute the new FUNCTION_TYPE. */
2222 if (object_type)
2224 if (vtt)
2225 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2226 TREE_VALUE (vtt),
2227 new_spec_types);
2229 if (in_charge)
2230 /* Put the in-charge parameter back. */
2231 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2232 TREE_VALUE (in_charge),
2233 new_spec_types);
2235 new_type = build_method_type_directly (object_type,
2236 TREE_TYPE (old_type),
2237 new_spec_types);
2239 else
2240 new_type = build_function_type (TREE_TYPE (old_type),
2241 new_spec_types);
2242 new_type = cp_build_type_attribute_variant (new_type,
2243 TYPE_ATTRIBUTES (old_type));
2244 new_type = build_exception_variant (new_type,
2245 TYPE_RAISES_EXCEPTIONS (old_type));
2246 TREE_TYPE (decl) = new_type;
2249 /* Return the number of template headers we expect to see for a definition
2250 or specialization of CTYPE or one of its non-template members. */
2253 num_template_headers_for_class (tree ctype)
2255 int num_templates = 0;
2257 while (ctype && CLASS_TYPE_P (ctype))
2259 /* You're supposed to have one `template <...>' for every
2260 template class, but you don't need one for a full
2261 specialization. For example:
2263 template <class T> struct S{};
2264 template <> struct S<int> { void f(); };
2265 void S<int>::f () {}
2267 is correct; there shouldn't be a `template <>' for the
2268 definition of `S<int>::f'. */
2269 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2270 /* If CTYPE does not have template information of any
2271 kind, then it is not a template, nor is it nested
2272 within a template. */
2273 break;
2274 if (explicit_class_specialization_p (ctype))
2275 break;
2276 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2277 ++num_templates;
2279 ctype = TYPE_CONTEXT (ctype);
2282 return num_templates;
2285 /* Do a simple sanity check on the template headers that precede the
2286 variable declaration DECL. */
2288 void
2289 check_template_variable (tree decl)
2291 tree ctx = CP_DECL_CONTEXT (decl);
2292 int wanted = num_template_headers_for_class (ctx);
2293 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2294 permerror (DECL_SOURCE_LOCATION (decl),
2295 "%qD is not a static data member of a class template", decl);
2296 else if (template_header_count > wanted)
2298 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2299 "too many template headers for %D (should be %d)",
2300 decl, wanted);
2301 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2302 inform (DECL_SOURCE_LOCATION (decl),
2303 "members of an explicitly specialized class are defined "
2304 "without a template header");
2308 /* Check to see if the function just declared, as indicated in
2309 DECLARATOR, and in DECL, is a specialization of a function
2310 template. We may also discover that the declaration is an explicit
2311 instantiation at this point.
2313 Returns DECL, or an equivalent declaration that should be used
2314 instead if all goes well. Issues an error message if something is
2315 amiss. Returns error_mark_node if the error is not easily
2316 recoverable.
2318 FLAGS is a bitmask consisting of the following flags:
2320 2: The function has a definition.
2321 4: The function is a friend.
2323 The TEMPLATE_COUNT is the number of references to qualifying
2324 template classes that appeared in the name of the function. For
2325 example, in
2327 template <class T> struct S { void f(); };
2328 void S<int>::f();
2330 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2331 classes are not counted in the TEMPLATE_COUNT, so that in
2333 template <class T> struct S {};
2334 template <> struct S<int> { void f(); }
2335 template <> void S<int>::f();
2337 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2338 invalid; there should be no template <>.)
2340 If the function is a specialization, it is marked as such via
2341 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2342 is set up correctly, and it is added to the list of specializations
2343 for that template. */
2345 tree
2346 check_explicit_specialization (tree declarator,
2347 tree decl,
2348 int template_count,
2349 int flags)
2351 int have_def = flags & 2;
2352 int is_friend = flags & 4;
2353 int specialization = 0;
2354 int explicit_instantiation = 0;
2355 int member_specialization = 0;
2356 tree ctype = DECL_CLASS_CONTEXT (decl);
2357 tree dname = DECL_NAME (decl);
2358 tmpl_spec_kind tsk;
2360 if (is_friend)
2362 if (!processing_specialization)
2363 tsk = tsk_none;
2364 else
2365 tsk = tsk_excessive_parms;
2367 else
2368 tsk = current_tmpl_spec_kind (template_count);
2370 switch (tsk)
2372 case tsk_none:
2373 if (processing_specialization)
2375 specialization = 1;
2376 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2378 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2380 if (is_friend)
2381 /* This could be something like:
2383 template <class T> void f(T);
2384 class S { friend void f<>(int); } */
2385 specialization = 1;
2386 else
2388 /* This case handles bogus declarations like template <>
2389 template <class T> void f<int>(); */
2391 error ("template-id %qD in declaration of primary template",
2392 declarator);
2393 return decl;
2396 break;
2398 case tsk_invalid_member_spec:
2399 /* The error has already been reported in
2400 check_specialization_scope. */
2401 return error_mark_node;
2403 case tsk_invalid_expl_inst:
2404 error ("template parameter list used in explicit instantiation");
2406 /* Fall through. */
2408 case tsk_expl_inst:
2409 if (have_def)
2410 error ("definition provided for explicit instantiation");
2412 explicit_instantiation = 1;
2413 break;
2415 case tsk_excessive_parms:
2416 case tsk_insufficient_parms:
2417 if (tsk == tsk_excessive_parms)
2418 error ("too many template parameter lists in declaration of %qD",
2419 decl);
2420 else if (template_header_count)
2421 error("too few template parameter lists in declaration of %qD", decl);
2422 else
2423 error("explicit specialization of %qD must be introduced by "
2424 "%<template <>%>", decl);
2426 /* Fall through. */
2427 case tsk_expl_spec:
2428 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2429 if (ctype)
2430 member_specialization = 1;
2431 else
2432 specialization = 1;
2433 break;
2435 case tsk_template:
2436 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2438 /* This case handles bogus declarations like template <>
2439 template <class T> void f<int>(); */
2441 if (uses_template_parms (declarator))
2442 error ("function template partial specialization %qD "
2443 "is not allowed", declarator);
2444 else
2445 error ("template-id %qD in declaration of primary template",
2446 declarator);
2447 return decl;
2450 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2451 /* This is a specialization of a member template, without
2452 specialization the containing class. Something like:
2454 template <class T> struct S {
2455 template <class U> void f (U);
2457 template <> template <class U> void S<int>::f(U) {}
2459 That's a specialization -- but of the entire template. */
2460 specialization = 1;
2461 break;
2463 default:
2464 gcc_unreachable ();
2467 if (specialization || member_specialization)
2469 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2470 for (; t; t = TREE_CHAIN (t))
2471 if (TREE_PURPOSE (t))
2473 permerror (input_location,
2474 "default argument specified in explicit specialization");
2475 break;
2479 if (specialization || member_specialization || explicit_instantiation)
2481 tree tmpl = NULL_TREE;
2482 tree targs = NULL_TREE;
2484 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2485 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2487 tree fns;
2489 gcc_assert (identifier_p (declarator));
2490 if (ctype)
2491 fns = dname;
2492 else
2494 /* If there is no class context, the explicit instantiation
2495 must be at namespace scope. */
2496 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2498 /* Find the namespace binding, using the declaration
2499 context. */
2500 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2501 false, true);
2502 if (fns == error_mark_node || !is_overloaded_fn (fns))
2504 error ("%qD is not a template function", dname);
2505 fns = error_mark_node;
2507 else
2509 tree fn = OVL_CURRENT (fns);
2510 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2511 CP_DECL_CONTEXT (fn)))
2512 error ("%qD is not declared in %qD",
2513 decl, current_namespace);
2517 declarator = lookup_template_function (fns, NULL_TREE);
2520 if (declarator == error_mark_node)
2521 return error_mark_node;
2523 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2525 if (!explicit_instantiation)
2526 /* A specialization in class scope. This is invalid,
2527 but the error will already have been flagged by
2528 check_specialization_scope. */
2529 return error_mark_node;
2530 else
2532 /* It's not valid to write an explicit instantiation in
2533 class scope, e.g.:
2535 class C { template void f(); }
2537 This case is caught by the parser. However, on
2538 something like:
2540 template class C { void f(); };
2542 (which is invalid) we can get here. The error will be
2543 issued later. */
2547 return decl;
2549 else if (ctype != NULL_TREE
2550 && (identifier_p (TREE_OPERAND (declarator, 0))))
2552 /* Find the list of functions in ctype that have the same
2553 name as the declared function. */
2554 tree name = TREE_OPERAND (declarator, 0);
2555 tree fns = NULL_TREE;
2556 int idx;
2558 if (constructor_name_p (name, ctype))
2560 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2562 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2563 : !CLASSTYPE_DESTRUCTORS (ctype))
2565 /* From [temp.expl.spec]:
2567 If such an explicit specialization for the member
2568 of a class template names an implicitly-declared
2569 special member function (clause _special_), the
2570 program is ill-formed.
2572 Similar language is found in [temp.explicit]. */
2573 error ("specialization of implicitly-declared special member function");
2574 return error_mark_node;
2577 name = is_constructor ? ctor_identifier : dtor_identifier;
2580 if (!DECL_CONV_FN_P (decl))
2582 idx = lookup_fnfields_1 (ctype, name);
2583 if (idx >= 0)
2584 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2586 else
2588 vec<tree, va_gc> *methods;
2589 tree ovl;
2591 /* For a type-conversion operator, we cannot do a
2592 name-based lookup. We might be looking for `operator
2593 int' which will be a specialization of `operator T'.
2594 So, we find *all* the conversion operators, and then
2595 select from them. */
2596 fns = NULL_TREE;
2598 methods = CLASSTYPE_METHOD_VEC (ctype);
2599 if (methods)
2600 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2601 methods->iterate (idx, &ovl);
2602 ++idx)
2604 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2605 /* There are no more conversion functions. */
2606 break;
2608 /* Glue all these conversion functions together
2609 with those we already have. */
2610 for (; ovl; ovl = OVL_NEXT (ovl))
2611 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2615 if (fns == NULL_TREE)
2617 error ("no member function %qD declared in %qT", name, ctype);
2618 return error_mark_node;
2620 else
2621 TREE_OPERAND (declarator, 0) = fns;
2624 /* Figure out what exactly is being specialized at this point.
2625 Note that for an explicit instantiation, even one for a
2626 member function, we cannot tell apriori whether the
2627 instantiation is for a member template, or just a member
2628 function of a template class. Even if a member template is
2629 being instantiated, the member template arguments may be
2630 elided if they can be deduced from the rest of the
2631 declaration. */
2632 tmpl = determine_specialization (declarator, decl,
2633 &targs,
2634 member_specialization,
2635 template_count,
2636 tsk);
2638 if (!tmpl || tmpl == error_mark_node)
2639 /* We couldn't figure out what this declaration was
2640 specializing. */
2641 return error_mark_node;
2642 else
2644 tree gen_tmpl = most_general_template (tmpl);
2646 if (explicit_instantiation)
2648 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2649 is done by do_decl_instantiation later. */
2651 int arg_depth = TMPL_ARGS_DEPTH (targs);
2652 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2654 if (arg_depth > parm_depth)
2656 /* If TMPL is not the most general template (for
2657 example, if TMPL is a friend template that is
2658 injected into namespace scope), then there will
2659 be too many levels of TARGS. Remove some of them
2660 here. */
2661 int i;
2662 tree new_targs;
2664 new_targs = make_tree_vec (parm_depth);
2665 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2666 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2667 = TREE_VEC_ELT (targs, i);
2668 targs = new_targs;
2671 return instantiate_template (tmpl, targs, tf_error);
2674 /* If we thought that the DECL was a member function, but it
2675 turns out to be specializing a static member function,
2676 make DECL a static member function as well. */
2677 if (DECL_STATIC_FUNCTION_P (tmpl)
2678 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2679 revert_static_member_fn (decl);
2681 /* If this is a specialization of a member template of a
2682 template class, we want to return the TEMPLATE_DECL, not
2683 the specialization of it. */
2684 if (tsk == tsk_template)
2686 tree result = DECL_TEMPLATE_RESULT (tmpl);
2687 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2688 DECL_INITIAL (result) = NULL_TREE;
2689 if (have_def)
2691 tree parm;
2692 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2693 DECL_SOURCE_LOCATION (result)
2694 = DECL_SOURCE_LOCATION (decl);
2695 /* We want to use the argument list specified in the
2696 definition, not in the original declaration. */
2697 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2698 for (parm = DECL_ARGUMENTS (result); parm;
2699 parm = DECL_CHAIN (parm))
2700 DECL_CONTEXT (parm) = result;
2702 return register_specialization (tmpl, gen_tmpl, targs,
2703 is_friend, 0);
2706 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2707 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2709 /* Inherit default function arguments from the template
2710 DECL is specializing. */
2711 copy_default_args_to_explicit_spec (decl);
2713 /* This specialization has the same protection as the
2714 template it specializes. */
2715 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2716 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2718 /* 7.1.1-1 [dcl.stc]
2720 A storage-class-specifier shall not be specified in an
2721 explicit specialization...
2723 The parser rejects these, so unless action is taken here,
2724 explicit function specializations will always appear with
2725 global linkage.
2727 The action recommended by the C++ CWG in response to C++
2728 defect report 605 is to make the storage class and linkage
2729 of the explicit specialization match the templated function:
2731 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2733 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2735 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2736 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2738 /* This specialization has the same linkage and visibility as
2739 the function template it specializes. */
2740 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2741 if (! TREE_PUBLIC (decl))
2743 DECL_INTERFACE_KNOWN (decl) = 1;
2744 DECL_NOT_REALLY_EXTERN (decl) = 1;
2746 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2747 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2749 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2750 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2754 /* If DECL is a friend declaration, declared using an
2755 unqualified name, the namespace associated with DECL may
2756 have been set incorrectly. For example, in:
2758 template <typename T> void f(T);
2759 namespace N {
2760 struct S { friend void f<int>(int); }
2763 we will have set the DECL_CONTEXT for the friend
2764 declaration to N, rather than to the global namespace. */
2765 if (DECL_NAMESPACE_SCOPE_P (decl))
2766 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2768 if (is_friend && !have_def)
2769 /* This is not really a declaration of a specialization.
2770 It's just the name of an instantiation. But, it's not
2771 a request for an instantiation, either. */
2772 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2773 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2774 /* This is indeed a specialization. In case of constructors
2775 and destructors, we need in-charge and not-in-charge
2776 versions in V3 ABI. */
2777 clone_function_decl (decl, /*update_method_vec_p=*/0);
2779 /* Register this specialization so that we can find it
2780 again. */
2781 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2785 return decl;
2788 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2789 parameters. These are represented in the same format used for
2790 DECL_TEMPLATE_PARMS. */
2793 comp_template_parms (const_tree parms1, const_tree parms2)
2795 const_tree p1;
2796 const_tree p2;
2798 if (parms1 == parms2)
2799 return 1;
2801 for (p1 = parms1, p2 = parms2;
2802 p1 != NULL_TREE && p2 != NULL_TREE;
2803 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2805 tree t1 = TREE_VALUE (p1);
2806 tree t2 = TREE_VALUE (p2);
2807 int i;
2809 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2810 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2812 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2813 return 0;
2815 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2817 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2818 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2820 /* If either of the template parameters are invalid, assume
2821 they match for the sake of error recovery. */
2822 if (parm1 == error_mark_node || parm2 == error_mark_node)
2823 return 1;
2825 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2826 return 0;
2828 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2829 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2830 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2831 continue;
2832 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2833 return 0;
2837 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2838 /* One set of parameters has more parameters lists than the
2839 other. */
2840 return 0;
2842 return 1;
2845 /* Determine whether PARM is a parameter pack. */
2847 bool
2848 template_parameter_pack_p (const_tree parm)
2850 /* Determine if we have a non-type template parameter pack. */
2851 if (TREE_CODE (parm) == PARM_DECL)
2852 return (DECL_TEMPLATE_PARM_P (parm)
2853 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2854 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2855 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2857 /* If this is a list of template parameters, we could get a
2858 TYPE_DECL or a TEMPLATE_DECL. */
2859 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2860 parm = TREE_TYPE (parm);
2862 /* Otherwise it must be a type template parameter. */
2863 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2864 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2865 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2868 /* Determine if T is a function parameter pack. */
2870 bool
2871 function_parameter_pack_p (const_tree t)
2873 if (t && TREE_CODE (t) == PARM_DECL)
2874 return DECL_PACK_P (t);
2875 return false;
2878 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2879 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2881 tree
2882 get_function_template_decl (const_tree primary_func_tmpl_inst)
2884 if (! primary_func_tmpl_inst
2885 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2886 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2887 return NULL;
2889 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2892 /* Return true iff the function parameter PARAM_DECL was expanded
2893 from the function parameter pack PACK. */
2895 bool
2896 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2898 if (DECL_ARTIFICIAL (param_decl)
2899 || !function_parameter_pack_p (pack))
2900 return false;
2902 /* The parameter pack and its pack arguments have the same
2903 DECL_PARM_INDEX. */
2904 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2907 /* Determine whether ARGS describes a variadic template args list,
2908 i.e., one that is terminated by a template argument pack. */
2910 static bool
2911 template_args_variadic_p (tree args)
2913 int nargs;
2914 tree last_parm;
2916 if (args == NULL_TREE)
2917 return false;
2919 args = INNERMOST_TEMPLATE_ARGS (args);
2920 nargs = TREE_VEC_LENGTH (args);
2922 if (nargs == 0)
2923 return false;
2925 last_parm = TREE_VEC_ELT (args, nargs - 1);
2927 return ARGUMENT_PACK_P (last_parm);
2930 /* Generate a new name for the parameter pack name NAME (an
2931 IDENTIFIER_NODE) that incorporates its */
2933 static tree
2934 make_ith_pack_parameter_name (tree name, int i)
2936 /* Munge the name to include the parameter index. */
2937 #define NUMBUF_LEN 128
2938 char numbuf[NUMBUF_LEN];
2939 char* newname;
2940 int newname_len;
2942 if (name == NULL_TREE)
2943 return name;
2944 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2945 newname_len = IDENTIFIER_LENGTH (name)
2946 + strlen (numbuf) + 2;
2947 newname = (char*)alloca (newname_len);
2948 snprintf (newname, newname_len,
2949 "%s#%i", IDENTIFIER_POINTER (name), i);
2950 return get_identifier (newname);
2953 /* Return true if T is a primary function, class or alias template
2954 instantiation. */
2956 bool
2957 primary_template_instantiation_p (const_tree t)
2959 if (!t)
2960 return false;
2962 if (TREE_CODE (t) == FUNCTION_DECL)
2963 return DECL_LANG_SPECIFIC (t)
2964 && DECL_TEMPLATE_INSTANTIATION (t)
2965 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2966 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2967 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2968 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2969 else if (alias_template_specialization_p (t))
2970 return true;
2971 return false;
2974 /* Return true if PARM is a template template parameter. */
2976 bool
2977 template_template_parameter_p (const_tree parm)
2979 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2982 /* Return true iff PARM is a DECL representing a type template
2983 parameter. */
2985 bool
2986 template_type_parameter_p (const_tree parm)
2988 return (parm
2989 && (TREE_CODE (parm) == TYPE_DECL
2990 || TREE_CODE (parm) == TEMPLATE_DECL)
2991 && DECL_TEMPLATE_PARM_P (parm));
2994 /* Return the template parameters of T if T is a
2995 primary template instantiation, NULL otherwise. */
2997 tree
2998 get_primary_template_innermost_parameters (const_tree t)
3000 tree parms = NULL, template_info = NULL;
3002 if ((template_info = get_template_info (t))
3003 && primary_template_instantiation_p (t))
3004 parms = INNERMOST_TEMPLATE_PARMS
3005 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3007 return parms;
3010 /* Return the template parameters of the LEVELth level from the full list
3011 of template parameters PARMS. */
3013 tree
3014 get_template_parms_at_level (tree parms, int level)
3016 tree p;
3017 if (!parms
3018 || TREE_CODE (parms) != TREE_LIST
3019 || level > TMPL_PARMS_DEPTH (parms))
3020 return NULL_TREE;
3022 for (p = parms; p; p = TREE_CHAIN (p))
3023 if (TMPL_PARMS_DEPTH (p) == level)
3024 return p;
3026 return NULL_TREE;
3029 /* Returns the template arguments of T if T is a template instantiation,
3030 NULL otherwise. */
3032 tree
3033 get_template_innermost_arguments (const_tree t)
3035 tree args = NULL, template_info = NULL;
3037 if ((template_info = get_template_info (t))
3038 && TI_ARGS (template_info))
3039 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3041 return args;
3044 /* Return the argument pack elements of T if T is a template argument pack,
3045 NULL otherwise. */
3047 tree
3048 get_template_argument_pack_elems (const_tree t)
3050 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3051 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3052 return NULL;
3054 return ARGUMENT_PACK_ARGS (t);
3057 /* Structure used to track the progress of find_parameter_packs_r. */
3058 struct find_parameter_pack_data
3060 /* TREE_LIST that will contain all of the parameter packs found by
3061 the traversal. */
3062 tree* parameter_packs;
3064 /* Set of AST nodes that have been visited by the traversal. */
3065 struct pointer_set_t *visited;
3068 /* Identifies all of the argument packs that occur in a template
3069 argument and appends them to the TREE_LIST inside DATA, which is a
3070 find_parameter_pack_data structure. This is a subroutine of
3071 make_pack_expansion and uses_parameter_packs. */
3072 static tree
3073 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3075 tree t = *tp;
3076 struct find_parameter_pack_data* ppd =
3077 (struct find_parameter_pack_data*)data;
3078 bool parameter_pack_p = false;
3080 /* Handle type aliases/typedefs. */
3081 if (TYPE_ALIAS_P (t))
3083 if (TYPE_TEMPLATE_INFO (t))
3084 cp_walk_tree (&TYPE_TI_ARGS (t),
3085 &find_parameter_packs_r,
3086 ppd, ppd->visited);
3087 *walk_subtrees = 0;
3088 return NULL_TREE;
3091 /* Identify whether this is a parameter pack or not. */
3092 switch (TREE_CODE (t))
3094 case TEMPLATE_PARM_INDEX:
3095 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3096 parameter_pack_p = true;
3097 break;
3099 case TEMPLATE_TYPE_PARM:
3100 t = TYPE_MAIN_VARIANT (t);
3101 case TEMPLATE_TEMPLATE_PARM:
3102 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3103 parameter_pack_p = true;
3104 break;
3106 case FIELD_DECL:
3107 case PARM_DECL:
3108 if (DECL_PACK_P (t))
3110 /* We don't want to walk into the type of a PARM_DECL,
3111 because we don't want to see the type parameter pack. */
3112 *walk_subtrees = 0;
3113 parameter_pack_p = true;
3115 break;
3117 /* Look through a lambda capture proxy to the field pack. */
3118 case VAR_DECL:
3119 if (DECL_HAS_VALUE_EXPR_P (t))
3121 tree v = DECL_VALUE_EXPR (t);
3122 cp_walk_tree (&v,
3123 &find_parameter_packs_r,
3124 ppd, ppd->visited);
3125 *walk_subtrees = 0;
3127 break;
3129 case BASES:
3130 parameter_pack_p = true;
3131 break;
3132 default:
3133 /* Not a parameter pack. */
3134 break;
3137 if (parameter_pack_p)
3139 /* Add this parameter pack to the list. */
3140 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3143 if (TYPE_P (t))
3144 cp_walk_tree (&TYPE_CONTEXT (t),
3145 &find_parameter_packs_r, ppd, ppd->visited);
3147 /* This switch statement will return immediately if we don't find a
3148 parameter pack. */
3149 switch (TREE_CODE (t))
3151 case TEMPLATE_PARM_INDEX:
3152 return NULL_TREE;
3154 case BOUND_TEMPLATE_TEMPLATE_PARM:
3155 /* Check the template itself. */
3156 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3157 &find_parameter_packs_r, ppd, ppd->visited);
3158 /* Check the template arguments. */
3159 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3160 ppd->visited);
3161 *walk_subtrees = 0;
3162 return NULL_TREE;
3164 case TEMPLATE_TYPE_PARM:
3165 case TEMPLATE_TEMPLATE_PARM:
3166 return NULL_TREE;
3168 case PARM_DECL:
3169 return NULL_TREE;
3171 case RECORD_TYPE:
3172 if (TYPE_PTRMEMFUNC_P (t))
3173 return NULL_TREE;
3174 /* Fall through. */
3176 case UNION_TYPE:
3177 case ENUMERAL_TYPE:
3178 if (TYPE_TEMPLATE_INFO (t))
3179 cp_walk_tree (&TYPE_TI_ARGS (t),
3180 &find_parameter_packs_r, ppd, ppd->visited);
3182 *walk_subtrees = 0;
3183 return NULL_TREE;
3185 case CONSTRUCTOR:
3186 case TEMPLATE_DECL:
3187 cp_walk_tree (&TREE_TYPE (t),
3188 &find_parameter_packs_r, ppd, ppd->visited);
3189 return NULL_TREE;
3191 case TYPENAME_TYPE:
3192 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3193 ppd, ppd->visited);
3194 *walk_subtrees = 0;
3195 return NULL_TREE;
3197 case TYPE_PACK_EXPANSION:
3198 case EXPR_PACK_EXPANSION:
3199 *walk_subtrees = 0;
3200 return NULL_TREE;
3202 case INTEGER_TYPE:
3203 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3204 ppd, ppd->visited);
3205 *walk_subtrees = 0;
3206 return NULL_TREE;
3208 case IDENTIFIER_NODE:
3209 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3210 ppd->visited);
3211 *walk_subtrees = 0;
3212 return NULL_TREE;
3214 default:
3215 return NULL_TREE;
3218 return NULL_TREE;
3221 /* Determines if the expression or type T uses any parameter packs. */
3222 bool
3223 uses_parameter_packs (tree t)
3225 tree parameter_packs = NULL_TREE;
3226 struct find_parameter_pack_data ppd;
3227 ppd.parameter_packs = &parameter_packs;
3228 ppd.visited = pointer_set_create ();
3229 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3230 pointer_set_destroy (ppd.visited);
3231 return parameter_packs != NULL_TREE;
3234 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3235 representation a base-class initializer into a parameter pack
3236 expansion. If all goes well, the resulting node will be an
3237 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3238 respectively. */
3239 tree
3240 make_pack_expansion (tree arg)
3242 tree result;
3243 tree parameter_packs = NULL_TREE;
3244 bool for_types = false;
3245 struct find_parameter_pack_data ppd;
3247 if (!arg || arg == error_mark_node)
3248 return arg;
3250 if (TREE_CODE (arg) == TREE_LIST)
3252 /* The only time we will see a TREE_LIST here is for a base
3253 class initializer. In this case, the TREE_PURPOSE will be a
3254 _TYPE node (representing the base class expansion we're
3255 initializing) and the TREE_VALUE will be a TREE_LIST
3256 containing the initialization arguments.
3258 The resulting expansion looks somewhat different from most
3259 expansions. Rather than returning just one _EXPANSION, we
3260 return a TREE_LIST whose TREE_PURPOSE is a
3261 TYPE_PACK_EXPANSION containing the bases that will be
3262 initialized. The TREE_VALUE will be identical to the
3263 original TREE_VALUE, which is a list of arguments that will
3264 be passed to each base. We do not introduce any new pack
3265 expansion nodes into the TREE_VALUE (although it is possible
3266 that some already exist), because the TREE_PURPOSE and
3267 TREE_VALUE all need to be expanded together with the same
3268 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3269 resulting TREE_PURPOSE will mention the parameter packs in
3270 both the bases and the arguments to the bases. */
3271 tree purpose;
3272 tree value;
3273 tree parameter_packs = NULL_TREE;
3275 /* Determine which parameter packs will be used by the base
3276 class expansion. */
3277 ppd.visited = pointer_set_create ();
3278 ppd.parameter_packs = &parameter_packs;
3279 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3280 &ppd, ppd.visited);
3282 if (parameter_packs == NULL_TREE)
3284 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3285 pointer_set_destroy (ppd.visited);
3286 return error_mark_node;
3289 if (TREE_VALUE (arg) != void_type_node)
3291 /* Collect the sets of parameter packs used in each of the
3292 initialization arguments. */
3293 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3295 /* Determine which parameter packs will be expanded in this
3296 argument. */
3297 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3298 &ppd, ppd.visited);
3302 pointer_set_destroy (ppd.visited);
3304 /* Create the pack expansion type for the base type. */
3305 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3306 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3307 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3309 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3310 they will rarely be compared to anything. */
3311 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3313 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3316 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3317 for_types = true;
3319 /* Build the PACK_EXPANSION_* node. */
3320 result = for_types
3321 ? cxx_make_type (TYPE_PACK_EXPANSION)
3322 : make_node (EXPR_PACK_EXPANSION);
3323 SET_PACK_EXPANSION_PATTERN (result, arg);
3324 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3326 /* Propagate type and const-expression information. */
3327 TREE_TYPE (result) = TREE_TYPE (arg);
3328 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3330 else
3331 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3332 they will rarely be compared to anything. */
3333 SET_TYPE_STRUCTURAL_EQUALITY (result);
3335 /* Determine which parameter packs will be expanded. */
3336 ppd.parameter_packs = &parameter_packs;
3337 ppd.visited = pointer_set_create ();
3338 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3339 pointer_set_destroy (ppd.visited);
3341 /* Make sure we found some parameter packs. */
3342 if (parameter_packs == NULL_TREE)
3344 if (TYPE_P (arg))
3345 error ("expansion pattern %<%T%> contains no argument packs", arg);
3346 else
3347 error ("expansion pattern %<%E%> contains no argument packs", arg);
3348 return error_mark_node;
3350 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3352 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3354 return result;
3357 /* Checks T for any "bare" parameter packs, which have not yet been
3358 expanded, and issues an error if any are found. This operation can
3359 only be done on full expressions or types (e.g., an expression
3360 statement, "if" condition, etc.), because we could have expressions like:
3362 foo(f(g(h(args)))...)
3364 where "args" is a parameter pack. check_for_bare_parameter_packs
3365 should not be called for the subexpressions args, h(args),
3366 g(h(args)), or f(g(h(args))), because we would produce erroneous
3367 error messages.
3369 Returns TRUE and emits an error if there were bare parameter packs,
3370 returns FALSE otherwise. */
3371 bool
3372 check_for_bare_parameter_packs (tree t)
3374 tree parameter_packs = NULL_TREE;
3375 struct find_parameter_pack_data ppd;
3377 if (!processing_template_decl || !t || t == error_mark_node)
3378 return false;
3380 if (TREE_CODE (t) == TYPE_DECL)
3381 t = TREE_TYPE (t);
3383 ppd.parameter_packs = &parameter_packs;
3384 ppd.visited = pointer_set_create ();
3385 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3386 pointer_set_destroy (ppd.visited);
3388 if (parameter_packs)
3390 error ("parameter packs not expanded with %<...%>:");
3391 while (parameter_packs)
3393 tree pack = TREE_VALUE (parameter_packs);
3394 tree name = NULL_TREE;
3396 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3397 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3398 name = TYPE_NAME (pack);
3399 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3400 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3401 else
3402 name = DECL_NAME (pack);
3404 if (name)
3405 inform (input_location, " %qD", name);
3406 else
3407 inform (input_location, " <anonymous>");
3409 parameter_packs = TREE_CHAIN (parameter_packs);
3412 return true;
3415 return false;
3418 /* Expand any parameter packs that occur in the template arguments in
3419 ARGS. */
3420 tree
3421 expand_template_argument_pack (tree args)
3423 tree result_args = NULL_TREE;
3424 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3425 int num_result_args = -1;
3426 int non_default_args_count = -1;
3428 /* First, determine if we need to expand anything, and the number of
3429 slots we'll need. */
3430 for (in_arg = 0; in_arg < nargs; ++in_arg)
3432 tree arg = TREE_VEC_ELT (args, in_arg);
3433 if (arg == NULL_TREE)
3434 return args;
3435 if (ARGUMENT_PACK_P (arg))
3437 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3438 if (num_result_args < 0)
3439 num_result_args = in_arg + num_packed;
3440 else
3441 num_result_args += num_packed;
3443 else
3445 if (num_result_args >= 0)
3446 num_result_args++;
3450 /* If no expansion is necessary, we're done. */
3451 if (num_result_args < 0)
3452 return args;
3454 /* Expand arguments. */
3455 result_args = make_tree_vec (num_result_args);
3456 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3457 non_default_args_count =
3458 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3459 for (in_arg = 0; in_arg < nargs; ++in_arg)
3461 tree arg = TREE_VEC_ELT (args, in_arg);
3462 if (ARGUMENT_PACK_P (arg))
3464 tree packed = ARGUMENT_PACK_ARGS (arg);
3465 int i, num_packed = TREE_VEC_LENGTH (packed);
3466 for (i = 0; i < num_packed; ++i, ++out_arg)
3467 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3468 if (non_default_args_count > 0)
3469 non_default_args_count += num_packed - 1;
3471 else
3473 TREE_VEC_ELT (result_args, out_arg) = arg;
3474 ++out_arg;
3477 if (non_default_args_count >= 0)
3478 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3479 return result_args;
3482 /* Checks if DECL shadows a template parameter.
3484 [temp.local]: A template-parameter shall not be redeclared within its
3485 scope (including nested scopes).
3487 Emits an error and returns TRUE if the DECL shadows a parameter,
3488 returns FALSE otherwise. */
3490 bool
3491 check_template_shadow (tree decl)
3493 tree olddecl;
3495 /* If we're not in a template, we can't possibly shadow a template
3496 parameter. */
3497 if (!current_template_parms)
3498 return true;
3500 /* Figure out what we're shadowing. */
3501 if (TREE_CODE (decl) == OVERLOAD)
3502 decl = OVL_CURRENT (decl);
3503 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3505 /* If there's no previous binding for this name, we're not shadowing
3506 anything, let alone a template parameter. */
3507 if (!olddecl)
3508 return true;
3510 /* If we're not shadowing a template parameter, we're done. Note
3511 that OLDDECL might be an OVERLOAD (or perhaps even an
3512 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3513 node. */
3514 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3515 return true;
3517 /* We check for decl != olddecl to avoid bogus errors for using a
3518 name inside a class. We check TPFI to avoid duplicate errors for
3519 inline member templates. */
3520 if (decl == olddecl
3521 || (DECL_TEMPLATE_PARM_P (decl)
3522 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3523 return true;
3525 /* Don't complain about the injected class name, as we've already
3526 complained about the class itself. */
3527 if (DECL_SELF_REFERENCE_P (decl))
3528 return false;
3530 error ("declaration of %q+#D", decl);
3531 error (" shadows template parm %q+#D", olddecl);
3532 return false;
3535 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3536 ORIG_LEVEL, DECL, and TYPE. */
3538 static tree
3539 build_template_parm_index (int index,
3540 int level,
3541 int orig_level,
3542 tree decl,
3543 tree type)
3545 tree t = make_node (TEMPLATE_PARM_INDEX);
3546 TEMPLATE_PARM_IDX (t) = index;
3547 TEMPLATE_PARM_LEVEL (t) = level;
3548 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3549 TEMPLATE_PARM_DECL (t) = decl;
3550 TREE_TYPE (t) = type;
3551 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3552 TREE_READONLY (t) = TREE_READONLY (decl);
3554 return t;
3557 /* Find the canonical type parameter for the given template type
3558 parameter. Returns the canonical type parameter, which may be TYPE
3559 if no such parameter existed. */
3561 static tree
3562 canonical_type_parameter (tree type)
3564 tree list;
3565 int idx = TEMPLATE_TYPE_IDX (type);
3566 if (!canonical_template_parms)
3567 vec_alloc (canonical_template_parms, idx+1);
3569 while (canonical_template_parms->length () <= (unsigned)idx)
3570 vec_safe_push (canonical_template_parms, NULL_TREE);
3572 list = (*canonical_template_parms)[idx];
3573 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3574 list = TREE_CHAIN (list);
3576 if (list)
3577 return TREE_VALUE (list);
3578 else
3580 (*canonical_template_parms)[idx]
3581 = tree_cons (NULL_TREE, type,
3582 (*canonical_template_parms)[idx]);
3583 return type;
3587 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3588 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3589 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3590 new one is created. */
3592 static tree
3593 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3594 tsubst_flags_t complain)
3596 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3597 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3598 != TEMPLATE_PARM_LEVEL (index) - levels)
3599 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3601 tree orig_decl = TEMPLATE_PARM_DECL (index);
3602 tree decl, t;
3604 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3605 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3606 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3607 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3608 DECL_ARTIFICIAL (decl) = 1;
3609 SET_DECL_TEMPLATE_PARM_P (decl);
3611 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3612 TEMPLATE_PARM_LEVEL (index) - levels,
3613 TEMPLATE_PARM_ORIG_LEVEL (index),
3614 decl, type);
3615 TEMPLATE_PARM_DESCENDANTS (index) = t;
3616 TEMPLATE_PARM_PARAMETER_PACK (t)
3617 = TEMPLATE_PARM_PARAMETER_PACK (index);
3619 /* Template template parameters need this. */
3620 if (TREE_CODE (decl) == TEMPLATE_DECL)
3621 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3622 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3623 args, complain);
3626 return TEMPLATE_PARM_DESCENDANTS (index);
3629 /* Process information from new template parameter PARM and append it
3630 to the LIST being built. This new parameter is a non-type
3631 parameter iff IS_NON_TYPE is true. This new parameter is a
3632 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3633 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3634 parameter list PARM belongs to. This is used used to create a
3635 proper canonical type for the type of PARM that is to be created,
3636 iff PARM is a type. If the size is not known, this parameter shall
3637 be set to 0. */
3639 tree
3640 process_template_parm (tree list, location_t parm_loc, tree parm,
3641 bool is_non_type, bool is_parameter_pack)
3643 tree decl = 0;
3644 tree defval;
3645 tree err_parm_list;
3646 int idx = 0;
3648 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3649 defval = TREE_PURPOSE (parm);
3651 if (list)
3653 tree p = tree_last (list);
3655 if (p && TREE_VALUE (p) != error_mark_node)
3657 p = TREE_VALUE (p);
3658 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3659 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3660 else
3661 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3664 ++idx;
3666 else
3667 idx = 0;
3669 if (is_non_type)
3671 parm = TREE_VALUE (parm);
3673 SET_DECL_TEMPLATE_PARM_P (parm);
3675 if (TREE_TYPE (parm) == error_mark_node)
3677 err_parm_list = build_tree_list (defval, parm);
3678 TREE_VALUE (err_parm_list) = error_mark_node;
3679 return chainon (list, err_parm_list);
3681 else
3683 /* [temp.param]
3685 The top-level cv-qualifiers on the template-parameter are
3686 ignored when determining its type. */
3687 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3688 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3690 err_parm_list = build_tree_list (defval, parm);
3691 TREE_VALUE (err_parm_list) = error_mark_node;
3692 return chainon (list, err_parm_list);
3695 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3697 /* This template parameter is not a parameter pack, but it
3698 should be. Complain about "bare" parameter packs. */
3699 check_for_bare_parameter_packs (TREE_TYPE (parm));
3701 /* Recover by calling this a parameter pack. */
3702 is_parameter_pack = true;
3706 /* A template parameter is not modifiable. */
3707 TREE_CONSTANT (parm) = 1;
3708 TREE_READONLY (parm) = 1;
3709 decl = build_decl (parm_loc,
3710 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3711 TREE_CONSTANT (decl) = 1;
3712 TREE_READONLY (decl) = 1;
3713 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3714 = build_template_parm_index (idx, processing_template_decl,
3715 processing_template_decl,
3716 decl, TREE_TYPE (parm));
3718 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3719 = is_parameter_pack;
3721 else
3723 tree t;
3724 parm = TREE_VALUE (TREE_VALUE (parm));
3726 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3728 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3729 /* This is for distinguishing between real templates and template
3730 template parameters */
3731 TREE_TYPE (parm) = t;
3732 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3733 decl = parm;
3735 else
3737 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3738 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3739 decl = build_decl (parm_loc,
3740 TYPE_DECL, parm, t);
3743 TYPE_NAME (t) = decl;
3744 TYPE_STUB_DECL (t) = decl;
3745 parm = decl;
3746 TEMPLATE_TYPE_PARM_INDEX (t)
3747 = build_template_parm_index (idx, processing_template_decl,
3748 processing_template_decl,
3749 decl, TREE_TYPE (parm));
3750 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3751 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3753 DECL_ARTIFICIAL (decl) = 1;
3754 SET_DECL_TEMPLATE_PARM_P (decl);
3755 pushdecl (decl);
3756 parm = build_tree_list (defval, parm);
3757 return chainon (list, parm);
3760 /* The end of a template parameter list has been reached. Process the
3761 tree list into a parameter vector, converting each parameter into a more
3762 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3763 as PARM_DECLs. */
3765 tree
3766 end_template_parm_list (tree parms)
3768 int nparms;
3769 tree parm, next;
3770 tree saved_parmlist = make_tree_vec (list_length (parms));
3772 current_template_parms
3773 = tree_cons (size_int (processing_template_decl),
3774 saved_parmlist, current_template_parms);
3776 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3778 next = TREE_CHAIN (parm);
3779 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3780 TREE_CHAIN (parm) = NULL_TREE;
3783 --processing_template_parmlist;
3785 return saved_parmlist;
3788 /* end_template_decl is called after a template declaration is seen. */
3790 void
3791 end_template_decl (void)
3793 reset_specialization ();
3795 if (! processing_template_decl)
3796 return;
3798 /* This matches the pushlevel in begin_template_parm_list. */
3799 finish_scope ();
3801 --processing_template_decl;
3802 current_template_parms = TREE_CHAIN (current_template_parms);
3805 /* Takes a TREE_LIST representing a template parameter and convert it
3806 into an argument suitable to be passed to the type substitution
3807 functions. Note that If the TREE_LIST contains an error_mark
3808 node, the returned argument is error_mark_node. */
3810 static tree
3811 template_parm_to_arg (tree t)
3814 if (t == NULL_TREE
3815 || TREE_CODE (t) != TREE_LIST)
3816 return t;
3818 if (error_operand_p (TREE_VALUE (t)))
3819 return error_mark_node;
3821 t = TREE_VALUE (t);
3823 if (TREE_CODE (t) == TYPE_DECL
3824 || TREE_CODE (t) == TEMPLATE_DECL)
3826 t = TREE_TYPE (t);
3828 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3830 /* Turn this argument into a TYPE_ARGUMENT_PACK
3831 with a single element, which expands T. */
3832 tree vec = make_tree_vec (1);
3833 #ifdef ENABLE_CHECKING
3834 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3835 (vec, TREE_VEC_LENGTH (vec));
3836 #endif
3837 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3839 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3840 SET_ARGUMENT_PACK_ARGS (t, vec);
3843 else
3845 t = DECL_INITIAL (t);
3847 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3849 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3850 with a single element, which expands T. */
3851 tree vec = make_tree_vec (1);
3852 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3853 #ifdef ENABLE_CHECKING
3854 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3855 (vec, TREE_VEC_LENGTH (vec));
3856 #endif
3857 t = convert_from_reference (t);
3858 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3860 t = make_node (NONTYPE_ARGUMENT_PACK);
3861 SET_ARGUMENT_PACK_ARGS (t, vec);
3862 TREE_TYPE (t) = type;
3865 return t;
3868 /* Given a set of template parameters, return them as a set of template
3869 arguments. The template parameters are represented as a TREE_VEC, in
3870 the form documented in cp-tree.h for template arguments. */
3872 static tree
3873 template_parms_to_args (tree parms)
3875 tree header;
3876 tree args = NULL_TREE;
3877 int length = TMPL_PARMS_DEPTH (parms);
3878 int l = length;
3880 /* If there is only one level of template parameters, we do not
3881 create a TREE_VEC of TREE_VECs. Instead, we return a single
3882 TREE_VEC containing the arguments. */
3883 if (length > 1)
3884 args = make_tree_vec (length);
3886 for (header = parms; header; header = TREE_CHAIN (header))
3888 tree a = copy_node (TREE_VALUE (header));
3889 int i;
3891 TREE_TYPE (a) = NULL_TREE;
3892 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3893 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3895 #ifdef ENABLE_CHECKING
3896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3897 #endif
3899 if (length > 1)
3900 TREE_VEC_ELT (args, --l) = a;
3901 else
3902 args = a;
3905 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3906 /* This can happen for template parms of a template template
3907 parameter, e.g:
3909 template<template<class T, class U> class TT> struct S;
3911 Consider the level of the parms of TT; T and U both have
3912 level 2; TT has no template parm of level 1. So in this case
3913 the first element of full_template_args is NULL_TREE. If we
3914 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3915 of 2. This will make tsubst wrongly consider that T and U
3916 have level 1. Instead, let's create a dummy vector as the
3917 first element of full_template_args so that TMPL_ARGS_DEPTH
3918 returns the correct depth for args. */
3919 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3920 return args;
3923 /* Within the declaration of a template, return the currently active
3924 template parameters as an argument TREE_VEC. */
3926 static tree
3927 current_template_args (void)
3929 return template_parms_to_args (current_template_parms);
3932 /* Update the declared TYPE by doing any lookups which were thought to be
3933 dependent, but are not now that we know the SCOPE of the declarator. */
3935 tree
3936 maybe_update_decl_type (tree orig_type, tree scope)
3938 tree type = orig_type;
3940 if (type == NULL_TREE)
3941 return type;
3943 if (TREE_CODE (orig_type) == TYPE_DECL)
3944 type = TREE_TYPE (type);
3946 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3947 && dependent_type_p (type)
3948 /* Don't bother building up the args in this case. */
3949 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3951 /* tsubst in the args corresponding to the template parameters,
3952 including auto if present. Most things will be unchanged, but
3953 make_typename_type and tsubst_qualified_id will resolve
3954 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3955 tree args = current_template_args ();
3956 tree auto_node = type_uses_auto (type);
3957 tree pushed;
3958 if (auto_node)
3960 tree auto_vec = make_tree_vec (1);
3961 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3962 args = add_to_template_args (args, auto_vec);
3964 pushed = push_scope (scope);
3965 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3966 if (pushed)
3967 pop_scope (scope);
3970 if (type == error_mark_node)
3971 return orig_type;
3973 if (TREE_CODE (orig_type) == TYPE_DECL)
3975 if (same_type_p (type, TREE_TYPE (orig_type)))
3976 type = orig_type;
3977 else
3978 type = TYPE_NAME (type);
3980 return type;
3983 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3984 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3985 a member template. Used by push_template_decl below. */
3987 static tree
3988 build_template_decl (tree decl, tree parms, bool member_template_p)
3990 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3991 DECL_TEMPLATE_PARMS (tmpl) = parms;
3992 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3993 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3994 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3996 return tmpl;
3999 struct template_parm_data
4001 /* The level of the template parameters we are currently
4002 processing. */
4003 int level;
4005 /* The index of the specialization argument we are currently
4006 processing. */
4007 int current_arg;
4009 /* An array whose size is the number of template parameters. The
4010 elements are nonzero if the parameter has been used in any one
4011 of the arguments processed so far. */
4012 int* parms;
4014 /* An array whose size is the number of template arguments. The
4015 elements are nonzero if the argument makes use of template
4016 parameters of this level. */
4017 int* arg_uses_template_parms;
4020 /* Subroutine of push_template_decl used to see if each template
4021 parameter in a partial specialization is used in the explicit
4022 argument list. If T is of the LEVEL given in DATA (which is
4023 treated as a template_parm_data*), then DATA->PARMS is marked
4024 appropriately. */
4026 static int
4027 mark_template_parm (tree t, void* data)
4029 int level;
4030 int idx;
4031 struct template_parm_data* tpd = (struct template_parm_data*) data;
4033 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4035 level = TEMPLATE_PARM_LEVEL (t);
4036 idx = TEMPLATE_PARM_IDX (t);
4038 else
4040 level = TEMPLATE_TYPE_LEVEL (t);
4041 idx = TEMPLATE_TYPE_IDX (t);
4044 if (level == tpd->level)
4046 tpd->parms[idx] = 1;
4047 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4050 /* Return zero so that for_each_template_parm will continue the
4051 traversal of the tree; we want to mark *every* template parm. */
4052 return 0;
4055 /* Process the partial specialization DECL. */
4057 static tree
4058 process_partial_specialization (tree decl)
4060 tree type = TREE_TYPE (decl);
4061 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4062 tree specargs = CLASSTYPE_TI_ARGS (type);
4063 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4064 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4065 tree inner_parms;
4066 tree inst;
4067 int nargs = TREE_VEC_LENGTH (inner_args);
4068 int ntparms;
4069 int i;
4070 bool did_error_intro = false;
4071 struct template_parm_data tpd;
4072 struct template_parm_data tpd2;
4074 gcc_assert (current_template_parms);
4076 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4077 ntparms = TREE_VEC_LENGTH (inner_parms);
4079 /* We check that each of the template parameters given in the
4080 partial specialization is used in the argument list to the
4081 specialization. For example:
4083 template <class T> struct S;
4084 template <class T> struct S<T*>;
4086 The second declaration is OK because `T*' uses the template
4087 parameter T, whereas
4089 template <class T> struct S<int>;
4091 is no good. Even trickier is:
4093 template <class T>
4094 struct S1
4096 template <class U>
4097 struct S2;
4098 template <class U>
4099 struct S2<T>;
4102 The S2<T> declaration is actually invalid; it is a
4103 full-specialization. Of course,
4105 template <class U>
4106 struct S2<T (*)(U)>;
4108 or some such would have been OK. */
4109 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4110 tpd.parms = XALLOCAVEC (int, ntparms);
4111 memset (tpd.parms, 0, sizeof (int) * ntparms);
4113 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4114 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4115 for (i = 0; i < nargs; ++i)
4117 tpd.current_arg = i;
4118 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4119 &mark_template_parm,
4120 &tpd,
4121 NULL,
4122 /*include_nondeduced_p=*/false);
4124 for (i = 0; i < ntparms; ++i)
4125 if (tpd.parms[i] == 0)
4127 /* One of the template parms was not used in the
4128 specialization. */
4129 if (!did_error_intro)
4131 error ("template parameters not used in partial specialization:");
4132 did_error_intro = true;
4135 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4138 if (did_error_intro)
4139 return error_mark_node;
4141 /* [temp.class.spec]
4143 The argument list of the specialization shall not be identical to
4144 the implicit argument list of the primary template. */
4145 if (comp_template_args
4146 (inner_args,
4147 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4148 (maintmpl)))))
4149 error ("partial specialization %qT does not specialize any template arguments", type);
4151 /* A partial specialization that replaces multiple parameters of the
4152 primary template with a pack expansion is less specialized for those
4153 parameters. */
4154 if (nargs < DECL_NTPARMS (maintmpl))
4156 error ("partial specialization is not more specialized than the "
4157 "primary template because it replaces multiple parameters "
4158 "with a pack expansion");
4159 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4160 return decl;
4163 /* [temp.class.spec]
4165 A partially specialized non-type argument expression shall not
4166 involve template parameters of the partial specialization except
4167 when the argument expression is a simple identifier.
4169 The type of a template parameter corresponding to a specialized
4170 non-type argument shall not be dependent on a parameter of the
4171 specialization.
4173 Also, we verify that pack expansions only occur at the
4174 end of the argument list. */
4175 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4176 tpd2.parms = 0;
4177 for (i = 0; i < nargs; ++i)
4179 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4180 tree arg = TREE_VEC_ELT (inner_args, i);
4181 tree packed_args = NULL_TREE;
4182 int j, len = 1;
4184 if (ARGUMENT_PACK_P (arg))
4186 /* Extract the arguments from the argument pack. We'll be
4187 iterating over these in the following loop. */
4188 packed_args = ARGUMENT_PACK_ARGS (arg);
4189 len = TREE_VEC_LENGTH (packed_args);
4192 for (j = 0; j < len; j++)
4194 if (packed_args)
4195 /* Get the Jth argument in the parameter pack. */
4196 arg = TREE_VEC_ELT (packed_args, j);
4198 if (PACK_EXPANSION_P (arg))
4200 /* Pack expansions must come at the end of the
4201 argument list. */
4202 if ((packed_args && j < len - 1)
4203 || (!packed_args && i < nargs - 1))
4205 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4206 error ("parameter pack argument %qE must be at the "
4207 "end of the template argument list", arg);
4208 else
4209 error ("parameter pack argument %qT must be at the "
4210 "end of the template argument list", arg);
4214 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4215 /* We only care about the pattern. */
4216 arg = PACK_EXPANSION_PATTERN (arg);
4218 if (/* These first two lines are the `non-type' bit. */
4219 !TYPE_P (arg)
4220 && TREE_CODE (arg) != TEMPLATE_DECL
4221 /* This next line is the `argument expression is not just a
4222 simple identifier' condition and also the `specialized
4223 non-type argument' bit. */
4224 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4226 if ((!packed_args && tpd.arg_uses_template_parms[i])
4227 || (packed_args && uses_template_parms (arg)))
4228 error ("template argument %qE involves template parameter(s)",
4229 arg);
4230 else
4232 /* Look at the corresponding template parameter,
4233 marking which template parameters its type depends
4234 upon. */
4235 tree type = TREE_TYPE (parm);
4237 if (!tpd2.parms)
4239 /* We haven't yet initialized TPD2. Do so now. */
4240 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4241 /* The number of parameters here is the number in the
4242 main template, which, as checked in the assertion
4243 above, is NARGS. */
4244 tpd2.parms = XALLOCAVEC (int, nargs);
4245 tpd2.level =
4246 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4249 /* Mark the template parameters. But this time, we're
4250 looking for the template parameters of the main
4251 template, not in the specialization. */
4252 tpd2.current_arg = i;
4253 tpd2.arg_uses_template_parms[i] = 0;
4254 memset (tpd2.parms, 0, sizeof (int) * nargs);
4255 for_each_template_parm (type,
4256 &mark_template_parm,
4257 &tpd2,
4258 NULL,
4259 /*include_nondeduced_p=*/false);
4261 if (tpd2.arg_uses_template_parms [i])
4263 /* The type depended on some template parameters.
4264 If they are fully specialized in the
4265 specialization, that's OK. */
4266 int j;
4267 int count = 0;
4268 for (j = 0; j < nargs; ++j)
4269 if (tpd2.parms[j] != 0
4270 && tpd.arg_uses_template_parms [j])
4271 ++count;
4272 if (count != 0)
4273 error_n (input_location, count,
4274 "type %qT of template argument %qE depends "
4275 "on a template parameter",
4276 "type %qT of template argument %qE depends "
4277 "on template parameters",
4278 type,
4279 arg);
4286 /* We should only get here once. */
4287 gcc_assert (!COMPLETE_TYPE_P (type));
4289 tree tmpl = build_template_decl (decl, current_template_parms,
4290 DECL_MEMBER_TEMPLATE_P (maintmpl));
4291 TREE_TYPE (tmpl) = type;
4292 DECL_TEMPLATE_RESULT (tmpl) = decl;
4293 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4294 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4295 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4297 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4298 = tree_cons (specargs, tmpl,
4299 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4300 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4302 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4303 inst = TREE_CHAIN (inst))
4305 tree inst_type = TREE_VALUE (inst);
4306 if (COMPLETE_TYPE_P (inst_type)
4307 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4309 tree spec = most_specialized_class (inst_type, tf_none);
4310 if (spec && TREE_TYPE (spec) == type)
4311 permerror (input_location,
4312 "partial specialization of %qT after instantiation "
4313 "of %qT", type, inst_type);
4317 return decl;
4320 /* Check that a template declaration's use of default arguments and
4321 parameter packs is not invalid. Here, PARMS are the template
4322 parameters. IS_PRIMARY is true if DECL is the thing declared by
4323 a primary template. IS_PARTIAL is true if DECL is a partial
4324 specialization.
4326 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4327 declaration (but not a definition); 1 indicates a declaration, 2
4328 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4329 emitted for extraneous default arguments.
4331 Returns TRUE if there were no errors found, FALSE otherwise. */
4333 bool
4334 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4335 bool is_partial, int is_friend_decl)
4337 const char *msg;
4338 int last_level_to_check;
4339 tree parm_level;
4340 bool no_errors = true;
4342 /* [temp.param]
4344 A default template-argument shall not be specified in a
4345 function template declaration or a function template definition, nor
4346 in the template-parameter-list of the definition of a member of a
4347 class template. */
4349 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4350 /* You can't have a function template declaration in a local
4351 scope, nor you can you define a member of a class template in a
4352 local scope. */
4353 return true;
4355 if (TREE_CODE (decl) == TYPE_DECL
4356 && TREE_TYPE (decl)
4357 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4358 /* A lambda doesn't have an explicit declaration; don't complain
4359 about the parms of the enclosing class. */
4360 return true;
4362 if (current_class_type
4363 && !TYPE_BEING_DEFINED (current_class_type)
4364 && DECL_LANG_SPECIFIC (decl)
4365 && DECL_DECLARES_FUNCTION_P (decl)
4366 /* If this is either a friend defined in the scope of the class
4367 or a member function. */
4368 && (DECL_FUNCTION_MEMBER_P (decl)
4369 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4370 : DECL_FRIEND_CONTEXT (decl)
4371 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4372 : false)
4373 /* And, if it was a member function, it really was defined in
4374 the scope of the class. */
4375 && (!DECL_FUNCTION_MEMBER_P (decl)
4376 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4377 /* We already checked these parameters when the template was
4378 declared, so there's no need to do it again now. This function
4379 was defined in class scope, but we're processing its body now
4380 that the class is complete. */
4381 return true;
4383 /* Core issue 226 (C++0x only): the following only applies to class
4384 templates. */
4385 if (is_primary
4386 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4388 /* [temp.param]
4390 If a template-parameter has a default template-argument, all
4391 subsequent template-parameters shall have a default
4392 template-argument supplied. */
4393 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4395 tree inner_parms = TREE_VALUE (parm_level);
4396 int ntparms = TREE_VEC_LENGTH (inner_parms);
4397 int seen_def_arg_p = 0;
4398 int i;
4400 for (i = 0; i < ntparms; ++i)
4402 tree parm = TREE_VEC_ELT (inner_parms, i);
4404 if (parm == error_mark_node)
4405 continue;
4407 if (TREE_PURPOSE (parm))
4408 seen_def_arg_p = 1;
4409 else if (seen_def_arg_p
4410 && !template_parameter_pack_p (TREE_VALUE (parm)))
4412 error ("no default argument for %qD", TREE_VALUE (parm));
4413 /* For better subsequent error-recovery, we indicate that
4414 there should have been a default argument. */
4415 TREE_PURPOSE (parm) = error_mark_node;
4416 no_errors = false;
4418 else if (!is_partial
4419 && !is_friend_decl
4420 /* Don't complain about an enclosing partial
4421 specialization. */
4422 && parm_level == parms
4423 && TREE_CODE (decl) == TYPE_DECL
4424 && i < ntparms - 1
4425 && template_parameter_pack_p (TREE_VALUE (parm)))
4427 /* A primary class template can only have one
4428 parameter pack, at the end of the template
4429 parameter list. */
4431 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4432 error ("parameter pack %qE must be at the end of the"
4433 " template parameter list", TREE_VALUE (parm));
4434 else
4435 error ("parameter pack %qT must be at the end of the"
4436 " template parameter list",
4437 TREE_TYPE (TREE_VALUE (parm)));
4439 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4440 = error_mark_node;
4441 no_errors = false;
4447 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4448 || is_partial
4449 || !is_primary
4450 || is_friend_decl)
4451 /* For an ordinary class template, default template arguments are
4452 allowed at the innermost level, e.g.:
4453 template <class T = int>
4454 struct S {};
4455 but, in a partial specialization, they're not allowed even
4456 there, as we have in [temp.class.spec]:
4458 The template parameter list of a specialization shall not
4459 contain default template argument values.
4461 So, for a partial specialization, or for a function template
4462 (in C++98/C++03), we look at all of them. */
4464 else
4465 /* But, for a primary class template that is not a partial
4466 specialization we look at all template parameters except the
4467 innermost ones. */
4468 parms = TREE_CHAIN (parms);
4470 /* Figure out what error message to issue. */
4471 if (is_friend_decl == 2)
4472 msg = G_("default template arguments may not be used in function template "
4473 "friend re-declaration");
4474 else if (is_friend_decl)
4475 msg = G_("default template arguments may not be used in function template "
4476 "friend declarations");
4477 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4478 msg = G_("default template arguments may not be used in function templates "
4479 "without -std=c++11 or -std=gnu++11");
4480 else if (is_partial)
4481 msg = G_("default template arguments may not be used in "
4482 "partial specializations");
4483 else
4484 msg = G_("default argument for template parameter for class enclosing %qD");
4486 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4487 /* If we're inside a class definition, there's no need to
4488 examine the parameters to the class itself. On the one
4489 hand, they will be checked when the class is defined, and,
4490 on the other, default arguments are valid in things like:
4491 template <class T = double>
4492 struct S { template <class U> void f(U); };
4493 Here the default argument for `S' has no bearing on the
4494 declaration of `f'. */
4495 last_level_to_check = template_class_depth (current_class_type) + 1;
4496 else
4497 /* Check everything. */
4498 last_level_to_check = 0;
4500 for (parm_level = parms;
4501 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4502 parm_level = TREE_CHAIN (parm_level))
4504 tree inner_parms = TREE_VALUE (parm_level);
4505 int i;
4506 int ntparms;
4508 ntparms = TREE_VEC_LENGTH (inner_parms);
4509 for (i = 0; i < ntparms; ++i)
4511 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4512 continue;
4514 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4516 if (msg)
4518 no_errors = false;
4519 if (is_friend_decl == 2)
4520 return no_errors;
4522 error (msg, decl);
4523 msg = 0;
4526 /* Clear out the default argument so that we are not
4527 confused later. */
4528 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4532 /* At this point, if we're still interested in issuing messages,
4533 they must apply to classes surrounding the object declared. */
4534 if (msg)
4535 msg = G_("default argument for template parameter for class "
4536 "enclosing %qD");
4539 return no_errors;
4542 /* Worker for push_template_decl_real, called via
4543 for_each_template_parm. DATA is really an int, indicating the
4544 level of the parameters we are interested in. If T is a template
4545 parameter of that level, return nonzero. */
4547 static int
4548 template_parm_this_level_p (tree t, void* data)
4550 int this_level = *(int *)data;
4551 int level;
4553 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4554 level = TEMPLATE_PARM_LEVEL (t);
4555 else
4556 level = TEMPLATE_TYPE_LEVEL (t);
4557 return level == this_level;
4560 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4561 parameters given by current_template_args, or reuses a
4562 previously existing one, if appropriate. Returns the DECL, or an
4563 equivalent one, if it is replaced via a call to duplicate_decls.
4565 If IS_FRIEND is true, DECL is a friend declaration. */
4567 tree
4568 push_template_decl_real (tree decl, bool is_friend)
4570 tree tmpl;
4571 tree args;
4572 tree info;
4573 tree ctx;
4574 bool is_primary;
4575 bool is_partial;
4576 int new_template_p = 0;
4577 /* True if the template is a member template, in the sense of
4578 [temp.mem]. */
4579 bool member_template_p = false;
4581 if (decl == error_mark_node || !current_template_parms)
4582 return error_mark_node;
4584 /* See if this is a partial specialization. */
4585 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4586 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4587 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4589 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4590 is_friend = true;
4592 if (is_friend)
4593 /* For a friend, we want the context of the friend function, not
4594 the type of which it is a friend. */
4595 ctx = CP_DECL_CONTEXT (decl);
4596 else if (CP_DECL_CONTEXT (decl)
4597 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4598 /* In the case of a virtual function, we want the class in which
4599 it is defined. */
4600 ctx = CP_DECL_CONTEXT (decl);
4601 else
4602 /* Otherwise, if we're currently defining some class, the DECL
4603 is assumed to be a member of the class. */
4604 ctx = current_scope ();
4606 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4607 ctx = NULL_TREE;
4609 if (!DECL_CONTEXT (decl))
4610 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4612 /* See if this is a primary template. */
4613 if (is_friend && ctx
4614 && uses_template_parms_level (ctx, processing_template_decl))
4615 /* A friend template that specifies a class context, i.e.
4616 template <typename T> friend void A<T>::f();
4617 is not primary. */
4618 is_primary = false;
4619 else
4620 is_primary = template_parm_scope_p ();
4622 if (is_primary)
4624 if (DECL_CLASS_SCOPE_P (decl))
4625 member_template_p = true;
4626 if (TREE_CODE (decl) == TYPE_DECL
4627 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4629 error ("template class without a name");
4630 return error_mark_node;
4632 else if (TREE_CODE (decl) == FUNCTION_DECL)
4634 if (DECL_DESTRUCTOR_P (decl))
4636 /* [temp.mem]
4638 A destructor shall not be a member template. */
4639 error ("destructor %qD declared as member template", decl);
4640 return error_mark_node;
4642 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4643 && (!prototype_p (TREE_TYPE (decl))
4644 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4645 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4646 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4647 == void_list_node)))
4649 /* [basic.stc.dynamic.allocation]
4651 An allocation function can be a function
4652 template. ... Template allocation functions shall
4653 have two or more parameters. */
4654 error ("invalid template declaration of %qD", decl);
4655 return error_mark_node;
4658 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4659 && CLASS_TYPE_P (TREE_TYPE (decl)))
4660 /* OK */;
4661 else if (TREE_CODE (decl) == TYPE_DECL
4662 && TYPE_DECL_ALIAS_P (decl))
4663 /* alias-declaration */
4664 gcc_assert (!DECL_ARTIFICIAL (decl));
4665 else
4667 error ("template declaration of %q#D", decl);
4668 return error_mark_node;
4672 /* Check to see that the rules regarding the use of default
4673 arguments are not being violated. */
4674 check_default_tmpl_args (decl, current_template_parms,
4675 is_primary, is_partial, /*is_friend_decl=*/0);
4677 /* Ensure that there are no parameter packs in the type of this
4678 declaration that have not been expanded. */
4679 if (TREE_CODE (decl) == FUNCTION_DECL)
4681 /* Check each of the arguments individually to see if there are
4682 any bare parameter packs. */
4683 tree type = TREE_TYPE (decl);
4684 tree arg = DECL_ARGUMENTS (decl);
4685 tree argtype = TYPE_ARG_TYPES (type);
4687 while (arg && argtype)
4689 if (!DECL_PACK_P (arg)
4690 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4692 /* This is a PARM_DECL that contains unexpanded parameter
4693 packs. We have already complained about this in the
4694 check_for_bare_parameter_packs call, so just replace
4695 these types with ERROR_MARK_NODE. */
4696 TREE_TYPE (arg) = error_mark_node;
4697 TREE_VALUE (argtype) = error_mark_node;
4700 arg = DECL_CHAIN (arg);
4701 argtype = TREE_CHAIN (argtype);
4704 /* Check for bare parameter packs in the return type and the
4705 exception specifiers. */
4706 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4707 /* Errors were already issued, set return type to int
4708 as the frontend doesn't expect error_mark_node as
4709 the return type. */
4710 TREE_TYPE (type) = integer_type_node;
4711 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4712 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4714 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4715 && TYPE_DECL_ALIAS_P (decl))
4716 ? DECL_ORIGINAL_TYPE (decl)
4717 : TREE_TYPE (decl)))
4719 TREE_TYPE (decl) = error_mark_node;
4720 return error_mark_node;
4723 if (is_partial)
4724 return process_partial_specialization (decl);
4726 args = current_template_args ();
4728 if (!ctx
4729 || TREE_CODE (ctx) == FUNCTION_DECL
4730 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4731 || (TREE_CODE (decl) == TYPE_DECL
4732 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4733 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4735 if (DECL_LANG_SPECIFIC (decl)
4736 && DECL_TEMPLATE_INFO (decl)
4737 && DECL_TI_TEMPLATE (decl))
4738 tmpl = DECL_TI_TEMPLATE (decl);
4739 /* If DECL is a TYPE_DECL for a class-template, then there won't
4740 be DECL_LANG_SPECIFIC. The information equivalent to
4741 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4742 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4743 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4744 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4746 /* Since a template declaration already existed for this
4747 class-type, we must be redeclaring it here. Make sure
4748 that the redeclaration is valid. */
4749 redeclare_class_template (TREE_TYPE (decl),
4750 current_template_parms);
4751 /* We don't need to create a new TEMPLATE_DECL; just use the
4752 one we already had. */
4753 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4755 else
4757 tmpl = build_template_decl (decl, current_template_parms,
4758 member_template_p);
4759 new_template_p = 1;
4761 if (DECL_LANG_SPECIFIC (decl)
4762 && DECL_TEMPLATE_SPECIALIZATION (decl))
4764 /* A specialization of a member template of a template
4765 class. */
4766 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4767 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4768 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4772 else
4774 tree a, t, current, parms;
4775 int i;
4776 tree tinfo = get_template_info (decl);
4778 if (!tinfo)
4780 error ("template definition of non-template %q#D", decl);
4781 return error_mark_node;
4784 tmpl = TI_TEMPLATE (tinfo);
4786 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4787 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4788 && DECL_TEMPLATE_SPECIALIZATION (decl)
4789 && DECL_MEMBER_TEMPLATE_P (tmpl))
4791 tree new_tmpl;
4793 /* The declaration is a specialization of a member
4794 template, declared outside the class. Therefore, the
4795 innermost template arguments will be NULL, so we
4796 replace them with the arguments determined by the
4797 earlier call to check_explicit_specialization. */
4798 args = DECL_TI_ARGS (decl);
4800 new_tmpl
4801 = build_template_decl (decl, current_template_parms,
4802 member_template_p);
4803 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4804 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4805 DECL_TI_TEMPLATE (decl) = new_tmpl;
4806 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4807 DECL_TEMPLATE_INFO (new_tmpl)
4808 = build_template_info (tmpl, args);
4810 register_specialization (new_tmpl,
4811 most_general_template (tmpl),
4812 args,
4813 is_friend, 0);
4814 return decl;
4817 /* Make sure the template headers we got make sense. */
4819 parms = DECL_TEMPLATE_PARMS (tmpl);
4820 i = TMPL_PARMS_DEPTH (parms);
4821 if (TMPL_ARGS_DEPTH (args) != i)
4823 error ("expected %d levels of template parms for %q#D, got %d",
4824 i, decl, TMPL_ARGS_DEPTH (args));
4826 else
4827 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4829 a = TMPL_ARGS_LEVEL (args, i);
4830 t = INNERMOST_TEMPLATE_PARMS (parms);
4832 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4834 if (current == decl)
4835 error ("got %d template parameters for %q#D",
4836 TREE_VEC_LENGTH (a), decl);
4837 else
4838 error ("got %d template parameters for %q#T",
4839 TREE_VEC_LENGTH (a), current);
4840 error (" but %d required", TREE_VEC_LENGTH (t));
4841 /* Avoid crash in import_export_decl. */
4842 DECL_INTERFACE_KNOWN (decl) = 1;
4843 return error_mark_node;
4846 if (current == decl)
4847 current = ctx;
4848 else if (current == NULL_TREE)
4849 /* Can happen in erroneous input. */
4850 break;
4851 else
4852 current = get_containing_scope (current);
4855 /* Check that the parms are used in the appropriate qualifying scopes
4856 in the declarator. */
4857 if (!comp_template_args
4858 (TI_ARGS (tinfo),
4859 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4861 error ("\
4862 template arguments to %qD do not match original template %qD",
4863 decl, DECL_TEMPLATE_RESULT (tmpl));
4864 if (!uses_template_parms (TI_ARGS (tinfo)))
4865 inform (input_location, "use template<> for an explicit specialization");
4866 /* Avoid crash in import_export_decl. */
4867 DECL_INTERFACE_KNOWN (decl) = 1;
4868 return error_mark_node;
4872 DECL_TEMPLATE_RESULT (tmpl) = decl;
4873 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4875 /* Push template declarations for global functions and types. Note
4876 that we do not try to push a global template friend declared in a
4877 template class; such a thing may well depend on the template
4878 parameters of the class. */
4879 if (new_template_p && !ctx
4880 && !(is_friend && template_class_depth (current_class_type) > 0))
4882 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4883 if (tmpl == error_mark_node)
4884 return error_mark_node;
4886 /* Hide template friend classes that haven't been declared yet. */
4887 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4889 DECL_ANTICIPATED (tmpl) = 1;
4890 DECL_FRIEND_P (tmpl) = 1;
4894 if (is_primary)
4896 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4897 int i;
4899 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4900 if (DECL_CONV_FN_P (tmpl))
4902 int depth = TMPL_PARMS_DEPTH (parms);
4904 /* It is a conversion operator. See if the type converted to
4905 depends on innermost template operands. */
4907 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4908 depth))
4909 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4912 /* Give template template parms a DECL_CONTEXT of the template
4913 for which they are a parameter. */
4914 parms = INNERMOST_TEMPLATE_PARMS (parms);
4915 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4917 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4918 if (TREE_CODE (parm) == TEMPLATE_DECL)
4919 DECL_CONTEXT (parm) = tmpl;
4923 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4924 back to its most general template. If TMPL is a specialization,
4925 ARGS may only have the innermost set of arguments. Add the missing
4926 argument levels if necessary. */
4927 if (DECL_TEMPLATE_INFO (tmpl))
4928 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4930 info = build_template_info (tmpl, args);
4932 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4933 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4934 else
4936 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4937 retrofit_lang_decl (decl);
4938 if (DECL_LANG_SPECIFIC (decl))
4939 DECL_TEMPLATE_INFO (decl) = info;
4942 return DECL_TEMPLATE_RESULT (tmpl);
4945 tree
4946 push_template_decl (tree decl)
4948 return push_template_decl_real (decl, false);
4951 /* FN is an inheriting constructor that inherits from the constructor
4952 template INHERITED; turn FN into a constructor template with a matching
4953 template header. */
4955 tree
4956 add_inherited_template_parms (tree fn, tree inherited)
4958 tree inner_parms
4959 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4960 inner_parms = copy_node (inner_parms);
4961 tree parms
4962 = tree_cons (size_int (processing_template_decl + 1),
4963 inner_parms, current_template_parms);
4964 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4965 tree args = template_parms_to_args (parms);
4966 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4967 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4968 DECL_TEMPLATE_RESULT (tmpl) = fn;
4969 DECL_ARTIFICIAL (tmpl) = true;
4970 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4971 return tmpl;
4974 /* Called when a class template TYPE is redeclared with the indicated
4975 template PARMS, e.g.:
4977 template <class T> struct S;
4978 template <class T> struct S {}; */
4980 bool
4981 redeclare_class_template (tree type, tree parms)
4983 tree tmpl;
4984 tree tmpl_parms;
4985 int i;
4987 if (!TYPE_TEMPLATE_INFO (type))
4989 error ("%qT is not a template type", type);
4990 return false;
4993 tmpl = TYPE_TI_TEMPLATE (type);
4994 if (!PRIMARY_TEMPLATE_P (tmpl))
4995 /* The type is nested in some template class. Nothing to worry
4996 about here; there are no new template parameters for the nested
4997 type. */
4998 return true;
5000 if (!parms)
5002 error ("template specifiers not specified in declaration of %qD",
5003 tmpl);
5004 return false;
5007 parms = INNERMOST_TEMPLATE_PARMS (parms);
5008 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5010 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5012 error_n (input_location, TREE_VEC_LENGTH (parms),
5013 "redeclared with %d template parameter",
5014 "redeclared with %d template parameters",
5015 TREE_VEC_LENGTH (parms));
5016 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5017 "previous declaration %q+D used %d template parameter",
5018 "previous declaration %q+D used %d template parameters",
5019 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5020 return false;
5023 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5025 tree tmpl_parm;
5026 tree parm;
5027 tree tmpl_default;
5028 tree parm_default;
5030 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5031 || TREE_VEC_ELT (parms, i) == error_mark_node)
5032 continue;
5034 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5035 if (tmpl_parm == error_mark_node)
5036 return false;
5038 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5039 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5040 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5042 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5043 TEMPLATE_DECL. */
5044 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5045 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5046 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5047 || (TREE_CODE (tmpl_parm) != PARM_DECL
5048 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5049 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5050 || (TREE_CODE (tmpl_parm) == PARM_DECL
5051 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5052 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5054 error ("template parameter %q+#D", tmpl_parm);
5055 error ("redeclared here as %q#D", parm);
5056 return false;
5059 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5061 /* We have in [temp.param]:
5063 A template-parameter may not be given default arguments
5064 by two different declarations in the same scope. */
5065 error_at (input_location, "redefinition of default argument for %q#D", parm);
5066 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5067 "original definition appeared here");
5068 return false;
5071 if (parm_default != NULL_TREE)
5072 /* Update the previous template parameters (which are the ones
5073 that will really count) with the new default value. */
5074 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5075 else if (tmpl_default != NULL_TREE)
5076 /* Update the new parameters, too; they'll be used as the
5077 parameters for any members. */
5078 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5081 return true;
5084 /* Simplify EXPR if it is a non-dependent expression. Returns the
5085 (possibly simplified) expression. */
5087 tree
5088 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5090 if (expr == NULL_TREE)
5091 return NULL_TREE;
5093 /* If we're in a template, but EXPR isn't value dependent, simplify
5094 it. We're supposed to treat:
5096 template <typename T> void f(T[1 + 1]);
5097 template <typename T> void f(T[2]);
5099 as two declarations of the same function, for example. */
5100 if (processing_template_decl
5101 && !instantiation_dependent_expression_p (expr)
5102 && potential_constant_expression (expr))
5104 HOST_WIDE_INT saved_processing_template_decl;
5106 saved_processing_template_decl = processing_template_decl;
5107 processing_template_decl = 0;
5108 expr = tsubst_copy_and_build (expr,
5109 /*args=*/NULL_TREE,
5110 complain,
5111 /*in_decl=*/NULL_TREE,
5112 /*function_p=*/false,
5113 /*integral_constant_expression_p=*/true);
5114 processing_template_decl = saved_processing_template_decl;
5116 return expr;
5119 tree
5120 fold_non_dependent_expr (tree expr)
5122 return fold_non_dependent_expr_sfinae (expr, tf_error);
5125 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5126 template declaration, or a TYPE_DECL for an alias declaration. */
5128 bool
5129 alias_type_or_template_p (tree t)
5131 if (t == NULL_TREE)
5132 return false;
5133 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5134 || (TYPE_P (t)
5135 && TYPE_NAME (t)
5136 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5137 || DECL_ALIAS_TEMPLATE_P (t));
5140 /* Return TRUE iff is a specialization of an alias template. */
5142 bool
5143 alias_template_specialization_p (const_tree t)
5145 if (t == NULL_TREE)
5146 return false;
5148 return (TYPE_P (t)
5149 && TYPE_TEMPLATE_INFO (t)
5150 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5151 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5154 /* Return the number of innermost template parameters in TMPL. */
5156 static int
5157 num_innermost_template_parms (tree tmpl)
5159 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5160 return TREE_VEC_LENGTH (parms);
5163 /* Return either TMPL or another template that it is equivalent to under DR
5164 1286: An alias that just changes the name of a template is equivalent to
5165 the other template. */
5167 static tree
5168 get_underlying_template (tree tmpl)
5170 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5171 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5173 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5174 if (TYPE_TEMPLATE_INFO (result))
5176 tree sub = TYPE_TI_TEMPLATE (result);
5177 if (PRIMARY_TEMPLATE_P (sub)
5178 && (num_innermost_template_parms (tmpl)
5179 == num_innermost_template_parms (sub))
5180 && same_type_p (result, TREE_TYPE (sub)))
5182 /* The alias type is equivalent to the pattern of the
5183 underlying template, so strip the alias. */
5184 tmpl = sub;
5185 continue;
5188 break;
5190 return tmpl;
5193 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5194 must be a function or a pointer-to-function type, as specified
5195 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5196 and check that the resulting function has external linkage. */
5198 static tree
5199 convert_nontype_argument_function (tree type, tree expr)
5201 tree fns = expr;
5202 tree fn, fn_no_ptr;
5203 linkage_kind linkage;
5205 fn = instantiate_type (type, fns, tf_none);
5206 if (fn == error_mark_node)
5207 return error_mark_node;
5209 fn_no_ptr = fn;
5210 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5211 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5212 if (BASELINK_P (fn_no_ptr))
5213 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5215 /* [temp.arg.nontype]/1
5217 A template-argument for a non-type, non-template template-parameter
5218 shall be one of:
5219 [...]
5220 -- the address of an object or function with external [C++11: or
5221 internal] linkage. */
5223 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5225 error ("%qE is not a valid template argument for type %qT", expr, type);
5226 if (TYPE_PTR_P (type))
5227 error ("it must be the address of a function with external linkage");
5228 else
5229 error ("it must be the name of a function with external linkage");
5230 return NULL_TREE;
5233 linkage = decl_linkage (fn_no_ptr);
5234 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5236 if (cxx_dialect >= cxx11)
5237 error ("%qE is not a valid template argument for type %qT "
5238 "because %qD has no linkage",
5239 expr, type, fn_no_ptr);
5240 else
5241 error ("%qE is not a valid template argument for type %qT "
5242 "because %qD does not have external linkage",
5243 expr, type, fn_no_ptr);
5244 return NULL_TREE;
5247 return fn;
5250 /* Subroutine of convert_nontype_argument.
5251 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5252 Emit an error otherwise. */
5254 static bool
5255 check_valid_ptrmem_cst_expr (tree type, tree expr,
5256 tsubst_flags_t complain)
5258 STRIP_NOPS (expr);
5259 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5260 return true;
5261 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5262 return true;
5263 if (complain & tf_error)
5265 error ("%qE is not a valid template argument for type %qT",
5266 expr, type);
5267 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5269 return false;
5272 /* Returns TRUE iff the address of OP is value-dependent.
5274 14.6.2.4 [temp.dep.temp]:
5275 A non-integral non-type template-argument is dependent if its type is
5276 dependent or it has either of the following forms
5277 qualified-id
5278 & qualified-id
5279 and contains a nested-name-specifier which specifies a class-name that
5280 names a dependent type.
5282 We generalize this to just say that the address of a member of a
5283 dependent class is value-dependent; the above doesn't cover the
5284 address of a static data member named with an unqualified-id. */
5286 static bool
5287 has_value_dependent_address (tree op)
5289 /* We could use get_inner_reference here, but there's no need;
5290 this is only relevant for template non-type arguments, which
5291 can only be expressed as &id-expression. */
5292 if (DECL_P (op))
5294 tree ctx = CP_DECL_CONTEXT (op);
5295 if (TYPE_P (ctx) && dependent_type_p (ctx))
5296 return true;
5299 return false;
5302 /* The next set of functions are used for providing helpful explanatory
5303 diagnostics for failed overload resolution. Their messages should be
5304 indented by two spaces for consistency with the messages in
5305 call.c */
5307 static int
5308 unify_success (bool /*explain_p*/)
5310 return 0;
5313 static int
5314 unify_parameter_deduction_failure (bool explain_p, tree parm)
5316 if (explain_p)
5317 inform (input_location,
5318 " couldn't deduce template parameter %qD", parm);
5319 return 1;
5322 static int
5323 unify_invalid (bool /*explain_p*/)
5325 return 1;
5328 static int
5329 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5331 if (explain_p)
5332 inform (input_location,
5333 " types %qT and %qT have incompatible cv-qualifiers",
5334 parm, arg);
5335 return 1;
5338 static int
5339 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5341 if (explain_p)
5342 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5343 return 1;
5346 static int
5347 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5349 if (explain_p)
5350 inform (input_location,
5351 " template parameter %qD is not a parameter pack, but "
5352 "argument %qD is",
5353 parm, arg);
5354 return 1;
5357 static int
5358 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5360 if (explain_p)
5361 inform (input_location,
5362 " template argument %qE does not match "
5363 "pointer-to-member constant %qE",
5364 arg, parm);
5365 return 1;
5368 static int
5369 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5371 if (explain_p)
5372 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5373 return 1;
5376 static int
5377 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5379 if (explain_p)
5380 inform (input_location,
5381 " inconsistent parameter pack deduction with %qT and %qT",
5382 old_arg, new_arg);
5383 return 1;
5386 static int
5387 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5389 if (explain_p)
5391 if (TYPE_P (parm))
5392 inform (input_location,
5393 " deduced conflicting types for parameter %qT (%qT and %qT)",
5394 parm, first, second);
5395 else
5396 inform (input_location,
5397 " deduced conflicting values for non-type parameter "
5398 "%qE (%qE and %qE)", parm, first, second);
5400 return 1;
5403 static int
5404 unify_vla_arg (bool explain_p, tree arg)
5406 if (explain_p)
5407 inform (input_location,
5408 " variable-sized array type %qT is not "
5409 "a valid template argument",
5410 arg);
5411 return 1;
5414 static int
5415 unify_method_type_error (bool explain_p, tree arg)
5417 if (explain_p)
5418 inform (input_location,
5419 " member function type %qT is not a valid template argument",
5420 arg);
5421 return 1;
5424 static int
5425 unify_arity (bool explain_p, int have, int wanted)
5427 if (explain_p)
5428 inform_n (input_location, wanted,
5429 " candidate expects %d argument, %d provided",
5430 " candidate expects %d arguments, %d provided",
5431 wanted, have);
5432 return 1;
5435 static int
5436 unify_too_many_arguments (bool explain_p, int have, int wanted)
5438 return unify_arity (explain_p, have, wanted);
5441 static int
5442 unify_too_few_arguments (bool explain_p, int have, int wanted)
5444 return unify_arity (explain_p, have, wanted);
5447 static int
5448 unify_arg_conversion (bool explain_p, tree to_type,
5449 tree from_type, tree arg)
5451 if (explain_p)
5452 inform (EXPR_LOC_OR_LOC (arg, input_location),
5453 " cannot convert %qE (type %qT) to type %qT",
5454 arg, from_type, to_type);
5455 return 1;
5458 static int
5459 unify_no_common_base (bool explain_p, enum template_base_result r,
5460 tree parm, tree arg)
5462 if (explain_p)
5463 switch (r)
5465 case tbr_ambiguous_baseclass:
5466 inform (input_location, " %qT is an ambiguous base class of %qT",
5467 parm, arg);
5468 break;
5469 default:
5470 inform (input_location, " %qT is not derived from %qT", arg, parm);
5471 break;
5473 return 1;
5476 static int
5477 unify_inconsistent_template_template_parameters (bool explain_p)
5479 if (explain_p)
5480 inform (input_location,
5481 " template parameters of a template template argument are "
5482 "inconsistent with other deduced template arguments");
5483 return 1;
5486 static int
5487 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5489 if (explain_p)
5490 inform (input_location,
5491 " can't deduce a template for %qT from non-template type %qT",
5492 parm, arg);
5493 return 1;
5496 static int
5497 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5499 if (explain_p)
5500 inform (input_location,
5501 " template argument %qE does not match %qD", arg, parm);
5502 return 1;
5505 static int
5506 unify_overload_resolution_failure (bool explain_p, tree arg)
5508 if (explain_p)
5509 inform (input_location,
5510 " could not resolve address from overloaded function %qE",
5511 arg);
5512 return 1;
5515 /* Attempt to convert the non-type template parameter EXPR to the
5516 indicated TYPE. If the conversion is successful, return the
5517 converted value. If the conversion is unsuccessful, return
5518 NULL_TREE if we issued an error message, or error_mark_node if we
5519 did not. We issue error messages for out-and-out bad template
5520 parameters, but not simply because the conversion failed, since we
5521 might be just trying to do argument deduction. Both TYPE and EXPR
5522 must be non-dependent.
5524 The conversion follows the special rules described in
5525 [temp.arg.nontype], and it is much more strict than an implicit
5526 conversion.
5528 This function is called twice for each template argument (see
5529 lookup_template_class for a more accurate description of this
5530 problem). This means that we need to handle expressions which
5531 are not valid in a C++ source, but can be created from the
5532 first call (for instance, casts to perform conversions). These
5533 hacks can go away after we fix the double coercion problem. */
5535 static tree
5536 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5538 tree expr_type;
5540 /* Detect immediately string literals as invalid non-type argument.
5541 This special-case is not needed for correctness (we would easily
5542 catch this later), but only to provide better diagnostic for this
5543 common user mistake. As suggested by DR 100, we do not mention
5544 linkage issues in the diagnostic as this is not the point. */
5545 /* FIXME we're making this OK. */
5546 if (TREE_CODE (expr) == STRING_CST)
5548 if (complain & tf_error)
5549 error ("%qE is not a valid template argument for type %qT "
5550 "because string literals can never be used in this context",
5551 expr, type);
5552 return NULL_TREE;
5555 /* Add the ADDR_EXPR now for the benefit of
5556 value_dependent_expression_p. */
5557 if (TYPE_PTROBV_P (type)
5558 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5560 expr = decay_conversion (expr, complain);
5561 if (expr == error_mark_node)
5562 return error_mark_node;
5565 /* If we are in a template, EXPR may be non-dependent, but still
5566 have a syntactic, rather than semantic, form. For example, EXPR
5567 might be a SCOPE_REF, rather than the VAR_DECL to which the
5568 SCOPE_REF refers. Preserving the qualifying scope is necessary
5569 so that access checking can be performed when the template is
5570 instantiated -- but here we need the resolved form so that we can
5571 convert the argument. */
5572 if (TYPE_REF_OBJ_P (type)
5573 && has_value_dependent_address (expr))
5574 /* If we want the address and it's value-dependent, don't fold. */;
5575 else if (!type_unknown_p (expr))
5576 expr = fold_non_dependent_expr_sfinae (expr, complain);
5577 if (error_operand_p (expr))
5578 return error_mark_node;
5579 expr_type = TREE_TYPE (expr);
5580 if (TREE_CODE (type) == REFERENCE_TYPE)
5581 expr = mark_lvalue_use (expr);
5582 else
5583 expr = mark_rvalue_use (expr);
5585 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5586 to a non-type argument of "nullptr". */
5587 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5588 expr = convert (type, expr);
5590 /* In C++11, integral or enumeration non-type template arguments can be
5591 arbitrary constant expressions. Pointer and pointer to
5592 member arguments can be general constant expressions that evaluate
5593 to a null value, but otherwise still need to be of a specific form. */
5594 if (cxx_dialect >= cxx11)
5596 if (TREE_CODE (expr) == PTRMEM_CST)
5597 /* A PTRMEM_CST is already constant, and a valid template
5598 argument for a parameter of pointer to member type, we just want
5599 to leave it in that form rather than lower it to a
5600 CONSTRUCTOR. */;
5601 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5602 expr = maybe_constant_value (expr);
5603 else if (TYPE_PTR_OR_PTRMEM_P (type))
5605 tree folded = maybe_constant_value (expr);
5606 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5607 : null_member_pointer_value_p (folded))
5608 expr = folded;
5612 /* HACK: Due to double coercion, we can get a
5613 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5614 which is the tree that we built on the first call (see
5615 below when coercing to reference to object or to reference to
5616 function). We just strip everything and get to the arg.
5617 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5618 for examples. */
5619 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5621 tree probe_type, probe = expr;
5622 if (REFERENCE_REF_P (probe))
5623 probe = TREE_OPERAND (probe, 0);
5624 probe_type = TREE_TYPE (probe);
5625 if (TREE_CODE (probe) == NOP_EXPR)
5627 /* ??? Maybe we could use convert_from_reference here, but we
5628 would need to relax its constraints because the NOP_EXPR
5629 could actually change the type to something more cv-qualified,
5630 and this is not folded by convert_from_reference. */
5631 tree addr = TREE_OPERAND (probe, 0);
5632 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5633 && TREE_CODE (addr) == ADDR_EXPR
5634 && TYPE_PTR_P (TREE_TYPE (addr))
5635 && (same_type_ignoring_top_level_qualifiers_p
5636 (TREE_TYPE (probe_type),
5637 TREE_TYPE (TREE_TYPE (addr)))))
5639 expr = TREE_OPERAND (addr, 0);
5640 expr_type = TREE_TYPE (probe_type);
5645 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5646 parameter is a pointer to object, through decay and
5647 qualification conversion. Let's strip everything. */
5648 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5650 tree probe = expr;
5651 STRIP_NOPS (probe);
5652 if (TREE_CODE (probe) == ADDR_EXPR
5653 && TYPE_PTR_P (TREE_TYPE (probe)))
5655 /* Skip the ADDR_EXPR only if it is part of the decay for
5656 an array. Otherwise, it is part of the original argument
5657 in the source code. */
5658 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5659 probe = TREE_OPERAND (probe, 0);
5660 expr = probe;
5661 expr_type = TREE_TYPE (expr);
5665 /* [temp.arg.nontype]/5, bullet 1
5667 For a non-type template-parameter of integral or enumeration type,
5668 integral promotions (_conv.prom_) and integral conversions
5669 (_conv.integral_) are applied. */
5670 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5672 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5673 t = maybe_constant_value (t);
5674 if (t != error_mark_node)
5675 expr = t;
5677 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5678 return error_mark_node;
5680 /* Notice that there are constant expressions like '4 % 0' which
5681 do not fold into integer constants. */
5682 if (TREE_CODE (expr) != INTEGER_CST)
5684 if (complain & tf_error)
5686 int errs = errorcount, warns = warningcount + werrorcount;
5687 if (processing_template_decl
5688 && !require_potential_constant_expression (expr))
5689 return NULL_TREE;
5690 expr = cxx_constant_value (expr);
5691 if (errorcount > errs || warningcount + werrorcount > warns)
5692 inform (EXPR_LOC_OR_LOC (expr, input_location),
5693 "in template argument for type %qT ", type);
5694 if (expr == error_mark_node)
5695 return NULL_TREE;
5696 /* else cxx_constant_value complained but gave us
5697 a real constant, so go ahead. */
5698 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5700 else
5701 return NULL_TREE;
5704 /* Avoid typedef problems. */
5705 if (TREE_TYPE (expr) != type)
5706 expr = fold_convert (type, expr);
5708 /* [temp.arg.nontype]/5, bullet 2
5710 For a non-type template-parameter of type pointer to object,
5711 qualification conversions (_conv.qual_) and the array-to-pointer
5712 conversion (_conv.array_) are applied. */
5713 else if (TYPE_PTROBV_P (type))
5715 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5717 A template-argument for a non-type, non-template template-parameter
5718 shall be one of: [...]
5720 -- the name of a non-type template-parameter;
5721 -- the address of an object or function with external linkage, [...]
5722 expressed as "& id-expression" where the & is optional if the name
5723 refers to a function or array, or if the corresponding
5724 template-parameter is a reference.
5726 Here, we do not care about functions, as they are invalid anyway
5727 for a parameter of type pointer-to-object. */
5729 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5730 /* Non-type template parameters are OK. */
5732 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5733 /* Null pointer values are OK in C++11. */;
5734 else if (TREE_CODE (expr) != ADDR_EXPR
5735 && TREE_CODE (expr_type) != ARRAY_TYPE)
5737 if (VAR_P (expr))
5739 error ("%qD is not a valid template argument "
5740 "because %qD is a variable, not the address of "
5741 "a variable",
5742 expr, expr);
5743 return NULL_TREE;
5745 if (POINTER_TYPE_P (expr_type))
5747 error ("%qE is not a valid template argument for %qT "
5748 "because it is not the address of a variable",
5749 expr, type);
5750 return NULL_TREE;
5752 /* Other values, like integer constants, might be valid
5753 non-type arguments of some other type. */
5754 return error_mark_node;
5756 else
5758 tree decl;
5760 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5761 ? TREE_OPERAND (expr, 0) : expr);
5762 if (!VAR_P (decl))
5764 error ("%qE is not a valid template argument of type %qT "
5765 "because %qE is not a variable",
5766 expr, type, decl);
5767 return NULL_TREE;
5769 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5771 error ("%qE is not a valid template argument of type %qT "
5772 "because %qD does not have external linkage",
5773 expr, type, decl);
5774 return NULL_TREE;
5776 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5778 error ("%qE is not a valid template argument of type %qT "
5779 "because %qD has no linkage",
5780 expr, type, decl);
5781 return NULL_TREE;
5785 expr = decay_conversion (expr, complain);
5786 if (expr == error_mark_node)
5787 return error_mark_node;
5789 expr = perform_qualification_conversions (type, expr);
5790 if (expr == error_mark_node)
5791 return error_mark_node;
5793 /* [temp.arg.nontype]/5, bullet 3
5795 For a non-type template-parameter of type reference to object, no
5796 conversions apply. The type referred to by the reference may be more
5797 cv-qualified than the (otherwise identical) type of the
5798 template-argument. The template-parameter is bound directly to the
5799 template-argument, which must be an lvalue. */
5800 else if (TYPE_REF_OBJ_P (type))
5802 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5803 expr_type))
5804 return error_mark_node;
5806 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5808 error ("%qE is not a valid template argument for type %qT "
5809 "because of conflicts in cv-qualification", expr, type);
5810 return NULL_TREE;
5813 if (!real_lvalue_p (expr))
5815 error ("%qE is not a valid template argument for type %qT "
5816 "because it is not an lvalue", expr, type);
5817 return NULL_TREE;
5820 /* [temp.arg.nontype]/1
5822 A template-argument for a non-type, non-template template-parameter
5823 shall be one of: [...]
5825 -- the address of an object or function with external linkage. */
5826 if (INDIRECT_REF_P (expr)
5827 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5829 expr = TREE_OPERAND (expr, 0);
5830 if (DECL_P (expr))
5832 error ("%q#D is not a valid template argument for type %qT "
5833 "because a reference variable does not have a constant "
5834 "address", expr, type);
5835 return NULL_TREE;
5839 if (!DECL_P (expr))
5841 error ("%qE is not a valid template argument for type %qT "
5842 "because it is not an object with external linkage",
5843 expr, type);
5844 return NULL_TREE;
5847 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5849 error ("%qE is not a valid template argument for type %qT "
5850 "because object %qD has not external linkage",
5851 expr, type, expr);
5852 return NULL_TREE;
5855 expr = build_nop (type, build_address (expr));
5857 /* [temp.arg.nontype]/5, bullet 4
5859 For a non-type template-parameter of type pointer to function, only
5860 the function-to-pointer conversion (_conv.func_) is applied. If the
5861 template-argument represents a set of overloaded functions (or a
5862 pointer to such), the matching function is selected from the set
5863 (_over.over_). */
5864 else if (TYPE_PTRFN_P (type))
5866 /* If the argument is a template-id, we might not have enough
5867 context information to decay the pointer. */
5868 if (!type_unknown_p (expr_type))
5870 expr = decay_conversion (expr, complain);
5871 if (expr == error_mark_node)
5872 return error_mark_node;
5875 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5876 /* Null pointer values are OK in C++11. */
5877 return perform_qualification_conversions (type, expr);
5879 expr = convert_nontype_argument_function (type, expr);
5880 if (!expr || expr == error_mark_node)
5881 return expr;
5883 /* [temp.arg.nontype]/5, bullet 5
5885 For a non-type template-parameter of type reference to function, no
5886 conversions apply. If the template-argument represents a set of
5887 overloaded functions, the matching function is selected from the set
5888 (_over.over_). */
5889 else if (TYPE_REFFN_P (type))
5891 if (TREE_CODE (expr) == ADDR_EXPR)
5893 error ("%qE is not a valid template argument for type %qT "
5894 "because it is a pointer", expr, type);
5895 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5896 return NULL_TREE;
5899 expr = convert_nontype_argument_function (type, expr);
5900 if (!expr || expr == error_mark_node)
5901 return expr;
5903 expr = build_nop (type, build_address (expr));
5905 /* [temp.arg.nontype]/5, bullet 6
5907 For a non-type template-parameter of type pointer to member function,
5908 no conversions apply. If the template-argument represents a set of
5909 overloaded member functions, the matching member function is selected
5910 from the set (_over.over_). */
5911 else if (TYPE_PTRMEMFUNC_P (type))
5913 expr = instantiate_type (type, expr, tf_none);
5914 if (expr == error_mark_node)
5915 return error_mark_node;
5917 /* [temp.arg.nontype] bullet 1 says the pointer to member
5918 expression must be a pointer-to-member constant. */
5919 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5920 return error_mark_node;
5922 /* There is no way to disable standard conversions in
5923 resolve_address_of_overloaded_function (called by
5924 instantiate_type). It is possible that the call succeeded by
5925 converting &B::I to &D::I (where B is a base of D), so we need
5926 to reject this conversion here.
5928 Actually, even if there was a way to disable standard conversions,
5929 it would still be better to reject them here so that we can
5930 provide a superior diagnostic. */
5931 if (!same_type_p (TREE_TYPE (expr), type))
5933 error ("%qE is not a valid template argument for type %qT "
5934 "because it is of type %qT", expr, type,
5935 TREE_TYPE (expr));
5936 /* If we are just one standard conversion off, explain. */
5937 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5938 inform (input_location,
5939 "standard conversions are not allowed in this context");
5940 return NULL_TREE;
5943 /* [temp.arg.nontype]/5, bullet 7
5945 For a non-type template-parameter of type pointer to data member,
5946 qualification conversions (_conv.qual_) are applied. */
5947 else if (TYPE_PTRDATAMEM_P (type))
5949 /* [temp.arg.nontype] bullet 1 says the pointer to member
5950 expression must be a pointer-to-member constant. */
5951 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5952 return error_mark_node;
5954 expr = perform_qualification_conversions (type, expr);
5955 if (expr == error_mark_node)
5956 return expr;
5958 else if (NULLPTR_TYPE_P (type))
5960 if (expr != nullptr_node)
5962 error ("%qE is not a valid template argument for type %qT "
5963 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5964 return NULL_TREE;
5966 return expr;
5968 /* A template non-type parameter must be one of the above. */
5969 else
5970 gcc_unreachable ();
5972 /* Sanity check: did we actually convert the argument to the
5973 right type? */
5974 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5975 (type, TREE_TYPE (expr)));
5976 return expr;
5979 /* Subroutine of coerce_template_template_parms, which returns 1 if
5980 PARM_PARM and ARG_PARM match using the rule for the template
5981 parameters of template template parameters. Both PARM and ARG are
5982 template parameters; the rest of the arguments are the same as for
5983 coerce_template_template_parms.
5985 static int
5986 coerce_template_template_parm (tree parm,
5987 tree arg,
5988 tsubst_flags_t complain,
5989 tree in_decl,
5990 tree outer_args)
5992 if (arg == NULL_TREE || arg == error_mark_node
5993 || parm == NULL_TREE || parm == error_mark_node)
5994 return 0;
5996 if (TREE_CODE (arg) != TREE_CODE (parm))
5997 return 0;
5999 switch (TREE_CODE (parm))
6001 case TEMPLATE_DECL:
6002 /* We encounter instantiations of templates like
6003 template <template <template <class> class> class TT>
6004 class C; */
6006 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6007 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6009 if (!coerce_template_template_parms
6010 (parmparm, argparm, complain, in_decl, outer_args))
6011 return 0;
6013 /* Fall through. */
6015 case TYPE_DECL:
6016 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6017 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6018 /* Argument is a parameter pack but parameter is not. */
6019 return 0;
6020 break;
6022 case PARM_DECL:
6023 /* The tsubst call is used to handle cases such as
6025 template <int> class C {};
6026 template <class T, template <T> class TT> class D {};
6027 D<int, C> d;
6029 i.e. the parameter list of TT depends on earlier parameters. */
6030 if (!uses_template_parms (TREE_TYPE (arg))
6031 && !same_type_p
6032 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6033 TREE_TYPE (arg)))
6034 return 0;
6036 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6037 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6038 /* Argument is a parameter pack but parameter is not. */
6039 return 0;
6041 break;
6043 default:
6044 gcc_unreachable ();
6047 return 1;
6051 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6052 template template parameters. Both PARM_PARMS and ARG_PARMS are
6053 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6054 or PARM_DECL.
6056 Consider the example:
6057 template <class T> class A;
6058 template<template <class U> class TT> class B;
6060 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6061 the parameters to A, and OUTER_ARGS contains A. */
6063 static int
6064 coerce_template_template_parms (tree parm_parms,
6065 tree arg_parms,
6066 tsubst_flags_t complain,
6067 tree in_decl,
6068 tree outer_args)
6070 int nparms, nargs, i;
6071 tree parm, arg;
6072 int variadic_p = 0;
6074 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6075 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6077 nparms = TREE_VEC_LENGTH (parm_parms);
6078 nargs = TREE_VEC_LENGTH (arg_parms);
6080 /* Determine whether we have a parameter pack at the end of the
6081 template template parameter's template parameter list. */
6082 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6084 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6086 if (parm == error_mark_node)
6087 return 0;
6089 switch (TREE_CODE (parm))
6091 case TEMPLATE_DECL:
6092 case TYPE_DECL:
6093 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6094 variadic_p = 1;
6095 break;
6097 case PARM_DECL:
6098 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6099 variadic_p = 1;
6100 break;
6102 default:
6103 gcc_unreachable ();
6107 if (nargs != nparms
6108 && !(variadic_p && nargs >= nparms - 1))
6109 return 0;
6111 /* Check all of the template parameters except the parameter pack at
6112 the end (if any). */
6113 for (i = 0; i < nparms - variadic_p; ++i)
6115 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6116 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6117 continue;
6119 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6120 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6122 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6123 outer_args))
6124 return 0;
6128 if (variadic_p)
6130 /* Check each of the template parameters in the template
6131 argument against the template parameter pack at the end of
6132 the template template parameter. */
6133 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6134 return 0;
6136 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6138 for (; i < nargs; ++i)
6140 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6141 continue;
6143 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6145 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6146 outer_args))
6147 return 0;
6151 return 1;
6154 /* Verifies that the deduced template arguments (in TARGS) for the
6155 template template parameters (in TPARMS) represent valid bindings,
6156 by comparing the template parameter list of each template argument
6157 to the template parameter list of its corresponding template
6158 template parameter, in accordance with DR150. This
6159 routine can only be called after all template arguments have been
6160 deduced. It will return TRUE if all of the template template
6161 parameter bindings are okay, FALSE otherwise. */
6162 bool
6163 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6165 int i, ntparms = TREE_VEC_LENGTH (tparms);
6166 bool ret = true;
6168 /* We're dealing with template parms in this process. */
6169 ++processing_template_decl;
6171 targs = INNERMOST_TEMPLATE_ARGS (targs);
6173 for (i = 0; i < ntparms; ++i)
6175 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6176 tree targ = TREE_VEC_ELT (targs, i);
6178 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6180 tree packed_args = NULL_TREE;
6181 int idx, len = 1;
6183 if (ARGUMENT_PACK_P (targ))
6185 /* Look inside the argument pack. */
6186 packed_args = ARGUMENT_PACK_ARGS (targ);
6187 len = TREE_VEC_LENGTH (packed_args);
6190 for (idx = 0; idx < len; ++idx)
6192 tree targ_parms = NULL_TREE;
6194 if (packed_args)
6195 /* Extract the next argument from the argument
6196 pack. */
6197 targ = TREE_VEC_ELT (packed_args, idx);
6199 if (PACK_EXPANSION_P (targ))
6200 /* Look at the pattern of the pack expansion. */
6201 targ = PACK_EXPANSION_PATTERN (targ);
6203 /* Extract the template parameters from the template
6204 argument. */
6205 if (TREE_CODE (targ) == TEMPLATE_DECL)
6206 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6207 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6208 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6210 /* Verify that we can coerce the template template
6211 parameters from the template argument to the template
6212 parameter. This requires an exact match. */
6213 if (targ_parms
6214 && !coerce_template_template_parms
6215 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6216 targ_parms,
6217 tf_none,
6218 tparm,
6219 targs))
6221 ret = false;
6222 goto out;
6228 out:
6230 --processing_template_decl;
6231 return ret;
6234 /* Since type attributes aren't mangled, we need to strip them from
6235 template type arguments. */
6237 static tree
6238 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6240 tree mv;
6241 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6242 return arg;
6243 mv = TYPE_MAIN_VARIANT (arg);
6244 arg = strip_typedefs (arg);
6245 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6246 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6248 if (complain & tf_warning)
6249 warning (0, "ignoring attributes on template argument %qT", arg);
6250 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6251 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6253 return arg;
6256 /* Convert the indicated template ARG as necessary to match the
6257 indicated template PARM. Returns the converted ARG, or
6258 error_mark_node if the conversion was unsuccessful. Error and
6259 warning messages are issued under control of COMPLAIN. This
6260 conversion is for the Ith parameter in the parameter list. ARGS is
6261 the full set of template arguments deduced so far. */
6263 static tree
6264 convert_template_argument (tree parm,
6265 tree arg,
6266 tree args,
6267 tsubst_flags_t complain,
6268 int i,
6269 tree in_decl)
6271 tree orig_arg;
6272 tree val;
6273 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6275 if (TREE_CODE (arg) == TREE_LIST
6276 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6278 /* The template argument was the name of some
6279 member function. That's usually
6280 invalid, but static members are OK. In any
6281 case, grab the underlying fields/functions
6282 and issue an error later if required. */
6283 orig_arg = TREE_VALUE (arg);
6284 TREE_TYPE (arg) = unknown_type_node;
6287 orig_arg = arg;
6289 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6290 requires_type = (TREE_CODE (parm) == TYPE_DECL
6291 || requires_tmpl_type);
6293 /* When determining whether an argument pack expansion is a template,
6294 look at the pattern. */
6295 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6296 arg = PACK_EXPANSION_PATTERN (arg);
6298 /* Deal with an injected-class-name used as a template template arg. */
6299 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6301 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6302 if (TREE_CODE (t) == TEMPLATE_DECL)
6304 if (cxx_dialect >= cxx11)
6305 /* OK under DR 1004. */;
6306 else if (complain & tf_warning_or_error)
6307 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6308 " used as template template argument", TYPE_NAME (arg));
6309 else if (flag_pedantic_errors)
6310 t = arg;
6312 arg = t;
6316 is_tmpl_type =
6317 ((TREE_CODE (arg) == TEMPLATE_DECL
6318 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6319 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6320 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6321 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6323 if (is_tmpl_type
6324 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6325 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6326 arg = TYPE_STUB_DECL (arg);
6328 is_type = TYPE_P (arg) || is_tmpl_type;
6330 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6331 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6333 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6335 if (complain & tf_error)
6336 error ("invalid use of destructor %qE as a type", orig_arg);
6337 return error_mark_node;
6340 permerror (input_location,
6341 "to refer to a type member of a template parameter, "
6342 "use %<typename %E%>", orig_arg);
6344 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6345 TREE_OPERAND (arg, 1),
6346 typename_type,
6347 complain);
6348 arg = orig_arg;
6349 is_type = 1;
6351 if (is_type != requires_type)
6353 if (in_decl)
6355 if (complain & tf_error)
6357 error ("type/value mismatch at argument %d in template "
6358 "parameter list for %qD",
6359 i + 1, in_decl);
6360 if (is_type)
6361 error (" expected a constant of type %qT, got %qT",
6362 TREE_TYPE (parm),
6363 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6364 else if (requires_tmpl_type)
6365 error (" expected a class template, got %qE", orig_arg);
6366 else
6367 error (" expected a type, got %qE", orig_arg);
6370 return error_mark_node;
6372 if (is_tmpl_type ^ requires_tmpl_type)
6374 if (in_decl && (complain & tf_error))
6376 error ("type/value mismatch at argument %d in template "
6377 "parameter list for %qD",
6378 i + 1, in_decl);
6379 if (is_tmpl_type)
6380 error (" expected a type, got %qT", DECL_NAME (arg));
6381 else
6382 error (" expected a class template, got %qT", orig_arg);
6384 return error_mark_node;
6387 if (is_type)
6389 if (requires_tmpl_type)
6391 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6392 val = orig_arg;
6393 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6394 /* The number of argument required is not known yet.
6395 Just accept it for now. */
6396 val = TREE_TYPE (arg);
6397 else
6399 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6400 tree argparm;
6402 /* Strip alias templates that are equivalent to another
6403 template. */
6404 arg = get_underlying_template (arg);
6405 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6407 if (coerce_template_template_parms (parmparm, argparm,
6408 complain, in_decl,
6409 args))
6411 val = arg;
6413 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6414 TEMPLATE_DECL. */
6415 if (val != error_mark_node)
6417 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6418 val = TREE_TYPE (val);
6419 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6420 val = make_pack_expansion (val);
6423 else
6425 if (in_decl && (complain & tf_error))
6427 error ("type/value mismatch at argument %d in "
6428 "template parameter list for %qD",
6429 i + 1, in_decl);
6430 error (" expected a template of type %qD, got %qT",
6431 parm, orig_arg);
6434 val = error_mark_node;
6438 else
6439 val = orig_arg;
6440 /* We only form one instance of each template specialization.
6441 Therefore, if we use a non-canonical variant (i.e., a
6442 typedef), any future messages referring to the type will use
6443 the typedef, which is confusing if those future uses do not
6444 themselves also use the typedef. */
6445 if (TYPE_P (val))
6446 val = canonicalize_type_argument (val, complain);
6448 else
6450 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6452 if (invalid_nontype_parm_type_p (t, complain))
6453 return error_mark_node;
6455 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6457 if (same_type_p (t, TREE_TYPE (orig_arg)))
6458 val = orig_arg;
6459 else
6461 /* Not sure if this is reachable, but it doesn't hurt
6462 to be robust. */
6463 error ("type mismatch in nontype parameter pack");
6464 val = error_mark_node;
6467 else if (!dependent_template_arg_p (orig_arg)
6468 && !uses_template_parms (t))
6469 /* We used to call digest_init here. However, digest_init
6470 will report errors, which we don't want when complain
6471 is zero. More importantly, digest_init will try too
6472 hard to convert things: for example, `0' should not be
6473 converted to pointer type at this point according to
6474 the standard. Accepting this is not merely an
6475 extension, since deciding whether or not these
6476 conversions can occur is part of determining which
6477 function template to call, or whether a given explicit
6478 argument specification is valid. */
6479 val = convert_nontype_argument (t, orig_arg, complain);
6480 else
6481 val = strip_typedefs_expr (orig_arg);
6483 if (val == NULL_TREE)
6484 val = error_mark_node;
6485 else if (val == error_mark_node && (complain & tf_error))
6486 error ("could not convert template argument %qE to %qT", orig_arg, t);
6488 if (TREE_CODE (val) == SCOPE_REF)
6490 /* Strip typedefs from the SCOPE_REF. */
6491 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6492 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6493 complain);
6494 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6495 QUALIFIED_NAME_IS_TEMPLATE (val));
6499 return val;
6502 /* Coerces the remaining template arguments in INNER_ARGS (from
6503 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6504 Returns the coerced argument pack. PARM_IDX is the position of this
6505 parameter in the template parameter list. ARGS is the original
6506 template argument list. */
6507 static tree
6508 coerce_template_parameter_pack (tree parms,
6509 int parm_idx,
6510 tree args,
6511 tree inner_args,
6512 int arg_idx,
6513 tree new_args,
6514 int* lost,
6515 tree in_decl,
6516 tsubst_flags_t complain)
6518 tree parm = TREE_VEC_ELT (parms, parm_idx);
6519 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6520 tree packed_args;
6521 tree argument_pack;
6522 tree packed_types = NULL_TREE;
6524 if (arg_idx > nargs)
6525 arg_idx = nargs;
6527 packed_args = make_tree_vec (nargs - arg_idx);
6529 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6530 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6532 /* When the template parameter is a non-type template
6533 parameter pack whose type uses parameter packs, we need
6534 to look at each of the template arguments
6535 separately. Build a vector of the types for these
6536 non-type template parameters in PACKED_TYPES. */
6537 tree expansion
6538 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6539 packed_types = tsubst_pack_expansion (expansion, args,
6540 complain, in_decl);
6542 if (packed_types == error_mark_node)
6543 return error_mark_node;
6545 /* Check that we have the right number of arguments. */
6546 if (arg_idx < nargs
6547 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6548 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6550 int needed_parms
6551 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6552 error ("wrong number of template arguments (%d, should be %d)",
6553 nargs, needed_parms);
6554 return error_mark_node;
6557 /* If we aren't able to check the actual arguments now
6558 (because they haven't been expanded yet), we can at least
6559 verify that all of the types used for the non-type
6560 template parameter pack are, in fact, valid for non-type
6561 template parameters. */
6562 if (arg_idx < nargs
6563 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6565 int j, len = TREE_VEC_LENGTH (packed_types);
6566 for (j = 0; j < len; ++j)
6568 tree t = TREE_VEC_ELT (packed_types, j);
6569 if (invalid_nontype_parm_type_p (t, complain))
6570 return error_mark_node;
6575 /* Convert the remaining arguments, which will be a part of the
6576 parameter pack "parm". */
6577 for (; arg_idx < nargs; ++arg_idx)
6579 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6580 tree actual_parm = TREE_VALUE (parm);
6582 if (packed_types && !PACK_EXPANSION_P (arg))
6584 /* When we have a vector of types (corresponding to the
6585 non-type template parameter pack that uses parameter
6586 packs in its type, as mention above), and the
6587 argument is not an expansion (which expands to a
6588 currently unknown number of arguments), clone the
6589 parm and give it the next type in PACKED_TYPES. */
6590 actual_parm = copy_node (actual_parm);
6591 TREE_TYPE (actual_parm) =
6592 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6595 if (arg == error_mark_node)
6597 if (complain & tf_error)
6598 error ("template argument %d is invalid", arg_idx + 1);
6600 else
6601 arg = convert_template_argument (actual_parm,
6602 arg, new_args, complain, parm_idx,
6603 in_decl);
6604 if (arg == error_mark_node)
6605 (*lost)++;
6606 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6609 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6610 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6611 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6612 else
6614 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6615 TREE_TYPE (argument_pack)
6616 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6617 TREE_CONSTANT (argument_pack) = 1;
6620 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6621 #ifdef ENABLE_CHECKING
6622 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6623 TREE_VEC_LENGTH (packed_args));
6624 #endif
6625 return argument_pack;
6628 /* Returns the number of pack expansions in the template argument vector
6629 ARGS. */
6631 static int
6632 pack_expansion_args_count (tree args)
6634 int i;
6635 int count = 0;
6636 if (args)
6637 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6639 tree elt = TREE_VEC_ELT (args, i);
6640 if (elt && PACK_EXPANSION_P (elt))
6641 ++count;
6643 return count;
6646 /* Convert all template arguments to their appropriate types, and
6647 return a vector containing the innermost resulting template
6648 arguments. If any error occurs, return error_mark_node. Error and
6649 warning messages are issued under control of COMPLAIN.
6651 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6652 for arguments not specified in ARGS. Otherwise, if
6653 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6654 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6655 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6656 ARGS. */
6658 static tree
6659 coerce_template_parms (tree parms,
6660 tree args,
6661 tree in_decl,
6662 tsubst_flags_t complain,
6663 bool require_all_args,
6664 bool use_default_args)
6666 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6667 tree inner_args;
6668 tree new_args;
6669 tree new_inner_args;
6670 int saved_unevaluated_operand;
6671 int saved_inhibit_evaluation_warnings;
6673 /* When used as a boolean value, indicates whether this is a
6674 variadic template parameter list. Since it's an int, we can also
6675 subtract it from nparms to get the number of non-variadic
6676 parameters. */
6677 int variadic_p = 0;
6678 int variadic_args_p = 0;
6679 int post_variadic_parms = 0;
6681 if (args == error_mark_node)
6682 return error_mark_node;
6684 nparms = TREE_VEC_LENGTH (parms);
6686 /* Determine if there are any parameter packs. */
6687 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6689 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6690 if (variadic_p)
6691 ++post_variadic_parms;
6692 if (template_parameter_pack_p (tparm))
6693 ++variadic_p;
6696 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6697 /* If there are no parameters that follow a parameter pack, we need to
6698 expand any argument packs so that we can deduce a parameter pack from
6699 some non-packed args followed by an argument pack, as in variadic85.C.
6700 If there are such parameters, we need to leave argument packs intact
6701 so the arguments are assigned properly. This can happen when dealing
6702 with a nested class inside a partial specialization of a class
6703 template, as in variadic92.C, or when deducing a template parameter pack
6704 from a sub-declarator, as in variadic114.C. */
6705 if (!post_variadic_parms)
6706 inner_args = expand_template_argument_pack (inner_args);
6708 /* Count any pack expansion args. */
6709 variadic_args_p = pack_expansion_args_count (inner_args);
6711 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6712 if ((nargs > nparms && !variadic_p)
6713 || (nargs < nparms - variadic_p
6714 && require_all_args
6715 && !variadic_args_p
6716 && (!use_default_args
6717 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6718 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6720 if (complain & tf_error)
6722 if (variadic_p)
6724 nparms -= variadic_p;
6725 error ("wrong number of template arguments "
6726 "(%d, should be %d or more)", nargs, nparms);
6728 else
6729 error ("wrong number of template arguments "
6730 "(%d, should be %d)", nargs, nparms);
6732 if (in_decl)
6733 error ("provided for %q+D", in_decl);
6736 return error_mark_node;
6738 /* We can't pass a pack expansion to a non-pack parameter of an alias
6739 template (DR 1430). */
6740 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6741 && variadic_args_p
6742 && nargs - variadic_args_p < nparms - variadic_p)
6744 if (complain & tf_error)
6746 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6748 tree arg = TREE_VEC_ELT (inner_args, i);
6749 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6751 if (PACK_EXPANSION_P (arg)
6752 && !template_parameter_pack_p (parm))
6754 error ("pack expansion argument for non-pack parameter "
6755 "%qD of alias template %qD", parm, in_decl);
6756 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6757 goto found;
6760 gcc_unreachable ();
6761 found:;
6763 return error_mark_node;
6766 /* We need to evaluate the template arguments, even though this
6767 template-id may be nested within a "sizeof". */
6768 saved_unevaluated_operand = cp_unevaluated_operand;
6769 cp_unevaluated_operand = 0;
6770 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6771 c_inhibit_evaluation_warnings = 0;
6772 new_inner_args = make_tree_vec (nparms);
6773 new_args = add_outermost_template_args (args, new_inner_args);
6774 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6776 tree arg;
6777 tree parm;
6779 /* Get the Ith template parameter. */
6780 parm = TREE_VEC_ELT (parms, parm_idx);
6782 if (parm == error_mark_node)
6784 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6785 continue;
6788 /* Calculate the next argument. */
6789 if (arg_idx < nargs)
6790 arg = TREE_VEC_ELT (inner_args, arg_idx);
6791 else
6792 arg = NULL_TREE;
6794 if (template_parameter_pack_p (TREE_VALUE (parm))
6795 && !(arg && ARGUMENT_PACK_P (arg)))
6797 /* All remaining arguments will be placed in the
6798 template parameter pack PARM. */
6799 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6800 inner_args, arg_idx,
6801 new_args, &lost,
6802 in_decl, complain);
6804 /* Store this argument. */
6805 if (arg == error_mark_node)
6806 lost++;
6807 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6809 /* We are done with all of the arguments. */
6810 arg_idx = nargs;
6812 continue;
6814 else if (arg)
6816 if (PACK_EXPANSION_P (arg))
6818 /* We don't know how many args we have yet, just
6819 use the unconverted ones for now. */
6820 new_inner_args = inner_args;
6821 break;
6824 else if (require_all_args)
6826 /* There must be a default arg in this case. */
6827 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6828 complain, in_decl);
6829 /* The position of the first default template argument,
6830 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6831 Record that. */
6832 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6833 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6835 else
6836 break;
6838 if (arg == error_mark_node)
6840 if (complain & tf_error)
6841 error ("template argument %d is invalid", arg_idx + 1);
6843 else if (!arg)
6844 /* This only occurs if there was an error in the template
6845 parameter list itself (which we would already have
6846 reported) that we are trying to recover from, e.g., a class
6847 template with a parameter list such as
6848 template<typename..., typename>. */
6849 ++lost;
6850 else
6851 arg = convert_template_argument (TREE_VALUE (parm),
6852 arg, new_args, complain,
6853 parm_idx, in_decl);
6855 if (arg == error_mark_node)
6856 lost++;
6857 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6859 cp_unevaluated_operand = saved_unevaluated_operand;
6860 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6862 if (lost)
6863 return error_mark_node;
6865 #ifdef ENABLE_CHECKING
6866 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6867 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6868 TREE_VEC_LENGTH (new_inner_args));
6869 #endif
6871 return new_inner_args;
6874 /* Like coerce_template_parms. If PARMS represents all template
6875 parameters levels, this function returns a vector of vectors
6876 representing all the resulting argument levels. Note that in this
6877 case, only the innermost arguments are coerced because the
6878 outermost ones are supposed to have been coerced already.
6880 Otherwise, if PARMS represents only (the innermost) vector of
6881 parameters, this function returns a vector containing just the
6882 innermost resulting arguments. */
6884 static tree
6885 coerce_innermost_template_parms (tree parms,
6886 tree args,
6887 tree in_decl,
6888 tsubst_flags_t complain,
6889 bool require_all_args,
6890 bool use_default_args)
6892 int parms_depth = TMPL_PARMS_DEPTH (parms);
6893 int args_depth = TMPL_ARGS_DEPTH (args);
6894 tree coerced_args;
6896 if (parms_depth > 1)
6898 coerced_args = make_tree_vec (parms_depth);
6899 tree level;
6900 int cur_depth;
6902 for (level = parms, cur_depth = parms_depth;
6903 parms_depth > 0 && level != NULL_TREE;
6904 level = TREE_CHAIN (level), --cur_depth)
6906 tree l;
6907 if (cur_depth == args_depth)
6908 l = coerce_template_parms (TREE_VALUE (level),
6909 args, in_decl, complain,
6910 require_all_args,
6911 use_default_args);
6912 else
6913 l = TMPL_ARGS_LEVEL (args, cur_depth);
6915 if (l == error_mark_node)
6916 return error_mark_node;
6918 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6921 else
6922 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6923 args, in_decl, complain,
6924 require_all_args,
6925 use_default_args);
6926 return coerced_args;
6929 /* Returns 1 if template args OT and NT are equivalent. */
6931 static int
6932 template_args_equal (tree ot, tree nt)
6934 if (nt == ot)
6935 return 1;
6936 if (nt == NULL_TREE || ot == NULL_TREE)
6937 return false;
6939 if (TREE_CODE (nt) == TREE_VEC)
6940 /* For member templates */
6941 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6942 else if (PACK_EXPANSION_P (ot))
6943 return (PACK_EXPANSION_P (nt)
6944 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6945 PACK_EXPANSION_PATTERN (nt))
6946 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6947 PACK_EXPANSION_EXTRA_ARGS (nt)));
6948 else if (ARGUMENT_PACK_P (ot))
6950 int i, len;
6951 tree opack, npack;
6953 if (!ARGUMENT_PACK_P (nt))
6954 return 0;
6956 opack = ARGUMENT_PACK_ARGS (ot);
6957 npack = ARGUMENT_PACK_ARGS (nt);
6958 len = TREE_VEC_LENGTH (opack);
6959 if (TREE_VEC_LENGTH (npack) != len)
6960 return 0;
6961 for (i = 0; i < len; ++i)
6962 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6963 TREE_VEC_ELT (npack, i)))
6964 return 0;
6965 return 1;
6967 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6969 /* We get here probably because we are in the middle of substituting
6970 into the pattern of a pack expansion. In that case the
6971 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6972 interested in. So we want to use the initial pack argument for
6973 the comparison. */
6974 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6975 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6976 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6977 return template_args_equal (ot, nt);
6979 else if (TYPE_P (nt))
6980 return TYPE_P (ot) && same_type_p (ot, nt);
6981 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6982 return 0;
6983 else
6984 return cp_tree_equal (ot, nt);
6987 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6988 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6989 NEWARG_PTR with the offending arguments if they are non-NULL. */
6991 static int
6992 comp_template_args_with_info (tree oldargs, tree newargs,
6993 tree *oldarg_ptr, tree *newarg_ptr)
6995 int i;
6997 if (oldargs == newargs)
6998 return 1;
7000 if (!oldargs || !newargs)
7001 return 0;
7003 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7004 return 0;
7006 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7008 tree nt = TREE_VEC_ELT (newargs, i);
7009 tree ot = TREE_VEC_ELT (oldargs, i);
7011 if (! template_args_equal (ot, nt))
7013 if (oldarg_ptr != NULL)
7014 *oldarg_ptr = ot;
7015 if (newarg_ptr != NULL)
7016 *newarg_ptr = nt;
7017 return 0;
7020 return 1;
7023 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7024 of template arguments. Returns 0 otherwise. */
7027 comp_template_args (tree oldargs, tree newargs)
7029 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7032 static void
7033 add_pending_template (tree d)
7035 tree ti = (TYPE_P (d)
7036 ? CLASSTYPE_TEMPLATE_INFO (d)
7037 : DECL_TEMPLATE_INFO (d));
7038 struct pending_template *pt;
7039 int level;
7041 if (TI_PENDING_TEMPLATE_FLAG (ti))
7042 return;
7044 /* We are called both from instantiate_decl, where we've already had a
7045 tinst_level pushed, and instantiate_template, where we haven't.
7046 Compensate. */
7047 level = !current_tinst_level || current_tinst_level->decl != d;
7049 if (level)
7050 push_tinst_level (d);
7052 pt = ggc_alloc_pending_template ();
7053 pt->next = NULL;
7054 pt->tinst = current_tinst_level;
7055 if (last_pending_template)
7056 last_pending_template->next = pt;
7057 else
7058 pending_templates = pt;
7060 last_pending_template = pt;
7062 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7064 if (level)
7065 pop_tinst_level ();
7069 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7070 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7071 documentation for TEMPLATE_ID_EXPR. */
7073 tree
7074 lookup_template_function (tree fns, tree arglist)
7076 tree type;
7078 if (fns == error_mark_node || arglist == error_mark_node)
7079 return error_mark_node;
7081 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7083 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7085 error ("%q#D is not a function template", fns);
7086 return error_mark_node;
7089 if (BASELINK_P (fns))
7091 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7092 unknown_type_node,
7093 BASELINK_FUNCTIONS (fns),
7094 arglist);
7095 return fns;
7098 type = TREE_TYPE (fns);
7099 if (TREE_CODE (fns) == OVERLOAD || !type)
7100 type = unknown_type_node;
7102 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7105 /* Within the scope of a template class S<T>, the name S gets bound
7106 (in build_self_reference) to a TYPE_DECL for the class, not a
7107 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7108 or one of its enclosing classes, and that type is a template,
7109 return the associated TEMPLATE_DECL. Otherwise, the original
7110 DECL is returned.
7112 Also handle the case when DECL is a TREE_LIST of ambiguous
7113 injected-class-names from different bases. */
7115 tree
7116 maybe_get_template_decl_from_type_decl (tree decl)
7118 if (decl == NULL_TREE)
7119 return decl;
7121 /* DR 176: A lookup that finds an injected-class-name (10.2
7122 [class.member.lookup]) can result in an ambiguity in certain cases
7123 (for example, if it is found in more than one base class). If all of
7124 the injected-class-names that are found refer to specializations of
7125 the same class template, and if the name is followed by a
7126 template-argument-list, the reference refers to the class template
7127 itself and not a specialization thereof, and is not ambiguous. */
7128 if (TREE_CODE (decl) == TREE_LIST)
7130 tree t, tmpl = NULL_TREE;
7131 for (t = decl; t; t = TREE_CHAIN (t))
7133 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7134 if (!tmpl)
7135 tmpl = elt;
7136 else if (tmpl != elt)
7137 break;
7139 if (tmpl && t == NULL_TREE)
7140 return tmpl;
7141 else
7142 return decl;
7145 return (decl != NULL_TREE
7146 && DECL_SELF_REFERENCE_P (decl)
7147 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7148 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7151 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7152 parameters, find the desired type.
7154 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7156 IN_DECL, if non-NULL, is the template declaration we are trying to
7157 instantiate.
7159 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7160 the class we are looking up.
7162 Issue error and warning messages under control of COMPLAIN.
7164 If the template class is really a local class in a template
7165 function, then the FUNCTION_CONTEXT is the function in which it is
7166 being instantiated.
7168 ??? Note that this function is currently called *twice* for each
7169 template-id: the first time from the parser, while creating the
7170 incomplete type (finish_template_type), and the second type during the
7171 real instantiation (instantiate_template_class). This is surely something
7172 that we want to avoid. It also causes some problems with argument
7173 coercion (see convert_nontype_argument for more information on this). */
7175 static tree
7176 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7177 int entering_scope, tsubst_flags_t complain)
7179 tree templ = NULL_TREE, parmlist;
7180 tree t;
7181 void **slot;
7182 spec_entry *entry;
7183 spec_entry elt;
7184 hashval_t hash;
7186 if (identifier_p (d1))
7188 tree value = innermost_non_namespace_value (d1);
7189 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7190 templ = value;
7191 else
7193 if (context)
7194 push_decl_namespace (context);
7195 templ = lookup_name (d1);
7196 templ = maybe_get_template_decl_from_type_decl (templ);
7197 if (context)
7198 pop_decl_namespace ();
7200 if (templ)
7201 context = DECL_CONTEXT (templ);
7203 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7205 tree type = TREE_TYPE (d1);
7207 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7208 an implicit typename for the second A. Deal with it. */
7209 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7210 type = TREE_TYPE (type);
7212 if (CLASSTYPE_TEMPLATE_INFO (type))
7214 templ = CLASSTYPE_TI_TEMPLATE (type);
7215 d1 = DECL_NAME (templ);
7218 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7219 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7221 templ = TYPE_TI_TEMPLATE (d1);
7222 d1 = DECL_NAME (templ);
7224 else if (TREE_CODE (d1) == TEMPLATE_DECL
7225 && DECL_TEMPLATE_RESULT (d1)
7226 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7228 templ = d1;
7229 d1 = DECL_NAME (templ);
7230 context = DECL_CONTEXT (templ);
7232 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7234 templ = d1;
7235 d1 = DECL_NAME (templ);
7238 /* Issue an error message if we didn't find a template. */
7239 if (! templ)
7241 if (complain & tf_error)
7242 error ("%qT is not a template", d1);
7243 return error_mark_node;
7246 if (TREE_CODE (templ) != TEMPLATE_DECL
7247 /* Make sure it's a user visible template, if it was named by
7248 the user. */
7249 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7250 && !PRIMARY_TEMPLATE_P (templ)))
7252 if (complain & tf_error)
7254 error ("non-template type %qT used as a template", d1);
7255 if (in_decl)
7256 error ("for template declaration %q+D", in_decl);
7258 return error_mark_node;
7261 complain &= ~tf_user;
7263 /* An alias that just changes the name of a template is equivalent to the
7264 other template, so if any of the arguments are pack expansions, strip
7265 the alias to avoid problems with a pack expansion passed to a non-pack
7266 alias template parameter (DR 1430). */
7267 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7268 templ = get_underlying_template (templ);
7270 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7272 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7273 template arguments */
7275 tree parm;
7276 tree arglist2;
7277 tree outer;
7279 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7281 /* Consider an example where a template template parameter declared as
7283 template <class T, class U = std::allocator<T> > class TT
7285 The template parameter level of T and U are one level larger than
7286 of TT. To proper process the default argument of U, say when an
7287 instantiation `TT<int>' is seen, we need to build the full
7288 arguments containing {int} as the innermost level. Outer levels,
7289 available when not appearing as default template argument, can be
7290 obtained from the arguments of the enclosing template.
7292 Suppose that TT is later substituted with std::vector. The above
7293 instantiation is `TT<int, std::allocator<T> >' with TT at
7294 level 1, and T at level 2, while the template arguments at level 1
7295 becomes {std::vector} and the inner level 2 is {int}. */
7297 outer = DECL_CONTEXT (templ);
7298 if (outer)
7299 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7300 else if (current_template_parms)
7301 /* This is an argument of the current template, so we haven't set
7302 DECL_CONTEXT yet. */
7303 outer = current_template_args ();
7305 if (outer)
7306 arglist = add_to_template_args (outer, arglist);
7308 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7309 complain,
7310 /*require_all_args=*/true,
7311 /*use_default_args=*/true);
7312 if (arglist2 == error_mark_node
7313 || (!uses_template_parms (arglist2)
7314 && check_instantiated_args (templ, arglist2, complain)))
7315 return error_mark_node;
7317 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7318 return parm;
7320 else
7322 tree template_type = TREE_TYPE (templ);
7323 tree gen_tmpl;
7324 tree type_decl;
7325 tree found = NULL_TREE;
7326 int arg_depth;
7327 int parm_depth;
7328 int is_dependent_type;
7329 int use_partial_inst_tmpl = false;
7331 if (template_type == error_mark_node)
7332 /* An error occurred while building the template TEMPL, and a
7333 diagnostic has most certainly been emitted for that
7334 already. Let's propagate that error. */
7335 return error_mark_node;
7337 gen_tmpl = most_general_template (templ);
7338 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7339 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7340 arg_depth = TMPL_ARGS_DEPTH (arglist);
7342 if (arg_depth == 1 && parm_depth > 1)
7344 /* We've been given an incomplete set of template arguments.
7345 For example, given:
7347 template <class T> struct S1 {
7348 template <class U> struct S2 {};
7349 template <class U> struct S2<U*> {};
7352 we will be called with an ARGLIST of `U*', but the
7353 TEMPLATE will be `template <class T> template
7354 <class U> struct S1<T>::S2'. We must fill in the missing
7355 arguments. */
7356 arglist
7357 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7358 arglist);
7359 arg_depth = TMPL_ARGS_DEPTH (arglist);
7362 /* Now we should have enough arguments. */
7363 gcc_assert (parm_depth == arg_depth);
7365 /* From here on, we're only interested in the most general
7366 template. */
7368 /* Calculate the BOUND_ARGS. These will be the args that are
7369 actually tsubst'd into the definition to create the
7370 instantiation. */
7371 if (parm_depth > 1)
7373 /* We have multiple levels of arguments to coerce, at once. */
7374 int i;
7375 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7377 tree bound_args = make_tree_vec (parm_depth);
7379 for (i = saved_depth,
7380 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7381 i > 0 && t != NULL_TREE;
7382 --i, t = TREE_CHAIN (t))
7384 tree a;
7385 if (i == saved_depth)
7386 a = coerce_template_parms (TREE_VALUE (t),
7387 arglist, gen_tmpl,
7388 complain,
7389 /*require_all_args=*/true,
7390 /*use_default_args=*/true);
7391 else
7392 /* Outer levels should have already been coerced. */
7393 a = TMPL_ARGS_LEVEL (arglist, i);
7395 /* Don't process further if one of the levels fails. */
7396 if (a == error_mark_node)
7398 /* Restore the ARGLIST to its full size. */
7399 TREE_VEC_LENGTH (arglist) = saved_depth;
7400 return error_mark_node;
7403 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7405 /* We temporarily reduce the length of the ARGLIST so
7406 that coerce_template_parms will see only the arguments
7407 corresponding to the template parameters it is
7408 examining. */
7409 TREE_VEC_LENGTH (arglist)--;
7412 /* Restore the ARGLIST to its full size. */
7413 TREE_VEC_LENGTH (arglist) = saved_depth;
7415 arglist = bound_args;
7417 else
7418 arglist
7419 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7420 INNERMOST_TEMPLATE_ARGS (arglist),
7421 gen_tmpl,
7422 complain,
7423 /*require_all_args=*/true,
7424 /*use_default_args=*/true);
7426 if (arglist == error_mark_node)
7427 /* We were unable to bind the arguments. */
7428 return error_mark_node;
7430 /* In the scope of a template class, explicit references to the
7431 template class refer to the type of the template, not any
7432 instantiation of it. For example, in:
7434 template <class T> class C { void f(C<T>); }
7436 the `C<T>' is just the same as `C'. Outside of the
7437 class, however, such a reference is an instantiation. */
7438 if ((entering_scope
7439 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7440 || currently_open_class (template_type))
7441 /* comp_template_args is expensive, check it last. */
7442 && comp_template_args (TYPE_TI_ARGS (template_type),
7443 arglist))
7444 return template_type;
7446 /* If we already have this specialization, return it. */
7447 elt.tmpl = gen_tmpl;
7448 elt.args = arglist;
7449 hash = hash_specialization (&elt);
7450 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7451 &elt, hash);
7453 if (entry)
7454 return entry->spec;
7456 is_dependent_type = uses_template_parms (arglist);
7458 /* If the deduced arguments are invalid, then the binding
7459 failed. */
7460 if (!is_dependent_type
7461 && check_instantiated_args (gen_tmpl,
7462 INNERMOST_TEMPLATE_ARGS (arglist),
7463 complain))
7464 return error_mark_node;
7466 if (!is_dependent_type
7467 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7468 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7469 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7471 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7472 DECL_NAME (gen_tmpl),
7473 /*tag_scope=*/ts_global);
7474 return found;
7477 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7478 complain, in_decl);
7479 if (context == error_mark_node)
7480 return error_mark_node;
7482 if (!context)
7483 context = global_namespace;
7485 /* Create the type. */
7486 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7488 /* The user referred to a specialization of an alias
7489 template represented by GEN_TMPL.
7491 [temp.alias]/2 says:
7493 When a template-id refers to the specialization of an
7494 alias template, it is equivalent to the associated
7495 type obtained by substitution of its
7496 template-arguments for the template-parameters in the
7497 type-id of the alias template. */
7499 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7500 /* Note that the call above (by indirectly calling
7501 register_specialization in tsubst_decl) registers the
7502 TYPE_DECL representing the specialization of the alias
7503 template. So next time someone substitutes ARGLIST for
7504 the template parms into the alias template (GEN_TMPL),
7505 she'll get that TYPE_DECL back. */
7507 if (t == error_mark_node)
7508 return t;
7510 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7512 if (!is_dependent_type)
7514 set_current_access_from_decl (TYPE_NAME (template_type));
7515 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7516 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7517 arglist, complain, in_decl),
7518 SCOPED_ENUM_P (template_type), NULL);
7520 if (t == error_mark_node)
7521 return t;
7523 else
7525 /* We don't want to call start_enum for this type, since
7526 the values for the enumeration constants may involve
7527 template parameters. And, no one should be interested
7528 in the enumeration constants for such a type. */
7529 t = cxx_make_type (ENUMERAL_TYPE);
7530 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7532 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7533 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7534 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7536 else if (CLASS_TYPE_P (template_type))
7538 t = make_class_type (TREE_CODE (template_type));
7539 CLASSTYPE_DECLARED_CLASS (t)
7540 = CLASSTYPE_DECLARED_CLASS (template_type);
7541 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7542 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7544 /* A local class. Make sure the decl gets registered properly. */
7545 if (context == current_function_decl)
7546 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7548 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7549 /* This instantiation is another name for the primary
7550 template type. Set the TYPE_CANONICAL field
7551 appropriately. */
7552 TYPE_CANONICAL (t) = template_type;
7553 else if (any_template_arguments_need_structural_equality_p (arglist))
7554 /* Some of the template arguments require structural
7555 equality testing, so this template class requires
7556 structural equality testing. */
7557 SET_TYPE_STRUCTURAL_EQUALITY (t);
7559 else
7560 gcc_unreachable ();
7562 /* If we called start_enum or pushtag above, this information
7563 will already be set up. */
7564 if (!TYPE_NAME (t))
7566 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7568 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7569 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7570 DECL_SOURCE_LOCATION (type_decl)
7571 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7573 else
7574 type_decl = TYPE_NAME (t);
7576 if (CLASS_TYPE_P (template_type))
7578 TREE_PRIVATE (type_decl)
7579 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7580 TREE_PROTECTED (type_decl)
7581 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7582 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7584 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7585 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7589 /* Let's consider the explicit specialization of a member
7590 of a class template specialization that is implicitely instantiated,
7591 e.g.:
7592 template<class T>
7593 struct S
7595 template<class U> struct M {}; //#0
7598 template<>
7599 template<>
7600 struct S<int>::M<char> //#1
7602 int i;
7604 [temp.expl.spec]/4 says this is valid.
7606 In this case, when we write:
7607 S<int>::M<char> m;
7609 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7610 the one of #0.
7612 When we encounter #1, we want to store the partial instantiation
7613 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7615 For all cases other than this "explicit specialization of member of a
7616 class template", we just want to store the most general template into
7617 the CLASSTYPE_TI_TEMPLATE of M.
7619 This case of "explicit specialization of member of a class template"
7620 only happens when:
7621 1/ the enclosing class is an instantiation of, and therefore not
7622 the same as, the context of the most general template, and
7623 2/ we aren't looking at the partial instantiation itself, i.e.
7624 the innermost arguments are not the same as the innermost parms of
7625 the most general template.
7627 So it's only when 1/ and 2/ happens that we want to use the partial
7628 instantiation of the member template in lieu of its most general
7629 template. */
7631 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7632 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7633 /* the enclosing class must be an instantiation... */
7634 && CLASS_TYPE_P (context)
7635 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7637 tree partial_inst_args;
7638 TREE_VEC_LENGTH (arglist)--;
7639 ++processing_template_decl;
7640 partial_inst_args =
7641 tsubst (INNERMOST_TEMPLATE_ARGS
7642 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7643 arglist, complain, NULL_TREE);
7644 --processing_template_decl;
7645 TREE_VEC_LENGTH (arglist)++;
7646 use_partial_inst_tmpl =
7647 /*...and we must not be looking at the partial instantiation
7648 itself. */
7649 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7650 partial_inst_args);
7653 if (!use_partial_inst_tmpl)
7654 /* This case is easy; there are no member templates involved. */
7655 found = gen_tmpl;
7656 else
7658 /* This is a full instantiation of a member template. Find
7659 the partial instantiation of which this is an instance. */
7661 /* Temporarily reduce by one the number of levels in the ARGLIST
7662 so as to avoid comparing the last set of arguments. */
7663 TREE_VEC_LENGTH (arglist)--;
7664 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7665 TREE_VEC_LENGTH (arglist)++;
7666 /* FOUND is either a proper class type, or an alias
7667 template specialization. In the later case, it's a
7668 TYPE_DECL, resulting from the substituting of arguments
7669 for parameters in the TYPE_DECL of the alias template
7670 done earlier. So be careful while getting the template
7671 of FOUND. */
7672 found = TREE_CODE (found) == TYPE_DECL
7673 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7674 : CLASSTYPE_TI_TEMPLATE (found);
7677 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7679 elt.spec = t;
7680 slot = htab_find_slot_with_hash (type_specializations,
7681 &elt, hash, INSERT);
7682 entry = ggc_alloc_spec_entry ();
7683 *entry = elt;
7684 *slot = entry;
7686 /* Note this use of the partial instantiation so we can check it
7687 later in maybe_process_partial_specialization. */
7688 DECL_TEMPLATE_INSTANTIATIONS (templ)
7689 = tree_cons (arglist, t,
7690 DECL_TEMPLATE_INSTANTIATIONS (templ));
7692 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7693 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7694 /* Now that the type has been registered on the instantiations
7695 list, we set up the enumerators. Because the enumeration
7696 constants may involve the enumeration type itself, we make
7697 sure to register the type first, and then create the
7698 constants. That way, doing tsubst_expr for the enumeration
7699 constants won't result in recursive calls here; we'll find
7700 the instantiation and exit above. */
7701 tsubst_enum (template_type, t, arglist);
7703 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7704 /* If the type makes use of template parameters, the
7705 code that generates debugging information will crash. */
7706 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7708 /* Possibly limit visibility based on template args. */
7709 TREE_PUBLIC (type_decl) = 1;
7710 determine_visibility (type_decl);
7712 return t;
7716 /* Wrapper for lookup_template_class_1. */
7718 tree
7719 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7720 int entering_scope, tsubst_flags_t complain)
7722 tree ret;
7723 timevar_push (TV_TEMPLATE_INST);
7724 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7725 entering_scope, complain);
7726 timevar_pop (TV_TEMPLATE_INST);
7727 return ret;
7730 struct pair_fn_data
7732 tree_fn_t fn;
7733 void *data;
7734 /* True when we should also visit template parameters that occur in
7735 non-deduced contexts. */
7736 bool include_nondeduced_p;
7737 struct pointer_set_t *visited;
7740 /* Called from for_each_template_parm via walk_tree. */
7742 static tree
7743 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7745 tree t = *tp;
7746 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7747 tree_fn_t fn = pfd->fn;
7748 void *data = pfd->data;
7750 if (TYPE_P (t)
7751 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7752 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7753 pfd->include_nondeduced_p))
7754 return error_mark_node;
7756 switch (TREE_CODE (t))
7758 case RECORD_TYPE:
7759 if (TYPE_PTRMEMFUNC_P (t))
7760 break;
7761 /* Fall through. */
7763 case UNION_TYPE:
7764 case ENUMERAL_TYPE:
7765 if (!TYPE_TEMPLATE_INFO (t))
7766 *walk_subtrees = 0;
7767 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7768 fn, data, pfd->visited,
7769 pfd->include_nondeduced_p))
7770 return error_mark_node;
7771 break;
7773 case INTEGER_TYPE:
7774 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7775 fn, data, pfd->visited,
7776 pfd->include_nondeduced_p)
7777 || for_each_template_parm (TYPE_MAX_VALUE (t),
7778 fn, data, pfd->visited,
7779 pfd->include_nondeduced_p))
7780 return error_mark_node;
7781 break;
7783 case METHOD_TYPE:
7784 /* Since we're not going to walk subtrees, we have to do this
7785 explicitly here. */
7786 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7787 pfd->visited, pfd->include_nondeduced_p))
7788 return error_mark_node;
7789 /* Fall through. */
7791 case FUNCTION_TYPE:
7792 /* Check the return type. */
7793 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7794 pfd->include_nondeduced_p))
7795 return error_mark_node;
7797 /* Check the parameter types. Since default arguments are not
7798 instantiated until they are needed, the TYPE_ARG_TYPES may
7799 contain expressions that involve template parameters. But,
7800 no-one should be looking at them yet. And, once they're
7801 instantiated, they don't contain template parameters, so
7802 there's no point in looking at them then, either. */
7804 tree parm;
7806 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7807 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7808 pfd->visited, pfd->include_nondeduced_p))
7809 return error_mark_node;
7811 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7812 want walk_tree walking into them itself. */
7813 *walk_subtrees = 0;
7815 break;
7817 case TYPEOF_TYPE:
7818 case UNDERLYING_TYPE:
7819 if (pfd->include_nondeduced_p
7820 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7821 pfd->visited,
7822 pfd->include_nondeduced_p))
7823 return error_mark_node;
7824 break;
7826 case FUNCTION_DECL:
7827 case VAR_DECL:
7828 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7829 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7830 pfd->visited, pfd->include_nondeduced_p))
7831 return error_mark_node;
7832 /* Fall through. */
7834 case PARM_DECL:
7835 case CONST_DECL:
7836 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7837 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7838 pfd->visited, pfd->include_nondeduced_p))
7839 return error_mark_node;
7840 if (DECL_CONTEXT (t)
7841 && pfd->include_nondeduced_p
7842 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7843 pfd->visited, pfd->include_nondeduced_p))
7844 return error_mark_node;
7845 break;
7847 case BOUND_TEMPLATE_TEMPLATE_PARM:
7848 /* Record template parameters such as `T' inside `TT<T>'. */
7849 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7850 pfd->include_nondeduced_p))
7851 return error_mark_node;
7852 /* Fall through. */
7854 case TEMPLATE_TEMPLATE_PARM:
7855 case TEMPLATE_TYPE_PARM:
7856 case TEMPLATE_PARM_INDEX:
7857 if (fn && (*fn)(t, data))
7858 return error_mark_node;
7859 else if (!fn)
7860 return error_mark_node;
7861 break;
7863 case TEMPLATE_DECL:
7864 /* A template template parameter is encountered. */
7865 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7866 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7867 pfd->include_nondeduced_p))
7868 return error_mark_node;
7870 /* Already substituted template template parameter */
7871 *walk_subtrees = 0;
7872 break;
7874 case TYPENAME_TYPE:
7875 if (!fn
7876 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7877 data, pfd->visited,
7878 pfd->include_nondeduced_p))
7879 return error_mark_node;
7880 break;
7882 case CONSTRUCTOR:
7883 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7884 && pfd->include_nondeduced_p
7885 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7886 (TREE_TYPE (t)), fn, data,
7887 pfd->visited, pfd->include_nondeduced_p))
7888 return error_mark_node;
7889 break;
7891 case INDIRECT_REF:
7892 case COMPONENT_REF:
7893 /* If there's no type, then this thing must be some expression
7894 involving template parameters. */
7895 if (!fn && !TREE_TYPE (t))
7896 return error_mark_node;
7897 break;
7899 case MODOP_EXPR:
7900 case CAST_EXPR:
7901 case IMPLICIT_CONV_EXPR:
7902 case REINTERPRET_CAST_EXPR:
7903 case CONST_CAST_EXPR:
7904 case STATIC_CAST_EXPR:
7905 case DYNAMIC_CAST_EXPR:
7906 case ARROW_EXPR:
7907 case DOTSTAR_EXPR:
7908 case TYPEID_EXPR:
7909 case PSEUDO_DTOR_EXPR:
7910 if (!fn)
7911 return error_mark_node;
7912 break;
7914 default:
7915 break;
7918 /* We didn't find any template parameters we liked. */
7919 return NULL_TREE;
7922 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7923 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7924 call FN with the parameter and the DATA.
7925 If FN returns nonzero, the iteration is terminated, and
7926 for_each_template_parm returns 1. Otherwise, the iteration
7927 continues. If FN never returns a nonzero value, the value
7928 returned by for_each_template_parm is 0. If FN is NULL, it is
7929 considered to be the function which always returns 1.
7931 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7932 parameters that occur in non-deduced contexts. When false, only
7933 visits those template parameters that can be deduced. */
7935 static int
7936 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7937 struct pointer_set_t *visited,
7938 bool include_nondeduced_p)
7940 struct pair_fn_data pfd;
7941 int result;
7943 /* Set up. */
7944 pfd.fn = fn;
7945 pfd.data = data;
7946 pfd.include_nondeduced_p = include_nondeduced_p;
7948 /* Walk the tree. (Conceptually, we would like to walk without
7949 duplicates, but for_each_template_parm_r recursively calls
7950 for_each_template_parm, so we would need to reorganize a fair
7951 bit to use walk_tree_without_duplicates, so we keep our own
7952 visited list.) */
7953 if (visited)
7954 pfd.visited = visited;
7955 else
7956 pfd.visited = pointer_set_create ();
7957 result = cp_walk_tree (&t,
7958 for_each_template_parm_r,
7959 &pfd,
7960 pfd.visited) != NULL_TREE;
7962 /* Clean up. */
7963 if (!visited)
7965 pointer_set_destroy (pfd.visited);
7966 pfd.visited = 0;
7969 return result;
7972 /* Returns true if T depends on any template parameter. */
7975 uses_template_parms (tree t)
7977 bool dependent_p;
7978 int saved_processing_template_decl;
7980 saved_processing_template_decl = processing_template_decl;
7981 if (!saved_processing_template_decl)
7982 processing_template_decl = 1;
7983 if (TYPE_P (t))
7984 dependent_p = dependent_type_p (t);
7985 else if (TREE_CODE (t) == TREE_VEC)
7986 dependent_p = any_dependent_template_arguments_p (t);
7987 else if (TREE_CODE (t) == TREE_LIST)
7988 dependent_p = (uses_template_parms (TREE_VALUE (t))
7989 || uses_template_parms (TREE_CHAIN (t)));
7990 else if (TREE_CODE (t) == TYPE_DECL)
7991 dependent_p = dependent_type_p (TREE_TYPE (t));
7992 else if (DECL_P (t)
7993 || EXPR_P (t)
7994 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7995 || TREE_CODE (t) == OVERLOAD
7996 || BASELINK_P (t)
7997 || identifier_p (t)
7998 || TREE_CODE (t) == TRAIT_EXPR
7999 || TREE_CODE (t) == CONSTRUCTOR
8000 || CONSTANT_CLASS_P (t))
8001 dependent_p = (type_dependent_expression_p (t)
8002 || value_dependent_expression_p (t));
8003 else
8005 gcc_assert (t == error_mark_node);
8006 dependent_p = false;
8009 processing_template_decl = saved_processing_template_decl;
8011 return dependent_p;
8014 /* Returns true iff current_function_decl is an incompletely instantiated
8015 template. Useful instead of processing_template_decl because the latter
8016 is set to 0 during fold_non_dependent_expr. */
8018 bool
8019 in_template_function (void)
8021 tree fn = current_function_decl;
8022 bool ret;
8023 ++processing_template_decl;
8024 ret = (fn && DECL_LANG_SPECIFIC (fn)
8025 && DECL_TEMPLATE_INFO (fn)
8026 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8027 --processing_template_decl;
8028 return ret;
8031 /* Returns true if T depends on any template parameter with level LEVEL. */
8034 uses_template_parms_level (tree t, int level)
8036 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8037 /*include_nondeduced_p=*/true);
8040 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8041 ill-formed translation unit, i.e. a variable or function that isn't
8042 usable in a constant expression. */
8044 static inline bool
8045 neglectable_inst_p (tree d)
8047 return (DECL_P (d)
8048 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8049 : decl_maybe_constant_var_p (d)));
8052 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8053 neglectable and instantiated from within an erroneous instantiation. */
8055 static bool
8056 limit_bad_template_recursion (tree decl)
8058 struct tinst_level *lev = current_tinst_level;
8059 int errs = errorcount + sorrycount;
8060 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8061 return false;
8063 for (; lev; lev = lev->next)
8064 if (neglectable_inst_p (lev->decl))
8065 break;
8067 return (lev && errs > lev->errors);
8070 static int tinst_depth;
8071 extern int max_tinst_depth;
8072 int depth_reached;
8074 static GTY(()) struct tinst_level *last_error_tinst_level;
8076 /* We're starting to instantiate D; record the template instantiation context
8077 for diagnostics and to restore it later. */
8080 push_tinst_level (tree d)
8082 struct tinst_level *new_level;
8084 if (tinst_depth >= max_tinst_depth)
8086 last_error_tinst_level = current_tinst_level;
8087 if (TREE_CODE (d) == TREE_LIST)
8088 error ("template instantiation depth exceeds maximum of %d (use "
8089 "-ftemplate-depth= to increase the maximum) substituting %qS",
8090 max_tinst_depth, d);
8091 else
8092 error ("template instantiation depth exceeds maximum of %d (use "
8093 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8094 max_tinst_depth, d);
8096 print_instantiation_context ();
8098 return 0;
8101 /* If the current instantiation caused problems, don't let it instantiate
8102 anything else. Do allow deduction substitution and decls usable in
8103 constant expressions. */
8104 if (limit_bad_template_recursion (d))
8105 return 0;
8107 new_level = ggc_alloc_tinst_level ();
8108 new_level->decl = d;
8109 new_level->locus = input_location;
8110 new_level->errors = errorcount+sorrycount;
8111 new_level->in_system_header_p = in_system_header_at (input_location);
8112 new_level->next = current_tinst_level;
8113 current_tinst_level = new_level;
8115 ++tinst_depth;
8116 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8117 depth_reached = tinst_depth;
8119 return 1;
8122 /* We're done instantiating this template; return to the instantiation
8123 context. */
8125 void
8126 pop_tinst_level (void)
8128 /* Restore the filename and line number stashed away when we started
8129 this instantiation. */
8130 input_location = current_tinst_level->locus;
8131 current_tinst_level = current_tinst_level->next;
8132 --tinst_depth;
8135 /* We're instantiating a deferred template; restore the template
8136 instantiation context in which the instantiation was requested, which
8137 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8139 static tree
8140 reopen_tinst_level (struct tinst_level *level)
8142 struct tinst_level *t;
8144 tinst_depth = 0;
8145 for (t = level; t; t = t->next)
8146 ++tinst_depth;
8148 current_tinst_level = level;
8149 pop_tinst_level ();
8150 if (current_tinst_level)
8151 current_tinst_level->errors = errorcount+sorrycount;
8152 return level->decl;
8155 /* Returns the TINST_LEVEL which gives the original instantiation
8156 context. */
8158 struct tinst_level *
8159 outermost_tinst_level (void)
8161 struct tinst_level *level = current_tinst_level;
8162 if (level)
8163 while (level->next)
8164 level = level->next;
8165 return level;
8168 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8169 vector of template arguments, as for tsubst.
8171 Returns an appropriate tsubst'd friend declaration. */
8173 static tree
8174 tsubst_friend_function (tree decl, tree args)
8176 tree new_friend;
8178 if (TREE_CODE (decl) == FUNCTION_DECL
8179 && DECL_TEMPLATE_INSTANTIATION (decl)
8180 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8181 /* This was a friend declared with an explicit template
8182 argument list, e.g.:
8184 friend void f<>(T);
8186 to indicate that f was a template instantiation, not a new
8187 function declaration. Now, we have to figure out what
8188 instantiation of what template. */
8190 tree template_id, arglist, fns;
8191 tree new_args;
8192 tree tmpl;
8193 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8195 /* Friend functions are looked up in the containing namespace scope.
8196 We must enter that scope, to avoid finding member functions of the
8197 current class with same name. */
8198 push_nested_namespace (ns);
8199 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8200 tf_warning_or_error, NULL_TREE,
8201 /*integral_constant_expression_p=*/false);
8202 pop_nested_namespace (ns);
8203 arglist = tsubst (DECL_TI_ARGS (decl), args,
8204 tf_warning_or_error, NULL_TREE);
8205 template_id = lookup_template_function (fns, arglist);
8207 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8208 tmpl = determine_specialization (template_id, new_friend,
8209 &new_args,
8210 /*need_member_template=*/0,
8211 TREE_VEC_LENGTH (args),
8212 tsk_none);
8213 return instantiate_template (tmpl, new_args, tf_error);
8216 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8218 /* The NEW_FRIEND will look like an instantiation, to the
8219 compiler, but is not an instantiation from the point of view of
8220 the language. For example, we might have had:
8222 template <class T> struct S {
8223 template <class U> friend void f(T, U);
8226 Then, in S<int>, template <class U> void f(int, U) is not an
8227 instantiation of anything. */
8228 if (new_friend == error_mark_node)
8229 return error_mark_node;
8231 DECL_USE_TEMPLATE (new_friend) = 0;
8232 if (TREE_CODE (decl) == TEMPLATE_DECL)
8234 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8235 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8236 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8239 /* The mangled name for the NEW_FRIEND is incorrect. The function
8240 is not a template instantiation and should not be mangled like
8241 one. Therefore, we forget the mangling here; we'll recompute it
8242 later if we need it. */
8243 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8245 SET_DECL_RTL (new_friend, NULL);
8246 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8249 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8251 tree old_decl;
8252 tree new_friend_template_info;
8253 tree new_friend_result_template_info;
8254 tree ns;
8255 int new_friend_is_defn;
8257 /* We must save some information from NEW_FRIEND before calling
8258 duplicate decls since that function will free NEW_FRIEND if
8259 possible. */
8260 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8261 new_friend_is_defn =
8262 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8263 (template_for_substitution (new_friend)))
8264 != NULL_TREE);
8265 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8267 /* This declaration is a `primary' template. */
8268 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8270 new_friend_result_template_info
8271 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8273 else
8274 new_friend_result_template_info = NULL_TREE;
8276 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8277 if (new_friend_is_defn)
8278 DECL_INITIAL (new_friend) = error_mark_node;
8280 /* Inside pushdecl_namespace_level, we will push into the
8281 current namespace. However, the friend function should go
8282 into the namespace of the template. */
8283 ns = decl_namespace_context (new_friend);
8284 push_nested_namespace (ns);
8285 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8286 pop_nested_namespace (ns);
8288 if (old_decl == error_mark_node)
8289 return error_mark_node;
8291 if (old_decl != new_friend)
8293 /* This new friend declaration matched an existing
8294 declaration. For example, given:
8296 template <class T> void f(T);
8297 template <class U> class C {
8298 template <class T> friend void f(T) {}
8301 the friend declaration actually provides the definition
8302 of `f', once C has been instantiated for some type. So,
8303 old_decl will be the out-of-class template declaration,
8304 while new_friend is the in-class definition.
8306 But, if `f' was called before this point, the
8307 instantiation of `f' will have DECL_TI_ARGS corresponding
8308 to `T' but not to `U', references to which might appear
8309 in the definition of `f'. Previously, the most general
8310 template for an instantiation of `f' was the out-of-class
8311 version; now it is the in-class version. Therefore, we
8312 run through all specialization of `f', adding to their
8313 DECL_TI_ARGS appropriately. In particular, they need a
8314 new set of outer arguments, corresponding to the
8315 arguments for this class instantiation.
8317 The same situation can arise with something like this:
8319 friend void f(int);
8320 template <class T> class C {
8321 friend void f(T) {}
8324 when `C<int>' is instantiated. Now, `f(int)' is defined
8325 in the class. */
8327 if (!new_friend_is_defn)
8328 /* On the other hand, if the in-class declaration does
8329 *not* provide a definition, then we don't want to alter
8330 existing definitions. We can just leave everything
8331 alone. */
8333 else
8335 tree new_template = TI_TEMPLATE (new_friend_template_info);
8336 tree new_args = TI_ARGS (new_friend_template_info);
8338 /* Overwrite whatever template info was there before, if
8339 any, with the new template information pertaining to
8340 the declaration. */
8341 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8343 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8345 /* We should have called reregister_specialization in
8346 duplicate_decls. */
8347 gcc_assert (retrieve_specialization (new_template,
8348 new_args, 0)
8349 == old_decl);
8351 /* Instantiate it if the global has already been used. */
8352 if (DECL_ODR_USED (old_decl))
8353 instantiate_decl (old_decl, /*defer_ok=*/true,
8354 /*expl_inst_class_mem_p=*/false);
8356 else
8358 tree t;
8360 /* Indicate that the old function template is a partial
8361 instantiation. */
8362 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8363 = new_friend_result_template_info;
8365 gcc_assert (new_template
8366 == most_general_template (new_template));
8367 gcc_assert (new_template != old_decl);
8369 /* Reassign any specializations already in the hash table
8370 to the new more general template, and add the
8371 additional template args. */
8372 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8373 t != NULL_TREE;
8374 t = TREE_CHAIN (t))
8376 tree spec = TREE_VALUE (t);
8377 spec_entry elt;
8379 elt.tmpl = old_decl;
8380 elt.args = DECL_TI_ARGS (spec);
8381 elt.spec = NULL_TREE;
8383 htab_remove_elt (decl_specializations, &elt);
8385 DECL_TI_ARGS (spec)
8386 = add_outermost_template_args (new_args,
8387 DECL_TI_ARGS (spec));
8389 register_specialization
8390 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8393 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8397 /* The information from NEW_FRIEND has been merged into OLD_DECL
8398 by duplicate_decls. */
8399 new_friend = old_decl;
8402 else
8404 tree context = DECL_CONTEXT (new_friend);
8405 bool dependent_p;
8407 /* In the code
8408 template <class T> class C {
8409 template <class U> friend void C1<U>::f (); // case 1
8410 friend void C2<T>::f (); // case 2
8412 we only need to make sure CONTEXT is a complete type for
8413 case 2. To distinguish between the two cases, we note that
8414 CONTEXT of case 1 remains dependent type after tsubst while
8415 this isn't true for case 2. */
8416 ++processing_template_decl;
8417 dependent_p = dependent_type_p (context);
8418 --processing_template_decl;
8420 if (!dependent_p
8421 && !complete_type_or_else (context, NULL_TREE))
8422 return error_mark_node;
8424 if (COMPLETE_TYPE_P (context))
8426 tree fn = new_friend;
8427 /* do_friend adds the TEMPLATE_DECL for any member friend
8428 template even if it isn't a member template, i.e.
8429 template <class T> friend A<T>::f();
8430 Look through it in that case. */
8431 if (TREE_CODE (fn) == TEMPLATE_DECL
8432 && !PRIMARY_TEMPLATE_P (fn))
8433 fn = DECL_TEMPLATE_RESULT (fn);
8434 /* Check to see that the declaration is really present, and,
8435 possibly obtain an improved declaration. */
8436 fn = check_classfn (context, fn, NULL_TREE);
8438 if (fn)
8439 new_friend = fn;
8443 return new_friend;
8446 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8447 template arguments, as for tsubst.
8449 Returns an appropriate tsubst'd friend type or error_mark_node on
8450 failure. */
8452 static tree
8453 tsubst_friend_class (tree friend_tmpl, tree args)
8455 tree friend_type;
8456 tree tmpl;
8457 tree context;
8459 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8461 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8462 return TREE_TYPE (t);
8465 context = CP_DECL_CONTEXT (friend_tmpl);
8467 if (context != global_namespace)
8469 if (TREE_CODE (context) == NAMESPACE_DECL)
8470 push_nested_namespace (context);
8471 else
8472 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8475 /* Look for a class template declaration. We look for hidden names
8476 because two friend declarations of the same template are the
8477 same. For example, in:
8479 struct A {
8480 template <typename> friend class F;
8482 template <typename> struct B {
8483 template <typename> friend class F;
8486 both F templates are the same. */
8487 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8488 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8490 /* But, if we don't find one, it might be because we're in a
8491 situation like this:
8493 template <class T>
8494 struct S {
8495 template <class U>
8496 friend struct S;
8499 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8500 for `S<int>', not the TEMPLATE_DECL. */
8501 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8503 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8504 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8507 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8509 /* The friend template has already been declared. Just
8510 check to see that the declarations match, and install any new
8511 default parameters. We must tsubst the default parameters,
8512 of course. We only need the innermost template parameters
8513 because that is all that redeclare_class_template will look
8514 at. */
8515 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8516 > TMPL_ARGS_DEPTH (args))
8518 tree parms;
8519 location_t saved_input_location;
8520 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8521 args, tf_warning_or_error);
8523 saved_input_location = input_location;
8524 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8525 redeclare_class_template (TREE_TYPE (tmpl), parms);
8526 input_location = saved_input_location;
8530 friend_type = TREE_TYPE (tmpl);
8532 else
8534 /* The friend template has not already been declared. In this
8535 case, the instantiation of the template class will cause the
8536 injection of this template into the global scope. */
8537 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8538 if (tmpl == error_mark_node)
8539 return error_mark_node;
8541 /* The new TMPL is not an instantiation of anything, so we
8542 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8543 the new type because that is supposed to be the corresponding
8544 template decl, i.e., TMPL. */
8545 DECL_USE_TEMPLATE (tmpl) = 0;
8546 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8547 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8548 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8549 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8551 /* Inject this template into the global scope. */
8552 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8555 if (context != global_namespace)
8557 if (TREE_CODE (context) == NAMESPACE_DECL)
8558 pop_nested_namespace (context);
8559 else
8560 pop_nested_class ();
8563 return friend_type;
8566 /* Returns zero if TYPE cannot be completed later due to circularity.
8567 Otherwise returns one. */
8569 static int
8570 can_complete_type_without_circularity (tree type)
8572 if (type == NULL_TREE || type == error_mark_node)
8573 return 0;
8574 else if (COMPLETE_TYPE_P (type))
8575 return 1;
8576 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8577 return can_complete_type_without_circularity (TREE_TYPE (type));
8578 else if (CLASS_TYPE_P (type)
8579 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8580 return 0;
8581 else
8582 return 1;
8585 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8587 /* Apply any attributes which had to be deferred until instantiation
8588 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8589 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8591 static void
8592 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8593 tree args, tsubst_flags_t complain, tree in_decl)
8595 tree last_dep = NULL_TREE;
8596 tree t;
8597 tree *p;
8599 for (t = attributes; t; t = TREE_CHAIN (t))
8600 if (ATTR_IS_DEPENDENT (t))
8602 last_dep = t;
8603 attributes = copy_list (attributes);
8604 break;
8607 if (DECL_P (*decl_p))
8609 if (TREE_TYPE (*decl_p) == error_mark_node)
8610 return;
8611 p = &DECL_ATTRIBUTES (*decl_p);
8613 else
8614 p = &TYPE_ATTRIBUTES (*decl_p);
8616 if (last_dep)
8618 tree late_attrs = NULL_TREE;
8619 tree *q = &late_attrs;
8621 for (*p = attributes; *p; )
8623 t = *p;
8624 if (ATTR_IS_DEPENDENT (t))
8626 *p = TREE_CHAIN (t);
8627 TREE_CHAIN (t) = NULL_TREE;
8628 if ((flag_openmp || flag_cilkplus)
8629 && is_attribute_p ("omp declare simd",
8630 get_attribute_name (t))
8631 && TREE_VALUE (t))
8633 tree clauses = TREE_VALUE (TREE_VALUE (t));
8634 clauses = tsubst_omp_clauses (clauses, true, args,
8635 complain, in_decl);
8636 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8637 clauses = finish_omp_clauses (clauses);
8638 tree parms = DECL_ARGUMENTS (*decl_p);
8639 clauses
8640 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8641 if (clauses)
8642 TREE_VALUE (TREE_VALUE (t)) = clauses;
8643 else
8644 TREE_VALUE (t) = NULL_TREE;
8646 /* If the first attribute argument is an identifier, don't
8647 pass it through tsubst. Attributes like mode, format,
8648 cleanup and several target specific attributes expect it
8649 unmodified. */
8650 else if (attribute_takes_identifier_p (get_attribute_name (t))
8651 && TREE_VALUE (t))
8653 tree chain
8654 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8655 in_decl,
8656 /*integral_constant_expression_p=*/false);
8657 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8658 TREE_VALUE (t)
8659 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8660 chain);
8662 else
8663 TREE_VALUE (t)
8664 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8665 /*integral_constant_expression_p=*/false);
8666 *q = t;
8667 q = &TREE_CHAIN (t);
8669 else
8670 p = &TREE_CHAIN (t);
8673 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8677 /* Perform (or defer) access check for typedefs that were referenced
8678 from within the template TMPL code.
8679 This is a subroutine of instantiate_decl and instantiate_class_template.
8680 TMPL is the template to consider and TARGS is the list of arguments of
8681 that template. */
8683 static void
8684 perform_typedefs_access_check (tree tmpl, tree targs)
8686 location_t saved_location;
8687 unsigned i;
8688 qualified_typedef_usage_t *iter;
8690 if (!tmpl
8691 || (!CLASS_TYPE_P (tmpl)
8692 && TREE_CODE (tmpl) != FUNCTION_DECL))
8693 return;
8695 saved_location = input_location;
8696 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8698 tree type_decl = iter->typedef_decl;
8699 tree type_scope = iter->context;
8701 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8702 continue;
8704 if (uses_template_parms (type_decl))
8705 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8706 if (uses_template_parms (type_scope))
8707 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8709 /* Make access check error messages point to the location
8710 of the use of the typedef. */
8711 input_location = iter->locus;
8712 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8713 type_decl, type_decl,
8714 tf_warning_or_error);
8716 input_location = saved_location;
8719 static tree
8720 instantiate_class_template_1 (tree type)
8722 tree templ, args, pattern, t, member;
8723 tree typedecl;
8724 tree pbinfo;
8725 tree base_list;
8726 unsigned int saved_maximum_field_alignment;
8727 tree fn_context;
8729 if (type == error_mark_node)
8730 return error_mark_node;
8732 if (COMPLETE_OR_OPEN_TYPE_P (type)
8733 || uses_template_parms (type))
8734 return type;
8736 /* Figure out which template is being instantiated. */
8737 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8738 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8740 /* Determine what specialization of the original template to
8741 instantiate. */
8742 t = most_specialized_class (type, tf_warning_or_error);
8743 if (t == error_mark_node)
8745 TYPE_BEING_DEFINED (type) = 1;
8746 return error_mark_node;
8748 else if (t)
8750 /* This TYPE is actually an instantiation of a partial
8751 specialization. We replace the innermost set of ARGS with
8752 the arguments appropriate for substitution. For example,
8753 given:
8755 template <class T> struct S {};
8756 template <class T> struct S<T*> {};
8758 and supposing that we are instantiating S<int*>, ARGS will
8759 presently be {int*} -- but we need {int}. */
8760 pattern = TREE_TYPE (t);
8761 args = TREE_PURPOSE (t);
8763 else
8765 pattern = TREE_TYPE (templ);
8766 args = CLASSTYPE_TI_ARGS (type);
8769 /* If the template we're instantiating is incomplete, then clearly
8770 there's nothing we can do. */
8771 if (!COMPLETE_TYPE_P (pattern))
8772 return type;
8774 /* If we've recursively instantiated too many templates, stop. */
8775 if (! push_tinst_level (type))
8776 return type;
8778 /* Now we're really doing the instantiation. Mark the type as in
8779 the process of being defined. */
8780 TYPE_BEING_DEFINED (type) = 1;
8782 /* We may be in the middle of deferred access check. Disable
8783 it now. */
8784 push_deferring_access_checks (dk_no_deferred);
8786 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8787 if (!fn_context)
8788 push_to_top_level ();
8789 /* Use #pragma pack from the template context. */
8790 saved_maximum_field_alignment = maximum_field_alignment;
8791 maximum_field_alignment = TYPE_PRECISION (pattern);
8793 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8795 /* Set the input location to the most specialized template definition.
8796 This is needed if tsubsting causes an error. */
8797 typedecl = TYPE_MAIN_DECL (pattern);
8798 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8799 DECL_SOURCE_LOCATION (typedecl);
8801 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8802 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8803 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8804 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8805 if (ANON_AGGR_TYPE_P (pattern))
8806 SET_ANON_AGGR_TYPE_P (type);
8807 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8809 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8810 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8811 /* Adjust visibility for template arguments. */
8812 determine_visibility (TYPE_MAIN_DECL (type));
8814 if (CLASS_TYPE_P (type))
8815 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8817 pbinfo = TYPE_BINFO (pattern);
8819 /* We should never instantiate a nested class before its enclosing
8820 class; we need to look up the nested class by name before we can
8821 instantiate it, and that lookup should instantiate the enclosing
8822 class. */
8823 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8824 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8826 base_list = NULL_TREE;
8827 if (BINFO_N_BASE_BINFOS (pbinfo))
8829 tree pbase_binfo;
8830 tree pushed_scope;
8831 int i;
8833 /* We must enter the scope containing the type, as that is where
8834 the accessibility of types named in dependent bases are
8835 looked up from. */
8836 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8838 /* Substitute into each of the bases to determine the actual
8839 basetypes. */
8840 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8842 tree base;
8843 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8844 tree expanded_bases = NULL_TREE;
8845 int idx, len = 1;
8847 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8849 expanded_bases =
8850 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8851 args, tf_error, NULL_TREE);
8852 if (expanded_bases == error_mark_node)
8853 continue;
8855 len = TREE_VEC_LENGTH (expanded_bases);
8858 for (idx = 0; idx < len; idx++)
8860 if (expanded_bases)
8861 /* Extract the already-expanded base class. */
8862 base = TREE_VEC_ELT (expanded_bases, idx);
8863 else
8864 /* Substitute to figure out the base class. */
8865 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8866 NULL_TREE);
8868 if (base == error_mark_node)
8869 continue;
8871 base_list = tree_cons (access, base, base_list);
8872 if (BINFO_VIRTUAL_P (pbase_binfo))
8873 TREE_TYPE (base_list) = integer_type_node;
8877 /* The list is now in reverse order; correct that. */
8878 base_list = nreverse (base_list);
8880 if (pushed_scope)
8881 pop_scope (pushed_scope);
8883 /* Now call xref_basetypes to set up all the base-class
8884 information. */
8885 xref_basetypes (type, base_list);
8887 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8888 (int) ATTR_FLAG_TYPE_IN_PLACE,
8889 args, tf_error, NULL_TREE);
8890 fixup_attribute_variants (type);
8892 /* Now that our base classes are set up, enter the scope of the
8893 class, so that name lookups into base classes, etc. will work
8894 correctly. This is precisely analogous to what we do in
8895 begin_class_definition when defining an ordinary non-template
8896 class, except we also need to push the enclosing classes. */
8897 push_nested_class (type);
8899 /* Now members are processed in the order of declaration. */
8900 for (member = CLASSTYPE_DECL_LIST (pattern);
8901 member; member = TREE_CHAIN (member))
8903 tree t = TREE_VALUE (member);
8905 if (TREE_PURPOSE (member))
8907 if (TYPE_P (t))
8909 /* Build new CLASSTYPE_NESTED_UTDS. */
8911 tree newtag;
8912 bool class_template_p;
8914 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8915 && TYPE_LANG_SPECIFIC (t)
8916 && CLASSTYPE_IS_TEMPLATE (t));
8917 /* If the member is a class template, then -- even after
8918 substitution -- there may be dependent types in the
8919 template argument list for the class. We increment
8920 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8921 that function will assume that no types are dependent
8922 when outside of a template. */
8923 if (class_template_p)
8924 ++processing_template_decl;
8925 newtag = tsubst (t, args, tf_error, NULL_TREE);
8926 if (class_template_p)
8927 --processing_template_decl;
8928 if (newtag == error_mark_node)
8929 continue;
8931 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8933 tree name = TYPE_IDENTIFIER (t);
8935 if (class_template_p)
8936 /* Unfortunately, lookup_template_class sets
8937 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8938 instantiation (i.e., for the type of a member
8939 template class nested within a template class.)
8940 This behavior is required for
8941 maybe_process_partial_specialization to work
8942 correctly, but is not accurate in this case;
8943 the TAG is not an instantiation of anything.
8944 (The corresponding TEMPLATE_DECL is an
8945 instantiation, but the TYPE is not.) */
8946 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8948 /* Now, we call pushtag to put this NEWTAG into the scope of
8949 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8950 pushtag calling push_template_decl. We don't have to do
8951 this for enums because it will already have been done in
8952 tsubst_enum. */
8953 if (name)
8954 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8955 pushtag (name, newtag, /*tag_scope=*/ts_current);
8958 else if (DECL_DECLARES_FUNCTION_P (t))
8960 /* Build new TYPE_METHODS. */
8961 tree r;
8963 if (TREE_CODE (t) == TEMPLATE_DECL)
8964 ++processing_template_decl;
8965 r = tsubst (t, args, tf_error, NULL_TREE);
8966 if (TREE_CODE (t) == TEMPLATE_DECL)
8967 --processing_template_decl;
8968 set_current_access_from_decl (r);
8969 finish_member_declaration (r);
8970 /* Instantiate members marked with attribute used. */
8971 if (r != error_mark_node && DECL_PRESERVE_P (r))
8972 mark_used (r);
8973 if (TREE_CODE (r) == FUNCTION_DECL
8974 && DECL_OMP_DECLARE_REDUCTION_P (r))
8975 cp_check_omp_declare_reduction (r);
8977 else
8979 /* Build new TYPE_FIELDS. */
8980 if (TREE_CODE (t) == STATIC_ASSERT)
8982 tree condition;
8984 ++c_inhibit_evaluation_warnings;
8985 condition =
8986 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8987 tf_warning_or_error, NULL_TREE,
8988 /*integral_constant_expression_p=*/true);
8989 --c_inhibit_evaluation_warnings;
8991 finish_static_assert (condition,
8992 STATIC_ASSERT_MESSAGE (t),
8993 STATIC_ASSERT_SOURCE_LOCATION (t),
8994 /*member_p=*/true);
8996 else if (TREE_CODE (t) != CONST_DECL)
8998 tree r;
8999 tree vec = NULL_TREE;
9000 int len = 1;
9002 /* The file and line for this declaration, to
9003 assist in error message reporting. Since we
9004 called push_tinst_level above, we don't need to
9005 restore these. */
9006 input_location = DECL_SOURCE_LOCATION (t);
9008 if (TREE_CODE (t) == TEMPLATE_DECL)
9009 ++processing_template_decl;
9010 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9011 if (TREE_CODE (t) == TEMPLATE_DECL)
9012 --processing_template_decl;
9014 if (TREE_CODE (r) == TREE_VEC)
9016 /* A capture pack became multiple fields. */
9017 vec = r;
9018 len = TREE_VEC_LENGTH (vec);
9021 for (int i = 0; i < len; ++i)
9023 if (vec)
9024 r = TREE_VEC_ELT (vec, i);
9025 if (VAR_P (r))
9027 /* In [temp.inst]:
9029 [t]he initialization (and any associated
9030 side-effects) of a static data member does
9031 not occur unless the static data member is
9032 itself used in a way that requires the
9033 definition of the static data member to
9034 exist.
9036 Therefore, we do not substitute into the
9037 initialized for the static data member here. */
9038 finish_static_data_member_decl
9040 /*init=*/NULL_TREE,
9041 /*init_const_expr_p=*/false,
9042 /*asmspec_tree=*/NULL_TREE,
9043 /*flags=*/0);
9044 /* Instantiate members marked with attribute used. */
9045 if (r != error_mark_node && DECL_PRESERVE_P (r))
9046 mark_used (r);
9048 else if (TREE_CODE (r) == FIELD_DECL)
9050 /* Determine whether R has a valid type and can be
9051 completed later. If R is invalid, then its type
9052 is replaced by error_mark_node. */
9053 tree rtype = TREE_TYPE (r);
9054 if (can_complete_type_without_circularity (rtype))
9055 complete_type (rtype);
9057 if (!COMPLETE_TYPE_P (rtype))
9059 cxx_incomplete_type_error (r, rtype);
9060 TREE_TYPE (r) = error_mark_node;
9064 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9065 such a thing will already have been added to the field
9066 list by tsubst_enum in finish_member_declaration in the
9067 CLASSTYPE_NESTED_UTDS case above. */
9068 if (!(TREE_CODE (r) == TYPE_DECL
9069 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9070 && DECL_ARTIFICIAL (r)))
9072 set_current_access_from_decl (r);
9073 finish_member_declaration (r);
9079 else
9081 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9082 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9084 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9086 tree friend_type = t;
9087 bool adjust_processing_template_decl = false;
9089 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9091 /* template <class T> friend class C; */
9092 friend_type = tsubst_friend_class (friend_type, args);
9093 adjust_processing_template_decl = true;
9095 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9097 /* template <class T> friend class C::D; */
9098 friend_type = tsubst (friend_type, args,
9099 tf_warning_or_error, NULL_TREE);
9100 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9101 friend_type = TREE_TYPE (friend_type);
9102 adjust_processing_template_decl = true;
9104 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9105 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9107 /* This could be either
9109 friend class T::C;
9111 when dependent_type_p is false or
9113 template <class U> friend class T::C;
9115 otherwise. */
9116 friend_type = tsubst (friend_type, args,
9117 tf_warning_or_error, NULL_TREE);
9118 /* Bump processing_template_decl for correct
9119 dependent_type_p calculation. */
9120 ++processing_template_decl;
9121 if (dependent_type_p (friend_type))
9122 adjust_processing_template_decl = true;
9123 --processing_template_decl;
9125 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9126 && hidden_name_p (TYPE_NAME (friend_type)))
9128 /* friend class C;
9130 where C hasn't been declared yet. Let's lookup name
9131 from namespace scope directly, bypassing any name that
9132 come from dependent base class. */
9133 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9135 /* The call to xref_tag_from_type does injection for friend
9136 classes. */
9137 push_nested_namespace (ns);
9138 friend_type =
9139 xref_tag_from_type (friend_type, NULL_TREE,
9140 /*tag_scope=*/ts_current);
9141 pop_nested_namespace (ns);
9143 else if (uses_template_parms (friend_type))
9144 /* friend class C<T>; */
9145 friend_type = tsubst (friend_type, args,
9146 tf_warning_or_error, NULL_TREE);
9147 /* Otherwise it's
9149 friend class C;
9151 where C is already declared or
9153 friend class C<int>;
9155 We don't have to do anything in these cases. */
9157 if (adjust_processing_template_decl)
9158 /* Trick make_friend_class into realizing that the friend
9159 we're adding is a template, not an ordinary class. It's
9160 important that we use make_friend_class since it will
9161 perform some error-checking and output cross-reference
9162 information. */
9163 ++processing_template_decl;
9165 if (friend_type != error_mark_node)
9166 make_friend_class (type, friend_type, /*complain=*/false);
9168 if (adjust_processing_template_decl)
9169 --processing_template_decl;
9171 else
9173 /* Build new DECL_FRIENDLIST. */
9174 tree r;
9176 /* The file and line for this declaration, to
9177 assist in error message reporting. Since we
9178 called push_tinst_level above, we don't need to
9179 restore these. */
9180 input_location = DECL_SOURCE_LOCATION (t);
9182 if (TREE_CODE (t) == TEMPLATE_DECL)
9184 ++processing_template_decl;
9185 push_deferring_access_checks (dk_no_check);
9188 r = tsubst_friend_function (t, args);
9189 add_friend (type, r, /*complain=*/false);
9190 if (TREE_CODE (t) == TEMPLATE_DECL)
9192 pop_deferring_access_checks ();
9193 --processing_template_decl;
9199 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9201 tree decl = lambda_function (type);
9202 if (decl)
9204 if (!DECL_TEMPLATE_INFO (decl)
9205 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9206 instantiate_decl (decl, false, false);
9208 /* We need to instantiate the capture list from the template
9209 after we've instantiated the closure members, but before we
9210 consider adding the conversion op. Also keep any captures
9211 that may have been added during instantiation of the op(). */
9212 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9213 tree tmpl_cap
9214 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9215 args, tf_warning_or_error, NULL_TREE,
9216 false, false);
9218 LAMBDA_EXPR_CAPTURE_LIST (expr)
9219 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9221 maybe_add_lambda_conv_op (type);
9223 else
9224 gcc_assert (errorcount);
9227 /* Set the file and line number information to whatever is given for
9228 the class itself. This puts error messages involving generated
9229 implicit functions at a predictable point, and the same point
9230 that would be used for non-template classes. */
9231 input_location = DECL_SOURCE_LOCATION (typedecl);
9233 unreverse_member_declarations (type);
9234 finish_struct_1 (type);
9235 TYPE_BEING_DEFINED (type) = 0;
9237 /* We don't instantiate default arguments for member functions. 14.7.1:
9239 The implicit instantiation of a class template specialization causes
9240 the implicit instantiation of the declarations, but not of the
9241 definitions or default arguments, of the class member functions,
9242 member classes, static data members and member templates.... */
9244 /* Some typedefs referenced from within the template code need to be access
9245 checked at template instantiation time, i.e now. These types were
9246 added to the template at parsing time. Let's get those and perform
9247 the access checks then. */
9248 perform_typedefs_access_check (pattern, args);
9249 perform_deferred_access_checks (tf_warning_or_error);
9250 pop_nested_class ();
9251 maximum_field_alignment = saved_maximum_field_alignment;
9252 if (!fn_context)
9253 pop_from_top_level ();
9254 pop_deferring_access_checks ();
9255 pop_tinst_level ();
9257 /* The vtable for a template class can be emitted in any translation
9258 unit in which the class is instantiated. When there is no key
9259 method, however, finish_struct_1 will already have added TYPE to
9260 the keyed_classes list. */
9261 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9262 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9264 return type;
9267 /* Wrapper for instantiate_class_template_1. */
9269 tree
9270 instantiate_class_template (tree type)
9272 tree ret;
9273 timevar_push (TV_TEMPLATE_INST);
9274 ret = instantiate_class_template_1 (type);
9275 timevar_pop (TV_TEMPLATE_INST);
9276 return ret;
9279 static tree
9280 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9282 tree r;
9284 if (!t)
9285 r = t;
9286 else if (TYPE_P (t))
9287 r = tsubst (t, args, complain, in_decl);
9288 else
9290 if (!(complain & tf_warning))
9291 ++c_inhibit_evaluation_warnings;
9292 r = tsubst_expr (t, args, complain, in_decl,
9293 /*integral_constant_expression_p=*/true);
9294 if (!(complain & tf_warning))
9295 --c_inhibit_evaluation_warnings;
9297 return r;
9300 /* Given a function parameter pack TMPL_PARM and some function parameters
9301 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9302 and set *SPEC_P to point at the next point in the list. */
9304 static tree
9305 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9307 /* Collect all of the extra "packed" parameters into an
9308 argument pack. */
9309 tree parmvec;
9310 tree parmtypevec;
9311 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9312 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9313 tree spec_parm = *spec_p;
9314 int i, len;
9316 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9317 if (tmpl_parm
9318 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9319 break;
9321 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9322 parmvec = make_tree_vec (len);
9323 parmtypevec = make_tree_vec (len);
9324 spec_parm = *spec_p;
9325 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9327 TREE_VEC_ELT (parmvec, i) = spec_parm;
9328 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9331 /* Build the argument packs. */
9332 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9333 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9334 TREE_TYPE (argpack) = argtypepack;
9335 *spec_p = spec_parm;
9337 return argpack;
9340 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9341 NONTYPE_ARGUMENT_PACK. */
9343 static tree
9344 make_fnparm_pack (tree spec_parm)
9346 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9349 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9350 pack expansion. */
9352 static bool
9353 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9355 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9356 if (i >= TREE_VEC_LENGTH (vec))
9357 return false;
9358 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9362 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9364 static tree
9365 make_argument_pack_select (tree arg_pack, unsigned index)
9367 tree aps = make_node (ARGUMENT_PACK_SELECT);
9369 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9370 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9372 return aps;
9375 /* This is a subroutine of tsubst_pack_expansion.
9377 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9378 mechanism to store the (non complete list of) arguments of the
9379 substitution and return a non substituted pack expansion, in order
9380 to wait for when we have enough arguments to really perform the
9381 substitution. */
9383 static bool
9384 use_pack_expansion_extra_args_p (tree parm_packs,
9385 int arg_pack_len,
9386 bool has_empty_arg)
9388 /* If one pack has an expansion and another pack has a normal
9389 argument or if one pack has an empty argument and an another
9390 one hasn't then tsubst_pack_expansion cannot perform the
9391 substitution and need to fall back on the
9392 PACK_EXPANSION_EXTRA mechanism. */
9393 if (parm_packs == NULL_TREE)
9394 return false;
9395 else if (has_empty_arg)
9396 return true;
9398 bool has_expansion_arg = false;
9399 for (int i = 0 ; i < arg_pack_len; ++i)
9401 bool has_non_expansion_arg = false;
9402 for (tree parm_pack = parm_packs;
9403 parm_pack;
9404 parm_pack = TREE_CHAIN (parm_pack))
9406 tree arg = TREE_VALUE (parm_pack);
9408 if (argument_pack_element_is_expansion_p (arg, i))
9409 has_expansion_arg = true;
9410 else
9411 has_non_expansion_arg = true;
9414 if (has_expansion_arg && has_non_expansion_arg)
9415 return true;
9417 return false;
9420 /* [temp.variadic]/6 says that:
9422 The instantiation of a pack expansion [...]
9423 produces a list E1,E2, ..., En, where N is the number of elements
9424 in the pack expansion parameters.
9426 This subroutine of tsubst_pack_expansion produces one of these Ei.
9428 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9429 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9430 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9431 INDEX is the index 'i' of the element Ei to produce. ARGS,
9432 COMPLAIN, and IN_DECL are the same parameters as for the
9433 tsubst_pack_expansion function.
9435 The function returns the resulting Ei upon successful completion,
9436 or error_mark_node.
9438 Note that this function possibly modifies the ARGS parameter, so
9439 it's the responsibility of the caller to restore it. */
9441 static tree
9442 gen_elem_of_pack_expansion_instantiation (tree pattern,
9443 tree parm_packs,
9444 unsigned index,
9445 tree args /* This parm gets
9446 modified. */,
9447 tsubst_flags_t complain,
9448 tree in_decl)
9450 tree t;
9451 bool ith_elem_is_expansion = false;
9453 /* For each parameter pack, change the substitution of the parameter
9454 pack to the ith argument in its argument pack, then expand the
9455 pattern. */
9456 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9458 tree parm = TREE_PURPOSE (pack);
9459 tree arg_pack = TREE_VALUE (pack);
9460 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9462 ith_elem_is_expansion |=
9463 argument_pack_element_is_expansion_p (arg_pack, index);
9465 /* Select the Ith argument from the pack. */
9466 if (TREE_CODE (parm) == PARM_DECL
9467 || TREE_CODE (parm) == FIELD_DECL)
9469 if (index == 0)
9471 aps = make_argument_pack_select (arg_pack, index);
9472 mark_used (parm);
9473 register_local_specialization (aps, parm);
9475 else
9476 aps = retrieve_local_specialization (parm);
9478 else
9480 int idx, level;
9481 template_parm_level_and_index (parm, &level, &idx);
9483 if (index == 0)
9485 aps = make_argument_pack_select (arg_pack, index);
9486 /* Update the corresponding argument. */
9487 TMPL_ARG (args, level, idx) = aps;
9489 else
9490 /* Re-use the ARGUMENT_PACK_SELECT. */
9491 aps = TMPL_ARG (args, level, idx);
9493 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9496 /* Substitute into the PATTERN with the (possibly altered)
9497 arguments. */
9498 if (!TYPE_P (pattern))
9499 t = tsubst_expr (pattern, args, complain, in_decl,
9500 /*integral_constant_expression_p=*/false);
9501 else
9502 t = tsubst (pattern, args, complain, in_decl);
9504 /* If the Ith argument pack element is a pack expansion, then
9505 the Ith element resulting from the substituting is going to
9506 be a pack expansion as well. */
9507 if (ith_elem_is_expansion)
9508 t = make_pack_expansion (t);
9510 return t;
9513 /* Substitute ARGS into T, which is an pack expansion
9514 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9515 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9516 (if only a partial substitution could be performed) or
9517 ERROR_MARK_NODE if there was an error. */
9518 tree
9519 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9520 tree in_decl)
9522 tree pattern;
9523 tree pack, packs = NULL_TREE;
9524 bool unsubstituted_packs = false;
9525 int i, len = -1;
9526 tree result;
9527 struct pointer_map_t *saved_local_specializations = NULL;
9528 bool need_local_specializations = false;
9529 int levels;
9531 gcc_assert (PACK_EXPANSION_P (t));
9532 pattern = PACK_EXPANSION_PATTERN (t);
9534 /* Add in any args remembered from an earlier partial instantiation. */
9535 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9537 levels = TMPL_ARGS_DEPTH (args);
9539 /* Determine the argument packs that will instantiate the parameter
9540 packs used in the expansion expression. While we're at it,
9541 compute the number of arguments to be expanded and make sure it
9542 is consistent. */
9543 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9544 pack = TREE_CHAIN (pack))
9546 tree parm_pack = TREE_VALUE (pack);
9547 tree arg_pack = NULL_TREE;
9548 tree orig_arg = NULL_TREE;
9549 int level = 0;
9551 if (TREE_CODE (parm_pack) == BASES)
9553 if (BASES_DIRECT (parm_pack))
9554 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9555 args, complain, in_decl, false));
9556 else
9557 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9558 args, complain, in_decl, false));
9560 if (TREE_CODE (parm_pack) == PARM_DECL)
9562 if (PACK_EXPANSION_LOCAL_P (t))
9563 arg_pack = retrieve_local_specialization (parm_pack);
9564 else
9566 /* We can't rely on local_specializations for a parameter
9567 name used later in a function declaration (such as in a
9568 late-specified return type). Even if it exists, it might
9569 have the wrong value for a recursive call. Just make a
9570 dummy decl, since it's only used for its type. */
9571 arg_pack = tsubst_decl (parm_pack, args, complain);
9572 if (arg_pack && DECL_PACK_P (arg_pack))
9573 /* Partial instantiation of the parm_pack, we can't build
9574 up an argument pack yet. */
9575 arg_pack = NULL_TREE;
9576 else
9577 arg_pack = make_fnparm_pack (arg_pack);
9578 need_local_specializations = true;
9581 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9582 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9583 else
9585 int idx;
9586 template_parm_level_and_index (parm_pack, &level, &idx);
9588 if (level <= levels)
9589 arg_pack = TMPL_ARG (args, level, idx);
9592 orig_arg = arg_pack;
9593 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9594 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9596 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9597 /* This can only happen if we forget to expand an argument
9598 pack somewhere else. Just return an error, silently. */
9600 result = make_tree_vec (1);
9601 TREE_VEC_ELT (result, 0) = error_mark_node;
9602 return result;
9605 if (arg_pack)
9607 int my_len =
9608 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9610 /* Don't bother trying to do a partial substitution with
9611 incomplete packs; we'll try again after deduction. */
9612 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9613 return t;
9615 if (len < 0)
9616 len = my_len;
9617 else if (len != my_len)
9619 if (!(complain & tf_error))
9620 /* Fail quietly. */;
9621 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9622 error ("mismatched argument pack lengths while expanding "
9623 "%<%T%>",
9624 pattern);
9625 else
9626 error ("mismatched argument pack lengths while expanding "
9627 "%<%E%>",
9628 pattern);
9629 return error_mark_node;
9632 /* Keep track of the parameter packs and their corresponding
9633 argument packs. */
9634 packs = tree_cons (parm_pack, arg_pack, packs);
9635 TREE_TYPE (packs) = orig_arg;
9637 else
9639 /* We can't substitute for this parameter pack. We use a flag as
9640 well as the missing_level counter because function parameter
9641 packs don't have a level. */
9642 unsubstituted_packs = true;
9646 /* We cannot expand this expansion expression, because we don't have
9647 all of the argument packs we need. */
9648 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9650 /* We got some full packs, but we can't substitute them in until we
9651 have values for all the packs. So remember these until then. */
9653 t = make_pack_expansion (pattern);
9654 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9655 return t;
9657 else if (unsubstituted_packs)
9659 /* There were no real arguments, we're just replacing a parameter
9660 pack with another version of itself. Substitute into the
9661 pattern and return a PACK_EXPANSION_*. The caller will need to
9662 deal with that. */
9663 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9664 t = tsubst_expr (pattern, args, complain, in_decl,
9665 /*integral_constant_expression_p=*/false);
9666 else
9667 t = tsubst (pattern, args, complain, in_decl);
9668 t = make_pack_expansion (t);
9669 return t;
9672 gcc_assert (len >= 0);
9674 if (need_local_specializations)
9676 /* We're in a late-specified return type, so create our own local
9677 specializations map; the current map is either NULL or (in the
9678 case of recursive unification) might have bindings that we don't
9679 want to use or alter. */
9680 saved_local_specializations = local_specializations;
9681 local_specializations = pointer_map_create ();
9684 /* For each argument in each argument pack, substitute into the
9685 pattern. */
9686 result = make_tree_vec (len);
9687 for (i = 0; i < len; ++i)
9689 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9691 args, complain,
9692 in_decl);
9693 TREE_VEC_ELT (result, i) = t;
9694 if (t == error_mark_node)
9696 result = error_mark_node;
9697 break;
9701 /* Update ARGS to restore the substitution from parameter packs to
9702 their argument packs. */
9703 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9705 tree parm = TREE_PURPOSE (pack);
9707 if (TREE_CODE (parm) == PARM_DECL
9708 || TREE_CODE (parm) == FIELD_DECL)
9709 register_local_specialization (TREE_TYPE (pack), parm);
9710 else
9712 int idx, level;
9714 if (TREE_VALUE (pack) == NULL_TREE)
9715 continue;
9717 template_parm_level_and_index (parm, &level, &idx);
9719 /* Update the corresponding argument. */
9720 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9721 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9722 TREE_TYPE (pack);
9723 else
9724 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9728 if (need_local_specializations)
9730 pointer_map_destroy (local_specializations);
9731 local_specializations = saved_local_specializations;
9734 return result;
9737 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9738 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9739 parameter packs; all parms generated from a function parameter pack will
9740 have the same DECL_PARM_INDEX. */
9742 tree
9743 get_pattern_parm (tree parm, tree tmpl)
9745 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9746 tree patparm;
9748 if (DECL_ARTIFICIAL (parm))
9750 for (patparm = DECL_ARGUMENTS (pattern);
9751 patparm; patparm = DECL_CHAIN (patparm))
9752 if (DECL_ARTIFICIAL (patparm)
9753 && DECL_NAME (parm) == DECL_NAME (patparm))
9754 break;
9756 else
9758 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9759 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9760 gcc_assert (DECL_PARM_INDEX (patparm)
9761 == DECL_PARM_INDEX (parm));
9764 return patparm;
9767 /* Substitute ARGS into the vector or list of template arguments T. */
9769 static tree
9770 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9772 tree orig_t = t;
9773 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9774 tree *elts;
9776 if (t == error_mark_node)
9777 return error_mark_node;
9779 len = TREE_VEC_LENGTH (t);
9780 elts = XALLOCAVEC (tree, len);
9782 for (i = 0; i < len; i++)
9784 tree orig_arg = TREE_VEC_ELT (t, i);
9785 tree new_arg;
9787 if (TREE_CODE (orig_arg) == TREE_VEC)
9788 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9789 else if (PACK_EXPANSION_P (orig_arg))
9791 /* Substitute into an expansion expression. */
9792 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9794 if (TREE_CODE (new_arg) == TREE_VEC)
9795 /* Add to the expanded length adjustment the number of
9796 expanded arguments. We subtract one from this
9797 measurement, because the argument pack expression
9798 itself is already counted as 1 in
9799 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9800 the argument pack is empty. */
9801 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9803 else if (ARGUMENT_PACK_P (orig_arg))
9805 /* Substitute into each of the arguments. */
9806 new_arg = TYPE_P (orig_arg)
9807 ? cxx_make_type (TREE_CODE (orig_arg))
9808 : make_node (TREE_CODE (orig_arg));
9810 SET_ARGUMENT_PACK_ARGS (
9811 new_arg,
9812 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9813 args, complain, in_decl));
9815 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9816 new_arg = error_mark_node;
9818 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9819 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9820 complain, in_decl);
9821 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9823 if (TREE_TYPE (new_arg) == error_mark_node)
9824 new_arg = error_mark_node;
9827 else
9828 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9830 if (new_arg == error_mark_node)
9831 return error_mark_node;
9833 elts[i] = new_arg;
9834 if (new_arg != orig_arg)
9835 need_new = 1;
9838 if (!need_new)
9839 return t;
9841 /* Make space for the expanded arguments coming from template
9842 argument packs. */
9843 t = make_tree_vec (len + expanded_len_adjust);
9844 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9845 arguments for a member template.
9846 In that case each TREE_VEC in ORIG_T represents a level of template
9847 arguments, and ORIG_T won't carry any non defaulted argument count.
9848 It will rather be the nested TREE_VECs that will carry one.
9849 In other words, ORIG_T carries a non defaulted argument count only
9850 if it doesn't contain any nested TREE_VEC. */
9851 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9853 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9854 count += expanded_len_adjust;
9855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9857 for (i = 0, out = 0; i < len; i++)
9859 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9860 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9861 && TREE_CODE (elts[i]) == TREE_VEC)
9863 int idx;
9865 /* Now expand the template argument pack "in place". */
9866 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9867 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9869 else
9871 TREE_VEC_ELT (t, out) = elts[i];
9872 out++;
9876 return t;
9879 /* Return the result of substituting ARGS into the template parameters
9880 given by PARMS. If there are m levels of ARGS and m + n levels of
9881 PARMS, then the result will contain n levels of PARMS. For
9882 example, if PARMS is `template <class T> template <class U>
9883 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9884 result will be `template <int*, double, class V>'. */
9886 static tree
9887 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9889 tree r = NULL_TREE;
9890 tree* new_parms;
9892 /* When substituting into a template, we must set
9893 PROCESSING_TEMPLATE_DECL as the template parameters may be
9894 dependent if they are based on one-another, and the dependency
9895 predicates are short-circuit outside of templates. */
9896 ++processing_template_decl;
9898 for (new_parms = &r;
9899 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9900 new_parms = &(TREE_CHAIN (*new_parms)),
9901 parms = TREE_CHAIN (parms))
9903 tree new_vec =
9904 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9905 int i;
9907 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9909 tree tuple;
9911 if (parms == error_mark_node)
9912 continue;
9914 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9916 if (tuple == error_mark_node)
9917 continue;
9919 TREE_VEC_ELT (new_vec, i) =
9920 tsubst_template_parm (tuple, args, complain);
9923 *new_parms =
9924 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9925 - TMPL_ARGS_DEPTH (args)),
9926 new_vec, NULL_TREE);
9929 --processing_template_decl;
9931 return r;
9934 /* Return the result of substituting ARGS into one template parameter
9935 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9936 parameter and which TREE_PURPOSE is the default argument of the
9937 template parameter. */
9939 static tree
9940 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9942 tree default_value, parm_decl;
9944 if (args == NULL_TREE
9945 || t == NULL_TREE
9946 || t == error_mark_node)
9947 return t;
9949 gcc_assert (TREE_CODE (t) == TREE_LIST);
9951 default_value = TREE_PURPOSE (t);
9952 parm_decl = TREE_VALUE (t);
9954 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9955 if (TREE_CODE (parm_decl) == PARM_DECL
9956 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9957 parm_decl = error_mark_node;
9958 default_value = tsubst_template_arg (default_value, args,
9959 complain, NULL_TREE);
9961 return build_tree_list (default_value, parm_decl);
9964 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9965 type T. If T is not an aggregate or enumeration type, it is
9966 handled as if by tsubst. IN_DECL is as for tsubst. If
9967 ENTERING_SCOPE is nonzero, T is the context for a template which
9968 we are presently tsubst'ing. Return the substituted value. */
9970 static tree
9971 tsubst_aggr_type (tree t,
9972 tree args,
9973 tsubst_flags_t complain,
9974 tree in_decl,
9975 int entering_scope)
9977 if (t == NULL_TREE)
9978 return NULL_TREE;
9980 switch (TREE_CODE (t))
9982 case RECORD_TYPE:
9983 if (TYPE_PTRMEMFUNC_P (t))
9984 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9986 /* Else fall through. */
9987 case ENUMERAL_TYPE:
9988 case UNION_TYPE:
9989 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9991 tree argvec;
9992 tree context;
9993 tree r;
9994 int saved_unevaluated_operand;
9995 int saved_inhibit_evaluation_warnings;
9997 /* In "sizeof(X<I>)" we need to evaluate "I". */
9998 saved_unevaluated_operand = cp_unevaluated_operand;
9999 cp_unevaluated_operand = 0;
10000 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10001 c_inhibit_evaluation_warnings = 0;
10003 /* First, determine the context for the type we are looking
10004 up. */
10005 context = TYPE_CONTEXT (t);
10006 if (context && TYPE_P (context))
10008 context = tsubst_aggr_type (context, args, complain,
10009 in_decl, /*entering_scope=*/1);
10010 /* If context is a nested class inside a class template,
10011 it may still need to be instantiated (c++/33959). */
10012 context = complete_type (context);
10015 /* Then, figure out what arguments are appropriate for the
10016 type we are trying to find. For example, given:
10018 template <class T> struct S;
10019 template <class T, class U> void f(T, U) { S<U> su; }
10021 and supposing that we are instantiating f<int, double>,
10022 then our ARGS will be {int, double}, but, when looking up
10023 S we only want {double}. */
10024 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10025 complain, in_decl);
10026 if (argvec == error_mark_node)
10027 r = error_mark_node;
10028 else
10030 r = lookup_template_class (t, argvec, in_decl, context,
10031 entering_scope, complain);
10032 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10035 cp_unevaluated_operand = saved_unevaluated_operand;
10036 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10038 return r;
10040 else
10041 /* This is not a template type, so there's nothing to do. */
10042 return t;
10044 default:
10045 return tsubst (t, args, complain, in_decl);
10049 /* Substitute into the default argument ARG (a default argument for
10050 FN), which has the indicated TYPE. */
10052 tree
10053 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10055 tree saved_class_ptr = NULL_TREE;
10056 tree saved_class_ref = NULL_TREE;
10057 int errs = errorcount + sorrycount;
10059 /* This can happen in invalid code. */
10060 if (TREE_CODE (arg) == DEFAULT_ARG)
10061 return arg;
10063 /* This default argument came from a template. Instantiate the
10064 default argument here, not in tsubst. In the case of
10065 something like:
10067 template <class T>
10068 struct S {
10069 static T t();
10070 void f(T = t());
10073 we must be careful to do name lookup in the scope of S<T>,
10074 rather than in the current class. */
10075 push_access_scope (fn);
10076 /* The "this" pointer is not valid in a default argument. */
10077 if (cfun)
10079 saved_class_ptr = current_class_ptr;
10080 cp_function_chain->x_current_class_ptr = NULL_TREE;
10081 saved_class_ref = current_class_ref;
10082 cp_function_chain->x_current_class_ref = NULL_TREE;
10085 push_deferring_access_checks(dk_no_deferred);
10086 /* The default argument expression may cause implicitly defined
10087 member functions to be synthesized, which will result in garbage
10088 collection. We must treat this situation as if we were within
10089 the body of function so as to avoid collecting live data on the
10090 stack. */
10091 ++function_depth;
10092 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10093 complain, NULL_TREE,
10094 /*integral_constant_expression_p=*/false);
10095 --function_depth;
10096 pop_deferring_access_checks();
10098 /* Restore the "this" pointer. */
10099 if (cfun)
10101 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10102 cp_function_chain->x_current_class_ref = saved_class_ref;
10105 if (errorcount+sorrycount > errs
10106 && (complain & tf_warning_or_error))
10107 inform (input_location,
10108 " when instantiating default argument for call to %D", fn);
10110 /* Make sure the default argument is reasonable. */
10111 arg = check_default_argument (type, arg, complain);
10113 pop_access_scope (fn);
10115 return arg;
10118 /* Substitute into all the default arguments for FN. */
10120 static void
10121 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10123 tree arg;
10124 tree tmpl_args;
10126 tmpl_args = DECL_TI_ARGS (fn);
10128 /* If this function is not yet instantiated, we certainly don't need
10129 its default arguments. */
10130 if (uses_template_parms (tmpl_args))
10131 return;
10132 /* Don't do this again for clones. */
10133 if (DECL_CLONED_FUNCTION_P (fn))
10134 return;
10136 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10137 arg;
10138 arg = TREE_CHAIN (arg))
10139 if (TREE_PURPOSE (arg))
10140 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10141 TREE_VALUE (arg),
10142 TREE_PURPOSE (arg),
10143 complain);
10146 /* Substitute the ARGS into the T, which is a _DECL. Return the
10147 result of the substitution. Issue error and warning messages under
10148 control of COMPLAIN. */
10150 static tree
10151 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10153 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10154 location_t saved_loc;
10155 tree r = NULL_TREE;
10156 tree in_decl = t;
10157 hashval_t hash = 0;
10159 /* Set the filename and linenumber to improve error-reporting. */
10160 saved_loc = input_location;
10161 input_location = DECL_SOURCE_LOCATION (t);
10163 switch (TREE_CODE (t))
10165 case TEMPLATE_DECL:
10167 /* We can get here when processing a member function template,
10168 member class template, or template template parameter. */
10169 tree decl = DECL_TEMPLATE_RESULT (t);
10170 tree spec;
10171 tree tmpl_args;
10172 tree full_args;
10174 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10176 /* Template template parameter is treated here. */
10177 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10178 if (new_type == error_mark_node)
10179 RETURN (error_mark_node);
10180 /* If we get a real template back, return it. This can happen in
10181 the context of most_specialized_class. */
10182 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10183 return new_type;
10185 r = copy_decl (t);
10186 DECL_CHAIN (r) = NULL_TREE;
10187 TREE_TYPE (r) = new_type;
10188 DECL_TEMPLATE_RESULT (r)
10189 = build_decl (DECL_SOURCE_LOCATION (decl),
10190 TYPE_DECL, DECL_NAME (decl), new_type);
10191 DECL_TEMPLATE_PARMS (r)
10192 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10193 complain);
10194 TYPE_NAME (new_type) = r;
10195 break;
10198 /* We might already have an instance of this template.
10199 The ARGS are for the surrounding class type, so the
10200 full args contain the tsubst'd args for the context,
10201 plus the innermost args from the template decl. */
10202 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10203 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10204 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10205 /* Because this is a template, the arguments will still be
10206 dependent, even after substitution. If
10207 PROCESSING_TEMPLATE_DECL is not set, the dependency
10208 predicates will short-circuit. */
10209 ++processing_template_decl;
10210 full_args = tsubst_template_args (tmpl_args, args,
10211 complain, in_decl);
10212 --processing_template_decl;
10213 if (full_args == error_mark_node)
10214 RETURN (error_mark_node);
10216 /* If this is a default template template argument,
10217 tsubst might not have changed anything. */
10218 if (full_args == tmpl_args)
10219 RETURN (t);
10221 hash = hash_tmpl_and_args (t, full_args);
10222 spec = retrieve_specialization (t, full_args, hash);
10223 if (spec != NULL_TREE)
10225 r = spec;
10226 break;
10229 /* Make a new template decl. It will be similar to the
10230 original, but will record the current template arguments.
10231 We also create a new function declaration, which is just
10232 like the old one, but points to this new template, rather
10233 than the old one. */
10234 r = copy_decl (t);
10235 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10236 DECL_CHAIN (r) = NULL_TREE;
10238 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10240 if (TREE_CODE (decl) == TYPE_DECL
10241 && !TYPE_DECL_ALIAS_P (decl))
10243 tree new_type;
10244 ++processing_template_decl;
10245 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10246 --processing_template_decl;
10247 if (new_type == error_mark_node)
10248 RETURN (error_mark_node);
10250 TREE_TYPE (r) = new_type;
10251 /* For a partial specialization, we need to keep pointing to
10252 the primary template. */
10253 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10254 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10255 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10256 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10257 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10259 else
10261 tree new_decl;
10262 ++processing_template_decl;
10263 new_decl = tsubst (decl, args, complain, in_decl);
10264 --processing_template_decl;
10265 if (new_decl == error_mark_node)
10266 RETURN (error_mark_node);
10268 DECL_TEMPLATE_RESULT (r) = new_decl;
10269 DECL_TI_TEMPLATE (new_decl) = r;
10270 TREE_TYPE (r) = TREE_TYPE (new_decl);
10271 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10272 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10275 SET_DECL_IMPLICIT_INSTANTIATION (r);
10276 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10277 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10279 /* The template parameters for this new template are all the
10280 template parameters for the old template, except the
10281 outermost level of parameters. */
10282 DECL_TEMPLATE_PARMS (r)
10283 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10284 complain);
10286 if (PRIMARY_TEMPLATE_P (t))
10287 DECL_PRIMARY_TEMPLATE (r) = r;
10289 if (TREE_CODE (decl) != TYPE_DECL)
10290 /* Record this non-type partial instantiation. */
10291 register_specialization (r, t,
10292 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10293 false, hash);
10295 break;
10297 case FUNCTION_DECL:
10299 tree ctx;
10300 tree argvec = NULL_TREE;
10301 tree *friends;
10302 tree gen_tmpl;
10303 tree type;
10304 int member;
10305 int args_depth;
10306 int parms_depth;
10308 /* Nobody should be tsubst'ing into non-template functions. */
10309 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10311 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10313 tree spec;
10314 bool dependent_p;
10316 /* If T is not dependent, just return it. We have to
10317 increment PROCESSING_TEMPLATE_DECL because
10318 value_dependent_expression_p assumes that nothing is
10319 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10320 ++processing_template_decl;
10321 dependent_p = value_dependent_expression_p (t);
10322 --processing_template_decl;
10323 if (!dependent_p)
10324 RETURN (t);
10326 /* Calculate the most general template of which R is a
10327 specialization, and the complete set of arguments used to
10328 specialize R. */
10329 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10330 argvec = tsubst_template_args (DECL_TI_ARGS
10331 (DECL_TEMPLATE_RESULT
10332 (DECL_TI_TEMPLATE (t))),
10333 args, complain, in_decl);
10334 if (argvec == error_mark_node)
10335 RETURN (error_mark_node);
10337 /* Check to see if we already have this specialization. */
10338 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10339 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10341 if (spec)
10343 r = spec;
10344 break;
10347 /* We can see more levels of arguments than parameters if
10348 there was a specialization of a member template, like
10349 this:
10351 template <class T> struct S { template <class U> void f(); }
10352 template <> template <class U> void S<int>::f(U);
10354 Here, we'll be substituting into the specialization,
10355 because that's where we can find the code we actually
10356 want to generate, but we'll have enough arguments for
10357 the most general template.
10359 We also deal with the peculiar case:
10361 template <class T> struct S {
10362 template <class U> friend void f();
10364 template <class U> void f() {}
10365 template S<int>;
10366 template void f<double>();
10368 Here, the ARGS for the instantiation of will be {int,
10369 double}. But, we only need as many ARGS as there are
10370 levels of template parameters in CODE_PATTERN. We are
10371 careful not to get fooled into reducing the ARGS in
10372 situations like:
10374 template <class T> struct S { template <class U> void f(U); }
10375 template <class T> template <> void S<T>::f(int) {}
10377 which we can spot because the pattern will be a
10378 specialization in this case. */
10379 args_depth = TMPL_ARGS_DEPTH (args);
10380 parms_depth =
10381 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10382 if (args_depth > parms_depth
10383 && !DECL_TEMPLATE_SPECIALIZATION (t))
10384 args = get_innermost_template_args (args, parms_depth);
10386 else
10388 /* This special case arises when we have something like this:
10390 template <class T> struct S {
10391 friend void f<int>(int, double);
10394 Here, the DECL_TI_TEMPLATE for the friend declaration
10395 will be an IDENTIFIER_NODE. We are being called from
10396 tsubst_friend_function, and we want only to create a
10397 new decl (R) with appropriate types so that we can call
10398 determine_specialization. */
10399 gen_tmpl = NULL_TREE;
10402 if (DECL_CLASS_SCOPE_P (t))
10404 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10405 member = 2;
10406 else
10407 member = 1;
10408 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10409 complain, t, /*entering_scope=*/1);
10411 else
10413 member = 0;
10414 ctx = DECL_CONTEXT (t);
10416 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10417 if (type == error_mark_node)
10418 RETURN (error_mark_node);
10420 /* If we hit excessive deduction depth, the type is bogus even if
10421 it isn't error_mark_node, so don't build a decl. */
10422 if (excessive_deduction_depth)
10423 RETURN (error_mark_node);
10425 /* We do NOT check for matching decls pushed separately at this
10426 point, as they may not represent instantiations of this
10427 template, and in any case are considered separate under the
10428 discrete model. */
10429 r = copy_decl (t);
10430 DECL_USE_TEMPLATE (r) = 0;
10431 TREE_TYPE (r) = type;
10432 /* Clear out the mangled name and RTL for the instantiation. */
10433 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10434 SET_DECL_RTL (r, NULL);
10435 /* Leave DECL_INITIAL set on deleted instantiations. */
10436 if (!DECL_DELETED_FN (r))
10437 DECL_INITIAL (r) = NULL_TREE;
10438 DECL_CONTEXT (r) = ctx;
10440 /* OpenMP UDRs have the only argument a reference to the declared
10441 type. We want to diagnose if the declared type is a reference,
10442 which is invalid, but as references to references are usually
10443 quietly merged, diagnose it here. */
10444 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10446 tree argtype
10447 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10448 argtype = tsubst (argtype, args, complain, in_decl);
10449 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10450 error_at (DECL_SOURCE_LOCATION (t),
10451 "reference type %qT in "
10452 "%<#pragma omp declare reduction%>", argtype);
10453 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10454 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10455 argtype);
10458 if (member && DECL_CONV_FN_P (r))
10459 /* Type-conversion operator. Reconstruct the name, in
10460 case it's the name of one of the template's parameters. */
10461 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10463 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10464 complain, t);
10465 DECL_RESULT (r) = NULL_TREE;
10467 TREE_STATIC (r) = 0;
10468 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10469 DECL_EXTERNAL (r) = 1;
10470 /* If this is an instantiation of a function with internal
10471 linkage, we already know what object file linkage will be
10472 assigned to the instantiation. */
10473 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10474 DECL_DEFER_OUTPUT (r) = 0;
10475 DECL_CHAIN (r) = NULL_TREE;
10476 DECL_PENDING_INLINE_INFO (r) = 0;
10477 DECL_PENDING_INLINE_P (r) = 0;
10478 DECL_SAVED_TREE (r) = NULL_TREE;
10479 DECL_STRUCT_FUNCTION (r) = NULL;
10480 TREE_USED (r) = 0;
10481 /* We'll re-clone as appropriate in instantiate_template. */
10482 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10484 /* If we aren't complaining now, return on error before we register
10485 the specialization so that we'll complain eventually. */
10486 if ((complain & tf_error) == 0
10487 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10488 && !grok_op_properties (r, /*complain=*/false))
10489 RETURN (error_mark_node);
10491 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10492 this in the special friend case mentioned above where
10493 GEN_TMPL is NULL. */
10494 if (gen_tmpl)
10496 DECL_TEMPLATE_INFO (r)
10497 = build_template_info (gen_tmpl, argvec);
10498 SET_DECL_IMPLICIT_INSTANTIATION (r);
10500 tree new_r
10501 = register_specialization (r, gen_tmpl, argvec, false, hash);
10502 if (new_r != r)
10503 /* We instantiated this while substituting into
10504 the type earlier (template/friend54.C). */
10505 RETURN (new_r);
10507 /* We're not supposed to instantiate default arguments
10508 until they are called, for a template. But, for a
10509 declaration like:
10511 template <class T> void f ()
10512 { extern void g(int i = T()); }
10514 we should do the substitution when the template is
10515 instantiated. We handle the member function case in
10516 instantiate_class_template since the default arguments
10517 might refer to other members of the class. */
10518 if (!member
10519 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10520 && !uses_template_parms (argvec))
10521 tsubst_default_arguments (r, complain);
10523 else
10524 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10526 /* Copy the list of befriending classes. */
10527 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10528 *friends;
10529 friends = &TREE_CHAIN (*friends))
10531 *friends = copy_node (*friends);
10532 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10533 args, complain,
10534 in_decl);
10537 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10539 maybe_retrofit_in_chrg (r);
10540 if (DECL_CONSTRUCTOR_P (r))
10541 grok_ctor_properties (ctx, r);
10542 if (DECL_INHERITED_CTOR_BASE (r))
10543 deduce_inheriting_ctor (r);
10544 /* If this is an instantiation of a member template, clone it.
10545 If it isn't, that'll be handled by
10546 clone_constructors_and_destructors. */
10547 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10548 clone_function_decl (r, /*update_method_vec_p=*/0);
10550 else if ((complain & tf_error) != 0
10551 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10552 && !grok_op_properties (r, /*complain=*/true))
10553 RETURN (error_mark_node);
10555 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10556 SET_DECL_FRIEND_CONTEXT (r,
10557 tsubst (DECL_FRIEND_CONTEXT (t),
10558 args, complain, in_decl));
10560 /* Possibly limit visibility based on template args. */
10561 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10562 if (DECL_VISIBILITY_SPECIFIED (t))
10564 DECL_VISIBILITY_SPECIFIED (r) = 0;
10565 DECL_ATTRIBUTES (r)
10566 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10568 determine_visibility (r);
10569 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10570 && !processing_template_decl)
10571 defaulted_late_check (r);
10573 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10574 args, complain, in_decl);
10576 break;
10578 case PARM_DECL:
10580 tree type = NULL_TREE;
10581 int i, len = 1;
10582 tree expanded_types = NULL_TREE;
10583 tree prev_r = NULL_TREE;
10584 tree first_r = NULL_TREE;
10586 if (DECL_PACK_P (t))
10588 /* If there is a local specialization that isn't a
10589 parameter pack, it means that we're doing a "simple"
10590 substitution from inside tsubst_pack_expansion. Just
10591 return the local specialization (which will be a single
10592 parm). */
10593 tree spec = retrieve_local_specialization (t);
10594 if (spec
10595 && TREE_CODE (spec) == PARM_DECL
10596 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10597 RETURN (spec);
10599 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10600 the parameters in this function parameter pack. */
10601 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10602 complain, in_decl);
10603 if (TREE_CODE (expanded_types) == TREE_VEC)
10605 len = TREE_VEC_LENGTH (expanded_types);
10607 /* Zero-length parameter packs are boring. Just substitute
10608 into the chain. */
10609 if (len == 0)
10610 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10611 TREE_CHAIN (t)));
10613 else
10615 /* All we did was update the type. Make a note of that. */
10616 type = expanded_types;
10617 expanded_types = NULL_TREE;
10621 /* Loop through all of the parameters we'll build. When T is
10622 a function parameter pack, LEN is the number of expanded
10623 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10624 r = NULL_TREE;
10625 for (i = 0; i < len; ++i)
10627 prev_r = r;
10628 r = copy_node (t);
10629 if (DECL_TEMPLATE_PARM_P (t))
10630 SET_DECL_TEMPLATE_PARM_P (r);
10632 if (expanded_types)
10633 /* We're on the Ith parameter of the function parameter
10634 pack. */
10636 /* Get the Ith type. */
10637 type = TREE_VEC_ELT (expanded_types, i);
10639 /* Rename the parameter to include the index. */
10640 DECL_NAME (r)
10641 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10643 else if (!type)
10644 /* We're dealing with a normal parameter. */
10645 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10647 type = type_decays_to (type);
10648 TREE_TYPE (r) = type;
10649 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10651 if (DECL_INITIAL (r))
10653 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10654 DECL_INITIAL (r) = TREE_TYPE (r);
10655 else
10656 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10657 complain, in_decl);
10660 DECL_CONTEXT (r) = NULL_TREE;
10662 if (!DECL_TEMPLATE_PARM_P (r))
10663 DECL_ARG_TYPE (r) = type_passed_as (type);
10665 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10666 args, complain, in_decl);
10668 /* Keep track of the first new parameter we
10669 generate. That's what will be returned to the
10670 caller. */
10671 if (!first_r)
10672 first_r = r;
10674 /* Build a proper chain of parameters when substituting
10675 into a function parameter pack. */
10676 if (prev_r)
10677 DECL_CHAIN (prev_r) = r;
10680 /* If cp_unevaluated_operand is set, we're just looking for a
10681 single dummy parameter, so don't keep going. */
10682 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10683 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10684 complain, DECL_CHAIN (t));
10686 /* FIRST_R contains the start of the chain we've built. */
10687 r = first_r;
10689 break;
10691 case FIELD_DECL:
10693 tree type = NULL_TREE;
10694 tree vec = NULL_TREE;
10695 tree expanded_types = NULL_TREE;
10696 int len = 1;
10698 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10700 /* This field is a lambda capture pack. Return a TREE_VEC of
10701 the expanded fields to instantiate_class_template_1 and
10702 store them in the specializations hash table as a
10703 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10704 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10705 complain, in_decl);
10706 if (TREE_CODE (expanded_types) == TREE_VEC)
10708 len = TREE_VEC_LENGTH (expanded_types);
10709 vec = make_tree_vec (len);
10711 else
10713 /* All we did was update the type. Make a note of that. */
10714 type = expanded_types;
10715 expanded_types = NULL_TREE;
10719 for (int i = 0; i < len; ++i)
10721 r = copy_decl (t);
10722 if (expanded_types)
10724 type = TREE_VEC_ELT (expanded_types, i);
10725 DECL_NAME (r)
10726 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10728 else if (!type)
10729 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10731 if (type == error_mark_node)
10732 RETURN (error_mark_node);
10733 TREE_TYPE (r) = type;
10734 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10736 if (DECL_C_BIT_FIELD (r))
10737 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10738 non-bit-fields DECL_INITIAL is a non-static data member
10739 initializer, which gets deferred instantiation. */
10740 DECL_INITIAL (r)
10741 = tsubst_expr (DECL_INITIAL (t), args,
10742 complain, in_decl,
10743 /*integral_constant_expression_p=*/true);
10744 else if (DECL_INITIAL (t))
10746 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10747 NSDMI in perform_member_init. Still set DECL_INITIAL
10748 so that we know there is one. */
10749 DECL_INITIAL (r) = void_zero_node;
10750 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10751 retrofit_lang_decl (r);
10752 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10754 /* We don't have to set DECL_CONTEXT here; it is set by
10755 finish_member_declaration. */
10756 DECL_CHAIN (r) = NULL_TREE;
10758 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10759 args, complain, in_decl);
10761 if (vec)
10762 TREE_VEC_ELT (vec, i) = r;
10765 if (vec)
10767 r = vec;
10768 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10769 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10770 SET_ARGUMENT_PACK_ARGS (pack, vec);
10771 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10772 TREE_TYPE (pack) = tpack;
10773 register_specialization (pack, t, args, false, 0);
10776 break;
10778 case USING_DECL:
10779 /* We reach here only for member using decls. We also need to check
10780 uses_template_parms because DECL_DEPENDENT_P is not set for a
10781 using-declaration that designates a member of the current
10782 instantiation (c++/53549). */
10783 if (DECL_DEPENDENT_P (t)
10784 || uses_template_parms (USING_DECL_SCOPE (t)))
10786 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10787 complain, in_decl);
10788 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10789 r = do_class_using_decl (inst_scope, name);
10790 if (!r)
10791 r = error_mark_node;
10792 else
10794 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10795 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10798 else
10800 r = copy_node (t);
10801 DECL_CHAIN (r) = NULL_TREE;
10803 break;
10805 case TYPE_DECL:
10806 case VAR_DECL:
10808 tree argvec = NULL_TREE;
10809 tree gen_tmpl = NULL_TREE;
10810 tree spec;
10811 tree tmpl = NULL_TREE;
10812 tree ctx;
10813 tree type = NULL_TREE;
10814 bool local_p;
10816 if (TREE_CODE (t) == TYPE_DECL
10817 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10819 /* If this is the canonical decl, we don't have to
10820 mess with instantiations, and often we can't (for
10821 typename, template type parms and such). Note that
10822 TYPE_NAME is not correct for the above test if
10823 we've copied the type for a typedef. */
10824 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10825 if (type == error_mark_node)
10826 RETURN (error_mark_node);
10827 r = TYPE_NAME (type);
10828 break;
10831 /* Check to see if we already have the specialization we
10832 need. */
10833 spec = NULL_TREE;
10834 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10836 /* T is a static data member or namespace-scope entity.
10837 We have to substitute into namespace-scope variables
10838 (even though such entities are never templates) because
10839 of cases like:
10841 template <class T> void f() { extern T t; }
10843 where the entity referenced is not known until
10844 instantiation time. */
10845 local_p = false;
10846 ctx = DECL_CONTEXT (t);
10847 if (DECL_CLASS_SCOPE_P (t))
10849 ctx = tsubst_aggr_type (ctx, args,
10850 complain,
10851 in_decl, /*entering_scope=*/1);
10852 /* If CTX is unchanged, then T is in fact the
10853 specialization we want. That situation occurs when
10854 referencing a static data member within in its own
10855 class. We can use pointer equality, rather than
10856 same_type_p, because DECL_CONTEXT is always
10857 canonical... */
10858 if (ctx == DECL_CONTEXT (t)
10859 && (TREE_CODE (t) != TYPE_DECL
10860 /* ... unless T is a member template; in which
10861 case our caller can be willing to create a
10862 specialization of that template represented
10863 by T. */
10864 || !(DECL_TI_TEMPLATE (t)
10865 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10866 spec = t;
10869 if (!spec)
10871 tmpl = DECL_TI_TEMPLATE (t);
10872 gen_tmpl = most_general_template (tmpl);
10873 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10874 if (argvec == error_mark_node)
10875 RETURN (error_mark_node);
10876 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10877 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10880 else
10882 /* A local variable. */
10883 local_p = true;
10884 /* Subsequent calls to pushdecl will fill this in. */
10885 ctx = NULL_TREE;
10886 spec = retrieve_local_specialization (t);
10888 /* If we already have the specialization we need, there is
10889 nothing more to do. */
10890 if (spec)
10892 r = spec;
10893 break;
10896 /* Create a new node for the specialization we need. */
10897 r = copy_decl (t);
10898 if (type == NULL_TREE)
10900 if (is_typedef_decl (t))
10901 type = DECL_ORIGINAL_TYPE (t);
10902 else
10903 type = TREE_TYPE (t);
10904 if (VAR_P (t)
10905 && VAR_HAD_UNKNOWN_BOUND (t)
10906 && type != error_mark_node)
10907 type = strip_array_domain (type);
10908 type = tsubst (type, args, complain, in_decl);
10910 if (VAR_P (r))
10912 /* Even if the original location is out of scope, the
10913 newly substituted one is not. */
10914 DECL_DEAD_FOR_LOCAL (r) = 0;
10915 DECL_INITIALIZED_P (r) = 0;
10916 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10917 if (type == error_mark_node)
10918 RETURN (error_mark_node);
10919 if (TREE_CODE (type) == FUNCTION_TYPE)
10921 /* It may seem that this case cannot occur, since:
10923 typedef void f();
10924 void g() { f x; }
10926 declares a function, not a variable. However:
10928 typedef void f();
10929 template <typename T> void g() { T t; }
10930 template void g<f>();
10932 is an attempt to declare a variable with function
10933 type. */
10934 error ("variable %qD has function type",
10935 /* R is not yet sufficiently initialized, so we
10936 just use its name. */
10937 DECL_NAME (r));
10938 RETURN (error_mark_node);
10940 type = complete_type (type);
10941 /* Wait until cp_finish_decl to set this again, to handle
10942 circular dependency (template/instantiate6.C). */
10943 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10944 type = check_var_type (DECL_NAME (r), type);
10946 if (DECL_HAS_VALUE_EXPR_P (t))
10948 tree ve = DECL_VALUE_EXPR (t);
10949 ve = tsubst_expr (ve, args, complain, in_decl,
10950 /*constant_expression_p=*/false);
10951 if (REFERENCE_REF_P (ve))
10953 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10954 ve = TREE_OPERAND (ve, 0);
10956 SET_DECL_VALUE_EXPR (r, ve);
10959 else if (DECL_SELF_REFERENCE_P (t))
10960 SET_DECL_SELF_REFERENCE_P (r);
10961 TREE_TYPE (r) = type;
10962 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10963 DECL_CONTEXT (r) = ctx;
10964 /* Clear out the mangled name and RTL for the instantiation. */
10965 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10966 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10967 SET_DECL_RTL (r, NULL);
10968 /* The initializer must not be expanded until it is required;
10969 see [temp.inst]. */
10970 DECL_INITIAL (r) = NULL_TREE;
10971 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10972 SET_DECL_RTL (r, NULL);
10973 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10974 if (VAR_P (r))
10976 /* Possibly limit visibility based on template args. */
10977 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10978 if (DECL_VISIBILITY_SPECIFIED (t))
10980 DECL_VISIBILITY_SPECIFIED (r) = 0;
10981 DECL_ATTRIBUTES (r)
10982 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10984 determine_visibility (r);
10987 if (!local_p)
10989 /* A static data member declaration is always marked
10990 external when it is declared in-class, even if an
10991 initializer is present. We mimic the non-template
10992 processing here. */
10993 DECL_EXTERNAL (r) = 1;
10995 register_specialization (r, gen_tmpl, argvec, false, hash);
10996 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10997 SET_DECL_IMPLICIT_INSTANTIATION (r);
10999 else if (!cp_unevaluated_operand)
11000 register_local_specialization (r, t);
11002 DECL_CHAIN (r) = NULL_TREE;
11004 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11005 /*flags=*/0,
11006 args, complain, in_decl);
11008 /* Preserve a typedef that names a type. */
11009 if (is_typedef_decl (r))
11011 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11012 set_underlying_type (r);
11015 layout_decl (r, 0);
11017 break;
11019 default:
11020 gcc_unreachable ();
11022 #undef RETURN
11024 out:
11025 /* Restore the file and line information. */
11026 input_location = saved_loc;
11028 return r;
11031 /* Substitute into the ARG_TYPES of a function type.
11032 If END is a TREE_CHAIN, leave it and any following types
11033 un-substituted. */
11035 static tree
11036 tsubst_arg_types (tree arg_types,
11037 tree args,
11038 tree end,
11039 tsubst_flags_t complain,
11040 tree in_decl)
11042 tree remaining_arg_types;
11043 tree type = NULL_TREE;
11044 int i = 1;
11045 tree expanded_args = NULL_TREE;
11046 tree default_arg;
11048 if (!arg_types || arg_types == void_list_node || arg_types == end)
11049 return arg_types;
11051 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11052 args, end, complain, in_decl);
11053 if (remaining_arg_types == error_mark_node)
11054 return error_mark_node;
11056 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11058 /* For a pack expansion, perform substitution on the
11059 entire expression. Later on, we'll handle the arguments
11060 one-by-one. */
11061 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11062 args, complain, in_decl);
11064 if (TREE_CODE (expanded_args) == TREE_VEC)
11065 /* So that we'll spin through the parameters, one by one. */
11066 i = TREE_VEC_LENGTH (expanded_args);
11067 else
11069 /* We only partially substituted into the parameter
11070 pack. Our type is TYPE_PACK_EXPANSION. */
11071 type = expanded_args;
11072 expanded_args = NULL_TREE;
11076 while (i > 0) {
11077 --i;
11079 if (expanded_args)
11080 type = TREE_VEC_ELT (expanded_args, i);
11081 else if (!type)
11082 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11084 if (type == error_mark_node)
11085 return error_mark_node;
11086 if (VOID_TYPE_P (type))
11088 if (complain & tf_error)
11090 error ("invalid parameter type %qT", type);
11091 if (in_decl)
11092 error ("in declaration %q+D", in_decl);
11094 return error_mark_node;
11096 /* DR 657. */
11097 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11098 return error_mark_node;
11100 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11101 top-level qualifiers as required. */
11102 type = cv_unqualified (type_decays_to (type));
11104 /* We do not substitute into default arguments here. The standard
11105 mandates that they be instantiated only when needed, which is
11106 done in build_over_call. */
11107 default_arg = TREE_PURPOSE (arg_types);
11109 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11111 /* We've instantiated a template before its default arguments
11112 have been parsed. This can happen for a nested template
11113 class, and is not an error unless we require the default
11114 argument in a call of this function. */
11115 remaining_arg_types =
11116 tree_cons (default_arg, type, remaining_arg_types);
11117 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11119 else
11120 remaining_arg_types =
11121 hash_tree_cons (default_arg, type, remaining_arg_types);
11124 return remaining_arg_types;
11127 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11128 *not* handle the exception-specification for FNTYPE, because the
11129 initial substitution of explicitly provided template parameters
11130 during argument deduction forbids substitution into the
11131 exception-specification:
11133 [temp.deduct]
11135 All references in the function type of the function template to the
11136 corresponding template parameters are replaced by the specified tem-
11137 plate argument values. If a substitution in a template parameter or
11138 in the function type of the function template results in an invalid
11139 type, type deduction fails. [Note: The equivalent substitution in
11140 exception specifications is done only when the function is instanti-
11141 ated, at which point a program is ill-formed if the substitution
11142 results in an invalid type.] */
11144 static tree
11145 tsubst_function_type (tree t,
11146 tree args,
11147 tsubst_flags_t complain,
11148 tree in_decl)
11150 tree return_type;
11151 tree arg_types;
11152 tree fntype;
11154 /* The TYPE_CONTEXT is not used for function/method types. */
11155 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11157 /* Substitute the return type. */
11158 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11159 if (return_type == error_mark_node)
11160 return error_mark_node;
11161 /* DR 486 clarifies that creation of a function type with an
11162 invalid return type is a deduction failure. */
11163 if (TREE_CODE (return_type) == ARRAY_TYPE
11164 || TREE_CODE (return_type) == FUNCTION_TYPE)
11166 if (complain & tf_error)
11168 if (TREE_CODE (return_type) == ARRAY_TYPE)
11169 error ("function returning an array");
11170 else
11171 error ("function returning a function");
11173 return error_mark_node;
11175 /* And DR 657. */
11176 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11177 return error_mark_node;
11179 /* Substitute the argument types. */
11180 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11181 complain, in_decl);
11182 if (arg_types == error_mark_node)
11183 return error_mark_node;
11185 /* Construct a new type node and return it. */
11186 if (TREE_CODE (t) == FUNCTION_TYPE)
11188 fntype = build_function_type (return_type, arg_types);
11189 fntype = apply_memfn_quals (fntype,
11190 type_memfn_quals (t),
11191 type_memfn_rqual (t));
11193 else
11195 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11196 /* Don't pick up extra function qualifiers from the basetype. */
11197 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11198 if (! MAYBE_CLASS_TYPE_P (r))
11200 /* [temp.deduct]
11202 Type deduction may fail for any of the following
11203 reasons:
11205 -- Attempting to create "pointer to member of T" when T
11206 is not a class type. */
11207 if (complain & tf_error)
11208 error ("creating pointer to member function of non-class type %qT",
11210 return error_mark_node;
11213 fntype = build_method_type_directly (r, return_type,
11214 TREE_CHAIN (arg_types));
11215 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11217 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11219 return fntype;
11222 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11223 ARGS into that specification, and return the substituted
11224 specification. If there is no specification, return NULL_TREE. */
11226 static tree
11227 tsubst_exception_specification (tree fntype,
11228 tree args,
11229 tsubst_flags_t complain,
11230 tree in_decl,
11231 bool defer_ok)
11233 tree specs;
11234 tree new_specs;
11236 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11237 new_specs = NULL_TREE;
11238 if (specs && TREE_PURPOSE (specs))
11240 /* A noexcept-specifier. */
11241 tree expr = TREE_PURPOSE (specs);
11242 if (TREE_CODE (expr) == INTEGER_CST)
11243 new_specs = expr;
11244 else if (defer_ok)
11246 /* Defer instantiation of noexcept-specifiers to avoid
11247 excessive instantiations (c++/49107). */
11248 new_specs = make_node (DEFERRED_NOEXCEPT);
11249 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11251 /* We already partially instantiated this member template,
11252 so combine the new args with the old. */
11253 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11254 = DEFERRED_NOEXCEPT_PATTERN (expr);
11255 DEFERRED_NOEXCEPT_ARGS (new_specs)
11256 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11258 else
11260 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11261 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11264 else
11265 new_specs = tsubst_copy_and_build
11266 (expr, args, complain, in_decl, /*function_p=*/false,
11267 /*integral_constant_expression_p=*/true);
11268 new_specs = build_noexcept_spec (new_specs, complain);
11270 else if (specs)
11272 if (! TREE_VALUE (specs))
11273 new_specs = specs;
11274 else
11275 while (specs)
11277 tree spec;
11278 int i, len = 1;
11279 tree expanded_specs = NULL_TREE;
11281 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11283 /* Expand the pack expansion type. */
11284 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11285 args, complain,
11286 in_decl);
11288 if (expanded_specs == error_mark_node)
11289 return error_mark_node;
11290 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11291 len = TREE_VEC_LENGTH (expanded_specs);
11292 else
11294 /* We're substituting into a member template, so
11295 we got a TYPE_PACK_EXPANSION back. Add that
11296 expansion and move on. */
11297 gcc_assert (TREE_CODE (expanded_specs)
11298 == TYPE_PACK_EXPANSION);
11299 new_specs = add_exception_specifier (new_specs,
11300 expanded_specs,
11301 complain);
11302 specs = TREE_CHAIN (specs);
11303 continue;
11307 for (i = 0; i < len; ++i)
11309 if (expanded_specs)
11310 spec = TREE_VEC_ELT (expanded_specs, i);
11311 else
11312 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11313 if (spec == error_mark_node)
11314 return spec;
11315 new_specs = add_exception_specifier (new_specs, spec,
11316 complain);
11319 specs = TREE_CHAIN (specs);
11322 return new_specs;
11325 /* Take the tree structure T and replace template parameters used
11326 therein with the argument vector ARGS. IN_DECL is an associated
11327 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11328 Issue error and warning messages under control of COMPLAIN. Note
11329 that we must be relatively non-tolerant of extensions here, in
11330 order to preserve conformance; if we allow substitutions that
11331 should not be allowed, we may allow argument deductions that should
11332 not succeed, and therefore report ambiguous overload situations
11333 where there are none. In theory, we could allow the substitution,
11334 but indicate that it should have failed, and allow our caller to
11335 make sure that the right thing happens, but we don't try to do this
11336 yet.
11338 This function is used for dealing with types, decls and the like;
11339 for expressions, use tsubst_expr or tsubst_copy. */
11341 tree
11342 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11344 enum tree_code code;
11345 tree type, r = NULL_TREE;
11347 if (t == NULL_TREE || t == error_mark_node
11348 || t == integer_type_node
11349 || t == void_type_node
11350 || t == char_type_node
11351 || t == unknown_type_node
11352 || TREE_CODE (t) == NAMESPACE_DECL
11353 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11354 return t;
11356 if (DECL_P (t))
11357 return tsubst_decl (t, args, complain);
11359 if (args == NULL_TREE)
11360 return t;
11362 code = TREE_CODE (t);
11364 if (code == IDENTIFIER_NODE)
11365 type = IDENTIFIER_TYPE_VALUE (t);
11366 else
11367 type = TREE_TYPE (t);
11369 gcc_assert (type != unknown_type_node);
11371 /* Reuse typedefs. We need to do this to handle dependent attributes,
11372 such as attribute aligned. */
11373 if (TYPE_P (t)
11374 && typedef_variant_p (t))
11376 tree decl = TYPE_NAME (t);
11378 if (alias_template_specialization_p (t))
11380 /* DECL represents an alias template and we want to
11381 instantiate it. */
11382 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11383 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11384 r = instantiate_alias_template (tmpl, gen_args, complain);
11386 else if (DECL_CLASS_SCOPE_P (decl)
11387 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11388 && uses_template_parms (DECL_CONTEXT (decl)))
11390 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11391 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11392 r = retrieve_specialization (tmpl, gen_args, 0);
11394 else if (DECL_FUNCTION_SCOPE_P (decl)
11395 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11396 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11397 r = retrieve_local_specialization (decl);
11398 else
11399 /* The typedef is from a non-template context. */
11400 return t;
11402 if (r)
11404 r = TREE_TYPE (r);
11405 r = cp_build_qualified_type_real
11406 (r, cp_type_quals (t) | cp_type_quals (r),
11407 complain | tf_ignore_bad_quals);
11408 return r;
11410 else
11412 /* We don't have an instantiation yet, so drop the typedef. */
11413 int quals = cp_type_quals (t);
11414 t = DECL_ORIGINAL_TYPE (decl);
11415 t = cp_build_qualified_type_real (t, quals,
11416 complain | tf_ignore_bad_quals);
11420 if (type
11421 && code != TYPENAME_TYPE
11422 && code != TEMPLATE_TYPE_PARM
11423 && code != IDENTIFIER_NODE
11424 && code != FUNCTION_TYPE
11425 && code != METHOD_TYPE)
11426 type = tsubst (type, args, complain, in_decl);
11427 if (type == error_mark_node)
11428 return error_mark_node;
11430 switch (code)
11432 case RECORD_TYPE:
11433 case UNION_TYPE:
11434 case ENUMERAL_TYPE:
11435 return tsubst_aggr_type (t, args, complain, in_decl,
11436 /*entering_scope=*/0);
11438 case ERROR_MARK:
11439 case IDENTIFIER_NODE:
11440 case VOID_TYPE:
11441 case REAL_TYPE:
11442 case COMPLEX_TYPE:
11443 case VECTOR_TYPE:
11444 case BOOLEAN_TYPE:
11445 case NULLPTR_TYPE:
11446 case LANG_TYPE:
11447 return t;
11449 case INTEGER_TYPE:
11450 if (t == integer_type_node)
11451 return t;
11453 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11454 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11455 return t;
11458 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11460 max = tsubst_expr (omax, args, complain, in_decl,
11461 /*integral_constant_expression_p=*/false);
11463 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11464 needed. */
11465 if (TREE_CODE (max) == NOP_EXPR
11466 && TREE_SIDE_EFFECTS (omax)
11467 && !TREE_TYPE (max))
11468 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11470 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11471 with TREE_SIDE_EFFECTS that indicates this is not an integral
11472 constant expression. */
11473 if (processing_template_decl
11474 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11476 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11477 TREE_SIDE_EFFECTS (max) = 1;
11480 return compute_array_index_type (NULL_TREE, max, complain);
11483 case TEMPLATE_TYPE_PARM:
11484 case TEMPLATE_TEMPLATE_PARM:
11485 case BOUND_TEMPLATE_TEMPLATE_PARM:
11486 case TEMPLATE_PARM_INDEX:
11488 int idx;
11489 int level;
11490 int levels;
11491 tree arg = NULL_TREE;
11493 r = NULL_TREE;
11495 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11496 template_parm_level_and_index (t, &level, &idx);
11498 levels = TMPL_ARGS_DEPTH (args);
11499 if (level <= levels)
11501 arg = TMPL_ARG (args, level, idx);
11503 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11505 /* See through ARGUMENT_PACK_SELECT arguments. */
11506 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11507 /* If the selected argument is an expansion E, that most
11508 likely means we were called from
11509 gen_elem_of_pack_expansion_instantiation during the
11510 substituting of pack an argument pack (which Ith
11511 element is a pack expansion, where I is
11512 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11513 In this case, the Ith element resulting from this
11514 substituting is going to be a pack expansion, which
11515 pattern is the pattern of E. Let's return the
11516 pattern of E, and
11517 gen_elem_of_pack_expansion_instantiation will
11518 build the resulting pack expansion from it. */
11519 if (PACK_EXPANSION_P (arg))
11520 arg = PACK_EXPANSION_PATTERN (arg);
11524 if (arg == error_mark_node)
11525 return error_mark_node;
11526 else if (arg != NULL_TREE)
11528 if (ARGUMENT_PACK_P (arg))
11529 /* If ARG is an argument pack, we don't actually want to
11530 perform a substitution here, because substitutions
11531 for argument packs are only done
11532 element-by-element. We can get to this point when
11533 substituting the type of a non-type template
11534 parameter pack, when that type actually contains
11535 template parameter packs from an outer template, e.g.,
11537 template<typename... Types> struct A {
11538 template<Types... Values> struct B { };
11539 }; */
11540 return t;
11542 if (code == TEMPLATE_TYPE_PARM)
11544 int quals;
11545 gcc_assert (TYPE_P (arg));
11547 quals = cp_type_quals (arg) | cp_type_quals (t);
11549 return cp_build_qualified_type_real
11550 (arg, quals, complain | tf_ignore_bad_quals);
11552 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11554 /* We are processing a type constructed from a
11555 template template parameter. */
11556 tree argvec = tsubst (TYPE_TI_ARGS (t),
11557 args, complain, in_decl);
11558 if (argvec == error_mark_node)
11559 return error_mark_node;
11561 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11562 || TREE_CODE (arg) == TEMPLATE_DECL
11563 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11565 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11566 /* Consider this code:
11568 template <template <class> class Template>
11569 struct Internal {
11570 template <class Arg> using Bind = Template<Arg>;
11573 template <template <class> class Template, class Arg>
11574 using Instantiate = Template<Arg>; //#0
11576 template <template <class> class Template,
11577 class Argument>
11578 using Bind =
11579 Instantiate<Internal<Template>::template Bind,
11580 Argument>; //#1
11582 When #1 is parsed, the
11583 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11584 parameter `Template' in #0 matches the
11585 UNBOUND_CLASS_TEMPLATE representing the argument
11586 `Internal<Template>::template Bind'; We then want
11587 to assemble the type `Bind<Argument>' that can't
11588 be fully created right now, because
11589 `Internal<Template>' not being complete, the Bind
11590 template cannot be looked up in that context. So
11591 we need to "store" `Bind<Argument>' for later
11592 when the context of Bind becomes complete. Let's
11593 store that in a TYPENAME_TYPE. */
11594 return make_typename_type (TYPE_CONTEXT (arg),
11595 build_nt (TEMPLATE_ID_EXPR,
11596 TYPE_IDENTIFIER (arg),
11597 argvec),
11598 typename_type,
11599 complain);
11601 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11602 are resolving nested-types in the signature of a
11603 member function templates. Otherwise ARG is a
11604 TEMPLATE_DECL and is the real template to be
11605 instantiated. */
11606 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11607 arg = TYPE_NAME (arg);
11609 r = lookup_template_class (arg,
11610 argvec, in_decl,
11611 DECL_CONTEXT (arg),
11612 /*entering_scope=*/0,
11613 complain);
11614 return cp_build_qualified_type_real
11615 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11617 else
11618 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11619 return convert_from_reference (unshare_expr (arg));
11622 if (level == 1)
11623 /* This can happen during the attempted tsubst'ing in
11624 unify. This means that we don't yet have any information
11625 about the template parameter in question. */
11626 return t;
11628 /* Early in template argument deduction substitution, we don't
11629 want to reduce the level of 'auto', or it will be confused
11630 with a normal template parm in subsequent deduction. */
11631 if (is_auto (t) && (complain & tf_partial))
11632 return t;
11634 /* If we get here, we must have been looking at a parm for a
11635 more deeply nested template. Make a new version of this
11636 template parameter, but with a lower level. */
11637 switch (code)
11639 case TEMPLATE_TYPE_PARM:
11640 case TEMPLATE_TEMPLATE_PARM:
11641 case BOUND_TEMPLATE_TEMPLATE_PARM:
11642 if (cp_type_quals (t))
11644 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11645 r = cp_build_qualified_type_real
11646 (r, cp_type_quals (t),
11647 complain | (code == TEMPLATE_TYPE_PARM
11648 ? tf_ignore_bad_quals : 0));
11650 else
11652 r = copy_type (t);
11653 TEMPLATE_TYPE_PARM_INDEX (r)
11654 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11655 r, levels, args, complain);
11656 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11657 TYPE_MAIN_VARIANT (r) = r;
11658 TYPE_POINTER_TO (r) = NULL_TREE;
11659 TYPE_REFERENCE_TO (r) = NULL_TREE;
11661 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11662 /* We have reduced the level of the template
11663 template parameter, but not the levels of its
11664 template parameters, so canonical_type_parameter
11665 will not be able to find the canonical template
11666 template parameter for this level. Thus, we
11667 require structural equality checking to compare
11668 TEMPLATE_TEMPLATE_PARMs. */
11669 SET_TYPE_STRUCTURAL_EQUALITY (r);
11670 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11671 SET_TYPE_STRUCTURAL_EQUALITY (r);
11672 else
11673 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11675 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11677 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11678 complain, in_decl);
11679 if (argvec == error_mark_node)
11680 return error_mark_node;
11682 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11683 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11686 break;
11688 case TEMPLATE_PARM_INDEX:
11689 r = reduce_template_parm_level (t, type, levels, args, complain);
11690 break;
11692 default:
11693 gcc_unreachable ();
11696 return r;
11699 case TREE_LIST:
11701 tree purpose, value, chain;
11703 if (t == void_list_node)
11704 return t;
11706 purpose = TREE_PURPOSE (t);
11707 if (purpose)
11709 purpose = tsubst (purpose, args, complain, in_decl);
11710 if (purpose == error_mark_node)
11711 return error_mark_node;
11713 value = TREE_VALUE (t);
11714 if (value)
11716 value = tsubst (value, args, complain, in_decl);
11717 if (value == error_mark_node)
11718 return error_mark_node;
11720 chain = TREE_CHAIN (t);
11721 if (chain && chain != void_type_node)
11723 chain = tsubst (chain, args, complain, in_decl);
11724 if (chain == error_mark_node)
11725 return error_mark_node;
11727 if (purpose == TREE_PURPOSE (t)
11728 && value == TREE_VALUE (t)
11729 && chain == TREE_CHAIN (t))
11730 return t;
11731 return hash_tree_cons (purpose, value, chain);
11734 case TREE_BINFO:
11735 /* We should never be tsubsting a binfo. */
11736 gcc_unreachable ();
11738 case TREE_VEC:
11739 /* A vector of template arguments. */
11740 gcc_assert (!type);
11741 return tsubst_template_args (t, args, complain, in_decl);
11743 case POINTER_TYPE:
11744 case REFERENCE_TYPE:
11746 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11747 return t;
11749 /* [temp.deduct]
11751 Type deduction may fail for any of the following
11752 reasons:
11754 -- Attempting to create a pointer to reference type.
11755 -- Attempting to create a reference to a reference type or
11756 a reference to void.
11758 Core issue 106 says that creating a reference to a reference
11759 during instantiation is no longer a cause for failure. We
11760 only enforce this check in strict C++98 mode. */
11761 if ((TREE_CODE (type) == REFERENCE_TYPE
11762 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11763 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11765 static location_t last_loc;
11767 /* We keep track of the last time we issued this error
11768 message to avoid spewing a ton of messages during a
11769 single bad template instantiation. */
11770 if (complain & tf_error
11771 && last_loc != input_location)
11773 if (VOID_TYPE_P (type))
11774 error ("forming reference to void");
11775 else if (code == POINTER_TYPE)
11776 error ("forming pointer to reference type %qT", type);
11777 else
11778 error ("forming reference to reference type %qT", type);
11779 last_loc = input_location;
11782 return error_mark_node;
11784 else if (TREE_CODE (type) == FUNCTION_TYPE
11785 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11786 || type_memfn_rqual (type) != REF_QUAL_NONE))
11788 if (complain & tf_error)
11790 if (code == POINTER_TYPE)
11791 error ("forming pointer to qualified function type %qT",
11792 type);
11793 else
11794 error ("forming reference to qualified function type %qT",
11795 type);
11797 return error_mark_node;
11799 else if (code == POINTER_TYPE)
11801 r = build_pointer_type (type);
11802 if (TREE_CODE (type) == METHOD_TYPE)
11803 r = build_ptrmemfunc_type (r);
11805 else if (TREE_CODE (type) == REFERENCE_TYPE)
11806 /* In C++0x, during template argument substitution, when there is an
11807 attempt to create a reference to a reference type, reference
11808 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11810 "If a template-argument for a template-parameter T names a type
11811 that is a reference to a type A, an attempt to create the type
11812 'lvalue reference to cv T' creates the type 'lvalue reference to
11813 A,' while an attempt to create the type type rvalue reference to
11814 cv T' creates the type T"
11816 r = cp_build_reference_type
11817 (TREE_TYPE (type),
11818 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11819 else
11820 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11821 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11823 if (cxx_dialect >= cxx1y
11824 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11825 && array_of_runtime_bound_p (type))
11827 if (complain & tf_warning_or_error)
11828 pedwarn
11829 (input_location, OPT_Wvla,
11830 code == REFERENCE_TYPE
11831 ? G_("cannot declare reference to array of runtime bound")
11832 : G_("cannot declare pointer to array of runtime bound"));
11833 else
11834 r = error_mark_node;
11837 if (r != error_mark_node)
11838 /* Will this ever be needed for TYPE_..._TO values? */
11839 layout_type (r);
11841 return r;
11843 case OFFSET_TYPE:
11845 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11846 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11848 /* [temp.deduct]
11850 Type deduction may fail for any of the following
11851 reasons:
11853 -- Attempting to create "pointer to member of T" when T
11854 is not a class type. */
11855 if (complain & tf_error)
11856 error ("creating pointer to member of non-class type %qT", r);
11857 return error_mark_node;
11859 if (TREE_CODE (type) == REFERENCE_TYPE)
11861 if (complain & tf_error)
11862 error ("creating pointer to member reference type %qT", type);
11863 return error_mark_node;
11865 if (VOID_TYPE_P (type))
11867 if (complain & tf_error)
11868 error ("creating pointer to member of type void");
11869 return error_mark_node;
11871 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11872 if (TREE_CODE (type) == FUNCTION_TYPE)
11874 /* The type of the implicit object parameter gets its
11875 cv-qualifiers from the FUNCTION_TYPE. */
11876 tree memptr;
11877 tree method_type
11878 = build_memfn_type (type, r, type_memfn_quals (type),
11879 type_memfn_rqual (type));
11880 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11881 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11882 complain);
11884 else
11885 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11886 cp_type_quals (t),
11887 complain);
11889 case FUNCTION_TYPE:
11890 case METHOD_TYPE:
11892 tree fntype;
11893 tree specs;
11894 fntype = tsubst_function_type (t, args, complain, in_decl);
11895 if (fntype == error_mark_node)
11896 return error_mark_node;
11898 /* Substitute the exception specification. */
11899 specs = tsubst_exception_specification (t, args, complain,
11900 in_decl, /*defer_ok*/true);
11901 if (specs == error_mark_node)
11902 return error_mark_node;
11903 if (specs)
11904 fntype = build_exception_variant (fntype, specs);
11905 return fntype;
11907 case ARRAY_TYPE:
11909 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11910 if (domain == error_mark_node)
11911 return error_mark_node;
11913 /* As an optimization, we avoid regenerating the array type if
11914 it will obviously be the same as T. */
11915 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11916 return t;
11918 /* These checks should match the ones in grokdeclarator.
11920 [temp.deduct]
11922 The deduction may fail for any of the following reasons:
11924 -- Attempting to create an array with an element type that
11925 is void, a function type, or a reference type, or [DR337]
11926 an abstract class type. */
11927 if (VOID_TYPE_P (type)
11928 || TREE_CODE (type) == FUNCTION_TYPE
11929 || TREE_CODE (type) == REFERENCE_TYPE)
11931 if (complain & tf_error)
11932 error ("creating array of %qT", type);
11933 return error_mark_node;
11936 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11937 return error_mark_node;
11939 r = build_cplus_array_type (type, domain);
11941 if (TYPE_USER_ALIGN (t))
11943 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11944 TYPE_USER_ALIGN (r) = 1;
11947 return r;
11950 case TYPENAME_TYPE:
11952 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11953 in_decl, /*entering_scope=*/1);
11954 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11955 complain, in_decl);
11957 if (ctx == error_mark_node || f == error_mark_node)
11958 return error_mark_node;
11960 if (!MAYBE_CLASS_TYPE_P (ctx))
11962 if (complain & tf_error)
11963 error ("%qT is not a class, struct, or union type", ctx);
11964 return error_mark_node;
11966 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11968 /* Normally, make_typename_type does not require that the CTX
11969 have complete type in order to allow things like:
11971 template <class T> struct S { typename S<T>::X Y; };
11973 But, such constructs have already been resolved by this
11974 point, so here CTX really should have complete type, unless
11975 it's a partial instantiation. */
11976 ctx = complete_type (ctx);
11977 if (!COMPLETE_TYPE_P (ctx))
11979 if (complain & tf_error)
11980 cxx_incomplete_type_error (NULL_TREE, ctx);
11981 return error_mark_node;
11985 f = make_typename_type (ctx, f, typename_type,
11986 complain | tf_keep_type_decl);
11987 if (f == error_mark_node)
11988 return f;
11989 if (TREE_CODE (f) == TYPE_DECL)
11991 complain |= tf_ignore_bad_quals;
11992 f = TREE_TYPE (f);
11995 if (TREE_CODE (f) != TYPENAME_TYPE)
11997 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11999 if (complain & tf_error)
12000 error ("%qT resolves to %qT, which is not an enumeration type",
12001 t, f);
12002 else
12003 return error_mark_node;
12005 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12007 if (complain & tf_error)
12008 error ("%qT resolves to %qT, which is is not a class type",
12009 t, f);
12010 else
12011 return error_mark_node;
12015 return cp_build_qualified_type_real
12016 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12019 case UNBOUND_CLASS_TEMPLATE:
12021 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12022 in_decl, /*entering_scope=*/1);
12023 tree name = TYPE_IDENTIFIER (t);
12024 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12026 if (ctx == error_mark_node || name == error_mark_node)
12027 return error_mark_node;
12029 if (parm_list)
12030 parm_list = tsubst_template_parms (parm_list, args, complain);
12031 return make_unbound_class_template (ctx, name, parm_list, complain);
12034 case TYPEOF_TYPE:
12036 tree type;
12038 ++cp_unevaluated_operand;
12039 ++c_inhibit_evaluation_warnings;
12041 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12042 complain, in_decl,
12043 /*integral_constant_expression_p=*/false);
12045 --cp_unevaluated_operand;
12046 --c_inhibit_evaluation_warnings;
12048 type = finish_typeof (type);
12049 return cp_build_qualified_type_real (type,
12050 cp_type_quals (t)
12051 | cp_type_quals (type),
12052 complain);
12055 case DECLTYPE_TYPE:
12057 tree type;
12059 ++cp_unevaluated_operand;
12060 ++c_inhibit_evaluation_warnings;
12062 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12063 complain|tf_decltype, in_decl,
12064 /*function_p*/false,
12065 /*integral_constant_expression*/false);
12067 --cp_unevaluated_operand;
12068 --c_inhibit_evaluation_warnings;
12070 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12071 type = lambda_capture_field_type (type,
12072 DECLTYPE_FOR_INIT_CAPTURE (t));
12073 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12074 type = lambda_proxy_type (type);
12075 else
12077 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12078 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12079 && EXPR_P (type))
12080 /* In a template ~id could be either a complement expression
12081 or an unqualified-id naming a destructor; if instantiating
12082 it produces an expression, it's not an id-expression or
12083 member access. */
12084 id = false;
12085 type = finish_decltype_type (type, id, complain);
12087 return cp_build_qualified_type_real (type,
12088 cp_type_quals (t)
12089 | cp_type_quals (type),
12090 complain);
12093 case UNDERLYING_TYPE:
12095 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12096 complain, in_decl);
12097 return finish_underlying_type (type);
12100 case TYPE_ARGUMENT_PACK:
12101 case NONTYPE_ARGUMENT_PACK:
12103 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12104 tree packed_out =
12105 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12106 args,
12107 complain,
12108 in_decl);
12109 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12111 /* For template nontype argument packs, also substitute into
12112 the type. */
12113 if (code == NONTYPE_ARGUMENT_PACK)
12114 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12116 return r;
12118 break;
12120 case INTEGER_CST:
12121 case REAL_CST:
12122 case STRING_CST:
12123 case PLUS_EXPR:
12124 case MINUS_EXPR:
12125 case NEGATE_EXPR:
12126 case NOP_EXPR:
12127 case INDIRECT_REF:
12128 case ADDR_EXPR:
12129 case CALL_EXPR:
12130 case ARRAY_REF:
12131 case SCOPE_REF:
12132 /* We should use one of the expression tsubsts for these codes. */
12133 gcc_unreachable ();
12135 default:
12136 sorry ("use of %qs in template", get_tree_code_name (code));
12137 return error_mark_node;
12141 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12142 type of the expression on the left-hand side of the "." or "->"
12143 operator. */
12145 static tree
12146 tsubst_baselink (tree baselink, tree object_type,
12147 tree args, tsubst_flags_t complain, tree in_decl)
12149 tree name;
12150 tree qualifying_scope;
12151 tree fns;
12152 tree optype;
12153 tree template_args = 0;
12154 bool template_id_p = false;
12155 bool qualified = BASELINK_QUALIFIED_P (baselink);
12157 /* A baselink indicates a function from a base class. Both the
12158 BASELINK_ACCESS_BINFO and the base class referenced may
12159 indicate bases of the template class, rather than the
12160 instantiated class. In addition, lookups that were not
12161 ambiguous before may be ambiguous now. Therefore, we perform
12162 the lookup again. */
12163 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12164 qualifying_scope = tsubst (qualifying_scope, args,
12165 complain, in_decl);
12166 fns = BASELINK_FUNCTIONS (baselink);
12167 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12168 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12170 template_id_p = true;
12171 template_args = TREE_OPERAND (fns, 1);
12172 fns = TREE_OPERAND (fns, 0);
12173 if (template_args)
12174 template_args = tsubst_template_args (template_args, args,
12175 complain, in_decl);
12177 name = DECL_NAME (get_first_fn (fns));
12178 if (IDENTIFIER_TYPENAME_P (name))
12179 name = mangle_conv_op_name_for_type (optype);
12180 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12181 if (!baselink)
12182 return error_mark_node;
12184 /* If lookup found a single function, mark it as used at this
12185 point. (If it lookup found multiple functions the one selected
12186 later by overload resolution will be marked as used at that
12187 point.) */
12188 if (BASELINK_P (baselink))
12189 fns = BASELINK_FUNCTIONS (baselink);
12190 if (!template_id_p && !really_overloaded_fn (fns))
12191 mark_used (OVL_CURRENT (fns));
12193 /* Add back the template arguments, if present. */
12194 if (BASELINK_P (baselink) && template_id_p)
12195 BASELINK_FUNCTIONS (baselink)
12196 = build_nt (TEMPLATE_ID_EXPR,
12197 BASELINK_FUNCTIONS (baselink),
12198 template_args);
12199 /* Update the conversion operator type. */
12200 BASELINK_OPTYPE (baselink) = optype;
12202 if (!object_type)
12203 object_type = current_class_type;
12205 if (qualified)
12206 baselink = adjust_result_of_qualified_name_lookup (baselink,
12207 qualifying_scope,
12208 object_type);
12209 return baselink;
12212 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12213 true if the qualified-id will be a postfix-expression in-and-of
12214 itself; false if more of the postfix-expression follows the
12215 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12216 of "&". */
12218 static tree
12219 tsubst_qualified_id (tree qualified_id, tree args,
12220 tsubst_flags_t complain, tree in_decl,
12221 bool done, bool address_p)
12223 tree expr;
12224 tree scope;
12225 tree name;
12226 bool is_template;
12227 tree template_args;
12228 location_t loc = UNKNOWN_LOCATION;
12230 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12232 /* Figure out what name to look up. */
12233 name = TREE_OPERAND (qualified_id, 1);
12234 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12236 is_template = true;
12237 loc = EXPR_LOCATION (name);
12238 template_args = TREE_OPERAND (name, 1);
12239 if (template_args)
12240 template_args = tsubst_template_args (template_args, args,
12241 complain, in_decl);
12242 name = TREE_OPERAND (name, 0);
12244 else
12246 is_template = false;
12247 template_args = NULL_TREE;
12250 /* Substitute into the qualifying scope. When there are no ARGS, we
12251 are just trying to simplify a non-dependent expression. In that
12252 case the qualifying scope may be dependent, and, in any case,
12253 substituting will not help. */
12254 scope = TREE_OPERAND (qualified_id, 0);
12255 if (args)
12257 scope = tsubst (scope, args, complain, in_decl);
12258 expr = tsubst_copy (name, args, complain, in_decl);
12260 else
12261 expr = name;
12263 if (dependent_scope_p (scope))
12265 if (is_template)
12266 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12267 return build_qualified_name (NULL_TREE, scope, expr,
12268 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12271 if (!BASELINK_P (name) && !DECL_P (expr))
12273 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12275 /* A BIT_NOT_EXPR is used to represent a destructor. */
12276 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12278 error ("qualifying type %qT does not match destructor name ~%qT",
12279 scope, TREE_OPERAND (expr, 0));
12280 expr = error_mark_node;
12282 else
12283 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12284 /*is_type_p=*/0, false);
12286 else
12287 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12288 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12289 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12291 if (complain & tf_error)
12293 error ("dependent-name %qE is parsed as a non-type, but "
12294 "instantiation yields a type", qualified_id);
12295 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12297 return error_mark_node;
12301 if (DECL_P (expr))
12303 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12304 scope);
12305 /* Remember that there was a reference to this entity. */
12306 mark_used (expr);
12309 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12311 if (complain & tf_error)
12312 qualified_name_lookup_error (scope,
12313 TREE_OPERAND (qualified_id, 1),
12314 expr, input_location);
12315 return error_mark_node;
12318 if (is_template)
12319 expr = lookup_template_function (expr, template_args);
12321 if (expr == error_mark_node && complain & tf_error)
12322 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12323 expr, input_location);
12324 else if (TYPE_P (scope))
12326 expr = (adjust_result_of_qualified_name_lookup
12327 (expr, scope, current_nonlambda_class_type ()));
12328 expr = (finish_qualified_id_expr
12329 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12330 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12331 /*template_arg_p=*/false, complain));
12334 /* Expressions do not generally have reference type. */
12335 if (TREE_CODE (expr) != SCOPE_REF
12336 /* However, if we're about to form a pointer-to-member, we just
12337 want the referenced member referenced. */
12338 && TREE_CODE (expr) != OFFSET_REF)
12339 expr = convert_from_reference (expr);
12341 return expr;
12344 /* Like tsubst, but deals with expressions. This function just replaces
12345 template parms; to finish processing the resultant expression, use
12346 tsubst_copy_and_build or tsubst_expr. */
12348 static tree
12349 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12351 enum tree_code code;
12352 tree r;
12354 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12355 return t;
12357 code = TREE_CODE (t);
12359 switch (code)
12361 case PARM_DECL:
12362 r = retrieve_local_specialization (t);
12364 if (r == NULL_TREE)
12366 /* We get here for a use of 'this' in an NSDMI. */
12367 if (DECL_NAME (t) == this_identifier
12368 && at_function_scope_p ()
12369 && DECL_CONSTRUCTOR_P (current_function_decl))
12370 return current_class_ptr;
12372 /* This can happen for a parameter name used later in a function
12373 declaration (such as in a late-specified return type). Just
12374 make a dummy decl, since it's only used for its type. */
12375 gcc_assert (cp_unevaluated_operand != 0);
12376 r = tsubst_decl (t, args, complain);
12377 /* Give it the template pattern as its context; its true context
12378 hasn't been instantiated yet and this is good enough for
12379 mangling. */
12380 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12383 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12384 r = ARGUMENT_PACK_SELECT_ARG (r);
12385 mark_used (r);
12386 return r;
12388 case CONST_DECL:
12390 tree enum_type;
12391 tree v;
12393 if (DECL_TEMPLATE_PARM_P (t))
12394 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12395 /* There is no need to substitute into namespace-scope
12396 enumerators. */
12397 if (DECL_NAMESPACE_SCOPE_P (t))
12398 return t;
12399 /* If ARGS is NULL, then T is known to be non-dependent. */
12400 if (args == NULL_TREE)
12401 return integral_constant_value (t);
12403 /* Unfortunately, we cannot just call lookup_name here.
12404 Consider:
12406 template <int I> int f() {
12407 enum E { a = I };
12408 struct S { void g() { E e = a; } };
12411 When we instantiate f<7>::S::g(), say, lookup_name is not
12412 clever enough to find f<7>::a. */
12413 enum_type
12414 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12415 /*entering_scope=*/0);
12417 for (v = TYPE_VALUES (enum_type);
12418 v != NULL_TREE;
12419 v = TREE_CHAIN (v))
12420 if (TREE_PURPOSE (v) == DECL_NAME (t))
12421 return TREE_VALUE (v);
12423 /* We didn't find the name. That should never happen; if
12424 name-lookup found it during preliminary parsing, we
12425 should find it again here during instantiation. */
12426 gcc_unreachable ();
12428 return t;
12430 case FIELD_DECL:
12431 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12433 /* Check for a local specialization set up by
12434 tsubst_pack_expansion. */
12435 tree r = retrieve_local_specialization (t);
12436 if (r)
12438 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12439 r = ARGUMENT_PACK_SELECT_ARG (r);
12440 return r;
12443 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12444 tsubst_decl put in the hash table. */
12445 return retrieve_specialization (t, args, 0);
12448 if (DECL_CONTEXT (t))
12450 tree ctx;
12452 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12453 /*entering_scope=*/1);
12454 if (ctx != DECL_CONTEXT (t))
12456 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12457 if (!r)
12459 if (complain & tf_error)
12460 error ("using invalid field %qD", t);
12461 return error_mark_node;
12463 return r;
12467 return t;
12469 case VAR_DECL:
12470 case FUNCTION_DECL:
12471 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12472 r = tsubst (t, args, complain, in_decl);
12473 else if (local_variable_p (t))
12475 r = retrieve_local_specialization (t);
12476 if (r == NULL_TREE)
12478 if (DECL_ANON_UNION_VAR_P (t))
12480 /* Just use name lookup to find a member alias for an
12481 anonymous union, but then add it to the hash table. */
12482 r = lookup_name (DECL_NAME (t));
12483 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12484 register_local_specialization (r, t);
12486 else
12488 /* This can happen for a variable used in a late-specified
12489 return type of a local lambda. Just make a dummy decl
12490 since it's only used for its type. */
12491 if (cp_unevaluated_operand)
12492 return tsubst_decl (t, args, complain);
12493 gcc_assert (errorcount || sorrycount);
12494 return error_mark_node;
12498 else
12499 r = t;
12500 mark_used (r);
12501 return r;
12503 case NAMESPACE_DECL:
12504 return t;
12506 case OVERLOAD:
12507 /* An OVERLOAD will always be a non-dependent overload set; an
12508 overload set from function scope will just be represented with an
12509 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12510 gcc_assert (!uses_template_parms (t));
12511 return t;
12513 case BASELINK:
12514 return tsubst_baselink (t, current_nonlambda_class_type (),
12515 args, complain, in_decl);
12517 case TEMPLATE_DECL:
12518 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12519 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12520 args, complain, in_decl);
12521 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12522 return tsubst (t, args, complain, in_decl);
12523 else if (DECL_CLASS_SCOPE_P (t)
12524 && uses_template_parms (DECL_CONTEXT (t)))
12526 /* Template template argument like the following example need
12527 special treatment:
12529 template <template <class> class TT> struct C {};
12530 template <class T> struct D {
12531 template <class U> struct E {};
12532 C<E> c; // #1
12534 D<int> d; // #2
12536 We are processing the template argument `E' in #1 for
12537 the template instantiation #2. Originally, `E' is a
12538 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12539 have to substitute this with one having context `D<int>'. */
12541 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12542 return lookup_field (context, DECL_NAME(t), 0, false);
12544 else
12545 /* Ordinary template template argument. */
12546 return t;
12548 case CAST_EXPR:
12549 case REINTERPRET_CAST_EXPR:
12550 case CONST_CAST_EXPR:
12551 case STATIC_CAST_EXPR:
12552 case DYNAMIC_CAST_EXPR:
12553 case IMPLICIT_CONV_EXPR:
12554 case CONVERT_EXPR:
12555 case NOP_EXPR:
12556 return build1
12557 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12558 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12560 case SIZEOF_EXPR:
12561 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12564 tree expanded, op = TREE_OPERAND (t, 0);
12565 int len = 0;
12567 if (SIZEOF_EXPR_TYPE_P (t))
12568 op = TREE_TYPE (op);
12570 ++cp_unevaluated_operand;
12571 ++c_inhibit_evaluation_warnings;
12572 /* We only want to compute the number of arguments. */
12573 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12574 --cp_unevaluated_operand;
12575 --c_inhibit_evaluation_warnings;
12577 if (TREE_CODE (expanded) == TREE_VEC)
12578 len = TREE_VEC_LENGTH (expanded);
12580 if (expanded == error_mark_node)
12581 return error_mark_node;
12582 else if (PACK_EXPANSION_P (expanded)
12583 || (TREE_CODE (expanded) == TREE_VEC
12584 && len > 0
12585 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12587 if (TREE_CODE (expanded) == TREE_VEC)
12588 expanded = TREE_VEC_ELT (expanded, len - 1);
12590 if (TYPE_P (expanded))
12591 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12592 complain & tf_error);
12593 else
12594 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12595 complain & tf_error);
12597 else
12598 return build_int_cst (size_type_node, len);
12600 if (SIZEOF_EXPR_TYPE_P (t))
12602 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12603 args, complain, in_decl);
12604 r = build1 (NOP_EXPR, r, error_mark_node);
12605 r = build1 (SIZEOF_EXPR,
12606 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12607 SIZEOF_EXPR_TYPE_P (r) = 1;
12608 return r;
12610 /* Fall through */
12612 case INDIRECT_REF:
12613 case NEGATE_EXPR:
12614 case TRUTH_NOT_EXPR:
12615 case BIT_NOT_EXPR:
12616 case ADDR_EXPR:
12617 case UNARY_PLUS_EXPR: /* Unary + */
12618 case ALIGNOF_EXPR:
12619 case AT_ENCODE_EXPR:
12620 case ARROW_EXPR:
12621 case THROW_EXPR:
12622 case TYPEID_EXPR:
12623 case REALPART_EXPR:
12624 case IMAGPART_EXPR:
12625 case PAREN_EXPR:
12626 return build1
12627 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12628 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12630 case COMPONENT_REF:
12632 tree object;
12633 tree name;
12635 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12636 name = TREE_OPERAND (t, 1);
12637 if (TREE_CODE (name) == BIT_NOT_EXPR)
12639 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12640 complain, in_decl);
12641 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12643 else if (TREE_CODE (name) == SCOPE_REF
12644 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12646 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12647 complain, in_decl);
12648 name = TREE_OPERAND (name, 1);
12649 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12650 complain, in_decl);
12651 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12652 name = build_qualified_name (/*type=*/NULL_TREE,
12653 base, name,
12654 /*template_p=*/false);
12656 else if (BASELINK_P (name))
12657 name = tsubst_baselink (name,
12658 non_reference (TREE_TYPE (object)),
12659 args, complain,
12660 in_decl);
12661 else
12662 name = tsubst_copy (name, args, complain, in_decl);
12663 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12666 case PLUS_EXPR:
12667 case MINUS_EXPR:
12668 case MULT_EXPR:
12669 case TRUNC_DIV_EXPR:
12670 case CEIL_DIV_EXPR:
12671 case FLOOR_DIV_EXPR:
12672 case ROUND_DIV_EXPR:
12673 case EXACT_DIV_EXPR:
12674 case BIT_AND_EXPR:
12675 case BIT_IOR_EXPR:
12676 case BIT_XOR_EXPR:
12677 case TRUNC_MOD_EXPR:
12678 case FLOOR_MOD_EXPR:
12679 case TRUTH_ANDIF_EXPR:
12680 case TRUTH_ORIF_EXPR:
12681 case TRUTH_AND_EXPR:
12682 case TRUTH_OR_EXPR:
12683 case RSHIFT_EXPR:
12684 case LSHIFT_EXPR:
12685 case RROTATE_EXPR:
12686 case LROTATE_EXPR:
12687 case EQ_EXPR:
12688 case NE_EXPR:
12689 case MAX_EXPR:
12690 case MIN_EXPR:
12691 case LE_EXPR:
12692 case GE_EXPR:
12693 case LT_EXPR:
12694 case GT_EXPR:
12695 case COMPOUND_EXPR:
12696 case DOTSTAR_EXPR:
12697 case MEMBER_REF:
12698 case PREDECREMENT_EXPR:
12699 case PREINCREMENT_EXPR:
12700 case POSTDECREMENT_EXPR:
12701 case POSTINCREMENT_EXPR:
12702 return build_nt
12703 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12704 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12706 case SCOPE_REF:
12707 return build_qualified_name (/*type=*/NULL_TREE,
12708 tsubst_copy (TREE_OPERAND (t, 0),
12709 args, complain, in_decl),
12710 tsubst_copy (TREE_OPERAND (t, 1),
12711 args, complain, in_decl),
12712 QUALIFIED_NAME_IS_TEMPLATE (t));
12714 case ARRAY_REF:
12715 return build_nt
12716 (ARRAY_REF,
12717 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12718 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12719 NULL_TREE, NULL_TREE);
12721 case CALL_EXPR:
12723 int n = VL_EXP_OPERAND_LENGTH (t);
12724 tree result = build_vl_exp (CALL_EXPR, n);
12725 int i;
12726 for (i = 0; i < n; i++)
12727 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12728 complain, in_decl);
12729 return result;
12732 case COND_EXPR:
12733 case MODOP_EXPR:
12734 case PSEUDO_DTOR_EXPR:
12735 case VEC_PERM_EXPR:
12737 r = build_nt
12738 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12739 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12740 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12741 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12742 return r;
12745 case NEW_EXPR:
12747 r = build_nt
12748 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12749 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12750 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12751 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12752 return r;
12755 case DELETE_EXPR:
12757 r = build_nt
12758 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12759 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12760 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12761 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12762 return r;
12765 case TEMPLATE_ID_EXPR:
12767 /* Substituted template arguments */
12768 tree fn = TREE_OPERAND (t, 0);
12769 tree targs = TREE_OPERAND (t, 1);
12771 fn = tsubst_copy (fn, args, complain, in_decl);
12772 if (targs)
12773 targs = tsubst_template_args (targs, args, complain, in_decl);
12775 return lookup_template_function (fn, targs);
12778 case TREE_LIST:
12780 tree purpose, value, chain;
12782 if (t == void_list_node)
12783 return t;
12785 purpose = TREE_PURPOSE (t);
12786 if (purpose)
12787 purpose = tsubst_copy (purpose, args, complain, in_decl);
12788 value = TREE_VALUE (t);
12789 if (value)
12790 value = tsubst_copy (value, args, complain, in_decl);
12791 chain = TREE_CHAIN (t);
12792 if (chain && chain != void_type_node)
12793 chain = tsubst_copy (chain, args, complain, in_decl);
12794 if (purpose == TREE_PURPOSE (t)
12795 && value == TREE_VALUE (t)
12796 && chain == TREE_CHAIN (t))
12797 return t;
12798 return tree_cons (purpose, value, chain);
12801 case RECORD_TYPE:
12802 case UNION_TYPE:
12803 case ENUMERAL_TYPE:
12804 case INTEGER_TYPE:
12805 case TEMPLATE_TYPE_PARM:
12806 case TEMPLATE_TEMPLATE_PARM:
12807 case BOUND_TEMPLATE_TEMPLATE_PARM:
12808 case TEMPLATE_PARM_INDEX:
12809 case POINTER_TYPE:
12810 case REFERENCE_TYPE:
12811 case OFFSET_TYPE:
12812 case FUNCTION_TYPE:
12813 case METHOD_TYPE:
12814 case ARRAY_TYPE:
12815 case TYPENAME_TYPE:
12816 case UNBOUND_CLASS_TEMPLATE:
12817 case TYPEOF_TYPE:
12818 case DECLTYPE_TYPE:
12819 case TYPE_DECL:
12820 return tsubst (t, args, complain, in_decl);
12822 case USING_DECL:
12823 t = DECL_NAME (t);
12824 /* Fall through. */
12825 case IDENTIFIER_NODE:
12826 if (IDENTIFIER_TYPENAME_P (t))
12828 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12829 return mangle_conv_op_name_for_type (new_type);
12831 else
12832 return t;
12834 case CONSTRUCTOR:
12835 /* This is handled by tsubst_copy_and_build. */
12836 gcc_unreachable ();
12838 case VA_ARG_EXPR:
12839 return build_x_va_arg (EXPR_LOCATION (t),
12840 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12841 in_decl),
12842 tsubst (TREE_TYPE (t), args, complain, in_decl));
12844 case CLEANUP_POINT_EXPR:
12845 /* We shouldn't have built any of these during initial template
12846 generation. Instead, they should be built during instantiation
12847 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12848 gcc_unreachable ();
12850 case OFFSET_REF:
12851 r = build2
12852 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12853 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12854 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12855 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12856 mark_used (TREE_OPERAND (r, 1));
12857 return r;
12859 case EXPR_PACK_EXPANSION:
12860 error ("invalid use of pack expansion expression");
12861 return error_mark_node;
12863 case NONTYPE_ARGUMENT_PACK:
12864 error ("use %<...%> to expand argument pack");
12865 return error_mark_node;
12867 case INTEGER_CST:
12868 case REAL_CST:
12869 case STRING_CST:
12870 case COMPLEX_CST:
12872 /* Instantiate any typedefs in the type. */
12873 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12874 r = fold_convert (type, t);
12875 gcc_assert (TREE_CODE (r) == code);
12876 return r;
12879 case PTRMEM_CST:
12880 /* These can sometimes show up in a partial instantiation, but never
12881 involve template parms. */
12882 gcc_assert (!uses_template_parms (t));
12883 return t;
12885 default:
12886 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12887 gcc_checking_assert (false);
12888 return t;
12892 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12894 static tree
12895 tsubst_omp_clauses (tree clauses, bool declare_simd,
12896 tree args, tsubst_flags_t complain, tree in_decl)
12898 tree new_clauses = NULL, nc, oc;
12900 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12902 nc = copy_node (oc);
12903 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12904 new_clauses = nc;
12906 switch (OMP_CLAUSE_CODE (nc))
12908 case OMP_CLAUSE_LASTPRIVATE:
12909 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12911 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12912 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12913 in_decl, /*integral_constant_expression_p=*/false);
12914 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12915 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12917 /* FALLTHRU */
12918 case OMP_CLAUSE_PRIVATE:
12919 case OMP_CLAUSE_SHARED:
12920 case OMP_CLAUSE_FIRSTPRIVATE:
12921 case OMP_CLAUSE_COPYIN:
12922 case OMP_CLAUSE_COPYPRIVATE:
12923 case OMP_CLAUSE_IF:
12924 case OMP_CLAUSE_NUM_THREADS:
12925 case OMP_CLAUSE_SCHEDULE:
12926 case OMP_CLAUSE_COLLAPSE:
12927 case OMP_CLAUSE_FINAL:
12928 case OMP_CLAUSE_DEPEND:
12929 case OMP_CLAUSE_FROM:
12930 case OMP_CLAUSE_TO:
12931 case OMP_CLAUSE_UNIFORM:
12932 case OMP_CLAUSE_MAP:
12933 case OMP_CLAUSE_DEVICE:
12934 case OMP_CLAUSE_DIST_SCHEDULE:
12935 case OMP_CLAUSE_NUM_TEAMS:
12936 case OMP_CLAUSE_THREAD_LIMIT:
12937 case OMP_CLAUSE_SAFELEN:
12938 case OMP_CLAUSE_SIMDLEN:
12939 OMP_CLAUSE_OPERAND (nc, 0)
12940 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12941 in_decl, /*integral_constant_expression_p=*/false);
12942 break;
12943 case OMP_CLAUSE_REDUCTION:
12944 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12946 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12947 if (TREE_CODE (placeholder) == SCOPE_REF)
12949 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12950 complain, in_decl);
12951 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12952 = build_qualified_name (NULL_TREE, scope,
12953 TREE_OPERAND (placeholder, 1),
12954 false);
12956 else
12957 gcc_assert (identifier_p (placeholder));
12959 OMP_CLAUSE_OPERAND (nc, 0)
12960 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12961 in_decl, /*integral_constant_expression_p=*/false);
12962 break;
12963 case OMP_CLAUSE_LINEAR:
12964 case OMP_CLAUSE_ALIGNED:
12965 OMP_CLAUSE_OPERAND (nc, 0)
12966 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12967 in_decl, /*integral_constant_expression_p=*/false);
12968 OMP_CLAUSE_OPERAND (nc, 1)
12969 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12970 in_decl, /*integral_constant_expression_p=*/false);
12971 break;
12973 case OMP_CLAUSE_NOWAIT:
12974 case OMP_CLAUSE_ORDERED:
12975 case OMP_CLAUSE_DEFAULT:
12976 case OMP_CLAUSE_UNTIED:
12977 case OMP_CLAUSE_MERGEABLE:
12978 case OMP_CLAUSE_INBRANCH:
12979 case OMP_CLAUSE_NOTINBRANCH:
12980 case OMP_CLAUSE_PROC_BIND:
12981 case OMP_CLAUSE_FOR:
12982 case OMP_CLAUSE_PARALLEL:
12983 case OMP_CLAUSE_SECTIONS:
12984 case OMP_CLAUSE_TASKGROUP:
12985 break;
12986 default:
12987 gcc_unreachable ();
12991 new_clauses = nreverse (new_clauses);
12992 if (!declare_simd)
12993 new_clauses = finish_omp_clauses (new_clauses);
12994 return new_clauses;
12997 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12999 static tree
13000 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13001 tree in_decl)
13003 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13005 tree purpose, value, chain;
13007 if (t == NULL)
13008 return t;
13010 if (TREE_CODE (t) != TREE_LIST)
13011 return tsubst_copy_and_build (t, args, complain, in_decl,
13012 /*function_p=*/false,
13013 /*integral_constant_expression_p=*/false);
13015 if (t == void_list_node)
13016 return t;
13018 purpose = TREE_PURPOSE (t);
13019 if (purpose)
13020 purpose = RECUR (purpose);
13021 value = TREE_VALUE (t);
13022 if (value)
13024 if (TREE_CODE (value) != LABEL_DECL)
13025 value = RECUR (value);
13026 else
13028 value = lookup_label (DECL_NAME (value));
13029 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13030 TREE_USED (value) = 1;
13033 chain = TREE_CHAIN (t);
13034 if (chain && chain != void_type_node)
13035 chain = RECUR (chain);
13036 return tree_cons (purpose, value, chain);
13037 #undef RECUR
13040 /* Substitute one OMP_FOR iterator. */
13042 static void
13043 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13044 tree condv, tree incrv, tree *clauses,
13045 tree args, tsubst_flags_t complain, tree in_decl,
13046 bool integral_constant_expression_p)
13048 #define RECUR(NODE) \
13049 tsubst_expr ((NODE), args, complain, in_decl, \
13050 integral_constant_expression_p)
13051 tree decl, init, cond, incr;
13052 bool init_decl;
13054 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13055 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13056 decl = TREE_OPERAND (init, 0);
13057 init = TREE_OPERAND (init, 1);
13058 /* Do this before substituting into decl to handle 'auto'. */
13059 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13060 init = RECUR (init);
13061 decl = RECUR (decl);
13063 if (decl == error_mark_node || init == error_mark_node)
13064 return;
13066 if (init_decl)
13068 gcc_assert (!processing_template_decl);
13069 init = DECL_INITIAL (decl);
13070 DECL_INITIAL (decl) = NULL_TREE;
13073 gcc_assert (!type_dependent_expression_p (decl));
13075 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13077 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13078 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13079 if (TREE_CODE (incr) == MODIFY_EXPR)
13080 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13081 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13082 RECUR (TREE_OPERAND (incr, 1)),
13083 complain);
13084 else
13085 incr = RECUR (incr);
13086 TREE_VEC_ELT (declv, i) = decl;
13087 TREE_VEC_ELT (initv, i) = init;
13088 TREE_VEC_ELT (condv, i) = cond;
13089 TREE_VEC_ELT (incrv, i) = incr;
13090 return;
13093 if (init && !init_decl)
13095 tree c;
13096 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13098 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13099 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13100 && OMP_CLAUSE_DECL (c) == decl)
13101 break;
13102 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13103 && OMP_CLAUSE_DECL (c) == decl)
13104 error ("iteration variable %qD should not be firstprivate", decl);
13105 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13106 && OMP_CLAUSE_DECL (c) == decl)
13107 error ("iteration variable %qD should not be reduction", decl);
13109 if (c == NULL)
13111 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13112 OMP_CLAUSE_DECL (c) = decl;
13113 c = finish_omp_clauses (c);
13114 if (c)
13116 OMP_CLAUSE_CHAIN (c) = *clauses;
13117 *clauses = c;
13121 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13122 if (COMPARISON_CLASS_P (cond))
13123 cond = build2 (TREE_CODE (cond), boolean_type_node,
13124 RECUR (TREE_OPERAND (cond, 0)),
13125 RECUR (TREE_OPERAND (cond, 1)));
13126 else
13127 cond = RECUR (cond);
13128 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13129 switch (TREE_CODE (incr))
13131 case PREINCREMENT_EXPR:
13132 case PREDECREMENT_EXPR:
13133 case POSTINCREMENT_EXPR:
13134 case POSTDECREMENT_EXPR:
13135 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13136 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13137 break;
13138 case MODIFY_EXPR:
13139 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13140 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13142 tree rhs = TREE_OPERAND (incr, 1);
13143 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13144 RECUR (TREE_OPERAND (incr, 0)),
13145 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13146 RECUR (TREE_OPERAND (rhs, 0)),
13147 RECUR (TREE_OPERAND (rhs, 1))));
13149 else
13150 incr = RECUR (incr);
13151 break;
13152 case MODOP_EXPR:
13153 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13154 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13156 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13157 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13158 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13159 TREE_TYPE (decl), lhs,
13160 RECUR (TREE_OPERAND (incr, 2))));
13162 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13163 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13164 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13166 tree rhs = TREE_OPERAND (incr, 2);
13167 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13168 RECUR (TREE_OPERAND (incr, 0)),
13169 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13170 RECUR (TREE_OPERAND (rhs, 0)),
13171 RECUR (TREE_OPERAND (rhs, 1))));
13173 else
13174 incr = RECUR (incr);
13175 break;
13176 default:
13177 incr = RECUR (incr);
13178 break;
13181 TREE_VEC_ELT (declv, i) = decl;
13182 TREE_VEC_ELT (initv, i) = init;
13183 TREE_VEC_ELT (condv, i) = cond;
13184 TREE_VEC_ELT (incrv, i) = incr;
13185 #undef RECUR
13188 /* Like tsubst_copy for expressions, etc. but also does semantic
13189 processing. */
13191 static tree
13192 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13193 bool integral_constant_expression_p)
13195 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13196 #define RECUR(NODE) \
13197 tsubst_expr ((NODE), args, complain, in_decl, \
13198 integral_constant_expression_p)
13200 tree stmt, tmp;
13201 tree r;
13202 location_t loc;
13204 if (t == NULL_TREE || t == error_mark_node)
13205 return t;
13207 loc = input_location;
13208 if (EXPR_HAS_LOCATION (t))
13209 input_location = EXPR_LOCATION (t);
13210 if (STATEMENT_CODE_P (TREE_CODE (t)))
13211 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13213 switch (TREE_CODE (t))
13215 case STATEMENT_LIST:
13217 tree_stmt_iterator i;
13218 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13219 RECUR (tsi_stmt (i));
13220 break;
13223 case CTOR_INITIALIZER:
13224 finish_mem_initializers (tsubst_initializer_list
13225 (TREE_OPERAND (t, 0), args));
13226 break;
13228 case RETURN_EXPR:
13229 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13230 break;
13232 case EXPR_STMT:
13233 tmp = RECUR (EXPR_STMT_EXPR (t));
13234 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13235 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13236 else
13237 finish_expr_stmt (tmp);
13238 break;
13240 case USING_STMT:
13241 do_using_directive (USING_STMT_NAMESPACE (t));
13242 break;
13244 case DECL_EXPR:
13246 tree decl, pattern_decl;
13247 tree init;
13249 pattern_decl = decl = DECL_EXPR_DECL (t);
13250 if (TREE_CODE (decl) == LABEL_DECL)
13251 finish_label_decl (DECL_NAME (decl));
13252 else if (TREE_CODE (decl) == USING_DECL)
13254 tree scope = USING_DECL_SCOPE (decl);
13255 tree name = DECL_NAME (decl);
13256 tree decl;
13258 scope = tsubst (scope, args, complain, in_decl);
13259 decl = lookup_qualified_name (scope, name,
13260 /*is_type_p=*/false,
13261 /*complain=*/false);
13262 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13263 qualified_name_lookup_error (scope, name, decl, input_location);
13264 else
13265 do_local_using_decl (decl, scope, name);
13267 else if (DECL_PACK_P (decl))
13269 /* Don't build up decls for a variadic capture proxy, we'll
13270 instantiate the elements directly as needed. */
13271 break;
13273 else
13275 init = DECL_INITIAL (decl);
13276 decl = tsubst (decl, args, complain, in_decl);
13277 if (decl != error_mark_node)
13279 /* By marking the declaration as instantiated, we avoid
13280 trying to instantiate it. Since instantiate_decl can't
13281 handle local variables, and since we've already done
13282 all that needs to be done, that's the right thing to
13283 do. */
13284 if (VAR_P (decl))
13285 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13286 if (VAR_P (decl)
13287 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13288 /* Anonymous aggregates are a special case. */
13289 finish_anon_union (decl);
13290 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13292 DECL_CONTEXT (decl) = current_function_decl;
13293 if (DECL_NAME (decl) == this_identifier)
13295 tree lam = DECL_CONTEXT (current_function_decl);
13296 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13297 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13299 insert_capture_proxy (decl);
13301 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13302 /* We already did a pushtag. */;
13303 else if (TREE_CODE (decl) == FUNCTION_DECL
13304 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13305 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13307 DECL_CONTEXT (decl) = NULL_TREE;
13308 pushdecl (decl);
13309 DECL_CONTEXT (decl) = current_function_decl;
13310 cp_check_omp_declare_reduction (decl);
13312 else
13314 int const_init = false;
13315 maybe_push_decl (decl);
13316 if (VAR_P (decl)
13317 && DECL_PRETTY_FUNCTION_P (decl))
13319 /* For __PRETTY_FUNCTION__ we have to adjust the
13320 initializer. */
13321 const char *const name
13322 = cxx_printable_name (current_function_decl, 2);
13323 init = cp_fname_init (name, &TREE_TYPE (decl));
13325 else
13327 tree t = RECUR (init);
13329 if (init && !t)
13331 /* If we had an initializer but it
13332 instantiated to nothing,
13333 value-initialize the object. This will
13334 only occur when the initializer was a
13335 pack expansion where the parameter packs
13336 used in that expansion were of length
13337 zero. */
13338 init = build_value_init (TREE_TYPE (decl),
13339 complain);
13340 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13341 init = get_target_expr_sfinae (init, complain);
13343 else
13344 init = t;
13347 if (VAR_P (decl))
13348 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13349 (pattern_decl));
13350 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13355 break;
13358 case FOR_STMT:
13359 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13360 RECUR (FOR_INIT_STMT (t));
13361 finish_for_init_stmt (stmt);
13362 tmp = RECUR (FOR_COND (t));
13363 finish_for_cond (tmp, stmt, false);
13364 tmp = RECUR (FOR_EXPR (t));
13365 finish_for_expr (tmp, stmt);
13366 RECUR (FOR_BODY (t));
13367 finish_for_stmt (stmt);
13368 break;
13370 case RANGE_FOR_STMT:
13372 tree decl, expr;
13373 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13374 decl = RANGE_FOR_DECL (t);
13375 decl = tsubst (decl, args, complain, in_decl);
13376 maybe_push_decl (decl);
13377 expr = RECUR (RANGE_FOR_EXPR (t));
13378 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13379 RECUR (RANGE_FOR_BODY (t));
13380 finish_for_stmt (stmt);
13382 break;
13384 case WHILE_STMT:
13385 stmt = begin_while_stmt ();
13386 tmp = RECUR (WHILE_COND (t));
13387 finish_while_stmt_cond (tmp, stmt, false);
13388 RECUR (WHILE_BODY (t));
13389 finish_while_stmt (stmt);
13390 break;
13392 case DO_STMT:
13393 stmt = begin_do_stmt ();
13394 RECUR (DO_BODY (t));
13395 finish_do_body (stmt);
13396 tmp = RECUR (DO_COND (t));
13397 finish_do_stmt (tmp, stmt, false);
13398 break;
13400 case IF_STMT:
13401 stmt = begin_if_stmt ();
13402 tmp = RECUR (IF_COND (t));
13403 finish_if_stmt_cond (tmp, stmt);
13404 RECUR (THEN_CLAUSE (t));
13405 finish_then_clause (stmt);
13407 if (ELSE_CLAUSE (t))
13409 begin_else_clause (stmt);
13410 RECUR (ELSE_CLAUSE (t));
13411 finish_else_clause (stmt);
13414 finish_if_stmt (stmt);
13415 break;
13417 case BIND_EXPR:
13418 if (BIND_EXPR_BODY_BLOCK (t))
13419 stmt = begin_function_body ();
13420 else
13421 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13422 ? BCS_TRY_BLOCK : 0);
13424 RECUR (BIND_EXPR_BODY (t));
13426 if (BIND_EXPR_BODY_BLOCK (t))
13427 finish_function_body (stmt);
13428 else
13429 finish_compound_stmt (stmt);
13430 break;
13432 case BREAK_STMT:
13433 finish_break_stmt ();
13434 break;
13436 case CONTINUE_STMT:
13437 finish_continue_stmt ();
13438 break;
13440 case SWITCH_STMT:
13441 stmt = begin_switch_stmt ();
13442 tmp = RECUR (SWITCH_STMT_COND (t));
13443 finish_switch_cond (tmp, stmt);
13444 RECUR (SWITCH_STMT_BODY (t));
13445 finish_switch_stmt (stmt);
13446 break;
13448 case CASE_LABEL_EXPR:
13449 finish_case_label (EXPR_LOCATION (t),
13450 RECUR (CASE_LOW (t)),
13451 RECUR (CASE_HIGH (t)));
13452 break;
13454 case LABEL_EXPR:
13456 tree decl = LABEL_EXPR_LABEL (t);
13457 tree label;
13459 label = finish_label_stmt (DECL_NAME (decl));
13460 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13461 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13463 break;
13465 case GOTO_EXPR:
13466 tmp = GOTO_DESTINATION (t);
13467 if (TREE_CODE (tmp) != LABEL_DECL)
13468 /* Computed goto's must be tsubst'd into. On the other hand,
13469 non-computed gotos must not be; the identifier in question
13470 will have no binding. */
13471 tmp = RECUR (tmp);
13472 else
13473 tmp = DECL_NAME (tmp);
13474 finish_goto_stmt (tmp);
13475 break;
13477 case ASM_EXPR:
13478 tmp = finish_asm_stmt
13479 (ASM_VOLATILE_P (t),
13480 RECUR (ASM_STRING (t)),
13481 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13482 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13483 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13484 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13486 tree asm_expr = tmp;
13487 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13488 asm_expr = TREE_OPERAND (asm_expr, 0);
13489 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13491 break;
13493 case TRY_BLOCK:
13494 if (CLEANUP_P (t))
13496 stmt = begin_try_block ();
13497 RECUR (TRY_STMTS (t));
13498 finish_cleanup_try_block (stmt);
13499 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13501 else
13503 tree compound_stmt = NULL_TREE;
13505 if (FN_TRY_BLOCK_P (t))
13506 stmt = begin_function_try_block (&compound_stmt);
13507 else
13508 stmt = begin_try_block ();
13510 RECUR (TRY_STMTS (t));
13512 if (FN_TRY_BLOCK_P (t))
13513 finish_function_try_block (stmt);
13514 else
13515 finish_try_block (stmt);
13517 RECUR (TRY_HANDLERS (t));
13518 if (FN_TRY_BLOCK_P (t))
13519 finish_function_handler_sequence (stmt, compound_stmt);
13520 else
13521 finish_handler_sequence (stmt);
13523 break;
13525 case HANDLER:
13527 tree decl = HANDLER_PARMS (t);
13529 if (decl)
13531 decl = tsubst (decl, args, complain, in_decl);
13532 /* Prevent instantiate_decl from trying to instantiate
13533 this variable. We've already done all that needs to be
13534 done. */
13535 if (decl != error_mark_node)
13536 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13538 stmt = begin_handler ();
13539 finish_handler_parms (decl, stmt);
13540 RECUR (HANDLER_BODY (t));
13541 finish_handler (stmt);
13543 break;
13545 case TAG_DEFN:
13546 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13547 if (CLASS_TYPE_P (tmp))
13549 /* Local classes are not independent templates; they are
13550 instantiated along with their containing function. And this
13551 way we don't have to deal with pushing out of one local class
13552 to instantiate a member of another local class. */
13553 tree fn;
13554 /* Closures are handled by the LAMBDA_EXPR. */
13555 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13556 complete_type (tmp);
13557 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13558 if (!DECL_ARTIFICIAL (fn))
13559 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13561 break;
13563 case STATIC_ASSERT:
13565 tree condition;
13567 ++c_inhibit_evaluation_warnings;
13568 condition =
13569 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13570 args,
13571 complain, in_decl,
13572 /*integral_constant_expression_p=*/true);
13573 --c_inhibit_evaluation_warnings;
13575 finish_static_assert (condition,
13576 STATIC_ASSERT_MESSAGE (t),
13577 STATIC_ASSERT_SOURCE_LOCATION (t),
13578 /*member_p=*/false);
13580 break;
13582 case OMP_PARALLEL:
13583 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13584 args, complain, in_decl);
13585 stmt = begin_omp_parallel ();
13586 RECUR (OMP_PARALLEL_BODY (t));
13587 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13588 = OMP_PARALLEL_COMBINED (t);
13589 break;
13591 case OMP_TASK:
13592 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13593 args, complain, in_decl);
13594 stmt = begin_omp_task ();
13595 RECUR (OMP_TASK_BODY (t));
13596 finish_omp_task (tmp, stmt);
13597 break;
13599 case OMP_FOR:
13600 case OMP_SIMD:
13601 case CILK_SIMD:
13602 case OMP_DISTRIBUTE:
13604 tree clauses, body, pre_body;
13605 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13606 tree incrv = NULL_TREE;
13607 int i;
13609 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13610 args, complain, in_decl);
13611 if (OMP_FOR_INIT (t) != NULL_TREE)
13613 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13614 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13615 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13616 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13619 stmt = begin_omp_structured_block ();
13621 pre_body = push_stmt_list ();
13622 RECUR (OMP_FOR_PRE_BODY (t));
13623 pre_body = pop_stmt_list (pre_body);
13625 if (OMP_FOR_INIT (t) != NULL_TREE)
13626 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13627 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13628 &clauses, args, complain, in_decl,
13629 integral_constant_expression_p);
13631 body = push_stmt_list ();
13632 RECUR (OMP_FOR_BODY (t));
13633 body = pop_stmt_list (body);
13635 if (OMP_FOR_INIT (t) != NULL_TREE)
13636 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13637 condv, incrv, body, pre_body, clauses);
13638 else
13640 t = make_node (TREE_CODE (t));
13641 TREE_TYPE (t) = void_type_node;
13642 OMP_FOR_BODY (t) = body;
13643 OMP_FOR_PRE_BODY (t) = pre_body;
13644 OMP_FOR_CLAUSES (t) = clauses;
13645 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13646 add_stmt (t);
13649 add_stmt (finish_omp_structured_block (stmt));
13651 break;
13653 case OMP_SECTIONS:
13654 case OMP_SINGLE:
13655 case OMP_TEAMS:
13656 case OMP_TARGET_DATA:
13657 case OMP_TARGET:
13658 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13659 args, complain, in_decl);
13660 stmt = push_stmt_list ();
13661 RECUR (OMP_BODY (t));
13662 stmt = pop_stmt_list (stmt);
13664 t = copy_node (t);
13665 OMP_BODY (t) = stmt;
13666 OMP_CLAUSES (t) = tmp;
13667 add_stmt (t);
13668 break;
13670 case OMP_TARGET_UPDATE:
13671 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13672 args, complain, in_decl);
13673 t = copy_node (t);
13674 OMP_CLAUSES (t) = tmp;
13675 add_stmt (t);
13676 break;
13678 case OMP_SECTION:
13679 case OMP_CRITICAL:
13680 case OMP_MASTER:
13681 case OMP_TASKGROUP:
13682 case OMP_ORDERED:
13683 stmt = push_stmt_list ();
13684 RECUR (OMP_BODY (t));
13685 stmt = pop_stmt_list (stmt);
13687 t = copy_node (t);
13688 OMP_BODY (t) = stmt;
13689 add_stmt (t);
13690 break;
13692 case OMP_ATOMIC:
13693 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13694 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13696 tree op1 = TREE_OPERAND (t, 1);
13697 tree rhs1 = NULL_TREE;
13698 tree lhs, rhs;
13699 if (TREE_CODE (op1) == COMPOUND_EXPR)
13701 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13702 op1 = TREE_OPERAND (op1, 1);
13704 lhs = RECUR (TREE_OPERAND (op1, 0));
13705 rhs = RECUR (TREE_OPERAND (op1, 1));
13706 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13707 NULL_TREE, NULL_TREE, rhs1,
13708 OMP_ATOMIC_SEQ_CST (t));
13710 else
13712 tree op1 = TREE_OPERAND (t, 1);
13713 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13714 tree rhs1 = NULL_TREE;
13715 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13716 enum tree_code opcode = NOP_EXPR;
13717 if (code == OMP_ATOMIC_READ)
13719 v = RECUR (TREE_OPERAND (op1, 0));
13720 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13722 else if (code == OMP_ATOMIC_CAPTURE_OLD
13723 || code == OMP_ATOMIC_CAPTURE_NEW)
13725 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13726 v = RECUR (TREE_OPERAND (op1, 0));
13727 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13728 if (TREE_CODE (op11) == COMPOUND_EXPR)
13730 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13731 op11 = TREE_OPERAND (op11, 1);
13733 lhs = RECUR (TREE_OPERAND (op11, 0));
13734 rhs = RECUR (TREE_OPERAND (op11, 1));
13735 opcode = TREE_CODE (op11);
13736 if (opcode == MODIFY_EXPR)
13737 opcode = NOP_EXPR;
13739 else
13741 code = OMP_ATOMIC;
13742 lhs = RECUR (TREE_OPERAND (op1, 0));
13743 rhs = RECUR (TREE_OPERAND (op1, 1));
13745 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13746 OMP_ATOMIC_SEQ_CST (t));
13748 break;
13750 case TRANSACTION_EXPR:
13752 int flags = 0;
13753 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13754 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13756 if (TRANSACTION_EXPR_IS_STMT (t))
13758 tree body = TRANSACTION_EXPR_BODY (t);
13759 tree noex = NULL_TREE;
13760 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13762 noex = MUST_NOT_THROW_COND (body);
13763 if (noex == NULL_TREE)
13764 noex = boolean_true_node;
13765 body = TREE_OPERAND (body, 0);
13767 stmt = begin_transaction_stmt (input_location, NULL, flags);
13768 RECUR (body);
13769 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13771 else
13773 stmt = build_transaction_expr (EXPR_LOCATION (t),
13774 RECUR (TRANSACTION_EXPR_BODY (t)),
13775 flags, NULL_TREE);
13776 RETURN (stmt);
13779 break;
13781 case MUST_NOT_THROW_EXPR:
13782 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13783 RECUR (MUST_NOT_THROW_COND (t))));
13785 case EXPR_PACK_EXPANSION:
13786 error ("invalid use of pack expansion expression");
13787 RETURN (error_mark_node);
13789 case NONTYPE_ARGUMENT_PACK:
13790 error ("use %<...%> to expand argument pack");
13791 RETURN (error_mark_node);
13793 case CILK_SPAWN_STMT:
13794 cfun->calls_cilk_spawn = 1;
13795 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
13797 case CILK_SYNC_STMT:
13798 RETURN (build_cilk_sync ());
13800 case COMPOUND_EXPR:
13801 tmp = RECUR (TREE_OPERAND (t, 0));
13802 if (tmp == NULL_TREE)
13803 /* If the first operand was a statement, we're done with it. */
13804 RETURN (RECUR (TREE_OPERAND (t, 1)));
13805 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13806 RECUR (TREE_OPERAND (t, 1)),
13807 complain));
13809 case ANNOTATE_EXPR:
13810 tmp = RECUR (TREE_OPERAND (t, 0));
13811 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
13812 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
13814 default:
13815 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13817 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13818 /*function_p=*/false,
13819 integral_constant_expression_p));
13822 RETURN (NULL_TREE);
13823 out:
13824 input_location = loc;
13825 return r;
13826 #undef RECUR
13827 #undef RETURN
13830 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13831 function. For description of the body see comment above
13832 cp_parser_omp_declare_reduction_exprs. */
13834 static void
13835 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13837 if (t == NULL_TREE || t == error_mark_node)
13838 return;
13840 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13842 tree_stmt_iterator tsi;
13843 int i;
13844 tree stmts[7];
13845 memset (stmts, 0, sizeof stmts);
13846 for (i = 0, tsi = tsi_start (t);
13847 i < 7 && !tsi_end_p (tsi);
13848 i++, tsi_next (&tsi))
13849 stmts[i] = tsi_stmt (tsi);
13850 gcc_assert (tsi_end_p (tsi));
13852 if (i >= 3)
13854 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13855 && TREE_CODE (stmts[1]) == DECL_EXPR);
13856 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13857 args, complain, in_decl);
13858 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13859 args, complain, in_decl);
13860 DECL_CONTEXT (omp_out) = current_function_decl;
13861 DECL_CONTEXT (omp_in) = current_function_decl;
13862 keep_next_level (true);
13863 tree block = begin_omp_structured_block ();
13864 tsubst_expr (stmts[2], args, complain, in_decl, false);
13865 block = finish_omp_structured_block (block);
13866 block = maybe_cleanup_point_expr_void (block);
13867 add_decl_expr (omp_out);
13868 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13869 TREE_NO_WARNING (omp_out) = 1;
13870 add_decl_expr (omp_in);
13871 finish_expr_stmt (block);
13873 if (i >= 6)
13875 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13876 && TREE_CODE (stmts[4]) == DECL_EXPR);
13877 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13878 args, complain, in_decl);
13879 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13880 args, complain, in_decl);
13881 DECL_CONTEXT (omp_priv) = current_function_decl;
13882 DECL_CONTEXT (omp_orig) = current_function_decl;
13883 keep_next_level (true);
13884 tree block = begin_omp_structured_block ();
13885 tsubst_expr (stmts[5], args, complain, in_decl, false);
13886 block = finish_omp_structured_block (block);
13887 block = maybe_cleanup_point_expr_void (block);
13888 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13889 add_decl_expr (omp_priv);
13890 add_decl_expr (omp_orig);
13891 finish_expr_stmt (block);
13892 if (i == 7)
13893 add_decl_expr (omp_orig);
13897 /* T is a postfix-expression that is not being used in a function
13898 call. Return the substituted version of T. */
13900 static tree
13901 tsubst_non_call_postfix_expression (tree t, tree args,
13902 tsubst_flags_t complain,
13903 tree in_decl)
13905 if (TREE_CODE (t) == SCOPE_REF)
13906 t = tsubst_qualified_id (t, args, complain, in_decl,
13907 /*done=*/false, /*address_p=*/false);
13908 else
13909 t = tsubst_copy_and_build (t, args, complain, in_decl,
13910 /*function_p=*/false,
13911 /*integral_constant_expression_p=*/false);
13913 return t;
13916 /* Like tsubst but deals with expressions and performs semantic
13917 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13919 tree
13920 tsubst_copy_and_build (tree t,
13921 tree args,
13922 tsubst_flags_t complain,
13923 tree in_decl,
13924 bool function_p,
13925 bool integral_constant_expression_p)
13927 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13928 #define RECUR(NODE) \
13929 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13930 /*function_p=*/false, \
13931 integral_constant_expression_p)
13933 tree retval, op1;
13934 location_t loc;
13936 if (t == NULL_TREE || t == error_mark_node)
13937 return t;
13939 loc = input_location;
13940 if (EXPR_HAS_LOCATION (t))
13941 input_location = EXPR_LOCATION (t);
13943 /* N3276 decltype magic only applies to calls at the top level or on the
13944 right side of a comma. */
13945 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13946 complain &= ~tf_decltype;
13948 switch (TREE_CODE (t))
13950 case USING_DECL:
13951 t = DECL_NAME (t);
13952 /* Fall through. */
13953 case IDENTIFIER_NODE:
13955 tree decl;
13956 cp_id_kind idk;
13957 bool non_integral_constant_expression_p;
13958 const char *error_msg;
13960 if (IDENTIFIER_TYPENAME_P (t))
13962 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13963 t = mangle_conv_op_name_for_type (new_type);
13966 /* Look up the name. */
13967 decl = lookup_name (t);
13969 /* By convention, expressions use ERROR_MARK_NODE to indicate
13970 failure, not NULL_TREE. */
13971 if (decl == NULL_TREE)
13972 decl = error_mark_node;
13974 decl = finish_id_expression (t, decl, NULL_TREE,
13975 &idk,
13976 integral_constant_expression_p,
13977 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13978 &non_integral_constant_expression_p,
13979 /*template_p=*/false,
13980 /*done=*/true,
13981 /*address_p=*/false,
13982 /*template_arg_p=*/false,
13983 &error_msg,
13984 input_location);
13985 if (error_msg)
13986 error (error_msg);
13987 if (!function_p && identifier_p (decl))
13989 if (complain & tf_error)
13990 unqualified_name_lookup_error (decl);
13991 decl = error_mark_node;
13993 RETURN (decl);
13996 case TEMPLATE_ID_EXPR:
13998 tree object;
13999 tree templ = RECUR (TREE_OPERAND (t, 0));
14000 tree targs = TREE_OPERAND (t, 1);
14002 if (targs)
14003 targs = tsubst_template_args (targs, args, complain, in_decl);
14005 if (TREE_CODE (templ) == COMPONENT_REF)
14007 object = TREE_OPERAND (templ, 0);
14008 templ = TREE_OPERAND (templ, 1);
14010 else
14011 object = NULL_TREE;
14012 templ = lookup_template_function (templ, targs);
14014 if (object)
14015 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14016 object, templ, NULL_TREE));
14017 else
14018 RETURN (baselink_for_fns (templ));
14021 case INDIRECT_REF:
14023 tree r = RECUR (TREE_OPERAND (t, 0));
14025 if (REFERENCE_REF_P (t))
14027 /* A type conversion to reference type will be enclosed in
14028 such an indirect ref, but the substitution of the cast
14029 will have also added such an indirect ref. */
14030 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14031 r = convert_from_reference (r);
14033 else
14034 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14035 complain|decltype_flag);
14036 RETURN (r);
14039 case NOP_EXPR:
14040 RETURN (build_nop
14041 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14042 RECUR (TREE_OPERAND (t, 0))));
14044 case IMPLICIT_CONV_EXPR:
14046 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14047 tree expr = RECUR (TREE_OPERAND (t, 0));
14048 int flags = LOOKUP_IMPLICIT;
14049 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14050 flags = LOOKUP_NORMAL;
14051 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14052 flags));
14055 case CONVERT_EXPR:
14056 RETURN (build1
14057 (CONVERT_EXPR,
14058 tsubst (TREE_TYPE (t), args, complain, in_decl),
14059 RECUR (TREE_OPERAND (t, 0))));
14061 case CAST_EXPR:
14062 case REINTERPRET_CAST_EXPR:
14063 case CONST_CAST_EXPR:
14064 case DYNAMIC_CAST_EXPR:
14065 case STATIC_CAST_EXPR:
14067 tree type;
14068 tree op, r = NULL_TREE;
14070 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14071 if (integral_constant_expression_p
14072 && !cast_valid_in_integral_constant_expression_p (type))
14074 if (complain & tf_error)
14075 error ("a cast to a type other than an integral or "
14076 "enumeration type cannot appear in a constant-expression");
14077 RETURN (error_mark_node);
14080 op = RECUR (TREE_OPERAND (t, 0));
14082 ++c_inhibit_evaluation_warnings;
14083 switch (TREE_CODE (t))
14085 case CAST_EXPR:
14086 r = build_functional_cast (type, op, complain);
14087 break;
14088 case REINTERPRET_CAST_EXPR:
14089 r = build_reinterpret_cast (type, op, complain);
14090 break;
14091 case CONST_CAST_EXPR:
14092 r = build_const_cast (type, op, complain);
14093 break;
14094 case DYNAMIC_CAST_EXPR:
14095 r = build_dynamic_cast (type, op, complain);
14096 break;
14097 case STATIC_CAST_EXPR:
14098 r = build_static_cast (type, op, complain);
14099 break;
14100 default:
14101 gcc_unreachable ();
14103 --c_inhibit_evaluation_warnings;
14105 RETURN (r);
14108 case POSTDECREMENT_EXPR:
14109 case POSTINCREMENT_EXPR:
14110 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14111 args, complain, in_decl);
14112 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14113 complain|decltype_flag));
14115 case PREDECREMENT_EXPR:
14116 case PREINCREMENT_EXPR:
14117 case NEGATE_EXPR:
14118 case BIT_NOT_EXPR:
14119 case ABS_EXPR:
14120 case TRUTH_NOT_EXPR:
14121 case UNARY_PLUS_EXPR: /* Unary + */
14122 case REALPART_EXPR:
14123 case IMAGPART_EXPR:
14124 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14125 RECUR (TREE_OPERAND (t, 0)),
14126 complain|decltype_flag));
14128 case FIX_TRUNC_EXPR:
14129 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14130 0, complain));
14132 case ADDR_EXPR:
14133 op1 = TREE_OPERAND (t, 0);
14134 if (TREE_CODE (op1) == LABEL_DECL)
14135 RETURN (finish_label_address_expr (DECL_NAME (op1),
14136 EXPR_LOCATION (op1)));
14137 if (TREE_CODE (op1) == SCOPE_REF)
14138 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14139 /*done=*/true, /*address_p=*/true);
14140 else
14141 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14142 in_decl);
14143 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14144 complain|decltype_flag));
14146 case PLUS_EXPR:
14147 case MINUS_EXPR:
14148 case MULT_EXPR:
14149 case TRUNC_DIV_EXPR:
14150 case CEIL_DIV_EXPR:
14151 case FLOOR_DIV_EXPR:
14152 case ROUND_DIV_EXPR:
14153 case EXACT_DIV_EXPR:
14154 case BIT_AND_EXPR:
14155 case BIT_IOR_EXPR:
14156 case BIT_XOR_EXPR:
14157 case TRUNC_MOD_EXPR:
14158 case FLOOR_MOD_EXPR:
14159 case TRUTH_ANDIF_EXPR:
14160 case TRUTH_ORIF_EXPR:
14161 case TRUTH_AND_EXPR:
14162 case TRUTH_OR_EXPR:
14163 case RSHIFT_EXPR:
14164 case LSHIFT_EXPR:
14165 case RROTATE_EXPR:
14166 case LROTATE_EXPR:
14167 case EQ_EXPR:
14168 case NE_EXPR:
14169 case MAX_EXPR:
14170 case MIN_EXPR:
14171 case LE_EXPR:
14172 case GE_EXPR:
14173 case LT_EXPR:
14174 case GT_EXPR:
14175 case MEMBER_REF:
14176 case DOTSTAR_EXPR:
14178 tree r;
14180 ++c_inhibit_evaluation_warnings;
14182 r = build_x_binary_op
14183 (input_location, TREE_CODE (t),
14184 RECUR (TREE_OPERAND (t, 0)),
14185 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14186 ? ERROR_MARK
14187 : TREE_CODE (TREE_OPERAND (t, 0))),
14188 RECUR (TREE_OPERAND (t, 1)),
14189 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14190 ? ERROR_MARK
14191 : TREE_CODE (TREE_OPERAND (t, 1))),
14192 /*overload=*/NULL,
14193 complain|decltype_flag);
14194 if (EXPR_P (r) && TREE_NO_WARNING (t))
14195 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14197 --c_inhibit_evaluation_warnings;
14199 RETURN (r);
14202 case POINTER_PLUS_EXPR:
14203 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14204 RECUR (TREE_OPERAND (t, 1)));
14206 case SCOPE_REF:
14207 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14208 /*address_p=*/false));
14209 case ARRAY_REF:
14210 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14211 args, complain, in_decl);
14212 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14213 RECUR (TREE_OPERAND (t, 1)),
14214 complain|decltype_flag));
14216 case ARRAY_NOTATION_REF:
14218 tree start_index, length, stride;
14219 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14220 args, complain, in_decl);
14221 start_index = RECUR (ARRAY_NOTATION_START (t));
14222 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14223 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14224 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14225 length, stride, TREE_TYPE (op1)));
14227 case SIZEOF_EXPR:
14228 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14229 RETURN (tsubst_copy (t, args, complain, in_decl));
14230 /* Fall through */
14232 case ALIGNOF_EXPR:
14234 tree r;
14236 op1 = TREE_OPERAND (t, 0);
14237 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14238 op1 = TREE_TYPE (op1);
14239 if (!args)
14241 /* When there are no ARGS, we are trying to evaluate a
14242 non-dependent expression from the parser. Trying to do
14243 the substitutions may not work. */
14244 if (!TYPE_P (op1))
14245 op1 = TREE_TYPE (op1);
14247 else
14249 ++cp_unevaluated_operand;
14250 ++c_inhibit_evaluation_warnings;
14251 if (TYPE_P (op1))
14252 op1 = tsubst (op1, args, complain, in_decl);
14253 else
14254 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14255 /*function_p=*/false,
14256 /*integral_constant_expression_p=*/
14257 false);
14258 --cp_unevaluated_operand;
14259 --c_inhibit_evaluation_warnings;
14261 if (TYPE_P (op1))
14262 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14263 complain & tf_error);
14264 else
14265 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14266 complain & tf_error);
14267 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14269 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14271 if (!processing_template_decl && TYPE_P (op1))
14273 r = build_min (SIZEOF_EXPR, size_type_node,
14274 build1 (NOP_EXPR, op1, error_mark_node));
14275 SIZEOF_EXPR_TYPE_P (r) = 1;
14277 else
14278 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14279 TREE_SIDE_EFFECTS (r) = 0;
14280 TREE_READONLY (r) = 1;
14282 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14284 RETURN (r);
14287 case AT_ENCODE_EXPR:
14289 op1 = TREE_OPERAND (t, 0);
14290 ++cp_unevaluated_operand;
14291 ++c_inhibit_evaluation_warnings;
14292 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14293 /*function_p=*/false,
14294 /*integral_constant_expression_p=*/false);
14295 --cp_unevaluated_operand;
14296 --c_inhibit_evaluation_warnings;
14297 RETURN (objc_build_encode_expr (op1));
14300 case NOEXCEPT_EXPR:
14301 op1 = TREE_OPERAND (t, 0);
14302 ++cp_unevaluated_operand;
14303 ++c_inhibit_evaluation_warnings;
14304 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14305 /*function_p=*/false,
14306 /*integral_constant_expression_p=*/false);
14307 --cp_unevaluated_operand;
14308 --c_inhibit_evaluation_warnings;
14309 RETURN (finish_noexcept_expr (op1, complain));
14311 case MODOP_EXPR:
14313 tree r;
14315 ++c_inhibit_evaluation_warnings;
14317 r = build_x_modify_expr
14318 (EXPR_LOCATION (t),
14319 RECUR (TREE_OPERAND (t, 0)),
14320 TREE_CODE (TREE_OPERAND (t, 1)),
14321 RECUR (TREE_OPERAND (t, 2)),
14322 complain|decltype_flag);
14323 /* TREE_NO_WARNING must be set if either the expression was
14324 parenthesized or it uses an operator such as >>= rather
14325 than plain assignment. In the former case, it was already
14326 set and must be copied. In the latter case,
14327 build_x_modify_expr sets it and it must not be reset
14328 here. */
14329 if (TREE_NO_WARNING (t))
14330 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14332 --c_inhibit_evaluation_warnings;
14334 RETURN (r);
14337 case ARROW_EXPR:
14338 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14339 args, complain, in_decl);
14340 /* Remember that there was a reference to this entity. */
14341 if (DECL_P (op1))
14342 mark_used (op1);
14343 RETURN (build_x_arrow (input_location, op1, complain));
14345 case NEW_EXPR:
14347 tree placement = RECUR (TREE_OPERAND (t, 0));
14348 tree init = RECUR (TREE_OPERAND (t, 3));
14349 vec<tree, va_gc> *placement_vec;
14350 vec<tree, va_gc> *init_vec;
14351 tree ret;
14353 if (placement == NULL_TREE)
14354 placement_vec = NULL;
14355 else
14357 placement_vec = make_tree_vector ();
14358 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14359 vec_safe_push (placement_vec, TREE_VALUE (placement));
14362 /* If there was an initializer in the original tree, but it
14363 instantiated to an empty list, then we should pass a
14364 non-NULL empty vector to tell build_new that it was an
14365 empty initializer() rather than no initializer. This can
14366 only happen when the initializer is a pack expansion whose
14367 parameter packs are of length zero. */
14368 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14369 init_vec = NULL;
14370 else
14372 init_vec = make_tree_vector ();
14373 if (init == void_zero_node)
14374 gcc_assert (init_vec != NULL);
14375 else
14377 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14378 vec_safe_push (init_vec, TREE_VALUE (init));
14382 ret = build_new (&placement_vec,
14383 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14384 RECUR (TREE_OPERAND (t, 2)),
14385 &init_vec,
14386 NEW_EXPR_USE_GLOBAL (t),
14387 complain);
14389 if (placement_vec != NULL)
14390 release_tree_vector (placement_vec);
14391 if (init_vec != NULL)
14392 release_tree_vector (init_vec);
14394 RETURN (ret);
14397 case DELETE_EXPR:
14398 RETURN (delete_sanity
14399 (RECUR (TREE_OPERAND (t, 0)),
14400 RECUR (TREE_OPERAND (t, 1)),
14401 DELETE_EXPR_USE_VEC (t),
14402 DELETE_EXPR_USE_GLOBAL (t),
14403 complain));
14405 case COMPOUND_EXPR:
14407 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14408 complain & ~tf_decltype, in_decl,
14409 /*function_p=*/false,
14410 integral_constant_expression_p);
14411 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14412 op0,
14413 RECUR (TREE_OPERAND (t, 1)),
14414 complain|decltype_flag));
14417 case CALL_EXPR:
14419 tree function;
14420 vec<tree, va_gc> *call_args;
14421 unsigned int nargs, i;
14422 bool qualified_p;
14423 bool koenig_p;
14424 tree ret;
14426 function = CALL_EXPR_FN (t);
14427 /* When we parsed the expression, we determined whether or
14428 not Koenig lookup should be performed. */
14429 koenig_p = KOENIG_LOOKUP_P (t);
14430 if (TREE_CODE (function) == SCOPE_REF)
14432 qualified_p = true;
14433 function = tsubst_qualified_id (function, args, complain, in_decl,
14434 /*done=*/false,
14435 /*address_p=*/false);
14437 else if (koenig_p && identifier_p (function))
14439 /* Do nothing; calling tsubst_copy_and_build on an identifier
14440 would incorrectly perform unqualified lookup again.
14442 Note that we can also have an IDENTIFIER_NODE if the earlier
14443 unqualified lookup found a member function; in that case
14444 koenig_p will be false and we do want to do the lookup
14445 again to find the instantiated member function.
14447 FIXME but doing that causes c++/15272, so we need to stop
14448 using IDENTIFIER_NODE in that situation. */
14449 qualified_p = false;
14451 else
14453 if (TREE_CODE (function) == COMPONENT_REF)
14455 tree op = TREE_OPERAND (function, 1);
14457 qualified_p = (TREE_CODE (op) == SCOPE_REF
14458 || (BASELINK_P (op)
14459 && BASELINK_QUALIFIED_P (op)));
14461 else
14462 qualified_p = false;
14464 if (TREE_CODE (function) == ADDR_EXPR
14465 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14466 /* Avoid error about taking the address of a constructor. */
14467 function = TREE_OPERAND (function, 0);
14469 function = tsubst_copy_and_build (function, args, complain,
14470 in_decl,
14471 !qualified_p,
14472 integral_constant_expression_p);
14474 if (BASELINK_P (function))
14475 qualified_p = true;
14478 nargs = call_expr_nargs (t);
14479 call_args = make_tree_vector ();
14480 for (i = 0; i < nargs; ++i)
14482 tree arg = CALL_EXPR_ARG (t, i);
14484 if (!PACK_EXPANSION_P (arg))
14485 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14486 else
14488 /* Expand the pack expansion and push each entry onto
14489 CALL_ARGS. */
14490 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14491 if (TREE_CODE (arg) == TREE_VEC)
14493 unsigned int len, j;
14495 len = TREE_VEC_LENGTH (arg);
14496 for (j = 0; j < len; ++j)
14498 tree value = TREE_VEC_ELT (arg, j);
14499 if (value != NULL_TREE)
14500 value = convert_from_reference (value);
14501 vec_safe_push (call_args, value);
14504 else
14506 /* A partial substitution. Add one entry. */
14507 vec_safe_push (call_args, arg);
14512 /* We do not perform argument-dependent lookup if normal
14513 lookup finds a non-function, in accordance with the
14514 expected resolution of DR 218. */
14515 if (koenig_p
14516 && ((is_overloaded_fn (function)
14517 /* If lookup found a member function, the Koenig lookup is
14518 not appropriate, even if an unqualified-name was used
14519 to denote the function. */
14520 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14521 || identifier_p (function))
14522 /* Only do this when substitution turns a dependent call
14523 into a non-dependent call. */
14524 && type_dependent_expression_p_push (t)
14525 && !any_type_dependent_arguments_p (call_args))
14526 function = perform_koenig_lookup (function, call_args, tf_none);
14528 if (identifier_p (function)
14529 && !any_type_dependent_arguments_p (call_args))
14531 if (koenig_p && (complain & tf_warning_or_error))
14533 /* For backwards compatibility and good diagnostics, try
14534 the unqualified lookup again if we aren't in SFINAE
14535 context. */
14536 tree unq = (tsubst_copy_and_build
14537 (function, args, complain, in_decl, true,
14538 integral_constant_expression_p));
14539 if (unq == error_mark_node)
14540 RETURN (error_mark_node);
14542 if (unq != function)
14544 tree fn = unq;
14545 if (INDIRECT_REF_P (fn))
14546 fn = TREE_OPERAND (fn, 0);
14547 if (TREE_CODE (fn) == COMPONENT_REF)
14548 fn = TREE_OPERAND (fn, 1);
14549 if (is_overloaded_fn (fn))
14550 fn = get_first_fn (fn);
14551 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
14552 "%qD was not declared in this scope, "
14553 "and no declarations were found by "
14554 "argument-dependent lookup at the point "
14555 "of instantiation", function))
14557 if (!DECL_P (fn))
14558 /* Can't say anything more. */;
14559 else if (DECL_CLASS_SCOPE_P (fn))
14561 location_t loc = EXPR_LOC_OR_LOC (t,
14562 input_location);
14563 inform (loc,
14564 "declarations in dependent base %qT are "
14565 "not found by unqualified lookup",
14566 DECL_CLASS_CONTEXT (fn));
14567 if (current_class_ptr)
14568 inform (loc,
14569 "use %<this->%D%> instead", function);
14570 else
14571 inform (loc,
14572 "use %<%T::%D%> instead",
14573 current_class_name, function);
14575 else
14576 inform (0, "%q+D declared here, later in the "
14577 "translation unit", fn);
14579 function = unq;
14582 if (identifier_p (function))
14584 if (complain & tf_error)
14585 unqualified_name_lookup_error (function);
14586 release_tree_vector (call_args);
14587 RETURN (error_mark_node);
14591 /* Remember that there was a reference to this entity. */
14592 if (DECL_P (function))
14593 mark_used (function);
14595 /* Put back tf_decltype for the actual call. */
14596 complain |= decltype_flag;
14598 if (TREE_CODE (function) == OFFSET_REF)
14599 ret = build_offset_ref_call_from_tree (function, &call_args,
14600 complain);
14601 else if (TREE_CODE (function) == COMPONENT_REF)
14603 tree instance = TREE_OPERAND (function, 0);
14604 tree fn = TREE_OPERAND (function, 1);
14606 if (processing_template_decl
14607 && (type_dependent_expression_p (instance)
14608 || (!BASELINK_P (fn)
14609 && TREE_CODE (fn) != FIELD_DECL)
14610 || type_dependent_expression_p (fn)
14611 || any_type_dependent_arguments_p (call_args)))
14612 ret = build_nt_call_vec (function, call_args);
14613 else if (!BASELINK_P (fn))
14614 ret = finish_call_expr (function, &call_args,
14615 /*disallow_virtual=*/false,
14616 /*koenig_p=*/false,
14617 complain);
14618 else
14619 ret = (build_new_method_call
14620 (instance, fn,
14621 &call_args, NULL_TREE,
14622 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14623 /*fn_p=*/NULL,
14624 complain));
14626 else
14627 ret = finish_call_expr (function, &call_args,
14628 /*disallow_virtual=*/qualified_p,
14629 koenig_p,
14630 complain);
14632 release_tree_vector (call_args);
14634 RETURN (ret);
14637 case COND_EXPR:
14639 tree cond = RECUR (TREE_OPERAND (t, 0));
14640 tree exp1, exp2;
14642 if (TREE_CODE (cond) == INTEGER_CST)
14644 if (integer_zerop (cond))
14646 ++c_inhibit_evaluation_warnings;
14647 exp1 = RECUR (TREE_OPERAND (t, 1));
14648 --c_inhibit_evaluation_warnings;
14649 exp2 = RECUR (TREE_OPERAND (t, 2));
14651 else
14653 exp1 = RECUR (TREE_OPERAND (t, 1));
14654 ++c_inhibit_evaluation_warnings;
14655 exp2 = RECUR (TREE_OPERAND (t, 2));
14656 --c_inhibit_evaluation_warnings;
14659 else
14661 exp1 = RECUR (TREE_OPERAND (t, 1));
14662 exp2 = RECUR (TREE_OPERAND (t, 2));
14665 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14666 cond, exp1, exp2, complain));
14669 case PSEUDO_DTOR_EXPR:
14670 RETURN (finish_pseudo_destructor_expr
14671 (RECUR (TREE_OPERAND (t, 0)),
14672 RECUR (TREE_OPERAND (t, 1)),
14673 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14674 input_location));
14676 case TREE_LIST:
14678 tree purpose, value, chain;
14680 if (t == void_list_node)
14681 RETURN (t);
14683 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14684 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14686 /* We have pack expansions, so expand those and
14687 create a new list out of it. */
14688 tree purposevec = NULL_TREE;
14689 tree valuevec = NULL_TREE;
14690 tree chain;
14691 int i, len = -1;
14693 /* Expand the argument expressions. */
14694 if (TREE_PURPOSE (t))
14695 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14696 complain, in_decl);
14697 if (TREE_VALUE (t))
14698 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14699 complain, in_decl);
14701 /* Build the rest of the list. */
14702 chain = TREE_CHAIN (t);
14703 if (chain && chain != void_type_node)
14704 chain = RECUR (chain);
14706 /* Determine the number of arguments. */
14707 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14709 len = TREE_VEC_LENGTH (purposevec);
14710 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14712 else if (TREE_CODE (valuevec) == TREE_VEC)
14713 len = TREE_VEC_LENGTH (valuevec);
14714 else
14716 /* Since we only performed a partial substitution into
14717 the argument pack, we only RETURN (a single list
14718 node. */
14719 if (purposevec == TREE_PURPOSE (t)
14720 && valuevec == TREE_VALUE (t)
14721 && chain == TREE_CHAIN (t))
14722 RETURN (t);
14724 RETURN (tree_cons (purposevec, valuevec, chain));
14727 /* Convert the argument vectors into a TREE_LIST */
14728 i = len;
14729 while (i > 0)
14731 /* Grab the Ith values. */
14732 i--;
14733 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14734 : NULL_TREE;
14735 value
14736 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14737 : NULL_TREE;
14739 /* Build the list (backwards). */
14740 chain = tree_cons (purpose, value, chain);
14743 RETURN (chain);
14746 purpose = TREE_PURPOSE (t);
14747 if (purpose)
14748 purpose = RECUR (purpose);
14749 value = TREE_VALUE (t);
14750 if (value)
14751 value = RECUR (value);
14752 chain = TREE_CHAIN (t);
14753 if (chain && chain != void_type_node)
14754 chain = RECUR (chain);
14755 if (purpose == TREE_PURPOSE (t)
14756 && value == TREE_VALUE (t)
14757 && chain == TREE_CHAIN (t))
14758 RETURN (t);
14759 RETURN (tree_cons (purpose, value, chain));
14762 case COMPONENT_REF:
14764 tree object;
14765 tree object_type;
14766 tree member;
14768 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14769 args, complain, in_decl);
14770 /* Remember that there was a reference to this entity. */
14771 if (DECL_P (object))
14772 mark_used (object);
14773 object_type = TREE_TYPE (object);
14775 member = TREE_OPERAND (t, 1);
14776 if (BASELINK_P (member))
14777 member = tsubst_baselink (member,
14778 non_reference (TREE_TYPE (object)),
14779 args, complain, in_decl);
14780 else
14781 member = tsubst_copy (member, args, complain, in_decl);
14782 if (member == error_mark_node)
14783 RETURN (error_mark_node);
14785 if (type_dependent_expression_p (object))
14786 /* We can't do much here. */;
14787 else if (!CLASS_TYPE_P (object_type))
14789 if (scalarish_type_p (object_type))
14791 tree s = NULL_TREE;
14792 tree dtor = member;
14794 if (TREE_CODE (dtor) == SCOPE_REF)
14796 s = TREE_OPERAND (dtor, 0);
14797 dtor = TREE_OPERAND (dtor, 1);
14799 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14801 dtor = TREE_OPERAND (dtor, 0);
14802 if (TYPE_P (dtor))
14803 RETURN (finish_pseudo_destructor_expr
14804 (object, s, dtor, input_location));
14808 else if (TREE_CODE (member) == SCOPE_REF
14809 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14811 /* Lookup the template functions now that we know what the
14812 scope is. */
14813 tree scope = TREE_OPERAND (member, 0);
14814 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14815 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14816 member = lookup_qualified_name (scope, tmpl,
14817 /*is_type_p=*/false,
14818 /*complain=*/false);
14819 if (BASELINK_P (member))
14821 BASELINK_FUNCTIONS (member)
14822 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14823 args);
14824 member = (adjust_result_of_qualified_name_lookup
14825 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14826 object_type));
14828 else
14830 qualified_name_lookup_error (scope, tmpl, member,
14831 input_location);
14832 RETURN (error_mark_node);
14835 else if (TREE_CODE (member) == SCOPE_REF
14836 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14837 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14839 if (complain & tf_error)
14841 if (TYPE_P (TREE_OPERAND (member, 0)))
14842 error ("%qT is not a class or namespace",
14843 TREE_OPERAND (member, 0));
14844 else
14845 error ("%qD is not a class or namespace",
14846 TREE_OPERAND (member, 0));
14848 RETURN (error_mark_node);
14850 else if (TREE_CODE (member) == FIELD_DECL)
14851 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14853 RETURN (finish_class_member_access_expr (object, member,
14854 /*template_p=*/false,
14855 complain));
14858 case THROW_EXPR:
14859 RETURN (build_throw
14860 (RECUR (TREE_OPERAND (t, 0))));
14862 case CONSTRUCTOR:
14864 vec<constructor_elt, va_gc> *n;
14865 constructor_elt *ce;
14866 unsigned HOST_WIDE_INT idx;
14867 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14868 bool process_index_p;
14869 int newlen;
14870 bool need_copy_p = false;
14871 tree r;
14873 if (type == error_mark_node)
14874 RETURN (error_mark_node);
14876 /* digest_init will do the wrong thing if we let it. */
14877 if (type && TYPE_PTRMEMFUNC_P (type))
14878 RETURN (t);
14880 /* We do not want to process the index of aggregate
14881 initializers as they are identifier nodes which will be
14882 looked up by digest_init. */
14883 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14885 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14886 newlen = vec_safe_length (n);
14887 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14889 if (ce->index && process_index_p
14890 /* An identifier index is looked up in the type
14891 being initialized, not the current scope. */
14892 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14893 ce->index = RECUR (ce->index);
14895 if (PACK_EXPANSION_P (ce->value))
14897 /* Substitute into the pack expansion. */
14898 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14899 in_decl);
14901 if (ce->value == error_mark_node
14902 || PACK_EXPANSION_P (ce->value))
14904 else if (TREE_VEC_LENGTH (ce->value) == 1)
14905 /* Just move the argument into place. */
14906 ce->value = TREE_VEC_ELT (ce->value, 0);
14907 else
14909 /* Update the length of the final CONSTRUCTOR
14910 arguments vector, and note that we will need to
14911 copy.*/
14912 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14913 need_copy_p = true;
14916 else
14917 ce->value = RECUR (ce->value);
14920 if (need_copy_p)
14922 vec<constructor_elt, va_gc> *old_n = n;
14924 vec_alloc (n, newlen);
14925 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14927 if (TREE_CODE (ce->value) == TREE_VEC)
14929 int i, len = TREE_VEC_LENGTH (ce->value);
14930 for (i = 0; i < len; ++i)
14931 CONSTRUCTOR_APPEND_ELT (n, 0,
14932 TREE_VEC_ELT (ce->value, i));
14934 else
14935 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14939 r = build_constructor (init_list_type_node, n);
14940 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14942 if (TREE_HAS_CONSTRUCTOR (t))
14943 RETURN (finish_compound_literal (type, r, complain));
14945 TREE_TYPE (r) = type;
14946 RETURN (r);
14949 case TYPEID_EXPR:
14951 tree operand_0 = TREE_OPERAND (t, 0);
14952 if (TYPE_P (operand_0))
14954 operand_0 = tsubst (operand_0, args, complain, in_decl);
14955 RETURN (get_typeid (operand_0, complain));
14957 else
14959 operand_0 = RECUR (operand_0);
14960 RETURN (build_typeid (operand_0, complain));
14964 case VAR_DECL:
14965 if (!args)
14966 RETURN (t);
14967 else if (DECL_PACK_P (t))
14969 /* We don't build decls for an instantiation of a
14970 variadic capture proxy, we instantiate the elements
14971 when needed. */
14972 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14973 return RECUR (DECL_VALUE_EXPR (t));
14975 /* Fall through */
14977 case PARM_DECL:
14979 tree r = tsubst_copy (t, args, complain, in_decl);
14981 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14982 /* If the original type was a reference, we'll be wrapped in
14983 the appropriate INDIRECT_REF. */
14984 r = convert_from_reference (r);
14985 RETURN (r);
14988 case VA_ARG_EXPR:
14989 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14990 RECUR (TREE_OPERAND (t, 0)),
14991 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14993 case OFFSETOF_EXPR:
14994 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14996 case TRAIT_EXPR:
14998 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
14999 complain, in_decl);
15001 tree type2 = TRAIT_EXPR_TYPE2 (t);
15002 if (type2)
15003 type2 = tsubst (type2, args, complain, in_decl);
15005 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15008 case STMT_EXPR:
15010 tree old_stmt_expr = cur_stmt_expr;
15011 tree stmt_expr = begin_stmt_expr ();
15013 cur_stmt_expr = stmt_expr;
15014 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15015 integral_constant_expression_p);
15016 stmt_expr = finish_stmt_expr (stmt_expr, false);
15017 cur_stmt_expr = old_stmt_expr;
15019 /* If the resulting list of expression statement is empty,
15020 fold it further into void_zero_node. */
15021 if (empty_expr_stmt_p (stmt_expr))
15022 stmt_expr = void_zero_node;
15024 RETURN (stmt_expr);
15027 case LAMBDA_EXPR:
15029 tree r = build_lambda_expr ();
15031 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15032 LAMBDA_EXPR_CLOSURE (r) = type;
15033 CLASSTYPE_LAMBDA_EXPR (type) = r;
15035 LAMBDA_EXPR_LOCATION (r)
15036 = LAMBDA_EXPR_LOCATION (t);
15037 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15038 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15039 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15040 LAMBDA_EXPR_DISCRIMINATOR (r)
15041 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15042 /* For a function scope, we want to use tsubst so that we don't
15043 complain about referring to an auto function before its return
15044 type has been deduced. Otherwise, we want to use tsubst_copy so
15045 that we look up the existing field/parameter/variable rather
15046 than build a new one. */
15047 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15048 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15049 scope = tsubst (scope, args, complain, in_decl);
15050 else if (scope && TREE_CODE (scope) == PARM_DECL)
15052 /* Look up the parameter we want directly, as tsubst_copy
15053 doesn't do what we need. */
15054 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15055 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15056 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15057 parm = DECL_CHAIN (parm);
15058 scope = parm;
15059 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15060 if (DECL_CONTEXT (scope) == NULL_TREE)
15061 DECL_CONTEXT (scope) = fn;
15063 else
15064 scope = RECUR (scope);
15065 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15066 LAMBDA_EXPR_RETURN_TYPE (r)
15067 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15069 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15070 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15072 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15073 determine_visibility (TYPE_NAME (type));
15074 /* Now that we know visibility, instantiate the type so we have a
15075 declaration of the op() for later calls to lambda_function. */
15076 complete_type (type);
15078 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15080 RETURN (build_lambda_object (r));
15083 case TARGET_EXPR:
15084 /* We can get here for a constant initializer of non-dependent type.
15085 FIXME stop folding in cp_parser_initializer_clause. */
15087 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15088 complain);
15089 RETURN (r);
15092 case TRANSACTION_EXPR:
15093 RETURN (tsubst_expr(t, args, complain, in_decl,
15094 integral_constant_expression_p));
15096 case PAREN_EXPR:
15097 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15099 case VEC_PERM_EXPR:
15100 RETURN (build_x_vec_perm_expr (input_location,
15101 RECUR (TREE_OPERAND (t, 0)),
15102 RECUR (TREE_OPERAND (t, 1)),
15103 RECUR (TREE_OPERAND (t, 2)),
15104 complain));
15106 default:
15107 /* Handle Objective-C++ constructs, if appropriate. */
15109 tree subst
15110 = objcp_tsubst_copy_and_build (t, args, complain,
15111 in_decl, /*function_p=*/false);
15112 if (subst)
15113 RETURN (subst);
15115 RETURN (tsubst_copy (t, args, complain, in_decl));
15118 #undef RECUR
15119 #undef RETURN
15120 out:
15121 input_location = loc;
15122 return retval;
15125 /* Verify that the instantiated ARGS are valid. For type arguments,
15126 make sure that the type's linkage is ok. For non-type arguments,
15127 make sure they are constants if they are integral or enumerations.
15128 Emit an error under control of COMPLAIN, and return TRUE on error. */
15130 static bool
15131 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15133 if (dependent_template_arg_p (t))
15134 return false;
15135 if (ARGUMENT_PACK_P (t))
15137 tree vec = ARGUMENT_PACK_ARGS (t);
15138 int len = TREE_VEC_LENGTH (vec);
15139 bool result = false;
15140 int i;
15142 for (i = 0; i < len; ++i)
15143 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15144 result = true;
15145 return result;
15147 else if (TYPE_P (t))
15149 /* [basic.link]: A name with no linkage (notably, the name
15150 of a class or enumeration declared in a local scope)
15151 shall not be used to declare an entity with linkage.
15152 This implies that names with no linkage cannot be used as
15153 template arguments
15155 DR 757 relaxes this restriction for C++0x. */
15156 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15157 : no_linkage_check (t, /*relaxed_p=*/false));
15159 if (nt)
15161 /* DR 488 makes use of a type with no linkage cause
15162 type deduction to fail. */
15163 if (complain & tf_error)
15165 if (TYPE_ANONYMOUS_P (nt))
15166 error ("%qT is/uses anonymous type", t);
15167 else
15168 error ("template argument for %qD uses local type %qT",
15169 tmpl, t);
15171 return true;
15173 /* In order to avoid all sorts of complications, we do not
15174 allow variably-modified types as template arguments. */
15175 else if (variably_modified_type_p (t, NULL_TREE))
15177 if (complain & tf_error)
15178 error ("%qT is a variably modified type", t);
15179 return true;
15182 /* Class template and alias template arguments should be OK. */
15183 else if (DECL_TYPE_TEMPLATE_P (t))
15185 /* A non-type argument of integral or enumerated type must be a
15186 constant. */
15187 else if (TREE_TYPE (t)
15188 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15189 && !TREE_CONSTANT (t))
15191 if (complain & tf_error)
15192 error ("integral expression %qE is not constant", t);
15193 return true;
15195 return false;
15198 static bool
15199 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15201 int ix, len = DECL_NTPARMS (tmpl);
15202 bool result = false;
15204 for (ix = 0; ix != len; ix++)
15206 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15207 result = true;
15209 if (result && (complain & tf_error))
15210 error (" trying to instantiate %qD", tmpl);
15211 return result;
15214 /* We're out of SFINAE context now, so generate diagnostics for the access
15215 errors we saw earlier when instantiating D from TMPL and ARGS. */
15217 static void
15218 recheck_decl_substitution (tree d, tree tmpl, tree args)
15220 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15221 tree type = TREE_TYPE (pattern);
15222 location_t loc = input_location;
15224 push_access_scope (d);
15225 push_deferring_access_checks (dk_no_deferred);
15226 input_location = DECL_SOURCE_LOCATION (pattern);
15227 tsubst (type, args, tf_warning_or_error, d);
15228 input_location = loc;
15229 pop_deferring_access_checks ();
15230 pop_access_scope (d);
15233 /* Instantiate the indicated variable, function, or alias template TMPL with
15234 the template arguments in TARG_PTR. */
15236 static tree
15237 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15239 tree targ_ptr = orig_args;
15240 tree fndecl;
15241 tree gen_tmpl;
15242 tree spec;
15243 bool access_ok = true;
15245 if (tmpl == error_mark_node)
15246 return error_mark_node;
15248 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15250 /* If this function is a clone, handle it specially. */
15251 if (DECL_CLONED_FUNCTION_P (tmpl))
15253 tree spec;
15254 tree clone;
15256 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15257 DECL_CLONED_FUNCTION. */
15258 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15259 targ_ptr, complain);
15260 if (spec == error_mark_node)
15261 return error_mark_node;
15263 /* Look for the clone. */
15264 FOR_EACH_CLONE (clone, spec)
15265 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15266 return clone;
15267 /* We should always have found the clone by now. */
15268 gcc_unreachable ();
15269 return NULL_TREE;
15272 if (targ_ptr == error_mark_node)
15273 return error_mark_node;
15275 /* Check to see if we already have this specialization. */
15276 gen_tmpl = most_general_template (tmpl);
15277 if (tmpl != gen_tmpl)
15278 /* The TMPL is a partial instantiation. To get a full set of
15279 arguments we must add the arguments used to perform the
15280 partial instantiation. */
15281 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15282 targ_ptr);
15284 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15285 but it doesn't seem to be on the hot path. */
15286 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15288 gcc_assert (tmpl == gen_tmpl
15289 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15290 == spec)
15291 || fndecl == NULL_TREE);
15293 if (spec != NULL_TREE)
15295 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15297 if (complain & tf_error)
15298 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15299 return error_mark_node;
15301 return spec;
15304 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15305 complain))
15306 return error_mark_node;
15308 /* We are building a FUNCTION_DECL, during which the access of its
15309 parameters and return types have to be checked. However this
15310 FUNCTION_DECL which is the desired context for access checking
15311 is not built yet. We solve this chicken-and-egg problem by
15312 deferring all checks until we have the FUNCTION_DECL. */
15313 push_deferring_access_checks (dk_deferred);
15315 /* Instantiation of the function happens in the context of the function
15316 template, not the context of the overload resolution we're doing. */
15317 push_to_top_level ();
15318 /* If there are dependent arguments, e.g. because we're doing partial
15319 ordering, make sure processing_template_decl stays set. */
15320 if (uses_template_parms (targ_ptr))
15321 ++processing_template_decl;
15322 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15324 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15325 complain, gen_tmpl);
15326 push_nested_class (ctx);
15328 /* Substitute template parameters to obtain the specialization. */
15329 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15330 targ_ptr, complain, gen_tmpl);
15331 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15332 pop_nested_class ();
15333 pop_from_top_level ();
15335 if (fndecl == error_mark_node)
15337 pop_deferring_access_checks ();
15338 return error_mark_node;
15341 /* The DECL_TI_TEMPLATE should always be the immediate parent
15342 template, not the most general template. */
15343 DECL_TI_TEMPLATE (fndecl) = tmpl;
15345 /* Now we know the specialization, compute access previously
15346 deferred. */
15347 push_access_scope (fndecl);
15348 if (!perform_deferred_access_checks (complain))
15349 access_ok = false;
15350 pop_access_scope (fndecl);
15351 pop_deferring_access_checks ();
15353 /* If we've just instantiated the main entry point for a function,
15354 instantiate all the alternate entry points as well. We do this
15355 by cloning the instantiation of the main entry point, not by
15356 instantiating the template clones. */
15357 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15358 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15360 if (!access_ok)
15362 if (!(complain & tf_error))
15364 /* Remember to reinstantiate when we're out of SFINAE so the user
15365 can see the errors. */
15366 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15368 return error_mark_node;
15370 return fndecl;
15373 /* Wrapper for instantiate_template_1. */
15375 tree
15376 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15378 tree ret;
15379 timevar_push (TV_TEMPLATE_INST);
15380 ret = instantiate_template_1 (tmpl, orig_args, complain);
15381 timevar_pop (TV_TEMPLATE_INST);
15382 return ret;
15385 /* Instantiate the alias template TMPL with ARGS. Also push a template
15386 instantiation level, which instantiate_template doesn't do because
15387 functions and variables have sufficient context established by the
15388 callers. */
15390 static tree
15391 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15393 struct pending_template *old_last_pend = last_pending_template;
15394 struct tinst_level *old_error_tinst = last_error_tinst_level;
15395 if (tmpl == error_mark_node || args == error_mark_node)
15396 return error_mark_node;
15397 tree tinst = build_tree_list (tmpl, args);
15398 if (!push_tinst_level (tinst))
15400 ggc_free (tinst);
15401 return error_mark_node;
15404 args =
15405 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15406 args, tmpl, complain,
15407 /*require_all_args=*/true,
15408 /*use_default_args=*/true);
15410 tree r = instantiate_template (tmpl, args, complain);
15411 pop_tinst_level ();
15412 /* We can't free this if a pending_template entry or last_error_tinst_level
15413 is pointing at it. */
15414 if (last_pending_template == old_last_pend
15415 && last_error_tinst_level == old_error_tinst)
15416 ggc_free (tinst);
15418 return r;
15421 /* PARM is a template parameter pack for FN. Returns true iff
15422 PARM is used in a deducible way in the argument list of FN. */
15424 static bool
15425 pack_deducible_p (tree parm, tree fn)
15427 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15428 for (; t; t = TREE_CHAIN (t))
15430 tree type = TREE_VALUE (t);
15431 tree packs;
15432 if (!PACK_EXPANSION_P (type))
15433 continue;
15434 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15435 packs; packs = TREE_CHAIN (packs))
15436 if (TREE_VALUE (packs) == parm)
15438 /* The template parameter pack is used in a function parameter
15439 pack. If this is the end of the parameter list, the
15440 template parameter pack is deducible. */
15441 if (TREE_CHAIN (t) == void_list_node)
15442 return true;
15443 else
15444 /* Otherwise, not. Well, it could be deduced from
15445 a non-pack parameter, but doing so would end up with
15446 a deduction mismatch, so don't bother. */
15447 return false;
15450 /* The template parameter pack isn't used in any function parameter
15451 packs, but it might be used deeper, e.g. tuple<Args...>. */
15452 return true;
15455 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15456 NARGS elements of the arguments that are being used when calling
15457 it. TARGS is a vector into which the deduced template arguments
15458 are placed.
15460 Returns either a FUNCTION_DECL for the matching specialization of FN or
15461 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15462 true, diagnostics will be printed to explain why it failed.
15464 If FN is a conversion operator, or we are trying to produce a specific
15465 specialization, RETURN_TYPE is the return type desired.
15467 The EXPLICIT_TARGS are explicit template arguments provided via a
15468 template-id.
15470 The parameter STRICT is one of:
15472 DEDUCE_CALL:
15473 We are deducing arguments for a function call, as in
15474 [temp.deduct.call].
15476 DEDUCE_CONV:
15477 We are deducing arguments for a conversion function, as in
15478 [temp.deduct.conv].
15480 DEDUCE_EXACT:
15481 We are deducing arguments when doing an explicit instantiation
15482 as in [temp.explicit], when determining an explicit specialization
15483 as in [temp.expl.spec], or when taking the address of a function
15484 template, as in [temp.deduct.funcaddr]. */
15486 tree
15487 fn_type_unification (tree fn,
15488 tree explicit_targs,
15489 tree targs,
15490 const tree *args,
15491 unsigned int nargs,
15492 tree return_type,
15493 unification_kind_t strict,
15494 int flags,
15495 bool explain_p,
15496 bool decltype_p)
15498 tree parms;
15499 tree fntype;
15500 tree decl = NULL_TREE;
15501 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15502 bool ok;
15503 static int deduction_depth;
15504 struct pending_template *old_last_pend = last_pending_template;
15505 struct tinst_level *old_error_tinst = last_error_tinst_level;
15506 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15507 tree tinst;
15508 tree r = error_mark_node;
15510 if (decltype_p)
15511 complain |= tf_decltype;
15513 /* In C++0x, it's possible to have a function template whose type depends
15514 on itself recursively. This is most obvious with decltype, but can also
15515 occur with enumeration scope (c++/48969). So we need to catch infinite
15516 recursion and reject the substitution at deduction time; this function
15517 will return error_mark_node for any repeated substitution.
15519 This also catches excessive recursion such as when f<N> depends on
15520 f<N-1> across all integers, and returns error_mark_node for all the
15521 substitutions back up to the initial one.
15523 This is, of course, not reentrant. */
15524 if (excessive_deduction_depth)
15525 return error_mark_node;
15526 tinst = build_tree_list (fn, NULL_TREE);
15527 ++deduction_depth;
15529 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15531 fntype = TREE_TYPE (fn);
15532 if (explicit_targs)
15534 /* [temp.deduct]
15536 The specified template arguments must match the template
15537 parameters in kind (i.e., type, nontype, template), and there
15538 must not be more arguments than there are parameters;
15539 otherwise type deduction fails.
15541 Nontype arguments must match the types of the corresponding
15542 nontype template parameters, or must be convertible to the
15543 types of the corresponding nontype parameters as specified in
15544 _temp.arg.nontype_, otherwise type deduction fails.
15546 All references in the function type of the function template
15547 to the corresponding template parameters are replaced by the
15548 specified template argument values. If a substitution in a
15549 template parameter or in the function type of the function
15550 template results in an invalid type, type deduction fails. */
15551 int i, len = TREE_VEC_LENGTH (tparms);
15552 location_t loc = input_location;
15553 bool incomplete = false;
15555 /* Adjust any explicit template arguments before entering the
15556 substitution context. */
15557 explicit_targs
15558 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15559 complain,
15560 /*require_all_args=*/false,
15561 /*use_default_args=*/false));
15562 if (explicit_targs == error_mark_node)
15563 goto fail;
15565 /* Substitute the explicit args into the function type. This is
15566 necessary so that, for instance, explicitly declared function
15567 arguments can match null pointed constants. If we were given
15568 an incomplete set of explicit args, we must not do semantic
15569 processing during substitution as we could create partial
15570 instantiations. */
15571 for (i = 0; i < len; i++)
15573 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15574 bool parameter_pack = false;
15575 tree targ = TREE_VEC_ELT (explicit_targs, i);
15577 /* Dig out the actual parm. */
15578 if (TREE_CODE (parm) == TYPE_DECL
15579 || TREE_CODE (parm) == TEMPLATE_DECL)
15581 parm = TREE_TYPE (parm);
15582 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15584 else if (TREE_CODE (parm) == PARM_DECL)
15586 parm = DECL_INITIAL (parm);
15587 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15590 if (!parameter_pack && targ == NULL_TREE)
15591 /* No explicit argument for this template parameter. */
15592 incomplete = true;
15594 if (parameter_pack && pack_deducible_p (parm, fn))
15596 /* Mark the argument pack as "incomplete". We could
15597 still deduce more arguments during unification.
15598 We remove this mark in type_unification_real. */
15599 if (targ)
15601 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15602 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15603 = ARGUMENT_PACK_ARGS (targ);
15606 /* We have some incomplete argument packs. */
15607 incomplete = true;
15611 TREE_VALUE (tinst) = explicit_targs;
15612 if (!push_tinst_level (tinst))
15614 excessive_deduction_depth = true;
15615 goto fail;
15617 processing_template_decl += incomplete;
15618 input_location = DECL_SOURCE_LOCATION (fn);
15619 /* Ignore any access checks; we'll see them again in
15620 instantiate_template and they might have the wrong
15621 access path at this point. */
15622 push_deferring_access_checks (dk_deferred);
15623 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15624 complain | tf_partial, NULL_TREE);
15625 pop_deferring_access_checks ();
15626 input_location = loc;
15627 processing_template_decl -= incomplete;
15628 pop_tinst_level ();
15630 if (fntype == error_mark_node)
15631 goto fail;
15633 /* Place the explicitly specified arguments in TARGS. */
15634 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15635 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15638 /* Never do unification on the 'this' parameter. */
15639 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15641 if (return_type)
15643 tree *new_args;
15645 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15646 new_args = XALLOCAVEC (tree, nargs + 1);
15647 new_args[0] = return_type;
15648 memcpy (new_args + 1, args, nargs * sizeof (tree));
15649 args = new_args;
15650 ++nargs;
15653 /* We allow incomplete unification without an error message here
15654 because the standard doesn't seem to explicitly prohibit it. Our
15655 callers must be ready to deal with unification failures in any
15656 event. */
15658 TREE_VALUE (tinst) = targs;
15659 /* If we aren't explaining yet, push tinst context so we can see where
15660 any errors (e.g. from class instantiations triggered by instantiation
15661 of default template arguments) come from. If we are explaining, this
15662 context is redundant. */
15663 if (!explain_p && !push_tinst_level (tinst))
15665 excessive_deduction_depth = true;
15666 goto fail;
15669 /* type_unification_real will pass back any access checks from default
15670 template argument substitution. */
15671 vec<deferred_access_check, va_gc> *checks;
15672 checks = NULL;
15674 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15675 targs, parms, args, nargs, /*subr=*/0,
15676 strict, flags, &checks, explain_p);
15677 if (!explain_p)
15678 pop_tinst_level ();
15679 if (!ok)
15680 goto fail;
15682 /* Now that we have bindings for all of the template arguments,
15683 ensure that the arguments deduced for the template template
15684 parameters have compatible template parameter lists. We cannot
15685 check this property before we have deduced all template
15686 arguments, because the template parameter types of a template
15687 template parameter might depend on prior template parameters
15688 deduced after the template template parameter. The following
15689 ill-formed example illustrates this issue:
15691 template<typename T, template<T> class C> void f(C<5>, T);
15693 template<int N> struct X {};
15695 void g() {
15696 f(X<5>(), 5l); // error: template argument deduction fails
15699 The template parameter list of 'C' depends on the template type
15700 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15701 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15702 time that we deduce 'C'. */
15703 if (!template_template_parm_bindings_ok_p
15704 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15706 unify_inconsistent_template_template_parameters (explain_p);
15707 goto fail;
15710 /* All is well so far. Now, check:
15712 [temp.deduct]
15714 When all template arguments have been deduced, all uses of
15715 template parameters in nondeduced contexts are replaced with
15716 the corresponding deduced argument values. If the
15717 substitution results in an invalid type, as described above,
15718 type deduction fails. */
15719 TREE_VALUE (tinst) = targs;
15720 if (!push_tinst_level (tinst))
15722 excessive_deduction_depth = true;
15723 goto fail;
15726 /* Also collect access checks from the instantiation. */
15727 reopen_deferring_access_checks (checks);
15729 decl = instantiate_template (fn, targs, complain);
15731 checks = get_deferred_access_checks ();
15732 pop_deferring_access_checks ();
15734 pop_tinst_level ();
15736 if (decl == error_mark_node)
15737 goto fail;
15739 /* Now perform any access checks encountered during substitution. */
15740 push_access_scope (decl);
15741 ok = perform_access_checks (checks, complain);
15742 pop_access_scope (decl);
15743 if (!ok)
15744 goto fail;
15746 /* If we're looking for an exact match, check that what we got
15747 is indeed an exact match. It might not be if some template
15748 parameters are used in non-deduced contexts. But don't check
15749 for an exact match if we have dependent template arguments;
15750 in that case we're doing partial ordering, and we already know
15751 that we have two candidates that will provide the actual type. */
15752 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
15754 tree substed = TREE_TYPE (decl);
15755 unsigned int i;
15757 tree sarg
15758 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15759 if (return_type)
15760 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15761 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15762 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15764 unify_type_mismatch (explain_p, args[i],
15765 TREE_VALUE (sarg));
15766 goto fail;
15770 r = decl;
15772 fail:
15773 --deduction_depth;
15774 if (excessive_deduction_depth)
15776 if (deduction_depth == 0)
15777 /* Reset once we're all the way out. */
15778 excessive_deduction_depth = false;
15781 /* We can't free this if a pending_template entry or last_error_tinst_level
15782 is pointing at it. */
15783 if (last_pending_template == old_last_pend
15784 && last_error_tinst_level == old_error_tinst)
15785 ggc_free (tinst);
15787 return r;
15790 /* Adjust types before performing type deduction, as described in
15791 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15792 sections are symmetric. PARM is the type of a function parameter
15793 or the return type of the conversion function. ARG is the type of
15794 the argument passed to the call, or the type of the value
15795 initialized with the result of the conversion function.
15796 ARG_EXPR is the original argument expression, which may be null. */
15798 static int
15799 maybe_adjust_types_for_deduction (unification_kind_t strict,
15800 tree* parm,
15801 tree* arg,
15802 tree arg_expr)
15804 int result = 0;
15806 switch (strict)
15808 case DEDUCE_CALL:
15809 break;
15811 case DEDUCE_CONV:
15813 /* Swap PARM and ARG throughout the remainder of this
15814 function; the handling is precisely symmetric since PARM
15815 will initialize ARG rather than vice versa. */
15816 tree* temp = parm;
15817 parm = arg;
15818 arg = temp;
15819 break;
15822 case DEDUCE_EXACT:
15823 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15824 too, but here handle it by stripping the reference from PARM
15825 rather than by adding it to ARG. */
15826 if (TREE_CODE (*parm) == REFERENCE_TYPE
15827 && TYPE_REF_IS_RVALUE (*parm)
15828 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15829 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15830 && TREE_CODE (*arg) == REFERENCE_TYPE
15831 && !TYPE_REF_IS_RVALUE (*arg))
15832 *parm = TREE_TYPE (*parm);
15833 /* Nothing else to do in this case. */
15834 return 0;
15836 default:
15837 gcc_unreachable ();
15840 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15842 /* [temp.deduct.call]
15844 If P is not a reference type:
15846 --If A is an array type, the pointer type produced by the
15847 array-to-pointer standard conversion (_conv.array_) is
15848 used in place of A for type deduction; otherwise,
15850 --If A is a function type, the pointer type produced by
15851 the function-to-pointer standard conversion
15852 (_conv.func_) is used in place of A for type deduction;
15853 otherwise,
15855 --If A is a cv-qualified type, the top level
15856 cv-qualifiers of A's type are ignored for type
15857 deduction. */
15858 if (TREE_CODE (*arg) == ARRAY_TYPE)
15859 *arg = build_pointer_type (TREE_TYPE (*arg));
15860 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15861 *arg = build_pointer_type (*arg);
15862 else
15863 *arg = TYPE_MAIN_VARIANT (*arg);
15866 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15867 of the form T&&, where T is a template parameter, and the argument
15868 is an lvalue, T is deduced as A& */
15869 if (TREE_CODE (*parm) == REFERENCE_TYPE
15870 && TYPE_REF_IS_RVALUE (*parm)
15871 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15872 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15873 && (arg_expr ? real_lvalue_p (arg_expr)
15874 /* try_one_overload doesn't provide an arg_expr, but
15875 functions are always lvalues. */
15876 : TREE_CODE (*arg) == FUNCTION_TYPE))
15877 *arg = build_reference_type (*arg);
15879 /* [temp.deduct.call]
15881 If P is a cv-qualified type, the top level cv-qualifiers
15882 of P's type are ignored for type deduction. If P is a
15883 reference type, the type referred to by P is used for
15884 type deduction. */
15885 *parm = TYPE_MAIN_VARIANT (*parm);
15886 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15888 *parm = TREE_TYPE (*parm);
15889 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15892 /* DR 322. For conversion deduction, remove a reference type on parm
15893 too (which has been swapped into ARG). */
15894 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15895 *arg = TREE_TYPE (*arg);
15897 return result;
15900 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15901 template which does contain any deducible template parameters; check if
15902 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15903 unify_one_argument. */
15905 static int
15906 check_non_deducible_conversion (tree parm, tree arg, int strict,
15907 int flags, bool explain_p)
15909 tree type;
15911 if (!TYPE_P (arg))
15912 type = TREE_TYPE (arg);
15913 else
15914 type = arg;
15916 if (same_type_p (parm, type))
15917 return unify_success (explain_p);
15919 if (strict == DEDUCE_CONV)
15921 if (can_convert_arg (type, parm, NULL_TREE, flags,
15922 explain_p ? tf_warning_or_error : tf_none))
15923 return unify_success (explain_p);
15925 else if (strict != DEDUCE_EXACT)
15927 if (can_convert_arg (parm, type,
15928 TYPE_P (arg) ? NULL_TREE : arg,
15929 flags, explain_p ? tf_warning_or_error : tf_none))
15930 return unify_success (explain_p);
15933 if (strict == DEDUCE_EXACT)
15934 return unify_type_mismatch (explain_p, parm, arg);
15935 else
15936 return unify_arg_conversion (explain_p, parm, type, arg);
15939 static bool uses_deducible_template_parms (tree type);
15941 /* Returns true iff the expression EXPR is one from which a template
15942 argument can be deduced. In other words, if it's an undecorated
15943 use of a template non-type parameter. */
15945 static bool
15946 deducible_expression (tree expr)
15948 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15951 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15952 deducible way; that is, if it has a max value of <PARM> - 1. */
15954 static bool
15955 deducible_array_bound (tree domain)
15957 if (domain == NULL_TREE)
15958 return false;
15960 tree max = TYPE_MAX_VALUE (domain);
15961 if (TREE_CODE (max) != MINUS_EXPR)
15962 return false;
15964 return deducible_expression (TREE_OPERAND (max, 0));
15967 /* Returns true iff the template arguments ARGS use a template parameter
15968 in a deducible way. */
15970 static bool
15971 deducible_template_args (tree args)
15973 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15975 bool deducible;
15976 tree elt = TREE_VEC_ELT (args, i);
15977 if (ARGUMENT_PACK_P (elt))
15978 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15979 else
15981 if (PACK_EXPANSION_P (elt))
15982 elt = PACK_EXPANSION_PATTERN (elt);
15983 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15984 deducible = true;
15985 else if (TYPE_P (elt))
15986 deducible = uses_deducible_template_parms (elt);
15987 else
15988 deducible = deducible_expression (elt);
15990 if (deducible)
15991 return true;
15993 return false;
15996 /* Returns true iff TYPE contains any deducible references to template
15997 parameters, as per 14.8.2.5. */
15999 static bool
16000 uses_deducible_template_parms (tree type)
16002 if (PACK_EXPANSION_P (type))
16003 type = PACK_EXPANSION_PATTERN (type);
16005 /* T
16006 cv-list T
16007 TT<T>
16008 TT<i>
16009 TT<> */
16010 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16011 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16012 return true;
16014 /* T*
16016 T&& */
16017 if (POINTER_TYPE_P (type))
16018 return uses_deducible_template_parms (TREE_TYPE (type));
16020 /* T[integer-constant ]
16021 type [i] */
16022 if (TREE_CODE (type) == ARRAY_TYPE)
16023 return (uses_deducible_template_parms (TREE_TYPE (type))
16024 || deducible_array_bound (TYPE_DOMAIN (type)));
16026 /* T type ::*
16027 type T::*
16028 T T::*
16029 T (type ::*)()
16030 type (T::*)()
16031 type (type ::*)(T)
16032 type (T::*)(T)
16033 T (type ::*)(T)
16034 T (T::*)()
16035 T (T::*)(T) */
16036 if (TYPE_PTRMEM_P (type))
16037 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16038 || (uses_deducible_template_parms
16039 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16041 /* template-name <T> (where template-name refers to a class template)
16042 template-name <i> (where template-name refers to a class template) */
16043 if (CLASS_TYPE_P (type)
16044 && CLASSTYPE_TEMPLATE_INFO (type)
16045 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16046 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16047 (CLASSTYPE_TI_ARGS (type)));
16049 /* type (T)
16051 T(T) */
16052 if (TREE_CODE (type) == FUNCTION_TYPE
16053 || TREE_CODE (type) == METHOD_TYPE)
16055 if (uses_deducible_template_parms (TREE_TYPE (type)))
16056 return true;
16057 tree parm = TYPE_ARG_TYPES (type);
16058 if (TREE_CODE (type) == METHOD_TYPE)
16059 parm = TREE_CHAIN (parm);
16060 for (; parm; parm = TREE_CHAIN (parm))
16061 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16062 return true;
16065 return false;
16068 /* Subroutine of type_unification_real and unify_pack_expansion to
16069 handle unification of a single P/A pair. Parameters are as
16070 for those functions. */
16072 static int
16073 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16074 int subr, unification_kind_t strict, int flags,
16075 bool explain_p)
16077 tree arg_expr = NULL_TREE;
16078 int arg_strict;
16080 if (arg == error_mark_node || parm == error_mark_node)
16081 return unify_invalid (explain_p);
16082 if (arg == unknown_type_node)
16083 /* We can't deduce anything from this, but we might get all the
16084 template args from other function args. */
16085 return unify_success (explain_p);
16087 /* Implicit conversions (Clause 4) will be performed on a function
16088 argument to convert it to the type of the corresponding function
16089 parameter if the parameter type contains no template-parameters that
16090 participate in template argument deduction. */
16091 if (TYPE_P (parm) && !uses_template_parms (parm))
16092 /* For function parameters that contain no template-parameters at all,
16093 we have historically checked for convertibility in order to shortcut
16094 consideration of this candidate. */
16095 return check_non_deducible_conversion (parm, arg, strict, flags,
16096 explain_p);
16097 else if (strict == DEDUCE_CALL
16098 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16099 /* For function parameters with only non-deducible template parameters,
16100 just return. */
16101 return unify_success (explain_p);
16103 switch (strict)
16105 case DEDUCE_CALL:
16106 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16107 | UNIFY_ALLOW_MORE_CV_QUAL
16108 | UNIFY_ALLOW_DERIVED);
16109 break;
16111 case DEDUCE_CONV:
16112 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16113 break;
16115 case DEDUCE_EXACT:
16116 arg_strict = UNIFY_ALLOW_NONE;
16117 break;
16119 default:
16120 gcc_unreachable ();
16123 /* We only do these transformations if this is the top-level
16124 parameter_type_list in a call or declaration matching; in other
16125 situations (nested function declarators, template argument lists) we
16126 won't be comparing a type to an expression, and we don't do any type
16127 adjustments. */
16128 if (!subr)
16130 if (!TYPE_P (arg))
16132 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16133 if (type_unknown_p (arg))
16135 /* [temp.deduct.type] A template-argument can be
16136 deduced from a pointer to function or pointer
16137 to member function argument if the set of
16138 overloaded functions does not contain function
16139 templates and at most one of a set of
16140 overloaded functions provides a unique
16141 match. */
16143 if (resolve_overloaded_unification
16144 (tparms, targs, parm, arg, strict,
16145 arg_strict, explain_p))
16146 return unify_success (explain_p);
16147 return unify_overload_resolution_failure (explain_p, arg);
16150 arg_expr = arg;
16151 arg = unlowered_expr_type (arg);
16152 if (arg == error_mark_node)
16153 return unify_invalid (explain_p);
16156 arg_strict |=
16157 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16159 else
16160 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16161 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16163 /* For deduction from an init-list we need the actual list. */
16164 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16165 arg = arg_expr;
16166 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16169 /* Most parms like fn_type_unification.
16171 If SUBR is 1, we're being called recursively (to unify the
16172 arguments of a function or method parameter of a function
16173 template).
16175 CHECKS is a pointer to a vector of access checks encountered while
16176 substituting default template arguments. */
16178 static int
16179 type_unification_real (tree tparms,
16180 tree targs,
16181 tree xparms,
16182 const tree *xargs,
16183 unsigned int xnargs,
16184 int subr,
16185 unification_kind_t strict,
16186 int flags,
16187 vec<deferred_access_check, va_gc> **checks,
16188 bool explain_p)
16190 tree parm, arg;
16191 int i;
16192 int ntparms = TREE_VEC_LENGTH (tparms);
16193 int saw_undeduced = 0;
16194 tree parms;
16195 const tree *args;
16196 unsigned int nargs;
16197 unsigned int ia;
16199 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16200 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16201 gcc_assert (ntparms > 0);
16203 /* Reset the number of non-defaulted template arguments contained
16204 in TARGS. */
16205 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16207 again:
16208 parms = xparms;
16209 args = xargs;
16210 nargs = xnargs;
16212 ia = 0;
16213 while (parms && parms != void_list_node
16214 && ia < nargs)
16216 parm = TREE_VALUE (parms);
16218 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16219 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16220 /* For a function parameter pack that occurs at the end of the
16221 parameter-declaration-list, the type A of each remaining
16222 argument of the call is compared with the type P of the
16223 declarator-id of the function parameter pack. */
16224 break;
16226 parms = TREE_CHAIN (parms);
16228 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16229 /* For a function parameter pack that does not occur at the
16230 end of the parameter-declaration-list, the type of the
16231 parameter pack is a non-deduced context. */
16232 continue;
16234 arg = args[ia];
16235 ++ia;
16237 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16238 flags, explain_p))
16239 return 1;
16242 if (parms
16243 && parms != void_list_node
16244 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16246 /* Unify the remaining arguments with the pack expansion type. */
16247 tree argvec;
16248 tree parmvec = make_tree_vec (1);
16250 /* Allocate a TREE_VEC and copy in all of the arguments */
16251 argvec = make_tree_vec (nargs - ia);
16252 for (i = 0; ia < nargs; ++ia, ++i)
16253 TREE_VEC_ELT (argvec, i) = args[ia];
16255 /* Copy the parameter into parmvec. */
16256 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16257 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16258 /*subr=*/subr, explain_p))
16259 return 1;
16261 /* Advance to the end of the list of parameters. */
16262 parms = TREE_CHAIN (parms);
16265 /* Fail if we've reached the end of the parm list, and more args
16266 are present, and the parm list isn't variadic. */
16267 if (ia < nargs && parms == void_list_node)
16268 return unify_too_many_arguments (explain_p, nargs, ia);
16269 /* Fail if parms are left and they don't have default values. */
16270 if (parms && parms != void_list_node
16271 && TREE_PURPOSE (parms) == NULL_TREE)
16273 unsigned int count = nargs;
16274 tree p = parms;
16275 while (p && p != void_list_node)
16277 count++;
16278 p = TREE_CHAIN (p);
16280 return unify_too_few_arguments (explain_p, ia, count);
16283 if (!subr)
16285 tsubst_flags_t complain = (explain_p
16286 ? tf_warning_or_error
16287 : tf_none);
16289 for (i = 0; i < ntparms; i++)
16291 tree targ = TREE_VEC_ELT (targs, i);
16292 tree tparm = TREE_VEC_ELT (tparms, i);
16294 /* Clear the "incomplete" flags on all argument packs now so that
16295 substituting them into later default arguments works. */
16296 if (targ && ARGUMENT_PACK_P (targ))
16298 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16299 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16302 if (targ || tparm == error_mark_node)
16303 continue;
16304 tparm = TREE_VALUE (tparm);
16306 /* If this is an undeduced nontype parameter that depends on
16307 a type parameter, try another pass; its type may have been
16308 deduced from a later argument than the one from which
16309 this parameter can be deduced. */
16310 if (TREE_CODE (tparm) == PARM_DECL
16311 && uses_template_parms (TREE_TYPE (tparm))
16312 && !saw_undeduced++)
16313 goto again;
16315 /* Core issue #226 (C++0x) [temp.deduct]:
16317 If a template argument has not been deduced, its
16318 default template argument, if any, is used.
16320 When we are in C++98 mode, TREE_PURPOSE will either
16321 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16322 to explicitly check cxx_dialect here. */
16323 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16325 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16326 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16327 reopen_deferring_access_checks (*checks);
16328 location_t save_loc = input_location;
16329 if (DECL_P (parm))
16330 input_location = DECL_SOURCE_LOCATION (parm);
16331 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16332 arg = convert_template_argument (parm, arg, targs, complain,
16333 i, NULL_TREE);
16334 input_location = save_loc;
16335 *checks = get_deferred_access_checks ();
16336 pop_deferring_access_checks ();
16337 if (arg == error_mark_node)
16338 return 1;
16339 else
16341 TREE_VEC_ELT (targs, i) = arg;
16342 /* The position of the first default template argument,
16343 is also the number of non-defaulted arguments in TARGS.
16344 Record that. */
16345 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16346 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16347 continue;
16351 /* If the type parameter is a parameter pack, then it will
16352 be deduced to an empty parameter pack. */
16353 if (template_parameter_pack_p (tparm))
16355 tree arg;
16357 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16359 arg = make_node (NONTYPE_ARGUMENT_PACK);
16360 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16361 TREE_CONSTANT (arg) = 1;
16363 else
16364 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16366 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16368 TREE_VEC_ELT (targs, i) = arg;
16369 continue;
16372 return unify_parameter_deduction_failure (explain_p, tparm);
16375 #ifdef ENABLE_CHECKING
16376 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16377 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16378 #endif
16380 return unify_success (explain_p);
16383 /* Subroutine of type_unification_real. Args are like the variables
16384 at the call site. ARG is an overloaded function (or template-id);
16385 we try deducing template args from each of the overloads, and if
16386 only one succeeds, we go with that. Modifies TARGS and returns
16387 true on success. */
16389 static bool
16390 resolve_overloaded_unification (tree tparms,
16391 tree targs,
16392 tree parm,
16393 tree arg,
16394 unification_kind_t strict,
16395 int sub_strict,
16396 bool explain_p)
16398 tree tempargs = copy_node (targs);
16399 int good = 0;
16400 tree goodfn = NULL_TREE;
16401 bool addr_p;
16403 if (TREE_CODE (arg) == ADDR_EXPR)
16405 arg = TREE_OPERAND (arg, 0);
16406 addr_p = true;
16408 else
16409 addr_p = false;
16411 if (TREE_CODE (arg) == COMPONENT_REF)
16412 /* Handle `&x' where `x' is some static or non-static member
16413 function name. */
16414 arg = TREE_OPERAND (arg, 1);
16416 if (TREE_CODE (arg) == OFFSET_REF)
16417 arg = TREE_OPERAND (arg, 1);
16419 /* Strip baselink information. */
16420 if (BASELINK_P (arg))
16421 arg = BASELINK_FUNCTIONS (arg);
16423 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16425 /* If we got some explicit template args, we need to plug them into
16426 the affected templates before we try to unify, in case the
16427 explicit args will completely resolve the templates in question. */
16429 int ok = 0;
16430 tree expl_subargs = TREE_OPERAND (arg, 1);
16431 arg = TREE_OPERAND (arg, 0);
16433 for (; arg; arg = OVL_NEXT (arg))
16435 tree fn = OVL_CURRENT (arg);
16436 tree subargs, elem;
16438 if (TREE_CODE (fn) != TEMPLATE_DECL)
16439 continue;
16441 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16442 expl_subargs, NULL_TREE, tf_none,
16443 /*require_all_args=*/true,
16444 /*use_default_args=*/true);
16445 if (subargs != error_mark_node
16446 && !any_dependent_template_arguments_p (subargs))
16448 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
16449 if (try_one_overload (tparms, targs, tempargs, parm,
16450 elem, strict, sub_strict, addr_p, explain_p)
16451 && (!goodfn || !same_type_p (goodfn, elem)))
16453 goodfn = elem;
16454 ++good;
16457 else if (subargs)
16458 ++ok;
16460 /* If no templates (or more than one) are fully resolved by the
16461 explicit arguments, this template-id is a non-deduced context; it
16462 could still be OK if we deduce all template arguments for the
16463 enclosing call through other arguments. */
16464 if (good != 1)
16465 good = ok;
16467 else if (TREE_CODE (arg) != OVERLOAD
16468 && TREE_CODE (arg) != FUNCTION_DECL)
16469 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16470 -- but the deduction does not succeed because the expression is
16471 not just the function on its own. */
16472 return false;
16473 else
16474 for (; arg; arg = OVL_NEXT (arg))
16475 if (try_one_overload (tparms, targs, tempargs, parm,
16476 TREE_TYPE (OVL_CURRENT (arg)),
16477 strict, sub_strict, addr_p, explain_p)
16478 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16480 goodfn = OVL_CURRENT (arg);
16481 ++good;
16484 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16485 to function or pointer to member function argument if the set of
16486 overloaded functions does not contain function templates and at most
16487 one of a set of overloaded functions provides a unique match.
16489 So if we found multiple possibilities, we return success but don't
16490 deduce anything. */
16492 if (good == 1)
16494 int i = TREE_VEC_LENGTH (targs);
16495 for (; i--; )
16496 if (TREE_VEC_ELT (tempargs, i))
16497 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16499 if (good)
16500 return true;
16502 return false;
16505 /* Core DR 115: In contexts where deduction is done and fails, or in
16506 contexts where deduction is not done, if a template argument list is
16507 specified and it, along with any default template arguments, identifies
16508 a single function template specialization, then the template-id is an
16509 lvalue for the function template specialization. */
16511 tree
16512 resolve_nondeduced_context (tree orig_expr)
16514 tree expr, offset, baselink;
16515 bool addr;
16517 if (!type_unknown_p (orig_expr))
16518 return orig_expr;
16520 expr = orig_expr;
16521 addr = false;
16522 offset = NULL_TREE;
16523 baselink = NULL_TREE;
16525 if (TREE_CODE (expr) == ADDR_EXPR)
16527 expr = TREE_OPERAND (expr, 0);
16528 addr = true;
16530 if (TREE_CODE (expr) == OFFSET_REF)
16532 offset = expr;
16533 expr = TREE_OPERAND (expr, 1);
16535 if (BASELINK_P (expr))
16537 baselink = expr;
16538 expr = BASELINK_FUNCTIONS (expr);
16541 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16543 int good = 0;
16544 tree goodfn = NULL_TREE;
16546 /* If we got some explicit template args, we need to plug them into
16547 the affected templates before we try to unify, in case the
16548 explicit args will completely resolve the templates in question. */
16550 tree expl_subargs = TREE_OPERAND (expr, 1);
16551 tree arg = TREE_OPERAND (expr, 0);
16552 tree badfn = NULL_TREE;
16553 tree badargs = NULL_TREE;
16555 for (; arg; arg = OVL_NEXT (arg))
16557 tree fn = OVL_CURRENT (arg);
16558 tree subargs, elem;
16560 if (TREE_CODE (fn) != TEMPLATE_DECL)
16561 continue;
16563 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16564 expl_subargs, NULL_TREE, tf_none,
16565 /*require_all_args=*/true,
16566 /*use_default_args=*/true);
16567 if (subargs != error_mark_node
16568 && !any_dependent_template_arguments_p (subargs))
16570 elem = instantiate_template (fn, subargs, tf_none);
16571 if (elem == error_mark_node)
16573 badfn = fn;
16574 badargs = subargs;
16576 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16578 goodfn = elem;
16579 ++good;
16583 if (good == 1)
16585 mark_used (goodfn);
16586 expr = goodfn;
16587 if (baselink)
16588 expr = build_baselink (BASELINK_BINFO (baselink),
16589 BASELINK_ACCESS_BINFO (baselink),
16590 expr, BASELINK_OPTYPE (baselink));
16591 if (offset)
16593 tree base
16594 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16595 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16597 if (addr)
16598 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16599 return expr;
16601 else if (good == 0 && badargs)
16602 /* There were no good options and at least one bad one, so let the
16603 user know what the problem is. */
16604 instantiate_template (badfn, badargs, tf_warning_or_error);
16606 return orig_expr;
16609 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16610 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16611 different overloads deduce different arguments for a given parm.
16612 ADDR_P is true if the expression for which deduction is being
16613 performed was of the form "& fn" rather than simply "fn".
16615 Returns 1 on success. */
16617 static int
16618 try_one_overload (tree tparms,
16619 tree orig_targs,
16620 tree targs,
16621 tree parm,
16622 tree arg,
16623 unification_kind_t strict,
16624 int sub_strict,
16625 bool addr_p,
16626 bool explain_p)
16628 int nargs;
16629 tree tempargs;
16630 int i;
16632 if (arg == error_mark_node)
16633 return 0;
16635 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16636 to function or pointer to member function argument if the set of
16637 overloaded functions does not contain function templates and at most
16638 one of a set of overloaded functions provides a unique match.
16640 So if this is a template, just return success. */
16642 if (uses_template_parms (arg))
16643 return 1;
16645 if (TREE_CODE (arg) == METHOD_TYPE)
16646 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16647 else if (addr_p)
16648 arg = build_pointer_type (arg);
16650 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16652 /* We don't copy orig_targs for this because if we have already deduced
16653 some template args from previous args, unify would complain when we
16654 try to deduce a template parameter for the same argument, even though
16655 there isn't really a conflict. */
16656 nargs = TREE_VEC_LENGTH (targs);
16657 tempargs = make_tree_vec (nargs);
16659 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16660 return 0;
16662 /* First make sure we didn't deduce anything that conflicts with
16663 explicitly specified args. */
16664 for (i = nargs; i--; )
16666 tree elt = TREE_VEC_ELT (tempargs, i);
16667 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16669 if (!elt)
16670 /*NOP*/;
16671 else if (uses_template_parms (elt))
16672 /* Since we're unifying against ourselves, we will fill in
16673 template args used in the function parm list with our own
16674 template parms. Discard them. */
16675 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16676 else if (oldelt && !template_args_equal (oldelt, elt))
16677 return 0;
16680 for (i = nargs; i--; )
16682 tree elt = TREE_VEC_ELT (tempargs, i);
16684 if (elt)
16685 TREE_VEC_ELT (targs, i) = elt;
16688 return 1;
16691 /* PARM is a template class (perhaps with unbound template
16692 parameters). ARG is a fully instantiated type. If ARG can be
16693 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16694 TARGS are as for unify. */
16696 static tree
16697 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16698 bool explain_p)
16700 tree copy_of_targs;
16702 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16703 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16704 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16705 return NULL_TREE;
16707 /* We need to make a new template argument vector for the call to
16708 unify. If we used TARGS, we'd clutter it up with the result of
16709 the attempted unification, even if this class didn't work out.
16710 We also don't want to commit ourselves to all the unifications
16711 we've already done, since unification is supposed to be done on
16712 an argument-by-argument basis. In other words, consider the
16713 following pathological case:
16715 template <int I, int J, int K>
16716 struct S {};
16718 template <int I, int J>
16719 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16721 template <int I, int J, int K>
16722 void f(S<I, J, K>, S<I, I, I>);
16724 void g() {
16725 S<0, 0, 0> s0;
16726 S<0, 1, 2> s2;
16728 f(s0, s2);
16731 Now, by the time we consider the unification involving `s2', we
16732 already know that we must have `f<0, 0, 0>'. But, even though
16733 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16734 because there are two ways to unify base classes of S<0, 1, 2>
16735 with S<I, I, I>. If we kept the already deduced knowledge, we
16736 would reject the possibility I=1. */
16737 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16739 /* If unification failed, we're done. */
16740 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16741 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16742 return NULL_TREE;
16744 return arg;
16747 /* Given a template type PARM and a class type ARG, find the unique
16748 base type in ARG that is an instance of PARM. We do not examine
16749 ARG itself; only its base-classes. If there is not exactly one
16750 appropriate base class, return NULL_TREE. PARM may be the type of
16751 a partial specialization, as well as a plain template type. Used
16752 by unify. */
16754 static enum template_base_result
16755 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16756 bool explain_p, tree *result)
16758 tree rval = NULL_TREE;
16759 tree binfo;
16761 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16763 binfo = TYPE_BINFO (complete_type (arg));
16764 if (!binfo)
16766 /* The type could not be completed. */
16767 *result = NULL_TREE;
16768 return tbr_incomplete_type;
16771 /* Walk in inheritance graph order. The search order is not
16772 important, and this avoids multiple walks of virtual bases. */
16773 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16775 tree r = try_class_unification (tparms, targs, parm,
16776 BINFO_TYPE (binfo), explain_p);
16778 if (r)
16780 /* If there is more than one satisfactory baseclass, then:
16782 [temp.deduct.call]
16784 If they yield more than one possible deduced A, the type
16785 deduction fails.
16787 applies. */
16788 if (rval && !same_type_p (r, rval))
16790 *result = NULL_TREE;
16791 return tbr_ambiguous_baseclass;
16794 rval = r;
16798 *result = rval;
16799 return tbr_success;
16802 /* Returns the level of DECL, which declares a template parameter. */
16804 static int
16805 template_decl_level (tree decl)
16807 switch (TREE_CODE (decl))
16809 case TYPE_DECL:
16810 case TEMPLATE_DECL:
16811 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16813 case PARM_DECL:
16814 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16816 default:
16817 gcc_unreachable ();
16819 return 0;
16822 /* Decide whether ARG can be unified with PARM, considering only the
16823 cv-qualifiers of each type, given STRICT as documented for unify.
16824 Returns nonzero iff the unification is OK on that basis. */
16826 static int
16827 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16829 int arg_quals = cp_type_quals (arg);
16830 int parm_quals = cp_type_quals (parm);
16832 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16833 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16835 /* Although a CVR qualifier is ignored when being applied to a
16836 substituted template parameter ([8.3.2]/1 for example), that
16837 does not allow us to unify "const T" with "int&" because both
16838 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16839 It is ok when we're allowing additional CV qualifiers
16840 at the outer level [14.8.2.1]/3,1st bullet. */
16841 if ((TREE_CODE (arg) == REFERENCE_TYPE
16842 || TREE_CODE (arg) == FUNCTION_TYPE
16843 || TREE_CODE (arg) == METHOD_TYPE)
16844 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16845 return 0;
16847 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16848 && (parm_quals & TYPE_QUAL_RESTRICT))
16849 return 0;
16852 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16853 && (arg_quals & parm_quals) != parm_quals)
16854 return 0;
16856 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16857 && (parm_quals & arg_quals) != arg_quals)
16858 return 0;
16860 return 1;
16863 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16864 void
16865 template_parm_level_and_index (tree parm, int* level, int* index)
16867 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16868 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16869 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16871 *index = TEMPLATE_TYPE_IDX (parm);
16872 *level = TEMPLATE_TYPE_LEVEL (parm);
16874 else
16876 *index = TEMPLATE_PARM_IDX (parm);
16877 *level = TEMPLATE_PARM_LEVEL (parm);
16881 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16882 do { \
16883 if (unify (TP, TA, P, A, S, EP)) \
16884 return 1; \
16885 } while (0);
16887 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16888 expansion at the end of PACKED_PARMS. Returns 0 if the type
16889 deduction succeeds, 1 otherwise. STRICT is the same as in
16890 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16891 call argument list. We'll need to adjust the arguments to make them
16892 types. SUBR tells us if this is from a recursive call to
16893 type_unification_real, or for comparing two template argument
16894 lists. */
16896 static int
16897 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16898 tree packed_args, unification_kind_t strict,
16899 bool subr, bool explain_p)
16901 tree parm
16902 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16903 tree pattern = PACK_EXPANSION_PATTERN (parm);
16904 tree pack, packs = NULL_TREE;
16905 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16907 packed_args = expand_template_argument_pack (packed_args);
16909 int len = TREE_VEC_LENGTH (packed_args);
16911 /* Determine the parameter packs we will be deducing from the
16912 pattern, and record their current deductions. */
16913 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16914 pack; pack = TREE_CHAIN (pack))
16916 tree parm_pack = TREE_VALUE (pack);
16917 int idx, level;
16919 /* Determine the index and level of this parameter pack. */
16920 template_parm_level_and_index (parm_pack, &level, &idx);
16922 /* Keep track of the parameter packs and their corresponding
16923 argument packs. */
16924 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16925 TREE_TYPE (packs) = make_tree_vec (len - start);
16928 /* Loop through all of the arguments that have not yet been
16929 unified and unify each with the pattern. */
16930 for (i = start; i < len; i++)
16932 tree parm;
16933 bool any_explicit = false;
16934 tree arg = TREE_VEC_ELT (packed_args, i);
16936 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16937 or the element of its argument pack at the current index if
16938 this argument was explicitly specified. */
16939 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16941 int idx, level;
16942 tree arg, pargs;
16943 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16945 arg = NULL_TREE;
16946 if (TREE_VALUE (pack)
16947 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16948 && (i - start < TREE_VEC_LENGTH (pargs)))
16950 any_explicit = true;
16951 arg = TREE_VEC_ELT (pargs, i - start);
16953 TMPL_ARG (targs, level, idx) = arg;
16956 /* If we had explicit template arguments, substitute them into the
16957 pattern before deduction. */
16958 if (any_explicit)
16960 /* Some arguments might still be unspecified or dependent. */
16961 bool dependent;
16962 ++processing_template_decl;
16963 dependent = any_dependent_template_arguments_p (targs);
16964 if (!dependent)
16965 --processing_template_decl;
16966 parm = tsubst (pattern, targs,
16967 explain_p ? tf_warning_or_error : tf_none,
16968 NULL_TREE);
16969 if (dependent)
16970 --processing_template_decl;
16971 if (parm == error_mark_node)
16972 return 1;
16974 else
16975 parm = pattern;
16977 /* Unify the pattern with the current argument. */
16978 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16979 LOOKUP_IMPLICIT, explain_p))
16980 return 1;
16982 /* For each parameter pack, collect the deduced value. */
16983 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16985 int idx, level;
16986 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16988 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16989 TMPL_ARG (targs, level, idx);
16993 /* Verify that the results of unification with the parameter packs
16994 produce results consistent with what we've seen before, and make
16995 the deduced argument packs available. */
16996 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16998 tree old_pack = TREE_VALUE (pack);
16999 tree new_args = TREE_TYPE (pack);
17000 int i, len = TREE_VEC_LENGTH (new_args);
17001 int idx, level;
17002 bool nondeduced_p = false;
17004 /* By default keep the original deduced argument pack.
17005 If necessary, more specific code is going to update the
17006 resulting deduced argument later down in this function. */
17007 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17008 TMPL_ARG (targs, level, idx) = old_pack;
17010 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17011 actually deduce anything. */
17012 for (i = 0; i < len && !nondeduced_p; ++i)
17013 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17014 nondeduced_p = true;
17015 if (nondeduced_p)
17016 continue;
17018 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17020 /* If we had fewer function args than explicit template args,
17021 just use the explicits. */
17022 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17023 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17024 if (len < explicit_len)
17025 new_args = explicit_args;
17028 if (!old_pack)
17030 tree result;
17031 /* Build the deduced *_ARGUMENT_PACK. */
17032 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17034 result = make_node (NONTYPE_ARGUMENT_PACK);
17035 TREE_TYPE (result) =
17036 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17037 TREE_CONSTANT (result) = 1;
17039 else
17040 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17042 SET_ARGUMENT_PACK_ARGS (result, new_args);
17044 /* Note the deduced argument packs for this parameter
17045 pack. */
17046 TMPL_ARG (targs, level, idx) = result;
17048 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17049 && (ARGUMENT_PACK_ARGS (old_pack)
17050 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17052 /* We only had the explicitly-provided arguments before, but
17053 now we have a complete set of arguments. */
17054 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17056 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17057 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17058 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17060 else
17062 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17063 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17065 if (!comp_template_args_with_info (old_args, new_args,
17066 &bad_old_arg, &bad_new_arg))
17067 /* Inconsistent unification of this parameter pack. */
17068 return unify_parameter_pack_inconsistent (explain_p,
17069 bad_old_arg,
17070 bad_new_arg);
17074 return unify_success (explain_p);
17077 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17078 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17079 parameters and return value are as for unify. */
17081 static int
17082 unify_array_domain (tree tparms, tree targs,
17083 tree parm_dom, tree arg_dom,
17084 bool explain_p)
17086 tree parm_max;
17087 tree arg_max;
17088 bool parm_cst;
17089 bool arg_cst;
17091 /* Our representation of array types uses "N - 1" as the
17092 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17093 not an integer constant. We cannot unify arbitrarily
17094 complex expressions, so we eliminate the MINUS_EXPRs
17095 here. */
17096 parm_max = TYPE_MAX_VALUE (parm_dom);
17097 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17098 if (!parm_cst)
17100 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17101 parm_max = TREE_OPERAND (parm_max, 0);
17103 arg_max = TYPE_MAX_VALUE (arg_dom);
17104 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17105 if (!arg_cst)
17107 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17108 trying to unify the type of a variable with the type
17109 of a template parameter. For example:
17111 template <unsigned int N>
17112 void f (char (&) [N]);
17113 int g();
17114 void h(int i) {
17115 char a[g(i)];
17116 f(a);
17119 Here, the type of the ARG will be "int [g(i)]", and
17120 may be a SAVE_EXPR, etc. */
17121 if (TREE_CODE (arg_max) != MINUS_EXPR)
17122 return unify_vla_arg (explain_p, arg_dom);
17123 arg_max = TREE_OPERAND (arg_max, 0);
17126 /* If only one of the bounds used a MINUS_EXPR, compensate
17127 by adding one to the other bound. */
17128 if (parm_cst && !arg_cst)
17129 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17130 integer_type_node,
17131 parm_max,
17132 integer_one_node);
17133 else if (arg_cst && !parm_cst)
17134 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17135 integer_type_node,
17136 arg_max,
17137 integer_one_node);
17139 return unify (tparms, targs, parm_max, arg_max,
17140 UNIFY_ALLOW_INTEGER, explain_p);
17143 /* Deduce the value of template parameters. TPARMS is the (innermost)
17144 set of template parameters to a template. TARGS is the bindings
17145 for those template parameters, as determined thus far; TARGS may
17146 include template arguments for outer levels of template parameters
17147 as well. PARM is a parameter to a template function, or a
17148 subcomponent of that parameter; ARG is the corresponding argument.
17149 This function attempts to match PARM with ARG in a manner
17150 consistent with the existing assignments in TARGS. If more values
17151 are deduced, then TARGS is updated.
17153 Returns 0 if the type deduction succeeds, 1 otherwise. The
17154 parameter STRICT is a bitwise or of the following flags:
17156 UNIFY_ALLOW_NONE:
17157 Require an exact match between PARM and ARG.
17158 UNIFY_ALLOW_MORE_CV_QUAL:
17159 Allow the deduced ARG to be more cv-qualified (by qualification
17160 conversion) than ARG.
17161 UNIFY_ALLOW_LESS_CV_QUAL:
17162 Allow the deduced ARG to be less cv-qualified than ARG.
17163 UNIFY_ALLOW_DERIVED:
17164 Allow the deduced ARG to be a template base class of ARG,
17165 or a pointer to a template base class of the type pointed to by
17166 ARG.
17167 UNIFY_ALLOW_INTEGER:
17168 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17169 case for more information.
17170 UNIFY_ALLOW_OUTER_LEVEL:
17171 This is the outermost level of a deduction. Used to determine validity
17172 of qualification conversions. A valid qualification conversion must
17173 have const qualified pointers leading up to the inner type which
17174 requires additional CV quals, except at the outer level, where const
17175 is not required [conv.qual]. It would be normal to set this flag in
17176 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17177 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17178 This is the outermost level of a deduction, and PARM can be more CV
17179 qualified at this point.
17180 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17181 This is the outermost level of a deduction, and PARM can be less CV
17182 qualified at this point. */
17184 static int
17185 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17186 bool explain_p)
17188 int idx;
17189 tree targ;
17190 tree tparm;
17191 int strict_in = strict;
17193 /* I don't think this will do the right thing with respect to types.
17194 But the only case I've seen it in so far has been array bounds, where
17195 signedness is the only information lost, and I think that will be
17196 okay. */
17197 while (TREE_CODE (parm) == NOP_EXPR)
17198 parm = TREE_OPERAND (parm, 0);
17200 if (arg == error_mark_node)
17201 return unify_invalid (explain_p);
17202 if (arg == unknown_type_node
17203 || arg == init_list_type_node)
17204 /* We can't deduce anything from this, but we might get all the
17205 template args from other function args. */
17206 return unify_success (explain_p);
17208 /* If PARM uses template parameters, then we can't bail out here,
17209 even if ARG == PARM, since we won't record unifications for the
17210 template parameters. We might need them if we're trying to
17211 figure out which of two things is more specialized. */
17212 if (arg == parm && !uses_template_parms (parm))
17213 return unify_success (explain_p);
17215 /* Handle init lists early, so the rest of the function can assume
17216 we're dealing with a type. */
17217 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17219 tree elt, elttype;
17220 unsigned i;
17221 tree orig_parm = parm;
17223 /* Replace T with std::initializer_list<T> for deduction. */
17224 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17225 && flag_deduce_init_list)
17226 parm = listify (parm);
17228 if (!is_std_init_list (parm)
17229 && TREE_CODE (parm) != ARRAY_TYPE)
17230 /* We can only deduce from an initializer list argument if the
17231 parameter is std::initializer_list or an array; otherwise this
17232 is a non-deduced context. */
17233 return unify_success (explain_p);
17235 if (TREE_CODE (parm) == ARRAY_TYPE)
17236 elttype = TREE_TYPE (parm);
17237 else
17238 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17240 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17242 int elt_strict = strict;
17244 if (elt == error_mark_node)
17245 return unify_invalid (explain_p);
17247 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17249 tree type = TREE_TYPE (elt);
17250 /* It should only be possible to get here for a call. */
17251 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17252 elt_strict |= maybe_adjust_types_for_deduction
17253 (DEDUCE_CALL, &elttype, &type, elt);
17254 elt = type;
17257 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17258 explain_p);
17261 if (TREE_CODE (parm) == ARRAY_TYPE)
17263 /* Also deduce from the length of the initializer list. */
17264 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17265 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17266 if (TYPE_DOMAIN (parm) != NULL_TREE)
17267 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17268 idx, explain_p);
17271 /* If the std::initializer_list<T> deduction worked, replace the
17272 deduced A with std::initializer_list<A>. */
17273 if (orig_parm != parm)
17275 idx = TEMPLATE_TYPE_IDX (orig_parm);
17276 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17277 targ = listify (targ);
17278 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17280 return unify_success (explain_p);
17283 /* Immediately reject some pairs that won't unify because of
17284 cv-qualification mismatches. */
17285 if (TREE_CODE (arg) == TREE_CODE (parm)
17286 && TYPE_P (arg)
17287 /* It is the elements of the array which hold the cv quals of an array
17288 type, and the elements might be template type parms. We'll check
17289 when we recurse. */
17290 && TREE_CODE (arg) != ARRAY_TYPE
17291 /* We check the cv-qualifiers when unifying with template type
17292 parameters below. We want to allow ARG `const T' to unify with
17293 PARM `T' for example, when computing which of two templates
17294 is more specialized, for example. */
17295 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17296 && !check_cv_quals_for_unify (strict_in, arg, parm))
17297 return unify_cv_qual_mismatch (explain_p, parm, arg);
17299 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17300 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17301 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17302 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17303 strict &= ~UNIFY_ALLOW_DERIVED;
17304 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17305 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17307 switch (TREE_CODE (parm))
17309 case TYPENAME_TYPE:
17310 case SCOPE_REF:
17311 case UNBOUND_CLASS_TEMPLATE:
17312 /* In a type which contains a nested-name-specifier, template
17313 argument values cannot be deduced for template parameters used
17314 within the nested-name-specifier. */
17315 return unify_success (explain_p);
17317 case TEMPLATE_TYPE_PARM:
17318 case TEMPLATE_TEMPLATE_PARM:
17319 case BOUND_TEMPLATE_TEMPLATE_PARM:
17320 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17321 if (tparm == error_mark_node)
17322 return unify_invalid (explain_p);
17324 if (TEMPLATE_TYPE_LEVEL (parm)
17325 != template_decl_level (tparm))
17326 /* The PARM is not one we're trying to unify. Just check
17327 to see if it matches ARG. */
17329 if (TREE_CODE (arg) == TREE_CODE (parm)
17330 && (is_auto (parm) ? is_auto (arg)
17331 : same_type_p (parm, arg)))
17332 return unify_success (explain_p);
17333 else
17334 return unify_type_mismatch (explain_p, parm, arg);
17336 idx = TEMPLATE_TYPE_IDX (parm);
17337 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17338 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17339 if (tparm == error_mark_node)
17340 return unify_invalid (explain_p);
17342 /* Check for mixed types and values. */
17343 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17344 && TREE_CODE (tparm) != TYPE_DECL)
17345 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17346 && TREE_CODE (tparm) != TEMPLATE_DECL))
17347 gcc_unreachable ();
17349 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17351 /* ARG must be constructed from a template class or a template
17352 template parameter. */
17353 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17354 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17355 return unify_template_deduction_failure (explain_p, parm, arg);
17358 tree parmvec = TYPE_TI_ARGS (parm);
17359 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17360 tree full_argvec = add_to_template_args (targs, argvec);
17361 tree parm_parms
17362 = DECL_INNERMOST_TEMPLATE_PARMS
17363 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17364 int i, len;
17365 int parm_variadic_p = 0;
17367 /* The resolution to DR150 makes clear that default
17368 arguments for an N-argument may not be used to bind T
17369 to a template template parameter with fewer than N
17370 parameters. It is not safe to permit the binding of
17371 default arguments as an extension, as that may change
17372 the meaning of a conforming program. Consider:
17374 struct Dense { static const unsigned int dim = 1; };
17376 template <template <typename> class View,
17377 typename Block>
17378 void operator+(float, View<Block> const&);
17380 template <typename Block,
17381 unsigned int Dim = Block::dim>
17382 struct Lvalue_proxy { operator float() const; };
17384 void
17385 test_1d (void) {
17386 Lvalue_proxy<Dense> p;
17387 float b;
17388 b + p;
17391 Here, if Lvalue_proxy is permitted to bind to View, then
17392 the global operator+ will be used; if they are not, the
17393 Lvalue_proxy will be converted to float. */
17394 if (coerce_template_parms (parm_parms,
17395 full_argvec,
17396 TYPE_TI_TEMPLATE (parm),
17397 (explain_p
17398 ? tf_warning_or_error
17399 : tf_none),
17400 /*require_all_args=*/true,
17401 /*use_default_args=*/false)
17402 == error_mark_node)
17403 return 1;
17405 /* Deduce arguments T, i from TT<T> or TT<i>.
17406 We check each element of PARMVEC and ARGVEC individually
17407 rather than the whole TREE_VEC since they can have
17408 different number of elements. */
17410 parmvec = expand_template_argument_pack (parmvec);
17411 argvec = expand_template_argument_pack (argvec);
17413 len = TREE_VEC_LENGTH (parmvec);
17415 /* Check if the parameters end in a pack, making them
17416 variadic. */
17417 if (len > 0
17418 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17419 parm_variadic_p = 1;
17421 for (i = 0; i < len - parm_variadic_p; ++i)
17422 /* If the template argument list of P contains a pack
17423 expansion that is not the last template argument, the
17424 entire template argument list is a non-deduced
17425 context. */
17426 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17427 return unify_success (explain_p);
17429 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17430 return unify_too_few_arguments (explain_p,
17431 TREE_VEC_LENGTH (argvec), len);
17433 for (i = 0; i < len - parm_variadic_p; ++i)
17435 RECUR_AND_CHECK_FAILURE (tparms, targs,
17436 TREE_VEC_ELT (parmvec, i),
17437 TREE_VEC_ELT (argvec, i),
17438 UNIFY_ALLOW_NONE, explain_p);
17441 if (parm_variadic_p
17442 && unify_pack_expansion (tparms, targs,
17443 parmvec, argvec,
17444 DEDUCE_EXACT,
17445 /*subr=*/true, explain_p))
17446 return 1;
17448 arg = TYPE_TI_TEMPLATE (arg);
17450 /* Fall through to deduce template name. */
17453 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17454 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17456 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17458 /* Simple cases: Value already set, does match or doesn't. */
17459 if (targ != NULL_TREE && template_args_equal (targ, arg))
17460 return unify_success (explain_p);
17461 else if (targ)
17462 return unify_inconsistency (explain_p, parm, targ, arg);
17464 else
17466 /* If PARM is `const T' and ARG is only `int', we don't have
17467 a match unless we are allowing additional qualification.
17468 If ARG is `const int' and PARM is just `T' that's OK;
17469 that binds `const int' to `T'. */
17470 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17471 arg, parm))
17472 return unify_cv_qual_mismatch (explain_p, parm, arg);
17474 /* Consider the case where ARG is `const volatile int' and
17475 PARM is `const T'. Then, T should be `volatile int'. */
17476 arg = cp_build_qualified_type_real
17477 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17478 if (arg == error_mark_node)
17479 return unify_invalid (explain_p);
17481 /* Simple cases: Value already set, does match or doesn't. */
17482 if (targ != NULL_TREE && same_type_p (targ, arg))
17483 return unify_success (explain_p);
17484 else if (targ)
17485 return unify_inconsistency (explain_p, parm, targ, arg);
17487 /* Make sure that ARG is not a variable-sized array. (Note
17488 that were talking about variable-sized arrays (like
17489 `int[n]'), rather than arrays of unknown size (like
17490 `int[]').) We'll get very confused by such a type since
17491 the bound of the array is not constant, and therefore
17492 not mangleable. Besides, such types are not allowed in
17493 ISO C++, so we can do as we please here. We do allow
17494 them for 'auto' deduction, since that isn't ABI-exposed. */
17495 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17496 return unify_vla_arg (explain_p, arg);
17498 /* Strip typedefs as in convert_template_argument. */
17499 arg = canonicalize_type_argument (arg, tf_none);
17502 /* If ARG is a parameter pack or an expansion, we cannot unify
17503 against it unless PARM is also a parameter pack. */
17504 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17505 && !template_parameter_pack_p (parm))
17506 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17508 /* If the argument deduction results is a METHOD_TYPE,
17509 then there is a problem.
17510 METHOD_TYPE doesn't map to any real C++ type the result of
17511 the deduction can not be of that type. */
17512 if (TREE_CODE (arg) == METHOD_TYPE)
17513 return unify_method_type_error (explain_p, arg);
17515 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17516 return unify_success (explain_p);
17518 case TEMPLATE_PARM_INDEX:
17519 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17520 if (tparm == error_mark_node)
17521 return unify_invalid (explain_p);
17523 if (TEMPLATE_PARM_LEVEL (parm)
17524 != template_decl_level (tparm))
17526 /* The PARM is not one we're trying to unify. Just check
17527 to see if it matches ARG. */
17528 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17529 && cp_tree_equal (parm, arg));
17530 if (result)
17531 unify_expression_unequal (explain_p, parm, arg);
17532 return result;
17535 idx = TEMPLATE_PARM_IDX (parm);
17536 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17538 if (targ)
17540 int x = !cp_tree_equal (targ, arg);
17541 if (x)
17542 unify_inconsistency (explain_p, parm, targ, arg);
17543 return x;
17546 /* [temp.deduct.type] If, in the declaration of a function template
17547 with a non-type template-parameter, the non-type
17548 template-parameter is used in an expression in the function
17549 parameter-list and, if the corresponding template-argument is
17550 deduced, the template-argument type shall match the type of the
17551 template-parameter exactly, except that a template-argument
17552 deduced from an array bound may be of any integral type.
17553 The non-type parameter might use already deduced type parameters. */
17554 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17555 if (!TREE_TYPE (arg))
17556 /* Template-parameter dependent expression. Just accept it for now.
17557 It will later be processed in convert_template_argument. */
17559 else if (same_type_p (TREE_TYPE (arg), tparm))
17560 /* OK */;
17561 else if ((strict & UNIFY_ALLOW_INTEGER)
17562 && CP_INTEGRAL_TYPE_P (tparm))
17563 /* Convert the ARG to the type of PARM; the deduced non-type
17564 template argument must exactly match the types of the
17565 corresponding parameter. */
17566 arg = fold (build_nop (tparm, arg));
17567 else if (uses_template_parms (tparm))
17568 /* We haven't deduced the type of this parameter yet. Try again
17569 later. */
17570 return unify_success (explain_p);
17571 else
17572 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17574 /* If ARG is a parameter pack or an expansion, we cannot unify
17575 against it unless PARM is also a parameter pack. */
17576 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17577 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17578 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17580 arg = strip_typedefs_expr (arg);
17581 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17582 return unify_success (explain_p);
17584 case PTRMEM_CST:
17586 /* A pointer-to-member constant can be unified only with
17587 another constant. */
17588 if (TREE_CODE (arg) != PTRMEM_CST)
17589 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17591 /* Just unify the class member. It would be useless (and possibly
17592 wrong, depending on the strict flags) to unify also
17593 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17594 arg refer to the same variable, even if through different
17595 classes. For instance:
17597 struct A { int x; };
17598 struct B : A { };
17600 Unification of &A::x and &B::x must succeed. */
17601 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17602 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17605 case POINTER_TYPE:
17607 if (!TYPE_PTR_P (arg))
17608 return unify_type_mismatch (explain_p, parm, arg);
17610 /* [temp.deduct.call]
17612 A can be another pointer or pointer to member type that can
17613 be converted to the deduced A via a qualification
17614 conversion (_conv.qual_).
17616 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17617 This will allow for additional cv-qualification of the
17618 pointed-to types if appropriate. */
17620 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17621 /* The derived-to-base conversion only persists through one
17622 level of pointers. */
17623 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17625 return unify (tparms, targs, TREE_TYPE (parm),
17626 TREE_TYPE (arg), strict, explain_p);
17629 case REFERENCE_TYPE:
17630 if (TREE_CODE (arg) != REFERENCE_TYPE)
17631 return unify_type_mismatch (explain_p, parm, arg);
17632 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17633 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17635 case ARRAY_TYPE:
17636 if (TREE_CODE (arg) != ARRAY_TYPE)
17637 return unify_type_mismatch (explain_p, parm, arg);
17638 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17639 != (TYPE_DOMAIN (arg) == NULL_TREE))
17640 return unify_type_mismatch (explain_p, parm, arg);
17641 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17642 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17643 if (TYPE_DOMAIN (parm) != NULL_TREE)
17644 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17645 TYPE_DOMAIN (arg), explain_p);
17646 return unify_success (explain_p);
17648 case REAL_TYPE:
17649 case COMPLEX_TYPE:
17650 case VECTOR_TYPE:
17651 case INTEGER_TYPE:
17652 case BOOLEAN_TYPE:
17653 case ENUMERAL_TYPE:
17654 case VOID_TYPE:
17655 case NULLPTR_TYPE:
17656 if (TREE_CODE (arg) != TREE_CODE (parm))
17657 return unify_type_mismatch (explain_p, parm, arg);
17659 /* We have already checked cv-qualification at the top of the
17660 function. */
17661 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17662 return unify_type_mismatch (explain_p, parm, arg);
17664 /* As far as unification is concerned, this wins. Later checks
17665 will invalidate it if necessary. */
17666 return unify_success (explain_p);
17668 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17669 /* Type INTEGER_CST can come from ordinary constant template args. */
17670 case INTEGER_CST:
17671 while (TREE_CODE (arg) == NOP_EXPR)
17672 arg = TREE_OPERAND (arg, 0);
17674 if (TREE_CODE (arg) != INTEGER_CST)
17675 return unify_template_argument_mismatch (explain_p, parm, arg);
17676 return (tree_int_cst_equal (parm, arg)
17677 ? unify_success (explain_p)
17678 : unify_template_argument_mismatch (explain_p, parm, arg));
17680 case TREE_VEC:
17682 int i, len, argslen;
17683 int parm_variadic_p = 0;
17685 if (TREE_CODE (arg) != TREE_VEC)
17686 return unify_template_argument_mismatch (explain_p, parm, arg);
17688 len = TREE_VEC_LENGTH (parm);
17689 argslen = TREE_VEC_LENGTH (arg);
17691 /* Check for pack expansions in the parameters. */
17692 for (i = 0; i < len; ++i)
17694 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17696 if (i == len - 1)
17697 /* We can unify against something with a trailing
17698 parameter pack. */
17699 parm_variadic_p = 1;
17700 else
17701 /* [temp.deduct.type]/9: If the template argument list of
17702 P contains a pack expansion that is not the last
17703 template argument, the entire template argument list
17704 is a non-deduced context. */
17705 return unify_success (explain_p);
17709 /* If we don't have enough arguments to satisfy the parameters
17710 (not counting the pack expression at the end), or we have
17711 too many arguments for a parameter list that doesn't end in
17712 a pack expression, we can't unify. */
17713 if (parm_variadic_p
17714 ? argslen < len - parm_variadic_p
17715 : argslen != len)
17716 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17718 /* Unify all of the parameters that precede the (optional)
17719 pack expression. */
17720 for (i = 0; i < len - parm_variadic_p; ++i)
17722 RECUR_AND_CHECK_FAILURE (tparms, targs,
17723 TREE_VEC_ELT (parm, i),
17724 TREE_VEC_ELT (arg, i),
17725 UNIFY_ALLOW_NONE, explain_p);
17727 if (parm_variadic_p)
17728 return unify_pack_expansion (tparms, targs, parm, arg,
17729 DEDUCE_EXACT,
17730 /*subr=*/true, explain_p);
17731 return unify_success (explain_p);
17734 case RECORD_TYPE:
17735 case UNION_TYPE:
17736 if (TREE_CODE (arg) != TREE_CODE (parm))
17737 return unify_type_mismatch (explain_p, parm, arg);
17739 if (TYPE_PTRMEMFUNC_P (parm))
17741 if (!TYPE_PTRMEMFUNC_P (arg))
17742 return unify_type_mismatch (explain_p, parm, arg);
17744 return unify (tparms, targs,
17745 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17746 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17747 strict, explain_p);
17750 if (CLASSTYPE_TEMPLATE_INFO (parm))
17752 tree t = NULL_TREE;
17754 if (strict_in & UNIFY_ALLOW_DERIVED)
17756 /* First, we try to unify the PARM and ARG directly. */
17757 t = try_class_unification (tparms, targs,
17758 parm, arg, explain_p);
17760 if (!t)
17762 /* Fallback to the special case allowed in
17763 [temp.deduct.call]:
17765 If P is a class, and P has the form
17766 template-id, then A can be a derived class of
17767 the deduced A. Likewise, if P is a pointer to
17768 a class of the form template-id, A can be a
17769 pointer to a derived class pointed to by the
17770 deduced A. */
17771 enum template_base_result r;
17772 r = get_template_base (tparms, targs, parm, arg,
17773 explain_p, &t);
17775 if (!t)
17776 return unify_no_common_base (explain_p, r, parm, arg);
17779 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17780 && (CLASSTYPE_TI_TEMPLATE (parm)
17781 == CLASSTYPE_TI_TEMPLATE (arg)))
17782 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17783 Then, we should unify `int' and `U'. */
17784 t = arg;
17785 else
17786 /* There's no chance of unification succeeding. */
17787 return unify_type_mismatch (explain_p, parm, arg);
17789 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17790 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17792 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17793 return unify_type_mismatch (explain_p, parm, arg);
17794 return unify_success (explain_p);
17796 case METHOD_TYPE:
17797 case FUNCTION_TYPE:
17799 unsigned int nargs;
17800 tree *args;
17801 tree a;
17802 unsigned int i;
17804 if (TREE_CODE (arg) != TREE_CODE (parm))
17805 return unify_type_mismatch (explain_p, parm, arg);
17807 /* CV qualifications for methods can never be deduced, they must
17808 match exactly. We need to check them explicitly here,
17809 because type_unification_real treats them as any other
17810 cv-qualified parameter. */
17811 if (TREE_CODE (parm) == METHOD_TYPE
17812 && (!check_cv_quals_for_unify
17813 (UNIFY_ALLOW_NONE,
17814 class_of_this_parm (arg),
17815 class_of_this_parm (parm))))
17816 return unify_cv_qual_mismatch (explain_p, parm, arg);
17818 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17819 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17821 nargs = list_length (TYPE_ARG_TYPES (arg));
17822 args = XALLOCAVEC (tree, nargs);
17823 for (a = TYPE_ARG_TYPES (arg), i = 0;
17824 a != NULL_TREE && a != void_list_node;
17825 a = TREE_CHAIN (a), ++i)
17826 args[i] = TREE_VALUE (a);
17827 nargs = i;
17829 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17830 args, nargs, 1, DEDUCE_EXACT,
17831 LOOKUP_NORMAL, NULL, explain_p);
17834 case OFFSET_TYPE:
17835 /* Unify a pointer to member with a pointer to member function, which
17836 deduces the type of the member as a function type. */
17837 if (TYPE_PTRMEMFUNC_P (arg))
17839 /* Check top-level cv qualifiers */
17840 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17841 return unify_cv_qual_mismatch (explain_p, parm, arg);
17843 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17844 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17845 UNIFY_ALLOW_NONE, explain_p);
17847 /* Determine the type of the function we are unifying against. */
17848 tree fntype = static_fn_type (arg);
17850 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17853 if (TREE_CODE (arg) != OFFSET_TYPE)
17854 return unify_type_mismatch (explain_p, parm, arg);
17855 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17856 TYPE_OFFSET_BASETYPE (arg),
17857 UNIFY_ALLOW_NONE, explain_p);
17858 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17859 strict, explain_p);
17861 case CONST_DECL:
17862 if (DECL_TEMPLATE_PARM_P (parm))
17863 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17864 if (arg != integral_constant_value (parm))
17865 return unify_template_argument_mismatch (explain_p, parm, arg);
17866 return unify_success (explain_p);
17868 case FIELD_DECL:
17869 case TEMPLATE_DECL:
17870 /* Matched cases are handled by the ARG == PARM test above. */
17871 return unify_template_argument_mismatch (explain_p, parm, arg);
17873 case VAR_DECL:
17874 /* A non-type template parameter that is a variable should be a
17875 an integral constant, in which case, it whould have been
17876 folded into its (constant) value. So we should not be getting
17877 a variable here. */
17878 gcc_unreachable ();
17880 case TYPE_ARGUMENT_PACK:
17881 case NONTYPE_ARGUMENT_PACK:
17882 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17883 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17885 case TYPEOF_TYPE:
17886 case DECLTYPE_TYPE:
17887 case UNDERLYING_TYPE:
17888 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17889 or UNDERLYING_TYPE nodes. */
17890 return unify_success (explain_p);
17892 case ERROR_MARK:
17893 /* Unification fails if we hit an error node. */
17894 return unify_invalid (explain_p);
17896 default:
17897 /* An unresolved overload is a nondeduced context. */
17898 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17899 return unify_success (explain_p);
17900 gcc_assert (EXPR_P (parm));
17902 /* We must be looking at an expression. This can happen with
17903 something like:
17905 template <int I>
17906 void foo(S<I>, S<I + 2>);
17908 This is a "nondeduced context":
17910 [deduct.type]
17912 The nondeduced contexts are:
17914 --A type that is a template-id in which one or more of
17915 the template-arguments is an expression that references
17916 a template-parameter.
17918 In these cases, we assume deduction succeeded, but don't
17919 actually infer any unifications. */
17921 if (!uses_template_parms (parm)
17922 && !template_args_equal (parm, arg))
17923 return unify_expression_unequal (explain_p, parm, arg);
17924 else
17925 return unify_success (explain_p);
17928 #undef RECUR_AND_CHECK_FAILURE
17930 /* Note that DECL can be defined in this translation unit, if
17931 required. */
17933 static void
17934 mark_definable (tree decl)
17936 tree clone;
17937 DECL_NOT_REALLY_EXTERN (decl) = 1;
17938 FOR_EACH_CLONE (clone, decl)
17939 DECL_NOT_REALLY_EXTERN (clone) = 1;
17942 /* Called if RESULT is explicitly instantiated, or is a member of an
17943 explicitly instantiated class. */
17945 void
17946 mark_decl_instantiated (tree result, int extern_p)
17948 SET_DECL_EXPLICIT_INSTANTIATION (result);
17950 /* If this entity has already been written out, it's too late to
17951 make any modifications. */
17952 if (TREE_ASM_WRITTEN (result))
17953 return;
17955 /* For anonymous namespace we don't need to do anything. */
17956 if (decl_anon_ns_mem_p (result))
17958 gcc_assert (!TREE_PUBLIC (result));
17959 return;
17962 if (TREE_CODE (result) != FUNCTION_DECL)
17963 /* The TREE_PUBLIC flag for function declarations will have been
17964 set correctly by tsubst. */
17965 TREE_PUBLIC (result) = 1;
17967 /* This might have been set by an earlier implicit instantiation. */
17968 DECL_COMDAT (result) = 0;
17970 if (extern_p)
17971 DECL_NOT_REALLY_EXTERN (result) = 0;
17972 else
17974 mark_definable (result);
17975 mark_needed (result);
17976 /* Always make artificials weak. */
17977 if (DECL_ARTIFICIAL (result) && flag_weak)
17978 comdat_linkage (result);
17979 /* For WIN32 we also want to put explicit instantiations in
17980 linkonce sections. */
17981 else if (TREE_PUBLIC (result))
17982 maybe_make_one_only (result);
17985 /* If EXTERN_P, then this function will not be emitted -- unless
17986 followed by an explicit instantiation, at which point its linkage
17987 will be adjusted. If !EXTERN_P, then this function will be
17988 emitted here. In neither circumstance do we want
17989 import_export_decl to adjust the linkage. */
17990 DECL_INTERFACE_KNOWN (result) = 1;
17993 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17994 important template arguments. If any are missing, we check whether
17995 they're important by using error_mark_node for substituting into any
17996 args that were used for partial ordering (the ones between ARGS and END)
17997 and seeing if it bubbles up. */
17999 static bool
18000 check_undeduced_parms (tree targs, tree args, tree end)
18002 bool found = false;
18003 int i;
18004 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18005 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18007 found = true;
18008 TREE_VEC_ELT (targs, i) = error_mark_node;
18010 if (found)
18012 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18013 if (substed == error_mark_node)
18014 return true;
18016 return false;
18019 /* Given two function templates PAT1 and PAT2, return:
18021 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18022 -1 if PAT2 is more specialized than PAT1.
18023 0 if neither is more specialized.
18025 LEN indicates the number of parameters we should consider
18026 (defaulted parameters should not be considered).
18028 The 1998 std underspecified function template partial ordering, and
18029 DR214 addresses the issue. We take pairs of arguments, one from
18030 each of the templates, and deduce them against each other. One of
18031 the templates will be more specialized if all the *other*
18032 template's arguments deduce against its arguments and at least one
18033 of its arguments *does* *not* deduce against the other template's
18034 corresponding argument. Deduction is done as for class templates.
18035 The arguments used in deduction have reference and top level cv
18036 qualifiers removed. Iff both arguments were originally reference
18037 types *and* deduction succeeds in both directions, an lvalue reference
18038 wins against an rvalue reference and otherwise the template
18039 with the more cv-qualified argument wins for that pairing (if
18040 neither is more cv-qualified, they both are equal). Unlike regular
18041 deduction, after all the arguments have been deduced in this way,
18042 we do *not* verify the deduced template argument values can be
18043 substituted into non-deduced contexts.
18045 The logic can be a bit confusing here, because we look at deduce1 and
18046 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18047 can find template arguments for pat1 to make arg1 look like arg2, that
18048 means that arg2 is at least as specialized as arg1. */
18051 more_specialized_fn (tree pat1, tree pat2, int len)
18053 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18054 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18055 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18056 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18057 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18058 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18059 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18060 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18061 tree origs1, origs2;
18062 bool lose1 = false;
18063 bool lose2 = false;
18065 /* Remove the this parameter from non-static member functions. If
18066 one is a non-static member function and the other is not a static
18067 member function, remove the first parameter from that function
18068 also. This situation occurs for operator functions where we
18069 locate both a member function (with this pointer) and non-member
18070 operator (with explicit first operand). */
18071 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18073 len--; /* LEN is the number of significant arguments for DECL1 */
18074 args1 = TREE_CHAIN (args1);
18075 if (!DECL_STATIC_FUNCTION_P (decl2))
18076 args2 = TREE_CHAIN (args2);
18078 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18080 args2 = TREE_CHAIN (args2);
18081 if (!DECL_STATIC_FUNCTION_P (decl1))
18083 len--;
18084 args1 = TREE_CHAIN (args1);
18088 /* If only one is a conversion operator, they are unordered. */
18089 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18090 return 0;
18092 /* Consider the return type for a conversion function */
18093 if (DECL_CONV_FN_P (decl1))
18095 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18096 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18097 len++;
18100 processing_template_decl++;
18102 origs1 = args1;
18103 origs2 = args2;
18105 while (len--
18106 /* Stop when an ellipsis is seen. */
18107 && args1 != NULL_TREE && args2 != NULL_TREE)
18109 tree arg1 = TREE_VALUE (args1);
18110 tree arg2 = TREE_VALUE (args2);
18111 int deduce1, deduce2;
18112 int quals1 = -1;
18113 int quals2 = -1;
18114 int ref1 = 0;
18115 int ref2 = 0;
18117 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18118 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18120 /* When both arguments are pack expansions, we need only
18121 unify the patterns themselves. */
18122 arg1 = PACK_EXPANSION_PATTERN (arg1);
18123 arg2 = PACK_EXPANSION_PATTERN (arg2);
18125 /* This is the last comparison we need to do. */
18126 len = 0;
18129 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18131 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18132 arg1 = TREE_TYPE (arg1);
18133 quals1 = cp_type_quals (arg1);
18136 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18138 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18139 arg2 = TREE_TYPE (arg2);
18140 quals2 = cp_type_quals (arg2);
18143 arg1 = TYPE_MAIN_VARIANT (arg1);
18144 arg2 = TYPE_MAIN_VARIANT (arg2);
18146 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18148 int i, len2 = list_length (args2);
18149 tree parmvec = make_tree_vec (1);
18150 tree argvec = make_tree_vec (len2);
18151 tree ta = args2;
18153 /* Setup the parameter vector, which contains only ARG1. */
18154 TREE_VEC_ELT (parmvec, 0) = arg1;
18156 /* Setup the argument vector, which contains the remaining
18157 arguments. */
18158 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18159 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18161 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18162 argvec, DEDUCE_EXACT,
18163 /*subr=*/true, /*explain_p=*/false)
18164 == 0);
18166 /* We cannot deduce in the other direction, because ARG1 is
18167 a pack expansion but ARG2 is not. */
18168 deduce2 = 0;
18170 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18172 int i, len1 = list_length (args1);
18173 tree parmvec = make_tree_vec (1);
18174 tree argvec = make_tree_vec (len1);
18175 tree ta = args1;
18177 /* Setup the parameter vector, which contains only ARG1. */
18178 TREE_VEC_ELT (parmvec, 0) = arg2;
18180 /* Setup the argument vector, which contains the remaining
18181 arguments. */
18182 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18183 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18185 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18186 argvec, DEDUCE_EXACT,
18187 /*subr=*/true, /*explain_p=*/false)
18188 == 0);
18190 /* We cannot deduce in the other direction, because ARG2 is
18191 a pack expansion but ARG1 is not.*/
18192 deduce1 = 0;
18195 else
18197 /* The normal case, where neither argument is a pack
18198 expansion. */
18199 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18200 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18201 == 0);
18202 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18203 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18204 == 0);
18207 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18208 arg2, then arg2 is not as specialized as arg1. */
18209 if (!deduce1)
18210 lose2 = true;
18211 if (!deduce2)
18212 lose1 = true;
18214 /* "If, for a given type, deduction succeeds in both directions
18215 (i.e., the types are identical after the transformations above)
18216 and both P and A were reference types (before being replaced with
18217 the type referred to above):
18218 - if the type from the argument template was an lvalue reference and
18219 the type from the parameter template was not, the argument type is
18220 considered to be more specialized than the other; otherwise,
18221 - if the type from the argument template is more cv-qualified
18222 than the type from the parameter template (as described above),
18223 the argument type is considered to be more specialized than the other;
18224 otherwise,
18225 - neither type is more specialized than the other." */
18227 if (deduce1 && deduce2)
18229 if (ref1 && ref2 && ref1 != ref2)
18231 if (ref1 > ref2)
18232 lose1 = true;
18233 else
18234 lose2 = true;
18236 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18238 if ((quals1 & quals2) == quals2)
18239 lose2 = true;
18240 if ((quals1 & quals2) == quals1)
18241 lose1 = true;
18245 if (lose1 && lose2)
18246 /* We've failed to deduce something in either direction.
18247 These must be unordered. */
18248 break;
18250 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18251 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18252 /* We have already processed all of the arguments in our
18253 handing of the pack expansion type. */
18254 len = 0;
18256 args1 = TREE_CHAIN (args1);
18257 args2 = TREE_CHAIN (args2);
18260 /* "In most cases, all template parameters must have values in order for
18261 deduction to succeed, but for partial ordering purposes a template
18262 parameter may remain without a value provided it is not used in the
18263 types being used for partial ordering."
18265 Thus, if we are missing any of the targs1 we need to substitute into
18266 origs1, then pat2 is not as specialized as pat1. This can happen when
18267 there is a nondeduced context. */
18268 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18269 lose2 = true;
18270 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18271 lose1 = true;
18273 processing_template_decl--;
18275 /* All things being equal, if the next argument is a pack expansion
18276 for one function but not for the other, prefer the
18277 non-variadic function. FIXME this is bogus; see c++/41958. */
18278 if (lose1 == lose2
18279 && args1 && TREE_VALUE (args1)
18280 && args2 && TREE_VALUE (args2))
18282 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18283 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18286 if (lose1 == lose2)
18287 return 0;
18288 else if (!lose1)
18289 return 1;
18290 else
18291 return -1;
18294 /* Determine which of two partial specializations of TMPL is more
18295 specialized.
18297 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18298 to the first partial specialization. The TREE_VALUE is the
18299 innermost set of template parameters for the partial
18300 specialization. PAT2 is similar, but for the second template.
18302 Return 1 if the first partial specialization is more specialized;
18303 -1 if the second is more specialized; 0 if neither is more
18304 specialized.
18306 See [temp.class.order] for information about determining which of
18307 two templates is more specialized. */
18309 static int
18310 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18312 tree targs;
18313 tree tmpl1, tmpl2;
18314 int winner = 0;
18315 bool any_deductions = false;
18317 tmpl1 = TREE_TYPE (pat1);
18318 tmpl2 = TREE_TYPE (pat2);
18320 /* Just like what happens for functions, if we are ordering between
18321 different class template specializations, we may encounter dependent
18322 types in the arguments, and we need our dependency check functions
18323 to behave correctly. */
18324 ++processing_template_decl;
18325 targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
18326 CLASSTYPE_TI_ARGS (tmpl1),
18327 CLASSTYPE_TI_ARGS (tmpl2));
18328 if (targs)
18330 --winner;
18331 any_deductions = true;
18334 targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
18335 CLASSTYPE_TI_ARGS (tmpl2),
18336 CLASSTYPE_TI_ARGS (tmpl1));
18337 if (targs)
18339 ++winner;
18340 any_deductions = true;
18342 --processing_template_decl;
18344 /* In the case of a tie where at least one of the class templates
18345 has a parameter pack at the end, the template with the most
18346 non-packed parameters wins. */
18347 if (winner == 0
18348 && any_deductions
18349 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18350 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18352 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18353 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18354 int len1 = TREE_VEC_LENGTH (args1);
18355 int len2 = TREE_VEC_LENGTH (args2);
18357 /* We don't count the pack expansion at the end. */
18358 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18359 --len1;
18360 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18361 --len2;
18363 if (len1 > len2)
18364 return 1;
18365 else if (len1 < len2)
18366 return -1;
18369 return winner;
18372 /* Return the template arguments that will produce the function signature
18373 DECL from the function template FN, with the explicit template
18374 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18375 also match. Return NULL_TREE if no satisfactory arguments could be
18376 found. */
18378 static tree
18379 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18381 int ntparms = DECL_NTPARMS (fn);
18382 tree targs = make_tree_vec (ntparms);
18383 tree decl_type = TREE_TYPE (decl);
18384 tree decl_arg_types;
18385 tree *args;
18386 unsigned int nargs, ix;
18387 tree arg;
18389 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18391 /* Never do unification on the 'this' parameter. */
18392 decl_arg_types = skip_artificial_parms_for (decl,
18393 TYPE_ARG_TYPES (decl_type));
18395 nargs = list_length (decl_arg_types);
18396 args = XALLOCAVEC (tree, nargs);
18397 for (arg = decl_arg_types, ix = 0;
18398 arg != NULL_TREE && arg != void_list_node;
18399 arg = TREE_CHAIN (arg), ++ix)
18400 args[ix] = TREE_VALUE (arg);
18402 if (fn_type_unification (fn, explicit_args, targs,
18403 args, ix,
18404 (check_rettype || DECL_CONV_FN_P (fn)
18405 ? TREE_TYPE (decl_type) : NULL_TREE),
18406 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18407 /*decltype*/false)
18408 == error_mark_node)
18409 return NULL_TREE;
18411 return targs;
18414 /* Return the innermost template arguments that, when applied to a partial
18415 specialization of TMPL whose innermost template parameters are
18416 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18417 ARGS.
18419 For example, suppose we have:
18421 template <class T, class U> struct S {};
18422 template <class T> struct S<T*, int> {};
18424 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18425 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18426 int}. The resulting vector will be {double}, indicating that `T'
18427 is bound to `double'. */
18429 static tree
18430 get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
18432 int i, ntparms = TREE_VEC_LENGTH (tparms);
18433 tree deduced_args;
18434 tree innermost_deduced_args;
18436 innermost_deduced_args = make_tree_vec (ntparms);
18437 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18439 deduced_args = copy_node (args);
18440 SET_TMPL_ARGS_LEVEL (deduced_args,
18441 TMPL_ARGS_DEPTH (deduced_args),
18442 innermost_deduced_args);
18444 else
18445 deduced_args = innermost_deduced_args;
18447 if (unify (tparms, deduced_args,
18448 INNERMOST_TEMPLATE_ARGS (spec_args),
18449 INNERMOST_TEMPLATE_ARGS (args),
18450 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18451 return NULL_TREE;
18453 for (i = 0; i < ntparms; ++i)
18454 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18455 return NULL_TREE;
18457 /* Verify that nondeduced template arguments agree with the type
18458 obtained from argument deduction.
18460 For example:
18462 struct A { typedef int X; };
18463 template <class T, class U> struct C {};
18464 template <class T> struct C<T, typename T::X> {};
18466 Then with the instantiation `C<A, int>', we can deduce that
18467 `T' is `A' but unify () does not check whether `typename T::X'
18468 is `int'. */
18469 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18470 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18471 spec_args, tmpl,
18472 tf_none, false, false);
18473 if (spec_args == error_mark_node
18474 /* We only need to check the innermost arguments; the other
18475 arguments will always agree. */
18476 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18477 INNERMOST_TEMPLATE_ARGS (args)))
18478 return NULL_TREE;
18480 /* Now that we have bindings for all of the template arguments,
18481 ensure that the arguments deduced for the template template
18482 parameters have compatible template parameter lists. See the use
18483 of template_template_parm_bindings_ok_p in fn_type_unification
18484 for more information. */
18485 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18486 return NULL_TREE;
18488 return deduced_args;
18491 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18492 Return the TREE_LIST node with the most specialized template, if
18493 any. If there is no most specialized template, the error_mark_node
18494 is returned.
18496 Note that this function does not look at, or modify, the
18497 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18498 returned is one of the elements of INSTANTIATIONS, callers may
18499 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18500 and retrieve it from the value returned. */
18502 tree
18503 most_specialized_instantiation (tree templates)
18505 tree fn, champ;
18507 ++processing_template_decl;
18509 champ = templates;
18510 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18512 int fate = 0;
18514 if (get_bindings (TREE_VALUE (champ),
18515 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18516 NULL_TREE, /*check_ret=*/true))
18517 fate--;
18519 if (get_bindings (TREE_VALUE (fn),
18520 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18521 NULL_TREE, /*check_ret=*/true))
18522 fate++;
18524 if (fate == -1)
18525 champ = fn;
18526 else if (!fate)
18528 /* Equally specialized, move to next function. If there
18529 is no next function, nothing's most specialized. */
18530 fn = TREE_CHAIN (fn);
18531 champ = fn;
18532 if (!fn)
18533 break;
18537 if (champ)
18538 /* Now verify that champ is better than everything earlier in the
18539 instantiation list. */
18540 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18541 if (get_bindings (TREE_VALUE (champ),
18542 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18543 NULL_TREE, /*check_ret=*/true)
18544 || !get_bindings (TREE_VALUE (fn),
18545 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18546 NULL_TREE, /*check_ret=*/true))
18548 champ = NULL_TREE;
18549 break;
18552 processing_template_decl--;
18554 if (!champ)
18555 return error_mark_node;
18557 return champ;
18560 /* If DECL is a specialization of some template, return the most
18561 general such template. Otherwise, returns NULL_TREE.
18563 For example, given:
18565 template <class T> struct S { template <class U> void f(U); };
18567 if TMPL is `template <class U> void S<int>::f(U)' this will return
18568 the full template. This function will not trace past partial
18569 specializations, however. For example, given in addition:
18571 template <class T> struct S<T*> { template <class U> void f(U); };
18573 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18574 `template <class T> template <class U> S<T*>::f(U)'. */
18576 tree
18577 most_general_template (tree decl)
18579 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18580 an immediate specialization. */
18581 if (TREE_CODE (decl) == FUNCTION_DECL)
18583 if (DECL_TEMPLATE_INFO (decl)) {
18584 decl = DECL_TI_TEMPLATE (decl);
18586 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18587 template friend. */
18588 if (TREE_CODE (decl) != TEMPLATE_DECL)
18589 return NULL_TREE;
18590 } else
18591 return NULL_TREE;
18594 /* Look for more and more general templates. */
18595 while (DECL_TEMPLATE_INFO (decl))
18597 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18598 (See cp-tree.h for details.) */
18599 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18600 break;
18602 if (CLASS_TYPE_P (TREE_TYPE (decl))
18603 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18604 break;
18606 /* Stop if we run into an explicitly specialized class template. */
18607 if (!DECL_NAMESPACE_SCOPE_P (decl)
18608 && DECL_CONTEXT (decl)
18609 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18610 break;
18612 decl = DECL_TI_TEMPLATE (decl);
18615 return decl;
18618 /* Return the most specialized of the class template partial
18619 specializations which can produce TYPE, a specialization of some class
18620 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18621 a _TYPE node corresponding to the partial specialization, while the
18622 TREE_PURPOSE is the set of template arguments that must be
18623 substituted into the TREE_TYPE in order to generate TYPE.
18625 If the choice of partial specialization is ambiguous, a diagnostic
18626 is issued, and the error_mark_node is returned. If there are no
18627 partial specializations matching TYPE, then NULL_TREE is
18628 returned, indicating that the primary template should be used. */
18630 static tree
18631 most_specialized_class (tree type, tsubst_flags_t complain)
18633 tree list = NULL_TREE;
18634 tree t;
18635 tree champ;
18636 int fate;
18637 bool ambiguous_p;
18638 tree outer_args = NULL_TREE;
18640 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
18641 tree main_tmpl = most_general_template (tmpl);
18642 tree args = CLASSTYPE_TI_ARGS (type);
18644 /* For determining which partial specialization to use, only the
18645 innermost args are interesting. */
18646 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18648 outer_args = strip_innermost_template_args (args, 1);
18649 args = INNERMOST_TEMPLATE_ARGS (args);
18652 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
18654 tree partial_spec_args;
18655 tree spec_args;
18656 tree spec_tmpl = TREE_VALUE (t);
18657 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18659 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18661 ++processing_template_decl;
18663 if (outer_args)
18665 /* Discard the outer levels of args, and then substitute in the
18666 template args from the enclosing class. */
18667 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18668 partial_spec_args = tsubst_template_args
18669 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18671 /* And the same for the partial specialization TEMPLATE_DECL. */
18672 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18675 partial_spec_args =
18676 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18677 partial_spec_args,
18678 tmpl, tf_none,
18679 /*require_all_args=*/true,
18680 /*use_default_args=*/true);
18682 --processing_template_decl;
18684 if (partial_spec_args == error_mark_node)
18685 return error_mark_node;
18686 if (spec_tmpl == error_mark_node)
18687 return error_mark_node;
18689 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18690 spec_args = get_class_bindings (tmpl, parms,
18691 partial_spec_args,
18692 args);
18693 if (spec_args)
18695 if (outer_args)
18696 spec_args = add_to_template_args (outer_args, spec_args);
18697 list = tree_cons (spec_args, orig_parms, list);
18698 TREE_TYPE (list) = TREE_TYPE (t);
18702 if (! list)
18703 return NULL_TREE;
18705 ambiguous_p = false;
18706 t = list;
18707 champ = t;
18708 t = TREE_CHAIN (t);
18709 for (; t; t = TREE_CHAIN (t))
18711 fate = more_specialized_class (tmpl, champ, t);
18712 if (fate == 1)
18714 else
18716 if (fate == 0)
18718 t = TREE_CHAIN (t);
18719 if (! t)
18721 ambiguous_p = true;
18722 break;
18725 champ = t;
18729 if (!ambiguous_p)
18730 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18732 fate = more_specialized_class (tmpl, champ, t);
18733 if (fate != 1)
18735 ambiguous_p = true;
18736 break;
18740 if (ambiguous_p)
18742 const char *str;
18743 char *spaces = NULL;
18744 if (!(complain & tf_error))
18745 return error_mark_node;
18746 error ("ambiguous class template instantiation for %q#T", type);
18747 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18748 for (t = list; t; t = TREE_CHAIN (t))
18750 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18751 spaces = spaces ? spaces : get_spaces (str);
18753 free (spaces);
18754 return error_mark_node;
18757 return champ;
18760 /* Explicitly instantiate DECL. */
18762 void
18763 do_decl_instantiation (tree decl, tree storage)
18765 tree result = NULL_TREE;
18766 int extern_p = 0;
18768 if (!decl || decl == error_mark_node)
18769 /* An error occurred, for which grokdeclarator has already issued
18770 an appropriate message. */
18771 return;
18772 else if (! DECL_LANG_SPECIFIC (decl))
18774 error ("explicit instantiation of non-template %q#D", decl);
18775 return;
18777 else if (VAR_P (decl))
18779 /* There is an asymmetry here in the way VAR_DECLs and
18780 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18781 the latter, the DECL we get back will be marked as a
18782 template instantiation, and the appropriate
18783 DECL_TEMPLATE_INFO will be set up. This does not happen for
18784 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18785 should handle VAR_DECLs as it currently handles
18786 FUNCTION_DECLs. */
18787 if (!DECL_CLASS_SCOPE_P (decl))
18789 error ("%qD is not a static data member of a class template", decl);
18790 return;
18792 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18793 if (!result || !VAR_P (result))
18795 error ("no matching template for %qD found", decl);
18796 return;
18798 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18800 error ("type %qT for explicit instantiation %qD does not match "
18801 "declared type %qT", TREE_TYPE (result), decl,
18802 TREE_TYPE (decl));
18803 return;
18806 else if (TREE_CODE (decl) != FUNCTION_DECL)
18808 error ("explicit instantiation of %q#D", decl);
18809 return;
18811 else
18812 result = decl;
18814 /* Check for various error cases. Note that if the explicit
18815 instantiation is valid the RESULT will currently be marked as an
18816 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18817 until we get here. */
18819 if (DECL_TEMPLATE_SPECIALIZATION (result))
18821 /* DR 259 [temp.spec].
18823 Both an explicit instantiation and a declaration of an explicit
18824 specialization shall not appear in a program unless the explicit
18825 instantiation follows a declaration of the explicit specialization.
18827 For a given set of template parameters, if an explicit
18828 instantiation of a template appears after a declaration of an
18829 explicit specialization for that template, the explicit
18830 instantiation has no effect. */
18831 return;
18833 else if (DECL_EXPLICIT_INSTANTIATION (result))
18835 /* [temp.spec]
18837 No program shall explicitly instantiate any template more
18838 than once.
18840 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18841 the first instantiation was `extern' and the second is not,
18842 and EXTERN_P for the opposite case. */
18843 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18844 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18845 /* If an "extern" explicit instantiation follows an ordinary
18846 explicit instantiation, the template is instantiated. */
18847 if (extern_p)
18848 return;
18850 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18852 error ("no matching template for %qD found", result);
18853 return;
18855 else if (!DECL_TEMPLATE_INFO (result))
18857 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18858 return;
18861 if (storage == NULL_TREE)
18863 else if (storage == ridpointers[(int) RID_EXTERN])
18865 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
18866 pedwarn (input_location, OPT_Wpedantic,
18867 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18868 "instantiations");
18869 extern_p = 1;
18871 else
18872 error ("storage class %qD applied to template instantiation", storage);
18874 check_explicit_instantiation_namespace (result);
18875 mark_decl_instantiated (result, extern_p);
18876 if (! extern_p)
18877 instantiate_decl (result, /*defer_ok=*/1,
18878 /*expl_inst_class_mem_p=*/false);
18881 static void
18882 mark_class_instantiated (tree t, int extern_p)
18884 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18885 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18886 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18887 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18888 if (! extern_p)
18890 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18891 rest_of_type_compilation (t, 1);
18895 /* Called from do_type_instantiation through binding_table_foreach to
18896 do recursive instantiation for the type bound in ENTRY. */
18897 static void
18898 bt_instantiate_type_proc (binding_entry entry, void *data)
18900 tree storage = *(tree *) data;
18902 if (MAYBE_CLASS_TYPE_P (entry->type)
18903 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18904 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18907 /* Called from do_type_instantiation to instantiate a member
18908 (a member function or a static member variable) of an
18909 explicitly instantiated class template. */
18910 static void
18911 instantiate_class_member (tree decl, int extern_p)
18913 mark_decl_instantiated (decl, extern_p);
18914 if (! extern_p)
18915 instantiate_decl (decl, /*defer_ok=*/1,
18916 /*expl_inst_class_mem_p=*/true);
18919 /* Perform an explicit instantiation of template class T. STORAGE, if
18920 non-null, is the RID for extern, inline or static. COMPLAIN is
18921 nonzero if this is called from the parser, zero if called recursively,
18922 since the standard is unclear (as detailed below). */
18924 void
18925 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18927 int extern_p = 0;
18928 int nomem_p = 0;
18929 int static_p = 0;
18930 int previous_instantiation_extern_p = 0;
18932 if (TREE_CODE (t) == TYPE_DECL)
18933 t = TREE_TYPE (t);
18935 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18937 tree tmpl =
18938 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18939 if (tmpl)
18940 error ("explicit instantiation of non-class template %qD", tmpl);
18941 else
18942 error ("explicit instantiation of non-template type %qT", t);
18943 return;
18946 complete_type (t);
18948 if (!COMPLETE_TYPE_P (t))
18950 if (complain & tf_error)
18951 error ("explicit instantiation of %q#T before definition of template",
18953 return;
18956 if (storage != NULL_TREE)
18958 if (!in_system_header_at (input_location))
18960 if (storage == ridpointers[(int) RID_EXTERN])
18962 if (cxx_dialect == cxx98)
18963 pedwarn (input_location, OPT_Wpedantic,
18964 "ISO C++ 1998 forbids the use of %<extern%> on "
18965 "explicit instantiations");
18967 else
18968 pedwarn (input_location, OPT_Wpedantic,
18969 "ISO C++ forbids the use of %qE"
18970 " on explicit instantiations", storage);
18973 if (storage == ridpointers[(int) RID_INLINE])
18974 nomem_p = 1;
18975 else if (storage == ridpointers[(int) RID_EXTERN])
18976 extern_p = 1;
18977 else if (storage == ridpointers[(int) RID_STATIC])
18978 static_p = 1;
18979 else
18981 error ("storage class %qD applied to template instantiation",
18982 storage);
18983 extern_p = 0;
18987 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18989 /* DR 259 [temp.spec].
18991 Both an explicit instantiation and a declaration of an explicit
18992 specialization shall not appear in a program unless the explicit
18993 instantiation follows a declaration of the explicit specialization.
18995 For a given set of template parameters, if an explicit
18996 instantiation of a template appears after a declaration of an
18997 explicit specialization for that template, the explicit
18998 instantiation has no effect. */
18999 return;
19001 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19003 /* [temp.spec]
19005 No program shall explicitly instantiate any template more
19006 than once.
19008 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19009 instantiation was `extern'. If EXTERN_P then the second is.
19010 These cases are OK. */
19011 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19013 if (!previous_instantiation_extern_p && !extern_p
19014 && (complain & tf_error))
19015 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19017 /* If we've already instantiated the template, just return now. */
19018 if (!CLASSTYPE_INTERFACE_ONLY (t))
19019 return;
19022 check_explicit_instantiation_namespace (TYPE_NAME (t));
19023 mark_class_instantiated (t, extern_p);
19025 if (nomem_p)
19026 return;
19029 tree tmp;
19031 /* In contrast to implicit instantiation, where only the
19032 declarations, and not the definitions, of members are
19033 instantiated, we have here:
19035 [temp.explicit]
19037 The explicit instantiation of a class template specialization
19038 implies the instantiation of all of its members not
19039 previously explicitly specialized in the translation unit
19040 containing the explicit instantiation.
19042 Of course, we can't instantiate member template classes, since
19043 we don't have any arguments for them. Note that the standard
19044 is unclear on whether the instantiation of the members are
19045 *explicit* instantiations or not. However, the most natural
19046 interpretation is that it should be an explicit instantiation. */
19048 if (! static_p)
19049 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19050 if (TREE_CODE (tmp) == FUNCTION_DECL
19051 && DECL_TEMPLATE_INSTANTIATION (tmp))
19052 instantiate_class_member (tmp, extern_p);
19054 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19055 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19056 instantiate_class_member (tmp, extern_p);
19058 if (CLASSTYPE_NESTED_UTDS (t))
19059 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19060 bt_instantiate_type_proc, &storage);
19064 /* Given a function DECL, which is a specialization of TMPL, modify
19065 DECL to be a re-instantiation of TMPL with the same template
19066 arguments. TMPL should be the template into which tsubst'ing
19067 should occur for DECL, not the most general template.
19069 One reason for doing this is a scenario like this:
19071 template <class T>
19072 void f(const T&, int i);
19074 void g() { f(3, 7); }
19076 template <class T>
19077 void f(const T& t, const int i) { }
19079 Note that when the template is first instantiated, with
19080 instantiate_template, the resulting DECL will have no name for the
19081 first parameter, and the wrong type for the second. So, when we go
19082 to instantiate the DECL, we regenerate it. */
19084 static void
19085 regenerate_decl_from_template (tree decl, tree tmpl)
19087 /* The arguments used to instantiate DECL, from the most general
19088 template. */
19089 tree args;
19090 tree code_pattern;
19092 args = DECL_TI_ARGS (decl);
19093 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19095 /* Make sure that we can see identifiers, and compute access
19096 correctly. */
19097 push_access_scope (decl);
19099 if (TREE_CODE (decl) == FUNCTION_DECL)
19101 tree decl_parm;
19102 tree pattern_parm;
19103 tree specs;
19104 int args_depth;
19105 int parms_depth;
19107 args_depth = TMPL_ARGS_DEPTH (args);
19108 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19109 if (args_depth > parms_depth)
19110 args = get_innermost_template_args (args, parms_depth);
19112 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19113 args, tf_error, NULL_TREE,
19114 /*defer_ok*/false);
19115 if (specs && specs != error_mark_node)
19116 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19117 specs);
19119 /* Merge parameter declarations. */
19120 decl_parm = skip_artificial_parms_for (decl,
19121 DECL_ARGUMENTS (decl));
19122 pattern_parm
19123 = skip_artificial_parms_for (code_pattern,
19124 DECL_ARGUMENTS (code_pattern));
19125 while (decl_parm && !DECL_PACK_P (pattern_parm))
19127 tree parm_type;
19128 tree attributes;
19130 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19131 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19132 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19133 NULL_TREE);
19134 parm_type = type_decays_to (parm_type);
19135 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19136 TREE_TYPE (decl_parm) = parm_type;
19137 attributes = DECL_ATTRIBUTES (pattern_parm);
19138 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19140 DECL_ATTRIBUTES (decl_parm) = attributes;
19141 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19143 decl_parm = DECL_CHAIN (decl_parm);
19144 pattern_parm = DECL_CHAIN (pattern_parm);
19146 /* Merge any parameters that match with the function parameter
19147 pack. */
19148 if (pattern_parm && DECL_PACK_P (pattern_parm))
19150 int i, len;
19151 tree expanded_types;
19152 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19153 the parameters in this function parameter pack. */
19154 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19155 args, tf_error, NULL_TREE);
19156 len = TREE_VEC_LENGTH (expanded_types);
19157 for (i = 0; i < len; i++)
19159 tree parm_type;
19160 tree attributes;
19162 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19163 /* Rename the parameter to include the index. */
19164 DECL_NAME (decl_parm) =
19165 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19166 parm_type = TREE_VEC_ELT (expanded_types, i);
19167 parm_type = type_decays_to (parm_type);
19168 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19169 TREE_TYPE (decl_parm) = parm_type;
19170 attributes = DECL_ATTRIBUTES (pattern_parm);
19171 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19173 DECL_ATTRIBUTES (decl_parm) = attributes;
19174 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19176 decl_parm = DECL_CHAIN (decl_parm);
19179 /* Merge additional specifiers from the CODE_PATTERN. */
19180 if (DECL_DECLARED_INLINE_P (code_pattern)
19181 && !DECL_DECLARED_INLINE_P (decl))
19182 DECL_DECLARED_INLINE_P (decl) = 1;
19184 else if (VAR_P (decl))
19186 DECL_INITIAL (decl) =
19187 tsubst_expr (DECL_INITIAL (code_pattern), args,
19188 tf_error, DECL_TI_TEMPLATE (decl),
19189 /*integral_constant_expression_p=*/false);
19190 if (VAR_HAD_UNKNOWN_BOUND (decl))
19191 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19192 tf_error, DECL_TI_TEMPLATE (decl));
19194 else
19195 gcc_unreachable ();
19197 pop_access_scope (decl);
19200 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19201 substituted to get DECL. */
19203 tree
19204 template_for_substitution (tree decl)
19206 tree tmpl = DECL_TI_TEMPLATE (decl);
19208 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19209 for the instantiation. This is not always the most general
19210 template. Consider, for example:
19212 template <class T>
19213 struct S { template <class U> void f();
19214 template <> void f<int>(); };
19216 and an instantiation of S<double>::f<int>. We want TD to be the
19217 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19218 while (/* An instantiation cannot have a definition, so we need a
19219 more general template. */
19220 DECL_TEMPLATE_INSTANTIATION (tmpl)
19221 /* We must also deal with friend templates. Given:
19223 template <class T> struct S {
19224 template <class U> friend void f() {};
19227 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19228 so far as the language is concerned, but that's still
19229 where we get the pattern for the instantiation from. On
19230 other hand, if the definition comes outside the class, say:
19232 template <class T> struct S {
19233 template <class U> friend void f();
19235 template <class U> friend void f() {}
19237 we don't need to look any further. That's what the check for
19238 DECL_INITIAL is for. */
19239 || (TREE_CODE (decl) == FUNCTION_DECL
19240 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19241 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19243 /* The present template, TD, should not be a definition. If it
19244 were a definition, we should be using it! Note that we
19245 cannot restructure the loop to just keep going until we find
19246 a template with a definition, since that might go too far if
19247 a specialization was declared, but not defined. */
19248 gcc_assert (!VAR_P (decl)
19249 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19251 /* Fetch the more general template. */
19252 tmpl = DECL_TI_TEMPLATE (tmpl);
19255 return tmpl;
19258 /* Returns true if we need to instantiate this template instance even if we
19259 know we aren't going to emit it.. */
19261 bool
19262 always_instantiate_p (tree decl)
19264 /* We always instantiate inline functions so that we can inline them. An
19265 explicit instantiation declaration prohibits implicit instantiation of
19266 non-inline functions. With high levels of optimization, we would
19267 normally inline non-inline functions -- but we're not allowed to do
19268 that for "extern template" functions. Therefore, we check
19269 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19270 return ((TREE_CODE (decl) == FUNCTION_DECL
19271 && (DECL_DECLARED_INLINE_P (decl)
19272 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19273 /* And we need to instantiate static data members so that
19274 their initializers are available in integral constant
19275 expressions. */
19276 || (VAR_P (decl)
19277 && decl_maybe_constant_var_p (decl)));
19280 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19281 instantiate it now, modifying TREE_TYPE (fn). */
19283 void
19284 maybe_instantiate_noexcept (tree fn)
19286 tree fntype, spec, noex, clone;
19288 /* Don't instantiate a noexcept-specification from template context. */
19289 if (processing_template_decl)
19290 return;
19292 if (DECL_CLONED_FUNCTION_P (fn))
19293 fn = DECL_CLONED_FUNCTION (fn);
19294 fntype = TREE_TYPE (fn);
19295 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19297 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19298 return;
19300 noex = TREE_PURPOSE (spec);
19302 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19304 if (push_tinst_level (fn))
19306 push_access_scope (fn);
19307 push_deferring_access_checks (dk_no_deferred);
19308 input_location = DECL_SOURCE_LOCATION (fn);
19309 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19310 DEFERRED_NOEXCEPT_ARGS (noex),
19311 tf_warning_or_error, fn,
19312 /*function_p=*/false,
19313 /*integral_constant_expression_p=*/true);
19314 pop_deferring_access_checks ();
19315 pop_access_scope (fn);
19316 pop_tinst_level ();
19317 spec = build_noexcept_spec (noex, tf_warning_or_error);
19318 if (spec == error_mark_node)
19319 spec = noexcept_false_spec;
19321 else
19322 spec = noexcept_false_spec;
19324 else
19326 /* This is an implicitly declared function, so NOEX is a list of
19327 other functions to evaluate and merge. */
19328 tree elt;
19329 spec = noexcept_true_spec;
19330 for (elt = noex; elt; elt = OVL_NEXT (elt))
19332 tree fn = OVL_CURRENT (elt);
19333 tree subspec;
19334 maybe_instantiate_noexcept (fn);
19335 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19336 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19340 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19342 FOR_EACH_CLONE (clone, fn)
19344 if (TREE_TYPE (clone) == fntype)
19345 TREE_TYPE (clone) = TREE_TYPE (fn);
19346 else
19347 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19351 /* Produce the definition of D, a _DECL generated from a template. If
19352 DEFER_OK is nonzero, then we don't have to actually do the
19353 instantiation now; we just have to do it sometime. Normally it is
19354 an error if this is an explicit instantiation but D is undefined.
19355 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19356 explicitly instantiated class template. */
19358 tree
19359 instantiate_decl (tree d, int defer_ok,
19360 bool expl_inst_class_mem_p)
19362 tree tmpl = DECL_TI_TEMPLATE (d);
19363 tree gen_args;
19364 tree args;
19365 tree td;
19366 tree code_pattern;
19367 tree spec;
19368 tree gen_tmpl;
19369 bool pattern_defined;
19370 location_t saved_loc = input_location;
19371 int saved_unevaluated_operand = cp_unevaluated_operand;
19372 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19373 bool external_p;
19374 tree fn_context;
19375 bool nested;
19377 /* This function should only be used to instantiate templates for
19378 functions and static member variables. */
19379 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19381 /* Variables are never deferred; if instantiation is required, they
19382 are instantiated right away. That allows for better code in the
19383 case that an expression refers to the value of the variable --
19384 if the variable has a constant value the referring expression can
19385 take advantage of that fact. */
19386 if (VAR_P (d)
19387 || DECL_DECLARED_CONSTEXPR_P (d))
19388 defer_ok = 0;
19390 /* Don't instantiate cloned functions. Instead, instantiate the
19391 functions they cloned. */
19392 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19393 d = DECL_CLONED_FUNCTION (d);
19395 if (DECL_TEMPLATE_INSTANTIATED (d)
19396 || (TREE_CODE (d) == FUNCTION_DECL
19397 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19398 || DECL_TEMPLATE_SPECIALIZATION (d))
19399 /* D has already been instantiated or explicitly specialized, so
19400 there's nothing for us to do here.
19402 It might seem reasonable to check whether or not D is an explicit
19403 instantiation, and, if so, stop here. But when an explicit
19404 instantiation is deferred until the end of the compilation,
19405 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19406 the instantiation. */
19407 return d;
19409 /* Check to see whether we know that this template will be
19410 instantiated in some other file, as with "extern template"
19411 extension. */
19412 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19414 /* In general, we do not instantiate such templates. */
19415 if (external_p && !always_instantiate_p (d))
19416 return d;
19418 gen_tmpl = most_general_template (tmpl);
19419 gen_args = DECL_TI_ARGS (d);
19421 if (tmpl != gen_tmpl)
19422 /* We should already have the extra args. */
19423 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19424 == TMPL_ARGS_DEPTH (gen_args));
19425 /* And what's in the hash table should match D. */
19426 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19427 || spec == NULL_TREE);
19429 /* This needs to happen before any tsubsting. */
19430 if (! push_tinst_level (d))
19431 return d;
19433 timevar_push (TV_TEMPLATE_INST);
19435 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19436 for the instantiation. */
19437 td = template_for_substitution (d);
19438 code_pattern = DECL_TEMPLATE_RESULT (td);
19440 /* We should never be trying to instantiate a member of a class
19441 template or partial specialization. */
19442 gcc_assert (d != code_pattern);
19444 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19445 || DECL_TEMPLATE_SPECIALIZATION (td))
19446 /* In the case of a friend template whose definition is provided
19447 outside the class, we may have too many arguments. Drop the
19448 ones we don't need. The same is true for specializations. */
19449 args = get_innermost_template_args
19450 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19451 else
19452 args = gen_args;
19454 if (TREE_CODE (d) == FUNCTION_DECL)
19455 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19456 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19457 else
19458 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19460 /* We may be in the middle of deferred access check. Disable it now. */
19461 push_deferring_access_checks (dk_no_deferred);
19463 /* Unless an explicit instantiation directive has already determined
19464 the linkage of D, remember that a definition is available for
19465 this entity. */
19466 if (pattern_defined
19467 && !DECL_INTERFACE_KNOWN (d)
19468 && !DECL_NOT_REALLY_EXTERN (d))
19469 mark_definable (d);
19471 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19472 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19473 input_location = DECL_SOURCE_LOCATION (d);
19475 /* If D is a member of an explicitly instantiated class template,
19476 and no definition is available, treat it like an implicit
19477 instantiation. */
19478 if (!pattern_defined && expl_inst_class_mem_p
19479 && DECL_EXPLICIT_INSTANTIATION (d))
19481 /* Leave linkage flags alone on instantiations with anonymous
19482 visibility. */
19483 if (TREE_PUBLIC (d))
19485 DECL_NOT_REALLY_EXTERN (d) = 0;
19486 DECL_INTERFACE_KNOWN (d) = 0;
19488 SET_DECL_IMPLICIT_INSTANTIATION (d);
19491 if (TREE_CODE (d) == FUNCTION_DECL)
19492 maybe_instantiate_noexcept (d);
19494 /* Defer all other templates, unless we have been explicitly
19495 forbidden from doing so. */
19496 if (/* If there is no definition, we cannot instantiate the
19497 template. */
19498 ! pattern_defined
19499 /* If it's OK to postpone instantiation, do so. */
19500 || defer_ok
19501 /* If this is a static data member that will be defined
19502 elsewhere, we don't want to instantiate the entire data
19503 member, but we do want to instantiate the initializer so that
19504 we can substitute that elsewhere. */
19505 || (external_p && VAR_P (d)))
19507 /* The definition of the static data member is now required so
19508 we must substitute the initializer. */
19509 if (VAR_P (d)
19510 && !DECL_INITIAL (d)
19511 && DECL_INITIAL (code_pattern))
19513 tree ns;
19514 tree init;
19515 bool const_init = false;
19517 ns = decl_namespace_context (d);
19518 push_nested_namespace (ns);
19519 push_nested_class (DECL_CONTEXT (d));
19520 init = tsubst_expr (DECL_INITIAL (code_pattern),
19521 args,
19522 tf_warning_or_error, NULL_TREE,
19523 /*integral_constant_expression_p=*/false);
19524 /* Make sure the initializer is still constant, in case of
19525 circular dependency (template/instantiate6.C). */
19526 const_init
19527 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19528 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19529 /*asmspec_tree=*/NULL_TREE,
19530 LOOKUP_ONLYCONVERTING);
19531 pop_nested_class ();
19532 pop_nested_namespace (ns);
19535 /* We restore the source position here because it's used by
19536 add_pending_template. */
19537 input_location = saved_loc;
19539 if (at_eof && !pattern_defined
19540 && DECL_EXPLICIT_INSTANTIATION (d)
19541 && DECL_NOT_REALLY_EXTERN (d))
19542 /* [temp.explicit]
19544 The definition of a non-exported function template, a
19545 non-exported member function template, or a non-exported
19546 member function or static data member of a class template
19547 shall be present in every translation unit in which it is
19548 explicitly instantiated. */
19549 permerror (input_location, "explicit instantiation of %qD "
19550 "but no definition available", d);
19552 /* If we're in unevaluated context, we just wanted to get the
19553 constant value; this isn't an odr use, so don't queue
19554 a full instantiation. */
19555 if (cp_unevaluated_operand != 0)
19556 goto out;
19557 /* ??? Historically, we have instantiated inline functions, even
19558 when marked as "extern template". */
19559 if (!(external_p && VAR_P (d)))
19560 add_pending_template (d);
19561 goto out;
19563 /* Tell the repository that D is available in this translation unit
19564 -- and see if it is supposed to be instantiated here. */
19565 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19567 /* In a PCH file, despite the fact that the repository hasn't
19568 requested instantiation in the PCH it is still possible that
19569 an instantiation will be required in a file that includes the
19570 PCH. */
19571 if (pch_file)
19572 add_pending_template (d);
19573 /* Instantiate inline functions so that the inliner can do its
19574 job, even though we'll not be emitting a copy of this
19575 function. */
19576 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19577 goto out;
19580 fn_context = decl_function_context (d);
19581 nested = (current_function_decl != NULL_TREE);
19582 if (!fn_context)
19583 push_to_top_level ();
19584 else
19586 if (nested)
19587 push_function_context ();
19588 cp_unevaluated_operand = 0;
19589 c_inhibit_evaluation_warnings = 0;
19592 /* Mark D as instantiated so that recursive calls to
19593 instantiate_decl do not try to instantiate it again. */
19594 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19596 /* Regenerate the declaration in case the template has been modified
19597 by a subsequent redeclaration. */
19598 regenerate_decl_from_template (d, td);
19600 /* We already set the file and line above. Reset them now in case
19601 they changed as a result of calling regenerate_decl_from_template. */
19602 input_location = DECL_SOURCE_LOCATION (d);
19604 if (VAR_P (d))
19606 tree init;
19607 bool const_init = false;
19609 /* Clear out DECL_RTL; whatever was there before may not be right
19610 since we've reset the type of the declaration. */
19611 SET_DECL_RTL (d, NULL);
19612 DECL_IN_AGGR_P (d) = 0;
19614 /* The initializer is placed in DECL_INITIAL by
19615 regenerate_decl_from_template so we don't need to
19616 push/pop_access_scope again here. Pull it out so that
19617 cp_finish_decl can process it. */
19618 init = DECL_INITIAL (d);
19619 DECL_INITIAL (d) = NULL_TREE;
19620 DECL_INITIALIZED_P (d) = 0;
19622 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19623 initializer. That function will defer actual emission until
19624 we have a chance to determine linkage. */
19625 DECL_EXTERNAL (d) = 0;
19627 /* Enter the scope of D so that access-checking works correctly. */
19628 push_nested_class (DECL_CONTEXT (d));
19629 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19630 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19631 pop_nested_class ();
19633 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19634 synthesize_method (d);
19635 else if (TREE_CODE (d) == FUNCTION_DECL)
19637 struct pointer_map_t *saved_local_specializations;
19638 tree subst_decl;
19639 tree tmpl_parm;
19640 tree spec_parm;
19641 tree block = NULL_TREE;
19643 /* Save away the current list, in case we are instantiating one
19644 template from within the body of another. */
19645 saved_local_specializations = local_specializations;
19647 /* Set up the list of local specializations. */
19648 local_specializations = pointer_map_create ();
19650 /* Set up context. */
19651 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19652 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19653 block = push_stmt_list ();
19654 else
19655 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19657 /* Some typedefs referenced from within the template code need to be
19658 access checked at template instantiation time, i.e now. These
19659 types were added to the template at parsing time. Let's get those
19660 and perform the access checks then. */
19661 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19662 gen_args);
19664 /* Create substitution entries for the parameters. */
19665 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19666 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19667 spec_parm = DECL_ARGUMENTS (d);
19668 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19670 register_local_specialization (spec_parm, tmpl_parm);
19671 spec_parm = skip_artificial_parms_for (d, spec_parm);
19672 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19674 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19676 if (!DECL_PACK_P (tmpl_parm))
19678 register_local_specialization (spec_parm, tmpl_parm);
19679 spec_parm = DECL_CHAIN (spec_parm);
19681 else
19683 /* Register the (value) argument pack as a specialization of
19684 TMPL_PARM, then move on. */
19685 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19686 register_local_specialization (argpack, tmpl_parm);
19689 gcc_assert (!spec_parm);
19691 /* Substitute into the body of the function. */
19692 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19693 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19694 tf_warning_or_error, tmpl);
19695 else
19697 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19698 tf_warning_or_error, tmpl,
19699 /*integral_constant_expression_p=*/false);
19701 /* Set the current input_location to the end of the function
19702 so that finish_function knows where we are. */
19703 input_location
19704 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19706 /* Remember if we saw an infinite loop in the template. */
19707 current_function_infinite_loop
19708 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
19711 /* We don't need the local specializations any more. */
19712 pointer_map_destroy (local_specializations);
19713 local_specializations = saved_local_specializations;
19715 /* Finish the function. */
19716 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19717 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19718 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19719 else
19721 d = finish_function (0);
19722 expand_or_defer_fn (d);
19725 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19726 cp_check_omp_declare_reduction (d);
19729 /* We're not deferring instantiation any more. */
19730 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19732 if (!fn_context)
19733 pop_from_top_level ();
19734 else if (nested)
19735 pop_function_context ();
19737 out:
19738 input_location = saved_loc;
19739 cp_unevaluated_operand = saved_unevaluated_operand;
19740 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19741 pop_deferring_access_checks ();
19742 pop_tinst_level ();
19744 timevar_pop (TV_TEMPLATE_INST);
19746 return d;
19749 /* Run through the list of templates that we wish we could
19750 instantiate, and instantiate any we can. RETRIES is the
19751 number of times we retry pending template instantiation. */
19753 void
19754 instantiate_pending_templates (int retries)
19756 int reconsider;
19757 location_t saved_loc = input_location;
19759 /* Instantiating templates may trigger vtable generation. This in turn
19760 may require further template instantiations. We place a limit here
19761 to avoid infinite loop. */
19762 if (pending_templates && retries >= max_tinst_depth)
19764 tree decl = pending_templates->tinst->decl;
19766 error ("template instantiation depth exceeds maximum of %d"
19767 " instantiating %q+D, possibly from virtual table generation"
19768 " (use -ftemplate-depth= to increase the maximum)",
19769 max_tinst_depth, decl);
19770 if (TREE_CODE (decl) == FUNCTION_DECL)
19771 /* Pretend that we defined it. */
19772 DECL_INITIAL (decl) = error_mark_node;
19773 return;
19778 struct pending_template **t = &pending_templates;
19779 struct pending_template *last = NULL;
19780 reconsider = 0;
19781 while (*t)
19783 tree instantiation = reopen_tinst_level ((*t)->tinst);
19784 bool complete = false;
19786 if (TYPE_P (instantiation))
19788 tree fn;
19790 if (!COMPLETE_TYPE_P (instantiation))
19792 instantiate_class_template (instantiation);
19793 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19794 for (fn = TYPE_METHODS (instantiation);
19796 fn = TREE_CHAIN (fn))
19797 if (! DECL_ARTIFICIAL (fn))
19798 instantiate_decl (fn,
19799 /*defer_ok=*/0,
19800 /*expl_inst_class_mem_p=*/false);
19801 if (COMPLETE_TYPE_P (instantiation))
19802 reconsider = 1;
19805 complete = COMPLETE_TYPE_P (instantiation);
19807 else
19809 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19810 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19812 instantiation
19813 = instantiate_decl (instantiation,
19814 /*defer_ok=*/0,
19815 /*expl_inst_class_mem_p=*/false);
19816 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19817 reconsider = 1;
19820 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19821 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19824 if (complete)
19825 /* If INSTANTIATION has been instantiated, then we don't
19826 need to consider it again in the future. */
19827 *t = (*t)->next;
19828 else
19830 last = *t;
19831 t = &(*t)->next;
19833 tinst_depth = 0;
19834 current_tinst_level = NULL;
19836 last_pending_template = last;
19838 while (reconsider);
19840 input_location = saved_loc;
19843 /* Substitute ARGVEC into T, which is a list of initializers for
19844 either base class or a non-static data member. The TREE_PURPOSEs
19845 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19846 instantiate_decl. */
19848 static tree
19849 tsubst_initializer_list (tree t, tree argvec)
19851 tree inits = NULL_TREE;
19853 for (; t; t = TREE_CHAIN (t))
19855 tree decl;
19856 tree init;
19857 tree expanded_bases = NULL_TREE;
19858 tree expanded_arguments = NULL_TREE;
19859 int i, len = 1;
19861 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19863 tree expr;
19864 tree arg;
19866 /* Expand the base class expansion type into separate base
19867 classes. */
19868 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19869 tf_warning_or_error,
19870 NULL_TREE);
19871 if (expanded_bases == error_mark_node)
19872 continue;
19874 /* We'll be building separate TREE_LISTs of arguments for
19875 each base. */
19876 len = TREE_VEC_LENGTH (expanded_bases);
19877 expanded_arguments = make_tree_vec (len);
19878 for (i = 0; i < len; i++)
19879 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19881 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19882 expand each argument in the TREE_VALUE of t. */
19883 expr = make_node (EXPR_PACK_EXPANSION);
19884 PACK_EXPANSION_LOCAL_P (expr) = true;
19885 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19886 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19888 if (TREE_VALUE (t) == void_type_node)
19889 /* VOID_TYPE_NODE is used to indicate
19890 value-initialization. */
19892 for (i = 0; i < len; i++)
19893 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19895 else
19897 /* Substitute parameter packs into each argument in the
19898 TREE_LIST. */
19899 in_base_initializer = 1;
19900 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19902 tree expanded_exprs;
19904 /* Expand the argument. */
19905 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19906 expanded_exprs
19907 = tsubst_pack_expansion (expr, argvec,
19908 tf_warning_or_error,
19909 NULL_TREE);
19910 if (expanded_exprs == error_mark_node)
19911 continue;
19913 /* Prepend each of the expanded expressions to the
19914 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19915 for (i = 0; i < len; i++)
19917 TREE_VEC_ELT (expanded_arguments, i) =
19918 tree_cons (NULL_TREE,
19919 TREE_VEC_ELT (expanded_exprs, i),
19920 TREE_VEC_ELT (expanded_arguments, i));
19923 in_base_initializer = 0;
19925 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19926 since we built them backwards. */
19927 for (i = 0; i < len; i++)
19929 TREE_VEC_ELT (expanded_arguments, i) =
19930 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19935 for (i = 0; i < len; ++i)
19937 if (expanded_bases)
19939 decl = TREE_VEC_ELT (expanded_bases, i);
19940 decl = expand_member_init (decl);
19941 init = TREE_VEC_ELT (expanded_arguments, i);
19943 else
19945 tree tmp;
19946 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19947 tf_warning_or_error, NULL_TREE);
19949 decl = expand_member_init (decl);
19950 if (decl && !DECL_P (decl))
19951 in_base_initializer = 1;
19953 init = TREE_VALUE (t);
19954 tmp = init;
19955 if (init != void_type_node)
19956 init = tsubst_expr (init, argvec,
19957 tf_warning_or_error, NULL_TREE,
19958 /*integral_constant_expression_p=*/false);
19959 if (init == NULL_TREE && tmp != NULL_TREE)
19960 /* If we had an initializer but it instantiated to nothing,
19961 value-initialize the object. This will only occur when
19962 the initializer was a pack expansion where the parameter
19963 packs used in that expansion were of length zero. */
19964 init = void_type_node;
19965 in_base_initializer = 0;
19968 if (decl)
19970 init = build_tree_list (decl, init);
19971 TREE_CHAIN (init) = inits;
19972 inits = init;
19976 return inits;
19979 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19981 static void
19982 set_current_access_from_decl (tree decl)
19984 if (TREE_PRIVATE (decl))
19985 current_access_specifier = access_private_node;
19986 else if (TREE_PROTECTED (decl))
19987 current_access_specifier = access_protected_node;
19988 else
19989 current_access_specifier = access_public_node;
19992 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19993 is the instantiation (which should have been created with
19994 start_enum) and ARGS are the template arguments to use. */
19996 static void
19997 tsubst_enum (tree tag, tree newtag, tree args)
19999 tree e;
20001 if (SCOPED_ENUM_P (newtag))
20002 begin_scope (sk_scoped_enum, newtag);
20004 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20006 tree value;
20007 tree decl;
20009 decl = TREE_VALUE (e);
20010 /* Note that in a template enum, the TREE_VALUE is the
20011 CONST_DECL, not the corresponding INTEGER_CST. */
20012 value = tsubst_expr (DECL_INITIAL (decl),
20013 args, tf_warning_or_error, NULL_TREE,
20014 /*integral_constant_expression_p=*/true);
20016 /* Give this enumeration constant the correct access. */
20017 set_current_access_from_decl (decl);
20019 /* Actually build the enumerator itself. */
20020 build_enumerator
20021 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20024 if (SCOPED_ENUM_P (newtag))
20025 finish_scope ();
20027 finish_enum_value_list (newtag);
20028 finish_enum (newtag);
20030 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20031 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20034 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20035 its type -- but without substituting the innermost set of template
20036 arguments. So, innermost set of template parameters will appear in
20037 the type. */
20039 tree
20040 get_mostly_instantiated_function_type (tree decl)
20042 tree fn_type;
20043 tree tmpl;
20044 tree targs;
20045 tree tparms;
20046 int parm_depth;
20048 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20049 targs = DECL_TI_ARGS (decl);
20050 tparms = DECL_TEMPLATE_PARMS (tmpl);
20051 parm_depth = TMPL_PARMS_DEPTH (tparms);
20053 /* There should be as many levels of arguments as there are levels
20054 of parameters. */
20055 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20057 fn_type = TREE_TYPE (tmpl);
20059 if (parm_depth == 1)
20060 /* No substitution is necessary. */
20062 else
20064 int i;
20065 tree partial_args;
20067 /* Replace the innermost level of the TARGS with NULL_TREEs to
20068 let tsubst know not to substitute for those parameters. */
20069 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20070 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20071 SET_TMPL_ARGS_LEVEL (partial_args, i,
20072 TMPL_ARGS_LEVEL (targs, i));
20073 SET_TMPL_ARGS_LEVEL (partial_args,
20074 TMPL_ARGS_DEPTH (targs),
20075 make_tree_vec (DECL_NTPARMS (tmpl)));
20077 /* Make sure that we can see identifiers, and compute access
20078 correctly. */
20079 push_access_scope (decl);
20081 ++processing_template_decl;
20082 /* Now, do the (partial) substitution to figure out the
20083 appropriate function type. */
20084 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20085 --processing_template_decl;
20087 /* Substitute into the template parameters to obtain the real
20088 innermost set of parameters. This step is important if the
20089 innermost set of template parameters contains value
20090 parameters whose types depend on outer template parameters. */
20091 TREE_VEC_LENGTH (partial_args)--;
20092 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20094 pop_access_scope (decl);
20097 return fn_type;
20100 /* Return truthvalue if we're processing a template different from
20101 the last one involved in diagnostics. */
20103 problematic_instantiation_changed (void)
20105 return current_tinst_level != last_error_tinst_level;
20108 /* Remember current template involved in diagnostics. */
20109 void
20110 record_last_problematic_instantiation (void)
20112 last_error_tinst_level = current_tinst_level;
20115 struct tinst_level *
20116 current_instantiation (void)
20118 return current_tinst_level;
20121 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20122 type. Return zero for ok, nonzero for disallowed. Issue error and
20123 warning messages under control of COMPLAIN. */
20125 static int
20126 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20128 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20129 return 0;
20130 else if (POINTER_TYPE_P (type))
20131 return 0;
20132 else if (TYPE_PTRMEM_P (type))
20133 return 0;
20134 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20135 return 0;
20136 else if (TREE_CODE (type) == TYPENAME_TYPE)
20137 return 0;
20138 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20139 return 0;
20140 else if (TREE_CODE (type) == NULLPTR_TYPE)
20141 return 0;
20143 if (complain & tf_error)
20145 if (type == error_mark_node)
20146 inform (input_location, "invalid template non-type parameter");
20147 else
20148 error ("%q#T is not a valid type for a template non-type parameter",
20149 type);
20151 return 1;
20154 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20155 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20157 static bool
20158 dependent_type_p_r (tree type)
20160 tree scope;
20162 /* [temp.dep.type]
20164 A type is dependent if it is:
20166 -- a template parameter. Template template parameters are types
20167 for us (since TYPE_P holds true for them) so we handle
20168 them here. */
20169 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20170 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20171 return true;
20172 /* -- a qualified-id with a nested-name-specifier which contains a
20173 class-name that names a dependent type or whose unqualified-id
20174 names a dependent type. */
20175 if (TREE_CODE (type) == TYPENAME_TYPE)
20176 return true;
20177 /* -- a cv-qualified type where the cv-unqualified type is
20178 dependent. */
20179 type = TYPE_MAIN_VARIANT (type);
20180 /* -- a compound type constructed from any dependent type. */
20181 if (TYPE_PTRMEM_P (type))
20182 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20183 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20184 (type)));
20185 else if (TYPE_PTR_P (type)
20186 || TREE_CODE (type) == REFERENCE_TYPE)
20187 return dependent_type_p (TREE_TYPE (type));
20188 else if (TREE_CODE (type) == FUNCTION_TYPE
20189 || TREE_CODE (type) == METHOD_TYPE)
20191 tree arg_type;
20193 if (dependent_type_p (TREE_TYPE (type)))
20194 return true;
20195 for (arg_type = TYPE_ARG_TYPES (type);
20196 arg_type;
20197 arg_type = TREE_CHAIN (arg_type))
20198 if (dependent_type_p (TREE_VALUE (arg_type)))
20199 return true;
20200 return false;
20202 /* -- an array type constructed from any dependent type or whose
20203 size is specified by a constant expression that is
20204 value-dependent.
20206 We checked for type- and value-dependence of the bounds in
20207 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20208 if (TREE_CODE (type) == ARRAY_TYPE)
20210 if (TYPE_DOMAIN (type)
20211 && dependent_type_p (TYPE_DOMAIN (type)))
20212 return true;
20213 return dependent_type_p (TREE_TYPE (type));
20216 /* -- a template-id in which either the template name is a template
20217 parameter ... */
20218 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20219 return true;
20220 /* ... or any of the template arguments is a dependent type or
20221 an expression that is type-dependent or value-dependent. */
20222 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20223 && (any_dependent_template_arguments_p
20224 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20225 return true;
20227 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20228 dependent; if the argument of the `typeof' expression is not
20229 type-dependent, then it should already been have resolved. */
20230 if (TREE_CODE (type) == TYPEOF_TYPE
20231 || TREE_CODE (type) == DECLTYPE_TYPE
20232 || TREE_CODE (type) == UNDERLYING_TYPE)
20233 return true;
20235 /* A template argument pack is dependent if any of its packed
20236 arguments are. */
20237 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20239 tree args = ARGUMENT_PACK_ARGS (type);
20240 int i, len = TREE_VEC_LENGTH (args);
20241 for (i = 0; i < len; ++i)
20242 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20243 return true;
20246 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20247 be template parameters. */
20248 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20249 return true;
20251 /* The standard does not specifically mention types that are local
20252 to template functions or local classes, but they should be
20253 considered dependent too. For example:
20255 template <int I> void f() {
20256 enum E { a = I };
20257 S<sizeof (E)> s;
20260 The size of `E' cannot be known until the value of `I' has been
20261 determined. Therefore, `E' must be considered dependent. */
20262 scope = TYPE_CONTEXT (type);
20263 if (scope && TYPE_P (scope))
20264 return dependent_type_p (scope);
20265 /* Don't use type_dependent_expression_p here, as it can lead
20266 to infinite recursion trying to determine whether a lambda
20267 nested in a lambda is dependent (c++/47687). */
20268 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20269 && DECL_LANG_SPECIFIC (scope)
20270 && DECL_TEMPLATE_INFO (scope)
20271 && (any_dependent_template_arguments_p
20272 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20273 return true;
20275 /* Other types are non-dependent. */
20276 return false;
20279 /* Returns TRUE if TYPE is dependent, in the sense of
20280 [temp.dep.type]. Note that a NULL type is considered dependent. */
20282 bool
20283 dependent_type_p (tree type)
20285 /* If there are no template parameters in scope, then there can't be
20286 any dependent types. */
20287 if (!processing_template_decl)
20289 /* If we are not processing a template, then nobody should be
20290 providing us with a dependent type. */
20291 gcc_assert (type);
20292 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20293 return false;
20296 /* If the type is NULL, we have not computed a type for the entity
20297 in question; in that case, the type is dependent. */
20298 if (!type)
20299 return true;
20301 /* Erroneous types can be considered non-dependent. */
20302 if (type == error_mark_node)
20303 return false;
20305 /* If we have not already computed the appropriate value for TYPE,
20306 do so now. */
20307 if (!TYPE_DEPENDENT_P_VALID (type))
20309 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20310 TYPE_DEPENDENT_P_VALID (type) = 1;
20313 return TYPE_DEPENDENT_P (type);
20316 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20317 lookup. In other words, a dependent type that is not the current
20318 instantiation. */
20320 bool
20321 dependent_scope_p (tree scope)
20323 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20324 && !currently_open_class (scope));
20327 /* T is a SCOPE_REF; return whether we need to consider it
20328 instantiation-dependent so that we can check access at instantiation
20329 time even though we know which member it resolves to. */
20331 static bool
20332 instantiation_dependent_scope_ref_p (tree t)
20334 if (DECL_P (TREE_OPERAND (t, 1))
20335 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20336 && accessible_in_template_p (TREE_OPERAND (t, 0),
20337 TREE_OPERAND (t, 1)))
20338 return false;
20339 else
20340 return true;
20343 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20344 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20345 expression. */
20347 /* Note that this predicate is not appropriate for general expressions;
20348 only constant expressions (that satisfy potential_constant_expression)
20349 can be tested for value dependence. */
20351 bool
20352 value_dependent_expression_p (tree expression)
20354 if (!processing_template_decl)
20355 return false;
20357 /* A name declared with a dependent type. */
20358 if (DECL_P (expression) && type_dependent_expression_p (expression))
20359 return true;
20361 switch (TREE_CODE (expression))
20363 case IDENTIFIER_NODE:
20364 /* A name that has not been looked up -- must be dependent. */
20365 return true;
20367 case TEMPLATE_PARM_INDEX:
20368 /* A non-type template parm. */
20369 return true;
20371 case CONST_DECL:
20372 /* A non-type template parm. */
20373 if (DECL_TEMPLATE_PARM_P (expression))
20374 return true;
20375 return value_dependent_expression_p (DECL_INITIAL (expression));
20377 case VAR_DECL:
20378 /* A constant with literal type and is initialized
20379 with an expression that is value-dependent.
20381 Note that a non-dependent parenthesized initializer will have
20382 already been replaced with its constant value, so if we see
20383 a TREE_LIST it must be dependent. */
20384 if (DECL_INITIAL (expression)
20385 && decl_constant_var_p (expression)
20386 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20387 || value_dependent_expression_p (DECL_INITIAL (expression))))
20388 return true;
20389 return false;
20391 case DYNAMIC_CAST_EXPR:
20392 case STATIC_CAST_EXPR:
20393 case CONST_CAST_EXPR:
20394 case REINTERPRET_CAST_EXPR:
20395 case CAST_EXPR:
20396 /* These expressions are value-dependent if the type to which
20397 the cast occurs is dependent or the expression being casted
20398 is value-dependent. */
20400 tree type = TREE_TYPE (expression);
20402 if (dependent_type_p (type))
20403 return true;
20405 /* A functional cast has a list of operands. */
20406 expression = TREE_OPERAND (expression, 0);
20407 if (!expression)
20409 /* If there are no operands, it must be an expression such
20410 as "int()". This should not happen for aggregate types
20411 because it would form non-constant expressions. */
20412 gcc_assert (cxx_dialect >= cxx11
20413 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20415 return false;
20418 if (TREE_CODE (expression) == TREE_LIST)
20419 return any_value_dependent_elements_p (expression);
20421 return value_dependent_expression_p (expression);
20424 case SIZEOF_EXPR:
20425 if (SIZEOF_EXPR_TYPE_P (expression))
20426 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20427 /* FALLTHRU */
20428 case ALIGNOF_EXPR:
20429 case TYPEID_EXPR:
20430 /* A `sizeof' expression is value-dependent if the operand is
20431 type-dependent or is a pack expansion. */
20432 expression = TREE_OPERAND (expression, 0);
20433 if (PACK_EXPANSION_P (expression))
20434 return true;
20435 else if (TYPE_P (expression))
20436 return dependent_type_p (expression);
20437 return instantiation_dependent_expression_p (expression);
20439 case AT_ENCODE_EXPR:
20440 /* An 'encode' expression is value-dependent if the operand is
20441 type-dependent. */
20442 expression = TREE_OPERAND (expression, 0);
20443 return dependent_type_p (expression);
20445 case NOEXCEPT_EXPR:
20446 expression = TREE_OPERAND (expression, 0);
20447 return instantiation_dependent_expression_p (expression);
20449 case SCOPE_REF:
20450 /* All instantiation-dependent expressions should also be considered
20451 value-dependent. */
20452 return instantiation_dependent_scope_ref_p (expression);
20454 case COMPONENT_REF:
20455 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20456 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20458 case NONTYPE_ARGUMENT_PACK:
20459 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20460 is value-dependent. */
20462 tree values = ARGUMENT_PACK_ARGS (expression);
20463 int i, len = TREE_VEC_LENGTH (values);
20465 for (i = 0; i < len; ++i)
20466 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20467 return true;
20469 return false;
20472 case TRAIT_EXPR:
20474 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20475 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20476 || (type2 ? dependent_type_p (type2) : false));
20479 case MODOP_EXPR:
20480 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20481 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20483 case ARRAY_REF:
20484 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20485 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20487 case ADDR_EXPR:
20489 tree op = TREE_OPERAND (expression, 0);
20490 return (value_dependent_expression_p (op)
20491 || has_value_dependent_address (op));
20494 case CALL_EXPR:
20496 tree fn = get_callee_fndecl (expression);
20497 int i, nargs;
20498 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20499 return true;
20500 nargs = call_expr_nargs (expression);
20501 for (i = 0; i < nargs; ++i)
20503 tree op = CALL_EXPR_ARG (expression, i);
20504 /* In a call to a constexpr member function, look through the
20505 implicit ADDR_EXPR on the object argument so that it doesn't
20506 cause the call to be considered value-dependent. We also
20507 look through it in potential_constant_expression. */
20508 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20509 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20510 && TREE_CODE (op) == ADDR_EXPR)
20511 op = TREE_OPERAND (op, 0);
20512 if (value_dependent_expression_p (op))
20513 return true;
20515 return false;
20518 case TEMPLATE_ID_EXPR:
20519 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20520 type-dependent. */
20521 return type_dependent_expression_p (expression);
20523 case CONSTRUCTOR:
20525 unsigned ix;
20526 tree val;
20527 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20528 if (value_dependent_expression_p (val))
20529 return true;
20530 return false;
20533 case STMT_EXPR:
20534 /* Treat a GNU statement expression as dependent to avoid crashing
20535 under fold_non_dependent_expr; it can't be constant. */
20536 return true;
20538 default:
20539 /* A constant expression is value-dependent if any subexpression is
20540 value-dependent. */
20541 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20543 case tcc_reference:
20544 case tcc_unary:
20545 case tcc_comparison:
20546 case tcc_binary:
20547 case tcc_expression:
20548 case tcc_vl_exp:
20550 int i, len = cp_tree_operand_length (expression);
20552 for (i = 0; i < len; i++)
20554 tree t = TREE_OPERAND (expression, i);
20556 /* In some cases, some of the operands may be missing.l
20557 (For example, in the case of PREDECREMENT_EXPR, the
20558 amount to increment by may be missing.) That doesn't
20559 make the expression dependent. */
20560 if (t && value_dependent_expression_p (t))
20561 return true;
20564 break;
20565 default:
20566 break;
20568 break;
20571 /* The expression is not value-dependent. */
20572 return false;
20575 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20576 [temp.dep.expr]. Note that an expression with no type is
20577 considered dependent. Other parts of the compiler arrange for an
20578 expression with type-dependent subexpressions to have no type, so
20579 this function doesn't have to be fully recursive. */
20581 bool
20582 type_dependent_expression_p (tree expression)
20584 if (!processing_template_decl)
20585 return false;
20587 if (expression == NULL_TREE || expression == error_mark_node)
20588 return false;
20590 /* An unresolved name is always dependent. */
20591 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20592 return true;
20594 /* Some expression forms are never type-dependent. */
20595 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20596 || TREE_CODE (expression) == SIZEOF_EXPR
20597 || TREE_CODE (expression) == ALIGNOF_EXPR
20598 || TREE_CODE (expression) == AT_ENCODE_EXPR
20599 || TREE_CODE (expression) == NOEXCEPT_EXPR
20600 || TREE_CODE (expression) == TRAIT_EXPR
20601 || TREE_CODE (expression) == TYPEID_EXPR
20602 || TREE_CODE (expression) == DELETE_EXPR
20603 || TREE_CODE (expression) == VEC_DELETE_EXPR
20604 || TREE_CODE (expression) == THROW_EXPR)
20605 return false;
20607 /* The types of these expressions depends only on the type to which
20608 the cast occurs. */
20609 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20610 || TREE_CODE (expression) == STATIC_CAST_EXPR
20611 || TREE_CODE (expression) == CONST_CAST_EXPR
20612 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20613 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20614 || TREE_CODE (expression) == CAST_EXPR)
20615 return dependent_type_p (TREE_TYPE (expression));
20617 /* The types of these expressions depends only on the type created
20618 by the expression. */
20619 if (TREE_CODE (expression) == NEW_EXPR
20620 || TREE_CODE (expression) == VEC_NEW_EXPR)
20622 /* For NEW_EXPR tree nodes created inside a template, either
20623 the object type itself or a TREE_LIST may appear as the
20624 operand 1. */
20625 tree type = TREE_OPERAND (expression, 1);
20626 if (TREE_CODE (type) == TREE_LIST)
20627 /* This is an array type. We need to check array dimensions
20628 as well. */
20629 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20630 || value_dependent_expression_p
20631 (TREE_OPERAND (TREE_VALUE (type), 1));
20632 else
20633 return dependent_type_p (type);
20636 if (TREE_CODE (expression) == SCOPE_REF)
20638 tree scope = TREE_OPERAND (expression, 0);
20639 tree name = TREE_OPERAND (expression, 1);
20641 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20642 contains an identifier associated by name lookup with one or more
20643 declarations declared with a dependent type, or...a
20644 nested-name-specifier or qualified-id that names a member of an
20645 unknown specialization. */
20646 return (type_dependent_expression_p (name)
20647 || dependent_scope_p (scope));
20650 if (TREE_CODE (expression) == FUNCTION_DECL
20651 && DECL_LANG_SPECIFIC (expression)
20652 && DECL_TEMPLATE_INFO (expression)
20653 && (any_dependent_template_arguments_p
20654 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20655 return true;
20657 if (TREE_CODE (expression) == TEMPLATE_DECL
20658 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20659 return false;
20661 if (TREE_CODE (expression) == STMT_EXPR)
20662 expression = stmt_expr_value_expr (expression);
20664 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20666 tree elt;
20667 unsigned i;
20669 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20671 if (type_dependent_expression_p (elt))
20672 return true;
20674 return false;
20677 /* A static data member of the current instantiation with incomplete
20678 array type is type-dependent, as the definition and specializations
20679 can have different bounds. */
20680 if (VAR_P (expression)
20681 && DECL_CLASS_SCOPE_P (expression)
20682 && dependent_type_p (DECL_CONTEXT (expression))
20683 && VAR_HAD_UNKNOWN_BOUND (expression))
20684 return true;
20686 /* An array of unknown bound depending on a variadic parameter, eg:
20688 template<typename... Args>
20689 void foo (Args... args)
20691 int arr[] = { args... };
20694 template<int... vals>
20695 void bar ()
20697 int arr[] = { vals... };
20700 If the array has no length and has an initializer, it must be that
20701 we couldn't determine its length in cp_complete_array_type because
20702 it is dependent. */
20703 if (VAR_P (expression)
20704 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20705 && !TYPE_DOMAIN (TREE_TYPE (expression))
20706 && DECL_INITIAL (expression))
20707 return true;
20709 if (TREE_TYPE (expression) == unknown_type_node)
20711 if (TREE_CODE (expression) == ADDR_EXPR)
20712 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20713 if (TREE_CODE (expression) == COMPONENT_REF
20714 || TREE_CODE (expression) == OFFSET_REF)
20716 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20717 return true;
20718 expression = TREE_OPERAND (expression, 1);
20719 if (identifier_p (expression))
20720 return false;
20722 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20723 if (TREE_CODE (expression) == SCOPE_REF)
20724 return false;
20726 /* Always dependent, on the number of arguments if nothing else. */
20727 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20728 return true;
20730 if (BASELINK_P (expression))
20731 expression = BASELINK_FUNCTIONS (expression);
20733 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20735 if (any_dependent_template_arguments_p
20736 (TREE_OPERAND (expression, 1)))
20737 return true;
20738 expression = TREE_OPERAND (expression, 0);
20740 gcc_assert (TREE_CODE (expression) == OVERLOAD
20741 || TREE_CODE (expression) == FUNCTION_DECL);
20743 while (expression)
20745 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20746 return true;
20747 expression = OVL_NEXT (expression);
20749 return false;
20752 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20754 return (dependent_type_p (TREE_TYPE (expression)));
20757 /* walk_tree callback function for instantiation_dependent_expression_p,
20758 below. Returns non-zero if a dependent subexpression is found. */
20760 static tree
20761 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20762 void * /*data*/)
20764 if (TYPE_P (*tp))
20766 /* We don't have to worry about decltype currently because decltype
20767 of an instantiation-dependent expr is a dependent type. This
20768 might change depending on the resolution of DR 1172. */
20769 *walk_subtrees = false;
20770 return NULL_TREE;
20772 enum tree_code code = TREE_CODE (*tp);
20773 switch (code)
20775 /* Don't treat an argument list as dependent just because it has no
20776 TREE_TYPE. */
20777 case TREE_LIST:
20778 case TREE_VEC:
20779 return NULL_TREE;
20781 case VAR_DECL:
20782 case CONST_DECL:
20783 /* A constant with a dependent initializer is dependent. */
20784 if (value_dependent_expression_p (*tp))
20785 return *tp;
20786 break;
20788 case TEMPLATE_PARM_INDEX:
20789 return *tp;
20791 /* Handle expressions with type operands. */
20792 case SIZEOF_EXPR:
20793 case ALIGNOF_EXPR:
20794 case TYPEID_EXPR:
20795 case AT_ENCODE_EXPR:
20797 tree op = TREE_OPERAND (*tp, 0);
20798 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20799 op = TREE_TYPE (op);
20800 if (TYPE_P (op))
20802 if (dependent_type_p (op))
20803 return *tp;
20804 else
20806 *walk_subtrees = false;
20807 return NULL_TREE;
20810 break;
20813 case TRAIT_EXPR:
20814 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20815 || (TRAIT_EXPR_TYPE2 (*tp)
20816 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20817 return *tp;
20818 *walk_subtrees = false;
20819 return NULL_TREE;
20821 case COMPONENT_REF:
20822 if (identifier_p (TREE_OPERAND (*tp, 1)))
20823 /* In a template, finish_class_member_access_expr creates a
20824 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20825 type-dependent, so that we can check access control at
20826 instantiation time (PR 42277). See also Core issue 1273. */
20827 return *tp;
20828 break;
20830 case SCOPE_REF:
20831 if (instantiation_dependent_scope_ref_p (*tp))
20832 return *tp;
20833 else
20834 break;
20836 /* Treat statement-expressions as dependent. */
20837 case BIND_EXPR:
20838 return *tp;
20840 default:
20841 break;
20844 if (type_dependent_expression_p (*tp))
20845 return *tp;
20846 else
20847 return NULL_TREE;
20850 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20851 sense defined by the ABI:
20853 "An expression is instantiation-dependent if it is type-dependent
20854 or value-dependent, or it has a subexpression that is type-dependent
20855 or value-dependent." */
20857 bool
20858 instantiation_dependent_expression_p (tree expression)
20860 tree result;
20862 if (!processing_template_decl)
20863 return false;
20865 if (expression == error_mark_node)
20866 return false;
20868 result = cp_walk_tree_without_duplicates (&expression,
20869 instantiation_dependent_r, NULL);
20870 return result != NULL_TREE;
20873 /* Like type_dependent_expression_p, but it also works while not processing
20874 a template definition, i.e. during substitution or mangling. */
20876 bool
20877 type_dependent_expression_p_push (tree expr)
20879 bool b;
20880 ++processing_template_decl;
20881 b = type_dependent_expression_p (expr);
20882 --processing_template_decl;
20883 return b;
20886 /* Returns TRUE if ARGS contains a type-dependent expression. */
20888 bool
20889 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20891 unsigned int i;
20892 tree arg;
20894 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20896 if (type_dependent_expression_p (arg))
20897 return true;
20899 return false;
20902 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20903 expressions) contains any type-dependent expressions. */
20905 bool
20906 any_type_dependent_elements_p (const_tree list)
20908 for (; list; list = TREE_CHAIN (list))
20909 if (type_dependent_expression_p (TREE_VALUE (list)))
20910 return true;
20912 return false;
20915 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20916 expressions) contains any value-dependent expressions. */
20918 bool
20919 any_value_dependent_elements_p (const_tree list)
20921 for (; list; list = TREE_CHAIN (list))
20922 if (value_dependent_expression_p (TREE_VALUE (list)))
20923 return true;
20925 return false;
20928 /* Returns TRUE if the ARG (a template argument) is dependent. */
20930 bool
20931 dependent_template_arg_p (tree arg)
20933 if (!processing_template_decl)
20934 return false;
20936 /* Assume a template argument that was wrongly written by the user
20937 is dependent. This is consistent with what
20938 any_dependent_template_arguments_p [that calls this function]
20939 does. */
20940 if (!arg || arg == error_mark_node)
20941 return true;
20943 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20944 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20946 if (TREE_CODE (arg) == TEMPLATE_DECL
20947 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20948 return dependent_template_p (arg);
20949 else if (ARGUMENT_PACK_P (arg))
20951 tree args = ARGUMENT_PACK_ARGS (arg);
20952 int i, len = TREE_VEC_LENGTH (args);
20953 for (i = 0; i < len; ++i)
20955 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20956 return true;
20959 return false;
20961 else if (TYPE_P (arg))
20962 return dependent_type_p (arg);
20963 else
20964 return (type_dependent_expression_p (arg)
20965 || value_dependent_expression_p (arg));
20968 /* Returns true if ARGS (a collection of template arguments) contains
20969 any types that require structural equality testing. */
20971 bool
20972 any_template_arguments_need_structural_equality_p (tree args)
20974 int i;
20975 int j;
20977 if (!args)
20978 return false;
20979 if (args == error_mark_node)
20980 return true;
20982 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20984 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20985 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20987 tree arg = TREE_VEC_ELT (level, j);
20988 tree packed_args = NULL_TREE;
20989 int k, len = 1;
20991 if (ARGUMENT_PACK_P (arg))
20993 /* Look inside the argument pack. */
20994 packed_args = ARGUMENT_PACK_ARGS (arg);
20995 len = TREE_VEC_LENGTH (packed_args);
20998 for (k = 0; k < len; ++k)
21000 if (packed_args)
21001 arg = TREE_VEC_ELT (packed_args, k);
21003 if (error_operand_p (arg))
21004 return true;
21005 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21006 continue;
21007 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21008 return true;
21009 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21010 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21011 return true;
21016 return false;
21019 /* Returns true if ARGS (a collection of template arguments) contains
21020 any dependent arguments. */
21022 bool
21023 any_dependent_template_arguments_p (const_tree args)
21025 int i;
21026 int j;
21028 if (!args)
21029 return false;
21030 if (args == error_mark_node)
21031 return true;
21033 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21035 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21036 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21037 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21038 return true;
21041 return false;
21044 /* Returns TRUE if the template TMPL is dependent. */
21046 bool
21047 dependent_template_p (tree tmpl)
21049 if (TREE_CODE (tmpl) == OVERLOAD)
21051 while (tmpl)
21053 if (dependent_template_p (OVL_CURRENT (tmpl)))
21054 return true;
21055 tmpl = OVL_NEXT (tmpl);
21057 return false;
21060 /* Template template parameters are dependent. */
21061 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21062 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21063 return true;
21064 /* So are names that have not been looked up. */
21065 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21066 return true;
21067 /* So are member templates of dependent classes. */
21068 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21069 return dependent_type_p (DECL_CONTEXT (tmpl));
21070 return false;
21073 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21075 bool
21076 dependent_template_id_p (tree tmpl, tree args)
21078 return (dependent_template_p (tmpl)
21079 || any_dependent_template_arguments_p (args));
21082 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21083 is dependent. */
21085 bool
21086 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21088 int i;
21090 if (!processing_template_decl)
21091 return false;
21093 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21095 tree decl = TREE_VEC_ELT (declv, i);
21096 tree init = TREE_VEC_ELT (initv, i);
21097 tree cond = TREE_VEC_ELT (condv, i);
21098 tree incr = TREE_VEC_ELT (incrv, i);
21100 if (type_dependent_expression_p (decl))
21101 return true;
21103 if (init && type_dependent_expression_p (init))
21104 return true;
21106 if (type_dependent_expression_p (cond))
21107 return true;
21109 if (COMPARISON_CLASS_P (cond)
21110 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21111 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21112 return true;
21114 if (TREE_CODE (incr) == MODOP_EXPR)
21116 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21117 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21118 return true;
21120 else if (type_dependent_expression_p (incr))
21121 return true;
21122 else if (TREE_CODE (incr) == MODIFY_EXPR)
21124 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21125 return true;
21126 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21128 tree t = TREE_OPERAND (incr, 1);
21129 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21130 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21131 return true;
21136 return false;
21139 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21140 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21141 no such TYPE can be found. Note that this function peers inside
21142 uninstantiated templates and therefore should be used only in
21143 extremely limited situations. ONLY_CURRENT_P restricts this
21144 peering to the currently open classes hierarchy (which is required
21145 when comparing types). */
21147 tree
21148 resolve_typename_type (tree type, bool only_current_p)
21150 tree scope;
21151 tree name;
21152 tree decl;
21153 int quals;
21154 tree pushed_scope;
21155 tree result;
21157 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21159 scope = TYPE_CONTEXT (type);
21160 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21161 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21162 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21163 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21164 identifier of the TYPENAME_TYPE anymore.
21165 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21166 TYPENAME_TYPE instead, we avoid messing up with a possible
21167 typedef variant case. */
21168 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21170 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21171 it first before we can figure out what NAME refers to. */
21172 if (TREE_CODE (scope) == TYPENAME_TYPE)
21174 if (TYPENAME_IS_RESOLVING_P (scope))
21175 /* Given a class template A with a dependent base with nested type C,
21176 typedef typename A::C::C C will land us here, as trying to resolve
21177 the initial A::C leads to the local C typedef, which leads back to
21178 A::C::C. So we break the recursion now. */
21179 return type;
21180 else
21181 scope = resolve_typename_type (scope, only_current_p);
21183 /* If we don't know what SCOPE refers to, then we cannot resolve the
21184 TYPENAME_TYPE. */
21185 if (TREE_CODE (scope) == TYPENAME_TYPE)
21186 return type;
21187 /* If the SCOPE is a template type parameter, we have no way of
21188 resolving the name. */
21189 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21190 return type;
21191 /* If the SCOPE is not the current instantiation, there's no reason
21192 to look inside it. */
21193 if (only_current_p && !currently_open_class (scope))
21194 return type;
21195 /* If this is a typedef, we don't want to look inside (c++/11987). */
21196 if (typedef_variant_p (type))
21197 return type;
21198 /* If SCOPE isn't the template itself, it will not have a valid
21199 TYPE_FIELDS list. */
21200 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21201 /* scope is either the template itself or a compatible instantiation
21202 like X<T>, so look up the name in the original template. */
21203 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21204 else
21205 /* scope is a partial instantiation, so we can't do the lookup or we
21206 will lose the template arguments. */
21207 return type;
21208 /* Enter the SCOPE so that name lookup will be resolved as if we
21209 were in the class definition. In particular, SCOPE will no
21210 longer be considered a dependent type. */
21211 pushed_scope = push_scope (scope);
21212 /* Look up the declaration. */
21213 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21214 tf_warning_or_error);
21216 result = NULL_TREE;
21218 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21219 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21220 if (!decl)
21221 /*nop*/;
21222 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21223 && TREE_CODE (decl) == TYPE_DECL)
21225 result = TREE_TYPE (decl);
21226 if (result == error_mark_node)
21227 result = NULL_TREE;
21229 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21230 && DECL_CLASS_TEMPLATE_P (decl))
21232 tree tmpl;
21233 tree args;
21234 /* Obtain the template and the arguments. */
21235 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21236 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21237 /* Instantiate the template. */
21238 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21239 /*entering_scope=*/0,
21240 tf_error | tf_user);
21241 if (result == error_mark_node)
21242 result = NULL_TREE;
21245 /* Leave the SCOPE. */
21246 if (pushed_scope)
21247 pop_scope (pushed_scope);
21249 /* If we failed to resolve it, return the original typename. */
21250 if (!result)
21251 return type;
21253 /* If lookup found a typename type, resolve that too. */
21254 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21256 /* Ill-formed programs can cause infinite recursion here, so we
21257 must catch that. */
21258 TYPENAME_IS_RESOLVING_P (type) = 1;
21259 result = resolve_typename_type (result, only_current_p);
21260 TYPENAME_IS_RESOLVING_P (type) = 0;
21263 /* Qualify the resulting type. */
21264 quals = cp_type_quals (type);
21265 if (quals)
21266 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21268 return result;
21271 /* EXPR is an expression which is not type-dependent. Return a proxy
21272 for EXPR that can be used to compute the types of larger
21273 expressions containing EXPR. */
21275 tree
21276 build_non_dependent_expr (tree expr)
21278 tree inner_expr;
21280 #ifdef ENABLE_CHECKING
21281 /* Try to get a constant value for all non-dependent expressions in
21282 order to expose bugs in *_dependent_expression_p and constexpr. */
21283 if (cxx_dialect >= cxx11)
21284 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21285 #endif
21287 /* Preserve OVERLOADs; the functions must be available to resolve
21288 types. */
21289 inner_expr = expr;
21290 if (TREE_CODE (inner_expr) == STMT_EXPR)
21291 inner_expr = stmt_expr_value_expr (inner_expr);
21292 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21293 inner_expr = TREE_OPERAND (inner_expr, 0);
21294 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21295 inner_expr = TREE_OPERAND (inner_expr, 1);
21296 if (is_overloaded_fn (inner_expr)
21297 || TREE_CODE (inner_expr) == OFFSET_REF)
21298 return expr;
21299 /* There is no need to return a proxy for a variable. */
21300 if (VAR_P (expr))
21301 return expr;
21302 /* Preserve string constants; conversions from string constants to
21303 "char *" are allowed, even though normally a "const char *"
21304 cannot be used to initialize a "char *". */
21305 if (TREE_CODE (expr) == STRING_CST)
21306 return expr;
21307 /* Preserve arithmetic constants, as an optimization -- there is no
21308 reason to create a new node. */
21309 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21310 return expr;
21311 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21312 There is at least one place where we want to know that a
21313 particular expression is a throw-expression: when checking a ?:
21314 expression, there are special rules if the second or third
21315 argument is a throw-expression. */
21316 if (TREE_CODE (expr) == THROW_EXPR)
21317 return expr;
21319 /* Don't wrap an initializer list, we need to be able to look inside. */
21320 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21321 return expr;
21323 /* Don't wrap a dummy object, we need to be able to test for it. */
21324 if (is_dummy_object (expr))
21325 return expr;
21327 if (TREE_CODE (expr) == COND_EXPR)
21328 return build3 (COND_EXPR,
21329 TREE_TYPE (expr),
21330 TREE_OPERAND (expr, 0),
21331 (TREE_OPERAND (expr, 1)
21332 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21333 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21334 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21335 if (TREE_CODE (expr) == COMPOUND_EXPR
21336 && !COMPOUND_EXPR_OVERLOADED (expr))
21337 return build2 (COMPOUND_EXPR,
21338 TREE_TYPE (expr),
21339 TREE_OPERAND (expr, 0),
21340 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21342 /* If the type is unknown, it can't really be non-dependent */
21343 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21345 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21346 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21349 /* ARGS is a vector of expressions as arguments to a function call.
21350 Replace the arguments with equivalent non-dependent expressions.
21351 This modifies ARGS in place. */
21353 void
21354 make_args_non_dependent (vec<tree, va_gc> *args)
21356 unsigned int ix;
21357 tree arg;
21359 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21361 tree newarg = build_non_dependent_expr (arg);
21362 if (newarg != arg)
21363 (*args)[ix] = newarg;
21367 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21368 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21369 parms. */
21371 static tree
21372 make_auto_1 (tree name)
21374 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21375 TYPE_NAME (au) = build_decl (input_location,
21376 TYPE_DECL, name, au);
21377 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21378 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21379 (0, processing_template_decl + 1, processing_template_decl + 1,
21380 TYPE_NAME (au), NULL_TREE);
21381 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21382 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21383 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21385 return au;
21388 tree
21389 make_decltype_auto (void)
21391 return make_auto_1 (get_identifier ("decltype(auto)"));
21394 tree
21395 make_auto (void)
21397 return make_auto_1 (get_identifier ("auto"));
21400 /* Given type ARG, return std::initializer_list<ARG>. */
21402 static tree
21403 listify (tree arg)
21405 tree std_init_list = namespace_binding
21406 (get_identifier ("initializer_list"), std_node);
21407 tree argvec;
21408 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21410 error ("deducing from brace-enclosed initializer list requires "
21411 "#include <initializer_list>");
21412 return error_mark_node;
21414 argvec = make_tree_vec (1);
21415 TREE_VEC_ELT (argvec, 0) = arg;
21416 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21417 NULL_TREE, 0, tf_warning_or_error);
21420 /* Replace auto in TYPE with std::initializer_list<auto>. */
21422 static tree
21423 listify_autos (tree type, tree auto_node)
21425 tree init_auto = listify (auto_node);
21426 tree argvec = make_tree_vec (1);
21427 TREE_VEC_ELT (argvec, 0) = init_auto;
21428 if (processing_template_decl)
21429 argvec = add_to_template_args (current_template_args (), argvec);
21430 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21433 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21434 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21436 tree
21437 do_auto_deduction (tree type, tree init, tree auto_node)
21439 tree targs;
21441 if (init == error_mark_node)
21442 return error_mark_node;
21444 if (type_dependent_expression_p (init))
21445 /* Defining a subset of type-dependent expressions that we can deduce
21446 from ahead of time isn't worth the trouble. */
21447 return type;
21449 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21450 with either a new invented type template parameter U or, if the
21451 initializer is a braced-init-list (8.5.4), with
21452 std::initializer_list<U>. */
21453 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21454 type = listify_autos (type, auto_node);
21456 init = resolve_nondeduced_context (init);
21458 targs = make_tree_vec (1);
21459 if (AUTO_IS_DECLTYPE (auto_node))
21461 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21462 TREE_VEC_ELT (targs, 0)
21463 = finish_decltype_type (init, id, tf_warning_or_error);
21464 if (type != auto_node)
21466 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21467 return error_mark_node;
21470 else
21472 tree parms = build_tree_list (NULL_TREE, type);
21473 tree tparms = make_tree_vec (1);
21474 int val;
21476 TREE_VEC_ELT (tparms, 0)
21477 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21478 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21479 DEDUCE_CALL, LOOKUP_NORMAL,
21480 NULL, /*explain_p=*/false);
21481 if (val > 0)
21483 if (processing_template_decl)
21484 /* Try again at instantiation time. */
21485 return type;
21486 if (type && type != error_mark_node)
21487 /* If type is error_mark_node a diagnostic must have been
21488 emitted by now. Also, having a mention to '<type error>'
21489 in the diagnostic is not really useful to the user. */
21491 if (cfun && auto_node == current_function_auto_return_pattern
21492 && LAMBDA_FUNCTION_P (current_function_decl))
21493 error ("unable to deduce lambda return type from %qE", init);
21494 else
21495 error ("unable to deduce %qT from %qE", type, init);
21497 return error_mark_node;
21501 /* If the list of declarators contains more than one declarator, the type
21502 of each declared variable is determined as described above. If the
21503 type deduced for the template parameter U is not the same in each
21504 deduction, the program is ill-formed. */
21505 if (TREE_TYPE (auto_node)
21506 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21508 if (cfun && auto_node == current_function_auto_return_pattern
21509 && LAMBDA_FUNCTION_P (current_function_decl))
21510 error ("inconsistent types %qT and %qT deduced for "
21511 "lambda return type", TREE_TYPE (auto_node),
21512 TREE_VEC_ELT (targs, 0));
21513 else
21514 error ("inconsistent deduction for %qT: %qT and then %qT",
21515 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21516 return error_mark_node;
21518 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21520 if (processing_template_decl)
21521 targs = add_to_template_args (current_template_args (), targs);
21522 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21525 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21526 result. */
21528 tree
21529 splice_late_return_type (tree type, tree late_return_type)
21531 tree argvec;
21533 if (late_return_type == NULL_TREE)
21534 return type;
21535 argvec = make_tree_vec (1);
21536 TREE_VEC_ELT (argvec, 0) = late_return_type;
21537 if (processing_template_parmlist)
21538 /* For a late-specified return type in a template type-parameter, we
21539 need to add a dummy argument level for its parmlist. */
21540 argvec = add_to_template_args
21541 (make_tree_vec (processing_template_parmlist), argvec);
21542 if (current_template_parms)
21543 argvec = add_to_template_args (current_template_args (), argvec);
21544 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21547 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21548 'decltype(auto)'. */
21550 bool
21551 is_auto (const_tree type)
21553 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21554 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21555 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21556 return true;
21557 else
21558 return false;
21561 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21562 a use of `auto'. Returns NULL_TREE otherwise. */
21564 tree
21565 type_uses_auto (tree type)
21567 return find_type_usage (type, is_auto);
21570 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21571 'decltype(auto)' or a concept. */
21573 bool
21574 is_auto_or_concept (const_tree type)
21576 return is_auto (type); // or concept
21579 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21580 a concept identifier) iff TYPE contains a use of a generic type. Returns
21581 NULL_TREE otherwise. */
21583 tree
21584 type_uses_auto_or_concept (tree type)
21586 return find_type_usage (type, is_auto_or_concept);
21590 /* For a given template T, return the vector of typedefs referenced
21591 in T for which access check is needed at T instantiation time.
21592 T is either a FUNCTION_DECL or a RECORD_TYPE.
21593 Those typedefs were added to T by the function
21594 append_type_to_template_for_access_check. */
21596 vec<qualified_typedef_usage_t, va_gc> *
21597 get_types_needing_access_check (tree t)
21599 tree ti;
21600 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21602 if (!t || t == error_mark_node)
21603 return NULL;
21605 if (!(ti = get_template_info (t)))
21606 return NULL;
21608 if (CLASS_TYPE_P (t)
21609 || TREE_CODE (t) == FUNCTION_DECL)
21611 if (!TI_TEMPLATE (ti))
21612 return NULL;
21614 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21617 return result;
21620 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21621 tied to T. That list of typedefs will be access checked at
21622 T instantiation time.
21623 T is either a FUNCTION_DECL or a RECORD_TYPE.
21624 TYPE_DECL is a TYPE_DECL node representing a typedef.
21625 SCOPE is the scope through which TYPE_DECL is accessed.
21626 LOCATION is the location of the usage point of TYPE_DECL.
21628 This function is a subroutine of
21629 append_type_to_template_for_access_check. */
21631 static void
21632 append_type_to_template_for_access_check_1 (tree t,
21633 tree type_decl,
21634 tree scope,
21635 location_t location)
21637 qualified_typedef_usage_t typedef_usage;
21638 tree ti;
21640 if (!t || t == error_mark_node)
21641 return;
21643 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21644 || CLASS_TYPE_P (t))
21645 && type_decl
21646 && TREE_CODE (type_decl) == TYPE_DECL
21647 && scope);
21649 if (!(ti = get_template_info (t)))
21650 return;
21652 gcc_assert (TI_TEMPLATE (ti));
21654 typedef_usage.typedef_decl = type_decl;
21655 typedef_usage.context = scope;
21656 typedef_usage.locus = location;
21658 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21661 /* Append TYPE_DECL to the template TEMPL.
21662 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21663 At TEMPL instanciation time, TYPE_DECL will be checked to see
21664 if it can be accessed through SCOPE.
21665 LOCATION is the location of the usage point of TYPE_DECL.
21667 e.g. consider the following code snippet:
21669 class C
21671 typedef int myint;
21674 template<class U> struct S
21676 C::myint mi; // <-- usage point of the typedef C::myint
21679 S<char> s;
21681 At S<char> instantiation time, we need to check the access of C::myint
21682 In other words, we need to check the access of the myint typedef through
21683 the C scope. For that purpose, this function will add the myint typedef
21684 and the scope C through which its being accessed to a list of typedefs
21685 tied to the template S. That list will be walked at template instantiation
21686 time and access check performed on each typedefs it contains.
21687 Note that this particular code snippet should yield an error because
21688 myint is private to C. */
21690 void
21691 append_type_to_template_for_access_check (tree templ,
21692 tree type_decl,
21693 tree scope,
21694 location_t location)
21696 qualified_typedef_usage_t *iter;
21697 unsigned i;
21699 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21701 /* Make sure we don't append the type to the template twice. */
21702 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21703 if (iter->typedef_decl == type_decl && scope == iter->context)
21704 return;
21706 append_type_to_template_for_access_check_1 (templ, type_decl,
21707 scope, location);
21710 /* Convert the generic type parameters in PARM that match the types given in the
21711 range [START_IDX, END_IDX) from the current_template_parms into generic type
21712 packs. */
21714 tree
21715 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21717 tree current = current_template_parms;
21718 int depth = TMPL_PARMS_DEPTH (current);
21719 current = INNERMOST_TEMPLATE_PARMS (current);
21720 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21722 for (int i = 0; i < start_idx; ++i)
21723 TREE_VEC_ELT (replacement, i)
21724 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21726 for (int i = start_idx; i < end_idx; ++i)
21728 /* Create a distinct parameter pack type from the current parm and add it
21729 to the replacement args to tsubst below into the generic function
21730 parameter. */
21732 tree o = TREE_TYPE (TREE_VALUE
21733 (TREE_VEC_ELT (current, i)));
21734 tree t = copy_type (o);
21735 TEMPLATE_TYPE_PARM_INDEX (t)
21736 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21737 o, 0, 0, tf_none);
21738 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21739 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21740 TYPE_MAIN_VARIANT (t) = t;
21741 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21742 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21743 TREE_VEC_ELT (replacement, i) = t;
21744 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21747 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21748 TREE_VEC_ELT (replacement, i)
21749 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21751 /* If there are more levels then build up the replacement with the outer
21752 template parms. */
21753 if (depth > 1)
21754 replacement = add_to_template_args (template_parms_to_args
21755 (TREE_CHAIN (current_template_parms)),
21756 replacement);
21758 return tsubst (parm, replacement, tf_none, NULL_TREE);
21762 /* Set up the hash tables for template instantiations. */
21764 void
21765 init_template_processing (void)
21767 decl_specializations = htab_create_ggc (37,
21768 hash_specialization,
21769 eq_specializations,
21770 ggc_free);
21771 type_specializations = htab_create_ggc (37,
21772 hash_specialization,
21773 eq_specializations,
21774 ggc_free);
21777 /* Print stats about the template hash tables for -fstats. */
21779 void
21780 print_template_statistics (void)
21782 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21783 "%f collisions\n", (long) htab_size (decl_specializations),
21784 (long) htab_elements (decl_specializations),
21785 htab_collisions (decl_specializations));
21786 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21787 "%f collisions\n", (long) htab_size (type_specializations),
21788 (long) htab_elements (type_specializations),
21789 htab_collisions (type_specializations));
21792 #include "gt-cp-pt.h"